blob: 4b1524a861f38e2aee10d8bd39bf3352d82ed0b8 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Universal Interface for Intel High Definition Audio Codec
3 *
4 * Generic widget tree parser
5 *
6 * Copyright (c) 2004 Takashi Iwai <tiwai@suse.de>
7 *
8 * This driver is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This driver is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */
22
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/init.h>
24#include <linux/slab.h>
Paul Gortmakerd81a6d72011-09-22 09:34:58 -040025#include <linux/export.h>
Takashi Iwai352f7f92012-12-19 12:52:06 +010026#include <linux/sort.h>
Takashi Iwai55196ff2013-01-24 17:32:56 +010027#include <linux/delay.h>
Takashi Iwaif873e532012-12-20 16:58:39 +010028#include <linux/ctype.h>
29#include <linux/string.h>
Takashi Iwai294765582013-01-17 09:52:11 +010030#include <linux/bitops.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <sound/core.h>
Takashi Iwai352f7f92012-12-19 12:52:06 +010032#include <sound/jack.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include "hda_codec.h"
34#include "hda_local.h"
Takashi Iwai352f7f92012-12-19 12:52:06 +010035#include "hda_auto_parser.h"
36#include "hda_jack.h"
Takashi Iwai7504b6c2013-03-18 11:25:51 +010037#include "hda_beep.h"
Takashi Iwai352f7f92012-12-19 12:52:06 +010038#include "hda_generic.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Takashi Iwai352f7f92012-12-19 12:52:06 +010041/* initialize hda_gen_spec struct */
42int snd_hda_gen_spec_init(struct hda_gen_spec *spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -070043{
Takashi Iwai352f7f92012-12-19 12:52:06 +010044 snd_array_init(&spec->kctls, sizeof(struct snd_kcontrol_new), 32);
Takashi Iwai352f7f92012-12-19 12:52:06 +010045 snd_array_init(&spec->paths, sizeof(struct nid_path), 8);
Takashi Iwai0186f4f2013-02-07 10:45:11 +010046 snd_array_init(&spec->loopback_list, sizeof(struct hda_amp_list), 8);
Takashi Iwai38cf6f12012-12-21 14:09:42 +010047 mutex_init(&spec->pcm_mutex);
Takashi Iwai352f7f92012-12-19 12:52:06 +010048 return 0;
49}
50EXPORT_SYMBOL_HDA(snd_hda_gen_spec_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Takashi Iwai12c93df2012-12-19 14:38:33 +010052struct snd_kcontrol_new *
53snd_hda_gen_add_kctl(struct hda_gen_spec *spec, const char *name,
54 const struct snd_kcontrol_new *temp)
Takashi Iwai352f7f92012-12-19 12:52:06 +010055{
56 struct snd_kcontrol_new *knew = snd_array_new(&spec->kctls);
57 if (!knew)
58 return NULL;
59 *knew = *temp;
60 if (name)
61 knew->name = kstrdup(name, GFP_KERNEL);
62 else if (knew->name)
63 knew->name = kstrdup(knew->name, GFP_KERNEL);
64 if (!knew->name)
65 return NULL;
66 return knew;
67}
Takashi Iwai12c93df2012-12-19 14:38:33 +010068EXPORT_SYMBOL_HDA(snd_hda_gen_add_kctl);
Takashi Iwai352f7f92012-12-19 12:52:06 +010069
70static void free_kctls(struct hda_gen_spec *spec)
71{
72 if (spec->kctls.list) {
73 struct snd_kcontrol_new *kctl = spec->kctls.list;
74 int i;
75 for (i = 0; i < spec->kctls.used; i++)
76 kfree(kctl[i].name);
77 }
78 snd_array_free(&spec->kctls);
79}
80
Takashi Iwai352f7f92012-12-19 12:52:06 +010081void snd_hda_gen_spec_free(struct hda_gen_spec *spec)
82{
83 if (!spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 return;
Takashi Iwai352f7f92012-12-19 12:52:06 +010085 free_kctls(spec);
Takashi Iwai352f7f92012-12-19 12:52:06 +010086 snd_array_free(&spec->paths);
Takashi Iwai0186f4f2013-02-07 10:45:11 +010087 snd_array_free(&spec->loopback_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088}
Takashi Iwai352f7f92012-12-19 12:52:06 +010089EXPORT_SYMBOL_HDA(snd_hda_gen_spec_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
91/*
Takashi Iwai1c70a582013-01-11 17:48:22 +010092 * store user hints
93 */
94static void parse_user_hints(struct hda_codec *codec)
95{
96 struct hda_gen_spec *spec = codec->spec;
97 int val;
98
99 val = snd_hda_get_bool_hint(codec, "jack_detect");
100 if (val >= 0)
101 codec->no_jack_detect = !val;
102 val = snd_hda_get_bool_hint(codec, "inv_jack_detect");
103 if (val >= 0)
104 codec->inv_jack_detect = !!val;
105 val = snd_hda_get_bool_hint(codec, "trigger_sense");
106 if (val >= 0)
107 codec->no_trigger_sense = !val;
108 val = snd_hda_get_bool_hint(codec, "inv_eapd");
109 if (val >= 0)
110 codec->inv_eapd = !!val;
111 val = snd_hda_get_bool_hint(codec, "pcm_format_first");
112 if (val >= 0)
113 codec->pcm_format_first = !!val;
114 val = snd_hda_get_bool_hint(codec, "sticky_stream");
115 if (val >= 0)
116 codec->no_sticky_stream = !val;
117 val = snd_hda_get_bool_hint(codec, "spdif_status_reset");
118 if (val >= 0)
119 codec->spdif_status_reset = !!val;
120 val = snd_hda_get_bool_hint(codec, "pin_amp_workaround");
121 if (val >= 0)
122 codec->pin_amp_workaround = !!val;
123 val = snd_hda_get_bool_hint(codec, "single_adc_amp");
124 if (val >= 0)
125 codec->single_adc_amp = !!val;
126
Takashi Iwaif72706b2013-01-16 18:20:07 +0100127 val = snd_hda_get_bool_hint(codec, "auto_mute");
128 if (val >= 0)
129 spec->suppress_auto_mute = !val;
Takashi Iwai1c70a582013-01-11 17:48:22 +0100130 val = snd_hda_get_bool_hint(codec, "auto_mic");
131 if (val >= 0)
132 spec->suppress_auto_mic = !val;
133 val = snd_hda_get_bool_hint(codec, "line_in_auto_switch");
134 if (val >= 0)
135 spec->line_in_auto_switch = !!val;
136 val = snd_hda_get_bool_hint(codec, "need_dac_fix");
137 if (val >= 0)
138 spec->need_dac_fix = !!val;
139 val = snd_hda_get_bool_hint(codec, "primary_hp");
140 if (val >= 0)
141 spec->no_primary_hp = !val;
142 val = snd_hda_get_bool_hint(codec, "multi_cap_vol");
143 if (val >= 0)
144 spec->multi_cap_vol = !!val;
145 val = snd_hda_get_bool_hint(codec, "inv_dmic_split");
146 if (val >= 0)
147 spec->inv_dmic_split = !!val;
148 val = snd_hda_get_bool_hint(codec, "indep_hp");
149 if (val >= 0)
150 spec->indep_hp = !!val;
151 val = snd_hda_get_bool_hint(codec, "add_stereo_mix_input");
152 if (val >= 0)
153 spec->add_stereo_mix_input = !!val;
Takashi Iwaif811c3c2013-03-07 18:32:59 +0100154 /* the following two are just for compatibility */
Takashi Iwai1c70a582013-01-11 17:48:22 +0100155 val = snd_hda_get_bool_hint(codec, "add_out_jack_modes");
156 if (val >= 0)
Takashi Iwaif811c3c2013-03-07 18:32:59 +0100157 spec->add_jack_modes = !!val;
Takashi Iwai294765582013-01-17 09:52:11 +0100158 val = snd_hda_get_bool_hint(codec, "add_in_jack_modes");
159 if (val >= 0)
Takashi Iwaif811c3c2013-03-07 18:32:59 +0100160 spec->add_jack_modes = !!val;
161 val = snd_hda_get_bool_hint(codec, "add_jack_modes");
162 if (val >= 0)
163 spec->add_jack_modes = !!val;
Takashi Iwai55196ff2013-01-24 17:32:56 +0100164 val = snd_hda_get_bool_hint(codec, "power_down_unused");
165 if (val >= 0)
166 spec->power_down_unused = !!val;
Takashi Iwai967303d2013-02-19 17:12:42 +0100167 val = snd_hda_get_bool_hint(codec, "add_hp_mic");
168 if (val >= 0)
169 spec->hp_mic = !!val;
170 val = snd_hda_get_bool_hint(codec, "hp_mic_detect");
171 if (val >= 0)
172 spec->suppress_hp_mic_detect = !val;
Takashi Iwai1c70a582013-01-11 17:48:22 +0100173
174 if (!snd_hda_get_int_hint(codec, "mixer_nid", &val))
175 spec->mixer_nid = val;
176}
177
178/*
Takashi Iwai2c12c302013-01-10 09:33:29 +0100179 * pin control value accesses
180 */
181
182#define update_pin_ctl(codec, pin, val) \
183 snd_hda_codec_update_cache(codec, pin, 0, \
184 AC_VERB_SET_PIN_WIDGET_CONTROL, val)
185
186/* restore the pinctl based on the cached value */
187static inline void restore_pin_ctl(struct hda_codec *codec, hda_nid_t pin)
188{
189 update_pin_ctl(codec, pin, snd_hda_codec_get_pin_target(codec, pin));
190}
191
192/* set the pinctl target value and write it if requested */
193static void set_pin_target(struct hda_codec *codec, hda_nid_t pin,
194 unsigned int val, bool do_write)
195{
196 if (!pin)
197 return;
198 val = snd_hda_correct_pin_ctl(codec, pin, val);
199 snd_hda_codec_set_pin_target(codec, pin, val);
200 if (do_write)
201 update_pin_ctl(codec, pin, val);
202}
203
204/* set pinctl target values for all given pins */
205static void set_pin_targets(struct hda_codec *codec, int num_pins,
206 hda_nid_t *pins, unsigned int val)
207{
208 int i;
209 for (i = 0; i < num_pins; i++)
210 set_pin_target(codec, pins[i], val, false);
211}
212
213/*
Takashi Iwai352f7f92012-12-19 12:52:06 +0100214 * parsing paths
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100217/* return the position of NID in the list, or -1 if not found */
218static int find_idx_in_nid_list(hda_nid_t nid, const hda_nid_t *list, int nums)
219{
220 int i;
221 for (i = 0; i < nums; i++)
222 if (list[i] == nid)
223 return i;
224 return -1;
225}
226
227/* return true if the given NID is contained in the path */
228static bool is_nid_contained(struct nid_path *path, hda_nid_t nid)
229{
230 return find_idx_in_nid_list(nid, path->path, path->depth) >= 0;
231}
232
Takashi Iwaif5172a72013-01-04 13:19:55 +0100233static struct nid_path *get_nid_path(struct hda_codec *codec,
234 hda_nid_t from_nid, hda_nid_t to_nid,
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100235 int anchor_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100237 struct hda_gen_spec *spec = codec->spec;
238 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
Takashi Iwai352f7f92012-12-19 12:52:06 +0100240 for (i = 0; i < spec->paths.used; i++) {
241 struct nid_path *path = snd_array_elem(&spec->paths, i);
242 if (path->depth <= 0)
243 continue;
244 if ((!from_nid || path->path[0] == from_nid) &&
Takashi Iwaif5172a72013-01-04 13:19:55 +0100245 (!to_nid || path->path[path->depth - 1] == to_nid)) {
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100246 if (!anchor_nid ||
247 (anchor_nid > 0 && is_nid_contained(path, anchor_nid)) ||
248 (anchor_nid < 0 && !is_nid_contained(path, anchor_nid)))
Takashi Iwaif5172a72013-01-04 13:19:55 +0100249 return path;
250 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 }
252 return NULL;
253}
Takashi Iwaif5172a72013-01-04 13:19:55 +0100254
255/* get the path between the given NIDs;
256 * passing 0 to either @pin or @dac behaves as a wildcard
257 */
258struct nid_path *snd_hda_get_nid_path(struct hda_codec *codec,
259 hda_nid_t from_nid, hda_nid_t to_nid)
260{
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100261 return get_nid_path(codec, from_nid, to_nid, 0);
Takashi Iwaif5172a72013-01-04 13:19:55 +0100262}
Takashi Iwai352f7f92012-12-19 12:52:06 +0100263EXPORT_SYMBOL_HDA(snd_hda_get_nid_path);
264
Takashi Iwai196c17662013-01-04 15:01:40 +0100265/* get the index number corresponding to the path instance;
266 * the index starts from 1, for easier checking the invalid value
267 */
268int snd_hda_get_path_idx(struct hda_codec *codec, struct nid_path *path)
269{
270 struct hda_gen_spec *spec = codec->spec;
271 struct nid_path *array = spec->paths.list;
272 ssize_t idx;
273
274 if (!spec->paths.used)
275 return 0;
276 idx = path - array;
277 if (idx < 0 || idx >= spec->paths.used)
278 return 0;
279 return idx + 1;
280}
Takashi Iwai4bd01e92013-01-22 15:17:20 +0100281EXPORT_SYMBOL_HDA(snd_hda_get_path_idx);
Takashi Iwai196c17662013-01-04 15:01:40 +0100282
283/* get the path instance corresponding to the given index number */
284struct nid_path *snd_hda_get_path_from_idx(struct hda_codec *codec, int idx)
285{
286 struct hda_gen_spec *spec = codec->spec;
287
288 if (idx <= 0 || idx > spec->paths.used)
289 return NULL;
290 return snd_array_elem(&spec->paths, idx - 1);
291}
Takashi Iwai4bd01e92013-01-22 15:17:20 +0100292EXPORT_SYMBOL_HDA(snd_hda_get_path_from_idx);
Takashi Iwai196c17662013-01-04 15:01:40 +0100293
Takashi Iwai352f7f92012-12-19 12:52:06 +0100294/* check whether the given DAC is already found in any existing paths */
295static bool is_dac_already_used(struct hda_codec *codec, hda_nid_t nid)
296{
297 struct hda_gen_spec *spec = codec->spec;
298 int i;
299
300 for (i = 0; i < spec->paths.used; i++) {
301 struct nid_path *path = snd_array_elem(&spec->paths, i);
302 if (path->path[0] == nid)
303 return true;
304 }
305 return false;
306}
307
308/* check whether the given two widgets can be connected */
309static bool is_reachable_path(struct hda_codec *codec,
310 hda_nid_t from_nid, hda_nid_t to_nid)
311{
312 if (!from_nid || !to_nid)
313 return false;
314 return snd_hda_get_conn_index(codec, to_nid, from_nid, true) >= 0;
315}
316
317/* nid, dir and idx */
318#define AMP_VAL_COMPARE_MASK (0xffff | (1U << 18) | (0x0f << 19))
319
320/* check whether the given ctl is already assigned in any path elements */
321static bool is_ctl_used(struct hda_codec *codec, unsigned int val, int type)
322{
323 struct hda_gen_spec *spec = codec->spec;
324 int i;
325
326 val &= AMP_VAL_COMPARE_MASK;
327 for (i = 0; i < spec->paths.used; i++) {
328 struct nid_path *path = snd_array_elem(&spec->paths, i);
329 if ((path->ctls[type] & AMP_VAL_COMPARE_MASK) == val)
330 return true;
331 }
332 return false;
333}
334
335/* check whether a control with the given (nid, dir, idx) was assigned */
336static bool is_ctl_associated(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwai8999bf02013-01-18 11:01:33 +0100337 int dir, int idx, int type)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100338{
339 unsigned int val = HDA_COMPOSE_AMP_VAL(nid, 3, idx, dir);
Takashi Iwai8999bf02013-01-18 11:01:33 +0100340 return is_ctl_used(codec, val, type);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100341}
342
Takashi Iwai0c8c0f52012-12-20 17:54:22 +0100343static void print_nid_path(const char *pfx, struct nid_path *path)
344{
345 char buf[40];
346 int i;
347
348
349 buf[0] = 0;
350 for (i = 0; i < path->depth; i++) {
351 char tmp[4];
352 sprintf(tmp, ":%02x", path->path[i]);
353 strlcat(buf, tmp, sizeof(buf));
354 }
355 snd_printdd("%s path: depth=%d %s\n", pfx, path->depth, buf);
356}
357
Takashi Iwai352f7f92012-12-19 12:52:06 +0100358/* called recursively */
359static bool __parse_nid_path(struct hda_codec *codec,
360 hda_nid_t from_nid, hda_nid_t to_nid,
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100361 int anchor_nid, struct nid_path *path,
362 int depth)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100363{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100364 const hda_nid_t *conn;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100365 int i, nums;
366
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100367 if (to_nid == anchor_nid)
368 anchor_nid = 0; /* anchor passed */
369 else if (to_nid == (hda_nid_t)(-anchor_nid))
370 return false; /* hit the exclusive nid */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100371
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100372 nums = snd_hda_get_conn_list(codec, to_nid, &conn);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100373 for (i = 0; i < nums; i++) {
374 if (conn[i] != from_nid) {
375 /* special case: when from_nid is 0,
376 * try to find an empty DAC
377 */
378 if (from_nid ||
379 get_wcaps_type(get_wcaps(codec, conn[i])) != AC_WID_AUD_OUT ||
380 is_dac_already_used(codec, conn[i]))
381 continue;
382 }
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100383 /* anchor is not requested or already passed? */
384 if (anchor_nid <= 0)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100385 goto found;
386 }
387 if (depth >= MAX_NID_PATH_DEPTH)
388 return false;
389 for (i = 0; i < nums; i++) {
390 unsigned int type;
391 type = get_wcaps_type(get_wcaps(codec, conn[i]));
392 if (type == AC_WID_AUD_OUT || type == AC_WID_AUD_IN ||
393 type == AC_WID_PIN)
394 continue;
395 if (__parse_nid_path(codec, from_nid, conn[i],
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100396 anchor_nid, path, depth + 1))
Takashi Iwai352f7f92012-12-19 12:52:06 +0100397 goto found;
398 }
399 return false;
400
401 found:
402 path->path[path->depth] = conn[i];
403 path->idx[path->depth + 1] = i;
404 if (nums > 1 && get_wcaps_type(get_wcaps(codec, to_nid)) != AC_WID_AUD_MIX)
405 path->multi[path->depth + 1] = 1;
406 path->depth++;
407 return true;
408}
409
410/* parse the widget path from the given nid to the target nid;
411 * when @from_nid is 0, try to find an empty DAC;
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100412 * when @anchor_nid is set to a positive value, only paths through the widget
413 * with the given value are evaluated.
414 * when @anchor_nid is set to a negative value, paths through the widget
415 * with the negative of given value are excluded, only other paths are chosen.
416 * when @anchor_nid is zero, no special handling about path selection.
Takashi Iwai352f7f92012-12-19 12:52:06 +0100417 */
418bool snd_hda_parse_nid_path(struct hda_codec *codec, hda_nid_t from_nid,
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100419 hda_nid_t to_nid, int anchor_nid,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100420 struct nid_path *path)
421{
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100422 if (__parse_nid_path(codec, from_nid, to_nid, anchor_nid, path, 1)) {
Takashi Iwai352f7f92012-12-19 12:52:06 +0100423 path->path[path->depth] = to_nid;
424 path->depth++;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100425 return true;
426 }
427 return false;
428}
429EXPORT_SYMBOL_HDA(snd_hda_parse_nid_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
431/*
Takashi Iwai352f7f92012-12-19 12:52:06 +0100432 * parse the path between the given NIDs and add to the path list.
433 * if no valid path is found, return NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100435struct nid_path *
436snd_hda_add_new_path(struct hda_codec *codec, hda_nid_t from_nid,
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100437 hda_nid_t to_nid, int anchor_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100439 struct hda_gen_spec *spec = codec->spec;
440 struct nid_path *path;
441
442 if (from_nid && to_nid && !is_reachable_path(codec, from_nid, to_nid))
443 return NULL;
444
Takashi Iwaif5172a72013-01-04 13:19:55 +0100445 /* check whether the path has been already added */
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100446 path = get_nid_path(codec, from_nid, to_nid, anchor_nid);
Takashi Iwaif5172a72013-01-04 13:19:55 +0100447 if (path)
448 return path;
449
Takashi Iwai352f7f92012-12-19 12:52:06 +0100450 path = snd_array_new(&spec->paths);
451 if (!path)
452 return NULL;
453 memset(path, 0, sizeof(*path));
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100454 if (snd_hda_parse_nid_path(codec, from_nid, to_nid, anchor_nid, path))
Takashi Iwai352f7f92012-12-19 12:52:06 +0100455 return path;
456 /* push back */
457 spec->paths.used--;
458 return NULL;
459}
460EXPORT_SYMBOL_HDA(snd_hda_add_new_path);
461
Takashi Iwai980428c2013-01-09 09:28:20 +0100462/* clear the given path as invalid so that it won't be picked up later */
463static void invalidate_nid_path(struct hda_codec *codec, int idx)
464{
465 struct nid_path *path = snd_hda_get_path_from_idx(codec, idx);
466 if (!path)
467 return;
468 memset(path, 0, sizeof(*path));
469}
470
Takashi Iwai352f7f92012-12-19 12:52:06 +0100471/* look for an empty DAC slot */
472static hda_nid_t look_for_dac(struct hda_codec *codec, hda_nid_t pin,
473 bool is_digital)
474{
475 struct hda_gen_spec *spec = codec->spec;
476 bool cap_digital;
477 int i;
478
479 for (i = 0; i < spec->num_all_dacs; i++) {
480 hda_nid_t nid = spec->all_dacs[i];
481 if (!nid || is_dac_already_used(codec, nid))
482 continue;
483 cap_digital = !!(get_wcaps(codec, nid) & AC_WCAP_DIGITAL);
484 if (is_digital != cap_digital)
485 continue;
486 if (is_reachable_path(codec, nid, pin))
487 return nid;
488 }
489 return 0;
490}
491
492/* replace the channels in the composed amp value with the given number */
493static unsigned int amp_val_replace_channels(unsigned int val, unsigned int chs)
494{
495 val &= ~(0x3U << 16);
496 val |= chs << 16;
497 return val;
498}
499
500/* check whether the widget has the given amp capability for the direction */
501static bool check_amp_caps(struct hda_codec *codec, hda_nid_t nid,
502 int dir, unsigned int bits)
503{
504 if (!nid)
505 return false;
506 if (get_wcaps(codec, nid) & (1 << (dir + 1)))
507 if (query_amp_caps(codec, nid, dir) & bits)
508 return true;
509 return false;
510}
511
David Henningsson99a55922013-01-16 15:58:44 +0100512static bool same_amp_caps(struct hda_codec *codec, hda_nid_t nid1,
513 hda_nid_t nid2, int dir)
514{
515 if (!(get_wcaps(codec, nid1) & (1 << (dir + 1))))
516 return !(get_wcaps(codec, nid2) & (1 << (dir + 1)));
517 return (query_amp_caps(codec, nid1, dir) ==
518 query_amp_caps(codec, nid2, dir));
519}
520
Takashi Iwai352f7f92012-12-19 12:52:06 +0100521#define nid_has_mute(codec, nid, dir) \
522 check_amp_caps(codec, nid, dir, AC_AMPCAP_MUTE)
523#define nid_has_volume(codec, nid, dir) \
524 check_amp_caps(codec, nid, dir, AC_AMPCAP_NUM_STEPS)
525
526/* look for a widget suitable for assigning a mute switch in the path */
527static hda_nid_t look_for_out_mute_nid(struct hda_codec *codec,
528 struct nid_path *path)
529{
530 int i;
531
532 for (i = path->depth - 1; i >= 0; i--) {
533 if (nid_has_mute(codec, path->path[i], HDA_OUTPUT))
534 return path->path[i];
535 if (i != path->depth - 1 && i != 0 &&
536 nid_has_mute(codec, path->path[i], HDA_INPUT))
537 return path->path[i];
538 }
539 return 0;
540}
541
542/* look for a widget suitable for assigning a volume ctl in the path */
543static hda_nid_t look_for_out_vol_nid(struct hda_codec *codec,
544 struct nid_path *path)
545{
546 int i;
547
548 for (i = path->depth - 1; i >= 0; i--) {
549 if (nid_has_volume(codec, path->path[i], HDA_OUTPUT))
550 return path->path[i];
551 }
Takashi Iwai82beb8f2007-08-10 17:09:26 +0200552 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553}
554
555/*
Takashi Iwai352f7f92012-12-19 12:52:06 +0100556 * path activation / deactivation
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100558
559/* can have the amp-in capability? */
560static bool has_amp_in(struct hda_codec *codec, struct nid_path *path, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100562 hda_nid_t nid = path->path[idx];
563 unsigned int caps = get_wcaps(codec, nid);
564 unsigned int type = get_wcaps_type(caps);
565
566 if (!(caps & AC_WCAP_IN_AMP))
567 return false;
568 if (type == AC_WID_PIN && idx > 0) /* only for input pins */
569 return false;
570 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571}
572
Takashi Iwai352f7f92012-12-19 12:52:06 +0100573/* can have the amp-out capability? */
574static bool has_amp_out(struct hda_codec *codec, struct nid_path *path, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100576 hda_nid_t nid = path->path[idx];
577 unsigned int caps = get_wcaps(codec, nid);
578 unsigned int type = get_wcaps_type(caps);
579
580 if (!(caps & AC_WCAP_OUT_AMP))
581 return false;
582 if (type == AC_WID_PIN && !idx) /* only for output pins */
583 return false;
584 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585}
586
Takashi Iwai352f7f92012-12-19 12:52:06 +0100587/* check whether the given (nid,dir,idx) is active */
588static bool is_active_nid(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwai7dddf2ae2013-01-24 16:31:35 +0100589 unsigned int dir, unsigned int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100591 struct hda_gen_spec *spec = codec->spec;
592 int i, n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
Takashi Iwai352f7f92012-12-19 12:52:06 +0100594 for (n = 0; n < spec->paths.used; n++) {
595 struct nid_path *path = snd_array_elem(&spec->paths, n);
596 if (!path->active)
597 continue;
598 for (i = 0; i < path->depth; i++) {
599 if (path->path[i] == nid) {
600 if (dir == HDA_OUTPUT || path->idx[i] == idx)
601 return true;
602 break;
603 }
604 }
605 }
606 return false;
607}
608
Takashi Iwaib1b9fbd2013-05-14 12:58:47 +0200609/* check whether the NID is referred by any active paths */
610#define is_active_nid_for_any(codec, nid) \
611 is_active_nid(codec, nid, HDA_OUTPUT, 0)
612
Takashi Iwai352f7f92012-12-19 12:52:06 +0100613/* get the default amp value for the target state */
614static int get_amp_val_to_activate(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwai8999bf02013-01-18 11:01:33 +0100615 int dir, unsigned int caps, bool enable)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100616{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100617 unsigned int val = 0;
618
Takashi Iwai352f7f92012-12-19 12:52:06 +0100619 if (caps & AC_AMPCAP_NUM_STEPS) {
620 /* set to 0dB */
621 if (enable)
622 val = (caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT;
623 }
624 if (caps & AC_AMPCAP_MUTE) {
625 if (!enable)
626 val |= HDA_AMP_MUTE;
627 }
628 return val;
629}
630
631/* initialize the amp value (only at the first time) */
632static void init_amp(struct hda_codec *codec, hda_nid_t nid, int dir, int idx)
633{
Takashi Iwai8999bf02013-01-18 11:01:33 +0100634 unsigned int caps = query_amp_caps(codec, nid, dir);
635 int val = get_amp_val_to_activate(codec, nid, dir, caps, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100636 snd_hda_codec_amp_init_stereo(codec, nid, dir, idx, 0xff, val);
637}
638
Takashi Iwai8999bf02013-01-18 11:01:33 +0100639/* calculate amp value mask we can modify;
640 * if the given amp is controlled by mixers, don't touch it
641 */
642static unsigned int get_amp_mask_to_modify(struct hda_codec *codec,
643 hda_nid_t nid, int dir, int idx,
644 unsigned int caps)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100645{
Takashi Iwai8999bf02013-01-18 11:01:33 +0100646 unsigned int mask = 0xff;
647
648 if (caps & AC_AMPCAP_MUTE) {
649 if (is_ctl_associated(codec, nid, dir, idx, NID_PATH_MUTE_CTL))
650 mask &= ~0x80;
651 }
652 if (caps & AC_AMPCAP_NUM_STEPS) {
653 if (is_ctl_associated(codec, nid, dir, idx, NID_PATH_VOL_CTL) ||
654 is_ctl_associated(codec, nid, dir, idx, NID_PATH_BOOST_CTL))
655 mask &= ~0x7f;
656 }
657 return mask;
658}
659
660static void activate_amp(struct hda_codec *codec, hda_nid_t nid, int dir,
661 int idx, int idx_to_check, bool enable)
662{
663 unsigned int caps;
664 unsigned int mask, val;
665
Takashi Iwai7dddf2ae2013-01-24 16:31:35 +0100666 if (!enable && is_active_nid(codec, nid, dir, idx_to_check))
Takashi Iwai352f7f92012-12-19 12:52:06 +0100667 return;
Takashi Iwai8999bf02013-01-18 11:01:33 +0100668
669 caps = query_amp_caps(codec, nid, dir);
670 val = get_amp_val_to_activate(codec, nid, dir, caps, enable);
671 mask = get_amp_mask_to_modify(codec, nid, dir, idx_to_check, caps);
672 if (!mask)
673 return;
674
675 val &= mask;
676 snd_hda_codec_amp_stereo(codec, nid, dir, idx, mask, val);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100677}
678
679static void activate_amp_out(struct hda_codec *codec, struct nid_path *path,
680 int i, bool enable)
681{
682 hda_nid_t nid = path->path[i];
683 init_amp(codec, nid, HDA_OUTPUT, 0);
Takashi Iwai8999bf02013-01-18 11:01:33 +0100684 activate_amp(codec, nid, HDA_OUTPUT, 0, 0, enable);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100685}
686
687static void activate_amp_in(struct hda_codec *codec, struct nid_path *path,
688 int i, bool enable, bool add_aamix)
689{
690 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100691 const hda_nid_t *conn;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100692 int n, nums, idx;
693 int type;
694 hda_nid_t nid = path->path[i];
695
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100696 nums = snd_hda_get_conn_list(codec, nid, &conn);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100697 type = get_wcaps_type(get_wcaps(codec, nid));
698 if (type == AC_WID_PIN ||
699 (type == AC_WID_AUD_IN && codec->single_adc_amp)) {
700 nums = 1;
701 idx = 0;
702 } else
703 idx = path->idx[i];
704
705 for (n = 0; n < nums; n++)
706 init_amp(codec, nid, HDA_INPUT, n);
707
Takashi Iwai352f7f92012-12-19 12:52:06 +0100708 /* here is a little bit tricky in comparison with activate_amp_out();
709 * when aa-mixer is available, we need to enable the path as well
710 */
711 for (n = 0; n < nums; n++) {
Takashi Iwaie4a395e2013-01-23 17:00:31 +0100712 if (n != idx && (!add_aamix || conn[n] != spec->mixer_merge_nid))
Takashi Iwai352f7f92012-12-19 12:52:06 +0100713 continue;
Takashi Iwai8999bf02013-01-18 11:01:33 +0100714 activate_amp(codec, nid, HDA_INPUT, n, idx, enable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 }
716}
717
Takashi Iwai352f7f92012-12-19 12:52:06 +0100718/* activate or deactivate the given path
719 * if @add_aamix is set, enable the input from aa-mix NID as well (if any)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100721void snd_hda_activate_path(struct hda_codec *codec, struct nid_path *path,
722 bool enable, bool add_aamix)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723{
Takashi Iwai55196ff2013-01-24 17:32:56 +0100724 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100725 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726
Takashi Iwai352f7f92012-12-19 12:52:06 +0100727 if (!enable)
728 path->active = false;
729
730 for (i = path->depth - 1; i >= 0; i--) {
Takashi Iwai55196ff2013-01-24 17:32:56 +0100731 hda_nid_t nid = path->path[i];
732 if (enable && spec->power_down_unused) {
733 /* make sure the widget is powered up */
734 if (!snd_hda_check_power_state(codec, nid, AC_PWRST_D0))
735 snd_hda_codec_write(codec, nid, 0,
736 AC_VERB_SET_POWER_STATE,
737 AC_PWRST_D0);
738 }
Takashi Iwai352f7f92012-12-19 12:52:06 +0100739 if (enable && path->multi[i])
Takashi Iwai55196ff2013-01-24 17:32:56 +0100740 snd_hda_codec_write_cache(codec, nid, 0,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100741 AC_VERB_SET_CONNECT_SEL,
742 path->idx[i]);
743 if (has_amp_in(codec, path, i))
744 activate_amp_in(codec, path, i, enable, add_aamix);
745 if (has_amp_out(codec, path, i))
746 activate_amp_out(codec, path, i, enable);
747 }
748
749 if (enable)
750 path->active = true;
751}
752EXPORT_SYMBOL_HDA(snd_hda_activate_path);
753
Takashi Iwai55196ff2013-01-24 17:32:56 +0100754/* if the given path is inactive, put widgets into D3 (only if suitable) */
755static void path_power_down_sync(struct hda_codec *codec, struct nid_path *path)
756{
757 struct hda_gen_spec *spec = codec->spec;
Jiri Slaby868211d2013-04-04 22:32:10 +0200758 bool changed = false;
Takashi Iwai55196ff2013-01-24 17:32:56 +0100759 int i;
760
761 if (!spec->power_down_unused || path->active)
762 return;
763
764 for (i = 0; i < path->depth; i++) {
765 hda_nid_t nid = path->path[i];
Takashi Iwaib1b9fbd2013-05-14 12:58:47 +0200766 if (!snd_hda_check_power_state(codec, nid, AC_PWRST_D3) &&
767 !is_active_nid_for_any(codec, nid)) {
Takashi Iwai55196ff2013-01-24 17:32:56 +0100768 snd_hda_codec_write(codec, nid, 0,
769 AC_VERB_SET_POWER_STATE,
770 AC_PWRST_D3);
771 changed = true;
772 }
773 }
774
775 if (changed) {
776 msleep(10);
777 snd_hda_codec_read(codec, path->path[0], 0,
778 AC_VERB_GET_POWER_STATE, 0);
779 }
780}
781
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +0100782/* turn on/off EAPD on the given pin */
783static void set_pin_eapd(struct hda_codec *codec, hda_nid_t pin, bool enable)
784{
785 struct hda_gen_spec *spec = codec->spec;
786 if (spec->own_eapd_ctl ||
787 !(snd_hda_query_pin_caps(codec, pin) & AC_PINCAP_EAPD))
788 return;
Takashi Iwaiecac3ed2012-12-21 15:23:01 +0100789 if (codec->inv_eapd)
790 enable = !enable;
Takashi Iwai05909d5c2013-05-31 19:55:54 +0200791 if (spec->keep_eapd_on && !enable)
792 return;
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +0100793 snd_hda_codec_update_cache(codec, pin, 0,
794 AC_VERB_SET_EAPD_BTLENABLE,
795 enable ? 0x02 : 0x00);
796}
797
Takashi Iwai3e367f12013-01-23 17:07:23 +0100798/* re-initialize the path specified by the given path index */
799static void resume_path_from_idx(struct hda_codec *codec, int path_idx)
800{
801 struct nid_path *path = snd_hda_get_path_from_idx(codec, path_idx);
802 if (path)
803 snd_hda_activate_path(codec, path, path->active, false);
804}
805
Takashi Iwai352f7f92012-12-19 12:52:06 +0100806
807/*
808 * Helper functions for creating mixer ctl elements
809 */
810
811enum {
812 HDA_CTL_WIDGET_VOL,
813 HDA_CTL_WIDGET_MUTE,
814 HDA_CTL_BIND_MUTE,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100815};
816static const struct snd_kcontrol_new control_templates[] = {
817 HDA_CODEC_VOLUME(NULL, 0, 0, 0),
818 HDA_CODEC_MUTE(NULL, 0, 0, 0),
819 HDA_BIND_MUTE(NULL, 0, 0, 0),
Takashi Iwai352f7f92012-12-19 12:52:06 +0100820};
821
822/* add dynamic controls from template */
Takashi Iwaia35bd1e2013-01-18 14:01:14 +0100823static struct snd_kcontrol_new *
824add_control(struct hda_gen_spec *spec, int type, const char *name,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100825 int cidx, unsigned long val)
826{
827 struct snd_kcontrol_new *knew;
828
Takashi Iwai12c93df2012-12-19 14:38:33 +0100829 knew = snd_hda_gen_add_kctl(spec, name, &control_templates[type]);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100830 if (!knew)
Takashi Iwaia35bd1e2013-01-18 14:01:14 +0100831 return NULL;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100832 knew->index = cidx;
833 if (get_amp_nid_(val))
834 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
835 knew->private_value = val;
Takashi Iwaia35bd1e2013-01-18 14:01:14 +0100836 return knew;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100837}
838
839static int add_control_with_pfx(struct hda_gen_spec *spec, int type,
840 const char *pfx, const char *dir,
841 const char *sfx, int cidx, unsigned long val)
842{
843 char name[32];
844 snprintf(name, sizeof(name), "%s %s %s", pfx, dir, sfx);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +0100845 if (!add_control(spec, type, name, cidx, val))
846 return -ENOMEM;
847 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100848}
849
850#define add_pb_vol_ctrl(spec, type, pfx, val) \
851 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", 0, val)
852#define add_pb_sw_ctrl(spec, type, pfx, val) \
853 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", 0, val)
854#define __add_pb_vol_ctrl(spec, type, pfx, cidx, val) \
855 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", cidx, val)
856#define __add_pb_sw_ctrl(spec, type, pfx, cidx, val) \
857 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", cidx, val)
858
859static int add_vol_ctl(struct hda_codec *codec, const char *pfx, int cidx,
860 unsigned int chs, struct nid_path *path)
861{
862 unsigned int val;
863 if (!path)
864 return 0;
865 val = path->ctls[NID_PATH_VOL_CTL];
866 if (!val)
867 return 0;
868 val = amp_val_replace_channels(val, chs);
869 return __add_pb_vol_ctrl(codec->spec, HDA_CTL_WIDGET_VOL, pfx, cidx, val);
870}
871
872/* return the channel bits suitable for the given path->ctls[] */
873static int get_default_ch_nums(struct hda_codec *codec, struct nid_path *path,
874 int type)
875{
876 int chs = 1; /* mono (left only) */
877 if (path) {
878 hda_nid_t nid = get_amp_nid_(path->ctls[type]);
879 if (nid && (get_wcaps(codec, nid) & AC_WCAP_STEREO))
880 chs = 3; /* stereo */
881 }
882 return chs;
883}
884
885static int add_stereo_vol(struct hda_codec *codec, const char *pfx, int cidx,
886 struct nid_path *path)
887{
888 int chs = get_default_ch_nums(codec, path, NID_PATH_VOL_CTL);
889 return add_vol_ctl(codec, pfx, cidx, chs, path);
890}
891
892/* create a mute-switch for the given mixer widget;
893 * if it has multiple sources (e.g. DAC and loopback), create a bind-mute
894 */
895static int add_sw_ctl(struct hda_codec *codec, const char *pfx, int cidx,
896 unsigned int chs, struct nid_path *path)
897{
898 unsigned int val;
899 int type = HDA_CTL_WIDGET_MUTE;
900
901 if (!path)
902 return 0;
903 val = path->ctls[NID_PATH_MUTE_CTL];
904 if (!val)
905 return 0;
906 val = amp_val_replace_channels(val, chs);
907 if (get_amp_direction_(val) == HDA_INPUT) {
908 hda_nid_t nid = get_amp_nid_(val);
909 int nums = snd_hda_get_num_conns(codec, nid);
910 if (nums > 1) {
911 type = HDA_CTL_BIND_MUTE;
912 val |= nums << 19;
913 }
914 }
915 return __add_pb_sw_ctrl(codec->spec, type, pfx, cidx, val);
916}
917
918static int add_stereo_sw(struct hda_codec *codec, const char *pfx,
919 int cidx, struct nid_path *path)
920{
921 int chs = get_default_ch_nums(codec, path, NID_PATH_MUTE_CTL);
922 return add_sw_ctl(codec, pfx, cidx, chs, path);
923}
924
Takashi Iwai247d85e2013-01-17 16:18:11 +0100925/* any ctl assigned to the path with the given index? */
926static bool path_has_mixer(struct hda_codec *codec, int path_idx, int ctl_type)
927{
928 struct nid_path *path = snd_hda_get_path_from_idx(codec, path_idx);
929 return path && path->ctls[ctl_type];
930}
931
Takashi Iwai352f7f92012-12-19 12:52:06 +0100932static const char * const channel_name[4] = {
933 "Front", "Surround", "CLFE", "Side"
934};
935
936/* give some appropriate ctl name prefix for the given line out channel */
Takashi Iwai247d85e2013-01-17 16:18:11 +0100937static const char *get_line_out_pfx(struct hda_codec *codec, int ch,
938 int *index, int ctl_type)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100939{
Takashi Iwai247d85e2013-01-17 16:18:11 +0100940 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100941 struct auto_pin_cfg *cfg = &spec->autocfg;
942
943 *index = 0;
944 if (cfg->line_outs == 1 && !spec->multi_ios &&
Takashi Iwai247d85e2013-01-17 16:18:11 +0100945 !cfg->hp_outs && !cfg->speaker_outs)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100946 return spec->vmaster_mute.hook ? "PCM" : "Master";
947
948 /* if there is really a single DAC used in the whole output paths,
949 * use it master (or "PCM" if a vmaster hook is present)
950 */
951 if (spec->multiout.num_dacs == 1 && !spec->mixer_nid &&
952 !spec->multiout.hp_out_nid[0] && !spec->multiout.extra_out_nid[0])
953 return spec->vmaster_mute.hook ? "PCM" : "Master";
954
Takashi Iwai247d85e2013-01-17 16:18:11 +0100955 /* multi-io channels */
956 if (ch >= cfg->line_outs)
957 return channel_name[ch];
958
Takashi Iwai352f7f92012-12-19 12:52:06 +0100959 switch (cfg->line_out_type) {
960 case AUTO_PIN_SPEAKER_OUT:
Takashi Iwai247d85e2013-01-17 16:18:11 +0100961 /* if the primary channel vol/mute is shared with HP volume,
962 * don't name it as Speaker
963 */
964 if (!ch && cfg->hp_outs &&
965 !path_has_mixer(codec, spec->hp_paths[0], ctl_type))
966 break;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100967 if (cfg->line_outs == 1)
968 return "Speaker";
969 if (cfg->line_outs == 2)
970 return ch ? "Bass Speaker" : "Speaker";
971 break;
972 case AUTO_PIN_HP_OUT:
Takashi Iwai247d85e2013-01-17 16:18:11 +0100973 /* if the primary channel vol/mute is shared with spk volume,
974 * don't name it as Headphone
975 */
976 if (!ch && cfg->speaker_outs &&
977 !path_has_mixer(codec, spec->speaker_paths[0], ctl_type))
978 break;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100979 /* for multi-io case, only the primary out */
980 if (ch && spec->multi_ios)
981 break;
982 *index = ch;
983 return "Headphone";
Takashi Iwai352f7f92012-12-19 12:52:06 +0100984 }
Takashi Iwai247d85e2013-01-17 16:18:11 +0100985
986 /* for a single channel output, we don't have to name the channel */
987 if (cfg->line_outs == 1 && !spec->multi_ios)
988 return "PCM";
989
Takashi Iwai352f7f92012-12-19 12:52:06 +0100990 if (ch >= ARRAY_SIZE(channel_name)) {
991 snd_BUG();
992 return "PCM";
993 }
994
995 return channel_name[ch];
996}
997
998/*
999 * Parse output paths
1000 */
1001
1002/* badness definition */
1003enum {
1004 /* No primary DAC is found for the main output */
1005 BAD_NO_PRIMARY_DAC = 0x10000,
1006 /* No DAC is found for the extra output */
1007 BAD_NO_DAC = 0x4000,
1008 /* No possible multi-ios */
Takashi Iwai1d739062013-02-13 14:17:32 +01001009 BAD_MULTI_IO = 0x120,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001010 /* No individual DAC for extra output */
1011 BAD_NO_EXTRA_DAC = 0x102,
1012 /* No individual DAC for extra surrounds */
1013 BAD_NO_EXTRA_SURR_DAC = 0x101,
1014 /* Primary DAC shared with main surrounds */
1015 BAD_SHARED_SURROUND = 0x100,
Takashi Iwai55a63d42013-03-21 17:20:12 +01001016 /* No independent HP possible */
Takashi Iwaibec8e682013-03-22 15:10:08 +01001017 BAD_NO_INDEP_HP = 0x10,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001018 /* Primary DAC shared with main CLFE */
1019 BAD_SHARED_CLFE = 0x10,
1020 /* Primary DAC shared with extra surrounds */
1021 BAD_SHARED_EXTRA_SURROUND = 0x10,
1022 /* Volume widget is shared */
1023 BAD_SHARED_VOL = 0x10,
1024};
1025
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001026/* look for widgets in the given path which are appropriate for
Takashi Iwai352f7f92012-12-19 12:52:06 +01001027 * volume and mute controls, and assign the values to ctls[].
1028 *
1029 * When no appropriate widget is found in the path, the badness value
1030 * is incremented depending on the situation. The function returns the
1031 * total badness for both volume and mute controls.
1032 */
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001033static int assign_out_path_ctls(struct hda_codec *codec, struct nid_path *path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001034{
Takashi Iwai352f7f92012-12-19 12:52:06 +01001035 hda_nid_t nid;
1036 unsigned int val;
1037 int badness = 0;
1038
1039 if (!path)
1040 return BAD_SHARED_VOL * 2;
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001041
1042 if (path->ctls[NID_PATH_VOL_CTL] ||
1043 path->ctls[NID_PATH_MUTE_CTL])
1044 return 0; /* already evaluated */
1045
Takashi Iwai352f7f92012-12-19 12:52:06 +01001046 nid = look_for_out_vol_nid(codec, path);
1047 if (nid) {
1048 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
1049 if (is_ctl_used(codec, val, NID_PATH_VOL_CTL))
1050 badness += BAD_SHARED_VOL;
1051 else
1052 path->ctls[NID_PATH_VOL_CTL] = val;
1053 } else
1054 badness += BAD_SHARED_VOL;
1055 nid = look_for_out_mute_nid(codec, path);
1056 if (nid) {
1057 unsigned int wid_type = get_wcaps_type(get_wcaps(codec, nid));
1058 if (wid_type == AC_WID_PIN || wid_type == AC_WID_AUD_OUT ||
1059 nid_has_mute(codec, nid, HDA_OUTPUT))
1060 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
1061 else
1062 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT);
1063 if (is_ctl_used(codec, val, NID_PATH_MUTE_CTL))
1064 badness += BAD_SHARED_VOL;
1065 else
1066 path->ctls[NID_PATH_MUTE_CTL] = val;
1067 } else
1068 badness += BAD_SHARED_VOL;
1069 return badness;
1070}
1071
Takashi Iwai98bd1112013-03-22 14:53:50 +01001072const struct badness_table hda_main_out_badness = {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001073 .no_primary_dac = BAD_NO_PRIMARY_DAC,
1074 .no_dac = BAD_NO_DAC,
1075 .shared_primary = BAD_NO_PRIMARY_DAC,
1076 .shared_surr = BAD_SHARED_SURROUND,
1077 .shared_clfe = BAD_SHARED_CLFE,
1078 .shared_surr_main = BAD_SHARED_SURROUND,
1079};
Takashi Iwai98bd1112013-03-22 14:53:50 +01001080EXPORT_SYMBOL_HDA(hda_main_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001081
Takashi Iwai98bd1112013-03-22 14:53:50 +01001082const struct badness_table hda_extra_out_badness = {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001083 .no_primary_dac = BAD_NO_DAC,
1084 .no_dac = BAD_NO_DAC,
1085 .shared_primary = BAD_NO_EXTRA_DAC,
1086 .shared_surr = BAD_SHARED_EXTRA_SURROUND,
1087 .shared_clfe = BAD_SHARED_EXTRA_SURROUND,
1088 .shared_surr_main = BAD_NO_EXTRA_SURR_DAC,
1089};
Takashi Iwai98bd1112013-03-22 14:53:50 +01001090EXPORT_SYMBOL_HDA(hda_extra_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001091
Takashi Iwai7385df62013-01-07 09:50:52 +01001092/* get the DAC of the primary output corresponding to the given array index */
1093static hda_nid_t get_primary_out(struct hda_codec *codec, int idx)
1094{
1095 struct hda_gen_spec *spec = codec->spec;
1096 struct auto_pin_cfg *cfg = &spec->autocfg;
1097
1098 if (cfg->line_outs > idx)
1099 return spec->private_dac_nids[idx];
1100 idx -= cfg->line_outs;
1101 if (spec->multi_ios > idx)
1102 return spec->multi_io[idx].dac;
1103 return 0;
1104}
1105
1106/* return the DAC if it's reachable, otherwise zero */
1107static inline hda_nid_t try_dac(struct hda_codec *codec,
1108 hda_nid_t dac, hda_nid_t pin)
1109{
1110 return is_reachable_path(codec, dac, pin) ? dac : 0;
1111}
1112
Takashi Iwai352f7f92012-12-19 12:52:06 +01001113/* try to assign DACs to pins and return the resultant badness */
1114static int try_assign_dacs(struct hda_codec *codec, int num_outs,
1115 const hda_nid_t *pins, hda_nid_t *dacs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001116 int *path_idx,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001117 const struct badness_table *bad)
1118{
1119 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001120 int i, j;
1121 int badness = 0;
1122 hda_nid_t dac;
1123
1124 if (!num_outs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 return 0;
1126
Takashi Iwai352f7f92012-12-19 12:52:06 +01001127 for (i = 0; i < num_outs; i++) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001128 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001129 hda_nid_t pin = pins[i];
Takashi Iwai1e0b5282013-01-04 12:56:52 +01001130
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001131 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
1132 if (path) {
1133 badness += assign_out_path_ctls(codec, path);
Takashi Iwai1e0b5282013-01-04 12:56:52 +01001134 continue;
1135 }
1136
1137 dacs[i] = look_for_dac(codec, pin, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001138 if (!dacs[i] && !i) {
Takashi Iwai980428c2013-01-09 09:28:20 +01001139 /* try to steal the DAC of surrounds for the front */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001140 for (j = 1; j < num_outs; j++) {
1141 if (is_reachable_path(codec, dacs[j], pin)) {
1142 dacs[0] = dacs[j];
1143 dacs[j] = 0;
Takashi Iwai980428c2013-01-09 09:28:20 +01001144 invalidate_nid_path(codec, path_idx[j]);
Takashi Iwai196c17662013-01-04 15:01:40 +01001145 path_idx[j] = 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001146 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 }
1148 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001149 }
1150 dac = dacs[i];
1151 if (!dac) {
Takashi Iwai7385df62013-01-07 09:50:52 +01001152 if (num_outs > 2)
1153 dac = try_dac(codec, get_primary_out(codec, i), pin);
1154 if (!dac)
1155 dac = try_dac(codec, dacs[0], pin);
1156 if (!dac)
1157 dac = try_dac(codec, get_primary_out(codec, i), pin);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001158 if (dac) {
1159 if (!i)
1160 badness += bad->shared_primary;
1161 else if (i == 1)
1162 badness += bad->shared_surr;
1163 else
1164 badness += bad->shared_clfe;
1165 } else if (is_reachable_path(codec, spec->private_dac_nids[0], pin)) {
1166 dac = spec->private_dac_nids[0];
1167 badness += bad->shared_surr_main;
1168 } else if (!i)
1169 badness += bad->no_primary_dac;
1170 else
1171 badness += bad->no_dac;
1172 }
Takashi Iwai1fa335b2013-01-21 11:43:19 +01001173 if (!dac)
1174 continue;
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001175 path = snd_hda_add_new_path(codec, dac, pin, -spec->mixer_nid);
Takashi Iwai117688a2013-01-04 15:41:41 +01001176 if (!path && !i && spec->mixer_nid) {
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001177 /* try with aamix */
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001178 path = snd_hda_add_new_path(codec, dac, pin, 0);
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001179 }
Takashi Iwai1fa335b2013-01-21 11:43:19 +01001180 if (!path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001181 dac = dacs[i] = 0;
Takashi Iwai1fa335b2013-01-21 11:43:19 +01001182 badness += bad->no_dac;
1183 } else {
Takashi Iwaia7694092013-01-21 10:43:18 +01001184 /* print_nid_path("output", path); */
Takashi Iwaie1284af2013-01-03 16:33:02 +01001185 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +01001186 path_idx[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001187 badness += assign_out_path_ctls(codec, path);
Takashi Iwaie1284af2013-01-03 16:33:02 +01001188 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001189 }
1190
1191 return badness;
1192}
1193
1194/* return NID if the given pin has only a single connection to a certain DAC */
1195static hda_nid_t get_dac_if_single(struct hda_codec *codec, hda_nid_t pin)
1196{
1197 struct hda_gen_spec *spec = codec->spec;
1198 int i;
1199 hda_nid_t nid_found = 0;
1200
1201 for (i = 0; i < spec->num_all_dacs; i++) {
1202 hda_nid_t nid = spec->all_dacs[i];
1203 if (!nid || is_dac_already_used(codec, nid))
1204 continue;
1205 if (is_reachable_path(codec, nid, pin)) {
1206 if (nid_found)
1207 return 0;
1208 nid_found = nid;
1209 }
1210 }
1211 return nid_found;
1212}
1213
1214/* check whether the given pin can be a multi-io pin */
1215static bool can_be_multiio_pin(struct hda_codec *codec,
1216 unsigned int location, hda_nid_t nid)
1217{
1218 unsigned int defcfg, caps;
1219
1220 defcfg = snd_hda_codec_get_pincfg(codec, nid);
1221 if (get_defcfg_connect(defcfg) != AC_JACK_PORT_COMPLEX)
1222 return false;
1223 if (location && get_defcfg_location(defcfg) != location)
1224 return false;
1225 caps = snd_hda_query_pin_caps(codec, nid);
1226 if (!(caps & AC_PINCAP_OUT))
1227 return false;
1228 return true;
1229}
1230
Takashi Iwaie22aab72013-01-04 14:50:04 +01001231/* count the number of input pins that are capable to be multi-io */
1232static int count_multiio_pins(struct hda_codec *codec, hda_nid_t reference_pin)
1233{
1234 struct hda_gen_spec *spec = codec->spec;
1235 struct auto_pin_cfg *cfg = &spec->autocfg;
1236 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1237 unsigned int location = get_defcfg_location(defcfg);
1238 int type, i;
1239 int num_pins = 0;
1240
1241 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1242 for (i = 0; i < cfg->num_inputs; i++) {
1243 if (cfg->inputs[i].type != type)
1244 continue;
1245 if (can_be_multiio_pin(codec, location,
1246 cfg->inputs[i].pin))
1247 num_pins++;
1248 }
1249 }
1250 return num_pins;
1251}
1252
Takashi Iwai352f7f92012-12-19 12:52:06 +01001253/*
1254 * multi-io helper
1255 *
1256 * When hardwired is set, try to fill ony hardwired pins, and returns
1257 * zero if any pins are filled, non-zero if nothing found.
1258 * When hardwired is off, try to fill possible input pins, and returns
1259 * the badness value.
1260 */
1261static int fill_multi_ios(struct hda_codec *codec,
1262 hda_nid_t reference_pin,
Takashi Iwaie22aab72013-01-04 14:50:04 +01001263 bool hardwired)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001264{
1265 struct hda_gen_spec *spec = codec->spec;
1266 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwaie22aab72013-01-04 14:50:04 +01001267 int type, i, j, num_pins, old_pins;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001268 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1269 unsigned int location = get_defcfg_location(defcfg);
1270 int badness = 0;
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001271 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001272
1273 old_pins = spec->multi_ios;
1274 if (old_pins >= 2)
1275 goto end_fill;
1276
Takashi Iwaie22aab72013-01-04 14:50:04 +01001277 num_pins = count_multiio_pins(codec, reference_pin);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001278 if (num_pins < 2)
1279 goto end_fill;
1280
Takashi Iwai352f7f92012-12-19 12:52:06 +01001281 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1282 for (i = 0; i < cfg->num_inputs; i++) {
1283 hda_nid_t nid = cfg->inputs[i].pin;
1284 hda_nid_t dac = 0;
1285
1286 if (cfg->inputs[i].type != type)
1287 continue;
1288 if (!can_be_multiio_pin(codec, location, nid))
1289 continue;
1290 for (j = 0; j < spec->multi_ios; j++) {
1291 if (nid == spec->multi_io[j].pin)
1292 break;
1293 }
1294 if (j < spec->multi_ios)
1295 continue;
1296
Takashi Iwai352f7f92012-12-19 12:52:06 +01001297 if (hardwired)
1298 dac = get_dac_if_single(codec, nid);
1299 else if (!dac)
1300 dac = look_for_dac(codec, nid, false);
1301 if (!dac) {
1302 badness++;
1303 continue;
1304 }
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001305 path = snd_hda_add_new_path(codec, dac, nid,
1306 -spec->mixer_nid);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001307 if (!path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001308 badness++;
1309 continue;
1310 }
Takashi Iwaia7694092013-01-21 10:43:18 +01001311 /* print_nid_path("multiio", path); */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001312 spec->multi_io[spec->multi_ios].pin = nid;
1313 spec->multi_io[spec->multi_ios].dac = dac;
Takashi Iwai196c17662013-01-04 15:01:40 +01001314 spec->out_paths[cfg->line_outs + spec->multi_ios] =
1315 snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001316 spec->multi_ios++;
1317 if (spec->multi_ios >= 2)
1318 break;
1319 }
1320 }
1321 end_fill:
1322 if (badness)
1323 badness = BAD_MULTI_IO;
1324 if (old_pins == spec->multi_ios) {
1325 if (hardwired)
1326 return 1; /* nothing found */
1327 else
1328 return badness; /* no badness if nothing found */
1329 }
1330 if (!hardwired && spec->multi_ios < 2) {
1331 /* cancel newly assigned paths */
1332 spec->paths.used -= spec->multi_ios - old_pins;
1333 spec->multi_ios = old_pins;
1334 return badness;
1335 }
1336
1337 /* assign volume and mute controls */
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001338 for (i = old_pins; i < spec->multi_ios; i++) {
1339 path = snd_hda_get_path_from_idx(codec, spec->out_paths[cfg->line_outs + i]);
1340 badness += assign_out_path_ctls(codec, path);
1341 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001342
1343 return badness;
1344}
1345
1346/* map DACs for all pins in the list if they are single connections */
1347static bool map_singles(struct hda_codec *codec, int outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001348 const hda_nid_t *pins, hda_nid_t *dacs, int *path_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001349{
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001350 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001351 int i;
1352 bool found = false;
1353 for (i = 0; i < outs; i++) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001354 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001355 hda_nid_t dac;
1356 if (dacs[i])
1357 continue;
1358 dac = get_dac_if_single(codec, pins[i]);
1359 if (!dac)
1360 continue;
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001361 path = snd_hda_add_new_path(codec, dac, pins[i],
1362 -spec->mixer_nid);
Takashi Iwai117688a2013-01-04 15:41:41 +01001363 if (!path && !i && spec->mixer_nid)
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001364 path = snd_hda_add_new_path(codec, dac, pins[i], 0);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001365 if (path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001366 dacs[i] = dac;
1367 found = true;
Takashi Iwaia7694092013-01-21 10:43:18 +01001368 /* print_nid_path("output", path); */
Takashi Iwaie1284af2013-01-03 16:33:02 +01001369 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +01001370 path_idx[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001371 }
1372 }
1373 return found;
1374}
1375
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001376/* create a new path including aamix if available, and return its index */
1377static int check_aamix_out_path(struct hda_codec *codec, int path_idx)
1378{
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001379 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001380 struct nid_path *path;
Takashi Iwai5ead56f2013-04-16 14:16:54 +02001381 hda_nid_t path_dac, dac, pin;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001382
1383 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001384 if (!path || !path->depth ||
1385 is_nid_contained(path, spec->mixer_nid))
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001386 return 0;
Takashi Iwai5ead56f2013-04-16 14:16:54 +02001387 path_dac = path->path[0];
1388 dac = spec->private_dac_nids[0];
Takashi Iwaif87498b2013-01-21 14:24:31 +01001389 pin = path->path[path->depth - 1];
1390 path = snd_hda_add_new_path(codec, dac, pin, spec->mixer_nid);
1391 if (!path) {
Takashi Iwai5ead56f2013-04-16 14:16:54 +02001392 if (dac != path_dac)
1393 dac = path_dac;
Takashi Iwaif87498b2013-01-21 14:24:31 +01001394 else if (spec->multiout.hp_out_nid[0])
1395 dac = spec->multiout.hp_out_nid[0];
1396 else if (spec->multiout.extra_out_nid[0])
1397 dac = spec->multiout.extra_out_nid[0];
Takashi Iwai5ead56f2013-04-16 14:16:54 +02001398 else
1399 dac = 0;
Takashi Iwaif87498b2013-01-21 14:24:31 +01001400 if (dac)
1401 path = snd_hda_add_new_path(codec, dac, pin,
1402 spec->mixer_nid);
1403 }
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001404 if (!path)
1405 return 0;
Takashi Iwaia7694092013-01-21 10:43:18 +01001406 /* print_nid_path("output-aamix", path); */
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001407 path->active = false; /* unused as default */
1408 return snd_hda_get_path_idx(codec, path);
1409}
1410
Takashi Iwai55a63d42013-03-21 17:20:12 +01001411/* check whether the independent HP is available with the current config */
1412static bool indep_hp_possible(struct hda_codec *codec)
1413{
1414 struct hda_gen_spec *spec = codec->spec;
1415 struct auto_pin_cfg *cfg = &spec->autocfg;
1416 struct nid_path *path;
1417 int i, idx;
1418
1419 if (cfg->line_out_type == AUTO_PIN_HP_OUT)
1420 idx = spec->out_paths[0];
1421 else
1422 idx = spec->hp_paths[0];
1423 path = snd_hda_get_path_from_idx(codec, idx);
1424 if (!path)
1425 return false;
1426
1427 /* assume no path conflicts unless aamix is involved */
1428 if (!spec->mixer_nid || !is_nid_contained(path, spec->mixer_nid))
1429 return true;
1430
1431 /* check whether output paths contain aamix */
1432 for (i = 0; i < cfg->line_outs; i++) {
1433 if (spec->out_paths[i] == idx)
1434 break;
1435 path = snd_hda_get_path_from_idx(codec, spec->out_paths[i]);
1436 if (path && is_nid_contained(path, spec->mixer_nid))
1437 return false;
1438 }
1439 for (i = 0; i < cfg->speaker_outs; i++) {
1440 path = snd_hda_get_path_from_idx(codec, spec->speaker_paths[i]);
1441 if (path && is_nid_contained(path, spec->mixer_nid))
1442 return false;
1443 }
1444
1445 return true;
1446}
1447
Takashi Iwaia07a9492013-01-07 16:44:06 +01001448/* fill the empty entries in the dac array for speaker/hp with the
1449 * shared dac pointed by the paths
1450 */
1451static void refill_shared_dacs(struct hda_codec *codec, int num_outs,
1452 hda_nid_t *dacs, int *path_idx)
1453{
1454 struct nid_path *path;
1455 int i;
1456
1457 for (i = 0; i < num_outs; i++) {
1458 if (dacs[i])
1459 continue;
1460 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
1461 if (!path)
1462 continue;
1463 dacs[i] = path->path[0];
1464 }
1465}
1466
Takashi Iwai352f7f92012-12-19 12:52:06 +01001467/* fill in the dac_nids table from the parsed pin configuration */
1468static int fill_and_eval_dacs(struct hda_codec *codec,
1469 bool fill_hardwired,
1470 bool fill_mio_first)
1471{
1472 struct hda_gen_spec *spec = codec->spec;
1473 struct auto_pin_cfg *cfg = &spec->autocfg;
1474 int i, err, badness;
1475
1476 /* set num_dacs once to full for look_for_dac() */
1477 spec->multiout.num_dacs = cfg->line_outs;
1478 spec->multiout.dac_nids = spec->private_dac_nids;
1479 memset(spec->private_dac_nids, 0, sizeof(spec->private_dac_nids));
1480 memset(spec->multiout.hp_out_nid, 0, sizeof(spec->multiout.hp_out_nid));
1481 memset(spec->multiout.extra_out_nid, 0, sizeof(spec->multiout.extra_out_nid));
1482 spec->multi_ios = 0;
1483 snd_array_free(&spec->paths);
Takashi Iwaicd5be3f2013-01-07 15:07:00 +01001484
1485 /* clear path indices */
1486 memset(spec->out_paths, 0, sizeof(spec->out_paths));
1487 memset(spec->hp_paths, 0, sizeof(spec->hp_paths));
1488 memset(spec->speaker_paths, 0, sizeof(spec->speaker_paths));
1489 memset(spec->aamix_out_paths, 0, sizeof(spec->aamix_out_paths));
1490 memset(spec->digout_paths, 0, sizeof(spec->digout_paths));
Takashi Iwaic697b712013-01-07 17:09:26 +01001491 memset(spec->input_paths, 0, sizeof(spec->input_paths));
Takashi Iwaicd5be3f2013-01-07 15:07:00 +01001492 memset(spec->loopback_paths, 0, sizeof(spec->loopback_paths));
1493 memset(&spec->digin_path, 0, sizeof(spec->digin_path));
1494
Takashi Iwai352f7f92012-12-19 12:52:06 +01001495 badness = 0;
1496
1497 /* fill hard-wired DACs first */
1498 if (fill_hardwired) {
1499 bool mapped;
1500 do {
1501 mapped = map_singles(codec, cfg->line_outs,
1502 cfg->line_out_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001503 spec->private_dac_nids,
1504 spec->out_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001505 mapped |= map_singles(codec, cfg->hp_outs,
1506 cfg->hp_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001507 spec->multiout.hp_out_nid,
1508 spec->hp_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001509 mapped |= map_singles(codec, cfg->speaker_outs,
1510 cfg->speaker_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001511 spec->multiout.extra_out_nid,
1512 spec->speaker_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001513 if (fill_mio_first && cfg->line_outs == 1 &&
1514 cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
Takashi Iwaie22aab72013-01-04 14:50:04 +01001515 err = fill_multi_ios(codec, cfg->line_out_pins[0], true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001516 if (!err)
1517 mapped = true;
1518 }
1519 } while (mapped);
1520 }
1521
1522 badness += try_assign_dacs(codec, cfg->line_outs, cfg->line_out_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001523 spec->private_dac_nids, spec->out_paths,
Takashi Iwai98bd1112013-03-22 14:53:50 +01001524 spec->main_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001525
Takashi Iwai352f7f92012-12-19 12:52:06 +01001526 if (fill_mio_first &&
1527 cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1528 /* try to fill multi-io first */
Takashi Iwaie22aab72013-01-04 14:50:04 +01001529 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001530 if (err < 0)
1531 return err;
1532 /* we don't count badness at this stage yet */
1533 }
1534
1535 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
1536 err = try_assign_dacs(codec, cfg->hp_outs, cfg->hp_pins,
1537 spec->multiout.hp_out_nid,
Takashi Iwai196c17662013-01-04 15:01:40 +01001538 spec->hp_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_out_type != AUTO_PIN_SPEAKER_OUT) {
1545 err = try_assign_dacs(codec, cfg->speaker_outs,
1546 cfg->speaker_pins,
1547 spec->multiout.extra_out_nid,
Takashi Iwai196c17662013-01-04 15:01:40 +01001548 spec->speaker_paths,
Takashi Iwai98bd1112013-03-22 14:53:50 +01001549 spec->extra_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001550 if (err < 0)
1551 return err;
1552 badness += err;
1553 }
1554 if (cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
Takashi Iwaie22aab72013-01-04 14:50:04 +01001555 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001556 if (err < 0)
1557 return err;
1558 badness += err;
1559 }
Takashi Iwaie22aab72013-01-04 14:50:04 +01001560
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001561 if (spec->mixer_nid) {
1562 spec->aamix_out_paths[0] =
1563 check_aamix_out_path(codec, spec->out_paths[0]);
1564 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1565 spec->aamix_out_paths[1] =
1566 check_aamix_out_path(codec, spec->hp_paths[0]);
1567 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1568 spec->aamix_out_paths[2] =
1569 check_aamix_out_path(codec, spec->speaker_paths[0]);
1570 }
1571
Takashi Iwaie22aab72013-01-04 14:50:04 +01001572 if (cfg->hp_outs && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
1573 if (count_multiio_pins(codec, cfg->hp_pins[0]) >= 2)
1574 spec->multi_ios = 1; /* give badness */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001575
Takashi Iwaia07a9492013-01-07 16:44:06 +01001576 /* re-count num_dacs and squash invalid entries */
1577 spec->multiout.num_dacs = 0;
1578 for (i = 0; i < cfg->line_outs; i++) {
1579 if (spec->private_dac_nids[i])
1580 spec->multiout.num_dacs++;
1581 else {
1582 memmove(spec->private_dac_nids + i,
1583 spec->private_dac_nids + i + 1,
1584 sizeof(hda_nid_t) * (cfg->line_outs - i - 1));
1585 spec->private_dac_nids[cfg->line_outs - 1] = 0;
1586 }
1587 }
1588
1589 spec->ext_channel_count = spec->min_channel_count =
David Henningssonc0f3b212013-01-16 11:45:37 +01001590 spec->multiout.num_dacs * 2;
Takashi Iwaia07a9492013-01-07 16:44:06 +01001591
Takashi Iwai352f7f92012-12-19 12:52:06 +01001592 if (spec->multi_ios == 2) {
1593 for (i = 0; i < 2; i++)
1594 spec->private_dac_nids[spec->multiout.num_dacs++] =
1595 spec->multi_io[i].dac;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001596 } else if (spec->multi_ios) {
1597 spec->multi_ios = 0;
1598 badness += BAD_MULTI_IO;
1599 }
1600
Takashi Iwai55a63d42013-03-21 17:20:12 +01001601 if (spec->indep_hp && !indep_hp_possible(codec))
1602 badness += BAD_NO_INDEP_HP;
1603
Takashi Iwaia07a9492013-01-07 16:44:06 +01001604 /* re-fill the shared DAC for speaker / headphone */
1605 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1606 refill_shared_dacs(codec, cfg->hp_outs,
1607 spec->multiout.hp_out_nid,
1608 spec->hp_paths);
1609 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1610 refill_shared_dacs(codec, cfg->speaker_outs,
1611 spec->multiout.extra_out_nid,
1612 spec->speaker_paths);
1613
Takashi Iwai352f7f92012-12-19 12:52:06 +01001614 return badness;
1615}
1616
1617#define DEBUG_BADNESS
1618
1619#ifdef DEBUG_BADNESS
1620#define debug_badness snd_printdd
1621#else
1622#define debug_badness(...)
1623#endif
1624
Takashi Iwaia7694092013-01-21 10:43:18 +01001625#ifdef DEBUG_BADNESS
1626static inline void print_nid_path_idx(struct hda_codec *codec,
1627 const char *pfx, int idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001628{
Takashi Iwaia7694092013-01-21 10:43:18 +01001629 struct nid_path *path;
1630
1631 path = snd_hda_get_path_from_idx(codec, idx);
1632 if (path)
1633 print_nid_path(pfx, path);
1634}
1635
1636static void debug_show_configs(struct hda_codec *codec,
1637 struct auto_pin_cfg *cfg)
1638{
1639 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia7694092013-01-21 10:43:18 +01001640 static const char * const lo_type[3] = { "LO", "SP", "HP" };
Takashi Iwaia7694092013-01-21 10:43:18 +01001641 int i;
1642
1643 debug_badness("multi_outs = %x/%x/%x/%x : %x/%x/%x/%x (type %s)\n",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001644 cfg->line_out_pins[0], cfg->line_out_pins[1],
Takashi Iwai708122e2012-12-20 17:56:57 +01001645 cfg->line_out_pins[2], cfg->line_out_pins[3],
Takashi Iwai352f7f92012-12-19 12:52:06 +01001646 spec->multiout.dac_nids[0],
1647 spec->multiout.dac_nids[1],
1648 spec->multiout.dac_nids[2],
Takashi Iwaia7694092013-01-21 10:43:18 +01001649 spec->multiout.dac_nids[3],
1650 lo_type[cfg->line_out_type]);
1651 for (i = 0; i < cfg->line_outs; i++)
1652 print_nid_path_idx(codec, " out", spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001653 if (spec->multi_ios > 0)
1654 debug_badness("multi_ios(%d) = %x/%x : %x/%x\n",
1655 spec->multi_ios,
1656 spec->multi_io[0].pin, spec->multi_io[1].pin,
1657 spec->multi_io[0].dac, spec->multi_io[1].dac);
Takashi Iwaia7694092013-01-21 10:43:18 +01001658 for (i = 0; i < spec->multi_ios; i++)
1659 print_nid_path_idx(codec, " mio",
1660 spec->out_paths[cfg->line_outs + i]);
1661 if (cfg->hp_outs)
1662 debug_badness("hp_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001663 cfg->hp_pins[0], cfg->hp_pins[1],
Takashi Iwai708122e2012-12-20 17:56:57 +01001664 cfg->hp_pins[2], cfg->hp_pins[3],
Takashi Iwai352f7f92012-12-19 12:52:06 +01001665 spec->multiout.hp_out_nid[0],
1666 spec->multiout.hp_out_nid[1],
1667 spec->multiout.hp_out_nid[2],
1668 spec->multiout.hp_out_nid[3]);
Takashi Iwaia7694092013-01-21 10:43:18 +01001669 for (i = 0; i < cfg->hp_outs; i++)
1670 print_nid_path_idx(codec, " hp ", spec->hp_paths[i]);
1671 if (cfg->speaker_outs)
1672 debug_badness("spk_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001673 cfg->speaker_pins[0], cfg->speaker_pins[1],
1674 cfg->speaker_pins[2], cfg->speaker_pins[3],
1675 spec->multiout.extra_out_nid[0],
1676 spec->multiout.extra_out_nid[1],
1677 spec->multiout.extra_out_nid[2],
1678 spec->multiout.extra_out_nid[3]);
Takashi Iwaia7694092013-01-21 10:43:18 +01001679 for (i = 0; i < cfg->speaker_outs; i++)
1680 print_nid_path_idx(codec, " spk", spec->speaker_paths[i]);
1681 for (i = 0; i < 3; i++)
1682 print_nid_path_idx(codec, " mix", spec->aamix_out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001683}
Takashi Iwaia7694092013-01-21 10:43:18 +01001684#else
1685#define debug_show_configs(codec, cfg) /* NOP */
1686#endif
Takashi Iwai352f7f92012-12-19 12:52:06 +01001687
1688/* find all available DACs of the codec */
1689static void fill_all_dac_nids(struct hda_codec *codec)
1690{
1691 struct hda_gen_spec *spec = codec->spec;
1692 int i;
1693 hda_nid_t nid = codec->start_nid;
1694
1695 spec->num_all_dacs = 0;
1696 memset(spec->all_dacs, 0, sizeof(spec->all_dacs));
1697 for (i = 0; i < codec->num_nodes; i++, nid++) {
1698 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_AUD_OUT)
1699 continue;
1700 if (spec->num_all_dacs >= ARRAY_SIZE(spec->all_dacs)) {
1701 snd_printk(KERN_ERR "hda: Too many DACs!\n");
1702 break;
1703 }
1704 spec->all_dacs[spec->num_all_dacs++] = nid;
1705 }
1706}
1707
1708static int parse_output_paths(struct hda_codec *codec)
1709{
1710 struct hda_gen_spec *spec = codec->spec;
1711 struct auto_pin_cfg *cfg = &spec->autocfg;
1712 struct auto_pin_cfg *best_cfg;
Takashi Iwai9314a582013-01-21 10:49:05 +01001713 unsigned int val;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001714 int best_badness = INT_MAX;
1715 int badness;
1716 bool fill_hardwired = true, fill_mio_first = true;
1717 bool best_wired = true, best_mio = true;
1718 bool hp_spk_swapped = false;
1719
Takashi Iwai352f7f92012-12-19 12:52:06 +01001720 best_cfg = kmalloc(sizeof(*best_cfg), GFP_KERNEL);
1721 if (!best_cfg)
1722 return -ENOMEM;
1723 *best_cfg = *cfg;
1724
1725 for (;;) {
1726 badness = fill_and_eval_dacs(codec, fill_hardwired,
1727 fill_mio_first);
1728 if (badness < 0) {
1729 kfree(best_cfg);
1730 return badness;
1731 }
1732 debug_badness("==> lo_type=%d, wired=%d, mio=%d, badness=0x%x\n",
1733 cfg->line_out_type, fill_hardwired, fill_mio_first,
1734 badness);
Takashi Iwaia7694092013-01-21 10:43:18 +01001735 debug_show_configs(codec, cfg);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001736 if (badness < best_badness) {
1737 best_badness = badness;
1738 *best_cfg = *cfg;
1739 best_wired = fill_hardwired;
1740 best_mio = fill_mio_first;
1741 }
1742 if (!badness)
1743 break;
1744 fill_mio_first = !fill_mio_first;
1745 if (!fill_mio_first)
1746 continue;
1747 fill_hardwired = !fill_hardwired;
1748 if (!fill_hardwired)
1749 continue;
1750 if (hp_spk_swapped)
1751 break;
1752 hp_spk_swapped = true;
1753 if (cfg->speaker_outs > 0 &&
1754 cfg->line_out_type == AUTO_PIN_HP_OUT) {
1755 cfg->hp_outs = cfg->line_outs;
1756 memcpy(cfg->hp_pins, cfg->line_out_pins,
1757 sizeof(cfg->hp_pins));
1758 cfg->line_outs = cfg->speaker_outs;
1759 memcpy(cfg->line_out_pins, cfg->speaker_pins,
1760 sizeof(cfg->speaker_pins));
1761 cfg->speaker_outs = 0;
1762 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
1763 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
1764 fill_hardwired = true;
1765 continue;
1766 }
1767 if (cfg->hp_outs > 0 &&
1768 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
1769 cfg->speaker_outs = cfg->line_outs;
1770 memcpy(cfg->speaker_pins, cfg->line_out_pins,
1771 sizeof(cfg->speaker_pins));
1772 cfg->line_outs = cfg->hp_outs;
1773 memcpy(cfg->line_out_pins, cfg->hp_pins,
1774 sizeof(cfg->hp_pins));
1775 cfg->hp_outs = 0;
1776 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
1777 cfg->line_out_type = AUTO_PIN_HP_OUT;
1778 fill_hardwired = true;
1779 continue;
1780 }
1781 break;
1782 }
1783
1784 if (badness) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001785 debug_badness("==> restoring best_cfg\n");
Takashi Iwai352f7f92012-12-19 12:52:06 +01001786 *cfg = *best_cfg;
1787 fill_and_eval_dacs(codec, best_wired, best_mio);
1788 }
1789 debug_badness("==> Best config: lo_type=%d, wired=%d, mio=%d\n",
1790 cfg->line_out_type, best_wired, best_mio);
Takashi Iwaia7694092013-01-21 10:43:18 +01001791 debug_show_configs(codec, cfg);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001792
1793 if (cfg->line_out_pins[0]) {
1794 struct nid_path *path;
Takashi Iwai196c17662013-01-04 15:01:40 +01001795 path = snd_hda_get_path_from_idx(codec, spec->out_paths[0]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001796 if (path)
1797 spec->vmaster_nid = look_for_out_vol_nid(codec, path);
Takashi Iwai7a71bbf2013-01-17 10:25:15 +01001798 if (spec->vmaster_nid)
1799 snd_hda_set_vmaster_tlv(codec, spec->vmaster_nid,
1800 HDA_OUTPUT, spec->vmaster_tlv);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001801 }
1802
Takashi Iwai9314a582013-01-21 10:49:05 +01001803 /* set initial pinctl targets */
1804 if (spec->prefer_hp_amp || cfg->line_out_type == AUTO_PIN_HP_OUT)
1805 val = PIN_HP;
1806 else
1807 val = PIN_OUT;
1808 set_pin_targets(codec, cfg->line_outs, cfg->line_out_pins, val);
1809 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1810 set_pin_targets(codec, cfg->hp_outs, cfg->hp_pins, PIN_HP);
1811 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1812 val = spec->prefer_hp_amp ? PIN_HP : PIN_OUT;
1813 set_pin_targets(codec, cfg->speaker_outs,
1814 cfg->speaker_pins, val);
1815 }
1816
Takashi Iwai55a63d42013-03-21 17:20:12 +01001817 /* clear indep_hp flag if not available */
1818 if (spec->indep_hp && !indep_hp_possible(codec))
1819 spec->indep_hp = 0;
1820
Takashi Iwai352f7f92012-12-19 12:52:06 +01001821 kfree(best_cfg);
1822 return 0;
1823}
1824
1825/* add playback controls from the parsed DAC table */
1826static int create_multi_out_ctls(struct hda_codec *codec,
1827 const struct auto_pin_cfg *cfg)
1828{
1829 struct hda_gen_spec *spec = codec->spec;
1830 int i, err, noutputs;
1831
1832 noutputs = cfg->line_outs;
1833 if (spec->multi_ios > 0 && cfg->line_outs < 3)
1834 noutputs += spec->multi_ios;
1835
1836 for (i = 0; i < noutputs; i++) {
1837 const char *name;
1838 int index;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001839 struct nid_path *path;
1840
Takashi Iwai196c17662013-01-04 15:01:40 +01001841 path = snd_hda_get_path_from_idx(codec, spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001842 if (!path)
1843 continue;
Takashi Iwai247d85e2013-01-17 16:18:11 +01001844
1845 name = get_line_out_pfx(codec, i, &index, NID_PATH_VOL_CTL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001846 if (!name || !strcmp(name, "CLFE")) {
1847 /* Center/LFE */
1848 err = add_vol_ctl(codec, "Center", 0, 1, path);
1849 if (err < 0)
1850 return err;
1851 err = add_vol_ctl(codec, "LFE", 0, 2, path);
1852 if (err < 0)
1853 return err;
Takashi Iwai247d85e2013-01-17 16:18:11 +01001854 } else {
1855 err = add_stereo_vol(codec, name, index, path);
1856 if (err < 0)
1857 return err;
1858 }
1859
1860 name = get_line_out_pfx(codec, i, &index, NID_PATH_MUTE_CTL);
1861 if (!name || !strcmp(name, "CLFE")) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001862 err = add_sw_ctl(codec, "Center", 0, 1, path);
1863 if (err < 0)
1864 return err;
1865 err = add_sw_ctl(codec, "LFE", 0, 2, path);
1866 if (err < 0)
1867 return err;
1868 } else {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001869 err = add_stereo_sw(codec, name, index, path);
1870 if (err < 0)
1871 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 }
1873 }
1874 return 0;
1875}
1876
Takashi Iwaic2c80382013-01-07 10:33:57 +01001877static int create_extra_out(struct hda_codec *codec, int path_idx,
Takashi Iwai196c17662013-01-04 15:01:40 +01001878 const char *pfx, int cidx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879{
Takashi Iwai352f7f92012-12-19 12:52:06 +01001880 struct nid_path *path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 int err;
1882
Takashi Iwai196c17662013-01-04 15:01:40 +01001883 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001884 if (!path)
1885 return 0;
Takashi Iwaic2c80382013-01-07 10:33:57 +01001886 err = add_stereo_vol(codec, pfx, cidx, path);
1887 if (err < 0)
1888 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001889 err = add_stereo_sw(codec, pfx, cidx, path);
1890 if (err < 0)
1891 return err;
1892 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893}
1894
Takashi Iwai352f7f92012-12-19 12:52:06 +01001895/* add playback controls for speaker and HP outputs */
1896static int create_extra_outs(struct hda_codec *codec, int num_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001897 const int *paths, const char *pfx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898{
Takashi Iwaic2c80382013-01-07 10:33:57 +01001899 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001900
1901 for (i = 0; i < num_pins; i++) {
Takashi Iwaic2c80382013-01-07 10:33:57 +01001902 const char *name;
1903 char tmp[44];
1904 int err, idx = 0;
1905
1906 if (num_pins == 2 && i == 1 && !strcmp(pfx, "Speaker"))
1907 name = "Bass Speaker";
1908 else if (num_pins >= 3) {
1909 snprintf(tmp, sizeof(tmp), "%s %s",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001910 pfx, channel_name[i]);
Takashi Iwaic2c80382013-01-07 10:33:57 +01001911 name = tmp;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001912 } else {
Takashi Iwaic2c80382013-01-07 10:33:57 +01001913 name = pfx;
1914 idx = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 }
Takashi Iwaic2c80382013-01-07 10:33:57 +01001916 err = create_extra_out(codec, paths[i], name, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001917 if (err < 0)
1918 return err;
1919 }
1920 return 0;
1921}
Takashi Iwai97ec5582006-03-21 11:29:07 +01001922
Takashi Iwai352f7f92012-12-19 12:52:06 +01001923static int create_hp_out_ctls(struct hda_codec *codec)
1924{
1925 struct hda_gen_spec *spec = codec->spec;
1926 return create_extra_outs(codec, spec->autocfg.hp_outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001927 spec->hp_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001928 "Headphone");
1929}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930
Takashi Iwai352f7f92012-12-19 12:52:06 +01001931static int create_speaker_out_ctls(struct hda_codec *codec)
1932{
1933 struct hda_gen_spec *spec = codec->spec;
1934 return create_extra_outs(codec, spec->autocfg.speaker_outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001935 spec->speaker_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001936 "Speaker");
1937}
1938
1939/*
Takashi Iwai38cf6f12012-12-21 14:09:42 +01001940 * independent HP controls
1941 */
1942
Takashi Iwai963afde2013-05-31 15:20:31 +02001943static void call_hp_automute(struct hda_codec *codec, struct hda_jack_tbl *jack);
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 Iwai963afde2013-05-31 15:20:31 +02002004 call_hp_automute(codec, NULL);
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 Iwai65033cc2013-04-16 12:31:05 +02002085/* Default value to be passed as aamix argument for snd_hda_activate_path();
2086 * used for output paths
2087 */
2088static bool aamix_default(struct hda_gen_spec *spec)
2089{
2090 return !spec->have_aamix_ctl || spec->aamix_mode;
2091}
2092
Takashi Iwai352f7f92012-12-19 12:52:06 +01002093static int set_multi_io(struct hda_codec *codec, int idx, bool output)
2094{
2095 struct hda_gen_spec *spec = codec->spec;
2096 hda_nid_t nid = spec->multi_io[idx].pin;
2097 struct nid_path *path;
2098
Takashi Iwai196c17662013-01-04 15:01:40 +01002099 path = get_multiio_path(codec, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002100 if (!path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 return -EINVAL;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002102
2103 if (path->active == output)
2104 return 0;
2105
2106 if (output) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01002107 set_pin_target(codec, nid, PIN_OUT, true);
Takashi Iwai65033cc2013-04-16 12:31:05 +02002108 snd_hda_activate_path(codec, path, true, aamix_default(spec));
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01002109 set_pin_eapd(codec, nid, true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002110 } else {
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01002111 set_pin_eapd(codec, nid, false);
Takashi Iwai65033cc2013-04-16 12:31:05 +02002112 snd_hda_activate_path(codec, path, false, aamix_default(spec));
Takashi Iwai2c12c302013-01-10 09:33:29 +01002113 set_pin_target(codec, nid, spec->multi_io[idx].ctl_in, true);
Takashi Iwai55196ff2013-01-24 17:32:56 +01002114 path_power_down_sync(codec, path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 }
Takashi Iwaia365fed2013-01-10 16:10:06 +01002116
2117 /* update jack retasking in case it modifies any of them */
Takashi Iwaia5cc2502013-01-16 18:08:55 +01002118 update_automute_all(codec);
Takashi Iwaia365fed2013-01-10 16:10:06 +01002119
Takashi Iwai352f7f92012-12-19 12:52:06 +01002120 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121}
2122
Takashi Iwai352f7f92012-12-19 12:52:06 +01002123static int ch_mode_put(struct snd_kcontrol *kcontrol,
2124 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002126 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2127 struct hda_gen_spec *spec = codec->spec;
2128 int i, ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129
Takashi Iwai352f7f92012-12-19 12:52:06 +01002130 ch = ucontrol->value.enumerated.item[0];
2131 if (ch < 0 || ch > spec->multi_ios)
2132 return -EINVAL;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002133 if (ch == (spec->ext_channel_count - spec->min_channel_count) / 2)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002134 return 0;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002135 spec->ext_channel_count = ch * 2 + spec->min_channel_count;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002136 for (i = 0; i < spec->multi_ios; i++)
2137 set_multi_io(codec, i, i < ch);
2138 spec->multiout.max_channels = max(spec->ext_channel_count,
2139 spec->const_channel_count);
2140 if (spec->need_dac_fix)
2141 spec->multiout.num_dacs = spec->multiout.max_channels / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 return 1;
2143}
2144
Takashi Iwai352f7f92012-12-19 12:52:06 +01002145static const struct snd_kcontrol_new channel_mode_enum = {
2146 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2147 .name = "Channel Mode",
2148 .info = ch_mode_info,
2149 .get = ch_mode_get,
2150 .put = ch_mode_put,
2151};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152
Takashi Iwai352f7f92012-12-19 12:52:06 +01002153static int create_multi_channel_mode(struct hda_codec *codec)
2154{
2155 struct hda_gen_spec *spec = codec->spec;
2156
2157 if (spec->multi_ios > 0) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01002158 if (!snd_hda_gen_add_kctl(spec, NULL, &channel_mode_enum))
Takashi Iwai352f7f92012-12-19 12:52:06 +01002159 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 return 0;
2162}
2163
Takashi Iwai352f7f92012-12-19 12:52:06 +01002164/*
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002165 * aamix loopback enable/disable switch
2166 */
2167
2168#define loopback_mixing_info indep_hp_info
2169
2170static int loopback_mixing_get(struct snd_kcontrol *kcontrol,
2171 struct snd_ctl_elem_value *ucontrol)
2172{
2173 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2174 struct hda_gen_spec *spec = codec->spec;
2175 ucontrol->value.enumerated.item[0] = spec->aamix_mode;
2176 return 0;
2177}
2178
2179static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002180 int nomix_path_idx, int mix_path_idx,
2181 int out_type)
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002182{
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002183 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002184 struct nid_path *nomix_path, *mix_path;
2185
2186 nomix_path = snd_hda_get_path_from_idx(codec, nomix_path_idx);
2187 mix_path = snd_hda_get_path_from_idx(codec, mix_path_idx);
2188 if (!nomix_path || !mix_path)
2189 return;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002190
2191 /* if HP aamix path is driven from a different DAC and the
2192 * independent HP mode is ON, can't turn on aamix path
2193 */
2194 if (out_type == AUTO_PIN_HP_OUT && spec->indep_hp_enabled &&
2195 mix_path->path[0] != spec->alt_dac_nid)
2196 do_mix = false;
2197
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002198 if (do_mix) {
2199 snd_hda_activate_path(codec, nomix_path, false, true);
2200 snd_hda_activate_path(codec, mix_path, true, true);
Takashi Iwai55196ff2013-01-24 17:32:56 +01002201 path_power_down_sync(codec, nomix_path);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002202 } else {
Takashi Iwai65033cc2013-04-16 12:31:05 +02002203 snd_hda_activate_path(codec, mix_path, false, false);
2204 snd_hda_activate_path(codec, nomix_path, true, false);
Takashi Iwai55196ff2013-01-24 17:32:56 +01002205 path_power_down_sync(codec, mix_path);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002206 }
2207}
2208
2209static int loopback_mixing_put(struct snd_kcontrol *kcontrol,
2210 struct snd_ctl_elem_value *ucontrol)
2211{
2212 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2213 struct hda_gen_spec *spec = codec->spec;
2214 unsigned int val = ucontrol->value.enumerated.item[0];
2215
2216 if (val == spec->aamix_mode)
2217 return 0;
2218 spec->aamix_mode = val;
2219 update_aamix_paths(codec, val, spec->out_paths[0],
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002220 spec->aamix_out_paths[0],
2221 spec->autocfg.line_out_type);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002222 update_aamix_paths(codec, val, spec->hp_paths[0],
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002223 spec->aamix_out_paths[1],
2224 AUTO_PIN_HP_OUT);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002225 update_aamix_paths(codec, val, spec->speaker_paths[0],
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002226 spec->aamix_out_paths[2],
2227 AUTO_PIN_SPEAKER_OUT);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002228 return 1;
2229}
2230
2231static const struct snd_kcontrol_new loopback_mixing_enum = {
2232 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2233 .name = "Loopback Mixing",
2234 .info = loopback_mixing_info,
2235 .get = loopback_mixing_get,
2236 .put = loopback_mixing_put,
2237};
2238
2239static int create_loopback_mixing_ctl(struct hda_codec *codec)
2240{
2241 struct hda_gen_spec *spec = codec->spec;
2242
2243 if (!spec->mixer_nid)
2244 return 0;
2245 if (!(spec->aamix_out_paths[0] || spec->aamix_out_paths[1] ||
2246 spec->aamix_out_paths[2]))
2247 return 0;
2248 if (!snd_hda_gen_add_kctl(spec, NULL, &loopback_mixing_enum))
2249 return -ENOMEM;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002250 spec->have_aamix_ctl = 1;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002251 return 0;
2252}
2253
2254/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01002255 * shared headphone/mic handling
2256 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02002257
Takashi Iwai352f7f92012-12-19 12:52:06 +01002258static void call_update_outputs(struct hda_codec *codec);
2259
2260/* for shared I/O, change the pin-control accordingly */
Takashi Iwai967303d2013-02-19 17:12:42 +01002261static void update_hp_mic(struct hda_codec *codec, int adc_mux, bool force)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002262{
2263 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai967303d2013-02-19 17:12:42 +01002264 bool as_mic;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002265 unsigned int val;
Takashi Iwai967303d2013-02-19 17:12:42 +01002266 hda_nid_t pin;
2267
2268 pin = spec->hp_mic_pin;
2269 as_mic = spec->cur_mux[adc_mux] == spec->hp_mic_mux_idx;
2270
2271 if (!force) {
2272 val = snd_hda_codec_get_pin_target(codec, pin);
2273 if (as_mic) {
2274 if (val & PIN_IN)
2275 return;
2276 } else {
2277 if (val & PIN_OUT)
2278 return;
2279 }
2280 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002281
2282 val = snd_hda_get_default_vref(codec, pin);
Takashi Iwai967303d2013-02-19 17:12:42 +01002283 /* if the HP pin doesn't support VREF and the codec driver gives an
2284 * alternative pin, set up the VREF on that pin instead
2285 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01002286 if (val == AC_PINCTL_VREF_HIZ && spec->shared_mic_vref_pin) {
2287 const hda_nid_t vref_pin = spec->shared_mic_vref_pin;
2288 unsigned int vref_val = snd_hda_get_default_vref(codec, vref_pin);
2289 if (vref_val != AC_PINCTL_VREF_HIZ)
Takashi Iwai7594aa32012-12-20 15:38:40 +01002290 snd_hda_set_pin_ctl_cache(codec, vref_pin,
Takashi Iwai967303d2013-02-19 17:12:42 +01002291 PIN_IN | (as_mic ? vref_val : 0));
Takashi Iwaicb53c622007-08-10 17:21:45 +02002292 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002293
Takashi Iwai8ba955c2013-03-07 18:40:58 +01002294 if (!spec->hp_mic_jack_modes) {
2295 if (as_mic)
2296 val |= PIN_IN;
2297 else
2298 val = PIN_HP;
2299 set_pin_target(codec, pin, val, true);
Takashi Iwai963afde2013-05-31 15:20:31 +02002300 call_hp_automute(codec, NULL);
Takashi Iwai8ba955c2013-03-07 18:40:58 +01002301 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002302}
2303
2304/* create a shared input with the headphone out */
Takashi Iwai967303d2013-02-19 17:12:42 +01002305static int create_hp_mic(struct hda_codec *codec)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002306{
2307 struct hda_gen_spec *spec = codec->spec;
2308 struct auto_pin_cfg *cfg = &spec->autocfg;
2309 unsigned int defcfg;
2310 hda_nid_t nid;
2311
Takashi Iwai967303d2013-02-19 17:12:42 +01002312 if (!spec->hp_mic) {
2313 if (spec->suppress_hp_mic_detect)
2314 return 0;
2315 /* automatic detection: only if no input or a single internal
2316 * input pin is found, try to detect the shared hp/mic
2317 */
2318 if (cfg->num_inputs > 1)
2319 return 0;
2320 else if (cfg->num_inputs == 1) {
2321 defcfg = snd_hda_codec_get_pincfg(codec, cfg->inputs[0].pin);
2322 if (snd_hda_get_input_pin_attr(defcfg) != INPUT_PIN_ATTR_INT)
2323 return 0;
2324 }
2325 }
2326
2327 spec->hp_mic = 0; /* clear once */
2328 if (cfg->num_inputs >= AUTO_CFG_MAX_INS)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002329 return 0;
2330
Takashi Iwai967303d2013-02-19 17:12:42 +01002331 nid = 0;
2332 if (cfg->line_out_type == AUTO_PIN_HP_OUT && cfg->line_outs > 0)
2333 nid = cfg->line_out_pins[0];
2334 else if (cfg->hp_outs > 0)
2335 nid = cfg->hp_pins[0];
2336 if (!nid)
2337 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002338
2339 if (!(snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_IN))
2340 return 0; /* no input */
2341
Takashi Iwai967303d2013-02-19 17:12:42 +01002342 cfg->inputs[cfg->num_inputs].pin = nid;
2343 cfg->inputs[cfg->num_inputs].type = AUTO_PIN_MIC;
David Henningssoncb420b12013-04-11 11:30:28 +02002344 cfg->inputs[cfg->num_inputs].is_headphone_mic = 1;
Takashi Iwai967303d2013-02-19 17:12:42 +01002345 cfg->num_inputs++;
2346 spec->hp_mic = 1;
2347 spec->hp_mic_pin = nid;
2348 /* we can't handle auto-mic together with HP-mic */
2349 spec->suppress_auto_mic = 1;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002350 snd_printdd("hda-codec: Enable shared I/O jack on NID 0x%x\n", nid);
2351 return 0;
2352}
2353
Takashi Iwai978e77e2013-01-10 16:57:58 +01002354/*
2355 * output jack mode
2356 */
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002357
2358static int create_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t pin);
2359
2360static const char * const out_jack_texts[] = {
2361 "Line Out", "Headphone Out",
2362};
2363
Takashi Iwai978e77e2013-01-10 16:57:58 +01002364static int out_jack_mode_info(struct snd_kcontrol *kcontrol,
2365 struct snd_ctl_elem_info *uinfo)
2366{
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002367 return snd_hda_enum_helper_info(kcontrol, uinfo, 2, out_jack_texts);
Takashi Iwai978e77e2013-01-10 16:57:58 +01002368}
2369
2370static int out_jack_mode_get(struct snd_kcontrol *kcontrol,
2371 struct snd_ctl_elem_value *ucontrol)
2372{
2373 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2374 hda_nid_t nid = kcontrol->private_value;
2375 if (snd_hda_codec_get_pin_target(codec, nid) == PIN_HP)
2376 ucontrol->value.enumerated.item[0] = 1;
2377 else
2378 ucontrol->value.enumerated.item[0] = 0;
2379 return 0;
2380}
2381
2382static int out_jack_mode_put(struct snd_kcontrol *kcontrol,
2383 struct snd_ctl_elem_value *ucontrol)
2384{
2385 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2386 hda_nid_t nid = kcontrol->private_value;
2387 unsigned int val;
2388
2389 val = ucontrol->value.enumerated.item[0] ? PIN_HP : PIN_OUT;
2390 if (snd_hda_codec_get_pin_target(codec, nid) == val)
2391 return 0;
2392 snd_hda_set_pin_ctl_cache(codec, nid, val);
2393 return 1;
2394}
2395
2396static const struct snd_kcontrol_new out_jack_mode_enum = {
2397 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2398 .info = out_jack_mode_info,
2399 .get = out_jack_mode_get,
2400 .put = out_jack_mode_put,
2401};
2402
2403static bool find_kctl_name(struct hda_codec *codec, const char *name, int idx)
2404{
2405 struct hda_gen_spec *spec = codec->spec;
2406 int i;
2407
2408 for (i = 0; i < spec->kctls.used; i++) {
2409 struct snd_kcontrol_new *kctl = snd_array_elem(&spec->kctls, i);
2410 if (!strcmp(kctl->name, name) && kctl->index == idx)
2411 return true;
2412 }
2413 return false;
2414}
2415
2416static void get_jack_mode_name(struct hda_codec *codec, hda_nid_t pin,
2417 char *name, size_t name_len)
2418{
2419 struct hda_gen_spec *spec = codec->spec;
2420 int idx = 0;
2421
2422 snd_hda_get_pin_label(codec, pin, &spec->autocfg, name, name_len, &idx);
2423 strlcat(name, " Jack Mode", name_len);
2424
2425 for (; find_kctl_name(codec, name, idx); idx++)
2426 ;
2427}
2428
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002429static int get_out_jack_num_items(struct hda_codec *codec, hda_nid_t pin)
2430{
2431 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaif811c3c2013-03-07 18:32:59 +01002432 if (spec->add_jack_modes) {
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002433 unsigned int pincap = snd_hda_query_pin_caps(codec, pin);
2434 if ((pincap & AC_PINCAP_OUT) && (pincap & AC_PINCAP_HP_DRV))
2435 return 2;
2436 }
2437 return 1;
2438}
2439
Takashi Iwai978e77e2013-01-10 16:57:58 +01002440static int create_out_jack_modes(struct hda_codec *codec, int num_pins,
2441 hda_nid_t *pins)
2442{
2443 struct hda_gen_spec *spec = codec->spec;
2444 int i;
2445
2446 for (i = 0; i < num_pins; i++) {
2447 hda_nid_t pin = pins[i];
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002448 if (pin == spec->hp_mic_pin) {
2449 int ret = create_hp_mic_jack_mode(codec, pin);
2450 if (ret < 0)
2451 return ret;
2452 continue;
2453 }
2454 if (get_out_jack_num_items(codec, pin) > 1) {
Takashi Iwai978e77e2013-01-10 16:57:58 +01002455 struct snd_kcontrol_new *knew;
2456 char name[44];
2457 get_jack_mode_name(codec, pin, name, sizeof(name));
2458 knew = snd_hda_gen_add_kctl(spec, name,
2459 &out_jack_mode_enum);
2460 if (!knew)
2461 return -ENOMEM;
2462 knew->private_value = pin;
2463 }
2464 }
2465
2466 return 0;
2467}
2468
Takashi Iwai294765582013-01-17 09:52:11 +01002469/*
2470 * input jack mode
2471 */
2472
2473/* from AC_PINCTL_VREF_HIZ to AC_PINCTL_VREF_100 */
2474#define NUM_VREFS 6
2475
2476static const char * const vref_texts[NUM_VREFS] = {
2477 "Line In", "Mic 50pc Bias", "Mic 0V Bias",
2478 "", "Mic 80pc Bias", "Mic 100pc Bias"
2479};
2480
2481static unsigned int get_vref_caps(struct hda_codec *codec, hda_nid_t pin)
2482{
2483 unsigned int pincap;
2484
2485 pincap = snd_hda_query_pin_caps(codec, pin);
2486 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
2487 /* filter out unusual vrefs */
2488 pincap &= ~(AC_PINCAP_VREF_GRD | AC_PINCAP_VREF_100);
2489 return pincap;
2490}
2491
2492/* convert from the enum item index to the vref ctl index (0=HIZ, 1=50%...) */
2493static int get_vref_idx(unsigned int vref_caps, unsigned int item_idx)
2494{
2495 unsigned int i, n = 0;
2496
2497 for (i = 0; i < NUM_VREFS; i++) {
2498 if (vref_caps & (1 << i)) {
2499 if (n == item_idx)
2500 return i;
2501 n++;
2502 }
2503 }
2504 return 0;
2505}
2506
2507/* convert back from the vref ctl index to the enum item index */
2508static int cvt_from_vref_idx(unsigned int vref_caps, unsigned int idx)
2509{
2510 unsigned int i, n = 0;
2511
2512 for (i = 0; i < NUM_VREFS; i++) {
2513 if (i == idx)
2514 return n;
2515 if (vref_caps & (1 << i))
2516 n++;
2517 }
2518 return 0;
2519}
2520
2521static int in_jack_mode_info(struct snd_kcontrol *kcontrol,
2522 struct snd_ctl_elem_info *uinfo)
2523{
2524 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2525 hda_nid_t nid = kcontrol->private_value;
2526 unsigned int vref_caps = get_vref_caps(codec, nid);
2527
2528 snd_hda_enum_helper_info(kcontrol, uinfo, hweight32(vref_caps),
2529 vref_texts);
2530 /* set the right text */
2531 strcpy(uinfo->value.enumerated.name,
2532 vref_texts[get_vref_idx(vref_caps, uinfo->value.enumerated.item)]);
2533 return 0;
2534}
2535
2536static int in_jack_mode_get(struct snd_kcontrol *kcontrol,
2537 struct snd_ctl_elem_value *ucontrol)
2538{
2539 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2540 hda_nid_t nid = kcontrol->private_value;
2541 unsigned int vref_caps = get_vref_caps(codec, nid);
2542 unsigned int idx;
2543
2544 idx = snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_VREFEN;
2545 ucontrol->value.enumerated.item[0] = cvt_from_vref_idx(vref_caps, idx);
2546 return 0;
2547}
2548
2549static int in_jack_mode_put(struct snd_kcontrol *kcontrol,
2550 struct snd_ctl_elem_value *ucontrol)
2551{
2552 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2553 hda_nid_t nid = kcontrol->private_value;
2554 unsigned int vref_caps = get_vref_caps(codec, nid);
2555 unsigned int val, idx;
2556
2557 val = snd_hda_codec_get_pin_target(codec, nid);
2558 idx = cvt_from_vref_idx(vref_caps, val & AC_PINCTL_VREFEN);
2559 if (idx == ucontrol->value.enumerated.item[0])
2560 return 0;
2561
2562 val &= ~AC_PINCTL_VREFEN;
2563 val |= get_vref_idx(vref_caps, ucontrol->value.enumerated.item[0]);
2564 snd_hda_set_pin_ctl_cache(codec, nid, val);
2565 return 1;
2566}
2567
2568static const struct snd_kcontrol_new in_jack_mode_enum = {
2569 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2570 .info = in_jack_mode_info,
2571 .get = in_jack_mode_get,
2572 .put = in_jack_mode_put,
2573};
2574
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002575static int get_in_jack_num_items(struct hda_codec *codec, hda_nid_t pin)
2576{
2577 struct hda_gen_spec *spec = codec->spec;
2578 int nitems = 0;
Takashi Iwaif811c3c2013-03-07 18:32:59 +01002579 if (spec->add_jack_modes)
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002580 nitems = hweight32(get_vref_caps(codec, pin));
2581 return nitems ? nitems : 1;
2582}
2583
Takashi Iwai294765582013-01-17 09:52:11 +01002584static int create_in_jack_mode(struct hda_codec *codec, hda_nid_t pin)
2585{
2586 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai294765582013-01-17 09:52:11 +01002587 struct snd_kcontrol_new *knew;
2588 char name[44];
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002589 unsigned int defcfg;
2590
Takashi Iwaif811c3c2013-03-07 18:32:59 +01002591 if (pin == spec->hp_mic_pin)
2592 return 0; /* already done in create_out_jack_mode() */
Takashi Iwai294765582013-01-17 09:52:11 +01002593
2594 /* no jack mode for fixed pins */
2595 defcfg = snd_hda_codec_get_pincfg(codec, pin);
2596 if (snd_hda_get_input_pin_attr(defcfg) == INPUT_PIN_ATTR_INT)
2597 return 0;
2598
2599 /* no multiple vref caps? */
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002600 if (get_in_jack_num_items(codec, pin) <= 1)
Takashi Iwai294765582013-01-17 09:52:11 +01002601 return 0;
2602
2603 get_jack_mode_name(codec, pin, name, sizeof(name));
2604 knew = snd_hda_gen_add_kctl(spec, name, &in_jack_mode_enum);
2605 if (!knew)
2606 return -ENOMEM;
2607 knew->private_value = pin;
2608 return 0;
2609}
2610
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002611/*
2612 * HP/mic shared jack mode
2613 */
2614static int hp_mic_jack_mode_info(struct snd_kcontrol *kcontrol,
2615 struct snd_ctl_elem_info *uinfo)
2616{
2617 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2618 hda_nid_t nid = kcontrol->private_value;
2619 int out_jacks = get_out_jack_num_items(codec, nid);
2620 int in_jacks = get_in_jack_num_items(codec, nid);
2621 const char *text = NULL;
2622 int idx;
2623
2624 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2625 uinfo->count = 1;
2626 uinfo->value.enumerated.items = out_jacks + in_jacks;
2627 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2628 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
2629 idx = uinfo->value.enumerated.item;
2630 if (idx < out_jacks) {
2631 if (out_jacks > 1)
2632 text = out_jack_texts[idx];
2633 else
2634 text = "Headphone Out";
2635 } else {
2636 idx -= out_jacks;
2637 if (in_jacks > 1) {
2638 unsigned int vref_caps = get_vref_caps(codec, nid);
2639 text = vref_texts[get_vref_idx(vref_caps, idx)];
2640 } else
2641 text = "Mic In";
2642 }
2643
2644 strcpy(uinfo->value.enumerated.name, text);
2645 return 0;
2646}
2647
2648static int get_cur_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t nid)
2649{
2650 int out_jacks = get_out_jack_num_items(codec, nid);
2651 int in_jacks = get_in_jack_num_items(codec, nid);
2652 unsigned int val = snd_hda_codec_get_pin_target(codec, nid);
2653 int idx = 0;
2654
2655 if (val & PIN_OUT) {
2656 if (out_jacks > 1 && val == PIN_HP)
2657 idx = 1;
2658 } else if (val & PIN_IN) {
2659 idx = out_jacks;
2660 if (in_jacks > 1) {
2661 unsigned int vref_caps = get_vref_caps(codec, nid);
2662 val &= AC_PINCTL_VREFEN;
2663 idx += cvt_from_vref_idx(vref_caps, val);
2664 }
2665 }
2666 return idx;
2667}
2668
2669static int hp_mic_jack_mode_get(struct snd_kcontrol *kcontrol,
2670 struct snd_ctl_elem_value *ucontrol)
2671{
2672 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2673 hda_nid_t nid = kcontrol->private_value;
2674 ucontrol->value.enumerated.item[0] =
2675 get_cur_hp_mic_jack_mode(codec, nid);
2676 return 0;
2677}
2678
2679static int hp_mic_jack_mode_put(struct snd_kcontrol *kcontrol,
2680 struct snd_ctl_elem_value *ucontrol)
2681{
2682 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2683 hda_nid_t nid = kcontrol->private_value;
2684 int out_jacks = get_out_jack_num_items(codec, nid);
2685 int in_jacks = get_in_jack_num_items(codec, nid);
2686 unsigned int val, oldval, idx;
2687
2688 oldval = get_cur_hp_mic_jack_mode(codec, nid);
2689 idx = ucontrol->value.enumerated.item[0];
2690 if (oldval == idx)
2691 return 0;
2692
2693 if (idx < out_jacks) {
2694 if (out_jacks > 1)
2695 val = idx ? PIN_HP : PIN_OUT;
2696 else
2697 val = PIN_HP;
2698 } else {
2699 idx -= out_jacks;
2700 if (in_jacks > 1) {
2701 unsigned int vref_caps = get_vref_caps(codec, nid);
2702 val = snd_hda_codec_get_pin_target(codec, nid);
Takashi Iwai3f550e32013-03-07 18:30:27 +01002703 val &= ~(AC_PINCTL_VREFEN | PIN_HP);
2704 val |= get_vref_idx(vref_caps, idx) | PIN_IN;
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002705 } else
2706 val = snd_hda_get_default_vref(codec, nid);
2707 }
2708 snd_hda_set_pin_ctl_cache(codec, nid, val);
Takashi Iwai963afde2013-05-31 15:20:31 +02002709 call_hp_automute(codec, NULL);
Takashi Iwai8ba955c2013-03-07 18:40:58 +01002710
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002711 return 1;
2712}
2713
2714static const struct snd_kcontrol_new hp_mic_jack_mode_enum = {
2715 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2716 .info = hp_mic_jack_mode_info,
2717 .get = hp_mic_jack_mode_get,
2718 .put = hp_mic_jack_mode_put,
2719};
2720
2721static int create_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t pin)
2722{
2723 struct hda_gen_spec *spec = codec->spec;
2724 struct snd_kcontrol_new *knew;
2725
2726 if (get_out_jack_num_items(codec, pin) <= 1 &&
2727 get_in_jack_num_items(codec, pin) <= 1)
2728 return 0; /* no need */
2729 knew = snd_hda_gen_add_kctl(spec, "Headphone Mic Jack Mode",
2730 &hp_mic_jack_mode_enum);
2731 if (!knew)
2732 return -ENOMEM;
2733 knew->private_value = pin;
Takashi Iwai8ba955c2013-03-07 18:40:58 +01002734 spec->hp_mic_jack_modes = 1;
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002735 return 0;
2736}
Takashi Iwai352f7f92012-12-19 12:52:06 +01002737
2738/*
2739 * Parse input paths
2740 */
2741
Takashi Iwai352f7f92012-12-19 12:52:06 +01002742/* add the powersave loopback-list entry */
Takashi Iwai0186f4f2013-02-07 10:45:11 +01002743static int add_loopback_list(struct hda_gen_spec *spec, hda_nid_t mix, int idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002744{
2745 struct hda_amp_list *list;
2746
Takashi Iwai0186f4f2013-02-07 10:45:11 +01002747 list = snd_array_new(&spec->loopback_list);
2748 if (!list)
2749 return -ENOMEM;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002750 list->nid = mix;
2751 list->dir = HDA_INPUT;
2752 list->idx = idx;
Takashi Iwai0186f4f2013-02-07 10:45:11 +01002753 spec->loopback.amplist = spec->loopback_list.list;
2754 return 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02002755}
Takashi Iwaicb53c622007-08-10 17:21:45 +02002756
Takashi Iwai352f7f92012-12-19 12:52:06 +01002757/* create input playback/capture controls for the given pin */
Takashi Iwai196c17662013-01-04 15:01:40 +01002758static int new_analog_input(struct hda_codec *codec, int input_idx,
2759 hda_nid_t pin, const char *ctlname, int ctlidx,
Takashi Iwai352f7f92012-12-19 12:52:06 +01002760 hda_nid_t mix_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002762 struct hda_gen_spec *spec = codec->spec;
2763 struct nid_path *path;
2764 unsigned int val;
2765 int err, idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766
Takashi Iwai352f7f92012-12-19 12:52:06 +01002767 if (!nid_has_volume(codec, mix_nid, HDA_INPUT) &&
2768 !nid_has_mute(codec, mix_nid, HDA_INPUT))
2769 return 0; /* no need for analog loopback */
2770
Takashi Iwai3ca529d2013-01-07 17:25:08 +01002771 path = snd_hda_add_new_path(codec, pin, mix_nid, 0);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002772 if (!path)
2773 return -EINVAL;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01002774 print_nid_path("loopback", path);
Takashi Iwai196c17662013-01-04 15:01:40 +01002775 spec->loopback_paths[input_idx] = snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002776
2777 idx = path->idx[path->depth - 1];
2778 if (nid_has_volume(codec, mix_nid, HDA_INPUT)) {
2779 val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
2780 err = __add_pb_vol_ctrl(spec, HDA_CTL_WIDGET_VOL, ctlname, ctlidx, val);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002781 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002783 path->ctls[NID_PATH_VOL_CTL] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784 }
2785
Takashi Iwai352f7f92012-12-19 12:52:06 +01002786 if (nid_has_mute(codec, mix_nid, HDA_INPUT)) {
2787 val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
2788 err = __add_pb_sw_ctrl(spec, HDA_CTL_WIDGET_MUTE, ctlname, ctlidx, val);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002789 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002791 path->ctls[NID_PATH_MUTE_CTL] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792 }
2793
Takashi Iwai352f7f92012-12-19 12:52:06 +01002794 path->active = true;
Takashi Iwai0186f4f2013-02-07 10:45:11 +01002795 err = add_loopback_list(spec, mix_nid, idx);
2796 if (err < 0)
2797 return err;
Takashi Iwaie4a395e2013-01-23 17:00:31 +01002798
2799 if (spec->mixer_nid != spec->mixer_merge_nid &&
2800 !spec->loopback_merge_path) {
2801 path = snd_hda_add_new_path(codec, spec->mixer_nid,
2802 spec->mixer_merge_nid, 0);
2803 if (path) {
2804 print_nid_path("loopback-merge", path);
2805 path->active = true;
2806 spec->loopback_merge_path =
2807 snd_hda_get_path_idx(codec, path);
2808 }
2809 }
2810
Takashi Iwai352f7f92012-12-19 12:52:06 +01002811 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812}
2813
Takashi Iwai352f7f92012-12-19 12:52:06 +01002814static int is_input_pin(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002816 unsigned int pincap = snd_hda_query_pin_caps(codec, nid);
2817 return (pincap & AC_PINCAP_IN) != 0;
2818}
2819
2820/* Parse the codec tree and retrieve ADCs */
2821static int fill_adc_nids(struct hda_codec *codec)
2822{
2823 struct hda_gen_spec *spec = codec->spec;
2824 hda_nid_t nid;
2825 hda_nid_t *adc_nids = spec->adc_nids;
2826 int max_nums = ARRAY_SIZE(spec->adc_nids);
2827 int i, nums = 0;
2828
2829 nid = codec->start_nid;
2830 for (i = 0; i < codec->num_nodes; i++, nid++) {
2831 unsigned int caps = get_wcaps(codec, nid);
2832 int type = get_wcaps_type(caps);
2833
2834 if (type != AC_WID_AUD_IN || (caps & AC_WCAP_DIGITAL))
2835 continue;
2836 adc_nids[nums] = nid;
2837 if (++nums >= max_nums)
2838 break;
2839 }
2840 spec->num_adc_nids = nums;
Takashi Iwai0ffd5342013-01-17 15:53:29 +01002841
2842 /* copy the detected ADCs to all_adcs[] */
2843 spec->num_all_adcs = nums;
2844 memcpy(spec->all_adcs, spec->adc_nids, nums * sizeof(hda_nid_t));
2845
Takashi Iwai352f7f92012-12-19 12:52:06 +01002846 return nums;
2847}
2848
2849/* filter out invalid adc_nids that don't give all active input pins;
2850 * if needed, check whether dynamic ADC-switching is available
2851 */
2852static int check_dyn_adc_switch(struct hda_codec *codec)
2853{
2854 struct hda_gen_spec *spec = codec->spec;
2855 struct hda_input_mux *imux = &spec->input_mux;
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002856 unsigned int ok_bits;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002857 int i, n, nums;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002858
Takashi Iwai352f7f92012-12-19 12:52:06 +01002859 nums = 0;
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002860 ok_bits = 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002861 for (n = 0; n < spec->num_adc_nids; n++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002862 for (i = 0; i < imux->num_items; i++) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002863 if (!spec->input_paths[i][n])
Takashi Iwai352f7f92012-12-19 12:52:06 +01002864 break;
2865 }
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002866 if (i >= imux->num_items) {
2867 ok_bits |= (1 << n);
2868 nums++;
2869 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002870 }
2871
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002872 if (!ok_bits) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002873 /* check whether ADC-switch is possible */
2874 for (i = 0; i < imux->num_items; i++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002875 for (n = 0; n < spec->num_adc_nids; n++) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002876 if (spec->input_paths[i][n]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002877 spec->dyn_adc_idx[i] = n;
2878 break;
2879 }
2880 }
2881 }
2882
2883 snd_printdd("hda-codec: enabling ADC switching\n");
2884 spec->dyn_adc_switch = 1;
2885 } else if (nums != spec->num_adc_nids) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002886 /* shrink the invalid adcs and input paths */
2887 nums = 0;
2888 for (n = 0; n < spec->num_adc_nids; n++) {
2889 if (!(ok_bits & (1 << n)))
2890 continue;
2891 if (n != nums) {
2892 spec->adc_nids[nums] = spec->adc_nids[n];
Takashi Iwai980428c2013-01-09 09:28:20 +01002893 for (i = 0; i < imux->num_items; i++) {
2894 invalidate_nid_path(codec,
2895 spec->input_paths[i][nums]);
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002896 spec->input_paths[i][nums] =
2897 spec->input_paths[i][n];
Takashi Iwai980428c2013-01-09 09:28:20 +01002898 }
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002899 }
2900 nums++;
2901 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002902 spec->num_adc_nids = nums;
2903 }
2904
Takashi Iwai967303d2013-02-19 17:12:42 +01002905 if (imux->num_items == 1 ||
2906 (imux->num_items == 2 && spec->hp_mic)) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002907 snd_printdd("hda-codec: reducing to a single ADC\n");
2908 spec->num_adc_nids = 1; /* reduce to a single ADC */
2909 }
2910
2911 /* single index for individual volumes ctls */
2912 if (!spec->dyn_adc_switch && spec->multi_cap_vol)
2913 spec->num_adc_nids = 1;
2914
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915 return 0;
2916}
2917
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002918/* parse capture source paths from the given pin and create imux items */
2919static int parse_capture_source(struct hda_codec *codec, hda_nid_t pin,
Takashi Iwai9dba2052013-01-18 10:01:15 +01002920 int cfg_idx, int num_adcs,
2921 const char *label, int anchor)
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002922{
2923 struct hda_gen_spec *spec = codec->spec;
2924 struct hda_input_mux *imux = &spec->input_mux;
2925 int imux_idx = imux->num_items;
2926 bool imux_added = false;
2927 int c;
2928
2929 for (c = 0; c < num_adcs; c++) {
2930 struct nid_path *path;
2931 hda_nid_t adc = spec->adc_nids[c];
2932
2933 if (!is_reachable_path(codec, pin, adc))
2934 continue;
2935 path = snd_hda_add_new_path(codec, pin, adc, anchor);
2936 if (!path)
2937 continue;
2938 print_nid_path("input", path);
2939 spec->input_paths[imux_idx][c] =
2940 snd_hda_get_path_idx(codec, path);
2941
2942 if (!imux_added) {
Takashi Iwai967303d2013-02-19 17:12:42 +01002943 if (spec->hp_mic_pin == pin)
2944 spec->hp_mic_mux_idx = imux->num_items;
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002945 spec->imux_pins[imux->num_items] = pin;
Takashi Iwai9dba2052013-01-18 10:01:15 +01002946 snd_hda_add_imux_item(imux, label, cfg_idx, NULL);
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002947 imux_added = true;
2948 }
2949 }
2950
2951 return 0;
2952}
2953
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01002955 * create playback/capture controls for input pins
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956 */
Takashi Iwai9dba2052013-01-18 10:01:15 +01002957
Takashi Iwaic9700422013-01-18 10:17:30 +01002958/* fill the label for each input at first */
2959static int fill_input_pin_labels(struct hda_codec *codec)
2960{
2961 struct hda_gen_spec *spec = codec->spec;
2962 const struct auto_pin_cfg *cfg = &spec->autocfg;
2963 int i;
2964
2965 for (i = 0; i < cfg->num_inputs; i++) {
2966 hda_nid_t pin = cfg->inputs[i].pin;
2967 const char *label;
2968 int j, idx;
2969
2970 if (!is_input_pin(codec, pin))
2971 continue;
2972
2973 label = hda_get_autocfg_input_label(codec, cfg, i);
2974 idx = 0;
David Henningsson8e8db7f2013-01-18 15:43:02 +01002975 for (j = i - 1; j >= 0; j--) {
Takashi Iwaic9700422013-01-18 10:17:30 +01002976 if (spec->input_labels[j] &&
2977 !strcmp(spec->input_labels[j], label)) {
2978 idx = spec->input_label_idxs[j] + 1;
2979 break;
2980 }
2981 }
2982
2983 spec->input_labels[i] = label;
2984 spec->input_label_idxs[i] = idx;
2985 }
2986
2987 return 0;
2988}
2989
Takashi Iwai9dba2052013-01-18 10:01:15 +01002990#define CFG_IDX_MIX 99 /* a dummy cfg->input idx for stereo mix */
2991
Takashi Iwai352f7f92012-12-19 12:52:06 +01002992static int create_input_ctls(struct hda_codec *codec)
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02002993{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002994 struct hda_gen_spec *spec = codec->spec;
2995 const struct auto_pin_cfg *cfg = &spec->autocfg;
2996 hda_nid_t mixer = spec->mixer_nid;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002997 int num_adcs;
Takashi Iwaic9700422013-01-18 10:17:30 +01002998 int i, err;
Takashi Iwai2c12c302013-01-10 09:33:29 +01002999 unsigned int val;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02003000
Takashi Iwai352f7f92012-12-19 12:52:06 +01003001 num_adcs = fill_adc_nids(codec);
3002 if (num_adcs < 0)
3003 return 0;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02003004
Takashi Iwaic9700422013-01-18 10:17:30 +01003005 err = fill_input_pin_labels(codec);
3006 if (err < 0)
3007 return err;
3008
Takashi Iwai352f7f92012-12-19 12:52:06 +01003009 for (i = 0; i < cfg->num_inputs; i++) {
3010 hda_nid_t pin;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011
Takashi Iwai352f7f92012-12-19 12:52:06 +01003012 pin = cfg->inputs[i].pin;
3013 if (!is_input_pin(codec, pin))
3014 continue;
3015
Takashi Iwai2c12c302013-01-10 09:33:29 +01003016 val = PIN_IN;
3017 if (cfg->inputs[i].type == AUTO_PIN_MIC)
3018 val |= snd_hda_get_default_vref(codec, pin);
Takashi Iwai93c9d8a2013-03-11 09:48:43 +01003019 if (pin != spec->hp_mic_pin)
3020 set_pin_target(codec, pin, val, false);
Takashi Iwai2c12c302013-01-10 09:33:29 +01003021
Takashi Iwai352f7f92012-12-19 12:52:06 +01003022 if (mixer) {
3023 if (is_reachable_path(codec, pin, mixer)) {
Takashi Iwai196c17662013-01-04 15:01:40 +01003024 err = new_analog_input(codec, i, pin,
Takashi Iwaic9700422013-01-18 10:17:30 +01003025 spec->input_labels[i],
3026 spec->input_label_idxs[i],
3027 mixer);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003028 if (err < 0)
3029 return err;
3030 }
3031 }
3032
Takashi Iwaic9700422013-01-18 10:17:30 +01003033 err = parse_capture_source(codec, pin, i, num_adcs,
3034 spec->input_labels[i], -mixer);
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003035 if (err < 0)
3036 return err;
Takashi Iwai294765582013-01-17 09:52:11 +01003037
Takashi Iwaif811c3c2013-03-07 18:32:59 +01003038 if (spec->add_jack_modes) {
Takashi Iwai294765582013-01-17 09:52:11 +01003039 err = create_in_jack_mode(codec, pin);
3040 if (err < 0)
3041 return err;
3042 }
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003043 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003044
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003045 if (mixer && spec->add_stereo_mix_input) {
Takashi Iwai9dba2052013-01-18 10:01:15 +01003046 err = parse_capture_source(codec, mixer, CFG_IDX_MIX, num_adcs,
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003047 "Stereo Mix", 0);
3048 if (err < 0)
3049 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003050 }
3051
3052 return 0;
3053}
3054
3055
3056/*
3057 * input source mux
3058 */
3059
Takashi Iwaic697b712013-01-07 17:09:26 +01003060/* get the input path specified by the given adc and imux indices */
3061static struct nid_path *get_input_path(struct hda_codec *codec, int adc_idx, int imux_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003062{
3063 struct hda_gen_spec *spec = codec->spec;
David Henningssonb56fa1e2013-01-16 11:45:35 +01003064 if (imux_idx < 0 || imux_idx >= HDA_MAX_NUM_INPUTS) {
3065 snd_BUG();
3066 return NULL;
3067 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003068 if (spec->dyn_adc_switch)
3069 adc_idx = spec->dyn_adc_idx[imux_idx];
David Henningssond3d982f2013-01-18 15:43:01 +01003070 if (adc_idx < 0 || adc_idx >= AUTO_CFG_MAX_INS) {
David Henningssonb56fa1e2013-01-16 11:45:35 +01003071 snd_BUG();
3072 return NULL;
3073 }
Takashi Iwaic697b712013-01-07 17:09:26 +01003074 return snd_hda_get_path_from_idx(codec, spec->input_paths[imux_idx][adc_idx]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003075}
3076
3077static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
3078 unsigned int idx);
3079
3080static int mux_enum_info(struct snd_kcontrol *kcontrol,
3081 struct snd_ctl_elem_info *uinfo)
3082{
3083 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3084 struct hda_gen_spec *spec = codec->spec;
3085 return snd_hda_input_mux_info(&spec->input_mux, uinfo);
3086}
3087
3088static int mux_enum_get(struct snd_kcontrol *kcontrol,
3089 struct snd_ctl_elem_value *ucontrol)
3090{
3091 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3092 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai2a8d5392013-01-18 16:23:25 +01003093 /* the ctls are created at once with multiple counts */
3094 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003095
3096 ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
3097 return 0;
3098}
3099
3100static int mux_enum_put(struct snd_kcontrol *kcontrol,
3101 struct snd_ctl_elem_value *ucontrol)
3102{
3103 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Takashi Iwai2a8d5392013-01-18 16:23:25 +01003104 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003105 return mux_select(codec, adc_idx,
3106 ucontrol->value.enumerated.item[0]);
3107}
3108
Takashi Iwai352f7f92012-12-19 12:52:06 +01003109static const struct snd_kcontrol_new cap_src_temp = {
3110 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3111 .name = "Input Source",
3112 .info = mux_enum_info,
3113 .get = mux_enum_get,
3114 .put = mux_enum_put,
3115};
3116
Takashi Iwai47d46ab2012-12-20 11:48:54 +01003117/*
3118 * capture volume and capture switch ctls
3119 */
3120
Takashi Iwai352f7f92012-12-19 12:52:06 +01003121typedef int (*put_call_t)(struct snd_kcontrol *kcontrol,
3122 struct snd_ctl_elem_value *ucontrol);
3123
Takashi Iwai47d46ab2012-12-20 11:48:54 +01003124/* call the given amp update function for all amps in the imux list at once */
Takashi Iwai352f7f92012-12-19 12:52:06 +01003125static int cap_put_caller(struct snd_kcontrol *kcontrol,
3126 struct snd_ctl_elem_value *ucontrol,
3127 put_call_t func, int type)
3128{
3129 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3130 struct hda_gen_spec *spec = codec->spec;
3131 const struct hda_input_mux *imux;
3132 struct nid_path *path;
3133 int i, adc_idx, err = 0;
3134
3135 imux = &spec->input_mux;
David Henningssona053d1e2013-01-16 11:45:36 +01003136 adc_idx = kcontrol->id.index;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003137 mutex_lock(&codec->control_mutex);
Takashi Iwai47d46ab2012-12-20 11:48:54 +01003138 /* we use the cache-only update at first since multiple input paths
3139 * may shared the same amp; by updating only caches, the redundant
3140 * writes to hardware can be reduced.
3141 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01003142 codec->cached_write = 1;
3143 for (i = 0; i < imux->num_items; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01003144 path = get_input_path(codec, adc_idx, i);
3145 if (!path || !path->ctls[type])
Takashi Iwai352f7f92012-12-19 12:52:06 +01003146 continue;
3147 kcontrol->private_value = path->ctls[type];
3148 err = func(kcontrol, ucontrol);
3149 if (err < 0)
3150 goto error;
3151 }
3152 error:
3153 codec->cached_write = 0;
3154 mutex_unlock(&codec->control_mutex);
Takashi Iwaidc870f32013-01-22 15:24:30 +01003155 snd_hda_codec_flush_cache(codec); /* flush the updates */
Takashi Iwai352f7f92012-12-19 12:52:06 +01003156 if (err >= 0 && spec->cap_sync_hook)
Takashi Iwaia90229e2013-01-18 14:10:00 +01003157 spec->cap_sync_hook(codec, ucontrol);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003158 return err;
3159}
3160
3161/* capture volume ctl callbacks */
3162#define cap_vol_info snd_hda_mixer_amp_volume_info
3163#define cap_vol_get snd_hda_mixer_amp_volume_get
3164#define cap_vol_tlv snd_hda_mixer_amp_tlv
3165
3166static int cap_vol_put(struct snd_kcontrol *kcontrol,
3167 struct snd_ctl_elem_value *ucontrol)
3168{
3169 return cap_put_caller(kcontrol, ucontrol,
3170 snd_hda_mixer_amp_volume_put,
3171 NID_PATH_VOL_CTL);
3172}
3173
3174static const struct snd_kcontrol_new cap_vol_temp = {
3175 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3176 .name = "Capture Volume",
3177 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
3178 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
3179 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK),
3180 .info = cap_vol_info,
3181 .get = cap_vol_get,
3182 .put = cap_vol_put,
3183 .tlv = { .c = cap_vol_tlv },
3184};
3185
3186/* capture switch ctl callbacks */
3187#define cap_sw_info snd_ctl_boolean_stereo_info
3188#define cap_sw_get snd_hda_mixer_amp_switch_get
3189
3190static int cap_sw_put(struct snd_kcontrol *kcontrol,
3191 struct snd_ctl_elem_value *ucontrol)
3192{
Takashi Iwaia90229e2013-01-18 14:10:00 +01003193 return cap_put_caller(kcontrol, ucontrol,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003194 snd_hda_mixer_amp_switch_put,
3195 NID_PATH_MUTE_CTL);
3196}
3197
3198static const struct snd_kcontrol_new cap_sw_temp = {
3199 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3200 .name = "Capture Switch",
3201 .info = cap_sw_info,
3202 .get = cap_sw_get,
3203 .put = cap_sw_put,
3204};
3205
3206static int parse_capvol_in_path(struct hda_codec *codec, struct nid_path *path)
3207{
3208 hda_nid_t nid;
3209 int i, depth;
3210
3211 path->ctls[NID_PATH_VOL_CTL] = path->ctls[NID_PATH_MUTE_CTL] = 0;
3212 for (depth = 0; depth < 3; depth++) {
3213 if (depth >= path->depth)
3214 return -EINVAL;
3215 i = path->depth - depth - 1;
3216 nid = path->path[i];
3217 if (!path->ctls[NID_PATH_VOL_CTL]) {
3218 if (nid_has_volume(codec, nid, HDA_OUTPUT))
3219 path->ctls[NID_PATH_VOL_CTL] =
3220 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3221 else if (nid_has_volume(codec, nid, HDA_INPUT)) {
3222 int idx = path->idx[i];
3223 if (!depth && codec->single_adc_amp)
3224 idx = 0;
3225 path->ctls[NID_PATH_VOL_CTL] =
3226 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
3227 }
3228 }
3229 if (!path->ctls[NID_PATH_MUTE_CTL]) {
3230 if (nid_has_mute(codec, nid, HDA_OUTPUT))
3231 path->ctls[NID_PATH_MUTE_CTL] =
3232 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3233 else if (nid_has_mute(codec, nid, HDA_INPUT)) {
3234 int idx = path->idx[i];
3235 if (!depth && codec->single_adc_amp)
3236 idx = 0;
3237 path->ctls[NID_PATH_MUTE_CTL] =
3238 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
3239 }
3240 }
Takashi Iwai97ec5582006-03-21 11:29:07 +01003241 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242 return 0;
3243}
3244
Takashi Iwai352f7f92012-12-19 12:52:06 +01003245static bool is_inv_dmic_pin(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003247 struct hda_gen_spec *spec = codec->spec;
3248 struct auto_pin_cfg *cfg = &spec->autocfg;
3249 unsigned int val;
3250 int i;
3251
3252 if (!spec->inv_dmic_split)
3253 return false;
3254 for (i = 0; i < cfg->num_inputs; i++) {
3255 if (cfg->inputs[i].pin != nid)
3256 continue;
3257 if (cfg->inputs[i].type != AUTO_PIN_MIC)
3258 return false;
3259 val = snd_hda_codec_get_pincfg(codec, nid);
3260 return snd_hda_get_input_pin_attr(val) == INPUT_PIN_ATTR_INT;
3261 }
3262 return false;
3263}
3264
Takashi Iwaia90229e2013-01-18 14:10:00 +01003265/* capture switch put callback for a single control with hook call */
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003266static int cap_single_sw_put(struct snd_kcontrol *kcontrol,
3267 struct snd_ctl_elem_value *ucontrol)
3268{
3269 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3270 struct hda_gen_spec *spec = codec->spec;
3271 int ret;
3272
3273 ret = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
3274 if (ret < 0)
3275 return ret;
3276
Takashi Iwaia90229e2013-01-18 14:10:00 +01003277 if (spec->cap_sync_hook)
3278 spec->cap_sync_hook(codec, ucontrol);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003279
3280 return ret;
3281}
3282
Takashi Iwai352f7f92012-12-19 12:52:06 +01003283static int add_single_cap_ctl(struct hda_codec *codec, const char *label,
3284 int idx, bool is_switch, unsigned int ctl,
3285 bool inv_dmic)
3286{
3287 struct hda_gen_spec *spec = codec->spec;
3288 char tmpname[44];
3289 int type = is_switch ? HDA_CTL_WIDGET_MUTE : HDA_CTL_WIDGET_VOL;
3290 const char *sfx = is_switch ? "Switch" : "Volume";
3291 unsigned int chs = inv_dmic ? 1 : 3;
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003292 struct snd_kcontrol_new *knew;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003293
3294 if (!ctl)
3295 return 0;
3296
3297 if (label)
3298 snprintf(tmpname, sizeof(tmpname),
3299 "%s Capture %s", label, sfx);
3300 else
3301 snprintf(tmpname, sizeof(tmpname),
3302 "Capture %s", sfx);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003303 knew = add_control(spec, type, tmpname, idx,
3304 amp_val_replace_channels(ctl, chs));
3305 if (!knew)
3306 return -ENOMEM;
Takashi Iwaia90229e2013-01-18 14:10:00 +01003307 if (is_switch)
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003308 knew->put = cap_single_sw_put;
3309 if (!inv_dmic)
3310 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003311
3312 /* Make independent right kcontrol */
3313 if (label)
3314 snprintf(tmpname, sizeof(tmpname),
3315 "Inverted %s Capture %s", label, sfx);
3316 else
3317 snprintf(tmpname, sizeof(tmpname),
3318 "Inverted Capture %s", sfx);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003319 knew = add_control(spec, type, tmpname, idx,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003320 amp_val_replace_channels(ctl, 2));
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003321 if (!knew)
3322 return -ENOMEM;
Takashi Iwaia90229e2013-01-18 14:10:00 +01003323 if (is_switch)
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003324 knew->put = cap_single_sw_put;
3325 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003326}
3327
3328/* create single (and simple) capture volume and switch controls */
3329static int create_single_cap_vol_ctl(struct hda_codec *codec, int idx,
3330 unsigned int vol_ctl, unsigned int sw_ctl,
3331 bool inv_dmic)
3332{
3333 int err;
3334 err = add_single_cap_ctl(codec, NULL, idx, false, vol_ctl, inv_dmic);
3335 if (err < 0)
3336 return err;
3337 err = add_single_cap_ctl(codec, NULL, idx, true, sw_ctl, inv_dmic);
3338 if (err < 0)
3339 return err;
3340 return 0;
3341}
3342
3343/* create bound capture volume and switch controls */
3344static int create_bind_cap_vol_ctl(struct hda_codec *codec, int idx,
3345 unsigned int vol_ctl, unsigned int sw_ctl)
3346{
3347 struct hda_gen_spec *spec = codec->spec;
3348 struct snd_kcontrol_new *knew;
3349
3350 if (vol_ctl) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01003351 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_vol_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003352 if (!knew)
3353 return -ENOMEM;
3354 knew->index = idx;
3355 knew->private_value = vol_ctl;
3356 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
3357 }
3358 if (sw_ctl) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01003359 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_sw_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003360 if (!knew)
3361 return -ENOMEM;
3362 knew->index = idx;
3363 knew->private_value = sw_ctl;
3364 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
3365 }
3366 return 0;
3367}
3368
3369/* return the vol ctl when used first in the imux list */
3370static unsigned int get_first_cap_ctl(struct hda_codec *codec, int idx, int type)
3371{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003372 struct nid_path *path;
3373 unsigned int ctl;
3374 int i;
3375
Takashi Iwaic697b712013-01-07 17:09:26 +01003376 path = get_input_path(codec, 0, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003377 if (!path)
3378 return 0;
3379 ctl = path->ctls[type];
3380 if (!ctl)
3381 return 0;
3382 for (i = 0; i < idx - 1; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01003383 path = get_input_path(codec, 0, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003384 if (path && path->ctls[type] == ctl)
3385 return 0;
3386 }
3387 return ctl;
3388}
3389
3390/* create individual capture volume and switch controls per input */
3391static int create_multi_cap_vol_ctl(struct hda_codec *codec)
3392{
3393 struct hda_gen_spec *spec = codec->spec;
3394 struct hda_input_mux *imux = &spec->input_mux;
Takashi Iwaic9700422013-01-18 10:17:30 +01003395 int i, err, type;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003396
3397 for (i = 0; i < imux->num_items; i++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003398 bool inv_dmic;
Takashi Iwaic9700422013-01-18 10:17:30 +01003399 int idx;
Takashi Iwai9dba2052013-01-18 10:01:15 +01003400
Takashi Iwaic9700422013-01-18 10:17:30 +01003401 idx = imux->items[i].index;
3402 if (idx >= spec->autocfg.num_inputs)
Takashi Iwai9dba2052013-01-18 10:01:15 +01003403 continue;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003404 inv_dmic = is_inv_dmic_pin(codec, spec->imux_pins[i]);
3405
3406 for (type = 0; type < 2; type++) {
Takashi Iwaic9700422013-01-18 10:17:30 +01003407 err = add_single_cap_ctl(codec,
3408 spec->input_labels[idx],
3409 spec->input_label_idxs[idx],
3410 type,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003411 get_first_cap_ctl(codec, i, type),
3412 inv_dmic);
3413 if (err < 0)
3414 return err;
3415 }
3416 }
3417 return 0;
3418}
3419
3420static int create_capture_mixers(struct hda_codec *codec)
3421{
3422 struct hda_gen_spec *spec = codec->spec;
3423 struct hda_input_mux *imux = &spec->input_mux;
3424 int i, n, nums, err;
3425
3426 if (spec->dyn_adc_switch)
3427 nums = 1;
3428 else
3429 nums = spec->num_adc_nids;
3430
3431 if (!spec->auto_mic && imux->num_items > 1) {
3432 struct snd_kcontrol_new *knew;
Takashi Iwai624d9142012-12-19 17:41:52 +01003433 const char *name;
3434 name = nums > 1 ? "Input Source" : "Capture Source";
3435 knew = snd_hda_gen_add_kctl(spec, name, &cap_src_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003436 if (!knew)
3437 return -ENOMEM;
3438 knew->count = nums;
3439 }
3440
3441 for (n = 0; n < nums; n++) {
3442 bool multi = false;
David Henningsson99a55922013-01-16 15:58:44 +01003443 bool multi_cap_vol = spec->multi_cap_vol;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003444 bool inv_dmic = false;
3445 int vol, sw;
3446
3447 vol = sw = 0;
3448 for (i = 0; i < imux->num_items; i++) {
3449 struct nid_path *path;
Takashi Iwaic697b712013-01-07 17:09:26 +01003450 path = get_input_path(codec, n, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003451 if (!path)
3452 continue;
3453 parse_capvol_in_path(codec, path);
3454 if (!vol)
3455 vol = path->ctls[NID_PATH_VOL_CTL];
David Henningsson99a55922013-01-16 15:58:44 +01003456 else if (vol != path->ctls[NID_PATH_VOL_CTL]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003457 multi = true;
David Henningsson99a55922013-01-16 15:58:44 +01003458 if (!same_amp_caps(codec, vol,
3459 path->ctls[NID_PATH_VOL_CTL], HDA_INPUT))
3460 multi_cap_vol = true;
3461 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003462 if (!sw)
3463 sw = path->ctls[NID_PATH_MUTE_CTL];
David Henningsson99a55922013-01-16 15:58:44 +01003464 else if (sw != path->ctls[NID_PATH_MUTE_CTL]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003465 multi = true;
David Henningsson99a55922013-01-16 15:58:44 +01003466 if (!same_amp_caps(codec, sw,
3467 path->ctls[NID_PATH_MUTE_CTL], HDA_INPUT))
3468 multi_cap_vol = true;
3469 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003470 if (is_inv_dmic_pin(codec, spec->imux_pins[i]))
3471 inv_dmic = true;
3472 }
3473
3474 if (!multi)
3475 err = create_single_cap_vol_ctl(codec, n, vol, sw,
3476 inv_dmic);
David Henningsson99a55922013-01-16 15:58:44 +01003477 else if (!multi_cap_vol)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003478 err = create_bind_cap_vol_ctl(codec, n, vol, sw);
3479 else
3480 err = create_multi_cap_vol_ctl(codec);
3481 if (err < 0)
3482 return err;
3483 }
3484
3485 return 0;
3486}
3487
3488/*
3489 * add mic boosts if needed
3490 */
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003491
3492/* check whether the given amp is feasible as a boost volume */
3493static bool check_boost_vol(struct hda_codec *codec, hda_nid_t nid,
3494 int dir, int idx)
3495{
3496 unsigned int step;
3497
3498 if (!nid_has_volume(codec, nid, dir) ||
3499 is_ctl_associated(codec, nid, dir, idx, NID_PATH_VOL_CTL) ||
3500 is_ctl_associated(codec, nid, dir, idx, NID_PATH_BOOST_CTL))
3501 return false;
3502
3503 step = (query_amp_caps(codec, nid, dir) & AC_AMPCAP_STEP_SIZE)
3504 >> AC_AMPCAP_STEP_SIZE_SHIFT;
3505 if (step < 0x20)
3506 return false;
3507 return true;
3508}
3509
3510/* look for a boost amp in a widget close to the pin */
3511static unsigned int look_for_boost_amp(struct hda_codec *codec,
3512 struct nid_path *path)
3513{
3514 unsigned int val = 0;
3515 hda_nid_t nid;
3516 int depth;
3517
3518 for (depth = 0; depth < 3; depth++) {
3519 if (depth >= path->depth - 1)
3520 break;
3521 nid = path->path[depth];
3522 if (depth && check_boost_vol(codec, nid, HDA_OUTPUT, 0)) {
3523 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3524 break;
3525 } else if (check_boost_vol(codec, nid, HDA_INPUT,
3526 path->idx[depth])) {
3527 val = HDA_COMPOSE_AMP_VAL(nid, 3, path->idx[depth],
3528 HDA_INPUT);
3529 break;
3530 }
3531 }
3532
3533 return val;
3534}
3535
Takashi Iwai352f7f92012-12-19 12:52:06 +01003536static int parse_mic_boost(struct hda_codec *codec)
3537{
3538 struct hda_gen_spec *spec = codec->spec;
3539 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003540 struct hda_input_mux *imux = &spec->input_mux;
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003541 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003542
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003543 if (!spec->num_adc_nids)
3544 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003545
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003546 for (i = 0; i < imux->num_items; i++) {
3547 struct nid_path *path;
3548 unsigned int val;
3549 int idx;
3550 char boost_label[44];
David Henningsson02aba552013-01-16 15:58:43 +01003551
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003552 idx = imux->items[i].index;
3553 if (idx >= imux->num_items)
3554 continue;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003555
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003556 /* check only line-in and mic pins */
Takashi Iwai1799cdd52013-01-18 14:37:16 +01003557 if (cfg->inputs[idx].type > AUTO_PIN_LINE_IN)
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003558 continue;
3559
3560 path = get_input_path(codec, 0, i);
3561 if (!path)
3562 continue;
3563
3564 val = look_for_boost_amp(codec, path);
3565 if (!val)
3566 continue;
3567
3568 /* create a boost control */
3569 snprintf(boost_label, sizeof(boost_label),
3570 "%s Boost Volume", spec->input_labels[idx]);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003571 if (!add_control(spec, HDA_CTL_WIDGET_VOL, boost_label,
3572 spec->input_label_idxs[idx], val))
3573 return -ENOMEM;
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003574
3575 path->ctls[NID_PATH_BOOST_CTL] = val;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003576 }
3577 return 0;
3578}
3579
3580/*
3581 * parse digital I/Os and set up NIDs in BIOS auto-parse mode
3582 */
3583static void parse_digital(struct hda_codec *codec)
3584{
3585 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003586 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003587 int i, nums;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003588 hda_nid_t dig_nid, pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003589
3590 /* support multiple SPDIFs; the secondary is set up as a slave */
3591 nums = 0;
3592 for (i = 0; i < spec->autocfg.dig_outs; i++) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01003593 pin = spec->autocfg.dig_out_pins[i];
Takashi Iwai352f7f92012-12-19 12:52:06 +01003594 dig_nid = look_for_dac(codec, pin, true);
3595 if (!dig_nid)
3596 continue;
Takashi Iwai3ca529d2013-01-07 17:25:08 +01003597 path = snd_hda_add_new_path(codec, dig_nid, pin, 0);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003598 if (!path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003599 continue;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003600 print_nid_path("digout", path);
Takashi Iwaie1284af2013-01-03 16:33:02 +01003601 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +01003602 spec->digout_paths[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai2c12c302013-01-10 09:33:29 +01003603 set_pin_target(codec, pin, PIN_OUT, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003604 if (!nums) {
3605 spec->multiout.dig_out_nid = dig_nid;
3606 spec->dig_out_type = spec->autocfg.dig_out_type[0];
3607 } else {
3608 spec->multiout.slave_dig_outs = spec->slave_dig_outs;
3609 if (nums >= ARRAY_SIZE(spec->slave_dig_outs) - 1)
3610 break;
3611 spec->slave_dig_outs[nums - 1] = dig_nid;
3612 }
3613 nums++;
3614 }
3615
3616 if (spec->autocfg.dig_in_pin) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01003617 pin = spec->autocfg.dig_in_pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003618 dig_nid = codec->start_nid;
3619 for (i = 0; i < codec->num_nodes; i++, dig_nid++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003620 unsigned int wcaps = get_wcaps(codec, dig_nid);
3621 if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
3622 continue;
3623 if (!(wcaps & AC_WCAP_DIGITAL))
3624 continue;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003625 path = snd_hda_add_new_path(codec, pin, dig_nid, 0);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003626 if (path) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003627 print_nid_path("digin", path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003628 path->active = true;
3629 spec->dig_in_nid = dig_nid;
Takashi Iwai2430d7b2013-01-04 15:09:42 +01003630 spec->digin_path = snd_hda_get_path_idx(codec, path);
Takashi Iwai2c12c302013-01-10 09:33:29 +01003631 set_pin_target(codec, pin, PIN_IN, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003632 break;
3633 }
3634 }
3635 }
3636}
3637
3638
3639/*
3640 * input MUX handling
3641 */
3642
3643static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur);
3644
3645/* select the given imux item; either unmute exclusively or select the route */
3646static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
3647 unsigned int idx)
3648{
3649 struct hda_gen_spec *spec = codec->spec;
3650 const struct hda_input_mux *imux;
Takashi Iwai55196ff2013-01-24 17:32:56 +01003651 struct nid_path *old_path, *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003652
3653 imux = &spec->input_mux;
3654 if (!imux->num_items)
3655 return 0;
3656
3657 if (idx >= imux->num_items)
3658 idx = imux->num_items - 1;
3659 if (spec->cur_mux[adc_idx] == idx)
3660 return 0;
3661
Takashi Iwai55196ff2013-01-24 17:32:56 +01003662 old_path = get_input_path(codec, adc_idx, spec->cur_mux[adc_idx]);
3663 if (!old_path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003664 return 0;
Takashi Iwai55196ff2013-01-24 17:32:56 +01003665 if (old_path->active)
3666 snd_hda_activate_path(codec, old_path, false, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003667
3668 spec->cur_mux[adc_idx] = idx;
3669
Takashi Iwai967303d2013-02-19 17:12:42 +01003670 if (spec->hp_mic)
3671 update_hp_mic(codec, adc_idx, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003672
3673 if (spec->dyn_adc_switch)
3674 dyn_adc_pcm_resetup(codec, idx);
3675
Takashi Iwaic697b712013-01-07 17:09:26 +01003676 path = get_input_path(codec, adc_idx, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003677 if (!path)
3678 return 0;
3679 if (path->active)
3680 return 0;
3681 snd_hda_activate_path(codec, path, true, false);
3682 if (spec->cap_sync_hook)
Takashi Iwaia90229e2013-01-18 14:10:00 +01003683 spec->cap_sync_hook(codec, NULL);
Takashi Iwai55196ff2013-01-24 17:32:56 +01003684 path_power_down_sync(codec, old_path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003685 return 1;
3686}
3687
3688
3689/*
3690 * Jack detections for HP auto-mute and mic-switch
3691 */
3692
3693/* check each pin in the given array; returns true if any of them is plugged */
3694static bool detect_jacks(struct hda_codec *codec, int num_pins, hda_nid_t *pins)
3695{
3696 int i, present = 0;
3697
3698 for (i = 0; i < num_pins; i++) {
3699 hda_nid_t nid = pins[i];
3700 if (!nid)
3701 break;
Takashi Iwai0b4df932013-01-10 09:45:13 +01003702 /* don't detect pins retasked as inputs */
3703 if (snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_IN_EN)
3704 continue;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003705 present |= snd_hda_jack_detect(codec, nid);
3706 }
3707 return present;
3708}
3709
3710/* standard HP/line-out auto-mute helper */
3711static void do_automute(struct hda_codec *codec, int num_pins, hda_nid_t *pins,
Takashi Iwai2c12c302013-01-10 09:33:29 +01003712 bool mute)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003713{
3714 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003715 int i;
3716
3717 for (i = 0; i < num_pins; i++) {
3718 hda_nid_t nid = pins[i];
Takashi Iwai967303d2013-02-19 17:12:42 +01003719 unsigned int val, oldval;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003720 if (!nid)
3721 break;
Takashi Iwai967303d2013-02-19 17:12:42 +01003722 oldval = snd_hda_codec_get_pin_target(codec, nid);
3723 if (oldval & PIN_IN)
3724 continue; /* no mute for inputs */
Takashi Iwai352f7f92012-12-19 12:52:06 +01003725 /* don't reset VREF value in case it's controlling
3726 * the amp (see alc861_fixup_asus_amp_vref_0f())
3727 */
Takashi Iwai2c12c302013-01-10 09:33:29 +01003728 if (spec->keep_vref_in_automute)
Takashi Iwai967303d2013-02-19 17:12:42 +01003729 val = oldval & ~PIN_HP;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003730 else
Takashi Iwai352f7f92012-12-19 12:52:06 +01003731 val = 0;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003732 if (!mute)
Takashi Iwai967303d2013-02-19 17:12:42 +01003733 val |= oldval;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003734 /* here we call update_pin_ctl() so that the pinctl is changed
3735 * without changing the pinctl target value;
3736 * the original target value will be still referred at the
3737 * init / resume again
3738 */
3739 update_pin_ctl(codec, nid, val);
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01003740 set_pin_eapd(codec, nid, !mute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003741 }
3742}
3743
3744/* Toggle outputs muting */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003745void snd_hda_gen_update_outputs(struct hda_codec *codec)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003746{
3747 struct hda_gen_spec *spec = codec->spec;
3748 int on;
3749
3750 /* Control HP pins/amps depending on master_mute state;
3751 * in general, HP pins/amps control should be enabled in all cases,
3752 * but currently set only for master_mute, just to be safe
3753 */
Takashi Iwai967303d2013-02-19 17:12:42 +01003754 do_automute(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
Takashi Iwai2c12c302013-01-10 09:33:29 +01003755 spec->autocfg.hp_pins, spec->master_mute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003756
3757 if (!spec->automute_speaker)
3758 on = 0;
3759 else
3760 on = spec->hp_jack_present | spec->line_jack_present;
3761 on |= spec->master_mute;
Takashi Iwai47b9ddb2013-01-16 18:18:00 +01003762 spec->speaker_muted = on;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003763 do_automute(codec, ARRAY_SIZE(spec->autocfg.speaker_pins),
Takashi Iwai2c12c302013-01-10 09:33:29 +01003764 spec->autocfg.speaker_pins, on);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003765
3766 /* toggle line-out mutes if needed, too */
3767 /* if LO is a copy of either HP or Speaker, don't need to handle it */
3768 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0] ||
3769 spec->autocfg.line_out_pins[0] == spec->autocfg.speaker_pins[0])
3770 return;
3771 if (!spec->automute_lo)
3772 on = 0;
3773 else
3774 on = spec->hp_jack_present;
3775 on |= spec->master_mute;
Takashi Iwai47b9ddb2013-01-16 18:18:00 +01003776 spec->line_out_muted = on;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003777 do_automute(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
Takashi Iwai2c12c302013-01-10 09:33:29 +01003778 spec->autocfg.line_out_pins, on);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003779}
Takashi Iwai5d550e12012-12-19 15:16:44 +01003780EXPORT_SYMBOL_HDA(snd_hda_gen_update_outputs);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003781
3782static void call_update_outputs(struct hda_codec *codec)
3783{
3784 struct hda_gen_spec *spec = codec->spec;
3785 if (spec->automute_hook)
3786 spec->automute_hook(codec);
3787 else
Takashi Iwai5d550e12012-12-19 15:16:44 +01003788 snd_hda_gen_update_outputs(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003789}
3790
3791/* standard HP-automute helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003792void snd_hda_gen_hp_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003793{
3794 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai92603c52013-01-22 07:46:31 +01003795 hda_nid_t *pins = spec->autocfg.hp_pins;
3796 int num_pins = ARRAY_SIZE(spec->autocfg.hp_pins);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003797
Takashi Iwai92603c52013-01-22 07:46:31 +01003798 /* No detection for the first HP jack during indep-HP mode */
3799 if (spec->indep_hp_enabled) {
3800 pins++;
3801 num_pins--;
3802 }
3803
3804 spec->hp_jack_present = detect_jacks(codec, num_pins, pins);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003805 if (!spec->detect_hp || (!spec->automute_speaker && !spec->automute_lo))
3806 return;
3807 call_update_outputs(codec);
3808}
Takashi Iwai5d550e12012-12-19 15:16:44 +01003809EXPORT_SYMBOL_HDA(snd_hda_gen_hp_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003810
3811/* standard line-out-automute helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003812void snd_hda_gen_line_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003813{
3814 struct hda_gen_spec *spec = codec->spec;
3815
3816 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
3817 return;
3818 /* check LO jack only when it's different from HP */
3819 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0])
3820 return;
3821
3822 spec->line_jack_present =
3823 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
3824 spec->autocfg.line_out_pins);
3825 if (!spec->automute_speaker || !spec->detect_lo)
3826 return;
3827 call_update_outputs(codec);
3828}
Takashi Iwai5d550e12012-12-19 15:16:44 +01003829EXPORT_SYMBOL_HDA(snd_hda_gen_line_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003830
3831/* standard mic auto-switch helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003832void snd_hda_gen_mic_autoswitch(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003833{
3834 struct hda_gen_spec *spec = codec->spec;
3835 int i;
3836
3837 if (!spec->auto_mic)
3838 return;
3839
3840 for (i = spec->am_num_entries - 1; i > 0; i--) {
Takashi Iwai0b4df932013-01-10 09:45:13 +01003841 hda_nid_t pin = spec->am_entry[i].pin;
3842 /* don't detect pins retasked as outputs */
3843 if (snd_hda_codec_get_pin_target(codec, pin) & AC_PINCTL_OUT_EN)
3844 continue;
3845 if (snd_hda_jack_detect(codec, pin)) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003846 mux_select(codec, 0, spec->am_entry[i].idx);
3847 return;
3848 }
3849 }
3850 mux_select(codec, 0, spec->am_entry[0].idx);
3851}
Takashi Iwai5d550e12012-12-19 15:16:44 +01003852EXPORT_SYMBOL_HDA(snd_hda_gen_mic_autoswitch);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003853
Takashi Iwai77afe0e2013-05-31 14:10:03 +02003854/* call appropriate hooks */
3855static void call_hp_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
3856{
3857 struct hda_gen_spec *spec = codec->spec;
3858 if (spec->hp_automute_hook)
3859 spec->hp_automute_hook(codec, jack);
3860 else
3861 snd_hda_gen_hp_automute(codec, jack);
3862}
3863
3864static void call_line_automute(struct hda_codec *codec,
3865 struct hda_jack_tbl *jack)
3866{
3867 struct hda_gen_spec *spec = codec->spec;
3868 if (spec->line_automute_hook)
3869 spec->line_automute_hook(codec, jack);
3870 else
3871 snd_hda_gen_line_automute(codec, jack);
3872}
3873
3874static void call_mic_autoswitch(struct hda_codec *codec,
3875 struct hda_jack_tbl *jack)
3876{
3877 struct hda_gen_spec *spec = codec->spec;
3878 if (spec->mic_autoswitch_hook)
3879 spec->mic_autoswitch_hook(codec, jack);
3880 else
3881 snd_hda_gen_mic_autoswitch(codec, jack);
3882}
3883
Takashi Iwai963afde2013-05-31 15:20:31 +02003884/* update jack retasking */
3885static void update_automute_all(struct hda_codec *codec)
3886{
3887 call_hp_automute(codec, NULL);
3888 call_line_automute(codec, NULL);
3889 call_mic_autoswitch(codec, NULL);
3890}
3891
Takashi Iwai352f7f92012-12-19 12:52:06 +01003892/*
3893 * Auto-Mute mode mixer enum support
3894 */
3895static int automute_mode_info(struct snd_kcontrol *kcontrol,
3896 struct snd_ctl_elem_info *uinfo)
3897{
3898 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3899 struct hda_gen_spec *spec = codec->spec;
3900 static const char * const texts3[] = {
3901 "Disabled", "Speaker Only", "Line Out+Speaker"
Takashi Iwai071c73a2006-08-23 18:34:06 +02003902 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003903
Takashi Iwai352f7f92012-12-19 12:52:06 +01003904 if (spec->automute_speaker_possible && spec->automute_lo_possible)
3905 return snd_hda_enum_helper_info(kcontrol, uinfo, 3, texts3);
3906 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
3907}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003908
Takashi Iwai352f7f92012-12-19 12:52:06 +01003909static int automute_mode_get(struct snd_kcontrol *kcontrol,
3910 struct snd_ctl_elem_value *ucontrol)
3911{
3912 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3913 struct hda_gen_spec *spec = codec->spec;
3914 unsigned int val = 0;
3915 if (spec->automute_speaker)
3916 val++;
3917 if (spec->automute_lo)
3918 val++;
Takashi Iwai071c73a2006-08-23 18:34:06 +02003919
Takashi Iwai352f7f92012-12-19 12:52:06 +01003920 ucontrol->value.enumerated.item[0] = val;
3921 return 0;
3922}
3923
3924static int automute_mode_put(struct snd_kcontrol *kcontrol,
3925 struct snd_ctl_elem_value *ucontrol)
3926{
3927 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3928 struct hda_gen_spec *spec = codec->spec;
3929
3930 switch (ucontrol->value.enumerated.item[0]) {
3931 case 0:
3932 if (!spec->automute_speaker && !spec->automute_lo)
3933 return 0;
3934 spec->automute_speaker = 0;
3935 spec->automute_lo = 0;
3936 break;
3937 case 1:
3938 if (spec->automute_speaker_possible) {
3939 if (!spec->automute_lo && spec->automute_speaker)
3940 return 0;
3941 spec->automute_speaker = 1;
3942 spec->automute_lo = 0;
3943 } else if (spec->automute_lo_possible) {
3944 if (spec->automute_lo)
3945 return 0;
3946 spec->automute_lo = 1;
3947 } else
3948 return -EINVAL;
3949 break;
3950 case 2:
3951 if (!spec->automute_lo_possible || !spec->automute_speaker_possible)
3952 return -EINVAL;
3953 if (spec->automute_speaker && spec->automute_lo)
3954 return 0;
3955 spec->automute_speaker = 1;
3956 spec->automute_lo = 1;
3957 break;
3958 default:
3959 return -EINVAL;
3960 }
3961 call_update_outputs(codec);
3962 return 1;
3963}
3964
3965static const struct snd_kcontrol_new automute_mode_enum = {
3966 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3967 .name = "Auto-Mute Mode",
3968 .info = automute_mode_info,
3969 .get = automute_mode_get,
3970 .put = automute_mode_put,
3971};
3972
3973static int add_automute_mode_enum(struct hda_codec *codec)
3974{
3975 struct hda_gen_spec *spec = codec->spec;
3976
Takashi Iwai12c93df2012-12-19 14:38:33 +01003977 if (!snd_hda_gen_add_kctl(spec, NULL, &automute_mode_enum))
Takashi Iwai352f7f92012-12-19 12:52:06 +01003978 return -ENOMEM;
3979 return 0;
3980}
3981
3982/*
3983 * Check the availability of HP/line-out auto-mute;
3984 * Set up appropriately if really supported
3985 */
3986static int check_auto_mute_availability(struct hda_codec *codec)
3987{
3988 struct hda_gen_spec *spec = codec->spec;
3989 struct auto_pin_cfg *cfg = &spec->autocfg;
3990 int present = 0;
3991 int i, err;
3992
Takashi Iwaif72706b2013-01-16 18:20:07 +01003993 if (spec->suppress_auto_mute)
3994 return 0;
3995
Takashi Iwai352f7f92012-12-19 12:52:06 +01003996 if (cfg->hp_pins[0])
3997 present++;
3998 if (cfg->line_out_pins[0])
3999 present++;
4000 if (cfg->speaker_pins[0])
4001 present++;
4002 if (present < 2) /* need two different output types */
Takashi Iwai071c73a2006-08-23 18:34:06 +02004003 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004004
4005 if (!cfg->speaker_pins[0] &&
4006 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
4007 memcpy(cfg->speaker_pins, cfg->line_out_pins,
4008 sizeof(cfg->speaker_pins));
4009 cfg->speaker_outs = cfg->line_outs;
Takashi Iwai071c73a2006-08-23 18:34:06 +02004010 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004011
Takashi Iwai352f7f92012-12-19 12:52:06 +01004012 if (!cfg->hp_pins[0] &&
4013 cfg->line_out_type == AUTO_PIN_HP_OUT) {
4014 memcpy(cfg->hp_pins, cfg->line_out_pins,
4015 sizeof(cfg->hp_pins));
4016 cfg->hp_outs = cfg->line_outs;
4017 }
4018
4019 for (i = 0; i < cfg->hp_outs; i++) {
4020 hda_nid_t nid = cfg->hp_pins[i];
4021 if (!is_jack_detectable(codec, nid))
4022 continue;
4023 snd_printdd("hda-codec: Enable HP auto-muting on NID 0x%x\n",
4024 nid);
4025 snd_hda_jack_detect_enable_callback(codec, nid, HDA_GEN_HP_EVENT,
Takashi Iwai77afe0e2013-05-31 14:10:03 +02004026 call_hp_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004027 spec->detect_hp = 1;
4028 }
4029
4030 if (cfg->line_out_type == AUTO_PIN_LINE_OUT && cfg->line_outs) {
4031 if (cfg->speaker_outs)
4032 for (i = 0; i < cfg->line_outs; i++) {
4033 hda_nid_t nid = cfg->line_out_pins[i];
4034 if (!is_jack_detectable(codec, nid))
4035 continue;
4036 snd_printdd("hda-codec: Enable Line-Out auto-muting on NID 0x%x\n", nid);
4037 snd_hda_jack_detect_enable_callback(codec, nid,
4038 HDA_GEN_FRONT_EVENT,
Takashi Iwai77afe0e2013-05-31 14:10:03 +02004039 call_line_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004040 spec->detect_lo = 1;
4041 }
4042 spec->automute_lo_possible = spec->detect_hp;
4043 }
4044
4045 spec->automute_speaker_possible = cfg->speaker_outs &&
4046 (spec->detect_hp || spec->detect_lo);
4047
4048 spec->automute_lo = spec->automute_lo_possible;
4049 spec->automute_speaker = spec->automute_speaker_possible;
4050
4051 if (spec->automute_speaker_possible || spec->automute_lo_possible) {
4052 /* create a control for automute mode */
4053 err = add_automute_mode_enum(codec);
4054 if (err < 0)
4055 return err;
4056 }
4057 return 0;
4058}
4059
Takashi Iwai352f7f92012-12-19 12:52:06 +01004060/* check whether all auto-mic pins are valid; setup indices if OK */
4061static bool auto_mic_check_imux(struct hda_codec *codec)
4062{
4063 struct hda_gen_spec *spec = codec->spec;
4064 const struct hda_input_mux *imux;
4065 int i;
4066
4067 imux = &spec->input_mux;
4068 for (i = 0; i < spec->am_num_entries; i++) {
4069 spec->am_entry[i].idx =
4070 find_idx_in_nid_list(spec->am_entry[i].pin,
4071 spec->imux_pins, imux->num_items);
4072 if (spec->am_entry[i].idx < 0)
4073 return false; /* no corresponding imux */
4074 }
4075
4076 /* we don't need the jack detection for the first pin */
4077 for (i = 1; i < spec->am_num_entries; i++)
4078 snd_hda_jack_detect_enable_callback(codec,
4079 spec->am_entry[i].pin,
4080 HDA_GEN_MIC_EVENT,
Takashi Iwai77afe0e2013-05-31 14:10:03 +02004081 call_mic_autoswitch);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004082 return true;
4083}
4084
4085static int compare_attr(const void *ap, const void *bp)
4086{
4087 const struct automic_entry *a = ap;
4088 const struct automic_entry *b = bp;
4089 return (int)(a->attr - b->attr);
4090}
4091
4092/*
4093 * Check the availability of auto-mic switch;
4094 * Set up if really supported
4095 */
4096static int check_auto_mic_availability(struct hda_codec *codec)
4097{
4098 struct hda_gen_spec *spec = codec->spec;
4099 struct auto_pin_cfg *cfg = &spec->autocfg;
4100 unsigned int types;
4101 int i, num_pins;
4102
Takashi Iwaid12daf62013-01-07 16:32:11 +01004103 if (spec->suppress_auto_mic)
4104 return 0;
4105
Takashi Iwai352f7f92012-12-19 12:52:06 +01004106 types = 0;
4107 num_pins = 0;
4108 for (i = 0; i < cfg->num_inputs; i++) {
4109 hda_nid_t nid = cfg->inputs[i].pin;
4110 unsigned int attr;
4111 attr = snd_hda_codec_get_pincfg(codec, nid);
4112 attr = snd_hda_get_input_pin_attr(attr);
4113 if (types & (1 << attr))
4114 return 0; /* already occupied */
4115 switch (attr) {
4116 case INPUT_PIN_ATTR_INT:
4117 if (cfg->inputs[i].type != AUTO_PIN_MIC)
4118 return 0; /* invalid type */
4119 break;
4120 case INPUT_PIN_ATTR_UNUSED:
4121 return 0; /* invalid entry */
4122 default:
4123 if (cfg->inputs[i].type > AUTO_PIN_LINE_IN)
4124 return 0; /* invalid type */
4125 if (!spec->line_in_auto_switch &&
4126 cfg->inputs[i].type != AUTO_PIN_MIC)
4127 return 0; /* only mic is allowed */
4128 if (!is_jack_detectable(codec, nid))
4129 return 0; /* no unsol support */
4130 break;
4131 }
4132 if (num_pins >= MAX_AUTO_MIC_PINS)
4133 return 0;
4134 types |= (1 << attr);
4135 spec->am_entry[num_pins].pin = nid;
4136 spec->am_entry[num_pins].attr = attr;
4137 num_pins++;
4138 }
4139
4140 if (num_pins < 2)
4141 return 0;
4142
4143 spec->am_num_entries = num_pins;
4144 /* sort the am_entry in the order of attr so that the pin with a
4145 * higher attr will be selected when the jack is plugged.
4146 */
4147 sort(spec->am_entry, num_pins, sizeof(spec->am_entry[0]),
4148 compare_attr, NULL);
4149
4150 if (!auto_mic_check_imux(codec))
4151 return 0;
4152
4153 spec->auto_mic = 1;
4154 spec->num_adc_nids = 1;
4155 spec->cur_mux[0] = spec->am_entry[0].idx;
4156 snd_printdd("hda-codec: Enable auto-mic switch on NID 0x%x/0x%x/0x%x\n",
4157 spec->am_entry[0].pin,
4158 spec->am_entry[1].pin,
4159 spec->am_entry[2].pin);
4160
4161 return 0;
4162}
4163
Takashi Iwai55196ff2013-01-24 17:32:56 +01004164/* power_filter hook; make inactive widgets into power down */
4165static unsigned int snd_hda_gen_path_power_filter(struct hda_codec *codec,
4166 hda_nid_t nid,
4167 unsigned int power_state)
4168{
4169 if (power_state != AC_PWRST_D0)
4170 return power_state;
4171 if (get_wcaps_type(get_wcaps(codec, nid)) >= AC_WID_POWER)
4172 return power_state;
Takashi Iwaib1b9fbd2013-05-14 12:58:47 +02004173 if (is_active_nid_for_any(codec, nid))
Takashi Iwai55196ff2013-01-24 17:32:56 +01004174 return power_state;
4175 return AC_PWRST_D3;
4176}
4177
Takashi Iwai352f7f92012-12-19 12:52:06 +01004178
Takashi Iwai9eb413e2012-12-19 14:41:21 +01004179/*
4180 * Parse the given BIOS configuration and set up the hda_gen_spec
4181 *
4182 * return 1 if successful, 0 if the proper config is not found,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004183 * or a negative error code
4184 */
4185int snd_hda_gen_parse_auto_config(struct hda_codec *codec,
Takashi Iwai9eb413e2012-12-19 14:41:21 +01004186 struct auto_pin_cfg *cfg)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004187{
4188 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004189 int err;
4190
Takashi Iwai1c70a582013-01-11 17:48:22 +01004191 parse_user_hints(codec);
4192
Takashi Iwaie4a395e2013-01-23 17:00:31 +01004193 if (spec->mixer_nid && !spec->mixer_merge_nid)
4194 spec->mixer_merge_nid = spec->mixer_nid;
4195
Takashi Iwai9eb413e2012-12-19 14:41:21 +01004196 if (cfg != &spec->autocfg) {
4197 spec->autocfg = *cfg;
4198 cfg = &spec->autocfg;
4199 }
4200
Takashi Iwai98bd1112013-03-22 14:53:50 +01004201 if (!spec->main_out_badness)
4202 spec->main_out_badness = &hda_main_out_badness;
4203 if (!spec->extra_out_badness)
4204 spec->extra_out_badness = &hda_extra_out_badness;
4205
David Henningsson6fc4cb92013-01-16 15:58:45 +01004206 fill_all_dac_nids(codec);
4207
Takashi Iwai352f7f92012-12-19 12:52:06 +01004208 if (!cfg->line_outs) {
4209 if (cfg->dig_outs || cfg->dig_in_pin) {
4210 spec->multiout.max_channels = 2;
4211 spec->no_analog = 1;
4212 goto dig_only;
4213 }
4214 return 0; /* can't find valid BIOS pin config */
4215 }
4216
4217 if (!spec->no_primary_hp &&
4218 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT &&
4219 cfg->line_outs <= cfg->hp_outs) {
4220 /* use HP as primary out */
4221 cfg->speaker_outs = cfg->line_outs;
4222 memcpy(cfg->speaker_pins, cfg->line_out_pins,
4223 sizeof(cfg->speaker_pins));
4224 cfg->line_outs = cfg->hp_outs;
4225 memcpy(cfg->line_out_pins, cfg->hp_pins, sizeof(cfg->hp_pins));
4226 cfg->hp_outs = 0;
4227 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
4228 cfg->line_out_type = AUTO_PIN_HP_OUT;
4229 }
4230
4231 err = parse_output_paths(codec);
4232 if (err < 0)
4233 return err;
4234 err = create_multi_channel_mode(codec);
4235 if (err < 0)
4236 return err;
4237 err = create_multi_out_ctls(codec, cfg);
4238 if (err < 0)
4239 return err;
4240 err = create_hp_out_ctls(codec);
4241 if (err < 0)
4242 return err;
4243 err = create_speaker_out_ctls(codec);
4244 if (err < 0)
4245 return err;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004246 err = create_indep_hp_ctls(codec);
4247 if (err < 0)
4248 return err;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01004249 err = create_loopback_mixing_ctl(codec);
4250 if (err < 0)
4251 return err;
Takashi Iwai967303d2013-02-19 17:12:42 +01004252 err = create_hp_mic(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004253 if (err < 0)
4254 return err;
4255 err = create_input_ctls(codec);
Takashi Iwaid13bd412008-07-30 15:01:45 +02004256 if (err < 0)
Takashi Iwai071c73a2006-08-23 18:34:06 +02004257 return err;
4258
Takashi Iwaia07a9492013-01-07 16:44:06 +01004259 spec->const_channel_count = spec->ext_channel_count;
4260 /* check the multiple speaker and headphone pins */
4261 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
4262 spec->const_channel_count = max(spec->const_channel_count,
4263 cfg->speaker_outs * 2);
4264 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
4265 spec->const_channel_count = max(spec->const_channel_count,
4266 cfg->hp_outs * 2);
4267 spec->multiout.max_channels = max(spec->ext_channel_count,
4268 spec->const_channel_count);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004269
4270 err = check_auto_mute_availability(codec);
4271 if (err < 0)
4272 return err;
4273
4274 err = check_dyn_adc_switch(codec);
4275 if (err < 0)
4276 return err;
4277
Takashi Iwai967303d2013-02-19 17:12:42 +01004278 err = check_auto_mic_availability(codec);
4279 if (err < 0)
4280 return err;
Takashi Iwai071c73a2006-08-23 18:34:06 +02004281
Takashi Iwai352f7f92012-12-19 12:52:06 +01004282 err = create_capture_mixers(codec);
4283 if (err < 0)
4284 return err;
4285
4286 err = parse_mic_boost(codec);
4287 if (err < 0)
4288 return err;
4289
Takashi Iwaif811c3c2013-03-07 18:32:59 +01004290 if (spec->add_jack_modes) {
Takashi Iwai978e77e2013-01-10 16:57:58 +01004291 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
4292 err = create_out_jack_modes(codec, cfg->line_outs,
4293 cfg->line_out_pins);
4294 if (err < 0)
4295 return err;
4296 }
4297 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
4298 err = create_out_jack_modes(codec, cfg->hp_outs,
4299 cfg->hp_pins);
4300 if (err < 0)
4301 return err;
4302 }
4303 }
4304
Takashi Iwai352f7f92012-12-19 12:52:06 +01004305 dig_only:
4306 parse_digital(codec);
4307
Takashi Iwai55196ff2013-01-24 17:32:56 +01004308 if (spec->power_down_unused)
4309 codec->power_filter = snd_hda_gen_path_power_filter;
4310
Takashi Iwai7504b6c2013-03-18 11:25:51 +01004311 if (!spec->no_analog && spec->beep_nid) {
4312 err = snd_hda_attach_beep_device(codec, spec->beep_nid);
4313 if (err < 0)
4314 return err;
4315 }
4316
Takashi Iwai352f7f92012-12-19 12:52:06 +01004317 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004318}
Takashi Iwai352f7f92012-12-19 12:52:06 +01004319EXPORT_SYMBOL_HDA(snd_hda_gen_parse_auto_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004320
4321
4322/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01004323 * Build control elements
Linus Torvalds1da177e2005-04-16 15:20:36 -07004324 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01004325
4326/* slave controls for virtual master */
4327static const char * const slave_pfxs[] = {
4328 "Front", "Surround", "Center", "LFE", "Side",
4329 "Headphone", "Speaker", "Mono", "Line Out",
4330 "CLFE", "Bass Speaker", "PCM",
Takashi Iwaiee79c692013-01-07 09:57:42 +01004331 "Speaker Front", "Speaker Surround", "Speaker CLFE", "Speaker Side",
4332 "Headphone Front", "Headphone Surround", "Headphone CLFE",
4333 "Headphone Side",
Takashi Iwai352f7f92012-12-19 12:52:06 +01004334 NULL,
4335};
4336
4337int snd_hda_gen_build_controls(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004338{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004339 struct hda_gen_spec *spec = codec->spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004340 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004341
Takashi Iwai36502d02012-12-19 15:15:10 +01004342 if (spec->kctls.used) {
4343 err = snd_hda_add_new_ctls(codec, spec->kctls.list);
4344 if (err < 0)
4345 return err;
4346 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004347
Takashi Iwai352f7f92012-12-19 12:52:06 +01004348 if (spec->multiout.dig_out_nid) {
4349 err = snd_hda_create_dig_out_ctls(codec,
4350 spec->multiout.dig_out_nid,
4351 spec->multiout.dig_out_nid,
4352 spec->pcm_rec[1].pcm_type);
4353 if (err < 0)
4354 return err;
4355 if (!spec->no_analog) {
4356 err = snd_hda_create_spdif_share_sw(codec,
4357 &spec->multiout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004358 if (err < 0)
4359 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004360 spec->multiout.share_spdif = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004361 }
4362 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01004363 if (spec->dig_in_nid) {
4364 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
4365 if (err < 0)
4366 return err;
4367 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004368
Takashi Iwai352f7f92012-12-19 12:52:06 +01004369 /* if we have no master control, let's create it */
4370 if (!spec->no_analog &&
4371 !snd_hda_find_mixer_ctl(codec, "Master Playback Volume")) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01004372 err = snd_hda_add_vmaster(codec, "Master Playback Volume",
Takashi Iwai7a71bbf2013-01-17 10:25:15 +01004373 spec->vmaster_tlv, slave_pfxs,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004374 "Playback Volume");
4375 if (err < 0)
4376 return err;
4377 }
4378 if (!spec->no_analog &&
4379 !snd_hda_find_mixer_ctl(codec, "Master Playback Switch")) {
4380 err = __snd_hda_add_vmaster(codec, "Master Playback Switch",
4381 NULL, slave_pfxs,
4382 "Playback Switch",
4383 true, &spec->vmaster_mute.sw_kctl);
4384 if (err < 0)
4385 return err;
4386 if (spec->vmaster_mute.hook)
Takashi Iwaifd25a972012-12-20 14:57:18 +01004387 snd_hda_add_vmaster_hook(codec, &spec->vmaster_mute,
4388 spec->vmaster_mute_enum);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004389 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004390
Takashi Iwai352f7f92012-12-19 12:52:06 +01004391 free_kctls(spec); /* no longer needed */
4392
Takashi Iwai352f7f92012-12-19 12:52:06 +01004393 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
4394 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004395 return err;
4396
4397 return 0;
4398}
Takashi Iwai352f7f92012-12-19 12:52:06 +01004399EXPORT_SYMBOL_HDA(snd_hda_gen_build_controls);
4400
Linus Torvalds1da177e2005-04-16 15:20:36 -07004401
4402/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01004403 * PCM definitions
Linus Torvalds1da177e2005-04-16 15:20:36 -07004404 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004405
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004406static void call_pcm_playback_hook(struct hda_pcm_stream *hinfo,
4407 struct hda_codec *codec,
4408 struct snd_pcm_substream *substream,
4409 int action)
4410{
4411 struct hda_gen_spec *spec = codec->spec;
4412 if (spec->pcm_playback_hook)
4413 spec->pcm_playback_hook(hinfo, codec, substream, action);
4414}
4415
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004416static void call_pcm_capture_hook(struct hda_pcm_stream *hinfo,
4417 struct hda_codec *codec,
4418 struct snd_pcm_substream *substream,
4419 int action)
4420{
4421 struct hda_gen_spec *spec = codec->spec;
4422 if (spec->pcm_capture_hook)
4423 spec->pcm_capture_hook(hinfo, codec, substream, action);
4424}
4425
Takashi Iwai352f7f92012-12-19 12:52:06 +01004426/*
4427 * Analog playback callbacks
4428 */
4429static int playback_pcm_open(struct hda_pcm_stream *hinfo,
4430 struct hda_codec *codec,
4431 struct snd_pcm_substream *substream)
4432{
4433 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004434 int err;
4435
4436 mutex_lock(&spec->pcm_mutex);
4437 err = snd_hda_multi_out_analog_open(codec,
4438 &spec->multiout, substream,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004439 hinfo);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004440 if (!err) {
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004441 spec->active_streams |= 1 << STREAM_MULTI_OUT;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004442 call_pcm_playback_hook(hinfo, codec, substream,
4443 HDA_GEN_PCM_ACT_OPEN);
4444 }
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004445 mutex_unlock(&spec->pcm_mutex);
4446 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004447}
4448
4449static int playback_pcm_prepare(struct hda_pcm_stream *hinfo,
Takashi Iwai97ec5582006-03-21 11:29:07 +01004450 struct hda_codec *codec,
4451 unsigned int stream_tag,
4452 unsigned int format,
4453 struct snd_pcm_substream *substream)
4454{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004455 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004456 int err;
4457
4458 err = snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
4459 stream_tag, format, substream);
4460 if (!err)
4461 call_pcm_playback_hook(hinfo, codec, substream,
4462 HDA_GEN_PCM_ACT_PREPARE);
4463 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004464}
Takashi Iwai97ec5582006-03-21 11:29:07 +01004465
Takashi Iwai352f7f92012-12-19 12:52:06 +01004466static int playback_pcm_cleanup(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;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004471 int err;
4472
4473 err = snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
4474 if (!err)
4475 call_pcm_playback_hook(hinfo, codec, substream,
4476 HDA_GEN_PCM_ACT_CLEANUP);
4477 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004478}
4479
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004480static int playback_pcm_close(struct hda_pcm_stream *hinfo,
4481 struct hda_codec *codec,
4482 struct snd_pcm_substream *substream)
4483{
4484 struct hda_gen_spec *spec = codec->spec;
4485 mutex_lock(&spec->pcm_mutex);
4486 spec->active_streams &= ~(1 << STREAM_MULTI_OUT);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004487 call_pcm_playback_hook(hinfo, codec, substream,
4488 HDA_GEN_PCM_ACT_CLOSE);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004489 mutex_unlock(&spec->pcm_mutex);
4490 return 0;
4491}
4492
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004493static int capture_pcm_open(struct hda_pcm_stream *hinfo,
4494 struct hda_codec *codec,
4495 struct snd_pcm_substream *substream)
4496{
4497 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_OPEN);
4498 return 0;
4499}
4500
4501static int capture_pcm_prepare(struct hda_pcm_stream *hinfo,
4502 struct hda_codec *codec,
4503 unsigned int stream_tag,
4504 unsigned int format,
4505 struct snd_pcm_substream *substream)
4506{
4507 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4508 call_pcm_capture_hook(hinfo, codec, substream,
4509 HDA_GEN_PCM_ACT_PREPARE);
4510 return 0;
4511}
4512
4513static int capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
4514 struct hda_codec *codec,
4515 struct snd_pcm_substream *substream)
4516{
4517 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
4518 call_pcm_capture_hook(hinfo, codec, substream,
4519 HDA_GEN_PCM_ACT_CLEANUP);
4520 return 0;
4521}
4522
4523static int capture_pcm_close(struct hda_pcm_stream *hinfo,
4524 struct hda_codec *codec,
4525 struct snd_pcm_substream *substream)
4526{
4527 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_CLOSE);
4528 return 0;
4529}
4530
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004531static int alt_playback_pcm_open(struct hda_pcm_stream *hinfo,
4532 struct hda_codec *codec,
4533 struct snd_pcm_substream *substream)
4534{
4535 struct hda_gen_spec *spec = codec->spec;
4536 int err = 0;
4537
4538 mutex_lock(&spec->pcm_mutex);
4539 if (!spec->indep_hp_enabled)
4540 err = -EBUSY;
4541 else
4542 spec->active_streams |= 1 << STREAM_INDEP_HP;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004543 call_pcm_playback_hook(hinfo, codec, substream,
4544 HDA_GEN_PCM_ACT_OPEN);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004545 mutex_unlock(&spec->pcm_mutex);
4546 return err;
4547}
4548
4549static int alt_playback_pcm_close(struct hda_pcm_stream *hinfo,
4550 struct hda_codec *codec,
4551 struct snd_pcm_substream *substream)
4552{
4553 struct hda_gen_spec *spec = codec->spec;
4554 mutex_lock(&spec->pcm_mutex);
4555 spec->active_streams &= ~(1 << STREAM_INDEP_HP);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004556 call_pcm_playback_hook(hinfo, codec, substream,
4557 HDA_GEN_PCM_ACT_CLOSE);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004558 mutex_unlock(&spec->pcm_mutex);
4559 return 0;
4560}
4561
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004562static int alt_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
4563 struct hda_codec *codec,
4564 unsigned int stream_tag,
4565 unsigned int format,
4566 struct snd_pcm_substream *substream)
4567{
4568 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4569 call_pcm_playback_hook(hinfo, codec, substream,
4570 HDA_GEN_PCM_ACT_PREPARE);
4571 return 0;
4572}
4573
4574static int alt_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
4575 struct hda_codec *codec,
4576 struct snd_pcm_substream *substream)
4577{
4578 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
4579 call_pcm_playback_hook(hinfo, codec, substream,
4580 HDA_GEN_PCM_ACT_CLEANUP);
4581 return 0;
4582}
4583
Takashi Iwai352f7f92012-12-19 12:52:06 +01004584/*
4585 * Digital out
4586 */
4587static int dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
4588 struct hda_codec *codec,
4589 struct snd_pcm_substream *substream)
4590{
4591 struct hda_gen_spec *spec = codec->spec;
4592 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
4593}
4594
4595static int dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
4596 struct hda_codec *codec,
4597 unsigned int stream_tag,
4598 unsigned int format,
4599 struct snd_pcm_substream *substream)
4600{
4601 struct hda_gen_spec *spec = codec->spec;
4602 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
4603 stream_tag, format, substream);
4604}
4605
4606static int dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
4607 struct hda_codec *codec,
4608 struct snd_pcm_substream *substream)
4609{
4610 struct hda_gen_spec *spec = codec->spec;
4611 return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
4612}
4613
4614static int dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
4615 struct hda_codec *codec,
4616 struct snd_pcm_substream *substream)
4617{
4618 struct hda_gen_spec *spec = codec->spec;
4619 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
4620}
4621
4622/*
4623 * Analog capture
4624 */
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004625#define alt_capture_pcm_open capture_pcm_open
4626#define alt_capture_pcm_close capture_pcm_close
4627
Takashi Iwai352f7f92012-12-19 12:52:06 +01004628static int alt_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
4629 struct hda_codec *codec,
4630 unsigned int stream_tag,
4631 unsigned int format,
4632 struct snd_pcm_substream *substream)
4633{
4634 struct hda_gen_spec *spec = codec->spec;
4635
4636 snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number + 1],
Takashi Iwai97ec5582006-03-21 11:29:07 +01004637 stream_tag, 0, format);
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004638 call_pcm_capture_hook(hinfo, codec, substream,
4639 HDA_GEN_PCM_ACT_PREPARE);
Takashi Iwai97ec5582006-03-21 11:29:07 +01004640 return 0;
4641}
4642
Takashi Iwai352f7f92012-12-19 12:52:06 +01004643static int alt_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
4644 struct hda_codec *codec,
4645 struct snd_pcm_substream *substream)
Takashi Iwai97ec5582006-03-21 11:29:07 +01004646{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004647 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai97ec5582006-03-21 11:29:07 +01004648
Takashi Iwai352f7f92012-12-19 12:52:06 +01004649 snd_hda_codec_cleanup_stream(codec,
4650 spec->adc_nids[substream->number + 1]);
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004651 call_pcm_capture_hook(hinfo, codec, substream,
4652 HDA_GEN_PCM_ACT_CLEANUP);
Takashi Iwai97ec5582006-03-21 11:29:07 +01004653 return 0;
4654}
4655
Takashi Iwai352f7f92012-12-19 12:52:06 +01004656/*
4657 */
4658static const struct hda_pcm_stream pcm_analog_playback = {
4659 .substreams = 1,
4660 .channels_min = 2,
4661 .channels_max = 8,
4662 /* NID is set in build_pcms */
4663 .ops = {
4664 .open = playback_pcm_open,
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004665 .close = playback_pcm_close,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004666 .prepare = playback_pcm_prepare,
4667 .cleanup = playback_pcm_cleanup
4668 },
4669};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004670
Takashi Iwai352f7f92012-12-19 12:52:06 +01004671static const struct hda_pcm_stream pcm_analog_capture = {
4672 .substreams = 1,
4673 .channels_min = 2,
4674 .channels_max = 2,
4675 /* NID is set in build_pcms */
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004676 .ops = {
4677 .open = capture_pcm_open,
4678 .close = capture_pcm_close,
4679 .prepare = capture_pcm_prepare,
4680 .cleanup = capture_pcm_cleanup
4681 },
Takashi Iwai352f7f92012-12-19 12:52:06 +01004682};
4683
4684static const struct hda_pcm_stream pcm_analog_alt_playback = {
4685 .substreams = 1,
4686 .channels_min = 2,
4687 .channels_max = 2,
4688 /* NID is set in build_pcms */
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004689 .ops = {
4690 .open = alt_playback_pcm_open,
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004691 .close = alt_playback_pcm_close,
4692 .prepare = alt_playback_pcm_prepare,
4693 .cleanup = alt_playback_pcm_cleanup
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004694 },
Takashi Iwai352f7f92012-12-19 12:52:06 +01004695};
4696
4697static const struct hda_pcm_stream pcm_analog_alt_capture = {
4698 .substreams = 2, /* can be overridden */
4699 .channels_min = 2,
4700 .channels_max = 2,
4701 /* NID is set in build_pcms */
4702 .ops = {
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004703 .open = alt_capture_pcm_open,
4704 .close = alt_capture_pcm_close,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004705 .prepare = alt_capture_pcm_prepare,
4706 .cleanup = alt_capture_pcm_cleanup
4707 },
4708};
4709
4710static const struct hda_pcm_stream pcm_digital_playback = {
4711 .substreams = 1,
4712 .channels_min = 2,
4713 .channels_max = 2,
4714 /* NID is set in build_pcms */
4715 .ops = {
4716 .open = dig_playback_pcm_open,
4717 .close = dig_playback_pcm_close,
4718 .prepare = dig_playback_pcm_prepare,
4719 .cleanup = dig_playback_pcm_cleanup
4720 },
4721};
4722
4723static const struct hda_pcm_stream pcm_digital_capture = {
4724 .substreams = 1,
4725 .channels_min = 2,
4726 .channels_max = 2,
4727 /* NID is set in build_pcms */
4728};
4729
4730/* Used by build_pcms to flag that a PCM has no playback stream */
4731static const struct hda_pcm_stream pcm_null_stream = {
4732 .substreams = 0,
4733 .channels_min = 0,
4734 .channels_max = 0,
4735};
4736
4737/*
4738 * dynamic changing ADC PCM streams
4739 */
4740static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur)
4741{
4742 struct hda_gen_spec *spec = codec->spec;
4743 hda_nid_t new_adc = spec->adc_nids[spec->dyn_adc_idx[cur]];
4744
4745 if (spec->cur_adc && spec->cur_adc != new_adc) {
4746 /* stream is running, let's swap the current ADC */
4747 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
4748 spec->cur_adc = new_adc;
4749 snd_hda_codec_setup_stream(codec, new_adc,
4750 spec->cur_adc_stream_tag, 0,
4751 spec->cur_adc_format);
4752 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004753 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01004754 return false;
4755}
4756
4757/* analog capture with dynamic dual-adc changes */
4758static int dyn_adc_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
4759 struct hda_codec *codec,
4760 unsigned int stream_tag,
4761 unsigned int format,
4762 struct snd_pcm_substream *substream)
4763{
4764 struct hda_gen_spec *spec = codec->spec;
4765 spec->cur_adc = spec->adc_nids[spec->dyn_adc_idx[spec->cur_mux[0]]];
4766 spec->cur_adc_stream_tag = stream_tag;
4767 spec->cur_adc_format = format;
4768 snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
4769 return 0;
4770}
4771
4772static int dyn_adc_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
4773 struct hda_codec *codec,
4774 struct snd_pcm_substream *substream)
4775{
4776 struct hda_gen_spec *spec = codec->spec;
4777 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
4778 spec->cur_adc = 0;
4779 return 0;
4780}
4781
4782static const struct hda_pcm_stream dyn_adc_pcm_analog_capture = {
4783 .substreams = 1,
4784 .channels_min = 2,
4785 .channels_max = 2,
4786 .nid = 0, /* fill later */
4787 .ops = {
4788 .prepare = dyn_adc_capture_pcm_prepare,
4789 .cleanup = dyn_adc_capture_pcm_cleanup
4790 },
4791};
4792
Takashi Iwaif873e532012-12-20 16:58:39 +01004793static void fill_pcm_stream_name(char *str, size_t len, const char *sfx,
4794 const char *chip_name)
4795{
4796 char *p;
4797
4798 if (*str)
4799 return;
4800 strlcpy(str, chip_name, len);
4801
4802 /* drop non-alnum chars after a space */
4803 for (p = strchr(str, ' '); p; p = strchr(p + 1, ' ')) {
4804 if (!isalnum(p[1])) {
4805 *p = 0;
4806 break;
4807 }
4808 }
4809 strlcat(str, sfx, len);
4810}
4811
Takashi Iwai352f7f92012-12-19 12:52:06 +01004812/* build PCM streams based on the parsed results */
4813int snd_hda_gen_build_pcms(struct hda_codec *codec)
4814{
4815 struct hda_gen_spec *spec = codec->spec;
4816 struct hda_pcm *info = spec->pcm_rec;
4817 const struct hda_pcm_stream *p;
4818 bool have_multi_adcs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004819
4820 codec->num_pcms = 1;
4821 codec->pcm_info = info;
4822
Takashi Iwai352f7f92012-12-19 12:52:06 +01004823 if (spec->no_analog)
4824 goto skip_analog;
4825
Takashi Iwaif873e532012-12-20 16:58:39 +01004826 fill_pcm_stream_name(spec->stream_name_analog,
4827 sizeof(spec->stream_name_analog),
4828 " Analog", codec->chip_name);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004829 info->name = spec->stream_name_analog;
4830
4831 if (spec->multiout.num_dacs > 0) {
4832 p = spec->stream_analog_playback;
4833 if (!p)
4834 p = &pcm_analog_playback;
4835 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
4836 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dac_nids[0];
4837 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
4838 spec->multiout.max_channels;
4839 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT &&
4840 spec->autocfg.line_outs == 2)
4841 info->stream[SNDRV_PCM_STREAM_PLAYBACK].chmap =
4842 snd_pcm_2_1_chmaps;
4843 }
4844 if (spec->num_adc_nids) {
4845 p = spec->stream_analog_capture;
4846 if (!p) {
4847 if (spec->dyn_adc_switch)
4848 p = &dyn_adc_pcm_analog_capture;
4849 else
4850 p = &pcm_analog_capture;
4851 }
4852 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
4853 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adc_nids[0];
4854 }
4855
Takashi Iwai352f7f92012-12-19 12:52:06 +01004856 skip_analog:
4857 /* SPDIF for stream index #1 */
4858 if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
Takashi Iwaif873e532012-12-20 16:58:39 +01004859 fill_pcm_stream_name(spec->stream_name_digital,
4860 sizeof(spec->stream_name_digital),
4861 " Digital", codec->chip_name);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004862 codec->num_pcms = 2;
4863 codec->slave_dig_outs = spec->multiout.slave_dig_outs;
4864 info = spec->pcm_rec + 1;
4865 info->name = spec->stream_name_digital;
4866 if (spec->dig_out_type)
4867 info->pcm_type = spec->dig_out_type;
4868 else
4869 info->pcm_type = HDA_PCM_TYPE_SPDIF;
4870 if (spec->multiout.dig_out_nid) {
4871 p = spec->stream_digital_playback;
4872 if (!p)
4873 p = &pcm_digital_playback;
4874 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
4875 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dig_out_nid;
4876 }
4877 if (spec->dig_in_nid) {
4878 p = spec->stream_digital_capture;
4879 if (!p)
4880 p = &pcm_digital_capture;
4881 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
4882 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in_nid;
4883 }
4884 }
4885
4886 if (spec->no_analog)
4887 return 0;
4888
4889 /* If the use of more than one ADC is requested for the current
4890 * model, configure a second analog capture-only PCM.
4891 */
4892 have_multi_adcs = (spec->num_adc_nids > 1) &&
4893 !spec->dyn_adc_switch && !spec->auto_mic;
4894 /* Additional Analaog capture for index #2 */
4895 if (spec->alt_dac_nid || have_multi_adcs) {
Takashi Iwaia6071482013-01-21 16:50:09 +01004896 fill_pcm_stream_name(spec->stream_name_alt_analog,
4897 sizeof(spec->stream_name_alt_analog),
4898 " Alt Analog", codec->chip_name);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004899 codec->num_pcms = 3;
4900 info = spec->pcm_rec + 2;
Takashi Iwaia6071482013-01-21 16:50:09 +01004901 info->name = spec->stream_name_alt_analog;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004902 if (spec->alt_dac_nid) {
4903 p = spec->stream_analog_alt_playback;
4904 if (!p)
4905 p = &pcm_analog_alt_playback;
4906 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
4907 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
4908 spec->alt_dac_nid;
4909 } else {
4910 info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
4911 pcm_null_stream;
4912 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = 0;
4913 }
4914 if (have_multi_adcs) {
4915 p = spec->stream_analog_alt_capture;
4916 if (!p)
4917 p = &pcm_analog_alt_capture;
4918 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
4919 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
4920 spec->adc_nids[1];
4921 info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams =
4922 spec->num_adc_nids - 1;
4923 } else {
4924 info->stream[SNDRV_PCM_STREAM_CAPTURE] =
4925 pcm_null_stream;
4926 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = 0;
4927 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004928 }
4929
4930 return 0;
4931}
Takashi Iwai352f7f92012-12-19 12:52:06 +01004932EXPORT_SYMBOL_HDA(snd_hda_gen_build_pcms);
4933
4934
4935/*
4936 * Standard auto-parser initializations
4937 */
4938
Takashi Iwaid4156932013-01-07 10:08:02 +01004939/* configure the given path as a proper output */
Takashi Iwai2c12c302013-01-10 09:33:29 +01004940static void set_output_and_unmute(struct hda_codec *codec, int path_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004941{
4942 struct nid_path *path;
Takashi Iwaid4156932013-01-07 10:08:02 +01004943 hda_nid_t pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004944
Takashi Iwai196c17662013-01-04 15:01:40 +01004945 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwaid4156932013-01-07 10:08:02 +01004946 if (!path || !path->depth)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004947 return;
Takashi Iwaid4156932013-01-07 10:08:02 +01004948 pin = path->path[path->depth - 1];
Takashi Iwai2c12c302013-01-10 09:33:29 +01004949 restore_pin_ctl(codec, pin);
Takashi Iwai65033cc2013-04-16 12:31:05 +02004950 snd_hda_activate_path(codec, path, path->active,
4951 aamix_default(codec->spec));
Takashi Iwaie1284af2013-01-03 16:33:02 +01004952 set_pin_eapd(codec, pin, path->active);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004953}
4954
4955/* initialize primary output paths */
4956static void init_multi_out(struct hda_codec *codec)
4957{
4958 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004959 int i;
4960
Takashi Iwaid4156932013-01-07 10:08:02 +01004961 for (i = 0; i < spec->autocfg.line_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01004962 set_output_and_unmute(codec, spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004963}
4964
Takashi Iwaidb23fd12012-12-20 15:27:24 +01004965
Takashi Iwai2c12c302013-01-10 09:33:29 +01004966static void __init_extra_out(struct hda_codec *codec, int num_outs, int *paths)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004967{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004968 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004969
Takashi Iwaid4156932013-01-07 10:08:02 +01004970 for (i = 0; i < num_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01004971 set_output_and_unmute(codec, paths[i]);
Takashi Iwaidb23fd12012-12-20 15:27:24 +01004972}
4973
4974/* initialize hp and speaker paths */
4975static void init_extra_out(struct hda_codec *codec)
4976{
4977 struct hda_gen_spec *spec = codec->spec;
4978
4979 if (spec->autocfg.line_out_type != AUTO_PIN_HP_OUT)
Takashi Iwai2c12c302013-01-10 09:33:29 +01004980 __init_extra_out(codec, spec->autocfg.hp_outs, spec->hp_paths);
Takashi Iwaidb23fd12012-12-20 15:27:24 +01004981 if (spec->autocfg.line_out_type != AUTO_PIN_SPEAKER_OUT)
4982 __init_extra_out(codec, spec->autocfg.speaker_outs,
Takashi Iwai2c12c302013-01-10 09:33:29 +01004983 spec->speaker_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004984}
4985
4986/* initialize multi-io paths */
4987static void init_multi_io(struct hda_codec *codec)
4988{
4989 struct hda_gen_spec *spec = codec->spec;
4990 int i;
4991
4992 for (i = 0; i < spec->multi_ios; i++) {
4993 hda_nid_t pin = spec->multi_io[i].pin;
4994 struct nid_path *path;
Takashi Iwai196c17662013-01-04 15:01:40 +01004995 path = get_multiio_path(codec, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004996 if (!path)
4997 continue;
4998 if (!spec->multi_io[i].ctl_in)
4999 spec->multi_io[i].ctl_in =
Takashi Iwai2c12c302013-01-10 09:33:29 +01005000 snd_hda_codec_get_pin_target(codec, pin);
Takashi Iwai65033cc2013-04-16 12:31:05 +02005001 snd_hda_activate_path(codec, path, path->active,
5002 aamix_default(spec));
Takashi Iwai352f7f92012-12-19 12:52:06 +01005003 }
5004}
5005
Takashi Iwai352f7f92012-12-19 12:52:06 +01005006/* set up input pins and loopback paths */
5007static void init_analog_input(struct hda_codec *codec)
5008{
5009 struct hda_gen_spec *spec = codec->spec;
5010 struct auto_pin_cfg *cfg = &spec->autocfg;
5011 int i;
5012
5013 for (i = 0; i < cfg->num_inputs; i++) {
5014 hda_nid_t nid = cfg->inputs[i].pin;
5015 if (is_input_pin(codec, nid))
Takashi Iwai2c12c302013-01-10 09:33:29 +01005016 restore_pin_ctl(codec, nid);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005017
5018 /* init loopback inputs */
5019 if (spec->mixer_nid) {
Takashi Iwai3e367f12013-01-23 17:07:23 +01005020 resume_path_from_idx(codec, spec->loopback_paths[i]);
5021 resume_path_from_idx(codec, spec->loopback_merge_path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005022 }
5023 }
5024}
5025
5026/* initialize ADC paths */
5027static void init_input_src(struct hda_codec *codec)
5028{
5029 struct hda_gen_spec *spec = codec->spec;
5030 struct hda_input_mux *imux = &spec->input_mux;
5031 struct nid_path *path;
5032 int i, c, nums;
5033
5034 if (spec->dyn_adc_switch)
5035 nums = 1;
5036 else
5037 nums = spec->num_adc_nids;
5038
5039 for (c = 0; c < nums; c++) {
5040 for (i = 0; i < imux->num_items; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01005041 path = get_input_path(codec, c, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005042 if (path) {
5043 bool active = path->active;
5044 if (i == spec->cur_mux[c])
5045 active = true;
5046 snd_hda_activate_path(codec, path, active, false);
5047 }
5048 }
Takashi Iwai967303d2013-02-19 17:12:42 +01005049 if (spec->hp_mic)
5050 update_hp_mic(codec, c, true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005051 }
5052
Takashi Iwai352f7f92012-12-19 12:52:06 +01005053 if (spec->cap_sync_hook)
Takashi Iwaia90229e2013-01-18 14:10:00 +01005054 spec->cap_sync_hook(codec, NULL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005055}
5056
5057/* set right pin controls for digital I/O */
5058static void init_digital(struct hda_codec *codec)
5059{
5060 struct hda_gen_spec *spec = codec->spec;
5061 int i;
5062 hda_nid_t pin;
5063
Takashi Iwaid4156932013-01-07 10:08:02 +01005064 for (i = 0; i < spec->autocfg.dig_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01005065 set_output_and_unmute(codec, spec->digout_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005066 pin = spec->autocfg.dig_in_pin;
Takashi Iwai2430d7b2013-01-04 15:09:42 +01005067 if (pin) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01005068 restore_pin_ctl(codec, pin);
Takashi Iwai3e367f12013-01-23 17:07:23 +01005069 resume_path_from_idx(codec, spec->digin_path);
Takashi Iwai2430d7b2013-01-04 15:09:42 +01005070 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01005071}
5072
Takashi Iwai973e4972012-12-20 15:16:09 +01005073/* clear unsol-event tags on unused pins; Conexant codecs seem to leave
5074 * invalid unsol tags by some reason
5075 */
5076static void clear_unsol_on_unused_pins(struct hda_codec *codec)
5077{
5078 int i;
5079
5080 for (i = 0; i < codec->init_pins.used; i++) {
5081 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
5082 hda_nid_t nid = pin->nid;
5083 if (is_jack_detectable(codec, nid) &&
5084 !snd_hda_jack_tbl_get(codec, nid))
5085 snd_hda_codec_update_cache(codec, nid, 0,
5086 AC_VERB_SET_UNSOLICITED_ENABLE, 0);
5087 }
5088}
5089
Takashi Iwai5187ac12013-01-07 12:52:16 +01005090/*
5091 * initialize the generic spec;
5092 * this can be put as patch_ops.init function
5093 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01005094int snd_hda_gen_init(struct hda_codec *codec)
5095{
5096 struct hda_gen_spec *spec = codec->spec;
5097
5098 if (spec->init_hook)
5099 spec->init_hook(codec);
5100
5101 snd_hda_apply_verbs(codec);
5102
Takashi Iwai3bbcd272012-12-20 11:50:58 +01005103 codec->cached_write = 1;
5104
Takashi Iwai352f7f92012-12-19 12:52:06 +01005105 init_multi_out(codec);
5106 init_extra_out(codec);
5107 init_multi_io(codec);
5108 init_analog_input(codec);
5109 init_input_src(codec);
5110 init_digital(codec);
5111
Takashi Iwai973e4972012-12-20 15:16:09 +01005112 clear_unsol_on_unused_pins(codec);
5113
Takashi Iwai352f7f92012-12-19 12:52:06 +01005114 /* call init functions of standard auto-mute helpers */
Takashi Iwaia5cc2502013-01-16 18:08:55 +01005115 update_automute_all(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005116
Takashi Iwaidc870f32013-01-22 15:24:30 +01005117 snd_hda_codec_flush_cache(codec);
Takashi Iwai3bbcd272012-12-20 11:50:58 +01005118
Takashi Iwai352f7f92012-12-19 12:52:06 +01005119 if (spec->vmaster_mute.sw_kctl && spec->vmaster_mute.hook)
5120 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
5121
5122 hda_call_check_power_status(codec, 0x01);
5123 return 0;
5124}
Takashi Iwaifce52a32013-01-07 12:42:48 +01005125EXPORT_SYMBOL_HDA(snd_hda_gen_init);
5126
Takashi Iwai5187ac12013-01-07 12:52:16 +01005127/*
5128 * free the generic spec;
5129 * this can be put as patch_ops.free function
5130 */
Takashi Iwaifce52a32013-01-07 12:42:48 +01005131void snd_hda_gen_free(struct hda_codec *codec)
5132{
Takashi Iwai7504b6c2013-03-18 11:25:51 +01005133 snd_hda_detach_beep_device(codec);
Takashi Iwaifce52a32013-01-07 12:42:48 +01005134 snd_hda_gen_spec_free(codec->spec);
5135 kfree(codec->spec);
5136 codec->spec = NULL;
5137}
5138EXPORT_SYMBOL_HDA(snd_hda_gen_free);
5139
5140#ifdef CONFIG_PM
Takashi Iwai5187ac12013-01-07 12:52:16 +01005141/*
5142 * check the loopback power save state;
5143 * this can be put as patch_ops.check_power_status function
5144 */
Takashi Iwaifce52a32013-01-07 12:42:48 +01005145int snd_hda_gen_check_power_status(struct hda_codec *codec, hda_nid_t nid)
5146{
5147 struct hda_gen_spec *spec = codec->spec;
5148 return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
5149}
5150EXPORT_SYMBOL_HDA(snd_hda_gen_check_power_status);
5151#endif
Takashi Iwai352f7f92012-12-19 12:52:06 +01005152
5153
5154/*
5155 * the generic codec support
5156 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005157
Takashi Iwai352f7f92012-12-19 12:52:06 +01005158static const struct hda_codec_ops generic_patch_ops = {
5159 .build_controls = snd_hda_gen_build_controls,
5160 .build_pcms = snd_hda_gen_build_pcms,
5161 .init = snd_hda_gen_init,
Takashi Iwaifce52a32013-01-07 12:42:48 +01005162 .free = snd_hda_gen_free,
Takashi Iwai352f7f92012-12-19 12:52:06 +01005163 .unsol_event = snd_hda_jack_unsol_event,
Takashi Iwai83012a72012-08-24 18:38:08 +02005164#ifdef CONFIG_PM
Takashi Iwaifce52a32013-01-07 12:42:48 +01005165 .check_power_status = snd_hda_gen_check_power_status,
Takashi Iwaicb53c622007-08-10 17:21:45 +02005166#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005167};
5168
Linus Torvalds1da177e2005-04-16 15:20:36 -07005169int snd_hda_parse_generic_codec(struct hda_codec *codec)
5170{
Takashi Iwai352f7f92012-12-19 12:52:06 +01005171 struct hda_gen_spec *spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005172 int err;
5173
Takashi Iwaie560d8d2005-09-09 14:21:46 +02005174 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005175 if (!spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005176 return -ENOMEM;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005177 snd_hda_gen_spec_init(spec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005178 codec->spec = spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005179
Takashi Iwai9eb413e2012-12-19 14:41:21 +01005180 err = snd_hda_parse_pin_defcfg(codec, &spec->autocfg, NULL, 0);
5181 if (err < 0)
5182 return err;
5183
5184 err = snd_hda_gen_parse_auto_config(codec, &spec->autocfg);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005185 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005186 goto error;
5187
5188 codec->patch_ops = generic_patch_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005189 return 0;
5190
Takashi Iwai352f7f92012-12-19 12:52:06 +01005191error:
Takashi Iwaifce52a32013-01-07 12:42:48 +01005192 snd_hda_gen_free(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005193 return err;
5194}
Takashi Iwaifce52a32013-01-07 12:42:48 +01005195EXPORT_SYMBOL_HDA(snd_hda_parse_generic_codec);