blob: cfdb917d74fb97f3e2c061675c5ec2706254889c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Universal Interface for Intel High Definition Audio Codec
3 *
4 * Generic widget tree parser
5 *
6 * Copyright (c) 2004 Takashi Iwai <tiwai@suse.de>
7 *
8 * This driver is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This driver is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */
22
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/init.h>
24#include <linux/slab.h>
Paul Gortmakerd81a6d72011-09-22 09:34:58 -040025#include <linux/export.h>
Takashi Iwai352f7f92012-12-19 12:52:06 +010026#include <linux/sort.h>
Takashi Iwai55196ff2013-01-24 17:32:56 +010027#include <linux/delay.h>
Takashi Iwaif873e532012-12-20 16:58:39 +010028#include <linux/ctype.h>
29#include <linux/string.h>
Takashi Iwai294765582013-01-17 09:52:11 +010030#include <linux/bitops.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <sound/core.h>
Takashi Iwai352f7f92012-12-19 12:52:06 +010032#include <sound/jack.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include "hda_codec.h"
34#include "hda_local.h"
Takashi Iwai352f7f92012-12-19 12:52:06 +010035#include "hda_auto_parser.h"
36#include "hda_jack.h"
Takashi Iwai7504b6c2013-03-18 11:25:51 +010037#include "hda_beep.h"
Takashi Iwai352f7f92012-12-19 12:52:06 +010038#include "hda_generic.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Takashi Iwai352f7f92012-12-19 12:52:06 +010041/* initialize hda_gen_spec struct */
42int snd_hda_gen_spec_init(struct hda_gen_spec *spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -070043{
Takashi Iwai352f7f92012-12-19 12:52:06 +010044 snd_array_init(&spec->kctls, sizeof(struct snd_kcontrol_new), 32);
Takashi Iwai352f7f92012-12-19 12:52:06 +010045 snd_array_init(&spec->paths, sizeof(struct nid_path), 8);
Takashi Iwai0186f4f2013-02-07 10:45:11 +010046 snd_array_init(&spec->loopback_list, sizeof(struct hda_amp_list), 8);
Takashi Iwai38cf6f12012-12-21 14:09:42 +010047 mutex_init(&spec->pcm_mutex);
Takashi Iwai352f7f92012-12-19 12:52:06 +010048 return 0;
49}
50EXPORT_SYMBOL_HDA(snd_hda_gen_spec_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Takashi Iwai12c93df2012-12-19 14:38:33 +010052struct snd_kcontrol_new *
53snd_hda_gen_add_kctl(struct hda_gen_spec *spec, const char *name,
54 const struct snd_kcontrol_new *temp)
Takashi Iwai352f7f92012-12-19 12:52:06 +010055{
56 struct snd_kcontrol_new *knew = snd_array_new(&spec->kctls);
57 if (!knew)
58 return NULL;
59 *knew = *temp;
60 if (name)
61 knew->name = kstrdup(name, GFP_KERNEL);
62 else if (knew->name)
63 knew->name = kstrdup(knew->name, GFP_KERNEL);
64 if (!knew->name)
65 return NULL;
66 return knew;
67}
Takashi Iwai12c93df2012-12-19 14:38:33 +010068EXPORT_SYMBOL_HDA(snd_hda_gen_add_kctl);
Takashi Iwai352f7f92012-12-19 12:52:06 +010069
70static void free_kctls(struct hda_gen_spec *spec)
71{
72 if (spec->kctls.list) {
73 struct snd_kcontrol_new *kctl = spec->kctls.list;
74 int i;
75 for (i = 0; i < spec->kctls.used; i++)
76 kfree(kctl[i].name);
77 }
78 snd_array_free(&spec->kctls);
79}
80
Takashi Iwai352f7f92012-12-19 12:52:06 +010081void snd_hda_gen_spec_free(struct hda_gen_spec *spec)
82{
83 if (!spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 return;
Takashi Iwai352f7f92012-12-19 12:52:06 +010085 free_kctls(spec);
Takashi Iwai352f7f92012-12-19 12:52:06 +010086 snd_array_free(&spec->paths);
Takashi Iwai0186f4f2013-02-07 10:45:11 +010087 snd_array_free(&spec->loopback_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088}
Takashi Iwai352f7f92012-12-19 12:52:06 +010089EXPORT_SYMBOL_HDA(snd_hda_gen_spec_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
91/*
Takashi Iwai1c70a582013-01-11 17:48:22 +010092 * store user hints
93 */
94static void parse_user_hints(struct hda_codec *codec)
95{
96 struct hda_gen_spec *spec = codec->spec;
97 int val;
98
99 val = snd_hda_get_bool_hint(codec, "jack_detect");
100 if (val >= 0)
101 codec->no_jack_detect = !val;
102 val = snd_hda_get_bool_hint(codec, "inv_jack_detect");
103 if (val >= 0)
104 codec->inv_jack_detect = !!val;
105 val = snd_hda_get_bool_hint(codec, "trigger_sense");
106 if (val >= 0)
107 codec->no_trigger_sense = !val;
108 val = snd_hda_get_bool_hint(codec, "inv_eapd");
109 if (val >= 0)
110 codec->inv_eapd = !!val;
111 val = snd_hda_get_bool_hint(codec, "pcm_format_first");
112 if (val >= 0)
113 codec->pcm_format_first = !!val;
114 val = snd_hda_get_bool_hint(codec, "sticky_stream");
115 if (val >= 0)
116 codec->no_sticky_stream = !val;
117 val = snd_hda_get_bool_hint(codec, "spdif_status_reset");
118 if (val >= 0)
119 codec->spdif_status_reset = !!val;
120 val = snd_hda_get_bool_hint(codec, "pin_amp_workaround");
121 if (val >= 0)
122 codec->pin_amp_workaround = !!val;
123 val = snd_hda_get_bool_hint(codec, "single_adc_amp");
124 if (val >= 0)
125 codec->single_adc_amp = !!val;
126
Takashi Iwaif72706b2013-01-16 18:20:07 +0100127 val = snd_hda_get_bool_hint(codec, "auto_mute");
128 if (val >= 0)
129 spec->suppress_auto_mute = !val;
Takashi Iwai1c70a582013-01-11 17:48:22 +0100130 val = snd_hda_get_bool_hint(codec, "auto_mic");
131 if (val >= 0)
132 spec->suppress_auto_mic = !val;
133 val = snd_hda_get_bool_hint(codec, "line_in_auto_switch");
134 if (val >= 0)
135 spec->line_in_auto_switch = !!val;
136 val = snd_hda_get_bool_hint(codec, "need_dac_fix");
137 if (val >= 0)
138 spec->need_dac_fix = !!val;
139 val = snd_hda_get_bool_hint(codec, "primary_hp");
140 if (val >= 0)
141 spec->no_primary_hp = !val;
142 val = snd_hda_get_bool_hint(codec, "multi_cap_vol");
143 if (val >= 0)
144 spec->multi_cap_vol = !!val;
145 val = snd_hda_get_bool_hint(codec, "inv_dmic_split");
146 if (val >= 0)
147 spec->inv_dmic_split = !!val;
148 val = snd_hda_get_bool_hint(codec, "indep_hp");
149 if (val >= 0)
150 spec->indep_hp = !!val;
151 val = snd_hda_get_bool_hint(codec, "add_stereo_mix_input");
152 if (val >= 0)
153 spec->add_stereo_mix_input = !!val;
Takashi Iwaif811c3c2013-03-07 18:32:59 +0100154 /* the following two are just for compatibility */
Takashi Iwai1c70a582013-01-11 17:48:22 +0100155 val = snd_hda_get_bool_hint(codec, "add_out_jack_modes");
156 if (val >= 0)
Takashi Iwaif811c3c2013-03-07 18:32:59 +0100157 spec->add_jack_modes = !!val;
Takashi Iwai294765582013-01-17 09:52:11 +0100158 val = snd_hda_get_bool_hint(codec, "add_in_jack_modes");
159 if (val >= 0)
Takashi Iwaif811c3c2013-03-07 18:32:59 +0100160 spec->add_jack_modes = !!val;
161 val = snd_hda_get_bool_hint(codec, "add_jack_modes");
162 if (val >= 0)
163 spec->add_jack_modes = !!val;
Takashi Iwai55196ff2013-01-24 17:32:56 +0100164 val = snd_hda_get_bool_hint(codec, "power_down_unused");
165 if (val >= 0)
166 spec->power_down_unused = !!val;
Takashi Iwai967303d2013-02-19 17:12:42 +0100167 val = snd_hda_get_bool_hint(codec, "add_hp_mic");
168 if (val >= 0)
169 spec->hp_mic = !!val;
170 val = snd_hda_get_bool_hint(codec, "hp_mic_detect");
171 if (val >= 0)
172 spec->suppress_hp_mic_detect = !val;
Takashi Iwai1c70a582013-01-11 17:48:22 +0100173
174 if (!snd_hda_get_int_hint(codec, "mixer_nid", &val))
175 spec->mixer_nid = val;
176}
177
178/*
Takashi Iwai2c12c302013-01-10 09:33:29 +0100179 * pin control value accesses
180 */
181
182#define update_pin_ctl(codec, pin, val) \
183 snd_hda_codec_update_cache(codec, pin, 0, \
184 AC_VERB_SET_PIN_WIDGET_CONTROL, val)
185
186/* restore the pinctl based on the cached value */
187static inline void restore_pin_ctl(struct hda_codec *codec, hda_nid_t pin)
188{
189 update_pin_ctl(codec, pin, snd_hda_codec_get_pin_target(codec, pin));
190}
191
192/* set the pinctl target value and write it if requested */
193static void set_pin_target(struct hda_codec *codec, hda_nid_t pin,
194 unsigned int val, bool do_write)
195{
196 if (!pin)
197 return;
198 val = snd_hda_correct_pin_ctl(codec, pin, val);
199 snd_hda_codec_set_pin_target(codec, pin, val);
200 if (do_write)
201 update_pin_ctl(codec, pin, val);
202}
203
204/* set pinctl target values for all given pins */
205static void set_pin_targets(struct hda_codec *codec, int num_pins,
206 hda_nid_t *pins, unsigned int val)
207{
208 int i;
209 for (i = 0; i < num_pins; i++)
210 set_pin_target(codec, pins[i], val, false);
211}
212
213/*
Takashi Iwai352f7f92012-12-19 12:52:06 +0100214 * parsing paths
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100217/* return the position of NID in the list, or -1 if not found */
218static int find_idx_in_nid_list(hda_nid_t nid, const hda_nid_t *list, int nums)
219{
220 int i;
221 for (i = 0; i < nums; i++)
222 if (list[i] == nid)
223 return i;
224 return -1;
225}
226
227/* return true if the given NID is contained in the path */
228static bool is_nid_contained(struct nid_path *path, hda_nid_t nid)
229{
230 return find_idx_in_nid_list(nid, path->path, path->depth) >= 0;
231}
232
Takashi Iwaif5172a72013-01-04 13:19:55 +0100233static struct nid_path *get_nid_path(struct hda_codec *codec,
234 hda_nid_t from_nid, hda_nid_t to_nid,
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100235 int anchor_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100237 struct hda_gen_spec *spec = codec->spec;
238 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
Takashi Iwai352f7f92012-12-19 12:52:06 +0100240 for (i = 0; i < spec->paths.used; i++) {
241 struct nid_path *path = snd_array_elem(&spec->paths, i);
242 if (path->depth <= 0)
243 continue;
244 if ((!from_nid || path->path[0] == from_nid) &&
Takashi Iwaif5172a72013-01-04 13:19:55 +0100245 (!to_nid || path->path[path->depth - 1] == to_nid)) {
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100246 if (!anchor_nid ||
247 (anchor_nid > 0 && is_nid_contained(path, anchor_nid)) ||
248 (anchor_nid < 0 && !is_nid_contained(path, anchor_nid)))
Takashi Iwaif5172a72013-01-04 13:19:55 +0100249 return path;
250 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 }
252 return NULL;
253}
Takashi Iwaif5172a72013-01-04 13:19:55 +0100254
255/* get the path between the given NIDs;
256 * passing 0 to either @pin or @dac behaves as a wildcard
257 */
258struct nid_path *snd_hda_get_nid_path(struct hda_codec *codec,
259 hda_nid_t from_nid, hda_nid_t to_nid)
260{
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100261 return get_nid_path(codec, from_nid, to_nid, 0);
Takashi Iwaif5172a72013-01-04 13:19:55 +0100262}
Takashi Iwai352f7f92012-12-19 12:52:06 +0100263EXPORT_SYMBOL_HDA(snd_hda_get_nid_path);
264
Takashi Iwai196c17662013-01-04 15:01:40 +0100265/* get the index number corresponding to the path instance;
266 * the index starts from 1, for easier checking the invalid value
267 */
268int snd_hda_get_path_idx(struct hda_codec *codec, struct nid_path *path)
269{
270 struct hda_gen_spec *spec = codec->spec;
271 struct nid_path *array = spec->paths.list;
272 ssize_t idx;
273
274 if (!spec->paths.used)
275 return 0;
276 idx = path - array;
277 if (idx < 0 || idx >= spec->paths.used)
278 return 0;
279 return idx + 1;
280}
Takashi Iwai4bd01e92013-01-22 15:17:20 +0100281EXPORT_SYMBOL_HDA(snd_hda_get_path_idx);
Takashi Iwai196c17662013-01-04 15:01:40 +0100282
283/* get the path instance corresponding to the given index number */
284struct nid_path *snd_hda_get_path_from_idx(struct hda_codec *codec, int idx)
285{
286 struct hda_gen_spec *spec = codec->spec;
287
288 if (idx <= 0 || idx > spec->paths.used)
289 return NULL;
290 return snd_array_elem(&spec->paths, idx - 1);
291}
Takashi Iwai4bd01e92013-01-22 15:17:20 +0100292EXPORT_SYMBOL_HDA(snd_hda_get_path_from_idx);
Takashi Iwai196c17662013-01-04 15:01:40 +0100293
Takashi Iwai352f7f92012-12-19 12:52:06 +0100294/* check whether the given DAC is already found in any existing paths */
295static bool is_dac_already_used(struct hda_codec *codec, hda_nid_t nid)
296{
297 struct hda_gen_spec *spec = codec->spec;
298 int i;
299
300 for (i = 0; i < spec->paths.used; i++) {
301 struct nid_path *path = snd_array_elem(&spec->paths, i);
302 if (path->path[0] == nid)
303 return true;
304 }
305 return false;
306}
307
308/* check whether the given two widgets can be connected */
309static bool is_reachable_path(struct hda_codec *codec,
310 hda_nid_t from_nid, hda_nid_t to_nid)
311{
312 if (!from_nid || !to_nid)
313 return false;
314 return snd_hda_get_conn_index(codec, to_nid, from_nid, true) >= 0;
315}
316
317/* nid, dir and idx */
318#define AMP_VAL_COMPARE_MASK (0xffff | (1U << 18) | (0x0f << 19))
319
320/* check whether the given ctl is already assigned in any path elements */
321static bool is_ctl_used(struct hda_codec *codec, unsigned int val, int type)
322{
323 struct hda_gen_spec *spec = codec->spec;
324 int i;
325
326 val &= AMP_VAL_COMPARE_MASK;
327 for (i = 0; i < spec->paths.used; i++) {
328 struct nid_path *path = snd_array_elem(&spec->paths, i);
329 if ((path->ctls[type] & AMP_VAL_COMPARE_MASK) == val)
330 return true;
331 }
332 return false;
333}
334
335/* check whether a control with the given (nid, dir, idx) was assigned */
336static bool is_ctl_associated(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwai8999bf02013-01-18 11:01:33 +0100337 int dir, int idx, int type)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100338{
339 unsigned int val = HDA_COMPOSE_AMP_VAL(nid, 3, idx, dir);
Takashi Iwai8999bf02013-01-18 11:01:33 +0100340 return is_ctl_used(codec, val, type);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100341}
342
Takashi Iwai0c8c0f52012-12-20 17:54:22 +0100343static void print_nid_path(const char *pfx, struct nid_path *path)
344{
345 char buf[40];
346 int i;
347
348
349 buf[0] = 0;
350 for (i = 0; i < path->depth; i++) {
351 char tmp[4];
352 sprintf(tmp, ":%02x", path->path[i]);
353 strlcat(buf, tmp, sizeof(buf));
354 }
355 snd_printdd("%s path: depth=%d %s\n", pfx, path->depth, buf);
356}
357
Takashi Iwai352f7f92012-12-19 12:52:06 +0100358/* called recursively */
359static bool __parse_nid_path(struct hda_codec *codec,
360 hda_nid_t from_nid, hda_nid_t to_nid,
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100361 int anchor_nid, struct nid_path *path,
362 int depth)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100363{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100364 const hda_nid_t *conn;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100365 int i, nums;
366
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100367 if (to_nid == anchor_nid)
368 anchor_nid = 0; /* anchor passed */
369 else if (to_nid == (hda_nid_t)(-anchor_nid))
370 return false; /* hit the exclusive nid */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100371
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100372 nums = snd_hda_get_conn_list(codec, to_nid, &conn);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100373 for (i = 0; i < nums; i++) {
374 if (conn[i] != from_nid) {
375 /* special case: when from_nid is 0,
376 * try to find an empty DAC
377 */
378 if (from_nid ||
379 get_wcaps_type(get_wcaps(codec, conn[i])) != AC_WID_AUD_OUT ||
380 is_dac_already_used(codec, conn[i]))
381 continue;
382 }
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100383 /* anchor is not requested or already passed? */
384 if (anchor_nid <= 0)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100385 goto found;
386 }
387 if (depth >= MAX_NID_PATH_DEPTH)
388 return false;
389 for (i = 0; i < nums; i++) {
390 unsigned int type;
391 type = get_wcaps_type(get_wcaps(codec, conn[i]));
392 if (type == AC_WID_AUD_OUT || type == AC_WID_AUD_IN ||
393 type == AC_WID_PIN)
394 continue;
395 if (__parse_nid_path(codec, from_nid, conn[i],
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100396 anchor_nid, path, depth + 1))
Takashi Iwai352f7f92012-12-19 12:52:06 +0100397 goto found;
398 }
399 return false;
400
401 found:
402 path->path[path->depth] = conn[i];
403 path->idx[path->depth + 1] = i;
404 if (nums > 1 && get_wcaps_type(get_wcaps(codec, to_nid)) != AC_WID_AUD_MIX)
405 path->multi[path->depth + 1] = 1;
406 path->depth++;
407 return true;
408}
409
410/* parse the widget path from the given nid to the target nid;
411 * when @from_nid is 0, try to find an empty DAC;
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100412 * when @anchor_nid is set to a positive value, only paths through the widget
413 * with the given value are evaluated.
414 * when @anchor_nid is set to a negative value, paths through the widget
415 * with the negative of given value are excluded, only other paths are chosen.
416 * when @anchor_nid is zero, no special handling about path selection.
Takashi Iwai352f7f92012-12-19 12:52:06 +0100417 */
418bool snd_hda_parse_nid_path(struct hda_codec *codec, hda_nid_t from_nid,
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100419 hda_nid_t to_nid, int anchor_nid,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100420 struct nid_path *path)
421{
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100422 if (__parse_nid_path(codec, from_nid, to_nid, anchor_nid, path, 1)) {
Takashi Iwai352f7f92012-12-19 12:52:06 +0100423 path->path[path->depth] = to_nid;
424 path->depth++;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100425 return true;
426 }
427 return false;
428}
429EXPORT_SYMBOL_HDA(snd_hda_parse_nid_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
431/*
Takashi Iwai352f7f92012-12-19 12:52:06 +0100432 * parse the path between the given NIDs and add to the path list.
433 * if no valid path is found, return NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100435struct nid_path *
436snd_hda_add_new_path(struct hda_codec *codec, hda_nid_t from_nid,
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100437 hda_nid_t to_nid, int anchor_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100439 struct hda_gen_spec *spec = codec->spec;
440 struct nid_path *path;
441
442 if (from_nid && to_nid && !is_reachable_path(codec, from_nid, to_nid))
443 return NULL;
444
Takashi Iwaif5172a72013-01-04 13:19:55 +0100445 /* check whether the path has been already added */
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100446 path = get_nid_path(codec, from_nid, to_nid, anchor_nid);
Takashi Iwaif5172a72013-01-04 13:19:55 +0100447 if (path)
448 return path;
449
Takashi Iwai352f7f92012-12-19 12:52:06 +0100450 path = snd_array_new(&spec->paths);
451 if (!path)
452 return NULL;
453 memset(path, 0, sizeof(*path));
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100454 if (snd_hda_parse_nid_path(codec, from_nid, to_nid, anchor_nid, path))
Takashi Iwai352f7f92012-12-19 12:52:06 +0100455 return path;
456 /* push back */
457 spec->paths.used--;
458 return NULL;
459}
460EXPORT_SYMBOL_HDA(snd_hda_add_new_path);
461
Takashi Iwai980428c2013-01-09 09:28:20 +0100462/* clear the given path as invalid so that it won't be picked up later */
463static void invalidate_nid_path(struct hda_codec *codec, int idx)
464{
465 struct nid_path *path = snd_hda_get_path_from_idx(codec, idx);
466 if (!path)
467 return;
468 memset(path, 0, sizeof(*path));
469}
470
Takashi Iwai352f7f92012-12-19 12:52:06 +0100471/* look for an empty DAC slot */
472static hda_nid_t look_for_dac(struct hda_codec *codec, hda_nid_t pin,
473 bool is_digital)
474{
475 struct hda_gen_spec *spec = codec->spec;
476 bool cap_digital;
477 int i;
478
479 for (i = 0; i < spec->num_all_dacs; i++) {
480 hda_nid_t nid = spec->all_dacs[i];
481 if (!nid || is_dac_already_used(codec, nid))
482 continue;
483 cap_digital = !!(get_wcaps(codec, nid) & AC_WCAP_DIGITAL);
484 if (is_digital != cap_digital)
485 continue;
486 if (is_reachable_path(codec, nid, pin))
487 return nid;
488 }
489 return 0;
490}
491
492/* replace the channels in the composed amp value with the given number */
493static unsigned int amp_val_replace_channels(unsigned int val, unsigned int chs)
494{
495 val &= ~(0x3U << 16);
496 val |= chs << 16;
497 return val;
498}
499
500/* check whether the widget has the given amp capability for the direction */
501static bool check_amp_caps(struct hda_codec *codec, hda_nid_t nid,
502 int dir, unsigned int bits)
503{
504 if (!nid)
505 return false;
506 if (get_wcaps(codec, nid) & (1 << (dir + 1)))
507 if (query_amp_caps(codec, nid, dir) & bits)
508 return true;
509 return false;
510}
511
David Henningsson99a55922013-01-16 15:58:44 +0100512static bool same_amp_caps(struct hda_codec *codec, hda_nid_t nid1,
513 hda_nid_t nid2, int dir)
514{
515 if (!(get_wcaps(codec, nid1) & (1 << (dir + 1))))
516 return !(get_wcaps(codec, nid2) & (1 << (dir + 1)));
517 return (query_amp_caps(codec, nid1, dir) ==
518 query_amp_caps(codec, nid2, dir));
519}
520
Takashi Iwai352f7f92012-12-19 12:52:06 +0100521#define nid_has_mute(codec, nid, dir) \
522 check_amp_caps(codec, nid, dir, AC_AMPCAP_MUTE)
523#define nid_has_volume(codec, nid, dir) \
524 check_amp_caps(codec, nid, dir, AC_AMPCAP_NUM_STEPS)
525
526/* look for a widget suitable for assigning a mute switch in the path */
527static hda_nid_t look_for_out_mute_nid(struct hda_codec *codec,
528 struct nid_path *path)
529{
530 int i;
531
532 for (i = path->depth - 1; i >= 0; i--) {
533 if (nid_has_mute(codec, path->path[i], HDA_OUTPUT))
534 return path->path[i];
535 if (i != path->depth - 1 && i != 0 &&
536 nid_has_mute(codec, path->path[i], HDA_INPUT))
537 return path->path[i];
538 }
539 return 0;
540}
541
542/* look for a widget suitable for assigning a volume ctl in the path */
543static hda_nid_t look_for_out_vol_nid(struct hda_codec *codec,
544 struct nid_path *path)
545{
546 int i;
547
548 for (i = path->depth - 1; i >= 0; i--) {
549 if (nid_has_volume(codec, path->path[i], HDA_OUTPUT))
550 return path->path[i];
551 }
Takashi Iwai82beb8f2007-08-10 17:09:26 +0200552 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553}
554
555/*
Takashi Iwai352f7f92012-12-19 12:52:06 +0100556 * path activation / deactivation
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100558
559/* can have the amp-in capability? */
560static bool has_amp_in(struct hda_codec *codec, struct nid_path *path, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100562 hda_nid_t nid = path->path[idx];
563 unsigned int caps = get_wcaps(codec, nid);
564 unsigned int type = get_wcaps_type(caps);
565
566 if (!(caps & AC_WCAP_IN_AMP))
567 return false;
568 if (type == AC_WID_PIN && idx > 0) /* only for input pins */
569 return false;
570 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571}
572
Takashi Iwai352f7f92012-12-19 12:52:06 +0100573/* can have the amp-out capability? */
574static bool has_amp_out(struct hda_codec *codec, struct nid_path *path, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100576 hda_nid_t nid = path->path[idx];
577 unsigned int caps = get_wcaps(codec, nid);
578 unsigned int type = get_wcaps_type(caps);
579
580 if (!(caps & AC_WCAP_OUT_AMP))
581 return false;
582 if (type == AC_WID_PIN && !idx) /* only for output pins */
583 return false;
584 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585}
586
Takashi Iwai352f7f92012-12-19 12:52:06 +0100587/* check whether the given (nid,dir,idx) is active */
588static bool is_active_nid(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwai7dddf2ae2013-01-24 16:31:35 +0100589 unsigned int dir, unsigned int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100591 struct hda_gen_spec *spec = codec->spec;
592 int i, n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
Takashi Iwai352f7f92012-12-19 12:52:06 +0100594 for (n = 0; n < spec->paths.used; n++) {
595 struct nid_path *path = snd_array_elem(&spec->paths, n);
596 if (!path->active)
597 continue;
598 for (i = 0; i < path->depth; i++) {
599 if (path->path[i] == nid) {
600 if (dir == HDA_OUTPUT || path->idx[i] == idx)
601 return true;
602 break;
603 }
604 }
605 }
606 return false;
607}
608
Takashi Iwaib1b9fbd2013-05-14 12:58:47 +0200609/* check whether the NID is referred by any active paths */
610#define is_active_nid_for_any(codec, nid) \
611 is_active_nid(codec, nid, HDA_OUTPUT, 0)
612
Takashi Iwai352f7f92012-12-19 12:52:06 +0100613/* get the default amp value for the target state */
614static int get_amp_val_to_activate(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwai8999bf02013-01-18 11:01:33 +0100615 int dir, unsigned int caps, bool enable)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100616{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100617 unsigned int val = 0;
618
Takashi Iwai352f7f92012-12-19 12:52:06 +0100619 if (caps & AC_AMPCAP_NUM_STEPS) {
620 /* set to 0dB */
621 if (enable)
622 val = (caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT;
623 }
624 if (caps & AC_AMPCAP_MUTE) {
625 if (!enable)
626 val |= HDA_AMP_MUTE;
627 }
628 return val;
629}
630
631/* initialize the amp value (only at the first time) */
632static void init_amp(struct hda_codec *codec, hda_nid_t nid, int dir, int idx)
633{
Takashi Iwai8999bf02013-01-18 11:01:33 +0100634 unsigned int caps = query_amp_caps(codec, nid, dir);
635 int val = get_amp_val_to_activate(codec, nid, dir, caps, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100636 snd_hda_codec_amp_init_stereo(codec, nid, dir, idx, 0xff, val);
637}
638
Takashi Iwai8999bf02013-01-18 11:01:33 +0100639/* calculate amp value mask we can modify;
640 * if the given amp is controlled by mixers, don't touch it
641 */
642static unsigned int get_amp_mask_to_modify(struct hda_codec *codec,
643 hda_nid_t nid, int dir, int idx,
644 unsigned int caps)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100645{
Takashi Iwai8999bf02013-01-18 11:01:33 +0100646 unsigned int mask = 0xff;
647
648 if (caps & AC_AMPCAP_MUTE) {
649 if (is_ctl_associated(codec, nid, dir, idx, NID_PATH_MUTE_CTL))
650 mask &= ~0x80;
651 }
652 if (caps & AC_AMPCAP_NUM_STEPS) {
653 if (is_ctl_associated(codec, nid, dir, idx, NID_PATH_VOL_CTL) ||
654 is_ctl_associated(codec, nid, dir, idx, NID_PATH_BOOST_CTL))
655 mask &= ~0x7f;
656 }
657 return mask;
658}
659
660static void activate_amp(struct hda_codec *codec, hda_nid_t nid, int dir,
661 int idx, int idx_to_check, bool enable)
662{
663 unsigned int caps;
664 unsigned int mask, val;
665
Takashi Iwai7dddf2ae2013-01-24 16:31:35 +0100666 if (!enable && is_active_nid(codec, nid, dir, idx_to_check))
Takashi Iwai352f7f92012-12-19 12:52:06 +0100667 return;
Takashi Iwai8999bf02013-01-18 11:01:33 +0100668
669 caps = query_amp_caps(codec, nid, dir);
670 val = get_amp_val_to_activate(codec, nid, dir, caps, enable);
671 mask = get_amp_mask_to_modify(codec, nid, dir, idx_to_check, caps);
672 if (!mask)
673 return;
674
675 val &= mask;
676 snd_hda_codec_amp_stereo(codec, nid, dir, idx, mask, val);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100677}
678
679static void activate_amp_out(struct hda_codec *codec, struct nid_path *path,
680 int i, bool enable)
681{
682 hda_nid_t nid = path->path[i];
683 init_amp(codec, nid, HDA_OUTPUT, 0);
Takashi Iwai8999bf02013-01-18 11:01:33 +0100684 activate_amp(codec, nid, HDA_OUTPUT, 0, 0, enable);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100685}
686
687static void activate_amp_in(struct hda_codec *codec, struct nid_path *path,
688 int i, bool enable, bool add_aamix)
689{
690 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100691 const hda_nid_t *conn;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100692 int n, nums, idx;
693 int type;
694 hda_nid_t nid = path->path[i];
695
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100696 nums = snd_hda_get_conn_list(codec, nid, &conn);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100697 type = get_wcaps_type(get_wcaps(codec, nid));
698 if (type == AC_WID_PIN ||
699 (type == AC_WID_AUD_IN && codec->single_adc_amp)) {
700 nums = 1;
701 idx = 0;
702 } else
703 idx = path->idx[i];
704
705 for (n = 0; n < nums; n++)
706 init_amp(codec, nid, HDA_INPUT, n);
707
Takashi Iwai352f7f92012-12-19 12:52:06 +0100708 /* here is a little bit tricky in comparison with activate_amp_out();
709 * when aa-mixer is available, we need to enable the path as well
710 */
711 for (n = 0; n < nums; n++) {
Takashi Iwaie4a395e2013-01-23 17:00:31 +0100712 if (n != idx && (!add_aamix || conn[n] != spec->mixer_merge_nid))
Takashi Iwai352f7f92012-12-19 12:52:06 +0100713 continue;
Takashi Iwai8999bf02013-01-18 11:01:33 +0100714 activate_amp(codec, nid, HDA_INPUT, n, idx, enable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 }
716}
717
Takashi Iwai352f7f92012-12-19 12:52:06 +0100718/* activate or deactivate the given path
719 * if @add_aamix is set, enable the input from aa-mix NID as well (if any)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100721void snd_hda_activate_path(struct hda_codec *codec, struct nid_path *path,
722 bool enable, bool add_aamix)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723{
Takashi Iwai55196ff2013-01-24 17:32:56 +0100724 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100725 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726
Takashi Iwai352f7f92012-12-19 12:52:06 +0100727 if (!enable)
728 path->active = false;
729
730 for (i = path->depth - 1; i >= 0; i--) {
Takashi Iwai55196ff2013-01-24 17:32:56 +0100731 hda_nid_t nid = path->path[i];
732 if (enable && spec->power_down_unused) {
733 /* make sure the widget is powered up */
734 if (!snd_hda_check_power_state(codec, nid, AC_PWRST_D0))
735 snd_hda_codec_write(codec, nid, 0,
736 AC_VERB_SET_POWER_STATE,
737 AC_PWRST_D0);
738 }
Takashi Iwai352f7f92012-12-19 12:52:06 +0100739 if (enable && path->multi[i])
Takashi Iwai55196ff2013-01-24 17:32:56 +0100740 snd_hda_codec_write_cache(codec, nid, 0,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100741 AC_VERB_SET_CONNECT_SEL,
742 path->idx[i]);
743 if (has_amp_in(codec, path, i))
744 activate_amp_in(codec, path, i, enable, add_aamix);
745 if (has_amp_out(codec, path, i))
746 activate_amp_out(codec, path, i, enable);
747 }
748
749 if (enable)
750 path->active = true;
751}
752EXPORT_SYMBOL_HDA(snd_hda_activate_path);
753
Takashi Iwai55196ff2013-01-24 17:32:56 +0100754/* if the given path is inactive, put widgets into D3 (only if suitable) */
755static void path_power_down_sync(struct hda_codec *codec, struct nid_path *path)
756{
757 struct hda_gen_spec *spec = codec->spec;
Jiri Slaby868211d2013-04-04 22:32:10 +0200758 bool changed = false;
Takashi Iwai55196ff2013-01-24 17:32:56 +0100759 int i;
760
761 if (!spec->power_down_unused || path->active)
762 return;
763
764 for (i = 0; i < path->depth; i++) {
765 hda_nid_t nid = path->path[i];
Takashi Iwaib1b9fbd2013-05-14 12:58:47 +0200766 if (!snd_hda_check_power_state(codec, nid, AC_PWRST_D3) &&
767 !is_active_nid_for_any(codec, nid)) {
Takashi Iwai55196ff2013-01-24 17:32:56 +0100768 snd_hda_codec_write(codec, nid, 0,
769 AC_VERB_SET_POWER_STATE,
770 AC_PWRST_D3);
771 changed = true;
772 }
773 }
774
775 if (changed) {
776 msleep(10);
777 snd_hda_codec_read(codec, path->path[0], 0,
778 AC_VERB_GET_POWER_STATE, 0);
779 }
780}
781
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +0100782/* turn on/off EAPD on the given pin */
783static void set_pin_eapd(struct hda_codec *codec, hda_nid_t pin, bool enable)
784{
785 struct hda_gen_spec *spec = codec->spec;
786 if (spec->own_eapd_ctl ||
787 !(snd_hda_query_pin_caps(codec, pin) & AC_PINCAP_EAPD))
788 return;
Takashi Iwaiecac3ed2012-12-21 15:23:01 +0100789 if (codec->inv_eapd)
790 enable = !enable;
Takashi Iwai05909d5c2013-05-31 19:55:54 +0200791 if (spec->keep_eapd_on && !enable)
792 return;
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +0100793 snd_hda_codec_update_cache(codec, pin, 0,
794 AC_VERB_SET_EAPD_BTLENABLE,
795 enable ? 0x02 : 0x00);
796}
797
Takashi Iwai3e367f12013-01-23 17:07:23 +0100798/* re-initialize the path specified by the given path index */
799static void resume_path_from_idx(struct hda_codec *codec, int path_idx)
800{
801 struct nid_path *path = snd_hda_get_path_from_idx(codec, path_idx);
802 if (path)
803 snd_hda_activate_path(codec, path, path->active, false);
804}
805
Takashi Iwai352f7f92012-12-19 12:52:06 +0100806
807/*
808 * Helper functions for creating mixer ctl elements
809 */
810
811enum {
812 HDA_CTL_WIDGET_VOL,
813 HDA_CTL_WIDGET_MUTE,
814 HDA_CTL_BIND_MUTE,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100815};
816static const struct snd_kcontrol_new control_templates[] = {
817 HDA_CODEC_VOLUME(NULL, 0, 0, 0),
818 HDA_CODEC_MUTE(NULL, 0, 0, 0),
819 HDA_BIND_MUTE(NULL, 0, 0, 0),
Takashi Iwai352f7f92012-12-19 12:52:06 +0100820};
821
822/* add dynamic controls from template */
Takashi Iwaia35bd1e2013-01-18 14:01:14 +0100823static struct snd_kcontrol_new *
824add_control(struct hda_gen_spec *spec, int type, const char *name,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100825 int cidx, unsigned long val)
826{
827 struct snd_kcontrol_new *knew;
828
Takashi Iwai12c93df2012-12-19 14:38:33 +0100829 knew = snd_hda_gen_add_kctl(spec, name, &control_templates[type]);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100830 if (!knew)
Takashi Iwaia35bd1e2013-01-18 14:01:14 +0100831 return NULL;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100832 knew->index = cidx;
833 if (get_amp_nid_(val))
834 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
835 knew->private_value = val;
Takashi Iwaia35bd1e2013-01-18 14:01:14 +0100836 return knew;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100837}
838
839static int add_control_with_pfx(struct hda_gen_spec *spec, int type,
840 const char *pfx, const char *dir,
841 const char *sfx, int cidx, unsigned long val)
842{
843 char name[32];
844 snprintf(name, sizeof(name), "%s %s %s", pfx, dir, sfx);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +0100845 if (!add_control(spec, type, name, cidx, val))
846 return -ENOMEM;
847 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100848}
849
850#define add_pb_vol_ctrl(spec, type, pfx, val) \
851 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", 0, val)
852#define add_pb_sw_ctrl(spec, type, pfx, val) \
853 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", 0, val)
854#define __add_pb_vol_ctrl(spec, type, pfx, cidx, val) \
855 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", cidx, val)
856#define __add_pb_sw_ctrl(spec, type, pfx, cidx, val) \
857 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", cidx, val)
858
859static int add_vol_ctl(struct hda_codec *codec, const char *pfx, int cidx,
860 unsigned int chs, struct nid_path *path)
861{
862 unsigned int val;
863 if (!path)
864 return 0;
865 val = path->ctls[NID_PATH_VOL_CTL];
866 if (!val)
867 return 0;
868 val = amp_val_replace_channels(val, chs);
869 return __add_pb_vol_ctrl(codec->spec, HDA_CTL_WIDGET_VOL, pfx, cidx, val);
870}
871
872/* return the channel bits suitable for the given path->ctls[] */
873static int get_default_ch_nums(struct hda_codec *codec, struct nid_path *path,
874 int type)
875{
876 int chs = 1; /* mono (left only) */
877 if (path) {
878 hda_nid_t nid = get_amp_nid_(path->ctls[type]);
879 if (nid && (get_wcaps(codec, nid) & AC_WCAP_STEREO))
880 chs = 3; /* stereo */
881 }
882 return chs;
883}
884
885static int add_stereo_vol(struct hda_codec *codec, const char *pfx, int cidx,
886 struct nid_path *path)
887{
888 int chs = get_default_ch_nums(codec, path, NID_PATH_VOL_CTL);
889 return add_vol_ctl(codec, pfx, cidx, chs, path);
890}
891
892/* create a mute-switch for the given mixer widget;
893 * if it has multiple sources (e.g. DAC and loopback), create a bind-mute
894 */
895static int add_sw_ctl(struct hda_codec *codec, const char *pfx, int cidx,
896 unsigned int chs, struct nid_path *path)
897{
898 unsigned int val;
899 int type = HDA_CTL_WIDGET_MUTE;
900
901 if (!path)
902 return 0;
903 val = path->ctls[NID_PATH_MUTE_CTL];
904 if (!val)
905 return 0;
906 val = amp_val_replace_channels(val, chs);
907 if (get_amp_direction_(val) == HDA_INPUT) {
908 hda_nid_t nid = get_amp_nid_(val);
909 int nums = snd_hda_get_num_conns(codec, nid);
910 if (nums > 1) {
911 type = HDA_CTL_BIND_MUTE;
912 val |= nums << 19;
913 }
914 }
915 return __add_pb_sw_ctrl(codec->spec, type, pfx, cidx, val);
916}
917
918static int add_stereo_sw(struct hda_codec *codec, const char *pfx,
919 int cidx, struct nid_path *path)
920{
921 int chs = get_default_ch_nums(codec, path, NID_PATH_MUTE_CTL);
922 return add_sw_ctl(codec, pfx, cidx, chs, path);
923}
924
Takashi Iwai247d85e2013-01-17 16:18:11 +0100925/* any ctl assigned to the path with the given index? */
926static bool path_has_mixer(struct hda_codec *codec, int path_idx, int ctl_type)
927{
928 struct nid_path *path = snd_hda_get_path_from_idx(codec, path_idx);
929 return path && path->ctls[ctl_type];
930}
931
Takashi Iwai352f7f92012-12-19 12:52:06 +0100932static const char * const channel_name[4] = {
933 "Front", "Surround", "CLFE", "Side"
934};
935
936/* give some appropriate ctl name prefix for the given line out channel */
Takashi Iwai247d85e2013-01-17 16:18:11 +0100937static const char *get_line_out_pfx(struct hda_codec *codec, int ch,
938 int *index, int ctl_type)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100939{
Takashi Iwai247d85e2013-01-17 16:18:11 +0100940 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100941 struct auto_pin_cfg *cfg = &spec->autocfg;
942
943 *index = 0;
944 if (cfg->line_outs == 1 && !spec->multi_ios &&
Takashi Iwai247d85e2013-01-17 16:18:11 +0100945 !cfg->hp_outs && !cfg->speaker_outs)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100946 return spec->vmaster_mute.hook ? "PCM" : "Master";
947
948 /* if there is really a single DAC used in the whole output paths,
949 * use it master (or "PCM" if a vmaster hook is present)
950 */
951 if (spec->multiout.num_dacs == 1 && !spec->mixer_nid &&
952 !spec->multiout.hp_out_nid[0] && !spec->multiout.extra_out_nid[0])
953 return spec->vmaster_mute.hook ? "PCM" : "Master";
954
Takashi Iwai247d85e2013-01-17 16:18:11 +0100955 /* multi-io channels */
956 if (ch >= cfg->line_outs)
957 return channel_name[ch];
958
Takashi Iwai352f7f92012-12-19 12:52:06 +0100959 switch (cfg->line_out_type) {
960 case AUTO_PIN_SPEAKER_OUT:
Takashi Iwai247d85e2013-01-17 16:18:11 +0100961 /* if the primary channel vol/mute is shared with HP volume,
962 * don't name it as Speaker
963 */
964 if (!ch && cfg->hp_outs &&
965 !path_has_mixer(codec, spec->hp_paths[0], ctl_type))
966 break;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100967 if (cfg->line_outs == 1)
968 return "Speaker";
969 if (cfg->line_outs == 2)
970 return ch ? "Bass Speaker" : "Speaker";
971 break;
972 case AUTO_PIN_HP_OUT:
Takashi Iwai247d85e2013-01-17 16:18:11 +0100973 /* if the primary channel vol/mute is shared with spk volume,
974 * don't name it as Headphone
975 */
976 if (!ch && cfg->speaker_outs &&
977 !path_has_mixer(codec, spec->speaker_paths[0], ctl_type))
978 break;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100979 /* for multi-io case, only the primary out */
980 if (ch && spec->multi_ios)
981 break;
982 *index = ch;
983 return "Headphone";
Takashi Iwai352f7f92012-12-19 12:52:06 +0100984 }
Takashi Iwai247d85e2013-01-17 16:18:11 +0100985
986 /* for a single channel output, we don't have to name the channel */
987 if (cfg->line_outs == 1 && !spec->multi_ios)
988 return "PCM";
989
Takashi Iwai352f7f92012-12-19 12:52:06 +0100990 if (ch >= ARRAY_SIZE(channel_name)) {
991 snd_BUG();
992 return "PCM";
993 }
994
995 return channel_name[ch];
996}
997
998/*
999 * Parse output paths
1000 */
1001
1002/* badness definition */
1003enum {
1004 /* No primary DAC is found for the main output */
1005 BAD_NO_PRIMARY_DAC = 0x10000,
1006 /* No DAC is found for the extra output */
1007 BAD_NO_DAC = 0x4000,
1008 /* No possible multi-ios */
Takashi Iwai1d739062013-02-13 14:17:32 +01001009 BAD_MULTI_IO = 0x120,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001010 /* No individual DAC for extra output */
1011 BAD_NO_EXTRA_DAC = 0x102,
1012 /* No individual DAC for extra surrounds */
1013 BAD_NO_EXTRA_SURR_DAC = 0x101,
1014 /* Primary DAC shared with main surrounds */
1015 BAD_SHARED_SURROUND = 0x100,
Takashi Iwai55a63d42013-03-21 17:20:12 +01001016 /* No independent HP possible */
Takashi Iwaibec8e682013-03-22 15:10:08 +01001017 BAD_NO_INDEP_HP = 0x10,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001018 /* Primary DAC shared with main CLFE */
1019 BAD_SHARED_CLFE = 0x10,
1020 /* Primary DAC shared with extra surrounds */
1021 BAD_SHARED_EXTRA_SURROUND = 0x10,
1022 /* Volume widget is shared */
1023 BAD_SHARED_VOL = 0x10,
1024};
1025
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001026/* look for widgets in the given path which are appropriate for
Takashi Iwai352f7f92012-12-19 12:52:06 +01001027 * volume and mute controls, and assign the values to ctls[].
1028 *
1029 * When no appropriate widget is found in the path, the badness value
1030 * is incremented depending on the situation. The function returns the
1031 * total badness for both volume and mute controls.
1032 */
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001033static int assign_out_path_ctls(struct hda_codec *codec, struct nid_path *path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001034{
Takashi Iwai352f7f92012-12-19 12:52:06 +01001035 hda_nid_t nid;
1036 unsigned int val;
1037 int badness = 0;
1038
1039 if (!path)
1040 return BAD_SHARED_VOL * 2;
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001041
1042 if (path->ctls[NID_PATH_VOL_CTL] ||
1043 path->ctls[NID_PATH_MUTE_CTL])
1044 return 0; /* already evaluated */
1045
Takashi Iwai352f7f92012-12-19 12:52:06 +01001046 nid = look_for_out_vol_nid(codec, path);
1047 if (nid) {
1048 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
1049 if (is_ctl_used(codec, val, NID_PATH_VOL_CTL))
1050 badness += BAD_SHARED_VOL;
1051 else
1052 path->ctls[NID_PATH_VOL_CTL] = val;
1053 } else
1054 badness += BAD_SHARED_VOL;
1055 nid = look_for_out_mute_nid(codec, path);
1056 if (nid) {
1057 unsigned int wid_type = get_wcaps_type(get_wcaps(codec, nid));
1058 if (wid_type == AC_WID_PIN || wid_type == AC_WID_AUD_OUT ||
1059 nid_has_mute(codec, nid, HDA_OUTPUT))
1060 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
1061 else
1062 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT);
1063 if (is_ctl_used(codec, val, NID_PATH_MUTE_CTL))
1064 badness += BAD_SHARED_VOL;
1065 else
1066 path->ctls[NID_PATH_MUTE_CTL] = val;
1067 } else
1068 badness += BAD_SHARED_VOL;
1069 return badness;
1070}
1071
Takashi Iwai98bd1112013-03-22 14:53:50 +01001072const struct badness_table hda_main_out_badness = {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001073 .no_primary_dac = BAD_NO_PRIMARY_DAC,
1074 .no_dac = BAD_NO_DAC,
1075 .shared_primary = BAD_NO_PRIMARY_DAC,
1076 .shared_surr = BAD_SHARED_SURROUND,
1077 .shared_clfe = BAD_SHARED_CLFE,
1078 .shared_surr_main = BAD_SHARED_SURROUND,
1079};
Takashi Iwai98bd1112013-03-22 14:53:50 +01001080EXPORT_SYMBOL_HDA(hda_main_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001081
Takashi Iwai98bd1112013-03-22 14:53:50 +01001082const struct badness_table hda_extra_out_badness = {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001083 .no_primary_dac = BAD_NO_DAC,
1084 .no_dac = BAD_NO_DAC,
1085 .shared_primary = BAD_NO_EXTRA_DAC,
1086 .shared_surr = BAD_SHARED_EXTRA_SURROUND,
1087 .shared_clfe = BAD_SHARED_EXTRA_SURROUND,
1088 .shared_surr_main = BAD_NO_EXTRA_SURR_DAC,
1089};
Takashi Iwai98bd1112013-03-22 14:53:50 +01001090EXPORT_SYMBOL_HDA(hda_extra_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001091
Takashi Iwai7385df62013-01-07 09:50:52 +01001092/* get the DAC of the primary output corresponding to the given array index */
1093static hda_nid_t get_primary_out(struct hda_codec *codec, int idx)
1094{
1095 struct hda_gen_spec *spec = codec->spec;
1096 struct auto_pin_cfg *cfg = &spec->autocfg;
1097
1098 if (cfg->line_outs > idx)
1099 return spec->private_dac_nids[idx];
1100 idx -= cfg->line_outs;
1101 if (spec->multi_ios > idx)
1102 return spec->multi_io[idx].dac;
1103 return 0;
1104}
1105
1106/* return the DAC if it's reachable, otherwise zero */
1107static inline hda_nid_t try_dac(struct hda_codec *codec,
1108 hda_nid_t dac, hda_nid_t pin)
1109{
1110 return is_reachable_path(codec, dac, pin) ? dac : 0;
1111}
1112
Takashi Iwai352f7f92012-12-19 12:52:06 +01001113/* try to assign DACs to pins and return the resultant badness */
1114static int try_assign_dacs(struct hda_codec *codec, int num_outs,
1115 const hda_nid_t *pins, hda_nid_t *dacs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001116 int *path_idx,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001117 const struct badness_table *bad)
1118{
1119 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001120 int i, j;
1121 int badness = 0;
1122 hda_nid_t dac;
1123
1124 if (!num_outs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 return 0;
1126
Takashi Iwai352f7f92012-12-19 12:52:06 +01001127 for (i = 0; i < num_outs; i++) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001128 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001129 hda_nid_t pin = pins[i];
Takashi Iwai1e0b5282013-01-04 12:56:52 +01001130
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001131 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
1132 if (path) {
1133 badness += assign_out_path_ctls(codec, path);
Takashi Iwai1e0b5282013-01-04 12:56:52 +01001134 continue;
1135 }
1136
1137 dacs[i] = look_for_dac(codec, pin, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001138 if (!dacs[i] && !i) {
Takashi Iwai980428c2013-01-09 09:28:20 +01001139 /* try to steal the DAC of surrounds for the front */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001140 for (j = 1; j < num_outs; j++) {
1141 if (is_reachable_path(codec, dacs[j], pin)) {
1142 dacs[0] = dacs[j];
1143 dacs[j] = 0;
Takashi Iwai980428c2013-01-09 09:28:20 +01001144 invalidate_nid_path(codec, path_idx[j]);
Takashi Iwai196c17662013-01-04 15:01:40 +01001145 path_idx[j] = 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001146 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 }
1148 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001149 }
1150 dac = dacs[i];
1151 if (!dac) {
Takashi Iwai7385df62013-01-07 09:50:52 +01001152 if (num_outs > 2)
1153 dac = try_dac(codec, get_primary_out(codec, i), pin);
1154 if (!dac)
1155 dac = try_dac(codec, dacs[0], pin);
1156 if (!dac)
1157 dac = try_dac(codec, get_primary_out(codec, i), pin);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001158 if (dac) {
1159 if (!i)
1160 badness += bad->shared_primary;
1161 else if (i == 1)
1162 badness += bad->shared_surr;
1163 else
1164 badness += bad->shared_clfe;
1165 } else if (is_reachable_path(codec, spec->private_dac_nids[0], pin)) {
1166 dac = spec->private_dac_nids[0];
1167 badness += bad->shared_surr_main;
1168 } else if (!i)
1169 badness += bad->no_primary_dac;
1170 else
1171 badness += bad->no_dac;
1172 }
Takashi Iwai1fa335b2013-01-21 11:43:19 +01001173 if (!dac)
1174 continue;
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001175 path = snd_hda_add_new_path(codec, dac, pin, -spec->mixer_nid);
Takashi Iwai117688a2013-01-04 15:41:41 +01001176 if (!path && !i && spec->mixer_nid) {
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001177 /* try with aamix */
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001178 path = snd_hda_add_new_path(codec, dac, pin, 0);
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001179 }
Takashi Iwai1fa335b2013-01-21 11:43:19 +01001180 if (!path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001181 dac = dacs[i] = 0;
Takashi Iwai1fa335b2013-01-21 11:43:19 +01001182 badness += bad->no_dac;
1183 } else {
Takashi Iwaia7694092013-01-21 10:43:18 +01001184 /* print_nid_path("output", path); */
Takashi Iwaie1284af2013-01-03 16:33:02 +01001185 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +01001186 path_idx[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001187 badness += assign_out_path_ctls(codec, path);
Takashi Iwaie1284af2013-01-03 16:33:02 +01001188 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001189 }
1190
1191 return badness;
1192}
1193
1194/* return NID if the given pin has only a single connection to a certain DAC */
1195static hda_nid_t get_dac_if_single(struct hda_codec *codec, hda_nid_t pin)
1196{
1197 struct hda_gen_spec *spec = codec->spec;
1198 int i;
1199 hda_nid_t nid_found = 0;
1200
1201 for (i = 0; i < spec->num_all_dacs; i++) {
1202 hda_nid_t nid = spec->all_dacs[i];
1203 if (!nid || is_dac_already_used(codec, nid))
1204 continue;
1205 if (is_reachable_path(codec, nid, pin)) {
1206 if (nid_found)
1207 return 0;
1208 nid_found = nid;
1209 }
1210 }
1211 return nid_found;
1212}
1213
1214/* check whether the given pin can be a multi-io pin */
1215static bool can_be_multiio_pin(struct hda_codec *codec,
1216 unsigned int location, hda_nid_t nid)
1217{
1218 unsigned int defcfg, caps;
1219
1220 defcfg = snd_hda_codec_get_pincfg(codec, nid);
1221 if (get_defcfg_connect(defcfg) != AC_JACK_PORT_COMPLEX)
1222 return false;
1223 if (location && get_defcfg_location(defcfg) != location)
1224 return false;
1225 caps = snd_hda_query_pin_caps(codec, nid);
1226 if (!(caps & AC_PINCAP_OUT))
1227 return false;
1228 return true;
1229}
1230
Takashi Iwaie22aab72013-01-04 14:50:04 +01001231/* count the number of input pins that are capable to be multi-io */
1232static int count_multiio_pins(struct hda_codec *codec, hda_nid_t reference_pin)
1233{
1234 struct hda_gen_spec *spec = codec->spec;
1235 struct auto_pin_cfg *cfg = &spec->autocfg;
1236 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1237 unsigned int location = get_defcfg_location(defcfg);
1238 int type, i;
1239 int num_pins = 0;
1240
1241 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1242 for (i = 0; i < cfg->num_inputs; i++) {
1243 if (cfg->inputs[i].type != type)
1244 continue;
1245 if (can_be_multiio_pin(codec, location,
1246 cfg->inputs[i].pin))
1247 num_pins++;
1248 }
1249 }
1250 return num_pins;
1251}
1252
Takashi Iwai352f7f92012-12-19 12:52:06 +01001253/*
1254 * multi-io helper
1255 *
1256 * When hardwired is set, try to fill ony hardwired pins, and returns
1257 * zero if any pins are filled, non-zero if nothing found.
1258 * When hardwired is off, try to fill possible input pins, and returns
1259 * the badness value.
1260 */
1261static int fill_multi_ios(struct hda_codec *codec,
1262 hda_nid_t reference_pin,
Takashi Iwaie22aab72013-01-04 14:50:04 +01001263 bool hardwired)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001264{
1265 struct hda_gen_spec *spec = codec->spec;
1266 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwaie22aab72013-01-04 14:50:04 +01001267 int type, i, j, num_pins, old_pins;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001268 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1269 unsigned int location = get_defcfg_location(defcfg);
1270 int badness = 0;
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001271 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001272
1273 old_pins = spec->multi_ios;
1274 if (old_pins >= 2)
1275 goto end_fill;
1276
Takashi Iwaie22aab72013-01-04 14:50:04 +01001277 num_pins = count_multiio_pins(codec, reference_pin);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001278 if (num_pins < 2)
1279 goto end_fill;
1280
Takashi Iwai352f7f92012-12-19 12:52:06 +01001281 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1282 for (i = 0; i < cfg->num_inputs; i++) {
1283 hda_nid_t nid = cfg->inputs[i].pin;
1284 hda_nid_t dac = 0;
1285
1286 if (cfg->inputs[i].type != type)
1287 continue;
1288 if (!can_be_multiio_pin(codec, location, nid))
1289 continue;
1290 for (j = 0; j < spec->multi_ios; j++) {
1291 if (nid == spec->multi_io[j].pin)
1292 break;
1293 }
1294 if (j < spec->multi_ios)
1295 continue;
1296
Takashi Iwai352f7f92012-12-19 12:52:06 +01001297 if (hardwired)
1298 dac = get_dac_if_single(codec, nid);
1299 else if (!dac)
1300 dac = look_for_dac(codec, nid, false);
1301 if (!dac) {
1302 badness++;
1303 continue;
1304 }
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001305 path = snd_hda_add_new_path(codec, dac, nid,
1306 -spec->mixer_nid);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001307 if (!path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001308 badness++;
1309 continue;
1310 }
Takashi Iwaia7694092013-01-21 10:43:18 +01001311 /* print_nid_path("multiio", path); */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001312 spec->multi_io[spec->multi_ios].pin = nid;
1313 spec->multi_io[spec->multi_ios].dac = dac;
Takashi Iwai196c17662013-01-04 15:01:40 +01001314 spec->out_paths[cfg->line_outs + spec->multi_ios] =
1315 snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001316 spec->multi_ios++;
1317 if (spec->multi_ios >= 2)
1318 break;
1319 }
1320 }
1321 end_fill:
1322 if (badness)
1323 badness = BAD_MULTI_IO;
1324 if (old_pins == spec->multi_ios) {
1325 if (hardwired)
1326 return 1; /* nothing found */
1327 else
1328 return badness; /* no badness if nothing found */
1329 }
1330 if (!hardwired && spec->multi_ios < 2) {
1331 /* cancel newly assigned paths */
1332 spec->paths.used -= spec->multi_ios - old_pins;
1333 spec->multi_ios = old_pins;
1334 return badness;
1335 }
1336
1337 /* assign volume and mute controls */
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001338 for (i = old_pins; i < spec->multi_ios; i++) {
1339 path = snd_hda_get_path_from_idx(codec, spec->out_paths[cfg->line_outs + i]);
1340 badness += assign_out_path_ctls(codec, path);
1341 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001342
1343 return badness;
1344}
1345
1346/* map DACs for all pins in the list if they are single connections */
1347static bool map_singles(struct hda_codec *codec, int outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001348 const hda_nid_t *pins, hda_nid_t *dacs, int *path_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001349{
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001350 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001351 int i;
1352 bool found = false;
1353 for (i = 0; i < outs; i++) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001354 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001355 hda_nid_t dac;
1356 if (dacs[i])
1357 continue;
1358 dac = get_dac_if_single(codec, pins[i]);
1359 if (!dac)
1360 continue;
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001361 path = snd_hda_add_new_path(codec, dac, pins[i],
1362 -spec->mixer_nid);
Takashi Iwai117688a2013-01-04 15:41:41 +01001363 if (!path && !i && spec->mixer_nid)
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001364 path = snd_hda_add_new_path(codec, dac, pins[i], 0);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001365 if (path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001366 dacs[i] = dac;
1367 found = true;
Takashi Iwaia7694092013-01-21 10:43:18 +01001368 /* print_nid_path("output", path); */
Takashi Iwaie1284af2013-01-03 16:33:02 +01001369 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +01001370 path_idx[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001371 }
1372 }
1373 return found;
1374}
1375
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001376/* create a new path including aamix if available, and return its index */
1377static int check_aamix_out_path(struct hda_codec *codec, int path_idx)
1378{
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001379 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001380 struct nid_path *path;
Takashi Iwai5ead56f2013-04-16 14:16:54 +02001381 hda_nid_t path_dac, dac, pin;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001382
1383 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001384 if (!path || !path->depth ||
1385 is_nid_contained(path, spec->mixer_nid))
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001386 return 0;
Takashi Iwai5ead56f2013-04-16 14:16:54 +02001387 path_dac = path->path[0];
1388 dac = spec->private_dac_nids[0];
Takashi Iwaif87498b2013-01-21 14:24:31 +01001389 pin = path->path[path->depth - 1];
1390 path = snd_hda_add_new_path(codec, dac, pin, spec->mixer_nid);
1391 if (!path) {
Takashi Iwai5ead56f2013-04-16 14:16:54 +02001392 if (dac != path_dac)
1393 dac = path_dac;
Takashi Iwaif87498b2013-01-21 14:24:31 +01001394 else if (spec->multiout.hp_out_nid[0])
1395 dac = spec->multiout.hp_out_nid[0];
1396 else if (spec->multiout.extra_out_nid[0])
1397 dac = spec->multiout.extra_out_nid[0];
Takashi Iwai5ead56f2013-04-16 14:16:54 +02001398 else
1399 dac = 0;
Takashi Iwaif87498b2013-01-21 14:24:31 +01001400 if (dac)
1401 path = snd_hda_add_new_path(codec, dac, pin,
1402 spec->mixer_nid);
1403 }
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001404 if (!path)
1405 return 0;
Takashi Iwaia7694092013-01-21 10:43:18 +01001406 /* print_nid_path("output-aamix", path); */
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001407 path->active = false; /* unused as default */
1408 return snd_hda_get_path_idx(codec, path);
1409}
1410
Takashi Iwai55a63d42013-03-21 17:20:12 +01001411/* check whether the independent HP is available with the current config */
1412static bool indep_hp_possible(struct hda_codec *codec)
1413{
1414 struct hda_gen_spec *spec = codec->spec;
1415 struct auto_pin_cfg *cfg = &spec->autocfg;
1416 struct nid_path *path;
1417 int i, idx;
1418
1419 if (cfg->line_out_type == AUTO_PIN_HP_OUT)
1420 idx = spec->out_paths[0];
1421 else
1422 idx = spec->hp_paths[0];
1423 path = snd_hda_get_path_from_idx(codec, idx);
1424 if (!path)
1425 return false;
1426
1427 /* assume no path conflicts unless aamix is involved */
1428 if (!spec->mixer_nid || !is_nid_contained(path, spec->mixer_nid))
1429 return true;
1430
1431 /* check whether output paths contain aamix */
1432 for (i = 0; i < cfg->line_outs; i++) {
1433 if (spec->out_paths[i] == idx)
1434 break;
1435 path = snd_hda_get_path_from_idx(codec, spec->out_paths[i]);
1436 if (path && is_nid_contained(path, spec->mixer_nid))
1437 return false;
1438 }
1439 for (i = 0; i < cfg->speaker_outs; i++) {
1440 path = snd_hda_get_path_from_idx(codec, spec->speaker_paths[i]);
1441 if (path && is_nid_contained(path, spec->mixer_nid))
1442 return false;
1443 }
1444
1445 return true;
1446}
1447
Takashi Iwaia07a9492013-01-07 16:44:06 +01001448/* fill the empty entries in the dac array for speaker/hp with the
1449 * shared dac pointed by the paths
1450 */
1451static void refill_shared_dacs(struct hda_codec *codec, int num_outs,
1452 hda_nid_t *dacs, int *path_idx)
1453{
1454 struct nid_path *path;
1455 int i;
1456
1457 for (i = 0; i < num_outs; i++) {
1458 if (dacs[i])
1459 continue;
1460 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
1461 if (!path)
1462 continue;
1463 dacs[i] = path->path[0];
1464 }
1465}
1466
Takashi Iwai352f7f92012-12-19 12:52:06 +01001467/* fill in the dac_nids table from the parsed pin configuration */
1468static int fill_and_eval_dacs(struct hda_codec *codec,
1469 bool fill_hardwired,
1470 bool fill_mio_first)
1471{
1472 struct hda_gen_spec *spec = codec->spec;
1473 struct auto_pin_cfg *cfg = &spec->autocfg;
1474 int i, err, badness;
1475
1476 /* set num_dacs once to full for look_for_dac() */
1477 spec->multiout.num_dacs = cfg->line_outs;
1478 spec->multiout.dac_nids = spec->private_dac_nids;
1479 memset(spec->private_dac_nids, 0, sizeof(spec->private_dac_nids));
1480 memset(spec->multiout.hp_out_nid, 0, sizeof(spec->multiout.hp_out_nid));
1481 memset(spec->multiout.extra_out_nid, 0, sizeof(spec->multiout.extra_out_nid));
1482 spec->multi_ios = 0;
1483 snd_array_free(&spec->paths);
Takashi Iwaicd5be3f2013-01-07 15:07:00 +01001484
1485 /* clear path indices */
1486 memset(spec->out_paths, 0, sizeof(spec->out_paths));
1487 memset(spec->hp_paths, 0, sizeof(spec->hp_paths));
1488 memset(spec->speaker_paths, 0, sizeof(spec->speaker_paths));
1489 memset(spec->aamix_out_paths, 0, sizeof(spec->aamix_out_paths));
1490 memset(spec->digout_paths, 0, sizeof(spec->digout_paths));
Takashi Iwaic697b712013-01-07 17:09:26 +01001491 memset(spec->input_paths, 0, sizeof(spec->input_paths));
Takashi Iwaicd5be3f2013-01-07 15:07:00 +01001492 memset(spec->loopback_paths, 0, sizeof(spec->loopback_paths));
1493 memset(&spec->digin_path, 0, sizeof(spec->digin_path));
1494
Takashi Iwai352f7f92012-12-19 12:52:06 +01001495 badness = 0;
1496
1497 /* fill hard-wired DACs first */
1498 if (fill_hardwired) {
1499 bool mapped;
1500 do {
1501 mapped = map_singles(codec, cfg->line_outs,
1502 cfg->line_out_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001503 spec->private_dac_nids,
1504 spec->out_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001505 mapped |= map_singles(codec, cfg->hp_outs,
1506 cfg->hp_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001507 spec->multiout.hp_out_nid,
1508 spec->hp_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001509 mapped |= map_singles(codec, cfg->speaker_outs,
1510 cfg->speaker_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001511 spec->multiout.extra_out_nid,
1512 spec->speaker_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001513 if (fill_mio_first && cfg->line_outs == 1 &&
1514 cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
Takashi Iwaie22aab72013-01-04 14:50:04 +01001515 err = fill_multi_ios(codec, cfg->line_out_pins[0], true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001516 if (!err)
1517 mapped = true;
1518 }
1519 } while (mapped);
1520 }
1521
1522 badness += try_assign_dacs(codec, cfg->line_outs, cfg->line_out_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001523 spec->private_dac_nids, spec->out_paths,
Takashi Iwai98bd1112013-03-22 14:53:50 +01001524 spec->main_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001525
Takashi Iwai352f7f92012-12-19 12:52:06 +01001526 if (fill_mio_first &&
1527 cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1528 /* try to fill multi-io first */
Takashi Iwaie22aab72013-01-04 14:50:04 +01001529 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001530 if (err < 0)
1531 return err;
1532 /* we don't count badness at this stage yet */
1533 }
1534
1535 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
1536 err = try_assign_dacs(codec, cfg->hp_outs, cfg->hp_pins,
1537 spec->multiout.hp_out_nid,
Takashi Iwai196c17662013-01-04 15:01:40 +01001538 spec->hp_paths,
Takashi Iwai98bd1112013-03-22 14:53:50 +01001539 spec->extra_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001540 if (err < 0)
1541 return err;
1542 badness += err;
1543 }
1544 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1545 err = try_assign_dacs(codec, cfg->speaker_outs,
1546 cfg->speaker_pins,
1547 spec->multiout.extra_out_nid,
Takashi Iwai196c17662013-01-04 15:01:40 +01001548 spec->speaker_paths,
Takashi Iwai98bd1112013-03-22 14:53:50 +01001549 spec->extra_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001550 if (err < 0)
1551 return err;
1552 badness += err;
1553 }
1554 if (cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
Takashi Iwaie22aab72013-01-04 14:50:04 +01001555 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001556 if (err < 0)
1557 return err;
1558 badness += err;
1559 }
Takashi Iwaie22aab72013-01-04 14:50:04 +01001560
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001561 if (spec->mixer_nid) {
1562 spec->aamix_out_paths[0] =
1563 check_aamix_out_path(codec, spec->out_paths[0]);
1564 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1565 spec->aamix_out_paths[1] =
1566 check_aamix_out_path(codec, spec->hp_paths[0]);
1567 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1568 spec->aamix_out_paths[2] =
1569 check_aamix_out_path(codec, spec->speaker_paths[0]);
1570 }
1571
Takashi Iwaie22aab72013-01-04 14:50:04 +01001572 if (cfg->hp_outs && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
1573 if (count_multiio_pins(codec, cfg->hp_pins[0]) >= 2)
1574 spec->multi_ios = 1; /* give badness */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001575
Takashi Iwaia07a9492013-01-07 16:44:06 +01001576 /* re-count num_dacs and squash invalid entries */
1577 spec->multiout.num_dacs = 0;
1578 for (i = 0; i < cfg->line_outs; i++) {
1579 if (spec->private_dac_nids[i])
1580 spec->multiout.num_dacs++;
1581 else {
1582 memmove(spec->private_dac_nids + i,
1583 spec->private_dac_nids + i + 1,
1584 sizeof(hda_nid_t) * (cfg->line_outs - i - 1));
1585 spec->private_dac_nids[cfg->line_outs - 1] = 0;
1586 }
1587 }
1588
1589 spec->ext_channel_count = spec->min_channel_count =
David Henningssonc0f3b212013-01-16 11:45:37 +01001590 spec->multiout.num_dacs * 2;
Takashi Iwaia07a9492013-01-07 16:44:06 +01001591
Takashi Iwai352f7f92012-12-19 12:52:06 +01001592 if (spec->multi_ios == 2) {
1593 for (i = 0; i < 2; i++)
1594 spec->private_dac_nids[spec->multiout.num_dacs++] =
1595 spec->multi_io[i].dac;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001596 } else if (spec->multi_ios) {
1597 spec->multi_ios = 0;
1598 badness += BAD_MULTI_IO;
1599 }
1600
Takashi Iwai55a63d42013-03-21 17:20:12 +01001601 if (spec->indep_hp && !indep_hp_possible(codec))
1602 badness += BAD_NO_INDEP_HP;
1603
Takashi Iwaia07a9492013-01-07 16:44:06 +01001604 /* re-fill the shared DAC for speaker / headphone */
1605 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1606 refill_shared_dacs(codec, cfg->hp_outs,
1607 spec->multiout.hp_out_nid,
1608 spec->hp_paths);
1609 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1610 refill_shared_dacs(codec, cfg->speaker_outs,
1611 spec->multiout.extra_out_nid,
1612 spec->speaker_paths);
1613
Takashi Iwai352f7f92012-12-19 12:52:06 +01001614 return badness;
1615}
1616
1617#define DEBUG_BADNESS
1618
1619#ifdef DEBUG_BADNESS
1620#define debug_badness snd_printdd
1621#else
1622#define debug_badness(...)
1623#endif
1624
Takashi Iwaia7694092013-01-21 10:43:18 +01001625#ifdef DEBUG_BADNESS
1626static inline void print_nid_path_idx(struct hda_codec *codec,
1627 const char *pfx, int idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001628{
Takashi Iwaia7694092013-01-21 10:43:18 +01001629 struct nid_path *path;
1630
1631 path = snd_hda_get_path_from_idx(codec, idx);
1632 if (path)
1633 print_nid_path(pfx, path);
1634}
1635
1636static void debug_show_configs(struct hda_codec *codec,
1637 struct auto_pin_cfg *cfg)
1638{
1639 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia7694092013-01-21 10:43:18 +01001640 static const char * const lo_type[3] = { "LO", "SP", "HP" };
Takashi Iwaia7694092013-01-21 10:43:18 +01001641 int i;
1642
1643 debug_badness("multi_outs = %x/%x/%x/%x : %x/%x/%x/%x (type %s)\n",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001644 cfg->line_out_pins[0], cfg->line_out_pins[1],
Takashi Iwai708122e2012-12-20 17:56:57 +01001645 cfg->line_out_pins[2], cfg->line_out_pins[3],
Takashi Iwai352f7f92012-12-19 12:52:06 +01001646 spec->multiout.dac_nids[0],
1647 spec->multiout.dac_nids[1],
1648 spec->multiout.dac_nids[2],
Takashi Iwaia7694092013-01-21 10:43:18 +01001649 spec->multiout.dac_nids[3],
1650 lo_type[cfg->line_out_type]);
1651 for (i = 0; i < cfg->line_outs; i++)
1652 print_nid_path_idx(codec, " out", spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001653 if (spec->multi_ios > 0)
1654 debug_badness("multi_ios(%d) = %x/%x : %x/%x\n",
1655 spec->multi_ios,
1656 spec->multi_io[0].pin, spec->multi_io[1].pin,
1657 spec->multi_io[0].dac, spec->multi_io[1].dac);
Takashi Iwaia7694092013-01-21 10:43:18 +01001658 for (i = 0; i < spec->multi_ios; i++)
1659 print_nid_path_idx(codec, " mio",
1660 spec->out_paths[cfg->line_outs + i]);
1661 if (cfg->hp_outs)
1662 debug_badness("hp_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001663 cfg->hp_pins[0], cfg->hp_pins[1],
Takashi Iwai708122e2012-12-20 17:56:57 +01001664 cfg->hp_pins[2], cfg->hp_pins[3],
Takashi Iwai352f7f92012-12-19 12:52:06 +01001665 spec->multiout.hp_out_nid[0],
1666 spec->multiout.hp_out_nid[1],
1667 spec->multiout.hp_out_nid[2],
1668 spec->multiout.hp_out_nid[3]);
Takashi Iwaia7694092013-01-21 10:43:18 +01001669 for (i = 0; i < cfg->hp_outs; i++)
1670 print_nid_path_idx(codec, " hp ", spec->hp_paths[i]);
1671 if (cfg->speaker_outs)
1672 debug_badness("spk_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001673 cfg->speaker_pins[0], cfg->speaker_pins[1],
1674 cfg->speaker_pins[2], cfg->speaker_pins[3],
1675 spec->multiout.extra_out_nid[0],
1676 spec->multiout.extra_out_nid[1],
1677 spec->multiout.extra_out_nid[2],
1678 spec->multiout.extra_out_nid[3]);
Takashi Iwaia7694092013-01-21 10:43:18 +01001679 for (i = 0; i < cfg->speaker_outs; i++)
1680 print_nid_path_idx(codec, " spk", spec->speaker_paths[i]);
1681 for (i = 0; i < 3; i++)
1682 print_nid_path_idx(codec, " mix", spec->aamix_out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001683}
Takashi Iwaia7694092013-01-21 10:43:18 +01001684#else
1685#define debug_show_configs(codec, cfg) /* NOP */
1686#endif
Takashi Iwai352f7f92012-12-19 12:52:06 +01001687
1688/* find all available DACs of the codec */
1689static void fill_all_dac_nids(struct hda_codec *codec)
1690{
1691 struct hda_gen_spec *spec = codec->spec;
1692 int i;
1693 hda_nid_t nid = codec->start_nid;
1694
1695 spec->num_all_dacs = 0;
1696 memset(spec->all_dacs, 0, sizeof(spec->all_dacs));
1697 for (i = 0; i < codec->num_nodes; i++, nid++) {
1698 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_AUD_OUT)
1699 continue;
1700 if (spec->num_all_dacs >= ARRAY_SIZE(spec->all_dacs)) {
1701 snd_printk(KERN_ERR "hda: Too many DACs!\n");
1702 break;
1703 }
1704 spec->all_dacs[spec->num_all_dacs++] = nid;
1705 }
1706}
1707
1708static int parse_output_paths(struct hda_codec *codec)
1709{
1710 struct hda_gen_spec *spec = codec->spec;
1711 struct auto_pin_cfg *cfg = &spec->autocfg;
1712 struct auto_pin_cfg *best_cfg;
Takashi Iwai9314a582013-01-21 10:49:05 +01001713 unsigned int val;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001714 int best_badness = INT_MAX;
1715 int badness;
1716 bool fill_hardwired = true, fill_mio_first = true;
1717 bool best_wired = true, best_mio = true;
1718 bool hp_spk_swapped = false;
1719
Takashi Iwai352f7f92012-12-19 12:52:06 +01001720 best_cfg = kmalloc(sizeof(*best_cfg), GFP_KERNEL);
1721 if (!best_cfg)
1722 return -ENOMEM;
1723 *best_cfg = *cfg;
1724
1725 for (;;) {
1726 badness = fill_and_eval_dacs(codec, fill_hardwired,
1727 fill_mio_first);
1728 if (badness < 0) {
1729 kfree(best_cfg);
1730 return badness;
1731 }
1732 debug_badness("==> lo_type=%d, wired=%d, mio=%d, badness=0x%x\n",
1733 cfg->line_out_type, fill_hardwired, fill_mio_first,
1734 badness);
Takashi Iwaia7694092013-01-21 10:43:18 +01001735 debug_show_configs(codec, cfg);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001736 if (badness < best_badness) {
1737 best_badness = badness;
1738 *best_cfg = *cfg;
1739 best_wired = fill_hardwired;
1740 best_mio = fill_mio_first;
1741 }
1742 if (!badness)
1743 break;
1744 fill_mio_first = !fill_mio_first;
1745 if (!fill_mio_first)
1746 continue;
1747 fill_hardwired = !fill_hardwired;
1748 if (!fill_hardwired)
1749 continue;
1750 if (hp_spk_swapped)
1751 break;
1752 hp_spk_swapped = true;
1753 if (cfg->speaker_outs > 0 &&
1754 cfg->line_out_type == AUTO_PIN_HP_OUT) {
1755 cfg->hp_outs = cfg->line_outs;
1756 memcpy(cfg->hp_pins, cfg->line_out_pins,
1757 sizeof(cfg->hp_pins));
1758 cfg->line_outs = cfg->speaker_outs;
1759 memcpy(cfg->line_out_pins, cfg->speaker_pins,
1760 sizeof(cfg->speaker_pins));
1761 cfg->speaker_outs = 0;
1762 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
1763 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
1764 fill_hardwired = true;
1765 continue;
1766 }
1767 if (cfg->hp_outs > 0 &&
1768 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
1769 cfg->speaker_outs = cfg->line_outs;
1770 memcpy(cfg->speaker_pins, cfg->line_out_pins,
1771 sizeof(cfg->speaker_pins));
1772 cfg->line_outs = cfg->hp_outs;
1773 memcpy(cfg->line_out_pins, cfg->hp_pins,
1774 sizeof(cfg->hp_pins));
1775 cfg->hp_outs = 0;
1776 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
1777 cfg->line_out_type = AUTO_PIN_HP_OUT;
1778 fill_hardwired = true;
1779 continue;
1780 }
1781 break;
1782 }
1783
1784 if (badness) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001785 debug_badness("==> restoring best_cfg\n");
Takashi Iwai352f7f92012-12-19 12:52:06 +01001786 *cfg = *best_cfg;
1787 fill_and_eval_dacs(codec, best_wired, best_mio);
1788 }
1789 debug_badness("==> Best config: lo_type=%d, wired=%d, mio=%d\n",
1790 cfg->line_out_type, best_wired, best_mio);
Takashi Iwaia7694092013-01-21 10:43:18 +01001791 debug_show_configs(codec, cfg);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001792
1793 if (cfg->line_out_pins[0]) {
1794 struct nid_path *path;
Takashi Iwai196c17662013-01-04 15:01:40 +01001795 path = snd_hda_get_path_from_idx(codec, spec->out_paths[0]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001796 if (path)
1797 spec->vmaster_nid = look_for_out_vol_nid(codec, path);
Takashi Iwai7a71bbf2013-01-17 10:25:15 +01001798 if (spec->vmaster_nid)
1799 snd_hda_set_vmaster_tlv(codec, spec->vmaster_nid,
1800 HDA_OUTPUT, spec->vmaster_tlv);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001801 }
1802
Takashi Iwai9314a582013-01-21 10:49:05 +01001803 /* set initial pinctl targets */
1804 if (spec->prefer_hp_amp || cfg->line_out_type == AUTO_PIN_HP_OUT)
1805 val = PIN_HP;
1806 else
1807 val = PIN_OUT;
1808 set_pin_targets(codec, cfg->line_outs, cfg->line_out_pins, val);
1809 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1810 set_pin_targets(codec, cfg->hp_outs, cfg->hp_pins, PIN_HP);
1811 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1812 val = spec->prefer_hp_amp ? PIN_HP : PIN_OUT;
1813 set_pin_targets(codec, cfg->speaker_outs,
1814 cfg->speaker_pins, val);
1815 }
1816
Takashi Iwai55a63d42013-03-21 17:20:12 +01001817 /* clear indep_hp flag if not available */
1818 if (spec->indep_hp && !indep_hp_possible(codec))
1819 spec->indep_hp = 0;
1820
Takashi Iwai352f7f92012-12-19 12:52:06 +01001821 kfree(best_cfg);
1822 return 0;
1823}
1824
1825/* add playback controls from the parsed DAC table */
1826static int create_multi_out_ctls(struct hda_codec *codec,
1827 const struct auto_pin_cfg *cfg)
1828{
1829 struct hda_gen_spec *spec = codec->spec;
1830 int i, err, noutputs;
1831
1832 noutputs = cfg->line_outs;
1833 if (spec->multi_ios > 0 && cfg->line_outs < 3)
1834 noutputs += spec->multi_ios;
1835
1836 for (i = 0; i < noutputs; i++) {
1837 const char *name;
1838 int index;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001839 struct nid_path *path;
1840
Takashi Iwai196c17662013-01-04 15:01:40 +01001841 path = snd_hda_get_path_from_idx(codec, spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001842 if (!path)
1843 continue;
Takashi Iwai247d85e2013-01-17 16:18:11 +01001844
1845 name = get_line_out_pfx(codec, i, &index, NID_PATH_VOL_CTL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001846 if (!name || !strcmp(name, "CLFE")) {
1847 /* Center/LFE */
1848 err = add_vol_ctl(codec, "Center", 0, 1, path);
1849 if (err < 0)
1850 return err;
1851 err = add_vol_ctl(codec, "LFE", 0, 2, path);
1852 if (err < 0)
1853 return err;
Takashi Iwai247d85e2013-01-17 16:18:11 +01001854 } else {
1855 err = add_stereo_vol(codec, name, index, path);
1856 if (err < 0)
1857 return err;
1858 }
1859
1860 name = get_line_out_pfx(codec, i, &index, NID_PATH_MUTE_CTL);
1861 if (!name || !strcmp(name, "CLFE")) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001862 err = add_sw_ctl(codec, "Center", 0, 1, path);
1863 if (err < 0)
1864 return err;
1865 err = add_sw_ctl(codec, "LFE", 0, 2, path);
1866 if (err < 0)
1867 return err;
1868 } else {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001869 err = add_stereo_sw(codec, name, index, path);
1870 if (err < 0)
1871 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 }
1873 }
1874 return 0;
1875}
1876
Takashi Iwaic2c80382013-01-07 10:33:57 +01001877static int create_extra_out(struct hda_codec *codec, int path_idx,
Takashi Iwai196c17662013-01-04 15:01:40 +01001878 const char *pfx, int cidx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879{
Takashi Iwai352f7f92012-12-19 12:52:06 +01001880 struct nid_path *path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 int err;
1882
Takashi Iwai196c17662013-01-04 15:01:40 +01001883 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001884 if (!path)
1885 return 0;
Takashi Iwaic2c80382013-01-07 10:33:57 +01001886 err = add_stereo_vol(codec, pfx, cidx, path);
1887 if (err < 0)
1888 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001889 err = add_stereo_sw(codec, pfx, cidx, path);
1890 if (err < 0)
1891 return err;
1892 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893}
1894
Takashi Iwai352f7f92012-12-19 12:52:06 +01001895/* add playback controls for speaker and HP outputs */
1896static int create_extra_outs(struct hda_codec *codec, int num_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001897 const int *paths, const char *pfx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898{
Takashi Iwaic2c80382013-01-07 10:33:57 +01001899 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001900
1901 for (i = 0; i < num_pins; i++) {
Takashi Iwaic2c80382013-01-07 10:33:57 +01001902 const char *name;
1903 char tmp[44];
1904 int err, idx = 0;
1905
1906 if (num_pins == 2 && i == 1 && !strcmp(pfx, "Speaker"))
1907 name = "Bass Speaker";
1908 else if (num_pins >= 3) {
1909 snprintf(tmp, sizeof(tmp), "%s %s",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001910 pfx, channel_name[i]);
Takashi Iwaic2c80382013-01-07 10:33:57 +01001911 name = tmp;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001912 } else {
Takashi Iwaic2c80382013-01-07 10:33:57 +01001913 name = pfx;
1914 idx = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 }
Takashi Iwaic2c80382013-01-07 10:33:57 +01001916 err = create_extra_out(codec, paths[i], name, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001917 if (err < 0)
1918 return err;
1919 }
1920 return 0;
1921}
Takashi Iwai97ec5582006-03-21 11:29:07 +01001922
Takashi Iwai352f7f92012-12-19 12:52:06 +01001923static int create_hp_out_ctls(struct hda_codec *codec)
1924{
1925 struct hda_gen_spec *spec = codec->spec;
1926 return create_extra_outs(codec, spec->autocfg.hp_outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001927 spec->hp_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001928 "Headphone");
1929}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930
Takashi Iwai352f7f92012-12-19 12:52:06 +01001931static int create_speaker_out_ctls(struct hda_codec *codec)
1932{
1933 struct hda_gen_spec *spec = codec->spec;
1934 return create_extra_outs(codec, spec->autocfg.speaker_outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001935 spec->speaker_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001936 "Speaker");
1937}
1938
1939/*
Takashi Iwai38cf6f12012-12-21 14:09:42 +01001940 * independent HP controls
1941 */
1942
Takashi Iwai8ba955c2013-03-07 18:40:58 +01001943/* update HP auto-mute state too */
1944static void update_hp_automute_hook(struct hda_codec *codec)
1945{
1946 struct hda_gen_spec *spec = codec->spec;
1947
1948 if (spec->hp_automute_hook)
1949 spec->hp_automute_hook(codec, NULL);
1950 else
1951 snd_hda_gen_hp_automute(codec, NULL);
1952}
1953
Takashi Iwai38cf6f12012-12-21 14:09:42 +01001954static int indep_hp_info(struct snd_kcontrol *kcontrol,
1955 struct snd_ctl_elem_info *uinfo)
1956{
1957 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
1958}
1959
1960static int indep_hp_get(struct snd_kcontrol *kcontrol,
1961 struct snd_ctl_elem_value *ucontrol)
1962{
1963 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1964 struct hda_gen_spec *spec = codec->spec;
1965 ucontrol->value.enumerated.item[0] = spec->indep_hp_enabled;
1966 return 0;
1967}
1968
Takashi Iwaia1e908e2013-01-21 15:11:25 +01001969static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
1970 int nomix_path_idx, int mix_path_idx,
1971 int out_type);
1972
Takashi Iwai38cf6f12012-12-21 14:09:42 +01001973static int indep_hp_put(struct snd_kcontrol *kcontrol,
1974 struct snd_ctl_elem_value *ucontrol)
1975{
1976 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1977 struct hda_gen_spec *spec = codec->spec;
1978 unsigned int select = ucontrol->value.enumerated.item[0];
1979 int ret = 0;
1980
1981 mutex_lock(&spec->pcm_mutex);
1982 if (spec->active_streams) {
1983 ret = -EBUSY;
1984 goto unlock;
1985 }
1986
1987 if (spec->indep_hp_enabled != select) {
Takashi Iwaia1e908e2013-01-21 15:11:25 +01001988 hda_nid_t *dacp;
1989 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
1990 dacp = &spec->private_dac_nids[0];
1991 else
1992 dacp = &spec->multiout.hp_out_nid[0];
1993
1994 /* update HP aamix paths in case it conflicts with indep HP */
1995 if (spec->have_aamix_ctl) {
1996 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
1997 update_aamix_paths(codec, spec->aamix_mode,
1998 spec->out_paths[0],
1999 spec->aamix_out_paths[0],
2000 spec->autocfg.line_out_type);
2001 else
2002 update_aamix_paths(codec, spec->aamix_mode,
2003 spec->hp_paths[0],
2004 spec->aamix_out_paths[1],
2005 AUTO_PIN_HP_OUT);
2006 }
2007
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002008 spec->indep_hp_enabled = select;
2009 if (spec->indep_hp_enabled)
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002010 *dacp = 0;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002011 else
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002012 *dacp = spec->alt_dac_nid;
Takashi Iwai92603c52013-01-22 07:46:31 +01002013
Takashi Iwai8ba955c2013-03-07 18:40:58 +01002014 update_hp_automute_hook(codec);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002015 ret = 1;
2016 }
2017 unlock:
2018 mutex_unlock(&spec->pcm_mutex);
2019 return ret;
2020}
2021
2022static const struct snd_kcontrol_new indep_hp_ctl = {
2023 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2024 .name = "Independent HP",
2025 .info = indep_hp_info,
2026 .get = indep_hp_get,
2027 .put = indep_hp_put,
2028};
2029
2030
2031static int create_indep_hp_ctls(struct hda_codec *codec)
2032{
2033 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002034 hda_nid_t dac;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002035
2036 if (!spec->indep_hp)
2037 return 0;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002038 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
2039 dac = spec->multiout.dac_nids[0];
2040 else
2041 dac = spec->multiout.hp_out_nid[0];
2042 if (!dac) {
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002043 spec->indep_hp = 0;
2044 return 0;
2045 }
2046
2047 spec->indep_hp_enabled = false;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002048 spec->alt_dac_nid = dac;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002049 if (!snd_hda_gen_add_kctl(spec, NULL, &indep_hp_ctl))
2050 return -ENOMEM;
2051 return 0;
2052}
2053
2054/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01002055 * channel mode enum control
2056 */
2057
2058static int ch_mode_info(struct snd_kcontrol *kcontrol,
2059 struct snd_ctl_elem_info *uinfo)
2060{
2061 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2062 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002063 int chs;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002064
2065 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2066 uinfo->count = 1;
2067 uinfo->value.enumerated.items = spec->multi_ios + 1;
2068 if (uinfo->value.enumerated.item > spec->multi_ios)
2069 uinfo->value.enumerated.item = spec->multi_ios;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002070 chs = uinfo->value.enumerated.item * 2 + spec->min_channel_count;
2071 sprintf(uinfo->value.enumerated.name, "%dch", chs);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002072 return 0;
2073}
2074
2075static int ch_mode_get(struct snd_kcontrol *kcontrol,
2076 struct snd_ctl_elem_value *ucontrol)
2077{
2078 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2079 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002080 ucontrol->value.enumerated.item[0] =
2081 (spec->ext_channel_count - spec->min_channel_count) / 2;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002082 return 0;
2083}
2084
Takashi Iwai196c17662013-01-04 15:01:40 +01002085static inline struct nid_path *
2086get_multiio_path(struct hda_codec *codec, int idx)
2087{
2088 struct hda_gen_spec *spec = codec->spec;
2089 return snd_hda_get_path_from_idx(codec,
2090 spec->out_paths[spec->autocfg.line_outs + idx]);
2091}
2092
Takashi Iwaia5cc2502013-01-16 18:08:55 +01002093static void update_automute_all(struct hda_codec *codec);
2094
Takashi Iwai65033cc2013-04-16 12:31:05 +02002095/* Default value to be passed as aamix argument for snd_hda_activate_path();
2096 * used for output paths
2097 */
2098static bool aamix_default(struct hda_gen_spec *spec)
2099{
2100 return !spec->have_aamix_ctl || spec->aamix_mode;
2101}
2102
Takashi Iwai352f7f92012-12-19 12:52:06 +01002103static int set_multi_io(struct hda_codec *codec, int idx, bool output)
2104{
2105 struct hda_gen_spec *spec = codec->spec;
2106 hda_nid_t nid = spec->multi_io[idx].pin;
2107 struct nid_path *path;
2108
Takashi Iwai196c17662013-01-04 15:01:40 +01002109 path = get_multiio_path(codec, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002110 if (!path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 return -EINVAL;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002112
2113 if (path->active == output)
2114 return 0;
2115
2116 if (output) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01002117 set_pin_target(codec, nid, PIN_OUT, true);
Takashi Iwai65033cc2013-04-16 12:31:05 +02002118 snd_hda_activate_path(codec, path, true, aamix_default(spec));
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01002119 set_pin_eapd(codec, nid, true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002120 } else {
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01002121 set_pin_eapd(codec, nid, false);
Takashi Iwai65033cc2013-04-16 12:31:05 +02002122 snd_hda_activate_path(codec, path, false, aamix_default(spec));
Takashi Iwai2c12c302013-01-10 09:33:29 +01002123 set_pin_target(codec, nid, spec->multi_io[idx].ctl_in, true);
Takashi Iwai55196ff2013-01-24 17:32:56 +01002124 path_power_down_sync(codec, path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 }
Takashi Iwaia365fed2013-01-10 16:10:06 +01002126
2127 /* update jack retasking in case it modifies any of them */
Takashi Iwaia5cc2502013-01-16 18:08:55 +01002128 update_automute_all(codec);
Takashi Iwaia365fed2013-01-10 16:10:06 +01002129
Takashi Iwai352f7f92012-12-19 12:52:06 +01002130 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131}
2132
Takashi Iwai352f7f92012-12-19 12:52:06 +01002133static int ch_mode_put(struct snd_kcontrol *kcontrol,
2134 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002136 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2137 struct hda_gen_spec *spec = codec->spec;
2138 int i, ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139
Takashi Iwai352f7f92012-12-19 12:52:06 +01002140 ch = ucontrol->value.enumerated.item[0];
2141 if (ch < 0 || ch > spec->multi_ios)
2142 return -EINVAL;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002143 if (ch == (spec->ext_channel_count - spec->min_channel_count) / 2)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002144 return 0;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002145 spec->ext_channel_count = ch * 2 + spec->min_channel_count;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002146 for (i = 0; i < spec->multi_ios; i++)
2147 set_multi_io(codec, i, i < ch);
2148 spec->multiout.max_channels = max(spec->ext_channel_count,
2149 spec->const_channel_count);
2150 if (spec->need_dac_fix)
2151 spec->multiout.num_dacs = spec->multiout.max_channels / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 return 1;
2153}
2154
Takashi Iwai352f7f92012-12-19 12:52:06 +01002155static const struct snd_kcontrol_new channel_mode_enum = {
2156 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2157 .name = "Channel Mode",
2158 .info = ch_mode_info,
2159 .get = ch_mode_get,
2160 .put = ch_mode_put,
2161};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162
Takashi Iwai352f7f92012-12-19 12:52:06 +01002163static int create_multi_channel_mode(struct hda_codec *codec)
2164{
2165 struct hda_gen_spec *spec = codec->spec;
2166
2167 if (spec->multi_ios > 0) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01002168 if (!snd_hda_gen_add_kctl(spec, NULL, &channel_mode_enum))
Takashi Iwai352f7f92012-12-19 12:52:06 +01002169 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 return 0;
2172}
2173
Takashi Iwai352f7f92012-12-19 12:52:06 +01002174/*
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002175 * aamix loopback enable/disable switch
2176 */
2177
2178#define loopback_mixing_info indep_hp_info
2179
2180static int loopback_mixing_get(struct snd_kcontrol *kcontrol,
2181 struct snd_ctl_elem_value *ucontrol)
2182{
2183 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2184 struct hda_gen_spec *spec = codec->spec;
2185 ucontrol->value.enumerated.item[0] = spec->aamix_mode;
2186 return 0;
2187}
2188
2189static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002190 int nomix_path_idx, int mix_path_idx,
2191 int out_type)
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002192{
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002193 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002194 struct nid_path *nomix_path, *mix_path;
2195
2196 nomix_path = snd_hda_get_path_from_idx(codec, nomix_path_idx);
2197 mix_path = snd_hda_get_path_from_idx(codec, mix_path_idx);
2198 if (!nomix_path || !mix_path)
2199 return;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002200
2201 /* if HP aamix path is driven from a different DAC and the
2202 * independent HP mode is ON, can't turn on aamix path
2203 */
2204 if (out_type == AUTO_PIN_HP_OUT && spec->indep_hp_enabled &&
2205 mix_path->path[0] != spec->alt_dac_nid)
2206 do_mix = false;
2207
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002208 if (do_mix) {
2209 snd_hda_activate_path(codec, nomix_path, false, true);
2210 snd_hda_activate_path(codec, mix_path, true, true);
Takashi Iwai55196ff2013-01-24 17:32:56 +01002211 path_power_down_sync(codec, nomix_path);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002212 } else {
Takashi Iwai65033cc2013-04-16 12:31:05 +02002213 snd_hda_activate_path(codec, mix_path, false, false);
2214 snd_hda_activate_path(codec, nomix_path, true, false);
Takashi Iwai55196ff2013-01-24 17:32:56 +01002215 path_power_down_sync(codec, mix_path);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002216 }
2217}
2218
2219static int loopback_mixing_put(struct snd_kcontrol *kcontrol,
2220 struct snd_ctl_elem_value *ucontrol)
2221{
2222 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2223 struct hda_gen_spec *spec = codec->spec;
2224 unsigned int val = ucontrol->value.enumerated.item[0];
2225
2226 if (val == spec->aamix_mode)
2227 return 0;
2228 spec->aamix_mode = val;
2229 update_aamix_paths(codec, val, spec->out_paths[0],
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002230 spec->aamix_out_paths[0],
2231 spec->autocfg.line_out_type);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002232 update_aamix_paths(codec, val, spec->hp_paths[0],
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002233 spec->aamix_out_paths[1],
2234 AUTO_PIN_HP_OUT);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002235 update_aamix_paths(codec, val, spec->speaker_paths[0],
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002236 spec->aamix_out_paths[2],
2237 AUTO_PIN_SPEAKER_OUT);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002238 return 1;
2239}
2240
2241static const struct snd_kcontrol_new loopback_mixing_enum = {
2242 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2243 .name = "Loopback Mixing",
2244 .info = loopback_mixing_info,
2245 .get = loopback_mixing_get,
2246 .put = loopback_mixing_put,
2247};
2248
2249static int create_loopback_mixing_ctl(struct hda_codec *codec)
2250{
2251 struct hda_gen_spec *spec = codec->spec;
2252
2253 if (!spec->mixer_nid)
2254 return 0;
2255 if (!(spec->aamix_out_paths[0] || spec->aamix_out_paths[1] ||
2256 spec->aamix_out_paths[2]))
2257 return 0;
2258 if (!snd_hda_gen_add_kctl(spec, NULL, &loopback_mixing_enum))
2259 return -ENOMEM;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002260 spec->have_aamix_ctl = 1;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002261 return 0;
2262}
2263
2264/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01002265 * shared headphone/mic handling
2266 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02002267
Takashi Iwai352f7f92012-12-19 12:52:06 +01002268static void call_update_outputs(struct hda_codec *codec);
2269
2270/* for shared I/O, change the pin-control accordingly */
Takashi Iwai967303d2013-02-19 17:12:42 +01002271static void update_hp_mic(struct hda_codec *codec, int adc_mux, bool force)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002272{
2273 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai967303d2013-02-19 17:12:42 +01002274 bool as_mic;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002275 unsigned int val;
Takashi Iwai967303d2013-02-19 17:12:42 +01002276 hda_nid_t pin;
2277
2278 pin = spec->hp_mic_pin;
2279 as_mic = spec->cur_mux[adc_mux] == spec->hp_mic_mux_idx;
2280
2281 if (!force) {
2282 val = snd_hda_codec_get_pin_target(codec, pin);
2283 if (as_mic) {
2284 if (val & PIN_IN)
2285 return;
2286 } else {
2287 if (val & PIN_OUT)
2288 return;
2289 }
2290 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002291
2292 val = snd_hda_get_default_vref(codec, pin);
Takashi Iwai967303d2013-02-19 17:12:42 +01002293 /* if the HP pin doesn't support VREF and the codec driver gives an
2294 * alternative pin, set up the VREF on that pin instead
2295 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01002296 if (val == AC_PINCTL_VREF_HIZ && spec->shared_mic_vref_pin) {
2297 const hda_nid_t vref_pin = spec->shared_mic_vref_pin;
2298 unsigned int vref_val = snd_hda_get_default_vref(codec, vref_pin);
2299 if (vref_val != AC_PINCTL_VREF_HIZ)
Takashi Iwai7594aa32012-12-20 15:38:40 +01002300 snd_hda_set_pin_ctl_cache(codec, vref_pin,
Takashi Iwai967303d2013-02-19 17:12:42 +01002301 PIN_IN | (as_mic ? vref_val : 0));
Takashi Iwaicb53c622007-08-10 17:21:45 +02002302 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002303
Takashi Iwai8ba955c2013-03-07 18:40:58 +01002304 if (!spec->hp_mic_jack_modes) {
2305 if (as_mic)
2306 val |= PIN_IN;
2307 else
2308 val = PIN_HP;
2309 set_pin_target(codec, pin, val, true);
2310 update_hp_automute_hook(codec);
2311 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002312}
2313
2314/* create a shared input with the headphone out */
Takashi Iwai967303d2013-02-19 17:12:42 +01002315static int create_hp_mic(struct hda_codec *codec)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002316{
2317 struct hda_gen_spec *spec = codec->spec;
2318 struct auto_pin_cfg *cfg = &spec->autocfg;
2319 unsigned int defcfg;
2320 hda_nid_t nid;
2321
Takashi Iwai967303d2013-02-19 17:12:42 +01002322 if (!spec->hp_mic) {
2323 if (spec->suppress_hp_mic_detect)
2324 return 0;
2325 /* automatic detection: only if no input or a single internal
2326 * input pin is found, try to detect the shared hp/mic
2327 */
2328 if (cfg->num_inputs > 1)
2329 return 0;
2330 else if (cfg->num_inputs == 1) {
2331 defcfg = snd_hda_codec_get_pincfg(codec, cfg->inputs[0].pin);
2332 if (snd_hda_get_input_pin_attr(defcfg) != INPUT_PIN_ATTR_INT)
2333 return 0;
2334 }
2335 }
2336
2337 spec->hp_mic = 0; /* clear once */
2338 if (cfg->num_inputs >= AUTO_CFG_MAX_INS)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002339 return 0;
2340
Takashi Iwai967303d2013-02-19 17:12:42 +01002341 nid = 0;
2342 if (cfg->line_out_type == AUTO_PIN_HP_OUT && cfg->line_outs > 0)
2343 nid = cfg->line_out_pins[0];
2344 else if (cfg->hp_outs > 0)
2345 nid = cfg->hp_pins[0];
2346 if (!nid)
2347 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002348
2349 if (!(snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_IN))
2350 return 0; /* no input */
2351
Takashi Iwai967303d2013-02-19 17:12:42 +01002352 cfg->inputs[cfg->num_inputs].pin = nid;
2353 cfg->inputs[cfg->num_inputs].type = AUTO_PIN_MIC;
David Henningssoncb420b12013-04-11 11:30:28 +02002354 cfg->inputs[cfg->num_inputs].is_headphone_mic = 1;
Takashi Iwai967303d2013-02-19 17:12:42 +01002355 cfg->num_inputs++;
2356 spec->hp_mic = 1;
2357 spec->hp_mic_pin = nid;
2358 /* we can't handle auto-mic together with HP-mic */
2359 spec->suppress_auto_mic = 1;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002360 snd_printdd("hda-codec: Enable shared I/O jack on NID 0x%x\n", nid);
2361 return 0;
2362}
2363
Takashi Iwai978e77e2013-01-10 16:57:58 +01002364/*
2365 * output jack mode
2366 */
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002367
2368static int create_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t pin);
2369
2370static const char * const out_jack_texts[] = {
2371 "Line Out", "Headphone Out",
2372};
2373
Takashi Iwai978e77e2013-01-10 16:57:58 +01002374static int out_jack_mode_info(struct snd_kcontrol *kcontrol,
2375 struct snd_ctl_elem_info *uinfo)
2376{
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002377 return snd_hda_enum_helper_info(kcontrol, uinfo, 2, out_jack_texts);
Takashi Iwai978e77e2013-01-10 16:57:58 +01002378}
2379
2380static int out_jack_mode_get(struct snd_kcontrol *kcontrol,
2381 struct snd_ctl_elem_value *ucontrol)
2382{
2383 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2384 hda_nid_t nid = kcontrol->private_value;
2385 if (snd_hda_codec_get_pin_target(codec, nid) == PIN_HP)
2386 ucontrol->value.enumerated.item[0] = 1;
2387 else
2388 ucontrol->value.enumerated.item[0] = 0;
2389 return 0;
2390}
2391
2392static int out_jack_mode_put(struct snd_kcontrol *kcontrol,
2393 struct snd_ctl_elem_value *ucontrol)
2394{
2395 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2396 hda_nid_t nid = kcontrol->private_value;
2397 unsigned int val;
2398
2399 val = ucontrol->value.enumerated.item[0] ? PIN_HP : PIN_OUT;
2400 if (snd_hda_codec_get_pin_target(codec, nid) == val)
2401 return 0;
2402 snd_hda_set_pin_ctl_cache(codec, nid, val);
2403 return 1;
2404}
2405
2406static const struct snd_kcontrol_new out_jack_mode_enum = {
2407 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2408 .info = out_jack_mode_info,
2409 .get = out_jack_mode_get,
2410 .put = out_jack_mode_put,
2411};
2412
2413static bool find_kctl_name(struct hda_codec *codec, const char *name, int idx)
2414{
2415 struct hda_gen_spec *spec = codec->spec;
2416 int i;
2417
2418 for (i = 0; i < spec->kctls.used; i++) {
2419 struct snd_kcontrol_new *kctl = snd_array_elem(&spec->kctls, i);
2420 if (!strcmp(kctl->name, name) && kctl->index == idx)
2421 return true;
2422 }
2423 return false;
2424}
2425
2426static void get_jack_mode_name(struct hda_codec *codec, hda_nid_t pin,
2427 char *name, size_t name_len)
2428{
2429 struct hda_gen_spec *spec = codec->spec;
2430 int idx = 0;
2431
2432 snd_hda_get_pin_label(codec, pin, &spec->autocfg, name, name_len, &idx);
2433 strlcat(name, " Jack Mode", name_len);
2434
2435 for (; find_kctl_name(codec, name, idx); idx++)
2436 ;
2437}
2438
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002439static int get_out_jack_num_items(struct hda_codec *codec, hda_nid_t pin)
2440{
2441 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaif811c3c2013-03-07 18:32:59 +01002442 if (spec->add_jack_modes) {
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002443 unsigned int pincap = snd_hda_query_pin_caps(codec, pin);
2444 if ((pincap & AC_PINCAP_OUT) && (pincap & AC_PINCAP_HP_DRV))
2445 return 2;
2446 }
2447 return 1;
2448}
2449
Takashi Iwai978e77e2013-01-10 16:57:58 +01002450static int create_out_jack_modes(struct hda_codec *codec, int num_pins,
2451 hda_nid_t *pins)
2452{
2453 struct hda_gen_spec *spec = codec->spec;
2454 int i;
2455
2456 for (i = 0; i < num_pins; i++) {
2457 hda_nid_t pin = pins[i];
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002458 if (pin == spec->hp_mic_pin) {
2459 int ret = create_hp_mic_jack_mode(codec, pin);
2460 if (ret < 0)
2461 return ret;
2462 continue;
2463 }
2464 if (get_out_jack_num_items(codec, pin) > 1) {
Takashi Iwai978e77e2013-01-10 16:57:58 +01002465 struct snd_kcontrol_new *knew;
2466 char name[44];
2467 get_jack_mode_name(codec, pin, name, sizeof(name));
2468 knew = snd_hda_gen_add_kctl(spec, name,
2469 &out_jack_mode_enum);
2470 if (!knew)
2471 return -ENOMEM;
2472 knew->private_value = pin;
2473 }
2474 }
2475
2476 return 0;
2477}
2478
Takashi Iwai294765582013-01-17 09:52:11 +01002479/*
2480 * input jack mode
2481 */
2482
2483/* from AC_PINCTL_VREF_HIZ to AC_PINCTL_VREF_100 */
2484#define NUM_VREFS 6
2485
2486static const char * const vref_texts[NUM_VREFS] = {
2487 "Line In", "Mic 50pc Bias", "Mic 0V Bias",
2488 "", "Mic 80pc Bias", "Mic 100pc Bias"
2489};
2490
2491static unsigned int get_vref_caps(struct hda_codec *codec, hda_nid_t pin)
2492{
2493 unsigned int pincap;
2494
2495 pincap = snd_hda_query_pin_caps(codec, pin);
2496 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
2497 /* filter out unusual vrefs */
2498 pincap &= ~(AC_PINCAP_VREF_GRD | AC_PINCAP_VREF_100);
2499 return pincap;
2500}
2501
2502/* convert from the enum item index to the vref ctl index (0=HIZ, 1=50%...) */
2503static int get_vref_idx(unsigned int vref_caps, unsigned int item_idx)
2504{
2505 unsigned int i, n = 0;
2506
2507 for (i = 0; i < NUM_VREFS; i++) {
2508 if (vref_caps & (1 << i)) {
2509 if (n == item_idx)
2510 return i;
2511 n++;
2512 }
2513 }
2514 return 0;
2515}
2516
2517/* convert back from the vref ctl index to the enum item index */
2518static int cvt_from_vref_idx(unsigned int vref_caps, unsigned int idx)
2519{
2520 unsigned int i, n = 0;
2521
2522 for (i = 0; i < NUM_VREFS; i++) {
2523 if (i == idx)
2524 return n;
2525 if (vref_caps & (1 << i))
2526 n++;
2527 }
2528 return 0;
2529}
2530
2531static int in_jack_mode_info(struct snd_kcontrol *kcontrol,
2532 struct snd_ctl_elem_info *uinfo)
2533{
2534 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2535 hda_nid_t nid = kcontrol->private_value;
2536 unsigned int vref_caps = get_vref_caps(codec, nid);
2537
2538 snd_hda_enum_helper_info(kcontrol, uinfo, hweight32(vref_caps),
2539 vref_texts);
2540 /* set the right text */
2541 strcpy(uinfo->value.enumerated.name,
2542 vref_texts[get_vref_idx(vref_caps, uinfo->value.enumerated.item)]);
2543 return 0;
2544}
2545
2546static int in_jack_mode_get(struct snd_kcontrol *kcontrol,
2547 struct snd_ctl_elem_value *ucontrol)
2548{
2549 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2550 hda_nid_t nid = kcontrol->private_value;
2551 unsigned int vref_caps = get_vref_caps(codec, nid);
2552 unsigned int idx;
2553
2554 idx = snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_VREFEN;
2555 ucontrol->value.enumerated.item[0] = cvt_from_vref_idx(vref_caps, idx);
2556 return 0;
2557}
2558
2559static int in_jack_mode_put(struct snd_kcontrol *kcontrol,
2560 struct snd_ctl_elem_value *ucontrol)
2561{
2562 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2563 hda_nid_t nid = kcontrol->private_value;
2564 unsigned int vref_caps = get_vref_caps(codec, nid);
2565 unsigned int val, idx;
2566
2567 val = snd_hda_codec_get_pin_target(codec, nid);
2568 idx = cvt_from_vref_idx(vref_caps, val & AC_PINCTL_VREFEN);
2569 if (idx == ucontrol->value.enumerated.item[0])
2570 return 0;
2571
2572 val &= ~AC_PINCTL_VREFEN;
2573 val |= get_vref_idx(vref_caps, ucontrol->value.enumerated.item[0]);
2574 snd_hda_set_pin_ctl_cache(codec, nid, val);
2575 return 1;
2576}
2577
2578static const struct snd_kcontrol_new in_jack_mode_enum = {
2579 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2580 .info = in_jack_mode_info,
2581 .get = in_jack_mode_get,
2582 .put = in_jack_mode_put,
2583};
2584
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002585static int get_in_jack_num_items(struct hda_codec *codec, hda_nid_t pin)
2586{
2587 struct hda_gen_spec *spec = codec->spec;
2588 int nitems = 0;
Takashi Iwaif811c3c2013-03-07 18:32:59 +01002589 if (spec->add_jack_modes)
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002590 nitems = hweight32(get_vref_caps(codec, pin));
2591 return nitems ? nitems : 1;
2592}
2593
Takashi Iwai294765582013-01-17 09:52:11 +01002594static int create_in_jack_mode(struct hda_codec *codec, hda_nid_t pin)
2595{
2596 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai294765582013-01-17 09:52:11 +01002597 struct snd_kcontrol_new *knew;
2598 char name[44];
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002599 unsigned int defcfg;
2600
Takashi Iwaif811c3c2013-03-07 18:32:59 +01002601 if (pin == spec->hp_mic_pin)
2602 return 0; /* already done in create_out_jack_mode() */
Takashi Iwai294765582013-01-17 09:52:11 +01002603
2604 /* no jack mode for fixed pins */
2605 defcfg = snd_hda_codec_get_pincfg(codec, pin);
2606 if (snd_hda_get_input_pin_attr(defcfg) == INPUT_PIN_ATTR_INT)
2607 return 0;
2608
2609 /* no multiple vref caps? */
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002610 if (get_in_jack_num_items(codec, pin) <= 1)
Takashi Iwai294765582013-01-17 09:52:11 +01002611 return 0;
2612
2613 get_jack_mode_name(codec, pin, name, sizeof(name));
2614 knew = snd_hda_gen_add_kctl(spec, name, &in_jack_mode_enum);
2615 if (!knew)
2616 return -ENOMEM;
2617 knew->private_value = pin;
2618 return 0;
2619}
2620
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002621/*
2622 * HP/mic shared jack mode
2623 */
2624static int hp_mic_jack_mode_info(struct snd_kcontrol *kcontrol,
2625 struct snd_ctl_elem_info *uinfo)
2626{
2627 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2628 hda_nid_t nid = kcontrol->private_value;
2629 int out_jacks = get_out_jack_num_items(codec, nid);
2630 int in_jacks = get_in_jack_num_items(codec, nid);
2631 const char *text = NULL;
2632 int idx;
2633
2634 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2635 uinfo->count = 1;
2636 uinfo->value.enumerated.items = out_jacks + in_jacks;
2637 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2638 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
2639 idx = uinfo->value.enumerated.item;
2640 if (idx < out_jacks) {
2641 if (out_jacks > 1)
2642 text = out_jack_texts[idx];
2643 else
2644 text = "Headphone Out";
2645 } else {
2646 idx -= out_jacks;
2647 if (in_jacks > 1) {
2648 unsigned int vref_caps = get_vref_caps(codec, nid);
2649 text = vref_texts[get_vref_idx(vref_caps, idx)];
2650 } else
2651 text = "Mic In";
2652 }
2653
2654 strcpy(uinfo->value.enumerated.name, text);
2655 return 0;
2656}
2657
2658static int get_cur_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t nid)
2659{
2660 int out_jacks = get_out_jack_num_items(codec, nid);
2661 int in_jacks = get_in_jack_num_items(codec, nid);
2662 unsigned int val = snd_hda_codec_get_pin_target(codec, nid);
2663 int idx = 0;
2664
2665 if (val & PIN_OUT) {
2666 if (out_jacks > 1 && val == PIN_HP)
2667 idx = 1;
2668 } else if (val & PIN_IN) {
2669 idx = out_jacks;
2670 if (in_jacks > 1) {
2671 unsigned int vref_caps = get_vref_caps(codec, nid);
2672 val &= AC_PINCTL_VREFEN;
2673 idx += cvt_from_vref_idx(vref_caps, val);
2674 }
2675 }
2676 return idx;
2677}
2678
2679static int hp_mic_jack_mode_get(struct snd_kcontrol *kcontrol,
2680 struct snd_ctl_elem_value *ucontrol)
2681{
2682 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2683 hda_nid_t nid = kcontrol->private_value;
2684 ucontrol->value.enumerated.item[0] =
2685 get_cur_hp_mic_jack_mode(codec, nid);
2686 return 0;
2687}
2688
2689static int hp_mic_jack_mode_put(struct snd_kcontrol *kcontrol,
2690 struct snd_ctl_elem_value *ucontrol)
2691{
2692 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2693 hda_nid_t nid = kcontrol->private_value;
2694 int out_jacks = get_out_jack_num_items(codec, nid);
2695 int in_jacks = get_in_jack_num_items(codec, nid);
2696 unsigned int val, oldval, idx;
2697
2698 oldval = get_cur_hp_mic_jack_mode(codec, nid);
2699 idx = ucontrol->value.enumerated.item[0];
2700 if (oldval == idx)
2701 return 0;
2702
2703 if (idx < out_jacks) {
2704 if (out_jacks > 1)
2705 val = idx ? PIN_HP : PIN_OUT;
2706 else
2707 val = PIN_HP;
2708 } else {
2709 idx -= out_jacks;
2710 if (in_jacks > 1) {
2711 unsigned int vref_caps = get_vref_caps(codec, nid);
2712 val = snd_hda_codec_get_pin_target(codec, nid);
Takashi Iwai3f550e32013-03-07 18:30:27 +01002713 val &= ~(AC_PINCTL_VREFEN | PIN_HP);
2714 val |= get_vref_idx(vref_caps, idx) | PIN_IN;
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002715 } else
2716 val = snd_hda_get_default_vref(codec, nid);
2717 }
2718 snd_hda_set_pin_ctl_cache(codec, nid, val);
Takashi Iwai8ba955c2013-03-07 18:40:58 +01002719 update_hp_automute_hook(codec);
2720
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002721 return 1;
2722}
2723
2724static const struct snd_kcontrol_new hp_mic_jack_mode_enum = {
2725 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2726 .info = hp_mic_jack_mode_info,
2727 .get = hp_mic_jack_mode_get,
2728 .put = hp_mic_jack_mode_put,
2729};
2730
2731static int create_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t pin)
2732{
2733 struct hda_gen_spec *spec = codec->spec;
2734 struct snd_kcontrol_new *knew;
2735
2736 if (get_out_jack_num_items(codec, pin) <= 1 &&
2737 get_in_jack_num_items(codec, pin) <= 1)
2738 return 0; /* no need */
2739 knew = snd_hda_gen_add_kctl(spec, "Headphone Mic Jack Mode",
2740 &hp_mic_jack_mode_enum);
2741 if (!knew)
2742 return -ENOMEM;
2743 knew->private_value = pin;
Takashi Iwai8ba955c2013-03-07 18:40:58 +01002744 spec->hp_mic_jack_modes = 1;
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002745 return 0;
2746}
Takashi Iwai352f7f92012-12-19 12:52:06 +01002747
2748/*
2749 * Parse input paths
2750 */
2751
Takashi Iwai352f7f92012-12-19 12:52:06 +01002752/* add the powersave loopback-list entry */
Takashi Iwai0186f4f2013-02-07 10:45:11 +01002753static int add_loopback_list(struct hda_gen_spec *spec, hda_nid_t mix, int idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002754{
2755 struct hda_amp_list *list;
2756
Takashi Iwai0186f4f2013-02-07 10:45:11 +01002757 list = snd_array_new(&spec->loopback_list);
2758 if (!list)
2759 return -ENOMEM;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002760 list->nid = mix;
2761 list->dir = HDA_INPUT;
2762 list->idx = idx;
Takashi Iwai0186f4f2013-02-07 10:45:11 +01002763 spec->loopback.amplist = spec->loopback_list.list;
2764 return 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02002765}
Takashi Iwaicb53c622007-08-10 17:21:45 +02002766
Takashi Iwai352f7f92012-12-19 12:52:06 +01002767/* create input playback/capture controls for the given pin */
Takashi Iwai196c17662013-01-04 15:01:40 +01002768static int new_analog_input(struct hda_codec *codec, int input_idx,
2769 hda_nid_t pin, const char *ctlname, int ctlidx,
Takashi Iwai352f7f92012-12-19 12:52:06 +01002770 hda_nid_t mix_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002772 struct hda_gen_spec *spec = codec->spec;
2773 struct nid_path *path;
2774 unsigned int val;
2775 int err, idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776
Takashi Iwai352f7f92012-12-19 12:52:06 +01002777 if (!nid_has_volume(codec, mix_nid, HDA_INPUT) &&
2778 !nid_has_mute(codec, mix_nid, HDA_INPUT))
2779 return 0; /* no need for analog loopback */
2780
Takashi Iwai3ca529d2013-01-07 17:25:08 +01002781 path = snd_hda_add_new_path(codec, pin, mix_nid, 0);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002782 if (!path)
2783 return -EINVAL;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01002784 print_nid_path("loopback", path);
Takashi Iwai196c17662013-01-04 15:01:40 +01002785 spec->loopback_paths[input_idx] = snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002786
2787 idx = path->idx[path->depth - 1];
2788 if (nid_has_volume(codec, mix_nid, HDA_INPUT)) {
2789 val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
2790 err = __add_pb_vol_ctrl(spec, HDA_CTL_WIDGET_VOL, ctlname, ctlidx, val);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002791 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002793 path->ctls[NID_PATH_VOL_CTL] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794 }
2795
Takashi Iwai352f7f92012-12-19 12:52:06 +01002796 if (nid_has_mute(codec, mix_nid, HDA_INPUT)) {
2797 val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
2798 err = __add_pb_sw_ctrl(spec, HDA_CTL_WIDGET_MUTE, ctlname, ctlidx, val);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002799 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002801 path->ctls[NID_PATH_MUTE_CTL] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802 }
2803
Takashi Iwai352f7f92012-12-19 12:52:06 +01002804 path->active = true;
Takashi Iwai0186f4f2013-02-07 10:45:11 +01002805 err = add_loopback_list(spec, mix_nid, idx);
2806 if (err < 0)
2807 return err;
Takashi Iwaie4a395e2013-01-23 17:00:31 +01002808
2809 if (spec->mixer_nid != spec->mixer_merge_nid &&
2810 !spec->loopback_merge_path) {
2811 path = snd_hda_add_new_path(codec, spec->mixer_nid,
2812 spec->mixer_merge_nid, 0);
2813 if (path) {
2814 print_nid_path("loopback-merge", path);
2815 path->active = true;
2816 spec->loopback_merge_path =
2817 snd_hda_get_path_idx(codec, path);
2818 }
2819 }
2820
Takashi Iwai352f7f92012-12-19 12:52:06 +01002821 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822}
2823
Takashi Iwai352f7f92012-12-19 12:52:06 +01002824static int is_input_pin(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002826 unsigned int pincap = snd_hda_query_pin_caps(codec, nid);
2827 return (pincap & AC_PINCAP_IN) != 0;
2828}
2829
2830/* Parse the codec tree and retrieve ADCs */
2831static int fill_adc_nids(struct hda_codec *codec)
2832{
2833 struct hda_gen_spec *spec = codec->spec;
2834 hda_nid_t nid;
2835 hda_nid_t *adc_nids = spec->adc_nids;
2836 int max_nums = ARRAY_SIZE(spec->adc_nids);
2837 int i, nums = 0;
2838
2839 nid = codec->start_nid;
2840 for (i = 0; i < codec->num_nodes; i++, nid++) {
2841 unsigned int caps = get_wcaps(codec, nid);
2842 int type = get_wcaps_type(caps);
2843
2844 if (type != AC_WID_AUD_IN || (caps & AC_WCAP_DIGITAL))
2845 continue;
2846 adc_nids[nums] = nid;
2847 if (++nums >= max_nums)
2848 break;
2849 }
2850 spec->num_adc_nids = nums;
Takashi Iwai0ffd5342013-01-17 15:53:29 +01002851
2852 /* copy the detected ADCs to all_adcs[] */
2853 spec->num_all_adcs = nums;
2854 memcpy(spec->all_adcs, spec->adc_nids, nums * sizeof(hda_nid_t));
2855
Takashi Iwai352f7f92012-12-19 12:52:06 +01002856 return nums;
2857}
2858
2859/* filter out invalid adc_nids that don't give all active input pins;
2860 * if needed, check whether dynamic ADC-switching is available
2861 */
2862static int check_dyn_adc_switch(struct hda_codec *codec)
2863{
2864 struct hda_gen_spec *spec = codec->spec;
2865 struct hda_input_mux *imux = &spec->input_mux;
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002866 unsigned int ok_bits;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002867 int i, n, nums;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002868
Takashi Iwai352f7f92012-12-19 12:52:06 +01002869 nums = 0;
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002870 ok_bits = 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002871 for (n = 0; n < spec->num_adc_nids; n++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002872 for (i = 0; i < imux->num_items; i++) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002873 if (!spec->input_paths[i][n])
Takashi Iwai352f7f92012-12-19 12:52:06 +01002874 break;
2875 }
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002876 if (i >= imux->num_items) {
2877 ok_bits |= (1 << n);
2878 nums++;
2879 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002880 }
2881
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002882 if (!ok_bits) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002883 /* check whether ADC-switch is possible */
2884 for (i = 0; i < imux->num_items; i++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002885 for (n = 0; n < spec->num_adc_nids; n++) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002886 if (spec->input_paths[i][n]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002887 spec->dyn_adc_idx[i] = n;
2888 break;
2889 }
2890 }
2891 }
2892
2893 snd_printdd("hda-codec: enabling ADC switching\n");
2894 spec->dyn_adc_switch = 1;
2895 } else if (nums != spec->num_adc_nids) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002896 /* shrink the invalid adcs and input paths */
2897 nums = 0;
2898 for (n = 0; n < spec->num_adc_nids; n++) {
2899 if (!(ok_bits & (1 << n)))
2900 continue;
2901 if (n != nums) {
2902 spec->adc_nids[nums] = spec->adc_nids[n];
Takashi Iwai980428c2013-01-09 09:28:20 +01002903 for (i = 0; i < imux->num_items; i++) {
2904 invalidate_nid_path(codec,
2905 spec->input_paths[i][nums]);
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002906 spec->input_paths[i][nums] =
2907 spec->input_paths[i][n];
Takashi Iwai980428c2013-01-09 09:28:20 +01002908 }
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002909 }
2910 nums++;
2911 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002912 spec->num_adc_nids = nums;
2913 }
2914
Takashi Iwai967303d2013-02-19 17:12:42 +01002915 if (imux->num_items == 1 ||
2916 (imux->num_items == 2 && spec->hp_mic)) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002917 snd_printdd("hda-codec: reducing to a single ADC\n");
2918 spec->num_adc_nids = 1; /* reduce to a single ADC */
2919 }
2920
2921 /* single index for individual volumes ctls */
2922 if (!spec->dyn_adc_switch && spec->multi_cap_vol)
2923 spec->num_adc_nids = 1;
2924
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925 return 0;
2926}
2927
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002928/* parse capture source paths from the given pin and create imux items */
2929static int parse_capture_source(struct hda_codec *codec, hda_nid_t pin,
Takashi Iwai9dba2052013-01-18 10:01:15 +01002930 int cfg_idx, int num_adcs,
2931 const char *label, int anchor)
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002932{
2933 struct hda_gen_spec *spec = codec->spec;
2934 struct hda_input_mux *imux = &spec->input_mux;
2935 int imux_idx = imux->num_items;
2936 bool imux_added = false;
2937 int c;
2938
2939 for (c = 0; c < num_adcs; c++) {
2940 struct nid_path *path;
2941 hda_nid_t adc = spec->adc_nids[c];
2942
2943 if (!is_reachable_path(codec, pin, adc))
2944 continue;
2945 path = snd_hda_add_new_path(codec, pin, adc, anchor);
2946 if (!path)
2947 continue;
2948 print_nid_path("input", path);
2949 spec->input_paths[imux_idx][c] =
2950 snd_hda_get_path_idx(codec, path);
2951
2952 if (!imux_added) {
Takashi Iwai967303d2013-02-19 17:12:42 +01002953 if (spec->hp_mic_pin == pin)
2954 spec->hp_mic_mux_idx = imux->num_items;
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002955 spec->imux_pins[imux->num_items] = pin;
Takashi Iwai9dba2052013-01-18 10:01:15 +01002956 snd_hda_add_imux_item(imux, label, cfg_idx, NULL);
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002957 imux_added = true;
2958 }
2959 }
2960
2961 return 0;
2962}
2963
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01002965 * create playback/capture controls for input pins
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966 */
Takashi Iwai9dba2052013-01-18 10:01:15 +01002967
Takashi Iwaic9700422013-01-18 10:17:30 +01002968/* fill the label for each input at first */
2969static int fill_input_pin_labels(struct hda_codec *codec)
2970{
2971 struct hda_gen_spec *spec = codec->spec;
2972 const struct auto_pin_cfg *cfg = &spec->autocfg;
2973 int i;
2974
2975 for (i = 0; i < cfg->num_inputs; i++) {
2976 hda_nid_t pin = cfg->inputs[i].pin;
2977 const char *label;
2978 int j, idx;
2979
2980 if (!is_input_pin(codec, pin))
2981 continue;
2982
2983 label = hda_get_autocfg_input_label(codec, cfg, i);
2984 idx = 0;
David Henningsson8e8db7f2013-01-18 15:43:02 +01002985 for (j = i - 1; j >= 0; j--) {
Takashi Iwaic9700422013-01-18 10:17:30 +01002986 if (spec->input_labels[j] &&
2987 !strcmp(spec->input_labels[j], label)) {
2988 idx = spec->input_label_idxs[j] + 1;
2989 break;
2990 }
2991 }
2992
2993 spec->input_labels[i] = label;
2994 spec->input_label_idxs[i] = idx;
2995 }
2996
2997 return 0;
2998}
2999
Takashi Iwai9dba2052013-01-18 10:01:15 +01003000#define CFG_IDX_MIX 99 /* a dummy cfg->input idx for stereo mix */
3001
Takashi Iwai352f7f92012-12-19 12:52:06 +01003002static int create_input_ctls(struct hda_codec *codec)
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02003003{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003004 struct hda_gen_spec *spec = codec->spec;
3005 const struct auto_pin_cfg *cfg = &spec->autocfg;
3006 hda_nid_t mixer = spec->mixer_nid;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003007 int num_adcs;
Takashi Iwaic9700422013-01-18 10:17:30 +01003008 int i, err;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003009 unsigned int val;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02003010
Takashi Iwai352f7f92012-12-19 12:52:06 +01003011 num_adcs = fill_adc_nids(codec);
3012 if (num_adcs < 0)
3013 return 0;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02003014
Takashi Iwaic9700422013-01-18 10:17:30 +01003015 err = fill_input_pin_labels(codec);
3016 if (err < 0)
3017 return err;
3018
Takashi Iwai352f7f92012-12-19 12:52:06 +01003019 for (i = 0; i < cfg->num_inputs; i++) {
3020 hda_nid_t pin;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021
Takashi Iwai352f7f92012-12-19 12:52:06 +01003022 pin = cfg->inputs[i].pin;
3023 if (!is_input_pin(codec, pin))
3024 continue;
3025
Takashi Iwai2c12c302013-01-10 09:33:29 +01003026 val = PIN_IN;
3027 if (cfg->inputs[i].type == AUTO_PIN_MIC)
3028 val |= snd_hda_get_default_vref(codec, pin);
Takashi Iwai93c9d8a2013-03-11 09:48:43 +01003029 if (pin != spec->hp_mic_pin)
3030 set_pin_target(codec, pin, val, false);
Takashi Iwai2c12c302013-01-10 09:33:29 +01003031
Takashi Iwai352f7f92012-12-19 12:52:06 +01003032 if (mixer) {
3033 if (is_reachable_path(codec, pin, mixer)) {
Takashi Iwai196c17662013-01-04 15:01:40 +01003034 err = new_analog_input(codec, i, pin,
Takashi Iwaic9700422013-01-18 10:17:30 +01003035 spec->input_labels[i],
3036 spec->input_label_idxs[i],
3037 mixer);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003038 if (err < 0)
3039 return err;
3040 }
3041 }
3042
Takashi Iwaic9700422013-01-18 10:17:30 +01003043 err = parse_capture_source(codec, pin, i, num_adcs,
3044 spec->input_labels[i], -mixer);
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003045 if (err < 0)
3046 return err;
Takashi Iwai294765582013-01-17 09:52:11 +01003047
Takashi Iwaif811c3c2013-03-07 18:32:59 +01003048 if (spec->add_jack_modes) {
Takashi Iwai294765582013-01-17 09:52:11 +01003049 err = create_in_jack_mode(codec, pin);
3050 if (err < 0)
3051 return err;
3052 }
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003053 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003054
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003055 if (mixer && spec->add_stereo_mix_input) {
Takashi Iwai9dba2052013-01-18 10:01:15 +01003056 err = parse_capture_source(codec, mixer, CFG_IDX_MIX, num_adcs,
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003057 "Stereo Mix", 0);
3058 if (err < 0)
3059 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003060 }
3061
3062 return 0;
3063}
3064
3065
3066/*
3067 * input source mux
3068 */
3069
Takashi Iwaic697b712013-01-07 17:09:26 +01003070/* get the input path specified by the given adc and imux indices */
3071static struct nid_path *get_input_path(struct hda_codec *codec, int adc_idx, int imux_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003072{
3073 struct hda_gen_spec *spec = codec->spec;
David Henningssonb56fa1e2013-01-16 11:45:35 +01003074 if (imux_idx < 0 || imux_idx >= HDA_MAX_NUM_INPUTS) {
3075 snd_BUG();
3076 return NULL;
3077 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003078 if (spec->dyn_adc_switch)
3079 adc_idx = spec->dyn_adc_idx[imux_idx];
David Henningssond3d982f2013-01-18 15:43:01 +01003080 if (adc_idx < 0 || adc_idx >= AUTO_CFG_MAX_INS) {
David Henningssonb56fa1e2013-01-16 11:45:35 +01003081 snd_BUG();
3082 return NULL;
3083 }
Takashi Iwaic697b712013-01-07 17:09:26 +01003084 return snd_hda_get_path_from_idx(codec, spec->input_paths[imux_idx][adc_idx]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003085}
3086
3087static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
3088 unsigned int idx);
3089
3090static int mux_enum_info(struct snd_kcontrol *kcontrol,
3091 struct snd_ctl_elem_info *uinfo)
3092{
3093 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3094 struct hda_gen_spec *spec = codec->spec;
3095 return snd_hda_input_mux_info(&spec->input_mux, uinfo);
3096}
3097
3098static int mux_enum_get(struct snd_kcontrol *kcontrol,
3099 struct snd_ctl_elem_value *ucontrol)
3100{
3101 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3102 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai2a8d5392013-01-18 16:23:25 +01003103 /* the ctls are created at once with multiple counts */
3104 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003105
3106 ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
3107 return 0;
3108}
3109
3110static int mux_enum_put(struct snd_kcontrol *kcontrol,
3111 struct snd_ctl_elem_value *ucontrol)
3112{
3113 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Takashi Iwai2a8d5392013-01-18 16:23:25 +01003114 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003115 return mux_select(codec, adc_idx,
3116 ucontrol->value.enumerated.item[0]);
3117}
3118
Takashi Iwai352f7f92012-12-19 12:52:06 +01003119static const struct snd_kcontrol_new cap_src_temp = {
3120 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3121 .name = "Input Source",
3122 .info = mux_enum_info,
3123 .get = mux_enum_get,
3124 .put = mux_enum_put,
3125};
3126
Takashi Iwai47d46ab2012-12-20 11:48:54 +01003127/*
3128 * capture volume and capture switch ctls
3129 */
3130
Takashi Iwai352f7f92012-12-19 12:52:06 +01003131typedef int (*put_call_t)(struct snd_kcontrol *kcontrol,
3132 struct snd_ctl_elem_value *ucontrol);
3133
Takashi Iwai47d46ab2012-12-20 11:48:54 +01003134/* call the given amp update function for all amps in the imux list at once */
Takashi Iwai352f7f92012-12-19 12:52:06 +01003135static int cap_put_caller(struct snd_kcontrol *kcontrol,
3136 struct snd_ctl_elem_value *ucontrol,
3137 put_call_t func, int type)
3138{
3139 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3140 struct hda_gen_spec *spec = codec->spec;
3141 const struct hda_input_mux *imux;
3142 struct nid_path *path;
3143 int i, adc_idx, err = 0;
3144
3145 imux = &spec->input_mux;
David Henningssona053d1e2013-01-16 11:45:36 +01003146 adc_idx = kcontrol->id.index;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003147 mutex_lock(&codec->control_mutex);
Takashi Iwai47d46ab2012-12-20 11:48:54 +01003148 /* we use the cache-only update at first since multiple input paths
3149 * may shared the same amp; by updating only caches, the redundant
3150 * writes to hardware can be reduced.
3151 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01003152 codec->cached_write = 1;
3153 for (i = 0; i < imux->num_items; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01003154 path = get_input_path(codec, adc_idx, i);
3155 if (!path || !path->ctls[type])
Takashi Iwai352f7f92012-12-19 12:52:06 +01003156 continue;
3157 kcontrol->private_value = path->ctls[type];
3158 err = func(kcontrol, ucontrol);
3159 if (err < 0)
3160 goto error;
3161 }
3162 error:
3163 codec->cached_write = 0;
3164 mutex_unlock(&codec->control_mutex);
Takashi Iwaidc870f32013-01-22 15:24:30 +01003165 snd_hda_codec_flush_cache(codec); /* flush the updates */
Takashi Iwai352f7f92012-12-19 12:52:06 +01003166 if (err >= 0 && spec->cap_sync_hook)
Takashi Iwaia90229e2013-01-18 14:10:00 +01003167 spec->cap_sync_hook(codec, ucontrol);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003168 return err;
3169}
3170
3171/* capture volume ctl callbacks */
3172#define cap_vol_info snd_hda_mixer_amp_volume_info
3173#define cap_vol_get snd_hda_mixer_amp_volume_get
3174#define cap_vol_tlv snd_hda_mixer_amp_tlv
3175
3176static int cap_vol_put(struct snd_kcontrol *kcontrol,
3177 struct snd_ctl_elem_value *ucontrol)
3178{
3179 return cap_put_caller(kcontrol, ucontrol,
3180 snd_hda_mixer_amp_volume_put,
3181 NID_PATH_VOL_CTL);
3182}
3183
3184static const struct snd_kcontrol_new cap_vol_temp = {
3185 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3186 .name = "Capture Volume",
3187 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
3188 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
3189 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK),
3190 .info = cap_vol_info,
3191 .get = cap_vol_get,
3192 .put = cap_vol_put,
3193 .tlv = { .c = cap_vol_tlv },
3194};
3195
3196/* capture switch ctl callbacks */
3197#define cap_sw_info snd_ctl_boolean_stereo_info
3198#define cap_sw_get snd_hda_mixer_amp_switch_get
3199
3200static int cap_sw_put(struct snd_kcontrol *kcontrol,
3201 struct snd_ctl_elem_value *ucontrol)
3202{
Takashi Iwaia90229e2013-01-18 14:10:00 +01003203 return cap_put_caller(kcontrol, ucontrol,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003204 snd_hda_mixer_amp_switch_put,
3205 NID_PATH_MUTE_CTL);
3206}
3207
3208static const struct snd_kcontrol_new cap_sw_temp = {
3209 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3210 .name = "Capture Switch",
3211 .info = cap_sw_info,
3212 .get = cap_sw_get,
3213 .put = cap_sw_put,
3214};
3215
3216static int parse_capvol_in_path(struct hda_codec *codec, struct nid_path *path)
3217{
3218 hda_nid_t nid;
3219 int i, depth;
3220
3221 path->ctls[NID_PATH_VOL_CTL] = path->ctls[NID_PATH_MUTE_CTL] = 0;
3222 for (depth = 0; depth < 3; depth++) {
3223 if (depth >= path->depth)
3224 return -EINVAL;
3225 i = path->depth - depth - 1;
3226 nid = path->path[i];
3227 if (!path->ctls[NID_PATH_VOL_CTL]) {
3228 if (nid_has_volume(codec, nid, HDA_OUTPUT))
3229 path->ctls[NID_PATH_VOL_CTL] =
3230 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3231 else if (nid_has_volume(codec, nid, HDA_INPUT)) {
3232 int idx = path->idx[i];
3233 if (!depth && codec->single_adc_amp)
3234 idx = 0;
3235 path->ctls[NID_PATH_VOL_CTL] =
3236 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
3237 }
3238 }
3239 if (!path->ctls[NID_PATH_MUTE_CTL]) {
3240 if (nid_has_mute(codec, nid, HDA_OUTPUT))
3241 path->ctls[NID_PATH_MUTE_CTL] =
3242 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3243 else if (nid_has_mute(codec, nid, HDA_INPUT)) {
3244 int idx = path->idx[i];
3245 if (!depth && codec->single_adc_amp)
3246 idx = 0;
3247 path->ctls[NID_PATH_MUTE_CTL] =
3248 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
3249 }
3250 }
Takashi Iwai97ec5582006-03-21 11:29:07 +01003251 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252 return 0;
3253}
3254
Takashi Iwai352f7f92012-12-19 12:52:06 +01003255static bool is_inv_dmic_pin(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003257 struct hda_gen_spec *spec = codec->spec;
3258 struct auto_pin_cfg *cfg = &spec->autocfg;
3259 unsigned int val;
3260 int i;
3261
3262 if (!spec->inv_dmic_split)
3263 return false;
3264 for (i = 0; i < cfg->num_inputs; i++) {
3265 if (cfg->inputs[i].pin != nid)
3266 continue;
3267 if (cfg->inputs[i].type != AUTO_PIN_MIC)
3268 return false;
3269 val = snd_hda_codec_get_pincfg(codec, nid);
3270 return snd_hda_get_input_pin_attr(val) == INPUT_PIN_ATTR_INT;
3271 }
3272 return false;
3273}
3274
Takashi Iwaia90229e2013-01-18 14:10:00 +01003275/* capture switch put callback for a single control with hook call */
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003276static int cap_single_sw_put(struct snd_kcontrol *kcontrol,
3277 struct snd_ctl_elem_value *ucontrol)
3278{
3279 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3280 struct hda_gen_spec *spec = codec->spec;
3281 int ret;
3282
3283 ret = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
3284 if (ret < 0)
3285 return ret;
3286
Takashi Iwaia90229e2013-01-18 14:10:00 +01003287 if (spec->cap_sync_hook)
3288 spec->cap_sync_hook(codec, ucontrol);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003289
3290 return ret;
3291}
3292
Takashi Iwai352f7f92012-12-19 12:52:06 +01003293static int add_single_cap_ctl(struct hda_codec *codec, const char *label,
3294 int idx, bool is_switch, unsigned int ctl,
3295 bool inv_dmic)
3296{
3297 struct hda_gen_spec *spec = codec->spec;
3298 char tmpname[44];
3299 int type = is_switch ? HDA_CTL_WIDGET_MUTE : HDA_CTL_WIDGET_VOL;
3300 const char *sfx = is_switch ? "Switch" : "Volume";
3301 unsigned int chs = inv_dmic ? 1 : 3;
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003302 struct snd_kcontrol_new *knew;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003303
3304 if (!ctl)
3305 return 0;
3306
3307 if (label)
3308 snprintf(tmpname, sizeof(tmpname),
3309 "%s Capture %s", label, sfx);
3310 else
3311 snprintf(tmpname, sizeof(tmpname),
3312 "Capture %s", sfx);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003313 knew = add_control(spec, type, tmpname, idx,
3314 amp_val_replace_channels(ctl, chs));
3315 if (!knew)
3316 return -ENOMEM;
Takashi Iwaia90229e2013-01-18 14:10:00 +01003317 if (is_switch)
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003318 knew->put = cap_single_sw_put;
3319 if (!inv_dmic)
3320 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003321
3322 /* Make independent right kcontrol */
3323 if (label)
3324 snprintf(tmpname, sizeof(tmpname),
3325 "Inverted %s Capture %s", label, sfx);
3326 else
3327 snprintf(tmpname, sizeof(tmpname),
3328 "Inverted Capture %s", sfx);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003329 knew = add_control(spec, type, tmpname, idx,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003330 amp_val_replace_channels(ctl, 2));
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003331 if (!knew)
3332 return -ENOMEM;
Takashi Iwaia90229e2013-01-18 14:10:00 +01003333 if (is_switch)
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003334 knew->put = cap_single_sw_put;
3335 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003336}
3337
3338/* create single (and simple) capture volume and switch controls */
3339static int create_single_cap_vol_ctl(struct hda_codec *codec, int idx,
3340 unsigned int vol_ctl, unsigned int sw_ctl,
3341 bool inv_dmic)
3342{
3343 int err;
3344 err = add_single_cap_ctl(codec, NULL, idx, false, vol_ctl, inv_dmic);
3345 if (err < 0)
3346 return err;
3347 err = add_single_cap_ctl(codec, NULL, idx, true, sw_ctl, inv_dmic);
3348 if (err < 0)
3349 return err;
3350 return 0;
3351}
3352
3353/* create bound capture volume and switch controls */
3354static int create_bind_cap_vol_ctl(struct hda_codec *codec, int idx,
3355 unsigned int vol_ctl, unsigned int sw_ctl)
3356{
3357 struct hda_gen_spec *spec = codec->spec;
3358 struct snd_kcontrol_new *knew;
3359
3360 if (vol_ctl) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01003361 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_vol_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003362 if (!knew)
3363 return -ENOMEM;
3364 knew->index = idx;
3365 knew->private_value = vol_ctl;
3366 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
3367 }
3368 if (sw_ctl) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01003369 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_sw_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003370 if (!knew)
3371 return -ENOMEM;
3372 knew->index = idx;
3373 knew->private_value = sw_ctl;
3374 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
3375 }
3376 return 0;
3377}
3378
3379/* return the vol ctl when used first in the imux list */
3380static unsigned int get_first_cap_ctl(struct hda_codec *codec, int idx, int type)
3381{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003382 struct nid_path *path;
3383 unsigned int ctl;
3384 int i;
3385
Takashi Iwaic697b712013-01-07 17:09:26 +01003386 path = get_input_path(codec, 0, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003387 if (!path)
3388 return 0;
3389 ctl = path->ctls[type];
3390 if (!ctl)
3391 return 0;
3392 for (i = 0; i < idx - 1; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01003393 path = get_input_path(codec, 0, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003394 if (path && path->ctls[type] == ctl)
3395 return 0;
3396 }
3397 return ctl;
3398}
3399
3400/* create individual capture volume and switch controls per input */
3401static int create_multi_cap_vol_ctl(struct hda_codec *codec)
3402{
3403 struct hda_gen_spec *spec = codec->spec;
3404 struct hda_input_mux *imux = &spec->input_mux;
Takashi Iwaic9700422013-01-18 10:17:30 +01003405 int i, err, type;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003406
3407 for (i = 0; i < imux->num_items; i++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003408 bool inv_dmic;
Takashi Iwaic9700422013-01-18 10:17:30 +01003409 int idx;
Takashi Iwai9dba2052013-01-18 10:01:15 +01003410
Takashi Iwaic9700422013-01-18 10:17:30 +01003411 idx = imux->items[i].index;
3412 if (idx >= spec->autocfg.num_inputs)
Takashi Iwai9dba2052013-01-18 10:01:15 +01003413 continue;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003414 inv_dmic = is_inv_dmic_pin(codec, spec->imux_pins[i]);
3415
3416 for (type = 0; type < 2; type++) {
Takashi Iwaic9700422013-01-18 10:17:30 +01003417 err = add_single_cap_ctl(codec,
3418 spec->input_labels[idx],
3419 spec->input_label_idxs[idx],
3420 type,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003421 get_first_cap_ctl(codec, i, type),
3422 inv_dmic);
3423 if (err < 0)
3424 return err;
3425 }
3426 }
3427 return 0;
3428}
3429
3430static int create_capture_mixers(struct hda_codec *codec)
3431{
3432 struct hda_gen_spec *spec = codec->spec;
3433 struct hda_input_mux *imux = &spec->input_mux;
3434 int i, n, nums, err;
3435
3436 if (spec->dyn_adc_switch)
3437 nums = 1;
3438 else
3439 nums = spec->num_adc_nids;
3440
3441 if (!spec->auto_mic && imux->num_items > 1) {
3442 struct snd_kcontrol_new *knew;
Takashi Iwai624d9142012-12-19 17:41:52 +01003443 const char *name;
3444 name = nums > 1 ? "Input Source" : "Capture Source";
3445 knew = snd_hda_gen_add_kctl(spec, name, &cap_src_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003446 if (!knew)
3447 return -ENOMEM;
3448 knew->count = nums;
3449 }
3450
3451 for (n = 0; n < nums; n++) {
3452 bool multi = false;
David Henningsson99a55922013-01-16 15:58:44 +01003453 bool multi_cap_vol = spec->multi_cap_vol;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003454 bool inv_dmic = false;
3455 int vol, sw;
3456
3457 vol = sw = 0;
3458 for (i = 0; i < imux->num_items; i++) {
3459 struct nid_path *path;
Takashi Iwaic697b712013-01-07 17:09:26 +01003460 path = get_input_path(codec, n, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003461 if (!path)
3462 continue;
3463 parse_capvol_in_path(codec, path);
3464 if (!vol)
3465 vol = path->ctls[NID_PATH_VOL_CTL];
David Henningsson99a55922013-01-16 15:58:44 +01003466 else if (vol != path->ctls[NID_PATH_VOL_CTL]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003467 multi = true;
David Henningsson99a55922013-01-16 15:58:44 +01003468 if (!same_amp_caps(codec, vol,
3469 path->ctls[NID_PATH_VOL_CTL], HDA_INPUT))
3470 multi_cap_vol = true;
3471 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003472 if (!sw)
3473 sw = path->ctls[NID_PATH_MUTE_CTL];
David Henningsson99a55922013-01-16 15:58:44 +01003474 else if (sw != path->ctls[NID_PATH_MUTE_CTL]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003475 multi = true;
David Henningsson99a55922013-01-16 15:58:44 +01003476 if (!same_amp_caps(codec, sw,
3477 path->ctls[NID_PATH_MUTE_CTL], HDA_INPUT))
3478 multi_cap_vol = true;
3479 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003480 if (is_inv_dmic_pin(codec, spec->imux_pins[i]))
3481 inv_dmic = true;
3482 }
3483
3484 if (!multi)
3485 err = create_single_cap_vol_ctl(codec, n, vol, sw,
3486 inv_dmic);
David Henningsson99a55922013-01-16 15:58:44 +01003487 else if (!multi_cap_vol)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003488 err = create_bind_cap_vol_ctl(codec, n, vol, sw);
3489 else
3490 err = create_multi_cap_vol_ctl(codec);
3491 if (err < 0)
3492 return err;
3493 }
3494
3495 return 0;
3496}
3497
3498/*
3499 * add mic boosts if needed
3500 */
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003501
3502/* check whether the given amp is feasible as a boost volume */
3503static bool check_boost_vol(struct hda_codec *codec, hda_nid_t nid,
3504 int dir, int idx)
3505{
3506 unsigned int step;
3507
3508 if (!nid_has_volume(codec, nid, dir) ||
3509 is_ctl_associated(codec, nid, dir, idx, NID_PATH_VOL_CTL) ||
3510 is_ctl_associated(codec, nid, dir, idx, NID_PATH_BOOST_CTL))
3511 return false;
3512
3513 step = (query_amp_caps(codec, nid, dir) & AC_AMPCAP_STEP_SIZE)
3514 >> AC_AMPCAP_STEP_SIZE_SHIFT;
3515 if (step < 0x20)
3516 return false;
3517 return true;
3518}
3519
3520/* look for a boost amp in a widget close to the pin */
3521static unsigned int look_for_boost_amp(struct hda_codec *codec,
3522 struct nid_path *path)
3523{
3524 unsigned int val = 0;
3525 hda_nid_t nid;
3526 int depth;
3527
3528 for (depth = 0; depth < 3; depth++) {
3529 if (depth >= path->depth - 1)
3530 break;
3531 nid = path->path[depth];
3532 if (depth && check_boost_vol(codec, nid, HDA_OUTPUT, 0)) {
3533 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3534 break;
3535 } else if (check_boost_vol(codec, nid, HDA_INPUT,
3536 path->idx[depth])) {
3537 val = HDA_COMPOSE_AMP_VAL(nid, 3, path->idx[depth],
3538 HDA_INPUT);
3539 break;
3540 }
3541 }
3542
3543 return val;
3544}
3545
Takashi Iwai352f7f92012-12-19 12:52:06 +01003546static int parse_mic_boost(struct hda_codec *codec)
3547{
3548 struct hda_gen_spec *spec = codec->spec;
3549 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003550 struct hda_input_mux *imux = &spec->input_mux;
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003551 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003552
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003553 if (!spec->num_adc_nids)
3554 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003555
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003556 for (i = 0; i < imux->num_items; i++) {
3557 struct nid_path *path;
3558 unsigned int val;
3559 int idx;
3560 char boost_label[44];
David Henningsson02aba552013-01-16 15:58:43 +01003561
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003562 idx = imux->items[i].index;
3563 if (idx >= imux->num_items)
3564 continue;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003565
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003566 /* check only line-in and mic pins */
Takashi Iwai1799cdd52013-01-18 14:37:16 +01003567 if (cfg->inputs[idx].type > AUTO_PIN_LINE_IN)
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003568 continue;
3569
3570 path = get_input_path(codec, 0, i);
3571 if (!path)
3572 continue;
3573
3574 val = look_for_boost_amp(codec, path);
3575 if (!val)
3576 continue;
3577
3578 /* create a boost control */
3579 snprintf(boost_label, sizeof(boost_label),
3580 "%s Boost Volume", spec->input_labels[idx]);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003581 if (!add_control(spec, HDA_CTL_WIDGET_VOL, boost_label,
3582 spec->input_label_idxs[idx], val))
3583 return -ENOMEM;
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003584
3585 path->ctls[NID_PATH_BOOST_CTL] = val;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003586 }
3587 return 0;
3588}
3589
3590/*
3591 * parse digital I/Os and set up NIDs in BIOS auto-parse mode
3592 */
3593static void parse_digital(struct hda_codec *codec)
3594{
3595 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003596 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003597 int i, nums;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003598 hda_nid_t dig_nid, pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003599
3600 /* support multiple SPDIFs; the secondary is set up as a slave */
3601 nums = 0;
3602 for (i = 0; i < spec->autocfg.dig_outs; i++) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01003603 pin = spec->autocfg.dig_out_pins[i];
Takashi Iwai352f7f92012-12-19 12:52:06 +01003604 dig_nid = look_for_dac(codec, pin, true);
3605 if (!dig_nid)
3606 continue;
Takashi Iwai3ca529d2013-01-07 17:25:08 +01003607 path = snd_hda_add_new_path(codec, dig_nid, pin, 0);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003608 if (!path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003609 continue;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003610 print_nid_path("digout", path);
Takashi Iwaie1284af2013-01-03 16:33:02 +01003611 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +01003612 spec->digout_paths[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai2c12c302013-01-10 09:33:29 +01003613 set_pin_target(codec, pin, PIN_OUT, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003614 if (!nums) {
3615 spec->multiout.dig_out_nid = dig_nid;
3616 spec->dig_out_type = spec->autocfg.dig_out_type[0];
3617 } else {
3618 spec->multiout.slave_dig_outs = spec->slave_dig_outs;
3619 if (nums >= ARRAY_SIZE(spec->slave_dig_outs) - 1)
3620 break;
3621 spec->slave_dig_outs[nums - 1] = dig_nid;
3622 }
3623 nums++;
3624 }
3625
3626 if (spec->autocfg.dig_in_pin) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01003627 pin = spec->autocfg.dig_in_pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003628 dig_nid = codec->start_nid;
3629 for (i = 0; i < codec->num_nodes; i++, dig_nid++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003630 unsigned int wcaps = get_wcaps(codec, dig_nid);
3631 if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
3632 continue;
3633 if (!(wcaps & AC_WCAP_DIGITAL))
3634 continue;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003635 path = snd_hda_add_new_path(codec, pin, dig_nid, 0);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003636 if (path) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003637 print_nid_path("digin", path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003638 path->active = true;
3639 spec->dig_in_nid = dig_nid;
Takashi Iwai2430d7b2013-01-04 15:09:42 +01003640 spec->digin_path = snd_hda_get_path_idx(codec, path);
Takashi Iwai2c12c302013-01-10 09:33:29 +01003641 set_pin_target(codec, pin, PIN_IN, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003642 break;
3643 }
3644 }
3645 }
3646}
3647
3648
3649/*
3650 * input MUX handling
3651 */
3652
3653static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur);
3654
3655/* select the given imux item; either unmute exclusively or select the route */
3656static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
3657 unsigned int idx)
3658{
3659 struct hda_gen_spec *spec = codec->spec;
3660 const struct hda_input_mux *imux;
Takashi Iwai55196ff2013-01-24 17:32:56 +01003661 struct nid_path *old_path, *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003662
3663 imux = &spec->input_mux;
3664 if (!imux->num_items)
3665 return 0;
3666
3667 if (idx >= imux->num_items)
3668 idx = imux->num_items - 1;
3669 if (spec->cur_mux[adc_idx] == idx)
3670 return 0;
3671
Takashi Iwai55196ff2013-01-24 17:32:56 +01003672 old_path = get_input_path(codec, adc_idx, spec->cur_mux[adc_idx]);
3673 if (!old_path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003674 return 0;
Takashi Iwai55196ff2013-01-24 17:32:56 +01003675 if (old_path->active)
3676 snd_hda_activate_path(codec, old_path, false, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003677
3678 spec->cur_mux[adc_idx] = idx;
3679
Takashi Iwai967303d2013-02-19 17:12:42 +01003680 if (spec->hp_mic)
3681 update_hp_mic(codec, adc_idx, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003682
3683 if (spec->dyn_adc_switch)
3684 dyn_adc_pcm_resetup(codec, idx);
3685
Takashi Iwaic697b712013-01-07 17:09:26 +01003686 path = get_input_path(codec, adc_idx, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003687 if (!path)
3688 return 0;
3689 if (path->active)
3690 return 0;
3691 snd_hda_activate_path(codec, path, true, false);
3692 if (spec->cap_sync_hook)
Takashi Iwaia90229e2013-01-18 14:10:00 +01003693 spec->cap_sync_hook(codec, NULL);
Takashi Iwai55196ff2013-01-24 17:32:56 +01003694 path_power_down_sync(codec, old_path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003695 return 1;
3696}
3697
3698
3699/*
3700 * Jack detections for HP auto-mute and mic-switch
3701 */
3702
3703/* check each pin in the given array; returns true if any of them is plugged */
3704static bool detect_jacks(struct hda_codec *codec, int num_pins, hda_nid_t *pins)
3705{
3706 int i, present = 0;
3707
3708 for (i = 0; i < num_pins; i++) {
3709 hda_nid_t nid = pins[i];
3710 if (!nid)
3711 break;
Takashi Iwai0b4df932013-01-10 09:45:13 +01003712 /* don't detect pins retasked as inputs */
3713 if (snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_IN_EN)
3714 continue;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003715 present |= snd_hda_jack_detect(codec, nid);
3716 }
3717 return present;
3718}
3719
3720/* standard HP/line-out auto-mute helper */
3721static void do_automute(struct hda_codec *codec, int num_pins, hda_nid_t *pins,
Takashi Iwai2c12c302013-01-10 09:33:29 +01003722 bool mute)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003723{
3724 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003725 int i;
3726
3727 for (i = 0; i < num_pins; i++) {
3728 hda_nid_t nid = pins[i];
Takashi Iwai967303d2013-02-19 17:12:42 +01003729 unsigned int val, oldval;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003730 if (!nid)
3731 break;
Takashi Iwai967303d2013-02-19 17:12:42 +01003732 oldval = snd_hda_codec_get_pin_target(codec, nid);
3733 if (oldval & PIN_IN)
3734 continue; /* no mute for inputs */
Takashi Iwai352f7f92012-12-19 12:52:06 +01003735 /* don't reset VREF value in case it's controlling
3736 * the amp (see alc861_fixup_asus_amp_vref_0f())
3737 */
Takashi Iwai2c12c302013-01-10 09:33:29 +01003738 if (spec->keep_vref_in_automute)
Takashi Iwai967303d2013-02-19 17:12:42 +01003739 val = oldval & ~PIN_HP;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003740 else
Takashi Iwai352f7f92012-12-19 12:52:06 +01003741 val = 0;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003742 if (!mute)
Takashi Iwai967303d2013-02-19 17:12:42 +01003743 val |= oldval;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003744 /* here we call update_pin_ctl() so that the pinctl is changed
3745 * without changing the pinctl target value;
3746 * the original target value will be still referred at the
3747 * init / resume again
3748 */
3749 update_pin_ctl(codec, nid, val);
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01003750 set_pin_eapd(codec, nid, !mute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003751 }
3752}
3753
3754/* Toggle outputs muting */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003755void snd_hda_gen_update_outputs(struct hda_codec *codec)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003756{
3757 struct hda_gen_spec *spec = codec->spec;
3758 int on;
3759
3760 /* Control HP pins/amps depending on master_mute state;
3761 * in general, HP pins/amps control should be enabled in all cases,
3762 * but currently set only for master_mute, just to be safe
3763 */
Takashi Iwai967303d2013-02-19 17:12:42 +01003764 do_automute(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
Takashi Iwai2c12c302013-01-10 09:33:29 +01003765 spec->autocfg.hp_pins, spec->master_mute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003766
3767 if (!spec->automute_speaker)
3768 on = 0;
3769 else
3770 on = spec->hp_jack_present | spec->line_jack_present;
3771 on |= spec->master_mute;
Takashi Iwai47b9ddb82013-01-16 18:18:00 +01003772 spec->speaker_muted = on;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003773 do_automute(codec, ARRAY_SIZE(spec->autocfg.speaker_pins),
Takashi Iwai2c12c302013-01-10 09:33:29 +01003774 spec->autocfg.speaker_pins, on);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003775
3776 /* toggle line-out mutes if needed, too */
3777 /* if LO is a copy of either HP or Speaker, don't need to handle it */
3778 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0] ||
3779 spec->autocfg.line_out_pins[0] == spec->autocfg.speaker_pins[0])
3780 return;
3781 if (!spec->automute_lo)
3782 on = 0;
3783 else
3784 on = spec->hp_jack_present;
3785 on |= spec->master_mute;
Takashi Iwai47b9ddb82013-01-16 18:18:00 +01003786 spec->line_out_muted = on;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003787 do_automute(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
Takashi Iwai2c12c302013-01-10 09:33:29 +01003788 spec->autocfg.line_out_pins, on);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003789}
Takashi Iwai5d550e12012-12-19 15:16:44 +01003790EXPORT_SYMBOL_HDA(snd_hda_gen_update_outputs);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003791
3792static void call_update_outputs(struct hda_codec *codec)
3793{
3794 struct hda_gen_spec *spec = codec->spec;
3795 if (spec->automute_hook)
3796 spec->automute_hook(codec);
3797 else
Takashi Iwai5d550e12012-12-19 15:16:44 +01003798 snd_hda_gen_update_outputs(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003799}
3800
3801/* standard HP-automute helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003802void snd_hda_gen_hp_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003803{
3804 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai92603c52013-01-22 07:46:31 +01003805 hda_nid_t *pins = spec->autocfg.hp_pins;
3806 int num_pins = ARRAY_SIZE(spec->autocfg.hp_pins);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003807
Takashi Iwai92603c52013-01-22 07:46:31 +01003808 /* No detection for the first HP jack during indep-HP mode */
3809 if (spec->indep_hp_enabled) {
3810 pins++;
3811 num_pins--;
3812 }
3813
3814 spec->hp_jack_present = detect_jacks(codec, num_pins, pins);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003815 if (!spec->detect_hp || (!spec->automute_speaker && !spec->automute_lo))
3816 return;
3817 call_update_outputs(codec);
3818}
Takashi Iwai5d550e12012-12-19 15:16:44 +01003819EXPORT_SYMBOL_HDA(snd_hda_gen_hp_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003820
3821/* standard line-out-automute helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003822void snd_hda_gen_line_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003823{
3824 struct hda_gen_spec *spec = codec->spec;
3825
3826 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
3827 return;
3828 /* check LO jack only when it's different from HP */
3829 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0])
3830 return;
3831
3832 spec->line_jack_present =
3833 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
3834 spec->autocfg.line_out_pins);
3835 if (!spec->automute_speaker || !spec->detect_lo)
3836 return;
3837 call_update_outputs(codec);
3838}
Takashi Iwai5d550e12012-12-19 15:16:44 +01003839EXPORT_SYMBOL_HDA(snd_hda_gen_line_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003840
3841/* standard mic auto-switch helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003842void snd_hda_gen_mic_autoswitch(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003843{
3844 struct hda_gen_spec *spec = codec->spec;
3845 int i;
3846
3847 if (!spec->auto_mic)
3848 return;
3849
3850 for (i = spec->am_num_entries - 1; i > 0; i--) {
Takashi Iwai0b4df932013-01-10 09:45:13 +01003851 hda_nid_t pin = spec->am_entry[i].pin;
3852 /* don't detect pins retasked as outputs */
3853 if (snd_hda_codec_get_pin_target(codec, pin) & AC_PINCTL_OUT_EN)
3854 continue;
3855 if (snd_hda_jack_detect(codec, pin)) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003856 mux_select(codec, 0, spec->am_entry[i].idx);
3857 return;
3858 }
3859 }
3860 mux_select(codec, 0, spec->am_entry[0].idx);
3861}
Takashi Iwai5d550e12012-12-19 15:16:44 +01003862EXPORT_SYMBOL_HDA(snd_hda_gen_mic_autoswitch);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003863
Takashi Iwaia5cc2502013-01-16 18:08:55 +01003864/* update jack retasking */
3865static void update_automute_all(struct hda_codec *codec)
3866{
3867 struct hda_gen_spec *spec = codec->spec;
3868
Takashi Iwai8ba955c2013-03-07 18:40:58 +01003869 update_hp_automute_hook(codec);
Takashi Iwaia5cc2502013-01-16 18:08:55 +01003870 if (spec->line_automute_hook)
3871 spec->line_automute_hook(codec, NULL);
3872 else
3873 snd_hda_gen_line_automute(codec, NULL);
3874 if (spec->mic_autoswitch_hook)
3875 spec->mic_autoswitch_hook(codec, NULL);
3876 else
3877 snd_hda_gen_mic_autoswitch(codec, NULL);
3878}
3879
Takashi Iwai77afe0e2013-05-31 14:10:03 +02003880/* call appropriate hooks */
3881static void call_hp_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
3882{
3883 struct hda_gen_spec *spec = codec->spec;
3884 if (spec->hp_automute_hook)
3885 spec->hp_automute_hook(codec, jack);
3886 else
3887 snd_hda_gen_hp_automute(codec, jack);
3888}
3889
3890static void call_line_automute(struct hda_codec *codec,
3891 struct hda_jack_tbl *jack)
3892{
3893 struct hda_gen_spec *spec = codec->spec;
3894 if (spec->line_automute_hook)
3895 spec->line_automute_hook(codec, jack);
3896 else
3897 snd_hda_gen_line_automute(codec, jack);
3898}
3899
3900static void call_mic_autoswitch(struct hda_codec *codec,
3901 struct hda_jack_tbl *jack)
3902{
3903 struct hda_gen_spec *spec = codec->spec;
3904 if (spec->mic_autoswitch_hook)
3905 spec->mic_autoswitch_hook(codec, jack);
3906 else
3907 snd_hda_gen_mic_autoswitch(codec, jack);
3908}
3909
Takashi Iwai352f7f92012-12-19 12:52:06 +01003910/*
3911 * Auto-Mute mode mixer enum support
3912 */
3913static int automute_mode_info(struct snd_kcontrol *kcontrol,
3914 struct snd_ctl_elem_info *uinfo)
3915{
3916 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3917 struct hda_gen_spec *spec = codec->spec;
3918 static const char * const texts3[] = {
3919 "Disabled", "Speaker Only", "Line Out+Speaker"
Takashi Iwai071c73a2006-08-23 18:34:06 +02003920 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003921
Takashi Iwai352f7f92012-12-19 12:52:06 +01003922 if (spec->automute_speaker_possible && spec->automute_lo_possible)
3923 return snd_hda_enum_helper_info(kcontrol, uinfo, 3, texts3);
3924 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
3925}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003926
Takashi Iwai352f7f92012-12-19 12:52:06 +01003927static int automute_mode_get(struct snd_kcontrol *kcontrol,
3928 struct snd_ctl_elem_value *ucontrol)
3929{
3930 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3931 struct hda_gen_spec *spec = codec->spec;
3932 unsigned int val = 0;
3933 if (spec->automute_speaker)
3934 val++;
3935 if (spec->automute_lo)
3936 val++;
Takashi Iwai071c73a2006-08-23 18:34:06 +02003937
Takashi Iwai352f7f92012-12-19 12:52:06 +01003938 ucontrol->value.enumerated.item[0] = val;
3939 return 0;
3940}
3941
3942static int automute_mode_put(struct snd_kcontrol *kcontrol,
3943 struct snd_ctl_elem_value *ucontrol)
3944{
3945 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3946 struct hda_gen_spec *spec = codec->spec;
3947
3948 switch (ucontrol->value.enumerated.item[0]) {
3949 case 0:
3950 if (!spec->automute_speaker && !spec->automute_lo)
3951 return 0;
3952 spec->automute_speaker = 0;
3953 spec->automute_lo = 0;
3954 break;
3955 case 1:
3956 if (spec->automute_speaker_possible) {
3957 if (!spec->automute_lo && spec->automute_speaker)
3958 return 0;
3959 spec->automute_speaker = 1;
3960 spec->automute_lo = 0;
3961 } else if (spec->automute_lo_possible) {
3962 if (spec->automute_lo)
3963 return 0;
3964 spec->automute_lo = 1;
3965 } else
3966 return -EINVAL;
3967 break;
3968 case 2:
3969 if (!spec->automute_lo_possible || !spec->automute_speaker_possible)
3970 return -EINVAL;
3971 if (spec->automute_speaker && spec->automute_lo)
3972 return 0;
3973 spec->automute_speaker = 1;
3974 spec->automute_lo = 1;
3975 break;
3976 default:
3977 return -EINVAL;
3978 }
3979 call_update_outputs(codec);
3980 return 1;
3981}
3982
3983static const struct snd_kcontrol_new automute_mode_enum = {
3984 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3985 .name = "Auto-Mute Mode",
3986 .info = automute_mode_info,
3987 .get = automute_mode_get,
3988 .put = automute_mode_put,
3989};
3990
3991static int add_automute_mode_enum(struct hda_codec *codec)
3992{
3993 struct hda_gen_spec *spec = codec->spec;
3994
Takashi Iwai12c93df2012-12-19 14:38:33 +01003995 if (!snd_hda_gen_add_kctl(spec, NULL, &automute_mode_enum))
Takashi Iwai352f7f92012-12-19 12:52:06 +01003996 return -ENOMEM;
3997 return 0;
3998}
3999
4000/*
4001 * Check the availability of HP/line-out auto-mute;
4002 * Set up appropriately if really supported
4003 */
4004static int check_auto_mute_availability(struct hda_codec *codec)
4005{
4006 struct hda_gen_spec *spec = codec->spec;
4007 struct auto_pin_cfg *cfg = &spec->autocfg;
4008 int present = 0;
4009 int i, err;
4010
Takashi Iwaif72706b2013-01-16 18:20:07 +01004011 if (spec->suppress_auto_mute)
4012 return 0;
4013
Takashi Iwai352f7f92012-12-19 12:52:06 +01004014 if (cfg->hp_pins[0])
4015 present++;
4016 if (cfg->line_out_pins[0])
4017 present++;
4018 if (cfg->speaker_pins[0])
4019 present++;
4020 if (present < 2) /* need two different output types */
Takashi Iwai071c73a2006-08-23 18:34:06 +02004021 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004022
4023 if (!cfg->speaker_pins[0] &&
4024 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
4025 memcpy(cfg->speaker_pins, cfg->line_out_pins,
4026 sizeof(cfg->speaker_pins));
4027 cfg->speaker_outs = cfg->line_outs;
Takashi Iwai071c73a2006-08-23 18:34:06 +02004028 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004029
Takashi Iwai352f7f92012-12-19 12:52:06 +01004030 if (!cfg->hp_pins[0] &&
4031 cfg->line_out_type == AUTO_PIN_HP_OUT) {
4032 memcpy(cfg->hp_pins, cfg->line_out_pins,
4033 sizeof(cfg->hp_pins));
4034 cfg->hp_outs = cfg->line_outs;
4035 }
4036
4037 for (i = 0; i < cfg->hp_outs; i++) {
4038 hda_nid_t nid = cfg->hp_pins[i];
4039 if (!is_jack_detectable(codec, nid))
4040 continue;
4041 snd_printdd("hda-codec: Enable HP auto-muting on NID 0x%x\n",
4042 nid);
4043 snd_hda_jack_detect_enable_callback(codec, nid, HDA_GEN_HP_EVENT,
Takashi Iwai77afe0e2013-05-31 14:10:03 +02004044 call_hp_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004045 spec->detect_hp = 1;
4046 }
4047
4048 if (cfg->line_out_type == AUTO_PIN_LINE_OUT && cfg->line_outs) {
4049 if (cfg->speaker_outs)
4050 for (i = 0; i < cfg->line_outs; i++) {
4051 hda_nid_t nid = cfg->line_out_pins[i];
4052 if (!is_jack_detectable(codec, nid))
4053 continue;
4054 snd_printdd("hda-codec: Enable Line-Out auto-muting on NID 0x%x\n", nid);
4055 snd_hda_jack_detect_enable_callback(codec, nid,
4056 HDA_GEN_FRONT_EVENT,
Takashi Iwai77afe0e2013-05-31 14:10:03 +02004057 call_line_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004058 spec->detect_lo = 1;
4059 }
4060 spec->automute_lo_possible = spec->detect_hp;
4061 }
4062
4063 spec->automute_speaker_possible = cfg->speaker_outs &&
4064 (spec->detect_hp || spec->detect_lo);
4065
4066 spec->automute_lo = spec->automute_lo_possible;
4067 spec->automute_speaker = spec->automute_speaker_possible;
4068
4069 if (spec->automute_speaker_possible || spec->automute_lo_possible) {
4070 /* create a control for automute mode */
4071 err = add_automute_mode_enum(codec);
4072 if (err < 0)
4073 return err;
4074 }
4075 return 0;
4076}
4077
Takashi Iwai352f7f92012-12-19 12:52:06 +01004078/* check whether all auto-mic pins are valid; setup indices if OK */
4079static bool auto_mic_check_imux(struct hda_codec *codec)
4080{
4081 struct hda_gen_spec *spec = codec->spec;
4082 const struct hda_input_mux *imux;
4083 int i;
4084
4085 imux = &spec->input_mux;
4086 for (i = 0; i < spec->am_num_entries; i++) {
4087 spec->am_entry[i].idx =
4088 find_idx_in_nid_list(spec->am_entry[i].pin,
4089 spec->imux_pins, imux->num_items);
4090 if (spec->am_entry[i].idx < 0)
4091 return false; /* no corresponding imux */
4092 }
4093
4094 /* we don't need the jack detection for the first pin */
4095 for (i = 1; i < spec->am_num_entries; i++)
4096 snd_hda_jack_detect_enable_callback(codec,
4097 spec->am_entry[i].pin,
4098 HDA_GEN_MIC_EVENT,
Takashi Iwai77afe0e2013-05-31 14:10:03 +02004099 call_mic_autoswitch);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004100 return true;
4101}
4102
4103static int compare_attr(const void *ap, const void *bp)
4104{
4105 const struct automic_entry *a = ap;
4106 const struct automic_entry *b = bp;
4107 return (int)(a->attr - b->attr);
4108}
4109
4110/*
4111 * Check the availability of auto-mic switch;
4112 * Set up if really supported
4113 */
4114static int check_auto_mic_availability(struct hda_codec *codec)
4115{
4116 struct hda_gen_spec *spec = codec->spec;
4117 struct auto_pin_cfg *cfg = &spec->autocfg;
4118 unsigned int types;
4119 int i, num_pins;
4120
Takashi Iwaid12daf62013-01-07 16:32:11 +01004121 if (spec->suppress_auto_mic)
4122 return 0;
4123
Takashi Iwai352f7f92012-12-19 12:52:06 +01004124 types = 0;
4125 num_pins = 0;
4126 for (i = 0; i < cfg->num_inputs; i++) {
4127 hda_nid_t nid = cfg->inputs[i].pin;
4128 unsigned int attr;
4129 attr = snd_hda_codec_get_pincfg(codec, nid);
4130 attr = snd_hda_get_input_pin_attr(attr);
4131 if (types & (1 << attr))
4132 return 0; /* already occupied */
4133 switch (attr) {
4134 case INPUT_PIN_ATTR_INT:
4135 if (cfg->inputs[i].type != AUTO_PIN_MIC)
4136 return 0; /* invalid type */
4137 break;
4138 case INPUT_PIN_ATTR_UNUSED:
4139 return 0; /* invalid entry */
4140 default:
4141 if (cfg->inputs[i].type > AUTO_PIN_LINE_IN)
4142 return 0; /* invalid type */
4143 if (!spec->line_in_auto_switch &&
4144 cfg->inputs[i].type != AUTO_PIN_MIC)
4145 return 0; /* only mic is allowed */
4146 if (!is_jack_detectable(codec, nid))
4147 return 0; /* no unsol support */
4148 break;
4149 }
4150 if (num_pins >= MAX_AUTO_MIC_PINS)
4151 return 0;
4152 types |= (1 << attr);
4153 spec->am_entry[num_pins].pin = nid;
4154 spec->am_entry[num_pins].attr = attr;
4155 num_pins++;
4156 }
4157
4158 if (num_pins < 2)
4159 return 0;
4160
4161 spec->am_num_entries = num_pins;
4162 /* sort the am_entry in the order of attr so that the pin with a
4163 * higher attr will be selected when the jack is plugged.
4164 */
4165 sort(spec->am_entry, num_pins, sizeof(spec->am_entry[0]),
4166 compare_attr, NULL);
4167
4168 if (!auto_mic_check_imux(codec))
4169 return 0;
4170
4171 spec->auto_mic = 1;
4172 spec->num_adc_nids = 1;
4173 spec->cur_mux[0] = spec->am_entry[0].idx;
4174 snd_printdd("hda-codec: Enable auto-mic switch on NID 0x%x/0x%x/0x%x\n",
4175 spec->am_entry[0].pin,
4176 spec->am_entry[1].pin,
4177 spec->am_entry[2].pin);
4178
4179 return 0;
4180}
4181
Takashi Iwai55196ff2013-01-24 17:32:56 +01004182/* power_filter hook; make inactive widgets into power down */
4183static unsigned int snd_hda_gen_path_power_filter(struct hda_codec *codec,
4184 hda_nid_t nid,
4185 unsigned int power_state)
4186{
4187 if (power_state != AC_PWRST_D0)
4188 return power_state;
4189 if (get_wcaps_type(get_wcaps(codec, nid)) >= AC_WID_POWER)
4190 return power_state;
Takashi Iwaib1b9fbd2013-05-14 12:58:47 +02004191 if (is_active_nid_for_any(codec, nid))
Takashi Iwai55196ff2013-01-24 17:32:56 +01004192 return power_state;
4193 return AC_PWRST_D3;
4194}
4195
Takashi Iwai352f7f92012-12-19 12:52:06 +01004196
Takashi Iwai9eb413e2012-12-19 14:41:21 +01004197/*
4198 * Parse the given BIOS configuration and set up the hda_gen_spec
4199 *
4200 * return 1 if successful, 0 if the proper config is not found,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004201 * or a negative error code
4202 */
4203int snd_hda_gen_parse_auto_config(struct hda_codec *codec,
Takashi Iwai9eb413e2012-12-19 14:41:21 +01004204 struct auto_pin_cfg *cfg)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004205{
4206 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004207 int err;
4208
Takashi Iwai1c70a582013-01-11 17:48:22 +01004209 parse_user_hints(codec);
4210
Takashi Iwaie4a395e2013-01-23 17:00:31 +01004211 if (spec->mixer_nid && !spec->mixer_merge_nid)
4212 spec->mixer_merge_nid = spec->mixer_nid;
4213
Takashi Iwai9eb413e2012-12-19 14:41:21 +01004214 if (cfg != &spec->autocfg) {
4215 spec->autocfg = *cfg;
4216 cfg = &spec->autocfg;
4217 }
4218
Takashi Iwai98bd1112013-03-22 14:53:50 +01004219 if (!spec->main_out_badness)
4220 spec->main_out_badness = &hda_main_out_badness;
4221 if (!spec->extra_out_badness)
4222 spec->extra_out_badness = &hda_extra_out_badness;
4223
David Henningsson6fc4cb92013-01-16 15:58:45 +01004224 fill_all_dac_nids(codec);
4225
Takashi Iwai352f7f92012-12-19 12:52:06 +01004226 if (!cfg->line_outs) {
4227 if (cfg->dig_outs || cfg->dig_in_pin) {
4228 spec->multiout.max_channels = 2;
4229 spec->no_analog = 1;
4230 goto dig_only;
4231 }
4232 return 0; /* can't find valid BIOS pin config */
4233 }
4234
4235 if (!spec->no_primary_hp &&
4236 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT &&
4237 cfg->line_outs <= cfg->hp_outs) {
4238 /* use HP as primary out */
4239 cfg->speaker_outs = cfg->line_outs;
4240 memcpy(cfg->speaker_pins, cfg->line_out_pins,
4241 sizeof(cfg->speaker_pins));
4242 cfg->line_outs = cfg->hp_outs;
4243 memcpy(cfg->line_out_pins, cfg->hp_pins, sizeof(cfg->hp_pins));
4244 cfg->hp_outs = 0;
4245 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
4246 cfg->line_out_type = AUTO_PIN_HP_OUT;
4247 }
4248
4249 err = parse_output_paths(codec);
4250 if (err < 0)
4251 return err;
4252 err = create_multi_channel_mode(codec);
4253 if (err < 0)
4254 return err;
4255 err = create_multi_out_ctls(codec, cfg);
4256 if (err < 0)
4257 return err;
4258 err = create_hp_out_ctls(codec);
4259 if (err < 0)
4260 return err;
4261 err = create_speaker_out_ctls(codec);
4262 if (err < 0)
4263 return err;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004264 err = create_indep_hp_ctls(codec);
4265 if (err < 0)
4266 return err;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01004267 err = create_loopback_mixing_ctl(codec);
4268 if (err < 0)
4269 return err;
Takashi Iwai967303d2013-02-19 17:12:42 +01004270 err = create_hp_mic(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004271 if (err < 0)
4272 return err;
4273 err = create_input_ctls(codec);
Takashi Iwaid13bd412008-07-30 15:01:45 +02004274 if (err < 0)
Takashi Iwai071c73a2006-08-23 18:34:06 +02004275 return err;
4276
Takashi Iwaia07a9492013-01-07 16:44:06 +01004277 spec->const_channel_count = spec->ext_channel_count;
4278 /* check the multiple speaker and headphone pins */
4279 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
4280 spec->const_channel_count = max(spec->const_channel_count,
4281 cfg->speaker_outs * 2);
4282 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
4283 spec->const_channel_count = max(spec->const_channel_count,
4284 cfg->hp_outs * 2);
4285 spec->multiout.max_channels = max(spec->ext_channel_count,
4286 spec->const_channel_count);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004287
4288 err = check_auto_mute_availability(codec);
4289 if (err < 0)
4290 return err;
4291
4292 err = check_dyn_adc_switch(codec);
4293 if (err < 0)
4294 return err;
4295
Takashi Iwai967303d2013-02-19 17:12:42 +01004296 err = check_auto_mic_availability(codec);
4297 if (err < 0)
4298 return err;
Takashi Iwai071c73a2006-08-23 18:34:06 +02004299
Takashi Iwai352f7f92012-12-19 12:52:06 +01004300 err = create_capture_mixers(codec);
4301 if (err < 0)
4302 return err;
4303
4304 err = parse_mic_boost(codec);
4305 if (err < 0)
4306 return err;
4307
Takashi Iwaif811c3c2013-03-07 18:32:59 +01004308 if (spec->add_jack_modes) {
Takashi Iwai978e77e2013-01-10 16:57:58 +01004309 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
4310 err = create_out_jack_modes(codec, cfg->line_outs,
4311 cfg->line_out_pins);
4312 if (err < 0)
4313 return err;
4314 }
4315 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
4316 err = create_out_jack_modes(codec, cfg->hp_outs,
4317 cfg->hp_pins);
4318 if (err < 0)
4319 return err;
4320 }
4321 }
4322
Takashi Iwai352f7f92012-12-19 12:52:06 +01004323 dig_only:
4324 parse_digital(codec);
4325
Takashi Iwai55196ff2013-01-24 17:32:56 +01004326 if (spec->power_down_unused)
4327 codec->power_filter = snd_hda_gen_path_power_filter;
4328
Takashi Iwai7504b6c2013-03-18 11:25:51 +01004329 if (!spec->no_analog && spec->beep_nid) {
4330 err = snd_hda_attach_beep_device(codec, spec->beep_nid);
4331 if (err < 0)
4332 return err;
4333 }
4334
Takashi Iwai352f7f92012-12-19 12:52:06 +01004335 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004336}
Takashi Iwai352f7f92012-12-19 12:52:06 +01004337EXPORT_SYMBOL_HDA(snd_hda_gen_parse_auto_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004338
4339
4340/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01004341 * Build control elements
Linus Torvalds1da177e2005-04-16 15:20:36 -07004342 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01004343
4344/* slave controls for virtual master */
4345static const char * const slave_pfxs[] = {
4346 "Front", "Surround", "Center", "LFE", "Side",
4347 "Headphone", "Speaker", "Mono", "Line Out",
4348 "CLFE", "Bass Speaker", "PCM",
Takashi Iwaiee79c692013-01-07 09:57:42 +01004349 "Speaker Front", "Speaker Surround", "Speaker CLFE", "Speaker Side",
4350 "Headphone Front", "Headphone Surround", "Headphone CLFE",
4351 "Headphone Side",
Takashi Iwai352f7f92012-12-19 12:52:06 +01004352 NULL,
4353};
4354
4355int snd_hda_gen_build_controls(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004356{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004357 struct hda_gen_spec *spec = codec->spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004358 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004359
Takashi Iwai36502d02012-12-19 15:15:10 +01004360 if (spec->kctls.used) {
4361 err = snd_hda_add_new_ctls(codec, spec->kctls.list);
4362 if (err < 0)
4363 return err;
4364 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004365
Takashi Iwai352f7f92012-12-19 12:52:06 +01004366 if (spec->multiout.dig_out_nid) {
4367 err = snd_hda_create_dig_out_ctls(codec,
4368 spec->multiout.dig_out_nid,
4369 spec->multiout.dig_out_nid,
4370 spec->pcm_rec[1].pcm_type);
4371 if (err < 0)
4372 return err;
4373 if (!spec->no_analog) {
4374 err = snd_hda_create_spdif_share_sw(codec,
4375 &spec->multiout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004376 if (err < 0)
4377 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004378 spec->multiout.share_spdif = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004379 }
4380 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01004381 if (spec->dig_in_nid) {
4382 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
4383 if (err < 0)
4384 return err;
4385 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004386
Takashi Iwai352f7f92012-12-19 12:52:06 +01004387 /* if we have no master control, let's create it */
4388 if (!spec->no_analog &&
4389 !snd_hda_find_mixer_ctl(codec, "Master Playback Volume")) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01004390 err = snd_hda_add_vmaster(codec, "Master Playback Volume",
Takashi Iwai7a71bbf2013-01-17 10:25:15 +01004391 spec->vmaster_tlv, slave_pfxs,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004392 "Playback Volume");
4393 if (err < 0)
4394 return err;
4395 }
4396 if (!spec->no_analog &&
4397 !snd_hda_find_mixer_ctl(codec, "Master Playback Switch")) {
4398 err = __snd_hda_add_vmaster(codec, "Master Playback Switch",
4399 NULL, slave_pfxs,
4400 "Playback Switch",
4401 true, &spec->vmaster_mute.sw_kctl);
4402 if (err < 0)
4403 return err;
4404 if (spec->vmaster_mute.hook)
Takashi Iwaifd25a972012-12-20 14:57:18 +01004405 snd_hda_add_vmaster_hook(codec, &spec->vmaster_mute,
4406 spec->vmaster_mute_enum);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004407 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004408
Takashi Iwai352f7f92012-12-19 12:52:06 +01004409 free_kctls(spec); /* no longer needed */
4410
Takashi Iwai352f7f92012-12-19 12:52:06 +01004411 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
4412 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004413 return err;
4414
4415 return 0;
4416}
Takashi Iwai352f7f92012-12-19 12:52:06 +01004417EXPORT_SYMBOL_HDA(snd_hda_gen_build_controls);
4418
Linus Torvalds1da177e2005-04-16 15:20:36 -07004419
4420/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01004421 * PCM definitions
Linus Torvalds1da177e2005-04-16 15:20:36 -07004422 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004423
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004424static void call_pcm_playback_hook(struct hda_pcm_stream *hinfo,
4425 struct hda_codec *codec,
4426 struct snd_pcm_substream *substream,
4427 int action)
4428{
4429 struct hda_gen_spec *spec = codec->spec;
4430 if (spec->pcm_playback_hook)
4431 spec->pcm_playback_hook(hinfo, codec, substream, action);
4432}
4433
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004434static void call_pcm_capture_hook(struct hda_pcm_stream *hinfo,
4435 struct hda_codec *codec,
4436 struct snd_pcm_substream *substream,
4437 int action)
4438{
4439 struct hda_gen_spec *spec = codec->spec;
4440 if (spec->pcm_capture_hook)
4441 spec->pcm_capture_hook(hinfo, codec, substream, action);
4442}
4443
Takashi Iwai352f7f92012-12-19 12:52:06 +01004444/*
4445 * Analog playback callbacks
4446 */
4447static int playback_pcm_open(struct hda_pcm_stream *hinfo,
4448 struct hda_codec *codec,
4449 struct snd_pcm_substream *substream)
4450{
4451 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004452 int err;
4453
4454 mutex_lock(&spec->pcm_mutex);
4455 err = snd_hda_multi_out_analog_open(codec,
4456 &spec->multiout, substream,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004457 hinfo);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004458 if (!err) {
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004459 spec->active_streams |= 1 << STREAM_MULTI_OUT;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004460 call_pcm_playback_hook(hinfo, codec, substream,
4461 HDA_GEN_PCM_ACT_OPEN);
4462 }
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004463 mutex_unlock(&spec->pcm_mutex);
4464 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004465}
4466
4467static int playback_pcm_prepare(struct hda_pcm_stream *hinfo,
Takashi Iwai97ec5582006-03-21 11:29:07 +01004468 struct hda_codec *codec,
4469 unsigned int stream_tag,
4470 unsigned int format,
4471 struct snd_pcm_substream *substream)
4472{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004473 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004474 int err;
4475
4476 err = snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
4477 stream_tag, format, substream);
4478 if (!err)
4479 call_pcm_playback_hook(hinfo, codec, substream,
4480 HDA_GEN_PCM_ACT_PREPARE);
4481 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004482}
Takashi Iwai97ec5582006-03-21 11:29:07 +01004483
Takashi Iwai352f7f92012-12-19 12:52:06 +01004484static int playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
4485 struct hda_codec *codec,
4486 struct snd_pcm_substream *substream)
4487{
4488 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004489 int err;
4490
4491 err = snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
4492 if (!err)
4493 call_pcm_playback_hook(hinfo, codec, substream,
4494 HDA_GEN_PCM_ACT_CLEANUP);
4495 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004496}
4497
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004498static int playback_pcm_close(struct hda_pcm_stream *hinfo,
4499 struct hda_codec *codec,
4500 struct snd_pcm_substream *substream)
4501{
4502 struct hda_gen_spec *spec = codec->spec;
4503 mutex_lock(&spec->pcm_mutex);
4504 spec->active_streams &= ~(1 << STREAM_MULTI_OUT);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004505 call_pcm_playback_hook(hinfo, codec, substream,
4506 HDA_GEN_PCM_ACT_CLOSE);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004507 mutex_unlock(&spec->pcm_mutex);
4508 return 0;
4509}
4510
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004511static int capture_pcm_open(struct hda_pcm_stream *hinfo,
4512 struct hda_codec *codec,
4513 struct snd_pcm_substream *substream)
4514{
4515 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_OPEN);
4516 return 0;
4517}
4518
4519static int capture_pcm_prepare(struct hda_pcm_stream *hinfo,
4520 struct hda_codec *codec,
4521 unsigned int stream_tag,
4522 unsigned int format,
4523 struct snd_pcm_substream *substream)
4524{
4525 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4526 call_pcm_capture_hook(hinfo, codec, substream,
4527 HDA_GEN_PCM_ACT_PREPARE);
4528 return 0;
4529}
4530
4531static int capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
4532 struct hda_codec *codec,
4533 struct snd_pcm_substream *substream)
4534{
4535 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
4536 call_pcm_capture_hook(hinfo, codec, substream,
4537 HDA_GEN_PCM_ACT_CLEANUP);
4538 return 0;
4539}
4540
4541static int capture_pcm_close(struct hda_pcm_stream *hinfo,
4542 struct hda_codec *codec,
4543 struct snd_pcm_substream *substream)
4544{
4545 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_CLOSE);
4546 return 0;
4547}
4548
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004549static int alt_playback_pcm_open(struct hda_pcm_stream *hinfo,
4550 struct hda_codec *codec,
4551 struct snd_pcm_substream *substream)
4552{
4553 struct hda_gen_spec *spec = codec->spec;
4554 int err = 0;
4555
4556 mutex_lock(&spec->pcm_mutex);
4557 if (!spec->indep_hp_enabled)
4558 err = -EBUSY;
4559 else
4560 spec->active_streams |= 1 << STREAM_INDEP_HP;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004561 call_pcm_playback_hook(hinfo, codec, substream,
4562 HDA_GEN_PCM_ACT_OPEN);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004563 mutex_unlock(&spec->pcm_mutex);
4564 return err;
4565}
4566
4567static int alt_playback_pcm_close(struct hda_pcm_stream *hinfo,
4568 struct hda_codec *codec,
4569 struct snd_pcm_substream *substream)
4570{
4571 struct hda_gen_spec *spec = codec->spec;
4572 mutex_lock(&spec->pcm_mutex);
4573 spec->active_streams &= ~(1 << STREAM_INDEP_HP);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004574 call_pcm_playback_hook(hinfo, codec, substream,
4575 HDA_GEN_PCM_ACT_CLOSE);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004576 mutex_unlock(&spec->pcm_mutex);
4577 return 0;
4578}
4579
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004580static int alt_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
4581 struct hda_codec *codec,
4582 unsigned int stream_tag,
4583 unsigned int format,
4584 struct snd_pcm_substream *substream)
4585{
4586 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4587 call_pcm_playback_hook(hinfo, codec, substream,
4588 HDA_GEN_PCM_ACT_PREPARE);
4589 return 0;
4590}
4591
4592static int alt_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
4593 struct hda_codec *codec,
4594 struct snd_pcm_substream *substream)
4595{
4596 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
4597 call_pcm_playback_hook(hinfo, codec, substream,
4598 HDA_GEN_PCM_ACT_CLEANUP);
4599 return 0;
4600}
4601
Takashi Iwai352f7f92012-12-19 12:52:06 +01004602/*
4603 * Digital out
4604 */
4605static int dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
4606 struct hda_codec *codec,
4607 struct snd_pcm_substream *substream)
4608{
4609 struct hda_gen_spec *spec = codec->spec;
4610 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
4611}
4612
4613static int dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
4614 struct hda_codec *codec,
4615 unsigned int stream_tag,
4616 unsigned int format,
4617 struct snd_pcm_substream *substream)
4618{
4619 struct hda_gen_spec *spec = codec->spec;
4620 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
4621 stream_tag, format, substream);
4622}
4623
4624static int dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
4625 struct hda_codec *codec,
4626 struct snd_pcm_substream *substream)
4627{
4628 struct hda_gen_spec *spec = codec->spec;
4629 return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
4630}
4631
4632static int dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
4633 struct hda_codec *codec,
4634 struct snd_pcm_substream *substream)
4635{
4636 struct hda_gen_spec *spec = codec->spec;
4637 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
4638}
4639
4640/*
4641 * Analog capture
4642 */
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004643#define alt_capture_pcm_open capture_pcm_open
4644#define alt_capture_pcm_close capture_pcm_close
4645
Takashi Iwai352f7f92012-12-19 12:52:06 +01004646static int alt_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
4647 struct hda_codec *codec,
4648 unsigned int stream_tag,
4649 unsigned int format,
4650 struct snd_pcm_substream *substream)
4651{
4652 struct hda_gen_spec *spec = codec->spec;
4653
4654 snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number + 1],
Takashi Iwai97ec5582006-03-21 11:29:07 +01004655 stream_tag, 0, format);
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004656 call_pcm_capture_hook(hinfo, codec, substream,
4657 HDA_GEN_PCM_ACT_PREPARE);
Takashi Iwai97ec5582006-03-21 11:29:07 +01004658 return 0;
4659}
4660
Takashi Iwai352f7f92012-12-19 12:52:06 +01004661static int alt_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
4662 struct hda_codec *codec,
4663 struct snd_pcm_substream *substream)
Takashi Iwai97ec5582006-03-21 11:29:07 +01004664{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004665 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai97ec5582006-03-21 11:29:07 +01004666
Takashi Iwai352f7f92012-12-19 12:52:06 +01004667 snd_hda_codec_cleanup_stream(codec,
4668 spec->adc_nids[substream->number + 1]);
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004669 call_pcm_capture_hook(hinfo, codec, substream,
4670 HDA_GEN_PCM_ACT_CLEANUP);
Takashi Iwai97ec5582006-03-21 11:29:07 +01004671 return 0;
4672}
4673
Takashi Iwai352f7f92012-12-19 12:52:06 +01004674/*
4675 */
4676static const struct hda_pcm_stream pcm_analog_playback = {
4677 .substreams = 1,
4678 .channels_min = 2,
4679 .channels_max = 8,
4680 /* NID is set in build_pcms */
4681 .ops = {
4682 .open = playback_pcm_open,
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004683 .close = playback_pcm_close,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004684 .prepare = playback_pcm_prepare,
4685 .cleanup = playback_pcm_cleanup
4686 },
4687};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004688
Takashi Iwai352f7f92012-12-19 12:52:06 +01004689static const struct hda_pcm_stream pcm_analog_capture = {
4690 .substreams = 1,
4691 .channels_min = 2,
4692 .channels_max = 2,
4693 /* NID is set in build_pcms */
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004694 .ops = {
4695 .open = capture_pcm_open,
4696 .close = capture_pcm_close,
4697 .prepare = capture_pcm_prepare,
4698 .cleanup = capture_pcm_cleanup
4699 },
Takashi Iwai352f7f92012-12-19 12:52:06 +01004700};
4701
4702static const struct hda_pcm_stream pcm_analog_alt_playback = {
4703 .substreams = 1,
4704 .channels_min = 2,
4705 .channels_max = 2,
4706 /* NID is set in build_pcms */
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004707 .ops = {
4708 .open = alt_playback_pcm_open,
Takashi Iwaie6b85f32013-01-07 11:54:34 +01004709 .close = alt_playback_pcm_close,
4710 .prepare = alt_playback_pcm_prepare,
4711 .cleanup = alt_playback_pcm_cleanup
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004712 },
Takashi Iwai352f7f92012-12-19 12:52:06 +01004713};
4714
4715static const struct hda_pcm_stream pcm_analog_alt_capture = {
4716 .substreams = 2, /* can be overridden */
4717 .channels_min = 2,
4718 .channels_max = 2,
4719 /* NID is set in build_pcms */
4720 .ops = {
Takashi Iwaiac2e8732013-01-17 15:57:10 +01004721 .open = alt_capture_pcm_open,
4722 .close = alt_capture_pcm_close,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004723 .prepare = alt_capture_pcm_prepare,
4724 .cleanup = alt_capture_pcm_cleanup
4725 },
4726};
4727
4728static const struct hda_pcm_stream pcm_digital_playback = {
4729 .substreams = 1,
4730 .channels_min = 2,
4731 .channels_max = 2,
4732 /* NID is set in build_pcms */
4733 .ops = {
4734 .open = dig_playback_pcm_open,
4735 .close = dig_playback_pcm_close,
4736 .prepare = dig_playback_pcm_prepare,
4737 .cleanup = dig_playback_pcm_cleanup
4738 },
4739};
4740
4741static const struct hda_pcm_stream pcm_digital_capture = {
4742 .substreams = 1,
4743 .channels_min = 2,
4744 .channels_max = 2,
4745 /* NID is set in build_pcms */
4746};
4747
4748/* Used by build_pcms to flag that a PCM has no playback stream */
4749static const struct hda_pcm_stream pcm_null_stream = {
4750 .substreams = 0,
4751 .channels_min = 0,
4752 .channels_max = 0,
4753};
4754
4755/*
4756 * dynamic changing ADC PCM streams
4757 */
4758static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur)
4759{
4760 struct hda_gen_spec *spec = codec->spec;
4761 hda_nid_t new_adc = spec->adc_nids[spec->dyn_adc_idx[cur]];
4762
4763 if (spec->cur_adc && spec->cur_adc != new_adc) {
4764 /* stream is running, let's swap the current ADC */
4765 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
4766 spec->cur_adc = new_adc;
4767 snd_hda_codec_setup_stream(codec, new_adc,
4768 spec->cur_adc_stream_tag, 0,
4769 spec->cur_adc_format);
4770 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004771 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01004772 return false;
4773}
4774
4775/* analog capture with dynamic dual-adc changes */
4776static int dyn_adc_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
4777 struct hda_codec *codec,
4778 unsigned int stream_tag,
4779 unsigned int format,
4780 struct snd_pcm_substream *substream)
4781{
4782 struct hda_gen_spec *spec = codec->spec;
4783 spec->cur_adc = spec->adc_nids[spec->dyn_adc_idx[spec->cur_mux[0]]];
4784 spec->cur_adc_stream_tag = stream_tag;
4785 spec->cur_adc_format = format;
4786 snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
4787 return 0;
4788}
4789
4790static int dyn_adc_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
4791 struct hda_codec *codec,
4792 struct snd_pcm_substream *substream)
4793{
4794 struct hda_gen_spec *spec = codec->spec;
4795 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
4796 spec->cur_adc = 0;
4797 return 0;
4798}
4799
4800static const struct hda_pcm_stream dyn_adc_pcm_analog_capture = {
4801 .substreams = 1,
4802 .channels_min = 2,
4803 .channels_max = 2,
4804 .nid = 0, /* fill later */
4805 .ops = {
4806 .prepare = dyn_adc_capture_pcm_prepare,
4807 .cleanup = dyn_adc_capture_pcm_cleanup
4808 },
4809};
4810
Takashi Iwaif873e532012-12-20 16:58:39 +01004811static void fill_pcm_stream_name(char *str, size_t len, const char *sfx,
4812 const char *chip_name)
4813{
4814 char *p;
4815
4816 if (*str)
4817 return;
4818 strlcpy(str, chip_name, len);
4819
4820 /* drop non-alnum chars after a space */
4821 for (p = strchr(str, ' '); p; p = strchr(p + 1, ' ')) {
4822 if (!isalnum(p[1])) {
4823 *p = 0;
4824 break;
4825 }
4826 }
4827 strlcat(str, sfx, len);
4828}
4829
Takashi Iwai352f7f92012-12-19 12:52:06 +01004830/* build PCM streams based on the parsed results */
4831int snd_hda_gen_build_pcms(struct hda_codec *codec)
4832{
4833 struct hda_gen_spec *spec = codec->spec;
4834 struct hda_pcm *info = spec->pcm_rec;
4835 const struct hda_pcm_stream *p;
4836 bool have_multi_adcs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004837
4838 codec->num_pcms = 1;
4839 codec->pcm_info = info;
4840
Takashi Iwai352f7f92012-12-19 12:52:06 +01004841 if (spec->no_analog)
4842 goto skip_analog;
4843
Takashi Iwaif873e532012-12-20 16:58:39 +01004844 fill_pcm_stream_name(spec->stream_name_analog,
4845 sizeof(spec->stream_name_analog),
4846 " Analog", codec->chip_name);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004847 info->name = spec->stream_name_analog;
4848
4849 if (spec->multiout.num_dacs > 0) {
4850 p = spec->stream_analog_playback;
4851 if (!p)
4852 p = &pcm_analog_playback;
4853 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
4854 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dac_nids[0];
4855 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
4856 spec->multiout.max_channels;
4857 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT &&
4858 spec->autocfg.line_outs == 2)
4859 info->stream[SNDRV_PCM_STREAM_PLAYBACK].chmap =
4860 snd_pcm_2_1_chmaps;
4861 }
4862 if (spec->num_adc_nids) {
4863 p = spec->stream_analog_capture;
4864 if (!p) {
4865 if (spec->dyn_adc_switch)
4866 p = &dyn_adc_pcm_analog_capture;
4867 else
4868 p = &pcm_analog_capture;
4869 }
4870 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
4871 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adc_nids[0];
4872 }
4873
Takashi Iwai352f7f92012-12-19 12:52:06 +01004874 skip_analog:
4875 /* SPDIF for stream index #1 */
4876 if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
Takashi Iwaif873e532012-12-20 16:58:39 +01004877 fill_pcm_stream_name(spec->stream_name_digital,
4878 sizeof(spec->stream_name_digital),
4879 " Digital", codec->chip_name);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004880 codec->num_pcms = 2;
4881 codec->slave_dig_outs = spec->multiout.slave_dig_outs;
4882 info = spec->pcm_rec + 1;
4883 info->name = spec->stream_name_digital;
4884 if (spec->dig_out_type)
4885 info->pcm_type = spec->dig_out_type;
4886 else
4887 info->pcm_type = HDA_PCM_TYPE_SPDIF;
4888 if (spec->multiout.dig_out_nid) {
4889 p = spec->stream_digital_playback;
4890 if (!p)
4891 p = &pcm_digital_playback;
4892 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
4893 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dig_out_nid;
4894 }
4895 if (spec->dig_in_nid) {
4896 p = spec->stream_digital_capture;
4897 if (!p)
4898 p = &pcm_digital_capture;
4899 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
4900 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in_nid;
4901 }
4902 }
4903
4904 if (spec->no_analog)
4905 return 0;
4906
4907 /* If the use of more than one ADC is requested for the current
4908 * model, configure a second analog capture-only PCM.
4909 */
4910 have_multi_adcs = (spec->num_adc_nids > 1) &&
4911 !spec->dyn_adc_switch && !spec->auto_mic;
4912 /* Additional Analaog capture for index #2 */
4913 if (spec->alt_dac_nid || have_multi_adcs) {
Takashi Iwaia6071482013-01-21 16:50:09 +01004914 fill_pcm_stream_name(spec->stream_name_alt_analog,
4915 sizeof(spec->stream_name_alt_analog),
4916 " Alt Analog", codec->chip_name);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004917 codec->num_pcms = 3;
4918 info = spec->pcm_rec + 2;
Takashi Iwaia6071482013-01-21 16:50:09 +01004919 info->name = spec->stream_name_alt_analog;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004920 if (spec->alt_dac_nid) {
4921 p = spec->stream_analog_alt_playback;
4922 if (!p)
4923 p = &pcm_analog_alt_playback;
4924 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
4925 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
4926 spec->alt_dac_nid;
4927 } else {
4928 info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
4929 pcm_null_stream;
4930 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = 0;
4931 }
4932 if (have_multi_adcs) {
4933 p = spec->stream_analog_alt_capture;
4934 if (!p)
4935 p = &pcm_analog_alt_capture;
4936 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
4937 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
4938 spec->adc_nids[1];
4939 info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams =
4940 spec->num_adc_nids - 1;
4941 } else {
4942 info->stream[SNDRV_PCM_STREAM_CAPTURE] =
4943 pcm_null_stream;
4944 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = 0;
4945 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004946 }
4947
4948 return 0;
4949}
Takashi Iwai352f7f92012-12-19 12:52:06 +01004950EXPORT_SYMBOL_HDA(snd_hda_gen_build_pcms);
4951
4952
4953/*
4954 * Standard auto-parser initializations
4955 */
4956
Takashi Iwaid4156932013-01-07 10:08:02 +01004957/* configure the given path as a proper output */
Takashi Iwai2c12c302013-01-10 09:33:29 +01004958static void set_output_and_unmute(struct hda_codec *codec, int path_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004959{
4960 struct nid_path *path;
Takashi Iwaid4156932013-01-07 10:08:02 +01004961 hda_nid_t pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004962
Takashi Iwai196c17662013-01-04 15:01:40 +01004963 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwaid4156932013-01-07 10:08:02 +01004964 if (!path || !path->depth)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004965 return;
Takashi Iwaid4156932013-01-07 10:08:02 +01004966 pin = path->path[path->depth - 1];
Takashi Iwai2c12c302013-01-10 09:33:29 +01004967 restore_pin_ctl(codec, pin);
Takashi Iwai65033cc2013-04-16 12:31:05 +02004968 snd_hda_activate_path(codec, path, path->active,
4969 aamix_default(codec->spec));
Takashi Iwaie1284af2013-01-03 16:33:02 +01004970 set_pin_eapd(codec, pin, path->active);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004971}
4972
4973/* initialize primary output paths */
4974static void init_multi_out(struct hda_codec *codec)
4975{
4976 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004977 int i;
4978
Takashi Iwaid4156932013-01-07 10:08:02 +01004979 for (i = 0; i < spec->autocfg.line_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01004980 set_output_and_unmute(codec, spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004981}
4982
Takashi Iwaidb23fd12012-12-20 15:27:24 +01004983
Takashi Iwai2c12c302013-01-10 09:33:29 +01004984static void __init_extra_out(struct hda_codec *codec, int num_outs, int *paths)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004985{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004986 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004987
Takashi Iwaid4156932013-01-07 10:08:02 +01004988 for (i = 0; i < num_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01004989 set_output_and_unmute(codec, paths[i]);
Takashi Iwaidb23fd12012-12-20 15:27:24 +01004990}
4991
4992/* initialize hp and speaker paths */
4993static void init_extra_out(struct hda_codec *codec)
4994{
4995 struct hda_gen_spec *spec = codec->spec;
4996
4997 if (spec->autocfg.line_out_type != AUTO_PIN_HP_OUT)
Takashi Iwai2c12c302013-01-10 09:33:29 +01004998 __init_extra_out(codec, spec->autocfg.hp_outs, spec->hp_paths);
Takashi Iwaidb23fd12012-12-20 15:27:24 +01004999 if (spec->autocfg.line_out_type != AUTO_PIN_SPEAKER_OUT)
5000 __init_extra_out(codec, spec->autocfg.speaker_outs,
Takashi Iwai2c12c302013-01-10 09:33:29 +01005001 spec->speaker_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005002}
5003
5004/* initialize multi-io paths */
5005static void init_multi_io(struct hda_codec *codec)
5006{
5007 struct hda_gen_spec *spec = codec->spec;
5008 int i;
5009
5010 for (i = 0; i < spec->multi_ios; i++) {
5011 hda_nid_t pin = spec->multi_io[i].pin;
5012 struct nid_path *path;
Takashi Iwai196c17662013-01-04 15:01:40 +01005013 path = get_multiio_path(codec, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005014 if (!path)
5015 continue;
5016 if (!spec->multi_io[i].ctl_in)
5017 spec->multi_io[i].ctl_in =
Takashi Iwai2c12c302013-01-10 09:33:29 +01005018 snd_hda_codec_get_pin_target(codec, pin);
Takashi Iwai65033cc2013-04-16 12:31:05 +02005019 snd_hda_activate_path(codec, path, path->active,
5020 aamix_default(spec));
Takashi Iwai352f7f92012-12-19 12:52:06 +01005021 }
5022}
5023
Takashi Iwai352f7f92012-12-19 12:52:06 +01005024/* set up input pins and loopback paths */
5025static void init_analog_input(struct hda_codec *codec)
5026{
5027 struct hda_gen_spec *spec = codec->spec;
5028 struct auto_pin_cfg *cfg = &spec->autocfg;
5029 int i;
5030
5031 for (i = 0; i < cfg->num_inputs; i++) {
5032 hda_nid_t nid = cfg->inputs[i].pin;
5033 if (is_input_pin(codec, nid))
Takashi Iwai2c12c302013-01-10 09:33:29 +01005034 restore_pin_ctl(codec, nid);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005035
5036 /* init loopback inputs */
5037 if (spec->mixer_nid) {
Takashi Iwai3e367f12013-01-23 17:07:23 +01005038 resume_path_from_idx(codec, spec->loopback_paths[i]);
5039 resume_path_from_idx(codec, spec->loopback_merge_path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005040 }
5041 }
5042}
5043
5044/* initialize ADC paths */
5045static void init_input_src(struct hda_codec *codec)
5046{
5047 struct hda_gen_spec *spec = codec->spec;
5048 struct hda_input_mux *imux = &spec->input_mux;
5049 struct nid_path *path;
5050 int i, c, nums;
5051
5052 if (spec->dyn_adc_switch)
5053 nums = 1;
5054 else
5055 nums = spec->num_adc_nids;
5056
5057 for (c = 0; c < nums; c++) {
5058 for (i = 0; i < imux->num_items; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01005059 path = get_input_path(codec, c, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005060 if (path) {
5061 bool active = path->active;
5062 if (i == spec->cur_mux[c])
5063 active = true;
5064 snd_hda_activate_path(codec, path, active, false);
5065 }
5066 }
Takashi Iwai967303d2013-02-19 17:12:42 +01005067 if (spec->hp_mic)
5068 update_hp_mic(codec, c, true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005069 }
5070
Takashi Iwai352f7f92012-12-19 12:52:06 +01005071 if (spec->cap_sync_hook)
Takashi Iwaia90229e2013-01-18 14:10:00 +01005072 spec->cap_sync_hook(codec, NULL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005073}
5074
5075/* set right pin controls for digital I/O */
5076static void init_digital(struct hda_codec *codec)
5077{
5078 struct hda_gen_spec *spec = codec->spec;
5079 int i;
5080 hda_nid_t pin;
5081
Takashi Iwaid4156932013-01-07 10:08:02 +01005082 for (i = 0; i < spec->autocfg.dig_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01005083 set_output_and_unmute(codec, spec->digout_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005084 pin = spec->autocfg.dig_in_pin;
Takashi Iwai2430d7b2013-01-04 15:09:42 +01005085 if (pin) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01005086 restore_pin_ctl(codec, pin);
Takashi Iwai3e367f12013-01-23 17:07:23 +01005087 resume_path_from_idx(codec, spec->digin_path);
Takashi Iwai2430d7b2013-01-04 15:09:42 +01005088 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01005089}
5090
Takashi Iwai973e4972012-12-20 15:16:09 +01005091/* clear unsol-event tags on unused pins; Conexant codecs seem to leave
5092 * invalid unsol tags by some reason
5093 */
5094static void clear_unsol_on_unused_pins(struct hda_codec *codec)
5095{
5096 int i;
5097
5098 for (i = 0; i < codec->init_pins.used; i++) {
5099 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
5100 hda_nid_t nid = pin->nid;
5101 if (is_jack_detectable(codec, nid) &&
5102 !snd_hda_jack_tbl_get(codec, nid))
5103 snd_hda_codec_update_cache(codec, nid, 0,
5104 AC_VERB_SET_UNSOLICITED_ENABLE, 0);
5105 }
5106}
5107
Takashi Iwai5187ac12013-01-07 12:52:16 +01005108/*
5109 * initialize the generic spec;
5110 * this can be put as patch_ops.init function
5111 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01005112int snd_hda_gen_init(struct hda_codec *codec)
5113{
5114 struct hda_gen_spec *spec = codec->spec;
5115
5116 if (spec->init_hook)
5117 spec->init_hook(codec);
5118
5119 snd_hda_apply_verbs(codec);
5120
Takashi Iwai3bbcd272012-12-20 11:50:58 +01005121 codec->cached_write = 1;
5122
Takashi Iwai352f7f92012-12-19 12:52:06 +01005123 init_multi_out(codec);
5124 init_extra_out(codec);
5125 init_multi_io(codec);
5126 init_analog_input(codec);
5127 init_input_src(codec);
5128 init_digital(codec);
5129
Takashi Iwai973e4972012-12-20 15:16:09 +01005130 clear_unsol_on_unused_pins(codec);
5131
Takashi Iwai352f7f92012-12-19 12:52:06 +01005132 /* call init functions of standard auto-mute helpers */
Takashi Iwaia5cc2502013-01-16 18:08:55 +01005133 update_automute_all(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005134
Takashi Iwaidc870f32013-01-22 15:24:30 +01005135 snd_hda_codec_flush_cache(codec);
Takashi Iwai3bbcd272012-12-20 11:50:58 +01005136
Takashi Iwai352f7f92012-12-19 12:52:06 +01005137 if (spec->vmaster_mute.sw_kctl && spec->vmaster_mute.hook)
5138 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
5139
5140 hda_call_check_power_status(codec, 0x01);
5141 return 0;
5142}
Takashi Iwaifce52a32013-01-07 12:42:48 +01005143EXPORT_SYMBOL_HDA(snd_hda_gen_init);
5144
Takashi Iwai5187ac12013-01-07 12:52:16 +01005145/*
5146 * free the generic spec;
5147 * this can be put as patch_ops.free function
5148 */
Takashi Iwaifce52a32013-01-07 12:42:48 +01005149void snd_hda_gen_free(struct hda_codec *codec)
5150{
Takashi Iwai7504b6c2013-03-18 11:25:51 +01005151 snd_hda_detach_beep_device(codec);
Takashi Iwaifce52a32013-01-07 12:42:48 +01005152 snd_hda_gen_spec_free(codec->spec);
5153 kfree(codec->spec);
5154 codec->spec = NULL;
5155}
5156EXPORT_SYMBOL_HDA(snd_hda_gen_free);
5157
5158#ifdef CONFIG_PM
Takashi Iwai5187ac12013-01-07 12:52:16 +01005159/*
5160 * check the loopback power save state;
5161 * this can be put as patch_ops.check_power_status function
5162 */
Takashi Iwaifce52a32013-01-07 12:42:48 +01005163int snd_hda_gen_check_power_status(struct hda_codec *codec, hda_nid_t nid)
5164{
5165 struct hda_gen_spec *spec = codec->spec;
5166 return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
5167}
5168EXPORT_SYMBOL_HDA(snd_hda_gen_check_power_status);
5169#endif
Takashi Iwai352f7f92012-12-19 12:52:06 +01005170
5171
5172/*
5173 * the generic codec support
5174 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005175
Takashi Iwai352f7f92012-12-19 12:52:06 +01005176static const struct hda_codec_ops generic_patch_ops = {
5177 .build_controls = snd_hda_gen_build_controls,
5178 .build_pcms = snd_hda_gen_build_pcms,
5179 .init = snd_hda_gen_init,
Takashi Iwaifce52a32013-01-07 12:42:48 +01005180 .free = snd_hda_gen_free,
Takashi Iwai352f7f92012-12-19 12:52:06 +01005181 .unsol_event = snd_hda_jack_unsol_event,
Takashi Iwai83012a72012-08-24 18:38:08 +02005182#ifdef CONFIG_PM
Takashi Iwaifce52a32013-01-07 12:42:48 +01005183 .check_power_status = snd_hda_gen_check_power_status,
Takashi Iwaicb53c622007-08-10 17:21:45 +02005184#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005185};
5186
Linus Torvalds1da177e2005-04-16 15:20:36 -07005187int snd_hda_parse_generic_codec(struct hda_codec *codec)
5188{
Takashi Iwai352f7f92012-12-19 12:52:06 +01005189 struct hda_gen_spec *spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005190 int err;
5191
Takashi Iwaie560d8d2005-09-09 14:21:46 +02005192 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005193 if (!spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005194 return -ENOMEM;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005195 snd_hda_gen_spec_init(spec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005196 codec->spec = spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005197
Takashi Iwai9eb413e2012-12-19 14:41:21 +01005198 err = snd_hda_parse_pin_defcfg(codec, &spec->autocfg, NULL, 0);
5199 if (err < 0)
5200 return err;
5201
5202 err = snd_hda_gen_parse_auto_config(codec, &spec->autocfg);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005203 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005204 goto error;
5205
5206 codec->patch_ops = generic_patch_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005207 return 0;
5208
Takashi Iwai352f7f92012-12-19 12:52:06 +01005209error:
Takashi Iwaifce52a32013-01-07 12:42:48 +01005210 snd_hda_gen_free(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005211 return err;
5212}
Takashi Iwaifce52a32013-01-07 12:42:48 +01005213EXPORT_SYMBOL_HDA(snd_hda_parse_generic_codec);