blob: 6af5aded1ed58a152b7e875280981fbea54ce9db [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 Iwai1c70a582013-01-11 17:48:22 +0100162
163 if (!snd_hda_get_int_hint(codec, "mixer_nid", &val))
164 spec->mixer_nid = val;
165}
166
167/*
Takashi Iwai2c12c302013-01-10 09:33:29 +0100168 * pin control value accesses
169 */
170
171#define update_pin_ctl(codec, pin, val) \
172 snd_hda_codec_update_cache(codec, pin, 0, \
173 AC_VERB_SET_PIN_WIDGET_CONTROL, val)
174
175/* restore the pinctl based on the cached value */
176static inline void restore_pin_ctl(struct hda_codec *codec, hda_nid_t pin)
177{
178 update_pin_ctl(codec, pin, snd_hda_codec_get_pin_target(codec, pin));
179}
180
181/* set the pinctl target value and write it if requested */
182static void set_pin_target(struct hda_codec *codec, hda_nid_t pin,
183 unsigned int val, bool do_write)
184{
185 if (!pin)
186 return;
187 val = snd_hda_correct_pin_ctl(codec, pin, val);
188 snd_hda_codec_set_pin_target(codec, pin, val);
189 if (do_write)
190 update_pin_ctl(codec, pin, val);
191}
192
193/* set pinctl target values for all given pins */
194static void set_pin_targets(struct hda_codec *codec, int num_pins,
195 hda_nid_t *pins, unsigned int val)
196{
197 int i;
198 for (i = 0; i < num_pins; i++)
199 set_pin_target(codec, pins[i], val, false);
200}
201
202/*
Takashi Iwai352f7f92012-12-19 12:52:06 +0100203 * parsing paths
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100206/* return the position of NID in the list, or -1 if not found */
207static int find_idx_in_nid_list(hda_nid_t nid, const hda_nid_t *list, int nums)
208{
209 int i;
210 for (i = 0; i < nums; i++)
211 if (list[i] == nid)
212 return i;
213 return -1;
214}
215
216/* return true if the given NID is contained in the path */
217static bool is_nid_contained(struct nid_path *path, hda_nid_t nid)
218{
219 return find_idx_in_nid_list(nid, path->path, path->depth) >= 0;
220}
221
Takashi Iwaif5172a72013-01-04 13:19:55 +0100222static struct nid_path *get_nid_path(struct hda_codec *codec,
223 hda_nid_t from_nid, hda_nid_t to_nid,
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100224 int anchor_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100226 struct hda_gen_spec *spec = codec->spec;
227 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
Takashi Iwai352f7f92012-12-19 12:52:06 +0100229 for (i = 0; i < spec->paths.used; i++) {
230 struct nid_path *path = snd_array_elem(&spec->paths, i);
231 if (path->depth <= 0)
232 continue;
233 if ((!from_nid || path->path[0] == from_nid) &&
Takashi Iwaif5172a72013-01-04 13:19:55 +0100234 (!to_nid || path->path[path->depth - 1] == to_nid)) {
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100235 if (!anchor_nid ||
236 (anchor_nid > 0 && is_nid_contained(path, anchor_nid)) ||
237 (anchor_nid < 0 && !is_nid_contained(path, anchor_nid)))
Takashi Iwaif5172a72013-01-04 13:19:55 +0100238 return path;
239 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 }
241 return NULL;
242}
Takashi Iwaif5172a72013-01-04 13:19:55 +0100243
244/* get the path between the given NIDs;
245 * passing 0 to either @pin or @dac behaves as a wildcard
246 */
247struct nid_path *snd_hda_get_nid_path(struct hda_codec *codec,
248 hda_nid_t from_nid, hda_nid_t to_nid)
249{
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100250 return get_nid_path(codec, from_nid, to_nid, 0);
Takashi Iwaif5172a72013-01-04 13:19:55 +0100251}
Takashi Iwai352f7f92012-12-19 12:52:06 +0100252EXPORT_SYMBOL_HDA(snd_hda_get_nid_path);
253
Takashi Iwai196c17662013-01-04 15:01:40 +0100254/* get the index number corresponding to the path instance;
255 * the index starts from 1, for easier checking the invalid value
256 */
257int snd_hda_get_path_idx(struct hda_codec *codec, struct nid_path *path)
258{
259 struct hda_gen_spec *spec = codec->spec;
260 struct nid_path *array = spec->paths.list;
261 ssize_t idx;
262
263 if (!spec->paths.used)
264 return 0;
265 idx = path - array;
266 if (idx < 0 || idx >= spec->paths.used)
267 return 0;
268 return idx + 1;
269}
Takashi Iwai4bd01e92013-01-22 15:17:20 +0100270EXPORT_SYMBOL_HDA(snd_hda_get_path_idx);
Takashi Iwai196c17662013-01-04 15:01:40 +0100271
272/* get the path instance corresponding to the given index number */
273struct nid_path *snd_hda_get_path_from_idx(struct hda_codec *codec, int idx)
274{
275 struct hda_gen_spec *spec = codec->spec;
276
277 if (idx <= 0 || idx > spec->paths.used)
278 return NULL;
279 return snd_array_elem(&spec->paths, idx - 1);
280}
Takashi Iwai4bd01e92013-01-22 15:17:20 +0100281EXPORT_SYMBOL_HDA(snd_hda_get_path_from_idx);
Takashi Iwai196c17662013-01-04 15:01:40 +0100282
Takashi Iwai352f7f92012-12-19 12:52:06 +0100283/* check whether the given DAC is already found in any existing paths */
284static bool is_dac_already_used(struct hda_codec *codec, hda_nid_t nid)
285{
286 struct hda_gen_spec *spec = codec->spec;
287 int i;
288
289 for (i = 0; i < spec->paths.used; i++) {
290 struct nid_path *path = snd_array_elem(&spec->paths, i);
291 if (path->path[0] == nid)
292 return true;
293 }
294 return false;
295}
296
297/* check whether the given two widgets can be connected */
298static bool is_reachable_path(struct hda_codec *codec,
299 hda_nid_t from_nid, hda_nid_t to_nid)
300{
301 if (!from_nid || !to_nid)
302 return false;
303 return snd_hda_get_conn_index(codec, to_nid, from_nid, true) >= 0;
304}
305
306/* nid, dir and idx */
307#define AMP_VAL_COMPARE_MASK (0xffff | (1U << 18) | (0x0f << 19))
308
309/* check whether the given ctl is already assigned in any path elements */
310static bool is_ctl_used(struct hda_codec *codec, unsigned int val, int type)
311{
312 struct hda_gen_spec *spec = codec->spec;
313 int i;
314
315 val &= AMP_VAL_COMPARE_MASK;
316 for (i = 0; i < spec->paths.used; i++) {
317 struct nid_path *path = snd_array_elem(&spec->paths, i);
318 if ((path->ctls[type] & AMP_VAL_COMPARE_MASK) == val)
319 return true;
320 }
321 return false;
322}
323
324/* check whether a control with the given (nid, dir, idx) was assigned */
325static bool is_ctl_associated(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwai8999bf02013-01-18 11:01:33 +0100326 int dir, int idx, int type)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100327{
328 unsigned int val = HDA_COMPOSE_AMP_VAL(nid, 3, idx, dir);
Takashi Iwai8999bf02013-01-18 11:01:33 +0100329 return is_ctl_used(codec, val, type);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100330}
331
Takashi Iwai0c8c0f52012-12-20 17:54:22 +0100332static void print_nid_path(const char *pfx, struct nid_path *path)
333{
334 char buf[40];
335 int i;
336
337
338 buf[0] = 0;
339 for (i = 0; i < path->depth; i++) {
340 char tmp[4];
341 sprintf(tmp, ":%02x", path->path[i]);
342 strlcat(buf, tmp, sizeof(buf));
343 }
344 snd_printdd("%s path: depth=%d %s\n", pfx, path->depth, buf);
345}
346
Takashi Iwai352f7f92012-12-19 12:52:06 +0100347/* called recursively */
348static bool __parse_nid_path(struct hda_codec *codec,
349 hda_nid_t from_nid, hda_nid_t to_nid,
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100350 int anchor_nid, struct nid_path *path,
351 int depth)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100352{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100353 const hda_nid_t *conn;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100354 int i, nums;
355
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100356 if (to_nid == anchor_nid)
357 anchor_nid = 0; /* anchor passed */
358 else if (to_nid == (hda_nid_t)(-anchor_nid))
359 return false; /* hit the exclusive nid */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100360
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100361 nums = snd_hda_get_conn_list(codec, to_nid, &conn);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100362 for (i = 0; i < nums; i++) {
363 if (conn[i] != from_nid) {
364 /* special case: when from_nid is 0,
365 * try to find an empty DAC
366 */
367 if (from_nid ||
368 get_wcaps_type(get_wcaps(codec, conn[i])) != AC_WID_AUD_OUT ||
369 is_dac_already_used(codec, conn[i]))
370 continue;
371 }
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100372 /* anchor is not requested or already passed? */
373 if (anchor_nid <= 0)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100374 goto found;
375 }
376 if (depth >= MAX_NID_PATH_DEPTH)
377 return false;
378 for (i = 0; i < nums; i++) {
379 unsigned int type;
380 type = get_wcaps_type(get_wcaps(codec, conn[i]));
381 if (type == AC_WID_AUD_OUT || type == AC_WID_AUD_IN ||
382 type == AC_WID_PIN)
383 continue;
384 if (__parse_nid_path(codec, from_nid, conn[i],
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100385 anchor_nid, path, depth + 1))
Takashi Iwai352f7f92012-12-19 12:52:06 +0100386 goto found;
387 }
388 return false;
389
390 found:
391 path->path[path->depth] = conn[i];
392 path->idx[path->depth + 1] = i;
393 if (nums > 1 && get_wcaps_type(get_wcaps(codec, to_nid)) != AC_WID_AUD_MIX)
394 path->multi[path->depth + 1] = 1;
395 path->depth++;
396 return true;
397}
398
399/* parse the widget path from the given nid to the target nid;
400 * when @from_nid is 0, try to find an empty DAC;
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100401 * when @anchor_nid is set to a positive value, only paths through the widget
402 * with the given value are evaluated.
403 * when @anchor_nid is set to a negative value, paths through the widget
404 * with the negative of given value are excluded, only other paths are chosen.
405 * when @anchor_nid is zero, no special handling about path selection.
Takashi Iwai352f7f92012-12-19 12:52:06 +0100406 */
407bool snd_hda_parse_nid_path(struct hda_codec *codec, hda_nid_t from_nid,
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100408 hda_nid_t to_nid, int anchor_nid,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100409 struct nid_path *path)
410{
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100411 if (__parse_nid_path(codec, from_nid, to_nid, anchor_nid, path, 1)) {
Takashi Iwai352f7f92012-12-19 12:52:06 +0100412 path->path[path->depth] = to_nid;
413 path->depth++;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100414 return true;
415 }
416 return false;
417}
418EXPORT_SYMBOL_HDA(snd_hda_parse_nid_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
420/*
Takashi Iwai352f7f92012-12-19 12:52:06 +0100421 * parse the path between the given NIDs and add to the path list.
422 * if no valid path is found, return NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100424struct nid_path *
425snd_hda_add_new_path(struct hda_codec *codec, hda_nid_t from_nid,
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100426 hda_nid_t to_nid, int anchor_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100428 struct hda_gen_spec *spec = codec->spec;
429 struct nid_path *path;
430
431 if (from_nid && to_nid && !is_reachable_path(codec, from_nid, to_nid))
432 return NULL;
433
Takashi Iwaif5172a72013-01-04 13:19:55 +0100434 /* check whether the path has been already added */
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100435 path = get_nid_path(codec, from_nid, to_nid, anchor_nid);
Takashi Iwaif5172a72013-01-04 13:19:55 +0100436 if (path)
437 return path;
438
Takashi Iwai352f7f92012-12-19 12:52:06 +0100439 path = snd_array_new(&spec->paths);
440 if (!path)
441 return NULL;
442 memset(path, 0, sizeof(*path));
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100443 if (snd_hda_parse_nid_path(codec, from_nid, to_nid, anchor_nid, path))
Takashi Iwai352f7f92012-12-19 12:52:06 +0100444 return path;
445 /* push back */
446 spec->paths.used--;
447 return NULL;
448}
449EXPORT_SYMBOL_HDA(snd_hda_add_new_path);
450
Takashi Iwai980428c2013-01-09 09:28:20 +0100451/* clear the given path as invalid so that it won't be picked up later */
452static void invalidate_nid_path(struct hda_codec *codec, int idx)
453{
454 struct nid_path *path = snd_hda_get_path_from_idx(codec, idx);
455 if (!path)
456 return;
457 memset(path, 0, sizeof(*path));
458}
459
Takashi Iwai352f7f92012-12-19 12:52:06 +0100460/* look for an empty DAC slot */
461static hda_nid_t look_for_dac(struct hda_codec *codec, hda_nid_t pin,
462 bool is_digital)
463{
464 struct hda_gen_spec *spec = codec->spec;
465 bool cap_digital;
466 int i;
467
468 for (i = 0; i < spec->num_all_dacs; i++) {
469 hda_nid_t nid = spec->all_dacs[i];
470 if (!nid || is_dac_already_used(codec, nid))
471 continue;
472 cap_digital = !!(get_wcaps(codec, nid) & AC_WCAP_DIGITAL);
473 if (is_digital != cap_digital)
474 continue;
475 if (is_reachable_path(codec, nid, pin))
476 return nid;
477 }
478 return 0;
479}
480
481/* replace the channels in the composed amp value with the given number */
482static unsigned int amp_val_replace_channels(unsigned int val, unsigned int chs)
483{
484 val &= ~(0x3U << 16);
485 val |= chs << 16;
486 return val;
487}
488
489/* check whether the widget has the given amp capability for the direction */
490static bool check_amp_caps(struct hda_codec *codec, hda_nid_t nid,
491 int dir, unsigned int bits)
492{
493 if (!nid)
494 return false;
495 if (get_wcaps(codec, nid) & (1 << (dir + 1)))
496 if (query_amp_caps(codec, nid, dir) & bits)
497 return true;
498 return false;
499}
500
David Henningsson99a55922013-01-16 15:58:44 +0100501static bool same_amp_caps(struct hda_codec *codec, hda_nid_t nid1,
502 hda_nid_t nid2, int dir)
503{
504 if (!(get_wcaps(codec, nid1) & (1 << (dir + 1))))
505 return !(get_wcaps(codec, nid2) & (1 << (dir + 1)));
506 return (query_amp_caps(codec, nid1, dir) ==
507 query_amp_caps(codec, nid2, dir));
508}
509
Takashi Iwai352f7f92012-12-19 12:52:06 +0100510#define nid_has_mute(codec, nid, dir) \
511 check_amp_caps(codec, nid, dir, AC_AMPCAP_MUTE)
512#define nid_has_volume(codec, nid, dir) \
513 check_amp_caps(codec, nid, dir, AC_AMPCAP_NUM_STEPS)
514
515/* look for a widget suitable for assigning a mute switch in the path */
516static hda_nid_t look_for_out_mute_nid(struct hda_codec *codec,
517 struct nid_path *path)
518{
519 int i;
520
521 for (i = path->depth - 1; i >= 0; i--) {
522 if (nid_has_mute(codec, path->path[i], HDA_OUTPUT))
523 return path->path[i];
524 if (i != path->depth - 1 && i != 0 &&
525 nid_has_mute(codec, path->path[i], HDA_INPUT))
526 return path->path[i];
527 }
528 return 0;
529}
530
531/* look for a widget suitable for assigning a volume ctl in the path */
532static hda_nid_t look_for_out_vol_nid(struct hda_codec *codec,
533 struct nid_path *path)
534{
535 int i;
536
537 for (i = path->depth - 1; i >= 0; i--) {
538 if (nid_has_volume(codec, path->path[i], HDA_OUTPUT))
539 return path->path[i];
540 }
Takashi Iwai82beb8f2007-08-10 17:09:26 +0200541 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542}
543
544/*
Takashi Iwai352f7f92012-12-19 12:52:06 +0100545 * path activation / deactivation
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100547
548/* can have the amp-in capability? */
549static bool has_amp_in(struct hda_codec *codec, struct nid_path *path, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100551 hda_nid_t nid = path->path[idx];
552 unsigned int caps = get_wcaps(codec, nid);
553 unsigned int type = get_wcaps_type(caps);
554
555 if (!(caps & AC_WCAP_IN_AMP))
556 return false;
557 if (type == AC_WID_PIN && idx > 0) /* only for input pins */
558 return false;
559 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560}
561
Takashi Iwai352f7f92012-12-19 12:52:06 +0100562/* can have the amp-out capability? */
563static bool has_amp_out(struct hda_codec *codec, struct nid_path *path, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100565 hda_nid_t nid = path->path[idx];
566 unsigned int caps = get_wcaps(codec, nid);
567 unsigned int type = get_wcaps_type(caps);
568
569 if (!(caps & AC_WCAP_OUT_AMP))
570 return false;
571 if (type == AC_WID_PIN && !idx) /* only for output pins */
572 return false;
573 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574}
575
Takashi Iwai352f7f92012-12-19 12:52:06 +0100576/* check whether the given (nid,dir,idx) is active */
577static bool is_active_nid(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwai7dddf2ae2013-01-24 16:31:35 +0100578 unsigned int dir, unsigned int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100580 struct hda_gen_spec *spec = codec->spec;
581 int i, n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
Takashi Iwai352f7f92012-12-19 12:52:06 +0100583 for (n = 0; n < spec->paths.used; n++) {
584 struct nid_path *path = snd_array_elem(&spec->paths, n);
585 if (!path->active)
586 continue;
587 for (i = 0; i < path->depth; i++) {
588 if (path->path[i] == nid) {
589 if (dir == HDA_OUTPUT || path->idx[i] == idx)
590 return true;
591 break;
592 }
593 }
594 }
595 return false;
596}
597
598/* get the default amp value for the target state */
599static int get_amp_val_to_activate(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwai8999bf02013-01-18 11:01:33 +0100600 int dir, unsigned int caps, bool enable)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100601{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100602 unsigned int val = 0;
603
Takashi Iwai352f7f92012-12-19 12:52:06 +0100604 if (caps & AC_AMPCAP_NUM_STEPS) {
605 /* set to 0dB */
606 if (enable)
607 val = (caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT;
608 }
609 if (caps & AC_AMPCAP_MUTE) {
610 if (!enable)
611 val |= HDA_AMP_MUTE;
612 }
613 return val;
614}
615
616/* initialize the amp value (only at the first time) */
617static void init_amp(struct hda_codec *codec, hda_nid_t nid, int dir, int idx)
618{
Takashi Iwai8999bf02013-01-18 11:01:33 +0100619 unsigned int caps = query_amp_caps(codec, nid, dir);
620 int val = get_amp_val_to_activate(codec, nid, dir, caps, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100621 snd_hda_codec_amp_init_stereo(codec, nid, dir, idx, 0xff, val);
622}
623
Takashi Iwai8999bf02013-01-18 11:01:33 +0100624/* calculate amp value mask we can modify;
625 * if the given amp is controlled by mixers, don't touch it
626 */
627static unsigned int get_amp_mask_to_modify(struct hda_codec *codec,
628 hda_nid_t nid, int dir, int idx,
629 unsigned int caps)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100630{
Takashi Iwai8999bf02013-01-18 11:01:33 +0100631 unsigned int mask = 0xff;
632
633 if (caps & AC_AMPCAP_MUTE) {
634 if (is_ctl_associated(codec, nid, dir, idx, NID_PATH_MUTE_CTL))
635 mask &= ~0x80;
636 }
637 if (caps & AC_AMPCAP_NUM_STEPS) {
638 if (is_ctl_associated(codec, nid, dir, idx, NID_PATH_VOL_CTL) ||
639 is_ctl_associated(codec, nid, dir, idx, NID_PATH_BOOST_CTL))
640 mask &= ~0x7f;
641 }
642 return mask;
643}
644
645static void activate_amp(struct hda_codec *codec, hda_nid_t nid, int dir,
646 int idx, int idx_to_check, bool enable)
647{
648 unsigned int caps;
649 unsigned int mask, val;
650
Takashi Iwai7dddf2ae2013-01-24 16:31:35 +0100651 if (!enable && is_active_nid(codec, nid, dir, idx_to_check))
Takashi Iwai352f7f92012-12-19 12:52:06 +0100652 return;
Takashi Iwai8999bf02013-01-18 11:01:33 +0100653
654 caps = query_amp_caps(codec, nid, dir);
655 val = get_amp_val_to_activate(codec, nid, dir, caps, enable);
656 mask = get_amp_mask_to_modify(codec, nid, dir, idx_to_check, caps);
657 if (!mask)
658 return;
659
660 val &= mask;
661 snd_hda_codec_amp_stereo(codec, nid, dir, idx, mask, val);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100662}
663
664static void activate_amp_out(struct hda_codec *codec, struct nid_path *path,
665 int i, bool enable)
666{
667 hda_nid_t nid = path->path[i];
668 init_amp(codec, nid, HDA_OUTPUT, 0);
Takashi Iwai8999bf02013-01-18 11:01:33 +0100669 activate_amp(codec, nid, HDA_OUTPUT, 0, 0, enable);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100670}
671
672static void activate_amp_in(struct hda_codec *codec, struct nid_path *path,
673 int i, bool enable, bool add_aamix)
674{
675 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100676 const hda_nid_t *conn;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100677 int n, nums, idx;
678 int type;
679 hda_nid_t nid = path->path[i];
680
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100681 nums = snd_hda_get_conn_list(codec, nid, &conn);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100682 type = get_wcaps_type(get_wcaps(codec, nid));
683 if (type == AC_WID_PIN ||
684 (type == AC_WID_AUD_IN && codec->single_adc_amp)) {
685 nums = 1;
686 idx = 0;
687 } else
688 idx = path->idx[i];
689
690 for (n = 0; n < nums; n++)
691 init_amp(codec, nid, HDA_INPUT, n);
692
Takashi Iwai352f7f92012-12-19 12:52:06 +0100693 /* here is a little bit tricky in comparison with activate_amp_out();
694 * when aa-mixer is available, we need to enable the path as well
695 */
696 for (n = 0; n < nums; n++) {
Takashi Iwaie4a395e2013-01-23 17:00:31 +0100697 if (n != idx && (!add_aamix || conn[n] != spec->mixer_merge_nid))
Takashi Iwai352f7f92012-12-19 12:52:06 +0100698 continue;
Takashi Iwai8999bf02013-01-18 11:01:33 +0100699 activate_amp(codec, nid, HDA_INPUT, n, idx, enable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 }
701}
702
Takashi Iwai352f7f92012-12-19 12:52:06 +0100703/* activate or deactivate the given path
704 * if @add_aamix is set, enable the input from aa-mix NID as well (if any)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100706void snd_hda_activate_path(struct hda_codec *codec, struct nid_path *path,
707 bool enable, bool add_aamix)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708{
Takashi Iwai55196ff2013-01-24 17:32:56 +0100709 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100710 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711
Takashi Iwai352f7f92012-12-19 12:52:06 +0100712 if (!enable)
713 path->active = false;
714
715 for (i = path->depth - 1; i >= 0; i--) {
Takashi Iwai55196ff2013-01-24 17:32:56 +0100716 hda_nid_t nid = path->path[i];
717 if (enable && spec->power_down_unused) {
718 /* make sure the widget is powered up */
719 if (!snd_hda_check_power_state(codec, nid, AC_PWRST_D0))
720 snd_hda_codec_write(codec, nid, 0,
721 AC_VERB_SET_POWER_STATE,
722 AC_PWRST_D0);
723 }
Takashi Iwai352f7f92012-12-19 12:52:06 +0100724 if (enable && path->multi[i])
Takashi Iwai55196ff2013-01-24 17:32:56 +0100725 snd_hda_codec_write_cache(codec, nid, 0,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100726 AC_VERB_SET_CONNECT_SEL,
727 path->idx[i]);
728 if (has_amp_in(codec, path, i))
729 activate_amp_in(codec, path, i, enable, add_aamix);
730 if (has_amp_out(codec, path, i))
731 activate_amp_out(codec, path, i, enable);
732 }
733
734 if (enable)
735 path->active = true;
736}
737EXPORT_SYMBOL_HDA(snd_hda_activate_path);
738
Takashi Iwai55196ff2013-01-24 17:32:56 +0100739/* if the given path is inactive, put widgets into D3 (only if suitable) */
740static void path_power_down_sync(struct hda_codec *codec, struct nid_path *path)
741{
742 struct hda_gen_spec *spec = codec->spec;
743 bool changed;
744 int i;
745
746 if (!spec->power_down_unused || path->active)
747 return;
748
749 for (i = 0; i < path->depth; i++) {
750 hda_nid_t nid = path->path[i];
751 if (!snd_hda_check_power_state(codec, nid, AC_PWRST_D3)) {
752 snd_hda_codec_write(codec, nid, 0,
753 AC_VERB_SET_POWER_STATE,
754 AC_PWRST_D3);
755 changed = true;
756 }
757 }
758
759 if (changed) {
760 msleep(10);
761 snd_hda_codec_read(codec, path->path[0], 0,
762 AC_VERB_GET_POWER_STATE, 0);
763 }
764}
765
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +0100766/* turn on/off EAPD on the given pin */
767static void set_pin_eapd(struct hda_codec *codec, hda_nid_t pin, bool enable)
768{
769 struct hda_gen_spec *spec = codec->spec;
770 if (spec->own_eapd_ctl ||
771 !(snd_hda_query_pin_caps(codec, pin) & AC_PINCAP_EAPD))
772 return;
Takashi Iwaiecac3ed2012-12-21 15:23:01 +0100773 if (codec->inv_eapd)
774 enable = !enable;
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +0100775 snd_hda_codec_update_cache(codec, pin, 0,
776 AC_VERB_SET_EAPD_BTLENABLE,
777 enable ? 0x02 : 0x00);
778}
779
Takashi Iwai3e367f12013-01-23 17:07:23 +0100780/* re-initialize the path specified by the given path index */
781static void resume_path_from_idx(struct hda_codec *codec, int path_idx)
782{
783 struct nid_path *path = snd_hda_get_path_from_idx(codec, path_idx);
784 if (path)
785 snd_hda_activate_path(codec, path, path->active, false);
786}
787
Takashi Iwai352f7f92012-12-19 12:52:06 +0100788
789/*
790 * Helper functions for creating mixer ctl elements
791 */
792
793enum {
794 HDA_CTL_WIDGET_VOL,
795 HDA_CTL_WIDGET_MUTE,
796 HDA_CTL_BIND_MUTE,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100797};
798static const struct snd_kcontrol_new control_templates[] = {
799 HDA_CODEC_VOLUME(NULL, 0, 0, 0),
800 HDA_CODEC_MUTE(NULL, 0, 0, 0),
801 HDA_BIND_MUTE(NULL, 0, 0, 0),
Takashi Iwai352f7f92012-12-19 12:52:06 +0100802};
803
804/* add dynamic controls from template */
Takashi Iwaia35bd1e2013-01-18 14:01:14 +0100805static struct snd_kcontrol_new *
806add_control(struct hda_gen_spec *spec, int type, const char *name,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100807 int cidx, unsigned long val)
808{
809 struct snd_kcontrol_new *knew;
810
Takashi Iwai12c93df2012-12-19 14:38:33 +0100811 knew = snd_hda_gen_add_kctl(spec, name, &control_templates[type]);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100812 if (!knew)
Takashi Iwaia35bd1e2013-01-18 14:01:14 +0100813 return NULL;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100814 knew->index = cidx;
815 if (get_amp_nid_(val))
816 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
817 knew->private_value = val;
Takashi Iwaia35bd1e2013-01-18 14:01:14 +0100818 return knew;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100819}
820
821static int add_control_with_pfx(struct hda_gen_spec *spec, int type,
822 const char *pfx, const char *dir,
823 const char *sfx, int cidx, unsigned long val)
824{
825 char name[32];
826 snprintf(name, sizeof(name), "%s %s %s", pfx, dir, sfx);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +0100827 if (!add_control(spec, type, name, cidx, val))
828 return -ENOMEM;
829 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100830}
831
832#define add_pb_vol_ctrl(spec, type, pfx, val) \
833 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", 0, val)
834#define add_pb_sw_ctrl(spec, type, pfx, val) \
835 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", 0, val)
836#define __add_pb_vol_ctrl(spec, type, pfx, cidx, val) \
837 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", cidx, val)
838#define __add_pb_sw_ctrl(spec, type, pfx, cidx, val) \
839 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", cidx, val)
840
841static int add_vol_ctl(struct hda_codec *codec, const char *pfx, int cidx,
842 unsigned int chs, struct nid_path *path)
843{
844 unsigned int val;
845 if (!path)
846 return 0;
847 val = path->ctls[NID_PATH_VOL_CTL];
848 if (!val)
849 return 0;
850 val = amp_val_replace_channels(val, chs);
851 return __add_pb_vol_ctrl(codec->spec, HDA_CTL_WIDGET_VOL, pfx, cidx, val);
852}
853
854/* return the channel bits suitable for the given path->ctls[] */
855static int get_default_ch_nums(struct hda_codec *codec, struct nid_path *path,
856 int type)
857{
858 int chs = 1; /* mono (left only) */
859 if (path) {
860 hda_nid_t nid = get_amp_nid_(path->ctls[type]);
861 if (nid && (get_wcaps(codec, nid) & AC_WCAP_STEREO))
862 chs = 3; /* stereo */
863 }
864 return chs;
865}
866
867static int add_stereo_vol(struct hda_codec *codec, const char *pfx, int cidx,
868 struct nid_path *path)
869{
870 int chs = get_default_ch_nums(codec, path, NID_PATH_VOL_CTL);
871 return add_vol_ctl(codec, pfx, cidx, chs, path);
872}
873
874/* create a mute-switch for the given mixer widget;
875 * if it has multiple sources (e.g. DAC and loopback), create a bind-mute
876 */
877static int add_sw_ctl(struct hda_codec *codec, const char *pfx, int cidx,
878 unsigned int chs, struct nid_path *path)
879{
880 unsigned int val;
881 int type = HDA_CTL_WIDGET_MUTE;
882
883 if (!path)
884 return 0;
885 val = path->ctls[NID_PATH_MUTE_CTL];
886 if (!val)
887 return 0;
888 val = amp_val_replace_channels(val, chs);
889 if (get_amp_direction_(val) == HDA_INPUT) {
890 hda_nid_t nid = get_amp_nid_(val);
891 int nums = snd_hda_get_num_conns(codec, nid);
892 if (nums > 1) {
893 type = HDA_CTL_BIND_MUTE;
894 val |= nums << 19;
895 }
896 }
897 return __add_pb_sw_ctrl(codec->spec, type, pfx, cidx, val);
898}
899
900static int add_stereo_sw(struct hda_codec *codec, const char *pfx,
901 int cidx, struct nid_path *path)
902{
903 int chs = get_default_ch_nums(codec, path, NID_PATH_MUTE_CTL);
904 return add_sw_ctl(codec, pfx, cidx, chs, path);
905}
906
Takashi Iwai247d85e2013-01-17 16:18:11 +0100907/* any ctl assigned to the path with the given index? */
908static bool path_has_mixer(struct hda_codec *codec, int path_idx, int ctl_type)
909{
910 struct nid_path *path = snd_hda_get_path_from_idx(codec, path_idx);
911 return path && path->ctls[ctl_type];
912}
913
Takashi Iwai352f7f92012-12-19 12:52:06 +0100914static const char * const channel_name[4] = {
915 "Front", "Surround", "CLFE", "Side"
916};
917
918/* give some appropriate ctl name prefix for the given line out channel */
Takashi Iwai247d85e2013-01-17 16:18:11 +0100919static const char *get_line_out_pfx(struct hda_codec *codec, int ch,
920 int *index, int ctl_type)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100921{
Takashi Iwai247d85e2013-01-17 16:18:11 +0100922 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100923 struct auto_pin_cfg *cfg = &spec->autocfg;
924
925 *index = 0;
926 if (cfg->line_outs == 1 && !spec->multi_ios &&
Takashi Iwai247d85e2013-01-17 16:18:11 +0100927 !cfg->hp_outs && !cfg->speaker_outs)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100928 return spec->vmaster_mute.hook ? "PCM" : "Master";
929
930 /* if there is really a single DAC used in the whole output paths,
931 * use it master (or "PCM" if a vmaster hook is present)
932 */
933 if (spec->multiout.num_dacs == 1 && !spec->mixer_nid &&
934 !spec->multiout.hp_out_nid[0] && !spec->multiout.extra_out_nid[0])
935 return spec->vmaster_mute.hook ? "PCM" : "Master";
936
Takashi Iwai247d85e2013-01-17 16:18:11 +0100937 /* multi-io channels */
938 if (ch >= cfg->line_outs)
939 return channel_name[ch];
940
Takashi Iwai352f7f92012-12-19 12:52:06 +0100941 switch (cfg->line_out_type) {
942 case AUTO_PIN_SPEAKER_OUT:
Takashi Iwai247d85e2013-01-17 16:18:11 +0100943 /* if the primary channel vol/mute is shared with HP volume,
944 * don't name it as Speaker
945 */
946 if (!ch && cfg->hp_outs &&
947 !path_has_mixer(codec, spec->hp_paths[0], ctl_type))
948 break;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100949 if (cfg->line_outs == 1)
950 return "Speaker";
951 if (cfg->line_outs == 2)
952 return ch ? "Bass Speaker" : "Speaker";
953 break;
954 case AUTO_PIN_HP_OUT:
Takashi Iwai247d85e2013-01-17 16:18:11 +0100955 /* if the primary channel vol/mute is shared with spk volume,
956 * don't name it as Headphone
957 */
958 if (!ch && cfg->speaker_outs &&
959 !path_has_mixer(codec, spec->speaker_paths[0], ctl_type))
960 break;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100961 /* for multi-io case, only the primary out */
962 if (ch && spec->multi_ios)
963 break;
964 *index = ch;
965 return "Headphone";
Takashi Iwai352f7f92012-12-19 12:52:06 +0100966 }
Takashi Iwai247d85e2013-01-17 16:18:11 +0100967
968 /* for a single channel output, we don't have to name the channel */
969 if (cfg->line_outs == 1 && !spec->multi_ios)
970 return "PCM";
971
Takashi Iwai352f7f92012-12-19 12:52:06 +0100972 if (ch >= ARRAY_SIZE(channel_name)) {
973 snd_BUG();
974 return "PCM";
975 }
976
977 return channel_name[ch];
978}
979
980/*
981 * Parse output paths
982 */
983
984/* badness definition */
985enum {
986 /* No primary DAC is found for the main output */
987 BAD_NO_PRIMARY_DAC = 0x10000,
988 /* No DAC is found for the extra output */
989 BAD_NO_DAC = 0x4000,
990 /* No possible multi-ios */
991 BAD_MULTI_IO = 0x103,
992 /* No individual DAC for extra output */
993 BAD_NO_EXTRA_DAC = 0x102,
994 /* No individual DAC for extra surrounds */
995 BAD_NO_EXTRA_SURR_DAC = 0x101,
996 /* Primary DAC shared with main surrounds */
997 BAD_SHARED_SURROUND = 0x100,
998 /* Primary DAC shared with main CLFE */
999 BAD_SHARED_CLFE = 0x10,
1000 /* Primary DAC shared with extra surrounds */
1001 BAD_SHARED_EXTRA_SURROUND = 0x10,
1002 /* Volume widget is shared */
1003 BAD_SHARED_VOL = 0x10,
1004};
1005
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001006/* look for widgets in the given path which are appropriate for
Takashi Iwai352f7f92012-12-19 12:52:06 +01001007 * volume and mute controls, and assign the values to ctls[].
1008 *
1009 * When no appropriate widget is found in the path, the badness value
1010 * is incremented depending on the situation. The function returns the
1011 * total badness for both volume and mute controls.
1012 */
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001013static int assign_out_path_ctls(struct hda_codec *codec, struct nid_path *path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001014{
Takashi Iwai352f7f92012-12-19 12:52:06 +01001015 hda_nid_t nid;
1016 unsigned int val;
1017 int badness = 0;
1018
1019 if (!path)
1020 return BAD_SHARED_VOL * 2;
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001021
1022 if (path->ctls[NID_PATH_VOL_CTL] ||
1023 path->ctls[NID_PATH_MUTE_CTL])
1024 return 0; /* already evaluated */
1025
Takashi Iwai352f7f92012-12-19 12:52:06 +01001026 nid = look_for_out_vol_nid(codec, path);
1027 if (nid) {
1028 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
1029 if (is_ctl_used(codec, val, NID_PATH_VOL_CTL))
1030 badness += BAD_SHARED_VOL;
1031 else
1032 path->ctls[NID_PATH_VOL_CTL] = val;
1033 } else
1034 badness += BAD_SHARED_VOL;
1035 nid = look_for_out_mute_nid(codec, path);
1036 if (nid) {
1037 unsigned int wid_type = get_wcaps_type(get_wcaps(codec, nid));
1038 if (wid_type == AC_WID_PIN || wid_type == AC_WID_AUD_OUT ||
1039 nid_has_mute(codec, nid, HDA_OUTPUT))
1040 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
1041 else
1042 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT);
1043 if (is_ctl_used(codec, val, NID_PATH_MUTE_CTL))
1044 badness += BAD_SHARED_VOL;
1045 else
1046 path->ctls[NID_PATH_MUTE_CTL] = val;
1047 } else
1048 badness += BAD_SHARED_VOL;
1049 return badness;
1050}
1051
1052struct badness_table {
1053 int no_primary_dac; /* no primary DAC */
1054 int no_dac; /* no secondary DACs */
1055 int shared_primary; /* primary DAC is shared with main output */
1056 int shared_surr; /* secondary DAC shared with main or primary */
1057 int shared_clfe; /* third DAC shared with main or primary */
1058 int shared_surr_main; /* secondary DAC sahred with main/DAC0 */
1059};
1060
1061static struct badness_table main_out_badness = {
1062 .no_primary_dac = BAD_NO_PRIMARY_DAC,
1063 .no_dac = BAD_NO_DAC,
1064 .shared_primary = BAD_NO_PRIMARY_DAC,
1065 .shared_surr = BAD_SHARED_SURROUND,
1066 .shared_clfe = BAD_SHARED_CLFE,
1067 .shared_surr_main = BAD_SHARED_SURROUND,
1068};
1069
1070static struct badness_table extra_out_badness = {
1071 .no_primary_dac = BAD_NO_DAC,
1072 .no_dac = BAD_NO_DAC,
1073 .shared_primary = BAD_NO_EXTRA_DAC,
1074 .shared_surr = BAD_SHARED_EXTRA_SURROUND,
1075 .shared_clfe = BAD_SHARED_EXTRA_SURROUND,
1076 .shared_surr_main = BAD_NO_EXTRA_SURR_DAC,
1077};
1078
Takashi Iwai7385df62013-01-07 09:50:52 +01001079/* get the DAC of the primary output corresponding to the given array index */
1080static hda_nid_t get_primary_out(struct hda_codec *codec, int idx)
1081{
1082 struct hda_gen_spec *spec = codec->spec;
1083 struct auto_pin_cfg *cfg = &spec->autocfg;
1084
1085 if (cfg->line_outs > idx)
1086 return spec->private_dac_nids[idx];
1087 idx -= cfg->line_outs;
1088 if (spec->multi_ios > idx)
1089 return spec->multi_io[idx].dac;
1090 return 0;
1091}
1092
1093/* return the DAC if it's reachable, otherwise zero */
1094static inline hda_nid_t try_dac(struct hda_codec *codec,
1095 hda_nid_t dac, hda_nid_t pin)
1096{
1097 return is_reachable_path(codec, dac, pin) ? dac : 0;
1098}
1099
Takashi Iwai352f7f92012-12-19 12:52:06 +01001100/* try to assign DACs to pins and return the resultant badness */
1101static int try_assign_dacs(struct hda_codec *codec, int num_outs,
1102 const hda_nid_t *pins, hda_nid_t *dacs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001103 int *path_idx,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001104 const struct badness_table *bad)
1105{
1106 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001107 int i, j;
1108 int badness = 0;
1109 hda_nid_t dac;
1110
1111 if (!num_outs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 return 0;
1113
Takashi Iwai352f7f92012-12-19 12:52:06 +01001114 for (i = 0; i < num_outs; i++) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001115 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001116 hda_nid_t pin = pins[i];
Takashi Iwai1e0b5282013-01-04 12:56:52 +01001117
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001118 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
1119 if (path) {
1120 badness += assign_out_path_ctls(codec, path);
Takashi Iwai1e0b5282013-01-04 12:56:52 +01001121 continue;
1122 }
1123
1124 dacs[i] = look_for_dac(codec, pin, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001125 if (!dacs[i] && !i) {
Takashi Iwai980428c2013-01-09 09:28:20 +01001126 /* try to steal the DAC of surrounds for the front */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001127 for (j = 1; j < num_outs; j++) {
1128 if (is_reachable_path(codec, dacs[j], pin)) {
1129 dacs[0] = dacs[j];
1130 dacs[j] = 0;
Takashi Iwai980428c2013-01-09 09:28:20 +01001131 invalidate_nid_path(codec, path_idx[j]);
Takashi Iwai196c17662013-01-04 15:01:40 +01001132 path_idx[j] = 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001133 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 }
1135 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001136 }
1137 dac = dacs[i];
1138 if (!dac) {
Takashi Iwai7385df62013-01-07 09:50:52 +01001139 if (num_outs > 2)
1140 dac = try_dac(codec, get_primary_out(codec, i), pin);
1141 if (!dac)
1142 dac = try_dac(codec, dacs[0], pin);
1143 if (!dac)
1144 dac = try_dac(codec, get_primary_out(codec, i), pin);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001145 if (dac) {
1146 if (!i)
1147 badness += bad->shared_primary;
1148 else if (i == 1)
1149 badness += bad->shared_surr;
1150 else
1151 badness += bad->shared_clfe;
1152 } else if (is_reachable_path(codec, spec->private_dac_nids[0], pin)) {
1153 dac = spec->private_dac_nids[0];
1154 badness += bad->shared_surr_main;
1155 } else if (!i)
1156 badness += bad->no_primary_dac;
1157 else
1158 badness += bad->no_dac;
1159 }
Takashi Iwai1fa335b2013-01-21 11:43:19 +01001160 if (!dac)
1161 continue;
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001162 path = snd_hda_add_new_path(codec, dac, pin, -spec->mixer_nid);
Takashi Iwai117688a2013-01-04 15:41:41 +01001163 if (!path && !i && spec->mixer_nid) {
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001164 /* try with aamix */
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001165 path = snd_hda_add_new_path(codec, dac, pin, 0);
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001166 }
Takashi Iwai1fa335b2013-01-21 11:43:19 +01001167 if (!path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001168 dac = dacs[i] = 0;
Takashi Iwai1fa335b2013-01-21 11:43:19 +01001169 badness += bad->no_dac;
1170 } else {
Takashi Iwaia7694092013-01-21 10:43:18 +01001171 /* print_nid_path("output", path); */
Takashi Iwaie1284af2013-01-03 16:33:02 +01001172 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +01001173 path_idx[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001174 badness += assign_out_path_ctls(codec, path);
Takashi Iwaie1284af2013-01-03 16:33:02 +01001175 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001176 }
1177
1178 return badness;
1179}
1180
1181/* return NID if the given pin has only a single connection to a certain DAC */
1182static hda_nid_t get_dac_if_single(struct hda_codec *codec, hda_nid_t pin)
1183{
1184 struct hda_gen_spec *spec = codec->spec;
1185 int i;
1186 hda_nid_t nid_found = 0;
1187
1188 for (i = 0; i < spec->num_all_dacs; i++) {
1189 hda_nid_t nid = spec->all_dacs[i];
1190 if (!nid || is_dac_already_used(codec, nid))
1191 continue;
1192 if (is_reachable_path(codec, nid, pin)) {
1193 if (nid_found)
1194 return 0;
1195 nid_found = nid;
1196 }
1197 }
1198 return nid_found;
1199}
1200
1201/* check whether the given pin can be a multi-io pin */
1202static bool can_be_multiio_pin(struct hda_codec *codec,
1203 unsigned int location, hda_nid_t nid)
1204{
1205 unsigned int defcfg, caps;
1206
1207 defcfg = snd_hda_codec_get_pincfg(codec, nid);
1208 if (get_defcfg_connect(defcfg) != AC_JACK_PORT_COMPLEX)
1209 return false;
1210 if (location && get_defcfg_location(defcfg) != location)
1211 return false;
1212 caps = snd_hda_query_pin_caps(codec, nid);
1213 if (!(caps & AC_PINCAP_OUT))
1214 return false;
1215 return true;
1216}
1217
Takashi Iwaie22aab72013-01-04 14:50:04 +01001218/* count the number of input pins that are capable to be multi-io */
1219static int count_multiio_pins(struct hda_codec *codec, hda_nid_t reference_pin)
1220{
1221 struct hda_gen_spec *spec = codec->spec;
1222 struct auto_pin_cfg *cfg = &spec->autocfg;
1223 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1224 unsigned int location = get_defcfg_location(defcfg);
1225 int type, i;
1226 int num_pins = 0;
1227
1228 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1229 for (i = 0; i < cfg->num_inputs; i++) {
1230 if (cfg->inputs[i].type != type)
1231 continue;
1232 if (can_be_multiio_pin(codec, location,
1233 cfg->inputs[i].pin))
1234 num_pins++;
1235 }
1236 }
1237 return num_pins;
1238}
1239
Takashi Iwai352f7f92012-12-19 12:52:06 +01001240/*
1241 * multi-io helper
1242 *
1243 * When hardwired is set, try to fill ony hardwired pins, and returns
1244 * zero if any pins are filled, non-zero if nothing found.
1245 * When hardwired is off, try to fill possible input pins, and returns
1246 * the badness value.
1247 */
1248static int fill_multi_ios(struct hda_codec *codec,
1249 hda_nid_t reference_pin,
Takashi Iwaie22aab72013-01-04 14:50:04 +01001250 bool hardwired)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001251{
1252 struct hda_gen_spec *spec = codec->spec;
1253 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwaie22aab72013-01-04 14:50:04 +01001254 int type, i, j, num_pins, old_pins;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001255 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1256 unsigned int location = get_defcfg_location(defcfg);
1257 int badness = 0;
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001258 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001259
1260 old_pins = spec->multi_ios;
1261 if (old_pins >= 2)
1262 goto end_fill;
1263
Takashi Iwaie22aab72013-01-04 14:50:04 +01001264 num_pins = count_multiio_pins(codec, reference_pin);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001265 if (num_pins < 2)
1266 goto end_fill;
1267
Takashi Iwai352f7f92012-12-19 12:52:06 +01001268 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1269 for (i = 0; i < cfg->num_inputs; i++) {
1270 hda_nid_t nid = cfg->inputs[i].pin;
1271 hda_nid_t dac = 0;
1272
1273 if (cfg->inputs[i].type != type)
1274 continue;
1275 if (!can_be_multiio_pin(codec, location, nid))
1276 continue;
1277 for (j = 0; j < spec->multi_ios; j++) {
1278 if (nid == spec->multi_io[j].pin)
1279 break;
1280 }
1281 if (j < spec->multi_ios)
1282 continue;
1283
Takashi Iwai352f7f92012-12-19 12:52:06 +01001284 if (hardwired)
1285 dac = get_dac_if_single(codec, nid);
1286 else if (!dac)
1287 dac = look_for_dac(codec, nid, false);
1288 if (!dac) {
1289 badness++;
1290 continue;
1291 }
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001292 path = snd_hda_add_new_path(codec, dac, nid,
1293 -spec->mixer_nid);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001294 if (!path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001295 badness++;
1296 continue;
1297 }
Takashi Iwaia7694092013-01-21 10:43:18 +01001298 /* print_nid_path("multiio", path); */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001299 spec->multi_io[spec->multi_ios].pin = nid;
1300 spec->multi_io[spec->multi_ios].dac = dac;
Takashi Iwai196c17662013-01-04 15:01:40 +01001301 spec->out_paths[cfg->line_outs + spec->multi_ios] =
1302 snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001303 spec->multi_ios++;
1304 if (spec->multi_ios >= 2)
1305 break;
1306 }
1307 }
1308 end_fill:
1309 if (badness)
1310 badness = BAD_MULTI_IO;
1311 if (old_pins == spec->multi_ios) {
1312 if (hardwired)
1313 return 1; /* nothing found */
1314 else
1315 return badness; /* no badness if nothing found */
1316 }
1317 if (!hardwired && spec->multi_ios < 2) {
1318 /* cancel newly assigned paths */
1319 spec->paths.used -= spec->multi_ios - old_pins;
1320 spec->multi_ios = old_pins;
1321 return badness;
1322 }
1323
1324 /* assign volume and mute controls */
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001325 for (i = old_pins; i < spec->multi_ios; i++) {
1326 path = snd_hda_get_path_from_idx(codec, spec->out_paths[cfg->line_outs + i]);
1327 badness += assign_out_path_ctls(codec, path);
1328 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001329
1330 return badness;
1331}
1332
1333/* map DACs for all pins in the list if they are single connections */
1334static bool map_singles(struct hda_codec *codec, int outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001335 const hda_nid_t *pins, hda_nid_t *dacs, int *path_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001336{
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001337 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001338 int i;
1339 bool found = false;
1340 for (i = 0; i < outs; i++) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001341 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001342 hda_nid_t dac;
1343 if (dacs[i])
1344 continue;
1345 dac = get_dac_if_single(codec, pins[i]);
1346 if (!dac)
1347 continue;
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001348 path = snd_hda_add_new_path(codec, dac, pins[i],
1349 -spec->mixer_nid);
Takashi Iwai117688a2013-01-04 15:41:41 +01001350 if (!path && !i && spec->mixer_nid)
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001351 path = snd_hda_add_new_path(codec, dac, pins[i], 0);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001352 if (path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001353 dacs[i] = dac;
1354 found = true;
Takashi Iwaia7694092013-01-21 10:43:18 +01001355 /* print_nid_path("output", path); */
Takashi Iwaie1284af2013-01-03 16:33:02 +01001356 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +01001357 path_idx[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001358 }
1359 }
1360 return found;
1361}
1362
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001363/* create a new path including aamix if available, and return its index */
1364static int check_aamix_out_path(struct hda_codec *codec, int path_idx)
1365{
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001366 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001367 struct nid_path *path;
Takashi Iwaif87498b2013-01-21 14:24:31 +01001368 hda_nid_t dac, pin;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001369
1370 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001371 if (!path || !path->depth ||
1372 is_nid_contained(path, spec->mixer_nid))
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001373 return 0;
Takashi Iwaif87498b2013-01-21 14:24:31 +01001374 dac = path->path[0];
1375 pin = path->path[path->depth - 1];
1376 path = snd_hda_add_new_path(codec, dac, pin, spec->mixer_nid);
1377 if (!path) {
1378 if (dac != spec->multiout.dac_nids[0])
1379 dac = spec->multiout.dac_nids[0];
1380 else if (spec->multiout.hp_out_nid[0])
1381 dac = spec->multiout.hp_out_nid[0];
1382 else if (spec->multiout.extra_out_nid[0])
1383 dac = spec->multiout.extra_out_nid[0];
1384 if (dac)
1385 path = snd_hda_add_new_path(codec, dac, pin,
1386 spec->mixer_nid);
1387 }
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001388 if (!path)
1389 return 0;
Takashi Iwaia7694092013-01-21 10:43:18 +01001390 /* print_nid_path("output-aamix", path); */
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001391 path->active = false; /* unused as default */
1392 return snd_hda_get_path_idx(codec, path);
1393}
1394
Takashi Iwaia07a9492013-01-07 16:44:06 +01001395/* fill the empty entries in the dac array for speaker/hp with the
1396 * shared dac pointed by the paths
1397 */
1398static void refill_shared_dacs(struct hda_codec *codec, int num_outs,
1399 hda_nid_t *dacs, int *path_idx)
1400{
1401 struct nid_path *path;
1402 int i;
1403
1404 for (i = 0; i < num_outs; i++) {
1405 if (dacs[i])
1406 continue;
1407 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
1408 if (!path)
1409 continue;
1410 dacs[i] = path->path[0];
1411 }
1412}
1413
Takashi Iwai352f7f92012-12-19 12:52:06 +01001414/* fill in the dac_nids table from the parsed pin configuration */
1415static int fill_and_eval_dacs(struct hda_codec *codec,
1416 bool fill_hardwired,
1417 bool fill_mio_first)
1418{
1419 struct hda_gen_spec *spec = codec->spec;
1420 struct auto_pin_cfg *cfg = &spec->autocfg;
1421 int i, err, badness;
1422
1423 /* set num_dacs once to full for look_for_dac() */
1424 spec->multiout.num_dacs = cfg->line_outs;
1425 spec->multiout.dac_nids = spec->private_dac_nids;
1426 memset(spec->private_dac_nids, 0, sizeof(spec->private_dac_nids));
1427 memset(spec->multiout.hp_out_nid, 0, sizeof(spec->multiout.hp_out_nid));
1428 memset(spec->multiout.extra_out_nid, 0, sizeof(spec->multiout.extra_out_nid));
1429 spec->multi_ios = 0;
1430 snd_array_free(&spec->paths);
Takashi Iwaicd5be3f2013-01-07 15:07:00 +01001431
1432 /* clear path indices */
1433 memset(spec->out_paths, 0, sizeof(spec->out_paths));
1434 memset(spec->hp_paths, 0, sizeof(spec->hp_paths));
1435 memset(spec->speaker_paths, 0, sizeof(spec->speaker_paths));
1436 memset(spec->aamix_out_paths, 0, sizeof(spec->aamix_out_paths));
1437 memset(spec->digout_paths, 0, sizeof(spec->digout_paths));
Takashi Iwaic697b712013-01-07 17:09:26 +01001438 memset(spec->input_paths, 0, sizeof(spec->input_paths));
Takashi Iwaicd5be3f2013-01-07 15:07:00 +01001439 memset(spec->loopback_paths, 0, sizeof(spec->loopback_paths));
1440 memset(&spec->digin_path, 0, sizeof(spec->digin_path));
1441
Takashi Iwai352f7f92012-12-19 12:52:06 +01001442 badness = 0;
1443
1444 /* fill hard-wired DACs first */
1445 if (fill_hardwired) {
1446 bool mapped;
1447 do {
1448 mapped = map_singles(codec, cfg->line_outs,
1449 cfg->line_out_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001450 spec->private_dac_nids,
1451 spec->out_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001452 mapped |= map_singles(codec, cfg->hp_outs,
1453 cfg->hp_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001454 spec->multiout.hp_out_nid,
1455 spec->hp_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001456 mapped |= map_singles(codec, cfg->speaker_outs,
1457 cfg->speaker_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001458 spec->multiout.extra_out_nid,
1459 spec->speaker_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001460 if (fill_mio_first && cfg->line_outs == 1 &&
1461 cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
Takashi Iwaie22aab72013-01-04 14:50:04 +01001462 err = fill_multi_ios(codec, cfg->line_out_pins[0], true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001463 if (!err)
1464 mapped = true;
1465 }
1466 } while (mapped);
1467 }
1468
1469 badness += try_assign_dacs(codec, cfg->line_outs, cfg->line_out_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001470 spec->private_dac_nids, spec->out_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001471 &main_out_badness);
1472
Takashi Iwai352f7f92012-12-19 12:52:06 +01001473 if (fill_mio_first &&
1474 cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1475 /* try to fill multi-io first */
Takashi Iwaie22aab72013-01-04 14:50:04 +01001476 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001477 if (err < 0)
1478 return err;
1479 /* we don't count badness at this stage yet */
1480 }
1481
1482 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
1483 err = try_assign_dacs(codec, cfg->hp_outs, cfg->hp_pins,
1484 spec->multiout.hp_out_nid,
Takashi Iwai196c17662013-01-04 15:01:40 +01001485 spec->hp_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001486 &extra_out_badness);
1487 if (err < 0)
1488 return err;
1489 badness += err;
1490 }
1491 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1492 err = try_assign_dacs(codec, cfg->speaker_outs,
1493 cfg->speaker_pins,
1494 spec->multiout.extra_out_nid,
Takashi Iwai196c17662013-01-04 15:01:40 +01001495 spec->speaker_paths,
1496 &extra_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001497 if (err < 0)
1498 return err;
1499 badness += err;
1500 }
1501 if (cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
Takashi Iwaie22aab72013-01-04 14:50:04 +01001502 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001503 if (err < 0)
1504 return err;
1505 badness += err;
1506 }
Takashi Iwaie22aab72013-01-04 14:50:04 +01001507
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001508 if (spec->mixer_nid) {
1509 spec->aamix_out_paths[0] =
1510 check_aamix_out_path(codec, spec->out_paths[0]);
1511 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1512 spec->aamix_out_paths[1] =
1513 check_aamix_out_path(codec, spec->hp_paths[0]);
1514 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1515 spec->aamix_out_paths[2] =
1516 check_aamix_out_path(codec, spec->speaker_paths[0]);
1517 }
1518
Takashi Iwaie22aab72013-01-04 14:50:04 +01001519 if (cfg->hp_outs && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
1520 if (count_multiio_pins(codec, cfg->hp_pins[0]) >= 2)
1521 spec->multi_ios = 1; /* give badness */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001522
Takashi Iwaia07a9492013-01-07 16:44:06 +01001523 /* re-count num_dacs and squash invalid entries */
1524 spec->multiout.num_dacs = 0;
1525 for (i = 0; i < cfg->line_outs; i++) {
1526 if (spec->private_dac_nids[i])
1527 spec->multiout.num_dacs++;
1528 else {
1529 memmove(spec->private_dac_nids + i,
1530 spec->private_dac_nids + i + 1,
1531 sizeof(hda_nid_t) * (cfg->line_outs - i - 1));
1532 spec->private_dac_nids[cfg->line_outs - 1] = 0;
1533 }
1534 }
1535
1536 spec->ext_channel_count = spec->min_channel_count =
David Henningssonc0f3b212013-01-16 11:45:37 +01001537 spec->multiout.num_dacs * 2;
Takashi Iwaia07a9492013-01-07 16:44:06 +01001538
Takashi Iwai352f7f92012-12-19 12:52:06 +01001539 if (spec->multi_ios == 2) {
1540 for (i = 0; i < 2; i++)
1541 spec->private_dac_nids[spec->multiout.num_dacs++] =
1542 spec->multi_io[i].dac;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001543 } else if (spec->multi_ios) {
1544 spec->multi_ios = 0;
1545 badness += BAD_MULTI_IO;
1546 }
1547
Takashi Iwaia07a9492013-01-07 16:44:06 +01001548 /* re-fill the shared DAC for speaker / headphone */
1549 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1550 refill_shared_dacs(codec, cfg->hp_outs,
1551 spec->multiout.hp_out_nid,
1552 spec->hp_paths);
1553 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1554 refill_shared_dacs(codec, cfg->speaker_outs,
1555 spec->multiout.extra_out_nid,
1556 spec->speaker_paths);
1557
Takashi Iwai352f7f92012-12-19 12:52:06 +01001558 return badness;
1559}
1560
1561#define DEBUG_BADNESS
1562
1563#ifdef DEBUG_BADNESS
1564#define debug_badness snd_printdd
1565#else
1566#define debug_badness(...)
1567#endif
1568
Takashi Iwaia7694092013-01-21 10:43:18 +01001569#ifdef DEBUG_BADNESS
1570static inline void print_nid_path_idx(struct hda_codec *codec,
1571 const char *pfx, int idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001572{
Takashi Iwaia7694092013-01-21 10:43:18 +01001573 struct nid_path *path;
1574
1575 path = snd_hda_get_path_from_idx(codec, idx);
1576 if (path)
1577 print_nid_path(pfx, path);
1578}
1579
1580static void debug_show_configs(struct hda_codec *codec,
1581 struct auto_pin_cfg *cfg)
1582{
1583 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia7694092013-01-21 10:43:18 +01001584 static const char * const lo_type[3] = { "LO", "SP", "HP" };
Takashi Iwaia7694092013-01-21 10:43:18 +01001585 int i;
1586
1587 debug_badness("multi_outs = %x/%x/%x/%x : %x/%x/%x/%x (type %s)\n",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001588 cfg->line_out_pins[0], cfg->line_out_pins[1],
Takashi Iwai708122e2012-12-20 17:56:57 +01001589 cfg->line_out_pins[2], cfg->line_out_pins[3],
Takashi Iwai352f7f92012-12-19 12:52:06 +01001590 spec->multiout.dac_nids[0],
1591 spec->multiout.dac_nids[1],
1592 spec->multiout.dac_nids[2],
Takashi Iwaia7694092013-01-21 10:43:18 +01001593 spec->multiout.dac_nids[3],
1594 lo_type[cfg->line_out_type]);
1595 for (i = 0; i < cfg->line_outs; i++)
1596 print_nid_path_idx(codec, " out", spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001597 if (spec->multi_ios > 0)
1598 debug_badness("multi_ios(%d) = %x/%x : %x/%x\n",
1599 spec->multi_ios,
1600 spec->multi_io[0].pin, spec->multi_io[1].pin,
1601 spec->multi_io[0].dac, spec->multi_io[1].dac);
Takashi Iwaia7694092013-01-21 10:43:18 +01001602 for (i = 0; i < spec->multi_ios; i++)
1603 print_nid_path_idx(codec, " mio",
1604 spec->out_paths[cfg->line_outs + i]);
1605 if (cfg->hp_outs)
1606 debug_badness("hp_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001607 cfg->hp_pins[0], cfg->hp_pins[1],
Takashi Iwai708122e2012-12-20 17:56:57 +01001608 cfg->hp_pins[2], cfg->hp_pins[3],
Takashi Iwai352f7f92012-12-19 12:52:06 +01001609 spec->multiout.hp_out_nid[0],
1610 spec->multiout.hp_out_nid[1],
1611 spec->multiout.hp_out_nid[2],
1612 spec->multiout.hp_out_nid[3]);
Takashi Iwaia7694092013-01-21 10:43:18 +01001613 for (i = 0; i < cfg->hp_outs; i++)
1614 print_nid_path_idx(codec, " hp ", spec->hp_paths[i]);
1615 if (cfg->speaker_outs)
1616 debug_badness("spk_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001617 cfg->speaker_pins[0], cfg->speaker_pins[1],
1618 cfg->speaker_pins[2], cfg->speaker_pins[3],
1619 spec->multiout.extra_out_nid[0],
1620 spec->multiout.extra_out_nid[1],
1621 spec->multiout.extra_out_nid[2],
1622 spec->multiout.extra_out_nid[3]);
Takashi Iwaia7694092013-01-21 10:43:18 +01001623 for (i = 0; i < cfg->speaker_outs; i++)
1624 print_nid_path_idx(codec, " spk", spec->speaker_paths[i]);
1625 for (i = 0; i < 3; i++)
1626 print_nid_path_idx(codec, " mix", spec->aamix_out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001627}
Takashi Iwaia7694092013-01-21 10:43:18 +01001628#else
1629#define debug_show_configs(codec, cfg) /* NOP */
1630#endif
Takashi Iwai352f7f92012-12-19 12:52:06 +01001631
1632/* find all available DACs of the codec */
1633static void fill_all_dac_nids(struct hda_codec *codec)
1634{
1635 struct hda_gen_spec *spec = codec->spec;
1636 int i;
1637 hda_nid_t nid = codec->start_nid;
1638
1639 spec->num_all_dacs = 0;
1640 memset(spec->all_dacs, 0, sizeof(spec->all_dacs));
1641 for (i = 0; i < codec->num_nodes; i++, nid++) {
1642 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_AUD_OUT)
1643 continue;
1644 if (spec->num_all_dacs >= ARRAY_SIZE(spec->all_dacs)) {
1645 snd_printk(KERN_ERR "hda: Too many DACs!\n");
1646 break;
1647 }
1648 spec->all_dacs[spec->num_all_dacs++] = nid;
1649 }
1650}
1651
1652static int parse_output_paths(struct hda_codec *codec)
1653{
1654 struct hda_gen_spec *spec = codec->spec;
1655 struct auto_pin_cfg *cfg = &spec->autocfg;
1656 struct auto_pin_cfg *best_cfg;
Takashi Iwai9314a582013-01-21 10:49:05 +01001657 unsigned int val;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001658 int best_badness = INT_MAX;
1659 int badness;
1660 bool fill_hardwired = true, fill_mio_first = true;
1661 bool best_wired = true, best_mio = true;
1662 bool hp_spk_swapped = false;
1663
Takashi Iwai352f7f92012-12-19 12:52:06 +01001664 best_cfg = kmalloc(sizeof(*best_cfg), GFP_KERNEL);
1665 if (!best_cfg)
1666 return -ENOMEM;
1667 *best_cfg = *cfg;
1668
1669 for (;;) {
1670 badness = fill_and_eval_dacs(codec, fill_hardwired,
1671 fill_mio_first);
1672 if (badness < 0) {
1673 kfree(best_cfg);
1674 return badness;
1675 }
1676 debug_badness("==> lo_type=%d, wired=%d, mio=%d, badness=0x%x\n",
1677 cfg->line_out_type, fill_hardwired, fill_mio_first,
1678 badness);
Takashi Iwaia7694092013-01-21 10:43:18 +01001679 debug_show_configs(codec, cfg);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001680 if (badness < best_badness) {
1681 best_badness = badness;
1682 *best_cfg = *cfg;
1683 best_wired = fill_hardwired;
1684 best_mio = fill_mio_first;
1685 }
1686 if (!badness)
1687 break;
1688 fill_mio_first = !fill_mio_first;
1689 if (!fill_mio_first)
1690 continue;
1691 fill_hardwired = !fill_hardwired;
1692 if (!fill_hardwired)
1693 continue;
1694 if (hp_spk_swapped)
1695 break;
1696 hp_spk_swapped = true;
1697 if (cfg->speaker_outs > 0 &&
1698 cfg->line_out_type == AUTO_PIN_HP_OUT) {
1699 cfg->hp_outs = cfg->line_outs;
1700 memcpy(cfg->hp_pins, cfg->line_out_pins,
1701 sizeof(cfg->hp_pins));
1702 cfg->line_outs = cfg->speaker_outs;
1703 memcpy(cfg->line_out_pins, cfg->speaker_pins,
1704 sizeof(cfg->speaker_pins));
1705 cfg->speaker_outs = 0;
1706 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
1707 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
1708 fill_hardwired = true;
1709 continue;
1710 }
1711 if (cfg->hp_outs > 0 &&
1712 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
1713 cfg->speaker_outs = cfg->line_outs;
1714 memcpy(cfg->speaker_pins, cfg->line_out_pins,
1715 sizeof(cfg->speaker_pins));
1716 cfg->line_outs = cfg->hp_outs;
1717 memcpy(cfg->line_out_pins, cfg->hp_pins,
1718 sizeof(cfg->hp_pins));
1719 cfg->hp_outs = 0;
1720 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
1721 cfg->line_out_type = AUTO_PIN_HP_OUT;
1722 fill_hardwired = true;
1723 continue;
1724 }
1725 break;
1726 }
1727
1728 if (badness) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001729 debug_badness("==> restoring best_cfg\n");
Takashi Iwai352f7f92012-12-19 12:52:06 +01001730 *cfg = *best_cfg;
1731 fill_and_eval_dacs(codec, best_wired, best_mio);
1732 }
1733 debug_badness("==> Best config: lo_type=%d, wired=%d, mio=%d\n",
1734 cfg->line_out_type, best_wired, best_mio);
Takashi Iwaia7694092013-01-21 10:43:18 +01001735 debug_show_configs(codec, cfg);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001736
1737 if (cfg->line_out_pins[0]) {
1738 struct nid_path *path;
Takashi Iwai196c17662013-01-04 15:01:40 +01001739 path = snd_hda_get_path_from_idx(codec, spec->out_paths[0]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001740 if (path)
1741 spec->vmaster_nid = look_for_out_vol_nid(codec, path);
Takashi Iwai7a71bbf2013-01-17 10:25:15 +01001742 if (spec->vmaster_nid)
1743 snd_hda_set_vmaster_tlv(codec, spec->vmaster_nid,
1744 HDA_OUTPUT, spec->vmaster_tlv);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001745 }
1746
Takashi Iwai9314a582013-01-21 10:49:05 +01001747 /* set initial pinctl targets */
1748 if (spec->prefer_hp_amp || cfg->line_out_type == AUTO_PIN_HP_OUT)
1749 val = PIN_HP;
1750 else
1751 val = PIN_OUT;
1752 set_pin_targets(codec, cfg->line_outs, cfg->line_out_pins, val);
1753 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1754 set_pin_targets(codec, cfg->hp_outs, cfg->hp_pins, PIN_HP);
1755 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1756 val = spec->prefer_hp_amp ? PIN_HP : PIN_OUT;
1757 set_pin_targets(codec, cfg->speaker_outs,
1758 cfg->speaker_pins, val);
1759 }
1760
Takashi Iwai352f7f92012-12-19 12:52:06 +01001761 kfree(best_cfg);
1762 return 0;
1763}
1764
1765/* add playback controls from the parsed DAC table */
1766static int create_multi_out_ctls(struct hda_codec *codec,
1767 const struct auto_pin_cfg *cfg)
1768{
1769 struct hda_gen_spec *spec = codec->spec;
1770 int i, err, noutputs;
1771
1772 noutputs = cfg->line_outs;
1773 if (spec->multi_ios > 0 && cfg->line_outs < 3)
1774 noutputs += spec->multi_ios;
1775
1776 for (i = 0; i < noutputs; i++) {
1777 const char *name;
1778 int index;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001779 struct nid_path *path;
1780
Takashi Iwai196c17662013-01-04 15:01:40 +01001781 path = snd_hda_get_path_from_idx(codec, spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001782 if (!path)
1783 continue;
Takashi Iwai247d85e2013-01-17 16:18:11 +01001784
1785 name = get_line_out_pfx(codec, i, &index, NID_PATH_VOL_CTL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001786 if (!name || !strcmp(name, "CLFE")) {
1787 /* Center/LFE */
1788 err = add_vol_ctl(codec, "Center", 0, 1, path);
1789 if (err < 0)
1790 return err;
1791 err = add_vol_ctl(codec, "LFE", 0, 2, path);
1792 if (err < 0)
1793 return err;
Takashi Iwai247d85e2013-01-17 16:18:11 +01001794 } else {
1795 err = add_stereo_vol(codec, name, index, path);
1796 if (err < 0)
1797 return err;
1798 }
1799
1800 name = get_line_out_pfx(codec, i, &index, NID_PATH_MUTE_CTL);
1801 if (!name || !strcmp(name, "CLFE")) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001802 err = add_sw_ctl(codec, "Center", 0, 1, path);
1803 if (err < 0)
1804 return err;
1805 err = add_sw_ctl(codec, "LFE", 0, 2, path);
1806 if (err < 0)
1807 return err;
1808 } else {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001809 err = add_stereo_sw(codec, name, index, path);
1810 if (err < 0)
1811 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 }
1813 }
1814 return 0;
1815}
1816
Takashi Iwaic2c80382013-01-07 10:33:57 +01001817static int create_extra_out(struct hda_codec *codec, int path_idx,
Takashi Iwai196c17662013-01-04 15:01:40 +01001818 const char *pfx, int cidx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819{
Takashi Iwai352f7f92012-12-19 12:52:06 +01001820 struct nid_path *path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 int err;
1822
Takashi Iwai196c17662013-01-04 15:01:40 +01001823 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001824 if (!path)
1825 return 0;
Takashi Iwaic2c80382013-01-07 10:33:57 +01001826 err = add_stereo_vol(codec, pfx, cidx, path);
1827 if (err < 0)
1828 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001829 err = add_stereo_sw(codec, pfx, cidx, path);
1830 if (err < 0)
1831 return err;
1832 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833}
1834
Takashi Iwai352f7f92012-12-19 12:52:06 +01001835/* add playback controls for speaker and HP outputs */
1836static int create_extra_outs(struct hda_codec *codec, int num_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001837 const int *paths, const char *pfx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838{
Takashi Iwaic2c80382013-01-07 10:33:57 +01001839 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001840
1841 for (i = 0; i < num_pins; i++) {
Takashi Iwaic2c80382013-01-07 10:33:57 +01001842 const char *name;
1843 char tmp[44];
1844 int err, idx = 0;
1845
1846 if (num_pins == 2 && i == 1 && !strcmp(pfx, "Speaker"))
1847 name = "Bass Speaker";
1848 else if (num_pins >= 3) {
1849 snprintf(tmp, sizeof(tmp), "%s %s",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001850 pfx, channel_name[i]);
Takashi Iwaic2c80382013-01-07 10:33:57 +01001851 name = tmp;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001852 } else {
Takashi Iwaic2c80382013-01-07 10:33:57 +01001853 name = pfx;
1854 idx = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 }
Takashi Iwaic2c80382013-01-07 10:33:57 +01001856 err = create_extra_out(codec, paths[i], name, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001857 if (err < 0)
1858 return err;
1859 }
1860 return 0;
1861}
Takashi Iwai97ec5582006-03-21 11:29:07 +01001862
Takashi Iwai352f7f92012-12-19 12:52:06 +01001863static int create_hp_out_ctls(struct hda_codec *codec)
1864{
1865 struct hda_gen_spec *spec = codec->spec;
1866 return create_extra_outs(codec, spec->autocfg.hp_outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001867 spec->hp_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001868 "Headphone");
1869}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870
Takashi Iwai352f7f92012-12-19 12:52:06 +01001871static int create_speaker_out_ctls(struct hda_codec *codec)
1872{
1873 struct hda_gen_spec *spec = codec->spec;
1874 return create_extra_outs(codec, spec->autocfg.speaker_outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001875 spec->speaker_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001876 "Speaker");
1877}
1878
1879/*
Takashi Iwai38cf6f12012-12-21 14:09:42 +01001880 * independent HP controls
1881 */
1882
1883static int indep_hp_info(struct snd_kcontrol *kcontrol,
1884 struct snd_ctl_elem_info *uinfo)
1885{
1886 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
1887}
1888
1889static int indep_hp_get(struct snd_kcontrol *kcontrol,
1890 struct snd_ctl_elem_value *ucontrol)
1891{
1892 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1893 struct hda_gen_spec *spec = codec->spec;
1894 ucontrol->value.enumerated.item[0] = spec->indep_hp_enabled;
1895 return 0;
1896}
1897
Takashi Iwaia1e908e2013-01-21 15:11:25 +01001898static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
1899 int nomix_path_idx, int mix_path_idx,
1900 int out_type);
1901
Takashi Iwai38cf6f12012-12-21 14:09:42 +01001902static int indep_hp_put(struct snd_kcontrol *kcontrol,
1903 struct snd_ctl_elem_value *ucontrol)
1904{
1905 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1906 struct hda_gen_spec *spec = codec->spec;
1907 unsigned int select = ucontrol->value.enumerated.item[0];
1908 int ret = 0;
1909
1910 mutex_lock(&spec->pcm_mutex);
1911 if (spec->active_streams) {
1912 ret = -EBUSY;
1913 goto unlock;
1914 }
1915
1916 if (spec->indep_hp_enabled != select) {
Takashi Iwaia1e908e2013-01-21 15:11:25 +01001917 hda_nid_t *dacp;
1918 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
1919 dacp = &spec->private_dac_nids[0];
1920 else
1921 dacp = &spec->multiout.hp_out_nid[0];
1922
1923 /* update HP aamix paths in case it conflicts with indep HP */
1924 if (spec->have_aamix_ctl) {
1925 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
1926 update_aamix_paths(codec, spec->aamix_mode,
1927 spec->out_paths[0],
1928 spec->aamix_out_paths[0],
1929 spec->autocfg.line_out_type);
1930 else
1931 update_aamix_paths(codec, spec->aamix_mode,
1932 spec->hp_paths[0],
1933 spec->aamix_out_paths[1],
1934 AUTO_PIN_HP_OUT);
1935 }
1936
Takashi Iwai38cf6f12012-12-21 14:09:42 +01001937 spec->indep_hp_enabled = select;
1938 if (spec->indep_hp_enabled)
Takashi Iwaia1e908e2013-01-21 15:11:25 +01001939 *dacp = 0;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01001940 else
Takashi Iwaia1e908e2013-01-21 15:11:25 +01001941 *dacp = spec->alt_dac_nid;
Takashi Iwai92603c52013-01-22 07:46:31 +01001942
1943 /* update HP auto-mute state too */
1944 if (spec->hp_automute_hook)
1945 spec->hp_automute_hook(codec, NULL);
1946 else
1947 snd_hda_gen_hp_automute(codec, NULL);
1948
Takashi Iwai38cf6f12012-12-21 14:09:42 +01001949 ret = 1;
1950 }
1951 unlock:
1952 mutex_unlock(&spec->pcm_mutex);
1953 return ret;
1954}
1955
1956static const struct snd_kcontrol_new indep_hp_ctl = {
1957 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1958 .name = "Independent HP",
1959 .info = indep_hp_info,
1960 .get = indep_hp_get,
1961 .put = indep_hp_put,
1962};
1963
1964
1965static int create_indep_hp_ctls(struct hda_codec *codec)
1966{
1967 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01001968 hda_nid_t dac;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01001969
1970 if (!spec->indep_hp)
1971 return 0;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01001972 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
1973 dac = spec->multiout.dac_nids[0];
1974 else
1975 dac = spec->multiout.hp_out_nid[0];
1976 if (!dac) {
Takashi Iwai38cf6f12012-12-21 14:09:42 +01001977 spec->indep_hp = 0;
1978 return 0;
1979 }
1980
1981 spec->indep_hp_enabled = false;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01001982 spec->alt_dac_nid = dac;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01001983 if (!snd_hda_gen_add_kctl(spec, NULL, &indep_hp_ctl))
1984 return -ENOMEM;
1985 return 0;
1986}
1987
1988/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01001989 * channel mode enum control
1990 */
1991
1992static int ch_mode_info(struct snd_kcontrol *kcontrol,
1993 struct snd_ctl_elem_info *uinfo)
1994{
1995 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1996 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia07a9492013-01-07 16:44:06 +01001997 int chs;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001998
1999 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2000 uinfo->count = 1;
2001 uinfo->value.enumerated.items = spec->multi_ios + 1;
2002 if (uinfo->value.enumerated.item > spec->multi_ios)
2003 uinfo->value.enumerated.item = spec->multi_ios;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002004 chs = uinfo->value.enumerated.item * 2 + spec->min_channel_count;
2005 sprintf(uinfo->value.enumerated.name, "%dch", chs);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002006 return 0;
2007}
2008
2009static int ch_mode_get(struct snd_kcontrol *kcontrol,
2010 struct snd_ctl_elem_value *ucontrol)
2011{
2012 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2013 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002014 ucontrol->value.enumerated.item[0] =
2015 (spec->ext_channel_count - spec->min_channel_count) / 2;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002016 return 0;
2017}
2018
Takashi Iwai196c17662013-01-04 15:01:40 +01002019static inline struct nid_path *
2020get_multiio_path(struct hda_codec *codec, int idx)
2021{
2022 struct hda_gen_spec *spec = codec->spec;
2023 return snd_hda_get_path_from_idx(codec,
2024 spec->out_paths[spec->autocfg.line_outs + idx]);
2025}
2026
Takashi Iwaia5cc2502013-01-16 18:08:55 +01002027static void update_automute_all(struct hda_codec *codec);
2028
Takashi Iwai352f7f92012-12-19 12:52:06 +01002029static int set_multi_io(struct hda_codec *codec, int idx, bool output)
2030{
2031 struct hda_gen_spec *spec = codec->spec;
2032 hda_nid_t nid = spec->multi_io[idx].pin;
2033 struct nid_path *path;
2034
Takashi Iwai196c17662013-01-04 15:01:40 +01002035 path = get_multiio_path(codec, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002036 if (!path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 return -EINVAL;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002038
2039 if (path->active == output)
2040 return 0;
2041
2042 if (output) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01002043 set_pin_target(codec, nid, PIN_OUT, true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002044 snd_hda_activate_path(codec, path, true, true);
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01002045 set_pin_eapd(codec, nid, true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002046 } else {
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01002047 set_pin_eapd(codec, nid, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002048 snd_hda_activate_path(codec, path, false, true);
Takashi Iwai2c12c302013-01-10 09:33:29 +01002049 set_pin_target(codec, nid, spec->multi_io[idx].ctl_in, true);
Takashi Iwai55196ff2013-01-24 17:32:56 +01002050 path_power_down_sync(codec, path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 }
Takashi Iwaia365fed2013-01-10 16:10:06 +01002052
2053 /* update jack retasking in case it modifies any of them */
Takashi Iwaia5cc2502013-01-16 18:08:55 +01002054 update_automute_all(codec);
Takashi Iwaia365fed2013-01-10 16:10:06 +01002055
Takashi Iwai352f7f92012-12-19 12:52:06 +01002056 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057}
2058
Takashi Iwai352f7f92012-12-19 12:52:06 +01002059static int ch_mode_put(struct snd_kcontrol *kcontrol,
2060 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002062 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2063 struct hda_gen_spec *spec = codec->spec;
2064 int i, ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065
Takashi Iwai352f7f92012-12-19 12:52:06 +01002066 ch = ucontrol->value.enumerated.item[0];
2067 if (ch < 0 || ch > spec->multi_ios)
2068 return -EINVAL;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002069 if (ch == (spec->ext_channel_count - spec->min_channel_count) / 2)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002070 return 0;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002071 spec->ext_channel_count = ch * 2 + spec->min_channel_count;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002072 for (i = 0; i < spec->multi_ios; i++)
2073 set_multi_io(codec, i, i < ch);
2074 spec->multiout.max_channels = max(spec->ext_channel_count,
2075 spec->const_channel_count);
2076 if (spec->need_dac_fix)
2077 spec->multiout.num_dacs = spec->multiout.max_channels / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 return 1;
2079}
2080
Takashi Iwai352f7f92012-12-19 12:52:06 +01002081static const struct snd_kcontrol_new channel_mode_enum = {
2082 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2083 .name = "Channel Mode",
2084 .info = ch_mode_info,
2085 .get = ch_mode_get,
2086 .put = ch_mode_put,
2087};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088
Takashi Iwai352f7f92012-12-19 12:52:06 +01002089static int create_multi_channel_mode(struct hda_codec *codec)
2090{
2091 struct hda_gen_spec *spec = codec->spec;
2092
2093 if (spec->multi_ios > 0) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01002094 if (!snd_hda_gen_add_kctl(spec, NULL, &channel_mode_enum))
Takashi Iwai352f7f92012-12-19 12:52:06 +01002095 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 return 0;
2098}
2099
Takashi Iwai352f7f92012-12-19 12:52:06 +01002100/*
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002101 * aamix loopback enable/disable switch
2102 */
2103
2104#define loopback_mixing_info indep_hp_info
2105
2106static int loopback_mixing_get(struct snd_kcontrol *kcontrol,
2107 struct snd_ctl_elem_value *ucontrol)
2108{
2109 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2110 struct hda_gen_spec *spec = codec->spec;
2111 ucontrol->value.enumerated.item[0] = spec->aamix_mode;
2112 return 0;
2113}
2114
2115static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002116 int nomix_path_idx, int mix_path_idx,
2117 int out_type)
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002118{
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002119 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002120 struct nid_path *nomix_path, *mix_path;
2121
2122 nomix_path = snd_hda_get_path_from_idx(codec, nomix_path_idx);
2123 mix_path = snd_hda_get_path_from_idx(codec, mix_path_idx);
2124 if (!nomix_path || !mix_path)
2125 return;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002126
2127 /* if HP aamix path is driven from a different DAC and the
2128 * independent HP mode is ON, can't turn on aamix path
2129 */
2130 if (out_type == AUTO_PIN_HP_OUT && spec->indep_hp_enabled &&
2131 mix_path->path[0] != spec->alt_dac_nid)
2132 do_mix = false;
2133
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002134 if (do_mix) {
2135 snd_hda_activate_path(codec, nomix_path, false, true);
2136 snd_hda_activate_path(codec, mix_path, true, true);
Takashi Iwai55196ff2013-01-24 17:32:56 +01002137 path_power_down_sync(codec, nomix_path);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002138 } else {
2139 snd_hda_activate_path(codec, mix_path, false, true);
2140 snd_hda_activate_path(codec, nomix_path, true, true);
Takashi Iwai55196ff2013-01-24 17:32:56 +01002141 path_power_down_sync(codec, mix_path);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002142 }
2143}
2144
2145static int loopback_mixing_put(struct snd_kcontrol *kcontrol,
2146 struct snd_ctl_elem_value *ucontrol)
2147{
2148 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2149 struct hda_gen_spec *spec = codec->spec;
2150 unsigned int val = ucontrol->value.enumerated.item[0];
2151
2152 if (val == spec->aamix_mode)
2153 return 0;
2154 spec->aamix_mode = val;
2155 update_aamix_paths(codec, val, spec->out_paths[0],
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002156 spec->aamix_out_paths[0],
2157 spec->autocfg.line_out_type);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002158 update_aamix_paths(codec, val, spec->hp_paths[0],
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002159 spec->aamix_out_paths[1],
2160 AUTO_PIN_HP_OUT);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002161 update_aamix_paths(codec, val, spec->speaker_paths[0],
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002162 spec->aamix_out_paths[2],
2163 AUTO_PIN_SPEAKER_OUT);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002164 return 1;
2165}
2166
2167static const struct snd_kcontrol_new loopback_mixing_enum = {
2168 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2169 .name = "Loopback Mixing",
2170 .info = loopback_mixing_info,
2171 .get = loopback_mixing_get,
2172 .put = loopback_mixing_put,
2173};
2174
2175static int create_loopback_mixing_ctl(struct hda_codec *codec)
2176{
2177 struct hda_gen_spec *spec = codec->spec;
2178
2179 if (!spec->mixer_nid)
2180 return 0;
2181 if (!(spec->aamix_out_paths[0] || spec->aamix_out_paths[1] ||
2182 spec->aamix_out_paths[2]))
2183 return 0;
2184 if (!snd_hda_gen_add_kctl(spec, NULL, &loopback_mixing_enum))
2185 return -ENOMEM;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002186 spec->have_aamix_ctl = 1;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002187 return 0;
2188}
2189
2190/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01002191 * shared headphone/mic handling
2192 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02002193
Takashi Iwai352f7f92012-12-19 12:52:06 +01002194static void call_update_outputs(struct hda_codec *codec);
2195
2196/* for shared I/O, change the pin-control accordingly */
2197static void update_shared_mic_hp(struct hda_codec *codec, bool set_as_mic)
2198{
2199 struct hda_gen_spec *spec = codec->spec;
2200 unsigned int val;
2201 hda_nid_t pin = spec->autocfg.inputs[1].pin;
2202 /* NOTE: this assumes that there are only two inputs, the
2203 * first is the real internal mic and the second is HP/mic jack.
2204 */
2205
2206 val = snd_hda_get_default_vref(codec, pin);
2207
2208 /* This pin does not have vref caps - let's enable vref on pin 0x18
2209 instead, as suggested by Realtek */
2210 if (val == AC_PINCTL_VREF_HIZ && spec->shared_mic_vref_pin) {
2211 const hda_nid_t vref_pin = spec->shared_mic_vref_pin;
2212 unsigned int vref_val = snd_hda_get_default_vref(codec, vref_pin);
2213 if (vref_val != AC_PINCTL_VREF_HIZ)
Takashi Iwai7594aa32012-12-20 15:38:40 +01002214 snd_hda_set_pin_ctl_cache(codec, vref_pin,
2215 PIN_IN | (set_as_mic ? vref_val : 0));
Takashi Iwaicb53c622007-08-10 17:21:45 +02002216 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002217
2218 val = set_as_mic ? val | PIN_IN : PIN_HP;
Takashi Iwai2c12c302013-01-10 09:33:29 +01002219 set_pin_target(codec, pin, val, true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002220
2221 spec->automute_speaker = !set_as_mic;
2222 call_update_outputs(codec);
2223}
2224
2225/* create a shared input with the headphone out */
2226static int create_shared_input(struct hda_codec *codec)
2227{
2228 struct hda_gen_spec *spec = codec->spec;
2229 struct auto_pin_cfg *cfg = &spec->autocfg;
2230 unsigned int defcfg;
2231 hda_nid_t nid;
2232
2233 /* only one internal input pin? */
2234 if (cfg->num_inputs != 1)
2235 return 0;
2236 defcfg = snd_hda_codec_get_pincfg(codec, cfg->inputs[0].pin);
2237 if (snd_hda_get_input_pin_attr(defcfg) != INPUT_PIN_ATTR_INT)
2238 return 0;
2239
2240 if (cfg->hp_outs == 1 && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
2241 nid = cfg->hp_pins[0]; /* OK, we have a single HP-out */
2242 else if (cfg->line_outs == 1 && cfg->line_out_type == AUTO_PIN_HP_OUT)
2243 nid = cfg->line_out_pins[0]; /* OK, we have a single line-out */
2244 else
2245 return 0; /* both not available */
2246
2247 if (!(snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_IN))
2248 return 0; /* no input */
2249
2250 cfg->inputs[1].pin = nid;
2251 cfg->inputs[1].type = AUTO_PIN_MIC;
2252 cfg->num_inputs = 2;
2253 spec->shared_mic_hp = 1;
2254 snd_printdd("hda-codec: Enable shared I/O jack on NID 0x%x\n", nid);
2255 return 0;
2256}
2257
Takashi Iwai978e77e2013-01-10 16:57:58 +01002258/*
2259 * output jack mode
2260 */
2261static int out_jack_mode_info(struct snd_kcontrol *kcontrol,
2262 struct snd_ctl_elem_info *uinfo)
2263{
2264 static const char * const texts[] = {
2265 "Line Out", "Headphone Out",
2266 };
2267 return snd_hda_enum_helper_info(kcontrol, uinfo, 2, texts);
2268}
2269
2270static int out_jack_mode_get(struct snd_kcontrol *kcontrol,
2271 struct snd_ctl_elem_value *ucontrol)
2272{
2273 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2274 hda_nid_t nid = kcontrol->private_value;
2275 if (snd_hda_codec_get_pin_target(codec, nid) == PIN_HP)
2276 ucontrol->value.enumerated.item[0] = 1;
2277 else
2278 ucontrol->value.enumerated.item[0] = 0;
2279 return 0;
2280}
2281
2282static int out_jack_mode_put(struct snd_kcontrol *kcontrol,
2283 struct snd_ctl_elem_value *ucontrol)
2284{
2285 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2286 hda_nid_t nid = kcontrol->private_value;
2287 unsigned int val;
2288
2289 val = ucontrol->value.enumerated.item[0] ? PIN_HP : PIN_OUT;
2290 if (snd_hda_codec_get_pin_target(codec, nid) == val)
2291 return 0;
2292 snd_hda_set_pin_ctl_cache(codec, nid, val);
2293 return 1;
2294}
2295
2296static const struct snd_kcontrol_new out_jack_mode_enum = {
2297 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2298 .info = out_jack_mode_info,
2299 .get = out_jack_mode_get,
2300 .put = out_jack_mode_put,
2301};
2302
2303static bool find_kctl_name(struct hda_codec *codec, const char *name, int idx)
2304{
2305 struct hda_gen_spec *spec = codec->spec;
2306 int i;
2307
2308 for (i = 0; i < spec->kctls.used; i++) {
2309 struct snd_kcontrol_new *kctl = snd_array_elem(&spec->kctls, i);
2310 if (!strcmp(kctl->name, name) && kctl->index == idx)
2311 return true;
2312 }
2313 return false;
2314}
2315
2316static void get_jack_mode_name(struct hda_codec *codec, hda_nid_t pin,
2317 char *name, size_t name_len)
2318{
2319 struct hda_gen_spec *spec = codec->spec;
2320 int idx = 0;
2321
2322 snd_hda_get_pin_label(codec, pin, &spec->autocfg, name, name_len, &idx);
2323 strlcat(name, " Jack Mode", name_len);
2324
2325 for (; find_kctl_name(codec, name, idx); idx++)
2326 ;
2327}
2328
2329static int create_out_jack_modes(struct hda_codec *codec, int num_pins,
2330 hda_nid_t *pins)
2331{
2332 struct hda_gen_spec *spec = codec->spec;
2333 int i;
2334
2335 for (i = 0; i < num_pins; i++) {
2336 hda_nid_t pin = pins[i];
2337 unsigned int pincap = snd_hda_query_pin_caps(codec, pin);
2338 if ((pincap & AC_PINCAP_OUT) && (pincap & AC_PINCAP_HP_DRV)) {
2339 struct snd_kcontrol_new *knew;
2340 char name[44];
2341 get_jack_mode_name(codec, pin, name, sizeof(name));
2342 knew = snd_hda_gen_add_kctl(spec, name,
2343 &out_jack_mode_enum);
2344 if (!knew)
2345 return -ENOMEM;
2346 knew->private_value = pin;
2347 }
2348 }
2349
2350 return 0;
2351}
2352
Takashi Iwai294765582013-01-17 09:52:11 +01002353/*
2354 * input jack mode
2355 */
2356
2357/* from AC_PINCTL_VREF_HIZ to AC_PINCTL_VREF_100 */
2358#define NUM_VREFS 6
2359
2360static const char * const vref_texts[NUM_VREFS] = {
2361 "Line In", "Mic 50pc Bias", "Mic 0V Bias",
2362 "", "Mic 80pc Bias", "Mic 100pc Bias"
2363};
2364
2365static unsigned int get_vref_caps(struct hda_codec *codec, hda_nid_t pin)
2366{
2367 unsigned int pincap;
2368
2369 pincap = snd_hda_query_pin_caps(codec, pin);
2370 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
2371 /* filter out unusual vrefs */
2372 pincap &= ~(AC_PINCAP_VREF_GRD | AC_PINCAP_VREF_100);
2373 return pincap;
2374}
2375
2376/* convert from the enum item index to the vref ctl index (0=HIZ, 1=50%...) */
2377static int get_vref_idx(unsigned int vref_caps, unsigned int item_idx)
2378{
2379 unsigned int i, n = 0;
2380
2381 for (i = 0; i < NUM_VREFS; i++) {
2382 if (vref_caps & (1 << i)) {
2383 if (n == item_idx)
2384 return i;
2385 n++;
2386 }
2387 }
2388 return 0;
2389}
2390
2391/* convert back from the vref ctl index to the enum item index */
2392static int cvt_from_vref_idx(unsigned int vref_caps, unsigned int idx)
2393{
2394 unsigned int i, n = 0;
2395
2396 for (i = 0; i < NUM_VREFS; i++) {
2397 if (i == idx)
2398 return n;
2399 if (vref_caps & (1 << i))
2400 n++;
2401 }
2402 return 0;
2403}
2404
2405static int in_jack_mode_info(struct snd_kcontrol *kcontrol,
2406 struct snd_ctl_elem_info *uinfo)
2407{
2408 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2409 hda_nid_t nid = kcontrol->private_value;
2410 unsigned int vref_caps = get_vref_caps(codec, nid);
2411
2412 snd_hda_enum_helper_info(kcontrol, uinfo, hweight32(vref_caps),
2413 vref_texts);
2414 /* set the right text */
2415 strcpy(uinfo->value.enumerated.name,
2416 vref_texts[get_vref_idx(vref_caps, uinfo->value.enumerated.item)]);
2417 return 0;
2418}
2419
2420static int in_jack_mode_get(struct snd_kcontrol *kcontrol,
2421 struct snd_ctl_elem_value *ucontrol)
2422{
2423 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2424 hda_nid_t nid = kcontrol->private_value;
2425 unsigned int vref_caps = get_vref_caps(codec, nid);
2426 unsigned int idx;
2427
2428 idx = snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_VREFEN;
2429 ucontrol->value.enumerated.item[0] = cvt_from_vref_idx(vref_caps, idx);
2430 return 0;
2431}
2432
2433static int in_jack_mode_put(struct snd_kcontrol *kcontrol,
2434 struct snd_ctl_elem_value *ucontrol)
2435{
2436 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2437 hda_nid_t nid = kcontrol->private_value;
2438 unsigned int vref_caps = get_vref_caps(codec, nid);
2439 unsigned int val, idx;
2440
2441 val = snd_hda_codec_get_pin_target(codec, nid);
2442 idx = cvt_from_vref_idx(vref_caps, val & AC_PINCTL_VREFEN);
2443 if (idx == ucontrol->value.enumerated.item[0])
2444 return 0;
2445
2446 val &= ~AC_PINCTL_VREFEN;
2447 val |= get_vref_idx(vref_caps, ucontrol->value.enumerated.item[0]);
2448 snd_hda_set_pin_ctl_cache(codec, nid, val);
2449 return 1;
2450}
2451
2452static const struct snd_kcontrol_new in_jack_mode_enum = {
2453 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2454 .info = in_jack_mode_info,
2455 .get = in_jack_mode_get,
2456 .put = in_jack_mode_put,
2457};
2458
2459static int create_in_jack_mode(struct hda_codec *codec, hda_nid_t pin)
2460{
2461 struct hda_gen_spec *spec = codec->spec;
2462 unsigned int defcfg;
2463 struct snd_kcontrol_new *knew;
2464 char name[44];
2465
2466 /* no jack mode for fixed pins */
2467 defcfg = snd_hda_codec_get_pincfg(codec, pin);
2468 if (snd_hda_get_input_pin_attr(defcfg) == INPUT_PIN_ATTR_INT)
2469 return 0;
2470
2471 /* no multiple vref caps? */
2472 if (hweight32(get_vref_caps(codec, pin)) <= 1)
2473 return 0;
2474
2475 get_jack_mode_name(codec, pin, name, sizeof(name));
2476 knew = snd_hda_gen_add_kctl(spec, name, &in_jack_mode_enum);
2477 if (!knew)
2478 return -ENOMEM;
2479 knew->private_value = pin;
2480 return 0;
2481}
2482
Takashi Iwai352f7f92012-12-19 12:52:06 +01002483
2484/*
2485 * Parse input paths
2486 */
2487
Takashi Iwai352f7f92012-12-19 12:52:06 +01002488/* add the powersave loopback-list entry */
Takashi Iwai0186f4f2013-02-07 10:45:11 +01002489static int add_loopback_list(struct hda_gen_spec *spec, hda_nid_t mix, int idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002490{
2491 struct hda_amp_list *list;
2492
Takashi Iwai0186f4f2013-02-07 10:45:11 +01002493 list = snd_array_new(&spec->loopback_list);
2494 if (!list)
2495 return -ENOMEM;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002496 list->nid = mix;
2497 list->dir = HDA_INPUT;
2498 list->idx = idx;
Takashi Iwai0186f4f2013-02-07 10:45:11 +01002499 spec->loopback.amplist = spec->loopback_list.list;
2500 return 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02002501}
Takashi Iwaicb53c622007-08-10 17:21:45 +02002502
Takashi Iwai352f7f92012-12-19 12:52:06 +01002503/* create input playback/capture controls for the given pin */
Takashi Iwai196c17662013-01-04 15:01:40 +01002504static int new_analog_input(struct hda_codec *codec, int input_idx,
2505 hda_nid_t pin, const char *ctlname, int ctlidx,
Takashi Iwai352f7f92012-12-19 12:52:06 +01002506 hda_nid_t mix_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002508 struct hda_gen_spec *spec = codec->spec;
2509 struct nid_path *path;
2510 unsigned int val;
2511 int err, idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512
Takashi Iwai352f7f92012-12-19 12:52:06 +01002513 if (!nid_has_volume(codec, mix_nid, HDA_INPUT) &&
2514 !nid_has_mute(codec, mix_nid, HDA_INPUT))
2515 return 0; /* no need for analog loopback */
2516
Takashi Iwai3ca529d2013-01-07 17:25:08 +01002517 path = snd_hda_add_new_path(codec, pin, mix_nid, 0);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002518 if (!path)
2519 return -EINVAL;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01002520 print_nid_path("loopback", path);
Takashi Iwai196c17662013-01-04 15:01:40 +01002521 spec->loopback_paths[input_idx] = snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002522
2523 idx = path->idx[path->depth - 1];
2524 if (nid_has_volume(codec, mix_nid, HDA_INPUT)) {
2525 val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
2526 err = __add_pb_vol_ctrl(spec, HDA_CTL_WIDGET_VOL, ctlname, ctlidx, val);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002527 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002529 path->ctls[NID_PATH_VOL_CTL] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 }
2531
Takashi Iwai352f7f92012-12-19 12:52:06 +01002532 if (nid_has_mute(codec, mix_nid, HDA_INPUT)) {
2533 val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
2534 err = __add_pb_sw_ctrl(spec, HDA_CTL_WIDGET_MUTE, ctlname, ctlidx, val);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002535 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002537 path->ctls[NID_PATH_MUTE_CTL] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538 }
2539
Takashi Iwai352f7f92012-12-19 12:52:06 +01002540 path->active = true;
Takashi Iwai0186f4f2013-02-07 10:45:11 +01002541 err = add_loopback_list(spec, mix_nid, idx);
2542 if (err < 0)
2543 return err;
Takashi Iwaie4a395e2013-01-23 17:00:31 +01002544
2545 if (spec->mixer_nid != spec->mixer_merge_nid &&
2546 !spec->loopback_merge_path) {
2547 path = snd_hda_add_new_path(codec, spec->mixer_nid,
2548 spec->mixer_merge_nid, 0);
2549 if (path) {
2550 print_nid_path("loopback-merge", path);
2551 path->active = true;
2552 spec->loopback_merge_path =
2553 snd_hda_get_path_idx(codec, path);
2554 }
2555 }
2556
Takashi Iwai352f7f92012-12-19 12:52:06 +01002557 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558}
2559
Takashi Iwai352f7f92012-12-19 12:52:06 +01002560static int is_input_pin(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002562 unsigned int pincap = snd_hda_query_pin_caps(codec, nid);
2563 return (pincap & AC_PINCAP_IN) != 0;
2564}
2565
2566/* Parse the codec tree and retrieve ADCs */
2567static int fill_adc_nids(struct hda_codec *codec)
2568{
2569 struct hda_gen_spec *spec = codec->spec;
2570 hda_nid_t nid;
2571 hda_nid_t *adc_nids = spec->adc_nids;
2572 int max_nums = ARRAY_SIZE(spec->adc_nids);
2573 int i, nums = 0;
2574
2575 nid = codec->start_nid;
2576 for (i = 0; i < codec->num_nodes; i++, nid++) {
2577 unsigned int caps = get_wcaps(codec, nid);
2578 int type = get_wcaps_type(caps);
2579
2580 if (type != AC_WID_AUD_IN || (caps & AC_WCAP_DIGITAL))
2581 continue;
2582 adc_nids[nums] = nid;
2583 if (++nums >= max_nums)
2584 break;
2585 }
2586 spec->num_adc_nids = nums;
Takashi Iwai0ffd5342013-01-17 15:53:29 +01002587
2588 /* copy the detected ADCs to all_adcs[] */
2589 spec->num_all_adcs = nums;
2590 memcpy(spec->all_adcs, spec->adc_nids, nums * sizeof(hda_nid_t));
2591
Takashi Iwai352f7f92012-12-19 12:52:06 +01002592 return nums;
2593}
2594
2595/* filter out invalid adc_nids that don't give all active input pins;
2596 * if needed, check whether dynamic ADC-switching is available
2597 */
2598static int check_dyn_adc_switch(struct hda_codec *codec)
2599{
2600 struct hda_gen_spec *spec = codec->spec;
2601 struct hda_input_mux *imux = &spec->input_mux;
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002602 unsigned int ok_bits;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002603 int i, n, nums;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002604
2605 again:
2606 nums = 0;
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002607 ok_bits = 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002608 for (n = 0; n < spec->num_adc_nids; n++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002609 for (i = 0; i < imux->num_items; i++) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002610 if (!spec->input_paths[i][n])
Takashi Iwai352f7f92012-12-19 12:52:06 +01002611 break;
2612 }
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002613 if (i >= imux->num_items) {
2614 ok_bits |= (1 << n);
2615 nums++;
2616 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002617 }
2618
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002619 if (!ok_bits) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002620 if (spec->shared_mic_hp) {
2621 spec->shared_mic_hp = 0;
2622 imux->num_items = 1;
2623 goto again;
2624 }
2625
2626 /* check whether ADC-switch is possible */
2627 for (i = 0; i < imux->num_items; i++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002628 for (n = 0; n < spec->num_adc_nids; n++) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002629 if (spec->input_paths[i][n]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002630 spec->dyn_adc_idx[i] = n;
2631 break;
2632 }
2633 }
2634 }
2635
2636 snd_printdd("hda-codec: enabling ADC switching\n");
2637 spec->dyn_adc_switch = 1;
2638 } else if (nums != spec->num_adc_nids) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002639 /* shrink the invalid adcs and input paths */
2640 nums = 0;
2641 for (n = 0; n < spec->num_adc_nids; n++) {
2642 if (!(ok_bits & (1 << n)))
2643 continue;
2644 if (n != nums) {
2645 spec->adc_nids[nums] = spec->adc_nids[n];
Takashi Iwai980428c2013-01-09 09:28:20 +01002646 for (i = 0; i < imux->num_items; i++) {
2647 invalidate_nid_path(codec,
2648 spec->input_paths[i][nums]);
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002649 spec->input_paths[i][nums] =
2650 spec->input_paths[i][n];
Takashi Iwai980428c2013-01-09 09:28:20 +01002651 }
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002652 }
2653 nums++;
2654 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002655 spec->num_adc_nids = nums;
2656 }
2657
2658 if (imux->num_items == 1 || spec->shared_mic_hp) {
2659 snd_printdd("hda-codec: reducing to a single ADC\n");
2660 spec->num_adc_nids = 1; /* reduce to a single ADC */
2661 }
2662
2663 /* single index for individual volumes ctls */
2664 if (!spec->dyn_adc_switch && spec->multi_cap_vol)
2665 spec->num_adc_nids = 1;
2666
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667 return 0;
2668}
2669
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002670/* parse capture source paths from the given pin and create imux items */
2671static int parse_capture_source(struct hda_codec *codec, hda_nid_t pin,
Takashi Iwai9dba2052013-01-18 10:01:15 +01002672 int cfg_idx, int num_adcs,
2673 const char *label, int anchor)
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002674{
2675 struct hda_gen_spec *spec = codec->spec;
2676 struct hda_input_mux *imux = &spec->input_mux;
2677 int imux_idx = imux->num_items;
2678 bool imux_added = false;
2679 int c;
2680
2681 for (c = 0; c < num_adcs; c++) {
2682 struct nid_path *path;
2683 hda_nid_t adc = spec->adc_nids[c];
2684
2685 if (!is_reachable_path(codec, pin, adc))
2686 continue;
2687 path = snd_hda_add_new_path(codec, pin, adc, anchor);
2688 if (!path)
2689 continue;
2690 print_nid_path("input", path);
2691 spec->input_paths[imux_idx][c] =
2692 snd_hda_get_path_idx(codec, path);
2693
2694 if (!imux_added) {
2695 spec->imux_pins[imux->num_items] = pin;
Takashi Iwai9dba2052013-01-18 10:01:15 +01002696 snd_hda_add_imux_item(imux, label, cfg_idx, NULL);
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002697 imux_added = true;
2698 }
2699 }
2700
2701 return 0;
2702}
2703
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01002705 * create playback/capture controls for input pins
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706 */
Takashi Iwai9dba2052013-01-18 10:01:15 +01002707
Takashi Iwaic9700422013-01-18 10:17:30 +01002708/* fill the label for each input at first */
2709static int fill_input_pin_labels(struct hda_codec *codec)
2710{
2711 struct hda_gen_spec *spec = codec->spec;
2712 const struct auto_pin_cfg *cfg = &spec->autocfg;
2713 int i;
2714
2715 for (i = 0; i < cfg->num_inputs; i++) {
2716 hda_nid_t pin = cfg->inputs[i].pin;
2717 const char *label;
2718 int j, idx;
2719
2720 if (!is_input_pin(codec, pin))
2721 continue;
2722
2723 label = hda_get_autocfg_input_label(codec, cfg, i);
2724 idx = 0;
David Henningsson8e8db7f2013-01-18 15:43:02 +01002725 for (j = i - 1; j >= 0; j--) {
Takashi Iwaic9700422013-01-18 10:17:30 +01002726 if (spec->input_labels[j] &&
2727 !strcmp(spec->input_labels[j], label)) {
2728 idx = spec->input_label_idxs[j] + 1;
2729 break;
2730 }
2731 }
2732
2733 spec->input_labels[i] = label;
2734 spec->input_label_idxs[i] = idx;
2735 }
2736
2737 return 0;
2738}
2739
Takashi Iwai9dba2052013-01-18 10:01:15 +01002740#define CFG_IDX_MIX 99 /* a dummy cfg->input idx for stereo mix */
2741
Takashi Iwai352f7f92012-12-19 12:52:06 +01002742static int create_input_ctls(struct hda_codec *codec)
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02002743{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002744 struct hda_gen_spec *spec = codec->spec;
2745 const struct auto_pin_cfg *cfg = &spec->autocfg;
2746 hda_nid_t mixer = spec->mixer_nid;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002747 int num_adcs;
Takashi Iwaic9700422013-01-18 10:17:30 +01002748 int i, err;
Takashi Iwai2c12c302013-01-10 09:33:29 +01002749 unsigned int val;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02002750
Takashi Iwai352f7f92012-12-19 12:52:06 +01002751 num_adcs = fill_adc_nids(codec);
2752 if (num_adcs < 0)
2753 return 0;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02002754
Takashi Iwaic9700422013-01-18 10:17:30 +01002755 err = fill_input_pin_labels(codec);
2756 if (err < 0)
2757 return err;
2758
Takashi Iwai352f7f92012-12-19 12:52:06 +01002759 for (i = 0; i < cfg->num_inputs; i++) {
2760 hda_nid_t pin;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761
Takashi Iwai352f7f92012-12-19 12:52:06 +01002762 pin = cfg->inputs[i].pin;
2763 if (!is_input_pin(codec, pin))
2764 continue;
2765
Takashi Iwai2c12c302013-01-10 09:33:29 +01002766 val = PIN_IN;
2767 if (cfg->inputs[i].type == AUTO_PIN_MIC)
2768 val |= snd_hda_get_default_vref(codec, pin);
2769 set_pin_target(codec, pin, val, false);
2770
Takashi Iwai352f7f92012-12-19 12:52:06 +01002771 if (mixer) {
2772 if (is_reachable_path(codec, pin, mixer)) {
Takashi Iwai196c17662013-01-04 15:01:40 +01002773 err = new_analog_input(codec, i, pin,
Takashi Iwaic9700422013-01-18 10:17:30 +01002774 spec->input_labels[i],
2775 spec->input_label_idxs[i],
2776 mixer);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002777 if (err < 0)
2778 return err;
2779 }
2780 }
2781
Takashi Iwaic9700422013-01-18 10:17:30 +01002782 err = parse_capture_source(codec, pin, i, num_adcs,
2783 spec->input_labels[i], -mixer);
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002784 if (err < 0)
2785 return err;
Takashi Iwai294765582013-01-17 09:52:11 +01002786
2787 if (spec->add_in_jack_modes) {
2788 err = create_in_jack_mode(codec, pin);
2789 if (err < 0)
2790 return err;
2791 }
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002792 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002793
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002794 if (mixer && spec->add_stereo_mix_input) {
Takashi Iwai9dba2052013-01-18 10:01:15 +01002795 err = parse_capture_source(codec, mixer, CFG_IDX_MIX, num_adcs,
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002796 "Stereo Mix", 0);
2797 if (err < 0)
2798 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002799 }
2800
2801 return 0;
2802}
2803
2804
2805/*
2806 * input source mux
2807 */
2808
Takashi Iwaic697b712013-01-07 17:09:26 +01002809/* get the input path specified by the given adc and imux indices */
2810static struct nid_path *get_input_path(struct hda_codec *codec, int adc_idx, int imux_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002811{
2812 struct hda_gen_spec *spec = codec->spec;
David Henningssonb56fa1e2013-01-16 11:45:35 +01002813 if (imux_idx < 0 || imux_idx >= HDA_MAX_NUM_INPUTS) {
2814 snd_BUG();
2815 return NULL;
2816 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002817 if (spec->dyn_adc_switch)
2818 adc_idx = spec->dyn_adc_idx[imux_idx];
David Henningssond3d982f2013-01-18 15:43:01 +01002819 if (adc_idx < 0 || adc_idx >= AUTO_CFG_MAX_INS) {
David Henningssonb56fa1e2013-01-16 11:45:35 +01002820 snd_BUG();
2821 return NULL;
2822 }
Takashi Iwaic697b712013-01-07 17:09:26 +01002823 return snd_hda_get_path_from_idx(codec, spec->input_paths[imux_idx][adc_idx]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002824}
2825
2826static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
2827 unsigned int idx);
2828
2829static int mux_enum_info(struct snd_kcontrol *kcontrol,
2830 struct snd_ctl_elem_info *uinfo)
2831{
2832 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2833 struct hda_gen_spec *spec = codec->spec;
2834 return snd_hda_input_mux_info(&spec->input_mux, uinfo);
2835}
2836
2837static int mux_enum_get(struct snd_kcontrol *kcontrol,
2838 struct snd_ctl_elem_value *ucontrol)
2839{
2840 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2841 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai2a8d5392013-01-18 16:23:25 +01002842 /* the ctls are created at once with multiple counts */
2843 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002844
2845 ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
2846 return 0;
2847}
2848
2849static int mux_enum_put(struct snd_kcontrol *kcontrol,
2850 struct snd_ctl_elem_value *ucontrol)
2851{
2852 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Takashi Iwai2a8d5392013-01-18 16:23:25 +01002853 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002854 return mux_select(codec, adc_idx,
2855 ucontrol->value.enumerated.item[0]);
2856}
2857
Takashi Iwai352f7f92012-12-19 12:52:06 +01002858static const struct snd_kcontrol_new cap_src_temp = {
2859 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2860 .name = "Input Source",
2861 .info = mux_enum_info,
2862 .get = mux_enum_get,
2863 .put = mux_enum_put,
2864};
2865
Takashi Iwai47d46ab2012-12-20 11:48:54 +01002866/*
2867 * capture volume and capture switch ctls
2868 */
2869
Takashi Iwai352f7f92012-12-19 12:52:06 +01002870typedef int (*put_call_t)(struct snd_kcontrol *kcontrol,
2871 struct snd_ctl_elem_value *ucontrol);
2872
Takashi Iwai47d46ab2012-12-20 11:48:54 +01002873/* call the given amp update function for all amps in the imux list at once */
Takashi Iwai352f7f92012-12-19 12:52:06 +01002874static int cap_put_caller(struct snd_kcontrol *kcontrol,
2875 struct snd_ctl_elem_value *ucontrol,
2876 put_call_t func, int type)
2877{
2878 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2879 struct hda_gen_spec *spec = codec->spec;
2880 const struct hda_input_mux *imux;
2881 struct nid_path *path;
2882 int i, adc_idx, err = 0;
2883
2884 imux = &spec->input_mux;
David Henningssona053d1e2013-01-16 11:45:36 +01002885 adc_idx = kcontrol->id.index;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002886 mutex_lock(&codec->control_mutex);
Takashi Iwai47d46ab2012-12-20 11:48:54 +01002887 /* we use the cache-only update at first since multiple input paths
2888 * may shared the same amp; by updating only caches, the redundant
2889 * writes to hardware can be reduced.
2890 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01002891 codec->cached_write = 1;
2892 for (i = 0; i < imux->num_items; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01002893 path = get_input_path(codec, adc_idx, i);
2894 if (!path || !path->ctls[type])
Takashi Iwai352f7f92012-12-19 12:52:06 +01002895 continue;
2896 kcontrol->private_value = path->ctls[type];
2897 err = func(kcontrol, ucontrol);
2898 if (err < 0)
2899 goto error;
2900 }
2901 error:
2902 codec->cached_write = 0;
2903 mutex_unlock(&codec->control_mutex);
Takashi Iwaidc870f32013-01-22 15:24:30 +01002904 snd_hda_codec_flush_cache(codec); /* flush the updates */
Takashi Iwai352f7f92012-12-19 12:52:06 +01002905 if (err >= 0 && spec->cap_sync_hook)
Takashi Iwaia90229e2013-01-18 14:10:00 +01002906 spec->cap_sync_hook(codec, ucontrol);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002907 return err;
2908}
2909
2910/* capture volume ctl callbacks */
2911#define cap_vol_info snd_hda_mixer_amp_volume_info
2912#define cap_vol_get snd_hda_mixer_amp_volume_get
2913#define cap_vol_tlv snd_hda_mixer_amp_tlv
2914
2915static int cap_vol_put(struct snd_kcontrol *kcontrol,
2916 struct snd_ctl_elem_value *ucontrol)
2917{
2918 return cap_put_caller(kcontrol, ucontrol,
2919 snd_hda_mixer_amp_volume_put,
2920 NID_PATH_VOL_CTL);
2921}
2922
2923static const struct snd_kcontrol_new cap_vol_temp = {
2924 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2925 .name = "Capture Volume",
2926 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
2927 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
2928 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK),
2929 .info = cap_vol_info,
2930 .get = cap_vol_get,
2931 .put = cap_vol_put,
2932 .tlv = { .c = cap_vol_tlv },
2933};
2934
2935/* capture switch ctl callbacks */
2936#define cap_sw_info snd_ctl_boolean_stereo_info
2937#define cap_sw_get snd_hda_mixer_amp_switch_get
2938
2939static int cap_sw_put(struct snd_kcontrol *kcontrol,
2940 struct snd_ctl_elem_value *ucontrol)
2941{
Takashi Iwaia90229e2013-01-18 14:10:00 +01002942 return cap_put_caller(kcontrol, ucontrol,
Takashi Iwai352f7f92012-12-19 12:52:06 +01002943 snd_hda_mixer_amp_switch_put,
2944 NID_PATH_MUTE_CTL);
2945}
2946
2947static const struct snd_kcontrol_new cap_sw_temp = {
2948 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2949 .name = "Capture Switch",
2950 .info = cap_sw_info,
2951 .get = cap_sw_get,
2952 .put = cap_sw_put,
2953};
2954
2955static int parse_capvol_in_path(struct hda_codec *codec, struct nid_path *path)
2956{
2957 hda_nid_t nid;
2958 int i, depth;
2959
2960 path->ctls[NID_PATH_VOL_CTL] = path->ctls[NID_PATH_MUTE_CTL] = 0;
2961 for (depth = 0; depth < 3; depth++) {
2962 if (depth >= path->depth)
2963 return -EINVAL;
2964 i = path->depth - depth - 1;
2965 nid = path->path[i];
2966 if (!path->ctls[NID_PATH_VOL_CTL]) {
2967 if (nid_has_volume(codec, nid, HDA_OUTPUT))
2968 path->ctls[NID_PATH_VOL_CTL] =
2969 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
2970 else if (nid_has_volume(codec, nid, HDA_INPUT)) {
2971 int idx = path->idx[i];
2972 if (!depth && codec->single_adc_amp)
2973 idx = 0;
2974 path->ctls[NID_PATH_VOL_CTL] =
2975 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
2976 }
2977 }
2978 if (!path->ctls[NID_PATH_MUTE_CTL]) {
2979 if (nid_has_mute(codec, nid, HDA_OUTPUT))
2980 path->ctls[NID_PATH_MUTE_CTL] =
2981 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
2982 else if (nid_has_mute(codec, nid, HDA_INPUT)) {
2983 int idx = path->idx[i];
2984 if (!depth && codec->single_adc_amp)
2985 idx = 0;
2986 path->ctls[NID_PATH_MUTE_CTL] =
2987 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
2988 }
2989 }
Takashi Iwai97ec5582006-03-21 11:29:07 +01002990 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991 return 0;
2992}
2993
Takashi Iwai352f7f92012-12-19 12:52:06 +01002994static bool is_inv_dmic_pin(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002996 struct hda_gen_spec *spec = codec->spec;
2997 struct auto_pin_cfg *cfg = &spec->autocfg;
2998 unsigned int val;
2999 int i;
3000
3001 if (!spec->inv_dmic_split)
3002 return false;
3003 for (i = 0; i < cfg->num_inputs; i++) {
3004 if (cfg->inputs[i].pin != nid)
3005 continue;
3006 if (cfg->inputs[i].type != AUTO_PIN_MIC)
3007 return false;
3008 val = snd_hda_codec_get_pincfg(codec, nid);
3009 return snd_hda_get_input_pin_attr(val) == INPUT_PIN_ATTR_INT;
3010 }
3011 return false;
3012}
3013
Takashi Iwaia90229e2013-01-18 14:10:00 +01003014/* capture switch put callback for a single control with hook call */
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003015static int cap_single_sw_put(struct snd_kcontrol *kcontrol,
3016 struct snd_ctl_elem_value *ucontrol)
3017{
3018 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3019 struct hda_gen_spec *spec = codec->spec;
3020 int ret;
3021
3022 ret = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
3023 if (ret < 0)
3024 return ret;
3025
Takashi Iwaia90229e2013-01-18 14:10:00 +01003026 if (spec->cap_sync_hook)
3027 spec->cap_sync_hook(codec, ucontrol);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003028
3029 return ret;
3030}
3031
Takashi Iwai352f7f92012-12-19 12:52:06 +01003032static int add_single_cap_ctl(struct hda_codec *codec, const char *label,
3033 int idx, bool is_switch, unsigned int ctl,
3034 bool inv_dmic)
3035{
3036 struct hda_gen_spec *spec = codec->spec;
3037 char tmpname[44];
3038 int type = is_switch ? HDA_CTL_WIDGET_MUTE : HDA_CTL_WIDGET_VOL;
3039 const char *sfx = is_switch ? "Switch" : "Volume";
3040 unsigned int chs = inv_dmic ? 1 : 3;
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003041 struct snd_kcontrol_new *knew;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003042
3043 if (!ctl)
3044 return 0;
3045
3046 if (label)
3047 snprintf(tmpname, sizeof(tmpname),
3048 "%s Capture %s", label, sfx);
3049 else
3050 snprintf(tmpname, sizeof(tmpname),
3051 "Capture %s", sfx);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003052 knew = add_control(spec, type, tmpname, idx,
3053 amp_val_replace_channels(ctl, chs));
3054 if (!knew)
3055 return -ENOMEM;
Takashi Iwaia90229e2013-01-18 14:10:00 +01003056 if (is_switch)
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003057 knew->put = cap_single_sw_put;
3058 if (!inv_dmic)
3059 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003060
3061 /* Make independent right kcontrol */
3062 if (label)
3063 snprintf(tmpname, sizeof(tmpname),
3064 "Inverted %s Capture %s", label, sfx);
3065 else
3066 snprintf(tmpname, sizeof(tmpname),
3067 "Inverted Capture %s", sfx);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003068 knew = add_control(spec, type, tmpname, idx,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003069 amp_val_replace_channels(ctl, 2));
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003070 if (!knew)
3071 return -ENOMEM;
Takashi Iwaia90229e2013-01-18 14:10:00 +01003072 if (is_switch)
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003073 knew->put = cap_single_sw_put;
3074 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003075}
3076
3077/* create single (and simple) capture volume and switch controls */
3078static int create_single_cap_vol_ctl(struct hda_codec *codec, int idx,
3079 unsigned int vol_ctl, unsigned int sw_ctl,
3080 bool inv_dmic)
3081{
3082 int err;
3083 err = add_single_cap_ctl(codec, NULL, idx, false, vol_ctl, inv_dmic);
3084 if (err < 0)
3085 return err;
3086 err = add_single_cap_ctl(codec, NULL, idx, true, sw_ctl, inv_dmic);
3087 if (err < 0)
3088 return err;
3089 return 0;
3090}
3091
3092/* create bound capture volume and switch controls */
3093static int create_bind_cap_vol_ctl(struct hda_codec *codec, int idx,
3094 unsigned int vol_ctl, unsigned int sw_ctl)
3095{
3096 struct hda_gen_spec *spec = codec->spec;
3097 struct snd_kcontrol_new *knew;
3098
3099 if (vol_ctl) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01003100 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_vol_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003101 if (!knew)
3102 return -ENOMEM;
3103 knew->index = idx;
3104 knew->private_value = vol_ctl;
3105 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
3106 }
3107 if (sw_ctl) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01003108 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_sw_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003109 if (!knew)
3110 return -ENOMEM;
3111 knew->index = idx;
3112 knew->private_value = sw_ctl;
3113 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
3114 }
3115 return 0;
3116}
3117
3118/* return the vol ctl when used first in the imux list */
3119static unsigned int get_first_cap_ctl(struct hda_codec *codec, int idx, int type)
3120{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003121 struct nid_path *path;
3122 unsigned int ctl;
3123 int i;
3124
Takashi Iwaic697b712013-01-07 17:09:26 +01003125 path = get_input_path(codec, 0, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003126 if (!path)
3127 return 0;
3128 ctl = path->ctls[type];
3129 if (!ctl)
3130 return 0;
3131 for (i = 0; i < idx - 1; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01003132 path = get_input_path(codec, 0, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003133 if (path && path->ctls[type] == ctl)
3134 return 0;
3135 }
3136 return ctl;
3137}
3138
3139/* create individual capture volume and switch controls per input */
3140static int create_multi_cap_vol_ctl(struct hda_codec *codec)
3141{
3142 struct hda_gen_spec *spec = codec->spec;
3143 struct hda_input_mux *imux = &spec->input_mux;
Takashi Iwaic9700422013-01-18 10:17:30 +01003144 int i, err, type;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003145
3146 for (i = 0; i < imux->num_items; i++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003147 bool inv_dmic;
Takashi Iwaic9700422013-01-18 10:17:30 +01003148 int idx;
Takashi Iwai9dba2052013-01-18 10:01:15 +01003149
Takashi Iwaic9700422013-01-18 10:17:30 +01003150 idx = imux->items[i].index;
3151 if (idx >= spec->autocfg.num_inputs)
Takashi Iwai9dba2052013-01-18 10:01:15 +01003152 continue;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003153 inv_dmic = is_inv_dmic_pin(codec, spec->imux_pins[i]);
3154
3155 for (type = 0; type < 2; type++) {
Takashi Iwaic9700422013-01-18 10:17:30 +01003156 err = add_single_cap_ctl(codec,
3157 spec->input_labels[idx],
3158 spec->input_label_idxs[idx],
3159 type,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003160 get_first_cap_ctl(codec, i, type),
3161 inv_dmic);
3162 if (err < 0)
3163 return err;
3164 }
3165 }
3166 return 0;
3167}
3168
3169static int create_capture_mixers(struct hda_codec *codec)
3170{
3171 struct hda_gen_spec *spec = codec->spec;
3172 struct hda_input_mux *imux = &spec->input_mux;
3173 int i, n, nums, err;
3174
3175 if (spec->dyn_adc_switch)
3176 nums = 1;
3177 else
3178 nums = spec->num_adc_nids;
3179
3180 if (!spec->auto_mic && imux->num_items > 1) {
3181 struct snd_kcontrol_new *knew;
Takashi Iwai624d9142012-12-19 17:41:52 +01003182 const char *name;
3183 name = nums > 1 ? "Input Source" : "Capture Source";
3184 knew = snd_hda_gen_add_kctl(spec, name, &cap_src_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003185 if (!knew)
3186 return -ENOMEM;
3187 knew->count = nums;
3188 }
3189
3190 for (n = 0; n < nums; n++) {
3191 bool multi = false;
David Henningsson99a55922013-01-16 15:58:44 +01003192 bool multi_cap_vol = spec->multi_cap_vol;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003193 bool inv_dmic = false;
3194 int vol, sw;
3195
3196 vol = sw = 0;
3197 for (i = 0; i < imux->num_items; i++) {
3198 struct nid_path *path;
Takashi Iwaic697b712013-01-07 17:09:26 +01003199 path = get_input_path(codec, n, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003200 if (!path)
3201 continue;
3202 parse_capvol_in_path(codec, path);
3203 if (!vol)
3204 vol = path->ctls[NID_PATH_VOL_CTL];
David Henningsson99a55922013-01-16 15:58:44 +01003205 else if (vol != path->ctls[NID_PATH_VOL_CTL]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003206 multi = true;
David Henningsson99a55922013-01-16 15:58:44 +01003207 if (!same_amp_caps(codec, vol,
3208 path->ctls[NID_PATH_VOL_CTL], HDA_INPUT))
3209 multi_cap_vol = true;
3210 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003211 if (!sw)
3212 sw = path->ctls[NID_PATH_MUTE_CTL];
David Henningsson99a55922013-01-16 15:58:44 +01003213 else if (sw != path->ctls[NID_PATH_MUTE_CTL]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003214 multi = true;
David Henningsson99a55922013-01-16 15:58:44 +01003215 if (!same_amp_caps(codec, sw,
3216 path->ctls[NID_PATH_MUTE_CTL], HDA_INPUT))
3217 multi_cap_vol = true;
3218 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003219 if (is_inv_dmic_pin(codec, spec->imux_pins[i]))
3220 inv_dmic = true;
3221 }
3222
3223 if (!multi)
3224 err = create_single_cap_vol_ctl(codec, n, vol, sw,
3225 inv_dmic);
David Henningsson99a55922013-01-16 15:58:44 +01003226 else if (!multi_cap_vol)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003227 err = create_bind_cap_vol_ctl(codec, n, vol, sw);
3228 else
3229 err = create_multi_cap_vol_ctl(codec);
3230 if (err < 0)
3231 return err;
3232 }
3233
3234 return 0;
3235}
3236
3237/*
3238 * add mic boosts if needed
3239 */
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003240
3241/* check whether the given amp is feasible as a boost volume */
3242static bool check_boost_vol(struct hda_codec *codec, hda_nid_t nid,
3243 int dir, int idx)
3244{
3245 unsigned int step;
3246
3247 if (!nid_has_volume(codec, nid, dir) ||
3248 is_ctl_associated(codec, nid, dir, idx, NID_PATH_VOL_CTL) ||
3249 is_ctl_associated(codec, nid, dir, idx, NID_PATH_BOOST_CTL))
3250 return false;
3251
3252 step = (query_amp_caps(codec, nid, dir) & AC_AMPCAP_STEP_SIZE)
3253 >> AC_AMPCAP_STEP_SIZE_SHIFT;
3254 if (step < 0x20)
3255 return false;
3256 return true;
3257}
3258
3259/* look for a boost amp in a widget close to the pin */
3260static unsigned int look_for_boost_amp(struct hda_codec *codec,
3261 struct nid_path *path)
3262{
3263 unsigned int val = 0;
3264 hda_nid_t nid;
3265 int depth;
3266
3267 for (depth = 0; depth < 3; depth++) {
3268 if (depth >= path->depth - 1)
3269 break;
3270 nid = path->path[depth];
3271 if (depth && check_boost_vol(codec, nid, HDA_OUTPUT, 0)) {
3272 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3273 break;
3274 } else if (check_boost_vol(codec, nid, HDA_INPUT,
3275 path->idx[depth])) {
3276 val = HDA_COMPOSE_AMP_VAL(nid, 3, path->idx[depth],
3277 HDA_INPUT);
3278 break;
3279 }
3280 }
3281
3282 return val;
3283}
3284
Takashi Iwai352f7f92012-12-19 12:52:06 +01003285static int parse_mic_boost(struct hda_codec *codec)
3286{
3287 struct hda_gen_spec *spec = codec->spec;
3288 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003289 struct hda_input_mux *imux = &spec->input_mux;
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003290 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003291
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003292 if (!spec->num_adc_nids)
3293 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003294
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003295 for (i = 0; i < imux->num_items; i++) {
3296 struct nid_path *path;
3297 unsigned int val;
3298 int idx;
3299 char boost_label[44];
David Henningsson02aba552013-01-16 15:58:43 +01003300
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003301 idx = imux->items[i].index;
3302 if (idx >= imux->num_items)
3303 continue;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003304
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003305 /* check only line-in and mic pins */
Takashi Iwai1799cdd52013-01-18 14:37:16 +01003306 if (cfg->inputs[idx].type > AUTO_PIN_LINE_IN)
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003307 continue;
3308
3309 path = get_input_path(codec, 0, i);
3310 if (!path)
3311 continue;
3312
3313 val = look_for_boost_amp(codec, path);
3314 if (!val)
3315 continue;
3316
3317 /* create a boost control */
3318 snprintf(boost_label, sizeof(boost_label),
3319 "%s Boost Volume", spec->input_labels[idx]);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003320 if (!add_control(spec, HDA_CTL_WIDGET_VOL, boost_label,
3321 spec->input_label_idxs[idx], val))
3322 return -ENOMEM;
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003323
3324 path->ctls[NID_PATH_BOOST_CTL] = val;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003325 }
3326 return 0;
3327}
3328
3329/*
3330 * parse digital I/Os and set up NIDs in BIOS auto-parse mode
3331 */
3332static void parse_digital(struct hda_codec *codec)
3333{
3334 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003335 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003336 int i, nums;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003337 hda_nid_t dig_nid, pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003338
3339 /* support multiple SPDIFs; the secondary is set up as a slave */
3340 nums = 0;
3341 for (i = 0; i < spec->autocfg.dig_outs; i++) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01003342 pin = spec->autocfg.dig_out_pins[i];
Takashi Iwai352f7f92012-12-19 12:52:06 +01003343 dig_nid = look_for_dac(codec, pin, true);
3344 if (!dig_nid)
3345 continue;
Takashi Iwai3ca529d2013-01-07 17:25:08 +01003346 path = snd_hda_add_new_path(codec, dig_nid, pin, 0);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003347 if (!path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003348 continue;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003349 print_nid_path("digout", path);
Takashi Iwaie1284af2013-01-03 16:33:02 +01003350 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +01003351 spec->digout_paths[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai2c12c302013-01-10 09:33:29 +01003352 set_pin_target(codec, pin, PIN_OUT, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003353 if (!nums) {
3354 spec->multiout.dig_out_nid = dig_nid;
3355 spec->dig_out_type = spec->autocfg.dig_out_type[0];
3356 } else {
3357 spec->multiout.slave_dig_outs = spec->slave_dig_outs;
3358 if (nums >= ARRAY_SIZE(spec->slave_dig_outs) - 1)
3359 break;
3360 spec->slave_dig_outs[nums - 1] = dig_nid;
3361 }
3362 nums++;
3363 }
3364
3365 if (spec->autocfg.dig_in_pin) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01003366 pin = spec->autocfg.dig_in_pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003367 dig_nid = codec->start_nid;
3368 for (i = 0; i < codec->num_nodes; i++, dig_nid++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003369 unsigned int wcaps = get_wcaps(codec, dig_nid);
3370 if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
3371 continue;
3372 if (!(wcaps & AC_WCAP_DIGITAL))
3373 continue;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003374 path = snd_hda_add_new_path(codec, pin, dig_nid, 0);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003375 if (path) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003376 print_nid_path("digin", path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003377 path->active = true;
3378 spec->dig_in_nid = dig_nid;
Takashi Iwai2430d7b2013-01-04 15:09:42 +01003379 spec->digin_path = snd_hda_get_path_idx(codec, path);
Takashi Iwai2c12c302013-01-10 09:33:29 +01003380 set_pin_target(codec, pin, PIN_IN, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003381 break;
3382 }
3383 }
3384 }
3385}
3386
3387
3388/*
3389 * input MUX handling
3390 */
3391
3392static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur);
3393
3394/* select the given imux item; either unmute exclusively or select the route */
3395static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
3396 unsigned int idx)
3397{
3398 struct hda_gen_spec *spec = codec->spec;
3399 const struct hda_input_mux *imux;
Takashi Iwai55196ff2013-01-24 17:32:56 +01003400 struct nid_path *old_path, *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003401
3402 imux = &spec->input_mux;
3403 if (!imux->num_items)
3404 return 0;
3405
3406 if (idx >= imux->num_items)
3407 idx = imux->num_items - 1;
3408 if (spec->cur_mux[adc_idx] == idx)
3409 return 0;
3410
Takashi Iwai55196ff2013-01-24 17:32:56 +01003411 old_path = get_input_path(codec, adc_idx, spec->cur_mux[adc_idx]);
3412 if (!old_path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003413 return 0;
Takashi Iwai55196ff2013-01-24 17:32:56 +01003414 if (old_path->active)
3415 snd_hda_activate_path(codec, old_path, false, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003416
3417 spec->cur_mux[adc_idx] = idx;
3418
3419 if (spec->shared_mic_hp)
3420 update_shared_mic_hp(codec, spec->cur_mux[adc_idx]);
3421
3422 if (spec->dyn_adc_switch)
3423 dyn_adc_pcm_resetup(codec, idx);
3424
Takashi Iwaic697b712013-01-07 17:09:26 +01003425 path = get_input_path(codec, adc_idx, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003426 if (!path)
3427 return 0;
3428 if (path->active)
3429 return 0;
3430 snd_hda_activate_path(codec, path, true, false);
3431 if (spec->cap_sync_hook)
Takashi Iwaia90229e2013-01-18 14:10:00 +01003432 spec->cap_sync_hook(codec, NULL);
Takashi Iwai55196ff2013-01-24 17:32:56 +01003433 path_power_down_sync(codec, old_path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003434 return 1;
3435}
3436
3437
3438/*
3439 * Jack detections for HP auto-mute and mic-switch
3440 */
3441
3442/* check each pin in the given array; returns true if any of them is plugged */
3443static bool detect_jacks(struct hda_codec *codec, int num_pins, hda_nid_t *pins)
3444{
3445 int i, present = 0;
3446
3447 for (i = 0; i < num_pins; i++) {
3448 hda_nid_t nid = pins[i];
3449 if (!nid)
3450 break;
Takashi Iwai0b4df932013-01-10 09:45:13 +01003451 /* don't detect pins retasked as inputs */
3452 if (snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_IN_EN)
3453 continue;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003454 present |= snd_hda_jack_detect(codec, nid);
3455 }
3456 return present;
3457}
3458
3459/* standard HP/line-out auto-mute helper */
3460static void do_automute(struct hda_codec *codec, int num_pins, hda_nid_t *pins,
Takashi Iwai2c12c302013-01-10 09:33:29 +01003461 bool mute)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003462{
3463 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003464 int i;
3465
3466 for (i = 0; i < num_pins; i++) {
3467 hda_nid_t nid = pins[i];
3468 unsigned int val;
3469 if (!nid)
3470 break;
3471 /* don't reset VREF value in case it's controlling
3472 * the amp (see alc861_fixup_asus_amp_vref_0f())
3473 */
Takashi Iwai2c12c302013-01-10 09:33:29 +01003474 if (spec->keep_vref_in_automute)
3475 val = snd_hda_codec_get_pin_target(codec, nid) & ~PIN_HP;
3476 else
Takashi Iwai352f7f92012-12-19 12:52:06 +01003477 val = 0;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003478 if (!mute)
3479 val |= snd_hda_codec_get_pin_target(codec, nid);
3480 /* here we call update_pin_ctl() so that the pinctl is changed
3481 * without changing the pinctl target value;
3482 * the original target value will be still referred at the
3483 * init / resume again
3484 */
3485 update_pin_ctl(codec, nid, val);
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01003486 set_pin_eapd(codec, nid, !mute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003487 }
3488}
3489
3490/* Toggle outputs muting */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003491void snd_hda_gen_update_outputs(struct hda_codec *codec)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003492{
3493 struct hda_gen_spec *spec = codec->spec;
3494 int on;
3495
3496 /* Control HP pins/amps depending on master_mute state;
3497 * in general, HP pins/amps control should be enabled in all cases,
3498 * but currently set only for master_mute, just to be safe
3499 */
3500 if (!spec->shared_mic_hp) /* don't change HP-pin when shared with mic */
3501 do_automute(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
Takashi Iwai2c12c302013-01-10 09:33:29 +01003502 spec->autocfg.hp_pins, spec->master_mute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003503
3504 if (!spec->automute_speaker)
3505 on = 0;
3506 else
3507 on = spec->hp_jack_present | spec->line_jack_present;
3508 on |= spec->master_mute;
Takashi Iwai47b9ddb2013-01-16 18:18:00 +01003509 spec->speaker_muted = on;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003510 do_automute(codec, ARRAY_SIZE(spec->autocfg.speaker_pins),
Takashi Iwai2c12c302013-01-10 09:33:29 +01003511 spec->autocfg.speaker_pins, on);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003512
3513 /* toggle line-out mutes if needed, too */
3514 /* if LO is a copy of either HP or Speaker, don't need to handle it */
3515 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0] ||
3516 spec->autocfg.line_out_pins[0] == spec->autocfg.speaker_pins[0])
3517 return;
3518 if (!spec->automute_lo)
3519 on = 0;
3520 else
3521 on = spec->hp_jack_present;
3522 on |= spec->master_mute;
Takashi Iwai47b9ddb2013-01-16 18:18:00 +01003523 spec->line_out_muted = on;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003524 do_automute(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
Takashi Iwai2c12c302013-01-10 09:33:29 +01003525 spec->autocfg.line_out_pins, on);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003526}
Takashi Iwai5d550e12012-12-19 15:16:44 +01003527EXPORT_SYMBOL_HDA(snd_hda_gen_update_outputs);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003528
3529static void call_update_outputs(struct hda_codec *codec)
3530{
3531 struct hda_gen_spec *spec = codec->spec;
3532 if (spec->automute_hook)
3533 spec->automute_hook(codec);
3534 else
Takashi Iwai5d550e12012-12-19 15:16:44 +01003535 snd_hda_gen_update_outputs(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003536}
3537
3538/* standard HP-automute helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003539void snd_hda_gen_hp_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003540{
3541 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai92603c52013-01-22 07:46:31 +01003542 hda_nid_t *pins = spec->autocfg.hp_pins;
3543 int num_pins = ARRAY_SIZE(spec->autocfg.hp_pins);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003544
Takashi Iwai92603c52013-01-22 07:46:31 +01003545 /* No detection for the first HP jack during indep-HP mode */
3546 if (spec->indep_hp_enabled) {
3547 pins++;
3548 num_pins--;
3549 }
3550
3551 spec->hp_jack_present = detect_jacks(codec, num_pins, pins);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003552 if (!spec->detect_hp || (!spec->automute_speaker && !spec->automute_lo))
3553 return;
3554 call_update_outputs(codec);
3555}
Takashi Iwai5d550e12012-12-19 15:16:44 +01003556EXPORT_SYMBOL_HDA(snd_hda_gen_hp_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003557
3558/* standard line-out-automute helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003559void snd_hda_gen_line_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003560{
3561 struct hda_gen_spec *spec = codec->spec;
3562
3563 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
3564 return;
3565 /* check LO jack only when it's different from HP */
3566 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0])
3567 return;
3568
3569 spec->line_jack_present =
3570 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
3571 spec->autocfg.line_out_pins);
3572 if (!spec->automute_speaker || !spec->detect_lo)
3573 return;
3574 call_update_outputs(codec);
3575}
Takashi Iwai5d550e12012-12-19 15:16:44 +01003576EXPORT_SYMBOL_HDA(snd_hda_gen_line_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003577
3578/* standard mic auto-switch helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003579void snd_hda_gen_mic_autoswitch(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003580{
3581 struct hda_gen_spec *spec = codec->spec;
3582 int i;
3583
3584 if (!spec->auto_mic)
3585 return;
3586
3587 for (i = spec->am_num_entries - 1; i > 0; i--) {
Takashi Iwai0b4df932013-01-10 09:45:13 +01003588 hda_nid_t pin = spec->am_entry[i].pin;
3589 /* don't detect pins retasked as outputs */
3590 if (snd_hda_codec_get_pin_target(codec, pin) & AC_PINCTL_OUT_EN)
3591 continue;
3592 if (snd_hda_jack_detect(codec, pin)) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003593 mux_select(codec, 0, spec->am_entry[i].idx);
3594 return;
3595 }
3596 }
3597 mux_select(codec, 0, spec->am_entry[0].idx);
3598}
Takashi Iwai5d550e12012-12-19 15:16:44 +01003599EXPORT_SYMBOL_HDA(snd_hda_gen_mic_autoswitch);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003600
Takashi Iwaia5cc2502013-01-16 18:08:55 +01003601/* update jack retasking */
3602static void update_automute_all(struct hda_codec *codec)
3603{
3604 struct hda_gen_spec *spec = codec->spec;
3605
3606 if (spec->hp_automute_hook)
3607 spec->hp_automute_hook(codec, NULL);
3608 else
3609 snd_hda_gen_hp_automute(codec, NULL);
3610 if (spec->line_automute_hook)
3611 spec->line_automute_hook(codec, NULL);
3612 else
3613 snd_hda_gen_line_automute(codec, NULL);
3614 if (spec->mic_autoswitch_hook)
3615 spec->mic_autoswitch_hook(codec, NULL);
3616 else
3617 snd_hda_gen_mic_autoswitch(codec, NULL);
3618}
3619
Takashi Iwai352f7f92012-12-19 12:52:06 +01003620/*
3621 * Auto-Mute mode mixer enum support
3622 */
3623static int automute_mode_info(struct snd_kcontrol *kcontrol,
3624 struct snd_ctl_elem_info *uinfo)
3625{
3626 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3627 struct hda_gen_spec *spec = codec->spec;
3628 static const char * const texts3[] = {
3629 "Disabled", "Speaker Only", "Line Out+Speaker"
Takashi Iwai071c73a2006-08-23 18:34:06 +02003630 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003631
Takashi Iwai352f7f92012-12-19 12:52:06 +01003632 if (spec->automute_speaker_possible && spec->automute_lo_possible)
3633 return snd_hda_enum_helper_info(kcontrol, uinfo, 3, texts3);
3634 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
3635}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003636
Takashi Iwai352f7f92012-12-19 12:52:06 +01003637static int automute_mode_get(struct snd_kcontrol *kcontrol,
3638 struct snd_ctl_elem_value *ucontrol)
3639{
3640 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3641 struct hda_gen_spec *spec = codec->spec;
3642 unsigned int val = 0;
3643 if (spec->automute_speaker)
3644 val++;
3645 if (spec->automute_lo)
3646 val++;
Takashi Iwai071c73a2006-08-23 18:34:06 +02003647
Takashi Iwai352f7f92012-12-19 12:52:06 +01003648 ucontrol->value.enumerated.item[0] = val;
3649 return 0;
3650}
3651
3652static int automute_mode_put(struct snd_kcontrol *kcontrol,
3653 struct snd_ctl_elem_value *ucontrol)
3654{
3655 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3656 struct hda_gen_spec *spec = codec->spec;
3657
3658 switch (ucontrol->value.enumerated.item[0]) {
3659 case 0:
3660 if (!spec->automute_speaker && !spec->automute_lo)
3661 return 0;
3662 spec->automute_speaker = 0;
3663 spec->automute_lo = 0;
3664 break;
3665 case 1:
3666 if (spec->automute_speaker_possible) {
3667 if (!spec->automute_lo && spec->automute_speaker)
3668 return 0;
3669 spec->automute_speaker = 1;
3670 spec->automute_lo = 0;
3671 } else if (spec->automute_lo_possible) {
3672 if (spec->automute_lo)
3673 return 0;
3674 spec->automute_lo = 1;
3675 } else
3676 return -EINVAL;
3677 break;
3678 case 2:
3679 if (!spec->automute_lo_possible || !spec->automute_speaker_possible)
3680 return -EINVAL;
3681 if (spec->automute_speaker && spec->automute_lo)
3682 return 0;
3683 spec->automute_speaker = 1;
3684 spec->automute_lo = 1;
3685 break;
3686 default:
3687 return -EINVAL;
3688 }
3689 call_update_outputs(codec);
3690 return 1;
3691}
3692
3693static const struct snd_kcontrol_new automute_mode_enum = {
3694 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3695 .name = "Auto-Mute Mode",
3696 .info = automute_mode_info,
3697 .get = automute_mode_get,
3698 .put = automute_mode_put,
3699};
3700
3701static int add_automute_mode_enum(struct hda_codec *codec)
3702{
3703 struct hda_gen_spec *spec = codec->spec;
3704
Takashi Iwai12c93df2012-12-19 14:38:33 +01003705 if (!snd_hda_gen_add_kctl(spec, NULL, &automute_mode_enum))
Takashi Iwai352f7f92012-12-19 12:52:06 +01003706 return -ENOMEM;
3707 return 0;
3708}
3709
3710/*
3711 * Check the availability of HP/line-out auto-mute;
3712 * Set up appropriately if really supported
3713 */
3714static int check_auto_mute_availability(struct hda_codec *codec)
3715{
3716 struct hda_gen_spec *spec = codec->spec;
3717 struct auto_pin_cfg *cfg = &spec->autocfg;
3718 int present = 0;
3719 int i, err;
3720
Takashi Iwaif72706b2013-01-16 18:20:07 +01003721 if (spec->suppress_auto_mute)
3722 return 0;
3723
Takashi Iwai352f7f92012-12-19 12:52:06 +01003724 if (cfg->hp_pins[0])
3725 present++;
3726 if (cfg->line_out_pins[0])
3727 present++;
3728 if (cfg->speaker_pins[0])
3729 present++;
3730 if (present < 2) /* need two different output types */
Takashi Iwai071c73a2006-08-23 18:34:06 +02003731 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003732
3733 if (!cfg->speaker_pins[0] &&
3734 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
3735 memcpy(cfg->speaker_pins, cfg->line_out_pins,
3736 sizeof(cfg->speaker_pins));
3737 cfg->speaker_outs = cfg->line_outs;
Takashi Iwai071c73a2006-08-23 18:34:06 +02003738 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003739
Takashi Iwai352f7f92012-12-19 12:52:06 +01003740 if (!cfg->hp_pins[0] &&
3741 cfg->line_out_type == AUTO_PIN_HP_OUT) {
3742 memcpy(cfg->hp_pins, cfg->line_out_pins,
3743 sizeof(cfg->hp_pins));
3744 cfg->hp_outs = cfg->line_outs;
3745 }
3746
3747 for (i = 0; i < cfg->hp_outs; i++) {
3748 hda_nid_t nid = cfg->hp_pins[i];
3749 if (!is_jack_detectable(codec, nid))
3750 continue;
3751 snd_printdd("hda-codec: Enable HP auto-muting on NID 0x%x\n",
3752 nid);
3753 snd_hda_jack_detect_enable_callback(codec, nid, HDA_GEN_HP_EVENT,
Takashi Iwai2e03e952013-01-03 15:55:06 +01003754 spec->hp_automute_hook ?
3755 spec->hp_automute_hook :
Takashi Iwai5d550e12012-12-19 15:16:44 +01003756 snd_hda_gen_hp_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003757 spec->detect_hp = 1;
3758 }
3759
3760 if (cfg->line_out_type == AUTO_PIN_LINE_OUT && cfg->line_outs) {
3761 if (cfg->speaker_outs)
3762 for (i = 0; i < cfg->line_outs; i++) {
3763 hda_nid_t nid = cfg->line_out_pins[i];
3764 if (!is_jack_detectable(codec, nid))
3765 continue;
3766 snd_printdd("hda-codec: Enable Line-Out auto-muting on NID 0x%x\n", nid);
3767 snd_hda_jack_detect_enable_callback(codec, nid,
3768 HDA_GEN_FRONT_EVENT,
Takashi Iwai2e03e952013-01-03 15:55:06 +01003769 spec->line_automute_hook ?
3770 spec->line_automute_hook :
Takashi Iwai5d550e12012-12-19 15:16:44 +01003771 snd_hda_gen_line_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003772 spec->detect_lo = 1;
3773 }
3774 spec->automute_lo_possible = spec->detect_hp;
3775 }
3776
3777 spec->automute_speaker_possible = cfg->speaker_outs &&
3778 (spec->detect_hp || spec->detect_lo);
3779
3780 spec->automute_lo = spec->automute_lo_possible;
3781 spec->automute_speaker = spec->automute_speaker_possible;
3782
3783 if (spec->automute_speaker_possible || spec->automute_lo_possible) {
3784 /* create a control for automute mode */
3785 err = add_automute_mode_enum(codec);
3786 if (err < 0)
3787 return err;
3788 }
3789 return 0;
3790}
3791
Takashi Iwai352f7f92012-12-19 12:52:06 +01003792/* check whether all auto-mic pins are valid; setup indices if OK */
3793static bool auto_mic_check_imux(struct hda_codec *codec)
3794{
3795 struct hda_gen_spec *spec = codec->spec;
3796 const struct hda_input_mux *imux;
3797 int i;
3798
3799 imux = &spec->input_mux;
3800 for (i = 0; i < spec->am_num_entries; i++) {
3801 spec->am_entry[i].idx =
3802 find_idx_in_nid_list(spec->am_entry[i].pin,
3803 spec->imux_pins, imux->num_items);
3804 if (spec->am_entry[i].idx < 0)
3805 return false; /* no corresponding imux */
3806 }
3807
3808 /* we don't need the jack detection for the first pin */
3809 for (i = 1; i < spec->am_num_entries; i++)
3810 snd_hda_jack_detect_enable_callback(codec,
3811 spec->am_entry[i].pin,
3812 HDA_GEN_MIC_EVENT,
Takashi Iwai2e03e952013-01-03 15:55:06 +01003813 spec->mic_autoswitch_hook ?
3814 spec->mic_autoswitch_hook :
Takashi Iwai5d550e12012-12-19 15:16:44 +01003815 snd_hda_gen_mic_autoswitch);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003816 return true;
3817}
3818
3819static int compare_attr(const void *ap, const void *bp)
3820{
3821 const struct automic_entry *a = ap;
3822 const struct automic_entry *b = bp;
3823 return (int)(a->attr - b->attr);
3824}
3825
3826/*
3827 * Check the availability of auto-mic switch;
3828 * Set up if really supported
3829 */
3830static int check_auto_mic_availability(struct hda_codec *codec)
3831{
3832 struct hda_gen_spec *spec = codec->spec;
3833 struct auto_pin_cfg *cfg = &spec->autocfg;
3834 unsigned int types;
3835 int i, num_pins;
3836
Takashi Iwaid12daf62013-01-07 16:32:11 +01003837 if (spec->suppress_auto_mic)
3838 return 0;
3839
Takashi Iwai352f7f92012-12-19 12:52:06 +01003840 types = 0;
3841 num_pins = 0;
3842 for (i = 0; i < cfg->num_inputs; i++) {
3843 hda_nid_t nid = cfg->inputs[i].pin;
3844 unsigned int attr;
3845 attr = snd_hda_codec_get_pincfg(codec, nid);
3846 attr = snd_hda_get_input_pin_attr(attr);
3847 if (types & (1 << attr))
3848 return 0; /* already occupied */
3849 switch (attr) {
3850 case INPUT_PIN_ATTR_INT:
3851 if (cfg->inputs[i].type != AUTO_PIN_MIC)
3852 return 0; /* invalid type */
3853 break;
3854 case INPUT_PIN_ATTR_UNUSED:
3855 return 0; /* invalid entry */
3856 default:
3857 if (cfg->inputs[i].type > AUTO_PIN_LINE_IN)
3858 return 0; /* invalid type */
3859 if (!spec->line_in_auto_switch &&
3860 cfg->inputs[i].type != AUTO_PIN_MIC)
3861 return 0; /* only mic is allowed */
3862 if (!is_jack_detectable(codec, nid))
3863 return 0; /* no unsol support */
3864 break;
3865 }
3866 if (num_pins >= MAX_AUTO_MIC_PINS)
3867 return 0;
3868 types |= (1 << attr);
3869 spec->am_entry[num_pins].pin = nid;
3870 spec->am_entry[num_pins].attr = attr;
3871 num_pins++;
3872 }
3873
3874 if (num_pins < 2)
3875 return 0;
3876
3877 spec->am_num_entries = num_pins;
3878 /* sort the am_entry in the order of attr so that the pin with a
3879 * higher attr will be selected when the jack is plugged.
3880 */
3881 sort(spec->am_entry, num_pins, sizeof(spec->am_entry[0]),
3882 compare_attr, NULL);
3883
3884 if (!auto_mic_check_imux(codec))
3885 return 0;
3886
3887 spec->auto_mic = 1;
3888 spec->num_adc_nids = 1;
3889 spec->cur_mux[0] = spec->am_entry[0].idx;
3890 snd_printdd("hda-codec: Enable auto-mic switch on NID 0x%x/0x%x/0x%x\n",
3891 spec->am_entry[0].pin,
3892 spec->am_entry[1].pin,
3893 spec->am_entry[2].pin);
3894
3895 return 0;
3896}
3897
Takashi Iwai55196ff2013-01-24 17:32:56 +01003898/* power_filter hook; make inactive widgets into power down */
3899static unsigned int snd_hda_gen_path_power_filter(struct hda_codec *codec,
3900 hda_nid_t nid,
3901 unsigned int power_state)
3902{
3903 if (power_state != AC_PWRST_D0)
3904 return power_state;
3905 if (get_wcaps_type(get_wcaps(codec, nid)) >= AC_WID_POWER)
3906 return power_state;
3907 if (is_active_nid(codec, nid, HDA_OUTPUT, 0))
3908 return power_state;
3909 return AC_PWRST_D3;
3910}
3911
Takashi Iwai352f7f92012-12-19 12:52:06 +01003912
Takashi Iwai9eb413e2012-12-19 14:41:21 +01003913/*
3914 * Parse the given BIOS configuration and set up the hda_gen_spec
3915 *
3916 * return 1 if successful, 0 if the proper config is not found,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003917 * or a negative error code
3918 */
3919int snd_hda_gen_parse_auto_config(struct hda_codec *codec,
Takashi Iwai9eb413e2012-12-19 14:41:21 +01003920 struct auto_pin_cfg *cfg)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003921{
3922 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003923 int err;
3924
Takashi Iwai1c70a582013-01-11 17:48:22 +01003925 parse_user_hints(codec);
3926
Takashi Iwaie4a395e2013-01-23 17:00:31 +01003927 if (spec->mixer_nid && !spec->mixer_merge_nid)
3928 spec->mixer_merge_nid = spec->mixer_nid;
3929
Takashi Iwai9eb413e2012-12-19 14:41:21 +01003930 if (cfg != &spec->autocfg) {
3931 spec->autocfg = *cfg;
3932 cfg = &spec->autocfg;
3933 }
3934
David Henningsson6fc4cb92013-01-16 15:58:45 +01003935 fill_all_dac_nids(codec);
3936
Takashi Iwai352f7f92012-12-19 12:52:06 +01003937 if (!cfg->line_outs) {
3938 if (cfg->dig_outs || cfg->dig_in_pin) {
3939 spec->multiout.max_channels = 2;
3940 spec->no_analog = 1;
3941 goto dig_only;
3942 }
3943 return 0; /* can't find valid BIOS pin config */
3944 }
3945
3946 if (!spec->no_primary_hp &&
3947 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT &&
3948 cfg->line_outs <= cfg->hp_outs) {
3949 /* use HP as primary out */
3950 cfg->speaker_outs = cfg->line_outs;
3951 memcpy(cfg->speaker_pins, cfg->line_out_pins,
3952 sizeof(cfg->speaker_pins));
3953 cfg->line_outs = cfg->hp_outs;
3954 memcpy(cfg->line_out_pins, cfg->hp_pins, sizeof(cfg->hp_pins));
3955 cfg->hp_outs = 0;
3956 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
3957 cfg->line_out_type = AUTO_PIN_HP_OUT;
3958 }
3959
3960 err = parse_output_paths(codec);
3961 if (err < 0)
3962 return err;
3963 err = create_multi_channel_mode(codec);
3964 if (err < 0)
3965 return err;
3966 err = create_multi_out_ctls(codec, cfg);
3967 if (err < 0)
3968 return err;
3969 err = create_hp_out_ctls(codec);
3970 if (err < 0)
3971 return err;
3972 err = create_speaker_out_ctls(codec);
3973 if (err < 0)
3974 return err;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003975 err = create_indep_hp_ctls(codec);
3976 if (err < 0)
3977 return err;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01003978 err = create_loopback_mixing_ctl(codec);
3979 if (err < 0)
3980 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003981 err = create_shared_input(codec);
3982 if (err < 0)
3983 return err;
3984 err = create_input_ctls(codec);
Takashi Iwaid13bd412008-07-30 15:01:45 +02003985 if (err < 0)
Takashi Iwai071c73a2006-08-23 18:34:06 +02003986 return err;
3987
Takashi Iwaia07a9492013-01-07 16:44:06 +01003988 spec->const_channel_count = spec->ext_channel_count;
3989 /* check the multiple speaker and headphone pins */
3990 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
3991 spec->const_channel_count = max(spec->const_channel_count,
3992 cfg->speaker_outs * 2);
3993 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
3994 spec->const_channel_count = max(spec->const_channel_count,
3995 cfg->hp_outs * 2);
3996 spec->multiout.max_channels = max(spec->ext_channel_count,
3997 spec->const_channel_count);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003998
3999 err = check_auto_mute_availability(codec);
4000 if (err < 0)
4001 return err;
4002
4003 err = check_dyn_adc_switch(codec);
4004 if (err < 0)
4005 return err;
4006
4007 if (!spec->shared_mic_hp) {
4008 err = check_auto_mic_availability(codec);
Takashi Iwaid13bd412008-07-30 15:01:45 +02004009 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004010 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004011 }
Takashi Iwai071c73a2006-08-23 18:34:06 +02004012
Takashi Iwai352f7f92012-12-19 12:52:06 +01004013 err = create_capture_mixers(codec);
4014 if (err < 0)
4015 return err;
4016
4017 err = parse_mic_boost(codec);
4018 if (err < 0)
4019 return err;
4020
Takashi Iwai978e77e2013-01-10 16:57:58 +01004021 if (spec->add_out_jack_modes) {
4022 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
4023 err = create_out_jack_modes(codec, cfg->line_outs,
4024 cfg->line_out_pins);
4025 if (err < 0)
4026 return err;
4027 }
4028 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
4029 err = create_out_jack_modes(codec, cfg->hp_outs,
4030 cfg->hp_pins);
4031 if (err < 0)
4032 return err;
4033 }
4034 }
4035
Takashi Iwai352f7f92012-12-19 12:52:06 +01004036 dig_only:
4037 parse_digital(codec);
4038
Takashi Iwai55196ff2013-01-24 17:32:56 +01004039 if (spec->power_down_unused)
4040 codec->power_filter = snd_hda_gen_path_power_filter;
4041
Takashi Iwai352f7f92012-12-19 12:52:06 +01004042 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004043}
Takashi Iwai352f7f92012-12-19 12:52:06 +01004044EXPORT_SYMBOL_HDA(snd_hda_gen_parse_auto_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004045
4046
4047/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01004048 * Build control elements
Linus Torvalds1da177e2005-04-16 15:20:36 -07004049 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01004050
4051/* slave controls for virtual master */
4052static const char * const slave_pfxs[] = {
4053 "Front", "Surround", "Center", "LFE", "Side",
4054 "Headphone", "Speaker", "Mono", "Line Out",
4055 "CLFE", "Bass Speaker", "PCM",
Takashi Iwaiee79c692013-01-07 09:57:42 +01004056 "Speaker Front", "Speaker Surround", "Speaker CLFE", "Speaker Side",
4057 "Headphone Front", "Headphone Surround", "Headphone CLFE",
4058 "Headphone Side",
Takashi Iwai352f7f92012-12-19 12:52:06 +01004059 NULL,
4060};
4061
4062int snd_hda_gen_build_controls(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004063{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004064 struct hda_gen_spec *spec = codec->spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004065 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004066
Takashi Iwai36502d02012-12-19 15:15:10 +01004067 if (spec->kctls.used) {
4068 err = snd_hda_add_new_ctls(codec, spec->kctls.list);
4069 if (err < 0)
4070 return err;
4071 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004072
Takashi Iwai352f7f92012-12-19 12:52:06 +01004073 if (spec->multiout.dig_out_nid) {
4074 err = snd_hda_create_dig_out_ctls(codec,
4075 spec->multiout.dig_out_nid,
4076 spec->multiout.dig_out_nid,
4077 spec->pcm_rec[1].pcm_type);
4078 if (err < 0)
4079 return err;
4080 if (!spec->no_analog) {
4081 err = snd_hda_create_spdif_share_sw(codec,
4082 &spec->multiout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004083 if (err < 0)
4084 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004085 spec->multiout.share_spdif = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004086 }
4087 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01004088 if (spec->dig_in_nid) {
4089 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
4090 if (err < 0)
4091 return err;
4092 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004093
Takashi Iwai352f7f92012-12-19 12:52:06 +01004094 /* if we have no master control, let's create it */
4095 if (!spec->no_analog &&
4096 !snd_hda_find_mixer_ctl(codec, "Master Playback Volume")) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01004097 err = snd_hda_add_vmaster(codec, "Master Playback Volume",
Takashi Iwai7a71bbf2013-01-17 10:25:15 +01004098 spec->vmaster_tlv, slave_pfxs,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004099 "Playback Volume");
4100 if (err < 0)
4101 return err;
4102 }
4103 if (!spec->no_analog &&
4104 !snd_hda_find_mixer_ctl(codec, "Master Playback Switch")) {
4105 err = __snd_hda_add_vmaster(codec, "Master Playback Switch",
4106 NULL, slave_pfxs,
4107 "Playback Switch",
4108 true, &spec->vmaster_mute.sw_kctl);
4109 if (err < 0)
4110 return err;
4111 if (spec->vmaster_mute.hook)
Takashi Iwaifd25a972012-12-20 14:57:18 +01004112 snd_hda_add_vmaster_hook(codec, &spec->vmaster_mute,
4113 spec->vmaster_mute_enum);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004114 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004115
Takashi Iwai352f7f92012-12-19 12:52:06 +01004116 free_kctls(spec); /* no longer needed */
4117
4118 if (spec->shared_mic_hp) {
4119 int err;
4120 int nid = spec->autocfg.inputs[1].pin;
4121 err = snd_hda_jack_add_kctl(codec, nid, "Headphone Mic", 0);
4122 if (err < 0)
4123 return err;
4124 err = snd_hda_jack_detect_enable(codec, nid, 0);
4125 if (err < 0)
4126 return err;
4127 }
4128
4129 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
4130 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004131 return err;
4132
4133 return 0;
4134}
Takashi Iwai352f7f92012-12-19 12:52:06 +01004135EXPORT_SYMBOL_HDA(snd_hda_gen_build_controls);
4136
Linus Torvalds1da177e2005-04-16 15:20:36 -07004137
4138/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01004139 * PCM definitions
Linus Torvalds1da177e2005-04-16 15:20:36 -07004140 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004141
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004142static void call_pcm_playback_hook(struct hda_pcm_stream *hinfo,
4143 struct hda_codec *codec,
4144 struct snd_pcm_substream *substream,
4145 int action)
4146{
4147 struct hda_gen_spec *spec = codec->spec;
4148 if (spec->pcm_playback_hook)
4149 spec->pcm_playback_hook(hinfo, codec, substream, action);
4150}
4151
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004152static void call_pcm_capture_hook(struct hda_pcm_stream *hinfo,
4153 struct hda_codec *codec,
4154 struct snd_pcm_substream *substream,
4155 int action)
4156{
4157 struct hda_gen_spec *spec = codec->spec;
4158 if (spec->pcm_capture_hook)
4159 spec->pcm_capture_hook(hinfo, codec, substream, action);
4160}
4161
Takashi Iwai352f7f92012-12-19 12:52:06 +01004162/*
4163 * Analog playback callbacks
4164 */
4165static int playback_pcm_open(struct hda_pcm_stream *hinfo,
4166 struct hda_codec *codec,
4167 struct snd_pcm_substream *substream)
4168{
4169 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004170 int err;
4171
4172 mutex_lock(&spec->pcm_mutex);
4173 err = snd_hda_multi_out_analog_open(codec,
4174 &spec->multiout, substream,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004175 hinfo);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004176 if (!err) {
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004177 spec->active_streams |= 1 << STREAM_MULTI_OUT;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004178 call_pcm_playback_hook(hinfo, codec, substream,
4179 HDA_GEN_PCM_ACT_OPEN);
4180 }
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004181 mutex_unlock(&spec->pcm_mutex);
4182 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004183}
4184
4185static int playback_pcm_prepare(struct hda_pcm_stream *hinfo,
Takashi Iwai97ec5582006-03-21 11:29:07 +01004186 struct hda_codec *codec,
4187 unsigned int stream_tag,
4188 unsigned int format,
4189 struct snd_pcm_substream *substream)
4190{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004191 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004192 int err;
4193
4194 err = snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
4195 stream_tag, format, substream);
4196 if (!err)
4197 call_pcm_playback_hook(hinfo, codec, substream,
4198 HDA_GEN_PCM_ACT_PREPARE);
4199 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004200}
Takashi Iwai97ec5582006-03-21 11:29:07 +01004201
Takashi Iwai352f7f92012-12-19 12:52:06 +01004202static int playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
4203 struct hda_codec *codec,
4204 struct snd_pcm_substream *substream)
4205{
4206 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004207 int err;
4208
4209 err = snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
4210 if (!err)
4211 call_pcm_playback_hook(hinfo, codec, substream,
4212 HDA_GEN_PCM_ACT_CLEANUP);
4213 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004214}
4215
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004216static int playback_pcm_close(struct hda_pcm_stream *hinfo,
4217 struct hda_codec *codec,
4218 struct snd_pcm_substream *substream)
4219{
4220 struct hda_gen_spec *spec = codec->spec;
4221 mutex_lock(&spec->pcm_mutex);
4222 spec->active_streams &= ~(1 << STREAM_MULTI_OUT);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004223 call_pcm_playback_hook(hinfo, codec, substream,
4224 HDA_GEN_PCM_ACT_CLOSE);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004225 mutex_unlock(&spec->pcm_mutex);
4226 return 0;
4227}
4228
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004229static int capture_pcm_open(struct hda_pcm_stream *hinfo,
4230 struct hda_codec *codec,
4231 struct snd_pcm_substream *substream)
4232{
4233 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_OPEN);
4234 return 0;
4235}
4236
4237static int capture_pcm_prepare(struct hda_pcm_stream *hinfo,
4238 struct hda_codec *codec,
4239 unsigned int stream_tag,
4240 unsigned int format,
4241 struct snd_pcm_substream *substream)
4242{
4243 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4244 call_pcm_capture_hook(hinfo, codec, substream,
4245 HDA_GEN_PCM_ACT_PREPARE);
4246 return 0;
4247}
4248
4249static int capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
4250 struct hda_codec *codec,
4251 struct snd_pcm_substream *substream)
4252{
4253 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
4254 call_pcm_capture_hook(hinfo, codec, substream,
4255 HDA_GEN_PCM_ACT_CLEANUP);
4256 return 0;
4257}
4258
4259static int capture_pcm_close(struct hda_pcm_stream *hinfo,
4260 struct hda_codec *codec,
4261 struct snd_pcm_substream *substream)
4262{
4263 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_CLOSE);
4264 return 0;
4265}
4266
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004267static int alt_playback_pcm_open(struct hda_pcm_stream *hinfo,
4268 struct hda_codec *codec,
4269 struct snd_pcm_substream *substream)
4270{
4271 struct hda_gen_spec *spec = codec->spec;
4272 int err = 0;
4273
4274 mutex_lock(&spec->pcm_mutex);
4275 if (!spec->indep_hp_enabled)
4276 err = -EBUSY;
4277 else
4278 spec->active_streams |= 1 << STREAM_INDEP_HP;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004279 call_pcm_playback_hook(hinfo, codec, substream,
4280 HDA_GEN_PCM_ACT_OPEN);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004281 mutex_unlock(&spec->pcm_mutex);
4282 return err;
4283}
4284
4285static int alt_playback_pcm_close(struct hda_pcm_stream *hinfo,
4286 struct hda_codec *codec,
4287 struct snd_pcm_substream *substream)
4288{
4289 struct hda_gen_spec *spec = codec->spec;
4290 mutex_lock(&spec->pcm_mutex);
4291 spec->active_streams &= ~(1 << STREAM_INDEP_HP);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004292 call_pcm_playback_hook(hinfo, codec, substream,
4293 HDA_GEN_PCM_ACT_CLOSE);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004294 mutex_unlock(&spec->pcm_mutex);
4295 return 0;
4296}
4297
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004298static int alt_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
4299 struct hda_codec *codec,
4300 unsigned int stream_tag,
4301 unsigned int format,
4302 struct snd_pcm_substream *substream)
4303{
4304 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4305 call_pcm_playback_hook(hinfo, codec, substream,
4306 HDA_GEN_PCM_ACT_PREPARE);
4307 return 0;
4308}
4309
4310static int alt_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
4311 struct hda_codec *codec,
4312 struct snd_pcm_substream *substream)
4313{
4314 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
4315 call_pcm_playback_hook(hinfo, codec, substream,
4316 HDA_GEN_PCM_ACT_CLEANUP);
4317 return 0;
4318}
4319
Takashi Iwai352f7f92012-12-19 12:52:06 +01004320/*
4321 * Digital out
4322 */
4323static int dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
4324 struct hda_codec *codec,
4325 struct snd_pcm_substream *substream)
4326{
4327 struct hda_gen_spec *spec = codec->spec;
4328 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
4329}
4330
4331static int dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
4332 struct hda_codec *codec,
4333 unsigned int stream_tag,
4334 unsigned int format,
4335 struct snd_pcm_substream *substream)
4336{
4337 struct hda_gen_spec *spec = codec->spec;
4338 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
4339 stream_tag, format, substream);
4340}
4341
4342static int dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
4343 struct hda_codec *codec,
4344 struct snd_pcm_substream *substream)
4345{
4346 struct hda_gen_spec *spec = codec->spec;
4347 return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
4348}
4349
4350static int dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
4351 struct hda_codec *codec,
4352 struct snd_pcm_substream *substream)
4353{
4354 struct hda_gen_spec *spec = codec->spec;
4355 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
4356}
4357
4358/*
4359 * Analog capture
4360 */
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004361#define alt_capture_pcm_open capture_pcm_open
4362#define alt_capture_pcm_close capture_pcm_close
4363
Takashi Iwai352f7f92012-12-19 12:52:06 +01004364static int alt_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
4365 struct hda_codec *codec,
4366 unsigned int stream_tag,
4367 unsigned int format,
4368 struct snd_pcm_substream *substream)
4369{
4370 struct hda_gen_spec *spec = codec->spec;
4371
4372 snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number + 1],
Takashi Iwai97ec5582006-03-21 11:29:07 +01004373 stream_tag, 0, format);
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004374 call_pcm_capture_hook(hinfo, codec, substream,
4375 HDA_GEN_PCM_ACT_PREPARE);
Takashi Iwai97ec5582006-03-21 11:29:07 +01004376 return 0;
4377}
4378
Takashi Iwai352f7f92012-12-19 12:52:06 +01004379static int alt_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
4380 struct hda_codec *codec,
4381 struct snd_pcm_substream *substream)
Takashi Iwai97ec5582006-03-21 11:29:07 +01004382{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004383 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai97ec5582006-03-21 11:29:07 +01004384
Takashi Iwai352f7f92012-12-19 12:52:06 +01004385 snd_hda_codec_cleanup_stream(codec,
4386 spec->adc_nids[substream->number + 1]);
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004387 call_pcm_capture_hook(hinfo, codec, substream,
4388 HDA_GEN_PCM_ACT_CLEANUP);
Takashi Iwai97ec5582006-03-21 11:29:07 +01004389 return 0;
4390}
4391
Takashi Iwai352f7f92012-12-19 12:52:06 +01004392/*
4393 */
4394static const struct hda_pcm_stream pcm_analog_playback = {
4395 .substreams = 1,
4396 .channels_min = 2,
4397 .channels_max = 8,
4398 /* NID is set in build_pcms */
4399 .ops = {
4400 .open = playback_pcm_open,
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004401 .close = playback_pcm_close,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004402 .prepare = playback_pcm_prepare,
4403 .cleanup = playback_pcm_cleanup
4404 },
4405};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004406
Takashi Iwai352f7f92012-12-19 12:52:06 +01004407static const struct hda_pcm_stream pcm_analog_capture = {
4408 .substreams = 1,
4409 .channels_min = 2,
4410 .channels_max = 2,
4411 /* NID is set in build_pcms */
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004412 .ops = {
4413 .open = capture_pcm_open,
4414 .close = capture_pcm_close,
4415 .prepare = capture_pcm_prepare,
4416 .cleanup = capture_pcm_cleanup
4417 },
Takashi Iwai352f7f92012-12-19 12:52:06 +01004418};
4419
4420static const struct hda_pcm_stream pcm_analog_alt_playback = {
4421 .substreams = 1,
4422 .channels_min = 2,
4423 .channels_max = 2,
4424 /* NID is set in build_pcms */
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004425 .ops = {
4426 .open = alt_playback_pcm_open,
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004427 .close = alt_playback_pcm_close,
4428 .prepare = alt_playback_pcm_prepare,
4429 .cleanup = alt_playback_pcm_cleanup
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004430 },
Takashi Iwai352f7f92012-12-19 12:52:06 +01004431};
4432
4433static const struct hda_pcm_stream pcm_analog_alt_capture = {
4434 .substreams = 2, /* can be overridden */
4435 .channels_min = 2,
4436 .channels_max = 2,
4437 /* NID is set in build_pcms */
4438 .ops = {
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004439 .open = alt_capture_pcm_open,
4440 .close = alt_capture_pcm_close,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004441 .prepare = alt_capture_pcm_prepare,
4442 .cleanup = alt_capture_pcm_cleanup
4443 },
4444};
4445
4446static const struct hda_pcm_stream pcm_digital_playback = {
4447 .substreams = 1,
4448 .channels_min = 2,
4449 .channels_max = 2,
4450 /* NID is set in build_pcms */
4451 .ops = {
4452 .open = dig_playback_pcm_open,
4453 .close = dig_playback_pcm_close,
4454 .prepare = dig_playback_pcm_prepare,
4455 .cleanup = dig_playback_pcm_cleanup
4456 },
4457};
4458
4459static const struct hda_pcm_stream pcm_digital_capture = {
4460 .substreams = 1,
4461 .channels_min = 2,
4462 .channels_max = 2,
4463 /* NID is set in build_pcms */
4464};
4465
4466/* Used by build_pcms to flag that a PCM has no playback stream */
4467static const struct hda_pcm_stream pcm_null_stream = {
4468 .substreams = 0,
4469 .channels_min = 0,
4470 .channels_max = 0,
4471};
4472
4473/*
4474 * dynamic changing ADC PCM streams
4475 */
4476static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur)
4477{
4478 struct hda_gen_spec *spec = codec->spec;
4479 hda_nid_t new_adc = spec->adc_nids[spec->dyn_adc_idx[cur]];
4480
4481 if (spec->cur_adc && spec->cur_adc != new_adc) {
4482 /* stream is running, let's swap the current ADC */
4483 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
4484 spec->cur_adc = new_adc;
4485 snd_hda_codec_setup_stream(codec, new_adc,
4486 spec->cur_adc_stream_tag, 0,
4487 spec->cur_adc_format);
4488 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004489 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01004490 return false;
4491}
4492
4493/* analog capture with dynamic dual-adc changes */
4494static int dyn_adc_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
4495 struct hda_codec *codec,
4496 unsigned int stream_tag,
4497 unsigned int format,
4498 struct snd_pcm_substream *substream)
4499{
4500 struct hda_gen_spec *spec = codec->spec;
4501 spec->cur_adc = spec->adc_nids[spec->dyn_adc_idx[spec->cur_mux[0]]];
4502 spec->cur_adc_stream_tag = stream_tag;
4503 spec->cur_adc_format = format;
4504 snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
4505 return 0;
4506}
4507
4508static int dyn_adc_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
4509 struct hda_codec *codec,
4510 struct snd_pcm_substream *substream)
4511{
4512 struct hda_gen_spec *spec = codec->spec;
4513 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
4514 spec->cur_adc = 0;
4515 return 0;
4516}
4517
4518static const struct hda_pcm_stream dyn_adc_pcm_analog_capture = {
4519 .substreams = 1,
4520 .channels_min = 2,
4521 .channels_max = 2,
4522 .nid = 0, /* fill later */
4523 .ops = {
4524 .prepare = dyn_adc_capture_pcm_prepare,
4525 .cleanup = dyn_adc_capture_pcm_cleanup
4526 },
4527};
4528
Takashi Iwaif873e532012-12-20 16:58:39 +01004529static void fill_pcm_stream_name(char *str, size_t len, const char *sfx,
4530 const char *chip_name)
4531{
4532 char *p;
4533
4534 if (*str)
4535 return;
4536 strlcpy(str, chip_name, len);
4537
4538 /* drop non-alnum chars after a space */
4539 for (p = strchr(str, ' '); p; p = strchr(p + 1, ' ')) {
4540 if (!isalnum(p[1])) {
4541 *p = 0;
4542 break;
4543 }
4544 }
4545 strlcat(str, sfx, len);
4546}
4547
Takashi Iwai352f7f92012-12-19 12:52:06 +01004548/* build PCM streams based on the parsed results */
4549int snd_hda_gen_build_pcms(struct hda_codec *codec)
4550{
4551 struct hda_gen_spec *spec = codec->spec;
4552 struct hda_pcm *info = spec->pcm_rec;
4553 const struct hda_pcm_stream *p;
4554 bool have_multi_adcs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004555
4556 codec->num_pcms = 1;
4557 codec->pcm_info = info;
4558
Takashi Iwai352f7f92012-12-19 12:52:06 +01004559 if (spec->no_analog)
4560 goto skip_analog;
4561
Takashi Iwaif873e532012-12-20 16:58:39 +01004562 fill_pcm_stream_name(spec->stream_name_analog,
4563 sizeof(spec->stream_name_analog),
4564 " Analog", codec->chip_name);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004565 info->name = spec->stream_name_analog;
4566
4567 if (spec->multiout.num_dacs > 0) {
4568 p = spec->stream_analog_playback;
4569 if (!p)
4570 p = &pcm_analog_playback;
4571 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
4572 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dac_nids[0];
4573 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
4574 spec->multiout.max_channels;
4575 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT &&
4576 spec->autocfg.line_outs == 2)
4577 info->stream[SNDRV_PCM_STREAM_PLAYBACK].chmap =
4578 snd_pcm_2_1_chmaps;
4579 }
4580 if (spec->num_adc_nids) {
4581 p = spec->stream_analog_capture;
4582 if (!p) {
4583 if (spec->dyn_adc_switch)
4584 p = &dyn_adc_pcm_analog_capture;
4585 else
4586 p = &pcm_analog_capture;
4587 }
4588 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
4589 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adc_nids[0];
4590 }
4591
Takashi Iwai352f7f92012-12-19 12:52:06 +01004592 skip_analog:
4593 /* SPDIF for stream index #1 */
4594 if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
Takashi Iwaif873e532012-12-20 16:58:39 +01004595 fill_pcm_stream_name(spec->stream_name_digital,
4596 sizeof(spec->stream_name_digital),
4597 " Digital", codec->chip_name);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004598 codec->num_pcms = 2;
4599 codec->slave_dig_outs = spec->multiout.slave_dig_outs;
4600 info = spec->pcm_rec + 1;
4601 info->name = spec->stream_name_digital;
4602 if (spec->dig_out_type)
4603 info->pcm_type = spec->dig_out_type;
4604 else
4605 info->pcm_type = HDA_PCM_TYPE_SPDIF;
4606 if (spec->multiout.dig_out_nid) {
4607 p = spec->stream_digital_playback;
4608 if (!p)
4609 p = &pcm_digital_playback;
4610 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
4611 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dig_out_nid;
4612 }
4613 if (spec->dig_in_nid) {
4614 p = spec->stream_digital_capture;
4615 if (!p)
4616 p = &pcm_digital_capture;
4617 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
4618 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in_nid;
4619 }
4620 }
4621
4622 if (spec->no_analog)
4623 return 0;
4624
4625 /* If the use of more than one ADC is requested for the current
4626 * model, configure a second analog capture-only PCM.
4627 */
4628 have_multi_adcs = (spec->num_adc_nids > 1) &&
4629 !spec->dyn_adc_switch && !spec->auto_mic;
4630 /* Additional Analaog capture for index #2 */
4631 if (spec->alt_dac_nid || have_multi_adcs) {
Takashi Iwaia6071482013-01-21 16:50:09 +01004632 fill_pcm_stream_name(spec->stream_name_alt_analog,
4633 sizeof(spec->stream_name_alt_analog),
4634 " Alt Analog", codec->chip_name);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004635 codec->num_pcms = 3;
4636 info = spec->pcm_rec + 2;
Takashi Iwaia6071482013-01-21 16:50:09 +01004637 info->name = spec->stream_name_alt_analog;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004638 if (spec->alt_dac_nid) {
4639 p = spec->stream_analog_alt_playback;
4640 if (!p)
4641 p = &pcm_analog_alt_playback;
4642 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
4643 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
4644 spec->alt_dac_nid;
4645 } else {
4646 info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
4647 pcm_null_stream;
4648 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = 0;
4649 }
4650 if (have_multi_adcs) {
4651 p = spec->stream_analog_alt_capture;
4652 if (!p)
4653 p = &pcm_analog_alt_capture;
4654 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
4655 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
4656 spec->adc_nids[1];
4657 info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams =
4658 spec->num_adc_nids - 1;
4659 } else {
4660 info->stream[SNDRV_PCM_STREAM_CAPTURE] =
4661 pcm_null_stream;
4662 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = 0;
4663 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004664 }
4665
4666 return 0;
4667}
Takashi Iwai352f7f92012-12-19 12:52:06 +01004668EXPORT_SYMBOL_HDA(snd_hda_gen_build_pcms);
4669
4670
4671/*
4672 * Standard auto-parser initializations
4673 */
4674
Takashi Iwaid4156932013-01-07 10:08:02 +01004675/* configure the given path as a proper output */
Takashi Iwai2c12c302013-01-10 09:33:29 +01004676static void set_output_and_unmute(struct hda_codec *codec, int path_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004677{
4678 struct nid_path *path;
Takashi Iwaid4156932013-01-07 10:08:02 +01004679 hda_nid_t pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004680
Takashi Iwai196c17662013-01-04 15:01:40 +01004681 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwaid4156932013-01-07 10:08:02 +01004682 if (!path || !path->depth)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004683 return;
Takashi Iwaid4156932013-01-07 10:08:02 +01004684 pin = path->path[path->depth - 1];
Takashi Iwai2c12c302013-01-10 09:33:29 +01004685 restore_pin_ctl(codec, pin);
Takashi Iwaie1284af2013-01-03 16:33:02 +01004686 snd_hda_activate_path(codec, path, path->active, true);
4687 set_pin_eapd(codec, pin, path->active);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004688}
4689
4690/* initialize primary output paths */
4691static void init_multi_out(struct hda_codec *codec)
4692{
4693 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004694 int i;
4695
Takashi Iwaid4156932013-01-07 10:08:02 +01004696 for (i = 0; i < spec->autocfg.line_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01004697 set_output_and_unmute(codec, spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004698}
4699
Takashi Iwaidb23fd12012-12-20 15:27:24 +01004700
Takashi Iwai2c12c302013-01-10 09:33:29 +01004701static void __init_extra_out(struct hda_codec *codec, int num_outs, int *paths)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004702{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004703 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004704
Takashi Iwaid4156932013-01-07 10:08:02 +01004705 for (i = 0; i < num_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01004706 set_output_and_unmute(codec, paths[i]);
Takashi Iwaidb23fd12012-12-20 15:27:24 +01004707}
4708
4709/* initialize hp and speaker paths */
4710static void init_extra_out(struct hda_codec *codec)
4711{
4712 struct hda_gen_spec *spec = codec->spec;
4713
4714 if (spec->autocfg.line_out_type != AUTO_PIN_HP_OUT)
Takashi Iwai2c12c302013-01-10 09:33:29 +01004715 __init_extra_out(codec, spec->autocfg.hp_outs, spec->hp_paths);
Takashi Iwaidb23fd12012-12-20 15:27:24 +01004716 if (spec->autocfg.line_out_type != AUTO_PIN_SPEAKER_OUT)
4717 __init_extra_out(codec, spec->autocfg.speaker_outs,
Takashi Iwai2c12c302013-01-10 09:33:29 +01004718 spec->speaker_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004719}
4720
4721/* initialize multi-io paths */
4722static void init_multi_io(struct hda_codec *codec)
4723{
4724 struct hda_gen_spec *spec = codec->spec;
4725 int i;
4726
4727 for (i = 0; i < spec->multi_ios; i++) {
4728 hda_nid_t pin = spec->multi_io[i].pin;
4729 struct nid_path *path;
Takashi Iwai196c17662013-01-04 15:01:40 +01004730 path = get_multiio_path(codec, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004731 if (!path)
4732 continue;
4733 if (!spec->multi_io[i].ctl_in)
4734 spec->multi_io[i].ctl_in =
Takashi Iwai2c12c302013-01-10 09:33:29 +01004735 snd_hda_codec_get_pin_target(codec, pin);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004736 snd_hda_activate_path(codec, path, path->active, true);
4737 }
4738}
4739
Takashi Iwai352f7f92012-12-19 12:52:06 +01004740/* set up input pins and loopback paths */
4741static void init_analog_input(struct hda_codec *codec)
4742{
4743 struct hda_gen_spec *spec = codec->spec;
4744 struct auto_pin_cfg *cfg = &spec->autocfg;
4745 int i;
4746
4747 for (i = 0; i < cfg->num_inputs; i++) {
4748 hda_nid_t nid = cfg->inputs[i].pin;
4749 if (is_input_pin(codec, nid))
Takashi Iwai2c12c302013-01-10 09:33:29 +01004750 restore_pin_ctl(codec, nid);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004751
4752 /* init loopback inputs */
4753 if (spec->mixer_nid) {
Takashi Iwai3e367f12013-01-23 17:07:23 +01004754 resume_path_from_idx(codec, spec->loopback_paths[i]);
4755 resume_path_from_idx(codec, spec->loopback_merge_path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004756 }
4757 }
4758}
4759
4760/* initialize ADC paths */
4761static void init_input_src(struct hda_codec *codec)
4762{
4763 struct hda_gen_spec *spec = codec->spec;
4764 struct hda_input_mux *imux = &spec->input_mux;
4765 struct nid_path *path;
4766 int i, c, nums;
4767
4768 if (spec->dyn_adc_switch)
4769 nums = 1;
4770 else
4771 nums = spec->num_adc_nids;
4772
4773 for (c = 0; c < nums; c++) {
4774 for (i = 0; i < imux->num_items; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01004775 path = get_input_path(codec, c, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004776 if (path) {
4777 bool active = path->active;
4778 if (i == spec->cur_mux[c])
4779 active = true;
4780 snd_hda_activate_path(codec, path, active, false);
4781 }
4782 }
4783 }
4784
4785 if (spec->shared_mic_hp)
4786 update_shared_mic_hp(codec, spec->cur_mux[0]);
4787
4788 if (spec->cap_sync_hook)
Takashi Iwaia90229e2013-01-18 14:10:00 +01004789 spec->cap_sync_hook(codec, NULL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004790}
4791
4792/* set right pin controls for digital I/O */
4793static void init_digital(struct hda_codec *codec)
4794{
4795 struct hda_gen_spec *spec = codec->spec;
4796 int i;
4797 hda_nid_t pin;
4798
Takashi Iwaid4156932013-01-07 10:08:02 +01004799 for (i = 0; i < spec->autocfg.dig_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01004800 set_output_and_unmute(codec, spec->digout_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004801 pin = spec->autocfg.dig_in_pin;
Takashi Iwai2430d7b2013-01-04 15:09:42 +01004802 if (pin) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01004803 restore_pin_ctl(codec, pin);
Takashi Iwai3e367f12013-01-23 17:07:23 +01004804 resume_path_from_idx(codec, spec->digin_path);
Takashi Iwai2430d7b2013-01-04 15:09:42 +01004805 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01004806}
4807
Takashi Iwai973e4972012-12-20 15:16:09 +01004808/* clear unsol-event tags on unused pins; Conexant codecs seem to leave
4809 * invalid unsol tags by some reason
4810 */
4811static void clear_unsol_on_unused_pins(struct hda_codec *codec)
4812{
4813 int i;
4814
4815 for (i = 0; i < codec->init_pins.used; i++) {
4816 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
4817 hda_nid_t nid = pin->nid;
4818 if (is_jack_detectable(codec, nid) &&
4819 !snd_hda_jack_tbl_get(codec, nid))
4820 snd_hda_codec_update_cache(codec, nid, 0,
4821 AC_VERB_SET_UNSOLICITED_ENABLE, 0);
4822 }
4823}
4824
Takashi Iwai5187ac12013-01-07 12:52:16 +01004825/*
4826 * initialize the generic spec;
4827 * this can be put as patch_ops.init function
4828 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01004829int snd_hda_gen_init(struct hda_codec *codec)
4830{
4831 struct hda_gen_spec *spec = codec->spec;
4832
4833 if (spec->init_hook)
4834 spec->init_hook(codec);
4835
4836 snd_hda_apply_verbs(codec);
4837
Takashi Iwai3bbcd272012-12-20 11:50:58 +01004838 codec->cached_write = 1;
4839
Takashi Iwai352f7f92012-12-19 12:52:06 +01004840 init_multi_out(codec);
4841 init_extra_out(codec);
4842 init_multi_io(codec);
4843 init_analog_input(codec);
4844 init_input_src(codec);
4845 init_digital(codec);
4846
Takashi Iwai973e4972012-12-20 15:16:09 +01004847 clear_unsol_on_unused_pins(codec);
4848
Takashi Iwai352f7f92012-12-19 12:52:06 +01004849 /* call init functions of standard auto-mute helpers */
Takashi Iwaia5cc2502013-01-16 18:08:55 +01004850 update_automute_all(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004851
Takashi Iwaidc870f32013-01-22 15:24:30 +01004852 snd_hda_codec_flush_cache(codec);
Takashi Iwai3bbcd272012-12-20 11:50:58 +01004853
Takashi Iwai352f7f92012-12-19 12:52:06 +01004854 if (spec->vmaster_mute.sw_kctl && spec->vmaster_mute.hook)
4855 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
4856
4857 hda_call_check_power_status(codec, 0x01);
4858 return 0;
4859}
Takashi Iwaifce52a32013-01-07 12:42:48 +01004860EXPORT_SYMBOL_HDA(snd_hda_gen_init);
4861
Takashi Iwai5187ac12013-01-07 12:52:16 +01004862/*
4863 * free the generic spec;
4864 * this can be put as patch_ops.free function
4865 */
Takashi Iwaifce52a32013-01-07 12:42:48 +01004866void snd_hda_gen_free(struct hda_codec *codec)
4867{
4868 snd_hda_gen_spec_free(codec->spec);
4869 kfree(codec->spec);
4870 codec->spec = NULL;
4871}
4872EXPORT_SYMBOL_HDA(snd_hda_gen_free);
4873
4874#ifdef CONFIG_PM
Takashi Iwai5187ac12013-01-07 12:52:16 +01004875/*
4876 * check the loopback power save state;
4877 * this can be put as patch_ops.check_power_status function
4878 */
Takashi Iwaifce52a32013-01-07 12:42:48 +01004879int snd_hda_gen_check_power_status(struct hda_codec *codec, hda_nid_t nid)
4880{
4881 struct hda_gen_spec *spec = codec->spec;
4882 return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
4883}
4884EXPORT_SYMBOL_HDA(snd_hda_gen_check_power_status);
4885#endif
Takashi Iwai352f7f92012-12-19 12:52:06 +01004886
4887
4888/*
4889 * the generic codec support
4890 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004891
Takashi Iwai352f7f92012-12-19 12:52:06 +01004892static const struct hda_codec_ops generic_patch_ops = {
4893 .build_controls = snd_hda_gen_build_controls,
4894 .build_pcms = snd_hda_gen_build_pcms,
4895 .init = snd_hda_gen_init,
Takashi Iwaifce52a32013-01-07 12:42:48 +01004896 .free = snd_hda_gen_free,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004897 .unsol_event = snd_hda_jack_unsol_event,
Takashi Iwai83012a72012-08-24 18:38:08 +02004898#ifdef CONFIG_PM
Takashi Iwaifce52a32013-01-07 12:42:48 +01004899 .check_power_status = snd_hda_gen_check_power_status,
Takashi Iwaicb53c622007-08-10 17:21:45 +02004900#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004901};
4902
Linus Torvalds1da177e2005-04-16 15:20:36 -07004903int snd_hda_parse_generic_codec(struct hda_codec *codec)
4904{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004905 struct hda_gen_spec *spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004906 int err;
4907
Takashi Iwaie560d8d2005-09-09 14:21:46 +02004908 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004909 if (!spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004910 return -ENOMEM;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004911 snd_hda_gen_spec_init(spec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004912 codec->spec = spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004913
Takashi Iwai9eb413e2012-12-19 14:41:21 +01004914 err = snd_hda_parse_pin_defcfg(codec, &spec->autocfg, NULL, 0);
4915 if (err < 0)
4916 return err;
4917
4918 err = snd_hda_gen_parse_auto_config(codec, &spec->autocfg);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004919 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004920 goto error;
4921
4922 codec->patch_ops = generic_patch_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004923 return 0;
4924
Takashi Iwai352f7f92012-12-19 12:52:06 +01004925error:
Takashi Iwaifce52a32013-01-07 12:42:48 +01004926 snd_hda_gen_free(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004927 return err;
4928}
Takashi Iwaifce52a32013-01-07 12:42:48 +01004929EXPORT_SYMBOL_HDA(snd_hda_parse_generic_codec);