blob: f3c058f6c83125c8f0383de60c6a9e6b8b0a9e69 [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>
Takashi Iwaib21bdd02013-11-18 12:03:56 +010031#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <sound/core.h>
Takashi Iwai352f7f92012-12-19 12:52:06 +010033#include <sound/jack.h>
Takashi Iwaid89c6c02014-09-01 10:07:04 +020034#include <sound/tlv.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include "hda_codec.h"
36#include "hda_local.h"
Takashi Iwai352f7f92012-12-19 12:52:06 +010037#include "hda_auto_parser.h"
38#include "hda_jack.h"
Takashi Iwai7504b6c2013-03-18 11:25:51 +010039#include "hda_beep.h"
Takashi Iwai352f7f92012-12-19 12:52:06 +010040#include "hda_generic.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
Takashi Iwaidda42bd2014-10-30 12:04:50 +010043/**
44 * snd_hda_gen_spec_init - initialize hda_gen_spec struct
45 * @spec: hda_gen_spec object to initialize
46 *
47 * Initialize the given hda_gen_spec object.
48 */
Takashi Iwai352f7f92012-12-19 12:52:06 +010049int snd_hda_gen_spec_init(struct hda_gen_spec *spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -070050{
Takashi Iwai352f7f92012-12-19 12:52:06 +010051 snd_array_init(&spec->kctls, sizeof(struct snd_kcontrol_new), 32);
Takashi Iwai352f7f92012-12-19 12:52:06 +010052 snd_array_init(&spec->paths, sizeof(struct nid_path), 8);
Takashi Iwai0186f4f2013-02-07 10:45:11 +010053 snd_array_init(&spec->loopback_list, sizeof(struct hda_amp_list), 8);
Takashi Iwai38cf6f12012-12-21 14:09:42 +010054 mutex_init(&spec->pcm_mutex);
Takashi Iwai352f7f92012-12-19 12:52:06 +010055 return 0;
56}
Takashi Iwai2698ea92013-12-18 07:45:52 +010057EXPORT_SYMBOL_GPL(snd_hda_gen_spec_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Takashi Iwaidda42bd2014-10-30 12:04:50 +010059/**
60 * snd_hda_gen_add_kctl - Add a new kctl_new struct from the template
61 * @spec: hda_gen_spec object
62 * @name: name string to override the template, NULL if unchanged
63 * @temp: template for the new kctl
64 *
65 * Add a new kctl (actually snd_kcontrol_new to be instantiated later)
66 * element based on the given snd_kcontrol_new template @temp and the
67 * name string @name to the list in @spec.
68 * Returns the newly created object or NULL as error.
69 */
Takashi Iwai12c93df2012-12-19 14:38:33 +010070struct snd_kcontrol_new *
71snd_hda_gen_add_kctl(struct hda_gen_spec *spec, const char *name,
72 const struct snd_kcontrol_new *temp)
Takashi Iwai352f7f92012-12-19 12:52:06 +010073{
74 struct snd_kcontrol_new *knew = snd_array_new(&spec->kctls);
75 if (!knew)
76 return NULL;
77 *knew = *temp;
78 if (name)
79 knew->name = kstrdup(name, GFP_KERNEL);
80 else if (knew->name)
81 knew->name = kstrdup(knew->name, GFP_KERNEL);
82 if (!knew->name)
83 return NULL;
84 return knew;
85}
Takashi Iwai2698ea92013-12-18 07:45:52 +010086EXPORT_SYMBOL_GPL(snd_hda_gen_add_kctl);
Takashi Iwai352f7f92012-12-19 12:52:06 +010087
88static void free_kctls(struct hda_gen_spec *spec)
89{
90 if (spec->kctls.list) {
91 struct snd_kcontrol_new *kctl = spec->kctls.list;
92 int i;
93 for (i = 0; i < spec->kctls.used; i++)
94 kfree(kctl[i].name);
95 }
96 snd_array_free(&spec->kctls);
97}
98
Takashi Iwaia8dca462014-02-10 18:23:57 +010099static void snd_hda_gen_spec_free(struct hda_gen_spec *spec)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100100{
101 if (!spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 return;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100103 free_kctls(spec);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100104 snd_array_free(&spec->paths);
Takashi Iwai0186f4f2013-02-07 10:45:11 +0100105 snd_array_free(&spec->loopback_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
108/*
Takashi Iwai1c70a582013-01-11 17:48:22 +0100109 * store user hints
110 */
111static void parse_user_hints(struct hda_codec *codec)
112{
113 struct hda_gen_spec *spec = codec->spec;
114 int val;
115
116 val = snd_hda_get_bool_hint(codec, "jack_detect");
117 if (val >= 0)
118 codec->no_jack_detect = !val;
119 val = snd_hda_get_bool_hint(codec, "inv_jack_detect");
120 if (val >= 0)
121 codec->inv_jack_detect = !!val;
122 val = snd_hda_get_bool_hint(codec, "trigger_sense");
123 if (val >= 0)
124 codec->no_trigger_sense = !val;
125 val = snd_hda_get_bool_hint(codec, "inv_eapd");
126 if (val >= 0)
127 codec->inv_eapd = !!val;
128 val = snd_hda_get_bool_hint(codec, "pcm_format_first");
129 if (val >= 0)
130 codec->pcm_format_first = !!val;
131 val = snd_hda_get_bool_hint(codec, "sticky_stream");
132 if (val >= 0)
133 codec->no_sticky_stream = !val;
134 val = snd_hda_get_bool_hint(codec, "spdif_status_reset");
135 if (val >= 0)
136 codec->spdif_status_reset = !!val;
137 val = snd_hda_get_bool_hint(codec, "pin_amp_workaround");
138 if (val >= 0)
139 codec->pin_amp_workaround = !!val;
140 val = snd_hda_get_bool_hint(codec, "single_adc_amp");
141 if (val >= 0)
142 codec->single_adc_amp = !!val;
Takashi Iwai967b1302015-03-20 18:21:03 +0100143 val = snd_hda_get_bool_hint(codec, "power_save_node");
Takashi Iwaie6feb5d2015-03-16 21:32:11 +0100144 if (val >= 0)
Takashi Iwai967b1302015-03-20 18:21:03 +0100145 codec->power_save_node = !!val;
Takashi Iwai1c70a582013-01-11 17:48:22 +0100146
Takashi Iwaif72706b2013-01-16 18:20:07 +0100147 val = snd_hda_get_bool_hint(codec, "auto_mute");
148 if (val >= 0)
149 spec->suppress_auto_mute = !val;
Takashi Iwai1c70a582013-01-11 17:48:22 +0100150 val = snd_hda_get_bool_hint(codec, "auto_mic");
151 if (val >= 0)
152 spec->suppress_auto_mic = !val;
153 val = snd_hda_get_bool_hint(codec, "line_in_auto_switch");
154 if (val >= 0)
155 spec->line_in_auto_switch = !!val;
Takashi Iwai7eebffd2013-06-24 16:00:21 +0200156 val = snd_hda_get_bool_hint(codec, "auto_mute_via_amp");
157 if (val >= 0)
158 spec->auto_mute_via_amp = !!val;
Takashi Iwai1c70a582013-01-11 17:48:22 +0100159 val = snd_hda_get_bool_hint(codec, "need_dac_fix");
160 if (val >= 0)
161 spec->need_dac_fix = !!val;
162 val = snd_hda_get_bool_hint(codec, "primary_hp");
163 if (val >= 0)
164 spec->no_primary_hp = !val;
Takashi Iwaida96fb52013-07-29 16:54:36 +0200165 val = snd_hda_get_bool_hint(codec, "multi_io");
166 if (val >= 0)
167 spec->no_multi_io = !val;
Takashi Iwai1c70a582013-01-11 17:48:22 +0100168 val = snd_hda_get_bool_hint(codec, "multi_cap_vol");
169 if (val >= 0)
170 spec->multi_cap_vol = !!val;
171 val = snd_hda_get_bool_hint(codec, "inv_dmic_split");
172 if (val >= 0)
173 spec->inv_dmic_split = !!val;
174 val = snd_hda_get_bool_hint(codec, "indep_hp");
175 if (val >= 0)
176 spec->indep_hp = !!val;
177 val = snd_hda_get_bool_hint(codec, "add_stereo_mix_input");
178 if (val >= 0)
179 spec->add_stereo_mix_input = !!val;
Takashi Iwaif811c3c2013-03-07 18:32:59 +0100180 /* the following two are just for compatibility */
Takashi Iwai1c70a582013-01-11 17:48:22 +0100181 val = snd_hda_get_bool_hint(codec, "add_out_jack_modes");
182 if (val >= 0)
Takashi Iwaif811c3c2013-03-07 18:32:59 +0100183 spec->add_jack_modes = !!val;
Takashi Iwai294765582013-01-17 09:52:11 +0100184 val = snd_hda_get_bool_hint(codec, "add_in_jack_modes");
185 if (val >= 0)
Takashi Iwaif811c3c2013-03-07 18:32:59 +0100186 spec->add_jack_modes = !!val;
187 val = snd_hda_get_bool_hint(codec, "add_jack_modes");
188 if (val >= 0)
189 spec->add_jack_modes = !!val;
Takashi Iwai55196ff2013-01-24 17:32:56 +0100190 val = snd_hda_get_bool_hint(codec, "power_down_unused");
191 if (val >= 0)
192 spec->power_down_unused = !!val;
Takashi Iwai967303d2013-02-19 17:12:42 +0100193 val = snd_hda_get_bool_hint(codec, "add_hp_mic");
194 if (val >= 0)
195 spec->hp_mic = !!val;
196 val = snd_hda_get_bool_hint(codec, "hp_mic_detect");
197 if (val >= 0)
198 spec->suppress_hp_mic_detect = !val;
Takashi Iwai1c70a582013-01-11 17:48:22 +0100199
200 if (!snd_hda_get_int_hint(codec, "mixer_nid", &val))
201 spec->mixer_nid = val;
202}
203
204/*
Takashi Iwai2c12c302013-01-10 09:33:29 +0100205 * pin control value accesses
206 */
207
208#define update_pin_ctl(codec, pin, val) \
209 snd_hda_codec_update_cache(codec, pin, 0, \
210 AC_VERB_SET_PIN_WIDGET_CONTROL, val)
211
212/* restore the pinctl based on the cached value */
213static inline void restore_pin_ctl(struct hda_codec *codec, hda_nid_t pin)
214{
215 update_pin_ctl(codec, pin, snd_hda_codec_get_pin_target(codec, pin));
216}
217
218/* set the pinctl target value and write it if requested */
219static void set_pin_target(struct hda_codec *codec, hda_nid_t pin,
220 unsigned int val, bool do_write)
221{
222 if (!pin)
223 return;
224 val = snd_hda_correct_pin_ctl(codec, pin, val);
225 snd_hda_codec_set_pin_target(codec, pin, val);
226 if (do_write)
227 update_pin_ctl(codec, pin, val);
228}
229
230/* set pinctl target values for all given pins */
231static void set_pin_targets(struct hda_codec *codec, int num_pins,
232 hda_nid_t *pins, unsigned int val)
233{
234 int i;
235 for (i = 0; i < num_pins; i++)
236 set_pin_target(codec, pins[i], val, false);
237}
238
239/*
Takashi Iwai352f7f92012-12-19 12:52:06 +0100240 * parsing paths
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100243/* return the position of NID in the list, or -1 if not found */
244static int find_idx_in_nid_list(hda_nid_t nid, const hda_nid_t *list, int nums)
245{
246 int i;
247 for (i = 0; i < nums; i++)
248 if (list[i] == nid)
249 return i;
250 return -1;
251}
252
253/* return true if the given NID is contained in the path */
254static bool is_nid_contained(struct nid_path *path, hda_nid_t nid)
255{
256 return find_idx_in_nid_list(nid, path->path, path->depth) >= 0;
257}
258
Takashi Iwaif5172a72013-01-04 13:19:55 +0100259static struct nid_path *get_nid_path(struct hda_codec *codec,
260 hda_nid_t from_nid, hda_nid_t to_nid,
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100261 int anchor_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100263 struct hda_gen_spec *spec = codec->spec;
264 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
Takashi Iwai352f7f92012-12-19 12:52:06 +0100266 for (i = 0; i < spec->paths.used; i++) {
267 struct nid_path *path = snd_array_elem(&spec->paths, i);
268 if (path->depth <= 0)
269 continue;
270 if ((!from_nid || path->path[0] == from_nid) &&
Takashi Iwaif5172a72013-01-04 13:19:55 +0100271 (!to_nid || path->path[path->depth - 1] == to_nid)) {
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100272 if (!anchor_nid ||
273 (anchor_nid > 0 && is_nid_contained(path, anchor_nid)) ||
274 (anchor_nid < 0 && !is_nid_contained(path, anchor_nid)))
Takashi Iwaif5172a72013-01-04 13:19:55 +0100275 return path;
276 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 }
278 return NULL;
279}
Takashi Iwaif5172a72013-01-04 13:19:55 +0100280
Takashi Iwaidda42bd2014-10-30 12:04:50 +0100281/**
Takashi Iwaidda42bd2014-10-30 12:04:50 +0100282 * snd_hda_get_path_idx - get the index number corresponding to the path
283 * instance
284 * @codec: the HDA codec
285 * @path: nid_path object
286 *
287 * The returned index starts from 1, i.e. the actual array index with offset 1,
288 * and zero is handled as an invalid path
Takashi Iwai196c17662013-01-04 15:01:40 +0100289 */
290int snd_hda_get_path_idx(struct hda_codec *codec, struct nid_path *path)
291{
292 struct hda_gen_spec *spec = codec->spec;
293 struct nid_path *array = spec->paths.list;
294 ssize_t idx;
295
296 if (!spec->paths.used)
297 return 0;
298 idx = path - array;
299 if (idx < 0 || idx >= spec->paths.used)
300 return 0;
301 return idx + 1;
302}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100303EXPORT_SYMBOL_GPL(snd_hda_get_path_idx);
Takashi Iwai196c17662013-01-04 15:01:40 +0100304
Takashi Iwaidda42bd2014-10-30 12:04:50 +0100305/**
306 * snd_hda_get_path_from_idx - get the path instance corresponding to the
307 * given index number
308 * @codec: the HDA codec
309 * @idx: the path index
310 */
Takashi Iwai196c17662013-01-04 15:01:40 +0100311struct nid_path *snd_hda_get_path_from_idx(struct hda_codec *codec, int idx)
312{
313 struct hda_gen_spec *spec = codec->spec;
314
315 if (idx <= 0 || idx > spec->paths.used)
316 return NULL;
317 return snd_array_elem(&spec->paths, idx - 1);
318}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100319EXPORT_SYMBOL_GPL(snd_hda_get_path_from_idx);
Takashi Iwai196c17662013-01-04 15:01:40 +0100320
Takashi Iwai352f7f92012-12-19 12:52:06 +0100321/* check whether the given DAC is already found in any existing paths */
322static bool is_dac_already_used(struct hda_codec *codec, hda_nid_t nid)
323{
324 struct hda_gen_spec *spec = codec->spec;
325 int i;
326
327 for (i = 0; i < spec->paths.used; i++) {
328 struct nid_path *path = snd_array_elem(&spec->paths, i);
329 if (path->path[0] == nid)
330 return true;
331 }
332 return false;
333}
334
335/* check whether the given two widgets can be connected */
336static bool is_reachable_path(struct hda_codec *codec,
337 hda_nid_t from_nid, hda_nid_t to_nid)
338{
339 if (!from_nid || !to_nid)
340 return false;
341 return snd_hda_get_conn_index(codec, to_nid, from_nid, true) >= 0;
342}
343
344/* nid, dir and idx */
345#define AMP_VAL_COMPARE_MASK (0xffff | (1U << 18) | (0x0f << 19))
346
347/* check whether the given ctl is already assigned in any path elements */
348static bool is_ctl_used(struct hda_codec *codec, unsigned int val, int type)
349{
350 struct hda_gen_spec *spec = codec->spec;
351 int i;
352
353 val &= AMP_VAL_COMPARE_MASK;
354 for (i = 0; i < spec->paths.used; i++) {
355 struct nid_path *path = snd_array_elem(&spec->paths, i);
356 if ((path->ctls[type] & AMP_VAL_COMPARE_MASK) == val)
357 return true;
358 }
359 return false;
360}
361
362/* check whether a control with the given (nid, dir, idx) was assigned */
363static bool is_ctl_associated(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwai8999bf02013-01-18 11:01:33 +0100364 int dir, int idx, int type)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100365{
366 unsigned int val = HDA_COMPOSE_AMP_VAL(nid, 3, idx, dir);
Takashi Iwai8999bf02013-01-18 11:01:33 +0100367 return is_ctl_used(codec, val, type);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100368}
369
Takashi Iwai4e76a882014-02-25 12:21:03 +0100370static void print_nid_path(struct hda_codec *codec,
371 const char *pfx, struct nid_path *path)
Takashi Iwai0c8c0f52012-12-20 17:54:22 +0100372{
373 char buf[40];
Joe Perchesd82353e2014-07-05 13:02:02 -0700374 char *pos = buf;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +0100375 int i;
376
Joe Perchesd82353e2014-07-05 13:02:02 -0700377 *pos = 0;
378 for (i = 0; i < path->depth; i++)
379 pos += scnprintf(pos, sizeof(buf) - (pos - buf), "%s%02x",
380 pos != buf ? ":" : "",
381 path->path[i]);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +0100382
Joe Perchesd82353e2014-07-05 13:02:02 -0700383 codec_dbg(codec, "%s path: depth=%d '%s'\n", pfx, path->depth, buf);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +0100384}
385
Takashi Iwai352f7f92012-12-19 12:52:06 +0100386/* called recursively */
387static bool __parse_nid_path(struct hda_codec *codec,
388 hda_nid_t from_nid, hda_nid_t to_nid,
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100389 int anchor_nid, struct nid_path *path,
390 int depth)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100391{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100392 const hda_nid_t *conn;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100393 int i, nums;
394
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100395 if (to_nid == anchor_nid)
396 anchor_nid = 0; /* anchor passed */
397 else if (to_nid == (hda_nid_t)(-anchor_nid))
398 return false; /* hit the exclusive nid */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100399
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100400 nums = snd_hda_get_conn_list(codec, to_nid, &conn);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100401 for (i = 0; i < nums; i++) {
402 if (conn[i] != from_nid) {
403 /* special case: when from_nid is 0,
404 * try to find an empty DAC
405 */
406 if (from_nid ||
407 get_wcaps_type(get_wcaps(codec, conn[i])) != AC_WID_AUD_OUT ||
408 is_dac_already_used(codec, conn[i]))
409 continue;
410 }
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100411 /* anchor is not requested or already passed? */
412 if (anchor_nid <= 0)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100413 goto found;
414 }
415 if (depth >= MAX_NID_PATH_DEPTH)
416 return false;
417 for (i = 0; i < nums; i++) {
418 unsigned int type;
419 type = get_wcaps_type(get_wcaps(codec, conn[i]));
420 if (type == AC_WID_AUD_OUT || type == AC_WID_AUD_IN ||
421 type == AC_WID_PIN)
422 continue;
423 if (__parse_nid_path(codec, from_nid, conn[i],
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100424 anchor_nid, path, depth + 1))
Takashi Iwai352f7f92012-12-19 12:52:06 +0100425 goto found;
426 }
427 return false;
428
429 found:
430 path->path[path->depth] = conn[i];
431 path->idx[path->depth + 1] = i;
432 if (nums > 1 && get_wcaps_type(get_wcaps(codec, to_nid)) != AC_WID_AUD_MIX)
433 path->multi[path->depth + 1] = 1;
434 path->depth++;
435 return true;
436}
437
Takashi Iwaic4a58c32015-12-08 11:48:39 +0100438/*
Takashi Iwaidda42bd2014-10-30 12:04:50 +0100439 * snd_hda_parse_nid_path - parse the widget path from the given nid to
440 * the target nid
441 * @codec: the HDA codec
442 * @from_nid: the NID where the path start from
443 * @to_nid: the NID where the path ends at
444 * @anchor_nid: the anchor indication
445 * @path: the path object to store the result
446 *
447 * Returns true if a matching path is found.
448 *
449 * The parsing behavior depends on parameters:
Takashi Iwai352f7f92012-12-19 12:52:06 +0100450 * when @from_nid is 0, try to find an empty DAC;
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100451 * when @anchor_nid is set to a positive value, only paths through the widget
452 * with the given value are evaluated.
453 * when @anchor_nid is set to a negative value, paths through the widget
454 * with the negative of given value are excluded, only other paths are chosen.
455 * when @anchor_nid is zero, no special handling about path selection.
Takashi Iwai352f7f92012-12-19 12:52:06 +0100456 */
Takashi Iwaic4a58c32015-12-08 11:48:39 +0100457static bool snd_hda_parse_nid_path(struct hda_codec *codec, hda_nid_t from_nid,
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100458 hda_nid_t to_nid, int anchor_nid,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100459 struct nid_path *path)
460{
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100461 if (__parse_nid_path(codec, from_nid, to_nid, anchor_nid, path, 1)) {
Takashi Iwai352f7f92012-12-19 12:52:06 +0100462 path->path[path->depth] = to_nid;
463 path->depth++;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100464 return true;
465 }
466 return false;
467}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
Takashi Iwaidda42bd2014-10-30 12:04:50 +0100469/**
470 * snd_hda_add_new_path - parse the path between the given NIDs and
471 * add to the path list
472 * @codec: the HDA codec
473 * @from_nid: the NID where the path start from
474 * @to_nid: the NID where the path ends at
475 * @anchor_nid: the anchor indication, see snd_hda_parse_nid_path()
476 *
477 * If no valid path is found, returns NULL.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100479struct nid_path *
480snd_hda_add_new_path(struct hda_codec *codec, hda_nid_t from_nid,
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100481 hda_nid_t to_nid, int anchor_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100483 struct hda_gen_spec *spec = codec->spec;
484 struct nid_path *path;
485
486 if (from_nid && to_nid && !is_reachable_path(codec, from_nid, to_nid))
487 return NULL;
488
Takashi Iwaif5172a72013-01-04 13:19:55 +0100489 /* check whether the path has been already added */
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100490 path = get_nid_path(codec, from_nid, to_nid, anchor_nid);
Takashi Iwaif5172a72013-01-04 13:19:55 +0100491 if (path)
492 return path;
493
Takashi Iwai352f7f92012-12-19 12:52:06 +0100494 path = snd_array_new(&spec->paths);
495 if (!path)
496 return NULL;
497 memset(path, 0, sizeof(*path));
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100498 if (snd_hda_parse_nid_path(codec, from_nid, to_nid, anchor_nid, path))
Takashi Iwai352f7f92012-12-19 12:52:06 +0100499 return path;
500 /* push back */
501 spec->paths.used--;
502 return NULL;
503}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100504EXPORT_SYMBOL_GPL(snd_hda_add_new_path);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100505
Takashi Iwai980428c2013-01-09 09:28:20 +0100506/* clear the given path as invalid so that it won't be picked up later */
507static void invalidate_nid_path(struct hda_codec *codec, int idx)
508{
509 struct nid_path *path = snd_hda_get_path_from_idx(codec, idx);
510 if (!path)
511 return;
512 memset(path, 0, sizeof(*path));
513}
514
Takashi Iwai36907392013-12-10 17:29:26 +0100515/* return a DAC if paired to the given pin by codec driver */
516static hda_nid_t get_preferred_dac(struct hda_codec *codec, hda_nid_t pin)
517{
518 struct hda_gen_spec *spec = codec->spec;
519 const hda_nid_t *list = spec->preferred_dacs;
520
521 if (!list)
522 return 0;
523 for (; *list; list += 2)
524 if (*list == pin)
525 return list[1];
526 return 0;
527}
528
Takashi Iwai352f7f92012-12-19 12:52:06 +0100529/* look for an empty DAC slot */
530static hda_nid_t look_for_dac(struct hda_codec *codec, hda_nid_t pin,
531 bool is_digital)
532{
533 struct hda_gen_spec *spec = codec->spec;
534 bool cap_digital;
535 int i;
536
537 for (i = 0; i < spec->num_all_dacs; i++) {
538 hda_nid_t nid = spec->all_dacs[i];
539 if (!nid || is_dac_already_used(codec, nid))
540 continue;
541 cap_digital = !!(get_wcaps(codec, nid) & AC_WCAP_DIGITAL);
542 if (is_digital != cap_digital)
543 continue;
544 if (is_reachable_path(codec, nid, pin))
545 return nid;
546 }
547 return 0;
548}
549
550/* replace the channels in the composed amp value with the given number */
551static unsigned int amp_val_replace_channels(unsigned int val, unsigned int chs)
552{
553 val &= ~(0x3U << 16);
554 val |= chs << 16;
555 return val;
556}
557
David Henningsson99a55922013-01-16 15:58:44 +0100558static bool same_amp_caps(struct hda_codec *codec, hda_nid_t nid1,
559 hda_nid_t nid2, int dir)
560{
561 if (!(get_wcaps(codec, nid1) & (1 << (dir + 1))))
562 return !(get_wcaps(codec, nid2) & (1 << (dir + 1)));
563 return (query_amp_caps(codec, nid1, dir) ==
564 query_amp_caps(codec, nid2, dir));
565}
566
Takashi Iwai352f7f92012-12-19 12:52:06 +0100567/* look for a widget suitable for assigning a mute switch in the path */
568static hda_nid_t look_for_out_mute_nid(struct hda_codec *codec,
569 struct nid_path *path)
570{
571 int i;
572
573 for (i = path->depth - 1; i >= 0; i--) {
574 if (nid_has_mute(codec, path->path[i], HDA_OUTPUT))
575 return path->path[i];
576 if (i != path->depth - 1 && i != 0 &&
577 nid_has_mute(codec, path->path[i], HDA_INPUT))
578 return path->path[i];
579 }
580 return 0;
581}
582
583/* look for a widget suitable for assigning a volume ctl in the path */
584static hda_nid_t look_for_out_vol_nid(struct hda_codec *codec,
585 struct nid_path *path)
586{
Takashi Iwaia1114a82013-11-04 16:32:01 +0100587 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100588 int i;
589
590 for (i = path->depth - 1; i >= 0; i--) {
Takashi Iwaia1114a82013-11-04 16:32:01 +0100591 hda_nid_t nid = path->path[i];
592 if ((spec->out_vol_mask >> nid) & 1)
593 continue;
594 if (nid_has_volume(codec, nid, HDA_OUTPUT))
595 return nid;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100596 }
Takashi Iwai82beb8f2007-08-10 17:09:26 +0200597 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598}
599
600/*
Takashi Iwai352f7f92012-12-19 12:52:06 +0100601 * path activation / deactivation
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100603
604/* can have the amp-in capability? */
605static bool has_amp_in(struct hda_codec *codec, struct nid_path *path, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100607 hda_nid_t nid = path->path[idx];
608 unsigned int caps = get_wcaps(codec, nid);
609 unsigned int type = get_wcaps_type(caps);
610
611 if (!(caps & AC_WCAP_IN_AMP))
612 return false;
613 if (type == AC_WID_PIN && idx > 0) /* only for input pins */
614 return false;
615 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616}
617
Takashi Iwai352f7f92012-12-19 12:52:06 +0100618/* can have the amp-out capability? */
619static bool has_amp_out(struct hda_codec *codec, struct nid_path *path, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100621 hda_nid_t nid = path->path[idx];
622 unsigned int caps = get_wcaps(codec, nid);
623 unsigned int type = get_wcaps_type(caps);
624
625 if (!(caps & AC_WCAP_OUT_AMP))
626 return false;
627 if (type == AC_WID_PIN && !idx) /* only for output pins */
628 return false;
629 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630}
631
Takashi Iwai352f7f92012-12-19 12:52:06 +0100632/* check whether the given (nid,dir,idx) is active */
633static bool is_active_nid(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwai7dddf2ae2013-01-24 16:31:35 +0100634 unsigned int dir, unsigned int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100636 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaie6feb5d2015-03-16 21:32:11 +0100637 int type = get_wcaps_type(get_wcaps(codec, nid));
Takashi Iwai352f7f92012-12-19 12:52:06 +0100638 int i, n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639
Takashi Iwai7639a062015-03-03 10:07:24 +0100640 if (nid == codec->core.afg)
Takashi Iwai5ccf8352015-03-18 09:23:10 +0100641 return true;
642
Takashi Iwai352f7f92012-12-19 12:52:06 +0100643 for (n = 0; n < spec->paths.used; n++) {
644 struct nid_path *path = snd_array_elem(&spec->paths, n);
645 if (!path->active)
646 continue;
Takashi Iwai967b1302015-03-20 18:21:03 +0100647 if (codec->power_save_node) {
Takashi Iwaie6feb5d2015-03-16 21:32:11 +0100648 if (!path->stream_enabled)
649 continue;
650 /* ignore unplugged paths except for DAC/ADC */
Takashi Iwai6b275b12015-03-20 18:11:05 +0100651 if (!(path->pin_enabled || path->pin_fixed) &&
Takashi Iwaie6feb5d2015-03-16 21:32:11 +0100652 type != AC_WID_AUD_OUT && type != AC_WID_AUD_IN)
653 continue;
654 }
Takashi Iwai352f7f92012-12-19 12:52:06 +0100655 for (i = 0; i < path->depth; i++) {
656 if (path->path[i] == nid) {
Takashi Iwai9d2b48f2015-08-24 10:45:27 +0200657 if (dir == HDA_OUTPUT || idx == -1 ||
658 path->idx[i] == idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100659 return true;
660 break;
661 }
662 }
663 }
664 return false;
665}
666
Takashi Iwaib1b9fbd2013-05-14 12:58:47 +0200667/* check whether the NID is referred by any active paths */
668#define is_active_nid_for_any(codec, nid) \
Takashi Iwai9d2b48f2015-08-24 10:45:27 +0200669 is_active_nid(codec, nid, HDA_OUTPUT, -1)
Takashi Iwaib1b9fbd2013-05-14 12:58:47 +0200670
Takashi Iwai352f7f92012-12-19 12:52:06 +0100671/* get the default amp value for the target state */
672static int get_amp_val_to_activate(struct hda_codec *codec, hda_nid_t nid,
Takashi Iwai8999bf02013-01-18 11:01:33 +0100673 int dir, unsigned int caps, bool enable)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100674{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100675 unsigned int val = 0;
676
Takashi Iwai352f7f92012-12-19 12:52:06 +0100677 if (caps & AC_AMPCAP_NUM_STEPS) {
678 /* set to 0dB */
679 if (enable)
680 val = (caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT;
681 }
Takashi Iwaif69910d2013-08-08 09:32:37 +0200682 if (caps & (AC_AMPCAP_MUTE | AC_AMPCAP_MIN_MUTE)) {
Takashi Iwai352f7f92012-12-19 12:52:06 +0100683 if (!enable)
684 val |= HDA_AMP_MUTE;
685 }
686 return val;
687}
688
Takashi Iwaicc261732015-03-16 10:18:08 +0100689/* is this a stereo widget or a stereo-to-mono mix? */
690static bool is_stereo_amps(struct hda_codec *codec, hda_nid_t nid, int dir)
691{
692 unsigned int wcaps = get_wcaps(codec, nid);
693 hda_nid_t conn;
694
695 if (wcaps & AC_WCAP_STEREO)
696 return true;
697 if (dir != HDA_INPUT || get_wcaps_type(wcaps) != AC_WID_AUD_MIX)
698 return false;
699 if (snd_hda_get_num_conns(codec, nid) != 1)
700 return false;
701 if (snd_hda_get_connections(codec, nid, &conn, 1) < 0)
702 return false;
703 return !!(get_wcaps(codec, conn) & AC_WCAP_STEREO);
704}
705
Takashi Iwai352f7f92012-12-19 12:52:06 +0100706/* initialize the amp value (only at the first time) */
707static void init_amp(struct hda_codec *codec, hda_nid_t nid, int dir, int idx)
708{
Takashi Iwai8999bf02013-01-18 11:01:33 +0100709 unsigned int caps = query_amp_caps(codec, nid, dir);
710 int val = get_amp_val_to_activate(codec, nid, dir, caps, false);
Takashi Iwaief403ed2015-03-12 08:30:11 +0100711
Takashi Iwaicc261732015-03-16 10:18:08 +0100712 if (is_stereo_amps(codec, nid, dir))
Takashi Iwaief403ed2015-03-12 08:30:11 +0100713 snd_hda_codec_amp_init_stereo(codec, nid, dir, idx, 0xff, val);
714 else
715 snd_hda_codec_amp_init(codec, nid, 0, dir, idx, 0xff, val);
716}
717
718/* update the amp, doing in stereo or mono depending on NID */
719static int update_amp(struct hda_codec *codec, hda_nid_t nid, int dir, int idx,
720 unsigned int mask, unsigned int val)
721{
Takashi Iwaicc261732015-03-16 10:18:08 +0100722 if (is_stereo_amps(codec, nid, dir))
Takashi Iwaief403ed2015-03-12 08:30:11 +0100723 return snd_hda_codec_amp_stereo(codec, nid, dir, idx,
724 mask, val);
725 else
726 return snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
727 mask, val);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100728}
729
Takashi Iwai8999bf02013-01-18 11:01:33 +0100730/* calculate amp value mask we can modify;
731 * if the given amp is controlled by mixers, don't touch it
732 */
733static unsigned int get_amp_mask_to_modify(struct hda_codec *codec,
734 hda_nid_t nid, int dir, int idx,
735 unsigned int caps)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100736{
Takashi Iwai8999bf02013-01-18 11:01:33 +0100737 unsigned int mask = 0xff;
738
Takashi Iwaif69910d2013-08-08 09:32:37 +0200739 if (caps & (AC_AMPCAP_MUTE | AC_AMPCAP_MIN_MUTE)) {
Takashi Iwai8999bf02013-01-18 11:01:33 +0100740 if (is_ctl_associated(codec, nid, dir, idx, NID_PATH_MUTE_CTL))
741 mask &= ~0x80;
742 }
743 if (caps & AC_AMPCAP_NUM_STEPS) {
744 if (is_ctl_associated(codec, nid, dir, idx, NID_PATH_VOL_CTL) ||
745 is_ctl_associated(codec, nid, dir, idx, NID_PATH_BOOST_CTL))
746 mask &= ~0x7f;
747 }
748 return mask;
749}
750
751static void activate_amp(struct hda_codec *codec, hda_nid_t nid, int dir,
752 int idx, int idx_to_check, bool enable)
753{
754 unsigned int caps;
755 unsigned int mask, val;
756
Takashi Iwai7dddf2ae2013-01-24 16:31:35 +0100757 if (!enable && is_active_nid(codec, nid, dir, idx_to_check))
Takashi Iwai352f7f92012-12-19 12:52:06 +0100758 return;
Takashi Iwai8999bf02013-01-18 11:01:33 +0100759
760 caps = query_amp_caps(codec, nid, dir);
761 val = get_amp_val_to_activate(codec, nid, dir, caps, enable);
762 mask = get_amp_mask_to_modify(codec, nid, dir, idx_to_check, caps);
763 if (!mask)
764 return;
765
766 val &= mask;
Takashi Iwaief403ed2015-03-12 08:30:11 +0100767 update_amp(codec, nid, dir, idx, mask, val);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100768}
769
770static void activate_amp_out(struct hda_codec *codec, struct nid_path *path,
771 int i, bool enable)
772{
773 hda_nid_t nid = path->path[i];
774 init_amp(codec, nid, HDA_OUTPUT, 0);
Takashi Iwai8999bf02013-01-18 11:01:33 +0100775 activate_amp(codec, nid, HDA_OUTPUT, 0, 0, enable);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100776}
777
778static void activate_amp_in(struct hda_codec *codec, struct nid_path *path,
779 int i, bool enable, bool add_aamix)
780{
781 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100782 const hda_nid_t *conn;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100783 int n, nums, idx;
784 int type;
785 hda_nid_t nid = path->path[i];
786
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100787 nums = snd_hda_get_conn_list(codec, nid, &conn);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100788 type = get_wcaps_type(get_wcaps(codec, nid));
789 if (type == AC_WID_PIN ||
790 (type == AC_WID_AUD_IN && codec->single_adc_amp)) {
791 nums = 1;
792 idx = 0;
793 } else
794 idx = path->idx[i];
795
796 for (n = 0; n < nums; n++)
797 init_amp(codec, nid, HDA_INPUT, n);
798
Takashi Iwai352f7f92012-12-19 12:52:06 +0100799 /* here is a little bit tricky in comparison with activate_amp_out();
800 * when aa-mixer is available, we need to enable the path as well
801 */
802 for (n = 0; n < nums; n++) {
Takashi Iwaie4a395e2013-01-23 17:00:31 +0100803 if (n != idx && (!add_aamix || conn[n] != spec->mixer_merge_nid))
Takashi Iwai352f7f92012-12-19 12:52:06 +0100804 continue;
Takashi Iwai8999bf02013-01-18 11:01:33 +0100805 activate_amp(codec, nid, HDA_INPUT, n, idx, enable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 }
807}
808
Takashi Iwaie6feb5d2015-03-16 21:32:11 +0100809/* sync power of each widget in the the given path */
810static hda_nid_t path_power_update(struct hda_codec *codec,
811 struct nid_path *path,
812 bool allow_powerdown)
813{
814 hda_nid_t nid, changed = 0;
815 int i, state;
816
817 for (i = 0; i < path->depth; i++) {
818 nid = path->path[i];
Takashi Iwai2206dc92015-04-09 10:18:31 +0200819 if (!(get_wcaps(codec, nid) & AC_WCAP_POWER))
820 continue;
Takashi Iwai7639a062015-03-03 10:07:24 +0100821 if (nid == codec->core.afg)
Takashi Iwai5ccf8352015-03-18 09:23:10 +0100822 continue;
Takashi Iwaie6feb5d2015-03-16 21:32:11 +0100823 if (!allow_powerdown || is_active_nid_for_any(codec, nid))
824 state = AC_PWRST_D0;
825 else
826 state = AC_PWRST_D3;
827 if (!snd_hda_check_power_state(codec, nid, state)) {
828 snd_hda_codec_write(codec, nid, 0,
829 AC_VERB_SET_POWER_STATE, state);
830 changed = nid;
Takashi Iwai48f4b3a2015-05-20 06:49:37 +0200831 /* all known codecs seem to be capable to handl
832 * widgets state even in D3, so far.
833 * if any new codecs need to restore the widget
834 * states after D0 transition, call the function
835 * below.
836 */
837#if 0 /* disabled */
Takashi Iwaid545a572015-04-04 12:17:28 +0200838 if (state == AC_PWRST_D0)
839 snd_hdac_regmap_sync_node(&codec->core, nid);
Takashi Iwai48f4b3a2015-05-20 06:49:37 +0200840#endif
Takashi Iwaie6feb5d2015-03-16 21:32:11 +0100841 }
842 }
843 return changed;
844}
845
846/* do sync with the last power state change */
847static void sync_power_state_change(struct hda_codec *codec, hda_nid_t nid)
848{
849 if (nid) {
850 msleep(10);
851 snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_POWER_STATE, 0);
852 }
853}
854
Takashi Iwaidda42bd2014-10-30 12:04:50 +0100855/**
856 * snd_hda_activate_path - activate or deactivate the given path
857 * @codec: the HDA codec
858 * @path: the path to activate/deactivate
859 * @enable: flag to activate or not
860 * @add_aamix: enable the input from aamix NID
861 *
862 * If @add_aamix is set, enable the input from aa-mix NID as well (if any).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100864void snd_hda_activate_path(struct hda_codec *codec, struct nid_path *path,
865 bool enable, bool add_aamix)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866{
Takashi Iwai55196ff2013-01-24 17:32:56 +0100867 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100868 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869
Takashi Iwaic7cd0ef2015-08-24 10:52:06 +0200870 path->active = enable;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100871
Takashi Iwaie6feb5d2015-03-16 21:32:11 +0100872 /* make sure the widget is powered up */
Takashi Iwai967b1302015-03-20 18:21:03 +0100873 if (enable && (spec->power_down_unused || codec->power_save_node))
874 path_power_update(codec, path, codec->power_save_node);
Takashi Iwaie6feb5d2015-03-16 21:32:11 +0100875
Takashi Iwai352f7f92012-12-19 12:52:06 +0100876 for (i = path->depth - 1; i >= 0; i--) {
Takashi Iwai55196ff2013-01-24 17:32:56 +0100877 hda_nid_t nid = path->path[i];
Takashi Iwaie6feb5d2015-03-16 21:32:11 +0100878
Takashi Iwai352f7f92012-12-19 12:52:06 +0100879 if (enable && path->multi[i])
Takashi Iwai8f0972d2014-01-27 16:26:16 +0100880 snd_hda_codec_update_cache(codec, nid, 0,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100881 AC_VERB_SET_CONNECT_SEL,
882 path->idx[i]);
883 if (has_amp_in(codec, path, i))
884 activate_amp_in(codec, path, i, enable, add_aamix);
885 if (has_amp_out(codec, path, i))
886 activate_amp_out(codec, path, i, enable);
887 }
Takashi Iwai352f7f92012-12-19 12:52:06 +0100888}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100889EXPORT_SYMBOL_GPL(snd_hda_activate_path);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100890
Takashi Iwai55196ff2013-01-24 17:32:56 +0100891/* if the given path is inactive, put widgets into D3 (only if suitable) */
892static void path_power_down_sync(struct hda_codec *codec, struct nid_path *path)
893{
894 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai55196ff2013-01-24 17:32:56 +0100895
Takashi Iwai967b1302015-03-20 18:21:03 +0100896 if (!(spec->power_down_unused || codec->power_save_node) || path->active)
Takashi Iwai55196ff2013-01-24 17:32:56 +0100897 return;
Takashi Iwaie6feb5d2015-03-16 21:32:11 +0100898 sync_power_state_change(codec, path_power_update(codec, path, true));
Takashi Iwai55196ff2013-01-24 17:32:56 +0100899}
900
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +0100901/* turn on/off EAPD on the given pin */
902static void set_pin_eapd(struct hda_codec *codec, hda_nid_t pin, bool enable)
903{
904 struct hda_gen_spec *spec = codec->spec;
905 if (spec->own_eapd_ctl ||
906 !(snd_hda_query_pin_caps(codec, pin) & AC_PINCAP_EAPD))
907 return;
Takashi Iwai05909d5c2013-05-31 19:55:54 +0200908 if (spec->keep_eapd_on && !enable)
909 return;
Takashi Iwai468ac412013-11-12 11:36:00 +0100910 if (codec->inv_eapd)
911 enable = !enable;
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +0100912 snd_hda_codec_update_cache(codec, pin, 0,
913 AC_VERB_SET_EAPD_BTLENABLE,
914 enable ? 0x02 : 0x00);
915}
916
Takashi Iwai3e367f12013-01-23 17:07:23 +0100917/* re-initialize the path specified by the given path index */
918static void resume_path_from_idx(struct hda_codec *codec, int path_idx)
919{
920 struct nid_path *path = snd_hda_get_path_from_idx(codec, path_idx);
921 if (path)
922 snd_hda_activate_path(codec, path, path->active, false);
923}
924
Takashi Iwai352f7f92012-12-19 12:52:06 +0100925
926/*
927 * Helper functions for creating mixer ctl elements
928 */
929
Takashi Iwai7eebffd2013-06-24 16:00:21 +0200930static int hda_gen_mixer_mute_put(struct snd_kcontrol *kcontrol,
931 struct snd_ctl_elem_value *ucontrol);
Takashi Iwaibc2eee22013-08-09 15:05:03 +0200932static int hda_gen_bind_mute_put(struct snd_kcontrol *kcontrol,
933 struct snd_ctl_elem_value *ucontrol);
Takashi Iwai7eebffd2013-06-24 16:00:21 +0200934
Takashi Iwai352f7f92012-12-19 12:52:06 +0100935enum {
936 HDA_CTL_WIDGET_VOL,
937 HDA_CTL_WIDGET_MUTE,
938 HDA_CTL_BIND_MUTE,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100939};
940static const struct snd_kcontrol_new control_templates[] = {
941 HDA_CODEC_VOLUME(NULL, 0, 0, 0),
Takashi Iwai7eebffd2013-06-24 16:00:21 +0200942 /* only the put callback is replaced for handling the special mute */
943 {
944 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
945 .subdevice = HDA_SUBDEV_AMP_FLAG,
946 .info = snd_hda_mixer_amp_switch_info,
947 .get = snd_hda_mixer_amp_switch_get,
948 .put = hda_gen_mixer_mute_put, /* replaced */
949 .private_value = HDA_COMPOSE_AMP_VAL(0, 3, 0, 0),
950 },
Takashi Iwaibc2eee22013-08-09 15:05:03 +0200951 {
952 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
953 .info = snd_hda_mixer_amp_switch_info,
954 .get = snd_hda_mixer_bind_switch_get,
955 .put = hda_gen_bind_mute_put, /* replaced */
956 .private_value = HDA_COMPOSE_AMP_VAL(0, 3, 0, 0),
957 },
Takashi Iwai352f7f92012-12-19 12:52:06 +0100958};
959
960/* add dynamic controls from template */
Takashi Iwaia35bd1e2013-01-18 14:01:14 +0100961static struct snd_kcontrol_new *
962add_control(struct hda_gen_spec *spec, int type, const char *name,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100963 int cidx, unsigned long val)
964{
965 struct snd_kcontrol_new *knew;
966
Takashi Iwai12c93df2012-12-19 14:38:33 +0100967 knew = snd_hda_gen_add_kctl(spec, name, &control_templates[type]);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100968 if (!knew)
Takashi Iwaia35bd1e2013-01-18 14:01:14 +0100969 return NULL;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100970 knew->index = cidx;
971 if (get_amp_nid_(val))
972 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
973 knew->private_value = val;
Takashi Iwaia35bd1e2013-01-18 14:01:14 +0100974 return knew;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100975}
976
977static int add_control_with_pfx(struct hda_gen_spec *spec, int type,
978 const char *pfx, const char *dir,
979 const char *sfx, int cidx, unsigned long val)
980{
Takashi Iwai975cc022013-06-28 11:56:49 +0200981 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
Takashi Iwai352f7f92012-12-19 12:52:06 +0100982 snprintf(name, sizeof(name), "%s %s %s", pfx, dir, sfx);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +0100983 if (!add_control(spec, type, name, cidx, val))
984 return -ENOMEM;
985 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100986}
987
988#define add_pb_vol_ctrl(spec, type, pfx, val) \
989 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", 0, val)
990#define add_pb_sw_ctrl(spec, type, pfx, val) \
991 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", 0, val)
992#define __add_pb_vol_ctrl(spec, type, pfx, cidx, val) \
993 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", cidx, val)
994#define __add_pb_sw_ctrl(spec, type, pfx, cidx, val) \
995 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", cidx, val)
996
997static int add_vol_ctl(struct hda_codec *codec, const char *pfx, int cidx,
998 unsigned int chs, struct nid_path *path)
999{
1000 unsigned int val;
1001 if (!path)
1002 return 0;
1003 val = path->ctls[NID_PATH_VOL_CTL];
1004 if (!val)
1005 return 0;
1006 val = amp_val_replace_channels(val, chs);
1007 return __add_pb_vol_ctrl(codec->spec, HDA_CTL_WIDGET_VOL, pfx, cidx, val);
1008}
1009
1010/* return the channel bits suitable for the given path->ctls[] */
1011static int get_default_ch_nums(struct hda_codec *codec, struct nid_path *path,
1012 int type)
1013{
1014 int chs = 1; /* mono (left only) */
1015 if (path) {
1016 hda_nid_t nid = get_amp_nid_(path->ctls[type]);
1017 if (nid && (get_wcaps(codec, nid) & AC_WCAP_STEREO))
1018 chs = 3; /* stereo */
1019 }
1020 return chs;
1021}
1022
1023static int add_stereo_vol(struct hda_codec *codec, const char *pfx, int cidx,
1024 struct nid_path *path)
1025{
1026 int chs = get_default_ch_nums(codec, path, NID_PATH_VOL_CTL);
1027 return add_vol_ctl(codec, pfx, cidx, chs, path);
1028}
1029
1030/* create a mute-switch for the given mixer widget;
1031 * if it has multiple sources (e.g. DAC and loopback), create a bind-mute
1032 */
1033static int add_sw_ctl(struct hda_codec *codec, const char *pfx, int cidx,
1034 unsigned int chs, struct nid_path *path)
1035{
1036 unsigned int val;
1037 int type = HDA_CTL_WIDGET_MUTE;
1038
1039 if (!path)
1040 return 0;
1041 val = path->ctls[NID_PATH_MUTE_CTL];
1042 if (!val)
1043 return 0;
1044 val = amp_val_replace_channels(val, chs);
1045 if (get_amp_direction_(val) == HDA_INPUT) {
1046 hda_nid_t nid = get_amp_nid_(val);
1047 int nums = snd_hda_get_num_conns(codec, nid);
1048 if (nums > 1) {
1049 type = HDA_CTL_BIND_MUTE;
1050 val |= nums << 19;
1051 }
1052 }
1053 return __add_pb_sw_ctrl(codec->spec, type, pfx, cidx, val);
1054}
1055
1056static int add_stereo_sw(struct hda_codec *codec, const char *pfx,
1057 int cidx, struct nid_path *path)
1058{
1059 int chs = get_default_ch_nums(codec, path, NID_PATH_MUTE_CTL);
1060 return add_sw_ctl(codec, pfx, cidx, chs, path);
1061}
1062
Takashi Iwai7eebffd2013-06-24 16:00:21 +02001063/* playback mute control with the software mute bit check */
Takashi Iwaibc2eee22013-08-09 15:05:03 +02001064static void sync_auto_mute_bits(struct snd_kcontrol *kcontrol,
1065 struct snd_ctl_elem_value *ucontrol)
Takashi Iwai7eebffd2013-06-24 16:00:21 +02001066{
1067 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1068 struct hda_gen_spec *spec = codec->spec;
1069
1070 if (spec->auto_mute_via_amp) {
1071 hda_nid_t nid = get_amp_nid(kcontrol);
1072 bool enabled = !((spec->mute_bits >> nid) & 1);
1073 ucontrol->value.integer.value[0] &= enabled;
1074 ucontrol->value.integer.value[1] &= enabled;
1075 }
Takashi Iwaibc2eee22013-08-09 15:05:03 +02001076}
Takashi Iwai7eebffd2013-06-24 16:00:21 +02001077
Takashi Iwaibc2eee22013-08-09 15:05:03 +02001078static int hda_gen_mixer_mute_put(struct snd_kcontrol *kcontrol,
1079 struct snd_ctl_elem_value *ucontrol)
1080{
1081 sync_auto_mute_bits(kcontrol, ucontrol);
Takashi Iwai7eebffd2013-06-24 16:00:21 +02001082 return snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
1083}
1084
Takashi Iwaibc2eee22013-08-09 15:05:03 +02001085static int hda_gen_bind_mute_put(struct snd_kcontrol *kcontrol,
1086 struct snd_ctl_elem_value *ucontrol)
1087{
1088 sync_auto_mute_bits(kcontrol, ucontrol);
1089 return snd_hda_mixer_bind_switch_put(kcontrol, ucontrol);
1090}
1091
Takashi Iwai247d85e2013-01-17 16:18:11 +01001092/* any ctl assigned to the path with the given index? */
1093static bool path_has_mixer(struct hda_codec *codec, int path_idx, int ctl_type)
1094{
1095 struct nid_path *path = snd_hda_get_path_from_idx(codec, path_idx);
1096 return path && path->ctls[ctl_type];
1097}
1098
Takashi Iwai352f7f92012-12-19 12:52:06 +01001099static const char * const channel_name[4] = {
1100 "Front", "Surround", "CLFE", "Side"
1101};
1102
1103/* give some appropriate ctl name prefix for the given line out channel */
Takashi Iwai247d85e2013-01-17 16:18:11 +01001104static const char *get_line_out_pfx(struct hda_codec *codec, int ch,
1105 int *index, int ctl_type)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001106{
Takashi Iwai247d85e2013-01-17 16:18:11 +01001107 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001108 struct auto_pin_cfg *cfg = &spec->autocfg;
1109
1110 *index = 0;
1111 if (cfg->line_outs == 1 && !spec->multi_ios &&
Takashi Iwai247d85e2013-01-17 16:18:11 +01001112 !cfg->hp_outs && !cfg->speaker_outs)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001113 return spec->vmaster_mute.hook ? "PCM" : "Master";
1114
1115 /* if there is really a single DAC used in the whole output paths,
1116 * use it master (or "PCM" if a vmaster hook is present)
1117 */
1118 if (spec->multiout.num_dacs == 1 && !spec->mixer_nid &&
1119 !spec->multiout.hp_out_nid[0] && !spec->multiout.extra_out_nid[0])
1120 return spec->vmaster_mute.hook ? "PCM" : "Master";
1121
Takashi Iwai247d85e2013-01-17 16:18:11 +01001122 /* multi-io channels */
1123 if (ch >= cfg->line_outs)
1124 return channel_name[ch];
1125
Takashi Iwai352f7f92012-12-19 12:52:06 +01001126 switch (cfg->line_out_type) {
1127 case AUTO_PIN_SPEAKER_OUT:
Takashi Iwai247d85e2013-01-17 16:18:11 +01001128 /* if the primary channel vol/mute is shared with HP volume,
1129 * don't name it as Speaker
1130 */
1131 if (!ch && cfg->hp_outs &&
1132 !path_has_mixer(codec, spec->hp_paths[0], ctl_type))
1133 break;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001134 if (cfg->line_outs == 1)
1135 return "Speaker";
1136 if (cfg->line_outs == 2)
1137 return ch ? "Bass Speaker" : "Speaker";
1138 break;
1139 case AUTO_PIN_HP_OUT:
Takashi Iwai247d85e2013-01-17 16:18:11 +01001140 /* if the primary channel vol/mute is shared with spk volume,
1141 * don't name it as Headphone
1142 */
1143 if (!ch && cfg->speaker_outs &&
1144 !path_has_mixer(codec, spec->speaker_paths[0], ctl_type))
1145 break;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001146 /* for multi-io case, only the primary out */
1147 if (ch && spec->multi_ios)
1148 break;
1149 *index = ch;
1150 return "Headphone";
David Henningsson03ad6a82014-10-16 15:33:45 +02001151 case AUTO_PIN_LINE_OUT:
1152 /* This deals with the case where we have two DACs and
1153 * one LO, one HP and one Speaker */
1154 if (!ch && cfg->speaker_outs && cfg->hp_outs) {
1155 bool hp_lo_shared = !path_has_mixer(codec, spec->hp_paths[0], ctl_type);
1156 bool spk_lo_shared = !path_has_mixer(codec, spec->speaker_paths[0], ctl_type);
1157 if (hp_lo_shared && spk_lo_shared)
1158 return spec->vmaster_mute.hook ? "PCM" : "Master";
1159 if (hp_lo_shared)
1160 return "Headphone+LO";
1161 if (spk_lo_shared)
1162 return "Speaker+LO";
1163 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001164 }
Takashi Iwai247d85e2013-01-17 16:18:11 +01001165
1166 /* for a single channel output, we don't have to name the channel */
1167 if (cfg->line_outs == 1 && !spec->multi_ios)
David Henningsson3abb4f42014-10-16 15:33:46 +02001168 return "Line Out";
Takashi Iwai247d85e2013-01-17 16:18:11 +01001169
Takashi Iwai352f7f92012-12-19 12:52:06 +01001170 if (ch >= ARRAY_SIZE(channel_name)) {
1171 snd_BUG();
1172 return "PCM";
1173 }
1174
1175 return channel_name[ch];
1176}
1177
1178/*
1179 * Parse output paths
1180 */
1181
1182/* badness definition */
1183enum {
1184 /* No primary DAC is found for the main output */
1185 BAD_NO_PRIMARY_DAC = 0x10000,
1186 /* No DAC is found for the extra output */
1187 BAD_NO_DAC = 0x4000,
1188 /* No possible multi-ios */
Takashi Iwai1d739062013-02-13 14:17:32 +01001189 BAD_MULTI_IO = 0x120,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001190 /* No individual DAC for extra output */
1191 BAD_NO_EXTRA_DAC = 0x102,
1192 /* No individual DAC for extra surrounds */
1193 BAD_NO_EXTRA_SURR_DAC = 0x101,
1194 /* Primary DAC shared with main surrounds */
1195 BAD_SHARED_SURROUND = 0x100,
Takashi Iwai55a63d42013-03-21 17:20:12 +01001196 /* No independent HP possible */
Takashi Iwaibec8e682013-03-22 15:10:08 +01001197 BAD_NO_INDEP_HP = 0x10,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001198 /* Primary DAC shared with main CLFE */
1199 BAD_SHARED_CLFE = 0x10,
1200 /* Primary DAC shared with extra surrounds */
1201 BAD_SHARED_EXTRA_SURROUND = 0x10,
1202 /* Volume widget is shared */
1203 BAD_SHARED_VOL = 0x10,
1204};
1205
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001206/* look for widgets in the given path which are appropriate for
Takashi Iwai352f7f92012-12-19 12:52:06 +01001207 * volume and mute controls, and assign the values to ctls[].
1208 *
1209 * When no appropriate widget is found in the path, the badness value
1210 * is incremented depending on the situation. The function returns the
1211 * total badness for both volume and mute controls.
1212 */
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001213static int assign_out_path_ctls(struct hda_codec *codec, struct nid_path *path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001214{
Takashi Iwaid89c6c02014-09-01 10:07:04 +02001215 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001216 hda_nid_t nid;
1217 unsigned int val;
1218 int badness = 0;
1219
1220 if (!path)
1221 return BAD_SHARED_VOL * 2;
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001222
1223 if (path->ctls[NID_PATH_VOL_CTL] ||
1224 path->ctls[NID_PATH_MUTE_CTL])
1225 return 0; /* already evaluated */
1226
Takashi Iwai352f7f92012-12-19 12:52:06 +01001227 nid = look_for_out_vol_nid(codec, path);
1228 if (nid) {
1229 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
Takashi Iwaid89c6c02014-09-01 10:07:04 +02001230 if (spec->dac_min_mute)
1231 val |= HDA_AMP_VAL_MIN_MUTE;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001232 if (is_ctl_used(codec, val, NID_PATH_VOL_CTL))
1233 badness += BAD_SHARED_VOL;
1234 else
1235 path->ctls[NID_PATH_VOL_CTL] = val;
1236 } else
1237 badness += BAD_SHARED_VOL;
1238 nid = look_for_out_mute_nid(codec, path);
1239 if (nid) {
1240 unsigned int wid_type = get_wcaps_type(get_wcaps(codec, nid));
1241 if (wid_type == AC_WID_PIN || wid_type == AC_WID_AUD_OUT ||
1242 nid_has_mute(codec, nid, HDA_OUTPUT))
1243 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
1244 else
1245 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT);
1246 if (is_ctl_used(codec, val, NID_PATH_MUTE_CTL))
1247 badness += BAD_SHARED_VOL;
1248 else
1249 path->ctls[NID_PATH_MUTE_CTL] = val;
1250 } else
1251 badness += BAD_SHARED_VOL;
1252 return badness;
1253}
1254
Takashi Iwai98bd1112013-03-22 14:53:50 +01001255const struct badness_table hda_main_out_badness = {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001256 .no_primary_dac = BAD_NO_PRIMARY_DAC,
1257 .no_dac = BAD_NO_DAC,
1258 .shared_primary = BAD_NO_PRIMARY_DAC,
1259 .shared_surr = BAD_SHARED_SURROUND,
1260 .shared_clfe = BAD_SHARED_CLFE,
1261 .shared_surr_main = BAD_SHARED_SURROUND,
1262};
Takashi Iwai2698ea92013-12-18 07:45:52 +01001263EXPORT_SYMBOL_GPL(hda_main_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001264
Takashi Iwai98bd1112013-03-22 14:53:50 +01001265const struct badness_table hda_extra_out_badness = {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001266 .no_primary_dac = BAD_NO_DAC,
1267 .no_dac = BAD_NO_DAC,
1268 .shared_primary = BAD_NO_EXTRA_DAC,
1269 .shared_surr = BAD_SHARED_EXTRA_SURROUND,
1270 .shared_clfe = BAD_SHARED_EXTRA_SURROUND,
1271 .shared_surr_main = BAD_NO_EXTRA_SURR_DAC,
1272};
Takashi Iwai2698ea92013-12-18 07:45:52 +01001273EXPORT_SYMBOL_GPL(hda_extra_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001274
Takashi Iwai7385df62013-01-07 09:50:52 +01001275/* get the DAC of the primary output corresponding to the given array index */
1276static hda_nid_t get_primary_out(struct hda_codec *codec, int idx)
1277{
1278 struct hda_gen_spec *spec = codec->spec;
1279 struct auto_pin_cfg *cfg = &spec->autocfg;
1280
1281 if (cfg->line_outs > idx)
1282 return spec->private_dac_nids[idx];
1283 idx -= cfg->line_outs;
1284 if (spec->multi_ios > idx)
1285 return spec->multi_io[idx].dac;
1286 return 0;
1287}
1288
1289/* return the DAC if it's reachable, otherwise zero */
1290static inline hda_nid_t try_dac(struct hda_codec *codec,
1291 hda_nid_t dac, hda_nid_t pin)
1292{
1293 return is_reachable_path(codec, dac, pin) ? dac : 0;
1294}
1295
Takashi Iwai352f7f92012-12-19 12:52:06 +01001296/* try to assign DACs to pins and return the resultant badness */
1297static int try_assign_dacs(struct hda_codec *codec, int num_outs,
1298 const hda_nid_t *pins, hda_nid_t *dacs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001299 int *path_idx,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001300 const struct badness_table *bad)
1301{
1302 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001303 int i, j;
1304 int badness = 0;
1305 hda_nid_t dac;
1306
1307 if (!num_outs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 return 0;
1309
Takashi Iwai352f7f92012-12-19 12:52:06 +01001310 for (i = 0; i < num_outs; i++) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001311 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001312 hda_nid_t pin = pins[i];
Takashi Iwai1e0b5282013-01-04 12:56:52 +01001313
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001314 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
1315 if (path) {
1316 badness += assign_out_path_ctls(codec, path);
Takashi Iwai1e0b5282013-01-04 12:56:52 +01001317 continue;
1318 }
1319
Takashi Iwai36907392013-12-10 17:29:26 +01001320 dacs[i] = get_preferred_dac(codec, pin);
1321 if (dacs[i]) {
1322 if (is_dac_already_used(codec, dacs[i]))
1323 badness += bad->shared_primary;
1324 }
1325
1326 if (!dacs[i])
1327 dacs[i] = look_for_dac(codec, pin, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001328 if (!dacs[i] && !i) {
Takashi Iwai980428c2013-01-09 09:28:20 +01001329 /* try to steal the DAC of surrounds for the front */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001330 for (j = 1; j < num_outs; j++) {
1331 if (is_reachable_path(codec, dacs[j], pin)) {
1332 dacs[0] = dacs[j];
1333 dacs[j] = 0;
Takashi Iwai980428c2013-01-09 09:28:20 +01001334 invalidate_nid_path(codec, path_idx[j]);
Takashi Iwai196c17662013-01-04 15:01:40 +01001335 path_idx[j] = 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001336 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 }
1338 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001339 }
1340 dac = dacs[i];
1341 if (!dac) {
Takashi Iwai7385df62013-01-07 09:50:52 +01001342 if (num_outs > 2)
1343 dac = try_dac(codec, get_primary_out(codec, i), pin);
1344 if (!dac)
1345 dac = try_dac(codec, dacs[0], pin);
1346 if (!dac)
1347 dac = try_dac(codec, get_primary_out(codec, i), pin);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001348 if (dac) {
1349 if (!i)
1350 badness += bad->shared_primary;
1351 else if (i == 1)
1352 badness += bad->shared_surr;
1353 else
1354 badness += bad->shared_clfe;
1355 } else if (is_reachable_path(codec, spec->private_dac_nids[0], pin)) {
1356 dac = spec->private_dac_nids[0];
1357 badness += bad->shared_surr_main;
1358 } else if (!i)
1359 badness += bad->no_primary_dac;
1360 else
1361 badness += bad->no_dac;
1362 }
Takashi Iwai1fa335b2013-01-21 11:43:19 +01001363 if (!dac)
1364 continue;
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001365 path = snd_hda_add_new_path(codec, dac, pin, -spec->mixer_nid);
Takashi Iwai117688a2013-01-04 15:41:41 +01001366 if (!path && !i && spec->mixer_nid) {
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001367 /* try with aamix */
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001368 path = snd_hda_add_new_path(codec, dac, pin, 0);
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001369 }
Takashi Iwai1fa335b2013-01-21 11:43:19 +01001370 if (!path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001371 dac = dacs[i] = 0;
Takashi Iwai1fa335b2013-01-21 11:43:19 +01001372 badness += bad->no_dac;
1373 } else {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001374 /* print_nid_path(codec, "output", path); */
Takashi Iwaie1284af2013-01-03 16:33:02 +01001375 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +01001376 path_idx[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001377 badness += assign_out_path_ctls(codec, path);
Takashi Iwaie1284af2013-01-03 16:33:02 +01001378 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001379 }
1380
1381 return badness;
1382}
1383
1384/* return NID if the given pin has only a single connection to a certain DAC */
1385static hda_nid_t get_dac_if_single(struct hda_codec *codec, hda_nid_t pin)
1386{
1387 struct hda_gen_spec *spec = codec->spec;
1388 int i;
1389 hda_nid_t nid_found = 0;
1390
1391 for (i = 0; i < spec->num_all_dacs; i++) {
1392 hda_nid_t nid = spec->all_dacs[i];
1393 if (!nid || is_dac_already_used(codec, nid))
1394 continue;
1395 if (is_reachable_path(codec, nid, pin)) {
1396 if (nid_found)
1397 return 0;
1398 nid_found = nid;
1399 }
1400 }
1401 return nid_found;
1402}
1403
1404/* check whether the given pin can be a multi-io pin */
1405static bool can_be_multiio_pin(struct hda_codec *codec,
1406 unsigned int location, hda_nid_t nid)
1407{
1408 unsigned int defcfg, caps;
1409
1410 defcfg = snd_hda_codec_get_pincfg(codec, nid);
1411 if (get_defcfg_connect(defcfg) != AC_JACK_PORT_COMPLEX)
1412 return false;
1413 if (location && get_defcfg_location(defcfg) != location)
1414 return false;
1415 caps = snd_hda_query_pin_caps(codec, nid);
1416 if (!(caps & AC_PINCAP_OUT))
1417 return false;
1418 return true;
1419}
1420
Takashi Iwaie22aab72013-01-04 14:50:04 +01001421/* count the number of input pins that are capable to be multi-io */
1422static int count_multiio_pins(struct hda_codec *codec, hda_nid_t reference_pin)
1423{
1424 struct hda_gen_spec *spec = codec->spec;
1425 struct auto_pin_cfg *cfg = &spec->autocfg;
1426 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1427 unsigned int location = get_defcfg_location(defcfg);
1428 int type, i;
1429 int num_pins = 0;
1430
1431 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1432 for (i = 0; i < cfg->num_inputs; i++) {
1433 if (cfg->inputs[i].type != type)
1434 continue;
1435 if (can_be_multiio_pin(codec, location,
1436 cfg->inputs[i].pin))
1437 num_pins++;
1438 }
1439 }
1440 return num_pins;
1441}
1442
Takashi Iwai352f7f92012-12-19 12:52:06 +01001443/*
1444 * multi-io helper
1445 *
1446 * When hardwired is set, try to fill ony hardwired pins, and returns
1447 * zero if any pins are filled, non-zero if nothing found.
1448 * When hardwired is off, try to fill possible input pins, and returns
1449 * the badness value.
1450 */
1451static int fill_multi_ios(struct hda_codec *codec,
1452 hda_nid_t reference_pin,
Takashi Iwaie22aab72013-01-04 14:50:04 +01001453 bool hardwired)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001454{
1455 struct hda_gen_spec *spec = codec->spec;
1456 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwaie22aab72013-01-04 14:50:04 +01001457 int type, i, j, num_pins, old_pins;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001458 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1459 unsigned int location = get_defcfg_location(defcfg);
1460 int badness = 0;
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001461 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001462
1463 old_pins = spec->multi_ios;
1464 if (old_pins >= 2)
1465 goto end_fill;
1466
Takashi Iwaie22aab72013-01-04 14:50:04 +01001467 num_pins = count_multiio_pins(codec, reference_pin);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001468 if (num_pins < 2)
1469 goto end_fill;
1470
Takashi Iwai352f7f92012-12-19 12:52:06 +01001471 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1472 for (i = 0; i < cfg->num_inputs; i++) {
1473 hda_nid_t nid = cfg->inputs[i].pin;
1474 hda_nid_t dac = 0;
1475
1476 if (cfg->inputs[i].type != type)
1477 continue;
1478 if (!can_be_multiio_pin(codec, location, nid))
1479 continue;
1480 for (j = 0; j < spec->multi_ios; j++) {
1481 if (nid == spec->multi_io[j].pin)
1482 break;
1483 }
1484 if (j < spec->multi_ios)
1485 continue;
1486
Takashi Iwai352f7f92012-12-19 12:52:06 +01001487 if (hardwired)
1488 dac = get_dac_if_single(codec, nid);
1489 else if (!dac)
1490 dac = look_for_dac(codec, nid, false);
1491 if (!dac) {
1492 badness++;
1493 continue;
1494 }
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001495 path = snd_hda_add_new_path(codec, dac, nid,
1496 -spec->mixer_nid);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001497 if (!path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001498 badness++;
1499 continue;
1500 }
Takashi Iwai4e76a882014-02-25 12:21:03 +01001501 /* print_nid_path(codec, "multiio", path); */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001502 spec->multi_io[spec->multi_ios].pin = nid;
1503 spec->multi_io[spec->multi_ios].dac = dac;
Takashi Iwai196c17662013-01-04 15:01:40 +01001504 spec->out_paths[cfg->line_outs + spec->multi_ios] =
1505 snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001506 spec->multi_ios++;
1507 if (spec->multi_ios >= 2)
1508 break;
1509 }
1510 }
1511 end_fill:
1512 if (badness)
1513 badness = BAD_MULTI_IO;
1514 if (old_pins == spec->multi_ios) {
1515 if (hardwired)
1516 return 1; /* nothing found */
1517 else
1518 return badness; /* no badness if nothing found */
1519 }
1520 if (!hardwired && spec->multi_ios < 2) {
1521 /* cancel newly assigned paths */
1522 spec->paths.used -= spec->multi_ios - old_pins;
1523 spec->multi_ios = old_pins;
1524 return badness;
1525 }
1526
1527 /* assign volume and mute controls */
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001528 for (i = old_pins; i < spec->multi_ios; i++) {
1529 path = snd_hda_get_path_from_idx(codec, spec->out_paths[cfg->line_outs + i]);
1530 badness += assign_out_path_ctls(codec, path);
1531 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001532
1533 return badness;
1534}
1535
1536/* map DACs for all pins in the list if they are single connections */
1537static bool map_singles(struct hda_codec *codec, int outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001538 const hda_nid_t *pins, hda_nid_t *dacs, int *path_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001539{
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001540 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001541 int i;
1542 bool found = false;
1543 for (i = 0; i < outs; i++) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001544 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001545 hda_nid_t dac;
1546 if (dacs[i])
1547 continue;
1548 dac = get_dac_if_single(codec, pins[i]);
1549 if (!dac)
1550 continue;
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001551 path = snd_hda_add_new_path(codec, dac, pins[i],
1552 -spec->mixer_nid);
Takashi Iwai117688a2013-01-04 15:41:41 +01001553 if (!path && !i && spec->mixer_nid)
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001554 path = snd_hda_add_new_path(codec, dac, pins[i], 0);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001555 if (path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001556 dacs[i] = dac;
1557 found = true;
Takashi Iwai4e76a882014-02-25 12:21:03 +01001558 /* print_nid_path(codec, "output", path); */
Takashi Iwaie1284af2013-01-03 16:33:02 +01001559 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +01001560 path_idx[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001561 }
1562 }
1563 return found;
1564}
1565
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001566/* create a new path including aamix if available, and return its index */
1567static int check_aamix_out_path(struct hda_codec *codec, int path_idx)
1568{
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001569 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001570 struct nid_path *path;
Takashi Iwai5ead56f2013-04-16 14:16:54 +02001571 hda_nid_t path_dac, dac, pin;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001572
1573 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001574 if (!path || !path->depth ||
1575 is_nid_contained(path, spec->mixer_nid))
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001576 return 0;
Takashi Iwai5ead56f2013-04-16 14:16:54 +02001577 path_dac = path->path[0];
1578 dac = spec->private_dac_nids[0];
Takashi Iwaif87498b2013-01-21 14:24:31 +01001579 pin = path->path[path->depth - 1];
1580 path = snd_hda_add_new_path(codec, dac, pin, spec->mixer_nid);
1581 if (!path) {
Takashi Iwai5ead56f2013-04-16 14:16:54 +02001582 if (dac != path_dac)
1583 dac = path_dac;
Takashi Iwaif87498b2013-01-21 14:24:31 +01001584 else if (spec->multiout.hp_out_nid[0])
1585 dac = spec->multiout.hp_out_nid[0];
1586 else if (spec->multiout.extra_out_nid[0])
1587 dac = spec->multiout.extra_out_nid[0];
Takashi Iwai5ead56f2013-04-16 14:16:54 +02001588 else
1589 dac = 0;
Takashi Iwaif87498b2013-01-21 14:24:31 +01001590 if (dac)
1591 path = snd_hda_add_new_path(codec, dac, pin,
1592 spec->mixer_nid);
1593 }
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001594 if (!path)
1595 return 0;
Takashi Iwai4e76a882014-02-25 12:21:03 +01001596 /* print_nid_path(codec, "output-aamix", path); */
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001597 path->active = false; /* unused as default */
Takashi Iwai6b275b12015-03-20 18:11:05 +01001598 path->pin_fixed = true; /* static route */
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001599 return snd_hda_get_path_idx(codec, path);
1600}
1601
Takashi Iwai55a63d42013-03-21 17:20:12 +01001602/* check whether the independent HP is available with the current config */
1603static bool indep_hp_possible(struct hda_codec *codec)
1604{
1605 struct hda_gen_spec *spec = codec->spec;
1606 struct auto_pin_cfg *cfg = &spec->autocfg;
1607 struct nid_path *path;
1608 int i, idx;
1609
1610 if (cfg->line_out_type == AUTO_PIN_HP_OUT)
1611 idx = spec->out_paths[0];
1612 else
1613 idx = spec->hp_paths[0];
1614 path = snd_hda_get_path_from_idx(codec, idx);
1615 if (!path)
1616 return false;
1617
1618 /* assume no path conflicts unless aamix is involved */
1619 if (!spec->mixer_nid || !is_nid_contained(path, spec->mixer_nid))
1620 return true;
1621
1622 /* check whether output paths contain aamix */
1623 for (i = 0; i < cfg->line_outs; i++) {
1624 if (spec->out_paths[i] == idx)
1625 break;
1626 path = snd_hda_get_path_from_idx(codec, spec->out_paths[i]);
1627 if (path && is_nid_contained(path, spec->mixer_nid))
1628 return false;
1629 }
1630 for (i = 0; i < cfg->speaker_outs; i++) {
1631 path = snd_hda_get_path_from_idx(codec, spec->speaker_paths[i]);
1632 if (path && is_nid_contained(path, spec->mixer_nid))
1633 return false;
1634 }
1635
1636 return true;
1637}
1638
Takashi Iwaia07a9492013-01-07 16:44:06 +01001639/* fill the empty entries in the dac array for speaker/hp with the
1640 * shared dac pointed by the paths
1641 */
1642static void refill_shared_dacs(struct hda_codec *codec, int num_outs,
1643 hda_nid_t *dacs, int *path_idx)
1644{
1645 struct nid_path *path;
1646 int i;
1647
1648 for (i = 0; i < num_outs; i++) {
1649 if (dacs[i])
1650 continue;
1651 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
1652 if (!path)
1653 continue;
1654 dacs[i] = path->path[0];
1655 }
1656}
1657
Takashi Iwai352f7f92012-12-19 12:52:06 +01001658/* fill in the dac_nids table from the parsed pin configuration */
1659static int fill_and_eval_dacs(struct hda_codec *codec,
1660 bool fill_hardwired,
1661 bool fill_mio_first)
1662{
1663 struct hda_gen_spec *spec = codec->spec;
1664 struct auto_pin_cfg *cfg = &spec->autocfg;
1665 int i, err, badness;
1666
1667 /* set num_dacs once to full for look_for_dac() */
1668 spec->multiout.num_dacs = cfg->line_outs;
1669 spec->multiout.dac_nids = spec->private_dac_nids;
1670 memset(spec->private_dac_nids, 0, sizeof(spec->private_dac_nids));
1671 memset(spec->multiout.hp_out_nid, 0, sizeof(spec->multiout.hp_out_nid));
1672 memset(spec->multiout.extra_out_nid, 0, sizeof(spec->multiout.extra_out_nid));
1673 spec->multi_ios = 0;
1674 snd_array_free(&spec->paths);
Takashi Iwaicd5be3f2013-01-07 15:07:00 +01001675
1676 /* clear path indices */
1677 memset(spec->out_paths, 0, sizeof(spec->out_paths));
1678 memset(spec->hp_paths, 0, sizeof(spec->hp_paths));
1679 memset(spec->speaker_paths, 0, sizeof(spec->speaker_paths));
1680 memset(spec->aamix_out_paths, 0, sizeof(spec->aamix_out_paths));
1681 memset(spec->digout_paths, 0, sizeof(spec->digout_paths));
Takashi Iwaic697b712013-01-07 17:09:26 +01001682 memset(spec->input_paths, 0, sizeof(spec->input_paths));
Takashi Iwaicd5be3f2013-01-07 15:07:00 +01001683 memset(spec->loopback_paths, 0, sizeof(spec->loopback_paths));
1684 memset(&spec->digin_path, 0, sizeof(spec->digin_path));
1685
Takashi Iwai352f7f92012-12-19 12:52:06 +01001686 badness = 0;
1687
1688 /* fill hard-wired DACs first */
1689 if (fill_hardwired) {
1690 bool mapped;
1691 do {
1692 mapped = map_singles(codec, cfg->line_outs,
1693 cfg->line_out_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001694 spec->private_dac_nids,
1695 spec->out_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001696 mapped |= map_singles(codec, cfg->hp_outs,
1697 cfg->hp_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001698 spec->multiout.hp_out_nid,
1699 spec->hp_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001700 mapped |= map_singles(codec, cfg->speaker_outs,
1701 cfg->speaker_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001702 spec->multiout.extra_out_nid,
1703 spec->speaker_paths);
Takashi Iwaida96fb52013-07-29 16:54:36 +02001704 if (!spec->no_multi_io &&
1705 fill_mio_first && cfg->line_outs == 1 &&
Takashi Iwai352f7f92012-12-19 12:52:06 +01001706 cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
Takashi Iwaie22aab72013-01-04 14:50:04 +01001707 err = fill_multi_ios(codec, cfg->line_out_pins[0], true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001708 if (!err)
1709 mapped = true;
1710 }
1711 } while (mapped);
1712 }
1713
1714 badness += try_assign_dacs(codec, cfg->line_outs, cfg->line_out_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001715 spec->private_dac_nids, spec->out_paths,
Takashi Iwai98bd1112013-03-22 14:53:50 +01001716 spec->main_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001717
Takashi Iwaida96fb52013-07-29 16:54:36 +02001718 if (!spec->no_multi_io && fill_mio_first &&
Takashi Iwai352f7f92012-12-19 12:52:06 +01001719 cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1720 /* try to fill multi-io first */
Takashi Iwaie22aab72013-01-04 14:50:04 +01001721 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001722 if (err < 0)
1723 return err;
1724 /* we don't count badness at this stage yet */
1725 }
1726
1727 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
1728 err = try_assign_dacs(codec, cfg->hp_outs, cfg->hp_pins,
1729 spec->multiout.hp_out_nid,
Takashi Iwai196c17662013-01-04 15:01:40 +01001730 spec->hp_paths,
Takashi Iwai98bd1112013-03-22 14:53:50 +01001731 spec->extra_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001732 if (err < 0)
1733 return err;
1734 badness += err;
1735 }
1736 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1737 err = try_assign_dacs(codec, cfg->speaker_outs,
1738 cfg->speaker_pins,
1739 spec->multiout.extra_out_nid,
Takashi Iwai196c17662013-01-04 15:01:40 +01001740 spec->speaker_paths,
Takashi Iwai98bd1112013-03-22 14:53:50 +01001741 spec->extra_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001742 if (err < 0)
1743 return err;
1744 badness += err;
1745 }
Takashi Iwaida96fb52013-07-29 16:54:36 +02001746 if (!spec->no_multi_io &&
1747 cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
Takashi Iwaie22aab72013-01-04 14:50:04 +01001748 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001749 if (err < 0)
1750 return err;
1751 badness += err;
1752 }
Takashi Iwaie22aab72013-01-04 14:50:04 +01001753
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001754 if (spec->mixer_nid) {
1755 spec->aamix_out_paths[0] =
1756 check_aamix_out_path(codec, spec->out_paths[0]);
1757 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1758 spec->aamix_out_paths[1] =
1759 check_aamix_out_path(codec, spec->hp_paths[0]);
1760 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1761 spec->aamix_out_paths[2] =
1762 check_aamix_out_path(codec, spec->speaker_paths[0]);
1763 }
1764
Takashi Iwaida96fb52013-07-29 16:54:36 +02001765 if (!spec->no_multi_io &&
1766 cfg->hp_outs && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
Takashi Iwaie22aab72013-01-04 14:50:04 +01001767 if (count_multiio_pins(codec, cfg->hp_pins[0]) >= 2)
1768 spec->multi_ios = 1; /* give badness */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001769
Takashi Iwaia07a9492013-01-07 16:44:06 +01001770 /* re-count num_dacs and squash invalid entries */
1771 spec->multiout.num_dacs = 0;
1772 for (i = 0; i < cfg->line_outs; i++) {
1773 if (spec->private_dac_nids[i])
1774 spec->multiout.num_dacs++;
1775 else {
1776 memmove(spec->private_dac_nids + i,
1777 spec->private_dac_nids + i + 1,
1778 sizeof(hda_nid_t) * (cfg->line_outs - i - 1));
1779 spec->private_dac_nids[cfg->line_outs - 1] = 0;
1780 }
1781 }
1782
1783 spec->ext_channel_count = spec->min_channel_count =
David Henningssonc0f3b212013-01-16 11:45:37 +01001784 spec->multiout.num_dacs * 2;
Takashi Iwaia07a9492013-01-07 16:44:06 +01001785
Takashi Iwai352f7f92012-12-19 12:52:06 +01001786 if (spec->multi_ios == 2) {
1787 for (i = 0; i < 2; i++)
1788 spec->private_dac_nids[spec->multiout.num_dacs++] =
1789 spec->multi_io[i].dac;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001790 } else if (spec->multi_ios) {
1791 spec->multi_ios = 0;
1792 badness += BAD_MULTI_IO;
1793 }
1794
Takashi Iwai55a63d42013-03-21 17:20:12 +01001795 if (spec->indep_hp && !indep_hp_possible(codec))
1796 badness += BAD_NO_INDEP_HP;
1797
Takashi Iwaia07a9492013-01-07 16:44:06 +01001798 /* re-fill the shared DAC for speaker / headphone */
1799 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1800 refill_shared_dacs(codec, cfg->hp_outs,
1801 spec->multiout.hp_out_nid,
1802 spec->hp_paths);
1803 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1804 refill_shared_dacs(codec, cfg->speaker_outs,
1805 spec->multiout.extra_out_nid,
1806 spec->speaker_paths);
1807
Takashi Iwai352f7f92012-12-19 12:52:06 +01001808 return badness;
1809}
1810
1811#define DEBUG_BADNESS
1812
1813#ifdef DEBUG_BADNESS
Joe Perchesd82353e2014-07-05 13:02:02 -07001814#define debug_badness(fmt, ...) \
1815 codec_dbg(codec, fmt, ##__VA_ARGS__)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001816#else
Joe Perchesd82353e2014-07-05 13:02:02 -07001817#define debug_badness(fmt, ...) \
1818 do { if (0) codec_dbg(codec, fmt, ##__VA_ARGS__); } while (0)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001819#endif
1820
Takashi Iwaia7694092013-01-21 10:43:18 +01001821#ifdef DEBUG_BADNESS
1822static inline void print_nid_path_idx(struct hda_codec *codec,
1823 const char *pfx, int idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001824{
Takashi Iwaia7694092013-01-21 10:43:18 +01001825 struct nid_path *path;
1826
1827 path = snd_hda_get_path_from_idx(codec, idx);
1828 if (path)
Takashi Iwai4e76a882014-02-25 12:21:03 +01001829 print_nid_path(codec, pfx, path);
Takashi Iwaia7694092013-01-21 10:43:18 +01001830}
1831
1832static void debug_show_configs(struct hda_codec *codec,
1833 struct auto_pin_cfg *cfg)
1834{
1835 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia7694092013-01-21 10:43:18 +01001836 static const char * const lo_type[3] = { "LO", "SP", "HP" };
Takashi Iwaia7694092013-01-21 10:43:18 +01001837 int i;
1838
1839 debug_badness("multi_outs = %x/%x/%x/%x : %x/%x/%x/%x (type %s)\n",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001840 cfg->line_out_pins[0], cfg->line_out_pins[1],
Takashi Iwai708122e2012-12-20 17:56:57 +01001841 cfg->line_out_pins[2], cfg->line_out_pins[3],
Takashi Iwai352f7f92012-12-19 12:52:06 +01001842 spec->multiout.dac_nids[0],
1843 spec->multiout.dac_nids[1],
1844 spec->multiout.dac_nids[2],
Takashi Iwaia7694092013-01-21 10:43:18 +01001845 spec->multiout.dac_nids[3],
1846 lo_type[cfg->line_out_type]);
1847 for (i = 0; i < cfg->line_outs; i++)
1848 print_nid_path_idx(codec, " out", spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001849 if (spec->multi_ios > 0)
1850 debug_badness("multi_ios(%d) = %x/%x : %x/%x\n",
1851 spec->multi_ios,
1852 spec->multi_io[0].pin, spec->multi_io[1].pin,
1853 spec->multi_io[0].dac, spec->multi_io[1].dac);
Takashi Iwaia7694092013-01-21 10:43:18 +01001854 for (i = 0; i < spec->multi_ios; i++)
1855 print_nid_path_idx(codec, " mio",
1856 spec->out_paths[cfg->line_outs + i]);
1857 if (cfg->hp_outs)
1858 debug_badness("hp_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001859 cfg->hp_pins[0], cfg->hp_pins[1],
Takashi Iwai708122e2012-12-20 17:56:57 +01001860 cfg->hp_pins[2], cfg->hp_pins[3],
Takashi Iwai352f7f92012-12-19 12:52:06 +01001861 spec->multiout.hp_out_nid[0],
1862 spec->multiout.hp_out_nid[1],
1863 spec->multiout.hp_out_nid[2],
1864 spec->multiout.hp_out_nid[3]);
Takashi Iwaia7694092013-01-21 10:43:18 +01001865 for (i = 0; i < cfg->hp_outs; i++)
1866 print_nid_path_idx(codec, " hp ", spec->hp_paths[i]);
1867 if (cfg->speaker_outs)
1868 debug_badness("spk_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001869 cfg->speaker_pins[0], cfg->speaker_pins[1],
1870 cfg->speaker_pins[2], cfg->speaker_pins[3],
1871 spec->multiout.extra_out_nid[0],
1872 spec->multiout.extra_out_nid[1],
1873 spec->multiout.extra_out_nid[2],
1874 spec->multiout.extra_out_nid[3]);
Takashi Iwaia7694092013-01-21 10:43:18 +01001875 for (i = 0; i < cfg->speaker_outs; i++)
1876 print_nid_path_idx(codec, " spk", spec->speaker_paths[i]);
1877 for (i = 0; i < 3; i++)
1878 print_nid_path_idx(codec, " mix", spec->aamix_out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001879}
Takashi Iwaia7694092013-01-21 10:43:18 +01001880#else
1881#define debug_show_configs(codec, cfg) /* NOP */
1882#endif
Takashi Iwai352f7f92012-12-19 12:52:06 +01001883
1884/* find all available DACs of the codec */
1885static void fill_all_dac_nids(struct hda_codec *codec)
1886{
1887 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai7639a062015-03-03 10:07:24 +01001888 hda_nid_t nid;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001889
1890 spec->num_all_dacs = 0;
1891 memset(spec->all_dacs, 0, sizeof(spec->all_dacs));
Takashi Iwai7639a062015-03-03 10:07:24 +01001892 for_each_hda_codec_node(nid, codec) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001893 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_AUD_OUT)
1894 continue;
1895 if (spec->num_all_dacs >= ARRAY_SIZE(spec->all_dacs)) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001896 codec_err(codec, "Too many DACs!\n");
Takashi Iwai352f7f92012-12-19 12:52:06 +01001897 break;
1898 }
1899 spec->all_dacs[spec->num_all_dacs++] = nid;
1900 }
1901}
1902
1903static int parse_output_paths(struct hda_codec *codec)
1904{
1905 struct hda_gen_spec *spec = codec->spec;
1906 struct auto_pin_cfg *cfg = &spec->autocfg;
1907 struct auto_pin_cfg *best_cfg;
Takashi Iwai9314a582013-01-21 10:49:05 +01001908 unsigned int val;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001909 int best_badness = INT_MAX;
1910 int badness;
1911 bool fill_hardwired = true, fill_mio_first = true;
1912 bool best_wired = true, best_mio = true;
1913 bool hp_spk_swapped = false;
1914
Takashi Iwai352f7f92012-12-19 12:52:06 +01001915 best_cfg = kmalloc(sizeof(*best_cfg), GFP_KERNEL);
1916 if (!best_cfg)
1917 return -ENOMEM;
1918 *best_cfg = *cfg;
1919
1920 for (;;) {
1921 badness = fill_and_eval_dacs(codec, fill_hardwired,
1922 fill_mio_first);
1923 if (badness < 0) {
1924 kfree(best_cfg);
1925 return badness;
1926 }
1927 debug_badness("==> lo_type=%d, wired=%d, mio=%d, badness=0x%x\n",
1928 cfg->line_out_type, fill_hardwired, fill_mio_first,
1929 badness);
Takashi Iwaia7694092013-01-21 10:43:18 +01001930 debug_show_configs(codec, cfg);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001931 if (badness < best_badness) {
1932 best_badness = badness;
1933 *best_cfg = *cfg;
1934 best_wired = fill_hardwired;
1935 best_mio = fill_mio_first;
1936 }
1937 if (!badness)
1938 break;
1939 fill_mio_first = !fill_mio_first;
1940 if (!fill_mio_first)
1941 continue;
1942 fill_hardwired = !fill_hardwired;
1943 if (!fill_hardwired)
1944 continue;
1945 if (hp_spk_swapped)
1946 break;
1947 hp_spk_swapped = true;
1948 if (cfg->speaker_outs > 0 &&
1949 cfg->line_out_type == AUTO_PIN_HP_OUT) {
1950 cfg->hp_outs = cfg->line_outs;
1951 memcpy(cfg->hp_pins, cfg->line_out_pins,
1952 sizeof(cfg->hp_pins));
1953 cfg->line_outs = cfg->speaker_outs;
1954 memcpy(cfg->line_out_pins, cfg->speaker_pins,
1955 sizeof(cfg->speaker_pins));
1956 cfg->speaker_outs = 0;
1957 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
1958 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
1959 fill_hardwired = true;
1960 continue;
1961 }
1962 if (cfg->hp_outs > 0 &&
1963 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
1964 cfg->speaker_outs = cfg->line_outs;
1965 memcpy(cfg->speaker_pins, cfg->line_out_pins,
1966 sizeof(cfg->speaker_pins));
1967 cfg->line_outs = cfg->hp_outs;
1968 memcpy(cfg->line_out_pins, cfg->hp_pins,
1969 sizeof(cfg->hp_pins));
1970 cfg->hp_outs = 0;
1971 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
1972 cfg->line_out_type = AUTO_PIN_HP_OUT;
1973 fill_hardwired = true;
1974 continue;
1975 }
1976 break;
1977 }
1978
1979 if (badness) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001980 debug_badness("==> restoring best_cfg\n");
Takashi Iwai352f7f92012-12-19 12:52:06 +01001981 *cfg = *best_cfg;
1982 fill_and_eval_dacs(codec, best_wired, best_mio);
1983 }
1984 debug_badness("==> Best config: lo_type=%d, wired=%d, mio=%d\n",
1985 cfg->line_out_type, best_wired, best_mio);
Takashi Iwaia7694092013-01-21 10:43:18 +01001986 debug_show_configs(codec, cfg);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001987
1988 if (cfg->line_out_pins[0]) {
1989 struct nid_path *path;
Takashi Iwai196c17662013-01-04 15:01:40 +01001990 path = snd_hda_get_path_from_idx(codec, spec->out_paths[0]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001991 if (path)
1992 spec->vmaster_nid = look_for_out_vol_nid(codec, path);
Takashi Iwaid89c6c02014-09-01 10:07:04 +02001993 if (spec->vmaster_nid) {
Takashi Iwai7a71bbf2013-01-17 10:25:15 +01001994 snd_hda_set_vmaster_tlv(codec, spec->vmaster_nid,
1995 HDA_OUTPUT, spec->vmaster_tlv);
Takashi Iwaid89c6c02014-09-01 10:07:04 +02001996 if (spec->dac_min_mute)
1997 spec->vmaster_tlv[3] |= TLV_DB_SCALE_MUTE;
1998 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001999 }
2000
Takashi Iwai9314a582013-01-21 10:49:05 +01002001 /* set initial pinctl targets */
2002 if (spec->prefer_hp_amp || cfg->line_out_type == AUTO_PIN_HP_OUT)
2003 val = PIN_HP;
2004 else
2005 val = PIN_OUT;
2006 set_pin_targets(codec, cfg->line_outs, cfg->line_out_pins, val);
2007 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
2008 set_pin_targets(codec, cfg->hp_outs, cfg->hp_pins, PIN_HP);
2009 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
2010 val = spec->prefer_hp_amp ? PIN_HP : PIN_OUT;
2011 set_pin_targets(codec, cfg->speaker_outs,
2012 cfg->speaker_pins, val);
2013 }
2014
Takashi Iwai55a63d42013-03-21 17:20:12 +01002015 /* clear indep_hp flag if not available */
2016 if (spec->indep_hp && !indep_hp_possible(codec))
2017 spec->indep_hp = 0;
2018
Takashi Iwai352f7f92012-12-19 12:52:06 +01002019 kfree(best_cfg);
2020 return 0;
2021}
2022
2023/* add playback controls from the parsed DAC table */
2024static int create_multi_out_ctls(struct hda_codec *codec,
2025 const struct auto_pin_cfg *cfg)
2026{
2027 struct hda_gen_spec *spec = codec->spec;
2028 int i, err, noutputs;
2029
2030 noutputs = cfg->line_outs;
2031 if (spec->multi_ios > 0 && cfg->line_outs < 3)
2032 noutputs += spec->multi_ios;
2033
2034 for (i = 0; i < noutputs; i++) {
2035 const char *name;
2036 int index;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002037 struct nid_path *path;
2038
Takashi Iwai196c17662013-01-04 15:01:40 +01002039 path = snd_hda_get_path_from_idx(codec, spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002040 if (!path)
2041 continue;
Takashi Iwai247d85e2013-01-17 16:18:11 +01002042
2043 name = get_line_out_pfx(codec, i, &index, NID_PATH_VOL_CTL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002044 if (!name || !strcmp(name, "CLFE")) {
2045 /* Center/LFE */
2046 err = add_vol_ctl(codec, "Center", 0, 1, path);
2047 if (err < 0)
2048 return err;
2049 err = add_vol_ctl(codec, "LFE", 0, 2, path);
2050 if (err < 0)
2051 return err;
Takashi Iwai247d85e2013-01-17 16:18:11 +01002052 } else {
2053 err = add_stereo_vol(codec, name, index, path);
2054 if (err < 0)
2055 return err;
2056 }
2057
2058 name = get_line_out_pfx(codec, i, &index, NID_PATH_MUTE_CTL);
2059 if (!name || !strcmp(name, "CLFE")) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002060 err = add_sw_ctl(codec, "Center", 0, 1, path);
2061 if (err < 0)
2062 return err;
2063 err = add_sw_ctl(codec, "LFE", 0, 2, path);
2064 if (err < 0)
2065 return err;
2066 } else {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002067 err = add_stereo_sw(codec, name, index, path);
2068 if (err < 0)
2069 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 }
2071 }
2072 return 0;
2073}
2074
Takashi Iwaic2c80382013-01-07 10:33:57 +01002075static int create_extra_out(struct hda_codec *codec, int path_idx,
Takashi Iwai196c17662013-01-04 15:01:40 +01002076 const char *pfx, int cidx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002078 struct nid_path *path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 int err;
2080
Takashi Iwai196c17662013-01-04 15:01:40 +01002081 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002082 if (!path)
2083 return 0;
Takashi Iwaic2c80382013-01-07 10:33:57 +01002084 err = add_stereo_vol(codec, pfx, cidx, path);
2085 if (err < 0)
2086 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002087 err = add_stereo_sw(codec, pfx, cidx, path);
2088 if (err < 0)
2089 return err;
2090 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091}
2092
Takashi Iwai352f7f92012-12-19 12:52:06 +01002093/* add playback controls for speaker and HP outputs */
2094static int create_extra_outs(struct hda_codec *codec, int num_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01002095 const int *paths, const char *pfx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096{
Takashi Iwaic2c80382013-01-07 10:33:57 +01002097 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002098
2099 for (i = 0; i < num_pins; i++) {
Takashi Iwaic2c80382013-01-07 10:33:57 +01002100 const char *name;
Takashi Iwai975cc022013-06-28 11:56:49 +02002101 char tmp[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
Takashi Iwaic2c80382013-01-07 10:33:57 +01002102 int err, idx = 0;
2103
2104 if (num_pins == 2 && i == 1 && !strcmp(pfx, "Speaker"))
2105 name = "Bass Speaker";
2106 else if (num_pins >= 3) {
2107 snprintf(tmp, sizeof(tmp), "%s %s",
Takashi Iwai352f7f92012-12-19 12:52:06 +01002108 pfx, channel_name[i]);
Takashi Iwaic2c80382013-01-07 10:33:57 +01002109 name = tmp;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002110 } else {
Takashi Iwaic2c80382013-01-07 10:33:57 +01002111 name = pfx;
2112 idx = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 }
Takashi Iwaic2c80382013-01-07 10:33:57 +01002114 err = create_extra_out(codec, paths[i], name, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002115 if (err < 0)
2116 return err;
2117 }
2118 return 0;
2119}
Takashi Iwai97ec5582006-03-21 11:29:07 +01002120
Takashi Iwai352f7f92012-12-19 12:52:06 +01002121static int create_hp_out_ctls(struct hda_codec *codec)
2122{
2123 struct hda_gen_spec *spec = codec->spec;
2124 return create_extra_outs(codec, spec->autocfg.hp_outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01002125 spec->hp_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01002126 "Headphone");
2127}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128
Takashi Iwai352f7f92012-12-19 12:52:06 +01002129static int create_speaker_out_ctls(struct hda_codec *codec)
2130{
2131 struct hda_gen_spec *spec = codec->spec;
2132 return create_extra_outs(codec, spec->autocfg.speaker_outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01002133 spec->speaker_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01002134 "Speaker");
2135}
2136
2137/*
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002138 * independent HP controls
2139 */
2140
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02002141static void call_hp_automute(struct hda_codec *codec,
2142 struct hda_jack_callback *jack);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002143static int indep_hp_info(struct snd_kcontrol *kcontrol,
2144 struct snd_ctl_elem_info *uinfo)
2145{
2146 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
2147}
2148
2149static int indep_hp_get(struct snd_kcontrol *kcontrol,
2150 struct snd_ctl_elem_value *ucontrol)
2151{
2152 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2153 struct hda_gen_spec *spec = codec->spec;
2154 ucontrol->value.enumerated.item[0] = spec->indep_hp_enabled;
2155 return 0;
2156}
2157
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002158static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
2159 int nomix_path_idx, int mix_path_idx,
2160 int out_type);
2161
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002162static int indep_hp_put(struct snd_kcontrol *kcontrol,
2163 struct snd_ctl_elem_value *ucontrol)
2164{
2165 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2166 struct hda_gen_spec *spec = codec->spec;
2167 unsigned int select = ucontrol->value.enumerated.item[0];
2168 int ret = 0;
2169
2170 mutex_lock(&spec->pcm_mutex);
2171 if (spec->active_streams) {
2172 ret = -EBUSY;
2173 goto unlock;
2174 }
2175
2176 if (spec->indep_hp_enabled != select) {
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002177 hda_nid_t *dacp;
2178 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
2179 dacp = &spec->private_dac_nids[0];
2180 else
2181 dacp = &spec->multiout.hp_out_nid[0];
2182
2183 /* update HP aamix paths in case it conflicts with indep HP */
2184 if (spec->have_aamix_ctl) {
2185 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
2186 update_aamix_paths(codec, spec->aamix_mode,
2187 spec->out_paths[0],
2188 spec->aamix_out_paths[0],
2189 spec->autocfg.line_out_type);
2190 else
2191 update_aamix_paths(codec, spec->aamix_mode,
2192 spec->hp_paths[0],
2193 spec->aamix_out_paths[1],
2194 AUTO_PIN_HP_OUT);
2195 }
2196
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002197 spec->indep_hp_enabled = select;
2198 if (spec->indep_hp_enabled)
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002199 *dacp = 0;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002200 else
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002201 *dacp = spec->alt_dac_nid;
Takashi Iwai92603c52013-01-22 07:46:31 +01002202
Takashi Iwai963afde2013-05-31 15:20:31 +02002203 call_hp_automute(codec, NULL);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002204 ret = 1;
2205 }
2206 unlock:
2207 mutex_unlock(&spec->pcm_mutex);
2208 return ret;
2209}
2210
2211static const struct snd_kcontrol_new indep_hp_ctl = {
2212 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2213 .name = "Independent HP",
2214 .info = indep_hp_info,
2215 .get = indep_hp_get,
2216 .put = indep_hp_put,
2217};
2218
2219
2220static int create_indep_hp_ctls(struct hda_codec *codec)
2221{
2222 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002223 hda_nid_t dac;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002224
2225 if (!spec->indep_hp)
2226 return 0;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002227 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
2228 dac = spec->multiout.dac_nids[0];
2229 else
2230 dac = spec->multiout.hp_out_nid[0];
2231 if (!dac) {
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002232 spec->indep_hp = 0;
2233 return 0;
2234 }
2235
2236 spec->indep_hp_enabled = false;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002237 spec->alt_dac_nid = dac;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01002238 if (!snd_hda_gen_add_kctl(spec, NULL, &indep_hp_ctl))
2239 return -ENOMEM;
2240 return 0;
2241}
2242
2243/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01002244 * channel mode enum control
2245 */
2246
2247static int ch_mode_info(struct snd_kcontrol *kcontrol,
2248 struct snd_ctl_elem_info *uinfo)
2249{
2250 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2251 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002252 int chs;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002253
2254 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2255 uinfo->count = 1;
2256 uinfo->value.enumerated.items = spec->multi_ios + 1;
2257 if (uinfo->value.enumerated.item > spec->multi_ios)
2258 uinfo->value.enumerated.item = spec->multi_ios;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002259 chs = uinfo->value.enumerated.item * 2 + spec->min_channel_count;
2260 sprintf(uinfo->value.enumerated.name, "%dch", chs);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002261 return 0;
2262}
2263
2264static int ch_mode_get(struct snd_kcontrol *kcontrol,
2265 struct snd_ctl_elem_value *ucontrol)
2266{
2267 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2268 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002269 ucontrol->value.enumerated.item[0] =
2270 (spec->ext_channel_count - spec->min_channel_count) / 2;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002271 return 0;
2272}
2273
Takashi Iwai196c17662013-01-04 15:01:40 +01002274static inline struct nid_path *
2275get_multiio_path(struct hda_codec *codec, int idx)
2276{
2277 struct hda_gen_spec *spec = codec->spec;
2278 return snd_hda_get_path_from_idx(codec,
2279 spec->out_paths[spec->autocfg.line_outs + idx]);
2280}
2281
Takashi Iwaia5cc2502013-01-16 18:08:55 +01002282static void update_automute_all(struct hda_codec *codec);
2283
Takashi Iwai65033cc2013-04-16 12:31:05 +02002284/* Default value to be passed as aamix argument for snd_hda_activate_path();
2285 * used for output paths
2286 */
2287static bool aamix_default(struct hda_gen_spec *spec)
2288{
2289 return !spec->have_aamix_ctl || spec->aamix_mode;
2290}
2291
Takashi Iwai352f7f92012-12-19 12:52:06 +01002292static int set_multi_io(struct hda_codec *codec, int idx, bool output)
2293{
2294 struct hda_gen_spec *spec = codec->spec;
2295 hda_nid_t nid = spec->multi_io[idx].pin;
2296 struct nid_path *path;
2297
Takashi Iwai196c17662013-01-04 15:01:40 +01002298 path = get_multiio_path(codec, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002299 if (!path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 return -EINVAL;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002301
2302 if (path->active == output)
2303 return 0;
2304
2305 if (output) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01002306 set_pin_target(codec, nid, PIN_OUT, true);
Takashi Iwai65033cc2013-04-16 12:31:05 +02002307 snd_hda_activate_path(codec, path, true, aamix_default(spec));
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01002308 set_pin_eapd(codec, nid, true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002309 } else {
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01002310 set_pin_eapd(codec, nid, false);
Takashi Iwai65033cc2013-04-16 12:31:05 +02002311 snd_hda_activate_path(codec, path, false, aamix_default(spec));
Takashi Iwai2c12c302013-01-10 09:33:29 +01002312 set_pin_target(codec, nid, spec->multi_io[idx].ctl_in, true);
Takashi Iwai55196ff2013-01-24 17:32:56 +01002313 path_power_down_sync(codec, path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 }
Takashi Iwaia365fed2013-01-10 16:10:06 +01002315
2316 /* update jack retasking in case it modifies any of them */
Takashi Iwaia5cc2502013-01-16 18:08:55 +01002317 update_automute_all(codec);
Takashi Iwaia365fed2013-01-10 16:10:06 +01002318
Takashi Iwai352f7f92012-12-19 12:52:06 +01002319 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320}
2321
Takashi Iwai352f7f92012-12-19 12:52:06 +01002322static int ch_mode_put(struct snd_kcontrol *kcontrol,
2323 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002325 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2326 struct hda_gen_spec *spec = codec->spec;
2327 int i, ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328
Takashi Iwai352f7f92012-12-19 12:52:06 +01002329 ch = ucontrol->value.enumerated.item[0];
2330 if (ch < 0 || ch > spec->multi_ios)
2331 return -EINVAL;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002332 if (ch == (spec->ext_channel_count - spec->min_channel_count) / 2)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002333 return 0;
Takashi Iwaia07a9492013-01-07 16:44:06 +01002334 spec->ext_channel_count = ch * 2 + spec->min_channel_count;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002335 for (i = 0; i < spec->multi_ios; i++)
2336 set_multi_io(codec, i, i < ch);
2337 spec->multiout.max_channels = max(spec->ext_channel_count,
2338 spec->const_channel_count);
2339 if (spec->need_dac_fix)
2340 spec->multiout.num_dacs = spec->multiout.max_channels / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 return 1;
2342}
2343
Takashi Iwai352f7f92012-12-19 12:52:06 +01002344static const struct snd_kcontrol_new channel_mode_enum = {
2345 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2346 .name = "Channel Mode",
2347 .info = ch_mode_info,
2348 .get = ch_mode_get,
2349 .put = ch_mode_put,
2350};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351
Takashi Iwai352f7f92012-12-19 12:52:06 +01002352static int create_multi_channel_mode(struct hda_codec *codec)
2353{
2354 struct hda_gen_spec *spec = codec->spec;
2355
2356 if (spec->multi_ios > 0) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01002357 if (!snd_hda_gen_add_kctl(spec, NULL, &channel_mode_enum))
Takashi Iwai352f7f92012-12-19 12:52:06 +01002358 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 return 0;
2361}
2362
Takashi Iwai352f7f92012-12-19 12:52:06 +01002363/*
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002364 * aamix loopback enable/disable switch
2365 */
2366
2367#define loopback_mixing_info indep_hp_info
2368
2369static int loopback_mixing_get(struct snd_kcontrol *kcontrol,
2370 struct snd_ctl_elem_value *ucontrol)
2371{
2372 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2373 struct hda_gen_spec *spec = codec->spec;
2374 ucontrol->value.enumerated.item[0] = spec->aamix_mode;
2375 return 0;
2376}
2377
2378static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002379 int nomix_path_idx, int mix_path_idx,
2380 int out_type)
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002381{
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002382 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002383 struct nid_path *nomix_path, *mix_path;
2384
2385 nomix_path = snd_hda_get_path_from_idx(codec, nomix_path_idx);
2386 mix_path = snd_hda_get_path_from_idx(codec, mix_path_idx);
2387 if (!nomix_path || !mix_path)
2388 return;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002389
2390 /* if HP aamix path is driven from a different DAC and the
2391 * independent HP mode is ON, can't turn on aamix path
2392 */
2393 if (out_type == AUTO_PIN_HP_OUT && spec->indep_hp_enabled &&
2394 mix_path->path[0] != spec->alt_dac_nid)
2395 do_mix = false;
2396
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002397 if (do_mix) {
2398 snd_hda_activate_path(codec, nomix_path, false, true);
2399 snd_hda_activate_path(codec, mix_path, true, true);
Takashi Iwai55196ff2013-01-24 17:32:56 +01002400 path_power_down_sync(codec, nomix_path);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002401 } else {
Takashi Iwai65033cc2013-04-16 12:31:05 +02002402 snd_hda_activate_path(codec, mix_path, false, false);
2403 snd_hda_activate_path(codec, nomix_path, true, false);
Takashi Iwai55196ff2013-01-24 17:32:56 +01002404 path_power_down_sync(codec, mix_path);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002405 }
2406}
2407
2408static int loopback_mixing_put(struct snd_kcontrol *kcontrol,
2409 struct snd_ctl_elem_value *ucontrol)
2410{
2411 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2412 struct hda_gen_spec *spec = codec->spec;
2413 unsigned int val = ucontrol->value.enumerated.item[0];
2414
2415 if (val == spec->aamix_mode)
2416 return 0;
2417 spec->aamix_mode = val;
2418 update_aamix_paths(codec, val, spec->out_paths[0],
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002419 spec->aamix_out_paths[0],
2420 spec->autocfg.line_out_type);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002421 update_aamix_paths(codec, val, spec->hp_paths[0],
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002422 spec->aamix_out_paths[1],
2423 AUTO_PIN_HP_OUT);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002424 update_aamix_paths(codec, val, spec->speaker_paths[0],
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002425 spec->aamix_out_paths[2],
2426 AUTO_PIN_SPEAKER_OUT);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002427 return 1;
2428}
2429
2430static const struct snd_kcontrol_new loopback_mixing_enum = {
2431 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2432 .name = "Loopback Mixing",
2433 .info = loopback_mixing_info,
2434 .get = loopback_mixing_get,
2435 .put = loopback_mixing_put,
2436};
2437
2438static int create_loopback_mixing_ctl(struct hda_codec *codec)
2439{
2440 struct hda_gen_spec *spec = codec->spec;
2441
2442 if (!spec->mixer_nid)
2443 return 0;
2444 if (!(spec->aamix_out_paths[0] || spec->aamix_out_paths[1] ||
2445 spec->aamix_out_paths[2]))
2446 return 0;
2447 if (!snd_hda_gen_add_kctl(spec, NULL, &loopback_mixing_enum))
2448 return -ENOMEM;
Takashi Iwaia1e908e2013-01-21 15:11:25 +01002449 spec->have_aamix_ctl = 1;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01002450 return 0;
2451}
2452
2453/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01002454 * shared headphone/mic handling
2455 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02002456
Takashi Iwai352f7f92012-12-19 12:52:06 +01002457static void call_update_outputs(struct hda_codec *codec);
2458
2459/* for shared I/O, change the pin-control accordingly */
Takashi Iwai967303d2013-02-19 17:12:42 +01002460static void update_hp_mic(struct hda_codec *codec, int adc_mux, bool force)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002461{
2462 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai967303d2013-02-19 17:12:42 +01002463 bool as_mic;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002464 unsigned int val;
Takashi Iwai967303d2013-02-19 17:12:42 +01002465 hda_nid_t pin;
2466
2467 pin = spec->hp_mic_pin;
2468 as_mic = spec->cur_mux[adc_mux] == spec->hp_mic_mux_idx;
2469
2470 if (!force) {
2471 val = snd_hda_codec_get_pin_target(codec, pin);
2472 if (as_mic) {
2473 if (val & PIN_IN)
2474 return;
2475 } else {
2476 if (val & PIN_OUT)
2477 return;
2478 }
2479 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002480
2481 val = snd_hda_get_default_vref(codec, pin);
Takashi Iwai967303d2013-02-19 17:12:42 +01002482 /* if the HP pin doesn't support VREF and the codec driver gives an
2483 * alternative pin, set up the VREF on that pin instead
2484 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01002485 if (val == AC_PINCTL_VREF_HIZ && spec->shared_mic_vref_pin) {
2486 const hda_nid_t vref_pin = spec->shared_mic_vref_pin;
2487 unsigned int vref_val = snd_hda_get_default_vref(codec, vref_pin);
2488 if (vref_val != AC_PINCTL_VREF_HIZ)
Takashi Iwai7594aa32012-12-20 15:38:40 +01002489 snd_hda_set_pin_ctl_cache(codec, vref_pin,
Takashi Iwai967303d2013-02-19 17:12:42 +01002490 PIN_IN | (as_mic ? vref_val : 0));
Takashi Iwaicb53c622007-08-10 17:21:45 +02002491 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002492
Takashi Iwai8ba955c2013-03-07 18:40:58 +01002493 if (!spec->hp_mic_jack_modes) {
2494 if (as_mic)
2495 val |= PIN_IN;
2496 else
2497 val = PIN_HP;
2498 set_pin_target(codec, pin, val, true);
Takashi Iwai963afde2013-05-31 15:20:31 +02002499 call_hp_automute(codec, NULL);
Takashi Iwai8ba955c2013-03-07 18:40:58 +01002500 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002501}
2502
2503/* create a shared input with the headphone out */
Takashi Iwai967303d2013-02-19 17:12:42 +01002504static int create_hp_mic(struct hda_codec *codec)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002505{
2506 struct hda_gen_spec *spec = codec->spec;
2507 struct auto_pin_cfg *cfg = &spec->autocfg;
2508 unsigned int defcfg;
2509 hda_nid_t nid;
2510
Takashi Iwai967303d2013-02-19 17:12:42 +01002511 if (!spec->hp_mic) {
2512 if (spec->suppress_hp_mic_detect)
2513 return 0;
2514 /* automatic detection: only if no input or a single internal
2515 * input pin is found, try to detect the shared hp/mic
2516 */
2517 if (cfg->num_inputs > 1)
2518 return 0;
2519 else if (cfg->num_inputs == 1) {
2520 defcfg = snd_hda_codec_get_pincfg(codec, cfg->inputs[0].pin);
2521 if (snd_hda_get_input_pin_attr(defcfg) != INPUT_PIN_ATTR_INT)
2522 return 0;
2523 }
2524 }
2525
2526 spec->hp_mic = 0; /* clear once */
2527 if (cfg->num_inputs >= AUTO_CFG_MAX_INS)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002528 return 0;
2529
Takashi Iwai967303d2013-02-19 17:12:42 +01002530 nid = 0;
2531 if (cfg->line_out_type == AUTO_PIN_HP_OUT && cfg->line_outs > 0)
2532 nid = cfg->line_out_pins[0];
2533 else if (cfg->hp_outs > 0)
2534 nid = cfg->hp_pins[0];
2535 if (!nid)
2536 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002537
2538 if (!(snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_IN))
2539 return 0; /* no input */
2540
Takashi Iwai967303d2013-02-19 17:12:42 +01002541 cfg->inputs[cfg->num_inputs].pin = nid;
2542 cfg->inputs[cfg->num_inputs].type = AUTO_PIN_MIC;
David Henningssoncb420b12013-04-11 11:30:28 +02002543 cfg->inputs[cfg->num_inputs].is_headphone_mic = 1;
Takashi Iwai967303d2013-02-19 17:12:42 +01002544 cfg->num_inputs++;
2545 spec->hp_mic = 1;
2546 spec->hp_mic_pin = nid;
2547 /* we can't handle auto-mic together with HP-mic */
2548 spec->suppress_auto_mic = 1;
Takashi Iwai4e76a882014-02-25 12:21:03 +01002549 codec_dbg(codec, "Enable shared I/O jack on NID 0x%x\n", nid);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002550 return 0;
2551}
2552
Takashi Iwai978e77e2013-01-10 16:57:58 +01002553/*
2554 * output jack mode
2555 */
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002556
2557static int create_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t pin);
2558
2559static const char * const out_jack_texts[] = {
2560 "Line Out", "Headphone Out",
2561};
2562
Takashi Iwai978e77e2013-01-10 16:57:58 +01002563static int out_jack_mode_info(struct snd_kcontrol *kcontrol,
2564 struct snd_ctl_elem_info *uinfo)
2565{
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002566 return snd_hda_enum_helper_info(kcontrol, uinfo, 2, out_jack_texts);
Takashi Iwai978e77e2013-01-10 16:57:58 +01002567}
2568
2569static int out_jack_mode_get(struct snd_kcontrol *kcontrol,
2570 struct snd_ctl_elem_value *ucontrol)
2571{
2572 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2573 hda_nid_t nid = kcontrol->private_value;
2574 if (snd_hda_codec_get_pin_target(codec, nid) == PIN_HP)
2575 ucontrol->value.enumerated.item[0] = 1;
2576 else
2577 ucontrol->value.enumerated.item[0] = 0;
2578 return 0;
2579}
2580
2581static int out_jack_mode_put(struct snd_kcontrol *kcontrol,
2582 struct snd_ctl_elem_value *ucontrol)
2583{
2584 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2585 hda_nid_t nid = kcontrol->private_value;
2586 unsigned int val;
2587
2588 val = ucontrol->value.enumerated.item[0] ? PIN_HP : PIN_OUT;
2589 if (snd_hda_codec_get_pin_target(codec, nid) == val)
2590 return 0;
2591 snd_hda_set_pin_ctl_cache(codec, nid, val);
2592 return 1;
2593}
2594
2595static const struct snd_kcontrol_new out_jack_mode_enum = {
2596 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2597 .info = out_jack_mode_info,
2598 .get = out_jack_mode_get,
2599 .put = out_jack_mode_put,
2600};
2601
2602static bool find_kctl_name(struct hda_codec *codec, const char *name, int idx)
2603{
2604 struct hda_gen_spec *spec = codec->spec;
2605 int i;
2606
2607 for (i = 0; i < spec->kctls.used; i++) {
2608 struct snd_kcontrol_new *kctl = snd_array_elem(&spec->kctls, i);
2609 if (!strcmp(kctl->name, name) && kctl->index == idx)
2610 return true;
2611 }
2612 return false;
2613}
2614
2615static void get_jack_mode_name(struct hda_codec *codec, hda_nid_t pin,
2616 char *name, size_t name_len)
2617{
2618 struct hda_gen_spec *spec = codec->spec;
2619 int idx = 0;
2620
2621 snd_hda_get_pin_label(codec, pin, &spec->autocfg, name, name_len, &idx);
2622 strlcat(name, " Jack Mode", name_len);
2623
2624 for (; find_kctl_name(codec, name, idx); idx++)
2625 ;
2626}
2627
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002628static int get_out_jack_num_items(struct hda_codec *codec, hda_nid_t pin)
2629{
2630 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaif811c3c2013-03-07 18:32:59 +01002631 if (spec->add_jack_modes) {
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002632 unsigned int pincap = snd_hda_query_pin_caps(codec, pin);
2633 if ((pincap & AC_PINCAP_OUT) && (pincap & AC_PINCAP_HP_DRV))
2634 return 2;
2635 }
2636 return 1;
2637}
2638
Takashi Iwai978e77e2013-01-10 16:57:58 +01002639static int create_out_jack_modes(struct hda_codec *codec, int num_pins,
2640 hda_nid_t *pins)
2641{
2642 struct hda_gen_spec *spec = codec->spec;
2643 int i;
2644
2645 for (i = 0; i < num_pins; i++) {
2646 hda_nid_t pin = pins[i];
Takashi Iwaiced4cef2013-11-26 08:33:45 +01002647 if (pin == spec->hp_mic_pin)
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002648 continue;
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002649 if (get_out_jack_num_items(codec, pin) > 1) {
Takashi Iwai978e77e2013-01-10 16:57:58 +01002650 struct snd_kcontrol_new *knew;
Takashi Iwai975cc022013-06-28 11:56:49 +02002651 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
Takashi Iwai978e77e2013-01-10 16:57:58 +01002652 get_jack_mode_name(codec, pin, name, sizeof(name));
2653 knew = snd_hda_gen_add_kctl(spec, name,
2654 &out_jack_mode_enum);
2655 if (!knew)
2656 return -ENOMEM;
2657 knew->private_value = pin;
2658 }
2659 }
2660
2661 return 0;
2662}
2663
Takashi Iwai294765582013-01-17 09:52:11 +01002664/*
2665 * input jack mode
2666 */
2667
2668/* from AC_PINCTL_VREF_HIZ to AC_PINCTL_VREF_100 */
2669#define NUM_VREFS 6
2670
2671static const char * const vref_texts[NUM_VREFS] = {
2672 "Line In", "Mic 50pc Bias", "Mic 0V Bias",
2673 "", "Mic 80pc Bias", "Mic 100pc Bias"
2674};
2675
2676static unsigned int get_vref_caps(struct hda_codec *codec, hda_nid_t pin)
2677{
2678 unsigned int pincap;
2679
2680 pincap = snd_hda_query_pin_caps(codec, pin);
2681 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
2682 /* filter out unusual vrefs */
2683 pincap &= ~(AC_PINCAP_VREF_GRD | AC_PINCAP_VREF_100);
2684 return pincap;
2685}
2686
2687/* convert from the enum item index to the vref ctl index (0=HIZ, 1=50%...) */
2688static int get_vref_idx(unsigned int vref_caps, unsigned int item_idx)
2689{
2690 unsigned int i, n = 0;
2691
2692 for (i = 0; i < NUM_VREFS; i++) {
2693 if (vref_caps & (1 << i)) {
2694 if (n == item_idx)
2695 return i;
2696 n++;
2697 }
2698 }
2699 return 0;
2700}
2701
2702/* convert back from the vref ctl index to the enum item index */
2703static int cvt_from_vref_idx(unsigned int vref_caps, unsigned int idx)
2704{
2705 unsigned int i, n = 0;
2706
2707 for (i = 0; i < NUM_VREFS; i++) {
2708 if (i == idx)
2709 return n;
2710 if (vref_caps & (1 << i))
2711 n++;
2712 }
2713 return 0;
2714}
2715
2716static int in_jack_mode_info(struct snd_kcontrol *kcontrol,
2717 struct snd_ctl_elem_info *uinfo)
2718{
2719 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2720 hda_nid_t nid = kcontrol->private_value;
2721 unsigned int vref_caps = get_vref_caps(codec, nid);
2722
2723 snd_hda_enum_helper_info(kcontrol, uinfo, hweight32(vref_caps),
2724 vref_texts);
2725 /* set the right text */
2726 strcpy(uinfo->value.enumerated.name,
2727 vref_texts[get_vref_idx(vref_caps, uinfo->value.enumerated.item)]);
2728 return 0;
2729}
2730
2731static int in_jack_mode_get(struct snd_kcontrol *kcontrol,
2732 struct snd_ctl_elem_value *ucontrol)
2733{
2734 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2735 hda_nid_t nid = kcontrol->private_value;
2736 unsigned int vref_caps = get_vref_caps(codec, nid);
2737 unsigned int idx;
2738
2739 idx = snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_VREFEN;
2740 ucontrol->value.enumerated.item[0] = cvt_from_vref_idx(vref_caps, idx);
2741 return 0;
2742}
2743
2744static int in_jack_mode_put(struct snd_kcontrol *kcontrol,
2745 struct snd_ctl_elem_value *ucontrol)
2746{
2747 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2748 hda_nid_t nid = kcontrol->private_value;
2749 unsigned int vref_caps = get_vref_caps(codec, nid);
2750 unsigned int val, idx;
2751
2752 val = snd_hda_codec_get_pin_target(codec, nid);
2753 idx = cvt_from_vref_idx(vref_caps, val & AC_PINCTL_VREFEN);
2754 if (idx == ucontrol->value.enumerated.item[0])
2755 return 0;
2756
2757 val &= ~AC_PINCTL_VREFEN;
2758 val |= get_vref_idx(vref_caps, ucontrol->value.enumerated.item[0]);
2759 snd_hda_set_pin_ctl_cache(codec, nid, val);
2760 return 1;
2761}
2762
2763static const struct snd_kcontrol_new in_jack_mode_enum = {
2764 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2765 .info = in_jack_mode_info,
2766 .get = in_jack_mode_get,
2767 .put = in_jack_mode_put,
2768};
2769
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002770static int get_in_jack_num_items(struct hda_codec *codec, hda_nid_t pin)
2771{
2772 struct hda_gen_spec *spec = codec->spec;
2773 int nitems = 0;
Takashi Iwaif811c3c2013-03-07 18:32:59 +01002774 if (spec->add_jack_modes)
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002775 nitems = hweight32(get_vref_caps(codec, pin));
2776 return nitems ? nitems : 1;
2777}
2778
Takashi Iwai294765582013-01-17 09:52:11 +01002779static int create_in_jack_mode(struct hda_codec *codec, hda_nid_t pin)
2780{
2781 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai294765582013-01-17 09:52:11 +01002782 struct snd_kcontrol_new *knew;
Takashi Iwai975cc022013-06-28 11:56:49 +02002783 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002784 unsigned int defcfg;
2785
Takashi Iwaif811c3c2013-03-07 18:32:59 +01002786 if (pin == spec->hp_mic_pin)
2787 return 0; /* already done in create_out_jack_mode() */
Takashi Iwai294765582013-01-17 09:52:11 +01002788
2789 /* no jack mode for fixed pins */
2790 defcfg = snd_hda_codec_get_pincfg(codec, pin);
2791 if (snd_hda_get_input_pin_attr(defcfg) == INPUT_PIN_ATTR_INT)
2792 return 0;
2793
2794 /* no multiple vref caps? */
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002795 if (get_in_jack_num_items(codec, pin) <= 1)
Takashi Iwai294765582013-01-17 09:52:11 +01002796 return 0;
2797
2798 get_jack_mode_name(codec, pin, name, sizeof(name));
2799 knew = snd_hda_gen_add_kctl(spec, name, &in_jack_mode_enum);
2800 if (!knew)
2801 return -ENOMEM;
2802 knew->private_value = pin;
2803 return 0;
2804}
2805
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002806/*
2807 * HP/mic shared jack mode
2808 */
2809static int hp_mic_jack_mode_info(struct snd_kcontrol *kcontrol,
2810 struct snd_ctl_elem_info *uinfo)
2811{
2812 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2813 hda_nid_t nid = kcontrol->private_value;
2814 int out_jacks = get_out_jack_num_items(codec, nid);
2815 int in_jacks = get_in_jack_num_items(codec, nid);
2816 const char *text = NULL;
2817 int idx;
2818
2819 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2820 uinfo->count = 1;
2821 uinfo->value.enumerated.items = out_jacks + in_jacks;
2822 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2823 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
2824 idx = uinfo->value.enumerated.item;
2825 if (idx < out_jacks) {
2826 if (out_jacks > 1)
2827 text = out_jack_texts[idx];
2828 else
2829 text = "Headphone Out";
2830 } else {
2831 idx -= out_jacks;
2832 if (in_jacks > 1) {
2833 unsigned int vref_caps = get_vref_caps(codec, nid);
2834 text = vref_texts[get_vref_idx(vref_caps, idx)];
2835 } else
2836 text = "Mic In";
2837 }
2838
2839 strcpy(uinfo->value.enumerated.name, text);
2840 return 0;
2841}
2842
2843static int get_cur_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t nid)
2844{
2845 int out_jacks = get_out_jack_num_items(codec, nid);
2846 int in_jacks = get_in_jack_num_items(codec, nid);
2847 unsigned int val = snd_hda_codec_get_pin_target(codec, nid);
2848 int idx = 0;
2849
2850 if (val & PIN_OUT) {
2851 if (out_jacks > 1 && val == PIN_HP)
2852 idx = 1;
2853 } else if (val & PIN_IN) {
2854 idx = out_jacks;
2855 if (in_jacks > 1) {
2856 unsigned int vref_caps = get_vref_caps(codec, nid);
2857 val &= AC_PINCTL_VREFEN;
2858 idx += cvt_from_vref_idx(vref_caps, val);
2859 }
2860 }
2861 return idx;
2862}
2863
2864static int hp_mic_jack_mode_get(struct snd_kcontrol *kcontrol,
2865 struct snd_ctl_elem_value *ucontrol)
2866{
2867 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2868 hda_nid_t nid = kcontrol->private_value;
2869 ucontrol->value.enumerated.item[0] =
2870 get_cur_hp_mic_jack_mode(codec, nid);
2871 return 0;
2872}
2873
2874static int hp_mic_jack_mode_put(struct snd_kcontrol *kcontrol,
2875 struct snd_ctl_elem_value *ucontrol)
2876{
2877 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2878 hda_nid_t nid = kcontrol->private_value;
2879 int out_jacks = get_out_jack_num_items(codec, nid);
2880 int in_jacks = get_in_jack_num_items(codec, nid);
2881 unsigned int val, oldval, idx;
2882
2883 oldval = get_cur_hp_mic_jack_mode(codec, nid);
2884 idx = ucontrol->value.enumerated.item[0];
2885 if (oldval == idx)
2886 return 0;
2887
2888 if (idx < out_jacks) {
2889 if (out_jacks > 1)
2890 val = idx ? PIN_HP : PIN_OUT;
2891 else
2892 val = PIN_HP;
2893 } else {
2894 idx -= out_jacks;
2895 if (in_jacks > 1) {
2896 unsigned int vref_caps = get_vref_caps(codec, nid);
2897 val = snd_hda_codec_get_pin_target(codec, nid);
Takashi Iwai3f550e32013-03-07 18:30:27 +01002898 val &= ~(AC_PINCTL_VREFEN | PIN_HP);
2899 val |= get_vref_idx(vref_caps, idx) | PIN_IN;
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002900 } else
Takashi Iwai16c0cefe2013-11-26 08:44:26 +01002901 val = snd_hda_get_default_vref(codec, nid) | PIN_IN;
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002902 }
2903 snd_hda_set_pin_ctl_cache(codec, nid, val);
Takashi Iwai963afde2013-05-31 15:20:31 +02002904 call_hp_automute(codec, NULL);
Takashi Iwai8ba955c2013-03-07 18:40:58 +01002905
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002906 return 1;
2907}
2908
2909static const struct snd_kcontrol_new hp_mic_jack_mode_enum = {
2910 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2911 .info = hp_mic_jack_mode_info,
2912 .get = hp_mic_jack_mode_get,
2913 .put = hp_mic_jack_mode_put,
2914};
2915
2916static int create_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t pin)
2917{
2918 struct hda_gen_spec *spec = codec->spec;
2919 struct snd_kcontrol_new *knew;
2920
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002921 knew = snd_hda_gen_add_kctl(spec, "Headphone Mic Jack Mode",
2922 &hp_mic_jack_mode_enum);
2923 if (!knew)
2924 return -ENOMEM;
2925 knew->private_value = pin;
Takashi Iwai8ba955c2013-03-07 18:40:58 +01002926 spec->hp_mic_jack_modes = 1;
Takashi Iwai5f171ba2013-02-19 18:14:54 +01002927 return 0;
2928}
Takashi Iwai352f7f92012-12-19 12:52:06 +01002929
2930/*
2931 * Parse input paths
2932 */
2933
Takashi Iwai352f7f92012-12-19 12:52:06 +01002934/* add the powersave loopback-list entry */
Takashi Iwai0186f4f2013-02-07 10:45:11 +01002935static int add_loopback_list(struct hda_gen_spec *spec, hda_nid_t mix, int idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002936{
2937 struct hda_amp_list *list;
2938
Takashi Iwai0186f4f2013-02-07 10:45:11 +01002939 list = snd_array_new(&spec->loopback_list);
2940 if (!list)
2941 return -ENOMEM;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002942 list->nid = mix;
2943 list->dir = HDA_INPUT;
2944 list->idx = idx;
Takashi Iwai0186f4f2013-02-07 10:45:11 +01002945 spec->loopback.amplist = spec->loopback_list.list;
2946 return 0;
Takashi Iwaicb53c622007-08-10 17:21:45 +02002947}
Takashi Iwaicb53c622007-08-10 17:21:45 +02002948
Takashi Iwai2ded3e52013-11-28 11:05:28 +01002949/* return true if either a volume or a mute amp is found for the given
2950 * aamix path; the amp has to be either in the mixer node or its direct leaf
2951 */
2952static bool look_for_mix_leaf_ctls(struct hda_codec *codec, hda_nid_t mix_nid,
2953 hda_nid_t pin, unsigned int *mix_val,
2954 unsigned int *mute_val)
2955{
2956 int idx, num_conns;
2957 const hda_nid_t *list;
2958 hda_nid_t nid;
2959
2960 idx = snd_hda_get_conn_index(codec, mix_nid, pin, true);
2961 if (idx < 0)
2962 return false;
2963
2964 *mix_val = *mute_val = 0;
2965 if (nid_has_volume(codec, mix_nid, HDA_INPUT))
2966 *mix_val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
2967 if (nid_has_mute(codec, mix_nid, HDA_INPUT))
2968 *mute_val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
2969 if (*mix_val && *mute_val)
2970 return true;
2971
2972 /* check leaf node */
2973 num_conns = snd_hda_get_conn_list(codec, mix_nid, &list);
2974 if (num_conns < idx)
2975 return false;
2976 nid = list[idx];
Takashi Iwai43a8e502014-01-07 18:11:44 +01002977 if (!*mix_val && nid_has_volume(codec, nid, HDA_OUTPUT) &&
2978 !is_ctl_associated(codec, nid, HDA_OUTPUT, 0, NID_PATH_VOL_CTL))
Takashi Iwai2ded3e52013-11-28 11:05:28 +01002979 *mix_val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
Takashi Iwai43a8e502014-01-07 18:11:44 +01002980 if (!*mute_val && nid_has_mute(codec, nid, HDA_OUTPUT) &&
2981 !is_ctl_associated(codec, nid, HDA_OUTPUT, 0, NID_PATH_MUTE_CTL))
Takashi Iwai2ded3e52013-11-28 11:05:28 +01002982 *mute_val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
2983
2984 return *mix_val || *mute_val;
2985}
2986
Takashi Iwai352f7f92012-12-19 12:52:06 +01002987/* create input playback/capture controls for the given pin */
Takashi Iwai196c17662013-01-04 15:01:40 +01002988static int new_analog_input(struct hda_codec *codec, int input_idx,
2989 hda_nid_t pin, const char *ctlname, int ctlidx,
Takashi Iwai352f7f92012-12-19 12:52:06 +01002990 hda_nid_t mix_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002992 struct hda_gen_spec *spec = codec->spec;
2993 struct nid_path *path;
Takashi Iwai2ded3e52013-11-28 11:05:28 +01002994 unsigned int mix_val, mute_val;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002995 int err, idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996
Takashi Iwai2ded3e52013-11-28 11:05:28 +01002997 if (!look_for_mix_leaf_ctls(codec, mix_nid, pin, &mix_val, &mute_val))
2998 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002999
Takashi Iwai3ca529d2013-01-07 17:25:08 +01003000 path = snd_hda_add_new_path(codec, pin, mix_nid, 0);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003001 if (!path)
3002 return -EINVAL;
Takashi Iwai4e76a882014-02-25 12:21:03 +01003003 print_nid_path(codec, "loopback", path);
Takashi Iwai196c17662013-01-04 15:01:40 +01003004 spec->loopback_paths[input_idx] = snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003005
3006 idx = path->idx[path->depth - 1];
Takashi Iwai2ded3e52013-11-28 11:05:28 +01003007 if (mix_val) {
3008 err = __add_pb_vol_ctrl(spec, HDA_CTL_WIDGET_VOL, ctlname, ctlidx, mix_val);
Takashi Iwaid13bd412008-07-30 15:01:45 +02003009 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010 return err;
Takashi Iwai2ded3e52013-11-28 11:05:28 +01003011 path->ctls[NID_PATH_VOL_CTL] = mix_val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012 }
3013
Takashi Iwai2ded3e52013-11-28 11:05:28 +01003014 if (mute_val) {
3015 err = __add_pb_sw_ctrl(spec, HDA_CTL_WIDGET_MUTE, ctlname, ctlidx, mute_val);
Takashi Iwaid13bd412008-07-30 15:01:45 +02003016 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017 return err;
Takashi Iwai2ded3e52013-11-28 11:05:28 +01003018 path->ctls[NID_PATH_MUTE_CTL] = mute_val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019 }
3020
Takashi Iwai352f7f92012-12-19 12:52:06 +01003021 path->active = true;
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01003022 path->stream_enabled = true; /* no DAC/ADC involved */
Takashi Iwai0186f4f2013-02-07 10:45:11 +01003023 err = add_loopback_list(spec, mix_nid, idx);
3024 if (err < 0)
3025 return err;
Takashi Iwaie4a395e2013-01-23 17:00:31 +01003026
3027 if (spec->mixer_nid != spec->mixer_merge_nid &&
3028 !spec->loopback_merge_path) {
3029 path = snd_hda_add_new_path(codec, spec->mixer_nid,
3030 spec->mixer_merge_nid, 0);
3031 if (path) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003032 print_nid_path(codec, "loopback-merge", path);
Takashi Iwaie4a395e2013-01-23 17:00:31 +01003033 path->active = true;
Takashi Iwai6b275b12015-03-20 18:11:05 +01003034 path->pin_fixed = true; /* static route */
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01003035 path->stream_enabled = true; /* no DAC/ADC involved */
Takashi Iwaie4a395e2013-01-23 17:00:31 +01003036 spec->loopback_merge_path =
3037 snd_hda_get_path_idx(codec, path);
3038 }
3039 }
3040
Takashi Iwai352f7f92012-12-19 12:52:06 +01003041 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042}
3043
Takashi Iwai352f7f92012-12-19 12:52:06 +01003044static int is_input_pin(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003046 unsigned int pincap = snd_hda_query_pin_caps(codec, nid);
3047 return (pincap & AC_PINCAP_IN) != 0;
3048}
3049
3050/* Parse the codec tree and retrieve ADCs */
3051static int fill_adc_nids(struct hda_codec *codec)
3052{
3053 struct hda_gen_spec *spec = codec->spec;
3054 hda_nid_t nid;
3055 hda_nid_t *adc_nids = spec->adc_nids;
3056 int max_nums = ARRAY_SIZE(spec->adc_nids);
Takashi Iwai7639a062015-03-03 10:07:24 +01003057 int nums = 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003058
Takashi Iwai7639a062015-03-03 10:07:24 +01003059 for_each_hda_codec_node(nid, codec) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003060 unsigned int caps = get_wcaps(codec, nid);
3061 int type = get_wcaps_type(caps);
3062
3063 if (type != AC_WID_AUD_IN || (caps & AC_WCAP_DIGITAL))
3064 continue;
3065 adc_nids[nums] = nid;
3066 if (++nums >= max_nums)
3067 break;
3068 }
3069 spec->num_adc_nids = nums;
Takashi Iwai0ffd5342013-01-17 15:53:29 +01003070
3071 /* copy the detected ADCs to all_adcs[] */
3072 spec->num_all_adcs = nums;
3073 memcpy(spec->all_adcs, spec->adc_nids, nums * sizeof(hda_nid_t));
3074
Takashi Iwai352f7f92012-12-19 12:52:06 +01003075 return nums;
3076}
3077
3078/* filter out invalid adc_nids that don't give all active input pins;
3079 * if needed, check whether dynamic ADC-switching is available
3080 */
3081static int check_dyn_adc_switch(struct hda_codec *codec)
3082{
3083 struct hda_gen_spec *spec = codec->spec;
3084 struct hda_input_mux *imux = &spec->input_mux;
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01003085 unsigned int ok_bits;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003086 int i, n, nums;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003087
Takashi Iwai352f7f92012-12-19 12:52:06 +01003088 nums = 0;
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01003089 ok_bits = 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003090 for (n = 0; n < spec->num_adc_nids; n++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003091 for (i = 0; i < imux->num_items; i++) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01003092 if (!spec->input_paths[i][n])
Takashi Iwai352f7f92012-12-19 12:52:06 +01003093 break;
3094 }
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01003095 if (i >= imux->num_items) {
3096 ok_bits |= (1 << n);
3097 nums++;
3098 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003099 }
3100
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01003101 if (!ok_bits) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003102 /* check whether ADC-switch is possible */
3103 for (i = 0; i < imux->num_items; i++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003104 for (n = 0; n < spec->num_adc_nids; n++) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01003105 if (spec->input_paths[i][n]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003106 spec->dyn_adc_idx[i] = n;
3107 break;
3108 }
3109 }
3110 }
3111
Takashi Iwai4e76a882014-02-25 12:21:03 +01003112 codec_dbg(codec, "enabling ADC switching\n");
Takashi Iwai352f7f92012-12-19 12:52:06 +01003113 spec->dyn_adc_switch = 1;
3114 } else if (nums != spec->num_adc_nids) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01003115 /* shrink the invalid adcs and input paths */
3116 nums = 0;
3117 for (n = 0; n < spec->num_adc_nids; n++) {
3118 if (!(ok_bits & (1 << n)))
3119 continue;
3120 if (n != nums) {
3121 spec->adc_nids[nums] = spec->adc_nids[n];
Takashi Iwai980428c2013-01-09 09:28:20 +01003122 for (i = 0; i < imux->num_items; i++) {
3123 invalidate_nid_path(codec,
3124 spec->input_paths[i][nums]);
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01003125 spec->input_paths[i][nums] =
3126 spec->input_paths[i][n];
Takashi Iwai980428c2013-01-09 09:28:20 +01003127 }
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01003128 }
3129 nums++;
3130 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003131 spec->num_adc_nids = nums;
3132 }
3133
Takashi Iwai967303d2013-02-19 17:12:42 +01003134 if (imux->num_items == 1 ||
3135 (imux->num_items == 2 && spec->hp_mic)) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003136 codec_dbg(codec, "reducing to a single ADC\n");
Takashi Iwai352f7f92012-12-19 12:52:06 +01003137 spec->num_adc_nids = 1; /* reduce to a single ADC */
3138 }
3139
3140 /* single index for individual volumes ctls */
3141 if (!spec->dyn_adc_switch && spec->multi_cap_vol)
3142 spec->num_adc_nids = 1;
3143
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144 return 0;
3145}
3146
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003147/* parse capture source paths from the given pin and create imux items */
3148static int parse_capture_source(struct hda_codec *codec, hda_nid_t pin,
Takashi Iwai9dba2052013-01-18 10:01:15 +01003149 int cfg_idx, int num_adcs,
3150 const char *label, int anchor)
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003151{
3152 struct hda_gen_spec *spec = codec->spec;
3153 struct hda_input_mux *imux = &spec->input_mux;
3154 int imux_idx = imux->num_items;
3155 bool imux_added = false;
3156 int c;
3157
3158 for (c = 0; c < num_adcs; c++) {
3159 struct nid_path *path;
3160 hda_nid_t adc = spec->adc_nids[c];
3161
3162 if (!is_reachable_path(codec, pin, adc))
3163 continue;
3164 path = snd_hda_add_new_path(codec, pin, adc, anchor);
3165 if (!path)
3166 continue;
Takashi Iwai4e76a882014-02-25 12:21:03 +01003167 print_nid_path(codec, "input", path);
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003168 spec->input_paths[imux_idx][c] =
3169 snd_hda_get_path_idx(codec, path);
3170
3171 if (!imux_added) {
Takashi Iwai967303d2013-02-19 17:12:42 +01003172 if (spec->hp_mic_pin == pin)
3173 spec->hp_mic_mux_idx = imux->num_items;
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003174 spec->imux_pins[imux->num_items] = pin;
Takashi Iwai6194b992014-06-06 18:12:16 +02003175 snd_hda_add_imux_item(codec, imux, label, cfg_idx, NULL);
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003176 imux_added = true;
Takashi Iwaif1e762d2013-12-09 16:02:24 +01003177 if (spec->dyn_adc_switch)
3178 spec->dyn_adc_idx[imux_idx] = c;
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003179 }
3180 }
3181
3182 return 0;
3183}
3184
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01003186 * create playback/capture controls for input pins
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187 */
Takashi Iwai9dba2052013-01-18 10:01:15 +01003188
Takashi Iwaic9700422013-01-18 10:17:30 +01003189/* fill the label for each input at first */
3190static int fill_input_pin_labels(struct hda_codec *codec)
3191{
3192 struct hda_gen_spec *spec = codec->spec;
3193 const struct auto_pin_cfg *cfg = &spec->autocfg;
3194 int i;
3195
3196 for (i = 0; i < cfg->num_inputs; i++) {
3197 hda_nid_t pin = cfg->inputs[i].pin;
3198 const char *label;
3199 int j, idx;
3200
3201 if (!is_input_pin(codec, pin))
3202 continue;
3203
3204 label = hda_get_autocfg_input_label(codec, cfg, i);
3205 idx = 0;
David Henningsson8e8db7f2013-01-18 15:43:02 +01003206 for (j = i - 1; j >= 0; j--) {
Takashi Iwaic9700422013-01-18 10:17:30 +01003207 if (spec->input_labels[j] &&
3208 !strcmp(spec->input_labels[j], label)) {
3209 idx = spec->input_label_idxs[j] + 1;
3210 break;
3211 }
3212 }
3213
3214 spec->input_labels[i] = label;
3215 spec->input_label_idxs[i] = idx;
3216 }
3217
3218 return 0;
3219}
3220
Takashi Iwai9dba2052013-01-18 10:01:15 +01003221#define CFG_IDX_MIX 99 /* a dummy cfg->input idx for stereo mix */
3222
Takashi Iwai352f7f92012-12-19 12:52:06 +01003223static int create_input_ctls(struct hda_codec *codec)
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02003224{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003225 struct hda_gen_spec *spec = codec->spec;
3226 const struct auto_pin_cfg *cfg = &spec->autocfg;
3227 hda_nid_t mixer = spec->mixer_nid;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003228 int num_adcs;
Takashi Iwaic9700422013-01-18 10:17:30 +01003229 int i, err;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003230 unsigned int val;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02003231
Takashi Iwai352f7f92012-12-19 12:52:06 +01003232 num_adcs = fill_adc_nids(codec);
3233 if (num_adcs < 0)
3234 return 0;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02003235
Takashi Iwaic9700422013-01-18 10:17:30 +01003236 err = fill_input_pin_labels(codec);
3237 if (err < 0)
3238 return err;
3239
Takashi Iwai352f7f92012-12-19 12:52:06 +01003240 for (i = 0; i < cfg->num_inputs; i++) {
3241 hda_nid_t pin;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242
Takashi Iwai352f7f92012-12-19 12:52:06 +01003243 pin = cfg->inputs[i].pin;
3244 if (!is_input_pin(codec, pin))
3245 continue;
3246
Takashi Iwai2c12c302013-01-10 09:33:29 +01003247 val = PIN_IN;
3248 if (cfg->inputs[i].type == AUTO_PIN_MIC)
3249 val |= snd_hda_get_default_vref(codec, pin);
Takashi Iwai3e1b0c42015-04-27 10:43:22 +02003250 if (pin != spec->hp_mic_pin &&
3251 !snd_hda_codec_get_pin_target(codec, pin))
Takashi Iwai93c9d8a2013-03-11 09:48:43 +01003252 set_pin_target(codec, pin, val, false);
Takashi Iwai2c12c302013-01-10 09:33:29 +01003253
Takashi Iwai352f7f92012-12-19 12:52:06 +01003254 if (mixer) {
3255 if (is_reachable_path(codec, pin, mixer)) {
Takashi Iwai196c17662013-01-04 15:01:40 +01003256 err = new_analog_input(codec, i, pin,
Takashi Iwaic9700422013-01-18 10:17:30 +01003257 spec->input_labels[i],
3258 spec->input_label_idxs[i],
3259 mixer);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003260 if (err < 0)
3261 return err;
3262 }
3263 }
3264
Takashi Iwaic9700422013-01-18 10:17:30 +01003265 err = parse_capture_source(codec, pin, i, num_adcs,
3266 spec->input_labels[i], -mixer);
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003267 if (err < 0)
3268 return err;
Takashi Iwai294765582013-01-17 09:52:11 +01003269
Takashi Iwaif811c3c2013-03-07 18:32:59 +01003270 if (spec->add_jack_modes) {
Takashi Iwai294765582013-01-17 09:52:11 +01003271 err = create_in_jack_mode(codec, pin);
3272 if (err < 0)
3273 return err;
3274 }
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003275 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003276
Takashi Iwaif1e762d2013-12-09 16:02:24 +01003277 /* add stereo mix when explicitly enabled via hint */
Takashi Iwai74f14b32014-12-15 13:43:59 +01003278 if (mixer && spec->add_stereo_mix_input == HDA_HINT_STEREO_MIX_ENABLE) {
Takashi Iwai9dba2052013-01-18 10:01:15 +01003279 err = parse_capture_source(codec, mixer, CFG_IDX_MIX, num_adcs,
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01003280 "Stereo Mix", 0);
3281 if (err < 0)
3282 return err;
Takashi Iwai82d04e12014-12-15 13:40:42 +01003283 else
3284 spec->suppress_auto_mic = 1;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003285 }
3286
3287 return 0;
3288}
3289
3290
3291/*
3292 * input source mux
3293 */
3294
Takashi Iwaic697b712013-01-07 17:09:26 +01003295/* get the input path specified by the given adc and imux indices */
3296static struct nid_path *get_input_path(struct hda_codec *codec, int adc_idx, int imux_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003297{
3298 struct hda_gen_spec *spec = codec->spec;
David Henningssonb56fa1e2013-01-16 11:45:35 +01003299 if (imux_idx < 0 || imux_idx >= HDA_MAX_NUM_INPUTS) {
3300 snd_BUG();
3301 return NULL;
3302 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003303 if (spec->dyn_adc_switch)
3304 adc_idx = spec->dyn_adc_idx[imux_idx];
David Henningssond3d982f2013-01-18 15:43:01 +01003305 if (adc_idx < 0 || adc_idx >= AUTO_CFG_MAX_INS) {
David Henningssonb56fa1e2013-01-16 11:45:35 +01003306 snd_BUG();
3307 return NULL;
3308 }
Takashi Iwaic697b712013-01-07 17:09:26 +01003309 return snd_hda_get_path_from_idx(codec, spec->input_paths[imux_idx][adc_idx]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003310}
3311
3312static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
3313 unsigned int idx);
3314
3315static int mux_enum_info(struct snd_kcontrol *kcontrol,
3316 struct snd_ctl_elem_info *uinfo)
3317{
3318 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3319 struct hda_gen_spec *spec = codec->spec;
3320 return snd_hda_input_mux_info(&spec->input_mux, uinfo);
3321}
3322
3323static int mux_enum_get(struct snd_kcontrol *kcontrol,
3324 struct snd_ctl_elem_value *ucontrol)
3325{
3326 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3327 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai2a8d5392013-01-18 16:23:25 +01003328 /* the ctls are created at once with multiple counts */
3329 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003330
3331 ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
3332 return 0;
3333}
3334
3335static int mux_enum_put(struct snd_kcontrol *kcontrol,
3336 struct snd_ctl_elem_value *ucontrol)
3337{
3338 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
Takashi Iwai2a8d5392013-01-18 16:23:25 +01003339 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003340 return mux_select(codec, adc_idx,
3341 ucontrol->value.enumerated.item[0]);
3342}
3343
Takashi Iwai352f7f92012-12-19 12:52:06 +01003344static const struct snd_kcontrol_new cap_src_temp = {
3345 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3346 .name = "Input Source",
3347 .info = mux_enum_info,
3348 .get = mux_enum_get,
3349 .put = mux_enum_put,
3350};
3351
Takashi Iwai47d46ab2012-12-20 11:48:54 +01003352/*
3353 * capture volume and capture switch ctls
3354 */
3355
Takashi Iwai352f7f92012-12-19 12:52:06 +01003356typedef int (*put_call_t)(struct snd_kcontrol *kcontrol,
3357 struct snd_ctl_elem_value *ucontrol);
3358
Takashi Iwai47d46ab2012-12-20 11:48:54 +01003359/* call the given amp update function for all amps in the imux list at once */
Takashi Iwai352f7f92012-12-19 12:52:06 +01003360static int cap_put_caller(struct snd_kcontrol *kcontrol,
3361 struct snd_ctl_elem_value *ucontrol,
3362 put_call_t func, int type)
3363{
3364 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3365 struct hda_gen_spec *spec = codec->spec;
3366 const struct hda_input_mux *imux;
3367 struct nid_path *path;
3368 int i, adc_idx, err = 0;
3369
3370 imux = &spec->input_mux;
David Henningssona053d1e2013-01-16 11:45:36 +01003371 adc_idx = kcontrol->id.index;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003372 mutex_lock(&codec->control_mutex);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003373 for (i = 0; i < imux->num_items; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01003374 path = get_input_path(codec, adc_idx, i);
3375 if (!path || !path->ctls[type])
Takashi Iwai352f7f92012-12-19 12:52:06 +01003376 continue;
3377 kcontrol->private_value = path->ctls[type];
3378 err = func(kcontrol, ucontrol);
3379 if (err < 0)
Takashi Iwaia551d912015-02-26 12:34:49 +01003380 break;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003381 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003382 mutex_unlock(&codec->control_mutex);
3383 if (err >= 0 && spec->cap_sync_hook)
Takashi Iwai7fe30712014-01-30 17:59:02 +01003384 spec->cap_sync_hook(codec, kcontrol, ucontrol);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003385 return err;
3386}
3387
3388/* capture volume ctl callbacks */
3389#define cap_vol_info snd_hda_mixer_amp_volume_info
3390#define cap_vol_get snd_hda_mixer_amp_volume_get
3391#define cap_vol_tlv snd_hda_mixer_amp_tlv
3392
3393static int cap_vol_put(struct snd_kcontrol *kcontrol,
3394 struct snd_ctl_elem_value *ucontrol)
3395{
3396 return cap_put_caller(kcontrol, ucontrol,
3397 snd_hda_mixer_amp_volume_put,
3398 NID_PATH_VOL_CTL);
3399}
3400
3401static const struct snd_kcontrol_new cap_vol_temp = {
3402 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3403 .name = "Capture Volume",
3404 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
3405 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
3406 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK),
3407 .info = cap_vol_info,
3408 .get = cap_vol_get,
3409 .put = cap_vol_put,
3410 .tlv = { .c = cap_vol_tlv },
3411};
3412
3413/* capture switch ctl callbacks */
3414#define cap_sw_info snd_ctl_boolean_stereo_info
3415#define cap_sw_get snd_hda_mixer_amp_switch_get
3416
3417static int cap_sw_put(struct snd_kcontrol *kcontrol,
3418 struct snd_ctl_elem_value *ucontrol)
3419{
Takashi Iwaia90229e2013-01-18 14:10:00 +01003420 return cap_put_caller(kcontrol, ucontrol,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003421 snd_hda_mixer_amp_switch_put,
3422 NID_PATH_MUTE_CTL);
3423}
3424
3425static const struct snd_kcontrol_new cap_sw_temp = {
3426 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3427 .name = "Capture Switch",
3428 .info = cap_sw_info,
3429 .get = cap_sw_get,
3430 .put = cap_sw_put,
3431};
3432
3433static int parse_capvol_in_path(struct hda_codec *codec, struct nid_path *path)
3434{
3435 hda_nid_t nid;
3436 int i, depth;
3437
3438 path->ctls[NID_PATH_VOL_CTL] = path->ctls[NID_PATH_MUTE_CTL] = 0;
3439 for (depth = 0; depth < 3; depth++) {
3440 if (depth >= path->depth)
3441 return -EINVAL;
3442 i = path->depth - depth - 1;
3443 nid = path->path[i];
3444 if (!path->ctls[NID_PATH_VOL_CTL]) {
3445 if (nid_has_volume(codec, nid, HDA_OUTPUT))
3446 path->ctls[NID_PATH_VOL_CTL] =
3447 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3448 else if (nid_has_volume(codec, nid, HDA_INPUT)) {
3449 int idx = path->idx[i];
3450 if (!depth && codec->single_adc_amp)
3451 idx = 0;
3452 path->ctls[NID_PATH_VOL_CTL] =
3453 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
3454 }
3455 }
3456 if (!path->ctls[NID_PATH_MUTE_CTL]) {
3457 if (nid_has_mute(codec, nid, HDA_OUTPUT))
3458 path->ctls[NID_PATH_MUTE_CTL] =
3459 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3460 else if (nid_has_mute(codec, nid, HDA_INPUT)) {
3461 int idx = path->idx[i];
3462 if (!depth && codec->single_adc_amp)
3463 idx = 0;
3464 path->ctls[NID_PATH_MUTE_CTL] =
3465 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
3466 }
3467 }
Takashi Iwai97ec5582006-03-21 11:29:07 +01003468 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003469 return 0;
3470}
3471
Takashi Iwai352f7f92012-12-19 12:52:06 +01003472static bool is_inv_dmic_pin(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003473{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003474 struct hda_gen_spec *spec = codec->spec;
3475 struct auto_pin_cfg *cfg = &spec->autocfg;
3476 unsigned int val;
3477 int i;
3478
3479 if (!spec->inv_dmic_split)
3480 return false;
3481 for (i = 0; i < cfg->num_inputs; i++) {
3482 if (cfg->inputs[i].pin != nid)
3483 continue;
3484 if (cfg->inputs[i].type != AUTO_PIN_MIC)
3485 return false;
3486 val = snd_hda_codec_get_pincfg(codec, nid);
3487 return snd_hda_get_input_pin_attr(val) == INPUT_PIN_ATTR_INT;
3488 }
3489 return false;
3490}
3491
Takashi Iwaia90229e2013-01-18 14:10:00 +01003492/* capture switch put callback for a single control with hook call */
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003493static int cap_single_sw_put(struct snd_kcontrol *kcontrol,
3494 struct snd_ctl_elem_value *ucontrol)
3495{
3496 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3497 struct hda_gen_spec *spec = codec->spec;
3498 int ret;
3499
3500 ret = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
3501 if (ret < 0)
3502 return ret;
3503
Takashi Iwaia90229e2013-01-18 14:10:00 +01003504 if (spec->cap_sync_hook)
Takashi Iwai7fe30712014-01-30 17:59:02 +01003505 spec->cap_sync_hook(codec, kcontrol, ucontrol);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003506
3507 return ret;
3508}
3509
Takashi Iwai352f7f92012-12-19 12:52:06 +01003510static int add_single_cap_ctl(struct hda_codec *codec, const char *label,
3511 int idx, bool is_switch, unsigned int ctl,
3512 bool inv_dmic)
3513{
3514 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai975cc022013-06-28 11:56:49 +02003515 char tmpname[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
Takashi Iwai352f7f92012-12-19 12:52:06 +01003516 int type = is_switch ? HDA_CTL_WIDGET_MUTE : HDA_CTL_WIDGET_VOL;
3517 const char *sfx = is_switch ? "Switch" : "Volume";
3518 unsigned int chs = inv_dmic ? 1 : 3;
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003519 struct snd_kcontrol_new *knew;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003520
3521 if (!ctl)
3522 return 0;
3523
3524 if (label)
3525 snprintf(tmpname, sizeof(tmpname),
3526 "%s Capture %s", label, sfx);
3527 else
3528 snprintf(tmpname, sizeof(tmpname),
3529 "Capture %s", sfx);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003530 knew = add_control(spec, type, tmpname, idx,
3531 amp_val_replace_channels(ctl, chs));
3532 if (!knew)
3533 return -ENOMEM;
Takashi Iwaia90229e2013-01-18 14:10:00 +01003534 if (is_switch)
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003535 knew->put = cap_single_sw_put;
3536 if (!inv_dmic)
3537 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003538
3539 /* Make independent right kcontrol */
3540 if (label)
3541 snprintf(tmpname, sizeof(tmpname),
3542 "Inverted %s Capture %s", label, sfx);
3543 else
3544 snprintf(tmpname, sizeof(tmpname),
3545 "Inverted Capture %s", sfx);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003546 knew = add_control(spec, type, tmpname, idx,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003547 amp_val_replace_channels(ctl, 2));
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003548 if (!knew)
3549 return -ENOMEM;
Takashi Iwaia90229e2013-01-18 14:10:00 +01003550 if (is_switch)
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003551 knew->put = cap_single_sw_put;
3552 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003553}
3554
3555/* create single (and simple) capture volume and switch controls */
3556static int create_single_cap_vol_ctl(struct hda_codec *codec, int idx,
3557 unsigned int vol_ctl, unsigned int sw_ctl,
3558 bool inv_dmic)
3559{
3560 int err;
3561 err = add_single_cap_ctl(codec, NULL, idx, false, vol_ctl, inv_dmic);
3562 if (err < 0)
3563 return err;
3564 err = add_single_cap_ctl(codec, NULL, idx, true, sw_ctl, inv_dmic);
3565 if (err < 0)
3566 return err;
3567 return 0;
3568}
3569
3570/* create bound capture volume and switch controls */
3571static int create_bind_cap_vol_ctl(struct hda_codec *codec, int idx,
3572 unsigned int vol_ctl, unsigned int sw_ctl)
3573{
3574 struct hda_gen_spec *spec = codec->spec;
3575 struct snd_kcontrol_new *knew;
3576
3577 if (vol_ctl) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01003578 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_vol_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003579 if (!knew)
3580 return -ENOMEM;
3581 knew->index = idx;
3582 knew->private_value = vol_ctl;
3583 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
3584 }
3585 if (sw_ctl) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01003586 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_sw_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003587 if (!knew)
3588 return -ENOMEM;
3589 knew->index = idx;
3590 knew->private_value = sw_ctl;
3591 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
3592 }
3593 return 0;
3594}
3595
3596/* return the vol ctl when used first in the imux list */
3597static unsigned int get_first_cap_ctl(struct hda_codec *codec, int idx, int type)
3598{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003599 struct nid_path *path;
3600 unsigned int ctl;
3601 int i;
3602
Takashi Iwaic697b712013-01-07 17:09:26 +01003603 path = get_input_path(codec, 0, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003604 if (!path)
3605 return 0;
3606 ctl = path->ctls[type];
3607 if (!ctl)
3608 return 0;
3609 for (i = 0; i < idx - 1; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01003610 path = get_input_path(codec, 0, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003611 if (path && path->ctls[type] == ctl)
3612 return 0;
3613 }
3614 return ctl;
3615}
3616
3617/* create individual capture volume and switch controls per input */
3618static int create_multi_cap_vol_ctl(struct hda_codec *codec)
3619{
3620 struct hda_gen_spec *spec = codec->spec;
3621 struct hda_input_mux *imux = &spec->input_mux;
Takashi Iwaic9700422013-01-18 10:17:30 +01003622 int i, err, type;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003623
3624 for (i = 0; i < imux->num_items; i++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003625 bool inv_dmic;
Takashi Iwaic9700422013-01-18 10:17:30 +01003626 int idx;
Takashi Iwai9dba2052013-01-18 10:01:15 +01003627
Takashi Iwaic9700422013-01-18 10:17:30 +01003628 idx = imux->items[i].index;
3629 if (idx >= spec->autocfg.num_inputs)
Takashi Iwai9dba2052013-01-18 10:01:15 +01003630 continue;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003631 inv_dmic = is_inv_dmic_pin(codec, spec->imux_pins[i]);
3632
3633 for (type = 0; type < 2; type++) {
Takashi Iwaic9700422013-01-18 10:17:30 +01003634 err = add_single_cap_ctl(codec,
3635 spec->input_labels[idx],
3636 spec->input_label_idxs[idx],
3637 type,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003638 get_first_cap_ctl(codec, i, type),
3639 inv_dmic);
3640 if (err < 0)
3641 return err;
3642 }
3643 }
3644 return 0;
3645}
3646
3647static int create_capture_mixers(struct hda_codec *codec)
3648{
3649 struct hda_gen_spec *spec = codec->spec;
3650 struct hda_input_mux *imux = &spec->input_mux;
3651 int i, n, nums, err;
3652
3653 if (spec->dyn_adc_switch)
3654 nums = 1;
3655 else
3656 nums = spec->num_adc_nids;
3657
3658 if (!spec->auto_mic && imux->num_items > 1) {
3659 struct snd_kcontrol_new *knew;
Takashi Iwai624d9142012-12-19 17:41:52 +01003660 const char *name;
3661 name = nums > 1 ? "Input Source" : "Capture Source";
3662 knew = snd_hda_gen_add_kctl(spec, name, &cap_src_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003663 if (!knew)
3664 return -ENOMEM;
3665 knew->count = nums;
3666 }
3667
3668 for (n = 0; n < nums; n++) {
3669 bool multi = false;
David Henningsson99a55922013-01-16 15:58:44 +01003670 bool multi_cap_vol = spec->multi_cap_vol;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003671 bool inv_dmic = false;
3672 int vol, sw;
3673
3674 vol = sw = 0;
3675 for (i = 0; i < imux->num_items; i++) {
3676 struct nid_path *path;
Takashi Iwaic697b712013-01-07 17:09:26 +01003677 path = get_input_path(codec, n, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003678 if (!path)
3679 continue;
3680 parse_capvol_in_path(codec, path);
3681 if (!vol)
3682 vol = path->ctls[NID_PATH_VOL_CTL];
David Henningsson99a55922013-01-16 15:58:44 +01003683 else if (vol != path->ctls[NID_PATH_VOL_CTL]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003684 multi = true;
David Henningsson99a55922013-01-16 15:58:44 +01003685 if (!same_amp_caps(codec, vol,
3686 path->ctls[NID_PATH_VOL_CTL], HDA_INPUT))
3687 multi_cap_vol = true;
3688 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003689 if (!sw)
3690 sw = path->ctls[NID_PATH_MUTE_CTL];
David Henningsson99a55922013-01-16 15:58:44 +01003691 else if (sw != path->ctls[NID_PATH_MUTE_CTL]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003692 multi = true;
David Henningsson99a55922013-01-16 15:58:44 +01003693 if (!same_amp_caps(codec, sw,
3694 path->ctls[NID_PATH_MUTE_CTL], HDA_INPUT))
3695 multi_cap_vol = true;
3696 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003697 if (is_inv_dmic_pin(codec, spec->imux_pins[i]))
3698 inv_dmic = true;
3699 }
3700
3701 if (!multi)
3702 err = create_single_cap_vol_ctl(codec, n, vol, sw,
3703 inv_dmic);
David Henningssonccb04152013-10-14 10:16:22 +02003704 else if (!multi_cap_vol && !inv_dmic)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003705 err = create_bind_cap_vol_ctl(codec, n, vol, sw);
3706 else
3707 err = create_multi_cap_vol_ctl(codec);
3708 if (err < 0)
3709 return err;
3710 }
3711
3712 return 0;
3713}
3714
3715/*
3716 * add mic boosts if needed
3717 */
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003718
3719/* check whether the given amp is feasible as a boost volume */
3720static bool check_boost_vol(struct hda_codec *codec, hda_nid_t nid,
3721 int dir, int idx)
3722{
3723 unsigned int step;
3724
3725 if (!nid_has_volume(codec, nid, dir) ||
3726 is_ctl_associated(codec, nid, dir, idx, NID_PATH_VOL_CTL) ||
3727 is_ctl_associated(codec, nid, dir, idx, NID_PATH_BOOST_CTL))
3728 return false;
3729
3730 step = (query_amp_caps(codec, nid, dir) & AC_AMPCAP_STEP_SIZE)
3731 >> AC_AMPCAP_STEP_SIZE_SHIFT;
3732 if (step < 0x20)
3733 return false;
3734 return true;
3735}
3736
3737/* look for a boost amp in a widget close to the pin */
3738static unsigned int look_for_boost_amp(struct hda_codec *codec,
3739 struct nid_path *path)
3740{
3741 unsigned int val = 0;
3742 hda_nid_t nid;
3743 int depth;
3744
3745 for (depth = 0; depth < 3; depth++) {
3746 if (depth >= path->depth - 1)
3747 break;
3748 nid = path->path[depth];
3749 if (depth && check_boost_vol(codec, nid, HDA_OUTPUT, 0)) {
3750 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3751 break;
3752 } else if (check_boost_vol(codec, nid, HDA_INPUT,
3753 path->idx[depth])) {
3754 val = HDA_COMPOSE_AMP_VAL(nid, 3, path->idx[depth],
3755 HDA_INPUT);
3756 break;
3757 }
3758 }
3759
3760 return val;
3761}
3762
Takashi Iwai352f7f92012-12-19 12:52:06 +01003763static int parse_mic_boost(struct hda_codec *codec)
3764{
3765 struct hda_gen_spec *spec = codec->spec;
3766 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003767 struct hda_input_mux *imux = &spec->input_mux;
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003768 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003769
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003770 if (!spec->num_adc_nids)
3771 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003772
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003773 for (i = 0; i < imux->num_items; i++) {
3774 struct nid_path *path;
3775 unsigned int val;
3776 int idx;
Takashi Iwai975cc022013-06-28 11:56:49 +02003777 char boost_label[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
David Henningsson02aba552013-01-16 15:58:43 +01003778
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003779 idx = imux->items[i].index;
3780 if (idx >= imux->num_items)
3781 continue;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003782
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003783 /* check only line-in and mic pins */
Takashi Iwai1799cdd52013-01-18 14:37:16 +01003784 if (cfg->inputs[idx].type > AUTO_PIN_LINE_IN)
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003785 continue;
3786
3787 path = get_input_path(codec, 0, i);
3788 if (!path)
3789 continue;
3790
3791 val = look_for_boost_amp(codec, path);
3792 if (!val)
3793 continue;
3794
3795 /* create a boost control */
3796 snprintf(boost_label, sizeof(boost_label),
3797 "%s Boost Volume", spec->input_labels[idx]);
Takashi Iwaia35bd1e2013-01-18 14:01:14 +01003798 if (!add_control(spec, HDA_CTL_WIDGET_VOL, boost_label,
3799 spec->input_label_idxs[idx], val))
3800 return -ENOMEM;
Takashi Iwai6f7c83a2013-01-18 11:07:15 +01003801
3802 path->ctls[NID_PATH_BOOST_CTL] = val;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003803 }
3804 return 0;
3805}
3806
3807/*
3808 * parse digital I/Os and set up NIDs in BIOS auto-parse mode
3809 */
3810static void parse_digital(struct hda_codec *codec)
3811{
3812 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003813 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003814 int i, nums;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003815 hda_nid_t dig_nid, pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003816
3817 /* support multiple SPDIFs; the secondary is set up as a slave */
3818 nums = 0;
3819 for (i = 0; i < spec->autocfg.dig_outs; i++) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01003820 pin = spec->autocfg.dig_out_pins[i];
Takashi Iwai352f7f92012-12-19 12:52:06 +01003821 dig_nid = look_for_dac(codec, pin, true);
3822 if (!dig_nid)
3823 continue;
Takashi Iwai3ca529d2013-01-07 17:25:08 +01003824 path = snd_hda_add_new_path(codec, dig_nid, pin, 0);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01003825 if (!path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003826 continue;
Takashi Iwai4e76a882014-02-25 12:21:03 +01003827 print_nid_path(codec, "digout", path);
Takashi Iwaie1284af2013-01-03 16:33:02 +01003828 path->active = true;
Takashi Iwai6b275b12015-03-20 18:11:05 +01003829 path->pin_fixed = true; /* no jack detection */
Takashi Iwai196c17662013-01-04 15:01:40 +01003830 spec->digout_paths[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai2c12c302013-01-10 09:33:29 +01003831 set_pin_target(codec, pin, PIN_OUT, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003832 if (!nums) {
3833 spec->multiout.dig_out_nid = dig_nid;
3834 spec->dig_out_type = spec->autocfg.dig_out_type[0];
3835 } else {
3836 spec->multiout.slave_dig_outs = spec->slave_dig_outs;
3837 if (nums >= ARRAY_SIZE(spec->slave_dig_outs) - 1)
Dan Carpenterd5764222014-05-14 17:18:31 +03003838 break;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003839 spec->slave_dig_outs[nums - 1] = dig_nid;
3840 }
3841 nums++;
3842 }
3843
3844 if (spec->autocfg.dig_in_pin) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01003845 pin = spec->autocfg.dig_in_pin;
Takashi Iwai7639a062015-03-03 10:07:24 +01003846 for_each_hda_codec_node(dig_nid, codec) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003847 unsigned int wcaps = get_wcaps(codec, dig_nid);
3848 if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
3849 continue;
3850 if (!(wcaps & AC_WCAP_DIGITAL))
3851 continue;
Takashi Iwai2c12c302013-01-10 09:33:29 +01003852 path = snd_hda_add_new_path(codec, pin, dig_nid, 0);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003853 if (path) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01003854 print_nid_path(codec, "digin", path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003855 path->active = true;
Takashi Iwai6b275b12015-03-20 18:11:05 +01003856 path->pin_fixed = true; /* no jack */
Takashi Iwai352f7f92012-12-19 12:52:06 +01003857 spec->dig_in_nid = dig_nid;
Takashi Iwai2430d7b2013-01-04 15:09:42 +01003858 spec->digin_path = snd_hda_get_path_idx(codec, path);
Takashi Iwai2c12c302013-01-10 09:33:29 +01003859 set_pin_target(codec, pin, PIN_IN, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003860 break;
3861 }
3862 }
3863 }
3864}
3865
3866
3867/*
3868 * input MUX handling
3869 */
3870
3871static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur);
3872
3873/* select the given imux item; either unmute exclusively or select the route */
3874static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
3875 unsigned int idx)
3876{
3877 struct hda_gen_spec *spec = codec->spec;
3878 const struct hda_input_mux *imux;
Takashi Iwai55196ff2013-01-24 17:32:56 +01003879 struct nid_path *old_path, *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003880
3881 imux = &spec->input_mux;
3882 if (!imux->num_items)
3883 return 0;
3884
3885 if (idx >= imux->num_items)
3886 idx = imux->num_items - 1;
3887 if (spec->cur_mux[adc_idx] == idx)
3888 return 0;
3889
Takashi Iwai55196ff2013-01-24 17:32:56 +01003890 old_path = get_input_path(codec, adc_idx, spec->cur_mux[adc_idx]);
3891 if (!old_path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003892 return 0;
Takashi Iwai55196ff2013-01-24 17:32:56 +01003893 if (old_path->active)
3894 snd_hda_activate_path(codec, old_path, false, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003895
3896 spec->cur_mux[adc_idx] = idx;
3897
Takashi Iwai967303d2013-02-19 17:12:42 +01003898 if (spec->hp_mic)
3899 update_hp_mic(codec, adc_idx, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003900
3901 if (spec->dyn_adc_switch)
3902 dyn_adc_pcm_resetup(codec, idx);
3903
Takashi Iwaic697b712013-01-07 17:09:26 +01003904 path = get_input_path(codec, adc_idx, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003905 if (!path)
3906 return 0;
3907 if (path->active)
3908 return 0;
3909 snd_hda_activate_path(codec, path, true, false);
3910 if (spec->cap_sync_hook)
Takashi Iwai7fe30712014-01-30 17:59:02 +01003911 spec->cap_sync_hook(codec, NULL, NULL);
Takashi Iwai55196ff2013-01-24 17:32:56 +01003912 path_power_down_sync(codec, old_path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003913 return 1;
3914}
3915
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01003916/* power up/down widgets in the all paths that match with the given NID
3917 * as terminals (either start- or endpoint)
3918 *
3919 * returns the last changed NID, or zero if unchanged.
3920 */
3921static hda_nid_t set_path_power(struct hda_codec *codec, hda_nid_t nid,
3922 int pin_state, int stream_state)
3923{
3924 struct hda_gen_spec *spec = codec->spec;
3925 hda_nid_t last, changed = 0;
3926 struct nid_path *path;
3927 int n;
3928
3929 for (n = 0; n < spec->paths.used; n++) {
3930 path = snd_array_elem(&spec->paths, n);
3931 if (path->path[0] == nid ||
3932 path->path[path->depth - 1] == nid) {
3933 bool pin_old = path->pin_enabled;
3934 bool stream_old = path->stream_enabled;
3935
3936 if (pin_state >= 0)
3937 path->pin_enabled = pin_state;
3938 if (stream_state >= 0)
3939 path->stream_enabled = stream_state;
Takashi Iwai6b275b12015-03-20 18:11:05 +01003940 if ((!path->pin_fixed && path->pin_enabled != pin_old)
3941 || path->stream_enabled != stream_old) {
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01003942 last = path_power_update(codec, path, true);
3943 if (last)
3944 changed = last;
3945 }
3946 }
3947 }
3948 return changed;
3949}
3950
Takashi Iwaid5ac0102015-04-09 10:21:30 +02003951/* check the jack status for power control */
3952static bool detect_pin_state(struct hda_codec *codec, hda_nid_t pin)
3953{
3954 if (!is_jack_detectable(codec, pin))
3955 return true;
3956 return snd_hda_jack_detect_state(codec, pin) != HDA_JACK_NOT_PRESENT;
3957}
3958
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01003959/* power up/down the paths of the given pin according to the jack state;
3960 * power = 0/1 : only power up/down if it matches with the jack state,
3961 * < 0 : force power up/down to follow the jack sate
3962 *
3963 * returns the last changed NID, or zero if unchanged.
3964 */
3965static hda_nid_t set_pin_power_jack(struct hda_codec *codec, hda_nid_t pin,
3966 int power)
3967{
3968 bool on;
3969
Takashi Iwai967b1302015-03-20 18:21:03 +01003970 if (!codec->power_save_node)
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01003971 return 0;
3972
Takashi Iwaid5ac0102015-04-09 10:21:30 +02003973 on = detect_pin_state(codec, pin);
3974
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01003975 if (power >= 0 && on != power)
3976 return 0;
3977 return set_path_power(codec, pin, on, -1);
3978}
3979
3980static void pin_power_callback(struct hda_codec *codec,
3981 struct hda_jack_callback *jack,
3982 bool on)
3983{
3984 if (jack && jack->tbl->nid)
3985 sync_power_state_change(codec,
3986 set_pin_power_jack(codec, jack->tbl->nid, on));
3987}
3988
3989/* callback only doing power up -- called at first */
3990static void pin_power_up_callback(struct hda_codec *codec,
3991 struct hda_jack_callback *jack)
3992{
3993 pin_power_callback(codec, jack, true);
3994}
3995
3996/* callback only doing power down -- called at last */
3997static void pin_power_down_callback(struct hda_codec *codec,
3998 struct hda_jack_callback *jack)
3999{
4000 pin_power_callback(codec, jack, false);
4001}
4002
4003/* set up the power up/down callbacks */
4004static void add_pin_power_ctls(struct hda_codec *codec, int num_pins,
4005 const hda_nid_t *pins, bool on)
4006{
4007 int i;
4008 hda_jack_callback_fn cb =
4009 on ? pin_power_up_callback : pin_power_down_callback;
4010
4011 for (i = 0; i < num_pins && pins[i]; i++) {
4012 if (is_jack_detectable(codec, pins[i]))
4013 snd_hda_jack_detect_enable_callback(codec, pins[i], cb);
4014 else
4015 set_path_power(codec, pins[i], true, -1);
4016 }
4017}
4018
4019/* enabled power callback to each available I/O pin with jack detections;
4020 * the digital I/O pins are excluded because of the unreliable detectsion
4021 */
4022static void add_all_pin_power_ctls(struct hda_codec *codec, bool on)
4023{
4024 struct hda_gen_spec *spec = codec->spec;
4025 struct auto_pin_cfg *cfg = &spec->autocfg;
4026 int i;
4027
Takashi Iwai967b1302015-03-20 18:21:03 +01004028 if (!codec->power_save_node)
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004029 return;
4030 add_pin_power_ctls(codec, cfg->line_outs, cfg->line_out_pins, on);
4031 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
4032 add_pin_power_ctls(codec, cfg->hp_outs, cfg->hp_pins, on);
4033 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
4034 add_pin_power_ctls(codec, cfg->speaker_outs, cfg->speaker_pins, on);
4035 for (i = 0; i < cfg->num_inputs; i++)
4036 add_pin_power_ctls(codec, 1, &cfg->inputs[i].pin, on);
4037}
4038
4039/* sync path power up/down with the jack states of given pins */
4040static void sync_pin_power_ctls(struct hda_codec *codec, int num_pins,
4041 const hda_nid_t *pins)
4042{
4043 int i;
4044
4045 for (i = 0; i < num_pins && pins[i]; i++)
4046 if (is_jack_detectable(codec, pins[i]))
4047 set_pin_power_jack(codec, pins[i], -1);
4048}
4049
4050/* sync path power up/down with pins; called at init and resume */
4051static void sync_all_pin_power_ctls(struct hda_codec *codec)
4052{
4053 struct hda_gen_spec *spec = codec->spec;
4054 struct auto_pin_cfg *cfg = &spec->autocfg;
4055 int i;
4056
Takashi Iwai967b1302015-03-20 18:21:03 +01004057 if (!codec->power_save_node)
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004058 return;
4059 sync_pin_power_ctls(codec, cfg->line_outs, cfg->line_out_pins);
4060 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
4061 sync_pin_power_ctls(codec, cfg->hp_outs, cfg->hp_pins);
4062 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
4063 sync_pin_power_ctls(codec, cfg->speaker_outs, cfg->speaker_pins);
4064 for (i = 0; i < cfg->num_inputs; i++)
4065 sync_pin_power_ctls(codec, 1, &cfg->inputs[i].pin);
4066}
Takashi Iwai352f7f92012-12-19 12:52:06 +01004067
Takashi Iwai5ccf8352015-03-18 09:23:10 +01004068/* add fake paths if not present yet */
4069static int add_fake_paths(struct hda_codec *codec, hda_nid_t nid,
4070 int num_pins, const hda_nid_t *pins)
4071{
4072 struct hda_gen_spec *spec = codec->spec;
4073 struct nid_path *path;
4074 int i;
4075
4076 for (i = 0; i < num_pins; i++) {
4077 if (!pins[i])
4078 break;
4079 if (get_nid_path(codec, nid, pins[i], 0))
4080 continue;
4081 path = snd_array_new(&spec->paths);
4082 if (!path)
4083 return -ENOMEM;
4084 memset(path, 0, sizeof(*path));
4085 path->depth = 2;
4086 path->path[0] = nid;
4087 path->path[1] = pins[i];
4088 path->active = true;
4089 }
4090 return 0;
4091}
4092
4093/* create fake paths to all outputs from beep */
4094static int add_fake_beep_paths(struct hda_codec *codec)
4095{
4096 struct hda_gen_spec *spec = codec->spec;
4097 struct auto_pin_cfg *cfg = &spec->autocfg;
4098 hda_nid_t nid = spec->beep_nid;
4099 int err;
4100
Takashi Iwai967b1302015-03-20 18:21:03 +01004101 if (!codec->power_save_node || !nid)
Takashi Iwai5ccf8352015-03-18 09:23:10 +01004102 return 0;
4103 err = add_fake_paths(codec, nid, cfg->line_outs, cfg->line_out_pins);
4104 if (err < 0)
4105 return err;
4106 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
4107 err = add_fake_paths(codec, nid, cfg->hp_outs, cfg->hp_pins);
4108 if (err < 0)
4109 return err;
4110 }
4111 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
4112 err = add_fake_paths(codec, nid, cfg->speaker_outs,
4113 cfg->speaker_pins);
4114 if (err < 0)
4115 return err;
4116 }
4117 return 0;
4118}
4119
4120/* power up/down beep widget and its output paths */
4121static void beep_power_hook(struct hda_beep *beep, bool on)
4122{
4123 set_path_power(beep->codec, beep->nid, -1, on);
4124}
4125
Takashi Iwai6b275b12015-03-20 18:11:05 +01004126/**
4127 * snd_hda_gen_fix_pin_power - Fix the power of the given pin widget to D0
4128 * @codec: the HDA codec
4129 * @pin: NID of pin to fix
4130 */
4131int snd_hda_gen_fix_pin_power(struct hda_codec *codec, hda_nid_t pin)
4132{
4133 struct hda_gen_spec *spec = codec->spec;
4134 struct nid_path *path;
4135
4136 path = snd_array_new(&spec->paths);
4137 if (!path)
4138 return -ENOMEM;
4139 memset(path, 0, sizeof(*path));
4140 path->depth = 1;
4141 path->path[0] = pin;
4142 path->active = true;
4143 path->pin_fixed = true;
4144 path->stream_enabled = true;
4145 return 0;
4146}
4147EXPORT_SYMBOL_GPL(snd_hda_gen_fix_pin_power);
4148
Takashi Iwai352f7f92012-12-19 12:52:06 +01004149/*
4150 * Jack detections for HP auto-mute and mic-switch
4151 */
4152
4153/* check each pin in the given array; returns true if any of them is plugged */
4154static bool detect_jacks(struct hda_codec *codec, int num_pins, hda_nid_t *pins)
4155{
Takashi Iwai60ea8ca2013-07-19 16:59:46 +02004156 int i;
4157 bool present = false;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004158
4159 for (i = 0; i < num_pins; i++) {
4160 hda_nid_t nid = pins[i];
4161 if (!nid)
4162 break;
Takashi Iwai0b4df932013-01-10 09:45:13 +01004163 /* don't detect pins retasked as inputs */
4164 if (snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_IN_EN)
4165 continue;
Takashi Iwai60ea8ca2013-07-19 16:59:46 +02004166 if (snd_hda_jack_detect_state(codec, nid) == HDA_JACK_PRESENT)
4167 present = true;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004168 }
4169 return present;
4170}
4171
4172/* standard HP/line-out auto-mute helper */
4173static void do_automute(struct hda_codec *codec, int num_pins, hda_nid_t *pins,
Takashi Iwaie80c60f2013-08-12 14:44:59 +02004174 int *paths, bool mute)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004175{
4176 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004177 int i;
4178
4179 for (i = 0; i < num_pins; i++) {
4180 hda_nid_t nid = pins[i];
Takashi Iwai967303d2013-02-19 17:12:42 +01004181 unsigned int val, oldval;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004182 if (!nid)
4183 break;
Takashi Iwai7eebffd2013-06-24 16:00:21 +02004184
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004185 oldval = snd_hda_codec_get_pin_target(codec, nid);
4186 if (oldval & PIN_IN)
4187 continue; /* no mute for inputs */
4188
Takashi Iwai7eebffd2013-06-24 16:00:21 +02004189 if (spec->auto_mute_via_amp) {
Takashi Iwaie80c60f2013-08-12 14:44:59 +02004190 struct nid_path *path;
4191 hda_nid_t mute_nid;
4192
4193 path = snd_hda_get_path_from_idx(codec, paths[i]);
4194 if (!path)
4195 continue;
4196 mute_nid = get_amp_nid_(path->ctls[NID_PATH_MUTE_CTL]);
4197 if (!mute_nid)
4198 continue;
Takashi Iwai7eebffd2013-06-24 16:00:21 +02004199 if (mute)
Takashi Iwaie80c60f2013-08-12 14:44:59 +02004200 spec->mute_bits |= (1ULL << mute_nid);
Takashi Iwai7eebffd2013-06-24 16:00:21 +02004201 else
Takashi Iwaie80c60f2013-08-12 14:44:59 +02004202 spec->mute_bits &= ~(1ULL << mute_nid);
Takashi Iwai7eebffd2013-06-24 16:00:21 +02004203 continue;
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004204 } else {
4205 /* don't reset VREF value in case it's controlling
4206 * the amp (see alc861_fixup_asus_amp_vref_0f())
4207 */
4208 if (spec->keep_vref_in_automute)
4209 val = oldval & ~PIN_HP;
4210 else
4211 val = 0;
4212 if (!mute)
4213 val |= oldval;
4214 /* here we call update_pin_ctl() so that the pinctl is
4215 * changed without changing the pinctl target value;
4216 * the original target value will be still referred at
4217 * the init / resume again
4218 */
4219 update_pin_ctl(codec, nid, val);
Takashi Iwai7eebffd2013-06-24 16:00:21 +02004220 }
4221
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01004222 set_pin_eapd(codec, nid, !mute);
Takashi Iwai967b1302015-03-20 18:21:03 +01004223 if (codec->power_save_node) {
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004224 bool on = !mute;
4225 if (on)
Takashi Iwaid5ac0102015-04-09 10:21:30 +02004226 on = detect_pin_state(codec, nid);
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004227 set_path_power(codec, nid, on, -1);
4228 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01004229 }
4230}
4231
Takashi Iwaidda42bd2014-10-30 12:04:50 +01004232/**
4233 * snd_hda_gen_update_outputs - Toggle outputs muting
4234 * @codec: the HDA codec
4235 *
4236 * Update the mute status of all outputs based on the current jack states.
4237 */
Takashi Iwai5d550e12012-12-19 15:16:44 +01004238void snd_hda_gen_update_outputs(struct hda_codec *codec)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004239{
4240 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaie80c60f2013-08-12 14:44:59 +02004241 int *paths;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004242 int on;
4243
4244 /* Control HP pins/amps depending on master_mute state;
4245 * in general, HP pins/amps control should be enabled in all cases,
4246 * but currently set only for master_mute, just to be safe
4247 */
Takashi Iwaie80c60f2013-08-12 14:44:59 +02004248 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
4249 paths = spec->out_paths;
4250 else
4251 paths = spec->hp_paths;
Takashi Iwai967303d2013-02-19 17:12:42 +01004252 do_automute(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
Takashi Iwaie80c60f2013-08-12 14:44:59 +02004253 spec->autocfg.hp_pins, paths, spec->master_mute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004254
4255 if (!spec->automute_speaker)
4256 on = 0;
4257 else
4258 on = spec->hp_jack_present | spec->line_jack_present;
4259 on |= spec->master_mute;
Takashi Iwai47b9ddb2013-01-16 18:18:00 +01004260 spec->speaker_muted = on;
Takashi Iwaie80c60f2013-08-12 14:44:59 +02004261 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
4262 paths = spec->out_paths;
4263 else
4264 paths = spec->speaker_paths;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004265 do_automute(codec, ARRAY_SIZE(spec->autocfg.speaker_pins),
Takashi Iwaie80c60f2013-08-12 14:44:59 +02004266 spec->autocfg.speaker_pins, paths, on);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004267
4268 /* toggle line-out mutes if needed, too */
4269 /* if LO is a copy of either HP or Speaker, don't need to handle it */
4270 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0] ||
4271 spec->autocfg.line_out_pins[0] == spec->autocfg.speaker_pins[0])
4272 return;
4273 if (!spec->automute_lo)
4274 on = 0;
4275 else
4276 on = spec->hp_jack_present;
4277 on |= spec->master_mute;
Takashi Iwai47b9ddb2013-01-16 18:18:00 +01004278 spec->line_out_muted = on;
Takashi Iwaie80c60f2013-08-12 14:44:59 +02004279 paths = spec->out_paths;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004280 do_automute(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
Takashi Iwaie80c60f2013-08-12 14:44:59 +02004281 spec->autocfg.line_out_pins, paths, on);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004282}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004283EXPORT_SYMBOL_GPL(snd_hda_gen_update_outputs);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004284
4285static void call_update_outputs(struct hda_codec *codec)
4286{
4287 struct hda_gen_spec *spec = codec->spec;
4288 if (spec->automute_hook)
4289 spec->automute_hook(codec);
4290 else
Takashi Iwai5d550e12012-12-19 15:16:44 +01004291 snd_hda_gen_update_outputs(codec);
Takashi Iwai7eebffd2013-06-24 16:00:21 +02004292
4293 /* sync the whole vmaster slaves to reflect the new auto-mute status */
4294 if (spec->auto_mute_via_amp && !codec->bus->shutdown)
4295 snd_ctl_sync_vmaster(spec->vmaster_mute.sw_kctl, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004296}
4297
Takashi Iwaidda42bd2014-10-30 12:04:50 +01004298/**
4299 * snd_hda_gen_hp_automute - standard HP-automute helper
4300 * @codec: the HDA codec
4301 * @jack: jack object, NULL for the whole
4302 */
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02004303void snd_hda_gen_hp_automute(struct hda_codec *codec,
4304 struct hda_jack_callback *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004305{
4306 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai92603c52013-01-22 07:46:31 +01004307 hda_nid_t *pins = spec->autocfg.hp_pins;
4308 int num_pins = ARRAY_SIZE(spec->autocfg.hp_pins);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004309
Takashi Iwai92603c52013-01-22 07:46:31 +01004310 /* No detection for the first HP jack during indep-HP mode */
4311 if (spec->indep_hp_enabled) {
4312 pins++;
4313 num_pins--;
4314 }
4315
4316 spec->hp_jack_present = detect_jacks(codec, num_pins, pins);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004317 if (!spec->detect_hp || (!spec->automute_speaker && !spec->automute_lo))
4318 return;
4319 call_update_outputs(codec);
4320}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004321EXPORT_SYMBOL_GPL(snd_hda_gen_hp_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004322
Takashi Iwaidda42bd2014-10-30 12:04:50 +01004323/**
4324 * snd_hda_gen_line_automute - standard line-out-automute helper
4325 * @codec: the HDA codec
4326 * @jack: jack object, NULL for the whole
4327 */
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02004328void snd_hda_gen_line_automute(struct hda_codec *codec,
4329 struct hda_jack_callback *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004330{
4331 struct hda_gen_spec *spec = codec->spec;
4332
4333 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
4334 return;
4335 /* check LO jack only when it's different from HP */
4336 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0])
4337 return;
4338
4339 spec->line_jack_present =
4340 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
4341 spec->autocfg.line_out_pins);
4342 if (!spec->automute_speaker || !spec->detect_lo)
4343 return;
4344 call_update_outputs(codec);
4345}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004346EXPORT_SYMBOL_GPL(snd_hda_gen_line_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004347
Takashi Iwaidda42bd2014-10-30 12:04:50 +01004348/**
4349 * snd_hda_gen_mic_autoswitch - standard mic auto-switch helper
4350 * @codec: the HDA codec
4351 * @jack: jack object, NULL for the whole
4352 */
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02004353void snd_hda_gen_mic_autoswitch(struct hda_codec *codec,
4354 struct hda_jack_callback *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004355{
4356 struct hda_gen_spec *spec = codec->spec;
4357 int i;
4358
4359 if (!spec->auto_mic)
4360 return;
4361
4362 for (i = spec->am_num_entries - 1; i > 0; i--) {
Takashi Iwai0b4df932013-01-10 09:45:13 +01004363 hda_nid_t pin = spec->am_entry[i].pin;
4364 /* don't detect pins retasked as outputs */
4365 if (snd_hda_codec_get_pin_target(codec, pin) & AC_PINCTL_OUT_EN)
4366 continue;
Takashi Iwai60ea8ca2013-07-19 16:59:46 +02004367 if (snd_hda_jack_detect_state(codec, pin) == HDA_JACK_PRESENT) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01004368 mux_select(codec, 0, spec->am_entry[i].idx);
4369 return;
4370 }
4371 }
4372 mux_select(codec, 0, spec->am_entry[0].idx);
4373}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004374EXPORT_SYMBOL_GPL(snd_hda_gen_mic_autoswitch);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004375
Takashi Iwai77afe0e2013-05-31 14:10:03 +02004376/* call appropriate hooks */
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02004377static void call_hp_automute(struct hda_codec *codec,
4378 struct hda_jack_callback *jack)
Takashi Iwai77afe0e2013-05-31 14:10:03 +02004379{
4380 struct hda_gen_spec *spec = codec->spec;
4381 if (spec->hp_automute_hook)
4382 spec->hp_automute_hook(codec, jack);
4383 else
4384 snd_hda_gen_hp_automute(codec, jack);
4385}
4386
4387static void call_line_automute(struct hda_codec *codec,
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02004388 struct hda_jack_callback *jack)
Takashi Iwai77afe0e2013-05-31 14:10:03 +02004389{
4390 struct hda_gen_spec *spec = codec->spec;
4391 if (spec->line_automute_hook)
4392 spec->line_automute_hook(codec, jack);
4393 else
4394 snd_hda_gen_line_automute(codec, jack);
4395}
4396
4397static void call_mic_autoswitch(struct hda_codec *codec,
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02004398 struct hda_jack_callback *jack)
Takashi Iwai77afe0e2013-05-31 14:10:03 +02004399{
4400 struct hda_gen_spec *spec = codec->spec;
4401 if (spec->mic_autoswitch_hook)
4402 spec->mic_autoswitch_hook(codec, jack);
4403 else
4404 snd_hda_gen_mic_autoswitch(codec, jack);
4405}
4406
Takashi Iwai963afde2013-05-31 15:20:31 +02004407/* update jack retasking */
4408static void update_automute_all(struct hda_codec *codec)
4409{
4410 call_hp_automute(codec, NULL);
4411 call_line_automute(codec, NULL);
4412 call_mic_autoswitch(codec, NULL);
4413}
4414
Takashi Iwai352f7f92012-12-19 12:52:06 +01004415/*
4416 * Auto-Mute mode mixer enum support
4417 */
4418static int automute_mode_info(struct snd_kcontrol *kcontrol,
4419 struct snd_ctl_elem_info *uinfo)
4420{
4421 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4422 struct hda_gen_spec *spec = codec->spec;
4423 static const char * const texts3[] = {
4424 "Disabled", "Speaker Only", "Line Out+Speaker"
Takashi Iwai071c73a2006-08-23 18:34:06 +02004425 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004426
Takashi Iwai352f7f92012-12-19 12:52:06 +01004427 if (spec->automute_speaker_possible && spec->automute_lo_possible)
4428 return snd_hda_enum_helper_info(kcontrol, uinfo, 3, texts3);
4429 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
4430}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004431
Takashi Iwai352f7f92012-12-19 12:52:06 +01004432static int automute_mode_get(struct snd_kcontrol *kcontrol,
4433 struct snd_ctl_elem_value *ucontrol)
4434{
4435 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4436 struct hda_gen_spec *spec = codec->spec;
4437 unsigned int val = 0;
4438 if (spec->automute_speaker)
4439 val++;
4440 if (spec->automute_lo)
4441 val++;
Takashi Iwai071c73a2006-08-23 18:34:06 +02004442
Takashi Iwai352f7f92012-12-19 12:52:06 +01004443 ucontrol->value.enumerated.item[0] = val;
4444 return 0;
4445}
4446
4447static int automute_mode_put(struct snd_kcontrol *kcontrol,
4448 struct snd_ctl_elem_value *ucontrol)
4449{
4450 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4451 struct hda_gen_spec *spec = codec->spec;
4452
4453 switch (ucontrol->value.enumerated.item[0]) {
4454 case 0:
4455 if (!spec->automute_speaker && !spec->automute_lo)
4456 return 0;
4457 spec->automute_speaker = 0;
4458 spec->automute_lo = 0;
4459 break;
4460 case 1:
4461 if (spec->automute_speaker_possible) {
4462 if (!spec->automute_lo && spec->automute_speaker)
4463 return 0;
4464 spec->automute_speaker = 1;
4465 spec->automute_lo = 0;
4466 } else if (spec->automute_lo_possible) {
4467 if (spec->automute_lo)
4468 return 0;
4469 spec->automute_lo = 1;
4470 } else
4471 return -EINVAL;
4472 break;
4473 case 2:
4474 if (!spec->automute_lo_possible || !spec->automute_speaker_possible)
4475 return -EINVAL;
4476 if (spec->automute_speaker && spec->automute_lo)
4477 return 0;
4478 spec->automute_speaker = 1;
4479 spec->automute_lo = 1;
4480 break;
4481 default:
4482 return -EINVAL;
4483 }
4484 call_update_outputs(codec);
4485 return 1;
4486}
4487
4488static const struct snd_kcontrol_new automute_mode_enum = {
4489 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
4490 .name = "Auto-Mute Mode",
4491 .info = automute_mode_info,
4492 .get = automute_mode_get,
4493 .put = automute_mode_put,
4494};
4495
4496static int add_automute_mode_enum(struct hda_codec *codec)
4497{
4498 struct hda_gen_spec *spec = codec->spec;
4499
Takashi Iwai12c93df2012-12-19 14:38:33 +01004500 if (!snd_hda_gen_add_kctl(spec, NULL, &automute_mode_enum))
Takashi Iwai352f7f92012-12-19 12:52:06 +01004501 return -ENOMEM;
4502 return 0;
4503}
4504
4505/*
4506 * Check the availability of HP/line-out auto-mute;
4507 * Set up appropriately if really supported
4508 */
4509static int check_auto_mute_availability(struct hda_codec *codec)
4510{
4511 struct hda_gen_spec *spec = codec->spec;
4512 struct auto_pin_cfg *cfg = &spec->autocfg;
4513 int present = 0;
4514 int i, err;
4515
Takashi Iwaif72706b2013-01-16 18:20:07 +01004516 if (spec->suppress_auto_mute)
4517 return 0;
4518
Takashi Iwai352f7f92012-12-19 12:52:06 +01004519 if (cfg->hp_pins[0])
4520 present++;
4521 if (cfg->line_out_pins[0])
4522 present++;
4523 if (cfg->speaker_pins[0])
4524 present++;
4525 if (present < 2) /* need two different output types */
Takashi Iwai071c73a2006-08-23 18:34:06 +02004526 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004527
4528 if (!cfg->speaker_pins[0] &&
4529 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
4530 memcpy(cfg->speaker_pins, cfg->line_out_pins,
4531 sizeof(cfg->speaker_pins));
4532 cfg->speaker_outs = cfg->line_outs;
Takashi Iwai071c73a2006-08-23 18:34:06 +02004533 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004534
Takashi Iwai352f7f92012-12-19 12:52:06 +01004535 if (!cfg->hp_pins[0] &&
4536 cfg->line_out_type == AUTO_PIN_HP_OUT) {
4537 memcpy(cfg->hp_pins, cfg->line_out_pins,
4538 sizeof(cfg->hp_pins));
4539 cfg->hp_outs = cfg->line_outs;
4540 }
4541
4542 for (i = 0; i < cfg->hp_outs; i++) {
4543 hda_nid_t nid = cfg->hp_pins[i];
4544 if (!is_jack_detectable(codec, nid))
4545 continue;
Takashi Iwai4e76a882014-02-25 12:21:03 +01004546 codec_dbg(codec, "Enable HP auto-muting on NID 0x%x\n", nid);
Takashi Iwai62f949b2014-09-11 14:06:53 +02004547 snd_hda_jack_detect_enable_callback(codec, nid,
Takashi Iwai77afe0e2013-05-31 14:10:03 +02004548 call_hp_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004549 spec->detect_hp = 1;
4550 }
4551
4552 if (cfg->line_out_type == AUTO_PIN_LINE_OUT && cfg->line_outs) {
4553 if (cfg->speaker_outs)
4554 for (i = 0; i < cfg->line_outs; i++) {
4555 hda_nid_t nid = cfg->line_out_pins[i];
4556 if (!is_jack_detectable(codec, nid))
4557 continue;
Takashi Iwai4e76a882014-02-25 12:21:03 +01004558 codec_dbg(codec, "Enable Line-Out auto-muting on NID 0x%x\n", nid);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004559 snd_hda_jack_detect_enable_callback(codec, nid,
Takashi Iwai77afe0e2013-05-31 14:10:03 +02004560 call_line_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004561 spec->detect_lo = 1;
4562 }
4563 spec->automute_lo_possible = spec->detect_hp;
4564 }
4565
4566 spec->automute_speaker_possible = cfg->speaker_outs &&
4567 (spec->detect_hp || spec->detect_lo);
4568
4569 spec->automute_lo = spec->automute_lo_possible;
4570 spec->automute_speaker = spec->automute_speaker_possible;
4571
4572 if (spec->automute_speaker_possible || spec->automute_lo_possible) {
4573 /* create a control for automute mode */
4574 err = add_automute_mode_enum(codec);
4575 if (err < 0)
4576 return err;
4577 }
4578 return 0;
4579}
4580
Takashi Iwai352f7f92012-12-19 12:52:06 +01004581/* check whether all auto-mic pins are valid; setup indices if OK */
4582static bool auto_mic_check_imux(struct hda_codec *codec)
4583{
4584 struct hda_gen_spec *spec = codec->spec;
4585 const struct hda_input_mux *imux;
4586 int i;
4587
4588 imux = &spec->input_mux;
4589 for (i = 0; i < spec->am_num_entries; i++) {
4590 spec->am_entry[i].idx =
4591 find_idx_in_nid_list(spec->am_entry[i].pin,
4592 spec->imux_pins, imux->num_items);
4593 if (spec->am_entry[i].idx < 0)
4594 return false; /* no corresponding imux */
4595 }
4596
4597 /* we don't need the jack detection for the first pin */
4598 for (i = 1; i < spec->am_num_entries; i++)
4599 snd_hda_jack_detect_enable_callback(codec,
4600 spec->am_entry[i].pin,
Takashi Iwai77afe0e2013-05-31 14:10:03 +02004601 call_mic_autoswitch);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004602 return true;
4603}
4604
4605static int compare_attr(const void *ap, const void *bp)
4606{
4607 const struct automic_entry *a = ap;
4608 const struct automic_entry *b = bp;
4609 return (int)(a->attr - b->attr);
4610}
4611
4612/*
4613 * Check the availability of auto-mic switch;
4614 * Set up if really supported
4615 */
4616static int check_auto_mic_availability(struct hda_codec *codec)
4617{
4618 struct hda_gen_spec *spec = codec->spec;
4619 struct auto_pin_cfg *cfg = &spec->autocfg;
4620 unsigned int types;
4621 int i, num_pins;
4622
Takashi Iwaid12daf62013-01-07 16:32:11 +01004623 if (spec->suppress_auto_mic)
4624 return 0;
4625
Takashi Iwai352f7f92012-12-19 12:52:06 +01004626 types = 0;
4627 num_pins = 0;
4628 for (i = 0; i < cfg->num_inputs; i++) {
4629 hda_nid_t nid = cfg->inputs[i].pin;
4630 unsigned int attr;
4631 attr = snd_hda_codec_get_pincfg(codec, nid);
4632 attr = snd_hda_get_input_pin_attr(attr);
4633 if (types & (1 << attr))
4634 return 0; /* already occupied */
4635 switch (attr) {
4636 case INPUT_PIN_ATTR_INT:
4637 if (cfg->inputs[i].type != AUTO_PIN_MIC)
4638 return 0; /* invalid type */
4639 break;
4640 case INPUT_PIN_ATTR_UNUSED:
4641 return 0; /* invalid entry */
4642 default:
4643 if (cfg->inputs[i].type > AUTO_PIN_LINE_IN)
4644 return 0; /* invalid type */
4645 if (!spec->line_in_auto_switch &&
4646 cfg->inputs[i].type != AUTO_PIN_MIC)
4647 return 0; /* only mic is allowed */
4648 if (!is_jack_detectable(codec, nid))
4649 return 0; /* no unsol support */
4650 break;
4651 }
4652 if (num_pins >= MAX_AUTO_MIC_PINS)
4653 return 0;
4654 types |= (1 << attr);
4655 spec->am_entry[num_pins].pin = nid;
4656 spec->am_entry[num_pins].attr = attr;
4657 num_pins++;
4658 }
4659
4660 if (num_pins < 2)
4661 return 0;
4662
4663 spec->am_num_entries = num_pins;
4664 /* sort the am_entry in the order of attr so that the pin with a
4665 * higher attr will be selected when the jack is plugged.
4666 */
4667 sort(spec->am_entry, num_pins, sizeof(spec->am_entry[0]),
4668 compare_attr, NULL);
4669
4670 if (!auto_mic_check_imux(codec))
4671 return 0;
4672
4673 spec->auto_mic = 1;
4674 spec->num_adc_nids = 1;
4675 spec->cur_mux[0] = spec->am_entry[0].idx;
Takashi Iwai4e76a882014-02-25 12:21:03 +01004676 codec_dbg(codec, "Enable auto-mic switch on NID 0x%x/0x%x/0x%x\n",
Takashi Iwai352f7f92012-12-19 12:52:06 +01004677 spec->am_entry[0].pin,
4678 spec->am_entry[1].pin,
4679 spec->am_entry[2].pin);
4680
4681 return 0;
4682}
4683
Takashi Iwaidda42bd2014-10-30 12:04:50 +01004684/**
4685 * snd_hda_gen_path_power_filter - power_filter hook to make inactive widgets
4686 * into power down
4687 * @codec: the HDA codec
4688 * @nid: NID to evalute
4689 * @power_state: target power state
4690 */
Takashi Iwaidfc6e462014-01-13 16:09:57 +01004691unsigned int snd_hda_gen_path_power_filter(struct hda_codec *codec,
Takashi Iwai55196ff2013-01-24 17:32:56 +01004692 hda_nid_t nid,
4693 unsigned int power_state)
4694{
Takashi Iwaib6c09b32015-04-09 10:25:03 +02004695 struct hda_gen_spec *spec = codec->spec;
4696
4697 if (!spec->power_down_unused && !codec->power_save_node)
4698 return power_state;
Takashi Iwai7639a062015-03-03 10:07:24 +01004699 if (power_state != AC_PWRST_D0 || nid == codec->core.afg)
Takashi Iwai55196ff2013-01-24 17:32:56 +01004700 return power_state;
4701 if (get_wcaps_type(get_wcaps(codec, nid)) >= AC_WID_POWER)
4702 return power_state;
Takashi Iwaib1b9fbd2013-05-14 12:58:47 +02004703 if (is_active_nid_for_any(codec, nid))
Takashi Iwai55196ff2013-01-24 17:32:56 +01004704 return power_state;
4705 return AC_PWRST_D3;
4706}
Takashi Iwaidfc6e462014-01-13 16:09:57 +01004707EXPORT_SYMBOL_GPL(snd_hda_gen_path_power_filter);
Takashi Iwai55196ff2013-01-24 17:32:56 +01004708
Takashi Iwaiebb93c02013-12-10 17:33:49 +01004709/* mute all aamix inputs initially; parse up to the first leaves */
4710static void mute_all_mixer_nid(struct hda_codec *codec, hda_nid_t mix)
4711{
4712 int i, nums;
4713 const hda_nid_t *conn;
4714 bool has_amp;
4715
4716 nums = snd_hda_get_conn_list(codec, mix, &conn);
4717 has_amp = nid_has_mute(codec, mix, HDA_INPUT);
4718 for (i = 0; i < nums; i++) {
4719 if (has_amp)
Takashi Iwaief403ed2015-03-12 08:30:11 +01004720 update_amp(codec, mix, HDA_INPUT, i,
4721 0xff, HDA_AMP_MUTE);
Takashi Iwaiebb93c02013-12-10 17:33:49 +01004722 else if (nid_has_volume(codec, conn[i], HDA_OUTPUT))
Takashi Iwaief403ed2015-03-12 08:30:11 +01004723 update_amp(codec, conn[i], HDA_OUTPUT, 0,
4724 0xff, HDA_AMP_MUTE);
Takashi Iwaiebb93c02013-12-10 17:33:49 +01004725 }
4726}
Takashi Iwai352f7f92012-12-19 12:52:06 +01004727
Takashi Iwaidda42bd2014-10-30 12:04:50 +01004728/**
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004729 * snd_hda_gen_stream_pm - Stream power management callback
4730 * @codec: the HDA codec
4731 * @nid: audio widget
4732 * @on: power on/off flag
4733 *
Takashi Iwai967b1302015-03-20 18:21:03 +01004734 * Set this in patch_ops.stream_pm. Only valid with power_save_node flag.
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004735 */
4736void snd_hda_gen_stream_pm(struct hda_codec *codec, hda_nid_t nid, bool on)
4737{
Takashi Iwai967b1302015-03-20 18:21:03 +01004738 if (codec->power_save_node)
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004739 set_path_power(codec, nid, -1, on);
4740}
4741EXPORT_SYMBOL_GPL(snd_hda_gen_stream_pm);
4742
4743/**
Takashi Iwaidda42bd2014-10-30 12:04:50 +01004744 * snd_hda_gen_parse_auto_config - Parse the given BIOS configuration and
4745 * set up the hda_gen_spec
4746 * @codec: the HDA codec
4747 * @cfg: Parsed pin configuration
Takashi Iwai9eb413e2012-12-19 14:41:21 +01004748 *
4749 * return 1 if successful, 0 if the proper config is not found,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004750 * or a negative error code
4751 */
4752int snd_hda_gen_parse_auto_config(struct hda_codec *codec,
Takashi Iwai9eb413e2012-12-19 14:41:21 +01004753 struct auto_pin_cfg *cfg)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004754{
4755 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004756 int err;
4757
Takashi Iwai1c70a582013-01-11 17:48:22 +01004758 parse_user_hints(codec);
4759
Takashi Iwaie4a395e2013-01-23 17:00:31 +01004760 if (spec->mixer_nid && !spec->mixer_merge_nid)
4761 spec->mixer_merge_nid = spec->mixer_nid;
4762
Takashi Iwai9eb413e2012-12-19 14:41:21 +01004763 if (cfg != &spec->autocfg) {
4764 spec->autocfg = *cfg;
4765 cfg = &spec->autocfg;
4766 }
4767
Takashi Iwai98bd1112013-03-22 14:53:50 +01004768 if (!spec->main_out_badness)
4769 spec->main_out_badness = &hda_main_out_badness;
4770 if (!spec->extra_out_badness)
4771 spec->extra_out_badness = &hda_extra_out_badness;
4772
David Henningsson6fc4cb92013-01-16 15:58:45 +01004773 fill_all_dac_nids(codec);
4774
Takashi Iwai352f7f92012-12-19 12:52:06 +01004775 if (!cfg->line_outs) {
4776 if (cfg->dig_outs || cfg->dig_in_pin) {
4777 spec->multiout.max_channels = 2;
4778 spec->no_analog = 1;
4779 goto dig_only;
4780 }
Takashi Iwaic9e4bdb2013-12-06 09:30:14 +01004781 if (!cfg->num_inputs && !cfg->dig_in_pin)
4782 return 0; /* can't find valid BIOS pin config */
Takashi Iwai352f7f92012-12-19 12:52:06 +01004783 }
4784
4785 if (!spec->no_primary_hp &&
4786 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT &&
4787 cfg->line_outs <= cfg->hp_outs) {
4788 /* use HP as primary out */
4789 cfg->speaker_outs = cfg->line_outs;
4790 memcpy(cfg->speaker_pins, cfg->line_out_pins,
4791 sizeof(cfg->speaker_pins));
4792 cfg->line_outs = cfg->hp_outs;
4793 memcpy(cfg->line_out_pins, cfg->hp_pins, sizeof(cfg->hp_pins));
4794 cfg->hp_outs = 0;
4795 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
4796 cfg->line_out_type = AUTO_PIN_HP_OUT;
4797 }
4798
4799 err = parse_output_paths(codec);
4800 if (err < 0)
4801 return err;
4802 err = create_multi_channel_mode(codec);
4803 if (err < 0)
4804 return err;
4805 err = create_multi_out_ctls(codec, cfg);
4806 if (err < 0)
4807 return err;
4808 err = create_hp_out_ctls(codec);
4809 if (err < 0)
4810 return err;
4811 err = create_speaker_out_ctls(codec);
4812 if (err < 0)
4813 return err;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01004814 err = create_indep_hp_ctls(codec);
4815 if (err < 0)
4816 return err;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01004817 err = create_loopback_mixing_ctl(codec);
4818 if (err < 0)
4819 return err;
Takashi Iwai967303d2013-02-19 17:12:42 +01004820 err = create_hp_mic(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004821 if (err < 0)
4822 return err;
4823 err = create_input_ctls(codec);
Takashi Iwaid13bd412008-07-30 15:01:45 +02004824 if (err < 0)
Takashi Iwai071c73a2006-08-23 18:34:06 +02004825 return err;
4826
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004827 /* add power-down pin callbacks at first */
4828 add_all_pin_power_ctls(codec, false);
4829
Takashi Iwaia07a9492013-01-07 16:44:06 +01004830 spec->const_channel_count = spec->ext_channel_count;
4831 /* check the multiple speaker and headphone pins */
4832 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
4833 spec->const_channel_count = max(spec->const_channel_count,
4834 cfg->speaker_outs * 2);
4835 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
4836 spec->const_channel_count = max(spec->const_channel_count,
4837 cfg->hp_outs * 2);
4838 spec->multiout.max_channels = max(spec->ext_channel_count,
4839 spec->const_channel_count);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004840
4841 err = check_auto_mute_availability(codec);
4842 if (err < 0)
4843 return err;
4844
4845 err = check_dyn_adc_switch(codec);
4846 if (err < 0)
4847 return err;
4848
Takashi Iwai967303d2013-02-19 17:12:42 +01004849 err = check_auto_mic_availability(codec);
4850 if (err < 0)
4851 return err;
Takashi Iwai071c73a2006-08-23 18:34:06 +02004852
Takashi Iwaif1e762d2013-12-09 16:02:24 +01004853 /* add stereo mix if available and not enabled yet */
4854 if (!spec->auto_mic && spec->mixer_nid &&
Takashi Iwai74f14b32014-12-15 13:43:59 +01004855 spec->add_stereo_mix_input == HDA_HINT_STEREO_MIX_AUTO &&
4856 spec->input_mux.num_items > 1) {
Takashi Iwaif1e762d2013-12-09 16:02:24 +01004857 err = parse_capture_source(codec, spec->mixer_nid,
4858 CFG_IDX_MIX, spec->num_all_adcs,
4859 "Stereo Mix", 0);
4860 if (err < 0)
4861 return err;
4862 }
4863
4864
Takashi Iwai352f7f92012-12-19 12:52:06 +01004865 err = create_capture_mixers(codec);
4866 if (err < 0)
4867 return err;
4868
4869 err = parse_mic_boost(codec);
4870 if (err < 0)
4871 return err;
4872
Takashi Iwaiced4cef2013-11-26 08:33:45 +01004873 /* create "Headphone Mic Jack Mode" if no input selection is
4874 * available (or user specifies add_jack_modes hint)
4875 */
4876 if (spec->hp_mic_pin &&
4877 (spec->auto_mic || spec->input_mux.num_items == 1 ||
4878 spec->add_jack_modes)) {
4879 err = create_hp_mic_jack_mode(codec, spec->hp_mic_pin);
4880 if (err < 0)
4881 return err;
4882 }
4883
Takashi Iwaif811c3c2013-03-07 18:32:59 +01004884 if (spec->add_jack_modes) {
Takashi Iwai978e77e2013-01-10 16:57:58 +01004885 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
4886 err = create_out_jack_modes(codec, cfg->line_outs,
4887 cfg->line_out_pins);
4888 if (err < 0)
4889 return err;
4890 }
4891 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
4892 err = create_out_jack_modes(codec, cfg->hp_outs,
4893 cfg->hp_pins);
4894 if (err < 0)
4895 return err;
4896 }
4897 }
4898
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01004899 /* add power-up pin callbacks at last */
4900 add_all_pin_power_ctls(codec, true);
4901
Takashi Iwaiebb93c02013-12-10 17:33:49 +01004902 /* mute all aamix input initially */
4903 if (spec->mixer_nid)
4904 mute_all_mixer_nid(codec, spec->mixer_nid);
4905
Takashi Iwai352f7f92012-12-19 12:52:06 +01004906 dig_only:
4907 parse_digital(codec);
4908
Takashi Iwai49fb1892015-05-27 08:37:19 +02004909 if (spec->power_down_unused || codec->power_save_node) {
Takashi Iwai24fef902015-04-09 10:28:15 +02004910 if (!codec->power_filter)
4911 codec->power_filter = snd_hda_gen_path_power_filter;
Takashi Iwai49fb1892015-05-27 08:37:19 +02004912 if (!codec->patch_ops.stream_pm)
4913 codec->patch_ops.stream_pm = snd_hda_gen_stream_pm;
4914 }
Takashi Iwai55196ff2013-01-24 17:32:56 +01004915
Takashi Iwai7504b6c2013-03-18 11:25:51 +01004916 if (!spec->no_analog && spec->beep_nid) {
4917 err = snd_hda_attach_beep_device(codec, spec->beep_nid);
4918 if (err < 0)
4919 return err;
Takashi Iwai967b1302015-03-20 18:21:03 +01004920 if (codec->beep && codec->power_save_node) {
Takashi Iwai5ccf8352015-03-18 09:23:10 +01004921 err = add_fake_beep_paths(codec);
4922 if (err < 0)
4923 return err;
4924 codec->beep->power_hook = beep_power_hook;
4925 }
Takashi Iwai7504b6c2013-03-18 11:25:51 +01004926 }
4927
Takashi Iwai352f7f92012-12-19 12:52:06 +01004928 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004929}
Takashi Iwai2698ea92013-12-18 07:45:52 +01004930EXPORT_SYMBOL_GPL(snd_hda_gen_parse_auto_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004931
4932
4933/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01004934 * Build control elements
Linus Torvalds1da177e2005-04-16 15:20:36 -07004935 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01004936
4937/* slave controls for virtual master */
4938static const char * const slave_pfxs[] = {
4939 "Front", "Surround", "Center", "LFE", "Side",
4940 "Headphone", "Speaker", "Mono", "Line Out",
4941 "CLFE", "Bass Speaker", "PCM",
Takashi Iwaiee79c692013-01-07 09:57:42 +01004942 "Speaker Front", "Speaker Surround", "Speaker CLFE", "Speaker Side",
4943 "Headphone Front", "Headphone Surround", "Headphone CLFE",
David Henningsson03ad6a82014-10-16 15:33:45 +02004944 "Headphone Side", "Headphone+LO", "Speaker+LO",
Takashi Iwai352f7f92012-12-19 12:52:06 +01004945 NULL,
4946};
4947
Takashi Iwaidda42bd2014-10-30 12:04:50 +01004948/**
4949 * snd_hda_gen_build_controls - Build controls from the parsed results
4950 * @codec: the HDA codec
4951 *
4952 * Pass this to build_controls patch_ops.
4953 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01004954int snd_hda_gen_build_controls(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004955{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004956 struct hda_gen_spec *spec = codec->spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004957 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004958
Takashi Iwai36502d02012-12-19 15:15:10 +01004959 if (spec->kctls.used) {
4960 err = snd_hda_add_new_ctls(codec, spec->kctls.list);
4961 if (err < 0)
4962 return err;
4963 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004964
Takashi Iwai352f7f92012-12-19 12:52:06 +01004965 if (spec->multiout.dig_out_nid) {
4966 err = snd_hda_create_dig_out_ctls(codec,
4967 spec->multiout.dig_out_nid,
4968 spec->multiout.dig_out_nid,
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01004969 spec->pcm_rec[1]->pcm_type);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004970 if (err < 0)
4971 return err;
4972 if (!spec->no_analog) {
4973 err = snd_hda_create_spdif_share_sw(codec,
4974 &spec->multiout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004975 if (err < 0)
4976 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004977 spec->multiout.share_spdif = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004978 }
4979 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01004980 if (spec->dig_in_nid) {
4981 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
4982 if (err < 0)
4983 return err;
4984 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004985
Takashi Iwai352f7f92012-12-19 12:52:06 +01004986 /* if we have no master control, let's create it */
4987 if (!spec->no_analog &&
4988 !snd_hda_find_mixer_ctl(codec, "Master Playback Volume")) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01004989 err = snd_hda_add_vmaster(codec, "Master Playback Volume",
Takashi Iwai7a71bbf2013-01-17 10:25:15 +01004990 spec->vmaster_tlv, slave_pfxs,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004991 "Playback Volume");
4992 if (err < 0)
4993 return err;
4994 }
4995 if (!spec->no_analog &&
4996 !snd_hda_find_mixer_ctl(codec, "Master Playback Switch")) {
4997 err = __snd_hda_add_vmaster(codec, "Master Playback Switch",
4998 NULL, slave_pfxs,
4999 "Playback Switch",
5000 true, &spec->vmaster_mute.sw_kctl);
5001 if (err < 0)
5002 return err;
Takashi Iwaib63eae02013-10-25 23:43:10 +02005003 if (spec->vmaster_mute.hook) {
Takashi Iwaifd25a972012-12-20 14:57:18 +01005004 snd_hda_add_vmaster_hook(codec, &spec->vmaster_mute,
5005 spec->vmaster_mute_enum);
Takashi Iwaib63eae02013-10-25 23:43:10 +02005006 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
5007 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01005008 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005009
Takashi Iwai352f7f92012-12-19 12:52:06 +01005010 free_kctls(spec); /* no longer needed */
5011
Takashi Iwai352f7f92012-12-19 12:52:06 +01005012 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
5013 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005014 return err;
5015
5016 return 0;
5017}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005018EXPORT_SYMBOL_GPL(snd_hda_gen_build_controls);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005019
Linus Torvalds1da177e2005-04-16 15:20:36 -07005020
5021/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01005022 * PCM definitions
Linus Torvalds1da177e2005-04-16 15:20:36 -07005023 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005024
Takashi Iwaie6b85f32013-01-07 11:54:34 +01005025static void call_pcm_playback_hook(struct hda_pcm_stream *hinfo,
5026 struct hda_codec *codec,
5027 struct snd_pcm_substream *substream,
5028 int action)
5029{
5030 struct hda_gen_spec *spec = codec->spec;
5031 if (spec->pcm_playback_hook)
5032 spec->pcm_playback_hook(hinfo, codec, substream, action);
5033}
5034
Takashi Iwaiac2e8732013-01-17 15:57:10 +01005035static void call_pcm_capture_hook(struct hda_pcm_stream *hinfo,
5036 struct hda_codec *codec,
5037 struct snd_pcm_substream *substream,
5038 int action)
5039{
5040 struct hda_gen_spec *spec = codec->spec;
5041 if (spec->pcm_capture_hook)
5042 spec->pcm_capture_hook(hinfo, codec, substream, action);
5043}
5044
Takashi Iwai352f7f92012-12-19 12:52:06 +01005045/*
5046 * Analog playback callbacks
5047 */
5048static int playback_pcm_open(struct hda_pcm_stream *hinfo,
5049 struct hda_codec *codec,
5050 struct snd_pcm_substream *substream)
5051{
5052 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01005053 int err;
5054
5055 mutex_lock(&spec->pcm_mutex);
5056 err = snd_hda_multi_out_analog_open(codec,
5057 &spec->multiout, substream,
Takashi Iwai352f7f92012-12-19 12:52:06 +01005058 hinfo);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01005059 if (!err) {
Takashi Iwai38cf6f12012-12-21 14:09:42 +01005060 spec->active_streams |= 1 << STREAM_MULTI_OUT;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01005061 call_pcm_playback_hook(hinfo, codec, substream,
5062 HDA_GEN_PCM_ACT_OPEN);
5063 }
Takashi Iwai38cf6f12012-12-21 14:09:42 +01005064 mutex_unlock(&spec->pcm_mutex);
5065 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005066}
5067
5068static int playback_pcm_prepare(struct hda_pcm_stream *hinfo,
Takashi Iwai97ec5582006-03-21 11:29:07 +01005069 struct hda_codec *codec,
5070 unsigned int stream_tag,
5071 unsigned int format,
5072 struct snd_pcm_substream *substream)
5073{
Takashi Iwai352f7f92012-12-19 12:52:06 +01005074 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01005075 int err;
5076
5077 err = snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
5078 stream_tag, format, substream);
5079 if (!err)
5080 call_pcm_playback_hook(hinfo, codec, substream,
5081 HDA_GEN_PCM_ACT_PREPARE);
5082 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005083}
Takashi Iwai97ec5582006-03-21 11:29:07 +01005084
Takashi Iwai352f7f92012-12-19 12:52:06 +01005085static int playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
5086 struct hda_codec *codec,
5087 struct snd_pcm_substream *substream)
5088{
5089 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01005090 int err;
5091
5092 err = snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
5093 if (!err)
5094 call_pcm_playback_hook(hinfo, codec, substream,
5095 HDA_GEN_PCM_ACT_CLEANUP);
5096 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005097}
5098
Takashi Iwai38cf6f12012-12-21 14:09:42 +01005099static int playback_pcm_close(struct hda_pcm_stream *hinfo,
5100 struct hda_codec *codec,
5101 struct snd_pcm_substream *substream)
5102{
5103 struct hda_gen_spec *spec = codec->spec;
5104 mutex_lock(&spec->pcm_mutex);
5105 spec->active_streams &= ~(1 << STREAM_MULTI_OUT);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01005106 call_pcm_playback_hook(hinfo, codec, substream,
5107 HDA_GEN_PCM_ACT_CLOSE);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01005108 mutex_unlock(&spec->pcm_mutex);
5109 return 0;
5110}
5111
Takashi Iwaiac2e8732013-01-17 15:57:10 +01005112static int capture_pcm_open(struct hda_pcm_stream *hinfo,
5113 struct hda_codec *codec,
5114 struct snd_pcm_substream *substream)
5115{
5116 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_OPEN);
5117 return 0;
5118}
5119
5120static int capture_pcm_prepare(struct hda_pcm_stream *hinfo,
5121 struct hda_codec *codec,
5122 unsigned int stream_tag,
5123 unsigned int format,
5124 struct snd_pcm_substream *substream)
5125{
5126 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
5127 call_pcm_capture_hook(hinfo, codec, substream,
5128 HDA_GEN_PCM_ACT_PREPARE);
5129 return 0;
5130}
5131
5132static int capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
5133 struct hda_codec *codec,
5134 struct snd_pcm_substream *substream)
5135{
5136 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
5137 call_pcm_capture_hook(hinfo, codec, substream,
5138 HDA_GEN_PCM_ACT_CLEANUP);
5139 return 0;
5140}
5141
5142static int capture_pcm_close(struct hda_pcm_stream *hinfo,
5143 struct hda_codec *codec,
5144 struct snd_pcm_substream *substream)
5145{
5146 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_CLOSE);
5147 return 0;
5148}
5149
Takashi Iwai38cf6f12012-12-21 14:09:42 +01005150static int alt_playback_pcm_open(struct hda_pcm_stream *hinfo,
5151 struct hda_codec *codec,
5152 struct snd_pcm_substream *substream)
5153{
5154 struct hda_gen_spec *spec = codec->spec;
5155 int err = 0;
5156
5157 mutex_lock(&spec->pcm_mutex);
Takashi Iwaid1f15e02015-07-08 09:22:10 +02005158 if (spec->indep_hp && !spec->indep_hp_enabled)
Takashi Iwai38cf6f12012-12-21 14:09:42 +01005159 err = -EBUSY;
5160 else
5161 spec->active_streams |= 1 << STREAM_INDEP_HP;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01005162 call_pcm_playback_hook(hinfo, codec, substream,
5163 HDA_GEN_PCM_ACT_OPEN);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01005164 mutex_unlock(&spec->pcm_mutex);
5165 return err;
5166}
5167
5168static int alt_playback_pcm_close(struct hda_pcm_stream *hinfo,
5169 struct hda_codec *codec,
5170 struct snd_pcm_substream *substream)
5171{
5172 struct hda_gen_spec *spec = codec->spec;
5173 mutex_lock(&spec->pcm_mutex);
5174 spec->active_streams &= ~(1 << STREAM_INDEP_HP);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01005175 call_pcm_playback_hook(hinfo, codec, substream,
5176 HDA_GEN_PCM_ACT_CLOSE);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01005177 mutex_unlock(&spec->pcm_mutex);
5178 return 0;
5179}
5180
Takashi Iwaie6b85f32013-01-07 11:54:34 +01005181static int alt_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
5182 struct hda_codec *codec,
5183 unsigned int stream_tag,
5184 unsigned int format,
5185 struct snd_pcm_substream *substream)
5186{
5187 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
5188 call_pcm_playback_hook(hinfo, codec, substream,
5189 HDA_GEN_PCM_ACT_PREPARE);
5190 return 0;
5191}
5192
5193static int alt_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
5194 struct hda_codec *codec,
5195 struct snd_pcm_substream *substream)
5196{
5197 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
5198 call_pcm_playback_hook(hinfo, codec, substream,
5199 HDA_GEN_PCM_ACT_CLEANUP);
5200 return 0;
5201}
5202
Takashi Iwai352f7f92012-12-19 12:52:06 +01005203/*
5204 * Digital out
5205 */
5206static int dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
5207 struct hda_codec *codec,
5208 struct snd_pcm_substream *substream)
5209{
5210 struct hda_gen_spec *spec = codec->spec;
5211 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
5212}
5213
5214static int dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
5215 struct hda_codec *codec,
5216 unsigned int stream_tag,
5217 unsigned int format,
5218 struct snd_pcm_substream *substream)
5219{
5220 struct hda_gen_spec *spec = codec->spec;
5221 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
5222 stream_tag, format, substream);
5223}
5224
5225static int dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
5226 struct hda_codec *codec,
5227 struct snd_pcm_substream *substream)
5228{
5229 struct hda_gen_spec *spec = codec->spec;
5230 return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
5231}
5232
5233static int dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
5234 struct hda_codec *codec,
5235 struct snd_pcm_substream *substream)
5236{
5237 struct hda_gen_spec *spec = codec->spec;
5238 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
5239}
5240
5241/*
5242 * Analog capture
5243 */
Takashi Iwaiac2e8732013-01-17 15:57:10 +01005244#define alt_capture_pcm_open capture_pcm_open
5245#define alt_capture_pcm_close capture_pcm_close
5246
Takashi Iwai352f7f92012-12-19 12:52:06 +01005247static int alt_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
5248 struct hda_codec *codec,
5249 unsigned int stream_tag,
5250 unsigned int format,
5251 struct snd_pcm_substream *substream)
5252{
5253 struct hda_gen_spec *spec = codec->spec;
5254
5255 snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number + 1],
Takashi Iwai97ec5582006-03-21 11:29:07 +01005256 stream_tag, 0, format);
Takashi Iwaiac2e8732013-01-17 15:57:10 +01005257 call_pcm_capture_hook(hinfo, codec, substream,
5258 HDA_GEN_PCM_ACT_PREPARE);
Takashi Iwai97ec5582006-03-21 11:29:07 +01005259 return 0;
5260}
5261
Takashi Iwai352f7f92012-12-19 12:52:06 +01005262static int alt_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
5263 struct hda_codec *codec,
5264 struct snd_pcm_substream *substream)
Takashi Iwai97ec5582006-03-21 11:29:07 +01005265{
Takashi Iwai352f7f92012-12-19 12:52:06 +01005266 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai97ec5582006-03-21 11:29:07 +01005267
Takashi Iwai352f7f92012-12-19 12:52:06 +01005268 snd_hda_codec_cleanup_stream(codec,
5269 spec->adc_nids[substream->number + 1]);
Takashi Iwaiac2e8732013-01-17 15:57:10 +01005270 call_pcm_capture_hook(hinfo, codec, substream,
5271 HDA_GEN_PCM_ACT_CLEANUP);
Takashi Iwai97ec5582006-03-21 11:29:07 +01005272 return 0;
5273}
5274
Takashi Iwai352f7f92012-12-19 12:52:06 +01005275/*
5276 */
5277static const struct hda_pcm_stream pcm_analog_playback = {
5278 .substreams = 1,
5279 .channels_min = 2,
5280 .channels_max = 8,
5281 /* NID is set in build_pcms */
5282 .ops = {
5283 .open = playback_pcm_open,
Takashi Iwai38cf6f12012-12-21 14:09:42 +01005284 .close = playback_pcm_close,
Takashi Iwai352f7f92012-12-19 12:52:06 +01005285 .prepare = playback_pcm_prepare,
5286 .cleanup = playback_pcm_cleanup
5287 },
5288};
Linus Torvalds1da177e2005-04-16 15:20:36 -07005289
Takashi Iwai352f7f92012-12-19 12:52:06 +01005290static const struct hda_pcm_stream pcm_analog_capture = {
5291 .substreams = 1,
5292 .channels_min = 2,
5293 .channels_max = 2,
5294 /* NID is set in build_pcms */
Takashi Iwaiac2e8732013-01-17 15:57:10 +01005295 .ops = {
5296 .open = capture_pcm_open,
5297 .close = capture_pcm_close,
5298 .prepare = capture_pcm_prepare,
5299 .cleanup = capture_pcm_cleanup
5300 },
Takashi Iwai352f7f92012-12-19 12:52:06 +01005301};
5302
5303static const struct hda_pcm_stream pcm_analog_alt_playback = {
5304 .substreams = 1,
5305 .channels_min = 2,
5306 .channels_max = 2,
5307 /* NID is set in build_pcms */
Takashi Iwai38cf6f12012-12-21 14:09:42 +01005308 .ops = {
5309 .open = alt_playback_pcm_open,
Takashi Iwaie6b85f32013-01-07 11:54:34 +01005310 .close = alt_playback_pcm_close,
5311 .prepare = alt_playback_pcm_prepare,
5312 .cleanup = alt_playback_pcm_cleanup
Takashi Iwai38cf6f12012-12-21 14:09:42 +01005313 },
Takashi Iwai352f7f92012-12-19 12:52:06 +01005314};
5315
5316static const struct hda_pcm_stream pcm_analog_alt_capture = {
5317 .substreams = 2, /* can be overridden */
5318 .channels_min = 2,
5319 .channels_max = 2,
5320 /* NID is set in build_pcms */
5321 .ops = {
Takashi Iwaiac2e8732013-01-17 15:57:10 +01005322 .open = alt_capture_pcm_open,
5323 .close = alt_capture_pcm_close,
Takashi Iwai352f7f92012-12-19 12:52:06 +01005324 .prepare = alt_capture_pcm_prepare,
5325 .cleanup = alt_capture_pcm_cleanup
5326 },
5327};
5328
5329static const struct hda_pcm_stream pcm_digital_playback = {
5330 .substreams = 1,
5331 .channels_min = 2,
5332 .channels_max = 2,
5333 /* NID is set in build_pcms */
5334 .ops = {
5335 .open = dig_playback_pcm_open,
5336 .close = dig_playback_pcm_close,
5337 .prepare = dig_playback_pcm_prepare,
5338 .cleanup = dig_playback_pcm_cleanup
5339 },
5340};
5341
5342static const struct hda_pcm_stream pcm_digital_capture = {
5343 .substreams = 1,
5344 .channels_min = 2,
5345 .channels_max = 2,
5346 /* NID is set in build_pcms */
5347};
5348
5349/* Used by build_pcms to flag that a PCM has no playback stream */
5350static const struct hda_pcm_stream pcm_null_stream = {
5351 .substreams = 0,
5352 .channels_min = 0,
5353 .channels_max = 0,
5354};
5355
5356/*
5357 * dynamic changing ADC PCM streams
5358 */
5359static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur)
5360{
5361 struct hda_gen_spec *spec = codec->spec;
5362 hda_nid_t new_adc = spec->adc_nids[spec->dyn_adc_idx[cur]];
5363
5364 if (spec->cur_adc && spec->cur_adc != new_adc) {
5365 /* stream is running, let's swap the current ADC */
5366 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
5367 spec->cur_adc = new_adc;
5368 snd_hda_codec_setup_stream(codec, new_adc,
5369 spec->cur_adc_stream_tag, 0,
5370 spec->cur_adc_format);
5371 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005372 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01005373 return false;
5374}
5375
5376/* analog capture with dynamic dual-adc changes */
5377static int dyn_adc_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
5378 struct hda_codec *codec,
5379 unsigned int stream_tag,
5380 unsigned int format,
5381 struct snd_pcm_substream *substream)
5382{
5383 struct hda_gen_spec *spec = codec->spec;
5384 spec->cur_adc = spec->adc_nids[spec->dyn_adc_idx[spec->cur_mux[0]]];
5385 spec->cur_adc_stream_tag = stream_tag;
5386 spec->cur_adc_format = format;
5387 snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
5388 return 0;
5389}
5390
5391static int dyn_adc_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
5392 struct hda_codec *codec,
5393 struct snd_pcm_substream *substream)
5394{
5395 struct hda_gen_spec *spec = codec->spec;
5396 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
5397 spec->cur_adc = 0;
5398 return 0;
5399}
5400
5401static const struct hda_pcm_stream dyn_adc_pcm_analog_capture = {
5402 .substreams = 1,
5403 .channels_min = 2,
5404 .channels_max = 2,
5405 .nid = 0, /* fill later */
5406 .ops = {
5407 .prepare = dyn_adc_capture_pcm_prepare,
5408 .cleanup = dyn_adc_capture_pcm_cleanup
5409 },
5410};
5411
Takashi Iwaif873e532012-12-20 16:58:39 +01005412static void fill_pcm_stream_name(char *str, size_t len, const char *sfx,
5413 const char *chip_name)
5414{
5415 char *p;
5416
5417 if (*str)
5418 return;
5419 strlcpy(str, chip_name, len);
5420
5421 /* drop non-alnum chars after a space */
5422 for (p = strchr(str, ' '); p; p = strchr(p + 1, ' ')) {
5423 if (!isalnum(p[1])) {
5424 *p = 0;
5425 break;
5426 }
5427 }
5428 strlcat(str, sfx, len);
5429}
5430
Takashi Iwaifb83b632015-03-16 23:34:34 +01005431/* copy PCM stream info from @default_str, and override non-NULL entries
5432 * from @spec_str and @nid
5433 */
5434static void setup_pcm_stream(struct hda_pcm_stream *str,
5435 const struct hda_pcm_stream *default_str,
5436 const struct hda_pcm_stream *spec_str,
5437 hda_nid_t nid)
5438{
5439 *str = *default_str;
5440 if (nid)
5441 str->nid = nid;
5442 if (spec_str) {
5443 if (spec_str->substreams)
5444 str->substreams = spec_str->substreams;
5445 if (spec_str->channels_min)
5446 str->channels_min = spec_str->channels_min;
5447 if (spec_str->channels_max)
5448 str->channels_max = spec_str->channels_max;
5449 if (spec_str->rates)
5450 str->rates = spec_str->rates;
5451 if (spec_str->formats)
5452 str->formats = spec_str->formats;
5453 if (spec_str->maxbps)
5454 str->maxbps = spec_str->maxbps;
5455 }
5456}
5457
Takashi Iwaidda42bd2014-10-30 12:04:50 +01005458/**
5459 * snd_hda_gen_build_pcms - build PCM streams based on the parsed results
5460 * @codec: the HDA codec
5461 *
5462 * Pass this to build_pcms patch_ops.
5463 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01005464int snd_hda_gen_build_pcms(struct hda_codec *codec)
5465{
5466 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01005467 struct hda_pcm *info;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005468 bool have_multi_adcs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005469
Takashi Iwai352f7f92012-12-19 12:52:06 +01005470 if (spec->no_analog)
5471 goto skip_analog;
5472
Takashi Iwaif873e532012-12-20 16:58:39 +01005473 fill_pcm_stream_name(spec->stream_name_analog,
5474 sizeof(spec->stream_name_analog),
Takashi Iwai7639a062015-03-03 10:07:24 +01005475 " Analog", codec->core.chip_name);
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01005476 info = snd_hda_codec_pcm_new(codec, "%s", spec->stream_name_analog);
5477 if (!info)
5478 return -ENOMEM;
5479 spec->pcm_rec[0] = info;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005480
5481 if (spec->multiout.num_dacs > 0) {
Takashi Iwaifb83b632015-03-16 23:34:34 +01005482 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_PLAYBACK],
5483 &pcm_analog_playback,
5484 spec->stream_analog_playback,
5485 spec->multiout.dac_nids[0]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005486 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
5487 spec->multiout.max_channels;
5488 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT &&
5489 spec->autocfg.line_outs == 2)
5490 info->stream[SNDRV_PCM_STREAM_PLAYBACK].chmap =
5491 snd_pcm_2_1_chmaps;
5492 }
5493 if (spec->num_adc_nids) {
Takashi Iwaifb83b632015-03-16 23:34:34 +01005494 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_CAPTURE],
5495 (spec->dyn_adc_switch ?
5496 &dyn_adc_pcm_analog_capture : &pcm_analog_capture),
5497 spec->stream_analog_capture,
5498 spec->adc_nids[0]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005499 }
5500
Takashi Iwai352f7f92012-12-19 12:52:06 +01005501 skip_analog:
5502 /* SPDIF for stream index #1 */
5503 if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
Takashi Iwaif873e532012-12-20 16:58:39 +01005504 fill_pcm_stream_name(spec->stream_name_digital,
5505 sizeof(spec->stream_name_digital),
Takashi Iwai7639a062015-03-03 10:07:24 +01005506 " Digital", codec->core.chip_name);
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01005507 info = snd_hda_codec_pcm_new(codec, "%s",
5508 spec->stream_name_digital);
5509 if (!info)
5510 return -ENOMEM;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005511 codec->slave_dig_outs = spec->multiout.slave_dig_outs;
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01005512 spec->pcm_rec[1] = info;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005513 if (spec->dig_out_type)
5514 info->pcm_type = spec->dig_out_type;
5515 else
5516 info->pcm_type = HDA_PCM_TYPE_SPDIF;
Takashi Iwaifb83b632015-03-16 23:34:34 +01005517 if (spec->multiout.dig_out_nid)
5518 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_PLAYBACK],
5519 &pcm_digital_playback,
5520 spec->stream_digital_playback,
5521 spec->multiout.dig_out_nid);
5522 if (spec->dig_in_nid)
5523 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_CAPTURE],
5524 &pcm_digital_capture,
5525 spec->stream_digital_capture,
5526 spec->dig_in_nid);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005527 }
5528
5529 if (spec->no_analog)
5530 return 0;
5531
5532 /* If the use of more than one ADC is requested for the current
5533 * model, configure a second analog capture-only PCM.
5534 */
5535 have_multi_adcs = (spec->num_adc_nids > 1) &&
5536 !spec->dyn_adc_switch && !spec->auto_mic;
5537 /* Additional Analaog capture for index #2 */
5538 if (spec->alt_dac_nid || have_multi_adcs) {
Takashi Iwaia6071482013-01-21 16:50:09 +01005539 fill_pcm_stream_name(spec->stream_name_alt_analog,
5540 sizeof(spec->stream_name_alt_analog),
Takashi Iwai7639a062015-03-03 10:07:24 +01005541 " Alt Analog", codec->core.chip_name);
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01005542 info = snd_hda_codec_pcm_new(codec, "%s",
5543 spec->stream_name_alt_analog);
5544 if (!info)
5545 return -ENOMEM;
5546 spec->pcm_rec[2] = info;
Takashi Iwaifb83b632015-03-16 23:34:34 +01005547 if (spec->alt_dac_nid)
5548 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_PLAYBACK],
5549 &pcm_analog_alt_playback,
5550 spec->stream_analog_alt_playback,
5551 spec->alt_dac_nid);
5552 else
5553 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_PLAYBACK],
5554 &pcm_null_stream, NULL, 0);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005555 if (have_multi_adcs) {
Takashi Iwaifb83b632015-03-16 23:34:34 +01005556 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_CAPTURE],
5557 &pcm_analog_alt_capture,
5558 spec->stream_analog_alt_capture,
5559 spec->adc_nids[1]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005560 info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams =
5561 spec->num_adc_nids - 1;
5562 } else {
Takashi Iwaifb83b632015-03-16 23:34:34 +01005563 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_CAPTURE],
5564 &pcm_null_stream, NULL, 0);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005565 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005566 }
5567
5568 return 0;
5569}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005570EXPORT_SYMBOL_GPL(snd_hda_gen_build_pcms);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005571
5572
5573/*
5574 * Standard auto-parser initializations
5575 */
5576
Takashi Iwaid4156932013-01-07 10:08:02 +01005577/* configure the given path as a proper output */
Takashi Iwai2c12c302013-01-10 09:33:29 +01005578static void set_output_and_unmute(struct hda_codec *codec, int path_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01005579{
5580 struct nid_path *path;
Takashi Iwaid4156932013-01-07 10:08:02 +01005581 hda_nid_t pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005582
Takashi Iwai196c17662013-01-04 15:01:40 +01005583 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwaid4156932013-01-07 10:08:02 +01005584 if (!path || !path->depth)
Takashi Iwai352f7f92012-12-19 12:52:06 +01005585 return;
Takashi Iwaid4156932013-01-07 10:08:02 +01005586 pin = path->path[path->depth - 1];
Takashi Iwai2c12c302013-01-10 09:33:29 +01005587 restore_pin_ctl(codec, pin);
Takashi Iwai65033cc2013-04-16 12:31:05 +02005588 snd_hda_activate_path(codec, path, path->active,
5589 aamix_default(codec->spec));
Takashi Iwaie1284af2013-01-03 16:33:02 +01005590 set_pin_eapd(codec, pin, path->active);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005591}
5592
5593/* initialize primary output paths */
5594static void init_multi_out(struct hda_codec *codec)
5595{
5596 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005597 int i;
5598
Takashi Iwaid4156932013-01-07 10:08:02 +01005599 for (i = 0; i < spec->autocfg.line_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01005600 set_output_and_unmute(codec, spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005601}
5602
Takashi Iwaidb23fd12012-12-20 15:27:24 +01005603
Takashi Iwai2c12c302013-01-10 09:33:29 +01005604static void __init_extra_out(struct hda_codec *codec, int num_outs, int *paths)
Takashi Iwai352f7f92012-12-19 12:52:06 +01005605{
Takashi Iwai352f7f92012-12-19 12:52:06 +01005606 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005607
Takashi Iwaid4156932013-01-07 10:08:02 +01005608 for (i = 0; i < num_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01005609 set_output_and_unmute(codec, paths[i]);
Takashi Iwaidb23fd12012-12-20 15:27:24 +01005610}
5611
5612/* initialize hp and speaker paths */
5613static void init_extra_out(struct hda_codec *codec)
5614{
5615 struct hda_gen_spec *spec = codec->spec;
5616
5617 if (spec->autocfg.line_out_type != AUTO_PIN_HP_OUT)
Takashi Iwai2c12c302013-01-10 09:33:29 +01005618 __init_extra_out(codec, spec->autocfg.hp_outs, spec->hp_paths);
Takashi Iwaidb23fd12012-12-20 15:27:24 +01005619 if (spec->autocfg.line_out_type != AUTO_PIN_SPEAKER_OUT)
5620 __init_extra_out(codec, spec->autocfg.speaker_outs,
Takashi Iwai2c12c302013-01-10 09:33:29 +01005621 spec->speaker_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005622}
5623
5624/* initialize multi-io paths */
5625static void init_multi_io(struct hda_codec *codec)
5626{
5627 struct hda_gen_spec *spec = codec->spec;
5628 int i;
5629
5630 for (i = 0; i < spec->multi_ios; i++) {
5631 hda_nid_t pin = spec->multi_io[i].pin;
5632 struct nid_path *path;
Takashi Iwai196c17662013-01-04 15:01:40 +01005633 path = get_multiio_path(codec, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005634 if (!path)
5635 continue;
5636 if (!spec->multi_io[i].ctl_in)
5637 spec->multi_io[i].ctl_in =
Takashi Iwai2c12c302013-01-10 09:33:29 +01005638 snd_hda_codec_get_pin_target(codec, pin);
Takashi Iwai65033cc2013-04-16 12:31:05 +02005639 snd_hda_activate_path(codec, path, path->active,
5640 aamix_default(spec));
Takashi Iwai352f7f92012-12-19 12:52:06 +01005641 }
5642}
5643
Takashi Iwai4f7f67f2013-12-03 10:51:37 +01005644static void init_aamix_paths(struct hda_codec *codec)
5645{
5646 struct hda_gen_spec *spec = codec->spec;
5647
5648 if (!spec->have_aamix_ctl)
5649 return;
5650 update_aamix_paths(codec, spec->aamix_mode, spec->out_paths[0],
5651 spec->aamix_out_paths[0],
5652 spec->autocfg.line_out_type);
5653 update_aamix_paths(codec, spec->aamix_mode, spec->hp_paths[0],
5654 spec->aamix_out_paths[1],
5655 AUTO_PIN_HP_OUT);
5656 update_aamix_paths(codec, spec->aamix_mode, spec->speaker_paths[0],
5657 spec->aamix_out_paths[2],
5658 AUTO_PIN_SPEAKER_OUT);
5659}
5660
Takashi Iwai352f7f92012-12-19 12:52:06 +01005661/* set up input pins and loopback paths */
5662static void init_analog_input(struct hda_codec *codec)
5663{
5664 struct hda_gen_spec *spec = codec->spec;
5665 struct auto_pin_cfg *cfg = &spec->autocfg;
5666 int i;
5667
5668 for (i = 0; i < cfg->num_inputs; i++) {
5669 hda_nid_t nid = cfg->inputs[i].pin;
5670 if (is_input_pin(codec, nid))
Takashi Iwai2c12c302013-01-10 09:33:29 +01005671 restore_pin_ctl(codec, nid);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005672
5673 /* init loopback inputs */
5674 if (spec->mixer_nid) {
Takashi Iwai3e367f12013-01-23 17:07:23 +01005675 resume_path_from_idx(codec, spec->loopback_paths[i]);
5676 resume_path_from_idx(codec, spec->loopback_merge_path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005677 }
5678 }
5679}
5680
5681/* initialize ADC paths */
5682static void init_input_src(struct hda_codec *codec)
5683{
5684 struct hda_gen_spec *spec = codec->spec;
5685 struct hda_input_mux *imux = &spec->input_mux;
5686 struct nid_path *path;
5687 int i, c, nums;
5688
5689 if (spec->dyn_adc_switch)
5690 nums = 1;
5691 else
5692 nums = spec->num_adc_nids;
5693
5694 for (c = 0; c < nums; c++) {
5695 for (i = 0; i < imux->num_items; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01005696 path = get_input_path(codec, c, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005697 if (path) {
5698 bool active = path->active;
5699 if (i == spec->cur_mux[c])
5700 active = true;
5701 snd_hda_activate_path(codec, path, active, false);
5702 }
5703 }
Takashi Iwai967303d2013-02-19 17:12:42 +01005704 if (spec->hp_mic)
5705 update_hp_mic(codec, c, true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005706 }
5707
Takashi Iwai352f7f92012-12-19 12:52:06 +01005708 if (spec->cap_sync_hook)
Takashi Iwai7fe30712014-01-30 17:59:02 +01005709 spec->cap_sync_hook(codec, NULL, NULL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005710}
5711
5712/* set right pin controls for digital I/O */
5713static void init_digital(struct hda_codec *codec)
5714{
5715 struct hda_gen_spec *spec = codec->spec;
5716 int i;
5717 hda_nid_t pin;
5718
Takashi Iwaid4156932013-01-07 10:08:02 +01005719 for (i = 0; i < spec->autocfg.dig_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01005720 set_output_and_unmute(codec, spec->digout_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005721 pin = spec->autocfg.dig_in_pin;
Takashi Iwai2430d7b2013-01-04 15:09:42 +01005722 if (pin) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01005723 restore_pin_ctl(codec, pin);
Takashi Iwai3e367f12013-01-23 17:07:23 +01005724 resume_path_from_idx(codec, spec->digin_path);
Takashi Iwai2430d7b2013-01-04 15:09:42 +01005725 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01005726}
5727
Takashi Iwai973e4972012-12-20 15:16:09 +01005728/* clear unsol-event tags on unused pins; Conexant codecs seem to leave
5729 * invalid unsol tags by some reason
5730 */
5731static void clear_unsol_on_unused_pins(struct hda_codec *codec)
5732{
5733 int i;
5734
5735 for (i = 0; i < codec->init_pins.used; i++) {
5736 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
5737 hda_nid_t nid = pin->nid;
5738 if (is_jack_detectable(codec, nid) &&
5739 !snd_hda_jack_tbl_get(codec, nid))
5740 snd_hda_codec_update_cache(codec, nid, 0,
5741 AC_VERB_SET_UNSOLICITED_ENABLE, 0);
5742 }
5743}
5744
Takashi Iwaidda42bd2014-10-30 12:04:50 +01005745/**
5746 * snd_hda_gen_init - initialize the generic spec
5747 * @codec: the HDA codec
5748 *
5749 * This can be put as patch_ops init function.
Takashi Iwai5187ac12013-01-07 12:52:16 +01005750 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01005751int snd_hda_gen_init(struct hda_codec *codec)
5752{
5753 struct hda_gen_spec *spec = codec->spec;
5754
5755 if (spec->init_hook)
5756 spec->init_hook(codec);
5757
5758 snd_hda_apply_verbs(codec);
5759
5760 init_multi_out(codec);
5761 init_extra_out(codec);
5762 init_multi_io(codec);
Takashi Iwai4f7f67f2013-12-03 10:51:37 +01005763 init_aamix_paths(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005764 init_analog_input(codec);
5765 init_input_src(codec);
5766 init_digital(codec);
5767
Takashi Iwai973e4972012-12-20 15:16:09 +01005768 clear_unsol_on_unused_pins(codec);
5769
Takashi Iwaie6feb5d2015-03-16 21:32:11 +01005770 sync_all_pin_power_ctls(codec);
5771
Takashi Iwai352f7f92012-12-19 12:52:06 +01005772 /* call init functions of standard auto-mute helpers */
Takashi Iwaia5cc2502013-01-16 18:08:55 +01005773 update_automute_all(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005774
Takashi Iwaia551d912015-02-26 12:34:49 +01005775 regcache_sync(codec->core.regmap);
Takashi Iwai3bbcd272012-12-20 11:50:58 +01005776
Takashi Iwai352f7f92012-12-19 12:52:06 +01005777 if (spec->vmaster_mute.sw_kctl && spec->vmaster_mute.hook)
5778 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
5779
5780 hda_call_check_power_status(codec, 0x01);
5781 return 0;
5782}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005783EXPORT_SYMBOL_GPL(snd_hda_gen_init);
Takashi Iwaifce52a32013-01-07 12:42:48 +01005784
Takashi Iwaidda42bd2014-10-30 12:04:50 +01005785/**
5786 * snd_hda_gen_free - free the generic spec
5787 * @codec: the HDA codec
5788 *
5789 * This can be put as patch_ops free function.
Takashi Iwai5187ac12013-01-07 12:52:16 +01005790 */
Takashi Iwaifce52a32013-01-07 12:42:48 +01005791void snd_hda_gen_free(struct hda_codec *codec)
5792{
Takashi Iwai8a02c0c2014-02-10 18:09:45 +01005793 snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_FREE);
Takashi Iwaifce52a32013-01-07 12:42:48 +01005794 snd_hda_gen_spec_free(codec->spec);
5795 kfree(codec->spec);
5796 codec->spec = NULL;
5797}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005798EXPORT_SYMBOL_GPL(snd_hda_gen_free);
Takashi Iwaifce52a32013-01-07 12:42:48 +01005799
5800#ifdef CONFIG_PM
Takashi Iwaidda42bd2014-10-30 12:04:50 +01005801/**
5802 * snd_hda_gen_check_power_status - check the loopback power save state
5803 * @codec: the HDA codec
5804 * @nid: NID to inspect
5805 *
5806 * This can be put as patch_ops check_power_status function.
Takashi Iwai5187ac12013-01-07 12:52:16 +01005807 */
Takashi Iwaifce52a32013-01-07 12:42:48 +01005808int snd_hda_gen_check_power_status(struct hda_codec *codec, hda_nid_t nid)
5809{
5810 struct hda_gen_spec *spec = codec->spec;
5811 return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
5812}
Takashi Iwai2698ea92013-12-18 07:45:52 +01005813EXPORT_SYMBOL_GPL(snd_hda_gen_check_power_status);
Takashi Iwaifce52a32013-01-07 12:42:48 +01005814#endif
Takashi Iwai352f7f92012-12-19 12:52:06 +01005815
5816
5817/*
5818 * the generic codec support
5819 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005820
Takashi Iwai352f7f92012-12-19 12:52:06 +01005821static const struct hda_codec_ops generic_patch_ops = {
5822 .build_controls = snd_hda_gen_build_controls,
5823 .build_pcms = snd_hda_gen_build_pcms,
5824 .init = snd_hda_gen_init,
Takashi Iwaifce52a32013-01-07 12:42:48 +01005825 .free = snd_hda_gen_free,
Takashi Iwai352f7f92012-12-19 12:52:06 +01005826 .unsol_event = snd_hda_jack_unsol_event,
Takashi Iwai83012a72012-08-24 18:38:08 +02005827#ifdef CONFIG_PM
Takashi Iwaifce52a32013-01-07 12:42:48 +01005828 .check_power_status = snd_hda_gen_check_power_status,
Takashi Iwaicb53c622007-08-10 17:21:45 +02005829#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005830};
5831
Takashi Iwaid8a766a2015-02-17 15:25:37 +01005832/*
Takashi Iwaidda42bd2014-10-30 12:04:50 +01005833 * snd_hda_parse_generic_codec - Generic codec parser
5834 * @codec: the HDA codec
Takashi Iwaidda42bd2014-10-30 12:04:50 +01005835 */
Takashi Iwaid8a766a2015-02-17 15:25:37 +01005836static int snd_hda_parse_generic_codec(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005837{
Takashi Iwai352f7f92012-12-19 12:52:06 +01005838 struct hda_gen_spec *spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005839 int err;
5840
Takashi Iwaie560d8d2005-09-09 14:21:46 +02005841 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005842 if (!spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005843 return -ENOMEM;
Takashi Iwai352f7f92012-12-19 12:52:06 +01005844 snd_hda_gen_spec_init(spec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005845 codec->spec = spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005846
Takashi Iwai9eb413e2012-12-19 14:41:21 +01005847 err = snd_hda_parse_pin_defcfg(codec, &spec->autocfg, NULL, 0);
5848 if (err < 0)
5849 return err;
5850
5851 err = snd_hda_gen_parse_auto_config(codec, &spec->autocfg);
Takashi Iwai352f7f92012-12-19 12:52:06 +01005852 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005853 goto error;
5854
5855 codec->patch_ops = generic_patch_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005856 return 0;
5857
Takashi Iwai352f7f92012-12-19 12:52:06 +01005858error:
Takashi Iwaifce52a32013-01-07 12:42:48 +01005859 snd_hda_gen_free(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005860 return err;
5861}
Takashi Iwaid8a766a2015-02-17 15:25:37 +01005862
Takashi Iwaib9a94a92015-10-01 16:20:04 +02005863static const struct hda_device_id snd_hda_id_generic[] = {
5864 HDA_CODEC_ENTRY(HDA_CODEC_ID_GENERIC, "Generic", snd_hda_parse_generic_codec),
Takashi Iwaid8a766a2015-02-17 15:25:37 +01005865 {} /* terminator */
5866};
Takashi Iwaib9a94a92015-10-01 16:20:04 +02005867MODULE_DEVICE_TABLE(hdaudio, snd_hda_id_generic);
Takashi Iwaid8a766a2015-02-17 15:25:37 +01005868
5869static struct hda_codec_driver generic_driver = {
Takashi Iwaib9a94a92015-10-01 16:20:04 +02005870 .id = snd_hda_id_generic,
Takashi Iwaid8a766a2015-02-17 15:25:37 +01005871};
5872
5873module_hda_codec_driver(generic_driver);
Takashi Iwaib21bdd02013-11-18 12:03:56 +01005874
5875MODULE_LICENSE("GPL");
5876MODULE_DESCRIPTION("Generic HD-audio codec parser");