blob: a9bf188fe849c2daed830dd6b26914ee4d604cf1 [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
Takashi Iwai980428c2013-01-09 09:28:20 +0100334/* clear the given path as invalid so that it won't be picked up later */
335static void invalidate_nid_path(struct hda_codec *codec, int idx)
336{
337 struct nid_path *path = snd_hda_get_path_from_idx(codec, idx);
338 if (!path)
339 return;
340 memset(path, 0, sizeof(*path));
341}
342
Takashi Iwai352f7f92012-12-19 12:52:06 +0100343/* look for an empty DAC slot */
344static hda_nid_t look_for_dac(struct hda_codec *codec, hda_nid_t pin,
345 bool is_digital)
346{
347 struct hda_gen_spec *spec = codec->spec;
348 bool cap_digital;
349 int i;
350
351 for (i = 0; i < spec->num_all_dacs; i++) {
352 hda_nid_t nid = spec->all_dacs[i];
353 if (!nid || is_dac_already_used(codec, nid))
354 continue;
355 cap_digital = !!(get_wcaps(codec, nid) & AC_WCAP_DIGITAL);
356 if (is_digital != cap_digital)
357 continue;
358 if (is_reachable_path(codec, nid, pin))
359 return nid;
360 }
361 return 0;
362}
363
364/* replace the channels in the composed amp value with the given number */
365static unsigned int amp_val_replace_channels(unsigned int val, unsigned int chs)
366{
367 val &= ~(0x3U << 16);
368 val |= chs << 16;
369 return val;
370}
371
372/* check whether the widget has the given amp capability for the direction */
373static bool check_amp_caps(struct hda_codec *codec, hda_nid_t nid,
374 int dir, unsigned int bits)
375{
376 if (!nid)
377 return false;
378 if (get_wcaps(codec, nid) & (1 << (dir + 1)))
379 if (query_amp_caps(codec, nid, dir) & bits)
380 return true;
381 return false;
382}
383
384#define nid_has_mute(codec, nid, dir) \
385 check_amp_caps(codec, nid, dir, AC_AMPCAP_MUTE)
386#define nid_has_volume(codec, nid, dir) \
387 check_amp_caps(codec, nid, dir, AC_AMPCAP_NUM_STEPS)
388
389/* look for a widget suitable for assigning a mute switch in the path */
390static hda_nid_t look_for_out_mute_nid(struct hda_codec *codec,
391 struct nid_path *path)
392{
393 int i;
394
395 for (i = path->depth - 1; i >= 0; i--) {
396 if (nid_has_mute(codec, path->path[i], HDA_OUTPUT))
397 return path->path[i];
398 if (i != path->depth - 1 && i != 0 &&
399 nid_has_mute(codec, path->path[i], HDA_INPUT))
400 return path->path[i];
401 }
402 return 0;
403}
404
405/* look for a widget suitable for assigning a volume ctl in the path */
406static hda_nid_t look_for_out_vol_nid(struct hda_codec *codec,
407 struct nid_path *path)
408{
409 int i;
410
411 for (i = path->depth - 1; i >= 0; i--) {
412 if (nid_has_volume(codec, path->path[i], HDA_OUTPUT))
413 return path->path[i];
414 }
Takashi Iwai82beb8f2007-08-10 17:09:26 +0200415 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416}
417
418/*
Takashi Iwai352f7f92012-12-19 12:52:06 +0100419 * path activation / deactivation
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100421
422/* can have the amp-in capability? */
423static bool has_amp_in(struct hda_codec *codec, struct nid_path *path, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100425 hda_nid_t nid = path->path[idx];
426 unsigned int caps = get_wcaps(codec, nid);
427 unsigned int type = get_wcaps_type(caps);
428
429 if (!(caps & AC_WCAP_IN_AMP))
430 return false;
431 if (type == AC_WID_PIN && idx > 0) /* only for input pins */
432 return false;
433 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434}
435
Takashi Iwai352f7f92012-12-19 12:52:06 +0100436/* can have the amp-out capability? */
437static bool has_amp_out(struct hda_codec *codec, struct nid_path *path, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100439 hda_nid_t nid = path->path[idx];
440 unsigned int caps = get_wcaps(codec, nid);
441 unsigned int type = get_wcaps_type(caps);
442
443 if (!(caps & AC_WCAP_OUT_AMP))
444 return false;
445 if (type == AC_WID_PIN && !idx) /* only for output pins */
446 return false;
447 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448}
449
Takashi Iwai352f7f92012-12-19 12:52:06 +0100450/* check whether the given (nid,dir,idx) is active */
451static bool is_active_nid(struct hda_codec *codec, hda_nid_t nid,
452 unsigned int idx, unsigned int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100454 struct hda_gen_spec *spec = codec->spec;
455 int i, n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456
Takashi Iwai352f7f92012-12-19 12:52:06 +0100457 for (n = 0; n < spec->paths.used; n++) {
458 struct nid_path *path = snd_array_elem(&spec->paths, n);
459 if (!path->active)
460 continue;
461 for (i = 0; i < path->depth; i++) {
462 if (path->path[i] == nid) {
463 if (dir == HDA_OUTPUT || path->idx[i] == idx)
464 return true;
465 break;
466 }
467 }
468 }
469 return false;
470}
471
472/* get the default amp value for the target state */
473static int get_amp_val_to_activate(struct hda_codec *codec, hda_nid_t nid,
474 int dir, bool enable)
475{
476 unsigned int caps;
477 unsigned int val = 0;
478
479 caps = query_amp_caps(codec, nid, dir);
480 if (caps & AC_AMPCAP_NUM_STEPS) {
481 /* set to 0dB */
482 if (enable)
483 val = (caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT;
484 }
485 if (caps & AC_AMPCAP_MUTE) {
486 if (!enable)
487 val |= HDA_AMP_MUTE;
488 }
489 return val;
490}
491
492/* initialize the amp value (only at the first time) */
493static void init_amp(struct hda_codec *codec, hda_nid_t nid, int dir, int idx)
494{
495 int val = get_amp_val_to_activate(codec, nid, dir, false);
496 snd_hda_codec_amp_init_stereo(codec, nid, dir, idx, 0xff, val);
497}
498
499static void activate_amp(struct hda_codec *codec, hda_nid_t nid, int dir,
500 int idx, bool enable)
501{
502 int val;
503 if (is_ctl_associated(codec, nid, dir, idx) ||
Takashi Iwai985803c2013-01-03 16:30:04 +0100504 (!enable && is_active_nid(codec, nid, dir, idx)))
Takashi Iwai352f7f92012-12-19 12:52:06 +0100505 return;
506 val = get_amp_val_to_activate(codec, nid, dir, enable);
507 snd_hda_codec_amp_stereo(codec, nid, dir, idx, 0xff, val);
508}
509
510static void activate_amp_out(struct hda_codec *codec, struct nid_path *path,
511 int i, bool enable)
512{
513 hda_nid_t nid = path->path[i];
514 init_amp(codec, nid, HDA_OUTPUT, 0);
515 activate_amp(codec, nid, HDA_OUTPUT, 0, enable);
516}
517
518static void activate_amp_in(struct hda_codec *codec, struct nid_path *path,
519 int i, bool enable, bool add_aamix)
520{
521 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100522 const hda_nid_t *conn;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100523 int n, nums, idx;
524 int type;
525 hda_nid_t nid = path->path[i];
526
Takashi Iwaiee8e7652013-01-03 15:25:11 +0100527 nums = snd_hda_get_conn_list(codec, nid, &conn);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100528 type = get_wcaps_type(get_wcaps(codec, nid));
529 if (type == AC_WID_PIN ||
530 (type == AC_WID_AUD_IN && codec->single_adc_amp)) {
531 nums = 1;
532 idx = 0;
533 } else
534 idx = path->idx[i];
535
536 for (n = 0; n < nums; n++)
537 init_amp(codec, nid, HDA_INPUT, n);
538
539 if (is_ctl_associated(codec, nid, HDA_INPUT, idx))
540 return;
541
542 /* here is a little bit tricky in comparison with activate_amp_out();
543 * when aa-mixer is available, we need to enable the path as well
544 */
545 for (n = 0; n < nums; n++) {
546 if (n != idx && (!add_aamix || conn[n] != spec->mixer_nid))
547 continue;
548 activate_amp(codec, nid, HDA_INPUT, n, enable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 }
550}
551
Takashi Iwai352f7f92012-12-19 12:52:06 +0100552/* activate or deactivate the given path
553 * if @add_aamix is set, enable the input from aa-mix NID as well (if any)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100555void snd_hda_activate_path(struct hda_codec *codec, struct nid_path *path,
556 bool enable, bool add_aamix)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100558 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
Takashi Iwai352f7f92012-12-19 12:52:06 +0100560 if (!enable)
561 path->active = false;
562
563 for (i = path->depth - 1; i >= 0; i--) {
564 if (enable && path->multi[i])
565 snd_hda_codec_write_cache(codec, path->path[i], 0,
566 AC_VERB_SET_CONNECT_SEL,
567 path->idx[i]);
568 if (has_amp_in(codec, path, i))
569 activate_amp_in(codec, path, i, enable, add_aamix);
570 if (has_amp_out(codec, path, i))
571 activate_amp_out(codec, path, i, enable);
572 }
573
574 if (enable)
575 path->active = true;
576}
577EXPORT_SYMBOL_HDA(snd_hda_activate_path);
578
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +0100579/* turn on/off EAPD on the given pin */
580static void set_pin_eapd(struct hda_codec *codec, hda_nid_t pin, bool enable)
581{
582 struct hda_gen_spec *spec = codec->spec;
583 if (spec->own_eapd_ctl ||
584 !(snd_hda_query_pin_caps(codec, pin) & AC_PINCAP_EAPD))
585 return;
Takashi Iwaiecac3ed2012-12-21 15:23:01 +0100586 if (codec->inv_eapd)
587 enable = !enable;
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +0100588 snd_hda_codec_update_cache(codec, pin, 0,
589 AC_VERB_SET_EAPD_BTLENABLE,
590 enable ? 0x02 : 0x00);
591}
592
Takashi Iwai352f7f92012-12-19 12:52:06 +0100593
594/*
595 * Helper functions for creating mixer ctl elements
596 */
597
598enum {
599 HDA_CTL_WIDGET_VOL,
600 HDA_CTL_WIDGET_MUTE,
601 HDA_CTL_BIND_MUTE,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100602};
603static const struct snd_kcontrol_new control_templates[] = {
604 HDA_CODEC_VOLUME(NULL, 0, 0, 0),
605 HDA_CODEC_MUTE(NULL, 0, 0, 0),
606 HDA_BIND_MUTE(NULL, 0, 0, 0),
Takashi Iwai352f7f92012-12-19 12:52:06 +0100607};
608
609/* add dynamic controls from template */
610static int add_control(struct hda_gen_spec *spec, int type, const char *name,
611 int cidx, unsigned long val)
612{
613 struct snd_kcontrol_new *knew;
614
Takashi Iwai12c93df2012-12-19 14:38:33 +0100615 knew = snd_hda_gen_add_kctl(spec, name, &control_templates[type]);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100616 if (!knew)
617 return -ENOMEM;
618 knew->index = cidx;
619 if (get_amp_nid_(val))
620 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
621 knew->private_value = val;
622 return 0;
623}
624
625static int add_control_with_pfx(struct hda_gen_spec *spec, int type,
626 const char *pfx, const char *dir,
627 const char *sfx, int cidx, unsigned long val)
628{
629 char name[32];
630 snprintf(name, sizeof(name), "%s %s %s", pfx, dir, sfx);
631 return add_control(spec, type, name, cidx, val);
632}
633
634#define add_pb_vol_ctrl(spec, type, pfx, val) \
635 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", 0, val)
636#define add_pb_sw_ctrl(spec, type, pfx, val) \
637 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", 0, val)
638#define __add_pb_vol_ctrl(spec, type, pfx, cidx, val) \
639 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", cidx, val)
640#define __add_pb_sw_ctrl(spec, type, pfx, cidx, val) \
641 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", cidx, val)
642
643static int add_vol_ctl(struct hda_codec *codec, const char *pfx, int cidx,
644 unsigned int chs, struct nid_path *path)
645{
646 unsigned int val;
647 if (!path)
648 return 0;
649 val = path->ctls[NID_PATH_VOL_CTL];
650 if (!val)
651 return 0;
652 val = amp_val_replace_channels(val, chs);
653 return __add_pb_vol_ctrl(codec->spec, HDA_CTL_WIDGET_VOL, pfx, cidx, val);
654}
655
656/* return the channel bits suitable for the given path->ctls[] */
657static int get_default_ch_nums(struct hda_codec *codec, struct nid_path *path,
658 int type)
659{
660 int chs = 1; /* mono (left only) */
661 if (path) {
662 hda_nid_t nid = get_amp_nid_(path->ctls[type]);
663 if (nid && (get_wcaps(codec, nid) & AC_WCAP_STEREO))
664 chs = 3; /* stereo */
665 }
666 return chs;
667}
668
669static int add_stereo_vol(struct hda_codec *codec, const char *pfx, int cidx,
670 struct nid_path *path)
671{
672 int chs = get_default_ch_nums(codec, path, NID_PATH_VOL_CTL);
673 return add_vol_ctl(codec, pfx, cidx, chs, path);
674}
675
676/* create a mute-switch for the given mixer widget;
677 * if it has multiple sources (e.g. DAC and loopback), create a bind-mute
678 */
679static int add_sw_ctl(struct hda_codec *codec, const char *pfx, int cidx,
680 unsigned int chs, struct nid_path *path)
681{
682 unsigned int val;
683 int type = HDA_CTL_WIDGET_MUTE;
684
685 if (!path)
686 return 0;
687 val = path->ctls[NID_PATH_MUTE_CTL];
688 if (!val)
689 return 0;
690 val = amp_val_replace_channels(val, chs);
691 if (get_amp_direction_(val) == HDA_INPUT) {
692 hda_nid_t nid = get_amp_nid_(val);
693 int nums = snd_hda_get_num_conns(codec, nid);
694 if (nums > 1) {
695 type = HDA_CTL_BIND_MUTE;
696 val |= nums << 19;
697 }
698 }
699 return __add_pb_sw_ctrl(codec->spec, type, pfx, cidx, val);
700}
701
702static int add_stereo_sw(struct hda_codec *codec, const char *pfx,
703 int cidx, struct nid_path *path)
704{
705 int chs = get_default_ch_nums(codec, path, NID_PATH_MUTE_CTL);
706 return add_sw_ctl(codec, pfx, cidx, chs, path);
707}
708
709static const char * const channel_name[4] = {
710 "Front", "Surround", "CLFE", "Side"
711};
712
713/* give some appropriate ctl name prefix for the given line out channel */
714static const char *get_line_out_pfx(struct hda_gen_spec *spec, int ch,
715 bool can_be_master, int *index)
716{
717 struct auto_pin_cfg *cfg = &spec->autocfg;
718
719 *index = 0;
720 if (cfg->line_outs == 1 && !spec->multi_ios &&
721 !cfg->hp_outs && !cfg->speaker_outs && can_be_master)
722 return spec->vmaster_mute.hook ? "PCM" : "Master";
723
724 /* if there is really a single DAC used in the whole output paths,
725 * use it master (or "PCM" if a vmaster hook is present)
726 */
727 if (spec->multiout.num_dacs == 1 && !spec->mixer_nid &&
728 !spec->multiout.hp_out_nid[0] && !spec->multiout.extra_out_nid[0])
729 return spec->vmaster_mute.hook ? "PCM" : "Master";
730
731 switch (cfg->line_out_type) {
732 case AUTO_PIN_SPEAKER_OUT:
733 if (cfg->line_outs == 1)
734 return "Speaker";
735 if (cfg->line_outs == 2)
736 return ch ? "Bass Speaker" : "Speaker";
737 break;
738 case AUTO_PIN_HP_OUT:
739 /* for multi-io case, only the primary out */
740 if (ch && spec->multi_ios)
741 break;
742 *index = ch;
743 return "Headphone";
744 default:
745 if (cfg->line_outs == 1 && !spec->multi_ios)
746 return "PCM";
747 break;
748 }
749 if (ch >= ARRAY_SIZE(channel_name)) {
750 snd_BUG();
751 return "PCM";
752 }
753
754 return channel_name[ch];
755}
756
757/*
758 * Parse output paths
759 */
760
761/* badness definition */
762enum {
763 /* No primary DAC is found for the main output */
764 BAD_NO_PRIMARY_DAC = 0x10000,
765 /* No DAC is found for the extra output */
766 BAD_NO_DAC = 0x4000,
767 /* No possible multi-ios */
768 BAD_MULTI_IO = 0x103,
769 /* No individual DAC for extra output */
770 BAD_NO_EXTRA_DAC = 0x102,
771 /* No individual DAC for extra surrounds */
772 BAD_NO_EXTRA_SURR_DAC = 0x101,
773 /* Primary DAC shared with main surrounds */
774 BAD_SHARED_SURROUND = 0x100,
775 /* Primary DAC shared with main CLFE */
776 BAD_SHARED_CLFE = 0x10,
777 /* Primary DAC shared with extra surrounds */
778 BAD_SHARED_EXTRA_SURROUND = 0x10,
779 /* Volume widget is shared */
780 BAD_SHARED_VOL = 0x10,
781};
782
Takashi Iwai0e614dd2013-01-07 15:11:44 +0100783/* look for widgets in the given path which are appropriate for
Takashi Iwai352f7f92012-12-19 12:52:06 +0100784 * volume and mute controls, and assign the values to ctls[].
785 *
786 * When no appropriate widget is found in the path, the badness value
787 * is incremented depending on the situation. The function returns the
788 * total badness for both volume and mute controls.
789 */
Takashi Iwai0e614dd2013-01-07 15:11:44 +0100790static int assign_out_path_ctls(struct hda_codec *codec, struct nid_path *path)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100791{
Takashi Iwai352f7f92012-12-19 12:52:06 +0100792 hda_nid_t nid;
793 unsigned int val;
794 int badness = 0;
795
796 if (!path)
797 return BAD_SHARED_VOL * 2;
Takashi Iwai0e614dd2013-01-07 15:11:44 +0100798
799 if (path->ctls[NID_PATH_VOL_CTL] ||
800 path->ctls[NID_PATH_MUTE_CTL])
801 return 0; /* already evaluated */
802
Takashi Iwai352f7f92012-12-19 12:52:06 +0100803 nid = look_for_out_vol_nid(codec, path);
804 if (nid) {
805 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
806 if (is_ctl_used(codec, val, NID_PATH_VOL_CTL))
807 badness += BAD_SHARED_VOL;
808 else
809 path->ctls[NID_PATH_VOL_CTL] = val;
810 } else
811 badness += BAD_SHARED_VOL;
812 nid = look_for_out_mute_nid(codec, path);
813 if (nid) {
814 unsigned int wid_type = get_wcaps_type(get_wcaps(codec, nid));
815 if (wid_type == AC_WID_PIN || wid_type == AC_WID_AUD_OUT ||
816 nid_has_mute(codec, nid, HDA_OUTPUT))
817 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
818 else
819 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT);
820 if (is_ctl_used(codec, val, NID_PATH_MUTE_CTL))
821 badness += BAD_SHARED_VOL;
822 else
823 path->ctls[NID_PATH_MUTE_CTL] = val;
824 } else
825 badness += BAD_SHARED_VOL;
826 return badness;
827}
828
829struct badness_table {
830 int no_primary_dac; /* no primary DAC */
831 int no_dac; /* no secondary DACs */
832 int shared_primary; /* primary DAC is shared with main output */
833 int shared_surr; /* secondary DAC shared with main or primary */
834 int shared_clfe; /* third DAC shared with main or primary */
835 int shared_surr_main; /* secondary DAC sahred with main/DAC0 */
836};
837
838static struct badness_table main_out_badness = {
839 .no_primary_dac = BAD_NO_PRIMARY_DAC,
840 .no_dac = BAD_NO_DAC,
841 .shared_primary = BAD_NO_PRIMARY_DAC,
842 .shared_surr = BAD_SHARED_SURROUND,
843 .shared_clfe = BAD_SHARED_CLFE,
844 .shared_surr_main = BAD_SHARED_SURROUND,
845};
846
847static struct badness_table extra_out_badness = {
848 .no_primary_dac = BAD_NO_DAC,
849 .no_dac = BAD_NO_DAC,
850 .shared_primary = BAD_NO_EXTRA_DAC,
851 .shared_surr = BAD_SHARED_EXTRA_SURROUND,
852 .shared_clfe = BAD_SHARED_EXTRA_SURROUND,
853 .shared_surr_main = BAD_NO_EXTRA_SURR_DAC,
854};
855
Takashi Iwai7385df62013-01-07 09:50:52 +0100856/* get the DAC of the primary output corresponding to the given array index */
857static hda_nid_t get_primary_out(struct hda_codec *codec, int idx)
858{
859 struct hda_gen_spec *spec = codec->spec;
860 struct auto_pin_cfg *cfg = &spec->autocfg;
861
862 if (cfg->line_outs > idx)
863 return spec->private_dac_nids[idx];
864 idx -= cfg->line_outs;
865 if (spec->multi_ios > idx)
866 return spec->multi_io[idx].dac;
867 return 0;
868}
869
870/* return the DAC if it's reachable, otherwise zero */
871static inline hda_nid_t try_dac(struct hda_codec *codec,
872 hda_nid_t dac, hda_nid_t pin)
873{
874 return is_reachable_path(codec, dac, pin) ? dac : 0;
875}
876
Takashi Iwai352f7f92012-12-19 12:52:06 +0100877/* try to assign DACs to pins and return the resultant badness */
878static int try_assign_dacs(struct hda_codec *codec, int num_outs,
879 const hda_nid_t *pins, hda_nid_t *dacs,
Takashi Iwai196c17662013-01-04 15:01:40 +0100880 int *path_idx,
Takashi Iwai352f7f92012-12-19 12:52:06 +0100881 const struct badness_table *bad)
882{
883 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100884 int i, j;
885 int badness = 0;
886 hda_nid_t dac;
887
888 if (!num_outs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 return 0;
890
Takashi Iwai352f7f92012-12-19 12:52:06 +0100891 for (i = 0; i < num_outs; i++) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +0100892 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100893 hda_nid_t pin = pins[i];
Takashi Iwai1e0b5282013-01-04 12:56:52 +0100894
Takashi Iwai0e614dd2013-01-07 15:11:44 +0100895 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
896 if (path) {
897 badness += assign_out_path_ctls(codec, path);
Takashi Iwai1e0b5282013-01-04 12:56:52 +0100898 continue;
899 }
900
901 dacs[i] = look_for_dac(codec, pin, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100902 if (!dacs[i] && !i) {
Takashi Iwai980428c2013-01-09 09:28:20 +0100903 /* try to steal the DAC of surrounds for the front */
Takashi Iwai352f7f92012-12-19 12:52:06 +0100904 for (j = 1; j < num_outs; j++) {
905 if (is_reachable_path(codec, dacs[j], pin)) {
906 dacs[0] = dacs[j];
907 dacs[j] = 0;
Takashi Iwai980428c2013-01-09 09:28:20 +0100908 invalidate_nid_path(codec, path_idx[j]);
Takashi Iwai196c17662013-01-04 15:01:40 +0100909 path_idx[j] = 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +0100910 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 }
912 }
Takashi Iwai352f7f92012-12-19 12:52:06 +0100913 }
914 dac = dacs[i];
915 if (!dac) {
Takashi Iwai7385df62013-01-07 09:50:52 +0100916 if (num_outs > 2)
917 dac = try_dac(codec, get_primary_out(codec, i), pin);
918 if (!dac)
919 dac = try_dac(codec, dacs[0], pin);
920 if (!dac)
921 dac = try_dac(codec, get_primary_out(codec, i), pin);
Takashi Iwai352f7f92012-12-19 12:52:06 +0100922 if (dac) {
923 if (!i)
924 badness += bad->shared_primary;
925 else if (i == 1)
926 badness += bad->shared_surr;
927 else
928 badness += bad->shared_clfe;
929 } else if (is_reachable_path(codec, spec->private_dac_nids[0], pin)) {
930 dac = spec->private_dac_nids[0];
931 badness += bad->shared_surr_main;
932 } else if (!i)
933 badness += bad->no_primary_dac;
934 else
935 badness += bad->no_dac;
936 }
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100937 path = snd_hda_add_new_path(codec, dac, pin, -spec->mixer_nid);
Takashi Iwai117688a2013-01-04 15:41:41 +0100938 if (!path && !i && spec->mixer_nid) {
Takashi Iwaib3a8c742012-12-20 18:29:16 +0100939 /* try with aamix */
Takashi Iwai3ca529d2013-01-07 17:25:08 +0100940 path = snd_hda_add_new_path(codec, dac, pin, 0);
Takashi Iwaib3a8c742012-12-20 18:29:16 +0100941 }
Takashi Iwai0c8c0f52012-12-20 17:54:22 +0100942 if (!path)
Takashi Iwai352f7f92012-12-19 12:52:06 +0100943 dac = dacs[i] = 0;
Takashi Iwaie1284af2013-01-03 16:33:02 +0100944 else {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +0100945 print_nid_path("output", path);
Takashi Iwaie1284af2013-01-03 16:33:02 +0100946 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +0100947 path_idx[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai0e614dd2013-01-07 15:11:44 +0100948 badness += assign_out_path_ctls(codec, path);
Takashi Iwaie1284af2013-01-03 16:33:02 +0100949 }
Takashi Iwai352f7f92012-12-19 12:52:06 +0100950 }
951
952 return badness;
953}
954
955/* return NID if the given pin has only a single connection to a certain DAC */
956static hda_nid_t get_dac_if_single(struct hda_codec *codec, hda_nid_t pin)
957{
958 struct hda_gen_spec *spec = codec->spec;
959 int i;
960 hda_nid_t nid_found = 0;
961
962 for (i = 0; i < spec->num_all_dacs; i++) {
963 hda_nid_t nid = spec->all_dacs[i];
964 if (!nid || is_dac_already_used(codec, nid))
965 continue;
966 if (is_reachable_path(codec, nid, pin)) {
967 if (nid_found)
968 return 0;
969 nid_found = nid;
970 }
971 }
972 return nid_found;
973}
974
975/* check whether the given pin can be a multi-io pin */
976static bool can_be_multiio_pin(struct hda_codec *codec,
977 unsigned int location, hda_nid_t nid)
978{
979 unsigned int defcfg, caps;
980
981 defcfg = snd_hda_codec_get_pincfg(codec, nid);
982 if (get_defcfg_connect(defcfg) != AC_JACK_PORT_COMPLEX)
983 return false;
984 if (location && get_defcfg_location(defcfg) != location)
985 return false;
986 caps = snd_hda_query_pin_caps(codec, nid);
987 if (!(caps & AC_PINCAP_OUT))
988 return false;
989 return true;
990}
991
Takashi Iwaie22aab72013-01-04 14:50:04 +0100992/* count the number of input pins that are capable to be multi-io */
993static int count_multiio_pins(struct hda_codec *codec, hda_nid_t reference_pin)
994{
995 struct hda_gen_spec *spec = codec->spec;
996 struct auto_pin_cfg *cfg = &spec->autocfg;
997 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
998 unsigned int location = get_defcfg_location(defcfg);
999 int type, i;
1000 int num_pins = 0;
1001
1002 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1003 for (i = 0; i < cfg->num_inputs; i++) {
1004 if (cfg->inputs[i].type != type)
1005 continue;
1006 if (can_be_multiio_pin(codec, location,
1007 cfg->inputs[i].pin))
1008 num_pins++;
1009 }
1010 }
1011 return num_pins;
1012}
1013
Takashi Iwai352f7f92012-12-19 12:52:06 +01001014/*
1015 * multi-io helper
1016 *
1017 * When hardwired is set, try to fill ony hardwired pins, and returns
1018 * zero if any pins are filled, non-zero if nothing found.
1019 * When hardwired is off, try to fill possible input pins, and returns
1020 * the badness value.
1021 */
1022static int fill_multi_ios(struct hda_codec *codec,
1023 hda_nid_t reference_pin,
Takashi Iwaie22aab72013-01-04 14:50:04 +01001024 bool hardwired)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001025{
1026 struct hda_gen_spec *spec = codec->spec;
1027 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwaie22aab72013-01-04 14:50:04 +01001028 int type, i, j, num_pins, old_pins;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001029 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1030 unsigned int location = get_defcfg_location(defcfg);
1031 int badness = 0;
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001032 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001033
1034 old_pins = spec->multi_ios;
1035 if (old_pins >= 2)
1036 goto end_fill;
1037
Takashi Iwaie22aab72013-01-04 14:50:04 +01001038 num_pins = count_multiio_pins(codec, reference_pin);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001039 if (num_pins < 2)
1040 goto end_fill;
1041
Takashi Iwai352f7f92012-12-19 12:52:06 +01001042 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1043 for (i = 0; i < cfg->num_inputs; i++) {
1044 hda_nid_t nid = cfg->inputs[i].pin;
1045 hda_nid_t dac = 0;
1046
1047 if (cfg->inputs[i].type != type)
1048 continue;
1049 if (!can_be_multiio_pin(codec, location, nid))
1050 continue;
1051 for (j = 0; j < spec->multi_ios; j++) {
1052 if (nid == spec->multi_io[j].pin)
1053 break;
1054 }
1055 if (j < spec->multi_ios)
1056 continue;
1057
Takashi Iwai352f7f92012-12-19 12:52:06 +01001058 if (hardwired)
1059 dac = get_dac_if_single(codec, nid);
1060 else if (!dac)
1061 dac = look_for_dac(codec, nid, false);
1062 if (!dac) {
1063 badness++;
1064 continue;
1065 }
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001066 path = snd_hda_add_new_path(codec, dac, nid,
1067 -spec->mixer_nid);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001068 if (!path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001069 badness++;
1070 continue;
1071 }
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001072 print_nid_path("multiio", path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001073 spec->multi_io[spec->multi_ios].pin = nid;
1074 spec->multi_io[spec->multi_ios].dac = dac;
Takashi Iwai196c17662013-01-04 15:01:40 +01001075 spec->out_paths[cfg->line_outs + spec->multi_ios] =
1076 snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001077 spec->multi_ios++;
1078 if (spec->multi_ios >= 2)
1079 break;
1080 }
1081 }
1082 end_fill:
1083 if (badness)
1084 badness = BAD_MULTI_IO;
1085 if (old_pins == spec->multi_ios) {
1086 if (hardwired)
1087 return 1; /* nothing found */
1088 else
1089 return badness; /* no badness if nothing found */
1090 }
1091 if (!hardwired && spec->multi_ios < 2) {
1092 /* cancel newly assigned paths */
1093 spec->paths.used -= spec->multi_ios - old_pins;
1094 spec->multi_ios = old_pins;
1095 return badness;
1096 }
1097
1098 /* assign volume and mute controls */
Takashi Iwai0e614dd2013-01-07 15:11:44 +01001099 for (i = old_pins; i < spec->multi_ios; i++) {
1100 path = snd_hda_get_path_from_idx(codec, spec->out_paths[cfg->line_outs + i]);
1101 badness += assign_out_path_ctls(codec, path);
1102 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001103
1104 return badness;
1105}
1106
1107/* map DACs for all pins in the list if they are single connections */
1108static bool map_singles(struct hda_codec *codec, int outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001109 const hda_nid_t *pins, hda_nid_t *dacs, int *path_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001110{
Takashi Iwaib3a8c742012-12-20 18:29:16 +01001111 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001112 int i;
1113 bool found = false;
1114 for (i = 0; i < outs; i++) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001115 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001116 hda_nid_t dac;
1117 if (dacs[i])
1118 continue;
1119 dac = get_dac_if_single(codec, pins[i]);
1120 if (!dac)
1121 continue;
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001122 path = snd_hda_add_new_path(codec, dac, pins[i],
1123 -spec->mixer_nid);
Takashi Iwai117688a2013-01-04 15:41:41 +01001124 if (!path && !i && spec->mixer_nid)
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001125 path = snd_hda_add_new_path(codec, dac, pins[i], 0);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001126 if (path) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001127 dacs[i] = dac;
1128 found = true;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001129 print_nid_path("output", path);
Takashi Iwaie1284af2013-01-03 16:33:02 +01001130 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +01001131 path_idx[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001132 }
1133 }
1134 return found;
1135}
1136
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001137/* create a new path including aamix if available, and return its index */
1138static int check_aamix_out_path(struct hda_codec *codec, int path_idx)
1139{
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001140 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001141 struct nid_path *path;
1142
1143 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001144 if (!path || !path->depth ||
1145 is_nid_contained(path, spec->mixer_nid))
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001146 return 0;
1147 path = snd_hda_add_new_path(codec, path->path[0],
1148 path->path[path->depth - 1],
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001149 spec->mixer_nid);
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001150 if (!path)
1151 return 0;
1152 print_nid_path("output-aamix", path);
1153 path->active = false; /* unused as default */
1154 return snd_hda_get_path_idx(codec, path);
1155}
1156
Takashi Iwaia07a9492013-01-07 16:44:06 +01001157/* fill the empty entries in the dac array for speaker/hp with the
1158 * shared dac pointed by the paths
1159 */
1160static void refill_shared_dacs(struct hda_codec *codec, int num_outs,
1161 hda_nid_t *dacs, int *path_idx)
1162{
1163 struct nid_path *path;
1164 int i;
1165
1166 for (i = 0; i < num_outs; i++) {
1167 if (dacs[i])
1168 continue;
1169 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
1170 if (!path)
1171 continue;
1172 dacs[i] = path->path[0];
1173 }
1174}
1175
Takashi Iwai352f7f92012-12-19 12:52:06 +01001176/* fill in the dac_nids table from the parsed pin configuration */
1177static int fill_and_eval_dacs(struct hda_codec *codec,
1178 bool fill_hardwired,
1179 bool fill_mio_first)
1180{
1181 struct hda_gen_spec *spec = codec->spec;
1182 struct auto_pin_cfg *cfg = &spec->autocfg;
1183 int i, err, badness;
1184
1185 /* set num_dacs once to full for look_for_dac() */
1186 spec->multiout.num_dacs = cfg->line_outs;
1187 spec->multiout.dac_nids = spec->private_dac_nids;
1188 memset(spec->private_dac_nids, 0, sizeof(spec->private_dac_nids));
1189 memset(spec->multiout.hp_out_nid, 0, sizeof(spec->multiout.hp_out_nid));
1190 memset(spec->multiout.extra_out_nid, 0, sizeof(spec->multiout.extra_out_nid));
1191 spec->multi_ios = 0;
1192 snd_array_free(&spec->paths);
Takashi Iwaicd5be3f2013-01-07 15:07:00 +01001193
1194 /* clear path indices */
1195 memset(spec->out_paths, 0, sizeof(spec->out_paths));
1196 memset(spec->hp_paths, 0, sizeof(spec->hp_paths));
1197 memset(spec->speaker_paths, 0, sizeof(spec->speaker_paths));
1198 memset(spec->aamix_out_paths, 0, sizeof(spec->aamix_out_paths));
1199 memset(spec->digout_paths, 0, sizeof(spec->digout_paths));
Takashi Iwaic697b712013-01-07 17:09:26 +01001200 memset(spec->input_paths, 0, sizeof(spec->input_paths));
Takashi Iwaicd5be3f2013-01-07 15:07:00 +01001201 memset(spec->loopback_paths, 0, sizeof(spec->loopback_paths));
1202 memset(&spec->digin_path, 0, sizeof(spec->digin_path));
1203
Takashi Iwai352f7f92012-12-19 12:52:06 +01001204 badness = 0;
1205
1206 /* fill hard-wired DACs first */
1207 if (fill_hardwired) {
1208 bool mapped;
1209 do {
1210 mapped = map_singles(codec, cfg->line_outs,
1211 cfg->line_out_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001212 spec->private_dac_nids,
1213 spec->out_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001214 mapped |= map_singles(codec, cfg->hp_outs,
1215 cfg->hp_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001216 spec->multiout.hp_out_nid,
1217 spec->hp_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001218 mapped |= map_singles(codec, cfg->speaker_outs,
1219 cfg->speaker_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001220 spec->multiout.extra_out_nid,
1221 spec->speaker_paths);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001222 if (fill_mio_first && cfg->line_outs == 1 &&
1223 cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
Takashi Iwaie22aab72013-01-04 14:50:04 +01001224 err = fill_multi_ios(codec, cfg->line_out_pins[0], true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001225 if (!err)
1226 mapped = true;
1227 }
1228 } while (mapped);
1229 }
1230
1231 badness += try_assign_dacs(codec, cfg->line_outs, cfg->line_out_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001232 spec->private_dac_nids, spec->out_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001233 &main_out_badness);
1234
Takashi Iwai352f7f92012-12-19 12:52:06 +01001235 if (fill_mio_first &&
1236 cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1237 /* try to fill multi-io first */
Takashi Iwaie22aab72013-01-04 14:50:04 +01001238 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001239 if (err < 0)
1240 return err;
1241 /* we don't count badness at this stage yet */
1242 }
1243
1244 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
1245 err = try_assign_dacs(codec, cfg->hp_outs, cfg->hp_pins,
1246 spec->multiout.hp_out_nid,
Takashi Iwai196c17662013-01-04 15:01:40 +01001247 spec->hp_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001248 &extra_out_badness);
1249 if (err < 0)
1250 return err;
1251 badness += err;
1252 }
1253 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1254 err = try_assign_dacs(codec, cfg->speaker_outs,
1255 cfg->speaker_pins,
1256 spec->multiout.extra_out_nid,
Takashi Iwai196c17662013-01-04 15:01:40 +01001257 spec->speaker_paths,
1258 &extra_out_badness);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001259 if (err < 0)
1260 return err;
1261 badness += err;
1262 }
1263 if (cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
Takashi Iwaie22aab72013-01-04 14:50:04 +01001264 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001265 if (err < 0)
1266 return err;
1267 badness += err;
1268 }
Takashi Iwaie22aab72013-01-04 14:50:04 +01001269
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001270 if (spec->mixer_nid) {
1271 spec->aamix_out_paths[0] =
1272 check_aamix_out_path(codec, spec->out_paths[0]);
1273 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1274 spec->aamix_out_paths[1] =
1275 check_aamix_out_path(codec, spec->hp_paths[0]);
1276 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1277 spec->aamix_out_paths[2] =
1278 check_aamix_out_path(codec, spec->speaker_paths[0]);
1279 }
1280
Takashi Iwaie22aab72013-01-04 14:50:04 +01001281 if (cfg->hp_outs && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
1282 if (count_multiio_pins(codec, cfg->hp_pins[0]) >= 2)
1283 spec->multi_ios = 1; /* give badness */
Takashi Iwai352f7f92012-12-19 12:52:06 +01001284
Takashi Iwaia07a9492013-01-07 16:44:06 +01001285 /* re-count num_dacs and squash invalid entries */
1286 spec->multiout.num_dacs = 0;
1287 for (i = 0; i < cfg->line_outs; i++) {
1288 if (spec->private_dac_nids[i])
1289 spec->multiout.num_dacs++;
1290 else {
1291 memmove(spec->private_dac_nids + i,
1292 spec->private_dac_nids + i + 1,
1293 sizeof(hda_nid_t) * (cfg->line_outs - i - 1));
1294 spec->private_dac_nids[cfg->line_outs - 1] = 0;
1295 }
1296 }
1297
1298 spec->ext_channel_count = spec->min_channel_count =
1299 spec->multiout.num_dacs;
1300
Takashi Iwai352f7f92012-12-19 12:52:06 +01001301 if (spec->multi_ios == 2) {
1302 for (i = 0; i < 2; i++)
1303 spec->private_dac_nids[spec->multiout.num_dacs++] =
1304 spec->multi_io[i].dac;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001305 } else if (spec->multi_ios) {
1306 spec->multi_ios = 0;
1307 badness += BAD_MULTI_IO;
1308 }
1309
Takashi Iwaia07a9492013-01-07 16:44:06 +01001310 /* re-fill the shared DAC for speaker / headphone */
1311 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1312 refill_shared_dacs(codec, cfg->hp_outs,
1313 spec->multiout.hp_out_nid,
1314 spec->hp_paths);
1315 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1316 refill_shared_dacs(codec, cfg->speaker_outs,
1317 spec->multiout.extra_out_nid,
1318 spec->speaker_paths);
1319
Takashi Iwai352f7f92012-12-19 12:52:06 +01001320 return badness;
1321}
1322
1323#define DEBUG_BADNESS
1324
1325#ifdef DEBUG_BADNESS
1326#define debug_badness snd_printdd
1327#else
1328#define debug_badness(...)
1329#endif
1330
1331static void debug_show_configs(struct hda_gen_spec *spec, struct auto_pin_cfg *cfg)
1332{
1333 debug_badness("multi_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1334 cfg->line_out_pins[0], cfg->line_out_pins[1],
Takashi Iwai708122e2012-12-20 17:56:57 +01001335 cfg->line_out_pins[2], cfg->line_out_pins[3],
Takashi Iwai352f7f92012-12-19 12:52:06 +01001336 spec->multiout.dac_nids[0],
1337 spec->multiout.dac_nids[1],
1338 spec->multiout.dac_nids[2],
1339 spec->multiout.dac_nids[3]);
1340 if (spec->multi_ios > 0)
1341 debug_badness("multi_ios(%d) = %x/%x : %x/%x\n",
1342 spec->multi_ios,
1343 spec->multi_io[0].pin, spec->multi_io[1].pin,
1344 spec->multi_io[0].dac, spec->multi_io[1].dac);
1345 debug_badness("hp_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1346 cfg->hp_pins[0], cfg->hp_pins[1],
Takashi Iwai708122e2012-12-20 17:56:57 +01001347 cfg->hp_pins[2], cfg->hp_pins[3],
Takashi Iwai352f7f92012-12-19 12:52:06 +01001348 spec->multiout.hp_out_nid[0],
1349 spec->multiout.hp_out_nid[1],
1350 spec->multiout.hp_out_nid[2],
1351 spec->multiout.hp_out_nid[3]);
1352 debug_badness("spk_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1353 cfg->speaker_pins[0], cfg->speaker_pins[1],
1354 cfg->speaker_pins[2], cfg->speaker_pins[3],
1355 spec->multiout.extra_out_nid[0],
1356 spec->multiout.extra_out_nid[1],
1357 spec->multiout.extra_out_nid[2],
1358 spec->multiout.extra_out_nid[3]);
1359}
1360
1361/* find all available DACs of the codec */
1362static void fill_all_dac_nids(struct hda_codec *codec)
1363{
1364 struct hda_gen_spec *spec = codec->spec;
1365 int i;
1366 hda_nid_t nid = codec->start_nid;
1367
1368 spec->num_all_dacs = 0;
1369 memset(spec->all_dacs, 0, sizeof(spec->all_dacs));
1370 for (i = 0; i < codec->num_nodes; i++, nid++) {
1371 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_AUD_OUT)
1372 continue;
1373 if (spec->num_all_dacs >= ARRAY_SIZE(spec->all_dacs)) {
1374 snd_printk(KERN_ERR "hda: Too many DACs!\n");
1375 break;
1376 }
1377 spec->all_dacs[spec->num_all_dacs++] = nid;
1378 }
1379}
1380
1381static int parse_output_paths(struct hda_codec *codec)
1382{
1383 struct hda_gen_spec *spec = codec->spec;
1384 struct auto_pin_cfg *cfg = &spec->autocfg;
1385 struct auto_pin_cfg *best_cfg;
1386 int best_badness = INT_MAX;
1387 int badness;
1388 bool fill_hardwired = true, fill_mio_first = true;
1389 bool best_wired = true, best_mio = true;
1390 bool hp_spk_swapped = false;
1391
1392 fill_all_dac_nids(codec);
1393
1394 best_cfg = kmalloc(sizeof(*best_cfg), GFP_KERNEL);
1395 if (!best_cfg)
1396 return -ENOMEM;
1397 *best_cfg = *cfg;
1398
1399 for (;;) {
1400 badness = fill_and_eval_dacs(codec, fill_hardwired,
1401 fill_mio_first);
1402 if (badness < 0) {
1403 kfree(best_cfg);
1404 return badness;
1405 }
1406 debug_badness("==> lo_type=%d, wired=%d, mio=%d, badness=0x%x\n",
1407 cfg->line_out_type, fill_hardwired, fill_mio_first,
1408 badness);
1409 debug_show_configs(spec, cfg);
1410 if (badness < best_badness) {
1411 best_badness = badness;
1412 *best_cfg = *cfg;
1413 best_wired = fill_hardwired;
1414 best_mio = fill_mio_first;
1415 }
1416 if (!badness)
1417 break;
1418 fill_mio_first = !fill_mio_first;
1419 if (!fill_mio_first)
1420 continue;
1421 fill_hardwired = !fill_hardwired;
1422 if (!fill_hardwired)
1423 continue;
1424 if (hp_spk_swapped)
1425 break;
1426 hp_spk_swapped = true;
1427 if (cfg->speaker_outs > 0 &&
1428 cfg->line_out_type == AUTO_PIN_HP_OUT) {
1429 cfg->hp_outs = cfg->line_outs;
1430 memcpy(cfg->hp_pins, cfg->line_out_pins,
1431 sizeof(cfg->hp_pins));
1432 cfg->line_outs = cfg->speaker_outs;
1433 memcpy(cfg->line_out_pins, cfg->speaker_pins,
1434 sizeof(cfg->speaker_pins));
1435 cfg->speaker_outs = 0;
1436 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
1437 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
1438 fill_hardwired = true;
1439 continue;
1440 }
1441 if (cfg->hp_outs > 0 &&
1442 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
1443 cfg->speaker_outs = cfg->line_outs;
1444 memcpy(cfg->speaker_pins, cfg->line_out_pins,
1445 sizeof(cfg->speaker_pins));
1446 cfg->line_outs = cfg->hp_outs;
1447 memcpy(cfg->line_out_pins, cfg->hp_pins,
1448 sizeof(cfg->hp_pins));
1449 cfg->hp_outs = 0;
1450 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
1451 cfg->line_out_type = AUTO_PIN_HP_OUT;
1452 fill_hardwired = true;
1453 continue;
1454 }
1455 break;
1456 }
1457
1458 if (badness) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001459 debug_badness("==> restoring best_cfg\n");
Takashi Iwai352f7f92012-12-19 12:52:06 +01001460 *cfg = *best_cfg;
1461 fill_and_eval_dacs(codec, best_wired, best_mio);
1462 }
1463 debug_badness("==> Best config: lo_type=%d, wired=%d, mio=%d\n",
1464 cfg->line_out_type, best_wired, best_mio);
1465 debug_show_configs(spec, cfg);
1466
1467 if (cfg->line_out_pins[0]) {
1468 struct nid_path *path;
Takashi Iwai196c17662013-01-04 15:01:40 +01001469 path = snd_hda_get_path_from_idx(codec, spec->out_paths[0]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001470 if (path)
1471 spec->vmaster_nid = look_for_out_vol_nid(codec, path);
1472 }
1473
1474 kfree(best_cfg);
1475 return 0;
1476}
1477
1478/* add playback controls from the parsed DAC table */
1479static int create_multi_out_ctls(struct hda_codec *codec,
1480 const struct auto_pin_cfg *cfg)
1481{
1482 struct hda_gen_spec *spec = codec->spec;
1483 int i, err, noutputs;
1484
1485 noutputs = cfg->line_outs;
1486 if (spec->multi_ios > 0 && cfg->line_outs < 3)
1487 noutputs += spec->multi_ios;
1488
1489 for (i = 0; i < noutputs; i++) {
1490 const char *name;
1491 int index;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001492 struct nid_path *path;
1493
Takashi Iwai352f7f92012-12-19 12:52:06 +01001494 if (i >= cfg->line_outs) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001495 index = 0;
1496 name = channel_name[i];
1497 } else {
Takashi Iwai352f7f92012-12-19 12:52:06 +01001498 name = get_line_out_pfx(spec, i, true, &index);
1499 }
1500
Takashi Iwai196c17662013-01-04 15:01:40 +01001501 path = snd_hda_get_path_from_idx(codec, spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001502 if (!path)
1503 continue;
1504 if (!name || !strcmp(name, "CLFE")) {
1505 /* Center/LFE */
1506 err = add_vol_ctl(codec, "Center", 0, 1, path);
1507 if (err < 0)
1508 return err;
1509 err = add_vol_ctl(codec, "LFE", 0, 2, path);
1510 if (err < 0)
1511 return err;
1512 err = add_sw_ctl(codec, "Center", 0, 1, path);
1513 if (err < 0)
1514 return err;
1515 err = add_sw_ctl(codec, "LFE", 0, 2, path);
1516 if (err < 0)
1517 return err;
1518 } else {
1519 err = add_stereo_vol(codec, name, index, path);
1520 if (err < 0)
1521 return err;
1522 err = add_stereo_sw(codec, name, index, path);
1523 if (err < 0)
1524 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 }
1526 }
1527 return 0;
1528}
1529
Takashi Iwaic2c80382013-01-07 10:33:57 +01001530static int create_extra_out(struct hda_codec *codec, int path_idx,
Takashi Iwai196c17662013-01-04 15:01:40 +01001531 const char *pfx, int cidx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532{
Takashi Iwai352f7f92012-12-19 12:52:06 +01001533 struct nid_path *path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 int err;
1535
Takashi Iwai196c17662013-01-04 15:01:40 +01001536 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001537 if (!path)
1538 return 0;
Takashi Iwaic2c80382013-01-07 10:33:57 +01001539 err = add_stereo_vol(codec, pfx, cidx, path);
1540 if (err < 0)
1541 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001542 err = add_stereo_sw(codec, pfx, cidx, path);
1543 if (err < 0)
1544 return err;
1545 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546}
1547
Takashi Iwai352f7f92012-12-19 12:52:06 +01001548/* add playback controls for speaker and HP outputs */
1549static int create_extra_outs(struct hda_codec *codec, int num_pins,
Takashi Iwai196c17662013-01-04 15:01:40 +01001550 const int *paths, const char *pfx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551{
Takashi Iwaic2c80382013-01-07 10:33:57 +01001552 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001553
1554 for (i = 0; i < num_pins; i++) {
Takashi Iwaic2c80382013-01-07 10:33:57 +01001555 const char *name;
1556 char tmp[44];
1557 int err, idx = 0;
1558
1559 if (num_pins == 2 && i == 1 && !strcmp(pfx, "Speaker"))
1560 name = "Bass Speaker";
1561 else if (num_pins >= 3) {
1562 snprintf(tmp, sizeof(tmp), "%s %s",
Takashi Iwai352f7f92012-12-19 12:52:06 +01001563 pfx, channel_name[i]);
Takashi Iwaic2c80382013-01-07 10:33:57 +01001564 name = tmp;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001565 } else {
Takashi Iwaic2c80382013-01-07 10:33:57 +01001566 name = pfx;
1567 idx = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 }
Takashi Iwaic2c80382013-01-07 10:33:57 +01001569 err = create_extra_out(codec, paths[i], name, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001570 if (err < 0)
1571 return err;
1572 }
1573 return 0;
1574}
Takashi Iwai97ec5582006-03-21 11:29:07 +01001575
Takashi Iwai352f7f92012-12-19 12:52:06 +01001576static int create_hp_out_ctls(struct hda_codec *codec)
1577{
1578 struct hda_gen_spec *spec = codec->spec;
1579 return create_extra_outs(codec, spec->autocfg.hp_outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001580 spec->hp_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001581 "Headphone");
1582}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583
Takashi Iwai352f7f92012-12-19 12:52:06 +01001584static int create_speaker_out_ctls(struct hda_codec *codec)
1585{
1586 struct hda_gen_spec *spec = codec->spec;
1587 return create_extra_outs(codec, spec->autocfg.speaker_outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01001588 spec->speaker_paths,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001589 "Speaker");
1590}
1591
1592/*
Takashi Iwai38cf6f12012-12-21 14:09:42 +01001593 * independent HP controls
1594 */
1595
1596static int indep_hp_info(struct snd_kcontrol *kcontrol,
1597 struct snd_ctl_elem_info *uinfo)
1598{
1599 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
1600}
1601
1602static int indep_hp_get(struct snd_kcontrol *kcontrol,
1603 struct snd_ctl_elem_value *ucontrol)
1604{
1605 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1606 struct hda_gen_spec *spec = codec->spec;
1607 ucontrol->value.enumerated.item[0] = spec->indep_hp_enabled;
1608 return 0;
1609}
1610
1611static int indep_hp_put(struct snd_kcontrol *kcontrol,
1612 struct snd_ctl_elem_value *ucontrol)
1613{
1614 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1615 struct hda_gen_spec *spec = codec->spec;
1616 unsigned int select = ucontrol->value.enumerated.item[0];
1617 int ret = 0;
1618
1619 mutex_lock(&spec->pcm_mutex);
1620 if (spec->active_streams) {
1621 ret = -EBUSY;
1622 goto unlock;
1623 }
1624
1625 if (spec->indep_hp_enabled != select) {
1626 spec->indep_hp_enabled = select;
1627 if (spec->indep_hp_enabled)
1628 spec->multiout.hp_out_nid[0] = 0;
1629 else
1630 spec->multiout.hp_out_nid[0] = spec->alt_dac_nid;
1631 ret = 1;
1632 }
1633 unlock:
1634 mutex_unlock(&spec->pcm_mutex);
1635 return ret;
1636}
1637
1638static const struct snd_kcontrol_new indep_hp_ctl = {
1639 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1640 .name = "Independent HP",
1641 .info = indep_hp_info,
1642 .get = indep_hp_get,
1643 .put = indep_hp_put,
1644};
1645
1646
1647static int create_indep_hp_ctls(struct hda_codec *codec)
1648{
1649 struct hda_gen_spec *spec = codec->spec;
1650
1651 if (!spec->indep_hp)
1652 return 0;
1653 if (!spec->multiout.hp_out_nid[0]) {
1654 spec->indep_hp = 0;
1655 return 0;
1656 }
1657
1658 spec->indep_hp_enabled = false;
1659 spec->alt_dac_nid = spec->multiout.hp_out_nid[0];
1660 if (!snd_hda_gen_add_kctl(spec, NULL, &indep_hp_ctl))
1661 return -ENOMEM;
1662 return 0;
1663}
1664
1665/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01001666 * channel mode enum control
1667 */
1668
1669static int ch_mode_info(struct snd_kcontrol *kcontrol,
1670 struct snd_ctl_elem_info *uinfo)
1671{
1672 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1673 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia07a9492013-01-07 16:44:06 +01001674 int chs;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001675
1676 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1677 uinfo->count = 1;
1678 uinfo->value.enumerated.items = spec->multi_ios + 1;
1679 if (uinfo->value.enumerated.item > spec->multi_ios)
1680 uinfo->value.enumerated.item = spec->multi_ios;
Takashi Iwaia07a9492013-01-07 16:44:06 +01001681 chs = uinfo->value.enumerated.item * 2 + spec->min_channel_count;
1682 sprintf(uinfo->value.enumerated.name, "%dch", chs);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001683 return 0;
1684}
1685
1686static int ch_mode_get(struct snd_kcontrol *kcontrol,
1687 struct snd_ctl_elem_value *ucontrol)
1688{
1689 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1690 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaia07a9492013-01-07 16:44:06 +01001691 ucontrol->value.enumerated.item[0] =
1692 (spec->ext_channel_count - spec->min_channel_count) / 2;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001693 return 0;
1694}
1695
Takashi Iwai196c17662013-01-04 15:01:40 +01001696static inline struct nid_path *
1697get_multiio_path(struct hda_codec *codec, int idx)
1698{
1699 struct hda_gen_spec *spec = codec->spec;
1700 return snd_hda_get_path_from_idx(codec,
1701 spec->out_paths[spec->autocfg.line_outs + idx]);
1702}
1703
Takashi Iwai352f7f92012-12-19 12:52:06 +01001704static int set_multi_io(struct hda_codec *codec, int idx, bool output)
1705{
1706 struct hda_gen_spec *spec = codec->spec;
1707 hda_nid_t nid = spec->multi_io[idx].pin;
1708 struct nid_path *path;
1709
Takashi Iwai196c17662013-01-04 15:01:40 +01001710 path = get_multiio_path(codec, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001711 if (!path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 return -EINVAL;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001713
1714 if (path->active == output)
1715 return 0;
1716
1717 if (output) {
1718 snd_hda_set_pin_ctl_cache(codec, nid, PIN_OUT);
1719 snd_hda_activate_path(codec, path, true, true);
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01001720 set_pin_eapd(codec, nid, true);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001721 } else {
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01001722 set_pin_eapd(codec, nid, false);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001723 snd_hda_activate_path(codec, path, false, true);
1724 snd_hda_set_pin_ctl_cache(codec, nid,
1725 spec->multi_io[idx].ctl_in);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001727 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728}
1729
Takashi Iwai352f7f92012-12-19 12:52:06 +01001730static int ch_mode_put(struct snd_kcontrol *kcontrol,
1731 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732{
Takashi Iwai352f7f92012-12-19 12:52:06 +01001733 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1734 struct hda_gen_spec *spec = codec->spec;
1735 int i, ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736
Takashi Iwai352f7f92012-12-19 12:52:06 +01001737 ch = ucontrol->value.enumerated.item[0];
1738 if (ch < 0 || ch > spec->multi_ios)
1739 return -EINVAL;
Takashi Iwaia07a9492013-01-07 16:44:06 +01001740 if (ch == (spec->ext_channel_count - spec->min_channel_count) / 2)
Takashi Iwai352f7f92012-12-19 12:52:06 +01001741 return 0;
Takashi Iwaia07a9492013-01-07 16:44:06 +01001742 spec->ext_channel_count = ch * 2 + spec->min_channel_count;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001743 for (i = 0; i < spec->multi_ios; i++)
1744 set_multi_io(codec, i, i < ch);
1745 spec->multiout.max_channels = max(spec->ext_channel_count,
1746 spec->const_channel_count);
1747 if (spec->need_dac_fix)
1748 spec->multiout.num_dacs = spec->multiout.max_channels / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 return 1;
1750}
1751
Takashi Iwai352f7f92012-12-19 12:52:06 +01001752static const struct snd_kcontrol_new channel_mode_enum = {
1753 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1754 .name = "Channel Mode",
1755 .info = ch_mode_info,
1756 .get = ch_mode_get,
1757 .put = ch_mode_put,
1758};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759
Takashi Iwai352f7f92012-12-19 12:52:06 +01001760static int create_multi_channel_mode(struct hda_codec *codec)
1761{
1762 struct hda_gen_spec *spec = codec->spec;
1763
1764 if (spec->multi_ios > 0) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01001765 if (!snd_hda_gen_add_kctl(spec, NULL, &channel_mode_enum))
Takashi Iwai352f7f92012-12-19 12:52:06 +01001766 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 return 0;
1769}
1770
Takashi Iwai352f7f92012-12-19 12:52:06 +01001771/*
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01001772 * aamix loopback enable/disable switch
1773 */
1774
1775#define loopback_mixing_info indep_hp_info
1776
1777static int loopback_mixing_get(struct snd_kcontrol *kcontrol,
1778 struct snd_ctl_elem_value *ucontrol)
1779{
1780 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1781 struct hda_gen_spec *spec = codec->spec;
1782 ucontrol->value.enumerated.item[0] = spec->aamix_mode;
1783 return 0;
1784}
1785
1786static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
1787 int nomix_path_idx, int mix_path_idx)
1788{
1789 struct nid_path *nomix_path, *mix_path;
1790
1791 nomix_path = snd_hda_get_path_from_idx(codec, nomix_path_idx);
1792 mix_path = snd_hda_get_path_from_idx(codec, mix_path_idx);
1793 if (!nomix_path || !mix_path)
1794 return;
1795 if (do_mix) {
1796 snd_hda_activate_path(codec, nomix_path, false, true);
1797 snd_hda_activate_path(codec, mix_path, true, true);
1798 } else {
1799 snd_hda_activate_path(codec, mix_path, false, true);
1800 snd_hda_activate_path(codec, nomix_path, true, true);
1801 }
1802}
1803
1804static int loopback_mixing_put(struct snd_kcontrol *kcontrol,
1805 struct snd_ctl_elem_value *ucontrol)
1806{
1807 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1808 struct hda_gen_spec *spec = codec->spec;
1809 unsigned int val = ucontrol->value.enumerated.item[0];
1810
1811 if (val == spec->aamix_mode)
1812 return 0;
1813 spec->aamix_mode = val;
1814 update_aamix_paths(codec, val, spec->out_paths[0],
1815 spec->aamix_out_paths[0]);
1816 update_aamix_paths(codec, val, spec->hp_paths[0],
1817 spec->aamix_out_paths[1]);
1818 update_aamix_paths(codec, val, spec->speaker_paths[0],
1819 spec->aamix_out_paths[2]);
1820 return 1;
1821}
1822
1823static const struct snd_kcontrol_new loopback_mixing_enum = {
1824 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1825 .name = "Loopback Mixing",
1826 .info = loopback_mixing_info,
1827 .get = loopback_mixing_get,
1828 .put = loopback_mixing_put,
1829};
1830
1831static int create_loopback_mixing_ctl(struct hda_codec *codec)
1832{
1833 struct hda_gen_spec *spec = codec->spec;
1834
1835 if (!spec->mixer_nid)
1836 return 0;
1837 if (!(spec->aamix_out_paths[0] || spec->aamix_out_paths[1] ||
1838 spec->aamix_out_paths[2]))
1839 return 0;
1840 if (!snd_hda_gen_add_kctl(spec, NULL, &loopback_mixing_enum))
1841 return -ENOMEM;
1842 return 0;
1843}
1844
1845/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01001846 * shared headphone/mic handling
1847 */
Takashi Iwaicb53c622007-08-10 17:21:45 +02001848
Takashi Iwai352f7f92012-12-19 12:52:06 +01001849static void call_update_outputs(struct hda_codec *codec);
1850
1851/* for shared I/O, change the pin-control accordingly */
1852static void update_shared_mic_hp(struct hda_codec *codec, bool set_as_mic)
1853{
1854 struct hda_gen_spec *spec = codec->spec;
1855 unsigned int val;
1856 hda_nid_t pin = spec->autocfg.inputs[1].pin;
1857 /* NOTE: this assumes that there are only two inputs, the
1858 * first is the real internal mic and the second is HP/mic jack.
1859 */
1860
1861 val = snd_hda_get_default_vref(codec, pin);
1862
1863 /* This pin does not have vref caps - let's enable vref on pin 0x18
1864 instead, as suggested by Realtek */
1865 if (val == AC_PINCTL_VREF_HIZ && spec->shared_mic_vref_pin) {
1866 const hda_nid_t vref_pin = spec->shared_mic_vref_pin;
1867 unsigned int vref_val = snd_hda_get_default_vref(codec, vref_pin);
1868 if (vref_val != AC_PINCTL_VREF_HIZ)
Takashi Iwai7594aa32012-12-20 15:38:40 +01001869 snd_hda_set_pin_ctl_cache(codec, vref_pin,
1870 PIN_IN | (set_as_mic ? vref_val : 0));
Takashi Iwaicb53c622007-08-10 17:21:45 +02001871 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01001872
1873 val = set_as_mic ? val | PIN_IN : PIN_HP;
Takashi Iwai7594aa32012-12-20 15:38:40 +01001874 snd_hda_set_pin_ctl_cache(codec, pin, val);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001875
1876 spec->automute_speaker = !set_as_mic;
1877 call_update_outputs(codec);
1878}
1879
1880/* create a shared input with the headphone out */
1881static int create_shared_input(struct hda_codec *codec)
1882{
1883 struct hda_gen_spec *spec = codec->spec;
1884 struct auto_pin_cfg *cfg = &spec->autocfg;
1885 unsigned int defcfg;
1886 hda_nid_t nid;
1887
1888 /* only one internal input pin? */
1889 if (cfg->num_inputs != 1)
1890 return 0;
1891 defcfg = snd_hda_codec_get_pincfg(codec, cfg->inputs[0].pin);
1892 if (snd_hda_get_input_pin_attr(defcfg) != INPUT_PIN_ATTR_INT)
1893 return 0;
1894
1895 if (cfg->hp_outs == 1 && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
1896 nid = cfg->hp_pins[0]; /* OK, we have a single HP-out */
1897 else if (cfg->line_outs == 1 && cfg->line_out_type == AUTO_PIN_HP_OUT)
1898 nid = cfg->line_out_pins[0]; /* OK, we have a single line-out */
1899 else
1900 return 0; /* both not available */
1901
1902 if (!(snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_IN))
1903 return 0; /* no input */
1904
1905 cfg->inputs[1].pin = nid;
1906 cfg->inputs[1].type = AUTO_PIN_MIC;
1907 cfg->num_inputs = 2;
1908 spec->shared_mic_hp = 1;
1909 snd_printdd("hda-codec: Enable shared I/O jack on NID 0x%x\n", nid);
1910 return 0;
1911}
1912
1913
1914/*
1915 * Parse input paths
1916 */
1917
1918#ifdef CONFIG_PM
1919/* add the powersave loopback-list entry */
1920static void add_loopback_list(struct hda_gen_spec *spec, hda_nid_t mix, int idx)
1921{
1922 struct hda_amp_list *list;
1923
1924 if (spec->num_loopbacks >= ARRAY_SIZE(spec->loopback_list) - 1)
1925 return;
1926 list = spec->loopback_list + spec->num_loopbacks;
1927 list->nid = mix;
1928 list->dir = HDA_INPUT;
1929 list->idx = idx;
1930 spec->num_loopbacks++;
Takashi Iwaicb53c622007-08-10 17:21:45 +02001931 spec->loopback.amplist = spec->loopback_list;
1932}
1933#else
Takashi Iwai352f7f92012-12-19 12:52:06 +01001934#define add_loopback_list(spec, mix, idx) /* NOP */
Takashi Iwaicb53c622007-08-10 17:21:45 +02001935#endif
1936
Takashi Iwai352f7f92012-12-19 12:52:06 +01001937/* create input playback/capture controls for the given pin */
Takashi Iwai196c17662013-01-04 15:01:40 +01001938static int new_analog_input(struct hda_codec *codec, int input_idx,
1939 hda_nid_t pin, const char *ctlname, int ctlidx,
Takashi Iwai352f7f92012-12-19 12:52:06 +01001940 hda_nid_t mix_nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941{
Takashi Iwai352f7f92012-12-19 12:52:06 +01001942 struct hda_gen_spec *spec = codec->spec;
1943 struct nid_path *path;
1944 unsigned int val;
1945 int err, idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946
Takashi Iwai352f7f92012-12-19 12:52:06 +01001947 if (!nid_has_volume(codec, mix_nid, HDA_INPUT) &&
1948 !nid_has_mute(codec, mix_nid, HDA_INPUT))
1949 return 0; /* no need for analog loopback */
1950
Takashi Iwai3ca529d2013-01-07 17:25:08 +01001951 path = snd_hda_add_new_path(codec, pin, mix_nid, 0);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001952 if (!path)
1953 return -EINVAL;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01001954 print_nid_path("loopback", path);
Takashi Iwai196c17662013-01-04 15:01:40 +01001955 spec->loopback_paths[input_idx] = snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01001956
1957 idx = path->idx[path->depth - 1];
1958 if (nid_has_volume(codec, mix_nid, HDA_INPUT)) {
1959 val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
1960 err = __add_pb_vol_ctrl(spec, HDA_CTL_WIDGET_VOL, ctlname, ctlidx, val);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001961 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001963 path->ctls[NID_PATH_VOL_CTL] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 }
1965
Takashi Iwai352f7f92012-12-19 12:52:06 +01001966 if (nid_has_mute(codec, mix_nid, HDA_INPUT)) {
1967 val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
1968 err = __add_pb_sw_ctrl(spec, HDA_CTL_WIDGET_MUTE, ctlname, ctlidx, val);
Takashi Iwaid13bd412008-07-30 15:01:45 +02001969 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01001971 path->ctls[NID_PATH_MUTE_CTL] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 }
1973
Takashi Iwai352f7f92012-12-19 12:52:06 +01001974 path->active = true;
1975 add_loopback_list(spec, mix_nid, idx);
1976 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977}
1978
Takashi Iwai352f7f92012-12-19 12:52:06 +01001979static int is_input_pin(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980{
Takashi Iwai352f7f92012-12-19 12:52:06 +01001981 unsigned int pincap = snd_hda_query_pin_caps(codec, nid);
1982 return (pincap & AC_PINCAP_IN) != 0;
1983}
1984
1985/* Parse the codec tree and retrieve ADCs */
1986static int fill_adc_nids(struct hda_codec *codec)
1987{
1988 struct hda_gen_spec *spec = codec->spec;
1989 hda_nid_t nid;
1990 hda_nid_t *adc_nids = spec->adc_nids;
1991 int max_nums = ARRAY_SIZE(spec->adc_nids);
1992 int i, nums = 0;
1993
1994 nid = codec->start_nid;
1995 for (i = 0; i < codec->num_nodes; i++, nid++) {
1996 unsigned int caps = get_wcaps(codec, nid);
1997 int type = get_wcaps_type(caps);
1998
1999 if (type != AC_WID_AUD_IN || (caps & AC_WCAP_DIGITAL))
2000 continue;
2001 adc_nids[nums] = nid;
2002 if (++nums >= max_nums)
2003 break;
2004 }
2005 spec->num_adc_nids = nums;
2006 return nums;
2007}
2008
2009/* filter out invalid adc_nids that don't give all active input pins;
2010 * if needed, check whether dynamic ADC-switching is available
2011 */
2012static int check_dyn_adc_switch(struct hda_codec *codec)
2013{
2014 struct hda_gen_spec *spec = codec->spec;
2015 struct hda_input_mux *imux = &spec->input_mux;
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002016 unsigned int ok_bits;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002017 int i, n, nums;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002018
2019 again:
2020 nums = 0;
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002021 ok_bits = 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002022 for (n = 0; n < spec->num_adc_nids; n++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002023 for (i = 0; i < imux->num_items; i++) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002024 if (!spec->input_paths[i][n])
Takashi Iwai352f7f92012-12-19 12:52:06 +01002025 break;
2026 }
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002027 if (i >= imux->num_items) {
2028 ok_bits |= (1 << n);
2029 nums++;
2030 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002031 }
2032
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002033 if (!ok_bits) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002034 if (spec->shared_mic_hp) {
2035 spec->shared_mic_hp = 0;
2036 imux->num_items = 1;
2037 goto again;
2038 }
2039
2040 /* check whether ADC-switch is possible */
2041 for (i = 0; i < imux->num_items; i++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002042 for (n = 0; n < spec->num_adc_nids; n++) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002043 if (spec->input_paths[i][n]) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002044 spec->dyn_adc_idx[i] = n;
2045 break;
2046 }
2047 }
2048 }
2049
2050 snd_printdd("hda-codec: enabling ADC switching\n");
2051 spec->dyn_adc_switch = 1;
2052 } else if (nums != spec->num_adc_nids) {
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002053 /* shrink the invalid adcs and input paths */
2054 nums = 0;
2055 for (n = 0; n < spec->num_adc_nids; n++) {
2056 if (!(ok_bits & (1 << n)))
2057 continue;
2058 if (n != nums) {
2059 spec->adc_nids[nums] = spec->adc_nids[n];
Takashi Iwai980428c2013-01-09 09:28:20 +01002060 for (i = 0; i < imux->num_items; i++) {
2061 invalidate_nid_path(codec,
2062 spec->input_paths[i][nums]);
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002063 spec->input_paths[i][nums] =
2064 spec->input_paths[i][n];
Takashi Iwai980428c2013-01-09 09:28:20 +01002065 }
Takashi Iwai3a65bcd2013-01-09 09:06:18 +01002066 }
2067 nums++;
2068 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002069 spec->num_adc_nids = nums;
2070 }
2071
2072 if (imux->num_items == 1 || spec->shared_mic_hp) {
2073 snd_printdd("hda-codec: reducing to a single ADC\n");
2074 spec->num_adc_nids = 1; /* reduce to a single ADC */
2075 }
2076
2077 /* single index for individual volumes ctls */
2078 if (!spec->dyn_adc_switch && spec->multi_cap_vol)
2079 spec->num_adc_nids = 1;
2080
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 return 0;
2082}
2083
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002084/* parse capture source paths from the given pin and create imux items */
2085static int parse_capture_source(struct hda_codec *codec, hda_nid_t pin,
2086 int num_adcs, const char *label, int anchor)
2087{
2088 struct hda_gen_spec *spec = codec->spec;
2089 struct hda_input_mux *imux = &spec->input_mux;
2090 int imux_idx = imux->num_items;
2091 bool imux_added = false;
2092 int c;
2093
2094 for (c = 0; c < num_adcs; c++) {
2095 struct nid_path *path;
2096 hda_nid_t adc = spec->adc_nids[c];
2097
2098 if (!is_reachable_path(codec, pin, adc))
2099 continue;
2100 path = snd_hda_add_new_path(codec, pin, adc, anchor);
2101 if (!path)
2102 continue;
2103 print_nid_path("input", path);
2104 spec->input_paths[imux_idx][c] =
2105 snd_hda_get_path_idx(codec, path);
2106
2107 if (!imux_added) {
2108 spec->imux_pins[imux->num_items] = pin;
2109 snd_hda_add_imux_item(imux, label,
2110 imux->num_items, NULL);
2111 imux_added = true;
2112 }
2113 }
2114
2115 return 0;
2116}
2117
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01002119 * create playback/capture controls for input pins
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01002121static int create_input_ctls(struct hda_codec *codec)
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02002122{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002123 struct hda_gen_spec *spec = codec->spec;
2124 const struct auto_pin_cfg *cfg = &spec->autocfg;
2125 hda_nid_t mixer = spec->mixer_nid;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002126 int num_adcs;
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002127 int i, err, type_idx = 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002128 const char *prev_label = NULL;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02002129
Takashi Iwai352f7f92012-12-19 12:52:06 +01002130 num_adcs = fill_adc_nids(codec);
2131 if (num_adcs < 0)
2132 return 0;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02002133
Takashi Iwai352f7f92012-12-19 12:52:06 +01002134 for (i = 0; i < cfg->num_inputs; i++) {
2135 hda_nid_t pin;
2136 const char *label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137
Takashi Iwai352f7f92012-12-19 12:52:06 +01002138 pin = cfg->inputs[i].pin;
2139 if (!is_input_pin(codec, pin))
2140 continue;
2141
2142 label = hda_get_autocfg_input_label(codec, cfg, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002143 if (prev_label && !strcmp(label, prev_label))
2144 type_idx++;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +02002145 else
Takashi Iwai352f7f92012-12-19 12:52:06 +01002146 type_idx = 0;
2147 prev_label = label;
2148
2149 if (mixer) {
2150 if (is_reachable_path(codec, pin, mixer)) {
Takashi Iwai196c17662013-01-04 15:01:40 +01002151 err = new_analog_input(codec, i, pin,
Takashi Iwai352f7f92012-12-19 12:52:06 +01002152 label, type_idx, mixer);
2153 if (err < 0)
2154 return err;
2155 }
2156 }
2157
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002158 err = parse_capture_source(codec, pin, num_adcs, label, -mixer);
2159 if (err < 0)
2160 return err;
2161 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01002162
Takashi Iwaif3fc0b02013-01-09 09:14:23 +01002163 if (mixer && spec->add_stereo_mix_input) {
2164 err = parse_capture_source(codec, mixer, num_adcs,
2165 "Stereo Mix", 0);
2166 if (err < 0)
2167 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002168 }
2169
2170 return 0;
2171}
2172
2173
2174/*
2175 * input source mux
2176 */
2177
Takashi Iwaic697b712013-01-07 17:09:26 +01002178/* get the input path specified by the given adc and imux indices */
2179static struct nid_path *get_input_path(struct hda_codec *codec, int adc_idx, int imux_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002180{
2181 struct hda_gen_spec *spec = codec->spec;
2182 if (spec->dyn_adc_switch)
2183 adc_idx = spec->dyn_adc_idx[imux_idx];
Takashi Iwaic697b712013-01-07 17:09:26 +01002184 return snd_hda_get_path_from_idx(codec, spec->input_paths[imux_idx][adc_idx]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002185}
2186
2187static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
2188 unsigned int idx);
2189
2190static int mux_enum_info(struct snd_kcontrol *kcontrol,
2191 struct snd_ctl_elem_info *uinfo)
2192{
2193 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2194 struct hda_gen_spec *spec = codec->spec;
2195 return snd_hda_input_mux_info(&spec->input_mux, uinfo);
2196}
2197
2198static int mux_enum_get(struct snd_kcontrol *kcontrol,
2199 struct snd_ctl_elem_value *ucontrol)
2200{
2201 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2202 struct hda_gen_spec *spec = codec->spec;
2203 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
2204
2205 ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
2206 return 0;
2207}
2208
2209static int mux_enum_put(struct snd_kcontrol *kcontrol,
2210 struct snd_ctl_elem_value *ucontrol)
2211{
2212 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2213 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
2214 return mux_select(codec, adc_idx,
2215 ucontrol->value.enumerated.item[0]);
2216}
2217
Takashi Iwai352f7f92012-12-19 12:52:06 +01002218static const struct snd_kcontrol_new cap_src_temp = {
2219 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2220 .name = "Input Source",
2221 .info = mux_enum_info,
2222 .get = mux_enum_get,
2223 .put = mux_enum_put,
2224};
2225
Takashi Iwai47d46ab2012-12-20 11:48:54 +01002226/*
2227 * capture volume and capture switch ctls
2228 */
2229
Takashi Iwai352f7f92012-12-19 12:52:06 +01002230typedef int (*put_call_t)(struct snd_kcontrol *kcontrol,
2231 struct snd_ctl_elem_value *ucontrol);
2232
Takashi Iwai47d46ab2012-12-20 11:48:54 +01002233/* call the given amp update function for all amps in the imux list at once */
Takashi Iwai352f7f92012-12-19 12:52:06 +01002234static int cap_put_caller(struct snd_kcontrol *kcontrol,
2235 struct snd_ctl_elem_value *ucontrol,
2236 put_call_t func, int type)
2237{
2238 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2239 struct hda_gen_spec *spec = codec->spec;
2240 const struct hda_input_mux *imux;
2241 struct nid_path *path;
2242 int i, adc_idx, err = 0;
2243
2244 imux = &spec->input_mux;
2245 adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
2246 mutex_lock(&codec->control_mutex);
Takashi Iwai47d46ab2012-12-20 11:48:54 +01002247 /* we use the cache-only update at first since multiple input paths
2248 * may shared the same amp; by updating only caches, the redundant
2249 * writes to hardware can be reduced.
2250 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01002251 codec->cached_write = 1;
2252 for (i = 0; i < imux->num_items; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01002253 path = get_input_path(codec, adc_idx, i);
2254 if (!path || !path->ctls[type])
Takashi Iwai352f7f92012-12-19 12:52:06 +01002255 continue;
2256 kcontrol->private_value = path->ctls[type];
2257 err = func(kcontrol, ucontrol);
2258 if (err < 0)
2259 goto error;
2260 }
2261 error:
2262 codec->cached_write = 0;
2263 mutex_unlock(&codec->control_mutex);
Takashi Iwai47d46ab2012-12-20 11:48:54 +01002264 snd_hda_codec_flush_amp_cache(codec); /* flush the updates */
Takashi Iwai352f7f92012-12-19 12:52:06 +01002265 if (err >= 0 && spec->cap_sync_hook)
2266 spec->cap_sync_hook(codec);
2267 return err;
2268}
2269
2270/* capture volume ctl callbacks */
2271#define cap_vol_info snd_hda_mixer_amp_volume_info
2272#define cap_vol_get snd_hda_mixer_amp_volume_get
2273#define cap_vol_tlv snd_hda_mixer_amp_tlv
2274
2275static int cap_vol_put(struct snd_kcontrol *kcontrol,
2276 struct snd_ctl_elem_value *ucontrol)
2277{
2278 return cap_put_caller(kcontrol, ucontrol,
2279 snd_hda_mixer_amp_volume_put,
2280 NID_PATH_VOL_CTL);
2281}
2282
2283static const struct snd_kcontrol_new cap_vol_temp = {
2284 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2285 .name = "Capture Volume",
2286 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
2287 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
2288 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK),
2289 .info = cap_vol_info,
2290 .get = cap_vol_get,
2291 .put = cap_vol_put,
2292 .tlv = { .c = cap_vol_tlv },
2293};
2294
2295/* capture switch ctl callbacks */
2296#define cap_sw_info snd_ctl_boolean_stereo_info
2297#define cap_sw_get snd_hda_mixer_amp_switch_get
2298
2299static int cap_sw_put(struct snd_kcontrol *kcontrol,
2300 struct snd_ctl_elem_value *ucontrol)
2301{
2302 return cap_put_caller(kcontrol, ucontrol,
2303 snd_hda_mixer_amp_switch_put,
2304 NID_PATH_MUTE_CTL);
2305}
2306
2307static const struct snd_kcontrol_new cap_sw_temp = {
2308 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2309 .name = "Capture Switch",
2310 .info = cap_sw_info,
2311 .get = cap_sw_get,
2312 .put = cap_sw_put,
2313};
2314
2315static int parse_capvol_in_path(struct hda_codec *codec, struct nid_path *path)
2316{
2317 hda_nid_t nid;
2318 int i, depth;
2319
2320 path->ctls[NID_PATH_VOL_CTL] = path->ctls[NID_PATH_MUTE_CTL] = 0;
2321 for (depth = 0; depth < 3; depth++) {
2322 if (depth >= path->depth)
2323 return -EINVAL;
2324 i = path->depth - depth - 1;
2325 nid = path->path[i];
2326 if (!path->ctls[NID_PATH_VOL_CTL]) {
2327 if (nid_has_volume(codec, nid, HDA_OUTPUT))
2328 path->ctls[NID_PATH_VOL_CTL] =
2329 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
2330 else if (nid_has_volume(codec, nid, HDA_INPUT)) {
2331 int idx = path->idx[i];
2332 if (!depth && codec->single_adc_amp)
2333 idx = 0;
2334 path->ctls[NID_PATH_VOL_CTL] =
2335 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
2336 }
2337 }
2338 if (!path->ctls[NID_PATH_MUTE_CTL]) {
2339 if (nid_has_mute(codec, nid, HDA_OUTPUT))
2340 path->ctls[NID_PATH_MUTE_CTL] =
2341 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
2342 else if (nid_has_mute(codec, nid, HDA_INPUT)) {
2343 int idx = path->idx[i];
2344 if (!depth && codec->single_adc_amp)
2345 idx = 0;
2346 path->ctls[NID_PATH_MUTE_CTL] =
2347 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
2348 }
2349 }
Takashi Iwai97ec5582006-03-21 11:29:07 +01002350 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 return 0;
2352}
2353
Takashi Iwai352f7f92012-12-19 12:52:06 +01002354static bool is_inv_dmic_pin(struct hda_codec *codec, hda_nid_t nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002356 struct hda_gen_spec *spec = codec->spec;
2357 struct auto_pin_cfg *cfg = &spec->autocfg;
2358 unsigned int val;
2359 int i;
2360
2361 if (!spec->inv_dmic_split)
2362 return false;
2363 for (i = 0; i < cfg->num_inputs; i++) {
2364 if (cfg->inputs[i].pin != nid)
2365 continue;
2366 if (cfg->inputs[i].type != AUTO_PIN_MIC)
2367 return false;
2368 val = snd_hda_codec_get_pincfg(codec, nid);
2369 return snd_hda_get_input_pin_attr(val) == INPUT_PIN_ATTR_INT;
2370 }
2371 return false;
2372}
2373
2374static int add_single_cap_ctl(struct hda_codec *codec, const char *label,
2375 int idx, bool is_switch, unsigned int ctl,
2376 bool inv_dmic)
2377{
2378 struct hda_gen_spec *spec = codec->spec;
2379 char tmpname[44];
2380 int type = is_switch ? HDA_CTL_WIDGET_MUTE : HDA_CTL_WIDGET_VOL;
2381 const char *sfx = is_switch ? "Switch" : "Volume";
2382 unsigned int chs = inv_dmic ? 1 : 3;
2383 int err;
2384
2385 if (!ctl)
2386 return 0;
2387
2388 if (label)
2389 snprintf(tmpname, sizeof(tmpname),
2390 "%s Capture %s", label, sfx);
2391 else
2392 snprintf(tmpname, sizeof(tmpname),
2393 "Capture %s", sfx);
2394 err = add_control(spec, type, tmpname, idx,
2395 amp_val_replace_channels(ctl, chs));
2396 if (err < 0 || !inv_dmic)
2397 return err;
2398
2399 /* Make independent right kcontrol */
2400 if (label)
2401 snprintf(tmpname, sizeof(tmpname),
2402 "Inverted %s Capture %s", label, sfx);
2403 else
2404 snprintf(tmpname, sizeof(tmpname),
2405 "Inverted Capture %s", sfx);
2406 return add_control(spec, type, tmpname, idx,
2407 amp_val_replace_channels(ctl, 2));
2408}
2409
2410/* create single (and simple) capture volume and switch controls */
2411static int create_single_cap_vol_ctl(struct hda_codec *codec, int idx,
2412 unsigned int vol_ctl, unsigned int sw_ctl,
2413 bool inv_dmic)
2414{
2415 int err;
2416 err = add_single_cap_ctl(codec, NULL, idx, false, vol_ctl, inv_dmic);
2417 if (err < 0)
2418 return err;
2419 err = add_single_cap_ctl(codec, NULL, idx, true, sw_ctl, inv_dmic);
2420 if (err < 0)
2421 return err;
2422 return 0;
2423}
2424
2425/* create bound capture volume and switch controls */
2426static int create_bind_cap_vol_ctl(struct hda_codec *codec, int idx,
2427 unsigned int vol_ctl, unsigned int sw_ctl)
2428{
2429 struct hda_gen_spec *spec = codec->spec;
2430 struct snd_kcontrol_new *knew;
2431
2432 if (vol_ctl) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01002433 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_vol_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002434 if (!knew)
2435 return -ENOMEM;
2436 knew->index = idx;
2437 knew->private_value = vol_ctl;
2438 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
2439 }
2440 if (sw_ctl) {
Takashi Iwai12c93df2012-12-19 14:38:33 +01002441 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_sw_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002442 if (!knew)
2443 return -ENOMEM;
2444 knew->index = idx;
2445 knew->private_value = sw_ctl;
2446 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
2447 }
2448 return 0;
2449}
2450
2451/* return the vol ctl when used first in the imux list */
2452static unsigned int get_first_cap_ctl(struct hda_codec *codec, int idx, int type)
2453{
Takashi Iwai352f7f92012-12-19 12:52:06 +01002454 struct nid_path *path;
2455 unsigned int ctl;
2456 int i;
2457
Takashi Iwaic697b712013-01-07 17:09:26 +01002458 path = get_input_path(codec, 0, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002459 if (!path)
2460 return 0;
2461 ctl = path->ctls[type];
2462 if (!ctl)
2463 return 0;
2464 for (i = 0; i < idx - 1; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01002465 path = get_input_path(codec, 0, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002466 if (path && path->ctls[type] == ctl)
2467 return 0;
2468 }
2469 return ctl;
2470}
2471
2472/* create individual capture volume and switch controls per input */
2473static int create_multi_cap_vol_ctl(struct hda_codec *codec)
2474{
2475 struct hda_gen_spec *spec = codec->spec;
2476 struct hda_input_mux *imux = &spec->input_mux;
2477 int i, err, type, type_idx = 0;
2478 const char *prev_label = NULL;
2479
2480 for (i = 0; i < imux->num_items; i++) {
2481 const char *label;
2482 bool inv_dmic;
2483 label = hda_get_autocfg_input_label(codec, &spec->autocfg, i);
2484 if (prev_label && !strcmp(label, prev_label))
2485 type_idx++;
2486 else
2487 type_idx = 0;
2488 prev_label = label;
2489 inv_dmic = is_inv_dmic_pin(codec, spec->imux_pins[i]);
2490
2491 for (type = 0; type < 2; type++) {
2492 err = add_single_cap_ctl(codec, label, type_idx, type,
2493 get_first_cap_ctl(codec, i, type),
2494 inv_dmic);
2495 if (err < 0)
2496 return err;
2497 }
2498 }
2499 return 0;
2500}
2501
2502static int create_capture_mixers(struct hda_codec *codec)
2503{
2504 struct hda_gen_spec *spec = codec->spec;
2505 struct hda_input_mux *imux = &spec->input_mux;
2506 int i, n, nums, err;
2507
2508 if (spec->dyn_adc_switch)
2509 nums = 1;
2510 else
2511 nums = spec->num_adc_nids;
2512
2513 if (!spec->auto_mic && imux->num_items > 1) {
2514 struct snd_kcontrol_new *knew;
Takashi Iwai624d9142012-12-19 17:41:52 +01002515 const char *name;
2516 name = nums > 1 ? "Input Source" : "Capture Source";
2517 knew = snd_hda_gen_add_kctl(spec, name, &cap_src_temp);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002518 if (!knew)
2519 return -ENOMEM;
2520 knew->count = nums;
2521 }
2522
2523 for (n = 0; n < nums; n++) {
2524 bool multi = false;
2525 bool inv_dmic = false;
2526 int vol, sw;
2527
2528 vol = sw = 0;
2529 for (i = 0; i < imux->num_items; i++) {
2530 struct nid_path *path;
Takashi Iwaic697b712013-01-07 17:09:26 +01002531 path = get_input_path(codec, n, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002532 if (!path)
2533 continue;
2534 parse_capvol_in_path(codec, path);
2535 if (!vol)
2536 vol = path->ctls[NID_PATH_VOL_CTL];
2537 else if (vol != path->ctls[NID_PATH_VOL_CTL])
2538 multi = true;
2539 if (!sw)
2540 sw = path->ctls[NID_PATH_MUTE_CTL];
2541 else if (sw != path->ctls[NID_PATH_MUTE_CTL])
2542 multi = true;
2543 if (is_inv_dmic_pin(codec, spec->imux_pins[i]))
2544 inv_dmic = true;
2545 }
2546
2547 if (!multi)
2548 err = create_single_cap_vol_ctl(codec, n, vol, sw,
2549 inv_dmic);
2550 else if (!spec->multi_cap_vol)
2551 err = create_bind_cap_vol_ctl(codec, n, vol, sw);
2552 else
2553 err = create_multi_cap_vol_ctl(codec);
2554 if (err < 0)
2555 return err;
2556 }
2557
2558 return 0;
2559}
2560
2561/*
2562 * add mic boosts if needed
2563 */
2564static int parse_mic_boost(struct hda_codec *codec)
2565{
2566 struct hda_gen_spec *spec = codec->spec;
2567 struct auto_pin_cfg *cfg = &spec->autocfg;
Takashi Iwai071c73a2006-08-23 18:34:06 +02002568 int i, err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002569 int type_idx = 0;
2570 hda_nid_t nid;
2571 const char *prev_label = NULL;
2572
2573 for (i = 0; i < cfg->num_inputs; i++) {
2574 if (cfg->inputs[i].type > AUTO_PIN_MIC)
2575 break;
2576 nid = cfg->inputs[i].pin;
2577 if (get_wcaps(codec, nid) & AC_WCAP_IN_AMP) {
2578 const char *label;
Takashi Iwai5abd4882013-01-07 09:43:18 +01002579 char boost_label[44];
Takashi Iwai352f7f92012-12-19 12:52:06 +01002580 struct nid_path *path;
2581 unsigned int val;
2582
2583 label = hda_get_autocfg_input_label(codec, cfg, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002584 if (prev_label && !strcmp(label, prev_label))
2585 type_idx++;
2586 else
2587 type_idx = 0;
2588 prev_label = label;
2589
2590 snprintf(boost_label, sizeof(boost_label),
2591 "%s Boost Volume", label);
2592 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT);
2593 err = add_control(spec, HDA_CTL_WIDGET_VOL,
2594 boost_label, type_idx, val);
2595 if (err < 0)
2596 return err;
2597
2598 path = snd_hda_get_nid_path(codec, nid, 0);
2599 if (path)
2600 path->ctls[NID_PATH_BOOST_CTL] = val;
2601 }
2602 }
2603 return 0;
2604}
2605
2606/*
2607 * parse digital I/Os and set up NIDs in BIOS auto-parse mode
2608 */
2609static void parse_digital(struct hda_codec *codec)
2610{
2611 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01002612 struct nid_path *path;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002613 int i, nums;
2614 hda_nid_t dig_nid;
2615
2616 /* support multiple SPDIFs; the secondary is set up as a slave */
2617 nums = 0;
2618 for (i = 0; i < spec->autocfg.dig_outs; i++) {
2619 hda_nid_t pin = spec->autocfg.dig_out_pins[i];
2620 dig_nid = look_for_dac(codec, pin, true);
2621 if (!dig_nid)
2622 continue;
Takashi Iwai3ca529d2013-01-07 17:25:08 +01002623 path = snd_hda_add_new_path(codec, dig_nid, pin, 0);
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01002624 if (!path)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002625 continue;
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01002626 print_nid_path("digout", path);
Takashi Iwaie1284af2013-01-03 16:33:02 +01002627 path->active = true;
Takashi Iwai196c17662013-01-04 15:01:40 +01002628 spec->digout_paths[i] = snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002629 if (!nums) {
2630 spec->multiout.dig_out_nid = dig_nid;
2631 spec->dig_out_type = spec->autocfg.dig_out_type[0];
2632 } else {
2633 spec->multiout.slave_dig_outs = spec->slave_dig_outs;
2634 if (nums >= ARRAY_SIZE(spec->slave_dig_outs) - 1)
2635 break;
2636 spec->slave_dig_outs[nums - 1] = dig_nid;
2637 }
2638 nums++;
2639 }
2640
2641 if (spec->autocfg.dig_in_pin) {
2642 dig_nid = codec->start_nid;
2643 for (i = 0; i < codec->num_nodes; i++, dig_nid++) {
Takashi Iwai352f7f92012-12-19 12:52:06 +01002644 unsigned int wcaps = get_wcaps(codec, dig_nid);
2645 if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
2646 continue;
2647 if (!(wcaps & AC_WCAP_DIGITAL))
2648 continue;
2649 path = snd_hda_add_new_path(codec,
2650 spec->autocfg.dig_in_pin,
Takashi Iwai3ca529d2013-01-07 17:25:08 +01002651 dig_nid, 0);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002652 if (path) {
Takashi Iwai0c8c0f52012-12-20 17:54:22 +01002653 print_nid_path("digin", path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002654 path->active = true;
2655 spec->dig_in_nid = dig_nid;
Takashi Iwai2430d7b2013-01-04 15:09:42 +01002656 spec->digin_path = snd_hda_get_path_idx(codec, path);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002657 break;
2658 }
2659 }
2660 }
2661}
2662
2663
2664/*
2665 * input MUX handling
2666 */
2667
2668static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur);
2669
2670/* select the given imux item; either unmute exclusively or select the route */
2671static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
2672 unsigned int idx)
2673{
2674 struct hda_gen_spec *spec = codec->spec;
2675 const struct hda_input_mux *imux;
2676 struct nid_path *path;
2677
2678 imux = &spec->input_mux;
2679 if (!imux->num_items)
2680 return 0;
2681
2682 if (idx >= imux->num_items)
2683 idx = imux->num_items - 1;
2684 if (spec->cur_mux[adc_idx] == idx)
2685 return 0;
2686
Takashi Iwaic697b712013-01-07 17:09:26 +01002687 path = get_input_path(codec, adc_idx, spec->cur_mux[adc_idx]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002688 if (!path)
2689 return 0;
2690 if (path->active)
2691 snd_hda_activate_path(codec, path, false, false);
2692
2693 spec->cur_mux[adc_idx] = idx;
2694
2695 if (spec->shared_mic_hp)
2696 update_shared_mic_hp(codec, spec->cur_mux[adc_idx]);
2697
2698 if (spec->dyn_adc_switch)
2699 dyn_adc_pcm_resetup(codec, idx);
2700
Takashi Iwaic697b712013-01-07 17:09:26 +01002701 path = get_input_path(codec, adc_idx, idx);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002702 if (!path)
2703 return 0;
2704 if (path->active)
2705 return 0;
2706 snd_hda_activate_path(codec, path, true, false);
2707 if (spec->cap_sync_hook)
2708 spec->cap_sync_hook(codec);
2709 return 1;
2710}
2711
2712
2713/*
2714 * Jack detections for HP auto-mute and mic-switch
2715 */
2716
2717/* check each pin in the given array; returns true if any of them is plugged */
2718static bool detect_jacks(struct hda_codec *codec, int num_pins, hda_nid_t *pins)
2719{
2720 int i, present = 0;
2721
2722 for (i = 0; i < num_pins; i++) {
2723 hda_nid_t nid = pins[i];
2724 if (!nid)
2725 break;
2726 present |= snd_hda_jack_detect(codec, nid);
2727 }
2728 return present;
2729}
2730
2731/* standard HP/line-out auto-mute helper */
2732static void do_automute(struct hda_codec *codec, int num_pins, hda_nid_t *pins,
2733 bool mute, bool hp_out)
2734{
2735 struct hda_gen_spec *spec = codec->spec;
2736 unsigned int pin_bits = mute ? 0 : (hp_out ? PIN_HP : PIN_OUT);
2737 int i;
2738
2739 for (i = 0; i < num_pins; i++) {
2740 hda_nid_t nid = pins[i];
2741 unsigned int val;
2742 if (!nid)
2743 break;
2744 /* don't reset VREF value in case it's controlling
2745 * the amp (see alc861_fixup_asus_amp_vref_0f())
2746 */
2747 if (spec->keep_vref_in_automute) {
2748 val = snd_hda_codec_read(codec, nid, 0,
2749 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
2750 val &= ~PIN_HP;
2751 } else
2752 val = 0;
2753 val |= pin_bits;
Takashi Iwai7594aa32012-12-20 15:38:40 +01002754 snd_hda_set_pin_ctl_cache(codec, nid, val);
Takashi Iwaid5a9f1b2012-12-20 15:36:30 +01002755 set_pin_eapd(codec, nid, !mute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002756 }
2757}
2758
2759/* Toggle outputs muting */
Takashi Iwai5d550e12012-12-19 15:16:44 +01002760void snd_hda_gen_update_outputs(struct hda_codec *codec)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002761{
2762 struct hda_gen_spec *spec = codec->spec;
2763 int on;
2764
2765 /* Control HP pins/amps depending on master_mute state;
2766 * in general, HP pins/amps control should be enabled in all cases,
2767 * but currently set only for master_mute, just to be safe
2768 */
2769 if (!spec->shared_mic_hp) /* don't change HP-pin when shared with mic */
2770 do_automute(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
2771 spec->autocfg.hp_pins, spec->master_mute, true);
2772
2773 if (!spec->automute_speaker)
2774 on = 0;
2775 else
2776 on = spec->hp_jack_present | spec->line_jack_present;
2777 on |= spec->master_mute;
2778 do_automute(codec, ARRAY_SIZE(spec->autocfg.speaker_pins),
2779 spec->autocfg.speaker_pins, on, false);
2780
2781 /* toggle line-out mutes if needed, too */
2782 /* if LO is a copy of either HP or Speaker, don't need to handle it */
2783 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0] ||
2784 spec->autocfg.line_out_pins[0] == spec->autocfg.speaker_pins[0])
2785 return;
2786 if (!spec->automute_lo)
2787 on = 0;
2788 else
2789 on = spec->hp_jack_present;
2790 on |= spec->master_mute;
2791 do_automute(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
2792 spec->autocfg.line_out_pins, on, false);
2793}
Takashi Iwai5d550e12012-12-19 15:16:44 +01002794EXPORT_SYMBOL_HDA(snd_hda_gen_update_outputs);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002795
2796static void call_update_outputs(struct hda_codec *codec)
2797{
2798 struct hda_gen_spec *spec = codec->spec;
2799 if (spec->automute_hook)
2800 spec->automute_hook(codec);
2801 else
Takashi Iwai5d550e12012-12-19 15:16:44 +01002802 snd_hda_gen_update_outputs(codec);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002803}
2804
2805/* standard HP-automute helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01002806void snd_hda_gen_hp_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002807{
2808 struct hda_gen_spec *spec = codec->spec;
2809
2810 spec->hp_jack_present =
2811 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
2812 spec->autocfg.hp_pins);
2813 if (!spec->detect_hp || (!spec->automute_speaker && !spec->automute_lo))
2814 return;
2815 call_update_outputs(codec);
2816}
Takashi Iwai5d550e12012-12-19 15:16:44 +01002817EXPORT_SYMBOL_HDA(snd_hda_gen_hp_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002818
2819/* standard line-out-automute helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01002820void snd_hda_gen_line_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002821{
2822 struct hda_gen_spec *spec = codec->spec;
2823
2824 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
2825 return;
2826 /* check LO jack only when it's different from HP */
2827 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0])
2828 return;
2829
2830 spec->line_jack_present =
2831 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
2832 spec->autocfg.line_out_pins);
2833 if (!spec->automute_speaker || !spec->detect_lo)
2834 return;
2835 call_update_outputs(codec);
2836}
Takashi Iwai5d550e12012-12-19 15:16:44 +01002837EXPORT_SYMBOL_HDA(snd_hda_gen_line_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002838
2839/* standard mic auto-switch helper */
Takashi Iwai5d550e12012-12-19 15:16:44 +01002840void snd_hda_gen_mic_autoswitch(struct hda_codec *codec, struct hda_jack_tbl *jack)
Takashi Iwai352f7f92012-12-19 12:52:06 +01002841{
2842 struct hda_gen_spec *spec = codec->spec;
2843 int i;
2844
2845 if (!spec->auto_mic)
2846 return;
2847
2848 for (i = spec->am_num_entries - 1; i > 0; i--) {
2849 if (snd_hda_jack_detect(codec, spec->am_entry[i].pin)) {
2850 mux_select(codec, 0, spec->am_entry[i].idx);
2851 return;
2852 }
2853 }
2854 mux_select(codec, 0, spec->am_entry[0].idx);
2855}
Takashi Iwai5d550e12012-12-19 15:16:44 +01002856EXPORT_SYMBOL_HDA(snd_hda_gen_mic_autoswitch);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002857
2858/*
2859 * Auto-Mute mode mixer enum support
2860 */
2861static int automute_mode_info(struct snd_kcontrol *kcontrol,
2862 struct snd_ctl_elem_info *uinfo)
2863{
2864 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2865 struct hda_gen_spec *spec = codec->spec;
2866 static const char * const texts3[] = {
2867 "Disabled", "Speaker Only", "Line Out+Speaker"
Takashi Iwai071c73a2006-08-23 18:34:06 +02002868 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869
Takashi Iwai352f7f92012-12-19 12:52:06 +01002870 if (spec->automute_speaker_possible && spec->automute_lo_possible)
2871 return snd_hda_enum_helper_info(kcontrol, uinfo, 3, texts3);
2872 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
2873}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874
Takashi Iwai352f7f92012-12-19 12:52:06 +01002875static int automute_mode_get(struct snd_kcontrol *kcontrol,
2876 struct snd_ctl_elem_value *ucontrol)
2877{
2878 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2879 struct hda_gen_spec *spec = codec->spec;
2880 unsigned int val = 0;
2881 if (spec->automute_speaker)
2882 val++;
2883 if (spec->automute_lo)
2884 val++;
Takashi Iwai071c73a2006-08-23 18:34:06 +02002885
Takashi Iwai352f7f92012-12-19 12:52:06 +01002886 ucontrol->value.enumerated.item[0] = val;
2887 return 0;
2888}
2889
2890static int automute_mode_put(struct snd_kcontrol *kcontrol,
2891 struct snd_ctl_elem_value *ucontrol)
2892{
2893 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2894 struct hda_gen_spec *spec = codec->spec;
2895
2896 switch (ucontrol->value.enumerated.item[0]) {
2897 case 0:
2898 if (!spec->automute_speaker && !spec->automute_lo)
2899 return 0;
2900 spec->automute_speaker = 0;
2901 spec->automute_lo = 0;
2902 break;
2903 case 1:
2904 if (spec->automute_speaker_possible) {
2905 if (!spec->automute_lo && spec->automute_speaker)
2906 return 0;
2907 spec->automute_speaker = 1;
2908 spec->automute_lo = 0;
2909 } else if (spec->automute_lo_possible) {
2910 if (spec->automute_lo)
2911 return 0;
2912 spec->automute_lo = 1;
2913 } else
2914 return -EINVAL;
2915 break;
2916 case 2:
2917 if (!spec->automute_lo_possible || !spec->automute_speaker_possible)
2918 return -EINVAL;
2919 if (spec->automute_speaker && spec->automute_lo)
2920 return 0;
2921 spec->automute_speaker = 1;
2922 spec->automute_lo = 1;
2923 break;
2924 default:
2925 return -EINVAL;
2926 }
2927 call_update_outputs(codec);
2928 return 1;
2929}
2930
2931static const struct snd_kcontrol_new automute_mode_enum = {
2932 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2933 .name = "Auto-Mute Mode",
2934 .info = automute_mode_info,
2935 .get = automute_mode_get,
2936 .put = automute_mode_put,
2937};
2938
2939static int add_automute_mode_enum(struct hda_codec *codec)
2940{
2941 struct hda_gen_spec *spec = codec->spec;
2942
Takashi Iwai12c93df2012-12-19 14:38:33 +01002943 if (!snd_hda_gen_add_kctl(spec, NULL, &automute_mode_enum))
Takashi Iwai352f7f92012-12-19 12:52:06 +01002944 return -ENOMEM;
2945 return 0;
2946}
2947
2948/*
2949 * Check the availability of HP/line-out auto-mute;
2950 * Set up appropriately if really supported
2951 */
2952static int check_auto_mute_availability(struct hda_codec *codec)
2953{
2954 struct hda_gen_spec *spec = codec->spec;
2955 struct auto_pin_cfg *cfg = &spec->autocfg;
2956 int present = 0;
2957 int i, err;
2958
2959 if (cfg->hp_pins[0])
2960 present++;
2961 if (cfg->line_out_pins[0])
2962 present++;
2963 if (cfg->speaker_pins[0])
2964 present++;
2965 if (present < 2) /* need two different output types */
Takashi Iwai071c73a2006-08-23 18:34:06 +02002966 return 0;
Takashi Iwai352f7f92012-12-19 12:52:06 +01002967
2968 if (!cfg->speaker_pins[0] &&
2969 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
2970 memcpy(cfg->speaker_pins, cfg->line_out_pins,
2971 sizeof(cfg->speaker_pins));
2972 cfg->speaker_outs = cfg->line_outs;
Takashi Iwai071c73a2006-08-23 18:34:06 +02002973 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974
Takashi Iwai352f7f92012-12-19 12:52:06 +01002975 if (!cfg->hp_pins[0] &&
2976 cfg->line_out_type == AUTO_PIN_HP_OUT) {
2977 memcpy(cfg->hp_pins, cfg->line_out_pins,
2978 sizeof(cfg->hp_pins));
2979 cfg->hp_outs = cfg->line_outs;
2980 }
2981
2982 for (i = 0; i < cfg->hp_outs; i++) {
2983 hda_nid_t nid = cfg->hp_pins[i];
2984 if (!is_jack_detectable(codec, nid))
2985 continue;
2986 snd_printdd("hda-codec: Enable HP auto-muting on NID 0x%x\n",
2987 nid);
2988 snd_hda_jack_detect_enable_callback(codec, nid, HDA_GEN_HP_EVENT,
Takashi Iwai2e03e952013-01-03 15:55:06 +01002989 spec->hp_automute_hook ?
2990 spec->hp_automute_hook :
Takashi Iwai5d550e12012-12-19 15:16:44 +01002991 snd_hda_gen_hp_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01002992 spec->detect_hp = 1;
2993 }
2994
2995 if (cfg->line_out_type == AUTO_PIN_LINE_OUT && cfg->line_outs) {
2996 if (cfg->speaker_outs)
2997 for (i = 0; i < cfg->line_outs; i++) {
2998 hda_nid_t nid = cfg->line_out_pins[i];
2999 if (!is_jack_detectable(codec, nid))
3000 continue;
3001 snd_printdd("hda-codec: Enable Line-Out auto-muting on NID 0x%x\n", nid);
3002 snd_hda_jack_detect_enable_callback(codec, nid,
3003 HDA_GEN_FRONT_EVENT,
Takashi Iwai2e03e952013-01-03 15:55:06 +01003004 spec->line_automute_hook ?
3005 spec->line_automute_hook :
Takashi Iwai5d550e12012-12-19 15:16:44 +01003006 snd_hda_gen_line_automute);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003007 spec->detect_lo = 1;
3008 }
3009 spec->automute_lo_possible = spec->detect_hp;
3010 }
3011
3012 spec->automute_speaker_possible = cfg->speaker_outs &&
3013 (spec->detect_hp || spec->detect_lo);
3014
3015 spec->automute_lo = spec->automute_lo_possible;
3016 spec->automute_speaker = spec->automute_speaker_possible;
3017
3018 if (spec->automute_speaker_possible || spec->automute_lo_possible) {
3019 /* create a control for automute mode */
3020 err = add_automute_mode_enum(codec);
3021 if (err < 0)
3022 return err;
3023 }
3024 return 0;
3025}
3026
Takashi Iwai352f7f92012-12-19 12:52:06 +01003027/* check whether all auto-mic pins are valid; setup indices if OK */
3028static bool auto_mic_check_imux(struct hda_codec *codec)
3029{
3030 struct hda_gen_spec *spec = codec->spec;
3031 const struct hda_input_mux *imux;
3032 int i;
3033
3034 imux = &spec->input_mux;
3035 for (i = 0; i < spec->am_num_entries; i++) {
3036 spec->am_entry[i].idx =
3037 find_idx_in_nid_list(spec->am_entry[i].pin,
3038 spec->imux_pins, imux->num_items);
3039 if (spec->am_entry[i].idx < 0)
3040 return false; /* no corresponding imux */
3041 }
3042
3043 /* we don't need the jack detection for the first pin */
3044 for (i = 1; i < spec->am_num_entries; i++)
3045 snd_hda_jack_detect_enable_callback(codec,
3046 spec->am_entry[i].pin,
3047 HDA_GEN_MIC_EVENT,
Takashi Iwai2e03e952013-01-03 15:55:06 +01003048 spec->mic_autoswitch_hook ?
3049 spec->mic_autoswitch_hook :
Takashi Iwai5d550e12012-12-19 15:16:44 +01003050 snd_hda_gen_mic_autoswitch);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003051 return true;
3052}
3053
3054static int compare_attr(const void *ap, const void *bp)
3055{
3056 const struct automic_entry *a = ap;
3057 const struct automic_entry *b = bp;
3058 return (int)(a->attr - b->attr);
3059}
3060
3061/*
3062 * Check the availability of auto-mic switch;
3063 * Set up if really supported
3064 */
3065static int check_auto_mic_availability(struct hda_codec *codec)
3066{
3067 struct hda_gen_spec *spec = codec->spec;
3068 struct auto_pin_cfg *cfg = &spec->autocfg;
3069 unsigned int types;
3070 int i, num_pins;
3071
Takashi Iwaid12daf62013-01-07 16:32:11 +01003072 if (spec->suppress_auto_mic)
3073 return 0;
3074
Takashi Iwai352f7f92012-12-19 12:52:06 +01003075 types = 0;
3076 num_pins = 0;
3077 for (i = 0; i < cfg->num_inputs; i++) {
3078 hda_nid_t nid = cfg->inputs[i].pin;
3079 unsigned int attr;
3080 attr = snd_hda_codec_get_pincfg(codec, nid);
3081 attr = snd_hda_get_input_pin_attr(attr);
3082 if (types & (1 << attr))
3083 return 0; /* already occupied */
3084 switch (attr) {
3085 case INPUT_PIN_ATTR_INT:
3086 if (cfg->inputs[i].type != AUTO_PIN_MIC)
3087 return 0; /* invalid type */
3088 break;
3089 case INPUT_PIN_ATTR_UNUSED:
3090 return 0; /* invalid entry */
3091 default:
3092 if (cfg->inputs[i].type > AUTO_PIN_LINE_IN)
3093 return 0; /* invalid type */
3094 if (!spec->line_in_auto_switch &&
3095 cfg->inputs[i].type != AUTO_PIN_MIC)
3096 return 0; /* only mic is allowed */
3097 if (!is_jack_detectable(codec, nid))
3098 return 0; /* no unsol support */
3099 break;
3100 }
3101 if (num_pins >= MAX_AUTO_MIC_PINS)
3102 return 0;
3103 types |= (1 << attr);
3104 spec->am_entry[num_pins].pin = nid;
3105 spec->am_entry[num_pins].attr = attr;
3106 num_pins++;
3107 }
3108
3109 if (num_pins < 2)
3110 return 0;
3111
3112 spec->am_num_entries = num_pins;
3113 /* sort the am_entry in the order of attr so that the pin with a
3114 * higher attr will be selected when the jack is plugged.
3115 */
3116 sort(spec->am_entry, num_pins, sizeof(spec->am_entry[0]),
3117 compare_attr, NULL);
3118
3119 if (!auto_mic_check_imux(codec))
3120 return 0;
3121
3122 spec->auto_mic = 1;
3123 spec->num_adc_nids = 1;
3124 spec->cur_mux[0] = spec->am_entry[0].idx;
3125 snd_printdd("hda-codec: Enable auto-mic switch on NID 0x%x/0x%x/0x%x\n",
3126 spec->am_entry[0].pin,
3127 spec->am_entry[1].pin,
3128 spec->am_entry[2].pin);
3129
3130 return 0;
3131}
3132
3133
Takashi Iwai9eb413e2012-12-19 14:41:21 +01003134/*
3135 * Parse the given BIOS configuration and set up the hda_gen_spec
3136 *
3137 * return 1 if successful, 0 if the proper config is not found,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003138 * or a negative error code
3139 */
3140int snd_hda_gen_parse_auto_config(struct hda_codec *codec,
Takashi Iwai9eb413e2012-12-19 14:41:21 +01003141 struct auto_pin_cfg *cfg)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003142{
3143 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003144 int err;
3145
Takashi Iwai9eb413e2012-12-19 14:41:21 +01003146 if (cfg != &spec->autocfg) {
3147 spec->autocfg = *cfg;
3148 cfg = &spec->autocfg;
3149 }
3150
Takashi Iwai352f7f92012-12-19 12:52:06 +01003151 if (!cfg->line_outs) {
3152 if (cfg->dig_outs || cfg->dig_in_pin) {
3153 spec->multiout.max_channels = 2;
3154 spec->no_analog = 1;
3155 goto dig_only;
3156 }
3157 return 0; /* can't find valid BIOS pin config */
3158 }
3159
3160 if (!spec->no_primary_hp &&
3161 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT &&
3162 cfg->line_outs <= cfg->hp_outs) {
3163 /* use HP as primary out */
3164 cfg->speaker_outs = cfg->line_outs;
3165 memcpy(cfg->speaker_pins, cfg->line_out_pins,
3166 sizeof(cfg->speaker_pins));
3167 cfg->line_outs = cfg->hp_outs;
3168 memcpy(cfg->line_out_pins, cfg->hp_pins, sizeof(cfg->hp_pins));
3169 cfg->hp_outs = 0;
3170 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
3171 cfg->line_out_type = AUTO_PIN_HP_OUT;
3172 }
3173
3174 err = parse_output_paths(codec);
3175 if (err < 0)
3176 return err;
3177 err = create_multi_channel_mode(codec);
3178 if (err < 0)
3179 return err;
3180 err = create_multi_out_ctls(codec, cfg);
3181 if (err < 0)
3182 return err;
3183 err = create_hp_out_ctls(codec);
3184 if (err < 0)
3185 return err;
3186 err = create_speaker_out_ctls(codec);
3187 if (err < 0)
3188 return err;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003189 err = create_indep_hp_ctls(codec);
3190 if (err < 0)
3191 return err;
Takashi Iwaic30aa7b2013-01-04 16:42:48 +01003192 err = create_loopback_mixing_ctl(codec);
3193 if (err < 0)
3194 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003195 err = create_shared_input(codec);
3196 if (err < 0)
3197 return err;
3198 err = create_input_ctls(codec);
Takashi Iwaid13bd412008-07-30 15:01:45 +02003199 if (err < 0)
Takashi Iwai071c73a2006-08-23 18:34:06 +02003200 return err;
3201
Takashi Iwaia07a9492013-01-07 16:44:06 +01003202 spec->const_channel_count = spec->ext_channel_count;
3203 /* check the multiple speaker and headphone pins */
3204 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
3205 spec->const_channel_count = max(spec->const_channel_count,
3206 cfg->speaker_outs * 2);
3207 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
3208 spec->const_channel_count = max(spec->const_channel_count,
3209 cfg->hp_outs * 2);
3210 spec->multiout.max_channels = max(spec->ext_channel_count,
3211 spec->const_channel_count);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003212
3213 err = check_auto_mute_availability(codec);
3214 if (err < 0)
3215 return err;
3216
3217 err = check_dyn_adc_switch(codec);
3218 if (err < 0)
3219 return err;
3220
3221 if (!spec->shared_mic_hp) {
3222 err = check_auto_mic_availability(codec);
Takashi Iwaid13bd412008-07-30 15:01:45 +02003223 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225 }
Takashi Iwai071c73a2006-08-23 18:34:06 +02003226
Takashi Iwai352f7f92012-12-19 12:52:06 +01003227 err = create_capture_mixers(codec);
3228 if (err < 0)
3229 return err;
3230
3231 err = parse_mic_boost(codec);
3232 if (err < 0)
3233 return err;
3234
3235 dig_only:
3236 parse_digital(codec);
3237
3238 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003239}
Takashi Iwai352f7f92012-12-19 12:52:06 +01003240EXPORT_SYMBOL_HDA(snd_hda_gen_parse_auto_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241
3242
3243/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01003244 * Build control elements
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01003246
3247/* slave controls for virtual master */
3248static const char * const slave_pfxs[] = {
3249 "Front", "Surround", "Center", "LFE", "Side",
3250 "Headphone", "Speaker", "Mono", "Line Out",
3251 "CLFE", "Bass Speaker", "PCM",
Takashi Iwaiee79c692013-01-07 09:57:42 +01003252 "Speaker Front", "Speaker Surround", "Speaker CLFE", "Speaker Side",
3253 "Headphone Front", "Headphone Surround", "Headphone CLFE",
3254 "Headphone Side",
Takashi Iwai352f7f92012-12-19 12:52:06 +01003255 NULL,
3256};
3257
3258int snd_hda_gen_build_controls(struct hda_codec *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003260 struct hda_gen_spec *spec = codec->spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003262
Takashi Iwai36502d02012-12-19 15:15:10 +01003263 if (spec->kctls.used) {
3264 err = snd_hda_add_new_ctls(codec, spec->kctls.list);
3265 if (err < 0)
3266 return err;
3267 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268
Takashi Iwai352f7f92012-12-19 12:52:06 +01003269 if (spec->multiout.dig_out_nid) {
3270 err = snd_hda_create_dig_out_ctls(codec,
3271 spec->multiout.dig_out_nid,
3272 spec->multiout.dig_out_nid,
3273 spec->pcm_rec[1].pcm_type);
3274 if (err < 0)
3275 return err;
3276 if (!spec->no_analog) {
3277 err = snd_hda_create_spdif_share_sw(codec,
3278 &spec->multiout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279 if (err < 0)
3280 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003281 spec->multiout.share_spdif = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282 }
3283 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003284 if (spec->dig_in_nid) {
3285 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
3286 if (err < 0)
3287 return err;
3288 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289
Takashi Iwai352f7f92012-12-19 12:52:06 +01003290 /* if we have no master control, let's create it */
3291 if (!spec->no_analog &&
3292 !snd_hda_find_mixer_ctl(codec, "Master Playback Volume")) {
3293 unsigned int vmaster_tlv[4];
3294 snd_hda_set_vmaster_tlv(codec, spec->vmaster_nid,
3295 HDA_OUTPUT, vmaster_tlv);
3296 err = snd_hda_add_vmaster(codec, "Master Playback Volume",
3297 vmaster_tlv, slave_pfxs,
3298 "Playback Volume");
3299 if (err < 0)
3300 return err;
3301 }
3302 if (!spec->no_analog &&
3303 !snd_hda_find_mixer_ctl(codec, "Master Playback Switch")) {
3304 err = __snd_hda_add_vmaster(codec, "Master Playback Switch",
3305 NULL, slave_pfxs,
3306 "Playback Switch",
3307 true, &spec->vmaster_mute.sw_kctl);
3308 if (err < 0)
3309 return err;
3310 if (spec->vmaster_mute.hook)
Takashi Iwaifd25a972012-12-20 14:57:18 +01003311 snd_hda_add_vmaster_hook(codec, &spec->vmaster_mute,
3312 spec->vmaster_mute_enum);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003313 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314
Takashi Iwai352f7f92012-12-19 12:52:06 +01003315 free_kctls(spec); /* no longer needed */
3316
3317 if (spec->shared_mic_hp) {
3318 int err;
3319 int nid = spec->autocfg.inputs[1].pin;
3320 err = snd_hda_jack_add_kctl(codec, nid, "Headphone Mic", 0);
3321 if (err < 0)
3322 return err;
3323 err = snd_hda_jack_detect_enable(codec, nid, 0);
3324 if (err < 0)
3325 return err;
3326 }
3327
3328 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
3329 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003330 return err;
3331
3332 return 0;
3333}
Takashi Iwai352f7f92012-12-19 12:52:06 +01003334EXPORT_SYMBOL_HDA(snd_hda_gen_build_controls);
3335
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336
3337/*
Takashi Iwai352f7f92012-12-19 12:52:06 +01003338 * PCM definitions
Linus Torvalds1da177e2005-04-16 15:20:36 -07003339 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003340
Takashi Iwaie6b85f32013-01-07 11:54:34 +01003341static void call_pcm_playback_hook(struct hda_pcm_stream *hinfo,
3342 struct hda_codec *codec,
3343 struct snd_pcm_substream *substream,
3344 int action)
3345{
3346 struct hda_gen_spec *spec = codec->spec;
3347 if (spec->pcm_playback_hook)
3348 spec->pcm_playback_hook(hinfo, codec, substream, action);
3349}
3350
Takashi Iwai352f7f92012-12-19 12:52:06 +01003351/*
3352 * Analog playback callbacks
3353 */
3354static int playback_pcm_open(struct hda_pcm_stream *hinfo,
3355 struct hda_codec *codec,
3356 struct snd_pcm_substream *substream)
3357{
3358 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003359 int err;
3360
3361 mutex_lock(&spec->pcm_mutex);
3362 err = snd_hda_multi_out_analog_open(codec,
3363 &spec->multiout, substream,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003364 hinfo);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01003365 if (!err) {
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003366 spec->active_streams |= 1 << STREAM_MULTI_OUT;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01003367 call_pcm_playback_hook(hinfo, codec, substream,
3368 HDA_GEN_PCM_ACT_OPEN);
3369 }
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003370 mutex_unlock(&spec->pcm_mutex);
3371 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003372}
3373
3374static int playback_pcm_prepare(struct hda_pcm_stream *hinfo,
Takashi Iwai97ec5582006-03-21 11:29:07 +01003375 struct hda_codec *codec,
3376 unsigned int stream_tag,
3377 unsigned int format,
3378 struct snd_pcm_substream *substream)
3379{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003380 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01003381 int err;
3382
3383 err = snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
3384 stream_tag, format, substream);
3385 if (!err)
3386 call_pcm_playback_hook(hinfo, codec, substream,
3387 HDA_GEN_PCM_ACT_PREPARE);
3388 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003389}
Takashi Iwai97ec5582006-03-21 11:29:07 +01003390
Takashi Iwai352f7f92012-12-19 12:52:06 +01003391static int playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
3392 struct hda_codec *codec,
3393 struct snd_pcm_substream *substream)
3394{
3395 struct hda_gen_spec *spec = codec->spec;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01003396 int err;
3397
3398 err = snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
3399 if (!err)
3400 call_pcm_playback_hook(hinfo, codec, substream,
3401 HDA_GEN_PCM_ACT_CLEANUP);
3402 return err;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003403}
3404
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003405static int playback_pcm_close(struct hda_pcm_stream *hinfo,
3406 struct hda_codec *codec,
3407 struct snd_pcm_substream *substream)
3408{
3409 struct hda_gen_spec *spec = codec->spec;
3410 mutex_lock(&spec->pcm_mutex);
3411 spec->active_streams &= ~(1 << STREAM_MULTI_OUT);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01003412 call_pcm_playback_hook(hinfo, codec, substream,
3413 HDA_GEN_PCM_ACT_CLOSE);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003414 mutex_unlock(&spec->pcm_mutex);
3415 return 0;
3416}
3417
3418static int alt_playback_pcm_open(struct hda_pcm_stream *hinfo,
3419 struct hda_codec *codec,
3420 struct snd_pcm_substream *substream)
3421{
3422 struct hda_gen_spec *spec = codec->spec;
3423 int err = 0;
3424
3425 mutex_lock(&spec->pcm_mutex);
3426 if (!spec->indep_hp_enabled)
3427 err = -EBUSY;
3428 else
3429 spec->active_streams |= 1 << STREAM_INDEP_HP;
Takashi Iwaie6b85f32013-01-07 11:54:34 +01003430 call_pcm_playback_hook(hinfo, codec, substream,
3431 HDA_GEN_PCM_ACT_OPEN);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003432 mutex_unlock(&spec->pcm_mutex);
3433 return err;
3434}
3435
3436static int alt_playback_pcm_close(struct hda_pcm_stream *hinfo,
3437 struct hda_codec *codec,
3438 struct snd_pcm_substream *substream)
3439{
3440 struct hda_gen_spec *spec = codec->spec;
3441 mutex_lock(&spec->pcm_mutex);
3442 spec->active_streams &= ~(1 << STREAM_INDEP_HP);
Takashi Iwaie6b85f32013-01-07 11:54:34 +01003443 call_pcm_playback_hook(hinfo, codec, substream,
3444 HDA_GEN_PCM_ACT_CLOSE);
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003445 mutex_unlock(&spec->pcm_mutex);
3446 return 0;
3447}
3448
Takashi Iwaie6b85f32013-01-07 11:54:34 +01003449static int alt_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
3450 struct hda_codec *codec,
3451 unsigned int stream_tag,
3452 unsigned int format,
3453 struct snd_pcm_substream *substream)
3454{
3455 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
3456 call_pcm_playback_hook(hinfo, codec, substream,
3457 HDA_GEN_PCM_ACT_PREPARE);
3458 return 0;
3459}
3460
3461static int alt_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
3462 struct hda_codec *codec,
3463 struct snd_pcm_substream *substream)
3464{
3465 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
3466 call_pcm_playback_hook(hinfo, codec, substream,
3467 HDA_GEN_PCM_ACT_CLEANUP);
3468 return 0;
3469}
3470
Takashi Iwai352f7f92012-12-19 12:52:06 +01003471/*
3472 * Digital out
3473 */
3474static int dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
3475 struct hda_codec *codec,
3476 struct snd_pcm_substream *substream)
3477{
3478 struct hda_gen_spec *spec = codec->spec;
3479 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
3480}
3481
3482static int dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
3483 struct hda_codec *codec,
3484 unsigned int stream_tag,
3485 unsigned int format,
3486 struct snd_pcm_substream *substream)
3487{
3488 struct hda_gen_spec *spec = codec->spec;
3489 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
3490 stream_tag, format, substream);
3491}
3492
3493static int dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
3494 struct hda_codec *codec,
3495 struct snd_pcm_substream *substream)
3496{
3497 struct hda_gen_spec *spec = codec->spec;
3498 return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
3499}
3500
3501static int dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
3502 struct hda_codec *codec,
3503 struct snd_pcm_substream *substream)
3504{
3505 struct hda_gen_spec *spec = codec->spec;
3506 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
3507}
3508
3509/*
3510 * Analog capture
3511 */
3512static int alt_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
3513 struct hda_codec *codec,
3514 unsigned int stream_tag,
3515 unsigned int format,
3516 struct snd_pcm_substream *substream)
3517{
3518 struct hda_gen_spec *spec = codec->spec;
3519
3520 snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number + 1],
Takashi Iwai97ec5582006-03-21 11:29:07 +01003521 stream_tag, 0, format);
3522 return 0;
3523}
3524
Takashi Iwai352f7f92012-12-19 12:52:06 +01003525static int alt_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
3526 struct hda_codec *codec,
3527 struct snd_pcm_substream *substream)
Takashi Iwai97ec5582006-03-21 11:29:07 +01003528{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003529 struct hda_gen_spec *spec = codec->spec;
Takashi Iwai97ec5582006-03-21 11:29:07 +01003530
Takashi Iwai352f7f92012-12-19 12:52:06 +01003531 snd_hda_codec_cleanup_stream(codec,
3532 spec->adc_nids[substream->number + 1]);
Takashi Iwai97ec5582006-03-21 11:29:07 +01003533 return 0;
3534}
3535
Takashi Iwai352f7f92012-12-19 12:52:06 +01003536/*
3537 */
3538static const struct hda_pcm_stream pcm_analog_playback = {
3539 .substreams = 1,
3540 .channels_min = 2,
3541 .channels_max = 8,
3542 /* NID is set in build_pcms */
3543 .ops = {
3544 .open = playback_pcm_open,
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003545 .close = playback_pcm_close,
Takashi Iwai352f7f92012-12-19 12:52:06 +01003546 .prepare = playback_pcm_prepare,
3547 .cleanup = playback_pcm_cleanup
3548 },
3549};
Linus Torvalds1da177e2005-04-16 15:20:36 -07003550
Takashi Iwai352f7f92012-12-19 12:52:06 +01003551static const struct hda_pcm_stream pcm_analog_capture = {
3552 .substreams = 1,
3553 .channels_min = 2,
3554 .channels_max = 2,
3555 /* NID is set in build_pcms */
3556};
3557
3558static const struct hda_pcm_stream pcm_analog_alt_playback = {
3559 .substreams = 1,
3560 .channels_min = 2,
3561 .channels_max = 2,
3562 /* NID is set in build_pcms */
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003563 .ops = {
3564 .open = alt_playback_pcm_open,
Takashi Iwaie6b85f32013-01-07 11:54:34 +01003565 .close = alt_playback_pcm_close,
3566 .prepare = alt_playback_pcm_prepare,
3567 .cleanup = alt_playback_pcm_cleanup
Takashi Iwai38cf6f12012-12-21 14:09:42 +01003568 },
Takashi Iwai352f7f92012-12-19 12:52:06 +01003569};
3570
3571static const struct hda_pcm_stream pcm_analog_alt_capture = {
3572 .substreams = 2, /* can be overridden */
3573 .channels_min = 2,
3574 .channels_max = 2,
3575 /* NID is set in build_pcms */
3576 .ops = {
3577 .prepare = alt_capture_pcm_prepare,
3578 .cleanup = alt_capture_pcm_cleanup
3579 },
3580};
3581
3582static const struct hda_pcm_stream pcm_digital_playback = {
3583 .substreams = 1,
3584 .channels_min = 2,
3585 .channels_max = 2,
3586 /* NID is set in build_pcms */
3587 .ops = {
3588 .open = dig_playback_pcm_open,
3589 .close = dig_playback_pcm_close,
3590 .prepare = dig_playback_pcm_prepare,
3591 .cleanup = dig_playback_pcm_cleanup
3592 },
3593};
3594
3595static const struct hda_pcm_stream pcm_digital_capture = {
3596 .substreams = 1,
3597 .channels_min = 2,
3598 .channels_max = 2,
3599 /* NID is set in build_pcms */
3600};
3601
3602/* Used by build_pcms to flag that a PCM has no playback stream */
3603static const struct hda_pcm_stream pcm_null_stream = {
3604 .substreams = 0,
3605 .channels_min = 0,
3606 .channels_max = 0,
3607};
3608
3609/*
3610 * dynamic changing ADC PCM streams
3611 */
3612static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur)
3613{
3614 struct hda_gen_spec *spec = codec->spec;
3615 hda_nid_t new_adc = spec->adc_nids[spec->dyn_adc_idx[cur]];
3616
3617 if (spec->cur_adc && spec->cur_adc != new_adc) {
3618 /* stream is running, let's swap the current ADC */
3619 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
3620 spec->cur_adc = new_adc;
3621 snd_hda_codec_setup_stream(codec, new_adc,
3622 spec->cur_adc_stream_tag, 0,
3623 spec->cur_adc_format);
3624 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003625 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003626 return false;
3627}
3628
3629/* analog capture with dynamic dual-adc changes */
3630static int dyn_adc_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
3631 struct hda_codec *codec,
3632 unsigned int stream_tag,
3633 unsigned int format,
3634 struct snd_pcm_substream *substream)
3635{
3636 struct hda_gen_spec *spec = codec->spec;
3637 spec->cur_adc = spec->adc_nids[spec->dyn_adc_idx[spec->cur_mux[0]]];
3638 spec->cur_adc_stream_tag = stream_tag;
3639 spec->cur_adc_format = format;
3640 snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
3641 return 0;
3642}
3643
3644static int dyn_adc_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
3645 struct hda_codec *codec,
3646 struct snd_pcm_substream *substream)
3647{
3648 struct hda_gen_spec *spec = codec->spec;
3649 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
3650 spec->cur_adc = 0;
3651 return 0;
3652}
3653
3654static const struct hda_pcm_stream dyn_adc_pcm_analog_capture = {
3655 .substreams = 1,
3656 .channels_min = 2,
3657 .channels_max = 2,
3658 .nid = 0, /* fill later */
3659 .ops = {
3660 .prepare = dyn_adc_capture_pcm_prepare,
3661 .cleanup = dyn_adc_capture_pcm_cleanup
3662 },
3663};
3664
Takashi Iwaif873e532012-12-20 16:58:39 +01003665static void fill_pcm_stream_name(char *str, size_t len, const char *sfx,
3666 const char *chip_name)
3667{
3668 char *p;
3669
3670 if (*str)
3671 return;
3672 strlcpy(str, chip_name, len);
3673
3674 /* drop non-alnum chars after a space */
3675 for (p = strchr(str, ' '); p; p = strchr(p + 1, ' ')) {
3676 if (!isalnum(p[1])) {
3677 *p = 0;
3678 break;
3679 }
3680 }
3681 strlcat(str, sfx, len);
3682}
3683
Takashi Iwai352f7f92012-12-19 12:52:06 +01003684/* build PCM streams based on the parsed results */
3685int snd_hda_gen_build_pcms(struct hda_codec *codec)
3686{
3687 struct hda_gen_spec *spec = codec->spec;
3688 struct hda_pcm *info = spec->pcm_rec;
3689 const struct hda_pcm_stream *p;
3690 bool have_multi_adcs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003691
3692 codec->num_pcms = 1;
3693 codec->pcm_info = info;
3694
Takashi Iwai352f7f92012-12-19 12:52:06 +01003695 if (spec->no_analog)
3696 goto skip_analog;
3697
Takashi Iwaif873e532012-12-20 16:58:39 +01003698 fill_pcm_stream_name(spec->stream_name_analog,
3699 sizeof(spec->stream_name_analog),
3700 " Analog", codec->chip_name);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003701 info->name = spec->stream_name_analog;
3702
3703 if (spec->multiout.num_dacs > 0) {
3704 p = spec->stream_analog_playback;
3705 if (!p)
3706 p = &pcm_analog_playback;
3707 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
3708 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dac_nids[0];
3709 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
3710 spec->multiout.max_channels;
3711 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT &&
3712 spec->autocfg.line_outs == 2)
3713 info->stream[SNDRV_PCM_STREAM_PLAYBACK].chmap =
3714 snd_pcm_2_1_chmaps;
3715 }
3716 if (spec->num_adc_nids) {
3717 p = spec->stream_analog_capture;
3718 if (!p) {
3719 if (spec->dyn_adc_switch)
3720 p = &dyn_adc_pcm_analog_capture;
3721 else
3722 p = &pcm_analog_capture;
3723 }
3724 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
3725 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adc_nids[0];
3726 }
3727
Takashi Iwai352f7f92012-12-19 12:52:06 +01003728 skip_analog:
3729 /* SPDIF for stream index #1 */
3730 if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
Takashi Iwaif873e532012-12-20 16:58:39 +01003731 fill_pcm_stream_name(spec->stream_name_digital,
3732 sizeof(spec->stream_name_digital),
3733 " Digital", codec->chip_name);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003734 codec->num_pcms = 2;
3735 codec->slave_dig_outs = spec->multiout.slave_dig_outs;
3736 info = spec->pcm_rec + 1;
3737 info->name = spec->stream_name_digital;
3738 if (spec->dig_out_type)
3739 info->pcm_type = spec->dig_out_type;
3740 else
3741 info->pcm_type = HDA_PCM_TYPE_SPDIF;
3742 if (spec->multiout.dig_out_nid) {
3743 p = spec->stream_digital_playback;
3744 if (!p)
3745 p = &pcm_digital_playback;
3746 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
3747 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dig_out_nid;
3748 }
3749 if (spec->dig_in_nid) {
3750 p = spec->stream_digital_capture;
3751 if (!p)
3752 p = &pcm_digital_capture;
3753 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
3754 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in_nid;
3755 }
3756 }
3757
3758 if (spec->no_analog)
3759 return 0;
3760
3761 /* If the use of more than one ADC is requested for the current
3762 * model, configure a second analog capture-only PCM.
3763 */
3764 have_multi_adcs = (spec->num_adc_nids > 1) &&
3765 !spec->dyn_adc_switch && !spec->auto_mic;
3766 /* Additional Analaog capture for index #2 */
3767 if (spec->alt_dac_nid || have_multi_adcs) {
3768 codec->num_pcms = 3;
3769 info = spec->pcm_rec + 2;
3770 info->name = spec->stream_name_analog;
3771 if (spec->alt_dac_nid) {
3772 p = spec->stream_analog_alt_playback;
3773 if (!p)
3774 p = &pcm_analog_alt_playback;
3775 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
3776 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
3777 spec->alt_dac_nid;
3778 } else {
3779 info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
3780 pcm_null_stream;
3781 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = 0;
3782 }
3783 if (have_multi_adcs) {
3784 p = spec->stream_analog_alt_capture;
3785 if (!p)
3786 p = &pcm_analog_alt_capture;
3787 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
3788 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
3789 spec->adc_nids[1];
3790 info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams =
3791 spec->num_adc_nids - 1;
3792 } else {
3793 info->stream[SNDRV_PCM_STREAM_CAPTURE] =
3794 pcm_null_stream;
3795 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = 0;
3796 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003797 }
3798
3799 return 0;
3800}
Takashi Iwai352f7f92012-12-19 12:52:06 +01003801EXPORT_SYMBOL_HDA(snd_hda_gen_build_pcms);
3802
3803
3804/*
3805 * Standard auto-parser initializations
3806 */
3807
Takashi Iwaid4156932013-01-07 10:08:02 +01003808/* configure the given path as a proper output */
3809static void set_output_and_unmute(struct hda_codec *codec,
Takashi Iwai196c17662013-01-04 15:01:40 +01003810 int pin_type, int path_idx)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003811{
3812 struct nid_path *path;
Takashi Iwaid4156932013-01-07 10:08:02 +01003813 hda_nid_t pin;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003814
Takashi Iwai196c17662013-01-04 15:01:40 +01003815 path = snd_hda_get_path_from_idx(codec, path_idx);
Takashi Iwaid4156932013-01-07 10:08:02 +01003816 if (!path || !path->depth)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003817 return;
Takashi Iwaid4156932013-01-07 10:08:02 +01003818 pin = path->path[path->depth - 1];
3819 snd_hda_set_pin_ctl_cache(codec, pin, pin_type);
Takashi Iwaie1284af2013-01-03 16:33:02 +01003820 snd_hda_activate_path(codec, path, path->active, true);
3821 set_pin_eapd(codec, pin, path->active);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003822}
3823
3824/* initialize primary output paths */
3825static void init_multi_out(struct hda_codec *codec)
3826{
3827 struct hda_gen_spec *spec = codec->spec;
3828 int pin_type;
3829 int i;
3830
3831 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
3832 pin_type = PIN_HP;
3833 else
3834 pin_type = PIN_OUT;
3835
Takashi Iwaid4156932013-01-07 10:08:02 +01003836 for (i = 0; i < spec->autocfg.line_outs; i++)
3837 set_output_and_unmute(codec, pin_type, spec->out_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003838}
3839
Takashi Iwaidb23fd12012-12-20 15:27:24 +01003840
3841static void __init_extra_out(struct hda_codec *codec, int num_outs,
Takashi Iwaid4156932013-01-07 10:08:02 +01003842 int *paths, int type)
Takashi Iwai352f7f92012-12-19 12:52:06 +01003843{
Takashi Iwai352f7f92012-12-19 12:52:06 +01003844 int i;
Takashi Iwai352f7f92012-12-19 12:52:06 +01003845
Takashi Iwaid4156932013-01-07 10:08:02 +01003846 for (i = 0; i < num_outs; i++)
3847 set_output_and_unmute(codec, type, paths[i]);
Takashi Iwaidb23fd12012-12-20 15:27:24 +01003848}
3849
3850/* initialize hp and speaker paths */
3851static void init_extra_out(struct hda_codec *codec)
3852{
3853 struct hda_gen_spec *spec = codec->spec;
3854
3855 if (spec->autocfg.line_out_type != AUTO_PIN_HP_OUT)
3856 __init_extra_out(codec, spec->autocfg.hp_outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01003857 spec->hp_paths, PIN_HP);
Takashi Iwaidb23fd12012-12-20 15:27:24 +01003858 if (spec->autocfg.line_out_type != AUTO_PIN_SPEAKER_OUT)
3859 __init_extra_out(codec, spec->autocfg.speaker_outs,
Takashi Iwai196c17662013-01-04 15:01:40 +01003860 spec->speaker_paths, PIN_OUT);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003861}
3862
3863/* initialize multi-io paths */
3864static void init_multi_io(struct hda_codec *codec)
3865{
3866 struct hda_gen_spec *spec = codec->spec;
3867 int i;
3868
3869 for (i = 0; i < spec->multi_ios; i++) {
3870 hda_nid_t pin = spec->multi_io[i].pin;
3871 struct nid_path *path;
Takashi Iwai196c17662013-01-04 15:01:40 +01003872 path = get_multiio_path(codec, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003873 if (!path)
3874 continue;
3875 if (!spec->multi_io[i].ctl_in)
3876 spec->multi_io[i].ctl_in =
3877 snd_hda_codec_update_cache(codec, pin, 0,
3878 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
3879 snd_hda_activate_path(codec, path, path->active, true);
3880 }
3881}
3882
3883/* set up the input pin config, depending on the given auto-pin type */
3884static void set_input_pin(struct hda_codec *codec, hda_nid_t nid,
3885 int auto_pin_type)
3886{
3887 unsigned int val = PIN_IN;
3888 if (auto_pin_type == AUTO_PIN_MIC)
3889 val |= snd_hda_get_default_vref(codec, nid);
Takashi Iwai7594aa32012-12-20 15:38:40 +01003890 snd_hda_set_pin_ctl_cache(codec, nid, val);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003891}
3892
3893/* set up input pins and loopback paths */
3894static void init_analog_input(struct hda_codec *codec)
3895{
3896 struct hda_gen_spec *spec = codec->spec;
3897 struct auto_pin_cfg *cfg = &spec->autocfg;
3898 int i;
3899
3900 for (i = 0; i < cfg->num_inputs; i++) {
3901 hda_nid_t nid = cfg->inputs[i].pin;
3902 if (is_input_pin(codec, nid))
3903 set_input_pin(codec, nid, cfg->inputs[i].type);
3904
3905 /* init loopback inputs */
3906 if (spec->mixer_nid) {
3907 struct nid_path *path;
Takashi Iwai196c17662013-01-04 15:01:40 +01003908 path = snd_hda_get_path_from_idx(codec, spec->loopback_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003909 if (path)
3910 snd_hda_activate_path(codec, path,
3911 path->active, false);
3912 }
3913 }
3914}
3915
3916/* initialize ADC paths */
3917static void init_input_src(struct hda_codec *codec)
3918{
3919 struct hda_gen_spec *spec = codec->spec;
3920 struct hda_input_mux *imux = &spec->input_mux;
3921 struct nid_path *path;
3922 int i, c, nums;
3923
3924 if (spec->dyn_adc_switch)
3925 nums = 1;
3926 else
3927 nums = spec->num_adc_nids;
3928
3929 for (c = 0; c < nums; c++) {
3930 for (i = 0; i < imux->num_items; i++) {
Takashi Iwaic697b712013-01-07 17:09:26 +01003931 path = get_input_path(codec, c, i);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003932 if (path) {
3933 bool active = path->active;
3934 if (i == spec->cur_mux[c])
3935 active = true;
3936 snd_hda_activate_path(codec, path, active, false);
3937 }
3938 }
3939 }
3940
3941 if (spec->shared_mic_hp)
3942 update_shared_mic_hp(codec, spec->cur_mux[0]);
3943
3944 if (spec->cap_sync_hook)
3945 spec->cap_sync_hook(codec);
3946}
3947
3948/* set right pin controls for digital I/O */
3949static void init_digital(struct hda_codec *codec)
3950{
3951 struct hda_gen_spec *spec = codec->spec;
3952 int i;
3953 hda_nid_t pin;
3954
Takashi Iwaid4156932013-01-07 10:08:02 +01003955 for (i = 0; i < spec->autocfg.dig_outs; i++)
3956 set_output_and_unmute(codec, PIN_OUT, spec->digout_paths[i]);
Takashi Iwai352f7f92012-12-19 12:52:06 +01003957 pin = spec->autocfg.dig_in_pin;
Takashi Iwai2430d7b2013-01-04 15:09:42 +01003958 if (pin) {
3959 struct nid_path *path;
Takashi Iwai7594aa32012-12-20 15:38:40 +01003960 snd_hda_set_pin_ctl_cache(codec, pin, PIN_IN);
Takashi Iwai2430d7b2013-01-04 15:09:42 +01003961 path = snd_hda_get_path_from_idx(codec, spec->digin_path);
3962 if (path)
3963 snd_hda_activate_path(codec, path, path->active, false);
3964 }
Takashi Iwai352f7f92012-12-19 12:52:06 +01003965}
3966
Takashi Iwai973e4972012-12-20 15:16:09 +01003967/* clear unsol-event tags on unused pins; Conexant codecs seem to leave
3968 * invalid unsol tags by some reason
3969 */
3970static void clear_unsol_on_unused_pins(struct hda_codec *codec)
3971{
3972 int i;
3973
3974 for (i = 0; i < codec->init_pins.used; i++) {
3975 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
3976 hda_nid_t nid = pin->nid;
3977 if (is_jack_detectable(codec, nid) &&
3978 !snd_hda_jack_tbl_get(codec, nid))
3979 snd_hda_codec_update_cache(codec, nid, 0,
3980 AC_VERB_SET_UNSOLICITED_ENABLE, 0);
3981 }
3982}
3983
Takashi Iwai5187ac12013-01-07 12:52:16 +01003984/*
3985 * initialize the generic spec;
3986 * this can be put as patch_ops.init function
3987 */
Takashi Iwai352f7f92012-12-19 12:52:06 +01003988int snd_hda_gen_init(struct hda_codec *codec)
3989{
3990 struct hda_gen_spec *spec = codec->spec;
3991
3992 if (spec->init_hook)
3993 spec->init_hook(codec);
3994
3995 snd_hda_apply_verbs(codec);
3996
Takashi Iwai3bbcd272012-12-20 11:50:58 +01003997 codec->cached_write = 1;
3998
Takashi Iwai352f7f92012-12-19 12:52:06 +01003999 init_multi_out(codec);
4000 init_extra_out(codec);
4001 init_multi_io(codec);
4002 init_analog_input(codec);
4003 init_input_src(codec);
4004 init_digital(codec);
4005
Takashi Iwai973e4972012-12-20 15:16:09 +01004006 clear_unsol_on_unused_pins(codec);
4007
Takashi Iwai352f7f92012-12-19 12:52:06 +01004008 /* call init functions of standard auto-mute helpers */
Takashi Iwai5d550e12012-12-19 15:16:44 +01004009 snd_hda_gen_hp_automute(codec, NULL);
4010 snd_hda_gen_line_automute(codec, NULL);
4011 snd_hda_gen_mic_autoswitch(codec, NULL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004012
Takashi Iwai3bbcd272012-12-20 11:50:58 +01004013 snd_hda_codec_flush_amp_cache(codec);
4014 snd_hda_codec_flush_cmd_cache(codec);
4015
Takashi Iwai352f7f92012-12-19 12:52:06 +01004016 if (spec->vmaster_mute.sw_kctl && spec->vmaster_mute.hook)
4017 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
4018
4019 hda_call_check_power_status(codec, 0x01);
4020 return 0;
4021}
Takashi Iwaifce52a32013-01-07 12:42:48 +01004022EXPORT_SYMBOL_HDA(snd_hda_gen_init);
4023
Takashi Iwai5187ac12013-01-07 12:52:16 +01004024/*
4025 * free the generic spec;
4026 * this can be put as patch_ops.free function
4027 */
Takashi Iwaifce52a32013-01-07 12:42:48 +01004028void snd_hda_gen_free(struct hda_codec *codec)
4029{
4030 snd_hda_gen_spec_free(codec->spec);
4031 kfree(codec->spec);
4032 codec->spec = NULL;
4033}
4034EXPORT_SYMBOL_HDA(snd_hda_gen_free);
4035
4036#ifdef CONFIG_PM
Takashi Iwai5187ac12013-01-07 12:52:16 +01004037/*
4038 * check the loopback power save state;
4039 * this can be put as patch_ops.check_power_status function
4040 */
Takashi Iwaifce52a32013-01-07 12:42:48 +01004041int snd_hda_gen_check_power_status(struct hda_codec *codec, hda_nid_t nid)
4042{
4043 struct hda_gen_spec *spec = codec->spec;
4044 return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
4045}
4046EXPORT_SYMBOL_HDA(snd_hda_gen_check_power_status);
4047#endif
Takashi Iwai352f7f92012-12-19 12:52:06 +01004048
4049
4050/*
4051 * the generic codec support
4052 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004053
Takashi Iwai352f7f92012-12-19 12:52:06 +01004054static const struct hda_codec_ops generic_patch_ops = {
4055 .build_controls = snd_hda_gen_build_controls,
4056 .build_pcms = snd_hda_gen_build_pcms,
4057 .init = snd_hda_gen_init,
Takashi Iwaifce52a32013-01-07 12:42:48 +01004058 .free = snd_hda_gen_free,
Takashi Iwai352f7f92012-12-19 12:52:06 +01004059 .unsol_event = snd_hda_jack_unsol_event,
Takashi Iwai83012a72012-08-24 18:38:08 +02004060#ifdef CONFIG_PM
Takashi Iwaifce52a32013-01-07 12:42:48 +01004061 .check_power_status = snd_hda_gen_check_power_status,
Takashi Iwaicb53c622007-08-10 17:21:45 +02004062#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004063};
4064
Linus Torvalds1da177e2005-04-16 15:20:36 -07004065int snd_hda_parse_generic_codec(struct hda_codec *codec)
4066{
Takashi Iwai352f7f92012-12-19 12:52:06 +01004067 struct hda_gen_spec *spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004068 int err;
4069
Takashi Iwaie560d8d2005-09-09 14:21:46 +02004070 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004071 if (!spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004072 return -ENOMEM;
Takashi Iwai352f7f92012-12-19 12:52:06 +01004073 snd_hda_gen_spec_init(spec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004074 codec->spec = spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004075
Takashi Iwai9eb413e2012-12-19 14:41:21 +01004076 err = snd_hda_parse_pin_defcfg(codec, &spec->autocfg, NULL, 0);
4077 if (err < 0)
4078 return err;
4079
4080 err = snd_hda_gen_parse_auto_config(codec, &spec->autocfg);
Takashi Iwai352f7f92012-12-19 12:52:06 +01004081 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004082 goto error;
4083
4084 codec->patch_ops = generic_patch_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004085 return 0;
4086
Takashi Iwai352f7f92012-12-19 12:52:06 +01004087error:
Takashi Iwaifce52a32013-01-07 12:42:48 +01004088 snd_hda_gen_free(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004089 return err;
4090}
Takashi Iwaifce52a32013-01-07 12:42:48 +01004091EXPORT_SYMBOL_HDA(snd_hda_parse_generic_codec);