blob: 55b7897444a0d575c22067c0066b09abe25db8dd [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 Iwaif873e532012-12-20 16:58:39 +010027#include <linux/ctype.h>
28#include <linux/string.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <sound/core.h>
Takashi Iwai352f7f92012-12-19 12:52:06 +010030#include <sound/jack.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include "hda_codec.h"
32#include "hda_local.h"
Takashi Iwai352f7f92012-12-19 12:52:06 +010033#include "hda_auto_parser.h"
34#include "hda_jack.h"
35#include "hda_generic.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Takashi Iwai352f7f92012-12-19 12:52:06 +010038/* initialize hda_gen_spec struct */
39int snd_hda_gen_spec_init(struct hda_gen_spec *spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -070040{
Takashi Iwai352f7f92012-12-19 12:52:06 +010041 snd_array_init(&spec->kctls, sizeof(struct snd_kcontrol_new), 32);
Takashi Iwai352f7f92012-12-19 12:52:06 +010042 snd_array_init(&spec->paths, sizeof(struct nid_path), 8);
Takashi Iwai38cf6f12012-12-21 14:09:42 +010043 mutex_init(&spec->pcm_mutex);
Takashi Iwai352f7f92012-12-19 12:52:06 +010044 return 0;
45}
46EXPORT_SYMBOL_HDA(snd_hda_gen_spec_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Takashi Iwai12c93df2012-12-19 14:38:33 +010048struct snd_kcontrol_new *
49snd_hda_gen_add_kctl(struct hda_gen_spec *spec, const char *name,
50 const struct snd_kcontrol_new *temp)
Takashi Iwai352f7f92012-12-19 12:52:06 +010051{
52 struct snd_kcontrol_new *knew = snd_array_new(&spec->kctls);
53 if (!knew)
54 return NULL;
55 *knew = *temp;
56 if (name)
57 knew->name = kstrdup(name, GFP_KERNEL);
58 else if (knew->name)
59 knew->name = kstrdup(knew->name, GFP_KERNEL);
60 if (!knew->name)
61 return NULL;
62 return knew;
63}
Takashi Iwai12c93df2012-12-19 14:38:33 +010064EXPORT_SYMBOL_HDA(snd_hda_gen_add_kctl);
Takashi Iwai352f7f92012-12-19 12:52:06 +010065
66static void free_kctls(struct hda_gen_spec *spec)
67{
68 if (spec->kctls.list) {
69 struct snd_kcontrol_new *kctl = spec->kctls.list;
70 int i;
71 for (i = 0; i < spec->kctls.used; i++)
72 kfree(kctl[i].name);
73 }
74 snd_array_free(&spec->kctls);
75}
76
Takashi Iwai352f7f92012-12-19 12:52:06 +010077void snd_hda_gen_spec_free(struct hda_gen_spec *spec)
78{
79 if (!spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 return;
Takashi Iwai352f7f92012-12-19 12:52:06 +010081 free_kctls(spec);
Takashi Iwai352f7f92012-12-19 12:52:06 +010082 snd_array_free(&spec->paths);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083}
Takashi Iwai352f7f92012-12-19 12:52:06 +010084EXPORT_SYMBOL_HDA(snd_hda_gen_spec_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
86/*
Takashi Iwai2c12c302013-01-10 09:33:29 +010087 * pin control value accesses
88 */
89
90#define update_pin_ctl(codec, pin, val) \
91 snd_hda_codec_update_cache(codec, pin, 0, \
92 AC_VERB_SET_PIN_WIDGET_CONTROL, val)
93
94/* restore the pinctl based on the cached value */
95static inline void restore_pin_ctl(struct hda_codec *codec, hda_nid_t pin)
96{
97 update_pin_ctl(codec, pin, snd_hda_codec_get_pin_target(codec, pin));
98}
99
100/* set the pinctl target value and write it if requested */
101static void set_pin_target(struct hda_codec *codec, hda_nid_t pin,
102 unsigned int val, bool do_write)
103{
104 if (!pin)
105 return;
106 val = snd_hda_correct_pin_ctl(codec, pin, val);
107 snd_hda_codec_set_pin_target(codec, pin, val);
108 if (do_write)
109 update_pin_ctl(codec, pin, val);
110}
111
112/* set pinctl target values for all given pins */
113static void set_pin_targets(struct hda_codec *codec, int num_pins,
114 hda_nid_t *pins, unsigned int val)
115{
116 int i;
117 for (i = 0; i < num_pins; i++)
118 set_pin_target(codec, pins[i], val, false);
119}
120
121/*
Takashi Iwai352f7f92012-12-19 12:52:06 +0100122 * parsing paths
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100125/* return the position of NID in the list, or -1 if not found */
126static int find_idx_in_nid_list(hda_nid_t nid, const hda_nid_t *list, int nums)
127{
128 int i;
129 for (i = 0; i < nums; i++)
130 if (list[i] == nid)
131 return i;
132 return -1;
133}
134
135/* return true if the given NID is contained in the path */
136static bool is_nid_contained(struct nid_path *path, hda_nid_t nid)
137{
138 return find_idx_in_nid_list(nid, path->path, path->depth) >= 0;
139}
140
Takashi Iwaif5172a72013-01-04 13:19:55 +0100141static struct nid_path *get_nid_path(struct hda_codec *codec,
142 hda_nid_t from_nid, hda_nid_t to_nid,
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100143 int anchor_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100145 struct hda_gen_spec *spec = codec->spec;
146 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
Takashi Iwai352f7f92012-12-19 12:52:06 +0100148 for (i = 0; i < spec->paths.used; i++) {
149 struct nid_path *path = snd_array_elem(&spec->paths, i);
150 if (path->depth <= 0)
151 continue;
152 if ((!from_nid || path->path[0] == from_nid) &&
Takashi Iwaif5172a72013-01-04 13:19:55 +0100153 (!to_nid || path->path[path->depth - 1] == to_nid)) {
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100154 if (!anchor_nid ||
155 (anchor_nid > 0 && is_nid_contained(path, anchor_nid)) ||
156 (anchor_nid < 0 && !is_nid_contained(path, anchor_nid)))
Takashi Iwaif5172a72013-01-04 13:19:55 +0100157 return path;
158 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 }
160 return NULL;
161}
Takashi Iwaif5172a72013-01-04 13:19:55 +0100162
163/* get the path between the given NIDs;
164 * passing 0 to either @pin or @dac behaves as a wildcard
165 */
166struct nid_path *snd_hda_get_nid_path(struct hda_codec *codec,
167 hda_nid_t from_nid, hda_nid_t to_nid)
168{
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100169 return get_nid_path(codec, from_nid, to_nid, 0);
Takashi Iwaif5172a72013-01-04 13:19:55 +0100170}
Takashi Iwai352f7f92012-12-19 12:52:06 +0100171EXPORT_SYMBOL_HDA(snd_hda_get_nid_path);
172
Takashi Iwai196c17662013-01-04 15:01:40 +0100173/* get the index number corresponding to the path instance;
174 * the index starts from 1, for easier checking the invalid value
175 */
176int snd_hda_get_path_idx(struct hda_codec *codec, struct nid_path *path)
177{
178 struct hda_gen_spec *spec = codec->spec;
179 struct nid_path *array = spec->paths.list;
180 ssize_t idx;
181
182 if (!spec->paths.used)
183 return 0;
184 idx = path - array;
185 if (idx < 0 || idx >= spec->paths.used)
186 return 0;
187 return idx + 1;
188}
189
190/* get the path instance corresponding to the given index number */
191struct nid_path *snd_hda_get_path_from_idx(struct hda_codec *codec, int idx)
192{
193 struct hda_gen_spec *spec = codec->spec;
194
195 if (idx <= 0 || idx > spec->paths.used)
196 return NULL;
197 return snd_array_elem(&spec->paths, idx - 1);
198}
199
Takashi Iwai352f7f92012-12-19 12:52:06 +0100200/* check whether the given DAC is already found in any existing paths */
201static bool is_dac_already_used(struct hda_codec *codec, hda_nid_t nid)
202{
203 struct hda_gen_spec *spec = codec->spec;
204 int i;
205
206 for (i = 0; i < spec->paths.used; i++) {
207 struct nid_path *path = snd_array_elem(&spec->paths, i);
208 if (path->path[0] == nid)
209 return true;
210 }
211 return false;
212}
213
214/* check whether the given two widgets can be connected */
215static bool is_reachable_path(struct hda_codec *codec,
216 hda_nid_t from_nid, hda_nid_t to_nid)
217{
218 if (!from_nid || !to_nid)
219 return false;
220 return snd_hda_get_conn_index(codec, to_nid, from_nid, true) >= 0;
221}
222
223/* nid, dir and idx */
224#define AMP_VAL_COMPARE_MASK (0xffff | (1U << 18) | (0x0f << 19))
225
226/* check whether the given ctl is already assigned in any path elements */
227static bool is_ctl_used(struct hda_codec *codec, unsigned int val, int type)
228{
229 struct hda_gen_spec *spec = codec->spec;
230 int i;
231
232 val &= AMP_VAL_COMPARE_MASK;
233 for (i = 0; i < spec->paths.used; i++) {
234 struct nid_path *path = snd_array_elem(&spec->paths, i);
235 if ((path->ctls[type] & AMP_VAL_COMPARE_MASK) == val)
236 return true;
237 }
238 return false;
239}
240
241/* check whether a control with the given (nid, dir, idx) was assigned */
242static bool is_ctl_associated(struct hda_codec *codec, hda_nid_t nid,
243 int dir, int idx)
244{
245 unsigned int val = HDA_COMPOSE_AMP_VAL(nid, 3, idx, dir);
246 return is_ctl_used(codec, val, NID_PATH_VOL_CTL) ||
247 is_ctl_used(codec, val, NID_PATH_MUTE_CTL);
248}
249
Takashi Iwai0c8c0f52012-12-20 17:54:22 +0100250static void print_nid_path(const char *pfx, struct nid_path *path)
251{
252 char buf[40];
253 int i;
254
255
256 buf[0] = 0;
257 for (i = 0; i < path->depth; i++) {
258 char tmp[4];
259 sprintf(tmp, ":%02x", path->path[i]);
260 strlcat(buf, tmp, sizeof(buf));
261 }
262 snd_printdd("%s path: depth=%d %s\n", pfx, path->depth, buf);
263}
264
Takashi Iwai352f7f92012-12-19 12:52:06 +0100265/* called recursively */
266static bool __parse_nid_path(struct hda_codec *codec,
267 hda_nid_t from_nid, hda_nid_t to_nid,
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100268 int anchor_nid, struct nid_path *path,
269 int depth)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100270{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100271 const hda_nid_t *conn;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100272 int i, nums;
273
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100274 if (to_nid == anchor_nid)
275 anchor_nid = 0; /* anchor passed */
276 else if (to_nid == (hda_nid_t)(-anchor_nid))
277 return false; /* hit the exclusive nid */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100278
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100279 nums = snd_hda_get_conn_list(codec, to_nid, &conn);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100280 for (i = 0; i < nums; i++) {
281 if (conn[i] != from_nid) {
282 /* special case: when from_nid is 0,
283 * try to find an empty DAC
284 */
285 if (from_nid ||
286 get_wcaps_type(get_wcaps(codec, conn[i])) != AC_WID_AUD_OUT ||
287 is_dac_already_used(codec, conn[i]))
288 continue;
289 }
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100290 /* anchor is not requested or already passed? */
291 if (anchor_nid <= 0)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100292 goto found;
293 }
294 if (depth >= MAX_NID_PATH_DEPTH)
295 return false;
296 for (i = 0; i < nums; i++) {
297 unsigned int type;
298 type = get_wcaps_type(get_wcaps(codec, conn[i]));
299 if (type == AC_WID_AUD_OUT || type == AC_WID_AUD_IN ||
300 type == AC_WID_PIN)
301 continue;
302 if (__parse_nid_path(codec, from_nid, conn[i],
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100303 anchor_nid, path, depth + 1))
Takashi Iwai352f7f92012-12-19 12:52:06 +0100304 goto found;
305 }
306 return false;
307
308 found:
309 path->path[path->depth] = conn[i];
310 path->idx[path->depth + 1] = i;
311 if (nums > 1 && get_wcaps_type(get_wcaps(codec, to_nid)) != AC_WID_AUD_MIX)
312 path->multi[path->depth + 1] = 1;
313 path->depth++;
314 return true;
315}
316
317/* parse the widget path from the given nid to the target nid;
318 * when @from_nid is 0, try to find an empty DAC;
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100319 * when @anchor_nid is set to a positive value, only paths through the widget
320 * with the given value are evaluated.
321 * when @anchor_nid is set to a negative value, paths through the widget
322 * with the negative of given value are excluded, only other paths are chosen.
323 * when @anchor_nid is zero, no special handling about path selection.
Takashi Iwai352f7f92012-12-19 12:52:06 +0100324 */
325bool snd_hda_parse_nid_path(struct hda_codec *codec, hda_nid_t from_nid,
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100326 hda_nid_t to_nid, int anchor_nid,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100327 struct nid_path *path)
328{
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100329 if (__parse_nid_path(codec, from_nid, to_nid, anchor_nid, path, 1)) {
Takashi Iwai352f7f92012-12-19 12:52:06 +0100330 path->path[path->depth] = to_nid;
331 path->depth++;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100332 return true;
333 }
334 return false;
335}
336EXPORT_SYMBOL_HDA(snd_hda_parse_nid_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
338/*
Takashi Iwai352f7f92012-12-19 12:52:06 +0100339 * parse the path between the given NIDs and add to the path list.
340 * if no valid path is found, return NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100342struct nid_path *
343snd_hda_add_new_path(struct hda_codec *codec, hda_nid_t from_nid,
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100344 hda_nid_t to_nid, int anchor_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100346 struct hda_gen_spec *spec = codec->spec;
347 struct nid_path *path;
348
349 if (from_nid && to_nid && !is_reachable_path(codec, from_nid, to_nid))
350 return NULL;
351
Takashi Iwaif5172a72013-01-04 13:19:55 +0100352 /* check whether the path has been already added */
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100353 path = get_nid_path(codec, from_nid, to_nid, anchor_nid);
Takashi Iwaif5172a72013-01-04 13:19:55 +0100354 if (path)
355 return path;
356
Takashi Iwai352f7f92012-12-19 12:52:06 +0100357 path = snd_array_new(&spec->paths);
358 if (!path)
359 return NULL;
360 memset(path, 0, sizeof(*path));
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100361 if (snd_hda_parse_nid_path(codec, from_nid, to_nid, anchor_nid, path))
Takashi Iwai352f7f92012-12-19 12:52:06 +0100362 return path;
363 /* push back */
364 spec->paths.used--;
365 return NULL;
366}
367EXPORT_SYMBOL_HDA(snd_hda_add_new_path);
368
Takashi Iwai980428c2013-01-09 09:28:20 +0100369/* clear the given path as invalid so that it won't be picked up later */
370static void invalidate_nid_path(struct hda_codec *codec, int idx)
371{
372 struct nid_path *path = snd_hda_get_path_from_idx(codec, idx);
373 if (!path)
374 return;
375 memset(path, 0, sizeof(*path));
376}
377
Takashi Iwai352f7f92012-12-19 12:52:06 +0100378/* look for an empty DAC slot */
379static hda_nid_t look_for_dac(struct hda_codec *codec, hda_nid_t pin,
380 bool is_digital)
381{
382 struct hda_gen_spec *spec = codec->spec;
383 bool cap_digital;
384 int i;
385
386 for (i = 0; i < spec->num_all_dacs; i++) {
387 hda_nid_t nid = spec->all_dacs[i];
388 if (!nid || is_dac_already_used(codec, nid))
389 continue;
390 cap_digital = !!(get_wcaps(codec, nid) & AC_WCAP_DIGITAL);
391 if (is_digital != cap_digital)
392 continue;
393 if (is_reachable_path(codec, nid, pin))
394 return nid;
395 }
396 return 0;
397}
398
399/* replace the channels in the composed amp value with the given number */
400static unsigned int amp_val_replace_channels(unsigned int val, unsigned int chs)
401{
402 val &= ~(0x3U << 16);
403 val |= chs << 16;
404 return val;
405}
406
407/* check whether the widget has the given amp capability for the direction */
408static bool check_amp_caps(struct hda_codec *codec, hda_nid_t nid,
409 int dir, unsigned int bits)
410{
411 if (!nid)
412 return false;
413 if (get_wcaps(codec, nid) & (1 << (dir + 1)))
414 if (query_amp_caps(codec, nid, dir) & bits)
415 return true;
416 return false;
417}
418
419#define nid_has_mute(codec, nid, dir) \
420 check_amp_caps(codec, nid, dir, AC_AMPCAP_MUTE)
421#define nid_has_volume(codec, nid, dir) \
422 check_amp_caps(codec, nid, dir, AC_AMPCAP_NUM_STEPS)
423
424/* look for a widget suitable for assigning a mute switch in the path */
425static hda_nid_t look_for_out_mute_nid(struct hda_codec *codec,
426 struct nid_path *path)
427{
428 int i;
429
430 for (i = path->depth - 1; i >= 0; i--) {
431 if (nid_has_mute(codec, path->path[i], HDA_OUTPUT))
432 return path->path[i];
433 if (i != path->depth - 1 && i != 0 &&
434 nid_has_mute(codec, path->path[i], HDA_INPUT))
435 return path->path[i];
436 }
437 return 0;
438}
439
440/* look for a widget suitable for assigning a volume ctl in the path */
441static hda_nid_t look_for_out_vol_nid(struct hda_codec *codec,
442 struct nid_path *path)
443{
444 int i;
445
446 for (i = path->depth - 1; i >= 0; i--) {
447 if (nid_has_volume(codec, path->path[i], HDA_OUTPUT))
448 return path->path[i];
449 }
Takashi Iwai82beb8f2007-08-10 17:09:26 +0200450 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451}
452
453/*
Takashi Iwai352f7f92012-12-19 12:52:06 +0100454 * path activation / deactivation
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100456
457/* can have the amp-in capability? */
458static bool has_amp_in(struct hda_codec *codec, struct nid_path *path, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100460 hda_nid_t nid = path->path[idx];
461 unsigned int caps = get_wcaps(codec, nid);
462 unsigned int type = get_wcaps_type(caps);
463
464 if (!(caps & AC_WCAP_IN_AMP))
465 return false;
466 if (type == AC_WID_PIN && idx > 0) /* only for input pins */
467 return false;
468 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469}
470
Takashi Iwai352f7f92012-12-19 12:52:06 +0100471/* can have the amp-out capability? */
472static bool has_amp_out(struct hda_codec *codec, struct nid_path *path, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100474 hda_nid_t nid = path->path[idx];
475 unsigned int caps = get_wcaps(codec, nid);
476 unsigned int type = get_wcaps_type(caps);
477
478 if (!(caps & AC_WCAP_OUT_AMP))
479 return false;
480 if (type == AC_WID_PIN && !idx) /* only for output pins */
481 return false;
482 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483}
484
Takashi Iwai352f7f92012-12-19 12:52:06 +0100485/* check whether the given (nid,dir,idx) is active */
486static bool is_active_nid(struct hda_codec *codec, hda_nid_t nid,
487 unsigned int idx, unsigned int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100489 struct hda_gen_spec *spec = codec->spec;
490 int i, n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
Takashi Iwai352f7f92012-12-19 12:52:06 +0100492 for (n = 0; n < spec->paths.used; n++) {
493 struct nid_path *path = snd_array_elem(&spec->paths, n);
494 if (!path->active)
495 continue;
496 for (i = 0; i < path->depth; i++) {
497 if (path->path[i] == nid) {
498 if (dir == HDA_OUTPUT || path->idx[i] == idx)
499 return true;
500 break;
501 }
502 }
503 }
504 return false;
505}
506
507/* get the default amp value for the target state */
508static int get_amp_val_to_activate(struct hda_codec *codec, hda_nid_t nid,
509 int dir, bool enable)
510{
511 unsigned int caps;
512 unsigned int val = 0;
513
514 caps = query_amp_caps(codec, nid, dir);
515 if (caps & AC_AMPCAP_NUM_STEPS) {
516 /* set to 0dB */
517 if (enable)
518 val = (caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT;
519 }
520 if (caps & AC_AMPCAP_MUTE) {
521 if (!enable)
522 val |= HDA_AMP_MUTE;
523 }
524 return val;
525}
526
527/* initialize the amp value (only at the first time) */
528static void init_amp(struct hda_codec *codec, hda_nid_t nid, int dir, int idx)
529{
530 int val = get_amp_val_to_activate(codec, nid, dir, false);
531 snd_hda_codec_amp_init_stereo(codec, nid, dir, idx, 0xff, val);
532}
533
534static void activate_amp(struct hda_codec *codec, hda_nid_t nid, int dir,
535 int idx, bool enable)
536{
537 int val;
538 if (is_ctl_associated(codec, nid, dir, idx) ||
Takashi Iwai985803c2013-01-03 16:30:04 +0100539 (!enable && is_active_nid(codec, nid, dir, idx)))
Takashi Iwai352f7f92012-12-19 12:52:06 +0100540 return;
541 val = get_amp_val_to_activate(codec, nid, dir, enable);
542 snd_hda_codec_amp_stereo(codec, nid, dir, idx, 0xff, val);
543}
544
545static void activate_amp_out(struct hda_codec *codec, struct nid_path *path,
546 int i, bool enable)
547{
548 hda_nid_t nid = path->path[i];
549 init_amp(codec, nid, HDA_OUTPUT, 0);
550 activate_amp(codec, nid, HDA_OUTPUT, 0, enable);
551}
552
553static void activate_amp_in(struct hda_codec *codec, struct nid_path *path,
554 int i, bool enable, bool add_aamix)
555{
556 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100557 const hda_nid_t *conn;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100558 int n, nums, idx;
559 int type;
560 hda_nid_t nid = path->path[i];
561
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100562 nums = snd_hda_get_conn_list(codec, nid, &conn);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100563 type = get_wcaps_type(get_wcaps(codec, nid));
564 if (type == AC_WID_PIN ||
565 (type == AC_WID_AUD_IN && codec->single_adc_amp)) {
566 nums = 1;
567 idx = 0;
568 } else
569 idx = path->idx[i];
570
571 for (n = 0; n < nums; n++)
572 init_amp(codec, nid, HDA_INPUT, n);
573
574 if (is_ctl_associated(codec, nid, HDA_INPUT, idx))
575 return;
576
577 /* here is a little bit tricky in comparison with activate_amp_out();
578 * when aa-mixer is available, we need to enable the path as well
579 */
580 for (n = 0; n < nums; n++) {
581 if (n != idx && (!add_aamix || conn[n] != spec->mixer_nid))
582 continue;
583 activate_amp(codec, nid, HDA_INPUT, n, enable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 }
585}
586
Takashi Iwai352f7f92012-12-19 12:52:06 +0100587/* activate or deactivate the given path
588 * if @add_aamix is set, enable the input from aa-mix NID as well (if any)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100590void snd_hda_activate_path(struct hda_codec *codec, struct nid_path *path,
591 bool enable, bool add_aamix)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100593 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594
Takashi Iwai352f7f92012-12-19 12:52:06 +0100595 if (!enable)
596 path->active = false;
597
598 for (i = path->depth - 1; i >= 0; i--) {
599 if (enable && path->multi[i])
600 snd_hda_codec_write_cache(codec, path->path[i], 0,
601 AC_VERB_SET_CONNECT_SEL,
602 path->idx[i]);
603 if (has_amp_in(codec, path, i))
604 activate_amp_in(codec, path, i, enable, add_aamix);
605 if (has_amp_out(codec, path, i))
606 activate_amp_out(codec, path, i, enable);
607 }
608
609 if (enable)
610 path->active = true;
611}
612EXPORT_SYMBOL_HDA(snd_hda_activate_path);
613
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +0100614/* turn on/off EAPD on the given pin */
615static void set_pin_eapd(struct hda_codec *codec, hda_nid_t pin, bool enable)
616{
617 struct hda_gen_spec *spec = codec->spec;
618 if (spec->own_eapd_ctl ||
619 !(snd_hda_query_pin_caps(codec, pin) & AC_PINCAP_EAPD))
620 return;
Takashi Iwaiecac3ed2012-12-21 15:23:01 +0100621 if (codec->inv_eapd)
622 enable = !enable;
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +0100623 snd_hda_codec_update_cache(codec, pin, 0,
624 AC_VERB_SET_EAPD_BTLENABLE,
625 enable ? 0x02 : 0x00);
626}
627
Takashi Iwai352f7f92012-12-19 12:52:06 +0100628
629/*
630 * Helper functions for creating mixer ctl elements
631 */
632
633enum {
634 HDA_CTL_WIDGET_VOL,
635 HDA_CTL_WIDGET_MUTE,
636 HDA_CTL_BIND_MUTE,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100637};
638static const struct snd_kcontrol_new control_templates[] = {
639 HDA_CODEC_VOLUME(NULL, 0, 0, 0),
640 HDA_CODEC_MUTE(NULL, 0, 0, 0),
641 HDA_BIND_MUTE(NULL, 0, 0, 0),
Takashi Iwai352f7f92012-12-19 12:52:06 +0100642};
643
644/* add dynamic controls from template */
645static int add_control(struct hda_gen_spec *spec, int type, const char *name,
646 int cidx, unsigned long val)
647{
648 struct snd_kcontrol_new *knew;
649
Takashi Iwai12c93df2012-12-19 14:38:33 +0100650 knew = snd_hda_gen_add_kctl(spec, name, &control_templates[type]);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100651 if (!knew)
652 return -ENOMEM;
653 knew->index = cidx;
654 if (get_amp_nid_(val))
655 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
656 knew->private_value = val;
657 return 0;
658}
659
660static int add_control_with_pfx(struct hda_gen_spec *spec, int type,
661 const char *pfx, const char *dir,
662 const char *sfx, int cidx, unsigned long val)
663{
664 char name[32];
665 snprintf(name, sizeof(name), "%s %s %s", pfx, dir, sfx);
666 return add_control(spec, type, name, cidx, val);
667}
668
669#define add_pb_vol_ctrl(spec, type, pfx, val) \
670 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", 0, val)
671#define add_pb_sw_ctrl(spec, type, pfx, val) \
672 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", 0, val)
673#define __add_pb_vol_ctrl(spec, type, pfx, cidx, val) \
674 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", cidx, val)
675#define __add_pb_sw_ctrl(spec, type, pfx, cidx, val) \
676 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", cidx, val)
677
678static int add_vol_ctl(struct hda_codec *codec, const char *pfx, int cidx,
679 unsigned int chs, struct nid_path *path)
680{
681 unsigned int val;
682 if (!path)
683 return 0;
684 val = path->ctls[NID_PATH_VOL_CTL];
685 if (!val)
686 return 0;
687 val = amp_val_replace_channels(val, chs);
688 return __add_pb_vol_ctrl(codec->spec, HDA_CTL_WIDGET_VOL, pfx, cidx, val);
689}
690
691/* return the channel bits suitable for the given path->ctls[] */
692static int get_default_ch_nums(struct hda_codec *codec, struct nid_path *path,
693 int type)
694{
695 int chs = 1; /* mono (left only) */
696 if (path) {
697 hda_nid_t nid = get_amp_nid_(path->ctls[type]);
698 if (nid && (get_wcaps(codec, nid) & AC_WCAP_STEREO))
699 chs = 3; /* stereo */
700 }
701 return chs;
702}
703
704static int add_stereo_vol(struct hda_codec *codec, const char *pfx, int cidx,
705 struct nid_path *path)
706{
707 int chs = get_default_ch_nums(codec, path, NID_PATH_VOL_CTL);
708 return add_vol_ctl(codec, pfx, cidx, chs, path);
709}
710
711/* create a mute-switch for the given mixer widget;
712 * if it has multiple sources (e.g. DAC and loopback), create a bind-mute
713 */
714static int add_sw_ctl(struct hda_codec *codec, const char *pfx, int cidx,
715 unsigned int chs, struct nid_path *path)
716{
717 unsigned int val;
718 int type = HDA_CTL_WIDGET_MUTE;
719
720 if (!path)
721 return 0;
722 val = path->ctls[NID_PATH_MUTE_CTL];
723 if (!val)
724 return 0;
725 val = amp_val_replace_channels(val, chs);
726 if (get_amp_direction_(val) == HDA_INPUT) {
727 hda_nid_t nid = get_amp_nid_(val);
728 int nums = snd_hda_get_num_conns(codec, nid);
729 if (nums > 1) {
730 type = HDA_CTL_BIND_MUTE;
731 val |= nums << 19;
732 }
733 }
734 return __add_pb_sw_ctrl(codec->spec, type, pfx, cidx, val);
735}
736
737static int add_stereo_sw(struct hda_codec *codec, const char *pfx,
738 int cidx, struct nid_path *path)
739{
740 int chs = get_default_ch_nums(codec, path, NID_PATH_MUTE_CTL);
741 return add_sw_ctl(codec, pfx, cidx, chs, path);
742}
743
744static const char * const channel_name[4] = {
745 "Front", "Surround", "CLFE", "Side"
746};
747
748/* give some appropriate ctl name prefix for the given line out channel */
749static const char *get_line_out_pfx(struct hda_gen_spec *spec, int ch,
750 bool can_be_master, int *index)
751{
752 struct auto_pin_cfg *cfg = &spec->autocfg;
753
754 *index = 0;
755 if (cfg->line_outs == 1 && !spec->multi_ios &&
756 !cfg->hp_outs && !cfg->speaker_outs && can_be_master)
757 return spec->vmaster_mute.hook ? "PCM" : "Master";
758
759 /* if there is really a single DAC used in the whole output paths,
760 * use it master (or "PCM" if a vmaster hook is present)
761 */
762 if (spec->multiout.num_dacs == 1 && !spec->mixer_nid &&
763 !spec->multiout.hp_out_nid[0] && !spec->multiout.extra_out_nid[0])
764 return spec->vmaster_mute.hook ? "PCM" : "Master";
765
766 switch (cfg->line_out_type) {
767 case AUTO_PIN_SPEAKER_OUT:
768 if (cfg->line_outs == 1)
769 return "Speaker";
770 if (cfg->line_outs == 2)
771 return ch ? "Bass Speaker" : "Speaker";
772 break;
773 case AUTO_PIN_HP_OUT:
774 /* for multi-io case, only the primary out */
775 if (ch && spec->multi_ios)
776 break;
777 *index = ch;
778 return "Headphone";
779 default:
780 if (cfg->line_outs == 1 && !spec->multi_ios)
781 return "PCM";
782 break;
783 }
784 if (ch >= ARRAY_SIZE(channel_name)) {
785 snd_BUG();
786 return "PCM";
787 }
788
789 return channel_name[ch];
790}
791
792/*
793 * Parse output paths
794 */
795
796/* badness definition */
797enum {
798 /* No primary DAC is found for the main output */
799 BAD_NO_PRIMARY_DAC = 0x10000,
800 /* No DAC is found for the extra output */
801 BAD_NO_DAC = 0x4000,
802 /* No possible multi-ios */
803 BAD_MULTI_IO = 0x103,
804 /* No individual DAC for extra output */
805 BAD_NO_EXTRA_DAC = 0x102,
806 /* No individual DAC for extra surrounds */
807 BAD_NO_EXTRA_SURR_DAC = 0x101,
808 /* Primary DAC shared with main surrounds */
809 BAD_SHARED_SURROUND = 0x100,
810 /* Primary DAC shared with main CLFE */
811 BAD_SHARED_CLFE = 0x10,
812 /* Primary DAC shared with extra surrounds */
813 BAD_SHARED_EXTRA_SURROUND = 0x10,
814 /* Volume widget is shared */
815 BAD_SHARED_VOL = 0x10,
816};
817
Takashi Iwai0e614dd2013-01-07 15:11:44 +0100818/* look for widgets in the given path which are appropriate for
Takashi Iwai352f7f92012-12-19 12:52:06 +0100819 * volume and mute controls, and assign the values to ctls[].
820 *
821 * When no appropriate widget is found in the path, the badness value
822 * is incremented depending on the situation. The function returns the
823 * total badness for both volume and mute controls.
824 */
Takashi Iwai0e614dd2013-01-07 15:11:44 +0100825static int assign_out_path_ctls(struct hda_codec *codec, struct nid_path *path)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100826{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100827 hda_nid_t nid;
828 unsigned int val;
829 int badness = 0;
830
831 if (!path)
832 return BAD_SHARED_VOL * 2;
Takashi Iwai0e614dd2013-01-07 15:11:44 +0100833
834 if (path->ctls[NID_PATH_VOL_CTL] ||
835 path->ctls[NID_PATH_MUTE_CTL])
836 return 0; /* already evaluated */
837
Takashi Iwai352f7f92012-12-19 12:52:06 +0100838 nid = look_for_out_vol_nid(codec, path);
839 if (nid) {
840 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
841 if (is_ctl_used(codec, val, NID_PATH_VOL_CTL))
842 badness += BAD_SHARED_VOL;
843 else
844 path->ctls[NID_PATH_VOL_CTL] = val;
845 } else
846 badness += BAD_SHARED_VOL;
847 nid = look_for_out_mute_nid(codec, path);
848 if (nid) {
849 unsigned int wid_type = get_wcaps_type(get_wcaps(codec, nid));
850 if (wid_type == AC_WID_PIN || wid_type == AC_WID_AUD_OUT ||
851 nid_has_mute(codec, nid, HDA_OUTPUT))
852 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
853 else
854 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT);
855 if (is_ctl_used(codec, val, NID_PATH_MUTE_CTL))
856 badness += BAD_SHARED_VOL;
857 else
858 path->ctls[NID_PATH_MUTE_CTL] = val;
859 } else
860 badness += BAD_SHARED_VOL;
861 return badness;
862}
863
864struct badness_table {
865 int no_primary_dac; /* no primary DAC */
866 int no_dac; /* no secondary DACs */
867 int shared_primary; /* primary DAC is shared with main output */
868 int shared_surr; /* secondary DAC shared with main or primary */
869 int shared_clfe; /* third DAC shared with main or primary */
870 int shared_surr_main; /* secondary DAC sahred with main/DAC0 */
871};
872
873static struct badness_table main_out_badness = {
874 .no_primary_dac = BAD_NO_PRIMARY_DAC,
875 .no_dac = BAD_NO_DAC,
876 .shared_primary = BAD_NO_PRIMARY_DAC,
877 .shared_surr = BAD_SHARED_SURROUND,
878 .shared_clfe = BAD_SHARED_CLFE,
879 .shared_surr_main = BAD_SHARED_SURROUND,
880};
881
882static struct badness_table extra_out_badness = {
883 .no_primary_dac = BAD_NO_DAC,
884 .no_dac = BAD_NO_DAC,
885 .shared_primary = BAD_NO_EXTRA_DAC,
886 .shared_surr = BAD_SHARED_EXTRA_SURROUND,
887 .shared_clfe = BAD_SHARED_EXTRA_SURROUND,
888 .shared_surr_main = BAD_NO_EXTRA_SURR_DAC,
889};
890
Takashi Iwai7385df62013-01-07 09:50:52 +0100891/* get the DAC of the primary output corresponding to the given array index */
892static hda_nid_t get_primary_out(struct hda_codec *codec, int idx)
893{
894 struct hda_gen_spec *spec = codec->spec;
895 struct auto_pin_cfg *cfg = &spec->autocfg;
896
897 if (cfg->line_outs > idx)
898 return spec->private_dac_nids[idx];
899 idx -= cfg->line_outs;
900 if (spec->multi_ios > idx)
901 return spec->multi_io[idx].dac;
902 return 0;
903}
904
905/* return the DAC if it's reachable, otherwise zero */
906static inline hda_nid_t try_dac(struct hda_codec *codec,
907 hda_nid_t dac, hda_nid_t pin)
908{
909 return is_reachable_path(codec, dac, pin) ? dac : 0;
910}
911
Takashi Iwai352f7f92012-12-19 12:52:06 +0100912/* try to assign DACs to pins and return the resultant badness */
913static int try_assign_dacs(struct hda_codec *codec, int num_outs,
914 const hda_nid_t *pins, hda_nid_t *dacs,
Takashi Iwai196c17662013-01-04 15:01:40 +0100915 int *path_idx,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100916 const struct badness_table *bad)
917{
918 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100919 int i, j;
920 int badness = 0;
921 hda_nid_t dac;
922
923 if (!num_outs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 return 0;
925
Takashi Iwai352f7f92012-12-19 12:52:06 +0100926 for (i = 0; i < num_outs; i++) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +0100927 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100928 hda_nid_t pin = pins[i];
Takashi Iwai1e0b5282013-01-04 12:56:52 +0100929
Takashi Iwai0e614dd2013-01-07 15:11:44 +0100930 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
931 if (path) {
932 badness += assign_out_path_ctls(codec, path);
Takashi Iwai1e0b5282013-01-04 12:56:52 +0100933 continue;
934 }
935
936 dacs[i] = look_for_dac(codec, pin, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100937 if (!dacs[i] && !i) {
Takashi Iwai980428c2013-01-09 09:28:20 +0100938 /* try to steal the DAC of surrounds for the front */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100939 for (j = 1; j < num_outs; j++) {
940 if (is_reachable_path(codec, dacs[j], pin)) {
941 dacs[0] = dacs[j];
942 dacs[j] = 0;
Takashi Iwai980428c2013-01-09 09:28:20 +0100943 invalidate_nid_path(codec, path_idx[j]);
Takashi Iwai196c17662013-01-04 15:01:40 +0100944 path_idx[j] = 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100945 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 }
947 }
Takashi Iwai352f7f92012-12-19 12:52:06 +0100948 }
949 dac = dacs[i];
950 if (!dac) {
Takashi Iwai7385df62013-01-07 09:50:52 +0100951 if (num_outs > 2)
952 dac = try_dac(codec, get_primary_out(codec, i), pin);
953 if (!dac)
954 dac = try_dac(codec, dacs[0], pin);
955 if (!dac)
956 dac = try_dac(codec, get_primary_out(codec, i), pin);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100957 if (dac) {
958 if (!i)
959 badness += bad->shared_primary;
960 else if (i == 1)
961 badness += bad->shared_surr;
962 else
963 badness += bad->shared_clfe;
964 } else if (is_reachable_path(codec, spec->private_dac_nids[0], pin)) {
965 dac = spec->private_dac_nids[0];
966 badness += bad->shared_surr_main;
967 } else if (!i)
968 badness += bad->no_primary_dac;
969 else
970 badness += bad->no_dac;
971 }
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100972 path = snd_hda_add_new_path(codec, dac, pin, -spec->mixer_nid);
Takashi Iwai117688a2013-01-04 15:41:41 +0100973 if (!path && !i && spec->mixer_nid) {
Takashi Iwaib3a8c742012-12-20 18:29:16 +0100974 /* try with aamix */
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100975 path = snd_hda_add_new_path(codec, dac, pin, 0);
Takashi Iwaib3a8c742012-12-20 18:29:16 +0100976 }
Takashi Iwai0c8c0f52012-12-20 17:54:22 +0100977 if (!path)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100978 dac = dacs[i] = 0;
Takashi Iwaie1284af2013-01-03 16:33:02 +0100979 else {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +0100980 print_nid_path("output", path);
Takashi Iwaie1284af2013-01-03 16:33:02 +0100981 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +0100982 path_idx[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai0e614dd2013-01-07 15:11:44 +0100983 badness += assign_out_path_ctls(codec, path);
Takashi Iwaie1284af2013-01-03 16:33:02 +0100984 }
Takashi Iwai352f7f92012-12-19 12:52:06 +0100985 }
986
987 return badness;
988}
989
990/* return NID if the given pin has only a single connection to a certain DAC */
991static hda_nid_t get_dac_if_single(struct hda_codec *codec, hda_nid_t pin)
992{
993 struct hda_gen_spec *spec = codec->spec;
994 int i;
995 hda_nid_t nid_found = 0;
996
997 for (i = 0; i < spec->num_all_dacs; i++) {
998 hda_nid_t nid = spec->all_dacs[i];
999 if (!nid || is_dac_already_used(codec, nid))
1000 continue;
1001 if (is_reachable_path(codec, nid, pin)) {
1002 if (nid_found)
1003 return 0;
1004 nid_found = nid;
1005 }
1006 }
1007 return nid_found;
1008}
1009
1010/* check whether the given pin can be a multi-io pin */
1011static bool can_be_multiio_pin(struct hda_codec *codec,
1012 unsigned int location, hda_nid_t nid)
1013{
1014 unsigned int defcfg, caps;
1015
1016 defcfg = snd_hda_codec_get_pincfg(codec, nid);
1017 if (get_defcfg_connect(defcfg) != AC_JACK_PORT_COMPLEX)
1018 return false;
1019 if (location && get_defcfg_location(defcfg) != location)
1020 return false;
1021 caps = snd_hda_query_pin_caps(codec, nid);
1022 if (!(caps & AC_PINCAP_OUT))
1023 return false;
1024 return true;
1025}
1026
Takashi Iwaie22aab72013-01-04 14:50:04 +01001027/* count the number of input pins that are capable to be multi-io */
1028static int count_multiio_pins(struct hda_codec *codec, hda_nid_t reference_pin)
1029{
1030 struct hda_gen_spec *spec = codec->spec;
1031 struct auto_pin_cfg *cfg = &spec->autocfg;
1032 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1033 unsigned int location = get_defcfg_location(defcfg);
1034 int type, i;
1035 int num_pins = 0;
1036
1037 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1038 for (i = 0; i < cfg->num_inputs; i++) {
1039 if (cfg->inputs[i].type != type)
1040 continue;
1041 if (can_be_multiio_pin(codec, location,
1042 cfg->inputs[i].pin))
1043 num_pins++;
1044 }
1045 }
1046 return num_pins;
1047}
1048
Takashi Iwai352f7f92012-12-19 12:52:06 +01001049/*
1050 * multi-io helper
1051 *
1052 * When hardwired is set, try to fill ony hardwired pins, and returns
1053 * zero if any pins are filled, non-zero if nothing found.
1054 * When hardwired is off, try to fill possible input pins, and returns
1055 * the badness value.
1056 */
1057static int fill_multi_ios(struct hda_codec *codec,
1058 hda_nid_t reference_pin,
Takashi Iwaie22aab72013-01-04 14:50:04 +01001059 bool hardwired)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001060{
1061 struct hda_gen_spec *spec = codec->spec;
1062 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwaie22aab72013-01-04 14:50:04 +01001063 int type, i, j, num_pins, old_pins;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001064 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1065 unsigned int location = get_defcfg_location(defcfg);
1066 int badness = 0;
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001067 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001068
1069 old_pins = spec->multi_ios;
1070 if (old_pins >= 2)
1071 goto end_fill;
1072
Takashi Iwaie22aab72013-01-04 14:50:04 +01001073 num_pins = count_multiio_pins(codec, reference_pin);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001074 if (num_pins < 2)
1075 goto end_fill;
1076
Takashi Iwai352f7f92012-12-19 12:52:06 +01001077 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1078 for (i = 0; i < cfg->num_inputs; i++) {
1079 hda_nid_t nid = cfg->inputs[i].pin;
1080 hda_nid_t dac = 0;
1081
1082 if (cfg->inputs[i].type != type)
1083 continue;
1084 if (!can_be_multiio_pin(codec, location, nid))
1085 continue;
1086 for (j = 0; j < spec->multi_ios; j++) {
1087 if (nid == spec->multi_io[j].pin)
1088 break;
1089 }
1090 if (j < spec->multi_ios)
1091 continue;
1092
Takashi Iwai352f7f92012-12-19 12:52:06 +01001093 if (hardwired)
1094 dac = get_dac_if_single(codec, nid);
1095 else if (!dac)
1096 dac = look_for_dac(codec, nid, false);
1097 if (!dac) {
1098 badness++;
1099 continue;
1100 }
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001101 path = snd_hda_add_new_path(codec, dac, nid,
1102 -spec->mixer_nid);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001103 if (!path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001104 badness++;
1105 continue;
1106 }
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001107 print_nid_path("multiio", path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001108 spec->multi_io[spec->multi_ios].pin = nid;
1109 spec->multi_io[spec->multi_ios].dac = dac;
Takashi Iwai196c17662013-01-04 15:01:40 +01001110 spec->out_paths[cfg->line_outs + spec->multi_ios] =
1111 snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001112 spec->multi_ios++;
1113 if (spec->multi_ios >= 2)
1114 break;
1115 }
1116 }
1117 end_fill:
1118 if (badness)
1119 badness = BAD_MULTI_IO;
1120 if (old_pins == spec->multi_ios) {
1121 if (hardwired)
1122 return 1; /* nothing found */
1123 else
1124 return badness; /* no badness if nothing found */
1125 }
1126 if (!hardwired && spec->multi_ios < 2) {
1127 /* cancel newly assigned paths */
1128 spec->paths.used -= spec->multi_ios - old_pins;
1129 spec->multi_ios = old_pins;
1130 return badness;
1131 }
1132
1133 /* assign volume and mute controls */
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001134 for (i = old_pins; i < spec->multi_ios; i++) {
1135 path = snd_hda_get_path_from_idx(codec, spec->out_paths[cfg->line_outs + i]);
1136 badness += assign_out_path_ctls(codec, path);
1137 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001138
1139 return badness;
1140}
1141
1142/* map DACs for all pins in the list if they are single connections */
1143static bool map_singles(struct hda_codec *codec, int outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001144 const hda_nid_t *pins, hda_nid_t *dacs, int *path_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001145{
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001146 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001147 int i;
1148 bool found = false;
1149 for (i = 0; i < outs; i++) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001150 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001151 hda_nid_t dac;
1152 if (dacs[i])
1153 continue;
1154 dac = get_dac_if_single(codec, pins[i]);
1155 if (!dac)
1156 continue;
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001157 path = snd_hda_add_new_path(codec, dac, pins[i],
1158 -spec->mixer_nid);
Takashi Iwai117688a2013-01-04 15:41:41 +01001159 if (!path && !i && spec->mixer_nid)
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001160 path = snd_hda_add_new_path(codec, dac, pins[i], 0);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001161 if (path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001162 dacs[i] = dac;
1163 found = true;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001164 print_nid_path("output", path);
Takashi Iwaie1284af2013-01-03 16:33:02 +01001165 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +01001166 path_idx[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001167 }
1168 }
1169 return found;
1170}
1171
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001172/* create a new path including aamix if available, and return its index */
1173static int check_aamix_out_path(struct hda_codec *codec, int path_idx)
1174{
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001175 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001176 struct nid_path *path;
1177
1178 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001179 if (!path || !path->depth ||
1180 is_nid_contained(path, spec->mixer_nid))
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001181 return 0;
1182 path = snd_hda_add_new_path(codec, path->path[0],
1183 path->path[path->depth - 1],
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001184 spec->mixer_nid);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001185 if (!path)
1186 return 0;
1187 print_nid_path("output-aamix", path);
1188 path->active = false; /* unused as default */
1189 return snd_hda_get_path_idx(codec, path);
1190}
1191
Takashi Iwaia07a9492013-01-07 16:44:06 +01001192/* fill the empty entries in the dac array for speaker/hp with the
1193 * shared dac pointed by the paths
1194 */
1195static void refill_shared_dacs(struct hda_codec *codec, int num_outs,
1196 hda_nid_t *dacs, int *path_idx)
1197{
1198 struct nid_path *path;
1199 int i;
1200
1201 for (i = 0; i < num_outs; i++) {
1202 if (dacs[i])
1203 continue;
1204 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
1205 if (!path)
1206 continue;
1207 dacs[i] = path->path[0];
1208 }
1209}
1210
Takashi Iwai352f7f92012-12-19 12:52:06 +01001211/* fill in the dac_nids table from the parsed pin configuration */
1212static int fill_and_eval_dacs(struct hda_codec *codec,
1213 bool fill_hardwired,
1214 bool fill_mio_first)
1215{
1216 struct hda_gen_spec *spec = codec->spec;
1217 struct auto_pin_cfg *cfg = &spec->autocfg;
1218 int i, err, badness;
1219
1220 /* set num_dacs once to full for look_for_dac() */
1221 spec->multiout.num_dacs = cfg->line_outs;
1222 spec->multiout.dac_nids = spec->private_dac_nids;
1223 memset(spec->private_dac_nids, 0, sizeof(spec->private_dac_nids));
1224 memset(spec->multiout.hp_out_nid, 0, sizeof(spec->multiout.hp_out_nid));
1225 memset(spec->multiout.extra_out_nid, 0, sizeof(spec->multiout.extra_out_nid));
1226 spec->multi_ios = 0;
1227 snd_array_free(&spec->paths);
Takashi Iwaicd5be3f2013-01-07 15:07:00 +01001228
1229 /* clear path indices */
1230 memset(spec->out_paths, 0, sizeof(spec->out_paths));
1231 memset(spec->hp_paths, 0, sizeof(spec->hp_paths));
1232 memset(spec->speaker_paths, 0, sizeof(spec->speaker_paths));
1233 memset(spec->aamix_out_paths, 0, sizeof(spec->aamix_out_paths));
1234 memset(spec->digout_paths, 0, sizeof(spec->digout_paths));
Takashi Iwaic697b712013-01-07 17:09:26 +01001235 memset(spec->input_paths, 0, sizeof(spec->input_paths));
Takashi Iwaicd5be3f2013-01-07 15:07:00 +01001236 memset(spec->loopback_paths, 0, sizeof(spec->loopback_paths));
1237 memset(&spec->digin_path, 0, sizeof(spec->digin_path));
1238
Takashi Iwai352f7f92012-12-19 12:52:06 +01001239 badness = 0;
1240
1241 /* fill hard-wired DACs first */
1242 if (fill_hardwired) {
1243 bool mapped;
1244 do {
1245 mapped = map_singles(codec, cfg->line_outs,
1246 cfg->line_out_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001247 spec->private_dac_nids,
1248 spec->out_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001249 mapped |= map_singles(codec, cfg->hp_outs,
1250 cfg->hp_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001251 spec->multiout.hp_out_nid,
1252 spec->hp_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001253 mapped |= map_singles(codec, cfg->speaker_outs,
1254 cfg->speaker_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001255 spec->multiout.extra_out_nid,
1256 spec->speaker_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001257 if (fill_mio_first && cfg->line_outs == 1 &&
1258 cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
Takashi Iwaie22aab72013-01-04 14:50:04 +01001259 err = fill_multi_ios(codec, cfg->line_out_pins[0], true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001260 if (!err)
1261 mapped = true;
1262 }
1263 } while (mapped);
1264 }
1265
1266 badness += try_assign_dacs(codec, cfg->line_outs, cfg->line_out_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001267 spec->private_dac_nids, spec->out_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001268 &main_out_badness);
1269
Takashi Iwai352f7f92012-12-19 12:52:06 +01001270 if (fill_mio_first &&
1271 cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1272 /* try to fill multi-io first */
Takashi Iwaie22aab72013-01-04 14:50:04 +01001273 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001274 if (err < 0)
1275 return err;
1276 /* we don't count badness at this stage yet */
1277 }
1278
1279 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
1280 err = try_assign_dacs(codec, cfg->hp_outs, cfg->hp_pins,
1281 spec->multiout.hp_out_nid,
Takashi Iwai196c17662013-01-04 15:01:40 +01001282 spec->hp_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001283 &extra_out_badness);
1284 if (err < 0)
1285 return err;
1286 badness += err;
1287 }
1288 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1289 err = try_assign_dacs(codec, cfg->speaker_outs,
1290 cfg->speaker_pins,
1291 spec->multiout.extra_out_nid,
Takashi Iwai196c17662013-01-04 15:01:40 +01001292 spec->speaker_paths,
1293 &extra_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001294 if (err < 0)
1295 return err;
1296 badness += err;
1297 }
1298 if (cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
Takashi Iwaie22aab72013-01-04 14:50:04 +01001299 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001300 if (err < 0)
1301 return err;
1302 badness += err;
1303 }
Takashi Iwaie22aab72013-01-04 14:50:04 +01001304
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001305 if (spec->mixer_nid) {
1306 spec->aamix_out_paths[0] =
1307 check_aamix_out_path(codec, spec->out_paths[0]);
1308 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1309 spec->aamix_out_paths[1] =
1310 check_aamix_out_path(codec, spec->hp_paths[0]);
1311 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1312 spec->aamix_out_paths[2] =
1313 check_aamix_out_path(codec, spec->speaker_paths[0]);
1314 }
1315
Takashi Iwaie22aab72013-01-04 14:50:04 +01001316 if (cfg->hp_outs && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
1317 if (count_multiio_pins(codec, cfg->hp_pins[0]) >= 2)
1318 spec->multi_ios = 1; /* give badness */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001319
Takashi Iwaia07a9492013-01-07 16:44:06 +01001320 /* re-count num_dacs and squash invalid entries */
1321 spec->multiout.num_dacs = 0;
1322 for (i = 0; i < cfg->line_outs; i++) {
1323 if (spec->private_dac_nids[i])
1324 spec->multiout.num_dacs++;
1325 else {
1326 memmove(spec->private_dac_nids + i,
1327 spec->private_dac_nids + i + 1,
1328 sizeof(hda_nid_t) * (cfg->line_outs - i - 1));
1329 spec->private_dac_nids[cfg->line_outs - 1] = 0;
1330 }
1331 }
1332
1333 spec->ext_channel_count = spec->min_channel_count =
1334 spec->multiout.num_dacs;
1335
Takashi Iwai352f7f92012-12-19 12:52:06 +01001336 if (spec->multi_ios == 2) {
1337 for (i = 0; i < 2; i++)
1338 spec->private_dac_nids[spec->multiout.num_dacs++] =
1339 spec->multi_io[i].dac;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001340 } else if (spec->multi_ios) {
1341 spec->multi_ios = 0;
1342 badness += BAD_MULTI_IO;
1343 }
1344
Takashi Iwaia07a9492013-01-07 16:44:06 +01001345 /* re-fill the shared DAC for speaker / headphone */
1346 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1347 refill_shared_dacs(codec, cfg->hp_outs,
1348 spec->multiout.hp_out_nid,
1349 spec->hp_paths);
1350 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1351 refill_shared_dacs(codec, cfg->speaker_outs,
1352 spec->multiout.extra_out_nid,
1353 spec->speaker_paths);
1354
Takashi Iwai2c12c302013-01-10 09:33:29 +01001355 /* set initial pinctl targets */
1356 set_pin_targets(codec, cfg->line_outs, cfg->line_out_pins,
1357 cfg->line_out_type == AUTO_PIN_HP_OUT ? PIN_HP : PIN_OUT);
1358 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1359 set_pin_targets(codec, cfg->hp_outs, cfg->hp_pins, PIN_HP);
1360 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1361 set_pin_targets(codec, cfg->speaker_outs,
1362 cfg->speaker_pins, PIN_OUT);
1363
Takashi Iwai352f7f92012-12-19 12:52:06 +01001364 return badness;
1365}
1366
1367#define DEBUG_BADNESS
1368
1369#ifdef DEBUG_BADNESS
1370#define debug_badness snd_printdd
1371#else
1372#define debug_badness(...)
1373#endif
1374
1375static void debug_show_configs(struct hda_gen_spec *spec, struct auto_pin_cfg *cfg)
1376{
1377 debug_badness("multi_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1378 cfg->line_out_pins[0], cfg->line_out_pins[1],
Takashi Iwai708122e2012-12-20 17:56:57 +01001379 cfg->line_out_pins[2], cfg->line_out_pins[3],
Takashi Iwai352f7f92012-12-19 12:52:06 +01001380 spec->multiout.dac_nids[0],
1381 spec->multiout.dac_nids[1],
1382 spec->multiout.dac_nids[2],
1383 spec->multiout.dac_nids[3]);
1384 if (spec->multi_ios > 0)
1385 debug_badness("multi_ios(%d) = %x/%x : %x/%x\n",
1386 spec->multi_ios,
1387 spec->multi_io[0].pin, spec->multi_io[1].pin,
1388 spec->multi_io[0].dac, spec->multi_io[1].dac);
1389 debug_badness("hp_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1390 cfg->hp_pins[0], cfg->hp_pins[1],
Takashi Iwai708122e2012-12-20 17:56:57 +01001391 cfg->hp_pins[2], cfg->hp_pins[3],
Takashi Iwai352f7f92012-12-19 12:52:06 +01001392 spec->multiout.hp_out_nid[0],
1393 spec->multiout.hp_out_nid[1],
1394 spec->multiout.hp_out_nid[2],
1395 spec->multiout.hp_out_nid[3]);
1396 debug_badness("spk_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1397 cfg->speaker_pins[0], cfg->speaker_pins[1],
1398 cfg->speaker_pins[2], cfg->speaker_pins[3],
1399 spec->multiout.extra_out_nid[0],
1400 spec->multiout.extra_out_nid[1],
1401 spec->multiout.extra_out_nid[2],
1402 spec->multiout.extra_out_nid[3]);
1403}
1404
1405/* find all available DACs of the codec */
1406static void fill_all_dac_nids(struct hda_codec *codec)
1407{
1408 struct hda_gen_spec *spec = codec->spec;
1409 int i;
1410 hda_nid_t nid = codec->start_nid;
1411
1412 spec->num_all_dacs = 0;
1413 memset(spec->all_dacs, 0, sizeof(spec->all_dacs));
1414 for (i = 0; i < codec->num_nodes; i++, nid++) {
1415 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_AUD_OUT)
1416 continue;
1417 if (spec->num_all_dacs >= ARRAY_SIZE(spec->all_dacs)) {
1418 snd_printk(KERN_ERR "hda: Too many DACs!\n");
1419 break;
1420 }
1421 spec->all_dacs[spec->num_all_dacs++] = nid;
1422 }
1423}
1424
1425static int parse_output_paths(struct hda_codec *codec)
1426{
1427 struct hda_gen_spec *spec = codec->spec;
1428 struct auto_pin_cfg *cfg = &spec->autocfg;
1429 struct auto_pin_cfg *best_cfg;
1430 int best_badness = INT_MAX;
1431 int badness;
1432 bool fill_hardwired = true, fill_mio_first = true;
1433 bool best_wired = true, best_mio = true;
1434 bool hp_spk_swapped = false;
1435
1436 fill_all_dac_nids(codec);
1437
1438 best_cfg = kmalloc(sizeof(*best_cfg), GFP_KERNEL);
1439 if (!best_cfg)
1440 return -ENOMEM;
1441 *best_cfg = *cfg;
1442
1443 for (;;) {
1444 badness = fill_and_eval_dacs(codec, fill_hardwired,
1445 fill_mio_first);
1446 if (badness < 0) {
1447 kfree(best_cfg);
1448 return badness;
1449 }
1450 debug_badness("==> lo_type=%d, wired=%d, mio=%d, badness=0x%x\n",
1451 cfg->line_out_type, fill_hardwired, fill_mio_first,
1452 badness);
1453 debug_show_configs(spec, cfg);
1454 if (badness < best_badness) {
1455 best_badness = badness;
1456 *best_cfg = *cfg;
1457 best_wired = fill_hardwired;
1458 best_mio = fill_mio_first;
1459 }
1460 if (!badness)
1461 break;
1462 fill_mio_first = !fill_mio_first;
1463 if (!fill_mio_first)
1464 continue;
1465 fill_hardwired = !fill_hardwired;
1466 if (!fill_hardwired)
1467 continue;
1468 if (hp_spk_swapped)
1469 break;
1470 hp_spk_swapped = true;
1471 if (cfg->speaker_outs > 0 &&
1472 cfg->line_out_type == AUTO_PIN_HP_OUT) {
1473 cfg->hp_outs = cfg->line_outs;
1474 memcpy(cfg->hp_pins, cfg->line_out_pins,
1475 sizeof(cfg->hp_pins));
1476 cfg->line_outs = cfg->speaker_outs;
1477 memcpy(cfg->line_out_pins, cfg->speaker_pins,
1478 sizeof(cfg->speaker_pins));
1479 cfg->speaker_outs = 0;
1480 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
1481 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
1482 fill_hardwired = true;
1483 continue;
1484 }
1485 if (cfg->hp_outs > 0 &&
1486 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
1487 cfg->speaker_outs = cfg->line_outs;
1488 memcpy(cfg->speaker_pins, cfg->line_out_pins,
1489 sizeof(cfg->speaker_pins));
1490 cfg->line_outs = cfg->hp_outs;
1491 memcpy(cfg->line_out_pins, cfg->hp_pins,
1492 sizeof(cfg->hp_pins));
1493 cfg->hp_outs = 0;
1494 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
1495 cfg->line_out_type = AUTO_PIN_HP_OUT;
1496 fill_hardwired = true;
1497 continue;
1498 }
1499 break;
1500 }
1501
1502 if (badness) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001503 debug_badness("==> restoring best_cfg\n");
Takashi Iwai352f7f92012-12-19 12:52:06 +01001504 *cfg = *best_cfg;
1505 fill_and_eval_dacs(codec, best_wired, best_mio);
1506 }
1507 debug_badness("==> Best config: lo_type=%d, wired=%d, mio=%d\n",
1508 cfg->line_out_type, best_wired, best_mio);
1509 debug_show_configs(spec, cfg);
1510
1511 if (cfg->line_out_pins[0]) {
1512 struct nid_path *path;
Takashi Iwai196c17662013-01-04 15:01:40 +01001513 path = snd_hda_get_path_from_idx(codec, spec->out_paths[0]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001514 if (path)
1515 spec->vmaster_nid = look_for_out_vol_nid(codec, path);
1516 }
1517
1518 kfree(best_cfg);
1519 return 0;
1520}
1521
1522/* add playback controls from the parsed DAC table */
1523static int create_multi_out_ctls(struct hda_codec *codec,
1524 const struct auto_pin_cfg *cfg)
1525{
1526 struct hda_gen_spec *spec = codec->spec;
1527 int i, err, noutputs;
1528
1529 noutputs = cfg->line_outs;
1530 if (spec->multi_ios > 0 && cfg->line_outs < 3)
1531 noutputs += spec->multi_ios;
1532
1533 for (i = 0; i < noutputs; i++) {
1534 const char *name;
1535 int index;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001536 struct nid_path *path;
1537
Takashi Iwai352f7f92012-12-19 12:52:06 +01001538 if (i >= cfg->line_outs) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001539 index = 0;
1540 name = channel_name[i];
1541 } else {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001542 name = get_line_out_pfx(spec, i, true, &index);
1543 }
1544
Takashi Iwai196c17662013-01-04 15:01:40 +01001545 path = snd_hda_get_path_from_idx(codec, spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001546 if (!path)
1547 continue;
1548 if (!name || !strcmp(name, "CLFE")) {
1549 /* Center/LFE */
1550 err = add_vol_ctl(codec, "Center", 0, 1, path);
1551 if (err < 0)
1552 return err;
1553 err = add_vol_ctl(codec, "LFE", 0, 2, path);
1554 if (err < 0)
1555 return err;
1556 err = add_sw_ctl(codec, "Center", 0, 1, path);
1557 if (err < 0)
1558 return err;
1559 err = add_sw_ctl(codec, "LFE", 0, 2, path);
1560 if (err < 0)
1561 return err;
1562 } else {
1563 err = add_stereo_vol(codec, name, index, path);
1564 if (err < 0)
1565 return err;
1566 err = add_stereo_sw(codec, name, index, path);
1567 if (err < 0)
1568 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 }
1570 }
1571 return 0;
1572}
1573
Takashi Iwaic2c80382013-01-07 10:33:57 +01001574static int create_extra_out(struct hda_codec *codec, int path_idx,
Takashi Iwai196c17662013-01-04 15:01:40 +01001575 const char *pfx, int cidx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576{
Takashi Iwai352f7f92012-12-19 12:52:06 +01001577 struct nid_path *path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 int err;
1579
Takashi Iwai196c17662013-01-04 15:01:40 +01001580 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001581 if (!path)
1582 return 0;
Takashi Iwaic2c80382013-01-07 10:33:57 +01001583 err = add_stereo_vol(codec, pfx, cidx, path);
1584 if (err < 0)
1585 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001586 err = add_stereo_sw(codec, pfx, cidx, path);
1587 if (err < 0)
1588 return err;
1589 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590}
1591
Takashi Iwai352f7f92012-12-19 12:52:06 +01001592/* add playback controls for speaker and HP outputs */
1593static int create_extra_outs(struct hda_codec *codec, int num_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001594 const int *paths, const char *pfx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595{
Takashi Iwaic2c80382013-01-07 10:33:57 +01001596 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001597
1598 for (i = 0; i < num_pins; i++) {
Takashi Iwaic2c80382013-01-07 10:33:57 +01001599 const char *name;
1600 char tmp[44];
1601 int err, idx = 0;
1602
1603 if (num_pins == 2 && i == 1 && !strcmp(pfx, "Speaker"))
1604 name = "Bass Speaker";
1605 else if (num_pins >= 3) {
1606 snprintf(tmp, sizeof(tmp), "%s %s",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001607 pfx, channel_name[i]);
Takashi Iwaic2c80382013-01-07 10:33:57 +01001608 name = tmp;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001609 } else {
Takashi Iwaic2c80382013-01-07 10:33:57 +01001610 name = pfx;
1611 idx = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 }
Takashi Iwaic2c80382013-01-07 10:33:57 +01001613 err = create_extra_out(codec, paths[i], name, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001614 if (err < 0)
1615 return err;
1616 }
1617 return 0;
1618}
Takashi Iwai97ec5582006-03-21 11:29:07 +01001619
Takashi Iwai352f7f92012-12-19 12:52:06 +01001620static int create_hp_out_ctls(struct hda_codec *codec)
1621{
1622 struct hda_gen_spec *spec = codec->spec;
1623 return create_extra_outs(codec, spec->autocfg.hp_outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001624 spec->hp_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001625 "Headphone");
1626}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627
Takashi Iwai352f7f92012-12-19 12:52:06 +01001628static int create_speaker_out_ctls(struct hda_codec *codec)
1629{
1630 struct hda_gen_spec *spec = codec->spec;
1631 return create_extra_outs(codec, spec->autocfg.speaker_outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001632 spec->speaker_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001633 "Speaker");
1634}
1635
1636/*
Takashi Iwai38cf6f12012-12-21 14:09:42 +01001637 * independent HP controls
1638 */
1639
1640static int indep_hp_info(struct snd_kcontrol *kcontrol,
1641 struct snd_ctl_elem_info *uinfo)
1642{
1643 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
1644}
1645
1646static int indep_hp_get(struct snd_kcontrol *kcontrol,
1647 struct snd_ctl_elem_value *ucontrol)
1648{
1649 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1650 struct hda_gen_spec *spec = codec->spec;
1651 ucontrol->value.enumerated.item[0] = spec->indep_hp_enabled;
1652 return 0;
1653}
1654
1655static int indep_hp_put(struct snd_kcontrol *kcontrol,
1656 struct snd_ctl_elem_value *ucontrol)
1657{
1658 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1659 struct hda_gen_spec *spec = codec->spec;
1660 unsigned int select = ucontrol->value.enumerated.item[0];
1661 int ret = 0;
1662
1663 mutex_lock(&spec->pcm_mutex);
1664 if (spec->active_streams) {
1665 ret = -EBUSY;
1666 goto unlock;
1667 }
1668
1669 if (spec->indep_hp_enabled != select) {
1670 spec->indep_hp_enabled = select;
1671 if (spec->indep_hp_enabled)
1672 spec->multiout.hp_out_nid[0] = 0;
1673 else
1674 spec->multiout.hp_out_nid[0] = spec->alt_dac_nid;
1675 ret = 1;
1676 }
1677 unlock:
1678 mutex_unlock(&spec->pcm_mutex);
1679 return ret;
1680}
1681
1682static const struct snd_kcontrol_new indep_hp_ctl = {
1683 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1684 .name = "Independent HP",
1685 .info = indep_hp_info,
1686 .get = indep_hp_get,
1687 .put = indep_hp_put,
1688};
1689
1690
1691static int create_indep_hp_ctls(struct hda_codec *codec)
1692{
1693 struct hda_gen_spec *spec = codec->spec;
1694
1695 if (!spec->indep_hp)
1696 return 0;
1697 if (!spec->multiout.hp_out_nid[0]) {
1698 spec->indep_hp = 0;
1699 return 0;
1700 }
1701
1702 spec->indep_hp_enabled = false;
1703 spec->alt_dac_nid = spec->multiout.hp_out_nid[0];
1704 if (!snd_hda_gen_add_kctl(spec, NULL, &indep_hp_ctl))
1705 return -ENOMEM;
1706 return 0;
1707}
1708
1709/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01001710 * channel mode enum control
1711 */
1712
1713static int ch_mode_info(struct snd_kcontrol *kcontrol,
1714 struct snd_ctl_elem_info *uinfo)
1715{
1716 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1717 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia07a9492013-01-07 16:44:06 +01001718 int chs;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001719
1720 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1721 uinfo->count = 1;
1722 uinfo->value.enumerated.items = spec->multi_ios + 1;
1723 if (uinfo->value.enumerated.item > spec->multi_ios)
1724 uinfo->value.enumerated.item = spec->multi_ios;
Takashi Iwaia07a9492013-01-07 16:44:06 +01001725 chs = uinfo->value.enumerated.item * 2 + spec->min_channel_count;
1726 sprintf(uinfo->value.enumerated.name, "%dch", chs);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001727 return 0;
1728}
1729
1730static int ch_mode_get(struct snd_kcontrol *kcontrol,
1731 struct snd_ctl_elem_value *ucontrol)
1732{
1733 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1734 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia07a9492013-01-07 16:44:06 +01001735 ucontrol->value.enumerated.item[0] =
1736 (spec->ext_channel_count - spec->min_channel_count) / 2;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001737 return 0;
1738}
1739
Takashi Iwai196c17662013-01-04 15:01:40 +01001740static inline struct nid_path *
1741get_multiio_path(struct hda_codec *codec, int idx)
1742{
1743 struct hda_gen_spec *spec = codec->spec;
1744 return snd_hda_get_path_from_idx(codec,
1745 spec->out_paths[spec->autocfg.line_outs + idx]);
1746}
1747
Takashi Iwai352f7f92012-12-19 12:52:06 +01001748static int set_multi_io(struct hda_codec *codec, int idx, bool output)
1749{
1750 struct hda_gen_spec *spec = codec->spec;
1751 hda_nid_t nid = spec->multi_io[idx].pin;
1752 struct nid_path *path;
1753
Takashi Iwai196c17662013-01-04 15:01:40 +01001754 path = get_multiio_path(codec, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001755 if (!path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 return -EINVAL;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001757
1758 if (path->active == output)
1759 return 0;
1760
1761 if (output) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01001762 set_pin_target(codec, nid, PIN_OUT, true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001763 snd_hda_activate_path(codec, path, true, true);
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01001764 set_pin_eapd(codec, nid, true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001765 } else {
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01001766 set_pin_eapd(codec, nid, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001767 snd_hda_activate_path(codec, path, false, true);
Takashi Iwai2c12c302013-01-10 09:33:29 +01001768 set_pin_target(codec, nid, spec->multi_io[idx].ctl_in, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 }
Takashi Iwaia365fed2013-01-10 16:10:06 +01001770
1771 /* update jack retasking in case it modifies any of them */
1772 snd_hda_gen_hp_automute(codec, NULL);
1773 snd_hda_gen_line_automute(codec, NULL);
1774 snd_hda_gen_mic_autoswitch(codec, NULL);
1775
Takashi Iwai352f7f92012-12-19 12:52:06 +01001776 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777}
1778
Takashi Iwai352f7f92012-12-19 12:52:06 +01001779static int ch_mode_put(struct snd_kcontrol *kcontrol,
1780 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781{
Takashi Iwai352f7f92012-12-19 12:52:06 +01001782 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1783 struct hda_gen_spec *spec = codec->spec;
1784 int i, ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785
Takashi Iwai352f7f92012-12-19 12:52:06 +01001786 ch = ucontrol->value.enumerated.item[0];
1787 if (ch < 0 || ch > spec->multi_ios)
1788 return -EINVAL;
Takashi Iwaia07a9492013-01-07 16:44:06 +01001789 if (ch == (spec->ext_channel_count - spec->min_channel_count) / 2)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001790 return 0;
Takashi Iwaia07a9492013-01-07 16:44:06 +01001791 spec->ext_channel_count = ch * 2 + spec->min_channel_count;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001792 for (i = 0; i < spec->multi_ios; i++)
1793 set_multi_io(codec, i, i < ch);
1794 spec->multiout.max_channels = max(spec->ext_channel_count,
1795 spec->const_channel_count);
1796 if (spec->need_dac_fix)
1797 spec->multiout.num_dacs = spec->multiout.max_channels / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 return 1;
1799}
1800
Takashi Iwai352f7f92012-12-19 12:52:06 +01001801static const struct snd_kcontrol_new channel_mode_enum = {
1802 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1803 .name = "Channel Mode",
1804 .info = ch_mode_info,
1805 .get = ch_mode_get,
1806 .put = ch_mode_put,
1807};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808
Takashi Iwai352f7f92012-12-19 12:52:06 +01001809static int create_multi_channel_mode(struct hda_codec *codec)
1810{
1811 struct hda_gen_spec *spec = codec->spec;
1812
1813 if (spec->multi_ios > 0) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01001814 if (!snd_hda_gen_add_kctl(spec, NULL, &channel_mode_enum))
Takashi Iwai352f7f92012-12-19 12:52:06 +01001815 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 return 0;
1818}
1819
Takashi Iwai352f7f92012-12-19 12:52:06 +01001820/*
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001821 * aamix loopback enable/disable switch
1822 */
1823
1824#define loopback_mixing_info indep_hp_info
1825
1826static int loopback_mixing_get(struct snd_kcontrol *kcontrol,
1827 struct snd_ctl_elem_value *ucontrol)
1828{
1829 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1830 struct hda_gen_spec *spec = codec->spec;
1831 ucontrol->value.enumerated.item[0] = spec->aamix_mode;
1832 return 0;
1833}
1834
1835static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
1836 int nomix_path_idx, int mix_path_idx)
1837{
1838 struct nid_path *nomix_path, *mix_path;
1839
1840 nomix_path = snd_hda_get_path_from_idx(codec, nomix_path_idx);
1841 mix_path = snd_hda_get_path_from_idx(codec, mix_path_idx);
1842 if (!nomix_path || !mix_path)
1843 return;
1844 if (do_mix) {
1845 snd_hda_activate_path(codec, nomix_path, false, true);
1846 snd_hda_activate_path(codec, mix_path, true, true);
1847 } else {
1848 snd_hda_activate_path(codec, mix_path, false, true);
1849 snd_hda_activate_path(codec, nomix_path, true, true);
1850 }
1851}
1852
1853static int loopback_mixing_put(struct snd_kcontrol *kcontrol,
1854 struct snd_ctl_elem_value *ucontrol)
1855{
1856 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1857 struct hda_gen_spec *spec = codec->spec;
1858 unsigned int val = ucontrol->value.enumerated.item[0];
1859
1860 if (val == spec->aamix_mode)
1861 return 0;
1862 spec->aamix_mode = val;
1863 update_aamix_paths(codec, val, spec->out_paths[0],
1864 spec->aamix_out_paths[0]);
1865 update_aamix_paths(codec, val, spec->hp_paths[0],
1866 spec->aamix_out_paths[1]);
1867 update_aamix_paths(codec, val, spec->speaker_paths[0],
1868 spec->aamix_out_paths[2]);
1869 return 1;
1870}
1871
1872static const struct snd_kcontrol_new loopback_mixing_enum = {
1873 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1874 .name = "Loopback Mixing",
1875 .info = loopback_mixing_info,
1876 .get = loopback_mixing_get,
1877 .put = loopback_mixing_put,
1878};
1879
1880static int create_loopback_mixing_ctl(struct hda_codec *codec)
1881{
1882 struct hda_gen_spec *spec = codec->spec;
1883
1884 if (!spec->mixer_nid)
1885 return 0;
1886 if (!(spec->aamix_out_paths[0] || spec->aamix_out_paths[1] ||
1887 spec->aamix_out_paths[2]))
1888 return 0;
1889 if (!snd_hda_gen_add_kctl(spec, NULL, &loopback_mixing_enum))
1890 return -ENOMEM;
1891 return 0;
1892}
1893
1894/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01001895 * shared headphone/mic handling
1896 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02001897
Takashi Iwai352f7f92012-12-19 12:52:06 +01001898static void call_update_outputs(struct hda_codec *codec);
1899
1900/* for shared I/O, change the pin-control accordingly */
1901static void update_shared_mic_hp(struct hda_codec *codec, bool set_as_mic)
1902{
1903 struct hda_gen_spec *spec = codec->spec;
1904 unsigned int val;
1905 hda_nid_t pin = spec->autocfg.inputs[1].pin;
1906 /* NOTE: this assumes that there are only two inputs, the
1907 * first is the real internal mic and the second is HP/mic jack.
1908 */
1909
1910 val = snd_hda_get_default_vref(codec, pin);
1911
1912 /* This pin does not have vref caps - let's enable vref on pin 0x18
1913 instead, as suggested by Realtek */
1914 if (val == AC_PINCTL_VREF_HIZ && spec->shared_mic_vref_pin) {
1915 const hda_nid_t vref_pin = spec->shared_mic_vref_pin;
1916 unsigned int vref_val = snd_hda_get_default_vref(codec, vref_pin);
1917 if (vref_val != AC_PINCTL_VREF_HIZ)
Takashi Iwai7594aa32012-12-20 15:38:40 +01001918 snd_hda_set_pin_ctl_cache(codec, vref_pin,
1919 PIN_IN | (set_as_mic ? vref_val : 0));
Takashi Iwaicb53c622007-08-10 17:21:45 +02001920 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001921
1922 val = set_as_mic ? val | PIN_IN : PIN_HP;
Takashi Iwai2c12c302013-01-10 09:33:29 +01001923 set_pin_target(codec, pin, val, true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001924
1925 spec->automute_speaker = !set_as_mic;
1926 call_update_outputs(codec);
1927}
1928
1929/* create a shared input with the headphone out */
1930static int create_shared_input(struct hda_codec *codec)
1931{
1932 struct hda_gen_spec *spec = codec->spec;
1933 struct auto_pin_cfg *cfg = &spec->autocfg;
1934 unsigned int defcfg;
1935 hda_nid_t nid;
1936
1937 /* only one internal input pin? */
1938 if (cfg->num_inputs != 1)
1939 return 0;
1940 defcfg = snd_hda_codec_get_pincfg(codec, cfg->inputs[0].pin);
1941 if (snd_hda_get_input_pin_attr(defcfg) != INPUT_PIN_ATTR_INT)
1942 return 0;
1943
1944 if (cfg->hp_outs == 1 && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
1945 nid = cfg->hp_pins[0]; /* OK, we have a single HP-out */
1946 else if (cfg->line_outs == 1 && cfg->line_out_type == AUTO_PIN_HP_OUT)
1947 nid = cfg->line_out_pins[0]; /* OK, we have a single line-out */
1948 else
1949 return 0; /* both not available */
1950
1951 if (!(snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_IN))
1952 return 0; /* no input */
1953
1954 cfg->inputs[1].pin = nid;
1955 cfg->inputs[1].type = AUTO_PIN_MIC;
1956 cfg->num_inputs = 2;
1957 spec->shared_mic_hp = 1;
1958 snd_printdd("hda-codec: Enable shared I/O jack on NID 0x%x\n", nid);
1959 return 0;
1960}
1961
Takashi Iwai978e77e2013-01-10 16:57:58 +01001962/*
1963 * output jack mode
1964 */
1965static int out_jack_mode_info(struct snd_kcontrol *kcontrol,
1966 struct snd_ctl_elem_info *uinfo)
1967{
1968 static const char * const texts[] = {
1969 "Line Out", "Headphone Out",
1970 };
1971 return snd_hda_enum_helper_info(kcontrol, uinfo, 2, texts);
1972}
1973
1974static int out_jack_mode_get(struct snd_kcontrol *kcontrol,
1975 struct snd_ctl_elem_value *ucontrol)
1976{
1977 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1978 hda_nid_t nid = kcontrol->private_value;
1979 if (snd_hda_codec_get_pin_target(codec, nid) == PIN_HP)
1980 ucontrol->value.enumerated.item[0] = 1;
1981 else
1982 ucontrol->value.enumerated.item[0] = 0;
1983 return 0;
1984}
1985
1986static int out_jack_mode_put(struct snd_kcontrol *kcontrol,
1987 struct snd_ctl_elem_value *ucontrol)
1988{
1989 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1990 hda_nid_t nid = kcontrol->private_value;
1991 unsigned int val;
1992
1993 val = ucontrol->value.enumerated.item[0] ? PIN_HP : PIN_OUT;
1994 if (snd_hda_codec_get_pin_target(codec, nid) == val)
1995 return 0;
1996 snd_hda_set_pin_ctl_cache(codec, nid, val);
1997 return 1;
1998}
1999
2000static const struct snd_kcontrol_new out_jack_mode_enum = {
2001 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2002 .info = out_jack_mode_info,
2003 .get = out_jack_mode_get,
2004 .put = out_jack_mode_put,
2005};
2006
2007static bool find_kctl_name(struct hda_codec *codec, const char *name, int idx)
2008{
2009 struct hda_gen_spec *spec = codec->spec;
2010 int i;
2011
2012 for (i = 0; i < spec->kctls.used; i++) {
2013 struct snd_kcontrol_new *kctl = snd_array_elem(&spec->kctls, i);
2014 if (!strcmp(kctl->name, name) && kctl->index == idx)
2015 return true;
2016 }
2017 return false;
2018}
2019
2020static void get_jack_mode_name(struct hda_codec *codec, hda_nid_t pin,
2021 char *name, size_t name_len)
2022{
2023 struct hda_gen_spec *spec = codec->spec;
2024 int idx = 0;
2025
2026 snd_hda_get_pin_label(codec, pin, &spec->autocfg, name, name_len, &idx);
2027 strlcat(name, " Jack Mode", name_len);
2028
2029 for (; find_kctl_name(codec, name, idx); idx++)
2030 ;
2031}
2032
2033static int create_out_jack_modes(struct hda_codec *codec, int num_pins,
2034 hda_nid_t *pins)
2035{
2036 struct hda_gen_spec *spec = codec->spec;
2037 int i;
2038
2039 for (i = 0; i < num_pins; i++) {
2040 hda_nid_t pin = pins[i];
2041 unsigned int pincap = snd_hda_query_pin_caps(codec, pin);
2042 if ((pincap & AC_PINCAP_OUT) && (pincap & AC_PINCAP_HP_DRV)) {
2043 struct snd_kcontrol_new *knew;
2044 char name[44];
2045 get_jack_mode_name(codec, pin, name, sizeof(name));
2046 knew = snd_hda_gen_add_kctl(spec, name,
2047 &out_jack_mode_enum);
2048 if (!knew)
2049 return -ENOMEM;
2050 knew->private_value = pin;
2051 }
2052 }
2053
2054 return 0;
2055}
2056
Takashi Iwai352f7f92012-12-19 12:52:06 +01002057
2058/*
2059 * Parse input paths
2060 */
2061
2062#ifdef CONFIG_PM
2063/* add the powersave loopback-list entry */
2064static void add_loopback_list(struct hda_gen_spec *spec, hda_nid_t mix, int idx)
2065{
2066 struct hda_amp_list *list;
2067
2068 if (spec->num_loopbacks >= ARRAY_SIZE(spec->loopback_list) - 1)
2069 return;
2070 list = spec->loopback_list + spec->num_loopbacks;
2071 list->nid = mix;
2072 list->dir = HDA_INPUT;
2073 list->idx = idx;
2074 spec->num_loopbacks++;
Takashi Iwaicb53c622007-08-10 17:21:45 +02002075 spec->loopback.amplist = spec->loopback_list;
2076}
2077#else
Takashi Iwai352f7f92012-12-19 12:52:06 +01002078#define add_loopback_list(spec, mix, idx) /* NOP */
Takashi Iwaicb53c622007-08-10 17:21:45 +02002079#endif
2080
Takashi Iwai352f7f92012-12-19 12:52:06 +01002081/* create input playback/capture controls for the given pin */
Takashi Iwai196c17662013-01-04 15:01:40 +01002082static int new_analog_input(struct hda_codec *codec, int input_idx,
2083 hda_nid_t pin, const char *ctlname, int ctlidx,
Takashi Iwai352f7f92012-12-19 12:52:06 +01002084 hda_nid_t mix_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002086 struct hda_gen_spec *spec = codec->spec;
2087 struct nid_path *path;
2088 unsigned int val;
2089 int err, idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090
Takashi Iwai352f7f92012-12-19 12:52:06 +01002091 if (!nid_has_volume(codec, mix_nid, HDA_INPUT) &&
2092 !nid_has_mute(codec, mix_nid, HDA_INPUT))
2093 return 0; /* no need for analog loopback */
2094
Takashi Iwai3ca529d2013-01-07 17:25:08 +01002095 path = snd_hda_add_new_path(codec, pin, mix_nid, 0);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002096 if (!path)
2097 return -EINVAL;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01002098 print_nid_path("loopback", path);
Takashi Iwai196c17662013-01-04 15:01:40 +01002099 spec->loopback_paths[input_idx] = snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002100
2101 idx = path->idx[path->depth - 1];
2102 if (nid_has_volume(codec, mix_nid, HDA_INPUT)) {
2103 val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
2104 err = __add_pb_vol_ctrl(spec, HDA_CTL_WIDGET_VOL, ctlname, ctlidx, val);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002105 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002107 path->ctls[NID_PATH_VOL_CTL] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 }
2109
Takashi Iwai352f7f92012-12-19 12:52:06 +01002110 if (nid_has_mute(codec, mix_nid, HDA_INPUT)) {
2111 val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
2112 err = __add_pb_sw_ctrl(spec, HDA_CTL_WIDGET_MUTE, ctlname, ctlidx, val);
Takashi Iwaid13bd412008-07-30 15:01:45 +02002113 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002115 path->ctls[NID_PATH_MUTE_CTL] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 }
2117
Takashi Iwai352f7f92012-12-19 12:52:06 +01002118 path->active = true;
2119 add_loopback_list(spec, mix_nid, idx);
2120 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121}
2122
Takashi Iwai352f7f92012-12-19 12:52:06 +01002123static int is_input_pin(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002125 unsigned int pincap = snd_hda_query_pin_caps(codec, nid);
2126 return (pincap & AC_PINCAP_IN) != 0;
2127}
2128
2129/* Parse the codec tree and retrieve ADCs */
2130static int fill_adc_nids(struct hda_codec *codec)
2131{
2132 struct hda_gen_spec *spec = codec->spec;
2133 hda_nid_t nid;
2134 hda_nid_t *adc_nids = spec->adc_nids;
2135 int max_nums = ARRAY_SIZE(spec->adc_nids);
2136 int i, nums = 0;
2137
2138 nid = codec->start_nid;
2139 for (i = 0; i < codec->num_nodes; i++, nid++) {
2140 unsigned int caps = get_wcaps(codec, nid);
2141 int type = get_wcaps_type(caps);
2142
2143 if (type != AC_WID_AUD_IN || (caps & AC_WCAP_DIGITAL))
2144 continue;
2145 adc_nids[nums] = nid;
2146 if (++nums >= max_nums)
2147 break;
2148 }
2149 spec->num_adc_nids = nums;
2150 return nums;
2151}
2152
2153/* filter out invalid adc_nids that don't give all active input pins;
2154 * if needed, check whether dynamic ADC-switching is available
2155 */
2156static int check_dyn_adc_switch(struct hda_codec *codec)
2157{
2158 struct hda_gen_spec *spec = codec->spec;
2159 struct hda_input_mux *imux = &spec->input_mux;
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002160 unsigned int ok_bits;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002161 int i, n, nums;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002162
2163 again:
2164 nums = 0;
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002165 ok_bits = 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002166 for (n = 0; n < spec->num_adc_nids; n++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002167 for (i = 0; i < imux->num_items; i++) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002168 if (!spec->input_paths[i][n])
Takashi Iwai352f7f92012-12-19 12:52:06 +01002169 break;
2170 }
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002171 if (i >= imux->num_items) {
2172 ok_bits |= (1 << n);
2173 nums++;
2174 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002175 }
2176
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002177 if (!ok_bits) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002178 if (spec->shared_mic_hp) {
2179 spec->shared_mic_hp = 0;
2180 imux->num_items = 1;
2181 goto again;
2182 }
2183
2184 /* check whether ADC-switch is possible */
2185 for (i = 0; i < imux->num_items; i++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002186 for (n = 0; n < spec->num_adc_nids; n++) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002187 if (spec->input_paths[i][n]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002188 spec->dyn_adc_idx[i] = n;
2189 break;
2190 }
2191 }
2192 }
2193
2194 snd_printdd("hda-codec: enabling ADC switching\n");
2195 spec->dyn_adc_switch = 1;
2196 } else if (nums != spec->num_adc_nids) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002197 /* shrink the invalid adcs and input paths */
2198 nums = 0;
2199 for (n = 0; n < spec->num_adc_nids; n++) {
2200 if (!(ok_bits & (1 << n)))
2201 continue;
2202 if (n != nums) {
2203 spec->adc_nids[nums] = spec->adc_nids[n];
Takashi Iwai980428c2013-01-09 09:28:20 +01002204 for (i = 0; i < imux->num_items; i++) {
2205 invalidate_nid_path(codec,
2206 spec->input_paths[i][nums]);
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002207 spec->input_paths[i][nums] =
2208 spec->input_paths[i][n];
Takashi Iwai980428c2013-01-09 09:28:20 +01002209 }
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002210 }
2211 nums++;
2212 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002213 spec->num_adc_nids = nums;
2214 }
2215
2216 if (imux->num_items == 1 || spec->shared_mic_hp) {
2217 snd_printdd("hda-codec: reducing to a single ADC\n");
2218 spec->num_adc_nids = 1; /* reduce to a single ADC */
2219 }
2220
2221 /* single index for individual volumes ctls */
2222 if (!spec->dyn_adc_switch && spec->multi_cap_vol)
2223 spec->num_adc_nids = 1;
2224
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 return 0;
2226}
2227
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002228/* parse capture source paths from the given pin and create imux items */
2229static int parse_capture_source(struct hda_codec *codec, hda_nid_t pin,
2230 int num_adcs, const char *label, int anchor)
2231{
2232 struct hda_gen_spec *spec = codec->spec;
2233 struct hda_input_mux *imux = &spec->input_mux;
2234 int imux_idx = imux->num_items;
2235 bool imux_added = false;
2236 int c;
2237
2238 for (c = 0; c < num_adcs; c++) {
2239 struct nid_path *path;
2240 hda_nid_t adc = spec->adc_nids[c];
2241
2242 if (!is_reachable_path(codec, pin, adc))
2243 continue;
2244 path = snd_hda_add_new_path(codec, pin, adc, anchor);
2245 if (!path)
2246 continue;
2247 print_nid_path("input", path);
2248 spec->input_paths[imux_idx][c] =
2249 snd_hda_get_path_idx(codec, path);
2250
2251 if (!imux_added) {
2252 spec->imux_pins[imux->num_items] = pin;
2253 snd_hda_add_imux_item(imux, label,
2254 imux->num_items, NULL);
2255 imux_added = true;
2256 }
2257 }
2258
2259 return 0;
2260}
2261
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01002263 * create playback/capture controls for input pins
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01002265static int create_input_ctls(struct hda_codec *codec)
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02002266{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002267 struct hda_gen_spec *spec = codec->spec;
2268 const struct auto_pin_cfg *cfg = &spec->autocfg;
2269 hda_nid_t mixer = spec->mixer_nid;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002270 int num_adcs;
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002271 int i, err, type_idx = 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002272 const char *prev_label = NULL;
Takashi Iwai2c12c302013-01-10 09:33:29 +01002273 unsigned int val;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02002274
Takashi Iwai352f7f92012-12-19 12:52:06 +01002275 num_adcs = fill_adc_nids(codec);
2276 if (num_adcs < 0)
2277 return 0;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02002278
Takashi Iwai352f7f92012-12-19 12:52:06 +01002279 for (i = 0; i < cfg->num_inputs; i++) {
2280 hda_nid_t pin;
2281 const char *label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282
Takashi Iwai352f7f92012-12-19 12:52:06 +01002283 pin = cfg->inputs[i].pin;
2284 if (!is_input_pin(codec, pin))
2285 continue;
2286
2287 label = hda_get_autocfg_input_label(codec, cfg, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002288 if (prev_label && !strcmp(label, prev_label))
2289 type_idx++;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02002290 else
Takashi Iwai352f7f92012-12-19 12:52:06 +01002291 type_idx = 0;
2292 prev_label = label;
2293
Takashi Iwai2c12c302013-01-10 09:33:29 +01002294 val = PIN_IN;
2295 if (cfg->inputs[i].type == AUTO_PIN_MIC)
2296 val |= snd_hda_get_default_vref(codec, pin);
2297 set_pin_target(codec, pin, val, false);
2298
Takashi Iwai352f7f92012-12-19 12:52:06 +01002299 if (mixer) {
2300 if (is_reachable_path(codec, pin, mixer)) {
Takashi Iwai196c17662013-01-04 15:01:40 +01002301 err = new_analog_input(codec, i, pin,
Takashi Iwai352f7f92012-12-19 12:52:06 +01002302 label, type_idx, mixer);
2303 if (err < 0)
2304 return err;
2305 }
2306 }
2307
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002308 err = parse_capture_source(codec, pin, num_adcs, label, -mixer);
2309 if (err < 0)
2310 return err;
2311 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002312
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002313 if (mixer && spec->add_stereo_mix_input) {
2314 err = parse_capture_source(codec, mixer, num_adcs,
2315 "Stereo Mix", 0);
2316 if (err < 0)
2317 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002318 }
2319
2320 return 0;
2321}
2322
2323
2324/*
2325 * input source mux
2326 */
2327
Takashi Iwaic697b712013-01-07 17:09:26 +01002328/* get the input path specified by the given adc and imux indices */
2329static struct nid_path *get_input_path(struct hda_codec *codec, int adc_idx, int imux_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002330{
2331 struct hda_gen_spec *spec = codec->spec;
2332 if (spec->dyn_adc_switch)
2333 adc_idx = spec->dyn_adc_idx[imux_idx];
Takashi Iwaic697b712013-01-07 17:09:26 +01002334 return snd_hda_get_path_from_idx(codec, spec->input_paths[imux_idx][adc_idx]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002335}
2336
2337static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
2338 unsigned int idx);
2339
2340static int mux_enum_info(struct snd_kcontrol *kcontrol,
2341 struct snd_ctl_elem_info *uinfo)
2342{
2343 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2344 struct hda_gen_spec *spec = codec->spec;
2345 return snd_hda_input_mux_info(&spec->input_mux, uinfo);
2346}
2347
2348static int mux_enum_get(struct snd_kcontrol *kcontrol,
2349 struct snd_ctl_elem_value *ucontrol)
2350{
2351 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2352 struct hda_gen_spec *spec = codec->spec;
2353 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
2354
2355 ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
2356 return 0;
2357}
2358
2359static int mux_enum_put(struct snd_kcontrol *kcontrol,
2360 struct snd_ctl_elem_value *ucontrol)
2361{
2362 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2363 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
2364 return mux_select(codec, adc_idx,
2365 ucontrol->value.enumerated.item[0]);
2366}
2367
Takashi Iwai352f7f92012-12-19 12:52:06 +01002368static const struct snd_kcontrol_new cap_src_temp = {
2369 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2370 .name = "Input Source",
2371 .info = mux_enum_info,
2372 .get = mux_enum_get,
2373 .put = mux_enum_put,
2374};
2375
Takashi Iwai47d46ab2012-12-20 11:48:54 +01002376/*
2377 * capture volume and capture switch ctls
2378 */
2379
Takashi Iwai352f7f92012-12-19 12:52:06 +01002380typedef int (*put_call_t)(struct snd_kcontrol *kcontrol,
2381 struct snd_ctl_elem_value *ucontrol);
2382
Takashi Iwai47d46ab2012-12-20 11:48:54 +01002383/* call the given amp update function for all amps in the imux list at once */
Takashi Iwai352f7f92012-12-19 12:52:06 +01002384static int cap_put_caller(struct snd_kcontrol *kcontrol,
2385 struct snd_ctl_elem_value *ucontrol,
2386 put_call_t func, int type)
2387{
2388 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2389 struct hda_gen_spec *spec = codec->spec;
2390 const struct hda_input_mux *imux;
2391 struct nid_path *path;
2392 int i, adc_idx, err = 0;
2393
2394 imux = &spec->input_mux;
2395 adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
2396 mutex_lock(&codec->control_mutex);
Takashi Iwai47d46ab2012-12-20 11:48:54 +01002397 /* we use the cache-only update at first since multiple input paths
2398 * may shared the same amp; by updating only caches, the redundant
2399 * writes to hardware can be reduced.
2400 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01002401 codec->cached_write = 1;
2402 for (i = 0; i < imux->num_items; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01002403 path = get_input_path(codec, adc_idx, i);
2404 if (!path || !path->ctls[type])
Takashi Iwai352f7f92012-12-19 12:52:06 +01002405 continue;
2406 kcontrol->private_value = path->ctls[type];
2407 err = func(kcontrol, ucontrol);
2408 if (err < 0)
2409 goto error;
2410 }
2411 error:
2412 codec->cached_write = 0;
2413 mutex_unlock(&codec->control_mutex);
Takashi Iwai47d46ab2012-12-20 11:48:54 +01002414 snd_hda_codec_flush_amp_cache(codec); /* flush the updates */
Takashi Iwai352f7f92012-12-19 12:52:06 +01002415 if (err >= 0 && spec->cap_sync_hook)
2416 spec->cap_sync_hook(codec);
2417 return err;
2418}
2419
2420/* capture volume ctl callbacks */
2421#define cap_vol_info snd_hda_mixer_amp_volume_info
2422#define cap_vol_get snd_hda_mixer_amp_volume_get
2423#define cap_vol_tlv snd_hda_mixer_amp_tlv
2424
2425static int cap_vol_put(struct snd_kcontrol *kcontrol,
2426 struct snd_ctl_elem_value *ucontrol)
2427{
2428 return cap_put_caller(kcontrol, ucontrol,
2429 snd_hda_mixer_amp_volume_put,
2430 NID_PATH_VOL_CTL);
2431}
2432
2433static const struct snd_kcontrol_new cap_vol_temp = {
2434 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2435 .name = "Capture Volume",
2436 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
2437 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
2438 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK),
2439 .info = cap_vol_info,
2440 .get = cap_vol_get,
2441 .put = cap_vol_put,
2442 .tlv = { .c = cap_vol_tlv },
2443};
2444
2445/* capture switch ctl callbacks */
2446#define cap_sw_info snd_ctl_boolean_stereo_info
2447#define cap_sw_get snd_hda_mixer_amp_switch_get
2448
2449static int cap_sw_put(struct snd_kcontrol *kcontrol,
2450 struct snd_ctl_elem_value *ucontrol)
2451{
2452 return cap_put_caller(kcontrol, ucontrol,
2453 snd_hda_mixer_amp_switch_put,
2454 NID_PATH_MUTE_CTL);
2455}
2456
2457static const struct snd_kcontrol_new cap_sw_temp = {
2458 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2459 .name = "Capture Switch",
2460 .info = cap_sw_info,
2461 .get = cap_sw_get,
2462 .put = cap_sw_put,
2463};
2464
2465static int parse_capvol_in_path(struct hda_codec *codec, struct nid_path *path)
2466{
2467 hda_nid_t nid;
2468 int i, depth;
2469
2470 path->ctls[NID_PATH_VOL_CTL] = path->ctls[NID_PATH_MUTE_CTL] = 0;
2471 for (depth = 0; depth < 3; depth++) {
2472 if (depth >= path->depth)
2473 return -EINVAL;
2474 i = path->depth - depth - 1;
2475 nid = path->path[i];
2476 if (!path->ctls[NID_PATH_VOL_CTL]) {
2477 if (nid_has_volume(codec, nid, HDA_OUTPUT))
2478 path->ctls[NID_PATH_VOL_CTL] =
2479 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
2480 else if (nid_has_volume(codec, nid, HDA_INPUT)) {
2481 int idx = path->idx[i];
2482 if (!depth && codec->single_adc_amp)
2483 idx = 0;
2484 path->ctls[NID_PATH_VOL_CTL] =
2485 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
2486 }
2487 }
2488 if (!path->ctls[NID_PATH_MUTE_CTL]) {
2489 if (nid_has_mute(codec, nid, HDA_OUTPUT))
2490 path->ctls[NID_PATH_MUTE_CTL] =
2491 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
2492 else if (nid_has_mute(codec, nid, HDA_INPUT)) {
2493 int idx = path->idx[i];
2494 if (!depth && codec->single_adc_amp)
2495 idx = 0;
2496 path->ctls[NID_PATH_MUTE_CTL] =
2497 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
2498 }
2499 }
Takashi Iwai97ec5582006-03-21 11:29:07 +01002500 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501 return 0;
2502}
2503
Takashi Iwai352f7f92012-12-19 12:52:06 +01002504static bool is_inv_dmic_pin(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002506 struct hda_gen_spec *spec = codec->spec;
2507 struct auto_pin_cfg *cfg = &spec->autocfg;
2508 unsigned int val;
2509 int i;
2510
2511 if (!spec->inv_dmic_split)
2512 return false;
2513 for (i = 0; i < cfg->num_inputs; i++) {
2514 if (cfg->inputs[i].pin != nid)
2515 continue;
2516 if (cfg->inputs[i].type != AUTO_PIN_MIC)
2517 return false;
2518 val = snd_hda_codec_get_pincfg(codec, nid);
2519 return snd_hda_get_input_pin_attr(val) == INPUT_PIN_ATTR_INT;
2520 }
2521 return false;
2522}
2523
2524static int add_single_cap_ctl(struct hda_codec *codec, const char *label,
2525 int idx, bool is_switch, unsigned int ctl,
2526 bool inv_dmic)
2527{
2528 struct hda_gen_spec *spec = codec->spec;
2529 char tmpname[44];
2530 int type = is_switch ? HDA_CTL_WIDGET_MUTE : HDA_CTL_WIDGET_VOL;
2531 const char *sfx = is_switch ? "Switch" : "Volume";
2532 unsigned int chs = inv_dmic ? 1 : 3;
2533 int err;
2534
2535 if (!ctl)
2536 return 0;
2537
2538 if (label)
2539 snprintf(tmpname, sizeof(tmpname),
2540 "%s Capture %s", label, sfx);
2541 else
2542 snprintf(tmpname, sizeof(tmpname),
2543 "Capture %s", sfx);
2544 err = add_control(spec, type, tmpname, idx,
2545 amp_val_replace_channels(ctl, chs));
2546 if (err < 0 || !inv_dmic)
2547 return err;
2548
2549 /* Make independent right kcontrol */
2550 if (label)
2551 snprintf(tmpname, sizeof(tmpname),
2552 "Inverted %s Capture %s", label, sfx);
2553 else
2554 snprintf(tmpname, sizeof(tmpname),
2555 "Inverted Capture %s", sfx);
2556 return add_control(spec, type, tmpname, idx,
2557 amp_val_replace_channels(ctl, 2));
2558}
2559
2560/* create single (and simple) capture volume and switch controls */
2561static int create_single_cap_vol_ctl(struct hda_codec *codec, int idx,
2562 unsigned int vol_ctl, unsigned int sw_ctl,
2563 bool inv_dmic)
2564{
2565 int err;
2566 err = add_single_cap_ctl(codec, NULL, idx, false, vol_ctl, inv_dmic);
2567 if (err < 0)
2568 return err;
2569 err = add_single_cap_ctl(codec, NULL, idx, true, sw_ctl, inv_dmic);
2570 if (err < 0)
2571 return err;
2572 return 0;
2573}
2574
2575/* create bound capture volume and switch controls */
2576static int create_bind_cap_vol_ctl(struct hda_codec *codec, int idx,
2577 unsigned int vol_ctl, unsigned int sw_ctl)
2578{
2579 struct hda_gen_spec *spec = codec->spec;
2580 struct snd_kcontrol_new *knew;
2581
2582 if (vol_ctl) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01002583 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_vol_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002584 if (!knew)
2585 return -ENOMEM;
2586 knew->index = idx;
2587 knew->private_value = vol_ctl;
2588 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
2589 }
2590 if (sw_ctl) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01002591 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_sw_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002592 if (!knew)
2593 return -ENOMEM;
2594 knew->index = idx;
2595 knew->private_value = sw_ctl;
2596 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
2597 }
2598 return 0;
2599}
2600
2601/* return the vol ctl when used first in the imux list */
2602static unsigned int get_first_cap_ctl(struct hda_codec *codec, int idx, int type)
2603{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002604 struct nid_path *path;
2605 unsigned int ctl;
2606 int i;
2607
Takashi Iwaic697b712013-01-07 17:09:26 +01002608 path = get_input_path(codec, 0, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002609 if (!path)
2610 return 0;
2611 ctl = path->ctls[type];
2612 if (!ctl)
2613 return 0;
2614 for (i = 0; i < idx - 1; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01002615 path = get_input_path(codec, 0, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002616 if (path && path->ctls[type] == ctl)
2617 return 0;
2618 }
2619 return ctl;
2620}
2621
2622/* create individual capture volume and switch controls per input */
2623static int create_multi_cap_vol_ctl(struct hda_codec *codec)
2624{
2625 struct hda_gen_spec *spec = codec->spec;
2626 struct hda_input_mux *imux = &spec->input_mux;
2627 int i, err, type, type_idx = 0;
2628 const char *prev_label = NULL;
2629
2630 for (i = 0; i < imux->num_items; i++) {
2631 const char *label;
2632 bool inv_dmic;
2633 label = hda_get_autocfg_input_label(codec, &spec->autocfg, i);
2634 if (prev_label && !strcmp(label, prev_label))
2635 type_idx++;
2636 else
2637 type_idx = 0;
2638 prev_label = label;
2639 inv_dmic = is_inv_dmic_pin(codec, spec->imux_pins[i]);
2640
2641 for (type = 0; type < 2; type++) {
2642 err = add_single_cap_ctl(codec, label, type_idx, type,
2643 get_first_cap_ctl(codec, i, type),
2644 inv_dmic);
2645 if (err < 0)
2646 return err;
2647 }
2648 }
2649 return 0;
2650}
2651
2652static int create_capture_mixers(struct hda_codec *codec)
2653{
2654 struct hda_gen_spec *spec = codec->spec;
2655 struct hda_input_mux *imux = &spec->input_mux;
2656 int i, n, nums, err;
2657
2658 if (spec->dyn_adc_switch)
2659 nums = 1;
2660 else
2661 nums = spec->num_adc_nids;
2662
2663 if (!spec->auto_mic && imux->num_items > 1) {
2664 struct snd_kcontrol_new *knew;
Takashi Iwai624d9142012-12-19 17:41:52 +01002665 const char *name;
2666 name = nums > 1 ? "Input Source" : "Capture Source";
2667 knew = snd_hda_gen_add_kctl(spec, name, &cap_src_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002668 if (!knew)
2669 return -ENOMEM;
2670 knew->count = nums;
2671 }
2672
2673 for (n = 0; n < nums; n++) {
2674 bool multi = false;
2675 bool inv_dmic = false;
2676 int vol, sw;
2677
2678 vol = sw = 0;
2679 for (i = 0; i < imux->num_items; i++) {
2680 struct nid_path *path;
Takashi Iwaic697b712013-01-07 17:09:26 +01002681 path = get_input_path(codec, n, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002682 if (!path)
2683 continue;
2684 parse_capvol_in_path(codec, path);
2685 if (!vol)
2686 vol = path->ctls[NID_PATH_VOL_CTL];
2687 else if (vol != path->ctls[NID_PATH_VOL_CTL])
2688 multi = true;
2689 if (!sw)
2690 sw = path->ctls[NID_PATH_MUTE_CTL];
2691 else if (sw != path->ctls[NID_PATH_MUTE_CTL])
2692 multi = true;
2693 if (is_inv_dmic_pin(codec, spec->imux_pins[i]))
2694 inv_dmic = true;
2695 }
2696
2697 if (!multi)
2698 err = create_single_cap_vol_ctl(codec, n, vol, sw,
2699 inv_dmic);
2700 else if (!spec->multi_cap_vol)
2701 err = create_bind_cap_vol_ctl(codec, n, vol, sw);
2702 else
2703 err = create_multi_cap_vol_ctl(codec);
2704 if (err < 0)
2705 return err;
2706 }
2707
2708 return 0;
2709}
2710
2711/*
2712 * add mic boosts if needed
2713 */
2714static int parse_mic_boost(struct hda_codec *codec)
2715{
2716 struct hda_gen_spec *spec = codec->spec;
2717 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai071c73a2006-08-23 18:34:06 +02002718 int i, err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002719 int type_idx = 0;
2720 hda_nid_t nid;
2721 const char *prev_label = NULL;
2722
2723 for (i = 0; i < cfg->num_inputs; i++) {
2724 if (cfg->inputs[i].type > AUTO_PIN_MIC)
2725 break;
2726 nid = cfg->inputs[i].pin;
2727 if (get_wcaps(codec, nid) & AC_WCAP_IN_AMP) {
2728 const char *label;
Takashi Iwai5abd4882013-01-07 09:43:18 +01002729 char boost_label[44];
Takashi Iwai352f7f92012-12-19 12:52:06 +01002730 struct nid_path *path;
2731 unsigned int val;
2732
2733 label = hda_get_autocfg_input_label(codec, cfg, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002734 if (prev_label && !strcmp(label, prev_label))
2735 type_idx++;
2736 else
2737 type_idx = 0;
2738 prev_label = label;
2739
2740 snprintf(boost_label, sizeof(boost_label),
2741 "%s Boost Volume", label);
2742 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT);
2743 err = add_control(spec, HDA_CTL_WIDGET_VOL,
2744 boost_label, type_idx, val);
2745 if (err < 0)
2746 return err;
2747
2748 path = snd_hda_get_nid_path(codec, nid, 0);
2749 if (path)
2750 path->ctls[NID_PATH_BOOST_CTL] = val;
2751 }
2752 }
2753 return 0;
2754}
2755
2756/*
2757 * parse digital I/Os and set up NIDs in BIOS auto-parse mode
2758 */
2759static void parse_digital(struct hda_codec *codec)
2760{
2761 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01002762 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002763 int i, nums;
Takashi Iwai2c12c302013-01-10 09:33:29 +01002764 hda_nid_t dig_nid, pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002765
2766 /* support multiple SPDIFs; the secondary is set up as a slave */
2767 nums = 0;
2768 for (i = 0; i < spec->autocfg.dig_outs; i++) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01002769 pin = spec->autocfg.dig_out_pins[i];
Takashi Iwai352f7f92012-12-19 12:52:06 +01002770 dig_nid = look_for_dac(codec, pin, true);
2771 if (!dig_nid)
2772 continue;
Takashi Iwai3ca529d2013-01-07 17:25:08 +01002773 path = snd_hda_add_new_path(codec, dig_nid, pin, 0);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01002774 if (!path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002775 continue;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01002776 print_nid_path("digout", path);
Takashi Iwaie1284af2013-01-03 16:33:02 +01002777 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +01002778 spec->digout_paths[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai2c12c302013-01-10 09:33:29 +01002779 set_pin_target(codec, pin, PIN_OUT, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002780 if (!nums) {
2781 spec->multiout.dig_out_nid = dig_nid;
2782 spec->dig_out_type = spec->autocfg.dig_out_type[0];
2783 } else {
2784 spec->multiout.slave_dig_outs = spec->slave_dig_outs;
2785 if (nums >= ARRAY_SIZE(spec->slave_dig_outs) - 1)
2786 break;
2787 spec->slave_dig_outs[nums - 1] = dig_nid;
2788 }
2789 nums++;
2790 }
2791
2792 if (spec->autocfg.dig_in_pin) {
Takashi Iwai2c12c302013-01-10 09:33:29 +01002793 pin = spec->autocfg.dig_in_pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002794 dig_nid = codec->start_nid;
2795 for (i = 0; i < codec->num_nodes; i++, dig_nid++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002796 unsigned int wcaps = get_wcaps(codec, dig_nid);
2797 if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
2798 continue;
2799 if (!(wcaps & AC_WCAP_DIGITAL))
2800 continue;
Takashi Iwai2c12c302013-01-10 09:33:29 +01002801 path = snd_hda_add_new_path(codec, pin, dig_nid, 0);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002802 if (path) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01002803 print_nid_path("digin", path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002804 path->active = true;
2805 spec->dig_in_nid = dig_nid;
Takashi Iwai2430d7b2013-01-04 15:09:42 +01002806 spec->digin_path = snd_hda_get_path_idx(codec, path);
Takashi Iwai2c12c302013-01-10 09:33:29 +01002807 set_pin_target(codec, pin, PIN_IN, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002808 break;
2809 }
2810 }
2811 }
2812}
2813
2814
2815/*
2816 * input MUX handling
2817 */
2818
2819static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur);
2820
2821/* select the given imux item; either unmute exclusively or select the route */
2822static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
2823 unsigned int idx)
2824{
2825 struct hda_gen_spec *spec = codec->spec;
2826 const struct hda_input_mux *imux;
2827 struct nid_path *path;
2828
2829 imux = &spec->input_mux;
2830 if (!imux->num_items)
2831 return 0;
2832
2833 if (idx >= imux->num_items)
2834 idx = imux->num_items - 1;
2835 if (spec->cur_mux[adc_idx] == idx)
2836 return 0;
2837
Takashi Iwaic697b712013-01-07 17:09:26 +01002838 path = get_input_path(codec, adc_idx, spec->cur_mux[adc_idx]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002839 if (!path)
2840 return 0;
2841 if (path->active)
2842 snd_hda_activate_path(codec, path, false, false);
2843
2844 spec->cur_mux[adc_idx] = idx;
2845
2846 if (spec->shared_mic_hp)
2847 update_shared_mic_hp(codec, spec->cur_mux[adc_idx]);
2848
2849 if (spec->dyn_adc_switch)
2850 dyn_adc_pcm_resetup(codec, idx);
2851
Takashi Iwaic697b712013-01-07 17:09:26 +01002852 path = get_input_path(codec, adc_idx, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002853 if (!path)
2854 return 0;
2855 if (path->active)
2856 return 0;
2857 snd_hda_activate_path(codec, path, true, false);
2858 if (spec->cap_sync_hook)
2859 spec->cap_sync_hook(codec);
2860 return 1;
2861}
2862
2863
2864/*
2865 * Jack detections for HP auto-mute and mic-switch
2866 */
2867
2868/* check each pin in the given array; returns true if any of them is plugged */
2869static bool detect_jacks(struct hda_codec *codec, int num_pins, hda_nid_t *pins)
2870{
2871 int i, present = 0;
2872
2873 for (i = 0; i < num_pins; i++) {
2874 hda_nid_t nid = pins[i];
2875 if (!nid)
2876 break;
Takashi Iwai0b4df932013-01-10 09:45:13 +01002877 /* don't detect pins retasked as inputs */
2878 if (snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_IN_EN)
2879 continue;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002880 present |= snd_hda_jack_detect(codec, nid);
2881 }
2882 return present;
2883}
2884
2885/* standard HP/line-out auto-mute helper */
2886static void do_automute(struct hda_codec *codec, int num_pins, hda_nid_t *pins,
Takashi Iwai2c12c302013-01-10 09:33:29 +01002887 bool mute)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002888{
2889 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002890 int i;
2891
2892 for (i = 0; i < num_pins; i++) {
2893 hda_nid_t nid = pins[i];
2894 unsigned int val;
2895 if (!nid)
2896 break;
2897 /* don't reset VREF value in case it's controlling
2898 * the amp (see alc861_fixup_asus_amp_vref_0f())
2899 */
Takashi Iwai2c12c302013-01-10 09:33:29 +01002900 if (spec->keep_vref_in_automute)
2901 val = snd_hda_codec_get_pin_target(codec, nid) & ~PIN_HP;
2902 else
Takashi Iwai352f7f92012-12-19 12:52:06 +01002903 val = 0;
Takashi Iwai2c12c302013-01-10 09:33:29 +01002904 if (!mute)
2905 val |= snd_hda_codec_get_pin_target(codec, nid);
2906 /* here we call update_pin_ctl() so that the pinctl is changed
2907 * without changing the pinctl target value;
2908 * the original target value will be still referred at the
2909 * init / resume again
2910 */
2911 update_pin_ctl(codec, nid, val);
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01002912 set_pin_eapd(codec, nid, !mute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002913 }
2914}
2915
2916/* Toggle outputs muting */
Takashi Iwai5d550e12012-12-19 15:16:44 +01002917void snd_hda_gen_update_outputs(struct hda_codec *codec)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002918{
2919 struct hda_gen_spec *spec = codec->spec;
2920 int on;
2921
2922 /* Control HP pins/amps depending on master_mute state;
2923 * in general, HP pins/amps control should be enabled in all cases,
2924 * but currently set only for master_mute, just to be safe
2925 */
2926 if (!spec->shared_mic_hp) /* don't change HP-pin when shared with mic */
2927 do_automute(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
Takashi Iwai2c12c302013-01-10 09:33:29 +01002928 spec->autocfg.hp_pins, spec->master_mute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002929
2930 if (!spec->automute_speaker)
2931 on = 0;
2932 else
2933 on = spec->hp_jack_present | spec->line_jack_present;
2934 on |= spec->master_mute;
2935 do_automute(codec, ARRAY_SIZE(spec->autocfg.speaker_pins),
Takashi Iwai2c12c302013-01-10 09:33:29 +01002936 spec->autocfg.speaker_pins, on);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002937
2938 /* toggle line-out mutes if needed, too */
2939 /* if LO is a copy of either HP or Speaker, don't need to handle it */
2940 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0] ||
2941 spec->autocfg.line_out_pins[0] == spec->autocfg.speaker_pins[0])
2942 return;
2943 if (!spec->automute_lo)
2944 on = 0;
2945 else
2946 on = spec->hp_jack_present;
2947 on |= spec->master_mute;
2948 do_automute(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
Takashi Iwai2c12c302013-01-10 09:33:29 +01002949 spec->autocfg.line_out_pins, on);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002950}
Takashi Iwai5d550e12012-12-19 15:16:44 +01002951EXPORT_SYMBOL_HDA(snd_hda_gen_update_outputs);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002952
2953static void call_update_outputs(struct hda_codec *codec)
2954{
2955 struct hda_gen_spec *spec = codec->spec;
2956 if (spec->automute_hook)
2957 spec->automute_hook(codec);
2958 else
Takashi Iwai5d550e12012-12-19 15:16:44 +01002959 snd_hda_gen_update_outputs(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002960}
2961
2962/* standard HP-automute helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01002963void snd_hda_gen_hp_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002964{
2965 struct hda_gen_spec *spec = codec->spec;
2966
2967 spec->hp_jack_present =
2968 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
2969 spec->autocfg.hp_pins);
2970 if (!spec->detect_hp || (!spec->automute_speaker && !spec->automute_lo))
2971 return;
2972 call_update_outputs(codec);
2973}
Takashi Iwai5d550e12012-12-19 15:16:44 +01002974EXPORT_SYMBOL_HDA(snd_hda_gen_hp_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002975
2976/* standard line-out-automute helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01002977void snd_hda_gen_line_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002978{
2979 struct hda_gen_spec *spec = codec->spec;
2980
2981 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
2982 return;
2983 /* check LO jack only when it's different from HP */
2984 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0])
2985 return;
2986
2987 spec->line_jack_present =
2988 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
2989 spec->autocfg.line_out_pins);
2990 if (!spec->automute_speaker || !spec->detect_lo)
2991 return;
2992 call_update_outputs(codec);
2993}
Takashi Iwai5d550e12012-12-19 15:16:44 +01002994EXPORT_SYMBOL_HDA(snd_hda_gen_line_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002995
2996/* standard mic auto-switch helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01002997void snd_hda_gen_mic_autoswitch(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002998{
2999 struct hda_gen_spec *spec = codec->spec;
3000 int i;
3001
3002 if (!spec->auto_mic)
3003 return;
3004
3005 for (i = spec->am_num_entries - 1; i > 0; i--) {
Takashi Iwai0b4df932013-01-10 09:45:13 +01003006 hda_nid_t pin = spec->am_entry[i].pin;
3007 /* don't detect pins retasked as outputs */
3008 if (snd_hda_codec_get_pin_target(codec, pin) & AC_PINCTL_OUT_EN)
3009 continue;
3010 if (snd_hda_jack_detect(codec, pin)) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01003011 mux_select(codec, 0, spec->am_entry[i].idx);
3012 return;
3013 }
3014 }
3015 mux_select(codec, 0, spec->am_entry[0].idx);
3016}
Takashi Iwai5d550e12012-12-19 15:16:44 +01003017EXPORT_SYMBOL_HDA(snd_hda_gen_mic_autoswitch);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003018
3019/*
3020 * Auto-Mute mode mixer enum support
3021 */
3022static int automute_mode_info(struct snd_kcontrol *kcontrol,
3023 struct snd_ctl_elem_info *uinfo)
3024{
3025 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3026 struct hda_gen_spec *spec = codec->spec;
3027 static const char * const texts3[] = {
3028 "Disabled", "Speaker Only", "Line Out+Speaker"
Takashi Iwai071c73a2006-08-23 18:34:06 +02003029 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030
Takashi Iwai352f7f92012-12-19 12:52:06 +01003031 if (spec->automute_speaker_possible && spec->automute_lo_possible)
3032 return snd_hda_enum_helper_info(kcontrol, uinfo, 3, texts3);
3033 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
3034}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035
Takashi Iwai352f7f92012-12-19 12:52:06 +01003036static int automute_mode_get(struct snd_kcontrol *kcontrol,
3037 struct snd_ctl_elem_value *ucontrol)
3038{
3039 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3040 struct hda_gen_spec *spec = codec->spec;
3041 unsigned int val = 0;
3042 if (spec->automute_speaker)
3043 val++;
3044 if (spec->automute_lo)
3045 val++;
Takashi Iwai071c73a2006-08-23 18:34:06 +02003046
Takashi Iwai352f7f92012-12-19 12:52:06 +01003047 ucontrol->value.enumerated.item[0] = val;
3048 return 0;
3049}
3050
3051static int automute_mode_put(struct snd_kcontrol *kcontrol,
3052 struct snd_ctl_elem_value *ucontrol)
3053{
3054 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3055 struct hda_gen_spec *spec = codec->spec;
3056
3057 switch (ucontrol->value.enumerated.item[0]) {
3058 case 0:
3059 if (!spec->automute_speaker && !spec->automute_lo)
3060 return 0;
3061 spec->automute_speaker = 0;
3062 spec->automute_lo = 0;
3063 break;
3064 case 1:
3065 if (spec->automute_speaker_possible) {
3066 if (!spec->automute_lo && spec->automute_speaker)
3067 return 0;
3068 spec->automute_speaker = 1;
3069 spec->automute_lo = 0;
3070 } else if (spec->automute_lo_possible) {
3071 if (spec->automute_lo)
3072 return 0;
3073 spec->automute_lo = 1;
3074 } else
3075 return -EINVAL;
3076 break;
3077 case 2:
3078 if (!spec->automute_lo_possible || !spec->automute_speaker_possible)
3079 return -EINVAL;
3080 if (spec->automute_speaker && spec->automute_lo)
3081 return 0;
3082 spec->automute_speaker = 1;
3083 spec->automute_lo = 1;
3084 break;
3085 default:
3086 return -EINVAL;
3087 }
3088 call_update_outputs(codec);
3089 return 1;
3090}
3091
3092static const struct snd_kcontrol_new automute_mode_enum = {
3093 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3094 .name = "Auto-Mute Mode",
3095 .info = automute_mode_info,
3096 .get = automute_mode_get,
3097 .put = automute_mode_put,
3098};
3099
3100static int add_automute_mode_enum(struct hda_codec *codec)
3101{
3102 struct hda_gen_spec *spec = codec->spec;
3103
Takashi Iwai12c93df2012-12-19 14:38:33 +01003104 if (!snd_hda_gen_add_kctl(spec, NULL, &automute_mode_enum))
Takashi Iwai352f7f92012-12-19 12:52:06 +01003105 return -ENOMEM;
3106 return 0;
3107}
3108
3109/*
3110 * Check the availability of HP/line-out auto-mute;
3111 * Set up appropriately if really supported
3112 */
3113static int check_auto_mute_availability(struct hda_codec *codec)
3114{
3115 struct hda_gen_spec *spec = codec->spec;
3116 struct auto_pin_cfg *cfg = &spec->autocfg;
3117 int present = 0;
3118 int i, err;
3119
3120 if (cfg->hp_pins[0])
3121 present++;
3122 if (cfg->line_out_pins[0])
3123 present++;
3124 if (cfg->speaker_pins[0])
3125 present++;
3126 if (present < 2) /* need two different output types */
Takashi Iwai071c73a2006-08-23 18:34:06 +02003127 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003128
3129 if (!cfg->speaker_pins[0] &&
3130 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
3131 memcpy(cfg->speaker_pins, cfg->line_out_pins,
3132 sizeof(cfg->speaker_pins));
3133 cfg->speaker_outs = cfg->line_outs;
Takashi Iwai071c73a2006-08-23 18:34:06 +02003134 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135
Takashi Iwai352f7f92012-12-19 12:52:06 +01003136 if (!cfg->hp_pins[0] &&
3137 cfg->line_out_type == AUTO_PIN_HP_OUT) {
3138 memcpy(cfg->hp_pins, cfg->line_out_pins,
3139 sizeof(cfg->hp_pins));
3140 cfg->hp_outs = cfg->line_outs;
3141 }
3142
3143 for (i = 0; i < cfg->hp_outs; i++) {
3144 hda_nid_t nid = cfg->hp_pins[i];
3145 if (!is_jack_detectable(codec, nid))
3146 continue;
3147 snd_printdd("hda-codec: Enable HP auto-muting on NID 0x%x\n",
3148 nid);
3149 snd_hda_jack_detect_enable_callback(codec, nid, HDA_GEN_HP_EVENT,
Takashi Iwai2e03e952013-01-03 15:55:06 +01003150 spec->hp_automute_hook ?
3151 spec->hp_automute_hook :
Takashi Iwai5d550e12012-12-19 15:16:44 +01003152 snd_hda_gen_hp_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003153 spec->detect_hp = 1;
3154 }
3155
3156 if (cfg->line_out_type == AUTO_PIN_LINE_OUT && cfg->line_outs) {
3157 if (cfg->speaker_outs)
3158 for (i = 0; i < cfg->line_outs; i++) {
3159 hda_nid_t nid = cfg->line_out_pins[i];
3160 if (!is_jack_detectable(codec, nid))
3161 continue;
3162 snd_printdd("hda-codec: Enable Line-Out auto-muting on NID 0x%x\n", nid);
3163 snd_hda_jack_detect_enable_callback(codec, nid,
3164 HDA_GEN_FRONT_EVENT,
Takashi Iwai2e03e952013-01-03 15:55:06 +01003165 spec->line_automute_hook ?
3166 spec->line_automute_hook :
Takashi Iwai5d550e12012-12-19 15:16:44 +01003167 snd_hda_gen_line_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003168 spec->detect_lo = 1;
3169 }
3170 spec->automute_lo_possible = spec->detect_hp;
3171 }
3172
3173 spec->automute_speaker_possible = cfg->speaker_outs &&
3174 (spec->detect_hp || spec->detect_lo);
3175
3176 spec->automute_lo = spec->automute_lo_possible;
3177 spec->automute_speaker = spec->automute_speaker_possible;
3178
3179 if (spec->automute_speaker_possible || spec->automute_lo_possible) {
3180 /* create a control for automute mode */
3181 err = add_automute_mode_enum(codec);
3182 if (err < 0)
3183 return err;
3184 }
3185 return 0;
3186}
3187
Takashi Iwai352f7f92012-12-19 12:52:06 +01003188/* check whether all auto-mic pins are valid; setup indices if OK */
3189static bool auto_mic_check_imux(struct hda_codec *codec)
3190{
3191 struct hda_gen_spec *spec = codec->spec;
3192 const struct hda_input_mux *imux;
3193 int i;
3194
3195 imux = &spec->input_mux;
3196 for (i = 0; i < spec->am_num_entries; i++) {
3197 spec->am_entry[i].idx =
3198 find_idx_in_nid_list(spec->am_entry[i].pin,
3199 spec->imux_pins, imux->num_items);
3200 if (spec->am_entry[i].idx < 0)
3201 return false; /* no corresponding imux */
3202 }
3203
3204 /* we don't need the jack detection for the first pin */
3205 for (i = 1; i < spec->am_num_entries; i++)
3206 snd_hda_jack_detect_enable_callback(codec,
3207 spec->am_entry[i].pin,
3208 HDA_GEN_MIC_EVENT,
Takashi Iwai2e03e952013-01-03 15:55:06 +01003209 spec->mic_autoswitch_hook ?
3210 spec->mic_autoswitch_hook :
Takashi Iwai5d550e12012-12-19 15:16:44 +01003211 snd_hda_gen_mic_autoswitch);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003212 return true;
3213}
3214
3215static int compare_attr(const void *ap, const void *bp)
3216{
3217 const struct automic_entry *a = ap;
3218 const struct automic_entry *b = bp;
3219 return (int)(a->attr - b->attr);
3220}
3221
3222/*
3223 * Check the availability of auto-mic switch;
3224 * Set up if really supported
3225 */
3226static int check_auto_mic_availability(struct hda_codec *codec)
3227{
3228 struct hda_gen_spec *spec = codec->spec;
3229 struct auto_pin_cfg *cfg = &spec->autocfg;
3230 unsigned int types;
3231 int i, num_pins;
3232
Takashi Iwaid12daf62013-01-07 16:32:11 +01003233 if (spec->suppress_auto_mic)
3234 return 0;
3235
Takashi Iwai352f7f92012-12-19 12:52:06 +01003236 types = 0;
3237 num_pins = 0;
3238 for (i = 0; i < cfg->num_inputs; i++) {
3239 hda_nid_t nid = cfg->inputs[i].pin;
3240 unsigned int attr;
3241 attr = snd_hda_codec_get_pincfg(codec, nid);
3242 attr = snd_hda_get_input_pin_attr(attr);
3243 if (types & (1 << attr))
3244 return 0; /* already occupied */
3245 switch (attr) {
3246 case INPUT_PIN_ATTR_INT:
3247 if (cfg->inputs[i].type != AUTO_PIN_MIC)
3248 return 0; /* invalid type */
3249 break;
3250 case INPUT_PIN_ATTR_UNUSED:
3251 return 0; /* invalid entry */
3252 default:
3253 if (cfg->inputs[i].type > AUTO_PIN_LINE_IN)
3254 return 0; /* invalid type */
3255 if (!spec->line_in_auto_switch &&
3256 cfg->inputs[i].type != AUTO_PIN_MIC)
3257 return 0; /* only mic is allowed */
3258 if (!is_jack_detectable(codec, nid))
3259 return 0; /* no unsol support */
3260 break;
3261 }
3262 if (num_pins >= MAX_AUTO_MIC_PINS)
3263 return 0;
3264 types |= (1 << attr);
3265 spec->am_entry[num_pins].pin = nid;
3266 spec->am_entry[num_pins].attr = attr;
3267 num_pins++;
3268 }
3269
3270 if (num_pins < 2)
3271 return 0;
3272
3273 spec->am_num_entries = num_pins;
3274 /* sort the am_entry in the order of attr so that the pin with a
3275 * higher attr will be selected when the jack is plugged.
3276 */
3277 sort(spec->am_entry, num_pins, sizeof(spec->am_entry[0]),
3278 compare_attr, NULL);
3279
3280 if (!auto_mic_check_imux(codec))
3281 return 0;
3282
3283 spec->auto_mic = 1;
3284 spec->num_adc_nids = 1;
3285 spec->cur_mux[0] = spec->am_entry[0].idx;
3286 snd_printdd("hda-codec: Enable auto-mic switch on NID 0x%x/0x%x/0x%x\n",
3287 spec->am_entry[0].pin,
3288 spec->am_entry[1].pin,
3289 spec->am_entry[2].pin);
3290
3291 return 0;
3292}
3293
3294
Takashi Iwai9eb413e2012-12-19 14:41:21 +01003295/*
3296 * Parse the given BIOS configuration and set up the hda_gen_spec
3297 *
3298 * return 1 if successful, 0 if the proper config is not found,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003299 * or a negative error code
3300 */
3301int snd_hda_gen_parse_auto_config(struct hda_codec *codec,
Takashi Iwai9eb413e2012-12-19 14:41:21 +01003302 struct auto_pin_cfg *cfg)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003303{
3304 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003305 int err;
3306
Takashi Iwai9eb413e2012-12-19 14:41:21 +01003307 if (cfg != &spec->autocfg) {
3308 spec->autocfg = *cfg;
3309 cfg = &spec->autocfg;
3310 }
3311
Takashi Iwai352f7f92012-12-19 12:52:06 +01003312 if (!cfg->line_outs) {
3313 if (cfg->dig_outs || cfg->dig_in_pin) {
3314 spec->multiout.max_channels = 2;
3315 spec->no_analog = 1;
3316 goto dig_only;
3317 }
3318 return 0; /* can't find valid BIOS pin config */
3319 }
3320
3321 if (!spec->no_primary_hp &&
3322 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT &&
3323 cfg->line_outs <= cfg->hp_outs) {
3324 /* use HP as primary out */
3325 cfg->speaker_outs = cfg->line_outs;
3326 memcpy(cfg->speaker_pins, cfg->line_out_pins,
3327 sizeof(cfg->speaker_pins));
3328 cfg->line_outs = cfg->hp_outs;
3329 memcpy(cfg->line_out_pins, cfg->hp_pins, sizeof(cfg->hp_pins));
3330 cfg->hp_outs = 0;
3331 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
3332 cfg->line_out_type = AUTO_PIN_HP_OUT;
3333 }
3334
3335 err = parse_output_paths(codec);
3336 if (err < 0)
3337 return err;
3338 err = create_multi_channel_mode(codec);
3339 if (err < 0)
3340 return err;
3341 err = create_multi_out_ctls(codec, cfg);
3342 if (err < 0)
3343 return err;
3344 err = create_hp_out_ctls(codec);
3345 if (err < 0)
3346 return err;
3347 err = create_speaker_out_ctls(codec);
3348 if (err < 0)
3349 return err;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003350 err = create_indep_hp_ctls(codec);
3351 if (err < 0)
3352 return err;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01003353 err = create_loopback_mixing_ctl(codec);
3354 if (err < 0)
3355 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003356 err = create_shared_input(codec);
3357 if (err < 0)
3358 return err;
3359 err = create_input_ctls(codec);
Takashi Iwaid13bd412008-07-30 15:01:45 +02003360 if (err < 0)
Takashi Iwai071c73a2006-08-23 18:34:06 +02003361 return err;
3362
Takashi Iwaia07a9492013-01-07 16:44:06 +01003363 spec->const_channel_count = spec->ext_channel_count;
3364 /* check the multiple speaker and headphone pins */
3365 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
3366 spec->const_channel_count = max(spec->const_channel_count,
3367 cfg->speaker_outs * 2);
3368 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
3369 spec->const_channel_count = max(spec->const_channel_count,
3370 cfg->hp_outs * 2);
3371 spec->multiout.max_channels = max(spec->ext_channel_count,
3372 spec->const_channel_count);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003373
3374 err = check_auto_mute_availability(codec);
3375 if (err < 0)
3376 return err;
3377
3378 err = check_dyn_adc_switch(codec);
3379 if (err < 0)
3380 return err;
3381
3382 if (!spec->shared_mic_hp) {
3383 err = check_auto_mic_availability(codec);
Takashi Iwaid13bd412008-07-30 15:01:45 +02003384 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003385 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003386 }
Takashi Iwai071c73a2006-08-23 18:34:06 +02003387
Takashi Iwai352f7f92012-12-19 12:52:06 +01003388 err = create_capture_mixers(codec);
3389 if (err < 0)
3390 return err;
3391
3392 err = parse_mic_boost(codec);
3393 if (err < 0)
3394 return err;
3395
Takashi Iwai978e77e2013-01-10 16:57:58 +01003396 if (spec->add_out_jack_modes) {
3397 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
3398 err = create_out_jack_modes(codec, cfg->line_outs,
3399 cfg->line_out_pins);
3400 if (err < 0)
3401 return err;
3402 }
3403 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
3404 err = create_out_jack_modes(codec, cfg->hp_outs,
3405 cfg->hp_pins);
3406 if (err < 0)
3407 return err;
3408 }
3409 }
3410
Takashi Iwai352f7f92012-12-19 12:52:06 +01003411 dig_only:
3412 parse_digital(codec);
3413
3414 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003415}
Takashi Iwai352f7f92012-12-19 12:52:06 +01003416EXPORT_SYMBOL_HDA(snd_hda_gen_parse_auto_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003417
3418
3419/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01003420 * Build control elements
Linus Torvalds1da177e2005-04-16 15:20:36 -07003421 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01003422
3423/* slave controls for virtual master */
3424static const char * const slave_pfxs[] = {
3425 "Front", "Surround", "Center", "LFE", "Side",
3426 "Headphone", "Speaker", "Mono", "Line Out",
3427 "CLFE", "Bass Speaker", "PCM",
Takashi Iwaiee79c692013-01-07 09:57:42 +01003428 "Speaker Front", "Speaker Surround", "Speaker CLFE", "Speaker Side",
3429 "Headphone Front", "Headphone Surround", "Headphone CLFE",
3430 "Headphone Side",
Takashi Iwai352f7f92012-12-19 12:52:06 +01003431 NULL,
3432};
3433
3434int snd_hda_gen_build_controls(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003435{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003436 struct hda_gen_spec *spec = codec->spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003437 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003438
Takashi Iwai36502d02012-12-19 15:15:10 +01003439 if (spec->kctls.used) {
3440 err = snd_hda_add_new_ctls(codec, spec->kctls.list);
3441 if (err < 0)
3442 return err;
3443 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003444
Takashi Iwai352f7f92012-12-19 12:52:06 +01003445 if (spec->multiout.dig_out_nid) {
3446 err = snd_hda_create_dig_out_ctls(codec,
3447 spec->multiout.dig_out_nid,
3448 spec->multiout.dig_out_nid,
3449 spec->pcm_rec[1].pcm_type);
3450 if (err < 0)
3451 return err;
3452 if (!spec->no_analog) {
3453 err = snd_hda_create_spdif_share_sw(codec,
3454 &spec->multiout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003455 if (err < 0)
3456 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003457 spec->multiout.share_spdif = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003458 }
3459 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003460 if (spec->dig_in_nid) {
3461 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
3462 if (err < 0)
3463 return err;
3464 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003465
Takashi Iwai352f7f92012-12-19 12:52:06 +01003466 /* if we have no master control, let's create it */
3467 if (!spec->no_analog &&
3468 !snd_hda_find_mixer_ctl(codec, "Master Playback Volume")) {
3469 unsigned int vmaster_tlv[4];
3470 snd_hda_set_vmaster_tlv(codec, spec->vmaster_nid,
3471 HDA_OUTPUT, vmaster_tlv);
3472 err = snd_hda_add_vmaster(codec, "Master Playback Volume",
3473 vmaster_tlv, slave_pfxs,
3474 "Playback Volume");
3475 if (err < 0)
3476 return err;
3477 }
3478 if (!spec->no_analog &&
3479 !snd_hda_find_mixer_ctl(codec, "Master Playback Switch")) {
3480 err = __snd_hda_add_vmaster(codec, "Master Playback Switch",
3481 NULL, slave_pfxs,
3482 "Playback Switch",
3483 true, &spec->vmaster_mute.sw_kctl);
3484 if (err < 0)
3485 return err;
3486 if (spec->vmaster_mute.hook)
Takashi Iwaifd25a972012-12-20 14:57:18 +01003487 snd_hda_add_vmaster_hook(codec, &spec->vmaster_mute,
3488 spec->vmaster_mute_enum);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003489 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003490
Takashi Iwai352f7f92012-12-19 12:52:06 +01003491 free_kctls(spec); /* no longer needed */
3492
3493 if (spec->shared_mic_hp) {
3494 int err;
3495 int nid = spec->autocfg.inputs[1].pin;
3496 err = snd_hda_jack_add_kctl(codec, nid, "Headphone Mic", 0);
3497 if (err < 0)
3498 return err;
3499 err = snd_hda_jack_detect_enable(codec, nid, 0);
3500 if (err < 0)
3501 return err;
3502 }
3503
3504 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
3505 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003506 return err;
3507
3508 return 0;
3509}
Takashi Iwai352f7f92012-12-19 12:52:06 +01003510EXPORT_SYMBOL_HDA(snd_hda_gen_build_controls);
3511
Linus Torvalds1da177e2005-04-16 15:20:36 -07003512
3513/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01003514 * PCM definitions
Linus Torvalds1da177e2005-04-16 15:20:36 -07003515 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003516
Takashi Iwaie6b85f32013-01-07 11:54:34 +01003517static void call_pcm_playback_hook(struct hda_pcm_stream *hinfo,
3518 struct hda_codec *codec,
3519 struct snd_pcm_substream *substream,
3520 int action)
3521{
3522 struct hda_gen_spec *spec = codec->spec;
3523 if (spec->pcm_playback_hook)
3524 spec->pcm_playback_hook(hinfo, codec, substream, action);
3525}
3526
Takashi Iwai352f7f92012-12-19 12:52:06 +01003527/*
3528 * Analog playback callbacks
3529 */
3530static int playback_pcm_open(struct hda_pcm_stream *hinfo,
3531 struct hda_codec *codec,
3532 struct snd_pcm_substream *substream)
3533{
3534 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003535 int err;
3536
3537 mutex_lock(&spec->pcm_mutex);
3538 err = snd_hda_multi_out_analog_open(codec,
3539 &spec->multiout, substream,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003540 hinfo);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01003541 if (!err) {
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003542 spec->active_streams |= 1 << STREAM_MULTI_OUT;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01003543 call_pcm_playback_hook(hinfo, codec, substream,
3544 HDA_GEN_PCM_ACT_OPEN);
3545 }
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003546 mutex_unlock(&spec->pcm_mutex);
3547 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003548}
3549
3550static int playback_pcm_prepare(struct hda_pcm_stream *hinfo,
Takashi Iwai97ec5582006-03-21 11:29:07 +01003551 struct hda_codec *codec,
3552 unsigned int stream_tag,
3553 unsigned int format,
3554 struct snd_pcm_substream *substream)
3555{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003556 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01003557 int err;
3558
3559 err = snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
3560 stream_tag, format, substream);
3561 if (!err)
3562 call_pcm_playback_hook(hinfo, codec, substream,
3563 HDA_GEN_PCM_ACT_PREPARE);
3564 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003565}
Takashi Iwai97ec5582006-03-21 11:29:07 +01003566
Takashi Iwai352f7f92012-12-19 12:52:06 +01003567static int playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
3568 struct hda_codec *codec,
3569 struct snd_pcm_substream *substream)
3570{
3571 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01003572 int err;
3573
3574 err = snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
3575 if (!err)
3576 call_pcm_playback_hook(hinfo, codec, substream,
3577 HDA_GEN_PCM_ACT_CLEANUP);
3578 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003579}
3580
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003581static int playback_pcm_close(struct hda_pcm_stream *hinfo,
3582 struct hda_codec *codec,
3583 struct snd_pcm_substream *substream)
3584{
3585 struct hda_gen_spec *spec = codec->spec;
3586 mutex_lock(&spec->pcm_mutex);
3587 spec->active_streams &= ~(1 << STREAM_MULTI_OUT);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01003588 call_pcm_playback_hook(hinfo, codec, substream,
3589 HDA_GEN_PCM_ACT_CLOSE);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003590 mutex_unlock(&spec->pcm_mutex);
3591 return 0;
3592}
3593
3594static int alt_playback_pcm_open(struct hda_pcm_stream *hinfo,
3595 struct hda_codec *codec,
3596 struct snd_pcm_substream *substream)
3597{
3598 struct hda_gen_spec *spec = codec->spec;
3599 int err = 0;
3600
3601 mutex_lock(&spec->pcm_mutex);
3602 if (!spec->indep_hp_enabled)
3603 err = -EBUSY;
3604 else
3605 spec->active_streams |= 1 << STREAM_INDEP_HP;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01003606 call_pcm_playback_hook(hinfo, codec, substream,
3607 HDA_GEN_PCM_ACT_OPEN);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003608 mutex_unlock(&spec->pcm_mutex);
3609 return err;
3610}
3611
3612static int alt_playback_pcm_close(struct hda_pcm_stream *hinfo,
3613 struct hda_codec *codec,
3614 struct snd_pcm_substream *substream)
3615{
3616 struct hda_gen_spec *spec = codec->spec;
3617 mutex_lock(&spec->pcm_mutex);
3618 spec->active_streams &= ~(1 << STREAM_INDEP_HP);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01003619 call_pcm_playback_hook(hinfo, codec, substream,
3620 HDA_GEN_PCM_ACT_CLOSE);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003621 mutex_unlock(&spec->pcm_mutex);
3622 return 0;
3623}
3624
Takashi Iwaie6b85f32013-01-07 11:54:34 +01003625static int alt_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
3626 struct hda_codec *codec,
3627 unsigned int stream_tag,
3628 unsigned int format,
3629 struct snd_pcm_substream *substream)
3630{
3631 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
3632 call_pcm_playback_hook(hinfo, codec, substream,
3633 HDA_GEN_PCM_ACT_PREPARE);
3634 return 0;
3635}
3636
3637static int alt_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
3638 struct hda_codec *codec,
3639 struct snd_pcm_substream *substream)
3640{
3641 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
3642 call_pcm_playback_hook(hinfo, codec, substream,
3643 HDA_GEN_PCM_ACT_CLEANUP);
3644 return 0;
3645}
3646
Takashi Iwai352f7f92012-12-19 12:52:06 +01003647/*
3648 * Digital out
3649 */
3650static int dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
3651 struct hda_codec *codec,
3652 struct snd_pcm_substream *substream)
3653{
3654 struct hda_gen_spec *spec = codec->spec;
3655 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
3656}
3657
3658static int dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
3659 struct hda_codec *codec,
3660 unsigned int stream_tag,
3661 unsigned int format,
3662 struct snd_pcm_substream *substream)
3663{
3664 struct hda_gen_spec *spec = codec->spec;
3665 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
3666 stream_tag, format, substream);
3667}
3668
3669static int dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
3670 struct hda_codec *codec,
3671 struct snd_pcm_substream *substream)
3672{
3673 struct hda_gen_spec *spec = codec->spec;
3674 return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
3675}
3676
3677static int dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
3678 struct hda_codec *codec,
3679 struct snd_pcm_substream *substream)
3680{
3681 struct hda_gen_spec *spec = codec->spec;
3682 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
3683}
3684
3685/*
3686 * Analog capture
3687 */
3688static int alt_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
3689 struct hda_codec *codec,
3690 unsigned int stream_tag,
3691 unsigned int format,
3692 struct snd_pcm_substream *substream)
3693{
3694 struct hda_gen_spec *spec = codec->spec;
3695
3696 snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number + 1],
Takashi Iwai97ec5582006-03-21 11:29:07 +01003697 stream_tag, 0, format);
3698 return 0;
3699}
3700
Takashi Iwai352f7f92012-12-19 12:52:06 +01003701static int alt_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
3702 struct hda_codec *codec,
3703 struct snd_pcm_substream *substream)
Takashi Iwai97ec5582006-03-21 11:29:07 +01003704{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003705 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai97ec5582006-03-21 11:29:07 +01003706
Takashi Iwai352f7f92012-12-19 12:52:06 +01003707 snd_hda_codec_cleanup_stream(codec,
3708 spec->adc_nids[substream->number + 1]);
Takashi Iwai97ec5582006-03-21 11:29:07 +01003709 return 0;
3710}
3711
Takashi Iwai352f7f92012-12-19 12:52:06 +01003712/*
3713 */
3714static const struct hda_pcm_stream pcm_analog_playback = {
3715 .substreams = 1,
3716 .channels_min = 2,
3717 .channels_max = 8,
3718 /* NID is set in build_pcms */
3719 .ops = {
3720 .open = playback_pcm_open,
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003721 .close = playback_pcm_close,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003722 .prepare = playback_pcm_prepare,
3723 .cleanup = playback_pcm_cleanup
3724 },
3725};
Linus Torvalds1da177e2005-04-16 15:20:36 -07003726
Takashi Iwai352f7f92012-12-19 12:52:06 +01003727static const struct hda_pcm_stream pcm_analog_capture = {
3728 .substreams = 1,
3729 .channels_min = 2,
3730 .channels_max = 2,
3731 /* NID is set in build_pcms */
3732};
3733
3734static const struct hda_pcm_stream pcm_analog_alt_playback = {
3735 .substreams = 1,
3736 .channels_min = 2,
3737 .channels_max = 2,
3738 /* NID is set in build_pcms */
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003739 .ops = {
3740 .open = alt_playback_pcm_open,
Takashi Iwaie6b85f32013-01-07 11:54:34 +01003741 .close = alt_playback_pcm_close,
3742 .prepare = alt_playback_pcm_prepare,
3743 .cleanup = alt_playback_pcm_cleanup
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003744 },
Takashi Iwai352f7f92012-12-19 12:52:06 +01003745};
3746
3747static const struct hda_pcm_stream pcm_analog_alt_capture = {
3748 .substreams = 2, /* can be overridden */
3749 .channels_min = 2,
3750 .channels_max = 2,
3751 /* NID is set in build_pcms */
3752 .ops = {
3753 .prepare = alt_capture_pcm_prepare,
3754 .cleanup = alt_capture_pcm_cleanup
3755 },
3756};
3757
3758static const struct hda_pcm_stream pcm_digital_playback = {
3759 .substreams = 1,
3760 .channels_min = 2,
3761 .channels_max = 2,
3762 /* NID is set in build_pcms */
3763 .ops = {
3764 .open = dig_playback_pcm_open,
3765 .close = dig_playback_pcm_close,
3766 .prepare = dig_playback_pcm_prepare,
3767 .cleanup = dig_playback_pcm_cleanup
3768 },
3769};
3770
3771static const struct hda_pcm_stream pcm_digital_capture = {
3772 .substreams = 1,
3773 .channels_min = 2,
3774 .channels_max = 2,
3775 /* NID is set in build_pcms */
3776};
3777
3778/* Used by build_pcms to flag that a PCM has no playback stream */
3779static const struct hda_pcm_stream pcm_null_stream = {
3780 .substreams = 0,
3781 .channels_min = 0,
3782 .channels_max = 0,
3783};
3784
3785/*
3786 * dynamic changing ADC PCM streams
3787 */
3788static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur)
3789{
3790 struct hda_gen_spec *spec = codec->spec;
3791 hda_nid_t new_adc = spec->adc_nids[spec->dyn_adc_idx[cur]];
3792
3793 if (spec->cur_adc && spec->cur_adc != new_adc) {
3794 /* stream is running, let's swap the current ADC */
3795 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
3796 spec->cur_adc = new_adc;
3797 snd_hda_codec_setup_stream(codec, new_adc,
3798 spec->cur_adc_stream_tag, 0,
3799 spec->cur_adc_format);
3800 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003801 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003802 return false;
3803}
3804
3805/* analog capture with dynamic dual-adc changes */
3806static int dyn_adc_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
3807 struct hda_codec *codec,
3808 unsigned int stream_tag,
3809 unsigned int format,
3810 struct snd_pcm_substream *substream)
3811{
3812 struct hda_gen_spec *spec = codec->spec;
3813 spec->cur_adc = spec->adc_nids[spec->dyn_adc_idx[spec->cur_mux[0]]];
3814 spec->cur_adc_stream_tag = stream_tag;
3815 spec->cur_adc_format = format;
3816 snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
3817 return 0;
3818}
3819
3820static int dyn_adc_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
3821 struct hda_codec *codec,
3822 struct snd_pcm_substream *substream)
3823{
3824 struct hda_gen_spec *spec = codec->spec;
3825 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
3826 spec->cur_adc = 0;
3827 return 0;
3828}
3829
3830static const struct hda_pcm_stream dyn_adc_pcm_analog_capture = {
3831 .substreams = 1,
3832 .channels_min = 2,
3833 .channels_max = 2,
3834 .nid = 0, /* fill later */
3835 .ops = {
3836 .prepare = dyn_adc_capture_pcm_prepare,
3837 .cleanup = dyn_adc_capture_pcm_cleanup
3838 },
3839};
3840
Takashi Iwaif873e532012-12-20 16:58:39 +01003841static void fill_pcm_stream_name(char *str, size_t len, const char *sfx,
3842 const char *chip_name)
3843{
3844 char *p;
3845
3846 if (*str)
3847 return;
3848 strlcpy(str, chip_name, len);
3849
3850 /* drop non-alnum chars after a space */
3851 for (p = strchr(str, ' '); p; p = strchr(p + 1, ' ')) {
3852 if (!isalnum(p[1])) {
3853 *p = 0;
3854 break;
3855 }
3856 }
3857 strlcat(str, sfx, len);
3858}
3859
Takashi Iwai352f7f92012-12-19 12:52:06 +01003860/* build PCM streams based on the parsed results */
3861int snd_hda_gen_build_pcms(struct hda_codec *codec)
3862{
3863 struct hda_gen_spec *spec = codec->spec;
3864 struct hda_pcm *info = spec->pcm_rec;
3865 const struct hda_pcm_stream *p;
3866 bool have_multi_adcs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003867
3868 codec->num_pcms = 1;
3869 codec->pcm_info = info;
3870
Takashi Iwai352f7f92012-12-19 12:52:06 +01003871 if (spec->no_analog)
3872 goto skip_analog;
3873
Takashi Iwaif873e532012-12-20 16:58:39 +01003874 fill_pcm_stream_name(spec->stream_name_analog,
3875 sizeof(spec->stream_name_analog),
3876 " Analog", codec->chip_name);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003877 info->name = spec->stream_name_analog;
3878
3879 if (spec->multiout.num_dacs > 0) {
3880 p = spec->stream_analog_playback;
3881 if (!p)
3882 p = &pcm_analog_playback;
3883 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
3884 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dac_nids[0];
3885 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
3886 spec->multiout.max_channels;
3887 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT &&
3888 spec->autocfg.line_outs == 2)
3889 info->stream[SNDRV_PCM_STREAM_PLAYBACK].chmap =
3890 snd_pcm_2_1_chmaps;
3891 }
3892 if (spec->num_adc_nids) {
3893 p = spec->stream_analog_capture;
3894 if (!p) {
3895 if (spec->dyn_adc_switch)
3896 p = &dyn_adc_pcm_analog_capture;
3897 else
3898 p = &pcm_analog_capture;
3899 }
3900 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
3901 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adc_nids[0];
3902 }
3903
Takashi Iwai352f7f92012-12-19 12:52:06 +01003904 skip_analog:
3905 /* SPDIF for stream index #1 */
3906 if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
Takashi Iwaif873e532012-12-20 16:58:39 +01003907 fill_pcm_stream_name(spec->stream_name_digital,
3908 sizeof(spec->stream_name_digital),
3909 " Digital", codec->chip_name);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003910 codec->num_pcms = 2;
3911 codec->slave_dig_outs = spec->multiout.slave_dig_outs;
3912 info = spec->pcm_rec + 1;
3913 info->name = spec->stream_name_digital;
3914 if (spec->dig_out_type)
3915 info->pcm_type = spec->dig_out_type;
3916 else
3917 info->pcm_type = HDA_PCM_TYPE_SPDIF;
3918 if (spec->multiout.dig_out_nid) {
3919 p = spec->stream_digital_playback;
3920 if (!p)
3921 p = &pcm_digital_playback;
3922 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
3923 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dig_out_nid;
3924 }
3925 if (spec->dig_in_nid) {
3926 p = spec->stream_digital_capture;
3927 if (!p)
3928 p = &pcm_digital_capture;
3929 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
3930 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in_nid;
3931 }
3932 }
3933
3934 if (spec->no_analog)
3935 return 0;
3936
3937 /* If the use of more than one ADC is requested for the current
3938 * model, configure a second analog capture-only PCM.
3939 */
3940 have_multi_adcs = (spec->num_adc_nids > 1) &&
3941 !spec->dyn_adc_switch && !spec->auto_mic;
3942 /* Additional Analaog capture for index #2 */
3943 if (spec->alt_dac_nid || have_multi_adcs) {
3944 codec->num_pcms = 3;
3945 info = spec->pcm_rec + 2;
3946 info->name = spec->stream_name_analog;
3947 if (spec->alt_dac_nid) {
3948 p = spec->stream_analog_alt_playback;
3949 if (!p)
3950 p = &pcm_analog_alt_playback;
3951 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
3952 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
3953 spec->alt_dac_nid;
3954 } else {
3955 info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
3956 pcm_null_stream;
3957 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = 0;
3958 }
3959 if (have_multi_adcs) {
3960 p = spec->stream_analog_alt_capture;
3961 if (!p)
3962 p = &pcm_analog_alt_capture;
3963 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
3964 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
3965 spec->adc_nids[1];
3966 info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams =
3967 spec->num_adc_nids - 1;
3968 } else {
3969 info->stream[SNDRV_PCM_STREAM_CAPTURE] =
3970 pcm_null_stream;
3971 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = 0;
3972 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003973 }
3974
3975 return 0;
3976}
Takashi Iwai352f7f92012-12-19 12:52:06 +01003977EXPORT_SYMBOL_HDA(snd_hda_gen_build_pcms);
3978
3979
3980/*
3981 * Standard auto-parser initializations
3982 */
3983
Takashi Iwaid4156932013-01-07 10:08:02 +01003984/* configure the given path as a proper output */
Takashi Iwai2c12c302013-01-10 09:33:29 +01003985static void set_output_and_unmute(struct hda_codec *codec, int path_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003986{
3987 struct nid_path *path;
Takashi Iwaid4156932013-01-07 10:08:02 +01003988 hda_nid_t pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003989
Takashi Iwai196c17662013-01-04 15:01:40 +01003990 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwaid4156932013-01-07 10:08:02 +01003991 if (!path || !path->depth)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003992 return;
Takashi Iwaid4156932013-01-07 10:08:02 +01003993 pin = path->path[path->depth - 1];
Takashi Iwai2c12c302013-01-10 09:33:29 +01003994 restore_pin_ctl(codec, pin);
Takashi Iwaie1284af2013-01-03 16:33:02 +01003995 snd_hda_activate_path(codec, path, path->active, true);
3996 set_pin_eapd(codec, pin, path->active);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003997}
3998
3999/* initialize primary output paths */
4000static void init_multi_out(struct hda_codec *codec)
4001{
4002 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004003 int i;
4004
Takashi Iwaid4156932013-01-07 10:08:02 +01004005 for (i = 0; i < spec->autocfg.line_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01004006 set_output_and_unmute(codec, spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004007}
4008
Takashi Iwaidb23fd12012-12-20 15:27:24 +01004009
Takashi Iwai2c12c302013-01-10 09:33:29 +01004010static void __init_extra_out(struct hda_codec *codec, int num_outs, int *paths)
Takashi Iwai352f7f92012-12-19 12:52:06 +01004011{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004012 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004013
Takashi Iwaid4156932013-01-07 10:08:02 +01004014 for (i = 0; i < num_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01004015 set_output_and_unmute(codec, paths[i]);
Takashi Iwaidb23fd12012-12-20 15:27:24 +01004016}
4017
4018/* initialize hp and speaker paths */
4019static void init_extra_out(struct hda_codec *codec)
4020{
4021 struct hda_gen_spec *spec = codec->spec;
4022
4023 if (spec->autocfg.line_out_type != AUTO_PIN_HP_OUT)
Takashi Iwai2c12c302013-01-10 09:33:29 +01004024 __init_extra_out(codec, spec->autocfg.hp_outs, spec->hp_paths);
Takashi Iwaidb23fd12012-12-20 15:27:24 +01004025 if (spec->autocfg.line_out_type != AUTO_PIN_SPEAKER_OUT)
4026 __init_extra_out(codec, spec->autocfg.speaker_outs,
Takashi Iwai2c12c302013-01-10 09:33:29 +01004027 spec->speaker_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004028}
4029
4030/* initialize multi-io paths */
4031static void init_multi_io(struct hda_codec *codec)
4032{
4033 struct hda_gen_spec *spec = codec->spec;
4034 int i;
4035
4036 for (i = 0; i < spec->multi_ios; i++) {
4037 hda_nid_t pin = spec->multi_io[i].pin;
4038 struct nid_path *path;
Takashi Iwai196c17662013-01-04 15:01:40 +01004039 path = get_multiio_path(codec, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004040 if (!path)
4041 continue;
4042 if (!spec->multi_io[i].ctl_in)
4043 spec->multi_io[i].ctl_in =
Takashi Iwai2c12c302013-01-10 09:33:29 +01004044 snd_hda_codec_get_pin_target(codec, pin);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004045 snd_hda_activate_path(codec, path, path->active, true);
4046 }
4047}
4048
Takashi Iwai352f7f92012-12-19 12:52:06 +01004049/* set up input pins and loopback paths */
4050static void init_analog_input(struct hda_codec *codec)
4051{
4052 struct hda_gen_spec *spec = codec->spec;
4053 struct auto_pin_cfg *cfg = &spec->autocfg;
4054 int i;
4055
4056 for (i = 0; i < cfg->num_inputs; i++) {
4057 hda_nid_t nid = cfg->inputs[i].pin;
4058 if (is_input_pin(codec, nid))
Takashi Iwai2c12c302013-01-10 09:33:29 +01004059 restore_pin_ctl(codec, nid);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004060
4061 /* init loopback inputs */
4062 if (spec->mixer_nid) {
4063 struct nid_path *path;
Takashi Iwai196c17662013-01-04 15:01:40 +01004064 path = snd_hda_get_path_from_idx(codec, spec->loopback_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004065 if (path)
4066 snd_hda_activate_path(codec, path,
4067 path->active, false);
4068 }
4069 }
4070}
4071
4072/* initialize ADC paths */
4073static void init_input_src(struct hda_codec *codec)
4074{
4075 struct hda_gen_spec *spec = codec->spec;
4076 struct hda_input_mux *imux = &spec->input_mux;
4077 struct nid_path *path;
4078 int i, c, nums;
4079
4080 if (spec->dyn_adc_switch)
4081 nums = 1;
4082 else
4083 nums = spec->num_adc_nids;
4084
4085 for (c = 0; c < nums; c++) {
4086 for (i = 0; i < imux->num_items; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01004087 path = get_input_path(codec, c, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004088 if (path) {
4089 bool active = path->active;
4090 if (i == spec->cur_mux[c])
4091 active = true;
4092 snd_hda_activate_path(codec, path, active, false);
4093 }
4094 }
4095 }
4096
4097 if (spec->shared_mic_hp)
4098 update_shared_mic_hp(codec, spec->cur_mux[0]);
4099
4100 if (spec->cap_sync_hook)
4101 spec->cap_sync_hook(codec);
4102}
4103
4104/* set right pin controls for digital I/O */
4105static void init_digital(struct hda_codec *codec)
4106{
4107 struct hda_gen_spec *spec = codec->spec;
4108 int i;
4109 hda_nid_t pin;
4110
Takashi Iwaid4156932013-01-07 10:08:02 +01004111 for (i = 0; i < spec->autocfg.dig_outs; i++)
Takashi Iwai2c12c302013-01-10 09:33:29 +01004112 set_output_and_unmute(codec, spec->digout_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004113 pin = spec->autocfg.dig_in_pin;
Takashi Iwai2430d7b2013-01-04 15:09:42 +01004114 if (pin) {
4115 struct nid_path *path;
Takashi Iwai2c12c302013-01-10 09:33:29 +01004116 restore_pin_ctl(codec, pin);
Takashi Iwai2430d7b2013-01-04 15:09:42 +01004117 path = snd_hda_get_path_from_idx(codec, spec->digin_path);
4118 if (path)
4119 snd_hda_activate_path(codec, path, path->active, false);
4120 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01004121}
4122
Takashi Iwai973e4972012-12-20 15:16:09 +01004123/* clear unsol-event tags on unused pins; Conexant codecs seem to leave
4124 * invalid unsol tags by some reason
4125 */
4126static void clear_unsol_on_unused_pins(struct hda_codec *codec)
4127{
4128 int i;
4129
4130 for (i = 0; i < codec->init_pins.used; i++) {
4131 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
4132 hda_nid_t nid = pin->nid;
4133 if (is_jack_detectable(codec, nid) &&
4134 !snd_hda_jack_tbl_get(codec, nid))
4135 snd_hda_codec_update_cache(codec, nid, 0,
4136 AC_VERB_SET_UNSOLICITED_ENABLE, 0);
4137 }
4138}
4139
Takashi Iwai5187ac12013-01-07 12:52:16 +01004140/*
4141 * initialize the generic spec;
4142 * this can be put as patch_ops.init function
4143 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01004144int snd_hda_gen_init(struct hda_codec *codec)
4145{
4146 struct hda_gen_spec *spec = codec->spec;
4147
4148 if (spec->init_hook)
4149 spec->init_hook(codec);
4150
4151 snd_hda_apply_verbs(codec);
4152
Takashi Iwai3bbcd272012-12-20 11:50:58 +01004153 codec->cached_write = 1;
4154
Takashi Iwai352f7f92012-12-19 12:52:06 +01004155 init_multi_out(codec);
4156 init_extra_out(codec);
4157 init_multi_io(codec);
4158 init_analog_input(codec);
4159 init_input_src(codec);
4160 init_digital(codec);
4161
Takashi Iwai973e4972012-12-20 15:16:09 +01004162 clear_unsol_on_unused_pins(codec);
4163
Takashi Iwai352f7f92012-12-19 12:52:06 +01004164 /* call init functions of standard auto-mute helpers */
Takashi Iwai5d550e12012-12-19 15:16:44 +01004165 snd_hda_gen_hp_automute(codec, NULL);
4166 snd_hda_gen_line_automute(codec, NULL);
4167 snd_hda_gen_mic_autoswitch(codec, NULL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004168
Takashi Iwai3bbcd272012-12-20 11:50:58 +01004169 snd_hda_codec_flush_amp_cache(codec);
4170 snd_hda_codec_flush_cmd_cache(codec);
4171
Takashi Iwai352f7f92012-12-19 12:52:06 +01004172 if (spec->vmaster_mute.sw_kctl && spec->vmaster_mute.hook)
4173 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
4174
4175 hda_call_check_power_status(codec, 0x01);
4176 return 0;
4177}
Takashi Iwaifce52a32013-01-07 12:42:48 +01004178EXPORT_SYMBOL_HDA(snd_hda_gen_init);
4179
Takashi Iwai5187ac12013-01-07 12:52:16 +01004180/*
4181 * free the generic spec;
4182 * this can be put as patch_ops.free function
4183 */
Takashi Iwaifce52a32013-01-07 12:42:48 +01004184void snd_hda_gen_free(struct hda_codec *codec)
4185{
4186 snd_hda_gen_spec_free(codec->spec);
4187 kfree(codec->spec);
4188 codec->spec = NULL;
4189}
4190EXPORT_SYMBOL_HDA(snd_hda_gen_free);
4191
4192#ifdef CONFIG_PM
Takashi Iwai5187ac12013-01-07 12:52:16 +01004193/*
4194 * check the loopback power save state;
4195 * this can be put as patch_ops.check_power_status function
4196 */
Takashi Iwaifce52a32013-01-07 12:42:48 +01004197int snd_hda_gen_check_power_status(struct hda_codec *codec, hda_nid_t nid)
4198{
4199 struct hda_gen_spec *spec = codec->spec;
4200 return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
4201}
4202EXPORT_SYMBOL_HDA(snd_hda_gen_check_power_status);
4203#endif
Takashi Iwai352f7f92012-12-19 12:52:06 +01004204
4205
4206/*
4207 * the generic codec support
4208 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004209
Takashi Iwai352f7f92012-12-19 12:52:06 +01004210static const struct hda_codec_ops generic_patch_ops = {
4211 .build_controls = snd_hda_gen_build_controls,
4212 .build_pcms = snd_hda_gen_build_pcms,
4213 .init = snd_hda_gen_init,
Takashi Iwaifce52a32013-01-07 12:42:48 +01004214 .free = snd_hda_gen_free,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004215 .unsol_event = snd_hda_jack_unsol_event,
Takashi Iwai83012a72012-08-24 18:38:08 +02004216#ifdef CONFIG_PM
Takashi Iwaifce52a32013-01-07 12:42:48 +01004217 .check_power_status = snd_hda_gen_check_power_status,
Takashi Iwaicb53c622007-08-10 17:21:45 +02004218#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004219};
4220
Linus Torvalds1da177e2005-04-16 15:20:36 -07004221int snd_hda_parse_generic_codec(struct hda_codec *codec)
4222{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004223 struct hda_gen_spec *spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004224 int err;
4225
Takashi Iwaie560d8d2005-09-09 14:21:46 +02004226 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004227 if (!spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004228 return -ENOMEM;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004229 snd_hda_gen_spec_init(spec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004230 codec->spec = spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004231
Takashi Iwai9eb413e2012-12-19 14:41:21 +01004232 err = snd_hda_parse_pin_defcfg(codec, &spec->autocfg, NULL, 0);
4233 if (err < 0)
4234 return err;
4235
4236 err = snd_hda_gen_parse_auto_config(codec, &spec->autocfg);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004237 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004238 goto error;
4239
4240 codec->patch_ops = generic_patch_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004241 return 0;
4242
Takashi Iwai352f7f92012-12-19 12:52:06 +01004243error:
Takashi Iwaifce52a32013-01-07 12:42:48 +01004244 snd_hda_gen_free(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004245 return err;
4246}
Takashi Iwaifce52a32013-01-07 12:42:48 +01004247EXPORT_SYMBOL_HDA(snd_hda_parse_generic_codec);