blob: 73de215da03f272b9cd734c2a3c5555bb0898908 [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"
37#include "hda_generic.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Takashi Iwai352f7f92012-12-19 12:52:06 +010040/* initialize hda_gen_spec struct */
41int snd_hda_gen_spec_init(struct hda_gen_spec *spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -070042{
Takashi Iwai352f7f92012-12-19 12:52:06 +010043 snd_array_init(&spec->kctls, sizeof(struct snd_kcontrol_new), 32);
Takashi Iwai352f7f92012-12-19 12:52:06 +010044 snd_array_init(&spec->paths, sizeof(struct nid_path), 8);
Takashi Iwai0186f4f2013-02-07 10:45:11 +010045 snd_array_init(&spec->loopback_list, sizeof(struct hda_amp_list), 8);
Takashi Iwai38cf6f12012-12-21 14:09:42 +010046 mutex_init(&spec->pcm_mutex);
Takashi Iwai352f7f92012-12-19 12:52:06 +010047 return 0;
48}
49EXPORT_SYMBOL_HDA(snd_hda_gen_spec_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
Takashi Iwai12c93df2012-12-19 14:38:33 +010051struct snd_kcontrol_new *
52snd_hda_gen_add_kctl(struct hda_gen_spec *spec, const char *name,
53 const struct snd_kcontrol_new *temp)
Takashi Iwai352f7f92012-12-19 12:52:06 +010054{
55 struct snd_kcontrol_new *knew = snd_array_new(&spec->kctls);
56 if (!knew)
57 return NULL;
58 *knew = *temp;
59 if (name)
60 knew->name = kstrdup(name, GFP_KERNEL);
61 else if (knew->name)
62 knew->name = kstrdup(knew->name, GFP_KERNEL);
63 if (!knew->name)
64 return NULL;
65 return knew;
66}
Takashi Iwai12c93df2012-12-19 14:38:33 +010067EXPORT_SYMBOL_HDA(snd_hda_gen_add_kctl);
Takashi Iwai352f7f92012-12-19 12:52:06 +010068
69static void free_kctls(struct hda_gen_spec *spec)
70{
71 if (spec->kctls.list) {
72 struct snd_kcontrol_new *kctl = spec->kctls.list;
73 int i;
74 for (i = 0; i < spec->kctls.used; i++)
75 kfree(kctl[i].name);
76 }
77 snd_array_free(&spec->kctls);
78}
79
Takashi Iwai352f7f92012-12-19 12:52:06 +010080void snd_hda_gen_spec_free(struct hda_gen_spec *spec)
81{
82 if (!spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 return;
Takashi Iwai352f7f92012-12-19 12:52:06 +010084 free_kctls(spec);
Takashi Iwai352f7f92012-12-19 12:52:06 +010085 snd_array_free(&spec->paths);
Takashi Iwai0186f4f2013-02-07 10:45:11 +010086 snd_array_free(&spec->loopback_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087}
Takashi Iwai352f7f92012-12-19 12:52:06 +010088EXPORT_SYMBOL_HDA(snd_hda_gen_spec_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
90/*
Takashi Iwai1c70a582013-01-11 17:48:22 +010091 * store user hints
92 */
93static void parse_user_hints(struct hda_codec *codec)
94{
95 struct hda_gen_spec *spec = codec->spec;
96 int val;
97
98 val = snd_hda_get_bool_hint(codec, "jack_detect");
99 if (val >= 0)
100 codec->no_jack_detect = !val;
101 val = snd_hda_get_bool_hint(codec, "inv_jack_detect");
102 if (val >= 0)
103 codec->inv_jack_detect = !!val;
104 val = snd_hda_get_bool_hint(codec, "trigger_sense");
105 if (val >= 0)
106 codec->no_trigger_sense = !val;
107 val = snd_hda_get_bool_hint(codec, "inv_eapd");
108 if (val >= 0)
109 codec->inv_eapd = !!val;
110 val = snd_hda_get_bool_hint(codec, "pcm_format_first");
111 if (val >= 0)
112 codec->pcm_format_first = !!val;
113 val = snd_hda_get_bool_hint(codec, "sticky_stream");
114 if (val >= 0)
115 codec->no_sticky_stream = !val;
116 val = snd_hda_get_bool_hint(codec, "spdif_status_reset");
117 if (val >= 0)
118 codec->spdif_status_reset = !!val;
119 val = snd_hda_get_bool_hint(codec, "pin_amp_workaround");
120 if (val >= 0)
121 codec->pin_amp_workaround = !!val;
122 val = snd_hda_get_bool_hint(codec, "single_adc_amp");
123 if (val >= 0)
124 codec->single_adc_amp = !!val;
125
Takashi Iwaif72706b2013-01-16 18:20:07 +0100126 val = snd_hda_get_bool_hint(codec, "auto_mute");
127 if (val >= 0)
128 spec->suppress_auto_mute = !val;
Takashi Iwai1c70a582013-01-11 17:48:22 +0100129 val = snd_hda_get_bool_hint(codec, "auto_mic");
130 if (val >= 0)
131 spec->suppress_auto_mic = !val;
132 val = snd_hda_get_bool_hint(codec, "line_in_auto_switch");
133 if (val >= 0)
134 spec->line_in_auto_switch = !!val;
135 val = snd_hda_get_bool_hint(codec, "need_dac_fix");
136 if (val >= 0)
137 spec->need_dac_fix = !!val;
138 val = snd_hda_get_bool_hint(codec, "primary_hp");
139 if (val >= 0)
140 spec->no_primary_hp = !val;
141 val = snd_hda_get_bool_hint(codec, "multi_cap_vol");
142 if (val >= 0)
143 spec->multi_cap_vol = !!val;
144 val = snd_hda_get_bool_hint(codec, "inv_dmic_split");
145 if (val >= 0)
146 spec->inv_dmic_split = !!val;
147 val = snd_hda_get_bool_hint(codec, "indep_hp");
148 if (val >= 0)
149 spec->indep_hp = !!val;
150 val = snd_hda_get_bool_hint(codec, "add_stereo_mix_input");
151 if (val >= 0)
152 spec->add_stereo_mix_input = !!val;
153 val = snd_hda_get_bool_hint(codec, "add_out_jack_modes");
154 if (val >= 0)
155 spec->add_out_jack_modes = !!val;
Takashi Iwai294765582013-01-17 09:52:11 +0100156 val = snd_hda_get_bool_hint(codec, "add_in_jack_modes");
157 if (val >= 0)
158 spec->add_in_jack_modes = !!val;
Takashi Iwai55196ff2013-01-24 17:32:56 +0100159 val = snd_hda_get_bool_hint(codec, "power_down_unused");
160 if (val >= 0)
161 spec->power_down_unused = !!val;
Takashi Iwai967303d2013-02-19 17:12:42 +0100162 val = snd_hda_get_bool_hint(codec, "add_hp_mic");
163 if (val >= 0)
164 spec->hp_mic = !!val;
165 val = snd_hda_get_bool_hint(codec, "hp_mic_detect");
166 if (val >= 0)
167 spec->suppress_hp_mic_detect = !val;
Takashi Iwai1c70a582013-01-11 17:48:22 +0100168
169 if (!snd_hda_get_int_hint(codec, "mixer_nid", &val))
170 spec->mixer_nid = val;
171}
172
173/*
Takashi Iwai2c12c302013-01-10 09:33:29 +0100174 * pin control value accesses
175 */
176
177#define update_pin_ctl(codec, pin, val) \
178 snd_hda_codec_update_cache(codec, pin, 0, \
179 AC_VERB_SET_PIN_WIDGET_CONTROL, val)
180
181/* restore the pinctl based on the cached value */
182static inline void restore_pin_ctl(struct hda_codec *codec, hda_nid_t pin)
183{
184 update_pin_ctl(codec, pin, snd_hda_codec_get_pin_target(codec, pin));
185}
186
187/* set the pinctl target value and write it if requested */
188static void set_pin_target(struct hda_codec *codec, hda_nid_t pin,
189 unsigned int val, bool do_write)
190{
191 if (!pin)
192 return;
193 val = snd_hda_correct_pin_ctl(codec, pin, val);
194 snd_hda_codec_set_pin_target(codec, pin, val);
195 if (do_write)
196 update_pin_ctl(codec, pin, val);
197}
198
199/* set pinctl target values for all given pins */
200static void set_pin_targets(struct hda_codec *codec, int num_pins,
201 hda_nid_t *pins, unsigned int val)
202{
203 int i;
204 for (i = 0; i < num_pins; i++)
205 set_pin_target(codec, pins[i], val, false);
206}
207
208/*
Takashi Iwai352f7f92012-12-19 12:52:06 +0100209 * parsing paths
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100212/* return the position of NID in the list, or -1 if not found */
213static int find_idx_in_nid_list(hda_nid_t nid, const hda_nid_t *list, int nums)
214{
215 int i;
216 for (i = 0; i < nums; i++)
217 if (list[i] == nid)
218 return i;
219 return -1;
220}
221
222/* return true if the given NID is contained in the path */
223static bool is_nid_contained(struct nid_path *path, hda_nid_t nid)
224{
225 return find_idx_in_nid_list(nid, path->path, path->depth) >= 0;
226}
227
Takashi Iwaif5172a72013-01-04 13:19:55 +0100228static struct nid_path *get_nid_path(struct hda_codec *codec,
229 hda_nid_t from_nid, hda_nid_t to_nid,
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100230 int anchor_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100232 struct hda_gen_spec *spec = codec->spec;
233 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
Takashi Iwai352f7f92012-12-19 12:52:06 +0100235 for (i = 0; i < spec->paths.used; i++) {
236 struct nid_path *path = snd_array_elem(&spec->paths, i);
237 if (path->depth <= 0)
238 continue;
239 if ((!from_nid || path->path[0] == from_nid) &&
Takashi Iwaif5172a72013-01-04 13:19:55 +0100240 (!to_nid || path->path[path->depth - 1] == to_nid)) {
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100241 if (!anchor_nid ||
242 (anchor_nid > 0 && is_nid_contained(path, anchor_nid)) ||
243 (anchor_nid < 0 && !is_nid_contained(path, anchor_nid)))
Takashi Iwaif5172a72013-01-04 13:19:55 +0100244 return path;
245 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 }
247 return NULL;
248}
Takashi Iwaif5172a72013-01-04 13:19:55 +0100249
250/* get the path between the given NIDs;
251 * passing 0 to either @pin or @dac behaves as a wildcard
252 */
253struct nid_path *snd_hda_get_nid_path(struct hda_codec *codec,
254 hda_nid_t from_nid, hda_nid_t to_nid)
255{
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100256 return get_nid_path(codec, from_nid, to_nid, 0);
Takashi Iwaif5172a72013-01-04 13:19:55 +0100257}
Takashi Iwai352f7f92012-12-19 12:52:06 +0100258EXPORT_SYMBOL_HDA(snd_hda_get_nid_path);
259
Takashi Iwai196c17662013-01-04 15:01:40 +0100260/* get the index number corresponding to the path instance;
261 * the index starts from 1, for easier checking the invalid value
262 */
263int snd_hda_get_path_idx(struct hda_codec *codec, struct nid_path *path)
264{
265 struct hda_gen_spec *spec = codec->spec;
266 struct nid_path *array = spec->paths.list;
267 ssize_t idx;
268
269 if (!spec->paths.used)
270 return 0;
271 idx = path - array;
272 if (idx < 0 || idx >= spec->paths.used)
273 return 0;
274 return idx + 1;
275}
Takashi Iwai4bd01e92013-01-22 15:17:20 +0100276EXPORT_SYMBOL_HDA(snd_hda_get_path_idx);
Takashi Iwai196c17662013-01-04 15:01:40 +0100277
278/* get the path instance corresponding to the given index number */
279struct nid_path *snd_hda_get_path_from_idx(struct hda_codec *codec, int idx)
280{
281 struct hda_gen_spec *spec = codec->spec;
282
283 if (idx <= 0 || idx > spec->paths.used)
284 return NULL;
285 return snd_array_elem(&spec->paths, idx - 1);
286}
Takashi Iwai4bd01e92013-01-22 15:17:20 +0100287EXPORT_SYMBOL_HDA(snd_hda_get_path_from_idx);
Takashi Iwai196c17662013-01-04 15:01:40 +0100288
Takashi Iwai352f7f92012-12-19 12:52:06 +0100289/* check whether the given DAC is already found in any existing paths */
290static bool is_dac_already_used(struct hda_codec *codec, hda_nid_t nid)
291{
292 struct hda_gen_spec *spec = codec->spec;
293 int i;
294
295 for (i = 0; i < spec->paths.used; i++) {
296 struct nid_path *path = snd_array_elem(&spec->paths, i);
297 if (path->path[0] == nid)
298 return true;
299 }
300 return false;
301}
302
303/* check whether the given two widgets can be connected */
304static bool is_reachable_path(struct hda_codec *codec,
305 hda_nid_t from_nid, hda_nid_t to_nid)
306{
307 if (!from_nid || !to_nid)
308 return false;
309 return snd_hda_get_conn_index(codec, to_nid, from_nid, true) >= 0;
310}
311
312/* nid, dir and idx */
313#define AMP_VAL_COMPARE_MASK (0xffff | (1U << 18) | (0x0f << 19))
314
315/* check whether the given ctl is already assigned in any path elements */
316static bool is_ctl_used(struct hda_codec *codec, unsigned int val, int type)
317{
318 struct hda_gen_spec *spec = codec->spec;
319 int i;
320
321 val &= AMP_VAL_COMPARE_MASK;
322 for (i = 0; i < spec->paths.used; i++) {
323 struct nid_path *path = snd_array_elem(&spec->paths, i);
324 if ((path->ctls[type] & AMP_VAL_COMPARE_MASK) == val)
325 return true;
326 }
327 return false;
328}
329
330/* check whether a control with the given (nid, dir, idx) was assigned */
331static bool is_ctl_associated(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwai8999bf02013-01-18 11:01:33 +0100332 int dir, int idx, int type)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100333{
334 unsigned int val = HDA_COMPOSE_AMP_VAL(nid, 3, idx, dir);
Takashi Iwai8999bf02013-01-18 11:01:33 +0100335 return is_ctl_used(codec, val, type);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100336}
337
Takashi Iwai0c8c0f52012-12-20 17:54:22 +0100338static void print_nid_path(const char *pfx, struct nid_path *path)
339{
340 char buf[40];
341 int i;
342
343
344 buf[0] = 0;
345 for (i = 0; i < path->depth; i++) {
346 char tmp[4];
347 sprintf(tmp, ":%02x", path->path[i]);
348 strlcat(buf, tmp, sizeof(buf));
349 }
350 snd_printdd("%s path: depth=%d %s\n", pfx, path->depth, buf);
351}
352
Takashi Iwai352f7f92012-12-19 12:52:06 +0100353/* called recursively */
354static bool __parse_nid_path(struct hda_codec *codec,
355 hda_nid_t from_nid, hda_nid_t to_nid,
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100356 int anchor_nid, struct nid_path *path,
357 int depth)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100358{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100359 const hda_nid_t *conn;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100360 int i, nums;
361
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100362 if (to_nid == anchor_nid)
363 anchor_nid = 0; /* anchor passed */
364 else if (to_nid == (hda_nid_t)(-anchor_nid))
365 return false; /* hit the exclusive nid */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100366
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100367 nums = snd_hda_get_conn_list(codec, to_nid, &conn);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100368 for (i = 0; i < nums; i++) {
369 if (conn[i] != from_nid) {
370 /* special case: when from_nid is 0,
371 * try to find an empty DAC
372 */
373 if (from_nid ||
374 get_wcaps_type(get_wcaps(codec, conn[i])) != AC_WID_AUD_OUT ||
375 is_dac_already_used(codec, conn[i]))
376 continue;
377 }
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100378 /* anchor is not requested or already passed? */
379 if (anchor_nid <= 0)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100380 goto found;
381 }
382 if (depth >= MAX_NID_PATH_DEPTH)
383 return false;
384 for (i = 0; i < nums; i++) {
385 unsigned int type;
386 type = get_wcaps_type(get_wcaps(codec, conn[i]));
387 if (type == AC_WID_AUD_OUT || type == AC_WID_AUD_IN ||
388 type == AC_WID_PIN)
389 continue;
390 if (__parse_nid_path(codec, from_nid, conn[i],
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100391 anchor_nid, path, depth + 1))
Takashi Iwai352f7f92012-12-19 12:52:06 +0100392 goto found;
393 }
394 return false;
395
396 found:
397 path->path[path->depth] = conn[i];
398 path->idx[path->depth + 1] = i;
399 if (nums > 1 && get_wcaps_type(get_wcaps(codec, to_nid)) != AC_WID_AUD_MIX)
400 path->multi[path->depth + 1] = 1;
401 path->depth++;
402 return true;
403}
404
405/* parse the widget path from the given nid to the target nid;
406 * when @from_nid is 0, try to find an empty DAC;
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100407 * when @anchor_nid is set to a positive value, only paths through the widget
408 * with the given value are evaluated.
409 * when @anchor_nid is set to a negative value, paths through the widget
410 * with the negative of given value are excluded, only other paths are chosen.
411 * when @anchor_nid is zero, no special handling about path selection.
Takashi Iwai352f7f92012-12-19 12:52:06 +0100412 */
413bool snd_hda_parse_nid_path(struct hda_codec *codec, hda_nid_t from_nid,
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100414 hda_nid_t to_nid, int anchor_nid,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100415 struct nid_path *path)
416{
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100417 if (__parse_nid_path(codec, from_nid, to_nid, anchor_nid, path, 1)) {
Takashi Iwai352f7f92012-12-19 12:52:06 +0100418 path->path[path->depth] = to_nid;
419 path->depth++;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100420 return true;
421 }
422 return false;
423}
424EXPORT_SYMBOL_HDA(snd_hda_parse_nid_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
426/*
Takashi Iwai352f7f92012-12-19 12:52:06 +0100427 * parse the path between the given NIDs and add to the path list.
428 * if no valid path is found, return NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100430struct nid_path *
431snd_hda_add_new_path(struct hda_codec *codec, hda_nid_t from_nid,
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100432 hda_nid_t to_nid, int anchor_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100434 struct hda_gen_spec *spec = codec->spec;
435 struct nid_path *path;
436
437 if (from_nid && to_nid && !is_reachable_path(codec, from_nid, to_nid))
438 return NULL;
439
Takashi Iwaif5172a72013-01-04 13:19:55 +0100440 /* check whether the path has been already added */
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100441 path = get_nid_path(codec, from_nid, to_nid, anchor_nid);
Takashi Iwaif5172a72013-01-04 13:19:55 +0100442 if (path)
443 return path;
444
Takashi Iwai352f7f92012-12-19 12:52:06 +0100445 path = snd_array_new(&spec->paths);
446 if (!path)
447 return NULL;
448 memset(path, 0, sizeof(*path));
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100449 if (snd_hda_parse_nid_path(codec, from_nid, to_nid, anchor_nid, path))
Takashi Iwai352f7f92012-12-19 12:52:06 +0100450 return path;
451 /* push back */
452 spec->paths.used--;
453 return NULL;
454}
455EXPORT_SYMBOL_HDA(snd_hda_add_new_path);
456
Takashi Iwai980428c2013-01-09 09:28:20 +0100457/* clear the given path as invalid so that it won't be picked up later */
458static void invalidate_nid_path(struct hda_codec *codec, int idx)
459{
460 struct nid_path *path = snd_hda_get_path_from_idx(codec, idx);
461 if (!path)
462 return;
463 memset(path, 0, sizeof(*path));
464}
465
Takashi Iwai352f7f92012-12-19 12:52:06 +0100466/* look for an empty DAC slot */
467static hda_nid_t look_for_dac(struct hda_codec *codec, hda_nid_t pin,
468 bool is_digital)
469{
470 struct hda_gen_spec *spec = codec->spec;
471 bool cap_digital;
472 int i;
473
474 for (i = 0; i < spec->num_all_dacs; i++) {
475 hda_nid_t nid = spec->all_dacs[i];
476 if (!nid || is_dac_already_used(codec, nid))
477 continue;
478 cap_digital = !!(get_wcaps(codec, nid) & AC_WCAP_DIGITAL);
479 if (is_digital != cap_digital)
480 continue;
481 if (is_reachable_path(codec, nid, pin))
482 return nid;
483 }
484 return 0;
485}
486
487/* replace the channels in the composed amp value with the given number */
488static unsigned int amp_val_replace_channels(unsigned int val, unsigned int chs)
489{
490 val &= ~(0x3U << 16);
491 val |= chs << 16;
492 return val;
493}
494
495/* check whether the widget has the given amp capability for the direction */
496static bool check_amp_caps(struct hda_codec *codec, hda_nid_t nid,
497 int dir, unsigned int bits)
498{
499 if (!nid)
500 return false;
501 if (get_wcaps(codec, nid) & (1 << (dir + 1)))
502 if (query_amp_caps(codec, nid, dir) & bits)
503 return true;
504 return false;
505}
506
David Henningsson99a55922013-01-16 15:58:44 +0100507static bool same_amp_caps(struct hda_codec *codec, hda_nid_t nid1,
508 hda_nid_t nid2, int dir)
509{
510 if (!(get_wcaps(codec, nid1) & (1 << (dir + 1))))
511 return !(get_wcaps(codec, nid2) & (1 << (dir + 1)));
512 return (query_amp_caps(codec, nid1, dir) ==
513 query_amp_caps(codec, nid2, dir));
514}
515
Takashi Iwai352f7f92012-12-19 12:52:06 +0100516#define nid_has_mute(codec, nid, dir) \
517 check_amp_caps(codec, nid, dir, AC_AMPCAP_MUTE)
518#define nid_has_volume(codec, nid, dir) \
519 check_amp_caps(codec, nid, dir, AC_AMPCAP_NUM_STEPS)
520
521/* look for a widget suitable for assigning a mute switch in the path */
522static hda_nid_t look_for_out_mute_nid(struct hda_codec *codec,
523 struct nid_path *path)
524{
525 int i;
526
527 for (i = path->depth - 1; i >= 0; i--) {
528 if (nid_has_mute(codec, path->path[i], HDA_OUTPUT))
529 return path->path[i];
530 if (i != path->depth - 1 && i != 0 &&
531 nid_has_mute(codec, path->path[i], HDA_INPUT))
532 return path->path[i];
533 }
534 return 0;
535}
536
537/* look for a widget suitable for assigning a volume ctl in the path */
538static hda_nid_t look_for_out_vol_nid(struct hda_codec *codec,
539 struct nid_path *path)
540{
541 int i;
542
543 for (i = path->depth - 1; i >= 0; i--) {
544 if (nid_has_volume(codec, path->path[i], HDA_OUTPUT))
545 return path->path[i];
546 }
Takashi Iwai82beb8f2007-08-10 17:09:26 +0200547 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548}
549
550/*
Takashi Iwai352f7f92012-12-19 12:52:06 +0100551 * path activation / deactivation
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100553
554/* can have the amp-in capability? */
555static bool has_amp_in(struct hda_codec *codec, struct nid_path *path, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100557 hda_nid_t nid = path->path[idx];
558 unsigned int caps = get_wcaps(codec, nid);
559 unsigned int type = get_wcaps_type(caps);
560
561 if (!(caps & AC_WCAP_IN_AMP))
562 return false;
563 if (type == AC_WID_PIN && idx > 0) /* only for input pins */
564 return false;
565 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566}
567
Takashi Iwai352f7f92012-12-19 12:52:06 +0100568/* can have the amp-out capability? */
569static bool has_amp_out(struct hda_codec *codec, struct nid_path *path, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100571 hda_nid_t nid = path->path[idx];
572 unsigned int caps = get_wcaps(codec, nid);
573 unsigned int type = get_wcaps_type(caps);
574
575 if (!(caps & AC_WCAP_OUT_AMP))
576 return false;
577 if (type == AC_WID_PIN && !idx) /* only for output pins */
578 return false;
579 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580}
581
Takashi Iwai352f7f92012-12-19 12:52:06 +0100582/* check whether the given (nid,dir,idx) is active */
583static bool is_active_nid(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwai7dddf2ae2013-01-24 16:31:35 +0100584 unsigned int dir, unsigned int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100586 struct hda_gen_spec *spec = codec->spec;
587 int i, n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
Takashi Iwai352f7f92012-12-19 12:52:06 +0100589 for (n = 0; n < spec->paths.used; n++) {
590 struct nid_path *path = snd_array_elem(&spec->paths, n);
591 if (!path->active)
592 continue;
593 for (i = 0; i < path->depth; i++) {
594 if (path->path[i] == nid) {
595 if (dir == HDA_OUTPUT || path->idx[i] == idx)
596 return true;
597 break;
598 }
599 }
600 }
601 return false;
602}
603
604/* get the default amp value for the target state */
605static int get_amp_val_to_activate(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwai8999bf02013-01-18 11:01:33 +0100606 int dir, unsigned int caps, bool enable)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100607{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100608 unsigned int val = 0;
609
Takashi Iwai352f7f92012-12-19 12:52:06 +0100610 if (caps & AC_AMPCAP_NUM_STEPS) {
611 /* set to 0dB */
612 if (enable)
613 val = (caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT;
614 }
615 if (caps & AC_AMPCAP_MUTE) {
616 if (!enable)
617 val |= HDA_AMP_MUTE;
618 }
619 return val;
620}
621
622/* initialize the amp value (only at the first time) */
623static void init_amp(struct hda_codec *codec, hda_nid_t nid, int dir, int idx)
624{
Takashi Iwai8999bf02013-01-18 11:01:33 +0100625 unsigned int caps = query_amp_caps(codec, nid, dir);
626 int val = get_amp_val_to_activate(codec, nid, dir, caps, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100627 snd_hda_codec_amp_init_stereo(codec, nid, dir, idx, 0xff, val);
628}
629
Takashi Iwai8999bf02013-01-18 11:01:33 +0100630/* calculate amp value mask we can modify;
631 * if the given amp is controlled by mixers, don't touch it
632 */
633static unsigned int get_amp_mask_to_modify(struct hda_codec *codec,
634 hda_nid_t nid, int dir, int idx,
635 unsigned int caps)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100636{
Takashi Iwai8999bf02013-01-18 11:01:33 +0100637 unsigned int mask = 0xff;
638
639 if (caps & AC_AMPCAP_MUTE) {
640 if (is_ctl_associated(codec, nid, dir, idx, NID_PATH_MUTE_CTL))
641 mask &= ~0x80;
642 }
643 if (caps & AC_AMPCAP_NUM_STEPS) {
644 if (is_ctl_associated(codec, nid, dir, idx, NID_PATH_VOL_CTL) ||
645 is_ctl_associated(codec, nid, dir, idx, NID_PATH_BOOST_CTL))
646 mask &= ~0x7f;
647 }
648 return mask;
649}
650
651static void activate_amp(struct hda_codec *codec, hda_nid_t nid, int dir,
652 int idx, int idx_to_check, bool enable)
653{
654 unsigned int caps;
655 unsigned int mask, val;
656
Takashi Iwai7dddf2ae2013-01-24 16:31:35 +0100657 if (!enable && is_active_nid(codec, nid, dir, idx_to_check))
Takashi Iwai352f7f92012-12-19 12:52:06 +0100658 return;
Takashi Iwai8999bf02013-01-18 11:01:33 +0100659
660 caps = query_amp_caps(codec, nid, dir);
661 val = get_amp_val_to_activate(codec, nid, dir, caps, enable);
662 mask = get_amp_mask_to_modify(codec, nid, dir, idx_to_check, caps);
663 if (!mask)
664 return;
665
666 val &= mask;
667 snd_hda_codec_amp_stereo(codec, nid, dir, idx, mask, val);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100668}
669
670static void activate_amp_out(struct hda_codec *codec, struct nid_path *path,
671 int i, bool enable)
672{
673 hda_nid_t nid = path->path[i];
674 init_amp(codec, nid, HDA_OUTPUT, 0);
Takashi Iwai8999bf02013-01-18 11:01:33 +0100675 activate_amp(codec, nid, HDA_OUTPUT, 0, 0, enable);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100676}
677
678static void activate_amp_in(struct hda_codec *codec, struct nid_path *path,
679 int i, bool enable, bool add_aamix)
680{
681 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100682 const hda_nid_t *conn;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100683 int n, nums, idx;
684 int type;
685 hda_nid_t nid = path->path[i];
686
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100687 nums = snd_hda_get_conn_list(codec, nid, &conn);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100688 type = get_wcaps_type(get_wcaps(codec, nid));
689 if (type == AC_WID_PIN ||
690 (type == AC_WID_AUD_IN && codec->single_adc_amp)) {
691 nums = 1;
692 idx = 0;
693 } else
694 idx = path->idx[i];
695
696 for (n = 0; n < nums; n++)
697 init_amp(codec, nid, HDA_INPUT, n);
698
Takashi Iwai352f7f92012-12-19 12:52:06 +0100699 /* here is a little bit tricky in comparison with activate_amp_out();
700 * when aa-mixer is available, we need to enable the path as well
701 */
702 for (n = 0; n < nums; n++) {
Takashi Iwaie4a395e2013-01-23 17:00:31 +0100703 if (n != idx && (!add_aamix || conn[n] != spec->mixer_merge_nid))
Takashi Iwai352f7f92012-12-19 12:52:06 +0100704 continue;
Takashi Iwai8999bf02013-01-18 11:01:33 +0100705 activate_amp(codec, nid, HDA_INPUT, n, idx, enable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 }
707}
708
Takashi Iwai352f7f92012-12-19 12:52:06 +0100709/* activate or deactivate the given path
710 * if @add_aamix is set, enable the input from aa-mix NID as well (if any)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100712void snd_hda_activate_path(struct hda_codec *codec, struct nid_path *path,
713 bool enable, bool add_aamix)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714{
Takashi Iwai55196ff2013-01-24 17:32:56 +0100715 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100716 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
Takashi Iwai352f7f92012-12-19 12:52:06 +0100718 if (!enable)
719 path->active = false;
720
721 for (i = path->depth - 1; i >= 0; i--) {
Takashi Iwai55196ff2013-01-24 17:32:56 +0100722 hda_nid_t nid = path->path[i];
723 if (enable && spec->power_down_unused) {
724 /* make sure the widget is powered up */
725 if (!snd_hda_check_power_state(codec, nid, AC_PWRST_D0))
726 snd_hda_codec_write(codec, nid, 0,
727 AC_VERB_SET_POWER_STATE,
728 AC_PWRST_D0);
729 }
Takashi Iwai352f7f92012-12-19 12:52:06 +0100730 if (enable && path->multi[i])
Takashi Iwai55196ff2013-01-24 17:32:56 +0100731 snd_hda_codec_write_cache(codec, nid, 0,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100732 AC_VERB_SET_CONNECT_SEL,
733 path->idx[i]);
734 if (has_amp_in(codec, path, i))
735 activate_amp_in(codec, path, i, enable, add_aamix);
736 if (has_amp_out(codec, path, i))
737 activate_amp_out(codec, path, i, enable);
738 }
739
740 if (enable)
741 path->active = true;
742}
743EXPORT_SYMBOL_HDA(snd_hda_activate_path);
744
Takashi Iwai55196ff2013-01-24 17:32:56 +0100745/* if the given path is inactive, put widgets into D3 (only if suitable) */
746static void path_power_down_sync(struct hda_codec *codec, struct nid_path *path)
747{
748 struct hda_gen_spec *spec = codec->spec;
749 bool changed;
750 int i;
751
752 if (!spec->power_down_unused || path->active)
753 return;
754
755 for (i = 0; i < path->depth; i++) {
756 hda_nid_t nid = path->path[i];
757 if (!snd_hda_check_power_state(codec, nid, AC_PWRST_D3)) {
758 snd_hda_codec_write(codec, nid, 0,
759 AC_VERB_SET_POWER_STATE,
760 AC_PWRST_D3);
761 changed = true;
762 }
763 }
764
765 if (changed) {
766 msleep(10);
767 snd_hda_codec_read(codec, path->path[0], 0,
768 AC_VERB_GET_POWER_STATE, 0);
769 }
770}
771
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +0100772/* turn on/off EAPD on the given pin */
773static void set_pin_eapd(struct hda_codec *codec, hda_nid_t pin, bool enable)
774{
775 struct hda_gen_spec *spec = codec->spec;
776 if (spec->own_eapd_ctl ||
777 !(snd_hda_query_pin_caps(codec, pin) & AC_PINCAP_EAPD))
778 return;
Takashi Iwaiecac3ed2012-12-21 15:23:01 +0100779 if (codec->inv_eapd)
780 enable = !enable;
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +0100781 snd_hda_codec_update_cache(codec, pin, 0,
782 AC_VERB_SET_EAPD_BTLENABLE,
783 enable ? 0x02 : 0x00);
784}
785
Takashi Iwai3e367f12013-01-23 17:07:23 +0100786/* re-initialize the path specified by the given path index */
787static void resume_path_from_idx(struct hda_codec *codec, int path_idx)
788{
789 struct nid_path *path = snd_hda_get_path_from_idx(codec, path_idx);
790 if (path)
791 snd_hda_activate_path(codec, path, path->active, false);
792}
793
Takashi Iwai352f7f92012-12-19 12:52:06 +0100794
795/*
796 * Helper functions for creating mixer ctl elements
797 */
798
799enum {
800 HDA_CTL_WIDGET_VOL,
801 HDA_CTL_WIDGET_MUTE,
802 HDA_CTL_BIND_MUTE,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100803};
804static const struct snd_kcontrol_new control_templates[] = {
805 HDA_CODEC_VOLUME(NULL, 0, 0, 0),
806 HDA_CODEC_MUTE(NULL, 0, 0, 0),
807 HDA_BIND_MUTE(NULL, 0, 0, 0),
Takashi Iwai352f7f92012-12-19 12:52:06 +0100808};
809
810/* add dynamic controls from template */
Takashi Iwaia35bd1e2013-01-18 14:01:14 +0100811static struct snd_kcontrol_new *
812add_control(struct hda_gen_spec *spec, int type, const char *name,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100813 int cidx, unsigned long val)
814{
815 struct snd_kcontrol_new *knew;
816
Takashi Iwai12c93df2012-12-19 14:38:33 +0100817 knew = snd_hda_gen_add_kctl(spec, name, &control_templates[type]);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100818 if (!knew)
Takashi Iwaia35bd1e2013-01-18 14:01:14 +0100819 return NULL;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100820 knew->index = cidx;
821 if (get_amp_nid_(val))
822 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
823 knew->private_value = val;
Takashi Iwaia35bd1e2013-01-18 14:01:14 +0100824 return knew;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100825}
826
827static int add_control_with_pfx(struct hda_gen_spec *spec, int type,
828 const char *pfx, const char *dir,
829 const char *sfx, int cidx, unsigned long val)
830{
831 char name[32];
832 snprintf(name, sizeof(name), "%s %s %s", pfx, dir, sfx);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +0100833 if (!add_control(spec, type, name, cidx, val))
834 return -ENOMEM;
835 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100836}
837
838#define add_pb_vol_ctrl(spec, type, pfx, val) \
839 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", 0, val)
840#define add_pb_sw_ctrl(spec, type, pfx, val) \
841 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", 0, val)
842#define __add_pb_vol_ctrl(spec, type, pfx, cidx, val) \
843 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", cidx, val)
844#define __add_pb_sw_ctrl(spec, type, pfx, cidx, val) \
845 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", cidx, val)
846
847static int add_vol_ctl(struct hda_codec *codec, const char *pfx, int cidx,
848 unsigned int chs, struct nid_path *path)
849{
850 unsigned int val;
851 if (!path)
852 return 0;
853 val = path->ctls[NID_PATH_VOL_CTL];
854 if (!val)
855 return 0;
856 val = amp_val_replace_channels(val, chs);
857 return __add_pb_vol_ctrl(codec->spec, HDA_CTL_WIDGET_VOL, pfx, cidx, val);
858}
859
860/* return the channel bits suitable for the given path->ctls[] */
861static int get_default_ch_nums(struct hda_codec *codec, struct nid_path *path,
862 int type)
863{
864 int chs = 1; /* mono (left only) */
865 if (path) {
866 hda_nid_t nid = get_amp_nid_(path->ctls[type]);
867 if (nid && (get_wcaps(codec, nid) & AC_WCAP_STEREO))
868 chs = 3; /* stereo */
869 }
870 return chs;
871}
872
873static int add_stereo_vol(struct hda_codec *codec, const char *pfx, int cidx,
874 struct nid_path *path)
875{
876 int chs = get_default_ch_nums(codec, path, NID_PATH_VOL_CTL);
877 return add_vol_ctl(codec, pfx, cidx, chs, path);
878}
879
880/* create a mute-switch for the given mixer widget;
881 * if it has multiple sources (e.g. DAC and loopback), create a bind-mute
882 */
883static int add_sw_ctl(struct hda_codec *codec, const char *pfx, int cidx,
884 unsigned int chs, struct nid_path *path)
885{
886 unsigned int val;
887 int type = HDA_CTL_WIDGET_MUTE;
888
889 if (!path)
890 return 0;
891 val = path->ctls[NID_PATH_MUTE_CTL];
892 if (!val)
893 return 0;
894 val = amp_val_replace_channels(val, chs);
895 if (get_amp_direction_(val) == HDA_INPUT) {
896 hda_nid_t nid = get_amp_nid_(val);
897 int nums = snd_hda_get_num_conns(codec, nid);
898 if (nums > 1) {
899 type = HDA_CTL_BIND_MUTE;
900 val |= nums << 19;
901 }
902 }
903 return __add_pb_sw_ctrl(codec->spec, type, pfx, cidx, val);
904}
905
906static int add_stereo_sw(struct hda_codec *codec, const char *pfx,
907 int cidx, struct nid_path *path)
908{
909 int chs = get_default_ch_nums(codec, path, NID_PATH_MUTE_CTL);
910 return add_sw_ctl(codec, pfx, cidx, chs, path);
911}
912
Takashi Iwai247d85e2013-01-17 16:18:11 +0100913/* any ctl assigned to the path with the given index? */
914static bool path_has_mixer(struct hda_codec *codec, int path_idx, int ctl_type)
915{
916 struct nid_path *path = snd_hda_get_path_from_idx(codec, path_idx);
917 return path && path->ctls[ctl_type];
918}
919
Takashi Iwai352f7f92012-12-19 12:52:06 +0100920static const char * const channel_name[4] = {
921 "Front", "Surround", "CLFE", "Side"
922};
923
924/* give some appropriate ctl name prefix for the given line out channel */
Takashi Iwai247d85e2013-01-17 16:18:11 +0100925static const char *get_line_out_pfx(struct hda_codec *codec, int ch,
926 int *index, int ctl_type)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100927{
Takashi Iwai247d85e2013-01-17 16:18:11 +0100928 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100929 struct auto_pin_cfg *cfg = &spec->autocfg;
930
931 *index = 0;
932 if (cfg->line_outs == 1 && !spec->multi_ios &&
Takashi Iwai247d85e2013-01-17 16:18:11 +0100933 !cfg->hp_outs && !cfg->speaker_outs)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100934 return spec->vmaster_mute.hook ? "PCM" : "Master";
935
936 /* if there is really a single DAC used in the whole output paths,
937 * use it master (or "PCM" if a vmaster hook is present)
938 */
939 if (spec->multiout.num_dacs == 1 && !spec->mixer_nid &&
940 !spec->multiout.hp_out_nid[0] && !spec->multiout.extra_out_nid[0])
941 return spec->vmaster_mute.hook ? "PCM" : "Master";
942
Takashi Iwai247d85e2013-01-17 16:18:11 +0100943 /* multi-io channels */
944 if (ch >= cfg->line_outs)
945 return channel_name[ch];
946
Takashi Iwai352f7f92012-12-19 12:52:06 +0100947 switch (cfg->line_out_type) {
948 case AUTO_PIN_SPEAKER_OUT:
Takashi Iwai247d85e2013-01-17 16:18:11 +0100949 /* if the primary channel vol/mute is shared with HP volume,
950 * don't name it as Speaker
951 */
952 if (!ch && cfg->hp_outs &&
953 !path_has_mixer(codec, spec->hp_paths[0], ctl_type))
954 break;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100955 if (cfg->line_outs == 1)
956 return "Speaker";
957 if (cfg->line_outs == 2)
958 return ch ? "Bass Speaker" : "Speaker";
959 break;
960 case AUTO_PIN_HP_OUT:
Takashi Iwai247d85e2013-01-17 16:18:11 +0100961 /* if the primary channel vol/mute is shared with spk volume,
962 * don't name it as Headphone
963 */
964 if (!ch && cfg->speaker_outs &&
965 !path_has_mixer(codec, spec->speaker_paths[0], ctl_type))
966 break;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100967 /* for multi-io case, only the primary out */
968 if (ch && spec->multi_ios)
969 break;
970 *index = ch;
971 return "Headphone";
Takashi Iwai352f7f92012-12-19 12:52:06 +0100972 }
Takashi Iwai247d85e2013-01-17 16:18:11 +0100973
974 /* for a single channel output, we don't have to name the channel */
975 if (cfg->line_outs == 1 && !spec->multi_ios)
976 return "PCM";
977
Takashi Iwai352f7f92012-12-19 12:52:06 +0100978 if (ch >= ARRAY_SIZE(channel_name)) {
979 snd_BUG();
980 return "PCM";
981 }
982
983 return channel_name[ch];
984}
985
986/*
987 * Parse output paths
988 */
989
990/* badness definition */
991enum {
992 /* No primary DAC is found for the main output */
993 BAD_NO_PRIMARY_DAC = 0x10000,
994 /* No DAC is found for the extra output */
995 BAD_NO_DAC = 0x4000,
996 /* No possible multi-ios */
Takashi Iwai1d739062013-02-13 14:17:32 +0100997 BAD_MULTI_IO = 0x120,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100998 /* No individual DAC for extra output */
999 BAD_NO_EXTRA_DAC = 0x102,
1000 /* No individual DAC for extra surrounds */
1001 BAD_NO_EXTRA_SURR_DAC = 0x101,
1002 /* Primary DAC shared with main surrounds */
1003 BAD_SHARED_SURROUND = 0x100,
1004 /* Primary DAC shared with main CLFE */
1005 BAD_SHARED_CLFE = 0x10,
1006 /* Primary DAC shared with extra surrounds */
1007 BAD_SHARED_EXTRA_SURROUND = 0x10,
1008 /* Volume widget is shared */
1009 BAD_SHARED_VOL = 0x10,
1010};
1011
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001012/* look for widgets in the given path which are appropriate for
Takashi Iwai352f7f92012-12-19 12:52:06 +01001013 * volume and mute controls, and assign the values to ctls[].
1014 *
1015 * When no appropriate widget is found in the path, the badness value
1016 * is incremented depending on the situation. The function returns the
1017 * total badness for both volume and mute controls.
1018 */
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001019static int assign_out_path_ctls(struct hda_codec *codec, struct nid_path *path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001020{
Takashi Iwai352f7f92012-12-19 12:52:06 +01001021 hda_nid_t nid;
1022 unsigned int val;
1023 int badness = 0;
1024
1025 if (!path)
1026 return BAD_SHARED_VOL * 2;
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001027
1028 if (path->ctls[NID_PATH_VOL_CTL] ||
1029 path->ctls[NID_PATH_MUTE_CTL])
1030 return 0; /* already evaluated */
1031
Takashi Iwai352f7f92012-12-19 12:52:06 +01001032 nid = look_for_out_vol_nid(codec, path);
1033 if (nid) {
1034 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
1035 if (is_ctl_used(codec, val, NID_PATH_VOL_CTL))
1036 badness += BAD_SHARED_VOL;
1037 else
1038 path->ctls[NID_PATH_VOL_CTL] = val;
1039 } else
1040 badness += BAD_SHARED_VOL;
1041 nid = look_for_out_mute_nid(codec, path);
1042 if (nid) {
1043 unsigned int wid_type = get_wcaps_type(get_wcaps(codec, nid));
1044 if (wid_type == AC_WID_PIN || wid_type == AC_WID_AUD_OUT ||
1045 nid_has_mute(codec, nid, HDA_OUTPUT))
1046 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
1047 else
1048 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT);
1049 if (is_ctl_used(codec, val, NID_PATH_MUTE_CTL))
1050 badness += BAD_SHARED_VOL;
1051 else
1052 path->ctls[NID_PATH_MUTE_CTL] = val;
1053 } else
1054 badness += BAD_SHARED_VOL;
1055 return badness;
1056}
1057
1058struct badness_table {
1059 int no_primary_dac; /* no primary DAC */
1060 int no_dac; /* no secondary DACs */
1061 int shared_primary; /* primary DAC is shared with main output */
1062 int shared_surr; /* secondary DAC shared with main or primary */
1063 int shared_clfe; /* third DAC shared with main or primary */
1064 int shared_surr_main; /* secondary DAC sahred with main/DAC0 */
1065};
1066
1067static struct badness_table main_out_badness = {
1068 .no_primary_dac = BAD_NO_PRIMARY_DAC,
1069 .no_dac = BAD_NO_DAC,
1070 .shared_primary = BAD_NO_PRIMARY_DAC,
1071 .shared_surr = BAD_SHARED_SURROUND,
1072 .shared_clfe = BAD_SHARED_CLFE,
1073 .shared_surr_main = BAD_SHARED_SURROUND,
1074};
1075
1076static struct badness_table extra_out_badness = {
1077 .no_primary_dac = BAD_NO_DAC,
1078 .no_dac = BAD_NO_DAC,
1079 .shared_primary = BAD_NO_EXTRA_DAC,
1080 .shared_surr = BAD_SHARED_EXTRA_SURROUND,
1081 .shared_clfe = BAD_SHARED_EXTRA_SURROUND,
1082 .shared_surr_main = BAD_NO_EXTRA_SURR_DAC,
1083};
1084
Takashi Iwai7385df62013-01-07 09:50:52 +01001085/* get the DAC of the primary output corresponding to the given array index */
1086static hda_nid_t get_primary_out(struct hda_codec *codec, int idx)
1087{
1088 struct hda_gen_spec *spec = codec->spec;
1089 struct auto_pin_cfg *cfg = &spec->autocfg;
1090
1091 if (cfg->line_outs > idx)
1092 return spec->private_dac_nids[idx];
1093 idx -= cfg->line_outs;
1094 if (spec->multi_ios > idx)
1095 return spec->multi_io[idx].dac;
1096 return 0;
1097}
1098
1099/* return the DAC if it's reachable, otherwise zero */
1100static inline hda_nid_t try_dac(struct hda_codec *codec,
1101 hda_nid_t dac, hda_nid_t pin)
1102{
1103 return is_reachable_path(codec, dac, pin) ? dac : 0;
1104}
1105
Takashi Iwai352f7f92012-12-19 12:52:06 +01001106/* try to assign DACs to pins and return the resultant badness */
1107static int try_assign_dacs(struct hda_codec *codec, int num_outs,
1108 const hda_nid_t *pins, hda_nid_t *dacs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001109 int *path_idx,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001110 const struct badness_table *bad)
1111{
1112 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001113 int i, j;
1114 int badness = 0;
1115 hda_nid_t dac;
1116
1117 if (!num_outs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 return 0;
1119
Takashi Iwai352f7f92012-12-19 12:52:06 +01001120 for (i = 0; i < num_outs; i++) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001121 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001122 hda_nid_t pin = pins[i];
Takashi Iwai1e0b5282013-01-04 12:56:52 +01001123
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001124 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
1125 if (path) {
1126 badness += assign_out_path_ctls(codec, path);
Takashi Iwai1e0b5282013-01-04 12:56:52 +01001127 continue;
1128 }
1129
1130 dacs[i] = look_for_dac(codec, pin, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001131 if (!dacs[i] && !i) {
Takashi Iwai980428c2013-01-09 09:28:20 +01001132 /* try to steal the DAC of surrounds for the front */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001133 for (j = 1; j < num_outs; j++) {
1134 if (is_reachable_path(codec, dacs[j], pin)) {
1135 dacs[0] = dacs[j];
1136 dacs[j] = 0;
Takashi Iwai980428c2013-01-09 09:28:20 +01001137 invalidate_nid_path(codec, path_idx[j]);
Takashi Iwai196c17662013-01-04 15:01:40 +01001138 path_idx[j] = 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001139 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 }
1141 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001142 }
1143 dac = dacs[i];
1144 if (!dac) {
Takashi Iwai7385df62013-01-07 09:50:52 +01001145 if (num_outs > 2)
1146 dac = try_dac(codec, get_primary_out(codec, i), pin);
1147 if (!dac)
1148 dac = try_dac(codec, dacs[0], pin);
1149 if (!dac)
1150 dac = try_dac(codec, get_primary_out(codec, i), pin);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001151 if (dac) {
1152 if (!i)
1153 badness += bad->shared_primary;
1154 else if (i == 1)
1155 badness += bad->shared_surr;
1156 else
1157 badness += bad->shared_clfe;
1158 } else if (is_reachable_path(codec, spec->private_dac_nids[0], pin)) {
1159 dac = spec->private_dac_nids[0];
1160 badness += bad->shared_surr_main;
1161 } else if (!i)
1162 badness += bad->no_primary_dac;
1163 else
1164 badness += bad->no_dac;
1165 }
Takashi Iwai1fa335b2013-01-21 11:43:19 +01001166 if (!dac)
1167 continue;
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001168 path = snd_hda_add_new_path(codec, dac, pin, -spec->mixer_nid);
Takashi Iwai117688a2013-01-04 15:41:41 +01001169 if (!path && !i && spec->mixer_nid) {
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001170 /* try with aamix */
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001171 path = snd_hda_add_new_path(codec, dac, pin, 0);
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001172 }
Takashi Iwai1fa335b2013-01-21 11:43:19 +01001173 if (!path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001174 dac = dacs[i] = 0;
Takashi Iwai1fa335b2013-01-21 11:43:19 +01001175 badness += bad->no_dac;
1176 } else {
Takashi Iwaia7694092013-01-21 10:43:18 +01001177 /* print_nid_path("output", path); */
Takashi Iwaie1284af2013-01-03 16:33:02 +01001178 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +01001179 path_idx[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001180 badness += assign_out_path_ctls(codec, path);
Takashi Iwaie1284af2013-01-03 16:33:02 +01001181 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001182 }
1183
1184 return badness;
1185}
1186
1187/* return NID if the given pin has only a single connection to a certain DAC */
1188static hda_nid_t get_dac_if_single(struct hda_codec *codec, hda_nid_t pin)
1189{
1190 struct hda_gen_spec *spec = codec->spec;
1191 int i;
1192 hda_nid_t nid_found = 0;
1193
1194 for (i = 0; i < spec->num_all_dacs; i++) {
1195 hda_nid_t nid = spec->all_dacs[i];
1196 if (!nid || is_dac_already_used(codec, nid))
1197 continue;
1198 if (is_reachable_path(codec, nid, pin)) {
1199 if (nid_found)
1200 return 0;
1201 nid_found = nid;
1202 }
1203 }
1204 return nid_found;
1205}
1206
1207/* check whether the given pin can be a multi-io pin */
1208static bool can_be_multiio_pin(struct hda_codec *codec,
1209 unsigned int location, hda_nid_t nid)
1210{
1211 unsigned int defcfg, caps;
1212
1213 defcfg = snd_hda_codec_get_pincfg(codec, nid);
1214 if (get_defcfg_connect(defcfg) != AC_JACK_PORT_COMPLEX)
1215 return false;
1216 if (location && get_defcfg_location(defcfg) != location)
1217 return false;
1218 caps = snd_hda_query_pin_caps(codec, nid);
1219 if (!(caps & AC_PINCAP_OUT))
1220 return false;
1221 return true;
1222}
1223
Takashi Iwaie22aab72013-01-04 14:50:04 +01001224/* count the number of input pins that are capable to be multi-io */
1225static int count_multiio_pins(struct hda_codec *codec, hda_nid_t reference_pin)
1226{
1227 struct hda_gen_spec *spec = codec->spec;
1228 struct auto_pin_cfg *cfg = &spec->autocfg;
1229 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1230 unsigned int location = get_defcfg_location(defcfg);
1231 int type, i;
1232 int num_pins = 0;
1233
1234 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1235 for (i = 0; i < cfg->num_inputs; i++) {
1236 if (cfg->inputs[i].type != type)
1237 continue;
1238 if (can_be_multiio_pin(codec, location,
1239 cfg->inputs[i].pin))
1240 num_pins++;
1241 }
1242 }
1243 return num_pins;
1244}
1245
Takashi Iwai352f7f92012-12-19 12:52:06 +01001246/*
1247 * multi-io helper
1248 *
1249 * When hardwired is set, try to fill ony hardwired pins, and returns
1250 * zero if any pins are filled, non-zero if nothing found.
1251 * When hardwired is off, try to fill possible input pins, and returns
1252 * the badness value.
1253 */
1254static int fill_multi_ios(struct hda_codec *codec,
1255 hda_nid_t reference_pin,
Takashi Iwaie22aab72013-01-04 14:50:04 +01001256 bool hardwired)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001257{
1258 struct hda_gen_spec *spec = codec->spec;
1259 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwaie22aab72013-01-04 14:50:04 +01001260 int type, i, j, num_pins, old_pins;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001261 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1262 unsigned int location = get_defcfg_location(defcfg);
1263 int badness = 0;
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001264 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001265
1266 old_pins = spec->multi_ios;
1267 if (old_pins >= 2)
1268 goto end_fill;
1269
Takashi Iwaie22aab72013-01-04 14:50:04 +01001270 num_pins = count_multiio_pins(codec, reference_pin);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001271 if (num_pins < 2)
1272 goto end_fill;
1273
Takashi Iwai352f7f92012-12-19 12:52:06 +01001274 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1275 for (i = 0; i < cfg->num_inputs; i++) {
1276 hda_nid_t nid = cfg->inputs[i].pin;
1277 hda_nid_t dac = 0;
1278
1279 if (cfg->inputs[i].type != type)
1280 continue;
1281 if (!can_be_multiio_pin(codec, location, nid))
1282 continue;
1283 for (j = 0; j < spec->multi_ios; j++) {
1284 if (nid == spec->multi_io[j].pin)
1285 break;
1286 }
1287 if (j < spec->multi_ios)
1288 continue;
1289
Takashi Iwai352f7f92012-12-19 12:52:06 +01001290 if (hardwired)
1291 dac = get_dac_if_single(codec, nid);
1292 else if (!dac)
1293 dac = look_for_dac(codec, nid, false);
1294 if (!dac) {
1295 badness++;
1296 continue;
1297 }
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001298 path = snd_hda_add_new_path(codec, dac, nid,
1299 -spec->mixer_nid);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001300 if (!path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001301 badness++;
1302 continue;
1303 }
Takashi Iwaia7694092013-01-21 10:43:18 +01001304 /* print_nid_path("multiio", path); */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001305 spec->multi_io[spec->multi_ios].pin = nid;
1306 spec->multi_io[spec->multi_ios].dac = dac;
Takashi Iwai196c17662013-01-04 15:01:40 +01001307 spec->out_paths[cfg->line_outs + spec->multi_ios] =
1308 snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001309 spec->multi_ios++;
1310 if (spec->multi_ios >= 2)
1311 break;
1312 }
1313 }
1314 end_fill:
1315 if (badness)
1316 badness = BAD_MULTI_IO;
1317 if (old_pins == spec->multi_ios) {
1318 if (hardwired)
1319 return 1; /* nothing found */
1320 else
1321 return badness; /* no badness if nothing found */
1322 }
1323 if (!hardwired && spec->multi_ios < 2) {
1324 /* cancel newly assigned paths */
1325 spec->paths.used -= spec->multi_ios - old_pins;
1326 spec->multi_ios = old_pins;
1327 return badness;
1328 }
1329
1330 /* assign volume and mute controls */
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001331 for (i = old_pins; i < spec->multi_ios; i++) {
1332 path = snd_hda_get_path_from_idx(codec, spec->out_paths[cfg->line_outs + i]);
1333 badness += assign_out_path_ctls(codec, path);
1334 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001335
1336 return badness;
1337}
1338
1339/* map DACs for all pins in the list if they are single connections */
1340static bool map_singles(struct hda_codec *codec, int outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001341 const hda_nid_t *pins, hda_nid_t *dacs, int *path_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001342{
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001343 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001344 int i;
1345 bool found = false;
1346 for (i = 0; i < outs; i++) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001347 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001348 hda_nid_t dac;
1349 if (dacs[i])
1350 continue;
1351 dac = get_dac_if_single(codec, pins[i]);
1352 if (!dac)
1353 continue;
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001354 path = snd_hda_add_new_path(codec, dac, pins[i],
1355 -spec->mixer_nid);
Takashi Iwai117688a2013-01-04 15:41:41 +01001356 if (!path && !i && spec->mixer_nid)
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001357 path = snd_hda_add_new_path(codec, dac, pins[i], 0);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001358 if (path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001359 dacs[i] = dac;
1360 found = true;
Takashi Iwaia7694092013-01-21 10:43:18 +01001361 /* print_nid_path("output", path); */
Takashi Iwaie1284af2013-01-03 16:33:02 +01001362 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +01001363 path_idx[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001364 }
1365 }
1366 return found;
1367}
1368
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001369/* create a new path including aamix if available, and return its index */
1370static int check_aamix_out_path(struct hda_codec *codec, int path_idx)
1371{
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001372 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001373 struct nid_path *path;
Takashi Iwaif87498b2013-01-21 14:24:31 +01001374 hda_nid_t dac, pin;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001375
1376 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001377 if (!path || !path->depth ||
1378 is_nid_contained(path, spec->mixer_nid))
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001379 return 0;
Takashi Iwaif87498b2013-01-21 14:24:31 +01001380 dac = path->path[0];
1381 pin = path->path[path->depth - 1];
1382 path = snd_hda_add_new_path(codec, dac, pin, spec->mixer_nid);
1383 if (!path) {
1384 if (dac != spec->multiout.dac_nids[0])
1385 dac = spec->multiout.dac_nids[0];
1386 else if (spec->multiout.hp_out_nid[0])
1387 dac = spec->multiout.hp_out_nid[0];
1388 else if (spec->multiout.extra_out_nid[0])
1389 dac = spec->multiout.extra_out_nid[0];
1390 if (dac)
1391 path = snd_hda_add_new_path(codec, dac, pin,
1392 spec->mixer_nid);
1393 }
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001394 if (!path)
1395 return 0;
Takashi Iwaia7694092013-01-21 10:43:18 +01001396 /* print_nid_path("output-aamix", path); */
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001397 path->active = false; /* unused as default */
1398 return snd_hda_get_path_idx(codec, path);
1399}
1400
Takashi Iwaia07a9492013-01-07 16:44:06 +01001401/* fill the empty entries in the dac array for speaker/hp with the
1402 * shared dac pointed by the paths
1403 */
1404static void refill_shared_dacs(struct hda_codec *codec, int num_outs,
1405 hda_nid_t *dacs, int *path_idx)
1406{
1407 struct nid_path *path;
1408 int i;
1409
1410 for (i = 0; i < num_outs; i++) {
1411 if (dacs[i])
1412 continue;
1413 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
1414 if (!path)
1415 continue;
1416 dacs[i] = path->path[0];
1417 }
1418}
1419
Takashi Iwai352f7f92012-12-19 12:52:06 +01001420/* fill in the dac_nids table from the parsed pin configuration */
1421static int fill_and_eval_dacs(struct hda_codec *codec,
1422 bool fill_hardwired,
1423 bool fill_mio_first)
1424{
1425 struct hda_gen_spec *spec = codec->spec;
1426 struct auto_pin_cfg *cfg = &spec->autocfg;
1427 int i, err, badness;
1428
1429 /* set num_dacs once to full for look_for_dac() */
1430 spec->multiout.num_dacs = cfg->line_outs;
1431 spec->multiout.dac_nids = spec->private_dac_nids;
1432 memset(spec->private_dac_nids, 0, sizeof(spec->private_dac_nids));
1433 memset(spec->multiout.hp_out_nid, 0, sizeof(spec->multiout.hp_out_nid));
1434 memset(spec->multiout.extra_out_nid, 0, sizeof(spec->multiout.extra_out_nid));
1435 spec->multi_ios = 0;
1436 snd_array_free(&spec->paths);
Takashi Iwaicd5be3f2013-01-07 15:07:00 +01001437
1438 /* clear path indices */
1439 memset(spec->out_paths, 0, sizeof(spec->out_paths));
1440 memset(spec->hp_paths, 0, sizeof(spec->hp_paths));
1441 memset(spec->speaker_paths, 0, sizeof(spec->speaker_paths));
1442 memset(spec->aamix_out_paths, 0, sizeof(spec->aamix_out_paths));
1443 memset(spec->digout_paths, 0, sizeof(spec->digout_paths));
Takashi Iwaic697b712013-01-07 17:09:26 +01001444 memset(spec->input_paths, 0, sizeof(spec->input_paths));
Takashi Iwaicd5be3f2013-01-07 15:07:00 +01001445 memset(spec->loopback_paths, 0, sizeof(spec->loopback_paths));
1446 memset(&spec->digin_path, 0, sizeof(spec->digin_path));
1447
Takashi Iwai352f7f92012-12-19 12:52:06 +01001448 badness = 0;
1449
1450 /* fill hard-wired DACs first */
1451 if (fill_hardwired) {
1452 bool mapped;
1453 do {
1454 mapped = map_singles(codec, cfg->line_outs,
1455 cfg->line_out_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001456 spec->private_dac_nids,
1457 spec->out_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001458 mapped |= map_singles(codec, cfg->hp_outs,
1459 cfg->hp_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001460 spec->multiout.hp_out_nid,
1461 spec->hp_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001462 mapped |= map_singles(codec, cfg->speaker_outs,
1463 cfg->speaker_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001464 spec->multiout.extra_out_nid,
1465 spec->speaker_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001466 if (fill_mio_first && cfg->line_outs == 1 &&
1467 cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
Takashi Iwaie22aab72013-01-04 14:50:04 +01001468 err = fill_multi_ios(codec, cfg->line_out_pins[0], true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001469 if (!err)
1470 mapped = true;
1471 }
1472 } while (mapped);
1473 }
1474
1475 badness += try_assign_dacs(codec, cfg->line_outs, cfg->line_out_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001476 spec->private_dac_nids, spec->out_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001477 &main_out_badness);
1478
Takashi Iwai352f7f92012-12-19 12:52:06 +01001479 if (fill_mio_first &&
1480 cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1481 /* try to fill multi-io first */
Takashi Iwaie22aab72013-01-04 14:50:04 +01001482 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001483 if (err < 0)
1484 return err;
1485 /* we don't count badness at this stage yet */
1486 }
1487
1488 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
1489 err = try_assign_dacs(codec, cfg->hp_outs, cfg->hp_pins,
1490 spec->multiout.hp_out_nid,
Takashi Iwai196c17662013-01-04 15:01:40 +01001491 spec->hp_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001492 &extra_out_badness);
1493 if (err < 0)
1494 return err;
1495 badness += err;
1496 }
1497 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1498 err = try_assign_dacs(codec, cfg->speaker_outs,
1499 cfg->speaker_pins,
1500 spec->multiout.extra_out_nid,
Takashi Iwai196c17662013-01-04 15:01:40 +01001501 spec->speaker_paths,
1502 &extra_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001503 if (err < 0)
1504 return err;
1505 badness += err;
1506 }
1507 if (cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
Takashi Iwaie22aab72013-01-04 14:50:04 +01001508 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001509 if (err < 0)
1510 return err;
1511 badness += err;
1512 }
Takashi Iwaie22aab72013-01-04 14:50:04 +01001513
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001514 if (spec->mixer_nid) {
1515 spec->aamix_out_paths[0] =
1516 check_aamix_out_path(codec, spec->out_paths[0]);
1517 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1518 spec->aamix_out_paths[1] =
1519 check_aamix_out_path(codec, spec->hp_paths[0]);
1520 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1521 spec->aamix_out_paths[2] =
1522 check_aamix_out_path(codec, spec->speaker_paths[0]);
1523 }
1524
Takashi Iwaie22aab72013-01-04 14:50:04 +01001525 if (cfg->hp_outs && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
1526 if (count_multiio_pins(codec, cfg->hp_pins[0]) >= 2)
1527 spec->multi_ios = 1; /* give badness */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001528
Takashi Iwaia07a9492013-01-07 16:44:06 +01001529 /* re-count num_dacs and squash invalid entries */
1530 spec->multiout.num_dacs = 0;
1531 for (i = 0; i < cfg->line_outs; i++) {
1532 if (spec->private_dac_nids[i])
1533 spec->multiout.num_dacs++;
1534 else {
1535 memmove(spec->private_dac_nids + i,
1536 spec->private_dac_nids + i + 1,
1537 sizeof(hda_nid_t) * (cfg->line_outs - i - 1));
1538 spec->private_dac_nids[cfg->line_outs - 1] = 0;
1539 }
1540 }
1541
1542 spec->ext_channel_count = spec->min_channel_count =
David Henningssonc0f3b212013-01-16 11:45:37 +01001543 spec->multiout.num_dacs * 2;
Takashi Iwaia07a9492013-01-07 16:44:06 +01001544
Takashi Iwai352f7f92012-12-19 12:52:06 +01001545 if (spec->multi_ios == 2) {
1546 for (i = 0; i < 2; i++)
1547 spec->private_dac_nids[spec->multiout.num_dacs++] =
1548 spec->multi_io[i].dac;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001549 } else if (spec->multi_ios) {
1550 spec->multi_ios = 0;
1551 badness += BAD_MULTI_IO;
1552 }
1553
Takashi Iwaia07a9492013-01-07 16:44:06 +01001554 /* re-fill the shared DAC for speaker / headphone */
1555 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1556 refill_shared_dacs(codec, cfg->hp_outs,
1557 spec->multiout.hp_out_nid,
1558 spec->hp_paths);
1559 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1560 refill_shared_dacs(codec, cfg->speaker_outs,
1561 spec->multiout.extra_out_nid,
1562 spec->speaker_paths);
1563
Takashi Iwai352f7f92012-12-19 12:52:06 +01001564 return badness;
1565}
1566
1567#define DEBUG_BADNESS
1568
1569#ifdef DEBUG_BADNESS
1570#define debug_badness snd_printdd
1571#else
1572#define debug_badness(...)
1573#endif
1574
Takashi Iwaia7694092013-01-21 10:43:18 +01001575#ifdef DEBUG_BADNESS
1576static inline void print_nid_path_idx(struct hda_codec *codec,
1577 const char *pfx, int idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001578{
Takashi Iwaia7694092013-01-21 10:43:18 +01001579 struct nid_path *path;
1580
1581 path = snd_hda_get_path_from_idx(codec, idx);
1582 if (path)
1583 print_nid_path(pfx, path);
1584}
1585
1586static void debug_show_configs(struct hda_codec *codec,
1587 struct auto_pin_cfg *cfg)
1588{
1589 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia7694092013-01-21 10:43:18 +01001590 static const char * const lo_type[3] = { "LO", "SP", "HP" };
Takashi Iwaia7694092013-01-21 10:43:18 +01001591 int i;
1592
1593 debug_badness("multi_outs = %x/%x/%x/%x : %x/%x/%x/%x (type %s)\n",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001594 cfg->line_out_pins[0], cfg->line_out_pins[1],
Takashi Iwai708122e2012-12-20 17:56:57 +01001595 cfg->line_out_pins[2], cfg->line_out_pins[3],
Takashi Iwai352f7f92012-12-19 12:52:06 +01001596 spec->multiout.dac_nids[0],
1597 spec->multiout.dac_nids[1],
1598 spec->multiout.dac_nids[2],
Takashi Iwaia7694092013-01-21 10:43:18 +01001599 spec->multiout.dac_nids[3],
1600 lo_type[cfg->line_out_type]);
1601 for (i = 0; i < cfg->line_outs; i++)
1602 print_nid_path_idx(codec, " out", spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001603 if (spec->multi_ios > 0)
1604 debug_badness("multi_ios(%d) = %x/%x : %x/%x\n",
1605 spec->multi_ios,
1606 spec->multi_io[0].pin, spec->multi_io[1].pin,
1607 spec->multi_io[0].dac, spec->multi_io[1].dac);
Takashi Iwaia7694092013-01-21 10:43:18 +01001608 for (i = 0; i < spec->multi_ios; i++)
1609 print_nid_path_idx(codec, " mio",
1610 spec->out_paths[cfg->line_outs + i]);
1611 if (cfg->hp_outs)
1612 debug_badness("hp_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001613 cfg->hp_pins[0], cfg->hp_pins[1],
Takashi Iwai708122e2012-12-20 17:56:57 +01001614 cfg->hp_pins[2], cfg->hp_pins[3],
Takashi Iwai352f7f92012-12-19 12:52:06 +01001615 spec->multiout.hp_out_nid[0],
1616 spec->multiout.hp_out_nid[1],
1617 spec->multiout.hp_out_nid[2],
1618 spec->multiout.hp_out_nid[3]);
Takashi Iwaia7694092013-01-21 10:43:18 +01001619 for (i = 0; i < cfg->hp_outs; i++)
1620 print_nid_path_idx(codec, " hp ", spec->hp_paths[i]);
1621 if (cfg->speaker_outs)
1622 debug_badness("spk_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001623 cfg->speaker_pins[0], cfg->speaker_pins[1],
1624 cfg->speaker_pins[2], cfg->speaker_pins[3],
1625 spec->multiout.extra_out_nid[0],
1626 spec->multiout.extra_out_nid[1],
1627 spec->multiout.extra_out_nid[2],
1628 spec->multiout.extra_out_nid[3]);
Takashi Iwaia7694092013-01-21 10:43:18 +01001629 for (i = 0; i < cfg->speaker_outs; i++)
1630 print_nid_path_idx(codec, " spk", spec->speaker_paths[i]);
1631 for (i = 0; i < 3; i++)
1632 print_nid_path_idx(codec, " mix", spec->aamix_out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001633}
Takashi Iwaia7694092013-01-21 10:43:18 +01001634#else
1635#define debug_show_configs(codec, cfg) /* NOP */
1636#endif
Takashi Iwai352f7f92012-12-19 12:52:06 +01001637
1638/* find all available DACs of the codec */
1639static void fill_all_dac_nids(struct hda_codec *codec)
1640{
1641 struct hda_gen_spec *spec = codec->spec;
1642 int i;
1643 hda_nid_t nid = codec->start_nid;
1644
1645 spec->num_all_dacs = 0;
1646 memset(spec->all_dacs, 0, sizeof(spec->all_dacs));
1647 for (i = 0; i < codec->num_nodes; i++, nid++) {
1648 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_AUD_OUT)
1649 continue;
1650 if (spec->num_all_dacs >= ARRAY_SIZE(spec->all_dacs)) {
1651 snd_printk(KERN_ERR "hda: Too many DACs!\n");
1652 break;
1653 }
1654 spec->all_dacs[spec->num_all_dacs++] = nid;
1655 }
1656}
1657
1658static int parse_output_paths(struct hda_codec *codec)
1659{
1660 struct hda_gen_spec *spec = codec->spec;
1661 struct auto_pin_cfg *cfg = &spec->autocfg;
1662 struct auto_pin_cfg *best_cfg;
Takashi Iwai9314a582013-01-21 10:49:05 +01001663 unsigned int val;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001664 int best_badness = INT_MAX;
1665 int badness;
1666 bool fill_hardwired = true, fill_mio_first = true;
1667 bool best_wired = true, best_mio = true;
1668 bool hp_spk_swapped = false;
1669
Takashi Iwai352f7f92012-12-19 12:52:06 +01001670 best_cfg = kmalloc(sizeof(*best_cfg), GFP_KERNEL);
1671 if (!best_cfg)
1672 return -ENOMEM;
1673 *best_cfg = *cfg;
1674
1675 for (;;) {
1676 badness = fill_and_eval_dacs(codec, fill_hardwired,
1677 fill_mio_first);
1678 if (badness < 0) {
1679 kfree(best_cfg);
1680 return badness;
1681 }
1682 debug_badness("==> lo_type=%d, wired=%d, mio=%d, badness=0x%x\n",
1683 cfg->line_out_type, fill_hardwired, fill_mio_first,
1684 badness);
Takashi Iwaia7694092013-01-21 10:43:18 +01001685 debug_show_configs(codec, cfg);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001686 if (badness < best_badness) {
1687 best_badness = badness;
1688 *best_cfg = *cfg;
1689 best_wired = fill_hardwired;
1690 best_mio = fill_mio_first;
1691 }
1692 if (!badness)
1693 break;
1694 fill_mio_first = !fill_mio_first;
1695 if (!fill_mio_first)
1696 continue;
1697 fill_hardwired = !fill_hardwired;
1698 if (!fill_hardwired)
1699 continue;
1700 if (hp_spk_swapped)
1701 break;
1702 hp_spk_swapped = true;
1703 if (cfg->speaker_outs > 0 &&
1704 cfg->line_out_type == AUTO_PIN_HP_OUT) {
1705 cfg->hp_outs = cfg->line_outs;
1706 memcpy(cfg->hp_pins, cfg->line_out_pins,
1707 sizeof(cfg->hp_pins));
1708 cfg->line_outs = cfg->speaker_outs;
1709 memcpy(cfg->line_out_pins, cfg->speaker_pins,
1710 sizeof(cfg->speaker_pins));
1711 cfg->speaker_outs = 0;
1712 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
1713 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
1714 fill_hardwired = true;
1715 continue;
1716 }
1717 if (cfg->hp_outs > 0 &&
1718 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
1719 cfg->speaker_outs = cfg->line_outs;
1720 memcpy(cfg->speaker_pins, cfg->line_out_pins,
1721 sizeof(cfg->speaker_pins));
1722 cfg->line_outs = cfg->hp_outs;
1723 memcpy(cfg->line_out_pins, cfg->hp_pins,
1724 sizeof(cfg->hp_pins));
1725 cfg->hp_outs = 0;
1726 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
1727 cfg->line_out_type = AUTO_PIN_HP_OUT;
1728 fill_hardwired = true;
1729 continue;
1730 }
1731 break;
1732 }
1733
1734 if (badness) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001735 debug_badness("==> restoring best_cfg\n");
Takashi Iwai352f7f92012-12-19 12:52:06 +01001736 *cfg = *best_cfg;
1737 fill_and_eval_dacs(codec, best_wired, best_mio);
1738 }
1739 debug_badness("==> Best config: lo_type=%d, wired=%d, mio=%d\n",
1740 cfg->line_out_type, best_wired, best_mio);
Takashi Iwaia7694092013-01-21 10:43:18 +01001741 debug_show_configs(codec, cfg);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001742
1743 if (cfg->line_out_pins[0]) {
1744 struct nid_path *path;
Takashi Iwai196c17662013-01-04 15:01:40 +01001745 path = snd_hda_get_path_from_idx(codec, spec->out_paths[0]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001746 if (path)
1747 spec->vmaster_nid = look_for_out_vol_nid(codec, path);
Takashi Iwai7a71bbf2013-01-17 10:25:15 +01001748 if (spec->vmaster_nid)
1749 snd_hda_set_vmaster_tlv(codec, spec->vmaster_nid,
1750 HDA_OUTPUT, spec->vmaster_tlv);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001751 }
1752
Takashi Iwai9314a582013-01-21 10:49:05 +01001753 /* set initial pinctl targets */
1754 if (spec->prefer_hp_amp || cfg->line_out_type == AUTO_PIN_HP_OUT)
1755 val = PIN_HP;
1756 else
1757 val = PIN_OUT;
1758 set_pin_targets(codec, cfg->line_outs, cfg->line_out_pins, val);
1759 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1760 set_pin_targets(codec, cfg->hp_outs, cfg->hp_pins, PIN_HP);
1761 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1762 val = spec->prefer_hp_amp ? PIN_HP : PIN_OUT;
1763 set_pin_targets(codec, cfg->speaker_outs,
1764 cfg->speaker_pins, val);
1765 }
1766
Takashi Iwai352f7f92012-12-19 12:52:06 +01001767 kfree(best_cfg);
1768 return 0;
1769}
1770
1771/* add playback controls from the parsed DAC table */
1772static int create_multi_out_ctls(struct hda_codec *codec,
1773 const struct auto_pin_cfg *cfg)
1774{
1775 struct hda_gen_spec *spec = codec->spec;
1776 int i, err, noutputs;
1777
1778 noutputs = cfg->line_outs;
1779 if (spec->multi_ios > 0 && cfg->line_outs < 3)
1780 noutputs += spec->multi_ios;
1781
1782 for (i = 0; i < noutputs; i++) {
1783 const char *name;
1784 int index;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001785 struct nid_path *path;
1786
Takashi Iwai196c17662013-01-04 15:01:40 +01001787 path = snd_hda_get_path_from_idx(codec, spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001788 if (!path)
1789 continue;
Takashi Iwai247d85e2013-01-17 16:18:11 +01001790
1791 name = get_line_out_pfx(codec, i, &index, NID_PATH_VOL_CTL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001792 if (!name || !strcmp(name, "CLFE")) {
1793 /* Center/LFE */
1794 err = add_vol_ctl(codec, "Center", 0, 1, path);
1795 if (err < 0)
1796 return err;
1797 err = add_vol_ctl(codec, "LFE", 0, 2, path);
1798 if (err < 0)
1799 return err;
Takashi Iwai247d85e2013-01-17 16:18:11 +01001800 } else {
1801 err = add_stereo_vol(codec, name, index, path);
1802 if (err < 0)
1803 return err;
1804 }
1805
1806 name = get_line_out_pfx(codec, i, &index, NID_PATH_MUTE_CTL);
1807 if (!name || !strcmp(name, "CLFE")) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001808 err = add_sw_ctl(codec, "Center", 0, 1, path);
1809 if (err < 0)
1810 return err;
1811 err = add_sw_ctl(codec, "LFE", 0, 2, path);
1812 if (err < 0)
1813 return err;
1814 } else {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001815 err = add_stereo_sw(codec, name, index, path);
1816 if (err < 0)
1817 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 }
1819 }
1820 return 0;
1821}
1822
Takashi Iwaic2c80382013-01-07 10:33:57 +01001823static int create_extra_out(struct hda_codec *codec, int path_idx,
Takashi Iwai196c17662013-01-04 15:01:40 +01001824 const char *pfx, int cidx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825{
Takashi Iwai352f7f92012-12-19 12:52:06 +01001826 struct nid_path *path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 int err;
1828
Takashi Iwai196c17662013-01-04 15:01:40 +01001829 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001830 if (!path)
1831 return 0;
Takashi Iwaic2c80382013-01-07 10:33:57 +01001832 err = add_stereo_vol(codec, pfx, cidx, path);
1833 if (err < 0)
1834 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001835 err = add_stereo_sw(codec, pfx, cidx, path);
1836 if (err < 0)
1837 return err;
1838 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839}
1840
Takashi Iwai352f7f92012-12-19 12:52:06 +01001841/* add playback controls for speaker and HP outputs */
1842static int create_extra_outs(struct hda_codec *codec, int num_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001843 const int *paths, const char *pfx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844{
Takashi Iwaic2c80382013-01-07 10:33:57 +01001845 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001846
1847 for (i = 0; i < num_pins; i++) {
Takashi Iwaic2c80382013-01-07 10:33:57 +01001848 const char *name;
1849 char tmp[44];
1850 int err, idx = 0;
1851
1852 if (num_pins == 2 && i == 1 && !strcmp(pfx, "Speaker"))
1853 name = "Bass Speaker";
1854 else if (num_pins >= 3) {
1855 snprintf(tmp, sizeof(tmp), "%s %s",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001856 pfx, channel_name[i]);
Takashi Iwaic2c80382013-01-07 10:33:57 +01001857 name = tmp;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001858 } else {
Takashi Iwaic2c80382013-01-07 10:33:57 +01001859 name = pfx;
1860 idx = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 }
Takashi Iwaic2c80382013-01-07 10:33:57 +01001862 err = create_extra_out(codec, paths[i], name, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001863 if (err < 0)
1864 return err;
1865 }
1866 return 0;
1867}
Takashi Iwai97ec5582006-03-21 11:29:07 +01001868
Takashi Iwai352f7f92012-12-19 12:52:06 +01001869static int create_hp_out_ctls(struct hda_codec *codec)
1870{
1871 struct hda_gen_spec *spec = codec->spec;
1872 return create_extra_outs(codec, spec->autocfg.hp_outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001873 spec->hp_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001874 "Headphone");
1875}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876
Takashi Iwai352f7f92012-12-19 12:52:06 +01001877static int create_speaker_out_ctls(struct hda_codec *codec)
1878{
1879 struct hda_gen_spec *spec = codec->spec;
1880 return create_extra_outs(codec, spec->autocfg.speaker_outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001881 spec->speaker_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001882 "Speaker");
1883}
1884
1885/*
Takashi Iwai38cf6f12012-12-21 14:09:42 +01001886 * independent HP controls
1887 */
1888
1889static int indep_hp_info(struct snd_kcontrol *kcontrol,
1890 struct snd_ctl_elem_info *uinfo)
1891{
1892 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
1893}
1894
1895static int indep_hp_get(struct snd_kcontrol *kcontrol,
1896 struct snd_ctl_elem_value *ucontrol)
1897{
1898 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1899 struct hda_gen_spec *spec = codec->spec;
1900 ucontrol->value.enumerated.item[0] = spec->indep_hp_enabled;
1901 return 0;
1902}
1903
Takashi Iwaia1e908e2013-01-21 15:11:25 +01001904static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
1905 int nomix_path_idx, int mix_path_idx,
1906 int out_type);
1907
Takashi Iwai38cf6f12012-12-21 14:09:42 +01001908static int indep_hp_put(struct snd_kcontrol *kcontrol,
1909 struct snd_ctl_elem_value *ucontrol)
1910{
1911 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1912 struct hda_gen_spec *spec = codec->spec;
1913 unsigned int select = ucontrol->value.enumerated.item[0];
1914 int ret = 0;
1915
1916 mutex_lock(&spec->pcm_mutex);
1917 if (spec->active_streams) {
1918 ret = -EBUSY;
1919 goto unlock;
1920 }
1921
1922 if (spec->indep_hp_enabled != select) {
Takashi Iwaia1e908e2013-01-21 15:11:25 +01001923 hda_nid_t *dacp;
1924 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
1925 dacp = &spec->private_dac_nids[0];
1926 else
1927 dacp = &spec->multiout.hp_out_nid[0];
1928
1929 /* update HP aamix paths in case it conflicts with indep HP */
1930 if (spec->have_aamix_ctl) {
1931 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
1932 update_aamix_paths(codec, spec->aamix_mode,
1933 spec->out_paths[0],
1934 spec->aamix_out_paths[0],
1935 spec->autocfg.line_out_type);
1936 else
1937 update_aamix_paths(codec, spec->aamix_mode,
1938 spec->hp_paths[0],
1939 spec->aamix_out_paths[1],
1940 AUTO_PIN_HP_OUT);
1941 }
1942
Takashi Iwai38cf6f12012-12-21 14:09:42 +01001943 spec->indep_hp_enabled = select;
1944 if (spec->indep_hp_enabled)
Takashi Iwaia1e908e2013-01-21 15:11:25 +01001945 *dacp = 0;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01001946 else
Takashi Iwaia1e908e2013-01-21 15:11:25 +01001947 *dacp = spec->alt_dac_nid;
Takashi Iwai92603c52013-01-22 07:46:31 +01001948
1949 /* update HP auto-mute state too */
1950 if (spec->hp_automute_hook)
1951 spec->hp_automute_hook(codec, NULL);
1952 else
1953 snd_hda_gen_hp_automute(codec, NULL);
1954
Takashi Iwai38cf6f12012-12-21 14:09:42 +01001955 ret = 1;
1956 }
1957 unlock:
1958 mutex_unlock(&spec->pcm_mutex);
1959 return ret;
1960}
1961
1962static const struct snd_kcontrol_new indep_hp_ctl = {
1963 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1964 .name = "Independent HP",
1965 .info = indep_hp_info,
1966 .get = indep_hp_get,
1967 .put = indep_hp_put,
1968};
1969
1970
1971static int create_indep_hp_ctls(struct hda_codec *codec)
1972{
1973 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01001974 hda_nid_t dac;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01001975
1976 if (!spec->indep_hp)
1977 return 0;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01001978 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
1979 dac = spec->multiout.dac_nids[0];
1980 else
1981 dac = spec->multiout.hp_out_nid[0];
1982 if (!dac) {
Takashi Iwai38cf6f12012-12-21 14:09:42 +01001983 spec->indep_hp = 0;
1984 return 0;
1985 }
1986
1987 spec->indep_hp_enabled = false;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01001988 spec->alt_dac_nid = dac;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01001989 if (!snd_hda_gen_add_kctl(spec, NULL, &indep_hp_ctl))
1990 return -ENOMEM;
1991 return 0;
1992}
1993
1994/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01001995 * channel mode enum control
1996 */
1997
1998static int ch_mode_info(struct snd_kcontrol *kcontrol,
1999 struct snd_ctl_elem_info *uinfo)
2000{
2001 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2002 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002003 int chs;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002004
2005 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2006 uinfo->count = 1;
2007 uinfo->value.enumerated.items = spec->multi_ios + 1;
2008 if (uinfo->value.enumerated.item > spec->multi_ios)
2009 uinfo->value.enumerated.item = spec->multi_ios;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002010 chs = uinfo->value.enumerated.item * 2 + spec->min_channel_count;
2011 sprintf(uinfo->value.enumerated.name, "%dch", chs);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002012 return 0;
2013}
2014
2015static int ch_mode_get(struct snd_kcontrol *kcontrol,
2016 struct snd_ctl_elem_value *ucontrol)
2017{
2018 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2019 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002020 ucontrol->value.enumerated.item[0] =
2021 (spec->ext_channel_count - spec->min_channel_count) / 2;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002022 return 0;
2023}
2024
Takashi Iwai196c17662013-01-04 15:01:40 +01002025static inline struct nid_path *
2026get_multiio_path(struct hda_codec *codec, int idx)
2027{
2028 struct hda_gen_spec *spec = codec->spec;
2029 return snd_hda_get_path_from_idx(codec,
2030 spec->out_paths[spec->autocfg.line_outs + idx]);
2031}
2032
Takashi Iwaia5cc2502013-01-16 18:08:55 +01002033static void update_automute_all(struct hda_codec *codec);
2034
Takashi Iwai352f7f92012-12-19 12:52:06 +01002035static int set_multi_io(struct hda_codec *codec, int idx, bool output)
2036{
2037 struct hda_gen_spec *spec = codec->spec;
2038 hda_nid_t nid = spec->multi_io[idx].pin;
2039 struct nid_path *path;
2040
Takashi Iwai196c17662013-01-04 15:01:40 +01002041 path = get_multiio_path(codec, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002042 if (!path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 return -EINVAL;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002044
2045 if (path->active == output)
2046 return 0;
2047
2048 if (output) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01002049 set_pin_target(codec, nid, PIN_OUT, true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002050 snd_hda_activate_path(codec, path, true, true);
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01002051 set_pin_eapd(codec, nid, true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002052 } else {
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01002053 set_pin_eapd(codec, nid, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002054 snd_hda_activate_path(codec, path, false, true);
Takashi Iwai2c12c302013-01-10 09:33:29 +01002055 set_pin_target(codec, nid, spec->multi_io[idx].ctl_in, true);
Takashi Iwai55196ff2013-01-24 17:32:56 +01002056 path_power_down_sync(codec, path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 }
Takashi Iwaia365fed2013-01-10 16:10:06 +01002058
2059 /* update jack retasking in case it modifies any of them */
Takashi Iwaia5cc2502013-01-16 18:08:55 +01002060 update_automute_all(codec);
Takashi Iwaia365fed2013-01-10 16:10:06 +01002061
Takashi Iwai352f7f92012-12-19 12:52:06 +01002062 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063}
2064
Takashi Iwai352f7f92012-12-19 12:52:06 +01002065static int ch_mode_put(struct snd_kcontrol *kcontrol,
2066 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002068 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2069 struct hda_gen_spec *spec = codec->spec;
2070 int i, ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071
Takashi Iwai352f7f92012-12-19 12:52:06 +01002072 ch = ucontrol->value.enumerated.item[0];
2073 if (ch < 0 || ch > spec->multi_ios)
2074 return -EINVAL;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002075 if (ch == (spec->ext_channel_count - spec->min_channel_count) / 2)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002076 return 0;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002077 spec->ext_channel_count = ch * 2 + spec->min_channel_count;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002078 for (i = 0; i < spec->multi_ios; i++)
2079 set_multi_io(codec, i, i < ch);
2080 spec->multiout.max_channels = max(spec->ext_channel_count,
2081 spec->const_channel_count);
2082 if (spec->need_dac_fix)
2083 spec->multiout.num_dacs = spec->multiout.max_channels / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 return 1;
2085}
2086
Takashi Iwai352f7f92012-12-19 12:52:06 +01002087static const struct snd_kcontrol_new channel_mode_enum = {
2088 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2089 .name = "Channel Mode",
2090 .info = ch_mode_info,
2091 .get = ch_mode_get,
2092 .put = ch_mode_put,
2093};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094
Takashi Iwai352f7f92012-12-19 12:52:06 +01002095static int create_multi_channel_mode(struct hda_codec *codec)
2096{
2097 struct hda_gen_spec *spec = codec->spec;
2098
2099 if (spec->multi_ios > 0) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01002100 if (!snd_hda_gen_add_kctl(spec, NULL, &channel_mode_enum))
Takashi Iwai352f7f92012-12-19 12:52:06 +01002101 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 return 0;
2104}
2105
Takashi Iwai352f7f92012-12-19 12:52:06 +01002106/*
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002107 * aamix loopback enable/disable switch
2108 */
2109
2110#define loopback_mixing_info indep_hp_info
2111
2112static int loopback_mixing_get(struct snd_kcontrol *kcontrol,
2113 struct snd_ctl_elem_value *ucontrol)
2114{
2115 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2116 struct hda_gen_spec *spec = codec->spec;
2117 ucontrol->value.enumerated.item[0] = spec->aamix_mode;
2118 return 0;
2119}
2120
2121static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002122 int nomix_path_idx, int mix_path_idx,
2123 int out_type)
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002124{
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002125 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002126 struct nid_path *nomix_path, *mix_path;
2127
2128 nomix_path = snd_hda_get_path_from_idx(codec, nomix_path_idx);
2129 mix_path = snd_hda_get_path_from_idx(codec, mix_path_idx);
2130 if (!nomix_path || !mix_path)
2131 return;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002132
2133 /* if HP aamix path is driven from a different DAC and the
2134 * independent HP mode is ON, can't turn on aamix path
2135 */
2136 if (out_type == AUTO_PIN_HP_OUT && spec->indep_hp_enabled &&
2137 mix_path->path[0] != spec->alt_dac_nid)
2138 do_mix = false;
2139
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002140 if (do_mix) {
2141 snd_hda_activate_path(codec, nomix_path, false, true);
2142 snd_hda_activate_path(codec, mix_path, true, true);
Takashi Iwai55196ff2013-01-24 17:32:56 +01002143 path_power_down_sync(codec, nomix_path);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002144 } else {
2145 snd_hda_activate_path(codec, mix_path, false, true);
2146 snd_hda_activate_path(codec, nomix_path, true, true);
Takashi Iwai55196ff2013-01-24 17:32:56 +01002147 path_power_down_sync(codec, mix_path);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002148 }
2149}
2150
2151static int loopback_mixing_put(struct snd_kcontrol *kcontrol,
2152 struct snd_ctl_elem_value *ucontrol)
2153{
2154 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2155 struct hda_gen_spec *spec = codec->spec;
2156 unsigned int val = ucontrol->value.enumerated.item[0];
2157
2158 if (val == spec->aamix_mode)
2159 return 0;
2160 spec->aamix_mode = val;
2161 update_aamix_paths(codec, val, spec->out_paths[0],
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002162 spec->aamix_out_paths[0],
2163 spec->autocfg.line_out_type);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002164 update_aamix_paths(codec, val, spec->hp_paths[0],
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002165 spec->aamix_out_paths[1],
2166 AUTO_PIN_HP_OUT);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002167 update_aamix_paths(codec, val, spec->speaker_paths[0],
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002168 spec->aamix_out_paths[2],
2169 AUTO_PIN_SPEAKER_OUT);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002170 return 1;
2171}
2172
2173static const struct snd_kcontrol_new loopback_mixing_enum = {
2174 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2175 .name = "Loopback Mixing",
2176 .info = loopback_mixing_info,
2177 .get = loopback_mixing_get,
2178 .put = loopback_mixing_put,
2179};
2180
2181static int create_loopback_mixing_ctl(struct hda_codec *codec)
2182{
2183 struct hda_gen_spec *spec = codec->spec;
2184
2185 if (!spec->mixer_nid)
2186 return 0;
2187 if (!(spec->aamix_out_paths[0] || spec->aamix_out_paths[1] ||
2188 spec->aamix_out_paths[2]))
2189 return 0;
2190 if (!snd_hda_gen_add_kctl(spec, NULL, &loopback_mixing_enum))
2191 return -ENOMEM;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002192 spec->have_aamix_ctl = 1;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002193 return 0;
2194}
2195
2196/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01002197 * shared headphone/mic handling
2198 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02002199
Takashi Iwai352f7f92012-12-19 12:52:06 +01002200static void call_update_outputs(struct hda_codec *codec);
2201
2202/* for shared I/O, change the pin-control accordingly */
Takashi Iwai967303d2013-02-19 17:12:42 +01002203static void update_hp_mic(struct hda_codec *codec, int adc_mux, bool force)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002204{
2205 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai967303d2013-02-19 17:12:42 +01002206 bool as_mic;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002207 unsigned int val;
Takashi Iwai967303d2013-02-19 17:12:42 +01002208 hda_nid_t pin;
2209
2210 pin = spec->hp_mic_pin;
2211 as_mic = spec->cur_mux[adc_mux] == spec->hp_mic_mux_idx;
2212
2213 if (!force) {
2214 val = snd_hda_codec_get_pin_target(codec, pin);
2215 if (as_mic) {
2216 if (val & PIN_IN)
2217 return;
2218 } else {
2219 if (val & PIN_OUT)
2220 return;
2221 }
2222 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002223
2224 val = snd_hda_get_default_vref(codec, pin);
Takashi Iwai967303d2013-02-19 17:12:42 +01002225 /* if the HP pin doesn't support VREF and the codec driver gives an
2226 * alternative pin, set up the VREF on that pin instead
2227 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01002228 if (val == AC_PINCTL_VREF_HIZ && spec->shared_mic_vref_pin) {
2229 const hda_nid_t vref_pin = spec->shared_mic_vref_pin;
2230 unsigned int vref_val = snd_hda_get_default_vref(codec, vref_pin);
2231 if (vref_val != AC_PINCTL_VREF_HIZ)
Takashi Iwai7594aa32012-12-20 15:38:40 +01002232 snd_hda_set_pin_ctl_cache(codec, vref_pin,
Takashi Iwai967303d2013-02-19 17:12:42 +01002233 PIN_IN | (as_mic ? vref_val : 0));
Takashi Iwaicb53c622007-08-10 17:21:45 +02002234 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002235
Takashi Iwai967303d2013-02-19 17:12:42 +01002236 if (as_mic)
2237 val |= PIN_IN;
2238 else
2239 val = PIN_HP;
Takashi Iwai2c12c302013-01-10 09:33:29 +01002240 set_pin_target(codec, pin, val, true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002241
Takashi Iwai967303d2013-02-19 17:12:42 +01002242 /* update HP auto-mute state too */
2243 if (spec->hp_automute_hook)
2244 spec->hp_automute_hook(codec, NULL);
2245 else
2246 snd_hda_gen_hp_automute(codec, NULL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002247}
2248
2249/* create a shared input with the headphone out */
Takashi Iwai967303d2013-02-19 17:12:42 +01002250static int create_hp_mic(struct hda_codec *codec)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002251{
2252 struct hda_gen_spec *spec = codec->spec;
2253 struct auto_pin_cfg *cfg = &spec->autocfg;
2254 unsigned int defcfg;
2255 hda_nid_t nid;
2256
Takashi Iwai967303d2013-02-19 17:12:42 +01002257 if (!spec->hp_mic) {
2258 if (spec->suppress_hp_mic_detect)
2259 return 0;
2260 /* automatic detection: only if no input or a single internal
2261 * input pin is found, try to detect the shared hp/mic
2262 */
2263 if (cfg->num_inputs > 1)
2264 return 0;
2265 else if (cfg->num_inputs == 1) {
2266 defcfg = snd_hda_codec_get_pincfg(codec, cfg->inputs[0].pin);
2267 if (snd_hda_get_input_pin_attr(defcfg) != INPUT_PIN_ATTR_INT)
2268 return 0;
2269 }
2270 }
2271
2272 spec->hp_mic = 0; /* clear once */
2273 if (cfg->num_inputs >= AUTO_CFG_MAX_INS)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002274 return 0;
2275
Takashi Iwai967303d2013-02-19 17:12:42 +01002276 nid = 0;
2277 if (cfg->line_out_type == AUTO_PIN_HP_OUT && cfg->line_outs > 0)
2278 nid = cfg->line_out_pins[0];
2279 else if (cfg->hp_outs > 0)
2280 nid = cfg->hp_pins[0];
2281 if (!nid)
2282 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002283
2284 if (!(snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_IN))
2285 return 0; /* no input */
2286
Takashi Iwai967303d2013-02-19 17:12:42 +01002287 cfg->inputs[cfg->num_inputs].pin = nid;
2288 cfg->inputs[cfg->num_inputs].type = AUTO_PIN_MIC;
2289 cfg->num_inputs++;
2290 spec->hp_mic = 1;
2291 spec->hp_mic_pin = nid;
2292 /* we can't handle auto-mic together with HP-mic */
2293 spec->suppress_auto_mic = 1;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002294 snd_printdd("hda-codec: Enable shared I/O jack on NID 0x%x\n", nid);
2295 return 0;
2296}
2297
Takashi Iwai978e77e2013-01-10 16:57:58 +01002298/*
2299 * output jack mode
2300 */
2301static int out_jack_mode_info(struct snd_kcontrol *kcontrol,
2302 struct snd_ctl_elem_info *uinfo)
2303{
2304 static const char * const texts[] = {
2305 "Line Out", "Headphone Out",
2306 };
2307 return snd_hda_enum_helper_info(kcontrol, uinfo, 2, texts);
2308}
2309
2310static int out_jack_mode_get(struct snd_kcontrol *kcontrol,
2311 struct snd_ctl_elem_value *ucontrol)
2312{
2313 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2314 hda_nid_t nid = kcontrol->private_value;
2315 if (snd_hda_codec_get_pin_target(codec, nid) == PIN_HP)
2316 ucontrol->value.enumerated.item[0] = 1;
2317 else
2318 ucontrol->value.enumerated.item[0] = 0;
2319 return 0;
2320}
2321
2322static int out_jack_mode_put(struct snd_kcontrol *kcontrol,
2323 struct snd_ctl_elem_value *ucontrol)
2324{
2325 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2326 hda_nid_t nid = kcontrol->private_value;
2327 unsigned int val;
2328
2329 val = ucontrol->value.enumerated.item[0] ? PIN_HP : PIN_OUT;
2330 if (snd_hda_codec_get_pin_target(codec, nid) == val)
2331 return 0;
2332 snd_hda_set_pin_ctl_cache(codec, nid, val);
2333 return 1;
2334}
2335
2336static const struct snd_kcontrol_new out_jack_mode_enum = {
2337 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2338 .info = out_jack_mode_info,
2339 .get = out_jack_mode_get,
2340 .put = out_jack_mode_put,
2341};
2342
2343static bool find_kctl_name(struct hda_codec *codec, const char *name, int idx)
2344{
2345 struct hda_gen_spec *spec = codec->spec;
2346 int i;
2347
2348 for (i = 0; i < spec->kctls.used; i++) {
2349 struct snd_kcontrol_new *kctl = snd_array_elem(&spec->kctls, i);
2350 if (!strcmp(kctl->name, name) && kctl->index == idx)
2351 return true;
2352 }
2353 return false;
2354}
2355
2356static void get_jack_mode_name(struct hda_codec *codec, hda_nid_t pin,
2357 char *name, size_t name_len)
2358{
2359 struct hda_gen_spec *spec = codec->spec;
2360 int idx = 0;
2361
2362 snd_hda_get_pin_label(codec, pin, &spec->autocfg, name, name_len, &idx);
2363 strlcat(name, " Jack Mode", name_len);
2364
2365 for (; find_kctl_name(codec, name, idx); idx++)
2366 ;
2367}
2368
2369static int create_out_jack_modes(struct hda_codec *codec, int num_pins,
2370 hda_nid_t *pins)
2371{
2372 struct hda_gen_spec *spec = codec->spec;
2373 int i;
2374
2375 for (i = 0; i < num_pins; i++) {
2376 hda_nid_t pin = pins[i];
2377 unsigned int pincap = snd_hda_query_pin_caps(codec, pin);
2378 if ((pincap & AC_PINCAP_OUT) && (pincap & AC_PINCAP_HP_DRV)) {
2379 struct snd_kcontrol_new *knew;
2380 char name[44];
2381 get_jack_mode_name(codec, pin, name, sizeof(name));
2382 knew = snd_hda_gen_add_kctl(spec, name,
2383 &out_jack_mode_enum);
2384 if (!knew)
2385 return -ENOMEM;
2386 knew->private_value = pin;
2387 }
2388 }
2389
2390 return 0;
2391}
2392
Takashi Iwai294765582013-01-17 09:52:11 +01002393/*
2394 * input jack mode
2395 */
2396
2397/* from AC_PINCTL_VREF_HIZ to AC_PINCTL_VREF_100 */
2398#define NUM_VREFS 6
2399
2400static const char * const vref_texts[NUM_VREFS] = {
2401 "Line In", "Mic 50pc Bias", "Mic 0V Bias",
2402 "", "Mic 80pc Bias", "Mic 100pc Bias"
2403};
2404
2405static unsigned int get_vref_caps(struct hda_codec *codec, hda_nid_t pin)
2406{
2407 unsigned int pincap;
2408
2409 pincap = snd_hda_query_pin_caps(codec, pin);
2410 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
2411 /* filter out unusual vrefs */
2412 pincap &= ~(AC_PINCAP_VREF_GRD | AC_PINCAP_VREF_100);
2413 return pincap;
2414}
2415
2416/* convert from the enum item index to the vref ctl index (0=HIZ, 1=50%...) */
2417static int get_vref_idx(unsigned int vref_caps, unsigned int item_idx)
2418{
2419 unsigned int i, n = 0;
2420
2421 for (i = 0; i < NUM_VREFS; i++) {
2422 if (vref_caps & (1 << i)) {
2423 if (n == item_idx)
2424 return i;
2425 n++;
2426 }
2427 }
2428 return 0;
2429}
2430
2431/* convert back from the vref ctl index to the enum item index */
2432static int cvt_from_vref_idx(unsigned int vref_caps, unsigned int idx)
2433{
2434 unsigned int i, n = 0;
2435
2436 for (i = 0; i < NUM_VREFS; i++) {
2437 if (i == idx)
2438 return n;
2439 if (vref_caps & (1 << i))
2440 n++;
2441 }
2442 return 0;
2443}
2444
2445static int in_jack_mode_info(struct snd_kcontrol *kcontrol,
2446 struct snd_ctl_elem_info *uinfo)
2447{
2448 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2449 hda_nid_t nid = kcontrol->private_value;
2450 unsigned int vref_caps = get_vref_caps(codec, nid);
2451
2452 snd_hda_enum_helper_info(kcontrol, uinfo, hweight32(vref_caps),
2453 vref_texts);
2454 /* set the right text */
2455 strcpy(uinfo->value.enumerated.name,
2456 vref_texts[get_vref_idx(vref_caps, uinfo->value.enumerated.item)]);
2457 return 0;
2458}
2459
2460static int in_jack_mode_get(struct snd_kcontrol *kcontrol,
2461 struct snd_ctl_elem_value *ucontrol)
2462{
2463 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2464 hda_nid_t nid = kcontrol->private_value;
2465 unsigned int vref_caps = get_vref_caps(codec, nid);
2466 unsigned int idx;
2467
2468 idx = snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_VREFEN;
2469 ucontrol->value.enumerated.item[0] = cvt_from_vref_idx(vref_caps, idx);
2470 return 0;
2471}
2472
2473static int in_jack_mode_put(struct snd_kcontrol *kcontrol,
2474 struct snd_ctl_elem_value *ucontrol)
2475{
2476 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2477 hda_nid_t nid = kcontrol->private_value;
2478 unsigned int vref_caps = get_vref_caps(codec, nid);
2479 unsigned int val, idx;
2480
2481 val = snd_hda_codec_get_pin_target(codec, nid);
2482 idx = cvt_from_vref_idx(vref_caps, val & AC_PINCTL_VREFEN);
2483 if (idx == ucontrol->value.enumerated.item[0])
2484 return 0;
2485
2486 val &= ~AC_PINCTL_VREFEN;
2487 val |= get_vref_idx(vref_caps, ucontrol->value.enumerated.item[0]);
2488 snd_hda_set_pin_ctl_cache(codec, nid, val);
2489 return 1;
2490}
2491
2492static const struct snd_kcontrol_new in_jack_mode_enum = {
2493 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2494 .info = in_jack_mode_info,
2495 .get = in_jack_mode_get,
2496 .put = in_jack_mode_put,
2497};
2498
2499static int create_in_jack_mode(struct hda_codec *codec, hda_nid_t pin)
2500{
2501 struct hda_gen_spec *spec = codec->spec;
2502 unsigned int defcfg;
2503 struct snd_kcontrol_new *knew;
2504 char name[44];
2505
2506 /* no jack mode for fixed pins */
2507 defcfg = snd_hda_codec_get_pincfg(codec, pin);
2508 if (snd_hda_get_input_pin_attr(defcfg) == INPUT_PIN_ATTR_INT)
2509 return 0;
2510
2511 /* no multiple vref caps? */
2512 if (hweight32(get_vref_caps(codec, pin)) <= 1)
2513 return 0;
2514
2515 get_jack_mode_name(codec, pin, name, sizeof(name));
2516 knew = snd_hda_gen_add_kctl(spec, name, &in_jack_mode_enum);
2517 if (!knew)
2518 return -ENOMEM;
2519 knew->private_value = pin;
2520 return 0;
2521}
2522
Takashi Iwai352f7f92012-12-19 12:52:06 +01002523
2524/*
2525 * Parse input paths
2526 */
2527
Takashi Iwai352f7f92012-12-19 12:52:06 +01002528/* add the powersave loopback-list entry */
Takashi Iwai0186f4f2013-02-07 10:45:11 +01002529static int add_loopback_list(struct hda_gen_spec *spec, hda_nid_t mix, int idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002530{
2531 struct hda_amp_list *list;
2532
Takashi Iwai0186f4f2013-02-07 10:45:11 +01002533 list = snd_array_new(&spec->loopback_list);
2534 if (!list)
2535 return -ENOMEM;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002536 list->nid = mix;
2537 list->dir = HDA_INPUT;
2538 list->idx = idx;
Takashi Iwai0186f4f2013-02-07 10:45:11 +01002539 spec->loopback.amplist = spec->loopback_list.list;
2540 return 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02002541}
Takashi Iwaicb53c622007-08-10 17:21:45 +02002542
Takashi Iwai352f7f92012-12-19 12:52:06 +01002543/* create input playback/capture controls for the given pin */
Takashi Iwai196c17662013-01-04 15:01:40 +01002544static int new_analog_input(struct hda_codec *codec, int input_idx,
2545 hda_nid_t pin, const char *ctlname, int ctlidx,
Takashi Iwai352f7f92012-12-19 12:52:06 +01002546 hda_nid_t mix_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002548 struct hda_gen_spec *spec = codec->spec;
2549 struct nid_path *path;
2550 unsigned int val;
2551 int err, idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552
Takashi Iwai352f7f92012-12-19 12:52:06 +01002553 if (!nid_has_volume(codec, mix_nid, HDA_INPUT) &&
2554 !nid_has_mute(codec, mix_nid, HDA_INPUT))
2555 return 0; /* no need for analog loopback */
2556
Takashi Iwai3ca529d2013-01-07 17:25:08 +01002557 path = snd_hda_add_new_path(codec, pin, mix_nid, 0);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002558 if (!path)
2559 return -EINVAL;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01002560 print_nid_path("loopback", path);
Takashi Iwai196c17662013-01-04 15:01:40 +01002561 spec->loopback_paths[input_idx] = snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002562
2563 idx = path->idx[path->depth - 1];
2564 if (nid_has_volume(codec, mix_nid, HDA_INPUT)) {
2565 val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
2566 err = __add_pb_vol_ctrl(spec, HDA_CTL_WIDGET_VOL, ctlname, ctlidx, val);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002567 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002569 path->ctls[NID_PATH_VOL_CTL] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570 }
2571
Takashi Iwai352f7f92012-12-19 12:52:06 +01002572 if (nid_has_mute(codec, mix_nid, HDA_INPUT)) {
2573 val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
2574 err = __add_pb_sw_ctrl(spec, HDA_CTL_WIDGET_MUTE, ctlname, ctlidx, val);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002575 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002577 path->ctls[NID_PATH_MUTE_CTL] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578 }
2579
Takashi Iwai352f7f92012-12-19 12:52:06 +01002580 path->active = true;
Takashi Iwai0186f4f2013-02-07 10:45:11 +01002581 err = add_loopback_list(spec, mix_nid, idx);
2582 if (err < 0)
2583 return err;
Takashi Iwaie4a395e2013-01-23 17:00:31 +01002584
2585 if (spec->mixer_nid != spec->mixer_merge_nid &&
2586 !spec->loopback_merge_path) {
2587 path = snd_hda_add_new_path(codec, spec->mixer_nid,
2588 spec->mixer_merge_nid, 0);
2589 if (path) {
2590 print_nid_path("loopback-merge", path);
2591 path->active = true;
2592 spec->loopback_merge_path =
2593 snd_hda_get_path_idx(codec, path);
2594 }
2595 }
2596
Takashi Iwai352f7f92012-12-19 12:52:06 +01002597 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598}
2599
Takashi Iwai352f7f92012-12-19 12:52:06 +01002600static int is_input_pin(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002602 unsigned int pincap = snd_hda_query_pin_caps(codec, nid);
2603 return (pincap & AC_PINCAP_IN) != 0;
2604}
2605
2606/* Parse the codec tree and retrieve ADCs */
2607static int fill_adc_nids(struct hda_codec *codec)
2608{
2609 struct hda_gen_spec *spec = codec->spec;
2610 hda_nid_t nid;
2611 hda_nid_t *adc_nids = spec->adc_nids;
2612 int max_nums = ARRAY_SIZE(spec->adc_nids);
2613 int i, nums = 0;
2614
2615 nid = codec->start_nid;
2616 for (i = 0; i < codec->num_nodes; i++, nid++) {
2617 unsigned int caps = get_wcaps(codec, nid);
2618 int type = get_wcaps_type(caps);
2619
2620 if (type != AC_WID_AUD_IN || (caps & AC_WCAP_DIGITAL))
2621 continue;
2622 adc_nids[nums] = nid;
2623 if (++nums >= max_nums)
2624 break;
2625 }
2626 spec->num_adc_nids = nums;
Takashi Iwai0ffd5342013-01-17 15:53:29 +01002627
2628 /* copy the detected ADCs to all_adcs[] */
2629 spec->num_all_adcs = nums;
2630 memcpy(spec->all_adcs, spec->adc_nids, nums * sizeof(hda_nid_t));
2631
Takashi Iwai352f7f92012-12-19 12:52:06 +01002632 return nums;
2633}
2634
2635/* filter out invalid adc_nids that don't give all active input pins;
2636 * if needed, check whether dynamic ADC-switching is available
2637 */
2638static int check_dyn_adc_switch(struct hda_codec *codec)
2639{
2640 struct hda_gen_spec *spec = codec->spec;
2641 struct hda_input_mux *imux = &spec->input_mux;
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002642 unsigned int ok_bits;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002643 int i, n, nums;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002644
Takashi Iwai352f7f92012-12-19 12:52:06 +01002645 nums = 0;
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002646 ok_bits = 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002647 for (n = 0; n < spec->num_adc_nids; n++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002648 for (i = 0; i < imux->num_items; i++) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002649 if (!spec->input_paths[i][n])
Takashi Iwai352f7f92012-12-19 12:52:06 +01002650 break;
2651 }
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002652 if (i >= imux->num_items) {
2653 ok_bits |= (1 << n);
2654 nums++;
2655 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002656 }
2657
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002658 if (!ok_bits) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002659 /* check whether ADC-switch is possible */
2660 for (i = 0; i < imux->num_items; i++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002661 for (n = 0; n < spec->num_adc_nids; n++) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002662 if (spec->input_paths[i][n]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002663 spec->dyn_adc_idx[i] = n;
2664 break;
2665 }
2666 }
2667 }
2668
2669 snd_printdd("hda-codec: enabling ADC switching\n");
2670 spec->dyn_adc_switch = 1;
2671 } else if (nums != spec->num_adc_nids) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002672 /* shrink the invalid adcs and input paths */
2673 nums = 0;
2674 for (n = 0; n < spec->num_adc_nids; n++) {
2675 if (!(ok_bits & (1 << n)))
2676 continue;
2677 if (n != nums) {
2678 spec->adc_nids[nums] = spec->adc_nids[n];
Takashi Iwai980428c2013-01-09 09:28:20 +01002679 for (i = 0; i < imux->num_items; i++) {
2680 invalidate_nid_path(codec,
2681 spec->input_paths[i][nums]);
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002682 spec->input_paths[i][nums] =
2683 spec->input_paths[i][n];
Takashi Iwai980428c2013-01-09 09:28:20 +01002684 }
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002685 }
2686 nums++;
2687 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002688 spec->num_adc_nids = nums;
2689 }
2690
Takashi Iwai967303d2013-02-19 17:12:42 +01002691 if (imux->num_items == 1 ||
2692 (imux->num_items == 2 && spec->hp_mic)) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002693 snd_printdd("hda-codec: reducing to a single ADC\n");
2694 spec->num_adc_nids = 1; /* reduce to a single ADC */
2695 }
2696
2697 /* single index for individual volumes ctls */
2698 if (!spec->dyn_adc_switch && spec->multi_cap_vol)
2699 spec->num_adc_nids = 1;
2700
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701 return 0;
2702}
2703
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002704/* parse capture source paths from the given pin and create imux items */
2705static int parse_capture_source(struct hda_codec *codec, hda_nid_t pin,
Takashi Iwai9dba2052013-01-18 10:01:15 +01002706 int cfg_idx, int num_adcs,
2707 const char *label, int anchor)
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002708{
2709 struct hda_gen_spec *spec = codec->spec;
2710 struct hda_input_mux *imux = &spec->input_mux;
2711 int imux_idx = imux->num_items;
2712 bool imux_added = false;
2713 int c;
2714
2715 for (c = 0; c < num_adcs; c++) {
2716 struct nid_path *path;
2717 hda_nid_t adc = spec->adc_nids[c];
2718
2719 if (!is_reachable_path(codec, pin, adc))
2720 continue;
2721 path = snd_hda_add_new_path(codec, pin, adc, anchor);
2722 if (!path)
2723 continue;
2724 print_nid_path("input", path);
2725 spec->input_paths[imux_idx][c] =
2726 snd_hda_get_path_idx(codec, path);
2727
2728 if (!imux_added) {
Takashi Iwai967303d2013-02-19 17:12:42 +01002729 if (spec->hp_mic_pin == pin)
2730 spec->hp_mic_mux_idx = imux->num_items;
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002731 spec->imux_pins[imux->num_items] = pin;
Takashi Iwai9dba2052013-01-18 10:01:15 +01002732 snd_hda_add_imux_item(imux, label, cfg_idx, NULL);
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002733 imux_added = true;
2734 }
2735 }
2736
2737 return 0;
2738}
2739
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01002741 * create playback/capture controls for input pins
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742 */
Takashi Iwai9dba2052013-01-18 10:01:15 +01002743
Takashi Iwaic9700422013-01-18 10:17:30 +01002744/* fill the label for each input at first */
2745static int fill_input_pin_labels(struct hda_codec *codec)
2746{
2747 struct hda_gen_spec *spec = codec->spec;
2748 const struct auto_pin_cfg *cfg = &spec->autocfg;
2749 int i;
2750
2751 for (i = 0; i < cfg->num_inputs; i++) {
2752 hda_nid_t pin = cfg->inputs[i].pin;
2753 const char *label;
2754 int j, idx;
2755
2756 if (!is_input_pin(codec, pin))
2757 continue;
2758
2759 label = hda_get_autocfg_input_label(codec, cfg, i);
2760 idx = 0;
David Henningsson8e8db7f2013-01-18 15:43:02 +01002761 for (j = i - 1; j >= 0; j--) {
Takashi Iwaic9700422013-01-18 10:17:30 +01002762 if (spec->input_labels[j] &&
2763 !strcmp(spec->input_labels[j], label)) {
2764 idx = spec->input_label_idxs[j] + 1;
2765 break;
2766 }
2767 }
2768
2769 spec->input_labels[i] = label;
2770 spec->input_label_idxs[i] = idx;
2771 }
2772
2773 return 0;
2774}
2775
Takashi Iwai9dba2052013-01-18 10:01:15 +01002776#define CFG_IDX_MIX 99 /* a dummy cfg->input idx for stereo mix */
2777
Takashi Iwai352f7f92012-12-19 12:52:06 +01002778static int create_input_ctls(struct hda_codec *codec)
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02002779{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002780 struct hda_gen_spec *spec = codec->spec;
2781 const struct auto_pin_cfg *cfg = &spec->autocfg;
2782 hda_nid_t mixer = spec->mixer_nid;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002783 int num_adcs;
Takashi Iwaic9700422013-01-18 10:17:30 +01002784 int i, err;
Takashi Iwai2c12c302013-01-10 09:33:29 +01002785 unsigned int val;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02002786
Takashi Iwai352f7f92012-12-19 12:52:06 +01002787 num_adcs = fill_adc_nids(codec);
2788 if (num_adcs < 0)
2789 return 0;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02002790
Takashi Iwaic9700422013-01-18 10:17:30 +01002791 err = fill_input_pin_labels(codec);
2792 if (err < 0)
2793 return err;
2794
Takashi Iwai352f7f92012-12-19 12:52:06 +01002795 for (i = 0; i < cfg->num_inputs; i++) {
2796 hda_nid_t pin;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797
Takashi Iwai352f7f92012-12-19 12:52:06 +01002798 pin = cfg->inputs[i].pin;
2799 if (!is_input_pin(codec, pin))
2800 continue;
2801
Takashi Iwai2c12c302013-01-10 09:33:29 +01002802 val = PIN_IN;
2803 if (cfg->inputs[i].type == AUTO_PIN_MIC)
2804 val |= snd_hda_get_default_vref(codec, pin);
2805 set_pin_target(codec, pin, val, false);
2806
Takashi Iwai352f7f92012-12-19 12:52:06 +01002807 if (mixer) {
2808 if (is_reachable_path(codec, pin, mixer)) {
Takashi Iwai196c17662013-01-04 15:01:40 +01002809 err = new_analog_input(codec, i, pin,
Takashi Iwaic9700422013-01-18 10:17:30 +01002810 spec->input_labels[i],
2811 spec->input_label_idxs[i],
2812 mixer);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002813 if (err < 0)
2814 return err;
2815 }
2816 }
2817
Takashi Iwaic9700422013-01-18 10:17:30 +01002818 err = parse_capture_source(codec, pin, i, num_adcs,
2819 spec->input_labels[i], -mixer);
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002820 if (err < 0)
2821 return err;
Takashi Iwai294765582013-01-17 09:52:11 +01002822
2823 if (spec->add_in_jack_modes) {
2824 err = create_in_jack_mode(codec, pin);
2825 if (err < 0)
2826 return err;
2827 }
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002828 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002829
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002830 if (mixer && spec->add_stereo_mix_input) {
Takashi Iwai9dba2052013-01-18 10:01:15 +01002831 err = parse_capture_source(codec, mixer, CFG_IDX_MIX, num_adcs,
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002832 "Stereo Mix", 0);
2833 if (err < 0)
2834 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002835 }
2836
2837 return 0;
2838}
2839
2840
2841/*
2842 * input source mux
2843 */
2844
Takashi Iwaic697b712013-01-07 17:09:26 +01002845/* get the input path specified by the given adc and imux indices */
2846static struct nid_path *get_input_path(struct hda_codec *codec, int adc_idx, int imux_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002847{
2848 struct hda_gen_spec *spec = codec->spec;
David Henningssonb56fa1e2013-01-16 11:45:35 +01002849 if (imux_idx < 0 || imux_idx >= HDA_MAX_NUM_INPUTS) {
2850 snd_BUG();
2851 return NULL;
2852 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002853 if (spec->dyn_adc_switch)
2854 adc_idx = spec->dyn_adc_idx[imux_idx];
David Henningssond3d982f2013-01-18 15:43:01 +01002855 if (adc_idx < 0 || adc_idx >= AUTO_CFG_MAX_INS) {
David Henningssonb56fa1e2013-01-16 11:45:35 +01002856 snd_BUG();
2857 return NULL;
2858 }
Takashi Iwaic697b712013-01-07 17:09:26 +01002859 return snd_hda_get_path_from_idx(codec, spec->input_paths[imux_idx][adc_idx]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002860}
2861
2862static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
2863 unsigned int idx);
2864
2865static int mux_enum_info(struct snd_kcontrol *kcontrol,
2866 struct snd_ctl_elem_info *uinfo)
2867{
2868 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2869 struct hda_gen_spec *spec = codec->spec;
2870 return snd_hda_input_mux_info(&spec->input_mux, uinfo);
2871}
2872
2873static int mux_enum_get(struct snd_kcontrol *kcontrol,
2874 struct snd_ctl_elem_value *ucontrol)
2875{
2876 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2877 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai2a8d5392013-01-18 16:23:25 +01002878 /* the ctls are created at once with multiple counts */
2879 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002880
2881 ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
2882 return 0;
2883}
2884
2885static int mux_enum_put(struct snd_kcontrol *kcontrol,
2886 struct snd_ctl_elem_value *ucontrol)
2887{
2888 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Takashi Iwai2a8d5392013-01-18 16:23:25 +01002889 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002890 return mux_select(codec, adc_idx,
2891 ucontrol->value.enumerated.item[0]);
2892}
2893
Takashi Iwai352f7f92012-12-19 12:52:06 +01002894static const struct snd_kcontrol_new cap_src_temp = {
2895 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2896 .name = "Input Source",
2897 .info = mux_enum_info,
2898 .get = mux_enum_get,
2899 .put = mux_enum_put,
2900};
2901
Takashi Iwai47d46ab2012-12-20 11:48:54 +01002902/*
2903 * capture volume and capture switch ctls
2904 */
2905
Takashi Iwai352f7f92012-12-19 12:52:06 +01002906typedef int (*put_call_t)(struct snd_kcontrol *kcontrol,
2907 struct snd_ctl_elem_value *ucontrol);
2908
Takashi Iwai47d46ab2012-12-20 11:48:54 +01002909/* call the given amp update function for all amps in the imux list at once */
Takashi Iwai352f7f92012-12-19 12:52:06 +01002910static int cap_put_caller(struct snd_kcontrol *kcontrol,
2911 struct snd_ctl_elem_value *ucontrol,
2912 put_call_t func, int type)
2913{
2914 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2915 struct hda_gen_spec *spec = codec->spec;
2916 const struct hda_input_mux *imux;
2917 struct nid_path *path;
2918 int i, adc_idx, err = 0;
2919
2920 imux = &spec->input_mux;
David Henningssona053d1e2013-01-16 11:45:36 +01002921 adc_idx = kcontrol->id.index;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002922 mutex_lock(&codec->control_mutex);
Takashi Iwai47d46ab2012-12-20 11:48:54 +01002923 /* we use the cache-only update at first since multiple input paths
2924 * may shared the same amp; by updating only caches, the redundant
2925 * writes to hardware can be reduced.
2926 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01002927 codec->cached_write = 1;
2928 for (i = 0; i < imux->num_items; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01002929 path = get_input_path(codec, adc_idx, i);
2930 if (!path || !path->ctls[type])
Takashi Iwai352f7f92012-12-19 12:52:06 +01002931 continue;
2932 kcontrol->private_value = path->ctls[type];
2933 err = func(kcontrol, ucontrol);
2934 if (err < 0)
2935 goto error;
2936 }
2937 error:
2938 codec->cached_write = 0;
2939 mutex_unlock(&codec->control_mutex);
Takashi Iwaidc870f32013-01-22 15:24:30 +01002940 snd_hda_codec_flush_cache(codec); /* flush the updates */
Takashi Iwai352f7f92012-12-19 12:52:06 +01002941 if (err >= 0 && spec->cap_sync_hook)
Takashi Iwaia90229e2013-01-18 14:10:00 +01002942 spec->cap_sync_hook(codec, ucontrol);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002943 return err;
2944}
2945
2946/* capture volume ctl callbacks */
2947#define cap_vol_info snd_hda_mixer_amp_volume_info
2948#define cap_vol_get snd_hda_mixer_amp_volume_get
2949#define cap_vol_tlv snd_hda_mixer_amp_tlv
2950
2951static int cap_vol_put(struct snd_kcontrol *kcontrol,
2952 struct snd_ctl_elem_value *ucontrol)
2953{
2954 return cap_put_caller(kcontrol, ucontrol,
2955 snd_hda_mixer_amp_volume_put,
2956 NID_PATH_VOL_CTL);
2957}
2958
2959static const struct snd_kcontrol_new cap_vol_temp = {
2960 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2961 .name = "Capture Volume",
2962 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
2963 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
2964 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK),
2965 .info = cap_vol_info,
2966 .get = cap_vol_get,
2967 .put = cap_vol_put,
2968 .tlv = { .c = cap_vol_tlv },
2969};
2970
2971/* capture switch ctl callbacks */
2972#define cap_sw_info snd_ctl_boolean_stereo_info
2973#define cap_sw_get snd_hda_mixer_amp_switch_get
2974
2975static int cap_sw_put(struct snd_kcontrol *kcontrol,
2976 struct snd_ctl_elem_value *ucontrol)
2977{
Takashi Iwaia90229e2013-01-18 14:10:00 +01002978 return cap_put_caller(kcontrol, ucontrol,
Takashi Iwai352f7f92012-12-19 12:52:06 +01002979 snd_hda_mixer_amp_switch_put,
2980 NID_PATH_MUTE_CTL);
2981}
2982
2983static const struct snd_kcontrol_new cap_sw_temp = {
2984 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2985 .name = "Capture Switch",
2986 .info = cap_sw_info,
2987 .get = cap_sw_get,
2988 .put = cap_sw_put,
2989};
2990
2991static int parse_capvol_in_path(struct hda_codec *codec, struct nid_path *path)
2992{
2993 hda_nid_t nid;
2994 int i, depth;
2995
2996 path->ctls[NID_PATH_VOL_CTL] = path->ctls[NID_PATH_MUTE_CTL] = 0;
2997 for (depth = 0; depth < 3; depth++) {
2998 if (depth >= path->depth)
2999 return -EINVAL;
3000 i = path->depth - depth - 1;
3001 nid = path->path[i];
3002 if (!path->ctls[NID_PATH_VOL_CTL]) {
3003 if (nid_has_volume(codec, nid, HDA_OUTPUT))
3004 path->ctls[NID_PATH_VOL_CTL] =
3005 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3006 else if (nid_has_volume(codec, nid, HDA_INPUT)) {
3007 int idx = path->idx[i];
3008 if (!depth && codec->single_adc_amp)
3009 idx = 0;
3010 path->ctls[NID_PATH_VOL_CTL] =
3011 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
3012 }
3013 }
3014 if (!path->ctls[NID_PATH_MUTE_CTL]) {
3015 if (nid_has_mute(codec, nid, HDA_OUTPUT))
3016 path->ctls[NID_PATH_MUTE_CTL] =
3017 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3018 else if (nid_has_mute(codec, nid, HDA_INPUT)) {
3019 int idx = path->idx[i];
3020 if (!depth && codec->single_adc_amp)
3021 idx = 0;
3022 path->ctls[NID_PATH_MUTE_CTL] =
3023 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
3024 }
3025 }
Takashi Iwai97ec5582006-03-21 11:29:07 +01003026 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027 return 0;
3028}
3029
Takashi Iwai352f7f92012-12-19 12:52:06 +01003030static bool is_inv_dmic_pin(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003032 struct hda_gen_spec *spec = codec->spec;
3033 struct auto_pin_cfg *cfg = &spec->autocfg;
3034 unsigned int val;
3035 int i;
3036
3037 if (!spec->inv_dmic_split)
3038 return false;
3039 for (i = 0; i < cfg->num_inputs; i++) {
3040 if (cfg->inputs[i].pin != nid)
3041 continue;
3042 if (cfg->inputs[i].type != AUTO_PIN_MIC)
3043 return false;
3044 val = snd_hda_codec_get_pincfg(codec, nid);
3045 return snd_hda_get_input_pin_attr(val) == INPUT_PIN_ATTR_INT;
3046 }
3047 return false;
3048}
3049
Takashi Iwaia90229e2013-01-18 14:10:00 +01003050/* capture switch put callback for a single control with hook call */
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003051static int cap_single_sw_put(struct snd_kcontrol *kcontrol,
3052 struct snd_ctl_elem_value *ucontrol)
3053{
3054 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3055 struct hda_gen_spec *spec = codec->spec;
3056 int ret;
3057
3058 ret = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
3059 if (ret < 0)
3060 return ret;
3061
Takashi Iwaia90229e2013-01-18 14:10:00 +01003062 if (spec->cap_sync_hook)
3063 spec->cap_sync_hook(codec, ucontrol);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003064
3065 return ret;
3066}
3067
Takashi Iwai352f7f92012-12-19 12:52:06 +01003068static int add_single_cap_ctl(struct hda_codec *codec, const char *label,
3069 int idx, bool is_switch, unsigned int ctl,
3070 bool inv_dmic)
3071{
3072 struct hda_gen_spec *spec = codec->spec;
3073 char tmpname[44];
3074 int type = is_switch ? HDA_CTL_WIDGET_MUTE : HDA_CTL_WIDGET_VOL;
3075 const char *sfx = is_switch ? "Switch" : "Volume";
3076 unsigned int chs = inv_dmic ? 1 : 3;
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003077 struct snd_kcontrol_new *knew;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003078
3079 if (!ctl)
3080 return 0;
3081
3082 if (label)
3083 snprintf(tmpname, sizeof(tmpname),
3084 "%s Capture %s", label, sfx);
3085 else
3086 snprintf(tmpname, sizeof(tmpname),
3087 "Capture %s", sfx);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003088 knew = add_control(spec, type, tmpname, idx,
3089 amp_val_replace_channels(ctl, chs));
3090 if (!knew)
3091 return -ENOMEM;
Takashi Iwaia90229e2013-01-18 14:10:00 +01003092 if (is_switch)
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003093 knew->put = cap_single_sw_put;
3094 if (!inv_dmic)
3095 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003096
3097 /* Make independent right kcontrol */
3098 if (label)
3099 snprintf(tmpname, sizeof(tmpname),
3100 "Inverted %s Capture %s", label, sfx);
3101 else
3102 snprintf(tmpname, sizeof(tmpname),
3103 "Inverted Capture %s", sfx);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003104 knew = add_control(spec, type, tmpname, idx,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003105 amp_val_replace_channels(ctl, 2));
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003106 if (!knew)
3107 return -ENOMEM;
Takashi Iwaia90229e2013-01-18 14:10:00 +01003108 if (is_switch)
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003109 knew->put = cap_single_sw_put;
3110 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003111}
3112
3113/* create single (and simple) capture volume and switch controls */
3114static int create_single_cap_vol_ctl(struct hda_codec *codec, int idx,
3115 unsigned int vol_ctl, unsigned int sw_ctl,
3116 bool inv_dmic)
3117{
3118 int err;
3119 err = add_single_cap_ctl(codec, NULL, idx, false, vol_ctl, inv_dmic);
3120 if (err < 0)
3121 return err;
3122 err = add_single_cap_ctl(codec, NULL, idx, true, sw_ctl, inv_dmic);
3123 if (err < 0)
3124 return err;
3125 return 0;
3126}
3127
3128/* create bound capture volume and switch controls */
3129static int create_bind_cap_vol_ctl(struct hda_codec *codec, int idx,
3130 unsigned int vol_ctl, unsigned int sw_ctl)
3131{
3132 struct hda_gen_spec *spec = codec->spec;
3133 struct snd_kcontrol_new *knew;
3134
3135 if (vol_ctl) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01003136 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_vol_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003137 if (!knew)
3138 return -ENOMEM;
3139 knew->index = idx;
3140 knew->private_value = vol_ctl;
3141 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
3142 }
3143 if (sw_ctl) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01003144 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_sw_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003145 if (!knew)
3146 return -ENOMEM;
3147 knew->index = idx;
3148 knew->private_value = sw_ctl;
3149 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
3150 }
3151 return 0;
3152}
3153
3154/* return the vol ctl when used first in the imux list */
3155static unsigned int get_first_cap_ctl(struct hda_codec *codec, int idx, int type)
3156{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003157 struct nid_path *path;
3158 unsigned int ctl;
3159 int i;
3160
Takashi Iwaic697b712013-01-07 17:09:26 +01003161 path = get_input_path(codec, 0, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003162 if (!path)
3163 return 0;
3164 ctl = path->ctls[type];
3165 if (!ctl)
3166 return 0;
3167 for (i = 0; i < idx - 1; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01003168 path = get_input_path(codec, 0, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003169 if (path && path->ctls[type] == ctl)
3170 return 0;
3171 }
3172 return ctl;
3173}
3174
3175/* create individual capture volume and switch controls per input */
3176static int create_multi_cap_vol_ctl(struct hda_codec *codec)
3177{
3178 struct hda_gen_spec *spec = codec->spec;
3179 struct hda_input_mux *imux = &spec->input_mux;
Takashi Iwaic9700422013-01-18 10:17:30 +01003180 int i, err, type;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003181
3182 for (i = 0; i < imux->num_items; i++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003183 bool inv_dmic;
Takashi Iwaic9700422013-01-18 10:17:30 +01003184 int idx;
Takashi Iwai9dba2052013-01-18 10:01:15 +01003185
Takashi Iwaic9700422013-01-18 10:17:30 +01003186 idx = imux->items[i].index;
3187 if (idx >= spec->autocfg.num_inputs)
Takashi Iwai9dba2052013-01-18 10:01:15 +01003188 continue;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003189 inv_dmic = is_inv_dmic_pin(codec, spec->imux_pins[i]);
3190
3191 for (type = 0; type < 2; type++) {
Takashi Iwaic9700422013-01-18 10:17:30 +01003192 err = add_single_cap_ctl(codec,
3193 spec->input_labels[idx],
3194 spec->input_label_idxs[idx],
3195 type,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003196 get_first_cap_ctl(codec, i, type),
3197 inv_dmic);
3198 if (err < 0)
3199 return err;
3200 }
3201 }
3202 return 0;
3203}
3204
3205static int create_capture_mixers(struct hda_codec *codec)
3206{
3207 struct hda_gen_spec *spec = codec->spec;
3208 struct hda_input_mux *imux = &spec->input_mux;
3209 int i, n, nums, err;
3210
3211 if (spec->dyn_adc_switch)
3212 nums = 1;
3213 else
3214 nums = spec->num_adc_nids;
3215
3216 if (!spec->auto_mic && imux->num_items > 1) {
3217 struct snd_kcontrol_new *knew;
Takashi Iwai624d9142012-12-19 17:41:52 +01003218 const char *name;
3219 name = nums > 1 ? "Input Source" : "Capture Source";
3220 knew = snd_hda_gen_add_kctl(spec, name, &cap_src_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003221 if (!knew)
3222 return -ENOMEM;
3223 knew->count = nums;
3224 }
3225
3226 for (n = 0; n < nums; n++) {
3227 bool multi = false;
David Henningsson99a55922013-01-16 15:58:44 +01003228 bool multi_cap_vol = spec->multi_cap_vol;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003229 bool inv_dmic = false;
3230 int vol, sw;
3231
3232 vol = sw = 0;
3233 for (i = 0; i < imux->num_items; i++) {
3234 struct nid_path *path;
Takashi Iwaic697b712013-01-07 17:09:26 +01003235 path = get_input_path(codec, n, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003236 if (!path)
3237 continue;
3238 parse_capvol_in_path(codec, path);
3239 if (!vol)
3240 vol = path->ctls[NID_PATH_VOL_CTL];
David Henningsson99a55922013-01-16 15:58:44 +01003241 else if (vol != path->ctls[NID_PATH_VOL_CTL]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003242 multi = true;
David Henningsson99a55922013-01-16 15:58:44 +01003243 if (!same_amp_caps(codec, vol,
3244 path->ctls[NID_PATH_VOL_CTL], HDA_INPUT))
3245 multi_cap_vol = true;
3246 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003247 if (!sw)
3248 sw = path->ctls[NID_PATH_MUTE_CTL];
David Henningsson99a55922013-01-16 15:58:44 +01003249 else if (sw != path->ctls[NID_PATH_MUTE_CTL]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003250 multi = true;
David Henningsson99a55922013-01-16 15:58:44 +01003251 if (!same_amp_caps(codec, sw,
3252 path->ctls[NID_PATH_MUTE_CTL], HDA_INPUT))
3253 multi_cap_vol = true;
3254 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003255 if (is_inv_dmic_pin(codec, spec->imux_pins[i]))
3256 inv_dmic = true;
3257 }
3258
3259 if (!multi)
3260 err = create_single_cap_vol_ctl(codec, n, vol, sw,
3261 inv_dmic);
David Henningsson99a55922013-01-16 15:58:44 +01003262 else if (!multi_cap_vol)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003263 err = create_bind_cap_vol_ctl(codec, n, vol, sw);
3264 else
3265 err = create_multi_cap_vol_ctl(codec);
3266 if (err < 0)
3267 return err;
3268 }
3269
3270 return 0;
3271}
3272
3273/*
3274 * add mic boosts if needed
3275 */
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003276
3277/* check whether the given amp is feasible as a boost volume */
3278static bool check_boost_vol(struct hda_codec *codec, hda_nid_t nid,
3279 int dir, int idx)
3280{
3281 unsigned int step;
3282
3283 if (!nid_has_volume(codec, nid, dir) ||
3284 is_ctl_associated(codec, nid, dir, idx, NID_PATH_VOL_CTL) ||
3285 is_ctl_associated(codec, nid, dir, idx, NID_PATH_BOOST_CTL))
3286 return false;
3287
3288 step = (query_amp_caps(codec, nid, dir) & AC_AMPCAP_STEP_SIZE)
3289 >> AC_AMPCAP_STEP_SIZE_SHIFT;
3290 if (step < 0x20)
3291 return false;
3292 return true;
3293}
3294
3295/* look for a boost amp in a widget close to the pin */
3296static unsigned int look_for_boost_amp(struct hda_codec *codec,
3297 struct nid_path *path)
3298{
3299 unsigned int val = 0;
3300 hda_nid_t nid;
3301 int depth;
3302
3303 for (depth = 0; depth < 3; depth++) {
3304 if (depth >= path->depth - 1)
3305 break;
3306 nid = path->path[depth];
3307 if (depth && check_boost_vol(codec, nid, HDA_OUTPUT, 0)) {
3308 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3309 break;
3310 } else if (check_boost_vol(codec, nid, HDA_INPUT,
3311 path->idx[depth])) {
3312 val = HDA_COMPOSE_AMP_VAL(nid, 3, path->idx[depth],
3313 HDA_INPUT);
3314 break;
3315 }
3316 }
3317
3318 return val;
3319}
3320
Takashi Iwai352f7f92012-12-19 12:52:06 +01003321static int parse_mic_boost(struct hda_codec *codec)
3322{
3323 struct hda_gen_spec *spec = codec->spec;
3324 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003325 struct hda_input_mux *imux = &spec->input_mux;
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003326 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003327
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003328 if (!spec->num_adc_nids)
3329 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003330
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003331 for (i = 0; i < imux->num_items; i++) {
3332 struct nid_path *path;
3333 unsigned int val;
3334 int idx;
3335 char boost_label[44];
David Henningsson02aba552013-01-16 15:58:43 +01003336
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003337 idx = imux->items[i].index;
3338 if (idx >= imux->num_items)
3339 continue;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003340
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003341 /* check only line-in and mic pins */
Takashi Iwai1799cdd52013-01-18 14:37:16 +01003342 if (cfg->inputs[idx].type > AUTO_PIN_LINE_IN)
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003343 continue;
3344
3345 path = get_input_path(codec, 0, i);
3346 if (!path)
3347 continue;
3348
3349 val = look_for_boost_amp(codec, path);
3350 if (!val)
3351 continue;
3352
3353 /* create a boost control */
3354 snprintf(boost_label, sizeof(boost_label),
3355 "%s Boost Volume", spec->input_labels[idx]);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003356 if (!add_control(spec, HDA_CTL_WIDGET_VOL, boost_label,
3357 spec->input_label_idxs[idx], val))
3358 return -ENOMEM;
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003359
3360 path->ctls[NID_PATH_BOOST_CTL] = val;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003361 }
3362 return 0;
3363}
3364
3365/*
3366 * parse digital I/Os and set up NIDs in BIOS auto-parse mode
3367 */
3368static void parse_digital(struct hda_codec *codec)
3369{
3370 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003371 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003372 int i, nums;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003373 hda_nid_t dig_nid, pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003374
3375 /* support multiple SPDIFs; the secondary is set up as a slave */
3376 nums = 0;
3377 for (i = 0; i < spec->autocfg.dig_outs; i++) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01003378 pin = spec->autocfg.dig_out_pins[i];
Takashi Iwai352f7f92012-12-19 12:52:06 +01003379 dig_nid = look_for_dac(codec, pin, true);
3380 if (!dig_nid)
3381 continue;
Takashi Iwai3ca529d2013-01-07 17:25:08 +01003382 path = snd_hda_add_new_path(codec, dig_nid, pin, 0);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003383 if (!path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003384 continue;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003385 print_nid_path("digout", path);
Takashi Iwaie1284af2013-01-03 16:33:02 +01003386 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +01003387 spec->digout_paths[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai2c12c302013-01-10 09:33:29 +01003388 set_pin_target(codec, pin, PIN_OUT, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003389 if (!nums) {
3390 spec->multiout.dig_out_nid = dig_nid;
3391 spec->dig_out_type = spec->autocfg.dig_out_type[0];
3392 } else {
3393 spec->multiout.slave_dig_outs = spec->slave_dig_outs;
3394 if (nums >= ARRAY_SIZE(spec->slave_dig_outs) - 1)
3395 break;
3396 spec->slave_dig_outs[nums - 1] = dig_nid;
3397 }
3398 nums++;
3399 }
3400
3401 if (spec->autocfg.dig_in_pin) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01003402 pin = spec->autocfg.dig_in_pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003403 dig_nid = codec->start_nid;
3404 for (i = 0; i < codec->num_nodes; i++, dig_nid++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003405 unsigned int wcaps = get_wcaps(codec, dig_nid);
3406 if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
3407 continue;
3408 if (!(wcaps & AC_WCAP_DIGITAL))
3409 continue;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003410 path = snd_hda_add_new_path(codec, pin, dig_nid, 0);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003411 if (path) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003412 print_nid_path("digin", path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003413 path->active = true;
3414 spec->dig_in_nid = dig_nid;
Takashi Iwai2430d7b2013-01-04 15:09:42 +01003415 spec->digin_path = snd_hda_get_path_idx(codec, path);
Takashi Iwai2c12c302013-01-10 09:33:29 +01003416 set_pin_target(codec, pin, PIN_IN, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003417 break;
3418 }
3419 }
3420 }
3421}
3422
3423
3424/*
3425 * input MUX handling
3426 */
3427
3428static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur);
3429
3430/* select the given imux item; either unmute exclusively or select the route */
3431static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
3432 unsigned int idx)
3433{
3434 struct hda_gen_spec *spec = codec->spec;
3435 const struct hda_input_mux *imux;
Takashi Iwai55196ff2013-01-24 17:32:56 +01003436 struct nid_path *old_path, *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003437
3438 imux = &spec->input_mux;
3439 if (!imux->num_items)
3440 return 0;
3441
3442 if (idx >= imux->num_items)
3443 idx = imux->num_items - 1;
3444 if (spec->cur_mux[adc_idx] == idx)
3445 return 0;
3446
Takashi Iwai55196ff2013-01-24 17:32:56 +01003447 old_path = get_input_path(codec, adc_idx, spec->cur_mux[adc_idx]);
3448 if (!old_path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003449 return 0;
Takashi Iwai55196ff2013-01-24 17:32:56 +01003450 if (old_path->active)
3451 snd_hda_activate_path(codec, old_path, false, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003452
3453 spec->cur_mux[adc_idx] = idx;
3454
Takashi Iwai967303d2013-02-19 17:12:42 +01003455 if (spec->hp_mic)
3456 update_hp_mic(codec, adc_idx, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003457
3458 if (spec->dyn_adc_switch)
3459 dyn_adc_pcm_resetup(codec, idx);
3460
Takashi Iwaic697b712013-01-07 17:09:26 +01003461 path = get_input_path(codec, adc_idx, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003462 if (!path)
3463 return 0;
3464 if (path->active)
3465 return 0;
3466 snd_hda_activate_path(codec, path, true, false);
3467 if (spec->cap_sync_hook)
Takashi Iwaia90229e2013-01-18 14:10:00 +01003468 spec->cap_sync_hook(codec, NULL);
Takashi Iwai55196ff2013-01-24 17:32:56 +01003469 path_power_down_sync(codec, old_path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003470 return 1;
3471}
3472
3473
3474/*
3475 * Jack detections for HP auto-mute and mic-switch
3476 */
3477
3478/* check each pin in the given array; returns true if any of them is plugged */
3479static bool detect_jacks(struct hda_codec *codec, int num_pins, hda_nid_t *pins)
3480{
3481 int i, present = 0;
3482
3483 for (i = 0; i < num_pins; i++) {
3484 hda_nid_t nid = pins[i];
3485 if (!nid)
3486 break;
Takashi Iwai0b4df932013-01-10 09:45:13 +01003487 /* don't detect pins retasked as inputs */
3488 if (snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_IN_EN)
3489 continue;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003490 present |= snd_hda_jack_detect(codec, nid);
3491 }
3492 return present;
3493}
3494
3495/* standard HP/line-out auto-mute helper */
3496static void do_automute(struct hda_codec *codec, int num_pins, hda_nid_t *pins,
Takashi Iwai2c12c302013-01-10 09:33:29 +01003497 bool mute)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003498{
3499 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003500 int i;
3501
3502 for (i = 0; i < num_pins; i++) {
3503 hda_nid_t nid = pins[i];
Takashi Iwai967303d2013-02-19 17:12:42 +01003504 unsigned int val, oldval;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003505 if (!nid)
3506 break;
Takashi Iwai967303d2013-02-19 17:12:42 +01003507 oldval = snd_hda_codec_get_pin_target(codec, nid);
3508 if (oldval & PIN_IN)
3509 continue; /* no mute for inputs */
Takashi Iwai352f7f92012-12-19 12:52:06 +01003510 /* don't reset VREF value in case it's controlling
3511 * the amp (see alc861_fixup_asus_amp_vref_0f())
3512 */
Takashi Iwai2c12c302013-01-10 09:33:29 +01003513 if (spec->keep_vref_in_automute)
Takashi Iwai967303d2013-02-19 17:12:42 +01003514 val = oldval & ~PIN_HP;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003515 else
Takashi Iwai352f7f92012-12-19 12:52:06 +01003516 val = 0;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003517 if (!mute)
Takashi Iwai967303d2013-02-19 17:12:42 +01003518 val |= oldval;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003519 /* here we call update_pin_ctl() so that the pinctl is changed
3520 * without changing the pinctl target value;
3521 * the original target value will be still referred at the
3522 * init / resume again
3523 */
3524 update_pin_ctl(codec, nid, val);
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01003525 set_pin_eapd(codec, nid, !mute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003526 }
3527}
3528
3529/* Toggle outputs muting */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003530void snd_hda_gen_update_outputs(struct hda_codec *codec)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003531{
3532 struct hda_gen_spec *spec = codec->spec;
3533 int on;
3534
3535 /* Control HP pins/amps depending on master_mute state;
3536 * in general, HP pins/amps control should be enabled in all cases,
3537 * but currently set only for master_mute, just to be safe
3538 */
Takashi Iwai967303d2013-02-19 17:12:42 +01003539 do_automute(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
Takashi Iwai2c12c302013-01-10 09:33:29 +01003540 spec->autocfg.hp_pins, spec->master_mute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003541
3542 if (!spec->automute_speaker)
3543 on = 0;
3544 else
3545 on = spec->hp_jack_present | spec->line_jack_present;
3546 on |= spec->master_mute;
Takashi Iwai47b9ddb2013-01-16 18:18:00 +01003547 spec->speaker_muted = on;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003548 do_automute(codec, ARRAY_SIZE(spec->autocfg.speaker_pins),
Takashi Iwai2c12c302013-01-10 09:33:29 +01003549 spec->autocfg.speaker_pins, on);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003550
3551 /* toggle line-out mutes if needed, too */
3552 /* if LO is a copy of either HP or Speaker, don't need to handle it */
3553 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0] ||
3554 spec->autocfg.line_out_pins[0] == spec->autocfg.speaker_pins[0])
3555 return;
3556 if (!spec->automute_lo)
3557 on = 0;
3558 else
3559 on = spec->hp_jack_present;
3560 on |= spec->master_mute;
Takashi Iwai47b9ddb2013-01-16 18:18:00 +01003561 spec->line_out_muted = on;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003562 do_automute(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
Takashi Iwai2c12c302013-01-10 09:33:29 +01003563 spec->autocfg.line_out_pins, on);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003564}
Takashi Iwai5d550e12012-12-19 15:16:44 +01003565EXPORT_SYMBOL_HDA(snd_hda_gen_update_outputs);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003566
3567static void call_update_outputs(struct hda_codec *codec)
3568{
3569 struct hda_gen_spec *spec = codec->spec;
3570 if (spec->automute_hook)
3571 spec->automute_hook(codec);
3572 else
Takashi Iwai5d550e12012-12-19 15:16:44 +01003573 snd_hda_gen_update_outputs(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003574}
3575
3576/* standard HP-automute helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003577void snd_hda_gen_hp_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003578{
3579 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai92603c52013-01-22 07:46:31 +01003580 hda_nid_t *pins = spec->autocfg.hp_pins;
3581 int num_pins = ARRAY_SIZE(spec->autocfg.hp_pins);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003582
Takashi Iwai92603c52013-01-22 07:46:31 +01003583 /* No detection for the first HP jack during indep-HP mode */
3584 if (spec->indep_hp_enabled) {
3585 pins++;
3586 num_pins--;
3587 }
3588
3589 spec->hp_jack_present = detect_jacks(codec, num_pins, pins);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003590 if (!spec->detect_hp || (!spec->automute_speaker && !spec->automute_lo))
3591 return;
3592 call_update_outputs(codec);
3593}
Takashi Iwai5d550e12012-12-19 15:16:44 +01003594EXPORT_SYMBOL_HDA(snd_hda_gen_hp_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003595
3596/* standard line-out-automute helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003597void snd_hda_gen_line_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003598{
3599 struct hda_gen_spec *spec = codec->spec;
3600
3601 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
3602 return;
3603 /* check LO jack only when it's different from HP */
3604 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0])
3605 return;
3606
3607 spec->line_jack_present =
3608 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
3609 spec->autocfg.line_out_pins);
3610 if (!spec->automute_speaker || !spec->detect_lo)
3611 return;
3612 call_update_outputs(codec);
3613}
Takashi Iwai5d550e12012-12-19 15:16:44 +01003614EXPORT_SYMBOL_HDA(snd_hda_gen_line_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003615
3616/* standard mic auto-switch helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003617void snd_hda_gen_mic_autoswitch(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003618{
3619 struct hda_gen_spec *spec = codec->spec;
3620 int i;
3621
3622 if (!spec->auto_mic)
3623 return;
3624
3625 for (i = spec->am_num_entries - 1; i > 0; i--) {
Takashi Iwai0b4df932013-01-10 09:45:13 +01003626 hda_nid_t pin = spec->am_entry[i].pin;
3627 /* don't detect pins retasked as outputs */
3628 if (snd_hda_codec_get_pin_target(codec, pin) & AC_PINCTL_OUT_EN)
3629 continue;
3630 if (snd_hda_jack_detect(codec, pin)) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003631 mux_select(codec, 0, spec->am_entry[i].idx);
3632 return;
3633 }
3634 }
3635 mux_select(codec, 0, spec->am_entry[0].idx);
3636}
Takashi Iwai5d550e12012-12-19 15:16:44 +01003637EXPORT_SYMBOL_HDA(snd_hda_gen_mic_autoswitch);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003638
Takashi Iwaia5cc2502013-01-16 18:08:55 +01003639/* update jack retasking */
3640static void update_automute_all(struct hda_codec *codec)
3641{
3642 struct hda_gen_spec *spec = codec->spec;
3643
3644 if (spec->hp_automute_hook)
3645 spec->hp_automute_hook(codec, NULL);
3646 else
3647 snd_hda_gen_hp_automute(codec, NULL);
3648 if (spec->line_automute_hook)
3649 spec->line_automute_hook(codec, NULL);
3650 else
3651 snd_hda_gen_line_automute(codec, NULL);
3652 if (spec->mic_autoswitch_hook)
3653 spec->mic_autoswitch_hook(codec, NULL);
3654 else
3655 snd_hda_gen_mic_autoswitch(codec, NULL);
3656}
3657
Takashi Iwai352f7f92012-12-19 12:52:06 +01003658/*
3659 * Auto-Mute mode mixer enum support
3660 */
3661static int automute_mode_info(struct snd_kcontrol *kcontrol,
3662 struct snd_ctl_elem_info *uinfo)
3663{
3664 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3665 struct hda_gen_spec *spec = codec->spec;
3666 static const char * const texts3[] = {
3667 "Disabled", "Speaker Only", "Line Out+Speaker"
Takashi Iwai071c73a2006-08-23 18:34:06 +02003668 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003669
Takashi Iwai352f7f92012-12-19 12:52:06 +01003670 if (spec->automute_speaker_possible && spec->automute_lo_possible)
3671 return snd_hda_enum_helper_info(kcontrol, uinfo, 3, texts3);
3672 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
3673}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003674
Takashi Iwai352f7f92012-12-19 12:52:06 +01003675static int automute_mode_get(struct snd_kcontrol *kcontrol,
3676 struct snd_ctl_elem_value *ucontrol)
3677{
3678 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3679 struct hda_gen_spec *spec = codec->spec;
3680 unsigned int val = 0;
3681 if (spec->automute_speaker)
3682 val++;
3683 if (spec->automute_lo)
3684 val++;
Takashi Iwai071c73a2006-08-23 18:34:06 +02003685
Takashi Iwai352f7f92012-12-19 12:52:06 +01003686 ucontrol->value.enumerated.item[0] = val;
3687 return 0;
3688}
3689
3690static int automute_mode_put(struct snd_kcontrol *kcontrol,
3691 struct snd_ctl_elem_value *ucontrol)
3692{
3693 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3694 struct hda_gen_spec *spec = codec->spec;
3695
3696 switch (ucontrol->value.enumerated.item[0]) {
3697 case 0:
3698 if (!spec->automute_speaker && !spec->automute_lo)
3699 return 0;
3700 spec->automute_speaker = 0;
3701 spec->automute_lo = 0;
3702 break;
3703 case 1:
3704 if (spec->automute_speaker_possible) {
3705 if (!spec->automute_lo && spec->automute_speaker)
3706 return 0;
3707 spec->automute_speaker = 1;
3708 spec->automute_lo = 0;
3709 } else if (spec->automute_lo_possible) {
3710 if (spec->automute_lo)
3711 return 0;
3712 spec->automute_lo = 1;
3713 } else
3714 return -EINVAL;
3715 break;
3716 case 2:
3717 if (!spec->automute_lo_possible || !spec->automute_speaker_possible)
3718 return -EINVAL;
3719 if (spec->automute_speaker && spec->automute_lo)
3720 return 0;
3721 spec->automute_speaker = 1;
3722 spec->automute_lo = 1;
3723 break;
3724 default:
3725 return -EINVAL;
3726 }
3727 call_update_outputs(codec);
3728 return 1;
3729}
3730
3731static const struct snd_kcontrol_new automute_mode_enum = {
3732 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3733 .name = "Auto-Mute Mode",
3734 .info = automute_mode_info,
3735 .get = automute_mode_get,
3736 .put = automute_mode_put,
3737};
3738
3739static int add_automute_mode_enum(struct hda_codec *codec)
3740{
3741 struct hda_gen_spec *spec = codec->spec;
3742
Takashi Iwai12c93df2012-12-19 14:38:33 +01003743 if (!snd_hda_gen_add_kctl(spec, NULL, &automute_mode_enum))
Takashi Iwai352f7f92012-12-19 12:52:06 +01003744 return -ENOMEM;
3745 return 0;
3746}
3747
3748/*
3749 * Check the availability of HP/line-out auto-mute;
3750 * Set up appropriately if really supported
3751 */
3752static int check_auto_mute_availability(struct hda_codec *codec)
3753{
3754 struct hda_gen_spec *spec = codec->spec;
3755 struct auto_pin_cfg *cfg = &spec->autocfg;
3756 int present = 0;
3757 int i, err;
3758
Takashi Iwaif72706b2013-01-16 18:20:07 +01003759 if (spec->suppress_auto_mute)
3760 return 0;
3761
Takashi Iwai352f7f92012-12-19 12:52:06 +01003762 if (cfg->hp_pins[0])
3763 present++;
3764 if (cfg->line_out_pins[0])
3765 present++;
3766 if (cfg->speaker_pins[0])
3767 present++;
3768 if (present < 2) /* need two different output types */
Takashi Iwai071c73a2006-08-23 18:34:06 +02003769 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003770
3771 if (!cfg->speaker_pins[0] &&
3772 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
3773 memcpy(cfg->speaker_pins, cfg->line_out_pins,
3774 sizeof(cfg->speaker_pins));
3775 cfg->speaker_outs = cfg->line_outs;
Takashi Iwai071c73a2006-08-23 18:34:06 +02003776 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003777
Takashi Iwai352f7f92012-12-19 12:52:06 +01003778 if (!cfg->hp_pins[0] &&
3779 cfg->line_out_type == AUTO_PIN_HP_OUT) {
3780 memcpy(cfg->hp_pins, cfg->line_out_pins,
3781 sizeof(cfg->hp_pins));
3782 cfg->hp_outs = cfg->line_outs;
3783 }
3784
3785 for (i = 0; i < cfg->hp_outs; i++) {
3786 hda_nid_t nid = cfg->hp_pins[i];
3787 if (!is_jack_detectable(codec, nid))
3788 continue;
3789 snd_printdd("hda-codec: Enable HP auto-muting on NID 0x%x\n",
3790 nid);
3791 snd_hda_jack_detect_enable_callback(codec, nid, HDA_GEN_HP_EVENT,
Takashi Iwai2e03e952013-01-03 15:55:06 +01003792 spec->hp_automute_hook ?
3793 spec->hp_automute_hook :
Takashi Iwai5d550e12012-12-19 15:16:44 +01003794 snd_hda_gen_hp_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003795 spec->detect_hp = 1;
3796 }
3797
3798 if (cfg->line_out_type == AUTO_PIN_LINE_OUT && cfg->line_outs) {
3799 if (cfg->speaker_outs)
3800 for (i = 0; i < cfg->line_outs; i++) {
3801 hda_nid_t nid = cfg->line_out_pins[i];
3802 if (!is_jack_detectable(codec, nid))
3803 continue;
3804 snd_printdd("hda-codec: Enable Line-Out auto-muting on NID 0x%x\n", nid);
3805 snd_hda_jack_detect_enable_callback(codec, nid,
3806 HDA_GEN_FRONT_EVENT,
Takashi Iwai2e03e952013-01-03 15:55:06 +01003807 spec->line_automute_hook ?
3808 spec->line_automute_hook :
Takashi Iwai5d550e12012-12-19 15:16:44 +01003809 snd_hda_gen_line_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003810 spec->detect_lo = 1;
3811 }
3812 spec->automute_lo_possible = spec->detect_hp;
3813 }
3814
3815 spec->automute_speaker_possible = cfg->speaker_outs &&
3816 (spec->detect_hp || spec->detect_lo);
3817
3818 spec->automute_lo = spec->automute_lo_possible;
3819 spec->automute_speaker = spec->automute_speaker_possible;
3820
3821 if (spec->automute_speaker_possible || spec->automute_lo_possible) {
3822 /* create a control for automute mode */
3823 err = add_automute_mode_enum(codec);
3824 if (err < 0)
3825 return err;
3826 }
3827 return 0;
3828}
3829
Takashi Iwai352f7f92012-12-19 12:52:06 +01003830/* check whether all auto-mic pins are valid; setup indices if OK */
3831static bool auto_mic_check_imux(struct hda_codec *codec)
3832{
3833 struct hda_gen_spec *spec = codec->spec;
3834 const struct hda_input_mux *imux;
3835 int i;
3836
3837 imux = &spec->input_mux;
3838 for (i = 0; i < spec->am_num_entries; i++) {
3839 spec->am_entry[i].idx =
3840 find_idx_in_nid_list(spec->am_entry[i].pin,
3841 spec->imux_pins, imux->num_items);
3842 if (spec->am_entry[i].idx < 0)
3843 return false; /* no corresponding imux */
3844 }
3845
3846 /* we don't need the jack detection for the first pin */
3847 for (i = 1; i < spec->am_num_entries; i++)
3848 snd_hda_jack_detect_enable_callback(codec,
3849 spec->am_entry[i].pin,
3850 HDA_GEN_MIC_EVENT,
Takashi Iwai2e03e952013-01-03 15:55:06 +01003851 spec->mic_autoswitch_hook ?
3852 spec->mic_autoswitch_hook :
Takashi Iwai5d550e12012-12-19 15:16:44 +01003853 snd_hda_gen_mic_autoswitch);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003854 return true;
3855}
3856
3857static int compare_attr(const void *ap, const void *bp)
3858{
3859 const struct automic_entry *a = ap;
3860 const struct automic_entry *b = bp;
3861 return (int)(a->attr - b->attr);
3862}
3863
3864/*
3865 * Check the availability of auto-mic switch;
3866 * Set up if really supported
3867 */
3868static int check_auto_mic_availability(struct hda_codec *codec)
3869{
3870 struct hda_gen_spec *spec = codec->spec;
3871 struct auto_pin_cfg *cfg = &spec->autocfg;
3872 unsigned int types;
3873 int i, num_pins;
3874
Takashi Iwaid12daf62013-01-07 16:32:11 +01003875 if (spec->suppress_auto_mic)
3876 return 0;
3877
Takashi Iwai352f7f92012-12-19 12:52:06 +01003878 types = 0;
3879 num_pins = 0;
3880 for (i = 0; i < cfg->num_inputs; i++) {
3881 hda_nid_t nid = cfg->inputs[i].pin;
3882 unsigned int attr;
3883 attr = snd_hda_codec_get_pincfg(codec, nid);
3884 attr = snd_hda_get_input_pin_attr(attr);
3885 if (types & (1 << attr))
3886 return 0; /* already occupied */
3887 switch (attr) {
3888 case INPUT_PIN_ATTR_INT:
3889 if (cfg->inputs[i].type != AUTO_PIN_MIC)
3890 return 0; /* invalid type */
3891 break;
3892 case INPUT_PIN_ATTR_UNUSED:
3893 return 0; /* invalid entry */
3894 default:
3895 if (cfg->inputs[i].type > AUTO_PIN_LINE_IN)
3896 return 0; /* invalid type */
3897 if (!spec->line_in_auto_switch &&
3898 cfg->inputs[i].type != AUTO_PIN_MIC)
3899 return 0; /* only mic is allowed */
3900 if (!is_jack_detectable(codec, nid))
3901 return 0; /* no unsol support */
3902 break;
3903 }
3904 if (num_pins >= MAX_AUTO_MIC_PINS)
3905 return 0;
3906 types |= (1 << attr);
3907 spec->am_entry[num_pins].pin = nid;
3908 spec->am_entry[num_pins].attr = attr;
3909 num_pins++;
3910 }
3911
3912 if (num_pins < 2)
3913 return 0;
3914
3915 spec->am_num_entries = num_pins;
3916 /* sort the am_entry in the order of attr so that the pin with a
3917 * higher attr will be selected when the jack is plugged.
3918 */
3919 sort(spec->am_entry, num_pins, sizeof(spec->am_entry[0]),
3920 compare_attr, NULL);
3921
3922 if (!auto_mic_check_imux(codec))
3923 return 0;
3924
3925 spec->auto_mic = 1;
3926 spec->num_adc_nids = 1;
3927 spec->cur_mux[0] = spec->am_entry[0].idx;
3928 snd_printdd("hda-codec: Enable auto-mic switch on NID 0x%x/0x%x/0x%x\n",
3929 spec->am_entry[0].pin,
3930 spec->am_entry[1].pin,
3931 spec->am_entry[2].pin);
3932
3933 return 0;
3934}
3935
Takashi Iwai55196ff2013-01-24 17:32:56 +01003936/* power_filter hook; make inactive widgets into power down */
3937static unsigned int snd_hda_gen_path_power_filter(struct hda_codec *codec,
3938 hda_nid_t nid,
3939 unsigned int power_state)
3940{
3941 if (power_state != AC_PWRST_D0)
3942 return power_state;
3943 if (get_wcaps_type(get_wcaps(codec, nid)) >= AC_WID_POWER)
3944 return power_state;
3945 if (is_active_nid(codec, nid, HDA_OUTPUT, 0))
3946 return power_state;
3947 return AC_PWRST_D3;
3948}
3949
Takashi Iwai352f7f92012-12-19 12:52:06 +01003950
Takashi Iwai9eb413e2012-12-19 14:41:21 +01003951/*
3952 * Parse the given BIOS configuration and set up the hda_gen_spec
3953 *
3954 * return 1 if successful, 0 if the proper config is not found,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003955 * or a negative error code
3956 */
3957int snd_hda_gen_parse_auto_config(struct hda_codec *codec,
Takashi Iwai9eb413e2012-12-19 14:41:21 +01003958 struct auto_pin_cfg *cfg)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003959{
3960 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003961 int err;
3962
Takashi Iwai1c70a582013-01-11 17:48:22 +01003963 parse_user_hints(codec);
3964
Takashi Iwaie4a395e2013-01-23 17:00:31 +01003965 if (spec->mixer_nid && !spec->mixer_merge_nid)
3966 spec->mixer_merge_nid = spec->mixer_nid;
3967
Takashi Iwai9eb413e2012-12-19 14:41:21 +01003968 if (cfg != &spec->autocfg) {
3969 spec->autocfg = *cfg;
3970 cfg = &spec->autocfg;
3971 }
3972
David Henningsson6fc4cb92013-01-16 15:58:45 +01003973 fill_all_dac_nids(codec);
3974
Takashi Iwai352f7f92012-12-19 12:52:06 +01003975 if (!cfg->line_outs) {
3976 if (cfg->dig_outs || cfg->dig_in_pin) {
3977 spec->multiout.max_channels = 2;
3978 spec->no_analog = 1;
3979 goto dig_only;
3980 }
3981 return 0; /* can't find valid BIOS pin config */
3982 }
3983
3984 if (!spec->no_primary_hp &&
3985 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT &&
3986 cfg->line_outs <= cfg->hp_outs) {
3987 /* use HP as primary out */
3988 cfg->speaker_outs = cfg->line_outs;
3989 memcpy(cfg->speaker_pins, cfg->line_out_pins,
3990 sizeof(cfg->speaker_pins));
3991 cfg->line_outs = cfg->hp_outs;
3992 memcpy(cfg->line_out_pins, cfg->hp_pins, sizeof(cfg->hp_pins));
3993 cfg->hp_outs = 0;
3994 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
3995 cfg->line_out_type = AUTO_PIN_HP_OUT;
3996 }
3997
3998 err = parse_output_paths(codec);
3999 if (err < 0)
4000 return err;
4001 err = create_multi_channel_mode(codec);
4002 if (err < 0)
4003 return err;
4004 err = create_multi_out_ctls(codec, cfg);
4005 if (err < 0)
4006 return err;
4007 err = create_hp_out_ctls(codec);
4008 if (err < 0)
4009 return err;
4010 err = create_speaker_out_ctls(codec);
4011 if (err < 0)
4012 return err;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004013 err = create_indep_hp_ctls(codec);
4014 if (err < 0)
4015 return err;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01004016 err = create_loopback_mixing_ctl(codec);
4017 if (err < 0)
4018 return err;
Takashi Iwai967303d2013-02-19 17:12:42 +01004019 err = create_hp_mic(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004020 if (err < 0)
4021 return err;
4022 err = create_input_ctls(codec);
Takashi Iwaid13bd412008-07-30 15:01:45 +02004023 if (err < 0)
Takashi Iwai071c73a2006-08-23 18:34:06 +02004024 return err;
4025
Takashi Iwaia07a9492013-01-07 16:44:06 +01004026 spec->const_channel_count = spec->ext_channel_count;
4027 /* check the multiple speaker and headphone pins */
4028 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
4029 spec->const_channel_count = max(spec->const_channel_count,
4030 cfg->speaker_outs * 2);
4031 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
4032 spec->const_channel_count = max(spec->const_channel_count,
4033 cfg->hp_outs * 2);
4034 spec->multiout.max_channels = max(spec->ext_channel_count,
4035 spec->const_channel_count);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004036
4037 err = check_auto_mute_availability(codec);
4038 if (err < 0)
4039 return err;
4040
4041 err = check_dyn_adc_switch(codec);
4042 if (err < 0)
4043 return err;
4044
Takashi Iwai967303d2013-02-19 17:12:42 +01004045 err = check_auto_mic_availability(codec);
4046 if (err < 0)
4047 return err;
Takashi Iwai071c73a2006-08-23 18:34:06 +02004048
Takashi Iwai352f7f92012-12-19 12:52:06 +01004049 err = create_capture_mixers(codec);
4050 if (err < 0)
4051 return err;
4052
4053 err = parse_mic_boost(codec);
4054 if (err < 0)
4055 return err;
4056
Takashi Iwai978e77e2013-01-10 16:57:58 +01004057 if (spec->add_out_jack_modes) {
4058 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
4059 err = create_out_jack_modes(codec, cfg->line_outs,
4060 cfg->line_out_pins);
4061 if (err < 0)
4062 return err;
4063 }
4064 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
4065 err = create_out_jack_modes(codec, cfg->hp_outs,
4066 cfg->hp_pins);
4067 if (err < 0)
4068 return err;
4069 }
4070 }
4071
Takashi Iwai352f7f92012-12-19 12:52:06 +01004072 dig_only:
4073 parse_digital(codec);
4074
Takashi Iwai55196ff2013-01-24 17:32:56 +01004075 if (spec->power_down_unused)
4076 codec->power_filter = snd_hda_gen_path_power_filter;
4077
Takashi Iwai352f7f92012-12-19 12:52:06 +01004078 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004079}
Takashi Iwai352f7f92012-12-19 12:52:06 +01004080EXPORT_SYMBOL_HDA(snd_hda_gen_parse_auto_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004081
4082
4083/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01004084 * Build control elements
Linus Torvalds1da177e2005-04-16 15:20:36 -07004085 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01004086
4087/* slave controls for virtual master */
4088static const char * const slave_pfxs[] = {
4089 "Front", "Surround", "Center", "LFE", "Side",
4090 "Headphone", "Speaker", "Mono", "Line Out",
4091 "CLFE", "Bass Speaker", "PCM",
Takashi Iwaiee79c692013-01-07 09:57:42 +01004092 "Speaker Front", "Speaker Surround", "Speaker CLFE", "Speaker Side",
4093 "Headphone Front", "Headphone Surround", "Headphone CLFE",
4094 "Headphone Side",
Takashi Iwai352f7f92012-12-19 12:52:06 +01004095 NULL,
4096};
4097
4098int snd_hda_gen_build_controls(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004099{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004100 struct hda_gen_spec *spec = codec->spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004101 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004102
Takashi Iwai36502d02012-12-19 15:15:10 +01004103 if (spec->kctls.used) {
4104 err = snd_hda_add_new_ctls(codec, spec->kctls.list);
4105 if (err < 0)
4106 return err;
4107 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004108
Takashi Iwai352f7f92012-12-19 12:52:06 +01004109 if (spec->multiout.dig_out_nid) {
4110 err = snd_hda_create_dig_out_ctls(codec,
4111 spec->multiout.dig_out_nid,
4112 spec->multiout.dig_out_nid,
4113 spec->pcm_rec[1].pcm_type);
4114 if (err < 0)
4115 return err;
4116 if (!spec->no_analog) {
4117 err = snd_hda_create_spdif_share_sw(codec,
4118 &spec->multiout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004119 if (err < 0)
4120 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004121 spec->multiout.share_spdif = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004122 }
4123 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01004124 if (spec->dig_in_nid) {
4125 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
4126 if (err < 0)
4127 return err;
4128 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004129
Takashi Iwai352f7f92012-12-19 12:52:06 +01004130 /* if we have no master control, let's create it */
4131 if (!spec->no_analog &&
4132 !snd_hda_find_mixer_ctl(codec, "Master Playback Volume")) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01004133 err = snd_hda_add_vmaster(codec, "Master Playback Volume",
Takashi Iwai7a71bbf2013-01-17 10:25:15 +01004134 spec->vmaster_tlv, slave_pfxs,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004135 "Playback Volume");
4136 if (err < 0)
4137 return err;
4138 }
4139 if (!spec->no_analog &&
4140 !snd_hda_find_mixer_ctl(codec, "Master Playback Switch")) {
4141 err = __snd_hda_add_vmaster(codec, "Master Playback Switch",
4142 NULL, slave_pfxs,
4143 "Playback Switch",
4144 true, &spec->vmaster_mute.sw_kctl);
4145 if (err < 0)
4146 return err;
4147 if (spec->vmaster_mute.hook)
Takashi Iwaifd25a972012-12-20 14:57:18 +01004148 snd_hda_add_vmaster_hook(codec, &spec->vmaster_mute,
4149 spec->vmaster_mute_enum);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004150 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004151
Takashi Iwai352f7f92012-12-19 12:52:06 +01004152 free_kctls(spec); /* no longer needed */
4153
Takashi Iwai967303d2013-02-19 17:12:42 +01004154 if (spec->hp_mic_pin) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01004155 int err;
Takashi Iwai967303d2013-02-19 17:12:42 +01004156 int nid = spec->hp_mic_pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004157 err = snd_hda_jack_add_kctl(codec, nid, "Headphone Mic", 0);
4158 if (err < 0)
4159 return err;
4160 err = snd_hda_jack_detect_enable(codec, nid, 0);
4161 if (err < 0)
4162 return err;
4163 }
4164
4165 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
4166 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004167 return err;
4168
4169 return 0;
4170}
Takashi Iwai352f7f92012-12-19 12:52:06 +01004171EXPORT_SYMBOL_HDA(snd_hda_gen_build_controls);
4172
Linus Torvalds1da177e2005-04-16 15:20:36 -07004173
4174/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01004175 * PCM definitions
Linus Torvalds1da177e2005-04-16 15:20:36 -07004176 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004177
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004178static void call_pcm_playback_hook(struct hda_pcm_stream *hinfo,
4179 struct hda_codec *codec,
4180 struct snd_pcm_substream *substream,
4181 int action)
4182{
4183 struct hda_gen_spec *spec = codec->spec;
4184 if (spec->pcm_playback_hook)
4185 spec->pcm_playback_hook(hinfo, codec, substream, action);
4186}
4187
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004188static void call_pcm_capture_hook(struct hda_pcm_stream *hinfo,
4189 struct hda_codec *codec,
4190 struct snd_pcm_substream *substream,
4191 int action)
4192{
4193 struct hda_gen_spec *spec = codec->spec;
4194 if (spec->pcm_capture_hook)
4195 spec->pcm_capture_hook(hinfo, codec, substream, action);
4196}
4197
Takashi Iwai352f7f92012-12-19 12:52:06 +01004198/*
4199 * Analog playback callbacks
4200 */
4201static int playback_pcm_open(struct hda_pcm_stream *hinfo,
4202 struct hda_codec *codec,
4203 struct snd_pcm_substream *substream)
4204{
4205 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004206 int err;
4207
4208 mutex_lock(&spec->pcm_mutex);
4209 err = snd_hda_multi_out_analog_open(codec,
4210 &spec->multiout, substream,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004211 hinfo);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004212 if (!err) {
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004213 spec->active_streams |= 1 << STREAM_MULTI_OUT;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004214 call_pcm_playback_hook(hinfo, codec, substream,
4215 HDA_GEN_PCM_ACT_OPEN);
4216 }
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004217 mutex_unlock(&spec->pcm_mutex);
4218 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004219}
4220
4221static int playback_pcm_prepare(struct hda_pcm_stream *hinfo,
Takashi Iwai97ec5582006-03-21 11:29:07 +01004222 struct hda_codec *codec,
4223 unsigned int stream_tag,
4224 unsigned int format,
4225 struct snd_pcm_substream *substream)
4226{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004227 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004228 int err;
4229
4230 err = snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
4231 stream_tag, format, substream);
4232 if (!err)
4233 call_pcm_playback_hook(hinfo, codec, substream,
4234 HDA_GEN_PCM_ACT_PREPARE);
4235 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004236}
Takashi Iwai97ec5582006-03-21 11:29:07 +01004237
Takashi Iwai352f7f92012-12-19 12:52:06 +01004238static int playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
4239 struct hda_codec *codec,
4240 struct snd_pcm_substream *substream)
4241{
4242 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004243 int err;
4244
4245 err = snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
4246 if (!err)
4247 call_pcm_playback_hook(hinfo, codec, substream,
4248 HDA_GEN_PCM_ACT_CLEANUP);
4249 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004250}
4251
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004252static int playback_pcm_close(struct hda_pcm_stream *hinfo,
4253 struct hda_codec *codec,
4254 struct snd_pcm_substream *substream)
4255{
4256 struct hda_gen_spec *spec = codec->spec;
4257 mutex_lock(&spec->pcm_mutex);
4258 spec->active_streams &= ~(1 << STREAM_MULTI_OUT);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004259 call_pcm_playback_hook(hinfo, codec, substream,
4260 HDA_GEN_PCM_ACT_CLOSE);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004261 mutex_unlock(&spec->pcm_mutex);
4262 return 0;
4263}
4264
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004265static int capture_pcm_open(struct hda_pcm_stream *hinfo,
4266 struct hda_codec *codec,
4267 struct snd_pcm_substream *substream)
4268{
4269 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_OPEN);
4270 return 0;
4271}
4272
4273static int capture_pcm_prepare(struct hda_pcm_stream *hinfo,
4274 struct hda_codec *codec,
4275 unsigned int stream_tag,
4276 unsigned int format,
4277 struct snd_pcm_substream *substream)
4278{
4279 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4280 call_pcm_capture_hook(hinfo, codec, substream,
4281 HDA_GEN_PCM_ACT_PREPARE);
4282 return 0;
4283}
4284
4285static int capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
4286 struct hda_codec *codec,
4287 struct snd_pcm_substream *substream)
4288{
4289 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
4290 call_pcm_capture_hook(hinfo, codec, substream,
4291 HDA_GEN_PCM_ACT_CLEANUP);
4292 return 0;
4293}
4294
4295static int capture_pcm_close(struct hda_pcm_stream *hinfo,
4296 struct hda_codec *codec,
4297 struct snd_pcm_substream *substream)
4298{
4299 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_CLOSE);
4300 return 0;
4301}
4302
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004303static int alt_playback_pcm_open(struct hda_pcm_stream *hinfo,
4304 struct hda_codec *codec,
4305 struct snd_pcm_substream *substream)
4306{
4307 struct hda_gen_spec *spec = codec->spec;
4308 int err = 0;
4309
4310 mutex_lock(&spec->pcm_mutex);
4311 if (!spec->indep_hp_enabled)
4312 err = -EBUSY;
4313 else
4314 spec->active_streams |= 1 << STREAM_INDEP_HP;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004315 call_pcm_playback_hook(hinfo, codec, substream,
4316 HDA_GEN_PCM_ACT_OPEN);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004317 mutex_unlock(&spec->pcm_mutex);
4318 return err;
4319}
4320
4321static int alt_playback_pcm_close(struct hda_pcm_stream *hinfo,
4322 struct hda_codec *codec,
4323 struct snd_pcm_substream *substream)
4324{
4325 struct hda_gen_spec *spec = codec->spec;
4326 mutex_lock(&spec->pcm_mutex);
4327 spec->active_streams &= ~(1 << STREAM_INDEP_HP);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004328 call_pcm_playback_hook(hinfo, codec, substream,
4329 HDA_GEN_PCM_ACT_CLOSE);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004330 mutex_unlock(&spec->pcm_mutex);
4331 return 0;
4332}
4333
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004334static int alt_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
4335 struct hda_codec *codec,
4336 unsigned int stream_tag,
4337 unsigned int format,
4338 struct snd_pcm_substream *substream)
4339{
4340 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4341 call_pcm_playback_hook(hinfo, codec, substream,
4342 HDA_GEN_PCM_ACT_PREPARE);
4343 return 0;
4344}
4345
4346static int alt_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
4347 struct hda_codec *codec,
4348 struct snd_pcm_substream *substream)
4349{
4350 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
4351 call_pcm_playback_hook(hinfo, codec, substream,
4352 HDA_GEN_PCM_ACT_CLEANUP);
4353 return 0;
4354}
4355
Takashi Iwai352f7f92012-12-19 12:52:06 +01004356/*
4357 * Digital out
4358 */
4359static int dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
4360 struct hda_codec *codec,
4361 struct snd_pcm_substream *substream)
4362{
4363 struct hda_gen_spec *spec = codec->spec;
4364 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
4365}
4366
4367static int dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
4368 struct hda_codec *codec,
4369 unsigned int stream_tag,
4370 unsigned int format,
4371 struct snd_pcm_substream *substream)
4372{
4373 struct hda_gen_spec *spec = codec->spec;
4374 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
4375 stream_tag, format, substream);
4376}
4377
4378static int dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
4379 struct hda_codec *codec,
4380 struct snd_pcm_substream *substream)
4381{
4382 struct hda_gen_spec *spec = codec->spec;
4383 return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
4384}
4385
4386static int dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
4387 struct hda_codec *codec,
4388 struct snd_pcm_substream *substream)
4389{
4390 struct hda_gen_spec *spec = codec->spec;
4391 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
4392}
4393
4394/*
4395 * Analog capture
4396 */
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004397#define alt_capture_pcm_open capture_pcm_open
4398#define alt_capture_pcm_close capture_pcm_close
4399
Takashi Iwai352f7f92012-12-19 12:52:06 +01004400static int alt_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
4401 struct hda_codec *codec,
4402 unsigned int stream_tag,
4403 unsigned int format,
4404 struct snd_pcm_substream *substream)
4405{
4406 struct hda_gen_spec *spec = codec->spec;
4407
4408 snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number + 1],
Takashi Iwai97ec5582006-03-21 11:29:07 +01004409 stream_tag, 0, format);
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004410 call_pcm_capture_hook(hinfo, codec, substream,
4411 HDA_GEN_PCM_ACT_PREPARE);
Takashi Iwai97ec5582006-03-21 11:29:07 +01004412 return 0;
4413}
4414
Takashi Iwai352f7f92012-12-19 12:52:06 +01004415static int alt_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
4416 struct hda_codec *codec,
4417 struct snd_pcm_substream *substream)
Takashi Iwai97ec5582006-03-21 11:29:07 +01004418{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004419 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai97ec5582006-03-21 11:29:07 +01004420
Takashi Iwai352f7f92012-12-19 12:52:06 +01004421 snd_hda_codec_cleanup_stream(codec,
4422 spec->adc_nids[substream->number + 1]);
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004423 call_pcm_capture_hook(hinfo, codec, substream,
4424 HDA_GEN_PCM_ACT_CLEANUP);
Takashi Iwai97ec5582006-03-21 11:29:07 +01004425 return 0;
4426}
4427
Takashi Iwai352f7f92012-12-19 12:52:06 +01004428/*
4429 */
4430static const struct hda_pcm_stream pcm_analog_playback = {
4431 .substreams = 1,
4432 .channels_min = 2,
4433 .channels_max = 8,
4434 /* NID is set in build_pcms */
4435 .ops = {
4436 .open = playback_pcm_open,
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004437 .close = playback_pcm_close,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004438 .prepare = playback_pcm_prepare,
4439 .cleanup = playback_pcm_cleanup
4440 },
4441};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004442
Takashi Iwai352f7f92012-12-19 12:52:06 +01004443static const struct hda_pcm_stream pcm_analog_capture = {
4444 .substreams = 1,
4445 .channels_min = 2,
4446 .channels_max = 2,
4447 /* NID is set in build_pcms */
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004448 .ops = {
4449 .open = capture_pcm_open,
4450 .close = capture_pcm_close,
4451 .prepare = capture_pcm_prepare,
4452 .cleanup = capture_pcm_cleanup
4453 },
Takashi Iwai352f7f92012-12-19 12:52:06 +01004454};
4455
4456static const struct hda_pcm_stream pcm_analog_alt_playback = {
4457 .substreams = 1,
4458 .channels_min = 2,
4459 .channels_max = 2,
4460 /* NID is set in build_pcms */
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004461 .ops = {
4462 .open = alt_playback_pcm_open,
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004463 .close = alt_playback_pcm_close,
4464 .prepare = alt_playback_pcm_prepare,
4465 .cleanup = alt_playback_pcm_cleanup
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004466 },
Takashi Iwai352f7f92012-12-19 12:52:06 +01004467};
4468
4469static const struct hda_pcm_stream pcm_analog_alt_capture = {
4470 .substreams = 2, /* can be overridden */
4471 .channels_min = 2,
4472 .channels_max = 2,
4473 /* NID is set in build_pcms */
4474 .ops = {
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004475 .open = alt_capture_pcm_open,
4476 .close = alt_capture_pcm_close,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004477 .prepare = alt_capture_pcm_prepare,
4478 .cleanup = alt_capture_pcm_cleanup
4479 },
4480};
4481
4482static const struct hda_pcm_stream pcm_digital_playback = {
4483 .substreams = 1,
4484 .channels_min = 2,
4485 .channels_max = 2,
4486 /* NID is set in build_pcms */
4487 .ops = {
4488 .open = dig_playback_pcm_open,
4489 .close = dig_playback_pcm_close,
4490 .prepare = dig_playback_pcm_prepare,
4491 .cleanup = dig_playback_pcm_cleanup
4492 },
4493};
4494
4495static const struct hda_pcm_stream pcm_digital_capture = {
4496 .substreams = 1,
4497 .channels_min = 2,
4498 .channels_max = 2,
4499 /* NID is set in build_pcms */
4500};
4501
4502/* Used by build_pcms to flag that a PCM has no playback stream */
4503static const struct hda_pcm_stream pcm_null_stream = {
4504 .substreams = 0,
4505 .channels_min = 0,
4506 .channels_max = 0,
4507};
4508
4509/*
4510 * dynamic changing ADC PCM streams
4511 */
4512static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur)
4513{
4514 struct hda_gen_spec *spec = codec->spec;
4515 hda_nid_t new_adc = spec->adc_nids[spec->dyn_adc_idx[cur]];
4516
4517 if (spec->cur_adc && spec->cur_adc != new_adc) {
4518 /* stream is running, let's swap the current ADC */
4519 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
4520 spec->cur_adc = new_adc;
4521 snd_hda_codec_setup_stream(codec, new_adc,
4522 spec->cur_adc_stream_tag, 0,
4523 spec->cur_adc_format);
4524 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004525 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01004526 return false;
4527}
4528
4529/* analog capture with dynamic dual-adc changes */
4530static int dyn_adc_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
4531 struct hda_codec *codec,
4532 unsigned int stream_tag,
4533 unsigned int format,
4534 struct snd_pcm_substream *substream)
4535{
4536 struct hda_gen_spec *spec = codec->spec;
4537 spec->cur_adc = spec->adc_nids[spec->dyn_adc_idx[spec->cur_mux[0]]];
4538 spec->cur_adc_stream_tag = stream_tag;
4539 spec->cur_adc_format = format;
4540 snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
4541 return 0;
4542}
4543
4544static int dyn_adc_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
4545 struct hda_codec *codec,
4546 struct snd_pcm_substream *substream)
4547{
4548 struct hda_gen_spec *spec = codec->spec;
4549 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
4550 spec->cur_adc = 0;
4551 return 0;
4552}
4553
4554static const struct hda_pcm_stream dyn_adc_pcm_analog_capture = {
4555 .substreams = 1,
4556 .channels_min = 2,
4557 .channels_max = 2,
4558 .nid = 0, /* fill later */
4559 .ops = {
4560 .prepare = dyn_adc_capture_pcm_prepare,
4561 .cleanup = dyn_adc_capture_pcm_cleanup
4562 },
4563};
4564
Takashi Iwaif873e532012-12-20 16:58:39 +01004565static void fill_pcm_stream_name(char *str, size_t len, const char *sfx,
4566 const char *chip_name)
4567{
4568 char *p;
4569
4570 if (*str)
4571 return;
4572 strlcpy(str, chip_name, len);
4573
4574 /* drop non-alnum chars after a space */
4575 for (p = strchr(str, ' '); p; p = strchr(p + 1, ' ')) {
4576 if (!isalnum(p[1])) {
4577 *p = 0;
4578 break;
4579 }
4580 }
4581 strlcat(str, sfx, len);
4582}
4583
Takashi Iwai352f7f92012-12-19 12:52:06 +01004584/* build PCM streams based on the parsed results */
4585int snd_hda_gen_build_pcms(struct hda_codec *codec)
4586{
4587 struct hda_gen_spec *spec = codec->spec;
4588 struct hda_pcm *info = spec->pcm_rec;
4589 const struct hda_pcm_stream *p;
4590 bool have_multi_adcs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004591
4592 codec->num_pcms = 1;
4593 codec->pcm_info = info;
4594
Takashi Iwai352f7f92012-12-19 12:52:06 +01004595 if (spec->no_analog)
4596 goto skip_analog;
4597
Takashi Iwaif873e532012-12-20 16:58:39 +01004598 fill_pcm_stream_name(spec->stream_name_analog,
4599 sizeof(spec->stream_name_analog),
4600 " Analog", codec->chip_name);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004601 info->name = spec->stream_name_analog;
4602
4603 if (spec->multiout.num_dacs > 0) {
4604 p = spec->stream_analog_playback;
4605 if (!p)
4606 p = &pcm_analog_playback;
4607 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
4608 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dac_nids[0];
4609 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
4610 spec->multiout.max_channels;
4611 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT &&
4612 spec->autocfg.line_outs == 2)
4613 info->stream[SNDRV_PCM_STREAM_PLAYBACK].chmap =
4614 snd_pcm_2_1_chmaps;
4615 }
4616 if (spec->num_adc_nids) {
4617 p = spec->stream_analog_capture;
4618 if (!p) {
4619 if (spec->dyn_adc_switch)
4620 p = &dyn_adc_pcm_analog_capture;
4621 else
4622 p = &pcm_analog_capture;
4623 }
4624 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
4625 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adc_nids[0];
4626 }
4627
Takashi Iwai352f7f92012-12-19 12:52:06 +01004628 skip_analog:
4629 /* SPDIF for stream index #1 */
4630 if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
Takashi Iwaif873e532012-12-20 16:58:39 +01004631 fill_pcm_stream_name(spec->stream_name_digital,
4632 sizeof(spec->stream_name_digital),
4633 " Digital", codec->chip_name);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004634 codec->num_pcms = 2;
4635 codec->slave_dig_outs = spec->multiout.slave_dig_outs;
4636 info = spec->pcm_rec + 1;
4637 info->name = spec->stream_name_digital;
4638 if (spec->dig_out_type)
4639 info->pcm_type = spec->dig_out_type;
4640 else
4641 info->pcm_type = HDA_PCM_TYPE_SPDIF;
4642 if (spec->multiout.dig_out_nid) {
4643 p = spec->stream_digital_playback;
4644 if (!p)
4645 p = &pcm_digital_playback;
4646 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
4647 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dig_out_nid;
4648 }
4649 if (spec->dig_in_nid) {
4650 p = spec->stream_digital_capture;
4651 if (!p)
4652 p = &pcm_digital_capture;
4653 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
4654 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in_nid;
4655 }
4656 }
4657
4658 if (spec->no_analog)
4659 return 0;
4660
4661 /* If the use of more than one ADC is requested for the current
4662 * model, configure a second analog capture-only PCM.
4663 */
4664 have_multi_adcs = (spec->num_adc_nids > 1) &&
4665 !spec->dyn_adc_switch && !spec->auto_mic;
4666 /* Additional Analaog capture for index #2 */
4667 if (spec->alt_dac_nid || have_multi_adcs) {
Takashi Iwaia6071482013-01-21 16:50:09 +01004668 fill_pcm_stream_name(spec->stream_name_alt_analog,
4669 sizeof(spec->stream_name_alt_analog),
4670 " Alt Analog", codec->chip_name);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004671 codec->num_pcms = 3;
4672 info = spec->pcm_rec + 2;
Takashi Iwaia6071482013-01-21 16:50:09 +01004673 info->name = spec->stream_name_alt_analog;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004674 if (spec->alt_dac_nid) {
4675 p = spec->stream_analog_alt_playback;
4676 if (!p)
4677 p = &pcm_analog_alt_playback;
4678 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
4679 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
4680 spec->alt_dac_nid;
4681 } else {
4682 info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
4683 pcm_null_stream;
4684 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = 0;
4685 }
4686 if (have_multi_adcs) {
4687 p = spec->stream_analog_alt_capture;
4688 if (!p)
4689 p = &pcm_analog_alt_capture;
4690 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
4691 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
4692 spec->adc_nids[1];
4693 info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams =
4694 spec->num_adc_nids - 1;
4695 } else {
4696 info->stream[SNDRV_PCM_STREAM_CAPTURE] =
4697 pcm_null_stream;
4698 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = 0;
4699 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004700 }
4701
4702 return 0;
4703}
Takashi Iwai352f7f92012-12-19 12:52:06 +01004704EXPORT_SYMBOL_HDA(snd_hda_gen_build_pcms);
4705
4706
4707/*
4708 * Standard auto-parser initializations
4709 */
4710
Takashi Iwaid4156932013-01-07 10:08:02 +01004711/* configure the given path as a proper output */
Takashi Iwai2c12c302013-01-10 09:33:29 +01004712static void set_output_and_unmute(struct hda_codec *codec, int path_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004713{
4714 struct nid_path *path;
Takashi Iwaid4156932013-01-07 10:08:02 +01004715 hda_nid_t pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004716
Takashi Iwai196c17662013-01-04 15:01:40 +01004717 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwaid4156932013-01-07 10:08:02 +01004718 if (!path || !path->depth)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004719 return;
Takashi Iwaid4156932013-01-07 10:08:02 +01004720 pin = path->path[path->depth - 1];
Takashi Iwai2c12c302013-01-10 09:33:29 +01004721 restore_pin_ctl(codec, pin);
Takashi Iwaie1284af2013-01-03 16:33:02 +01004722 snd_hda_activate_path(codec, path, path->active, true);
4723 set_pin_eapd(codec, pin, path->active);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004724}
4725
4726/* initialize primary output paths */
4727static void init_multi_out(struct hda_codec *codec)
4728{
4729 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004730 int i;
4731
Takashi Iwaid4156932013-01-07 10:08:02 +01004732 for (i = 0; i < spec->autocfg.line_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01004733 set_output_and_unmute(codec, spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004734}
4735
Takashi Iwaidb23fd12012-12-20 15:27:24 +01004736
Takashi Iwai2c12c302013-01-10 09:33:29 +01004737static void __init_extra_out(struct hda_codec *codec, int num_outs, int *paths)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004738{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004739 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004740
Takashi Iwaid4156932013-01-07 10:08:02 +01004741 for (i = 0; i < num_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01004742 set_output_and_unmute(codec, paths[i]);
Takashi Iwaidb23fd12012-12-20 15:27:24 +01004743}
4744
4745/* initialize hp and speaker paths */
4746static void init_extra_out(struct hda_codec *codec)
4747{
4748 struct hda_gen_spec *spec = codec->spec;
4749
4750 if (spec->autocfg.line_out_type != AUTO_PIN_HP_OUT)
Takashi Iwai2c12c302013-01-10 09:33:29 +01004751 __init_extra_out(codec, spec->autocfg.hp_outs, spec->hp_paths);
Takashi Iwaidb23fd12012-12-20 15:27:24 +01004752 if (spec->autocfg.line_out_type != AUTO_PIN_SPEAKER_OUT)
4753 __init_extra_out(codec, spec->autocfg.speaker_outs,
Takashi Iwai2c12c302013-01-10 09:33:29 +01004754 spec->speaker_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004755}
4756
4757/* initialize multi-io paths */
4758static void init_multi_io(struct hda_codec *codec)
4759{
4760 struct hda_gen_spec *spec = codec->spec;
4761 int i;
4762
4763 for (i = 0; i < spec->multi_ios; i++) {
4764 hda_nid_t pin = spec->multi_io[i].pin;
4765 struct nid_path *path;
Takashi Iwai196c17662013-01-04 15:01:40 +01004766 path = get_multiio_path(codec, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004767 if (!path)
4768 continue;
4769 if (!spec->multi_io[i].ctl_in)
4770 spec->multi_io[i].ctl_in =
Takashi Iwai2c12c302013-01-10 09:33:29 +01004771 snd_hda_codec_get_pin_target(codec, pin);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004772 snd_hda_activate_path(codec, path, path->active, true);
4773 }
4774}
4775
Takashi Iwai352f7f92012-12-19 12:52:06 +01004776/* set up input pins and loopback paths */
4777static void init_analog_input(struct hda_codec *codec)
4778{
4779 struct hda_gen_spec *spec = codec->spec;
4780 struct auto_pin_cfg *cfg = &spec->autocfg;
4781 int i;
4782
4783 for (i = 0; i < cfg->num_inputs; i++) {
4784 hda_nid_t nid = cfg->inputs[i].pin;
4785 if (is_input_pin(codec, nid))
Takashi Iwai2c12c302013-01-10 09:33:29 +01004786 restore_pin_ctl(codec, nid);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004787
4788 /* init loopback inputs */
4789 if (spec->mixer_nid) {
Takashi Iwai3e367f12013-01-23 17:07:23 +01004790 resume_path_from_idx(codec, spec->loopback_paths[i]);
4791 resume_path_from_idx(codec, spec->loopback_merge_path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004792 }
4793 }
4794}
4795
4796/* initialize ADC paths */
4797static void init_input_src(struct hda_codec *codec)
4798{
4799 struct hda_gen_spec *spec = codec->spec;
4800 struct hda_input_mux *imux = &spec->input_mux;
4801 struct nid_path *path;
4802 int i, c, nums;
4803
4804 if (spec->dyn_adc_switch)
4805 nums = 1;
4806 else
4807 nums = spec->num_adc_nids;
4808
4809 for (c = 0; c < nums; c++) {
4810 for (i = 0; i < imux->num_items; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01004811 path = get_input_path(codec, c, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004812 if (path) {
4813 bool active = path->active;
4814 if (i == spec->cur_mux[c])
4815 active = true;
4816 snd_hda_activate_path(codec, path, active, false);
4817 }
4818 }
Takashi Iwai967303d2013-02-19 17:12:42 +01004819 if (spec->hp_mic)
4820 update_hp_mic(codec, c, true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004821 }
4822
Takashi Iwai352f7f92012-12-19 12:52:06 +01004823 if (spec->cap_sync_hook)
Takashi Iwaia90229e2013-01-18 14:10:00 +01004824 spec->cap_sync_hook(codec, NULL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004825}
4826
4827/* set right pin controls for digital I/O */
4828static void init_digital(struct hda_codec *codec)
4829{
4830 struct hda_gen_spec *spec = codec->spec;
4831 int i;
4832 hda_nid_t pin;
4833
Takashi Iwaid4156932013-01-07 10:08:02 +01004834 for (i = 0; i < spec->autocfg.dig_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01004835 set_output_and_unmute(codec, spec->digout_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004836 pin = spec->autocfg.dig_in_pin;
Takashi Iwai2430d7b2013-01-04 15:09:42 +01004837 if (pin) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01004838 restore_pin_ctl(codec, pin);
Takashi Iwai3e367f12013-01-23 17:07:23 +01004839 resume_path_from_idx(codec, spec->digin_path);
Takashi Iwai2430d7b2013-01-04 15:09:42 +01004840 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01004841}
4842
Takashi Iwai973e4972012-12-20 15:16:09 +01004843/* clear unsol-event tags on unused pins; Conexant codecs seem to leave
4844 * invalid unsol tags by some reason
4845 */
4846static void clear_unsol_on_unused_pins(struct hda_codec *codec)
4847{
4848 int i;
4849
4850 for (i = 0; i < codec->init_pins.used; i++) {
4851 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
4852 hda_nid_t nid = pin->nid;
4853 if (is_jack_detectable(codec, nid) &&
4854 !snd_hda_jack_tbl_get(codec, nid))
4855 snd_hda_codec_update_cache(codec, nid, 0,
4856 AC_VERB_SET_UNSOLICITED_ENABLE, 0);
4857 }
4858}
4859
Takashi Iwai5187ac12013-01-07 12:52:16 +01004860/*
4861 * initialize the generic spec;
4862 * this can be put as patch_ops.init function
4863 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01004864int snd_hda_gen_init(struct hda_codec *codec)
4865{
4866 struct hda_gen_spec *spec = codec->spec;
4867
4868 if (spec->init_hook)
4869 spec->init_hook(codec);
4870
4871 snd_hda_apply_verbs(codec);
4872
Takashi Iwai3bbcd272012-12-20 11:50:58 +01004873 codec->cached_write = 1;
4874
Takashi Iwai352f7f92012-12-19 12:52:06 +01004875 init_multi_out(codec);
4876 init_extra_out(codec);
4877 init_multi_io(codec);
4878 init_analog_input(codec);
4879 init_input_src(codec);
4880 init_digital(codec);
4881
Takashi Iwai973e4972012-12-20 15:16:09 +01004882 clear_unsol_on_unused_pins(codec);
4883
Takashi Iwai352f7f92012-12-19 12:52:06 +01004884 /* call init functions of standard auto-mute helpers */
Takashi Iwaia5cc2502013-01-16 18:08:55 +01004885 update_automute_all(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004886
Takashi Iwaidc870f32013-01-22 15:24:30 +01004887 snd_hda_codec_flush_cache(codec);
Takashi Iwai3bbcd272012-12-20 11:50:58 +01004888
Takashi Iwai352f7f92012-12-19 12:52:06 +01004889 if (spec->vmaster_mute.sw_kctl && spec->vmaster_mute.hook)
4890 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
4891
4892 hda_call_check_power_status(codec, 0x01);
4893 return 0;
4894}
Takashi Iwaifce52a32013-01-07 12:42:48 +01004895EXPORT_SYMBOL_HDA(snd_hda_gen_init);
4896
Takashi Iwai5187ac12013-01-07 12:52:16 +01004897/*
4898 * free the generic spec;
4899 * this can be put as patch_ops.free function
4900 */
Takashi Iwaifce52a32013-01-07 12:42:48 +01004901void snd_hda_gen_free(struct hda_codec *codec)
4902{
4903 snd_hda_gen_spec_free(codec->spec);
4904 kfree(codec->spec);
4905 codec->spec = NULL;
4906}
4907EXPORT_SYMBOL_HDA(snd_hda_gen_free);
4908
4909#ifdef CONFIG_PM
Takashi Iwai5187ac12013-01-07 12:52:16 +01004910/*
4911 * check the loopback power save state;
4912 * this can be put as patch_ops.check_power_status function
4913 */
Takashi Iwaifce52a32013-01-07 12:42:48 +01004914int snd_hda_gen_check_power_status(struct hda_codec *codec, hda_nid_t nid)
4915{
4916 struct hda_gen_spec *spec = codec->spec;
4917 return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
4918}
4919EXPORT_SYMBOL_HDA(snd_hda_gen_check_power_status);
4920#endif
Takashi Iwai352f7f92012-12-19 12:52:06 +01004921
4922
4923/*
4924 * the generic codec support
4925 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004926
Takashi Iwai352f7f92012-12-19 12:52:06 +01004927static const struct hda_codec_ops generic_patch_ops = {
4928 .build_controls = snd_hda_gen_build_controls,
4929 .build_pcms = snd_hda_gen_build_pcms,
4930 .init = snd_hda_gen_init,
Takashi Iwaifce52a32013-01-07 12:42:48 +01004931 .free = snd_hda_gen_free,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004932 .unsol_event = snd_hda_jack_unsol_event,
Takashi Iwai83012a72012-08-24 18:38:08 +02004933#ifdef CONFIG_PM
Takashi Iwaifce52a32013-01-07 12:42:48 +01004934 .check_power_status = snd_hda_gen_check_power_status,
Takashi Iwaicb53c622007-08-10 17:21:45 +02004935#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004936};
4937
Linus Torvalds1da177e2005-04-16 15:20:36 -07004938int snd_hda_parse_generic_codec(struct hda_codec *codec)
4939{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004940 struct hda_gen_spec *spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004941 int err;
4942
Takashi Iwaie560d8d2005-09-09 14:21:46 +02004943 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004944 if (!spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004945 return -ENOMEM;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004946 snd_hda_gen_spec_init(spec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004947 codec->spec = spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004948
Takashi Iwai9eb413e2012-12-19 14:41:21 +01004949 err = snd_hda_parse_pin_defcfg(codec, &spec->autocfg, NULL, 0);
4950 if (err < 0)
4951 return err;
4952
4953 err = snd_hda_gen_parse_auto_config(codec, &spec->autocfg);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004954 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004955 goto error;
4956
4957 codec->patch_ops = generic_patch_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004958 return 0;
4959
Takashi Iwai352f7f92012-12-19 12:52:06 +01004960error:
Takashi Iwaifce52a32013-01-07 12:42:48 +01004961 snd_hda_gen_free(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004962 return err;
4963}
Takashi Iwaifce52a32013-01-07 12:42:48 +01004964EXPORT_SYMBOL_HDA(snd_hda_parse_generic_codec);