blob: aa4e6398e3b479bde04a2685625c5ecc288f4d77 [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 Iwai352f7f92012-12-19 12:52:06 +010087 * parsing paths
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
Takashi Iwai3ca529d2013-01-07 17:25:08 +010090/* return the position of NID in the list, or -1 if not found */
91static int find_idx_in_nid_list(hda_nid_t nid, const hda_nid_t *list, int nums)
92{
93 int i;
94 for (i = 0; i < nums; i++)
95 if (list[i] == nid)
96 return i;
97 return -1;
98}
99
100/* return true if the given NID is contained in the path */
101static bool is_nid_contained(struct nid_path *path, hda_nid_t nid)
102{
103 return find_idx_in_nid_list(nid, path->path, path->depth) >= 0;
104}
105
Takashi Iwaif5172a72013-01-04 13:19:55 +0100106static struct nid_path *get_nid_path(struct hda_codec *codec,
107 hda_nid_t from_nid, hda_nid_t to_nid,
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100108 int anchor_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100110 struct hda_gen_spec *spec = codec->spec;
111 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
Takashi Iwai352f7f92012-12-19 12:52:06 +0100113 for (i = 0; i < spec->paths.used; i++) {
114 struct nid_path *path = snd_array_elem(&spec->paths, i);
115 if (path->depth <= 0)
116 continue;
117 if ((!from_nid || path->path[0] == from_nid) &&
Takashi Iwaif5172a72013-01-04 13:19:55 +0100118 (!to_nid || path->path[path->depth - 1] == to_nid)) {
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100119 if (!anchor_nid ||
120 (anchor_nid > 0 && is_nid_contained(path, anchor_nid)) ||
121 (anchor_nid < 0 && !is_nid_contained(path, anchor_nid)))
Takashi Iwaif5172a72013-01-04 13:19:55 +0100122 return path;
123 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 }
125 return NULL;
126}
Takashi Iwaif5172a72013-01-04 13:19:55 +0100127
128/* get the path between the given NIDs;
129 * passing 0 to either @pin or @dac behaves as a wildcard
130 */
131struct nid_path *snd_hda_get_nid_path(struct hda_codec *codec,
132 hda_nid_t from_nid, hda_nid_t to_nid)
133{
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100134 return get_nid_path(codec, from_nid, to_nid, 0);
Takashi Iwaif5172a72013-01-04 13:19:55 +0100135}
Takashi Iwai352f7f92012-12-19 12:52:06 +0100136EXPORT_SYMBOL_HDA(snd_hda_get_nid_path);
137
Takashi Iwai196c17662013-01-04 15:01:40 +0100138/* get the index number corresponding to the path instance;
139 * the index starts from 1, for easier checking the invalid value
140 */
141int snd_hda_get_path_idx(struct hda_codec *codec, struct nid_path *path)
142{
143 struct hda_gen_spec *spec = codec->spec;
144 struct nid_path *array = spec->paths.list;
145 ssize_t idx;
146
147 if (!spec->paths.used)
148 return 0;
149 idx = path - array;
150 if (idx < 0 || idx >= spec->paths.used)
151 return 0;
152 return idx + 1;
153}
154
155/* get the path instance corresponding to the given index number */
156struct nid_path *snd_hda_get_path_from_idx(struct hda_codec *codec, int idx)
157{
158 struct hda_gen_spec *spec = codec->spec;
159
160 if (idx <= 0 || idx > spec->paths.used)
161 return NULL;
162 return snd_array_elem(&spec->paths, idx - 1);
163}
164
Takashi Iwai352f7f92012-12-19 12:52:06 +0100165/* check whether the given DAC is already found in any existing paths */
166static bool is_dac_already_used(struct hda_codec *codec, hda_nid_t nid)
167{
168 struct hda_gen_spec *spec = codec->spec;
169 int i;
170
171 for (i = 0; i < spec->paths.used; i++) {
172 struct nid_path *path = snd_array_elem(&spec->paths, i);
173 if (path->path[0] == nid)
174 return true;
175 }
176 return false;
177}
178
179/* check whether the given two widgets can be connected */
180static bool is_reachable_path(struct hda_codec *codec,
181 hda_nid_t from_nid, hda_nid_t to_nid)
182{
183 if (!from_nid || !to_nid)
184 return false;
185 return snd_hda_get_conn_index(codec, to_nid, from_nid, true) >= 0;
186}
187
188/* nid, dir and idx */
189#define AMP_VAL_COMPARE_MASK (0xffff | (1U << 18) | (0x0f << 19))
190
191/* check whether the given ctl is already assigned in any path elements */
192static bool is_ctl_used(struct hda_codec *codec, unsigned int val, int type)
193{
194 struct hda_gen_spec *spec = codec->spec;
195 int i;
196
197 val &= AMP_VAL_COMPARE_MASK;
198 for (i = 0; i < spec->paths.used; i++) {
199 struct nid_path *path = snd_array_elem(&spec->paths, i);
200 if ((path->ctls[type] & AMP_VAL_COMPARE_MASK) == val)
201 return true;
202 }
203 return false;
204}
205
206/* check whether a control with the given (nid, dir, idx) was assigned */
207static bool is_ctl_associated(struct hda_codec *codec, hda_nid_t nid,
208 int dir, int idx)
209{
210 unsigned int val = HDA_COMPOSE_AMP_VAL(nid, 3, idx, dir);
211 return is_ctl_used(codec, val, NID_PATH_VOL_CTL) ||
212 is_ctl_used(codec, val, NID_PATH_MUTE_CTL);
213}
214
Takashi Iwai0c8c0f52012-12-20 17:54:22 +0100215static void print_nid_path(const char *pfx, struct nid_path *path)
216{
217 char buf[40];
218 int i;
219
220
221 buf[0] = 0;
222 for (i = 0; i < path->depth; i++) {
223 char tmp[4];
224 sprintf(tmp, ":%02x", path->path[i]);
225 strlcat(buf, tmp, sizeof(buf));
226 }
227 snd_printdd("%s path: depth=%d %s\n", pfx, path->depth, buf);
228}
229
Takashi Iwai352f7f92012-12-19 12:52:06 +0100230/* called recursively */
231static bool __parse_nid_path(struct hda_codec *codec,
232 hda_nid_t from_nid, hda_nid_t to_nid,
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100233 int anchor_nid, struct nid_path *path,
234 int depth)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100235{
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100236 const hda_nid_t *conn;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100237 int i, nums;
238
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100239 if (to_nid == anchor_nid)
240 anchor_nid = 0; /* anchor passed */
241 else if (to_nid == (hda_nid_t)(-anchor_nid))
242 return false; /* hit the exclusive nid */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100243
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100244 nums = snd_hda_get_conn_list(codec, to_nid, &conn);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100245 for (i = 0; i < nums; i++) {
246 if (conn[i] != from_nid) {
247 /* special case: when from_nid is 0,
248 * try to find an empty DAC
249 */
250 if (from_nid ||
251 get_wcaps_type(get_wcaps(codec, conn[i])) != AC_WID_AUD_OUT ||
252 is_dac_already_used(codec, conn[i]))
253 continue;
254 }
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100255 /* anchor is not requested or already passed? */
256 if (anchor_nid <= 0)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100257 goto found;
258 }
259 if (depth >= MAX_NID_PATH_DEPTH)
260 return false;
261 for (i = 0; i < nums; i++) {
262 unsigned int type;
263 type = get_wcaps_type(get_wcaps(codec, conn[i]));
264 if (type == AC_WID_AUD_OUT || type == AC_WID_AUD_IN ||
265 type == AC_WID_PIN)
266 continue;
267 if (__parse_nid_path(codec, from_nid, conn[i],
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100268 anchor_nid, path, depth + 1))
Takashi Iwai352f7f92012-12-19 12:52:06 +0100269 goto found;
270 }
271 return false;
272
273 found:
274 path->path[path->depth] = conn[i];
275 path->idx[path->depth + 1] = i;
276 if (nums > 1 && get_wcaps_type(get_wcaps(codec, to_nid)) != AC_WID_AUD_MIX)
277 path->multi[path->depth + 1] = 1;
278 path->depth++;
279 return true;
280}
281
282/* parse the widget path from the given nid to the target nid;
283 * when @from_nid is 0, try to find an empty DAC;
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100284 * when @anchor_nid is set to a positive value, only paths through the widget
285 * with the given value are evaluated.
286 * when @anchor_nid is set to a negative value, paths through the widget
287 * with the negative of given value are excluded, only other paths are chosen.
288 * when @anchor_nid is zero, no special handling about path selection.
Takashi Iwai352f7f92012-12-19 12:52:06 +0100289 */
290bool snd_hda_parse_nid_path(struct hda_codec *codec, hda_nid_t from_nid,
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100291 hda_nid_t to_nid, int anchor_nid,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100292 struct nid_path *path)
293{
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100294 if (__parse_nid_path(codec, from_nid, to_nid, anchor_nid, path, 1)) {
Takashi Iwai352f7f92012-12-19 12:52:06 +0100295 path->path[path->depth] = to_nid;
296 path->depth++;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100297 return true;
298 }
299 return false;
300}
301EXPORT_SYMBOL_HDA(snd_hda_parse_nid_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
303/*
Takashi Iwai352f7f92012-12-19 12:52:06 +0100304 * parse the path between the given NIDs and add to the path list.
305 * if no valid path is found, return NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100307struct nid_path *
308snd_hda_add_new_path(struct hda_codec *codec, hda_nid_t from_nid,
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100309 hda_nid_t to_nid, int anchor_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100311 struct hda_gen_spec *spec = codec->spec;
312 struct nid_path *path;
313
314 if (from_nid && to_nid && !is_reachable_path(codec, from_nid, to_nid))
315 return NULL;
316
Takashi Iwaif5172a72013-01-04 13:19:55 +0100317 /* check whether the path has been already added */
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100318 path = get_nid_path(codec, from_nid, to_nid, anchor_nid);
Takashi Iwaif5172a72013-01-04 13:19:55 +0100319 if (path)
320 return path;
321
Takashi Iwai352f7f92012-12-19 12:52:06 +0100322 path = snd_array_new(&spec->paths);
323 if (!path)
324 return NULL;
325 memset(path, 0, sizeof(*path));
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100326 if (snd_hda_parse_nid_path(codec, from_nid, to_nid, anchor_nid, path))
Takashi Iwai352f7f92012-12-19 12:52:06 +0100327 return path;
328 /* push back */
329 spec->paths.used--;
330 return NULL;
331}
332EXPORT_SYMBOL_HDA(snd_hda_add_new_path);
333
334/* look for an empty DAC slot */
335static hda_nid_t look_for_dac(struct hda_codec *codec, hda_nid_t pin,
336 bool is_digital)
337{
338 struct hda_gen_spec *spec = codec->spec;
339 bool cap_digital;
340 int i;
341
342 for (i = 0; i < spec->num_all_dacs; i++) {
343 hda_nid_t nid = spec->all_dacs[i];
344 if (!nid || is_dac_already_used(codec, nid))
345 continue;
346 cap_digital = !!(get_wcaps(codec, nid) & AC_WCAP_DIGITAL);
347 if (is_digital != cap_digital)
348 continue;
349 if (is_reachable_path(codec, nid, pin))
350 return nid;
351 }
352 return 0;
353}
354
355/* replace the channels in the composed amp value with the given number */
356static unsigned int amp_val_replace_channels(unsigned int val, unsigned int chs)
357{
358 val &= ~(0x3U << 16);
359 val |= chs << 16;
360 return val;
361}
362
363/* check whether the widget has the given amp capability for the direction */
364static bool check_amp_caps(struct hda_codec *codec, hda_nid_t nid,
365 int dir, unsigned int bits)
366{
367 if (!nid)
368 return false;
369 if (get_wcaps(codec, nid) & (1 << (dir + 1)))
370 if (query_amp_caps(codec, nid, dir) & bits)
371 return true;
372 return false;
373}
374
375#define nid_has_mute(codec, nid, dir) \
376 check_amp_caps(codec, nid, dir, AC_AMPCAP_MUTE)
377#define nid_has_volume(codec, nid, dir) \
378 check_amp_caps(codec, nid, dir, AC_AMPCAP_NUM_STEPS)
379
380/* look for a widget suitable for assigning a mute switch in the path */
381static hda_nid_t look_for_out_mute_nid(struct hda_codec *codec,
382 struct nid_path *path)
383{
384 int i;
385
386 for (i = path->depth - 1; i >= 0; i--) {
387 if (nid_has_mute(codec, path->path[i], HDA_OUTPUT))
388 return path->path[i];
389 if (i != path->depth - 1 && i != 0 &&
390 nid_has_mute(codec, path->path[i], HDA_INPUT))
391 return path->path[i];
392 }
393 return 0;
394}
395
396/* look for a widget suitable for assigning a volume ctl in the path */
397static hda_nid_t look_for_out_vol_nid(struct hda_codec *codec,
398 struct nid_path *path)
399{
400 int i;
401
402 for (i = path->depth - 1; i >= 0; i--) {
403 if (nid_has_volume(codec, path->path[i], HDA_OUTPUT))
404 return path->path[i];
405 }
Takashi Iwai82beb8f2007-08-10 17:09:26 +0200406 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407}
408
409/*
Takashi Iwai352f7f92012-12-19 12:52:06 +0100410 * path activation / deactivation
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100412
413/* can have the amp-in capability? */
414static bool has_amp_in(struct hda_codec *codec, struct nid_path *path, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100416 hda_nid_t nid = path->path[idx];
417 unsigned int caps = get_wcaps(codec, nid);
418 unsigned int type = get_wcaps_type(caps);
419
420 if (!(caps & AC_WCAP_IN_AMP))
421 return false;
422 if (type == AC_WID_PIN && idx > 0) /* only for input pins */
423 return false;
424 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425}
426
Takashi Iwai352f7f92012-12-19 12:52:06 +0100427/* can have the amp-out capability? */
428static bool has_amp_out(struct hda_codec *codec, struct nid_path *path, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100430 hda_nid_t nid = path->path[idx];
431 unsigned int caps = get_wcaps(codec, nid);
432 unsigned int type = get_wcaps_type(caps);
433
434 if (!(caps & AC_WCAP_OUT_AMP))
435 return false;
436 if (type == AC_WID_PIN && !idx) /* only for output pins */
437 return false;
438 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439}
440
Takashi Iwai352f7f92012-12-19 12:52:06 +0100441/* check whether the given (nid,dir,idx) is active */
442static bool is_active_nid(struct hda_codec *codec, hda_nid_t nid,
443 unsigned int idx, unsigned int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100445 struct hda_gen_spec *spec = codec->spec;
446 int i, n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
Takashi Iwai352f7f92012-12-19 12:52:06 +0100448 for (n = 0; n < spec->paths.used; n++) {
449 struct nid_path *path = snd_array_elem(&spec->paths, n);
450 if (!path->active)
451 continue;
452 for (i = 0; i < path->depth; i++) {
453 if (path->path[i] == nid) {
454 if (dir == HDA_OUTPUT || path->idx[i] == idx)
455 return true;
456 break;
457 }
458 }
459 }
460 return false;
461}
462
463/* get the default amp value for the target state */
464static int get_amp_val_to_activate(struct hda_codec *codec, hda_nid_t nid,
465 int dir, bool enable)
466{
467 unsigned int caps;
468 unsigned int val = 0;
469
470 caps = query_amp_caps(codec, nid, dir);
471 if (caps & AC_AMPCAP_NUM_STEPS) {
472 /* set to 0dB */
473 if (enable)
474 val = (caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT;
475 }
476 if (caps & AC_AMPCAP_MUTE) {
477 if (!enable)
478 val |= HDA_AMP_MUTE;
479 }
480 return val;
481}
482
483/* initialize the amp value (only at the first time) */
484static void init_amp(struct hda_codec *codec, hda_nid_t nid, int dir, int idx)
485{
486 int val = get_amp_val_to_activate(codec, nid, dir, false);
487 snd_hda_codec_amp_init_stereo(codec, nid, dir, idx, 0xff, val);
488}
489
490static void activate_amp(struct hda_codec *codec, hda_nid_t nid, int dir,
491 int idx, bool enable)
492{
493 int val;
494 if (is_ctl_associated(codec, nid, dir, idx) ||
Takashi Iwai985803c2013-01-03 16:30:04 +0100495 (!enable && is_active_nid(codec, nid, dir, idx)))
Takashi Iwai352f7f92012-12-19 12:52:06 +0100496 return;
497 val = get_amp_val_to_activate(codec, nid, dir, enable);
498 snd_hda_codec_amp_stereo(codec, nid, dir, idx, 0xff, val);
499}
500
501static void activate_amp_out(struct hda_codec *codec, struct nid_path *path,
502 int i, bool enable)
503{
504 hda_nid_t nid = path->path[i];
505 init_amp(codec, nid, HDA_OUTPUT, 0);
506 activate_amp(codec, nid, HDA_OUTPUT, 0, enable);
507}
508
509static void activate_amp_in(struct hda_codec *codec, struct nid_path *path,
510 int i, bool enable, bool add_aamix)
511{
512 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100513 const hda_nid_t *conn;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100514 int n, nums, idx;
515 int type;
516 hda_nid_t nid = path->path[i];
517
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100518 nums = snd_hda_get_conn_list(codec, nid, &conn);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100519 type = get_wcaps_type(get_wcaps(codec, nid));
520 if (type == AC_WID_PIN ||
521 (type == AC_WID_AUD_IN && codec->single_adc_amp)) {
522 nums = 1;
523 idx = 0;
524 } else
525 idx = path->idx[i];
526
527 for (n = 0; n < nums; n++)
528 init_amp(codec, nid, HDA_INPUT, n);
529
530 if (is_ctl_associated(codec, nid, HDA_INPUT, idx))
531 return;
532
533 /* here is a little bit tricky in comparison with activate_amp_out();
534 * when aa-mixer is available, we need to enable the path as well
535 */
536 for (n = 0; n < nums; n++) {
537 if (n != idx && (!add_aamix || conn[n] != spec->mixer_nid))
538 continue;
539 activate_amp(codec, nid, HDA_INPUT, n, enable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 }
541}
542
Takashi Iwai352f7f92012-12-19 12:52:06 +0100543/* activate or deactivate the given path
544 * if @add_aamix is set, enable the input from aa-mix NID as well (if any)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100546void snd_hda_activate_path(struct hda_codec *codec, struct nid_path *path,
547 bool enable, bool add_aamix)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100549 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
Takashi Iwai352f7f92012-12-19 12:52:06 +0100551 if (!enable)
552 path->active = false;
553
554 for (i = path->depth - 1; i >= 0; i--) {
555 if (enable && path->multi[i])
556 snd_hda_codec_write_cache(codec, path->path[i], 0,
557 AC_VERB_SET_CONNECT_SEL,
558 path->idx[i]);
559 if (has_amp_in(codec, path, i))
560 activate_amp_in(codec, path, i, enable, add_aamix);
561 if (has_amp_out(codec, path, i))
562 activate_amp_out(codec, path, i, enable);
563 }
564
565 if (enable)
566 path->active = true;
567}
568EXPORT_SYMBOL_HDA(snd_hda_activate_path);
569
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +0100570/* turn on/off EAPD on the given pin */
571static void set_pin_eapd(struct hda_codec *codec, hda_nid_t pin, bool enable)
572{
573 struct hda_gen_spec *spec = codec->spec;
574 if (spec->own_eapd_ctl ||
575 !(snd_hda_query_pin_caps(codec, pin) & AC_PINCAP_EAPD))
576 return;
Takashi Iwaiecac3ed2012-12-21 15:23:01 +0100577 if (codec->inv_eapd)
578 enable = !enable;
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +0100579 snd_hda_codec_update_cache(codec, pin, 0,
580 AC_VERB_SET_EAPD_BTLENABLE,
581 enable ? 0x02 : 0x00);
582}
583
Takashi Iwai352f7f92012-12-19 12:52:06 +0100584
585/*
586 * Helper functions for creating mixer ctl elements
587 */
588
589enum {
590 HDA_CTL_WIDGET_VOL,
591 HDA_CTL_WIDGET_MUTE,
592 HDA_CTL_BIND_MUTE,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100593};
594static const struct snd_kcontrol_new control_templates[] = {
595 HDA_CODEC_VOLUME(NULL, 0, 0, 0),
596 HDA_CODEC_MUTE(NULL, 0, 0, 0),
597 HDA_BIND_MUTE(NULL, 0, 0, 0),
Takashi Iwai352f7f92012-12-19 12:52:06 +0100598};
599
600/* add dynamic controls from template */
601static int add_control(struct hda_gen_spec *spec, int type, const char *name,
602 int cidx, unsigned long val)
603{
604 struct snd_kcontrol_new *knew;
605
Takashi Iwai12c93df2012-12-19 14:38:33 +0100606 knew = snd_hda_gen_add_kctl(spec, name, &control_templates[type]);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100607 if (!knew)
608 return -ENOMEM;
609 knew->index = cidx;
610 if (get_amp_nid_(val))
611 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
612 knew->private_value = val;
613 return 0;
614}
615
616static int add_control_with_pfx(struct hda_gen_spec *spec, int type,
617 const char *pfx, const char *dir,
618 const char *sfx, int cidx, unsigned long val)
619{
620 char name[32];
621 snprintf(name, sizeof(name), "%s %s %s", pfx, dir, sfx);
622 return add_control(spec, type, name, cidx, val);
623}
624
625#define add_pb_vol_ctrl(spec, type, pfx, val) \
626 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", 0, val)
627#define add_pb_sw_ctrl(spec, type, pfx, val) \
628 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", 0, val)
629#define __add_pb_vol_ctrl(spec, type, pfx, cidx, val) \
630 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", cidx, val)
631#define __add_pb_sw_ctrl(spec, type, pfx, cidx, val) \
632 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", cidx, val)
633
634static int add_vol_ctl(struct hda_codec *codec, const char *pfx, int cidx,
635 unsigned int chs, struct nid_path *path)
636{
637 unsigned int val;
638 if (!path)
639 return 0;
640 val = path->ctls[NID_PATH_VOL_CTL];
641 if (!val)
642 return 0;
643 val = amp_val_replace_channels(val, chs);
644 return __add_pb_vol_ctrl(codec->spec, HDA_CTL_WIDGET_VOL, pfx, cidx, val);
645}
646
647/* return the channel bits suitable for the given path->ctls[] */
648static int get_default_ch_nums(struct hda_codec *codec, struct nid_path *path,
649 int type)
650{
651 int chs = 1; /* mono (left only) */
652 if (path) {
653 hda_nid_t nid = get_amp_nid_(path->ctls[type]);
654 if (nid && (get_wcaps(codec, nid) & AC_WCAP_STEREO))
655 chs = 3; /* stereo */
656 }
657 return chs;
658}
659
660static int add_stereo_vol(struct hda_codec *codec, const char *pfx, int cidx,
661 struct nid_path *path)
662{
663 int chs = get_default_ch_nums(codec, path, NID_PATH_VOL_CTL);
664 return add_vol_ctl(codec, pfx, cidx, chs, path);
665}
666
667/* create a mute-switch for the given mixer widget;
668 * if it has multiple sources (e.g. DAC and loopback), create a bind-mute
669 */
670static int add_sw_ctl(struct hda_codec *codec, const char *pfx, int cidx,
671 unsigned int chs, struct nid_path *path)
672{
673 unsigned int val;
674 int type = HDA_CTL_WIDGET_MUTE;
675
676 if (!path)
677 return 0;
678 val = path->ctls[NID_PATH_MUTE_CTL];
679 if (!val)
680 return 0;
681 val = amp_val_replace_channels(val, chs);
682 if (get_amp_direction_(val) == HDA_INPUT) {
683 hda_nid_t nid = get_amp_nid_(val);
684 int nums = snd_hda_get_num_conns(codec, nid);
685 if (nums > 1) {
686 type = HDA_CTL_BIND_MUTE;
687 val |= nums << 19;
688 }
689 }
690 return __add_pb_sw_ctrl(codec->spec, type, pfx, cidx, val);
691}
692
693static int add_stereo_sw(struct hda_codec *codec, const char *pfx,
694 int cidx, struct nid_path *path)
695{
696 int chs = get_default_ch_nums(codec, path, NID_PATH_MUTE_CTL);
697 return add_sw_ctl(codec, pfx, cidx, chs, path);
698}
699
700static const char * const channel_name[4] = {
701 "Front", "Surround", "CLFE", "Side"
702};
703
704/* give some appropriate ctl name prefix for the given line out channel */
705static const char *get_line_out_pfx(struct hda_gen_spec *spec, int ch,
706 bool can_be_master, int *index)
707{
708 struct auto_pin_cfg *cfg = &spec->autocfg;
709
710 *index = 0;
711 if (cfg->line_outs == 1 && !spec->multi_ios &&
712 !cfg->hp_outs && !cfg->speaker_outs && can_be_master)
713 return spec->vmaster_mute.hook ? "PCM" : "Master";
714
715 /* if there is really a single DAC used in the whole output paths,
716 * use it master (or "PCM" if a vmaster hook is present)
717 */
718 if (spec->multiout.num_dacs == 1 && !spec->mixer_nid &&
719 !spec->multiout.hp_out_nid[0] && !spec->multiout.extra_out_nid[0])
720 return spec->vmaster_mute.hook ? "PCM" : "Master";
721
722 switch (cfg->line_out_type) {
723 case AUTO_PIN_SPEAKER_OUT:
724 if (cfg->line_outs == 1)
725 return "Speaker";
726 if (cfg->line_outs == 2)
727 return ch ? "Bass Speaker" : "Speaker";
728 break;
729 case AUTO_PIN_HP_OUT:
730 /* for multi-io case, only the primary out */
731 if (ch && spec->multi_ios)
732 break;
733 *index = ch;
734 return "Headphone";
735 default:
736 if (cfg->line_outs == 1 && !spec->multi_ios)
737 return "PCM";
738 break;
739 }
740 if (ch >= ARRAY_SIZE(channel_name)) {
741 snd_BUG();
742 return "PCM";
743 }
744
745 return channel_name[ch];
746}
747
748/*
749 * Parse output paths
750 */
751
752/* badness definition */
753enum {
754 /* No primary DAC is found for the main output */
755 BAD_NO_PRIMARY_DAC = 0x10000,
756 /* No DAC is found for the extra output */
757 BAD_NO_DAC = 0x4000,
758 /* No possible multi-ios */
759 BAD_MULTI_IO = 0x103,
760 /* No individual DAC for extra output */
761 BAD_NO_EXTRA_DAC = 0x102,
762 /* No individual DAC for extra surrounds */
763 BAD_NO_EXTRA_SURR_DAC = 0x101,
764 /* Primary DAC shared with main surrounds */
765 BAD_SHARED_SURROUND = 0x100,
766 /* Primary DAC shared with main CLFE */
767 BAD_SHARED_CLFE = 0x10,
768 /* Primary DAC shared with extra surrounds */
769 BAD_SHARED_EXTRA_SURROUND = 0x10,
770 /* Volume widget is shared */
771 BAD_SHARED_VOL = 0x10,
772};
773
Takashi Iwai0e614dd2013-01-07 15:11:44 +0100774/* look for widgets in the given path which are appropriate for
Takashi Iwai352f7f92012-12-19 12:52:06 +0100775 * volume and mute controls, and assign the values to ctls[].
776 *
777 * When no appropriate widget is found in the path, the badness value
778 * is incremented depending on the situation. The function returns the
779 * total badness for both volume and mute controls.
780 */
Takashi Iwai0e614dd2013-01-07 15:11:44 +0100781static int assign_out_path_ctls(struct hda_codec *codec, struct nid_path *path)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100782{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100783 hda_nid_t nid;
784 unsigned int val;
785 int badness = 0;
786
787 if (!path)
788 return BAD_SHARED_VOL * 2;
Takashi Iwai0e614dd2013-01-07 15:11:44 +0100789
790 if (path->ctls[NID_PATH_VOL_CTL] ||
791 path->ctls[NID_PATH_MUTE_CTL])
792 return 0; /* already evaluated */
793
Takashi Iwai352f7f92012-12-19 12:52:06 +0100794 nid = look_for_out_vol_nid(codec, path);
795 if (nid) {
796 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
797 if (is_ctl_used(codec, val, NID_PATH_VOL_CTL))
798 badness += BAD_SHARED_VOL;
799 else
800 path->ctls[NID_PATH_VOL_CTL] = val;
801 } else
802 badness += BAD_SHARED_VOL;
803 nid = look_for_out_mute_nid(codec, path);
804 if (nid) {
805 unsigned int wid_type = get_wcaps_type(get_wcaps(codec, nid));
806 if (wid_type == AC_WID_PIN || wid_type == AC_WID_AUD_OUT ||
807 nid_has_mute(codec, nid, HDA_OUTPUT))
808 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
809 else
810 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT);
811 if (is_ctl_used(codec, val, NID_PATH_MUTE_CTL))
812 badness += BAD_SHARED_VOL;
813 else
814 path->ctls[NID_PATH_MUTE_CTL] = val;
815 } else
816 badness += BAD_SHARED_VOL;
817 return badness;
818}
819
820struct badness_table {
821 int no_primary_dac; /* no primary DAC */
822 int no_dac; /* no secondary DACs */
823 int shared_primary; /* primary DAC is shared with main output */
824 int shared_surr; /* secondary DAC shared with main or primary */
825 int shared_clfe; /* third DAC shared with main or primary */
826 int shared_surr_main; /* secondary DAC sahred with main/DAC0 */
827};
828
829static struct badness_table main_out_badness = {
830 .no_primary_dac = BAD_NO_PRIMARY_DAC,
831 .no_dac = BAD_NO_DAC,
832 .shared_primary = BAD_NO_PRIMARY_DAC,
833 .shared_surr = BAD_SHARED_SURROUND,
834 .shared_clfe = BAD_SHARED_CLFE,
835 .shared_surr_main = BAD_SHARED_SURROUND,
836};
837
838static struct badness_table extra_out_badness = {
839 .no_primary_dac = BAD_NO_DAC,
840 .no_dac = BAD_NO_DAC,
841 .shared_primary = BAD_NO_EXTRA_DAC,
842 .shared_surr = BAD_SHARED_EXTRA_SURROUND,
843 .shared_clfe = BAD_SHARED_EXTRA_SURROUND,
844 .shared_surr_main = BAD_NO_EXTRA_SURR_DAC,
845};
846
Takashi Iwai7385df62013-01-07 09:50:52 +0100847/* get the DAC of the primary output corresponding to the given array index */
848static hda_nid_t get_primary_out(struct hda_codec *codec, int idx)
849{
850 struct hda_gen_spec *spec = codec->spec;
851 struct auto_pin_cfg *cfg = &spec->autocfg;
852
853 if (cfg->line_outs > idx)
854 return spec->private_dac_nids[idx];
855 idx -= cfg->line_outs;
856 if (spec->multi_ios > idx)
857 return spec->multi_io[idx].dac;
858 return 0;
859}
860
861/* return the DAC if it's reachable, otherwise zero */
862static inline hda_nid_t try_dac(struct hda_codec *codec,
863 hda_nid_t dac, hda_nid_t pin)
864{
865 return is_reachable_path(codec, dac, pin) ? dac : 0;
866}
867
Takashi Iwai352f7f92012-12-19 12:52:06 +0100868/* try to assign DACs to pins and return the resultant badness */
869static int try_assign_dacs(struct hda_codec *codec, int num_outs,
870 const hda_nid_t *pins, hda_nid_t *dacs,
Takashi Iwai196c17662013-01-04 15:01:40 +0100871 int *path_idx,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100872 const struct badness_table *bad)
873{
874 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100875 int i, j;
876 int badness = 0;
877 hda_nid_t dac;
878
879 if (!num_outs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 return 0;
881
Takashi Iwai352f7f92012-12-19 12:52:06 +0100882 for (i = 0; i < num_outs; i++) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +0100883 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100884 hda_nid_t pin = pins[i];
Takashi Iwai1e0b5282013-01-04 12:56:52 +0100885
Takashi Iwai0e614dd2013-01-07 15:11:44 +0100886 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
887 if (path) {
888 badness += assign_out_path_ctls(codec, path);
Takashi Iwai1e0b5282013-01-04 12:56:52 +0100889 continue;
890 }
891
892 dacs[i] = look_for_dac(codec, pin, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100893 if (!dacs[i] && !i) {
894 for (j = 1; j < num_outs; j++) {
895 if (is_reachable_path(codec, dacs[j], pin)) {
896 dacs[0] = dacs[j];
897 dacs[j] = 0;
Takashi Iwai196c17662013-01-04 15:01:40 +0100898 path_idx[j] = 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100899 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 }
901 }
Takashi Iwai352f7f92012-12-19 12:52:06 +0100902 }
903 dac = dacs[i];
904 if (!dac) {
Takashi Iwai7385df62013-01-07 09:50:52 +0100905 if (num_outs > 2)
906 dac = try_dac(codec, get_primary_out(codec, i), pin);
907 if (!dac)
908 dac = try_dac(codec, dacs[0], pin);
909 if (!dac)
910 dac = try_dac(codec, get_primary_out(codec, i), pin);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100911 if (dac) {
912 if (!i)
913 badness += bad->shared_primary;
914 else if (i == 1)
915 badness += bad->shared_surr;
916 else
917 badness += bad->shared_clfe;
918 } else if (is_reachable_path(codec, spec->private_dac_nids[0], pin)) {
919 dac = spec->private_dac_nids[0];
920 badness += bad->shared_surr_main;
921 } else if (!i)
922 badness += bad->no_primary_dac;
923 else
924 badness += bad->no_dac;
925 }
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100926 path = snd_hda_add_new_path(codec, dac, pin, -spec->mixer_nid);
Takashi Iwai117688a2013-01-04 15:41:41 +0100927 if (!path && !i && spec->mixer_nid) {
Takashi Iwaib3a8c742012-12-20 18:29:16 +0100928 /* try with aamix */
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100929 path = snd_hda_add_new_path(codec, dac, pin, 0);
Takashi Iwaib3a8c742012-12-20 18:29:16 +0100930 }
Takashi Iwai0c8c0f52012-12-20 17:54:22 +0100931 if (!path)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100932 dac = dacs[i] = 0;
Takashi Iwaie1284af2013-01-03 16:33:02 +0100933 else {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +0100934 print_nid_path("output", path);
Takashi Iwaie1284af2013-01-03 16:33:02 +0100935 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +0100936 path_idx[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai0e614dd2013-01-07 15:11:44 +0100937 badness += assign_out_path_ctls(codec, path);
Takashi Iwaie1284af2013-01-03 16:33:02 +0100938 }
Takashi Iwai352f7f92012-12-19 12:52:06 +0100939 }
940
941 return badness;
942}
943
944/* return NID if the given pin has only a single connection to a certain DAC */
945static hda_nid_t get_dac_if_single(struct hda_codec *codec, hda_nid_t pin)
946{
947 struct hda_gen_spec *spec = codec->spec;
948 int i;
949 hda_nid_t nid_found = 0;
950
951 for (i = 0; i < spec->num_all_dacs; i++) {
952 hda_nid_t nid = spec->all_dacs[i];
953 if (!nid || is_dac_already_used(codec, nid))
954 continue;
955 if (is_reachable_path(codec, nid, pin)) {
956 if (nid_found)
957 return 0;
958 nid_found = nid;
959 }
960 }
961 return nid_found;
962}
963
964/* check whether the given pin can be a multi-io pin */
965static bool can_be_multiio_pin(struct hda_codec *codec,
966 unsigned int location, hda_nid_t nid)
967{
968 unsigned int defcfg, caps;
969
970 defcfg = snd_hda_codec_get_pincfg(codec, nid);
971 if (get_defcfg_connect(defcfg) != AC_JACK_PORT_COMPLEX)
972 return false;
973 if (location && get_defcfg_location(defcfg) != location)
974 return false;
975 caps = snd_hda_query_pin_caps(codec, nid);
976 if (!(caps & AC_PINCAP_OUT))
977 return false;
978 return true;
979}
980
Takashi Iwaie22aab72013-01-04 14:50:04 +0100981/* count the number of input pins that are capable to be multi-io */
982static int count_multiio_pins(struct hda_codec *codec, hda_nid_t reference_pin)
983{
984 struct hda_gen_spec *spec = codec->spec;
985 struct auto_pin_cfg *cfg = &spec->autocfg;
986 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
987 unsigned int location = get_defcfg_location(defcfg);
988 int type, i;
989 int num_pins = 0;
990
991 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
992 for (i = 0; i < cfg->num_inputs; i++) {
993 if (cfg->inputs[i].type != type)
994 continue;
995 if (can_be_multiio_pin(codec, location,
996 cfg->inputs[i].pin))
997 num_pins++;
998 }
999 }
1000 return num_pins;
1001}
1002
Takashi Iwai352f7f92012-12-19 12:52:06 +01001003/*
1004 * multi-io helper
1005 *
1006 * When hardwired is set, try to fill ony hardwired pins, and returns
1007 * zero if any pins are filled, non-zero if nothing found.
1008 * When hardwired is off, try to fill possible input pins, and returns
1009 * the badness value.
1010 */
1011static int fill_multi_ios(struct hda_codec *codec,
1012 hda_nid_t reference_pin,
Takashi Iwaie22aab72013-01-04 14:50:04 +01001013 bool hardwired)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001014{
1015 struct hda_gen_spec *spec = codec->spec;
1016 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwaie22aab72013-01-04 14:50:04 +01001017 int type, i, j, num_pins, old_pins;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001018 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1019 unsigned int location = get_defcfg_location(defcfg);
1020 int badness = 0;
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001021 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001022
1023 old_pins = spec->multi_ios;
1024 if (old_pins >= 2)
1025 goto end_fill;
1026
Takashi Iwaie22aab72013-01-04 14:50:04 +01001027 num_pins = count_multiio_pins(codec, reference_pin);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001028 if (num_pins < 2)
1029 goto end_fill;
1030
Takashi Iwai352f7f92012-12-19 12:52:06 +01001031 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1032 for (i = 0; i < cfg->num_inputs; i++) {
1033 hda_nid_t nid = cfg->inputs[i].pin;
1034 hda_nid_t dac = 0;
1035
1036 if (cfg->inputs[i].type != type)
1037 continue;
1038 if (!can_be_multiio_pin(codec, location, nid))
1039 continue;
1040 for (j = 0; j < spec->multi_ios; j++) {
1041 if (nid == spec->multi_io[j].pin)
1042 break;
1043 }
1044 if (j < spec->multi_ios)
1045 continue;
1046
Takashi Iwai352f7f92012-12-19 12:52:06 +01001047 if (hardwired)
1048 dac = get_dac_if_single(codec, nid);
1049 else if (!dac)
1050 dac = look_for_dac(codec, nid, false);
1051 if (!dac) {
1052 badness++;
1053 continue;
1054 }
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001055 path = snd_hda_add_new_path(codec, dac, nid,
1056 -spec->mixer_nid);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001057 if (!path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001058 badness++;
1059 continue;
1060 }
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001061 print_nid_path("multiio", path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001062 spec->multi_io[spec->multi_ios].pin = nid;
1063 spec->multi_io[spec->multi_ios].dac = dac;
Takashi Iwai196c17662013-01-04 15:01:40 +01001064 spec->out_paths[cfg->line_outs + spec->multi_ios] =
1065 snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001066 spec->multi_ios++;
1067 if (spec->multi_ios >= 2)
1068 break;
1069 }
1070 }
1071 end_fill:
1072 if (badness)
1073 badness = BAD_MULTI_IO;
1074 if (old_pins == spec->multi_ios) {
1075 if (hardwired)
1076 return 1; /* nothing found */
1077 else
1078 return badness; /* no badness if nothing found */
1079 }
1080 if (!hardwired && spec->multi_ios < 2) {
1081 /* cancel newly assigned paths */
1082 spec->paths.used -= spec->multi_ios - old_pins;
1083 spec->multi_ios = old_pins;
1084 return badness;
1085 }
1086
1087 /* assign volume and mute controls */
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001088 for (i = old_pins; i < spec->multi_ios; i++) {
1089 path = snd_hda_get_path_from_idx(codec, spec->out_paths[cfg->line_outs + i]);
1090 badness += assign_out_path_ctls(codec, path);
1091 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001092
1093 return badness;
1094}
1095
1096/* map DACs for all pins in the list if they are single connections */
1097static bool map_singles(struct hda_codec *codec, int outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001098 const hda_nid_t *pins, hda_nid_t *dacs, int *path_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001099{
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001100 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001101 int i;
1102 bool found = false;
1103 for (i = 0; i < outs; i++) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001104 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001105 hda_nid_t dac;
1106 if (dacs[i])
1107 continue;
1108 dac = get_dac_if_single(codec, pins[i]);
1109 if (!dac)
1110 continue;
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001111 path = snd_hda_add_new_path(codec, dac, pins[i],
1112 -spec->mixer_nid);
Takashi Iwai117688a2013-01-04 15:41:41 +01001113 if (!path && !i && spec->mixer_nid)
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001114 path = snd_hda_add_new_path(codec, dac, pins[i], 0);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001115 if (path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001116 dacs[i] = dac;
1117 found = true;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001118 print_nid_path("output", path);
Takashi Iwaie1284af2013-01-03 16:33:02 +01001119 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +01001120 path_idx[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001121 }
1122 }
1123 return found;
1124}
1125
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001126/* create a new path including aamix if available, and return its index */
1127static int check_aamix_out_path(struct hda_codec *codec, int path_idx)
1128{
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001129 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001130 struct nid_path *path;
1131
1132 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001133 if (!path || !path->depth ||
1134 is_nid_contained(path, spec->mixer_nid))
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001135 return 0;
1136 path = snd_hda_add_new_path(codec, path->path[0],
1137 path->path[path->depth - 1],
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001138 spec->mixer_nid);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001139 if (!path)
1140 return 0;
1141 print_nid_path("output-aamix", path);
1142 path->active = false; /* unused as default */
1143 return snd_hda_get_path_idx(codec, path);
1144}
1145
Takashi Iwaia07a9492013-01-07 16:44:06 +01001146/* fill the empty entries in the dac array for speaker/hp with the
1147 * shared dac pointed by the paths
1148 */
1149static void refill_shared_dacs(struct hda_codec *codec, int num_outs,
1150 hda_nid_t *dacs, int *path_idx)
1151{
1152 struct nid_path *path;
1153 int i;
1154
1155 for (i = 0; i < num_outs; i++) {
1156 if (dacs[i])
1157 continue;
1158 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
1159 if (!path)
1160 continue;
1161 dacs[i] = path->path[0];
1162 }
1163}
1164
Takashi Iwai352f7f92012-12-19 12:52:06 +01001165/* fill in the dac_nids table from the parsed pin configuration */
1166static int fill_and_eval_dacs(struct hda_codec *codec,
1167 bool fill_hardwired,
1168 bool fill_mio_first)
1169{
1170 struct hda_gen_spec *spec = codec->spec;
1171 struct auto_pin_cfg *cfg = &spec->autocfg;
1172 int i, err, badness;
1173
1174 /* set num_dacs once to full for look_for_dac() */
1175 spec->multiout.num_dacs = cfg->line_outs;
1176 spec->multiout.dac_nids = spec->private_dac_nids;
1177 memset(spec->private_dac_nids, 0, sizeof(spec->private_dac_nids));
1178 memset(spec->multiout.hp_out_nid, 0, sizeof(spec->multiout.hp_out_nid));
1179 memset(spec->multiout.extra_out_nid, 0, sizeof(spec->multiout.extra_out_nid));
1180 spec->multi_ios = 0;
1181 snd_array_free(&spec->paths);
Takashi Iwaicd5be3f2013-01-07 15:07:00 +01001182
1183 /* clear path indices */
1184 memset(spec->out_paths, 0, sizeof(spec->out_paths));
1185 memset(spec->hp_paths, 0, sizeof(spec->hp_paths));
1186 memset(spec->speaker_paths, 0, sizeof(spec->speaker_paths));
1187 memset(spec->aamix_out_paths, 0, sizeof(spec->aamix_out_paths));
1188 memset(spec->digout_paths, 0, sizeof(spec->digout_paths));
Takashi Iwaic697b712013-01-07 17:09:26 +01001189 memset(spec->input_paths, 0, sizeof(spec->input_paths));
Takashi Iwaicd5be3f2013-01-07 15:07:00 +01001190 memset(spec->loopback_paths, 0, sizeof(spec->loopback_paths));
1191 memset(&spec->digin_path, 0, sizeof(spec->digin_path));
1192
Takashi Iwai352f7f92012-12-19 12:52:06 +01001193 badness = 0;
1194
1195 /* fill hard-wired DACs first */
1196 if (fill_hardwired) {
1197 bool mapped;
1198 do {
1199 mapped = map_singles(codec, cfg->line_outs,
1200 cfg->line_out_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001201 spec->private_dac_nids,
1202 spec->out_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001203 mapped |= map_singles(codec, cfg->hp_outs,
1204 cfg->hp_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001205 spec->multiout.hp_out_nid,
1206 spec->hp_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001207 mapped |= map_singles(codec, cfg->speaker_outs,
1208 cfg->speaker_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001209 spec->multiout.extra_out_nid,
1210 spec->speaker_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001211 if (fill_mio_first && cfg->line_outs == 1 &&
1212 cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
Takashi Iwaie22aab72013-01-04 14:50:04 +01001213 err = fill_multi_ios(codec, cfg->line_out_pins[0], true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001214 if (!err)
1215 mapped = true;
1216 }
1217 } while (mapped);
1218 }
1219
1220 badness += try_assign_dacs(codec, cfg->line_outs, cfg->line_out_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001221 spec->private_dac_nids, spec->out_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001222 &main_out_badness);
1223
Takashi Iwai352f7f92012-12-19 12:52:06 +01001224 if (fill_mio_first &&
1225 cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1226 /* try to fill multi-io first */
Takashi Iwaie22aab72013-01-04 14:50:04 +01001227 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001228 if (err < 0)
1229 return err;
1230 /* we don't count badness at this stage yet */
1231 }
1232
1233 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
1234 err = try_assign_dacs(codec, cfg->hp_outs, cfg->hp_pins,
1235 spec->multiout.hp_out_nid,
Takashi Iwai196c17662013-01-04 15:01:40 +01001236 spec->hp_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001237 &extra_out_badness);
1238 if (err < 0)
1239 return err;
1240 badness += err;
1241 }
1242 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1243 err = try_assign_dacs(codec, cfg->speaker_outs,
1244 cfg->speaker_pins,
1245 spec->multiout.extra_out_nid,
Takashi Iwai196c17662013-01-04 15:01:40 +01001246 spec->speaker_paths,
1247 &extra_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001248 if (err < 0)
1249 return err;
1250 badness += err;
1251 }
1252 if (cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
Takashi Iwaie22aab72013-01-04 14:50:04 +01001253 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001254 if (err < 0)
1255 return err;
1256 badness += err;
1257 }
Takashi Iwaie22aab72013-01-04 14:50:04 +01001258
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001259 if (spec->mixer_nid) {
1260 spec->aamix_out_paths[0] =
1261 check_aamix_out_path(codec, spec->out_paths[0]);
1262 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1263 spec->aamix_out_paths[1] =
1264 check_aamix_out_path(codec, spec->hp_paths[0]);
1265 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1266 spec->aamix_out_paths[2] =
1267 check_aamix_out_path(codec, spec->speaker_paths[0]);
1268 }
1269
Takashi Iwaie22aab72013-01-04 14:50:04 +01001270 if (cfg->hp_outs && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
1271 if (count_multiio_pins(codec, cfg->hp_pins[0]) >= 2)
1272 spec->multi_ios = 1; /* give badness */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001273
Takashi Iwaia07a9492013-01-07 16:44:06 +01001274 /* re-count num_dacs and squash invalid entries */
1275 spec->multiout.num_dacs = 0;
1276 for (i = 0; i < cfg->line_outs; i++) {
1277 if (spec->private_dac_nids[i])
1278 spec->multiout.num_dacs++;
1279 else {
1280 memmove(spec->private_dac_nids + i,
1281 spec->private_dac_nids + i + 1,
1282 sizeof(hda_nid_t) * (cfg->line_outs - i - 1));
1283 spec->private_dac_nids[cfg->line_outs - 1] = 0;
1284 }
1285 }
1286
1287 spec->ext_channel_count = spec->min_channel_count =
1288 spec->multiout.num_dacs;
1289
Takashi Iwai352f7f92012-12-19 12:52:06 +01001290 if (spec->multi_ios == 2) {
1291 for (i = 0; i < 2; i++)
1292 spec->private_dac_nids[spec->multiout.num_dacs++] =
1293 spec->multi_io[i].dac;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001294 } else if (spec->multi_ios) {
1295 spec->multi_ios = 0;
1296 badness += BAD_MULTI_IO;
1297 }
1298
Takashi Iwaia07a9492013-01-07 16:44:06 +01001299 /* re-fill the shared DAC for speaker / headphone */
1300 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1301 refill_shared_dacs(codec, cfg->hp_outs,
1302 spec->multiout.hp_out_nid,
1303 spec->hp_paths);
1304 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1305 refill_shared_dacs(codec, cfg->speaker_outs,
1306 spec->multiout.extra_out_nid,
1307 spec->speaker_paths);
1308
Takashi Iwai352f7f92012-12-19 12:52:06 +01001309 return badness;
1310}
1311
1312#define DEBUG_BADNESS
1313
1314#ifdef DEBUG_BADNESS
1315#define debug_badness snd_printdd
1316#else
1317#define debug_badness(...)
1318#endif
1319
1320static void debug_show_configs(struct hda_gen_spec *spec, struct auto_pin_cfg *cfg)
1321{
1322 debug_badness("multi_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1323 cfg->line_out_pins[0], cfg->line_out_pins[1],
Takashi Iwai708122e2012-12-20 17:56:57 +01001324 cfg->line_out_pins[2], cfg->line_out_pins[3],
Takashi Iwai352f7f92012-12-19 12:52:06 +01001325 spec->multiout.dac_nids[0],
1326 spec->multiout.dac_nids[1],
1327 spec->multiout.dac_nids[2],
1328 spec->multiout.dac_nids[3]);
1329 if (spec->multi_ios > 0)
1330 debug_badness("multi_ios(%d) = %x/%x : %x/%x\n",
1331 spec->multi_ios,
1332 spec->multi_io[0].pin, spec->multi_io[1].pin,
1333 spec->multi_io[0].dac, spec->multi_io[1].dac);
1334 debug_badness("hp_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1335 cfg->hp_pins[0], cfg->hp_pins[1],
Takashi Iwai708122e2012-12-20 17:56:57 +01001336 cfg->hp_pins[2], cfg->hp_pins[3],
Takashi Iwai352f7f92012-12-19 12:52:06 +01001337 spec->multiout.hp_out_nid[0],
1338 spec->multiout.hp_out_nid[1],
1339 spec->multiout.hp_out_nid[2],
1340 spec->multiout.hp_out_nid[3]);
1341 debug_badness("spk_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1342 cfg->speaker_pins[0], cfg->speaker_pins[1],
1343 cfg->speaker_pins[2], cfg->speaker_pins[3],
1344 spec->multiout.extra_out_nid[0],
1345 spec->multiout.extra_out_nid[1],
1346 spec->multiout.extra_out_nid[2],
1347 spec->multiout.extra_out_nid[3]);
1348}
1349
1350/* find all available DACs of the codec */
1351static void fill_all_dac_nids(struct hda_codec *codec)
1352{
1353 struct hda_gen_spec *spec = codec->spec;
1354 int i;
1355 hda_nid_t nid = codec->start_nid;
1356
1357 spec->num_all_dacs = 0;
1358 memset(spec->all_dacs, 0, sizeof(spec->all_dacs));
1359 for (i = 0; i < codec->num_nodes; i++, nid++) {
1360 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_AUD_OUT)
1361 continue;
1362 if (spec->num_all_dacs >= ARRAY_SIZE(spec->all_dacs)) {
1363 snd_printk(KERN_ERR "hda: Too many DACs!\n");
1364 break;
1365 }
1366 spec->all_dacs[spec->num_all_dacs++] = nid;
1367 }
1368}
1369
1370static int parse_output_paths(struct hda_codec *codec)
1371{
1372 struct hda_gen_spec *spec = codec->spec;
1373 struct auto_pin_cfg *cfg = &spec->autocfg;
1374 struct auto_pin_cfg *best_cfg;
1375 int best_badness = INT_MAX;
1376 int badness;
1377 bool fill_hardwired = true, fill_mio_first = true;
1378 bool best_wired = true, best_mio = true;
1379 bool hp_spk_swapped = false;
1380
1381 fill_all_dac_nids(codec);
1382
1383 best_cfg = kmalloc(sizeof(*best_cfg), GFP_KERNEL);
1384 if (!best_cfg)
1385 return -ENOMEM;
1386 *best_cfg = *cfg;
1387
1388 for (;;) {
1389 badness = fill_and_eval_dacs(codec, fill_hardwired,
1390 fill_mio_first);
1391 if (badness < 0) {
1392 kfree(best_cfg);
1393 return badness;
1394 }
1395 debug_badness("==> lo_type=%d, wired=%d, mio=%d, badness=0x%x\n",
1396 cfg->line_out_type, fill_hardwired, fill_mio_first,
1397 badness);
1398 debug_show_configs(spec, cfg);
1399 if (badness < best_badness) {
1400 best_badness = badness;
1401 *best_cfg = *cfg;
1402 best_wired = fill_hardwired;
1403 best_mio = fill_mio_first;
1404 }
1405 if (!badness)
1406 break;
1407 fill_mio_first = !fill_mio_first;
1408 if (!fill_mio_first)
1409 continue;
1410 fill_hardwired = !fill_hardwired;
1411 if (!fill_hardwired)
1412 continue;
1413 if (hp_spk_swapped)
1414 break;
1415 hp_spk_swapped = true;
1416 if (cfg->speaker_outs > 0 &&
1417 cfg->line_out_type == AUTO_PIN_HP_OUT) {
1418 cfg->hp_outs = cfg->line_outs;
1419 memcpy(cfg->hp_pins, cfg->line_out_pins,
1420 sizeof(cfg->hp_pins));
1421 cfg->line_outs = cfg->speaker_outs;
1422 memcpy(cfg->line_out_pins, cfg->speaker_pins,
1423 sizeof(cfg->speaker_pins));
1424 cfg->speaker_outs = 0;
1425 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
1426 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
1427 fill_hardwired = true;
1428 continue;
1429 }
1430 if (cfg->hp_outs > 0 &&
1431 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
1432 cfg->speaker_outs = cfg->line_outs;
1433 memcpy(cfg->speaker_pins, cfg->line_out_pins,
1434 sizeof(cfg->speaker_pins));
1435 cfg->line_outs = cfg->hp_outs;
1436 memcpy(cfg->line_out_pins, cfg->hp_pins,
1437 sizeof(cfg->hp_pins));
1438 cfg->hp_outs = 0;
1439 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
1440 cfg->line_out_type = AUTO_PIN_HP_OUT;
1441 fill_hardwired = true;
1442 continue;
1443 }
1444 break;
1445 }
1446
1447 if (badness) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001448 debug_badness("==> restoring best_cfg\n");
Takashi Iwai352f7f92012-12-19 12:52:06 +01001449 *cfg = *best_cfg;
1450 fill_and_eval_dacs(codec, best_wired, best_mio);
1451 }
1452 debug_badness("==> Best config: lo_type=%d, wired=%d, mio=%d\n",
1453 cfg->line_out_type, best_wired, best_mio);
1454 debug_show_configs(spec, cfg);
1455
1456 if (cfg->line_out_pins[0]) {
1457 struct nid_path *path;
Takashi Iwai196c17662013-01-04 15:01:40 +01001458 path = snd_hda_get_path_from_idx(codec, spec->out_paths[0]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001459 if (path)
1460 spec->vmaster_nid = look_for_out_vol_nid(codec, path);
1461 }
1462
1463 kfree(best_cfg);
1464 return 0;
1465}
1466
1467/* add playback controls from the parsed DAC table */
1468static int create_multi_out_ctls(struct hda_codec *codec,
1469 const struct auto_pin_cfg *cfg)
1470{
1471 struct hda_gen_spec *spec = codec->spec;
1472 int i, err, noutputs;
1473
1474 noutputs = cfg->line_outs;
1475 if (spec->multi_ios > 0 && cfg->line_outs < 3)
1476 noutputs += spec->multi_ios;
1477
1478 for (i = 0; i < noutputs; i++) {
1479 const char *name;
1480 int index;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001481 struct nid_path *path;
1482
Takashi Iwai352f7f92012-12-19 12:52:06 +01001483 if (i >= cfg->line_outs) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001484 index = 0;
1485 name = channel_name[i];
1486 } else {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001487 name = get_line_out_pfx(spec, i, true, &index);
1488 }
1489
Takashi Iwai196c17662013-01-04 15:01:40 +01001490 path = snd_hda_get_path_from_idx(codec, spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001491 if (!path)
1492 continue;
1493 if (!name || !strcmp(name, "CLFE")) {
1494 /* Center/LFE */
1495 err = add_vol_ctl(codec, "Center", 0, 1, path);
1496 if (err < 0)
1497 return err;
1498 err = add_vol_ctl(codec, "LFE", 0, 2, path);
1499 if (err < 0)
1500 return err;
1501 err = add_sw_ctl(codec, "Center", 0, 1, path);
1502 if (err < 0)
1503 return err;
1504 err = add_sw_ctl(codec, "LFE", 0, 2, path);
1505 if (err < 0)
1506 return err;
1507 } else {
1508 err = add_stereo_vol(codec, name, index, path);
1509 if (err < 0)
1510 return err;
1511 err = add_stereo_sw(codec, name, index, path);
1512 if (err < 0)
1513 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 }
1515 }
1516 return 0;
1517}
1518
Takashi Iwaic2c80382013-01-07 10:33:57 +01001519static int create_extra_out(struct hda_codec *codec, int path_idx,
Takashi Iwai196c17662013-01-04 15:01:40 +01001520 const char *pfx, int cidx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521{
Takashi Iwai352f7f92012-12-19 12:52:06 +01001522 struct nid_path *path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 int err;
1524
Takashi Iwai196c17662013-01-04 15:01:40 +01001525 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001526 if (!path)
1527 return 0;
Takashi Iwaic2c80382013-01-07 10:33:57 +01001528 err = add_stereo_vol(codec, pfx, cidx, path);
1529 if (err < 0)
1530 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001531 err = add_stereo_sw(codec, pfx, cidx, path);
1532 if (err < 0)
1533 return err;
1534 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535}
1536
Takashi Iwai352f7f92012-12-19 12:52:06 +01001537/* add playback controls for speaker and HP outputs */
1538static int create_extra_outs(struct hda_codec *codec, int num_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001539 const int *paths, const char *pfx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540{
Takashi Iwaic2c80382013-01-07 10:33:57 +01001541 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001542
1543 for (i = 0; i < num_pins; i++) {
Takashi Iwaic2c80382013-01-07 10:33:57 +01001544 const char *name;
1545 char tmp[44];
1546 int err, idx = 0;
1547
1548 if (num_pins == 2 && i == 1 && !strcmp(pfx, "Speaker"))
1549 name = "Bass Speaker";
1550 else if (num_pins >= 3) {
1551 snprintf(tmp, sizeof(tmp), "%s %s",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001552 pfx, channel_name[i]);
Takashi Iwaic2c80382013-01-07 10:33:57 +01001553 name = tmp;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001554 } else {
Takashi Iwaic2c80382013-01-07 10:33:57 +01001555 name = pfx;
1556 idx = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 }
Takashi Iwaic2c80382013-01-07 10:33:57 +01001558 err = create_extra_out(codec, paths[i], name, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001559 if (err < 0)
1560 return err;
1561 }
1562 return 0;
1563}
Takashi Iwai97ec5582006-03-21 11:29:07 +01001564
Takashi Iwai352f7f92012-12-19 12:52:06 +01001565static int create_hp_out_ctls(struct hda_codec *codec)
1566{
1567 struct hda_gen_spec *spec = codec->spec;
1568 return create_extra_outs(codec, spec->autocfg.hp_outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001569 spec->hp_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001570 "Headphone");
1571}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572
Takashi Iwai352f7f92012-12-19 12:52:06 +01001573static int create_speaker_out_ctls(struct hda_codec *codec)
1574{
1575 struct hda_gen_spec *spec = codec->spec;
1576 return create_extra_outs(codec, spec->autocfg.speaker_outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001577 spec->speaker_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001578 "Speaker");
1579}
1580
1581/*
Takashi Iwai38cf6f12012-12-21 14:09:42 +01001582 * independent HP controls
1583 */
1584
1585static int indep_hp_info(struct snd_kcontrol *kcontrol,
1586 struct snd_ctl_elem_info *uinfo)
1587{
1588 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
1589}
1590
1591static int indep_hp_get(struct snd_kcontrol *kcontrol,
1592 struct snd_ctl_elem_value *ucontrol)
1593{
1594 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1595 struct hda_gen_spec *spec = codec->spec;
1596 ucontrol->value.enumerated.item[0] = spec->indep_hp_enabled;
1597 return 0;
1598}
1599
1600static int indep_hp_put(struct snd_kcontrol *kcontrol,
1601 struct snd_ctl_elem_value *ucontrol)
1602{
1603 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1604 struct hda_gen_spec *spec = codec->spec;
1605 unsigned int select = ucontrol->value.enumerated.item[0];
1606 int ret = 0;
1607
1608 mutex_lock(&spec->pcm_mutex);
1609 if (spec->active_streams) {
1610 ret = -EBUSY;
1611 goto unlock;
1612 }
1613
1614 if (spec->indep_hp_enabled != select) {
1615 spec->indep_hp_enabled = select;
1616 if (spec->indep_hp_enabled)
1617 spec->multiout.hp_out_nid[0] = 0;
1618 else
1619 spec->multiout.hp_out_nid[0] = spec->alt_dac_nid;
1620 ret = 1;
1621 }
1622 unlock:
1623 mutex_unlock(&spec->pcm_mutex);
1624 return ret;
1625}
1626
1627static const struct snd_kcontrol_new indep_hp_ctl = {
1628 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1629 .name = "Independent HP",
1630 .info = indep_hp_info,
1631 .get = indep_hp_get,
1632 .put = indep_hp_put,
1633};
1634
1635
1636static int create_indep_hp_ctls(struct hda_codec *codec)
1637{
1638 struct hda_gen_spec *spec = codec->spec;
1639
1640 if (!spec->indep_hp)
1641 return 0;
1642 if (!spec->multiout.hp_out_nid[0]) {
1643 spec->indep_hp = 0;
1644 return 0;
1645 }
1646
1647 spec->indep_hp_enabled = false;
1648 spec->alt_dac_nid = spec->multiout.hp_out_nid[0];
1649 if (!snd_hda_gen_add_kctl(spec, NULL, &indep_hp_ctl))
1650 return -ENOMEM;
1651 return 0;
1652}
1653
1654/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01001655 * channel mode enum control
1656 */
1657
1658static int ch_mode_info(struct snd_kcontrol *kcontrol,
1659 struct snd_ctl_elem_info *uinfo)
1660{
1661 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1662 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia07a9492013-01-07 16:44:06 +01001663 int chs;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001664
1665 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1666 uinfo->count = 1;
1667 uinfo->value.enumerated.items = spec->multi_ios + 1;
1668 if (uinfo->value.enumerated.item > spec->multi_ios)
1669 uinfo->value.enumerated.item = spec->multi_ios;
Takashi Iwaia07a9492013-01-07 16:44:06 +01001670 chs = uinfo->value.enumerated.item * 2 + spec->min_channel_count;
1671 sprintf(uinfo->value.enumerated.name, "%dch", chs);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001672 return 0;
1673}
1674
1675static int ch_mode_get(struct snd_kcontrol *kcontrol,
1676 struct snd_ctl_elem_value *ucontrol)
1677{
1678 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1679 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia07a9492013-01-07 16:44:06 +01001680 ucontrol->value.enumerated.item[0] =
1681 (spec->ext_channel_count - spec->min_channel_count) / 2;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001682 return 0;
1683}
1684
Takashi Iwai196c17662013-01-04 15:01:40 +01001685static inline struct nid_path *
1686get_multiio_path(struct hda_codec *codec, int idx)
1687{
1688 struct hda_gen_spec *spec = codec->spec;
1689 return snd_hda_get_path_from_idx(codec,
1690 spec->out_paths[spec->autocfg.line_outs + idx]);
1691}
1692
Takashi Iwai352f7f92012-12-19 12:52:06 +01001693static int set_multi_io(struct hda_codec *codec, int idx, bool output)
1694{
1695 struct hda_gen_spec *spec = codec->spec;
1696 hda_nid_t nid = spec->multi_io[idx].pin;
1697 struct nid_path *path;
1698
Takashi Iwai196c17662013-01-04 15:01:40 +01001699 path = get_multiio_path(codec, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001700 if (!path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 return -EINVAL;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001702
1703 if (path->active == output)
1704 return 0;
1705
1706 if (output) {
1707 snd_hda_set_pin_ctl_cache(codec, nid, PIN_OUT);
1708 snd_hda_activate_path(codec, path, true, true);
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01001709 set_pin_eapd(codec, nid, true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001710 } else {
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01001711 set_pin_eapd(codec, nid, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001712 snd_hda_activate_path(codec, path, false, true);
1713 snd_hda_set_pin_ctl_cache(codec, nid,
1714 spec->multi_io[idx].ctl_in);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001716 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717}
1718
Takashi Iwai352f7f92012-12-19 12:52:06 +01001719static int ch_mode_put(struct snd_kcontrol *kcontrol,
1720 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721{
Takashi Iwai352f7f92012-12-19 12:52:06 +01001722 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1723 struct hda_gen_spec *spec = codec->spec;
1724 int i, ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725
Takashi Iwai352f7f92012-12-19 12:52:06 +01001726 ch = ucontrol->value.enumerated.item[0];
1727 if (ch < 0 || ch > spec->multi_ios)
1728 return -EINVAL;
Takashi Iwaia07a9492013-01-07 16:44:06 +01001729 if (ch == (spec->ext_channel_count - spec->min_channel_count) / 2)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001730 return 0;
Takashi Iwaia07a9492013-01-07 16:44:06 +01001731 spec->ext_channel_count = ch * 2 + spec->min_channel_count;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001732 for (i = 0; i < spec->multi_ios; i++)
1733 set_multi_io(codec, i, i < ch);
1734 spec->multiout.max_channels = max(spec->ext_channel_count,
1735 spec->const_channel_count);
1736 if (spec->need_dac_fix)
1737 spec->multiout.num_dacs = spec->multiout.max_channels / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 return 1;
1739}
1740
Takashi Iwai352f7f92012-12-19 12:52:06 +01001741static const struct snd_kcontrol_new channel_mode_enum = {
1742 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1743 .name = "Channel Mode",
1744 .info = ch_mode_info,
1745 .get = ch_mode_get,
1746 .put = ch_mode_put,
1747};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748
Takashi Iwai352f7f92012-12-19 12:52:06 +01001749static int create_multi_channel_mode(struct hda_codec *codec)
1750{
1751 struct hda_gen_spec *spec = codec->spec;
1752
1753 if (spec->multi_ios > 0) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01001754 if (!snd_hda_gen_add_kctl(spec, NULL, &channel_mode_enum))
Takashi Iwai352f7f92012-12-19 12:52:06 +01001755 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 return 0;
1758}
1759
Takashi Iwai352f7f92012-12-19 12:52:06 +01001760/*
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001761 * aamix loopback enable/disable switch
1762 */
1763
1764#define loopback_mixing_info indep_hp_info
1765
1766static int loopback_mixing_get(struct snd_kcontrol *kcontrol,
1767 struct snd_ctl_elem_value *ucontrol)
1768{
1769 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1770 struct hda_gen_spec *spec = codec->spec;
1771 ucontrol->value.enumerated.item[0] = spec->aamix_mode;
1772 return 0;
1773}
1774
1775static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
1776 int nomix_path_idx, int mix_path_idx)
1777{
1778 struct nid_path *nomix_path, *mix_path;
1779
1780 nomix_path = snd_hda_get_path_from_idx(codec, nomix_path_idx);
1781 mix_path = snd_hda_get_path_from_idx(codec, mix_path_idx);
1782 if (!nomix_path || !mix_path)
1783 return;
1784 if (do_mix) {
1785 snd_hda_activate_path(codec, nomix_path, false, true);
1786 snd_hda_activate_path(codec, mix_path, true, true);
1787 } else {
1788 snd_hda_activate_path(codec, mix_path, false, true);
1789 snd_hda_activate_path(codec, nomix_path, true, true);
1790 }
1791}
1792
1793static int loopback_mixing_put(struct snd_kcontrol *kcontrol,
1794 struct snd_ctl_elem_value *ucontrol)
1795{
1796 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1797 struct hda_gen_spec *spec = codec->spec;
1798 unsigned int val = ucontrol->value.enumerated.item[0];
1799
1800 if (val == spec->aamix_mode)
1801 return 0;
1802 spec->aamix_mode = val;
1803 update_aamix_paths(codec, val, spec->out_paths[0],
1804 spec->aamix_out_paths[0]);
1805 update_aamix_paths(codec, val, spec->hp_paths[0],
1806 spec->aamix_out_paths[1]);
1807 update_aamix_paths(codec, val, spec->speaker_paths[0],
1808 spec->aamix_out_paths[2]);
1809 return 1;
1810}
1811
1812static const struct snd_kcontrol_new loopback_mixing_enum = {
1813 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1814 .name = "Loopback Mixing",
1815 .info = loopback_mixing_info,
1816 .get = loopback_mixing_get,
1817 .put = loopback_mixing_put,
1818};
1819
1820static int create_loopback_mixing_ctl(struct hda_codec *codec)
1821{
1822 struct hda_gen_spec *spec = codec->spec;
1823
1824 if (!spec->mixer_nid)
1825 return 0;
1826 if (!(spec->aamix_out_paths[0] || spec->aamix_out_paths[1] ||
1827 spec->aamix_out_paths[2]))
1828 return 0;
1829 if (!snd_hda_gen_add_kctl(spec, NULL, &loopback_mixing_enum))
1830 return -ENOMEM;
1831 return 0;
1832}
1833
1834/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01001835 * shared headphone/mic handling
1836 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02001837
Takashi Iwai352f7f92012-12-19 12:52:06 +01001838static void call_update_outputs(struct hda_codec *codec);
1839
1840/* for shared I/O, change the pin-control accordingly */
1841static void update_shared_mic_hp(struct hda_codec *codec, bool set_as_mic)
1842{
1843 struct hda_gen_spec *spec = codec->spec;
1844 unsigned int val;
1845 hda_nid_t pin = spec->autocfg.inputs[1].pin;
1846 /* NOTE: this assumes that there are only two inputs, the
1847 * first is the real internal mic and the second is HP/mic jack.
1848 */
1849
1850 val = snd_hda_get_default_vref(codec, pin);
1851
1852 /* This pin does not have vref caps - let's enable vref on pin 0x18
1853 instead, as suggested by Realtek */
1854 if (val == AC_PINCTL_VREF_HIZ && spec->shared_mic_vref_pin) {
1855 const hda_nid_t vref_pin = spec->shared_mic_vref_pin;
1856 unsigned int vref_val = snd_hda_get_default_vref(codec, vref_pin);
1857 if (vref_val != AC_PINCTL_VREF_HIZ)
Takashi Iwai7594aa32012-12-20 15:38:40 +01001858 snd_hda_set_pin_ctl_cache(codec, vref_pin,
1859 PIN_IN | (set_as_mic ? vref_val : 0));
Takashi Iwaicb53c622007-08-10 17:21:45 +02001860 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001861
1862 val = set_as_mic ? val | PIN_IN : PIN_HP;
Takashi Iwai7594aa32012-12-20 15:38:40 +01001863 snd_hda_set_pin_ctl_cache(codec, pin, val);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001864
1865 spec->automute_speaker = !set_as_mic;
1866 call_update_outputs(codec);
1867}
1868
1869/* create a shared input with the headphone out */
1870static int create_shared_input(struct hda_codec *codec)
1871{
1872 struct hda_gen_spec *spec = codec->spec;
1873 struct auto_pin_cfg *cfg = &spec->autocfg;
1874 unsigned int defcfg;
1875 hda_nid_t nid;
1876
1877 /* only one internal input pin? */
1878 if (cfg->num_inputs != 1)
1879 return 0;
1880 defcfg = snd_hda_codec_get_pincfg(codec, cfg->inputs[0].pin);
1881 if (snd_hda_get_input_pin_attr(defcfg) != INPUT_PIN_ATTR_INT)
1882 return 0;
1883
1884 if (cfg->hp_outs == 1 && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
1885 nid = cfg->hp_pins[0]; /* OK, we have a single HP-out */
1886 else if (cfg->line_outs == 1 && cfg->line_out_type == AUTO_PIN_HP_OUT)
1887 nid = cfg->line_out_pins[0]; /* OK, we have a single line-out */
1888 else
1889 return 0; /* both not available */
1890
1891 if (!(snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_IN))
1892 return 0; /* no input */
1893
1894 cfg->inputs[1].pin = nid;
1895 cfg->inputs[1].type = AUTO_PIN_MIC;
1896 cfg->num_inputs = 2;
1897 spec->shared_mic_hp = 1;
1898 snd_printdd("hda-codec: Enable shared I/O jack on NID 0x%x\n", nid);
1899 return 0;
1900}
1901
1902
1903/*
1904 * Parse input paths
1905 */
1906
1907#ifdef CONFIG_PM
1908/* add the powersave loopback-list entry */
1909static void add_loopback_list(struct hda_gen_spec *spec, hda_nid_t mix, int idx)
1910{
1911 struct hda_amp_list *list;
1912
1913 if (spec->num_loopbacks >= ARRAY_SIZE(spec->loopback_list) - 1)
1914 return;
1915 list = spec->loopback_list + spec->num_loopbacks;
1916 list->nid = mix;
1917 list->dir = HDA_INPUT;
1918 list->idx = idx;
1919 spec->num_loopbacks++;
Takashi Iwaicb53c622007-08-10 17:21:45 +02001920 spec->loopback.amplist = spec->loopback_list;
1921}
1922#else
Takashi Iwai352f7f92012-12-19 12:52:06 +01001923#define add_loopback_list(spec, mix, idx) /* NOP */
Takashi Iwaicb53c622007-08-10 17:21:45 +02001924#endif
1925
Takashi Iwai352f7f92012-12-19 12:52:06 +01001926/* create input playback/capture controls for the given pin */
Takashi Iwai196c17662013-01-04 15:01:40 +01001927static int new_analog_input(struct hda_codec *codec, int input_idx,
1928 hda_nid_t pin, const char *ctlname, int ctlidx,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001929 hda_nid_t mix_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930{
Takashi Iwai352f7f92012-12-19 12:52:06 +01001931 struct hda_gen_spec *spec = codec->spec;
1932 struct nid_path *path;
1933 unsigned int val;
1934 int err, idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935
Takashi Iwai352f7f92012-12-19 12:52:06 +01001936 if (!nid_has_volume(codec, mix_nid, HDA_INPUT) &&
1937 !nid_has_mute(codec, mix_nid, HDA_INPUT))
1938 return 0; /* no need for analog loopback */
1939
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001940 path = snd_hda_add_new_path(codec, pin, mix_nid, 0);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001941 if (!path)
1942 return -EINVAL;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001943 print_nid_path("loopback", path);
Takashi Iwai196c17662013-01-04 15:01:40 +01001944 spec->loopback_paths[input_idx] = snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001945
1946 idx = path->idx[path->depth - 1];
1947 if (nid_has_volume(codec, mix_nid, HDA_INPUT)) {
1948 val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
1949 err = __add_pb_vol_ctrl(spec, HDA_CTL_WIDGET_VOL, ctlname, ctlidx, val);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001950 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001952 path->ctls[NID_PATH_VOL_CTL] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 }
1954
Takashi Iwai352f7f92012-12-19 12:52:06 +01001955 if (nid_has_mute(codec, mix_nid, HDA_INPUT)) {
1956 val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
1957 err = __add_pb_sw_ctrl(spec, HDA_CTL_WIDGET_MUTE, ctlname, ctlidx, val);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001958 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001960 path->ctls[NID_PATH_MUTE_CTL] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 }
1962
Takashi Iwai352f7f92012-12-19 12:52:06 +01001963 path->active = true;
1964 add_loopback_list(spec, mix_nid, idx);
1965 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966}
1967
Takashi Iwai352f7f92012-12-19 12:52:06 +01001968static int is_input_pin(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969{
Takashi Iwai352f7f92012-12-19 12:52:06 +01001970 unsigned int pincap = snd_hda_query_pin_caps(codec, nid);
1971 return (pincap & AC_PINCAP_IN) != 0;
1972}
1973
1974/* Parse the codec tree and retrieve ADCs */
1975static int fill_adc_nids(struct hda_codec *codec)
1976{
1977 struct hda_gen_spec *spec = codec->spec;
1978 hda_nid_t nid;
1979 hda_nid_t *adc_nids = spec->adc_nids;
1980 int max_nums = ARRAY_SIZE(spec->adc_nids);
1981 int i, nums = 0;
1982
1983 nid = codec->start_nid;
1984 for (i = 0; i < codec->num_nodes; i++, nid++) {
1985 unsigned int caps = get_wcaps(codec, nid);
1986 int type = get_wcaps_type(caps);
1987
1988 if (type != AC_WID_AUD_IN || (caps & AC_WCAP_DIGITAL))
1989 continue;
1990 adc_nids[nums] = nid;
1991 if (++nums >= max_nums)
1992 break;
1993 }
1994 spec->num_adc_nids = nums;
1995 return nums;
1996}
1997
1998/* filter out invalid adc_nids that don't give all active input pins;
1999 * if needed, check whether dynamic ADC-switching is available
2000 */
2001static int check_dyn_adc_switch(struct hda_codec *codec)
2002{
2003 struct hda_gen_spec *spec = codec->spec;
2004 struct hda_input_mux *imux = &spec->input_mux;
2005 hda_nid_t adc_nids[ARRAY_SIZE(spec->adc_nids)];
2006 int i, n, nums;
2007 hda_nid_t pin, adc;
2008
2009 again:
2010 nums = 0;
2011 for (n = 0; n < spec->num_adc_nids; n++) {
2012 adc = spec->adc_nids[n];
2013 for (i = 0; i < imux->num_items; i++) {
2014 pin = spec->imux_pins[i];
2015 if (!is_reachable_path(codec, pin, adc))
2016 break;
2017 }
2018 if (i >= imux->num_items)
2019 adc_nids[nums++] = adc;
2020 }
2021
2022 if (!nums) {
2023 if (spec->shared_mic_hp) {
2024 spec->shared_mic_hp = 0;
2025 imux->num_items = 1;
2026 goto again;
2027 }
2028
2029 /* check whether ADC-switch is possible */
2030 for (i = 0; i < imux->num_items; i++) {
2031 pin = spec->imux_pins[i];
2032 for (n = 0; n < spec->num_adc_nids; n++) {
2033 adc = spec->adc_nids[n];
2034 if (is_reachable_path(codec, pin, adc)) {
2035 spec->dyn_adc_idx[i] = n;
2036 break;
2037 }
2038 }
2039 }
2040
2041 snd_printdd("hda-codec: enabling ADC switching\n");
2042 spec->dyn_adc_switch = 1;
2043 } else if (nums != spec->num_adc_nids) {
2044 memcpy(spec->adc_nids, adc_nids, nums * sizeof(hda_nid_t));
2045 spec->num_adc_nids = nums;
2046 }
2047
2048 if (imux->num_items == 1 || spec->shared_mic_hp) {
2049 snd_printdd("hda-codec: reducing to a single ADC\n");
2050 spec->num_adc_nids = 1; /* reduce to a single ADC */
2051 }
2052
2053 /* single index for individual volumes ctls */
2054 if (!spec->dyn_adc_switch && spec->multi_cap_vol)
2055 spec->num_adc_nids = 1;
2056
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 return 0;
2058}
2059
2060/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01002061 * create playback/capture controls for input pins
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01002063static int create_input_ctls(struct hda_codec *codec)
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02002064{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002065 struct hda_gen_spec *spec = codec->spec;
2066 const struct auto_pin_cfg *cfg = &spec->autocfg;
2067 hda_nid_t mixer = spec->mixer_nid;
2068 struct hda_input_mux *imux = &spec->input_mux;
2069 int num_adcs;
2070 int i, c, err, type_idx = 0;
2071 const char *prev_label = NULL;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02002072
Takashi Iwai352f7f92012-12-19 12:52:06 +01002073 num_adcs = fill_adc_nids(codec);
2074 if (num_adcs < 0)
2075 return 0;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02002076
Takashi Iwai352f7f92012-12-19 12:52:06 +01002077 for (i = 0; i < cfg->num_inputs; i++) {
2078 hda_nid_t pin;
2079 const char *label;
Takashi Iwaic697b712013-01-07 17:09:26 +01002080 int imux_idx;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002081 bool imux_added;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082
Takashi Iwai352f7f92012-12-19 12:52:06 +01002083 pin = cfg->inputs[i].pin;
2084 if (!is_input_pin(codec, pin))
2085 continue;
2086
2087 label = hda_get_autocfg_input_label(codec, cfg, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002088 if (prev_label && !strcmp(label, prev_label))
2089 type_idx++;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02002090 else
Takashi Iwai352f7f92012-12-19 12:52:06 +01002091 type_idx = 0;
2092 prev_label = label;
2093
2094 if (mixer) {
2095 if (is_reachable_path(codec, pin, mixer)) {
Takashi Iwai196c17662013-01-04 15:01:40 +01002096 err = new_analog_input(codec, i, pin,
Takashi Iwai352f7f92012-12-19 12:52:06 +01002097 label, type_idx, mixer);
2098 if (err < 0)
2099 return err;
2100 }
2101 }
2102
2103 imux_added = false;
Takashi Iwaic697b712013-01-07 17:09:26 +01002104 imux_idx = imux->num_items;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002105 for (c = 0; c < num_adcs; c++) {
2106 struct nid_path *path;
2107 hda_nid_t adc = spec->adc_nids[c];
2108
2109 if (!is_reachable_path(codec, pin, adc))
2110 continue;
Takashi Iwaica296832013-01-09 08:41:41 +01002111 path = snd_hda_add_new_path(codec, pin, adc, -mixer);
2112 if (!path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002113 continue;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01002114 print_nid_path("input", path);
Takashi Iwaic697b712013-01-07 17:09:26 +01002115 spec->input_paths[imux_idx][c] =
2116 snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002117
2118 if (!imux_added) {
2119 spec->imux_pins[imux->num_items] = pin;
2120 snd_hda_add_imux_item(imux, label,
2121 imux->num_items, NULL);
2122 imux_added = true;
2123 }
2124 }
2125 }
2126
2127 return 0;
2128}
2129
2130
2131/*
2132 * input source mux
2133 */
2134
Takashi Iwaic697b712013-01-07 17:09:26 +01002135/* get the input path specified by the given adc and imux indices */
2136static struct nid_path *get_input_path(struct hda_codec *codec, int adc_idx, int imux_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002137{
2138 struct hda_gen_spec *spec = codec->spec;
2139 if (spec->dyn_adc_switch)
2140 adc_idx = spec->dyn_adc_idx[imux_idx];
Takashi Iwaic697b712013-01-07 17:09:26 +01002141 return snd_hda_get_path_from_idx(codec, spec->input_paths[imux_idx][adc_idx]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002142}
2143
2144static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
2145 unsigned int idx);
2146
2147static int mux_enum_info(struct snd_kcontrol *kcontrol,
2148 struct snd_ctl_elem_info *uinfo)
2149{
2150 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2151 struct hda_gen_spec *spec = codec->spec;
2152 return snd_hda_input_mux_info(&spec->input_mux, uinfo);
2153}
2154
2155static int mux_enum_get(struct snd_kcontrol *kcontrol,
2156 struct snd_ctl_elem_value *ucontrol)
2157{
2158 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2159 struct hda_gen_spec *spec = codec->spec;
2160 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
2161
2162 ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
2163 return 0;
2164}
2165
2166static int mux_enum_put(struct snd_kcontrol *kcontrol,
2167 struct snd_ctl_elem_value *ucontrol)
2168{
2169 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2170 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
2171 return mux_select(codec, adc_idx,
2172 ucontrol->value.enumerated.item[0]);
2173}
2174
Takashi Iwai352f7f92012-12-19 12:52:06 +01002175static const struct snd_kcontrol_new cap_src_temp = {
2176 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2177 .name = "Input Source",
2178 .info = mux_enum_info,
2179 .get = mux_enum_get,
2180 .put = mux_enum_put,
2181};
2182
Takashi Iwai47d46ab2012-12-20 11:48:54 +01002183/*
2184 * capture volume and capture switch ctls
2185 */
2186
Takashi Iwai352f7f92012-12-19 12:52:06 +01002187typedef int (*put_call_t)(struct snd_kcontrol *kcontrol,
2188 struct snd_ctl_elem_value *ucontrol);
2189
Takashi Iwai47d46ab2012-12-20 11:48:54 +01002190/* call the given amp update function for all amps in the imux list at once */
Takashi Iwai352f7f92012-12-19 12:52:06 +01002191static int cap_put_caller(struct snd_kcontrol *kcontrol,
2192 struct snd_ctl_elem_value *ucontrol,
2193 put_call_t func, int type)
2194{
2195 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2196 struct hda_gen_spec *spec = codec->spec;
2197 const struct hda_input_mux *imux;
2198 struct nid_path *path;
2199 int i, adc_idx, err = 0;
2200
2201 imux = &spec->input_mux;
2202 adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
2203 mutex_lock(&codec->control_mutex);
Takashi Iwai47d46ab2012-12-20 11:48:54 +01002204 /* we use the cache-only update at first since multiple input paths
2205 * may shared the same amp; by updating only caches, the redundant
2206 * writes to hardware can be reduced.
2207 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01002208 codec->cached_write = 1;
2209 for (i = 0; i < imux->num_items; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01002210 path = get_input_path(codec, adc_idx, i);
2211 if (!path || !path->ctls[type])
Takashi Iwai352f7f92012-12-19 12:52:06 +01002212 continue;
2213 kcontrol->private_value = path->ctls[type];
2214 err = func(kcontrol, ucontrol);
2215 if (err < 0)
2216 goto error;
2217 }
2218 error:
2219 codec->cached_write = 0;
2220 mutex_unlock(&codec->control_mutex);
Takashi Iwai47d46ab2012-12-20 11:48:54 +01002221 snd_hda_codec_flush_amp_cache(codec); /* flush the updates */
Takashi Iwai352f7f92012-12-19 12:52:06 +01002222 if (err >= 0 && spec->cap_sync_hook)
2223 spec->cap_sync_hook(codec);
2224 return err;
2225}
2226
2227/* capture volume ctl callbacks */
2228#define cap_vol_info snd_hda_mixer_amp_volume_info
2229#define cap_vol_get snd_hda_mixer_amp_volume_get
2230#define cap_vol_tlv snd_hda_mixer_amp_tlv
2231
2232static int cap_vol_put(struct snd_kcontrol *kcontrol,
2233 struct snd_ctl_elem_value *ucontrol)
2234{
2235 return cap_put_caller(kcontrol, ucontrol,
2236 snd_hda_mixer_amp_volume_put,
2237 NID_PATH_VOL_CTL);
2238}
2239
2240static const struct snd_kcontrol_new cap_vol_temp = {
2241 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2242 .name = "Capture Volume",
2243 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
2244 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
2245 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK),
2246 .info = cap_vol_info,
2247 .get = cap_vol_get,
2248 .put = cap_vol_put,
2249 .tlv = { .c = cap_vol_tlv },
2250};
2251
2252/* capture switch ctl callbacks */
2253#define cap_sw_info snd_ctl_boolean_stereo_info
2254#define cap_sw_get snd_hda_mixer_amp_switch_get
2255
2256static int cap_sw_put(struct snd_kcontrol *kcontrol,
2257 struct snd_ctl_elem_value *ucontrol)
2258{
2259 return cap_put_caller(kcontrol, ucontrol,
2260 snd_hda_mixer_amp_switch_put,
2261 NID_PATH_MUTE_CTL);
2262}
2263
2264static const struct snd_kcontrol_new cap_sw_temp = {
2265 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2266 .name = "Capture Switch",
2267 .info = cap_sw_info,
2268 .get = cap_sw_get,
2269 .put = cap_sw_put,
2270};
2271
2272static int parse_capvol_in_path(struct hda_codec *codec, struct nid_path *path)
2273{
2274 hda_nid_t nid;
2275 int i, depth;
2276
2277 path->ctls[NID_PATH_VOL_CTL] = path->ctls[NID_PATH_MUTE_CTL] = 0;
2278 for (depth = 0; depth < 3; depth++) {
2279 if (depth >= path->depth)
2280 return -EINVAL;
2281 i = path->depth - depth - 1;
2282 nid = path->path[i];
2283 if (!path->ctls[NID_PATH_VOL_CTL]) {
2284 if (nid_has_volume(codec, nid, HDA_OUTPUT))
2285 path->ctls[NID_PATH_VOL_CTL] =
2286 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
2287 else if (nid_has_volume(codec, nid, HDA_INPUT)) {
2288 int idx = path->idx[i];
2289 if (!depth && codec->single_adc_amp)
2290 idx = 0;
2291 path->ctls[NID_PATH_VOL_CTL] =
2292 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
2293 }
2294 }
2295 if (!path->ctls[NID_PATH_MUTE_CTL]) {
2296 if (nid_has_mute(codec, nid, HDA_OUTPUT))
2297 path->ctls[NID_PATH_MUTE_CTL] =
2298 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
2299 else if (nid_has_mute(codec, nid, HDA_INPUT)) {
2300 int idx = path->idx[i];
2301 if (!depth && codec->single_adc_amp)
2302 idx = 0;
2303 path->ctls[NID_PATH_MUTE_CTL] =
2304 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
2305 }
2306 }
Takashi Iwai97ec5582006-03-21 11:29:07 +01002307 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 return 0;
2309}
2310
Takashi Iwai352f7f92012-12-19 12:52:06 +01002311static bool is_inv_dmic_pin(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002313 struct hda_gen_spec *spec = codec->spec;
2314 struct auto_pin_cfg *cfg = &spec->autocfg;
2315 unsigned int val;
2316 int i;
2317
2318 if (!spec->inv_dmic_split)
2319 return false;
2320 for (i = 0; i < cfg->num_inputs; i++) {
2321 if (cfg->inputs[i].pin != nid)
2322 continue;
2323 if (cfg->inputs[i].type != AUTO_PIN_MIC)
2324 return false;
2325 val = snd_hda_codec_get_pincfg(codec, nid);
2326 return snd_hda_get_input_pin_attr(val) == INPUT_PIN_ATTR_INT;
2327 }
2328 return false;
2329}
2330
2331static int add_single_cap_ctl(struct hda_codec *codec, const char *label,
2332 int idx, bool is_switch, unsigned int ctl,
2333 bool inv_dmic)
2334{
2335 struct hda_gen_spec *spec = codec->spec;
2336 char tmpname[44];
2337 int type = is_switch ? HDA_CTL_WIDGET_MUTE : HDA_CTL_WIDGET_VOL;
2338 const char *sfx = is_switch ? "Switch" : "Volume";
2339 unsigned int chs = inv_dmic ? 1 : 3;
2340 int err;
2341
2342 if (!ctl)
2343 return 0;
2344
2345 if (label)
2346 snprintf(tmpname, sizeof(tmpname),
2347 "%s Capture %s", label, sfx);
2348 else
2349 snprintf(tmpname, sizeof(tmpname),
2350 "Capture %s", sfx);
2351 err = add_control(spec, type, tmpname, idx,
2352 amp_val_replace_channels(ctl, chs));
2353 if (err < 0 || !inv_dmic)
2354 return err;
2355
2356 /* Make independent right kcontrol */
2357 if (label)
2358 snprintf(tmpname, sizeof(tmpname),
2359 "Inverted %s Capture %s", label, sfx);
2360 else
2361 snprintf(tmpname, sizeof(tmpname),
2362 "Inverted Capture %s", sfx);
2363 return add_control(spec, type, tmpname, idx,
2364 amp_val_replace_channels(ctl, 2));
2365}
2366
2367/* create single (and simple) capture volume and switch controls */
2368static int create_single_cap_vol_ctl(struct hda_codec *codec, int idx,
2369 unsigned int vol_ctl, unsigned int sw_ctl,
2370 bool inv_dmic)
2371{
2372 int err;
2373 err = add_single_cap_ctl(codec, NULL, idx, false, vol_ctl, inv_dmic);
2374 if (err < 0)
2375 return err;
2376 err = add_single_cap_ctl(codec, NULL, idx, true, sw_ctl, inv_dmic);
2377 if (err < 0)
2378 return err;
2379 return 0;
2380}
2381
2382/* create bound capture volume and switch controls */
2383static int create_bind_cap_vol_ctl(struct hda_codec *codec, int idx,
2384 unsigned int vol_ctl, unsigned int sw_ctl)
2385{
2386 struct hda_gen_spec *spec = codec->spec;
2387 struct snd_kcontrol_new *knew;
2388
2389 if (vol_ctl) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01002390 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_vol_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002391 if (!knew)
2392 return -ENOMEM;
2393 knew->index = idx;
2394 knew->private_value = vol_ctl;
2395 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
2396 }
2397 if (sw_ctl) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01002398 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_sw_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002399 if (!knew)
2400 return -ENOMEM;
2401 knew->index = idx;
2402 knew->private_value = sw_ctl;
2403 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
2404 }
2405 return 0;
2406}
2407
2408/* return the vol ctl when used first in the imux list */
2409static unsigned int get_first_cap_ctl(struct hda_codec *codec, int idx, int type)
2410{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002411 struct nid_path *path;
2412 unsigned int ctl;
2413 int i;
2414
Takashi Iwaic697b712013-01-07 17:09:26 +01002415 path = get_input_path(codec, 0, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002416 if (!path)
2417 return 0;
2418 ctl = path->ctls[type];
2419 if (!ctl)
2420 return 0;
2421 for (i = 0; i < idx - 1; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01002422 path = get_input_path(codec, 0, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002423 if (path && path->ctls[type] == ctl)
2424 return 0;
2425 }
2426 return ctl;
2427}
2428
2429/* create individual capture volume and switch controls per input */
2430static int create_multi_cap_vol_ctl(struct hda_codec *codec)
2431{
2432 struct hda_gen_spec *spec = codec->spec;
2433 struct hda_input_mux *imux = &spec->input_mux;
2434 int i, err, type, type_idx = 0;
2435 const char *prev_label = NULL;
2436
2437 for (i = 0; i < imux->num_items; i++) {
2438 const char *label;
2439 bool inv_dmic;
2440 label = hda_get_autocfg_input_label(codec, &spec->autocfg, i);
2441 if (prev_label && !strcmp(label, prev_label))
2442 type_idx++;
2443 else
2444 type_idx = 0;
2445 prev_label = label;
2446 inv_dmic = is_inv_dmic_pin(codec, spec->imux_pins[i]);
2447
2448 for (type = 0; type < 2; type++) {
2449 err = add_single_cap_ctl(codec, label, type_idx, type,
2450 get_first_cap_ctl(codec, i, type),
2451 inv_dmic);
2452 if (err < 0)
2453 return err;
2454 }
2455 }
2456 return 0;
2457}
2458
2459static int create_capture_mixers(struct hda_codec *codec)
2460{
2461 struct hda_gen_spec *spec = codec->spec;
2462 struct hda_input_mux *imux = &spec->input_mux;
2463 int i, n, nums, err;
2464
2465 if (spec->dyn_adc_switch)
2466 nums = 1;
2467 else
2468 nums = spec->num_adc_nids;
2469
2470 if (!spec->auto_mic && imux->num_items > 1) {
2471 struct snd_kcontrol_new *knew;
Takashi Iwai624d9142012-12-19 17:41:52 +01002472 const char *name;
2473 name = nums > 1 ? "Input Source" : "Capture Source";
2474 knew = snd_hda_gen_add_kctl(spec, name, &cap_src_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002475 if (!knew)
2476 return -ENOMEM;
2477 knew->count = nums;
2478 }
2479
2480 for (n = 0; n < nums; n++) {
2481 bool multi = false;
2482 bool inv_dmic = false;
2483 int vol, sw;
2484
2485 vol = sw = 0;
2486 for (i = 0; i < imux->num_items; i++) {
2487 struct nid_path *path;
Takashi Iwaic697b712013-01-07 17:09:26 +01002488 path = get_input_path(codec, n, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002489 if (!path)
2490 continue;
2491 parse_capvol_in_path(codec, path);
2492 if (!vol)
2493 vol = path->ctls[NID_PATH_VOL_CTL];
2494 else if (vol != path->ctls[NID_PATH_VOL_CTL])
2495 multi = true;
2496 if (!sw)
2497 sw = path->ctls[NID_PATH_MUTE_CTL];
2498 else if (sw != path->ctls[NID_PATH_MUTE_CTL])
2499 multi = true;
2500 if (is_inv_dmic_pin(codec, spec->imux_pins[i]))
2501 inv_dmic = true;
2502 }
2503
2504 if (!multi)
2505 err = create_single_cap_vol_ctl(codec, n, vol, sw,
2506 inv_dmic);
2507 else if (!spec->multi_cap_vol)
2508 err = create_bind_cap_vol_ctl(codec, n, vol, sw);
2509 else
2510 err = create_multi_cap_vol_ctl(codec);
2511 if (err < 0)
2512 return err;
2513 }
2514
2515 return 0;
2516}
2517
2518/*
2519 * add mic boosts if needed
2520 */
2521static int parse_mic_boost(struct hda_codec *codec)
2522{
2523 struct hda_gen_spec *spec = codec->spec;
2524 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai071c73a2006-08-23 18:34:06 +02002525 int i, err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002526 int type_idx = 0;
2527 hda_nid_t nid;
2528 const char *prev_label = NULL;
2529
2530 for (i = 0; i < cfg->num_inputs; i++) {
2531 if (cfg->inputs[i].type > AUTO_PIN_MIC)
2532 break;
2533 nid = cfg->inputs[i].pin;
2534 if (get_wcaps(codec, nid) & AC_WCAP_IN_AMP) {
2535 const char *label;
Takashi Iwai5abd4882013-01-07 09:43:18 +01002536 char boost_label[44];
Takashi Iwai352f7f92012-12-19 12:52:06 +01002537 struct nid_path *path;
2538 unsigned int val;
2539
2540 label = hda_get_autocfg_input_label(codec, cfg, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002541 if (prev_label && !strcmp(label, prev_label))
2542 type_idx++;
2543 else
2544 type_idx = 0;
2545 prev_label = label;
2546
2547 snprintf(boost_label, sizeof(boost_label),
2548 "%s Boost Volume", label);
2549 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT);
2550 err = add_control(spec, HDA_CTL_WIDGET_VOL,
2551 boost_label, type_idx, val);
2552 if (err < 0)
2553 return err;
2554
2555 path = snd_hda_get_nid_path(codec, nid, 0);
2556 if (path)
2557 path->ctls[NID_PATH_BOOST_CTL] = val;
2558 }
2559 }
2560 return 0;
2561}
2562
2563/*
2564 * parse digital I/Os and set up NIDs in BIOS auto-parse mode
2565 */
2566static void parse_digital(struct hda_codec *codec)
2567{
2568 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01002569 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002570 int i, nums;
2571 hda_nid_t dig_nid;
2572
2573 /* support multiple SPDIFs; the secondary is set up as a slave */
2574 nums = 0;
2575 for (i = 0; i < spec->autocfg.dig_outs; i++) {
2576 hda_nid_t pin = spec->autocfg.dig_out_pins[i];
2577 dig_nid = look_for_dac(codec, pin, true);
2578 if (!dig_nid)
2579 continue;
Takashi Iwai3ca529d2013-01-07 17:25:08 +01002580 path = snd_hda_add_new_path(codec, dig_nid, pin, 0);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01002581 if (!path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002582 continue;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01002583 print_nid_path("digout", path);
Takashi Iwaie1284af2013-01-03 16:33:02 +01002584 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +01002585 spec->digout_paths[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002586 if (!nums) {
2587 spec->multiout.dig_out_nid = dig_nid;
2588 spec->dig_out_type = spec->autocfg.dig_out_type[0];
2589 } else {
2590 spec->multiout.slave_dig_outs = spec->slave_dig_outs;
2591 if (nums >= ARRAY_SIZE(spec->slave_dig_outs) - 1)
2592 break;
2593 spec->slave_dig_outs[nums - 1] = dig_nid;
2594 }
2595 nums++;
2596 }
2597
2598 if (spec->autocfg.dig_in_pin) {
2599 dig_nid = codec->start_nid;
2600 for (i = 0; i < codec->num_nodes; i++, dig_nid++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002601 unsigned int wcaps = get_wcaps(codec, dig_nid);
2602 if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
2603 continue;
2604 if (!(wcaps & AC_WCAP_DIGITAL))
2605 continue;
2606 path = snd_hda_add_new_path(codec,
2607 spec->autocfg.dig_in_pin,
Takashi Iwai3ca529d2013-01-07 17:25:08 +01002608 dig_nid, 0);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002609 if (path) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01002610 print_nid_path("digin", path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002611 path->active = true;
2612 spec->dig_in_nid = dig_nid;
Takashi Iwai2430d7b2013-01-04 15:09:42 +01002613 spec->digin_path = snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002614 break;
2615 }
2616 }
2617 }
2618}
2619
2620
2621/*
2622 * input MUX handling
2623 */
2624
2625static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur);
2626
2627/* select the given imux item; either unmute exclusively or select the route */
2628static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
2629 unsigned int idx)
2630{
2631 struct hda_gen_spec *spec = codec->spec;
2632 const struct hda_input_mux *imux;
2633 struct nid_path *path;
2634
2635 imux = &spec->input_mux;
2636 if (!imux->num_items)
2637 return 0;
2638
2639 if (idx >= imux->num_items)
2640 idx = imux->num_items - 1;
2641 if (spec->cur_mux[adc_idx] == idx)
2642 return 0;
2643
Takashi Iwaic697b712013-01-07 17:09:26 +01002644 path = get_input_path(codec, adc_idx, spec->cur_mux[adc_idx]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002645 if (!path)
2646 return 0;
2647 if (path->active)
2648 snd_hda_activate_path(codec, path, false, false);
2649
2650 spec->cur_mux[adc_idx] = idx;
2651
2652 if (spec->shared_mic_hp)
2653 update_shared_mic_hp(codec, spec->cur_mux[adc_idx]);
2654
2655 if (spec->dyn_adc_switch)
2656 dyn_adc_pcm_resetup(codec, idx);
2657
Takashi Iwaic697b712013-01-07 17:09:26 +01002658 path = get_input_path(codec, adc_idx, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002659 if (!path)
2660 return 0;
2661 if (path->active)
2662 return 0;
2663 snd_hda_activate_path(codec, path, true, false);
2664 if (spec->cap_sync_hook)
2665 spec->cap_sync_hook(codec);
2666 return 1;
2667}
2668
2669
2670/*
2671 * Jack detections for HP auto-mute and mic-switch
2672 */
2673
2674/* check each pin in the given array; returns true if any of them is plugged */
2675static bool detect_jacks(struct hda_codec *codec, int num_pins, hda_nid_t *pins)
2676{
2677 int i, present = 0;
2678
2679 for (i = 0; i < num_pins; i++) {
2680 hda_nid_t nid = pins[i];
2681 if (!nid)
2682 break;
2683 present |= snd_hda_jack_detect(codec, nid);
2684 }
2685 return present;
2686}
2687
2688/* standard HP/line-out auto-mute helper */
2689static void do_automute(struct hda_codec *codec, int num_pins, hda_nid_t *pins,
2690 bool mute, bool hp_out)
2691{
2692 struct hda_gen_spec *spec = codec->spec;
2693 unsigned int pin_bits = mute ? 0 : (hp_out ? PIN_HP : PIN_OUT);
2694 int i;
2695
2696 for (i = 0; i < num_pins; i++) {
2697 hda_nid_t nid = pins[i];
2698 unsigned int val;
2699 if (!nid)
2700 break;
2701 /* don't reset VREF value in case it's controlling
2702 * the amp (see alc861_fixup_asus_amp_vref_0f())
2703 */
2704 if (spec->keep_vref_in_automute) {
2705 val = snd_hda_codec_read(codec, nid, 0,
2706 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
2707 val &= ~PIN_HP;
2708 } else
2709 val = 0;
2710 val |= pin_bits;
Takashi Iwai7594aa32012-12-20 15:38:40 +01002711 snd_hda_set_pin_ctl_cache(codec, nid, val);
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01002712 set_pin_eapd(codec, nid, !mute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002713 }
2714}
2715
2716/* Toggle outputs muting */
Takashi Iwai5d550e12012-12-19 15:16:44 +01002717void snd_hda_gen_update_outputs(struct hda_codec *codec)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002718{
2719 struct hda_gen_spec *spec = codec->spec;
2720 int on;
2721
2722 /* Control HP pins/amps depending on master_mute state;
2723 * in general, HP pins/amps control should be enabled in all cases,
2724 * but currently set only for master_mute, just to be safe
2725 */
2726 if (!spec->shared_mic_hp) /* don't change HP-pin when shared with mic */
2727 do_automute(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
2728 spec->autocfg.hp_pins, spec->master_mute, true);
2729
2730 if (!spec->automute_speaker)
2731 on = 0;
2732 else
2733 on = spec->hp_jack_present | spec->line_jack_present;
2734 on |= spec->master_mute;
2735 do_automute(codec, ARRAY_SIZE(spec->autocfg.speaker_pins),
2736 spec->autocfg.speaker_pins, on, false);
2737
2738 /* toggle line-out mutes if needed, too */
2739 /* if LO is a copy of either HP or Speaker, don't need to handle it */
2740 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0] ||
2741 spec->autocfg.line_out_pins[0] == spec->autocfg.speaker_pins[0])
2742 return;
2743 if (!spec->automute_lo)
2744 on = 0;
2745 else
2746 on = spec->hp_jack_present;
2747 on |= spec->master_mute;
2748 do_automute(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
2749 spec->autocfg.line_out_pins, on, false);
2750}
Takashi Iwai5d550e12012-12-19 15:16:44 +01002751EXPORT_SYMBOL_HDA(snd_hda_gen_update_outputs);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002752
2753static void call_update_outputs(struct hda_codec *codec)
2754{
2755 struct hda_gen_spec *spec = codec->spec;
2756 if (spec->automute_hook)
2757 spec->automute_hook(codec);
2758 else
Takashi Iwai5d550e12012-12-19 15:16:44 +01002759 snd_hda_gen_update_outputs(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002760}
2761
2762/* standard HP-automute helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01002763void snd_hda_gen_hp_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002764{
2765 struct hda_gen_spec *spec = codec->spec;
2766
2767 spec->hp_jack_present =
2768 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
2769 spec->autocfg.hp_pins);
2770 if (!spec->detect_hp || (!spec->automute_speaker && !spec->automute_lo))
2771 return;
2772 call_update_outputs(codec);
2773}
Takashi Iwai5d550e12012-12-19 15:16:44 +01002774EXPORT_SYMBOL_HDA(snd_hda_gen_hp_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002775
2776/* standard line-out-automute helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01002777void snd_hda_gen_line_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002778{
2779 struct hda_gen_spec *spec = codec->spec;
2780
2781 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
2782 return;
2783 /* check LO jack only when it's different from HP */
2784 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0])
2785 return;
2786
2787 spec->line_jack_present =
2788 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
2789 spec->autocfg.line_out_pins);
2790 if (!spec->automute_speaker || !spec->detect_lo)
2791 return;
2792 call_update_outputs(codec);
2793}
Takashi Iwai5d550e12012-12-19 15:16:44 +01002794EXPORT_SYMBOL_HDA(snd_hda_gen_line_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002795
2796/* standard mic auto-switch helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01002797void snd_hda_gen_mic_autoswitch(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002798{
2799 struct hda_gen_spec *spec = codec->spec;
2800 int i;
2801
2802 if (!spec->auto_mic)
2803 return;
2804
2805 for (i = spec->am_num_entries - 1; i > 0; i--) {
2806 if (snd_hda_jack_detect(codec, spec->am_entry[i].pin)) {
2807 mux_select(codec, 0, spec->am_entry[i].idx);
2808 return;
2809 }
2810 }
2811 mux_select(codec, 0, spec->am_entry[0].idx);
2812}
Takashi Iwai5d550e12012-12-19 15:16:44 +01002813EXPORT_SYMBOL_HDA(snd_hda_gen_mic_autoswitch);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002814
2815/*
2816 * Auto-Mute mode mixer enum support
2817 */
2818static int automute_mode_info(struct snd_kcontrol *kcontrol,
2819 struct snd_ctl_elem_info *uinfo)
2820{
2821 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2822 struct hda_gen_spec *spec = codec->spec;
2823 static const char * const texts3[] = {
2824 "Disabled", "Speaker Only", "Line Out+Speaker"
Takashi Iwai071c73a2006-08-23 18:34:06 +02002825 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826
Takashi Iwai352f7f92012-12-19 12:52:06 +01002827 if (spec->automute_speaker_possible && spec->automute_lo_possible)
2828 return snd_hda_enum_helper_info(kcontrol, uinfo, 3, texts3);
2829 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
2830}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831
Takashi Iwai352f7f92012-12-19 12:52:06 +01002832static int automute_mode_get(struct snd_kcontrol *kcontrol,
2833 struct snd_ctl_elem_value *ucontrol)
2834{
2835 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2836 struct hda_gen_spec *spec = codec->spec;
2837 unsigned int val = 0;
2838 if (spec->automute_speaker)
2839 val++;
2840 if (spec->automute_lo)
2841 val++;
Takashi Iwai071c73a2006-08-23 18:34:06 +02002842
Takashi Iwai352f7f92012-12-19 12:52:06 +01002843 ucontrol->value.enumerated.item[0] = val;
2844 return 0;
2845}
2846
2847static int automute_mode_put(struct snd_kcontrol *kcontrol,
2848 struct snd_ctl_elem_value *ucontrol)
2849{
2850 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2851 struct hda_gen_spec *spec = codec->spec;
2852
2853 switch (ucontrol->value.enumerated.item[0]) {
2854 case 0:
2855 if (!spec->automute_speaker && !spec->automute_lo)
2856 return 0;
2857 spec->automute_speaker = 0;
2858 spec->automute_lo = 0;
2859 break;
2860 case 1:
2861 if (spec->automute_speaker_possible) {
2862 if (!spec->automute_lo && spec->automute_speaker)
2863 return 0;
2864 spec->automute_speaker = 1;
2865 spec->automute_lo = 0;
2866 } else if (spec->automute_lo_possible) {
2867 if (spec->automute_lo)
2868 return 0;
2869 spec->automute_lo = 1;
2870 } else
2871 return -EINVAL;
2872 break;
2873 case 2:
2874 if (!spec->automute_lo_possible || !spec->automute_speaker_possible)
2875 return -EINVAL;
2876 if (spec->automute_speaker && spec->automute_lo)
2877 return 0;
2878 spec->automute_speaker = 1;
2879 spec->automute_lo = 1;
2880 break;
2881 default:
2882 return -EINVAL;
2883 }
2884 call_update_outputs(codec);
2885 return 1;
2886}
2887
2888static const struct snd_kcontrol_new automute_mode_enum = {
2889 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2890 .name = "Auto-Mute Mode",
2891 .info = automute_mode_info,
2892 .get = automute_mode_get,
2893 .put = automute_mode_put,
2894};
2895
2896static int add_automute_mode_enum(struct hda_codec *codec)
2897{
2898 struct hda_gen_spec *spec = codec->spec;
2899
Takashi Iwai12c93df2012-12-19 14:38:33 +01002900 if (!snd_hda_gen_add_kctl(spec, NULL, &automute_mode_enum))
Takashi Iwai352f7f92012-12-19 12:52:06 +01002901 return -ENOMEM;
2902 return 0;
2903}
2904
2905/*
2906 * Check the availability of HP/line-out auto-mute;
2907 * Set up appropriately if really supported
2908 */
2909static int check_auto_mute_availability(struct hda_codec *codec)
2910{
2911 struct hda_gen_spec *spec = codec->spec;
2912 struct auto_pin_cfg *cfg = &spec->autocfg;
2913 int present = 0;
2914 int i, err;
2915
2916 if (cfg->hp_pins[0])
2917 present++;
2918 if (cfg->line_out_pins[0])
2919 present++;
2920 if (cfg->speaker_pins[0])
2921 present++;
2922 if (present < 2) /* need two different output types */
Takashi Iwai071c73a2006-08-23 18:34:06 +02002923 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002924
2925 if (!cfg->speaker_pins[0] &&
2926 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
2927 memcpy(cfg->speaker_pins, cfg->line_out_pins,
2928 sizeof(cfg->speaker_pins));
2929 cfg->speaker_outs = cfg->line_outs;
Takashi Iwai071c73a2006-08-23 18:34:06 +02002930 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931
Takashi Iwai352f7f92012-12-19 12:52:06 +01002932 if (!cfg->hp_pins[0] &&
2933 cfg->line_out_type == AUTO_PIN_HP_OUT) {
2934 memcpy(cfg->hp_pins, cfg->line_out_pins,
2935 sizeof(cfg->hp_pins));
2936 cfg->hp_outs = cfg->line_outs;
2937 }
2938
2939 for (i = 0; i < cfg->hp_outs; i++) {
2940 hda_nid_t nid = cfg->hp_pins[i];
2941 if (!is_jack_detectable(codec, nid))
2942 continue;
2943 snd_printdd("hda-codec: Enable HP auto-muting on NID 0x%x\n",
2944 nid);
2945 snd_hda_jack_detect_enable_callback(codec, nid, HDA_GEN_HP_EVENT,
Takashi Iwai2e03e952013-01-03 15:55:06 +01002946 spec->hp_automute_hook ?
2947 spec->hp_automute_hook :
Takashi Iwai5d550e12012-12-19 15:16:44 +01002948 snd_hda_gen_hp_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002949 spec->detect_hp = 1;
2950 }
2951
2952 if (cfg->line_out_type == AUTO_PIN_LINE_OUT && cfg->line_outs) {
2953 if (cfg->speaker_outs)
2954 for (i = 0; i < cfg->line_outs; i++) {
2955 hda_nid_t nid = cfg->line_out_pins[i];
2956 if (!is_jack_detectable(codec, nid))
2957 continue;
2958 snd_printdd("hda-codec: Enable Line-Out auto-muting on NID 0x%x\n", nid);
2959 snd_hda_jack_detect_enable_callback(codec, nid,
2960 HDA_GEN_FRONT_EVENT,
Takashi Iwai2e03e952013-01-03 15:55:06 +01002961 spec->line_automute_hook ?
2962 spec->line_automute_hook :
Takashi Iwai5d550e12012-12-19 15:16:44 +01002963 snd_hda_gen_line_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002964 spec->detect_lo = 1;
2965 }
2966 spec->automute_lo_possible = spec->detect_hp;
2967 }
2968
2969 spec->automute_speaker_possible = cfg->speaker_outs &&
2970 (spec->detect_hp || spec->detect_lo);
2971
2972 spec->automute_lo = spec->automute_lo_possible;
2973 spec->automute_speaker = spec->automute_speaker_possible;
2974
2975 if (spec->automute_speaker_possible || spec->automute_lo_possible) {
2976 /* create a control for automute mode */
2977 err = add_automute_mode_enum(codec);
2978 if (err < 0)
2979 return err;
2980 }
2981 return 0;
2982}
2983
Takashi Iwai352f7f92012-12-19 12:52:06 +01002984/* check whether all auto-mic pins are valid; setup indices if OK */
2985static bool auto_mic_check_imux(struct hda_codec *codec)
2986{
2987 struct hda_gen_spec *spec = codec->spec;
2988 const struct hda_input_mux *imux;
2989 int i;
2990
2991 imux = &spec->input_mux;
2992 for (i = 0; i < spec->am_num_entries; i++) {
2993 spec->am_entry[i].idx =
2994 find_idx_in_nid_list(spec->am_entry[i].pin,
2995 spec->imux_pins, imux->num_items);
2996 if (spec->am_entry[i].idx < 0)
2997 return false; /* no corresponding imux */
2998 }
2999
3000 /* we don't need the jack detection for the first pin */
3001 for (i = 1; i < spec->am_num_entries; i++)
3002 snd_hda_jack_detect_enable_callback(codec,
3003 spec->am_entry[i].pin,
3004 HDA_GEN_MIC_EVENT,
Takashi Iwai2e03e952013-01-03 15:55:06 +01003005 spec->mic_autoswitch_hook ?
3006 spec->mic_autoswitch_hook :
Takashi Iwai5d550e12012-12-19 15:16:44 +01003007 snd_hda_gen_mic_autoswitch);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003008 return true;
3009}
3010
3011static int compare_attr(const void *ap, const void *bp)
3012{
3013 const struct automic_entry *a = ap;
3014 const struct automic_entry *b = bp;
3015 return (int)(a->attr - b->attr);
3016}
3017
3018/*
3019 * Check the availability of auto-mic switch;
3020 * Set up if really supported
3021 */
3022static int check_auto_mic_availability(struct hda_codec *codec)
3023{
3024 struct hda_gen_spec *spec = codec->spec;
3025 struct auto_pin_cfg *cfg = &spec->autocfg;
3026 unsigned int types;
3027 int i, num_pins;
3028
Takashi Iwaid12daf62013-01-07 16:32:11 +01003029 if (spec->suppress_auto_mic)
3030 return 0;
3031
Takashi Iwai352f7f92012-12-19 12:52:06 +01003032 types = 0;
3033 num_pins = 0;
3034 for (i = 0; i < cfg->num_inputs; i++) {
3035 hda_nid_t nid = cfg->inputs[i].pin;
3036 unsigned int attr;
3037 attr = snd_hda_codec_get_pincfg(codec, nid);
3038 attr = snd_hda_get_input_pin_attr(attr);
3039 if (types & (1 << attr))
3040 return 0; /* already occupied */
3041 switch (attr) {
3042 case INPUT_PIN_ATTR_INT:
3043 if (cfg->inputs[i].type != AUTO_PIN_MIC)
3044 return 0; /* invalid type */
3045 break;
3046 case INPUT_PIN_ATTR_UNUSED:
3047 return 0; /* invalid entry */
3048 default:
3049 if (cfg->inputs[i].type > AUTO_PIN_LINE_IN)
3050 return 0; /* invalid type */
3051 if (!spec->line_in_auto_switch &&
3052 cfg->inputs[i].type != AUTO_PIN_MIC)
3053 return 0; /* only mic is allowed */
3054 if (!is_jack_detectable(codec, nid))
3055 return 0; /* no unsol support */
3056 break;
3057 }
3058 if (num_pins >= MAX_AUTO_MIC_PINS)
3059 return 0;
3060 types |= (1 << attr);
3061 spec->am_entry[num_pins].pin = nid;
3062 spec->am_entry[num_pins].attr = attr;
3063 num_pins++;
3064 }
3065
3066 if (num_pins < 2)
3067 return 0;
3068
3069 spec->am_num_entries = num_pins;
3070 /* sort the am_entry in the order of attr so that the pin with a
3071 * higher attr will be selected when the jack is plugged.
3072 */
3073 sort(spec->am_entry, num_pins, sizeof(spec->am_entry[0]),
3074 compare_attr, NULL);
3075
3076 if (!auto_mic_check_imux(codec))
3077 return 0;
3078
3079 spec->auto_mic = 1;
3080 spec->num_adc_nids = 1;
3081 spec->cur_mux[0] = spec->am_entry[0].idx;
3082 snd_printdd("hda-codec: Enable auto-mic switch on NID 0x%x/0x%x/0x%x\n",
3083 spec->am_entry[0].pin,
3084 spec->am_entry[1].pin,
3085 spec->am_entry[2].pin);
3086
3087 return 0;
3088}
3089
3090
Takashi Iwai9eb413e2012-12-19 14:41:21 +01003091/*
3092 * Parse the given BIOS configuration and set up the hda_gen_spec
3093 *
3094 * return 1 if successful, 0 if the proper config is not found,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003095 * or a negative error code
3096 */
3097int snd_hda_gen_parse_auto_config(struct hda_codec *codec,
Takashi Iwai9eb413e2012-12-19 14:41:21 +01003098 struct auto_pin_cfg *cfg)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003099{
3100 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003101 int err;
3102
Takashi Iwai9eb413e2012-12-19 14:41:21 +01003103 if (cfg != &spec->autocfg) {
3104 spec->autocfg = *cfg;
3105 cfg = &spec->autocfg;
3106 }
3107
Takashi Iwai352f7f92012-12-19 12:52:06 +01003108 if (!cfg->line_outs) {
3109 if (cfg->dig_outs || cfg->dig_in_pin) {
3110 spec->multiout.max_channels = 2;
3111 spec->no_analog = 1;
3112 goto dig_only;
3113 }
3114 return 0; /* can't find valid BIOS pin config */
3115 }
3116
3117 if (!spec->no_primary_hp &&
3118 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT &&
3119 cfg->line_outs <= cfg->hp_outs) {
3120 /* use HP as primary out */
3121 cfg->speaker_outs = cfg->line_outs;
3122 memcpy(cfg->speaker_pins, cfg->line_out_pins,
3123 sizeof(cfg->speaker_pins));
3124 cfg->line_outs = cfg->hp_outs;
3125 memcpy(cfg->line_out_pins, cfg->hp_pins, sizeof(cfg->hp_pins));
3126 cfg->hp_outs = 0;
3127 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
3128 cfg->line_out_type = AUTO_PIN_HP_OUT;
3129 }
3130
3131 err = parse_output_paths(codec);
3132 if (err < 0)
3133 return err;
3134 err = create_multi_channel_mode(codec);
3135 if (err < 0)
3136 return err;
3137 err = create_multi_out_ctls(codec, cfg);
3138 if (err < 0)
3139 return err;
3140 err = create_hp_out_ctls(codec);
3141 if (err < 0)
3142 return err;
3143 err = create_speaker_out_ctls(codec);
3144 if (err < 0)
3145 return err;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003146 err = create_indep_hp_ctls(codec);
3147 if (err < 0)
3148 return err;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01003149 err = create_loopback_mixing_ctl(codec);
3150 if (err < 0)
3151 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003152 err = create_shared_input(codec);
3153 if (err < 0)
3154 return err;
3155 err = create_input_ctls(codec);
Takashi Iwaid13bd412008-07-30 15:01:45 +02003156 if (err < 0)
Takashi Iwai071c73a2006-08-23 18:34:06 +02003157 return err;
3158
Takashi Iwaia07a9492013-01-07 16:44:06 +01003159 spec->const_channel_count = spec->ext_channel_count;
3160 /* check the multiple speaker and headphone pins */
3161 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
3162 spec->const_channel_count = max(spec->const_channel_count,
3163 cfg->speaker_outs * 2);
3164 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
3165 spec->const_channel_count = max(spec->const_channel_count,
3166 cfg->hp_outs * 2);
3167 spec->multiout.max_channels = max(spec->ext_channel_count,
3168 spec->const_channel_count);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003169
3170 err = check_auto_mute_availability(codec);
3171 if (err < 0)
3172 return err;
3173
3174 err = check_dyn_adc_switch(codec);
3175 if (err < 0)
3176 return err;
3177
3178 if (!spec->shared_mic_hp) {
3179 err = check_auto_mic_availability(codec);
Takashi Iwaid13bd412008-07-30 15:01:45 +02003180 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182 }
Takashi Iwai071c73a2006-08-23 18:34:06 +02003183
Takashi Iwai352f7f92012-12-19 12:52:06 +01003184 err = create_capture_mixers(codec);
3185 if (err < 0)
3186 return err;
3187
3188 err = parse_mic_boost(codec);
3189 if (err < 0)
3190 return err;
3191
3192 dig_only:
3193 parse_digital(codec);
3194
3195 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196}
Takashi Iwai352f7f92012-12-19 12:52:06 +01003197EXPORT_SYMBOL_HDA(snd_hda_gen_parse_auto_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198
3199
3200/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01003201 * Build control elements
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01003203
3204/* slave controls for virtual master */
3205static const char * const slave_pfxs[] = {
3206 "Front", "Surround", "Center", "LFE", "Side",
3207 "Headphone", "Speaker", "Mono", "Line Out",
3208 "CLFE", "Bass Speaker", "PCM",
Takashi Iwaiee79c692013-01-07 09:57:42 +01003209 "Speaker Front", "Speaker Surround", "Speaker CLFE", "Speaker Side",
3210 "Headphone Front", "Headphone Surround", "Headphone CLFE",
3211 "Headphone Side",
Takashi Iwai352f7f92012-12-19 12:52:06 +01003212 NULL,
3213};
3214
3215int snd_hda_gen_build_controls(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003216{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003217 struct hda_gen_spec *spec = codec->spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219
Takashi Iwai36502d02012-12-19 15:15:10 +01003220 if (spec->kctls.used) {
3221 err = snd_hda_add_new_ctls(codec, spec->kctls.list);
3222 if (err < 0)
3223 return err;
3224 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225
Takashi Iwai352f7f92012-12-19 12:52:06 +01003226 if (spec->multiout.dig_out_nid) {
3227 err = snd_hda_create_dig_out_ctls(codec,
3228 spec->multiout.dig_out_nid,
3229 spec->multiout.dig_out_nid,
3230 spec->pcm_rec[1].pcm_type);
3231 if (err < 0)
3232 return err;
3233 if (!spec->no_analog) {
3234 err = snd_hda_create_spdif_share_sw(codec,
3235 &spec->multiout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236 if (err < 0)
3237 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003238 spec->multiout.share_spdif = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003239 }
3240 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003241 if (spec->dig_in_nid) {
3242 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
3243 if (err < 0)
3244 return err;
3245 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246
Takashi Iwai352f7f92012-12-19 12:52:06 +01003247 /* if we have no master control, let's create it */
3248 if (!spec->no_analog &&
3249 !snd_hda_find_mixer_ctl(codec, "Master Playback Volume")) {
3250 unsigned int vmaster_tlv[4];
3251 snd_hda_set_vmaster_tlv(codec, spec->vmaster_nid,
3252 HDA_OUTPUT, vmaster_tlv);
3253 err = snd_hda_add_vmaster(codec, "Master Playback Volume",
3254 vmaster_tlv, slave_pfxs,
3255 "Playback Volume");
3256 if (err < 0)
3257 return err;
3258 }
3259 if (!spec->no_analog &&
3260 !snd_hda_find_mixer_ctl(codec, "Master Playback Switch")) {
3261 err = __snd_hda_add_vmaster(codec, "Master Playback Switch",
3262 NULL, slave_pfxs,
3263 "Playback Switch",
3264 true, &spec->vmaster_mute.sw_kctl);
3265 if (err < 0)
3266 return err;
3267 if (spec->vmaster_mute.hook)
Takashi Iwaifd25a972012-12-20 14:57:18 +01003268 snd_hda_add_vmaster_hook(codec, &spec->vmaster_mute,
3269 spec->vmaster_mute_enum);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003270 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271
Takashi Iwai352f7f92012-12-19 12:52:06 +01003272 free_kctls(spec); /* no longer needed */
3273
3274 if (spec->shared_mic_hp) {
3275 int err;
3276 int nid = spec->autocfg.inputs[1].pin;
3277 err = snd_hda_jack_add_kctl(codec, nid, "Headphone Mic", 0);
3278 if (err < 0)
3279 return err;
3280 err = snd_hda_jack_detect_enable(codec, nid, 0);
3281 if (err < 0)
3282 return err;
3283 }
3284
3285 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
3286 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287 return err;
3288
3289 return 0;
3290}
Takashi Iwai352f7f92012-12-19 12:52:06 +01003291EXPORT_SYMBOL_HDA(snd_hda_gen_build_controls);
3292
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293
3294/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01003295 * PCM definitions
Linus Torvalds1da177e2005-04-16 15:20:36 -07003296 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003297
Takashi Iwaie6b85f32013-01-07 11:54:34 +01003298static void call_pcm_playback_hook(struct hda_pcm_stream *hinfo,
3299 struct hda_codec *codec,
3300 struct snd_pcm_substream *substream,
3301 int action)
3302{
3303 struct hda_gen_spec *spec = codec->spec;
3304 if (spec->pcm_playback_hook)
3305 spec->pcm_playback_hook(hinfo, codec, substream, action);
3306}
3307
Takashi Iwai352f7f92012-12-19 12:52:06 +01003308/*
3309 * Analog playback callbacks
3310 */
3311static int playback_pcm_open(struct hda_pcm_stream *hinfo,
3312 struct hda_codec *codec,
3313 struct snd_pcm_substream *substream)
3314{
3315 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003316 int err;
3317
3318 mutex_lock(&spec->pcm_mutex);
3319 err = snd_hda_multi_out_analog_open(codec,
3320 &spec->multiout, substream,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003321 hinfo);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01003322 if (!err) {
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003323 spec->active_streams |= 1 << STREAM_MULTI_OUT;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01003324 call_pcm_playback_hook(hinfo, codec, substream,
3325 HDA_GEN_PCM_ACT_OPEN);
3326 }
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003327 mutex_unlock(&spec->pcm_mutex);
3328 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003329}
3330
3331static int playback_pcm_prepare(struct hda_pcm_stream *hinfo,
Takashi Iwai97ec5582006-03-21 11:29:07 +01003332 struct hda_codec *codec,
3333 unsigned int stream_tag,
3334 unsigned int format,
3335 struct snd_pcm_substream *substream)
3336{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003337 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01003338 int err;
3339
3340 err = snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
3341 stream_tag, format, substream);
3342 if (!err)
3343 call_pcm_playback_hook(hinfo, codec, substream,
3344 HDA_GEN_PCM_ACT_PREPARE);
3345 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003346}
Takashi Iwai97ec5582006-03-21 11:29:07 +01003347
Takashi Iwai352f7f92012-12-19 12:52:06 +01003348static int playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
3349 struct hda_codec *codec,
3350 struct snd_pcm_substream *substream)
3351{
3352 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01003353 int err;
3354
3355 err = snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
3356 if (!err)
3357 call_pcm_playback_hook(hinfo, codec, substream,
3358 HDA_GEN_PCM_ACT_CLEANUP);
3359 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003360}
3361
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003362static int playback_pcm_close(struct hda_pcm_stream *hinfo,
3363 struct hda_codec *codec,
3364 struct snd_pcm_substream *substream)
3365{
3366 struct hda_gen_spec *spec = codec->spec;
3367 mutex_lock(&spec->pcm_mutex);
3368 spec->active_streams &= ~(1 << STREAM_MULTI_OUT);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01003369 call_pcm_playback_hook(hinfo, codec, substream,
3370 HDA_GEN_PCM_ACT_CLOSE);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003371 mutex_unlock(&spec->pcm_mutex);
3372 return 0;
3373}
3374
3375static int alt_playback_pcm_open(struct hda_pcm_stream *hinfo,
3376 struct hda_codec *codec,
3377 struct snd_pcm_substream *substream)
3378{
3379 struct hda_gen_spec *spec = codec->spec;
3380 int err = 0;
3381
3382 mutex_lock(&spec->pcm_mutex);
3383 if (!spec->indep_hp_enabled)
3384 err = -EBUSY;
3385 else
3386 spec->active_streams |= 1 << STREAM_INDEP_HP;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01003387 call_pcm_playback_hook(hinfo, codec, substream,
3388 HDA_GEN_PCM_ACT_OPEN);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003389 mutex_unlock(&spec->pcm_mutex);
3390 return err;
3391}
3392
3393static int alt_playback_pcm_close(struct hda_pcm_stream *hinfo,
3394 struct hda_codec *codec,
3395 struct snd_pcm_substream *substream)
3396{
3397 struct hda_gen_spec *spec = codec->spec;
3398 mutex_lock(&spec->pcm_mutex);
3399 spec->active_streams &= ~(1 << STREAM_INDEP_HP);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01003400 call_pcm_playback_hook(hinfo, codec, substream,
3401 HDA_GEN_PCM_ACT_CLOSE);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003402 mutex_unlock(&spec->pcm_mutex);
3403 return 0;
3404}
3405
Takashi Iwaie6b85f32013-01-07 11:54:34 +01003406static int alt_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
3407 struct hda_codec *codec,
3408 unsigned int stream_tag,
3409 unsigned int format,
3410 struct snd_pcm_substream *substream)
3411{
3412 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
3413 call_pcm_playback_hook(hinfo, codec, substream,
3414 HDA_GEN_PCM_ACT_PREPARE);
3415 return 0;
3416}
3417
3418static int alt_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
3419 struct hda_codec *codec,
3420 struct snd_pcm_substream *substream)
3421{
3422 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
3423 call_pcm_playback_hook(hinfo, codec, substream,
3424 HDA_GEN_PCM_ACT_CLEANUP);
3425 return 0;
3426}
3427
Takashi Iwai352f7f92012-12-19 12:52:06 +01003428/*
3429 * Digital out
3430 */
3431static int dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
3432 struct hda_codec *codec,
3433 struct snd_pcm_substream *substream)
3434{
3435 struct hda_gen_spec *spec = codec->spec;
3436 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
3437}
3438
3439static int dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
3440 struct hda_codec *codec,
3441 unsigned int stream_tag,
3442 unsigned int format,
3443 struct snd_pcm_substream *substream)
3444{
3445 struct hda_gen_spec *spec = codec->spec;
3446 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
3447 stream_tag, format, substream);
3448}
3449
3450static int dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
3451 struct hda_codec *codec,
3452 struct snd_pcm_substream *substream)
3453{
3454 struct hda_gen_spec *spec = codec->spec;
3455 return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
3456}
3457
3458static int dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
3459 struct hda_codec *codec,
3460 struct snd_pcm_substream *substream)
3461{
3462 struct hda_gen_spec *spec = codec->spec;
3463 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
3464}
3465
3466/*
3467 * Analog capture
3468 */
3469static int alt_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
3470 struct hda_codec *codec,
3471 unsigned int stream_tag,
3472 unsigned int format,
3473 struct snd_pcm_substream *substream)
3474{
3475 struct hda_gen_spec *spec = codec->spec;
3476
3477 snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number + 1],
Takashi Iwai97ec5582006-03-21 11:29:07 +01003478 stream_tag, 0, format);
3479 return 0;
3480}
3481
Takashi Iwai352f7f92012-12-19 12:52:06 +01003482static int alt_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
3483 struct hda_codec *codec,
3484 struct snd_pcm_substream *substream)
Takashi Iwai97ec5582006-03-21 11:29:07 +01003485{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003486 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai97ec5582006-03-21 11:29:07 +01003487
Takashi Iwai352f7f92012-12-19 12:52:06 +01003488 snd_hda_codec_cleanup_stream(codec,
3489 spec->adc_nids[substream->number + 1]);
Takashi Iwai97ec5582006-03-21 11:29:07 +01003490 return 0;
3491}
3492
Takashi Iwai352f7f92012-12-19 12:52:06 +01003493/*
3494 */
3495static const struct hda_pcm_stream pcm_analog_playback = {
3496 .substreams = 1,
3497 .channels_min = 2,
3498 .channels_max = 8,
3499 /* NID is set in build_pcms */
3500 .ops = {
3501 .open = playback_pcm_open,
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003502 .close = playback_pcm_close,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003503 .prepare = playback_pcm_prepare,
3504 .cleanup = playback_pcm_cleanup
3505 },
3506};
Linus Torvalds1da177e2005-04-16 15:20:36 -07003507
Takashi Iwai352f7f92012-12-19 12:52:06 +01003508static const struct hda_pcm_stream pcm_analog_capture = {
3509 .substreams = 1,
3510 .channels_min = 2,
3511 .channels_max = 2,
3512 /* NID is set in build_pcms */
3513};
3514
3515static const struct hda_pcm_stream pcm_analog_alt_playback = {
3516 .substreams = 1,
3517 .channels_min = 2,
3518 .channels_max = 2,
3519 /* NID is set in build_pcms */
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003520 .ops = {
3521 .open = alt_playback_pcm_open,
Takashi Iwaie6b85f32013-01-07 11:54:34 +01003522 .close = alt_playback_pcm_close,
3523 .prepare = alt_playback_pcm_prepare,
3524 .cleanup = alt_playback_pcm_cleanup
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003525 },
Takashi Iwai352f7f92012-12-19 12:52:06 +01003526};
3527
3528static const struct hda_pcm_stream pcm_analog_alt_capture = {
3529 .substreams = 2, /* can be overridden */
3530 .channels_min = 2,
3531 .channels_max = 2,
3532 /* NID is set in build_pcms */
3533 .ops = {
3534 .prepare = alt_capture_pcm_prepare,
3535 .cleanup = alt_capture_pcm_cleanup
3536 },
3537};
3538
3539static const struct hda_pcm_stream pcm_digital_playback = {
3540 .substreams = 1,
3541 .channels_min = 2,
3542 .channels_max = 2,
3543 /* NID is set in build_pcms */
3544 .ops = {
3545 .open = dig_playback_pcm_open,
3546 .close = dig_playback_pcm_close,
3547 .prepare = dig_playback_pcm_prepare,
3548 .cleanup = dig_playback_pcm_cleanup
3549 },
3550};
3551
3552static const struct hda_pcm_stream pcm_digital_capture = {
3553 .substreams = 1,
3554 .channels_min = 2,
3555 .channels_max = 2,
3556 /* NID is set in build_pcms */
3557};
3558
3559/* Used by build_pcms to flag that a PCM has no playback stream */
3560static const struct hda_pcm_stream pcm_null_stream = {
3561 .substreams = 0,
3562 .channels_min = 0,
3563 .channels_max = 0,
3564};
3565
3566/*
3567 * dynamic changing ADC PCM streams
3568 */
3569static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur)
3570{
3571 struct hda_gen_spec *spec = codec->spec;
3572 hda_nid_t new_adc = spec->adc_nids[spec->dyn_adc_idx[cur]];
3573
3574 if (spec->cur_adc && spec->cur_adc != new_adc) {
3575 /* stream is running, let's swap the current ADC */
3576 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
3577 spec->cur_adc = new_adc;
3578 snd_hda_codec_setup_stream(codec, new_adc,
3579 spec->cur_adc_stream_tag, 0,
3580 spec->cur_adc_format);
3581 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003582 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003583 return false;
3584}
3585
3586/* analog capture with dynamic dual-adc changes */
3587static int dyn_adc_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
3588 struct hda_codec *codec,
3589 unsigned int stream_tag,
3590 unsigned int format,
3591 struct snd_pcm_substream *substream)
3592{
3593 struct hda_gen_spec *spec = codec->spec;
3594 spec->cur_adc = spec->adc_nids[spec->dyn_adc_idx[spec->cur_mux[0]]];
3595 spec->cur_adc_stream_tag = stream_tag;
3596 spec->cur_adc_format = format;
3597 snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
3598 return 0;
3599}
3600
3601static int dyn_adc_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
3602 struct hda_codec *codec,
3603 struct snd_pcm_substream *substream)
3604{
3605 struct hda_gen_spec *spec = codec->spec;
3606 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
3607 spec->cur_adc = 0;
3608 return 0;
3609}
3610
3611static const struct hda_pcm_stream dyn_adc_pcm_analog_capture = {
3612 .substreams = 1,
3613 .channels_min = 2,
3614 .channels_max = 2,
3615 .nid = 0, /* fill later */
3616 .ops = {
3617 .prepare = dyn_adc_capture_pcm_prepare,
3618 .cleanup = dyn_adc_capture_pcm_cleanup
3619 },
3620};
3621
Takashi Iwaif873e532012-12-20 16:58:39 +01003622static void fill_pcm_stream_name(char *str, size_t len, const char *sfx,
3623 const char *chip_name)
3624{
3625 char *p;
3626
3627 if (*str)
3628 return;
3629 strlcpy(str, chip_name, len);
3630
3631 /* drop non-alnum chars after a space */
3632 for (p = strchr(str, ' '); p; p = strchr(p + 1, ' ')) {
3633 if (!isalnum(p[1])) {
3634 *p = 0;
3635 break;
3636 }
3637 }
3638 strlcat(str, sfx, len);
3639}
3640
Takashi Iwai352f7f92012-12-19 12:52:06 +01003641/* build PCM streams based on the parsed results */
3642int snd_hda_gen_build_pcms(struct hda_codec *codec)
3643{
3644 struct hda_gen_spec *spec = codec->spec;
3645 struct hda_pcm *info = spec->pcm_rec;
3646 const struct hda_pcm_stream *p;
3647 bool have_multi_adcs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003648
3649 codec->num_pcms = 1;
3650 codec->pcm_info = info;
3651
Takashi Iwai352f7f92012-12-19 12:52:06 +01003652 if (spec->no_analog)
3653 goto skip_analog;
3654
Takashi Iwaif873e532012-12-20 16:58:39 +01003655 fill_pcm_stream_name(spec->stream_name_analog,
3656 sizeof(spec->stream_name_analog),
3657 " Analog", codec->chip_name);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003658 info->name = spec->stream_name_analog;
3659
3660 if (spec->multiout.num_dacs > 0) {
3661 p = spec->stream_analog_playback;
3662 if (!p)
3663 p = &pcm_analog_playback;
3664 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
3665 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dac_nids[0];
3666 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
3667 spec->multiout.max_channels;
3668 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT &&
3669 spec->autocfg.line_outs == 2)
3670 info->stream[SNDRV_PCM_STREAM_PLAYBACK].chmap =
3671 snd_pcm_2_1_chmaps;
3672 }
3673 if (spec->num_adc_nids) {
3674 p = spec->stream_analog_capture;
3675 if (!p) {
3676 if (spec->dyn_adc_switch)
3677 p = &dyn_adc_pcm_analog_capture;
3678 else
3679 p = &pcm_analog_capture;
3680 }
3681 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
3682 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adc_nids[0];
3683 }
3684
Takashi Iwai352f7f92012-12-19 12:52:06 +01003685 skip_analog:
3686 /* SPDIF for stream index #1 */
3687 if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
Takashi Iwaif873e532012-12-20 16:58:39 +01003688 fill_pcm_stream_name(spec->stream_name_digital,
3689 sizeof(spec->stream_name_digital),
3690 " Digital", codec->chip_name);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003691 codec->num_pcms = 2;
3692 codec->slave_dig_outs = spec->multiout.slave_dig_outs;
3693 info = spec->pcm_rec + 1;
3694 info->name = spec->stream_name_digital;
3695 if (spec->dig_out_type)
3696 info->pcm_type = spec->dig_out_type;
3697 else
3698 info->pcm_type = HDA_PCM_TYPE_SPDIF;
3699 if (spec->multiout.dig_out_nid) {
3700 p = spec->stream_digital_playback;
3701 if (!p)
3702 p = &pcm_digital_playback;
3703 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
3704 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dig_out_nid;
3705 }
3706 if (spec->dig_in_nid) {
3707 p = spec->stream_digital_capture;
3708 if (!p)
3709 p = &pcm_digital_capture;
3710 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
3711 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in_nid;
3712 }
3713 }
3714
3715 if (spec->no_analog)
3716 return 0;
3717
3718 /* If the use of more than one ADC is requested for the current
3719 * model, configure a second analog capture-only PCM.
3720 */
3721 have_multi_adcs = (spec->num_adc_nids > 1) &&
3722 !spec->dyn_adc_switch && !spec->auto_mic;
3723 /* Additional Analaog capture for index #2 */
3724 if (spec->alt_dac_nid || have_multi_adcs) {
3725 codec->num_pcms = 3;
3726 info = spec->pcm_rec + 2;
3727 info->name = spec->stream_name_analog;
3728 if (spec->alt_dac_nid) {
3729 p = spec->stream_analog_alt_playback;
3730 if (!p)
3731 p = &pcm_analog_alt_playback;
3732 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
3733 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
3734 spec->alt_dac_nid;
3735 } else {
3736 info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
3737 pcm_null_stream;
3738 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = 0;
3739 }
3740 if (have_multi_adcs) {
3741 p = spec->stream_analog_alt_capture;
3742 if (!p)
3743 p = &pcm_analog_alt_capture;
3744 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
3745 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
3746 spec->adc_nids[1];
3747 info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams =
3748 spec->num_adc_nids - 1;
3749 } else {
3750 info->stream[SNDRV_PCM_STREAM_CAPTURE] =
3751 pcm_null_stream;
3752 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = 0;
3753 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003754 }
3755
3756 return 0;
3757}
Takashi Iwai352f7f92012-12-19 12:52:06 +01003758EXPORT_SYMBOL_HDA(snd_hda_gen_build_pcms);
3759
3760
3761/*
3762 * Standard auto-parser initializations
3763 */
3764
Takashi Iwaid4156932013-01-07 10:08:02 +01003765/* configure the given path as a proper output */
3766static void set_output_and_unmute(struct hda_codec *codec,
Takashi Iwai196c17662013-01-04 15:01:40 +01003767 int pin_type, int path_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003768{
3769 struct nid_path *path;
Takashi Iwaid4156932013-01-07 10:08:02 +01003770 hda_nid_t pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003771
Takashi Iwai196c17662013-01-04 15:01:40 +01003772 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwaid4156932013-01-07 10:08:02 +01003773 if (!path || !path->depth)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003774 return;
Takashi Iwaid4156932013-01-07 10:08:02 +01003775 pin = path->path[path->depth - 1];
3776 snd_hda_set_pin_ctl_cache(codec, pin, pin_type);
Takashi Iwaie1284af2013-01-03 16:33:02 +01003777 snd_hda_activate_path(codec, path, path->active, true);
3778 set_pin_eapd(codec, pin, path->active);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003779}
3780
3781/* initialize primary output paths */
3782static void init_multi_out(struct hda_codec *codec)
3783{
3784 struct hda_gen_spec *spec = codec->spec;
3785 int pin_type;
3786 int i;
3787
3788 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
3789 pin_type = PIN_HP;
3790 else
3791 pin_type = PIN_OUT;
3792
Takashi Iwaid4156932013-01-07 10:08:02 +01003793 for (i = 0; i < spec->autocfg.line_outs; i++)
3794 set_output_and_unmute(codec, pin_type, spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003795}
3796
Takashi Iwaidb23fd12012-12-20 15:27:24 +01003797
3798static void __init_extra_out(struct hda_codec *codec, int num_outs,
Takashi Iwaid4156932013-01-07 10:08:02 +01003799 int *paths, int type)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003800{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003801 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003802
Takashi Iwaid4156932013-01-07 10:08:02 +01003803 for (i = 0; i < num_outs; i++)
3804 set_output_and_unmute(codec, type, paths[i]);
Takashi Iwaidb23fd12012-12-20 15:27:24 +01003805}
3806
3807/* initialize hp and speaker paths */
3808static void init_extra_out(struct hda_codec *codec)
3809{
3810 struct hda_gen_spec *spec = codec->spec;
3811
3812 if (spec->autocfg.line_out_type != AUTO_PIN_HP_OUT)
3813 __init_extra_out(codec, spec->autocfg.hp_outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01003814 spec->hp_paths, PIN_HP);
Takashi Iwaidb23fd12012-12-20 15:27:24 +01003815 if (spec->autocfg.line_out_type != AUTO_PIN_SPEAKER_OUT)
3816 __init_extra_out(codec, spec->autocfg.speaker_outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01003817 spec->speaker_paths, PIN_OUT);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003818}
3819
3820/* initialize multi-io paths */
3821static void init_multi_io(struct hda_codec *codec)
3822{
3823 struct hda_gen_spec *spec = codec->spec;
3824 int i;
3825
3826 for (i = 0; i < spec->multi_ios; i++) {
3827 hda_nid_t pin = spec->multi_io[i].pin;
3828 struct nid_path *path;
Takashi Iwai196c17662013-01-04 15:01:40 +01003829 path = get_multiio_path(codec, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003830 if (!path)
3831 continue;
3832 if (!spec->multi_io[i].ctl_in)
3833 spec->multi_io[i].ctl_in =
3834 snd_hda_codec_update_cache(codec, pin, 0,
3835 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
3836 snd_hda_activate_path(codec, path, path->active, true);
3837 }
3838}
3839
3840/* set up the input pin config, depending on the given auto-pin type */
3841static void set_input_pin(struct hda_codec *codec, hda_nid_t nid,
3842 int auto_pin_type)
3843{
3844 unsigned int val = PIN_IN;
3845 if (auto_pin_type == AUTO_PIN_MIC)
3846 val |= snd_hda_get_default_vref(codec, nid);
Takashi Iwai7594aa32012-12-20 15:38:40 +01003847 snd_hda_set_pin_ctl_cache(codec, nid, val);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003848}
3849
3850/* set up input pins and loopback paths */
3851static void init_analog_input(struct hda_codec *codec)
3852{
3853 struct hda_gen_spec *spec = codec->spec;
3854 struct auto_pin_cfg *cfg = &spec->autocfg;
3855 int i;
3856
3857 for (i = 0; i < cfg->num_inputs; i++) {
3858 hda_nid_t nid = cfg->inputs[i].pin;
3859 if (is_input_pin(codec, nid))
3860 set_input_pin(codec, nid, cfg->inputs[i].type);
3861
3862 /* init loopback inputs */
3863 if (spec->mixer_nid) {
3864 struct nid_path *path;
Takashi Iwai196c17662013-01-04 15:01:40 +01003865 path = snd_hda_get_path_from_idx(codec, spec->loopback_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003866 if (path)
3867 snd_hda_activate_path(codec, path,
3868 path->active, false);
3869 }
3870 }
3871}
3872
3873/* initialize ADC paths */
3874static void init_input_src(struct hda_codec *codec)
3875{
3876 struct hda_gen_spec *spec = codec->spec;
3877 struct hda_input_mux *imux = &spec->input_mux;
3878 struct nid_path *path;
3879 int i, c, nums;
3880
3881 if (spec->dyn_adc_switch)
3882 nums = 1;
3883 else
3884 nums = spec->num_adc_nids;
3885
3886 for (c = 0; c < nums; c++) {
3887 for (i = 0; i < imux->num_items; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01003888 path = get_input_path(codec, c, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003889 if (path) {
3890 bool active = path->active;
3891 if (i == spec->cur_mux[c])
3892 active = true;
3893 snd_hda_activate_path(codec, path, active, false);
3894 }
3895 }
3896 }
3897
3898 if (spec->shared_mic_hp)
3899 update_shared_mic_hp(codec, spec->cur_mux[0]);
3900
3901 if (spec->cap_sync_hook)
3902 spec->cap_sync_hook(codec);
3903}
3904
3905/* set right pin controls for digital I/O */
3906static void init_digital(struct hda_codec *codec)
3907{
3908 struct hda_gen_spec *spec = codec->spec;
3909 int i;
3910 hda_nid_t pin;
3911
Takashi Iwaid4156932013-01-07 10:08:02 +01003912 for (i = 0; i < spec->autocfg.dig_outs; i++)
3913 set_output_and_unmute(codec, PIN_OUT, spec->digout_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003914 pin = spec->autocfg.dig_in_pin;
Takashi Iwai2430d7b2013-01-04 15:09:42 +01003915 if (pin) {
3916 struct nid_path *path;
Takashi Iwai7594aa32012-12-20 15:38:40 +01003917 snd_hda_set_pin_ctl_cache(codec, pin, PIN_IN);
Takashi Iwai2430d7b2013-01-04 15:09:42 +01003918 path = snd_hda_get_path_from_idx(codec, spec->digin_path);
3919 if (path)
3920 snd_hda_activate_path(codec, path, path->active, false);
3921 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003922}
3923
Takashi Iwai973e4972012-12-20 15:16:09 +01003924/* clear unsol-event tags on unused pins; Conexant codecs seem to leave
3925 * invalid unsol tags by some reason
3926 */
3927static void clear_unsol_on_unused_pins(struct hda_codec *codec)
3928{
3929 int i;
3930
3931 for (i = 0; i < codec->init_pins.used; i++) {
3932 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
3933 hda_nid_t nid = pin->nid;
3934 if (is_jack_detectable(codec, nid) &&
3935 !snd_hda_jack_tbl_get(codec, nid))
3936 snd_hda_codec_update_cache(codec, nid, 0,
3937 AC_VERB_SET_UNSOLICITED_ENABLE, 0);
3938 }
3939}
3940
Takashi Iwai5187ac12013-01-07 12:52:16 +01003941/*
3942 * initialize the generic spec;
3943 * this can be put as patch_ops.init function
3944 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01003945int snd_hda_gen_init(struct hda_codec *codec)
3946{
3947 struct hda_gen_spec *spec = codec->spec;
3948
3949 if (spec->init_hook)
3950 spec->init_hook(codec);
3951
3952 snd_hda_apply_verbs(codec);
3953
Takashi Iwai3bbcd272012-12-20 11:50:58 +01003954 codec->cached_write = 1;
3955
Takashi Iwai352f7f92012-12-19 12:52:06 +01003956 init_multi_out(codec);
3957 init_extra_out(codec);
3958 init_multi_io(codec);
3959 init_analog_input(codec);
3960 init_input_src(codec);
3961 init_digital(codec);
3962
Takashi Iwai973e4972012-12-20 15:16:09 +01003963 clear_unsol_on_unused_pins(codec);
3964
Takashi Iwai352f7f92012-12-19 12:52:06 +01003965 /* call init functions of standard auto-mute helpers */
Takashi Iwai5d550e12012-12-19 15:16:44 +01003966 snd_hda_gen_hp_automute(codec, NULL);
3967 snd_hda_gen_line_automute(codec, NULL);
3968 snd_hda_gen_mic_autoswitch(codec, NULL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003969
Takashi Iwai3bbcd272012-12-20 11:50:58 +01003970 snd_hda_codec_flush_amp_cache(codec);
3971 snd_hda_codec_flush_cmd_cache(codec);
3972
Takashi Iwai352f7f92012-12-19 12:52:06 +01003973 if (spec->vmaster_mute.sw_kctl && spec->vmaster_mute.hook)
3974 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
3975
3976 hda_call_check_power_status(codec, 0x01);
3977 return 0;
3978}
Takashi Iwaifce52a32013-01-07 12:42:48 +01003979EXPORT_SYMBOL_HDA(snd_hda_gen_init);
3980
Takashi Iwai5187ac12013-01-07 12:52:16 +01003981/*
3982 * free the generic spec;
3983 * this can be put as patch_ops.free function
3984 */
Takashi Iwaifce52a32013-01-07 12:42:48 +01003985void snd_hda_gen_free(struct hda_codec *codec)
3986{
3987 snd_hda_gen_spec_free(codec->spec);
3988 kfree(codec->spec);
3989 codec->spec = NULL;
3990}
3991EXPORT_SYMBOL_HDA(snd_hda_gen_free);
3992
3993#ifdef CONFIG_PM
Takashi Iwai5187ac12013-01-07 12:52:16 +01003994/*
3995 * check the loopback power save state;
3996 * this can be put as patch_ops.check_power_status function
3997 */
Takashi Iwaifce52a32013-01-07 12:42:48 +01003998int snd_hda_gen_check_power_status(struct hda_codec *codec, hda_nid_t nid)
3999{
4000 struct hda_gen_spec *spec = codec->spec;
4001 return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
4002}
4003EXPORT_SYMBOL_HDA(snd_hda_gen_check_power_status);
4004#endif
Takashi Iwai352f7f92012-12-19 12:52:06 +01004005
4006
4007/*
4008 * the generic codec support
4009 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004010
Takashi Iwai352f7f92012-12-19 12:52:06 +01004011static const struct hda_codec_ops generic_patch_ops = {
4012 .build_controls = snd_hda_gen_build_controls,
4013 .build_pcms = snd_hda_gen_build_pcms,
4014 .init = snd_hda_gen_init,
Takashi Iwaifce52a32013-01-07 12:42:48 +01004015 .free = snd_hda_gen_free,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004016 .unsol_event = snd_hda_jack_unsol_event,
Takashi Iwai83012a72012-08-24 18:38:08 +02004017#ifdef CONFIG_PM
Takashi Iwaifce52a32013-01-07 12:42:48 +01004018 .check_power_status = snd_hda_gen_check_power_status,
Takashi Iwaicb53c622007-08-10 17:21:45 +02004019#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004020};
4021
Linus Torvalds1da177e2005-04-16 15:20:36 -07004022int snd_hda_parse_generic_codec(struct hda_codec *codec)
4023{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004024 struct hda_gen_spec *spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004025 int err;
4026
Takashi Iwaie560d8d2005-09-09 14:21:46 +02004027 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004028 if (!spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004029 return -ENOMEM;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004030 snd_hda_gen_spec_init(spec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004031 codec->spec = spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004032
Takashi Iwai9eb413e2012-12-19 14:41:21 +01004033 err = snd_hda_parse_pin_defcfg(codec, &spec->autocfg, NULL, 0);
4034 if (err < 0)
4035 return err;
4036
4037 err = snd_hda_gen_parse_auto_config(codec, &spec->autocfg);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004038 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004039 goto error;
4040
4041 codec->patch_ops = generic_patch_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004042 return 0;
4043
Takashi Iwai352f7f92012-12-19 12:52:06 +01004044error:
Takashi Iwaifce52a32013-01-07 12:42:48 +01004045 snd_hda_gen_free(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004046 return err;
4047}
Takashi Iwaifce52a32013-01-07 12:42:48 +01004048EXPORT_SYMBOL_HDA(snd_hda_parse_generic_codec);