blob: 3067ed4fe3b2f7fdb7edfca13e2b14575b9707a1 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Universal Interface for Intel High Definition Audio Codec
3 *
4 * Generic widget tree parser
5 *
6 * Copyright (c) 2004 Takashi Iwai <tiwai@suse.de>
7 *
8 * This driver is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This driver is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */
22
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/init.h>
24#include <linux/slab.h>
Paul Gortmakerd81a6d72011-09-22 09:34:58 -040025#include <linux/export.h>
Takashi Iwai352f7f92012-12-19 12:52:06 +010026#include <linux/sort.h>
Takashi Iwai55196ff2013-01-24 17:32:56 +010027#include <linux/delay.h>
Takashi Iwaif873e532012-12-20 16:58:39 +010028#include <linux/ctype.h>
29#include <linux/string.h>
Takashi Iwai294765582013-01-17 09:52:11 +010030#include <linux/bitops.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <sound/core.h>
Takashi Iwai352f7f92012-12-19 12:52:06 +010032#include <sound/jack.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include "hda_codec.h"
34#include "hda_local.h"
Takashi Iwai352f7f92012-12-19 12:52:06 +010035#include "hda_auto_parser.h"
36#include "hda_jack.h"
Takashi Iwai7504b6c2013-03-18 11:25:51 +010037#include "hda_beep.h"
Takashi Iwai352f7f92012-12-19 12:52:06 +010038#include "hda_generic.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Takashi Iwai352f7f92012-12-19 12:52:06 +010041/* initialize hda_gen_spec struct */
42int snd_hda_gen_spec_init(struct hda_gen_spec *spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -070043{
Takashi Iwai352f7f92012-12-19 12:52:06 +010044 snd_array_init(&spec->kctls, sizeof(struct snd_kcontrol_new), 32);
Takashi Iwai352f7f92012-12-19 12:52:06 +010045 snd_array_init(&spec->paths, sizeof(struct nid_path), 8);
Takashi Iwai0186f4f2013-02-07 10:45:11 +010046 snd_array_init(&spec->loopback_list, sizeof(struct hda_amp_list), 8);
Takashi Iwai38cf6f12012-12-21 14:09:42 +010047 mutex_init(&spec->pcm_mutex);
Takashi Iwai352f7f92012-12-19 12:52:06 +010048 return 0;
49}
50EXPORT_SYMBOL_HDA(snd_hda_gen_spec_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Takashi Iwai12c93df2012-12-19 14:38:33 +010052struct snd_kcontrol_new *
53snd_hda_gen_add_kctl(struct hda_gen_spec *spec, const char *name,
54 const struct snd_kcontrol_new *temp)
Takashi Iwai352f7f92012-12-19 12:52:06 +010055{
56 struct snd_kcontrol_new *knew = snd_array_new(&spec->kctls);
57 if (!knew)
58 return NULL;
59 *knew = *temp;
60 if (name)
61 knew->name = kstrdup(name, GFP_KERNEL);
62 else if (knew->name)
63 knew->name = kstrdup(knew->name, GFP_KERNEL);
64 if (!knew->name)
65 return NULL;
66 return knew;
67}
Takashi Iwai12c93df2012-12-19 14:38:33 +010068EXPORT_SYMBOL_HDA(snd_hda_gen_add_kctl);
Takashi Iwai352f7f92012-12-19 12:52:06 +010069
70static void free_kctls(struct hda_gen_spec *spec)
71{
72 if (spec->kctls.list) {
73 struct snd_kcontrol_new *kctl = spec->kctls.list;
74 int i;
75 for (i = 0; i < spec->kctls.used; i++)
76 kfree(kctl[i].name);
77 }
78 snd_array_free(&spec->kctls);
79}
80
Takashi Iwai352f7f92012-12-19 12:52:06 +010081void snd_hda_gen_spec_free(struct hda_gen_spec *spec)
82{
83 if (!spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 return;
Takashi Iwai352f7f92012-12-19 12:52:06 +010085 free_kctls(spec);
Takashi Iwai352f7f92012-12-19 12:52:06 +010086 snd_array_free(&spec->paths);
Takashi Iwai0186f4f2013-02-07 10:45:11 +010087 snd_array_free(&spec->loopback_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088}
Takashi Iwai352f7f92012-12-19 12:52:06 +010089EXPORT_SYMBOL_HDA(snd_hda_gen_spec_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
91/*
Takashi Iwai1c70a582013-01-11 17:48:22 +010092 * store user hints
93 */
94static void parse_user_hints(struct hda_codec *codec)
95{
96 struct hda_gen_spec *spec = codec->spec;
97 int val;
98
99 val = snd_hda_get_bool_hint(codec, "jack_detect");
100 if (val >= 0)
101 codec->no_jack_detect = !val;
102 val = snd_hda_get_bool_hint(codec, "inv_jack_detect");
103 if (val >= 0)
104 codec->inv_jack_detect = !!val;
105 val = snd_hda_get_bool_hint(codec, "trigger_sense");
106 if (val >= 0)
107 codec->no_trigger_sense = !val;
108 val = snd_hda_get_bool_hint(codec, "inv_eapd");
109 if (val >= 0)
110 codec->inv_eapd = !!val;
111 val = snd_hda_get_bool_hint(codec, "pcm_format_first");
112 if (val >= 0)
113 codec->pcm_format_first = !!val;
114 val = snd_hda_get_bool_hint(codec, "sticky_stream");
115 if (val >= 0)
116 codec->no_sticky_stream = !val;
117 val = snd_hda_get_bool_hint(codec, "spdif_status_reset");
118 if (val >= 0)
119 codec->spdif_status_reset = !!val;
120 val = snd_hda_get_bool_hint(codec, "pin_amp_workaround");
121 if (val >= 0)
122 codec->pin_amp_workaround = !!val;
123 val = snd_hda_get_bool_hint(codec, "single_adc_amp");
124 if (val >= 0)
125 codec->single_adc_amp = !!val;
126
Takashi Iwaif72706b2013-01-16 18:20:07 +0100127 val = snd_hda_get_bool_hint(codec, "auto_mute");
128 if (val >= 0)
129 spec->suppress_auto_mute = !val;
Takashi Iwai1c70a582013-01-11 17:48:22 +0100130 val = snd_hda_get_bool_hint(codec, "auto_mic");
131 if (val >= 0)
132 spec->suppress_auto_mic = !val;
133 val = snd_hda_get_bool_hint(codec, "line_in_auto_switch");
134 if (val >= 0)
135 spec->line_in_auto_switch = !!val;
Takashi Iwai7eebffd2013-06-24 16:00:21 +0200136 val = snd_hda_get_bool_hint(codec, "auto_mute_via_amp");
137 if (val >= 0)
138 spec->auto_mute_via_amp = !!val;
Takashi Iwai1c70a582013-01-11 17:48:22 +0100139 val = snd_hda_get_bool_hint(codec, "need_dac_fix");
140 if (val >= 0)
141 spec->need_dac_fix = !!val;
142 val = snd_hda_get_bool_hint(codec, "primary_hp");
143 if (val >= 0)
144 spec->no_primary_hp = !val;
Takashi Iwaida96fb52013-07-29 16:54:36 +0200145 val = snd_hda_get_bool_hint(codec, "multi_io");
146 if (val >= 0)
147 spec->no_multi_io = !val;
Takashi Iwai1c70a582013-01-11 17:48:22 +0100148 val = snd_hda_get_bool_hint(codec, "multi_cap_vol");
149 if (val >= 0)
150 spec->multi_cap_vol = !!val;
151 val = snd_hda_get_bool_hint(codec, "inv_dmic_split");
152 if (val >= 0)
153 spec->inv_dmic_split = !!val;
154 val = snd_hda_get_bool_hint(codec, "indep_hp");
155 if (val >= 0)
156 spec->indep_hp = !!val;
157 val = snd_hda_get_bool_hint(codec, "add_stereo_mix_input");
158 if (val >= 0)
159 spec->add_stereo_mix_input = !!val;
Takashi Iwaif811c3c2013-03-07 18:32:59 +0100160 /* the following two are just for compatibility */
Takashi Iwai1c70a582013-01-11 17:48:22 +0100161 val = snd_hda_get_bool_hint(codec, "add_out_jack_modes");
162 if (val >= 0)
Takashi Iwaif811c3c2013-03-07 18:32:59 +0100163 spec->add_jack_modes = !!val;
Takashi Iwai294765582013-01-17 09:52:11 +0100164 val = snd_hda_get_bool_hint(codec, "add_in_jack_modes");
165 if (val >= 0)
Takashi Iwaif811c3c2013-03-07 18:32:59 +0100166 spec->add_jack_modes = !!val;
167 val = snd_hda_get_bool_hint(codec, "add_jack_modes");
168 if (val >= 0)
169 spec->add_jack_modes = !!val;
Takashi Iwai55196ff2013-01-24 17:32:56 +0100170 val = snd_hda_get_bool_hint(codec, "power_down_unused");
171 if (val >= 0)
172 spec->power_down_unused = !!val;
Takashi Iwai967303d2013-02-19 17:12:42 +0100173 val = snd_hda_get_bool_hint(codec, "add_hp_mic");
174 if (val >= 0)
175 spec->hp_mic = !!val;
176 val = snd_hda_get_bool_hint(codec, "hp_mic_detect");
177 if (val >= 0)
178 spec->suppress_hp_mic_detect = !val;
Takashi Iwai1c70a582013-01-11 17:48:22 +0100179
180 if (!snd_hda_get_int_hint(codec, "mixer_nid", &val))
181 spec->mixer_nid = val;
182}
183
184/*
Takashi Iwai2c12c302013-01-10 09:33:29 +0100185 * pin control value accesses
186 */
187
188#define update_pin_ctl(codec, pin, val) \
189 snd_hda_codec_update_cache(codec, pin, 0, \
190 AC_VERB_SET_PIN_WIDGET_CONTROL, val)
191
192/* restore the pinctl based on the cached value */
193static inline void restore_pin_ctl(struct hda_codec *codec, hda_nid_t pin)
194{
195 update_pin_ctl(codec, pin, snd_hda_codec_get_pin_target(codec, pin));
196}
197
198/* set the pinctl target value and write it if requested */
199static void set_pin_target(struct hda_codec *codec, hda_nid_t pin,
200 unsigned int val, bool do_write)
201{
202 if (!pin)
203 return;
204 val = snd_hda_correct_pin_ctl(codec, pin, val);
205 snd_hda_codec_set_pin_target(codec, pin, val);
206 if (do_write)
207 update_pin_ctl(codec, pin, val);
208}
209
210/* set pinctl target values for all given pins */
211static void set_pin_targets(struct hda_codec *codec, int num_pins,
212 hda_nid_t *pins, unsigned int val)
213{
214 int i;
215 for (i = 0; i < num_pins; i++)
216 set_pin_target(codec, pins[i], val, false);
217}
218
219/*
Takashi Iwai352f7f92012-12-19 12:52:06 +0100220 * parsing paths
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100223/* return the position of NID in the list, or -1 if not found */
224static int find_idx_in_nid_list(hda_nid_t nid, const hda_nid_t *list, int nums)
225{
226 int i;
227 for (i = 0; i < nums; i++)
228 if (list[i] == nid)
229 return i;
230 return -1;
231}
232
233/* return true if the given NID is contained in the path */
234static bool is_nid_contained(struct nid_path *path, hda_nid_t nid)
235{
236 return find_idx_in_nid_list(nid, path->path, path->depth) >= 0;
237}
238
Takashi Iwaif5172a72013-01-04 13:19:55 +0100239static struct nid_path *get_nid_path(struct hda_codec *codec,
240 hda_nid_t from_nid, hda_nid_t to_nid,
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100241 int anchor_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100243 struct hda_gen_spec *spec = codec->spec;
244 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
Takashi Iwai352f7f92012-12-19 12:52:06 +0100246 for (i = 0; i < spec->paths.used; i++) {
247 struct nid_path *path = snd_array_elem(&spec->paths, i);
248 if (path->depth <= 0)
249 continue;
250 if ((!from_nid || path->path[0] == from_nid) &&
Takashi Iwaif5172a72013-01-04 13:19:55 +0100251 (!to_nid || path->path[path->depth - 1] == to_nid)) {
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100252 if (!anchor_nid ||
253 (anchor_nid > 0 && is_nid_contained(path, anchor_nid)) ||
254 (anchor_nid < 0 && !is_nid_contained(path, anchor_nid)))
Takashi Iwaif5172a72013-01-04 13:19:55 +0100255 return path;
256 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 }
258 return NULL;
259}
Takashi Iwaif5172a72013-01-04 13:19:55 +0100260
261/* get the path between the given NIDs;
262 * passing 0 to either @pin or @dac behaves as a wildcard
263 */
264struct nid_path *snd_hda_get_nid_path(struct hda_codec *codec,
265 hda_nid_t from_nid, hda_nid_t to_nid)
266{
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100267 return get_nid_path(codec, from_nid, to_nid, 0);
Takashi Iwaif5172a72013-01-04 13:19:55 +0100268}
Takashi Iwai352f7f92012-12-19 12:52:06 +0100269EXPORT_SYMBOL_HDA(snd_hda_get_nid_path);
270
Takashi Iwai196c17662013-01-04 15:01:40 +0100271/* get the index number corresponding to the path instance;
272 * the index starts from 1, for easier checking the invalid value
273 */
274int snd_hda_get_path_idx(struct hda_codec *codec, struct nid_path *path)
275{
276 struct hda_gen_spec *spec = codec->spec;
277 struct nid_path *array = spec->paths.list;
278 ssize_t idx;
279
280 if (!spec->paths.used)
281 return 0;
282 idx = path - array;
283 if (idx < 0 || idx >= spec->paths.used)
284 return 0;
285 return idx + 1;
286}
Takashi Iwai4bd01e92013-01-22 15:17:20 +0100287EXPORT_SYMBOL_HDA(snd_hda_get_path_idx);
Takashi Iwai196c17662013-01-04 15:01:40 +0100288
289/* get the path instance corresponding to the given index number */
290struct nid_path *snd_hda_get_path_from_idx(struct hda_codec *codec, int idx)
291{
292 struct hda_gen_spec *spec = codec->spec;
293
294 if (idx <= 0 || idx > spec->paths.used)
295 return NULL;
296 return snd_array_elem(&spec->paths, idx - 1);
297}
Takashi Iwai4bd01e92013-01-22 15:17:20 +0100298EXPORT_SYMBOL_HDA(snd_hda_get_path_from_idx);
Takashi Iwai196c17662013-01-04 15:01:40 +0100299
Takashi Iwai352f7f92012-12-19 12:52:06 +0100300/* check whether the given DAC is already found in any existing paths */
301static bool is_dac_already_used(struct hda_codec *codec, hda_nid_t nid)
302{
303 struct hda_gen_spec *spec = codec->spec;
304 int i;
305
306 for (i = 0; i < spec->paths.used; i++) {
307 struct nid_path *path = snd_array_elem(&spec->paths, i);
308 if (path->path[0] == nid)
309 return true;
310 }
311 return false;
312}
313
314/* check whether the given two widgets can be connected */
315static bool is_reachable_path(struct hda_codec *codec,
316 hda_nid_t from_nid, hda_nid_t to_nid)
317{
318 if (!from_nid || !to_nid)
319 return false;
320 return snd_hda_get_conn_index(codec, to_nid, from_nid, true) >= 0;
321}
322
323/* nid, dir and idx */
324#define AMP_VAL_COMPARE_MASK (0xffff | (1U << 18) | (0x0f << 19))
325
326/* check whether the given ctl is already assigned in any path elements */
327static bool is_ctl_used(struct hda_codec *codec, unsigned int val, int type)
328{
329 struct hda_gen_spec *spec = codec->spec;
330 int i;
331
332 val &= AMP_VAL_COMPARE_MASK;
333 for (i = 0; i < spec->paths.used; i++) {
334 struct nid_path *path = snd_array_elem(&spec->paths, i);
335 if ((path->ctls[type] & AMP_VAL_COMPARE_MASK) == val)
336 return true;
337 }
338 return false;
339}
340
341/* check whether a control with the given (nid, dir, idx) was assigned */
342static bool is_ctl_associated(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwai8999bf02013-01-18 11:01:33 +0100343 int dir, int idx, int type)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100344{
345 unsigned int val = HDA_COMPOSE_AMP_VAL(nid, 3, idx, dir);
Takashi Iwai8999bf02013-01-18 11:01:33 +0100346 return is_ctl_used(codec, val, type);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100347}
348
Takashi Iwai0c8c0f52012-12-20 17:54:22 +0100349static void print_nid_path(const char *pfx, struct nid_path *path)
350{
351 char buf[40];
352 int i;
353
354
355 buf[0] = 0;
356 for (i = 0; i < path->depth; i++) {
357 char tmp[4];
358 sprintf(tmp, ":%02x", path->path[i]);
359 strlcat(buf, tmp, sizeof(buf));
360 }
361 snd_printdd("%s path: depth=%d %s\n", pfx, path->depth, buf);
362}
363
Takashi Iwai352f7f92012-12-19 12:52:06 +0100364/* called recursively */
365static bool __parse_nid_path(struct hda_codec *codec,
366 hda_nid_t from_nid, hda_nid_t to_nid,
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100367 int anchor_nid, struct nid_path *path,
368 int depth)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100369{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100370 const hda_nid_t *conn;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100371 int i, nums;
372
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100373 if (to_nid == anchor_nid)
374 anchor_nid = 0; /* anchor passed */
375 else if (to_nid == (hda_nid_t)(-anchor_nid))
376 return false; /* hit the exclusive nid */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100377
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100378 nums = snd_hda_get_conn_list(codec, to_nid, &conn);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100379 for (i = 0; i < nums; i++) {
380 if (conn[i] != from_nid) {
381 /* special case: when from_nid is 0,
382 * try to find an empty DAC
383 */
384 if (from_nid ||
385 get_wcaps_type(get_wcaps(codec, conn[i])) != AC_WID_AUD_OUT ||
386 is_dac_already_used(codec, conn[i]))
387 continue;
388 }
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100389 /* anchor is not requested or already passed? */
390 if (anchor_nid <= 0)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100391 goto found;
392 }
393 if (depth >= MAX_NID_PATH_DEPTH)
394 return false;
395 for (i = 0; i < nums; i++) {
396 unsigned int type;
397 type = get_wcaps_type(get_wcaps(codec, conn[i]));
398 if (type == AC_WID_AUD_OUT || type == AC_WID_AUD_IN ||
399 type == AC_WID_PIN)
400 continue;
401 if (__parse_nid_path(codec, from_nid, conn[i],
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100402 anchor_nid, path, depth + 1))
Takashi Iwai352f7f92012-12-19 12:52:06 +0100403 goto found;
404 }
405 return false;
406
407 found:
408 path->path[path->depth] = conn[i];
409 path->idx[path->depth + 1] = i;
410 if (nums > 1 && get_wcaps_type(get_wcaps(codec, to_nid)) != AC_WID_AUD_MIX)
411 path->multi[path->depth + 1] = 1;
412 path->depth++;
413 return true;
414}
415
416/* parse the widget path from the given nid to the target nid;
417 * when @from_nid is 0, try to find an empty DAC;
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100418 * when @anchor_nid is set to a positive value, only paths through the widget
419 * with the given value are evaluated.
420 * when @anchor_nid is set to a negative value, paths through the widget
421 * with the negative of given value are excluded, only other paths are chosen.
422 * when @anchor_nid is zero, no special handling about path selection.
Takashi Iwai352f7f92012-12-19 12:52:06 +0100423 */
424bool snd_hda_parse_nid_path(struct hda_codec *codec, hda_nid_t from_nid,
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100425 hda_nid_t to_nid, int anchor_nid,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100426 struct nid_path *path)
427{
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100428 if (__parse_nid_path(codec, from_nid, to_nid, anchor_nid, path, 1)) {
Takashi Iwai352f7f92012-12-19 12:52:06 +0100429 path->path[path->depth] = to_nid;
430 path->depth++;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100431 return true;
432 }
433 return false;
434}
435EXPORT_SYMBOL_HDA(snd_hda_parse_nid_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
437/*
Takashi Iwai352f7f92012-12-19 12:52:06 +0100438 * parse the path between the given NIDs and add to the path list.
439 * if no valid path is found, return NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100441struct nid_path *
442snd_hda_add_new_path(struct hda_codec *codec, hda_nid_t from_nid,
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100443 hda_nid_t to_nid, int anchor_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100445 struct hda_gen_spec *spec = codec->spec;
446 struct nid_path *path;
447
448 if (from_nid && to_nid && !is_reachable_path(codec, from_nid, to_nid))
449 return NULL;
450
Takashi Iwaif5172a72013-01-04 13:19:55 +0100451 /* check whether the path has been already added */
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100452 path = get_nid_path(codec, from_nid, to_nid, anchor_nid);
Takashi Iwaif5172a72013-01-04 13:19:55 +0100453 if (path)
454 return path;
455
Takashi Iwai352f7f92012-12-19 12:52:06 +0100456 path = snd_array_new(&spec->paths);
457 if (!path)
458 return NULL;
459 memset(path, 0, sizeof(*path));
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100460 if (snd_hda_parse_nid_path(codec, from_nid, to_nid, anchor_nid, path))
Takashi Iwai352f7f92012-12-19 12:52:06 +0100461 return path;
462 /* push back */
463 spec->paths.used--;
464 return NULL;
465}
466EXPORT_SYMBOL_HDA(snd_hda_add_new_path);
467
Takashi Iwai980428c2013-01-09 09:28:20 +0100468/* clear the given path as invalid so that it won't be picked up later */
469static void invalidate_nid_path(struct hda_codec *codec, int idx)
470{
471 struct nid_path *path = snd_hda_get_path_from_idx(codec, idx);
472 if (!path)
473 return;
474 memset(path, 0, sizeof(*path));
475}
476
Takashi Iwai352f7f92012-12-19 12:52:06 +0100477/* look for an empty DAC slot */
478static hda_nid_t look_for_dac(struct hda_codec *codec, hda_nid_t pin,
479 bool is_digital)
480{
481 struct hda_gen_spec *spec = codec->spec;
482 bool cap_digital;
483 int i;
484
485 for (i = 0; i < spec->num_all_dacs; i++) {
486 hda_nid_t nid = spec->all_dacs[i];
487 if (!nid || is_dac_already_used(codec, nid))
488 continue;
489 cap_digital = !!(get_wcaps(codec, nid) & AC_WCAP_DIGITAL);
490 if (is_digital != cap_digital)
491 continue;
492 if (is_reachable_path(codec, nid, pin))
493 return nid;
494 }
495 return 0;
496}
497
498/* replace the channels in the composed amp value with the given number */
499static unsigned int amp_val_replace_channels(unsigned int val, unsigned int chs)
500{
501 val &= ~(0x3U << 16);
502 val |= chs << 16;
503 return val;
504}
505
506/* check whether the widget has the given amp capability for the direction */
507static bool check_amp_caps(struct hda_codec *codec, hda_nid_t nid,
508 int dir, unsigned int bits)
509{
510 if (!nid)
511 return false;
512 if (get_wcaps(codec, nid) & (1 << (dir + 1)))
513 if (query_amp_caps(codec, nid, dir) & bits)
514 return true;
515 return false;
516}
517
David Henningsson99a55922013-01-16 15:58:44 +0100518static bool same_amp_caps(struct hda_codec *codec, hda_nid_t nid1,
519 hda_nid_t nid2, int dir)
520{
521 if (!(get_wcaps(codec, nid1) & (1 << (dir + 1))))
522 return !(get_wcaps(codec, nid2) & (1 << (dir + 1)));
523 return (query_amp_caps(codec, nid1, dir) ==
524 query_amp_caps(codec, nid2, dir));
525}
526
Takashi Iwai352f7f92012-12-19 12:52:06 +0100527#define nid_has_mute(codec, nid, dir) \
Takashi Iwaif69910d2013-08-08 09:32:37 +0200528 check_amp_caps(codec, nid, dir, (AC_AMPCAP_MUTE | AC_AMPCAP_MIN_MUTE))
Takashi Iwai352f7f92012-12-19 12:52:06 +0100529#define nid_has_volume(codec, nid, dir) \
530 check_amp_caps(codec, nid, dir, AC_AMPCAP_NUM_STEPS)
531
532/* look for a widget suitable for assigning a mute switch in the path */
533static hda_nid_t look_for_out_mute_nid(struct hda_codec *codec,
534 struct nid_path *path)
535{
536 int i;
537
538 for (i = path->depth - 1; i >= 0; i--) {
539 if (nid_has_mute(codec, path->path[i], HDA_OUTPUT))
540 return path->path[i];
541 if (i != path->depth - 1 && i != 0 &&
542 nid_has_mute(codec, path->path[i], HDA_INPUT))
543 return path->path[i];
544 }
545 return 0;
546}
547
548/* look for a widget suitable for assigning a volume ctl in the path */
549static hda_nid_t look_for_out_vol_nid(struct hda_codec *codec,
550 struct nid_path *path)
551{
Takashi Iwaia1114a82013-11-04 16:32:01 +0100552 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100553 int i;
554
555 for (i = path->depth - 1; i >= 0; i--) {
Takashi Iwaia1114a82013-11-04 16:32:01 +0100556 hda_nid_t nid = path->path[i];
557 if ((spec->out_vol_mask >> nid) & 1)
558 continue;
559 if (nid_has_volume(codec, nid, HDA_OUTPUT))
560 return nid;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100561 }
Takashi Iwai82beb8f2007-08-10 17:09:26 +0200562 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563}
564
565/*
Takashi Iwai352f7f92012-12-19 12:52:06 +0100566 * path activation / deactivation
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100568
569/* can have the amp-in capability? */
570static bool has_amp_in(struct hda_codec *codec, struct nid_path *path, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100572 hda_nid_t nid = path->path[idx];
573 unsigned int caps = get_wcaps(codec, nid);
574 unsigned int type = get_wcaps_type(caps);
575
576 if (!(caps & AC_WCAP_IN_AMP))
577 return false;
578 if (type == AC_WID_PIN && idx > 0) /* only for input pins */
579 return false;
580 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581}
582
Takashi Iwai352f7f92012-12-19 12:52:06 +0100583/* can have the amp-out capability? */
584static bool has_amp_out(struct hda_codec *codec, struct nid_path *path, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100586 hda_nid_t nid = path->path[idx];
587 unsigned int caps = get_wcaps(codec, nid);
588 unsigned int type = get_wcaps_type(caps);
589
590 if (!(caps & AC_WCAP_OUT_AMP))
591 return false;
592 if (type == AC_WID_PIN && !idx) /* only for output pins */
593 return false;
594 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595}
596
Takashi Iwai352f7f92012-12-19 12:52:06 +0100597/* check whether the given (nid,dir,idx) is active */
598static bool is_active_nid(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwai7dddf2ae2013-01-24 16:31:35 +0100599 unsigned int dir, unsigned int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100601 struct hda_gen_spec *spec = codec->spec;
602 int i, n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
Takashi Iwai352f7f92012-12-19 12:52:06 +0100604 for (n = 0; n < spec->paths.used; n++) {
605 struct nid_path *path = snd_array_elem(&spec->paths, n);
606 if (!path->active)
607 continue;
608 for (i = 0; i < path->depth; i++) {
609 if (path->path[i] == nid) {
610 if (dir == HDA_OUTPUT || path->idx[i] == idx)
611 return true;
612 break;
613 }
614 }
615 }
616 return false;
617}
618
Takashi Iwaib1b9fbd2013-05-14 12:58:47 +0200619/* check whether the NID is referred by any active paths */
620#define is_active_nid_for_any(codec, nid) \
621 is_active_nid(codec, nid, HDA_OUTPUT, 0)
622
Takashi Iwai352f7f92012-12-19 12:52:06 +0100623/* get the default amp value for the target state */
624static int get_amp_val_to_activate(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwai8999bf02013-01-18 11:01:33 +0100625 int dir, unsigned int caps, bool enable)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100626{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100627 unsigned int val = 0;
628
Takashi Iwai352f7f92012-12-19 12:52:06 +0100629 if (caps & AC_AMPCAP_NUM_STEPS) {
630 /* set to 0dB */
631 if (enable)
632 val = (caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT;
633 }
Takashi Iwaif69910d2013-08-08 09:32:37 +0200634 if (caps & (AC_AMPCAP_MUTE | AC_AMPCAP_MIN_MUTE)) {
Takashi Iwai352f7f92012-12-19 12:52:06 +0100635 if (!enable)
636 val |= HDA_AMP_MUTE;
637 }
638 return val;
639}
640
641/* initialize the amp value (only at the first time) */
642static void init_amp(struct hda_codec *codec, hda_nid_t nid, int dir, int idx)
643{
Takashi Iwai8999bf02013-01-18 11:01:33 +0100644 unsigned int caps = query_amp_caps(codec, nid, dir);
645 int val = get_amp_val_to_activate(codec, nid, dir, caps, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100646 snd_hda_codec_amp_init_stereo(codec, nid, dir, idx, 0xff, val);
647}
648
Takashi Iwai8999bf02013-01-18 11:01:33 +0100649/* calculate amp value mask we can modify;
650 * if the given amp is controlled by mixers, don't touch it
651 */
652static unsigned int get_amp_mask_to_modify(struct hda_codec *codec,
653 hda_nid_t nid, int dir, int idx,
654 unsigned int caps)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100655{
Takashi Iwai8999bf02013-01-18 11:01:33 +0100656 unsigned int mask = 0xff;
657
Takashi Iwaif69910d2013-08-08 09:32:37 +0200658 if (caps & (AC_AMPCAP_MUTE | AC_AMPCAP_MIN_MUTE)) {
Takashi Iwai8999bf02013-01-18 11:01:33 +0100659 if (is_ctl_associated(codec, nid, dir, idx, NID_PATH_MUTE_CTL))
660 mask &= ~0x80;
661 }
662 if (caps & AC_AMPCAP_NUM_STEPS) {
663 if (is_ctl_associated(codec, nid, dir, idx, NID_PATH_VOL_CTL) ||
664 is_ctl_associated(codec, nid, dir, idx, NID_PATH_BOOST_CTL))
665 mask &= ~0x7f;
666 }
667 return mask;
668}
669
670static void activate_amp(struct hda_codec *codec, hda_nid_t nid, int dir,
671 int idx, int idx_to_check, bool enable)
672{
673 unsigned int caps;
674 unsigned int mask, val;
675
Takashi Iwai7dddf2ae2013-01-24 16:31:35 +0100676 if (!enable && is_active_nid(codec, nid, dir, idx_to_check))
Takashi Iwai352f7f92012-12-19 12:52:06 +0100677 return;
Takashi Iwai8999bf02013-01-18 11:01:33 +0100678
679 caps = query_amp_caps(codec, nid, dir);
680 val = get_amp_val_to_activate(codec, nid, dir, caps, enable);
681 mask = get_amp_mask_to_modify(codec, nid, dir, idx_to_check, caps);
682 if (!mask)
683 return;
684
685 val &= mask;
686 snd_hda_codec_amp_stereo(codec, nid, dir, idx, mask, val);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100687}
688
689static void activate_amp_out(struct hda_codec *codec, struct nid_path *path,
690 int i, bool enable)
691{
692 hda_nid_t nid = path->path[i];
693 init_amp(codec, nid, HDA_OUTPUT, 0);
Takashi Iwai8999bf02013-01-18 11:01:33 +0100694 activate_amp(codec, nid, HDA_OUTPUT, 0, 0, enable);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100695}
696
697static void activate_amp_in(struct hda_codec *codec, struct nid_path *path,
698 int i, bool enable, bool add_aamix)
699{
700 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100701 const hda_nid_t *conn;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100702 int n, nums, idx;
703 int type;
704 hda_nid_t nid = path->path[i];
705
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100706 nums = snd_hda_get_conn_list(codec, nid, &conn);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100707 type = get_wcaps_type(get_wcaps(codec, nid));
708 if (type == AC_WID_PIN ||
709 (type == AC_WID_AUD_IN && codec->single_adc_amp)) {
710 nums = 1;
711 idx = 0;
712 } else
713 idx = path->idx[i];
714
715 for (n = 0; n < nums; n++)
716 init_amp(codec, nid, HDA_INPUT, n);
717
Takashi Iwai352f7f92012-12-19 12:52:06 +0100718 /* here is a little bit tricky in comparison with activate_amp_out();
719 * when aa-mixer is available, we need to enable the path as well
720 */
721 for (n = 0; n < nums; n++) {
Takashi Iwaie4a395e2013-01-23 17:00:31 +0100722 if (n != idx && (!add_aamix || conn[n] != spec->mixer_merge_nid))
Takashi Iwai352f7f92012-12-19 12:52:06 +0100723 continue;
Takashi Iwai8999bf02013-01-18 11:01:33 +0100724 activate_amp(codec, nid, HDA_INPUT, n, idx, enable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 }
726}
727
Takashi Iwai352f7f92012-12-19 12:52:06 +0100728/* activate or deactivate the given path
729 * if @add_aamix is set, enable the input from aa-mix NID as well (if any)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100731void snd_hda_activate_path(struct hda_codec *codec, struct nid_path *path,
732 bool enable, bool add_aamix)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733{
Takashi Iwai55196ff2013-01-24 17:32:56 +0100734 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100735 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736
Takashi Iwai352f7f92012-12-19 12:52:06 +0100737 if (!enable)
738 path->active = false;
739
740 for (i = path->depth - 1; i >= 0; i--) {
Takashi Iwai55196ff2013-01-24 17:32:56 +0100741 hda_nid_t nid = path->path[i];
742 if (enable && spec->power_down_unused) {
743 /* make sure the widget is powered up */
744 if (!snd_hda_check_power_state(codec, nid, AC_PWRST_D0))
745 snd_hda_codec_write(codec, nid, 0,
746 AC_VERB_SET_POWER_STATE,
747 AC_PWRST_D0);
748 }
Takashi Iwai352f7f92012-12-19 12:52:06 +0100749 if (enable && path->multi[i])
Takashi Iwai55196ff2013-01-24 17:32:56 +0100750 snd_hda_codec_write_cache(codec, nid, 0,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100751 AC_VERB_SET_CONNECT_SEL,
752 path->idx[i]);
753 if (has_amp_in(codec, path, i))
754 activate_amp_in(codec, path, i, enable, add_aamix);
755 if (has_amp_out(codec, path, i))
756 activate_amp_out(codec, path, i, enable);
757 }
758
759 if (enable)
760 path->active = true;
761}
762EXPORT_SYMBOL_HDA(snd_hda_activate_path);
763
Takashi Iwai55196ff2013-01-24 17:32:56 +0100764/* if the given path is inactive, put widgets into D3 (only if suitable) */
765static void path_power_down_sync(struct hda_codec *codec, struct nid_path *path)
766{
767 struct hda_gen_spec *spec = codec->spec;
Jiri Slaby868211d2013-04-04 22:32:10 +0200768 bool changed = false;
Takashi Iwai55196ff2013-01-24 17:32:56 +0100769 int i;
770
771 if (!spec->power_down_unused || path->active)
772 return;
773
774 for (i = 0; i < path->depth; i++) {
775 hda_nid_t nid = path->path[i];
Takashi Iwaib1b9fbd2013-05-14 12:58:47 +0200776 if (!snd_hda_check_power_state(codec, nid, AC_PWRST_D3) &&
777 !is_active_nid_for_any(codec, nid)) {
Takashi Iwai55196ff2013-01-24 17:32:56 +0100778 snd_hda_codec_write(codec, nid, 0,
779 AC_VERB_SET_POWER_STATE,
780 AC_PWRST_D3);
781 changed = true;
782 }
783 }
784
785 if (changed) {
786 msleep(10);
787 snd_hda_codec_read(codec, path->path[0], 0,
788 AC_VERB_GET_POWER_STATE, 0);
789 }
790}
791
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +0100792/* turn on/off EAPD on the given pin */
793static void set_pin_eapd(struct hda_codec *codec, hda_nid_t pin, bool enable)
794{
795 struct hda_gen_spec *spec = codec->spec;
796 if (spec->own_eapd_ctl ||
797 !(snd_hda_query_pin_caps(codec, pin) & AC_PINCAP_EAPD))
798 return;
Takashi Iwai05909d5c2013-05-31 19:55:54 +0200799 if (spec->keep_eapd_on && !enable)
800 return;
Takashi Iwai468ac412013-11-12 11:36:00 +0100801 if (codec->inv_eapd)
802 enable = !enable;
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +0100803 snd_hda_codec_update_cache(codec, pin, 0,
804 AC_VERB_SET_EAPD_BTLENABLE,
805 enable ? 0x02 : 0x00);
806}
807
Takashi Iwai3e367f12013-01-23 17:07:23 +0100808/* re-initialize the path specified by the given path index */
809static void resume_path_from_idx(struct hda_codec *codec, int path_idx)
810{
811 struct nid_path *path = snd_hda_get_path_from_idx(codec, path_idx);
812 if (path)
813 snd_hda_activate_path(codec, path, path->active, false);
814}
815
Takashi Iwai352f7f92012-12-19 12:52:06 +0100816
817/*
818 * Helper functions for creating mixer ctl elements
819 */
820
Takashi Iwai7eebffd2013-06-24 16:00:21 +0200821static int hda_gen_mixer_mute_put(struct snd_kcontrol *kcontrol,
822 struct snd_ctl_elem_value *ucontrol);
Takashi Iwaibc2eee22013-08-09 15:05:03 +0200823static int hda_gen_bind_mute_put(struct snd_kcontrol *kcontrol,
824 struct snd_ctl_elem_value *ucontrol);
Takashi Iwai7eebffd2013-06-24 16:00:21 +0200825
Takashi Iwai352f7f92012-12-19 12:52:06 +0100826enum {
827 HDA_CTL_WIDGET_VOL,
828 HDA_CTL_WIDGET_MUTE,
829 HDA_CTL_BIND_MUTE,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100830};
831static const struct snd_kcontrol_new control_templates[] = {
832 HDA_CODEC_VOLUME(NULL, 0, 0, 0),
Takashi Iwai7eebffd2013-06-24 16:00:21 +0200833 /* only the put callback is replaced for handling the special mute */
834 {
835 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
836 .subdevice = HDA_SUBDEV_AMP_FLAG,
837 .info = snd_hda_mixer_amp_switch_info,
838 .get = snd_hda_mixer_amp_switch_get,
839 .put = hda_gen_mixer_mute_put, /* replaced */
840 .private_value = HDA_COMPOSE_AMP_VAL(0, 3, 0, 0),
841 },
Takashi Iwaibc2eee22013-08-09 15:05:03 +0200842 {
843 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
844 .info = snd_hda_mixer_amp_switch_info,
845 .get = snd_hda_mixer_bind_switch_get,
846 .put = hda_gen_bind_mute_put, /* replaced */
847 .private_value = HDA_COMPOSE_AMP_VAL(0, 3, 0, 0),
848 },
Takashi Iwai352f7f92012-12-19 12:52:06 +0100849};
850
851/* add dynamic controls from template */
Takashi Iwaia35bd1e2013-01-18 14:01:14 +0100852static struct snd_kcontrol_new *
853add_control(struct hda_gen_spec *spec, int type, const char *name,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100854 int cidx, unsigned long val)
855{
856 struct snd_kcontrol_new *knew;
857
Takashi Iwai12c93df2012-12-19 14:38:33 +0100858 knew = snd_hda_gen_add_kctl(spec, name, &control_templates[type]);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100859 if (!knew)
Takashi Iwaia35bd1e2013-01-18 14:01:14 +0100860 return NULL;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100861 knew->index = cidx;
862 if (get_amp_nid_(val))
863 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
864 knew->private_value = val;
Takashi Iwaia35bd1e2013-01-18 14:01:14 +0100865 return knew;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100866}
867
868static int add_control_with_pfx(struct hda_gen_spec *spec, int type,
869 const char *pfx, const char *dir,
870 const char *sfx, int cidx, unsigned long val)
871{
Takashi Iwai975cc022013-06-28 11:56:49 +0200872 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
Takashi Iwai352f7f92012-12-19 12:52:06 +0100873 snprintf(name, sizeof(name), "%s %s %s", pfx, dir, sfx);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +0100874 if (!add_control(spec, type, name, cidx, val))
875 return -ENOMEM;
876 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100877}
878
879#define add_pb_vol_ctrl(spec, type, pfx, val) \
880 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", 0, val)
881#define add_pb_sw_ctrl(spec, type, pfx, val) \
882 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", 0, val)
883#define __add_pb_vol_ctrl(spec, type, pfx, cidx, val) \
884 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", cidx, val)
885#define __add_pb_sw_ctrl(spec, type, pfx, cidx, val) \
886 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", cidx, val)
887
888static int add_vol_ctl(struct hda_codec *codec, const char *pfx, int cidx,
889 unsigned int chs, struct nid_path *path)
890{
891 unsigned int val;
892 if (!path)
893 return 0;
894 val = path->ctls[NID_PATH_VOL_CTL];
895 if (!val)
896 return 0;
897 val = amp_val_replace_channels(val, chs);
898 return __add_pb_vol_ctrl(codec->spec, HDA_CTL_WIDGET_VOL, pfx, cidx, val);
899}
900
901/* return the channel bits suitable for the given path->ctls[] */
902static int get_default_ch_nums(struct hda_codec *codec, struct nid_path *path,
903 int type)
904{
905 int chs = 1; /* mono (left only) */
906 if (path) {
907 hda_nid_t nid = get_amp_nid_(path->ctls[type]);
908 if (nid && (get_wcaps(codec, nid) & AC_WCAP_STEREO))
909 chs = 3; /* stereo */
910 }
911 return chs;
912}
913
914static int add_stereo_vol(struct hda_codec *codec, const char *pfx, int cidx,
915 struct nid_path *path)
916{
917 int chs = get_default_ch_nums(codec, path, NID_PATH_VOL_CTL);
918 return add_vol_ctl(codec, pfx, cidx, chs, path);
919}
920
921/* create a mute-switch for the given mixer widget;
922 * if it has multiple sources (e.g. DAC and loopback), create a bind-mute
923 */
924static int add_sw_ctl(struct hda_codec *codec, const char *pfx, int cidx,
925 unsigned int chs, struct nid_path *path)
926{
927 unsigned int val;
928 int type = HDA_CTL_WIDGET_MUTE;
929
930 if (!path)
931 return 0;
932 val = path->ctls[NID_PATH_MUTE_CTL];
933 if (!val)
934 return 0;
935 val = amp_val_replace_channels(val, chs);
936 if (get_amp_direction_(val) == HDA_INPUT) {
937 hda_nid_t nid = get_amp_nid_(val);
938 int nums = snd_hda_get_num_conns(codec, nid);
939 if (nums > 1) {
940 type = HDA_CTL_BIND_MUTE;
941 val |= nums << 19;
942 }
943 }
944 return __add_pb_sw_ctrl(codec->spec, type, pfx, cidx, val);
945}
946
947static int add_stereo_sw(struct hda_codec *codec, const char *pfx,
948 int cidx, struct nid_path *path)
949{
950 int chs = get_default_ch_nums(codec, path, NID_PATH_MUTE_CTL);
951 return add_sw_ctl(codec, pfx, cidx, chs, path);
952}
953
Takashi Iwai7eebffd2013-06-24 16:00:21 +0200954/* playback mute control with the software mute bit check */
Takashi Iwaibc2eee22013-08-09 15:05:03 +0200955static void sync_auto_mute_bits(struct snd_kcontrol *kcontrol,
956 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai7eebffd2013-06-24 16:00:21 +0200957{
958 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
959 struct hda_gen_spec *spec = codec->spec;
960
961 if (spec->auto_mute_via_amp) {
962 hda_nid_t nid = get_amp_nid(kcontrol);
963 bool enabled = !((spec->mute_bits >> nid) & 1);
964 ucontrol->value.integer.value[0] &= enabled;
965 ucontrol->value.integer.value[1] &= enabled;
966 }
Takashi Iwaibc2eee22013-08-09 15:05:03 +0200967}
Takashi Iwai7eebffd2013-06-24 16:00:21 +0200968
Takashi Iwaibc2eee22013-08-09 15:05:03 +0200969static int hda_gen_mixer_mute_put(struct snd_kcontrol *kcontrol,
970 struct snd_ctl_elem_value *ucontrol)
971{
972 sync_auto_mute_bits(kcontrol, ucontrol);
Takashi Iwai7eebffd2013-06-24 16:00:21 +0200973 return snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
974}
975
Takashi Iwaibc2eee22013-08-09 15:05:03 +0200976static int hda_gen_bind_mute_put(struct snd_kcontrol *kcontrol,
977 struct snd_ctl_elem_value *ucontrol)
978{
979 sync_auto_mute_bits(kcontrol, ucontrol);
980 return snd_hda_mixer_bind_switch_put(kcontrol, ucontrol);
981}
982
Takashi Iwai247d85e2013-01-17 16:18:11 +0100983/* any ctl assigned to the path with the given index? */
984static bool path_has_mixer(struct hda_codec *codec, int path_idx, int ctl_type)
985{
986 struct nid_path *path = snd_hda_get_path_from_idx(codec, path_idx);
987 return path && path->ctls[ctl_type];
988}
989
Takashi Iwai352f7f92012-12-19 12:52:06 +0100990static const char * const channel_name[4] = {
991 "Front", "Surround", "CLFE", "Side"
992};
993
994/* give some appropriate ctl name prefix for the given line out channel */
Takashi Iwai247d85e2013-01-17 16:18:11 +0100995static const char *get_line_out_pfx(struct hda_codec *codec, int ch,
996 int *index, int ctl_type)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100997{
Takashi Iwai247d85e2013-01-17 16:18:11 +0100998 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100999 struct auto_pin_cfg *cfg = &spec->autocfg;
1000
1001 *index = 0;
1002 if (cfg->line_outs == 1 && !spec->multi_ios &&
Takashi Iwai247d85e2013-01-17 16:18:11 +01001003 !cfg->hp_outs && !cfg->speaker_outs)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001004 return spec->vmaster_mute.hook ? "PCM" : "Master";
1005
1006 /* if there is really a single DAC used in the whole output paths,
1007 * use it master (or "PCM" if a vmaster hook is present)
1008 */
1009 if (spec->multiout.num_dacs == 1 && !spec->mixer_nid &&
1010 !spec->multiout.hp_out_nid[0] && !spec->multiout.extra_out_nid[0])
1011 return spec->vmaster_mute.hook ? "PCM" : "Master";
1012
Takashi Iwai247d85e2013-01-17 16:18:11 +01001013 /* multi-io channels */
1014 if (ch >= cfg->line_outs)
1015 return channel_name[ch];
1016
Takashi Iwai352f7f92012-12-19 12:52:06 +01001017 switch (cfg->line_out_type) {
1018 case AUTO_PIN_SPEAKER_OUT:
Takashi Iwai247d85e2013-01-17 16:18:11 +01001019 /* if the primary channel vol/mute is shared with HP volume,
1020 * don't name it as Speaker
1021 */
1022 if (!ch && cfg->hp_outs &&
1023 !path_has_mixer(codec, spec->hp_paths[0], ctl_type))
1024 break;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001025 if (cfg->line_outs == 1)
1026 return "Speaker";
1027 if (cfg->line_outs == 2)
1028 return ch ? "Bass Speaker" : "Speaker";
1029 break;
1030 case AUTO_PIN_HP_OUT:
Takashi Iwai247d85e2013-01-17 16:18:11 +01001031 /* if the primary channel vol/mute is shared with spk volume,
1032 * don't name it as Headphone
1033 */
1034 if (!ch && cfg->speaker_outs &&
1035 !path_has_mixer(codec, spec->speaker_paths[0], ctl_type))
1036 break;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001037 /* for multi-io case, only the primary out */
1038 if (ch && spec->multi_ios)
1039 break;
1040 *index = ch;
1041 return "Headphone";
Takashi Iwai352f7f92012-12-19 12:52:06 +01001042 }
Takashi Iwai247d85e2013-01-17 16:18:11 +01001043
1044 /* for a single channel output, we don't have to name the channel */
1045 if (cfg->line_outs == 1 && !spec->multi_ios)
1046 return "PCM";
1047
Takashi Iwai352f7f92012-12-19 12:52:06 +01001048 if (ch >= ARRAY_SIZE(channel_name)) {
1049 snd_BUG();
1050 return "PCM";
1051 }
1052
1053 return channel_name[ch];
1054}
1055
1056/*
1057 * Parse output paths
1058 */
1059
1060/* badness definition */
1061enum {
1062 /* No primary DAC is found for the main output */
1063 BAD_NO_PRIMARY_DAC = 0x10000,
1064 /* No DAC is found for the extra output */
1065 BAD_NO_DAC = 0x4000,
1066 /* No possible multi-ios */
Takashi Iwai1d739062013-02-13 14:17:32 +01001067 BAD_MULTI_IO = 0x120,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001068 /* No individual DAC for extra output */
1069 BAD_NO_EXTRA_DAC = 0x102,
1070 /* No individual DAC for extra surrounds */
1071 BAD_NO_EXTRA_SURR_DAC = 0x101,
1072 /* Primary DAC shared with main surrounds */
1073 BAD_SHARED_SURROUND = 0x100,
Takashi Iwai55a63d42013-03-21 17:20:12 +01001074 /* No independent HP possible */
Takashi Iwaibec8e682013-03-22 15:10:08 +01001075 BAD_NO_INDEP_HP = 0x10,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001076 /* Primary DAC shared with main CLFE */
1077 BAD_SHARED_CLFE = 0x10,
1078 /* Primary DAC shared with extra surrounds */
1079 BAD_SHARED_EXTRA_SURROUND = 0x10,
1080 /* Volume widget is shared */
1081 BAD_SHARED_VOL = 0x10,
1082};
1083
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001084/* look for widgets in the given path which are appropriate for
Takashi Iwai352f7f92012-12-19 12:52:06 +01001085 * volume and mute controls, and assign the values to ctls[].
1086 *
1087 * When no appropriate widget is found in the path, the badness value
1088 * is incremented depending on the situation. The function returns the
1089 * total badness for both volume and mute controls.
1090 */
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001091static int assign_out_path_ctls(struct hda_codec *codec, struct nid_path *path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001092{
Takashi Iwai352f7f92012-12-19 12:52:06 +01001093 hda_nid_t nid;
1094 unsigned int val;
1095 int badness = 0;
1096
1097 if (!path)
1098 return BAD_SHARED_VOL * 2;
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001099
1100 if (path->ctls[NID_PATH_VOL_CTL] ||
1101 path->ctls[NID_PATH_MUTE_CTL])
1102 return 0; /* already evaluated */
1103
Takashi Iwai352f7f92012-12-19 12:52:06 +01001104 nid = look_for_out_vol_nid(codec, path);
1105 if (nid) {
1106 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
1107 if (is_ctl_used(codec, val, NID_PATH_VOL_CTL))
1108 badness += BAD_SHARED_VOL;
1109 else
1110 path->ctls[NID_PATH_VOL_CTL] = val;
1111 } else
1112 badness += BAD_SHARED_VOL;
1113 nid = look_for_out_mute_nid(codec, path);
1114 if (nid) {
1115 unsigned int wid_type = get_wcaps_type(get_wcaps(codec, nid));
1116 if (wid_type == AC_WID_PIN || wid_type == AC_WID_AUD_OUT ||
1117 nid_has_mute(codec, nid, HDA_OUTPUT))
1118 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
1119 else
1120 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT);
1121 if (is_ctl_used(codec, val, NID_PATH_MUTE_CTL))
1122 badness += BAD_SHARED_VOL;
1123 else
1124 path->ctls[NID_PATH_MUTE_CTL] = val;
1125 } else
1126 badness += BAD_SHARED_VOL;
1127 return badness;
1128}
1129
Takashi Iwai98bd1112013-03-22 14:53:50 +01001130const struct badness_table hda_main_out_badness = {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001131 .no_primary_dac = BAD_NO_PRIMARY_DAC,
1132 .no_dac = BAD_NO_DAC,
1133 .shared_primary = BAD_NO_PRIMARY_DAC,
1134 .shared_surr = BAD_SHARED_SURROUND,
1135 .shared_clfe = BAD_SHARED_CLFE,
1136 .shared_surr_main = BAD_SHARED_SURROUND,
1137};
Takashi Iwai98bd1112013-03-22 14:53:50 +01001138EXPORT_SYMBOL_HDA(hda_main_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001139
Takashi Iwai98bd1112013-03-22 14:53:50 +01001140const struct badness_table hda_extra_out_badness = {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001141 .no_primary_dac = BAD_NO_DAC,
1142 .no_dac = BAD_NO_DAC,
1143 .shared_primary = BAD_NO_EXTRA_DAC,
1144 .shared_surr = BAD_SHARED_EXTRA_SURROUND,
1145 .shared_clfe = BAD_SHARED_EXTRA_SURROUND,
1146 .shared_surr_main = BAD_NO_EXTRA_SURR_DAC,
1147};
Takashi Iwai98bd1112013-03-22 14:53:50 +01001148EXPORT_SYMBOL_HDA(hda_extra_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001149
Takashi Iwai7385df62013-01-07 09:50:52 +01001150/* get the DAC of the primary output corresponding to the given array index */
1151static hda_nid_t get_primary_out(struct hda_codec *codec, int idx)
1152{
1153 struct hda_gen_spec *spec = codec->spec;
1154 struct auto_pin_cfg *cfg = &spec->autocfg;
1155
1156 if (cfg->line_outs > idx)
1157 return spec->private_dac_nids[idx];
1158 idx -= cfg->line_outs;
1159 if (spec->multi_ios > idx)
1160 return spec->multi_io[idx].dac;
1161 return 0;
1162}
1163
1164/* return the DAC if it's reachable, otherwise zero */
1165static inline hda_nid_t try_dac(struct hda_codec *codec,
1166 hda_nid_t dac, hda_nid_t pin)
1167{
1168 return is_reachable_path(codec, dac, pin) ? dac : 0;
1169}
1170
Takashi Iwai352f7f92012-12-19 12:52:06 +01001171/* try to assign DACs to pins and return the resultant badness */
1172static int try_assign_dacs(struct hda_codec *codec, int num_outs,
1173 const hda_nid_t *pins, hda_nid_t *dacs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001174 int *path_idx,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001175 const struct badness_table *bad)
1176{
1177 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001178 int i, j;
1179 int badness = 0;
1180 hda_nid_t dac;
1181
1182 if (!num_outs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 return 0;
1184
Takashi Iwai352f7f92012-12-19 12:52:06 +01001185 for (i = 0; i < num_outs; i++) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001186 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001187 hda_nid_t pin = pins[i];
Takashi Iwai1e0b5282013-01-04 12:56:52 +01001188
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001189 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
1190 if (path) {
1191 badness += assign_out_path_ctls(codec, path);
Takashi Iwai1e0b5282013-01-04 12:56:52 +01001192 continue;
1193 }
1194
1195 dacs[i] = look_for_dac(codec, pin, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001196 if (!dacs[i] && !i) {
Takashi Iwai980428c2013-01-09 09:28:20 +01001197 /* try to steal the DAC of surrounds for the front */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001198 for (j = 1; j < num_outs; j++) {
1199 if (is_reachable_path(codec, dacs[j], pin)) {
1200 dacs[0] = dacs[j];
1201 dacs[j] = 0;
Takashi Iwai980428c2013-01-09 09:28:20 +01001202 invalidate_nid_path(codec, path_idx[j]);
Takashi Iwai196c17662013-01-04 15:01:40 +01001203 path_idx[j] = 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001204 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 }
1206 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001207 }
1208 dac = dacs[i];
1209 if (!dac) {
Takashi Iwai7385df62013-01-07 09:50:52 +01001210 if (num_outs > 2)
1211 dac = try_dac(codec, get_primary_out(codec, i), pin);
1212 if (!dac)
1213 dac = try_dac(codec, dacs[0], pin);
1214 if (!dac)
1215 dac = try_dac(codec, get_primary_out(codec, i), pin);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001216 if (dac) {
1217 if (!i)
1218 badness += bad->shared_primary;
1219 else if (i == 1)
1220 badness += bad->shared_surr;
1221 else
1222 badness += bad->shared_clfe;
1223 } else if (is_reachable_path(codec, spec->private_dac_nids[0], pin)) {
1224 dac = spec->private_dac_nids[0];
1225 badness += bad->shared_surr_main;
1226 } else if (!i)
1227 badness += bad->no_primary_dac;
1228 else
1229 badness += bad->no_dac;
1230 }
Takashi Iwai1fa335b2013-01-21 11:43:19 +01001231 if (!dac)
1232 continue;
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001233 path = snd_hda_add_new_path(codec, dac, pin, -spec->mixer_nid);
Takashi Iwai117688a2013-01-04 15:41:41 +01001234 if (!path && !i && spec->mixer_nid) {
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001235 /* try with aamix */
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001236 path = snd_hda_add_new_path(codec, dac, pin, 0);
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001237 }
Takashi Iwai1fa335b2013-01-21 11:43:19 +01001238 if (!path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001239 dac = dacs[i] = 0;
Takashi Iwai1fa335b2013-01-21 11:43:19 +01001240 badness += bad->no_dac;
1241 } else {
Takashi Iwaia7694092013-01-21 10:43:18 +01001242 /* print_nid_path("output", path); */
Takashi Iwaie1284af2013-01-03 16:33:02 +01001243 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +01001244 path_idx[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001245 badness += assign_out_path_ctls(codec, path);
Takashi Iwaie1284af2013-01-03 16:33:02 +01001246 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001247 }
1248
1249 return badness;
1250}
1251
1252/* return NID if the given pin has only a single connection to a certain DAC */
1253static hda_nid_t get_dac_if_single(struct hda_codec *codec, hda_nid_t pin)
1254{
1255 struct hda_gen_spec *spec = codec->spec;
1256 int i;
1257 hda_nid_t nid_found = 0;
1258
1259 for (i = 0; i < spec->num_all_dacs; i++) {
1260 hda_nid_t nid = spec->all_dacs[i];
1261 if (!nid || is_dac_already_used(codec, nid))
1262 continue;
1263 if (is_reachable_path(codec, nid, pin)) {
1264 if (nid_found)
1265 return 0;
1266 nid_found = nid;
1267 }
1268 }
1269 return nid_found;
1270}
1271
1272/* check whether the given pin can be a multi-io pin */
1273static bool can_be_multiio_pin(struct hda_codec *codec,
1274 unsigned int location, hda_nid_t nid)
1275{
1276 unsigned int defcfg, caps;
1277
1278 defcfg = snd_hda_codec_get_pincfg(codec, nid);
1279 if (get_defcfg_connect(defcfg) != AC_JACK_PORT_COMPLEX)
1280 return false;
1281 if (location && get_defcfg_location(defcfg) != location)
1282 return false;
1283 caps = snd_hda_query_pin_caps(codec, nid);
1284 if (!(caps & AC_PINCAP_OUT))
1285 return false;
1286 return true;
1287}
1288
Takashi Iwaie22aab72013-01-04 14:50:04 +01001289/* count the number of input pins that are capable to be multi-io */
1290static int count_multiio_pins(struct hda_codec *codec, hda_nid_t reference_pin)
1291{
1292 struct hda_gen_spec *spec = codec->spec;
1293 struct auto_pin_cfg *cfg = &spec->autocfg;
1294 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1295 unsigned int location = get_defcfg_location(defcfg);
1296 int type, i;
1297 int num_pins = 0;
1298
1299 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1300 for (i = 0; i < cfg->num_inputs; i++) {
1301 if (cfg->inputs[i].type != type)
1302 continue;
1303 if (can_be_multiio_pin(codec, location,
1304 cfg->inputs[i].pin))
1305 num_pins++;
1306 }
1307 }
1308 return num_pins;
1309}
1310
Takashi Iwai352f7f92012-12-19 12:52:06 +01001311/*
1312 * multi-io helper
1313 *
1314 * When hardwired is set, try to fill ony hardwired pins, and returns
1315 * zero if any pins are filled, non-zero if nothing found.
1316 * When hardwired is off, try to fill possible input pins, and returns
1317 * the badness value.
1318 */
1319static int fill_multi_ios(struct hda_codec *codec,
1320 hda_nid_t reference_pin,
Takashi Iwaie22aab72013-01-04 14:50:04 +01001321 bool hardwired)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001322{
1323 struct hda_gen_spec *spec = codec->spec;
1324 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwaie22aab72013-01-04 14:50:04 +01001325 int type, i, j, num_pins, old_pins;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001326 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1327 unsigned int location = get_defcfg_location(defcfg);
1328 int badness = 0;
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001329 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001330
1331 old_pins = spec->multi_ios;
1332 if (old_pins >= 2)
1333 goto end_fill;
1334
Takashi Iwaie22aab72013-01-04 14:50:04 +01001335 num_pins = count_multiio_pins(codec, reference_pin);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001336 if (num_pins < 2)
1337 goto end_fill;
1338
Takashi Iwai352f7f92012-12-19 12:52:06 +01001339 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1340 for (i = 0; i < cfg->num_inputs; i++) {
1341 hda_nid_t nid = cfg->inputs[i].pin;
1342 hda_nid_t dac = 0;
1343
1344 if (cfg->inputs[i].type != type)
1345 continue;
1346 if (!can_be_multiio_pin(codec, location, nid))
1347 continue;
1348 for (j = 0; j < spec->multi_ios; j++) {
1349 if (nid == spec->multi_io[j].pin)
1350 break;
1351 }
1352 if (j < spec->multi_ios)
1353 continue;
1354
Takashi Iwai352f7f92012-12-19 12:52:06 +01001355 if (hardwired)
1356 dac = get_dac_if_single(codec, nid);
1357 else if (!dac)
1358 dac = look_for_dac(codec, nid, false);
1359 if (!dac) {
1360 badness++;
1361 continue;
1362 }
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001363 path = snd_hda_add_new_path(codec, dac, nid,
1364 -spec->mixer_nid);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001365 if (!path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001366 badness++;
1367 continue;
1368 }
Takashi Iwaia7694092013-01-21 10:43:18 +01001369 /* print_nid_path("multiio", path); */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001370 spec->multi_io[spec->multi_ios].pin = nid;
1371 spec->multi_io[spec->multi_ios].dac = dac;
Takashi Iwai196c17662013-01-04 15:01:40 +01001372 spec->out_paths[cfg->line_outs + spec->multi_ios] =
1373 snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001374 spec->multi_ios++;
1375 if (spec->multi_ios >= 2)
1376 break;
1377 }
1378 }
1379 end_fill:
1380 if (badness)
1381 badness = BAD_MULTI_IO;
1382 if (old_pins == spec->multi_ios) {
1383 if (hardwired)
1384 return 1; /* nothing found */
1385 else
1386 return badness; /* no badness if nothing found */
1387 }
1388 if (!hardwired && spec->multi_ios < 2) {
1389 /* cancel newly assigned paths */
1390 spec->paths.used -= spec->multi_ios - old_pins;
1391 spec->multi_ios = old_pins;
1392 return badness;
1393 }
1394
1395 /* assign volume and mute controls */
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001396 for (i = old_pins; i < spec->multi_ios; i++) {
1397 path = snd_hda_get_path_from_idx(codec, spec->out_paths[cfg->line_outs + i]);
1398 badness += assign_out_path_ctls(codec, path);
1399 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001400
1401 return badness;
1402}
1403
1404/* map DACs for all pins in the list if they are single connections */
1405static bool map_singles(struct hda_codec *codec, int outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001406 const hda_nid_t *pins, hda_nid_t *dacs, int *path_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001407{
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001408 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001409 int i;
1410 bool found = false;
1411 for (i = 0; i < outs; i++) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001412 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001413 hda_nid_t dac;
1414 if (dacs[i])
1415 continue;
1416 dac = get_dac_if_single(codec, pins[i]);
1417 if (!dac)
1418 continue;
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001419 path = snd_hda_add_new_path(codec, dac, pins[i],
1420 -spec->mixer_nid);
Takashi Iwai117688a2013-01-04 15:41:41 +01001421 if (!path && !i && spec->mixer_nid)
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001422 path = snd_hda_add_new_path(codec, dac, pins[i], 0);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001423 if (path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001424 dacs[i] = dac;
1425 found = true;
Takashi Iwaia7694092013-01-21 10:43:18 +01001426 /* print_nid_path("output", path); */
Takashi Iwaie1284af2013-01-03 16:33:02 +01001427 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +01001428 path_idx[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001429 }
1430 }
1431 return found;
1432}
1433
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001434/* create a new path including aamix if available, and return its index */
1435static int check_aamix_out_path(struct hda_codec *codec, int path_idx)
1436{
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001437 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001438 struct nid_path *path;
Takashi Iwai5ead56f2013-04-16 14:16:54 +02001439 hda_nid_t path_dac, dac, pin;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001440
1441 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001442 if (!path || !path->depth ||
1443 is_nid_contained(path, spec->mixer_nid))
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001444 return 0;
Takashi Iwai5ead56f2013-04-16 14:16:54 +02001445 path_dac = path->path[0];
1446 dac = spec->private_dac_nids[0];
Takashi Iwaif87498b2013-01-21 14:24:31 +01001447 pin = path->path[path->depth - 1];
1448 path = snd_hda_add_new_path(codec, dac, pin, spec->mixer_nid);
1449 if (!path) {
Takashi Iwai5ead56f2013-04-16 14:16:54 +02001450 if (dac != path_dac)
1451 dac = path_dac;
Takashi Iwaif87498b2013-01-21 14:24:31 +01001452 else if (spec->multiout.hp_out_nid[0])
1453 dac = spec->multiout.hp_out_nid[0];
1454 else if (spec->multiout.extra_out_nid[0])
1455 dac = spec->multiout.extra_out_nid[0];
Takashi Iwai5ead56f2013-04-16 14:16:54 +02001456 else
1457 dac = 0;
Takashi Iwaif87498b2013-01-21 14:24:31 +01001458 if (dac)
1459 path = snd_hda_add_new_path(codec, dac, pin,
1460 spec->mixer_nid);
1461 }
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001462 if (!path)
1463 return 0;
Takashi Iwaia7694092013-01-21 10:43:18 +01001464 /* print_nid_path("output-aamix", path); */
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001465 path->active = false; /* unused as default */
1466 return snd_hda_get_path_idx(codec, path);
1467}
1468
Takashi Iwai55a63d42013-03-21 17:20:12 +01001469/* check whether the independent HP is available with the current config */
1470static bool indep_hp_possible(struct hda_codec *codec)
1471{
1472 struct hda_gen_spec *spec = codec->spec;
1473 struct auto_pin_cfg *cfg = &spec->autocfg;
1474 struct nid_path *path;
1475 int i, idx;
1476
1477 if (cfg->line_out_type == AUTO_PIN_HP_OUT)
1478 idx = spec->out_paths[0];
1479 else
1480 idx = spec->hp_paths[0];
1481 path = snd_hda_get_path_from_idx(codec, idx);
1482 if (!path)
1483 return false;
1484
1485 /* assume no path conflicts unless aamix is involved */
1486 if (!spec->mixer_nid || !is_nid_contained(path, spec->mixer_nid))
1487 return true;
1488
1489 /* check whether output paths contain aamix */
1490 for (i = 0; i < cfg->line_outs; i++) {
1491 if (spec->out_paths[i] == idx)
1492 break;
1493 path = snd_hda_get_path_from_idx(codec, spec->out_paths[i]);
1494 if (path && is_nid_contained(path, spec->mixer_nid))
1495 return false;
1496 }
1497 for (i = 0; i < cfg->speaker_outs; i++) {
1498 path = snd_hda_get_path_from_idx(codec, spec->speaker_paths[i]);
1499 if (path && is_nid_contained(path, spec->mixer_nid))
1500 return false;
1501 }
1502
1503 return true;
1504}
1505
Takashi Iwaia07a9492013-01-07 16:44:06 +01001506/* fill the empty entries in the dac array for speaker/hp with the
1507 * shared dac pointed by the paths
1508 */
1509static void refill_shared_dacs(struct hda_codec *codec, int num_outs,
1510 hda_nid_t *dacs, int *path_idx)
1511{
1512 struct nid_path *path;
1513 int i;
1514
1515 for (i = 0; i < num_outs; i++) {
1516 if (dacs[i])
1517 continue;
1518 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
1519 if (!path)
1520 continue;
1521 dacs[i] = path->path[0];
1522 }
1523}
1524
Takashi Iwai352f7f92012-12-19 12:52:06 +01001525/* fill in the dac_nids table from the parsed pin configuration */
1526static int fill_and_eval_dacs(struct hda_codec *codec,
1527 bool fill_hardwired,
1528 bool fill_mio_first)
1529{
1530 struct hda_gen_spec *spec = codec->spec;
1531 struct auto_pin_cfg *cfg = &spec->autocfg;
1532 int i, err, badness;
1533
1534 /* set num_dacs once to full for look_for_dac() */
1535 spec->multiout.num_dacs = cfg->line_outs;
1536 spec->multiout.dac_nids = spec->private_dac_nids;
1537 memset(spec->private_dac_nids, 0, sizeof(spec->private_dac_nids));
1538 memset(spec->multiout.hp_out_nid, 0, sizeof(spec->multiout.hp_out_nid));
1539 memset(spec->multiout.extra_out_nid, 0, sizeof(spec->multiout.extra_out_nid));
1540 spec->multi_ios = 0;
1541 snd_array_free(&spec->paths);
Takashi Iwaicd5be3f2013-01-07 15:07:00 +01001542
1543 /* clear path indices */
1544 memset(spec->out_paths, 0, sizeof(spec->out_paths));
1545 memset(spec->hp_paths, 0, sizeof(spec->hp_paths));
1546 memset(spec->speaker_paths, 0, sizeof(spec->speaker_paths));
1547 memset(spec->aamix_out_paths, 0, sizeof(spec->aamix_out_paths));
1548 memset(spec->digout_paths, 0, sizeof(spec->digout_paths));
Takashi Iwaic697b712013-01-07 17:09:26 +01001549 memset(spec->input_paths, 0, sizeof(spec->input_paths));
Takashi Iwaicd5be3f2013-01-07 15:07:00 +01001550 memset(spec->loopback_paths, 0, sizeof(spec->loopback_paths));
1551 memset(&spec->digin_path, 0, sizeof(spec->digin_path));
1552
Takashi Iwai352f7f92012-12-19 12:52:06 +01001553 badness = 0;
1554
1555 /* fill hard-wired DACs first */
1556 if (fill_hardwired) {
1557 bool mapped;
1558 do {
1559 mapped = map_singles(codec, cfg->line_outs,
1560 cfg->line_out_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001561 spec->private_dac_nids,
1562 spec->out_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001563 mapped |= map_singles(codec, cfg->hp_outs,
1564 cfg->hp_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001565 spec->multiout.hp_out_nid,
1566 spec->hp_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001567 mapped |= map_singles(codec, cfg->speaker_outs,
1568 cfg->speaker_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001569 spec->multiout.extra_out_nid,
1570 spec->speaker_paths);
Takashi Iwaida96fb52013-07-29 16:54:36 +02001571 if (!spec->no_multi_io &&
1572 fill_mio_first && cfg->line_outs == 1 &&
Takashi Iwai352f7f92012-12-19 12:52:06 +01001573 cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
Takashi Iwaie22aab72013-01-04 14:50:04 +01001574 err = fill_multi_ios(codec, cfg->line_out_pins[0], true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001575 if (!err)
1576 mapped = true;
1577 }
1578 } while (mapped);
1579 }
1580
1581 badness += try_assign_dacs(codec, cfg->line_outs, cfg->line_out_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001582 spec->private_dac_nids, spec->out_paths,
Takashi Iwai98bd1112013-03-22 14:53:50 +01001583 spec->main_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001584
Takashi Iwaida96fb52013-07-29 16:54:36 +02001585 if (!spec->no_multi_io && fill_mio_first &&
Takashi Iwai352f7f92012-12-19 12:52:06 +01001586 cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1587 /* try to fill multi-io first */
Takashi Iwaie22aab72013-01-04 14:50:04 +01001588 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001589 if (err < 0)
1590 return err;
1591 /* we don't count badness at this stage yet */
1592 }
1593
1594 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
1595 err = try_assign_dacs(codec, cfg->hp_outs, cfg->hp_pins,
1596 spec->multiout.hp_out_nid,
Takashi Iwai196c17662013-01-04 15:01:40 +01001597 spec->hp_paths,
Takashi Iwai98bd1112013-03-22 14:53:50 +01001598 spec->extra_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001599 if (err < 0)
1600 return err;
1601 badness += err;
1602 }
1603 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1604 err = try_assign_dacs(codec, cfg->speaker_outs,
1605 cfg->speaker_pins,
1606 spec->multiout.extra_out_nid,
Takashi Iwai196c17662013-01-04 15:01:40 +01001607 spec->speaker_paths,
Takashi Iwai98bd1112013-03-22 14:53:50 +01001608 spec->extra_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001609 if (err < 0)
1610 return err;
1611 badness += err;
1612 }
Takashi Iwaida96fb52013-07-29 16:54:36 +02001613 if (!spec->no_multi_io &&
1614 cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
Takashi Iwaie22aab72013-01-04 14:50:04 +01001615 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001616 if (err < 0)
1617 return err;
1618 badness += err;
1619 }
Takashi Iwaie22aab72013-01-04 14:50:04 +01001620
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001621 if (spec->mixer_nid) {
1622 spec->aamix_out_paths[0] =
1623 check_aamix_out_path(codec, spec->out_paths[0]);
1624 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1625 spec->aamix_out_paths[1] =
1626 check_aamix_out_path(codec, spec->hp_paths[0]);
1627 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1628 spec->aamix_out_paths[2] =
1629 check_aamix_out_path(codec, spec->speaker_paths[0]);
1630 }
1631
Takashi Iwaida96fb52013-07-29 16:54:36 +02001632 if (!spec->no_multi_io &&
1633 cfg->hp_outs && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
Takashi Iwaie22aab72013-01-04 14:50:04 +01001634 if (count_multiio_pins(codec, cfg->hp_pins[0]) >= 2)
1635 spec->multi_ios = 1; /* give badness */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001636
Takashi Iwaia07a9492013-01-07 16:44:06 +01001637 /* re-count num_dacs and squash invalid entries */
1638 spec->multiout.num_dacs = 0;
1639 for (i = 0; i < cfg->line_outs; i++) {
1640 if (spec->private_dac_nids[i])
1641 spec->multiout.num_dacs++;
1642 else {
1643 memmove(spec->private_dac_nids + i,
1644 spec->private_dac_nids + i + 1,
1645 sizeof(hda_nid_t) * (cfg->line_outs - i - 1));
1646 spec->private_dac_nids[cfg->line_outs - 1] = 0;
1647 }
1648 }
1649
1650 spec->ext_channel_count = spec->min_channel_count =
David Henningssonc0f3b212013-01-16 11:45:37 +01001651 spec->multiout.num_dacs * 2;
Takashi Iwaia07a9492013-01-07 16:44:06 +01001652
Takashi Iwai352f7f92012-12-19 12:52:06 +01001653 if (spec->multi_ios == 2) {
1654 for (i = 0; i < 2; i++)
1655 spec->private_dac_nids[spec->multiout.num_dacs++] =
1656 spec->multi_io[i].dac;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001657 } else if (spec->multi_ios) {
1658 spec->multi_ios = 0;
1659 badness += BAD_MULTI_IO;
1660 }
1661
Takashi Iwai55a63d42013-03-21 17:20:12 +01001662 if (spec->indep_hp && !indep_hp_possible(codec))
1663 badness += BAD_NO_INDEP_HP;
1664
Takashi Iwaia07a9492013-01-07 16:44:06 +01001665 /* re-fill the shared DAC for speaker / headphone */
1666 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1667 refill_shared_dacs(codec, cfg->hp_outs,
1668 spec->multiout.hp_out_nid,
1669 spec->hp_paths);
1670 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1671 refill_shared_dacs(codec, cfg->speaker_outs,
1672 spec->multiout.extra_out_nid,
1673 spec->speaker_paths);
1674
Takashi Iwai352f7f92012-12-19 12:52:06 +01001675 return badness;
1676}
1677
1678#define DEBUG_BADNESS
1679
1680#ifdef DEBUG_BADNESS
1681#define debug_badness snd_printdd
1682#else
1683#define debug_badness(...)
1684#endif
1685
Takashi Iwaia7694092013-01-21 10:43:18 +01001686#ifdef DEBUG_BADNESS
1687static inline void print_nid_path_idx(struct hda_codec *codec,
1688 const char *pfx, int idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001689{
Takashi Iwaia7694092013-01-21 10:43:18 +01001690 struct nid_path *path;
1691
1692 path = snd_hda_get_path_from_idx(codec, idx);
1693 if (path)
1694 print_nid_path(pfx, path);
1695}
1696
1697static void debug_show_configs(struct hda_codec *codec,
1698 struct auto_pin_cfg *cfg)
1699{
1700 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia7694092013-01-21 10:43:18 +01001701 static const char * const lo_type[3] = { "LO", "SP", "HP" };
Takashi Iwaia7694092013-01-21 10:43:18 +01001702 int i;
1703
1704 debug_badness("multi_outs = %x/%x/%x/%x : %x/%x/%x/%x (type %s)\n",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001705 cfg->line_out_pins[0], cfg->line_out_pins[1],
Takashi Iwai708122e2012-12-20 17:56:57 +01001706 cfg->line_out_pins[2], cfg->line_out_pins[3],
Takashi Iwai352f7f92012-12-19 12:52:06 +01001707 spec->multiout.dac_nids[0],
1708 spec->multiout.dac_nids[1],
1709 spec->multiout.dac_nids[2],
Takashi Iwaia7694092013-01-21 10:43:18 +01001710 spec->multiout.dac_nids[3],
1711 lo_type[cfg->line_out_type]);
1712 for (i = 0; i < cfg->line_outs; i++)
1713 print_nid_path_idx(codec, " out", spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001714 if (spec->multi_ios > 0)
1715 debug_badness("multi_ios(%d) = %x/%x : %x/%x\n",
1716 spec->multi_ios,
1717 spec->multi_io[0].pin, spec->multi_io[1].pin,
1718 spec->multi_io[0].dac, spec->multi_io[1].dac);
Takashi Iwaia7694092013-01-21 10:43:18 +01001719 for (i = 0; i < spec->multi_ios; i++)
1720 print_nid_path_idx(codec, " mio",
1721 spec->out_paths[cfg->line_outs + i]);
1722 if (cfg->hp_outs)
1723 debug_badness("hp_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001724 cfg->hp_pins[0], cfg->hp_pins[1],
Takashi Iwai708122e2012-12-20 17:56:57 +01001725 cfg->hp_pins[2], cfg->hp_pins[3],
Takashi Iwai352f7f92012-12-19 12:52:06 +01001726 spec->multiout.hp_out_nid[0],
1727 spec->multiout.hp_out_nid[1],
1728 spec->multiout.hp_out_nid[2],
1729 spec->multiout.hp_out_nid[3]);
Takashi Iwaia7694092013-01-21 10:43:18 +01001730 for (i = 0; i < cfg->hp_outs; i++)
1731 print_nid_path_idx(codec, " hp ", spec->hp_paths[i]);
1732 if (cfg->speaker_outs)
1733 debug_badness("spk_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001734 cfg->speaker_pins[0], cfg->speaker_pins[1],
1735 cfg->speaker_pins[2], cfg->speaker_pins[3],
1736 spec->multiout.extra_out_nid[0],
1737 spec->multiout.extra_out_nid[1],
1738 spec->multiout.extra_out_nid[2],
1739 spec->multiout.extra_out_nid[3]);
Takashi Iwaia7694092013-01-21 10:43:18 +01001740 for (i = 0; i < cfg->speaker_outs; i++)
1741 print_nid_path_idx(codec, " spk", spec->speaker_paths[i]);
1742 for (i = 0; i < 3; i++)
1743 print_nid_path_idx(codec, " mix", spec->aamix_out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001744}
Takashi Iwaia7694092013-01-21 10:43:18 +01001745#else
1746#define debug_show_configs(codec, cfg) /* NOP */
1747#endif
Takashi Iwai352f7f92012-12-19 12:52:06 +01001748
1749/* find all available DACs of the codec */
1750static void fill_all_dac_nids(struct hda_codec *codec)
1751{
1752 struct hda_gen_spec *spec = codec->spec;
1753 int i;
1754 hda_nid_t nid = codec->start_nid;
1755
1756 spec->num_all_dacs = 0;
1757 memset(spec->all_dacs, 0, sizeof(spec->all_dacs));
1758 for (i = 0; i < codec->num_nodes; i++, nid++) {
1759 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_AUD_OUT)
1760 continue;
1761 if (spec->num_all_dacs >= ARRAY_SIZE(spec->all_dacs)) {
1762 snd_printk(KERN_ERR "hda: Too many DACs!\n");
1763 break;
1764 }
1765 spec->all_dacs[spec->num_all_dacs++] = nid;
1766 }
1767}
1768
1769static int parse_output_paths(struct hda_codec *codec)
1770{
1771 struct hda_gen_spec *spec = codec->spec;
1772 struct auto_pin_cfg *cfg = &spec->autocfg;
1773 struct auto_pin_cfg *best_cfg;
Takashi Iwai9314a582013-01-21 10:49:05 +01001774 unsigned int val;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001775 int best_badness = INT_MAX;
1776 int badness;
1777 bool fill_hardwired = true, fill_mio_first = true;
1778 bool best_wired = true, best_mio = true;
1779 bool hp_spk_swapped = false;
1780
Takashi Iwai352f7f92012-12-19 12:52:06 +01001781 best_cfg = kmalloc(sizeof(*best_cfg), GFP_KERNEL);
1782 if (!best_cfg)
1783 return -ENOMEM;
1784 *best_cfg = *cfg;
1785
1786 for (;;) {
1787 badness = fill_and_eval_dacs(codec, fill_hardwired,
1788 fill_mio_first);
1789 if (badness < 0) {
1790 kfree(best_cfg);
1791 return badness;
1792 }
1793 debug_badness("==> lo_type=%d, wired=%d, mio=%d, badness=0x%x\n",
1794 cfg->line_out_type, fill_hardwired, fill_mio_first,
1795 badness);
Takashi Iwaia7694092013-01-21 10:43:18 +01001796 debug_show_configs(codec, cfg);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001797 if (badness < best_badness) {
1798 best_badness = badness;
1799 *best_cfg = *cfg;
1800 best_wired = fill_hardwired;
1801 best_mio = fill_mio_first;
1802 }
1803 if (!badness)
1804 break;
1805 fill_mio_first = !fill_mio_first;
1806 if (!fill_mio_first)
1807 continue;
1808 fill_hardwired = !fill_hardwired;
1809 if (!fill_hardwired)
1810 continue;
1811 if (hp_spk_swapped)
1812 break;
1813 hp_spk_swapped = true;
1814 if (cfg->speaker_outs > 0 &&
1815 cfg->line_out_type == AUTO_PIN_HP_OUT) {
1816 cfg->hp_outs = cfg->line_outs;
1817 memcpy(cfg->hp_pins, cfg->line_out_pins,
1818 sizeof(cfg->hp_pins));
1819 cfg->line_outs = cfg->speaker_outs;
1820 memcpy(cfg->line_out_pins, cfg->speaker_pins,
1821 sizeof(cfg->speaker_pins));
1822 cfg->speaker_outs = 0;
1823 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
1824 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
1825 fill_hardwired = true;
1826 continue;
1827 }
1828 if (cfg->hp_outs > 0 &&
1829 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
1830 cfg->speaker_outs = cfg->line_outs;
1831 memcpy(cfg->speaker_pins, cfg->line_out_pins,
1832 sizeof(cfg->speaker_pins));
1833 cfg->line_outs = cfg->hp_outs;
1834 memcpy(cfg->line_out_pins, cfg->hp_pins,
1835 sizeof(cfg->hp_pins));
1836 cfg->hp_outs = 0;
1837 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
1838 cfg->line_out_type = AUTO_PIN_HP_OUT;
1839 fill_hardwired = true;
1840 continue;
1841 }
1842 break;
1843 }
1844
1845 if (badness) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001846 debug_badness("==> restoring best_cfg\n");
Takashi Iwai352f7f92012-12-19 12:52:06 +01001847 *cfg = *best_cfg;
1848 fill_and_eval_dacs(codec, best_wired, best_mio);
1849 }
1850 debug_badness("==> Best config: lo_type=%d, wired=%d, mio=%d\n",
1851 cfg->line_out_type, best_wired, best_mio);
Takashi Iwaia7694092013-01-21 10:43:18 +01001852 debug_show_configs(codec, cfg);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001853
1854 if (cfg->line_out_pins[0]) {
1855 struct nid_path *path;
Takashi Iwai196c17662013-01-04 15:01:40 +01001856 path = snd_hda_get_path_from_idx(codec, spec->out_paths[0]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001857 if (path)
1858 spec->vmaster_nid = look_for_out_vol_nid(codec, path);
Takashi Iwai7a71bbf2013-01-17 10:25:15 +01001859 if (spec->vmaster_nid)
1860 snd_hda_set_vmaster_tlv(codec, spec->vmaster_nid,
1861 HDA_OUTPUT, spec->vmaster_tlv);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001862 }
1863
Takashi Iwai9314a582013-01-21 10:49:05 +01001864 /* set initial pinctl targets */
1865 if (spec->prefer_hp_amp || cfg->line_out_type == AUTO_PIN_HP_OUT)
1866 val = PIN_HP;
1867 else
1868 val = PIN_OUT;
1869 set_pin_targets(codec, cfg->line_outs, cfg->line_out_pins, val);
1870 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1871 set_pin_targets(codec, cfg->hp_outs, cfg->hp_pins, PIN_HP);
1872 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1873 val = spec->prefer_hp_amp ? PIN_HP : PIN_OUT;
1874 set_pin_targets(codec, cfg->speaker_outs,
1875 cfg->speaker_pins, val);
1876 }
1877
Takashi Iwai55a63d42013-03-21 17:20:12 +01001878 /* clear indep_hp flag if not available */
1879 if (spec->indep_hp && !indep_hp_possible(codec))
1880 spec->indep_hp = 0;
1881
Takashi Iwai352f7f92012-12-19 12:52:06 +01001882 kfree(best_cfg);
1883 return 0;
1884}
1885
1886/* add playback controls from the parsed DAC table */
1887static int create_multi_out_ctls(struct hda_codec *codec,
1888 const struct auto_pin_cfg *cfg)
1889{
1890 struct hda_gen_spec *spec = codec->spec;
1891 int i, err, noutputs;
1892
1893 noutputs = cfg->line_outs;
1894 if (spec->multi_ios > 0 && cfg->line_outs < 3)
1895 noutputs += spec->multi_ios;
1896
1897 for (i = 0; i < noutputs; i++) {
1898 const char *name;
1899 int index;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001900 struct nid_path *path;
1901
Takashi Iwai196c17662013-01-04 15:01:40 +01001902 path = snd_hda_get_path_from_idx(codec, spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001903 if (!path)
1904 continue;
Takashi Iwai247d85e2013-01-17 16:18:11 +01001905
1906 name = get_line_out_pfx(codec, i, &index, NID_PATH_VOL_CTL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001907 if (!name || !strcmp(name, "CLFE")) {
1908 /* Center/LFE */
1909 err = add_vol_ctl(codec, "Center", 0, 1, path);
1910 if (err < 0)
1911 return err;
1912 err = add_vol_ctl(codec, "LFE", 0, 2, path);
1913 if (err < 0)
1914 return err;
Takashi Iwai247d85e2013-01-17 16:18:11 +01001915 } else {
1916 err = add_stereo_vol(codec, name, index, path);
1917 if (err < 0)
1918 return err;
1919 }
1920
1921 name = get_line_out_pfx(codec, i, &index, NID_PATH_MUTE_CTL);
1922 if (!name || !strcmp(name, "CLFE")) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001923 err = add_sw_ctl(codec, "Center", 0, 1, path);
1924 if (err < 0)
1925 return err;
1926 err = add_sw_ctl(codec, "LFE", 0, 2, path);
1927 if (err < 0)
1928 return err;
1929 } else {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001930 err = add_stereo_sw(codec, name, index, path);
1931 if (err < 0)
1932 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 }
1934 }
1935 return 0;
1936}
1937
Takashi Iwaic2c80382013-01-07 10:33:57 +01001938static int create_extra_out(struct hda_codec *codec, int path_idx,
Takashi Iwai196c17662013-01-04 15:01:40 +01001939 const char *pfx, int cidx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940{
Takashi Iwai352f7f92012-12-19 12:52:06 +01001941 struct nid_path *path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 int err;
1943
Takashi Iwai196c17662013-01-04 15:01:40 +01001944 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001945 if (!path)
1946 return 0;
Takashi Iwaic2c80382013-01-07 10:33:57 +01001947 err = add_stereo_vol(codec, pfx, cidx, path);
1948 if (err < 0)
1949 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001950 err = add_stereo_sw(codec, pfx, cidx, path);
1951 if (err < 0)
1952 return err;
1953 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954}
1955
Takashi Iwai352f7f92012-12-19 12:52:06 +01001956/* add playback controls for speaker and HP outputs */
1957static int create_extra_outs(struct hda_codec *codec, int num_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001958 const int *paths, const char *pfx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959{
Takashi Iwaic2c80382013-01-07 10:33:57 +01001960 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001961
1962 for (i = 0; i < num_pins; i++) {
Takashi Iwaic2c80382013-01-07 10:33:57 +01001963 const char *name;
Takashi Iwai975cc022013-06-28 11:56:49 +02001964 char tmp[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
Takashi Iwaic2c80382013-01-07 10:33:57 +01001965 int err, idx = 0;
1966
1967 if (num_pins == 2 && i == 1 && !strcmp(pfx, "Speaker"))
1968 name = "Bass Speaker";
1969 else if (num_pins >= 3) {
1970 snprintf(tmp, sizeof(tmp), "%s %s",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001971 pfx, channel_name[i]);
Takashi Iwaic2c80382013-01-07 10:33:57 +01001972 name = tmp;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001973 } else {
Takashi Iwaic2c80382013-01-07 10:33:57 +01001974 name = pfx;
1975 idx = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 }
Takashi Iwaic2c80382013-01-07 10:33:57 +01001977 err = create_extra_out(codec, paths[i], name, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001978 if (err < 0)
1979 return err;
1980 }
1981 return 0;
1982}
Takashi Iwai97ec5582006-03-21 11:29:07 +01001983
Takashi Iwai352f7f92012-12-19 12:52:06 +01001984static int create_hp_out_ctls(struct hda_codec *codec)
1985{
1986 struct hda_gen_spec *spec = codec->spec;
1987 return create_extra_outs(codec, spec->autocfg.hp_outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001988 spec->hp_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001989 "Headphone");
1990}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991
Takashi Iwai352f7f92012-12-19 12:52:06 +01001992static int create_speaker_out_ctls(struct hda_codec *codec)
1993{
1994 struct hda_gen_spec *spec = codec->spec;
1995 return create_extra_outs(codec, spec->autocfg.speaker_outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001996 spec->speaker_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001997 "Speaker");
1998}
1999
2000/*
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002001 * independent HP controls
2002 */
2003
Takashi Iwai963afde2013-05-31 15:20:31 +02002004static void call_hp_automute(struct hda_codec *codec, struct hda_jack_tbl *jack);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002005static int indep_hp_info(struct snd_kcontrol *kcontrol,
2006 struct snd_ctl_elem_info *uinfo)
2007{
2008 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
2009}
2010
2011static int indep_hp_get(struct snd_kcontrol *kcontrol,
2012 struct snd_ctl_elem_value *ucontrol)
2013{
2014 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2015 struct hda_gen_spec *spec = codec->spec;
2016 ucontrol->value.enumerated.item[0] = spec->indep_hp_enabled;
2017 return 0;
2018}
2019
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002020static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
2021 int nomix_path_idx, int mix_path_idx,
2022 int out_type);
2023
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002024static int indep_hp_put(struct snd_kcontrol *kcontrol,
2025 struct snd_ctl_elem_value *ucontrol)
2026{
2027 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2028 struct hda_gen_spec *spec = codec->spec;
2029 unsigned int select = ucontrol->value.enumerated.item[0];
2030 int ret = 0;
2031
2032 mutex_lock(&spec->pcm_mutex);
2033 if (spec->active_streams) {
2034 ret = -EBUSY;
2035 goto unlock;
2036 }
2037
2038 if (spec->indep_hp_enabled != select) {
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002039 hda_nid_t *dacp;
2040 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
2041 dacp = &spec->private_dac_nids[0];
2042 else
2043 dacp = &spec->multiout.hp_out_nid[0];
2044
2045 /* update HP aamix paths in case it conflicts with indep HP */
2046 if (spec->have_aamix_ctl) {
2047 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
2048 update_aamix_paths(codec, spec->aamix_mode,
2049 spec->out_paths[0],
2050 spec->aamix_out_paths[0],
2051 spec->autocfg.line_out_type);
2052 else
2053 update_aamix_paths(codec, spec->aamix_mode,
2054 spec->hp_paths[0],
2055 spec->aamix_out_paths[1],
2056 AUTO_PIN_HP_OUT);
2057 }
2058
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002059 spec->indep_hp_enabled = select;
2060 if (spec->indep_hp_enabled)
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002061 *dacp = 0;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002062 else
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002063 *dacp = spec->alt_dac_nid;
Takashi Iwai92603c52013-01-22 07:46:31 +01002064
Takashi Iwai963afde2013-05-31 15:20:31 +02002065 call_hp_automute(codec, NULL);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002066 ret = 1;
2067 }
2068 unlock:
2069 mutex_unlock(&spec->pcm_mutex);
2070 return ret;
2071}
2072
2073static const struct snd_kcontrol_new indep_hp_ctl = {
2074 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2075 .name = "Independent HP",
2076 .info = indep_hp_info,
2077 .get = indep_hp_get,
2078 .put = indep_hp_put,
2079};
2080
2081
2082static int create_indep_hp_ctls(struct hda_codec *codec)
2083{
2084 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002085 hda_nid_t dac;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002086
2087 if (!spec->indep_hp)
2088 return 0;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002089 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
2090 dac = spec->multiout.dac_nids[0];
2091 else
2092 dac = spec->multiout.hp_out_nid[0];
2093 if (!dac) {
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002094 spec->indep_hp = 0;
2095 return 0;
2096 }
2097
2098 spec->indep_hp_enabled = false;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002099 spec->alt_dac_nid = dac;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002100 if (!snd_hda_gen_add_kctl(spec, NULL, &indep_hp_ctl))
2101 return -ENOMEM;
2102 return 0;
2103}
2104
2105/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01002106 * channel mode enum control
2107 */
2108
2109static int ch_mode_info(struct snd_kcontrol *kcontrol,
2110 struct snd_ctl_elem_info *uinfo)
2111{
2112 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2113 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002114 int chs;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002115
2116 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2117 uinfo->count = 1;
2118 uinfo->value.enumerated.items = spec->multi_ios + 1;
2119 if (uinfo->value.enumerated.item > spec->multi_ios)
2120 uinfo->value.enumerated.item = spec->multi_ios;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002121 chs = uinfo->value.enumerated.item * 2 + spec->min_channel_count;
2122 sprintf(uinfo->value.enumerated.name, "%dch", chs);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002123 return 0;
2124}
2125
2126static int ch_mode_get(struct snd_kcontrol *kcontrol,
2127 struct snd_ctl_elem_value *ucontrol)
2128{
2129 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2130 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002131 ucontrol->value.enumerated.item[0] =
2132 (spec->ext_channel_count - spec->min_channel_count) / 2;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002133 return 0;
2134}
2135
Takashi Iwai196c17662013-01-04 15:01:40 +01002136static inline struct nid_path *
2137get_multiio_path(struct hda_codec *codec, int idx)
2138{
2139 struct hda_gen_spec *spec = codec->spec;
2140 return snd_hda_get_path_from_idx(codec,
2141 spec->out_paths[spec->autocfg.line_outs + idx]);
2142}
2143
Takashi Iwaia5cc2502013-01-16 18:08:55 +01002144static void update_automute_all(struct hda_codec *codec);
2145
Takashi Iwai65033cc2013-04-16 12:31:05 +02002146/* Default value to be passed as aamix argument for snd_hda_activate_path();
2147 * used for output paths
2148 */
2149static bool aamix_default(struct hda_gen_spec *spec)
2150{
2151 return !spec->have_aamix_ctl || spec->aamix_mode;
2152}
2153
Takashi Iwai352f7f92012-12-19 12:52:06 +01002154static int set_multi_io(struct hda_codec *codec, int idx, bool output)
2155{
2156 struct hda_gen_spec *spec = codec->spec;
2157 hda_nid_t nid = spec->multi_io[idx].pin;
2158 struct nid_path *path;
2159
Takashi Iwai196c17662013-01-04 15:01:40 +01002160 path = get_multiio_path(codec, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002161 if (!path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 return -EINVAL;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002163
2164 if (path->active == output)
2165 return 0;
2166
2167 if (output) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01002168 set_pin_target(codec, nid, PIN_OUT, true);
Takashi Iwai65033cc2013-04-16 12:31:05 +02002169 snd_hda_activate_path(codec, path, true, aamix_default(spec));
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01002170 set_pin_eapd(codec, nid, true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002171 } else {
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01002172 set_pin_eapd(codec, nid, false);
Takashi Iwai65033cc2013-04-16 12:31:05 +02002173 snd_hda_activate_path(codec, path, false, aamix_default(spec));
Takashi Iwai2c12c302013-01-10 09:33:29 +01002174 set_pin_target(codec, nid, spec->multi_io[idx].ctl_in, true);
Takashi Iwai55196ff2013-01-24 17:32:56 +01002175 path_power_down_sync(codec, path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 }
Takashi Iwaia365fed2013-01-10 16:10:06 +01002177
2178 /* update jack retasking in case it modifies any of them */
Takashi Iwaia5cc2502013-01-16 18:08:55 +01002179 update_automute_all(codec);
Takashi Iwaia365fed2013-01-10 16:10:06 +01002180
Takashi Iwai352f7f92012-12-19 12:52:06 +01002181 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182}
2183
Takashi Iwai352f7f92012-12-19 12:52:06 +01002184static int ch_mode_put(struct snd_kcontrol *kcontrol,
2185 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002187 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2188 struct hda_gen_spec *spec = codec->spec;
2189 int i, ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190
Takashi Iwai352f7f92012-12-19 12:52:06 +01002191 ch = ucontrol->value.enumerated.item[0];
2192 if (ch < 0 || ch > spec->multi_ios)
2193 return -EINVAL;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002194 if (ch == (spec->ext_channel_count - spec->min_channel_count) / 2)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002195 return 0;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002196 spec->ext_channel_count = ch * 2 + spec->min_channel_count;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002197 for (i = 0; i < spec->multi_ios; i++)
2198 set_multi_io(codec, i, i < ch);
2199 spec->multiout.max_channels = max(spec->ext_channel_count,
2200 spec->const_channel_count);
2201 if (spec->need_dac_fix)
2202 spec->multiout.num_dacs = spec->multiout.max_channels / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 return 1;
2204}
2205
Takashi Iwai352f7f92012-12-19 12:52:06 +01002206static const struct snd_kcontrol_new channel_mode_enum = {
2207 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2208 .name = "Channel Mode",
2209 .info = ch_mode_info,
2210 .get = ch_mode_get,
2211 .put = ch_mode_put,
2212};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213
Takashi Iwai352f7f92012-12-19 12:52:06 +01002214static int create_multi_channel_mode(struct hda_codec *codec)
2215{
2216 struct hda_gen_spec *spec = codec->spec;
2217
2218 if (spec->multi_ios > 0) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01002219 if (!snd_hda_gen_add_kctl(spec, NULL, &channel_mode_enum))
Takashi Iwai352f7f92012-12-19 12:52:06 +01002220 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 return 0;
2223}
2224
Takashi Iwai352f7f92012-12-19 12:52:06 +01002225/*
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002226 * aamix loopback enable/disable switch
2227 */
2228
2229#define loopback_mixing_info indep_hp_info
2230
2231static int loopback_mixing_get(struct snd_kcontrol *kcontrol,
2232 struct snd_ctl_elem_value *ucontrol)
2233{
2234 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2235 struct hda_gen_spec *spec = codec->spec;
2236 ucontrol->value.enumerated.item[0] = spec->aamix_mode;
2237 return 0;
2238}
2239
2240static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002241 int nomix_path_idx, int mix_path_idx,
2242 int out_type)
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002243{
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002244 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002245 struct nid_path *nomix_path, *mix_path;
2246
2247 nomix_path = snd_hda_get_path_from_idx(codec, nomix_path_idx);
2248 mix_path = snd_hda_get_path_from_idx(codec, mix_path_idx);
2249 if (!nomix_path || !mix_path)
2250 return;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002251
2252 /* if HP aamix path is driven from a different DAC and the
2253 * independent HP mode is ON, can't turn on aamix path
2254 */
2255 if (out_type == AUTO_PIN_HP_OUT && spec->indep_hp_enabled &&
2256 mix_path->path[0] != spec->alt_dac_nid)
2257 do_mix = false;
2258
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002259 if (do_mix) {
2260 snd_hda_activate_path(codec, nomix_path, false, true);
2261 snd_hda_activate_path(codec, mix_path, true, true);
Takashi Iwai55196ff2013-01-24 17:32:56 +01002262 path_power_down_sync(codec, nomix_path);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002263 } else {
Takashi Iwai65033cc2013-04-16 12:31:05 +02002264 snd_hda_activate_path(codec, mix_path, false, false);
2265 snd_hda_activate_path(codec, nomix_path, true, false);
Takashi Iwai55196ff2013-01-24 17:32:56 +01002266 path_power_down_sync(codec, mix_path);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002267 }
2268}
2269
2270static int loopback_mixing_put(struct snd_kcontrol *kcontrol,
2271 struct snd_ctl_elem_value *ucontrol)
2272{
2273 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2274 struct hda_gen_spec *spec = codec->spec;
2275 unsigned int val = ucontrol->value.enumerated.item[0];
2276
2277 if (val == spec->aamix_mode)
2278 return 0;
2279 spec->aamix_mode = val;
2280 update_aamix_paths(codec, val, spec->out_paths[0],
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002281 spec->aamix_out_paths[0],
2282 spec->autocfg.line_out_type);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002283 update_aamix_paths(codec, val, spec->hp_paths[0],
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002284 spec->aamix_out_paths[1],
2285 AUTO_PIN_HP_OUT);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002286 update_aamix_paths(codec, val, spec->speaker_paths[0],
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002287 spec->aamix_out_paths[2],
2288 AUTO_PIN_SPEAKER_OUT);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002289 return 1;
2290}
2291
2292static const struct snd_kcontrol_new loopback_mixing_enum = {
2293 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2294 .name = "Loopback Mixing",
2295 .info = loopback_mixing_info,
2296 .get = loopback_mixing_get,
2297 .put = loopback_mixing_put,
2298};
2299
2300static int create_loopback_mixing_ctl(struct hda_codec *codec)
2301{
2302 struct hda_gen_spec *spec = codec->spec;
2303
2304 if (!spec->mixer_nid)
2305 return 0;
2306 if (!(spec->aamix_out_paths[0] || spec->aamix_out_paths[1] ||
2307 spec->aamix_out_paths[2]))
2308 return 0;
2309 if (!snd_hda_gen_add_kctl(spec, NULL, &loopback_mixing_enum))
2310 return -ENOMEM;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002311 spec->have_aamix_ctl = 1;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002312 return 0;
2313}
2314
2315/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01002316 * shared headphone/mic handling
2317 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02002318
Takashi Iwai352f7f92012-12-19 12:52:06 +01002319static void call_update_outputs(struct hda_codec *codec);
2320
2321/* for shared I/O, change the pin-control accordingly */
Takashi Iwai967303d2013-02-19 17:12:42 +01002322static void update_hp_mic(struct hda_codec *codec, int adc_mux, bool force)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002323{
2324 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai967303d2013-02-19 17:12:42 +01002325 bool as_mic;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002326 unsigned int val;
Takashi Iwai967303d2013-02-19 17:12:42 +01002327 hda_nid_t pin;
2328
2329 pin = spec->hp_mic_pin;
2330 as_mic = spec->cur_mux[adc_mux] == spec->hp_mic_mux_idx;
2331
2332 if (!force) {
2333 val = snd_hda_codec_get_pin_target(codec, pin);
2334 if (as_mic) {
2335 if (val & PIN_IN)
2336 return;
2337 } else {
2338 if (val & PIN_OUT)
2339 return;
2340 }
2341 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002342
2343 val = snd_hda_get_default_vref(codec, pin);
Takashi Iwai967303d2013-02-19 17:12:42 +01002344 /* if the HP pin doesn't support VREF and the codec driver gives an
2345 * alternative pin, set up the VREF on that pin instead
2346 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01002347 if (val == AC_PINCTL_VREF_HIZ && spec->shared_mic_vref_pin) {
2348 const hda_nid_t vref_pin = spec->shared_mic_vref_pin;
2349 unsigned int vref_val = snd_hda_get_default_vref(codec, vref_pin);
2350 if (vref_val != AC_PINCTL_VREF_HIZ)
Takashi Iwai7594aa32012-12-20 15:38:40 +01002351 snd_hda_set_pin_ctl_cache(codec, vref_pin,
Takashi Iwai967303d2013-02-19 17:12:42 +01002352 PIN_IN | (as_mic ? vref_val : 0));
Takashi Iwaicb53c622007-08-10 17:21:45 +02002353 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002354
Takashi Iwai8ba955c2013-03-07 18:40:58 +01002355 if (!spec->hp_mic_jack_modes) {
2356 if (as_mic)
2357 val |= PIN_IN;
2358 else
2359 val = PIN_HP;
2360 set_pin_target(codec, pin, val, true);
Takashi Iwai963afde2013-05-31 15:20:31 +02002361 call_hp_automute(codec, NULL);
Takashi Iwai8ba955c2013-03-07 18:40:58 +01002362 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002363}
2364
2365/* create a shared input with the headphone out */
Takashi Iwai967303d2013-02-19 17:12:42 +01002366static int create_hp_mic(struct hda_codec *codec)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002367{
2368 struct hda_gen_spec *spec = codec->spec;
2369 struct auto_pin_cfg *cfg = &spec->autocfg;
2370 unsigned int defcfg;
2371 hda_nid_t nid;
2372
Takashi Iwai967303d2013-02-19 17:12:42 +01002373 if (!spec->hp_mic) {
2374 if (spec->suppress_hp_mic_detect)
2375 return 0;
2376 /* automatic detection: only if no input or a single internal
2377 * input pin is found, try to detect the shared hp/mic
2378 */
2379 if (cfg->num_inputs > 1)
2380 return 0;
2381 else if (cfg->num_inputs == 1) {
2382 defcfg = snd_hda_codec_get_pincfg(codec, cfg->inputs[0].pin);
2383 if (snd_hda_get_input_pin_attr(defcfg) != INPUT_PIN_ATTR_INT)
2384 return 0;
2385 }
2386 }
2387
2388 spec->hp_mic = 0; /* clear once */
2389 if (cfg->num_inputs >= AUTO_CFG_MAX_INS)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002390 return 0;
2391
Takashi Iwai967303d2013-02-19 17:12:42 +01002392 nid = 0;
2393 if (cfg->line_out_type == AUTO_PIN_HP_OUT && cfg->line_outs > 0)
2394 nid = cfg->line_out_pins[0];
2395 else if (cfg->hp_outs > 0)
2396 nid = cfg->hp_pins[0];
2397 if (!nid)
2398 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002399
2400 if (!(snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_IN))
2401 return 0; /* no input */
2402
Takashi Iwai967303d2013-02-19 17:12:42 +01002403 cfg->inputs[cfg->num_inputs].pin = nid;
2404 cfg->inputs[cfg->num_inputs].type = AUTO_PIN_MIC;
David Henningssoncb420b12013-04-11 11:30:28 +02002405 cfg->inputs[cfg->num_inputs].is_headphone_mic = 1;
Takashi Iwai967303d2013-02-19 17:12:42 +01002406 cfg->num_inputs++;
2407 spec->hp_mic = 1;
2408 spec->hp_mic_pin = nid;
2409 /* we can't handle auto-mic together with HP-mic */
2410 spec->suppress_auto_mic = 1;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002411 snd_printdd("hda-codec: Enable shared I/O jack on NID 0x%x\n", nid);
2412 return 0;
2413}
2414
Takashi Iwai978e77e2013-01-10 16:57:58 +01002415/*
2416 * output jack mode
2417 */
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002418
2419static int create_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t pin);
2420
2421static const char * const out_jack_texts[] = {
2422 "Line Out", "Headphone Out",
2423};
2424
Takashi Iwai978e77e2013-01-10 16:57:58 +01002425static int out_jack_mode_info(struct snd_kcontrol *kcontrol,
2426 struct snd_ctl_elem_info *uinfo)
2427{
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002428 return snd_hda_enum_helper_info(kcontrol, uinfo, 2, out_jack_texts);
Takashi Iwai978e77e2013-01-10 16:57:58 +01002429}
2430
2431static int out_jack_mode_get(struct snd_kcontrol *kcontrol,
2432 struct snd_ctl_elem_value *ucontrol)
2433{
2434 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2435 hda_nid_t nid = kcontrol->private_value;
2436 if (snd_hda_codec_get_pin_target(codec, nid) == PIN_HP)
2437 ucontrol->value.enumerated.item[0] = 1;
2438 else
2439 ucontrol->value.enumerated.item[0] = 0;
2440 return 0;
2441}
2442
2443static int out_jack_mode_put(struct snd_kcontrol *kcontrol,
2444 struct snd_ctl_elem_value *ucontrol)
2445{
2446 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2447 hda_nid_t nid = kcontrol->private_value;
2448 unsigned int val;
2449
2450 val = ucontrol->value.enumerated.item[0] ? PIN_HP : PIN_OUT;
2451 if (snd_hda_codec_get_pin_target(codec, nid) == val)
2452 return 0;
2453 snd_hda_set_pin_ctl_cache(codec, nid, val);
2454 return 1;
2455}
2456
2457static const struct snd_kcontrol_new out_jack_mode_enum = {
2458 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2459 .info = out_jack_mode_info,
2460 .get = out_jack_mode_get,
2461 .put = out_jack_mode_put,
2462};
2463
2464static bool find_kctl_name(struct hda_codec *codec, const char *name, int idx)
2465{
2466 struct hda_gen_spec *spec = codec->spec;
2467 int i;
2468
2469 for (i = 0; i < spec->kctls.used; i++) {
2470 struct snd_kcontrol_new *kctl = snd_array_elem(&spec->kctls, i);
2471 if (!strcmp(kctl->name, name) && kctl->index == idx)
2472 return true;
2473 }
2474 return false;
2475}
2476
2477static void get_jack_mode_name(struct hda_codec *codec, hda_nid_t pin,
2478 char *name, size_t name_len)
2479{
2480 struct hda_gen_spec *spec = codec->spec;
2481 int idx = 0;
2482
2483 snd_hda_get_pin_label(codec, pin, &spec->autocfg, name, name_len, &idx);
2484 strlcat(name, " Jack Mode", name_len);
2485
2486 for (; find_kctl_name(codec, name, idx); idx++)
2487 ;
2488}
2489
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002490static int get_out_jack_num_items(struct hda_codec *codec, hda_nid_t pin)
2491{
2492 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaif811c3c2013-03-07 18:32:59 +01002493 if (spec->add_jack_modes) {
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002494 unsigned int pincap = snd_hda_query_pin_caps(codec, pin);
2495 if ((pincap & AC_PINCAP_OUT) && (pincap & AC_PINCAP_HP_DRV))
2496 return 2;
2497 }
2498 return 1;
2499}
2500
Takashi Iwai978e77e2013-01-10 16:57:58 +01002501static int create_out_jack_modes(struct hda_codec *codec, int num_pins,
2502 hda_nid_t *pins)
2503{
2504 struct hda_gen_spec *spec = codec->spec;
2505 int i;
2506
2507 for (i = 0; i < num_pins; i++) {
2508 hda_nid_t pin = pins[i];
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002509 if (pin == spec->hp_mic_pin) {
2510 int ret = create_hp_mic_jack_mode(codec, pin);
2511 if (ret < 0)
2512 return ret;
2513 continue;
2514 }
2515 if (get_out_jack_num_items(codec, pin) > 1) {
Takashi Iwai978e77e2013-01-10 16:57:58 +01002516 struct snd_kcontrol_new *knew;
Takashi Iwai975cc022013-06-28 11:56:49 +02002517 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
Takashi Iwai978e77e2013-01-10 16:57:58 +01002518 get_jack_mode_name(codec, pin, name, sizeof(name));
2519 knew = snd_hda_gen_add_kctl(spec, name,
2520 &out_jack_mode_enum);
2521 if (!knew)
2522 return -ENOMEM;
2523 knew->private_value = pin;
2524 }
2525 }
2526
2527 return 0;
2528}
2529
Takashi Iwai294765582013-01-17 09:52:11 +01002530/*
2531 * input jack mode
2532 */
2533
2534/* from AC_PINCTL_VREF_HIZ to AC_PINCTL_VREF_100 */
2535#define NUM_VREFS 6
2536
2537static const char * const vref_texts[NUM_VREFS] = {
2538 "Line In", "Mic 50pc Bias", "Mic 0V Bias",
2539 "", "Mic 80pc Bias", "Mic 100pc Bias"
2540};
2541
2542static unsigned int get_vref_caps(struct hda_codec *codec, hda_nid_t pin)
2543{
2544 unsigned int pincap;
2545
2546 pincap = snd_hda_query_pin_caps(codec, pin);
2547 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
2548 /* filter out unusual vrefs */
2549 pincap &= ~(AC_PINCAP_VREF_GRD | AC_PINCAP_VREF_100);
2550 return pincap;
2551}
2552
2553/* convert from the enum item index to the vref ctl index (0=HIZ, 1=50%...) */
2554static int get_vref_idx(unsigned int vref_caps, unsigned int item_idx)
2555{
2556 unsigned int i, n = 0;
2557
2558 for (i = 0; i < NUM_VREFS; i++) {
2559 if (vref_caps & (1 << i)) {
2560 if (n == item_idx)
2561 return i;
2562 n++;
2563 }
2564 }
2565 return 0;
2566}
2567
2568/* convert back from the vref ctl index to the enum item index */
2569static int cvt_from_vref_idx(unsigned int vref_caps, unsigned int idx)
2570{
2571 unsigned int i, n = 0;
2572
2573 for (i = 0; i < NUM_VREFS; i++) {
2574 if (i == idx)
2575 return n;
2576 if (vref_caps & (1 << i))
2577 n++;
2578 }
2579 return 0;
2580}
2581
2582static int in_jack_mode_info(struct snd_kcontrol *kcontrol,
2583 struct snd_ctl_elem_info *uinfo)
2584{
2585 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2586 hda_nid_t nid = kcontrol->private_value;
2587 unsigned int vref_caps = get_vref_caps(codec, nid);
2588
2589 snd_hda_enum_helper_info(kcontrol, uinfo, hweight32(vref_caps),
2590 vref_texts);
2591 /* set the right text */
2592 strcpy(uinfo->value.enumerated.name,
2593 vref_texts[get_vref_idx(vref_caps, uinfo->value.enumerated.item)]);
2594 return 0;
2595}
2596
2597static int in_jack_mode_get(struct snd_kcontrol *kcontrol,
2598 struct snd_ctl_elem_value *ucontrol)
2599{
2600 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2601 hda_nid_t nid = kcontrol->private_value;
2602 unsigned int vref_caps = get_vref_caps(codec, nid);
2603 unsigned int idx;
2604
2605 idx = snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_VREFEN;
2606 ucontrol->value.enumerated.item[0] = cvt_from_vref_idx(vref_caps, idx);
2607 return 0;
2608}
2609
2610static int in_jack_mode_put(struct snd_kcontrol *kcontrol,
2611 struct snd_ctl_elem_value *ucontrol)
2612{
2613 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2614 hda_nid_t nid = kcontrol->private_value;
2615 unsigned int vref_caps = get_vref_caps(codec, nid);
2616 unsigned int val, idx;
2617
2618 val = snd_hda_codec_get_pin_target(codec, nid);
2619 idx = cvt_from_vref_idx(vref_caps, val & AC_PINCTL_VREFEN);
2620 if (idx == ucontrol->value.enumerated.item[0])
2621 return 0;
2622
2623 val &= ~AC_PINCTL_VREFEN;
2624 val |= get_vref_idx(vref_caps, ucontrol->value.enumerated.item[0]);
2625 snd_hda_set_pin_ctl_cache(codec, nid, val);
2626 return 1;
2627}
2628
2629static const struct snd_kcontrol_new in_jack_mode_enum = {
2630 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2631 .info = in_jack_mode_info,
2632 .get = in_jack_mode_get,
2633 .put = in_jack_mode_put,
2634};
2635
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002636static int get_in_jack_num_items(struct hda_codec *codec, hda_nid_t pin)
2637{
2638 struct hda_gen_spec *spec = codec->spec;
2639 int nitems = 0;
Takashi Iwaif811c3c2013-03-07 18:32:59 +01002640 if (spec->add_jack_modes)
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002641 nitems = hweight32(get_vref_caps(codec, pin));
2642 return nitems ? nitems : 1;
2643}
2644
Takashi Iwai294765582013-01-17 09:52:11 +01002645static int create_in_jack_mode(struct hda_codec *codec, hda_nid_t pin)
2646{
2647 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai294765582013-01-17 09:52:11 +01002648 struct snd_kcontrol_new *knew;
Takashi Iwai975cc022013-06-28 11:56:49 +02002649 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002650 unsigned int defcfg;
2651
Takashi Iwaif811c3c2013-03-07 18:32:59 +01002652 if (pin == spec->hp_mic_pin)
2653 return 0; /* already done in create_out_jack_mode() */
Takashi Iwai294765582013-01-17 09:52:11 +01002654
2655 /* no jack mode for fixed pins */
2656 defcfg = snd_hda_codec_get_pincfg(codec, pin);
2657 if (snd_hda_get_input_pin_attr(defcfg) == INPUT_PIN_ATTR_INT)
2658 return 0;
2659
2660 /* no multiple vref caps? */
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002661 if (get_in_jack_num_items(codec, pin) <= 1)
Takashi Iwai294765582013-01-17 09:52:11 +01002662 return 0;
2663
2664 get_jack_mode_name(codec, pin, name, sizeof(name));
2665 knew = snd_hda_gen_add_kctl(spec, name, &in_jack_mode_enum);
2666 if (!knew)
2667 return -ENOMEM;
2668 knew->private_value = pin;
2669 return 0;
2670}
2671
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002672/*
2673 * HP/mic shared jack mode
2674 */
2675static int hp_mic_jack_mode_info(struct snd_kcontrol *kcontrol,
2676 struct snd_ctl_elem_info *uinfo)
2677{
2678 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2679 hda_nid_t nid = kcontrol->private_value;
2680 int out_jacks = get_out_jack_num_items(codec, nid);
2681 int in_jacks = get_in_jack_num_items(codec, nid);
2682 const char *text = NULL;
2683 int idx;
2684
2685 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2686 uinfo->count = 1;
2687 uinfo->value.enumerated.items = out_jacks + in_jacks;
2688 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2689 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
2690 idx = uinfo->value.enumerated.item;
2691 if (idx < out_jacks) {
2692 if (out_jacks > 1)
2693 text = out_jack_texts[idx];
2694 else
2695 text = "Headphone Out";
2696 } else {
2697 idx -= out_jacks;
2698 if (in_jacks > 1) {
2699 unsigned int vref_caps = get_vref_caps(codec, nid);
2700 text = vref_texts[get_vref_idx(vref_caps, idx)];
2701 } else
2702 text = "Mic In";
2703 }
2704
2705 strcpy(uinfo->value.enumerated.name, text);
2706 return 0;
2707}
2708
2709static int get_cur_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t nid)
2710{
2711 int out_jacks = get_out_jack_num_items(codec, nid);
2712 int in_jacks = get_in_jack_num_items(codec, nid);
2713 unsigned int val = snd_hda_codec_get_pin_target(codec, nid);
2714 int idx = 0;
2715
2716 if (val & PIN_OUT) {
2717 if (out_jacks > 1 && val == PIN_HP)
2718 idx = 1;
2719 } else if (val & PIN_IN) {
2720 idx = out_jacks;
2721 if (in_jacks > 1) {
2722 unsigned int vref_caps = get_vref_caps(codec, nid);
2723 val &= AC_PINCTL_VREFEN;
2724 idx += cvt_from_vref_idx(vref_caps, val);
2725 }
2726 }
2727 return idx;
2728}
2729
2730static int hp_mic_jack_mode_get(struct snd_kcontrol *kcontrol,
2731 struct snd_ctl_elem_value *ucontrol)
2732{
2733 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2734 hda_nid_t nid = kcontrol->private_value;
2735 ucontrol->value.enumerated.item[0] =
2736 get_cur_hp_mic_jack_mode(codec, nid);
2737 return 0;
2738}
2739
2740static int hp_mic_jack_mode_put(struct snd_kcontrol *kcontrol,
2741 struct snd_ctl_elem_value *ucontrol)
2742{
2743 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2744 hda_nid_t nid = kcontrol->private_value;
2745 int out_jacks = get_out_jack_num_items(codec, nid);
2746 int in_jacks = get_in_jack_num_items(codec, nid);
2747 unsigned int val, oldval, idx;
2748
2749 oldval = get_cur_hp_mic_jack_mode(codec, nid);
2750 idx = ucontrol->value.enumerated.item[0];
2751 if (oldval == idx)
2752 return 0;
2753
2754 if (idx < out_jacks) {
2755 if (out_jacks > 1)
2756 val = idx ? PIN_HP : PIN_OUT;
2757 else
2758 val = PIN_HP;
2759 } else {
2760 idx -= out_jacks;
2761 if (in_jacks > 1) {
2762 unsigned int vref_caps = get_vref_caps(codec, nid);
2763 val = snd_hda_codec_get_pin_target(codec, nid);
Takashi Iwai3f550e32013-03-07 18:30:27 +01002764 val &= ~(AC_PINCTL_VREFEN | PIN_HP);
2765 val |= get_vref_idx(vref_caps, idx) | PIN_IN;
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002766 } else
2767 val = snd_hda_get_default_vref(codec, nid);
2768 }
2769 snd_hda_set_pin_ctl_cache(codec, nid, val);
Takashi Iwai963afde2013-05-31 15:20:31 +02002770 call_hp_automute(codec, NULL);
Takashi Iwai8ba955c2013-03-07 18:40:58 +01002771
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002772 return 1;
2773}
2774
2775static const struct snd_kcontrol_new hp_mic_jack_mode_enum = {
2776 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2777 .info = hp_mic_jack_mode_info,
2778 .get = hp_mic_jack_mode_get,
2779 .put = hp_mic_jack_mode_put,
2780};
2781
2782static int create_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t pin)
2783{
2784 struct hda_gen_spec *spec = codec->spec;
2785 struct snd_kcontrol_new *knew;
2786
2787 if (get_out_jack_num_items(codec, pin) <= 1 &&
2788 get_in_jack_num_items(codec, pin) <= 1)
2789 return 0; /* no need */
2790 knew = snd_hda_gen_add_kctl(spec, "Headphone Mic Jack Mode",
2791 &hp_mic_jack_mode_enum);
2792 if (!knew)
2793 return -ENOMEM;
2794 knew->private_value = pin;
Takashi Iwai8ba955c2013-03-07 18:40:58 +01002795 spec->hp_mic_jack_modes = 1;
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002796 return 0;
2797}
Takashi Iwai352f7f92012-12-19 12:52:06 +01002798
2799/*
2800 * Parse input paths
2801 */
2802
Takashi Iwai352f7f92012-12-19 12:52:06 +01002803/* add the powersave loopback-list entry */
Takashi Iwai0186f4f2013-02-07 10:45:11 +01002804static int add_loopback_list(struct hda_gen_spec *spec, hda_nid_t mix, int idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002805{
2806 struct hda_amp_list *list;
2807
Takashi Iwai0186f4f2013-02-07 10:45:11 +01002808 list = snd_array_new(&spec->loopback_list);
2809 if (!list)
2810 return -ENOMEM;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002811 list->nid = mix;
2812 list->dir = HDA_INPUT;
2813 list->idx = idx;
Takashi Iwai0186f4f2013-02-07 10:45:11 +01002814 spec->loopback.amplist = spec->loopback_list.list;
2815 return 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02002816}
Takashi Iwaicb53c622007-08-10 17:21:45 +02002817
Takashi Iwai352f7f92012-12-19 12:52:06 +01002818/* create input playback/capture controls for the given pin */
Takashi Iwai196c17662013-01-04 15:01:40 +01002819static int new_analog_input(struct hda_codec *codec, int input_idx,
2820 hda_nid_t pin, const char *ctlname, int ctlidx,
Takashi Iwai352f7f92012-12-19 12:52:06 +01002821 hda_nid_t mix_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002823 struct hda_gen_spec *spec = codec->spec;
2824 struct nid_path *path;
2825 unsigned int val;
2826 int err, idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827
Takashi Iwai352f7f92012-12-19 12:52:06 +01002828 if (!nid_has_volume(codec, mix_nid, HDA_INPUT) &&
2829 !nid_has_mute(codec, mix_nid, HDA_INPUT))
2830 return 0; /* no need for analog loopback */
2831
Takashi Iwai3ca529d2013-01-07 17:25:08 +01002832 path = snd_hda_add_new_path(codec, pin, mix_nid, 0);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002833 if (!path)
2834 return -EINVAL;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01002835 print_nid_path("loopback", path);
Takashi Iwai196c17662013-01-04 15:01:40 +01002836 spec->loopback_paths[input_idx] = snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002837
2838 idx = path->idx[path->depth - 1];
2839 if (nid_has_volume(codec, mix_nid, HDA_INPUT)) {
2840 val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
2841 err = __add_pb_vol_ctrl(spec, HDA_CTL_WIDGET_VOL, ctlname, ctlidx, val);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002842 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002844 path->ctls[NID_PATH_VOL_CTL] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845 }
2846
Takashi Iwai352f7f92012-12-19 12:52:06 +01002847 if (nid_has_mute(codec, mix_nid, HDA_INPUT)) {
2848 val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
2849 err = __add_pb_sw_ctrl(spec, HDA_CTL_WIDGET_MUTE, ctlname, ctlidx, val);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002850 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002852 path->ctls[NID_PATH_MUTE_CTL] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853 }
2854
Takashi Iwai352f7f92012-12-19 12:52:06 +01002855 path->active = true;
Takashi Iwai0186f4f2013-02-07 10:45:11 +01002856 err = add_loopback_list(spec, mix_nid, idx);
2857 if (err < 0)
2858 return err;
Takashi Iwaie4a395e2013-01-23 17:00:31 +01002859
2860 if (spec->mixer_nid != spec->mixer_merge_nid &&
2861 !spec->loopback_merge_path) {
2862 path = snd_hda_add_new_path(codec, spec->mixer_nid,
2863 spec->mixer_merge_nid, 0);
2864 if (path) {
2865 print_nid_path("loopback-merge", path);
2866 path->active = true;
2867 spec->loopback_merge_path =
2868 snd_hda_get_path_idx(codec, path);
2869 }
2870 }
2871
Takashi Iwai352f7f92012-12-19 12:52:06 +01002872 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873}
2874
Takashi Iwai352f7f92012-12-19 12:52:06 +01002875static int is_input_pin(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002877 unsigned int pincap = snd_hda_query_pin_caps(codec, nid);
2878 return (pincap & AC_PINCAP_IN) != 0;
2879}
2880
2881/* Parse the codec tree and retrieve ADCs */
2882static int fill_adc_nids(struct hda_codec *codec)
2883{
2884 struct hda_gen_spec *spec = codec->spec;
2885 hda_nid_t nid;
2886 hda_nid_t *adc_nids = spec->adc_nids;
2887 int max_nums = ARRAY_SIZE(spec->adc_nids);
2888 int i, nums = 0;
2889
2890 nid = codec->start_nid;
2891 for (i = 0; i < codec->num_nodes; i++, nid++) {
2892 unsigned int caps = get_wcaps(codec, nid);
2893 int type = get_wcaps_type(caps);
2894
2895 if (type != AC_WID_AUD_IN || (caps & AC_WCAP_DIGITAL))
2896 continue;
2897 adc_nids[nums] = nid;
2898 if (++nums >= max_nums)
2899 break;
2900 }
2901 spec->num_adc_nids = nums;
Takashi Iwai0ffd5342013-01-17 15:53:29 +01002902
2903 /* copy the detected ADCs to all_adcs[] */
2904 spec->num_all_adcs = nums;
2905 memcpy(spec->all_adcs, spec->adc_nids, nums * sizeof(hda_nid_t));
2906
Takashi Iwai352f7f92012-12-19 12:52:06 +01002907 return nums;
2908}
2909
2910/* filter out invalid adc_nids that don't give all active input pins;
2911 * if needed, check whether dynamic ADC-switching is available
2912 */
2913static int check_dyn_adc_switch(struct hda_codec *codec)
2914{
2915 struct hda_gen_spec *spec = codec->spec;
2916 struct hda_input_mux *imux = &spec->input_mux;
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002917 unsigned int ok_bits;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002918 int i, n, nums;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002919
Takashi Iwai352f7f92012-12-19 12:52:06 +01002920 nums = 0;
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002921 ok_bits = 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002922 for (n = 0; n < spec->num_adc_nids; n++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002923 for (i = 0; i < imux->num_items; i++) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002924 if (!spec->input_paths[i][n])
Takashi Iwai352f7f92012-12-19 12:52:06 +01002925 break;
2926 }
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002927 if (i >= imux->num_items) {
2928 ok_bits |= (1 << n);
2929 nums++;
2930 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002931 }
2932
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002933 if (!ok_bits) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002934 /* check whether ADC-switch is possible */
2935 for (i = 0; i < imux->num_items; i++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002936 for (n = 0; n < spec->num_adc_nids; n++) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002937 if (spec->input_paths[i][n]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002938 spec->dyn_adc_idx[i] = n;
2939 break;
2940 }
2941 }
2942 }
2943
2944 snd_printdd("hda-codec: enabling ADC switching\n");
2945 spec->dyn_adc_switch = 1;
2946 } else if (nums != spec->num_adc_nids) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002947 /* shrink the invalid adcs and input paths */
2948 nums = 0;
2949 for (n = 0; n < spec->num_adc_nids; n++) {
2950 if (!(ok_bits & (1 << n)))
2951 continue;
2952 if (n != nums) {
2953 spec->adc_nids[nums] = spec->adc_nids[n];
Takashi Iwai980428c2013-01-09 09:28:20 +01002954 for (i = 0; i < imux->num_items; i++) {
2955 invalidate_nid_path(codec,
2956 spec->input_paths[i][nums]);
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002957 spec->input_paths[i][nums] =
2958 spec->input_paths[i][n];
Takashi Iwai980428c2013-01-09 09:28:20 +01002959 }
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002960 }
2961 nums++;
2962 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002963 spec->num_adc_nids = nums;
2964 }
2965
Takashi Iwai967303d2013-02-19 17:12:42 +01002966 if (imux->num_items == 1 ||
2967 (imux->num_items == 2 && spec->hp_mic)) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002968 snd_printdd("hda-codec: reducing to a single ADC\n");
2969 spec->num_adc_nids = 1; /* reduce to a single ADC */
2970 }
2971
2972 /* single index for individual volumes ctls */
2973 if (!spec->dyn_adc_switch && spec->multi_cap_vol)
2974 spec->num_adc_nids = 1;
2975
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976 return 0;
2977}
2978
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002979/* parse capture source paths from the given pin and create imux items */
2980static int parse_capture_source(struct hda_codec *codec, hda_nid_t pin,
Takashi Iwai9dba2052013-01-18 10:01:15 +01002981 int cfg_idx, int num_adcs,
2982 const char *label, int anchor)
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002983{
2984 struct hda_gen_spec *spec = codec->spec;
2985 struct hda_input_mux *imux = &spec->input_mux;
2986 int imux_idx = imux->num_items;
2987 bool imux_added = false;
2988 int c;
2989
2990 for (c = 0; c < num_adcs; c++) {
2991 struct nid_path *path;
2992 hda_nid_t adc = spec->adc_nids[c];
2993
2994 if (!is_reachable_path(codec, pin, adc))
2995 continue;
2996 path = snd_hda_add_new_path(codec, pin, adc, anchor);
2997 if (!path)
2998 continue;
2999 print_nid_path("input", path);
3000 spec->input_paths[imux_idx][c] =
3001 snd_hda_get_path_idx(codec, path);
3002
3003 if (!imux_added) {
Takashi Iwai967303d2013-02-19 17:12:42 +01003004 if (spec->hp_mic_pin == pin)
3005 spec->hp_mic_mux_idx = imux->num_items;
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003006 spec->imux_pins[imux->num_items] = pin;
Takashi Iwai9dba2052013-01-18 10:01:15 +01003007 snd_hda_add_imux_item(imux, label, cfg_idx, NULL);
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003008 imux_added = true;
3009 }
3010 }
3011
3012 return 0;
3013}
3014
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01003016 * create playback/capture controls for input pins
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017 */
Takashi Iwai9dba2052013-01-18 10:01:15 +01003018
Takashi Iwaic9700422013-01-18 10:17:30 +01003019/* fill the label for each input at first */
3020static int fill_input_pin_labels(struct hda_codec *codec)
3021{
3022 struct hda_gen_spec *spec = codec->spec;
3023 const struct auto_pin_cfg *cfg = &spec->autocfg;
3024 int i;
3025
3026 for (i = 0; i < cfg->num_inputs; i++) {
3027 hda_nid_t pin = cfg->inputs[i].pin;
3028 const char *label;
3029 int j, idx;
3030
3031 if (!is_input_pin(codec, pin))
3032 continue;
3033
3034 label = hda_get_autocfg_input_label(codec, cfg, i);
3035 idx = 0;
David Henningsson8e8db7f2013-01-18 15:43:02 +01003036 for (j = i - 1; j >= 0; j--) {
Takashi Iwaic9700422013-01-18 10:17:30 +01003037 if (spec->input_labels[j] &&
3038 !strcmp(spec->input_labels[j], label)) {
3039 idx = spec->input_label_idxs[j] + 1;
3040 break;
3041 }
3042 }
3043
3044 spec->input_labels[i] = label;
3045 spec->input_label_idxs[i] = idx;
3046 }
3047
3048 return 0;
3049}
3050
Takashi Iwai9dba2052013-01-18 10:01:15 +01003051#define CFG_IDX_MIX 99 /* a dummy cfg->input idx for stereo mix */
3052
Takashi Iwai352f7f92012-12-19 12:52:06 +01003053static int create_input_ctls(struct hda_codec *codec)
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02003054{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003055 struct hda_gen_spec *spec = codec->spec;
3056 const struct auto_pin_cfg *cfg = &spec->autocfg;
3057 hda_nid_t mixer = spec->mixer_nid;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003058 int num_adcs;
Takashi Iwaic9700422013-01-18 10:17:30 +01003059 int i, err;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003060 unsigned int val;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02003061
Takashi Iwai352f7f92012-12-19 12:52:06 +01003062 num_adcs = fill_adc_nids(codec);
3063 if (num_adcs < 0)
3064 return 0;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02003065
Takashi Iwaic9700422013-01-18 10:17:30 +01003066 err = fill_input_pin_labels(codec);
3067 if (err < 0)
3068 return err;
3069
Takashi Iwai352f7f92012-12-19 12:52:06 +01003070 for (i = 0; i < cfg->num_inputs; i++) {
3071 hda_nid_t pin;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072
Takashi Iwai352f7f92012-12-19 12:52:06 +01003073 pin = cfg->inputs[i].pin;
3074 if (!is_input_pin(codec, pin))
3075 continue;
3076
Takashi Iwai2c12c302013-01-10 09:33:29 +01003077 val = PIN_IN;
3078 if (cfg->inputs[i].type == AUTO_PIN_MIC)
3079 val |= snd_hda_get_default_vref(codec, pin);
Takashi Iwai93c9d8a2013-03-11 09:48:43 +01003080 if (pin != spec->hp_mic_pin)
3081 set_pin_target(codec, pin, val, false);
Takashi Iwai2c12c302013-01-10 09:33:29 +01003082
Takashi Iwai352f7f92012-12-19 12:52:06 +01003083 if (mixer) {
3084 if (is_reachable_path(codec, pin, mixer)) {
Takashi Iwai196c17662013-01-04 15:01:40 +01003085 err = new_analog_input(codec, i, pin,
Takashi Iwaic9700422013-01-18 10:17:30 +01003086 spec->input_labels[i],
3087 spec->input_label_idxs[i],
3088 mixer);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003089 if (err < 0)
3090 return err;
3091 }
3092 }
3093
Takashi Iwaic9700422013-01-18 10:17:30 +01003094 err = parse_capture_source(codec, pin, i, num_adcs,
3095 spec->input_labels[i], -mixer);
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003096 if (err < 0)
3097 return err;
Takashi Iwai294765582013-01-17 09:52:11 +01003098
Takashi Iwaif811c3c2013-03-07 18:32:59 +01003099 if (spec->add_jack_modes) {
Takashi Iwai294765582013-01-17 09:52:11 +01003100 err = create_in_jack_mode(codec, pin);
3101 if (err < 0)
3102 return err;
3103 }
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003104 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003105
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003106 if (mixer && spec->add_stereo_mix_input) {
Takashi Iwai9dba2052013-01-18 10:01:15 +01003107 err = parse_capture_source(codec, mixer, CFG_IDX_MIX, num_adcs,
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003108 "Stereo Mix", 0);
3109 if (err < 0)
3110 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003111 }
3112
3113 return 0;
3114}
3115
3116
3117/*
3118 * input source mux
3119 */
3120
Takashi Iwaic697b712013-01-07 17:09:26 +01003121/* get the input path specified by the given adc and imux indices */
3122static struct nid_path *get_input_path(struct hda_codec *codec, int adc_idx, int imux_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003123{
3124 struct hda_gen_spec *spec = codec->spec;
David Henningssonb56fa1e2013-01-16 11:45:35 +01003125 if (imux_idx < 0 || imux_idx >= HDA_MAX_NUM_INPUTS) {
3126 snd_BUG();
3127 return NULL;
3128 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003129 if (spec->dyn_adc_switch)
3130 adc_idx = spec->dyn_adc_idx[imux_idx];
David Henningssond3d982f2013-01-18 15:43:01 +01003131 if (adc_idx < 0 || adc_idx >= AUTO_CFG_MAX_INS) {
David Henningssonb56fa1e2013-01-16 11:45:35 +01003132 snd_BUG();
3133 return NULL;
3134 }
Takashi Iwaic697b712013-01-07 17:09:26 +01003135 return snd_hda_get_path_from_idx(codec, spec->input_paths[imux_idx][adc_idx]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003136}
3137
3138static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
3139 unsigned int idx);
3140
3141static int mux_enum_info(struct snd_kcontrol *kcontrol,
3142 struct snd_ctl_elem_info *uinfo)
3143{
3144 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3145 struct hda_gen_spec *spec = codec->spec;
3146 return snd_hda_input_mux_info(&spec->input_mux, uinfo);
3147}
3148
3149static int mux_enum_get(struct snd_kcontrol *kcontrol,
3150 struct snd_ctl_elem_value *ucontrol)
3151{
3152 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3153 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai2a8d5392013-01-18 16:23:25 +01003154 /* the ctls are created at once with multiple counts */
3155 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003156
3157 ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
3158 return 0;
3159}
3160
3161static int mux_enum_put(struct snd_kcontrol *kcontrol,
3162 struct snd_ctl_elem_value *ucontrol)
3163{
3164 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Takashi Iwai2a8d5392013-01-18 16:23:25 +01003165 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003166 return mux_select(codec, adc_idx,
3167 ucontrol->value.enumerated.item[0]);
3168}
3169
Takashi Iwai352f7f92012-12-19 12:52:06 +01003170static const struct snd_kcontrol_new cap_src_temp = {
3171 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3172 .name = "Input Source",
3173 .info = mux_enum_info,
3174 .get = mux_enum_get,
3175 .put = mux_enum_put,
3176};
3177
Takashi Iwai47d46ab2012-12-20 11:48:54 +01003178/*
3179 * capture volume and capture switch ctls
3180 */
3181
Takashi Iwai352f7f92012-12-19 12:52:06 +01003182typedef int (*put_call_t)(struct snd_kcontrol *kcontrol,
3183 struct snd_ctl_elem_value *ucontrol);
3184
Takashi Iwai47d46ab2012-12-20 11:48:54 +01003185/* call the given amp update function for all amps in the imux list at once */
Takashi Iwai352f7f92012-12-19 12:52:06 +01003186static int cap_put_caller(struct snd_kcontrol *kcontrol,
3187 struct snd_ctl_elem_value *ucontrol,
3188 put_call_t func, int type)
3189{
3190 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3191 struct hda_gen_spec *spec = codec->spec;
3192 const struct hda_input_mux *imux;
3193 struct nid_path *path;
3194 int i, adc_idx, err = 0;
3195
3196 imux = &spec->input_mux;
David Henningssona053d1e2013-01-16 11:45:36 +01003197 adc_idx = kcontrol->id.index;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003198 mutex_lock(&codec->control_mutex);
Takashi Iwai47d46ab2012-12-20 11:48:54 +01003199 /* we use the cache-only update at first since multiple input paths
3200 * may shared the same amp; by updating only caches, the redundant
3201 * writes to hardware can be reduced.
3202 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01003203 codec->cached_write = 1;
3204 for (i = 0; i < imux->num_items; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01003205 path = get_input_path(codec, adc_idx, i);
3206 if (!path || !path->ctls[type])
Takashi Iwai352f7f92012-12-19 12:52:06 +01003207 continue;
3208 kcontrol->private_value = path->ctls[type];
3209 err = func(kcontrol, ucontrol);
3210 if (err < 0)
3211 goto error;
3212 }
3213 error:
3214 codec->cached_write = 0;
3215 mutex_unlock(&codec->control_mutex);
Takashi Iwaidc870f32013-01-22 15:24:30 +01003216 snd_hda_codec_flush_cache(codec); /* flush the updates */
Takashi Iwai352f7f92012-12-19 12:52:06 +01003217 if (err >= 0 && spec->cap_sync_hook)
Takashi Iwaia90229e2013-01-18 14:10:00 +01003218 spec->cap_sync_hook(codec, ucontrol);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003219 return err;
3220}
3221
3222/* capture volume ctl callbacks */
3223#define cap_vol_info snd_hda_mixer_amp_volume_info
3224#define cap_vol_get snd_hda_mixer_amp_volume_get
3225#define cap_vol_tlv snd_hda_mixer_amp_tlv
3226
3227static int cap_vol_put(struct snd_kcontrol *kcontrol,
3228 struct snd_ctl_elem_value *ucontrol)
3229{
3230 return cap_put_caller(kcontrol, ucontrol,
3231 snd_hda_mixer_amp_volume_put,
3232 NID_PATH_VOL_CTL);
3233}
3234
3235static const struct snd_kcontrol_new cap_vol_temp = {
3236 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3237 .name = "Capture Volume",
3238 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
3239 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
3240 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK),
3241 .info = cap_vol_info,
3242 .get = cap_vol_get,
3243 .put = cap_vol_put,
3244 .tlv = { .c = cap_vol_tlv },
3245};
3246
3247/* capture switch ctl callbacks */
3248#define cap_sw_info snd_ctl_boolean_stereo_info
3249#define cap_sw_get snd_hda_mixer_amp_switch_get
3250
3251static int cap_sw_put(struct snd_kcontrol *kcontrol,
3252 struct snd_ctl_elem_value *ucontrol)
3253{
Takashi Iwaia90229e2013-01-18 14:10:00 +01003254 return cap_put_caller(kcontrol, ucontrol,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003255 snd_hda_mixer_amp_switch_put,
3256 NID_PATH_MUTE_CTL);
3257}
3258
3259static const struct snd_kcontrol_new cap_sw_temp = {
3260 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3261 .name = "Capture Switch",
3262 .info = cap_sw_info,
3263 .get = cap_sw_get,
3264 .put = cap_sw_put,
3265};
3266
3267static int parse_capvol_in_path(struct hda_codec *codec, struct nid_path *path)
3268{
3269 hda_nid_t nid;
3270 int i, depth;
3271
3272 path->ctls[NID_PATH_VOL_CTL] = path->ctls[NID_PATH_MUTE_CTL] = 0;
3273 for (depth = 0; depth < 3; depth++) {
3274 if (depth >= path->depth)
3275 return -EINVAL;
3276 i = path->depth - depth - 1;
3277 nid = path->path[i];
3278 if (!path->ctls[NID_PATH_VOL_CTL]) {
3279 if (nid_has_volume(codec, nid, HDA_OUTPUT))
3280 path->ctls[NID_PATH_VOL_CTL] =
3281 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3282 else if (nid_has_volume(codec, nid, HDA_INPUT)) {
3283 int idx = path->idx[i];
3284 if (!depth && codec->single_adc_amp)
3285 idx = 0;
3286 path->ctls[NID_PATH_VOL_CTL] =
3287 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
3288 }
3289 }
3290 if (!path->ctls[NID_PATH_MUTE_CTL]) {
3291 if (nid_has_mute(codec, nid, HDA_OUTPUT))
3292 path->ctls[NID_PATH_MUTE_CTL] =
3293 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3294 else if (nid_has_mute(codec, nid, HDA_INPUT)) {
3295 int idx = path->idx[i];
3296 if (!depth && codec->single_adc_amp)
3297 idx = 0;
3298 path->ctls[NID_PATH_MUTE_CTL] =
3299 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
3300 }
3301 }
Takashi Iwai97ec5582006-03-21 11:29:07 +01003302 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003303 return 0;
3304}
3305
Takashi Iwai352f7f92012-12-19 12:52:06 +01003306static bool is_inv_dmic_pin(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003307{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003308 struct hda_gen_spec *spec = codec->spec;
3309 struct auto_pin_cfg *cfg = &spec->autocfg;
3310 unsigned int val;
3311 int i;
3312
3313 if (!spec->inv_dmic_split)
3314 return false;
3315 for (i = 0; i < cfg->num_inputs; i++) {
3316 if (cfg->inputs[i].pin != nid)
3317 continue;
3318 if (cfg->inputs[i].type != AUTO_PIN_MIC)
3319 return false;
3320 val = snd_hda_codec_get_pincfg(codec, nid);
3321 return snd_hda_get_input_pin_attr(val) == INPUT_PIN_ATTR_INT;
3322 }
3323 return false;
3324}
3325
Takashi Iwaia90229e2013-01-18 14:10:00 +01003326/* capture switch put callback for a single control with hook call */
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003327static int cap_single_sw_put(struct snd_kcontrol *kcontrol,
3328 struct snd_ctl_elem_value *ucontrol)
3329{
3330 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3331 struct hda_gen_spec *spec = codec->spec;
3332 int ret;
3333
3334 ret = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
3335 if (ret < 0)
3336 return ret;
3337
Takashi Iwaia90229e2013-01-18 14:10:00 +01003338 if (spec->cap_sync_hook)
3339 spec->cap_sync_hook(codec, ucontrol);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003340
3341 return ret;
3342}
3343
Takashi Iwai352f7f92012-12-19 12:52:06 +01003344static int add_single_cap_ctl(struct hda_codec *codec, const char *label,
3345 int idx, bool is_switch, unsigned int ctl,
3346 bool inv_dmic)
3347{
3348 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai975cc022013-06-28 11:56:49 +02003349 char tmpname[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
Takashi Iwai352f7f92012-12-19 12:52:06 +01003350 int type = is_switch ? HDA_CTL_WIDGET_MUTE : HDA_CTL_WIDGET_VOL;
3351 const char *sfx = is_switch ? "Switch" : "Volume";
3352 unsigned int chs = inv_dmic ? 1 : 3;
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003353 struct snd_kcontrol_new *knew;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003354
3355 if (!ctl)
3356 return 0;
3357
3358 if (label)
3359 snprintf(tmpname, sizeof(tmpname),
3360 "%s Capture %s", label, sfx);
3361 else
3362 snprintf(tmpname, sizeof(tmpname),
3363 "Capture %s", sfx);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003364 knew = add_control(spec, type, tmpname, idx,
3365 amp_val_replace_channels(ctl, chs));
3366 if (!knew)
3367 return -ENOMEM;
Takashi Iwaia90229e2013-01-18 14:10:00 +01003368 if (is_switch)
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003369 knew->put = cap_single_sw_put;
3370 if (!inv_dmic)
3371 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003372
3373 /* Make independent right kcontrol */
3374 if (label)
3375 snprintf(tmpname, sizeof(tmpname),
3376 "Inverted %s Capture %s", label, sfx);
3377 else
3378 snprintf(tmpname, sizeof(tmpname),
3379 "Inverted Capture %s", sfx);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003380 knew = add_control(spec, type, tmpname, idx,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003381 amp_val_replace_channels(ctl, 2));
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003382 if (!knew)
3383 return -ENOMEM;
Takashi Iwaia90229e2013-01-18 14:10:00 +01003384 if (is_switch)
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003385 knew->put = cap_single_sw_put;
3386 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003387}
3388
3389/* create single (and simple) capture volume and switch controls */
3390static int create_single_cap_vol_ctl(struct hda_codec *codec, int idx,
3391 unsigned int vol_ctl, unsigned int sw_ctl,
3392 bool inv_dmic)
3393{
3394 int err;
3395 err = add_single_cap_ctl(codec, NULL, idx, false, vol_ctl, inv_dmic);
3396 if (err < 0)
3397 return err;
3398 err = add_single_cap_ctl(codec, NULL, idx, true, sw_ctl, inv_dmic);
3399 if (err < 0)
3400 return err;
3401 return 0;
3402}
3403
3404/* create bound capture volume and switch controls */
3405static int create_bind_cap_vol_ctl(struct hda_codec *codec, int idx,
3406 unsigned int vol_ctl, unsigned int sw_ctl)
3407{
3408 struct hda_gen_spec *spec = codec->spec;
3409 struct snd_kcontrol_new *knew;
3410
3411 if (vol_ctl) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01003412 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_vol_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003413 if (!knew)
3414 return -ENOMEM;
3415 knew->index = idx;
3416 knew->private_value = vol_ctl;
3417 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
3418 }
3419 if (sw_ctl) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01003420 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_sw_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003421 if (!knew)
3422 return -ENOMEM;
3423 knew->index = idx;
3424 knew->private_value = sw_ctl;
3425 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
3426 }
3427 return 0;
3428}
3429
3430/* return the vol ctl when used first in the imux list */
3431static unsigned int get_first_cap_ctl(struct hda_codec *codec, int idx, int type)
3432{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003433 struct nid_path *path;
3434 unsigned int ctl;
3435 int i;
3436
Takashi Iwaic697b712013-01-07 17:09:26 +01003437 path = get_input_path(codec, 0, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003438 if (!path)
3439 return 0;
3440 ctl = path->ctls[type];
3441 if (!ctl)
3442 return 0;
3443 for (i = 0; i < idx - 1; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01003444 path = get_input_path(codec, 0, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003445 if (path && path->ctls[type] == ctl)
3446 return 0;
3447 }
3448 return ctl;
3449}
3450
3451/* create individual capture volume and switch controls per input */
3452static int create_multi_cap_vol_ctl(struct hda_codec *codec)
3453{
3454 struct hda_gen_spec *spec = codec->spec;
3455 struct hda_input_mux *imux = &spec->input_mux;
Takashi Iwaic9700422013-01-18 10:17:30 +01003456 int i, err, type;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003457
3458 for (i = 0; i < imux->num_items; i++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003459 bool inv_dmic;
Takashi Iwaic9700422013-01-18 10:17:30 +01003460 int idx;
Takashi Iwai9dba2052013-01-18 10:01:15 +01003461
Takashi Iwaic9700422013-01-18 10:17:30 +01003462 idx = imux->items[i].index;
3463 if (idx >= spec->autocfg.num_inputs)
Takashi Iwai9dba2052013-01-18 10:01:15 +01003464 continue;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003465 inv_dmic = is_inv_dmic_pin(codec, spec->imux_pins[i]);
3466
3467 for (type = 0; type < 2; type++) {
Takashi Iwaic9700422013-01-18 10:17:30 +01003468 err = add_single_cap_ctl(codec,
3469 spec->input_labels[idx],
3470 spec->input_label_idxs[idx],
3471 type,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003472 get_first_cap_ctl(codec, i, type),
3473 inv_dmic);
3474 if (err < 0)
3475 return err;
3476 }
3477 }
3478 return 0;
3479}
3480
3481static int create_capture_mixers(struct hda_codec *codec)
3482{
3483 struct hda_gen_spec *spec = codec->spec;
3484 struct hda_input_mux *imux = &spec->input_mux;
3485 int i, n, nums, err;
3486
3487 if (spec->dyn_adc_switch)
3488 nums = 1;
3489 else
3490 nums = spec->num_adc_nids;
3491
3492 if (!spec->auto_mic && imux->num_items > 1) {
3493 struct snd_kcontrol_new *knew;
Takashi Iwai624d9142012-12-19 17:41:52 +01003494 const char *name;
3495 name = nums > 1 ? "Input Source" : "Capture Source";
3496 knew = snd_hda_gen_add_kctl(spec, name, &cap_src_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003497 if (!knew)
3498 return -ENOMEM;
3499 knew->count = nums;
3500 }
3501
3502 for (n = 0; n < nums; n++) {
3503 bool multi = false;
David Henningsson99a55922013-01-16 15:58:44 +01003504 bool multi_cap_vol = spec->multi_cap_vol;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003505 bool inv_dmic = false;
3506 int vol, sw;
3507
3508 vol = sw = 0;
3509 for (i = 0; i < imux->num_items; i++) {
3510 struct nid_path *path;
Takashi Iwaic697b712013-01-07 17:09:26 +01003511 path = get_input_path(codec, n, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003512 if (!path)
3513 continue;
3514 parse_capvol_in_path(codec, path);
3515 if (!vol)
3516 vol = path->ctls[NID_PATH_VOL_CTL];
David Henningsson99a55922013-01-16 15:58:44 +01003517 else if (vol != path->ctls[NID_PATH_VOL_CTL]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003518 multi = true;
David Henningsson99a55922013-01-16 15:58:44 +01003519 if (!same_amp_caps(codec, vol,
3520 path->ctls[NID_PATH_VOL_CTL], HDA_INPUT))
3521 multi_cap_vol = true;
3522 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003523 if (!sw)
3524 sw = path->ctls[NID_PATH_MUTE_CTL];
David Henningsson99a55922013-01-16 15:58:44 +01003525 else if (sw != path->ctls[NID_PATH_MUTE_CTL]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003526 multi = true;
David Henningsson99a55922013-01-16 15:58:44 +01003527 if (!same_amp_caps(codec, sw,
3528 path->ctls[NID_PATH_MUTE_CTL], HDA_INPUT))
3529 multi_cap_vol = true;
3530 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003531 if (is_inv_dmic_pin(codec, spec->imux_pins[i]))
3532 inv_dmic = true;
3533 }
3534
3535 if (!multi)
3536 err = create_single_cap_vol_ctl(codec, n, vol, sw,
3537 inv_dmic);
David Henningssonccb04152013-10-14 10:16:22 +02003538 else if (!multi_cap_vol && !inv_dmic)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003539 err = create_bind_cap_vol_ctl(codec, n, vol, sw);
3540 else
3541 err = create_multi_cap_vol_ctl(codec);
3542 if (err < 0)
3543 return err;
3544 }
3545
3546 return 0;
3547}
3548
3549/*
3550 * add mic boosts if needed
3551 */
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003552
3553/* check whether the given amp is feasible as a boost volume */
3554static bool check_boost_vol(struct hda_codec *codec, hda_nid_t nid,
3555 int dir, int idx)
3556{
3557 unsigned int step;
3558
3559 if (!nid_has_volume(codec, nid, dir) ||
3560 is_ctl_associated(codec, nid, dir, idx, NID_PATH_VOL_CTL) ||
3561 is_ctl_associated(codec, nid, dir, idx, NID_PATH_BOOST_CTL))
3562 return false;
3563
3564 step = (query_amp_caps(codec, nid, dir) & AC_AMPCAP_STEP_SIZE)
3565 >> AC_AMPCAP_STEP_SIZE_SHIFT;
3566 if (step < 0x20)
3567 return false;
3568 return true;
3569}
3570
3571/* look for a boost amp in a widget close to the pin */
3572static unsigned int look_for_boost_amp(struct hda_codec *codec,
3573 struct nid_path *path)
3574{
3575 unsigned int val = 0;
3576 hda_nid_t nid;
3577 int depth;
3578
3579 for (depth = 0; depth < 3; depth++) {
3580 if (depth >= path->depth - 1)
3581 break;
3582 nid = path->path[depth];
3583 if (depth && check_boost_vol(codec, nid, HDA_OUTPUT, 0)) {
3584 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3585 break;
3586 } else if (check_boost_vol(codec, nid, HDA_INPUT,
3587 path->idx[depth])) {
3588 val = HDA_COMPOSE_AMP_VAL(nid, 3, path->idx[depth],
3589 HDA_INPUT);
3590 break;
3591 }
3592 }
3593
3594 return val;
3595}
3596
Takashi Iwai352f7f92012-12-19 12:52:06 +01003597static int parse_mic_boost(struct hda_codec *codec)
3598{
3599 struct hda_gen_spec *spec = codec->spec;
3600 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003601 struct hda_input_mux *imux = &spec->input_mux;
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003602 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003603
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003604 if (!spec->num_adc_nids)
3605 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003606
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003607 for (i = 0; i < imux->num_items; i++) {
3608 struct nid_path *path;
3609 unsigned int val;
3610 int idx;
Takashi Iwai975cc022013-06-28 11:56:49 +02003611 char boost_label[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
David Henningsson02aba552013-01-16 15:58:43 +01003612
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003613 idx = imux->items[i].index;
3614 if (idx >= imux->num_items)
3615 continue;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003616
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003617 /* check only line-in and mic pins */
Takashi Iwai1799cdd52013-01-18 14:37:16 +01003618 if (cfg->inputs[idx].type > AUTO_PIN_LINE_IN)
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003619 continue;
3620
3621 path = get_input_path(codec, 0, i);
3622 if (!path)
3623 continue;
3624
3625 val = look_for_boost_amp(codec, path);
3626 if (!val)
3627 continue;
3628
3629 /* create a boost control */
3630 snprintf(boost_label, sizeof(boost_label),
3631 "%s Boost Volume", spec->input_labels[idx]);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003632 if (!add_control(spec, HDA_CTL_WIDGET_VOL, boost_label,
3633 spec->input_label_idxs[idx], val))
3634 return -ENOMEM;
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003635
3636 path->ctls[NID_PATH_BOOST_CTL] = val;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003637 }
3638 return 0;
3639}
3640
3641/*
3642 * parse digital I/Os and set up NIDs in BIOS auto-parse mode
3643 */
3644static void parse_digital(struct hda_codec *codec)
3645{
3646 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003647 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003648 int i, nums;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003649 hda_nid_t dig_nid, pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003650
3651 /* support multiple SPDIFs; the secondary is set up as a slave */
3652 nums = 0;
3653 for (i = 0; i < spec->autocfg.dig_outs; i++) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01003654 pin = spec->autocfg.dig_out_pins[i];
Takashi Iwai352f7f92012-12-19 12:52:06 +01003655 dig_nid = look_for_dac(codec, pin, true);
3656 if (!dig_nid)
3657 continue;
Takashi Iwai3ca529d2013-01-07 17:25:08 +01003658 path = snd_hda_add_new_path(codec, dig_nid, pin, 0);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003659 if (!path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003660 continue;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003661 print_nid_path("digout", path);
Takashi Iwaie1284af2013-01-03 16:33:02 +01003662 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +01003663 spec->digout_paths[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai2c12c302013-01-10 09:33:29 +01003664 set_pin_target(codec, pin, PIN_OUT, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003665 if (!nums) {
3666 spec->multiout.dig_out_nid = dig_nid;
3667 spec->dig_out_type = spec->autocfg.dig_out_type[0];
3668 } else {
3669 spec->multiout.slave_dig_outs = spec->slave_dig_outs;
3670 if (nums >= ARRAY_SIZE(spec->slave_dig_outs) - 1)
3671 break;
3672 spec->slave_dig_outs[nums - 1] = dig_nid;
3673 }
3674 nums++;
3675 }
3676
3677 if (spec->autocfg.dig_in_pin) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01003678 pin = spec->autocfg.dig_in_pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003679 dig_nid = codec->start_nid;
3680 for (i = 0; i < codec->num_nodes; i++, dig_nid++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003681 unsigned int wcaps = get_wcaps(codec, dig_nid);
3682 if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
3683 continue;
3684 if (!(wcaps & AC_WCAP_DIGITAL))
3685 continue;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003686 path = snd_hda_add_new_path(codec, pin, dig_nid, 0);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003687 if (path) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003688 print_nid_path("digin", path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003689 path->active = true;
3690 spec->dig_in_nid = dig_nid;
Takashi Iwai2430d7b2013-01-04 15:09:42 +01003691 spec->digin_path = snd_hda_get_path_idx(codec, path);
Takashi Iwai2c12c302013-01-10 09:33:29 +01003692 set_pin_target(codec, pin, PIN_IN, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003693 break;
3694 }
3695 }
3696 }
3697}
3698
3699
3700/*
3701 * input MUX handling
3702 */
3703
3704static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur);
3705
3706/* select the given imux item; either unmute exclusively or select the route */
3707static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
3708 unsigned int idx)
3709{
3710 struct hda_gen_spec *spec = codec->spec;
3711 const struct hda_input_mux *imux;
Takashi Iwai55196ff2013-01-24 17:32:56 +01003712 struct nid_path *old_path, *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003713
3714 imux = &spec->input_mux;
3715 if (!imux->num_items)
3716 return 0;
3717
3718 if (idx >= imux->num_items)
3719 idx = imux->num_items - 1;
3720 if (spec->cur_mux[adc_idx] == idx)
3721 return 0;
3722
Takashi Iwai55196ff2013-01-24 17:32:56 +01003723 old_path = get_input_path(codec, adc_idx, spec->cur_mux[adc_idx]);
3724 if (!old_path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003725 return 0;
Takashi Iwai55196ff2013-01-24 17:32:56 +01003726 if (old_path->active)
3727 snd_hda_activate_path(codec, old_path, false, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003728
3729 spec->cur_mux[adc_idx] = idx;
3730
Takashi Iwai967303d2013-02-19 17:12:42 +01003731 if (spec->hp_mic)
3732 update_hp_mic(codec, adc_idx, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003733
3734 if (spec->dyn_adc_switch)
3735 dyn_adc_pcm_resetup(codec, idx);
3736
Takashi Iwaic697b712013-01-07 17:09:26 +01003737 path = get_input_path(codec, adc_idx, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003738 if (!path)
3739 return 0;
3740 if (path->active)
3741 return 0;
3742 snd_hda_activate_path(codec, path, true, false);
3743 if (spec->cap_sync_hook)
Takashi Iwaia90229e2013-01-18 14:10:00 +01003744 spec->cap_sync_hook(codec, NULL);
Takashi Iwai55196ff2013-01-24 17:32:56 +01003745 path_power_down_sync(codec, old_path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003746 return 1;
3747}
3748
3749
3750/*
3751 * Jack detections for HP auto-mute and mic-switch
3752 */
3753
3754/* check each pin in the given array; returns true if any of them is plugged */
3755static bool detect_jacks(struct hda_codec *codec, int num_pins, hda_nid_t *pins)
3756{
Takashi Iwai60ea8ca2013-07-19 16:59:46 +02003757 int i;
3758 bool present = false;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003759
3760 for (i = 0; i < num_pins; i++) {
3761 hda_nid_t nid = pins[i];
3762 if (!nid)
3763 break;
Takashi Iwai0b4df932013-01-10 09:45:13 +01003764 /* don't detect pins retasked as inputs */
3765 if (snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_IN_EN)
3766 continue;
Takashi Iwai60ea8ca2013-07-19 16:59:46 +02003767 if (snd_hda_jack_detect_state(codec, nid) == HDA_JACK_PRESENT)
3768 present = true;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003769 }
3770 return present;
3771}
3772
3773/* standard HP/line-out auto-mute helper */
3774static void do_automute(struct hda_codec *codec, int num_pins, hda_nid_t *pins,
Takashi Iwaie80c60f2013-08-12 14:44:59 +02003775 int *paths, bool mute)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003776{
3777 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003778 int i;
3779
3780 for (i = 0; i < num_pins; i++) {
3781 hda_nid_t nid = pins[i];
Takashi Iwai967303d2013-02-19 17:12:42 +01003782 unsigned int val, oldval;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003783 if (!nid)
3784 break;
Takashi Iwai7eebffd2013-06-24 16:00:21 +02003785
3786 if (spec->auto_mute_via_amp) {
Takashi Iwaie80c60f2013-08-12 14:44:59 +02003787 struct nid_path *path;
3788 hda_nid_t mute_nid;
3789
3790 path = snd_hda_get_path_from_idx(codec, paths[i]);
3791 if (!path)
3792 continue;
3793 mute_nid = get_amp_nid_(path->ctls[NID_PATH_MUTE_CTL]);
3794 if (!mute_nid)
3795 continue;
Takashi Iwai7eebffd2013-06-24 16:00:21 +02003796 if (mute)
Takashi Iwaie80c60f2013-08-12 14:44:59 +02003797 spec->mute_bits |= (1ULL << mute_nid);
Takashi Iwai7eebffd2013-06-24 16:00:21 +02003798 else
Takashi Iwaie80c60f2013-08-12 14:44:59 +02003799 spec->mute_bits &= ~(1ULL << mute_nid);
Takashi Iwai7eebffd2013-06-24 16:00:21 +02003800 set_pin_eapd(codec, nid, !mute);
3801 continue;
3802 }
3803
Takashi Iwai967303d2013-02-19 17:12:42 +01003804 oldval = snd_hda_codec_get_pin_target(codec, nid);
3805 if (oldval & PIN_IN)
3806 continue; /* no mute for inputs */
Takashi Iwai352f7f92012-12-19 12:52:06 +01003807 /* don't reset VREF value in case it's controlling
3808 * the amp (see alc861_fixup_asus_amp_vref_0f())
3809 */
Takashi Iwai2c12c302013-01-10 09:33:29 +01003810 if (spec->keep_vref_in_automute)
Takashi Iwai967303d2013-02-19 17:12:42 +01003811 val = oldval & ~PIN_HP;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003812 else
Takashi Iwai352f7f92012-12-19 12:52:06 +01003813 val = 0;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003814 if (!mute)
Takashi Iwai967303d2013-02-19 17:12:42 +01003815 val |= oldval;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003816 /* here we call update_pin_ctl() so that the pinctl is changed
3817 * without changing the pinctl target value;
3818 * the original target value will be still referred at the
3819 * init / resume again
3820 */
3821 update_pin_ctl(codec, nid, val);
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01003822 set_pin_eapd(codec, nid, !mute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003823 }
3824}
3825
3826/* Toggle outputs muting */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003827void snd_hda_gen_update_outputs(struct hda_codec *codec)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003828{
3829 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaie80c60f2013-08-12 14:44:59 +02003830 int *paths;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003831 int on;
3832
3833 /* Control HP pins/amps depending on master_mute state;
3834 * in general, HP pins/amps control should be enabled in all cases,
3835 * but currently set only for master_mute, just to be safe
3836 */
Takashi Iwaie80c60f2013-08-12 14:44:59 +02003837 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
3838 paths = spec->out_paths;
3839 else
3840 paths = spec->hp_paths;
Takashi Iwai967303d2013-02-19 17:12:42 +01003841 do_automute(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
Takashi Iwaie80c60f2013-08-12 14:44:59 +02003842 spec->autocfg.hp_pins, paths, spec->master_mute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003843
3844 if (!spec->automute_speaker)
3845 on = 0;
3846 else
3847 on = spec->hp_jack_present | spec->line_jack_present;
3848 on |= spec->master_mute;
Takashi Iwai47b9ddb82013-01-16 18:18:00 +01003849 spec->speaker_muted = on;
Takashi Iwaie80c60f2013-08-12 14:44:59 +02003850 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
3851 paths = spec->out_paths;
3852 else
3853 paths = spec->speaker_paths;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003854 do_automute(codec, ARRAY_SIZE(spec->autocfg.speaker_pins),
Takashi Iwaie80c60f2013-08-12 14:44:59 +02003855 spec->autocfg.speaker_pins, paths, on);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003856
3857 /* toggle line-out mutes if needed, too */
3858 /* if LO is a copy of either HP or Speaker, don't need to handle it */
3859 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0] ||
3860 spec->autocfg.line_out_pins[0] == spec->autocfg.speaker_pins[0])
3861 return;
3862 if (!spec->automute_lo)
3863 on = 0;
3864 else
3865 on = spec->hp_jack_present;
3866 on |= spec->master_mute;
Takashi Iwai47b9ddb82013-01-16 18:18:00 +01003867 spec->line_out_muted = on;
Takashi Iwaie80c60f2013-08-12 14:44:59 +02003868 paths = spec->out_paths;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003869 do_automute(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
Takashi Iwaie80c60f2013-08-12 14:44:59 +02003870 spec->autocfg.line_out_pins, paths, on);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003871}
Takashi Iwai5d550e12012-12-19 15:16:44 +01003872EXPORT_SYMBOL_HDA(snd_hda_gen_update_outputs);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003873
3874static void call_update_outputs(struct hda_codec *codec)
3875{
3876 struct hda_gen_spec *spec = codec->spec;
3877 if (spec->automute_hook)
3878 spec->automute_hook(codec);
3879 else
Takashi Iwai5d550e12012-12-19 15:16:44 +01003880 snd_hda_gen_update_outputs(codec);
Takashi Iwai7eebffd2013-06-24 16:00:21 +02003881
3882 /* sync the whole vmaster slaves to reflect the new auto-mute status */
3883 if (spec->auto_mute_via_amp && !codec->bus->shutdown)
3884 snd_ctl_sync_vmaster(spec->vmaster_mute.sw_kctl, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003885}
3886
3887/* standard HP-automute helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003888void snd_hda_gen_hp_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003889{
3890 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai92603c52013-01-22 07:46:31 +01003891 hda_nid_t *pins = spec->autocfg.hp_pins;
3892 int num_pins = ARRAY_SIZE(spec->autocfg.hp_pins);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003893
Takashi Iwai92603c52013-01-22 07:46:31 +01003894 /* No detection for the first HP jack during indep-HP mode */
3895 if (spec->indep_hp_enabled) {
3896 pins++;
3897 num_pins--;
3898 }
3899
3900 spec->hp_jack_present = detect_jacks(codec, num_pins, pins);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003901 if (!spec->detect_hp || (!spec->automute_speaker && !spec->automute_lo))
3902 return;
3903 call_update_outputs(codec);
3904}
Takashi Iwai5d550e12012-12-19 15:16:44 +01003905EXPORT_SYMBOL_HDA(snd_hda_gen_hp_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003906
3907/* standard line-out-automute helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003908void snd_hda_gen_line_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003909{
3910 struct hda_gen_spec *spec = codec->spec;
3911
3912 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
3913 return;
3914 /* check LO jack only when it's different from HP */
3915 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0])
3916 return;
3917
3918 spec->line_jack_present =
3919 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
3920 spec->autocfg.line_out_pins);
3921 if (!spec->automute_speaker || !spec->detect_lo)
3922 return;
3923 call_update_outputs(codec);
3924}
Takashi Iwai5d550e12012-12-19 15:16:44 +01003925EXPORT_SYMBOL_HDA(snd_hda_gen_line_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003926
3927/* standard mic auto-switch helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003928void snd_hda_gen_mic_autoswitch(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003929{
3930 struct hda_gen_spec *spec = codec->spec;
3931 int i;
3932
3933 if (!spec->auto_mic)
3934 return;
3935
3936 for (i = spec->am_num_entries - 1; i > 0; i--) {
Takashi Iwai0b4df932013-01-10 09:45:13 +01003937 hda_nid_t pin = spec->am_entry[i].pin;
3938 /* don't detect pins retasked as outputs */
3939 if (snd_hda_codec_get_pin_target(codec, pin) & AC_PINCTL_OUT_EN)
3940 continue;
Takashi Iwai60ea8ca2013-07-19 16:59:46 +02003941 if (snd_hda_jack_detect_state(codec, pin) == HDA_JACK_PRESENT) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003942 mux_select(codec, 0, spec->am_entry[i].idx);
3943 return;
3944 }
3945 }
3946 mux_select(codec, 0, spec->am_entry[0].idx);
3947}
Takashi Iwai5d550e12012-12-19 15:16:44 +01003948EXPORT_SYMBOL_HDA(snd_hda_gen_mic_autoswitch);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003949
Takashi Iwai77afe0e2013-05-31 14:10:03 +02003950/* call appropriate hooks */
3951static void call_hp_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
3952{
3953 struct hda_gen_spec *spec = codec->spec;
3954 if (spec->hp_automute_hook)
3955 spec->hp_automute_hook(codec, jack);
3956 else
3957 snd_hda_gen_hp_automute(codec, jack);
3958}
3959
3960static void call_line_automute(struct hda_codec *codec,
3961 struct hda_jack_tbl *jack)
3962{
3963 struct hda_gen_spec *spec = codec->spec;
3964 if (spec->line_automute_hook)
3965 spec->line_automute_hook(codec, jack);
3966 else
3967 snd_hda_gen_line_automute(codec, jack);
3968}
3969
3970static void call_mic_autoswitch(struct hda_codec *codec,
3971 struct hda_jack_tbl *jack)
3972{
3973 struct hda_gen_spec *spec = codec->spec;
3974 if (spec->mic_autoswitch_hook)
3975 spec->mic_autoswitch_hook(codec, jack);
3976 else
3977 snd_hda_gen_mic_autoswitch(codec, jack);
3978}
3979
Takashi Iwai963afde2013-05-31 15:20:31 +02003980/* update jack retasking */
3981static void update_automute_all(struct hda_codec *codec)
3982{
3983 call_hp_automute(codec, NULL);
3984 call_line_automute(codec, NULL);
3985 call_mic_autoswitch(codec, NULL);
3986}
3987
Takashi Iwai352f7f92012-12-19 12:52:06 +01003988/*
3989 * Auto-Mute mode mixer enum support
3990 */
3991static int automute_mode_info(struct snd_kcontrol *kcontrol,
3992 struct snd_ctl_elem_info *uinfo)
3993{
3994 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3995 struct hda_gen_spec *spec = codec->spec;
3996 static const char * const texts3[] = {
3997 "Disabled", "Speaker Only", "Line Out+Speaker"
Takashi Iwai071c73a2006-08-23 18:34:06 +02003998 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003999
Takashi Iwai352f7f92012-12-19 12:52:06 +01004000 if (spec->automute_speaker_possible && spec->automute_lo_possible)
4001 return snd_hda_enum_helper_info(kcontrol, uinfo, 3, texts3);
4002 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
4003}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004004
Takashi Iwai352f7f92012-12-19 12:52:06 +01004005static int automute_mode_get(struct snd_kcontrol *kcontrol,
4006 struct snd_ctl_elem_value *ucontrol)
4007{
4008 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4009 struct hda_gen_spec *spec = codec->spec;
4010 unsigned int val = 0;
4011 if (spec->automute_speaker)
4012 val++;
4013 if (spec->automute_lo)
4014 val++;
Takashi Iwai071c73a2006-08-23 18:34:06 +02004015
Takashi Iwai352f7f92012-12-19 12:52:06 +01004016 ucontrol->value.enumerated.item[0] = val;
4017 return 0;
4018}
4019
4020static int automute_mode_put(struct snd_kcontrol *kcontrol,
4021 struct snd_ctl_elem_value *ucontrol)
4022{
4023 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4024 struct hda_gen_spec *spec = codec->spec;
4025
4026 switch (ucontrol->value.enumerated.item[0]) {
4027 case 0:
4028 if (!spec->automute_speaker && !spec->automute_lo)
4029 return 0;
4030 spec->automute_speaker = 0;
4031 spec->automute_lo = 0;
4032 break;
4033 case 1:
4034 if (spec->automute_speaker_possible) {
4035 if (!spec->automute_lo && spec->automute_speaker)
4036 return 0;
4037 spec->automute_speaker = 1;
4038 spec->automute_lo = 0;
4039 } else if (spec->automute_lo_possible) {
4040 if (spec->automute_lo)
4041 return 0;
4042 spec->automute_lo = 1;
4043 } else
4044 return -EINVAL;
4045 break;
4046 case 2:
4047 if (!spec->automute_lo_possible || !spec->automute_speaker_possible)
4048 return -EINVAL;
4049 if (spec->automute_speaker && spec->automute_lo)
4050 return 0;
4051 spec->automute_speaker = 1;
4052 spec->automute_lo = 1;
4053 break;
4054 default:
4055 return -EINVAL;
4056 }
4057 call_update_outputs(codec);
4058 return 1;
4059}
4060
4061static const struct snd_kcontrol_new automute_mode_enum = {
4062 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
4063 .name = "Auto-Mute Mode",
4064 .info = automute_mode_info,
4065 .get = automute_mode_get,
4066 .put = automute_mode_put,
4067};
4068
4069static int add_automute_mode_enum(struct hda_codec *codec)
4070{
4071 struct hda_gen_spec *spec = codec->spec;
4072
Takashi Iwai12c93df2012-12-19 14:38:33 +01004073 if (!snd_hda_gen_add_kctl(spec, NULL, &automute_mode_enum))
Takashi Iwai352f7f92012-12-19 12:52:06 +01004074 return -ENOMEM;
4075 return 0;
4076}
4077
4078/*
4079 * Check the availability of HP/line-out auto-mute;
4080 * Set up appropriately if really supported
4081 */
4082static int check_auto_mute_availability(struct hda_codec *codec)
4083{
4084 struct hda_gen_spec *spec = codec->spec;
4085 struct auto_pin_cfg *cfg = &spec->autocfg;
4086 int present = 0;
4087 int i, err;
4088
Takashi Iwaif72706b2013-01-16 18:20:07 +01004089 if (spec->suppress_auto_mute)
4090 return 0;
4091
Takashi Iwai352f7f92012-12-19 12:52:06 +01004092 if (cfg->hp_pins[0])
4093 present++;
4094 if (cfg->line_out_pins[0])
4095 present++;
4096 if (cfg->speaker_pins[0])
4097 present++;
4098 if (present < 2) /* need two different output types */
Takashi Iwai071c73a2006-08-23 18:34:06 +02004099 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004100
4101 if (!cfg->speaker_pins[0] &&
4102 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
4103 memcpy(cfg->speaker_pins, cfg->line_out_pins,
4104 sizeof(cfg->speaker_pins));
4105 cfg->speaker_outs = cfg->line_outs;
Takashi Iwai071c73a2006-08-23 18:34:06 +02004106 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004107
Takashi Iwai352f7f92012-12-19 12:52:06 +01004108 if (!cfg->hp_pins[0] &&
4109 cfg->line_out_type == AUTO_PIN_HP_OUT) {
4110 memcpy(cfg->hp_pins, cfg->line_out_pins,
4111 sizeof(cfg->hp_pins));
4112 cfg->hp_outs = cfg->line_outs;
4113 }
4114
4115 for (i = 0; i < cfg->hp_outs; i++) {
4116 hda_nid_t nid = cfg->hp_pins[i];
4117 if (!is_jack_detectable(codec, nid))
4118 continue;
4119 snd_printdd("hda-codec: Enable HP auto-muting on NID 0x%x\n",
4120 nid);
4121 snd_hda_jack_detect_enable_callback(codec, nid, HDA_GEN_HP_EVENT,
Takashi Iwai77afe0e2013-05-31 14:10:03 +02004122 call_hp_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004123 spec->detect_hp = 1;
4124 }
4125
4126 if (cfg->line_out_type == AUTO_PIN_LINE_OUT && cfg->line_outs) {
4127 if (cfg->speaker_outs)
4128 for (i = 0; i < cfg->line_outs; i++) {
4129 hda_nid_t nid = cfg->line_out_pins[i];
4130 if (!is_jack_detectable(codec, nid))
4131 continue;
4132 snd_printdd("hda-codec: Enable Line-Out auto-muting on NID 0x%x\n", nid);
4133 snd_hda_jack_detect_enable_callback(codec, nid,
4134 HDA_GEN_FRONT_EVENT,
Takashi Iwai77afe0e2013-05-31 14:10:03 +02004135 call_line_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004136 spec->detect_lo = 1;
4137 }
4138 spec->automute_lo_possible = spec->detect_hp;
4139 }
4140
4141 spec->automute_speaker_possible = cfg->speaker_outs &&
4142 (spec->detect_hp || spec->detect_lo);
4143
4144 spec->automute_lo = spec->automute_lo_possible;
4145 spec->automute_speaker = spec->automute_speaker_possible;
4146
4147 if (spec->automute_speaker_possible || spec->automute_lo_possible) {
4148 /* create a control for automute mode */
4149 err = add_automute_mode_enum(codec);
4150 if (err < 0)
4151 return err;
4152 }
4153 return 0;
4154}
4155
Takashi Iwai352f7f92012-12-19 12:52:06 +01004156/* check whether all auto-mic pins are valid; setup indices if OK */
4157static bool auto_mic_check_imux(struct hda_codec *codec)
4158{
4159 struct hda_gen_spec *spec = codec->spec;
4160 const struct hda_input_mux *imux;
4161 int i;
4162
4163 imux = &spec->input_mux;
4164 for (i = 0; i < spec->am_num_entries; i++) {
4165 spec->am_entry[i].idx =
4166 find_idx_in_nid_list(spec->am_entry[i].pin,
4167 spec->imux_pins, imux->num_items);
4168 if (spec->am_entry[i].idx < 0)
4169 return false; /* no corresponding imux */
4170 }
4171
4172 /* we don't need the jack detection for the first pin */
4173 for (i = 1; i < spec->am_num_entries; i++)
4174 snd_hda_jack_detect_enable_callback(codec,
4175 spec->am_entry[i].pin,
4176 HDA_GEN_MIC_EVENT,
Takashi Iwai77afe0e2013-05-31 14:10:03 +02004177 call_mic_autoswitch);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004178 return true;
4179}
4180
4181static int compare_attr(const void *ap, const void *bp)
4182{
4183 const struct automic_entry *a = ap;
4184 const struct automic_entry *b = bp;
4185 return (int)(a->attr - b->attr);
4186}
4187
4188/*
4189 * Check the availability of auto-mic switch;
4190 * Set up if really supported
4191 */
4192static int check_auto_mic_availability(struct hda_codec *codec)
4193{
4194 struct hda_gen_spec *spec = codec->spec;
4195 struct auto_pin_cfg *cfg = &spec->autocfg;
4196 unsigned int types;
4197 int i, num_pins;
4198
Takashi Iwaid12daf62013-01-07 16:32:11 +01004199 if (spec->suppress_auto_mic)
4200 return 0;
4201
Takashi Iwai352f7f92012-12-19 12:52:06 +01004202 types = 0;
4203 num_pins = 0;
4204 for (i = 0; i < cfg->num_inputs; i++) {
4205 hda_nid_t nid = cfg->inputs[i].pin;
4206 unsigned int attr;
4207 attr = snd_hda_codec_get_pincfg(codec, nid);
4208 attr = snd_hda_get_input_pin_attr(attr);
4209 if (types & (1 << attr))
4210 return 0; /* already occupied */
4211 switch (attr) {
4212 case INPUT_PIN_ATTR_INT:
4213 if (cfg->inputs[i].type != AUTO_PIN_MIC)
4214 return 0; /* invalid type */
4215 break;
4216 case INPUT_PIN_ATTR_UNUSED:
4217 return 0; /* invalid entry */
4218 default:
4219 if (cfg->inputs[i].type > AUTO_PIN_LINE_IN)
4220 return 0; /* invalid type */
4221 if (!spec->line_in_auto_switch &&
4222 cfg->inputs[i].type != AUTO_PIN_MIC)
4223 return 0; /* only mic is allowed */
4224 if (!is_jack_detectable(codec, nid))
4225 return 0; /* no unsol support */
4226 break;
4227 }
4228 if (num_pins >= MAX_AUTO_MIC_PINS)
4229 return 0;
4230 types |= (1 << attr);
4231 spec->am_entry[num_pins].pin = nid;
4232 spec->am_entry[num_pins].attr = attr;
4233 num_pins++;
4234 }
4235
4236 if (num_pins < 2)
4237 return 0;
4238
4239 spec->am_num_entries = num_pins;
4240 /* sort the am_entry in the order of attr so that the pin with a
4241 * higher attr will be selected when the jack is plugged.
4242 */
4243 sort(spec->am_entry, num_pins, sizeof(spec->am_entry[0]),
4244 compare_attr, NULL);
4245
4246 if (!auto_mic_check_imux(codec))
4247 return 0;
4248
4249 spec->auto_mic = 1;
4250 spec->num_adc_nids = 1;
4251 spec->cur_mux[0] = spec->am_entry[0].idx;
4252 snd_printdd("hda-codec: Enable auto-mic switch on NID 0x%x/0x%x/0x%x\n",
4253 spec->am_entry[0].pin,
4254 spec->am_entry[1].pin,
4255 spec->am_entry[2].pin);
4256
4257 return 0;
4258}
4259
Takashi Iwai55196ff2013-01-24 17:32:56 +01004260/* power_filter hook; make inactive widgets into power down */
4261static unsigned int snd_hda_gen_path_power_filter(struct hda_codec *codec,
4262 hda_nid_t nid,
4263 unsigned int power_state)
4264{
4265 if (power_state != AC_PWRST_D0)
4266 return power_state;
4267 if (get_wcaps_type(get_wcaps(codec, nid)) >= AC_WID_POWER)
4268 return power_state;
Takashi Iwaib1b9fbd2013-05-14 12:58:47 +02004269 if (is_active_nid_for_any(codec, nid))
Takashi Iwai55196ff2013-01-24 17:32:56 +01004270 return power_state;
4271 return AC_PWRST_D3;
4272}
4273
Takashi Iwai352f7f92012-12-19 12:52:06 +01004274
Takashi Iwai9eb413e2012-12-19 14:41:21 +01004275/*
4276 * Parse the given BIOS configuration and set up the hda_gen_spec
4277 *
4278 * return 1 if successful, 0 if the proper config is not found,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004279 * or a negative error code
4280 */
4281int snd_hda_gen_parse_auto_config(struct hda_codec *codec,
Takashi Iwai9eb413e2012-12-19 14:41:21 +01004282 struct auto_pin_cfg *cfg)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004283{
4284 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004285 int err;
4286
Takashi Iwai1c70a582013-01-11 17:48:22 +01004287 parse_user_hints(codec);
4288
Takashi Iwaie4a395e2013-01-23 17:00:31 +01004289 if (spec->mixer_nid && !spec->mixer_merge_nid)
4290 spec->mixer_merge_nid = spec->mixer_nid;
4291
Takashi Iwai9eb413e2012-12-19 14:41:21 +01004292 if (cfg != &spec->autocfg) {
4293 spec->autocfg = *cfg;
4294 cfg = &spec->autocfg;
4295 }
4296
Takashi Iwai98bd1112013-03-22 14:53:50 +01004297 if (!spec->main_out_badness)
4298 spec->main_out_badness = &hda_main_out_badness;
4299 if (!spec->extra_out_badness)
4300 spec->extra_out_badness = &hda_extra_out_badness;
4301
David Henningsson6fc4cb92013-01-16 15:58:45 +01004302 fill_all_dac_nids(codec);
4303
Takashi Iwai352f7f92012-12-19 12:52:06 +01004304 if (!cfg->line_outs) {
4305 if (cfg->dig_outs || cfg->dig_in_pin) {
4306 spec->multiout.max_channels = 2;
4307 spec->no_analog = 1;
4308 goto dig_only;
4309 }
4310 return 0; /* can't find valid BIOS pin config */
4311 }
4312
4313 if (!spec->no_primary_hp &&
4314 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT &&
4315 cfg->line_outs <= cfg->hp_outs) {
4316 /* use HP as primary out */
4317 cfg->speaker_outs = cfg->line_outs;
4318 memcpy(cfg->speaker_pins, cfg->line_out_pins,
4319 sizeof(cfg->speaker_pins));
4320 cfg->line_outs = cfg->hp_outs;
4321 memcpy(cfg->line_out_pins, cfg->hp_pins, sizeof(cfg->hp_pins));
4322 cfg->hp_outs = 0;
4323 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
4324 cfg->line_out_type = AUTO_PIN_HP_OUT;
4325 }
4326
4327 err = parse_output_paths(codec);
4328 if (err < 0)
4329 return err;
4330 err = create_multi_channel_mode(codec);
4331 if (err < 0)
4332 return err;
4333 err = create_multi_out_ctls(codec, cfg);
4334 if (err < 0)
4335 return err;
4336 err = create_hp_out_ctls(codec);
4337 if (err < 0)
4338 return err;
4339 err = create_speaker_out_ctls(codec);
4340 if (err < 0)
4341 return err;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004342 err = create_indep_hp_ctls(codec);
4343 if (err < 0)
4344 return err;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01004345 err = create_loopback_mixing_ctl(codec);
4346 if (err < 0)
4347 return err;
Takashi Iwai967303d2013-02-19 17:12:42 +01004348 err = create_hp_mic(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004349 if (err < 0)
4350 return err;
4351 err = create_input_ctls(codec);
Takashi Iwaid13bd412008-07-30 15:01:45 +02004352 if (err < 0)
Takashi Iwai071c73a2006-08-23 18:34:06 +02004353 return err;
4354
Takashi Iwaia07a9492013-01-07 16:44:06 +01004355 spec->const_channel_count = spec->ext_channel_count;
4356 /* check the multiple speaker and headphone pins */
4357 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
4358 spec->const_channel_count = max(spec->const_channel_count,
4359 cfg->speaker_outs * 2);
4360 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
4361 spec->const_channel_count = max(spec->const_channel_count,
4362 cfg->hp_outs * 2);
4363 spec->multiout.max_channels = max(spec->ext_channel_count,
4364 spec->const_channel_count);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004365
4366 err = check_auto_mute_availability(codec);
4367 if (err < 0)
4368 return err;
4369
4370 err = check_dyn_adc_switch(codec);
4371 if (err < 0)
4372 return err;
4373
Takashi Iwai967303d2013-02-19 17:12:42 +01004374 err = check_auto_mic_availability(codec);
4375 if (err < 0)
4376 return err;
Takashi Iwai071c73a2006-08-23 18:34:06 +02004377
Takashi Iwai352f7f92012-12-19 12:52:06 +01004378 err = create_capture_mixers(codec);
4379 if (err < 0)
4380 return err;
4381
4382 err = parse_mic_boost(codec);
4383 if (err < 0)
4384 return err;
4385
Takashi Iwaif811c3c2013-03-07 18:32:59 +01004386 if (spec->add_jack_modes) {
Takashi Iwai978e77e2013-01-10 16:57:58 +01004387 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
4388 err = create_out_jack_modes(codec, cfg->line_outs,
4389 cfg->line_out_pins);
4390 if (err < 0)
4391 return err;
4392 }
4393 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
4394 err = create_out_jack_modes(codec, cfg->hp_outs,
4395 cfg->hp_pins);
4396 if (err < 0)
4397 return err;
4398 }
4399 }
4400
Takashi Iwai352f7f92012-12-19 12:52:06 +01004401 dig_only:
4402 parse_digital(codec);
4403
Takashi Iwai55196ff2013-01-24 17:32:56 +01004404 if (spec->power_down_unused)
4405 codec->power_filter = snd_hda_gen_path_power_filter;
4406
Takashi Iwai7504b6c2013-03-18 11:25:51 +01004407 if (!spec->no_analog && spec->beep_nid) {
4408 err = snd_hda_attach_beep_device(codec, spec->beep_nid);
4409 if (err < 0)
4410 return err;
4411 }
4412
Takashi Iwai352f7f92012-12-19 12:52:06 +01004413 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004414}
Takashi Iwai352f7f92012-12-19 12:52:06 +01004415EXPORT_SYMBOL_HDA(snd_hda_gen_parse_auto_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004416
4417
4418/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01004419 * Build control elements
Linus Torvalds1da177e2005-04-16 15:20:36 -07004420 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01004421
4422/* slave controls for virtual master */
4423static const char * const slave_pfxs[] = {
4424 "Front", "Surround", "Center", "LFE", "Side",
4425 "Headphone", "Speaker", "Mono", "Line Out",
4426 "CLFE", "Bass Speaker", "PCM",
Takashi Iwaiee79c692013-01-07 09:57:42 +01004427 "Speaker Front", "Speaker Surround", "Speaker CLFE", "Speaker Side",
4428 "Headphone Front", "Headphone Surround", "Headphone CLFE",
4429 "Headphone Side",
Takashi Iwai352f7f92012-12-19 12:52:06 +01004430 NULL,
4431};
4432
4433int snd_hda_gen_build_controls(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004434{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004435 struct hda_gen_spec *spec = codec->spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004436 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004437
Takashi Iwai36502d02012-12-19 15:15:10 +01004438 if (spec->kctls.used) {
4439 err = snd_hda_add_new_ctls(codec, spec->kctls.list);
4440 if (err < 0)
4441 return err;
4442 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004443
Takashi Iwai352f7f92012-12-19 12:52:06 +01004444 if (spec->multiout.dig_out_nid) {
4445 err = snd_hda_create_dig_out_ctls(codec,
4446 spec->multiout.dig_out_nid,
4447 spec->multiout.dig_out_nid,
4448 spec->pcm_rec[1].pcm_type);
4449 if (err < 0)
4450 return err;
4451 if (!spec->no_analog) {
4452 err = snd_hda_create_spdif_share_sw(codec,
4453 &spec->multiout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004454 if (err < 0)
4455 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004456 spec->multiout.share_spdif = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004457 }
4458 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01004459 if (spec->dig_in_nid) {
4460 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
4461 if (err < 0)
4462 return err;
4463 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004464
Takashi Iwai352f7f92012-12-19 12:52:06 +01004465 /* if we have no master control, let's create it */
4466 if (!spec->no_analog &&
4467 !snd_hda_find_mixer_ctl(codec, "Master Playback Volume")) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01004468 err = snd_hda_add_vmaster(codec, "Master Playback Volume",
Takashi Iwai7a71bbf2013-01-17 10:25:15 +01004469 spec->vmaster_tlv, slave_pfxs,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004470 "Playback Volume");
4471 if (err < 0)
4472 return err;
4473 }
4474 if (!spec->no_analog &&
4475 !snd_hda_find_mixer_ctl(codec, "Master Playback Switch")) {
4476 err = __snd_hda_add_vmaster(codec, "Master Playback Switch",
4477 NULL, slave_pfxs,
4478 "Playback Switch",
4479 true, &spec->vmaster_mute.sw_kctl);
4480 if (err < 0)
4481 return err;
Takashi Iwaib63eae02013-10-25 23:43:10 +02004482 if (spec->vmaster_mute.hook) {
Takashi Iwaifd25a972012-12-20 14:57:18 +01004483 snd_hda_add_vmaster_hook(codec, &spec->vmaster_mute,
4484 spec->vmaster_mute_enum);
Takashi Iwaib63eae02013-10-25 23:43:10 +02004485 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
4486 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01004487 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004488
Takashi Iwai352f7f92012-12-19 12:52:06 +01004489 free_kctls(spec); /* no longer needed */
4490
Takashi Iwai352f7f92012-12-19 12:52:06 +01004491 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
4492 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004493 return err;
4494
4495 return 0;
4496}
Takashi Iwai352f7f92012-12-19 12:52:06 +01004497EXPORT_SYMBOL_HDA(snd_hda_gen_build_controls);
4498
Linus Torvalds1da177e2005-04-16 15:20:36 -07004499
4500/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01004501 * PCM definitions
Linus Torvalds1da177e2005-04-16 15:20:36 -07004502 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004503
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004504static void call_pcm_playback_hook(struct hda_pcm_stream *hinfo,
4505 struct hda_codec *codec,
4506 struct snd_pcm_substream *substream,
4507 int action)
4508{
4509 struct hda_gen_spec *spec = codec->spec;
4510 if (spec->pcm_playback_hook)
4511 spec->pcm_playback_hook(hinfo, codec, substream, action);
4512}
4513
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004514static void call_pcm_capture_hook(struct hda_pcm_stream *hinfo,
4515 struct hda_codec *codec,
4516 struct snd_pcm_substream *substream,
4517 int action)
4518{
4519 struct hda_gen_spec *spec = codec->spec;
4520 if (spec->pcm_capture_hook)
4521 spec->pcm_capture_hook(hinfo, codec, substream, action);
4522}
4523
Takashi Iwai352f7f92012-12-19 12:52:06 +01004524/*
4525 * Analog playback callbacks
4526 */
4527static int playback_pcm_open(struct hda_pcm_stream *hinfo,
4528 struct hda_codec *codec,
4529 struct snd_pcm_substream *substream)
4530{
4531 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004532 int err;
4533
4534 mutex_lock(&spec->pcm_mutex);
4535 err = snd_hda_multi_out_analog_open(codec,
4536 &spec->multiout, substream,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004537 hinfo);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004538 if (!err) {
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004539 spec->active_streams |= 1 << STREAM_MULTI_OUT;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004540 call_pcm_playback_hook(hinfo, codec, substream,
4541 HDA_GEN_PCM_ACT_OPEN);
4542 }
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004543 mutex_unlock(&spec->pcm_mutex);
4544 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004545}
4546
4547static int playback_pcm_prepare(struct hda_pcm_stream *hinfo,
Takashi Iwai97ec5582006-03-21 11:29:07 +01004548 struct hda_codec *codec,
4549 unsigned int stream_tag,
4550 unsigned int format,
4551 struct snd_pcm_substream *substream)
4552{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004553 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004554 int err;
4555
4556 err = snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
4557 stream_tag, format, substream);
4558 if (!err)
4559 call_pcm_playback_hook(hinfo, codec, substream,
4560 HDA_GEN_PCM_ACT_PREPARE);
4561 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004562}
Takashi Iwai97ec5582006-03-21 11:29:07 +01004563
Takashi Iwai352f7f92012-12-19 12:52:06 +01004564static int playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
4565 struct hda_codec *codec,
4566 struct snd_pcm_substream *substream)
4567{
4568 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004569 int err;
4570
4571 err = snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
4572 if (!err)
4573 call_pcm_playback_hook(hinfo, codec, substream,
4574 HDA_GEN_PCM_ACT_CLEANUP);
4575 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004576}
4577
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004578static int playback_pcm_close(struct hda_pcm_stream *hinfo,
4579 struct hda_codec *codec,
4580 struct snd_pcm_substream *substream)
4581{
4582 struct hda_gen_spec *spec = codec->spec;
4583 mutex_lock(&spec->pcm_mutex);
4584 spec->active_streams &= ~(1 << STREAM_MULTI_OUT);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004585 call_pcm_playback_hook(hinfo, codec, substream,
4586 HDA_GEN_PCM_ACT_CLOSE);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004587 mutex_unlock(&spec->pcm_mutex);
4588 return 0;
4589}
4590
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004591static int capture_pcm_open(struct hda_pcm_stream *hinfo,
4592 struct hda_codec *codec,
4593 struct snd_pcm_substream *substream)
4594{
4595 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_OPEN);
4596 return 0;
4597}
4598
4599static int capture_pcm_prepare(struct hda_pcm_stream *hinfo,
4600 struct hda_codec *codec,
4601 unsigned int stream_tag,
4602 unsigned int format,
4603 struct snd_pcm_substream *substream)
4604{
4605 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4606 call_pcm_capture_hook(hinfo, codec, substream,
4607 HDA_GEN_PCM_ACT_PREPARE);
4608 return 0;
4609}
4610
4611static int capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
4612 struct hda_codec *codec,
4613 struct snd_pcm_substream *substream)
4614{
4615 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
4616 call_pcm_capture_hook(hinfo, codec, substream,
4617 HDA_GEN_PCM_ACT_CLEANUP);
4618 return 0;
4619}
4620
4621static int capture_pcm_close(struct hda_pcm_stream *hinfo,
4622 struct hda_codec *codec,
4623 struct snd_pcm_substream *substream)
4624{
4625 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_CLOSE);
4626 return 0;
4627}
4628
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004629static int alt_playback_pcm_open(struct hda_pcm_stream *hinfo,
4630 struct hda_codec *codec,
4631 struct snd_pcm_substream *substream)
4632{
4633 struct hda_gen_spec *spec = codec->spec;
4634 int err = 0;
4635
4636 mutex_lock(&spec->pcm_mutex);
4637 if (!spec->indep_hp_enabled)
4638 err = -EBUSY;
4639 else
4640 spec->active_streams |= 1 << STREAM_INDEP_HP;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004641 call_pcm_playback_hook(hinfo, codec, substream,
4642 HDA_GEN_PCM_ACT_OPEN);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004643 mutex_unlock(&spec->pcm_mutex);
4644 return err;
4645}
4646
4647static int alt_playback_pcm_close(struct hda_pcm_stream *hinfo,
4648 struct hda_codec *codec,
4649 struct snd_pcm_substream *substream)
4650{
4651 struct hda_gen_spec *spec = codec->spec;
4652 mutex_lock(&spec->pcm_mutex);
4653 spec->active_streams &= ~(1 << STREAM_INDEP_HP);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004654 call_pcm_playback_hook(hinfo, codec, substream,
4655 HDA_GEN_PCM_ACT_CLOSE);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004656 mutex_unlock(&spec->pcm_mutex);
4657 return 0;
4658}
4659
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004660static int alt_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
4661 struct hda_codec *codec,
4662 unsigned int stream_tag,
4663 unsigned int format,
4664 struct snd_pcm_substream *substream)
4665{
4666 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4667 call_pcm_playback_hook(hinfo, codec, substream,
4668 HDA_GEN_PCM_ACT_PREPARE);
4669 return 0;
4670}
4671
4672static int alt_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
4673 struct hda_codec *codec,
4674 struct snd_pcm_substream *substream)
4675{
4676 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
4677 call_pcm_playback_hook(hinfo, codec, substream,
4678 HDA_GEN_PCM_ACT_CLEANUP);
4679 return 0;
4680}
4681
Takashi Iwai352f7f92012-12-19 12:52:06 +01004682/*
4683 * Digital out
4684 */
4685static int dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
4686 struct hda_codec *codec,
4687 struct snd_pcm_substream *substream)
4688{
4689 struct hda_gen_spec *spec = codec->spec;
4690 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
4691}
4692
4693static int dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
4694 struct hda_codec *codec,
4695 unsigned int stream_tag,
4696 unsigned int format,
4697 struct snd_pcm_substream *substream)
4698{
4699 struct hda_gen_spec *spec = codec->spec;
4700 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
4701 stream_tag, format, substream);
4702}
4703
4704static int dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
4705 struct hda_codec *codec,
4706 struct snd_pcm_substream *substream)
4707{
4708 struct hda_gen_spec *spec = codec->spec;
4709 return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
4710}
4711
4712static int dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
4713 struct hda_codec *codec,
4714 struct snd_pcm_substream *substream)
4715{
4716 struct hda_gen_spec *spec = codec->spec;
4717 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
4718}
4719
4720/*
4721 * Analog capture
4722 */
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004723#define alt_capture_pcm_open capture_pcm_open
4724#define alt_capture_pcm_close capture_pcm_close
4725
Takashi Iwai352f7f92012-12-19 12:52:06 +01004726static int alt_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
4727 struct hda_codec *codec,
4728 unsigned int stream_tag,
4729 unsigned int format,
4730 struct snd_pcm_substream *substream)
4731{
4732 struct hda_gen_spec *spec = codec->spec;
4733
4734 snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number + 1],
Takashi Iwai97ec5582006-03-21 11:29:07 +01004735 stream_tag, 0, format);
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004736 call_pcm_capture_hook(hinfo, codec, substream,
4737 HDA_GEN_PCM_ACT_PREPARE);
Takashi Iwai97ec5582006-03-21 11:29:07 +01004738 return 0;
4739}
4740
Takashi Iwai352f7f92012-12-19 12:52:06 +01004741static int alt_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
4742 struct hda_codec *codec,
4743 struct snd_pcm_substream *substream)
Takashi Iwai97ec5582006-03-21 11:29:07 +01004744{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004745 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai97ec5582006-03-21 11:29:07 +01004746
Takashi Iwai352f7f92012-12-19 12:52:06 +01004747 snd_hda_codec_cleanup_stream(codec,
4748 spec->adc_nids[substream->number + 1]);
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004749 call_pcm_capture_hook(hinfo, codec, substream,
4750 HDA_GEN_PCM_ACT_CLEANUP);
Takashi Iwai97ec5582006-03-21 11:29:07 +01004751 return 0;
4752}
4753
Takashi Iwai352f7f92012-12-19 12:52:06 +01004754/*
4755 */
4756static const struct hda_pcm_stream pcm_analog_playback = {
4757 .substreams = 1,
4758 .channels_min = 2,
4759 .channels_max = 8,
4760 /* NID is set in build_pcms */
4761 .ops = {
4762 .open = playback_pcm_open,
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004763 .close = playback_pcm_close,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004764 .prepare = playback_pcm_prepare,
4765 .cleanup = playback_pcm_cleanup
4766 },
4767};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004768
Takashi Iwai352f7f92012-12-19 12:52:06 +01004769static const struct hda_pcm_stream pcm_analog_capture = {
4770 .substreams = 1,
4771 .channels_min = 2,
4772 .channels_max = 2,
4773 /* NID is set in build_pcms */
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004774 .ops = {
4775 .open = capture_pcm_open,
4776 .close = capture_pcm_close,
4777 .prepare = capture_pcm_prepare,
4778 .cleanup = capture_pcm_cleanup
4779 },
Takashi Iwai352f7f92012-12-19 12:52:06 +01004780};
4781
4782static const struct hda_pcm_stream pcm_analog_alt_playback = {
4783 .substreams = 1,
4784 .channels_min = 2,
4785 .channels_max = 2,
4786 /* NID is set in build_pcms */
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004787 .ops = {
4788 .open = alt_playback_pcm_open,
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004789 .close = alt_playback_pcm_close,
4790 .prepare = alt_playback_pcm_prepare,
4791 .cleanup = alt_playback_pcm_cleanup
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004792 },
Takashi Iwai352f7f92012-12-19 12:52:06 +01004793};
4794
4795static const struct hda_pcm_stream pcm_analog_alt_capture = {
4796 .substreams = 2, /* can be overridden */
4797 .channels_min = 2,
4798 .channels_max = 2,
4799 /* NID is set in build_pcms */
4800 .ops = {
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004801 .open = alt_capture_pcm_open,
4802 .close = alt_capture_pcm_close,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004803 .prepare = alt_capture_pcm_prepare,
4804 .cleanup = alt_capture_pcm_cleanup
4805 },
4806};
4807
4808static const struct hda_pcm_stream pcm_digital_playback = {
4809 .substreams = 1,
4810 .channels_min = 2,
4811 .channels_max = 2,
4812 /* NID is set in build_pcms */
4813 .ops = {
4814 .open = dig_playback_pcm_open,
4815 .close = dig_playback_pcm_close,
4816 .prepare = dig_playback_pcm_prepare,
4817 .cleanup = dig_playback_pcm_cleanup
4818 },
4819};
4820
4821static const struct hda_pcm_stream pcm_digital_capture = {
4822 .substreams = 1,
4823 .channels_min = 2,
4824 .channels_max = 2,
4825 /* NID is set in build_pcms */
4826};
4827
4828/* Used by build_pcms to flag that a PCM has no playback stream */
4829static const struct hda_pcm_stream pcm_null_stream = {
4830 .substreams = 0,
4831 .channels_min = 0,
4832 .channels_max = 0,
4833};
4834
4835/*
4836 * dynamic changing ADC PCM streams
4837 */
4838static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur)
4839{
4840 struct hda_gen_spec *spec = codec->spec;
4841 hda_nid_t new_adc = spec->adc_nids[spec->dyn_adc_idx[cur]];
4842
4843 if (spec->cur_adc && spec->cur_adc != new_adc) {
4844 /* stream is running, let's swap the current ADC */
4845 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
4846 spec->cur_adc = new_adc;
4847 snd_hda_codec_setup_stream(codec, new_adc,
4848 spec->cur_adc_stream_tag, 0,
4849 spec->cur_adc_format);
4850 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004851 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01004852 return false;
4853}
4854
4855/* analog capture with dynamic dual-adc changes */
4856static int dyn_adc_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
4857 struct hda_codec *codec,
4858 unsigned int stream_tag,
4859 unsigned int format,
4860 struct snd_pcm_substream *substream)
4861{
4862 struct hda_gen_spec *spec = codec->spec;
4863 spec->cur_adc = spec->adc_nids[spec->dyn_adc_idx[spec->cur_mux[0]]];
4864 spec->cur_adc_stream_tag = stream_tag;
4865 spec->cur_adc_format = format;
4866 snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
4867 return 0;
4868}
4869
4870static int dyn_adc_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
4871 struct hda_codec *codec,
4872 struct snd_pcm_substream *substream)
4873{
4874 struct hda_gen_spec *spec = codec->spec;
4875 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
4876 spec->cur_adc = 0;
4877 return 0;
4878}
4879
4880static const struct hda_pcm_stream dyn_adc_pcm_analog_capture = {
4881 .substreams = 1,
4882 .channels_min = 2,
4883 .channels_max = 2,
4884 .nid = 0, /* fill later */
4885 .ops = {
4886 .prepare = dyn_adc_capture_pcm_prepare,
4887 .cleanup = dyn_adc_capture_pcm_cleanup
4888 },
4889};
4890
Takashi Iwaif873e532012-12-20 16:58:39 +01004891static void fill_pcm_stream_name(char *str, size_t len, const char *sfx,
4892 const char *chip_name)
4893{
4894 char *p;
4895
4896 if (*str)
4897 return;
4898 strlcpy(str, chip_name, len);
4899
4900 /* drop non-alnum chars after a space */
4901 for (p = strchr(str, ' '); p; p = strchr(p + 1, ' ')) {
4902 if (!isalnum(p[1])) {
4903 *p = 0;
4904 break;
4905 }
4906 }
4907 strlcat(str, sfx, len);
4908}
4909
Takashi Iwai352f7f92012-12-19 12:52:06 +01004910/* build PCM streams based on the parsed results */
4911int snd_hda_gen_build_pcms(struct hda_codec *codec)
4912{
4913 struct hda_gen_spec *spec = codec->spec;
4914 struct hda_pcm *info = spec->pcm_rec;
4915 const struct hda_pcm_stream *p;
4916 bool have_multi_adcs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004917
4918 codec->num_pcms = 1;
4919 codec->pcm_info = info;
4920
Takashi Iwai352f7f92012-12-19 12:52:06 +01004921 if (spec->no_analog)
4922 goto skip_analog;
4923
Takashi Iwaif873e532012-12-20 16:58:39 +01004924 fill_pcm_stream_name(spec->stream_name_analog,
4925 sizeof(spec->stream_name_analog),
4926 " Analog", codec->chip_name);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004927 info->name = spec->stream_name_analog;
4928
4929 if (spec->multiout.num_dacs > 0) {
4930 p = spec->stream_analog_playback;
4931 if (!p)
4932 p = &pcm_analog_playback;
4933 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
4934 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dac_nids[0];
4935 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
4936 spec->multiout.max_channels;
4937 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT &&
4938 spec->autocfg.line_outs == 2)
4939 info->stream[SNDRV_PCM_STREAM_PLAYBACK].chmap =
4940 snd_pcm_2_1_chmaps;
4941 }
4942 if (spec->num_adc_nids) {
4943 p = spec->stream_analog_capture;
4944 if (!p) {
4945 if (spec->dyn_adc_switch)
4946 p = &dyn_adc_pcm_analog_capture;
4947 else
4948 p = &pcm_analog_capture;
4949 }
4950 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
4951 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adc_nids[0];
4952 }
4953
Takashi Iwai352f7f92012-12-19 12:52:06 +01004954 skip_analog:
4955 /* SPDIF for stream index #1 */
4956 if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
Takashi Iwaif873e532012-12-20 16:58:39 +01004957 fill_pcm_stream_name(spec->stream_name_digital,
4958 sizeof(spec->stream_name_digital),
4959 " Digital", codec->chip_name);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004960 codec->num_pcms = 2;
4961 codec->slave_dig_outs = spec->multiout.slave_dig_outs;
4962 info = spec->pcm_rec + 1;
4963 info->name = spec->stream_name_digital;
4964 if (spec->dig_out_type)
4965 info->pcm_type = spec->dig_out_type;
4966 else
4967 info->pcm_type = HDA_PCM_TYPE_SPDIF;
4968 if (spec->multiout.dig_out_nid) {
4969 p = spec->stream_digital_playback;
4970 if (!p)
4971 p = &pcm_digital_playback;
4972 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
4973 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dig_out_nid;
4974 }
4975 if (spec->dig_in_nid) {
4976 p = spec->stream_digital_capture;
4977 if (!p)
4978 p = &pcm_digital_capture;
4979 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
4980 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in_nid;
4981 }
4982 }
4983
4984 if (spec->no_analog)
4985 return 0;
4986
4987 /* If the use of more than one ADC is requested for the current
4988 * model, configure a second analog capture-only PCM.
4989 */
4990 have_multi_adcs = (spec->num_adc_nids > 1) &&
4991 !spec->dyn_adc_switch && !spec->auto_mic;
4992 /* Additional Analaog capture for index #2 */
4993 if (spec->alt_dac_nid || have_multi_adcs) {
Takashi Iwaia6071482013-01-21 16:50:09 +01004994 fill_pcm_stream_name(spec->stream_name_alt_analog,
4995 sizeof(spec->stream_name_alt_analog),
4996 " Alt Analog", codec->chip_name);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004997 codec->num_pcms = 3;
4998 info = spec->pcm_rec + 2;
Takashi Iwaia6071482013-01-21 16:50:09 +01004999 info->name = spec->stream_name_alt_analog;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005000 if (spec->alt_dac_nid) {
5001 p = spec->stream_analog_alt_playback;
5002 if (!p)
5003 p = &pcm_analog_alt_playback;
5004 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
5005 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
5006 spec->alt_dac_nid;
5007 } else {
5008 info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
5009 pcm_null_stream;
5010 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = 0;
5011 }
5012 if (have_multi_adcs) {
5013 p = spec->stream_analog_alt_capture;
5014 if (!p)
5015 p = &pcm_analog_alt_capture;
5016 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
5017 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
5018 spec->adc_nids[1];
5019 info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams =
5020 spec->num_adc_nids - 1;
5021 } else {
5022 info->stream[SNDRV_PCM_STREAM_CAPTURE] =
5023 pcm_null_stream;
5024 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = 0;
5025 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005026 }
5027
5028 return 0;
5029}
Takashi Iwai352f7f92012-12-19 12:52:06 +01005030EXPORT_SYMBOL_HDA(snd_hda_gen_build_pcms);
5031
5032
5033/*
5034 * Standard auto-parser initializations
5035 */
5036
Takashi Iwaid4156932013-01-07 10:08:02 +01005037/* configure the given path as a proper output */
Takashi Iwai2c12c302013-01-10 09:33:29 +01005038static void set_output_and_unmute(struct hda_codec *codec, int path_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01005039{
5040 struct nid_path *path;
Takashi Iwaid4156932013-01-07 10:08:02 +01005041 hda_nid_t pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005042
Takashi Iwai196c17662013-01-04 15:01:40 +01005043 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwaid4156932013-01-07 10:08:02 +01005044 if (!path || !path->depth)
Takashi Iwai352f7f92012-12-19 12:52:06 +01005045 return;
Takashi Iwaid4156932013-01-07 10:08:02 +01005046 pin = path->path[path->depth - 1];
Takashi Iwai2c12c302013-01-10 09:33:29 +01005047 restore_pin_ctl(codec, pin);
Takashi Iwai65033cc2013-04-16 12:31:05 +02005048 snd_hda_activate_path(codec, path, path->active,
5049 aamix_default(codec->spec));
Takashi Iwaie1284af2013-01-03 16:33:02 +01005050 set_pin_eapd(codec, pin, path->active);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005051}
5052
5053/* initialize primary output paths */
5054static void init_multi_out(struct hda_codec *codec)
5055{
5056 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005057 int i;
5058
Takashi Iwaid4156932013-01-07 10:08:02 +01005059 for (i = 0; i < spec->autocfg.line_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01005060 set_output_and_unmute(codec, spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005061}
5062
Takashi Iwaidb23fd12012-12-20 15:27:24 +01005063
Takashi Iwai2c12c302013-01-10 09:33:29 +01005064static void __init_extra_out(struct hda_codec *codec, int num_outs, int *paths)
Takashi Iwai352f7f92012-12-19 12:52:06 +01005065{
Takashi Iwai352f7f92012-12-19 12:52:06 +01005066 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005067
Takashi Iwaid4156932013-01-07 10:08:02 +01005068 for (i = 0; i < num_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01005069 set_output_and_unmute(codec, paths[i]);
Takashi Iwaidb23fd12012-12-20 15:27:24 +01005070}
5071
5072/* initialize hp and speaker paths */
5073static void init_extra_out(struct hda_codec *codec)
5074{
5075 struct hda_gen_spec *spec = codec->spec;
5076
5077 if (spec->autocfg.line_out_type != AUTO_PIN_HP_OUT)
Takashi Iwai2c12c302013-01-10 09:33:29 +01005078 __init_extra_out(codec, spec->autocfg.hp_outs, spec->hp_paths);
Takashi Iwaidb23fd12012-12-20 15:27:24 +01005079 if (spec->autocfg.line_out_type != AUTO_PIN_SPEAKER_OUT)
5080 __init_extra_out(codec, spec->autocfg.speaker_outs,
Takashi Iwai2c12c302013-01-10 09:33:29 +01005081 spec->speaker_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005082}
5083
5084/* initialize multi-io paths */
5085static void init_multi_io(struct hda_codec *codec)
5086{
5087 struct hda_gen_spec *spec = codec->spec;
5088 int i;
5089
5090 for (i = 0; i < spec->multi_ios; i++) {
5091 hda_nid_t pin = spec->multi_io[i].pin;
5092 struct nid_path *path;
Takashi Iwai196c17662013-01-04 15:01:40 +01005093 path = get_multiio_path(codec, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005094 if (!path)
5095 continue;
5096 if (!spec->multi_io[i].ctl_in)
5097 spec->multi_io[i].ctl_in =
Takashi Iwai2c12c302013-01-10 09:33:29 +01005098 snd_hda_codec_get_pin_target(codec, pin);
Takashi Iwai65033cc2013-04-16 12:31:05 +02005099 snd_hda_activate_path(codec, path, path->active,
5100 aamix_default(spec));
Takashi Iwai352f7f92012-12-19 12:52:06 +01005101 }
5102}
5103
Takashi Iwai352f7f92012-12-19 12:52:06 +01005104/* set up input pins and loopback paths */
5105static void init_analog_input(struct hda_codec *codec)
5106{
5107 struct hda_gen_spec *spec = codec->spec;
5108 struct auto_pin_cfg *cfg = &spec->autocfg;
5109 int i;
5110
5111 for (i = 0; i < cfg->num_inputs; i++) {
5112 hda_nid_t nid = cfg->inputs[i].pin;
5113 if (is_input_pin(codec, nid))
Takashi Iwai2c12c302013-01-10 09:33:29 +01005114 restore_pin_ctl(codec, nid);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005115
5116 /* init loopback inputs */
5117 if (spec->mixer_nid) {
Takashi Iwai3e367f12013-01-23 17:07:23 +01005118 resume_path_from_idx(codec, spec->loopback_paths[i]);
5119 resume_path_from_idx(codec, spec->loopback_merge_path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005120 }
5121 }
5122}
5123
5124/* initialize ADC paths */
5125static void init_input_src(struct hda_codec *codec)
5126{
5127 struct hda_gen_spec *spec = codec->spec;
5128 struct hda_input_mux *imux = &spec->input_mux;
5129 struct nid_path *path;
5130 int i, c, nums;
5131
5132 if (spec->dyn_adc_switch)
5133 nums = 1;
5134 else
5135 nums = spec->num_adc_nids;
5136
5137 for (c = 0; c < nums; c++) {
5138 for (i = 0; i < imux->num_items; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01005139 path = get_input_path(codec, c, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005140 if (path) {
5141 bool active = path->active;
5142 if (i == spec->cur_mux[c])
5143 active = true;
5144 snd_hda_activate_path(codec, path, active, false);
5145 }
5146 }
Takashi Iwai967303d2013-02-19 17:12:42 +01005147 if (spec->hp_mic)
5148 update_hp_mic(codec, c, true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005149 }
5150
Takashi Iwai352f7f92012-12-19 12:52:06 +01005151 if (spec->cap_sync_hook)
Takashi Iwaia90229e2013-01-18 14:10:00 +01005152 spec->cap_sync_hook(codec, NULL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005153}
5154
5155/* set right pin controls for digital I/O */
5156static void init_digital(struct hda_codec *codec)
5157{
5158 struct hda_gen_spec *spec = codec->spec;
5159 int i;
5160 hda_nid_t pin;
5161
Takashi Iwaid4156932013-01-07 10:08:02 +01005162 for (i = 0; i < spec->autocfg.dig_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01005163 set_output_and_unmute(codec, spec->digout_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005164 pin = spec->autocfg.dig_in_pin;
Takashi Iwai2430d7b2013-01-04 15:09:42 +01005165 if (pin) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01005166 restore_pin_ctl(codec, pin);
Takashi Iwai3e367f12013-01-23 17:07:23 +01005167 resume_path_from_idx(codec, spec->digin_path);
Takashi Iwai2430d7b2013-01-04 15:09:42 +01005168 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01005169}
5170
Takashi Iwai973e4972012-12-20 15:16:09 +01005171/* clear unsol-event tags on unused pins; Conexant codecs seem to leave
5172 * invalid unsol tags by some reason
5173 */
5174static void clear_unsol_on_unused_pins(struct hda_codec *codec)
5175{
5176 int i;
5177
5178 for (i = 0; i < codec->init_pins.used; i++) {
5179 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
5180 hda_nid_t nid = pin->nid;
5181 if (is_jack_detectable(codec, nid) &&
5182 !snd_hda_jack_tbl_get(codec, nid))
5183 snd_hda_codec_update_cache(codec, nid, 0,
5184 AC_VERB_SET_UNSOLICITED_ENABLE, 0);
5185 }
5186}
5187
Takashi Iwai5187ac12013-01-07 12:52:16 +01005188/*
5189 * initialize the generic spec;
5190 * this can be put as patch_ops.init function
5191 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01005192int snd_hda_gen_init(struct hda_codec *codec)
5193{
5194 struct hda_gen_spec *spec = codec->spec;
5195
5196 if (spec->init_hook)
5197 spec->init_hook(codec);
5198
5199 snd_hda_apply_verbs(codec);
5200
Takashi Iwai3bbcd272012-12-20 11:50:58 +01005201 codec->cached_write = 1;
5202
Takashi Iwai352f7f92012-12-19 12:52:06 +01005203 init_multi_out(codec);
5204 init_extra_out(codec);
5205 init_multi_io(codec);
5206 init_analog_input(codec);
5207 init_input_src(codec);
5208 init_digital(codec);
5209
Takashi Iwai973e4972012-12-20 15:16:09 +01005210 clear_unsol_on_unused_pins(codec);
5211
Takashi Iwai352f7f92012-12-19 12:52:06 +01005212 /* call init functions of standard auto-mute helpers */
Takashi Iwaia5cc2502013-01-16 18:08:55 +01005213 update_automute_all(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005214
Takashi Iwaidc870f32013-01-22 15:24:30 +01005215 snd_hda_codec_flush_cache(codec);
Takashi Iwai3bbcd272012-12-20 11:50:58 +01005216
Takashi Iwai352f7f92012-12-19 12:52:06 +01005217 if (spec->vmaster_mute.sw_kctl && spec->vmaster_mute.hook)
5218 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
5219
5220 hda_call_check_power_status(codec, 0x01);
5221 return 0;
5222}
Takashi Iwaifce52a32013-01-07 12:42:48 +01005223EXPORT_SYMBOL_HDA(snd_hda_gen_init);
5224
Takashi Iwai5187ac12013-01-07 12:52:16 +01005225/*
5226 * free the generic spec;
5227 * this can be put as patch_ops.free function
5228 */
Takashi Iwaifce52a32013-01-07 12:42:48 +01005229void snd_hda_gen_free(struct hda_codec *codec)
5230{
Takashi Iwai7504b6c2013-03-18 11:25:51 +01005231 snd_hda_detach_beep_device(codec);
Takashi Iwaifce52a32013-01-07 12:42:48 +01005232 snd_hda_gen_spec_free(codec->spec);
5233 kfree(codec->spec);
5234 codec->spec = NULL;
5235}
5236EXPORT_SYMBOL_HDA(snd_hda_gen_free);
5237
5238#ifdef CONFIG_PM
Takashi Iwai5187ac12013-01-07 12:52:16 +01005239/*
5240 * check the loopback power save state;
5241 * this can be put as patch_ops.check_power_status function
5242 */
Takashi Iwaifce52a32013-01-07 12:42:48 +01005243int snd_hda_gen_check_power_status(struct hda_codec *codec, hda_nid_t nid)
5244{
5245 struct hda_gen_spec *spec = codec->spec;
5246 return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
5247}
5248EXPORT_SYMBOL_HDA(snd_hda_gen_check_power_status);
5249#endif
Takashi Iwai352f7f92012-12-19 12:52:06 +01005250
5251
5252/*
5253 * the generic codec support
5254 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005255
Takashi Iwai352f7f92012-12-19 12:52:06 +01005256static const struct hda_codec_ops generic_patch_ops = {
5257 .build_controls = snd_hda_gen_build_controls,
5258 .build_pcms = snd_hda_gen_build_pcms,
5259 .init = snd_hda_gen_init,
Takashi Iwaifce52a32013-01-07 12:42:48 +01005260 .free = snd_hda_gen_free,
Takashi Iwai352f7f92012-12-19 12:52:06 +01005261 .unsol_event = snd_hda_jack_unsol_event,
Takashi Iwai83012a72012-08-24 18:38:08 +02005262#ifdef CONFIG_PM
Takashi Iwaifce52a32013-01-07 12:42:48 +01005263 .check_power_status = snd_hda_gen_check_power_status,
Takashi Iwaicb53c622007-08-10 17:21:45 +02005264#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005265};
5266
Linus Torvalds1da177e2005-04-16 15:20:36 -07005267int snd_hda_parse_generic_codec(struct hda_codec *codec)
5268{
Takashi Iwai352f7f92012-12-19 12:52:06 +01005269 struct hda_gen_spec *spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005270 int err;
5271
Takashi Iwaie560d8d2005-09-09 14:21:46 +02005272 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005273 if (!spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005274 return -ENOMEM;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005275 snd_hda_gen_spec_init(spec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005276 codec->spec = spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005277
Takashi Iwai9eb413e2012-12-19 14:41:21 +01005278 err = snd_hda_parse_pin_defcfg(codec, &spec->autocfg, NULL, 0);
5279 if (err < 0)
5280 return err;
5281
5282 err = snd_hda_gen_parse_auto_config(codec, &spec->autocfg);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005283 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005284 goto error;
5285
5286 codec->patch_ops = generic_patch_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005287 return 0;
5288
Takashi Iwai352f7f92012-12-19 12:52:06 +01005289error:
Takashi Iwaifce52a32013-01-07 12:42:48 +01005290 snd_hda_gen_free(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005291 return err;
5292}
Takashi Iwaifce52a32013-01-07 12:42:48 +01005293EXPORT_SYMBOL_HDA(snd_hda_parse_generic_codec);