ALSA: hda - Drop spec->channel_mode field from hda_gen_spec
[linux-2.6-block.git] / sound / pci / hda / hda_generic.c
CommitLineData
1da177e4
LT
1/*
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
1da177e4
LT
23#include <linux/init.h>
24#include <linux/slab.h>
d81a6d71 25#include <linux/export.h>
352f7f91 26#include <linux/sort.h>
f873e536
TI
27#include <linux/ctype.h>
28#include <linux/string.h>
1da177e4 29#include <sound/core.h>
352f7f91 30#include <sound/jack.h>
1da177e4
LT
31#include "hda_codec.h"
32#include "hda_local.h"
352f7f91
TI
33#include "hda_auto_parser.h"
34#include "hda_jack.h"
35#include "hda_generic.h"
1da177e4 36
a7da6ce5 37
352f7f91
TI
38/* initialize hda_gen_spec struct */
39int snd_hda_gen_spec_init(struct hda_gen_spec *spec)
40{
41 snd_array_init(&spec->kctls, sizeof(struct snd_kcontrol_new), 32);
42 snd_array_init(&spec->bind_ctls, sizeof(struct hda_bind_ctls *), 8);
43 snd_array_init(&spec->paths, sizeof(struct nid_path), 8);
44 return 0;
45}
46EXPORT_SYMBOL_HDA(snd_hda_gen_spec_init);
1da177e4 47
12c93df6
TI
48struct snd_kcontrol_new *
49snd_hda_gen_add_kctl(struct hda_gen_spec *spec, const char *name,
50 const struct snd_kcontrol_new *temp)
352f7f91
TI
51{
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}
12c93df6 64EXPORT_SYMBOL_HDA(snd_hda_gen_add_kctl);
1da177e4 65
352f7f91
TI
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}
1da177e4 76
352f7f91
TI
77static struct hda_bind_ctls *new_bind_ctl(struct hda_codec *codec,
78 unsigned int nums,
79 struct hda_ctl_ops *ops)
80{
81 struct hda_gen_spec *spec = codec->spec;
82 struct hda_bind_ctls **ctlp, *ctl;
83 ctlp = snd_array_new(&spec->bind_ctls);
84 if (!ctlp)
85 return NULL;
86 ctl = kzalloc(sizeof(*ctl) + sizeof(long) * (nums + 1), GFP_KERNEL);
87 *ctlp = ctl;
88 if (ctl)
89 ctl->ops = ops;
90 return ctl;
91}
1da177e4 92
352f7f91
TI
93static void free_bind_ctls(struct hda_gen_spec *spec)
94{
95 if (spec->bind_ctls.list) {
96 struct hda_bind_ctls **ctl = spec->bind_ctls.list;
97 int i;
98 for (i = 0; i < spec->bind_ctls.used; i++)
99 kfree(ctl[i]);
100 }
101 snd_array_free(&spec->bind_ctls);
102}
cb53c626 103
352f7f91
TI
104void snd_hda_gen_spec_free(struct hda_gen_spec *spec)
105{
106 if (!spec)
107 return;
108 free_kctls(spec);
109 free_bind_ctls(spec);
110 snd_array_free(&spec->paths);
111}
112EXPORT_SYMBOL_HDA(snd_hda_gen_spec_free);
1da177e4
LT
113
114/*
352f7f91 115 * parsing paths
1da177e4 116 */
1da177e4 117
352f7f91
TI
118/* get the path between the given NIDs;
119 * passing 0 to either @pin or @dac behaves as a wildcard
1da177e4 120 */
352f7f91
TI
121struct nid_path *snd_hda_get_nid_path(struct hda_codec *codec,
122 hda_nid_t from_nid, hda_nid_t to_nid)
1da177e4 123{
352f7f91
TI
124 struct hda_gen_spec *spec = codec->spec;
125 int i;
1da177e4 126
352f7f91
TI
127 for (i = 0; i < spec->paths.used; i++) {
128 struct nid_path *path = snd_array_elem(&spec->paths, i);
129 if (path->depth <= 0)
130 continue;
131 if ((!from_nid || path->path[0] == from_nid) &&
132 (!to_nid || path->path[path->depth - 1] == to_nid))
133 return path;
1da177e4 134 }
352f7f91 135 return NULL;
1da177e4 136}
352f7f91 137EXPORT_SYMBOL_HDA(snd_hda_get_nid_path);
1da177e4 138
352f7f91
TI
139/* check whether the given DAC is already found in any existing paths */
140static bool is_dac_already_used(struct hda_codec *codec, hda_nid_t nid)
1da177e4 141{
352f7f91
TI
142 struct hda_gen_spec *spec = codec->spec;
143 int i;
1da177e4 144
352f7f91
TI
145 for (i = 0; i < spec->paths.used; i++) {
146 struct nid_path *path = snd_array_elem(&spec->paths, i);
147 if (path->path[0] == nid)
148 return true;
d2569505 149 }
352f7f91
TI
150 return false;
151}
1da177e4 152
352f7f91
TI
153/* check whether the given two widgets can be connected */
154static bool is_reachable_path(struct hda_codec *codec,
155 hda_nid_t from_nid, hda_nid_t to_nid)
156{
157 if (!from_nid || !to_nid)
158 return false;
159 return snd_hda_get_conn_index(codec, to_nid, from_nid, true) >= 0;
160}
1da177e4 161
352f7f91
TI
162/* nid, dir and idx */
163#define AMP_VAL_COMPARE_MASK (0xffff | (1U << 18) | (0x0f << 19))
164
165/* check whether the given ctl is already assigned in any path elements */
166static bool is_ctl_used(struct hda_codec *codec, unsigned int val, int type)
167{
168 struct hda_gen_spec *spec = codec->spec;
169 int i;
170
171 val &= AMP_VAL_COMPARE_MASK;
172 for (i = 0; i < spec->paths.used; i++) {
173 struct nid_path *path = snd_array_elem(&spec->paths, i);
174 if ((path->ctls[type] & AMP_VAL_COMPARE_MASK) == val)
175 return true;
1da177e4 176 }
352f7f91 177 return false;
1da177e4
LT
178}
179
352f7f91
TI
180/* check whether a control with the given (nid, dir, idx) was assigned */
181static bool is_ctl_associated(struct hda_codec *codec, hda_nid_t nid,
182 int dir, int idx)
1da177e4 183{
352f7f91
TI
184 unsigned int val = HDA_COMPOSE_AMP_VAL(nid, 3, idx, dir);
185 return is_ctl_used(codec, val, NID_PATH_VOL_CTL) ||
186 is_ctl_used(codec, val, NID_PATH_MUTE_CTL);
187}
1da177e4 188
352f7f91
TI
189/* called recursively */
190static bool __parse_nid_path(struct hda_codec *codec,
191 hda_nid_t from_nid, hda_nid_t to_nid,
192 int with_aa_mix, struct nid_path *path, int depth)
193{
194 struct hda_gen_spec *spec = codec->spec;
195 hda_nid_t conn[16];
196 int i, nums;
197
198 if (to_nid == spec->mixer_nid) {
199 if (!with_aa_mix)
200 return false;
201 with_aa_mix = 2; /* mark aa-mix is included */
1da177e4
LT
202 }
203
352f7f91
TI
204 nums = snd_hda_get_connections(codec, to_nid, conn, ARRAY_SIZE(conn));
205 for (i = 0; i < nums; i++) {
206 if (conn[i] != from_nid) {
207 /* special case: when from_nid is 0,
208 * try to find an empty DAC
209 */
210 if (from_nid ||
211 get_wcaps_type(get_wcaps(codec, conn[i])) != AC_WID_AUD_OUT ||
212 is_dac_already_used(codec, conn[i]))
213 continue;
214 }
215 /* aa-mix is requested but not included? */
216 if (!(spec->mixer_nid && with_aa_mix == 1))
217 goto found;
1da177e4 218 }
352f7f91
TI
219 if (depth >= MAX_NID_PATH_DEPTH)
220 return false;
221 for (i = 0; i < nums; i++) {
222 unsigned int type;
223 type = get_wcaps_type(get_wcaps(codec, conn[i]));
224 if (type == AC_WID_AUD_OUT || type == AC_WID_AUD_IN ||
225 type == AC_WID_PIN)
226 continue;
227 if (__parse_nid_path(codec, from_nid, conn[i],
228 with_aa_mix, path, depth + 1))
229 goto found;
230 }
231 return false;
232
233 found:
234 path->path[path->depth] = conn[i];
235 path->idx[path->depth + 1] = i;
236 if (nums > 1 && get_wcaps_type(get_wcaps(codec, to_nid)) != AC_WID_AUD_MIX)
237 path->multi[path->depth + 1] = 1;
238 path->depth++;
239 return true;
1da177e4
LT
240}
241
352f7f91
TI
242/* parse the widget path from the given nid to the target nid;
243 * when @from_nid is 0, try to find an empty DAC;
244 * when @with_aa_mix is 0, paths with spec->mixer_nid are excluded.
245 * when @with_aa_mix is 1, paths without spec->mixer_nid are excluded.
246 * when @with_aa_mix is 2, no special handling about spec->mixer_nid.
1da177e4 247 */
352f7f91
TI
248bool snd_hda_parse_nid_path(struct hda_codec *codec, hda_nid_t from_nid,
249 hda_nid_t to_nid, int with_aa_mix,
250 struct nid_path *path)
1da177e4 251{
352f7f91
TI
252 if (__parse_nid_path(codec, from_nid, to_nid, with_aa_mix, path, 1)) {
253 path->path[path->depth] = to_nid;
254 path->depth++;
255#if 0
256 snd_printdd("path: depth=%d, %02x/%02x/%02x/%02x/%02x\n",
257 path->depth, path->path[0], path->path[1],
258 path->path[2], path->path[3], path->path[4]);
259#endif
260 return true;
1da177e4 261 }
352f7f91 262 return false;
1da177e4 263}
352f7f91 264EXPORT_SYMBOL_HDA(snd_hda_parse_nid_path);
1da177e4
LT
265
266/*
352f7f91
TI
267 * parse the path between the given NIDs and add to the path list.
268 * if no valid path is found, return NULL
1da177e4 269 */
352f7f91
TI
270struct nid_path *
271snd_hda_add_new_path(struct hda_codec *codec, hda_nid_t from_nid,
272 hda_nid_t to_nid, int with_aa_mix)
273{
274 struct hda_gen_spec *spec = codec->spec;
275 struct nid_path *path;
276
277 if (from_nid && to_nid && !is_reachable_path(codec, from_nid, to_nid))
278 return NULL;
279
280 path = snd_array_new(&spec->paths);
281 if (!path)
282 return NULL;
283 memset(path, 0, sizeof(*path));
284 if (snd_hda_parse_nid_path(codec, from_nid, to_nid, with_aa_mix, path))
285 return path;
286 /* push back */
287 spec->paths.used--;
288 return NULL;
1da177e4 289}
352f7f91 290EXPORT_SYMBOL_HDA(snd_hda_add_new_path);
1da177e4 291
352f7f91
TI
292/* look for an empty DAC slot */
293static hda_nid_t look_for_dac(struct hda_codec *codec, hda_nid_t pin,
294 bool is_digital)
295{
296 struct hda_gen_spec *spec = codec->spec;
297 bool cap_digital;
298 int i;
299
300 for (i = 0; i < spec->num_all_dacs; i++) {
301 hda_nid_t nid = spec->all_dacs[i];
302 if (!nid || is_dac_already_used(codec, nid))
303 continue;
304 cap_digital = !!(get_wcaps(codec, nid) & AC_WCAP_DIGITAL);
305 if (is_digital != cap_digital)
306 continue;
307 if (is_reachable_path(codec, nid, pin))
308 return nid;
309 }
82beb8fd 310 return 0;
1da177e4
LT
311}
312
352f7f91
TI
313/* replace the channels in the composed amp value with the given number */
314static unsigned int amp_val_replace_channels(unsigned int val, unsigned int chs)
1da177e4 315{
352f7f91
TI
316 val &= ~(0x3U << 16);
317 val |= chs << 16;
318 return val;
1da177e4
LT
319}
320
352f7f91
TI
321/* check whether the widget has the given amp capability for the direction */
322static bool check_amp_caps(struct hda_codec *codec, hda_nid_t nid,
323 int dir, unsigned int bits)
1da177e4 324{
352f7f91
TI
325 if (!nid)
326 return false;
327 if (get_wcaps(codec, nid) & (1 << (dir + 1)))
328 if (query_amp_caps(codec, nid, dir) & bits)
329 return true;
330 return false;
331}
332
333#define nid_has_mute(codec, nid, dir) \
334 check_amp_caps(codec, nid, dir, AC_AMPCAP_MUTE)
335#define nid_has_volume(codec, nid, dir) \
336 check_amp_caps(codec, nid, dir, AC_AMPCAP_NUM_STEPS)
337
338/* look for a widget suitable for assigning a mute switch in the path */
339static hda_nid_t look_for_out_mute_nid(struct hda_codec *codec,
340 struct nid_path *path)
341{
342 int i;
343
344 for (i = path->depth - 1; i >= 0; i--) {
345 if (nid_has_mute(codec, path->path[i], HDA_OUTPUT))
346 return path->path[i];
347 if (i != path->depth - 1 && i != 0 &&
348 nid_has_mute(codec, path->path[i], HDA_INPUT))
349 return path->path[i];
350 }
351 return 0;
352}
353
354/* look for a widget suitable for assigning a volume ctl in the path */
355static hda_nid_t look_for_out_vol_nid(struct hda_codec *codec,
356 struct nid_path *path)
357{
358 int i;
1da177e4 359
352f7f91
TI
360 for (i = path->depth - 1; i >= 0; i--) {
361 if (nid_has_volume(codec, path->path[i], HDA_OUTPUT))
362 return path->path[i];
1da177e4 363 }
352f7f91 364 return 0;
1da177e4
LT
365}
366
367/*
352f7f91 368 * path activation / deactivation
1da177e4 369 */
352f7f91
TI
370
371/* can have the amp-in capability? */
372static bool has_amp_in(struct hda_codec *codec, struct nid_path *path, int idx)
1da177e4 373{
352f7f91
TI
374 hda_nid_t nid = path->path[idx];
375 unsigned int caps = get_wcaps(codec, nid);
376 unsigned int type = get_wcaps_type(caps);
377
378 if (!(caps & AC_WCAP_IN_AMP))
379 return false;
380 if (type == AC_WID_PIN && idx > 0) /* only for input pins */
381 return false;
382 return true;
383}
1da177e4 384
352f7f91
TI
385/* can have the amp-out capability? */
386static bool has_amp_out(struct hda_codec *codec, struct nid_path *path, int idx)
387{
388 hda_nid_t nid = path->path[idx];
389 unsigned int caps = get_wcaps(codec, nid);
390 unsigned int type = get_wcaps_type(caps);
391
392 if (!(caps & AC_WCAP_OUT_AMP))
393 return false;
394 if (type == AC_WID_PIN && !idx) /* only for output pins */
395 return false;
396 return true;
397}
1da177e4 398
352f7f91
TI
399/* check whether the given (nid,dir,idx) is active */
400static bool is_active_nid(struct hda_codec *codec, hda_nid_t nid,
401 unsigned int idx, unsigned int dir)
402{
403 struct hda_gen_spec *spec = codec->spec;
404 int i, n;
1da177e4 405
352f7f91
TI
406 for (n = 0; n < spec->paths.used; n++) {
407 struct nid_path *path = snd_array_elem(&spec->paths, n);
408 if (!path->active)
1da177e4 409 continue;
352f7f91
TI
410 for (i = 0; i < path->depth; i++) {
411 if (path->path[i] == nid) {
412 if (dir == HDA_OUTPUT || path->idx[i] == idx)
413 return true;
414 break;
1da177e4 415 }
1da177e4
LT
416 }
417 }
352f7f91 418 return false;
1da177e4
LT
419}
420
352f7f91
TI
421/* get the default amp value for the target state */
422static int get_amp_val_to_activate(struct hda_codec *codec, hda_nid_t nid,
423 int dir, bool enable)
1da177e4 424{
352f7f91
TI
425 unsigned int caps;
426 unsigned int val = 0;
427
428 caps = query_amp_caps(codec, nid, dir);
429 if (caps & AC_AMPCAP_NUM_STEPS) {
430 /* set to 0dB */
431 if (enable)
432 val = (caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT;
1da177e4 433 }
352f7f91
TI
434 if (caps & AC_AMPCAP_MUTE) {
435 if (!enable)
436 val |= HDA_AMP_MUTE;
437 }
438 return val;
1da177e4
LT
439}
440
352f7f91
TI
441/* initialize the amp value (only at the first time) */
442static void init_amp(struct hda_codec *codec, hda_nid_t nid, int dir, int idx)
443{
444 int val = get_amp_val_to_activate(codec, nid, dir, false);
445 snd_hda_codec_amp_init_stereo(codec, nid, dir, idx, 0xff, val);
446}
1da177e4 447
352f7f91
TI
448static void activate_amp(struct hda_codec *codec, hda_nid_t nid, int dir,
449 int idx, bool enable)
450{
451 int val;
452 if (is_ctl_associated(codec, nid, dir, idx) ||
453 is_active_nid(codec, nid, dir, idx))
454 return;
455 val = get_amp_val_to_activate(codec, nid, dir, enable);
456 snd_hda_codec_amp_stereo(codec, nid, dir, idx, 0xff, val);
457}
458
459static void activate_amp_out(struct hda_codec *codec, struct nid_path *path,
460 int i, bool enable)
461{
462 hda_nid_t nid = path->path[i];
463 init_amp(codec, nid, HDA_OUTPUT, 0);
464 activate_amp(codec, nid, HDA_OUTPUT, 0, enable);
465}
466
467static void activate_amp_in(struct hda_codec *codec, struct nid_path *path,
468 int i, bool enable, bool add_aamix)
1da177e4 469{
352f7f91
TI
470 struct hda_gen_spec *spec = codec->spec;
471 hda_nid_t conn[16];
472 int n, nums, idx;
473 int type;
474 hda_nid_t nid = path->path[i];
475
476 nums = snd_hda_get_connections(codec, nid, conn, ARRAY_SIZE(conn));
477 type = get_wcaps_type(get_wcaps(codec, nid));
478 if (type == AC_WID_PIN ||
479 (type == AC_WID_AUD_IN && codec->single_adc_amp)) {
480 nums = 1;
481 idx = 0;
482 } else
483 idx = path->idx[i];
484
485 for (n = 0; n < nums; n++)
486 init_amp(codec, nid, HDA_INPUT, n);
487
488 if (is_ctl_associated(codec, nid, HDA_INPUT, idx))
489 return;
1da177e4 490
352f7f91
TI
491 /* here is a little bit tricky in comparison with activate_amp_out();
492 * when aa-mixer is available, we need to enable the path as well
1da177e4 493 */
352f7f91
TI
494 for (n = 0; n < nums; n++) {
495 if (n != idx && (!add_aamix || conn[n] != spec->mixer_nid))
496 continue;
497 activate_amp(codec, nid, HDA_INPUT, n, enable);
1da177e4 498 }
352f7f91 499}
1da177e4 500
352f7f91
TI
501/* activate or deactivate the given path
502 * if @add_aamix is set, enable the input from aa-mix NID as well (if any)
503 */
504void snd_hda_activate_path(struct hda_codec *codec, struct nid_path *path,
505 bool enable, bool add_aamix)
506{
507 int i;
508
509 if (!enable)
510 path->active = false;
511
512 for (i = path->depth - 1; i >= 0; i--) {
513 if (enable && path->multi[i])
514 snd_hda_codec_write_cache(codec, path->path[i], 0,
515 AC_VERB_SET_CONNECT_SEL,
516 path->idx[i]);
517 if (has_amp_in(codec, path, i))
518 activate_amp_in(codec, path, i, enable, add_aamix);
519 if (has_amp_out(codec, path, i))
520 activate_amp_out(codec, path, i, enable);
1da177e4
LT
521 }
522
352f7f91
TI
523 if (enable)
524 path->active = true;
1da177e4 525}
352f7f91
TI
526EXPORT_SYMBOL_HDA(snd_hda_activate_path);
527
d5a9f1bb
TI
528/* turn on/off EAPD on the given pin */
529static void set_pin_eapd(struct hda_codec *codec, hda_nid_t pin, bool enable)
530{
531 struct hda_gen_spec *spec = codec->spec;
532 if (spec->own_eapd_ctl ||
533 !(snd_hda_query_pin_caps(codec, pin) & AC_PINCAP_EAPD))
534 return;
535 snd_hda_codec_update_cache(codec, pin, 0,
536 AC_VERB_SET_EAPD_BTLENABLE,
537 enable ? 0x02 : 0x00);
538}
539
1da177e4
LT
540
541/*
352f7f91 542 * Helper functions for creating mixer ctl elements
1da177e4
LT
543 */
544
352f7f91
TI
545enum {
546 HDA_CTL_WIDGET_VOL,
547 HDA_CTL_WIDGET_MUTE,
548 HDA_CTL_BIND_MUTE,
549 HDA_CTL_BIND_VOL,
550 HDA_CTL_BIND_SW,
551};
552static const struct snd_kcontrol_new control_templates[] = {
553 HDA_CODEC_VOLUME(NULL, 0, 0, 0),
554 HDA_CODEC_MUTE(NULL, 0, 0, 0),
555 HDA_BIND_MUTE(NULL, 0, 0, 0),
556 HDA_BIND_VOL(NULL, 0),
557 HDA_BIND_SW(NULL, 0),
558};
1da177e4 559
352f7f91
TI
560/* add dynamic controls from template */
561static int add_control(struct hda_gen_spec *spec, int type, const char *name,
562 int cidx, unsigned long val)
1da177e4 563{
352f7f91 564 struct snd_kcontrol_new *knew;
1da177e4 565
12c93df6 566 knew = snd_hda_gen_add_kctl(spec, name, &control_templates[type]);
352f7f91
TI
567 if (!knew)
568 return -ENOMEM;
569 knew->index = cidx;
570 if (get_amp_nid_(val))
571 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
572 knew->private_value = val;
1da177e4
LT
573 return 0;
574}
575
352f7f91
TI
576static int add_control_with_pfx(struct hda_gen_spec *spec, int type,
577 const char *pfx, const char *dir,
578 const char *sfx, int cidx, unsigned long val)
1da177e4 579{
352f7f91
TI
580 char name[32];
581 snprintf(name, sizeof(name), "%s %s %s", pfx, dir, sfx);
582 return add_control(spec, type, name, cidx, val);
1da177e4
LT
583}
584
352f7f91
TI
585#define add_pb_vol_ctrl(spec, type, pfx, val) \
586 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", 0, val)
587#define add_pb_sw_ctrl(spec, type, pfx, val) \
588 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", 0, val)
589#define __add_pb_vol_ctrl(spec, type, pfx, cidx, val) \
590 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", cidx, val)
591#define __add_pb_sw_ctrl(spec, type, pfx, cidx, val) \
592 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", cidx, val)
593
594static int add_vol_ctl(struct hda_codec *codec, const char *pfx, int cidx,
595 unsigned int chs, struct nid_path *path)
596{
597 unsigned int val;
598 if (!path)
599 return 0;
600 val = path->ctls[NID_PATH_VOL_CTL];
601 if (!val)
602 return 0;
603 val = amp_val_replace_channels(val, chs);
604 return __add_pb_vol_ctrl(codec->spec, HDA_CTL_WIDGET_VOL, pfx, cidx, val);
605}
606
607/* return the channel bits suitable for the given path->ctls[] */
608static int get_default_ch_nums(struct hda_codec *codec, struct nid_path *path,
609 int type)
610{
611 int chs = 1; /* mono (left only) */
612 if (path) {
613 hda_nid_t nid = get_amp_nid_(path->ctls[type]);
614 if (nid && (get_wcaps(codec, nid) & AC_WCAP_STEREO))
615 chs = 3; /* stereo */
1da177e4 616 }
352f7f91 617 return chs;
1da177e4
LT
618}
619
352f7f91
TI
620static int add_stereo_vol(struct hda_codec *codec, const char *pfx, int cidx,
621 struct nid_path *path)
622{
623 int chs = get_default_ch_nums(codec, path, NID_PATH_VOL_CTL);
624 return add_vol_ctl(codec, pfx, cidx, chs, path);
625}
626
627/* create a mute-switch for the given mixer widget;
628 * if it has multiple sources (e.g. DAC and loopback), create a bind-mute
1da177e4 629 */
352f7f91
TI
630static int add_sw_ctl(struct hda_codec *codec, const char *pfx, int cidx,
631 unsigned int chs, struct nid_path *path)
1da177e4 632{
352f7f91
TI
633 unsigned int val;
634 int type = HDA_CTL_WIDGET_MUTE;
1da177e4 635
352f7f91 636 if (!path)
1da177e4 637 return 0;
352f7f91
TI
638 val = path->ctls[NID_PATH_MUTE_CTL];
639 if (!val)
1da177e4 640 return 0;
352f7f91
TI
641 val = amp_val_replace_channels(val, chs);
642 if (get_amp_direction_(val) == HDA_INPUT) {
643 hda_nid_t nid = get_amp_nid_(val);
644 int nums = snd_hda_get_num_conns(codec, nid);
645 if (nums > 1) {
646 type = HDA_CTL_BIND_MUTE;
647 val |= nums << 19;
648 }
1da177e4 649 }
352f7f91
TI
650 return __add_pb_sw_ctrl(codec->spec, type, pfx, cidx, val);
651}
1da177e4 652
352f7f91
TI
653static int add_stereo_sw(struct hda_codec *codec, const char *pfx,
654 int cidx, struct nid_path *path)
655{
656 int chs = get_default_ch_nums(codec, path, NID_PATH_MUTE_CTL);
657 return add_sw_ctl(codec, pfx, cidx, chs, path);
658}
1da177e4 659
352f7f91
TI
660static const char * const channel_name[4] = {
661 "Front", "Surround", "CLFE", "Side"
662};
97ec558a 663
352f7f91
TI
664/* give some appropriate ctl name prefix for the given line out channel */
665static const char *get_line_out_pfx(struct hda_gen_spec *spec, int ch,
666 bool can_be_master, int *index)
667{
668 struct auto_pin_cfg *cfg = &spec->autocfg;
1da177e4 669
352f7f91
TI
670 *index = 0;
671 if (cfg->line_outs == 1 && !spec->multi_ios &&
672 !cfg->hp_outs && !cfg->speaker_outs && can_be_master)
673 return spec->vmaster_mute.hook ? "PCM" : "Master";
1da177e4 674
352f7f91
TI
675 /* if there is really a single DAC used in the whole output paths,
676 * use it master (or "PCM" if a vmaster hook is present)
677 */
678 if (spec->multiout.num_dacs == 1 && !spec->mixer_nid &&
679 !spec->multiout.hp_out_nid[0] && !spec->multiout.extra_out_nid[0])
680 return spec->vmaster_mute.hook ? "PCM" : "Master";
681
682 switch (cfg->line_out_type) {
683 case AUTO_PIN_SPEAKER_OUT:
684 if (cfg->line_outs == 1)
685 return "Speaker";
686 if (cfg->line_outs == 2)
687 return ch ? "Bass Speaker" : "Speaker";
688 break;
689 case AUTO_PIN_HP_OUT:
690 /* for multi-io case, only the primary out */
691 if (ch && spec->multi_ios)
692 break;
693 *index = ch;
694 return "Headphone";
695 default:
696 if (cfg->line_outs == 1 && !spec->multi_ios)
697 return "PCM";
698 break;
699 }
700 if (ch >= ARRAY_SIZE(channel_name)) {
701 snd_BUG();
702 return "PCM";
1da177e4 703 }
1da177e4 704
352f7f91 705 return channel_name[ch];
1da177e4
LT
706}
707
708/*
352f7f91 709 * Parse output paths
1da177e4 710 */
352f7f91
TI
711
712/* badness definition */
713enum {
714 /* No primary DAC is found for the main output */
715 BAD_NO_PRIMARY_DAC = 0x10000,
716 /* No DAC is found for the extra output */
717 BAD_NO_DAC = 0x4000,
718 /* No possible multi-ios */
719 BAD_MULTI_IO = 0x103,
720 /* No individual DAC for extra output */
721 BAD_NO_EXTRA_DAC = 0x102,
722 /* No individual DAC for extra surrounds */
723 BAD_NO_EXTRA_SURR_DAC = 0x101,
724 /* Primary DAC shared with main surrounds */
725 BAD_SHARED_SURROUND = 0x100,
726 /* Primary DAC shared with main CLFE */
727 BAD_SHARED_CLFE = 0x10,
728 /* Primary DAC shared with extra surrounds */
729 BAD_SHARED_EXTRA_SURROUND = 0x10,
730 /* Volume widget is shared */
731 BAD_SHARED_VOL = 0x10,
732};
733
734/* look for widgets in the path between the given NIDs appropriate for
735 * volume and mute controls, and assign the values to ctls[].
736 *
737 * When no appropriate widget is found in the path, the badness value
738 * is incremented depending on the situation. The function returns the
739 * total badness for both volume and mute controls.
740 */
741static int assign_out_path_ctls(struct hda_codec *codec, hda_nid_t pin,
742 hda_nid_t dac)
1da177e4 743{
352f7f91
TI
744 struct nid_path *path = snd_hda_get_nid_path(codec, dac, pin);
745 hda_nid_t nid;
746 unsigned int val;
747 int badness = 0;
748
749 if (!path)
750 return BAD_SHARED_VOL * 2;
751 nid = look_for_out_vol_nid(codec, path);
752 if (nid) {
753 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
754 if (is_ctl_used(codec, val, NID_PATH_VOL_CTL))
755 badness += BAD_SHARED_VOL;
756 else
757 path->ctls[NID_PATH_VOL_CTL] = val;
758 } else
759 badness += BAD_SHARED_VOL;
760 nid = look_for_out_mute_nid(codec, path);
761 if (nid) {
762 unsigned int wid_type = get_wcaps_type(get_wcaps(codec, nid));
763 if (wid_type == AC_WID_PIN || wid_type == AC_WID_AUD_OUT ||
764 nid_has_mute(codec, nid, HDA_OUTPUT))
765 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
766 else
767 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT);
768 if (is_ctl_used(codec, val, NID_PATH_MUTE_CTL))
769 badness += BAD_SHARED_VOL;
770 else
771 path->ctls[NID_PATH_MUTE_CTL] = val;
772 } else
773 badness += BAD_SHARED_VOL;
774 return badness;
775}
1da177e4 776
352f7f91
TI
777struct badness_table {
778 int no_primary_dac; /* no primary DAC */
779 int no_dac; /* no secondary DACs */
780 int shared_primary; /* primary DAC is shared with main output */
781 int shared_surr; /* secondary DAC shared with main or primary */
782 int shared_clfe; /* third DAC shared with main or primary */
783 int shared_surr_main; /* secondary DAC sahred with main/DAC0 */
784};
1da177e4 785
352f7f91
TI
786static struct badness_table main_out_badness = {
787 .no_primary_dac = BAD_NO_PRIMARY_DAC,
788 .no_dac = BAD_NO_DAC,
789 .shared_primary = BAD_NO_PRIMARY_DAC,
790 .shared_surr = BAD_SHARED_SURROUND,
791 .shared_clfe = BAD_SHARED_CLFE,
792 .shared_surr_main = BAD_SHARED_SURROUND,
793};
794
795static struct badness_table extra_out_badness = {
796 .no_primary_dac = BAD_NO_DAC,
797 .no_dac = BAD_NO_DAC,
798 .shared_primary = BAD_NO_EXTRA_DAC,
799 .shared_surr = BAD_SHARED_EXTRA_SURROUND,
800 .shared_clfe = BAD_SHARED_EXTRA_SURROUND,
801 .shared_surr_main = BAD_NO_EXTRA_SURR_DAC,
802};
803
804/* try to assign DACs to pins and return the resultant badness */
805static int try_assign_dacs(struct hda_codec *codec, int num_outs,
806 const hda_nid_t *pins, hda_nid_t *dacs,
807 const struct badness_table *bad)
808{
809 struct hda_gen_spec *spec = codec->spec;
810 struct auto_pin_cfg *cfg = &spec->autocfg;
811 int i, j;
812 int badness = 0;
813 hda_nid_t dac;
814
815 if (!num_outs)
816 return 0;
817
818 for (i = 0; i < num_outs; i++) {
819 hda_nid_t pin = pins[i];
820 if (!dacs[i])
821 dacs[i] = look_for_dac(codec, pin, false);
822 if (!dacs[i] && !i) {
823 for (j = 1; j < num_outs; j++) {
824 if (is_reachable_path(codec, dacs[j], pin)) {
825 dacs[0] = dacs[j];
826 dacs[j] = 0;
827 break;
828 }
829 }
071c73ad 830 }
352f7f91
TI
831 dac = dacs[i];
832 if (!dac) {
833 if (is_reachable_path(codec, dacs[0], pin))
834 dac = dacs[0];
835 else if (cfg->line_outs > i &&
836 is_reachable_path(codec, spec->private_dac_nids[i], pin))
837 dac = spec->private_dac_nids[i];
838 if (dac) {
839 if (!i)
840 badness += bad->shared_primary;
841 else if (i == 1)
842 badness += bad->shared_surr;
843 else
844 badness += bad->shared_clfe;
845 } else if (is_reachable_path(codec, spec->private_dac_nids[0], pin)) {
846 dac = spec->private_dac_nids[0];
847 badness += bad->shared_surr_main;
848 } else if (!i)
849 badness += bad->no_primary_dac;
850 else
851 badness += bad->no_dac;
1da177e4 852 }
352f7f91
TI
853 if (!snd_hda_add_new_path(codec, dac, pin, 0))
854 dac = dacs[i] = 0;
855 if (dac)
856 badness += assign_out_path_ctls(codec, pin, dac);
1da177e4
LT
857 }
858
352f7f91 859 return badness;
1da177e4
LT
860}
861
352f7f91
TI
862/* return NID if the given pin has only a single connection to a certain DAC */
863static hda_nid_t get_dac_if_single(struct hda_codec *codec, hda_nid_t pin)
1da177e4 864{
352f7f91
TI
865 struct hda_gen_spec *spec = codec->spec;
866 int i;
867 hda_nid_t nid_found = 0;
1da177e4 868
352f7f91
TI
869 for (i = 0; i < spec->num_all_dacs; i++) {
870 hda_nid_t nid = spec->all_dacs[i];
871 if (!nid || is_dac_already_used(codec, nid))
872 continue;
873 if (is_reachable_path(codec, nid, pin)) {
874 if (nid_found)
1da177e4 875 return 0;
352f7f91 876 nid_found = nid;
1da177e4
LT
877 }
878 }
352f7f91 879 return nid_found;
1da177e4
LT
880}
881
352f7f91
TI
882/* check whether the given pin can be a multi-io pin */
883static bool can_be_multiio_pin(struct hda_codec *codec,
884 unsigned int location, hda_nid_t nid)
cb53c626 885{
352f7f91
TI
886 unsigned int defcfg, caps;
887
888 defcfg = snd_hda_codec_get_pincfg(codec, nid);
889 if (get_defcfg_connect(defcfg) != AC_JACK_PORT_COMPLEX)
890 return false;
891 if (location && get_defcfg_location(defcfg) != location)
892 return false;
893 caps = snd_hda_query_pin_caps(codec, nid);
894 if (!(caps & AC_PINCAP_OUT))
895 return false;
896 return true;
cb53c626 897}
cb53c626 898
1da177e4 899/*
352f7f91
TI
900 * multi-io helper
901 *
902 * When hardwired is set, try to fill ony hardwired pins, and returns
903 * zero if any pins are filled, non-zero if nothing found.
904 * When hardwired is off, try to fill possible input pins, and returns
905 * the badness value.
1da177e4 906 */
352f7f91
TI
907static int fill_multi_ios(struct hda_codec *codec,
908 hda_nid_t reference_pin,
909 bool hardwired, int offset)
1da177e4 910{
352f7f91
TI
911 struct hda_gen_spec *spec = codec->spec;
912 struct auto_pin_cfg *cfg = &spec->autocfg;
913 int type, i, j, dacs, num_pins, old_pins;
914 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
915 unsigned int location = get_defcfg_location(defcfg);
916 int badness = 0;
917
918 old_pins = spec->multi_ios;
919 if (old_pins >= 2)
920 goto end_fill;
921
922 num_pins = 0;
923 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
924 for (i = 0; i < cfg->num_inputs; i++) {
925 if (cfg->inputs[i].type != type)
926 continue;
927 if (can_be_multiio_pin(codec, location,
928 cfg->inputs[i].pin))
929 num_pins++;
930 }
1da177e4 931 }
352f7f91
TI
932 if (num_pins < 2)
933 goto end_fill;
1da177e4 934
352f7f91
TI
935 dacs = spec->multiout.num_dacs;
936 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
937 for (i = 0; i < cfg->num_inputs; i++) {
938 hda_nid_t nid = cfg->inputs[i].pin;
939 hda_nid_t dac = 0;
1da177e4 940
352f7f91
TI
941 if (cfg->inputs[i].type != type)
942 continue;
943 if (!can_be_multiio_pin(codec, location, nid))
944 continue;
945 for (j = 0; j < spec->multi_ios; j++) {
946 if (nid == spec->multi_io[j].pin)
947 break;
948 }
949 if (j < spec->multi_ios)
950 continue;
951
952 if (offset && offset + spec->multi_ios < dacs) {
953 dac = spec->private_dac_nids[offset + spec->multi_ios];
954 if (!is_reachable_path(codec, dac, nid))
955 dac = 0;
956 }
957 if (hardwired)
958 dac = get_dac_if_single(codec, nid);
959 else if (!dac)
960 dac = look_for_dac(codec, nid, false);
961 if (!dac) {
962 badness++;
963 continue;
964 }
965 if (!snd_hda_add_new_path(codec, dac, nid, 0)) {
966 badness++;
967 continue;
968 }
969 spec->multi_io[spec->multi_ios].pin = nid;
970 spec->multi_io[spec->multi_ios].dac = dac;
971 spec->multi_ios++;
972 if (spec->multi_ios >= 2)
973 break;
974 }
975 }
976 end_fill:
977 if (badness)
978 badness = BAD_MULTI_IO;
979 if (old_pins == spec->multi_ios) {
980 if (hardwired)
981 return 1; /* nothing found */
982 else
983 return badness; /* no badness if nothing found */
984 }
985 if (!hardwired && spec->multi_ios < 2) {
986 /* cancel newly assigned paths */
987 spec->paths.used -= spec->multi_ios - old_pins;
988 spec->multi_ios = old_pins;
989 return badness;
990 }
991
992 /* assign volume and mute controls */
993 for (i = old_pins; i < spec->multi_ios; i++)
994 badness += assign_out_path_ctls(codec, spec->multi_io[i].pin,
995 spec->multi_io[i].dac);
996
997 return badness;
998}
999
1000/* map DACs for all pins in the list if they are single connections */
1001static bool map_singles(struct hda_codec *codec, int outs,
1002 const hda_nid_t *pins, hda_nid_t *dacs)
1003{
1004 int i;
1005 bool found = false;
1006 for (i = 0; i < outs; i++) {
1007 hda_nid_t dac;
1008 if (dacs[i])
1009 continue;
1010 dac = get_dac_if_single(codec, pins[i]);
1011 if (!dac)
1012 continue;
1013 if (snd_hda_add_new_path(codec, dac, pins[i], 0)) {
1014 dacs[i] = dac;
1015 found = true;
1016 }
1017 }
1018 return found;
1019}
1020
1021/* fill in the dac_nids table from the parsed pin configuration */
1022static int fill_and_eval_dacs(struct hda_codec *codec,
1023 bool fill_hardwired,
1024 bool fill_mio_first)
1025{
1026 struct hda_gen_spec *spec = codec->spec;
1027 struct auto_pin_cfg *cfg = &spec->autocfg;
1028 int i, err, badness;
1029
1030 /* set num_dacs once to full for look_for_dac() */
1031 spec->multiout.num_dacs = cfg->line_outs;
1032 spec->multiout.dac_nids = spec->private_dac_nids;
1033 memset(spec->private_dac_nids, 0, sizeof(spec->private_dac_nids));
1034 memset(spec->multiout.hp_out_nid, 0, sizeof(spec->multiout.hp_out_nid));
1035 memset(spec->multiout.extra_out_nid, 0, sizeof(spec->multiout.extra_out_nid));
1036 spec->multi_ios = 0;
1037 snd_array_free(&spec->paths);
1038 badness = 0;
1039
1040 /* fill hard-wired DACs first */
1041 if (fill_hardwired) {
1042 bool mapped;
1043 do {
1044 mapped = map_singles(codec, cfg->line_outs,
1045 cfg->line_out_pins,
1046 spec->private_dac_nids);
1047 mapped |= map_singles(codec, cfg->hp_outs,
1048 cfg->hp_pins,
1049 spec->multiout.hp_out_nid);
1050 mapped |= map_singles(codec, cfg->speaker_outs,
1051 cfg->speaker_pins,
1052 spec->multiout.extra_out_nid);
1053 if (fill_mio_first && cfg->line_outs == 1 &&
1054 cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1055 err = fill_multi_ios(codec, cfg->line_out_pins[0], true, 0);
1056 if (!err)
1057 mapped = true;
1058 }
1059 } while (mapped);
1060 }
1061
1062 badness += try_assign_dacs(codec, cfg->line_outs, cfg->line_out_pins,
1063 spec->private_dac_nids,
1064 &main_out_badness);
1065
1066 /* re-count num_dacs and squash invalid entries */
1067 spec->multiout.num_dacs = 0;
1068 for (i = 0; i < cfg->line_outs; i++) {
1069 if (spec->private_dac_nids[i])
1070 spec->multiout.num_dacs++;
1071 else {
1072 memmove(spec->private_dac_nids + i,
1073 spec->private_dac_nids + i + 1,
1074 sizeof(hda_nid_t) * (cfg->line_outs - i - 1));
1075 spec->private_dac_nids[cfg->line_outs - 1] = 0;
1076 }
1077 }
1078
1079 if (fill_mio_first &&
1080 cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1081 /* try to fill multi-io first */
1082 err = fill_multi_ios(codec, cfg->line_out_pins[0], false, 0);
1083 if (err < 0)
1084 return err;
1085 /* we don't count badness at this stage yet */
1086 }
1087
1088 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
1089 err = try_assign_dacs(codec, cfg->hp_outs, cfg->hp_pins,
1090 spec->multiout.hp_out_nid,
1091 &extra_out_badness);
1092 if (err < 0)
1093 return err;
1094 badness += err;
1095 }
1096 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1097 err = try_assign_dacs(codec, cfg->speaker_outs,
1098 cfg->speaker_pins,
1099 spec->multiout.extra_out_nid,
1100 &extra_out_badness);
1101 if (err < 0)
1102 return err;
1103 badness += err;
1104 }
1105 if (cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1106 err = fill_multi_ios(codec, cfg->line_out_pins[0], false, 0);
1107 if (err < 0)
1108 return err;
1109 badness += err;
1110 }
1111 if (cfg->hp_outs && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
1112 /* try multi-ios with HP + inputs */
1113 int offset = 0;
1114 if (cfg->line_outs >= 3)
1115 offset = 1;
1116 err = fill_multi_ios(codec, cfg->hp_pins[0], false, offset);
1117 if (err < 0)
1118 return err;
1119 badness += err;
1120 }
1121
1122 if (spec->multi_ios == 2) {
1123 for (i = 0; i < 2; i++)
1124 spec->private_dac_nids[spec->multiout.num_dacs++] =
1125 spec->multi_io[i].dac;
1126 spec->ext_channel_count = 2;
1127 } else if (spec->multi_ios) {
1128 spec->multi_ios = 0;
1129 badness += BAD_MULTI_IO;
1130 }
1131
1132 return badness;
1133}
1134
1135#define DEBUG_BADNESS
1136
1137#ifdef DEBUG_BADNESS
1138#define debug_badness snd_printdd
1139#else
1140#define debug_badness(...)
1141#endif
1142
1143static void debug_show_configs(struct hda_gen_spec *spec, struct auto_pin_cfg *cfg)
1144{
1145 debug_badness("multi_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1146 cfg->line_out_pins[0], cfg->line_out_pins[1],
1147 cfg->line_out_pins[2], cfg->line_out_pins[2],
1148 spec->multiout.dac_nids[0],
1149 spec->multiout.dac_nids[1],
1150 spec->multiout.dac_nids[2],
1151 spec->multiout.dac_nids[3]);
1152 if (spec->multi_ios > 0)
1153 debug_badness("multi_ios(%d) = %x/%x : %x/%x\n",
1154 spec->multi_ios,
1155 spec->multi_io[0].pin, spec->multi_io[1].pin,
1156 spec->multi_io[0].dac, spec->multi_io[1].dac);
1157 debug_badness("hp_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1158 cfg->hp_pins[0], cfg->hp_pins[1],
1159 cfg->hp_pins[2], cfg->hp_pins[2],
1160 spec->multiout.hp_out_nid[0],
1161 spec->multiout.hp_out_nid[1],
1162 spec->multiout.hp_out_nid[2],
1163 spec->multiout.hp_out_nid[3]);
1164 debug_badness("spk_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1165 cfg->speaker_pins[0], cfg->speaker_pins[1],
1166 cfg->speaker_pins[2], cfg->speaker_pins[3],
1167 spec->multiout.extra_out_nid[0],
1168 spec->multiout.extra_out_nid[1],
1169 spec->multiout.extra_out_nid[2],
1170 spec->multiout.extra_out_nid[3]);
1171}
1172
1173/* find all available DACs of the codec */
1174static void fill_all_dac_nids(struct hda_codec *codec)
1175{
1176 struct hda_gen_spec *spec = codec->spec;
1177 int i;
1178 hda_nid_t nid = codec->start_nid;
1179
1180 spec->num_all_dacs = 0;
1181 memset(spec->all_dacs, 0, sizeof(spec->all_dacs));
1182 for (i = 0; i < codec->num_nodes; i++, nid++) {
1183 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_AUD_OUT)
1184 continue;
1185 if (spec->num_all_dacs >= ARRAY_SIZE(spec->all_dacs)) {
1186 snd_printk(KERN_ERR "hda: Too many DACs!\n");
1187 break;
1188 }
1189 spec->all_dacs[spec->num_all_dacs++] = nid;
1190 }
1191}
1192
1193static int parse_output_paths(struct hda_codec *codec)
1194{
1195 struct hda_gen_spec *spec = codec->spec;
1196 struct auto_pin_cfg *cfg = &spec->autocfg;
1197 struct auto_pin_cfg *best_cfg;
1198 int best_badness = INT_MAX;
1199 int badness;
1200 bool fill_hardwired = true, fill_mio_first = true;
1201 bool best_wired = true, best_mio = true;
1202 bool hp_spk_swapped = false;
1203
1204 fill_all_dac_nids(codec);
1205
1206 best_cfg = kmalloc(sizeof(*best_cfg), GFP_KERNEL);
1207 if (!best_cfg)
1208 return -ENOMEM;
1209 *best_cfg = *cfg;
1210
1211 for (;;) {
1212 badness = fill_and_eval_dacs(codec, fill_hardwired,
1213 fill_mio_first);
1214 if (badness < 0) {
1215 kfree(best_cfg);
1216 return badness;
1217 }
1218 debug_badness("==> lo_type=%d, wired=%d, mio=%d, badness=0x%x\n",
1219 cfg->line_out_type, fill_hardwired, fill_mio_first,
1220 badness);
1221 debug_show_configs(spec, cfg);
1222 if (badness < best_badness) {
1223 best_badness = badness;
1224 *best_cfg = *cfg;
1225 best_wired = fill_hardwired;
1226 best_mio = fill_mio_first;
1227 }
1228 if (!badness)
1229 break;
1230 fill_mio_first = !fill_mio_first;
1231 if (!fill_mio_first)
1232 continue;
1233 fill_hardwired = !fill_hardwired;
1234 if (!fill_hardwired)
1235 continue;
1236 if (hp_spk_swapped)
1237 break;
1238 hp_spk_swapped = true;
1239 if (cfg->speaker_outs > 0 &&
1240 cfg->line_out_type == AUTO_PIN_HP_OUT) {
1241 cfg->hp_outs = cfg->line_outs;
1242 memcpy(cfg->hp_pins, cfg->line_out_pins,
1243 sizeof(cfg->hp_pins));
1244 cfg->line_outs = cfg->speaker_outs;
1245 memcpy(cfg->line_out_pins, cfg->speaker_pins,
1246 sizeof(cfg->speaker_pins));
1247 cfg->speaker_outs = 0;
1248 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
1249 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
1250 fill_hardwired = true;
1251 continue;
1252 }
1253 if (cfg->hp_outs > 0 &&
1254 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
1255 cfg->speaker_outs = cfg->line_outs;
1256 memcpy(cfg->speaker_pins, cfg->line_out_pins,
1257 sizeof(cfg->speaker_pins));
1258 cfg->line_outs = cfg->hp_outs;
1259 memcpy(cfg->line_out_pins, cfg->hp_pins,
1260 sizeof(cfg->hp_pins));
1261 cfg->hp_outs = 0;
1262 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
1263 cfg->line_out_type = AUTO_PIN_HP_OUT;
1264 fill_hardwired = true;
1265 continue;
1266 }
1267 break;
1268 }
1269
1270 if (badness) {
1271 *cfg = *best_cfg;
1272 fill_and_eval_dacs(codec, best_wired, best_mio);
1273 }
1274 debug_badness("==> Best config: lo_type=%d, wired=%d, mio=%d\n",
1275 cfg->line_out_type, best_wired, best_mio);
1276 debug_show_configs(spec, cfg);
1277
1278 if (cfg->line_out_pins[0]) {
1279 struct nid_path *path;
1280 path = snd_hda_get_nid_path(codec,
1281 spec->multiout.dac_nids[0],
1282 cfg->line_out_pins[0]);
1283 if (path)
1284 spec->vmaster_nid = look_for_out_vol_nid(codec, path);
1285 }
1286
1287 kfree(best_cfg);
1288 return 0;
1289}
1290
1291/* add playback controls from the parsed DAC table */
1292static int create_multi_out_ctls(struct hda_codec *codec,
1293 const struct auto_pin_cfg *cfg)
1294{
1295 struct hda_gen_spec *spec = codec->spec;
1296 int i, err, noutputs;
1297
1298 noutputs = cfg->line_outs;
1299 if (spec->multi_ios > 0 && cfg->line_outs < 3)
1300 noutputs += spec->multi_ios;
1301
1302 for (i = 0; i < noutputs; i++) {
1303 const char *name;
1304 int index;
1305 hda_nid_t dac, pin;
1306 struct nid_path *path;
1307
1308 dac = spec->multiout.dac_nids[i];
1309 if (!dac)
1310 continue;
1311 if (i >= cfg->line_outs) {
1312 pin = spec->multi_io[i - 1].pin;
1313 index = 0;
1314 name = channel_name[i];
1315 } else {
1316 pin = cfg->line_out_pins[i];
1317 name = get_line_out_pfx(spec, i, true, &index);
1318 }
1319
1320 path = snd_hda_get_nid_path(codec, dac, pin);
1321 if (!path)
1322 continue;
1323 if (!name || !strcmp(name, "CLFE")) {
1324 /* Center/LFE */
1325 err = add_vol_ctl(codec, "Center", 0, 1, path);
1326 if (err < 0)
1327 return err;
1328 err = add_vol_ctl(codec, "LFE", 0, 2, path);
1329 if (err < 0)
1330 return err;
1331 err = add_sw_ctl(codec, "Center", 0, 1, path);
1332 if (err < 0)
1333 return err;
1334 err = add_sw_ctl(codec, "LFE", 0, 2, path);
1335 if (err < 0)
1336 return err;
1337 } else {
1338 err = add_stereo_vol(codec, name, index, path);
1339 if (err < 0)
1340 return err;
1341 err = add_stereo_sw(codec, name, index, path);
1342 if (err < 0)
1343 return err;
1344 }
1345 }
1346 return 0;
1347}
1348
1349static int create_extra_out(struct hda_codec *codec, hda_nid_t pin,
1350 hda_nid_t dac, const char *pfx, int cidx)
1351{
1352 struct nid_path *path;
1353 int err;
1354
1355 path = snd_hda_get_nid_path(codec, dac, pin);
1356 if (!path)
1357 return 0;
1358 /* bind volume control will be created in the case of dac = 0 */
1359 if (dac) {
1360 err = add_stereo_vol(codec, pfx, cidx, path);
1361 if (err < 0)
1362 return err;
1363 }
1364 err = add_stereo_sw(codec, pfx, cidx, path);
1365 if (err < 0)
1366 return err;
1367 return 0;
1368}
1369
1370/* add playback controls for speaker and HP outputs */
1371static int create_extra_outs(struct hda_codec *codec, int num_pins,
1372 const hda_nid_t *pins, const hda_nid_t *dacs,
1373 const char *pfx)
1374{
1375 struct hda_gen_spec *spec = codec->spec;
1376 struct hda_bind_ctls *ctl;
1377 char name[32];
1378 int i, n, err;
1379
1380 if (!num_pins || !pins[0])
1381 return 0;
1382
1383 if (num_pins == 1) {
1384 hda_nid_t dac = *dacs;
1385 if (!dac)
1386 dac = spec->multiout.dac_nids[0];
1387 return create_extra_out(codec, *pins, dac, pfx, 0);
1388 }
1389
1390 for (i = 0; i < num_pins; i++) {
1391 hda_nid_t dac;
1392 if (dacs[num_pins - 1])
1393 dac = dacs[i]; /* with individual volumes */
1394 else
1395 dac = 0;
1396 if (num_pins == 2 && i == 1 && !strcmp(pfx, "Speaker")) {
1397 err = create_extra_out(codec, pins[i], dac,
1398 "Bass Speaker", 0);
1399 } else if (num_pins >= 3) {
1400 snprintf(name, sizeof(name), "%s %s",
1401 pfx, channel_name[i]);
1402 err = create_extra_out(codec, pins[i], dac, name, 0);
1403 } else {
1404 err = create_extra_out(codec, pins[i], dac, pfx, i);
1405 }
1406 if (err < 0)
1407 return err;
1408 }
1409 if (dacs[num_pins - 1])
1410 return 0;
1411
1412 /* Let's create a bind-controls for volumes */
1413 ctl = new_bind_ctl(codec, num_pins, &snd_hda_bind_vol);
1414 if (!ctl)
1415 return -ENOMEM;
1416 n = 0;
1417 for (i = 0; i < num_pins; i++) {
1418 hda_nid_t vol;
1419 struct nid_path *path;
1420 if (!pins[i] || !dacs[i])
1421 continue;
1422 path = snd_hda_get_nid_path(codec, dacs[i], pins[i]);
1423 if (!path)
1424 continue;
1425 vol = look_for_out_vol_nid(codec, path);
1426 if (vol)
1427 ctl->values[n++] =
1428 HDA_COMPOSE_AMP_VAL(vol, 3, 0, HDA_OUTPUT);
1429 }
1430 if (n) {
1431 snprintf(name, sizeof(name), "%s Playback Volume", pfx);
1432 err = add_control(spec, HDA_CTL_BIND_VOL, name, 0, (long)ctl);
1433 if (err < 0)
1434 return err;
1435 }
1436 return 0;
1437}
1438
1439static int create_hp_out_ctls(struct hda_codec *codec)
1440{
1441 struct hda_gen_spec *spec = codec->spec;
1442 return create_extra_outs(codec, spec->autocfg.hp_outs,
1443 spec->autocfg.hp_pins,
1444 spec->multiout.hp_out_nid,
1445 "Headphone");
1446}
1447
1448static int create_speaker_out_ctls(struct hda_codec *codec)
1449{
1450 struct hda_gen_spec *spec = codec->spec;
1451 return create_extra_outs(codec, spec->autocfg.speaker_outs,
1452 spec->autocfg.speaker_pins,
1453 spec->multiout.extra_out_nid,
1454 "Speaker");
1455}
1456
1457/*
1458 * channel mode enum control
1459 */
1460
1461static int ch_mode_info(struct snd_kcontrol *kcontrol,
1462 struct snd_ctl_elem_info *uinfo)
1463{
1464 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1465 struct hda_gen_spec *spec = codec->spec;
1466
1467 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1468 uinfo->count = 1;
1469 uinfo->value.enumerated.items = spec->multi_ios + 1;
1470 if (uinfo->value.enumerated.item > spec->multi_ios)
1471 uinfo->value.enumerated.item = spec->multi_ios;
1472 sprintf(uinfo->value.enumerated.name, "%dch",
1473 (uinfo->value.enumerated.item + 1) * 2);
1474 return 0;
1475}
1476
1477static int ch_mode_get(struct snd_kcontrol *kcontrol,
1478 struct snd_ctl_elem_value *ucontrol)
1479{
1480 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1481 struct hda_gen_spec *spec = codec->spec;
1482 ucontrol->value.enumerated.item[0] = (spec->ext_channel_count - 1) / 2;
1483 return 0;
1484}
1485
1486static int set_multi_io(struct hda_codec *codec, int idx, bool output)
1487{
1488 struct hda_gen_spec *spec = codec->spec;
1489 hda_nid_t nid = spec->multi_io[idx].pin;
1490 struct nid_path *path;
1491
1492 path = snd_hda_get_nid_path(codec, spec->multi_io[idx].dac, nid);
1493 if (!path)
1494 return -EINVAL;
1495
1496 if (path->active == output)
1497 return 0;
1498
1499 if (output) {
1500 snd_hda_set_pin_ctl_cache(codec, nid, PIN_OUT);
1501 snd_hda_activate_path(codec, path, true, true);
d5a9f1bb 1502 set_pin_eapd(codec, nid, true);
352f7f91 1503 } else {
d5a9f1bb 1504 set_pin_eapd(codec, nid, false);
352f7f91
TI
1505 snd_hda_activate_path(codec, path, false, true);
1506 snd_hda_set_pin_ctl_cache(codec, nid,
1507 spec->multi_io[idx].ctl_in);
1508 }
1509 return 0;
1510}
1511
1512static int ch_mode_put(struct snd_kcontrol *kcontrol,
1513 struct snd_ctl_elem_value *ucontrol)
1514{
1515 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1516 struct hda_gen_spec *spec = codec->spec;
1517 int i, ch;
1518
1519 ch = ucontrol->value.enumerated.item[0];
1520 if (ch < 0 || ch > spec->multi_ios)
1521 return -EINVAL;
1522 if (ch == (spec->ext_channel_count - 1) / 2)
1523 return 0;
1524 spec->ext_channel_count = (ch + 1) * 2;
1525 for (i = 0; i < spec->multi_ios; i++)
1526 set_multi_io(codec, i, i < ch);
1527 spec->multiout.max_channels = max(spec->ext_channel_count,
1528 spec->const_channel_count);
1529 if (spec->need_dac_fix)
1530 spec->multiout.num_dacs = spec->multiout.max_channels / 2;
1531 return 1;
1532}
1533
1534static const struct snd_kcontrol_new channel_mode_enum = {
1535 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1536 .name = "Channel Mode",
1537 .info = ch_mode_info,
1538 .get = ch_mode_get,
1539 .put = ch_mode_put,
1540};
1541
1542static int create_multi_channel_mode(struct hda_codec *codec)
1543{
1544 struct hda_gen_spec *spec = codec->spec;
1545
1546 if (spec->multi_ios > 0) {
12c93df6 1547 if (!snd_hda_gen_add_kctl(spec, NULL, &channel_mode_enum))
352f7f91
TI
1548 return -ENOMEM;
1549 }
1550 return 0;
1551}
1552
1553/*
1554 * shared headphone/mic handling
1555 */
1556
1557static void call_update_outputs(struct hda_codec *codec);
1558
1559/* for shared I/O, change the pin-control accordingly */
1560static void update_shared_mic_hp(struct hda_codec *codec, bool set_as_mic)
1561{
1562 struct hda_gen_spec *spec = codec->spec;
1563 unsigned int val;
1564 hda_nid_t pin = spec->autocfg.inputs[1].pin;
1565 /* NOTE: this assumes that there are only two inputs, the
1566 * first is the real internal mic and the second is HP/mic jack.
1567 */
1568
1569 val = snd_hda_get_default_vref(codec, pin);
1570
1571 /* This pin does not have vref caps - let's enable vref on pin 0x18
1572 instead, as suggested by Realtek */
1573 if (val == AC_PINCTL_VREF_HIZ && spec->shared_mic_vref_pin) {
1574 const hda_nid_t vref_pin = spec->shared_mic_vref_pin;
1575 unsigned int vref_val = snd_hda_get_default_vref(codec, vref_pin);
1576 if (vref_val != AC_PINCTL_VREF_HIZ)
7594aa33
TI
1577 snd_hda_set_pin_ctl_cache(codec, vref_pin,
1578 PIN_IN | (set_as_mic ? vref_val : 0));
352f7f91
TI
1579 }
1580
1581 val = set_as_mic ? val | PIN_IN : PIN_HP;
7594aa33 1582 snd_hda_set_pin_ctl_cache(codec, pin, val);
352f7f91
TI
1583
1584 spec->automute_speaker = !set_as_mic;
1585 call_update_outputs(codec);
1586}
1587
1588/* create a shared input with the headphone out */
1589static int create_shared_input(struct hda_codec *codec)
1590{
1591 struct hda_gen_spec *spec = codec->spec;
1592 struct auto_pin_cfg *cfg = &spec->autocfg;
1593 unsigned int defcfg;
1594 hda_nid_t nid;
1595
1596 /* only one internal input pin? */
1597 if (cfg->num_inputs != 1)
1598 return 0;
1599 defcfg = snd_hda_codec_get_pincfg(codec, cfg->inputs[0].pin);
1600 if (snd_hda_get_input_pin_attr(defcfg) != INPUT_PIN_ATTR_INT)
1601 return 0;
1602
1603 if (cfg->hp_outs == 1 && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
1604 nid = cfg->hp_pins[0]; /* OK, we have a single HP-out */
1605 else if (cfg->line_outs == 1 && cfg->line_out_type == AUTO_PIN_HP_OUT)
1606 nid = cfg->line_out_pins[0]; /* OK, we have a single line-out */
1607 else
1608 return 0; /* both not available */
1609
1610 if (!(snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_IN))
1611 return 0; /* no input */
1612
1613 cfg->inputs[1].pin = nid;
1614 cfg->inputs[1].type = AUTO_PIN_MIC;
1615 cfg->num_inputs = 2;
1616 spec->shared_mic_hp = 1;
1617 snd_printdd("hda-codec: Enable shared I/O jack on NID 0x%x\n", nid);
1618 return 0;
1619}
1620
1621
1622/*
1623 * Parse input paths
1624 */
1625
1626#ifdef CONFIG_PM
1627/* add the powersave loopback-list entry */
1628static void add_loopback_list(struct hda_gen_spec *spec, hda_nid_t mix, int idx)
1629{
1630 struct hda_amp_list *list;
1631
1632 if (spec->num_loopbacks >= ARRAY_SIZE(spec->loopback_list) - 1)
1633 return;
1634 list = spec->loopback_list + spec->num_loopbacks;
1635 list->nid = mix;
1636 list->dir = HDA_INPUT;
1637 list->idx = idx;
1638 spec->num_loopbacks++;
1639 spec->loopback.amplist = spec->loopback_list;
1640}
1641#else
1642#define add_loopback_list(spec, mix, idx) /* NOP */
1643#endif
1644
1645/* create input playback/capture controls for the given pin */
1646static int new_analog_input(struct hda_codec *codec, hda_nid_t pin,
1647 const char *ctlname, int ctlidx,
1648 hda_nid_t mix_nid)
1649{
1650 struct hda_gen_spec *spec = codec->spec;
1651 struct nid_path *path;
1652 unsigned int val;
1653 int err, idx;
1654
1655 if (!nid_has_volume(codec, mix_nid, HDA_INPUT) &&
1656 !nid_has_mute(codec, mix_nid, HDA_INPUT))
1657 return 0; /* no need for analog loopback */
1658
1659 path = snd_hda_add_new_path(codec, pin, mix_nid, 2);
1660 if (!path)
1661 return -EINVAL;
1662
1663 idx = path->idx[path->depth - 1];
1664 if (nid_has_volume(codec, mix_nid, HDA_INPUT)) {
1665 val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
1666 err = __add_pb_vol_ctrl(spec, HDA_CTL_WIDGET_VOL, ctlname, ctlidx, val);
1667 if (err < 0)
1668 return err;
1669 path->ctls[NID_PATH_VOL_CTL] = val;
1670 }
1671
1672 if (nid_has_mute(codec, mix_nid, HDA_INPUT)) {
1673 val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
1674 err = __add_pb_sw_ctrl(spec, HDA_CTL_WIDGET_MUTE, ctlname, ctlidx, val);
1675 if (err < 0)
1676 return err;
1677 path->ctls[NID_PATH_MUTE_CTL] = val;
1678 }
1679
1680 path->active = true;
1681 add_loopback_list(spec, mix_nid, idx);
1682 return 0;
1683}
1684
1685static int is_input_pin(struct hda_codec *codec, hda_nid_t nid)
1686{
1687 unsigned int pincap = snd_hda_query_pin_caps(codec, nid);
1688 return (pincap & AC_PINCAP_IN) != 0;
1689}
1690
1691/* Parse the codec tree and retrieve ADCs */
1692static int fill_adc_nids(struct hda_codec *codec)
1693{
1694 struct hda_gen_spec *spec = codec->spec;
1695 hda_nid_t nid;
1696 hda_nid_t *adc_nids = spec->adc_nids;
1697 int max_nums = ARRAY_SIZE(spec->adc_nids);
1698 int i, nums = 0;
1699
1700 nid = codec->start_nid;
1701 for (i = 0; i < codec->num_nodes; i++, nid++) {
1702 unsigned int caps = get_wcaps(codec, nid);
1703 int type = get_wcaps_type(caps);
1704
1705 if (type != AC_WID_AUD_IN || (caps & AC_WCAP_DIGITAL))
1706 continue;
1707 adc_nids[nums] = nid;
1708 if (++nums >= max_nums)
1709 break;
1710 }
1711 spec->num_adc_nids = nums;
1712 return nums;
1713}
1714
1715/* filter out invalid adc_nids that don't give all active input pins;
1716 * if needed, check whether dynamic ADC-switching is available
1717 */
1718static int check_dyn_adc_switch(struct hda_codec *codec)
1719{
1720 struct hda_gen_spec *spec = codec->spec;
1721 struct hda_input_mux *imux = &spec->input_mux;
1722 hda_nid_t adc_nids[ARRAY_SIZE(spec->adc_nids)];
1723 int i, n, nums;
1724 hda_nid_t pin, adc;
1725
1726 again:
1727 nums = 0;
1728 for (n = 0; n < spec->num_adc_nids; n++) {
1729 adc = spec->adc_nids[n];
1730 for (i = 0; i < imux->num_items; i++) {
1731 pin = spec->imux_pins[i];
1732 if (!is_reachable_path(codec, pin, adc))
1733 break;
1734 }
1735 if (i >= imux->num_items)
1736 adc_nids[nums++] = adc;
1737 }
1738
1739 if (!nums) {
1740 if (spec->shared_mic_hp) {
1741 spec->shared_mic_hp = 0;
1742 imux->num_items = 1;
1743 goto again;
1744 }
1745
1746 /* check whether ADC-switch is possible */
1747 for (i = 0; i < imux->num_items; i++) {
1748 pin = spec->imux_pins[i];
1749 for (n = 0; n < spec->num_adc_nids; n++) {
1750 adc = spec->adc_nids[n];
1751 if (is_reachable_path(codec, pin, adc)) {
1752 spec->dyn_adc_idx[i] = n;
1753 break;
1754 }
1755 }
1756 }
1757
1758 snd_printdd("hda-codec: enabling ADC switching\n");
1759 spec->dyn_adc_switch = 1;
1760 } else if (nums != spec->num_adc_nids) {
1761 memcpy(spec->adc_nids, adc_nids, nums * sizeof(hda_nid_t));
1762 spec->num_adc_nids = nums;
1763 }
1764
1765 if (imux->num_items == 1 || spec->shared_mic_hp) {
1766 snd_printdd("hda-codec: reducing to a single ADC\n");
1767 spec->num_adc_nids = 1; /* reduce to a single ADC */
1768 }
1769
1770 /* single index for individual volumes ctls */
1771 if (!spec->dyn_adc_switch && spec->multi_cap_vol)
1772 spec->num_adc_nids = 1;
1773
1774 return 0;
1775}
1776
1777/*
1778 * create playback/capture controls for input pins
1779 */
1780static int create_input_ctls(struct hda_codec *codec)
1781{
1782 struct hda_gen_spec *spec = codec->spec;
1783 const struct auto_pin_cfg *cfg = &spec->autocfg;
1784 hda_nid_t mixer = spec->mixer_nid;
1785 struct hda_input_mux *imux = &spec->input_mux;
1786 int num_adcs;
1787 int i, c, err, type_idx = 0;
1788 const char *prev_label = NULL;
1789
1790 num_adcs = fill_adc_nids(codec);
1791 if (num_adcs < 0)
1792 return 0;
1793
1794 for (i = 0; i < cfg->num_inputs; i++) {
1795 hda_nid_t pin;
1796 const char *label;
1797 bool imux_added;
1798
1799 pin = cfg->inputs[i].pin;
1800 if (!is_input_pin(codec, pin))
1801 continue;
1802
1803 label = hda_get_autocfg_input_label(codec, cfg, i);
1804 if (spec->shared_mic_hp && !strcmp(label, "Misc"))
1805 label = "Headphone Mic";
1806 if (prev_label && !strcmp(label, prev_label))
1807 type_idx++;
1808 else
1809 type_idx = 0;
1810 prev_label = label;
1811
1812 if (mixer) {
1813 if (is_reachable_path(codec, pin, mixer)) {
1814 err = new_analog_input(codec, pin,
1815 label, type_idx, mixer);
1816 if (err < 0)
1817 return err;
1818 }
1819 }
1820
1821 imux_added = false;
1822 for (c = 0; c < num_adcs; c++) {
1823 struct nid_path *path;
1824 hda_nid_t adc = spec->adc_nids[c];
1825
1826 if (!is_reachable_path(codec, pin, adc))
1827 continue;
1828 path = snd_array_new(&spec->paths);
1829 if (!path)
1830 return -ENOMEM;
1831 memset(path, 0, sizeof(*path));
1832 if (!snd_hda_parse_nid_path(codec, pin, adc, 2, path)) {
1833 snd_printd(KERN_ERR
1834 "invalid input path 0x%x -> 0x%x\n",
1835 pin, adc);
1836 spec->paths.used--;
1837 continue;
1838 }
1839
1840 if (!imux_added) {
1841 spec->imux_pins[imux->num_items] = pin;
1842 snd_hda_add_imux_item(imux, label,
1843 imux->num_items, NULL);
1844 imux_added = true;
1845 }
1846 }
1847 }
1848
1849 return 0;
1850}
1851
1852
1853/*
1854 * input source mux
1855 */
1856
1857/* get the ADC NID corresponding to the given index */
1858static hda_nid_t get_adc_nid(struct hda_codec *codec, int adc_idx, int imux_idx)
1859{
1860 struct hda_gen_spec *spec = codec->spec;
1861 if (spec->dyn_adc_switch)
1862 adc_idx = spec->dyn_adc_idx[imux_idx];
1863 return spec->adc_nids[adc_idx];
1864}
1865
1866static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
1867 unsigned int idx);
1868
1869static int mux_enum_info(struct snd_kcontrol *kcontrol,
1870 struct snd_ctl_elem_info *uinfo)
1871{
1872 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1873 struct hda_gen_spec *spec = codec->spec;
1874 return snd_hda_input_mux_info(&spec->input_mux, uinfo);
1875}
1876
1877static int mux_enum_get(struct snd_kcontrol *kcontrol,
1878 struct snd_ctl_elem_value *ucontrol)
1879{
1880 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1881 struct hda_gen_spec *spec = codec->spec;
1882 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
1883
1884 ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
1885 return 0;
1886}
1887
1888static int mux_enum_put(struct snd_kcontrol *kcontrol,
1889 struct snd_ctl_elem_value *ucontrol)
1890{
1891 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1892 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
1893 return mux_select(codec, adc_idx,
1894 ucontrol->value.enumerated.item[0]);
1895}
1896
352f7f91
TI
1897static const struct snd_kcontrol_new cap_src_temp = {
1898 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1899 .name = "Input Source",
1900 .info = mux_enum_info,
1901 .get = mux_enum_get,
1902 .put = mux_enum_put,
1903};
1904
47d46abb
TI
1905/*
1906 * capture volume and capture switch ctls
1907 */
1908
352f7f91
TI
1909typedef int (*put_call_t)(struct snd_kcontrol *kcontrol,
1910 struct snd_ctl_elem_value *ucontrol);
1911
47d46abb 1912/* call the given amp update function for all amps in the imux list at once */
352f7f91
TI
1913static int cap_put_caller(struct snd_kcontrol *kcontrol,
1914 struct snd_ctl_elem_value *ucontrol,
1915 put_call_t func, int type)
1916{
1917 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1918 struct hda_gen_spec *spec = codec->spec;
1919 const struct hda_input_mux *imux;
1920 struct nid_path *path;
1921 int i, adc_idx, err = 0;
1922
1923 imux = &spec->input_mux;
1924 adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
1925 mutex_lock(&codec->control_mutex);
47d46abb
TI
1926 /* we use the cache-only update at first since multiple input paths
1927 * may shared the same amp; by updating only caches, the redundant
1928 * writes to hardware can be reduced.
1929 */
352f7f91
TI
1930 codec->cached_write = 1;
1931 for (i = 0; i < imux->num_items; i++) {
1932 path = snd_hda_get_nid_path(codec, spec->imux_pins[i],
1933 get_adc_nid(codec, adc_idx, i));
1934 if (!path->ctls[type])
1935 continue;
1936 kcontrol->private_value = path->ctls[type];
1937 err = func(kcontrol, ucontrol);
1938 if (err < 0)
1939 goto error;
1940 }
1941 error:
1942 codec->cached_write = 0;
1943 mutex_unlock(&codec->control_mutex);
47d46abb 1944 snd_hda_codec_flush_amp_cache(codec); /* flush the updates */
352f7f91
TI
1945 if (err >= 0 && spec->cap_sync_hook)
1946 spec->cap_sync_hook(codec);
1947 return err;
1948}
1949
1950/* capture volume ctl callbacks */
1951#define cap_vol_info snd_hda_mixer_amp_volume_info
1952#define cap_vol_get snd_hda_mixer_amp_volume_get
1953#define cap_vol_tlv snd_hda_mixer_amp_tlv
1954
1955static int cap_vol_put(struct snd_kcontrol *kcontrol,
1956 struct snd_ctl_elem_value *ucontrol)
1957{
1958 return cap_put_caller(kcontrol, ucontrol,
1959 snd_hda_mixer_amp_volume_put,
1960 NID_PATH_VOL_CTL);
1961}
1962
1963static const struct snd_kcontrol_new cap_vol_temp = {
1964 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1965 .name = "Capture Volume",
1966 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
1967 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
1968 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK),
1969 .info = cap_vol_info,
1970 .get = cap_vol_get,
1971 .put = cap_vol_put,
1972 .tlv = { .c = cap_vol_tlv },
1973};
1974
1975/* capture switch ctl callbacks */
1976#define cap_sw_info snd_ctl_boolean_stereo_info
1977#define cap_sw_get snd_hda_mixer_amp_switch_get
1978
1979static int cap_sw_put(struct snd_kcontrol *kcontrol,
1980 struct snd_ctl_elem_value *ucontrol)
1981{
1982 return cap_put_caller(kcontrol, ucontrol,
1983 snd_hda_mixer_amp_switch_put,
1984 NID_PATH_MUTE_CTL);
1985}
1986
1987static const struct snd_kcontrol_new cap_sw_temp = {
1988 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1989 .name = "Capture Switch",
1990 .info = cap_sw_info,
1991 .get = cap_sw_get,
1992 .put = cap_sw_put,
1993};
1994
1995static int parse_capvol_in_path(struct hda_codec *codec, struct nid_path *path)
1996{
1997 hda_nid_t nid;
1998 int i, depth;
1999
2000 path->ctls[NID_PATH_VOL_CTL] = path->ctls[NID_PATH_MUTE_CTL] = 0;
2001 for (depth = 0; depth < 3; depth++) {
2002 if (depth >= path->depth)
2003 return -EINVAL;
2004 i = path->depth - depth - 1;
2005 nid = path->path[i];
2006 if (!path->ctls[NID_PATH_VOL_CTL]) {
2007 if (nid_has_volume(codec, nid, HDA_OUTPUT))
2008 path->ctls[NID_PATH_VOL_CTL] =
2009 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
2010 else if (nid_has_volume(codec, nid, HDA_INPUT)) {
2011 int idx = path->idx[i];
2012 if (!depth && codec->single_adc_amp)
2013 idx = 0;
2014 path->ctls[NID_PATH_VOL_CTL] =
2015 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
2016 }
2017 }
2018 if (!path->ctls[NID_PATH_MUTE_CTL]) {
2019 if (nid_has_mute(codec, nid, HDA_OUTPUT))
2020 path->ctls[NID_PATH_MUTE_CTL] =
2021 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
2022 else if (nid_has_mute(codec, nid, HDA_INPUT)) {
2023 int idx = path->idx[i];
2024 if (!depth && codec->single_adc_amp)
2025 idx = 0;
2026 path->ctls[NID_PATH_MUTE_CTL] =
2027 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
2028 }
2029 }
2030 }
2031 return 0;
2032}
2033
2034static bool is_inv_dmic_pin(struct hda_codec *codec, hda_nid_t nid)
2035{
2036 struct hda_gen_spec *spec = codec->spec;
2037 struct auto_pin_cfg *cfg = &spec->autocfg;
2038 unsigned int val;
2039 int i;
2040
2041 if (!spec->inv_dmic_split)
2042 return false;
2043 for (i = 0; i < cfg->num_inputs; i++) {
2044 if (cfg->inputs[i].pin != nid)
2045 continue;
2046 if (cfg->inputs[i].type != AUTO_PIN_MIC)
2047 return false;
2048 val = snd_hda_codec_get_pincfg(codec, nid);
2049 return snd_hda_get_input_pin_attr(val) == INPUT_PIN_ATTR_INT;
2050 }
2051 return false;
2052}
2053
2054static int add_single_cap_ctl(struct hda_codec *codec, const char *label,
2055 int idx, bool is_switch, unsigned int ctl,
2056 bool inv_dmic)
2057{
2058 struct hda_gen_spec *spec = codec->spec;
2059 char tmpname[44];
2060 int type = is_switch ? HDA_CTL_WIDGET_MUTE : HDA_CTL_WIDGET_VOL;
2061 const char *sfx = is_switch ? "Switch" : "Volume";
2062 unsigned int chs = inv_dmic ? 1 : 3;
2063 int err;
2064
2065 if (!ctl)
2066 return 0;
2067
2068 if (label)
2069 snprintf(tmpname, sizeof(tmpname),
2070 "%s Capture %s", label, sfx);
2071 else
2072 snprintf(tmpname, sizeof(tmpname),
2073 "Capture %s", sfx);
2074 err = add_control(spec, type, tmpname, idx,
2075 amp_val_replace_channels(ctl, chs));
2076 if (err < 0 || !inv_dmic)
2077 return err;
2078
2079 /* Make independent right kcontrol */
2080 if (label)
2081 snprintf(tmpname, sizeof(tmpname),
2082 "Inverted %s Capture %s", label, sfx);
2083 else
2084 snprintf(tmpname, sizeof(tmpname),
2085 "Inverted Capture %s", sfx);
2086 return add_control(spec, type, tmpname, idx,
2087 amp_val_replace_channels(ctl, 2));
2088}
2089
2090/* create single (and simple) capture volume and switch controls */
2091static int create_single_cap_vol_ctl(struct hda_codec *codec, int idx,
2092 unsigned int vol_ctl, unsigned int sw_ctl,
2093 bool inv_dmic)
2094{
2095 int err;
2096 err = add_single_cap_ctl(codec, NULL, idx, false, vol_ctl, inv_dmic);
2097 if (err < 0)
2098 return err;
2099 err = add_single_cap_ctl(codec, NULL, idx, true, sw_ctl, inv_dmic);
2100 if (err < 0)
2101 return err;
2102 return 0;
2103}
2104
2105/* create bound capture volume and switch controls */
2106static int create_bind_cap_vol_ctl(struct hda_codec *codec, int idx,
2107 unsigned int vol_ctl, unsigned int sw_ctl)
2108{
2109 struct hda_gen_spec *spec = codec->spec;
2110 struct snd_kcontrol_new *knew;
2111
2112 if (vol_ctl) {
12c93df6 2113 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_vol_temp);
352f7f91
TI
2114 if (!knew)
2115 return -ENOMEM;
2116 knew->index = idx;
2117 knew->private_value = vol_ctl;
2118 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
2119 }
2120 if (sw_ctl) {
12c93df6 2121 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_sw_temp);
352f7f91
TI
2122 if (!knew)
2123 return -ENOMEM;
2124 knew->index = idx;
2125 knew->private_value = sw_ctl;
2126 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
2127 }
2128 return 0;
2129}
2130
2131/* return the vol ctl when used first in the imux list */
2132static unsigned int get_first_cap_ctl(struct hda_codec *codec, int idx, int type)
2133{
2134 struct hda_gen_spec *spec = codec->spec;
2135 struct nid_path *path;
2136 unsigned int ctl;
2137 int i;
2138
2139 path = snd_hda_get_nid_path(codec, spec->imux_pins[idx],
2140 get_adc_nid(codec, 0, idx));
2141 if (!path)
2142 return 0;
2143 ctl = path->ctls[type];
2144 if (!ctl)
2145 return 0;
2146 for (i = 0; i < idx - 1; i++) {
2147 path = snd_hda_get_nid_path(codec, spec->imux_pins[i],
2148 get_adc_nid(codec, 0, i));
2149 if (path && path->ctls[type] == ctl)
2150 return 0;
2151 }
2152 return ctl;
2153}
2154
2155/* create individual capture volume and switch controls per input */
2156static int create_multi_cap_vol_ctl(struct hda_codec *codec)
2157{
2158 struct hda_gen_spec *spec = codec->spec;
2159 struct hda_input_mux *imux = &spec->input_mux;
2160 int i, err, type, type_idx = 0;
2161 const char *prev_label = NULL;
2162
2163 for (i = 0; i < imux->num_items; i++) {
2164 const char *label;
2165 bool inv_dmic;
2166 label = hda_get_autocfg_input_label(codec, &spec->autocfg, i);
2167 if (prev_label && !strcmp(label, prev_label))
2168 type_idx++;
2169 else
2170 type_idx = 0;
2171 prev_label = label;
2172 inv_dmic = is_inv_dmic_pin(codec, spec->imux_pins[i]);
2173
2174 for (type = 0; type < 2; type++) {
2175 err = add_single_cap_ctl(codec, label, type_idx, type,
2176 get_first_cap_ctl(codec, i, type),
2177 inv_dmic);
2178 if (err < 0)
2179 return err;
2180 }
2181 }
2182 return 0;
2183}
2184
2185static int create_capture_mixers(struct hda_codec *codec)
2186{
2187 struct hda_gen_spec *spec = codec->spec;
2188 struct hda_input_mux *imux = &spec->input_mux;
2189 int i, n, nums, err;
2190
2191 if (spec->dyn_adc_switch)
2192 nums = 1;
2193 else
2194 nums = spec->num_adc_nids;
2195
2196 if (!spec->auto_mic && imux->num_items > 1) {
2197 struct snd_kcontrol_new *knew;
624d914d
TI
2198 const char *name;
2199 name = nums > 1 ? "Input Source" : "Capture Source";
2200 knew = snd_hda_gen_add_kctl(spec, name, &cap_src_temp);
352f7f91
TI
2201 if (!knew)
2202 return -ENOMEM;
2203 knew->count = nums;
2204 }
2205
2206 for (n = 0; n < nums; n++) {
2207 bool multi = false;
2208 bool inv_dmic = false;
2209 int vol, sw;
2210
2211 vol = sw = 0;
2212 for (i = 0; i < imux->num_items; i++) {
2213 struct nid_path *path;
2214 path = snd_hda_get_nid_path(codec, spec->imux_pins[i],
2215 get_adc_nid(codec, n, i));
2216 if (!path)
2217 continue;
2218 parse_capvol_in_path(codec, path);
2219 if (!vol)
2220 vol = path->ctls[NID_PATH_VOL_CTL];
2221 else if (vol != path->ctls[NID_PATH_VOL_CTL])
2222 multi = true;
2223 if (!sw)
2224 sw = path->ctls[NID_PATH_MUTE_CTL];
2225 else if (sw != path->ctls[NID_PATH_MUTE_CTL])
2226 multi = true;
2227 if (is_inv_dmic_pin(codec, spec->imux_pins[i]))
2228 inv_dmic = true;
2229 }
2230
2231 if (!multi)
2232 err = create_single_cap_vol_ctl(codec, n, vol, sw,
2233 inv_dmic);
2234 else if (!spec->multi_cap_vol)
2235 err = create_bind_cap_vol_ctl(codec, n, vol, sw);
2236 else
2237 err = create_multi_cap_vol_ctl(codec);
2238 if (err < 0)
2239 return err;
2240 }
2241
2242 return 0;
2243}
2244
2245/*
2246 * add mic boosts if needed
2247 */
2248static int parse_mic_boost(struct hda_codec *codec)
2249{
2250 struct hda_gen_spec *spec = codec->spec;
2251 struct auto_pin_cfg *cfg = &spec->autocfg;
2252 int i, err;
2253 int type_idx = 0;
2254 hda_nid_t nid;
2255 const char *prev_label = NULL;
2256
2257 for (i = 0; i < cfg->num_inputs; i++) {
2258 if (cfg->inputs[i].type > AUTO_PIN_MIC)
2259 break;
2260 nid = cfg->inputs[i].pin;
2261 if (get_wcaps(codec, nid) & AC_WCAP_IN_AMP) {
2262 const char *label;
2263 char boost_label[32];
2264 struct nid_path *path;
2265 unsigned int val;
2266
2267 label = hda_get_autocfg_input_label(codec, cfg, i);
2268 if (spec->shared_mic_hp && !strcmp(label, "Misc"))
2269 label = "Headphone Mic";
2270 if (prev_label && !strcmp(label, prev_label))
2271 type_idx++;
2272 else
2273 type_idx = 0;
2274 prev_label = label;
2275
2276 snprintf(boost_label, sizeof(boost_label),
2277 "%s Boost Volume", label);
2278 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT);
2279 err = add_control(spec, HDA_CTL_WIDGET_VOL,
2280 boost_label, type_idx, val);
2281 if (err < 0)
2282 return err;
2283
2284 path = snd_hda_get_nid_path(codec, nid, 0);
2285 if (path)
2286 path->ctls[NID_PATH_BOOST_CTL] = val;
2287 }
2288 }
2289 return 0;
2290}
2291
2292/*
2293 * parse digital I/Os and set up NIDs in BIOS auto-parse mode
2294 */
2295static void parse_digital(struct hda_codec *codec)
2296{
2297 struct hda_gen_spec *spec = codec->spec;
2298 int i, nums;
2299 hda_nid_t dig_nid;
2300
2301 /* support multiple SPDIFs; the secondary is set up as a slave */
2302 nums = 0;
2303 for (i = 0; i < spec->autocfg.dig_outs; i++) {
2304 hda_nid_t pin = spec->autocfg.dig_out_pins[i];
2305 dig_nid = look_for_dac(codec, pin, true);
2306 if (!dig_nid)
2307 continue;
2308 if (!snd_hda_add_new_path(codec, dig_nid, pin, 2))
2309 continue;
2310 if (!nums) {
2311 spec->multiout.dig_out_nid = dig_nid;
2312 spec->dig_out_type = spec->autocfg.dig_out_type[0];
2313 } else {
2314 spec->multiout.slave_dig_outs = spec->slave_dig_outs;
2315 if (nums >= ARRAY_SIZE(spec->slave_dig_outs) - 1)
2316 break;
2317 spec->slave_dig_outs[nums - 1] = dig_nid;
2318 }
2319 nums++;
2320 }
2321
2322 if (spec->autocfg.dig_in_pin) {
2323 dig_nid = codec->start_nid;
2324 for (i = 0; i < codec->num_nodes; i++, dig_nid++) {
2325 struct nid_path *path;
2326 unsigned int wcaps = get_wcaps(codec, dig_nid);
2327 if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
2328 continue;
2329 if (!(wcaps & AC_WCAP_DIGITAL))
2330 continue;
2331 path = snd_hda_add_new_path(codec,
2332 spec->autocfg.dig_in_pin,
2333 dig_nid, 2);
2334 if (path) {
2335 path->active = true;
2336 spec->dig_in_nid = dig_nid;
2337 break;
2338 }
2339 }
2340 }
2341}
2342
2343
2344/*
2345 * input MUX handling
2346 */
2347
2348static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur);
2349
2350/* select the given imux item; either unmute exclusively or select the route */
2351static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
2352 unsigned int idx)
2353{
2354 struct hda_gen_spec *spec = codec->spec;
2355 const struct hda_input_mux *imux;
2356 struct nid_path *path;
2357
2358 imux = &spec->input_mux;
2359 if (!imux->num_items)
2360 return 0;
2361
2362 if (idx >= imux->num_items)
2363 idx = imux->num_items - 1;
2364 if (spec->cur_mux[adc_idx] == idx)
2365 return 0;
2366
2367 path = snd_hda_get_nid_path(codec,
2368 spec->imux_pins[spec->cur_mux[adc_idx]],
2369 spec->adc_nids[adc_idx]);
2370 if (!path)
2371 return 0;
2372 if (path->active)
2373 snd_hda_activate_path(codec, path, false, false);
2374
2375 spec->cur_mux[adc_idx] = idx;
2376
2377 if (spec->shared_mic_hp)
2378 update_shared_mic_hp(codec, spec->cur_mux[adc_idx]);
2379
2380 if (spec->dyn_adc_switch)
2381 dyn_adc_pcm_resetup(codec, idx);
2382
2383 path = snd_hda_get_nid_path(codec, spec->imux_pins[idx],
2384 get_adc_nid(codec, adc_idx, idx));
2385 if (!path)
2386 return 0;
2387 if (path->active)
2388 return 0;
2389 snd_hda_activate_path(codec, path, true, false);
2390 if (spec->cap_sync_hook)
2391 spec->cap_sync_hook(codec);
2392 return 1;
2393}
2394
2395
2396/*
2397 * Jack detections for HP auto-mute and mic-switch
2398 */
2399
2400/* check each pin in the given array; returns true if any of them is plugged */
2401static bool detect_jacks(struct hda_codec *codec, int num_pins, hda_nid_t *pins)
2402{
2403 int i, present = 0;
2404
2405 for (i = 0; i < num_pins; i++) {
2406 hda_nid_t nid = pins[i];
2407 if (!nid)
2408 break;
2409 present |= snd_hda_jack_detect(codec, nid);
2410 }
2411 return present;
2412}
2413
2414/* standard HP/line-out auto-mute helper */
2415static void do_automute(struct hda_codec *codec, int num_pins, hda_nid_t *pins,
2416 bool mute, bool hp_out)
2417{
2418 struct hda_gen_spec *spec = codec->spec;
2419 unsigned int pin_bits = mute ? 0 : (hp_out ? PIN_HP : PIN_OUT);
2420 int i;
2421
2422 for (i = 0; i < num_pins; i++) {
2423 hda_nid_t nid = pins[i];
2424 unsigned int val;
2425 if (!nid)
2426 break;
2427 /* don't reset VREF value in case it's controlling
2428 * the amp (see alc861_fixup_asus_amp_vref_0f())
2429 */
2430 if (spec->keep_vref_in_automute) {
2431 val = snd_hda_codec_read(codec, nid, 0,
2432 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
2433 val &= ~PIN_HP;
2434 } else
2435 val = 0;
2436 val |= pin_bits;
7594aa33 2437 snd_hda_set_pin_ctl_cache(codec, nid, val);
d5a9f1bb 2438 set_pin_eapd(codec, nid, !mute);
352f7f91
TI
2439 }
2440}
2441
2442/* Toggle outputs muting */
5d550e15 2443void snd_hda_gen_update_outputs(struct hda_codec *codec)
352f7f91
TI
2444{
2445 struct hda_gen_spec *spec = codec->spec;
2446 int on;
2447
2448 /* Control HP pins/amps depending on master_mute state;
2449 * in general, HP pins/amps control should be enabled in all cases,
2450 * but currently set only for master_mute, just to be safe
2451 */
2452 if (!spec->shared_mic_hp) /* don't change HP-pin when shared with mic */
2453 do_automute(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
2454 spec->autocfg.hp_pins, spec->master_mute, true);
2455
2456 if (!spec->automute_speaker)
2457 on = 0;
2458 else
2459 on = spec->hp_jack_present | spec->line_jack_present;
2460 on |= spec->master_mute;
2461 do_automute(codec, ARRAY_SIZE(spec->autocfg.speaker_pins),
2462 spec->autocfg.speaker_pins, on, false);
2463
2464 /* toggle line-out mutes if needed, too */
2465 /* if LO is a copy of either HP or Speaker, don't need to handle it */
2466 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0] ||
2467 spec->autocfg.line_out_pins[0] == spec->autocfg.speaker_pins[0])
2468 return;
2469 if (!spec->automute_lo)
2470 on = 0;
2471 else
2472 on = spec->hp_jack_present;
2473 on |= spec->master_mute;
2474 do_automute(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
2475 spec->autocfg.line_out_pins, on, false);
2476}
5d550e15 2477EXPORT_SYMBOL_HDA(snd_hda_gen_update_outputs);
352f7f91
TI
2478
2479static void call_update_outputs(struct hda_codec *codec)
2480{
2481 struct hda_gen_spec *spec = codec->spec;
2482 if (spec->automute_hook)
2483 spec->automute_hook(codec);
2484 else
5d550e15 2485 snd_hda_gen_update_outputs(codec);
352f7f91
TI
2486}
2487
2488/* standard HP-automute helper */
5d550e15 2489void snd_hda_gen_hp_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
352f7f91
TI
2490{
2491 struct hda_gen_spec *spec = codec->spec;
2492
2493 spec->hp_jack_present =
2494 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
2495 spec->autocfg.hp_pins);
2496 if (!spec->detect_hp || (!spec->automute_speaker && !spec->automute_lo))
2497 return;
2498 call_update_outputs(codec);
2499}
5d550e15 2500EXPORT_SYMBOL_HDA(snd_hda_gen_hp_automute);
352f7f91
TI
2501
2502/* standard line-out-automute helper */
5d550e15 2503void snd_hda_gen_line_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
352f7f91
TI
2504{
2505 struct hda_gen_spec *spec = codec->spec;
2506
2507 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
2508 return;
2509 /* check LO jack only when it's different from HP */
2510 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0])
2511 return;
2512
2513 spec->line_jack_present =
2514 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
2515 spec->autocfg.line_out_pins);
2516 if (!spec->automute_speaker || !spec->detect_lo)
2517 return;
2518 call_update_outputs(codec);
2519}
5d550e15 2520EXPORT_SYMBOL_HDA(snd_hda_gen_line_automute);
352f7f91
TI
2521
2522/* standard mic auto-switch helper */
5d550e15 2523void snd_hda_gen_mic_autoswitch(struct hda_codec *codec, struct hda_jack_tbl *jack)
352f7f91
TI
2524{
2525 struct hda_gen_spec *spec = codec->spec;
2526 int i;
2527
2528 if (!spec->auto_mic)
2529 return;
2530
2531 for (i = spec->am_num_entries - 1; i > 0; i--) {
2532 if (snd_hda_jack_detect(codec, spec->am_entry[i].pin)) {
2533 mux_select(codec, 0, spec->am_entry[i].idx);
2534 return;
2535 }
2536 }
2537 mux_select(codec, 0, spec->am_entry[0].idx);
2538}
5d550e15 2539EXPORT_SYMBOL_HDA(snd_hda_gen_mic_autoswitch);
352f7f91
TI
2540
2541/*
2542 * Auto-Mute mode mixer enum support
2543 */
2544static int automute_mode_info(struct snd_kcontrol *kcontrol,
2545 struct snd_ctl_elem_info *uinfo)
2546{
2547 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2548 struct hda_gen_spec *spec = codec->spec;
2549 static const char * const texts3[] = {
2550 "Disabled", "Speaker Only", "Line Out+Speaker"
2551 };
2552
2553 if (spec->automute_speaker_possible && spec->automute_lo_possible)
2554 return snd_hda_enum_helper_info(kcontrol, uinfo, 3, texts3);
2555 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
2556}
2557
2558static int automute_mode_get(struct snd_kcontrol *kcontrol,
2559 struct snd_ctl_elem_value *ucontrol)
2560{
2561 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2562 struct hda_gen_spec *spec = codec->spec;
2563 unsigned int val = 0;
2564 if (spec->automute_speaker)
2565 val++;
2566 if (spec->automute_lo)
2567 val++;
2568
2569 ucontrol->value.enumerated.item[0] = val;
2570 return 0;
2571}
2572
2573static int automute_mode_put(struct snd_kcontrol *kcontrol,
2574 struct snd_ctl_elem_value *ucontrol)
2575{
2576 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2577 struct hda_gen_spec *spec = codec->spec;
2578
2579 switch (ucontrol->value.enumerated.item[0]) {
2580 case 0:
2581 if (!spec->automute_speaker && !spec->automute_lo)
2582 return 0;
2583 spec->automute_speaker = 0;
2584 spec->automute_lo = 0;
2585 break;
2586 case 1:
2587 if (spec->automute_speaker_possible) {
2588 if (!spec->automute_lo && spec->automute_speaker)
2589 return 0;
2590 spec->automute_speaker = 1;
2591 spec->automute_lo = 0;
2592 } else if (spec->automute_lo_possible) {
2593 if (spec->automute_lo)
2594 return 0;
2595 spec->automute_lo = 1;
2596 } else
2597 return -EINVAL;
2598 break;
2599 case 2:
2600 if (!spec->automute_lo_possible || !spec->automute_speaker_possible)
2601 return -EINVAL;
2602 if (spec->automute_speaker && spec->automute_lo)
2603 return 0;
2604 spec->automute_speaker = 1;
2605 spec->automute_lo = 1;
2606 break;
2607 default:
2608 return -EINVAL;
2609 }
2610 call_update_outputs(codec);
2611 return 1;
2612}
2613
2614static const struct snd_kcontrol_new automute_mode_enum = {
2615 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2616 .name = "Auto-Mute Mode",
2617 .info = automute_mode_info,
2618 .get = automute_mode_get,
2619 .put = automute_mode_put,
2620};
2621
2622static int add_automute_mode_enum(struct hda_codec *codec)
2623{
2624 struct hda_gen_spec *spec = codec->spec;
2625
12c93df6 2626 if (!snd_hda_gen_add_kctl(spec, NULL, &automute_mode_enum))
352f7f91
TI
2627 return -ENOMEM;
2628 return 0;
2629}
2630
2631/*
2632 * Check the availability of HP/line-out auto-mute;
2633 * Set up appropriately if really supported
2634 */
2635static int check_auto_mute_availability(struct hda_codec *codec)
2636{
2637 struct hda_gen_spec *spec = codec->spec;
2638 struct auto_pin_cfg *cfg = &spec->autocfg;
2639 int present = 0;
2640 int i, err;
2641
2642 if (cfg->hp_pins[0])
2643 present++;
2644 if (cfg->line_out_pins[0])
2645 present++;
2646 if (cfg->speaker_pins[0])
2647 present++;
2648 if (present < 2) /* need two different output types */
2649 return 0;
2650
2651 if (!cfg->speaker_pins[0] &&
2652 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
2653 memcpy(cfg->speaker_pins, cfg->line_out_pins,
2654 sizeof(cfg->speaker_pins));
2655 cfg->speaker_outs = cfg->line_outs;
2656 }
2657
2658 if (!cfg->hp_pins[0] &&
2659 cfg->line_out_type == AUTO_PIN_HP_OUT) {
2660 memcpy(cfg->hp_pins, cfg->line_out_pins,
2661 sizeof(cfg->hp_pins));
2662 cfg->hp_outs = cfg->line_outs;
2663 }
2664
2665 for (i = 0; i < cfg->hp_outs; i++) {
2666 hda_nid_t nid = cfg->hp_pins[i];
2667 if (!is_jack_detectable(codec, nid))
2668 continue;
2669 snd_printdd("hda-codec: Enable HP auto-muting on NID 0x%x\n",
2670 nid);
2671 snd_hda_jack_detect_enable_callback(codec, nid, HDA_GEN_HP_EVENT,
5d550e15 2672 snd_hda_gen_hp_automute);
352f7f91
TI
2673 spec->detect_hp = 1;
2674 }
2675
2676 if (cfg->line_out_type == AUTO_PIN_LINE_OUT && cfg->line_outs) {
2677 if (cfg->speaker_outs)
2678 for (i = 0; i < cfg->line_outs; i++) {
2679 hda_nid_t nid = cfg->line_out_pins[i];
2680 if (!is_jack_detectable(codec, nid))
2681 continue;
2682 snd_printdd("hda-codec: Enable Line-Out auto-muting on NID 0x%x\n", nid);
2683 snd_hda_jack_detect_enable_callback(codec, nid,
2684 HDA_GEN_FRONT_EVENT,
5d550e15 2685 snd_hda_gen_line_automute);
352f7f91
TI
2686 spec->detect_lo = 1;
2687 }
2688 spec->automute_lo_possible = spec->detect_hp;
2689 }
2690
2691 spec->automute_speaker_possible = cfg->speaker_outs &&
2692 (spec->detect_hp || spec->detect_lo);
2693
2694 spec->automute_lo = spec->automute_lo_possible;
2695 spec->automute_speaker = spec->automute_speaker_possible;
2696
2697 if (spec->automute_speaker_possible || spec->automute_lo_possible) {
2698 /* create a control for automute mode */
2699 err = add_automute_mode_enum(codec);
2700 if (err < 0)
2701 return err;
2702 }
2703 return 0;
2704}
2705
2706/* return the position of NID in the list, or -1 if not found */
2707static int find_idx_in_nid_list(hda_nid_t nid, const hda_nid_t *list, int nums)
2708{
2709 int i;
2710 for (i = 0; i < nums; i++)
2711 if (list[i] == nid)
2712 return i;
2713 return -1;
2714}
2715
2716/* check whether all auto-mic pins are valid; setup indices if OK */
2717static bool auto_mic_check_imux(struct hda_codec *codec)
2718{
2719 struct hda_gen_spec *spec = codec->spec;
2720 const struct hda_input_mux *imux;
2721 int i;
2722
2723 imux = &spec->input_mux;
2724 for (i = 0; i < spec->am_num_entries; i++) {
2725 spec->am_entry[i].idx =
2726 find_idx_in_nid_list(spec->am_entry[i].pin,
2727 spec->imux_pins, imux->num_items);
2728 if (spec->am_entry[i].idx < 0)
2729 return false; /* no corresponding imux */
2730 }
2731
2732 /* we don't need the jack detection for the first pin */
2733 for (i = 1; i < spec->am_num_entries; i++)
2734 snd_hda_jack_detect_enable_callback(codec,
2735 spec->am_entry[i].pin,
2736 HDA_GEN_MIC_EVENT,
5d550e15 2737 snd_hda_gen_mic_autoswitch);
352f7f91
TI
2738 return true;
2739}
2740
2741static int compare_attr(const void *ap, const void *bp)
2742{
2743 const struct automic_entry *a = ap;
2744 const struct automic_entry *b = bp;
2745 return (int)(a->attr - b->attr);
2746}
1da177e4
LT
2747
2748/*
352f7f91
TI
2749 * Check the availability of auto-mic switch;
2750 * Set up if really supported
1da177e4 2751 */
352f7f91
TI
2752static int check_auto_mic_availability(struct hda_codec *codec)
2753{
2754 struct hda_gen_spec *spec = codec->spec;
2755 struct auto_pin_cfg *cfg = &spec->autocfg;
2756 unsigned int types;
2757 int i, num_pins;
2758
2759 types = 0;
2760 num_pins = 0;
2761 for (i = 0; i < cfg->num_inputs; i++) {
2762 hda_nid_t nid = cfg->inputs[i].pin;
2763 unsigned int attr;
2764 attr = snd_hda_codec_get_pincfg(codec, nid);
2765 attr = snd_hda_get_input_pin_attr(attr);
2766 if (types & (1 << attr))
2767 return 0; /* already occupied */
2768 switch (attr) {
2769 case INPUT_PIN_ATTR_INT:
2770 if (cfg->inputs[i].type != AUTO_PIN_MIC)
2771 return 0; /* invalid type */
2772 break;
2773 case INPUT_PIN_ATTR_UNUSED:
2774 return 0; /* invalid entry */
2775 default:
2776 if (cfg->inputs[i].type > AUTO_PIN_LINE_IN)
2777 return 0; /* invalid type */
2778 if (!spec->line_in_auto_switch &&
2779 cfg->inputs[i].type != AUTO_PIN_MIC)
2780 return 0; /* only mic is allowed */
2781 if (!is_jack_detectable(codec, nid))
2782 return 0; /* no unsol support */
2783 break;
2784 }
2785 if (num_pins >= MAX_AUTO_MIC_PINS)
2786 return 0;
2787 types |= (1 << attr);
2788 spec->am_entry[num_pins].pin = nid;
2789 spec->am_entry[num_pins].attr = attr;
2790 num_pins++;
2791 }
2792
2793 if (num_pins < 2)
2794 return 0;
2795
2796 spec->am_num_entries = num_pins;
2797 /* sort the am_entry in the order of attr so that the pin with a
2798 * higher attr will be selected when the jack is plugged.
2799 */
2800 sort(spec->am_entry, num_pins, sizeof(spec->am_entry[0]),
2801 compare_attr, NULL);
2802
2803 if (!auto_mic_check_imux(codec))
2804 return 0;
2805
2806 spec->auto_mic = 1;
2807 spec->num_adc_nids = 1;
2808 spec->cur_mux[0] = spec->am_entry[0].idx;
2809 snd_printdd("hda-codec: Enable auto-mic switch on NID 0x%x/0x%x/0x%x\n",
2810 spec->am_entry[0].pin,
2811 spec->am_entry[1].pin,
2812 spec->am_entry[2].pin);
2813
1da177e4
LT
2814 return 0;
2815}
2816
1da177e4 2817
9eb413e5
TI
2818/*
2819 * Parse the given BIOS configuration and set up the hda_gen_spec
2820 *
2821 * return 1 if successful, 0 if the proper config is not found,
352f7f91
TI
2822 * or a negative error code
2823 */
2824int snd_hda_gen_parse_auto_config(struct hda_codec *codec,
9eb413e5 2825 struct auto_pin_cfg *cfg)
352f7f91
TI
2826{
2827 struct hda_gen_spec *spec = codec->spec;
352f7f91
TI
2828 int err;
2829
9eb413e5
TI
2830 if (cfg != &spec->autocfg) {
2831 spec->autocfg = *cfg;
2832 cfg = &spec->autocfg;
2833 }
2834
352f7f91
TI
2835 if (!cfg->line_outs) {
2836 if (cfg->dig_outs || cfg->dig_in_pin) {
2837 spec->multiout.max_channels = 2;
2838 spec->no_analog = 1;
2839 goto dig_only;
2840 }
2841 return 0; /* can't find valid BIOS pin config */
2842 }
2843
2844 if (!spec->no_primary_hp &&
2845 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT &&
2846 cfg->line_outs <= cfg->hp_outs) {
2847 /* use HP as primary out */
2848 cfg->speaker_outs = cfg->line_outs;
2849 memcpy(cfg->speaker_pins, cfg->line_out_pins,
2850 sizeof(cfg->speaker_pins));
2851 cfg->line_outs = cfg->hp_outs;
2852 memcpy(cfg->line_out_pins, cfg->hp_pins, sizeof(cfg->hp_pins));
2853 cfg->hp_outs = 0;
2854 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
2855 cfg->line_out_type = AUTO_PIN_HP_OUT;
2856 }
2857
2858 err = parse_output_paths(codec);
2859 if (err < 0)
2860 return err;
2861 err = create_multi_channel_mode(codec);
2862 if (err < 0)
2863 return err;
2864 err = create_multi_out_ctls(codec, cfg);
2865 if (err < 0)
2866 return err;
2867 err = create_hp_out_ctls(codec);
2868 if (err < 0)
2869 return err;
2870 err = create_speaker_out_ctls(codec);
2871 if (err < 0)
2872 return err;
2873 err = create_shared_input(codec);
2874 if (err < 0)
2875 return err;
2876 err = create_input_ctls(codec);
2877 if (err < 0)
2878 return err;
2879
2880 /* check the multiple speaker pins */
2881 if (cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
2882 spec->const_channel_count = cfg->line_outs * 2;
2883 else
2884 spec->const_channel_count = cfg->speaker_outs * 2;
2885
2886 if (spec->multi_ios > 0)
2887 spec->multiout.max_channels = max(spec->ext_channel_count,
2888 spec->const_channel_count);
2889 else
2890 spec->multiout.max_channels = spec->multiout.num_dacs * 2;
2891
2892 err = check_auto_mute_availability(codec);
2893 if (err < 0)
2894 return err;
2895
2896 err = check_dyn_adc_switch(codec);
2897 if (err < 0)
2898 return err;
2899
2900 if (!spec->shared_mic_hp) {
2901 err = check_auto_mic_availability(codec);
97ec558a
TI
2902 if (err < 0)
2903 return err;
2904 }
1da177e4 2905
352f7f91
TI
2906 err = create_capture_mixers(codec);
2907 if (err < 0)
2908 return err;
a7da6ce5 2909
352f7f91
TI
2910 err = parse_mic_boost(codec);
2911 if (err < 0)
2912 return err;
2913
2914 dig_only:
2915 parse_digital(codec);
2916
2917 return 1;
a7da6ce5 2918}
352f7f91 2919EXPORT_SYMBOL_HDA(snd_hda_gen_parse_auto_config);
a7da6ce5 2920
071c73ad 2921
352f7f91
TI
2922/*
2923 * Build control elements
2924 */
2925
2926/* slave controls for virtual master */
2927static const char * const slave_pfxs[] = {
2928 "Front", "Surround", "Center", "LFE", "Side",
2929 "Headphone", "Speaker", "Mono", "Line Out",
2930 "CLFE", "Bass Speaker", "PCM",
2931 NULL,
2932};
2933
2934int snd_hda_gen_build_controls(struct hda_codec *codec)
2935{
2936 struct hda_gen_spec *spec = codec->spec;
2937 int err;
1da177e4 2938
36502d02
TI
2939 if (spec->kctls.used) {
2940 err = snd_hda_add_new_ctls(codec, spec->kctls.list);
2941 if (err < 0)
2942 return err;
2943 }
071c73ad 2944
352f7f91
TI
2945 if (spec->multiout.dig_out_nid) {
2946 err = snd_hda_create_dig_out_ctls(codec,
2947 spec->multiout.dig_out_nid,
2948 spec->multiout.dig_out_nid,
2949 spec->pcm_rec[1].pcm_type);
2950 if (err < 0)
2951 return err;
2952 if (!spec->no_analog) {
2953 err = snd_hda_create_spdif_share_sw(codec,
2954 &spec->multiout);
2955 if (err < 0)
2956 return err;
2957 spec->multiout.share_spdif = 1;
2958 }
2959 }
2960 if (spec->dig_in_nid) {
2961 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
071c73ad
TI
2962 if (err < 0)
2963 return err;
071c73ad 2964 }
1da177e4 2965
352f7f91
TI
2966 /* if we have no master control, let's create it */
2967 if (!spec->no_analog &&
2968 !snd_hda_find_mixer_ctl(codec, "Master Playback Volume")) {
2969 unsigned int vmaster_tlv[4];
2970 snd_hda_set_vmaster_tlv(codec, spec->vmaster_nid,
2971 HDA_OUTPUT, vmaster_tlv);
2972 err = snd_hda_add_vmaster(codec, "Master Playback Volume",
2973 vmaster_tlv, slave_pfxs,
2974 "Playback Volume");
2975 if (err < 0)
2976 return err;
2977 }
2978 if (!spec->no_analog &&
2979 !snd_hda_find_mixer_ctl(codec, "Master Playback Switch")) {
2980 err = __snd_hda_add_vmaster(codec, "Master Playback Switch",
2981 NULL, slave_pfxs,
2982 "Playback Switch",
2983 true, &spec->vmaster_mute.sw_kctl);
2984 if (err < 0)
2985 return err;
2986 if (spec->vmaster_mute.hook)
fd25a97a
TI
2987 snd_hda_add_vmaster_hook(codec, &spec->vmaster_mute,
2988 spec->vmaster_mute_enum);
352f7f91 2989 }
071c73ad 2990
352f7f91 2991 free_kctls(spec); /* no longer needed */
071c73ad 2992
352f7f91
TI
2993 if (spec->shared_mic_hp) {
2994 int err;
2995 int nid = spec->autocfg.inputs[1].pin;
2996 err = snd_hda_jack_add_kctl(codec, nid, "Headphone Mic", 0);
2997 if (err < 0)
2998 return err;
2999 err = snd_hda_jack_detect_enable(codec, nid, 0);
d13bd412 3000 if (err < 0)
1da177e4 3001 return err;
1da177e4 3002 }
071c73ad 3003
352f7f91
TI
3004 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
3005 if (err < 0)
3006 return err;
3007
1da177e4
LT
3008 return 0;
3009}
352f7f91 3010EXPORT_SYMBOL_HDA(snd_hda_gen_build_controls);
1da177e4
LT
3011
3012
3013/*
352f7f91 3014 * PCM definitions
1da177e4 3015 */
1da177e4 3016
352f7f91
TI
3017/*
3018 * Analog playback callbacks
3019 */
3020static int playback_pcm_open(struct hda_pcm_stream *hinfo,
3021 struct hda_codec *codec,
3022 struct snd_pcm_substream *substream)
3023{
3024 struct hda_gen_spec *spec = codec->spec;
3025 return snd_hda_multi_out_analog_open(codec, &spec->multiout, substream,
3026 hinfo);
3027}
1da177e4 3028
352f7f91
TI
3029static int playback_pcm_prepare(struct hda_pcm_stream *hinfo,
3030 struct hda_codec *codec,
3031 unsigned int stream_tag,
3032 unsigned int format,
3033 struct snd_pcm_substream *substream)
3034{
3035 struct hda_gen_spec *spec = codec->spec;
3036 return snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
3037 stream_tag, format, substream);
3038}
1da177e4 3039
352f7f91
TI
3040static int playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
3041 struct hda_codec *codec,
3042 struct snd_pcm_substream *substream)
3043{
3044 struct hda_gen_spec *spec = codec->spec;
3045 return snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
1da177e4
LT
3046}
3047
3048/*
352f7f91 3049 * Digital out
1da177e4 3050 */
352f7f91
TI
3051static int dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
3052 struct hda_codec *codec,
3053 struct snd_pcm_substream *substream)
1da177e4 3054{
352f7f91
TI
3055 struct hda_gen_spec *spec = codec->spec;
3056 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
3057}
1da177e4 3058
352f7f91
TI
3059static int dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
3060 struct hda_codec *codec,
3061 unsigned int stream_tag,
3062 unsigned int format,
3063 struct snd_pcm_substream *substream)
3064{
3065 struct hda_gen_spec *spec = codec->spec;
3066 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
3067 stream_tag, format, substream);
3068}
1da177e4 3069
352f7f91
TI
3070static int dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
3071 struct hda_codec *codec,
3072 struct snd_pcm_substream *substream)
3073{
3074 struct hda_gen_spec *spec = codec->spec;
3075 return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
3076}
3077
3078static int dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
3079 struct hda_codec *codec,
3080 struct snd_pcm_substream *substream)
3081{
3082 struct hda_gen_spec *spec = codec->spec;
3083 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
1da177e4
LT
3084}
3085
3086/*
352f7f91 3087 * Analog capture
1da177e4 3088 */
352f7f91
TI
3089static int alt_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
3090 struct hda_codec *codec,
3091 unsigned int stream_tag,
3092 unsigned int format,
3093 struct snd_pcm_substream *substream)
1da177e4 3094{
352f7f91 3095 struct hda_gen_spec *spec = codec->spec;
1da177e4 3096
352f7f91
TI
3097 snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number + 1],
3098 stream_tag, 0, format);
3099 return 0;
3100}
3101
3102static int alt_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
3103 struct hda_codec *codec,
3104 struct snd_pcm_substream *substream)
3105{
3106 struct hda_gen_spec *spec = codec->spec;
1da177e4 3107
352f7f91
TI
3108 snd_hda_codec_cleanup_stream(codec,
3109 spec->adc_nids[substream->number + 1]);
1da177e4
LT
3110 return 0;
3111}
3112
3113/*
1da177e4 3114 */
352f7f91
TI
3115static const struct hda_pcm_stream pcm_analog_playback = {
3116 .substreams = 1,
3117 .channels_min = 2,
3118 .channels_max = 8,
3119 /* NID is set in build_pcms */
3120 .ops = {
3121 .open = playback_pcm_open,
3122 .prepare = playback_pcm_prepare,
3123 .cleanup = playback_pcm_cleanup
3124 },
3125};
3126
3127static const struct hda_pcm_stream pcm_analog_capture = {
1da177e4
LT
3128 .substreams = 1,
3129 .channels_min = 2,
3130 .channels_max = 2,
352f7f91 3131 /* NID is set in build_pcms */
1da177e4
LT
3132};
3133
352f7f91
TI
3134static const struct hda_pcm_stream pcm_analog_alt_playback = {
3135 .substreams = 1,
3136 .channels_min = 2,
3137 .channels_max = 2,
3138 /* NID is set in build_pcms */
3139};
3140
3141static const struct hda_pcm_stream pcm_analog_alt_capture = {
3142 .substreams = 2, /* can be overridden */
3143 .channels_min = 2,
3144 .channels_max = 2,
3145 /* NID is set in build_pcms */
3146 .ops = {
3147 .prepare = alt_capture_pcm_prepare,
3148 .cleanup = alt_capture_pcm_cleanup
3149 },
3150};
3151
3152static const struct hda_pcm_stream pcm_digital_playback = {
3153 .substreams = 1,
3154 .channels_min = 2,
3155 .channels_max = 2,
3156 /* NID is set in build_pcms */
3157 .ops = {
3158 .open = dig_playback_pcm_open,
3159 .close = dig_playback_pcm_close,
3160 .prepare = dig_playback_pcm_prepare,
3161 .cleanup = dig_playback_pcm_cleanup
3162 },
3163};
3164
3165static const struct hda_pcm_stream pcm_digital_capture = {
3166 .substreams = 1,
3167 .channels_min = 2,
3168 .channels_max = 2,
3169 /* NID is set in build_pcms */
3170};
3171
3172/* Used by build_pcms to flag that a PCM has no playback stream */
3173static const struct hda_pcm_stream pcm_null_stream = {
3174 .substreams = 0,
3175 .channels_min = 0,
3176 .channels_max = 0,
3177};
3178
3179/*
3180 * dynamic changing ADC PCM streams
3181 */
3182static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur)
97ec558a 3183{
352f7f91
TI
3184 struct hda_gen_spec *spec = codec->spec;
3185 hda_nid_t new_adc = spec->adc_nids[spec->dyn_adc_idx[cur]];
3186
3187 if (spec->cur_adc && spec->cur_adc != new_adc) {
3188 /* stream is running, let's swap the current ADC */
3189 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
3190 spec->cur_adc = new_adc;
3191 snd_hda_codec_setup_stream(codec, new_adc,
3192 spec->cur_adc_stream_tag, 0,
3193 spec->cur_adc_format);
3194 return true;
3195 }
3196 return false;
3197}
97ec558a 3198
352f7f91
TI
3199/* analog capture with dynamic dual-adc changes */
3200static int dyn_adc_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
3201 struct hda_codec *codec,
3202 unsigned int stream_tag,
3203 unsigned int format,
3204 struct snd_pcm_substream *substream)
3205{
3206 struct hda_gen_spec *spec = codec->spec;
3207 spec->cur_adc = spec->adc_nids[spec->dyn_adc_idx[spec->cur_mux[0]]];
3208 spec->cur_adc_stream_tag = stream_tag;
3209 spec->cur_adc_format = format;
3210 snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
97ec558a
TI
3211 return 0;
3212}
3213
352f7f91
TI
3214static int dyn_adc_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
3215 struct hda_codec *codec,
3216 struct snd_pcm_substream *substream)
97ec558a 3217{
352f7f91
TI
3218 struct hda_gen_spec *spec = codec->spec;
3219 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
3220 spec->cur_adc = 0;
97ec558a
TI
3221 return 0;
3222}
3223
352f7f91
TI
3224static const struct hda_pcm_stream dyn_adc_pcm_analog_capture = {
3225 .substreams = 1,
3226 .channels_min = 2,
3227 .channels_max = 2,
3228 .nid = 0, /* fill later */
3229 .ops = {
3230 .prepare = dyn_adc_capture_pcm_prepare,
3231 .cleanup = dyn_adc_capture_pcm_cleanup
3232 },
3233};
3234
f873e536
TI
3235static void fill_pcm_stream_name(char *str, size_t len, const char *sfx,
3236 const char *chip_name)
3237{
3238 char *p;
3239
3240 if (*str)
3241 return;
3242 strlcpy(str, chip_name, len);
3243
3244 /* drop non-alnum chars after a space */
3245 for (p = strchr(str, ' '); p; p = strchr(p + 1, ' ')) {
3246 if (!isalnum(p[1])) {
3247 *p = 0;
3248 break;
3249 }
3250 }
3251 strlcat(str, sfx, len);
3252}
3253
352f7f91
TI
3254/* build PCM streams based on the parsed results */
3255int snd_hda_gen_build_pcms(struct hda_codec *codec)
1da177e4 3256{
352f7f91
TI
3257 struct hda_gen_spec *spec = codec->spec;
3258 struct hda_pcm *info = spec->pcm_rec;
3259 const struct hda_pcm_stream *p;
3260 bool have_multi_adcs;
352f7f91
TI
3261
3262 codec->num_pcms = 1;
3263 codec->pcm_info = info;
3264
3265 if (spec->no_analog)
3266 goto skip_analog;
3267
f873e536
TI
3268 fill_pcm_stream_name(spec->stream_name_analog,
3269 sizeof(spec->stream_name_analog),
3270 " Analog", codec->chip_name);
352f7f91
TI
3271 info->name = spec->stream_name_analog;
3272
3273 if (spec->multiout.num_dacs > 0) {
3274 p = spec->stream_analog_playback;
3275 if (!p)
3276 p = &pcm_analog_playback;
3277 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
3278 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dac_nids[0];
3279 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
3280 spec->multiout.max_channels;
3281 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT &&
3282 spec->autocfg.line_outs == 2)
3283 info->stream[SNDRV_PCM_STREAM_PLAYBACK].chmap =
3284 snd_pcm_2_1_chmaps;
3285 }
3286 if (spec->num_adc_nids) {
3287 p = spec->stream_analog_capture;
3288 if (!p) {
3289 if (spec->dyn_adc_switch)
3290 p = &dyn_adc_pcm_analog_capture;
3291 else
3292 p = &pcm_analog_capture;
3293 }
3294 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
3295 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adc_nids[0];
3296 }
3297
352f7f91
TI
3298 skip_analog:
3299 /* SPDIF for stream index #1 */
3300 if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
f873e536
TI
3301 fill_pcm_stream_name(spec->stream_name_digital,
3302 sizeof(spec->stream_name_digital),
3303 " Digital", codec->chip_name);
352f7f91
TI
3304 codec->num_pcms = 2;
3305 codec->slave_dig_outs = spec->multiout.slave_dig_outs;
3306 info = spec->pcm_rec + 1;
3307 info->name = spec->stream_name_digital;
3308 if (spec->dig_out_type)
3309 info->pcm_type = spec->dig_out_type;
3310 else
3311 info->pcm_type = HDA_PCM_TYPE_SPDIF;
3312 if (spec->multiout.dig_out_nid) {
3313 p = spec->stream_digital_playback;
3314 if (!p)
3315 p = &pcm_digital_playback;
3316 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
3317 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dig_out_nid;
3318 }
3319 if (spec->dig_in_nid) {
3320 p = spec->stream_digital_capture;
3321 if (!p)
3322 p = &pcm_digital_capture;
3323 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
3324 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in_nid;
3325 }
3326 }
1da177e4 3327
352f7f91 3328 if (spec->no_analog)
1da177e4 3329 return 0;
352f7f91
TI
3330
3331 /* If the use of more than one ADC is requested for the current
3332 * model, configure a second analog capture-only PCM.
3333 */
3334 have_multi_adcs = (spec->num_adc_nids > 1) &&
3335 !spec->dyn_adc_switch && !spec->auto_mic;
3336 /* Additional Analaog capture for index #2 */
3337 if (spec->alt_dac_nid || have_multi_adcs) {
3338 codec->num_pcms = 3;
3339 info = spec->pcm_rec + 2;
3340 info->name = spec->stream_name_analog;
3341 if (spec->alt_dac_nid) {
3342 p = spec->stream_analog_alt_playback;
3343 if (!p)
3344 p = &pcm_analog_alt_playback;
3345 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
3346 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
3347 spec->alt_dac_nid;
3348 } else {
3349 info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
3350 pcm_null_stream;
3351 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = 0;
3352 }
3353 if (have_multi_adcs) {
3354 p = spec->stream_analog_alt_capture;
3355 if (!p)
3356 p = &pcm_analog_alt_capture;
3357 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
3358 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
3359 spec->adc_nids[1];
3360 info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams =
3361 spec->num_adc_nids - 1;
3362 } else {
3363 info->stream[SNDRV_PCM_STREAM_CAPTURE] =
3364 pcm_null_stream;
3365 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = 0;
3366 }
1da177e4
LT
3367 }
3368
352f7f91
TI
3369 return 0;
3370}
3371EXPORT_SYMBOL_HDA(snd_hda_gen_build_pcms);
3372
3373
3374/*
3375 * Standard auto-parser initializations
3376 */
3377
3378/* configure the path from the given dac to the pin as the proper output */
3379static void set_output_and_unmute(struct hda_codec *codec, hda_nid_t pin,
3380 int pin_type, hda_nid_t dac)
3381{
3382 struct nid_path *path;
3383
3384 snd_hda_set_pin_ctl_cache(codec, pin, pin_type);
3385 path = snd_hda_get_nid_path(codec, dac, pin);
3386 if (!path)
3387 return;
3388 if (path->active)
3389 return;
3390 snd_hda_activate_path(codec, path, true, true);
d5a9f1bb 3391 set_pin_eapd(codec, pin, true);
352f7f91
TI
3392}
3393
3394/* initialize primary output paths */
3395static void init_multi_out(struct hda_codec *codec)
3396{
3397 struct hda_gen_spec *spec = codec->spec;
64049c81 3398 hda_nid_t nid, dac;
352f7f91
TI
3399 int pin_type;
3400 int i;
3401
3402 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
3403 pin_type = PIN_HP;
3404 else
3405 pin_type = PIN_OUT;
3406
64049c81
TI
3407 for (i = 0; i < spec->autocfg.line_outs; i++) {
3408 nid = spec->autocfg.line_out_pins[i];
3409 if (nid) {
3410 dac = spec->multiout.dac_nids[i];
3411 if (!dac)
3412 dac = spec->multiout.dac_nids[0];
3413 set_output_and_unmute(codec, nid, pin_type, dac);
3414 }
352f7f91
TI
3415 }
3416}
3417
db23fd19
TI
3418
3419static void __init_extra_out(struct hda_codec *codec, int num_outs,
3420 hda_nid_t *pins, hda_nid_t *dacs, int type)
352f7f91
TI
3421{
3422 struct hda_gen_spec *spec = codec->spec;
3423 int i;
3424 hda_nid_t pin, dac;
3425
db23fd19
TI
3426 for (i = 0; i < num_outs; i++) {
3427 pin = pins[i];
352f7f91
TI
3428 if (!pin)
3429 break;
db23fd19 3430 dac = dacs[i];
352f7f91 3431 if (!dac) {
db23fd19
TI
3432 if (i > 0 && dacs[0])
3433 dac = dacs[0];
352f7f91
TI
3434 else
3435 dac = spec->multiout.dac_nids[0];
3436 }
db23fd19 3437 set_output_and_unmute(codec, pin, type, dac);
352f7f91
TI
3438 }
3439}
3440
db23fd19
TI
3441/* initialize hp and speaker paths */
3442static void init_extra_out(struct hda_codec *codec)
3443{
3444 struct hda_gen_spec *spec = codec->spec;
3445
3446 if (spec->autocfg.line_out_type != AUTO_PIN_HP_OUT)
3447 __init_extra_out(codec, spec->autocfg.hp_outs,
3448 spec->autocfg.hp_pins,
3449 spec->multiout.hp_out_nid, PIN_HP);
3450 if (spec->autocfg.line_out_type != AUTO_PIN_SPEAKER_OUT)
3451 __init_extra_out(codec, spec->autocfg.speaker_outs,
3452 spec->autocfg.speaker_pins,
3453 spec->multiout.extra_out_nid, PIN_OUT);
3454}
3455
352f7f91
TI
3456/* initialize multi-io paths */
3457static void init_multi_io(struct hda_codec *codec)
3458{
3459 struct hda_gen_spec *spec = codec->spec;
3460 int i;
3461
3462 for (i = 0; i < spec->multi_ios; i++) {
3463 hda_nid_t pin = spec->multi_io[i].pin;
3464 struct nid_path *path;
3465 path = snd_hda_get_nid_path(codec, spec->multi_io[i].dac, pin);
3466 if (!path)
3467 continue;
3468 if (!spec->multi_io[i].ctl_in)
3469 spec->multi_io[i].ctl_in =
3470 snd_hda_codec_update_cache(codec, pin, 0,
3471 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
3472 snd_hda_activate_path(codec, path, path->active, true);
3473 }
3474}
3475
3476/* set up the input pin config, depending on the given auto-pin type */
3477static void set_input_pin(struct hda_codec *codec, hda_nid_t nid,
3478 int auto_pin_type)
3479{
3480 unsigned int val = PIN_IN;
3481 if (auto_pin_type == AUTO_PIN_MIC)
3482 val |= snd_hda_get_default_vref(codec, nid);
7594aa33 3483 snd_hda_set_pin_ctl_cache(codec, nid, val);
352f7f91
TI
3484}
3485
3486/* set up input pins and loopback paths */
3487static void init_analog_input(struct hda_codec *codec)
3488{
3489 struct hda_gen_spec *spec = codec->spec;
3490 struct auto_pin_cfg *cfg = &spec->autocfg;
3491 int i;
3492
3493 for (i = 0; i < cfg->num_inputs; i++) {
3494 hda_nid_t nid = cfg->inputs[i].pin;
3495 if (is_input_pin(codec, nid))
3496 set_input_pin(codec, nid, cfg->inputs[i].type);
3497
3498 /* init loopback inputs */
3499 if (spec->mixer_nid) {
3500 struct nid_path *path;
3501 path = snd_hda_get_nid_path(codec, nid, spec->mixer_nid);
3502 if (path)
3503 snd_hda_activate_path(codec, path,
3504 path->active, false);
3505 }
3506 }
3507}
3508
3509/* initialize ADC paths */
3510static void init_input_src(struct hda_codec *codec)
3511{
3512 struct hda_gen_spec *spec = codec->spec;
3513 struct hda_input_mux *imux = &spec->input_mux;
3514 struct nid_path *path;
3515 int i, c, nums;
1da177e4 3516
352f7f91
TI
3517 if (spec->dyn_adc_switch)
3518 nums = 1;
3519 else
3520 nums = spec->num_adc_nids;
3521
3522 for (c = 0; c < nums; c++) {
3523 for (i = 0; i < imux->num_items; i++) {
3524 path = snd_hda_get_nid_path(codec, spec->imux_pins[i],
3525 get_adc_nid(codec, c, i));
3526 if (path) {
3527 bool active = path->active;
3528 if (i == spec->cur_mux[c])
3529 active = true;
3530 snd_hda_activate_path(codec, path, active, false);
3531 }
97ec558a 3532 }
1da177e4 3533 }
352f7f91
TI
3534
3535 if (spec->shared_mic_hp)
3536 update_shared_mic_hp(codec, spec->cur_mux[0]);
3537
3538 if (spec->cap_sync_hook)
3539 spec->cap_sync_hook(codec);
3540}
3541
3542/* set right pin controls for digital I/O */
3543static void init_digital(struct hda_codec *codec)
3544{
3545 struct hda_gen_spec *spec = codec->spec;
3546 int i;
3547 hda_nid_t pin;
3548
3549 for (i = 0; i < spec->autocfg.dig_outs; i++) {
3550 pin = spec->autocfg.dig_out_pins[i];
3551 if (!pin)
3552 continue;
3553 set_output_and_unmute(codec, pin, PIN_OUT, 0);
1da177e4 3554 }
352f7f91
TI
3555 pin = spec->autocfg.dig_in_pin;
3556 if (pin)
7594aa33 3557 snd_hda_set_pin_ctl_cache(codec, pin, PIN_IN);
352f7f91
TI
3558}
3559
973e4972
TI
3560/* clear unsol-event tags on unused pins; Conexant codecs seem to leave
3561 * invalid unsol tags by some reason
3562 */
3563static void clear_unsol_on_unused_pins(struct hda_codec *codec)
3564{
3565 int i;
3566
3567 for (i = 0; i < codec->init_pins.used; i++) {
3568 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
3569 hda_nid_t nid = pin->nid;
3570 if (is_jack_detectable(codec, nid) &&
3571 !snd_hda_jack_tbl_get(codec, nid))
3572 snd_hda_codec_update_cache(codec, nid, 0,
3573 AC_VERB_SET_UNSOLICITED_ENABLE, 0);
3574 }
3575}
3576
352f7f91
TI
3577int snd_hda_gen_init(struct hda_codec *codec)
3578{
3579 struct hda_gen_spec *spec = codec->spec;
3580
3581 if (spec->init_hook)
3582 spec->init_hook(codec);
3583
3584 snd_hda_apply_verbs(codec);
3585
3bbcd274
TI
3586 codec->cached_write = 1;
3587
352f7f91
TI
3588 init_multi_out(codec);
3589 init_extra_out(codec);
3590 init_multi_io(codec);
3591 init_analog_input(codec);
3592 init_input_src(codec);
3593 init_digital(codec);
1da177e4 3594
973e4972
TI
3595 clear_unsol_on_unused_pins(codec);
3596
352f7f91 3597 /* call init functions of standard auto-mute helpers */
5d550e15
TI
3598 snd_hda_gen_hp_automute(codec, NULL);
3599 snd_hda_gen_line_automute(codec, NULL);
3600 snd_hda_gen_mic_autoswitch(codec, NULL);
352f7f91 3601
3bbcd274
TI
3602 snd_hda_codec_flush_amp_cache(codec);
3603 snd_hda_codec_flush_cmd_cache(codec);
3604
352f7f91
TI
3605 if (spec->vmaster_mute.sw_kctl && spec->vmaster_mute.hook)
3606 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
3607
3608 hda_call_check_power_status(codec, 0x01);
1da177e4
LT
3609 return 0;
3610}
352f7f91
TI
3611EXPORT_SYMBOL(snd_hda_gen_init);
3612
3613
3614/*
3615 * the generic codec support
3616 */
1da177e4 3617
83012a7c 3618#ifdef CONFIG_PM
cb53c626
TI
3619static int generic_check_power_status(struct hda_codec *codec, hda_nid_t nid)
3620{
352f7f91 3621 struct hda_gen_spec *spec = codec->spec;
cb53c626
TI
3622 return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
3623}
3624#endif
3625
352f7f91
TI
3626static void generic_free(struct hda_codec *codec)
3627{
3628 snd_hda_gen_spec_free(codec->spec);
3629 kfree(codec->spec);
3630 codec->spec = NULL;
3631}
1da177e4 3632
352f7f91
TI
3633static const struct hda_codec_ops generic_patch_ops = {
3634 .build_controls = snd_hda_gen_build_controls,
3635 .build_pcms = snd_hda_gen_build_pcms,
3636 .init = snd_hda_gen_init,
3637 .free = generic_free,
3638 .unsol_event = snd_hda_jack_unsol_event,
83012a7c 3639#ifdef CONFIG_PM
cb53c626
TI
3640 .check_power_status = generic_check_power_status,
3641#endif
1da177e4
LT
3642};
3643
1da177e4
LT
3644int snd_hda_parse_generic_codec(struct hda_codec *codec)
3645{
352f7f91 3646 struct hda_gen_spec *spec;
1da177e4
LT
3647 int err;
3648
e560d8d8 3649 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
352f7f91 3650 if (!spec)
1da177e4 3651 return -ENOMEM;
352f7f91 3652 snd_hda_gen_spec_init(spec);
1da177e4 3653 codec->spec = spec;
1da177e4 3654
9eb413e5
TI
3655 err = snd_hda_parse_pin_defcfg(codec, &spec->autocfg, NULL, 0);
3656 if (err < 0)
3657 return err;
3658
3659 err = snd_hda_gen_parse_auto_config(codec, &spec->autocfg);
352f7f91 3660 if (err < 0)
1da177e4
LT
3661 goto error;
3662
3663 codec->patch_ops = generic_patch_ops;
1da177e4
LT
3664 return 0;
3665
352f7f91
TI
3666error:
3667 generic_free(codec);
1da177e4
LT
3668 return err;
3669}
1289e9e8 3670EXPORT_SYMBOL(snd_hda_parse_generic_codec);