blob: 31ffd663f7d107c5193c21121cffcaaaa766a0e3 [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,
Takashi Iwai7dddf2ae2013-01-24 16:31:35 +0100572 unsigned int dir, unsigned int idx)
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
Takashi Iwai7dddf2ae2013-01-24 16:31:35 +0100645 if (!enable && is_active_nid(codec, nid, dir, idx_to_check))
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++) {
Takashi Iwaie4a395e2013-01-23 17:00:31 +0100691 if (n != idx && (!add_aamix || conn[n] != spec->mixer_merge_nid))
Takashi Iwai352f7f92012-12-19 12:52:06 +0100692 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 Iwai3e367f12013-01-23 17:07:23 +0100738/* re-initialize the path specified by the given path index */
739static void resume_path_from_idx(struct hda_codec *codec, int path_idx)
740{
741 struct nid_path *path = snd_hda_get_path_from_idx(codec, path_idx);
742 if (path)
743 snd_hda_activate_path(codec, path, path->active, false);
744}
745
Takashi Iwai352f7f92012-12-19 12:52:06 +0100746
747/*
748 * Helper functions for creating mixer ctl elements
749 */
750
751enum {
752 HDA_CTL_WIDGET_VOL,
753 HDA_CTL_WIDGET_MUTE,
754 HDA_CTL_BIND_MUTE,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100755};
756static const struct snd_kcontrol_new control_templates[] = {
757 HDA_CODEC_VOLUME(NULL, 0, 0, 0),
758 HDA_CODEC_MUTE(NULL, 0, 0, 0),
759 HDA_BIND_MUTE(NULL, 0, 0, 0),
Takashi Iwai352f7f92012-12-19 12:52:06 +0100760};
761
762/* add dynamic controls from template */
Takashi Iwaia35bd1e2013-01-18 14:01:14 +0100763static struct snd_kcontrol_new *
764add_control(struct hda_gen_spec *spec, int type, const char *name,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100765 int cidx, unsigned long val)
766{
767 struct snd_kcontrol_new *knew;
768
Takashi Iwai12c93df2012-12-19 14:38:33 +0100769 knew = snd_hda_gen_add_kctl(spec, name, &control_templates[type]);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100770 if (!knew)
Takashi Iwaia35bd1e2013-01-18 14:01:14 +0100771 return NULL;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100772 knew->index = cidx;
773 if (get_amp_nid_(val))
774 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
775 knew->private_value = val;
Takashi Iwaia35bd1e2013-01-18 14:01:14 +0100776 return knew;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100777}
778
779static int add_control_with_pfx(struct hda_gen_spec *spec, int type,
780 const char *pfx, const char *dir,
781 const char *sfx, int cidx, unsigned long val)
782{
783 char name[32];
784 snprintf(name, sizeof(name), "%s %s %s", pfx, dir, sfx);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +0100785 if (!add_control(spec, type, name, cidx, val))
786 return -ENOMEM;
787 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100788}
789
790#define add_pb_vol_ctrl(spec, type, pfx, val) \
791 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", 0, val)
792#define add_pb_sw_ctrl(spec, type, pfx, val) \
793 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", 0, val)
794#define __add_pb_vol_ctrl(spec, type, pfx, cidx, val) \
795 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", cidx, val)
796#define __add_pb_sw_ctrl(spec, type, pfx, cidx, val) \
797 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", cidx, val)
798
799static int add_vol_ctl(struct hda_codec *codec, const char *pfx, int cidx,
800 unsigned int chs, struct nid_path *path)
801{
802 unsigned int val;
803 if (!path)
804 return 0;
805 val = path->ctls[NID_PATH_VOL_CTL];
806 if (!val)
807 return 0;
808 val = amp_val_replace_channels(val, chs);
809 return __add_pb_vol_ctrl(codec->spec, HDA_CTL_WIDGET_VOL, pfx, cidx, val);
810}
811
812/* return the channel bits suitable for the given path->ctls[] */
813static int get_default_ch_nums(struct hda_codec *codec, struct nid_path *path,
814 int type)
815{
816 int chs = 1; /* mono (left only) */
817 if (path) {
818 hda_nid_t nid = get_amp_nid_(path->ctls[type]);
819 if (nid && (get_wcaps(codec, nid) & AC_WCAP_STEREO))
820 chs = 3; /* stereo */
821 }
822 return chs;
823}
824
825static int add_stereo_vol(struct hda_codec *codec, const char *pfx, int cidx,
826 struct nid_path *path)
827{
828 int chs = get_default_ch_nums(codec, path, NID_PATH_VOL_CTL);
829 return add_vol_ctl(codec, pfx, cidx, chs, path);
830}
831
832/* create a mute-switch for the given mixer widget;
833 * if it has multiple sources (e.g. DAC and loopback), create a bind-mute
834 */
835static int add_sw_ctl(struct hda_codec *codec, const char *pfx, int cidx,
836 unsigned int chs, struct nid_path *path)
837{
838 unsigned int val;
839 int type = HDA_CTL_WIDGET_MUTE;
840
841 if (!path)
842 return 0;
843 val = path->ctls[NID_PATH_MUTE_CTL];
844 if (!val)
845 return 0;
846 val = amp_val_replace_channels(val, chs);
847 if (get_amp_direction_(val) == HDA_INPUT) {
848 hda_nid_t nid = get_amp_nid_(val);
849 int nums = snd_hda_get_num_conns(codec, nid);
850 if (nums > 1) {
851 type = HDA_CTL_BIND_MUTE;
852 val |= nums << 19;
853 }
854 }
855 return __add_pb_sw_ctrl(codec->spec, type, pfx, cidx, val);
856}
857
858static int add_stereo_sw(struct hda_codec *codec, const char *pfx,
859 int cidx, struct nid_path *path)
860{
861 int chs = get_default_ch_nums(codec, path, NID_PATH_MUTE_CTL);
862 return add_sw_ctl(codec, pfx, cidx, chs, path);
863}
864
Takashi Iwai247d85e2013-01-17 16:18:11 +0100865/* any ctl assigned to the path with the given index? */
866static bool path_has_mixer(struct hda_codec *codec, int path_idx, int ctl_type)
867{
868 struct nid_path *path = snd_hda_get_path_from_idx(codec, path_idx);
869 return path && path->ctls[ctl_type];
870}
871
Takashi Iwai352f7f92012-12-19 12:52:06 +0100872static const char * const channel_name[4] = {
873 "Front", "Surround", "CLFE", "Side"
874};
875
876/* give some appropriate ctl name prefix for the given line out channel */
Takashi Iwai247d85e2013-01-17 16:18:11 +0100877static const char *get_line_out_pfx(struct hda_codec *codec, int ch,
878 int *index, int ctl_type)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100879{
Takashi Iwai247d85e2013-01-17 16:18:11 +0100880 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100881 struct auto_pin_cfg *cfg = &spec->autocfg;
882
883 *index = 0;
884 if (cfg->line_outs == 1 && !spec->multi_ios &&
Takashi Iwai247d85e2013-01-17 16:18:11 +0100885 !cfg->hp_outs && !cfg->speaker_outs)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100886 return spec->vmaster_mute.hook ? "PCM" : "Master";
887
888 /* if there is really a single DAC used in the whole output paths,
889 * use it master (or "PCM" if a vmaster hook is present)
890 */
891 if (spec->multiout.num_dacs == 1 && !spec->mixer_nid &&
892 !spec->multiout.hp_out_nid[0] && !spec->multiout.extra_out_nid[0])
893 return spec->vmaster_mute.hook ? "PCM" : "Master";
894
Takashi Iwai247d85e2013-01-17 16:18:11 +0100895 /* multi-io channels */
896 if (ch >= cfg->line_outs)
897 return channel_name[ch];
898
Takashi Iwai352f7f92012-12-19 12:52:06 +0100899 switch (cfg->line_out_type) {
900 case AUTO_PIN_SPEAKER_OUT:
Takashi Iwai247d85e2013-01-17 16:18:11 +0100901 /* if the primary channel vol/mute is shared with HP volume,
902 * don't name it as Speaker
903 */
904 if (!ch && cfg->hp_outs &&
905 !path_has_mixer(codec, spec->hp_paths[0], ctl_type))
906 break;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100907 if (cfg->line_outs == 1)
908 return "Speaker";
909 if (cfg->line_outs == 2)
910 return ch ? "Bass Speaker" : "Speaker";
911 break;
912 case AUTO_PIN_HP_OUT:
Takashi Iwai247d85e2013-01-17 16:18:11 +0100913 /* if the primary channel vol/mute is shared with spk volume,
914 * don't name it as Headphone
915 */
916 if (!ch && cfg->speaker_outs &&
917 !path_has_mixer(codec, spec->speaker_paths[0], ctl_type))
918 break;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100919 /* for multi-io case, only the primary out */
920 if (ch && spec->multi_ios)
921 break;
922 *index = ch;
923 return "Headphone";
Takashi Iwai352f7f92012-12-19 12:52:06 +0100924 }
Takashi Iwai247d85e2013-01-17 16:18:11 +0100925
926 /* for a single channel output, we don't have to name the channel */
927 if (cfg->line_outs == 1 && !spec->multi_ios)
928 return "PCM";
929
Takashi Iwai352f7f92012-12-19 12:52:06 +0100930 if (ch >= ARRAY_SIZE(channel_name)) {
931 snd_BUG();
932 return "PCM";
933 }
934
935 return channel_name[ch];
936}
937
938/*
939 * Parse output paths
940 */
941
942/* badness definition */
943enum {
944 /* No primary DAC is found for the main output */
945 BAD_NO_PRIMARY_DAC = 0x10000,
946 /* No DAC is found for the extra output */
947 BAD_NO_DAC = 0x4000,
948 /* No possible multi-ios */
949 BAD_MULTI_IO = 0x103,
950 /* No individual DAC for extra output */
951 BAD_NO_EXTRA_DAC = 0x102,
952 /* No individual DAC for extra surrounds */
953 BAD_NO_EXTRA_SURR_DAC = 0x101,
954 /* Primary DAC shared with main surrounds */
955 BAD_SHARED_SURROUND = 0x100,
956 /* Primary DAC shared with main CLFE */
957 BAD_SHARED_CLFE = 0x10,
958 /* Primary DAC shared with extra surrounds */
959 BAD_SHARED_EXTRA_SURROUND = 0x10,
960 /* Volume widget is shared */
961 BAD_SHARED_VOL = 0x10,
962};
963
Takashi Iwai0e614dd2013-01-07 15:11:44 +0100964/* look for widgets in the given path which are appropriate for
Takashi Iwai352f7f92012-12-19 12:52:06 +0100965 * volume and mute controls, and assign the values to ctls[].
966 *
967 * When no appropriate widget is found in the path, the badness value
968 * is incremented depending on the situation. The function returns the
969 * total badness for both volume and mute controls.
970 */
Takashi Iwai0e614dd2013-01-07 15:11:44 +0100971static int assign_out_path_ctls(struct hda_codec *codec, struct nid_path *path)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100972{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100973 hda_nid_t nid;
974 unsigned int val;
975 int badness = 0;
976
977 if (!path)
978 return BAD_SHARED_VOL * 2;
Takashi Iwai0e614dd2013-01-07 15:11:44 +0100979
980 if (path->ctls[NID_PATH_VOL_CTL] ||
981 path->ctls[NID_PATH_MUTE_CTL])
982 return 0; /* already evaluated */
983
Takashi Iwai352f7f92012-12-19 12:52:06 +0100984 nid = look_for_out_vol_nid(codec, path);
985 if (nid) {
986 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
987 if (is_ctl_used(codec, val, NID_PATH_VOL_CTL))
988 badness += BAD_SHARED_VOL;
989 else
990 path->ctls[NID_PATH_VOL_CTL] = val;
991 } else
992 badness += BAD_SHARED_VOL;
993 nid = look_for_out_mute_nid(codec, path);
994 if (nid) {
995 unsigned int wid_type = get_wcaps_type(get_wcaps(codec, nid));
996 if (wid_type == AC_WID_PIN || wid_type == AC_WID_AUD_OUT ||
997 nid_has_mute(codec, nid, HDA_OUTPUT))
998 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
999 else
1000 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT);
1001 if (is_ctl_used(codec, val, NID_PATH_MUTE_CTL))
1002 badness += BAD_SHARED_VOL;
1003 else
1004 path->ctls[NID_PATH_MUTE_CTL] = val;
1005 } else
1006 badness += BAD_SHARED_VOL;
1007 return badness;
1008}
1009
1010struct badness_table {
1011 int no_primary_dac; /* no primary DAC */
1012 int no_dac; /* no secondary DACs */
1013 int shared_primary; /* primary DAC is shared with main output */
1014 int shared_surr; /* secondary DAC shared with main or primary */
1015 int shared_clfe; /* third DAC shared with main or primary */
1016 int shared_surr_main; /* secondary DAC sahred with main/DAC0 */
1017};
1018
1019static struct badness_table main_out_badness = {
1020 .no_primary_dac = BAD_NO_PRIMARY_DAC,
1021 .no_dac = BAD_NO_DAC,
1022 .shared_primary = BAD_NO_PRIMARY_DAC,
1023 .shared_surr = BAD_SHARED_SURROUND,
1024 .shared_clfe = BAD_SHARED_CLFE,
1025 .shared_surr_main = BAD_SHARED_SURROUND,
1026};
1027
1028static struct badness_table extra_out_badness = {
1029 .no_primary_dac = BAD_NO_DAC,
1030 .no_dac = BAD_NO_DAC,
1031 .shared_primary = BAD_NO_EXTRA_DAC,
1032 .shared_surr = BAD_SHARED_EXTRA_SURROUND,
1033 .shared_clfe = BAD_SHARED_EXTRA_SURROUND,
1034 .shared_surr_main = BAD_NO_EXTRA_SURR_DAC,
1035};
1036
Takashi Iwai7385df62013-01-07 09:50:52 +01001037/* get the DAC of the primary output corresponding to the given array index */
1038static hda_nid_t get_primary_out(struct hda_codec *codec, int idx)
1039{
1040 struct hda_gen_spec *spec = codec->spec;
1041 struct auto_pin_cfg *cfg = &spec->autocfg;
1042
1043 if (cfg->line_outs > idx)
1044 return spec->private_dac_nids[idx];
1045 idx -= cfg->line_outs;
1046 if (spec->multi_ios > idx)
1047 return spec->multi_io[idx].dac;
1048 return 0;
1049}
1050
1051/* return the DAC if it's reachable, otherwise zero */
1052static inline hda_nid_t try_dac(struct hda_codec *codec,
1053 hda_nid_t dac, hda_nid_t pin)
1054{
1055 return is_reachable_path(codec, dac, pin) ? dac : 0;
1056}
1057
Takashi Iwai352f7f92012-12-19 12:52:06 +01001058/* try to assign DACs to pins and return the resultant badness */
1059static int try_assign_dacs(struct hda_codec *codec, int num_outs,
1060 const hda_nid_t *pins, hda_nid_t *dacs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001061 int *path_idx,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001062 const struct badness_table *bad)
1063{
1064 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001065 int i, j;
1066 int badness = 0;
1067 hda_nid_t dac;
1068
1069 if (!num_outs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 return 0;
1071
Takashi Iwai352f7f92012-12-19 12:52:06 +01001072 for (i = 0; i < num_outs; i++) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001073 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001074 hda_nid_t pin = pins[i];
Takashi Iwai1e0b5282013-01-04 12:56:52 +01001075
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001076 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
1077 if (path) {
1078 badness += assign_out_path_ctls(codec, path);
Takashi Iwai1e0b5282013-01-04 12:56:52 +01001079 continue;
1080 }
1081
1082 dacs[i] = look_for_dac(codec, pin, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001083 if (!dacs[i] && !i) {
Takashi Iwai980428c2013-01-09 09:28:20 +01001084 /* try to steal the DAC of surrounds for the front */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001085 for (j = 1; j < num_outs; j++) {
1086 if (is_reachable_path(codec, dacs[j], pin)) {
1087 dacs[0] = dacs[j];
1088 dacs[j] = 0;
Takashi Iwai980428c2013-01-09 09:28:20 +01001089 invalidate_nid_path(codec, path_idx[j]);
Takashi Iwai196c17662013-01-04 15:01:40 +01001090 path_idx[j] = 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001091 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 }
1093 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001094 }
1095 dac = dacs[i];
1096 if (!dac) {
Takashi Iwai7385df62013-01-07 09:50:52 +01001097 if (num_outs > 2)
1098 dac = try_dac(codec, get_primary_out(codec, i), pin);
1099 if (!dac)
1100 dac = try_dac(codec, dacs[0], pin);
1101 if (!dac)
1102 dac = try_dac(codec, get_primary_out(codec, i), pin);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001103 if (dac) {
1104 if (!i)
1105 badness += bad->shared_primary;
1106 else if (i == 1)
1107 badness += bad->shared_surr;
1108 else
1109 badness += bad->shared_clfe;
1110 } else if (is_reachable_path(codec, spec->private_dac_nids[0], pin)) {
1111 dac = spec->private_dac_nids[0];
1112 badness += bad->shared_surr_main;
1113 } else if (!i)
1114 badness += bad->no_primary_dac;
1115 else
1116 badness += bad->no_dac;
1117 }
Takashi Iwai1fa335b2013-01-21 11:43:19 +01001118 if (!dac)
1119 continue;
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001120 path = snd_hda_add_new_path(codec, dac, pin, -spec->mixer_nid);
Takashi Iwai117688a2013-01-04 15:41:41 +01001121 if (!path && !i && spec->mixer_nid) {
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001122 /* try with aamix */
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001123 path = snd_hda_add_new_path(codec, dac, pin, 0);
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001124 }
Takashi Iwai1fa335b2013-01-21 11:43:19 +01001125 if (!path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001126 dac = dacs[i] = 0;
Takashi Iwai1fa335b2013-01-21 11:43:19 +01001127 badness += bad->no_dac;
1128 } else {
Takashi Iwaia7694092013-01-21 10:43:18 +01001129 /* print_nid_path("output", path); */
Takashi Iwaie1284af2013-01-03 16:33:02 +01001130 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +01001131 path_idx[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001132 badness += assign_out_path_ctls(codec, path);
Takashi Iwaie1284af2013-01-03 16:33:02 +01001133 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001134 }
1135
1136 return badness;
1137}
1138
1139/* return NID if the given pin has only a single connection to a certain DAC */
1140static hda_nid_t get_dac_if_single(struct hda_codec *codec, hda_nid_t pin)
1141{
1142 struct hda_gen_spec *spec = codec->spec;
1143 int i;
1144 hda_nid_t nid_found = 0;
1145
1146 for (i = 0; i < spec->num_all_dacs; i++) {
1147 hda_nid_t nid = spec->all_dacs[i];
1148 if (!nid || is_dac_already_used(codec, nid))
1149 continue;
1150 if (is_reachable_path(codec, nid, pin)) {
1151 if (nid_found)
1152 return 0;
1153 nid_found = nid;
1154 }
1155 }
1156 return nid_found;
1157}
1158
1159/* check whether the given pin can be a multi-io pin */
1160static bool can_be_multiio_pin(struct hda_codec *codec,
1161 unsigned int location, hda_nid_t nid)
1162{
1163 unsigned int defcfg, caps;
1164
1165 defcfg = snd_hda_codec_get_pincfg(codec, nid);
1166 if (get_defcfg_connect(defcfg) != AC_JACK_PORT_COMPLEX)
1167 return false;
1168 if (location && get_defcfg_location(defcfg) != location)
1169 return false;
1170 caps = snd_hda_query_pin_caps(codec, nid);
1171 if (!(caps & AC_PINCAP_OUT))
1172 return false;
1173 return true;
1174}
1175
Takashi Iwaie22aab72013-01-04 14:50:04 +01001176/* count the number of input pins that are capable to be multi-io */
1177static int count_multiio_pins(struct hda_codec *codec, hda_nid_t reference_pin)
1178{
1179 struct hda_gen_spec *spec = codec->spec;
1180 struct auto_pin_cfg *cfg = &spec->autocfg;
1181 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1182 unsigned int location = get_defcfg_location(defcfg);
1183 int type, i;
1184 int num_pins = 0;
1185
1186 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1187 for (i = 0; i < cfg->num_inputs; i++) {
1188 if (cfg->inputs[i].type != type)
1189 continue;
1190 if (can_be_multiio_pin(codec, location,
1191 cfg->inputs[i].pin))
1192 num_pins++;
1193 }
1194 }
1195 return num_pins;
1196}
1197
Takashi Iwai352f7f92012-12-19 12:52:06 +01001198/*
1199 * multi-io helper
1200 *
1201 * When hardwired is set, try to fill ony hardwired pins, and returns
1202 * zero if any pins are filled, non-zero if nothing found.
1203 * When hardwired is off, try to fill possible input pins, and returns
1204 * the badness value.
1205 */
1206static int fill_multi_ios(struct hda_codec *codec,
1207 hda_nid_t reference_pin,
Takashi Iwaie22aab72013-01-04 14:50:04 +01001208 bool hardwired)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001209{
1210 struct hda_gen_spec *spec = codec->spec;
1211 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwaie22aab72013-01-04 14:50:04 +01001212 int type, i, j, num_pins, old_pins;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001213 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1214 unsigned int location = get_defcfg_location(defcfg);
1215 int badness = 0;
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001216 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001217
1218 old_pins = spec->multi_ios;
1219 if (old_pins >= 2)
1220 goto end_fill;
1221
Takashi Iwaie22aab72013-01-04 14:50:04 +01001222 num_pins = count_multiio_pins(codec, reference_pin);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001223 if (num_pins < 2)
1224 goto end_fill;
1225
Takashi Iwai352f7f92012-12-19 12:52:06 +01001226 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1227 for (i = 0; i < cfg->num_inputs; i++) {
1228 hda_nid_t nid = cfg->inputs[i].pin;
1229 hda_nid_t dac = 0;
1230
1231 if (cfg->inputs[i].type != type)
1232 continue;
1233 if (!can_be_multiio_pin(codec, location, nid))
1234 continue;
1235 for (j = 0; j < spec->multi_ios; j++) {
1236 if (nid == spec->multi_io[j].pin)
1237 break;
1238 }
1239 if (j < spec->multi_ios)
1240 continue;
1241
Takashi Iwai352f7f92012-12-19 12:52:06 +01001242 if (hardwired)
1243 dac = get_dac_if_single(codec, nid);
1244 else if (!dac)
1245 dac = look_for_dac(codec, nid, false);
1246 if (!dac) {
1247 badness++;
1248 continue;
1249 }
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001250 path = snd_hda_add_new_path(codec, dac, nid,
1251 -spec->mixer_nid);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001252 if (!path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001253 badness++;
1254 continue;
1255 }
Takashi Iwaia7694092013-01-21 10:43:18 +01001256 /* print_nid_path("multiio", path); */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001257 spec->multi_io[spec->multi_ios].pin = nid;
1258 spec->multi_io[spec->multi_ios].dac = dac;
Takashi Iwai196c17662013-01-04 15:01:40 +01001259 spec->out_paths[cfg->line_outs + spec->multi_ios] =
1260 snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001261 spec->multi_ios++;
1262 if (spec->multi_ios >= 2)
1263 break;
1264 }
1265 }
1266 end_fill:
1267 if (badness)
1268 badness = BAD_MULTI_IO;
1269 if (old_pins == spec->multi_ios) {
1270 if (hardwired)
1271 return 1; /* nothing found */
1272 else
1273 return badness; /* no badness if nothing found */
1274 }
1275 if (!hardwired && spec->multi_ios < 2) {
1276 /* cancel newly assigned paths */
1277 spec->paths.used -= spec->multi_ios - old_pins;
1278 spec->multi_ios = old_pins;
1279 return badness;
1280 }
1281
1282 /* assign volume and mute controls */
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001283 for (i = old_pins; i < spec->multi_ios; i++) {
1284 path = snd_hda_get_path_from_idx(codec, spec->out_paths[cfg->line_outs + i]);
1285 badness += assign_out_path_ctls(codec, path);
1286 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001287
1288 return badness;
1289}
1290
1291/* map DACs for all pins in the list if they are single connections */
1292static bool map_singles(struct hda_codec *codec, int outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001293 const hda_nid_t *pins, hda_nid_t *dacs, int *path_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001294{
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001295 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001296 int i;
1297 bool found = false;
1298 for (i = 0; i < outs; i++) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001299 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001300 hda_nid_t dac;
1301 if (dacs[i])
1302 continue;
1303 dac = get_dac_if_single(codec, pins[i]);
1304 if (!dac)
1305 continue;
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001306 path = snd_hda_add_new_path(codec, dac, pins[i],
1307 -spec->mixer_nid);
Takashi Iwai117688a2013-01-04 15:41:41 +01001308 if (!path && !i && spec->mixer_nid)
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001309 path = snd_hda_add_new_path(codec, dac, pins[i], 0);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001310 if (path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001311 dacs[i] = dac;
1312 found = true;
Takashi Iwaia7694092013-01-21 10:43:18 +01001313 /* print_nid_path("output", path); */
Takashi Iwaie1284af2013-01-03 16:33:02 +01001314 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +01001315 path_idx[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001316 }
1317 }
1318 return found;
1319}
1320
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001321/* create a new path including aamix if available, and return its index */
1322static int check_aamix_out_path(struct hda_codec *codec, int path_idx)
1323{
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001324 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001325 struct nid_path *path;
Takashi Iwaif87498b2013-01-21 14:24:31 +01001326 hda_nid_t dac, pin;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001327
1328 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001329 if (!path || !path->depth ||
1330 is_nid_contained(path, spec->mixer_nid))
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001331 return 0;
Takashi Iwaif87498b2013-01-21 14:24:31 +01001332 dac = path->path[0];
1333 pin = path->path[path->depth - 1];
1334 path = snd_hda_add_new_path(codec, dac, pin, spec->mixer_nid);
1335 if (!path) {
1336 if (dac != spec->multiout.dac_nids[0])
1337 dac = spec->multiout.dac_nids[0];
1338 else if (spec->multiout.hp_out_nid[0])
1339 dac = spec->multiout.hp_out_nid[0];
1340 else if (spec->multiout.extra_out_nid[0])
1341 dac = spec->multiout.extra_out_nid[0];
1342 if (dac)
1343 path = snd_hda_add_new_path(codec, dac, pin,
1344 spec->mixer_nid);
1345 }
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001346 if (!path)
1347 return 0;
Takashi Iwaia7694092013-01-21 10:43:18 +01001348 /* print_nid_path("output-aamix", path); */
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001349 path->active = false; /* unused as default */
1350 return snd_hda_get_path_idx(codec, path);
1351}
1352
Takashi Iwaia07a9492013-01-07 16:44:06 +01001353/* fill the empty entries in the dac array for speaker/hp with the
1354 * shared dac pointed by the paths
1355 */
1356static void refill_shared_dacs(struct hda_codec *codec, int num_outs,
1357 hda_nid_t *dacs, int *path_idx)
1358{
1359 struct nid_path *path;
1360 int i;
1361
1362 for (i = 0; i < num_outs; i++) {
1363 if (dacs[i])
1364 continue;
1365 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
1366 if (!path)
1367 continue;
1368 dacs[i] = path->path[0];
1369 }
1370}
1371
Takashi Iwai352f7f92012-12-19 12:52:06 +01001372/* fill in the dac_nids table from the parsed pin configuration */
1373static int fill_and_eval_dacs(struct hda_codec *codec,
1374 bool fill_hardwired,
1375 bool fill_mio_first)
1376{
1377 struct hda_gen_spec *spec = codec->spec;
1378 struct auto_pin_cfg *cfg = &spec->autocfg;
1379 int i, err, badness;
1380
1381 /* set num_dacs once to full for look_for_dac() */
1382 spec->multiout.num_dacs = cfg->line_outs;
1383 spec->multiout.dac_nids = spec->private_dac_nids;
1384 memset(spec->private_dac_nids, 0, sizeof(spec->private_dac_nids));
1385 memset(spec->multiout.hp_out_nid, 0, sizeof(spec->multiout.hp_out_nid));
1386 memset(spec->multiout.extra_out_nid, 0, sizeof(spec->multiout.extra_out_nid));
1387 spec->multi_ios = 0;
1388 snd_array_free(&spec->paths);
Takashi Iwaicd5be3f2013-01-07 15:07:00 +01001389
1390 /* clear path indices */
1391 memset(spec->out_paths, 0, sizeof(spec->out_paths));
1392 memset(spec->hp_paths, 0, sizeof(spec->hp_paths));
1393 memset(spec->speaker_paths, 0, sizeof(spec->speaker_paths));
1394 memset(spec->aamix_out_paths, 0, sizeof(spec->aamix_out_paths));
1395 memset(spec->digout_paths, 0, sizeof(spec->digout_paths));
Takashi Iwaic697b712013-01-07 17:09:26 +01001396 memset(spec->input_paths, 0, sizeof(spec->input_paths));
Takashi Iwaicd5be3f2013-01-07 15:07:00 +01001397 memset(spec->loopback_paths, 0, sizeof(spec->loopback_paths));
1398 memset(&spec->digin_path, 0, sizeof(spec->digin_path));
1399
Takashi Iwai352f7f92012-12-19 12:52:06 +01001400 badness = 0;
1401
1402 /* fill hard-wired DACs first */
1403 if (fill_hardwired) {
1404 bool mapped;
1405 do {
1406 mapped = map_singles(codec, cfg->line_outs,
1407 cfg->line_out_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001408 spec->private_dac_nids,
1409 spec->out_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001410 mapped |= map_singles(codec, cfg->hp_outs,
1411 cfg->hp_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001412 spec->multiout.hp_out_nid,
1413 spec->hp_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001414 mapped |= map_singles(codec, cfg->speaker_outs,
1415 cfg->speaker_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001416 spec->multiout.extra_out_nid,
1417 spec->speaker_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001418 if (fill_mio_first && cfg->line_outs == 1 &&
1419 cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
Takashi Iwaie22aab72013-01-04 14:50:04 +01001420 err = fill_multi_ios(codec, cfg->line_out_pins[0], true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001421 if (!err)
1422 mapped = true;
1423 }
1424 } while (mapped);
1425 }
1426
1427 badness += try_assign_dacs(codec, cfg->line_outs, cfg->line_out_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001428 spec->private_dac_nids, spec->out_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001429 &main_out_badness);
1430
Takashi Iwai352f7f92012-12-19 12:52:06 +01001431 if (fill_mio_first &&
1432 cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1433 /* try to fill multi-io first */
Takashi Iwaie22aab72013-01-04 14:50:04 +01001434 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001435 if (err < 0)
1436 return err;
1437 /* we don't count badness at this stage yet */
1438 }
1439
1440 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
1441 err = try_assign_dacs(codec, cfg->hp_outs, cfg->hp_pins,
1442 spec->multiout.hp_out_nid,
Takashi Iwai196c17662013-01-04 15:01:40 +01001443 spec->hp_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001444 &extra_out_badness);
1445 if (err < 0)
1446 return err;
1447 badness += err;
1448 }
1449 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1450 err = try_assign_dacs(codec, cfg->speaker_outs,
1451 cfg->speaker_pins,
1452 spec->multiout.extra_out_nid,
Takashi Iwai196c17662013-01-04 15:01:40 +01001453 spec->speaker_paths,
1454 &extra_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001455 if (err < 0)
1456 return err;
1457 badness += err;
1458 }
1459 if (cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
Takashi Iwaie22aab72013-01-04 14:50:04 +01001460 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001461 if (err < 0)
1462 return err;
1463 badness += err;
1464 }
Takashi Iwaie22aab72013-01-04 14:50:04 +01001465
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001466 if (spec->mixer_nid) {
1467 spec->aamix_out_paths[0] =
1468 check_aamix_out_path(codec, spec->out_paths[0]);
1469 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1470 spec->aamix_out_paths[1] =
1471 check_aamix_out_path(codec, spec->hp_paths[0]);
1472 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1473 spec->aamix_out_paths[2] =
1474 check_aamix_out_path(codec, spec->speaker_paths[0]);
1475 }
1476
Takashi Iwaie22aab72013-01-04 14:50:04 +01001477 if (cfg->hp_outs && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
1478 if (count_multiio_pins(codec, cfg->hp_pins[0]) >= 2)
1479 spec->multi_ios = 1; /* give badness */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001480
Takashi Iwaia07a9492013-01-07 16:44:06 +01001481 /* re-count num_dacs and squash invalid entries */
1482 spec->multiout.num_dacs = 0;
1483 for (i = 0; i < cfg->line_outs; i++) {
1484 if (spec->private_dac_nids[i])
1485 spec->multiout.num_dacs++;
1486 else {
1487 memmove(spec->private_dac_nids + i,
1488 spec->private_dac_nids + i + 1,
1489 sizeof(hda_nid_t) * (cfg->line_outs - i - 1));
1490 spec->private_dac_nids[cfg->line_outs - 1] = 0;
1491 }
1492 }
1493
1494 spec->ext_channel_count = spec->min_channel_count =
David Henningssonc0f3b212013-01-16 11:45:37 +01001495 spec->multiout.num_dacs * 2;
Takashi Iwaia07a9492013-01-07 16:44:06 +01001496
Takashi Iwai352f7f92012-12-19 12:52:06 +01001497 if (spec->multi_ios == 2) {
1498 for (i = 0; i < 2; i++)
1499 spec->private_dac_nids[spec->multiout.num_dacs++] =
1500 spec->multi_io[i].dac;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001501 } else if (spec->multi_ios) {
1502 spec->multi_ios = 0;
1503 badness += BAD_MULTI_IO;
1504 }
1505
Takashi Iwaia07a9492013-01-07 16:44:06 +01001506 /* re-fill the shared DAC for speaker / headphone */
1507 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1508 refill_shared_dacs(codec, cfg->hp_outs,
1509 spec->multiout.hp_out_nid,
1510 spec->hp_paths);
1511 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1512 refill_shared_dacs(codec, cfg->speaker_outs,
1513 spec->multiout.extra_out_nid,
1514 spec->speaker_paths);
1515
Takashi Iwai352f7f92012-12-19 12:52:06 +01001516 return badness;
1517}
1518
1519#define DEBUG_BADNESS
1520
1521#ifdef DEBUG_BADNESS
1522#define debug_badness snd_printdd
1523#else
1524#define debug_badness(...)
1525#endif
1526
Takashi Iwaia7694092013-01-21 10:43:18 +01001527#ifdef DEBUG_BADNESS
1528static inline void print_nid_path_idx(struct hda_codec *codec,
1529 const char *pfx, int idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001530{
Takashi Iwaia7694092013-01-21 10:43:18 +01001531 struct nid_path *path;
1532
1533 path = snd_hda_get_path_from_idx(codec, idx);
1534 if (path)
1535 print_nid_path(pfx, path);
1536}
1537
1538static void debug_show_configs(struct hda_codec *codec,
1539 struct auto_pin_cfg *cfg)
1540{
1541 struct hda_gen_spec *spec = codec->spec;
1542#ifdef CONFIG_SND_DEBUG_VERBOSE
1543 static const char * const lo_type[3] = { "LO", "SP", "HP" };
1544#endif
1545 int i;
1546
1547 debug_badness("multi_outs = %x/%x/%x/%x : %x/%x/%x/%x (type %s)\n",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001548 cfg->line_out_pins[0], cfg->line_out_pins[1],
Takashi Iwai708122e2012-12-20 17:56:57 +01001549 cfg->line_out_pins[2], cfg->line_out_pins[3],
Takashi Iwai352f7f92012-12-19 12:52:06 +01001550 spec->multiout.dac_nids[0],
1551 spec->multiout.dac_nids[1],
1552 spec->multiout.dac_nids[2],
Takashi Iwaia7694092013-01-21 10:43:18 +01001553 spec->multiout.dac_nids[3],
1554 lo_type[cfg->line_out_type]);
1555 for (i = 0; i < cfg->line_outs; i++)
1556 print_nid_path_idx(codec, " out", spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001557 if (spec->multi_ios > 0)
1558 debug_badness("multi_ios(%d) = %x/%x : %x/%x\n",
1559 spec->multi_ios,
1560 spec->multi_io[0].pin, spec->multi_io[1].pin,
1561 spec->multi_io[0].dac, spec->multi_io[1].dac);
Takashi Iwaia7694092013-01-21 10:43:18 +01001562 for (i = 0; i < spec->multi_ios; i++)
1563 print_nid_path_idx(codec, " mio",
1564 spec->out_paths[cfg->line_outs + i]);
1565 if (cfg->hp_outs)
1566 debug_badness("hp_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001567 cfg->hp_pins[0], cfg->hp_pins[1],
Takashi Iwai708122e2012-12-20 17:56:57 +01001568 cfg->hp_pins[2], cfg->hp_pins[3],
Takashi Iwai352f7f92012-12-19 12:52:06 +01001569 spec->multiout.hp_out_nid[0],
1570 spec->multiout.hp_out_nid[1],
1571 spec->multiout.hp_out_nid[2],
1572 spec->multiout.hp_out_nid[3]);
Takashi Iwaia7694092013-01-21 10:43:18 +01001573 for (i = 0; i < cfg->hp_outs; i++)
1574 print_nid_path_idx(codec, " hp ", spec->hp_paths[i]);
1575 if (cfg->speaker_outs)
1576 debug_badness("spk_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001577 cfg->speaker_pins[0], cfg->speaker_pins[1],
1578 cfg->speaker_pins[2], cfg->speaker_pins[3],
1579 spec->multiout.extra_out_nid[0],
1580 spec->multiout.extra_out_nid[1],
1581 spec->multiout.extra_out_nid[2],
1582 spec->multiout.extra_out_nid[3]);
Takashi Iwaia7694092013-01-21 10:43:18 +01001583 for (i = 0; i < cfg->speaker_outs; i++)
1584 print_nid_path_idx(codec, " spk", spec->speaker_paths[i]);
1585 for (i = 0; i < 3; i++)
1586 print_nid_path_idx(codec, " mix", spec->aamix_out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001587}
Takashi Iwaia7694092013-01-21 10:43:18 +01001588#else
1589#define debug_show_configs(codec, cfg) /* NOP */
1590#endif
Takashi Iwai352f7f92012-12-19 12:52:06 +01001591
1592/* find all available DACs of the codec */
1593static void fill_all_dac_nids(struct hda_codec *codec)
1594{
1595 struct hda_gen_spec *spec = codec->spec;
1596 int i;
1597 hda_nid_t nid = codec->start_nid;
1598
1599 spec->num_all_dacs = 0;
1600 memset(spec->all_dacs, 0, sizeof(spec->all_dacs));
1601 for (i = 0; i < codec->num_nodes; i++, nid++) {
1602 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_AUD_OUT)
1603 continue;
1604 if (spec->num_all_dacs >= ARRAY_SIZE(spec->all_dacs)) {
1605 snd_printk(KERN_ERR "hda: Too many DACs!\n");
1606 break;
1607 }
1608 spec->all_dacs[spec->num_all_dacs++] = nid;
1609 }
1610}
1611
1612static int parse_output_paths(struct hda_codec *codec)
1613{
1614 struct hda_gen_spec *spec = codec->spec;
1615 struct auto_pin_cfg *cfg = &spec->autocfg;
1616 struct auto_pin_cfg *best_cfg;
Takashi Iwai9314a582013-01-21 10:49:05 +01001617 unsigned int val;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001618 int best_badness = INT_MAX;
1619 int badness;
1620 bool fill_hardwired = true, fill_mio_first = true;
1621 bool best_wired = true, best_mio = true;
1622 bool hp_spk_swapped = false;
1623
Takashi Iwai352f7f92012-12-19 12:52:06 +01001624 best_cfg = kmalloc(sizeof(*best_cfg), GFP_KERNEL);
1625 if (!best_cfg)
1626 return -ENOMEM;
1627 *best_cfg = *cfg;
1628
1629 for (;;) {
1630 badness = fill_and_eval_dacs(codec, fill_hardwired,
1631 fill_mio_first);
1632 if (badness < 0) {
1633 kfree(best_cfg);
1634 return badness;
1635 }
1636 debug_badness("==> lo_type=%d, wired=%d, mio=%d, badness=0x%x\n",
1637 cfg->line_out_type, fill_hardwired, fill_mio_first,
1638 badness);
Takashi Iwaia7694092013-01-21 10:43:18 +01001639 debug_show_configs(codec, cfg);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001640 if (badness < best_badness) {
1641 best_badness = badness;
1642 *best_cfg = *cfg;
1643 best_wired = fill_hardwired;
1644 best_mio = fill_mio_first;
1645 }
1646 if (!badness)
1647 break;
1648 fill_mio_first = !fill_mio_first;
1649 if (!fill_mio_first)
1650 continue;
1651 fill_hardwired = !fill_hardwired;
1652 if (!fill_hardwired)
1653 continue;
1654 if (hp_spk_swapped)
1655 break;
1656 hp_spk_swapped = true;
1657 if (cfg->speaker_outs > 0 &&
1658 cfg->line_out_type == AUTO_PIN_HP_OUT) {
1659 cfg->hp_outs = cfg->line_outs;
1660 memcpy(cfg->hp_pins, cfg->line_out_pins,
1661 sizeof(cfg->hp_pins));
1662 cfg->line_outs = cfg->speaker_outs;
1663 memcpy(cfg->line_out_pins, cfg->speaker_pins,
1664 sizeof(cfg->speaker_pins));
1665 cfg->speaker_outs = 0;
1666 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
1667 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
1668 fill_hardwired = true;
1669 continue;
1670 }
1671 if (cfg->hp_outs > 0 &&
1672 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
1673 cfg->speaker_outs = cfg->line_outs;
1674 memcpy(cfg->speaker_pins, cfg->line_out_pins,
1675 sizeof(cfg->speaker_pins));
1676 cfg->line_outs = cfg->hp_outs;
1677 memcpy(cfg->line_out_pins, cfg->hp_pins,
1678 sizeof(cfg->hp_pins));
1679 cfg->hp_outs = 0;
1680 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
1681 cfg->line_out_type = AUTO_PIN_HP_OUT;
1682 fill_hardwired = true;
1683 continue;
1684 }
1685 break;
1686 }
1687
1688 if (badness) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001689 debug_badness("==> restoring best_cfg\n");
Takashi Iwai352f7f92012-12-19 12:52:06 +01001690 *cfg = *best_cfg;
1691 fill_and_eval_dacs(codec, best_wired, best_mio);
1692 }
1693 debug_badness("==> Best config: lo_type=%d, wired=%d, mio=%d\n",
1694 cfg->line_out_type, best_wired, best_mio);
Takashi Iwaia7694092013-01-21 10:43:18 +01001695 debug_show_configs(codec, cfg);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001696
1697 if (cfg->line_out_pins[0]) {
1698 struct nid_path *path;
Takashi Iwai196c17662013-01-04 15:01:40 +01001699 path = snd_hda_get_path_from_idx(codec, spec->out_paths[0]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001700 if (path)
1701 spec->vmaster_nid = look_for_out_vol_nid(codec, path);
Takashi Iwai7a71bbf2013-01-17 10:25:15 +01001702 if (spec->vmaster_nid)
1703 snd_hda_set_vmaster_tlv(codec, spec->vmaster_nid,
1704 HDA_OUTPUT, spec->vmaster_tlv);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001705 }
1706
Takashi Iwai9314a582013-01-21 10:49:05 +01001707 /* set initial pinctl targets */
1708 if (spec->prefer_hp_amp || cfg->line_out_type == AUTO_PIN_HP_OUT)
1709 val = PIN_HP;
1710 else
1711 val = PIN_OUT;
1712 set_pin_targets(codec, cfg->line_outs, cfg->line_out_pins, val);
1713 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1714 set_pin_targets(codec, cfg->hp_outs, cfg->hp_pins, PIN_HP);
1715 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1716 val = spec->prefer_hp_amp ? PIN_HP : PIN_OUT;
1717 set_pin_targets(codec, cfg->speaker_outs,
1718 cfg->speaker_pins, val);
1719 }
1720
Takashi Iwai352f7f92012-12-19 12:52:06 +01001721 kfree(best_cfg);
1722 return 0;
1723}
1724
1725/* add playback controls from the parsed DAC table */
1726static int create_multi_out_ctls(struct hda_codec *codec,
1727 const struct auto_pin_cfg *cfg)
1728{
1729 struct hda_gen_spec *spec = codec->spec;
1730 int i, err, noutputs;
1731
1732 noutputs = cfg->line_outs;
1733 if (spec->multi_ios > 0 && cfg->line_outs < 3)
1734 noutputs += spec->multi_ios;
1735
1736 for (i = 0; i < noutputs; i++) {
1737 const char *name;
1738 int index;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001739 struct nid_path *path;
1740
Takashi Iwai196c17662013-01-04 15:01:40 +01001741 path = snd_hda_get_path_from_idx(codec, spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001742 if (!path)
1743 continue;
Takashi Iwai247d85e2013-01-17 16:18:11 +01001744
1745 name = get_line_out_pfx(codec, i, &index, NID_PATH_VOL_CTL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001746 if (!name || !strcmp(name, "CLFE")) {
1747 /* Center/LFE */
1748 err = add_vol_ctl(codec, "Center", 0, 1, path);
1749 if (err < 0)
1750 return err;
1751 err = add_vol_ctl(codec, "LFE", 0, 2, path);
1752 if (err < 0)
1753 return err;
Takashi Iwai247d85e2013-01-17 16:18:11 +01001754 } else {
1755 err = add_stereo_vol(codec, name, index, path);
1756 if (err < 0)
1757 return err;
1758 }
1759
1760 name = get_line_out_pfx(codec, i, &index, NID_PATH_MUTE_CTL);
1761 if (!name || !strcmp(name, "CLFE")) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001762 err = add_sw_ctl(codec, "Center", 0, 1, path);
1763 if (err < 0)
1764 return err;
1765 err = add_sw_ctl(codec, "LFE", 0, 2, path);
1766 if (err < 0)
1767 return err;
1768 } else {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001769 err = add_stereo_sw(codec, name, index, path);
1770 if (err < 0)
1771 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 }
1773 }
1774 return 0;
1775}
1776
Takashi Iwaic2c80382013-01-07 10:33:57 +01001777static int create_extra_out(struct hda_codec *codec, int path_idx,
Takashi Iwai196c17662013-01-04 15:01:40 +01001778 const char *pfx, int cidx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779{
Takashi Iwai352f7f92012-12-19 12:52:06 +01001780 struct nid_path *path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 int err;
1782
Takashi Iwai196c17662013-01-04 15:01:40 +01001783 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001784 if (!path)
1785 return 0;
Takashi Iwaic2c80382013-01-07 10:33:57 +01001786 err = add_stereo_vol(codec, pfx, cidx, path);
1787 if (err < 0)
1788 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001789 err = add_stereo_sw(codec, pfx, cidx, path);
1790 if (err < 0)
1791 return err;
1792 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793}
1794
Takashi Iwai352f7f92012-12-19 12:52:06 +01001795/* add playback controls for speaker and HP outputs */
1796static int create_extra_outs(struct hda_codec *codec, int num_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001797 const int *paths, const char *pfx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798{
Takashi Iwaic2c80382013-01-07 10:33:57 +01001799 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001800
1801 for (i = 0; i < num_pins; i++) {
Takashi Iwaic2c80382013-01-07 10:33:57 +01001802 const char *name;
1803 char tmp[44];
1804 int err, idx = 0;
1805
1806 if (num_pins == 2 && i == 1 && !strcmp(pfx, "Speaker"))
1807 name = "Bass Speaker";
1808 else if (num_pins >= 3) {
1809 snprintf(tmp, sizeof(tmp), "%s %s",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001810 pfx, channel_name[i]);
Takashi Iwaic2c80382013-01-07 10:33:57 +01001811 name = tmp;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001812 } else {
Takashi Iwaic2c80382013-01-07 10:33:57 +01001813 name = pfx;
1814 idx = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 }
Takashi Iwaic2c80382013-01-07 10:33:57 +01001816 err = create_extra_out(codec, paths[i], name, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001817 if (err < 0)
1818 return err;
1819 }
1820 return 0;
1821}
Takashi Iwai97ec5582006-03-21 11:29:07 +01001822
Takashi Iwai352f7f92012-12-19 12:52:06 +01001823static int create_hp_out_ctls(struct hda_codec *codec)
1824{
1825 struct hda_gen_spec *spec = codec->spec;
1826 return create_extra_outs(codec, spec->autocfg.hp_outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001827 spec->hp_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001828 "Headphone");
1829}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830
Takashi Iwai352f7f92012-12-19 12:52:06 +01001831static int create_speaker_out_ctls(struct hda_codec *codec)
1832{
1833 struct hda_gen_spec *spec = codec->spec;
1834 return create_extra_outs(codec, spec->autocfg.speaker_outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001835 spec->speaker_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001836 "Speaker");
1837}
1838
1839/*
Takashi Iwai38cf6f12012-12-21 14:09:42 +01001840 * independent HP controls
1841 */
1842
1843static int indep_hp_info(struct snd_kcontrol *kcontrol,
1844 struct snd_ctl_elem_info *uinfo)
1845{
1846 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
1847}
1848
1849static int indep_hp_get(struct snd_kcontrol *kcontrol,
1850 struct snd_ctl_elem_value *ucontrol)
1851{
1852 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1853 struct hda_gen_spec *spec = codec->spec;
1854 ucontrol->value.enumerated.item[0] = spec->indep_hp_enabled;
1855 return 0;
1856}
1857
Takashi Iwaia1e908e2013-01-21 15:11:25 +01001858static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
1859 int nomix_path_idx, int mix_path_idx,
1860 int out_type);
1861
Takashi Iwai38cf6f12012-12-21 14:09:42 +01001862static int indep_hp_put(struct snd_kcontrol *kcontrol,
1863 struct snd_ctl_elem_value *ucontrol)
1864{
1865 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1866 struct hda_gen_spec *spec = codec->spec;
1867 unsigned int select = ucontrol->value.enumerated.item[0];
1868 int ret = 0;
1869
1870 mutex_lock(&spec->pcm_mutex);
1871 if (spec->active_streams) {
1872 ret = -EBUSY;
1873 goto unlock;
1874 }
1875
1876 if (spec->indep_hp_enabled != select) {
Takashi Iwaia1e908e2013-01-21 15:11:25 +01001877 hda_nid_t *dacp;
1878 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
1879 dacp = &spec->private_dac_nids[0];
1880 else
1881 dacp = &spec->multiout.hp_out_nid[0];
1882
1883 /* update HP aamix paths in case it conflicts with indep HP */
1884 if (spec->have_aamix_ctl) {
1885 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
1886 update_aamix_paths(codec, spec->aamix_mode,
1887 spec->out_paths[0],
1888 spec->aamix_out_paths[0],
1889 spec->autocfg.line_out_type);
1890 else
1891 update_aamix_paths(codec, spec->aamix_mode,
1892 spec->hp_paths[0],
1893 spec->aamix_out_paths[1],
1894 AUTO_PIN_HP_OUT);
1895 }
1896
Takashi Iwai38cf6f12012-12-21 14:09:42 +01001897 spec->indep_hp_enabled = select;
1898 if (spec->indep_hp_enabled)
Takashi Iwaia1e908e2013-01-21 15:11:25 +01001899 *dacp = 0;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01001900 else
Takashi Iwaia1e908e2013-01-21 15:11:25 +01001901 *dacp = spec->alt_dac_nid;
Takashi Iwai92603c52013-01-22 07:46:31 +01001902
1903 /* update HP auto-mute state too */
1904 if (spec->hp_automute_hook)
1905 spec->hp_automute_hook(codec, NULL);
1906 else
1907 snd_hda_gen_hp_automute(codec, NULL);
1908
Takashi Iwai38cf6f12012-12-21 14:09:42 +01001909 ret = 1;
1910 }
1911 unlock:
1912 mutex_unlock(&spec->pcm_mutex);
1913 return ret;
1914}
1915
1916static const struct snd_kcontrol_new indep_hp_ctl = {
1917 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1918 .name = "Independent HP",
1919 .info = indep_hp_info,
1920 .get = indep_hp_get,
1921 .put = indep_hp_put,
1922};
1923
1924
1925static int create_indep_hp_ctls(struct hda_codec *codec)
1926{
1927 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01001928 hda_nid_t dac;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01001929
1930 if (!spec->indep_hp)
1931 return 0;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01001932 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
1933 dac = spec->multiout.dac_nids[0];
1934 else
1935 dac = spec->multiout.hp_out_nid[0];
1936 if (!dac) {
Takashi Iwai38cf6f12012-12-21 14:09:42 +01001937 spec->indep_hp = 0;
1938 return 0;
1939 }
1940
1941 spec->indep_hp_enabled = false;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01001942 spec->alt_dac_nid = dac;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01001943 if (!snd_hda_gen_add_kctl(spec, NULL, &indep_hp_ctl))
1944 return -ENOMEM;
1945 return 0;
1946}
1947
1948/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01001949 * channel mode enum control
1950 */
1951
1952static int ch_mode_info(struct snd_kcontrol *kcontrol,
1953 struct snd_ctl_elem_info *uinfo)
1954{
1955 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1956 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia07a9492013-01-07 16:44:06 +01001957 int chs;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001958
1959 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1960 uinfo->count = 1;
1961 uinfo->value.enumerated.items = spec->multi_ios + 1;
1962 if (uinfo->value.enumerated.item > spec->multi_ios)
1963 uinfo->value.enumerated.item = spec->multi_ios;
Takashi Iwaia07a9492013-01-07 16:44:06 +01001964 chs = uinfo->value.enumerated.item * 2 + spec->min_channel_count;
1965 sprintf(uinfo->value.enumerated.name, "%dch", chs);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001966 return 0;
1967}
1968
1969static int ch_mode_get(struct snd_kcontrol *kcontrol,
1970 struct snd_ctl_elem_value *ucontrol)
1971{
1972 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1973 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia07a9492013-01-07 16:44:06 +01001974 ucontrol->value.enumerated.item[0] =
1975 (spec->ext_channel_count - spec->min_channel_count) / 2;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001976 return 0;
1977}
1978
Takashi Iwai196c17662013-01-04 15:01:40 +01001979static inline struct nid_path *
1980get_multiio_path(struct hda_codec *codec, int idx)
1981{
1982 struct hda_gen_spec *spec = codec->spec;
1983 return snd_hda_get_path_from_idx(codec,
1984 spec->out_paths[spec->autocfg.line_outs + idx]);
1985}
1986
Takashi Iwaia5cc2502013-01-16 18:08:55 +01001987static void update_automute_all(struct hda_codec *codec);
1988
Takashi Iwai352f7f92012-12-19 12:52:06 +01001989static int set_multi_io(struct hda_codec *codec, int idx, bool output)
1990{
1991 struct hda_gen_spec *spec = codec->spec;
1992 hda_nid_t nid = spec->multi_io[idx].pin;
1993 struct nid_path *path;
1994
Takashi Iwai196c17662013-01-04 15:01:40 +01001995 path = get_multiio_path(codec, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001996 if (!path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 return -EINVAL;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001998
1999 if (path->active == output)
2000 return 0;
2001
2002 if (output) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01002003 set_pin_target(codec, nid, PIN_OUT, true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002004 snd_hda_activate_path(codec, path, true, true);
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01002005 set_pin_eapd(codec, nid, true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002006 } else {
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01002007 set_pin_eapd(codec, nid, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002008 snd_hda_activate_path(codec, path, false, true);
Takashi Iwai2c12c302013-01-10 09:33:29 +01002009 set_pin_target(codec, nid, spec->multi_io[idx].ctl_in, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 }
Takashi Iwaia365fed2013-01-10 16:10:06 +01002011
2012 /* update jack retasking in case it modifies any of them */
Takashi Iwaia5cc2502013-01-16 18:08:55 +01002013 update_automute_all(codec);
Takashi Iwaia365fed2013-01-10 16:10:06 +01002014
Takashi Iwai352f7f92012-12-19 12:52:06 +01002015 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016}
2017
Takashi Iwai352f7f92012-12-19 12:52:06 +01002018static int ch_mode_put(struct snd_kcontrol *kcontrol,
2019 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002021 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2022 struct hda_gen_spec *spec = codec->spec;
2023 int i, ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024
Takashi Iwai352f7f92012-12-19 12:52:06 +01002025 ch = ucontrol->value.enumerated.item[0];
2026 if (ch < 0 || ch > spec->multi_ios)
2027 return -EINVAL;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002028 if (ch == (spec->ext_channel_count - spec->min_channel_count) / 2)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002029 return 0;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002030 spec->ext_channel_count = ch * 2 + spec->min_channel_count;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002031 for (i = 0; i < spec->multi_ios; i++)
2032 set_multi_io(codec, i, i < ch);
2033 spec->multiout.max_channels = max(spec->ext_channel_count,
2034 spec->const_channel_count);
2035 if (spec->need_dac_fix)
2036 spec->multiout.num_dacs = spec->multiout.max_channels / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 return 1;
2038}
2039
Takashi Iwai352f7f92012-12-19 12:52:06 +01002040static const struct snd_kcontrol_new channel_mode_enum = {
2041 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2042 .name = "Channel Mode",
2043 .info = ch_mode_info,
2044 .get = ch_mode_get,
2045 .put = ch_mode_put,
2046};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047
Takashi Iwai352f7f92012-12-19 12:52:06 +01002048static int create_multi_channel_mode(struct hda_codec *codec)
2049{
2050 struct hda_gen_spec *spec = codec->spec;
2051
2052 if (spec->multi_ios > 0) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01002053 if (!snd_hda_gen_add_kctl(spec, NULL, &channel_mode_enum))
Takashi Iwai352f7f92012-12-19 12:52:06 +01002054 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 return 0;
2057}
2058
Takashi Iwai352f7f92012-12-19 12:52:06 +01002059/*
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002060 * aamix loopback enable/disable switch
2061 */
2062
2063#define loopback_mixing_info indep_hp_info
2064
2065static int loopback_mixing_get(struct snd_kcontrol *kcontrol,
2066 struct snd_ctl_elem_value *ucontrol)
2067{
2068 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2069 struct hda_gen_spec *spec = codec->spec;
2070 ucontrol->value.enumerated.item[0] = spec->aamix_mode;
2071 return 0;
2072}
2073
2074static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002075 int nomix_path_idx, int mix_path_idx,
2076 int out_type)
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002077{
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002078 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002079 struct nid_path *nomix_path, *mix_path;
2080
2081 nomix_path = snd_hda_get_path_from_idx(codec, nomix_path_idx);
2082 mix_path = snd_hda_get_path_from_idx(codec, mix_path_idx);
2083 if (!nomix_path || !mix_path)
2084 return;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002085
2086 /* if HP aamix path is driven from a different DAC and the
2087 * independent HP mode is ON, can't turn on aamix path
2088 */
2089 if (out_type == AUTO_PIN_HP_OUT && spec->indep_hp_enabled &&
2090 mix_path->path[0] != spec->alt_dac_nid)
2091 do_mix = false;
2092
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002093 if (do_mix) {
2094 snd_hda_activate_path(codec, nomix_path, false, true);
2095 snd_hda_activate_path(codec, mix_path, true, true);
2096 } else {
2097 snd_hda_activate_path(codec, mix_path, false, true);
2098 snd_hda_activate_path(codec, nomix_path, true, true);
2099 }
2100}
2101
2102static int loopback_mixing_put(struct snd_kcontrol *kcontrol,
2103 struct snd_ctl_elem_value *ucontrol)
2104{
2105 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2106 struct hda_gen_spec *spec = codec->spec;
2107 unsigned int val = ucontrol->value.enumerated.item[0];
2108
2109 if (val == spec->aamix_mode)
2110 return 0;
2111 spec->aamix_mode = val;
2112 update_aamix_paths(codec, val, spec->out_paths[0],
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002113 spec->aamix_out_paths[0],
2114 spec->autocfg.line_out_type);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002115 update_aamix_paths(codec, val, spec->hp_paths[0],
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002116 spec->aamix_out_paths[1],
2117 AUTO_PIN_HP_OUT);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002118 update_aamix_paths(codec, val, spec->speaker_paths[0],
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002119 spec->aamix_out_paths[2],
2120 AUTO_PIN_SPEAKER_OUT);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002121 return 1;
2122}
2123
2124static const struct snd_kcontrol_new loopback_mixing_enum = {
2125 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2126 .name = "Loopback Mixing",
2127 .info = loopback_mixing_info,
2128 .get = loopback_mixing_get,
2129 .put = loopback_mixing_put,
2130};
2131
2132static int create_loopback_mixing_ctl(struct hda_codec *codec)
2133{
2134 struct hda_gen_spec *spec = codec->spec;
2135
2136 if (!spec->mixer_nid)
2137 return 0;
2138 if (!(spec->aamix_out_paths[0] || spec->aamix_out_paths[1] ||
2139 spec->aamix_out_paths[2]))
2140 return 0;
2141 if (!snd_hda_gen_add_kctl(spec, NULL, &loopback_mixing_enum))
2142 return -ENOMEM;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002143 spec->have_aamix_ctl = 1;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002144 return 0;
2145}
2146
2147/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01002148 * shared headphone/mic handling
2149 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02002150
Takashi Iwai352f7f92012-12-19 12:52:06 +01002151static void call_update_outputs(struct hda_codec *codec);
2152
2153/* for shared I/O, change the pin-control accordingly */
2154static void update_shared_mic_hp(struct hda_codec *codec, bool set_as_mic)
2155{
2156 struct hda_gen_spec *spec = codec->spec;
2157 unsigned int val;
2158 hda_nid_t pin = spec->autocfg.inputs[1].pin;
2159 /* NOTE: this assumes that there are only two inputs, the
2160 * first is the real internal mic and the second is HP/mic jack.
2161 */
2162
2163 val = snd_hda_get_default_vref(codec, pin);
2164
2165 /* This pin does not have vref caps - let's enable vref on pin 0x18
2166 instead, as suggested by Realtek */
2167 if (val == AC_PINCTL_VREF_HIZ && spec->shared_mic_vref_pin) {
2168 const hda_nid_t vref_pin = spec->shared_mic_vref_pin;
2169 unsigned int vref_val = snd_hda_get_default_vref(codec, vref_pin);
2170 if (vref_val != AC_PINCTL_VREF_HIZ)
Takashi Iwai7594aa32012-12-20 15:38:40 +01002171 snd_hda_set_pin_ctl_cache(codec, vref_pin,
2172 PIN_IN | (set_as_mic ? vref_val : 0));
Takashi Iwaicb53c622007-08-10 17:21:45 +02002173 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002174
2175 val = set_as_mic ? val | PIN_IN : PIN_HP;
Takashi Iwai2c12c302013-01-10 09:33:29 +01002176 set_pin_target(codec, pin, val, true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002177
2178 spec->automute_speaker = !set_as_mic;
2179 call_update_outputs(codec);
2180}
2181
2182/* create a shared input with the headphone out */
2183static int create_shared_input(struct hda_codec *codec)
2184{
2185 struct hda_gen_spec *spec = codec->spec;
2186 struct auto_pin_cfg *cfg = &spec->autocfg;
2187 unsigned int defcfg;
2188 hda_nid_t nid;
2189
2190 /* only one internal input pin? */
2191 if (cfg->num_inputs != 1)
2192 return 0;
2193 defcfg = snd_hda_codec_get_pincfg(codec, cfg->inputs[0].pin);
2194 if (snd_hda_get_input_pin_attr(defcfg) != INPUT_PIN_ATTR_INT)
2195 return 0;
2196
2197 if (cfg->hp_outs == 1 && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
2198 nid = cfg->hp_pins[0]; /* OK, we have a single HP-out */
2199 else if (cfg->line_outs == 1 && cfg->line_out_type == AUTO_PIN_HP_OUT)
2200 nid = cfg->line_out_pins[0]; /* OK, we have a single line-out */
2201 else
2202 return 0; /* both not available */
2203
2204 if (!(snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_IN))
2205 return 0; /* no input */
2206
2207 cfg->inputs[1].pin = nid;
2208 cfg->inputs[1].type = AUTO_PIN_MIC;
2209 cfg->num_inputs = 2;
2210 spec->shared_mic_hp = 1;
2211 snd_printdd("hda-codec: Enable shared I/O jack on NID 0x%x\n", nid);
2212 return 0;
2213}
2214
Takashi Iwai978e77e2013-01-10 16:57:58 +01002215/*
2216 * output jack mode
2217 */
2218static int out_jack_mode_info(struct snd_kcontrol *kcontrol,
2219 struct snd_ctl_elem_info *uinfo)
2220{
2221 static const char * const texts[] = {
2222 "Line Out", "Headphone Out",
2223 };
2224 return snd_hda_enum_helper_info(kcontrol, uinfo, 2, texts);
2225}
2226
2227static int out_jack_mode_get(struct snd_kcontrol *kcontrol,
2228 struct snd_ctl_elem_value *ucontrol)
2229{
2230 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2231 hda_nid_t nid = kcontrol->private_value;
2232 if (snd_hda_codec_get_pin_target(codec, nid) == PIN_HP)
2233 ucontrol->value.enumerated.item[0] = 1;
2234 else
2235 ucontrol->value.enumerated.item[0] = 0;
2236 return 0;
2237}
2238
2239static int out_jack_mode_put(struct snd_kcontrol *kcontrol,
2240 struct snd_ctl_elem_value *ucontrol)
2241{
2242 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2243 hda_nid_t nid = kcontrol->private_value;
2244 unsigned int val;
2245
2246 val = ucontrol->value.enumerated.item[0] ? PIN_HP : PIN_OUT;
2247 if (snd_hda_codec_get_pin_target(codec, nid) == val)
2248 return 0;
2249 snd_hda_set_pin_ctl_cache(codec, nid, val);
2250 return 1;
2251}
2252
2253static const struct snd_kcontrol_new out_jack_mode_enum = {
2254 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2255 .info = out_jack_mode_info,
2256 .get = out_jack_mode_get,
2257 .put = out_jack_mode_put,
2258};
2259
2260static bool find_kctl_name(struct hda_codec *codec, const char *name, int idx)
2261{
2262 struct hda_gen_spec *spec = codec->spec;
2263 int i;
2264
2265 for (i = 0; i < spec->kctls.used; i++) {
2266 struct snd_kcontrol_new *kctl = snd_array_elem(&spec->kctls, i);
2267 if (!strcmp(kctl->name, name) && kctl->index == idx)
2268 return true;
2269 }
2270 return false;
2271}
2272
2273static void get_jack_mode_name(struct hda_codec *codec, hda_nid_t pin,
2274 char *name, size_t name_len)
2275{
2276 struct hda_gen_spec *spec = codec->spec;
2277 int idx = 0;
2278
2279 snd_hda_get_pin_label(codec, pin, &spec->autocfg, name, name_len, &idx);
2280 strlcat(name, " Jack Mode", name_len);
2281
2282 for (; find_kctl_name(codec, name, idx); idx++)
2283 ;
2284}
2285
2286static int create_out_jack_modes(struct hda_codec *codec, int num_pins,
2287 hda_nid_t *pins)
2288{
2289 struct hda_gen_spec *spec = codec->spec;
2290 int i;
2291
2292 for (i = 0; i < num_pins; i++) {
2293 hda_nid_t pin = pins[i];
2294 unsigned int pincap = snd_hda_query_pin_caps(codec, pin);
2295 if ((pincap & AC_PINCAP_OUT) && (pincap & AC_PINCAP_HP_DRV)) {
2296 struct snd_kcontrol_new *knew;
2297 char name[44];
2298 get_jack_mode_name(codec, pin, name, sizeof(name));
2299 knew = snd_hda_gen_add_kctl(spec, name,
2300 &out_jack_mode_enum);
2301 if (!knew)
2302 return -ENOMEM;
2303 knew->private_value = pin;
2304 }
2305 }
2306
2307 return 0;
2308}
2309
Takashi Iwai294765582013-01-17 09:52:11 +01002310/*
2311 * input jack mode
2312 */
2313
2314/* from AC_PINCTL_VREF_HIZ to AC_PINCTL_VREF_100 */
2315#define NUM_VREFS 6
2316
2317static const char * const vref_texts[NUM_VREFS] = {
2318 "Line In", "Mic 50pc Bias", "Mic 0V Bias",
2319 "", "Mic 80pc Bias", "Mic 100pc Bias"
2320};
2321
2322static unsigned int get_vref_caps(struct hda_codec *codec, hda_nid_t pin)
2323{
2324 unsigned int pincap;
2325
2326 pincap = snd_hda_query_pin_caps(codec, pin);
2327 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
2328 /* filter out unusual vrefs */
2329 pincap &= ~(AC_PINCAP_VREF_GRD | AC_PINCAP_VREF_100);
2330 return pincap;
2331}
2332
2333/* convert from the enum item index to the vref ctl index (0=HIZ, 1=50%...) */
2334static int get_vref_idx(unsigned int vref_caps, unsigned int item_idx)
2335{
2336 unsigned int i, n = 0;
2337
2338 for (i = 0; i < NUM_VREFS; i++) {
2339 if (vref_caps & (1 << i)) {
2340 if (n == item_idx)
2341 return i;
2342 n++;
2343 }
2344 }
2345 return 0;
2346}
2347
2348/* convert back from the vref ctl index to the enum item index */
2349static int cvt_from_vref_idx(unsigned int vref_caps, unsigned int idx)
2350{
2351 unsigned int i, n = 0;
2352
2353 for (i = 0; i < NUM_VREFS; i++) {
2354 if (i == idx)
2355 return n;
2356 if (vref_caps & (1 << i))
2357 n++;
2358 }
2359 return 0;
2360}
2361
2362static int in_jack_mode_info(struct snd_kcontrol *kcontrol,
2363 struct snd_ctl_elem_info *uinfo)
2364{
2365 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2366 hda_nid_t nid = kcontrol->private_value;
2367 unsigned int vref_caps = get_vref_caps(codec, nid);
2368
2369 snd_hda_enum_helper_info(kcontrol, uinfo, hweight32(vref_caps),
2370 vref_texts);
2371 /* set the right text */
2372 strcpy(uinfo->value.enumerated.name,
2373 vref_texts[get_vref_idx(vref_caps, uinfo->value.enumerated.item)]);
2374 return 0;
2375}
2376
2377static int in_jack_mode_get(struct snd_kcontrol *kcontrol,
2378 struct snd_ctl_elem_value *ucontrol)
2379{
2380 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2381 hda_nid_t nid = kcontrol->private_value;
2382 unsigned int vref_caps = get_vref_caps(codec, nid);
2383 unsigned int idx;
2384
2385 idx = snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_VREFEN;
2386 ucontrol->value.enumerated.item[0] = cvt_from_vref_idx(vref_caps, idx);
2387 return 0;
2388}
2389
2390static int in_jack_mode_put(struct snd_kcontrol *kcontrol,
2391 struct snd_ctl_elem_value *ucontrol)
2392{
2393 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2394 hda_nid_t nid = kcontrol->private_value;
2395 unsigned int vref_caps = get_vref_caps(codec, nid);
2396 unsigned int val, idx;
2397
2398 val = snd_hda_codec_get_pin_target(codec, nid);
2399 idx = cvt_from_vref_idx(vref_caps, val & AC_PINCTL_VREFEN);
2400 if (idx == ucontrol->value.enumerated.item[0])
2401 return 0;
2402
2403 val &= ~AC_PINCTL_VREFEN;
2404 val |= get_vref_idx(vref_caps, ucontrol->value.enumerated.item[0]);
2405 snd_hda_set_pin_ctl_cache(codec, nid, val);
2406 return 1;
2407}
2408
2409static const struct snd_kcontrol_new in_jack_mode_enum = {
2410 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2411 .info = in_jack_mode_info,
2412 .get = in_jack_mode_get,
2413 .put = in_jack_mode_put,
2414};
2415
2416static int create_in_jack_mode(struct hda_codec *codec, hda_nid_t pin)
2417{
2418 struct hda_gen_spec *spec = codec->spec;
2419 unsigned int defcfg;
2420 struct snd_kcontrol_new *knew;
2421 char name[44];
2422
2423 /* no jack mode for fixed pins */
2424 defcfg = snd_hda_codec_get_pincfg(codec, pin);
2425 if (snd_hda_get_input_pin_attr(defcfg) == INPUT_PIN_ATTR_INT)
2426 return 0;
2427
2428 /* no multiple vref caps? */
2429 if (hweight32(get_vref_caps(codec, pin)) <= 1)
2430 return 0;
2431
2432 get_jack_mode_name(codec, pin, name, sizeof(name));
2433 knew = snd_hda_gen_add_kctl(spec, name, &in_jack_mode_enum);
2434 if (!knew)
2435 return -ENOMEM;
2436 knew->private_value = pin;
2437 return 0;
2438}
2439
Takashi Iwai352f7f92012-12-19 12:52:06 +01002440
2441/*
2442 * Parse input paths
2443 */
2444
2445#ifdef CONFIG_PM
2446/* add the powersave loopback-list entry */
2447static void add_loopback_list(struct hda_gen_spec *spec, hda_nid_t mix, int idx)
2448{
2449 struct hda_amp_list *list;
2450
2451 if (spec->num_loopbacks >= ARRAY_SIZE(spec->loopback_list) - 1)
2452 return;
2453 list = spec->loopback_list + spec->num_loopbacks;
2454 list->nid = mix;
2455 list->dir = HDA_INPUT;
2456 list->idx = idx;
2457 spec->num_loopbacks++;
Takashi Iwaicb53c622007-08-10 17:21:45 +02002458 spec->loopback.amplist = spec->loopback_list;
2459}
2460#else
Takashi Iwai352f7f92012-12-19 12:52:06 +01002461#define add_loopback_list(spec, mix, idx) /* NOP */
Takashi Iwaicb53c622007-08-10 17:21:45 +02002462#endif
2463
Takashi Iwai352f7f92012-12-19 12:52:06 +01002464/* create input playback/capture controls for the given pin */
Takashi Iwai196c17662013-01-04 15:01:40 +01002465static int new_analog_input(struct hda_codec *codec, int input_idx,
2466 hda_nid_t pin, const char *ctlname, int ctlidx,
Takashi Iwai352f7f92012-12-19 12:52:06 +01002467 hda_nid_t mix_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002469 struct hda_gen_spec *spec = codec->spec;
2470 struct nid_path *path;
2471 unsigned int val;
2472 int err, idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473
Takashi Iwai352f7f92012-12-19 12:52:06 +01002474 if (!nid_has_volume(codec, mix_nid, HDA_INPUT) &&
2475 !nid_has_mute(codec, mix_nid, HDA_INPUT))
2476 return 0; /* no need for analog loopback */
2477
Takashi Iwai3ca529d2013-01-07 17:25:08 +01002478 path = snd_hda_add_new_path(codec, pin, mix_nid, 0);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002479 if (!path)
2480 return -EINVAL;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01002481 print_nid_path("loopback", path);
Takashi Iwai196c17662013-01-04 15:01:40 +01002482 spec->loopback_paths[input_idx] = snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002483
2484 idx = path->idx[path->depth - 1];
2485 if (nid_has_volume(codec, mix_nid, HDA_INPUT)) {
2486 val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
2487 err = __add_pb_vol_ctrl(spec, HDA_CTL_WIDGET_VOL, 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_VOL_CTL] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491 }
2492
Takashi Iwai352f7f92012-12-19 12:52:06 +01002493 if (nid_has_mute(codec, mix_nid, HDA_INPUT)) {
2494 val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
2495 err = __add_pb_sw_ctrl(spec, HDA_CTL_WIDGET_MUTE, ctlname, ctlidx, val);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002496 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002498 path->ctls[NID_PATH_MUTE_CTL] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499 }
2500
Takashi Iwai352f7f92012-12-19 12:52:06 +01002501 path->active = true;
2502 add_loopback_list(spec, mix_nid, idx);
Takashi Iwaie4a395e2013-01-23 17:00:31 +01002503
2504 if (spec->mixer_nid != spec->mixer_merge_nid &&
2505 !spec->loopback_merge_path) {
2506 path = snd_hda_add_new_path(codec, spec->mixer_nid,
2507 spec->mixer_merge_nid, 0);
2508 if (path) {
2509 print_nid_path("loopback-merge", path);
2510 path->active = true;
2511 spec->loopback_merge_path =
2512 snd_hda_get_path_idx(codec, path);
2513 }
2514 }
2515
Takashi Iwai352f7f92012-12-19 12:52:06 +01002516 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517}
2518
Takashi Iwai352f7f92012-12-19 12:52:06 +01002519static int is_input_pin(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002521 unsigned int pincap = snd_hda_query_pin_caps(codec, nid);
2522 return (pincap & AC_PINCAP_IN) != 0;
2523}
2524
2525/* Parse the codec tree and retrieve ADCs */
2526static int fill_adc_nids(struct hda_codec *codec)
2527{
2528 struct hda_gen_spec *spec = codec->spec;
2529 hda_nid_t nid;
2530 hda_nid_t *adc_nids = spec->adc_nids;
2531 int max_nums = ARRAY_SIZE(spec->adc_nids);
2532 int i, nums = 0;
2533
2534 nid = codec->start_nid;
2535 for (i = 0; i < codec->num_nodes; i++, nid++) {
2536 unsigned int caps = get_wcaps(codec, nid);
2537 int type = get_wcaps_type(caps);
2538
2539 if (type != AC_WID_AUD_IN || (caps & AC_WCAP_DIGITAL))
2540 continue;
2541 adc_nids[nums] = nid;
2542 if (++nums >= max_nums)
2543 break;
2544 }
2545 spec->num_adc_nids = nums;
Takashi Iwai0ffd5342013-01-17 15:53:29 +01002546
2547 /* copy the detected ADCs to all_adcs[] */
2548 spec->num_all_adcs = nums;
2549 memcpy(spec->all_adcs, spec->adc_nids, nums * sizeof(hda_nid_t));
2550
Takashi Iwai352f7f92012-12-19 12:52:06 +01002551 return nums;
2552}
2553
2554/* filter out invalid adc_nids that don't give all active input pins;
2555 * if needed, check whether dynamic ADC-switching is available
2556 */
2557static int check_dyn_adc_switch(struct hda_codec *codec)
2558{
2559 struct hda_gen_spec *spec = codec->spec;
2560 struct hda_input_mux *imux = &spec->input_mux;
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002561 unsigned int ok_bits;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002562 int i, n, nums;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002563
2564 again:
2565 nums = 0;
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002566 ok_bits = 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002567 for (n = 0; n < spec->num_adc_nids; n++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002568 for (i = 0; i < imux->num_items; i++) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002569 if (!spec->input_paths[i][n])
Takashi Iwai352f7f92012-12-19 12:52:06 +01002570 break;
2571 }
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002572 if (i >= imux->num_items) {
2573 ok_bits |= (1 << n);
2574 nums++;
2575 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002576 }
2577
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002578 if (!ok_bits) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002579 if (spec->shared_mic_hp) {
2580 spec->shared_mic_hp = 0;
2581 imux->num_items = 1;
2582 goto again;
2583 }
2584
2585 /* check whether ADC-switch is possible */
2586 for (i = 0; i < imux->num_items; i++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002587 for (n = 0; n < spec->num_adc_nids; n++) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002588 if (spec->input_paths[i][n]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002589 spec->dyn_adc_idx[i] = n;
2590 break;
2591 }
2592 }
2593 }
2594
2595 snd_printdd("hda-codec: enabling ADC switching\n");
2596 spec->dyn_adc_switch = 1;
2597 } else if (nums != spec->num_adc_nids) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002598 /* shrink the invalid adcs and input paths */
2599 nums = 0;
2600 for (n = 0; n < spec->num_adc_nids; n++) {
2601 if (!(ok_bits & (1 << n)))
2602 continue;
2603 if (n != nums) {
2604 spec->adc_nids[nums] = spec->adc_nids[n];
Takashi Iwai980428c2013-01-09 09:28:20 +01002605 for (i = 0; i < imux->num_items; i++) {
2606 invalidate_nid_path(codec,
2607 spec->input_paths[i][nums]);
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002608 spec->input_paths[i][nums] =
2609 spec->input_paths[i][n];
Takashi Iwai980428c2013-01-09 09:28:20 +01002610 }
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002611 }
2612 nums++;
2613 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002614 spec->num_adc_nids = nums;
2615 }
2616
2617 if (imux->num_items == 1 || spec->shared_mic_hp) {
2618 snd_printdd("hda-codec: reducing to a single ADC\n");
2619 spec->num_adc_nids = 1; /* reduce to a single ADC */
2620 }
2621
2622 /* single index for individual volumes ctls */
2623 if (!spec->dyn_adc_switch && spec->multi_cap_vol)
2624 spec->num_adc_nids = 1;
2625
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626 return 0;
2627}
2628
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002629/* parse capture source paths from the given pin and create imux items */
2630static int parse_capture_source(struct hda_codec *codec, hda_nid_t pin,
Takashi Iwai9dba2052013-01-18 10:01:15 +01002631 int cfg_idx, int num_adcs,
2632 const char *label, int anchor)
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002633{
2634 struct hda_gen_spec *spec = codec->spec;
2635 struct hda_input_mux *imux = &spec->input_mux;
2636 int imux_idx = imux->num_items;
2637 bool imux_added = false;
2638 int c;
2639
2640 for (c = 0; c < num_adcs; c++) {
2641 struct nid_path *path;
2642 hda_nid_t adc = spec->adc_nids[c];
2643
2644 if (!is_reachable_path(codec, pin, adc))
2645 continue;
2646 path = snd_hda_add_new_path(codec, pin, adc, anchor);
2647 if (!path)
2648 continue;
2649 print_nid_path("input", path);
2650 spec->input_paths[imux_idx][c] =
2651 snd_hda_get_path_idx(codec, path);
2652
2653 if (!imux_added) {
2654 spec->imux_pins[imux->num_items] = pin;
Takashi Iwai9dba2052013-01-18 10:01:15 +01002655 snd_hda_add_imux_item(imux, label, cfg_idx, NULL);
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002656 imux_added = true;
2657 }
2658 }
2659
2660 return 0;
2661}
2662
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01002664 * create playback/capture controls for input pins
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665 */
Takashi Iwai9dba2052013-01-18 10:01:15 +01002666
Takashi Iwaic9700422013-01-18 10:17:30 +01002667/* fill the label for each input at first */
2668static int fill_input_pin_labels(struct hda_codec *codec)
2669{
2670 struct hda_gen_spec *spec = codec->spec;
2671 const struct auto_pin_cfg *cfg = &spec->autocfg;
2672 int i;
2673
2674 for (i = 0; i < cfg->num_inputs; i++) {
2675 hda_nid_t pin = cfg->inputs[i].pin;
2676 const char *label;
2677 int j, idx;
2678
2679 if (!is_input_pin(codec, pin))
2680 continue;
2681
2682 label = hda_get_autocfg_input_label(codec, cfg, i);
2683 idx = 0;
David Henningsson8e8db7f2013-01-18 15:43:02 +01002684 for (j = i - 1; j >= 0; j--) {
Takashi Iwaic9700422013-01-18 10:17:30 +01002685 if (spec->input_labels[j] &&
2686 !strcmp(spec->input_labels[j], label)) {
2687 idx = spec->input_label_idxs[j] + 1;
2688 break;
2689 }
2690 }
2691
2692 spec->input_labels[i] = label;
2693 spec->input_label_idxs[i] = idx;
2694 }
2695
2696 return 0;
2697}
2698
Takashi Iwai9dba2052013-01-18 10:01:15 +01002699#define CFG_IDX_MIX 99 /* a dummy cfg->input idx for stereo mix */
2700
Takashi Iwai352f7f92012-12-19 12:52:06 +01002701static int create_input_ctls(struct hda_codec *codec)
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02002702{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002703 struct hda_gen_spec *spec = codec->spec;
2704 const struct auto_pin_cfg *cfg = &spec->autocfg;
2705 hda_nid_t mixer = spec->mixer_nid;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002706 int num_adcs;
Takashi Iwaic9700422013-01-18 10:17:30 +01002707 int i, err;
Takashi Iwai2c12c302013-01-10 09:33:29 +01002708 unsigned int val;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02002709
Takashi Iwai352f7f92012-12-19 12:52:06 +01002710 num_adcs = fill_adc_nids(codec);
2711 if (num_adcs < 0)
2712 return 0;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02002713
Takashi Iwaic9700422013-01-18 10:17:30 +01002714 err = fill_input_pin_labels(codec);
2715 if (err < 0)
2716 return err;
2717
Takashi Iwai352f7f92012-12-19 12:52:06 +01002718 for (i = 0; i < cfg->num_inputs; i++) {
2719 hda_nid_t pin;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720
Takashi Iwai352f7f92012-12-19 12:52:06 +01002721 pin = cfg->inputs[i].pin;
2722 if (!is_input_pin(codec, pin))
2723 continue;
2724
Takashi Iwai2c12c302013-01-10 09:33:29 +01002725 val = PIN_IN;
2726 if (cfg->inputs[i].type == AUTO_PIN_MIC)
2727 val |= snd_hda_get_default_vref(codec, pin);
2728 set_pin_target(codec, pin, val, false);
2729
Takashi Iwai352f7f92012-12-19 12:52:06 +01002730 if (mixer) {
2731 if (is_reachable_path(codec, pin, mixer)) {
Takashi Iwai196c17662013-01-04 15:01:40 +01002732 err = new_analog_input(codec, i, pin,
Takashi Iwaic9700422013-01-18 10:17:30 +01002733 spec->input_labels[i],
2734 spec->input_label_idxs[i],
2735 mixer);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002736 if (err < 0)
2737 return err;
2738 }
2739 }
2740
Takashi Iwaic9700422013-01-18 10:17:30 +01002741 err = parse_capture_source(codec, pin, i, num_adcs,
2742 spec->input_labels[i], -mixer);
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002743 if (err < 0)
2744 return err;
Takashi Iwai294765582013-01-17 09:52:11 +01002745
2746 if (spec->add_in_jack_modes) {
2747 err = create_in_jack_mode(codec, pin);
2748 if (err < 0)
2749 return err;
2750 }
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002751 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002752
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002753 if (mixer && spec->add_stereo_mix_input) {
Takashi Iwai9dba2052013-01-18 10:01:15 +01002754 err = parse_capture_source(codec, mixer, CFG_IDX_MIX, num_adcs,
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002755 "Stereo Mix", 0);
2756 if (err < 0)
2757 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002758 }
2759
2760 return 0;
2761}
2762
2763
2764/*
2765 * input source mux
2766 */
2767
Takashi Iwaic697b712013-01-07 17:09:26 +01002768/* get the input path specified by the given adc and imux indices */
2769static struct nid_path *get_input_path(struct hda_codec *codec, int adc_idx, int imux_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002770{
2771 struct hda_gen_spec *spec = codec->spec;
David Henningssonb56fa1e2013-01-16 11:45:35 +01002772 if (imux_idx < 0 || imux_idx >= HDA_MAX_NUM_INPUTS) {
2773 snd_BUG();
2774 return NULL;
2775 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002776 if (spec->dyn_adc_switch)
2777 adc_idx = spec->dyn_adc_idx[imux_idx];
David Henningssond3d982f2013-01-18 15:43:01 +01002778 if (adc_idx < 0 || adc_idx >= AUTO_CFG_MAX_INS) {
David Henningssonb56fa1e2013-01-16 11:45:35 +01002779 snd_BUG();
2780 return NULL;
2781 }
Takashi Iwaic697b712013-01-07 17:09:26 +01002782 return snd_hda_get_path_from_idx(codec, spec->input_paths[imux_idx][adc_idx]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002783}
2784
2785static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
2786 unsigned int idx);
2787
2788static int mux_enum_info(struct snd_kcontrol *kcontrol,
2789 struct snd_ctl_elem_info *uinfo)
2790{
2791 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2792 struct hda_gen_spec *spec = codec->spec;
2793 return snd_hda_input_mux_info(&spec->input_mux, uinfo);
2794}
2795
2796static int mux_enum_get(struct snd_kcontrol *kcontrol,
2797 struct snd_ctl_elem_value *ucontrol)
2798{
2799 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2800 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai2a8d5392013-01-18 16:23:25 +01002801 /* the ctls are created at once with multiple counts */
2802 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002803
2804 ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
2805 return 0;
2806}
2807
2808static int mux_enum_put(struct snd_kcontrol *kcontrol,
2809 struct snd_ctl_elem_value *ucontrol)
2810{
2811 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Takashi Iwai2a8d5392013-01-18 16:23:25 +01002812 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002813 return mux_select(codec, adc_idx,
2814 ucontrol->value.enumerated.item[0]);
2815}
2816
Takashi Iwai352f7f92012-12-19 12:52:06 +01002817static const struct snd_kcontrol_new cap_src_temp = {
2818 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2819 .name = "Input Source",
2820 .info = mux_enum_info,
2821 .get = mux_enum_get,
2822 .put = mux_enum_put,
2823};
2824
Takashi Iwai47d46ab2012-12-20 11:48:54 +01002825/*
2826 * capture volume and capture switch ctls
2827 */
2828
Takashi Iwai352f7f92012-12-19 12:52:06 +01002829typedef int (*put_call_t)(struct snd_kcontrol *kcontrol,
2830 struct snd_ctl_elem_value *ucontrol);
2831
Takashi Iwai47d46ab2012-12-20 11:48:54 +01002832/* call the given amp update function for all amps in the imux list at once */
Takashi Iwai352f7f92012-12-19 12:52:06 +01002833static int cap_put_caller(struct snd_kcontrol *kcontrol,
2834 struct snd_ctl_elem_value *ucontrol,
2835 put_call_t func, int type)
2836{
2837 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2838 struct hda_gen_spec *spec = codec->spec;
2839 const struct hda_input_mux *imux;
2840 struct nid_path *path;
2841 int i, adc_idx, err = 0;
2842
2843 imux = &spec->input_mux;
David Henningssona053d1e2013-01-16 11:45:36 +01002844 adc_idx = kcontrol->id.index;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002845 mutex_lock(&codec->control_mutex);
Takashi Iwai47d46ab2012-12-20 11:48:54 +01002846 /* we use the cache-only update at first since multiple input paths
2847 * may shared the same amp; by updating only caches, the redundant
2848 * writes to hardware can be reduced.
2849 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01002850 codec->cached_write = 1;
2851 for (i = 0; i < imux->num_items; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01002852 path = get_input_path(codec, adc_idx, i);
2853 if (!path || !path->ctls[type])
Takashi Iwai352f7f92012-12-19 12:52:06 +01002854 continue;
2855 kcontrol->private_value = path->ctls[type];
2856 err = func(kcontrol, ucontrol);
2857 if (err < 0)
2858 goto error;
2859 }
2860 error:
2861 codec->cached_write = 0;
2862 mutex_unlock(&codec->control_mutex);
Takashi Iwaidc870f32013-01-22 15:24:30 +01002863 snd_hda_codec_flush_cache(codec); /* flush the updates */
Takashi Iwai352f7f92012-12-19 12:52:06 +01002864 if (err >= 0 && spec->cap_sync_hook)
Takashi Iwaia90229e2013-01-18 14:10:00 +01002865 spec->cap_sync_hook(codec, ucontrol);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002866 return err;
2867}
2868
2869/* capture volume ctl callbacks */
2870#define cap_vol_info snd_hda_mixer_amp_volume_info
2871#define cap_vol_get snd_hda_mixer_amp_volume_get
2872#define cap_vol_tlv snd_hda_mixer_amp_tlv
2873
2874static int cap_vol_put(struct snd_kcontrol *kcontrol,
2875 struct snd_ctl_elem_value *ucontrol)
2876{
2877 return cap_put_caller(kcontrol, ucontrol,
2878 snd_hda_mixer_amp_volume_put,
2879 NID_PATH_VOL_CTL);
2880}
2881
2882static const struct snd_kcontrol_new cap_vol_temp = {
2883 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2884 .name = "Capture Volume",
2885 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
2886 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
2887 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK),
2888 .info = cap_vol_info,
2889 .get = cap_vol_get,
2890 .put = cap_vol_put,
2891 .tlv = { .c = cap_vol_tlv },
2892};
2893
2894/* capture switch ctl callbacks */
2895#define cap_sw_info snd_ctl_boolean_stereo_info
2896#define cap_sw_get snd_hda_mixer_amp_switch_get
2897
2898static int cap_sw_put(struct snd_kcontrol *kcontrol,
2899 struct snd_ctl_elem_value *ucontrol)
2900{
Takashi Iwaia90229e2013-01-18 14:10:00 +01002901 return cap_put_caller(kcontrol, ucontrol,
Takashi Iwai352f7f92012-12-19 12:52:06 +01002902 snd_hda_mixer_amp_switch_put,
2903 NID_PATH_MUTE_CTL);
2904}
2905
2906static const struct snd_kcontrol_new cap_sw_temp = {
2907 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2908 .name = "Capture Switch",
2909 .info = cap_sw_info,
2910 .get = cap_sw_get,
2911 .put = cap_sw_put,
2912};
2913
2914static int parse_capvol_in_path(struct hda_codec *codec, struct nid_path *path)
2915{
2916 hda_nid_t nid;
2917 int i, depth;
2918
2919 path->ctls[NID_PATH_VOL_CTL] = path->ctls[NID_PATH_MUTE_CTL] = 0;
2920 for (depth = 0; depth < 3; depth++) {
2921 if (depth >= path->depth)
2922 return -EINVAL;
2923 i = path->depth - depth - 1;
2924 nid = path->path[i];
2925 if (!path->ctls[NID_PATH_VOL_CTL]) {
2926 if (nid_has_volume(codec, nid, HDA_OUTPUT))
2927 path->ctls[NID_PATH_VOL_CTL] =
2928 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
2929 else if (nid_has_volume(codec, nid, HDA_INPUT)) {
2930 int idx = path->idx[i];
2931 if (!depth && codec->single_adc_amp)
2932 idx = 0;
2933 path->ctls[NID_PATH_VOL_CTL] =
2934 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
2935 }
2936 }
2937 if (!path->ctls[NID_PATH_MUTE_CTL]) {
2938 if (nid_has_mute(codec, nid, HDA_OUTPUT))
2939 path->ctls[NID_PATH_MUTE_CTL] =
2940 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
2941 else if (nid_has_mute(codec, nid, HDA_INPUT)) {
2942 int idx = path->idx[i];
2943 if (!depth && codec->single_adc_amp)
2944 idx = 0;
2945 path->ctls[NID_PATH_MUTE_CTL] =
2946 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
2947 }
2948 }
Takashi Iwai97ec5582006-03-21 11:29:07 +01002949 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950 return 0;
2951}
2952
Takashi Iwai352f7f92012-12-19 12:52:06 +01002953static bool is_inv_dmic_pin(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002955 struct hda_gen_spec *spec = codec->spec;
2956 struct auto_pin_cfg *cfg = &spec->autocfg;
2957 unsigned int val;
2958 int i;
2959
2960 if (!spec->inv_dmic_split)
2961 return false;
2962 for (i = 0; i < cfg->num_inputs; i++) {
2963 if (cfg->inputs[i].pin != nid)
2964 continue;
2965 if (cfg->inputs[i].type != AUTO_PIN_MIC)
2966 return false;
2967 val = snd_hda_codec_get_pincfg(codec, nid);
2968 return snd_hda_get_input_pin_attr(val) == INPUT_PIN_ATTR_INT;
2969 }
2970 return false;
2971}
2972
Takashi Iwaia90229e2013-01-18 14:10:00 +01002973/* capture switch put callback for a single control with hook call */
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01002974static int cap_single_sw_put(struct snd_kcontrol *kcontrol,
2975 struct snd_ctl_elem_value *ucontrol)
2976{
2977 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2978 struct hda_gen_spec *spec = codec->spec;
2979 int ret;
2980
2981 ret = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2982 if (ret < 0)
2983 return ret;
2984
Takashi Iwaia90229e2013-01-18 14:10:00 +01002985 if (spec->cap_sync_hook)
2986 spec->cap_sync_hook(codec, ucontrol);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01002987
2988 return ret;
2989}
2990
Takashi Iwai352f7f92012-12-19 12:52:06 +01002991static int add_single_cap_ctl(struct hda_codec *codec, const char *label,
2992 int idx, bool is_switch, unsigned int ctl,
2993 bool inv_dmic)
2994{
2995 struct hda_gen_spec *spec = codec->spec;
2996 char tmpname[44];
2997 int type = is_switch ? HDA_CTL_WIDGET_MUTE : HDA_CTL_WIDGET_VOL;
2998 const char *sfx = is_switch ? "Switch" : "Volume";
2999 unsigned int chs = inv_dmic ? 1 : 3;
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003000 struct snd_kcontrol_new *knew;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003001
3002 if (!ctl)
3003 return 0;
3004
3005 if (label)
3006 snprintf(tmpname, sizeof(tmpname),
3007 "%s Capture %s", label, sfx);
3008 else
3009 snprintf(tmpname, sizeof(tmpname),
3010 "Capture %s", sfx);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003011 knew = add_control(spec, type, tmpname, idx,
3012 amp_val_replace_channels(ctl, chs));
3013 if (!knew)
3014 return -ENOMEM;
Takashi Iwaia90229e2013-01-18 14:10:00 +01003015 if (is_switch)
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003016 knew->put = cap_single_sw_put;
3017 if (!inv_dmic)
3018 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003019
3020 /* Make independent right kcontrol */
3021 if (label)
3022 snprintf(tmpname, sizeof(tmpname),
3023 "Inverted %s Capture %s", label, sfx);
3024 else
3025 snprintf(tmpname, sizeof(tmpname),
3026 "Inverted Capture %s", sfx);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003027 knew = add_control(spec, type, tmpname, idx,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003028 amp_val_replace_channels(ctl, 2));
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003029 if (!knew)
3030 return -ENOMEM;
Takashi Iwaia90229e2013-01-18 14:10:00 +01003031 if (is_switch)
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003032 knew->put = cap_single_sw_put;
3033 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003034}
3035
3036/* create single (and simple) capture volume and switch controls */
3037static int create_single_cap_vol_ctl(struct hda_codec *codec, int idx,
3038 unsigned int vol_ctl, unsigned int sw_ctl,
3039 bool inv_dmic)
3040{
3041 int err;
3042 err = add_single_cap_ctl(codec, NULL, idx, false, vol_ctl, inv_dmic);
3043 if (err < 0)
3044 return err;
3045 err = add_single_cap_ctl(codec, NULL, idx, true, sw_ctl, inv_dmic);
3046 if (err < 0)
3047 return err;
3048 return 0;
3049}
3050
3051/* create bound capture volume and switch controls */
3052static int create_bind_cap_vol_ctl(struct hda_codec *codec, int idx,
3053 unsigned int vol_ctl, unsigned int sw_ctl)
3054{
3055 struct hda_gen_spec *spec = codec->spec;
3056 struct snd_kcontrol_new *knew;
3057
3058 if (vol_ctl) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01003059 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_vol_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003060 if (!knew)
3061 return -ENOMEM;
3062 knew->index = idx;
3063 knew->private_value = vol_ctl;
3064 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
3065 }
3066 if (sw_ctl) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01003067 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_sw_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003068 if (!knew)
3069 return -ENOMEM;
3070 knew->index = idx;
3071 knew->private_value = sw_ctl;
3072 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
3073 }
3074 return 0;
3075}
3076
3077/* return the vol ctl when used first in the imux list */
3078static unsigned int get_first_cap_ctl(struct hda_codec *codec, int idx, int type)
3079{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003080 struct nid_path *path;
3081 unsigned int ctl;
3082 int i;
3083
Takashi Iwaic697b712013-01-07 17:09:26 +01003084 path = get_input_path(codec, 0, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003085 if (!path)
3086 return 0;
3087 ctl = path->ctls[type];
3088 if (!ctl)
3089 return 0;
3090 for (i = 0; i < idx - 1; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01003091 path = get_input_path(codec, 0, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003092 if (path && path->ctls[type] == ctl)
3093 return 0;
3094 }
3095 return ctl;
3096}
3097
3098/* create individual capture volume and switch controls per input */
3099static int create_multi_cap_vol_ctl(struct hda_codec *codec)
3100{
3101 struct hda_gen_spec *spec = codec->spec;
3102 struct hda_input_mux *imux = &spec->input_mux;
Takashi Iwaic9700422013-01-18 10:17:30 +01003103 int i, err, type;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003104
3105 for (i = 0; i < imux->num_items; i++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003106 bool inv_dmic;
Takashi Iwaic9700422013-01-18 10:17:30 +01003107 int idx;
Takashi Iwai9dba2052013-01-18 10:01:15 +01003108
Takashi Iwaic9700422013-01-18 10:17:30 +01003109 idx = imux->items[i].index;
3110 if (idx >= spec->autocfg.num_inputs)
Takashi Iwai9dba2052013-01-18 10:01:15 +01003111 continue;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003112 inv_dmic = is_inv_dmic_pin(codec, spec->imux_pins[i]);
3113
3114 for (type = 0; type < 2; type++) {
Takashi Iwaic9700422013-01-18 10:17:30 +01003115 err = add_single_cap_ctl(codec,
3116 spec->input_labels[idx],
3117 spec->input_label_idxs[idx],
3118 type,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003119 get_first_cap_ctl(codec, i, type),
3120 inv_dmic);
3121 if (err < 0)
3122 return err;
3123 }
3124 }
3125 return 0;
3126}
3127
3128static int create_capture_mixers(struct hda_codec *codec)
3129{
3130 struct hda_gen_spec *spec = codec->spec;
3131 struct hda_input_mux *imux = &spec->input_mux;
3132 int i, n, nums, err;
3133
3134 if (spec->dyn_adc_switch)
3135 nums = 1;
3136 else
3137 nums = spec->num_adc_nids;
3138
3139 if (!spec->auto_mic && imux->num_items > 1) {
3140 struct snd_kcontrol_new *knew;
Takashi Iwai624d9142012-12-19 17:41:52 +01003141 const char *name;
3142 name = nums > 1 ? "Input Source" : "Capture Source";
3143 knew = snd_hda_gen_add_kctl(spec, name, &cap_src_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003144 if (!knew)
3145 return -ENOMEM;
3146 knew->count = nums;
3147 }
3148
3149 for (n = 0; n < nums; n++) {
3150 bool multi = false;
David Henningsson99a55922013-01-16 15:58:44 +01003151 bool multi_cap_vol = spec->multi_cap_vol;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003152 bool inv_dmic = false;
3153 int vol, sw;
3154
3155 vol = sw = 0;
3156 for (i = 0; i < imux->num_items; i++) {
3157 struct nid_path *path;
Takashi Iwaic697b712013-01-07 17:09:26 +01003158 path = get_input_path(codec, n, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003159 if (!path)
3160 continue;
3161 parse_capvol_in_path(codec, path);
3162 if (!vol)
3163 vol = path->ctls[NID_PATH_VOL_CTL];
David Henningsson99a55922013-01-16 15:58:44 +01003164 else if (vol != path->ctls[NID_PATH_VOL_CTL]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003165 multi = true;
David Henningsson99a55922013-01-16 15:58:44 +01003166 if (!same_amp_caps(codec, vol,
3167 path->ctls[NID_PATH_VOL_CTL], HDA_INPUT))
3168 multi_cap_vol = true;
3169 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003170 if (!sw)
3171 sw = path->ctls[NID_PATH_MUTE_CTL];
David Henningsson99a55922013-01-16 15:58:44 +01003172 else if (sw != path->ctls[NID_PATH_MUTE_CTL]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003173 multi = true;
David Henningsson99a55922013-01-16 15:58:44 +01003174 if (!same_amp_caps(codec, sw,
3175 path->ctls[NID_PATH_MUTE_CTL], HDA_INPUT))
3176 multi_cap_vol = true;
3177 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003178 if (is_inv_dmic_pin(codec, spec->imux_pins[i]))
3179 inv_dmic = true;
3180 }
3181
3182 if (!multi)
3183 err = create_single_cap_vol_ctl(codec, n, vol, sw,
3184 inv_dmic);
David Henningsson99a55922013-01-16 15:58:44 +01003185 else if (!multi_cap_vol)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003186 err = create_bind_cap_vol_ctl(codec, n, vol, sw);
3187 else
3188 err = create_multi_cap_vol_ctl(codec);
3189 if (err < 0)
3190 return err;
3191 }
3192
3193 return 0;
3194}
3195
3196/*
3197 * add mic boosts if needed
3198 */
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003199
3200/* check whether the given amp is feasible as a boost volume */
3201static bool check_boost_vol(struct hda_codec *codec, hda_nid_t nid,
3202 int dir, int idx)
3203{
3204 unsigned int step;
3205
3206 if (!nid_has_volume(codec, nid, dir) ||
3207 is_ctl_associated(codec, nid, dir, idx, NID_PATH_VOL_CTL) ||
3208 is_ctl_associated(codec, nid, dir, idx, NID_PATH_BOOST_CTL))
3209 return false;
3210
3211 step = (query_amp_caps(codec, nid, dir) & AC_AMPCAP_STEP_SIZE)
3212 >> AC_AMPCAP_STEP_SIZE_SHIFT;
3213 if (step < 0x20)
3214 return false;
3215 return true;
3216}
3217
3218/* look for a boost amp in a widget close to the pin */
3219static unsigned int look_for_boost_amp(struct hda_codec *codec,
3220 struct nid_path *path)
3221{
3222 unsigned int val = 0;
3223 hda_nid_t nid;
3224 int depth;
3225
3226 for (depth = 0; depth < 3; depth++) {
3227 if (depth >= path->depth - 1)
3228 break;
3229 nid = path->path[depth];
3230 if (depth && check_boost_vol(codec, nid, HDA_OUTPUT, 0)) {
3231 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3232 break;
3233 } else if (check_boost_vol(codec, nid, HDA_INPUT,
3234 path->idx[depth])) {
3235 val = HDA_COMPOSE_AMP_VAL(nid, 3, path->idx[depth],
3236 HDA_INPUT);
3237 break;
3238 }
3239 }
3240
3241 return val;
3242}
3243
Takashi Iwai352f7f92012-12-19 12:52:06 +01003244static int parse_mic_boost(struct hda_codec *codec)
3245{
3246 struct hda_gen_spec *spec = codec->spec;
3247 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003248 struct hda_input_mux *imux = &spec->input_mux;
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003249 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003250
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003251 if (!spec->num_adc_nids)
3252 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003253
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003254 for (i = 0; i < imux->num_items; i++) {
3255 struct nid_path *path;
3256 unsigned int val;
3257 int idx;
3258 char boost_label[44];
David Henningsson02aba552013-01-16 15:58:43 +01003259
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003260 idx = imux->items[i].index;
3261 if (idx >= imux->num_items)
3262 continue;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003263
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003264 /* check only line-in and mic pins */
Takashi Iwai1799cdd52013-01-18 14:37:16 +01003265 if (cfg->inputs[idx].type > AUTO_PIN_LINE_IN)
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003266 continue;
3267
3268 path = get_input_path(codec, 0, i);
3269 if (!path)
3270 continue;
3271
3272 val = look_for_boost_amp(codec, path);
3273 if (!val)
3274 continue;
3275
3276 /* create a boost control */
3277 snprintf(boost_label, sizeof(boost_label),
3278 "%s Boost Volume", spec->input_labels[idx]);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003279 if (!add_control(spec, HDA_CTL_WIDGET_VOL, boost_label,
3280 spec->input_label_idxs[idx], val))
3281 return -ENOMEM;
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003282
3283 path->ctls[NID_PATH_BOOST_CTL] = val;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003284 }
3285 return 0;
3286}
3287
3288/*
3289 * parse digital I/Os and set up NIDs in BIOS auto-parse mode
3290 */
3291static void parse_digital(struct hda_codec *codec)
3292{
3293 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003294 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003295 int i, nums;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003296 hda_nid_t dig_nid, pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003297
3298 /* support multiple SPDIFs; the secondary is set up as a slave */
3299 nums = 0;
3300 for (i = 0; i < spec->autocfg.dig_outs; i++) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01003301 pin = spec->autocfg.dig_out_pins[i];
Takashi Iwai352f7f92012-12-19 12:52:06 +01003302 dig_nid = look_for_dac(codec, pin, true);
3303 if (!dig_nid)
3304 continue;
Takashi Iwai3ca529d2013-01-07 17:25:08 +01003305 path = snd_hda_add_new_path(codec, dig_nid, pin, 0);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003306 if (!path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003307 continue;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003308 print_nid_path("digout", path);
Takashi Iwaie1284af2013-01-03 16:33:02 +01003309 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +01003310 spec->digout_paths[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai2c12c302013-01-10 09:33:29 +01003311 set_pin_target(codec, pin, PIN_OUT, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003312 if (!nums) {
3313 spec->multiout.dig_out_nid = dig_nid;
3314 spec->dig_out_type = spec->autocfg.dig_out_type[0];
3315 } else {
3316 spec->multiout.slave_dig_outs = spec->slave_dig_outs;
3317 if (nums >= ARRAY_SIZE(spec->slave_dig_outs) - 1)
3318 break;
3319 spec->slave_dig_outs[nums - 1] = dig_nid;
3320 }
3321 nums++;
3322 }
3323
3324 if (spec->autocfg.dig_in_pin) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01003325 pin = spec->autocfg.dig_in_pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003326 dig_nid = codec->start_nid;
3327 for (i = 0; i < codec->num_nodes; i++, dig_nid++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003328 unsigned int wcaps = get_wcaps(codec, dig_nid);
3329 if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
3330 continue;
3331 if (!(wcaps & AC_WCAP_DIGITAL))
3332 continue;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003333 path = snd_hda_add_new_path(codec, pin, dig_nid, 0);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003334 if (path) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003335 print_nid_path("digin", path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003336 path->active = true;
3337 spec->dig_in_nid = dig_nid;
Takashi Iwai2430d7b2013-01-04 15:09:42 +01003338 spec->digin_path = snd_hda_get_path_idx(codec, path);
Takashi Iwai2c12c302013-01-10 09:33:29 +01003339 set_pin_target(codec, pin, PIN_IN, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003340 break;
3341 }
3342 }
3343 }
3344}
3345
3346
3347/*
3348 * input MUX handling
3349 */
3350
3351static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur);
3352
3353/* select the given imux item; either unmute exclusively or select the route */
3354static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
3355 unsigned int idx)
3356{
3357 struct hda_gen_spec *spec = codec->spec;
3358 const struct hda_input_mux *imux;
3359 struct nid_path *path;
3360
3361 imux = &spec->input_mux;
3362 if (!imux->num_items)
3363 return 0;
3364
3365 if (idx >= imux->num_items)
3366 idx = imux->num_items - 1;
3367 if (spec->cur_mux[adc_idx] == idx)
3368 return 0;
3369
Takashi Iwaic697b712013-01-07 17:09:26 +01003370 path = get_input_path(codec, adc_idx, spec->cur_mux[adc_idx]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003371 if (!path)
3372 return 0;
3373 if (path->active)
3374 snd_hda_activate_path(codec, path, false, false);
3375
3376 spec->cur_mux[adc_idx] = idx;
3377
3378 if (spec->shared_mic_hp)
3379 update_shared_mic_hp(codec, spec->cur_mux[adc_idx]);
3380
3381 if (spec->dyn_adc_switch)
3382 dyn_adc_pcm_resetup(codec, idx);
3383
Takashi Iwaic697b712013-01-07 17:09:26 +01003384 path = get_input_path(codec, adc_idx, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003385 if (!path)
3386 return 0;
3387 if (path->active)
3388 return 0;
3389 snd_hda_activate_path(codec, path, true, false);
3390 if (spec->cap_sync_hook)
Takashi Iwaia90229e2013-01-18 14:10:00 +01003391 spec->cap_sync_hook(codec, NULL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003392 return 1;
3393}
3394
3395
3396/*
3397 * Jack detections for HP auto-mute and mic-switch
3398 */
3399
3400/* check each pin in the given array; returns true if any of them is plugged */
3401static bool detect_jacks(struct hda_codec *codec, int num_pins, hda_nid_t *pins)
3402{
3403 int i, present = 0;
3404
3405 for (i = 0; i < num_pins; i++) {
3406 hda_nid_t nid = pins[i];
3407 if (!nid)
3408 break;
Takashi Iwai0b4df932013-01-10 09:45:13 +01003409 /* don't detect pins retasked as inputs */
3410 if (snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_IN_EN)
3411 continue;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003412 present |= snd_hda_jack_detect(codec, nid);
3413 }
3414 return present;
3415}
3416
3417/* standard HP/line-out auto-mute helper */
3418static void do_automute(struct hda_codec *codec, int num_pins, hda_nid_t *pins,
Takashi Iwai2c12c302013-01-10 09:33:29 +01003419 bool mute)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003420{
3421 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003422 int i;
3423
3424 for (i = 0; i < num_pins; i++) {
3425 hda_nid_t nid = pins[i];
3426 unsigned int val;
3427 if (!nid)
3428 break;
3429 /* don't reset VREF value in case it's controlling
3430 * the amp (see alc861_fixup_asus_amp_vref_0f())
3431 */
Takashi Iwai2c12c302013-01-10 09:33:29 +01003432 if (spec->keep_vref_in_automute)
3433 val = snd_hda_codec_get_pin_target(codec, nid) & ~PIN_HP;
3434 else
Takashi Iwai352f7f92012-12-19 12:52:06 +01003435 val = 0;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003436 if (!mute)
3437 val |= snd_hda_codec_get_pin_target(codec, nid);
3438 /* here we call update_pin_ctl() so that the pinctl is changed
3439 * without changing the pinctl target value;
3440 * the original target value will be still referred at the
3441 * init / resume again
3442 */
3443 update_pin_ctl(codec, nid, val);
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01003444 set_pin_eapd(codec, nid, !mute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003445 }
3446}
3447
3448/* Toggle outputs muting */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003449void snd_hda_gen_update_outputs(struct hda_codec *codec)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003450{
3451 struct hda_gen_spec *spec = codec->spec;
3452 int on;
3453
3454 /* Control HP pins/amps depending on master_mute state;
3455 * in general, HP pins/amps control should be enabled in all cases,
3456 * but currently set only for master_mute, just to be safe
3457 */
3458 if (!spec->shared_mic_hp) /* don't change HP-pin when shared with mic */
3459 do_automute(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
Takashi Iwai2c12c302013-01-10 09:33:29 +01003460 spec->autocfg.hp_pins, spec->master_mute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003461
3462 if (!spec->automute_speaker)
3463 on = 0;
3464 else
3465 on = spec->hp_jack_present | spec->line_jack_present;
3466 on |= spec->master_mute;
Takashi Iwai47b9ddb2013-01-16 18:18:00 +01003467 spec->speaker_muted = on;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003468 do_automute(codec, ARRAY_SIZE(spec->autocfg.speaker_pins),
Takashi Iwai2c12c302013-01-10 09:33:29 +01003469 spec->autocfg.speaker_pins, on);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003470
3471 /* toggle line-out mutes if needed, too */
3472 /* if LO is a copy of either HP or Speaker, don't need to handle it */
3473 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0] ||
3474 spec->autocfg.line_out_pins[0] == spec->autocfg.speaker_pins[0])
3475 return;
3476 if (!spec->automute_lo)
3477 on = 0;
3478 else
3479 on = spec->hp_jack_present;
3480 on |= spec->master_mute;
Takashi Iwai47b9ddb2013-01-16 18:18:00 +01003481 spec->line_out_muted = on;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003482 do_automute(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
Takashi Iwai2c12c302013-01-10 09:33:29 +01003483 spec->autocfg.line_out_pins, on);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003484}
Takashi Iwai5d550e12012-12-19 15:16:44 +01003485EXPORT_SYMBOL_HDA(snd_hda_gen_update_outputs);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003486
3487static void call_update_outputs(struct hda_codec *codec)
3488{
3489 struct hda_gen_spec *spec = codec->spec;
3490 if (spec->automute_hook)
3491 spec->automute_hook(codec);
3492 else
Takashi Iwai5d550e12012-12-19 15:16:44 +01003493 snd_hda_gen_update_outputs(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003494}
3495
3496/* standard HP-automute helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003497void snd_hda_gen_hp_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;
Takashi Iwai92603c52013-01-22 07:46:31 +01003500 hda_nid_t *pins = spec->autocfg.hp_pins;
3501 int num_pins = ARRAY_SIZE(spec->autocfg.hp_pins);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003502
Takashi Iwai92603c52013-01-22 07:46:31 +01003503 /* No detection for the first HP jack during indep-HP mode */
3504 if (spec->indep_hp_enabled) {
3505 pins++;
3506 num_pins--;
3507 }
3508
3509 spec->hp_jack_present = detect_jacks(codec, num_pins, pins);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003510 if (!spec->detect_hp || (!spec->automute_speaker && !spec->automute_lo))
3511 return;
3512 call_update_outputs(codec);
3513}
Takashi Iwai5d550e12012-12-19 15:16:44 +01003514EXPORT_SYMBOL_HDA(snd_hda_gen_hp_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003515
3516/* standard line-out-automute helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003517void snd_hda_gen_line_automute(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
3521 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
3522 return;
3523 /* check LO jack only when it's different from HP */
3524 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0])
3525 return;
3526
3527 spec->line_jack_present =
3528 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
3529 spec->autocfg.line_out_pins);
3530 if (!spec->automute_speaker || !spec->detect_lo)
3531 return;
3532 call_update_outputs(codec);
3533}
Takashi Iwai5d550e12012-12-19 15:16:44 +01003534EXPORT_SYMBOL_HDA(snd_hda_gen_line_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003535
3536/* standard mic auto-switch helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003537void snd_hda_gen_mic_autoswitch(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003538{
3539 struct hda_gen_spec *spec = codec->spec;
3540 int i;
3541
3542 if (!spec->auto_mic)
3543 return;
3544
3545 for (i = spec->am_num_entries - 1; i > 0; i--) {
Takashi Iwai0b4df932013-01-10 09:45:13 +01003546 hda_nid_t pin = spec->am_entry[i].pin;
3547 /* don't detect pins retasked as outputs */
3548 if (snd_hda_codec_get_pin_target(codec, pin) & AC_PINCTL_OUT_EN)
3549 continue;
3550 if (snd_hda_jack_detect(codec, pin)) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003551 mux_select(codec, 0, spec->am_entry[i].idx);
3552 return;
3553 }
3554 }
3555 mux_select(codec, 0, spec->am_entry[0].idx);
3556}
Takashi Iwai5d550e12012-12-19 15:16:44 +01003557EXPORT_SYMBOL_HDA(snd_hda_gen_mic_autoswitch);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003558
Takashi Iwaia5cc2502013-01-16 18:08:55 +01003559/* update jack retasking */
3560static void update_automute_all(struct hda_codec *codec)
3561{
3562 struct hda_gen_spec *spec = codec->spec;
3563
3564 if (spec->hp_automute_hook)
3565 spec->hp_automute_hook(codec, NULL);
3566 else
3567 snd_hda_gen_hp_automute(codec, NULL);
3568 if (spec->line_automute_hook)
3569 spec->line_automute_hook(codec, NULL);
3570 else
3571 snd_hda_gen_line_automute(codec, NULL);
3572 if (spec->mic_autoswitch_hook)
3573 spec->mic_autoswitch_hook(codec, NULL);
3574 else
3575 snd_hda_gen_mic_autoswitch(codec, NULL);
3576}
3577
Takashi Iwai352f7f92012-12-19 12:52:06 +01003578/*
3579 * Auto-Mute mode mixer enum support
3580 */
3581static int automute_mode_info(struct snd_kcontrol *kcontrol,
3582 struct snd_ctl_elem_info *uinfo)
3583{
3584 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3585 struct hda_gen_spec *spec = codec->spec;
3586 static const char * const texts3[] = {
3587 "Disabled", "Speaker Only", "Line Out+Speaker"
Takashi Iwai071c73a2006-08-23 18:34:06 +02003588 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003589
Takashi Iwai352f7f92012-12-19 12:52:06 +01003590 if (spec->automute_speaker_possible && spec->automute_lo_possible)
3591 return snd_hda_enum_helper_info(kcontrol, uinfo, 3, texts3);
3592 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
3593}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003594
Takashi Iwai352f7f92012-12-19 12:52:06 +01003595static int automute_mode_get(struct snd_kcontrol *kcontrol,
3596 struct snd_ctl_elem_value *ucontrol)
3597{
3598 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3599 struct hda_gen_spec *spec = codec->spec;
3600 unsigned int val = 0;
3601 if (spec->automute_speaker)
3602 val++;
3603 if (spec->automute_lo)
3604 val++;
Takashi Iwai071c73a2006-08-23 18:34:06 +02003605
Takashi Iwai352f7f92012-12-19 12:52:06 +01003606 ucontrol->value.enumerated.item[0] = val;
3607 return 0;
3608}
3609
3610static int automute_mode_put(struct snd_kcontrol *kcontrol,
3611 struct snd_ctl_elem_value *ucontrol)
3612{
3613 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3614 struct hda_gen_spec *spec = codec->spec;
3615
3616 switch (ucontrol->value.enumerated.item[0]) {
3617 case 0:
3618 if (!spec->automute_speaker && !spec->automute_lo)
3619 return 0;
3620 spec->automute_speaker = 0;
3621 spec->automute_lo = 0;
3622 break;
3623 case 1:
3624 if (spec->automute_speaker_possible) {
3625 if (!spec->automute_lo && spec->automute_speaker)
3626 return 0;
3627 spec->automute_speaker = 1;
3628 spec->automute_lo = 0;
3629 } else if (spec->automute_lo_possible) {
3630 if (spec->automute_lo)
3631 return 0;
3632 spec->automute_lo = 1;
3633 } else
3634 return -EINVAL;
3635 break;
3636 case 2:
3637 if (!spec->automute_lo_possible || !spec->automute_speaker_possible)
3638 return -EINVAL;
3639 if (spec->automute_speaker && spec->automute_lo)
3640 return 0;
3641 spec->automute_speaker = 1;
3642 spec->automute_lo = 1;
3643 break;
3644 default:
3645 return -EINVAL;
3646 }
3647 call_update_outputs(codec);
3648 return 1;
3649}
3650
3651static const struct snd_kcontrol_new automute_mode_enum = {
3652 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3653 .name = "Auto-Mute Mode",
3654 .info = automute_mode_info,
3655 .get = automute_mode_get,
3656 .put = automute_mode_put,
3657};
3658
3659static int add_automute_mode_enum(struct hda_codec *codec)
3660{
3661 struct hda_gen_spec *spec = codec->spec;
3662
Takashi Iwai12c93df2012-12-19 14:38:33 +01003663 if (!snd_hda_gen_add_kctl(spec, NULL, &automute_mode_enum))
Takashi Iwai352f7f92012-12-19 12:52:06 +01003664 return -ENOMEM;
3665 return 0;
3666}
3667
3668/*
3669 * Check the availability of HP/line-out auto-mute;
3670 * Set up appropriately if really supported
3671 */
3672static int check_auto_mute_availability(struct hda_codec *codec)
3673{
3674 struct hda_gen_spec *spec = codec->spec;
3675 struct auto_pin_cfg *cfg = &spec->autocfg;
3676 int present = 0;
3677 int i, err;
3678
Takashi Iwaif72706b2013-01-16 18:20:07 +01003679 if (spec->suppress_auto_mute)
3680 return 0;
3681
Takashi Iwai352f7f92012-12-19 12:52:06 +01003682 if (cfg->hp_pins[0])
3683 present++;
3684 if (cfg->line_out_pins[0])
3685 present++;
3686 if (cfg->speaker_pins[0])
3687 present++;
3688 if (present < 2) /* need two different output types */
Takashi Iwai071c73a2006-08-23 18:34:06 +02003689 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003690
3691 if (!cfg->speaker_pins[0] &&
3692 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
3693 memcpy(cfg->speaker_pins, cfg->line_out_pins,
3694 sizeof(cfg->speaker_pins));
3695 cfg->speaker_outs = cfg->line_outs;
Takashi Iwai071c73a2006-08-23 18:34:06 +02003696 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003697
Takashi Iwai352f7f92012-12-19 12:52:06 +01003698 if (!cfg->hp_pins[0] &&
3699 cfg->line_out_type == AUTO_PIN_HP_OUT) {
3700 memcpy(cfg->hp_pins, cfg->line_out_pins,
3701 sizeof(cfg->hp_pins));
3702 cfg->hp_outs = cfg->line_outs;
3703 }
3704
3705 for (i = 0; i < cfg->hp_outs; i++) {
3706 hda_nid_t nid = cfg->hp_pins[i];
3707 if (!is_jack_detectable(codec, nid))
3708 continue;
3709 snd_printdd("hda-codec: Enable HP auto-muting on NID 0x%x\n",
3710 nid);
3711 snd_hda_jack_detect_enable_callback(codec, nid, HDA_GEN_HP_EVENT,
Takashi Iwai2e03e952013-01-03 15:55:06 +01003712 spec->hp_automute_hook ?
3713 spec->hp_automute_hook :
Takashi Iwai5d550e12012-12-19 15:16:44 +01003714 snd_hda_gen_hp_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003715 spec->detect_hp = 1;
3716 }
3717
3718 if (cfg->line_out_type == AUTO_PIN_LINE_OUT && cfg->line_outs) {
3719 if (cfg->speaker_outs)
3720 for (i = 0; i < cfg->line_outs; i++) {
3721 hda_nid_t nid = cfg->line_out_pins[i];
3722 if (!is_jack_detectable(codec, nid))
3723 continue;
3724 snd_printdd("hda-codec: Enable Line-Out auto-muting on NID 0x%x\n", nid);
3725 snd_hda_jack_detect_enable_callback(codec, nid,
3726 HDA_GEN_FRONT_EVENT,
Takashi Iwai2e03e952013-01-03 15:55:06 +01003727 spec->line_automute_hook ?
3728 spec->line_automute_hook :
Takashi Iwai5d550e12012-12-19 15:16:44 +01003729 snd_hda_gen_line_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003730 spec->detect_lo = 1;
3731 }
3732 spec->automute_lo_possible = spec->detect_hp;
3733 }
3734
3735 spec->automute_speaker_possible = cfg->speaker_outs &&
3736 (spec->detect_hp || spec->detect_lo);
3737
3738 spec->automute_lo = spec->automute_lo_possible;
3739 spec->automute_speaker = spec->automute_speaker_possible;
3740
3741 if (spec->automute_speaker_possible || spec->automute_lo_possible) {
3742 /* create a control for automute mode */
3743 err = add_automute_mode_enum(codec);
3744 if (err < 0)
3745 return err;
3746 }
3747 return 0;
3748}
3749
Takashi Iwai352f7f92012-12-19 12:52:06 +01003750/* check whether all auto-mic pins are valid; setup indices if OK */
3751static bool auto_mic_check_imux(struct hda_codec *codec)
3752{
3753 struct hda_gen_spec *spec = codec->spec;
3754 const struct hda_input_mux *imux;
3755 int i;
3756
3757 imux = &spec->input_mux;
3758 for (i = 0; i < spec->am_num_entries; i++) {
3759 spec->am_entry[i].idx =
3760 find_idx_in_nid_list(spec->am_entry[i].pin,
3761 spec->imux_pins, imux->num_items);
3762 if (spec->am_entry[i].idx < 0)
3763 return false; /* no corresponding imux */
3764 }
3765
3766 /* we don't need the jack detection for the first pin */
3767 for (i = 1; i < spec->am_num_entries; i++)
3768 snd_hda_jack_detect_enable_callback(codec,
3769 spec->am_entry[i].pin,
3770 HDA_GEN_MIC_EVENT,
Takashi Iwai2e03e952013-01-03 15:55:06 +01003771 spec->mic_autoswitch_hook ?
3772 spec->mic_autoswitch_hook :
Takashi Iwai5d550e12012-12-19 15:16:44 +01003773 snd_hda_gen_mic_autoswitch);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003774 return true;
3775}
3776
3777static int compare_attr(const void *ap, const void *bp)
3778{
3779 const struct automic_entry *a = ap;
3780 const struct automic_entry *b = bp;
3781 return (int)(a->attr - b->attr);
3782}
3783
3784/*
3785 * Check the availability of auto-mic switch;
3786 * Set up if really supported
3787 */
3788static int check_auto_mic_availability(struct hda_codec *codec)
3789{
3790 struct hda_gen_spec *spec = codec->spec;
3791 struct auto_pin_cfg *cfg = &spec->autocfg;
3792 unsigned int types;
3793 int i, num_pins;
3794
Takashi Iwaid12daf62013-01-07 16:32:11 +01003795 if (spec->suppress_auto_mic)
3796 return 0;
3797
Takashi Iwai352f7f92012-12-19 12:52:06 +01003798 types = 0;
3799 num_pins = 0;
3800 for (i = 0; i < cfg->num_inputs; i++) {
3801 hda_nid_t nid = cfg->inputs[i].pin;
3802 unsigned int attr;
3803 attr = snd_hda_codec_get_pincfg(codec, nid);
3804 attr = snd_hda_get_input_pin_attr(attr);
3805 if (types & (1 << attr))
3806 return 0; /* already occupied */
3807 switch (attr) {
3808 case INPUT_PIN_ATTR_INT:
3809 if (cfg->inputs[i].type != AUTO_PIN_MIC)
3810 return 0; /* invalid type */
3811 break;
3812 case INPUT_PIN_ATTR_UNUSED:
3813 return 0; /* invalid entry */
3814 default:
3815 if (cfg->inputs[i].type > AUTO_PIN_LINE_IN)
3816 return 0; /* invalid type */
3817 if (!spec->line_in_auto_switch &&
3818 cfg->inputs[i].type != AUTO_PIN_MIC)
3819 return 0; /* only mic is allowed */
3820 if (!is_jack_detectable(codec, nid))
3821 return 0; /* no unsol support */
3822 break;
3823 }
3824 if (num_pins >= MAX_AUTO_MIC_PINS)
3825 return 0;
3826 types |= (1 << attr);
3827 spec->am_entry[num_pins].pin = nid;
3828 spec->am_entry[num_pins].attr = attr;
3829 num_pins++;
3830 }
3831
3832 if (num_pins < 2)
3833 return 0;
3834
3835 spec->am_num_entries = num_pins;
3836 /* sort the am_entry in the order of attr so that the pin with a
3837 * higher attr will be selected when the jack is plugged.
3838 */
3839 sort(spec->am_entry, num_pins, sizeof(spec->am_entry[0]),
3840 compare_attr, NULL);
3841
3842 if (!auto_mic_check_imux(codec))
3843 return 0;
3844
3845 spec->auto_mic = 1;
3846 spec->num_adc_nids = 1;
3847 spec->cur_mux[0] = spec->am_entry[0].idx;
3848 snd_printdd("hda-codec: Enable auto-mic switch on NID 0x%x/0x%x/0x%x\n",
3849 spec->am_entry[0].pin,
3850 spec->am_entry[1].pin,
3851 spec->am_entry[2].pin);
3852
3853 return 0;
3854}
3855
3856
Takashi Iwai9eb413e2012-12-19 14:41:21 +01003857/*
3858 * Parse the given BIOS configuration and set up the hda_gen_spec
3859 *
3860 * return 1 if successful, 0 if the proper config is not found,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003861 * or a negative error code
3862 */
3863int snd_hda_gen_parse_auto_config(struct hda_codec *codec,
Takashi Iwai9eb413e2012-12-19 14:41:21 +01003864 struct auto_pin_cfg *cfg)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003865{
3866 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003867 int err;
3868
Takashi Iwai1c70a582013-01-11 17:48:22 +01003869 parse_user_hints(codec);
3870
Takashi Iwaie4a395e2013-01-23 17:00:31 +01003871 if (spec->mixer_nid && !spec->mixer_merge_nid)
3872 spec->mixer_merge_nid = spec->mixer_nid;
3873
Takashi Iwai9eb413e2012-12-19 14:41:21 +01003874 if (cfg != &spec->autocfg) {
3875 spec->autocfg = *cfg;
3876 cfg = &spec->autocfg;
3877 }
3878
David Henningsson6fc4cb92013-01-16 15:58:45 +01003879 fill_all_dac_nids(codec);
3880
Takashi Iwai352f7f92012-12-19 12:52:06 +01003881 if (!cfg->line_outs) {
3882 if (cfg->dig_outs || cfg->dig_in_pin) {
3883 spec->multiout.max_channels = 2;
3884 spec->no_analog = 1;
3885 goto dig_only;
3886 }
3887 return 0; /* can't find valid BIOS pin config */
3888 }
3889
3890 if (!spec->no_primary_hp &&
3891 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT &&
3892 cfg->line_outs <= cfg->hp_outs) {
3893 /* use HP as primary out */
3894 cfg->speaker_outs = cfg->line_outs;
3895 memcpy(cfg->speaker_pins, cfg->line_out_pins,
3896 sizeof(cfg->speaker_pins));
3897 cfg->line_outs = cfg->hp_outs;
3898 memcpy(cfg->line_out_pins, cfg->hp_pins, sizeof(cfg->hp_pins));
3899 cfg->hp_outs = 0;
3900 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
3901 cfg->line_out_type = AUTO_PIN_HP_OUT;
3902 }
3903
3904 err = parse_output_paths(codec);
3905 if (err < 0)
3906 return err;
3907 err = create_multi_channel_mode(codec);
3908 if (err < 0)
3909 return err;
3910 err = create_multi_out_ctls(codec, cfg);
3911 if (err < 0)
3912 return err;
3913 err = create_hp_out_ctls(codec);
3914 if (err < 0)
3915 return err;
3916 err = create_speaker_out_ctls(codec);
3917 if (err < 0)
3918 return err;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003919 err = create_indep_hp_ctls(codec);
3920 if (err < 0)
3921 return err;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01003922 err = create_loopback_mixing_ctl(codec);
3923 if (err < 0)
3924 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003925 err = create_shared_input(codec);
3926 if (err < 0)
3927 return err;
3928 err = create_input_ctls(codec);
Takashi Iwaid13bd412008-07-30 15:01:45 +02003929 if (err < 0)
Takashi Iwai071c73a2006-08-23 18:34:06 +02003930 return err;
3931
Takashi Iwaia07a9492013-01-07 16:44:06 +01003932 spec->const_channel_count = spec->ext_channel_count;
3933 /* check the multiple speaker and headphone pins */
3934 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
3935 spec->const_channel_count = max(spec->const_channel_count,
3936 cfg->speaker_outs * 2);
3937 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
3938 spec->const_channel_count = max(spec->const_channel_count,
3939 cfg->hp_outs * 2);
3940 spec->multiout.max_channels = max(spec->ext_channel_count,
3941 spec->const_channel_count);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003942
3943 err = check_auto_mute_availability(codec);
3944 if (err < 0)
3945 return err;
3946
3947 err = check_dyn_adc_switch(codec);
3948 if (err < 0)
3949 return err;
3950
3951 if (!spec->shared_mic_hp) {
3952 err = check_auto_mic_availability(codec);
Takashi Iwaid13bd412008-07-30 15:01:45 +02003953 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003954 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003955 }
Takashi Iwai071c73a2006-08-23 18:34:06 +02003956
Takashi Iwai352f7f92012-12-19 12:52:06 +01003957 err = create_capture_mixers(codec);
3958 if (err < 0)
3959 return err;
3960
3961 err = parse_mic_boost(codec);
3962 if (err < 0)
3963 return err;
3964
Takashi Iwai978e77e2013-01-10 16:57:58 +01003965 if (spec->add_out_jack_modes) {
3966 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
3967 err = create_out_jack_modes(codec, cfg->line_outs,
3968 cfg->line_out_pins);
3969 if (err < 0)
3970 return err;
3971 }
3972 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
3973 err = create_out_jack_modes(codec, cfg->hp_outs,
3974 cfg->hp_pins);
3975 if (err < 0)
3976 return err;
3977 }
3978 }
3979
Takashi Iwai352f7f92012-12-19 12:52:06 +01003980 dig_only:
3981 parse_digital(codec);
3982
3983 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003984}
Takashi Iwai352f7f92012-12-19 12:52:06 +01003985EXPORT_SYMBOL_HDA(snd_hda_gen_parse_auto_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003986
3987
3988/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01003989 * Build control elements
Linus Torvalds1da177e2005-04-16 15:20:36 -07003990 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01003991
3992/* slave controls for virtual master */
3993static const char * const slave_pfxs[] = {
3994 "Front", "Surround", "Center", "LFE", "Side",
3995 "Headphone", "Speaker", "Mono", "Line Out",
3996 "CLFE", "Bass Speaker", "PCM",
Takashi Iwaiee79c692013-01-07 09:57:42 +01003997 "Speaker Front", "Speaker Surround", "Speaker CLFE", "Speaker Side",
3998 "Headphone Front", "Headphone Surround", "Headphone CLFE",
3999 "Headphone Side",
Takashi Iwai352f7f92012-12-19 12:52:06 +01004000 NULL,
4001};
4002
4003int snd_hda_gen_build_controls(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004004{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004005 struct hda_gen_spec *spec = codec->spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004006 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004007
Takashi Iwai36502d02012-12-19 15:15:10 +01004008 if (spec->kctls.used) {
4009 err = snd_hda_add_new_ctls(codec, spec->kctls.list);
4010 if (err < 0)
4011 return err;
4012 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004013
Takashi Iwai352f7f92012-12-19 12:52:06 +01004014 if (spec->multiout.dig_out_nid) {
4015 err = snd_hda_create_dig_out_ctls(codec,
4016 spec->multiout.dig_out_nid,
4017 spec->multiout.dig_out_nid,
4018 spec->pcm_rec[1].pcm_type);
4019 if (err < 0)
4020 return err;
4021 if (!spec->no_analog) {
4022 err = snd_hda_create_spdif_share_sw(codec,
4023 &spec->multiout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004024 if (err < 0)
4025 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004026 spec->multiout.share_spdif = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004027 }
4028 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01004029 if (spec->dig_in_nid) {
4030 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
4031 if (err < 0)
4032 return err;
4033 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004034
Takashi Iwai352f7f92012-12-19 12:52:06 +01004035 /* if we have no master control, let's create it */
4036 if (!spec->no_analog &&
4037 !snd_hda_find_mixer_ctl(codec, "Master Playback Volume")) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01004038 err = snd_hda_add_vmaster(codec, "Master Playback Volume",
Takashi Iwai7a71bbf2013-01-17 10:25:15 +01004039 spec->vmaster_tlv, slave_pfxs,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004040 "Playback Volume");
4041 if (err < 0)
4042 return err;
4043 }
4044 if (!spec->no_analog &&
4045 !snd_hda_find_mixer_ctl(codec, "Master Playback Switch")) {
4046 err = __snd_hda_add_vmaster(codec, "Master Playback Switch",
4047 NULL, slave_pfxs,
4048 "Playback Switch",
4049 true, &spec->vmaster_mute.sw_kctl);
4050 if (err < 0)
4051 return err;
4052 if (spec->vmaster_mute.hook)
Takashi Iwaifd25a972012-12-20 14:57:18 +01004053 snd_hda_add_vmaster_hook(codec, &spec->vmaster_mute,
4054 spec->vmaster_mute_enum);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004055 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004056
Takashi Iwai352f7f92012-12-19 12:52:06 +01004057 free_kctls(spec); /* no longer needed */
4058
4059 if (spec->shared_mic_hp) {
4060 int err;
4061 int nid = spec->autocfg.inputs[1].pin;
4062 err = snd_hda_jack_add_kctl(codec, nid, "Headphone Mic", 0);
4063 if (err < 0)
4064 return err;
4065 err = snd_hda_jack_detect_enable(codec, nid, 0);
4066 if (err < 0)
4067 return err;
4068 }
4069
4070 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
4071 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004072 return err;
4073
4074 return 0;
4075}
Takashi Iwai352f7f92012-12-19 12:52:06 +01004076EXPORT_SYMBOL_HDA(snd_hda_gen_build_controls);
4077
Linus Torvalds1da177e2005-04-16 15:20:36 -07004078
4079/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01004080 * PCM definitions
Linus Torvalds1da177e2005-04-16 15:20:36 -07004081 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004082
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004083static void call_pcm_playback_hook(struct hda_pcm_stream *hinfo,
4084 struct hda_codec *codec,
4085 struct snd_pcm_substream *substream,
4086 int action)
4087{
4088 struct hda_gen_spec *spec = codec->spec;
4089 if (spec->pcm_playback_hook)
4090 spec->pcm_playback_hook(hinfo, codec, substream, action);
4091}
4092
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004093static void call_pcm_capture_hook(struct hda_pcm_stream *hinfo,
4094 struct hda_codec *codec,
4095 struct snd_pcm_substream *substream,
4096 int action)
4097{
4098 struct hda_gen_spec *spec = codec->spec;
4099 if (spec->pcm_capture_hook)
4100 spec->pcm_capture_hook(hinfo, codec, substream, action);
4101}
4102
Takashi Iwai352f7f92012-12-19 12:52:06 +01004103/*
4104 * Analog playback callbacks
4105 */
4106static int playback_pcm_open(struct hda_pcm_stream *hinfo,
4107 struct hda_codec *codec,
4108 struct snd_pcm_substream *substream)
4109{
4110 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004111 int err;
4112
4113 mutex_lock(&spec->pcm_mutex);
4114 err = snd_hda_multi_out_analog_open(codec,
4115 &spec->multiout, substream,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004116 hinfo);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004117 if (!err) {
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004118 spec->active_streams |= 1 << STREAM_MULTI_OUT;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004119 call_pcm_playback_hook(hinfo, codec, substream,
4120 HDA_GEN_PCM_ACT_OPEN);
4121 }
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004122 mutex_unlock(&spec->pcm_mutex);
4123 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004124}
4125
4126static int playback_pcm_prepare(struct hda_pcm_stream *hinfo,
Takashi Iwai97ec5582006-03-21 11:29:07 +01004127 struct hda_codec *codec,
4128 unsigned int stream_tag,
4129 unsigned int format,
4130 struct snd_pcm_substream *substream)
4131{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004132 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004133 int err;
4134
4135 err = snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
4136 stream_tag, format, substream);
4137 if (!err)
4138 call_pcm_playback_hook(hinfo, codec, substream,
4139 HDA_GEN_PCM_ACT_PREPARE);
4140 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004141}
Takashi Iwai97ec5582006-03-21 11:29:07 +01004142
Takashi Iwai352f7f92012-12-19 12:52:06 +01004143static int playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
4144 struct hda_codec *codec,
4145 struct snd_pcm_substream *substream)
4146{
4147 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004148 int err;
4149
4150 err = snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
4151 if (!err)
4152 call_pcm_playback_hook(hinfo, codec, substream,
4153 HDA_GEN_PCM_ACT_CLEANUP);
4154 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004155}
4156
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004157static int playback_pcm_close(struct hda_pcm_stream *hinfo,
4158 struct hda_codec *codec,
4159 struct snd_pcm_substream *substream)
4160{
4161 struct hda_gen_spec *spec = codec->spec;
4162 mutex_lock(&spec->pcm_mutex);
4163 spec->active_streams &= ~(1 << STREAM_MULTI_OUT);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004164 call_pcm_playback_hook(hinfo, codec, substream,
4165 HDA_GEN_PCM_ACT_CLOSE);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004166 mutex_unlock(&spec->pcm_mutex);
4167 return 0;
4168}
4169
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004170static int capture_pcm_open(struct hda_pcm_stream *hinfo,
4171 struct hda_codec *codec,
4172 struct snd_pcm_substream *substream)
4173{
4174 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_OPEN);
4175 return 0;
4176}
4177
4178static int capture_pcm_prepare(struct hda_pcm_stream *hinfo,
4179 struct hda_codec *codec,
4180 unsigned int stream_tag,
4181 unsigned int format,
4182 struct snd_pcm_substream *substream)
4183{
4184 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4185 call_pcm_capture_hook(hinfo, codec, substream,
4186 HDA_GEN_PCM_ACT_PREPARE);
4187 return 0;
4188}
4189
4190static int capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
4191 struct hda_codec *codec,
4192 struct snd_pcm_substream *substream)
4193{
4194 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
4195 call_pcm_capture_hook(hinfo, codec, substream,
4196 HDA_GEN_PCM_ACT_CLEANUP);
4197 return 0;
4198}
4199
4200static int capture_pcm_close(struct hda_pcm_stream *hinfo,
4201 struct hda_codec *codec,
4202 struct snd_pcm_substream *substream)
4203{
4204 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_CLOSE);
4205 return 0;
4206}
4207
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004208static int alt_playback_pcm_open(struct hda_pcm_stream *hinfo,
4209 struct hda_codec *codec,
4210 struct snd_pcm_substream *substream)
4211{
4212 struct hda_gen_spec *spec = codec->spec;
4213 int err = 0;
4214
4215 mutex_lock(&spec->pcm_mutex);
4216 if (!spec->indep_hp_enabled)
4217 err = -EBUSY;
4218 else
4219 spec->active_streams |= 1 << STREAM_INDEP_HP;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004220 call_pcm_playback_hook(hinfo, codec, substream,
4221 HDA_GEN_PCM_ACT_OPEN);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004222 mutex_unlock(&spec->pcm_mutex);
4223 return err;
4224}
4225
4226static int alt_playback_pcm_close(struct hda_pcm_stream *hinfo,
4227 struct hda_codec *codec,
4228 struct snd_pcm_substream *substream)
4229{
4230 struct hda_gen_spec *spec = codec->spec;
4231 mutex_lock(&spec->pcm_mutex);
4232 spec->active_streams &= ~(1 << STREAM_INDEP_HP);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004233 call_pcm_playback_hook(hinfo, codec, substream,
4234 HDA_GEN_PCM_ACT_CLOSE);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004235 mutex_unlock(&spec->pcm_mutex);
4236 return 0;
4237}
4238
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004239static int alt_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
4240 struct hda_codec *codec,
4241 unsigned int stream_tag,
4242 unsigned int format,
4243 struct snd_pcm_substream *substream)
4244{
4245 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4246 call_pcm_playback_hook(hinfo, codec, substream,
4247 HDA_GEN_PCM_ACT_PREPARE);
4248 return 0;
4249}
4250
4251static int alt_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
4252 struct hda_codec *codec,
4253 struct snd_pcm_substream *substream)
4254{
4255 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
4256 call_pcm_playback_hook(hinfo, codec, substream,
4257 HDA_GEN_PCM_ACT_CLEANUP);
4258 return 0;
4259}
4260
Takashi Iwai352f7f92012-12-19 12:52:06 +01004261/*
4262 * Digital out
4263 */
4264static int dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
4265 struct hda_codec *codec,
4266 struct snd_pcm_substream *substream)
4267{
4268 struct hda_gen_spec *spec = codec->spec;
4269 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
4270}
4271
4272static int dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
4273 struct hda_codec *codec,
4274 unsigned int stream_tag,
4275 unsigned int format,
4276 struct snd_pcm_substream *substream)
4277{
4278 struct hda_gen_spec *spec = codec->spec;
4279 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
4280 stream_tag, format, substream);
4281}
4282
4283static int dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
4284 struct hda_codec *codec,
4285 struct snd_pcm_substream *substream)
4286{
4287 struct hda_gen_spec *spec = codec->spec;
4288 return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
4289}
4290
4291static int dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
4292 struct hda_codec *codec,
4293 struct snd_pcm_substream *substream)
4294{
4295 struct hda_gen_spec *spec = codec->spec;
4296 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
4297}
4298
4299/*
4300 * Analog capture
4301 */
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004302#define alt_capture_pcm_open capture_pcm_open
4303#define alt_capture_pcm_close capture_pcm_close
4304
Takashi Iwai352f7f92012-12-19 12:52:06 +01004305static int alt_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
4306 struct hda_codec *codec,
4307 unsigned int stream_tag,
4308 unsigned int format,
4309 struct snd_pcm_substream *substream)
4310{
4311 struct hda_gen_spec *spec = codec->spec;
4312
4313 snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number + 1],
Takashi Iwai97ec5582006-03-21 11:29:07 +01004314 stream_tag, 0, format);
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004315 call_pcm_capture_hook(hinfo, codec, substream,
4316 HDA_GEN_PCM_ACT_PREPARE);
Takashi Iwai97ec5582006-03-21 11:29:07 +01004317 return 0;
4318}
4319
Takashi Iwai352f7f92012-12-19 12:52:06 +01004320static int alt_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
4321 struct hda_codec *codec,
4322 struct snd_pcm_substream *substream)
Takashi Iwai97ec5582006-03-21 11:29:07 +01004323{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004324 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai97ec5582006-03-21 11:29:07 +01004325
Takashi Iwai352f7f92012-12-19 12:52:06 +01004326 snd_hda_codec_cleanup_stream(codec,
4327 spec->adc_nids[substream->number + 1]);
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004328 call_pcm_capture_hook(hinfo, codec, substream,
4329 HDA_GEN_PCM_ACT_CLEANUP);
Takashi Iwai97ec5582006-03-21 11:29:07 +01004330 return 0;
4331}
4332
Takashi Iwai352f7f92012-12-19 12:52:06 +01004333/*
4334 */
4335static const struct hda_pcm_stream pcm_analog_playback = {
4336 .substreams = 1,
4337 .channels_min = 2,
4338 .channels_max = 8,
4339 /* NID is set in build_pcms */
4340 .ops = {
4341 .open = playback_pcm_open,
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004342 .close = playback_pcm_close,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004343 .prepare = playback_pcm_prepare,
4344 .cleanup = playback_pcm_cleanup
4345 },
4346};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004347
Takashi Iwai352f7f92012-12-19 12:52:06 +01004348static const struct hda_pcm_stream pcm_analog_capture = {
4349 .substreams = 1,
4350 .channels_min = 2,
4351 .channels_max = 2,
4352 /* NID is set in build_pcms */
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004353 .ops = {
4354 .open = capture_pcm_open,
4355 .close = capture_pcm_close,
4356 .prepare = capture_pcm_prepare,
4357 .cleanup = capture_pcm_cleanup
4358 },
Takashi Iwai352f7f92012-12-19 12:52:06 +01004359};
4360
4361static const struct hda_pcm_stream pcm_analog_alt_playback = {
4362 .substreams = 1,
4363 .channels_min = 2,
4364 .channels_max = 2,
4365 /* NID is set in build_pcms */
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004366 .ops = {
4367 .open = alt_playback_pcm_open,
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004368 .close = alt_playback_pcm_close,
4369 .prepare = alt_playback_pcm_prepare,
4370 .cleanup = alt_playback_pcm_cleanup
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004371 },
Takashi Iwai352f7f92012-12-19 12:52:06 +01004372};
4373
4374static const struct hda_pcm_stream pcm_analog_alt_capture = {
4375 .substreams = 2, /* can be overridden */
4376 .channels_min = 2,
4377 .channels_max = 2,
4378 /* NID is set in build_pcms */
4379 .ops = {
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004380 .open = alt_capture_pcm_open,
4381 .close = alt_capture_pcm_close,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004382 .prepare = alt_capture_pcm_prepare,
4383 .cleanup = alt_capture_pcm_cleanup
4384 },
4385};
4386
4387static const struct hda_pcm_stream pcm_digital_playback = {
4388 .substreams = 1,
4389 .channels_min = 2,
4390 .channels_max = 2,
4391 /* NID is set in build_pcms */
4392 .ops = {
4393 .open = dig_playback_pcm_open,
4394 .close = dig_playback_pcm_close,
4395 .prepare = dig_playback_pcm_prepare,
4396 .cleanup = dig_playback_pcm_cleanup
4397 },
4398};
4399
4400static const struct hda_pcm_stream pcm_digital_capture = {
4401 .substreams = 1,
4402 .channels_min = 2,
4403 .channels_max = 2,
4404 /* NID is set in build_pcms */
4405};
4406
4407/* Used by build_pcms to flag that a PCM has no playback stream */
4408static const struct hda_pcm_stream pcm_null_stream = {
4409 .substreams = 0,
4410 .channels_min = 0,
4411 .channels_max = 0,
4412};
4413
4414/*
4415 * dynamic changing ADC PCM streams
4416 */
4417static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur)
4418{
4419 struct hda_gen_spec *spec = codec->spec;
4420 hda_nid_t new_adc = spec->adc_nids[spec->dyn_adc_idx[cur]];
4421
4422 if (spec->cur_adc && spec->cur_adc != new_adc) {
4423 /* stream is running, let's swap the current ADC */
4424 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
4425 spec->cur_adc = new_adc;
4426 snd_hda_codec_setup_stream(codec, new_adc,
4427 spec->cur_adc_stream_tag, 0,
4428 spec->cur_adc_format);
4429 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004430 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01004431 return false;
4432}
4433
4434/* analog capture with dynamic dual-adc changes */
4435static int dyn_adc_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
4436 struct hda_codec *codec,
4437 unsigned int stream_tag,
4438 unsigned int format,
4439 struct snd_pcm_substream *substream)
4440{
4441 struct hda_gen_spec *spec = codec->spec;
4442 spec->cur_adc = spec->adc_nids[spec->dyn_adc_idx[spec->cur_mux[0]]];
4443 spec->cur_adc_stream_tag = stream_tag;
4444 spec->cur_adc_format = format;
4445 snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
4446 return 0;
4447}
4448
4449static int dyn_adc_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
4450 struct hda_codec *codec,
4451 struct snd_pcm_substream *substream)
4452{
4453 struct hda_gen_spec *spec = codec->spec;
4454 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
4455 spec->cur_adc = 0;
4456 return 0;
4457}
4458
4459static const struct hda_pcm_stream dyn_adc_pcm_analog_capture = {
4460 .substreams = 1,
4461 .channels_min = 2,
4462 .channels_max = 2,
4463 .nid = 0, /* fill later */
4464 .ops = {
4465 .prepare = dyn_adc_capture_pcm_prepare,
4466 .cleanup = dyn_adc_capture_pcm_cleanup
4467 },
4468};
4469
Takashi Iwaif873e532012-12-20 16:58:39 +01004470static void fill_pcm_stream_name(char *str, size_t len, const char *sfx,
4471 const char *chip_name)
4472{
4473 char *p;
4474
4475 if (*str)
4476 return;
4477 strlcpy(str, chip_name, len);
4478
4479 /* drop non-alnum chars after a space */
4480 for (p = strchr(str, ' '); p; p = strchr(p + 1, ' ')) {
4481 if (!isalnum(p[1])) {
4482 *p = 0;
4483 break;
4484 }
4485 }
4486 strlcat(str, sfx, len);
4487}
4488
Takashi Iwai352f7f92012-12-19 12:52:06 +01004489/* build PCM streams based on the parsed results */
4490int snd_hda_gen_build_pcms(struct hda_codec *codec)
4491{
4492 struct hda_gen_spec *spec = codec->spec;
4493 struct hda_pcm *info = spec->pcm_rec;
4494 const struct hda_pcm_stream *p;
4495 bool have_multi_adcs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004496
4497 codec->num_pcms = 1;
4498 codec->pcm_info = info;
4499
Takashi Iwai352f7f92012-12-19 12:52:06 +01004500 if (spec->no_analog)
4501 goto skip_analog;
4502
Takashi Iwaif873e532012-12-20 16:58:39 +01004503 fill_pcm_stream_name(spec->stream_name_analog,
4504 sizeof(spec->stream_name_analog),
4505 " Analog", codec->chip_name);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004506 info->name = spec->stream_name_analog;
4507
4508 if (spec->multiout.num_dacs > 0) {
4509 p = spec->stream_analog_playback;
4510 if (!p)
4511 p = &pcm_analog_playback;
4512 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
4513 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dac_nids[0];
4514 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
4515 spec->multiout.max_channels;
4516 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT &&
4517 spec->autocfg.line_outs == 2)
4518 info->stream[SNDRV_PCM_STREAM_PLAYBACK].chmap =
4519 snd_pcm_2_1_chmaps;
4520 }
4521 if (spec->num_adc_nids) {
4522 p = spec->stream_analog_capture;
4523 if (!p) {
4524 if (spec->dyn_adc_switch)
4525 p = &dyn_adc_pcm_analog_capture;
4526 else
4527 p = &pcm_analog_capture;
4528 }
4529 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
4530 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adc_nids[0];
4531 }
4532
Takashi Iwai352f7f92012-12-19 12:52:06 +01004533 skip_analog:
4534 /* SPDIF for stream index #1 */
4535 if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
Takashi Iwaif873e532012-12-20 16:58:39 +01004536 fill_pcm_stream_name(spec->stream_name_digital,
4537 sizeof(spec->stream_name_digital),
4538 " Digital", codec->chip_name);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004539 codec->num_pcms = 2;
4540 codec->slave_dig_outs = spec->multiout.slave_dig_outs;
4541 info = spec->pcm_rec + 1;
4542 info->name = spec->stream_name_digital;
4543 if (spec->dig_out_type)
4544 info->pcm_type = spec->dig_out_type;
4545 else
4546 info->pcm_type = HDA_PCM_TYPE_SPDIF;
4547 if (spec->multiout.dig_out_nid) {
4548 p = spec->stream_digital_playback;
4549 if (!p)
4550 p = &pcm_digital_playback;
4551 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
4552 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dig_out_nid;
4553 }
4554 if (spec->dig_in_nid) {
4555 p = spec->stream_digital_capture;
4556 if (!p)
4557 p = &pcm_digital_capture;
4558 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
4559 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in_nid;
4560 }
4561 }
4562
4563 if (spec->no_analog)
4564 return 0;
4565
4566 /* If the use of more than one ADC is requested for the current
4567 * model, configure a second analog capture-only PCM.
4568 */
4569 have_multi_adcs = (spec->num_adc_nids > 1) &&
4570 !spec->dyn_adc_switch && !spec->auto_mic;
4571 /* Additional Analaog capture for index #2 */
4572 if (spec->alt_dac_nid || have_multi_adcs) {
Takashi Iwaia6071482013-01-21 16:50:09 +01004573 fill_pcm_stream_name(spec->stream_name_alt_analog,
4574 sizeof(spec->stream_name_alt_analog),
4575 " Alt Analog", codec->chip_name);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004576 codec->num_pcms = 3;
4577 info = spec->pcm_rec + 2;
Takashi Iwaia6071482013-01-21 16:50:09 +01004578 info->name = spec->stream_name_alt_analog;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004579 if (spec->alt_dac_nid) {
4580 p = spec->stream_analog_alt_playback;
4581 if (!p)
4582 p = &pcm_analog_alt_playback;
4583 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
4584 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
4585 spec->alt_dac_nid;
4586 } else {
4587 info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
4588 pcm_null_stream;
4589 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = 0;
4590 }
4591 if (have_multi_adcs) {
4592 p = spec->stream_analog_alt_capture;
4593 if (!p)
4594 p = &pcm_analog_alt_capture;
4595 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
4596 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
4597 spec->adc_nids[1];
4598 info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams =
4599 spec->num_adc_nids - 1;
4600 } else {
4601 info->stream[SNDRV_PCM_STREAM_CAPTURE] =
4602 pcm_null_stream;
4603 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = 0;
4604 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004605 }
4606
4607 return 0;
4608}
Takashi Iwai352f7f92012-12-19 12:52:06 +01004609EXPORT_SYMBOL_HDA(snd_hda_gen_build_pcms);
4610
4611
4612/*
4613 * Standard auto-parser initializations
4614 */
4615
Takashi Iwaid4156932013-01-07 10:08:02 +01004616/* configure the given path as a proper output */
Takashi Iwai2c12c302013-01-10 09:33:29 +01004617static void set_output_and_unmute(struct hda_codec *codec, int path_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004618{
4619 struct nid_path *path;
Takashi Iwaid4156932013-01-07 10:08:02 +01004620 hda_nid_t pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004621
Takashi Iwai196c17662013-01-04 15:01:40 +01004622 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwaid4156932013-01-07 10:08:02 +01004623 if (!path || !path->depth)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004624 return;
Takashi Iwaid4156932013-01-07 10:08:02 +01004625 pin = path->path[path->depth - 1];
Takashi Iwai2c12c302013-01-10 09:33:29 +01004626 restore_pin_ctl(codec, pin);
Takashi Iwaie1284af2013-01-03 16:33:02 +01004627 snd_hda_activate_path(codec, path, path->active, true);
4628 set_pin_eapd(codec, pin, path->active);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004629}
4630
4631/* initialize primary output paths */
4632static void init_multi_out(struct hda_codec *codec)
4633{
4634 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004635 int i;
4636
Takashi Iwaid4156932013-01-07 10:08:02 +01004637 for (i = 0; i < spec->autocfg.line_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01004638 set_output_and_unmute(codec, spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004639}
4640
Takashi Iwaidb23fd12012-12-20 15:27:24 +01004641
Takashi Iwai2c12c302013-01-10 09:33:29 +01004642static void __init_extra_out(struct hda_codec *codec, int num_outs, int *paths)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004643{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004644 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004645
Takashi Iwaid4156932013-01-07 10:08:02 +01004646 for (i = 0; i < num_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01004647 set_output_and_unmute(codec, paths[i]);
Takashi Iwaidb23fd12012-12-20 15:27:24 +01004648}
4649
4650/* initialize hp and speaker paths */
4651static void init_extra_out(struct hda_codec *codec)
4652{
4653 struct hda_gen_spec *spec = codec->spec;
4654
4655 if (spec->autocfg.line_out_type != AUTO_PIN_HP_OUT)
Takashi Iwai2c12c302013-01-10 09:33:29 +01004656 __init_extra_out(codec, spec->autocfg.hp_outs, spec->hp_paths);
Takashi Iwaidb23fd12012-12-20 15:27:24 +01004657 if (spec->autocfg.line_out_type != AUTO_PIN_SPEAKER_OUT)
4658 __init_extra_out(codec, spec->autocfg.speaker_outs,
Takashi Iwai2c12c302013-01-10 09:33:29 +01004659 spec->speaker_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004660}
4661
4662/* initialize multi-io paths */
4663static void init_multi_io(struct hda_codec *codec)
4664{
4665 struct hda_gen_spec *spec = codec->spec;
4666 int i;
4667
4668 for (i = 0; i < spec->multi_ios; i++) {
4669 hda_nid_t pin = spec->multi_io[i].pin;
4670 struct nid_path *path;
Takashi Iwai196c17662013-01-04 15:01:40 +01004671 path = get_multiio_path(codec, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004672 if (!path)
4673 continue;
4674 if (!spec->multi_io[i].ctl_in)
4675 spec->multi_io[i].ctl_in =
Takashi Iwai2c12c302013-01-10 09:33:29 +01004676 snd_hda_codec_get_pin_target(codec, pin);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004677 snd_hda_activate_path(codec, path, path->active, true);
4678 }
4679}
4680
Takashi Iwai352f7f92012-12-19 12:52:06 +01004681/* set up input pins and loopback paths */
4682static void init_analog_input(struct hda_codec *codec)
4683{
4684 struct hda_gen_spec *spec = codec->spec;
4685 struct auto_pin_cfg *cfg = &spec->autocfg;
4686 int i;
4687
4688 for (i = 0; i < cfg->num_inputs; i++) {
4689 hda_nid_t nid = cfg->inputs[i].pin;
4690 if (is_input_pin(codec, nid))
Takashi Iwai2c12c302013-01-10 09:33:29 +01004691 restore_pin_ctl(codec, nid);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004692
4693 /* init loopback inputs */
4694 if (spec->mixer_nid) {
Takashi Iwai3e367f12013-01-23 17:07:23 +01004695 resume_path_from_idx(codec, spec->loopback_paths[i]);
4696 resume_path_from_idx(codec, spec->loopback_merge_path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004697 }
4698 }
4699}
4700
4701/* initialize ADC paths */
4702static void init_input_src(struct hda_codec *codec)
4703{
4704 struct hda_gen_spec *spec = codec->spec;
4705 struct hda_input_mux *imux = &spec->input_mux;
4706 struct nid_path *path;
4707 int i, c, nums;
4708
4709 if (spec->dyn_adc_switch)
4710 nums = 1;
4711 else
4712 nums = spec->num_adc_nids;
4713
4714 for (c = 0; c < nums; c++) {
4715 for (i = 0; i < imux->num_items; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01004716 path = get_input_path(codec, c, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004717 if (path) {
4718 bool active = path->active;
4719 if (i == spec->cur_mux[c])
4720 active = true;
4721 snd_hda_activate_path(codec, path, active, false);
4722 }
4723 }
4724 }
4725
4726 if (spec->shared_mic_hp)
4727 update_shared_mic_hp(codec, spec->cur_mux[0]);
4728
4729 if (spec->cap_sync_hook)
Takashi Iwaia90229e2013-01-18 14:10:00 +01004730 spec->cap_sync_hook(codec, NULL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004731}
4732
4733/* set right pin controls for digital I/O */
4734static void init_digital(struct hda_codec *codec)
4735{
4736 struct hda_gen_spec *spec = codec->spec;
4737 int i;
4738 hda_nid_t pin;
4739
Takashi Iwaid4156932013-01-07 10:08:02 +01004740 for (i = 0; i < spec->autocfg.dig_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01004741 set_output_and_unmute(codec, spec->digout_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004742 pin = spec->autocfg.dig_in_pin;
Takashi Iwai2430d7b2013-01-04 15:09:42 +01004743 if (pin) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01004744 restore_pin_ctl(codec, pin);
Takashi Iwai3e367f12013-01-23 17:07:23 +01004745 resume_path_from_idx(codec, spec->digin_path);
Takashi Iwai2430d7b2013-01-04 15:09:42 +01004746 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01004747}
4748
Takashi Iwai973e4972012-12-20 15:16:09 +01004749/* clear unsol-event tags on unused pins; Conexant codecs seem to leave
4750 * invalid unsol tags by some reason
4751 */
4752static void clear_unsol_on_unused_pins(struct hda_codec *codec)
4753{
4754 int i;
4755
4756 for (i = 0; i < codec->init_pins.used; i++) {
4757 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
4758 hda_nid_t nid = pin->nid;
4759 if (is_jack_detectable(codec, nid) &&
4760 !snd_hda_jack_tbl_get(codec, nid))
4761 snd_hda_codec_update_cache(codec, nid, 0,
4762 AC_VERB_SET_UNSOLICITED_ENABLE, 0);
4763 }
4764}
4765
Takashi Iwai5187ac12013-01-07 12:52:16 +01004766/*
4767 * initialize the generic spec;
4768 * this can be put as patch_ops.init function
4769 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01004770int snd_hda_gen_init(struct hda_codec *codec)
4771{
4772 struct hda_gen_spec *spec = codec->spec;
4773
4774 if (spec->init_hook)
4775 spec->init_hook(codec);
4776
4777 snd_hda_apply_verbs(codec);
4778
Takashi Iwai3bbcd272012-12-20 11:50:58 +01004779 codec->cached_write = 1;
4780
Takashi Iwai352f7f92012-12-19 12:52:06 +01004781 init_multi_out(codec);
4782 init_extra_out(codec);
4783 init_multi_io(codec);
4784 init_analog_input(codec);
4785 init_input_src(codec);
4786 init_digital(codec);
4787
Takashi Iwai973e4972012-12-20 15:16:09 +01004788 clear_unsol_on_unused_pins(codec);
4789
Takashi Iwai352f7f92012-12-19 12:52:06 +01004790 /* call init functions of standard auto-mute helpers */
Takashi Iwaia5cc2502013-01-16 18:08:55 +01004791 update_automute_all(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004792
Takashi Iwaidc870f32013-01-22 15:24:30 +01004793 snd_hda_codec_flush_cache(codec);
Takashi Iwai3bbcd272012-12-20 11:50:58 +01004794
Takashi Iwai352f7f92012-12-19 12:52:06 +01004795 if (spec->vmaster_mute.sw_kctl && spec->vmaster_mute.hook)
4796 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
4797
4798 hda_call_check_power_status(codec, 0x01);
4799 return 0;
4800}
Takashi Iwaifce52a32013-01-07 12:42:48 +01004801EXPORT_SYMBOL_HDA(snd_hda_gen_init);
4802
Takashi Iwai5187ac12013-01-07 12:52:16 +01004803/*
4804 * free the generic spec;
4805 * this can be put as patch_ops.free function
4806 */
Takashi Iwaifce52a32013-01-07 12:42:48 +01004807void snd_hda_gen_free(struct hda_codec *codec)
4808{
4809 snd_hda_gen_spec_free(codec->spec);
4810 kfree(codec->spec);
4811 codec->spec = NULL;
4812}
4813EXPORT_SYMBOL_HDA(snd_hda_gen_free);
4814
4815#ifdef CONFIG_PM
Takashi Iwai5187ac12013-01-07 12:52:16 +01004816/*
4817 * check the loopback power save state;
4818 * this can be put as patch_ops.check_power_status function
4819 */
Takashi Iwaifce52a32013-01-07 12:42:48 +01004820int snd_hda_gen_check_power_status(struct hda_codec *codec, hda_nid_t nid)
4821{
4822 struct hda_gen_spec *spec = codec->spec;
4823 return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
4824}
4825EXPORT_SYMBOL_HDA(snd_hda_gen_check_power_status);
4826#endif
Takashi Iwai352f7f92012-12-19 12:52:06 +01004827
4828
4829/*
4830 * the generic codec support
4831 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004832
Takashi Iwai352f7f92012-12-19 12:52:06 +01004833static const struct hda_codec_ops generic_patch_ops = {
4834 .build_controls = snd_hda_gen_build_controls,
4835 .build_pcms = snd_hda_gen_build_pcms,
4836 .init = snd_hda_gen_init,
Takashi Iwaifce52a32013-01-07 12:42:48 +01004837 .free = snd_hda_gen_free,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004838 .unsol_event = snd_hda_jack_unsol_event,
Takashi Iwai83012a72012-08-24 18:38:08 +02004839#ifdef CONFIG_PM
Takashi Iwaifce52a32013-01-07 12:42:48 +01004840 .check_power_status = snd_hda_gen_check_power_status,
Takashi Iwaicb53c622007-08-10 17:21:45 +02004841#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004842};
4843
Linus Torvalds1da177e2005-04-16 15:20:36 -07004844int snd_hda_parse_generic_codec(struct hda_codec *codec)
4845{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004846 struct hda_gen_spec *spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004847 int err;
4848
Takashi Iwaie560d8d2005-09-09 14:21:46 +02004849 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004850 if (!spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004851 return -ENOMEM;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004852 snd_hda_gen_spec_init(spec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004853 codec->spec = spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004854
Takashi Iwai9eb413e2012-12-19 14:41:21 +01004855 err = snd_hda_parse_pin_defcfg(codec, &spec->autocfg, NULL, 0);
4856 if (err < 0)
4857 return err;
4858
4859 err = snd_hda_gen_parse_auto_config(codec, &spec->autocfg);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004860 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004861 goto error;
4862
4863 codec->patch_ops = generic_patch_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004864 return 0;
4865
Takashi Iwai352f7f92012-12-19 12:52:06 +01004866error:
Takashi Iwaifce52a32013-01-07 12:42:48 +01004867 snd_hda_gen_free(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004868 return err;
4869}
Takashi Iwaifce52a32013-01-07 12:42:48 +01004870EXPORT_SYMBOL_HDA(snd_hda_parse_generic_codec);