blob: ac079f93c5354ae576eca7745047883e4b0d051c [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 Iwai55196ff2013-01-24 17:32:56 +010027#include <linux/delay.h>
Takashi Iwaif873e532012-12-20 16:58:39 +010028#include <linux/ctype.h>
29#include <linux/string.h>
Takashi Iwai294765582013-01-17 09:52:11 +010030#include <linux/bitops.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <sound/core.h>
Takashi Iwai352f7f92012-12-19 12:52:06 +010032#include <sound/jack.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include "hda_codec.h"
34#include "hda_local.h"
Takashi Iwai352f7f92012-12-19 12:52:06 +010035#include "hda_auto_parser.h"
36#include "hda_jack.h"
Takashi Iwai7504b6c2013-03-18 11:25:51 +010037#include "hda_beep.h"
Takashi Iwai352f7f92012-12-19 12:52:06 +010038#include "hda_generic.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Takashi Iwai352f7f92012-12-19 12:52:06 +010041/* initialize hda_gen_spec struct */
42int snd_hda_gen_spec_init(struct hda_gen_spec *spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -070043{
Takashi Iwai352f7f92012-12-19 12:52:06 +010044 snd_array_init(&spec->kctls, sizeof(struct snd_kcontrol_new), 32);
Takashi Iwai352f7f92012-12-19 12:52:06 +010045 snd_array_init(&spec->paths, sizeof(struct nid_path), 8);
Takashi Iwai0186f4f2013-02-07 10:45:11 +010046 snd_array_init(&spec->loopback_list, sizeof(struct hda_amp_list), 8);
Takashi Iwai38cf6f12012-12-21 14:09:42 +010047 mutex_init(&spec->pcm_mutex);
Takashi Iwai352f7f92012-12-19 12:52:06 +010048 return 0;
49}
50EXPORT_SYMBOL_HDA(snd_hda_gen_spec_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Takashi Iwai12c93df2012-12-19 14:38:33 +010052struct snd_kcontrol_new *
53snd_hda_gen_add_kctl(struct hda_gen_spec *spec, const char *name,
54 const struct snd_kcontrol_new *temp)
Takashi Iwai352f7f92012-12-19 12:52:06 +010055{
56 struct snd_kcontrol_new *knew = snd_array_new(&spec->kctls);
57 if (!knew)
58 return NULL;
59 *knew = *temp;
60 if (name)
61 knew->name = kstrdup(name, GFP_KERNEL);
62 else if (knew->name)
63 knew->name = kstrdup(knew->name, GFP_KERNEL);
64 if (!knew->name)
65 return NULL;
66 return knew;
67}
Takashi Iwai12c93df2012-12-19 14:38:33 +010068EXPORT_SYMBOL_HDA(snd_hda_gen_add_kctl);
Takashi Iwai352f7f92012-12-19 12:52:06 +010069
70static void free_kctls(struct hda_gen_spec *spec)
71{
72 if (spec->kctls.list) {
73 struct snd_kcontrol_new *kctl = spec->kctls.list;
74 int i;
75 for (i = 0; i < spec->kctls.used; i++)
76 kfree(kctl[i].name);
77 }
78 snd_array_free(&spec->kctls);
79}
80
Takashi Iwai352f7f92012-12-19 12:52:06 +010081void snd_hda_gen_spec_free(struct hda_gen_spec *spec)
82{
83 if (!spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 return;
Takashi Iwai352f7f92012-12-19 12:52:06 +010085 free_kctls(spec);
Takashi Iwai352f7f92012-12-19 12:52:06 +010086 snd_array_free(&spec->paths);
Takashi Iwai0186f4f2013-02-07 10:45:11 +010087 snd_array_free(&spec->loopback_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088}
Takashi Iwai352f7f92012-12-19 12:52:06 +010089EXPORT_SYMBOL_HDA(snd_hda_gen_spec_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
91/*
Takashi Iwai1c70a582013-01-11 17:48:22 +010092 * store user hints
93 */
94static void parse_user_hints(struct hda_codec *codec)
95{
96 struct hda_gen_spec *spec = codec->spec;
97 int val;
98
99 val = snd_hda_get_bool_hint(codec, "jack_detect");
100 if (val >= 0)
101 codec->no_jack_detect = !val;
102 val = snd_hda_get_bool_hint(codec, "inv_jack_detect");
103 if (val >= 0)
104 codec->inv_jack_detect = !!val;
105 val = snd_hda_get_bool_hint(codec, "trigger_sense");
106 if (val >= 0)
107 codec->no_trigger_sense = !val;
108 val = snd_hda_get_bool_hint(codec, "inv_eapd");
109 if (val >= 0)
110 codec->inv_eapd = !!val;
111 val = snd_hda_get_bool_hint(codec, "pcm_format_first");
112 if (val >= 0)
113 codec->pcm_format_first = !!val;
114 val = snd_hda_get_bool_hint(codec, "sticky_stream");
115 if (val >= 0)
116 codec->no_sticky_stream = !val;
117 val = snd_hda_get_bool_hint(codec, "spdif_status_reset");
118 if (val >= 0)
119 codec->spdif_status_reset = !!val;
120 val = snd_hda_get_bool_hint(codec, "pin_amp_workaround");
121 if (val >= 0)
122 codec->pin_amp_workaround = !!val;
123 val = snd_hda_get_bool_hint(codec, "single_adc_amp");
124 if (val >= 0)
125 codec->single_adc_amp = !!val;
126
Takashi Iwaif72706b2013-01-16 18:20:07 +0100127 val = snd_hda_get_bool_hint(codec, "auto_mute");
128 if (val >= 0)
129 spec->suppress_auto_mute = !val;
Takashi Iwai1c70a582013-01-11 17:48:22 +0100130 val = snd_hda_get_bool_hint(codec, "auto_mic");
131 if (val >= 0)
132 spec->suppress_auto_mic = !val;
133 val = snd_hda_get_bool_hint(codec, "line_in_auto_switch");
134 if (val >= 0)
135 spec->line_in_auto_switch = !!val;
136 val = snd_hda_get_bool_hint(codec, "need_dac_fix");
137 if (val >= 0)
138 spec->need_dac_fix = !!val;
139 val = snd_hda_get_bool_hint(codec, "primary_hp");
140 if (val >= 0)
141 spec->no_primary_hp = !val;
142 val = snd_hda_get_bool_hint(codec, "multi_cap_vol");
143 if (val >= 0)
144 spec->multi_cap_vol = !!val;
145 val = snd_hda_get_bool_hint(codec, "inv_dmic_split");
146 if (val >= 0)
147 spec->inv_dmic_split = !!val;
148 val = snd_hda_get_bool_hint(codec, "indep_hp");
149 if (val >= 0)
150 spec->indep_hp = !!val;
151 val = snd_hda_get_bool_hint(codec, "add_stereo_mix_input");
152 if (val >= 0)
153 spec->add_stereo_mix_input = !!val;
Takashi Iwaif811c3c2013-03-07 18:32:59 +0100154 /* the following two are just for compatibility */
Takashi Iwai1c70a582013-01-11 17:48:22 +0100155 val = snd_hda_get_bool_hint(codec, "add_out_jack_modes");
156 if (val >= 0)
Takashi Iwaif811c3c2013-03-07 18:32:59 +0100157 spec->add_jack_modes = !!val;
Takashi Iwai294765582013-01-17 09:52:11 +0100158 val = snd_hda_get_bool_hint(codec, "add_in_jack_modes");
159 if (val >= 0)
Takashi Iwaif811c3c2013-03-07 18:32:59 +0100160 spec->add_jack_modes = !!val;
161 val = snd_hda_get_bool_hint(codec, "add_jack_modes");
162 if (val >= 0)
163 spec->add_jack_modes = !!val;
Takashi Iwai55196ff2013-01-24 17:32:56 +0100164 val = snd_hda_get_bool_hint(codec, "power_down_unused");
165 if (val >= 0)
166 spec->power_down_unused = !!val;
Takashi Iwai967303d2013-02-19 17:12:42 +0100167 val = snd_hda_get_bool_hint(codec, "add_hp_mic");
168 if (val >= 0)
169 spec->hp_mic = !!val;
170 val = snd_hda_get_bool_hint(codec, "hp_mic_detect");
171 if (val >= 0)
172 spec->suppress_hp_mic_detect = !val;
Takashi Iwai1c70a582013-01-11 17:48:22 +0100173
174 if (!snd_hda_get_int_hint(codec, "mixer_nid", &val))
175 spec->mixer_nid = val;
176}
177
178/*
Takashi Iwai2c12c302013-01-10 09:33:29 +0100179 * pin control value accesses
180 */
181
182#define update_pin_ctl(codec, pin, val) \
183 snd_hda_codec_update_cache(codec, pin, 0, \
184 AC_VERB_SET_PIN_WIDGET_CONTROL, val)
185
186/* restore the pinctl based on the cached value */
187static inline void restore_pin_ctl(struct hda_codec *codec, hda_nid_t pin)
188{
189 update_pin_ctl(codec, pin, snd_hda_codec_get_pin_target(codec, pin));
190}
191
192/* set the pinctl target value and write it if requested */
193static void set_pin_target(struct hda_codec *codec, hda_nid_t pin,
194 unsigned int val, bool do_write)
195{
196 if (!pin)
197 return;
198 val = snd_hda_correct_pin_ctl(codec, pin, val);
199 snd_hda_codec_set_pin_target(codec, pin, val);
200 if (do_write)
201 update_pin_ctl(codec, pin, val);
202}
203
204/* set pinctl target values for all given pins */
205static void set_pin_targets(struct hda_codec *codec, int num_pins,
206 hda_nid_t *pins, unsigned int val)
207{
208 int i;
209 for (i = 0; i < num_pins; i++)
210 set_pin_target(codec, pins[i], val, false);
211}
212
213/*
Takashi Iwai352f7f92012-12-19 12:52:06 +0100214 * parsing paths
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100217/* return the position of NID in the list, or -1 if not found */
218static int find_idx_in_nid_list(hda_nid_t nid, const hda_nid_t *list, int nums)
219{
220 int i;
221 for (i = 0; i < nums; i++)
222 if (list[i] == nid)
223 return i;
224 return -1;
225}
226
227/* return true if the given NID is contained in the path */
228static bool is_nid_contained(struct nid_path *path, hda_nid_t nid)
229{
230 return find_idx_in_nid_list(nid, path->path, path->depth) >= 0;
231}
232
Takashi Iwaif5172a72013-01-04 13:19:55 +0100233static struct nid_path *get_nid_path(struct hda_codec *codec,
234 hda_nid_t from_nid, hda_nid_t to_nid,
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100235 int anchor_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100237 struct hda_gen_spec *spec = codec->spec;
238 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
Takashi Iwai352f7f92012-12-19 12:52:06 +0100240 for (i = 0; i < spec->paths.used; i++) {
241 struct nid_path *path = snd_array_elem(&spec->paths, i);
242 if (path->depth <= 0)
243 continue;
244 if ((!from_nid || path->path[0] == from_nid) &&
Takashi Iwaif5172a72013-01-04 13:19:55 +0100245 (!to_nid || path->path[path->depth - 1] == to_nid)) {
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100246 if (!anchor_nid ||
247 (anchor_nid > 0 && is_nid_contained(path, anchor_nid)) ||
248 (anchor_nid < 0 && !is_nid_contained(path, anchor_nid)))
Takashi Iwaif5172a72013-01-04 13:19:55 +0100249 return path;
250 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 }
252 return NULL;
253}
Takashi Iwaif5172a72013-01-04 13:19:55 +0100254
255/* get the path between the given NIDs;
256 * passing 0 to either @pin or @dac behaves as a wildcard
257 */
258struct nid_path *snd_hda_get_nid_path(struct hda_codec *codec,
259 hda_nid_t from_nid, hda_nid_t to_nid)
260{
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100261 return get_nid_path(codec, from_nid, to_nid, 0);
Takashi Iwaif5172a72013-01-04 13:19:55 +0100262}
Takashi Iwai352f7f92012-12-19 12:52:06 +0100263EXPORT_SYMBOL_HDA(snd_hda_get_nid_path);
264
Takashi Iwai196c17662013-01-04 15:01:40 +0100265/* get the index number corresponding to the path instance;
266 * the index starts from 1, for easier checking the invalid value
267 */
268int snd_hda_get_path_idx(struct hda_codec *codec, struct nid_path *path)
269{
270 struct hda_gen_spec *spec = codec->spec;
271 struct nid_path *array = spec->paths.list;
272 ssize_t idx;
273
274 if (!spec->paths.used)
275 return 0;
276 idx = path - array;
277 if (idx < 0 || idx >= spec->paths.used)
278 return 0;
279 return idx + 1;
280}
Takashi Iwai4bd01e92013-01-22 15:17:20 +0100281EXPORT_SYMBOL_HDA(snd_hda_get_path_idx);
Takashi Iwai196c17662013-01-04 15:01:40 +0100282
283/* get the path instance corresponding to the given index number */
284struct nid_path *snd_hda_get_path_from_idx(struct hda_codec *codec, int idx)
285{
286 struct hda_gen_spec *spec = codec->spec;
287
288 if (idx <= 0 || idx > spec->paths.used)
289 return NULL;
290 return snd_array_elem(&spec->paths, idx - 1);
291}
Takashi Iwai4bd01e92013-01-22 15:17:20 +0100292EXPORT_SYMBOL_HDA(snd_hda_get_path_from_idx);
Takashi Iwai196c17662013-01-04 15:01:40 +0100293
Takashi Iwai352f7f92012-12-19 12:52:06 +0100294/* check whether the given DAC is already found in any existing paths */
295static bool is_dac_already_used(struct hda_codec *codec, hda_nid_t nid)
296{
297 struct hda_gen_spec *spec = codec->spec;
298 int i;
299
300 for (i = 0; i < spec->paths.used; i++) {
301 struct nid_path *path = snd_array_elem(&spec->paths, i);
302 if (path->path[0] == nid)
303 return true;
304 }
305 return false;
306}
307
308/* check whether the given two widgets can be connected */
309static bool is_reachable_path(struct hda_codec *codec,
310 hda_nid_t from_nid, hda_nid_t to_nid)
311{
312 if (!from_nid || !to_nid)
313 return false;
314 return snd_hda_get_conn_index(codec, to_nid, from_nid, true) >= 0;
315}
316
317/* nid, dir and idx */
318#define AMP_VAL_COMPARE_MASK (0xffff | (1U << 18) | (0x0f << 19))
319
320/* check whether the given ctl is already assigned in any path elements */
321static bool is_ctl_used(struct hda_codec *codec, unsigned int val, int type)
322{
323 struct hda_gen_spec *spec = codec->spec;
324 int i;
325
326 val &= AMP_VAL_COMPARE_MASK;
327 for (i = 0; i < spec->paths.used; i++) {
328 struct nid_path *path = snd_array_elem(&spec->paths, i);
329 if ((path->ctls[type] & AMP_VAL_COMPARE_MASK) == val)
330 return true;
331 }
332 return false;
333}
334
335/* check whether a control with the given (nid, dir, idx) was assigned */
336static bool is_ctl_associated(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwai8999bf02013-01-18 11:01:33 +0100337 int dir, int idx, int type)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100338{
339 unsigned int val = HDA_COMPOSE_AMP_VAL(nid, 3, idx, dir);
Takashi Iwai8999bf02013-01-18 11:01:33 +0100340 return is_ctl_used(codec, val, type);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100341}
342
Takashi Iwai0c8c0f52012-12-20 17:54:22 +0100343static void print_nid_path(const char *pfx, struct nid_path *path)
344{
345 char buf[40];
346 int i;
347
348
349 buf[0] = 0;
350 for (i = 0; i < path->depth; i++) {
351 char tmp[4];
352 sprintf(tmp, ":%02x", path->path[i]);
353 strlcat(buf, tmp, sizeof(buf));
354 }
355 snd_printdd("%s path: depth=%d %s\n", pfx, path->depth, buf);
356}
357
Takashi Iwai352f7f92012-12-19 12:52:06 +0100358/* called recursively */
359static bool __parse_nid_path(struct hda_codec *codec,
360 hda_nid_t from_nid, hda_nid_t to_nid,
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100361 int anchor_nid, struct nid_path *path,
362 int depth)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100363{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100364 const hda_nid_t *conn;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100365 int i, nums;
366
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100367 if (to_nid == anchor_nid)
368 anchor_nid = 0; /* anchor passed */
369 else if (to_nid == (hda_nid_t)(-anchor_nid))
370 return false; /* hit the exclusive nid */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100371
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100372 nums = snd_hda_get_conn_list(codec, to_nid, &conn);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100373 for (i = 0; i < nums; i++) {
374 if (conn[i] != from_nid) {
375 /* special case: when from_nid is 0,
376 * try to find an empty DAC
377 */
378 if (from_nid ||
379 get_wcaps_type(get_wcaps(codec, conn[i])) != AC_WID_AUD_OUT ||
380 is_dac_already_used(codec, conn[i]))
381 continue;
382 }
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100383 /* anchor is not requested or already passed? */
384 if (anchor_nid <= 0)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100385 goto found;
386 }
387 if (depth >= MAX_NID_PATH_DEPTH)
388 return false;
389 for (i = 0; i < nums; i++) {
390 unsigned int type;
391 type = get_wcaps_type(get_wcaps(codec, conn[i]));
392 if (type == AC_WID_AUD_OUT || type == AC_WID_AUD_IN ||
393 type == AC_WID_PIN)
394 continue;
395 if (__parse_nid_path(codec, from_nid, conn[i],
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100396 anchor_nid, path, depth + 1))
Takashi Iwai352f7f92012-12-19 12:52:06 +0100397 goto found;
398 }
399 return false;
400
401 found:
402 path->path[path->depth] = conn[i];
403 path->idx[path->depth + 1] = i;
404 if (nums > 1 && get_wcaps_type(get_wcaps(codec, to_nid)) != AC_WID_AUD_MIX)
405 path->multi[path->depth + 1] = 1;
406 path->depth++;
407 return true;
408}
409
410/* parse the widget path from the given nid to the target nid;
411 * when @from_nid is 0, try to find an empty DAC;
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100412 * when @anchor_nid is set to a positive value, only paths through the widget
413 * with the given value are evaluated.
414 * when @anchor_nid is set to a negative value, paths through the widget
415 * with the negative of given value are excluded, only other paths are chosen.
416 * when @anchor_nid is zero, no special handling about path selection.
Takashi Iwai352f7f92012-12-19 12:52:06 +0100417 */
418bool snd_hda_parse_nid_path(struct hda_codec *codec, hda_nid_t from_nid,
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100419 hda_nid_t to_nid, int anchor_nid,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100420 struct nid_path *path)
421{
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100422 if (__parse_nid_path(codec, from_nid, to_nid, anchor_nid, path, 1)) {
Takashi Iwai352f7f92012-12-19 12:52:06 +0100423 path->path[path->depth] = to_nid;
424 path->depth++;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100425 return true;
426 }
427 return false;
428}
429EXPORT_SYMBOL_HDA(snd_hda_parse_nid_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
431/*
Takashi Iwai352f7f92012-12-19 12:52:06 +0100432 * parse the path between the given NIDs and add to the path list.
433 * if no valid path is found, return NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100435struct nid_path *
436snd_hda_add_new_path(struct hda_codec *codec, hda_nid_t from_nid,
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100437 hda_nid_t to_nid, int anchor_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100439 struct hda_gen_spec *spec = codec->spec;
440 struct nid_path *path;
441
442 if (from_nid && to_nid && !is_reachable_path(codec, from_nid, to_nid))
443 return NULL;
444
Takashi Iwaif5172a72013-01-04 13:19:55 +0100445 /* check whether the path has been already added */
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100446 path = get_nid_path(codec, from_nid, to_nid, anchor_nid);
Takashi Iwaif5172a72013-01-04 13:19:55 +0100447 if (path)
448 return path;
449
Takashi Iwai352f7f92012-12-19 12:52:06 +0100450 path = snd_array_new(&spec->paths);
451 if (!path)
452 return NULL;
453 memset(path, 0, sizeof(*path));
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100454 if (snd_hda_parse_nid_path(codec, from_nid, to_nid, anchor_nid, path))
Takashi Iwai352f7f92012-12-19 12:52:06 +0100455 return path;
456 /* push back */
457 spec->paths.used--;
458 return NULL;
459}
460EXPORT_SYMBOL_HDA(snd_hda_add_new_path);
461
Takashi Iwai980428c2013-01-09 09:28:20 +0100462/* clear the given path as invalid so that it won't be picked up later */
463static void invalidate_nid_path(struct hda_codec *codec, int idx)
464{
465 struct nid_path *path = snd_hda_get_path_from_idx(codec, idx);
466 if (!path)
467 return;
468 memset(path, 0, sizeof(*path));
469}
470
Takashi Iwai352f7f92012-12-19 12:52:06 +0100471/* look for an empty DAC slot */
472static hda_nid_t look_for_dac(struct hda_codec *codec, hda_nid_t pin,
473 bool is_digital)
474{
475 struct hda_gen_spec *spec = codec->spec;
476 bool cap_digital;
477 int i;
478
479 for (i = 0; i < spec->num_all_dacs; i++) {
480 hda_nid_t nid = spec->all_dacs[i];
481 if (!nid || is_dac_already_used(codec, nid))
482 continue;
483 cap_digital = !!(get_wcaps(codec, nid) & AC_WCAP_DIGITAL);
484 if (is_digital != cap_digital)
485 continue;
486 if (is_reachable_path(codec, nid, pin))
487 return nid;
488 }
489 return 0;
490}
491
492/* replace the channels in the composed amp value with the given number */
493static unsigned int amp_val_replace_channels(unsigned int val, unsigned int chs)
494{
495 val &= ~(0x3U << 16);
496 val |= chs << 16;
497 return val;
498}
499
500/* check whether the widget has the given amp capability for the direction */
501static bool check_amp_caps(struct hda_codec *codec, hda_nid_t nid,
502 int dir, unsigned int bits)
503{
504 if (!nid)
505 return false;
506 if (get_wcaps(codec, nid) & (1 << (dir + 1)))
507 if (query_amp_caps(codec, nid, dir) & bits)
508 return true;
509 return false;
510}
511
David Henningsson99a55922013-01-16 15:58:44 +0100512static bool same_amp_caps(struct hda_codec *codec, hda_nid_t nid1,
513 hda_nid_t nid2, int dir)
514{
515 if (!(get_wcaps(codec, nid1) & (1 << (dir + 1))))
516 return !(get_wcaps(codec, nid2) & (1 << (dir + 1)));
517 return (query_amp_caps(codec, nid1, dir) ==
518 query_amp_caps(codec, nid2, dir));
519}
520
Takashi Iwai352f7f92012-12-19 12:52:06 +0100521#define nid_has_mute(codec, nid, dir) \
522 check_amp_caps(codec, nid, dir, AC_AMPCAP_MUTE)
523#define nid_has_volume(codec, nid, dir) \
524 check_amp_caps(codec, nid, dir, AC_AMPCAP_NUM_STEPS)
525
526/* look for a widget suitable for assigning a mute switch in the path */
527static hda_nid_t look_for_out_mute_nid(struct hda_codec *codec,
528 struct nid_path *path)
529{
530 int i;
531
532 for (i = path->depth - 1; i >= 0; i--) {
533 if (nid_has_mute(codec, path->path[i], HDA_OUTPUT))
534 return path->path[i];
535 if (i != path->depth - 1 && i != 0 &&
536 nid_has_mute(codec, path->path[i], HDA_INPUT))
537 return path->path[i];
538 }
539 return 0;
540}
541
542/* look for a widget suitable for assigning a volume ctl in the path */
543static hda_nid_t look_for_out_vol_nid(struct hda_codec *codec,
544 struct nid_path *path)
545{
546 int i;
547
548 for (i = path->depth - 1; i >= 0; i--) {
549 if (nid_has_volume(codec, path->path[i], HDA_OUTPUT))
550 return path->path[i];
551 }
Takashi Iwai82beb8f2007-08-10 17:09:26 +0200552 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553}
554
555/*
Takashi Iwai352f7f92012-12-19 12:52:06 +0100556 * path activation / deactivation
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100558
559/* can have the amp-in capability? */
560static bool has_amp_in(struct hda_codec *codec, struct nid_path *path, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100562 hda_nid_t nid = path->path[idx];
563 unsigned int caps = get_wcaps(codec, nid);
564 unsigned int type = get_wcaps_type(caps);
565
566 if (!(caps & AC_WCAP_IN_AMP))
567 return false;
568 if (type == AC_WID_PIN && idx > 0) /* only for input pins */
569 return false;
570 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571}
572
Takashi Iwai352f7f92012-12-19 12:52:06 +0100573/* can have the amp-out capability? */
574static bool has_amp_out(struct hda_codec *codec, struct nid_path *path, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100576 hda_nid_t nid = path->path[idx];
577 unsigned int caps = get_wcaps(codec, nid);
578 unsigned int type = get_wcaps_type(caps);
579
580 if (!(caps & AC_WCAP_OUT_AMP))
581 return false;
582 if (type == AC_WID_PIN && !idx) /* only for output pins */
583 return false;
584 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585}
586
Takashi Iwai352f7f92012-12-19 12:52:06 +0100587/* check whether the given (nid,dir,idx) is active */
588static bool is_active_nid(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwai7dddf2ae2013-01-24 16:31:35 +0100589 unsigned int dir, unsigned int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100591 struct hda_gen_spec *spec = codec->spec;
592 int i, n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
Takashi Iwai352f7f92012-12-19 12:52:06 +0100594 for (n = 0; n < spec->paths.used; n++) {
595 struct nid_path *path = snd_array_elem(&spec->paths, n);
596 if (!path->active)
597 continue;
598 for (i = 0; i < path->depth; i++) {
599 if (path->path[i] == nid) {
600 if (dir == HDA_OUTPUT || path->idx[i] == idx)
601 return true;
602 break;
603 }
604 }
605 }
606 return false;
607}
608
609/* get the default amp value for the target state */
610static int get_amp_val_to_activate(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwai8999bf02013-01-18 11:01:33 +0100611 int dir, unsigned int caps, bool enable)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100612{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100613 unsigned int val = 0;
614
Takashi Iwai352f7f92012-12-19 12:52:06 +0100615 if (caps & AC_AMPCAP_NUM_STEPS) {
616 /* set to 0dB */
617 if (enable)
618 val = (caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT;
619 }
620 if (caps & AC_AMPCAP_MUTE) {
621 if (!enable)
622 val |= HDA_AMP_MUTE;
623 }
624 return val;
625}
626
627/* initialize the amp value (only at the first time) */
628static void init_amp(struct hda_codec *codec, hda_nid_t nid, int dir, int idx)
629{
Takashi Iwai8999bf02013-01-18 11:01:33 +0100630 unsigned int caps = query_amp_caps(codec, nid, dir);
631 int val = get_amp_val_to_activate(codec, nid, dir, caps, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100632 snd_hda_codec_amp_init_stereo(codec, nid, dir, idx, 0xff, val);
633}
634
Takashi Iwai8999bf02013-01-18 11:01:33 +0100635/* calculate amp value mask we can modify;
636 * if the given amp is controlled by mixers, don't touch it
637 */
638static unsigned int get_amp_mask_to_modify(struct hda_codec *codec,
639 hda_nid_t nid, int dir, int idx,
640 unsigned int caps)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100641{
Takashi Iwai8999bf02013-01-18 11:01:33 +0100642 unsigned int mask = 0xff;
643
644 if (caps & AC_AMPCAP_MUTE) {
645 if (is_ctl_associated(codec, nid, dir, idx, NID_PATH_MUTE_CTL))
646 mask &= ~0x80;
647 }
648 if (caps & AC_AMPCAP_NUM_STEPS) {
649 if (is_ctl_associated(codec, nid, dir, idx, NID_PATH_VOL_CTL) ||
650 is_ctl_associated(codec, nid, dir, idx, NID_PATH_BOOST_CTL))
651 mask &= ~0x7f;
652 }
653 return mask;
654}
655
656static void activate_amp(struct hda_codec *codec, hda_nid_t nid, int dir,
657 int idx, int idx_to_check, bool enable)
658{
659 unsigned int caps;
660 unsigned int mask, val;
661
Takashi Iwai7dddf2ae2013-01-24 16:31:35 +0100662 if (!enable && is_active_nid(codec, nid, dir, idx_to_check))
Takashi Iwai352f7f92012-12-19 12:52:06 +0100663 return;
Takashi Iwai8999bf02013-01-18 11:01:33 +0100664
665 caps = query_amp_caps(codec, nid, dir);
666 val = get_amp_val_to_activate(codec, nid, dir, caps, enable);
667 mask = get_amp_mask_to_modify(codec, nid, dir, idx_to_check, caps);
668 if (!mask)
669 return;
670
671 val &= mask;
672 snd_hda_codec_amp_stereo(codec, nid, dir, idx, mask, val);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100673}
674
675static void activate_amp_out(struct hda_codec *codec, struct nid_path *path,
676 int i, bool enable)
677{
678 hda_nid_t nid = path->path[i];
679 init_amp(codec, nid, HDA_OUTPUT, 0);
Takashi Iwai8999bf02013-01-18 11:01:33 +0100680 activate_amp(codec, nid, HDA_OUTPUT, 0, 0, enable);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100681}
682
683static void activate_amp_in(struct hda_codec *codec, struct nid_path *path,
684 int i, bool enable, bool add_aamix)
685{
686 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100687 const hda_nid_t *conn;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100688 int n, nums, idx;
689 int type;
690 hda_nid_t nid = path->path[i];
691
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100692 nums = snd_hda_get_conn_list(codec, nid, &conn);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100693 type = get_wcaps_type(get_wcaps(codec, nid));
694 if (type == AC_WID_PIN ||
695 (type == AC_WID_AUD_IN && codec->single_adc_amp)) {
696 nums = 1;
697 idx = 0;
698 } else
699 idx = path->idx[i];
700
701 for (n = 0; n < nums; n++)
702 init_amp(codec, nid, HDA_INPUT, n);
703
Takashi Iwai352f7f92012-12-19 12:52:06 +0100704 /* here is a little bit tricky in comparison with activate_amp_out();
705 * when aa-mixer is available, we need to enable the path as well
706 */
707 for (n = 0; n < nums; n++) {
Takashi Iwaie4a395e2013-01-23 17:00:31 +0100708 if (n != idx && (!add_aamix || conn[n] != spec->mixer_merge_nid))
Takashi Iwai352f7f92012-12-19 12:52:06 +0100709 continue;
Takashi Iwai8999bf02013-01-18 11:01:33 +0100710 activate_amp(codec, nid, HDA_INPUT, n, idx, enable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 }
712}
713
Takashi Iwai352f7f92012-12-19 12:52:06 +0100714/* activate or deactivate the given path
715 * if @add_aamix is set, enable the input from aa-mix NID as well (if any)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100717void snd_hda_activate_path(struct hda_codec *codec, struct nid_path *path,
718 bool enable, bool add_aamix)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719{
Takashi Iwai55196ff2013-01-24 17:32:56 +0100720 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100721 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
Takashi Iwai352f7f92012-12-19 12:52:06 +0100723 if (!enable)
724 path->active = false;
725
726 for (i = path->depth - 1; i >= 0; i--) {
Takashi Iwai55196ff2013-01-24 17:32:56 +0100727 hda_nid_t nid = path->path[i];
728 if (enable && spec->power_down_unused) {
729 /* make sure the widget is powered up */
730 if (!snd_hda_check_power_state(codec, nid, AC_PWRST_D0))
731 snd_hda_codec_write(codec, nid, 0,
732 AC_VERB_SET_POWER_STATE,
733 AC_PWRST_D0);
734 }
Takashi Iwai352f7f92012-12-19 12:52:06 +0100735 if (enable && path->multi[i])
Takashi Iwai55196ff2013-01-24 17:32:56 +0100736 snd_hda_codec_write_cache(codec, nid, 0,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100737 AC_VERB_SET_CONNECT_SEL,
738 path->idx[i]);
739 if (has_amp_in(codec, path, i))
740 activate_amp_in(codec, path, i, enable, add_aamix);
741 if (has_amp_out(codec, path, i))
742 activate_amp_out(codec, path, i, enable);
743 }
744
745 if (enable)
746 path->active = true;
747}
748EXPORT_SYMBOL_HDA(snd_hda_activate_path);
749
Takashi Iwai55196ff2013-01-24 17:32:56 +0100750/* if the given path is inactive, put widgets into D3 (only if suitable) */
751static void path_power_down_sync(struct hda_codec *codec, struct nid_path *path)
752{
753 struct hda_gen_spec *spec = codec->spec;
Jiri Slaby868211d2013-04-04 22:32:10 +0200754 bool changed = false;
Takashi Iwai55196ff2013-01-24 17:32:56 +0100755 int i;
756
757 if (!spec->power_down_unused || path->active)
758 return;
759
760 for (i = 0; i < path->depth; i++) {
761 hda_nid_t nid = path->path[i];
762 if (!snd_hda_check_power_state(codec, nid, AC_PWRST_D3)) {
763 snd_hda_codec_write(codec, nid, 0,
764 AC_VERB_SET_POWER_STATE,
765 AC_PWRST_D3);
766 changed = true;
767 }
768 }
769
770 if (changed) {
771 msleep(10);
772 snd_hda_codec_read(codec, path->path[0], 0,
773 AC_VERB_GET_POWER_STATE, 0);
774 }
775}
776
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +0100777/* turn on/off EAPD on the given pin */
778static void set_pin_eapd(struct hda_codec *codec, hda_nid_t pin, bool enable)
779{
780 struct hda_gen_spec *spec = codec->spec;
781 if (spec->own_eapd_ctl ||
782 !(snd_hda_query_pin_caps(codec, pin) & AC_PINCAP_EAPD))
783 return;
Takashi Iwaiecac3ed2012-12-21 15:23:01 +0100784 if (codec->inv_eapd)
785 enable = !enable;
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +0100786 snd_hda_codec_update_cache(codec, pin, 0,
787 AC_VERB_SET_EAPD_BTLENABLE,
788 enable ? 0x02 : 0x00);
789}
790
Takashi Iwai3e367f12013-01-23 17:07:23 +0100791/* re-initialize the path specified by the given path index */
792static void resume_path_from_idx(struct hda_codec *codec, int path_idx)
793{
794 struct nid_path *path = snd_hda_get_path_from_idx(codec, path_idx);
795 if (path)
796 snd_hda_activate_path(codec, path, path->active, false);
797}
798
Takashi Iwai352f7f92012-12-19 12:52:06 +0100799
800/*
801 * Helper functions for creating mixer ctl elements
802 */
803
804enum {
805 HDA_CTL_WIDGET_VOL,
806 HDA_CTL_WIDGET_MUTE,
807 HDA_CTL_BIND_MUTE,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100808};
809static const struct snd_kcontrol_new control_templates[] = {
810 HDA_CODEC_VOLUME(NULL, 0, 0, 0),
811 HDA_CODEC_MUTE(NULL, 0, 0, 0),
812 HDA_BIND_MUTE(NULL, 0, 0, 0),
Takashi Iwai352f7f92012-12-19 12:52:06 +0100813};
814
815/* add dynamic controls from template */
Takashi Iwaia35bd1e2013-01-18 14:01:14 +0100816static struct snd_kcontrol_new *
817add_control(struct hda_gen_spec *spec, int type, const char *name,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100818 int cidx, unsigned long val)
819{
820 struct snd_kcontrol_new *knew;
821
Takashi Iwai12c93df2012-12-19 14:38:33 +0100822 knew = snd_hda_gen_add_kctl(spec, name, &control_templates[type]);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100823 if (!knew)
Takashi Iwaia35bd1e2013-01-18 14:01:14 +0100824 return NULL;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100825 knew->index = cidx;
826 if (get_amp_nid_(val))
827 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
828 knew->private_value = val;
Takashi Iwaia35bd1e2013-01-18 14:01:14 +0100829 return knew;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100830}
831
832static int add_control_with_pfx(struct hda_gen_spec *spec, int type,
833 const char *pfx, const char *dir,
834 const char *sfx, int cidx, unsigned long val)
835{
836 char name[32];
837 snprintf(name, sizeof(name), "%s %s %s", pfx, dir, sfx);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +0100838 if (!add_control(spec, type, name, cidx, val))
839 return -ENOMEM;
840 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100841}
842
843#define add_pb_vol_ctrl(spec, type, pfx, val) \
844 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", 0, val)
845#define add_pb_sw_ctrl(spec, type, pfx, val) \
846 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", 0, val)
847#define __add_pb_vol_ctrl(spec, type, pfx, cidx, val) \
848 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", cidx, val)
849#define __add_pb_sw_ctrl(spec, type, pfx, cidx, val) \
850 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", cidx, val)
851
852static int add_vol_ctl(struct hda_codec *codec, const char *pfx, int cidx,
853 unsigned int chs, struct nid_path *path)
854{
855 unsigned int val;
856 if (!path)
857 return 0;
858 val = path->ctls[NID_PATH_VOL_CTL];
859 if (!val)
860 return 0;
861 val = amp_val_replace_channels(val, chs);
862 return __add_pb_vol_ctrl(codec->spec, HDA_CTL_WIDGET_VOL, pfx, cidx, val);
863}
864
865/* return the channel bits suitable for the given path->ctls[] */
866static int get_default_ch_nums(struct hda_codec *codec, struct nid_path *path,
867 int type)
868{
869 int chs = 1; /* mono (left only) */
870 if (path) {
871 hda_nid_t nid = get_amp_nid_(path->ctls[type]);
872 if (nid && (get_wcaps(codec, nid) & AC_WCAP_STEREO))
873 chs = 3; /* stereo */
874 }
875 return chs;
876}
877
878static int add_stereo_vol(struct hda_codec *codec, const char *pfx, int cidx,
879 struct nid_path *path)
880{
881 int chs = get_default_ch_nums(codec, path, NID_PATH_VOL_CTL);
882 return add_vol_ctl(codec, pfx, cidx, chs, path);
883}
884
885/* create a mute-switch for the given mixer widget;
886 * if it has multiple sources (e.g. DAC and loopback), create a bind-mute
887 */
888static int add_sw_ctl(struct hda_codec *codec, const char *pfx, int cidx,
889 unsigned int chs, struct nid_path *path)
890{
891 unsigned int val;
892 int type = HDA_CTL_WIDGET_MUTE;
893
894 if (!path)
895 return 0;
896 val = path->ctls[NID_PATH_MUTE_CTL];
897 if (!val)
898 return 0;
899 val = amp_val_replace_channels(val, chs);
900 if (get_amp_direction_(val) == HDA_INPUT) {
901 hda_nid_t nid = get_amp_nid_(val);
902 int nums = snd_hda_get_num_conns(codec, nid);
903 if (nums > 1) {
904 type = HDA_CTL_BIND_MUTE;
905 val |= nums << 19;
906 }
907 }
908 return __add_pb_sw_ctrl(codec->spec, type, pfx, cidx, val);
909}
910
911static int add_stereo_sw(struct hda_codec *codec, const char *pfx,
912 int cidx, struct nid_path *path)
913{
914 int chs = get_default_ch_nums(codec, path, NID_PATH_MUTE_CTL);
915 return add_sw_ctl(codec, pfx, cidx, chs, path);
916}
917
Takashi Iwai247d85e2013-01-17 16:18:11 +0100918/* any ctl assigned to the path with the given index? */
919static bool path_has_mixer(struct hda_codec *codec, int path_idx, int ctl_type)
920{
921 struct nid_path *path = snd_hda_get_path_from_idx(codec, path_idx);
922 return path && path->ctls[ctl_type];
923}
924
Takashi Iwai352f7f92012-12-19 12:52:06 +0100925static const char * const channel_name[4] = {
926 "Front", "Surround", "CLFE", "Side"
927};
928
929/* give some appropriate ctl name prefix for the given line out channel */
Takashi Iwai247d85e2013-01-17 16:18:11 +0100930static const char *get_line_out_pfx(struct hda_codec *codec, int ch,
931 int *index, int ctl_type)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100932{
Takashi Iwai247d85e2013-01-17 16:18:11 +0100933 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100934 struct auto_pin_cfg *cfg = &spec->autocfg;
935
936 *index = 0;
937 if (cfg->line_outs == 1 && !spec->multi_ios &&
Takashi Iwai247d85e2013-01-17 16:18:11 +0100938 !cfg->hp_outs && !cfg->speaker_outs)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100939 return spec->vmaster_mute.hook ? "PCM" : "Master";
940
941 /* if there is really a single DAC used in the whole output paths,
942 * use it master (or "PCM" if a vmaster hook is present)
943 */
944 if (spec->multiout.num_dacs == 1 && !spec->mixer_nid &&
945 !spec->multiout.hp_out_nid[0] && !spec->multiout.extra_out_nid[0])
946 return spec->vmaster_mute.hook ? "PCM" : "Master";
947
Takashi Iwai247d85e2013-01-17 16:18:11 +0100948 /* multi-io channels */
949 if (ch >= cfg->line_outs)
950 return channel_name[ch];
951
Takashi Iwai352f7f92012-12-19 12:52:06 +0100952 switch (cfg->line_out_type) {
953 case AUTO_PIN_SPEAKER_OUT:
Takashi Iwai247d85e2013-01-17 16:18:11 +0100954 /* if the primary channel vol/mute is shared with HP volume,
955 * don't name it as Speaker
956 */
957 if (!ch && cfg->hp_outs &&
958 !path_has_mixer(codec, spec->hp_paths[0], ctl_type))
959 break;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100960 if (cfg->line_outs == 1)
961 return "Speaker";
962 if (cfg->line_outs == 2)
963 return ch ? "Bass Speaker" : "Speaker";
964 break;
965 case AUTO_PIN_HP_OUT:
Takashi Iwai247d85e2013-01-17 16:18:11 +0100966 /* if the primary channel vol/mute is shared with spk volume,
967 * don't name it as Headphone
968 */
969 if (!ch && cfg->speaker_outs &&
970 !path_has_mixer(codec, spec->speaker_paths[0], ctl_type))
971 break;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100972 /* for multi-io case, only the primary out */
973 if (ch && spec->multi_ios)
974 break;
975 *index = ch;
976 return "Headphone";
Takashi Iwai352f7f92012-12-19 12:52:06 +0100977 }
Takashi Iwai247d85e2013-01-17 16:18:11 +0100978
979 /* for a single channel output, we don't have to name the channel */
980 if (cfg->line_outs == 1 && !spec->multi_ios)
981 return "PCM";
982
Takashi Iwai352f7f92012-12-19 12:52:06 +0100983 if (ch >= ARRAY_SIZE(channel_name)) {
984 snd_BUG();
985 return "PCM";
986 }
987
988 return channel_name[ch];
989}
990
991/*
992 * Parse output paths
993 */
994
995/* badness definition */
996enum {
997 /* No primary DAC is found for the main output */
998 BAD_NO_PRIMARY_DAC = 0x10000,
999 /* No DAC is found for the extra output */
1000 BAD_NO_DAC = 0x4000,
1001 /* No possible multi-ios */
Takashi Iwai1d739062013-02-13 14:17:32 +01001002 BAD_MULTI_IO = 0x120,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001003 /* No individual DAC for extra output */
1004 BAD_NO_EXTRA_DAC = 0x102,
1005 /* No individual DAC for extra surrounds */
1006 BAD_NO_EXTRA_SURR_DAC = 0x101,
1007 /* Primary DAC shared with main surrounds */
1008 BAD_SHARED_SURROUND = 0x100,
Takashi Iwai55a63d42013-03-21 17:20:12 +01001009 /* No independent HP possible */
Takashi Iwaibec8e682013-03-22 15:10:08 +01001010 BAD_NO_INDEP_HP = 0x10,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001011 /* Primary DAC shared with main CLFE */
1012 BAD_SHARED_CLFE = 0x10,
1013 /* Primary DAC shared with extra surrounds */
1014 BAD_SHARED_EXTRA_SURROUND = 0x10,
1015 /* Volume widget is shared */
1016 BAD_SHARED_VOL = 0x10,
1017};
1018
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001019/* look for widgets in the given path which are appropriate for
Takashi Iwai352f7f92012-12-19 12:52:06 +01001020 * volume and mute controls, and assign the values to ctls[].
1021 *
1022 * When no appropriate widget is found in the path, the badness value
1023 * is incremented depending on the situation. The function returns the
1024 * total badness for both volume and mute controls.
1025 */
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001026static int assign_out_path_ctls(struct hda_codec *codec, struct nid_path *path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001027{
Takashi Iwai352f7f92012-12-19 12:52:06 +01001028 hda_nid_t nid;
1029 unsigned int val;
1030 int badness = 0;
1031
1032 if (!path)
1033 return BAD_SHARED_VOL * 2;
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001034
1035 if (path->ctls[NID_PATH_VOL_CTL] ||
1036 path->ctls[NID_PATH_MUTE_CTL])
1037 return 0; /* already evaluated */
1038
Takashi Iwai352f7f92012-12-19 12:52:06 +01001039 nid = look_for_out_vol_nid(codec, path);
1040 if (nid) {
1041 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
1042 if (is_ctl_used(codec, val, NID_PATH_VOL_CTL))
1043 badness += BAD_SHARED_VOL;
1044 else
1045 path->ctls[NID_PATH_VOL_CTL] = val;
1046 } else
1047 badness += BAD_SHARED_VOL;
1048 nid = look_for_out_mute_nid(codec, path);
1049 if (nid) {
1050 unsigned int wid_type = get_wcaps_type(get_wcaps(codec, nid));
1051 if (wid_type == AC_WID_PIN || wid_type == AC_WID_AUD_OUT ||
1052 nid_has_mute(codec, nid, HDA_OUTPUT))
1053 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
1054 else
1055 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT);
1056 if (is_ctl_used(codec, val, NID_PATH_MUTE_CTL))
1057 badness += BAD_SHARED_VOL;
1058 else
1059 path->ctls[NID_PATH_MUTE_CTL] = val;
1060 } else
1061 badness += BAD_SHARED_VOL;
1062 return badness;
1063}
1064
Takashi Iwai98bd1112013-03-22 14:53:50 +01001065const struct badness_table hda_main_out_badness = {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001066 .no_primary_dac = BAD_NO_PRIMARY_DAC,
1067 .no_dac = BAD_NO_DAC,
1068 .shared_primary = BAD_NO_PRIMARY_DAC,
1069 .shared_surr = BAD_SHARED_SURROUND,
1070 .shared_clfe = BAD_SHARED_CLFE,
1071 .shared_surr_main = BAD_SHARED_SURROUND,
1072};
Takashi Iwai98bd1112013-03-22 14:53:50 +01001073EXPORT_SYMBOL_HDA(hda_main_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001074
Takashi Iwai98bd1112013-03-22 14:53:50 +01001075const struct badness_table hda_extra_out_badness = {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001076 .no_primary_dac = BAD_NO_DAC,
1077 .no_dac = BAD_NO_DAC,
1078 .shared_primary = BAD_NO_EXTRA_DAC,
1079 .shared_surr = BAD_SHARED_EXTRA_SURROUND,
1080 .shared_clfe = BAD_SHARED_EXTRA_SURROUND,
1081 .shared_surr_main = BAD_NO_EXTRA_SURR_DAC,
1082};
Takashi Iwai98bd1112013-03-22 14:53:50 +01001083EXPORT_SYMBOL_HDA(hda_extra_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001084
Takashi Iwai7385df62013-01-07 09:50:52 +01001085/* get the DAC of the primary output corresponding to the given array index */
1086static hda_nid_t get_primary_out(struct hda_codec *codec, int idx)
1087{
1088 struct hda_gen_spec *spec = codec->spec;
1089 struct auto_pin_cfg *cfg = &spec->autocfg;
1090
1091 if (cfg->line_outs > idx)
1092 return spec->private_dac_nids[idx];
1093 idx -= cfg->line_outs;
1094 if (spec->multi_ios > idx)
1095 return spec->multi_io[idx].dac;
1096 return 0;
1097}
1098
1099/* return the DAC if it's reachable, otherwise zero */
1100static inline hda_nid_t try_dac(struct hda_codec *codec,
1101 hda_nid_t dac, hda_nid_t pin)
1102{
1103 return is_reachable_path(codec, dac, pin) ? dac : 0;
1104}
1105
Takashi Iwai352f7f92012-12-19 12:52:06 +01001106/* try to assign DACs to pins and return the resultant badness */
1107static int try_assign_dacs(struct hda_codec *codec, int num_outs,
1108 const hda_nid_t *pins, hda_nid_t *dacs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001109 int *path_idx,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001110 const struct badness_table *bad)
1111{
1112 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001113 int i, j;
1114 int badness = 0;
1115 hda_nid_t dac;
1116
1117 if (!num_outs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 return 0;
1119
Takashi Iwai352f7f92012-12-19 12:52:06 +01001120 for (i = 0; i < num_outs; i++) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001121 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001122 hda_nid_t pin = pins[i];
Takashi Iwai1e0b5282013-01-04 12:56:52 +01001123
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001124 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
1125 if (path) {
1126 badness += assign_out_path_ctls(codec, path);
Takashi Iwai1e0b5282013-01-04 12:56:52 +01001127 continue;
1128 }
1129
1130 dacs[i] = look_for_dac(codec, pin, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001131 if (!dacs[i] && !i) {
Takashi Iwai980428c2013-01-09 09:28:20 +01001132 /* try to steal the DAC of surrounds for the front */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001133 for (j = 1; j < num_outs; j++) {
1134 if (is_reachable_path(codec, dacs[j], pin)) {
1135 dacs[0] = dacs[j];
1136 dacs[j] = 0;
Takashi Iwai980428c2013-01-09 09:28:20 +01001137 invalidate_nid_path(codec, path_idx[j]);
Takashi Iwai196c17662013-01-04 15:01:40 +01001138 path_idx[j] = 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001139 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 }
1141 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001142 }
1143 dac = dacs[i];
1144 if (!dac) {
Takashi Iwai7385df62013-01-07 09:50:52 +01001145 if (num_outs > 2)
1146 dac = try_dac(codec, get_primary_out(codec, i), pin);
1147 if (!dac)
1148 dac = try_dac(codec, dacs[0], pin);
1149 if (!dac)
1150 dac = try_dac(codec, get_primary_out(codec, i), pin);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001151 if (dac) {
1152 if (!i)
1153 badness += bad->shared_primary;
1154 else if (i == 1)
1155 badness += bad->shared_surr;
1156 else
1157 badness += bad->shared_clfe;
1158 } else if (is_reachable_path(codec, spec->private_dac_nids[0], pin)) {
1159 dac = spec->private_dac_nids[0];
1160 badness += bad->shared_surr_main;
1161 } else if (!i)
1162 badness += bad->no_primary_dac;
1163 else
1164 badness += bad->no_dac;
1165 }
Takashi Iwai1fa335b2013-01-21 11:43:19 +01001166 if (!dac)
1167 continue;
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001168 path = snd_hda_add_new_path(codec, dac, pin, -spec->mixer_nid);
Takashi Iwai117688a2013-01-04 15:41:41 +01001169 if (!path && !i && spec->mixer_nid) {
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001170 /* try with aamix */
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001171 path = snd_hda_add_new_path(codec, dac, pin, 0);
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001172 }
Takashi Iwai1fa335b2013-01-21 11:43:19 +01001173 if (!path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001174 dac = dacs[i] = 0;
Takashi Iwai1fa335b2013-01-21 11:43:19 +01001175 badness += bad->no_dac;
1176 } else {
Takashi Iwaia7694092013-01-21 10:43:18 +01001177 /* print_nid_path("output", path); */
Takashi Iwaie1284af2013-01-03 16:33:02 +01001178 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +01001179 path_idx[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001180 badness += assign_out_path_ctls(codec, path);
Takashi Iwaie1284af2013-01-03 16:33:02 +01001181 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001182 }
1183
1184 return badness;
1185}
1186
1187/* return NID if the given pin has only a single connection to a certain DAC */
1188static hda_nid_t get_dac_if_single(struct hda_codec *codec, hda_nid_t pin)
1189{
1190 struct hda_gen_spec *spec = codec->spec;
1191 int i;
1192 hda_nid_t nid_found = 0;
1193
1194 for (i = 0; i < spec->num_all_dacs; i++) {
1195 hda_nid_t nid = spec->all_dacs[i];
1196 if (!nid || is_dac_already_used(codec, nid))
1197 continue;
1198 if (is_reachable_path(codec, nid, pin)) {
1199 if (nid_found)
1200 return 0;
1201 nid_found = nid;
1202 }
1203 }
1204 return nid_found;
1205}
1206
1207/* check whether the given pin can be a multi-io pin */
1208static bool can_be_multiio_pin(struct hda_codec *codec,
1209 unsigned int location, hda_nid_t nid)
1210{
1211 unsigned int defcfg, caps;
1212
1213 defcfg = snd_hda_codec_get_pincfg(codec, nid);
1214 if (get_defcfg_connect(defcfg) != AC_JACK_PORT_COMPLEX)
1215 return false;
1216 if (location && get_defcfg_location(defcfg) != location)
1217 return false;
1218 caps = snd_hda_query_pin_caps(codec, nid);
1219 if (!(caps & AC_PINCAP_OUT))
1220 return false;
1221 return true;
1222}
1223
Takashi Iwaie22aab72013-01-04 14:50:04 +01001224/* count the number of input pins that are capable to be multi-io */
1225static int count_multiio_pins(struct hda_codec *codec, hda_nid_t reference_pin)
1226{
1227 struct hda_gen_spec *spec = codec->spec;
1228 struct auto_pin_cfg *cfg = &spec->autocfg;
1229 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1230 unsigned int location = get_defcfg_location(defcfg);
1231 int type, i;
1232 int num_pins = 0;
1233
1234 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1235 for (i = 0; i < cfg->num_inputs; i++) {
1236 if (cfg->inputs[i].type != type)
1237 continue;
1238 if (can_be_multiio_pin(codec, location,
1239 cfg->inputs[i].pin))
1240 num_pins++;
1241 }
1242 }
1243 return num_pins;
1244}
1245
Takashi Iwai352f7f92012-12-19 12:52:06 +01001246/*
1247 * multi-io helper
1248 *
1249 * When hardwired is set, try to fill ony hardwired pins, and returns
1250 * zero if any pins are filled, non-zero if nothing found.
1251 * When hardwired is off, try to fill possible input pins, and returns
1252 * the badness value.
1253 */
1254static int fill_multi_ios(struct hda_codec *codec,
1255 hda_nid_t reference_pin,
Takashi Iwaie22aab72013-01-04 14:50:04 +01001256 bool hardwired)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001257{
1258 struct hda_gen_spec *spec = codec->spec;
1259 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwaie22aab72013-01-04 14:50:04 +01001260 int type, i, j, num_pins, old_pins;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001261 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1262 unsigned int location = get_defcfg_location(defcfg);
1263 int badness = 0;
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001264 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001265
1266 old_pins = spec->multi_ios;
1267 if (old_pins >= 2)
1268 goto end_fill;
1269
Takashi Iwaie22aab72013-01-04 14:50:04 +01001270 num_pins = count_multiio_pins(codec, reference_pin);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001271 if (num_pins < 2)
1272 goto end_fill;
1273
Takashi Iwai352f7f92012-12-19 12:52:06 +01001274 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1275 for (i = 0; i < cfg->num_inputs; i++) {
1276 hda_nid_t nid = cfg->inputs[i].pin;
1277 hda_nid_t dac = 0;
1278
1279 if (cfg->inputs[i].type != type)
1280 continue;
1281 if (!can_be_multiio_pin(codec, location, nid))
1282 continue;
1283 for (j = 0; j < spec->multi_ios; j++) {
1284 if (nid == spec->multi_io[j].pin)
1285 break;
1286 }
1287 if (j < spec->multi_ios)
1288 continue;
1289
Takashi Iwai352f7f92012-12-19 12:52:06 +01001290 if (hardwired)
1291 dac = get_dac_if_single(codec, nid);
1292 else if (!dac)
1293 dac = look_for_dac(codec, nid, false);
1294 if (!dac) {
1295 badness++;
1296 continue;
1297 }
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001298 path = snd_hda_add_new_path(codec, dac, nid,
1299 -spec->mixer_nid);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001300 if (!path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001301 badness++;
1302 continue;
1303 }
Takashi Iwaia7694092013-01-21 10:43:18 +01001304 /* print_nid_path("multiio", path); */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001305 spec->multi_io[spec->multi_ios].pin = nid;
1306 spec->multi_io[spec->multi_ios].dac = dac;
Takashi Iwai196c17662013-01-04 15:01:40 +01001307 spec->out_paths[cfg->line_outs + spec->multi_ios] =
1308 snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001309 spec->multi_ios++;
1310 if (spec->multi_ios >= 2)
1311 break;
1312 }
1313 }
1314 end_fill:
1315 if (badness)
1316 badness = BAD_MULTI_IO;
1317 if (old_pins == spec->multi_ios) {
1318 if (hardwired)
1319 return 1; /* nothing found */
1320 else
1321 return badness; /* no badness if nothing found */
1322 }
1323 if (!hardwired && spec->multi_ios < 2) {
1324 /* cancel newly assigned paths */
1325 spec->paths.used -= spec->multi_ios - old_pins;
1326 spec->multi_ios = old_pins;
1327 return badness;
1328 }
1329
1330 /* assign volume and mute controls */
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001331 for (i = old_pins; i < spec->multi_ios; i++) {
1332 path = snd_hda_get_path_from_idx(codec, spec->out_paths[cfg->line_outs + i]);
1333 badness += assign_out_path_ctls(codec, path);
1334 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001335
1336 return badness;
1337}
1338
1339/* map DACs for all pins in the list if they are single connections */
1340static bool map_singles(struct hda_codec *codec, int outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001341 const hda_nid_t *pins, hda_nid_t *dacs, int *path_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001342{
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001343 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001344 int i;
1345 bool found = false;
1346 for (i = 0; i < outs; i++) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001347 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001348 hda_nid_t dac;
1349 if (dacs[i])
1350 continue;
1351 dac = get_dac_if_single(codec, pins[i]);
1352 if (!dac)
1353 continue;
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001354 path = snd_hda_add_new_path(codec, dac, pins[i],
1355 -spec->mixer_nid);
Takashi Iwai117688a2013-01-04 15:41:41 +01001356 if (!path && !i && spec->mixer_nid)
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001357 path = snd_hda_add_new_path(codec, dac, pins[i], 0);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001358 if (path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001359 dacs[i] = dac;
1360 found = true;
Takashi Iwaia7694092013-01-21 10:43:18 +01001361 /* print_nid_path("output", path); */
Takashi Iwaie1284af2013-01-03 16:33:02 +01001362 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +01001363 path_idx[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001364 }
1365 }
1366 return found;
1367}
1368
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001369/* create a new path including aamix if available, and return its index */
1370static int check_aamix_out_path(struct hda_codec *codec, int path_idx)
1371{
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001372 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001373 struct nid_path *path;
Takashi Iwai5ead56f2013-04-16 14:16:54 +02001374 hda_nid_t path_dac, dac, pin;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001375
1376 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001377 if (!path || !path->depth ||
1378 is_nid_contained(path, spec->mixer_nid))
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001379 return 0;
Takashi Iwai5ead56f2013-04-16 14:16:54 +02001380 path_dac = path->path[0];
1381 dac = spec->private_dac_nids[0];
Takashi Iwaif87498b2013-01-21 14:24:31 +01001382 pin = path->path[path->depth - 1];
1383 path = snd_hda_add_new_path(codec, dac, pin, spec->mixer_nid);
1384 if (!path) {
Takashi Iwai5ead56f2013-04-16 14:16:54 +02001385 if (dac != path_dac)
1386 dac = path_dac;
Takashi Iwaif87498b2013-01-21 14:24:31 +01001387 else if (spec->multiout.hp_out_nid[0])
1388 dac = spec->multiout.hp_out_nid[0];
1389 else if (spec->multiout.extra_out_nid[0])
1390 dac = spec->multiout.extra_out_nid[0];
Takashi Iwai5ead56f2013-04-16 14:16:54 +02001391 else
1392 dac = 0;
Takashi Iwaif87498b2013-01-21 14:24:31 +01001393 if (dac)
1394 path = snd_hda_add_new_path(codec, dac, pin,
1395 spec->mixer_nid);
1396 }
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001397 if (!path)
1398 return 0;
Takashi Iwaia7694092013-01-21 10:43:18 +01001399 /* print_nid_path("output-aamix", path); */
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001400 path->active = false; /* unused as default */
1401 return snd_hda_get_path_idx(codec, path);
1402}
1403
Takashi Iwai55a63d42013-03-21 17:20:12 +01001404/* check whether the independent HP is available with the current config */
1405static bool indep_hp_possible(struct hda_codec *codec)
1406{
1407 struct hda_gen_spec *spec = codec->spec;
1408 struct auto_pin_cfg *cfg = &spec->autocfg;
1409 struct nid_path *path;
1410 int i, idx;
1411
1412 if (cfg->line_out_type == AUTO_PIN_HP_OUT)
1413 idx = spec->out_paths[0];
1414 else
1415 idx = spec->hp_paths[0];
1416 path = snd_hda_get_path_from_idx(codec, idx);
1417 if (!path)
1418 return false;
1419
1420 /* assume no path conflicts unless aamix is involved */
1421 if (!spec->mixer_nid || !is_nid_contained(path, spec->mixer_nid))
1422 return true;
1423
1424 /* check whether output paths contain aamix */
1425 for (i = 0; i < cfg->line_outs; i++) {
1426 if (spec->out_paths[i] == idx)
1427 break;
1428 path = snd_hda_get_path_from_idx(codec, spec->out_paths[i]);
1429 if (path && is_nid_contained(path, spec->mixer_nid))
1430 return false;
1431 }
1432 for (i = 0; i < cfg->speaker_outs; i++) {
1433 path = snd_hda_get_path_from_idx(codec, spec->speaker_paths[i]);
1434 if (path && is_nid_contained(path, spec->mixer_nid))
1435 return false;
1436 }
1437
1438 return true;
1439}
1440
Takashi Iwaia07a9492013-01-07 16:44:06 +01001441/* fill the empty entries in the dac array for speaker/hp with the
1442 * shared dac pointed by the paths
1443 */
1444static void refill_shared_dacs(struct hda_codec *codec, int num_outs,
1445 hda_nid_t *dacs, int *path_idx)
1446{
1447 struct nid_path *path;
1448 int i;
1449
1450 for (i = 0; i < num_outs; i++) {
1451 if (dacs[i])
1452 continue;
1453 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
1454 if (!path)
1455 continue;
1456 dacs[i] = path->path[0];
1457 }
1458}
1459
Takashi Iwai352f7f92012-12-19 12:52:06 +01001460/* fill in the dac_nids table from the parsed pin configuration */
1461static int fill_and_eval_dacs(struct hda_codec *codec,
1462 bool fill_hardwired,
1463 bool fill_mio_first)
1464{
1465 struct hda_gen_spec *spec = codec->spec;
1466 struct auto_pin_cfg *cfg = &spec->autocfg;
1467 int i, err, badness;
1468
1469 /* set num_dacs once to full for look_for_dac() */
1470 spec->multiout.num_dacs = cfg->line_outs;
1471 spec->multiout.dac_nids = spec->private_dac_nids;
1472 memset(spec->private_dac_nids, 0, sizeof(spec->private_dac_nids));
1473 memset(spec->multiout.hp_out_nid, 0, sizeof(spec->multiout.hp_out_nid));
1474 memset(spec->multiout.extra_out_nid, 0, sizeof(spec->multiout.extra_out_nid));
1475 spec->multi_ios = 0;
1476 snd_array_free(&spec->paths);
Takashi Iwaicd5be3f2013-01-07 15:07:00 +01001477
1478 /* clear path indices */
1479 memset(spec->out_paths, 0, sizeof(spec->out_paths));
1480 memset(spec->hp_paths, 0, sizeof(spec->hp_paths));
1481 memset(spec->speaker_paths, 0, sizeof(spec->speaker_paths));
1482 memset(spec->aamix_out_paths, 0, sizeof(spec->aamix_out_paths));
1483 memset(spec->digout_paths, 0, sizeof(spec->digout_paths));
Takashi Iwaic697b712013-01-07 17:09:26 +01001484 memset(spec->input_paths, 0, sizeof(spec->input_paths));
Takashi Iwaicd5be3f2013-01-07 15:07:00 +01001485 memset(spec->loopback_paths, 0, sizeof(spec->loopback_paths));
1486 memset(&spec->digin_path, 0, sizeof(spec->digin_path));
1487
Takashi Iwai352f7f92012-12-19 12:52:06 +01001488 badness = 0;
1489
1490 /* fill hard-wired DACs first */
1491 if (fill_hardwired) {
1492 bool mapped;
1493 do {
1494 mapped = map_singles(codec, cfg->line_outs,
1495 cfg->line_out_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001496 spec->private_dac_nids,
1497 spec->out_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001498 mapped |= map_singles(codec, cfg->hp_outs,
1499 cfg->hp_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001500 spec->multiout.hp_out_nid,
1501 spec->hp_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001502 mapped |= map_singles(codec, cfg->speaker_outs,
1503 cfg->speaker_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001504 spec->multiout.extra_out_nid,
1505 spec->speaker_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001506 if (fill_mio_first && cfg->line_outs == 1 &&
1507 cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
Takashi Iwaie22aab72013-01-04 14:50:04 +01001508 err = fill_multi_ios(codec, cfg->line_out_pins[0], true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001509 if (!err)
1510 mapped = true;
1511 }
1512 } while (mapped);
1513 }
1514
1515 badness += try_assign_dacs(codec, cfg->line_outs, cfg->line_out_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001516 spec->private_dac_nids, spec->out_paths,
Takashi Iwai98bd1112013-03-22 14:53:50 +01001517 spec->main_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001518
Takashi Iwai352f7f92012-12-19 12:52:06 +01001519 if (fill_mio_first &&
1520 cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1521 /* try to fill multi-io first */
Takashi Iwaie22aab72013-01-04 14:50:04 +01001522 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001523 if (err < 0)
1524 return err;
1525 /* we don't count badness at this stage yet */
1526 }
1527
1528 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
1529 err = try_assign_dacs(codec, cfg->hp_outs, cfg->hp_pins,
1530 spec->multiout.hp_out_nid,
Takashi Iwai196c17662013-01-04 15:01:40 +01001531 spec->hp_paths,
Takashi Iwai98bd1112013-03-22 14:53:50 +01001532 spec->extra_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001533 if (err < 0)
1534 return err;
1535 badness += err;
1536 }
1537 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1538 err = try_assign_dacs(codec, cfg->speaker_outs,
1539 cfg->speaker_pins,
1540 spec->multiout.extra_out_nid,
Takashi Iwai196c17662013-01-04 15:01:40 +01001541 spec->speaker_paths,
Takashi Iwai98bd1112013-03-22 14:53:50 +01001542 spec->extra_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001543 if (err < 0)
1544 return err;
1545 badness += err;
1546 }
1547 if (cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
Takashi Iwaie22aab72013-01-04 14:50:04 +01001548 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001549 if (err < 0)
1550 return err;
1551 badness += err;
1552 }
Takashi Iwaie22aab72013-01-04 14:50:04 +01001553
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001554 if (spec->mixer_nid) {
1555 spec->aamix_out_paths[0] =
1556 check_aamix_out_path(codec, spec->out_paths[0]);
1557 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1558 spec->aamix_out_paths[1] =
1559 check_aamix_out_path(codec, spec->hp_paths[0]);
1560 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1561 spec->aamix_out_paths[2] =
1562 check_aamix_out_path(codec, spec->speaker_paths[0]);
1563 }
1564
Takashi Iwaie22aab72013-01-04 14:50:04 +01001565 if (cfg->hp_outs && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
1566 if (count_multiio_pins(codec, cfg->hp_pins[0]) >= 2)
1567 spec->multi_ios = 1; /* give badness */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001568
Takashi Iwaia07a9492013-01-07 16:44:06 +01001569 /* re-count num_dacs and squash invalid entries */
1570 spec->multiout.num_dacs = 0;
1571 for (i = 0; i < cfg->line_outs; i++) {
1572 if (spec->private_dac_nids[i])
1573 spec->multiout.num_dacs++;
1574 else {
1575 memmove(spec->private_dac_nids + i,
1576 spec->private_dac_nids + i + 1,
1577 sizeof(hda_nid_t) * (cfg->line_outs - i - 1));
1578 spec->private_dac_nids[cfg->line_outs - 1] = 0;
1579 }
1580 }
1581
1582 spec->ext_channel_count = spec->min_channel_count =
David Henningssonc0f3b212013-01-16 11:45:37 +01001583 spec->multiout.num_dacs * 2;
Takashi Iwaia07a9492013-01-07 16:44:06 +01001584
Takashi Iwai352f7f92012-12-19 12:52:06 +01001585 if (spec->multi_ios == 2) {
1586 for (i = 0; i < 2; i++)
1587 spec->private_dac_nids[spec->multiout.num_dacs++] =
1588 spec->multi_io[i].dac;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001589 } else if (spec->multi_ios) {
1590 spec->multi_ios = 0;
1591 badness += BAD_MULTI_IO;
1592 }
1593
Takashi Iwai55a63d42013-03-21 17:20:12 +01001594 if (spec->indep_hp && !indep_hp_possible(codec))
1595 badness += BAD_NO_INDEP_HP;
1596
Takashi Iwaia07a9492013-01-07 16:44:06 +01001597 /* re-fill the shared DAC for speaker / headphone */
1598 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1599 refill_shared_dacs(codec, cfg->hp_outs,
1600 spec->multiout.hp_out_nid,
1601 spec->hp_paths);
1602 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1603 refill_shared_dacs(codec, cfg->speaker_outs,
1604 spec->multiout.extra_out_nid,
1605 spec->speaker_paths);
1606
Takashi Iwai352f7f92012-12-19 12:52:06 +01001607 return badness;
1608}
1609
1610#define DEBUG_BADNESS
1611
1612#ifdef DEBUG_BADNESS
1613#define debug_badness snd_printdd
1614#else
1615#define debug_badness(...)
1616#endif
1617
Takashi Iwaia7694092013-01-21 10:43:18 +01001618#ifdef DEBUG_BADNESS
1619static inline void print_nid_path_idx(struct hda_codec *codec,
1620 const char *pfx, int idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001621{
Takashi Iwaia7694092013-01-21 10:43:18 +01001622 struct nid_path *path;
1623
1624 path = snd_hda_get_path_from_idx(codec, idx);
1625 if (path)
1626 print_nid_path(pfx, path);
1627}
1628
1629static void debug_show_configs(struct hda_codec *codec,
1630 struct auto_pin_cfg *cfg)
1631{
1632 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia7694092013-01-21 10:43:18 +01001633 static const char * const lo_type[3] = { "LO", "SP", "HP" };
Takashi Iwaia7694092013-01-21 10:43:18 +01001634 int i;
1635
1636 debug_badness("multi_outs = %x/%x/%x/%x : %x/%x/%x/%x (type %s)\n",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001637 cfg->line_out_pins[0], cfg->line_out_pins[1],
Takashi Iwai708122e2012-12-20 17:56:57 +01001638 cfg->line_out_pins[2], cfg->line_out_pins[3],
Takashi Iwai352f7f92012-12-19 12:52:06 +01001639 spec->multiout.dac_nids[0],
1640 spec->multiout.dac_nids[1],
1641 spec->multiout.dac_nids[2],
Takashi Iwaia7694092013-01-21 10:43:18 +01001642 spec->multiout.dac_nids[3],
1643 lo_type[cfg->line_out_type]);
1644 for (i = 0; i < cfg->line_outs; i++)
1645 print_nid_path_idx(codec, " out", spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001646 if (spec->multi_ios > 0)
1647 debug_badness("multi_ios(%d) = %x/%x : %x/%x\n",
1648 spec->multi_ios,
1649 spec->multi_io[0].pin, spec->multi_io[1].pin,
1650 spec->multi_io[0].dac, spec->multi_io[1].dac);
Takashi Iwaia7694092013-01-21 10:43:18 +01001651 for (i = 0; i < spec->multi_ios; i++)
1652 print_nid_path_idx(codec, " mio",
1653 spec->out_paths[cfg->line_outs + i]);
1654 if (cfg->hp_outs)
1655 debug_badness("hp_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001656 cfg->hp_pins[0], cfg->hp_pins[1],
Takashi Iwai708122e2012-12-20 17:56:57 +01001657 cfg->hp_pins[2], cfg->hp_pins[3],
Takashi Iwai352f7f92012-12-19 12:52:06 +01001658 spec->multiout.hp_out_nid[0],
1659 spec->multiout.hp_out_nid[1],
1660 spec->multiout.hp_out_nid[2],
1661 spec->multiout.hp_out_nid[3]);
Takashi Iwaia7694092013-01-21 10:43:18 +01001662 for (i = 0; i < cfg->hp_outs; i++)
1663 print_nid_path_idx(codec, " hp ", spec->hp_paths[i]);
1664 if (cfg->speaker_outs)
1665 debug_badness("spk_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001666 cfg->speaker_pins[0], cfg->speaker_pins[1],
1667 cfg->speaker_pins[2], cfg->speaker_pins[3],
1668 spec->multiout.extra_out_nid[0],
1669 spec->multiout.extra_out_nid[1],
1670 spec->multiout.extra_out_nid[2],
1671 spec->multiout.extra_out_nid[3]);
Takashi Iwaia7694092013-01-21 10:43:18 +01001672 for (i = 0; i < cfg->speaker_outs; i++)
1673 print_nid_path_idx(codec, " spk", spec->speaker_paths[i]);
1674 for (i = 0; i < 3; i++)
1675 print_nid_path_idx(codec, " mix", spec->aamix_out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001676}
Takashi Iwaia7694092013-01-21 10:43:18 +01001677#else
1678#define debug_show_configs(codec, cfg) /* NOP */
1679#endif
Takashi Iwai352f7f92012-12-19 12:52:06 +01001680
1681/* find all available DACs of the codec */
1682static void fill_all_dac_nids(struct hda_codec *codec)
1683{
1684 struct hda_gen_spec *spec = codec->spec;
1685 int i;
1686 hda_nid_t nid = codec->start_nid;
1687
1688 spec->num_all_dacs = 0;
1689 memset(spec->all_dacs, 0, sizeof(spec->all_dacs));
1690 for (i = 0; i < codec->num_nodes; i++, nid++) {
1691 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_AUD_OUT)
1692 continue;
1693 if (spec->num_all_dacs >= ARRAY_SIZE(spec->all_dacs)) {
1694 snd_printk(KERN_ERR "hda: Too many DACs!\n");
1695 break;
1696 }
1697 spec->all_dacs[spec->num_all_dacs++] = nid;
1698 }
1699}
1700
1701static int parse_output_paths(struct hda_codec *codec)
1702{
1703 struct hda_gen_spec *spec = codec->spec;
1704 struct auto_pin_cfg *cfg = &spec->autocfg;
1705 struct auto_pin_cfg *best_cfg;
Takashi Iwai9314a582013-01-21 10:49:05 +01001706 unsigned int val;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001707 int best_badness = INT_MAX;
1708 int badness;
1709 bool fill_hardwired = true, fill_mio_first = true;
1710 bool best_wired = true, best_mio = true;
1711 bool hp_spk_swapped = false;
1712
Takashi Iwai352f7f92012-12-19 12:52:06 +01001713 best_cfg = kmalloc(sizeof(*best_cfg), GFP_KERNEL);
1714 if (!best_cfg)
1715 return -ENOMEM;
1716 *best_cfg = *cfg;
1717
1718 for (;;) {
1719 badness = fill_and_eval_dacs(codec, fill_hardwired,
1720 fill_mio_first);
1721 if (badness < 0) {
1722 kfree(best_cfg);
1723 return badness;
1724 }
1725 debug_badness("==> lo_type=%d, wired=%d, mio=%d, badness=0x%x\n",
1726 cfg->line_out_type, fill_hardwired, fill_mio_first,
1727 badness);
Takashi Iwaia7694092013-01-21 10:43:18 +01001728 debug_show_configs(codec, cfg);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001729 if (badness < best_badness) {
1730 best_badness = badness;
1731 *best_cfg = *cfg;
1732 best_wired = fill_hardwired;
1733 best_mio = fill_mio_first;
1734 }
1735 if (!badness)
1736 break;
1737 fill_mio_first = !fill_mio_first;
1738 if (!fill_mio_first)
1739 continue;
1740 fill_hardwired = !fill_hardwired;
1741 if (!fill_hardwired)
1742 continue;
1743 if (hp_spk_swapped)
1744 break;
1745 hp_spk_swapped = true;
1746 if (cfg->speaker_outs > 0 &&
1747 cfg->line_out_type == AUTO_PIN_HP_OUT) {
1748 cfg->hp_outs = cfg->line_outs;
1749 memcpy(cfg->hp_pins, cfg->line_out_pins,
1750 sizeof(cfg->hp_pins));
1751 cfg->line_outs = cfg->speaker_outs;
1752 memcpy(cfg->line_out_pins, cfg->speaker_pins,
1753 sizeof(cfg->speaker_pins));
1754 cfg->speaker_outs = 0;
1755 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
1756 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
1757 fill_hardwired = true;
1758 continue;
1759 }
1760 if (cfg->hp_outs > 0 &&
1761 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
1762 cfg->speaker_outs = cfg->line_outs;
1763 memcpy(cfg->speaker_pins, cfg->line_out_pins,
1764 sizeof(cfg->speaker_pins));
1765 cfg->line_outs = cfg->hp_outs;
1766 memcpy(cfg->line_out_pins, cfg->hp_pins,
1767 sizeof(cfg->hp_pins));
1768 cfg->hp_outs = 0;
1769 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
1770 cfg->line_out_type = AUTO_PIN_HP_OUT;
1771 fill_hardwired = true;
1772 continue;
1773 }
1774 break;
1775 }
1776
1777 if (badness) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001778 debug_badness("==> restoring best_cfg\n");
Takashi Iwai352f7f92012-12-19 12:52:06 +01001779 *cfg = *best_cfg;
1780 fill_and_eval_dacs(codec, best_wired, best_mio);
1781 }
1782 debug_badness("==> Best config: lo_type=%d, wired=%d, mio=%d\n",
1783 cfg->line_out_type, best_wired, best_mio);
Takashi Iwaia7694092013-01-21 10:43:18 +01001784 debug_show_configs(codec, cfg);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001785
1786 if (cfg->line_out_pins[0]) {
1787 struct nid_path *path;
Takashi Iwai196c17662013-01-04 15:01:40 +01001788 path = snd_hda_get_path_from_idx(codec, spec->out_paths[0]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001789 if (path)
1790 spec->vmaster_nid = look_for_out_vol_nid(codec, path);
Takashi Iwai7a71bbf2013-01-17 10:25:15 +01001791 if (spec->vmaster_nid)
1792 snd_hda_set_vmaster_tlv(codec, spec->vmaster_nid,
1793 HDA_OUTPUT, spec->vmaster_tlv);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001794 }
1795
Takashi Iwai9314a582013-01-21 10:49:05 +01001796 /* set initial pinctl targets */
1797 if (spec->prefer_hp_amp || cfg->line_out_type == AUTO_PIN_HP_OUT)
1798 val = PIN_HP;
1799 else
1800 val = PIN_OUT;
1801 set_pin_targets(codec, cfg->line_outs, cfg->line_out_pins, val);
1802 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1803 set_pin_targets(codec, cfg->hp_outs, cfg->hp_pins, PIN_HP);
1804 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1805 val = spec->prefer_hp_amp ? PIN_HP : PIN_OUT;
1806 set_pin_targets(codec, cfg->speaker_outs,
1807 cfg->speaker_pins, val);
1808 }
1809
Takashi Iwai55a63d42013-03-21 17:20:12 +01001810 /* clear indep_hp flag if not available */
1811 if (spec->indep_hp && !indep_hp_possible(codec))
1812 spec->indep_hp = 0;
1813
Takashi Iwai352f7f92012-12-19 12:52:06 +01001814 kfree(best_cfg);
1815 return 0;
1816}
1817
1818/* add playback controls from the parsed DAC table */
1819static int create_multi_out_ctls(struct hda_codec *codec,
1820 const struct auto_pin_cfg *cfg)
1821{
1822 struct hda_gen_spec *spec = codec->spec;
1823 int i, err, noutputs;
1824
1825 noutputs = cfg->line_outs;
1826 if (spec->multi_ios > 0 && cfg->line_outs < 3)
1827 noutputs += spec->multi_ios;
1828
1829 for (i = 0; i < noutputs; i++) {
1830 const char *name;
1831 int index;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001832 struct nid_path *path;
1833
Takashi Iwai196c17662013-01-04 15:01:40 +01001834 path = snd_hda_get_path_from_idx(codec, spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001835 if (!path)
1836 continue;
Takashi Iwai247d85e2013-01-17 16:18:11 +01001837
1838 name = get_line_out_pfx(codec, i, &index, NID_PATH_VOL_CTL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001839 if (!name || !strcmp(name, "CLFE")) {
1840 /* Center/LFE */
1841 err = add_vol_ctl(codec, "Center", 0, 1, path);
1842 if (err < 0)
1843 return err;
1844 err = add_vol_ctl(codec, "LFE", 0, 2, path);
1845 if (err < 0)
1846 return err;
Takashi Iwai247d85e2013-01-17 16:18:11 +01001847 } else {
1848 err = add_stereo_vol(codec, name, index, path);
1849 if (err < 0)
1850 return err;
1851 }
1852
1853 name = get_line_out_pfx(codec, i, &index, NID_PATH_MUTE_CTL);
1854 if (!name || !strcmp(name, "CLFE")) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001855 err = add_sw_ctl(codec, "Center", 0, 1, path);
1856 if (err < 0)
1857 return err;
1858 err = add_sw_ctl(codec, "LFE", 0, 2, path);
1859 if (err < 0)
1860 return err;
1861 } else {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001862 err = add_stereo_sw(codec, name, index, path);
1863 if (err < 0)
1864 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 }
1866 }
1867 return 0;
1868}
1869
Takashi Iwaic2c80382013-01-07 10:33:57 +01001870static int create_extra_out(struct hda_codec *codec, int path_idx,
Takashi Iwai196c17662013-01-04 15:01:40 +01001871 const char *pfx, int cidx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872{
Takashi Iwai352f7f92012-12-19 12:52:06 +01001873 struct nid_path *path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 int err;
1875
Takashi Iwai196c17662013-01-04 15:01:40 +01001876 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001877 if (!path)
1878 return 0;
Takashi Iwaic2c80382013-01-07 10:33:57 +01001879 err = add_stereo_vol(codec, pfx, cidx, path);
1880 if (err < 0)
1881 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001882 err = add_stereo_sw(codec, pfx, cidx, path);
1883 if (err < 0)
1884 return err;
1885 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886}
1887
Takashi Iwai352f7f92012-12-19 12:52:06 +01001888/* add playback controls for speaker and HP outputs */
1889static int create_extra_outs(struct hda_codec *codec, int num_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001890 const int *paths, const char *pfx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891{
Takashi Iwaic2c80382013-01-07 10:33:57 +01001892 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001893
1894 for (i = 0; i < num_pins; i++) {
Takashi Iwaic2c80382013-01-07 10:33:57 +01001895 const char *name;
1896 char tmp[44];
1897 int err, idx = 0;
1898
1899 if (num_pins == 2 && i == 1 && !strcmp(pfx, "Speaker"))
1900 name = "Bass Speaker";
1901 else if (num_pins >= 3) {
1902 snprintf(tmp, sizeof(tmp), "%s %s",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001903 pfx, channel_name[i]);
Takashi Iwaic2c80382013-01-07 10:33:57 +01001904 name = tmp;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001905 } else {
Takashi Iwaic2c80382013-01-07 10:33:57 +01001906 name = pfx;
1907 idx = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 }
Takashi Iwaic2c80382013-01-07 10:33:57 +01001909 err = create_extra_out(codec, paths[i], name, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001910 if (err < 0)
1911 return err;
1912 }
1913 return 0;
1914}
Takashi Iwai97ec5582006-03-21 11:29:07 +01001915
Takashi Iwai352f7f92012-12-19 12:52:06 +01001916static int create_hp_out_ctls(struct hda_codec *codec)
1917{
1918 struct hda_gen_spec *spec = codec->spec;
1919 return create_extra_outs(codec, spec->autocfg.hp_outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001920 spec->hp_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001921 "Headphone");
1922}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923
Takashi Iwai352f7f92012-12-19 12:52:06 +01001924static int create_speaker_out_ctls(struct hda_codec *codec)
1925{
1926 struct hda_gen_spec *spec = codec->spec;
1927 return create_extra_outs(codec, spec->autocfg.speaker_outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001928 spec->speaker_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001929 "Speaker");
1930}
1931
1932/*
Takashi Iwai38cf6f12012-12-21 14:09:42 +01001933 * independent HP controls
1934 */
1935
Takashi Iwai8ba955c2013-03-07 18:40:58 +01001936/* update HP auto-mute state too */
1937static void update_hp_automute_hook(struct hda_codec *codec)
1938{
1939 struct hda_gen_spec *spec = codec->spec;
1940
1941 if (spec->hp_automute_hook)
1942 spec->hp_automute_hook(codec, NULL);
1943 else
1944 snd_hda_gen_hp_automute(codec, NULL);
1945}
1946
Takashi Iwai38cf6f12012-12-21 14:09:42 +01001947static int indep_hp_info(struct snd_kcontrol *kcontrol,
1948 struct snd_ctl_elem_info *uinfo)
1949{
1950 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
1951}
1952
1953static int indep_hp_get(struct snd_kcontrol *kcontrol,
1954 struct snd_ctl_elem_value *ucontrol)
1955{
1956 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1957 struct hda_gen_spec *spec = codec->spec;
1958 ucontrol->value.enumerated.item[0] = spec->indep_hp_enabled;
1959 return 0;
1960}
1961
Takashi Iwaia1e908e2013-01-21 15:11:25 +01001962static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
1963 int nomix_path_idx, int mix_path_idx,
1964 int out_type);
1965
Takashi Iwai38cf6f12012-12-21 14:09:42 +01001966static int indep_hp_put(struct snd_kcontrol *kcontrol,
1967 struct snd_ctl_elem_value *ucontrol)
1968{
1969 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1970 struct hda_gen_spec *spec = codec->spec;
1971 unsigned int select = ucontrol->value.enumerated.item[0];
1972 int ret = 0;
1973
1974 mutex_lock(&spec->pcm_mutex);
1975 if (spec->active_streams) {
1976 ret = -EBUSY;
1977 goto unlock;
1978 }
1979
1980 if (spec->indep_hp_enabled != select) {
Takashi Iwaia1e908e2013-01-21 15:11:25 +01001981 hda_nid_t *dacp;
1982 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
1983 dacp = &spec->private_dac_nids[0];
1984 else
1985 dacp = &spec->multiout.hp_out_nid[0];
1986
1987 /* update HP aamix paths in case it conflicts with indep HP */
1988 if (spec->have_aamix_ctl) {
1989 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
1990 update_aamix_paths(codec, spec->aamix_mode,
1991 spec->out_paths[0],
1992 spec->aamix_out_paths[0],
1993 spec->autocfg.line_out_type);
1994 else
1995 update_aamix_paths(codec, spec->aamix_mode,
1996 spec->hp_paths[0],
1997 spec->aamix_out_paths[1],
1998 AUTO_PIN_HP_OUT);
1999 }
2000
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002001 spec->indep_hp_enabled = select;
2002 if (spec->indep_hp_enabled)
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002003 *dacp = 0;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002004 else
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002005 *dacp = spec->alt_dac_nid;
Takashi Iwai92603c52013-01-22 07:46:31 +01002006
Takashi Iwai8ba955c2013-03-07 18:40:58 +01002007 update_hp_automute_hook(codec);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002008 ret = 1;
2009 }
2010 unlock:
2011 mutex_unlock(&spec->pcm_mutex);
2012 return ret;
2013}
2014
2015static const struct snd_kcontrol_new indep_hp_ctl = {
2016 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2017 .name = "Independent HP",
2018 .info = indep_hp_info,
2019 .get = indep_hp_get,
2020 .put = indep_hp_put,
2021};
2022
2023
2024static int create_indep_hp_ctls(struct hda_codec *codec)
2025{
2026 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002027 hda_nid_t dac;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002028
2029 if (!spec->indep_hp)
2030 return 0;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002031 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
2032 dac = spec->multiout.dac_nids[0];
2033 else
2034 dac = spec->multiout.hp_out_nid[0];
2035 if (!dac) {
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002036 spec->indep_hp = 0;
2037 return 0;
2038 }
2039
2040 spec->indep_hp_enabled = false;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002041 spec->alt_dac_nid = dac;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002042 if (!snd_hda_gen_add_kctl(spec, NULL, &indep_hp_ctl))
2043 return -ENOMEM;
2044 return 0;
2045}
2046
2047/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01002048 * channel mode enum control
2049 */
2050
2051static int ch_mode_info(struct snd_kcontrol *kcontrol,
2052 struct snd_ctl_elem_info *uinfo)
2053{
2054 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2055 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002056 int chs;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002057
2058 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2059 uinfo->count = 1;
2060 uinfo->value.enumerated.items = spec->multi_ios + 1;
2061 if (uinfo->value.enumerated.item > spec->multi_ios)
2062 uinfo->value.enumerated.item = spec->multi_ios;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002063 chs = uinfo->value.enumerated.item * 2 + spec->min_channel_count;
2064 sprintf(uinfo->value.enumerated.name, "%dch", chs);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002065 return 0;
2066}
2067
2068static int ch_mode_get(struct snd_kcontrol *kcontrol,
2069 struct snd_ctl_elem_value *ucontrol)
2070{
2071 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2072 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002073 ucontrol->value.enumerated.item[0] =
2074 (spec->ext_channel_count - spec->min_channel_count) / 2;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002075 return 0;
2076}
2077
Takashi Iwai196c17662013-01-04 15:01:40 +01002078static inline struct nid_path *
2079get_multiio_path(struct hda_codec *codec, int idx)
2080{
2081 struct hda_gen_spec *spec = codec->spec;
2082 return snd_hda_get_path_from_idx(codec,
2083 spec->out_paths[spec->autocfg.line_outs + idx]);
2084}
2085
Takashi Iwaia5cc2502013-01-16 18:08:55 +01002086static void update_automute_all(struct hda_codec *codec);
2087
Takashi Iwai65033cc2013-04-16 12:31:05 +02002088/* Default value to be passed as aamix argument for snd_hda_activate_path();
2089 * used for output paths
2090 */
2091static bool aamix_default(struct hda_gen_spec *spec)
2092{
2093 return !spec->have_aamix_ctl || spec->aamix_mode;
2094}
2095
Takashi Iwai352f7f92012-12-19 12:52:06 +01002096static int set_multi_io(struct hda_codec *codec, int idx, bool output)
2097{
2098 struct hda_gen_spec *spec = codec->spec;
2099 hda_nid_t nid = spec->multi_io[idx].pin;
2100 struct nid_path *path;
2101
Takashi Iwai196c17662013-01-04 15:01:40 +01002102 path = get_multiio_path(codec, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002103 if (!path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 return -EINVAL;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002105
2106 if (path->active == output)
2107 return 0;
2108
2109 if (output) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01002110 set_pin_target(codec, nid, PIN_OUT, true);
Takashi Iwai65033cc2013-04-16 12:31:05 +02002111 snd_hda_activate_path(codec, path, true, aamix_default(spec));
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01002112 set_pin_eapd(codec, nid, true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002113 } else {
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01002114 set_pin_eapd(codec, nid, false);
Takashi Iwai65033cc2013-04-16 12:31:05 +02002115 snd_hda_activate_path(codec, path, false, aamix_default(spec));
Takashi Iwai2c12c302013-01-10 09:33:29 +01002116 set_pin_target(codec, nid, spec->multi_io[idx].ctl_in, true);
Takashi Iwai55196ff2013-01-24 17:32:56 +01002117 path_power_down_sync(codec, path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 }
Takashi Iwaia365fed2013-01-10 16:10:06 +01002119
2120 /* update jack retasking in case it modifies any of them */
Takashi Iwaia5cc2502013-01-16 18:08:55 +01002121 update_automute_all(codec);
Takashi Iwaia365fed2013-01-10 16:10:06 +01002122
Takashi Iwai352f7f92012-12-19 12:52:06 +01002123 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124}
2125
Takashi Iwai352f7f92012-12-19 12:52:06 +01002126static int ch_mode_put(struct snd_kcontrol *kcontrol,
2127 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002129 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2130 struct hda_gen_spec *spec = codec->spec;
2131 int i, ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132
Takashi Iwai352f7f92012-12-19 12:52:06 +01002133 ch = ucontrol->value.enumerated.item[0];
2134 if (ch < 0 || ch > spec->multi_ios)
2135 return -EINVAL;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002136 if (ch == (spec->ext_channel_count - spec->min_channel_count) / 2)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002137 return 0;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002138 spec->ext_channel_count = ch * 2 + spec->min_channel_count;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002139 for (i = 0; i < spec->multi_ios; i++)
2140 set_multi_io(codec, i, i < ch);
2141 spec->multiout.max_channels = max(spec->ext_channel_count,
2142 spec->const_channel_count);
2143 if (spec->need_dac_fix)
2144 spec->multiout.num_dacs = spec->multiout.max_channels / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145 return 1;
2146}
2147
Takashi Iwai352f7f92012-12-19 12:52:06 +01002148static const struct snd_kcontrol_new channel_mode_enum = {
2149 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2150 .name = "Channel Mode",
2151 .info = ch_mode_info,
2152 .get = ch_mode_get,
2153 .put = ch_mode_put,
2154};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155
Takashi Iwai352f7f92012-12-19 12:52:06 +01002156static int create_multi_channel_mode(struct hda_codec *codec)
2157{
2158 struct hda_gen_spec *spec = codec->spec;
2159
2160 if (spec->multi_ios > 0) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01002161 if (!snd_hda_gen_add_kctl(spec, NULL, &channel_mode_enum))
Takashi Iwai352f7f92012-12-19 12:52:06 +01002162 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 return 0;
2165}
2166
Takashi Iwai352f7f92012-12-19 12:52:06 +01002167/*
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002168 * aamix loopback enable/disable switch
2169 */
2170
2171#define loopback_mixing_info indep_hp_info
2172
2173static int loopback_mixing_get(struct snd_kcontrol *kcontrol,
2174 struct snd_ctl_elem_value *ucontrol)
2175{
2176 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2177 struct hda_gen_spec *spec = codec->spec;
2178 ucontrol->value.enumerated.item[0] = spec->aamix_mode;
2179 return 0;
2180}
2181
2182static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002183 int nomix_path_idx, int mix_path_idx,
2184 int out_type)
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002185{
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002186 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002187 struct nid_path *nomix_path, *mix_path;
2188
2189 nomix_path = snd_hda_get_path_from_idx(codec, nomix_path_idx);
2190 mix_path = snd_hda_get_path_from_idx(codec, mix_path_idx);
2191 if (!nomix_path || !mix_path)
2192 return;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002193
2194 /* if HP aamix path is driven from a different DAC and the
2195 * independent HP mode is ON, can't turn on aamix path
2196 */
2197 if (out_type == AUTO_PIN_HP_OUT && spec->indep_hp_enabled &&
2198 mix_path->path[0] != spec->alt_dac_nid)
2199 do_mix = false;
2200
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002201 if (do_mix) {
2202 snd_hda_activate_path(codec, nomix_path, false, true);
2203 snd_hda_activate_path(codec, mix_path, true, true);
Takashi Iwai55196ff2013-01-24 17:32:56 +01002204 path_power_down_sync(codec, nomix_path);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002205 } else {
Takashi Iwai65033cc2013-04-16 12:31:05 +02002206 snd_hda_activate_path(codec, mix_path, false, false);
2207 snd_hda_activate_path(codec, nomix_path, true, false);
Takashi Iwai55196ff2013-01-24 17:32:56 +01002208 path_power_down_sync(codec, mix_path);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002209 }
2210}
2211
2212static int loopback_mixing_put(struct snd_kcontrol *kcontrol,
2213 struct snd_ctl_elem_value *ucontrol)
2214{
2215 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2216 struct hda_gen_spec *spec = codec->spec;
2217 unsigned int val = ucontrol->value.enumerated.item[0];
2218
2219 if (val == spec->aamix_mode)
2220 return 0;
2221 spec->aamix_mode = val;
2222 update_aamix_paths(codec, val, spec->out_paths[0],
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002223 spec->aamix_out_paths[0],
2224 spec->autocfg.line_out_type);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002225 update_aamix_paths(codec, val, spec->hp_paths[0],
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002226 spec->aamix_out_paths[1],
2227 AUTO_PIN_HP_OUT);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002228 update_aamix_paths(codec, val, spec->speaker_paths[0],
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002229 spec->aamix_out_paths[2],
2230 AUTO_PIN_SPEAKER_OUT);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002231 return 1;
2232}
2233
2234static const struct snd_kcontrol_new loopback_mixing_enum = {
2235 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2236 .name = "Loopback Mixing",
2237 .info = loopback_mixing_info,
2238 .get = loopback_mixing_get,
2239 .put = loopback_mixing_put,
2240};
2241
2242static int create_loopback_mixing_ctl(struct hda_codec *codec)
2243{
2244 struct hda_gen_spec *spec = codec->spec;
2245
2246 if (!spec->mixer_nid)
2247 return 0;
2248 if (!(spec->aamix_out_paths[0] || spec->aamix_out_paths[1] ||
2249 spec->aamix_out_paths[2]))
2250 return 0;
2251 if (!snd_hda_gen_add_kctl(spec, NULL, &loopback_mixing_enum))
2252 return -ENOMEM;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002253 spec->have_aamix_ctl = 1;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002254 return 0;
2255}
2256
2257/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01002258 * shared headphone/mic handling
2259 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02002260
Takashi Iwai352f7f92012-12-19 12:52:06 +01002261static void call_update_outputs(struct hda_codec *codec);
2262
2263/* for shared I/O, change the pin-control accordingly */
Takashi Iwai967303d2013-02-19 17:12:42 +01002264static void update_hp_mic(struct hda_codec *codec, int adc_mux, bool force)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002265{
2266 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai967303d2013-02-19 17:12:42 +01002267 bool as_mic;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002268 unsigned int val;
Takashi Iwai967303d2013-02-19 17:12:42 +01002269 hda_nid_t pin;
2270
2271 pin = spec->hp_mic_pin;
2272 as_mic = spec->cur_mux[adc_mux] == spec->hp_mic_mux_idx;
2273
2274 if (!force) {
2275 val = snd_hda_codec_get_pin_target(codec, pin);
2276 if (as_mic) {
2277 if (val & PIN_IN)
2278 return;
2279 } else {
2280 if (val & PIN_OUT)
2281 return;
2282 }
2283 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002284
2285 val = snd_hda_get_default_vref(codec, pin);
Takashi Iwai967303d2013-02-19 17:12:42 +01002286 /* if the HP pin doesn't support VREF and the codec driver gives an
2287 * alternative pin, set up the VREF on that pin instead
2288 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01002289 if (val == AC_PINCTL_VREF_HIZ && spec->shared_mic_vref_pin) {
2290 const hda_nid_t vref_pin = spec->shared_mic_vref_pin;
2291 unsigned int vref_val = snd_hda_get_default_vref(codec, vref_pin);
2292 if (vref_val != AC_PINCTL_VREF_HIZ)
Takashi Iwai7594aa32012-12-20 15:38:40 +01002293 snd_hda_set_pin_ctl_cache(codec, vref_pin,
Takashi Iwai967303d2013-02-19 17:12:42 +01002294 PIN_IN | (as_mic ? vref_val : 0));
Takashi Iwaicb53c622007-08-10 17:21:45 +02002295 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002296
Takashi Iwai8ba955c2013-03-07 18:40:58 +01002297 if (!spec->hp_mic_jack_modes) {
2298 if (as_mic)
2299 val |= PIN_IN;
2300 else
2301 val = PIN_HP;
2302 set_pin_target(codec, pin, val, true);
2303 update_hp_automute_hook(codec);
2304 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002305}
2306
2307/* create a shared input with the headphone out */
Takashi Iwai967303d2013-02-19 17:12:42 +01002308static int create_hp_mic(struct hda_codec *codec)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002309{
2310 struct hda_gen_spec *spec = codec->spec;
2311 struct auto_pin_cfg *cfg = &spec->autocfg;
2312 unsigned int defcfg;
2313 hda_nid_t nid;
2314
Takashi Iwai967303d2013-02-19 17:12:42 +01002315 if (!spec->hp_mic) {
2316 if (spec->suppress_hp_mic_detect)
2317 return 0;
2318 /* automatic detection: only if no input or a single internal
2319 * input pin is found, try to detect the shared hp/mic
2320 */
2321 if (cfg->num_inputs > 1)
2322 return 0;
2323 else if (cfg->num_inputs == 1) {
2324 defcfg = snd_hda_codec_get_pincfg(codec, cfg->inputs[0].pin);
2325 if (snd_hda_get_input_pin_attr(defcfg) != INPUT_PIN_ATTR_INT)
2326 return 0;
2327 }
2328 }
2329
2330 spec->hp_mic = 0; /* clear once */
2331 if (cfg->num_inputs >= AUTO_CFG_MAX_INS)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002332 return 0;
2333
Takashi Iwai967303d2013-02-19 17:12:42 +01002334 nid = 0;
2335 if (cfg->line_out_type == AUTO_PIN_HP_OUT && cfg->line_outs > 0)
2336 nid = cfg->line_out_pins[0];
2337 else if (cfg->hp_outs > 0)
2338 nid = cfg->hp_pins[0];
2339 if (!nid)
2340 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002341
2342 if (!(snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_IN))
2343 return 0; /* no input */
2344
Takashi Iwai967303d2013-02-19 17:12:42 +01002345 cfg->inputs[cfg->num_inputs].pin = nid;
2346 cfg->inputs[cfg->num_inputs].type = AUTO_PIN_MIC;
David Henningssoncb420b12013-04-11 11:30:28 +02002347 cfg->inputs[cfg->num_inputs].is_headphone_mic = 1;
Takashi Iwai967303d2013-02-19 17:12:42 +01002348 cfg->num_inputs++;
2349 spec->hp_mic = 1;
2350 spec->hp_mic_pin = nid;
2351 /* we can't handle auto-mic together with HP-mic */
2352 spec->suppress_auto_mic = 1;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002353 snd_printdd("hda-codec: Enable shared I/O jack on NID 0x%x\n", nid);
2354 return 0;
2355}
2356
Takashi Iwai978e77e2013-01-10 16:57:58 +01002357/*
2358 * output jack mode
2359 */
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002360
2361static int create_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t pin);
2362
2363static const char * const out_jack_texts[] = {
2364 "Line Out", "Headphone Out",
2365};
2366
Takashi Iwai978e77e2013-01-10 16:57:58 +01002367static int out_jack_mode_info(struct snd_kcontrol *kcontrol,
2368 struct snd_ctl_elem_info *uinfo)
2369{
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002370 return snd_hda_enum_helper_info(kcontrol, uinfo, 2, out_jack_texts);
Takashi Iwai978e77e2013-01-10 16:57:58 +01002371}
2372
2373static int out_jack_mode_get(struct snd_kcontrol *kcontrol,
2374 struct snd_ctl_elem_value *ucontrol)
2375{
2376 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2377 hda_nid_t nid = kcontrol->private_value;
2378 if (snd_hda_codec_get_pin_target(codec, nid) == PIN_HP)
2379 ucontrol->value.enumerated.item[0] = 1;
2380 else
2381 ucontrol->value.enumerated.item[0] = 0;
2382 return 0;
2383}
2384
2385static int out_jack_mode_put(struct snd_kcontrol *kcontrol,
2386 struct snd_ctl_elem_value *ucontrol)
2387{
2388 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2389 hda_nid_t nid = kcontrol->private_value;
2390 unsigned int val;
2391
2392 val = ucontrol->value.enumerated.item[0] ? PIN_HP : PIN_OUT;
2393 if (snd_hda_codec_get_pin_target(codec, nid) == val)
2394 return 0;
2395 snd_hda_set_pin_ctl_cache(codec, nid, val);
2396 return 1;
2397}
2398
2399static const struct snd_kcontrol_new out_jack_mode_enum = {
2400 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2401 .info = out_jack_mode_info,
2402 .get = out_jack_mode_get,
2403 .put = out_jack_mode_put,
2404};
2405
2406static bool find_kctl_name(struct hda_codec *codec, const char *name, int idx)
2407{
2408 struct hda_gen_spec *spec = codec->spec;
2409 int i;
2410
2411 for (i = 0; i < spec->kctls.used; i++) {
2412 struct snd_kcontrol_new *kctl = snd_array_elem(&spec->kctls, i);
2413 if (!strcmp(kctl->name, name) && kctl->index == idx)
2414 return true;
2415 }
2416 return false;
2417}
2418
2419static void get_jack_mode_name(struct hda_codec *codec, hda_nid_t pin,
2420 char *name, size_t name_len)
2421{
2422 struct hda_gen_spec *spec = codec->spec;
2423 int idx = 0;
2424
2425 snd_hda_get_pin_label(codec, pin, &spec->autocfg, name, name_len, &idx);
2426 strlcat(name, " Jack Mode", name_len);
2427
2428 for (; find_kctl_name(codec, name, idx); idx++)
2429 ;
2430}
2431
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002432static int get_out_jack_num_items(struct hda_codec *codec, hda_nid_t pin)
2433{
2434 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaif811c3c2013-03-07 18:32:59 +01002435 if (spec->add_jack_modes) {
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002436 unsigned int pincap = snd_hda_query_pin_caps(codec, pin);
2437 if ((pincap & AC_PINCAP_OUT) && (pincap & AC_PINCAP_HP_DRV))
2438 return 2;
2439 }
2440 return 1;
2441}
2442
Takashi Iwai978e77e2013-01-10 16:57:58 +01002443static int create_out_jack_modes(struct hda_codec *codec, int num_pins,
2444 hda_nid_t *pins)
2445{
2446 struct hda_gen_spec *spec = codec->spec;
2447 int i;
2448
2449 for (i = 0; i < num_pins; i++) {
2450 hda_nid_t pin = pins[i];
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002451 if (pin == spec->hp_mic_pin) {
2452 int ret = create_hp_mic_jack_mode(codec, pin);
2453 if (ret < 0)
2454 return ret;
2455 continue;
2456 }
2457 if (get_out_jack_num_items(codec, pin) > 1) {
Takashi Iwai978e77e2013-01-10 16:57:58 +01002458 struct snd_kcontrol_new *knew;
2459 char name[44];
2460 get_jack_mode_name(codec, pin, name, sizeof(name));
2461 knew = snd_hda_gen_add_kctl(spec, name,
2462 &out_jack_mode_enum);
2463 if (!knew)
2464 return -ENOMEM;
2465 knew->private_value = pin;
2466 }
2467 }
2468
2469 return 0;
2470}
2471
Takashi Iwai294765582013-01-17 09:52:11 +01002472/*
2473 * input jack mode
2474 */
2475
2476/* from AC_PINCTL_VREF_HIZ to AC_PINCTL_VREF_100 */
2477#define NUM_VREFS 6
2478
2479static const char * const vref_texts[NUM_VREFS] = {
2480 "Line In", "Mic 50pc Bias", "Mic 0V Bias",
2481 "", "Mic 80pc Bias", "Mic 100pc Bias"
2482};
2483
2484static unsigned int get_vref_caps(struct hda_codec *codec, hda_nid_t pin)
2485{
2486 unsigned int pincap;
2487
2488 pincap = snd_hda_query_pin_caps(codec, pin);
2489 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
2490 /* filter out unusual vrefs */
2491 pincap &= ~(AC_PINCAP_VREF_GRD | AC_PINCAP_VREF_100);
2492 return pincap;
2493}
2494
2495/* convert from the enum item index to the vref ctl index (0=HIZ, 1=50%...) */
2496static int get_vref_idx(unsigned int vref_caps, unsigned int item_idx)
2497{
2498 unsigned int i, n = 0;
2499
2500 for (i = 0; i < NUM_VREFS; i++) {
2501 if (vref_caps & (1 << i)) {
2502 if (n == item_idx)
2503 return i;
2504 n++;
2505 }
2506 }
2507 return 0;
2508}
2509
2510/* convert back from the vref ctl index to the enum item index */
2511static int cvt_from_vref_idx(unsigned int vref_caps, unsigned int idx)
2512{
2513 unsigned int i, n = 0;
2514
2515 for (i = 0; i < NUM_VREFS; i++) {
2516 if (i == idx)
2517 return n;
2518 if (vref_caps & (1 << i))
2519 n++;
2520 }
2521 return 0;
2522}
2523
2524static int in_jack_mode_info(struct snd_kcontrol *kcontrol,
2525 struct snd_ctl_elem_info *uinfo)
2526{
2527 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2528 hda_nid_t nid = kcontrol->private_value;
2529 unsigned int vref_caps = get_vref_caps(codec, nid);
2530
2531 snd_hda_enum_helper_info(kcontrol, uinfo, hweight32(vref_caps),
2532 vref_texts);
2533 /* set the right text */
2534 strcpy(uinfo->value.enumerated.name,
2535 vref_texts[get_vref_idx(vref_caps, uinfo->value.enumerated.item)]);
2536 return 0;
2537}
2538
2539static int in_jack_mode_get(struct snd_kcontrol *kcontrol,
2540 struct snd_ctl_elem_value *ucontrol)
2541{
2542 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2543 hda_nid_t nid = kcontrol->private_value;
2544 unsigned int vref_caps = get_vref_caps(codec, nid);
2545 unsigned int idx;
2546
2547 idx = snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_VREFEN;
2548 ucontrol->value.enumerated.item[0] = cvt_from_vref_idx(vref_caps, idx);
2549 return 0;
2550}
2551
2552static int in_jack_mode_put(struct snd_kcontrol *kcontrol,
2553 struct snd_ctl_elem_value *ucontrol)
2554{
2555 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2556 hda_nid_t nid = kcontrol->private_value;
2557 unsigned int vref_caps = get_vref_caps(codec, nid);
2558 unsigned int val, idx;
2559
2560 val = snd_hda_codec_get_pin_target(codec, nid);
2561 idx = cvt_from_vref_idx(vref_caps, val & AC_PINCTL_VREFEN);
2562 if (idx == ucontrol->value.enumerated.item[0])
2563 return 0;
2564
2565 val &= ~AC_PINCTL_VREFEN;
2566 val |= get_vref_idx(vref_caps, ucontrol->value.enumerated.item[0]);
2567 snd_hda_set_pin_ctl_cache(codec, nid, val);
2568 return 1;
2569}
2570
2571static const struct snd_kcontrol_new in_jack_mode_enum = {
2572 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2573 .info = in_jack_mode_info,
2574 .get = in_jack_mode_get,
2575 .put = in_jack_mode_put,
2576};
2577
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002578static int get_in_jack_num_items(struct hda_codec *codec, hda_nid_t pin)
2579{
2580 struct hda_gen_spec *spec = codec->spec;
2581 int nitems = 0;
Takashi Iwaif811c3c2013-03-07 18:32:59 +01002582 if (spec->add_jack_modes)
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002583 nitems = hweight32(get_vref_caps(codec, pin));
2584 return nitems ? nitems : 1;
2585}
2586
Takashi Iwai294765582013-01-17 09:52:11 +01002587static int create_in_jack_mode(struct hda_codec *codec, hda_nid_t pin)
2588{
2589 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai294765582013-01-17 09:52:11 +01002590 struct snd_kcontrol_new *knew;
2591 char name[44];
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002592 unsigned int defcfg;
2593
Takashi Iwaif811c3c2013-03-07 18:32:59 +01002594 if (pin == spec->hp_mic_pin)
2595 return 0; /* already done in create_out_jack_mode() */
Takashi Iwai294765582013-01-17 09:52:11 +01002596
2597 /* no jack mode for fixed pins */
2598 defcfg = snd_hda_codec_get_pincfg(codec, pin);
2599 if (snd_hda_get_input_pin_attr(defcfg) == INPUT_PIN_ATTR_INT)
2600 return 0;
2601
2602 /* no multiple vref caps? */
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002603 if (get_in_jack_num_items(codec, pin) <= 1)
Takashi Iwai294765582013-01-17 09:52:11 +01002604 return 0;
2605
2606 get_jack_mode_name(codec, pin, name, sizeof(name));
2607 knew = snd_hda_gen_add_kctl(spec, name, &in_jack_mode_enum);
2608 if (!knew)
2609 return -ENOMEM;
2610 knew->private_value = pin;
2611 return 0;
2612}
2613
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002614/*
2615 * HP/mic shared jack mode
2616 */
2617static int hp_mic_jack_mode_info(struct snd_kcontrol *kcontrol,
2618 struct snd_ctl_elem_info *uinfo)
2619{
2620 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2621 hda_nid_t nid = kcontrol->private_value;
2622 int out_jacks = get_out_jack_num_items(codec, nid);
2623 int in_jacks = get_in_jack_num_items(codec, nid);
2624 const char *text = NULL;
2625 int idx;
2626
2627 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2628 uinfo->count = 1;
2629 uinfo->value.enumerated.items = out_jacks + in_jacks;
2630 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2631 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
2632 idx = uinfo->value.enumerated.item;
2633 if (idx < out_jacks) {
2634 if (out_jacks > 1)
2635 text = out_jack_texts[idx];
2636 else
2637 text = "Headphone Out";
2638 } else {
2639 idx -= out_jacks;
2640 if (in_jacks > 1) {
2641 unsigned int vref_caps = get_vref_caps(codec, nid);
2642 text = vref_texts[get_vref_idx(vref_caps, idx)];
2643 } else
2644 text = "Mic In";
2645 }
2646
2647 strcpy(uinfo->value.enumerated.name, text);
2648 return 0;
2649}
2650
2651static int get_cur_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t nid)
2652{
2653 int out_jacks = get_out_jack_num_items(codec, nid);
2654 int in_jacks = get_in_jack_num_items(codec, nid);
2655 unsigned int val = snd_hda_codec_get_pin_target(codec, nid);
2656 int idx = 0;
2657
2658 if (val & PIN_OUT) {
2659 if (out_jacks > 1 && val == PIN_HP)
2660 idx = 1;
2661 } else if (val & PIN_IN) {
2662 idx = out_jacks;
2663 if (in_jacks > 1) {
2664 unsigned int vref_caps = get_vref_caps(codec, nid);
2665 val &= AC_PINCTL_VREFEN;
2666 idx += cvt_from_vref_idx(vref_caps, val);
2667 }
2668 }
2669 return idx;
2670}
2671
2672static int hp_mic_jack_mode_get(struct snd_kcontrol *kcontrol,
2673 struct snd_ctl_elem_value *ucontrol)
2674{
2675 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2676 hda_nid_t nid = kcontrol->private_value;
2677 ucontrol->value.enumerated.item[0] =
2678 get_cur_hp_mic_jack_mode(codec, nid);
2679 return 0;
2680}
2681
2682static int hp_mic_jack_mode_put(struct snd_kcontrol *kcontrol,
2683 struct snd_ctl_elem_value *ucontrol)
2684{
2685 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2686 hda_nid_t nid = kcontrol->private_value;
2687 int out_jacks = get_out_jack_num_items(codec, nid);
2688 int in_jacks = get_in_jack_num_items(codec, nid);
2689 unsigned int val, oldval, idx;
2690
2691 oldval = get_cur_hp_mic_jack_mode(codec, nid);
2692 idx = ucontrol->value.enumerated.item[0];
2693 if (oldval == idx)
2694 return 0;
2695
2696 if (idx < out_jacks) {
2697 if (out_jacks > 1)
2698 val = idx ? PIN_HP : PIN_OUT;
2699 else
2700 val = PIN_HP;
2701 } else {
2702 idx -= out_jacks;
2703 if (in_jacks > 1) {
2704 unsigned int vref_caps = get_vref_caps(codec, nid);
2705 val = snd_hda_codec_get_pin_target(codec, nid);
Takashi Iwai3f550e32013-03-07 18:30:27 +01002706 val &= ~(AC_PINCTL_VREFEN | PIN_HP);
2707 val |= get_vref_idx(vref_caps, idx) | PIN_IN;
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002708 } else
2709 val = snd_hda_get_default_vref(codec, nid);
2710 }
2711 snd_hda_set_pin_ctl_cache(codec, nid, val);
Takashi Iwai8ba955c2013-03-07 18:40:58 +01002712 update_hp_automute_hook(codec);
2713
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002714 return 1;
2715}
2716
2717static const struct snd_kcontrol_new hp_mic_jack_mode_enum = {
2718 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2719 .info = hp_mic_jack_mode_info,
2720 .get = hp_mic_jack_mode_get,
2721 .put = hp_mic_jack_mode_put,
2722};
2723
2724static int create_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t pin)
2725{
2726 struct hda_gen_spec *spec = codec->spec;
2727 struct snd_kcontrol_new *knew;
2728
2729 if (get_out_jack_num_items(codec, pin) <= 1 &&
2730 get_in_jack_num_items(codec, pin) <= 1)
2731 return 0; /* no need */
2732 knew = snd_hda_gen_add_kctl(spec, "Headphone Mic Jack Mode",
2733 &hp_mic_jack_mode_enum);
2734 if (!knew)
2735 return -ENOMEM;
2736 knew->private_value = pin;
Takashi Iwai8ba955c2013-03-07 18:40:58 +01002737 spec->hp_mic_jack_modes = 1;
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002738 return 0;
2739}
Takashi Iwai352f7f92012-12-19 12:52:06 +01002740
2741/*
2742 * Parse input paths
2743 */
2744
Takashi Iwai352f7f92012-12-19 12:52:06 +01002745/* add the powersave loopback-list entry */
Takashi Iwai0186f4f2013-02-07 10:45:11 +01002746static int add_loopback_list(struct hda_gen_spec *spec, hda_nid_t mix, int idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002747{
2748 struct hda_amp_list *list;
2749
Takashi Iwai0186f4f2013-02-07 10:45:11 +01002750 list = snd_array_new(&spec->loopback_list);
2751 if (!list)
2752 return -ENOMEM;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002753 list->nid = mix;
2754 list->dir = HDA_INPUT;
2755 list->idx = idx;
Takashi Iwai0186f4f2013-02-07 10:45:11 +01002756 spec->loopback.amplist = spec->loopback_list.list;
2757 return 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02002758}
Takashi Iwaicb53c622007-08-10 17:21:45 +02002759
Takashi Iwai352f7f92012-12-19 12:52:06 +01002760/* create input playback/capture controls for the given pin */
Takashi Iwai196c17662013-01-04 15:01:40 +01002761static int new_analog_input(struct hda_codec *codec, int input_idx,
2762 hda_nid_t pin, const char *ctlname, int ctlidx,
Takashi Iwai352f7f92012-12-19 12:52:06 +01002763 hda_nid_t mix_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002765 struct hda_gen_spec *spec = codec->spec;
2766 struct nid_path *path;
2767 unsigned int val;
2768 int err, idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769
Takashi Iwai352f7f92012-12-19 12:52:06 +01002770 if (!nid_has_volume(codec, mix_nid, HDA_INPUT) &&
2771 !nid_has_mute(codec, mix_nid, HDA_INPUT))
2772 return 0; /* no need for analog loopback */
2773
Takashi Iwai3ca529d2013-01-07 17:25:08 +01002774 path = snd_hda_add_new_path(codec, pin, mix_nid, 0);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002775 if (!path)
2776 return -EINVAL;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01002777 print_nid_path("loopback", path);
Takashi Iwai196c17662013-01-04 15:01:40 +01002778 spec->loopback_paths[input_idx] = snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002779
2780 idx = path->idx[path->depth - 1];
2781 if (nid_has_volume(codec, mix_nid, HDA_INPUT)) {
2782 val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
2783 err = __add_pb_vol_ctrl(spec, HDA_CTL_WIDGET_VOL, ctlname, ctlidx, val);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002784 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002786 path->ctls[NID_PATH_VOL_CTL] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787 }
2788
Takashi Iwai352f7f92012-12-19 12:52:06 +01002789 if (nid_has_mute(codec, mix_nid, HDA_INPUT)) {
2790 val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
2791 err = __add_pb_sw_ctrl(spec, HDA_CTL_WIDGET_MUTE, ctlname, ctlidx, val);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002792 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002794 path->ctls[NID_PATH_MUTE_CTL] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795 }
2796
Takashi Iwai352f7f92012-12-19 12:52:06 +01002797 path->active = true;
Takashi Iwai0186f4f2013-02-07 10:45:11 +01002798 err = add_loopback_list(spec, mix_nid, idx);
2799 if (err < 0)
2800 return err;
Takashi Iwaie4a395e2013-01-23 17:00:31 +01002801
2802 if (spec->mixer_nid != spec->mixer_merge_nid &&
2803 !spec->loopback_merge_path) {
2804 path = snd_hda_add_new_path(codec, spec->mixer_nid,
2805 spec->mixer_merge_nid, 0);
2806 if (path) {
2807 print_nid_path("loopback-merge", path);
2808 path->active = true;
2809 spec->loopback_merge_path =
2810 snd_hda_get_path_idx(codec, path);
2811 }
2812 }
2813
Takashi Iwai352f7f92012-12-19 12:52:06 +01002814 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815}
2816
Takashi Iwai352f7f92012-12-19 12:52:06 +01002817static int is_input_pin(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002819 unsigned int pincap = snd_hda_query_pin_caps(codec, nid);
2820 return (pincap & AC_PINCAP_IN) != 0;
2821}
2822
2823/* Parse the codec tree and retrieve ADCs */
2824static int fill_adc_nids(struct hda_codec *codec)
2825{
2826 struct hda_gen_spec *spec = codec->spec;
2827 hda_nid_t nid;
2828 hda_nid_t *adc_nids = spec->adc_nids;
2829 int max_nums = ARRAY_SIZE(spec->adc_nids);
2830 int i, nums = 0;
2831
2832 nid = codec->start_nid;
2833 for (i = 0; i < codec->num_nodes; i++, nid++) {
2834 unsigned int caps = get_wcaps(codec, nid);
2835 int type = get_wcaps_type(caps);
2836
2837 if (type != AC_WID_AUD_IN || (caps & AC_WCAP_DIGITAL))
2838 continue;
2839 adc_nids[nums] = nid;
2840 if (++nums >= max_nums)
2841 break;
2842 }
2843 spec->num_adc_nids = nums;
Takashi Iwai0ffd5342013-01-17 15:53:29 +01002844
2845 /* copy the detected ADCs to all_adcs[] */
2846 spec->num_all_adcs = nums;
2847 memcpy(spec->all_adcs, spec->adc_nids, nums * sizeof(hda_nid_t));
2848
Takashi Iwai352f7f92012-12-19 12:52:06 +01002849 return nums;
2850}
2851
2852/* filter out invalid adc_nids that don't give all active input pins;
2853 * if needed, check whether dynamic ADC-switching is available
2854 */
2855static int check_dyn_adc_switch(struct hda_codec *codec)
2856{
2857 struct hda_gen_spec *spec = codec->spec;
2858 struct hda_input_mux *imux = &spec->input_mux;
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002859 unsigned int ok_bits;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002860 int i, n, nums;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002861
Takashi Iwai352f7f92012-12-19 12:52:06 +01002862 nums = 0;
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002863 ok_bits = 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002864 for (n = 0; n < spec->num_adc_nids; n++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002865 for (i = 0; i < imux->num_items; i++) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002866 if (!spec->input_paths[i][n])
Takashi Iwai352f7f92012-12-19 12:52:06 +01002867 break;
2868 }
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002869 if (i >= imux->num_items) {
2870 ok_bits |= (1 << n);
2871 nums++;
2872 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002873 }
2874
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002875 if (!ok_bits) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002876 /* check whether ADC-switch is possible */
2877 for (i = 0; i < imux->num_items; i++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002878 for (n = 0; n < spec->num_adc_nids; n++) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002879 if (spec->input_paths[i][n]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002880 spec->dyn_adc_idx[i] = n;
2881 break;
2882 }
2883 }
2884 }
2885
2886 snd_printdd("hda-codec: enabling ADC switching\n");
2887 spec->dyn_adc_switch = 1;
2888 } else if (nums != spec->num_adc_nids) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002889 /* shrink the invalid adcs and input paths */
2890 nums = 0;
2891 for (n = 0; n < spec->num_adc_nids; n++) {
2892 if (!(ok_bits & (1 << n)))
2893 continue;
2894 if (n != nums) {
2895 spec->adc_nids[nums] = spec->adc_nids[n];
Takashi Iwai980428c2013-01-09 09:28:20 +01002896 for (i = 0; i < imux->num_items; i++) {
2897 invalidate_nid_path(codec,
2898 spec->input_paths[i][nums]);
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002899 spec->input_paths[i][nums] =
2900 spec->input_paths[i][n];
Takashi Iwai980428c2013-01-09 09:28:20 +01002901 }
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002902 }
2903 nums++;
2904 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002905 spec->num_adc_nids = nums;
2906 }
2907
Takashi Iwai967303d2013-02-19 17:12:42 +01002908 if (imux->num_items == 1 ||
2909 (imux->num_items == 2 && spec->hp_mic)) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002910 snd_printdd("hda-codec: reducing to a single ADC\n");
2911 spec->num_adc_nids = 1; /* reduce to a single ADC */
2912 }
2913
2914 /* single index for individual volumes ctls */
2915 if (!spec->dyn_adc_switch && spec->multi_cap_vol)
2916 spec->num_adc_nids = 1;
2917
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918 return 0;
2919}
2920
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002921/* parse capture source paths from the given pin and create imux items */
2922static int parse_capture_source(struct hda_codec *codec, hda_nid_t pin,
Takashi Iwai9dba2052013-01-18 10:01:15 +01002923 int cfg_idx, int num_adcs,
2924 const char *label, int anchor)
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002925{
2926 struct hda_gen_spec *spec = codec->spec;
2927 struct hda_input_mux *imux = &spec->input_mux;
2928 int imux_idx = imux->num_items;
2929 bool imux_added = false;
2930 int c;
2931
2932 for (c = 0; c < num_adcs; c++) {
2933 struct nid_path *path;
2934 hda_nid_t adc = spec->adc_nids[c];
2935
2936 if (!is_reachable_path(codec, pin, adc))
2937 continue;
2938 path = snd_hda_add_new_path(codec, pin, adc, anchor);
2939 if (!path)
2940 continue;
2941 print_nid_path("input", path);
2942 spec->input_paths[imux_idx][c] =
2943 snd_hda_get_path_idx(codec, path);
2944
2945 if (!imux_added) {
Takashi Iwai967303d2013-02-19 17:12:42 +01002946 if (spec->hp_mic_pin == pin)
2947 spec->hp_mic_mux_idx = imux->num_items;
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002948 spec->imux_pins[imux->num_items] = pin;
Takashi Iwai9dba2052013-01-18 10:01:15 +01002949 snd_hda_add_imux_item(imux, label, cfg_idx, NULL);
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002950 imux_added = true;
2951 }
2952 }
2953
2954 return 0;
2955}
2956
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01002958 * create playback/capture controls for input pins
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959 */
Takashi Iwai9dba2052013-01-18 10:01:15 +01002960
Takashi Iwaic9700422013-01-18 10:17:30 +01002961/* fill the label for each input at first */
2962static int fill_input_pin_labels(struct hda_codec *codec)
2963{
2964 struct hda_gen_spec *spec = codec->spec;
2965 const struct auto_pin_cfg *cfg = &spec->autocfg;
2966 int i;
2967
2968 for (i = 0; i < cfg->num_inputs; i++) {
2969 hda_nid_t pin = cfg->inputs[i].pin;
2970 const char *label;
2971 int j, idx;
2972
2973 if (!is_input_pin(codec, pin))
2974 continue;
2975
2976 label = hda_get_autocfg_input_label(codec, cfg, i);
2977 idx = 0;
David Henningsson8e8db7f2013-01-18 15:43:02 +01002978 for (j = i - 1; j >= 0; j--) {
Takashi Iwaic9700422013-01-18 10:17:30 +01002979 if (spec->input_labels[j] &&
2980 !strcmp(spec->input_labels[j], label)) {
2981 idx = spec->input_label_idxs[j] + 1;
2982 break;
2983 }
2984 }
2985
2986 spec->input_labels[i] = label;
2987 spec->input_label_idxs[i] = idx;
2988 }
2989
2990 return 0;
2991}
2992
Takashi Iwai9dba2052013-01-18 10:01:15 +01002993#define CFG_IDX_MIX 99 /* a dummy cfg->input idx for stereo mix */
2994
Takashi Iwai352f7f92012-12-19 12:52:06 +01002995static int create_input_ctls(struct hda_codec *codec)
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02002996{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002997 struct hda_gen_spec *spec = codec->spec;
2998 const struct auto_pin_cfg *cfg = &spec->autocfg;
2999 hda_nid_t mixer = spec->mixer_nid;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003000 int num_adcs;
Takashi Iwaic9700422013-01-18 10:17:30 +01003001 int i, err;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003002 unsigned int val;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02003003
Takashi Iwai352f7f92012-12-19 12:52:06 +01003004 num_adcs = fill_adc_nids(codec);
3005 if (num_adcs < 0)
3006 return 0;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02003007
Takashi Iwaic9700422013-01-18 10:17:30 +01003008 err = fill_input_pin_labels(codec);
3009 if (err < 0)
3010 return err;
3011
Takashi Iwai352f7f92012-12-19 12:52:06 +01003012 for (i = 0; i < cfg->num_inputs; i++) {
3013 hda_nid_t pin;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014
Takashi Iwai352f7f92012-12-19 12:52:06 +01003015 pin = cfg->inputs[i].pin;
3016 if (!is_input_pin(codec, pin))
3017 continue;
3018
Takashi Iwai2c12c302013-01-10 09:33:29 +01003019 val = PIN_IN;
3020 if (cfg->inputs[i].type == AUTO_PIN_MIC)
3021 val |= snd_hda_get_default_vref(codec, pin);
Takashi Iwai93c9d8a2013-03-11 09:48:43 +01003022 if (pin != spec->hp_mic_pin)
3023 set_pin_target(codec, pin, val, false);
Takashi Iwai2c12c302013-01-10 09:33:29 +01003024
Takashi Iwai352f7f92012-12-19 12:52:06 +01003025 if (mixer) {
3026 if (is_reachable_path(codec, pin, mixer)) {
Takashi Iwai196c17662013-01-04 15:01:40 +01003027 err = new_analog_input(codec, i, pin,
Takashi Iwaic9700422013-01-18 10:17:30 +01003028 spec->input_labels[i],
3029 spec->input_label_idxs[i],
3030 mixer);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003031 if (err < 0)
3032 return err;
3033 }
3034 }
3035
Takashi Iwaic9700422013-01-18 10:17:30 +01003036 err = parse_capture_source(codec, pin, i, num_adcs,
3037 spec->input_labels[i], -mixer);
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003038 if (err < 0)
3039 return err;
Takashi Iwai294765582013-01-17 09:52:11 +01003040
Takashi Iwaif811c3c2013-03-07 18:32:59 +01003041 if (spec->add_jack_modes) {
Takashi Iwai294765582013-01-17 09:52:11 +01003042 err = create_in_jack_mode(codec, pin);
3043 if (err < 0)
3044 return err;
3045 }
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003046 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003047
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003048 if (mixer && spec->add_stereo_mix_input) {
Takashi Iwai9dba2052013-01-18 10:01:15 +01003049 err = parse_capture_source(codec, mixer, CFG_IDX_MIX, num_adcs,
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003050 "Stereo Mix", 0);
3051 if (err < 0)
3052 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003053 }
3054
3055 return 0;
3056}
3057
3058
3059/*
3060 * input source mux
3061 */
3062
Takashi Iwaic697b712013-01-07 17:09:26 +01003063/* get the input path specified by the given adc and imux indices */
3064static struct nid_path *get_input_path(struct hda_codec *codec, int adc_idx, int imux_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003065{
3066 struct hda_gen_spec *spec = codec->spec;
David Henningssonb56fa1e2013-01-16 11:45:35 +01003067 if (imux_idx < 0 || imux_idx >= HDA_MAX_NUM_INPUTS) {
3068 snd_BUG();
3069 return NULL;
3070 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003071 if (spec->dyn_adc_switch)
3072 adc_idx = spec->dyn_adc_idx[imux_idx];
David Henningssond3d982f2013-01-18 15:43:01 +01003073 if (adc_idx < 0 || adc_idx >= AUTO_CFG_MAX_INS) {
David Henningssonb56fa1e2013-01-16 11:45:35 +01003074 snd_BUG();
3075 return NULL;
3076 }
Takashi Iwaic697b712013-01-07 17:09:26 +01003077 return snd_hda_get_path_from_idx(codec, spec->input_paths[imux_idx][adc_idx]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003078}
3079
3080static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
3081 unsigned int idx);
3082
3083static int mux_enum_info(struct snd_kcontrol *kcontrol,
3084 struct snd_ctl_elem_info *uinfo)
3085{
3086 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3087 struct hda_gen_spec *spec = codec->spec;
3088 return snd_hda_input_mux_info(&spec->input_mux, uinfo);
3089}
3090
3091static int mux_enum_get(struct snd_kcontrol *kcontrol,
3092 struct snd_ctl_elem_value *ucontrol)
3093{
3094 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3095 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai2a8d5392013-01-18 16:23:25 +01003096 /* the ctls are created at once with multiple counts */
3097 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003098
3099 ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
3100 return 0;
3101}
3102
3103static int mux_enum_put(struct snd_kcontrol *kcontrol,
3104 struct snd_ctl_elem_value *ucontrol)
3105{
3106 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Takashi Iwai2a8d5392013-01-18 16:23:25 +01003107 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003108 return mux_select(codec, adc_idx,
3109 ucontrol->value.enumerated.item[0]);
3110}
3111
Takashi Iwai352f7f92012-12-19 12:52:06 +01003112static const struct snd_kcontrol_new cap_src_temp = {
3113 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3114 .name = "Input Source",
3115 .info = mux_enum_info,
3116 .get = mux_enum_get,
3117 .put = mux_enum_put,
3118};
3119
Takashi Iwai47d46ab2012-12-20 11:48:54 +01003120/*
3121 * capture volume and capture switch ctls
3122 */
3123
Takashi Iwai352f7f92012-12-19 12:52:06 +01003124typedef int (*put_call_t)(struct snd_kcontrol *kcontrol,
3125 struct snd_ctl_elem_value *ucontrol);
3126
Takashi Iwai47d46ab2012-12-20 11:48:54 +01003127/* call the given amp update function for all amps in the imux list at once */
Takashi Iwai352f7f92012-12-19 12:52:06 +01003128static int cap_put_caller(struct snd_kcontrol *kcontrol,
3129 struct snd_ctl_elem_value *ucontrol,
3130 put_call_t func, int type)
3131{
3132 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3133 struct hda_gen_spec *spec = codec->spec;
3134 const struct hda_input_mux *imux;
3135 struct nid_path *path;
3136 int i, adc_idx, err = 0;
3137
3138 imux = &spec->input_mux;
David Henningssona053d1e2013-01-16 11:45:36 +01003139 adc_idx = kcontrol->id.index;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003140 mutex_lock(&codec->control_mutex);
Takashi Iwai47d46ab2012-12-20 11:48:54 +01003141 /* we use the cache-only update at first since multiple input paths
3142 * may shared the same amp; by updating only caches, the redundant
3143 * writes to hardware can be reduced.
3144 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01003145 codec->cached_write = 1;
3146 for (i = 0; i < imux->num_items; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01003147 path = get_input_path(codec, adc_idx, i);
3148 if (!path || !path->ctls[type])
Takashi Iwai352f7f92012-12-19 12:52:06 +01003149 continue;
3150 kcontrol->private_value = path->ctls[type];
3151 err = func(kcontrol, ucontrol);
3152 if (err < 0)
3153 goto error;
3154 }
3155 error:
3156 codec->cached_write = 0;
3157 mutex_unlock(&codec->control_mutex);
Takashi Iwaidc870f32013-01-22 15:24:30 +01003158 snd_hda_codec_flush_cache(codec); /* flush the updates */
Takashi Iwai352f7f92012-12-19 12:52:06 +01003159 if (err >= 0 && spec->cap_sync_hook)
Takashi Iwaia90229e2013-01-18 14:10:00 +01003160 spec->cap_sync_hook(codec, ucontrol);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003161 return err;
3162}
3163
3164/* capture volume ctl callbacks */
3165#define cap_vol_info snd_hda_mixer_amp_volume_info
3166#define cap_vol_get snd_hda_mixer_amp_volume_get
3167#define cap_vol_tlv snd_hda_mixer_amp_tlv
3168
3169static int cap_vol_put(struct snd_kcontrol *kcontrol,
3170 struct snd_ctl_elem_value *ucontrol)
3171{
3172 return cap_put_caller(kcontrol, ucontrol,
3173 snd_hda_mixer_amp_volume_put,
3174 NID_PATH_VOL_CTL);
3175}
3176
3177static const struct snd_kcontrol_new cap_vol_temp = {
3178 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3179 .name = "Capture Volume",
3180 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
3181 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
3182 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK),
3183 .info = cap_vol_info,
3184 .get = cap_vol_get,
3185 .put = cap_vol_put,
3186 .tlv = { .c = cap_vol_tlv },
3187};
3188
3189/* capture switch ctl callbacks */
3190#define cap_sw_info snd_ctl_boolean_stereo_info
3191#define cap_sw_get snd_hda_mixer_amp_switch_get
3192
3193static int cap_sw_put(struct snd_kcontrol *kcontrol,
3194 struct snd_ctl_elem_value *ucontrol)
3195{
Takashi Iwaia90229e2013-01-18 14:10:00 +01003196 return cap_put_caller(kcontrol, ucontrol,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003197 snd_hda_mixer_amp_switch_put,
3198 NID_PATH_MUTE_CTL);
3199}
3200
3201static const struct snd_kcontrol_new cap_sw_temp = {
3202 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3203 .name = "Capture Switch",
3204 .info = cap_sw_info,
3205 .get = cap_sw_get,
3206 .put = cap_sw_put,
3207};
3208
3209static int parse_capvol_in_path(struct hda_codec *codec, struct nid_path *path)
3210{
3211 hda_nid_t nid;
3212 int i, depth;
3213
3214 path->ctls[NID_PATH_VOL_CTL] = path->ctls[NID_PATH_MUTE_CTL] = 0;
3215 for (depth = 0; depth < 3; depth++) {
3216 if (depth >= path->depth)
3217 return -EINVAL;
3218 i = path->depth - depth - 1;
3219 nid = path->path[i];
3220 if (!path->ctls[NID_PATH_VOL_CTL]) {
3221 if (nid_has_volume(codec, nid, HDA_OUTPUT))
3222 path->ctls[NID_PATH_VOL_CTL] =
3223 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3224 else if (nid_has_volume(codec, nid, HDA_INPUT)) {
3225 int idx = path->idx[i];
3226 if (!depth && codec->single_adc_amp)
3227 idx = 0;
3228 path->ctls[NID_PATH_VOL_CTL] =
3229 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
3230 }
3231 }
3232 if (!path->ctls[NID_PATH_MUTE_CTL]) {
3233 if (nid_has_mute(codec, nid, HDA_OUTPUT))
3234 path->ctls[NID_PATH_MUTE_CTL] =
3235 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3236 else if (nid_has_mute(codec, nid, HDA_INPUT)) {
3237 int idx = path->idx[i];
3238 if (!depth && codec->single_adc_amp)
3239 idx = 0;
3240 path->ctls[NID_PATH_MUTE_CTL] =
3241 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
3242 }
3243 }
Takashi Iwai97ec5582006-03-21 11:29:07 +01003244 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245 return 0;
3246}
3247
Takashi Iwai352f7f92012-12-19 12:52:06 +01003248static bool is_inv_dmic_pin(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003249{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003250 struct hda_gen_spec *spec = codec->spec;
3251 struct auto_pin_cfg *cfg = &spec->autocfg;
3252 unsigned int val;
3253 int i;
3254
3255 if (!spec->inv_dmic_split)
3256 return false;
3257 for (i = 0; i < cfg->num_inputs; i++) {
3258 if (cfg->inputs[i].pin != nid)
3259 continue;
3260 if (cfg->inputs[i].type != AUTO_PIN_MIC)
3261 return false;
3262 val = snd_hda_codec_get_pincfg(codec, nid);
3263 return snd_hda_get_input_pin_attr(val) == INPUT_PIN_ATTR_INT;
3264 }
3265 return false;
3266}
3267
Takashi Iwaia90229e2013-01-18 14:10:00 +01003268/* capture switch put callback for a single control with hook call */
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003269static int cap_single_sw_put(struct snd_kcontrol *kcontrol,
3270 struct snd_ctl_elem_value *ucontrol)
3271{
3272 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3273 struct hda_gen_spec *spec = codec->spec;
3274 int ret;
3275
3276 ret = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
3277 if (ret < 0)
3278 return ret;
3279
Takashi Iwaia90229e2013-01-18 14:10:00 +01003280 if (spec->cap_sync_hook)
3281 spec->cap_sync_hook(codec, ucontrol);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003282
3283 return ret;
3284}
3285
Takashi Iwai352f7f92012-12-19 12:52:06 +01003286static int add_single_cap_ctl(struct hda_codec *codec, const char *label,
3287 int idx, bool is_switch, unsigned int ctl,
3288 bool inv_dmic)
3289{
3290 struct hda_gen_spec *spec = codec->spec;
3291 char tmpname[44];
3292 int type = is_switch ? HDA_CTL_WIDGET_MUTE : HDA_CTL_WIDGET_VOL;
3293 const char *sfx = is_switch ? "Switch" : "Volume";
3294 unsigned int chs = inv_dmic ? 1 : 3;
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003295 struct snd_kcontrol_new *knew;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003296
3297 if (!ctl)
3298 return 0;
3299
3300 if (label)
3301 snprintf(tmpname, sizeof(tmpname),
3302 "%s Capture %s", label, sfx);
3303 else
3304 snprintf(tmpname, sizeof(tmpname),
3305 "Capture %s", sfx);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003306 knew = add_control(spec, type, tmpname, idx,
3307 amp_val_replace_channels(ctl, chs));
3308 if (!knew)
3309 return -ENOMEM;
Takashi Iwaia90229e2013-01-18 14:10:00 +01003310 if (is_switch)
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003311 knew->put = cap_single_sw_put;
3312 if (!inv_dmic)
3313 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003314
3315 /* Make independent right kcontrol */
3316 if (label)
3317 snprintf(tmpname, sizeof(tmpname),
3318 "Inverted %s Capture %s", label, sfx);
3319 else
3320 snprintf(tmpname, sizeof(tmpname),
3321 "Inverted Capture %s", sfx);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003322 knew = add_control(spec, type, tmpname, idx,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003323 amp_val_replace_channels(ctl, 2));
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003324 if (!knew)
3325 return -ENOMEM;
Takashi Iwaia90229e2013-01-18 14:10:00 +01003326 if (is_switch)
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003327 knew->put = cap_single_sw_put;
3328 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003329}
3330
3331/* create single (and simple) capture volume and switch controls */
3332static int create_single_cap_vol_ctl(struct hda_codec *codec, int idx,
3333 unsigned int vol_ctl, unsigned int sw_ctl,
3334 bool inv_dmic)
3335{
3336 int err;
3337 err = add_single_cap_ctl(codec, NULL, idx, false, vol_ctl, inv_dmic);
3338 if (err < 0)
3339 return err;
3340 err = add_single_cap_ctl(codec, NULL, idx, true, sw_ctl, inv_dmic);
3341 if (err < 0)
3342 return err;
3343 return 0;
3344}
3345
3346/* create bound capture volume and switch controls */
3347static int create_bind_cap_vol_ctl(struct hda_codec *codec, int idx,
3348 unsigned int vol_ctl, unsigned int sw_ctl)
3349{
3350 struct hda_gen_spec *spec = codec->spec;
3351 struct snd_kcontrol_new *knew;
3352
3353 if (vol_ctl) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01003354 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_vol_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003355 if (!knew)
3356 return -ENOMEM;
3357 knew->index = idx;
3358 knew->private_value = vol_ctl;
3359 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
3360 }
3361 if (sw_ctl) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01003362 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_sw_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003363 if (!knew)
3364 return -ENOMEM;
3365 knew->index = idx;
3366 knew->private_value = sw_ctl;
3367 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
3368 }
3369 return 0;
3370}
3371
3372/* return the vol ctl when used first in the imux list */
3373static unsigned int get_first_cap_ctl(struct hda_codec *codec, int idx, int type)
3374{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003375 struct nid_path *path;
3376 unsigned int ctl;
3377 int i;
3378
Takashi Iwaic697b712013-01-07 17:09:26 +01003379 path = get_input_path(codec, 0, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003380 if (!path)
3381 return 0;
3382 ctl = path->ctls[type];
3383 if (!ctl)
3384 return 0;
3385 for (i = 0; i < idx - 1; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01003386 path = get_input_path(codec, 0, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003387 if (path && path->ctls[type] == ctl)
3388 return 0;
3389 }
3390 return ctl;
3391}
3392
3393/* create individual capture volume and switch controls per input */
3394static int create_multi_cap_vol_ctl(struct hda_codec *codec)
3395{
3396 struct hda_gen_spec *spec = codec->spec;
3397 struct hda_input_mux *imux = &spec->input_mux;
Takashi Iwaic9700422013-01-18 10:17:30 +01003398 int i, err, type;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003399
3400 for (i = 0; i < imux->num_items; i++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003401 bool inv_dmic;
Takashi Iwaic9700422013-01-18 10:17:30 +01003402 int idx;
Takashi Iwai9dba2052013-01-18 10:01:15 +01003403
Takashi Iwaic9700422013-01-18 10:17:30 +01003404 idx = imux->items[i].index;
3405 if (idx >= spec->autocfg.num_inputs)
Takashi Iwai9dba2052013-01-18 10:01:15 +01003406 continue;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003407 inv_dmic = is_inv_dmic_pin(codec, spec->imux_pins[i]);
3408
3409 for (type = 0; type < 2; type++) {
Takashi Iwaic9700422013-01-18 10:17:30 +01003410 err = add_single_cap_ctl(codec,
3411 spec->input_labels[idx],
3412 spec->input_label_idxs[idx],
3413 type,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003414 get_first_cap_ctl(codec, i, type),
3415 inv_dmic);
3416 if (err < 0)
3417 return err;
3418 }
3419 }
3420 return 0;
3421}
3422
3423static int create_capture_mixers(struct hda_codec *codec)
3424{
3425 struct hda_gen_spec *spec = codec->spec;
3426 struct hda_input_mux *imux = &spec->input_mux;
3427 int i, n, nums, err;
3428
3429 if (spec->dyn_adc_switch)
3430 nums = 1;
3431 else
3432 nums = spec->num_adc_nids;
3433
3434 if (!spec->auto_mic && imux->num_items > 1) {
3435 struct snd_kcontrol_new *knew;
Takashi Iwai624d9142012-12-19 17:41:52 +01003436 const char *name;
3437 name = nums > 1 ? "Input Source" : "Capture Source";
3438 knew = snd_hda_gen_add_kctl(spec, name, &cap_src_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003439 if (!knew)
3440 return -ENOMEM;
3441 knew->count = nums;
3442 }
3443
3444 for (n = 0; n < nums; n++) {
3445 bool multi = false;
David Henningsson99a55922013-01-16 15:58:44 +01003446 bool multi_cap_vol = spec->multi_cap_vol;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003447 bool inv_dmic = false;
3448 int vol, sw;
3449
3450 vol = sw = 0;
3451 for (i = 0; i < imux->num_items; i++) {
3452 struct nid_path *path;
Takashi Iwaic697b712013-01-07 17:09:26 +01003453 path = get_input_path(codec, n, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003454 if (!path)
3455 continue;
3456 parse_capvol_in_path(codec, path);
3457 if (!vol)
3458 vol = path->ctls[NID_PATH_VOL_CTL];
David Henningsson99a55922013-01-16 15:58:44 +01003459 else if (vol != path->ctls[NID_PATH_VOL_CTL]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003460 multi = true;
David Henningsson99a55922013-01-16 15:58:44 +01003461 if (!same_amp_caps(codec, vol,
3462 path->ctls[NID_PATH_VOL_CTL], HDA_INPUT))
3463 multi_cap_vol = true;
3464 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003465 if (!sw)
3466 sw = path->ctls[NID_PATH_MUTE_CTL];
David Henningsson99a55922013-01-16 15:58:44 +01003467 else if (sw != path->ctls[NID_PATH_MUTE_CTL]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003468 multi = true;
David Henningsson99a55922013-01-16 15:58:44 +01003469 if (!same_amp_caps(codec, sw,
3470 path->ctls[NID_PATH_MUTE_CTL], HDA_INPUT))
3471 multi_cap_vol = true;
3472 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003473 if (is_inv_dmic_pin(codec, spec->imux_pins[i]))
3474 inv_dmic = true;
3475 }
3476
3477 if (!multi)
3478 err = create_single_cap_vol_ctl(codec, n, vol, sw,
3479 inv_dmic);
David Henningsson99a55922013-01-16 15:58:44 +01003480 else if (!multi_cap_vol)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003481 err = create_bind_cap_vol_ctl(codec, n, vol, sw);
3482 else
3483 err = create_multi_cap_vol_ctl(codec);
3484 if (err < 0)
3485 return err;
3486 }
3487
3488 return 0;
3489}
3490
3491/*
3492 * add mic boosts if needed
3493 */
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003494
3495/* check whether the given amp is feasible as a boost volume */
3496static bool check_boost_vol(struct hda_codec *codec, hda_nid_t nid,
3497 int dir, int idx)
3498{
3499 unsigned int step;
3500
3501 if (!nid_has_volume(codec, nid, dir) ||
3502 is_ctl_associated(codec, nid, dir, idx, NID_PATH_VOL_CTL) ||
3503 is_ctl_associated(codec, nid, dir, idx, NID_PATH_BOOST_CTL))
3504 return false;
3505
3506 step = (query_amp_caps(codec, nid, dir) & AC_AMPCAP_STEP_SIZE)
3507 >> AC_AMPCAP_STEP_SIZE_SHIFT;
3508 if (step < 0x20)
3509 return false;
3510 return true;
3511}
3512
3513/* look for a boost amp in a widget close to the pin */
3514static unsigned int look_for_boost_amp(struct hda_codec *codec,
3515 struct nid_path *path)
3516{
3517 unsigned int val = 0;
3518 hda_nid_t nid;
3519 int depth;
3520
3521 for (depth = 0; depth < 3; depth++) {
3522 if (depth >= path->depth - 1)
3523 break;
3524 nid = path->path[depth];
3525 if (depth && check_boost_vol(codec, nid, HDA_OUTPUT, 0)) {
3526 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3527 break;
3528 } else if (check_boost_vol(codec, nid, HDA_INPUT,
3529 path->idx[depth])) {
3530 val = HDA_COMPOSE_AMP_VAL(nid, 3, path->idx[depth],
3531 HDA_INPUT);
3532 break;
3533 }
3534 }
3535
3536 return val;
3537}
3538
Takashi Iwai352f7f92012-12-19 12:52:06 +01003539static int parse_mic_boost(struct hda_codec *codec)
3540{
3541 struct hda_gen_spec *spec = codec->spec;
3542 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003543 struct hda_input_mux *imux = &spec->input_mux;
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003544 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003545
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003546 if (!spec->num_adc_nids)
3547 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003548
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003549 for (i = 0; i < imux->num_items; i++) {
3550 struct nid_path *path;
3551 unsigned int val;
3552 int idx;
3553 char boost_label[44];
David Henningsson02aba552013-01-16 15:58:43 +01003554
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003555 idx = imux->items[i].index;
3556 if (idx >= imux->num_items)
3557 continue;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003558
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003559 /* check only line-in and mic pins */
Takashi Iwai1799cdd52013-01-18 14:37:16 +01003560 if (cfg->inputs[idx].type > AUTO_PIN_LINE_IN)
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003561 continue;
3562
3563 path = get_input_path(codec, 0, i);
3564 if (!path)
3565 continue;
3566
3567 val = look_for_boost_amp(codec, path);
3568 if (!val)
3569 continue;
3570
3571 /* create a boost control */
3572 snprintf(boost_label, sizeof(boost_label),
3573 "%s Boost Volume", spec->input_labels[idx]);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003574 if (!add_control(spec, HDA_CTL_WIDGET_VOL, boost_label,
3575 spec->input_label_idxs[idx], val))
3576 return -ENOMEM;
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003577
3578 path->ctls[NID_PATH_BOOST_CTL] = val;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003579 }
3580 return 0;
3581}
3582
3583/*
3584 * parse digital I/Os and set up NIDs in BIOS auto-parse mode
3585 */
3586static void parse_digital(struct hda_codec *codec)
3587{
3588 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003589 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003590 int i, nums;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003591 hda_nid_t dig_nid, pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003592
3593 /* support multiple SPDIFs; the secondary is set up as a slave */
3594 nums = 0;
3595 for (i = 0; i < spec->autocfg.dig_outs; i++) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01003596 pin = spec->autocfg.dig_out_pins[i];
Takashi Iwai352f7f92012-12-19 12:52:06 +01003597 dig_nid = look_for_dac(codec, pin, true);
3598 if (!dig_nid)
3599 continue;
Takashi Iwai3ca529d2013-01-07 17:25:08 +01003600 path = snd_hda_add_new_path(codec, dig_nid, pin, 0);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003601 if (!path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003602 continue;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003603 print_nid_path("digout", path);
Takashi Iwaie1284af2013-01-03 16:33:02 +01003604 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +01003605 spec->digout_paths[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai2c12c302013-01-10 09:33:29 +01003606 set_pin_target(codec, pin, PIN_OUT, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003607 if (!nums) {
3608 spec->multiout.dig_out_nid = dig_nid;
3609 spec->dig_out_type = spec->autocfg.dig_out_type[0];
3610 } else {
3611 spec->multiout.slave_dig_outs = spec->slave_dig_outs;
3612 if (nums >= ARRAY_SIZE(spec->slave_dig_outs) - 1)
3613 break;
3614 spec->slave_dig_outs[nums - 1] = dig_nid;
3615 }
3616 nums++;
3617 }
3618
3619 if (spec->autocfg.dig_in_pin) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01003620 pin = spec->autocfg.dig_in_pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003621 dig_nid = codec->start_nid;
3622 for (i = 0; i < codec->num_nodes; i++, dig_nid++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003623 unsigned int wcaps = get_wcaps(codec, dig_nid);
3624 if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
3625 continue;
3626 if (!(wcaps & AC_WCAP_DIGITAL))
3627 continue;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003628 path = snd_hda_add_new_path(codec, pin, dig_nid, 0);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003629 if (path) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003630 print_nid_path("digin", path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003631 path->active = true;
3632 spec->dig_in_nid = dig_nid;
Takashi Iwai2430d7b2013-01-04 15:09:42 +01003633 spec->digin_path = snd_hda_get_path_idx(codec, path);
Takashi Iwai2c12c302013-01-10 09:33:29 +01003634 set_pin_target(codec, pin, PIN_IN, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003635 break;
3636 }
3637 }
3638 }
3639}
3640
3641
3642/*
3643 * input MUX handling
3644 */
3645
3646static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur);
3647
3648/* select the given imux item; either unmute exclusively or select the route */
3649static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
3650 unsigned int idx)
3651{
3652 struct hda_gen_spec *spec = codec->spec;
3653 const struct hda_input_mux *imux;
Takashi Iwai55196ff2013-01-24 17:32:56 +01003654 struct nid_path *old_path, *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003655
3656 imux = &spec->input_mux;
3657 if (!imux->num_items)
3658 return 0;
3659
3660 if (idx >= imux->num_items)
3661 idx = imux->num_items - 1;
3662 if (spec->cur_mux[adc_idx] == idx)
3663 return 0;
3664
Takashi Iwai55196ff2013-01-24 17:32:56 +01003665 old_path = get_input_path(codec, adc_idx, spec->cur_mux[adc_idx]);
3666 if (!old_path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003667 return 0;
Takashi Iwai55196ff2013-01-24 17:32:56 +01003668 if (old_path->active)
3669 snd_hda_activate_path(codec, old_path, false, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003670
3671 spec->cur_mux[adc_idx] = idx;
3672
Takashi Iwai967303d2013-02-19 17:12:42 +01003673 if (spec->hp_mic)
3674 update_hp_mic(codec, adc_idx, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003675
3676 if (spec->dyn_adc_switch)
3677 dyn_adc_pcm_resetup(codec, idx);
3678
Takashi Iwaic697b712013-01-07 17:09:26 +01003679 path = get_input_path(codec, adc_idx, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003680 if (!path)
3681 return 0;
3682 if (path->active)
3683 return 0;
3684 snd_hda_activate_path(codec, path, true, false);
3685 if (spec->cap_sync_hook)
Takashi Iwaia90229e2013-01-18 14:10:00 +01003686 spec->cap_sync_hook(codec, NULL);
Takashi Iwai55196ff2013-01-24 17:32:56 +01003687 path_power_down_sync(codec, old_path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003688 return 1;
3689}
3690
3691
3692/*
3693 * Jack detections for HP auto-mute and mic-switch
3694 */
3695
3696/* check each pin in the given array; returns true if any of them is plugged */
3697static bool detect_jacks(struct hda_codec *codec, int num_pins, hda_nid_t *pins)
3698{
3699 int i, present = 0;
3700
3701 for (i = 0; i < num_pins; i++) {
3702 hda_nid_t nid = pins[i];
3703 if (!nid)
3704 break;
Takashi Iwai0b4df932013-01-10 09:45:13 +01003705 /* don't detect pins retasked as inputs */
3706 if (snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_IN_EN)
3707 continue;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003708 present |= snd_hda_jack_detect(codec, nid);
3709 }
3710 return present;
3711}
3712
3713/* standard HP/line-out auto-mute helper */
3714static void do_automute(struct hda_codec *codec, int num_pins, hda_nid_t *pins,
Takashi Iwai2c12c302013-01-10 09:33:29 +01003715 bool mute)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003716{
3717 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003718 int i;
3719
3720 for (i = 0; i < num_pins; i++) {
3721 hda_nid_t nid = pins[i];
Takashi Iwai967303d2013-02-19 17:12:42 +01003722 unsigned int val, oldval;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003723 if (!nid)
3724 break;
Takashi Iwai967303d2013-02-19 17:12:42 +01003725 oldval = snd_hda_codec_get_pin_target(codec, nid);
3726 if (oldval & PIN_IN)
3727 continue; /* no mute for inputs */
Takashi Iwai352f7f92012-12-19 12:52:06 +01003728 /* don't reset VREF value in case it's controlling
3729 * the amp (see alc861_fixup_asus_amp_vref_0f())
3730 */
Takashi Iwai2c12c302013-01-10 09:33:29 +01003731 if (spec->keep_vref_in_automute)
Takashi Iwai967303d2013-02-19 17:12:42 +01003732 val = oldval & ~PIN_HP;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003733 else
Takashi Iwai352f7f92012-12-19 12:52:06 +01003734 val = 0;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003735 if (!mute)
Takashi Iwai967303d2013-02-19 17:12:42 +01003736 val |= oldval;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003737 /* here we call update_pin_ctl() so that the pinctl is changed
3738 * without changing the pinctl target value;
3739 * the original target value will be still referred at the
3740 * init / resume again
3741 */
3742 update_pin_ctl(codec, nid, val);
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01003743 set_pin_eapd(codec, nid, !mute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003744 }
3745}
3746
3747/* Toggle outputs muting */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003748void snd_hda_gen_update_outputs(struct hda_codec *codec)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003749{
3750 struct hda_gen_spec *spec = codec->spec;
3751 int on;
3752
3753 /* Control HP pins/amps depending on master_mute state;
3754 * in general, HP pins/amps control should be enabled in all cases,
3755 * but currently set only for master_mute, just to be safe
3756 */
Takashi Iwai967303d2013-02-19 17:12:42 +01003757 do_automute(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
Takashi Iwai2c12c302013-01-10 09:33:29 +01003758 spec->autocfg.hp_pins, spec->master_mute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003759
3760 if (!spec->automute_speaker)
3761 on = 0;
3762 else
3763 on = spec->hp_jack_present | spec->line_jack_present;
3764 on |= spec->master_mute;
Takashi Iwai47b9ddb2013-01-16 18:18:00 +01003765 spec->speaker_muted = on;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003766 do_automute(codec, ARRAY_SIZE(spec->autocfg.speaker_pins),
Takashi Iwai2c12c302013-01-10 09:33:29 +01003767 spec->autocfg.speaker_pins, on);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003768
3769 /* toggle line-out mutes if needed, too */
3770 /* if LO is a copy of either HP or Speaker, don't need to handle it */
3771 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0] ||
3772 spec->autocfg.line_out_pins[0] == spec->autocfg.speaker_pins[0])
3773 return;
3774 if (!spec->automute_lo)
3775 on = 0;
3776 else
3777 on = spec->hp_jack_present;
3778 on |= spec->master_mute;
Takashi Iwai47b9ddb2013-01-16 18:18:00 +01003779 spec->line_out_muted = on;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003780 do_automute(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
Takashi Iwai2c12c302013-01-10 09:33:29 +01003781 spec->autocfg.line_out_pins, on);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003782}
Takashi Iwai5d550e12012-12-19 15:16:44 +01003783EXPORT_SYMBOL_HDA(snd_hda_gen_update_outputs);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003784
3785static void call_update_outputs(struct hda_codec *codec)
3786{
3787 struct hda_gen_spec *spec = codec->spec;
3788 if (spec->automute_hook)
3789 spec->automute_hook(codec);
3790 else
Takashi Iwai5d550e12012-12-19 15:16:44 +01003791 snd_hda_gen_update_outputs(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003792}
3793
3794/* standard HP-automute helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003795void snd_hda_gen_hp_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003796{
3797 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai92603c52013-01-22 07:46:31 +01003798 hda_nid_t *pins = spec->autocfg.hp_pins;
3799 int num_pins = ARRAY_SIZE(spec->autocfg.hp_pins);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003800
Takashi Iwai92603c52013-01-22 07:46:31 +01003801 /* No detection for the first HP jack during indep-HP mode */
3802 if (spec->indep_hp_enabled) {
3803 pins++;
3804 num_pins--;
3805 }
3806
3807 spec->hp_jack_present = detect_jacks(codec, num_pins, pins);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003808 if (!spec->detect_hp || (!spec->automute_speaker && !spec->automute_lo))
3809 return;
3810 call_update_outputs(codec);
3811}
Takashi Iwai5d550e12012-12-19 15:16:44 +01003812EXPORT_SYMBOL_HDA(snd_hda_gen_hp_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003813
3814/* standard line-out-automute helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003815void snd_hda_gen_line_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003816{
3817 struct hda_gen_spec *spec = codec->spec;
3818
3819 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
3820 return;
3821 /* check LO jack only when it's different from HP */
3822 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0])
3823 return;
3824
3825 spec->line_jack_present =
3826 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
3827 spec->autocfg.line_out_pins);
3828 if (!spec->automute_speaker || !spec->detect_lo)
3829 return;
3830 call_update_outputs(codec);
3831}
Takashi Iwai5d550e12012-12-19 15:16:44 +01003832EXPORT_SYMBOL_HDA(snd_hda_gen_line_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003833
3834/* standard mic auto-switch helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003835void snd_hda_gen_mic_autoswitch(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003836{
3837 struct hda_gen_spec *spec = codec->spec;
3838 int i;
3839
3840 if (!spec->auto_mic)
3841 return;
3842
3843 for (i = spec->am_num_entries - 1; i > 0; i--) {
Takashi Iwai0b4df932013-01-10 09:45:13 +01003844 hda_nid_t pin = spec->am_entry[i].pin;
3845 /* don't detect pins retasked as outputs */
3846 if (snd_hda_codec_get_pin_target(codec, pin) & AC_PINCTL_OUT_EN)
3847 continue;
3848 if (snd_hda_jack_detect(codec, pin)) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003849 mux_select(codec, 0, spec->am_entry[i].idx);
3850 return;
3851 }
3852 }
3853 mux_select(codec, 0, spec->am_entry[0].idx);
3854}
Takashi Iwai5d550e12012-12-19 15:16:44 +01003855EXPORT_SYMBOL_HDA(snd_hda_gen_mic_autoswitch);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003856
Takashi Iwaia5cc2502013-01-16 18:08:55 +01003857/* update jack retasking */
3858static void update_automute_all(struct hda_codec *codec)
3859{
3860 struct hda_gen_spec *spec = codec->spec;
3861
Takashi Iwai8ba955c2013-03-07 18:40:58 +01003862 update_hp_automute_hook(codec);
Takashi Iwaia5cc2502013-01-16 18:08:55 +01003863 if (spec->line_automute_hook)
3864 spec->line_automute_hook(codec, NULL);
3865 else
3866 snd_hda_gen_line_automute(codec, NULL);
3867 if (spec->mic_autoswitch_hook)
3868 spec->mic_autoswitch_hook(codec, NULL);
3869 else
3870 snd_hda_gen_mic_autoswitch(codec, NULL);
3871}
3872
Takashi Iwai352f7f92012-12-19 12:52:06 +01003873/*
3874 * Auto-Mute mode mixer enum support
3875 */
3876static int automute_mode_info(struct snd_kcontrol *kcontrol,
3877 struct snd_ctl_elem_info *uinfo)
3878{
3879 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3880 struct hda_gen_spec *spec = codec->spec;
3881 static const char * const texts3[] = {
3882 "Disabled", "Speaker Only", "Line Out+Speaker"
Takashi Iwai071c73a2006-08-23 18:34:06 +02003883 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003884
Takashi Iwai352f7f92012-12-19 12:52:06 +01003885 if (spec->automute_speaker_possible && spec->automute_lo_possible)
3886 return snd_hda_enum_helper_info(kcontrol, uinfo, 3, texts3);
3887 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
3888}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003889
Takashi Iwai352f7f92012-12-19 12:52:06 +01003890static int automute_mode_get(struct snd_kcontrol *kcontrol,
3891 struct snd_ctl_elem_value *ucontrol)
3892{
3893 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3894 struct hda_gen_spec *spec = codec->spec;
3895 unsigned int val = 0;
3896 if (spec->automute_speaker)
3897 val++;
3898 if (spec->automute_lo)
3899 val++;
Takashi Iwai071c73a2006-08-23 18:34:06 +02003900
Takashi Iwai352f7f92012-12-19 12:52:06 +01003901 ucontrol->value.enumerated.item[0] = val;
3902 return 0;
3903}
3904
3905static int automute_mode_put(struct snd_kcontrol *kcontrol,
3906 struct snd_ctl_elem_value *ucontrol)
3907{
3908 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3909 struct hda_gen_spec *spec = codec->spec;
3910
3911 switch (ucontrol->value.enumerated.item[0]) {
3912 case 0:
3913 if (!spec->automute_speaker && !spec->automute_lo)
3914 return 0;
3915 spec->automute_speaker = 0;
3916 spec->automute_lo = 0;
3917 break;
3918 case 1:
3919 if (spec->automute_speaker_possible) {
3920 if (!spec->automute_lo && spec->automute_speaker)
3921 return 0;
3922 spec->automute_speaker = 1;
3923 spec->automute_lo = 0;
3924 } else if (spec->automute_lo_possible) {
3925 if (spec->automute_lo)
3926 return 0;
3927 spec->automute_lo = 1;
3928 } else
3929 return -EINVAL;
3930 break;
3931 case 2:
3932 if (!spec->automute_lo_possible || !spec->automute_speaker_possible)
3933 return -EINVAL;
3934 if (spec->automute_speaker && spec->automute_lo)
3935 return 0;
3936 spec->automute_speaker = 1;
3937 spec->automute_lo = 1;
3938 break;
3939 default:
3940 return -EINVAL;
3941 }
3942 call_update_outputs(codec);
3943 return 1;
3944}
3945
3946static const struct snd_kcontrol_new automute_mode_enum = {
3947 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3948 .name = "Auto-Mute Mode",
3949 .info = automute_mode_info,
3950 .get = automute_mode_get,
3951 .put = automute_mode_put,
3952};
3953
3954static int add_automute_mode_enum(struct hda_codec *codec)
3955{
3956 struct hda_gen_spec *spec = codec->spec;
3957
Takashi Iwai12c93df2012-12-19 14:38:33 +01003958 if (!snd_hda_gen_add_kctl(spec, NULL, &automute_mode_enum))
Takashi Iwai352f7f92012-12-19 12:52:06 +01003959 return -ENOMEM;
3960 return 0;
3961}
3962
3963/*
3964 * Check the availability of HP/line-out auto-mute;
3965 * Set up appropriately if really supported
3966 */
3967static int check_auto_mute_availability(struct hda_codec *codec)
3968{
3969 struct hda_gen_spec *spec = codec->spec;
3970 struct auto_pin_cfg *cfg = &spec->autocfg;
3971 int present = 0;
3972 int i, err;
3973
Takashi Iwaif72706b2013-01-16 18:20:07 +01003974 if (spec->suppress_auto_mute)
3975 return 0;
3976
Takashi Iwai352f7f92012-12-19 12:52:06 +01003977 if (cfg->hp_pins[0])
3978 present++;
3979 if (cfg->line_out_pins[0])
3980 present++;
3981 if (cfg->speaker_pins[0])
3982 present++;
3983 if (present < 2) /* need two different output types */
Takashi Iwai071c73a2006-08-23 18:34:06 +02003984 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003985
3986 if (!cfg->speaker_pins[0] &&
3987 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
3988 memcpy(cfg->speaker_pins, cfg->line_out_pins,
3989 sizeof(cfg->speaker_pins));
3990 cfg->speaker_outs = cfg->line_outs;
Takashi Iwai071c73a2006-08-23 18:34:06 +02003991 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003992
Takashi Iwai352f7f92012-12-19 12:52:06 +01003993 if (!cfg->hp_pins[0] &&
3994 cfg->line_out_type == AUTO_PIN_HP_OUT) {
3995 memcpy(cfg->hp_pins, cfg->line_out_pins,
3996 sizeof(cfg->hp_pins));
3997 cfg->hp_outs = cfg->line_outs;
3998 }
3999
4000 for (i = 0; i < cfg->hp_outs; i++) {
4001 hda_nid_t nid = cfg->hp_pins[i];
4002 if (!is_jack_detectable(codec, nid))
4003 continue;
4004 snd_printdd("hda-codec: Enable HP auto-muting on NID 0x%x\n",
4005 nid);
4006 snd_hda_jack_detect_enable_callback(codec, nid, HDA_GEN_HP_EVENT,
Takashi Iwai2e03e952013-01-03 15:55:06 +01004007 spec->hp_automute_hook ?
4008 spec->hp_automute_hook :
Takashi Iwai5d550e12012-12-19 15:16:44 +01004009 snd_hda_gen_hp_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004010 spec->detect_hp = 1;
4011 }
4012
4013 if (cfg->line_out_type == AUTO_PIN_LINE_OUT && cfg->line_outs) {
4014 if (cfg->speaker_outs)
4015 for (i = 0; i < cfg->line_outs; i++) {
4016 hda_nid_t nid = cfg->line_out_pins[i];
4017 if (!is_jack_detectable(codec, nid))
4018 continue;
4019 snd_printdd("hda-codec: Enable Line-Out auto-muting on NID 0x%x\n", nid);
4020 snd_hda_jack_detect_enable_callback(codec, nid,
4021 HDA_GEN_FRONT_EVENT,
Takashi Iwai2e03e952013-01-03 15:55:06 +01004022 spec->line_automute_hook ?
4023 spec->line_automute_hook :
Takashi Iwai5d550e12012-12-19 15:16:44 +01004024 snd_hda_gen_line_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004025 spec->detect_lo = 1;
4026 }
4027 spec->automute_lo_possible = spec->detect_hp;
4028 }
4029
4030 spec->automute_speaker_possible = cfg->speaker_outs &&
4031 (spec->detect_hp || spec->detect_lo);
4032
4033 spec->automute_lo = spec->automute_lo_possible;
4034 spec->automute_speaker = spec->automute_speaker_possible;
4035
4036 if (spec->automute_speaker_possible || spec->automute_lo_possible) {
4037 /* create a control for automute mode */
4038 err = add_automute_mode_enum(codec);
4039 if (err < 0)
4040 return err;
4041 }
4042 return 0;
4043}
4044
Takashi Iwai352f7f92012-12-19 12:52:06 +01004045/* check whether all auto-mic pins are valid; setup indices if OK */
4046static bool auto_mic_check_imux(struct hda_codec *codec)
4047{
4048 struct hda_gen_spec *spec = codec->spec;
4049 const struct hda_input_mux *imux;
4050 int i;
4051
4052 imux = &spec->input_mux;
4053 for (i = 0; i < spec->am_num_entries; i++) {
4054 spec->am_entry[i].idx =
4055 find_idx_in_nid_list(spec->am_entry[i].pin,
4056 spec->imux_pins, imux->num_items);
4057 if (spec->am_entry[i].idx < 0)
4058 return false; /* no corresponding imux */
4059 }
4060
4061 /* we don't need the jack detection for the first pin */
4062 for (i = 1; i < spec->am_num_entries; i++)
4063 snd_hda_jack_detect_enable_callback(codec,
4064 spec->am_entry[i].pin,
4065 HDA_GEN_MIC_EVENT,
Takashi Iwai2e03e952013-01-03 15:55:06 +01004066 spec->mic_autoswitch_hook ?
4067 spec->mic_autoswitch_hook :
Takashi Iwai5d550e12012-12-19 15:16:44 +01004068 snd_hda_gen_mic_autoswitch);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004069 return true;
4070}
4071
4072static int compare_attr(const void *ap, const void *bp)
4073{
4074 const struct automic_entry *a = ap;
4075 const struct automic_entry *b = bp;
4076 return (int)(a->attr - b->attr);
4077}
4078
4079/*
4080 * Check the availability of auto-mic switch;
4081 * Set up if really supported
4082 */
4083static int check_auto_mic_availability(struct hda_codec *codec)
4084{
4085 struct hda_gen_spec *spec = codec->spec;
4086 struct auto_pin_cfg *cfg = &spec->autocfg;
4087 unsigned int types;
4088 int i, num_pins;
4089
Takashi Iwaid12daf62013-01-07 16:32:11 +01004090 if (spec->suppress_auto_mic)
4091 return 0;
4092
Takashi Iwai352f7f92012-12-19 12:52:06 +01004093 types = 0;
4094 num_pins = 0;
4095 for (i = 0; i < cfg->num_inputs; i++) {
4096 hda_nid_t nid = cfg->inputs[i].pin;
4097 unsigned int attr;
4098 attr = snd_hda_codec_get_pincfg(codec, nid);
4099 attr = snd_hda_get_input_pin_attr(attr);
4100 if (types & (1 << attr))
4101 return 0; /* already occupied */
4102 switch (attr) {
4103 case INPUT_PIN_ATTR_INT:
4104 if (cfg->inputs[i].type != AUTO_PIN_MIC)
4105 return 0; /* invalid type */
4106 break;
4107 case INPUT_PIN_ATTR_UNUSED:
4108 return 0; /* invalid entry */
4109 default:
4110 if (cfg->inputs[i].type > AUTO_PIN_LINE_IN)
4111 return 0; /* invalid type */
4112 if (!spec->line_in_auto_switch &&
4113 cfg->inputs[i].type != AUTO_PIN_MIC)
4114 return 0; /* only mic is allowed */
4115 if (!is_jack_detectable(codec, nid))
4116 return 0; /* no unsol support */
4117 break;
4118 }
4119 if (num_pins >= MAX_AUTO_MIC_PINS)
4120 return 0;
4121 types |= (1 << attr);
4122 spec->am_entry[num_pins].pin = nid;
4123 spec->am_entry[num_pins].attr = attr;
4124 num_pins++;
4125 }
4126
4127 if (num_pins < 2)
4128 return 0;
4129
4130 spec->am_num_entries = num_pins;
4131 /* sort the am_entry in the order of attr so that the pin with a
4132 * higher attr will be selected when the jack is plugged.
4133 */
4134 sort(spec->am_entry, num_pins, sizeof(spec->am_entry[0]),
4135 compare_attr, NULL);
4136
4137 if (!auto_mic_check_imux(codec))
4138 return 0;
4139
4140 spec->auto_mic = 1;
4141 spec->num_adc_nids = 1;
4142 spec->cur_mux[0] = spec->am_entry[0].idx;
4143 snd_printdd("hda-codec: Enable auto-mic switch on NID 0x%x/0x%x/0x%x\n",
4144 spec->am_entry[0].pin,
4145 spec->am_entry[1].pin,
4146 spec->am_entry[2].pin);
4147
4148 return 0;
4149}
4150
Takashi Iwai55196ff2013-01-24 17:32:56 +01004151/* power_filter hook; make inactive widgets into power down */
4152static unsigned int snd_hda_gen_path_power_filter(struct hda_codec *codec,
4153 hda_nid_t nid,
4154 unsigned int power_state)
4155{
4156 if (power_state != AC_PWRST_D0)
4157 return power_state;
4158 if (get_wcaps_type(get_wcaps(codec, nid)) >= AC_WID_POWER)
4159 return power_state;
4160 if (is_active_nid(codec, nid, HDA_OUTPUT, 0))
4161 return power_state;
4162 return AC_PWRST_D3;
4163}
4164
Takashi Iwai352f7f92012-12-19 12:52:06 +01004165
Takashi Iwai9eb413e2012-12-19 14:41:21 +01004166/*
4167 * Parse the given BIOS configuration and set up the hda_gen_spec
4168 *
4169 * return 1 if successful, 0 if the proper config is not found,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004170 * or a negative error code
4171 */
4172int snd_hda_gen_parse_auto_config(struct hda_codec *codec,
Takashi Iwai9eb413e2012-12-19 14:41:21 +01004173 struct auto_pin_cfg *cfg)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004174{
4175 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004176 int err;
4177
Takashi Iwai1c70a582013-01-11 17:48:22 +01004178 parse_user_hints(codec);
4179
Takashi Iwaie4a395e2013-01-23 17:00:31 +01004180 if (spec->mixer_nid && !spec->mixer_merge_nid)
4181 spec->mixer_merge_nid = spec->mixer_nid;
4182
Takashi Iwai9eb413e2012-12-19 14:41:21 +01004183 if (cfg != &spec->autocfg) {
4184 spec->autocfg = *cfg;
4185 cfg = &spec->autocfg;
4186 }
4187
Takashi Iwai98bd1112013-03-22 14:53:50 +01004188 if (!spec->main_out_badness)
4189 spec->main_out_badness = &hda_main_out_badness;
4190 if (!spec->extra_out_badness)
4191 spec->extra_out_badness = &hda_extra_out_badness;
4192
David Henningsson6fc4cb92013-01-16 15:58:45 +01004193 fill_all_dac_nids(codec);
4194
Takashi Iwai352f7f92012-12-19 12:52:06 +01004195 if (!cfg->line_outs) {
4196 if (cfg->dig_outs || cfg->dig_in_pin) {
4197 spec->multiout.max_channels = 2;
4198 spec->no_analog = 1;
4199 goto dig_only;
4200 }
4201 return 0; /* can't find valid BIOS pin config */
4202 }
4203
4204 if (!spec->no_primary_hp &&
4205 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT &&
4206 cfg->line_outs <= cfg->hp_outs) {
4207 /* use HP as primary out */
4208 cfg->speaker_outs = cfg->line_outs;
4209 memcpy(cfg->speaker_pins, cfg->line_out_pins,
4210 sizeof(cfg->speaker_pins));
4211 cfg->line_outs = cfg->hp_outs;
4212 memcpy(cfg->line_out_pins, cfg->hp_pins, sizeof(cfg->hp_pins));
4213 cfg->hp_outs = 0;
4214 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
4215 cfg->line_out_type = AUTO_PIN_HP_OUT;
4216 }
4217
4218 err = parse_output_paths(codec);
4219 if (err < 0)
4220 return err;
4221 err = create_multi_channel_mode(codec);
4222 if (err < 0)
4223 return err;
4224 err = create_multi_out_ctls(codec, cfg);
4225 if (err < 0)
4226 return err;
4227 err = create_hp_out_ctls(codec);
4228 if (err < 0)
4229 return err;
4230 err = create_speaker_out_ctls(codec);
4231 if (err < 0)
4232 return err;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004233 err = create_indep_hp_ctls(codec);
4234 if (err < 0)
4235 return err;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01004236 err = create_loopback_mixing_ctl(codec);
4237 if (err < 0)
4238 return err;
Takashi Iwai967303d2013-02-19 17:12:42 +01004239 err = create_hp_mic(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004240 if (err < 0)
4241 return err;
4242 err = create_input_ctls(codec);
Takashi Iwaid13bd412008-07-30 15:01:45 +02004243 if (err < 0)
Takashi Iwai071c73a2006-08-23 18:34:06 +02004244 return err;
4245
Takashi Iwaia07a9492013-01-07 16:44:06 +01004246 spec->const_channel_count = spec->ext_channel_count;
4247 /* check the multiple speaker and headphone pins */
4248 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
4249 spec->const_channel_count = max(spec->const_channel_count,
4250 cfg->speaker_outs * 2);
4251 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
4252 spec->const_channel_count = max(spec->const_channel_count,
4253 cfg->hp_outs * 2);
4254 spec->multiout.max_channels = max(spec->ext_channel_count,
4255 spec->const_channel_count);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004256
4257 err = check_auto_mute_availability(codec);
4258 if (err < 0)
4259 return err;
4260
4261 err = check_dyn_adc_switch(codec);
4262 if (err < 0)
4263 return err;
4264
Takashi Iwai967303d2013-02-19 17:12:42 +01004265 err = check_auto_mic_availability(codec);
4266 if (err < 0)
4267 return err;
Takashi Iwai071c73a2006-08-23 18:34:06 +02004268
Takashi Iwai352f7f92012-12-19 12:52:06 +01004269 err = create_capture_mixers(codec);
4270 if (err < 0)
4271 return err;
4272
4273 err = parse_mic_boost(codec);
4274 if (err < 0)
4275 return err;
4276
Takashi Iwaif811c3c2013-03-07 18:32:59 +01004277 if (spec->add_jack_modes) {
Takashi Iwai978e77e2013-01-10 16:57:58 +01004278 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
4279 err = create_out_jack_modes(codec, cfg->line_outs,
4280 cfg->line_out_pins);
4281 if (err < 0)
4282 return err;
4283 }
4284 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
4285 err = create_out_jack_modes(codec, cfg->hp_outs,
4286 cfg->hp_pins);
4287 if (err < 0)
4288 return err;
4289 }
4290 }
4291
Takashi Iwai352f7f92012-12-19 12:52:06 +01004292 dig_only:
4293 parse_digital(codec);
4294
Takashi Iwai55196ff2013-01-24 17:32:56 +01004295 if (spec->power_down_unused)
4296 codec->power_filter = snd_hda_gen_path_power_filter;
4297
Takashi Iwai7504b6c2013-03-18 11:25:51 +01004298 if (!spec->no_analog && spec->beep_nid) {
4299 err = snd_hda_attach_beep_device(codec, spec->beep_nid);
4300 if (err < 0)
4301 return err;
4302 }
4303
Takashi Iwai352f7f92012-12-19 12:52:06 +01004304 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004305}
Takashi Iwai352f7f92012-12-19 12:52:06 +01004306EXPORT_SYMBOL_HDA(snd_hda_gen_parse_auto_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004307
4308
4309/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01004310 * Build control elements
Linus Torvalds1da177e2005-04-16 15:20:36 -07004311 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01004312
4313/* slave controls for virtual master */
4314static const char * const slave_pfxs[] = {
4315 "Front", "Surround", "Center", "LFE", "Side",
4316 "Headphone", "Speaker", "Mono", "Line Out",
4317 "CLFE", "Bass Speaker", "PCM",
Takashi Iwaiee79c692013-01-07 09:57:42 +01004318 "Speaker Front", "Speaker Surround", "Speaker CLFE", "Speaker Side",
4319 "Headphone Front", "Headphone Surround", "Headphone CLFE",
4320 "Headphone Side",
Takashi Iwai352f7f92012-12-19 12:52:06 +01004321 NULL,
4322};
4323
4324int snd_hda_gen_build_controls(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004325{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004326 struct hda_gen_spec *spec = codec->spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004327 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004328
Takashi Iwai36502d02012-12-19 15:15:10 +01004329 if (spec->kctls.used) {
4330 err = snd_hda_add_new_ctls(codec, spec->kctls.list);
4331 if (err < 0)
4332 return err;
4333 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004334
Takashi Iwai352f7f92012-12-19 12:52:06 +01004335 if (spec->multiout.dig_out_nid) {
4336 err = snd_hda_create_dig_out_ctls(codec,
4337 spec->multiout.dig_out_nid,
4338 spec->multiout.dig_out_nid,
4339 spec->pcm_rec[1].pcm_type);
4340 if (err < 0)
4341 return err;
4342 if (!spec->no_analog) {
4343 err = snd_hda_create_spdif_share_sw(codec,
4344 &spec->multiout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004345 if (err < 0)
4346 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004347 spec->multiout.share_spdif = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004348 }
4349 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01004350 if (spec->dig_in_nid) {
4351 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
4352 if (err < 0)
4353 return err;
4354 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004355
Takashi Iwai352f7f92012-12-19 12:52:06 +01004356 /* if we have no master control, let's create it */
4357 if (!spec->no_analog &&
4358 !snd_hda_find_mixer_ctl(codec, "Master Playback Volume")) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01004359 err = snd_hda_add_vmaster(codec, "Master Playback Volume",
Takashi Iwai7a71bbf2013-01-17 10:25:15 +01004360 spec->vmaster_tlv, slave_pfxs,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004361 "Playback Volume");
4362 if (err < 0)
4363 return err;
4364 }
4365 if (!spec->no_analog &&
4366 !snd_hda_find_mixer_ctl(codec, "Master Playback Switch")) {
4367 err = __snd_hda_add_vmaster(codec, "Master Playback Switch",
4368 NULL, slave_pfxs,
4369 "Playback Switch",
4370 true, &spec->vmaster_mute.sw_kctl);
4371 if (err < 0)
4372 return err;
4373 if (spec->vmaster_mute.hook)
Takashi Iwaifd25a972012-12-20 14:57:18 +01004374 snd_hda_add_vmaster_hook(codec, &spec->vmaster_mute,
4375 spec->vmaster_mute_enum);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004376 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004377
Takashi Iwai352f7f92012-12-19 12:52:06 +01004378 free_kctls(spec); /* no longer needed */
4379
Takashi Iwai352f7f92012-12-19 12:52:06 +01004380 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
4381 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004382 return err;
4383
4384 return 0;
4385}
Takashi Iwai352f7f92012-12-19 12:52:06 +01004386EXPORT_SYMBOL_HDA(snd_hda_gen_build_controls);
4387
Linus Torvalds1da177e2005-04-16 15:20:36 -07004388
4389/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01004390 * PCM definitions
Linus Torvalds1da177e2005-04-16 15:20:36 -07004391 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004392
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004393static void call_pcm_playback_hook(struct hda_pcm_stream *hinfo,
4394 struct hda_codec *codec,
4395 struct snd_pcm_substream *substream,
4396 int action)
4397{
4398 struct hda_gen_spec *spec = codec->spec;
4399 if (spec->pcm_playback_hook)
4400 spec->pcm_playback_hook(hinfo, codec, substream, action);
4401}
4402
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004403static void call_pcm_capture_hook(struct hda_pcm_stream *hinfo,
4404 struct hda_codec *codec,
4405 struct snd_pcm_substream *substream,
4406 int action)
4407{
4408 struct hda_gen_spec *spec = codec->spec;
4409 if (spec->pcm_capture_hook)
4410 spec->pcm_capture_hook(hinfo, codec, substream, action);
4411}
4412
Takashi Iwai352f7f92012-12-19 12:52:06 +01004413/*
4414 * Analog playback callbacks
4415 */
4416static int playback_pcm_open(struct hda_pcm_stream *hinfo,
4417 struct hda_codec *codec,
4418 struct snd_pcm_substream *substream)
4419{
4420 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004421 int err;
4422
4423 mutex_lock(&spec->pcm_mutex);
4424 err = snd_hda_multi_out_analog_open(codec,
4425 &spec->multiout, substream,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004426 hinfo);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004427 if (!err) {
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004428 spec->active_streams |= 1 << STREAM_MULTI_OUT;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004429 call_pcm_playback_hook(hinfo, codec, substream,
4430 HDA_GEN_PCM_ACT_OPEN);
4431 }
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004432 mutex_unlock(&spec->pcm_mutex);
4433 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004434}
4435
4436static int playback_pcm_prepare(struct hda_pcm_stream *hinfo,
Takashi Iwai97ec5582006-03-21 11:29:07 +01004437 struct hda_codec *codec,
4438 unsigned int stream_tag,
4439 unsigned int format,
4440 struct snd_pcm_substream *substream)
4441{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004442 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004443 int err;
4444
4445 err = snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
4446 stream_tag, format, substream);
4447 if (!err)
4448 call_pcm_playback_hook(hinfo, codec, substream,
4449 HDA_GEN_PCM_ACT_PREPARE);
4450 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004451}
Takashi Iwai97ec5582006-03-21 11:29:07 +01004452
Takashi Iwai352f7f92012-12-19 12:52:06 +01004453static int playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
4454 struct hda_codec *codec,
4455 struct snd_pcm_substream *substream)
4456{
4457 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004458 int err;
4459
4460 err = snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
4461 if (!err)
4462 call_pcm_playback_hook(hinfo, codec, substream,
4463 HDA_GEN_PCM_ACT_CLEANUP);
4464 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004465}
4466
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004467static int playback_pcm_close(struct hda_pcm_stream *hinfo,
4468 struct hda_codec *codec,
4469 struct snd_pcm_substream *substream)
4470{
4471 struct hda_gen_spec *spec = codec->spec;
4472 mutex_lock(&spec->pcm_mutex);
4473 spec->active_streams &= ~(1 << STREAM_MULTI_OUT);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004474 call_pcm_playback_hook(hinfo, codec, substream,
4475 HDA_GEN_PCM_ACT_CLOSE);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004476 mutex_unlock(&spec->pcm_mutex);
4477 return 0;
4478}
4479
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004480static int capture_pcm_open(struct hda_pcm_stream *hinfo,
4481 struct hda_codec *codec,
4482 struct snd_pcm_substream *substream)
4483{
4484 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_OPEN);
4485 return 0;
4486}
4487
4488static int capture_pcm_prepare(struct hda_pcm_stream *hinfo,
4489 struct hda_codec *codec,
4490 unsigned int stream_tag,
4491 unsigned int format,
4492 struct snd_pcm_substream *substream)
4493{
4494 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4495 call_pcm_capture_hook(hinfo, codec, substream,
4496 HDA_GEN_PCM_ACT_PREPARE);
4497 return 0;
4498}
4499
4500static int capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
4501 struct hda_codec *codec,
4502 struct snd_pcm_substream *substream)
4503{
4504 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
4505 call_pcm_capture_hook(hinfo, codec, substream,
4506 HDA_GEN_PCM_ACT_CLEANUP);
4507 return 0;
4508}
4509
4510static int capture_pcm_close(struct hda_pcm_stream *hinfo,
4511 struct hda_codec *codec,
4512 struct snd_pcm_substream *substream)
4513{
4514 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_CLOSE);
4515 return 0;
4516}
4517
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004518static int alt_playback_pcm_open(struct hda_pcm_stream *hinfo,
4519 struct hda_codec *codec,
4520 struct snd_pcm_substream *substream)
4521{
4522 struct hda_gen_spec *spec = codec->spec;
4523 int err = 0;
4524
4525 mutex_lock(&spec->pcm_mutex);
4526 if (!spec->indep_hp_enabled)
4527 err = -EBUSY;
4528 else
4529 spec->active_streams |= 1 << STREAM_INDEP_HP;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004530 call_pcm_playback_hook(hinfo, codec, substream,
4531 HDA_GEN_PCM_ACT_OPEN);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004532 mutex_unlock(&spec->pcm_mutex);
4533 return err;
4534}
4535
4536static int alt_playback_pcm_close(struct hda_pcm_stream *hinfo,
4537 struct hda_codec *codec,
4538 struct snd_pcm_substream *substream)
4539{
4540 struct hda_gen_spec *spec = codec->spec;
4541 mutex_lock(&spec->pcm_mutex);
4542 spec->active_streams &= ~(1 << STREAM_INDEP_HP);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004543 call_pcm_playback_hook(hinfo, codec, substream,
4544 HDA_GEN_PCM_ACT_CLOSE);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004545 mutex_unlock(&spec->pcm_mutex);
4546 return 0;
4547}
4548
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004549static int alt_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
4550 struct hda_codec *codec,
4551 unsigned int stream_tag,
4552 unsigned int format,
4553 struct snd_pcm_substream *substream)
4554{
4555 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4556 call_pcm_playback_hook(hinfo, codec, substream,
4557 HDA_GEN_PCM_ACT_PREPARE);
4558 return 0;
4559}
4560
4561static int alt_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
4562 struct hda_codec *codec,
4563 struct snd_pcm_substream *substream)
4564{
4565 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
4566 call_pcm_playback_hook(hinfo, codec, substream,
4567 HDA_GEN_PCM_ACT_CLEANUP);
4568 return 0;
4569}
4570
Takashi Iwai352f7f92012-12-19 12:52:06 +01004571/*
4572 * Digital out
4573 */
4574static int dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
4575 struct hda_codec *codec,
4576 struct snd_pcm_substream *substream)
4577{
4578 struct hda_gen_spec *spec = codec->spec;
4579 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
4580}
4581
4582static int dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
4583 struct hda_codec *codec,
4584 unsigned int stream_tag,
4585 unsigned int format,
4586 struct snd_pcm_substream *substream)
4587{
4588 struct hda_gen_spec *spec = codec->spec;
4589 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
4590 stream_tag, format, substream);
4591}
4592
4593static int dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
4594 struct hda_codec *codec,
4595 struct snd_pcm_substream *substream)
4596{
4597 struct hda_gen_spec *spec = codec->spec;
4598 return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
4599}
4600
4601static int dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
4602 struct hda_codec *codec,
4603 struct snd_pcm_substream *substream)
4604{
4605 struct hda_gen_spec *spec = codec->spec;
4606 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
4607}
4608
4609/*
4610 * Analog capture
4611 */
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004612#define alt_capture_pcm_open capture_pcm_open
4613#define alt_capture_pcm_close capture_pcm_close
4614
Takashi Iwai352f7f92012-12-19 12:52:06 +01004615static int alt_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
4616 struct hda_codec *codec,
4617 unsigned int stream_tag,
4618 unsigned int format,
4619 struct snd_pcm_substream *substream)
4620{
4621 struct hda_gen_spec *spec = codec->spec;
4622
4623 snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number + 1],
Takashi Iwai97ec5582006-03-21 11:29:07 +01004624 stream_tag, 0, format);
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004625 call_pcm_capture_hook(hinfo, codec, substream,
4626 HDA_GEN_PCM_ACT_PREPARE);
Takashi Iwai97ec5582006-03-21 11:29:07 +01004627 return 0;
4628}
4629
Takashi Iwai352f7f92012-12-19 12:52:06 +01004630static int alt_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
4631 struct hda_codec *codec,
4632 struct snd_pcm_substream *substream)
Takashi Iwai97ec5582006-03-21 11:29:07 +01004633{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004634 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai97ec5582006-03-21 11:29:07 +01004635
Takashi Iwai352f7f92012-12-19 12:52:06 +01004636 snd_hda_codec_cleanup_stream(codec,
4637 spec->adc_nids[substream->number + 1]);
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004638 call_pcm_capture_hook(hinfo, codec, substream,
4639 HDA_GEN_PCM_ACT_CLEANUP);
Takashi Iwai97ec5582006-03-21 11:29:07 +01004640 return 0;
4641}
4642
Takashi Iwai352f7f92012-12-19 12:52:06 +01004643/*
4644 */
4645static const struct hda_pcm_stream pcm_analog_playback = {
4646 .substreams = 1,
4647 .channels_min = 2,
4648 .channels_max = 8,
4649 /* NID is set in build_pcms */
4650 .ops = {
4651 .open = playback_pcm_open,
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004652 .close = playback_pcm_close,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004653 .prepare = playback_pcm_prepare,
4654 .cleanup = playback_pcm_cleanup
4655 },
4656};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004657
Takashi Iwai352f7f92012-12-19 12:52:06 +01004658static const struct hda_pcm_stream pcm_analog_capture = {
4659 .substreams = 1,
4660 .channels_min = 2,
4661 .channels_max = 2,
4662 /* NID is set in build_pcms */
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004663 .ops = {
4664 .open = capture_pcm_open,
4665 .close = capture_pcm_close,
4666 .prepare = capture_pcm_prepare,
4667 .cleanup = capture_pcm_cleanup
4668 },
Takashi Iwai352f7f92012-12-19 12:52:06 +01004669};
4670
4671static const struct hda_pcm_stream pcm_analog_alt_playback = {
4672 .substreams = 1,
4673 .channels_min = 2,
4674 .channels_max = 2,
4675 /* NID is set in build_pcms */
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004676 .ops = {
4677 .open = alt_playback_pcm_open,
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004678 .close = alt_playback_pcm_close,
4679 .prepare = alt_playback_pcm_prepare,
4680 .cleanup = alt_playback_pcm_cleanup
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004681 },
Takashi Iwai352f7f92012-12-19 12:52:06 +01004682};
4683
4684static const struct hda_pcm_stream pcm_analog_alt_capture = {
4685 .substreams = 2, /* can be overridden */
4686 .channels_min = 2,
4687 .channels_max = 2,
4688 /* NID is set in build_pcms */
4689 .ops = {
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004690 .open = alt_capture_pcm_open,
4691 .close = alt_capture_pcm_close,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004692 .prepare = alt_capture_pcm_prepare,
4693 .cleanup = alt_capture_pcm_cleanup
4694 },
4695};
4696
4697static const struct hda_pcm_stream pcm_digital_playback = {
4698 .substreams = 1,
4699 .channels_min = 2,
4700 .channels_max = 2,
4701 /* NID is set in build_pcms */
4702 .ops = {
4703 .open = dig_playback_pcm_open,
4704 .close = dig_playback_pcm_close,
4705 .prepare = dig_playback_pcm_prepare,
4706 .cleanup = dig_playback_pcm_cleanup
4707 },
4708};
4709
4710static const struct hda_pcm_stream pcm_digital_capture = {
4711 .substreams = 1,
4712 .channels_min = 2,
4713 .channels_max = 2,
4714 /* NID is set in build_pcms */
4715};
4716
4717/* Used by build_pcms to flag that a PCM has no playback stream */
4718static const struct hda_pcm_stream pcm_null_stream = {
4719 .substreams = 0,
4720 .channels_min = 0,
4721 .channels_max = 0,
4722};
4723
4724/*
4725 * dynamic changing ADC PCM streams
4726 */
4727static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur)
4728{
4729 struct hda_gen_spec *spec = codec->spec;
4730 hda_nid_t new_adc = spec->adc_nids[spec->dyn_adc_idx[cur]];
4731
4732 if (spec->cur_adc && spec->cur_adc != new_adc) {
4733 /* stream is running, let's swap the current ADC */
4734 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
4735 spec->cur_adc = new_adc;
4736 snd_hda_codec_setup_stream(codec, new_adc,
4737 spec->cur_adc_stream_tag, 0,
4738 spec->cur_adc_format);
4739 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004740 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01004741 return false;
4742}
4743
4744/* analog capture with dynamic dual-adc changes */
4745static int dyn_adc_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
4746 struct hda_codec *codec,
4747 unsigned int stream_tag,
4748 unsigned int format,
4749 struct snd_pcm_substream *substream)
4750{
4751 struct hda_gen_spec *spec = codec->spec;
4752 spec->cur_adc = spec->adc_nids[spec->dyn_adc_idx[spec->cur_mux[0]]];
4753 spec->cur_adc_stream_tag = stream_tag;
4754 spec->cur_adc_format = format;
4755 snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
4756 return 0;
4757}
4758
4759static int dyn_adc_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
4760 struct hda_codec *codec,
4761 struct snd_pcm_substream *substream)
4762{
4763 struct hda_gen_spec *spec = codec->spec;
4764 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
4765 spec->cur_adc = 0;
4766 return 0;
4767}
4768
4769static const struct hda_pcm_stream dyn_adc_pcm_analog_capture = {
4770 .substreams = 1,
4771 .channels_min = 2,
4772 .channels_max = 2,
4773 .nid = 0, /* fill later */
4774 .ops = {
4775 .prepare = dyn_adc_capture_pcm_prepare,
4776 .cleanup = dyn_adc_capture_pcm_cleanup
4777 },
4778};
4779
Takashi Iwaif873e532012-12-20 16:58:39 +01004780static void fill_pcm_stream_name(char *str, size_t len, const char *sfx,
4781 const char *chip_name)
4782{
4783 char *p;
4784
4785 if (*str)
4786 return;
4787 strlcpy(str, chip_name, len);
4788
4789 /* drop non-alnum chars after a space */
4790 for (p = strchr(str, ' '); p; p = strchr(p + 1, ' ')) {
4791 if (!isalnum(p[1])) {
4792 *p = 0;
4793 break;
4794 }
4795 }
4796 strlcat(str, sfx, len);
4797}
4798
Takashi Iwai352f7f92012-12-19 12:52:06 +01004799/* build PCM streams based on the parsed results */
4800int snd_hda_gen_build_pcms(struct hda_codec *codec)
4801{
4802 struct hda_gen_spec *spec = codec->spec;
4803 struct hda_pcm *info = spec->pcm_rec;
4804 const struct hda_pcm_stream *p;
4805 bool have_multi_adcs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004806
4807 codec->num_pcms = 1;
4808 codec->pcm_info = info;
4809
Takashi Iwai352f7f92012-12-19 12:52:06 +01004810 if (spec->no_analog)
4811 goto skip_analog;
4812
Takashi Iwaif873e532012-12-20 16:58:39 +01004813 fill_pcm_stream_name(spec->stream_name_analog,
4814 sizeof(spec->stream_name_analog),
4815 " Analog", codec->chip_name);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004816 info->name = spec->stream_name_analog;
4817
4818 if (spec->multiout.num_dacs > 0) {
4819 p = spec->stream_analog_playback;
4820 if (!p)
4821 p = &pcm_analog_playback;
4822 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
4823 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dac_nids[0];
4824 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
4825 spec->multiout.max_channels;
4826 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT &&
4827 spec->autocfg.line_outs == 2)
4828 info->stream[SNDRV_PCM_STREAM_PLAYBACK].chmap =
4829 snd_pcm_2_1_chmaps;
4830 }
4831 if (spec->num_adc_nids) {
4832 p = spec->stream_analog_capture;
4833 if (!p) {
4834 if (spec->dyn_adc_switch)
4835 p = &dyn_adc_pcm_analog_capture;
4836 else
4837 p = &pcm_analog_capture;
4838 }
4839 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
4840 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adc_nids[0];
4841 }
4842
Takashi Iwai352f7f92012-12-19 12:52:06 +01004843 skip_analog:
4844 /* SPDIF for stream index #1 */
4845 if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
Takashi Iwaif873e532012-12-20 16:58:39 +01004846 fill_pcm_stream_name(spec->stream_name_digital,
4847 sizeof(spec->stream_name_digital),
4848 " Digital", codec->chip_name);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004849 codec->num_pcms = 2;
4850 codec->slave_dig_outs = spec->multiout.slave_dig_outs;
4851 info = spec->pcm_rec + 1;
4852 info->name = spec->stream_name_digital;
4853 if (spec->dig_out_type)
4854 info->pcm_type = spec->dig_out_type;
4855 else
4856 info->pcm_type = HDA_PCM_TYPE_SPDIF;
4857 if (spec->multiout.dig_out_nid) {
4858 p = spec->stream_digital_playback;
4859 if (!p)
4860 p = &pcm_digital_playback;
4861 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
4862 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dig_out_nid;
4863 }
4864 if (spec->dig_in_nid) {
4865 p = spec->stream_digital_capture;
4866 if (!p)
4867 p = &pcm_digital_capture;
4868 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
4869 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in_nid;
4870 }
4871 }
4872
4873 if (spec->no_analog)
4874 return 0;
4875
4876 /* If the use of more than one ADC is requested for the current
4877 * model, configure a second analog capture-only PCM.
4878 */
4879 have_multi_adcs = (spec->num_adc_nids > 1) &&
4880 !spec->dyn_adc_switch && !spec->auto_mic;
4881 /* Additional Analaog capture for index #2 */
4882 if (spec->alt_dac_nid || have_multi_adcs) {
Takashi Iwaia6071482013-01-21 16:50:09 +01004883 fill_pcm_stream_name(spec->stream_name_alt_analog,
4884 sizeof(spec->stream_name_alt_analog),
4885 " Alt Analog", codec->chip_name);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004886 codec->num_pcms = 3;
4887 info = spec->pcm_rec + 2;
Takashi Iwaia6071482013-01-21 16:50:09 +01004888 info->name = spec->stream_name_alt_analog;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004889 if (spec->alt_dac_nid) {
4890 p = spec->stream_analog_alt_playback;
4891 if (!p)
4892 p = &pcm_analog_alt_playback;
4893 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
4894 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
4895 spec->alt_dac_nid;
4896 } else {
4897 info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
4898 pcm_null_stream;
4899 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = 0;
4900 }
4901 if (have_multi_adcs) {
4902 p = spec->stream_analog_alt_capture;
4903 if (!p)
4904 p = &pcm_analog_alt_capture;
4905 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
4906 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
4907 spec->adc_nids[1];
4908 info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams =
4909 spec->num_adc_nids - 1;
4910 } else {
4911 info->stream[SNDRV_PCM_STREAM_CAPTURE] =
4912 pcm_null_stream;
4913 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = 0;
4914 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004915 }
4916
4917 return 0;
4918}
Takashi Iwai352f7f92012-12-19 12:52:06 +01004919EXPORT_SYMBOL_HDA(snd_hda_gen_build_pcms);
4920
4921
4922/*
4923 * Standard auto-parser initializations
4924 */
4925
Takashi Iwaid4156932013-01-07 10:08:02 +01004926/* configure the given path as a proper output */
Takashi Iwai2c12c302013-01-10 09:33:29 +01004927static void set_output_and_unmute(struct hda_codec *codec, int path_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004928{
4929 struct nid_path *path;
Takashi Iwaid4156932013-01-07 10:08:02 +01004930 hda_nid_t pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004931
Takashi Iwai196c17662013-01-04 15:01:40 +01004932 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwaid4156932013-01-07 10:08:02 +01004933 if (!path || !path->depth)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004934 return;
Takashi Iwaid4156932013-01-07 10:08:02 +01004935 pin = path->path[path->depth - 1];
Takashi Iwai2c12c302013-01-10 09:33:29 +01004936 restore_pin_ctl(codec, pin);
Takashi Iwai65033cc2013-04-16 12:31:05 +02004937 snd_hda_activate_path(codec, path, path->active,
4938 aamix_default(codec->spec));
Takashi Iwaie1284af2013-01-03 16:33:02 +01004939 set_pin_eapd(codec, pin, path->active);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004940}
4941
4942/* initialize primary output paths */
4943static void init_multi_out(struct hda_codec *codec)
4944{
4945 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004946 int i;
4947
Takashi Iwaid4156932013-01-07 10:08:02 +01004948 for (i = 0; i < spec->autocfg.line_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01004949 set_output_and_unmute(codec, spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004950}
4951
Takashi Iwaidb23fd12012-12-20 15:27:24 +01004952
Takashi Iwai2c12c302013-01-10 09:33:29 +01004953static void __init_extra_out(struct hda_codec *codec, int num_outs, int *paths)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004954{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004955 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004956
Takashi Iwaid4156932013-01-07 10:08:02 +01004957 for (i = 0; i < num_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01004958 set_output_and_unmute(codec, paths[i]);
Takashi Iwaidb23fd12012-12-20 15:27:24 +01004959}
4960
4961/* initialize hp and speaker paths */
4962static void init_extra_out(struct hda_codec *codec)
4963{
4964 struct hda_gen_spec *spec = codec->spec;
4965
4966 if (spec->autocfg.line_out_type != AUTO_PIN_HP_OUT)
Takashi Iwai2c12c302013-01-10 09:33:29 +01004967 __init_extra_out(codec, spec->autocfg.hp_outs, spec->hp_paths);
Takashi Iwaidb23fd12012-12-20 15:27:24 +01004968 if (spec->autocfg.line_out_type != AUTO_PIN_SPEAKER_OUT)
4969 __init_extra_out(codec, spec->autocfg.speaker_outs,
Takashi Iwai2c12c302013-01-10 09:33:29 +01004970 spec->speaker_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004971}
4972
4973/* initialize multi-io paths */
4974static void init_multi_io(struct hda_codec *codec)
4975{
4976 struct hda_gen_spec *spec = codec->spec;
4977 int i;
4978
4979 for (i = 0; i < spec->multi_ios; i++) {
4980 hda_nid_t pin = spec->multi_io[i].pin;
4981 struct nid_path *path;
Takashi Iwai196c17662013-01-04 15:01:40 +01004982 path = get_multiio_path(codec, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004983 if (!path)
4984 continue;
4985 if (!spec->multi_io[i].ctl_in)
4986 spec->multi_io[i].ctl_in =
Takashi Iwai2c12c302013-01-10 09:33:29 +01004987 snd_hda_codec_get_pin_target(codec, pin);
Takashi Iwai65033cc2013-04-16 12:31:05 +02004988 snd_hda_activate_path(codec, path, path->active,
4989 aamix_default(spec));
Takashi Iwai352f7f92012-12-19 12:52:06 +01004990 }
4991}
4992
Takashi Iwai352f7f92012-12-19 12:52:06 +01004993/* set up input pins and loopback paths */
4994static void init_analog_input(struct hda_codec *codec)
4995{
4996 struct hda_gen_spec *spec = codec->spec;
4997 struct auto_pin_cfg *cfg = &spec->autocfg;
4998 int i;
4999
5000 for (i = 0; i < cfg->num_inputs; i++) {
5001 hda_nid_t nid = cfg->inputs[i].pin;
5002 if (is_input_pin(codec, nid))
Takashi Iwai2c12c302013-01-10 09:33:29 +01005003 restore_pin_ctl(codec, nid);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005004
5005 /* init loopback inputs */
5006 if (spec->mixer_nid) {
Takashi Iwai3e367f12013-01-23 17:07:23 +01005007 resume_path_from_idx(codec, spec->loopback_paths[i]);
5008 resume_path_from_idx(codec, spec->loopback_merge_path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005009 }
5010 }
5011}
5012
5013/* initialize ADC paths */
5014static void init_input_src(struct hda_codec *codec)
5015{
5016 struct hda_gen_spec *spec = codec->spec;
5017 struct hda_input_mux *imux = &spec->input_mux;
5018 struct nid_path *path;
5019 int i, c, nums;
5020
5021 if (spec->dyn_adc_switch)
5022 nums = 1;
5023 else
5024 nums = spec->num_adc_nids;
5025
5026 for (c = 0; c < nums; c++) {
5027 for (i = 0; i < imux->num_items; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01005028 path = get_input_path(codec, c, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005029 if (path) {
5030 bool active = path->active;
5031 if (i == spec->cur_mux[c])
5032 active = true;
5033 snd_hda_activate_path(codec, path, active, false);
5034 }
5035 }
Takashi Iwai967303d2013-02-19 17:12:42 +01005036 if (spec->hp_mic)
5037 update_hp_mic(codec, c, true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005038 }
5039
Takashi Iwai352f7f92012-12-19 12:52:06 +01005040 if (spec->cap_sync_hook)
Takashi Iwaia90229e2013-01-18 14:10:00 +01005041 spec->cap_sync_hook(codec, NULL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005042}
5043
5044/* set right pin controls for digital I/O */
5045static void init_digital(struct hda_codec *codec)
5046{
5047 struct hda_gen_spec *spec = codec->spec;
5048 int i;
5049 hda_nid_t pin;
5050
Takashi Iwaid4156932013-01-07 10:08:02 +01005051 for (i = 0; i < spec->autocfg.dig_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01005052 set_output_and_unmute(codec, spec->digout_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005053 pin = spec->autocfg.dig_in_pin;
Takashi Iwai2430d7b2013-01-04 15:09:42 +01005054 if (pin) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01005055 restore_pin_ctl(codec, pin);
Takashi Iwai3e367f12013-01-23 17:07:23 +01005056 resume_path_from_idx(codec, spec->digin_path);
Takashi Iwai2430d7b2013-01-04 15:09:42 +01005057 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01005058}
5059
Takashi Iwai973e4972012-12-20 15:16:09 +01005060/* clear unsol-event tags on unused pins; Conexant codecs seem to leave
5061 * invalid unsol tags by some reason
5062 */
5063static void clear_unsol_on_unused_pins(struct hda_codec *codec)
5064{
5065 int i;
5066
5067 for (i = 0; i < codec->init_pins.used; i++) {
5068 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
5069 hda_nid_t nid = pin->nid;
5070 if (is_jack_detectable(codec, nid) &&
5071 !snd_hda_jack_tbl_get(codec, nid))
5072 snd_hda_codec_update_cache(codec, nid, 0,
5073 AC_VERB_SET_UNSOLICITED_ENABLE, 0);
5074 }
5075}
5076
Takashi Iwai5187ac12013-01-07 12:52:16 +01005077/*
5078 * initialize the generic spec;
5079 * this can be put as patch_ops.init function
5080 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01005081int snd_hda_gen_init(struct hda_codec *codec)
5082{
5083 struct hda_gen_spec *spec = codec->spec;
5084
5085 if (spec->init_hook)
5086 spec->init_hook(codec);
5087
5088 snd_hda_apply_verbs(codec);
5089
Takashi Iwai3bbcd272012-12-20 11:50:58 +01005090 codec->cached_write = 1;
5091
Takashi Iwai352f7f92012-12-19 12:52:06 +01005092 init_multi_out(codec);
5093 init_extra_out(codec);
5094 init_multi_io(codec);
5095 init_analog_input(codec);
5096 init_input_src(codec);
5097 init_digital(codec);
5098
Takashi Iwai973e4972012-12-20 15:16:09 +01005099 clear_unsol_on_unused_pins(codec);
5100
Takashi Iwai352f7f92012-12-19 12:52:06 +01005101 /* call init functions of standard auto-mute helpers */
Takashi Iwaia5cc2502013-01-16 18:08:55 +01005102 update_automute_all(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005103
Takashi Iwaidc870f32013-01-22 15:24:30 +01005104 snd_hda_codec_flush_cache(codec);
Takashi Iwai3bbcd272012-12-20 11:50:58 +01005105
Takashi Iwai352f7f92012-12-19 12:52:06 +01005106 if (spec->vmaster_mute.sw_kctl && spec->vmaster_mute.hook)
5107 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
5108
5109 hda_call_check_power_status(codec, 0x01);
5110 return 0;
5111}
Takashi Iwaifce52a32013-01-07 12:42:48 +01005112EXPORT_SYMBOL_HDA(snd_hda_gen_init);
5113
Takashi Iwai5187ac12013-01-07 12:52:16 +01005114/*
5115 * free the generic spec;
5116 * this can be put as patch_ops.free function
5117 */
Takashi Iwaifce52a32013-01-07 12:42:48 +01005118void snd_hda_gen_free(struct hda_codec *codec)
5119{
Takashi Iwai7504b6c2013-03-18 11:25:51 +01005120 snd_hda_detach_beep_device(codec);
Takashi Iwaifce52a32013-01-07 12:42:48 +01005121 snd_hda_gen_spec_free(codec->spec);
5122 kfree(codec->spec);
5123 codec->spec = NULL;
5124}
5125EXPORT_SYMBOL_HDA(snd_hda_gen_free);
5126
5127#ifdef CONFIG_PM
Takashi Iwai5187ac12013-01-07 12:52:16 +01005128/*
5129 * check the loopback power save state;
5130 * this can be put as patch_ops.check_power_status function
5131 */
Takashi Iwaifce52a32013-01-07 12:42:48 +01005132int snd_hda_gen_check_power_status(struct hda_codec *codec, hda_nid_t nid)
5133{
5134 struct hda_gen_spec *spec = codec->spec;
5135 return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
5136}
5137EXPORT_SYMBOL_HDA(snd_hda_gen_check_power_status);
5138#endif
Takashi Iwai352f7f92012-12-19 12:52:06 +01005139
5140
5141/*
5142 * the generic codec support
5143 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005144
Takashi Iwai352f7f92012-12-19 12:52:06 +01005145static const struct hda_codec_ops generic_patch_ops = {
5146 .build_controls = snd_hda_gen_build_controls,
5147 .build_pcms = snd_hda_gen_build_pcms,
5148 .init = snd_hda_gen_init,
Takashi Iwaifce52a32013-01-07 12:42:48 +01005149 .free = snd_hda_gen_free,
Takashi Iwai352f7f92012-12-19 12:52:06 +01005150 .unsol_event = snd_hda_jack_unsol_event,
Takashi Iwai83012a72012-08-24 18:38:08 +02005151#ifdef CONFIG_PM
Takashi Iwaifce52a32013-01-07 12:42:48 +01005152 .check_power_status = snd_hda_gen_check_power_status,
Takashi Iwaicb53c622007-08-10 17:21:45 +02005153#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005154};
5155
Linus Torvalds1da177e2005-04-16 15:20:36 -07005156int snd_hda_parse_generic_codec(struct hda_codec *codec)
5157{
Takashi Iwai352f7f92012-12-19 12:52:06 +01005158 struct hda_gen_spec *spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005159 int err;
5160
Takashi Iwaie560d8d2005-09-09 14:21:46 +02005161 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005162 if (!spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005163 return -ENOMEM;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005164 snd_hda_gen_spec_init(spec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005165 codec->spec = spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005166
Takashi Iwai9eb413e2012-12-19 14:41:21 +01005167 err = snd_hda_parse_pin_defcfg(codec, &spec->autocfg, NULL, 0);
5168 if (err < 0)
5169 return err;
5170
5171 err = snd_hda_gen_parse_auto_config(codec, &spec->autocfg);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005172 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005173 goto error;
5174
5175 codec->patch_ops = generic_patch_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005176 return 0;
5177
Takashi Iwai352f7f92012-12-19 12:52:06 +01005178error:
Takashi Iwaifce52a32013-01-07 12:42:48 +01005179 snd_hda_gen_free(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005180 return err;
5181}
Takashi Iwaifce52a32013-01-07 12:42:48 +01005182EXPORT_SYMBOL_HDA(snd_hda_parse_generic_codec);