blob: 2dbe767be16bba7e84a3f3219ea1e9e98f52ef84 [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;
Jiri Slaby868211d2013-04-04 22:32:10 +0200743 bool changed = false;
Takashi Iwai55196ff2013-01-24 17:32:56 +0100744 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 */
Takashi Iwai1d739062013-02-13 14:17:32 +0100991 BAD_MULTI_IO = 0x120,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100992 /* 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,
Takashi Iwai55a63d42013-03-21 17:20:12 +0100998 /* No independent HP possible */
999 BAD_NO_INDEP_HP = 0x40,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001000 /* Primary DAC shared with main CLFE */
1001 BAD_SHARED_CLFE = 0x10,
1002 /* Primary DAC shared with extra surrounds */
1003 BAD_SHARED_EXTRA_SURROUND = 0x10,
1004 /* Volume widget is shared */
1005 BAD_SHARED_VOL = 0x10,
1006};
1007
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001008/* look for widgets in the given path which are appropriate for
Takashi Iwai352f7f92012-12-19 12:52:06 +01001009 * volume and mute controls, and assign the values to ctls[].
1010 *
1011 * When no appropriate widget is found in the path, the badness value
1012 * is incremented depending on the situation. The function returns the
1013 * total badness for both volume and mute controls.
1014 */
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001015static int assign_out_path_ctls(struct hda_codec *codec, struct nid_path *path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001016{
Takashi Iwai352f7f92012-12-19 12:52:06 +01001017 hda_nid_t nid;
1018 unsigned int val;
1019 int badness = 0;
1020
1021 if (!path)
1022 return BAD_SHARED_VOL * 2;
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001023
1024 if (path->ctls[NID_PATH_VOL_CTL] ||
1025 path->ctls[NID_PATH_MUTE_CTL])
1026 return 0; /* already evaluated */
1027
Takashi Iwai352f7f92012-12-19 12:52:06 +01001028 nid = look_for_out_vol_nid(codec, path);
1029 if (nid) {
1030 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
1031 if (is_ctl_used(codec, val, NID_PATH_VOL_CTL))
1032 badness += BAD_SHARED_VOL;
1033 else
1034 path->ctls[NID_PATH_VOL_CTL] = val;
1035 } else
1036 badness += BAD_SHARED_VOL;
1037 nid = look_for_out_mute_nid(codec, path);
1038 if (nid) {
1039 unsigned int wid_type = get_wcaps_type(get_wcaps(codec, nid));
1040 if (wid_type == AC_WID_PIN || wid_type == AC_WID_AUD_OUT ||
1041 nid_has_mute(codec, nid, HDA_OUTPUT))
1042 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
1043 else
1044 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT);
1045 if (is_ctl_used(codec, val, NID_PATH_MUTE_CTL))
1046 badness += BAD_SHARED_VOL;
1047 else
1048 path->ctls[NID_PATH_MUTE_CTL] = val;
1049 } else
1050 badness += BAD_SHARED_VOL;
1051 return badness;
1052}
1053
1054struct badness_table {
1055 int no_primary_dac; /* no primary DAC */
1056 int no_dac; /* no secondary DACs */
1057 int shared_primary; /* primary DAC is shared with main output */
1058 int shared_surr; /* secondary DAC shared with main or primary */
1059 int shared_clfe; /* third DAC shared with main or primary */
1060 int shared_surr_main; /* secondary DAC sahred with main/DAC0 */
1061};
1062
1063static struct badness_table main_out_badness = {
1064 .no_primary_dac = BAD_NO_PRIMARY_DAC,
1065 .no_dac = BAD_NO_DAC,
1066 .shared_primary = BAD_NO_PRIMARY_DAC,
1067 .shared_surr = BAD_SHARED_SURROUND,
1068 .shared_clfe = BAD_SHARED_CLFE,
1069 .shared_surr_main = BAD_SHARED_SURROUND,
1070};
1071
1072static struct badness_table extra_out_badness = {
1073 .no_primary_dac = BAD_NO_DAC,
1074 .no_dac = BAD_NO_DAC,
1075 .shared_primary = BAD_NO_EXTRA_DAC,
1076 .shared_surr = BAD_SHARED_EXTRA_SURROUND,
1077 .shared_clfe = BAD_SHARED_EXTRA_SURROUND,
1078 .shared_surr_main = BAD_NO_EXTRA_SURR_DAC,
1079};
1080
Takashi Iwai7385df62013-01-07 09:50:52 +01001081/* get the DAC of the primary output corresponding to the given array index */
1082static hda_nid_t get_primary_out(struct hda_codec *codec, int idx)
1083{
1084 struct hda_gen_spec *spec = codec->spec;
1085 struct auto_pin_cfg *cfg = &spec->autocfg;
1086
1087 if (cfg->line_outs > idx)
1088 return spec->private_dac_nids[idx];
1089 idx -= cfg->line_outs;
1090 if (spec->multi_ios > idx)
1091 return spec->multi_io[idx].dac;
1092 return 0;
1093}
1094
1095/* return the DAC if it's reachable, otherwise zero */
1096static inline hda_nid_t try_dac(struct hda_codec *codec,
1097 hda_nid_t dac, hda_nid_t pin)
1098{
1099 return is_reachable_path(codec, dac, pin) ? dac : 0;
1100}
1101
Takashi Iwai352f7f92012-12-19 12:52:06 +01001102/* try to assign DACs to pins and return the resultant badness */
1103static int try_assign_dacs(struct hda_codec *codec, int num_outs,
1104 const hda_nid_t *pins, hda_nid_t *dacs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001105 int *path_idx,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001106 const struct badness_table *bad)
1107{
1108 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001109 int i, j;
1110 int badness = 0;
1111 hda_nid_t dac;
1112
1113 if (!num_outs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 return 0;
1115
Takashi Iwai352f7f92012-12-19 12:52:06 +01001116 for (i = 0; i < num_outs; i++) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001117 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001118 hda_nid_t pin = pins[i];
Takashi Iwai1e0b5282013-01-04 12:56:52 +01001119
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001120 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
1121 if (path) {
1122 badness += assign_out_path_ctls(codec, path);
Takashi Iwai1e0b5282013-01-04 12:56:52 +01001123 continue;
1124 }
1125
1126 dacs[i] = look_for_dac(codec, pin, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001127 if (!dacs[i] && !i) {
Takashi Iwai980428c2013-01-09 09:28:20 +01001128 /* try to steal the DAC of surrounds for the front */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001129 for (j = 1; j < num_outs; j++) {
1130 if (is_reachable_path(codec, dacs[j], pin)) {
1131 dacs[0] = dacs[j];
1132 dacs[j] = 0;
Takashi Iwai980428c2013-01-09 09:28:20 +01001133 invalidate_nid_path(codec, path_idx[j]);
Takashi Iwai196c17662013-01-04 15:01:40 +01001134 path_idx[j] = 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001135 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 }
1137 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001138 }
1139 dac = dacs[i];
1140 if (!dac) {
Takashi Iwai7385df62013-01-07 09:50:52 +01001141 if (num_outs > 2)
1142 dac = try_dac(codec, get_primary_out(codec, i), pin);
1143 if (!dac)
1144 dac = try_dac(codec, dacs[0], pin);
1145 if (!dac)
1146 dac = try_dac(codec, get_primary_out(codec, i), pin);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001147 if (dac) {
1148 if (!i)
1149 badness += bad->shared_primary;
1150 else if (i == 1)
1151 badness += bad->shared_surr;
1152 else
1153 badness += bad->shared_clfe;
1154 } else if (is_reachable_path(codec, spec->private_dac_nids[0], pin)) {
1155 dac = spec->private_dac_nids[0];
1156 badness += bad->shared_surr_main;
1157 } else if (!i)
1158 badness += bad->no_primary_dac;
1159 else
1160 badness += bad->no_dac;
1161 }
Takashi Iwai1fa335b2013-01-21 11:43:19 +01001162 if (!dac)
1163 continue;
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001164 path = snd_hda_add_new_path(codec, dac, pin, -spec->mixer_nid);
Takashi Iwai117688a2013-01-04 15:41:41 +01001165 if (!path && !i && spec->mixer_nid) {
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001166 /* try with aamix */
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001167 path = snd_hda_add_new_path(codec, dac, pin, 0);
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001168 }
Takashi Iwai1fa335b2013-01-21 11:43:19 +01001169 if (!path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001170 dac = dacs[i] = 0;
Takashi Iwai1fa335b2013-01-21 11:43:19 +01001171 badness += bad->no_dac;
1172 } else {
Takashi Iwaia7694092013-01-21 10:43:18 +01001173 /* print_nid_path("output", path); */
Takashi Iwaie1284af2013-01-03 16:33:02 +01001174 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +01001175 path_idx[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001176 badness += assign_out_path_ctls(codec, path);
Takashi Iwaie1284af2013-01-03 16:33:02 +01001177 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001178 }
1179
1180 return badness;
1181}
1182
1183/* return NID if the given pin has only a single connection to a certain DAC */
1184static hda_nid_t get_dac_if_single(struct hda_codec *codec, hda_nid_t pin)
1185{
1186 struct hda_gen_spec *spec = codec->spec;
1187 int i;
1188 hda_nid_t nid_found = 0;
1189
1190 for (i = 0; i < spec->num_all_dacs; i++) {
1191 hda_nid_t nid = spec->all_dacs[i];
1192 if (!nid || is_dac_already_used(codec, nid))
1193 continue;
1194 if (is_reachable_path(codec, nid, pin)) {
1195 if (nid_found)
1196 return 0;
1197 nid_found = nid;
1198 }
1199 }
1200 return nid_found;
1201}
1202
1203/* check whether the given pin can be a multi-io pin */
1204static bool can_be_multiio_pin(struct hda_codec *codec,
1205 unsigned int location, hda_nid_t nid)
1206{
1207 unsigned int defcfg, caps;
1208
1209 defcfg = snd_hda_codec_get_pincfg(codec, nid);
1210 if (get_defcfg_connect(defcfg) != AC_JACK_PORT_COMPLEX)
1211 return false;
1212 if (location && get_defcfg_location(defcfg) != location)
1213 return false;
1214 caps = snd_hda_query_pin_caps(codec, nid);
1215 if (!(caps & AC_PINCAP_OUT))
1216 return false;
1217 return true;
1218}
1219
Takashi Iwaie22aab72013-01-04 14:50:04 +01001220/* count the number of input pins that are capable to be multi-io */
1221static int count_multiio_pins(struct hda_codec *codec, hda_nid_t reference_pin)
1222{
1223 struct hda_gen_spec *spec = codec->spec;
1224 struct auto_pin_cfg *cfg = &spec->autocfg;
1225 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1226 unsigned int location = get_defcfg_location(defcfg);
1227 int type, i;
1228 int num_pins = 0;
1229
1230 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1231 for (i = 0; i < cfg->num_inputs; i++) {
1232 if (cfg->inputs[i].type != type)
1233 continue;
1234 if (can_be_multiio_pin(codec, location,
1235 cfg->inputs[i].pin))
1236 num_pins++;
1237 }
1238 }
1239 return num_pins;
1240}
1241
Takashi Iwai352f7f92012-12-19 12:52:06 +01001242/*
1243 * multi-io helper
1244 *
1245 * When hardwired is set, try to fill ony hardwired pins, and returns
1246 * zero if any pins are filled, non-zero if nothing found.
1247 * When hardwired is off, try to fill possible input pins, and returns
1248 * the badness value.
1249 */
1250static int fill_multi_ios(struct hda_codec *codec,
1251 hda_nid_t reference_pin,
Takashi Iwaie22aab72013-01-04 14:50:04 +01001252 bool hardwired)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001253{
1254 struct hda_gen_spec *spec = codec->spec;
1255 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwaie22aab72013-01-04 14:50:04 +01001256 int type, i, j, num_pins, old_pins;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001257 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1258 unsigned int location = get_defcfg_location(defcfg);
1259 int badness = 0;
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001260 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001261
1262 old_pins = spec->multi_ios;
1263 if (old_pins >= 2)
1264 goto end_fill;
1265
Takashi Iwaie22aab72013-01-04 14:50:04 +01001266 num_pins = count_multiio_pins(codec, reference_pin);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001267 if (num_pins < 2)
1268 goto end_fill;
1269
Takashi Iwai352f7f92012-12-19 12:52:06 +01001270 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1271 for (i = 0; i < cfg->num_inputs; i++) {
1272 hda_nid_t nid = cfg->inputs[i].pin;
1273 hda_nid_t dac = 0;
1274
1275 if (cfg->inputs[i].type != type)
1276 continue;
1277 if (!can_be_multiio_pin(codec, location, nid))
1278 continue;
1279 for (j = 0; j < spec->multi_ios; j++) {
1280 if (nid == spec->multi_io[j].pin)
1281 break;
1282 }
1283 if (j < spec->multi_ios)
1284 continue;
1285
Takashi Iwai352f7f92012-12-19 12:52:06 +01001286 if (hardwired)
1287 dac = get_dac_if_single(codec, nid);
1288 else if (!dac)
1289 dac = look_for_dac(codec, nid, false);
1290 if (!dac) {
1291 badness++;
1292 continue;
1293 }
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001294 path = snd_hda_add_new_path(codec, dac, nid,
1295 -spec->mixer_nid);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001296 if (!path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001297 badness++;
1298 continue;
1299 }
Takashi Iwaia7694092013-01-21 10:43:18 +01001300 /* print_nid_path("multiio", path); */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001301 spec->multi_io[spec->multi_ios].pin = nid;
1302 spec->multi_io[spec->multi_ios].dac = dac;
Takashi Iwai196c17662013-01-04 15:01:40 +01001303 spec->out_paths[cfg->line_outs + spec->multi_ios] =
1304 snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001305 spec->multi_ios++;
1306 if (spec->multi_ios >= 2)
1307 break;
1308 }
1309 }
1310 end_fill:
1311 if (badness)
1312 badness = BAD_MULTI_IO;
1313 if (old_pins == spec->multi_ios) {
1314 if (hardwired)
1315 return 1; /* nothing found */
1316 else
1317 return badness; /* no badness if nothing found */
1318 }
1319 if (!hardwired && spec->multi_ios < 2) {
1320 /* cancel newly assigned paths */
1321 spec->paths.used -= spec->multi_ios - old_pins;
1322 spec->multi_ios = old_pins;
1323 return badness;
1324 }
1325
1326 /* assign volume and mute controls */
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001327 for (i = old_pins; i < spec->multi_ios; i++) {
1328 path = snd_hda_get_path_from_idx(codec, spec->out_paths[cfg->line_outs + i]);
1329 badness += assign_out_path_ctls(codec, path);
1330 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001331
1332 return badness;
1333}
1334
1335/* map DACs for all pins in the list if they are single connections */
1336static bool map_singles(struct hda_codec *codec, int outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001337 const hda_nid_t *pins, hda_nid_t *dacs, int *path_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001338{
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001339 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001340 int i;
1341 bool found = false;
1342 for (i = 0; i < outs; i++) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001343 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001344 hda_nid_t dac;
1345 if (dacs[i])
1346 continue;
1347 dac = get_dac_if_single(codec, pins[i]);
1348 if (!dac)
1349 continue;
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001350 path = snd_hda_add_new_path(codec, dac, pins[i],
1351 -spec->mixer_nid);
Takashi Iwai117688a2013-01-04 15:41:41 +01001352 if (!path && !i && spec->mixer_nid)
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001353 path = snd_hda_add_new_path(codec, dac, pins[i], 0);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001354 if (path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001355 dacs[i] = dac;
1356 found = true;
Takashi Iwaia7694092013-01-21 10:43:18 +01001357 /* print_nid_path("output", path); */
Takashi Iwaie1284af2013-01-03 16:33:02 +01001358 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +01001359 path_idx[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001360 }
1361 }
1362 return found;
1363}
1364
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001365/* create a new path including aamix if available, and return its index */
1366static int check_aamix_out_path(struct hda_codec *codec, int path_idx)
1367{
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001368 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001369 struct nid_path *path;
Takashi Iwaif87498b2013-01-21 14:24:31 +01001370 hda_nid_t dac, pin;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001371
1372 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001373 if (!path || !path->depth ||
1374 is_nid_contained(path, spec->mixer_nid))
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001375 return 0;
Takashi Iwaif87498b2013-01-21 14:24:31 +01001376 dac = path->path[0];
1377 pin = path->path[path->depth - 1];
1378 path = snd_hda_add_new_path(codec, dac, pin, spec->mixer_nid);
1379 if (!path) {
1380 if (dac != spec->multiout.dac_nids[0])
1381 dac = spec->multiout.dac_nids[0];
1382 else if (spec->multiout.hp_out_nid[0])
1383 dac = spec->multiout.hp_out_nid[0];
1384 else if (spec->multiout.extra_out_nid[0])
1385 dac = spec->multiout.extra_out_nid[0];
1386 if (dac)
1387 path = snd_hda_add_new_path(codec, dac, pin,
1388 spec->mixer_nid);
1389 }
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001390 if (!path)
1391 return 0;
Takashi Iwaia7694092013-01-21 10:43:18 +01001392 /* print_nid_path("output-aamix", path); */
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001393 path->active = false; /* unused as default */
1394 return snd_hda_get_path_idx(codec, path);
1395}
1396
Takashi Iwai55a63d42013-03-21 17:20:12 +01001397/* check whether the independent HP is available with the current config */
1398static bool indep_hp_possible(struct hda_codec *codec)
1399{
1400 struct hda_gen_spec *spec = codec->spec;
1401 struct auto_pin_cfg *cfg = &spec->autocfg;
1402 struct nid_path *path;
1403 int i, idx;
1404
1405 if (cfg->line_out_type == AUTO_PIN_HP_OUT)
1406 idx = spec->out_paths[0];
1407 else
1408 idx = spec->hp_paths[0];
1409 path = snd_hda_get_path_from_idx(codec, idx);
1410 if (!path)
1411 return false;
1412
1413 /* assume no path conflicts unless aamix is involved */
1414 if (!spec->mixer_nid || !is_nid_contained(path, spec->mixer_nid))
1415 return true;
1416
1417 /* check whether output paths contain aamix */
1418 for (i = 0; i < cfg->line_outs; i++) {
1419 if (spec->out_paths[i] == idx)
1420 break;
1421 path = snd_hda_get_path_from_idx(codec, spec->out_paths[i]);
1422 if (path && is_nid_contained(path, spec->mixer_nid))
1423 return false;
1424 }
1425 for (i = 0; i < cfg->speaker_outs; i++) {
1426 path = snd_hda_get_path_from_idx(codec, spec->speaker_paths[i]);
1427 if (path && is_nid_contained(path, spec->mixer_nid))
1428 return false;
1429 }
1430
1431 return true;
1432}
1433
Takashi Iwaia07a9492013-01-07 16:44:06 +01001434/* fill the empty entries in the dac array for speaker/hp with the
1435 * shared dac pointed by the paths
1436 */
1437static void refill_shared_dacs(struct hda_codec *codec, int num_outs,
1438 hda_nid_t *dacs, int *path_idx)
1439{
1440 struct nid_path *path;
1441 int i;
1442
1443 for (i = 0; i < num_outs; i++) {
1444 if (dacs[i])
1445 continue;
1446 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
1447 if (!path)
1448 continue;
1449 dacs[i] = path->path[0];
1450 }
1451}
1452
Takashi Iwai352f7f92012-12-19 12:52:06 +01001453/* fill in the dac_nids table from the parsed pin configuration */
1454static int fill_and_eval_dacs(struct hda_codec *codec,
1455 bool fill_hardwired,
1456 bool fill_mio_first)
1457{
1458 struct hda_gen_spec *spec = codec->spec;
1459 struct auto_pin_cfg *cfg = &spec->autocfg;
1460 int i, err, badness;
1461
1462 /* set num_dacs once to full for look_for_dac() */
1463 spec->multiout.num_dacs = cfg->line_outs;
1464 spec->multiout.dac_nids = spec->private_dac_nids;
1465 memset(spec->private_dac_nids, 0, sizeof(spec->private_dac_nids));
1466 memset(spec->multiout.hp_out_nid, 0, sizeof(spec->multiout.hp_out_nid));
1467 memset(spec->multiout.extra_out_nid, 0, sizeof(spec->multiout.extra_out_nid));
1468 spec->multi_ios = 0;
1469 snd_array_free(&spec->paths);
Takashi Iwaicd5be3f2013-01-07 15:07:00 +01001470
1471 /* clear path indices */
1472 memset(spec->out_paths, 0, sizeof(spec->out_paths));
1473 memset(spec->hp_paths, 0, sizeof(spec->hp_paths));
1474 memset(spec->speaker_paths, 0, sizeof(spec->speaker_paths));
1475 memset(spec->aamix_out_paths, 0, sizeof(spec->aamix_out_paths));
1476 memset(spec->digout_paths, 0, sizeof(spec->digout_paths));
Takashi Iwaic697b712013-01-07 17:09:26 +01001477 memset(spec->input_paths, 0, sizeof(spec->input_paths));
Takashi Iwaicd5be3f2013-01-07 15:07:00 +01001478 memset(spec->loopback_paths, 0, sizeof(spec->loopback_paths));
1479 memset(&spec->digin_path, 0, sizeof(spec->digin_path));
1480
Takashi Iwai352f7f92012-12-19 12:52:06 +01001481 badness = 0;
1482
1483 /* fill hard-wired DACs first */
1484 if (fill_hardwired) {
1485 bool mapped;
1486 do {
1487 mapped = map_singles(codec, cfg->line_outs,
1488 cfg->line_out_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001489 spec->private_dac_nids,
1490 spec->out_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001491 mapped |= map_singles(codec, cfg->hp_outs,
1492 cfg->hp_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001493 spec->multiout.hp_out_nid,
1494 spec->hp_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001495 mapped |= map_singles(codec, cfg->speaker_outs,
1496 cfg->speaker_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001497 spec->multiout.extra_out_nid,
1498 spec->speaker_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001499 if (fill_mio_first && cfg->line_outs == 1 &&
1500 cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
Takashi Iwaie22aab72013-01-04 14:50:04 +01001501 err = fill_multi_ios(codec, cfg->line_out_pins[0], true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001502 if (!err)
1503 mapped = true;
1504 }
1505 } while (mapped);
1506 }
1507
1508 badness += try_assign_dacs(codec, cfg->line_outs, cfg->line_out_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001509 spec->private_dac_nids, spec->out_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001510 &main_out_badness);
1511
Takashi Iwai352f7f92012-12-19 12:52:06 +01001512 if (fill_mio_first &&
1513 cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1514 /* try to fill multi-io first */
Takashi Iwaie22aab72013-01-04 14:50:04 +01001515 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001516 if (err < 0)
1517 return err;
1518 /* we don't count badness at this stage yet */
1519 }
1520
1521 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
1522 err = try_assign_dacs(codec, cfg->hp_outs, cfg->hp_pins,
1523 spec->multiout.hp_out_nid,
Takashi Iwai196c17662013-01-04 15:01:40 +01001524 spec->hp_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001525 &extra_out_badness);
1526 if (err < 0)
1527 return err;
1528 badness += err;
1529 }
1530 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1531 err = try_assign_dacs(codec, cfg->speaker_outs,
1532 cfg->speaker_pins,
1533 spec->multiout.extra_out_nid,
Takashi Iwai196c17662013-01-04 15:01:40 +01001534 spec->speaker_paths,
1535 &extra_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001536 if (err < 0)
1537 return err;
1538 badness += err;
1539 }
1540 if (cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
Takashi Iwaie22aab72013-01-04 14:50:04 +01001541 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001542 if (err < 0)
1543 return err;
1544 badness += err;
1545 }
Takashi Iwaie22aab72013-01-04 14:50:04 +01001546
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001547 if (spec->mixer_nid) {
1548 spec->aamix_out_paths[0] =
1549 check_aamix_out_path(codec, spec->out_paths[0]);
1550 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1551 spec->aamix_out_paths[1] =
1552 check_aamix_out_path(codec, spec->hp_paths[0]);
1553 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1554 spec->aamix_out_paths[2] =
1555 check_aamix_out_path(codec, spec->speaker_paths[0]);
1556 }
1557
Takashi Iwaie22aab72013-01-04 14:50:04 +01001558 if (cfg->hp_outs && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
1559 if (count_multiio_pins(codec, cfg->hp_pins[0]) >= 2)
1560 spec->multi_ios = 1; /* give badness */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001561
Takashi Iwaia07a9492013-01-07 16:44:06 +01001562 /* re-count num_dacs and squash invalid entries */
1563 spec->multiout.num_dacs = 0;
1564 for (i = 0; i < cfg->line_outs; i++) {
1565 if (spec->private_dac_nids[i])
1566 spec->multiout.num_dacs++;
1567 else {
1568 memmove(spec->private_dac_nids + i,
1569 spec->private_dac_nids + i + 1,
1570 sizeof(hda_nid_t) * (cfg->line_outs - i - 1));
1571 spec->private_dac_nids[cfg->line_outs - 1] = 0;
1572 }
1573 }
1574
1575 spec->ext_channel_count = spec->min_channel_count =
David Henningssonc0f3b212013-01-16 11:45:37 +01001576 spec->multiout.num_dacs * 2;
Takashi Iwaia07a9492013-01-07 16:44:06 +01001577
Takashi Iwai352f7f92012-12-19 12:52:06 +01001578 if (spec->multi_ios == 2) {
1579 for (i = 0; i < 2; i++)
1580 spec->private_dac_nids[spec->multiout.num_dacs++] =
1581 spec->multi_io[i].dac;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001582 } else if (spec->multi_ios) {
1583 spec->multi_ios = 0;
1584 badness += BAD_MULTI_IO;
1585 }
1586
Takashi Iwai55a63d42013-03-21 17:20:12 +01001587 if (spec->indep_hp && !indep_hp_possible(codec))
1588 badness += BAD_NO_INDEP_HP;
1589
Takashi Iwaia07a9492013-01-07 16:44:06 +01001590 /* re-fill the shared DAC for speaker / headphone */
1591 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1592 refill_shared_dacs(codec, cfg->hp_outs,
1593 spec->multiout.hp_out_nid,
1594 spec->hp_paths);
1595 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1596 refill_shared_dacs(codec, cfg->speaker_outs,
1597 spec->multiout.extra_out_nid,
1598 spec->speaker_paths);
1599
Takashi Iwai352f7f92012-12-19 12:52:06 +01001600 return badness;
1601}
1602
1603#define DEBUG_BADNESS
1604
1605#ifdef DEBUG_BADNESS
1606#define debug_badness snd_printdd
1607#else
1608#define debug_badness(...)
1609#endif
1610
Takashi Iwaia7694092013-01-21 10:43:18 +01001611#ifdef DEBUG_BADNESS
1612static inline void print_nid_path_idx(struct hda_codec *codec,
1613 const char *pfx, int idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001614{
Takashi Iwaia7694092013-01-21 10:43:18 +01001615 struct nid_path *path;
1616
1617 path = snd_hda_get_path_from_idx(codec, idx);
1618 if (path)
1619 print_nid_path(pfx, path);
1620}
1621
1622static void debug_show_configs(struct hda_codec *codec,
1623 struct auto_pin_cfg *cfg)
1624{
1625 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia7694092013-01-21 10:43:18 +01001626 static const char * const lo_type[3] = { "LO", "SP", "HP" };
Takashi Iwaia7694092013-01-21 10:43:18 +01001627 int i;
1628
1629 debug_badness("multi_outs = %x/%x/%x/%x : %x/%x/%x/%x (type %s)\n",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001630 cfg->line_out_pins[0], cfg->line_out_pins[1],
Takashi Iwai708122e2012-12-20 17:56:57 +01001631 cfg->line_out_pins[2], cfg->line_out_pins[3],
Takashi Iwai352f7f92012-12-19 12:52:06 +01001632 spec->multiout.dac_nids[0],
1633 spec->multiout.dac_nids[1],
1634 spec->multiout.dac_nids[2],
Takashi Iwaia7694092013-01-21 10:43:18 +01001635 spec->multiout.dac_nids[3],
1636 lo_type[cfg->line_out_type]);
1637 for (i = 0; i < cfg->line_outs; i++)
1638 print_nid_path_idx(codec, " out", spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001639 if (spec->multi_ios > 0)
1640 debug_badness("multi_ios(%d) = %x/%x : %x/%x\n",
1641 spec->multi_ios,
1642 spec->multi_io[0].pin, spec->multi_io[1].pin,
1643 spec->multi_io[0].dac, spec->multi_io[1].dac);
Takashi Iwaia7694092013-01-21 10:43:18 +01001644 for (i = 0; i < spec->multi_ios; i++)
1645 print_nid_path_idx(codec, " mio",
1646 spec->out_paths[cfg->line_outs + i]);
1647 if (cfg->hp_outs)
1648 debug_badness("hp_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001649 cfg->hp_pins[0], cfg->hp_pins[1],
Takashi Iwai708122e2012-12-20 17:56:57 +01001650 cfg->hp_pins[2], cfg->hp_pins[3],
Takashi Iwai352f7f92012-12-19 12:52:06 +01001651 spec->multiout.hp_out_nid[0],
1652 spec->multiout.hp_out_nid[1],
1653 spec->multiout.hp_out_nid[2],
1654 spec->multiout.hp_out_nid[3]);
Takashi Iwaia7694092013-01-21 10:43:18 +01001655 for (i = 0; i < cfg->hp_outs; i++)
1656 print_nid_path_idx(codec, " hp ", spec->hp_paths[i]);
1657 if (cfg->speaker_outs)
1658 debug_badness("spk_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001659 cfg->speaker_pins[0], cfg->speaker_pins[1],
1660 cfg->speaker_pins[2], cfg->speaker_pins[3],
1661 spec->multiout.extra_out_nid[0],
1662 spec->multiout.extra_out_nid[1],
1663 spec->multiout.extra_out_nid[2],
1664 spec->multiout.extra_out_nid[3]);
Takashi Iwaia7694092013-01-21 10:43:18 +01001665 for (i = 0; i < cfg->speaker_outs; i++)
1666 print_nid_path_idx(codec, " spk", spec->speaker_paths[i]);
1667 for (i = 0; i < 3; i++)
1668 print_nid_path_idx(codec, " mix", spec->aamix_out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001669}
Takashi Iwaia7694092013-01-21 10:43:18 +01001670#else
1671#define debug_show_configs(codec, cfg) /* NOP */
1672#endif
Takashi Iwai352f7f92012-12-19 12:52:06 +01001673
1674/* find all available DACs of the codec */
1675static void fill_all_dac_nids(struct hda_codec *codec)
1676{
1677 struct hda_gen_spec *spec = codec->spec;
1678 int i;
1679 hda_nid_t nid = codec->start_nid;
1680
1681 spec->num_all_dacs = 0;
1682 memset(spec->all_dacs, 0, sizeof(spec->all_dacs));
1683 for (i = 0; i < codec->num_nodes; i++, nid++) {
1684 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_AUD_OUT)
1685 continue;
1686 if (spec->num_all_dacs >= ARRAY_SIZE(spec->all_dacs)) {
1687 snd_printk(KERN_ERR "hda: Too many DACs!\n");
1688 break;
1689 }
1690 spec->all_dacs[spec->num_all_dacs++] = nid;
1691 }
1692}
1693
1694static int parse_output_paths(struct hda_codec *codec)
1695{
1696 struct hda_gen_spec *spec = codec->spec;
1697 struct auto_pin_cfg *cfg = &spec->autocfg;
1698 struct auto_pin_cfg *best_cfg;
Takashi Iwai9314a582013-01-21 10:49:05 +01001699 unsigned int val;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001700 int best_badness = INT_MAX;
1701 int badness;
1702 bool fill_hardwired = true, fill_mio_first = true;
1703 bool best_wired = true, best_mio = true;
1704 bool hp_spk_swapped = false;
1705
Takashi Iwai352f7f92012-12-19 12:52:06 +01001706 best_cfg = kmalloc(sizeof(*best_cfg), GFP_KERNEL);
1707 if (!best_cfg)
1708 return -ENOMEM;
1709 *best_cfg = *cfg;
1710
1711 for (;;) {
1712 badness = fill_and_eval_dacs(codec, fill_hardwired,
1713 fill_mio_first);
1714 if (badness < 0) {
1715 kfree(best_cfg);
1716 return badness;
1717 }
1718 debug_badness("==> lo_type=%d, wired=%d, mio=%d, badness=0x%x\n",
1719 cfg->line_out_type, fill_hardwired, fill_mio_first,
1720 badness);
Takashi Iwaia7694092013-01-21 10:43:18 +01001721 debug_show_configs(codec, cfg);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001722 if (badness < best_badness) {
1723 best_badness = badness;
1724 *best_cfg = *cfg;
1725 best_wired = fill_hardwired;
1726 best_mio = fill_mio_first;
1727 }
1728 if (!badness)
1729 break;
1730 fill_mio_first = !fill_mio_first;
1731 if (!fill_mio_first)
1732 continue;
1733 fill_hardwired = !fill_hardwired;
1734 if (!fill_hardwired)
1735 continue;
1736 if (hp_spk_swapped)
1737 break;
1738 hp_spk_swapped = true;
1739 if (cfg->speaker_outs > 0 &&
1740 cfg->line_out_type == AUTO_PIN_HP_OUT) {
1741 cfg->hp_outs = cfg->line_outs;
1742 memcpy(cfg->hp_pins, cfg->line_out_pins,
1743 sizeof(cfg->hp_pins));
1744 cfg->line_outs = cfg->speaker_outs;
1745 memcpy(cfg->line_out_pins, cfg->speaker_pins,
1746 sizeof(cfg->speaker_pins));
1747 cfg->speaker_outs = 0;
1748 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
1749 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
1750 fill_hardwired = true;
1751 continue;
1752 }
1753 if (cfg->hp_outs > 0 &&
1754 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
1755 cfg->speaker_outs = cfg->line_outs;
1756 memcpy(cfg->speaker_pins, cfg->line_out_pins,
1757 sizeof(cfg->speaker_pins));
1758 cfg->line_outs = cfg->hp_outs;
1759 memcpy(cfg->line_out_pins, cfg->hp_pins,
1760 sizeof(cfg->hp_pins));
1761 cfg->hp_outs = 0;
1762 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
1763 cfg->line_out_type = AUTO_PIN_HP_OUT;
1764 fill_hardwired = true;
1765 continue;
1766 }
1767 break;
1768 }
1769
1770 if (badness) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001771 debug_badness("==> restoring best_cfg\n");
Takashi Iwai352f7f92012-12-19 12:52:06 +01001772 *cfg = *best_cfg;
1773 fill_and_eval_dacs(codec, best_wired, best_mio);
1774 }
1775 debug_badness("==> Best config: lo_type=%d, wired=%d, mio=%d\n",
1776 cfg->line_out_type, best_wired, best_mio);
Takashi Iwaia7694092013-01-21 10:43:18 +01001777 debug_show_configs(codec, cfg);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001778
1779 if (cfg->line_out_pins[0]) {
1780 struct nid_path *path;
Takashi Iwai196c17662013-01-04 15:01:40 +01001781 path = snd_hda_get_path_from_idx(codec, spec->out_paths[0]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001782 if (path)
1783 spec->vmaster_nid = look_for_out_vol_nid(codec, path);
Takashi Iwai7a71bbf2013-01-17 10:25:15 +01001784 if (spec->vmaster_nid)
1785 snd_hda_set_vmaster_tlv(codec, spec->vmaster_nid,
1786 HDA_OUTPUT, spec->vmaster_tlv);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001787 }
1788
Takashi Iwai9314a582013-01-21 10:49:05 +01001789 /* set initial pinctl targets */
1790 if (spec->prefer_hp_amp || cfg->line_out_type == AUTO_PIN_HP_OUT)
1791 val = PIN_HP;
1792 else
1793 val = PIN_OUT;
1794 set_pin_targets(codec, cfg->line_outs, cfg->line_out_pins, val);
1795 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1796 set_pin_targets(codec, cfg->hp_outs, cfg->hp_pins, PIN_HP);
1797 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1798 val = spec->prefer_hp_amp ? PIN_HP : PIN_OUT;
1799 set_pin_targets(codec, cfg->speaker_outs,
1800 cfg->speaker_pins, val);
1801 }
1802
Takashi Iwai55a63d42013-03-21 17:20:12 +01001803 /* clear indep_hp flag if not available */
1804 if (spec->indep_hp && !indep_hp_possible(codec))
1805 spec->indep_hp = 0;
1806
Takashi Iwai352f7f92012-12-19 12:52:06 +01001807 kfree(best_cfg);
1808 return 0;
1809}
1810
1811/* add playback controls from the parsed DAC table */
1812static int create_multi_out_ctls(struct hda_codec *codec,
1813 const struct auto_pin_cfg *cfg)
1814{
1815 struct hda_gen_spec *spec = codec->spec;
1816 int i, err, noutputs;
1817
1818 noutputs = cfg->line_outs;
1819 if (spec->multi_ios > 0 && cfg->line_outs < 3)
1820 noutputs += spec->multi_ios;
1821
1822 for (i = 0; i < noutputs; i++) {
1823 const char *name;
1824 int index;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001825 struct nid_path *path;
1826
Takashi Iwai196c17662013-01-04 15:01:40 +01001827 path = snd_hda_get_path_from_idx(codec, spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001828 if (!path)
1829 continue;
Takashi Iwai247d85e2013-01-17 16:18:11 +01001830
1831 name = get_line_out_pfx(codec, i, &index, NID_PATH_VOL_CTL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001832 if (!name || !strcmp(name, "CLFE")) {
1833 /* Center/LFE */
1834 err = add_vol_ctl(codec, "Center", 0, 1, path);
1835 if (err < 0)
1836 return err;
1837 err = add_vol_ctl(codec, "LFE", 0, 2, path);
1838 if (err < 0)
1839 return err;
Takashi Iwai247d85e2013-01-17 16:18:11 +01001840 } else {
1841 err = add_stereo_vol(codec, name, index, path);
1842 if (err < 0)
1843 return err;
1844 }
1845
1846 name = get_line_out_pfx(codec, i, &index, NID_PATH_MUTE_CTL);
1847 if (!name || !strcmp(name, "CLFE")) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001848 err = add_sw_ctl(codec, "Center", 0, 1, path);
1849 if (err < 0)
1850 return err;
1851 err = add_sw_ctl(codec, "LFE", 0, 2, path);
1852 if (err < 0)
1853 return err;
1854 } else {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001855 err = add_stereo_sw(codec, name, index, path);
1856 if (err < 0)
1857 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 }
1859 }
1860 return 0;
1861}
1862
Takashi Iwaic2c80382013-01-07 10:33:57 +01001863static int create_extra_out(struct hda_codec *codec, int path_idx,
Takashi Iwai196c17662013-01-04 15:01:40 +01001864 const char *pfx, int cidx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865{
Takashi Iwai352f7f92012-12-19 12:52:06 +01001866 struct nid_path *path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 int err;
1868
Takashi Iwai196c17662013-01-04 15:01:40 +01001869 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001870 if (!path)
1871 return 0;
Takashi Iwaic2c80382013-01-07 10:33:57 +01001872 err = add_stereo_vol(codec, pfx, cidx, path);
1873 if (err < 0)
1874 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001875 err = add_stereo_sw(codec, pfx, cidx, path);
1876 if (err < 0)
1877 return err;
1878 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879}
1880
Takashi Iwai352f7f92012-12-19 12:52:06 +01001881/* add playback controls for speaker and HP outputs */
1882static int create_extra_outs(struct hda_codec *codec, int num_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001883 const int *paths, const char *pfx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884{
Takashi Iwaic2c80382013-01-07 10:33:57 +01001885 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001886
1887 for (i = 0; i < num_pins; i++) {
Takashi Iwaic2c80382013-01-07 10:33:57 +01001888 const char *name;
1889 char tmp[44];
1890 int err, idx = 0;
1891
1892 if (num_pins == 2 && i == 1 && !strcmp(pfx, "Speaker"))
1893 name = "Bass Speaker";
1894 else if (num_pins >= 3) {
1895 snprintf(tmp, sizeof(tmp), "%s %s",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001896 pfx, channel_name[i]);
Takashi Iwaic2c80382013-01-07 10:33:57 +01001897 name = tmp;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001898 } else {
Takashi Iwaic2c80382013-01-07 10:33:57 +01001899 name = pfx;
1900 idx = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 }
Takashi Iwaic2c80382013-01-07 10:33:57 +01001902 err = create_extra_out(codec, paths[i], name, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001903 if (err < 0)
1904 return err;
1905 }
1906 return 0;
1907}
Takashi Iwai97ec5582006-03-21 11:29:07 +01001908
Takashi Iwai352f7f92012-12-19 12:52:06 +01001909static int create_hp_out_ctls(struct hda_codec *codec)
1910{
1911 struct hda_gen_spec *spec = codec->spec;
1912 return create_extra_outs(codec, spec->autocfg.hp_outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001913 spec->hp_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001914 "Headphone");
1915}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916
Takashi Iwai352f7f92012-12-19 12:52:06 +01001917static int create_speaker_out_ctls(struct hda_codec *codec)
1918{
1919 struct hda_gen_spec *spec = codec->spec;
1920 return create_extra_outs(codec, spec->autocfg.speaker_outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001921 spec->speaker_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001922 "Speaker");
1923}
1924
1925/*
Takashi Iwai38cf6f12012-12-21 14:09:42 +01001926 * independent HP controls
1927 */
1928
1929static int indep_hp_info(struct snd_kcontrol *kcontrol,
1930 struct snd_ctl_elem_info *uinfo)
1931{
1932 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
1933}
1934
1935static int indep_hp_get(struct snd_kcontrol *kcontrol,
1936 struct snd_ctl_elem_value *ucontrol)
1937{
1938 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1939 struct hda_gen_spec *spec = codec->spec;
1940 ucontrol->value.enumerated.item[0] = spec->indep_hp_enabled;
1941 return 0;
1942}
1943
Takashi Iwaia1e908e2013-01-21 15:11:25 +01001944static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
1945 int nomix_path_idx, int mix_path_idx,
1946 int out_type);
1947
Takashi Iwai38cf6f12012-12-21 14:09:42 +01001948static int indep_hp_put(struct snd_kcontrol *kcontrol,
1949 struct snd_ctl_elem_value *ucontrol)
1950{
1951 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1952 struct hda_gen_spec *spec = codec->spec;
1953 unsigned int select = ucontrol->value.enumerated.item[0];
1954 int ret = 0;
1955
1956 mutex_lock(&spec->pcm_mutex);
1957 if (spec->active_streams) {
1958 ret = -EBUSY;
1959 goto unlock;
1960 }
1961
1962 if (spec->indep_hp_enabled != select) {
Takashi Iwaia1e908e2013-01-21 15:11:25 +01001963 hda_nid_t *dacp;
1964 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
1965 dacp = &spec->private_dac_nids[0];
1966 else
1967 dacp = &spec->multiout.hp_out_nid[0];
1968
1969 /* update HP aamix paths in case it conflicts with indep HP */
1970 if (spec->have_aamix_ctl) {
1971 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
1972 update_aamix_paths(codec, spec->aamix_mode,
1973 spec->out_paths[0],
1974 spec->aamix_out_paths[0],
1975 spec->autocfg.line_out_type);
1976 else
1977 update_aamix_paths(codec, spec->aamix_mode,
1978 spec->hp_paths[0],
1979 spec->aamix_out_paths[1],
1980 AUTO_PIN_HP_OUT);
1981 }
1982
Takashi Iwai38cf6f12012-12-21 14:09:42 +01001983 spec->indep_hp_enabled = select;
1984 if (spec->indep_hp_enabled)
Takashi Iwaia1e908e2013-01-21 15:11:25 +01001985 *dacp = 0;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01001986 else
Takashi Iwaia1e908e2013-01-21 15:11:25 +01001987 *dacp = spec->alt_dac_nid;
Takashi Iwai92603c52013-01-22 07:46:31 +01001988
1989 /* update HP auto-mute state too */
1990 if (spec->hp_automute_hook)
1991 spec->hp_automute_hook(codec, NULL);
1992 else
1993 snd_hda_gen_hp_automute(codec, NULL);
1994
Takashi Iwai38cf6f12012-12-21 14:09:42 +01001995 ret = 1;
1996 }
1997 unlock:
1998 mutex_unlock(&spec->pcm_mutex);
1999 return ret;
2000}
2001
2002static const struct snd_kcontrol_new indep_hp_ctl = {
2003 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2004 .name = "Independent HP",
2005 .info = indep_hp_info,
2006 .get = indep_hp_get,
2007 .put = indep_hp_put,
2008};
2009
2010
2011static int create_indep_hp_ctls(struct hda_codec *codec)
2012{
2013 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002014 hda_nid_t dac;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002015
2016 if (!spec->indep_hp)
2017 return 0;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002018 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
2019 dac = spec->multiout.dac_nids[0];
2020 else
2021 dac = spec->multiout.hp_out_nid[0];
2022 if (!dac) {
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002023 spec->indep_hp = 0;
2024 return 0;
2025 }
2026
2027 spec->indep_hp_enabled = false;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002028 spec->alt_dac_nid = dac;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002029 if (!snd_hda_gen_add_kctl(spec, NULL, &indep_hp_ctl))
2030 return -ENOMEM;
2031 return 0;
2032}
2033
2034/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01002035 * channel mode enum control
2036 */
2037
2038static int ch_mode_info(struct snd_kcontrol *kcontrol,
2039 struct snd_ctl_elem_info *uinfo)
2040{
2041 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2042 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002043 int chs;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002044
2045 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2046 uinfo->count = 1;
2047 uinfo->value.enumerated.items = spec->multi_ios + 1;
2048 if (uinfo->value.enumerated.item > spec->multi_ios)
2049 uinfo->value.enumerated.item = spec->multi_ios;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002050 chs = uinfo->value.enumerated.item * 2 + spec->min_channel_count;
2051 sprintf(uinfo->value.enumerated.name, "%dch", chs);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002052 return 0;
2053}
2054
2055static int ch_mode_get(struct snd_kcontrol *kcontrol,
2056 struct snd_ctl_elem_value *ucontrol)
2057{
2058 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2059 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002060 ucontrol->value.enumerated.item[0] =
2061 (spec->ext_channel_count - spec->min_channel_count) / 2;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002062 return 0;
2063}
2064
Takashi Iwai196c17662013-01-04 15:01:40 +01002065static inline struct nid_path *
2066get_multiio_path(struct hda_codec *codec, int idx)
2067{
2068 struct hda_gen_spec *spec = codec->spec;
2069 return snd_hda_get_path_from_idx(codec,
2070 spec->out_paths[spec->autocfg.line_outs + idx]);
2071}
2072
Takashi Iwaia5cc2502013-01-16 18:08:55 +01002073static void update_automute_all(struct hda_codec *codec);
2074
Takashi Iwai352f7f92012-12-19 12:52:06 +01002075static int set_multi_io(struct hda_codec *codec, int idx, bool output)
2076{
2077 struct hda_gen_spec *spec = codec->spec;
2078 hda_nid_t nid = spec->multi_io[idx].pin;
2079 struct nid_path *path;
2080
Takashi Iwai196c17662013-01-04 15:01:40 +01002081 path = get_multiio_path(codec, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002082 if (!path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 return -EINVAL;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002084
2085 if (path->active == output)
2086 return 0;
2087
2088 if (output) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01002089 set_pin_target(codec, nid, PIN_OUT, true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002090 snd_hda_activate_path(codec, path, true, true);
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01002091 set_pin_eapd(codec, nid, true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002092 } else {
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01002093 set_pin_eapd(codec, nid, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002094 snd_hda_activate_path(codec, path, false, true);
Takashi Iwai2c12c302013-01-10 09:33:29 +01002095 set_pin_target(codec, nid, spec->multi_io[idx].ctl_in, true);
Takashi Iwai55196ff2013-01-24 17:32:56 +01002096 path_power_down_sync(codec, path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 }
Takashi Iwaia365fed2013-01-10 16:10:06 +01002098
2099 /* update jack retasking in case it modifies any of them */
Takashi Iwaia5cc2502013-01-16 18:08:55 +01002100 update_automute_all(codec);
Takashi Iwaia365fed2013-01-10 16:10:06 +01002101
Takashi Iwai352f7f92012-12-19 12:52:06 +01002102 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103}
2104
Takashi Iwai352f7f92012-12-19 12:52:06 +01002105static int ch_mode_put(struct snd_kcontrol *kcontrol,
2106 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002108 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2109 struct hda_gen_spec *spec = codec->spec;
2110 int i, ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111
Takashi Iwai352f7f92012-12-19 12:52:06 +01002112 ch = ucontrol->value.enumerated.item[0];
2113 if (ch < 0 || ch > spec->multi_ios)
2114 return -EINVAL;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002115 if (ch == (spec->ext_channel_count - spec->min_channel_count) / 2)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002116 return 0;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002117 spec->ext_channel_count = ch * 2 + spec->min_channel_count;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002118 for (i = 0; i < spec->multi_ios; i++)
2119 set_multi_io(codec, i, i < ch);
2120 spec->multiout.max_channels = max(spec->ext_channel_count,
2121 spec->const_channel_count);
2122 if (spec->need_dac_fix)
2123 spec->multiout.num_dacs = spec->multiout.max_channels / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 return 1;
2125}
2126
Takashi Iwai352f7f92012-12-19 12:52:06 +01002127static const struct snd_kcontrol_new channel_mode_enum = {
2128 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2129 .name = "Channel Mode",
2130 .info = ch_mode_info,
2131 .get = ch_mode_get,
2132 .put = ch_mode_put,
2133};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134
Takashi Iwai352f7f92012-12-19 12:52:06 +01002135static int create_multi_channel_mode(struct hda_codec *codec)
2136{
2137 struct hda_gen_spec *spec = codec->spec;
2138
2139 if (spec->multi_ios > 0) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01002140 if (!snd_hda_gen_add_kctl(spec, NULL, &channel_mode_enum))
Takashi Iwai352f7f92012-12-19 12:52:06 +01002141 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 return 0;
2144}
2145
Takashi Iwai352f7f92012-12-19 12:52:06 +01002146/*
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002147 * aamix loopback enable/disable switch
2148 */
2149
2150#define loopback_mixing_info indep_hp_info
2151
2152static int loopback_mixing_get(struct snd_kcontrol *kcontrol,
2153 struct snd_ctl_elem_value *ucontrol)
2154{
2155 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2156 struct hda_gen_spec *spec = codec->spec;
2157 ucontrol->value.enumerated.item[0] = spec->aamix_mode;
2158 return 0;
2159}
2160
2161static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002162 int nomix_path_idx, int mix_path_idx,
2163 int out_type)
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002164{
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002165 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002166 struct nid_path *nomix_path, *mix_path;
2167
2168 nomix_path = snd_hda_get_path_from_idx(codec, nomix_path_idx);
2169 mix_path = snd_hda_get_path_from_idx(codec, mix_path_idx);
2170 if (!nomix_path || !mix_path)
2171 return;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002172
2173 /* if HP aamix path is driven from a different DAC and the
2174 * independent HP mode is ON, can't turn on aamix path
2175 */
2176 if (out_type == AUTO_PIN_HP_OUT && spec->indep_hp_enabled &&
2177 mix_path->path[0] != spec->alt_dac_nid)
2178 do_mix = false;
2179
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002180 if (do_mix) {
2181 snd_hda_activate_path(codec, nomix_path, false, true);
2182 snd_hda_activate_path(codec, mix_path, true, true);
Takashi Iwai55196ff2013-01-24 17:32:56 +01002183 path_power_down_sync(codec, nomix_path);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002184 } else {
2185 snd_hda_activate_path(codec, mix_path, false, true);
2186 snd_hda_activate_path(codec, nomix_path, true, true);
Takashi Iwai55196ff2013-01-24 17:32:56 +01002187 path_power_down_sync(codec, mix_path);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002188 }
2189}
2190
2191static int loopback_mixing_put(struct snd_kcontrol *kcontrol,
2192 struct snd_ctl_elem_value *ucontrol)
2193{
2194 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2195 struct hda_gen_spec *spec = codec->spec;
2196 unsigned int val = ucontrol->value.enumerated.item[0];
2197
2198 if (val == spec->aamix_mode)
2199 return 0;
2200 spec->aamix_mode = val;
2201 update_aamix_paths(codec, val, spec->out_paths[0],
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002202 spec->aamix_out_paths[0],
2203 spec->autocfg.line_out_type);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002204 update_aamix_paths(codec, val, spec->hp_paths[0],
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002205 spec->aamix_out_paths[1],
2206 AUTO_PIN_HP_OUT);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002207 update_aamix_paths(codec, val, spec->speaker_paths[0],
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002208 spec->aamix_out_paths[2],
2209 AUTO_PIN_SPEAKER_OUT);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002210 return 1;
2211}
2212
2213static const struct snd_kcontrol_new loopback_mixing_enum = {
2214 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2215 .name = "Loopback Mixing",
2216 .info = loopback_mixing_info,
2217 .get = loopback_mixing_get,
2218 .put = loopback_mixing_put,
2219};
2220
2221static int create_loopback_mixing_ctl(struct hda_codec *codec)
2222{
2223 struct hda_gen_spec *spec = codec->spec;
2224
2225 if (!spec->mixer_nid)
2226 return 0;
2227 if (!(spec->aamix_out_paths[0] || spec->aamix_out_paths[1] ||
2228 spec->aamix_out_paths[2]))
2229 return 0;
2230 if (!snd_hda_gen_add_kctl(spec, NULL, &loopback_mixing_enum))
2231 return -ENOMEM;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002232 spec->have_aamix_ctl = 1;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002233 return 0;
2234}
2235
2236/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01002237 * shared headphone/mic handling
2238 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02002239
Takashi Iwai352f7f92012-12-19 12:52:06 +01002240static void call_update_outputs(struct hda_codec *codec);
2241
2242/* for shared I/O, change the pin-control accordingly */
2243static void update_shared_mic_hp(struct hda_codec *codec, bool set_as_mic)
2244{
2245 struct hda_gen_spec *spec = codec->spec;
2246 unsigned int val;
2247 hda_nid_t pin = spec->autocfg.inputs[1].pin;
2248 /* NOTE: this assumes that there are only two inputs, the
2249 * first is the real internal mic and the second is HP/mic jack.
2250 */
2251
2252 val = snd_hda_get_default_vref(codec, pin);
2253
2254 /* This pin does not have vref caps - let's enable vref on pin 0x18
2255 instead, as suggested by Realtek */
2256 if (val == AC_PINCTL_VREF_HIZ && spec->shared_mic_vref_pin) {
2257 const hda_nid_t vref_pin = spec->shared_mic_vref_pin;
2258 unsigned int vref_val = snd_hda_get_default_vref(codec, vref_pin);
2259 if (vref_val != AC_PINCTL_VREF_HIZ)
Takashi Iwai7594aa32012-12-20 15:38:40 +01002260 snd_hda_set_pin_ctl_cache(codec, vref_pin,
2261 PIN_IN | (set_as_mic ? vref_val : 0));
Takashi Iwaicb53c622007-08-10 17:21:45 +02002262 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002263
2264 val = set_as_mic ? val | PIN_IN : PIN_HP;
Takashi Iwai2c12c302013-01-10 09:33:29 +01002265 set_pin_target(codec, pin, val, true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002266
2267 spec->automute_speaker = !set_as_mic;
2268 call_update_outputs(codec);
2269}
2270
2271/* create a shared input with the headphone out */
2272static int create_shared_input(struct hda_codec *codec)
2273{
2274 struct hda_gen_spec *spec = codec->spec;
2275 struct auto_pin_cfg *cfg = &spec->autocfg;
2276 unsigned int defcfg;
2277 hda_nid_t nid;
2278
2279 /* only one internal input pin? */
2280 if (cfg->num_inputs != 1)
2281 return 0;
2282 defcfg = snd_hda_codec_get_pincfg(codec, cfg->inputs[0].pin);
2283 if (snd_hda_get_input_pin_attr(defcfg) != INPUT_PIN_ATTR_INT)
2284 return 0;
2285
2286 if (cfg->hp_outs == 1 && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
2287 nid = cfg->hp_pins[0]; /* OK, we have a single HP-out */
2288 else if (cfg->line_outs == 1 && cfg->line_out_type == AUTO_PIN_HP_OUT)
2289 nid = cfg->line_out_pins[0]; /* OK, we have a single line-out */
2290 else
2291 return 0; /* both not available */
2292
2293 if (!(snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_IN))
2294 return 0; /* no input */
2295
2296 cfg->inputs[1].pin = nid;
2297 cfg->inputs[1].type = AUTO_PIN_MIC;
2298 cfg->num_inputs = 2;
2299 spec->shared_mic_hp = 1;
2300 snd_printdd("hda-codec: Enable shared I/O jack on NID 0x%x\n", nid);
2301 return 0;
2302}
2303
Takashi Iwai978e77e2013-01-10 16:57:58 +01002304/*
2305 * output jack mode
2306 */
2307static int out_jack_mode_info(struct snd_kcontrol *kcontrol,
2308 struct snd_ctl_elem_info *uinfo)
2309{
2310 static const char * const texts[] = {
2311 "Line Out", "Headphone Out",
2312 };
2313 return snd_hda_enum_helper_info(kcontrol, uinfo, 2, texts);
2314}
2315
2316static int out_jack_mode_get(struct snd_kcontrol *kcontrol,
2317 struct snd_ctl_elem_value *ucontrol)
2318{
2319 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2320 hda_nid_t nid = kcontrol->private_value;
2321 if (snd_hda_codec_get_pin_target(codec, nid) == PIN_HP)
2322 ucontrol->value.enumerated.item[0] = 1;
2323 else
2324 ucontrol->value.enumerated.item[0] = 0;
2325 return 0;
2326}
2327
2328static int out_jack_mode_put(struct snd_kcontrol *kcontrol,
2329 struct snd_ctl_elem_value *ucontrol)
2330{
2331 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2332 hda_nid_t nid = kcontrol->private_value;
2333 unsigned int val;
2334
2335 val = ucontrol->value.enumerated.item[0] ? PIN_HP : PIN_OUT;
2336 if (snd_hda_codec_get_pin_target(codec, nid) == val)
2337 return 0;
2338 snd_hda_set_pin_ctl_cache(codec, nid, val);
2339 return 1;
2340}
2341
2342static const struct snd_kcontrol_new out_jack_mode_enum = {
2343 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2344 .info = out_jack_mode_info,
2345 .get = out_jack_mode_get,
2346 .put = out_jack_mode_put,
2347};
2348
2349static bool find_kctl_name(struct hda_codec *codec, const char *name, int idx)
2350{
2351 struct hda_gen_spec *spec = codec->spec;
2352 int i;
2353
2354 for (i = 0; i < spec->kctls.used; i++) {
2355 struct snd_kcontrol_new *kctl = snd_array_elem(&spec->kctls, i);
2356 if (!strcmp(kctl->name, name) && kctl->index == idx)
2357 return true;
2358 }
2359 return false;
2360}
2361
2362static void get_jack_mode_name(struct hda_codec *codec, hda_nid_t pin,
2363 char *name, size_t name_len)
2364{
2365 struct hda_gen_spec *spec = codec->spec;
2366 int idx = 0;
2367
2368 snd_hda_get_pin_label(codec, pin, &spec->autocfg, name, name_len, &idx);
2369 strlcat(name, " Jack Mode", name_len);
2370
2371 for (; find_kctl_name(codec, name, idx); idx++)
2372 ;
2373}
2374
2375static int create_out_jack_modes(struct hda_codec *codec, int num_pins,
2376 hda_nid_t *pins)
2377{
2378 struct hda_gen_spec *spec = codec->spec;
2379 int i;
2380
2381 for (i = 0; i < num_pins; i++) {
2382 hda_nid_t pin = pins[i];
2383 unsigned int pincap = snd_hda_query_pin_caps(codec, pin);
2384 if ((pincap & AC_PINCAP_OUT) && (pincap & AC_PINCAP_HP_DRV)) {
2385 struct snd_kcontrol_new *knew;
2386 char name[44];
2387 get_jack_mode_name(codec, pin, name, sizeof(name));
2388 knew = snd_hda_gen_add_kctl(spec, name,
2389 &out_jack_mode_enum);
2390 if (!knew)
2391 return -ENOMEM;
2392 knew->private_value = pin;
2393 }
2394 }
2395
2396 return 0;
2397}
2398
Takashi Iwai294765582013-01-17 09:52:11 +01002399/*
2400 * input jack mode
2401 */
2402
2403/* from AC_PINCTL_VREF_HIZ to AC_PINCTL_VREF_100 */
2404#define NUM_VREFS 6
2405
2406static const char * const vref_texts[NUM_VREFS] = {
2407 "Line In", "Mic 50pc Bias", "Mic 0V Bias",
2408 "", "Mic 80pc Bias", "Mic 100pc Bias"
2409};
2410
2411static unsigned int get_vref_caps(struct hda_codec *codec, hda_nid_t pin)
2412{
2413 unsigned int pincap;
2414
2415 pincap = snd_hda_query_pin_caps(codec, pin);
2416 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
2417 /* filter out unusual vrefs */
2418 pincap &= ~(AC_PINCAP_VREF_GRD | AC_PINCAP_VREF_100);
2419 return pincap;
2420}
2421
2422/* convert from the enum item index to the vref ctl index (0=HIZ, 1=50%...) */
2423static int get_vref_idx(unsigned int vref_caps, unsigned int item_idx)
2424{
2425 unsigned int i, n = 0;
2426
2427 for (i = 0; i < NUM_VREFS; i++) {
2428 if (vref_caps & (1 << i)) {
2429 if (n == item_idx)
2430 return i;
2431 n++;
2432 }
2433 }
2434 return 0;
2435}
2436
2437/* convert back from the vref ctl index to the enum item index */
2438static int cvt_from_vref_idx(unsigned int vref_caps, unsigned int idx)
2439{
2440 unsigned int i, n = 0;
2441
2442 for (i = 0; i < NUM_VREFS; i++) {
2443 if (i == idx)
2444 return n;
2445 if (vref_caps & (1 << i))
2446 n++;
2447 }
2448 return 0;
2449}
2450
2451static int in_jack_mode_info(struct snd_kcontrol *kcontrol,
2452 struct snd_ctl_elem_info *uinfo)
2453{
2454 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2455 hda_nid_t nid = kcontrol->private_value;
2456 unsigned int vref_caps = get_vref_caps(codec, nid);
2457
2458 snd_hda_enum_helper_info(kcontrol, uinfo, hweight32(vref_caps),
2459 vref_texts);
2460 /* set the right text */
2461 strcpy(uinfo->value.enumerated.name,
2462 vref_texts[get_vref_idx(vref_caps, uinfo->value.enumerated.item)]);
2463 return 0;
2464}
2465
2466static int in_jack_mode_get(struct snd_kcontrol *kcontrol,
2467 struct snd_ctl_elem_value *ucontrol)
2468{
2469 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2470 hda_nid_t nid = kcontrol->private_value;
2471 unsigned int vref_caps = get_vref_caps(codec, nid);
2472 unsigned int idx;
2473
2474 idx = snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_VREFEN;
2475 ucontrol->value.enumerated.item[0] = cvt_from_vref_idx(vref_caps, idx);
2476 return 0;
2477}
2478
2479static int in_jack_mode_put(struct snd_kcontrol *kcontrol,
2480 struct snd_ctl_elem_value *ucontrol)
2481{
2482 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2483 hda_nid_t nid = kcontrol->private_value;
2484 unsigned int vref_caps = get_vref_caps(codec, nid);
2485 unsigned int val, idx;
2486
2487 val = snd_hda_codec_get_pin_target(codec, nid);
2488 idx = cvt_from_vref_idx(vref_caps, val & AC_PINCTL_VREFEN);
2489 if (idx == ucontrol->value.enumerated.item[0])
2490 return 0;
2491
2492 val &= ~AC_PINCTL_VREFEN;
2493 val |= get_vref_idx(vref_caps, ucontrol->value.enumerated.item[0]);
2494 snd_hda_set_pin_ctl_cache(codec, nid, val);
2495 return 1;
2496}
2497
2498static const struct snd_kcontrol_new in_jack_mode_enum = {
2499 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2500 .info = in_jack_mode_info,
2501 .get = in_jack_mode_get,
2502 .put = in_jack_mode_put,
2503};
2504
2505static int create_in_jack_mode(struct hda_codec *codec, hda_nid_t pin)
2506{
2507 struct hda_gen_spec *spec = codec->spec;
2508 unsigned int defcfg;
2509 struct snd_kcontrol_new *knew;
2510 char name[44];
2511
2512 /* no jack mode for fixed pins */
2513 defcfg = snd_hda_codec_get_pincfg(codec, pin);
2514 if (snd_hda_get_input_pin_attr(defcfg) == INPUT_PIN_ATTR_INT)
2515 return 0;
2516
2517 /* no multiple vref caps? */
2518 if (hweight32(get_vref_caps(codec, pin)) <= 1)
2519 return 0;
2520
2521 get_jack_mode_name(codec, pin, name, sizeof(name));
2522 knew = snd_hda_gen_add_kctl(spec, name, &in_jack_mode_enum);
2523 if (!knew)
2524 return -ENOMEM;
2525 knew->private_value = pin;
2526 return 0;
2527}
2528
Takashi Iwai352f7f92012-12-19 12:52:06 +01002529
2530/*
2531 * Parse input paths
2532 */
2533
Takashi Iwai352f7f92012-12-19 12:52:06 +01002534/* add the powersave loopback-list entry */
Takashi Iwai0186f4f2013-02-07 10:45:11 +01002535static int add_loopback_list(struct hda_gen_spec *spec, hda_nid_t mix, int idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002536{
2537 struct hda_amp_list *list;
2538
Takashi Iwai0186f4f2013-02-07 10:45:11 +01002539 list = snd_array_new(&spec->loopback_list);
2540 if (!list)
2541 return -ENOMEM;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002542 list->nid = mix;
2543 list->dir = HDA_INPUT;
2544 list->idx = idx;
Takashi Iwai0186f4f2013-02-07 10:45:11 +01002545 spec->loopback.amplist = spec->loopback_list.list;
2546 return 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02002547}
Takashi Iwaicb53c622007-08-10 17:21:45 +02002548
Takashi Iwai352f7f92012-12-19 12:52:06 +01002549/* create input playback/capture controls for the given pin */
Takashi Iwai196c17662013-01-04 15:01:40 +01002550static int new_analog_input(struct hda_codec *codec, int input_idx,
2551 hda_nid_t pin, const char *ctlname, int ctlidx,
Takashi Iwai352f7f92012-12-19 12:52:06 +01002552 hda_nid_t mix_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002554 struct hda_gen_spec *spec = codec->spec;
2555 struct nid_path *path;
2556 unsigned int val;
2557 int err, idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558
Takashi Iwai352f7f92012-12-19 12:52:06 +01002559 if (!nid_has_volume(codec, mix_nid, HDA_INPUT) &&
2560 !nid_has_mute(codec, mix_nid, HDA_INPUT))
2561 return 0; /* no need for analog loopback */
2562
Takashi Iwai3ca529d2013-01-07 17:25:08 +01002563 path = snd_hda_add_new_path(codec, pin, mix_nid, 0);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002564 if (!path)
2565 return -EINVAL;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01002566 print_nid_path("loopback", path);
Takashi Iwai196c17662013-01-04 15:01:40 +01002567 spec->loopback_paths[input_idx] = snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002568
2569 idx = path->idx[path->depth - 1];
2570 if (nid_has_volume(codec, mix_nid, HDA_INPUT)) {
2571 val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
2572 err = __add_pb_vol_ctrl(spec, HDA_CTL_WIDGET_VOL, ctlname, ctlidx, val);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002573 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002575 path->ctls[NID_PATH_VOL_CTL] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576 }
2577
Takashi Iwai352f7f92012-12-19 12:52:06 +01002578 if (nid_has_mute(codec, mix_nid, HDA_INPUT)) {
2579 val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
2580 err = __add_pb_sw_ctrl(spec, HDA_CTL_WIDGET_MUTE, ctlname, ctlidx, val);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002581 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002583 path->ctls[NID_PATH_MUTE_CTL] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584 }
2585
Takashi Iwai352f7f92012-12-19 12:52:06 +01002586 path->active = true;
Takashi Iwai0186f4f2013-02-07 10:45:11 +01002587 err = add_loopback_list(spec, mix_nid, idx);
2588 if (err < 0)
2589 return err;
Takashi Iwaie4a395e2013-01-23 17:00:31 +01002590
2591 if (spec->mixer_nid != spec->mixer_merge_nid &&
2592 !spec->loopback_merge_path) {
2593 path = snd_hda_add_new_path(codec, spec->mixer_nid,
2594 spec->mixer_merge_nid, 0);
2595 if (path) {
2596 print_nid_path("loopback-merge", path);
2597 path->active = true;
2598 spec->loopback_merge_path =
2599 snd_hda_get_path_idx(codec, path);
2600 }
2601 }
2602
Takashi Iwai352f7f92012-12-19 12:52:06 +01002603 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604}
2605
Takashi Iwai352f7f92012-12-19 12:52:06 +01002606static int is_input_pin(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002608 unsigned int pincap = snd_hda_query_pin_caps(codec, nid);
2609 return (pincap & AC_PINCAP_IN) != 0;
2610}
2611
2612/* Parse the codec tree and retrieve ADCs */
2613static int fill_adc_nids(struct hda_codec *codec)
2614{
2615 struct hda_gen_spec *spec = codec->spec;
2616 hda_nid_t nid;
2617 hda_nid_t *adc_nids = spec->adc_nids;
2618 int max_nums = ARRAY_SIZE(spec->adc_nids);
2619 int i, nums = 0;
2620
2621 nid = codec->start_nid;
2622 for (i = 0; i < codec->num_nodes; i++, nid++) {
2623 unsigned int caps = get_wcaps(codec, nid);
2624 int type = get_wcaps_type(caps);
2625
2626 if (type != AC_WID_AUD_IN || (caps & AC_WCAP_DIGITAL))
2627 continue;
2628 adc_nids[nums] = nid;
2629 if (++nums >= max_nums)
2630 break;
2631 }
2632 spec->num_adc_nids = nums;
Takashi Iwai0ffd5342013-01-17 15:53:29 +01002633
2634 /* copy the detected ADCs to all_adcs[] */
2635 spec->num_all_adcs = nums;
2636 memcpy(spec->all_adcs, spec->adc_nids, nums * sizeof(hda_nid_t));
2637
Takashi Iwai352f7f92012-12-19 12:52:06 +01002638 return nums;
2639}
2640
2641/* filter out invalid adc_nids that don't give all active input pins;
2642 * if needed, check whether dynamic ADC-switching is available
2643 */
2644static int check_dyn_adc_switch(struct hda_codec *codec)
2645{
2646 struct hda_gen_spec *spec = codec->spec;
2647 struct hda_input_mux *imux = &spec->input_mux;
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002648 unsigned int ok_bits;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002649 int i, n, nums;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002650
2651 again:
2652 nums = 0;
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002653 ok_bits = 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002654 for (n = 0; n < spec->num_adc_nids; n++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002655 for (i = 0; i < imux->num_items; i++) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002656 if (!spec->input_paths[i][n])
Takashi Iwai352f7f92012-12-19 12:52:06 +01002657 break;
2658 }
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002659 if (i >= imux->num_items) {
2660 ok_bits |= (1 << n);
2661 nums++;
2662 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002663 }
2664
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002665 if (!ok_bits) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002666 if (spec->shared_mic_hp) {
2667 spec->shared_mic_hp = 0;
2668 imux->num_items = 1;
2669 goto again;
2670 }
2671
2672 /* check whether ADC-switch is possible */
2673 for (i = 0; i < imux->num_items; i++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002674 for (n = 0; n < spec->num_adc_nids; n++) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002675 if (spec->input_paths[i][n]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002676 spec->dyn_adc_idx[i] = n;
2677 break;
2678 }
2679 }
2680 }
2681
2682 snd_printdd("hda-codec: enabling ADC switching\n");
2683 spec->dyn_adc_switch = 1;
2684 } else if (nums != spec->num_adc_nids) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002685 /* shrink the invalid adcs and input paths */
2686 nums = 0;
2687 for (n = 0; n < spec->num_adc_nids; n++) {
2688 if (!(ok_bits & (1 << n)))
2689 continue;
2690 if (n != nums) {
2691 spec->adc_nids[nums] = spec->adc_nids[n];
Takashi Iwai980428c2013-01-09 09:28:20 +01002692 for (i = 0; i < imux->num_items; i++) {
2693 invalidate_nid_path(codec,
2694 spec->input_paths[i][nums]);
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002695 spec->input_paths[i][nums] =
2696 spec->input_paths[i][n];
Takashi Iwai980428c2013-01-09 09:28:20 +01002697 }
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002698 }
2699 nums++;
2700 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002701 spec->num_adc_nids = nums;
2702 }
2703
2704 if (imux->num_items == 1 || spec->shared_mic_hp) {
2705 snd_printdd("hda-codec: reducing to a single ADC\n");
2706 spec->num_adc_nids = 1; /* reduce to a single ADC */
2707 }
2708
2709 /* single index for individual volumes ctls */
2710 if (!spec->dyn_adc_switch && spec->multi_cap_vol)
2711 spec->num_adc_nids = 1;
2712
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713 return 0;
2714}
2715
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002716/* parse capture source paths from the given pin and create imux items */
2717static int parse_capture_source(struct hda_codec *codec, hda_nid_t pin,
Takashi Iwai9dba2052013-01-18 10:01:15 +01002718 int cfg_idx, int num_adcs,
2719 const char *label, int anchor)
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002720{
2721 struct hda_gen_spec *spec = codec->spec;
2722 struct hda_input_mux *imux = &spec->input_mux;
2723 int imux_idx = imux->num_items;
2724 bool imux_added = false;
2725 int c;
2726
2727 for (c = 0; c < num_adcs; c++) {
2728 struct nid_path *path;
2729 hda_nid_t adc = spec->adc_nids[c];
2730
2731 if (!is_reachable_path(codec, pin, adc))
2732 continue;
2733 path = snd_hda_add_new_path(codec, pin, adc, anchor);
2734 if (!path)
2735 continue;
2736 print_nid_path("input", path);
2737 spec->input_paths[imux_idx][c] =
2738 snd_hda_get_path_idx(codec, path);
2739
2740 if (!imux_added) {
2741 spec->imux_pins[imux->num_items] = pin;
Takashi Iwai9dba2052013-01-18 10:01:15 +01002742 snd_hda_add_imux_item(imux, label, cfg_idx, NULL);
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002743 imux_added = true;
2744 }
2745 }
2746
2747 return 0;
2748}
2749
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01002751 * create playback/capture controls for input pins
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752 */
Takashi Iwai9dba2052013-01-18 10:01:15 +01002753
Takashi Iwaic9700422013-01-18 10:17:30 +01002754/* fill the label for each input at first */
2755static int fill_input_pin_labels(struct hda_codec *codec)
2756{
2757 struct hda_gen_spec *spec = codec->spec;
2758 const struct auto_pin_cfg *cfg = &spec->autocfg;
2759 int i;
2760
2761 for (i = 0; i < cfg->num_inputs; i++) {
2762 hda_nid_t pin = cfg->inputs[i].pin;
2763 const char *label;
2764 int j, idx;
2765
2766 if (!is_input_pin(codec, pin))
2767 continue;
2768
2769 label = hda_get_autocfg_input_label(codec, cfg, i);
2770 idx = 0;
David Henningsson8e8db7f2013-01-18 15:43:02 +01002771 for (j = i - 1; j >= 0; j--) {
Takashi Iwaic9700422013-01-18 10:17:30 +01002772 if (spec->input_labels[j] &&
2773 !strcmp(spec->input_labels[j], label)) {
2774 idx = spec->input_label_idxs[j] + 1;
2775 break;
2776 }
2777 }
2778
2779 spec->input_labels[i] = label;
2780 spec->input_label_idxs[i] = idx;
2781 }
2782
2783 return 0;
2784}
2785
Takashi Iwai9dba2052013-01-18 10:01:15 +01002786#define CFG_IDX_MIX 99 /* a dummy cfg->input idx for stereo mix */
2787
Takashi Iwai352f7f92012-12-19 12:52:06 +01002788static int create_input_ctls(struct hda_codec *codec)
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02002789{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002790 struct hda_gen_spec *spec = codec->spec;
2791 const struct auto_pin_cfg *cfg = &spec->autocfg;
2792 hda_nid_t mixer = spec->mixer_nid;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002793 int num_adcs;
Takashi Iwaic9700422013-01-18 10:17:30 +01002794 int i, err;
Takashi Iwai2c12c302013-01-10 09:33:29 +01002795 unsigned int val;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02002796
Takashi Iwai352f7f92012-12-19 12:52:06 +01002797 num_adcs = fill_adc_nids(codec);
2798 if (num_adcs < 0)
2799 return 0;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02002800
Takashi Iwaic9700422013-01-18 10:17:30 +01002801 err = fill_input_pin_labels(codec);
2802 if (err < 0)
2803 return err;
2804
Takashi Iwai352f7f92012-12-19 12:52:06 +01002805 for (i = 0; i < cfg->num_inputs; i++) {
2806 hda_nid_t pin;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807
Takashi Iwai352f7f92012-12-19 12:52:06 +01002808 pin = cfg->inputs[i].pin;
2809 if (!is_input_pin(codec, pin))
2810 continue;
2811
Takashi Iwai2c12c302013-01-10 09:33:29 +01002812 val = PIN_IN;
2813 if (cfg->inputs[i].type == AUTO_PIN_MIC)
2814 val |= snd_hda_get_default_vref(codec, pin);
2815 set_pin_target(codec, pin, val, false);
2816
Takashi Iwai352f7f92012-12-19 12:52:06 +01002817 if (mixer) {
2818 if (is_reachable_path(codec, pin, mixer)) {
Takashi Iwai196c17662013-01-04 15:01:40 +01002819 err = new_analog_input(codec, i, pin,
Takashi Iwaic9700422013-01-18 10:17:30 +01002820 spec->input_labels[i],
2821 spec->input_label_idxs[i],
2822 mixer);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002823 if (err < 0)
2824 return err;
2825 }
2826 }
2827
Takashi Iwaic9700422013-01-18 10:17:30 +01002828 err = parse_capture_source(codec, pin, i, num_adcs,
2829 spec->input_labels[i], -mixer);
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002830 if (err < 0)
2831 return err;
Takashi Iwai294765582013-01-17 09:52:11 +01002832
2833 if (spec->add_in_jack_modes) {
2834 err = create_in_jack_mode(codec, pin);
2835 if (err < 0)
2836 return err;
2837 }
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002838 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002839
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002840 if (mixer && spec->add_stereo_mix_input) {
Takashi Iwai9dba2052013-01-18 10:01:15 +01002841 err = parse_capture_source(codec, mixer, CFG_IDX_MIX, num_adcs,
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002842 "Stereo Mix", 0);
2843 if (err < 0)
2844 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002845 }
2846
2847 return 0;
2848}
2849
2850
2851/*
2852 * input source mux
2853 */
2854
Takashi Iwaic697b712013-01-07 17:09:26 +01002855/* get the input path specified by the given adc and imux indices */
2856static struct nid_path *get_input_path(struct hda_codec *codec, int adc_idx, int imux_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002857{
2858 struct hda_gen_spec *spec = codec->spec;
David Henningssonb56fa1e2013-01-16 11:45:35 +01002859 if (imux_idx < 0 || imux_idx >= HDA_MAX_NUM_INPUTS) {
2860 snd_BUG();
2861 return NULL;
2862 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002863 if (spec->dyn_adc_switch)
2864 adc_idx = spec->dyn_adc_idx[imux_idx];
David Henningssond3d982f2013-01-18 15:43:01 +01002865 if (adc_idx < 0 || adc_idx >= AUTO_CFG_MAX_INS) {
David Henningssonb56fa1e2013-01-16 11:45:35 +01002866 snd_BUG();
2867 return NULL;
2868 }
Takashi Iwaic697b712013-01-07 17:09:26 +01002869 return snd_hda_get_path_from_idx(codec, spec->input_paths[imux_idx][adc_idx]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002870}
2871
2872static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
2873 unsigned int idx);
2874
2875static int mux_enum_info(struct snd_kcontrol *kcontrol,
2876 struct snd_ctl_elem_info *uinfo)
2877{
2878 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2879 struct hda_gen_spec *spec = codec->spec;
2880 return snd_hda_input_mux_info(&spec->input_mux, uinfo);
2881}
2882
2883static int mux_enum_get(struct snd_kcontrol *kcontrol,
2884 struct snd_ctl_elem_value *ucontrol)
2885{
2886 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2887 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai2a8d5392013-01-18 16:23:25 +01002888 /* the ctls are created at once with multiple counts */
2889 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002890
2891 ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
2892 return 0;
2893}
2894
2895static int mux_enum_put(struct snd_kcontrol *kcontrol,
2896 struct snd_ctl_elem_value *ucontrol)
2897{
2898 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Takashi Iwai2a8d5392013-01-18 16:23:25 +01002899 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002900 return mux_select(codec, adc_idx,
2901 ucontrol->value.enumerated.item[0]);
2902}
2903
Takashi Iwai352f7f92012-12-19 12:52:06 +01002904static const struct snd_kcontrol_new cap_src_temp = {
2905 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2906 .name = "Input Source",
2907 .info = mux_enum_info,
2908 .get = mux_enum_get,
2909 .put = mux_enum_put,
2910};
2911
Takashi Iwai47d46ab2012-12-20 11:48:54 +01002912/*
2913 * capture volume and capture switch ctls
2914 */
2915
Takashi Iwai352f7f92012-12-19 12:52:06 +01002916typedef int (*put_call_t)(struct snd_kcontrol *kcontrol,
2917 struct snd_ctl_elem_value *ucontrol);
2918
Takashi Iwai47d46ab2012-12-20 11:48:54 +01002919/* call the given amp update function for all amps in the imux list at once */
Takashi Iwai352f7f92012-12-19 12:52:06 +01002920static int cap_put_caller(struct snd_kcontrol *kcontrol,
2921 struct snd_ctl_elem_value *ucontrol,
2922 put_call_t func, int type)
2923{
2924 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2925 struct hda_gen_spec *spec = codec->spec;
2926 const struct hda_input_mux *imux;
2927 struct nid_path *path;
2928 int i, adc_idx, err = 0;
2929
2930 imux = &spec->input_mux;
David Henningssona053d1e2013-01-16 11:45:36 +01002931 adc_idx = kcontrol->id.index;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002932 mutex_lock(&codec->control_mutex);
Takashi Iwai47d46ab2012-12-20 11:48:54 +01002933 /* we use the cache-only update at first since multiple input paths
2934 * may shared the same amp; by updating only caches, the redundant
2935 * writes to hardware can be reduced.
2936 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01002937 codec->cached_write = 1;
2938 for (i = 0; i < imux->num_items; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01002939 path = get_input_path(codec, adc_idx, i);
2940 if (!path || !path->ctls[type])
Takashi Iwai352f7f92012-12-19 12:52:06 +01002941 continue;
2942 kcontrol->private_value = path->ctls[type];
2943 err = func(kcontrol, ucontrol);
2944 if (err < 0)
2945 goto error;
2946 }
2947 error:
2948 codec->cached_write = 0;
2949 mutex_unlock(&codec->control_mutex);
Takashi Iwaidc870f32013-01-22 15:24:30 +01002950 snd_hda_codec_flush_cache(codec); /* flush the updates */
Takashi Iwai352f7f92012-12-19 12:52:06 +01002951 if (err >= 0 && spec->cap_sync_hook)
Takashi Iwaia90229e2013-01-18 14:10:00 +01002952 spec->cap_sync_hook(codec, ucontrol);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002953 return err;
2954}
2955
2956/* capture volume ctl callbacks */
2957#define cap_vol_info snd_hda_mixer_amp_volume_info
2958#define cap_vol_get snd_hda_mixer_amp_volume_get
2959#define cap_vol_tlv snd_hda_mixer_amp_tlv
2960
2961static int cap_vol_put(struct snd_kcontrol *kcontrol,
2962 struct snd_ctl_elem_value *ucontrol)
2963{
2964 return cap_put_caller(kcontrol, ucontrol,
2965 snd_hda_mixer_amp_volume_put,
2966 NID_PATH_VOL_CTL);
2967}
2968
2969static const struct snd_kcontrol_new cap_vol_temp = {
2970 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2971 .name = "Capture Volume",
2972 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
2973 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
2974 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK),
2975 .info = cap_vol_info,
2976 .get = cap_vol_get,
2977 .put = cap_vol_put,
2978 .tlv = { .c = cap_vol_tlv },
2979};
2980
2981/* capture switch ctl callbacks */
2982#define cap_sw_info snd_ctl_boolean_stereo_info
2983#define cap_sw_get snd_hda_mixer_amp_switch_get
2984
2985static int cap_sw_put(struct snd_kcontrol *kcontrol,
2986 struct snd_ctl_elem_value *ucontrol)
2987{
Takashi Iwaia90229e2013-01-18 14:10:00 +01002988 return cap_put_caller(kcontrol, ucontrol,
Takashi Iwai352f7f92012-12-19 12:52:06 +01002989 snd_hda_mixer_amp_switch_put,
2990 NID_PATH_MUTE_CTL);
2991}
2992
2993static const struct snd_kcontrol_new cap_sw_temp = {
2994 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2995 .name = "Capture Switch",
2996 .info = cap_sw_info,
2997 .get = cap_sw_get,
2998 .put = cap_sw_put,
2999};
3000
3001static int parse_capvol_in_path(struct hda_codec *codec, struct nid_path *path)
3002{
3003 hda_nid_t nid;
3004 int i, depth;
3005
3006 path->ctls[NID_PATH_VOL_CTL] = path->ctls[NID_PATH_MUTE_CTL] = 0;
3007 for (depth = 0; depth < 3; depth++) {
3008 if (depth >= path->depth)
3009 return -EINVAL;
3010 i = path->depth - depth - 1;
3011 nid = path->path[i];
3012 if (!path->ctls[NID_PATH_VOL_CTL]) {
3013 if (nid_has_volume(codec, nid, HDA_OUTPUT))
3014 path->ctls[NID_PATH_VOL_CTL] =
3015 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3016 else if (nid_has_volume(codec, nid, HDA_INPUT)) {
3017 int idx = path->idx[i];
3018 if (!depth && codec->single_adc_amp)
3019 idx = 0;
3020 path->ctls[NID_PATH_VOL_CTL] =
3021 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
3022 }
3023 }
3024 if (!path->ctls[NID_PATH_MUTE_CTL]) {
3025 if (nid_has_mute(codec, nid, HDA_OUTPUT))
3026 path->ctls[NID_PATH_MUTE_CTL] =
3027 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3028 else if (nid_has_mute(codec, nid, HDA_INPUT)) {
3029 int idx = path->idx[i];
3030 if (!depth && codec->single_adc_amp)
3031 idx = 0;
3032 path->ctls[NID_PATH_MUTE_CTL] =
3033 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
3034 }
3035 }
Takashi Iwai97ec5582006-03-21 11:29:07 +01003036 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037 return 0;
3038}
3039
Takashi Iwai352f7f92012-12-19 12:52:06 +01003040static bool is_inv_dmic_pin(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003042 struct hda_gen_spec *spec = codec->spec;
3043 struct auto_pin_cfg *cfg = &spec->autocfg;
3044 unsigned int val;
3045 int i;
3046
3047 if (!spec->inv_dmic_split)
3048 return false;
3049 for (i = 0; i < cfg->num_inputs; i++) {
3050 if (cfg->inputs[i].pin != nid)
3051 continue;
3052 if (cfg->inputs[i].type != AUTO_PIN_MIC)
3053 return false;
3054 val = snd_hda_codec_get_pincfg(codec, nid);
3055 return snd_hda_get_input_pin_attr(val) == INPUT_PIN_ATTR_INT;
3056 }
3057 return false;
3058}
3059
Takashi Iwaia90229e2013-01-18 14:10:00 +01003060/* capture switch put callback for a single control with hook call */
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003061static int cap_single_sw_put(struct snd_kcontrol *kcontrol,
3062 struct snd_ctl_elem_value *ucontrol)
3063{
3064 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3065 struct hda_gen_spec *spec = codec->spec;
3066 int ret;
3067
3068 ret = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
3069 if (ret < 0)
3070 return ret;
3071
Takashi Iwaia90229e2013-01-18 14:10:00 +01003072 if (spec->cap_sync_hook)
3073 spec->cap_sync_hook(codec, ucontrol);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003074
3075 return ret;
3076}
3077
Takashi Iwai352f7f92012-12-19 12:52:06 +01003078static int add_single_cap_ctl(struct hda_codec *codec, const char *label,
3079 int idx, bool is_switch, unsigned int ctl,
3080 bool inv_dmic)
3081{
3082 struct hda_gen_spec *spec = codec->spec;
3083 char tmpname[44];
3084 int type = is_switch ? HDA_CTL_WIDGET_MUTE : HDA_CTL_WIDGET_VOL;
3085 const char *sfx = is_switch ? "Switch" : "Volume";
3086 unsigned int chs = inv_dmic ? 1 : 3;
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003087 struct snd_kcontrol_new *knew;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003088
3089 if (!ctl)
3090 return 0;
3091
3092 if (label)
3093 snprintf(tmpname, sizeof(tmpname),
3094 "%s Capture %s", label, sfx);
3095 else
3096 snprintf(tmpname, sizeof(tmpname),
3097 "Capture %s", sfx);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003098 knew = add_control(spec, type, tmpname, idx,
3099 amp_val_replace_channels(ctl, chs));
3100 if (!knew)
3101 return -ENOMEM;
Takashi Iwaia90229e2013-01-18 14:10:00 +01003102 if (is_switch)
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003103 knew->put = cap_single_sw_put;
3104 if (!inv_dmic)
3105 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003106
3107 /* Make independent right kcontrol */
3108 if (label)
3109 snprintf(tmpname, sizeof(tmpname),
3110 "Inverted %s Capture %s", label, sfx);
3111 else
3112 snprintf(tmpname, sizeof(tmpname),
3113 "Inverted Capture %s", sfx);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003114 knew = add_control(spec, type, tmpname, idx,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003115 amp_val_replace_channels(ctl, 2));
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003116 if (!knew)
3117 return -ENOMEM;
Takashi Iwaia90229e2013-01-18 14:10:00 +01003118 if (is_switch)
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003119 knew->put = cap_single_sw_put;
3120 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003121}
3122
3123/* create single (and simple) capture volume and switch controls */
3124static int create_single_cap_vol_ctl(struct hda_codec *codec, int idx,
3125 unsigned int vol_ctl, unsigned int sw_ctl,
3126 bool inv_dmic)
3127{
3128 int err;
3129 err = add_single_cap_ctl(codec, NULL, idx, false, vol_ctl, inv_dmic);
3130 if (err < 0)
3131 return err;
3132 err = add_single_cap_ctl(codec, NULL, idx, true, sw_ctl, inv_dmic);
3133 if (err < 0)
3134 return err;
3135 return 0;
3136}
3137
3138/* create bound capture volume and switch controls */
3139static int create_bind_cap_vol_ctl(struct hda_codec *codec, int idx,
3140 unsigned int vol_ctl, unsigned int sw_ctl)
3141{
3142 struct hda_gen_spec *spec = codec->spec;
3143 struct snd_kcontrol_new *knew;
3144
3145 if (vol_ctl) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01003146 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_vol_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003147 if (!knew)
3148 return -ENOMEM;
3149 knew->index = idx;
3150 knew->private_value = vol_ctl;
3151 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
3152 }
3153 if (sw_ctl) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01003154 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_sw_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003155 if (!knew)
3156 return -ENOMEM;
3157 knew->index = idx;
3158 knew->private_value = sw_ctl;
3159 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
3160 }
3161 return 0;
3162}
3163
3164/* return the vol ctl when used first in the imux list */
3165static unsigned int get_first_cap_ctl(struct hda_codec *codec, int idx, int type)
3166{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003167 struct nid_path *path;
3168 unsigned int ctl;
3169 int i;
3170
Takashi Iwaic697b712013-01-07 17:09:26 +01003171 path = get_input_path(codec, 0, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003172 if (!path)
3173 return 0;
3174 ctl = path->ctls[type];
3175 if (!ctl)
3176 return 0;
3177 for (i = 0; i < idx - 1; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01003178 path = get_input_path(codec, 0, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003179 if (path && path->ctls[type] == ctl)
3180 return 0;
3181 }
3182 return ctl;
3183}
3184
3185/* create individual capture volume and switch controls per input */
3186static int create_multi_cap_vol_ctl(struct hda_codec *codec)
3187{
3188 struct hda_gen_spec *spec = codec->spec;
3189 struct hda_input_mux *imux = &spec->input_mux;
Takashi Iwaic9700422013-01-18 10:17:30 +01003190 int i, err, type;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003191
3192 for (i = 0; i < imux->num_items; i++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003193 bool inv_dmic;
Takashi Iwaic9700422013-01-18 10:17:30 +01003194 int idx;
Takashi Iwai9dba2052013-01-18 10:01:15 +01003195
Takashi Iwaic9700422013-01-18 10:17:30 +01003196 idx = imux->items[i].index;
3197 if (idx >= spec->autocfg.num_inputs)
Takashi Iwai9dba2052013-01-18 10:01:15 +01003198 continue;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003199 inv_dmic = is_inv_dmic_pin(codec, spec->imux_pins[i]);
3200
3201 for (type = 0; type < 2; type++) {
Takashi Iwaic9700422013-01-18 10:17:30 +01003202 err = add_single_cap_ctl(codec,
3203 spec->input_labels[idx],
3204 spec->input_label_idxs[idx],
3205 type,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003206 get_first_cap_ctl(codec, i, type),
3207 inv_dmic);
3208 if (err < 0)
3209 return err;
3210 }
3211 }
3212 return 0;
3213}
3214
3215static int create_capture_mixers(struct hda_codec *codec)
3216{
3217 struct hda_gen_spec *spec = codec->spec;
3218 struct hda_input_mux *imux = &spec->input_mux;
3219 int i, n, nums, err;
3220
3221 if (spec->dyn_adc_switch)
3222 nums = 1;
3223 else
3224 nums = spec->num_adc_nids;
3225
3226 if (!spec->auto_mic && imux->num_items > 1) {
3227 struct snd_kcontrol_new *knew;
Takashi Iwai624d9142012-12-19 17:41:52 +01003228 const char *name;
3229 name = nums > 1 ? "Input Source" : "Capture Source";
3230 knew = snd_hda_gen_add_kctl(spec, name, &cap_src_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003231 if (!knew)
3232 return -ENOMEM;
3233 knew->count = nums;
3234 }
3235
3236 for (n = 0; n < nums; n++) {
3237 bool multi = false;
David Henningsson99a55922013-01-16 15:58:44 +01003238 bool multi_cap_vol = spec->multi_cap_vol;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003239 bool inv_dmic = false;
3240 int vol, sw;
3241
3242 vol = sw = 0;
3243 for (i = 0; i < imux->num_items; i++) {
3244 struct nid_path *path;
Takashi Iwaic697b712013-01-07 17:09:26 +01003245 path = get_input_path(codec, n, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003246 if (!path)
3247 continue;
3248 parse_capvol_in_path(codec, path);
3249 if (!vol)
3250 vol = path->ctls[NID_PATH_VOL_CTL];
David Henningsson99a55922013-01-16 15:58:44 +01003251 else if (vol != path->ctls[NID_PATH_VOL_CTL]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003252 multi = true;
David Henningsson99a55922013-01-16 15:58:44 +01003253 if (!same_amp_caps(codec, vol,
3254 path->ctls[NID_PATH_VOL_CTL], HDA_INPUT))
3255 multi_cap_vol = true;
3256 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003257 if (!sw)
3258 sw = path->ctls[NID_PATH_MUTE_CTL];
David Henningsson99a55922013-01-16 15:58:44 +01003259 else if (sw != path->ctls[NID_PATH_MUTE_CTL]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003260 multi = true;
David Henningsson99a55922013-01-16 15:58:44 +01003261 if (!same_amp_caps(codec, sw,
3262 path->ctls[NID_PATH_MUTE_CTL], HDA_INPUT))
3263 multi_cap_vol = true;
3264 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003265 if (is_inv_dmic_pin(codec, spec->imux_pins[i]))
3266 inv_dmic = true;
3267 }
3268
3269 if (!multi)
3270 err = create_single_cap_vol_ctl(codec, n, vol, sw,
3271 inv_dmic);
David Henningsson99a55922013-01-16 15:58:44 +01003272 else if (!multi_cap_vol)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003273 err = create_bind_cap_vol_ctl(codec, n, vol, sw);
3274 else
3275 err = create_multi_cap_vol_ctl(codec);
3276 if (err < 0)
3277 return err;
3278 }
3279
3280 return 0;
3281}
3282
3283/*
3284 * add mic boosts if needed
3285 */
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003286
3287/* check whether the given amp is feasible as a boost volume */
3288static bool check_boost_vol(struct hda_codec *codec, hda_nid_t nid,
3289 int dir, int idx)
3290{
3291 unsigned int step;
3292
3293 if (!nid_has_volume(codec, nid, dir) ||
3294 is_ctl_associated(codec, nid, dir, idx, NID_PATH_VOL_CTL) ||
3295 is_ctl_associated(codec, nid, dir, idx, NID_PATH_BOOST_CTL))
3296 return false;
3297
3298 step = (query_amp_caps(codec, nid, dir) & AC_AMPCAP_STEP_SIZE)
3299 >> AC_AMPCAP_STEP_SIZE_SHIFT;
3300 if (step < 0x20)
3301 return false;
3302 return true;
3303}
3304
3305/* look for a boost amp in a widget close to the pin */
3306static unsigned int look_for_boost_amp(struct hda_codec *codec,
3307 struct nid_path *path)
3308{
3309 unsigned int val = 0;
3310 hda_nid_t nid;
3311 int depth;
3312
3313 for (depth = 0; depth < 3; depth++) {
3314 if (depth >= path->depth - 1)
3315 break;
3316 nid = path->path[depth];
3317 if (depth && check_boost_vol(codec, nid, HDA_OUTPUT, 0)) {
3318 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3319 break;
3320 } else if (check_boost_vol(codec, nid, HDA_INPUT,
3321 path->idx[depth])) {
3322 val = HDA_COMPOSE_AMP_VAL(nid, 3, path->idx[depth],
3323 HDA_INPUT);
3324 break;
3325 }
3326 }
3327
3328 return val;
3329}
3330
Takashi Iwai352f7f92012-12-19 12:52:06 +01003331static int parse_mic_boost(struct hda_codec *codec)
3332{
3333 struct hda_gen_spec *spec = codec->spec;
3334 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003335 struct hda_input_mux *imux = &spec->input_mux;
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003336 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003337
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003338 if (!spec->num_adc_nids)
3339 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003340
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003341 for (i = 0; i < imux->num_items; i++) {
3342 struct nid_path *path;
3343 unsigned int val;
3344 int idx;
3345 char boost_label[44];
David Henningsson02aba552013-01-16 15:58:43 +01003346
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003347 idx = imux->items[i].index;
3348 if (idx >= imux->num_items)
3349 continue;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003350
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003351 /* check only line-in and mic pins */
Takashi Iwai1799cdd2013-01-18 14:37:16 +01003352 if (cfg->inputs[idx].type > AUTO_PIN_LINE_IN)
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003353 continue;
3354
3355 path = get_input_path(codec, 0, i);
3356 if (!path)
3357 continue;
3358
3359 val = look_for_boost_amp(codec, path);
3360 if (!val)
3361 continue;
3362
3363 /* create a boost control */
3364 snprintf(boost_label, sizeof(boost_label),
3365 "%s Boost Volume", spec->input_labels[idx]);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003366 if (!add_control(spec, HDA_CTL_WIDGET_VOL, boost_label,
3367 spec->input_label_idxs[idx], val))
3368 return -ENOMEM;
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003369
3370 path->ctls[NID_PATH_BOOST_CTL] = val;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003371 }
3372 return 0;
3373}
3374
3375/*
3376 * parse digital I/Os and set up NIDs in BIOS auto-parse mode
3377 */
3378static void parse_digital(struct hda_codec *codec)
3379{
3380 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003381 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003382 int i, nums;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003383 hda_nid_t dig_nid, pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003384
3385 /* support multiple SPDIFs; the secondary is set up as a slave */
3386 nums = 0;
3387 for (i = 0; i < spec->autocfg.dig_outs; i++) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01003388 pin = spec->autocfg.dig_out_pins[i];
Takashi Iwai352f7f92012-12-19 12:52:06 +01003389 dig_nid = look_for_dac(codec, pin, true);
3390 if (!dig_nid)
3391 continue;
Takashi Iwai3ca529d2013-01-07 17:25:08 +01003392 path = snd_hda_add_new_path(codec, dig_nid, pin, 0);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003393 if (!path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003394 continue;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003395 print_nid_path("digout", path);
Takashi Iwaie1284af2013-01-03 16:33:02 +01003396 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +01003397 spec->digout_paths[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai2c12c302013-01-10 09:33:29 +01003398 set_pin_target(codec, pin, PIN_OUT, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003399 if (!nums) {
3400 spec->multiout.dig_out_nid = dig_nid;
3401 spec->dig_out_type = spec->autocfg.dig_out_type[0];
3402 } else {
3403 spec->multiout.slave_dig_outs = spec->slave_dig_outs;
3404 if (nums >= ARRAY_SIZE(spec->slave_dig_outs) - 1)
3405 break;
3406 spec->slave_dig_outs[nums - 1] = dig_nid;
3407 }
3408 nums++;
3409 }
3410
3411 if (spec->autocfg.dig_in_pin) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01003412 pin = spec->autocfg.dig_in_pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003413 dig_nid = codec->start_nid;
3414 for (i = 0; i < codec->num_nodes; i++, dig_nid++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003415 unsigned int wcaps = get_wcaps(codec, dig_nid);
3416 if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
3417 continue;
3418 if (!(wcaps & AC_WCAP_DIGITAL))
3419 continue;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003420 path = snd_hda_add_new_path(codec, pin, dig_nid, 0);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003421 if (path) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003422 print_nid_path("digin", path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003423 path->active = true;
3424 spec->dig_in_nid = dig_nid;
Takashi Iwai2430d7b2013-01-04 15:09:42 +01003425 spec->digin_path = snd_hda_get_path_idx(codec, path);
Takashi Iwai2c12c302013-01-10 09:33:29 +01003426 set_pin_target(codec, pin, PIN_IN, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003427 break;
3428 }
3429 }
3430 }
3431}
3432
3433
3434/*
3435 * input MUX handling
3436 */
3437
3438static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur);
3439
3440/* select the given imux item; either unmute exclusively or select the route */
3441static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
3442 unsigned int idx)
3443{
3444 struct hda_gen_spec *spec = codec->spec;
3445 const struct hda_input_mux *imux;
Takashi Iwai55196ff2013-01-24 17:32:56 +01003446 struct nid_path *old_path, *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003447
3448 imux = &spec->input_mux;
3449 if (!imux->num_items)
3450 return 0;
3451
3452 if (idx >= imux->num_items)
3453 idx = imux->num_items - 1;
3454 if (spec->cur_mux[adc_idx] == idx)
3455 return 0;
3456
Takashi Iwai55196ff2013-01-24 17:32:56 +01003457 old_path = get_input_path(codec, adc_idx, spec->cur_mux[adc_idx]);
3458 if (!old_path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003459 return 0;
Takashi Iwai55196ff2013-01-24 17:32:56 +01003460 if (old_path->active)
3461 snd_hda_activate_path(codec, old_path, false, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003462
3463 spec->cur_mux[adc_idx] = idx;
3464
3465 if (spec->shared_mic_hp)
3466 update_shared_mic_hp(codec, spec->cur_mux[adc_idx]);
3467
3468 if (spec->dyn_adc_switch)
3469 dyn_adc_pcm_resetup(codec, idx);
3470
Takashi Iwaic697b712013-01-07 17:09:26 +01003471 path = get_input_path(codec, adc_idx, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003472 if (!path)
3473 return 0;
3474 if (path->active)
3475 return 0;
3476 snd_hda_activate_path(codec, path, true, false);
3477 if (spec->cap_sync_hook)
Takashi Iwaia90229e2013-01-18 14:10:00 +01003478 spec->cap_sync_hook(codec, NULL);
Takashi Iwai55196ff2013-01-24 17:32:56 +01003479 path_power_down_sync(codec, old_path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003480 return 1;
3481}
3482
3483
3484/*
3485 * Jack detections for HP auto-mute and mic-switch
3486 */
3487
3488/* check each pin in the given array; returns true if any of them is plugged */
3489static bool detect_jacks(struct hda_codec *codec, int num_pins, hda_nid_t *pins)
3490{
3491 int i, present = 0;
3492
3493 for (i = 0; i < num_pins; i++) {
3494 hda_nid_t nid = pins[i];
3495 if (!nid)
3496 break;
Takashi Iwai0b4df932013-01-10 09:45:13 +01003497 /* don't detect pins retasked as inputs */
3498 if (snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_IN_EN)
3499 continue;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003500 present |= snd_hda_jack_detect(codec, nid);
3501 }
3502 return present;
3503}
3504
3505/* standard HP/line-out auto-mute helper */
3506static void do_automute(struct hda_codec *codec, int num_pins, hda_nid_t *pins,
Takashi Iwai2c12c302013-01-10 09:33:29 +01003507 bool mute)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003508{
3509 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003510 int i;
3511
3512 for (i = 0; i < num_pins; i++) {
3513 hda_nid_t nid = pins[i];
3514 unsigned int val;
3515 if (!nid)
3516 break;
3517 /* don't reset VREF value in case it's controlling
3518 * the amp (see alc861_fixup_asus_amp_vref_0f())
3519 */
Takashi Iwai2c12c302013-01-10 09:33:29 +01003520 if (spec->keep_vref_in_automute)
3521 val = snd_hda_codec_get_pin_target(codec, nid) & ~PIN_HP;
3522 else
Takashi Iwai352f7f92012-12-19 12:52:06 +01003523 val = 0;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003524 if (!mute)
3525 val |= snd_hda_codec_get_pin_target(codec, nid);
3526 /* here we call update_pin_ctl() so that the pinctl is changed
3527 * without changing the pinctl target value;
3528 * the original target value will be still referred at the
3529 * init / resume again
3530 */
3531 update_pin_ctl(codec, nid, val);
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01003532 set_pin_eapd(codec, nid, !mute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003533 }
3534}
3535
3536/* Toggle outputs muting */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003537void snd_hda_gen_update_outputs(struct hda_codec *codec)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003538{
3539 struct hda_gen_spec *spec = codec->spec;
3540 int on;
3541
3542 /* Control HP pins/amps depending on master_mute state;
3543 * in general, HP pins/amps control should be enabled in all cases,
3544 * but currently set only for master_mute, just to be safe
3545 */
3546 if (!spec->shared_mic_hp) /* don't change HP-pin when shared with mic */
3547 do_automute(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
Takashi Iwai2c12c302013-01-10 09:33:29 +01003548 spec->autocfg.hp_pins, spec->master_mute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003549
3550 if (!spec->automute_speaker)
3551 on = 0;
3552 else
3553 on = spec->hp_jack_present | spec->line_jack_present;
3554 on |= spec->master_mute;
Takashi Iwai47b9ddb2013-01-16 18:18:00 +01003555 spec->speaker_muted = on;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003556 do_automute(codec, ARRAY_SIZE(spec->autocfg.speaker_pins),
Takashi Iwai2c12c302013-01-10 09:33:29 +01003557 spec->autocfg.speaker_pins, on);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003558
3559 /* toggle line-out mutes if needed, too */
3560 /* if LO is a copy of either HP or Speaker, don't need to handle it */
3561 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0] ||
3562 spec->autocfg.line_out_pins[0] == spec->autocfg.speaker_pins[0])
3563 return;
3564 if (!spec->automute_lo)
3565 on = 0;
3566 else
3567 on = spec->hp_jack_present;
3568 on |= spec->master_mute;
Takashi Iwai47b9ddb2013-01-16 18:18:00 +01003569 spec->line_out_muted = on;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003570 do_automute(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
Takashi Iwai2c12c302013-01-10 09:33:29 +01003571 spec->autocfg.line_out_pins, on);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003572}
Takashi Iwai5d550e12012-12-19 15:16:44 +01003573EXPORT_SYMBOL_HDA(snd_hda_gen_update_outputs);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003574
3575static void call_update_outputs(struct hda_codec *codec)
3576{
3577 struct hda_gen_spec *spec = codec->spec;
3578 if (spec->automute_hook)
3579 spec->automute_hook(codec);
3580 else
Takashi Iwai5d550e12012-12-19 15:16:44 +01003581 snd_hda_gen_update_outputs(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003582}
3583
3584/* standard HP-automute helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003585void snd_hda_gen_hp_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003586{
3587 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai92603c52013-01-22 07:46:31 +01003588 hda_nid_t *pins = spec->autocfg.hp_pins;
3589 int num_pins = ARRAY_SIZE(spec->autocfg.hp_pins);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003590
Takashi Iwai92603c52013-01-22 07:46:31 +01003591 /* No detection for the first HP jack during indep-HP mode */
3592 if (spec->indep_hp_enabled) {
3593 pins++;
3594 num_pins--;
3595 }
3596
3597 spec->hp_jack_present = detect_jacks(codec, num_pins, pins);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003598 if (!spec->detect_hp || (!spec->automute_speaker && !spec->automute_lo))
3599 return;
3600 call_update_outputs(codec);
3601}
Takashi Iwai5d550e12012-12-19 15:16:44 +01003602EXPORT_SYMBOL_HDA(snd_hda_gen_hp_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003603
3604/* standard line-out-automute helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003605void snd_hda_gen_line_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003606{
3607 struct hda_gen_spec *spec = codec->spec;
3608
3609 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
3610 return;
3611 /* check LO jack only when it's different from HP */
3612 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0])
3613 return;
3614
3615 spec->line_jack_present =
3616 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
3617 spec->autocfg.line_out_pins);
3618 if (!spec->automute_speaker || !spec->detect_lo)
3619 return;
3620 call_update_outputs(codec);
3621}
Takashi Iwai5d550e12012-12-19 15:16:44 +01003622EXPORT_SYMBOL_HDA(snd_hda_gen_line_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003623
3624/* standard mic auto-switch helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003625void snd_hda_gen_mic_autoswitch(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003626{
3627 struct hda_gen_spec *spec = codec->spec;
3628 int i;
3629
3630 if (!spec->auto_mic)
3631 return;
3632
3633 for (i = spec->am_num_entries - 1; i > 0; i--) {
Takashi Iwai0b4df932013-01-10 09:45:13 +01003634 hda_nid_t pin = spec->am_entry[i].pin;
3635 /* don't detect pins retasked as outputs */
3636 if (snd_hda_codec_get_pin_target(codec, pin) & AC_PINCTL_OUT_EN)
3637 continue;
3638 if (snd_hda_jack_detect(codec, pin)) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003639 mux_select(codec, 0, spec->am_entry[i].idx);
3640 return;
3641 }
3642 }
3643 mux_select(codec, 0, spec->am_entry[0].idx);
3644}
Takashi Iwai5d550e12012-12-19 15:16:44 +01003645EXPORT_SYMBOL_HDA(snd_hda_gen_mic_autoswitch);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003646
Takashi Iwaia5cc2502013-01-16 18:08:55 +01003647/* update jack retasking */
3648static void update_automute_all(struct hda_codec *codec)
3649{
3650 struct hda_gen_spec *spec = codec->spec;
3651
3652 if (spec->hp_automute_hook)
3653 spec->hp_automute_hook(codec, NULL);
3654 else
3655 snd_hda_gen_hp_automute(codec, NULL);
3656 if (spec->line_automute_hook)
3657 spec->line_automute_hook(codec, NULL);
3658 else
3659 snd_hda_gen_line_automute(codec, NULL);
3660 if (spec->mic_autoswitch_hook)
3661 spec->mic_autoswitch_hook(codec, NULL);
3662 else
3663 snd_hda_gen_mic_autoswitch(codec, NULL);
3664}
3665
Takashi Iwai352f7f92012-12-19 12:52:06 +01003666/*
3667 * Auto-Mute mode mixer enum support
3668 */
3669static int automute_mode_info(struct snd_kcontrol *kcontrol,
3670 struct snd_ctl_elem_info *uinfo)
3671{
3672 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3673 struct hda_gen_spec *spec = codec->spec;
3674 static const char * const texts3[] = {
3675 "Disabled", "Speaker Only", "Line Out+Speaker"
Takashi Iwai071c73a2006-08-23 18:34:06 +02003676 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003677
Takashi Iwai352f7f92012-12-19 12:52:06 +01003678 if (spec->automute_speaker_possible && spec->automute_lo_possible)
3679 return snd_hda_enum_helper_info(kcontrol, uinfo, 3, texts3);
3680 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
3681}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003682
Takashi Iwai352f7f92012-12-19 12:52:06 +01003683static int automute_mode_get(struct snd_kcontrol *kcontrol,
3684 struct snd_ctl_elem_value *ucontrol)
3685{
3686 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3687 struct hda_gen_spec *spec = codec->spec;
3688 unsigned int val = 0;
3689 if (spec->automute_speaker)
3690 val++;
3691 if (spec->automute_lo)
3692 val++;
Takashi Iwai071c73a2006-08-23 18:34:06 +02003693
Takashi Iwai352f7f92012-12-19 12:52:06 +01003694 ucontrol->value.enumerated.item[0] = val;
3695 return 0;
3696}
3697
3698static int automute_mode_put(struct snd_kcontrol *kcontrol,
3699 struct snd_ctl_elem_value *ucontrol)
3700{
3701 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3702 struct hda_gen_spec *spec = codec->spec;
3703
3704 switch (ucontrol->value.enumerated.item[0]) {
3705 case 0:
3706 if (!spec->automute_speaker && !spec->automute_lo)
3707 return 0;
3708 spec->automute_speaker = 0;
3709 spec->automute_lo = 0;
3710 break;
3711 case 1:
3712 if (spec->automute_speaker_possible) {
3713 if (!spec->automute_lo && spec->automute_speaker)
3714 return 0;
3715 spec->automute_speaker = 1;
3716 spec->automute_lo = 0;
3717 } else if (spec->automute_lo_possible) {
3718 if (spec->automute_lo)
3719 return 0;
3720 spec->automute_lo = 1;
3721 } else
3722 return -EINVAL;
3723 break;
3724 case 2:
3725 if (!spec->automute_lo_possible || !spec->automute_speaker_possible)
3726 return -EINVAL;
3727 if (spec->automute_speaker && spec->automute_lo)
3728 return 0;
3729 spec->automute_speaker = 1;
3730 spec->automute_lo = 1;
3731 break;
3732 default:
3733 return -EINVAL;
3734 }
3735 call_update_outputs(codec);
3736 return 1;
3737}
3738
3739static const struct snd_kcontrol_new automute_mode_enum = {
3740 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3741 .name = "Auto-Mute Mode",
3742 .info = automute_mode_info,
3743 .get = automute_mode_get,
3744 .put = automute_mode_put,
3745};
3746
3747static int add_automute_mode_enum(struct hda_codec *codec)
3748{
3749 struct hda_gen_spec *spec = codec->spec;
3750
Takashi Iwai12c93df2012-12-19 14:38:33 +01003751 if (!snd_hda_gen_add_kctl(spec, NULL, &automute_mode_enum))
Takashi Iwai352f7f92012-12-19 12:52:06 +01003752 return -ENOMEM;
3753 return 0;
3754}
3755
3756/*
3757 * Check the availability of HP/line-out auto-mute;
3758 * Set up appropriately if really supported
3759 */
3760static int check_auto_mute_availability(struct hda_codec *codec)
3761{
3762 struct hda_gen_spec *spec = codec->spec;
3763 struct auto_pin_cfg *cfg = &spec->autocfg;
3764 int present = 0;
3765 int i, err;
3766
Takashi Iwaif72706b2013-01-16 18:20:07 +01003767 if (spec->suppress_auto_mute)
3768 return 0;
3769
Takashi Iwai352f7f92012-12-19 12:52:06 +01003770 if (cfg->hp_pins[0])
3771 present++;
3772 if (cfg->line_out_pins[0])
3773 present++;
3774 if (cfg->speaker_pins[0])
3775 present++;
3776 if (present < 2) /* need two different output types */
Takashi Iwai071c73a2006-08-23 18:34:06 +02003777 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003778
3779 if (!cfg->speaker_pins[0] &&
3780 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
3781 memcpy(cfg->speaker_pins, cfg->line_out_pins,
3782 sizeof(cfg->speaker_pins));
3783 cfg->speaker_outs = cfg->line_outs;
Takashi Iwai071c73a2006-08-23 18:34:06 +02003784 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003785
Takashi Iwai352f7f92012-12-19 12:52:06 +01003786 if (!cfg->hp_pins[0] &&
3787 cfg->line_out_type == AUTO_PIN_HP_OUT) {
3788 memcpy(cfg->hp_pins, cfg->line_out_pins,
3789 sizeof(cfg->hp_pins));
3790 cfg->hp_outs = cfg->line_outs;
3791 }
3792
3793 for (i = 0; i < cfg->hp_outs; i++) {
3794 hda_nid_t nid = cfg->hp_pins[i];
3795 if (!is_jack_detectable(codec, nid))
3796 continue;
3797 snd_printdd("hda-codec: Enable HP auto-muting on NID 0x%x\n",
3798 nid);
3799 snd_hda_jack_detect_enable_callback(codec, nid, HDA_GEN_HP_EVENT,
Takashi Iwai2e03e952013-01-03 15:55:06 +01003800 spec->hp_automute_hook ?
3801 spec->hp_automute_hook :
Takashi Iwai5d550e12012-12-19 15:16:44 +01003802 snd_hda_gen_hp_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003803 spec->detect_hp = 1;
3804 }
3805
3806 if (cfg->line_out_type == AUTO_PIN_LINE_OUT && cfg->line_outs) {
3807 if (cfg->speaker_outs)
3808 for (i = 0; i < cfg->line_outs; i++) {
3809 hda_nid_t nid = cfg->line_out_pins[i];
3810 if (!is_jack_detectable(codec, nid))
3811 continue;
3812 snd_printdd("hda-codec: Enable Line-Out auto-muting on NID 0x%x\n", nid);
3813 snd_hda_jack_detect_enable_callback(codec, nid,
3814 HDA_GEN_FRONT_EVENT,
Takashi Iwai2e03e952013-01-03 15:55:06 +01003815 spec->line_automute_hook ?
3816 spec->line_automute_hook :
Takashi Iwai5d550e12012-12-19 15:16:44 +01003817 snd_hda_gen_line_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003818 spec->detect_lo = 1;
3819 }
3820 spec->automute_lo_possible = spec->detect_hp;
3821 }
3822
3823 spec->automute_speaker_possible = cfg->speaker_outs &&
3824 (spec->detect_hp || spec->detect_lo);
3825
3826 spec->automute_lo = spec->automute_lo_possible;
3827 spec->automute_speaker = spec->automute_speaker_possible;
3828
3829 if (spec->automute_speaker_possible || spec->automute_lo_possible) {
3830 /* create a control for automute mode */
3831 err = add_automute_mode_enum(codec);
3832 if (err < 0)
3833 return err;
3834 }
3835 return 0;
3836}
3837
Takashi Iwai352f7f92012-12-19 12:52:06 +01003838/* check whether all auto-mic pins are valid; setup indices if OK */
3839static bool auto_mic_check_imux(struct hda_codec *codec)
3840{
3841 struct hda_gen_spec *spec = codec->spec;
3842 const struct hda_input_mux *imux;
3843 int i;
3844
3845 imux = &spec->input_mux;
3846 for (i = 0; i < spec->am_num_entries; i++) {
3847 spec->am_entry[i].idx =
3848 find_idx_in_nid_list(spec->am_entry[i].pin,
3849 spec->imux_pins, imux->num_items);
3850 if (spec->am_entry[i].idx < 0)
3851 return false; /* no corresponding imux */
3852 }
3853
3854 /* we don't need the jack detection for the first pin */
3855 for (i = 1; i < spec->am_num_entries; i++)
3856 snd_hda_jack_detect_enable_callback(codec,
3857 spec->am_entry[i].pin,
3858 HDA_GEN_MIC_EVENT,
Takashi Iwai2e03e952013-01-03 15:55:06 +01003859 spec->mic_autoswitch_hook ?
3860 spec->mic_autoswitch_hook :
Takashi Iwai5d550e12012-12-19 15:16:44 +01003861 snd_hda_gen_mic_autoswitch);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003862 return true;
3863}
3864
3865static int compare_attr(const void *ap, const void *bp)
3866{
3867 const struct automic_entry *a = ap;
3868 const struct automic_entry *b = bp;
3869 return (int)(a->attr - b->attr);
3870}
3871
3872/*
3873 * Check the availability of auto-mic switch;
3874 * Set up if really supported
3875 */
3876static int check_auto_mic_availability(struct hda_codec *codec)
3877{
3878 struct hda_gen_spec *spec = codec->spec;
3879 struct auto_pin_cfg *cfg = &spec->autocfg;
3880 unsigned int types;
3881 int i, num_pins;
3882
Takashi Iwaid12daf62013-01-07 16:32:11 +01003883 if (spec->suppress_auto_mic)
3884 return 0;
3885
Takashi Iwai352f7f92012-12-19 12:52:06 +01003886 types = 0;
3887 num_pins = 0;
3888 for (i = 0; i < cfg->num_inputs; i++) {
3889 hda_nid_t nid = cfg->inputs[i].pin;
3890 unsigned int attr;
3891 attr = snd_hda_codec_get_pincfg(codec, nid);
3892 attr = snd_hda_get_input_pin_attr(attr);
3893 if (types & (1 << attr))
3894 return 0; /* already occupied */
3895 switch (attr) {
3896 case INPUT_PIN_ATTR_INT:
3897 if (cfg->inputs[i].type != AUTO_PIN_MIC)
3898 return 0; /* invalid type */
3899 break;
3900 case INPUT_PIN_ATTR_UNUSED:
3901 return 0; /* invalid entry */
3902 default:
3903 if (cfg->inputs[i].type > AUTO_PIN_LINE_IN)
3904 return 0; /* invalid type */
3905 if (!spec->line_in_auto_switch &&
3906 cfg->inputs[i].type != AUTO_PIN_MIC)
3907 return 0; /* only mic is allowed */
3908 if (!is_jack_detectable(codec, nid))
3909 return 0; /* no unsol support */
3910 break;
3911 }
3912 if (num_pins >= MAX_AUTO_MIC_PINS)
3913 return 0;
3914 types |= (1 << attr);
3915 spec->am_entry[num_pins].pin = nid;
3916 spec->am_entry[num_pins].attr = attr;
3917 num_pins++;
3918 }
3919
3920 if (num_pins < 2)
3921 return 0;
3922
3923 spec->am_num_entries = num_pins;
3924 /* sort the am_entry in the order of attr so that the pin with a
3925 * higher attr will be selected when the jack is plugged.
3926 */
3927 sort(spec->am_entry, num_pins, sizeof(spec->am_entry[0]),
3928 compare_attr, NULL);
3929
3930 if (!auto_mic_check_imux(codec))
3931 return 0;
3932
3933 spec->auto_mic = 1;
3934 spec->num_adc_nids = 1;
3935 spec->cur_mux[0] = spec->am_entry[0].idx;
3936 snd_printdd("hda-codec: Enable auto-mic switch on NID 0x%x/0x%x/0x%x\n",
3937 spec->am_entry[0].pin,
3938 spec->am_entry[1].pin,
3939 spec->am_entry[2].pin);
3940
3941 return 0;
3942}
3943
Takashi Iwai55196ff2013-01-24 17:32:56 +01003944/* power_filter hook; make inactive widgets into power down */
3945static unsigned int snd_hda_gen_path_power_filter(struct hda_codec *codec,
3946 hda_nid_t nid,
3947 unsigned int power_state)
3948{
3949 if (power_state != AC_PWRST_D0)
3950 return power_state;
3951 if (get_wcaps_type(get_wcaps(codec, nid)) >= AC_WID_POWER)
3952 return power_state;
3953 if (is_active_nid(codec, nid, HDA_OUTPUT, 0))
3954 return power_state;
3955 return AC_PWRST_D3;
3956}
3957
Takashi Iwai352f7f92012-12-19 12:52:06 +01003958
Takashi Iwai9eb413e2012-12-19 14:41:21 +01003959/*
3960 * Parse the given BIOS configuration and set up the hda_gen_spec
3961 *
3962 * return 1 if successful, 0 if the proper config is not found,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003963 * or a negative error code
3964 */
3965int snd_hda_gen_parse_auto_config(struct hda_codec *codec,
Takashi Iwai9eb413e2012-12-19 14:41:21 +01003966 struct auto_pin_cfg *cfg)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003967{
3968 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003969 int err;
3970
Takashi Iwai1c70a582013-01-11 17:48:22 +01003971 parse_user_hints(codec);
3972
Takashi Iwaie4a395e2013-01-23 17:00:31 +01003973 if (spec->mixer_nid && !spec->mixer_merge_nid)
3974 spec->mixer_merge_nid = spec->mixer_nid;
3975
Takashi Iwai9eb413e2012-12-19 14:41:21 +01003976 if (cfg != &spec->autocfg) {
3977 spec->autocfg = *cfg;
3978 cfg = &spec->autocfg;
3979 }
3980
David Henningsson6fc4cb92013-01-16 15:58:45 +01003981 fill_all_dac_nids(codec);
3982
Takashi Iwai352f7f92012-12-19 12:52:06 +01003983 if (!cfg->line_outs) {
3984 if (cfg->dig_outs || cfg->dig_in_pin) {
3985 spec->multiout.max_channels = 2;
3986 spec->no_analog = 1;
3987 goto dig_only;
3988 }
3989 return 0; /* can't find valid BIOS pin config */
3990 }
3991
3992 if (!spec->no_primary_hp &&
3993 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT &&
3994 cfg->line_outs <= cfg->hp_outs) {
3995 /* use HP as primary out */
3996 cfg->speaker_outs = cfg->line_outs;
3997 memcpy(cfg->speaker_pins, cfg->line_out_pins,
3998 sizeof(cfg->speaker_pins));
3999 cfg->line_outs = cfg->hp_outs;
4000 memcpy(cfg->line_out_pins, cfg->hp_pins, sizeof(cfg->hp_pins));
4001 cfg->hp_outs = 0;
4002 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
4003 cfg->line_out_type = AUTO_PIN_HP_OUT;
4004 }
4005
4006 err = parse_output_paths(codec);
4007 if (err < 0)
4008 return err;
4009 err = create_multi_channel_mode(codec);
4010 if (err < 0)
4011 return err;
4012 err = create_multi_out_ctls(codec, cfg);
4013 if (err < 0)
4014 return err;
4015 err = create_hp_out_ctls(codec);
4016 if (err < 0)
4017 return err;
4018 err = create_speaker_out_ctls(codec);
4019 if (err < 0)
4020 return err;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004021 err = create_indep_hp_ctls(codec);
4022 if (err < 0)
4023 return err;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01004024 err = create_loopback_mixing_ctl(codec);
4025 if (err < 0)
4026 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004027 err = create_shared_input(codec);
4028 if (err < 0)
4029 return err;
4030 err = create_input_ctls(codec);
Takashi Iwaid13bd412008-07-30 15:01:45 +02004031 if (err < 0)
Takashi Iwai071c73a2006-08-23 18:34:06 +02004032 return err;
4033
Takashi Iwaia07a9492013-01-07 16:44:06 +01004034 spec->const_channel_count = spec->ext_channel_count;
4035 /* check the multiple speaker and headphone pins */
4036 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
4037 spec->const_channel_count = max(spec->const_channel_count,
4038 cfg->speaker_outs * 2);
4039 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
4040 spec->const_channel_count = max(spec->const_channel_count,
4041 cfg->hp_outs * 2);
4042 spec->multiout.max_channels = max(spec->ext_channel_count,
4043 spec->const_channel_count);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004044
4045 err = check_auto_mute_availability(codec);
4046 if (err < 0)
4047 return err;
4048
4049 err = check_dyn_adc_switch(codec);
4050 if (err < 0)
4051 return err;
4052
4053 if (!spec->shared_mic_hp) {
4054 err = check_auto_mic_availability(codec);
Takashi Iwaid13bd412008-07-30 15:01:45 +02004055 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004056 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004057 }
Takashi Iwai071c73a2006-08-23 18:34:06 +02004058
Takashi Iwai352f7f92012-12-19 12:52:06 +01004059 err = create_capture_mixers(codec);
4060 if (err < 0)
4061 return err;
4062
4063 err = parse_mic_boost(codec);
4064 if (err < 0)
4065 return err;
4066
Takashi Iwai978e77e2013-01-10 16:57:58 +01004067 if (spec->add_out_jack_modes) {
4068 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
4069 err = create_out_jack_modes(codec, cfg->line_outs,
4070 cfg->line_out_pins);
4071 if (err < 0)
4072 return err;
4073 }
4074 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
4075 err = create_out_jack_modes(codec, cfg->hp_outs,
4076 cfg->hp_pins);
4077 if (err < 0)
4078 return err;
4079 }
4080 }
4081
Takashi Iwai352f7f92012-12-19 12:52:06 +01004082 dig_only:
4083 parse_digital(codec);
4084
Takashi Iwai55196ff2013-01-24 17:32:56 +01004085 if (spec->power_down_unused)
4086 codec->power_filter = snd_hda_gen_path_power_filter;
4087
Takashi Iwai352f7f92012-12-19 12:52:06 +01004088 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004089}
Takashi Iwai352f7f92012-12-19 12:52:06 +01004090EXPORT_SYMBOL_HDA(snd_hda_gen_parse_auto_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004091
4092
4093/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01004094 * Build control elements
Linus Torvalds1da177e2005-04-16 15:20:36 -07004095 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01004096
4097/* slave controls for virtual master */
4098static const char * const slave_pfxs[] = {
4099 "Front", "Surround", "Center", "LFE", "Side",
4100 "Headphone", "Speaker", "Mono", "Line Out",
4101 "CLFE", "Bass Speaker", "PCM",
Takashi Iwaiee79c692013-01-07 09:57:42 +01004102 "Speaker Front", "Speaker Surround", "Speaker CLFE", "Speaker Side",
4103 "Headphone Front", "Headphone Surround", "Headphone CLFE",
4104 "Headphone Side",
Takashi Iwai352f7f92012-12-19 12:52:06 +01004105 NULL,
4106};
4107
4108int snd_hda_gen_build_controls(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004109{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004110 struct hda_gen_spec *spec = codec->spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004111 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004112
Takashi Iwai36502d02012-12-19 15:15:10 +01004113 if (spec->kctls.used) {
4114 err = snd_hda_add_new_ctls(codec, spec->kctls.list);
4115 if (err < 0)
4116 return err;
4117 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004118
Takashi Iwai352f7f92012-12-19 12:52:06 +01004119 if (spec->multiout.dig_out_nid) {
4120 err = snd_hda_create_dig_out_ctls(codec,
4121 spec->multiout.dig_out_nid,
4122 spec->multiout.dig_out_nid,
4123 spec->pcm_rec[1].pcm_type);
4124 if (err < 0)
4125 return err;
4126 if (!spec->no_analog) {
4127 err = snd_hda_create_spdif_share_sw(codec,
4128 &spec->multiout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004129 if (err < 0)
4130 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004131 spec->multiout.share_spdif = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004132 }
4133 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01004134 if (spec->dig_in_nid) {
4135 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
4136 if (err < 0)
4137 return err;
4138 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004139
Takashi Iwai352f7f92012-12-19 12:52:06 +01004140 /* if we have no master control, let's create it */
4141 if (!spec->no_analog &&
4142 !snd_hda_find_mixer_ctl(codec, "Master Playback Volume")) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01004143 err = snd_hda_add_vmaster(codec, "Master Playback Volume",
Takashi Iwai7a71bbf2013-01-17 10:25:15 +01004144 spec->vmaster_tlv, slave_pfxs,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004145 "Playback Volume");
4146 if (err < 0)
4147 return err;
4148 }
4149 if (!spec->no_analog &&
4150 !snd_hda_find_mixer_ctl(codec, "Master Playback Switch")) {
4151 err = __snd_hda_add_vmaster(codec, "Master Playback Switch",
4152 NULL, slave_pfxs,
4153 "Playback Switch",
4154 true, &spec->vmaster_mute.sw_kctl);
4155 if (err < 0)
4156 return err;
4157 if (spec->vmaster_mute.hook)
Takashi Iwaifd25a972012-12-20 14:57:18 +01004158 snd_hda_add_vmaster_hook(codec, &spec->vmaster_mute,
4159 spec->vmaster_mute_enum);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004160 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004161
Takashi Iwai352f7f92012-12-19 12:52:06 +01004162 free_kctls(spec); /* no longer needed */
4163
4164 if (spec->shared_mic_hp) {
4165 int err;
4166 int nid = spec->autocfg.inputs[1].pin;
4167 err = snd_hda_jack_add_kctl(codec, nid, "Headphone Mic", 0);
4168 if (err < 0)
4169 return err;
4170 err = snd_hda_jack_detect_enable(codec, nid, 0);
4171 if (err < 0)
4172 return err;
4173 }
4174
4175 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
4176 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004177 return err;
4178
4179 return 0;
4180}
Takashi Iwai352f7f92012-12-19 12:52:06 +01004181EXPORT_SYMBOL_HDA(snd_hda_gen_build_controls);
4182
Linus Torvalds1da177e2005-04-16 15:20:36 -07004183
4184/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01004185 * PCM definitions
Linus Torvalds1da177e2005-04-16 15:20:36 -07004186 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004187
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004188static void call_pcm_playback_hook(struct hda_pcm_stream *hinfo,
4189 struct hda_codec *codec,
4190 struct snd_pcm_substream *substream,
4191 int action)
4192{
4193 struct hda_gen_spec *spec = codec->spec;
4194 if (spec->pcm_playback_hook)
4195 spec->pcm_playback_hook(hinfo, codec, substream, action);
4196}
4197
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004198static void call_pcm_capture_hook(struct hda_pcm_stream *hinfo,
4199 struct hda_codec *codec,
4200 struct snd_pcm_substream *substream,
4201 int action)
4202{
4203 struct hda_gen_spec *spec = codec->spec;
4204 if (spec->pcm_capture_hook)
4205 spec->pcm_capture_hook(hinfo, codec, substream, action);
4206}
4207
Takashi Iwai352f7f92012-12-19 12:52:06 +01004208/*
4209 * Analog playback callbacks
4210 */
4211static int playback_pcm_open(struct hda_pcm_stream *hinfo,
4212 struct hda_codec *codec,
4213 struct snd_pcm_substream *substream)
4214{
4215 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004216 int err;
4217
4218 mutex_lock(&spec->pcm_mutex);
4219 err = snd_hda_multi_out_analog_open(codec,
4220 &spec->multiout, substream,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004221 hinfo);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004222 if (!err) {
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004223 spec->active_streams |= 1 << STREAM_MULTI_OUT;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004224 call_pcm_playback_hook(hinfo, codec, substream,
4225 HDA_GEN_PCM_ACT_OPEN);
4226 }
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004227 mutex_unlock(&spec->pcm_mutex);
4228 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004229}
4230
4231static int playback_pcm_prepare(struct hda_pcm_stream *hinfo,
Takashi Iwai97ec5582006-03-21 11:29:07 +01004232 struct hda_codec *codec,
4233 unsigned int stream_tag,
4234 unsigned int format,
4235 struct snd_pcm_substream *substream)
4236{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004237 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004238 int err;
4239
4240 err = snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
4241 stream_tag, format, substream);
4242 if (!err)
4243 call_pcm_playback_hook(hinfo, codec, substream,
4244 HDA_GEN_PCM_ACT_PREPARE);
4245 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004246}
Takashi Iwai97ec5582006-03-21 11:29:07 +01004247
Takashi Iwai352f7f92012-12-19 12:52:06 +01004248static int playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
4249 struct hda_codec *codec,
4250 struct snd_pcm_substream *substream)
4251{
4252 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004253 int err;
4254
4255 err = snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
4256 if (!err)
4257 call_pcm_playback_hook(hinfo, codec, substream,
4258 HDA_GEN_PCM_ACT_CLEANUP);
4259 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004260}
4261
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004262static int playback_pcm_close(struct hda_pcm_stream *hinfo,
4263 struct hda_codec *codec,
4264 struct snd_pcm_substream *substream)
4265{
4266 struct hda_gen_spec *spec = codec->spec;
4267 mutex_lock(&spec->pcm_mutex);
4268 spec->active_streams &= ~(1 << STREAM_MULTI_OUT);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004269 call_pcm_playback_hook(hinfo, codec, substream,
4270 HDA_GEN_PCM_ACT_CLOSE);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004271 mutex_unlock(&spec->pcm_mutex);
4272 return 0;
4273}
4274
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004275static int capture_pcm_open(struct hda_pcm_stream *hinfo,
4276 struct hda_codec *codec,
4277 struct snd_pcm_substream *substream)
4278{
4279 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_OPEN);
4280 return 0;
4281}
4282
4283static int capture_pcm_prepare(struct hda_pcm_stream *hinfo,
4284 struct hda_codec *codec,
4285 unsigned int stream_tag,
4286 unsigned int format,
4287 struct snd_pcm_substream *substream)
4288{
4289 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4290 call_pcm_capture_hook(hinfo, codec, substream,
4291 HDA_GEN_PCM_ACT_PREPARE);
4292 return 0;
4293}
4294
4295static int capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
4296 struct hda_codec *codec,
4297 struct snd_pcm_substream *substream)
4298{
4299 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
4300 call_pcm_capture_hook(hinfo, codec, substream,
4301 HDA_GEN_PCM_ACT_CLEANUP);
4302 return 0;
4303}
4304
4305static int capture_pcm_close(struct hda_pcm_stream *hinfo,
4306 struct hda_codec *codec,
4307 struct snd_pcm_substream *substream)
4308{
4309 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_CLOSE);
4310 return 0;
4311}
4312
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004313static int alt_playback_pcm_open(struct hda_pcm_stream *hinfo,
4314 struct hda_codec *codec,
4315 struct snd_pcm_substream *substream)
4316{
4317 struct hda_gen_spec *spec = codec->spec;
4318 int err = 0;
4319
4320 mutex_lock(&spec->pcm_mutex);
4321 if (!spec->indep_hp_enabled)
4322 err = -EBUSY;
4323 else
4324 spec->active_streams |= 1 << STREAM_INDEP_HP;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004325 call_pcm_playback_hook(hinfo, codec, substream,
4326 HDA_GEN_PCM_ACT_OPEN);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004327 mutex_unlock(&spec->pcm_mutex);
4328 return err;
4329}
4330
4331static int alt_playback_pcm_close(struct hda_pcm_stream *hinfo,
4332 struct hda_codec *codec,
4333 struct snd_pcm_substream *substream)
4334{
4335 struct hda_gen_spec *spec = codec->spec;
4336 mutex_lock(&spec->pcm_mutex);
4337 spec->active_streams &= ~(1 << STREAM_INDEP_HP);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004338 call_pcm_playback_hook(hinfo, codec, substream,
4339 HDA_GEN_PCM_ACT_CLOSE);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004340 mutex_unlock(&spec->pcm_mutex);
4341 return 0;
4342}
4343
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004344static int alt_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
4345 struct hda_codec *codec,
4346 unsigned int stream_tag,
4347 unsigned int format,
4348 struct snd_pcm_substream *substream)
4349{
4350 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4351 call_pcm_playback_hook(hinfo, codec, substream,
4352 HDA_GEN_PCM_ACT_PREPARE);
4353 return 0;
4354}
4355
4356static int alt_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
4357 struct hda_codec *codec,
4358 struct snd_pcm_substream *substream)
4359{
4360 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
4361 call_pcm_playback_hook(hinfo, codec, substream,
4362 HDA_GEN_PCM_ACT_CLEANUP);
4363 return 0;
4364}
4365
Takashi Iwai352f7f92012-12-19 12:52:06 +01004366/*
4367 * Digital out
4368 */
4369static int dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
4370 struct hda_codec *codec,
4371 struct snd_pcm_substream *substream)
4372{
4373 struct hda_gen_spec *spec = codec->spec;
4374 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
4375}
4376
4377static int dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
4378 struct hda_codec *codec,
4379 unsigned int stream_tag,
4380 unsigned int format,
4381 struct snd_pcm_substream *substream)
4382{
4383 struct hda_gen_spec *spec = codec->spec;
4384 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
4385 stream_tag, format, substream);
4386}
4387
4388static int dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
4389 struct hda_codec *codec,
4390 struct snd_pcm_substream *substream)
4391{
4392 struct hda_gen_spec *spec = codec->spec;
4393 return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
4394}
4395
4396static int dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
4397 struct hda_codec *codec,
4398 struct snd_pcm_substream *substream)
4399{
4400 struct hda_gen_spec *spec = codec->spec;
4401 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
4402}
4403
4404/*
4405 * Analog capture
4406 */
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004407#define alt_capture_pcm_open capture_pcm_open
4408#define alt_capture_pcm_close capture_pcm_close
4409
Takashi Iwai352f7f92012-12-19 12:52:06 +01004410static int alt_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
4411 struct hda_codec *codec,
4412 unsigned int stream_tag,
4413 unsigned int format,
4414 struct snd_pcm_substream *substream)
4415{
4416 struct hda_gen_spec *spec = codec->spec;
4417
4418 snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number + 1],
Takashi Iwai97ec5582006-03-21 11:29:07 +01004419 stream_tag, 0, format);
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004420 call_pcm_capture_hook(hinfo, codec, substream,
4421 HDA_GEN_PCM_ACT_PREPARE);
Takashi Iwai97ec5582006-03-21 11:29:07 +01004422 return 0;
4423}
4424
Takashi Iwai352f7f92012-12-19 12:52:06 +01004425static int alt_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
4426 struct hda_codec *codec,
4427 struct snd_pcm_substream *substream)
Takashi Iwai97ec5582006-03-21 11:29:07 +01004428{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004429 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai97ec5582006-03-21 11:29:07 +01004430
Takashi Iwai352f7f92012-12-19 12:52:06 +01004431 snd_hda_codec_cleanup_stream(codec,
4432 spec->adc_nids[substream->number + 1]);
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004433 call_pcm_capture_hook(hinfo, codec, substream,
4434 HDA_GEN_PCM_ACT_CLEANUP);
Takashi Iwai97ec5582006-03-21 11:29:07 +01004435 return 0;
4436}
4437
Takashi Iwai352f7f92012-12-19 12:52:06 +01004438/*
4439 */
4440static const struct hda_pcm_stream pcm_analog_playback = {
4441 .substreams = 1,
4442 .channels_min = 2,
4443 .channels_max = 8,
4444 /* NID is set in build_pcms */
4445 .ops = {
4446 .open = playback_pcm_open,
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004447 .close = playback_pcm_close,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004448 .prepare = playback_pcm_prepare,
4449 .cleanup = playback_pcm_cleanup
4450 },
4451};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004452
Takashi Iwai352f7f92012-12-19 12:52:06 +01004453static const struct hda_pcm_stream pcm_analog_capture = {
4454 .substreams = 1,
4455 .channels_min = 2,
4456 .channels_max = 2,
4457 /* NID is set in build_pcms */
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004458 .ops = {
4459 .open = capture_pcm_open,
4460 .close = capture_pcm_close,
4461 .prepare = capture_pcm_prepare,
4462 .cleanup = capture_pcm_cleanup
4463 },
Takashi Iwai352f7f92012-12-19 12:52:06 +01004464};
4465
4466static const struct hda_pcm_stream pcm_analog_alt_playback = {
4467 .substreams = 1,
4468 .channels_min = 2,
4469 .channels_max = 2,
4470 /* NID is set in build_pcms */
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004471 .ops = {
4472 .open = alt_playback_pcm_open,
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004473 .close = alt_playback_pcm_close,
4474 .prepare = alt_playback_pcm_prepare,
4475 .cleanup = alt_playback_pcm_cleanup
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004476 },
Takashi Iwai352f7f92012-12-19 12:52:06 +01004477};
4478
4479static const struct hda_pcm_stream pcm_analog_alt_capture = {
4480 .substreams = 2, /* can be overridden */
4481 .channels_min = 2,
4482 .channels_max = 2,
4483 /* NID is set in build_pcms */
4484 .ops = {
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004485 .open = alt_capture_pcm_open,
4486 .close = alt_capture_pcm_close,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004487 .prepare = alt_capture_pcm_prepare,
4488 .cleanup = alt_capture_pcm_cleanup
4489 },
4490};
4491
4492static const struct hda_pcm_stream pcm_digital_playback = {
4493 .substreams = 1,
4494 .channels_min = 2,
4495 .channels_max = 2,
4496 /* NID is set in build_pcms */
4497 .ops = {
4498 .open = dig_playback_pcm_open,
4499 .close = dig_playback_pcm_close,
4500 .prepare = dig_playback_pcm_prepare,
4501 .cleanup = dig_playback_pcm_cleanup
4502 },
4503};
4504
4505static const struct hda_pcm_stream pcm_digital_capture = {
4506 .substreams = 1,
4507 .channels_min = 2,
4508 .channels_max = 2,
4509 /* NID is set in build_pcms */
4510};
4511
4512/* Used by build_pcms to flag that a PCM has no playback stream */
4513static const struct hda_pcm_stream pcm_null_stream = {
4514 .substreams = 0,
4515 .channels_min = 0,
4516 .channels_max = 0,
4517};
4518
4519/*
4520 * dynamic changing ADC PCM streams
4521 */
4522static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur)
4523{
4524 struct hda_gen_spec *spec = codec->spec;
4525 hda_nid_t new_adc = spec->adc_nids[spec->dyn_adc_idx[cur]];
4526
4527 if (spec->cur_adc && spec->cur_adc != new_adc) {
4528 /* stream is running, let's swap the current ADC */
4529 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
4530 spec->cur_adc = new_adc;
4531 snd_hda_codec_setup_stream(codec, new_adc,
4532 spec->cur_adc_stream_tag, 0,
4533 spec->cur_adc_format);
4534 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004535 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01004536 return false;
4537}
4538
4539/* analog capture with dynamic dual-adc changes */
4540static int dyn_adc_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
4541 struct hda_codec *codec,
4542 unsigned int stream_tag,
4543 unsigned int format,
4544 struct snd_pcm_substream *substream)
4545{
4546 struct hda_gen_spec *spec = codec->spec;
4547 spec->cur_adc = spec->adc_nids[spec->dyn_adc_idx[spec->cur_mux[0]]];
4548 spec->cur_adc_stream_tag = stream_tag;
4549 spec->cur_adc_format = format;
4550 snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
4551 return 0;
4552}
4553
4554static int dyn_adc_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
4555 struct hda_codec *codec,
4556 struct snd_pcm_substream *substream)
4557{
4558 struct hda_gen_spec *spec = codec->spec;
4559 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
4560 spec->cur_adc = 0;
4561 return 0;
4562}
4563
4564static const struct hda_pcm_stream dyn_adc_pcm_analog_capture = {
4565 .substreams = 1,
4566 .channels_min = 2,
4567 .channels_max = 2,
4568 .nid = 0, /* fill later */
4569 .ops = {
4570 .prepare = dyn_adc_capture_pcm_prepare,
4571 .cleanup = dyn_adc_capture_pcm_cleanup
4572 },
4573};
4574
Takashi Iwaif873e532012-12-20 16:58:39 +01004575static void fill_pcm_stream_name(char *str, size_t len, const char *sfx,
4576 const char *chip_name)
4577{
4578 char *p;
4579
4580 if (*str)
4581 return;
4582 strlcpy(str, chip_name, len);
4583
4584 /* drop non-alnum chars after a space */
4585 for (p = strchr(str, ' '); p; p = strchr(p + 1, ' ')) {
4586 if (!isalnum(p[1])) {
4587 *p = 0;
4588 break;
4589 }
4590 }
4591 strlcat(str, sfx, len);
4592}
4593
Takashi Iwai352f7f92012-12-19 12:52:06 +01004594/* build PCM streams based on the parsed results */
4595int snd_hda_gen_build_pcms(struct hda_codec *codec)
4596{
4597 struct hda_gen_spec *spec = codec->spec;
4598 struct hda_pcm *info = spec->pcm_rec;
4599 const struct hda_pcm_stream *p;
4600 bool have_multi_adcs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004601
4602 codec->num_pcms = 1;
4603 codec->pcm_info = info;
4604
Takashi Iwai352f7f92012-12-19 12:52:06 +01004605 if (spec->no_analog)
4606 goto skip_analog;
4607
Takashi Iwaif873e532012-12-20 16:58:39 +01004608 fill_pcm_stream_name(spec->stream_name_analog,
4609 sizeof(spec->stream_name_analog),
4610 " Analog", codec->chip_name);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004611 info->name = spec->stream_name_analog;
4612
4613 if (spec->multiout.num_dacs > 0) {
4614 p = spec->stream_analog_playback;
4615 if (!p)
4616 p = &pcm_analog_playback;
4617 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
4618 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dac_nids[0];
4619 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
4620 spec->multiout.max_channels;
4621 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT &&
4622 spec->autocfg.line_outs == 2)
4623 info->stream[SNDRV_PCM_STREAM_PLAYBACK].chmap =
4624 snd_pcm_2_1_chmaps;
4625 }
4626 if (spec->num_adc_nids) {
4627 p = spec->stream_analog_capture;
4628 if (!p) {
4629 if (spec->dyn_adc_switch)
4630 p = &dyn_adc_pcm_analog_capture;
4631 else
4632 p = &pcm_analog_capture;
4633 }
4634 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
4635 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adc_nids[0];
4636 }
4637
Takashi Iwai352f7f92012-12-19 12:52:06 +01004638 skip_analog:
4639 /* SPDIF for stream index #1 */
4640 if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
Takashi Iwaif873e532012-12-20 16:58:39 +01004641 fill_pcm_stream_name(spec->stream_name_digital,
4642 sizeof(spec->stream_name_digital),
4643 " Digital", codec->chip_name);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004644 codec->num_pcms = 2;
4645 codec->slave_dig_outs = spec->multiout.slave_dig_outs;
4646 info = spec->pcm_rec + 1;
4647 info->name = spec->stream_name_digital;
4648 if (spec->dig_out_type)
4649 info->pcm_type = spec->dig_out_type;
4650 else
4651 info->pcm_type = HDA_PCM_TYPE_SPDIF;
4652 if (spec->multiout.dig_out_nid) {
4653 p = spec->stream_digital_playback;
4654 if (!p)
4655 p = &pcm_digital_playback;
4656 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
4657 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dig_out_nid;
4658 }
4659 if (spec->dig_in_nid) {
4660 p = spec->stream_digital_capture;
4661 if (!p)
4662 p = &pcm_digital_capture;
4663 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
4664 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in_nid;
4665 }
4666 }
4667
4668 if (spec->no_analog)
4669 return 0;
4670
4671 /* If the use of more than one ADC is requested for the current
4672 * model, configure a second analog capture-only PCM.
4673 */
4674 have_multi_adcs = (spec->num_adc_nids > 1) &&
4675 !spec->dyn_adc_switch && !spec->auto_mic;
4676 /* Additional Analaog capture for index #2 */
4677 if (spec->alt_dac_nid || have_multi_adcs) {
Takashi Iwaia6071482013-01-21 16:50:09 +01004678 fill_pcm_stream_name(spec->stream_name_alt_analog,
4679 sizeof(spec->stream_name_alt_analog),
4680 " Alt Analog", codec->chip_name);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004681 codec->num_pcms = 3;
4682 info = spec->pcm_rec + 2;
Takashi Iwaia6071482013-01-21 16:50:09 +01004683 info->name = spec->stream_name_alt_analog;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004684 if (spec->alt_dac_nid) {
4685 p = spec->stream_analog_alt_playback;
4686 if (!p)
4687 p = &pcm_analog_alt_playback;
4688 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
4689 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
4690 spec->alt_dac_nid;
4691 } else {
4692 info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
4693 pcm_null_stream;
4694 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = 0;
4695 }
4696 if (have_multi_adcs) {
4697 p = spec->stream_analog_alt_capture;
4698 if (!p)
4699 p = &pcm_analog_alt_capture;
4700 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
4701 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
4702 spec->adc_nids[1];
4703 info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams =
4704 spec->num_adc_nids - 1;
4705 } else {
4706 info->stream[SNDRV_PCM_STREAM_CAPTURE] =
4707 pcm_null_stream;
4708 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = 0;
4709 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004710 }
4711
4712 return 0;
4713}
Takashi Iwai352f7f92012-12-19 12:52:06 +01004714EXPORT_SYMBOL_HDA(snd_hda_gen_build_pcms);
4715
4716
4717/*
4718 * Standard auto-parser initializations
4719 */
4720
Takashi Iwaid4156932013-01-07 10:08:02 +01004721/* configure the given path as a proper output */
Takashi Iwai2c12c302013-01-10 09:33:29 +01004722static void set_output_and_unmute(struct hda_codec *codec, int path_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004723{
4724 struct nid_path *path;
Takashi Iwaid4156932013-01-07 10:08:02 +01004725 hda_nid_t pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004726
Takashi Iwai196c17662013-01-04 15:01:40 +01004727 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwaid4156932013-01-07 10:08:02 +01004728 if (!path || !path->depth)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004729 return;
Takashi Iwaid4156932013-01-07 10:08:02 +01004730 pin = path->path[path->depth - 1];
Takashi Iwai2c12c302013-01-10 09:33:29 +01004731 restore_pin_ctl(codec, pin);
Takashi Iwaie1284af2013-01-03 16:33:02 +01004732 snd_hda_activate_path(codec, path, path->active, true);
4733 set_pin_eapd(codec, pin, path->active);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004734}
4735
4736/* initialize primary output paths */
4737static void init_multi_out(struct hda_codec *codec)
4738{
4739 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004740 int i;
4741
Takashi Iwaid4156932013-01-07 10:08:02 +01004742 for (i = 0; i < spec->autocfg.line_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01004743 set_output_and_unmute(codec, spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004744}
4745
Takashi Iwaidb23fd12012-12-20 15:27:24 +01004746
Takashi Iwai2c12c302013-01-10 09:33:29 +01004747static void __init_extra_out(struct hda_codec *codec, int num_outs, int *paths)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004748{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004749 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004750
Takashi Iwaid4156932013-01-07 10:08:02 +01004751 for (i = 0; i < num_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01004752 set_output_and_unmute(codec, paths[i]);
Takashi Iwaidb23fd12012-12-20 15:27:24 +01004753}
4754
4755/* initialize hp and speaker paths */
4756static void init_extra_out(struct hda_codec *codec)
4757{
4758 struct hda_gen_spec *spec = codec->spec;
4759
4760 if (spec->autocfg.line_out_type != AUTO_PIN_HP_OUT)
Takashi Iwai2c12c302013-01-10 09:33:29 +01004761 __init_extra_out(codec, spec->autocfg.hp_outs, spec->hp_paths);
Takashi Iwaidb23fd12012-12-20 15:27:24 +01004762 if (spec->autocfg.line_out_type != AUTO_PIN_SPEAKER_OUT)
4763 __init_extra_out(codec, spec->autocfg.speaker_outs,
Takashi Iwai2c12c302013-01-10 09:33:29 +01004764 spec->speaker_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004765}
4766
4767/* initialize multi-io paths */
4768static void init_multi_io(struct hda_codec *codec)
4769{
4770 struct hda_gen_spec *spec = codec->spec;
4771 int i;
4772
4773 for (i = 0; i < spec->multi_ios; i++) {
4774 hda_nid_t pin = spec->multi_io[i].pin;
4775 struct nid_path *path;
Takashi Iwai196c17662013-01-04 15:01:40 +01004776 path = get_multiio_path(codec, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004777 if (!path)
4778 continue;
4779 if (!spec->multi_io[i].ctl_in)
4780 spec->multi_io[i].ctl_in =
Takashi Iwai2c12c302013-01-10 09:33:29 +01004781 snd_hda_codec_get_pin_target(codec, pin);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004782 snd_hda_activate_path(codec, path, path->active, true);
4783 }
4784}
4785
Takashi Iwai352f7f92012-12-19 12:52:06 +01004786/* set up input pins and loopback paths */
4787static void init_analog_input(struct hda_codec *codec)
4788{
4789 struct hda_gen_spec *spec = codec->spec;
4790 struct auto_pin_cfg *cfg = &spec->autocfg;
4791 int i;
4792
4793 for (i = 0; i < cfg->num_inputs; i++) {
4794 hda_nid_t nid = cfg->inputs[i].pin;
4795 if (is_input_pin(codec, nid))
Takashi Iwai2c12c302013-01-10 09:33:29 +01004796 restore_pin_ctl(codec, nid);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004797
4798 /* init loopback inputs */
4799 if (spec->mixer_nid) {
Takashi Iwai3e367f12013-01-23 17:07:23 +01004800 resume_path_from_idx(codec, spec->loopback_paths[i]);
4801 resume_path_from_idx(codec, spec->loopback_merge_path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004802 }
4803 }
4804}
4805
4806/* initialize ADC paths */
4807static void init_input_src(struct hda_codec *codec)
4808{
4809 struct hda_gen_spec *spec = codec->spec;
4810 struct hda_input_mux *imux = &spec->input_mux;
4811 struct nid_path *path;
4812 int i, c, nums;
4813
4814 if (spec->dyn_adc_switch)
4815 nums = 1;
4816 else
4817 nums = spec->num_adc_nids;
4818
4819 for (c = 0; c < nums; c++) {
4820 for (i = 0; i < imux->num_items; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01004821 path = get_input_path(codec, c, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004822 if (path) {
4823 bool active = path->active;
4824 if (i == spec->cur_mux[c])
4825 active = true;
4826 snd_hda_activate_path(codec, path, active, false);
4827 }
4828 }
4829 }
4830
4831 if (spec->shared_mic_hp)
4832 update_shared_mic_hp(codec, spec->cur_mux[0]);
4833
4834 if (spec->cap_sync_hook)
Takashi Iwaia90229e2013-01-18 14:10:00 +01004835 spec->cap_sync_hook(codec, NULL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004836}
4837
4838/* set right pin controls for digital I/O */
4839static void init_digital(struct hda_codec *codec)
4840{
4841 struct hda_gen_spec *spec = codec->spec;
4842 int i;
4843 hda_nid_t pin;
4844
Takashi Iwaid4156932013-01-07 10:08:02 +01004845 for (i = 0; i < spec->autocfg.dig_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01004846 set_output_and_unmute(codec, spec->digout_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004847 pin = spec->autocfg.dig_in_pin;
Takashi Iwai2430d7b2013-01-04 15:09:42 +01004848 if (pin) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01004849 restore_pin_ctl(codec, pin);
Takashi Iwai3e367f12013-01-23 17:07:23 +01004850 resume_path_from_idx(codec, spec->digin_path);
Takashi Iwai2430d7b2013-01-04 15:09:42 +01004851 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01004852}
4853
Takashi Iwai973e4972012-12-20 15:16:09 +01004854/* clear unsol-event tags on unused pins; Conexant codecs seem to leave
4855 * invalid unsol tags by some reason
4856 */
4857static void clear_unsol_on_unused_pins(struct hda_codec *codec)
4858{
4859 int i;
4860
4861 for (i = 0; i < codec->init_pins.used; i++) {
4862 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
4863 hda_nid_t nid = pin->nid;
4864 if (is_jack_detectable(codec, nid) &&
4865 !snd_hda_jack_tbl_get(codec, nid))
4866 snd_hda_codec_update_cache(codec, nid, 0,
4867 AC_VERB_SET_UNSOLICITED_ENABLE, 0);
4868 }
4869}
4870
Takashi Iwai5187ac12013-01-07 12:52:16 +01004871/*
4872 * initialize the generic spec;
4873 * this can be put as patch_ops.init function
4874 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01004875int snd_hda_gen_init(struct hda_codec *codec)
4876{
4877 struct hda_gen_spec *spec = codec->spec;
4878
4879 if (spec->init_hook)
4880 spec->init_hook(codec);
4881
4882 snd_hda_apply_verbs(codec);
4883
Takashi Iwai3bbcd272012-12-20 11:50:58 +01004884 codec->cached_write = 1;
4885
Takashi Iwai352f7f92012-12-19 12:52:06 +01004886 init_multi_out(codec);
4887 init_extra_out(codec);
4888 init_multi_io(codec);
4889 init_analog_input(codec);
4890 init_input_src(codec);
4891 init_digital(codec);
4892
Takashi Iwai973e4972012-12-20 15:16:09 +01004893 clear_unsol_on_unused_pins(codec);
4894
Takashi Iwai352f7f92012-12-19 12:52:06 +01004895 /* call init functions of standard auto-mute helpers */
Takashi Iwaia5cc2502013-01-16 18:08:55 +01004896 update_automute_all(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004897
Takashi Iwaidc870f32013-01-22 15:24:30 +01004898 snd_hda_codec_flush_cache(codec);
Takashi Iwai3bbcd272012-12-20 11:50:58 +01004899
Takashi Iwai352f7f92012-12-19 12:52:06 +01004900 if (spec->vmaster_mute.sw_kctl && spec->vmaster_mute.hook)
4901 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
4902
4903 hda_call_check_power_status(codec, 0x01);
4904 return 0;
4905}
Takashi Iwaifce52a32013-01-07 12:42:48 +01004906EXPORT_SYMBOL_HDA(snd_hda_gen_init);
4907
Takashi Iwai5187ac12013-01-07 12:52:16 +01004908/*
4909 * free the generic spec;
4910 * this can be put as patch_ops.free function
4911 */
Takashi Iwaifce52a32013-01-07 12:42:48 +01004912void snd_hda_gen_free(struct hda_codec *codec)
4913{
4914 snd_hda_gen_spec_free(codec->spec);
4915 kfree(codec->spec);
4916 codec->spec = NULL;
4917}
4918EXPORT_SYMBOL_HDA(snd_hda_gen_free);
4919
4920#ifdef CONFIG_PM
Takashi Iwai5187ac12013-01-07 12:52:16 +01004921/*
4922 * check the loopback power save state;
4923 * this can be put as patch_ops.check_power_status function
4924 */
Takashi Iwaifce52a32013-01-07 12:42:48 +01004925int snd_hda_gen_check_power_status(struct hda_codec *codec, hda_nid_t nid)
4926{
4927 struct hda_gen_spec *spec = codec->spec;
4928 return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
4929}
4930EXPORT_SYMBOL_HDA(snd_hda_gen_check_power_status);
4931#endif
Takashi Iwai352f7f92012-12-19 12:52:06 +01004932
4933
4934/*
4935 * the generic codec support
4936 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004937
Takashi Iwai352f7f92012-12-19 12:52:06 +01004938static const struct hda_codec_ops generic_patch_ops = {
4939 .build_controls = snd_hda_gen_build_controls,
4940 .build_pcms = snd_hda_gen_build_pcms,
4941 .init = snd_hda_gen_init,
Takashi Iwaifce52a32013-01-07 12:42:48 +01004942 .free = snd_hda_gen_free,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004943 .unsol_event = snd_hda_jack_unsol_event,
Takashi Iwai83012a72012-08-24 18:38:08 +02004944#ifdef CONFIG_PM
Takashi Iwaifce52a32013-01-07 12:42:48 +01004945 .check_power_status = snd_hda_gen_check_power_status,
Takashi Iwaicb53c622007-08-10 17:21:45 +02004946#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004947};
4948
Linus Torvalds1da177e2005-04-16 15:20:36 -07004949int snd_hda_parse_generic_codec(struct hda_codec *codec)
4950{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004951 struct hda_gen_spec *spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004952 int err;
4953
Takashi Iwaie560d8d2005-09-09 14:21:46 +02004954 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004955 if (!spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004956 return -ENOMEM;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004957 snd_hda_gen_spec_init(spec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004958 codec->spec = spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004959
Takashi Iwai9eb413e2012-12-19 14:41:21 +01004960 err = snd_hda_parse_pin_defcfg(codec, &spec->autocfg, NULL, 0);
4961 if (err < 0)
4962 return err;
4963
4964 err = snd_hda_gen_parse_auto_config(codec, &spec->autocfg);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004965 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004966 goto error;
4967
4968 codec->patch_ops = generic_patch_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004969 return 0;
4970
Takashi Iwai352f7f92012-12-19 12:52:06 +01004971error:
Takashi Iwaifce52a32013-01-07 12:42:48 +01004972 snd_hda_gen_free(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004973 return err;
4974}
Takashi Iwaifce52a32013-01-07 12:42:48 +01004975EXPORT_SYMBOL_HDA(snd_hda_parse_generic_codec);