blob: ae85bbd2e6f842a8fcc0feac57ecc709cfd4b6b3 [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
Takashi Iwaib1b9fbd2013-05-14 12:58:47 +0200609/* check whether the NID is referred by any active paths */
610#define is_active_nid_for_any(codec, nid) \
611 is_active_nid(codec, nid, HDA_OUTPUT, 0)
612
Takashi Iwai352f7f92012-12-19 12:52:06 +0100613/* get the default amp value for the target state */
614static int get_amp_val_to_activate(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwai8999bf02013-01-18 11:01:33 +0100615 int dir, unsigned int caps, bool enable)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100616{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100617 unsigned int val = 0;
618
Takashi Iwai352f7f92012-12-19 12:52:06 +0100619 if (caps & AC_AMPCAP_NUM_STEPS) {
620 /* set to 0dB */
621 if (enable)
622 val = (caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT;
623 }
624 if (caps & AC_AMPCAP_MUTE) {
625 if (!enable)
626 val |= HDA_AMP_MUTE;
627 }
628 return val;
629}
630
631/* initialize the amp value (only at the first time) */
632static void init_amp(struct hda_codec *codec, hda_nid_t nid, int dir, int idx)
633{
Takashi Iwai8999bf02013-01-18 11:01:33 +0100634 unsigned int caps = query_amp_caps(codec, nid, dir);
635 int val = get_amp_val_to_activate(codec, nid, dir, caps, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100636 snd_hda_codec_amp_init_stereo(codec, nid, dir, idx, 0xff, val);
637}
638
Takashi Iwai8999bf02013-01-18 11:01:33 +0100639/* calculate amp value mask we can modify;
640 * if the given amp is controlled by mixers, don't touch it
641 */
642static unsigned int get_amp_mask_to_modify(struct hda_codec *codec,
643 hda_nid_t nid, int dir, int idx,
644 unsigned int caps)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100645{
Takashi Iwai8999bf02013-01-18 11:01:33 +0100646 unsigned int mask = 0xff;
647
648 if (caps & AC_AMPCAP_MUTE) {
649 if (is_ctl_associated(codec, nid, dir, idx, NID_PATH_MUTE_CTL))
650 mask &= ~0x80;
651 }
652 if (caps & AC_AMPCAP_NUM_STEPS) {
653 if (is_ctl_associated(codec, nid, dir, idx, NID_PATH_VOL_CTL) ||
654 is_ctl_associated(codec, nid, dir, idx, NID_PATH_BOOST_CTL))
655 mask &= ~0x7f;
656 }
657 return mask;
658}
659
660static void activate_amp(struct hda_codec *codec, hda_nid_t nid, int dir,
661 int idx, int idx_to_check, bool enable)
662{
663 unsigned int caps;
664 unsigned int mask, val;
665
Takashi Iwai7dddf2ae2013-01-24 16:31:35 +0100666 if (!enable && is_active_nid(codec, nid, dir, idx_to_check))
Takashi Iwai352f7f92012-12-19 12:52:06 +0100667 return;
Takashi Iwai8999bf02013-01-18 11:01:33 +0100668
669 caps = query_amp_caps(codec, nid, dir);
670 val = get_amp_val_to_activate(codec, nid, dir, caps, enable);
671 mask = get_amp_mask_to_modify(codec, nid, dir, idx_to_check, caps);
672 if (!mask)
673 return;
674
675 val &= mask;
676 snd_hda_codec_amp_stereo(codec, nid, dir, idx, mask, val);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100677}
678
679static void activate_amp_out(struct hda_codec *codec, struct nid_path *path,
680 int i, bool enable)
681{
682 hda_nid_t nid = path->path[i];
683 init_amp(codec, nid, HDA_OUTPUT, 0);
Takashi Iwai8999bf02013-01-18 11:01:33 +0100684 activate_amp(codec, nid, HDA_OUTPUT, 0, 0, enable);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100685}
686
687static void activate_amp_in(struct hda_codec *codec, struct nid_path *path,
688 int i, bool enable, bool add_aamix)
689{
690 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100691 const hda_nid_t *conn;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100692 int n, nums, idx;
693 int type;
694 hda_nid_t nid = path->path[i];
695
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100696 nums = snd_hda_get_conn_list(codec, nid, &conn);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100697 type = get_wcaps_type(get_wcaps(codec, nid));
698 if (type == AC_WID_PIN ||
699 (type == AC_WID_AUD_IN && codec->single_adc_amp)) {
700 nums = 1;
701 idx = 0;
702 } else
703 idx = path->idx[i];
704
705 for (n = 0; n < nums; n++)
706 init_amp(codec, nid, HDA_INPUT, n);
707
Takashi Iwai352f7f92012-12-19 12:52:06 +0100708 /* here is a little bit tricky in comparison with activate_amp_out();
709 * when aa-mixer is available, we need to enable the path as well
710 */
711 for (n = 0; n < nums; n++) {
Takashi Iwaie4a395e2013-01-23 17:00:31 +0100712 if (n != idx && (!add_aamix || conn[n] != spec->mixer_merge_nid))
Takashi Iwai352f7f92012-12-19 12:52:06 +0100713 continue;
Takashi Iwai8999bf02013-01-18 11:01:33 +0100714 activate_amp(codec, nid, HDA_INPUT, n, idx, enable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 }
716}
717
Takashi Iwai352f7f92012-12-19 12:52:06 +0100718/* activate or deactivate the given path
719 * if @add_aamix is set, enable the input from aa-mix NID as well (if any)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100721void snd_hda_activate_path(struct hda_codec *codec, struct nid_path *path,
722 bool enable, bool add_aamix)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723{
Takashi Iwai55196ff2013-01-24 17:32:56 +0100724 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100725 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726
Takashi Iwai352f7f92012-12-19 12:52:06 +0100727 if (!enable)
728 path->active = false;
729
730 for (i = path->depth - 1; i >= 0; i--) {
Takashi Iwai55196ff2013-01-24 17:32:56 +0100731 hda_nid_t nid = path->path[i];
732 if (enable && spec->power_down_unused) {
733 /* make sure the widget is powered up */
734 if (!snd_hda_check_power_state(codec, nid, AC_PWRST_D0))
735 snd_hda_codec_write(codec, nid, 0,
736 AC_VERB_SET_POWER_STATE,
737 AC_PWRST_D0);
738 }
Takashi Iwai352f7f92012-12-19 12:52:06 +0100739 if (enable && path->multi[i])
Takashi Iwai55196ff2013-01-24 17:32:56 +0100740 snd_hda_codec_write_cache(codec, nid, 0,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100741 AC_VERB_SET_CONNECT_SEL,
742 path->idx[i]);
743 if (has_amp_in(codec, path, i))
744 activate_amp_in(codec, path, i, enable, add_aamix);
745 if (has_amp_out(codec, path, i))
746 activate_amp_out(codec, path, i, enable);
747 }
748
749 if (enable)
750 path->active = true;
751}
752EXPORT_SYMBOL_HDA(snd_hda_activate_path);
753
Takashi Iwai55196ff2013-01-24 17:32:56 +0100754/* if the given path is inactive, put widgets into D3 (only if suitable) */
755static void path_power_down_sync(struct hda_codec *codec, struct nid_path *path)
756{
757 struct hda_gen_spec *spec = codec->spec;
Jiri Slaby868211d2013-04-04 22:32:10 +0200758 bool changed = false;
Takashi Iwai55196ff2013-01-24 17:32:56 +0100759 int i;
760
761 if (!spec->power_down_unused || path->active)
762 return;
763
764 for (i = 0; i < path->depth; i++) {
765 hda_nid_t nid = path->path[i];
Takashi Iwaib1b9fbd2013-05-14 12:58:47 +0200766 if (!snd_hda_check_power_state(codec, nid, AC_PWRST_D3) &&
767 !is_active_nid_for_any(codec, nid)) {
Takashi Iwai55196ff2013-01-24 17:32:56 +0100768 snd_hda_codec_write(codec, nid, 0,
769 AC_VERB_SET_POWER_STATE,
770 AC_PWRST_D3);
771 changed = true;
772 }
773 }
774
775 if (changed) {
776 msleep(10);
777 snd_hda_codec_read(codec, path->path[0], 0,
778 AC_VERB_GET_POWER_STATE, 0);
779 }
780}
781
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +0100782/* turn on/off EAPD on the given pin */
783static void set_pin_eapd(struct hda_codec *codec, hda_nid_t pin, bool enable)
784{
785 struct hda_gen_spec *spec = codec->spec;
786 if (spec->own_eapd_ctl ||
787 !(snd_hda_query_pin_caps(codec, pin) & AC_PINCAP_EAPD))
788 return;
Takashi Iwaiecac3ed2012-12-21 15:23:01 +0100789 if (codec->inv_eapd)
790 enable = !enable;
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +0100791 snd_hda_codec_update_cache(codec, pin, 0,
792 AC_VERB_SET_EAPD_BTLENABLE,
793 enable ? 0x02 : 0x00);
794}
795
Takashi Iwai3e367f12013-01-23 17:07:23 +0100796/* re-initialize the path specified by the given path index */
797static void resume_path_from_idx(struct hda_codec *codec, int path_idx)
798{
799 struct nid_path *path = snd_hda_get_path_from_idx(codec, path_idx);
800 if (path)
801 snd_hda_activate_path(codec, path, path->active, false);
802}
803
Takashi Iwai352f7f92012-12-19 12:52:06 +0100804
805/*
806 * Helper functions for creating mixer ctl elements
807 */
808
809enum {
810 HDA_CTL_WIDGET_VOL,
811 HDA_CTL_WIDGET_MUTE,
812 HDA_CTL_BIND_MUTE,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100813};
814static const struct snd_kcontrol_new control_templates[] = {
815 HDA_CODEC_VOLUME(NULL, 0, 0, 0),
816 HDA_CODEC_MUTE(NULL, 0, 0, 0),
817 HDA_BIND_MUTE(NULL, 0, 0, 0),
Takashi Iwai352f7f92012-12-19 12:52:06 +0100818};
819
820/* add dynamic controls from template */
Takashi Iwaia35bd1e2013-01-18 14:01:14 +0100821static struct snd_kcontrol_new *
822add_control(struct hda_gen_spec *spec, int type, const char *name,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100823 int cidx, unsigned long val)
824{
825 struct snd_kcontrol_new *knew;
826
Takashi Iwai12c93df2012-12-19 14:38:33 +0100827 knew = snd_hda_gen_add_kctl(spec, name, &control_templates[type]);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100828 if (!knew)
Takashi Iwaia35bd1e2013-01-18 14:01:14 +0100829 return NULL;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100830 knew->index = cidx;
831 if (get_amp_nid_(val))
832 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
833 knew->private_value = val;
Takashi Iwaia35bd1e2013-01-18 14:01:14 +0100834 return knew;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100835}
836
837static int add_control_with_pfx(struct hda_gen_spec *spec, int type,
838 const char *pfx, const char *dir,
839 const char *sfx, int cidx, unsigned long val)
840{
841 char name[32];
842 snprintf(name, sizeof(name), "%s %s %s", pfx, dir, sfx);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +0100843 if (!add_control(spec, type, name, cidx, val))
844 return -ENOMEM;
845 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100846}
847
848#define add_pb_vol_ctrl(spec, type, pfx, val) \
849 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", 0, val)
850#define add_pb_sw_ctrl(spec, type, pfx, val) \
851 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", 0, val)
852#define __add_pb_vol_ctrl(spec, type, pfx, cidx, val) \
853 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", cidx, val)
854#define __add_pb_sw_ctrl(spec, type, pfx, cidx, val) \
855 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", cidx, val)
856
857static int add_vol_ctl(struct hda_codec *codec, const char *pfx, int cidx,
858 unsigned int chs, struct nid_path *path)
859{
860 unsigned int val;
861 if (!path)
862 return 0;
863 val = path->ctls[NID_PATH_VOL_CTL];
864 if (!val)
865 return 0;
866 val = amp_val_replace_channels(val, chs);
867 return __add_pb_vol_ctrl(codec->spec, HDA_CTL_WIDGET_VOL, pfx, cidx, val);
868}
869
870/* return the channel bits suitable for the given path->ctls[] */
871static int get_default_ch_nums(struct hda_codec *codec, struct nid_path *path,
872 int type)
873{
874 int chs = 1; /* mono (left only) */
875 if (path) {
876 hda_nid_t nid = get_amp_nid_(path->ctls[type]);
877 if (nid && (get_wcaps(codec, nid) & AC_WCAP_STEREO))
878 chs = 3; /* stereo */
879 }
880 return chs;
881}
882
883static int add_stereo_vol(struct hda_codec *codec, const char *pfx, int cidx,
884 struct nid_path *path)
885{
886 int chs = get_default_ch_nums(codec, path, NID_PATH_VOL_CTL);
887 return add_vol_ctl(codec, pfx, cidx, chs, path);
888}
889
890/* create a mute-switch for the given mixer widget;
891 * if it has multiple sources (e.g. DAC and loopback), create a bind-mute
892 */
893static int add_sw_ctl(struct hda_codec *codec, const char *pfx, int cidx,
894 unsigned int chs, struct nid_path *path)
895{
896 unsigned int val;
897 int type = HDA_CTL_WIDGET_MUTE;
898
899 if (!path)
900 return 0;
901 val = path->ctls[NID_PATH_MUTE_CTL];
902 if (!val)
903 return 0;
904 val = amp_val_replace_channels(val, chs);
905 if (get_amp_direction_(val) == HDA_INPUT) {
906 hda_nid_t nid = get_amp_nid_(val);
907 int nums = snd_hda_get_num_conns(codec, nid);
908 if (nums > 1) {
909 type = HDA_CTL_BIND_MUTE;
910 val |= nums << 19;
911 }
912 }
913 return __add_pb_sw_ctrl(codec->spec, type, pfx, cidx, val);
914}
915
916static int add_stereo_sw(struct hda_codec *codec, const char *pfx,
917 int cidx, struct nid_path *path)
918{
919 int chs = get_default_ch_nums(codec, path, NID_PATH_MUTE_CTL);
920 return add_sw_ctl(codec, pfx, cidx, chs, path);
921}
922
Takashi Iwai247d85e2013-01-17 16:18:11 +0100923/* any ctl assigned to the path with the given index? */
924static bool path_has_mixer(struct hda_codec *codec, int path_idx, int ctl_type)
925{
926 struct nid_path *path = snd_hda_get_path_from_idx(codec, path_idx);
927 return path && path->ctls[ctl_type];
928}
929
Takashi Iwai352f7f92012-12-19 12:52:06 +0100930static const char * const channel_name[4] = {
931 "Front", "Surround", "CLFE", "Side"
932};
933
934/* give some appropriate ctl name prefix for the given line out channel */
Takashi Iwai247d85e2013-01-17 16:18:11 +0100935static const char *get_line_out_pfx(struct hda_codec *codec, int ch,
936 int *index, int ctl_type)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100937{
Takashi Iwai247d85e2013-01-17 16:18:11 +0100938 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100939 struct auto_pin_cfg *cfg = &spec->autocfg;
940
941 *index = 0;
942 if (cfg->line_outs == 1 && !spec->multi_ios &&
Takashi Iwai247d85e2013-01-17 16:18:11 +0100943 !cfg->hp_outs && !cfg->speaker_outs)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100944 return spec->vmaster_mute.hook ? "PCM" : "Master";
945
946 /* if there is really a single DAC used in the whole output paths,
947 * use it master (or "PCM" if a vmaster hook is present)
948 */
949 if (spec->multiout.num_dacs == 1 && !spec->mixer_nid &&
950 !spec->multiout.hp_out_nid[0] && !spec->multiout.extra_out_nid[0])
951 return spec->vmaster_mute.hook ? "PCM" : "Master";
952
Takashi Iwai247d85e2013-01-17 16:18:11 +0100953 /* multi-io channels */
954 if (ch >= cfg->line_outs)
955 return channel_name[ch];
956
Takashi Iwai352f7f92012-12-19 12:52:06 +0100957 switch (cfg->line_out_type) {
958 case AUTO_PIN_SPEAKER_OUT:
Takashi Iwai247d85e2013-01-17 16:18:11 +0100959 /* if the primary channel vol/mute is shared with HP volume,
960 * don't name it as Speaker
961 */
962 if (!ch && cfg->hp_outs &&
963 !path_has_mixer(codec, spec->hp_paths[0], ctl_type))
964 break;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100965 if (cfg->line_outs == 1)
966 return "Speaker";
967 if (cfg->line_outs == 2)
968 return ch ? "Bass Speaker" : "Speaker";
969 break;
970 case AUTO_PIN_HP_OUT:
Takashi Iwai247d85e2013-01-17 16:18:11 +0100971 /* if the primary channel vol/mute is shared with spk volume,
972 * don't name it as Headphone
973 */
974 if (!ch && cfg->speaker_outs &&
975 !path_has_mixer(codec, spec->speaker_paths[0], ctl_type))
976 break;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100977 /* for multi-io case, only the primary out */
978 if (ch && spec->multi_ios)
979 break;
980 *index = ch;
981 return "Headphone";
Takashi Iwai352f7f92012-12-19 12:52:06 +0100982 }
Takashi Iwai247d85e2013-01-17 16:18:11 +0100983
984 /* for a single channel output, we don't have to name the channel */
985 if (cfg->line_outs == 1 && !spec->multi_ios)
986 return "PCM";
987
Takashi Iwai352f7f92012-12-19 12:52:06 +0100988 if (ch >= ARRAY_SIZE(channel_name)) {
989 snd_BUG();
990 return "PCM";
991 }
992
993 return channel_name[ch];
994}
995
996/*
997 * Parse output paths
998 */
999
1000/* badness definition */
1001enum {
1002 /* No primary DAC is found for the main output */
1003 BAD_NO_PRIMARY_DAC = 0x10000,
1004 /* No DAC is found for the extra output */
1005 BAD_NO_DAC = 0x4000,
1006 /* No possible multi-ios */
Takashi Iwai1d739062013-02-13 14:17:32 +01001007 BAD_MULTI_IO = 0x120,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001008 /* No individual DAC for extra output */
1009 BAD_NO_EXTRA_DAC = 0x102,
1010 /* No individual DAC for extra surrounds */
1011 BAD_NO_EXTRA_SURR_DAC = 0x101,
1012 /* Primary DAC shared with main surrounds */
1013 BAD_SHARED_SURROUND = 0x100,
Takashi Iwai55a63d42013-03-21 17:20:12 +01001014 /* No independent HP possible */
Takashi Iwaibec8e682013-03-22 15:10:08 +01001015 BAD_NO_INDEP_HP = 0x10,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001016 /* Primary DAC shared with main CLFE */
1017 BAD_SHARED_CLFE = 0x10,
1018 /* Primary DAC shared with extra surrounds */
1019 BAD_SHARED_EXTRA_SURROUND = 0x10,
1020 /* Volume widget is shared */
1021 BAD_SHARED_VOL = 0x10,
1022};
1023
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001024/* look for widgets in the given path which are appropriate for
Takashi Iwai352f7f92012-12-19 12:52:06 +01001025 * volume and mute controls, and assign the values to ctls[].
1026 *
1027 * When no appropriate widget is found in the path, the badness value
1028 * is incremented depending on the situation. The function returns the
1029 * total badness for both volume and mute controls.
1030 */
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001031static int assign_out_path_ctls(struct hda_codec *codec, struct nid_path *path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001032{
Takashi Iwai352f7f92012-12-19 12:52:06 +01001033 hda_nid_t nid;
1034 unsigned int val;
1035 int badness = 0;
1036
1037 if (!path)
1038 return BAD_SHARED_VOL * 2;
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001039
1040 if (path->ctls[NID_PATH_VOL_CTL] ||
1041 path->ctls[NID_PATH_MUTE_CTL])
1042 return 0; /* already evaluated */
1043
Takashi Iwai352f7f92012-12-19 12:52:06 +01001044 nid = look_for_out_vol_nid(codec, path);
1045 if (nid) {
1046 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
1047 if (is_ctl_used(codec, val, NID_PATH_VOL_CTL))
1048 badness += BAD_SHARED_VOL;
1049 else
1050 path->ctls[NID_PATH_VOL_CTL] = val;
1051 } else
1052 badness += BAD_SHARED_VOL;
1053 nid = look_for_out_mute_nid(codec, path);
1054 if (nid) {
1055 unsigned int wid_type = get_wcaps_type(get_wcaps(codec, nid));
1056 if (wid_type == AC_WID_PIN || wid_type == AC_WID_AUD_OUT ||
1057 nid_has_mute(codec, nid, HDA_OUTPUT))
1058 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
1059 else
1060 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT);
1061 if (is_ctl_used(codec, val, NID_PATH_MUTE_CTL))
1062 badness += BAD_SHARED_VOL;
1063 else
1064 path->ctls[NID_PATH_MUTE_CTL] = val;
1065 } else
1066 badness += BAD_SHARED_VOL;
1067 return badness;
1068}
1069
Takashi Iwai98bd1112013-03-22 14:53:50 +01001070const struct badness_table hda_main_out_badness = {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001071 .no_primary_dac = BAD_NO_PRIMARY_DAC,
1072 .no_dac = BAD_NO_DAC,
1073 .shared_primary = BAD_NO_PRIMARY_DAC,
1074 .shared_surr = BAD_SHARED_SURROUND,
1075 .shared_clfe = BAD_SHARED_CLFE,
1076 .shared_surr_main = BAD_SHARED_SURROUND,
1077};
Takashi Iwai98bd1112013-03-22 14:53:50 +01001078EXPORT_SYMBOL_HDA(hda_main_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001079
Takashi Iwai98bd1112013-03-22 14:53:50 +01001080const struct badness_table hda_extra_out_badness = {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001081 .no_primary_dac = BAD_NO_DAC,
1082 .no_dac = BAD_NO_DAC,
1083 .shared_primary = BAD_NO_EXTRA_DAC,
1084 .shared_surr = BAD_SHARED_EXTRA_SURROUND,
1085 .shared_clfe = BAD_SHARED_EXTRA_SURROUND,
1086 .shared_surr_main = BAD_NO_EXTRA_SURR_DAC,
1087};
Takashi Iwai98bd1112013-03-22 14:53:50 +01001088EXPORT_SYMBOL_HDA(hda_extra_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001089
Takashi Iwai7385df62013-01-07 09:50:52 +01001090/* get the DAC of the primary output corresponding to the given array index */
1091static hda_nid_t get_primary_out(struct hda_codec *codec, int idx)
1092{
1093 struct hda_gen_spec *spec = codec->spec;
1094 struct auto_pin_cfg *cfg = &spec->autocfg;
1095
1096 if (cfg->line_outs > idx)
1097 return spec->private_dac_nids[idx];
1098 idx -= cfg->line_outs;
1099 if (spec->multi_ios > idx)
1100 return spec->multi_io[idx].dac;
1101 return 0;
1102}
1103
1104/* return the DAC if it's reachable, otherwise zero */
1105static inline hda_nid_t try_dac(struct hda_codec *codec,
1106 hda_nid_t dac, hda_nid_t pin)
1107{
1108 return is_reachable_path(codec, dac, pin) ? dac : 0;
1109}
1110
Takashi Iwai352f7f92012-12-19 12:52:06 +01001111/* try to assign DACs to pins and return the resultant badness */
1112static int try_assign_dacs(struct hda_codec *codec, int num_outs,
1113 const hda_nid_t *pins, hda_nid_t *dacs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001114 int *path_idx,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001115 const struct badness_table *bad)
1116{
1117 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001118 int i, j;
1119 int badness = 0;
1120 hda_nid_t dac;
1121
1122 if (!num_outs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 return 0;
1124
Takashi Iwai352f7f92012-12-19 12:52:06 +01001125 for (i = 0; i < num_outs; i++) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001126 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001127 hda_nid_t pin = pins[i];
Takashi Iwai1e0b5282013-01-04 12:56:52 +01001128
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001129 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
1130 if (path) {
1131 badness += assign_out_path_ctls(codec, path);
Takashi Iwai1e0b5282013-01-04 12:56:52 +01001132 continue;
1133 }
1134
1135 dacs[i] = look_for_dac(codec, pin, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001136 if (!dacs[i] && !i) {
Takashi Iwai980428c2013-01-09 09:28:20 +01001137 /* try to steal the DAC of surrounds for the front */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001138 for (j = 1; j < num_outs; j++) {
1139 if (is_reachable_path(codec, dacs[j], pin)) {
1140 dacs[0] = dacs[j];
1141 dacs[j] = 0;
Takashi Iwai980428c2013-01-09 09:28:20 +01001142 invalidate_nid_path(codec, path_idx[j]);
Takashi Iwai196c17662013-01-04 15:01:40 +01001143 path_idx[j] = 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001144 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 }
1146 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001147 }
1148 dac = dacs[i];
1149 if (!dac) {
Takashi Iwai7385df62013-01-07 09:50:52 +01001150 if (num_outs > 2)
1151 dac = try_dac(codec, get_primary_out(codec, i), pin);
1152 if (!dac)
1153 dac = try_dac(codec, dacs[0], pin);
1154 if (!dac)
1155 dac = try_dac(codec, get_primary_out(codec, i), pin);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001156 if (dac) {
1157 if (!i)
1158 badness += bad->shared_primary;
1159 else if (i == 1)
1160 badness += bad->shared_surr;
1161 else
1162 badness += bad->shared_clfe;
1163 } else if (is_reachable_path(codec, spec->private_dac_nids[0], pin)) {
1164 dac = spec->private_dac_nids[0];
1165 badness += bad->shared_surr_main;
1166 } else if (!i)
1167 badness += bad->no_primary_dac;
1168 else
1169 badness += bad->no_dac;
1170 }
Takashi Iwai1fa335b2013-01-21 11:43:19 +01001171 if (!dac)
1172 continue;
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001173 path = snd_hda_add_new_path(codec, dac, pin, -spec->mixer_nid);
Takashi Iwai117688a2013-01-04 15:41:41 +01001174 if (!path && !i && spec->mixer_nid) {
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001175 /* try with aamix */
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001176 path = snd_hda_add_new_path(codec, dac, pin, 0);
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001177 }
Takashi Iwai1fa335b2013-01-21 11:43:19 +01001178 if (!path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001179 dac = dacs[i] = 0;
Takashi Iwai1fa335b2013-01-21 11:43:19 +01001180 badness += bad->no_dac;
1181 } else {
Takashi Iwaia7694092013-01-21 10:43:18 +01001182 /* print_nid_path("output", path); */
Takashi Iwaie1284af2013-01-03 16:33:02 +01001183 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +01001184 path_idx[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001185 badness += assign_out_path_ctls(codec, path);
Takashi Iwaie1284af2013-01-03 16:33:02 +01001186 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001187 }
1188
1189 return badness;
1190}
1191
1192/* return NID if the given pin has only a single connection to a certain DAC */
1193static hda_nid_t get_dac_if_single(struct hda_codec *codec, hda_nid_t pin)
1194{
1195 struct hda_gen_spec *spec = codec->spec;
1196 int i;
1197 hda_nid_t nid_found = 0;
1198
1199 for (i = 0; i < spec->num_all_dacs; i++) {
1200 hda_nid_t nid = spec->all_dacs[i];
1201 if (!nid || is_dac_already_used(codec, nid))
1202 continue;
1203 if (is_reachable_path(codec, nid, pin)) {
1204 if (nid_found)
1205 return 0;
1206 nid_found = nid;
1207 }
1208 }
1209 return nid_found;
1210}
1211
1212/* check whether the given pin can be a multi-io pin */
1213static bool can_be_multiio_pin(struct hda_codec *codec,
1214 unsigned int location, hda_nid_t nid)
1215{
1216 unsigned int defcfg, caps;
1217
1218 defcfg = snd_hda_codec_get_pincfg(codec, nid);
1219 if (get_defcfg_connect(defcfg) != AC_JACK_PORT_COMPLEX)
1220 return false;
1221 if (location && get_defcfg_location(defcfg) != location)
1222 return false;
1223 caps = snd_hda_query_pin_caps(codec, nid);
1224 if (!(caps & AC_PINCAP_OUT))
1225 return false;
1226 return true;
1227}
1228
Takashi Iwaie22aab72013-01-04 14:50:04 +01001229/* count the number of input pins that are capable to be multi-io */
1230static int count_multiio_pins(struct hda_codec *codec, hda_nid_t reference_pin)
1231{
1232 struct hda_gen_spec *spec = codec->spec;
1233 struct auto_pin_cfg *cfg = &spec->autocfg;
1234 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1235 unsigned int location = get_defcfg_location(defcfg);
1236 int type, i;
1237 int num_pins = 0;
1238
1239 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1240 for (i = 0; i < cfg->num_inputs; i++) {
1241 if (cfg->inputs[i].type != type)
1242 continue;
1243 if (can_be_multiio_pin(codec, location,
1244 cfg->inputs[i].pin))
1245 num_pins++;
1246 }
1247 }
1248 return num_pins;
1249}
1250
Takashi Iwai352f7f92012-12-19 12:52:06 +01001251/*
1252 * multi-io helper
1253 *
1254 * When hardwired is set, try to fill ony hardwired pins, and returns
1255 * zero if any pins are filled, non-zero if nothing found.
1256 * When hardwired is off, try to fill possible input pins, and returns
1257 * the badness value.
1258 */
1259static int fill_multi_ios(struct hda_codec *codec,
1260 hda_nid_t reference_pin,
Takashi Iwaie22aab72013-01-04 14:50:04 +01001261 bool hardwired)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001262{
1263 struct hda_gen_spec *spec = codec->spec;
1264 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwaie22aab72013-01-04 14:50:04 +01001265 int type, i, j, num_pins, old_pins;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001266 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1267 unsigned int location = get_defcfg_location(defcfg);
1268 int badness = 0;
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001269 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001270
1271 old_pins = spec->multi_ios;
1272 if (old_pins >= 2)
1273 goto end_fill;
1274
Takashi Iwaie22aab72013-01-04 14:50:04 +01001275 num_pins = count_multiio_pins(codec, reference_pin);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001276 if (num_pins < 2)
1277 goto end_fill;
1278
Takashi Iwai352f7f92012-12-19 12:52:06 +01001279 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1280 for (i = 0; i < cfg->num_inputs; i++) {
1281 hda_nid_t nid = cfg->inputs[i].pin;
1282 hda_nid_t dac = 0;
1283
1284 if (cfg->inputs[i].type != type)
1285 continue;
1286 if (!can_be_multiio_pin(codec, location, nid))
1287 continue;
1288 for (j = 0; j < spec->multi_ios; j++) {
1289 if (nid == spec->multi_io[j].pin)
1290 break;
1291 }
1292 if (j < spec->multi_ios)
1293 continue;
1294
Takashi Iwai352f7f92012-12-19 12:52:06 +01001295 if (hardwired)
1296 dac = get_dac_if_single(codec, nid);
1297 else if (!dac)
1298 dac = look_for_dac(codec, nid, false);
1299 if (!dac) {
1300 badness++;
1301 continue;
1302 }
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001303 path = snd_hda_add_new_path(codec, dac, nid,
1304 -spec->mixer_nid);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001305 if (!path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001306 badness++;
1307 continue;
1308 }
Takashi Iwaia7694092013-01-21 10:43:18 +01001309 /* print_nid_path("multiio", path); */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001310 spec->multi_io[spec->multi_ios].pin = nid;
1311 spec->multi_io[spec->multi_ios].dac = dac;
Takashi Iwai196c17662013-01-04 15:01:40 +01001312 spec->out_paths[cfg->line_outs + spec->multi_ios] =
1313 snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001314 spec->multi_ios++;
1315 if (spec->multi_ios >= 2)
1316 break;
1317 }
1318 }
1319 end_fill:
1320 if (badness)
1321 badness = BAD_MULTI_IO;
1322 if (old_pins == spec->multi_ios) {
1323 if (hardwired)
1324 return 1; /* nothing found */
1325 else
1326 return badness; /* no badness if nothing found */
1327 }
1328 if (!hardwired && spec->multi_ios < 2) {
1329 /* cancel newly assigned paths */
1330 spec->paths.used -= spec->multi_ios - old_pins;
1331 spec->multi_ios = old_pins;
1332 return badness;
1333 }
1334
1335 /* assign volume and mute controls */
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001336 for (i = old_pins; i < spec->multi_ios; i++) {
1337 path = snd_hda_get_path_from_idx(codec, spec->out_paths[cfg->line_outs + i]);
1338 badness += assign_out_path_ctls(codec, path);
1339 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001340
1341 return badness;
1342}
1343
1344/* map DACs for all pins in the list if they are single connections */
1345static bool map_singles(struct hda_codec *codec, int outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001346 const hda_nid_t *pins, hda_nid_t *dacs, int *path_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001347{
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001348 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001349 int i;
1350 bool found = false;
1351 for (i = 0; i < outs; i++) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001352 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001353 hda_nid_t dac;
1354 if (dacs[i])
1355 continue;
1356 dac = get_dac_if_single(codec, pins[i]);
1357 if (!dac)
1358 continue;
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001359 path = snd_hda_add_new_path(codec, dac, pins[i],
1360 -spec->mixer_nid);
Takashi Iwai117688a2013-01-04 15:41:41 +01001361 if (!path && !i && spec->mixer_nid)
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001362 path = snd_hda_add_new_path(codec, dac, pins[i], 0);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001363 if (path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001364 dacs[i] = dac;
1365 found = true;
Takashi Iwaia7694092013-01-21 10:43:18 +01001366 /* print_nid_path("output", path); */
Takashi Iwaie1284af2013-01-03 16:33:02 +01001367 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +01001368 path_idx[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001369 }
1370 }
1371 return found;
1372}
1373
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001374/* create a new path including aamix if available, and return its index */
1375static int check_aamix_out_path(struct hda_codec *codec, int path_idx)
1376{
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001377 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001378 struct nid_path *path;
Takashi Iwai5ead56f2013-04-16 14:16:54 +02001379 hda_nid_t path_dac, dac, pin;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001380
1381 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001382 if (!path || !path->depth ||
1383 is_nid_contained(path, spec->mixer_nid))
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001384 return 0;
Takashi Iwai5ead56f2013-04-16 14:16:54 +02001385 path_dac = path->path[0];
1386 dac = spec->private_dac_nids[0];
Takashi Iwaif87498b2013-01-21 14:24:31 +01001387 pin = path->path[path->depth - 1];
1388 path = snd_hda_add_new_path(codec, dac, pin, spec->mixer_nid);
1389 if (!path) {
Takashi Iwai5ead56f2013-04-16 14:16:54 +02001390 if (dac != path_dac)
1391 dac = path_dac;
Takashi Iwaif87498b2013-01-21 14:24:31 +01001392 else if (spec->multiout.hp_out_nid[0])
1393 dac = spec->multiout.hp_out_nid[0];
1394 else if (spec->multiout.extra_out_nid[0])
1395 dac = spec->multiout.extra_out_nid[0];
Takashi Iwai5ead56f2013-04-16 14:16:54 +02001396 else
1397 dac = 0;
Takashi Iwaif87498b2013-01-21 14:24:31 +01001398 if (dac)
1399 path = snd_hda_add_new_path(codec, dac, pin,
1400 spec->mixer_nid);
1401 }
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001402 if (!path)
1403 return 0;
Takashi Iwaia7694092013-01-21 10:43:18 +01001404 /* print_nid_path("output-aamix", path); */
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001405 path->active = false; /* unused as default */
1406 return snd_hda_get_path_idx(codec, path);
1407}
1408
Takashi Iwai55a63d42013-03-21 17:20:12 +01001409/* check whether the independent HP is available with the current config */
1410static bool indep_hp_possible(struct hda_codec *codec)
1411{
1412 struct hda_gen_spec *spec = codec->spec;
1413 struct auto_pin_cfg *cfg = &spec->autocfg;
1414 struct nid_path *path;
1415 int i, idx;
1416
1417 if (cfg->line_out_type == AUTO_PIN_HP_OUT)
1418 idx = spec->out_paths[0];
1419 else
1420 idx = spec->hp_paths[0];
1421 path = snd_hda_get_path_from_idx(codec, idx);
1422 if (!path)
1423 return false;
1424
1425 /* assume no path conflicts unless aamix is involved */
1426 if (!spec->mixer_nid || !is_nid_contained(path, spec->mixer_nid))
1427 return true;
1428
1429 /* check whether output paths contain aamix */
1430 for (i = 0; i < cfg->line_outs; i++) {
1431 if (spec->out_paths[i] == idx)
1432 break;
1433 path = snd_hda_get_path_from_idx(codec, spec->out_paths[i]);
1434 if (path && is_nid_contained(path, spec->mixer_nid))
1435 return false;
1436 }
1437 for (i = 0; i < cfg->speaker_outs; i++) {
1438 path = snd_hda_get_path_from_idx(codec, spec->speaker_paths[i]);
1439 if (path && is_nid_contained(path, spec->mixer_nid))
1440 return false;
1441 }
1442
1443 return true;
1444}
1445
Takashi Iwaia07a9492013-01-07 16:44:06 +01001446/* fill the empty entries in the dac array for speaker/hp with the
1447 * shared dac pointed by the paths
1448 */
1449static void refill_shared_dacs(struct hda_codec *codec, int num_outs,
1450 hda_nid_t *dacs, int *path_idx)
1451{
1452 struct nid_path *path;
1453 int i;
1454
1455 for (i = 0; i < num_outs; i++) {
1456 if (dacs[i])
1457 continue;
1458 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
1459 if (!path)
1460 continue;
1461 dacs[i] = path->path[0];
1462 }
1463}
1464
Takashi Iwai352f7f92012-12-19 12:52:06 +01001465/* fill in the dac_nids table from the parsed pin configuration */
1466static int fill_and_eval_dacs(struct hda_codec *codec,
1467 bool fill_hardwired,
1468 bool fill_mio_first)
1469{
1470 struct hda_gen_spec *spec = codec->spec;
1471 struct auto_pin_cfg *cfg = &spec->autocfg;
1472 int i, err, badness;
1473
1474 /* set num_dacs once to full for look_for_dac() */
1475 spec->multiout.num_dacs = cfg->line_outs;
1476 spec->multiout.dac_nids = spec->private_dac_nids;
1477 memset(spec->private_dac_nids, 0, sizeof(spec->private_dac_nids));
1478 memset(spec->multiout.hp_out_nid, 0, sizeof(spec->multiout.hp_out_nid));
1479 memset(spec->multiout.extra_out_nid, 0, sizeof(spec->multiout.extra_out_nid));
1480 spec->multi_ios = 0;
1481 snd_array_free(&spec->paths);
Takashi Iwaicd5be3f2013-01-07 15:07:00 +01001482
1483 /* clear path indices */
1484 memset(spec->out_paths, 0, sizeof(spec->out_paths));
1485 memset(spec->hp_paths, 0, sizeof(spec->hp_paths));
1486 memset(spec->speaker_paths, 0, sizeof(spec->speaker_paths));
1487 memset(spec->aamix_out_paths, 0, sizeof(spec->aamix_out_paths));
1488 memset(spec->digout_paths, 0, sizeof(spec->digout_paths));
Takashi Iwaic697b712013-01-07 17:09:26 +01001489 memset(spec->input_paths, 0, sizeof(spec->input_paths));
Takashi Iwaicd5be3f2013-01-07 15:07:00 +01001490 memset(spec->loopback_paths, 0, sizeof(spec->loopback_paths));
1491 memset(&spec->digin_path, 0, sizeof(spec->digin_path));
1492
Takashi Iwai352f7f92012-12-19 12:52:06 +01001493 badness = 0;
1494
1495 /* fill hard-wired DACs first */
1496 if (fill_hardwired) {
1497 bool mapped;
1498 do {
1499 mapped = map_singles(codec, cfg->line_outs,
1500 cfg->line_out_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001501 spec->private_dac_nids,
1502 spec->out_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001503 mapped |= map_singles(codec, cfg->hp_outs,
1504 cfg->hp_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001505 spec->multiout.hp_out_nid,
1506 spec->hp_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001507 mapped |= map_singles(codec, cfg->speaker_outs,
1508 cfg->speaker_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001509 spec->multiout.extra_out_nid,
1510 spec->speaker_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001511 if (fill_mio_first && cfg->line_outs == 1 &&
1512 cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
Takashi Iwaie22aab72013-01-04 14:50:04 +01001513 err = fill_multi_ios(codec, cfg->line_out_pins[0], true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001514 if (!err)
1515 mapped = true;
1516 }
1517 } while (mapped);
1518 }
1519
1520 badness += try_assign_dacs(codec, cfg->line_outs, cfg->line_out_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001521 spec->private_dac_nids, spec->out_paths,
Takashi Iwai98bd1112013-03-22 14:53:50 +01001522 spec->main_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001523
Takashi Iwai352f7f92012-12-19 12:52:06 +01001524 if (fill_mio_first &&
1525 cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1526 /* try to fill multi-io first */
Takashi Iwaie22aab72013-01-04 14:50:04 +01001527 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001528 if (err < 0)
1529 return err;
1530 /* we don't count badness at this stage yet */
1531 }
1532
1533 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
1534 err = try_assign_dacs(codec, cfg->hp_outs, cfg->hp_pins,
1535 spec->multiout.hp_out_nid,
Takashi Iwai196c17662013-01-04 15:01:40 +01001536 spec->hp_paths,
Takashi Iwai98bd1112013-03-22 14:53:50 +01001537 spec->extra_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001538 if (err < 0)
1539 return err;
1540 badness += err;
1541 }
1542 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1543 err = try_assign_dacs(codec, cfg->speaker_outs,
1544 cfg->speaker_pins,
1545 spec->multiout.extra_out_nid,
Takashi Iwai196c17662013-01-04 15:01:40 +01001546 spec->speaker_paths,
Takashi Iwai98bd1112013-03-22 14:53:50 +01001547 spec->extra_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001548 if (err < 0)
1549 return err;
1550 badness += err;
1551 }
1552 if (cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
Takashi Iwaie22aab72013-01-04 14:50:04 +01001553 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001554 if (err < 0)
1555 return err;
1556 badness += err;
1557 }
Takashi Iwaie22aab72013-01-04 14:50:04 +01001558
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001559 if (spec->mixer_nid) {
1560 spec->aamix_out_paths[0] =
1561 check_aamix_out_path(codec, spec->out_paths[0]);
1562 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1563 spec->aamix_out_paths[1] =
1564 check_aamix_out_path(codec, spec->hp_paths[0]);
1565 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1566 spec->aamix_out_paths[2] =
1567 check_aamix_out_path(codec, spec->speaker_paths[0]);
1568 }
1569
Takashi Iwaie22aab72013-01-04 14:50:04 +01001570 if (cfg->hp_outs && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
1571 if (count_multiio_pins(codec, cfg->hp_pins[0]) >= 2)
1572 spec->multi_ios = 1; /* give badness */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001573
Takashi Iwaia07a9492013-01-07 16:44:06 +01001574 /* re-count num_dacs and squash invalid entries */
1575 spec->multiout.num_dacs = 0;
1576 for (i = 0; i < cfg->line_outs; i++) {
1577 if (spec->private_dac_nids[i])
1578 spec->multiout.num_dacs++;
1579 else {
1580 memmove(spec->private_dac_nids + i,
1581 spec->private_dac_nids + i + 1,
1582 sizeof(hda_nid_t) * (cfg->line_outs - i - 1));
1583 spec->private_dac_nids[cfg->line_outs - 1] = 0;
1584 }
1585 }
1586
1587 spec->ext_channel_count = spec->min_channel_count =
David Henningssonc0f3b212013-01-16 11:45:37 +01001588 spec->multiout.num_dacs * 2;
Takashi Iwaia07a9492013-01-07 16:44:06 +01001589
Takashi Iwai352f7f92012-12-19 12:52:06 +01001590 if (spec->multi_ios == 2) {
1591 for (i = 0; i < 2; i++)
1592 spec->private_dac_nids[spec->multiout.num_dacs++] =
1593 spec->multi_io[i].dac;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001594 } else if (spec->multi_ios) {
1595 spec->multi_ios = 0;
1596 badness += BAD_MULTI_IO;
1597 }
1598
Takashi Iwai55a63d42013-03-21 17:20:12 +01001599 if (spec->indep_hp && !indep_hp_possible(codec))
1600 badness += BAD_NO_INDEP_HP;
1601
Takashi Iwaia07a9492013-01-07 16:44:06 +01001602 /* re-fill the shared DAC for speaker / headphone */
1603 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1604 refill_shared_dacs(codec, cfg->hp_outs,
1605 spec->multiout.hp_out_nid,
1606 spec->hp_paths);
1607 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1608 refill_shared_dacs(codec, cfg->speaker_outs,
1609 spec->multiout.extra_out_nid,
1610 spec->speaker_paths);
1611
Takashi Iwai352f7f92012-12-19 12:52:06 +01001612 return badness;
1613}
1614
1615#define DEBUG_BADNESS
1616
1617#ifdef DEBUG_BADNESS
1618#define debug_badness snd_printdd
1619#else
1620#define debug_badness(...)
1621#endif
1622
Takashi Iwaia7694092013-01-21 10:43:18 +01001623#ifdef DEBUG_BADNESS
1624static inline void print_nid_path_idx(struct hda_codec *codec,
1625 const char *pfx, int idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001626{
Takashi Iwaia7694092013-01-21 10:43:18 +01001627 struct nid_path *path;
1628
1629 path = snd_hda_get_path_from_idx(codec, idx);
1630 if (path)
1631 print_nid_path(pfx, path);
1632}
1633
1634static void debug_show_configs(struct hda_codec *codec,
1635 struct auto_pin_cfg *cfg)
1636{
1637 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia7694092013-01-21 10:43:18 +01001638 static const char * const lo_type[3] = { "LO", "SP", "HP" };
Takashi Iwaia7694092013-01-21 10:43:18 +01001639 int i;
1640
1641 debug_badness("multi_outs = %x/%x/%x/%x : %x/%x/%x/%x (type %s)\n",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001642 cfg->line_out_pins[0], cfg->line_out_pins[1],
Takashi Iwai708122e2012-12-20 17:56:57 +01001643 cfg->line_out_pins[2], cfg->line_out_pins[3],
Takashi Iwai352f7f92012-12-19 12:52:06 +01001644 spec->multiout.dac_nids[0],
1645 spec->multiout.dac_nids[1],
1646 spec->multiout.dac_nids[2],
Takashi Iwaia7694092013-01-21 10:43:18 +01001647 spec->multiout.dac_nids[3],
1648 lo_type[cfg->line_out_type]);
1649 for (i = 0; i < cfg->line_outs; i++)
1650 print_nid_path_idx(codec, " out", spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001651 if (spec->multi_ios > 0)
1652 debug_badness("multi_ios(%d) = %x/%x : %x/%x\n",
1653 spec->multi_ios,
1654 spec->multi_io[0].pin, spec->multi_io[1].pin,
1655 spec->multi_io[0].dac, spec->multi_io[1].dac);
Takashi Iwaia7694092013-01-21 10:43:18 +01001656 for (i = 0; i < spec->multi_ios; i++)
1657 print_nid_path_idx(codec, " mio",
1658 spec->out_paths[cfg->line_outs + i]);
1659 if (cfg->hp_outs)
1660 debug_badness("hp_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001661 cfg->hp_pins[0], cfg->hp_pins[1],
Takashi Iwai708122e2012-12-20 17:56:57 +01001662 cfg->hp_pins[2], cfg->hp_pins[3],
Takashi Iwai352f7f92012-12-19 12:52:06 +01001663 spec->multiout.hp_out_nid[0],
1664 spec->multiout.hp_out_nid[1],
1665 spec->multiout.hp_out_nid[2],
1666 spec->multiout.hp_out_nid[3]);
Takashi Iwaia7694092013-01-21 10:43:18 +01001667 for (i = 0; i < cfg->hp_outs; i++)
1668 print_nid_path_idx(codec, " hp ", spec->hp_paths[i]);
1669 if (cfg->speaker_outs)
1670 debug_badness("spk_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001671 cfg->speaker_pins[0], cfg->speaker_pins[1],
1672 cfg->speaker_pins[2], cfg->speaker_pins[3],
1673 spec->multiout.extra_out_nid[0],
1674 spec->multiout.extra_out_nid[1],
1675 spec->multiout.extra_out_nid[2],
1676 spec->multiout.extra_out_nid[3]);
Takashi Iwaia7694092013-01-21 10:43:18 +01001677 for (i = 0; i < cfg->speaker_outs; i++)
1678 print_nid_path_idx(codec, " spk", spec->speaker_paths[i]);
1679 for (i = 0; i < 3; i++)
1680 print_nid_path_idx(codec, " mix", spec->aamix_out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001681}
Takashi Iwaia7694092013-01-21 10:43:18 +01001682#else
1683#define debug_show_configs(codec, cfg) /* NOP */
1684#endif
Takashi Iwai352f7f92012-12-19 12:52:06 +01001685
1686/* find all available DACs of the codec */
1687static void fill_all_dac_nids(struct hda_codec *codec)
1688{
1689 struct hda_gen_spec *spec = codec->spec;
1690 int i;
1691 hda_nid_t nid = codec->start_nid;
1692
1693 spec->num_all_dacs = 0;
1694 memset(spec->all_dacs, 0, sizeof(spec->all_dacs));
1695 for (i = 0; i < codec->num_nodes; i++, nid++) {
1696 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_AUD_OUT)
1697 continue;
1698 if (spec->num_all_dacs >= ARRAY_SIZE(spec->all_dacs)) {
1699 snd_printk(KERN_ERR "hda: Too many DACs!\n");
1700 break;
1701 }
1702 spec->all_dacs[spec->num_all_dacs++] = nid;
1703 }
1704}
1705
1706static int parse_output_paths(struct hda_codec *codec)
1707{
1708 struct hda_gen_spec *spec = codec->spec;
1709 struct auto_pin_cfg *cfg = &spec->autocfg;
1710 struct auto_pin_cfg *best_cfg;
Takashi Iwai9314a582013-01-21 10:49:05 +01001711 unsigned int val;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001712 int best_badness = INT_MAX;
1713 int badness;
1714 bool fill_hardwired = true, fill_mio_first = true;
1715 bool best_wired = true, best_mio = true;
1716 bool hp_spk_swapped = false;
1717
Takashi Iwai352f7f92012-12-19 12:52:06 +01001718 best_cfg = kmalloc(sizeof(*best_cfg), GFP_KERNEL);
1719 if (!best_cfg)
1720 return -ENOMEM;
1721 *best_cfg = *cfg;
1722
1723 for (;;) {
1724 badness = fill_and_eval_dacs(codec, fill_hardwired,
1725 fill_mio_first);
1726 if (badness < 0) {
1727 kfree(best_cfg);
1728 return badness;
1729 }
1730 debug_badness("==> lo_type=%d, wired=%d, mio=%d, badness=0x%x\n",
1731 cfg->line_out_type, fill_hardwired, fill_mio_first,
1732 badness);
Takashi Iwaia7694092013-01-21 10:43:18 +01001733 debug_show_configs(codec, cfg);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001734 if (badness < best_badness) {
1735 best_badness = badness;
1736 *best_cfg = *cfg;
1737 best_wired = fill_hardwired;
1738 best_mio = fill_mio_first;
1739 }
1740 if (!badness)
1741 break;
1742 fill_mio_first = !fill_mio_first;
1743 if (!fill_mio_first)
1744 continue;
1745 fill_hardwired = !fill_hardwired;
1746 if (!fill_hardwired)
1747 continue;
1748 if (hp_spk_swapped)
1749 break;
1750 hp_spk_swapped = true;
1751 if (cfg->speaker_outs > 0 &&
1752 cfg->line_out_type == AUTO_PIN_HP_OUT) {
1753 cfg->hp_outs = cfg->line_outs;
1754 memcpy(cfg->hp_pins, cfg->line_out_pins,
1755 sizeof(cfg->hp_pins));
1756 cfg->line_outs = cfg->speaker_outs;
1757 memcpy(cfg->line_out_pins, cfg->speaker_pins,
1758 sizeof(cfg->speaker_pins));
1759 cfg->speaker_outs = 0;
1760 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
1761 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
1762 fill_hardwired = true;
1763 continue;
1764 }
1765 if (cfg->hp_outs > 0 &&
1766 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
1767 cfg->speaker_outs = cfg->line_outs;
1768 memcpy(cfg->speaker_pins, cfg->line_out_pins,
1769 sizeof(cfg->speaker_pins));
1770 cfg->line_outs = cfg->hp_outs;
1771 memcpy(cfg->line_out_pins, cfg->hp_pins,
1772 sizeof(cfg->hp_pins));
1773 cfg->hp_outs = 0;
1774 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
1775 cfg->line_out_type = AUTO_PIN_HP_OUT;
1776 fill_hardwired = true;
1777 continue;
1778 }
1779 break;
1780 }
1781
1782 if (badness) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001783 debug_badness("==> restoring best_cfg\n");
Takashi Iwai352f7f92012-12-19 12:52:06 +01001784 *cfg = *best_cfg;
1785 fill_and_eval_dacs(codec, best_wired, best_mio);
1786 }
1787 debug_badness("==> Best config: lo_type=%d, wired=%d, mio=%d\n",
1788 cfg->line_out_type, best_wired, best_mio);
Takashi Iwaia7694092013-01-21 10:43:18 +01001789 debug_show_configs(codec, cfg);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001790
1791 if (cfg->line_out_pins[0]) {
1792 struct nid_path *path;
Takashi Iwai196c17662013-01-04 15:01:40 +01001793 path = snd_hda_get_path_from_idx(codec, spec->out_paths[0]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001794 if (path)
1795 spec->vmaster_nid = look_for_out_vol_nid(codec, path);
Takashi Iwai7a71bbf2013-01-17 10:25:15 +01001796 if (spec->vmaster_nid)
1797 snd_hda_set_vmaster_tlv(codec, spec->vmaster_nid,
1798 HDA_OUTPUT, spec->vmaster_tlv);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001799 }
1800
Takashi Iwai9314a582013-01-21 10:49:05 +01001801 /* set initial pinctl targets */
1802 if (spec->prefer_hp_amp || cfg->line_out_type == AUTO_PIN_HP_OUT)
1803 val = PIN_HP;
1804 else
1805 val = PIN_OUT;
1806 set_pin_targets(codec, cfg->line_outs, cfg->line_out_pins, val);
1807 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1808 set_pin_targets(codec, cfg->hp_outs, cfg->hp_pins, PIN_HP);
1809 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1810 val = spec->prefer_hp_amp ? PIN_HP : PIN_OUT;
1811 set_pin_targets(codec, cfg->speaker_outs,
1812 cfg->speaker_pins, val);
1813 }
1814
Takashi Iwai55a63d42013-03-21 17:20:12 +01001815 /* clear indep_hp flag if not available */
1816 if (spec->indep_hp && !indep_hp_possible(codec))
1817 spec->indep_hp = 0;
1818
Takashi Iwai352f7f92012-12-19 12:52:06 +01001819 kfree(best_cfg);
1820 return 0;
1821}
1822
1823/* add playback controls from the parsed DAC table */
1824static int create_multi_out_ctls(struct hda_codec *codec,
1825 const struct auto_pin_cfg *cfg)
1826{
1827 struct hda_gen_spec *spec = codec->spec;
1828 int i, err, noutputs;
1829
1830 noutputs = cfg->line_outs;
1831 if (spec->multi_ios > 0 && cfg->line_outs < 3)
1832 noutputs += spec->multi_ios;
1833
1834 for (i = 0; i < noutputs; i++) {
1835 const char *name;
1836 int index;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001837 struct nid_path *path;
1838
Takashi Iwai196c17662013-01-04 15:01:40 +01001839 path = snd_hda_get_path_from_idx(codec, spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001840 if (!path)
1841 continue;
Takashi Iwai247d85e2013-01-17 16:18:11 +01001842
1843 name = get_line_out_pfx(codec, i, &index, NID_PATH_VOL_CTL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001844 if (!name || !strcmp(name, "CLFE")) {
1845 /* Center/LFE */
1846 err = add_vol_ctl(codec, "Center", 0, 1, path);
1847 if (err < 0)
1848 return err;
1849 err = add_vol_ctl(codec, "LFE", 0, 2, path);
1850 if (err < 0)
1851 return err;
Takashi Iwai247d85e2013-01-17 16:18:11 +01001852 } else {
1853 err = add_stereo_vol(codec, name, index, path);
1854 if (err < 0)
1855 return err;
1856 }
1857
1858 name = get_line_out_pfx(codec, i, &index, NID_PATH_MUTE_CTL);
1859 if (!name || !strcmp(name, "CLFE")) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001860 err = add_sw_ctl(codec, "Center", 0, 1, path);
1861 if (err < 0)
1862 return err;
1863 err = add_sw_ctl(codec, "LFE", 0, 2, path);
1864 if (err < 0)
1865 return err;
1866 } else {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001867 err = add_stereo_sw(codec, name, index, path);
1868 if (err < 0)
1869 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 }
1871 }
1872 return 0;
1873}
1874
Takashi Iwaic2c80382013-01-07 10:33:57 +01001875static int create_extra_out(struct hda_codec *codec, int path_idx,
Takashi Iwai196c17662013-01-04 15:01:40 +01001876 const char *pfx, int cidx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877{
Takashi Iwai352f7f92012-12-19 12:52:06 +01001878 struct nid_path *path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 int err;
1880
Takashi Iwai196c17662013-01-04 15:01:40 +01001881 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001882 if (!path)
1883 return 0;
Takashi Iwaic2c80382013-01-07 10:33:57 +01001884 err = add_stereo_vol(codec, pfx, cidx, path);
1885 if (err < 0)
1886 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001887 err = add_stereo_sw(codec, pfx, cidx, path);
1888 if (err < 0)
1889 return err;
1890 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891}
1892
Takashi Iwai352f7f92012-12-19 12:52:06 +01001893/* add playback controls for speaker and HP outputs */
1894static int create_extra_outs(struct hda_codec *codec, int num_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001895 const int *paths, const char *pfx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896{
Takashi Iwaic2c80382013-01-07 10:33:57 +01001897 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001898
1899 for (i = 0; i < num_pins; i++) {
Takashi Iwaic2c80382013-01-07 10:33:57 +01001900 const char *name;
1901 char tmp[44];
1902 int err, idx = 0;
1903
1904 if (num_pins == 2 && i == 1 && !strcmp(pfx, "Speaker"))
1905 name = "Bass Speaker";
1906 else if (num_pins >= 3) {
1907 snprintf(tmp, sizeof(tmp), "%s %s",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001908 pfx, channel_name[i]);
Takashi Iwaic2c80382013-01-07 10:33:57 +01001909 name = tmp;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001910 } else {
Takashi Iwaic2c80382013-01-07 10:33:57 +01001911 name = pfx;
1912 idx = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 }
Takashi Iwaic2c80382013-01-07 10:33:57 +01001914 err = create_extra_out(codec, paths[i], name, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001915 if (err < 0)
1916 return err;
1917 }
1918 return 0;
1919}
Takashi Iwai97ec5582006-03-21 11:29:07 +01001920
Takashi Iwai352f7f92012-12-19 12:52:06 +01001921static int create_hp_out_ctls(struct hda_codec *codec)
1922{
1923 struct hda_gen_spec *spec = codec->spec;
1924 return create_extra_outs(codec, spec->autocfg.hp_outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001925 spec->hp_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001926 "Headphone");
1927}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928
Takashi Iwai352f7f92012-12-19 12:52:06 +01001929static int create_speaker_out_ctls(struct hda_codec *codec)
1930{
1931 struct hda_gen_spec *spec = codec->spec;
1932 return create_extra_outs(codec, spec->autocfg.speaker_outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001933 spec->speaker_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001934 "Speaker");
1935}
1936
1937/*
Takashi Iwai38cf6f12012-12-21 14:09:42 +01001938 * independent HP controls
1939 */
1940
Takashi Iwai8ba955c2013-03-07 18:40:58 +01001941/* update HP auto-mute state too */
1942static void update_hp_automute_hook(struct hda_codec *codec)
1943{
1944 struct hda_gen_spec *spec = codec->spec;
1945
1946 if (spec->hp_automute_hook)
1947 spec->hp_automute_hook(codec, NULL);
1948 else
1949 snd_hda_gen_hp_automute(codec, NULL);
1950}
1951
Takashi Iwai38cf6f12012-12-21 14:09:42 +01001952static int indep_hp_info(struct snd_kcontrol *kcontrol,
1953 struct snd_ctl_elem_info *uinfo)
1954{
1955 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
1956}
1957
1958static int indep_hp_get(struct snd_kcontrol *kcontrol,
1959 struct snd_ctl_elem_value *ucontrol)
1960{
1961 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1962 struct hda_gen_spec *spec = codec->spec;
1963 ucontrol->value.enumerated.item[0] = spec->indep_hp_enabled;
1964 return 0;
1965}
1966
Takashi Iwaia1e908e2013-01-21 15:11:25 +01001967static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
1968 int nomix_path_idx, int mix_path_idx,
1969 int out_type);
1970
Takashi Iwai38cf6f12012-12-21 14:09:42 +01001971static int indep_hp_put(struct snd_kcontrol *kcontrol,
1972 struct snd_ctl_elem_value *ucontrol)
1973{
1974 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1975 struct hda_gen_spec *spec = codec->spec;
1976 unsigned int select = ucontrol->value.enumerated.item[0];
1977 int ret = 0;
1978
1979 mutex_lock(&spec->pcm_mutex);
1980 if (spec->active_streams) {
1981 ret = -EBUSY;
1982 goto unlock;
1983 }
1984
1985 if (spec->indep_hp_enabled != select) {
Takashi Iwaia1e908e2013-01-21 15:11:25 +01001986 hda_nid_t *dacp;
1987 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
1988 dacp = &spec->private_dac_nids[0];
1989 else
1990 dacp = &spec->multiout.hp_out_nid[0];
1991
1992 /* update HP aamix paths in case it conflicts with indep HP */
1993 if (spec->have_aamix_ctl) {
1994 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
1995 update_aamix_paths(codec, spec->aamix_mode,
1996 spec->out_paths[0],
1997 spec->aamix_out_paths[0],
1998 spec->autocfg.line_out_type);
1999 else
2000 update_aamix_paths(codec, spec->aamix_mode,
2001 spec->hp_paths[0],
2002 spec->aamix_out_paths[1],
2003 AUTO_PIN_HP_OUT);
2004 }
2005
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002006 spec->indep_hp_enabled = select;
2007 if (spec->indep_hp_enabled)
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002008 *dacp = 0;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002009 else
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002010 *dacp = spec->alt_dac_nid;
Takashi Iwai92603c52013-01-22 07:46:31 +01002011
Takashi Iwai8ba955c2013-03-07 18:40:58 +01002012 update_hp_automute_hook(codec);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002013 ret = 1;
2014 }
2015 unlock:
2016 mutex_unlock(&spec->pcm_mutex);
2017 return ret;
2018}
2019
2020static const struct snd_kcontrol_new indep_hp_ctl = {
2021 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2022 .name = "Independent HP",
2023 .info = indep_hp_info,
2024 .get = indep_hp_get,
2025 .put = indep_hp_put,
2026};
2027
2028
2029static int create_indep_hp_ctls(struct hda_codec *codec)
2030{
2031 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002032 hda_nid_t dac;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002033
2034 if (!spec->indep_hp)
2035 return 0;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002036 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
2037 dac = spec->multiout.dac_nids[0];
2038 else
2039 dac = spec->multiout.hp_out_nid[0];
2040 if (!dac) {
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002041 spec->indep_hp = 0;
2042 return 0;
2043 }
2044
2045 spec->indep_hp_enabled = false;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002046 spec->alt_dac_nid = dac;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002047 if (!snd_hda_gen_add_kctl(spec, NULL, &indep_hp_ctl))
2048 return -ENOMEM;
2049 return 0;
2050}
2051
2052/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01002053 * channel mode enum control
2054 */
2055
2056static int ch_mode_info(struct snd_kcontrol *kcontrol,
2057 struct snd_ctl_elem_info *uinfo)
2058{
2059 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2060 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002061 int chs;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002062
2063 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2064 uinfo->count = 1;
2065 uinfo->value.enumerated.items = spec->multi_ios + 1;
2066 if (uinfo->value.enumerated.item > spec->multi_ios)
2067 uinfo->value.enumerated.item = spec->multi_ios;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002068 chs = uinfo->value.enumerated.item * 2 + spec->min_channel_count;
2069 sprintf(uinfo->value.enumerated.name, "%dch", chs);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002070 return 0;
2071}
2072
2073static int ch_mode_get(struct snd_kcontrol *kcontrol,
2074 struct snd_ctl_elem_value *ucontrol)
2075{
2076 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2077 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002078 ucontrol->value.enumerated.item[0] =
2079 (spec->ext_channel_count - spec->min_channel_count) / 2;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002080 return 0;
2081}
2082
Takashi Iwai196c17662013-01-04 15:01:40 +01002083static inline struct nid_path *
2084get_multiio_path(struct hda_codec *codec, int idx)
2085{
2086 struct hda_gen_spec *spec = codec->spec;
2087 return snd_hda_get_path_from_idx(codec,
2088 spec->out_paths[spec->autocfg.line_outs + idx]);
2089}
2090
Takashi Iwaia5cc2502013-01-16 18:08:55 +01002091static void update_automute_all(struct hda_codec *codec);
2092
Takashi Iwai65033cc2013-04-16 12:31:05 +02002093/* Default value to be passed as aamix argument for snd_hda_activate_path();
2094 * used for output paths
2095 */
2096static bool aamix_default(struct hda_gen_spec *spec)
2097{
2098 return !spec->have_aamix_ctl || spec->aamix_mode;
2099}
2100
Takashi Iwai352f7f92012-12-19 12:52:06 +01002101static int set_multi_io(struct hda_codec *codec, int idx, bool output)
2102{
2103 struct hda_gen_spec *spec = codec->spec;
2104 hda_nid_t nid = spec->multi_io[idx].pin;
2105 struct nid_path *path;
2106
Takashi Iwai196c17662013-01-04 15:01:40 +01002107 path = get_multiio_path(codec, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002108 if (!path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 return -EINVAL;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002110
2111 if (path->active == output)
2112 return 0;
2113
2114 if (output) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01002115 set_pin_target(codec, nid, PIN_OUT, true);
Takashi Iwai65033cc2013-04-16 12:31:05 +02002116 snd_hda_activate_path(codec, path, true, aamix_default(spec));
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01002117 set_pin_eapd(codec, nid, true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002118 } else {
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01002119 set_pin_eapd(codec, nid, false);
Takashi Iwai65033cc2013-04-16 12:31:05 +02002120 snd_hda_activate_path(codec, path, false, aamix_default(spec));
Takashi Iwai2c12c302013-01-10 09:33:29 +01002121 set_pin_target(codec, nid, spec->multi_io[idx].ctl_in, true);
Takashi Iwai55196ff2013-01-24 17:32:56 +01002122 path_power_down_sync(codec, path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 }
Takashi Iwaia365fed2013-01-10 16:10:06 +01002124
2125 /* update jack retasking in case it modifies any of them */
Takashi Iwaia5cc2502013-01-16 18:08:55 +01002126 update_automute_all(codec);
Takashi Iwaia365fed2013-01-10 16:10:06 +01002127
Takashi Iwai352f7f92012-12-19 12:52:06 +01002128 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129}
2130
Takashi Iwai352f7f92012-12-19 12:52:06 +01002131static int ch_mode_put(struct snd_kcontrol *kcontrol,
2132 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002134 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2135 struct hda_gen_spec *spec = codec->spec;
2136 int i, ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137
Takashi Iwai352f7f92012-12-19 12:52:06 +01002138 ch = ucontrol->value.enumerated.item[0];
2139 if (ch < 0 || ch > spec->multi_ios)
2140 return -EINVAL;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002141 if (ch == (spec->ext_channel_count - spec->min_channel_count) / 2)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002142 return 0;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002143 spec->ext_channel_count = ch * 2 + spec->min_channel_count;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002144 for (i = 0; i < spec->multi_ios; i++)
2145 set_multi_io(codec, i, i < ch);
2146 spec->multiout.max_channels = max(spec->ext_channel_count,
2147 spec->const_channel_count);
2148 if (spec->need_dac_fix)
2149 spec->multiout.num_dacs = spec->multiout.max_channels / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 return 1;
2151}
2152
Takashi Iwai352f7f92012-12-19 12:52:06 +01002153static const struct snd_kcontrol_new channel_mode_enum = {
2154 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2155 .name = "Channel Mode",
2156 .info = ch_mode_info,
2157 .get = ch_mode_get,
2158 .put = ch_mode_put,
2159};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160
Takashi Iwai352f7f92012-12-19 12:52:06 +01002161static int create_multi_channel_mode(struct hda_codec *codec)
2162{
2163 struct hda_gen_spec *spec = codec->spec;
2164
2165 if (spec->multi_ios > 0) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01002166 if (!snd_hda_gen_add_kctl(spec, NULL, &channel_mode_enum))
Takashi Iwai352f7f92012-12-19 12:52:06 +01002167 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 return 0;
2170}
2171
Takashi Iwai352f7f92012-12-19 12:52:06 +01002172/*
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002173 * aamix loopback enable/disable switch
2174 */
2175
2176#define loopback_mixing_info indep_hp_info
2177
2178static int loopback_mixing_get(struct snd_kcontrol *kcontrol,
2179 struct snd_ctl_elem_value *ucontrol)
2180{
2181 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2182 struct hda_gen_spec *spec = codec->spec;
2183 ucontrol->value.enumerated.item[0] = spec->aamix_mode;
2184 return 0;
2185}
2186
2187static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002188 int nomix_path_idx, int mix_path_idx,
2189 int out_type)
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002190{
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002191 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002192 struct nid_path *nomix_path, *mix_path;
2193
2194 nomix_path = snd_hda_get_path_from_idx(codec, nomix_path_idx);
2195 mix_path = snd_hda_get_path_from_idx(codec, mix_path_idx);
2196 if (!nomix_path || !mix_path)
2197 return;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002198
2199 /* if HP aamix path is driven from a different DAC and the
2200 * independent HP mode is ON, can't turn on aamix path
2201 */
2202 if (out_type == AUTO_PIN_HP_OUT && spec->indep_hp_enabled &&
2203 mix_path->path[0] != spec->alt_dac_nid)
2204 do_mix = false;
2205
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002206 if (do_mix) {
2207 snd_hda_activate_path(codec, nomix_path, false, true);
2208 snd_hda_activate_path(codec, mix_path, true, true);
Takashi Iwai55196ff2013-01-24 17:32:56 +01002209 path_power_down_sync(codec, nomix_path);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002210 } else {
Takashi Iwai65033cc2013-04-16 12:31:05 +02002211 snd_hda_activate_path(codec, mix_path, false, false);
2212 snd_hda_activate_path(codec, nomix_path, true, false);
Takashi Iwai55196ff2013-01-24 17:32:56 +01002213 path_power_down_sync(codec, mix_path);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002214 }
2215}
2216
2217static int loopback_mixing_put(struct snd_kcontrol *kcontrol,
2218 struct snd_ctl_elem_value *ucontrol)
2219{
2220 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2221 struct hda_gen_spec *spec = codec->spec;
2222 unsigned int val = ucontrol->value.enumerated.item[0];
2223
2224 if (val == spec->aamix_mode)
2225 return 0;
2226 spec->aamix_mode = val;
2227 update_aamix_paths(codec, val, spec->out_paths[0],
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002228 spec->aamix_out_paths[0],
2229 spec->autocfg.line_out_type);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002230 update_aamix_paths(codec, val, spec->hp_paths[0],
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002231 spec->aamix_out_paths[1],
2232 AUTO_PIN_HP_OUT);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002233 update_aamix_paths(codec, val, spec->speaker_paths[0],
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002234 spec->aamix_out_paths[2],
2235 AUTO_PIN_SPEAKER_OUT);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002236 return 1;
2237}
2238
2239static const struct snd_kcontrol_new loopback_mixing_enum = {
2240 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2241 .name = "Loopback Mixing",
2242 .info = loopback_mixing_info,
2243 .get = loopback_mixing_get,
2244 .put = loopback_mixing_put,
2245};
2246
2247static int create_loopback_mixing_ctl(struct hda_codec *codec)
2248{
2249 struct hda_gen_spec *spec = codec->spec;
2250
2251 if (!spec->mixer_nid)
2252 return 0;
2253 if (!(spec->aamix_out_paths[0] || spec->aamix_out_paths[1] ||
2254 spec->aamix_out_paths[2]))
2255 return 0;
2256 if (!snd_hda_gen_add_kctl(spec, NULL, &loopback_mixing_enum))
2257 return -ENOMEM;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002258 spec->have_aamix_ctl = 1;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002259 return 0;
2260}
2261
2262/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01002263 * shared headphone/mic handling
2264 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02002265
Takashi Iwai352f7f92012-12-19 12:52:06 +01002266static void call_update_outputs(struct hda_codec *codec);
2267
2268/* for shared I/O, change the pin-control accordingly */
Takashi Iwai967303d2013-02-19 17:12:42 +01002269static void update_hp_mic(struct hda_codec *codec, int adc_mux, bool force)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002270{
2271 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai967303d2013-02-19 17:12:42 +01002272 bool as_mic;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002273 unsigned int val;
Takashi Iwai967303d2013-02-19 17:12:42 +01002274 hda_nid_t pin;
2275
2276 pin = spec->hp_mic_pin;
2277 as_mic = spec->cur_mux[adc_mux] == spec->hp_mic_mux_idx;
2278
2279 if (!force) {
2280 val = snd_hda_codec_get_pin_target(codec, pin);
2281 if (as_mic) {
2282 if (val & PIN_IN)
2283 return;
2284 } else {
2285 if (val & PIN_OUT)
2286 return;
2287 }
2288 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002289
2290 val = snd_hda_get_default_vref(codec, pin);
Takashi Iwai967303d2013-02-19 17:12:42 +01002291 /* if the HP pin doesn't support VREF and the codec driver gives an
2292 * alternative pin, set up the VREF on that pin instead
2293 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01002294 if (val == AC_PINCTL_VREF_HIZ && spec->shared_mic_vref_pin) {
2295 const hda_nid_t vref_pin = spec->shared_mic_vref_pin;
2296 unsigned int vref_val = snd_hda_get_default_vref(codec, vref_pin);
2297 if (vref_val != AC_PINCTL_VREF_HIZ)
Takashi Iwai7594aa32012-12-20 15:38:40 +01002298 snd_hda_set_pin_ctl_cache(codec, vref_pin,
Takashi Iwai967303d2013-02-19 17:12:42 +01002299 PIN_IN | (as_mic ? vref_val : 0));
Takashi Iwaicb53c622007-08-10 17:21:45 +02002300 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002301
Takashi Iwai8ba955c2013-03-07 18:40:58 +01002302 if (!spec->hp_mic_jack_modes) {
2303 if (as_mic)
2304 val |= PIN_IN;
2305 else
2306 val = PIN_HP;
2307 set_pin_target(codec, pin, val, true);
2308 update_hp_automute_hook(codec);
2309 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002310}
2311
2312/* create a shared input with the headphone out */
Takashi Iwai967303d2013-02-19 17:12:42 +01002313static int create_hp_mic(struct hda_codec *codec)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002314{
2315 struct hda_gen_spec *spec = codec->spec;
2316 struct auto_pin_cfg *cfg = &spec->autocfg;
2317 unsigned int defcfg;
2318 hda_nid_t nid;
2319
Takashi Iwai967303d2013-02-19 17:12:42 +01002320 if (!spec->hp_mic) {
2321 if (spec->suppress_hp_mic_detect)
2322 return 0;
2323 /* automatic detection: only if no input or a single internal
2324 * input pin is found, try to detect the shared hp/mic
2325 */
2326 if (cfg->num_inputs > 1)
2327 return 0;
2328 else if (cfg->num_inputs == 1) {
2329 defcfg = snd_hda_codec_get_pincfg(codec, cfg->inputs[0].pin);
2330 if (snd_hda_get_input_pin_attr(defcfg) != INPUT_PIN_ATTR_INT)
2331 return 0;
2332 }
2333 }
2334
2335 spec->hp_mic = 0; /* clear once */
2336 if (cfg->num_inputs >= AUTO_CFG_MAX_INS)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002337 return 0;
2338
Takashi Iwai967303d2013-02-19 17:12:42 +01002339 nid = 0;
2340 if (cfg->line_out_type == AUTO_PIN_HP_OUT && cfg->line_outs > 0)
2341 nid = cfg->line_out_pins[0];
2342 else if (cfg->hp_outs > 0)
2343 nid = cfg->hp_pins[0];
2344 if (!nid)
2345 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002346
2347 if (!(snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_IN))
2348 return 0; /* no input */
2349
Takashi Iwai967303d2013-02-19 17:12:42 +01002350 cfg->inputs[cfg->num_inputs].pin = nid;
2351 cfg->inputs[cfg->num_inputs].type = AUTO_PIN_MIC;
David Henningssoncb420b12013-04-11 11:30:28 +02002352 cfg->inputs[cfg->num_inputs].is_headphone_mic = 1;
Takashi Iwai967303d2013-02-19 17:12:42 +01002353 cfg->num_inputs++;
2354 spec->hp_mic = 1;
2355 spec->hp_mic_pin = nid;
2356 /* we can't handle auto-mic together with HP-mic */
2357 spec->suppress_auto_mic = 1;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002358 snd_printdd("hda-codec: Enable shared I/O jack on NID 0x%x\n", nid);
2359 return 0;
2360}
2361
Takashi Iwai978e77e2013-01-10 16:57:58 +01002362/*
2363 * output jack mode
2364 */
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002365
2366static int create_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t pin);
2367
2368static const char * const out_jack_texts[] = {
2369 "Line Out", "Headphone Out",
2370};
2371
Takashi Iwai978e77e2013-01-10 16:57:58 +01002372static int out_jack_mode_info(struct snd_kcontrol *kcontrol,
2373 struct snd_ctl_elem_info *uinfo)
2374{
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002375 return snd_hda_enum_helper_info(kcontrol, uinfo, 2, out_jack_texts);
Takashi Iwai978e77e2013-01-10 16:57:58 +01002376}
2377
2378static int out_jack_mode_get(struct snd_kcontrol *kcontrol,
2379 struct snd_ctl_elem_value *ucontrol)
2380{
2381 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2382 hda_nid_t nid = kcontrol->private_value;
2383 if (snd_hda_codec_get_pin_target(codec, nid) == PIN_HP)
2384 ucontrol->value.enumerated.item[0] = 1;
2385 else
2386 ucontrol->value.enumerated.item[0] = 0;
2387 return 0;
2388}
2389
2390static int out_jack_mode_put(struct snd_kcontrol *kcontrol,
2391 struct snd_ctl_elem_value *ucontrol)
2392{
2393 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2394 hda_nid_t nid = kcontrol->private_value;
2395 unsigned int val;
2396
2397 val = ucontrol->value.enumerated.item[0] ? PIN_HP : PIN_OUT;
2398 if (snd_hda_codec_get_pin_target(codec, nid) == val)
2399 return 0;
2400 snd_hda_set_pin_ctl_cache(codec, nid, val);
2401 return 1;
2402}
2403
2404static const struct snd_kcontrol_new out_jack_mode_enum = {
2405 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2406 .info = out_jack_mode_info,
2407 .get = out_jack_mode_get,
2408 .put = out_jack_mode_put,
2409};
2410
2411static bool find_kctl_name(struct hda_codec *codec, const char *name, int idx)
2412{
2413 struct hda_gen_spec *spec = codec->spec;
2414 int i;
2415
2416 for (i = 0; i < spec->kctls.used; i++) {
2417 struct snd_kcontrol_new *kctl = snd_array_elem(&spec->kctls, i);
2418 if (!strcmp(kctl->name, name) && kctl->index == idx)
2419 return true;
2420 }
2421 return false;
2422}
2423
2424static void get_jack_mode_name(struct hda_codec *codec, hda_nid_t pin,
2425 char *name, size_t name_len)
2426{
2427 struct hda_gen_spec *spec = codec->spec;
2428 int idx = 0;
2429
2430 snd_hda_get_pin_label(codec, pin, &spec->autocfg, name, name_len, &idx);
2431 strlcat(name, " Jack Mode", name_len);
2432
2433 for (; find_kctl_name(codec, name, idx); idx++)
2434 ;
2435}
2436
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002437static int get_out_jack_num_items(struct hda_codec *codec, hda_nid_t pin)
2438{
2439 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaif811c3c2013-03-07 18:32:59 +01002440 if (spec->add_jack_modes) {
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002441 unsigned int pincap = snd_hda_query_pin_caps(codec, pin);
2442 if ((pincap & AC_PINCAP_OUT) && (pincap & AC_PINCAP_HP_DRV))
2443 return 2;
2444 }
2445 return 1;
2446}
2447
Takashi Iwai978e77e2013-01-10 16:57:58 +01002448static int create_out_jack_modes(struct hda_codec *codec, int num_pins,
2449 hda_nid_t *pins)
2450{
2451 struct hda_gen_spec *spec = codec->spec;
2452 int i;
2453
2454 for (i = 0; i < num_pins; i++) {
2455 hda_nid_t pin = pins[i];
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002456 if (pin == spec->hp_mic_pin) {
2457 int ret = create_hp_mic_jack_mode(codec, pin);
2458 if (ret < 0)
2459 return ret;
2460 continue;
2461 }
2462 if (get_out_jack_num_items(codec, pin) > 1) {
Takashi Iwai978e77e2013-01-10 16:57:58 +01002463 struct snd_kcontrol_new *knew;
2464 char name[44];
2465 get_jack_mode_name(codec, pin, name, sizeof(name));
2466 knew = snd_hda_gen_add_kctl(spec, name,
2467 &out_jack_mode_enum);
2468 if (!knew)
2469 return -ENOMEM;
2470 knew->private_value = pin;
2471 }
2472 }
2473
2474 return 0;
2475}
2476
Takashi Iwai294765582013-01-17 09:52:11 +01002477/*
2478 * input jack mode
2479 */
2480
2481/* from AC_PINCTL_VREF_HIZ to AC_PINCTL_VREF_100 */
2482#define NUM_VREFS 6
2483
2484static const char * const vref_texts[NUM_VREFS] = {
2485 "Line In", "Mic 50pc Bias", "Mic 0V Bias",
2486 "", "Mic 80pc Bias", "Mic 100pc Bias"
2487};
2488
2489static unsigned int get_vref_caps(struct hda_codec *codec, hda_nid_t pin)
2490{
2491 unsigned int pincap;
2492
2493 pincap = snd_hda_query_pin_caps(codec, pin);
2494 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
2495 /* filter out unusual vrefs */
2496 pincap &= ~(AC_PINCAP_VREF_GRD | AC_PINCAP_VREF_100);
2497 return pincap;
2498}
2499
2500/* convert from the enum item index to the vref ctl index (0=HIZ, 1=50%...) */
2501static int get_vref_idx(unsigned int vref_caps, unsigned int item_idx)
2502{
2503 unsigned int i, n = 0;
2504
2505 for (i = 0; i < NUM_VREFS; i++) {
2506 if (vref_caps & (1 << i)) {
2507 if (n == item_idx)
2508 return i;
2509 n++;
2510 }
2511 }
2512 return 0;
2513}
2514
2515/* convert back from the vref ctl index to the enum item index */
2516static int cvt_from_vref_idx(unsigned int vref_caps, unsigned int idx)
2517{
2518 unsigned int i, n = 0;
2519
2520 for (i = 0; i < NUM_VREFS; i++) {
2521 if (i == idx)
2522 return n;
2523 if (vref_caps & (1 << i))
2524 n++;
2525 }
2526 return 0;
2527}
2528
2529static int in_jack_mode_info(struct snd_kcontrol *kcontrol,
2530 struct snd_ctl_elem_info *uinfo)
2531{
2532 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2533 hda_nid_t nid = kcontrol->private_value;
2534 unsigned int vref_caps = get_vref_caps(codec, nid);
2535
2536 snd_hda_enum_helper_info(kcontrol, uinfo, hweight32(vref_caps),
2537 vref_texts);
2538 /* set the right text */
2539 strcpy(uinfo->value.enumerated.name,
2540 vref_texts[get_vref_idx(vref_caps, uinfo->value.enumerated.item)]);
2541 return 0;
2542}
2543
2544static int in_jack_mode_get(struct snd_kcontrol *kcontrol,
2545 struct snd_ctl_elem_value *ucontrol)
2546{
2547 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2548 hda_nid_t nid = kcontrol->private_value;
2549 unsigned int vref_caps = get_vref_caps(codec, nid);
2550 unsigned int idx;
2551
2552 idx = snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_VREFEN;
2553 ucontrol->value.enumerated.item[0] = cvt_from_vref_idx(vref_caps, idx);
2554 return 0;
2555}
2556
2557static int in_jack_mode_put(struct snd_kcontrol *kcontrol,
2558 struct snd_ctl_elem_value *ucontrol)
2559{
2560 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2561 hda_nid_t nid = kcontrol->private_value;
2562 unsigned int vref_caps = get_vref_caps(codec, nid);
2563 unsigned int val, idx;
2564
2565 val = snd_hda_codec_get_pin_target(codec, nid);
2566 idx = cvt_from_vref_idx(vref_caps, val & AC_PINCTL_VREFEN);
2567 if (idx == ucontrol->value.enumerated.item[0])
2568 return 0;
2569
2570 val &= ~AC_PINCTL_VREFEN;
2571 val |= get_vref_idx(vref_caps, ucontrol->value.enumerated.item[0]);
2572 snd_hda_set_pin_ctl_cache(codec, nid, val);
2573 return 1;
2574}
2575
2576static const struct snd_kcontrol_new in_jack_mode_enum = {
2577 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2578 .info = in_jack_mode_info,
2579 .get = in_jack_mode_get,
2580 .put = in_jack_mode_put,
2581};
2582
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002583static int get_in_jack_num_items(struct hda_codec *codec, hda_nid_t pin)
2584{
2585 struct hda_gen_spec *spec = codec->spec;
2586 int nitems = 0;
Takashi Iwaif811c3c2013-03-07 18:32:59 +01002587 if (spec->add_jack_modes)
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002588 nitems = hweight32(get_vref_caps(codec, pin));
2589 return nitems ? nitems : 1;
2590}
2591
Takashi Iwai294765582013-01-17 09:52:11 +01002592static int create_in_jack_mode(struct hda_codec *codec, hda_nid_t pin)
2593{
2594 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai294765582013-01-17 09:52:11 +01002595 struct snd_kcontrol_new *knew;
2596 char name[44];
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002597 unsigned int defcfg;
2598
Takashi Iwaif811c3c2013-03-07 18:32:59 +01002599 if (pin == spec->hp_mic_pin)
2600 return 0; /* already done in create_out_jack_mode() */
Takashi Iwai294765582013-01-17 09:52:11 +01002601
2602 /* no jack mode for fixed pins */
2603 defcfg = snd_hda_codec_get_pincfg(codec, pin);
2604 if (snd_hda_get_input_pin_attr(defcfg) == INPUT_PIN_ATTR_INT)
2605 return 0;
2606
2607 /* no multiple vref caps? */
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002608 if (get_in_jack_num_items(codec, pin) <= 1)
Takashi Iwai294765582013-01-17 09:52:11 +01002609 return 0;
2610
2611 get_jack_mode_name(codec, pin, name, sizeof(name));
2612 knew = snd_hda_gen_add_kctl(spec, name, &in_jack_mode_enum);
2613 if (!knew)
2614 return -ENOMEM;
2615 knew->private_value = pin;
2616 return 0;
2617}
2618
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002619/*
2620 * HP/mic shared jack mode
2621 */
2622static int hp_mic_jack_mode_info(struct snd_kcontrol *kcontrol,
2623 struct snd_ctl_elem_info *uinfo)
2624{
2625 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2626 hda_nid_t nid = kcontrol->private_value;
2627 int out_jacks = get_out_jack_num_items(codec, nid);
2628 int in_jacks = get_in_jack_num_items(codec, nid);
2629 const char *text = NULL;
2630 int idx;
2631
2632 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2633 uinfo->count = 1;
2634 uinfo->value.enumerated.items = out_jacks + in_jacks;
2635 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2636 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
2637 idx = uinfo->value.enumerated.item;
2638 if (idx < out_jacks) {
2639 if (out_jacks > 1)
2640 text = out_jack_texts[idx];
2641 else
2642 text = "Headphone Out";
2643 } else {
2644 idx -= out_jacks;
2645 if (in_jacks > 1) {
2646 unsigned int vref_caps = get_vref_caps(codec, nid);
2647 text = vref_texts[get_vref_idx(vref_caps, idx)];
2648 } else
2649 text = "Mic In";
2650 }
2651
2652 strcpy(uinfo->value.enumerated.name, text);
2653 return 0;
2654}
2655
2656static int get_cur_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t nid)
2657{
2658 int out_jacks = get_out_jack_num_items(codec, nid);
2659 int in_jacks = get_in_jack_num_items(codec, nid);
2660 unsigned int val = snd_hda_codec_get_pin_target(codec, nid);
2661 int idx = 0;
2662
2663 if (val & PIN_OUT) {
2664 if (out_jacks > 1 && val == PIN_HP)
2665 idx = 1;
2666 } else if (val & PIN_IN) {
2667 idx = out_jacks;
2668 if (in_jacks > 1) {
2669 unsigned int vref_caps = get_vref_caps(codec, nid);
2670 val &= AC_PINCTL_VREFEN;
2671 idx += cvt_from_vref_idx(vref_caps, val);
2672 }
2673 }
2674 return idx;
2675}
2676
2677static int hp_mic_jack_mode_get(struct snd_kcontrol *kcontrol,
2678 struct snd_ctl_elem_value *ucontrol)
2679{
2680 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2681 hda_nid_t nid = kcontrol->private_value;
2682 ucontrol->value.enumerated.item[0] =
2683 get_cur_hp_mic_jack_mode(codec, nid);
2684 return 0;
2685}
2686
2687static int hp_mic_jack_mode_put(struct snd_kcontrol *kcontrol,
2688 struct snd_ctl_elem_value *ucontrol)
2689{
2690 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2691 hda_nid_t nid = kcontrol->private_value;
2692 int out_jacks = get_out_jack_num_items(codec, nid);
2693 int in_jacks = get_in_jack_num_items(codec, nid);
2694 unsigned int val, oldval, idx;
2695
2696 oldval = get_cur_hp_mic_jack_mode(codec, nid);
2697 idx = ucontrol->value.enumerated.item[0];
2698 if (oldval == idx)
2699 return 0;
2700
2701 if (idx < out_jacks) {
2702 if (out_jacks > 1)
2703 val = idx ? PIN_HP : PIN_OUT;
2704 else
2705 val = PIN_HP;
2706 } else {
2707 idx -= out_jacks;
2708 if (in_jacks > 1) {
2709 unsigned int vref_caps = get_vref_caps(codec, nid);
2710 val = snd_hda_codec_get_pin_target(codec, nid);
Takashi Iwai3f550e32013-03-07 18:30:27 +01002711 val &= ~(AC_PINCTL_VREFEN | PIN_HP);
2712 val |= get_vref_idx(vref_caps, idx) | PIN_IN;
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002713 } else
2714 val = snd_hda_get_default_vref(codec, nid);
2715 }
2716 snd_hda_set_pin_ctl_cache(codec, nid, val);
Takashi Iwai8ba955c2013-03-07 18:40:58 +01002717 update_hp_automute_hook(codec);
2718
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002719 return 1;
2720}
2721
2722static const struct snd_kcontrol_new hp_mic_jack_mode_enum = {
2723 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2724 .info = hp_mic_jack_mode_info,
2725 .get = hp_mic_jack_mode_get,
2726 .put = hp_mic_jack_mode_put,
2727};
2728
2729static int create_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t pin)
2730{
2731 struct hda_gen_spec *spec = codec->spec;
2732 struct snd_kcontrol_new *knew;
2733
2734 if (get_out_jack_num_items(codec, pin) <= 1 &&
2735 get_in_jack_num_items(codec, pin) <= 1)
2736 return 0; /* no need */
2737 knew = snd_hda_gen_add_kctl(spec, "Headphone Mic Jack Mode",
2738 &hp_mic_jack_mode_enum);
2739 if (!knew)
2740 return -ENOMEM;
2741 knew->private_value = pin;
Takashi Iwai8ba955c2013-03-07 18:40:58 +01002742 spec->hp_mic_jack_modes = 1;
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002743 return 0;
2744}
Takashi Iwai352f7f92012-12-19 12:52:06 +01002745
2746/*
2747 * Parse input paths
2748 */
2749
Takashi Iwai352f7f92012-12-19 12:52:06 +01002750/* add the powersave loopback-list entry */
Takashi Iwai0186f4f2013-02-07 10:45:11 +01002751static int add_loopback_list(struct hda_gen_spec *spec, hda_nid_t mix, int idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002752{
2753 struct hda_amp_list *list;
2754
Takashi Iwai0186f4f2013-02-07 10:45:11 +01002755 list = snd_array_new(&spec->loopback_list);
2756 if (!list)
2757 return -ENOMEM;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002758 list->nid = mix;
2759 list->dir = HDA_INPUT;
2760 list->idx = idx;
Takashi Iwai0186f4f2013-02-07 10:45:11 +01002761 spec->loopback.amplist = spec->loopback_list.list;
2762 return 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02002763}
Takashi Iwaicb53c622007-08-10 17:21:45 +02002764
Takashi Iwai352f7f92012-12-19 12:52:06 +01002765/* create input playback/capture controls for the given pin */
Takashi Iwai196c17662013-01-04 15:01:40 +01002766static int new_analog_input(struct hda_codec *codec, int input_idx,
2767 hda_nid_t pin, const char *ctlname, int ctlidx,
Takashi Iwai352f7f92012-12-19 12:52:06 +01002768 hda_nid_t mix_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002770 struct hda_gen_spec *spec = codec->spec;
2771 struct nid_path *path;
2772 unsigned int val;
2773 int err, idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774
Takashi Iwai352f7f92012-12-19 12:52:06 +01002775 if (!nid_has_volume(codec, mix_nid, HDA_INPUT) &&
2776 !nid_has_mute(codec, mix_nid, HDA_INPUT))
2777 return 0; /* no need for analog loopback */
2778
Takashi Iwai3ca529d2013-01-07 17:25:08 +01002779 path = snd_hda_add_new_path(codec, pin, mix_nid, 0);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002780 if (!path)
2781 return -EINVAL;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01002782 print_nid_path("loopback", path);
Takashi Iwai196c17662013-01-04 15:01:40 +01002783 spec->loopback_paths[input_idx] = snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002784
2785 idx = path->idx[path->depth - 1];
2786 if (nid_has_volume(codec, mix_nid, HDA_INPUT)) {
2787 val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
2788 err = __add_pb_vol_ctrl(spec, HDA_CTL_WIDGET_VOL, ctlname, ctlidx, val);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002789 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002791 path->ctls[NID_PATH_VOL_CTL] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792 }
2793
Takashi Iwai352f7f92012-12-19 12:52:06 +01002794 if (nid_has_mute(codec, mix_nid, HDA_INPUT)) {
2795 val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
2796 err = __add_pb_sw_ctrl(spec, HDA_CTL_WIDGET_MUTE, ctlname, ctlidx, val);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002797 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002799 path->ctls[NID_PATH_MUTE_CTL] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800 }
2801
Takashi Iwai352f7f92012-12-19 12:52:06 +01002802 path->active = true;
Takashi Iwai0186f4f2013-02-07 10:45:11 +01002803 err = add_loopback_list(spec, mix_nid, idx);
2804 if (err < 0)
2805 return err;
Takashi Iwaie4a395e2013-01-23 17:00:31 +01002806
2807 if (spec->mixer_nid != spec->mixer_merge_nid &&
2808 !spec->loopback_merge_path) {
2809 path = snd_hda_add_new_path(codec, spec->mixer_nid,
2810 spec->mixer_merge_nid, 0);
2811 if (path) {
2812 print_nid_path("loopback-merge", path);
2813 path->active = true;
2814 spec->loopback_merge_path =
2815 snd_hda_get_path_idx(codec, path);
2816 }
2817 }
2818
Takashi Iwai352f7f92012-12-19 12:52:06 +01002819 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820}
2821
Takashi Iwai352f7f92012-12-19 12:52:06 +01002822static int is_input_pin(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002824 unsigned int pincap = snd_hda_query_pin_caps(codec, nid);
2825 return (pincap & AC_PINCAP_IN) != 0;
2826}
2827
2828/* Parse the codec tree and retrieve ADCs */
2829static int fill_adc_nids(struct hda_codec *codec)
2830{
2831 struct hda_gen_spec *spec = codec->spec;
2832 hda_nid_t nid;
2833 hda_nid_t *adc_nids = spec->adc_nids;
2834 int max_nums = ARRAY_SIZE(spec->adc_nids);
2835 int i, nums = 0;
2836
2837 nid = codec->start_nid;
2838 for (i = 0; i < codec->num_nodes; i++, nid++) {
2839 unsigned int caps = get_wcaps(codec, nid);
2840 int type = get_wcaps_type(caps);
2841
2842 if (type != AC_WID_AUD_IN || (caps & AC_WCAP_DIGITAL))
2843 continue;
2844 adc_nids[nums] = nid;
2845 if (++nums >= max_nums)
2846 break;
2847 }
2848 spec->num_adc_nids = nums;
Takashi Iwai0ffd5342013-01-17 15:53:29 +01002849
2850 /* copy the detected ADCs to all_adcs[] */
2851 spec->num_all_adcs = nums;
2852 memcpy(spec->all_adcs, spec->adc_nids, nums * sizeof(hda_nid_t));
2853
Takashi Iwai352f7f92012-12-19 12:52:06 +01002854 return nums;
2855}
2856
2857/* filter out invalid adc_nids that don't give all active input pins;
2858 * if needed, check whether dynamic ADC-switching is available
2859 */
2860static int check_dyn_adc_switch(struct hda_codec *codec)
2861{
2862 struct hda_gen_spec *spec = codec->spec;
2863 struct hda_input_mux *imux = &spec->input_mux;
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002864 unsigned int ok_bits;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002865 int i, n, nums;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002866
Takashi Iwai352f7f92012-12-19 12:52:06 +01002867 nums = 0;
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002868 ok_bits = 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002869 for (n = 0; n < spec->num_adc_nids; n++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002870 for (i = 0; i < imux->num_items; i++) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002871 if (!spec->input_paths[i][n])
Takashi Iwai352f7f92012-12-19 12:52:06 +01002872 break;
2873 }
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002874 if (i >= imux->num_items) {
2875 ok_bits |= (1 << n);
2876 nums++;
2877 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002878 }
2879
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002880 if (!ok_bits) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002881 /* check whether ADC-switch is possible */
2882 for (i = 0; i < imux->num_items; i++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002883 for (n = 0; n < spec->num_adc_nids; n++) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002884 if (spec->input_paths[i][n]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002885 spec->dyn_adc_idx[i] = n;
2886 break;
2887 }
2888 }
2889 }
2890
2891 snd_printdd("hda-codec: enabling ADC switching\n");
2892 spec->dyn_adc_switch = 1;
2893 } else if (nums != spec->num_adc_nids) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002894 /* shrink the invalid adcs and input paths */
2895 nums = 0;
2896 for (n = 0; n < spec->num_adc_nids; n++) {
2897 if (!(ok_bits & (1 << n)))
2898 continue;
2899 if (n != nums) {
2900 spec->adc_nids[nums] = spec->adc_nids[n];
Takashi Iwai980428c2013-01-09 09:28:20 +01002901 for (i = 0; i < imux->num_items; i++) {
2902 invalidate_nid_path(codec,
2903 spec->input_paths[i][nums]);
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002904 spec->input_paths[i][nums] =
2905 spec->input_paths[i][n];
Takashi Iwai980428c2013-01-09 09:28:20 +01002906 }
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002907 }
2908 nums++;
2909 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002910 spec->num_adc_nids = nums;
2911 }
2912
Takashi Iwai967303d2013-02-19 17:12:42 +01002913 if (imux->num_items == 1 ||
2914 (imux->num_items == 2 && spec->hp_mic)) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002915 snd_printdd("hda-codec: reducing to a single ADC\n");
2916 spec->num_adc_nids = 1; /* reduce to a single ADC */
2917 }
2918
2919 /* single index for individual volumes ctls */
2920 if (!spec->dyn_adc_switch && spec->multi_cap_vol)
2921 spec->num_adc_nids = 1;
2922
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923 return 0;
2924}
2925
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002926/* parse capture source paths from the given pin and create imux items */
2927static int parse_capture_source(struct hda_codec *codec, hda_nid_t pin,
Takashi Iwai9dba2052013-01-18 10:01:15 +01002928 int cfg_idx, int num_adcs,
2929 const char *label, int anchor)
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002930{
2931 struct hda_gen_spec *spec = codec->spec;
2932 struct hda_input_mux *imux = &spec->input_mux;
2933 int imux_idx = imux->num_items;
2934 bool imux_added = false;
2935 int c;
2936
2937 for (c = 0; c < num_adcs; c++) {
2938 struct nid_path *path;
2939 hda_nid_t adc = spec->adc_nids[c];
2940
2941 if (!is_reachable_path(codec, pin, adc))
2942 continue;
2943 path = snd_hda_add_new_path(codec, pin, adc, anchor);
2944 if (!path)
2945 continue;
2946 print_nid_path("input", path);
2947 spec->input_paths[imux_idx][c] =
2948 snd_hda_get_path_idx(codec, path);
2949
2950 if (!imux_added) {
Takashi Iwai967303d2013-02-19 17:12:42 +01002951 if (spec->hp_mic_pin == pin)
2952 spec->hp_mic_mux_idx = imux->num_items;
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002953 spec->imux_pins[imux->num_items] = pin;
Takashi Iwai9dba2052013-01-18 10:01:15 +01002954 snd_hda_add_imux_item(imux, label, cfg_idx, NULL);
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002955 imux_added = true;
2956 }
2957 }
2958
2959 return 0;
2960}
2961
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01002963 * create playback/capture controls for input pins
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964 */
Takashi Iwai9dba2052013-01-18 10:01:15 +01002965
Takashi Iwaic9700422013-01-18 10:17:30 +01002966/* fill the label for each input at first */
2967static int fill_input_pin_labels(struct hda_codec *codec)
2968{
2969 struct hda_gen_spec *spec = codec->spec;
2970 const struct auto_pin_cfg *cfg = &spec->autocfg;
2971 int i;
2972
2973 for (i = 0; i < cfg->num_inputs; i++) {
2974 hda_nid_t pin = cfg->inputs[i].pin;
2975 const char *label;
2976 int j, idx;
2977
2978 if (!is_input_pin(codec, pin))
2979 continue;
2980
2981 label = hda_get_autocfg_input_label(codec, cfg, i);
2982 idx = 0;
David Henningsson8e8db7f2013-01-18 15:43:02 +01002983 for (j = i - 1; j >= 0; j--) {
Takashi Iwaic9700422013-01-18 10:17:30 +01002984 if (spec->input_labels[j] &&
2985 !strcmp(spec->input_labels[j], label)) {
2986 idx = spec->input_label_idxs[j] + 1;
2987 break;
2988 }
2989 }
2990
2991 spec->input_labels[i] = label;
2992 spec->input_label_idxs[i] = idx;
2993 }
2994
2995 return 0;
2996}
2997
Takashi Iwai9dba2052013-01-18 10:01:15 +01002998#define CFG_IDX_MIX 99 /* a dummy cfg->input idx for stereo mix */
2999
Takashi Iwai352f7f92012-12-19 12:52:06 +01003000static int create_input_ctls(struct hda_codec *codec)
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02003001{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003002 struct hda_gen_spec *spec = codec->spec;
3003 const struct auto_pin_cfg *cfg = &spec->autocfg;
3004 hda_nid_t mixer = spec->mixer_nid;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003005 int num_adcs;
Takashi Iwaic9700422013-01-18 10:17:30 +01003006 int i, err;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003007 unsigned int val;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02003008
Takashi Iwai352f7f92012-12-19 12:52:06 +01003009 num_adcs = fill_adc_nids(codec);
3010 if (num_adcs < 0)
3011 return 0;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02003012
Takashi Iwaic9700422013-01-18 10:17:30 +01003013 err = fill_input_pin_labels(codec);
3014 if (err < 0)
3015 return err;
3016
Takashi Iwai352f7f92012-12-19 12:52:06 +01003017 for (i = 0; i < cfg->num_inputs; i++) {
3018 hda_nid_t pin;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019
Takashi Iwai352f7f92012-12-19 12:52:06 +01003020 pin = cfg->inputs[i].pin;
3021 if (!is_input_pin(codec, pin))
3022 continue;
3023
Takashi Iwai2c12c302013-01-10 09:33:29 +01003024 val = PIN_IN;
3025 if (cfg->inputs[i].type == AUTO_PIN_MIC)
3026 val |= snd_hda_get_default_vref(codec, pin);
Takashi Iwai93c9d8a2013-03-11 09:48:43 +01003027 if (pin != spec->hp_mic_pin)
3028 set_pin_target(codec, pin, val, false);
Takashi Iwai2c12c302013-01-10 09:33:29 +01003029
Takashi Iwai352f7f92012-12-19 12:52:06 +01003030 if (mixer) {
3031 if (is_reachable_path(codec, pin, mixer)) {
Takashi Iwai196c17662013-01-04 15:01:40 +01003032 err = new_analog_input(codec, i, pin,
Takashi Iwaic9700422013-01-18 10:17:30 +01003033 spec->input_labels[i],
3034 spec->input_label_idxs[i],
3035 mixer);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003036 if (err < 0)
3037 return err;
3038 }
3039 }
3040
Takashi Iwaic9700422013-01-18 10:17:30 +01003041 err = parse_capture_source(codec, pin, i, num_adcs,
3042 spec->input_labels[i], -mixer);
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003043 if (err < 0)
3044 return err;
Takashi Iwai294765582013-01-17 09:52:11 +01003045
Takashi Iwaif811c3c2013-03-07 18:32:59 +01003046 if (spec->add_jack_modes) {
Takashi Iwai294765582013-01-17 09:52:11 +01003047 err = create_in_jack_mode(codec, pin);
3048 if (err < 0)
3049 return err;
3050 }
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003051 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003052
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003053 if (mixer && spec->add_stereo_mix_input) {
Takashi Iwai9dba2052013-01-18 10:01:15 +01003054 err = parse_capture_source(codec, mixer, CFG_IDX_MIX, num_adcs,
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003055 "Stereo Mix", 0);
3056 if (err < 0)
3057 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003058 }
3059
3060 return 0;
3061}
3062
3063
3064/*
3065 * input source mux
3066 */
3067
Takashi Iwaic697b712013-01-07 17:09:26 +01003068/* get the input path specified by the given adc and imux indices */
3069static struct nid_path *get_input_path(struct hda_codec *codec, int adc_idx, int imux_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003070{
3071 struct hda_gen_spec *spec = codec->spec;
David Henningssonb56fa1e2013-01-16 11:45:35 +01003072 if (imux_idx < 0 || imux_idx >= HDA_MAX_NUM_INPUTS) {
3073 snd_BUG();
3074 return NULL;
3075 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003076 if (spec->dyn_adc_switch)
3077 adc_idx = spec->dyn_adc_idx[imux_idx];
David Henningssond3d982f2013-01-18 15:43:01 +01003078 if (adc_idx < 0 || adc_idx >= AUTO_CFG_MAX_INS) {
David Henningssonb56fa1e2013-01-16 11:45:35 +01003079 snd_BUG();
3080 return NULL;
3081 }
Takashi Iwaic697b712013-01-07 17:09:26 +01003082 return snd_hda_get_path_from_idx(codec, spec->input_paths[imux_idx][adc_idx]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003083}
3084
3085static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
3086 unsigned int idx);
3087
3088static int mux_enum_info(struct snd_kcontrol *kcontrol,
3089 struct snd_ctl_elem_info *uinfo)
3090{
3091 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3092 struct hda_gen_spec *spec = codec->spec;
3093 return snd_hda_input_mux_info(&spec->input_mux, uinfo);
3094}
3095
3096static int mux_enum_get(struct snd_kcontrol *kcontrol,
3097 struct snd_ctl_elem_value *ucontrol)
3098{
3099 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3100 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai2a8d5392013-01-18 16:23:25 +01003101 /* the ctls are created at once with multiple counts */
3102 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003103
3104 ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
3105 return 0;
3106}
3107
3108static int mux_enum_put(struct snd_kcontrol *kcontrol,
3109 struct snd_ctl_elem_value *ucontrol)
3110{
3111 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Takashi Iwai2a8d5392013-01-18 16:23:25 +01003112 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003113 return mux_select(codec, adc_idx,
3114 ucontrol->value.enumerated.item[0]);
3115}
3116
Takashi Iwai352f7f92012-12-19 12:52:06 +01003117static const struct snd_kcontrol_new cap_src_temp = {
3118 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3119 .name = "Input Source",
3120 .info = mux_enum_info,
3121 .get = mux_enum_get,
3122 .put = mux_enum_put,
3123};
3124
Takashi Iwai47d46ab2012-12-20 11:48:54 +01003125/*
3126 * capture volume and capture switch ctls
3127 */
3128
Takashi Iwai352f7f92012-12-19 12:52:06 +01003129typedef int (*put_call_t)(struct snd_kcontrol *kcontrol,
3130 struct snd_ctl_elem_value *ucontrol);
3131
Takashi Iwai47d46ab2012-12-20 11:48:54 +01003132/* call the given amp update function for all amps in the imux list at once */
Takashi Iwai352f7f92012-12-19 12:52:06 +01003133static int cap_put_caller(struct snd_kcontrol *kcontrol,
3134 struct snd_ctl_elem_value *ucontrol,
3135 put_call_t func, int type)
3136{
3137 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3138 struct hda_gen_spec *spec = codec->spec;
3139 const struct hda_input_mux *imux;
3140 struct nid_path *path;
3141 int i, adc_idx, err = 0;
3142
3143 imux = &spec->input_mux;
David Henningssona053d1e2013-01-16 11:45:36 +01003144 adc_idx = kcontrol->id.index;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003145 mutex_lock(&codec->control_mutex);
Takashi Iwai47d46ab2012-12-20 11:48:54 +01003146 /* we use the cache-only update at first since multiple input paths
3147 * may shared the same amp; by updating only caches, the redundant
3148 * writes to hardware can be reduced.
3149 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01003150 codec->cached_write = 1;
3151 for (i = 0; i < imux->num_items; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01003152 path = get_input_path(codec, adc_idx, i);
3153 if (!path || !path->ctls[type])
Takashi Iwai352f7f92012-12-19 12:52:06 +01003154 continue;
3155 kcontrol->private_value = path->ctls[type];
3156 err = func(kcontrol, ucontrol);
3157 if (err < 0)
3158 goto error;
3159 }
3160 error:
3161 codec->cached_write = 0;
3162 mutex_unlock(&codec->control_mutex);
Takashi Iwaidc870f32013-01-22 15:24:30 +01003163 snd_hda_codec_flush_cache(codec); /* flush the updates */
Takashi Iwai352f7f92012-12-19 12:52:06 +01003164 if (err >= 0 && spec->cap_sync_hook)
Takashi Iwaia90229e2013-01-18 14:10:00 +01003165 spec->cap_sync_hook(codec, ucontrol);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003166 return err;
3167}
3168
3169/* capture volume ctl callbacks */
3170#define cap_vol_info snd_hda_mixer_amp_volume_info
3171#define cap_vol_get snd_hda_mixer_amp_volume_get
3172#define cap_vol_tlv snd_hda_mixer_amp_tlv
3173
3174static int cap_vol_put(struct snd_kcontrol *kcontrol,
3175 struct snd_ctl_elem_value *ucontrol)
3176{
3177 return cap_put_caller(kcontrol, ucontrol,
3178 snd_hda_mixer_amp_volume_put,
3179 NID_PATH_VOL_CTL);
3180}
3181
3182static const struct snd_kcontrol_new cap_vol_temp = {
3183 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3184 .name = "Capture Volume",
3185 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
3186 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
3187 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK),
3188 .info = cap_vol_info,
3189 .get = cap_vol_get,
3190 .put = cap_vol_put,
3191 .tlv = { .c = cap_vol_tlv },
3192};
3193
3194/* capture switch ctl callbacks */
3195#define cap_sw_info snd_ctl_boolean_stereo_info
3196#define cap_sw_get snd_hda_mixer_amp_switch_get
3197
3198static int cap_sw_put(struct snd_kcontrol *kcontrol,
3199 struct snd_ctl_elem_value *ucontrol)
3200{
Takashi Iwaia90229e2013-01-18 14:10:00 +01003201 return cap_put_caller(kcontrol, ucontrol,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003202 snd_hda_mixer_amp_switch_put,
3203 NID_PATH_MUTE_CTL);
3204}
3205
3206static const struct snd_kcontrol_new cap_sw_temp = {
3207 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3208 .name = "Capture Switch",
3209 .info = cap_sw_info,
3210 .get = cap_sw_get,
3211 .put = cap_sw_put,
3212};
3213
3214static int parse_capvol_in_path(struct hda_codec *codec, struct nid_path *path)
3215{
3216 hda_nid_t nid;
3217 int i, depth;
3218
3219 path->ctls[NID_PATH_VOL_CTL] = path->ctls[NID_PATH_MUTE_CTL] = 0;
3220 for (depth = 0; depth < 3; depth++) {
3221 if (depth >= path->depth)
3222 return -EINVAL;
3223 i = path->depth - depth - 1;
3224 nid = path->path[i];
3225 if (!path->ctls[NID_PATH_VOL_CTL]) {
3226 if (nid_has_volume(codec, nid, HDA_OUTPUT))
3227 path->ctls[NID_PATH_VOL_CTL] =
3228 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3229 else if (nid_has_volume(codec, nid, HDA_INPUT)) {
3230 int idx = path->idx[i];
3231 if (!depth && codec->single_adc_amp)
3232 idx = 0;
3233 path->ctls[NID_PATH_VOL_CTL] =
3234 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
3235 }
3236 }
3237 if (!path->ctls[NID_PATH_MUTE_CTL]) {
3238 if (nid_has_mute(codec, nid, HDA_OUTPUT))
3239 path->ctls[NID_PATH_MUTE_CTL] =
3240 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3241 else if (nid_has_mute(codec, nid, HDA_INPUT)) {
3242 int idx = path->idx[i];
3243 if (!depth && codec->single_adc_amp)
3244 idx = 0;
3245 path->ctls[NID_PATH_MUTE_CTL] =
3246 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
3247 }
3248 }
Takashi Iwai97ec5582006-03-21 11:29:07 +01003249 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250 return 0;
3251}
3252
Takashi Iwai352f7f92012-12-19 12:52:06 +01003253static bool is_inv_dmic_pin(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003255 struct hda_gen_spec *spec = codec->spec;
3256 struct auto_pin_cfg *cfg = &spec->autocfg;
3257 unsigned int val;
3258 int i;
3259
3260 if (!spec->inv_dmic_split)
3261 return false;
3262 for (i = 0; i < cfg->num_inputs; i++) {
3263 if (cfg->inputs[i].pin != nid)
3264 continue;
3265 if (cfg->inputs[i].type != AUTO_PIN_MIC)
3266 return false;
3267 val = snd_hda_codec_get_pincfg(codec, nid);
3268 return snd_hda_get_input_pin_attr(val) == INPUT_PIN_ATTR_INT;
3269 }
3270 return false;
3271}
3272
Takashi Iwaia90229e2013-01-18 14:10:00 +01003273/* capture switch put callback for a single control with hook call */
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003274static int cap_single_sw_put(struct snd_kcontrol *kcontrol,
3275 struct snd_ctl_elem_value *ucontrol)
3276{
3277 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3278 struct hda_gen_spec *spec = codec->spec;
3279 int ret;
3280
3281 ret = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
3282 if (ret < 0)
3283 return ret;
3284
Takashi Iwaia90229e2013-01-18 14:10:00 +01003285 if (spec->cap_sync_hook)
3286 spec->cap_sync_hook(codec, ucontrol);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003287
3288 return ret;
3289}
3290
Takashi Iwai352f7f92012-12-19 12:52:06 +01003291static int add_single_cap_ctl(struct hda_codec *codec, const char *label,
3292 int idx, bool is_switch, unsigned int ctl,
3293 bool inv_dmic)
3294{
3295 struct hda_gen_spec *spec = codec->spec;
3296 char tmpname[44];
3297 int type = is_switch ? HDA_CTL_WIDGET_MUTE : HDA_CTL_WIDGET_VOL;
3298 const char *sfx = is_switch ? "Switch" : "Volume";
3299 unsigned int chs = inv_dmic ? 1 : 3;
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003300 struct snd_kcontrol_new *knew;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003301
3302 if (!ctl)
3303 return 0;
3304
3305 if (label)
3306 snprintf(tmpname, sizeof(tmpname),
3307 "%s Capture %s", label, sfx);
3308 else
3309 snprintf(tmpname, sizeof(tmpname),
3310 "Capture %s", sfx);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003311 knew = add_control(spec, type, tmpname, idx,
3312 amp_val_replace_channels(ctl, chs));
3313 if (!knew)
3314 return -ENOMEM;
Takashi Iwaia90229e2013-01-18 14:10:00 +01003315 if (is_switch)
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003316 knew->put = cap_single_sw_put;
3317 if (!inv_dmic)
3318 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003319
3320 /* Make independent right kcontrol */
3321 if (label)
3322 snprintf(tmpname, sizeof(tmpname),
3323 "Inverted %s Capture %s", label, sfx);
3324 else
3325 snprintf(tmpname, sizeof(tmpname),
3326 "Inverted Capture %s", sfx);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003327 knew = add_control(spec, type, tmpname, idx,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003328 amp_val_replace_channels(ctl, 2));
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003329 if (!knew)
3330 return -ENOMEM;
Takashi Iwaia90229e2013-01-18 14:10:00 +01003331 if (is_switch)
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003332 knew->put = cap_single_sw_put;
3333 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003334}
3335
3336/* create single (and simple) capture volume and switch controls */
3337static int create_single_cap_vol_ctl(struct hda_codec *codec, int idx,
3338 unsigned int vol_ctl, unsigned int sw_ctl,
3339 bool inv_dmic)
3340{
3341 int err;
3342 err = add_single_cap_ctl(codec, NULL, idx, false, vol_ctl, inv_dmic);
3343 if (err < 0)
3344 return err;
3345 err = add_single_cap_ctl(codec, NULL, idx, true, sw_ctl, inv_dmic);
3346 if (err < 0)
3347 return err;
3348 return 0;
3349}
3350
3351/* create bound capture volume and switch controls */
3352static int create_bind_cap_vol_ctl(struct hda_codec *codec, int idx,
3353 unsigned int vol_ctl, unsigned int sw_ctl)
3354{
3355 struct hda_gen_spec *spec = codec->spec;
3356 struct snd_kcontrol_new *knew;
3357
3358 if (vol_ctl) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01003359 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_vol_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003360 if (!knew)
3361 return -ENOMEM;
3362 knew->index = idx;
3363 knew->private_value = vol_ctl;
3364 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
3365 }
3366 if (sw_ctl) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01003367 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_sw_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003368 if (!knew)
3369 return -ENOMEM;
3370 knew->index = idx;
3371 knew->private_value = sw_ctl;
3372 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
3373 }
3374 return 0;
3375}
3376
3377/* return the vol ctl when used first in the imux list */
3378static unsigned int get_first_cap_ctl(struct hda_codec *codec, int idx, int type)
3379{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003380 struct nid_path *path;
3381 unsigned int ctl;
3382 int i;
3383
Takashi Iwaic697b712013-01-07 17:09:26 +01003384 path = get_input_path(codec, 0, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003385 if (!path)
3386 return 0;
3387 ctl = path->ctls[type];
3388 if (!ctl)
3389 return 0;
3390 for (i = 0; i < idx - 1; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01003391 path = get_input_path(codec, 0, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003392 if (path && path->ctls[type] == ctl)
3393 return 0;
3394 }
3395 return ctl;
3396}
3397
3398/* create individual capture volume and switch controls per input */
3399static int create_multi_cap_vol_ctl(struct hda_codec *codec)
3400{
3401 struct hda_gen_spec *spec = codec->spec;
3402 struct hda_input_mux *imux = &spec->input_mux;
Takashi Iwaic9700422013-01-18 10:17:30 +01003403 int i, err, type;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003404
3405 for (i = 0; i < imux->num_items; i++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003406 bool inv_dmic;
Takashi Iwaic9700422013-01-18 10:17:30 +01003407 int idx;
Takashi Iwai9dba2052013-01-18 10:01:15 +01003408
Takashi Iwaic9700422013-01-18 10:17:30 +01003409 idx = imux->items[i].index;
3410 if (idx >= spec->autocfg.num_inputs)
Takashi Iwai9dba2052013-01-18 10:01:15 +01003411 continue;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003412 inv_dmic = is_inv_dmic_pin(codec, spec->imux_pins[i]);
3413
3414 for (type = 0; type < 2; type++) {
Takashi Iwaic9700422013-01-18 10:17:30 +01003415 err = add_single_cap_ctl(codec,
3416 spec->input_labels[idx],
3417 spec->input_label_idxs[idx],
3418 type,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003419 get_first_cap_ctl(codec, i, type),
3420 inv_dmic);
3421 if (err < 0)
3422 return err;
3423 }
3424 }
3425 return 0;
3426}
3427
3428static int create_capture_mixers(struct hda_codec *codec)
3429{
3430 struct hda_gen_spec *spec = codec->spec;
3431 struct hda_input_mux *imux = &spec->input_mux;
3432 int i, n, nums, err;
3433
3434 if (spec->dyn_adc_switch)
3435 nums = 1;
3436 else
3437 nums = spec->num_adc_nids;
3438
3439 if (!spec->auto_mic && imux->num_items > 1) {
3440 struct snd_kcontrol_new *knew;
Takashi Iwai624d9142012-12-19 17:41:52 +01003441 const char *name;
3442 name = nums > 1 ? "Input Source" : "Capture Source";
3443 knew = snd_hda_gen_add_kctl(spec, name, &cap_src_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003444 if (!knew)
3445 return -ENOMEM;
3446 knew->count = nums;
3447 }
3448
3449 for (n = 0; n < nums; n++) {
3450 bool multi = false;
David Henningsson99a55922013-01-16 15:58:44 +01003451 bool multi_cap_vol = spec->multi_cap_vol;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003452 bool inv_dmic = false;
3453 int vol, sw;
3454
3455 vol = sw = 0;
3456 for (i = 0; i < imux->num_items; i++) {
3457 struct nid_path *path;
Takashi Iwaic697b712013-01-07 17:09:26 +01003458 path = get_input_path(codec, n, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003459 if (!path)
3460 continue;
3461 parse_capvol_in_path(codec, path);
3462 if (!vol)
3463 vol = path->ctls[NID_PATH_VOL_CTL];
David Henningsson99a55922013-01-16 15:58:44 +01003464 else if (vol != path->ctls[NID_PATH_VOL_CTL]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003465 multi = true;
David Henningsson99a55922013-01-16 15:58:44 +01003466 if (!same_amp_caps(codec, vol,
3467 path->ctls[NID_PATH_VOL_CTL], HDA_INPUT))
3468 multi_cap_vol = true;
3469 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003470 if (!sw)
3471 sw = path->ctls[NID_PATH_MUTE_CTL];
David Henningsson99a55922013-01-16 15:58:44 +01003472 else if (sw != path->ctls[NID_PATH_MUTE_CTL]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003473 multi = true;
David Henningsson99a55922013-01-16 15:58:44 +01003474 if (!same_amp_caps(codec, sw,
3475 path->ctls[NID_PATH_MUTE_CTL], HDA_INPUT))
3476 multi_cap_vol = true;
3477 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003478 if (is_inv_dmic_pin(codec, spec->imux_pins[i]))
3479 inv_dmic = true;
3480 }
3481
3482 if (!multi)
3483 err = create_single_cap_vol_ctl(codec, n, vol, sw,
3484 inv_dmic);
David Henningsson99a55922013-01-16 15:58:44 +01003485 else if (!multi_cap_vol)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003486 err = create_bind_cap_vol_ctl(codec, n, vol, sw);
3487 else
3488 err = create_multi_cap_vol_ctl(codec);
3489 if (err < 0)
3490 return err;
3491 }
3492
3493 return 0;
3494}
3495
3496/*
3497 * add mic boosts if needed
3498 */
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003499
3500/* check whether the given amp is feasible as a boost volume */
3501static bool check_boost_vol(struct hda_codec *codec, hda_nid_t nid,
3502 int dir, int idx)
3503{
3504 unsigned int step;
3505
3506 if (!nid_has_volume(codec, nid, dir) ||
3507 is_ctl_associated(codec, nid, dir, idx, NID_PATH_VOL_CTL) ||
3508 is_ctl_associated(codec, nid, dir, idx, NID_PATH_BOOST_CTL))
3509 return false;
3510
3511 step = (query_amp_caps(codec, nid, dir) & AC_AMPCAP_STEP_SIZE)
3512 >> AC_AMPCAP_STEP_SIZE_SHIFT;
3513 if (step < 0x20)
3514 return false;
3515 return true;
3516}
3517
3518/* look for a boost amp in a widget close to the pin */
3519static unsigned int look_for_boost_amp(struct hda_codec *codec,
3520 struct nid_path *path)
3521{
3522 unsigned int val = 0;
3523 hda_nid_t nid;
3524 int depth;
3525
3526 for (depth = 0; depth < 3; depth++) {
3527 if (depth >= path->depth - 1)
3528 break;
3529 nid = path->path[depth];
3530 if (depth && check_boost_vol(codec, nid, HDA_OUTPUT, 0)) {
3531 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3532 break;
3533 } else if (check_boost_vol(codec, nid, HDA_INPUT,
3534 path->idx[depth])) {
3535 val = HDA_COMPOSE_AMP_VAL(nid, 3, path->idx[depth],
3536 HDA_INPUT);
3537 break;
3538 }
3539 }
3540
3541 return val;
3542}
3543
Takashi Iwai352f7f92012-12-19 12:52:06 +01003544static int parse_mic_boost(struct hda_codec *codec)
3545{
3546 struct hda_gen_spec *spec = codec->spec;
3547 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003548 struct hda_input_mux *imux = &spec->input_mux;
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003549 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003550
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003551 if (!spec->num_adc_nids)
3552 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003553
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003554 for (i = 0; i < imux->num_items; i++) {
3555 struct nid_path *path;
3556 unsigned int val;
3557 int idx;
3558 char boost_label[44];
David Henningsson02aba552013-01-16 15:58:43 +01003559
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003560 idx = imux->items[i].index;
3561 if (idx >= imux->num_items)
3562 continue;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003563
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003564 /* check only line-in and mic pins */
Takashi Iwai1799cdd2013-01-18 14:37:16 +01003565 if (cfg->inputs[idx].type > AUTO_PIN_LINE_IN)
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003566 continue;
3567
3568 path = get_input_path(codec, 0, i);
3569 if (!path)
3570 continue;
3571
3572 val = look_for_boost_amp(codec, path);
3573 if (!val)
3574 continue;
3575
3576 /* create a boost control */
3577 snprintf(boost_label, sizeof(boost_label),
3578 "%s Boost Volume", spec->input_labels[idx]);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003579 if (!add_control(spec, HDA_CTL_WIDGET_VOL, boost_label,
3580 spec->input_label_idxs[idx], val))
3581 return -ENOMEM;
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003582
3583 path->ctls[NID_PATH_BOOST_CTL] = val;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003584 }
3585 return 0;
3586}
3587
3588/*
3589 * parse digital I/Os and set up NIDs in BIOS auto-parse mode
3590 */
3591static void parse_digital(struct hda_codec *codec)
3592{
3593 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003594 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003595 int i, nums;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003596 hda_nid_t dig_nid, pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003597
3598 /* support multiple SPDIFs; the secondary is set up as a slave */
3599 nums = 0;
3600 for (i = 0; i < spec->autocfg.dig_outs; i++) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01003601 pin = spec->autocfg.dig_out_pins[i];
Takashi Iwai352f7f92012-12-19 12:52:06 +01003602 dig_nid = look_for_dac(codec, pin, true);
3603 if (!dig_nid)
3604 continue;
Takashi Iwai3ca529d2013-01-07 17:25:08 +01003605 path = snd_hda_add_new_path(codec, dig_nid, pin, 0);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003606 if (!path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003607 continue;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003608 print_nid_path("digout", path);
Takashi Iwaie1284af2013-01-03 16:33:02 +01003609 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +01003610 spec->digout_paths[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai2c12c302013-01-10 09:33:29 +01003611 set_pin_target(codec, pin, PIN_OUT, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003612 if (!nums) {
3613 spec->multiout.dig_out_nid = dig_nid;
3614 spec->dig_out_type = spec->autocfg.dig_out_type[0];
3615 } else {
3616 spec->multiout.slave_dig_outs = spec->slave_dig_outs;
3617 if (nums >= ARRAY_SIZE(spec->slave_dig_outs) - 1)
3618 break;
3619 spec->slave_dig_outs[nums - 1] = dig_nid;
3620 }
3621 nums++;
3622 }
3623
3624 if (spec->autocfg.dig_in_pin) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01003625 pin = spec->autocfg.dig_in_pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003626 dig_nid = codec->start_nid;
3627 for (i = 0; i < codec->num_nodes; i++, dig_nid++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003628 unsigned int wcaps = get_wcaps(codec, dig_nid);
3629 if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
3630 continue;
3631 if (!(wcaps & AC_WCAP_DIGITAL))
3632 continue;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003633 path = snd_hda_add_new_path(codec, pin, dig_nid, 0);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003634 if (path) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003635 print_nid_path("digin", path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003636 path->active = true;
3637 spec->dig_in_nid = dig_nid;
Takashi Iwai2430d7b2013-01-04 15:09:42 +01003638 spec->digin_path = snd_hda_get_path_idx(codec, path);
Takashi Iwai2c12c302013-01-10 09:33:29 +01003639 set_pin_target(codec, pin, PIN_IN, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003640 break;
3641 }
3642 }
3643 }
3644}
3645
3646
3647/*
3648 * input MUX handling
3649 */
3650
3651static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur);
3652
3653/* select the given imux item; either unmute exclusively or select the route */
3654static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
3655 unsigned int idx)
3656{
3657 struct hda_gen_spec *spec = codec->spec;
3658 const struct hda_input_mux *imux;
Takashi Iwai55196ff2013-01-24 17:32:56 +01003659 struct nid_path *old_path, *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003660
3661 imux = &spec->input_mux;
3662 if (!imux->num_items)
3663 return 0;
3664
3665 if (idx >= imux->num_items)
3666 idx = imux->num_items - 1;
3667 if (spec->cur_mux[adc_idx] == idx)
3668 return 0;
3669
Takashi Iwai55196ff2013-01-24 17:32:56 +01003670 old_path = get_input_path(codec, adc_idx, spec->cur_mux[adc_idx]);
3671 if (!old_path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003672 return 0;
Takashi Iwai55196ff2013-01-24 17:32:56 +01003673 if (old_path->active)
3674 snd_hda_activate_path(codec, old_path, false, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003675
3676 spec->cur_mux[adc_idx] = idx;
3677
Takashi Iwai967303d2013-02-19 17:12:42 +01003678 if (spec->hp_mic)
3679 update_hp_mic(codec, adc_idx, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003680
3681 if (spec->dyn_adc_switch)
3682 dyn_adc_pcm_resetup(codec, idx);
3683
Takashi Iwaic697b712013-01-07 17:09:26 +01003684 path = get_input_path(codec, adc_idx, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003685 if (!path)
3686 return 0;
3687 if (path->active)
3688 return 0;
3689 snd_hda_activate_path(codec, path, true, false);
3690 if (spec->cap_sync_hook)
Takashi Iwaia90229e2013-01-18 14:10:00 +01003691 spec->cap_sync_hook(codec, NULL);
Takashi Iwai55196ff2013-01-24 17:32:56 +01003692 path_power_down_sync(codec, old_path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003693 return 1;
3694}
3695
3696
3697/*
3698 * Jack detections for HP auto-mute and mic-switch
3699 */
3700
3701/* check each pin in the given array; returns true if any of them is plugged */
3702static bool detect_jacks(struct hda_codec *codec, int num_pins, hda_nid_t *pins)
3703{
3704 int i, present = 0;
3705
3706 for (i = 0; i < num_pins; i++) {
3707 hda_nid_t nid = pins[i];
3708 if (!nid)
3709 break;
Takashi Iwai0b4df932013-01-10 09:45:13 +01003710 /* don't detect pins retasked as inputs */
3711 if (snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_IN_EN)
3712 continue;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003713 present |= snd_hda_jack_detect(codec, nid);
3714 }
3715 return present;
3716}
3717
3718/* standard HP/line-out auto-mute helper */
3719static void do_automute(struct hda_codec *codec, int num_pins, hda_nid_t *pins,
Takashi Iwai2c12c302013-01-10 09:33:29 +01003720 bool mute)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003721{
3722 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003723 int i;
3724
3725 for (i = 0; i < num_pins; i++) {
3726 hda_nid_t nid = pins[i];
Takashi Iwai967303d2013-02-19 17:12:42 +01003727 unsigned int val, oldval;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003728 if (!nid)
3729 break;
Takashi Iwai967303d2013-02-19 17:12:42 +01003730 oldval = snd_hda_codec_get_pin_target(codec, nid);
3731 if (oldval & PIN_IN)
3732 continue; /* no mute for inputs */
Takashi Iwai352f7f92012-12-19 12:52:06 +01003733 /* don't reset VREF value in case it's controlling
3734 * the amp (see alc861_fixup_asus_amp_vref_0f())
3735 */
Takashi Iwai2c12c302013-01-10 09:33:29 +01003736 if (spec->keep_vref_in_automute)
Takashi Iwai967303d2013-02-19 17:12:42 +01003737 val = oldval & ~PIN_HP;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003738 else
Takashi Iwai352f7f92012-12-19 12:52:06 +01003739 val = 0;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003740 if (!mute)
Takashi Iwai967303d2013-02-19 17:12:42 +01003741 val |= oldval;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003742 /* here we call update_pin_ctl() so that the pinctl is changed
3743 * without changing the pinctl target value;
3744 * the original target value will be still referred at the
3745 * init / resume again
3746 */
3747 update_pin_ctl(codec, nid, val);
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01003748 set_pin_eapd(codec, nid, !mute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003749 }
3750}
3751
3752/* Toggle outputs muting */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003753void snd_hda_gen_update_outputs(struct hda_codec *codec)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003754{
3755 struct hda_gen_spec *spec = codec->spec;
3756 int on;
3757
3758 /* Control HP pins/amps depending on master_mute state;
3759 * in general, HP pins/amps control should be enabled in all cases,
3760 * but currently set only for master_mute, just to be safe
3761 */
Takashi Iwai967303d2013-02-19 17:12:42 +01003762 do_automute(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
Takashi Iwai2c12c302013-01-10 09:33:29 +01003763 spec->autocfg.hp_pins, spec->master_mute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003764
3765 if (!spec->automute_speaker)
3766 on = 0;
3767 else
3768 on = spec->hp_jack_present | spec->line_jack_present;
3769 on |= spec->master_mute;
Takashi Iwai47b9ddb2013-01-16 18:18:00 +01003770 spec->speaker_muted = on;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003771 do_automute(codec, ARRAY_SIZE(spec->autocfg.speaker_pins),
Takashi Iwai2c12c302013-01-10 09:33:29 +01003772 spec->autocfg.speaker_pins, on);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003773
3774 /* toggle line-out mutes if needed, too */
3775 /* if LO is a copy of either HP or Speaker, don't need to handle it */
3776 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0] ||
3777 spec->autocfg.line_out_pins[0] == spec->autocfg.speaker_pins[0])
3778 return;
3779 if (!spec->automute_lo)
3780 on = 0;
3781 else
3782 on = spec->hp_jack_present;
3783 on |= spec->master_mute;
Takashi Iwai47b9ddb2013-01-16 18:18:00 +01003784 spec->line_out_muted = on;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003785 do_automute(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
Takashi Iwai2c12c302013-01-10 09:33:29 +01003786 spec->autocfg.line_out_pins, on);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003787}
Takashi Iwai5d550e12012-12-19 15:16:44 +01003788EXPORT_SYMBOL_HDA(snd_hda_gen_update_outputs);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003789
3790static void call_update_outputs(struct hda_codec *codec)
3791{
3792 struct hda_gen_spec *spec = codec->spec;
3793 if (spec->automute_hook)
3794 spec->automute_hook(codec);
3795 else
Takashi Iwai5d550e12012-12-19 15:16:44 +01003796 snd_hda_gen_update_outputs(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003797}
3798
3799/* standard HP-automute helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003800void snd_hda_gen_hp_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003801{
3802 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai92603c52013-01-22 07:46:31 +01003803 hda_nid_t *pins = spec->autocfg.hp_pins;
3804 int num_pins = ARRAY_SIZE(spec->autocfg.hp_pins);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003805
Takashi Iwai92603c52013-01-22 07:46:31 +01003806 /* No detection for the first HP jack during indep-HP mode */
3807 if (spec->indep_hp_enabled) {
3808 pins++;
3809 num_pins--;
3810 }
3811
3812 spec->hp_jack_present = detect_jacks(codec, num_pins, pins);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003813 if (!spec->detect_hp || (!spec->automute_speaker && !spec->automute_lo))
3814 return;
3815 call_update_outputs(codec);
3816}
Takashi Iwai5d550e12012-12-19 15:16:44 +01003817EXPORT_SYMBOL_HDA(snd_hda_gen_hp_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003818
3819/* standard line-out-automute helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003820void snd_hda_gen_line_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003821{
3822 struct hda_gen_spec *spec = codec->spec;
3823
3824 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
3825 return;
3826 /* check LO jack only when it's different from HP */
3827 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0])
3828 return;
3829
3830 spec->line_jack_present =
3831 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
3832 spec->autocfg.line_out_pins);
3833 if (!spec->automute_speaker || !spec->detect_lo)
3834 return;
3835 call_update_outputs(codec);
3836}
Takashi Iwai5d550e12012-12-19 15:16:44 +01003837EXPORT_SYMBOL_HDA(snd_hda_gen_line_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003838
3839/* standard mic auto-switch helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003840void snd_hda_gen_mic_autoswitch(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003841{
3842 struct hda_gen_spec *spec = codec->spec;
3843 int i;
3844
3845 if (!spec->auto_mic)
3846 return;
3847
3848 for (i = spec->am_num_entries - 1; i > 0; i--) {
Takashi Iwai0b4df932013-01-10 09:45:13 +01003849 hda_nid_t pin = spec->am_entry[i].pin;
3850 /* don't detect pins retasked as outputs */
3851 if (snd_hda_codec_get_pin_target(codec, pin) & AC_PINCTL_OUT_EN)
3852 continue;
3853 if (snd_hda_jack_detect(codec, pin)) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003854 mux_select(codec, 0, spec->am_entry[i].idx);
3855 return;
3856 }
3857 }
3858 mux_select(codec, 0, spec->am_entry[0].idx);
3859}
Takashi Iwai5d550e12012-12-19 15:16:44 +01003860EXPORT_SYMBOL_HDA(snd_hda_gen_mic_autoswitch);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003861
Takashi Iwaia5cc2502013-01-16 18:08:55 +01003862/* update jack retasking */
3863static void update_automute_all(struct hda_codec *codec)
3864{
3865 struct hda_gen_spec *spec = codec->spec;
3866
Takashi Iwai8ba955c2013-03-07 18:40:58 +01003867 update_hp_automute_hook(codec);
Takashi Iwaia5cc2502013-01-16 18:08:55 +01003868 if (spec->line_automute_hook)
3869 spec->line_automute_hook(codec, NULL);
3870 else
3871 snd_hda_gen_line_automute(codec, NULL);
3872 if (spec->mic_autoswitch_hook)
3873 spec->mic_autoswitch_hook(codec, NULL);
3874 else
3875 snd_hda_gen_mic_autoswitch(codec, NULL);
3876}
3877
Takashi Iwai352f7f92012-12-19 12:52:06 +01003878/*
3879 * Auto-Mute mode mixer enum support
3880 */
3881static int automute_mode_info(struct snd_kcontrol *kcontrol,
3882 struct snd_ctl_elem_info *uinfo)
3883{
3884 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3885 struct hda_gen_spec *spec = codec->spec;
3886 static const char * const texts3[] = {
3887 "Disabled", "Speaker Only", "Line Out+Speaker"
Takashi Iwai071c73a2006-08-23 18:34:06 +02003888 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003889
Takashi Iwai352f7f92012-12-19 12:52:06 +01003890 if (spec->automute_speaker_possible && spec->automute_lo_possible)
3891 return snd_hda_enum_helper_info(kcontrol, uinfo, 3, texts3);
3892 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
3893}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003894
Takashi Iwai352f7f92012-12-19 12:52:06 +01003895static int automute_mode_get(struct snd_kcontrol *kcontrol,
3896 struct snd_ctl_elem_value *ucontrol)
3897{
3898 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3899 struct hda_gen_spec *spec = codec->spec;
3900 unsigned int val = 0;
3901 if (spec->automute_speaker)
3902 val++;
3903 if (spec->automute_lo)
3904 val++;
Takashi Iwai071c73a2006-08-23 18:34:06 +02003905
Takashi Iwai352f7f92012-12-19 12:52:06 +01003906 ucontrol->value.enumerated.item[0] = val;
3907 return 0;
3908}
3909
3910static int automute_mode_put(struct snd_kcontrol *kcontrol,
3911 struct snd_ctl_elem_value *ucontrol)
3912{
3913 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3914 struct hda_gen_spec *spec = codec->spec;
3915
3916 switch (ucontrol->value.enumerated.item[0]) {
3917 case 0:
3918 if (!spec->automute_speaker && !spec->automute_lo)
3919 return 0;
3920 spec->automute_speaker = 0;
3921 spec->automute_lo = 0;
3922 break;
3923 case 1:
3924 if (spec->automute_speaker_possible) {
3925 if (!spec->automute_lo && spec->automute_speaker)
3926 return 0;
3927 spec->automute_speaker = 1;
3928 spec->automute_lo = 0;
3929 } else if (spec->automute_lo_possible) {
3930 if (spec->automute_lo)
3931 return 0;
3932 spec->automute_lo = 1;
3933 } else
3934 return -EINVAL;
3935 break;
3936 case 2:
3937 if (!spec->automute_lo_possible || !spec->automute_speaker_possible)
3938 return -EINVAL;
3939 if (spec->automute_speaker && spec->automute_lo)
3940 return 0;
3941 spec->automute_speaker = 1;
3942 spec->automute_lo = 1;
3943 break;
3944 default:
3945 return -EINVAL;
3946 }
3947 call_update_outputs(codec);
3948 return 1;
3949}
3950
3951static const struct snd_kcontrol_new automute_mode_enum = {
3952 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3953 .name = "Auto-Mute Mode",
3954 .info = automute_mode_info,
3955 .get = automute_mode_get,
3956 .put = automute_mode_put,
3957};
3958
3959static int add_automute_mode_enum(struct hda_codec *codec)
3960{
3961 struct hda_gen_spec *spec = codec->spec;
3962
Takashi Iwai12c93df2012-12-19 14:38:33 +01003963 if (!snd_hda_gen_add_kctl(spec, NULL, &automute_mode_enum))
Takashi Iwai352f7f92012-12-19 12:52:06 +01003964 return -ENOMEM;
3965 return 0;
3966}
3967
3968/*
3969 * Check the availability of HP/line-out auto-mute;
3970 * Set up appropriately if really supported
3971 */
3972static int check_auto_mute_availability(struct hda_codec *codec)
3973{
3974 struct hda_gen_spec *spec = codec->spec;
3975 struct auto_pin_cfg *cfg = &spec->autocfg;
3976 int present = 0;
3977 int i, err;
3978
Takashi Iwaif72706b2013-01-16 18:20:07 +01003979 if (spec->suppress_auto_mute)
3980 return 0;
3981
Takashi Iwai352f7f92012-12-19 12:52:06 +01003982 if (cfg->hp_pins[0])
3983 present++;
3984 if (cfg->line_out_pins[0])
3985 present++;
3986 if (cfg->speaker_pins[0])
3987 present++;
3988 if (present < 2) /* need two different output types */
Takashi Iwai071c73a2006-08-23 18:34:06 +02003989 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003990
3991 if (!cfg->speaker_pins[0] &&
3992 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
3993 memcpy(cfg->speaker_pins, cfg->line_out_pins,
3994 sizeof(cfg->speaker_pins));
3995 cfg->speaker_outs = cfg->line_outs;
Takashi Iwai071c73a2006-08-23 18:34:06 +02003996 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003997
Takashi Iwai352f7f92012-12-19 12:52:06 +01003998 if (!cfg->hp_pins[0] &&
3999 cfg->line_out_type == AUTO_PIN_HP_OUT) {
4000 memcpy(cfg->hp_pins, cfg->line_out_pins,
4001 sizeof(cfg->hp_pins));
4002 cfg->hp_outs = cfg->line_outs;
4003 }
4004
4005 for (i = 0; i < cfg->hp_outs; i++) {
4006 hda_nid_t nid = cfg->hp_pins[i];
4007 if (!is_jack_detectable(codec, nid))
4008 continue;
4009 snd_printdd("hda-codec: Enable HP auto-muting on NID 0x%x\n",
4010 nid);
4011 snd_hda_jack_detect_enable_callback(codec, nid, HDA_GEN_HP_EVENT,
Takashi Iwai2e03e952013-01-03 15:55:06 +01004012 spec->hp_automute_hook ?
4013 spec->hp_automute_hook :
Takashi Iwai5d550e12012-12-19 15:16:44 +01004014 snd_hda_gen_hp_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004015 spec->detect_hp = 1;
4016 }
4017
4018 if (cfg->line_out_type == AUTO_PIN_LINE_OUT && cfg->line_outs) {
4019 if (cfg->speaker_outs)
4020 for (i = 0; i < cfg->line_outs; i++) {
4021 hda_nid_t nid = cfg->line_out_pins[i];
4022 if (!is_jack_detectable(codec, nid))
4023 continue;
4024 snd_printdd("hda-codec: Enable Line-Out auto-muting on NID 0x%x\n", nid);
4025 snd_hda_jack_detect_enable_callback(codec, nid,
4026 HDA_GEN_FRONT_EVENT,
Takashi Iwai2e03e952013-01-03 15:55:06 +01004027 spec->line_automute_hook ?
4028 spec->line_automute_hook :
Takashi Iwai5d550e12012-12-19 15:16:44 +01004029 snd_hda_gen_line_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004030 spec->detect_lo = 1;
4031 }
4032 spec->automute_lo_possible = spec->detect_hp;
4033 }
4034
4035 spec->automute_speaker_possible = cfg->speaker_outs &&
4036 (spec->detect_hp || spec->detect_lo);
4037
4038 spec->automute_lo = spec->automute_lo_possible;
4039 spec->automute_speaker = spec->automute_speaker_possible;
4040
4041 if (spec->automute_speaker_possible || spec->automute_lo_possible) {
4042 /* create a control for automute mode */
4043 err = add_automute_mode_enum(codec);
4044 if (err < 0)
4045 return err;
4046 }
4047 return 0;
4048}
4049
Takashi Iwai352f7f92012-12-19 12:52:06 +01004050/* check whether all auto-mic pins are valid; setup indices if OK */
4051static bool auto_mic_check_imux(struct hda_codec *codec)
4052{
4053 struct hda_gen_spec *spec = codec->spec;
4054 const struct hda_input_mux *imux;
4055 int i;
4056
4057 imux = &spec->input_mux;
4058 for (i = 0; i < spec->am_num_entries; i++) {
4059 spec->am_entry[i].idx =
4060 find_idx_in_nid_list(spec->am_entry[i].pin,
4061 spec->imux_pins, imux->num_items);
4062 if (spec->am_entry[i].idx < 0)
4063 return false; /* no corresponding imux */
4064 }
4065
4066 /* we don't need the jack detection for the first pin */
4067 for (i = 1; i < spec->am_num_entries; i++)
4068 snd_hda_jack_detect_enable_callback(codec,
4069 spec->am_entry[i].pin,
4070 HDA_GEN_MIC_EVENT,
Takashi Iwai2e03e952013-01-03 15:55:06 +01004071 spec->mic_autoswitch_hook ?
4072 spec->mic_autoswitch_hook :
Takashi Iwai5d550e12012-12-19 15:16:44 +01004073 snd_hda_gen_mic_autoswitch);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004074 return true;
4075}
4076
4077static int compare_attr(const void *ap, const void *bp)
4078{
4079 const struct automic_entry *a = ap;
4080 const struct automic_entry *b = bp;
4081 return (int)(a->attr - b->attr);
4082}
4083
4084/*
4085 * Check the availability of auto-mic switch;
4086 * Set up if really supported
4087 */
4088static int check_auto_mic_availability(struct hda_codec *codec)
4089{
4090 struct hda_gen_spec *spec = codec->spec;
4091 struct auto_pin_cfg *cfg = &spec->autocfg;
4092 unsigned int types;
4093 int i, num_pins;
4094
Takashi Iwaid12daf62013-01-07 16:32:11 +01004095 if (spec->suppress_auto_mic)
4096 return 0;
4097
Takashi Iwai352f7f92012-12-19 12:52:06 +01004098 types = 0;
4099 num_pins = 0;
4100 for (i = 0; i < cfg->num_inputs; i++) {
4101 hda_nid_t nid = cfg->inputs[i].pin;
4102 unsigned int attr;
4103 attr = snd_hda_codec_get_pincfg(codec, nid);
4104 attr = snd_hda_get_input_pin_attr(attr);
4105 if (types & (1 << attr))
4106 return 0; /* already occupied */
4107 switch (attr) {
4108 case INPUT_PIN_ATTR_INT:
4109 if (cfg->inputs[i].type != AUTO_PIN_MIC)
4110 return 0; /* invalid type */
4111 break;
4112 case INPUT_PIN_ATTR_UNUSED:
4113 return 0; /* invalid entry */
4114 default:
4115 if (cfg->inputs[i].type > AUTO_PIN_LINE_IN)
4116 return 0; /* invalid type */
4117 if (!spec->line_in_auto_switch &&
4118 cfg->inputs[i].type != AUTO_PIN_MIC)
4119 return 0; /* only mic is allowed */
4120 if (!is_jack_detectable(codec, nid))
4121 return 0; /* no unsol support */
4122 break;
4123 }
4124 if (num_pins >= MAX_AUTO_MIC_PINS)
4125 return 0;
4126 types |= (1 << attr);
4127 spec->am_entry[num_pins].pin = nid;
4128 spec->am_entry[num_pins].attr = attr;
4129 num_pins++;
4130 }
4131
4132 if (num_pins < 2)
4133 return 0;
4134
4135 spec->am_num_entries = num_pins;
4136 /* sort the am_entry in the order of attr so that the pin with a
4137 * higher attr will be selected when the jack is plugged.
4138 */
4139 sort(spec->am_entry, num_pins, sizeof(spec->am_entry[0]),
4140 compare_attr, NULL);
4141
4142 if (!auto_mic_check_imux(codec))
4143 return 0;
4144
4145 spec->auto_mic = 1;
4146 spec->num_adc_nids = 1;
4147 spec->cur_mux[0] = spec->am_entry[0].idx;
4148 snd_printdd("hda-codec: Enable auto-mic switch on NID 0x%x/0x%x/0x%x\n",
4149 spec->am_entry[0].pin,
4150 spec->am_entry[1].pin,
4151 spec->am_entry[2].pin);
4152
4153 return 0;
4154}
4155
Takashi Iwai55196ff2013-01-24 17:32:56 +01004156/* power_filter hook; make inactive widgets into power down */
4157static unsigned int snd_hda_gen_path_power_filter(struct hda_codec *codec,
4158 hda_nid_t nid,
4159 unsigned int power_state)
4160{
4161 if (power_state != AC_PWRST_D0)
4162 return power_state;
4163 if (get_wcaps_type(get_wcaps(codec, nid)) >= AC_WID_POWER)
4164 return power_state;
Takashi Iwaib1b9fbd2013-05-14 12:58:47 +02004165 if (is_active_nid_for_any(codec, nid))
Takashi Iwai55196ff2013-01-24 17:32:56 +01004166 return power_state;
4167 return AC_PWRST_D3;
4168}
4169
Takashi Iwai352f7f92012-12-19 12:52:06 +01004170
Takashi Iwai9eb413e2012-12-19 14:41:21 +01004171/*
4172 * Parse the given BIOS configuration and set up the hda_gen_spec
4173 *
4174 * return 1 if successful, 0 if the proper config is not found,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004175 * or a negative error code
4176 */
4177int snd_hda_gen_parse_auto_config(struct hda_codec *codec,
Takashi Iwai9eb413e2012-12-19 14:41:21 +01004178 struct auto_pin_cfg *cfg)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004179{
4180 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004181 int err;
4182
Takashi Iwai1c70a582013-01-11 17:48:22 +01004183 parse_user_hints(codec);
4184
Takashi Iwaie4a395e2013-01-23 17:00:31 +01004185 if (spec->mixer_nid && !spec->mixer_merge_nid)
4186 spec->mixer_merge_nid = spec->mixer_nid;
4187
Takashi Iwai9eb413e2012-12-19 14:41:21 +01004188 if (cfg != &spec->autocfg) {
4189 spec->autocfg = *cfg;
4190 cfg = &spec->autocfg;
4191 }
4192
Takashi Iwai98bd1112013-03-22 14:53:50 +01004193 if (!spec->main_out_badness)
4194 spec->main_out_badness = &hda_main_out_badness;
4195 if (!spec->extra_out_badness)
4196 spec->extra_out_badness = &hda_extra_out_badness;
4197
David Henningsson6fc4cb92013-01-16 15:58:45 +01004198 fill_all_dac_nids(codec);
4199
Takashi Iwai352f7f92012-12-19 12:52:06 +01004200 if (!cfg->line_outs) {
4201 if (cfg->dig_outs || cfg->dig_in_pin) {
4202 spec->multiout.max_channels = 2;
4203 spec->no_analog = 1;
4204 goto dig_only;
4205 }
4206 return 0; /* can't find valid BIOS pin config */
4207 }
4208
4209 if (!spec->no_primary_hp &&
4210 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT &&
4211 cfg->line_outs <= cfg->hp_outs) {
4212 /* use HP as primary out */
4213 cfg->speaker_outs = cfg->line_outs;
4214 memcpy(cfg->speaker_pins, cfg->line_out_pins,
4215 sizeof(cfg->speaker_pins));
4216 cfg->line_outs = cfg->hp_outs;
4217 memcpy(cfg->line_out_pins, cfg->hp_pins, sizeof(cfg->hp_pins));
4218 cfg->hp_outs = 0;
4219 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
4220 cfg->line_out_type = AUTO_PIN_HP_OUT;
4221 }
4222
4223 err = parse_output_paths(codec);
4224 if (err < 0)
4225 return err;
4226 err = create_multi_channel_mode(codec);
4227 if (err < 0)
4228 return err;
4229 err = create_multi_out_ctls(codec, cfg);
4230 if (err < 0)
4231 return err;
4232 err = create_hp_out_ctls(codec);
4233 if (err < 0)
4234 return err;
4235 err = create_speaker_out_ctls(codec);
4236 if (err < 0)
4237 return err;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004238 err = create_indep_hp_ctls(codec);
4239 if (err < 0)
4240 return err;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01004241 err = create_loopback_mixing_ctl(codec);
4242 if (err < 0)
4243 return err;
Takashi Iwai967303d2013-02-19 17:12:42 +01004244 err = create_hp_mic(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004245 if (err < 0)
4246 return err;
4247 err = create_input_ctls(codec);
Takashi Iwaid13bd412008-07-30 15:01:45 +02004248 if (err < 0)
Takashi Iwai071c73a2006-08-23 18:34:06 +02004249 return err;
4250
Takashi Iwaia07a9492013-01-07 16:44:06 +01004251 spec->const_channel_count = spec->ext_channel_count;
4252 /* check the multiple speaker and headphone pins */
4253 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
4254 spec->const_channel_count = max(spec->const_channel_count,
4255 cfg->speaker_outs * 2);
4256 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
4257 spec->const_channel_count = max(spec->const_channel_count,
4258 cfg->hp_outs * 2);
4259 spec->multiout.max_channels = max(spec->ext_channel_count,
4260 spec->const_channel_count);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004261
4262 err = check_auto_mute_availability(codec);
4263 if (err < 0)
4264 return err;
4265
4266 err = check_dyn_adc_switch(codec);
4267 if (err < 0)
4268 return err;
4269
Takashi Iwai967303d2013-02-19 17:12:42 +01004270 err = check_auto_mic_availability(codec);
4271 if (err < 0)
4272 return err;
Takashi Iwai071c73a2006-08-23 18:34:06 +02004273
Takashi Iwai352f7f92012-12-19 12:52:06 +01004274 err = create_capture_mixers(codec);
4275 if (err < 0)
4276 return err;
4277
4278 err = parse_mic_boost(codec);
4279 if (err < 0)
4280 return err;
4281
Takashi Iwaif811c3c2013-03-07 18:32:59 +01004282 if (spec->add_jack_modes) {
Takashi Iwai978e77e2013-01-10 16:57:58 +01004283 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
4284 err = create_out_jack_modes(codec, cfg->line_outs,
4285 cfg->line_out_pins);
4286 if (err < 0)
4287 return err;
4288 }
4289 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
4290 err = create_out_jack_modes(codec, cfg->hp_outs,
4291 cfg->hp_pins);
4292 if (err < 0)
4293 return err;
4294 }
4295 }
4296
Takashi Iwai352f7f92012-12-19 12:52:06 +01004297 dig_only:
4298 parse_digital(codec);
4299
Takashi Iwai55196ff2013-01-24 17:32:56 +01004300 if (spec->power_down_unused)
4301 codec->power_filter = snd_hda_gen_path_power_filter;
4302
Takashi Iwai7504b6c2013-03-18 11:25:51 +01004303 if (!spec->no_analog && spec->beep_nid) {
4304 err = snd_hda_attach_beep_device(codec, spec->beep_nid);
4305 if (err < 0)
4306 return err;
4307 }
4308
Takashi Iwai352f7f92012-12-19 12:52:06 +01004309 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004310}
Takashi Iwai352f7f92012-12-19 12:52:06 +01004311EXPORT_SYMBOL_HDA(snd_hda_gen_parse_auto_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004312
4313
4314/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01004315 * Build control elements
Linus Torvalds1da177e2005-04-16 15:20:36 -07004316 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01004317
4318/* slave controls for virtual master */
4319static const char * const slave_pfxs[] = {
4320 "Front", "Surround", "Center", "LFE", "Side",
4321 "Headphone", "Speaker", "Mono", "Line Out",
4322 "CLFE", "Bass Speaker", "PCM",
Takashi Iwaiee79c692013-01-07 09:57:42 +01004323 "Speaker Front", "Speaker Surround", "Speaker CLFE", "Speaker Side",
4324 "Headphone Front", "Headphone Surround", "Headphone CLFE",
4325 "Headphone Side",
Takashi Iwai352f7f92012-12-19 12:52:06 +01004326 NULL,
4327};
4328
4329int snd_hda_gen_build_controls(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004330{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004331 struct hda_gen_spec *spec = codec->spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004332 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004333
Takashi Iwai36502d02012-12-19 15:15:10 +01004334 if (spec->kctls.used) {
4335 err = snd_hda_add_new_ctls(codec, spec->kctls.list);
4336 if (err < 0)
4337 return err;
4338 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004339
Takashi Iwai352f7f92012-12-19 12:52:06 +01004340 if (spec->multiout.dig_out_nid) {
4341 err = snd_hda_create_dig_out_ctls(codec,
4342 spec->multiout.dig_out_nid,
4343 spec->multiout.dig_out_nid,
4344 spec->pcm_rec[1].pcm_type);
4345 if (err < 0)
4346 return err;
4347 if (!spec->no_analog) {
4348 err = snd_hda_create_spdif_share_sw(codec,
4349 &spec->multiout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004350 if (err < 0)
4351 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004352 spec->multiout.share_spdif = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004353 }
4354 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01004355 if (spec->dig_in_nid) {
4356 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
4357 if (err < 0)
4358 return err;
4359 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004360
Takashi Iwai352f7f92012-12-19 12:52:06 +01004361 /* if we have no master control, let's create it */
4362 if (!spec->no_analog &&
4363 !snd_hda_find_mixer_ctl(codec, "Master Playback Volume")) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01004364 err = snd_hda_add_vmaster(codec, "Master Playback Volume",
Takashi Iwai7a71bbf2013-01-17 10:25:15 +01004365 spec->vmaster_tlv, slave_pfxs,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004366 "Playback Volume");
4367 if (err < 0)
4368 return err;
4369 }
4370 if (!spec->no_analog &&
4371 !snd_hda_find_mixer_ctl(codec, "Master Playback Switch")) {
4372 err = __snd_hda_add_vmaster(codec, "Master Playback Switch",
4373 NULL, slave_pfxs,
4374 "Playback Switch",
4375 true, &spec->vmaster_mute.sw_kctl);
4376 if (err < 0)
4377 return err;
4378 if (spec->vmaster_mute.hook)
Takashi Iwaifd25a972012-12-20 14:57:18 +01004379 snd_hda_add_vmaster_hook(codec, &spec->vmaster_mute,
4380 spec->vmaster_mute_enum);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004381 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004382
Takashi Iwai352f7f92012-12-19 12:52:06 +01004383 free_kctls(spec); /* no longer needed */
4384
Takashi Iwai352f7f92012-12-19 12:52:06 +01004385 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
4386 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004387 return err;
4388
4389 return 0;
4390}
Takashi Iwai352f7f92012-12-19 12:52:06 +01004391EXPORT_SYMBOL_HDA(snd_hda_gen_build_controls);
4392
Linus Torvalds1da177e2005-04-16 15:20:36 -07004393
4394/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01004395 * PCM definitions
Linus Torvalds1da177e2005-04-16 15:20:36 -07004396 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004397
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004398static void call_pcm_playback_hook(struct hda_pcm_stream *hinfo,
4399 struct hda_codec *codec,
4400 struct snd_pcm_substream *substream,
4401 int action)
4402{
4403 struct hda_gen_spec *spec = codec->spec;
4404 if (spec->pcm_playback_hook)
4405 spec->pcm_playback_hook(hinfo, codec, substream, action);
4406}
4407
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004408static void call_pcm_capture_hook(struct hda_pcm_stream *hinfo,
4409 struct hda_codec *codec,
4410 struct snd_pcm_substream *substream,
4411 int action)
4412{
4413 struct hda_gen_spec *spec = codec->spec;
4414 if (spec->pcm_capture_hook)
4415 spec->pcm_capture_hook(hinfo, codec, substream, action);
4416}
4417
Takashi Iwai352f7f92012-12-19 12:52:06 +01004418/*
4419 * Analog playback callbacks
4420 */
4421static int playback_pcm_open(struct hda_pcm_stream *hinfo,
4422 struct hda_codec *codec,
4423 struct snd_pcm_substream *substream)
4424{
4425 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004426 int err;
4427
4428 mutex_lock(&spec->pcm_mutex);
4429 err = snd_hda_multi_out_analog_open(codec,
4430 &spec->multiout, substream,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004431 hinfo);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004432 if (!err) {
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004433 spec->active_streams |= 1 << STREAM_MULTI_OUT;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004434 call_pcm_playback_hook(hinfo, codec, substream,
4435 HDA_GEN_PCM_ACT_OPEN);
4436 }
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004437 mutex_unlock(&spec->pcm_mutex);
4438 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004439}
4440
4441static int playback_pcm_prepare(struct hda_pcm_stream *hinfo,
Takashi Iwai97ec5582006-03-21 11:29:07 +01004442 struct hda_codec *codec,
4443 unsigned int stream_tag,
4444 unsigned int format,
4445 struct snd_pcm_substream *substream)
4446{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004447 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004448 int err;
4449
4450 err = snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
4451 stream_tag, format, substream);
4452 if (!err)
4453 call_pcm_playback_hook(hinfo, codec, substream,
4454 HDA_GEN_PCM_ACT_PREPARE);
4455 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004456}
Takashi Iwai97ec5582006-03-21 11:29:07 +01004457
Takashi Iwai352f7f92012-12-19 12:52:06 +01004458static int playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
4459 struct hda_codec *codec,
4460 struct snd_pcm_substream *substream)
4461{
4462 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004463 int err;
4464
4465 err = snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
4466 if (!err)
4467 call_pcm_playback_hook(hinfo, codec, substream,
4468 HDA_GEN_PCM_ACT_CLEANUP);
4469 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004470}
4471
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004472static int playback_pcm_close(struct hda_pcm_stream *hinfo,
4473 struct hda_codec *codec,
4474 struct snd_pcm_substream *substream)
4475{
4476 struct hda_gen_spec *spec = codec->spec;
4477 mutex_lock(&spec->pcm_mutex);
4478 spec->active_streams &= ~(1 << STREAM_MULTI_OUT);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004479 call_pcm_playback_hook(hinfo, codec, substream,
4480 HDA_GEN_PCM_ACT_CLOSE);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004481 mutex_unlock(&spec->pcm_mutex);
4482 return 0;
4483}
4484
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004485static int capture_pcm_open(struct hda_pcm_stream *hinfo,
4486 struct hda_codec *codec,
4487 struct snd_pcm_substream *substream)
4488{
4489 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_OPEN);
4490 return 0;
4491}
4492
4493static int capture_pcm_prepare(struct hda_pcm_stream *hinfo,
4494 struct hda_codec *codec,
4495 unsigned int stream_tag,
4496 unsigned int format,
4497 struct snd_pcm_substream *substream)
4498{
4499 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4500 call_pcm_capture_hook(hinfo, codec, substream,
4501 HDA_GEN_PCM_ACT_PREPARE);
4502 return 0;
4503}
4504
4505static int capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
4506 struct hda_codec *codec,
4507 struct snd_pcm_substream *substream)
4508{
4509 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
4510 call_pcm_capture_hook(hinfo, codec, substream,
4511 HDA_GEN_PCM_ACT_CLEANUP);
4512 return 0;
4513}
4514
4515static int capture_pcm_close(struct hda_pcm_stream *hinfo,
4516 struct hda_codec *codec,
4517 struct snd_pcm_substream *substream)
4518{
4519 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_CLOSE);
4520 return 0;
4521}
4522
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004523static int alt_playback_pcm_open(struct hda_pcm_stream *hinfo,
4524 struct hda_codec *codec,
4525 struct snd_pcm_substream *substream)
4526{
4527 struct hda_gen_spec *spec = codec->spec;
4528 int err = 0;
4529
4530 mutex_lock(&spec->pcm_mutex);
4531 if (!spec->indep_hp_enabled)
4532 err = -EBUSY;
4533 else
4534 spec->active_streams |= 1 << STREAM_INDEP_HP;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004535 call_pcm_playback_hook(hinfo, codec, substream,
4536 HDA_GEN_PCM_ACT_OPEN);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004537 mutex_unlock(&spec->pcm_mutex);
4538 return err;
4539}
4540
4541static int alt_playback_pcm_close(struct hda_pcm_stream *hinfo,
4542 struct hda_codec *codec,
4543 struct snd_pcm_substream *substream)
4544{
4545 struct hda_gen_spec *spec = codec->spec;
4546 mutex_lock(&spec->pcm_mutex);
4547 spec->active_streams &= ~(1 << STREAM_INDEP_HP);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004548 call_pcm_playback_hook(hinfo, codec, substream,
4549 HDA_GEN_PCM_ACT_CLOSE);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004550 mutex_unlock(&spec->pcm_mutex);
4551 return 0;
4552}
4553
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004554static int alt_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
4555 struct hda_codec *codec,
4556 unsigned int stream_tag,
4557 unsigned int format,
4558 struct snd_pcm_substream *substream)
4559{
4560 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4561 call_pcm_playback_hook(hinfo, codec, substream,
4562 HDA_GEN_PCM_ACT_PREPARE);
4563 return 0;
4564}
4565
4566static int alt_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
4567 struct hda_codec *codec,
4568 struct snd_pcm_substream *substream)
4569{
4570 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
4571 call_pcm_playback_hook(hinfo, codec, substream,
4572 HDA_GEN_PCM_ACT_CLEANUP);
4573 return 0;
4574}
4575
Takashi Iwai352f7f92012-12-19 12:52:06 +01004576/*
4577 * Digital out
4578 */
4579static int dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
4580 struct hda_codec *codec,
4581 struct snd_pcm_substream *substream)
4582{
4583 struct hda_gen_spec *spec = codec->spec;
4584 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
4585}
4586
4587static int dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
4588 struct hda_codec *codec,
4589 unsigned int stream_tag,
4590 unsigned int format,
4591 struct snd_pcm_substream *substream)
4592{
4593 struct hda_gen_spec *spec = codec->spec;
4594 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
4595 stream_tag, format, substream);
4596}
4597
4598static int dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
4599 struct hda_codec *codec,
4600 struct snd_pcm_substream *substream)
4601{
4602 struct hda_gen_spec *spec = codec->spec;
4603 return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
4604}
4605
4606static int dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
4607 struct hda_codec *codec,
4608 struct snd_pcm_substream *substream)
4609{
4610 struct hda_gen_spec *spec = codec->spec;
4611 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
4612}
4613
4614/*
4615 * Analog capture
4616 */
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004617#define alt_capture_pcm_open capture_pcm_open
4618#define alt_capture_pcm_close capture_pcm_close
4619
Takashi Iwai352f7f92012-12-19 12:52:06 +01004620static int alt_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
4621 struct hda_codec *codec,
4622 unsigned int stream_tag,
4623 unsigned int format,
4624 struct snd_pcm_substream *substream)
4625{
4626 struct hda_gen_spec *spec = codec->spec;
4627
4628 snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number + 1],
Takashi Iwai97ec5582006-03-21 11:29:07 +01004629 stream_tag, 0, format);
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004630 call_pcm_capture_hook(hinfo, codec, substream,
4631 HDA_GEN_PCM_ACT_PREPARE);
Takashi Iwai97ec5582006-03-21 11:29:07 +01004632 return 0;
4633}
4634
Takashi Iwai352f7f92012-12-19 12:52:06 +01004635static int alt_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
4636 struct hda_codec *codec,
4637 struct snd_pcm_substream *substream)
Takashi Iwai97ec5582006-03-21 11:29:07 +01004638{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004639 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai97ec5582006-03-21 11:29:07 +01004640
Takashi Iwai352f7f92012-12-19 12:52:06 +01004641 snd_hda_codec_cleanup_stream(codec,
4642 spec->adc_nids[substream->number + 1]);
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004643 call_pcm_capture_hook(hinfo, codec, substream,
4644 HDA_GEN_PCM_ACT_CLEANUP);
Takashi Iwai97ec5582006-03-21 11:29:07 +01004645 return 0;
4646}
4647
Takashi Iwai352f7f92012-12-19 12:52:06 +01004648/*
4649 */
4650static const struct hda_pcm_stream pcm_analog_playback = {
4651 .substreams = 1,
4652 .channels_min = 2,
4653 .channels_max = 8,
4654 /* NID is set in build_pcms */
4655 .ops = {
4656 .open = playback_pcm_open,
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004657 .close = playback_pcm_close,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004658 .prepare = playback_pcm_prepare,
4659 .cleanup = playback_pcm_cleanup
4660 },
4661};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004662
Takashi Iwai352f7f92012-12-19 12:52:06 +01004663static const struct hda_pcm_stream pcm_analog_capture = {
4664 .substreams = 1,
4665 .channels_min = 2,
4666 .channels_max = 2,
4667 /* NID is set in build_pcms */
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004668 .ops = {
4669 .open = capture_pcm_open,
4670 .close = capture_pcm_close,
4671 .prepare = capture_pcm_prepare,
4672 .cleanup = capture_pcm_cleanup
4673 },
Takashi Iwai352f7f92012-12-19 12:52:06 +01004674};
4675
4676static const struct hda_pcm_stream pcm_analog_alt_playback = {
4677 .substreams = 1,
4678 .channels_min = 2,
4679 .channels_max = 2,
4680 /* NID is set in build_pcms */
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004681 .ops = {
4682 .open = alt_playback_pcm_open,
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004683 .close = alt_playback_pcm_close,
4684 .prepare = alt_playback_pcm_prepare,
4685 .cleanup = alt_playback_pcm_cleanup
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004686 },
Takashi Iwai352f7f92012-12-19 12:52:06 +01004687};
4688
4689static const struct hda_pcm_stream pcm_analog_alt_capture = {
4690 .substreams = 2, /* can be overridden */
4691 .channels_min = 2,
4692 .channels_max = 2,
4693 /* NID is set in build_pcms */
4694 .ops = {
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004695 .open = alt_capture_pcm_open,
4696 .close = alt_capture_pcm_close,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004697 .prepare = alt_capture_pcm_prepare,
4698 .cleanup = alt_capture_pcm_cleanup
4699 },
4700};
4701
4702static const struct hda_pcm_stream pcm_digital_playback = {
4703 .substreams = 1,
4704 .channels_min = 2,
4705 .channels_max = 2,
4706 /* NID is set in build_pcms */
4707 .ops = {
4708 .open = dig_playback_pcm_open,
4709 .close = dig_playback_pcm_close,
4710 .prepare = dig_playback_pcm_prepare,
4711 .cleanup = dig_playback_pcm_cleanup
4712 },
4713};
4714
4715static const struct hda_pcm_stream pcm_digital_capture = {
4716 .substreams = 1,
4717 .channels_min = 2,
4718 .channels_max = 2,
4719 /* NID is set in build_pcms */
4720};
4721
4722/* Used by build_pcms to flag that a PCM has no playback stream */
4723static const struct hda_pcm_stream pcm_null_stream = {
4724 .substreams = 0,
4725 .channels_min = 0,
4726 .channels_max = 0,
4727};
4728
4729/*
4730 * dynamic changing ADC PCM streams
4731 */
4732static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur)
4733{
4734 struct hda_gen_spec *spec = codec->spec;
4735 hda_nid_t new_adc = spec->adc_nids[spec->dyn_adc_idx[cur]];
4736
4737 if (spec->cur_adc && spec->cur_adc != new_adc) {
4738 /* stream is running, let's swap the current ADC */
4739 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
4740 spec->cur_adc = new_adc;
4741 snd_hda_codec_setup_stream(codec, new_adc,
4742 spec->cur_adc_stream_tag, 0,
4743 spec->cur_adc_format);
4744 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004745 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01004746 return false;
4747}
4748
4749/* analog capture with dynamic dual-adc changes */
4750static int dyn_adc_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
4751 struct hda_codec *codec,
4752 unsigned int stream_tag,
4753 unsigned int format,
4754 struct snd_pcm_substream *substream)
4755{
4756 struct hda_gen_spec *spec = codec->spec;
4757 spec->cur_adc = spec->adc_nids[spec->dyn_adc_idx[spec->cur_mux[0]]];
4758 spec->cur_adc_stream_tag = stream_tag;
4759 spec->cur_adc_format = format;
4760 snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
4761 return 0;
4762}
4763
4764static int dyn_adc_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
4765 struct hda_codec *codec,
4766 struct snd_pcm_substream *substream)
4767{
4768 struct hda_gen_spec *spec = codec->spec;
4769 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
4770 spec->cur_adc = 0;
4771 return 0;
4772}
4773
4774static const struct hda_pcm_stream dyn_adc_pcm_analog_capture = {
4775 .substreams = 1,
4776 .channels_min = 2,
4777 .channels_max = 2,
4778 .nid = 0, /* fill later */
4779 .ops = {
4780 .prepare = dyn_adc_capture_pcm_prepare,
4781 .cleanup = dyn_adc_capture_pcm_cleanup
4782 },
4783};
4784
Takashi Iwaif873e532012-12-20 16:58:39 +01004785static void fill_pcm_stream_name(char *str, size_t len, const char *sfx,
4786 const char *chip_name)
4787{
4788 char *p;
4789
4790 if (*str)
4791 return;
4792 strlcpy(str, chip_name, len);
4793
4794 /* drop non-alnum chars after a space */
4795 for (p = strchr(str, ' '); p; p = strchr(p + 1, ' ')) {
4796 if (!isalnum(p[1])) {
4797 *p = 0;
4798 break;
4799 }
4800 }
4801 strlcat(str, sfx, len);
4802}
4803
Takashi Iwai352f7f92012-12-19 12:52:06 +01004804/* build PCM streams based on the parsed results */
4805int snd_hda_gen_build_pcms(struct hda_codec *codec)
4806{
4807 struct hda_gen_spec *spec = codec->spec;
4808 struct hda_pcm *info = spec->pcm_rec;
4809 const struct hda_pcm_stream *p;
4810 bool have_multi_adcs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004811
4812 codec->num_pcms = 1;
4813 codec->pcm_info = info;
4814
Takashi Iwai352f7f92012-12-19 12:52:06 +01004815 if (spec->no_analog)
4816 goto skip_analog;
4817
Takashi Iwaif873e532012-12-20 16:58:39 +01004818 fill_pcm_stream_name(spec->stream_name_analog,
4819 sizeof(spec->stream_name_analog),
4820 " Analog", codec->chip_name);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004821 info->name = spec->stream_name_analog;
4822
4823 if (spec->multiout.num_dacs > 0) {
4824 p = spec->stream_analog_playback;
4825 if (!p)
4826 p = &pcm_analog_playback;
4827 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
4828 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dac_nids[0];
4829 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
4830 spec->multiout.max_channels;
4831 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT &&
4832 spec->autocfg.line_outs == 2)
4833 info->stream[SNDRV_PCM_STREAM_PLAYBACK].chmap =
4834 snd_pcm_2_1_chmaps;
4835 }
4836 if (spec->num_adc_nids) {
4837 p = spec->stream_analog_capture;
4838 if (!p) {
4839 if (spec->dyn_adc_switch)
4840 p = &dyn_adc_pcm_analog_capture;
4841 else
4842 p = &pcm_analog_capture;
4843 }
4844 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
4845 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adc_nids[0];
4846 }
4847
Takashi Iwai352f7f92012-12-19 12:52:06 +01004848 skip_analog:
4849 /* SPDIF for stream index #1 */
4850 if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
Takashi Iwaif873e532012-12-20 16:58:39 +01004851 fill_pcm_stream_name(spec->stream_name_digital,
4852 sizeof(spec->stream_name_digital),
4853 " Digital", codec->chip_name);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004854 codec->num_pcms = 2;
4855 codec->slave_dig_outs = spec->multiout.slave_dig_outs;
4856 info = spec->pcm_rec + 1;
4857 info->name = spec->stream_name_digital;
4858 if (spec->dig_out_type)
4859 info->pcm_type = spec->dig_out_type;
4860 else
4861 info->pcm_type = HDA_PCM_TYPE_SPDIF;
4862 if (spec->multiout.dig_out_nid) {
4863 p = spec->stream_digital_playback;
4864 if (!p)
4865 p = &pcm_digital_playback;
4866 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
4867 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dig_out_nid;
4868 }
4869 if (spec->dig_in_nid) {
4870 p = spec->stream_digital_capture;
4871 if (!p)
4872 p = &pcm_digital_capture;
4873 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
4874 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in_nid;
4875 }
4876 }
4877
4878 if (spec->no_analog)
4879 return 0;
4880
4881 /* If the use of more than one ADC is requested for the current
4882 * model, configure a second analog capture-only PCM.
4883 */
4884 have_multi_adcs = (spec->num_adc_nids > 1) &&
4885 !spec->dyn_adc_switch && !spec->auto_mic;
4886 /* Additional Analaog capture for index #2 */
4887 if (spec->alt_dac_nid || have_multi_adcs) {
Takashi Iwaia6071482013-01-21 16:50:09 +01004888 fill_pcm_stream_name(spec->stream_name_alt_analog,
4889 sizeof(spec->stream_name_alt_analog),
4890 " Alt Analog", codec->chip_name);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004891 codec->num_pcms = 3;
4892 info = spec->pcm_rec + 2;
Takashi Iwaia6071482013-01-21 16:50:09 +01004893 info->name = spec->stream_name_alt_analog;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004894 if (spec->alt_dac_nid) {
4895 p = spec->stream_analog_alt_playback;
4896 if (!p)
4897 p = &pcm_analog_alt_playback;
4898 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
4899 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
4900 spec->alt_dac_nid;
4901 } else {
4902 info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
4903 pcm_null_stream;
4904 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = 0;
4905 }
4906 if (have_multi_adcs) {
4907 p = spec->stream_analog_alt_capture;
4908 if (!p)
4909 p = &pcm_analog_alt_capture;
4910 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
4911 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
4912 spec->adc_nids[1];
4913 info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams =
4914 spec->num_adc_nids - 1;
4915 } else {
4916 info->stream[SNDRV_PCM_STREAM_CAPTURE] =
4917 pcm_null_stream;
4918 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = 0;
4919 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004920 }
4921
4922 return 0;
4923}
Takashi Iwai352f7f92012-12-19 12:52:06 +01004924EXPORT_SYMBOL_HDA(snd_hda_gen_build_pcms);
4925
4926
4927/*
4928 * Standard auto-parser initializations
4929 */
4930
Takashi Iwaid4156932013-01-07 10:08:02 +01004931/* configure the given path as a proper output */
Takashi Iwai2c12c302013-01-10 09:33:29 +01004932static void set_output_and_unmute(struct hda_codec *codec, int path_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004933{
4934 struct nid_path *path;
Takashi Iwaid4156932013-01-07 10:08:02 +01004935 hda_nid_t pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004936
Takashi Iwai196c17662013-01-04 15:01:40 +01004937 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwaid4156932013-01-07 10:08:02 +01004938 if (!path || !path->depth)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004939 return;
Takashi Iwaid4156932013-01-07 10:08:02 +01004940 pin = path->path[path->depth - 1];
Takashi Iwai2c12c302013-01-10 09:33:29 +01004941 restore_pin_ctl(codec, pin);
Takashi Iwai65033cc2013-04-16 12:31:05 +02004942 snd_hda_activate_path(codec, path, path->active,
4943 aamix_default(codec->spec));
Takashi Iwaie1284af2013-01-03 16:33:02 +01004944 set_pin_eapd(codec, pin, path->active);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004945}
4946
4947/* initialize primary output paths */
4948static void init_multi_out(struct hda_codec *codec)
4949{
4950 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004951 int i;
4952
Takashi Iwaid4156932013-01-07 10:08:02 +01004953 for (i = 0; i < spec->autocfg.line_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01004954 set_output_and_unmute(codec, spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004955}
4956
Takashi Iwaidb23fd12012-12-20 15:27:24 +01004957
Takashi Iwai2c12c302013-01-10 09:33:29 +01004958static void __init_extra_out(struct hda_codec *codec, int num_outs, int *paths)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004959{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004960 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004961
Takashi Iwaid4156932013-01-07 10:08:02 +01004962 for (i = 0; i < num_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01004963 set_output_and_unmute(codec, paths[i]);
Takashi Iwaidb23fd12012-12-20 15:27:24 +01004964}
4965
4966/* initialize hp and speaker paths */
4967static void init_extra_out(struct hda_codec *codec)
4968{
4969 struct hda_gen_spec *spec = codec->spec;
4970
4971 if (spec->autocfg.line_out_type != AUTO_PIN_HP_OUT)
Takashi Iwai2c12c302013-01-10 09:33:29 +01004972 __init_extra_out(codec, spec->autocfg.hp_outs, spec->hp_paths);
Takashi Iwaidb23fd12012-12-20 15:27:24 +01004973 if (spec->autocfg.line_out_type != AUTO_PIN_SPEAKER_OUT)
4974 __init_extra_out(codec, spec->autocfg.speaker_outs,
Takashi Iwai2c12c302013-01-10 09:33:29 +01004975 spec->speaker_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004976}
4977
4978/* initialize multi-io paths */
4979static void init_multi_io(struct hda_codec *codec)
4980{
4981 struct hda_gen_spec *spec = codec->spec;
4982 int i;
4983
4984 for (i = 0; i < spec->multi_ios; i++) {
4985 hda_nid_t pin = spec->multi_io[i].pin;
4986 struct nid_path *path;
Takashi Iwai196c17662013-01-04 15:01:40 +01004987 path = get_multiio_path(codec, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004988 if (!path)
4989 continue;
4990 if (!spec->multi_io[i].ctl_in)
4991 spec->multi_io[i].ctl_in =
Takashi Iwai2c12c302013-01-10 09:33:29 +01004992 snd_hda_codec_get_pin_target(codec, pin);
Takashi Iwai65033cc2013-04-16 12:31:05 +02004993 snd_hda_activate_path(codec, path, path->active,
4994 aamix_default(spec));
Takashi Iwai352f7f92012-12-19 12:52:06 +01004995 }
4996}
4997
Takashi Iwai352f7f92012-12-19 12:52:06 +01004998/* set up input pins and loopback paths */
4999static void init_analog_input(struct hda_codec *codec)
5000{
5001 struct hda_gen_spec *spec = codec->spec;
5002 struct auto_pin_cfg *cfg = &spec->autocfg;
5003 int i;
5004
5005 for (i = 0; i < cfg->num_inputs; i++) {
5006 hda_nid_t nid = cfg->inputs[i].pin;
5007 if (is_input_pin(codec, nid))
Takashi Iwai2c12c302013-01-10 09:33:29 +01005008 restore_pin_ctl(codec, nid);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005009
5010 /* init loopback inputs */
5011 if (spec->mixer_nid) {
Takashi Iwai3e367f12013-01-23 17:07:23 +01005012 resume_path_from_idx(codec, spec->loopback_paths[i]);
5013 resume_path_from_idx(codec, spec->loopback_merge_path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005014 }
5015 }
5016}
5017
5018/* initialize ADC paths */
5019static void init_input_src(struct hda_codec *codec)
5020{
5021 struct hda_gen_spec *spec = codec->spec;
5022 struct hda_input_mux *imux = &spec->input_mux;
5023 struct nid_path *path;
5024 int i, c, nums;
5025
5026 if (spec->dyn_adc_switch)
5027 nums = 1;
5028 else
5029 nums = spec->num_adc_nids;
5030
5031 for (c = 0; c < nums; c++) {
5032 for (i = 0; i < imux->num_items; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01005033 path = get_input_path(codec, c, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005034 if (path) {
5035 bool active = path->active;
5036 if (i == spec->cur_mux[c])
5037 active = true;
5038 snd_hda_activate_path(codec, path, active, false);
5039 }
5040 }
Takashi Iwai967303d2013-02-19 17:12:42 +01005041 if (spec->hp_mic)
5042 update_hp_mic(codec, c, true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005043 }
5044
Takashi Iwai352f7f92012-12-19 12:52:06 +01005045 if (spec->cap_sync_hook)
Takashi Iwaia90229e2013-01-18 14:10:00 +01005046 spec->cap_sync_hook(codec, NULL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005047}
5048
5049/* set right pin controls for digital I/O */
5050static void init_digital(struct hda_codec *codec)
5051{
5052 struct hda_gen_spec *spec = codec->spec;
5053 int i;
5054 hda_nid_t pin;
5055
Takashi Iwaid4156932013-01-07 10:08:02 +01005056 for (i = 0; i < spec->autocfg.dig_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01005057 set_output_and_unmute(codec, spec->digout_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005058 pin = spec->autocfg.dig_in_pin;
Takashi Iwai2430d7b2013-01-04 15:09:42 +01005059 if (pin) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01005060 restore_pin_ctl(codec, pin);
Takashi Iwai3e367f12013-01-23 17:07:23 +01005061 resume_path_from_idx(codec, spec->digin_path);
Takashi Iwai2430d7b2013-01-04 15:09:42 +01005062 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01005063}
5064
Takashi Iwai973e4972012-12-20 15:16:09 +01005065/* clear unsol-event tags on unused pins; Conexant codecs seem to leave
5066 * invalid unsol tags by some reason
5067 */
5068static void clear_unsol_on_unused_pins(struct hda_codec *codec)
5069{
5070 int i;
5071
5072 for (i = 0; i < codec->init_pins.used; i++) {
5073 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
5074 hda_nid_t nid = pin->nid;
5075 if (is_jack_detectable(codec, nid) &&
5076 !snd_hda_jack_tbl_get(codec, nid))
5077 snd_hda_codec_update_cache(codec, nid, 0,
5078 AC_VERB_SET_UNSOLICITED_ENABLE, 0);
5079 }
5080}
5081
Takashi Iwai5187ac12013-01-07 12:52:16 +01005082/*
5083 * initialize the generic spec;
5084 * this can be put as patch_ops.init function
5085 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01005086int snd_hda_gen_init(struct hda_codec *codec)
5087{
5088 struct hda_gen_spec *spec = codec->spec;
5089
5090 if (spec->init_hook)
5091 spec->init_hook(codec);
5092
5093 snd_hda_apply_verbs(codec);
5094
Takashi Iwai3bbcd272012-12-20 11:50:58 +01005095 codec->cached_write = 1;
5096
Takashi Iwai352f7f92012-12-19 12:52:06 +01005097 init_multi_out(codec);
5098 init_extra_out(codec);
5099 init_multi_io(codec);
5100 init_analog_input(codec);
5101 init_input_src(codec);
5102 init_digital(codec);
5103
Takashi Iwai973e4972012-12-20 15:16:09 +01005104 clear_unsol_on_unused_pins(codec);
5105
Takashi Iwai352f7f92012-12-19 12:52:06 +01005106 /* call init functions of standard auto-mute helpers */
Takashi Iwaia5cc2502013-01-16 18:08:55 +01005107 update_automute_all(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005108
Takashi Iwaidc870f32013-01-22 15:24:30 +01005109 snd_hda_codec_flush_cache(codec);
Takashi Iwai3bbcd272012-12-20 11:50:58 +01005110
Takashi Iwai352f7f92012-12-19 12:52:06 +01005111 if (spec->vmaster_mute.sw_kctl && spec->vmaster_mute.hook)
5112 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
5113
5114 hda_call_check_power_status(codec, 0x01);
5115 return 0;
5116}
Takashi Iwaifce52a32013-01-07 12:42:48 +01005117EXPORT_SYMBOL_HDA(snd_hda_gen_init);
5118
Takashi Iwai5187ac12013-01-07 12:52:16 +01005119/*
5120 * free the generic spec;
5121 * this can be put as patch_ops.free function
5122 */
Takashi Iwaifce52a32013-01-07 12:42:48 +01005123void snd_hda_gen_free(struct hda_codec *codec)
5124{
Takashi Iwai7504b6c2013-03-18 11:25:51 +01005125 snd_hda_detach_beep_device(codec);
Takashi Iwaifce52a32013-01-07 12:42:48 +01005126 snd_hda_gen_spec_free(codec->spec);
5127 kfree(codec->spec);
5128 codec->spec = NULL;
5129}
5130EXPORT_SYMBOL_HDA(snd_hda_gen_free);
5131
5132#ifdef CONFIG_PM
Takashi Iwai5187ac12013-01-07 12:52:16 +01005133/*
5134 * check the loopback power save state;
5135 * this can be put as patch_ops.check_power_status function
5136 */
Takashi Iwaifce52a32013-01-07 12:42:48 +01005137int snd_hda_gen_check_power_status(struct hda_codec *codec, hda_nid_t nid)
5138{
5139 struct hda_gen_spec *spec = codec->spec;
5140 return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
5141}
5142EXPORT_SYMBOL_HDA(snd_hda_gen_check_power_status);
5143#endif
Takashi Iwai352f7f92012-12-19 12:52:06 +01005144
5145
5146/*
5147 * the generic codec support
5148 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005149
Takashi Iwai352f7f92012-12-19 12:52:06 +01005150static const struct hda_codec_ops generic_patch_ops = {
5151 .build_controls = snd_hda_gen_build_controls,
5152 .build_pcms = snd_hda_gen_build_pcms,
5153 .init = snd_hda_gen_init,
Takashi Iwaifce52a32013-01-07 12:42:48 +01005154 .free = snd_hda_gen_free,
Takashi Iwai352f7f92012-12-19 12:52:06 +01005155 .unsol_event = snd_hda_jack_unsol_event,
Takashi Iwai83012a72012-08-24 18:38:08 +02005156#ifdef CONFIG_PM
Takashi Iwaifce52a32013-01-07 12:42:48 +01005157 .check_power_status = snd_hda_gen_check_power_status,
Takashi Iwaicb53c622007-08-10 17:21:45 +02005158#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005159};
5160
Linus Torvalds1da177e2005-04-16 15:20:36 -07005161int snd_hda_parse_generic_codec(struct hda_codec *codec)
5162{
Takashi Iwai352f7f92012-12-19 12:52:06 +01005163 struct hda_gen_spec *spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005164 int err;
5165
Takashi Iwaie560d8d2005-09-09 14:21:46 +02005166 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005167 if (!spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005168 return -ENOMEM;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005169 snd_hda_gen_spec_init(spec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005170 codec->spec = spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005171
Takashi Iwai9eb413e2012-12-19 14:41:21 +01005172 err = snd_hda_parse_pin_defcfg(codec, &spec->autocfg, NULL, 0);
5173 if (err < 0)
5174 return err;
5175
5176 err = snd_hda_gen_parse_auto_config(codec, &spec->autocfg);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005177 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005178 goto error;
5179
5180 codec->patch_ops = generic_patch_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005181 return 0;
5182
Takashi Iwai352f7f92012-12-19 12:52:06 +01005183error:
Takashi Iwaifce52a32013-01-07 12:42:48 +01005184 snd_hda_gen_free(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005185 return err;
5186}
Takashi Iwaifce52a32013-01-07 12:42:48 +01005187EXPORT_SYMBOL_HDA(snd_hda_parse_generic_codec);