2 * Universal Interface for Intel High Definition Audio Codec
4 * Generic widget tree parser
6 * Copyright (c) 2004 Takashi Iwai <tiwai@suse.de>
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.
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.
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
23 #include <linux/init.h>
24 #include <linux/slab.h>
25 #include <sound/core.h>
26 #include "hda_codec.h"
27 #include "hda_local.h"
29 /* widget node for parsing */
31 hda_nid_t nid
; /* NID of this widget */
32 unsigned short nconns
; /* number of input connections */
34 hda_nid_t slist
[2]; /* temporay list */
35 unsigned int wid_caps
; /* widget capabilities */
36 unsigned char type
; /* widget type */
37 unsigned char pin_ctl
; /* pin controls */
38 unsigned char checked
; /* the flag indicates that the node is already parsed */
39 unsigned int pin_caps
; /* pin widget capabilities */
40 unsigned int def_cfg
; /* default configuration */
41 unsigned int amp_out_caps
; /* AMP out capabilities */
42 unsigned int amp_in_caps
; /* AMP in capabilities */
43 struct list_head list
;
46 /* patch-specific record */
48 #define MAX_PCM_VOLS 2
50 struct hda_gnode
*node
; /* Node for PCM volume */
51 unsigned int index
; /* connection of PCM volume */
55 struct hda_gnode
*dac_node
[2]; /* DAC node */
56 struct hda_gnode
*out_pin_node
[2]; /* Output pin (Line-Out) node */
57 struct pcm_vol pcm_vol
[MAX_PCM_VOLS
]; /* PCM volumes */
58 unsigned int pcm_vol_nodes
; /* number of PCM volumes */
60 struct hda_gnode
*adc_node
; /* ADC node */
61 struct hda_gnode
*cap_vol_node
; /* Node for capture volume */
62 unsigned int cur_cap_src
; /* current capture source */
63 struct hda_input_mux input_mux
;
65 unsigned int def_amp_in_caps
;
66 unsigned int def_amp_out_caps
;
68 struct hda_pcm pcm_rec
; /* PCM information */
70 struct list_head nid_list
; /* list of widgets */
72 #ifdef CONFIG_SND_HDA_POWER_SAVE
73 #define MAX_LOOPBACK_AMPS 7
74 struct hda_loopback_check loopback
;
76 struct hda_amp_list loopback_list
[MAX_LOOPBACK_AMPS
+ 1];
81 * retrieve the default device type from the default config value
83 #define defcfg_type(node) (((node)->def_cfg & AC_DEFCFG_DEVICE) >> \
84 AC_DEFCFG_DEVICE_SHIFT)
85 #define defcfg_location(node) (((node)->def_cfg & AC_DEFCFG_LOCATION) >> \
86 AC_DEFCFG_LOCATION_SHIFT)
87 #define defcfg_port_conn(node) (((node)->def_cfg & AC_DEFCFG_PORT_CONN) >> \
88 AC_DEFCFG_PORT_CONN_SHIFT)
93 static void snd_hda_generic_free(struct hda_codec
*codec
)
95 struct hda_gspec
*spec
= codec
->spec
;
96 struct hda_gnode
*node
, *n
;
100 /* free all widgets */
101 list_for_each_entry_safe(node
, n
, &spec
->nid_list
, list
) {
102 if (node
->conn_list
!= node
->slist
)
103 kfree(node
->conn_list
);
111 * add a new widget node and read its attributes
113 static int add_new_node(struct hda_codec
*codec
, struct hda_gspec
*spec
, hda_nid_t nid
)
115 struct hda_gnode
*node
;
117 hda_nid_t conn_list
[HDA_MAX_CONNECTIONS
];
119 node
= kzalloc(sizeof(*node
), GFP_KERNEL
);
123 node
->wid_caps
= get_wcaps(codec
, nid
);
124 node
->type
= get_wcaps_type(node
->wid_caps
);
125 if (node
->wid_caps
& AC_WCAP_CONN_LIST
) {
126 nconns
= snd_hda_get_connections(codec
, nid
, conn_list
,
127 HDA_MAX_CONNECTIONS
);
135 if (nconns
<= ARRAY_SIZE(node
->slist
))
136 node
->conn_list
= node
->slist
;
138 node
->conn_list
= kmalloc(sizeof(hda_nid_t
) * nconns
,
140 if (! node
->conn_list
) {
141 snd_printk(KERN_ERR
"hda-generic: cannot malloc\n");
146 memcpy(node
->conn_list
, conn_list
, nconns
* sizeof(hda_nid_t
));
147 node
->nconns
= nconns
;
149 if (node
->type
== AC_WID_PIN
) {
150 node
->pin_caps
= snd_hda_query_pin_caps(codec
, node
->nid
);
151 node
->pin_ctl
= snd_hda_codec_read(codec
, node
->nid
, 0, AC_VERB_GET_PIN_WIDGET_CONTROL
, 0);
152 node
->def_cfg
= snd_hda_codec_get_pincfg(codec
, node
->nid
);
155 if (node
->wid_caps
& AC_WCAP_OUT_AMP
) {
156 if (node
->wid_caps
& AC_WCAP_AMP_OVRD
)
157 node
->amp_out_caps
= snd_hda_param_read(codec
, node
->nid
, AC_PAR_AMP_OUT_CAP
);
158 if (! node
->amp_out_caps
)
159 node
->amp_out_caps
= spec
->def_amp_out_caps
;
161 if (node
->wid_caps
& AC_WCAP_IN_AMP
) {
162 if (node
->wid_caps
& AC_WCAP_AMP_OVRD
)
163 node
->amp_in_caps
= snd_hda_param_read(codec
, node
->nid
, AC_PAR_AMP_IN_CAP
);
164 if (! node
->amp_in_caps
)
165 node
->amp_in_caps
= spec
->def_amp_in_caps
;
167 list_add_tail(&node
->list
, &spec
->nid_list
);
172 * build the AFG subtree
174 static int build_afg_tree(struct hda_codec
*codec
)
176 struct hda_gspec
*spec
= codec
->spec
;
180 if (snd_BUG_ON(!spec
))
183 spec
->def_amp_out_caps
= snd_hda_param_read(codec
, codec
->afg
, AC_PAR_AMP_OUT_CAP
);
184 spec
->def_amp_in_caps
= snd_hda_param_read(codec
, codec
->afg
, AC_PAR_AMP_IN_CAP
);
186 nodes
= snd_hda_get_sub_nodes(codec
, codec
->afg
, &nid
);
187 if (! nid
|| nodes
< 0) {
188 printk(KERN_ERR
"Invalid AFG subtree\n");
192 /* parse all nodes belonging to the AFG */
193 for (i
= 0; i
< nodes
; i
++, nid
++) {
194 if ((err
= add_new_node(codec
, spec
, nid
)) < 0)
203 * look for the node record for the given NID
205 /* FIXME: should avoid the braindead linear search */
206 static struct hda_gnode
*hda_get_node(struct hda_gspec
*spec
, hda_nid_t nid
)
208 struct hda_gnode
*node
;
210 list_for_each_entry(node
, &spec
->nid_list
, list
) {
211 if (node
->nid
== nid
)
218 * unmute (and set max vol) the output amplifier
220 static int unmute_output(struct hda_codec
*codec
, struct hda_gnode
*node
)
222 unsigned int val
, ofs
;
223 snd_printdd("UNMUTE OUT: NID=0x%x\n", node
->nid
);
224 val
= (node
->amp_out_caps
& AC_AMPCAP_NUM_STEPS
) >> AC_AMPCAP_NUM_STEPS_SHIFT
;
225 ofs
= (node
->amp_out_caps
& AC_AMPCAP_OFFSET
) >> AC_AMPCAP_OFFSET_SHIFT
;
228 snd_hda_codec_amp_stereo(codec
, node
->nid
, HDA_OUTPUT
, 0, 0xff, val
);
233 * unmute (and set max vol) the input amplifier
235 static int unmute_input(struct hda_codec
*codec
, struct hda_gnode
*node
, unsigned int index
)
237 unsigned int val
, ofs
;
238 snd_printdd("UNMUTE IN: NID=0x%x IDX=0x%x\n", node
->nid
, index
);
239 val
= (node
->amp_in_caps
& AC_AMPCAP_NUM_STEPS
) >> AC_AMPCAP_NUM_STEPS_SHIFT
;
240 ofs
= (node
->amp_in_caps
& AC_AMPCAP_OFFSET
) >> AC_AMPCAP_OFFSET_SHIFT
;
243 snd_hda_codec_amp_stereo(codec
, node
->nid
, HDA_INPUT
, index
, 0xff, val
);
248 * select the input connection of the given node.
250 static int select_input_connection(struct hda_codec
*codec
, struct hda_gnode
*node
,
253 snd_printdd("CONNECT: NID=0x%x IDX=0x%x\n", node
->nid
, index
);
254 return snd_hda_codec_write_cache(codec
, node
->nid
, 0,
255 AC_VERB_SET_CONNECT_SEL
, index
);
259 * clear checked flag of each node in the node list
261 static void clear_check_flags(struct hda_gspec
*spec
)
263 struct hda_gnode
*node
;
265 list_for_each_entry(node
, &spec
->nid_list
, list
) {
271 * parse the output path recursively until reach to an audio output widget
273 * returns 0 if not found, 1 if found, or a negative error code.
275 static int parse_output_path(struct hda_codec
*codec
, struct hda_gspec
*spec
,
276 struct hda_gnode
*node
, int dac_idx
)
279 struct hda_gnode
*child
;
285 if (node
->type
== AC_WID_AUD_OUT
) {
286 if (node
->wid_caps
& AC_WCAP_DIGITAL
) {
287 snd_printdd("Skip Digital OUT node %x\n", node
->nid
);
290 snd_printdd("AUD_OUT found %x\n", node
->nid
);
291 if (spec
->dac_node
[dac_idx
]) {
292 /* already DAC node is assigned, just unmute & connect */
293 return node
== spec
->dac_node
[dac_idx
];
295 spec
->dac_node
[dac_idx
] = node
;
296 if ((node
->wid_caps
& AC_WCAP_OUT_AMP
) &&
297 spec
->pcm_vol_nodes
< MAX_PCM_VOLS
) {
298 spec
->pcm_vol
[spec
->pcm_vol_nodes
].node
= node
;
299 spec
->pcm_vol
[spec
->pcm_vol_nodes
].index
= 0;
300 spec
->pcm_vol_nodes
++;
302 return 1; /* found */
305 for (i
= 0; i
< node
->nconns
; i
++) {
306 child
= hda_get_node(spec
, node
->conn_list
[i
]);
309 err
= parse_output_path(codec
, spec
, child
, dac_idx
);
314 * select the path, unmute both input and output
316 if (node
->nconns
> 1)
317 select_input_connection(codec
, node
, i
);
318 unmute_input(codec
, node
, i
);
319 unmute_output(codec
, node
);
320 if (spec
->dac_node
[dac_idx
] &&
321 spec
->pcm_vol_nodes
< MAX_PCM_VOLS
&&
322 !(spec
->dac_node
[dac_idx
]->wid_caps
&
324 if ((node
->wid_caps
& AC_WCAP_IN_AMP
) ||
325 (node
->wid_caps
& AC_WCAP_OUT_AMP
)) {
326 int n
= spec
->pcm_vol_nodes
;
327 spec
->pcm_vol
[n
].node
= node
;
328 spec
->pcm_vol
[n
].index
= i
;
329 spec
->pcm_vol_nodes
++;
339 * Look for the output PIN widget with the given jack type
340 * and parse the output path to that PIN.
342 * Returns the PIN node when the path to DAC is established.
344 static struct hda_gnode
*parse_output_jack(struct hda_codec
*codec
,
345 struct hda_gspec
*spec
,
348 struct hda_gnode
*node
;
351 list_for_each_entry(node
, &spec
->nid_list
, list
) {
352 if (node
->type
!= AC_WID_PIN
)
354 /* output capable? */
355 if (! (node
->pin_caps
& AC_PINCAP_OUT
))
357 if (defcfg_port_conn(node
) == AC_JACK_PORT_NONE
)
358 continue; /* unconnected */
359 if (jack_type
>= 0) {
360 if (jack_type
!= defcfg_type(node
))
362 if (node
->wid_caps
& AC_WCAP_DIGITAL
)
363 continue; /* skip SPDIF */
365 /* output as default? */
366 if (! (node
->pin_ctl
& AC_PINCTL_OUT_EN
))
369 clear_check_flags(spec
);
370 err
= parse_output_path(codec
, spec
, node
, 0);
373 if (! err
&& spec
->out_pin_node
[0]) {
374 err
= parse_output_path(codec
, spec
, node
, 1);
379 /* unmute the PIN output */
380 unmute_output(codec
, node
);
381 /* set PIN-Out enable */
382 snd_hda_codec_write_cache(codec
, node
->nid
, 0,
383 AC_VERB_SET_PIN_WIDGET_CONTROL
,
385 ((node
->pin_caps
& AC_PINCAP_HP_DRV
) ?
386 AC_PINCTL_HP_EN
: 0));
397 static int parse_output(struct hda_codec
*codec
)
399 struct hda_gspec
*spec
= codec
->spec
;
400 struct hda_gnode
*node
;
403 * Look for the output PIN widget
405 /* first, look for the line-out pin */
406 node
= parse_output_jack(codec
, spec
, AC_JACK_LINE_OUT
);
407 if (node
) /* found, remember the PIN node */
408 spec
->out_pin_node
[0] = node
;
410 /* if no line-out is found, try speaker out */
411 node
= parse_output_jack(codec
, spec
, AC_JACK_SPEAKER
);
413 spec
->out_pin_node
[0] = node
;
415 /* look for the HP-out pin */
416 node
= parse_output_jack(codec
, spec
, AC_JACK_HP_OUT
);
418 if (! spec
->out_pin_node
[0])
419 spec
->out_pin_node
[0] = node
;
421 spec
->out_pin_node
[1] = node
;
424 if (! spec
->out_pin_node
[0]) {
425 /* no line-out or HP pins found,
426 * then choose for the first output pin
428 spec
->out_pin_node
[0] = parse_output_jack(codec
, spec
, -1);
429 if (! spec
->out_pin_node
[0])
430 snd_printd("hda_generic: no proper output path found\n");
440 /* control callbacks */
441 static int capture_source_info(struct snd_kcontrol
*kcontrol
, struct snd_ctl_elem_info
*uinfo
)
443 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
444 struct hda_gspec
*spec
= codec
->spec
;
445 return snd_hda_input_mux_info(&spec
->input_mux
, uinfo
);
448 static int capture_source_get(struct snd_kcontrol
*kcontrol
, struct snd_ctl_elem_value
*ucontrol
)
450 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
451 struct hda_gspec
*spec
= codec
->spec
;
453 ucontrol
->value
.enumerated
.item
[0] = spec
->cur_cap_src
;
457 static int capture_source_put(struct snd_kcontrol
*kcontrol
, struct snd_ctl_elem_value
*ucontrol
)
459 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
460 struct hda_gspec
*spec
= codec
->spec
;
461 return snd_hda_input_mux_put(codec
, &spec
->input_mux
, ucontrol
,
462 spec
->adc_node
->nid
, &spec
->cur_cap_src
);
466 * return the string name of the given input PIN widget
468 static const char *get_input_type(struct hda_gnode
*node
, unsigned int *pinctl
)
470 unsigned int location
= defcfg_location(node
);
471 switch (defcfg_type(node
)) {
472 case AC_JACK_LINE_IN
:
473 if ((location
& 0x0f) == AC_JACK_LOC_FRONT
)
479 *pinctl
|= AC_PINCTL_VREF_GRD
;
483 if ((location
& 0x0f) == AC_JACK_LOC_FRONT
)
489 (AC_PINCAP_VREF_80
<< AC_PINCAP_VREF_SHIFT
)))
490 *pinctl
|= AC_PINCTL_VREF_80
;
491 if ((location
& 0x0f) == AC_JACK_LOC_FRONT
)
494 case AC_JACK_SPDIF_IN
:
496 case AC_JACK_DIG_OTHER_IN
:
503 * parse the nodes recursively until reach to the input PIN
505 * returns 0 if not found, 1 if found, or a negative error code.
507 static int parse_adc_sub_nodes(struct hda_codec
*codec
, struct hda_gspec
*spec
,
508 struct hda_gnode
*node
, int idx
)
518 if (node
->type
!= AC_WID_PIN
) {
519 for (i
= 0; i
< node
->nconns
; i
++) {
520 struct hda_gnode
*child
;
521 child
= hda_get_node(spec
, node
->conn_list
[i
]);
524 err
= parse_adc_sub_nodes(codec
, spec
, child
, idx
);
529 * select the path, unmute both input and output
531 if (node
->nconns
> 1)
532 select_input_connection(codec
, node
, i
);
533 unmute_input(codec
, node
, i
);
534 unmute_output(codec
, node
);
542 if (! (node
->pin_caps
& AC_PINCAP_IN
))
545 if (defcfg_port_conn(node
) == AC_JACK_PORT_NONE
)
546 return 0; /* unconnected */
548 if (node
->wid_caps
& AC_WCAP_DIGITAL
)
549 return 0; /* skip SPDIF */
551 if (spec
->input_mux
.num_items
>= HDA_MAX_NUM_INPUTS
) {
552 snd_printk(KERN_ERR
"hda_generic: Too many items for capture\n");
556 pinctl
= AC_PINCTL_IN_EN
;
557 /* create a proper capture source label */
558 type
= get_input_type(node
, &pinctl
);
560 /* input as default? */
561 if (! (node
->pin_ctl
& AC_PINCTL_IN_EN
))
565 snd_hda_add_imux_item(&spec
->input_mux
, type
, idx
, NULL
);
567 /* unmute the PIN external input */
568 unmute_input(codec
, node
, 0); /* index = 0? */
569 /* set PIN-In enable */
570 snd_hda_codec_write_cache(codec
, node
->nid
, 0,
571 AC_VERB_SET_PIN_WIDGET_CONTROL
, pinctl
);
573 return 1; /* found */
579 static int parse_input_path(struct hda_codec
*codec
, struct hda_gnode
*adc_node
)
581 struct hda_gspec
*spec
= codec
->spec
;
582 struct hda_gnode
*node
;
585 snd_printdd("AUD_IN = %x\n", adc_node
->nid
);
586 clear_check_flags(spec
);
588 // awk added - fixed no recording due to muted widget
589 unmute_input(codec
, adc_node
, 0);
592 * check each connection of the ADC
593 * if it reaches to a proper input PIN, add the path as the
596 /* first, check the direct connections to PIN widgets */
597 for (i
= 0; i
< adc_node
->nconns
; i
++) {
598 node
= hda_get_node(spec
, adc_node
->conn_list
[i
]);
599 if (node
&& node
->type
== AC_WID_PIN
) {
600 err
= parse_adc_sub_nodes(codec
, spec
, node
, i
);
605 /* ... then check the rests, more complicated connections */
606 for (i
= 0; i
< adc_node
->nconns
; i
++) {
607 node
= hda_get_node(spec
, adc_node
->conn_list
[i
]);
608 if (node
&& node
->type
!= AC_WID_PIN
) {
609 err
= parse_adc_sub_nodes(codec
, spec
, node
, i
);
615 if (! spec
->input_mux
.num_items
)
616 return 0; /* no input path found... */
618 snd_printdd("[Capture Source] NID=0x%x, #SRC=%d\n", adc_node
->nid
, spec
->input_mux
.num_items
);
619 for (i
= 0; i
< spec
->input_mux
.num_items
; i
++)
620 snd_printdd(" [%s] IDX=0x%x\n", spec
->input_mux
.items
[i
].label
,
621 spec
->input_mux
.items
[i
].index
);
623 spec
->adc_node
= adc_node
;
630 static int parse_input(struct hda_codec
*codec
)
632 struct hda_gspec
*spec
= codec
->spec
;
633 struct hda_gnode
*node
;
637 * At first we look for an audio input widget.
638 * If it reaches to certain input PINs, we take it as the
641 list_for_each_entry(node
, &spec
->nid_list
, list
) {
642 if (node
->wid_caps
& AC_WCAP_DIGITAL
)
643 continue; /* skip SPDIF */
644 if (node
->type
== AC_WID_AUD_IN
) {
645 err
= parse_input_path(codec
, node
);
652 snd_printd("hda_generic: no proper input path found\n");
656 #ifdef CONFIG_SND_HDA_POWER_SAVE
657 static void add_input_loopback(struct hda_codec
*codec
, hda_nid_t nid
,
660 struct hda_gspec
*spec
= codec
->spec
;
661 struct hda_amp_list
*p
;
663 if (spec
->num_loopbacks
>= MAX_LOOPBACK_AMPS
) {
664 snd_printk(KERN_ERR
"hda_generic: Too many loopback ctls\n");
667 p
= &spec
->loopback_list
[spec
->num_loopbacks
++];
671 spec
->loopback
.amplist
= spec
->loopback_list
;
674 #define add_input_loopback(codec,nid,dir,idx)
678 * create mixer controls if possible
680 static int create_mixer(struct hda_codec
*codec
, struct hda_gnode
*node
,
681 unsigned int index
, const char *type
,
682 const char *dir_sfx
, int is_loopback
)
687 struct snd_kcontrol_new knew
;
690 sprintf(name
, "%s %s Switch", type
, dir_sfx
);
692 sprintf(name
, "%s Switch", dir_sfx
);
693 if ((node
->wid_caps
& AC_WCAP_IN_AMP
) &&
694 (node
->amp_in_caps
& AC_AMPCAP_MUTE
)) {
695 knew
= (struct snd_kcontrol_new
)HDA_CODEC_MUTE(name
, node
->nid
, index
, HDA_INPUT
);
697 add_input_loopback(codec
, node
->nid
, HDA_INPUT
, index
);
698 snd_printdd("[%s] NID=0x%x, DIR=IN, IDX=0x%x\n", name
, node
->nid
, index
);
699 err
= snd_hda_ctl_add(codec
, node
->nid
,
700 snd_ctl_new1(&knew
, codec
));
704 } else if ((node
->wid_caps
& AC_WCAP_OUT_AMP
) &&
705 (node
->amp_out_caps
& AC_AMPCAP_MUTE
)) {
706 knew
= (struct snd_kcontrol_new
)HDA_CODEC_MUTE(name
, node
->nid
, 0, HDA_OUTPUT
);
708 add_input_loopback(codec
, node
->nid
, HDA_OUTPUT
, 0);
709 snd_printdd("[%s] NID=0x%x, DIR=OUT\n", name
, node
->nid
);
710 err
= snd_hda_ctl_add(codec
, node
->nid
,
711 snd_ctl_new1(&knew
, codec
));
718 sprintf(name
, "%s %s Volume", type
, dir_sfx
);
720 sprintf(name
, "%s Volume", dir_sfx
);
721 if ((node
->wid_caps
& AC_WCAP_IN_AMP
) &&
722 (node
->amp_in_caps
& AC_AMPCAP_NUM_STEPS
)) {
723 knew
= (struct snd_kcontrol_new
)HDA_CODEC_VOLUME(name
, node
->nid
, index
, HDA_INPUT
);
724 snd_printdd("[%s] NID=0x%x, DIR=IN, IDX=0x%x\n", name
, node
->nid
, index
);
725 err
= snd_hda_ctl_add(codec
, node
->nid
,
726 snd_ctl_new1(&knew
, codec
));
730 } else if ((node
->wid_caps
& AC_WCAP_OUT_AMP
) &&
731 (node
->amp_out_caps
& AC_AMPCAP_NUM_STEPS
)) {
732 knew
= (struct snd_kcontrol_new
)HDA_CODEC_VOLUME(name
, node
->nid
, 0, HDA_OUTPUT
);
733 snd_printdd("[%s] NID=0x%x, DIR=OUT\n", name
, node
->nid
);
734 err
= snd_hda_ctl_add(codec
, node
->nid
,
735 snd_ctl_new1(&knew
, codec
));
745 * check whether the controls with the given name and direction suffix already exist
747 static int check_existing_control(struct hda_codec
*codec
, const char *type
, const char *dir
)
749 struct snd_ctl_elem_id id
;
750 memset(&id
, 0, sizeof(id
));
751 sprintf(id
.name
, "%s %s Volume", type
, dir
);
752 id
.iface
= SNDRV_CTL_ELEM_IFACE_MIXER
;
753 if (snd_ctl_find_id(codec
->bus
->card
, &id
))
755 sprintf(id
.name
, "%s %s Switch", type
, dir
);
756 id
.iface
= SNDRV_CTL_ELEM_IFACE_MIXER
;
757 if (snd_ctl_find_id(codec
->bus
->card
, &id
))
763 * build output mixer controls
765 static int create_output_mixers(struct hda_codec
*codec
,
766 const char * const *names
)
768 struct hda_gspec
*spec
= codec
->spec
;
771 for (i
= 0; i
< spec
->pcm_vol_nodes
; i
++) {
772 err
= create_mixer(codec
, spec
->pcm_vol
[i
].node
,
773 spec
->pcm_vol
[i
].index
,
774 names
[i
], "Playback", 0);
781 static int build_output_controls(struct hda_codec
*codec
)
783 struct hda_gspec
*spec
= codec
->spec
;
784 static const char * const types_speaker
[] = { "Speaker", "Headphone" };
785 static const char * const types_line
[] = { "Front", "Headphone" };
787 switch (spec
->pcm_vol_nodes
) {
789 return create_mixer(codec
, spec
->pcm_vol
[0].node
,
790 spec
->pcm_vol
[0].index
,
791 "Master", "Playback", 0);
793 if (defcfg_type(spec
->out_pin_node
[0]) == AC_JACK_SPEAKER
)
794 return create_output_mixers(codec
, types_speaker
);
796 return create_output_mixers(codec
, types_line
);
801 /* create capture volume/switch */
802 static int build_input_controls(struct hda_codec
*codec
)
804 struct hda_gspec
*spec
= codec
->spec
;
805 struct hda_gnode
*adc_node
= spec
->adc_node
;
807 static struct snd_kcontrol_new cap_sel
= {
808 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
809 .name
= "Capture Source",
810 .info
= capture_source_info
,
811 .get
= capture_source_get
,
812 .put
= capture_source_put
,
815 if (! adc_node
|| ! spec
->input_mux
.num_items
)
816 return 0; /* not found */
818 spec
->cur_cap_src
= 0;
819 select_input_connection(codec
, adc_node
,
820 spec
->input_mux
.items
[0].index
);
822 /* create capture volume and switch controls if the ADC has an amp */
823 /* do we have only a single item? */
824 if (spec
->input_mux
.num_items
== 1) {
825 err
= create_mixer(codec
, adc_node
,
826 spec
->input_mux
.items
[0].index
,
833 /* create input MUX if multiple sources are available */
834 err
= snd_hda_ctl_add(codec
, spec
->adc_node
->nid
,
835 snd_ctl_new1(&cap_sel
, codec
));
839 /* no volume control? */
840 if (! (adc_node
->wid_caps
& AC_WCAP_IN_AMP
) ||
841 ! (adc_node
->amp_in_caps
& AC_AMPCAP_NUM_STEPS
))
844 for (i
= 0; i
< spec
->input_mux
.num_items
; i
++) {
845 struct snd_kcontrol_new knew
;
847 sprintf(name
, "%s Capture Volume",
848 spec
->input_mux
.items
[i
].label
);
849 knew
= (struct snd_kcontrol_new
)
850 HDA_CODEC_VOLUME(name
, adc_node
->nid
,
851 spec
->input_mux
.items
[i
].index
,
853 err
= snd_hda_ctl_add(codec
, adc_node
->nid
,
854 snd_ctl_new1(&knew
, codec
));
864 * parse the nodes recursively until reach to the output PIN.
866 * returns 0 - if not found,
867 * 1 - if found, but no mixer is created
868 * 2 - if found and mixer was already created, (just skip)
869 * a negative error code
871 static int parse_loopback_path(struct hda_codec
*codec
, struct hda_gspec
*spec
,
872 struct hda_gnode
*node
, struct hda_gnode
*dest_node
,
881 if (node
== dest_node
) {
882 /* loopback connection found */
886 for (i
= 0; i
< node
->nconns
; i
++) {
887 struct hda_gnode
*child
= hda_get_node(spec
, node
->conn_list
[i
]);
890 err
= parse_loopback_path(codec
, spec
, child
, dest_node
, type
);
895 err
= create_mixer(codec
, node
, i
, type
,
900 return 2; /* ok, created */
901 /* not created, maybe in the lower path */
904 /* connect and unmute */
905 if (node
->nconns
> 1)
906 select_input_connection(codec
, node
, i
);
907 unmute_input(codec
, node
, i
);
908 unmute_output(codec
, node
);
916 * parse the tree and build the loopback controls
918 static int build_loopback_controls(struct hda_codec
*codec
)
920 struct hda_gspec
*spec
= codec
->spec
;
921 struct hda_gnode
*node
;
925 if (! spec
->out_pin_node
[0])
928 list_for_each_entry(node
, &spec
->nid_list
, list
) {
929 if (node
->type
!= AC_WID_PIN
)
932 if (! (node
->pin_caps
& AC_PINCAP_IN
))
934 type
= get_input_type(node
, NULL
);
936 if (check_existing_control(codec
, type
, "Playback"))
938 clear_check_flags(spec
);
939 err
= parse_loopback_path(codec
, spec
,
940 spec
->out_pin_node
[0],
952 * build mixer controls
954 static int build_generic_controls(struct hda_codec
*codec
)
958 if ((err
= build_input_controls(codec
)) < 0 ||
959 (err
= build_output_controls(codec
)) < 0 ||
960 (err
= build_loopback_controls(codec
)) < 0)
969 static struct hda_pcm_stream generic_pcm_playback
= {
975 static int generic_pcm2_prepare(struct hda_pcm_stream
*hinfo
,
976 struct hda_codec
*codec
,
977 unsigned int stream_tag
,
979 struct snd_pcm_substream
*substream
)
981 struct hda_gspec
*spec
= codec
->spec
;
983 snd_hda_codec_setup_stream(codec
, hinfo
->nid
, stream_tag
, 0, format
);
984 snd_hda_codec_setup_stream(codec
, spec
->dac_node
[1]->nid
,
985 stream_tag
, 0, format
);
989 static int generic_pcm2_cleanup(struct hda_pcm_stream
*hinfo
,
990 struct hda_codec
*codec
,
991 struct snd_pcm_substream
*substream
)
993 struct hda_gspec
*spec
= codec
->spec
;
995 snd_hda_codec_cleanup_stream(codec
, hinfo
->nid
);
996 snd_hda_codec_cleanup_stream(codec
, spec
->dac_node
[1]->nid
);
1000 static int build_generic_pcms(struct hda_codec
*codec
)
1002 struct hda_gspec
*spec
= codec
->spec
;
1003 struct hda_pcm
*info
= &spec
->pcm_rec
;
1005 if (! spec
->dac_node
[0] && ! spec
->adc_node
) {
1006 snd_printd("hda_generic: no PCM found\n");
1010 codec
->num_pcms
= 1;
1011 codec
->pcm_info
= info
;
1013 info
->name
= "HDA Generic";
1014 if (spec
->dac_node
[0]) {
1015 info
->stream
[0] = generic_pcm_playback
;
1016 info
->stream
[0].nid
= spec
->dac_node
[0]->nid
;
1017 if (spec
->dac_node
[1]) {
1018 info
->stream
[0].ops
.prepare
= generic_pcm2_prepare
;
1019 info
->stream
[0].ops
.cleanup
= generic_pcm2_cleanup
;
1022 if (spec
->adc_node
) {
1023 info
->stream
[1] = generic_pcm_playback
;
1024 info
->stream
[1].nid
= spec
->adc_node
->nid
;
1030 #ifdef CONFIG_SND_HDA_POWER_SAVE
1031 static int generic_check_power_status(struct hda_codec
*codec
, hda_nid_t nid
)
1033 struct hda_gspec
*spec
= codec
->spec
;
1034 return snd_hda_check_amp_list_power(codec
, &spec
->loopback
, nid
);
1041 static struct hda_codec_ops generic_patch_ops
= {
1042 .build_controls
= build_generic_controls
,
1043 .build_pcms
= build_generic_pcms
,
1044 .free
= snd_hda_generic_free
,
1045 #ifdef CONFIG_SND_HDA_POWER_SAVE
1046 .check_power_status
= generic_check_power_status
,
1051 * the generic parser
1053 int snd_hda_parse_generic_codec(struct hda_codec
*codec
)
1055 struct hda_gspec
*spec
;
1061 spec
= kzalloc(sizeof(*spec
), GFP_KERNEL
);
1063 printk(KERN_ERR
"hda_generic: can't allocate spec\n");
1067 INIT_LIST_HEAD(&spec
->nid_list
);
1069 if ((err
= build_afg_tree(codec
)) < 0)
1072 if ((err
= parse_input(codec
)) < 0 ||
1073 (err
= parse_output(codec
)) < 0)
1076 codec
->patch_ops
= generic_patch_ops
;
1081 snd_hda_generic_free(codec
);
1084 EXPORT_SYMBOL(snd_hda_parse_generic_codec
);