blob: 326302fcab44b81d3fce9ab7d49b2e4e08ce9608 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Universal Interface for Intel High Definition Audio Codec
3 *
4 * Generic widget tree parser
5 *
6 * Copyright (c) 2004 Takashi Iwai <tiwai@suse.de>
7 *
8 * This driver is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This driver is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */
22
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/init.h>
24#include <linux/slab.h>
Paul Gortmakerd81a6d72011-09-22 09:34:58 -040025#include <linux/export.h>
Takashi Iwai352f7f92012-12-19 12:52:06 +010026#include <linux/sort.h>
Takashi Iwai55196ff2013-01-24 17:32:56 +010027#include <linux/delay.h>
Takashi Iwaif873e532012-12-20 16:58:39 +010028#include <linux/ctype.h>
29#include <linux/string.h>
Takashi Iwai294765582013-01-17 09:52:11 +010030#include <linux/bitops.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <sound/core.h>
Takashi Iwai352f7f92012-12-19 12:52:06 +010032#include <sound/jack.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include "hda_codec.h"
34#include "hda_local.h"
Takashi Iwai352f7f92012-12-19 12:52:06 +010035#include "hda_auto_parser.h"
36#include "hda_jack.h"
Takashi Iwai7504b6c2013-03-18 11:25:51 +010037#include "hda_beep.h"
Takashi Iwai352f7f92012-12-19 12:52:06 +010038#include "hda_generic.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Takashi Iwai352f7f92012-12-19 12:52:06 +010041/* initialize hda_gen_spec struct */
42int snd_hda_gen_spec_init(struct hda_gen_spec *spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -070043{
Takashi Iwai352f7f92012-12-19 12:52:06 +010044 snd_array_init(&spec->kctls, sizeof(struct snd_kcontrol_new), 32);
Takashi Iwai352f7f92012-12-19 12:52:06 +010045 snd_array_init(&spec->paths, sizeof(struct nid_path), 8);
Takashi Iwai0186f4f2013-02-07 10:45:11 +010046 snd_array_init(&spec->loopback_list, sizeof(struct hda_amp_list), 8);
Takashi Iwai38cf6f12012-12-21 14:09:42 +010047 mutex_init(&spec->pcm_mutex);
Takashi Iwai352f7f92012-12-19 12:52:06 +010048 return 0;
49}
50EXPORT_SYMBOL_HDA(snd_hda_gen_spec_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Takashi Iwai12c93df2012-12-19 14:38:33 +010052struct snd_kcontrol_new *
53snd_hda_gen_add_kctl(struct hda_gen_spec *spec, const char *name,
54 const struct snd_kcontrol_new *temp)
Takashi Iwai352f7f92012-12-19 12:52:06 +010055{
56 struct snd_kcontrol_new *knew = snd_array_new(&spec->kctls);
57 if (!knew)
58 return NULL;
59 *knew = *temp;
60 if (name)
61 knew->name = kstrdup(name, GFP_KERNEL);
62 else if (knew->name)
63 knew->name = kstrdup(knew->name, GFP_KERNEL);
64 if (!knew->name)
65 return NULL;
66 return knew;
67}
Takashi Iwai12c93df2012-12-19 14:38:33 +010068EXPORT_SYMBOL_HDA(snd_hda_gen_add_kctl);
Takashi Iwai352f7f92012-12-19 12:52:06 +010069
70static void free_kctls(struct hda_gen_spec *spec)
71{
72 if (spec->kctls.list) {
73 struct snd_kcontrol_new *kctl = spec->kctls.list;
74 int i;
75 for (i = 0; i < spec->kctls.used; i++)
76 kfree(kctl[i].name);
77 }
78 snd_array_free(&spec->kctls);
79}
80
Takashi Iwai352f7f92012-12-19 12:52:06 +010081void snd_hda_gen_spec_free(struct hda_gen_spec *spec)
82{
83 if (!spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 return;
Takashi Iwai352f7f92012-12-19 12:52:06 +010085 free_kctls(spec);
Takashi Iwai352f7f92012-12-19 12:52:06 +010086 snd_array_free(&spec->paths);
Takashi Iwai0186f4f2013-02-07 10:45:11 +010087 snd_array_free(&spec->loopback_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088}
Takashi Iwai352f7f92012-12-19 12:52:06 +010089EXPORT_SYMBOL_HDA(snd_hda_gen_spec_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
91/*
Takashi Iwai1c70a582013-01-11 17:48:22 +010092 * store user hints
93 */
94static void parse_user_hints(struct hda_codec *codec)
95{
96 struct hda_gen_spec *spec = codec->spec;
97 int val;
98
99 val = snd_hda_get_bool_hint(codec, "jack_detect");
100 if (val >= 0)
101 codec->no_jack_detect = !val;
102 val = snd_hda_get_bool_hint(codec, "inv_jack_detect");
103 if (val >= 0)
104 codec->inv_jack_detect = !!val;
105 val = snd_hda_get_bool_hint(codec, "trigger_sense");
106 if (val >= 0)
107 codec->no_trigger_sense = !val;
108 val = snd_hda_get_bool_hint(codec, "inv_eapd");
109 if (val >= 0)
110 codec->inv_eapd = !!val;
111 val = snd_hda_get_bool_hint(codec, "pcm_format_first");
112 if (val >= 0)
113 codec->pcm_format_first = !!val;
114 val = snd_hda_get_bool_hint(codec, "sticky_stream");
115 if (val >= 0)
116 codec->no_sticky_stream = !val;
117 val = snd_hda_get_bool_hint(codec, "spdif_status_reset");
118 if (val >= 0)
119 codec->spdif_status_reset = !!val;
120 val = snd_hda_get_bool_hint(codec, "pin_amp_workaround");
121 if (val >= 0)
122 codec->pin_amp_workaround = !!val;
123 val = snd_hda_get_bool_hint(codec, "single_adc_amp");
124 if (val >= 0)
125 codec->single_adc_amp = !!val;
126
Takashi Iwaif72706b2013-01-16 18:20:07 +0100127 val = snd_hda_get_bool_hint(codec, "auto_mute");
128 if (val >= 0)
129 spec->suppress_auto_mute = !val;
Takashi Iwai1c70a582013-01-11 17:48:22 +0100130 val = snd_hda_get_bool_hint(codec, "auto_mic");
131 if (val >= 0)
132 spec->suppress_auto_mic = !val;
133 val = snd_hda_get_bool_hint(codec, "line_in_auto_switch");
134 if (val >= 0)
135 spec->line_in_auto_switch = !!val;
136 val = snd_hda_get_bool_hint(codec, "need_dac_fix");
137 if (val >= 0)
138 spec->need_dac_fix = !!val;
139 val = snd_hda_get_bool_hint(codec, "primary_hp");
140 if (val >= 0)
141 spec->no_primary_hp = !val;
142 val = snd_hda_get_bool_hint(codec, "multi_cap_vol");
143 if (val >= 0)
144 spec->multi_cap_vol = !!val;
145 val = snd_hda_get_bool_hint(codec, "inv_dmic_split");
146 if (val >= 0)
147 spec->inv_dmic_split = !!val;
148 val = snd_hda_get_bool_hint(codec, "indep_hp");
149 if (val >= 0)
150 spec->indep_hp = !!val;
151 val = snd_hda_get_bool_hint(codec, "add_stereo_mix_input");
152 if (val >= 0)
153 spec->add_stereo_mix_input = !!val;
Takashi Iwaif811c3c2013-03-07 18:32:59 +0100154 /* the following two are just for compatibility */
Takashi Iwai1c70a582013-01-11 17:48:22 +0100155 val = snd_hda_get_bool_hint(codec, "add_out_jack_modes");
156 if (val >= 0)
Takashi Iwaif811c3c2013-03-07 18:32:59 +0100157 spec->add_jack_modes = !!val;
Takashi Iwai294765582013-01-17 09:52:11 +0100158 val = snd_hda_get_bool_hint(codec, "add_in_jack_modes");
159 if (val >= 0)
Takashi Iwaif811c3c2013-03-07 18:32:59 +0100160 spec->add_jack_modes = !!val;
161 val = snd_hda_get_bool_hint(codec, "add_jack_modes");
162 if (val >= 0)
163 spec->add_jack_modes = !!val;
Takashi Iwai55196ff2013-01-24 17:32:56 +0100164 val = snd_hda_get_bool_hint(codec, "power_down_unused");
165 if (val >= 0)
166 spec->power_down_unused = !!val;
Takashi Iwai967303d2013-02-19 17:12:42 +0100167 val = snd_hda_get_bool_hint(codec, "add_hp_mic");
168 if (val >= 0)
169 spec->hp_mic = !!val;
170 val = snd_hda_get_bool_hint(codec, "hp_mic_detect");
171 if (val >= 0)
172 spec->suppress_hp_mic_detect = !val;
Takashi Iwai1c70a582013-01-11 17:48:22 +0100173
174 if (!snd_hda_get_int_hint(codec, "mixer_nid", &val))
175 spec->mixer_nid = val;
176}
177
178/*
Takashi Iwai2c12c302013-01-10 09:33:29 +0100179 * pin control value accesses
180 */
181
182#define update_pin_ctl(codec, pin, val) \
183 snd_hda_codec_update_cache(codec, pin, 0, \
184 AC_VERB_SET_PIN_WIDGET_CONTROL, val)
185
186/* restore the pinctl based on the cached value */
187static inline void restore_pin_ctl(struct hda_codec *codec, hda_nid_t pin)
188{
189 update_pin_ctl(codec, pin, snd_hda_codec_get_pin_target(codec, pin));
190}
191
192/* set the pinctl target value and write it if requested */
193static void set_pin_target(struct hda_codec *codec, hda_nid_t pin,
194 unsigned int val, bool do_write)
195{
196 if (!pin)
197 return;
198 val = snd_hda_correct_pin_ctl(codec, pin, val);
199 snd_hda_codec_set_pin_target(codec, pin, val);
200 if (do_write)
201 update_pin_ctl(codec, pin, val);
202}
203
204/* set pinctl target values for all given pins */
205static void set_pin_targets(struct hda_codec *codec, int num_pins,
206 hda_nid_t *pins, unsigned int val)
207{
208 int i;
209 for (i = 0; i < num_pins; i++)
210 set_pin_target(codec, pins[i], val, false);
211}
212
213/*
Takashi Iwai352f7f92012-12-19 12:52:06 +0100214 * parsing paths
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100217/* return the position of NID in the list, or -1 if not found */
218static int find_idx_in_nid_list(hda_nid_t nid, const hda_nid_t *list, int nums)
219{
220 int i;
221 for (i = 0; i < nums; i++)
222 if (list[i] == nid)
223 return i;
224 return -1;
225}
226
227/* return true if the given NID is contained in the path */
228static bool is_nid_contained(struct nid_path *path, hda_nid_t nid)
229{
230 return find_idx_in_nid_list(nid, path->path, path->depth) >= 0;
231}
232
Takashi Iwaif5172a72013-01-04 13:19:55 +0100233static struct nid_path *get_nid_path(struct hda_codec *codec,
234 hda_nid_t from_nid, hda_nid_t to_nid,
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100235 int anchor_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100237 struct hda_gen_spec *spec = codec->spec;
238 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
Takashi Iwai352f7f92012-12-19 12:52:06 +0100240 for (i = 0; i < spec->paths.used; i++) {
241 struct nid_path *path = snd_array_elem(&spec->paths, i);
242 if (path->depth <= 0)
243 continue;
244 if ((!from_nid || path->path[0] == from_nid) &&
Takashi Iwaif5172a72013-01-04 13:19:55 +0100245 (!to_nid || path->path[path->depth - 1] == to_nid)) {
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100246 if (!anchor_nid ||
247 (anchor_nid > 0 && is_nid_contained(path, anchor_nid)) ||
248 (anchor_nid < 0 && !is_nid_contained(path, anchor_nid)))
Takashi Iwaif5172a72013-01-04 13:19:55 +0100249 return path;
250 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 }
252 return NULL;
253}
Takashi Iwaif5172a72013-01-04 13:19:55 +0100254
255/* get the path between the given NIDs;
256 * passing 0 to either @pin or @dac behaves as a wildcard
257 */
258struct nid_path *snd_hda_get_nid_path(struct hda_codec *codec,
259 hda_nid_t from_nid, hda_nid_t to_nid)
260{
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100261 return get_nid_path(codec, from_nid, to_nid, 0);
Takashi Iwaif5172a72013-01-04 13:19:55 +0100262}
Takashi Iwai352f7f92012-12-19 12:52:06 +0100263EXPORT_SYMBOL_HDA(snd_hda_get_nid_path);
264
Takashi Iwai196c17662013-01-04 15:01:40 +0100265/* get the index number corresponding to the path instance;
266 * the index starts from 1, for easier checking the invalid value
267 */
268int snd_hda_get_path_idx(struct hda_codec *codec, struct nid_path *path)
269{
270 struct hda_gen_spec *spec = codec->spec;
271 struct nid_path *array = spec->paths.list;
272 ssize_t idx;
273
274 if (!spec->paths.used)
275 return 0;
276 idx = path - array;
277 if (idx < 0 || idx >= spec->paths.used)
278 return 0;
279 return idx + 1;
280}
Takashi Iwai4bd01e92013-01-22 15:17:20 +0100281EXPORT_SYMBOL_HDA(snd_hda_get_path_idx);
Takashi Iwai196c17662013-01-04 15:01:40 +0100282
283/* get the path instance corresponding to the given index number */
284struct nid_path *snd_hda_get_path_from_idx(struct hda_codec *codec, int idx)
285{
286 struct hda_gen_spec *spec = codec->spec;
287
288 if (idx <= 0 || idx > spec->paths.used)
289 return NULL;
290 return snd_array_elem(&spec->paths, idx - 1);
291}
Takashi Iwai4bd01e92013-01-22 15:17:20 +0100292EXPORT_SYMBOL_HDA(snd_hda_get_path_from_idx);
Takashi Iwai196c17662013-01-04 15:01:40 +0100293
Takashi Iwai352f7f92012-12-19 12:52:06 +0100294/* check whether the given DAC is already found in any existing paths */
295static bool is_dac_already_used(struct hda_codec *codec, hda_nid_t nid)
296{
297 struct hda_gen_spec *spec = codec->spec;
298 int i;
299
300 for (i = 0; i < spec->paths.used; i++) {
301 struct nid_path *path = snd_array_elem(&spec->paths, i);
302 if (path->path[0] == nid)
303 return true;
304 }
305 return false;
306}
307
308/* check whether the given two widgets can be connected */
309static bool is_reachable_path(struct hda_codec *codec,
310 hda_nid_t from_nid, hda_nid_t to_nid)
311{
312 if (!from_nid || !to_nid)
313 return false;
314 return snd_hda_get_conn_index(codec, to_nid, from_nid, true) >= 0;
315}
316
317/* nid, dir and idx */
318#define AMP_VAL_COMPARE_MASK (0xffff | (1U << 18) | (0x0f << 19))
319
320/* check whether the given ctl is already assigned in any path elements */
321static bool is_ctl_used(struct hda_codec *codec, unsigned int val, int type)
322{
323 struct hda_gen_spec *spec = codec->spec;
324 int i;
325
326 val &= AMP_VAL_COMPARE_MASK;
327 for (i = 0; i < spec->paths.used; i++) {
328 struct nid_path *path = snd_array_elem(&spec->paths, i);
329 if ((path->ctls[type] & AMP_VAL_COMPARE_MASK) == val)
330 return true;
331 }
332 return false;
333}
334
335/* check whether a control with the given (nid, dir, idx) was assigned */
336static bool is_ctl_associated(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwai8999bf02013-01-18 11:01:33 +0100337 int dir, int idx, int type)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100338{
339 unsigned int val = HDA_COMPOSE_AMP_VAL(nid, 3, idx, dir);
Takashi Iwai8999bf02013-01-18 11:01:33 +0100340 return is_ctl_used(codec, val, type);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100341}
342
Takashi Iwai0c8c0f52012-12-20 17:54:22 +0100343static void print_nid_path(const char *pfx, struct nid_path *path)
344{
345 char buf[40];
346 int i;
347
348
349 buf[0] = 0;
350 for (i = 0; i < path->depth; i++) {
351 char tmp[4];
352 sprintf(tmp, ":%02x", path->path[i]);
353 strlcat(buf, tmp, sizeof(buf));
354 }
355 snd_printdd("%s path: depth=%d %s\n", pfx, path->depth, buf);
356}
357
Takashi Iwai352f7f92012-12-19 12:52:06 +0100358/* called recursively */
359static bool __parse_nid_path(struct hda_codec *codec,
360 hda_nid_t from_nid, hda_nid_t to_nid,
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100361 int anchor_nid, struct nid_path *path,
362 int depth)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100363{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100364 const hda_nid_t *conn;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100365 int i, nums;
366
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100367 if (to_nid == anchor_nid)
368 anchor_nid = 0; /* anchor passed */
369 else if (to_nid == (hda_nid_t)(-anchor_nid))
370 return false; /* hit the exclusive nid */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100371
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100372 nums = snd_hda_get_conn_list(codec, to_nid, &conn);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100373 for (i = 0; i < nums; i++) {
374 if (conn[i] != from_nid) {
375 /* special case: when from_nid is 0,
376 * try to find an empty DAC
377 */
378 if (from_nid ||
379 get_wcaps_type(get_wcaps(codec, conn[i])) != AC_WID_AUD_OUT ||
380 is_dac_already_used(codec, conn[i]))
381 continue;
382 }
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100383 /* anchor is not requested or already passed? */
384 if (anchor_nid <= 0)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100385 goto found;
386 }
387 if (depth >= MAX_NID_PATH_DEPTH)
388 return false;
389 for (i = 0; i < nums; i++) {
390 unsigned int type;
391 type = get_wcaps_type(get_wcaps(codec, conn[i]));
392 if (type == AC_WID_AUD_OUT || type == AC_WID_AUD_IN ||
393 type == AC_WID_PIN)
394 continue;
395 if (__parse_nid_path(codec, from_nid, conn[i],
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100396 anchor_nid, path, depth + 1))
Takashi Iwai352f7f92012-12-19 12:52:06 +0100397 goto found;
398 }
399 return false;
400
401 found:
402 path->path[path->depth] = conn[i];
403 path->idx[path->depth + 1] = i;
404 if (nums > 1 && get_wcaps_type(get_wcaps(codec, to_nid)) != AC_WID_AUD_MIX)
405 path->multi[path->depth + 1] = 1;
406 path->depth++;
407 return true;
408}
409
410/* parse the widget path from the given nid to the target nid;
411 * when @from_nid is 0, try to find an empty DAC;
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100412 * when @anchor_nid is set to a positive value, only paths through the widget
413 * with the given value are evaluated.
414 * when @anchor_nid is set to a negative value, paths through the widget
415 * with the negative of given value are excluded, only other paths are chosen.
416 * when @anchor_nid is zero, no special handling about path selection.
Takashi Iwai352f7f92012-12-19 12:52:06 +0100417 */
418bool snd_hda_parse_nid_path(struct hda_codec *codec, hda_nid_t from_nid,
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100419 hda_nid_t to_nid, int anchor_nid,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100420 struct nid_path *path)
421{
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100422 if (__parse_nid_path(codec, from_nid, to_nid, anchor_nid, path, 1)) {
Takashi Iwai352f7f92012-12-19 12:52:06 +0100423 path->path[path->depth] = to_nid;
424 path->depth++;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100425 return true;
426 }
427 return false;
428}
429EXPORT_SYMBOL_HDA(snd_hda_parse_nid_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
431/*
Takashi Iwai352f7f92012-12-19 12:52:06 +0100432 * parse the path between the given NIDs and add to the path list.
433 * if no valid path is found, return NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100435struct nid_path *
436snd_hda_add_new_path(struct hda_codec *codec, hda_nid_t from_nid,
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100437 hda_nid_t to_nid, int anchor_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100439 struct hda_gen_spec *spec = codec->spec;
440 struct nid_path *path;
441
442 if (from_nid && to_nid && !is_reachable_path(codec, from_nid, to_nid))
443 return NULL;
444
Takashi Iwaif5172a72013-01-04 13:19:55 +0100445 /* check whether the path has been already added */
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100446 path = get_nid_path(codec, from_nid, to_nid, anchor_nid);
Takashi Iwaif5172a72013-01-04 13:19:55 +0100447 if (path)
448 return path;
449
Takashi Iwai352f7f92012-12-19 12:52:06 +0100450 path = snd_array_new(&spec->paths);
451 if (!path)
452 return NULL;
453 memset(path, 0, sizeof(*path));
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100454 if (snd_hda_parse_nid_path(codec, from_nid, to_nid, anchor_nid, path))
Takashi Iwai352f7f92012-12-19 12:52:06 +0100455 return path;
456 /* push back */
457 spec->paths.used--;
458 return NULL;
459}
460EXPORT_SYMBOL_HDA(snd_hda_add_new_path);
461
Takashi Iwai980428c2013-01-09 09:28:20 +0100462/* clear the given path as invalid so that it won't be picked up later */
463static void invalidate_nid_path(struct hda_codec *codec, int idx)
464{
465 struct nid_path *path = snd_hda_get_path_from_idx(codec, idx);
466 if (!path)
467 return;
468 memset(path, 0, sizeof(*path));
469}
470
Takashi Iwai352f7f92012-12-19 12:52:06 +0100471/* look for an empty DAC slot */
472static hda_nid_t look_for_dac(struct hda_codec *codec, hda_nid_t pin,
473 bool is_digital)
474{
475 struct hda_gen_spec *spec = codec->spec;
476 bool cap_digital;
477 int i;
478
479 for (i = 0; i < spec->num_all_dacs; i++) {
480 hda_nid_t nid = spec->all_dacs[i];
481 if (!nid || is_dac_already_used(codec, nid))
482 continue;
483 cap_digital = !!(get_wcaps(codec, nid) & AC_WCAP_DIGITAL);
484 if (is_digital != cap_digital)
485 continue;
486 if (is_reachable_path(codec, nid, pin))
487 return nid;
488 }
489 return 0;
490}
491
492/* replace the channels in the composed amp value with the given number */
493static unsigned int amp_val_replace_channels(unsigned int val, unsigned int chs)
494{
495 val &= ~(0x3U << 16);
496 val |= chs << 16;
497 return val;
498}
499
500/* check whether the widget has the given amp capability for the direction */
501static bool check_amp_caps(struct hda_codec *codec, hda_nid_t nid,
502 int dir, unsigned int bits)
503{
504 if (!nid)
505 return false;
506 if (get_wcaps(codec, nid) & (1 << (dir + 1)))
507 if (query_amp_caps(codec, nid, dir) & bits)
508 return true;
509 return false;
510}
511
David Henningsson99a55922013-01-16 15:58:44 +0100512static bool same_amp_caps(struct hda_codec *codec, hda_nid_t nid1,
513 hda_nid_t nid2, int dir)
514{
515 if (!(get_wcaps(codec, nid1) & (1 << (dir + 1))))
516 return !(get_wcaps(codec, nid2) & (1 << (dir + 1)));
517 return (query_amp_caps(codec, nid1, dir) ==
518 query_amp_caps(codec, nid2, dir));
519}
520
Takashi Iwai352f7f92012-12-19 12:52:06 +0100521#define nid_has_mute(codec, nid, dir) \
522 check_amp_caps(codec, nid, dir, AC_AMPCAP_MUTE)
523#define nid_has_volume(codec, nid, dir) \
524 check_amp_caps(codec, nid, dir, AC_AMPCAP_NUM_STEPS)
525
526/* look for a widget suitable for assigning a mute switch in the path */
527static hda_nid_t look_for_out_mute_nid(struct hda_codec *codec,
528 struct nid_path *path)
529{
530 int i;
531
532 for (i = path->depth - 1; i >= 0; i--) {
533 if (nid_has_mute(codec, path->path[i], HDA_OUTPUT))
534 return path->path[i];
535 if (i != path->depth - 1 && i != 0 &&
536 nid_has_mute(codec, path->path[i], HDA_INPUT))
537 return path->path[i];
538 }
539 return 0;
540}
541
542/* look for a widget suitable for assigning a volume ctl in the path */
543static hda_nid_t look_for_out_vol_nid(struct hda_codec *codec,
544 struct nid_path *path)
545{
546 int i;
547
548 for (i = path->depth - 1; i >= 0; i--) {
549 if (nid_has_volume(codec, path->path[i], HDA_OUTPUT))
550 return path->path[i];
551 }
Takashi Iwai82beb8f2007-08-10 17:09:26 +0200552 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553}
554
555/*
Takashi Iwai352f7f92012-12-19 12:52:06 +0100556 * path activation / deactivation
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100558
559/* can have the amp-in capability? */
560static bool has_amp_in(struct hda_codec *codec, struct nid_path *path, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100562 hda_nid_t nid = path->path[idx];
563 unsigned int caps = get_wcaps(codec, nid);
564 unsigned int type = get_wcaps_type(caps);
565
566 if (!(caps & AC_WCAP_IN_AMP))
567 return false;
568 if (type == AC_WID_PIN && idx > 0) /* only for input pins */
569 return false;
570 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571}
572
Takashi Iwai352f7f92012-12-19 12:52:06 +0100573/* can have the amp-out capability? */
574static bool has_amp_out(struct hda_codec *codec, struct nid_path *path, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100576 hda_nid_t nid = path->path[idx];
577 unsigned int caps = get_wcaps(codec, nid);
578 unsigned int type = get_wcaps_type(caps);
579
580 if (!(caps & AC_WCAP_OUT_AMP))
581 return false;
582 if (type == AC_WID_PIN && !idx) /* only for output pins */
583 return false;
584 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585}
586
Takashi Iwai352f7f92012-12-19 12:52:06 +0100587/* check whether the given (nid,dir,idx) is active */
588static bool is_active_nid(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwai7dddf2ae2013-01-24 16:31:35 +0100589 unsigned int dir, unsigned int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100591 struct hda_gen_spec *spec = codec->spec;
592 int i, n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
Takashi Iwai352f7f92012-12-19 12:52:06 +0100594 for (n = 0; n < spec->paths.used; n++) {
595 struct nid_path *path = snd_array_elem(&spec->paths, n);
596 if (!path->active)
597 continue;
598 for (i = 0; i < path->depth; i++) {
599 if (path->path[i] == nid) {
600 if (dir == HDA_OUTPUT || path->idx[i] == idx)
601 return true;
602 break;
603 }
604 }
605 }
606 return false;
607}
608
609/* get the default amp value for the target state */
610static int get_amp_val_to_activate(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwai8999bf02013-01-18 11:01:33 +0100611 int dir, unsigned int caps, bool enable)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100612{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100613 unsigned int val = 0;
614
Takashi Iwai352f7f92012-12-19 12:52:06 +0100615 if (caps & AC_AMPCAP_NUM_STEPS) {
616 /* set to 0dB */
617 if (enable)
618 val = (caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT;
619 }
620 if (caps & AC_AMPCAP_MUTE) {
621 if (!enable)
622 val |= HDA_AMP_MUTE;
623 }
624 return val;
625}
626
627/* initialize the amp value (only at the first time) */
628static void init_amp(struct hda_codec *codec, hda_nid_t nid, int dir, int idx)
629{
Takashi Iwai8999bf02013-01-18 11:01:33 +0100630 unsigned int caps = query_amp_caps(codec, nid, dir);
631 int val = get_amp_val_to_activate(codec, nid, dir, caps, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100632 snd_hda_codec_amp_init_stereo(codec, nid, dir, idx, 0xff, val);
633}
634
Takashi Iwai8999bf02013-01-18 11:01:33 +0100635/* calculate amp value mask we can modify;
636 * if the given amp is controlled by mixers, don't touch it
637 */
638static unsigned int get_amp_mask_to_modify(struct hda_codec *codec,
639 hda_nid_t nid, int dir, int idx,
640 unsigned int caps)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100641{
Takashi Iwai8999bf02013-01-18 11:01:33 +0100642 unsigned int mask = 0xff;
643
644 if (caps & AC_AMPCAP_MUTE) {
645 if (is_ctl_associated(codec, nid, dir, idx, NID_PATH_MUTE_CTL))
646 mask &= ~0x80;
647 }
648 if (caps & AC_AMPCAP_NUM_STEPS) {
649 if (is_ctl_associated(codec, nid, dir, idx, NID_PATH_VOL_CTL) ||
650 is_ctl_associated(codec, nid, dir, idx, NID_PATH_BOOST_CTL))
651 mask &= ~0x7f;
652 }
653 return mask;
654}
655
656static void activate_amp(struct hda_codec *codec, hda_nid_t nid, int dir,
657 int idx, int idx_to_check, bool enable)
658{
659 unsigned int caps;
660 unsigned int mask, val;
661
Takashi Iwai7dddf2ae2013-01-24 16:31:35 +0100662 if (!enable && is_active_nid(codec, nid, dir, idx_to_check))
Takashi Iwai352f7f92012-12-19 12:52:06 +0100663 return;
Takashi Iwai8999bf02013-01-18 11:01:33 +0100664
665 caps = query_amp_caps(codec, nid, dir);
666 val = get_amp_val_to_activate(codec, nid, dir, caps, enable);
667 mask = get_amp_mask_to_modify(codec, nid, dir, idx_to_check, caps);
668 if (!mask)
669 return;
670
671 val &= mask;
672 snd_hda_codec_amp_stereo(codec, nid, dir, idx, mask, val);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100673}
674
675static void activate_amp_out(struct hda_codec *codec, struct nid_path *path,
676 int i, bool enable)
677{
678 hda_nid_t nid = path->path[i];
679 init_amp(codec, nid, HDA_OUTPUT, 0);
Takashi Iwai8999bf02013-01-18 11:01:33 +0100680 activate_amp(codec, nid, HDA_OUTPUT, 0, 0, enable);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100681}
682
683static void activate_amp_in(struct hda_codec *codec, struct nid_path *path,
684 int i, bool enable, bool add_aamix)
685{
686 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100687 const hda_nid_t *conn;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100688 int n, nums, idx;
689 int type;
690 hda_nid_t nid = path->path[i];
691
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100692 nums = snd_hda_get_conn_list(codec, nid, &conn);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100693 type = get_wcaps_type(get_wcaps(codec, nid));
694 if (type == AC_WID_PIN ||
695 (type == AC_WID_AUD_IN && codec->single_adc_amp)) {
696 nums = 1;
697 idx = 0;
698 } else
699 idx = path->idx[i];
700
701 for (n = 0; n < nums; n++)
702 init_amp(codec, nid, HDA_INPUT, n);
703
Takashi Iwai352f7f92012-12-19 12:52:06 +0100704 /* here is a little bit tricky in comparison with activate_amp_out();
705 * when aa-mixer is available, we need to enable the path as well
706 */
707 for (n = 0; n < nums; n++) {
Takashi Iwaie4a395e2013-01-23 17:00:31 +0100708 if (n != idx && (!add_aamix || conn[n] != spec->mixer_merge_nid))
Takashi Iwai352f7f92012-12-19 12:52:06 +0100709 continue;
Takashi Iwai8999bf02013-01-18 11:01:33 +0100710 activate_amp(codec, nid, HDA_INPUT, n, idx, enable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 }
712}
713
Takashi Iwai352f7f92012-12-19 12:52:06 +0100714/* activate or deactivate the given path
715 * if @add_aamix is set, enable the input from aa-mix NID as well (if any)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100717void snd_hda_activate_path(struct hda_codec *codec, struct nid_path *path,
718 bool enable, bool add_aamix)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719{
Takashi Iwai55196ff2013-01-24 17:32:56 +0100720 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100721 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
Takashi Iwai352f7f92012-12-19 12:52:06 +0100723 if (!enable)
724 path->active = false;
725
726 for (i = path->depth - 1; i >= 0; i--) {
Takashi Iwai55196ff2013-01-24 17:32:56 +0100727 hda_nid_t nid = path->path[i];
728 if (enable && spec->power_down_unused) {
729 /* make sure the widget is powered up */
730 if (!snd_hda_check_power_state(codec, nid, AC_PWRST_D0))
731 snd_hda_codec_write(codec, nid, 0,
732 AC_VERB_SET_POWER_STATE,
733 AC_PWRST_D0);
734 }
Takashi Iwai352f7f92012-12-19 12:52:06 +0100735 if (enable && path->multi[i])
Takashi Iwai55196ff2013-01-24 17:32:56 +0100736 snd_hda_codec_write_cache(codec, nid, 0,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100737 AC_VERB_SET_CONNECT_SEL,
738 path->idx[i]);
739 if (has_amp_in(codec, path, i))
740 activate_amp_in(codec, path, i, enable, add_aamix);
741 if (has_amp_out(codec, path, i))
742 activate_amp_out(codec, path, i, enable);
743 }
744
745 if (enable)
746 path->active = true;
747}
748EXPORT_SYMBOL_HDA(snd_hda_activate_path);
749
Takashi Iwai55196ff2013-01-24 17:32:56 +0100750/* if the given path is inactive, put widgets into D3 (only if suitable) */
751static void path_power_down_sync(struct hda_codec *codec, struct nid_path *path)
752{
753 struct hda_gen_spec *spec = codec->spec;
754 bool changed;
755 int i;
756
757 if (!spec->power_down_unused || path->active)
758 return;
759
760 for (i = 0; i < path->depth; i++) {
761 hda_nid_t nid = path->path[i];
762 if (!snd_hda_check_power_state(codec, nid, AC_PWRST_D3)) {
763 snd_hda_codec_write(codec, nid, 0,
764 AC_VERB_SET_POWER_STATE,
765 AC_PWRST_D3);
766 changed = true;
767 }
768 }
769
770 if (changed) {
771 msleep(10);
772 snd_hda_codec_read(codec, path->path[0], 0,
773 AC_VERB_GET_POWER_STATE, 0);
774 }
775}
776
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +0100777/* turn on/off EAPD on the given pin */
778static void set_pin_eapd(struct hda_codec *codec, hda_nid_t pin, bool enable)
779{
780 struct hda_gen_spec *spec = codec->spec;
781 if (spec->own_eapd_ctl ||
782 !(snd_hda_query_pin_caps(codec, pin) & AC_PINCAP_EAPD))
783 return;
Takashi Iwaiecac3ed2012-12-21 15:23:01 +0100784 if (codec->inv_eapd)
785 enable = !enable;
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +0100786 snd_hda_codec_update_cache(codec, pin, 0,
787 AC_VERB_SET_EAPD_BTLENABLE,
788 enable ? 0x02 : 0x00);
789}
790
Takashi Iwai3e367f12013-01-23 17:07:23 +0100791/* re-initialize the path specified by the given path index */
792static void resume_path_from_idx(struct hda_codec *codec, int path_idx)
793{
794 struct nid_path *path = snd_hda_get_path_from_idx(codec, path_idx);
795 if (path)
796 snd_hda_activate_path(codec, path, path->active, false);
797}
798
Takashi Iwai352f7f92012-12-19 12:52:06 +0100799
800/*
801 * Helper functions for creating mixer ctl elements
802 */
803
804enum {
805 HDA_CTL_WIDGET_VOL,
806 HDA_CTL_WIDGET_MUTE,
807 HDA_CTL_BIND_MUTE,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100808};
809static const struct snd_kcontrol_new control_templates[] = {
810 HDA_CODEC_VOLUME(NULL, 0, 0, 0),
811 HDA_CODEC_MUTE(NULL, 0, 0, 0),
812 HDA_BIND_MUTE(NULL, 0, 0, 0),
Takashi Iwai352f7f92012-12-19 12:52:06 +0100813};
814
815/* add dynamic controls from template */
Takashi Iwaia35bd1e2013-01-18 14:01:14 +0100816static struct snd_kcontrol_new *
817add_control(struct hda_gen_spec *spec, int type, const char *name,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100818 int cidx, unsigned long val)
819{
820 struct snd_kcontrol_new *knew;
821
Takashi Iwai12c93df2012-12-19 14:38:33 +0100822 knew = snd_hda_gen_add_kctl(spec, name, &control_templates[type]);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100823 if (!knew)
Takashi Iwaia35bd1e2013-01-18 14:01:14 +0100824 return NULL;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100825 knew->index = cidx;
826 if (get_amp_nid_(val))
827 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
828 knew->private_value = val;
Takashi Iwaia35bd1e2013-01-18 14:01:14 +0100829 return knew;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100830}
831
832static int add_control_with_pfx(struct hda_gen_spec *spec, int type,
833 const char *pfx, const char *dir,
834 const char *sfx, int cidx, unsigned long val)
835{
836 char name[32];
837 snprintf(name, sizeof(name), "%s %s %s", pfx, dir, sfx);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +0100838 if (!add_control(spec, type, name, cidx, val))
839 return -ENOMEM;
840 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100841}
842
843#define add_pb_vol_ctrl(spec, type, pfx, val) \
844 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", 0, val)
845#define add_pb_sw_ctrl(spec, type, pfx, val) \
846 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", 0, val)
847#define __add_pb_vol_ctrl(spec, type, pfx, cidx, val) \
848 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", cidx, val)
849#define __add_pb_sw_ctrl(spec, type, pfx, cidx, val) \
850 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", cidx, val)
851
852static int add_vol_ctl(struct hda_codec *codec, const char *pfx, int cidx,
853 unsigned int chs, struct nid_path *path)
854{
855 unsigned int val;
856 if (!path)
857 return 0;
858 val = path->ctls[NID_PATH_VOL_CTL];
859 if (!val)
860 return 0;
861 val = amp_val_replace_channels(val, chs);
862 return __add_pb_vol_ctrl(codec->spec, HDA_CTL_WIDGET_VOL, pfx, cidx, val);
863}
864
865/* return the channel bits suitable for the given path->ctls[] */
866static int get_default_ch_nums(struct hda_codec *codec, struct nid_path *path,
867 int type)
868{
869 int chs = 1; /* mono (left only) */
870 if (path) {
871 hda_nid_t nid = get_amp_nid_(path->ctls[type]);
872 if (nid && (get_wcaps(codec, nid) & AC_WCAP_STEREO))
873 chs = 3; /* stereo */
874 }
875 return chs;
876}
877
878static int add_stereo_vol(struct hda_codec *codec, const char *pfx, int cidx,
879 struct nid_path *path)
880{
881 int chs = get_default_ch_nums(codec, path, NID_PATH_VOL_CTL);
882 return add_vol_ctl(codec, pfx, cidx, chs, path);
883}
884
885/* create a mute-switch for the given mixer widget;
886 * if it has multiple sources (e.g. DAC and loopback), create a bind-mute
887 */
888static int add_sw_ctl(struct hda_codec *codec, const char *pfx, int cidx,
889 unsigned int chs, struct nid_path *path)
890{
891 unsigned int val;
892 int type = HDA_CTL_WIDGET_MUTE;
893
894 if (!path)
895 return 0;
896 val = path->ctls[NID_PATH_MUTE_CTL];
897 if (!val)
898 return 0;
899 val = amp_val_replace_channels(val, chs);
900 if (get_amp_direction_(val) == HDA_INPUT) {
901 hda_nid_t nid = get_amp_nid_(val);
902 int nums = snd_hda_get_num_conns(codec, nid);
903 if (nums > 1) {
904 type = HDA_CTL_BIND_MUTE;
905 val |= nums << 19;
906 }
907 }
908 return __add_pb_sw_ctrl(codec->spec, type, pfx, cidx, val);
909}
910
911static int add_stereo_sw(struct hda_codec *codec, const char *pfx,
912 int cidx, struct nid_path *path)
913{
914 int chs = get_default_ch_nums(codec, path, NID_PATH_MUTE_CTL);
915 return add_sw_ctl(codec, pfx, cidx, chs, path);
916}
917
Takashi Iwai247d85e2013-01-17 16:18:11 +0100918/* any ctl assigned to the path with the given index? */
919static bool path_has_mixer(struct hda_codec *codec, int path_idx, int ctl_type)
920{
921 struct nid_path *path = snd_hda_get_path_from_idx(codec, path_idx);
922 return path && path->ctls[ctl_type];
923}
924
Takashi Iwai352f7f92012-12-19 12:52:06 +0100925static const char * const channel_name[4] = {
926 "Front", "Surround", "CLFE", "Side"
927};
928
929/* give some appropriate ctl name prefix for the given line out channel */
Takashi Iwai247d85e2013-01-17 16:18:11 +0100930static const char *get_line_out_pfx(struct hda_codec *codec, int ch,
931 int *index, int ctl_type)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100932{
Takashi Iwai247d85e2013-01-17 16:18:11 +0100933 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100934 struct auto_pin_cfg *cfg = &spec->autocfg;
935
936 *index = 0;
937 if (cfg->line_outs == 1 && !spec->multi_ios &&
Takashi Iwai247d85e2013-01-17 16:18:11 +0100938 !cfg->hp_outs && !cfg->speaker_outs)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100939 return spec->vmaster_mute.hook ? "PCM" : "Master";
940
941 /* if there is really a single DAC used in the whole output paths,
942 * use it master (or "PCM" if a vmaster hook is present)
943 */
944 if (spec->multiout.num_dacs == 1 && !spec->mixer_nid &&
945 !spec->multiout.hp_out_nid[0] && !spec->multiout.extra_out_nid[0])
946 return spec->vmaster_mute.hook ? "PCM" : "Master";
947
Takashi Iwai247d85e2013-01-17 16:18:11 +0100948 /* multi-io channels */
949 if (ch >= cfg->line_outs)
950 return channel_name[ch];
951
Takashi Iwai352f7f92012-12-19 12:52:06 +0100952 switch (cfg->line_out_type) {
953 case AUTO_PIN_SPEAKER_OUT:
Takashi Iwai247d85e2013-01-17 16:18:11 +0100954 /* if the primary channel vol/mute is shared with HP volume,
955 * don't name it as Speaker
956 */
957 if (!ch && cfg->hp_outs &&
958 !path_has_mixer(codec, spec->hp_paths[0], ctl_type))
959 break;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100960 if (cfg->line_outs == 1)
961 return "Speaker";
962 if (cfg->line_outs == 2)
963 return ch ? "Bass Speaker" : "Speaker";
964 break;
965 case AUTO_PIN_HP_OUT:
Takashi Iwai247d85e2013-01-17 16:18:11 +0100966 /* if the primary channel vol/mute is shared with spk volume,
967 * don't name it as Headphone
968 */
969 if (!ch && cfg->speaker_outs &&
970 !path_has_mixer(codec, spec->speaker_paths[0], ctl_type))
971 break;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100972 /* for multi-io case, only the primary out */
973 if (ch && spec->multi_ios)
974 break;
975 *index = ch;
976 return "Headphone";
Takashi Iwai352f7f92012-12-19 12:52:06 +0100977 }
Takashi Iwai247d85e2013-01-17 16:18:11 +0100978
979 /* for a single channel output, we don't have to name the channel */
980 if (cfg->line_outs == 1 && !spec->multi_ios)
981 return "PCM";
982
Takashi Iwai352f7f92012-12-19 12:52:06 +0100983 if (ch >= ARRAY_SIZE(channel_name)) {
984 snd_BUG();
985 return "PCM";
986 }
987
988 return channel_name[ch];
989}
990
991/*
992 * Parse output paths
993 */
994
995/* badness definition */
996enum {
997 /* No primary DAC is found for the main output */
998 BAD_NO_PRIMARY_DAC = 0x10000,
999 /* No DAC is found for the extra output */
1000 BAD_NO_DAC = 0x4000,
1001 /* No possible multi-ios */
Takashi Iwai1d739062013-02-13 14:17:32 +01001002 BAD_MULTI_IO = 0x120,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001003 /* No individual DAC for extra output */
1004 BAD_NO_EXTRA_DAC = 0x102,
1005 /* No individual DAC for extra surrounds */
1006 BAD_NO_EXTRA_SURR_DAC = 0x101,
1007 /* Primary DAC shared with main surrounds */
1008 BAD_SHARED_SURROUND = 0x100,
Takashi Iwai55a63d42013-03-21 17:20:12 +01001009 /* No independent HP possible */
1010 BAD_NO_INDEP_HP = 0x40,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001011 /* Primary DAC shared with main CLFE */
1012 BAD_SHARED_CLFE = 0x10,
1013 /* Primary DAC shared with extra surrounds */
1014 BAD_SHARED_EXTRA_SURROUND = 0x10,
1015 /* Volume widget is shared */
1016 BAD_SHARED_VOL = 0x10,
1017};
1018
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001019/* look for widgets in the given path which are appropriate for
Takashi Iwai352f7f92012-12-19 12:52:06 +01001020 * volume and mute controls, and assign the values to ctls[].
1021 *
1022 * When no appropriate widget is found in the path, the badness value
1023 * is incremented depending on the situation. The function returns the
1024 * total badness for both volume and mute controls.
1025 */
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001026static int assign_out_path_ctls(struct hda_codec *codec, struct nid_path *path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001027{
Takashi Iwai352f7f92012-12-19 12:52:06 +01001028 hda_nid_t nid;
1029 unsigned int val;
1030 int badness = 0;
1031
1032 if (!path)
1033 return BAD_SHARED_VOL * 2;
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001034
1035 if (path->ctls[NID_PATH_VOL_CTL] ||
1036 path->ctls[NID_PATH_MUTE_CTL])
1037 return 0; /* already evaluated */
1038
Takashi Iwai352f7f92012-12-19 12:52:06 +01001039 nid = look_for_out_vol_nid(codec, path);
1040 if (nid) {
1041 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
1042 if (is_ctl_used(codec, val, NID_PATH_VOL_CTL))
1043 badness += BAD_SHARED_VOL;
1044 else
1045 path->ctls[NID_PATH_VOL_CTL] = val;
1046 } else
1047 badness += BAD_SHARED_VOL;
1048 nid = look_for_out_mute_nid(codec, path);
1049 if (nid) {
1050 unsigned int wid_type = get_wcaps_type(get_wcaps(codec, nid));
1051 if (wid_type == AC_WID_PIN || wid_type == AC_WID_AUD_OUT ||
1052 nid_has_mute(codec, nid, HDA_OUTPUT))
1053 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
1054 else
1055 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT);
1056 if (is_ctl_used(codec, val, NID_PATH_MUTE_CTL))
1057 badness += BAD_SHARED_VOL;
1058 else
1059 path->ctls[NID_PATH_MUTE_CTL] = val;
1060 } else
1061 badness += BAD_SHARED_VOL;
1062 return badness;
1063}
1064
Takashi Iwai98bd1112013-03-22 14:53:50 +01001065const struct badness_table hda_main_out_badness = {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001066 .no_primary_dac = BAD_NO_PRIMARY_DAC,
1067 .no_dac = BAD_NO_DAC,
1068 .shared_primary = BAD_NO_PRIMARY_DAC,
1069 .shared_surr = BAD_SHARED_SURROUND,
1070 .shared_clfe = BAD_SHARED_CLFE,
1071 .shared_surr_main = BAD_SHARED_SURROUND,
1072};
Takashi Iwai98bd1112013-03-22 14:53:50 +01001073EXPORT_SYMBOL_HDA(hda_main_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001074
Takashi Iwai98bd1112013-03-22 14:53:50 +01001075const struct badness_table hda_extra_out_badness = {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001076 .no_primary_dac = BAD_NO_DAC,
1077 .no_dac = BAD_NO_DAC,
1078 .shared_primary = BAD_NO_EXTRA_DAC,
1079 .shared_surr = BAD_SHARED_EXTRA_SURROUND,
1080 .shared_clfe = BAD_SHARED_EXTRA_SURROUND,
1081 .shared_surr_main = BAD_NO_EXTRA_SURR_DAC,
1082};
Takashi Iwai98bd1112013-03-22 14:53:50 +01001083EXPORT_SYMBOL_HDA(hda_extra_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001084
Takashi Iwai7385df62013-01-07 09:50:52 +01001085/* get the DAC of the primary output corresponding to the given array index */
1086static hda_nid_t get_primary_out(struct hda_codec *codec, int idx)
1087{
1088 struct hda_gen_spec *spec = codec->spec;
1089 struct auto_pin_cfg *cfg = &spec->autocfg;
1090
1091 if (cfg->line_outs > idx)
1092 return spec->private_dac_nids[idx];
1093 idx -= cfg->line_outs;
1094 if (spec->multi_ios > idx)
1095 return spec->multi_io[idx].dac;
1096 return 0;
1097}
1098
1099/* return the DAC if it's reachable, otherwise zero */
1100static inline hda_nid_t try_dac(struct hda_codec *codec,
1101 hda_nid_t dac, hda_nid_t pin)
1102{
1103 return is_reachable_path(codec, dac, pin) ? dac : 0;
1104}
1105
Takashi Iwai352f7f92012-12-19 12:52:06 +01001106/* try to assign DACs to pins and return the resultant badness */
1107static int try_assign_dacs(struct hda_codec *codec, int num_outs,
1108 const hda_nid_t *pins, hda_nid_t *dacs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001109 int *path_idx,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001110 const struct badness_table *bad)
1111{
1112 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001113 int i, j;
1114 int badness = 0;
1115 hda_nid_t dac;
1116
1117 if (!num_outs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 return 0;
1119
Takashi Iwai352f7f92012-12-19 12:52:06 +01001120 for (i = 0; i < num_outs; i++) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001121 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001122 hda_nid_t pin = pins[i];
Takashi Iwai1e0b5282013-01-04 12:56:52 +01001123
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001124 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
1125 if (path) {
1126 badness += assign_out_path_ctls(codec, path);
Takashi Iwai1e0b5282013-01-04 12:56:52 +01001127 continue;
1128 }
1129
1130 dacs[i] = look_for_dac(codec, pin, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001131 if (!dacs[i] && !i) {
Takashi Iwai980428c2013-01-09 09:28:20 +01001132 /* try to steal the DAC of surrounds for the front */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001133 for (j = 1; j < num_outs; j++) {
1134 if (is_reachable_path(codec, dacs[j], pin)) {
1135 dacs[0] = dacs[j];
1136 dacs[j] = 0;
Takashi Iwai980428c2013-01-09 09:28:20 +01001137 invalidate_nid_path(codec, path_idx[j]);
Takashi Iwai196c17662013-01-04 15:01:40 +01001138 path_idx[j] = 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001139 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 }
1141 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001142 }
1143 dac = dacs[i];
1144 if (!dac) {
Takashi Iwai7385df62013-01-07 09:50:52 +01001145 if (num_outs > 2)
1146 dac = try_dac(codec, get_primary_out(codec, i), pin);
1147 if (!dac)
1148 dac = try_dac(codec, dacs[0], pin);
1149 if (!dac)
1150 dac = try_dac(codec, get_primary_out(codec, i), pin);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001151 if (dac) {
1152 if (!i)
1153 badness += bad->shared_primary;
1154 else if (i == 1)
1155 badness += bad->shared_surr;
1156 else
1157 badness += bad->shared_clfe;
1158 } else if (is_reachable_path(codec, spec->private_dac_nids[0], pin)) {
1159 dac = spec->private_dac_nids[0];
1160 badness += bad->shared_surr_main;
1161 } else if (!i)
1162 badness += bad->no_primary_dac;
1163 else
1164 badness += bad->no_dac;
1165 }
Takashi Iwai1fa335b2013-01-21 11:43:19 +01001166 if (!dac)
1167 continue;
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001168 path = snd_hda_add_new_path(codec, dac, pin, -spec->mixer_nid);
Takashi Iwai117688a2013-01-04 15:41:41 +01001169 if (!path && !i && spec->mixer_nid) {
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001170 /* try with aamix */
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001171 path = snd_hda_add_new_path(codec, dac, pin, 0);
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001172 }
Takashi Iwai1fa335b2013-01-21 11:43:19 +01001173 if (!path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001174 dac = dacs[i] = 0;
Takashi Iwai1fa335b2013-01-21 11:43:19 +01001175 badness += bad->no_dac;
1176 } else {
Takashi Iwaia7694092013-01-21 10:43:18 +01001177 /* print_nid_path("output", path); */
Takashi Iwaie1284af2013-01-03 16:33:02 +01001178 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +01001179 path_idx[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001180 badness += assign_out_path_ctls(codec, path);
Takashi Iwaie1284af2013-01-03 16:33:02 +01001181 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001182 }
1183
1184 return badness;
1185}
1186
1187/* return NID if the given pin has only a single connection to a certain DAC */
1188static hda_nid_t get_dac_if_single(struct hda_codec *codec, hda_nid_t pin)
1189{
1190 struct hda_gen_spec *spec = codec->spec;
1191 int i;
1192 hda_nid_t nid_found = 0;
1193
1194 for (i = 0; i < spec->num_all_dacs; i++) {
1195 hda_nid_t nid = spec->all_dacs[i];
1196 if (!nid || is_dac_already_used(codec, nid))
1197 continue;
1198 if (is_reachable_path(codec, nid, pin)) {
1199 if (nid_found)
1200 return 0;
1201 nid_found = nid;
1202 }
1203 }
1204 return nid_found;
1205}
1206
1207/* check whether the given pin can be a multi-io pin */
1208static bool can_be_multiio_pin(struct hda_codec *codec,
1209 unsigned int location, hda_nid_t nid)
1210{
1211 unsigned int defcfg, caps;
1212
1213 defcfg = snd_hda_codec_get_pincfg(codec, nid);
1214 if (get_defcfg_connect(defcfg) != AC_JACK_PORT_COMPLEX)
1215 return false;
1216 if (location && get_defcfg_location(defcfg) != location)
1217 return false;
1218 caps = snd_hda_query_pin_caps(codec, nid);
1219 if (!(caps & AC_PINCAP_OUT))
1220 return false;
1221 return true;
1222}
1223
Takashi Iwaie22aab72013-01-04 14:50:04 +01001224/* count the number of input pins that are capable to be multi-io */
1225static int count_multiio_pins(struct hda_codec *codec, hda_nid_t reference_pin)
1226{
1227 struct hda_gen_spec *spec = codec->spec;
1228 struct auto_pin_cfg *cfg = &spec->autocfg;
1229 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1230 unsigned int location = get_defcfg_location(defcfg);
1231 int type, i;
1232 int num_pins = 0;
1233
1234 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1235 for (i = 0; i < cfg->num_inputs; i++) {
1236 if (cfg->inputs[i].type != type)
1237 continue;
1238 if (can_be_multiio_pin(codec, location,
1239 cfg->inputs[i].pin))
1240 num_pins++;
1241 }
1242 }
1243 return num_pins;
1244}
1245
Takashi Iwai352f7f92012-12-19 12:52:06 +01001246/*
1247 * multi-io helper
1248 *
1249 * When hardwired is set, try to fill ony hardwired pins, and returns
1250 * zero if any pins are filled, non-zero if nothing found.
1251 * When hardwired is off, try to fill possible input pins, and returns
1252 * the badness value.
1253 */
1254static int fill_multi_ios(struct hda_codec *codec,
1255 hda_nid_t reference_pin,
Takashi Iwaie22aab72013-01-04 14:50:04 +01001256 bool hardwired)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001257{
1258 struct hda_gen_spec *spec = codec->spec;
1259 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwaie22aab72013-01-04 14:50:04 +01001260 int type, i, j, num_pins, old_pins;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001261 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1262 unsigned int location = get_defcfg_location(defcfg);
1263 int badness = 0;
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001264 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001265
1266 old_pins = spec->multi_ios;
1267 if (old_pins >= 2)
1268 goto end_fill;
1269
Takashi Iwaie22aab72013-01-04 14:50:04 +01001270 num_pins = count_multiio_pins(codec, reference_pin);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001271 if (num_pins < 2)
1272 goto end_fill;
1273
Takashi Iwai352f7f92012-12-19 12:52:06 +01001274 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1275 for (i = 0; i < cfg->num_inputs; i++) {
1276 hda_nid_t nid = cfg->inputs[i].pin;
1277 hda_nid_t dac = 0;
1278
1279 if (cfg->inputs[i].type != type)
1280 continue;
1281 if (!can_be_multiio_pin(codec, location, nid))
1282 continue;
1283 for (j = 0; j < spec->multi_ios; j++) {
1284 if (nid == spec->multi_io[j].pin)
1285 break;
1286 }
1287 if (j < spec->multi_ios)
1288 continue;
1289
Takashi Iwai352f7f92012-12-19 12:52:06 +01001290 if (hardwired)
1291 dac = get_dac_if_single(codec, nid);
1292 else if (!dac)
1293 dac = look_for_dac(codec, nid, false);
1294 if (!dac) {
1295 badness++;
1296 continue;
1297 }
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001298 path = snd_hda_add_new_path(codec, dac, nid,
1299 -spec->mixer_nid);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001300 if (!path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001301 badness++;
1302 continue;
1303 }
Takashi Iwaia7694092013-01-21 10:43:18 +01001304 /* print_nid_path("multiio", path); */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001305 spec->multi_io[spec->multi_ios].pin = nid;
1306 spec->multi_io[spec->multi_ios].dac = dac;
Takashi Iwai196c17662013-01-04 15:01:40 +01001307 spec->out_paths[cfg->line_outs + spec->multi_ios] =
1308 snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001309 spec->multi_ios++;
1310 if (spec->multi_ios >= 2)
1311 break;
1312 }
1313 }
1314 end_fill:
1315 if (badness)
1316 badness = BAD_MULTI_IO;
1317 if (old_pins == spec->multi_ios) {
1318 if (hardwired)
1319 return 1; /* nothing found */
1320 else
1321 return badness; /* no badness if nothing found */
1322 }
1323 if (!hardwired && spec->multi_ios < 2) {
1324 /* cancel newly assigned paths */
1325 spec->paths.used -= spec->multi_ios - old_pins;
1326 spec->multi_ios = old_pins;
1327 return badness;
1328 }
1329
1330 /* assign volume and mute controls */
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001331 for (i = old_pins; i < spec->multi_ios; i++) {
1332 path = snd_hda_get_path_from_idx(codec, spec->out_paths[cfg->line_outs + i]);
1333 badness += assign_out_path_ctls(codec, path);
1334 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001335
1336 return badness;
1337}
1338
1339/* map DACs for all pins in the list if they are single connections */
1340static bool map_singles(struct hda_codec *codec, int outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001341 const hda_nid_t *pins, hda_nid_t *dacs, int *path_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001342{
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001343 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001344 int i;
1345 bool found = false;
1346 for (i = 0; i < outs; i++) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001347 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001348 hda_nid_t dac;
1349 if (dacs[i])
1350 continue;
1351 dac = get_dac_if_single(codec, pins[i]);
1352 if (!dac)
1353 continue;
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001354 path = snd_hda_add_new_path(codec, dac, pins[i],
1355 -spec->mixer_nid);
Takashi Iwai117688a2013-01-04 15:41:41 +01001356 if (!path && !i && spec->mixer_nid)
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001357 path = snd_hda_add_new_path(codec, dac, pins[i], 0);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001358 if (path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001359 dacs[i] = dac;
1360 found = true;
Takashi Iwaia7694092013-01-21 10:43:18 +01001361 /* print_nid_path("output", path); */
Takashi Iwaie1284af2013-01-03 16:33:02 +01001362 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +01001363 path_idx[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001364 }
1365 }
1366 return found;
1367}
1368
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001369/* create a new path including aamix if available, and return its index */
1370static int check_aamix_out_path(struct hda_codec *codec, int path_idx)
1371{
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001372 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001373 struct nid_path *path;
Takashi Iwaif87498b2013-01-21 14:24:31 +01001374 hda_nid_t dac, pin;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001375
1376 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001377 if (!path || !path->depth ||
1378 is_nid_contained(path, spec->mixer_nid))
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001379 return 0;
Takashi Iwaif87498b2013-01-21 14:24:31 +01001380 dac = path->path[0];
1381 pin = path->path[path->depth - 1];
1382 path = snd_hda_add_new_path(codec, dac, pin, spec->mixer_nid);
1383 if (!path) {
1384 if (dac != spec->multiout.dac_nids[0])
1385 dac = spec->multiout.dac_nids[0];
1386 else if (spec->multiout.hp_out_nid[0])
1387 dac = spec->multiout.hp_out_nid[0];
1388 else if (spec->multiout.extra_out_nid[0])
1389 dac = spec->multiout.extra_out_nid[0];
1390 if (dac)
1391 path = snd_hda_add_new_path(codec, dac, pin,
1392 spec->mixer_nid);
1393 }
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001394 if (!path)
1395 return 0;
Takashi Iwaia7694092013-01-21 10:43:18 +01001396 /* print_nid_path("output-aamix", path); */
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001397 path->active = false; /* unused as default */
1398 return snd_hda_get_path_idx(codec, path);
1399}
1400
Takashi Iwai55a63d42013-03-21 17:20:12 +01001401/* check whether the independent HP is available with the current config */
1402static bool indep_hp_possible(struct hda_codec *codec)
1403{
1404 struct hda_gen_spec *spec = codec->spec;
1405 struct auto_pin_cfg *cfg = &spec->autocfg;
1406 struct nid_path *path;
1407 int i, idx;
1408
1409 if (cfg->line_out_type == AUTO_PIN_HP_OUT)
1410 idx = spec->out_paths[0];
1411 else
1412 idx = spec->hp_paths[0];
1413 path = snd_hda_get_path_from_idx(codec, idx);
1414 if (!path)
1415 return false;
1416
1417 /* assume no path conflicts unless aamix is involved */
1418 if (!spec->mixer_nid || !is_nid_contained(path, spec->mixer_nid))
1419 return true;
1420
1421 /* check whether output paths contain aamix */
1422 for (i = 0; i < cfg->line_outs; i++) {
1423 if (spec->out_paths[i] == idx)
1424 break;
1425 path = snd_hda_get_path_from_idx(codec, spec->out_paths[i]);
1426 if (path && is_nid_contained(path, spec->mixer_nid))
1427 return false;
1428 }
1429 for (i = 0; i < cfg->speaker_outs; i++) {
1430 path = snd_hda_get_path_from_idx(codec, spec->speaker_paths[i]);
1431 if (path && is_nid_contained(path, spec->mixer_nid))
1432 return false;
1433 }
1434
1435 return true;
1436}
1437
Takashi Iwaia07a9492013-01-07 16:44:06 +01001438/* fill the empty entries in the dac array for speaker/hp with the
1439 * shared dac pointed by the paths
1440 */
1441static void refill_shared_dacs(struct hda_codec *codec, int num_outs,
1442 hda_nid_t *dacs, int *path_idx)
1443{
1444 struct nid_path *path;
1445 int i;
1446
1447 for (i = 0; i < num_outs; i++) {
1448 if (dacs[i])
1449 continue;
1450 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
1451 if (!path)
1452 continue;
1453 dacs[i] = path->path[0];
1454 }
1455}
1456
Takashi Iwai352f7f92012-12-19 12:52:06 +01001457/* fill in the dac_nids table from the parsed pin configuration */
1458static int fill_and_eval_dacs(struct hda_codec *codec,
1459 bool fill_hardwired,
1460 bool fill_mio_first)
1461{
1462 struct hda_gen_spec *spec = codec->spec;
1463 struct auto_pin_cfg *cfg = &spec->autocfg;
1464 int i, err, badness;
1465
1466 /* set num_dacs once to full for look_for_dac() */
1467 spec->multiout.num_dacs = cfg->line_outs;
1468 spec->multiout.dac_nids = spec->private_dac_nids;
1469 memset(spec->private_dac_nids, 0, sizeof(spec->private_dac_nids));
1470 memset(spec->multiout.hp_out_nid, 0, sizeof(spec->multiout.hp_out_nid));
1471 memset(spec->multiout.extra_out_nid, 0, sizeof(spec->multiout.extra_out_nid));
1472 spec->multi_ios = 0;
1473 snd_array_free(&spec->paths);
Takashi Iwaicd5be3f2013-01-07 15:07:00 +01001474
1475 /* clear path indices */
1476 memset(spec->out_paths, 0, sizeof(spec->out_paths));
1477 memset(spec->hp_paths, 0, sizeof(spec->hp_paths));
1478 memset(spec->speaker_paths, 0, sizeof(spec->speaker_paths));
1479 memset(spec->aamix_out_paths, 0, sizeof(spec->aamix_out_paths));
1480 memset(spec->digout_paths, 0, sizeof(spec->digout_paths));
Takashi Iwaic697b712013-01-07 17:09:26 +01001481 memset(spec->input_paths, 0, sizeof(spec->input_paths));
Takashi Iwaicd5be3f2013-01-07 15:07:00 +01001482 memset(spec->loopback_paths, 0, sizeof(spec->loopback_paths));
1483 memset(&spec->digin_path, 0, sizeof(spec->digin_path));
1484
Takashi Iwai352f7f92012-12-19 12:52:06 +01001485 badness = 0;
1486
1487 /* fill hard-wired DACs first */
1488 if (fill_hardwired) {
1489 bool mapped;
1490 do {
1491 mapped = map_singles(codec, cfg->line_outs,
1492 cfg->line_out_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001493 spec->private_dac_nids,
1494 spec->out_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001495 mapped |= map_singles(codec, cfg->hp_outs,
1496 cfg->hp_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001497 spec->multiout.hp_out_nid,
1498 spec->hp_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001499 mapped |= map_singles(codec, cfg->speaker_outs,
1500 cfg->speaker_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001501 spec->multiout.extra_out_nid,
1502 spec->speaker_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001503 if (fill_mio_first && cfg->line_outs == 1 &&
1504 cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
Takashi Iwaie22aab72013-01-04 14:50:04 +01001505 err = fill_multi_ios(codec, cfg->line_out_pins[0], true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001506 if (!err)
1507 mapped = true;
1508 }
1509 } while (mapped);
1510 }
1511
1512 badness += try_assign_dacs(codec, cfg->line_outs, cfg->line_out_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001513 spec->private_dac_nids, spec->out_paths,
Takashi Iwai98bd1112013-03-22 14:53:50 +01001514 spec->main_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001515
Takashi Iwai352f7f92012-12-19 12:52:06 +01001516 if (fill_mio_first &&
1517 cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1518 /* try to fill multi-io first */
Takashi Iwaie22aab72013-01-04 14:50:04 +01001519 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001520 if (err < 0)
1521 return err;
1522 /* we don't count badness at this stage yet */
1523 }
1524
1525 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
1526 err = try_assign_dacs(codec, cfg->hp_outs, cfg->hp_pins,
1527 spec->multiout.hp_out_nid,
Takashi Iwai196c17662013-01-04 15:01:40 +01001528 spec->hp_paths,
Takashi Iwai98bd1112013-03-22 14:53:50 +01001529 spec->extra_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001530 if (err < 0)
1531 return err;
1532 badness += err;
1533 }
1534 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1535 err = try_assign_dacs(codec, cfg->speaker_outs,
1536 cfg->speaker_pins,
1537 spec->multiout.extra_out_nid,
Takashi Iwai196c17662013-01-04 15:01:40 +01001538 spec->speaker_paths,
Takashi Iwai98bd1112013-03-22 14:53:50 +01001539 spec->extra_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001540 if (err < 0)
1541 return err;
1542 badness += err;
1543 }
1544 if (cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
Takashi Iwaie22aab72013-01-04 14:50:04 +01001545 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001546 if (err < 0)
1547 return err;
1548 badness += err;
1549 }
Takashi Iwaie22aab72013-01-04 14:50:04 +01001550
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001551 if (spec->mixer_nid) {
1552 spec->aamix_out_paths[0] =
1553 check_aamix_out_path(codec, spec->out_paths[0]);
1554 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1555 spec->aamix_out_paths[1] =
1556 check_aamix_out_path(codec, spec->hp_paths[0]);
1557 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1558 spec->aamix_out_paths[2] =
1559 check_aamix_out_path(codec, spec->speaker_paths[0]);
1560 }
1561
Takashi Iwaie22aab72013-01-04 14:50:04 +01001562 if (cfg->hp_outs && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
1563 if (count_multiio_pins(codec, cfg->hp_pins[0]) >= 2)
1564 spec->multi_ios = 1; /* give badness */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001565
Takashi Iwaia07a9492013-01-07 16:44:06 +01001566 /* re-count num_dacs and squash invalid entries */
1567 spec->multiout.num_dacs = 0;
1568 for (i = 0; i < cfg->line_outs; i++) {
1569 if (spec->private_dac_nids[i])
1570 spec->multiout.num_dacs++;
1571 else {
1572 memmove(spec->private_dac_nids + i,
1573 spec->private_dac_nids + i + 1,
1574 sizeof(hda_nid_t) * (cfg->line_outs - i - 1));
1575 spec->private_dac_nids[cfg->line_outs - 1] = 0;
1576 }
1577 }
1578
1579 spec->ext_channel_count = spec->min_channel_count =
David Henningssonc0f3b212013-01-16 11:45:37 +01001580 spec->multiout.num_dacs * 2;
Takashi Iwaia07a9492013-01-07 16:44:06 +01001581
Takashi Iwai352f7f92012-12-19 12:52:06 +01001582 if (spec->multi_ios == 2) {
1583 for (i = 0; i < 2; i++)
1584 spec->private_dac_nids[spec->multiout.num_dacs++] =
1585 spec->multi_io[i].dac;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001586 } else if (spec->multi_ios) {
1587 spec->multi_ios = 0;
1588 badness += BAD_MULTI_IO;
1589 }
1590
Takashi Iwai55a63d42013-03-21 17:20:12 +01001591 if (spec->indep_hp && !indep_hp_possible(codec))
1592 badness += BAD_NO_INDEP_HP;
1593
Takashi Iwaia07a9492013-01-07 16:44:06 +01001594 /* re-fill the shared DAC for speaker / headphone */
1595 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1596 refill_shared_dacs(codec, cfg->hp_outs,
1597 spec->multiout.hp_out_nid,
1598 spec->hp_paths);
1599 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1600 refill_shared_dacs(codec, cfg->speaker_outs,
1601 spec->multiout.extra_out_nid,
1602 spec->speaker_paths);
1603
Takashi Iwai352f7f92012-12-19 12:52:06 +01001604 return badness;
1605}
1606
1607#define DEBUG_BADNESS
1608
1609#ifdef DEBUG_BADNESS
1610#define debug_badness snd_printdd
1611#else
1612#define debug_badness(...)
1613#endif
1614
Takashi Iwaia7694092013-01-21 10:43:18 +01001615#ifdef DEBUG_BADNESS
1616static inline void print_nid_path_idx(struct hda_codec *codec,
1617 const char *pfx, int idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001618{
Takashi Iwaia7694092013-01-21 10:43:18 +01001619 struct nid_path *path;
1620
1621 path = snd_hda_get_path_from_idx(codec, idx);
1622 if (path)
1623 print_nid_path(pfx, path);
1624}
1625
1626static void debug_show_configs(struct hda_codec *codec,
1627 struct auto_pin_cfg *cfg)
1628{
1629 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia7694092013-01-21 10:43:18 +01001630 static const char * const lo_type[3] = { "LO", "SP", "HP" };
Takashi Iwaia7694092013-01-21 10:43:18 +01001631 int i;
1632
1633 debug_badness("multi_outs = %x/%x/%x/%x : %x/%x/%x/%x (type %s)\n",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001634 cfg->line_out_pins[0], cfg->line_out_pins[1],
Takashi Iwai708122e2012-12-20 17:56:57 +01001635 cfg->line_out_pins[2], cfg->line_out_pins[3],
Takashi Iwai352f7f92012-12-19 12:52:06 +01001636 spec->multiout.dac_nids[0],
1637 spec->multiout.dac_nids[1],
1638 spec->multiout.dac_nids[2],
Takashi Iwaia7694092013-01-21 10:43:18 +01001639 spec->multiout.dac_nids[3],
1640 lo_type[cfg->line_out_type]);
1641 for (i = 0; i < cfg->line_outs; i++)
1642 print_nid_path_idx(codec, " out", spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001643 if (spec->multi_ios > 0)
1644 debug_badness("multi_ios(%d) = %x/%x : %x/%x\n",
1645 spec->multi_ios,
1646 spec->multi_io[0].pin, spec->multi_io[1].pin,
1647 spec->multi_io[0].dac, spec->multi_io[1].dac);
Takashi Iwaia7694092013-01-21 10:43:18 +01001648 for (i = 0; i < spec->multi_ios; i++)
1649 print_nid_path_idx(codec, " mio",
1650 spec->out_paths[cfg->line_outs + i]);
1651 if (cfg->hp_outs)
1652 debug_badness("hp_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001653 cfg->hp_pins[0], cfg->hp_pins[1],
Takashi Iwai708122e2012-12-20 17:56:57 +01001654 cfg->hp_pins[2], cfg->hp_pins[3],
Takashi Iwai352f7f92012-12-19 12:52:06 +01001655 spec->multiout.hp_out_nid[0],
1656 spec->multiout.hp_out_nid[1],
1657 spec->multiout.hp_out_nid[2],
1658 spec->multiout.hp_out_nid[3]);
Takashi Iwaia7694092013-01-21 10:43:18 +01001659 for (i = 0; i < cfg->hp_outs; i++)
1660 print_nid_path_idx(codec, " hp ", spec->hp_paths[i]);
1661 if (cfg->speaker_outs)
1662 debug_badness("spk_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001663 cfg->speaker_pins[0], cfg->speaker_pins[1],
1664 cfg->speaker_pins[2], cfg->speaker_pins[3],
1665 spec->multiout.extra_out_nid[0],
1666 spec->multiout.extra_out_nid[1],
1667 spec->multiout.extra_out_nid[2],
1668 spec->multiout.extra_out_nid[3]);
Takashi Iwaia7694092013-01-21 10:43:18 +01001669 for (i = 0; i < cfg->speaker_outs; i++)
1670 print_nid_path_idx(codec, " spk", spec->speaker_paths[i]);
1671 for (i = 0; i < 3; i++)
1672 print_nid_path_idx(codec, " mix", spec->aamix_out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001673}
Takashi Iwaia7694092013-01-21 10:43:18 +01001674#else
1675#define debug_show_configs(codec, cfg) /* NOP */
1676#endif
Takashi Iwai352f7f92012-12-19 12:52:06 +01001677
1678/* find all available DACs of the codec */
1679static void fill_all_dac_nids(struct hda_codec *codec)
1680{
1681 struct hda_gen_spec *spec = codec->spec;
1682 int i;
1683 hda_nid_t nid = codec->start_nid;
1684
1685 spec->num_all_dacs = 0;
1686 memset(spec->all_dacs, 0, sizeof(spec->all_dacs));
1687 for (i = 0; i < codec->num_nodes; i++, nid++) {
1688 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_AUD_OUT)
1689 continue;
1690 if (spec->num_all_dacs >= ARRAY_SIZE(spec->all_dacs)) {
1691 snd_printk(KERN_ERR "hda: Too many DACs!\n");
1692 break;
1693 }
1694 spec->all_dacs[spec->num_all_dacs++] = nid;
1695 }
1696}
1697
1698static int parse_output_paths(struct hda_codec *codec)
1699{
1700 struct hda_gen_spec *spec = codec->spec;
1701 struct auto_pin_cfg *cfg = &spec->autocfg;
1702 struct auto_pin_cfg *best_cfg;
Takashi Iwai9314a582013-01-21 10:49:05 +01001703 unsigned int val;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001704 int best_badness = INT_MAX;
1705 int badness;
1706 bool fill_hardwired = true, fill_mio_first = true;
1707 bool best_wired = true, best_mio = true;
1708 bool hp_spk_swapped = false;
1709
Takashi Iwai352f7f92012-12-19 12:52:06 +01001710 best_cfg = kmalloc(sizeof(*best_cfg), GFP_KERNEL);
1711 if (!best_cfg)
1712 return -ENOMEM;
1713 *best_cfg = *cfg;
1714
1715 for (;;) {
1716 badness = fill_and_eval_dacs(codec, fill_hardwired,
1717 fill_mio_first);
1718 if (badness < 0) {
1719 kfree(best_cfg);
1720 return badness;
1721 }
1722 debug_badness("==> lo_type=%d, wired=%d, mio=%d, badness=0x%x\n",
1723 cfg->line_out_type, fill_hardwired, fill_mio_first,
1724 badness);
Takashi Iwaia7694092013-01-21 10:43:18 +01001725 debug_show_configs(codec, cfg);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001726 if (badness < best_badness) {
1727 best_badness = badness;
1728 *best_cfg = *cfg;
1729 best_wired = fill_hardwired;
1730 best_mio = fill_mio_first;
1731 }
1732 if (!badness)
1733 break;
1734 fill_mio_first = !fill_mio_first;
1735 if (!fill_mio_first)
1736 continue;
1737 fill_hardwired = !fill_hardwired;
1738 if (!fill_hardwired)
1739 continue;
1740 if (hp_spk_swapped)
1741 break;
1742 hp_spk_swapped = true;
1743 if (cfg->speaker_outs > 0 &&
1744 cfg->line_out_type == AUTO_PIN_HP_OUT) {
1745 cfg->hp_outs = cfg->line_outs;
1746 memcpy(cfg->hp_pins, cfg->line_out_pins,
1747 sizeof(cfg->hp_pins));
1748 cfg->line_outs = cfg->speaker_outs;
1749 memcpy(cfg->line_out_pins, cfg->speaker_pins,
1750 sizeof(cfg->speaker_pins));
1751 cfg->speaker_outs = 0;
1752 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
1753 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
1754 fill_hardwired = true;
1755 continue;
1756 }
1757 if (cfg->hp_outs > 0 &&
1758 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
1759 cfg->speaker_outs = cfg->line_outs;
1760 memcpy(cfg->speaker_pins, cfg->line_out_pins,
1761 sizeof(cfg->speaker_pins));
1762 cfg->line_outs = cfg->hp_outs;
1763 memcpy(cfg->line_out_pins, cfg->hp_pins,
1764 sizeof(cfg->hp_pins));
1765 cfg->hp_outs = 0;
1766 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
1767 cfg->line_out_type = AUTO_PIN_HP_OUT;
1768 fill_hardwired = true;
1769 continue;
1770 }
1771 break;
1772 }
1773
1774 if (badness) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001775 debug_badness("==> restoring best_cfg\n");
Takashi Iwai352f7f92012-12-19 12:52:06 +01001776 *cfg = *best_cfg;
1777 fill_and_eval_dacs(codec, best_wired, best_mio);
1778 }
1779 debug_badness("==> Best config: lo_type=%d, wired=%d, mio=%d\n",
1780 cfg->line_out_type, best_wired, best_mio);
Takashi Iwaia7694092013-01-21 10:43:18 +01001781 debug_show_configs(codec, cfg);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001782
1783 if (cfg->line_out_pins[0]) {
1784 struct nid_path *path;
Takashi Iwai196c17662013-01-04 15:01:40 +01001785 path = snd_hda_get_path_from_idx(codec, spec->out_paths[0]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001786 if (path)
1787 spec->vmaster_nid = look_for_out_vol_nid(codec, path);
Takashi Iwai7a71bbf2013-01-17 10:25:15 +01001788 if (spec->vmaster_nid)
1789 snd_hda_set_vmaster_tlv(codec, spec->vmaster_nid,
1790 HDA_OUTPUT, spec->vmaster_tlv);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001791 }
1792
Takashi Iwai9314a582013-01-21 10:49:05 +01001793 /* set initial pinctl targets */
1794 if (spec->prefer_hp_amp || cfg->line_out_type == AUTO_PIN_HP_OUT)
1795 val = PIN_HP;
1796 else
1797 val = PIN_OUT;
1798 set_pin_targets(codec, cfg->line_outs, cfg->line_out_pins, val);
1799 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1800 set_pin_targets(codec, cfg->hp_outs, cfg->hp_pins, PIN_HP);
1801 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1802 val = spec->prefer_hp_amp ? PIN_HP : PIN_OUT;
1803 set_pin_targets(codec, cfg->speaker_outs,
1804 cfg->speaker_pins, val);
1805 }
1806
Takashi Iwai55a63d42013-03-21 17:20:12 +01001807 /* clear indep_hp flag if not available */
1808 if (spec->indep_hp && !indep_hp_possible(codec))
1809 spec->indep_hp = 0;
1810
Takashi Iwai352f7f92012-12-19 12:52:06 +01001811 kfree(best_cfg);
1812 return 0;
1813}
1814
1815/* add playback controls from the parsed DAC table */
1816static int create_multi_out_ctls(struct hda_codec *codec,
1817 const struct auto_pin_cfg *cfg)
1818{
1819 struct hda_gen_spec *spec = codec->spec;
1820 int i, err, noutputs;
1821
1822 noutputs = cfg->line_outs;
1823 if (spec->multi_ios > 0 && cfg->line_outs < 3)
1824 noutputs += spec->multi_ios;
1825
1826 for (i = 0; i < noutputs; i++) {
1827 const char *name;
1828 int index;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001829 struct nid_path *path;
1830
Takashi Iwai196c17662013-01-04 15:01:40 +01001831 path = snd_hda_get_path_from_idx(codec, spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001832 if (!path)
1833 continue;
Takashi Iwai247d85e2013-01-17 16:18:11 +01001834
1835 name = get_line_out_pfx(codec, i, &index, NID_PATH_VOL_CTL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001836 if (!name || !strcmp(name, "CLFE")) {
1837 /* Center/LFE */
1838 err = add_vol_ctl(codec, "Center", 0, 1, path);
1839 if (err < 0)
1840 return err;
1841 err = add_vol_ctl(codec, "LFE", 0, 2, path);
1842 if (err < 0)
1843 return err;
Takashi Iwai247d85e2013-01-17 16:18:11 +01001844 } else {
1845 err = add_stereo_vol(codec, name, index, path);
1846 if (err < 0)
1847 return err;
1848 }
1849
1850 name = get_line_out_pfx(codec, i, &index, NID_PATH_MUTE_CTL);
1851 if (!name || !strcmp(name, "CLFE")) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001852 err = add_sw_ctl(codec, "Center", 0, 1, path);
1853 if (err < 0)
1854 return err;
1855 err = add_sw_ctl(codec, "LFE", 0, 2, path);
1856 if (err < 0)
1857 return err;
1858 } else {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001859 err = add_stereo_sw(codec, name, index, path);
1860 if (err < 0)
1861 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 }
1863 }
1864 return 0;
1865}
1866
Takashi Iwaic2c80382013-01-07 10:33:57 +01001867static int create_extra_out(struct hda_codec *codec, int path_idx,
Takashi Iwai196c17662013-01-04 15:01:40 +01001868 const char *pfx, int cidx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869{
Takashi Iwai352f7f92012-12-19 12:52:06 +01001870 struct nid_path *path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 int err;
1872
Takashi Iwai196c17662013-01-04 15:01:40 +01001873 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001874 if (!path)
1875 return 0;
Takashi Iwaic2c80382013-01-07 10:33:57 +01001876 err = add_stereo_vol(codec, pfx, cidx, path);
1877 if (err < 0)
1878 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001879 err = add_stereo_sw(codec, pfx, cidx, path);
1880 if (err < 0)
1881 return err;
1882 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883}
1884
Takashi Iwai352f7f92012-12-19 12:52:06 +01001885/* add playback controls for speaker and HP outputs */
1886static int create_extra_outs(struct hda_codec *codec, int num_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001887 const int *paths, const char *pfx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888{
Takashi Iwaic2c80382013-01-07 10:33:57 +01001889 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001890
1891 for (i = 0; i < num_pins; i++) {
Takashi Iwaic2c80382013-01-07 10:33:57 +01001892 const char *name;
1893 char tmp[44];
1894 int err, idx = 0;
1895
1896 if (num_pins == 2 && i == 1 && !strcmp(pfx, "Speaker"))
1897 name = "Bass Speaker";
1898 else if (num_pins >= 3) {
1899 snprintf(tmp, sizeof(tmp), "%s %s",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001900 pfx, channel_name[i]);
Takashi Iwaic2c80382013-01-07 10:33:57 +01001901 name = tmp;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001902 } else {
Takashi Iwaic2c80382013-01-07 10:33:57 +01001903 name = pfx;
1904 idx = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 }
Takashi Iwaic2c80382013-01-07 10:33:57 +01001906 err = create_extra_out(codec, paths[i], name, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001907 if (err < 0)
1908 return err;
1909 }
1910 return 0;
1911}
Takashi Iwai97ec5582006-03-21 11:29:07 +01001912
Takashi Iwai352f7f92012-12-19 12:52:06 +01001913static int create_hp_out_ctls(struct hda_codec *codec)
1914{
1915 struct hda_gen_spec *spec = codec->spec;
1916 return create_extra_outs(codec, spec->autocfg.hp_outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001917 spec->hp_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001918 "Headphone");
1919}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920
Takashi Iwai352f7f92012-12-19 12:52:06 +01001921static int create_speaker_out_ctls(struct hda_codec *codec)
1922{
1923 struct hda_gen_spec *spec = codec->spec;
1924 return create_extra_outs(codec, spec->autocfg.speaker_outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001925 spec->speaker_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001926 "Speaker");
1927}
1928
1929/*
Takashi Iwai38cf6f12012-12-21 14:09:42 +01001930 * independent HP controls
1931 */
1932
Takashi Iwai8ba955c2013-03-07 18:40:58 +01001933/* update HP auto-mute state too */
1934static void update_hp_automute_hook(struct hda_codec *codec)
1935{
1936 struct hda_gen_spec *spec = codec->spec;
1937
1938 if (spec->hp_automute_hook)
1939 spec->hp_automute_hook(codec, NULL);
1940 else
1941 snd_hda_gen_hp_automute(codec, NULL);
1942}
1943
Takashi Iwai38cf6f12012-12-21 14:09:42 +01001944static int indep_hp_info(struct snd_kcontrol *kcontrol,
1945 struct snd_ctl_elem_info *uinfo)
1946{
1947 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
1948}
1949
1950static int indep_hp_get(struct snd_kcontrol *kcontrol,
1951 struct snd_ctl_elem_value *ucontrol)
1952{
1953 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1954 struct hda_gen_spec *spec = codec->spec;
1955 ucontrol->value.enumerated.item[0] = spec->indep_hp_enabled;
1956 return 0;
1957}
1958
Takashi Iwaia1e908e2013-01-21 15:11:25 +01001959static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
1960 int nomix_path_idx, int mix_path_idx,
1961 int out_type);
1962
Takashi Iwai38cf6f12012-12-21 14:09:42 +01001963static int indep_hp_put(struct snd_kcontrol *kcontrol,
1964 struct snd_ctl_elem_value *ucontrol)
1965{
1966 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1967 struct hda_gen_spec *spec = codec->spec;
1968 unsigned int select = ucontrol->value.enumerated.item[0];
1969 int ret = 0;
1970
1971 mutex_lock(&spec->pcm_mutex);
1972 if (spec->active_streams) {
1973 ret = -EBUSY;
1974 goto unlock;
1975 }
1976
1977 if (spec->indep_hp_enabled != select) {
Takashi Iwaia1e908e2013-01-21 15:11:25 +01001978 hda_nid_t *dacp;
1979 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
1980 dacp = &spec->private_dac_nids[0];
1981 else
1982 dacp = &spec->multiout.hp_out_nid[0];
1983
1984 /* update HP aamix paths in case it conflicts with indep HP */
1985 if (spec->have_aamix_ctl) {
1986 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
1987 update_aamix_paths(codec, spec->aamix_mode,
1988 spec->out_paths[0],
1989 spec->aamix_out_paths[0],
1990 spec->autocfg.line_out_type);
1991 else
1992 update_aamix_paths(codec, spec->aamix_mode,
1993 spec->hp_paths[0],
1994 spec->aamix_out_paths[1],
1995 AUTO_PIN_HP_OUT);
1996 }
1997
Takashi Iwai38cf6f12012-12-21 14:09:42 +01001998 spec->indep_hp_enabled = select;
1999 if (spec->indep_hp_enabled)
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002000 *dacp = 0;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002001 else
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002002 *dacp = spec->alt_dac_nid;
Takashi Iwai92603c52013-01-22 07:46:31 +01002003
Takashi Iwai8ba955c2013-03-07 18:40:58 +01002004 update_hp_automute_hook(codec);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002005 ret = 1;
2006 }
2007 unlock:
2008 mutex_unlock(&spec->pcm_mutex);
2009 return ret;
2010}
2011
2012static const struct snd_kcontrol_new indep_hp_ctl = {
2013 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2014 .name = "Independent HP",
2015 .info = indep_hp_info,
2016 .get = indep_hp_get,
2017 .put = indep_hp_put,
2018};
2019
2020
2021static int create_indep_hp_ctls(struct hda_codec *codec)
2022{
2023 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002024 hda_nid_t dac;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002025
2026 if (!spec->indep_hp)
2027 return 0;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002028 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
2029 dac = spec->multiout.dac_nids[0];
2030 else
2031 dac = spec->multiout.hp_out_nid[0];
2032 if (!dac) {
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002033 spec->indep_hp = 0;
2034 return 0;
2035 }
2036
2037 spec->indep_hp_enabled = false;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002038 spec->alt_dac_nid = dac;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002039 if (!snd_hda_gen_add_kctl(spec, NULL, &indep_hp_ctl))
2040 return -ENOMEM;
2041 return 0;
2042}
2043
2044/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01002045 * channel mode enum control
2046 */
2047
2048static int ch_mode_info(struct snd_kcontrol *kcontrol,
2049 struct snd_ctl_elem_info *uinfo)
2050{
2051 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2052 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002053 int chs;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002054
2055 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2056 uinfo->count = 1;
2057 uinfo->value.enumerated.items = spec->multi_ios + 1;
2058 if (uinfo->value.enumerated.item > spec->multi_ios)
2059 uinfo->value.enumerated.item = spec->multi_ios;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002060 chs = uinfo->value.enumerated.item * 2 + spec->min_channel_count;
2061 sprintf(uinfo->value.enumerated.name, "%dch", chs);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002062 return 0;
2063}
2064
2065static int ch_mode_get(struct snd_kcontrol *kcontrol,
2066 struct snd_ctl_elem_value *ucontrol)
2067{
2068 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2069 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002070 ucontrol->value.enumerated.item[0] =
2071 (spec->ext_channel_count - spec->min_channel_count) / 2;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002072 return 0;
2073}
2074
Takashi Iwai196c17662013-01-04 15:01:40 +01002075static inline struct nid_path *
2076get_multiio_path(struct hda_codec *codec, int idx)
2077{
2078 struct hda_gen_spec *spec = codec->spec;
2079 return snd_hda_get_path_from_idx(codec,
2080 spec->out_paths[spec->autocfg.line_outs + idx]);
2081}
2082
Takashi Iwaia5cc2502013-01-16 18:08:55 +01002083static void update_automute_all(struct hda_codec *codec);
2084
Takashi Iwai352f7f92012-12-19 12:52:06 +01002085static int set_multi_io(struct hda_codec *codec, int idx, bool output)
2086{
2087 struct hda_gen_spec *spec = codec->spec;
2088 hda_nid_t nid = spec->multi_io[idx].pin;
2089 struct nid_path *path;
2090
Takashi Iwai196c17662013-01-04 15:01:40 +01002091 path = get_multiio_path(codec, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002092 if (!path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 return -EINVAL;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002094
2095 if (path->active == output)
2096 return 0;
2097
2098 if (output) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01002099 set_pin_target(codec, nid, PIN_OUT, true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002100 snd_hda_activate_path(codec, path, true, true);
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01002101 set_pin_eapd(codec, nid, true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002102 } else {
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01002103 set_pin_eapd(codec, nid, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002104 snd_hda_activate_path(codec, path, false, true);
Takashi Iwai2c12c302013-01-10 09:33:29 +01002105 set_pin_target(codec, nid, spec->multi_io[idx].ctl_in, true);
Takashi Iwai55196ff2013-01-24 17:32:56 +01002106 path_power_down_sync(codec, path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 }
Takashi Iwaia365fed2013-01-10 16:10:06 +01002108
2109 /* update jack retasking in case it modifies any of them */
Takashi Iwaia5cc2502013-01-16 18:08:55 +01002110 update_automute_all(codec);
Takashi Iwaia365fed2013-01-10 16:10:06 +01002111
Takashi Iwai352f7f92012-12-19 12:52:06 +01002112 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113}
2114
Takashi Iwai352f7f92012-12-19 12:52:06 +01002115static int ch_mode_put(struct snd_kcontrol *kcontrol,
2116 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002118 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2119 struct hda_gen_spec *spec = codec->spec;
2120 int i, ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121
Takashi Iwai352f7f92012-12-19 12:52:06 +01002122 ch = ucontrol->value.enumerated.item[0];
2123 if (ch < 0 || ch > spec->multi_ios)
2124 return -EINVAL;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002125 if (ch == (spec->ext_channel_count - spec->min_channel_count) / 2)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002126 return 0;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002127 spec->ext_channel_count = ch * 2 + spec->min_channel_count;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002128 for (i = 0; i < spec->multi_ios; i++)
2129 set_multi_io(codec, i, i < ch);
2130 spec->multiout.max_channels = max(spec->ext_channel_count,
2131 spec->const_channel_count);
2132 if (spec->need_dac_fix)
2133 spec->multiout.num_dacs = spec->multiout.max_channels / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 return 1;
2135}
2136
Takashi Iwai352f7f92012-12-19 12:52:06 +01002137static const struct snd_kcontrol_new channel_mode_enum = {
2138 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2139 .name = "Channel Mode",
2140 .info = ch_mode_info,
2141 .get = ch_mode_get,
2142 .put = ch_mode_put,
2143};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144
Takashi Iwai352f7f92012-12-19 12:52:06 +01002145static int create_multi_channel_mode(struct hda_codec *codec)
2146{
2147 struct hda_gen_spec *spec = codec->spec;
2148
2149 if (spec->multi_ios > 0) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01002150 if (!snd_hda_gen_add_kctl(spec, NULL, &channel_mode_enum))
Takashi Iwai352f7f92012-12-19 12:52:06 +01002151 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 return 0;
2154}
2155
Takashi Iwai352f7f92012-12-19 12:52:06 +01002156/*
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002157 * aamix loopback enable/disable switch
2158 */
2159
2160#define loopback_mixing_info indep_hp_info
2161
2162static int loopback_mixing_get(struct snd_kcontrol *kcontrol,
2163 struct snd_ctl_elem_value *ucontrol)
2164{
2165 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2166 struct hda_gen_spec *spec = codec->spec;
2167 ucontrol->value.enumerated.item[0] = spec->aamix_mode;
2168 return 0;
2169}
2170
2171static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002172 int nomix_path_idx, int mix_path_idx,
2173 int out_type)
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002174{
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002175 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002176 struct nid_path *nomix_path, *mix_path;
2177
2178 nomix_path = snd_hda_get_path_from_idx(codec, nomix_path_idx);
2179 mix_path = snd_hda_get_path_from_idx(codec, mix_path_idx);
2180 if (!nomix_path || !mix_path)
2181 return;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002182
2183 /* if HP aamix path is driven from a different DAC and the
2184 * independent HP mode is ON, can't turn on aamix path
2185 */
2186 if (out_type == AUTO_PIN_HP_OUT && spec->indep_hp_enabled &&
2187 mix_path->path[0] != spec->alt_dac_nid)
2188 do_mix = false;
2189
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002190 if (do_mix) {
2191 snd_hda_activate_path(codec, nomix_path, false, true);
2192 snd_hda_activate_path(codec, mix_path, true, true);
Takashi Iwai55196ff2013-01-24 17:32:56 +01002193 path_power_down_sync(codec, nomix_path);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002194 } else {
2195 snd_hda_activate_path(codec, mix_path, false, true);
2196 snd_hda_activate_path(codec, nomix_path, true, true);
Takashi Iwai55196ff2013-01-24 17:32:56 +01002197 path_power_down_sync(codec, mix_path);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002198 }
2199}
2200
2201static int loopback_mixing_put(struct snd_kcontrol *kcontrol,
2202 struct snd_ctl_elem_value *ucontrol)
2203{
2204 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2205 struct hda_gen_spec *spec = codec->spec;
2206 unsigned int val = ucontrol->value.enumerated.item[0];
2207
2208 if (val == spec->aamix_mode)
2209 return 0;
2210 spec->aamix_mode = val;
2211 update_aamix_paths(codec, val, spec->out_paths[0],
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002212 spec->aamix_out_paths[0],
2213 spec->autocfg.line_out_type);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002214 update_aamix_paths(codec, val, spec->hp_paths[0],
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002215 spec->aamix_out_paths[1],
2216 AUTO_PIN_HP_OUT);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002217 update_aamix_paths(codec, val, spec->speaker_paths[0],
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002218 spec->aamix_out_paths[2],
2219 AUTO_PIN_SPEAKER_OUT);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002220 return 1;
2221}
2222
2223static const struct snd_kcontrol_new loopback_mixing_enum = {
2224 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2225 .name = "Loopback Mixing",
2226 .info = loopback_mixing_info,
2227 .get = loopback_mixing_get,
2228 .put = loopback_mixing_put,
2229};
2230
2231static int create_loopback_mixing_ctl(struct hda_codec *codec)
2232{
2233 struct hda_gen_spec *spec = codec->spec;
2234
2235 if (!spec->mixer_nid)
2236 return 0;
2237 if (!(spec->aamix_out_paths[0] || spec->aamix_out_paths[1] ||
2238 spec->aamix_out_paths[2]))
2239 return 0;
2240 if (!snd_hda_gen_add_kctl(spec, NULL, &loopback_mixing_enum))
2241 return -ENOMEM;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002242 spec->have_aamix_ctl = 1;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002243 return 0;
2244}
2245
2246/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01002247 * shared headphone/mic handling
2248 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02002249
Takashi Iwai352f7f92012-12-19 12:52:06 +01002250static void call_update_outputs(struct hda_codec *codec);
2251
2252/* for shared I/O, change the pin-control accordingly */
Takashi Iwai967303d2013-02-19 17:12:42 +01002253static void update_hp_mic(struct hda_codec *codec, int adc_mux, bool force)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002254{
2255 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai967303d2013-02-19 17:12:42 +01002256 bool as_mic;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002257 unsigned int val;
Takashi Iwai967303d2013-02-19 17:12:42 +01002258 hda_nid_t pin;
2259
2260 pin = spec->hp_mic_pin;
2261 as_mic = spec->cur_mux[adc_mux] == spec->hp_mic_mux_idx;
2262
2263 if (!force) {
2264 val = snd_hda_codec_get_pin_target(codec, pin);
2265 if (as_mic) {
2266 if (val & PIN_IN)
2267 return;
2268 } else {
2269 if (val & PIN_OUT)
2270 return;
2271 }
2272 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002273
2274 val = snd_hda_get_default_vref(codec, pin);
Takashi Iwai967303d2013-02-19 17:12:42 +01002275 /* if the HP pin doesn't support VREF and the codec driver gives an
2276 * alternative pin, set up the VREF on that pin instead
2277 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01002278 if (val == AC_PINCTL_VREF_HIZ && spec->shared_mic_vref_pin) {
2279 const hda_nid_t vref_pin = spec->shared_mic_vref_pin;
2280 unsigned int vref_val = snd_hda_get_default_vref(codec, vref_pin);
2281 if (vref_val != AC_PINCTL_VREF_HIZ)
Takashi Iwai7594aa32012-12-20 15:38:40 +01002282 snd_hda_set_pin_ctl_cache(codec, vref_pin,
Takashi Iwai967303d2013-02-19 17:12:42 +01002283 PIN_IN | (as_mic ? vref_val : 0));
Takashi Iwaicb53c622007-08-10 17:21:45 +02002284 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002285
Takashi Iwai8ba955c2013-03-07 18:40:58 +01002286 if (!spec->hp_mic_jack_modes) {
2287 if (as_mic)
2288 val |= PIN_IN;
2289 else
2290 val = PIN_HP;
2291 set_pin_target(codec, pin, val, true);
2292 update_hp_automute_hook(codec);
2293 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002294}
2295
2296/* create a shared input with the headphone out */
Takashi Iwai967303d2013-02-19 17:12:42 +01002297static int create_hp_mic(struct hda_codec *codec)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002298{
2299 struct hda_gen_spec *spec = codec->spec;
2300 struct auto_pin_cfg *cfg = &spec->autocfg;
2301 unsigned int defcfg;
2302 hda_nid_t nid;
2303
Takashi Iwai967303d2013-02-19 17:12:42 +01002304 if (!spec->hp_mic) {
2305 if (spec->suppress_hp_mic_detect)
2306 return 0;
2307 /* automatic detection: only if no input or a single internal
2308 * input pin is found, try to detect the shared hp/mic
2309 */
2310 if (cfg->num_inputs > 1)
2311 return 0;
2312 else if (cfg->num_inputs == 1) {
2313 defcfg = snd_hda_codec_get_pincfg(codec, cfg->inputs[0].pin);
2314 if (snd_hda_get_input_pin_attr(defcfg) != INPUT_PIN_ATTR_INT)
2315 return 0;
2316 }
2317 }
2318
2319 spec->hp_mic = 0; /* clear once */
2320 if (cfg->num_inputs >= AUTO_CFG_MAX_INS)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002321 return 0;
2322
Takashi Iwai967303d2013-02-19 17:12:42 +01002323 nid = 0;
2324 if (cfg->line_out_type == AUTO_PIN_HP_OUT && cfg->line_outs > 0)
2325 nid = cfg->line_out_pins[0];
2326 else if (cfg->hp_outs > 0)
2327 nid = cfg->hp_pins[0];
2328 if (!nid)
2329 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002330
2331 if (!(snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_IN))
2332 return 0; /* no input */
2333
Takashi Iwai967303d2013-02-19 17:12:42 +01002334 cfg->inputs[cfg->num_inputs].pin = nid;
2335 cfg->inputs[cfg->num_inputs].type = AUTO_PIN_MIC;
2336 cfg->num_inputs++;
2337 spec->hp_mic = 1;
2338 spec->hp_mic_pin = nid;
2339 /* we can't handle auto-mic together with HP-mic */
2340 spec->suppress_auto_mic = 1;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002341 snd_printdd("hda-codec: Enable shared I/O jack on NID 0x%x\n", nid);
2342 return 0;
2343}
2344
Takashi Iwai978e77e2013-01-10 16:57:58 +01002345/*
2346 * output jack mode
2347 */
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002348
2349static int create_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t pin);
2350
2351static const char * const out_jack_texts[] = {
2352 "Line Out", "Headphone Out",
2353};
2354
Takashi Iwai978e77e2013-01-10 16:57:58 +01002355static int out_jack_mode_info(struct snd_kcontrol *kcontrol,
2356 struct snd_ctl_elem_info *uinfo)
2357{
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002358 return snd_hda_enum_helper_info(kcontrol, uinfo, 2, out_jack_texts);
Takashi Iwai978e77e2013-01-10 16:57:58 +01002359}
2360
2361static int out_jack_mode_get(struct snd_kcontrol *kcontrol,
2362 struct snd_ctl_elem_value *ucontrol)
2363{
2364 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2365 hda_nid_t nid = kcontrol->private_value;
2366 if (snd_hda_codec_get_pin_target(codec, nid) == PIN_HP)
2367 ucontrol->value.enumerated.item[0] = 1;
2368 else
2369 ucontrol->value.enumerated.item[0] = 0;
2370 return 0;
2371}
2372
2373static int out_jack_mode_put(struct snd_kcontrol *kcontrol,
2374 struct snd_ctl_elem_value *ucontrol)
2375{
2376 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2377 hda_nid_t nid = kcontrol->private_value;
2378 unsigned int val;
2379
2380 val = ucontrol->value.enumerated.item[0] ? PIN_HP : PIN_OUT;
2381 if (snd_hda_codec_get_pin_target(codec, nid) == val)
2382 return 0;
2383 snd_hda_set_pin_ctl_cache(codec, nid, val);
2384 return 1;
2385}
2386
2387static const struct snd_kcontrol_new out_jack_mode_enum = {
2388 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2389 .info = out_jack_mode_info,
2390 .get = out_jack_mode_get,
2391 .put = out_jack_mode_put,
2392};
2393
2394static bool find_kctl_name(struct hda_codec *codec, const char *name, int idx)
2395{
2396 struct hda_gen_spec *spec = codec->spec;
2397 int i;
2398
2399 for (i = 0; i < spec->kctls.used; i++) {
2400 struct snd_kcontrol_new *kctl = snd_array_elem(&spec->kctls, i);
2401 if (!strcmp(kctl->name, name) && kctl->index == idx)
2402 return true;
2403 }
2404 return false;
2405}
2406
2407static void get_jack_mode_name(struct hda_codec *codec, hda_nid_t pin,
2408 char *name, size_t name_len)
2409{
2410 struct hda_gen_spec *spec = codec->spec;
2411 int idx = 0;
2412
2413 snd_hda_get_pin_label(codec, pin, &spec->autocfg, name, name_len, &idx);
2414 strlcat(name, " Jack Mode", name_len);
2415
2416 for (; find_kctl_name(codec, name, idx); idx++)
2417 ;
2418}
2419
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002420static int get_out_jack_num_items(struct hda_codec *codec, hda_nid_t pin)
2421{
2422 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaif811c3c2013-03-07 18:32:59 +01002423 if (spec->add_jack_modes) {
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002424 unsigned int pincap = snd_hda_query_pin_caps(codec, pin);
2425 if ((pincap & AC_PINCAP_OUT) && (pincap & AC_PINCAP_HP_DRV))
2426 return 2;
2427 }
2428 return 1;
2429}
2430
Takashi Iwai978e77e2013-01-10 16:57:58 +01002431static int create_out_jack_modes(struct hda_codec *codec, int num_pins,
2432 hda_nid_t *pins)
2433{
2434 struct hda_gen_spec *spec = codec->spec;
2435 int i;
2436
2437 for (i = 0; i < num_pins; i++) {
2438 hda_nid_t pin = pins[i];
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002439 if (pin == spec->hp_mic_pin) {
2440 int ret = create_hp_mic_jack_mode(codec, pin);
2441 if (ret < 0)
2442 return ret;
2443 continue;
2444 }
2445 if (get_out_jack_num_items(codec, pin) > 1) {
Takashi Iwai978e77e2013-01-10 16:57:58 +01002446 struct snd_kcontrol_new *knew;
2447 char name[44];
2448 get_jack_mode_name(codec, pin, name, sizeof(name));
2449 knew = snd_hda_gen_add_kctl(spec, name,
2450 &out_jack_mode_enum);
2451 if (!knew)
2452 return -ENOMEM;
2453 knew->private_value = pin;
2454 }
2455 }
2456
2457 return 0;
2458}
2459
Takashi Iwai294765582013-01-17 09:52:11 +01002460/*
2461 * input jack mode
2462 */
2463
2464/* from AC_PINCTL_VREF_HIZ to AC_PINCTL_VREF_100 */
2465#define NUM_VREFS 6
2466
2467static const char * const vref_texts[NUM_VREFS] = {
2468 "Line In", "Mic 50pc Bias", "Mic 0V Bias",
2469 "", "Mic 80pc Bias", "Mic 100pc Bias"
2470};
2471
2472static unsigned int get_vref_caps(struct hda_codec *codec, hda_nid_t pin)
2473{
2474 unsigned int pincap;
2475
2476 pincap = snd_hda_query_pin_caps(codec, pin);
2477 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
2478 /* filter out unusual vrefs */
2479 pincap &= ~(AC_PINCAP_VREF_GRD | AC_PINCAP_VREF_100);
2480 return pincap;
2481}
2482
2483/* convert from the enum item index to the vref ctl index (0=HIZ, 1=50%...) */
2484static int get_vref_idx(unsigned int vref_caps, unsigned int item_idx)
2485{
2486 unsigned int i, n = 0;
2487
2488 for (i = 0; i < NUM_VREFS; i++) {
2489 if (vref_caps & (1 << i)) {
2490 if (n == item_idx)
2491 return i;
2492 n++;
2493 }
2494 }
2495 return 0;
2496}
2497
2498/* convert back from the vref ctl index to the enum item index */
2499static int cvt_from_vref_idx(unsigned int vref_caps, unsigned int idx)
2500{
2501 unsigned int i, n = 0;
2502
2503 for (i = 0; i < NUM_VREFS; i++) {
2504 if (i == idx)
2505 return n;
2506 if (vref_caps & (1 << i))
2507 n++;
2508 }
2509 return 0;
2510}
2511
2512static int in_jack_mode_info(struct snd_kcontrol *kcontrol,
2513 struct snd_ctl_elem_info *uinfo)
2514{
2515 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2516 hda_nid_t nid = kcontrol->private_value;
2517 unsigned int vref_caps = get_vref_caps(codec, nid);
2518
2519 snd_hda_enum_helper_info(kcontrol, uinfo, hweight32(vref_caps),
2520 vref_texts);
2521 /* set the right text */
2522 strcpy(uinfo->value.enumerated.name,
2523 vref_texts[get_vref_idx(vref_caps, uinfo->value.enumerated.item)]);
2524 return 0;
2525}
2526
2527static int in_jack_mode_get(struct snd_kcontrol *kcontrol,
2528 struct snd_ctl_elem_value *ucontrol)
2529{
2530 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2531 hda_nid_t nid = kcontrol->private_value;
2532 unsigned int vref_caps = get_vref_caps(codec, nid);
2533 unsigned int idx;
2534
2535 idx = snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_VREFEN;
2536 ucontrol->value.enumerated.item[0] = cvt_from_vref_idx(vref_caps, idx);
2537 return 0;
2538}
2539
2540static int in_jack_mode_put(struct snd_kcontrol *kcontrol,
2541 struct snd_ctl_elem_value *ucontrol)
2542{
2543 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2544 hda_nid_t nid = kcontrol->private_value;
2545 unsigned int vref_caps = get_vref_caps(codec, nid);
2546 unsigned int val, idx;
2547
2548 val = snd_hda_codec_get_pin_target(codec, nid);
2549 idx = cvt_from_vref_idx(vref_caps, val & AC_PINCTL_VREFEN);
2550 if (idx == ucontrol->value.enumerated.item[0])
2551 return 0;
2552
2553 val &= ~AC_PINCTL_VREFEN;
2554 val |= get_vref_idx(vref_caps, ucontrol->value.enumerated.item[0]);
2555 snd_hda_set_pin_ctl_cache(codec, nid, val);
2556 return 1;
2557}
2558
2559static const struct snd_kcontrol_new in_jack_mode_enum = {
2560 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2561 .info = in_jack_mode_info,
2562 .get = in_jack_mode_get,
2563 .put = in_jack_mode_put,
2564};
2565
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002566static int get_in_jack_num_items(struct hda_codec *codec, hda_nid_t pin)
2567{
2568 struct hda_gen_spec *spec = codec->spec;
2569 int nitems = 0;
Takashi Iwaif811c3c2013-03-07 18:32:59 +01002570 if (spec->add_jack_modes)
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002571 nitems = hweight32(get_vref_caps(codec, pin));
2572 return nitems ? nitems : 1;
2573}
2574
Takashi Iwai294765582013-01-17 09:52:11 +01002575static int create_in_jack_mode(struct hda_codec *codec, hda_nid_t pin)
2576{
2577 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai294765582013-01-17 09:52:11 +01002578 struct snd_kcontrol_new *knew;
2579 char name[44];
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002580 unsigned int defcfg;
2581
Takashi Iwaif811c3c2013-03-07 18:32:59 +01002582 if (pin == spec->hp_mic_pin)
2583 return 0; /* already done in create_out_jack_mode() */
Takashi Iwai294765582013-01-17 09:52:11 +01002584
2585 /* no jack mode for fixed pins */
2586 defcfg = snd_hda_codec_get_pincfg(codec, pin);
2587 if (snd_hda_get_input_pin_attr(defcfg) == INPUT_PIN_ATTR_INT)
2588 return 0;
2589
2590 /* no multiple vref caps? */
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002591 if (get_in_jack_num_items(codec, pin) <= 1)
Takashi Iwai294765582013-01-17 09:52:11 +01002592 return 0;
2593
2594 get_jack_mode_name(codec, pin, name, sizeof(name));
2595 knew = snd_hda_gen_add_kctl(spec, name, &in_jack_mode_enum);
2596 if (!knew)
2597 return -ENOMEM;
2598 knew->private_value = pin;
2599 return 0;
2600}
2601
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002602/*
2603 * HP/mic shared jack mode
2604 */
2605static int hp_mic_jack_mode_info(struct snd_kcontrol *kcontrol,
2606 struct snd_ctl_elem_info *uinfo)
2607{
2608 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2609 hda_nid_t nid = kcontrol->private_value;
2610 int out_jacks = get_out_jack_num_items(codec, nid);
2611 int in_jacks = get_in_jack_num_items(codec, nid);
2612 const char *text = NULL;
2613 int idx;
2614
2615 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2616 uinfo->count = 1;
2617 uinfo->value.enumerated.items = out_jacks + in_jacks;
2618 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2619 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
2620 idx = uinfo->value.enumerated.item;
2621 if (idx < out_jacks) {
2622 if (out_jacks > 1)
2623 text = out_jack_texts[idx];
2624 else
2625 text = "Headphone Out";
2626 } else {
2627 idx -= out_jacks;
2628 if (in_jacks > 1) {
2629 unsigned int vref_caps = get_vref_caps(codec, nid);
2630 text = vref_texts[get_vref_idx(vref_caps, idx)];
2631 } else
2632 text = "Mic In";
2633 }
2634
2635 strcpy(uinfo->value.enumerated.name, text);
2636 return 0;
2637}
2638
2639static int get_cur_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t nid)
2640{
2641 int out_jacks = get_out_jack_num_items(codec, nid);
2642 int in_jacks = get_in_jack_num_items(codec, nid);
2643 unsigned int val = snd_hda_codec_get_pin_target(codec, nid);
2644 int idx = 0;
2645
2646 if (val & PIN_OUT) {
2647 if (out_jacks > 1 && val == PIN_HP)
2648 idx = 1;
2649 } else if (val & PIN_IN) {
2650 idx = out_jacks;
2651 if (in_jacks > 1) {
2652 unsigned int vref_caps = get_vref_caps(codec, nid);
2653 val &= AC_PINCTL_VREFEN;
2654 idx += cvt_from_vref_idx(vref_caps, val);
2655 }
2656 }
2657 return idx;
2658}
2659
2660static int hp_mic_jack_mode_get(struct snd_kcontrol *kcontrol,
2661 struct snd_ctl_elem_value *ucontrol)
2662{
2663 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2664 hda_nid_t nid = kcontrol->private_value;
2665 ucontrol->value.enumerated.item[0] =
2666 get_cur_hp_mic_jack_mode(codec, nid);
2667 return 0;
2668}
2669
2670static int hp_mic_jack_mode_put(struct snd_kcontrol *kcontrol,
2671 struct snd_ctl_elem_value *ucontrol)
2672{
2673 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2674 hda_nid_t nid = kcontrol->private_value;
2675 int out_jacks = get_out_jack_num_items(codec, nid);
2676 int in_jacks = get_in_jack_num_items(codec, nid);
2677 unsigned int val, oldval, idx;
2678
2679 oldval = get_cur_hp_mic_jack_mode(codec, nid);
2680 idx = ucontrol->value.enumerated.item[0];
2681 if (oldval == idx)
2682 return 0;
2683
2684 if (idx < out_jacks) {
2685 if (out_jacks > 1)
2686 val = idx ? PIN_HP : PIN_OUT;
2687 else
2688 val = PIN_HP;
2689 } else {
2690 idx -= out_jacks;
2691 if (in_jacks > 1) {
2692 unsigned int vref_caps = get_vref_caps(codec, nid);
2693 val = snd_hda_codec_get_pin_target(codec, nid);
Takashi Iwai3f550e32013-03-07 18:30:27 +01002694 val &= ~(AC_PINCTL_VREFEN | PIN_HP);
2695 val |= get_vref_idx(vref_caps, idx) | PIN_IN;
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002696 } else
2697 val = snd_hda_get_default_vref(codec, nid);
2698 }
2699 snd_hda_set_pin_ctl_cache(codec, nid, val);
Takashi Iwai8ba955c2013-03-07 18:40:58 +01002700 update_hp_automute_hook(codec);
2701
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002702 return 1;
2703}
2704
2705static const struct snd_kcontrol_new hp_mic_jack_mode_enum = {
2706 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2707 .info = hp_mic_jack_mode_info,
2708 .get = hp_mic_jack_mode_get,
2709 .put = hp_mic_jack_mode_put,
2710};
2711
2712static int create_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t pin)
2713{
2714 struct hda_gen_spec *spec = codec->spec;
2715 struct snd_kcontrol_new *knew;
2716
2717 if (get_out_jack_num_items(codec, pin) <= 1 &&
2718 get_in_jack_num_items(codec, pin) <= 1)
2719 return 0; /* no need */
2720 knew = snd_hda_gen_add_kctl(spec, "Headphone Mic Jack Mode",
2721 &hp_mic_jack_mode_enum);
2722 if (!knew)
2723 return -ENOMEM;
2724 knew->private_value = pin;
Takashi Iwai8ba955c2013-03-07 18:40:58 +01002725 spec->hp_mic_jack_modes = 1;
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002726 return 0;
2727}
Takashi Iwai352f7f92012-12-19 12:52:06 +01002728
2729/*
2730 * Parse input paths
2731 */
2732
Takashi Iwai352f7f92012-12-19 12:52:06 +01002733/* add the powersave loopback-list entry */
Takashi Iwai0186f4f2013-02-07 10:45:11 +01002734static int add_loopback_list(struct hda_gen_spec *spec, hda_nid_t mix, int idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002735{
2736 struct hda_amp_list *list;
2737
Takashi Iwai0186f4f2013-02-07 10:45:11 +01002738 list = snd_array_new(&spec->loopback_list);
2739 if (!list)
2740 return -ENOMEM;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002741 list->nid = mix;
2742 list->dir = HDA_INPUT;
2743 list->idx = idx;
Takashi Iwai0186f4f2013-02-07 10:45:11 +01002744 spec->loopback.amplist = spec->loopback_list.list;
2745 return 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02002746}
Takashi Iwaicb53c622007-08-10 17:21:45 +02002747
Takashi Iwai352f7f92012-12-19 12:52:06 +01002748/* create input playback/capture controls for the given pin */
Takashi Iwai196c17662013-01-04 15:01:40 +01002749static int new_analog_input(struct hda_codec *codec, int input_idx,
2750 hda_nid_t pin, const char *ctlname, int ctlidx,
Takashi Iwai352f7f92012-12-19 12:52:06 +01002751 hda_nid_t mix_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002753 struct hda_gen_spec *spec = codec->spec;
2754 struct nid_path *path;
2755 unsigned int val;
2756 int err, idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757
Takashi Iwai352f7f92012-12-19 12:52:06 +01002758 if (!nid_has_volume(codec, mix_nid, HDA_INPUT) &&
2759 !nid_has_mute(codec, mix_nid, HDA_INPUT))
2760 return 0; /* no need for analog loopback */
2761
Takashi Iwai3ca529d2013-01-07 17:25:08 +01002762 path = snd_hda_add_new_path(codec, pin, mix_nid, 0);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002763 if (!path)
2764 return -EINVAL;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01002765 print_nid_path("loopback", path);
Takashi Iwai196c17662013-01-04 15:01:40 +01002766 spec->loopback_paths[input_idx] = snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002767
2768 idx = path->idx[path->depth - 1];
2769 if (nid_has_volume(codec, mix_nid, HDA_INPUT)) {
2770 val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
2771 err = __add_pb_vol_ctrl(spec, HDA_CTL_WIDGET_VOL, ctlname, ctlidx, val);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002772 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002774 path->ctls[NID_PATH_VOL_CTL] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775 }
2776
Takashi Iwai352f7f92012-12-19 12:52:06 +01002777 if (nid_has_mute(codec, mix_nid, HDA_INPUT)) {
2778 val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
2779 err = __add_pb_sw_ctrl(spec, HDA_CTL_WIDGET_MUTE, ctlname, ctlidx, val);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002780 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002782 path->ctls[NID_PATH_MUTE_CTL] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783 }
2784
Takashi Iwai352f7f92012-12-19 12:52:06 +01002785 path->active = true;
Takashi Iwai0186f4f2013-02-07 10:45:11 +01002786 err = add_loopback_list(spec, mix_nid, idx);
2787 if (err < 0)
2788 return err;
Takashi Iwaie4a395e2013-01-23 17:00:31 +01002789
2790 if (spec->mixer_nid != spec->mixer_merge_nid &&
2791 !spec->loopback_merge_path) {
2792 path = snd_hda_add_new_path(codec, spec->mixer_nid,
2793 spec->mixer_merge_nid, 0);
2794 if (path) {
2795 print_nid_path("loopback-merge", path);
2796 path->active = true;
2797 spec->loopback_merge_path =
2798 snd_hda_get_path_idx(codec, path);
2799 }
2800 }
2801
Takashi Iwai352f7f92012-12-19 12:52:06 +01002802 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803}
2804
Takashi Iwai352f7f92012-12-19 12:52:06 +01002805static int is_input_pin(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002807 unsigned int pincap = snd_hda_query_pin_caps(codec, nid);
2808 return (pincap & AC_PINCAP_IN) != 0;
2809}
2810
2811/* Parse the codec tree and retrieve ADCs */
2812static int fill_adc_nids(struct hda_codec *codec)
2813{
2814 struct hda_gen_spec *spec = codec->spec;
2815 hda_nid_t nid;
2816 hda_nid_t *adc_nids = spec->adc_nids;
2817 int max_nums = ARRAY_SIZE(spec->adc_nids);
2818 int i, nums = 0;
2819
2820 nid = codec->start_nid;
2821 for (i = 0; i < codec->num_nodes; i++, nid++) {
2822 unsigned int caps = get_wcaps(codec, nid);
2823 int type = get_wcaps_type(caps);
2824
2825 if (type != AC_WID_AUD_IN || (caps & AC_WCAP_DIGITAL))
2826 continue;
2827 adc_nids[nums] = nid;
2828 if (++nums >= max_nums)
2829 break;
2830 }
2831 spec->num_adc_nids = nums;
Takashi Iwai0ffd5342013-01-17 15:53:29 +01002832
2833 /* copy the detected ADCs to all_adcs[] */
2834 spec->num_all_adcs = nums;
2835 memcpy(spec->all_adcs, spec->adc_nids, nums * sizeof(hda_nid_t));
2836
Takashi Iwai352f7f92012-12-19 12:52:06 +01002837 return nums;
2838}
2839
2840/* filter out invalid adc_nids that don't give all active input pins;
2841 * if needed, check whether dynamic ADC-switching is available
2842 */
2843static int check_dyn_adc_switch(struct hda_codec *codec)
2844{
2845 struct hda_gen_spec *spec = codec->spec;
2846 struct hda_input_mux *imux = &spec->input_mux;
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002847 unsigned int ok_bits;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002848 int i, n, nums;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002849
Takashi Iwai352f7f92012-12-19 12:52:06 +01002850 nums = 0;
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002851 ok_bits = 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002852 for (n = 0; n < spec->num_adc_nids; n++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002853 for (i = 0; i < imux->num_items; i++) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002854 if (!spec->input_paths[i][n])
Takashi Iwai352f7f92012-12-19 12:52:06 +01002855 break;
2856 }
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002857 if (i >= imux->num_items) {
2858 ok_bits |= (1 << n);
2859 nums++;
2860 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002861 }
2862
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002863 if (!ok_bits) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002864 /* check whether ADC-switch is possible */
2865 for (i = 0; i < imux->num_items; i++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002866 for (n = 0; n < spec->num_adc_nids; n++) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002867 if (spec->input_paths[i][n]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002868 spec->dyn_adc_idx[i] = n;
2869 break;
2870 }
2871 }
2872 }
2873
2874 snd_printdd("hda-codec: enabling ADC switching\n");
2875 spec->dyn_adc_switch = 1;
2876 } else if (nums != spec->num_adc_nids) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002877 /* shrink the invalid adcs and input paths */
2878 nums = 0;
2879 for (n = 0; n < spec->num_adc_nids; n++) {
2880 if (!(ok_bits & (1 << n)))
2881 continue;
2882 if (n != nums) {
2883 spec->adc_nids[nums] = spec->adc_nids[n];
Takashi Iwai980428c2013-01-09 09:28:20 +01002884 for (i = 0; i < imux->num_items; i++) {
2885 invalidate_nid_path(codec,
2886 spec->input_paths[i][nums]);
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002887 spec->input_paths[i][nums] =
2888 spec->input_paths[i][n];
Takashi Iwai980428c2013-01-09 09:28:20 +01002889 }
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002890 }
2891 nums++;
2892 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002893 spec->num_adc_nids = nums;
2894 }
2895
Takashi Iwai967303d2013-02-19 17:12:42 +01002896 if (imux->num_items == 1 ||
2897 (imux->num_items == 2 && spec->hp_mic)) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002898 snd_printdd("hda-codec: reducing to a single ADC\n");
2899 spec->num_adc_nids = 1; /* reduce to a single ADC */
2900 }
2901
2902 /* single index for individual volumes ctls */
2903 if (!spec->dyn_adc_switch && spec->multi_cap_vol)
2904 spec->num_adc_nids = 1;
2905
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906 return 0;
2907}
2908
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002909/* parse capture source paths from the given pin and create imux items */
2910static int parse_capture_source(struct hda_codec *codec, hda_nid_t pin,
Takashi Iwai9dba2052013-01-18 10:01:15 +01002911 int cfg_idx, int num_adcs,
2912 const char *label, int anchor)
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002913{
2914 struct hda_gen_spec *spec = codec->spec;
2915 struct hda_input_mux *imux = &spec->input_mux;
2916 int imux_idx = imux->num_items;
2917 bool imux_added = false;
2918 int c;
2919
2920 for (c = 0; c < num_adcs; c++) {
2921 struct nid_path *path;
2922 hda_nid_t adc = spec->adc_nids[c];
2923
2924 if (!is_reachable_path(codec, pin, adc))
2925 continue;
2926 path = snd_hda_add_new_path(codec, pin, adc, anchor);
2927 if (!path)
2928 continue;
2929 print_nid_path("input", path);
2930 spec->input_paths[imux_idx][c] =
2931 snd_hda_get_path_idx(codec, path);
2932
2933 if (!imux_added) {
Takashi Iwai967303d2013-02-19 17:12:42 +01002934 if (spec->hp_mic_pin == pin)
2935 spec->hp_mic_mux_idx = imux->num_items;
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002936 spec->imux_pins[imux->num_items] = pin;
Takashi Iwai9dba2052013-01-18 10:01:15 +01002937 snd_hda_add_imux_item(imux, label, cfg_idx, NULL);
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002938 imux_added = true;
2939 }
2940 }
2941
2942 return 0;
2943}
2944
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01002946 * create playback/capture controls for input pins
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947 */
Takashi Iwai9dba2052013-01-18 10:01:15 +01002948
Takashi Iwaic9700422013-01-18 10:17:30 +01002949/* fill the label for each input at first */
2950static int fill_input_pin_labels(struct hda_codec *codec)
2951{
2952 struct hda_gen_spec *spec = codec->spec;
2953 const struct auto_pin_cfg *cfg = &spec->autocfg;
2954 int i;
2955
2956 for (i = 0; i < cfg->num_inputs; i++) {
2957 hda_nid_t pin = cfg->inputs[i].pin;
2958 const char *label;
2959 int j, idx;
2960
2961 if (!is_input_pin(codec, pin))
2962 continue;
2963
2964 label = hda_get_autocfg_input_label(codec, cfg, i);
2965 idx = 0;
David Henningsson8e8db7f2013-01-18 15:43:02 +01002966 for (j = i - 1; j >= 0; j--) {
Takashi Iwaic9700422013-01-18 10:17:30 +01002967 if (spec->input_labels[j] &&
2968 !strcmp(spec->input_labels[j], label)) {
2969 idx = spec->input_label_idxs[j] + 1;
2970 break;
2971 }
2972 }
2973
2974 spec->input_labels[i] = label;
2975 spec->input_label_idxs[i] = idx;
2976 }
2977
2978 return 0;
2979}
2980
Takashi Iwai9dba2052013-01-18 10:01:15 +01002981#define CFG_IDX_MIX 99 /* a dummy cfg->input idx for stereo mix */
2982
Takashi Iwai352f7f92012-12-19 12:52:06 +01002983static int create_input_ctls(struct hda_codec *codec)
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02002984{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002985 struct hda_gen_spec *spec = codec->spec;
2986 const struct auto_pin_cfg *cfg = &spec->autocfg;
2987 hda_nid_t mixer = spec->mixer_nid;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002988 int num_adcs;
Takashi Iwaic9700422013-01-18 10:17:30 +01002989 int i, err;
Takashi Iwai2c12c302013-01-10 09:33:29 +01002990 unsigned int val;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02002991
Takashi Iwai352f7f92012-12-19 12:52:06 +01002992 num_adcs = fill_adc_nids(codec);
2993 if (num_adcs < 0)
2994 return 0;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02002995
Takashi Iwaic9700422013-01-18 10:17:30 +01002996 err = fill_input_pin_labels(codec);
2997 if (err < 0)
2998 return err;
2999
Takashi Iwai352f7f92012-12-19 12:52:06 +01003000 for (i = 0; i < cfg->num_inputs; i++) {
3001 hda_nid_t pin;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002
Takashi Iwai352f7f92012-12-19 12:52:06 +01003003 pin = cfg->inputs[i].pin;
3004 if (!is_input_pin(codec, pin))
3005 continue;
3006
Takashi Iwai2c12c302013-01-10 09:33:29 +01003007 val = PIN_IN;
3008 if (cfg->inputs[i].type == AUTO_PIN_MIC)
3009 val |= snd_hda_get_default_vref(codec, pin);
Takashi Iwai93c9d8a2013-03-11 09:48:43 +01003010 if (pin != spec->hp_mic_pin)
3011 set_pin_target(codec, pin, val, false);
Takashi Iwai2c12c302013-01-10 09:33:29 +01003012
Takashi Iwai352f7f92012-12-19 12:52:06 +01003013 if (mixer) {
3014 if (is_reachable_path(codec, pin, mixer)) {
Takashi Iwai196c17662013-01-04 15:01:40 +01003015 err = new_analog_input(codec, i, pin,
Takashi Iwaic9700422013-01-18 10:17:30 +01003016 spec->input_labels[i],
3017 spec->input_label_idxs[i],
3018 mixer);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003019 if (err < 0)
3020 return err;
3021 }
3022 }
3023
Takashi Iwaic9700422013-01-18 10:17:30 +01003024 err = parse_capture_source(codec, pin, i, num_adcs,
3025 spec->input_labels[i], -mixer);
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003026 if (err < 0)
3027 return err;
Takashi Iwai294765582013-01-17 09:52:11 +01003028
Takashi Iwaif811c3c2013-03-07 18:32:59 +01003029 if (spec->add_jack_modes) {
Takashi Iwai294765582013-01-17 09:52:11 +01003030 err = create_in_jack_mode(codec, pin);
3031 if (err < 0)
3032 return err;
3033 }
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003034 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003035
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003036 if (mixer && spec->add_stereo_mix_input) {
Takashi Iwai9dba2052013-01-18 10:01:15 +01003037 err = parse_capture_source(codec, mixer, CFG_IDX_MIX, num_adcs,
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003038 "Stereo Mix", 0);
3039 if (err < 0)
3040 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003041 }
3042
3043 return 0;
3044}
3045
3046
3047/*
3048 * input source mux
3049 */
3050
Takashi Iwaic697b712013-01-07 17:09:26 +01003051/* get the input path specified by the given adc and imux indices */
3052static struct nid_path *get_input_path(struct hda_codec *codec, int adc_idx, int imux_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003053{
3054 struct hda_gen_spec *spec = codec->spec;
David Henningssonb56fa1e2013-01-16 11:45:35 +01003055 if (imux_idx < 0 || imux_idx >= HDA_MAX_NUM_INPUTS) {
3056 snd_BUG();
3057 return NULL;
3058 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003059 if (spec->dyn_adc_switch)
3060 adc_idx = spec->dyn_adc_idx[imux_idx];
David Henningssond3d982f2013-01-18 15:43:01 +01003061 if (adc_idx < 0 || adc_idx >= AUTO_CFG_MAX_INS) {
David Henningssonb56fa1e2013-01-16 11:45:35 +01003062 snd_BUG();
3063 return NULL;
3064 }
Takashi Iwaic697b712013-01-07 17:09:26 +01003065 return snd_hda_get_path_from_idx(codec, spec->input_paths[imux_idx][adc_idx]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003066}
3067
3068static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
3069 unsigned int idx);
3070
3071static int mux_enum_info(struct snd_kcontrol *kcontrol,
3072 struct snd_ctl_elem_info *uinfo)
3073{
3074 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3075 struct hda_gen_spec *spec = codec->spec;
3076 return snd_hda_input_mux_info(&spec->input_mux, uinfo);
3077}
3078
3079static int mux_enum_get(struct snd_kcontrol *kcontrol,
3080 struct snd_ctl_elem_value *ucontrol)
3081{
3082 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3083 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai2a8d5392013-01-18 16:23:25 +01003084 /* the ctls are created at once with multiple counts */
3085 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003086
3087 ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
3088 return 0;
3089}
3090
3091static int mux_enum_put(struct snd_kcontrol *kcontrol,
3092 struct snd_ctl_elem_value *ucontrol)
3093{
3094 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Takashi Iwai2a8d5392013-01-18 16:23:25 +01003095 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003096 return mux_select(codec, adc_idx,
3097 ucontrol->value.enumerated.item[0]);
3098}
3099
Takashi Iwai352f7f92012-12-19 12:52:06 +01003100static const struct snd_kcontrol_new cap_src_temp = {
3101 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3102 .name = "Input Source",
3103 .info = mux_enum_info,
3104 .get = mux_enum_get,
3105 .put = mux_enum_put,
3106};
3107
Takashi Iwai47d46ab2012-12-20 11:48:54 +01003108/*
3109 * capture volume and capture switch ctls
3110 */
3111
Takashi Iwai352f7f92012-12-19 12:52:06 +01003112typedef int (*put_call_t)(struct snd_kcontrol *kcontrol,
3113 struct snd_ctl_elem_value *ucontrol);
3114
Takashi Iwai47d46ab2012-12-20 11:48:54 +01003115/* call the given amp update function for all amps in the imux list at once */
Takashi Iwai352f7f92012-12-19 12:52:06 +01003116static int cap_put_caller(struct snd_kcontrol *kcontrol,
3117 struct snd_ctl_elem_value *ucontrol,
3118 put_call_t func, int type)
3119{
3120 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3121 struct hda_gen_spec *spec = codec->spec;
3122 const struct hda_input_mux *imux;
3123 struct nid_path *path;
3124 int i, adc_idx, err = 0;
3125
3126 imux = &spec->input_mux;
David Henningssona053d1e2013-01-16 11:45:36 +01003127 adc_idx = kcontrol->id.index;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003128 mutex_lock(&codec->control_mutex);
Takashi Iwai47d46ab2012-12-20 11:48:54 +01003129 /* we use the cache-only update at first since multiple input paths
3130 * may shared the same amp; by updating only caches, the redundant
3131 * writes to hardware can be reduced.
3132 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01003133 codec->cached_write = 1;
3134 for (i = 0; i < imux->num_items; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01003135 path = get_input_path(codec, adc_idx, i);
3136 if (!path || !path->ctls[type])
Takashi Iwai352f7f92012-12-19 12:52:06 +01003137 continue;
3138 kcontrol->private_value = path->ctls[type];
3139 err = func(kcontrol, ucontrol);
3140 if (err < 0)
3141 goto error;
3142 }
3143 error:
3144 codec->cached_write = 0;
3145 mutex_unlock(&codec->control_mutex);
Takashi Iwaidc870f32013-01-22 15:24:30 +01003146 snd_hda_codec_flush_cache(codec); /* flush the updates */
Takashi Iwai352f7f92012-12-19 12:52:06 +01003147 if (err >= 0 && spec->cap_sync_hook)
Takashi Iwaia90229e2013-01-18 14:10:00 +01003148 spec->cap_sync_hook(codec, ucontrol);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003149 return err;
3150}
3151
3152/* capture volume ctl callbacks */
3153#define cap_vol_info snd_hda_mixer_amp_volume_info
3154#define cap_vol_get snd_hda_mixer_amp_volume_get
3155#define cap_vol_tlv snd_hda_mixer_amp_tlv
3156
3157static int cap_vol_put(struct snd_kcontrol *kcontrol,
3158 struct snd_ctl_elem_value *ucontrol)
3159{
3160 return cap_put_caller(kcontrol, ucontrol,
3161 snd_hda_mixer_amp_volume_put,
3162 NID_PATH_VOL_CTL);
3163}
3164
3165static const struct snd_kcontrol_new cap_vol_temp = {
3166 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3167 .name = "Capture Volume",
3168 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
3169 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
3170 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK),
3171 .info = cap_vol_info,
3172 .get = cap_vol_get,
3173 .put = cap_vol_put,
3174 .tlv = { .c = cap_vol_tlv },
3175};
3176
3177/* capture switch ctl callbacks */
3178#define cap_sw_info snd_ctl_boolean_stereo_info
3179#define cap_sw_get snd_hda_mixer_amp_switch_get
3180
3181static int cap_sw_put(struct snd_kcontrol *kcontrol,
3182 struct snd_ctl_elem_value *ucontrol)
3183{
Takashi Iwaia90229e2013-01-18 14:10:00 +01003184 return cap_put_caller(kcontrol, ucontrol,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003185 snd_hda_mixer_amp_switch_put,
3186 NID_PATH_MUTE_CTL);
3187}
3188
3189static const struct snd_kcontrol_new cap_sw_temp = {
3190 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3191 .name = "Capture Switch",
3192 .info = cap_sw_info,
3193 .get = cap_sw_get,
3194 .put = cap_sw_put,
3195};
3196
3197static int parse_capvol_in_path(struct hda_codec *codec, struct nid_path *path)
3198{
3199 hda_nid_t nid;
3200 int i, depth;
3201
3202 path->ctls[NID_PATH_VOL_CTL] = path->ctls[NID_PATH_MUTE_CTL] = 0;
3203 for (depth = 0; depth < 3; depth++) {
3204 if (depth >= path->depth)
3205 return -EINVAL;
3206 i = path->depth - depth - 1;
3207 nid = path->path[i];
3208 if (!path->ctls[NID_PATH_VOL_CTL]) {
3209 if (nid_has_volume(codec, nid, HDA_OUTPUT))
3210 path->ctls[NID_PATH_VOL_CTL] =
3211 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3212 else if (nid_has_volume(codec, nid, HDA_INPUT)) {
3213 int idx = path->idx[i];
3214 if (!depth && codec->single_adc_amp)
3215 idx = 0;
3216 path->ctls[NID_PATH_VOL_CTL] =
3217 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
3218 }
3219 }
3220 if (!path->ctls[NID_PATH_MUTE_CTL]) {
3221 if (nid_has_mute(codec, nid, HDA_OUTPUT))
3222 path->ctls[NID_PATH_MUTE_CTL] =
3223 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3224 else if (nid_has_mute(codec, nid, HDA_INPUT)) {
3225 int idx = path->idx[i];
3226 if (!depth && codec->single_adc_amp)
3227 idx = 0;
3228 path->ctls[NID_PATH_MUTE_CTL] =
3229 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
3230 }
3231 }
Takashi Iwai97ec5582006-03-21 11:29:07 +01003232 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233 return 0;
3234}
3235
Takashi Iwai352f7f92012-12-19 12:52:06 +01003236static bool is_inv_dmic_pin(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003238 struct hda_gen_spec *spec = codec->spec;
3239 struct auto_pin_cfg *cfg = &spec->autocfg;
3240 unsigned int val;
3241 int i;
3242
3243 if (!spec->inv_dmic_split)
3244 return false;
3245 for (i = 0; i < cfg->num_inputs; i++) {
3246 if (cfg->inputs[i].pin != nid)
3247 continue;
3248 if (cfg->inputs[i].type != AUTO_PIN_MIC)
3249 return false;
3250 val = snd_hda_codec_get_pincfg(codec, nid);
3251 return snd_hda_get_input_pin_attr(val) == INPUT_PIN_ATTR_INT;
3252 }
3253 return false;
3254}
3255
Takashi Iwaia90229e2013-01-18 14:10:00 +01003256/* capture switch put callback for a single control with hook call */
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003257static int cap_single_sw_put(struct snd_kcontrol *kcontrol,
3258 struct snd_ctl_elem_value *ucontrol)
3259{
3260 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3261 struct hda_gen_spec *spec = codec->spec;
3262 int ret;
3263
3264 ret = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
3265 if (ret < 0)
3266 return ret;
3267
Takashi Iwaia90229e2013-01-18 14:10:00 +01003268 if (spec->cap_sync_hook)
3269 spec->cap_sync_hook(codec, ucontrol);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003270
3271 return ret;
3272}
3273
Takashi Iwai352f7f92012-12-19 12:52:06 +01003274static int add_single_cap_ctl(struct hda_codec *codec, const char *label,
3275 int idx, bool is_switch, unsigned int ctl,
3276 bool inv_dmic)
3277{
3278 struct hda_gen_spec *spec = codec->spec;
3279 char tmpname[44];
3280 int type = is_switch ? HDA_CTL_WIDGET_MUTE : HDA_CTL_WIDGET_VOL;
3281 const char *sfx = is_switch ? "Switch" : "Volume";
3282 unsigned int chs = inv_dmic ? 1 : 3;
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003283 struct snd_kcontrol_new *knew;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003284
3285 if (!ctl)
3286 return 0;
3287
3288 if (label)
3289 snprintf(tmpname, sizeof(tmpname),
3290 "%s Capture %s", label, sfx);
3291 else
3292 snprintf(tmpname, sizeof(tmpname),
3293 "Capture %s", sfx);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003294 knew = add_control(spec, type, tmpname, idx,
3295 amp_val_replace_channels(ctl, chs));
3296 if (!knew)
3297 return -ENOMEM;
Takashi Iwaia90229e2013-01-18 14:10:00 +01003298 if (is_switch)
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003299 knew->put = cap_single_sw_put;
3300 if (!inv_dmic)
3301 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003302
3303 /* Make independent right kcontrol */
3304 if (label)
3305 snprintf(tmpname, sizeof(tmpname),
3306 "Inverted %s Capture %s", label, sfx);
3307 else
3308 snprintf(tmpname, sizeof(tmpname),
3309 "Inverted Capture %s", sfx);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003310 knew = add_control(spec, type, tmpname, idx,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003311 amp_val_replace_channels(ctl, 2));
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003312 if (!knew)
3313 return -ENOMEM;
Takashi Iwaia90229e2013-01-18 14:10:00 +01003314 if (is_switch)
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003315 knew->put = cap_single_sw_put;
3316 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003317}
3318
3319/* create single (and simple) capture volume and switch controls */
3320static int create_single_cap_vol_ctl(struct hda_codec *codec, int idx,
3321 unsigned int vol_ctl, unsigned int sw_ctl,
3322 bool inv_dmic)
3323{
3324 int err;
3325 err = add_single_cap_ctl(codec, NULL, idx, false, vol_ctl, inv_dmic);
3326 if (err < 0)
3327 return err;
3328 err = add_single_cap_ctl(codec, NULL, idx, true, sw_ctl, inv_dmic);
3329 if (err < 0)
3330 return err;
3331 return 0;
3332}
3333
3334/* create bound capture volume and switch controls */
3335static int create_bind_cap_vol_ctl(struct hda_codec *codec, int idx,
3336 unsigned int vol_ctl, unsigned int sw_ctl)
3337{
3338 struct hda_gen_spec *spec = codec->spec;
3339 struct snd_kcontrol_new *knew;
3340
3341 if (vol_ctl) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01003342 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_vol_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003343 if (!knew)
3344 return -ENOMEM;
3345 knew->index = idx;
3346 knew->private_value = vol_ctl;
3347 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
3348 }
3349 if (sw_ctl) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01003350 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_sw_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003351 if (!knew)
3352 return -ENOMEM;
3353 knew->index = idx;
3354 knew->private_value = sw_ctl;
3355 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
3356 }
3357 return 0;
3358}
3359
3360/* return the vol ctl when used first in the imux list */
3361static unsigned int get_first_cap_ctl(struct hda_codec *codec, int idx, int type)
3362{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003363 struct nid_path *path;
3364 unsigned int ctl;
3365 int i;
3366
Takashi Iwaic697b712013-01-07 17:09:26 +01003367 path = get_input_path(codec, 0, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003368 if (!path)
3369 return 0;
3370 ctl = path->ctls[type];
3371 if (!ctl)
3372 return 0;
3373 for (i = 0; i < idx - 1; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01003374 path = get_input_path(codec, 0, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003375 if (path && path->ctls[type] == ctl)
3376 return 0;
3377 }
3378 return ctl;
3379}
3380
3381/* create individual capture volume and switch controls per input */
3382static int create_multi_cap_vol_ctl(struct hda_codec *codec)
3383{
3384 struct hda_gen_spec *spec = codec->spec;
3385 struct hda_input_mux *imux = &spec->input_mux;
Takashi Iwaic9700422013-01-18 10:17:30 +01003386 int i, err, type;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003387
3388 for (i = 0; i < imux->num_items; i++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003389 bool inv_dmic;
Takashi Iwaic9700422013-01-18 10:17:30 +01003390 int idx;
Takashi Iwai9dba2052013-01-18 10:01:15 +01003391
Takashi Iwaic9700422013-01-18 10:17:30 +01003392 idx = imux->items[i].index;
3393 if (idx >= spec->autocfg.num_inputs)
Takashi Iwai9dba2052013-01-18 10:01:15 +01003394 continue;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003395 inv_dmic = is_inv_dmic_pin(codec, spec->imux_pins[i]);
3396
3397 for (type = 0; type < 2; type++) {
Takashi Iwaic9700422013-01-18 10:17:30 +01003398 err = add_single_cap_ctl(codec,
3399 spec->input_labels[idx],
3400 spec->input_label_idxs[idx],
3401 type,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003402 get_first_cap_ctl(codec, i, type),
3403 inv_dmic);
3404 if (err < 0)
3405 return err;
3406 }
3407 }
3408 return 0;
3409}
3410
3411static int create_capture_mixers(struct hda_codec *codec)
3412{
3413 struct hda_gen_spec *spec = codec->spec;
3414 struct hda_input_mux *imux = &spec->input_mux;
3415 int i, n, nums, err;
3416
3417 if (spec->dyn_adc_switch)
3418 nums = 1;
3419 else
3420 nums = spec->num_adc_nids;
3421
3422 if (!spec->auto_mic && imux->num_items > 1) {
3423 struct snd_kcontrol_new *knew;
Takashi Iwai624d9142012-12-19 17:41:52 +01003424 const char *name;
3425 name = nums > 1 ? "Input Source" : "Capture Source";
3426 knew = snd_hda_gen_add_kctl(spec, name, &cap_src_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003427 if (!knew)
3428 return -ENOMEM;
3429 knew->count = nums;
3430 }
3431
3432 for (n = 0; n < nums; n++) {
3433 bool multi = false;
David Henningsson99a55922013-01-16 15:58:44 +01003434 bool multi_cap_vol = spec->multi_cap_vol;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003435 bool inv_dmic = false;
3436 int vol, sw;
3437
3438 vol = sw = 0;
3439 for (i = 0; i < imux->num_items; i++) {
3440 struct nid_path *path;
Takashi Iwaic697b712013-01-07 17:09:26 +01003441 path = get_input_path(codec, n, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003442 if (!path)
3443 continue;
3444 parse_capvol_in_path(codec, path);
3445 if (!vol)
3446 vol = path->ctls[NID_PATH_VOL_CTL];
David Henningsson99a55922013-01-16 15:58:44 +01003447 else if (vol != path->ctls[NID_PATH_VOL_CTL]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003448 multi = true;
David Henningsson99a55922013-01-16 15:58:44 +01003449 if (!same_amp_caps(codec, vol,
3450 path->ctls[NID_PATH_VOL_CTL], HDA_INPUT))
3451 multi_cap_vol = true;
3452 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003453 if (!sw)
3454 sw = path->ctls[NID_PATH_MUTE_CTL];
David Henningsson99a55922013-01-16 15:58:44 +01003455 else if (sw != path->ctls[NID_PATH_MUTE_CTL]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003456 multi = true;
David Henningsson99a55922013-01-16 15:58:44 +01003457 if (!same_amp_caps(codec, sw,
3458 path->ctls[NID_PATH_MUTE_CTL], HDA_INPUT))
3459 multi_cap_vol = true;
3460 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003461 if (is_inv_dmic_pin(codec, spec->imux_pins[i]))
3462 inv_dmic = true;
3463 }
3464
3465 if (!multi)
3466 err = create_single_cap_vol_ctl(codec, n, vol, sw,
3467 inv_dmic);
David Henningsson99a55922013-01-16 15:58:44 +01003468 else if (!multi_cap_vol)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003469 err = create_bind_cap_vol_ctl(codec, n, vol, sw);
3470 else
3471 err = create_multi_cap_vol_ctl(codec);
3472 if (err < 0)
3473 return err;
3474 }
3475
3476 return 0;
3477}
3478
3479/*
3480 * add mic boosts if needed
3481 */
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003482
3483/* check whether the given amp is feasible as a boost volume */
3484static bool check_boost_vol(struct hda_codec *codec, hda_nid_t nid,
3485 int dir, int idx)
3486{
3487 unsigned int step;
3488
3489 if (!nid_has_volume(codec, nid, dir) ||
3490 is_ctl_associated(codec, nid, dir, idx, NID_PATH_VOL_CTL) ||
3491 is_ctl_associated(codec, nid, dir, idx, NID_PATH_BOOST_CTL))
3492 return false;
3493
3494 step = (query_amp_caps(codec, nid, dir) & AC_AMPCAP_STEP_SIZE)
3495 >> AC_AMPCAP_STEP_SIZE_SHIFT;
3496 if (step < 0x20)
3497 return false;
3498 return true;
3499}
3500
3501/* look for a boost amp in a widget close to the pin */
3502static unsigned int look_for_boost_amp(struct hda_codec *codec,
3503 struct nid_path *path)
3504{
3505 unsigned int val = 0;
3506 hda_nid_t nid;
3507 int depth;
3508
3509 for (depth = 0; depth < 3; depth++) {
3510 if (depth >= path->depth - 1)
3511 break;
3512 nid = path->path[depth];
3513 if (depth && check_boost_vol(codec, nid, HDA_OUTPUT, 0)) {
3514 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3515 break;
3516 } else if (check_boost_vol(codec, nid, HDA_INPUT,
3517 path->idx[depth])) {
3518 val = HDA_COMPOSE_AMP_VAL(nid, 3, path->idx[depth],
3519 HDA_INPUT);
3520 break;
3521 }
3522 }
3523
3524 return val;
3525}
3526
Takashi Iwai352f7f92012-12-19 12:52:06 +01003527static int parse_mic_boost(struct hda_codec *codec)
3528{
3529 struct hda_gen_spec *spec = codec->spec;
3530 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003531 struct hda_input_mux *imux = &spec->input_mux;
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003532 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003533
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003534 if (!spec->num_adc_nids)
3535 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003536
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003537 for (i = 0; i < imux->num_items; i++) {
3538 struct nid_path *path;
3539 unsigned int val;
3540 int idx;
3541 char boost_label[44];
David Henningsson02aba552013-01-16 15:58:43 +01003542
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003543 idx = imux->items[i].index;
3544 if (idx >= imux->num_items)
3545 continue;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003546
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003547 /* check only line-in and mic pins */
Takashi Iwai1799cdd52013-01-18 14:37:16 +01003548 if (cfg->inputs[idx].type > AUTO_PIN_LINE_IN)
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003549 continue;
3550
3551 path = get_input_path(codec, 0, i);
3552 if (!path)
3553 continue;
3554
3555 val = look_for_boost_amp(codec, path);
3556 if (!val)
3557 continue;
3558
3559 /* create a boost control */
3560 snprintf(boost_label, sizeof(boost_label),
3561 "%s Boost Volume", spec->input_labels[idx]);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003562 if (!add_control(spec, HDA_CTL_WIDGET_VOL, boost_label,
3563 spec->input_label_idxs[idx], val))
3564 return -ENOMEM;
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003565
3566 path->ctls[NID_PATH_BOOST_CTL] = val;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003567 }
3568 return 0;
3569}
3570
3571/*
3572 * parse digital I/Os and set up NIDs in BIOS auto-parse mode
3573 */
3574static void parse_digital(struct hda_codec *codec)
3575{
3576 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003577 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003578 int i, nums;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003579 hda_nid_t dig_nid, pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003580
3581 /* support multiple SPDIFs; the secondary is set up as a slave */
3582 nums = 0;
3583 for (i = 0; i < spec->autocfg.dig_outs; i++) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01003584 pin = spec->autocfg.dig_out_pins[i];
Takashi Iwai352f7f92012-12-19 12:52:06 +01003585 dig_nid = look_for_dac(codec, pin, true);
3586 if (!dig_nid)
3587 continue;
Takashi Iwai3ca529d2013-01-07 17:25:08 +01003588 path = snd_hda_add_new_path(codec, dig_nid, pin, 0);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003589 if (!path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003590 continue;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003591 print_nid_path("digout", path);
Takashi Iwaie1284af2013-01-03 16:33:02 +01003592 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +01003593 spec->digout_paths[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai2c12c302013-01-10 09:33:29 +01003594 set_pin_target(codec, pin, PIN_OUT, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003595 if (!nums) {
3596 spec->multiout.dig_out_nid = dig_nid;
3597 spec->dig_out_type = spec->autocfg.dig_out_type[0];
3598 } else {
3599 spec->multiout.slave_dig_outs = spec->slave_dig_outs;
3600 if (nums >= ARRAY_SIZE(spec->slave_dig_outs) - 1)
3601 break;
3602 spec->slave_dig_outs[nums - 1] = dig_nid;
3603 }
3604 nums++;
3605 }
3606
3607 if (spec->autocfg.dig_in_pin) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01003608 pin = spec->autocfg.dig_in_pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003609 dig_nid = codec->start_nid;
3610 for (i = 0; i < codec->num_nodes; i++, dig_nid++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003611 unsigned int wcaps = get_wcaps(codec, dig_nid);
3612 if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
3613 continue;
3614 if (!(wcaps & AC_WCAP_DIGITAL))
3615 continue;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003616 path = snd_hda_add_new_path(codec, pin, dig_nid, 0);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003617 if (path) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003618 print_nid_path("digin", path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003619 path->active = true;
3620 spec->dig_in_nid = dig_nid;
Takashi Iwai2430d7b2013-01-04 15:09:42 +01003621 spec->digin_path = snd_hda_get_path_idx(codec, path);
Takashi Iwai2c12c302013-01-10 09:33:29 +01003622 set_pin_target(codec, pin, PIN_IN, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003623 break;
3624 }
3625 }
3626 }
3627}
3628
3629
3630/*
3631 * input MUX handling
3632 */
3633
3634static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur);
3635
3636/* select the given imux item; either unmute exclusively or select the route */
3637static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
3638 unsigned int idx)
3639{
3640 struct hda_gen_spec *spec = codec->spec;
3641 const struct hda_input_mux *imux;
Takashi Iwai55196ff2013-01-24 17:32:56 +01003642 struct nid_path *old_path, *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003643
3644 imux = &spec->input_mux;
3645 if (!imux->num_items)
3646 return 0;
3647
3648 if (idx >= imux->num_items)
3649 idx = imux->num_items - 1;
3650 if (spec->cur_mux[adc_idx] == idx)
3651 return 0;
3652
Takashi Iwai55196ff2013-01-24 17:32:56 +01003653 old_path = get_input_path(codec, adc_idx, spec->cur_mux[adc_idx]);
3654 if (!old_path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003655 return 0;
Takashi Iwai55196ff2013-01-24 17:32:56 +01003656 if (old_path->active)
3657 snd_hda_activate_path(codec, old_path, false, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003658
3659 spec->cur_mux[adc_idx] = idx;
3660
Takashi Iwai967303d2013-02-19 17:12:42 +01003661 if (spec->hp_mic)
3662 update_hp_mic(codec, adc_idx, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003663
3664 if (spec->dyn_adc_switch)
3665 dyn_adc_pcm_resetup(codec, idx);
3666
Takashi Iwaic697b712013-01-07 17:09:26 +01003667 path = get_input_path(codec, adc_idx, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003668 if (!path)
3669 return 0;
3670 if (path->active)
3671 return 0;
3672 snd_hda_activate_path(codec, path, true, false);
3673 if (spec->cap_sync_hook)
Takashi Iwaia90229e2013-01-18 14:10:00 +01003674 spec->cap_sync_hook(codec, NULL);
Takashi Iwai55196ff2013-01-24 17:32:56 +01003675 path_power_down_sync(codec, old_path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003676 return 1;
3677}
3678
3679
3680/*
3681 * Jack detections for HP auto-mute and mic-switch
3682 */
3683
3684/* check each pin in the given array; returns true if any of them is plugged */
3685static bool detect_jacks(struct hda_codec *codec, int num_pins, hda_nid_t *pins)
3686{
3687 int i, present = 0;
3688
3689 for (i = 0; i < num_pins; i++) {
3690 hda_nid_t nid = pins[i];
3691 if (!nid)
3692 break;
Takashi Iwai0b4df932013-01-10 09:45:13 +01003693 /* don't detect pins retasked as inputs */
3694 if (snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_IN_EN)
3695 continue;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003696 present |= snd_hda_jack_detect(codec, nid);
3697 }
3698 return present;
3699}
3700
3701/* standard HP/line-out auto-mute helper */
3702static void do_automute(struct hda_codec *codec, int num_pins, hda_nid_t *pins,
Takashi Iwai2c12c302013-01-10 09:33:29 +01003703 bool mute)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003704{
3705 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003706 int i;
3707
3708 for (i = 0; i < num_pins; i++) {
3709 hda_nid_t nid = pins[i];
Takashi Iwai967303d2013-02-19 17:12:42 +01003710 unsigned int val, oldval;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003711 if (!nid)
3712 break;
Takashi Iwai967303d2013-02-19 17:12:42 +01003713 oldval = snd_hda_codec_get_pin_target(codec, nid);
3714 if (oldval & PIN_IN)
3715 continue; /* no mute for inputs */
Takashi Iwai352f7f92012-12-19 12:52:06 +01003716 /* don't reset VREF value in case it's controlling
3717 * the amp (see alc861_fixup_asus_amp_vref_0f())
3718 */
Takashi Iwai2c12c302013-01-10 09:33:29 +01003719 if (spec->keep_vref_in_automute)
Takashi Iwai967303d2013-02-19 17:12:42 +01003720 val = oldval & ~PIN_HP;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003721 else
Takashi Iwai352f7f92012-12-19 12:52:06 +01003722 val = 0;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003723 if (!mute)
Takashi Iwai967303d2013-02-19 17:12:42 +01003724 val |= oldval;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003725 /* here we call update_pin_ctl() so that the pinctl is changed
3726 * without changing the pinctl target value;
3727 * the original target value will be still referred at the
3728 * init / resume again
3729 */
3730 update_pin_ctl(codec, nid, val);
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01003731 set_pin_eapd(codec, nid, !mute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003732 }
3733}
3734
3735/* Toggle outputs muting */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003736void snd_hda_gen_update_outputs(struct hda_codec *codec)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003737{
3738 struct hda_gen_spec *spec = codec->spec;
3739 int on;
3740
3741 /* Control HP pins/amps depending on master_mute state;
3742 * in general, HP pins/amps control should be enabled in all cases,
3743 * but currently set only for master_mute, just to be safe
3744 */
Takashi Iwai967303d2013-02-19 17:12:42 +01003745 do_automute(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
Takashi Iwai2c12c302013-01-10 09:33:29 +01003746 spec->autocfg.hp_pins, spec->master_mute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003747
3748 if (!spec->automute_speaker)
3749 on = 0;
3750 else
3751 on = spec->hp_jack_present | spec->line_jack_present;
3752 on |= spec->master_mute;
Takashi Iwai47b9ddb2013-01-16 18:18:00 +01003753 spec->speaker_muted = on;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003754 do_automute(codec, ARRAY_SIZE(spec->autocfg.speaker_pins),
Takashi Iwai2c12c302013-01-10 09:33:29 +01003755 spec->autocfg.speaker_pins, on);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003756
3757 /* toggle line-out mutes if needed, too */
3758 /* if LO is a copy of either HP or Speaker, don't need to handle it */
3759 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0] ||
3760 spec->autocfg.line_out_pins[0] == spec->autocfg.speaker_pins[0])
3761 return;
3762 if (!spec->automute_lo)
3763 on = 0;
3764 else
3765 on = spec->hp_jack_present;
3766 on |= spec->master_mute;
Takashi Iwai47b9ddb2013-01-16 18:18:00 +01003767 spec->line_out_muted = on;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003768 do_automute(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
Takashi Iwai2c12c302013-01-10 09:33:29 +01003769 spec->autocfg.line_out_pins, on);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003770}
Takashi Iwai5d550e12012-12-19 15:16:44 +01003771EXPORT_SYMBOL_HDA(snd_hda_gen_update_outputs);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003772
3773static void call_update_outputs(struct hda_codec *codec)
3774{
3775 struct hda_gen_spec *spec = codec->spec;
3776 if (spec->automute_hook)
3777 spec->automute_hook(codec);
3778 else
Takashi Iwai5d550e12012-12-19 15:16:44 +01003779 snd_hda_gen_update_outputs(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003780}
3781
3782/* standard HP-automute helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003783void snd_hda_gen_hp_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003784{
3785 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai92603c52013-01-22 07:46:31 +01003786 hda_nid_t *pins = spec->autocfg.hp_pins;
3787 int num_pins = ARRAY_SIZE(spec->autocfg.hp_pins);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003788
Takashi Iwai92603c52013-01-22 07:46:31 +01003789 /* No detection for the first HP jack during indep-HP mode */
3790 if (spec->indep_hp_enabled) {
3791 pins++;
3792 num_pins--;
3793 }
3794
3795 spec->hp_jack_present = detect_jacks(codec, num_pins, pins);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003796 if (!spec->detect_hp || (!spec->automute_speaker && !spec->automute_lo))
3797 return;
3798 call_update_outputs(codec);
3799}
Takashi Iwai5d550e12012-12-19 15:16:44 +01003800EXPORT_SYMBOL_HDA(snd_hda_gen_hp_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003801
3802/* standard line-out-automute helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003803void snd_hda_gen_line_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003804{
3805 struct hda_gen_spec *spec = codec->spec;
3806
3807 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
3808 return;
3809 /* check LO jack only when it's different from HP */
3810 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0])
3811 return;
3812
3813 spec->line_jack_present =
3814 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
3815 spec->autocfg.line_out_pins);
3816 if (!spec->automute_speaker || !spec->detect_lo)
3817 return;
3818 call_update_outputs(codec);
3819}
Takashi Iwai5d550e12012-12-19 15:16:44 +01003820EXPORT_SYMBOL_HDA(snd_hda_gen_line_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003821
3822/* standard mic auto-switch helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003823void snd_hda_gen_mic_autoswitch(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003824{
3825 struct hda_gen_spec *spec = codec->spec;
3826 int i;
3827
3828 if (!spec->auto_mic)
3829 return;
3830
3831 for (i = spec->am_num_entries - 1; i > 0; i--) {
Takashi Iwai0b4df932013-01-10 09:45:13 +01003832 hda_nid_t pin = spec->am_entry[i].pin;
3833 /* don't detect pins retasked as outputs */
3834 if (snd_hda_codec_get_pin_target(codec, pin) & AC_PINCTL_OUT_EN)
3835 continue;
3836 if (snd_hda_jack_detect(codec, pin)) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003837 mux_select(codec, 0, spec->am_entry[i].idx);
3838 return;
3839 }
3840 }
3841 mux_select(codec, 0, spec->am_entry[0].idx);
3842}
Takashi Iwai5d550e12012-12-19 15:16:44 +01003843EXPORT_SYMBOL_HDA(snd_hda_gen_mic_autoswitch);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003844
Takashi Iwaia5cc2502013-01-16 18:08:55 +01003845/* update jack retasking */
3846static void update_automute_all(struct hda_codec *codec)
3847{
3848 struct hda_gen_spec *spec = codec->spec;
3849
Takashi Iwai8ba955c2013-03-07 18:40:58 +01003850 update_hp_automute_hook(codec);
Takashi Iwaia5cc2502013-01-16 18:08:55 +01003851 if (spec->line_automute_hook)
3852 spec->line_automute_hook(codec, NULL);
3853 else
3854 snd_hda_gen_line_automute(codec, NULL);
3855 if (spec->mic_autoswitch_hook)
3856 spec->mic_autoswitch_hook(codec, NULL);
3857 else
3858 snd_hda_gen_mic_autoswitch(codec, NULL);
3859}
3860
Takashi Iwai352f7f92012-12-19 12:52:06 +01003861/*
3862 * Auto-Mute mode mixer enum support
3863 */
3864static int automute_mode_info(struct snd_kcontrol *kcontrol,
3865 struct snd_ctl_elem_info *uinfo)
3866{
3867 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3868 struct hda_gen_spec *spec = codec->spec;
3869 static const char * const texts3[] = {
3870 "Disabled", "Speaker Only", "Line Out+Speaker"
Takashi Iwai071c73a2006-08-23 18:34:06 +02003871 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003872
Takashi Iwai352f7f92012-12-19 12:52:06 +01003873 if (spec->automute_speaker_possible && spec->automute_lo_possible)
3874 return snd_hda_enum_helper_info(kcontrol, uinfo, 3, texts3);
3875 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
3876}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003877
Takashi Iwai352f7f92012-12-19 12:52:06 +01003878static int automute_mode_get(struct snd_kcontrol *kcontrol,
3879 struct snd_ctl_elem_value *ucontrol)
3880{
3881 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3882 struct hda_gen_spec *spec = codec->spec;
3883 unsigned int val = 0;
3884 if (spec->automute_speaker)
3885 val++;
3886 if (spec->automute_lo)
3887 val++;
Takashi Iwai071c73a2006-08-23 18:34:06 +02003888
Takashi Iwai352f7f92012-12-19 12:52:06 +01003889 ucontrol->value.enumerated.item[0] = val;
3890 return 0;
3891}
3892
3893static int automute_mode_put(struct snd_kcontrol *kcontrol,
3894 struct snd_ctl_elem_value *ucontrol)
3895{
3896 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3897 struct hda_gen_spec *spec = codec->spec;
3898
3899 switch (ucontrol->value.enumerated.item[0]) {
3900 case 0:
3901 if (!spec->automute_speaker && !spec->automute_lo)
3902 return 0;
3903 spec->automute_speaker = 0;
3904 spec->automute_lo = 0;
3905 break;
3906 case 1:
3907 if (spec->automute_speaker_possible) {
3908 if (!spec->automute_lo && spec->automute_speaker)
3909 return 0;
3910 spec->automute_speaker = 1;
3911 spec->automute_lo = 0;
3912 } else if (spec->automute_lo_possible) {
3913 if (spec->automute_lo)
3914 return 0;
3915 spec->automute_lo = 1;
3916 } else
3917 return -EINVAL;
3918 break;
3919 case 2:
3920 if (!spec->automute_lo_possible || !spec->automute_speaker_possible)
3921 return -EINVAL;
3922 if (spec->automute_speaker && spec->automute_lo)
3923 return 0;
3924 spec->automute_speaker = 1;
3925 spec->automute_lo = 1;
3926 break;
3927 default:
3928 return -EINVAL;
3929 }
3930 call_update_outputs(codec);
3931 return 1;
3932}
3933
3934static const struct snd_kcontrol_new automute_mode_enum = {
3935 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3936 .name = "Auto-Mute Mode",
3937 .info = automute_mode_info,
3938 .get = automute_mode_get,
3939 .put = automute_mode_put,
3940};
3941
3942static int add_automute_mode_enum(struct hda_codec *codec)
3943{
3944 struct hda_gen_spec *spec = codec->spec;
3945
Takashi Iwai12c93df2012-12-19 14:38:33 +01003946 if (!snd_hda_gen_add_kctl(spec, NULL, &automute_mode_enum))
Takashi Iwai352f7f92012-12-19 12:52:06 +01003947 return -ENOMEM;
3948 return 0;
3949}
3950
3951/*
3952 * Check the availability of HP/line-out auto-mute;
3953 * Set up appropriately if really supported
3954 */
3955static int check_auto_mute_availability(struct hda_codec *codec)
3956{
3957 struct hda_gen_spec *spec = codec->spec;
3958 struct auto_pin_cfg *cfg = &spec->autocfg;
3959 int present = 0;
3960 int i, err;
3961
Takashi Iwaif72706b2013-01-16 18:20:07 +01003962 if (spec->suppress_auto_mute)
3963 return 0;
3964
Takashi Iwai352f7f92012-12-19 12:52:06 +01003965 if (cfg->hp_pins[0])
3966 present++;
3967 if (cfg->line_out_pins[0])
3968 present++;
3969 if (cfg->speaker_pins[0])
3970 present++;
3971 if (present < 2) /* need two different output types */
Takashi Iwai071c73a2006-08-23 18:34:06 +02003972 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003973
3974 if (!cfg->speaker_pins[0] &&
3975 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
3976 memcpy(cfg->speaker_pins, cfg->line_out_pins,
3977 sizeof(cfg->speaker_pins));
3978 cfg->speaker_outs = cfg->line_outs;
Takashi Iwai071c73a2006-08-23 18:34:06 +02003979 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003980
Takashi Iwai352f7f92012-12-19 12:52:06 +01003981 if (!cfg->hp_pins[0] &&
3982 cfg->line_out_type == AUTO_PIN_HP_OUT) {
3983 memcpy(cfg->hp_pins, cfg->line_out_pins,
3984 sizeof(cfg->hp_pins));
3985 cfg->hp_outs = cfg->line_outs;
3986 }
3987
3988 for (i = 0; i < cfg->hp_outs; i++) {
3989 hda_nid_t nid = cfg->hp_pins[i];
3990 if (!is_jack_detectable(codec, nid))
3991 continue;
3992 snd_printdd("hda-codec: Enable HP auto-muting on NID 0x%x\n",
3993 nid);
3994 snd_hda_jack_detect_enable_callback(codec, nid, HDA_GEN_HP_EVENT,
Takashi Iwai2e03e952013-01-03 15:55:06 +01003995 spec->hp_automute_hook ?
3996 spec->hp_automute_hook :
Takashi Iwai5d550e12012-12-19 15:16:44 +01003997 snd_hda_gen_hp_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003998 spec->detect_hp = 1;
3999 }
4000
4001 if (cfg->line_out_type == AUTO_PIN_LINE_OUT && cfg->line_outs) {
4002 if (cfg->speaker_outs)
4003 for (i = 0; i < cfg->line_outs; i++) {
4004 hda_nid_t nid = cfg->line_out_pins[i];
4005 if (!is_jack_detectable(codec, nid))
4006 continue;
4007 snd_printdd("hda-codec: Enable Line-Out auto-muting on NID 0x%x\n", nid);
4008 snd_hda_jack_detect_enable_callback(codec, nid,
4009 HDA_GEN_FRONT_EVENT,
Takashi Iwai2e03e952013-01-03 15:55:06 +01004010 spec->line_automute_hook ?
4011 spec->line_automute_hook :
Takashi Iwai5d550e12012-12-19 15:16:44 +01004012 snd_hda_gen_line_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004013 spec->detect_lo = 1;
4014 }
4015 spec->automute_lo_possible = spec->detect_hp;
4016 }
4017
4018 spec->automute_speaker_possible = cfg->speaker_outs &&
4019 (spec->detect_hp || spec->detect_lo);
4020
4021 spec->automute_lo = spec->automute_lo_possible;
4022 spec->automute_speaker = spec->automute_speaker_possible;
4023
4024 if (spec->automute_speaker_possible || spec->automute_lo_possible) {
4025 /* create a control for automute mode */
4026 err = add_automute_mode_enum(codec);
4027 if (err < 0)
4028 return err;
4029 }
4030 return 0;
4031}
4032
Takashi Iwai352f7f92012-12-19 12:52:06 +01004033/* check whether all auto-mic pins are valid; setup indices if OK */
4034static bool auto_mic_check_imux(struct hda_codec *codec)
4035{
4036 struct hda_gen_spec *spec = codec->spec;
4037 const struct hda_input_mux *imux;
4038 int i;
4039
4040 imux = &spec->input_mux;
4041 for (i = 0; i < spec->am_num_entries; i++) {
4042 spec->am_entry[i].idx =
4043 find_idx_in_nid_list(spec->am_entry[i].pin,
4044 spec->imux_pins, imux->num_items);
4045 if (spec->am_entry[i].idx < 0)
4046 return false; /* no corresponding imux */
4047 }
4048
4049 /* we don't need the jack detection for the first pin */
4050 for (i = 1; i < spec->am_num_entries; i++)
4051 snd_hda_jack_detect_enable_callback(codec,
4052 spec->am_entry[i].pin,
4053 HDA_GEN_MIC_EVENT,
Takashi Iwai2e03e952013-01-03 15:55:06 +01004054 spec->mic_autoswitch_hook ?
4055 spec->mic_autoswitch_hook :
Takashi Iwai5d550e12012-12-19 15:16:44 +01004056 snd_hda_gen_mic_autoswitch);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004057 return true;
4058}
4059
4060static int compare_attr(const void *ap, const void *bp)
4061{
4062 const struct automic_entry *a = ap;
4063 const struct automic_entry *b = bp;
4064 return (int)(a->attr - b->attr);
4065}
4066
4067/*
4068 * Check the availability of auto-mic switch;
4069 * Set up if really supported
4070 */
4071static int check_auto_mic_availability(struct hda_codec *codec)
4072{
4073 struct hda_gen_spec *spec = codec->spec;
4074 struct auto_pin_cfg *cfg = &spec->autocfg;
4075 unsigned int types;
4076 int i, num_pins;
4077
Takashi Iwaid12daf62013-01-07 16:32:11 +01004078 if (spec->suppress_auto_mic)
4079 return 0;
4080
Takashi Iwai352f7f92012-12-19 12:52:06 +01004081 types = 0;
4082 num_pins = 0;
4083 for (i = 0; i < cfg->num_inputs; i++) {
4084 hda_nid_t nid = cfg->inputs[i].pin;
4085 unsigned int attr;
4086 attr = snd_hda_codec_get_pincfg(codec, nid);
4087 attr = snd_hda_get_input_pin_attr(attr);
4088 if (types & (1 << attr))
4089 return 0; /* already occupied */
4090 switch (attr) {
4091 case INPUT_PIN_ATTR_INT:
4092 if (cfg->inputs[i].type != AUTO_PIN_MIC)
4093 return 0; /* invalid type */
4094 break;
4095 case INPUT_PIN_ATTR_UNUSED:
4096 return 0; /* invalid entry */
4097 default:
4098 if (cfg->inputs[i].type > AUTO_PIN_LINE_IN)
4099 return 0; /* invalid type */
4100 if (!spec->line_in_auto_switch &&
4101 cfg->inputs[i].type != AUTO_PIN_MIC)
4102 return 0; /* only mic is allowed */
4103 if (!is_jack_detectable(codec, nid))
4104 return 0; /* no unsol support */
4105 break;
4106 }
4107 if (num_pins >= MAX_AUTO_MIC_PINS)
4108 return 0;
4109 types |= (1 << attr);
4110 spec->am_entry[num_pins].pin = nid;
4111 spec->am_entry[num_pins].attr = attr;
4112 num_pins++;
4113 }
4114
4115 if (num_pins < 2)
4116 return 0;
4117
4118 spec->am_num_entries = num_pins;
4119 /* sort the am_entry in the order of attr so that the pin with a
4120 * higher attr will be selected when the jack is plugged.
4121 */
4122 sort(spec->am_entry, num_pins, sizeof(spec->am_entry[0]),
4123 compare_attr, NULL);
4124
4125 if (!auto_mic_check_imux(codec))
4126 return 0;
4127
4128 spec->auto_mic = 1;
4129 spec->num_adc_nids = 1;
4130 spec->cur_mux[0] = spec->am_entry[0].idx;
4131 snd_printdd("hda-codec: Enable auto-mic switch on NID 0x%x/0x%x/0x%x\n",
4132 spec->am_entry[0].pin,
4133 spec->am_entry[1].pin,
4134 spec->am_entry[2].pin);
4135
4136 return 0;
4137}
4138
Takashi Iwai55196ff2013-01-24 17:32:56 +01004139/* power_filter hook; make inactive widgets into power down */
4140static unsigned int snd_hda_gen_path_power_filter(struct hda_codec *codec,
4141 hda_nid_t nid,
4142 unsigned int power_state)
4143{
4144 if (power_state != AC_PWRST_D0)
4145 return power_state;
4146 if (get_wcaps_type(get_wcaps(codec, nid)) >= AC_WID_POWER)
4147 return power_state;
4148 if (is_active_nid(codec, nid, HDA_OUTPUT, 0))
4149 return power_state;
4150 return AC_PWRST_D3;
4151}
4152
Takashi Iwai352f7f92012-12-19 12:52:06 +01004153
Takashi Iwai9eb413e2012-12-19 14:41:21 +01004154/*
4155 * Parse the given BIOS configuration and set up the hda_gen_spec
4156 *
4157 * return 1 if successful, 0 if the proper config is not found,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004158 * or a negative error code
4159 */
4160int snd_hda_gen_parse_auto_config(struct hda_codec *codec,
Takashi Iwai9eb413e2012-12-19 14:41:21 +01004161 struct auto_pin_cfg *cfg)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004162{
4163 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004164 int err;
4165
Takashi Iwai1c70a582013-01-11 17:48:22 +01004166 parse_user_hints(codec);
4167
Takashi Iwaie4a395e2013-01-23 17:00:31 +01004168 if (spec->mixer_nid && !spec->mixer_merge_nid)
4169 spec->mixer_merge_nid = spec->mixer_nid;
4170
Takashi Iwai9eb413e2012-12-19 14:41:21 +01004171 if (cfg != &spec->autocfg) {
4172 spec->autocfg = *cfg;
4173 cfg = &spec->autocfg;
4174 }
4175
Takashi Iwai98bd1112013-03-22 14:53:50 +01004176 if (!spec->main_out_badness)
4177 spec->main_out_badness = &hda_main_out_badness;
4178 if (!spec->extra_out_badness)
4179 spec->extra_out_badness = &hda_extra_out_badness;
4180
David Henningsson6fc4cb92013-01-16 15:58:45 +01004181 fill_all_dac_nids(codec);
4182
Takashi Iwai352f7f92012-12-19 12:52:06 +01004183 if (!cfg->line_outs) {
4184 if (cfg->dig_outs || cfg->dig_in_pin) {
4185 spec->multiout.max_channels = 2;
4186 spec->no_analog = 1;
4187 goto dig_only;
4188 }
4189 return 0; /* can't find valid BIOS pin config */
4190 }
4191
4192 if (!spec->no_primary_hp &&
4193 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT &&
4194 cfg->line_outs <= cfg->hp_outs) {
4195 /* use HP as primary out */
4196 cfg->speaker_outs = cfg->line_outs;
4197 memcpy(cfg->speaker_pins, cfg->line_out_pins,
4198 sizeof(cfg->speaker_pins));
4199 cfg->line_outs = cfg->hp_outs;
4200 memcpy(cfg->line_out_pins, cfg->hp_pins, sizeof(cfg->hp_pins));
4201 cfg->hp_outs = 0;
4202 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
4203 cfg->line_out_type = AUTO_PIN_HP_OUT;
4204 }
4205
4206 err = parse_output_paths(codec);
4207 if (err < 0)
4208 return err;
4209 err = create_multi_channel_mode(codec);
4210 if (err < 0)
4211 return err;
4212 err = create_multi_out_ctls(codec, cfg);
4213 if (err < 0)
4214 return err;
4215 err = create_hp_out_ctls(codec);
4216 if (err < 0)
4217 return err;
4218 err = create_speaker_out_ctls(codec);
4219 if (err < 0)
4220 return err;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004221 err = create_indep_hp_ctls(codec);
4222 if (err < 0)
4223 return err;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01004224 err = create_loopback_mixing_ctl(codec);
4225 if (err < 0)
4226 return err;
Takashi Iwai967303d2013-02-19 17:12:42 +01004227 err = create_hp_mic(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004228 if (err < 0)
4229 return err;
4230 err = create_input_ctls(codec);
Takashi Iwaid13bd412008-07-30 15:01:45 +02004231 if (err < 0)
Takashi Iwai071c73a2006-08-23 18:34:06 +02004232 return err;
4233
Takashi Iwaia07a9492013-01-07 16:44:06 +01004234 spec->const_channel_count = spec->ext_channel_count;
4235 /* check the multiple speaker and headphone pins */
4236 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
4237 spec->const_channel_count = max(spec->const_channel_count,
4238 cfg->speaker_outs * 2);
4239 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
4240 spec->const_channel_count = max(spec->const_channel_count,
4241 cfg->hp_outs * 2);
4242 spec->multiout.max_channels = max(spec->ext_channel_count,
4243 spec->const_channel_count);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004244
4245 err = check_auto_mute_availability(codec);
4246 if (err < 0)
4247 return err;
4248
4249 err = check_dyn_adc_switch(codec);
4250 if (err < 0)
4251 return err;
4252
Takashi Iwai967303d2013-02-19 17:12:42 +01004253 err = check_auto_mic_availability(codec);
4254 if (err < 0)
4255 return err;
Takashi Iwai071c73a2006-08-23 18:34:06 +02004256
Takashi Iwai352f7f92012-12-19 12:52:06 +01004257 err = create_capture_mixers(codec);
4258 if (err < 0)
4259 return err;
4260
4261 err = parse_mic_boost(codec);
4262 if (err < 0)
4263 return err;
4264
Takashi Iwaif811c3c2013-03-07 18:32:59 +01004265 if (spec->add_jack_modes) {
Takashi Iwai978e77e2013-01-10 16:57:58 +01004266 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
4267 err = create_out_jack_modes(codec, cfg->line_outs,
4268 cfg->line_out_pins);
4269 if (err < 0)
4270 return err;
4271 }
4272 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
4273 err = create_out_jack_modes(codec, cfg->hp_outs,
4274 cfg->hp_pins);
4275 if (err < 0)
4276 return err;
4277 }
4278 }
4279
Takashi Iwai352f7f92012-12-19 12:52:06 +01004280 dig_only:
4281 parse_digital(codec);
4282
Takashi Iwai55196ff2013-01-24 17:32:56 +01004283 if (spec->power_down_unused)
4284 codec->power_filter = snd_hda_gen_path_power_filter;
4285
Takashi Iwai7504b6c2013-03-18 11:25:51 +01004286 if (!spec->no_analog && spec->beep_nid) {
4287 err = snd_hda_attach_beep_device(codec, spec->beep_nid);
4288 if (err < 0)
4289 return err;
4290 }
4291
Takashi Iwai352f7f92012-12-19 12:52:06 +01004292 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004293}
Takashi Iwai352f7f92012-12-19 12:52:06 +01004294EXPORT_SYMBOL_HDA(snd_hda_gen_parse_auto_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004295
4296
4297/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01004298 * Build control elements
Linus Torvalds1da177e2005-04-16 15:20:36 -07004299 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01004300
4301/* slave controls for virtual master */
4302static const char * const slave_pfxs[] = {
4303 "Front", "Surround", "Center", "LFE", "Side",
4304 "Headphone", "Speaker", "Mono", "Line Out",
4305 "CLFE", "Bass Speaker", "PCM",
Takashi Iwaiee79c692013-01-07 09:57:42 +01004306 "Speaker Front", "Speaker Surround", "Speaker CLFE", "Speaker Side",
4307 "Headphone Front", "Headphone Surround", "Headphone CLFE",
4308 "Headphone Side",
Takashi Iwai352f7f92012-12-19 12:52:06 +01004309 NULL,
4310};
4311
4312int snd_hda_gen_build_controls(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004313{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004314 struct hda_gen_spec *spec = codec->spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004315 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004316
Takashi Iwai36502d02012-12-19 15:15:10 +01004317 if (spec->kctls.used) {
4318 err = snd_hda_add_new_ctls(codec, spec->kctls.list);
4319 if (err < 0)
4320 return err;
4321 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004322
Takashi Iwai352f7f92012-12-19 12:52:06 +01004323 if (spec->multiout.dig_out_nid) {
4324 err = snd_hda_create_dig_out_ctls(codec,
4325 spec->multiout.dig_out_nid,
4326 spec->multiout.dig_out_nid,
4327 spec->pcm_rec[1].pcm_type);
4328 if (err < 0)
4329 return err;
4330 if (!spec->no_analog) {
4331 err = snd_hda_create_spdif_share_sw(codec,
4332 &spec->multiout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004333 if (err < 0)
4334 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004335 spec->multiout.share_spdif = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004336 }
4337 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01004338 if (spec->dig_in_nid) {
4339 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
4340 if (err < 0)
4341 return err;
4342 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004343
Takashi Iwai352f7f92012-12-19 12:52:06 +01004344 /* if we have no master control, let's create it */
4345 if (!spec->no_analog &&
4346 !snd_hda_find_mixer_ctl(codec, "Master Playback Volume")) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01004347 err = snd_hda_add_vmaster(codec, "Master Playback Volume",
Takashi Iwai7a71bbf2013-01-17 10:25:15 +01004348 spec->vmaster_tlv, slave_pfxs,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004349 "Playback Volume");
4350 if (err < 0)
4351 return err;
4352 }
4353 if (!spec->no_analog &&
4354 !snd_hda_find_mixer_ctl(codec, "Master Playback Switch")) {
4355 err = __snd_hda_add_vmaster(codec, "Master Playback Switch",
4356 NULL, slave_pfxs,
4357 "Playback Switch",
4358 true, &spec->vmaster_mute.sw_kctl);
4359 if (err < 0)
4360 return err;
4361 if (spec->vmaster_mute.hook)
Takashi Iwaifd25a972012-12-20 14:57:18 +01004362 snd_hda_add_vmaster_hook(codec, &spec->vmaster_mute,
4363 spec->vmaster_mute_enum);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004364 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004365
Takashi Iwai352f7f92012-12-19 12:52:06 +01004366 free_kctls(spec); /* no longer needed */
4367
Takashi Iwai967303d2013-02-19 17:12:42 +01004368 if (spec->hp_mic_pin) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01004369 int err;
Takashi Iwai967303d2013-02-19 17:12:42 +01004370 int nid = spec->hp_mic_pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004371 err = snd_hda_jack_add_kctl(codec, nid, "Headphone Mic", 0);
4372 if (err < 0)
4373 return err;
4374 err = snd_hda_jack_detect_enable(codec, nid, 0);
4375 if (err < 0)
4376 return err;
4377 }
4378
4379 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
4380 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004381 return err;
4382
4383 return 0;
4384}
Takashi Iwai352f7f92012-12-19 12:52:06 +01004385EXPORT_SYMBOL_HDA(snd_hda_gen_build_controls);
4386
Linus Torvalds1da177e2005-04-16 15:20:36 -07004387
4388/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01004389 * PCM definitions
Linus Torvalds1da177e2005-04-16 15:20:36 -07004390 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004391
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004392static void call_pcm_playback_hook(struct hda_pcm_stream *hinfo,
4393 struct hda_codec *codec,
4394 struct snd_pcm_substream *substream,
4395 int action)
4396{
4397 struct hda_gen_spec *spec = codec->spec;
4398 if (spec->pcm_playback_hook)
4399 spec->pcm_playback_hook(hinfo, codec, substream, action);
4400}
4401
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004402static void call_pcm_capture_hook(struct hda_pcm_stream *hinfo,
4403 struct hda_codec *codec,
4404 struct snd_pcm_substream *substream,
4405 int action)
4406{
4407 struct hda_gen_spec *spec = codec->spec;
4408 if (spec->pcm_capture_hook)
4409 spec->pcm_capture_hook(hinfo, codec, substream, action);
4410}
4411
Takashi Iwai352f7f92012-12-19 12:52:06 +01004412/*
4413 * Analog playback callbacks
4414 */
4415static int playback_pcm_open(struct hda_pcm_stream *hinfo,
4416 struct hda_codec *codec,
4417 struct snd_pcm_substream *substream)
4418{
4419 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004420 int err;
4421
4422 mutex_lock(&spec->pcm_mutex);
4423 err = snd_hda_multi_out_analog_open(codec,
4424 &spec->multiout, substream,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004425 hinfo);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004426 if (!err) {
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004427 spec->active_streams |= 1 << STREAM_MULTI_OUT;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004428 call_pcm_playback_hook(hinfo, codec, substream,
4429 HDA_GEN_PCM_ACT_OPEN);
4430 }
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004431 mutex_unlock(&spec->pcm_mutex);
4432 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004433}
4434
4435static int playback_pcm_prepare(struct hda_pcm_stream *hinfo,
Takashi Iwai97ec5582006-03-21 11:29:07 +01004436 struct hda_codec *codec,
4437 unsigned int stream_tag,
4438 unsigned int format,
4439 struct snd_pcm_substream *substream)
4440{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004441 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004442 int err;
4443
4444 err = snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
4445 stream_tag, format, substream);
4446 if (!err)
4447 call_pcm_playback_hook(hinfo, codec, substream,
4448 HDA_GEN_PCM_ACT_PREPARE);
4449 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004450}
Takashi Iwai97ec5582006-03-21 11:29:07 +01004451
Takashi Iwai352f7f92012-12-19 12:52:06 +01004452static int playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
4453 struct hda_codec *codec,
4454 struct snd_pcm_substream *substream)
4455{
4456 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004457 int err;
4458
4459 err = snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
4460 if (!err)
4461 call_pcm_playback_hook(hinfo, codec, substream,
4462 HDA_GEN_PCM_ACT_CLEANUP);
4463 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004464}
4465
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004466static int playback_pcm_close(struct hda_pcm_stream *hinfo,
4467 struct hda_codec *codec,
4468 struct snd_pcm_substream *substream)
4469{
4470 struct hda_gen_spec *spec = codec->spec;
4471 mutex_lock(&spec->pcm_mutex);
4472 spec->active_streams &= ~(1 << STREAM_MULTI_OUT);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004473 call_pcm_playback_hook(hinfo, codec, substream,
4474 HDA_GEN_PCM_ACT_CLOSE);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004475 mutex_unlock(&spec->pcm_mutex);
4476 return 0;
4477}
4478
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004479static int capture_pcm_open(struct hda_pcm_stream *hinfo,
4480 struct hda_codec *codec,
4481 struct snd_pcm_substream *substream)
4482{
4483 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_OPEN);
4484 return 0;
4485}
4486
4487static int capture_pcm_prepare(struct hda_pcm_stream *hinfo,
4488 struct hda_codec *codec,
4489 unsigned int stream_tag,
4490 unsigned int format,
4491 struct snd_pcm_substream *substream)
4492{
4493 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4494 call_pcm_capture_hook(hinfo, codec, substream,
4495 HDA_GEN_PCM_ACT_PREPARE);
4496 return 0;
4497}
4498
4499static int capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
4500 struct hda_codec *codec,
4501 struct snd_pcm_substream *substream)
4502{
4503 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
4504 call_pcm_capture_hook(hinfo, codec, substream,
4505 HDA_GEN_PCM_ACT_CLEANUP);
4506 return 0;
4507}
4508
4509static int capture_pcm_close(struct hda_pcm_stream *hinfo,
4510 struct hda_codec *codec,
4511 struct snd_pcm_substream *substream)
4512{
4513 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_CLOSE);
4514 return 0;
4515}
4516
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004517static int alt_playback_pcm_open(struct hda_pcm_stream *hinfo,
4518 struct hda_codec *codec,
4519 struct snd_pcm_substream *substream)
4520{
4521 struct hda_gen_spec *spec = codec->spec;
4522 int err = 0;
4523
4524 mutex_lock(&spec->pcm_mutex);
4525 if (!spec->indep_hp_enabled)
4526 err = -EBUSY;
4527 else
4528 spec->active_streams |= 1 << STREAM_INDEP_HP;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004529 call_pcm_playback_hook(hinfo, codec, substream,
4530 HDA_GEN_PCM_ACT_OPEN);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004531 mutex_unlock(&spec->pcm_mutex);
4532 return err;
4533}
4534
4535static int alt_playback_pcm_close(struct hda_pcm_stream *hinfo,
4536 struct hda_codec *codec,
4537 struct snd_pcm_substream *substream)
4538{
4539 struct hda_gen_spec *spec = codec->spec;
4540 mutex_lock(&spec->pcm_mutex);
4541 spec->active_streams &= ~(1 << STREAM_INDEP_HP);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004542 call_pcm_playback_hook(hinfo, codec, substream,
4543 HDA_GEN_PCM_ACT_CLOSE);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004544 mutex_unlock(&spec->pcm_mutex);
4545 return 0;
4546}
4547
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004548static int alt_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
4549 struct hda_codec *codec,
4550 unsigned int stream_tag,
4551 unsigned int format,
4552 struct snd_pcm_substream *substream)
4553{
4554 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4555 call_pcm_playback_hook(hinfo, codec, substream,
4556 HDA_GEN_PCM_ACT_PREPARE);
4557 return 0;
4558}
4559
4560static int alt_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
4561 struct hda_codec *codec,
4562 struct snd_pcm_substream *substream)
4563{
4564 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
4565 call_pcm_playback_hook(hinfo, codec, substream,
4566 HDA_GEN_PCM_ACT_CLEANUP);
4567 return 0;
4568}
4569
Takashi Iwai352f7f92012-12-19 12:52:06 +01004570/*
4571 * Digital out
4572 */
4573static int dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
4574 struct hda_codec *codec,
4575 struct snd_pcm_substream *substream)
4576{
4577 struct hda_gen_spec *spec = codec->spec;
4578 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
4579}
4580
4581static int dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
4582 struct hda_codec *codec,
4583 unsigned int stream_tag,
4584 unsigned int format,
4585 struct snd_pcm_substream *substream)
4586{
4587 struct hda_gen_spec *spec = codec->spec;
4588 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
4589 stream_tag, format, substream);
4590}
4591
4592static int dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
4593 struct hda_codec *codec,
4594 struct snd_pcm_substream *substream)
4595{
4596 struct hda_gen_spec *spec = codec->spec;
4597 return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
4598}
4599
4600static int dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
4601 struct hda_codec *codec,
4602 struct snd_pcm_substream *substream)
4603{
4604 struct hda_gen_spec *spec = codec->spec;
4605 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
4606}
4607
4608/*
4609 * Analog capture
4610 */
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004611#define alt_capture_pcm_open capture_pcm_open
4612#define alt_capture_pcm_close capture_pcm_close
4613
Takashi Iwai352f7f92012-12-19 12:52:06 +01004614static int alt_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
4615 struct hda_codec *codec,
4616 unsigned int stream_tag,
4617 unsigned int format,
4618 struct snd_pcm_substream *substream)
4619{
4620 struct hda_gen_spec *spec = codec->spec;
4621
4622 snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number + 1],
Takashi Iwai97ec5582006-03-21 11:29:07 +01004623 stream_tag, 0, format);
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004624 call_pcm_capture_hook(hinfo, codec, substream,
4625 HDA_GEN_PCM_ACT_PREPARE);
Takashi Iwai97ec5582006-03-21 11:29:07 +01004626 return 0;
4627}
4628
Takashi Iwai352f7f92012-12-19 12:52:06 +01004629static int alt_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
4630 struct hda_codec *codec,
4631 struct snd_pcm_substream *substream)
Takashi Iwai97ec5582006-03-21 11:29:07 +01004632{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004633 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai97ec5582006-03-21 11:29:07 +01004634
Takashi Iwai352f7f92012-12-19 12:52:06 +01004635 snd_hda_codec_cleanup_stream(codec,
4636 spec->adc_nids[substream->number + 1]);
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004637 call_pcm_capture_hook(hinfo, codec, substream,
4638 HDA_GEN_PCM_ACT_CLEANUP);
Takashi Iwai97ec5582006-03-21 11:29:07 +01004639 return 0;
4640}
4641
Takashi Iwai352f7f92012-12-19 12:52:06 +01004642/*
4643 */
4644static const struct hda_pcm_stream pcm_analog_playback = {
4645 .substreams = 1,
4646 .channels_min = 2,
4647 .channels_max = 8,
4648 /* NID is set in build_pcms */
4649 .ops = {
4650 .open = playback_pcm_open,
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004651 .close = playback_pcm_close,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004652 .prepare = playback_pcm_prepare,
4653 .cleanup = playback_pcm_cleanup
4654 },
4655};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004656
Takashi Iwai352f7f92012-12-19 12:52:06 +01004657static const struct hda_pcm_stream pcm_analog_capture = {
4658 .substreams = 1,
4659 .channels_min = 2,
4660 .channels_max = 2,
4661 /* NID is set in build_pcms */
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004662 .ops = {
4663 .open = capture_pcm_open,
4664 .close = capture_pcm_close,
4665 .prepare = capture_pcm_prepare,
4666 .cleanup = capture_pcm_cleanup
4667 },
Takashi Iwai352f7f92012-12-19 12:52:06 +01004668};
4669
4670static const struct hda_pcm_stream pcm_analog_alt_playback = {
4671 .substreams = 1,
4672 .channels_min = 2,
4673 .channels_max = 2,
4674 /* NID is set in build_pcms */
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004675 .ops = {
4676 .open = alt_playback_pcm_open,
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004677 .close = alt_playback_pcm_close,
4678 .prepare = alt_playback_pcm_prepare,
4679 .cleanup = alt_playback_pcm_cleanup
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004680 },
Takashi Iwai352f7f92012-12-19 12:52:06 +01004681};
4682
4683static const struct hda_pcm_stream pcm_analog_alt_capture = {
4684 .substreams = 2, /* can be overridden */
4685 .channels_min = 2,
4686 .channels_max = 2,
4687 /* NID is set in build_pcms */
4688 .ops = {
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004689 .open = alt_capture_pcm_open,
4690 .close = alt_capture_pcm_close,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004691 .prepare = alt_capture_pcm_prepare,
4692 .cleanup = alt_capture_pcm_cleanup
4693 },
4694};
4695
4696static const struct hda_pcm_stream pcm_digital_playback = {
4697 .substreams = 1,
4698 .channels_min = 2,
4699 .channels_max = 2,
4700 /* NID is set in build_pcms */
4701 .ops = {
4702 .open = dig_playback_pcm_open,
4703 .close = dig_playback_pcm_close,
4704 .prepare = dig_playback_pcm_prepare,
4705 .cleanup = dig_playback_pcm_cleanup
4706 },
4707};
4708
4709static const struct hda_pcm_stream pcm_digital_capture = {
4710 .substreams = 1,
4711 .channels_min = 2,
4712 .channels_max = 2,
4713 /* NID is set in build_pcms */
4714};
4715
4716/* Used by build_pcms to flag that a PCM has no playback stream */
4717static const struct hda_pcm_stream pcm_null_stream = {
4718 .substreams = 0,
4719 .channels_min = 0,
4720 .channels_max = 0,
4721};
4722
4723/*
4724 * dynamic changing ADC PCM streams
4725 */
4726static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur)
4727{
4728 struct hda_gen_spec *spec = codec->spec;
4729 hda_nid_t new_adc = spec->adc_nids[spec->dyn_adc_idx[cur]];
4730
4731 if (spec->cur_adc && spec->cur_adc != new_adc) {
4732 /* stream is running, let's swap the current ADC */
4733 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
4734 spec->cur_adc = new_adc;
4735 snd_hda_codec_setup_stream(codec, new_adc,
4736 spec->cur_adc_stream_tag, 0,
4737 spec->cur_adc_format);
4738 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004739 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01004740 return false;
4741}
4742
4743/* analog capture with dynamic dual-adc changes */
4744static int dyn_adc_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
4745 struct hda_codec *codec,
4746 unsigned int stream_tag,
4747 unsigned int format,
4748 struct snd_pcm_substream *substream)
4749{
4750 struct hda_gen_spec *spec = codec->spec;
4751 spec->cur_adc = spec->adc_nids[spec->dyn_adc_idx[spec->cur_mux[0]]];
4752 spec->cur_adc_stream_tag = stream_tag;
4753 spec->cur_adc_format = format;
4754 snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
4755 return 0;
4756}
4757
4758static int dyn_adc_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
4759 struct hda_codec *codec,
4760 struct snd_pcm_substream *substream)
4761{
4762 struct hda_gen_spec *spec = codec->spec;
4763 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
4764 spec->cur_adc = 0;
4765 return 0;
4766}
4767
4768static const struct hda_pcm_stream dyn_adc_pcm_analog_capture = {
4769 .substreams = 1,
4770 .channels_min = 2,
4771 .channels_max = 2,
4772 .nid = 0, /* fill later */
4773 .ops = {
4774 .prepare = dyn_adc_capture_pcm_prepare,
4775 .cleanup = dyn_adc_capture_pcm_cleanup
4776 },
4777};
4778
Takashi Iwaif873e532012-12-20 16:58:39 +01004779static void fill_pcm_stream_name(char *str, size_t len, const char *sfx,
4780 const char *chip_name)
4781{
4782 char *p;
4783
4784 if (*str)
4785 return;
4786 strlcpy(str, chip_name, len);
4787
4788 /* drop non-alnum chars after a space */
4789 for (p = strchr(str, ' '); p; p = strchr(p + 1, ' ')) {
4790 if (!isalnum(p[1])) {
4791 *p = 0;
4792 break;
4793 }
4794 }
4795 strlcat(str, sfx, len);
4796}
4797
Takashi Iwai352f7f92012-12-19 12:52:06 +01004798/* build PCM streams based on the parsed results */
4799int snd_hda_gen_build_pcms(struct hda_codec *codec)
4800{
4801 struct hda_gen_spec *spec = codec->spec;
4802 struct hda_pcm *info = spec->pcm_rec;
4803 const struct hda_pcm_stream *p;
4804 bool have_multi_adcs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004805
4806 codec->num_pcms = 1;
4807 codec->pcm_info = info;
4808
Takashi Iwai352f7f92012-12-19 12:52:06 +01004809 if (spec->no_analog)
4810 goto skip_analog;
4811
Takashi Iwaif873e532012-12-20 16:58:39 +01004812 fill_pcm_stream_name(spec->stream_name_analog,
4813 sizeof(spec->stream_name_analog),
4814 " Analog", codec->chip_name);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004815 info->name = spec->stream_name_analog;
4816
4817 if (spec->multiout.num_dacs > 0) {
4818 p = spec->stream_analog_playback;
4819 if (!p)
4820 p = &pcm_analog_playback;
4821 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
4822 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dac_nids[0];
4823 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
4824 spec->multiout.max_channels;
4825 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT &&
4826 spec->autocfg.line_outs == 2)
4827 info->stream[SNDRV_PCM_STREAM_PLAYBACK].chmap =
4828 snd_pcm_2_1_chmaps;
4829 }
4830 if (spec->num_adc_nids) {
4831 p = spec->stream_analog_capture;
4832 if (!p) {
4833 if (spec->dyn_adc_switch)
4834 p = &dyn_adc_pcm_analog_capture;
4835 else
4836 p = &pcm_analog_capture;
4837 }
4838 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
4839 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adc_nids[0];
4840 }
4841
Takashi Iwai352f7f92012-12-19 12:52:06 +01004842 skip_analog:
4843 /* SPDIF for stream index #1 */
4844 if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
Takashi Iwaif873e532012-12-20 16:58:39 +01004845 fill_pcm_stream_name(spec->stream_name_digital,
4846 sizeof(spec->stream_name_digital),
4847 " Digital", codec->chip_name);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004848 codec->num_pcms = 2;
4849 codec->slave_dig_outs = spec->multiout.slave_dig_outs;
4850 info = spec->pcm_rec + 1;
4851 info->name = spec->stream_name_digital;
4852 if (spec->dig_out_type)
4853 info->pcm_type = spec->dig_out_type;
4854 else
4855 info->pcm_type = HDA_PCM_TYPE_SPDIF;
4856 if (spec->multiout.dig_out_nid) {
4857 p = spec->stream_digital_playback;
4858 if (!p)
4859 p = &pcm_digital_playback;
4860 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
4861 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dig_out_nid;
4862 }
4863 if (spec->dig_in_nid) {
4864 p = spec->stream_digital_capture;
4865 if (!p)
4866 p = &pcm_digital_capture;
4867 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
4868 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in_nid;
4869 }
4870 }
4871
4872 if (spec->no_analog)
4873 return 0;
4874
4875 /* If the use of more than one ADC is requested for the current
4876 * model, configure a second analog capture-only PCM.
4877 */
4878 have_multi_adcs = (spec->num_adc_nids > 1) &&
4879 !spec->dyn_adc_switch && !spec->auto_mic;
4880 /* Additional Analaog capture for index #2 */
4881 if (spec->alt_dac_nid || have_multi_adcs) {
Takashi Iwaia6071482013-01-21 16:50:09 +01004882 fill_pcm_stream_name(spec->stream_name_alt_analog,
4883 sizeof(spec->stream_name_alt_analog),
4884 " Alt Analog", codec->chip_name);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004885 codec->num_pcms = 3;
4886 info = spec->pcm_rec + 2;
Takashi Iwaia6071482013-01-21 16:50:09 +01004887 info->name = spec->stream_name_alt_analog;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004888 if (spec->alt_dac_nid) {
4889 p = spec->stream_analog_alt_playback;
4890 if (!p)
4891 p = &pcm_analog_alt_playback;
4892 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
4893 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
4894 spec->alt_dac_nid;
4895 } else {
4896 info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
4897 pcm_null_stream;
4898 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = 0;
4899 }
4900 if (have_multi_adcs) {
4901 p = spec->stream_analog_alt_capture;
4902 if (!p)
4903 p = &pcm_analog_alt_capture;
4904 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
4905 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
4906 spec->adc_nids[1];
4907 info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams =
4908 spec->num_adc_nids - 1;
4909 } else {
4910 info->stream[SNDRV_PCM_STREAM_CAPTURE] =
4911 pcm_null_stream;
4912 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = 0;
4913 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004914 }
4915
4916 return 0;
4917}
Takashi Iwai352f7f92012-12-19 12:52:06 +01004918EXPORT_SYMBOL_HDA(snd_hda_gen_build_pcms);
4919
4920
4921/*
4922 * Standard auto-parser initializations
4923 */
4924
Takashi Iwaid4156932013-01-07 10:08:02 +01004925/* configure the given path as a proper output */
Takashi Iwai2c12c302013-01-10 09:33:29 +01004926static void set_output_and_unmute(struct hda_codec *codec, int path_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004927{
4928 struct nid_path *path;
Takashi Iwaid4156932013-01-07 10:08:02 +01004929 hda_nid_t pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004930
Takashi Iwai196c17662013-01-04 15:01:40 +01004931 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwaid4156932013-01-07 10:08:02 +01004932 if (!path || !path->depth)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004933 return;
Takashi Iwaid4156932013-01-07 10:08:02 +01004934 pin = path->path[path->depth - 1];
Takashi Iwai2c12c302013-01-10 09:33:29 +01004935 restore_pin_ctl(codec, pin);
Takashi Iwaie1284af2013-01-03 16:33:02 +01004936 snd_hda_activate_path(codec, path, path->active, true);
4937 set_pin_eapd(codec, pin, path->active);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004938}
4939
4940/* initialize primary output paths */
4941static void init_multi_out(struct hda_codec *codec)
4942{
4943 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004944 int i;
4945
Takashi Iwaid4156932013-01-07 10:08:02 +01004946 for (i = 0; i < spec->autocfg.line_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01004947 set_output_and_unmute(codec, spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004948}
4949
Takashi Iwaidb23fd12012-12-20 15:27:24 +01004950
Takashi Iwai2c12c302013-01-10 09:33:29 +01004951static void __init_extra_out(struct hda_codec *codec, int num_outs, int *paths)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004952{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004953 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004954
Takashi Iwaid4156932013-01-07 10:08:02 +01004955 for (i = 0; i < num_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01004956 set_output_and_unmute(codec, paths[i]);
Takashi Iwaidb23fd12012-12-20 15:27:24 +01004957}
4958
4959/* initialize hp and speaker paths */
4960static void init_extra_out(struct hda_codec *codec)
4961{
4962 struct hda_gen_spec *spec = codec->spec;
4963
4964 if (spec->autocfg.line_out_type != AUTO_PIN_HP_OUT)
Takashi Iwai2c12c302013-01-10 09:33:29 +01004965 __init_extra_out(codec, spec->autocfg.hp_outs, spec->hp_paths);
Takashi Iwaidb23fd12012-12-20 15:27:24 +01004966 if (spec->autocfg.line_out_type != AUTO_PIN_SPEAKER_OUT)
4967 __init_extra_out(codec, spec->autocfg.speaker_outs,
Takashi Iwai2c12c302013-01-10 09:33:29 +01004968 spec->speaker_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004969}
4970
4971/* initialize multi-io paths */
4972static void init_multi_io(struct hda_codec *codec)
4973{
4974 struct hda_gen_spec *spec = codec->spec;
4975 int i;
4976
4977 for (i = 0; i < spec->multi_ios; i++) {
4978 hda_nid_t pin = spec->multi_io[i].pin;
4979 struct nid_path *path;
Takashi Iwai196c17662013-01-04 15:01:40 +01004980 path = get_multiio_path(codec, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004981 if (!path)
4982 continue;
4983 if (!spec->multi_io[i].ctl_in)
4984 spec->multi_io[i].ctl_in =
Takashi Iwai2c12c302013-01-10 09:33:29 +01004985 snd_hda_codec_get_pin_target(codec, pin);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004986 snd_hda_activate_path(codec, path, path->active, true);
4987 }
4988}
4989
Takashi Iwai352f7f92012-12-19 12:52:06 +01004990/* set up input pins and loopback paths */
4991static void init_analog_input(struct hda_codec *codec)
4992{
4993 struct hda_gen_spec *spec = codec->spec;
4994 struct auto_pin_cfg *cfg = &spec->autocfg;
4995 int i;
4996
4997 for (i = 0; i < cfg->num_inputs; i++) {
4998 hda_nid_t nid = cfg->inputs[i].pin;
4999 if (is_input_pin(codec, nid))
Takashi Iwai2c12c302013-01-10 09:33:29 +01005000 restore_pin_ctl(codec, nid);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005001
5002 /* init loopback inputs */
5003 if (spec->mixer_nid) {
Takashi Iwai3e367f12013-01-23 17:07:23 +01005004 resume_path_from_idx(codec, spec->loopback_paths[i]);
5005 resume_path_from_idx(codec, spec->loopback_merge_path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005006 }
5007 }
5008}
5009
5010/* initialize ADC paths */
5011static void init_input_src(struct hda_codec *codec)
5012{
5013 struct hda_gen_spec *spec = codec->spec;
5014 struct hda_input_mux *imux = &spec->input_mux;
5015 struct nid_path *path;
5016 int i, c, nums;
5017
5018 if (spec->dyn_adc_switch)
5019 nums = 1;
5020 else
5021 nums = spec->num_adc_nids;
5022
5023 for (c = 0; c < nums; c++) {
5024 for (i = 0; i < imux->num_items; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01005025 path = get_input_path(codec, c, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005026 if (path) {
5027 bool active = path->active;
5028 if (i == spec->cur_mux[c])
5029 active = true;
5030 snd_hda_activate_path(codec, path, active, false);
5031 }
5032 }
Takashi Iwai967303d2013-02-19 17:12:42 +01005033 if (spec->hp_mic)
5034 update_hp_mic(codec, c, true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005035 }
5036
Takashi Iwai352f7f92012-12-19 12:52:06 +01005037 if (spec->cap_sync_hook)
Takashi Iwaia90229e2013-01-18 14:10:00 +01005038 spec->cap_sync_hook(codec, NULL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005039}
5040
5041/* set right pin controls for digital I/O */
5042static void init_digital(struct hda_codec *codec)
5043{
5044 struct hda_gen_spec *spec = codec->spec;
5045 int i;
5046 hda_nid_t pin;
5047
Takashi Iwaid4156932013-01-07 10:08:02 +01005048 for (i = 0; i < spec->autocfg.dig_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01005049 set_output_and_unmute(codec, spec->digout_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005050 pin = spec->autocfg.dig_in_pin;
Takashi Iwai2430d7b2013-01-04 15:09:42 +01005051 if (pin) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01005052 restore_pin_ctl(codec, pin);
Takashi Iwai3e367f12013-01-23 17:07:23 +01005053 resume_path_from_idx(codec, spec->digin_path);
Takashi Iwai2430d7b2013-01-04 15:09:42 +01005054 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01005055}
5056
Takashi Iwai973e4972012-12-20 15:16:09 +01005057/* clear unsol-event tags on unused pins; Conexant codecs seem to leave
5058 * invalid unsol tags by some reason
5059 */
5060static void clear_unsol_on_unused_pins(struct hda_codec *codec)
5061{
5062 int i;
5063
5064 for (i = 0; i < codec->init_pins.used; i++) {
5065 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
5066 hda_nid_t nid = pin->nid;
5067 if (is_jack_detectable(codec, nid) &&
5068 !snd_hda_jack_tbl_get(codec, nid))
5069 snd_hda_codec_update_cache(codec, nid, 0,
5070 AC_VERB_SET_UNSOLICITED_ENABLE, 0);
5071 }
5072}
5073
Takashi Iwai5187ac12013-01-07 12:52:16 +01005074/*
5075 * initialize the generic spec;
5076 * this can be put as patch_ops.init function
5077 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01005078int snd_hda_gen_init(struct hda_codec *codec)
5079{
5080 struct hda_gen_spec *spec = codec->spec;
5081
5082 if (spec->init_hook)
5083 spec->init_hook(codec);
5084
5085 snd_hda_apply_verbs(codec);
5086
Takashi Iwai3bbcd272012-12-20 11:50:58 +01005087 codec->cached_write = 1;
5088
Takashi Iwai352f7f92012-12-19 12:52:06 +01005089 init_multi_out(codec);
5090 init_extra_out(codec);
5091 init_multi_io(codec);
5092 init_analog_input(codec);
5093 init_input_src(codec);
5094 init_digital(codec);
5095
Takashi Iwai973e4972012-12-20 15:16:09 +01005096 clear_unsol_on_unused_pins(codec);
5097
Takashi Iwai352f7f92012-12-19 12:52:06 +01005098 /* call init functions of standard auto-mute helpers */
Takashi Iwaia5cc2502013-01-16 18:08:55 +01005099 update_automute_all(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005100
Takashi Iwaidc870f32013-01-22 15:24:30 +01005101 snd_hda_codec_flush_cache(codec);
Takashi Iwai3bbcd272012-12-20 11:50:58 +01005102
Takashi Iwai352f7f92012-12-19 12:52:06 +01005103 if (spec->vmaster_mute.sw_kctl && spec->vmaster_mute.hook)
5104 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
5105
5106 hda_call_check_power_status(codec, 0x01);
5107 return 0;
5108}
Takashi Iwaifce52a32013-01-07 12:42:48 +01005109EXPORT_SYMBOL_HDA(snd_hda_gen_init);
5110
Takashi Iwai5187ac12013-01-07 12:52:16 +01005111/*
5112 * free the generic spec;
5113 * this can be put as patch_ops.free function
5114 */
Takashi Iwaifce52a32013-01-07 12:42:48 +01005115void snd_hda_gen_free(struct hda_codec *codec)
5116{
Takashi Iwai7504b6c2013-03-18 11:25:51 +01005117 snd_hda_detach_beep_device(codec);
Takashi Iwaifce52a32013-01-07 12:42:48 +01005118 snd_hda_gen_spec_free(codec->spec);
5119 kfree(codec->spec);
5120 codec->spec = NULL;
5121}
5122EXPORT_SYMBOL_HDA(snd_hda_gen_free);
5123
5124#ifdef CONFIG_PM
Takashi Iwai5187ac12013-01-07 12:52:16 +01005125/*
5126 * check the loopback power save state;
5127 * this can be put as patch_ops.check_power_status function
5128 */
Takashi Iwaifce52a32013-01-07 12:42:48 +01005129int snd_hda_gen_check_power_status(struct hda_codec *codec, hda_nid_t nid)
5130{
5131 struct hda_gen_spec *spec = codec->spec;
5132 return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
5133}
5134EXPORT_SYMBOL_HDA(snd_hda_gen_check_power_status);
5135#endif
Takashi Iwai352f7f92012-12-19 12:52:06 +01005136
5137
5138/*
5139 * the generic codec support
5140 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005141
Takashi Iwai352f7f92012-12-19 12:52:06 +01005142static const struct hda_codec_ops generic_patch_ops = {
5143 .build_controls = snd_hda_gen_build_controls,
5144 .build_pcms = snd_hda_gen_build_pcms,
5145 .init = snd_hda_gen_init,
Takashi Iwaifce52a32013-01-07 12:42:48 +01005146 .free = snd_hda_gen_free,
Takashi Iwai352f7f92012-12-19 12:52:06 +01005147 .unsol_event = snd_hda_jack_unsol_event,
Takashi Iwai83012a72012-08-24 18:38:08 +02005148#ifdef CONFIG_PM
Takashi Iwaifce52a32013-01-07 12:42:48 +01005149 .check_power_status = snd_hda_gen_check_power_status,
Takashi Iwaicb53c622007-08-10 17:21:45 +02005150#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005151};
5152
Linus Torvalds1da177e2005-04-16 15:20:36 -07005153int snd_hda_parse_generic_codec(struct hda_codec *codec)
5154{
Takashi Iwai352f7f92012-12-19 12:52:06 +01005155 struct hda_gen_spec *spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005156 int err;
5157
Takashi Iwaie560d8d2005-09-09 14:21:46 +02005158 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005159 if (!spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005160 return -ENOMEM;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005161 snd_hda_gen_spec_init(spec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005162 codec->spec = spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005163
Takashi Iwai9eb413e2012-12-19 14:41:21 +01005164 err = snd_hda_parse_pin_defcfg(codec, &spec->autocfg, NULL, 0);
5165 if (err < 0)
5166 return err;
5167
5168 err = snd_hda_gen_parse_auto_config(codec, &spec->autocfg);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005169 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005170 goto error;
5171
5172 codec->patch_ops = generic_patch_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005173 return 0;
5174
Takashi Iwai352f7f92012-12-19 12:52:06 +01005175error:
Takashi Iwaifce52a32013-01-07 12:42:48 +01005176 snd_hda_gen_free(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005177 return err;
5178}
Takashi Iwaifce52a32013-01-07 12:42:48 +01005179EXPORT_SYMBOL_HDA(snd_hda_parse_generic_codec);