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 <sound/driver.h>
24 #include <linux/init.h>
25 #include <linux/slab.h>
26 #include <sound/core.h>
27 #include "hda_codec.h"
28 #include "hda_local.h"
30 /* widget node for parsing */
32 hda_nid_t nid; /* NID of this widget */
33 unsigned short nconns; /* number of input connections */
35 hda_nid_t slist[2]; /* temporay list */
36 unsigned int wid_caps; /* widget capabilities */
37 unsigned char type; /* widget type */
38 unsigned char pin_ctl; /* pin controls */
39 unsigned char checked; /* the flag indicates that the node is already parsed */
40 unsigned int pin_caps; /* pin widget capabilities */
41 unsigned int def_cfg; /* default configuration */
42 unsigned int amp_out_caps; /* AMP out capabilities */
43 unsigned int amp_in_caps; /* AMP in capabilities */
44 struct list_head list;
47 /* patch-specific record */
49 #define MAX_PCM_VOLS 2
51 struct hda_gnode *node; /* Node for PCM volume */
52 unsigned int index; /* connection of PCM volume */
56 struct hda_gnode *dac_node[2]; /* DAC node */
57 struct hda_gnode *out_pin_node[2]; /* Output pin (Line-Out) node */
58 struct pcm_vol pcm_vol[MAX_PCM_VOLS]; /* PCM volumes */
59 unsigned int pcm_vol_nodes; /* number of PCM volumes */
61 struct hda_gnode *adc_node; /* ADC node */
62 struct hda_gnode *cap_vol_node; /* Node for capture volume */
63 unsigned int cur_cap_src; /* current capture source */
64 struct hda_input_mux input_mux;
65 char cap_labels[HDA_MAX_NUM_INPUTS][16];
67 unsigned int def_amp_in_caps;
68 unsigned int def_amp_out_caps;
70 struct hda_pcm pcm_rec; /* PCM information */
72 struct list_head nid_list; /* list of widgets */
74 #ifdef CONFIG_SND_HDA_POWER_SAVE
75 #define MAX_LOOPBACK_AMPS 7
76 struct hda_loopback_check loopback;
78 struct hda_amp_list loopback_list[MAX_LOOPBACK_AMPS + 1];
83 * retrieve the default device type from the default config value
85 #define defcfg_type(node) (((node)->def_cfg & AC_DEFCFG_DEVICE) >> \
86 AC_DEFCFG_DEVICE_SHIFT)
87 #define defcfg_location(node) (((node)->def_cfg & AC_DEFCFG_LOCATION) >> \
88 AC_DEFCFG_LOCATION_SHIFT)
89 #define defcfg_port_conn(node) (((node)->def_cfg & AC_DEFCFG_PORT_CONN) >> \
90 AC_DEFCFG_PORT_CONN_SHIFT)
95 static void snd_hda_generic_free(struct hda_codec *codec)
97 struct hda_gspec *spec = codec->spec;
98 struct list_head *p, *n;
102 /* free all widgets */
103 list_for_each_safe(p, n, &spec->nid_list) {
104 struct hda_gnode *node = list_entry(p, struct hda_gnode, list);
105 if (node->conn_list != node->slist)
106 kfree(node->conn_list);
114 * add a new widget node and read its attributes
116 static int add_new_node(struct hda_codec *codec, struct hda_gspec *spec, hda_nid_t nid)
118 struct hda_gnode *node;
120 hda_nid_t conn_list[HDA_MAX_CONNECTIONS];
122 node = kzalloc(sizeof(*node), GFP_KERNEL);
126 nconns = snd_hda_get_connections(codec, nid, conn_list,
127 HDA_MAX_CONNECTIONS);
132 if (nconns <= ARRAY_SIZE(node->slist))
133 node->conn_list = node->slist;
135 node->conn_list = kmalloc(sizeof(hda_nid_t) * nconns,
137 if (! node->conn_list) {
138 snd_printk(KERN_ERR "hda-generic: cannot malloc\n");
143 memcpy(node->conn_list, conn_list, nconns * sizeof(hda_nid_t));
144 node->nconns = nconns;
145 node->wid_caps = get_wcaps(codec, nid);
146 node->type = (node->wid_caps & AC_WCAP_TYPE) >> AC_WCAP_TYPE_SHIFT;
148 if (node->type == AC_WID_PIN) {
149 node->pin_caps = snd_hda_param_read(codec, node->nid, AC_PAR_PIN_CAP);
150 node->pin_ctl = snd_hda_codec_read(codec, node->nid, 0, AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
151 node->def_cfg = snd_hda_codec_read(codec, node->nid, 0, AC_VERB_GET_CONFIG_DEFAULT, 0);
154 if (node->wid_caps & AC_WCAP_OUT_AMP) {
155 if (node->wid_caps & AC_WCAP_AMP_OVRD)
156 node->amp_out_caps = snd_hda_param_read(codec, node->nid, AC_PAR_AMP_OUT_CAP);
157 if (! node->amp_out_caps)
158 node->amp_out_caps = spec->def_amp_out_caps;
160 if (node->wid_caps & AC_WCAP_IN_AMP) {
161 if (node->wid_caps & AC_WCAP_AMP_OVRD)
162 node->amp_in_caps = snd_hda_param_read(codec, node->nid, AC_PAR_AMP_IN_CAP);
163 if (! node->amp_in_caps)
164 node->amp_in_caps = spec->def_amp_in_caps;
166 list_add_tail(&node->list, &spec->nid_list);
171 * build the AFG subtree
173 static int build_afg_tree(struct hda_codec *codec)
175 struct hda_gspec *spec = codec->spec;
179 snd_assert(spec, return -EINVAL);
181 spec->def_amp_out_caps = snd_hda_param_read(codec, codec->afg, AC_PAR_AMP_OUT_CAP);
182 spec->def_amp_in_caps = snd_hda_param_read(codec, codec->afg, AC_PAR_AMP_IN_CAP);
184 nodes = snd_hda_get_sub_nodes(codec, codec->afg, &nid);
185 if (! nid || nodes < 0) {
186 printk(KERN_ERR "Invalid AFG subtree\n");
190 /* parse all nodes belonging to the AFG */
191 for (i = 0; i < nodes; i++, nid++) {
192 if ((err = add_new_node(codec, spec, nid)) < 0)
201 * look for the node record for the given NID
203 /* FIXME: should avoid the braindead linear search */
204 static struct hda_gnode *hda_get_node(struct hda_gspec *spec, hda_nid_t nid)
207 struct hda_gnode *node;
209 list_for_each(p, &spec->nid_list) {
210 node = list_entry(p, struct hda_gnode, 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)
264 struct hda_gnode *node;
266 list_for_each(p, &spec->nid_list) {
267 node = list_entry(p, struct hda_gnode, list);
273 * parse the output path recursively until reach to an audio output widget
275 * returns 0 if not found, 1 if found, or a negative error code.
277 static int parse_output_path(struct hda_codec *codec, struct hda_gspec *spec,
278 struct hda_gnode *node, int dac_idx)
281 struct hda_gnode *child;
287 if (node->type == AC_WID_AUD_OUT) {
288 if (node->wid_caps & AC_WCAP_DIGITAL) {
289 snd_printdd("Skip Digital OUT node %x\n", node->nid);
292 snd_printdd("AUD_OUT found %x\n", node->nid);
293 if (spec->dac_node[dac_idx]) {
294 /* already DAC node is assigned, just unmute & connect */
295 return node == spec->dac_node[dac_idx];
297 spec->dac_node[dac_idx] = node;
298 if ((node->wid_caps & AC_WCAP_OUT_AMP) &&
299 spec->pcm_vol_nodes < MAX_PCM_VOLS) {
300 spec->pcm_vol[spec->pcm_vol_nodes].node = node;
301 spec->pcm_vol[spec->pcm_vol_nodes].index = 0;
302 spec->pcm_vol_nodes++;
304 return 1; /* found */
307 for (i = 0; i < node->nconns; i++) {
308 child = hda_get_node(spec, node->conn_list[i]);
311 err = parse_output_path(codec, spec, child, dac_idx);
316 * select the path, unmute both input and output
318 if (node->nconns > 1)
319 select_input_connection(codec, node, i);
320 unmute_input(codec, node, i);
321 unmute_output(codec, node);
322 if (spec->dac_node[dac_idx] &&
323 spec->pcm_vol_nodes < MAX_PCM_VOLS &&
324 !(spec->dac_node[dac_idx]->wid_caps &
326 if ((node->wid_caps & AC_WCAP_IN_AMP) ||
327 (node->wid_caps & AC_WCAP_OUT_AMP)) {
328 int n = spec->pcm_vol_nodes;
329 spec->pcm_vol[n].node = node;
330 spec->pcm_vol[n].index = i;
331 spec->pcm_vol_nodes++;
341 * Look for the output PIN widget with the given jack type
342 * and parse the output path to that PIN.
344 * Returns the PIN node when the path to DAC is established.
346 static struct hda_gnode *parse_output_jack(struct hda_codec *codec,
347 struct hda_gspec *spec,
351 struct hda_gnode *node;
354 list_for_each(p, &spec->nid_list) {
355 node = list_entry(p, struct hda_gnode, list);
356 if (node->type != AC_WID_PIN)
358 /* output capable? */
359 if (! (node->pin_caps & AC_PINCAP_OUT))
361 if (defcfg_port_conn(node) == AC_JACK_PORT_NONE)
362 continue; /* unconnected */
363 if (jack_type >= 0) {
364 if (jack_type != defcfg_type(node))
366 if (node->wid_caps & AC_WCAP_DIGITAL)
367 continue; /* skip SPDIF */
369 /* output as default? */
370 if (! (node->pin_ctl & AC_PINCTL_OUT_EN))
373 clear_check_flags(spec);
374 err = parse_output_path(codec, spec, node, 0);
377 if (! err && spec->out_pin_node[0]) {
378 err = parse_output_path(codec, spec, node, 1);
383 /* unmute the PIN output */
384 unmute_output(codec, node);
385 /* set PIN-Out enable */
386 snd_hda_codec_write_cache(codec, node->nid, 0,
387 AC_VERB_SET_PIN_WIDGET_CONTROL,
389 ((node->pin_caps & AC_PINCAP_HP_DRV) ?
390 AC_PINCTL_HP_EN : 0));
401 static int parse_output(struct hda_codec *codec)
403 struct hda_gspec *spec = codec->spec;
404 struct hda_gnode *node;
407 * Look for the output PIN widget
409 /* first, look for the line-out pin */
410 node = parse_output_jack(codec, spec, AC_JACK_LINE_OUT);
411 if (node) /* found, remember the PIN node */
412 spec->out_pin_node[0] = node;
414 /* if no line-out is found, try speaker out */
415 node = parse_output_jack(codec, spec, AC_JACK_SPEAKER);
417 spec->out_pin_node[0] = node;
419 /* look for the HP-out pin */
420 node = parse_output_jack(codec, spec, AC_JACK_HP_OUT);
422 if (! spec->out_pin_node[0])
423 spec->out_pin_node[0] = node;
425 spec->out_pin_node[1] = node;
428 if (! spec->out_pin_node[0]) {
429 /* no line-out or HP pins found,
430 * then choose for the first output pin
432 spec->out_pin_node[0] = parse_output_jack(codec, spec, -1);
433 if (! spec->out_pin_node[0])
434 snd_printd("hda_generic: no proper output path found\n");
444 /* control callbacks */
445 static int capture_source_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
447 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
448 struct hda_gspec *spec = codec->spec;
449 return snd_hda_input_mux_info(&spec->input_mux, uinfo);
452 static int capture_source_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
454 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
455 struct hda_gspec *spec = codec->spec;
457 ucontrol->value.enumerated.item[0] = spec->cur_cap_src;
461 static int capture_source_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
463 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
464 struct hda_gspec *spec = codec->spec;
465 return snd_hda_input_mux_put(codec, &spec->input_mux, ucontrol,
466 spec->adc_node->nid, &spec->cur_cap_src);
470 * return the string name of the given input PIN widget
472 static const char *get_input_type(struct hda_gnode *node, unsigned int *pinctl)
474 unsigned int location = defcfg_location(node);
475 switch (defcfg_type(node)) {
476 case AC_JACK_LINE_IN:
477 if ((location & 0x0f) == AC_JACK_LOC_FRONT)
483 *pinctl |= AC_PINCTL_VREF_GRD;
487 if ((location & 0x0f) == AC_JACK_LOC_FRONT)
493 (AC_PINCAP_VREF_80 << AC_PINCAP_VREF_SHIFT)))
494 *pinctl |= AC_PINCTL_VREF_80;
495 if ((location & 0x0f) == AC_JACK_LOC_FRONT)
498 case AC_JACK_SPDIF_IN:
500 case AC_JACK_DIG_OTHER_IN:
507 * parse the nodes recursively until reach to the input PIN
509 * returns 0 if not found, 1 if found, or a negative error code.
511 static int parse_adc_sub_nodes(struct hda_codec *codec, struct hda_gspec *spec,
512 struct hda_gnode *node)
523 if (node->type != AC_WID_PIN) {
524 for (i = 0; i < node->nconns; i++) {
525 struct hda_gnode *child;
526 child = hda_get_node(spec, node->conn_list[i]);
529 err = parse_adc_sub_nodes(codec, spec, child);
534 * select the path, unmute both input and output
536 if (node->nconns > 1)
537 select_input_connection(codec, node, i);
538 unmute_input(codec, node, i);
539 unmute_output(codec, node);
547 if (! (node->pin_caps & AC_PINCAP_IN))
550 if (defcfg_port_conn(node) == AC_JACK_PORT_NONE)
551 return 0; /* unconnected */
553 if (node->wid_caps & AC_WCAP_DIGITAL)
554 return 0; /* skip SPDIF */
556 if (spec->input_mux.num_items >= HDA_MAX_NUM_INPUTS) {
557 snd_printk(KERN_ERR "hda_generic: Too many items for capture\n");
561 pinctl = AC_PINCTL_IN_EN;
562 /* create a proper capture source label */
563 type = get_input_type(node, &pinctl);
565 /* input as default? */
566 if (! (node->pin_ctl & AC_PINCTL_IN_EN))
570 label = spec->cap_labels[spec->input_mux.num_items];
572 spec->input_mux.items[spec->input_mux.num_items].label = label;
574 /* unmute the PIN external input */
575 unmute_input(codec, node, 0); /* index = 0? */
576 /* set PIN-In enable */
577 snd_hda_codec_write_cache(codec, node->nid, 0,
578 AC_VERB_SET_PIN_WIDGET_CONTROL, pinctl);
580 return 1; /* found */
583 /* add a capture source element */
584 static void add_cap_src(struct hda_gspec *spec, int idx)
586 struct hda_input_mux_item *csrc;
590 num = spec->input_mux.num_items;
591 csrc = &spec->input_mux.items[num];
592 buf = spec->cap_labels[num];
593 for (ocap = 0; ocap < num; ocap++) {
594 if (! strcmp(buf, spec->cap_labels[ocap])) {
595 /* same label already exists,
596 * put the index number to be unique
598 sprintf(buf, "%s %d", spec->cap_labels[ocap], num);
603 spec->input_mux.num_items++;
609 static int parse_input_path(struct hda_codec *codec, struct hda_gnode *adc_node)
611 struct hda_gspec *spec = codec->spec;
612 struct hda_gnode *node;
615 snd_printdd("AUD_IN = %x\n", adc_node->nid);
616 clear_check_flags(spec);
618 // awk added - fixed no recording due to muted widget
619 unmute_input(codec, adc_node, 0);
622 * check each connection of the ADC
623 * if it reaches to a proper input PIN, add the path as the
626 /* first, check the direct connections to PIN widgets */
627 for (i = 0; i < adc_node->nconns; i++) {
628 node = hda_get_node(spec, adc_node->conn_list[i]);
629 if (node && node->type == AC_WID_PIN) {
630 err = parse_adc_sub_nodes(codec, spec, node);
634 add_cap_src(spec, i);
637 /* ... then check the rests, more complicated connections */
638 for (i = 0; i < adc_node->nconns; i++) {
639 node = hda_get_node(spec, adc_node->conn_list[i]);
640 if (node && node->type != AC_WID_PIN) {
641 err = parse_adc_sub_nodes(codec, spec, node);
645 add_cap_src(spec, i);
649 if (! spec->input_mux.num_items)
650 return 0; /* no input path found... */
652 snd_printdd("[Capture Source] NID=0x%x, #SRC=%d\n", adc_node->nid, spec->input_mux.num_items);
653 for (i = 0; i < spec->input_mux.num_items; i++)
654 snd_printdd(" [%s] IDX=0x%x\n", spec->input_mux.items[i].label,
655 spec->input_mux.items[i].index);
657 spec->adc_node = adc_node;
664 static int parse_input(struct hda_codec *codec)
666 struct hda_gspec *spec = codec->spec;
668 struct hda_gnode *node;
672 * At first we look for an audio input widget.
673 * If it reaches to certain input PINs, we take it as the
676 list_for_each(p, &spec->nid_list) {
677 node = list_entry(p, struct hda_gnode, list);
678 if (node->wid_caps & AC_WCAP_DIGITAL)
679 continue; /* skip SPDIF */
680 if (node->type == AC_WID_AUD_IN) {
681 err = parse_input_path(codec, node);
688 snd_printd("hda_generic: no proper input path found\n");
692 #ifdef CONFIG_SND_HDA_POWER_SAVE
693 static void add_input_loopback(struct hda_codec *codec, hda_nid_t nid,
696 struct hda_gspec *spec = codec->spec;
697 struct hda_amp_list *p;
699 if (spec->num_loopbacks >= MAX_LOOPBACK_AMPS) {
700 snd_printk(KERN_ERR "hda_generic: Too many loopback ctls\n");
703 p = &spec->loopback_list[spec->num_loopbacks++];
707 spec->loopback.amplist = spec->loopback_list;
710 #define add_input_loopback(codec,nid,dir,idx)
714 * create mixer controls if possible
716 static int create_mixer(struct hda_codec *codec, struct hda_gnode *node,
717 unsigned int index, const char *type,
718 const char *dir_sfx, int is_loopback)
723 struct snd_kcontrol_new knew;
726 sprintf(name, "%s %s Switch", type, dir_sfx);
728 sprintf(name, "%s Switch", dir_sfx);
729 if ((node->wid_caps & AC_WCAP_IN_AMP) &&
730 (node->amp_in_caps & AC_AMPCAP_MUTE)) {
731 knew = (struct snd_kcontrol_new)HDA_CODEC_MUTE(name, node->nid, index, HDA_INPUT);
733 add_input_loopback(codec, node->nid, HDA_INPUT, index);
734 snd_printdd("[%s] NID=0x%x, DIR=IN, IDX=0x%x\n", name, node->nid, index);
735 if ((err = snd_ctl_add(codec->bus->card, snd_ctl_new1(&knew, codec))) < 0)
738 } else if ((node->wid_caps & AC_WCAP_OUT_AMP) &&
739 (node->amp_out_caps & AC_AMPCAP_MUTE)) {
740 knew = (struct snd_kcontrol_new)HDA_CODEC_MUTE(name, node->nid, 0, HDA_OUTPUT);
742 add_input_loopback(codec, node->nid, HDA_OUTPUT, 0);
743 snd_printdd("[%s] NID=0x%x, DIR=OUT\n", name, node->nid);
744 if ((err = snd_ctl_add(codec->bus->card, snd_ctl_new1(&knew, codec))) < 0)
750 sprintf(name, "%s %s Volume", type, dir_sfx);
752 sprintf(name, "%s Volume", dir_sfx);
753 if ((node->wid_caps & AC_WCAP_IN_AMP) &&
754 (node->amp_in_caps & AC_AMPCAP_NUM_STEPS)) {
755 knew = (struct snd_kcontrol_new)HDA_CODEC_VOLUME(name, node->nid, index, HDA_INPUT);
756 snd_printdd("[%s] NID=0x%x, DIR=IN, IDX=0x%x\n", name, node->nid, index);
757 if ((err = snd_ctl_add(codec->bus->card, snd_ctl_new1(&knew, codec))) < 0)
760 } else if ((node->wid_caps & AC_WCAP_OUT_AMP) &&
761 (node->amp_out_caps & AC_AMPCAP_NUM_STEPS)) {
762 knew = (struct snd_kcontrol_new)HDA_CODEC_VOLUME(name, node->nid, 0, HDA_OUTPUT);
763 snd_printdd("[%s] NID=0x%x, DIR=OUT\n", name, node->nid);
764 if ((err = snd_ctl_add(codec->bus->card, snd_ctl_new1(&knew, codec))) < 0)
773 * check whether the controls with the given name and direction suffix already exist
775 static int check_existing_control(struct hda_codec *codec, const char *type, const char *dir)
777 struct snd_ctl_elem_id id;
778 memset(&id, 0, sizeof(id));
779 sprintf(id.name, "%s %s Volume", type, dir);
780 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
781 if (snd_ctl_find_id(codec->bus->card, &id))
783 sprintf(id.name, "%s %s Switch", type, dir);
784 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
785 if (snd_ctl_find_id(codec->bus->card, &id))
791 * build output mixer controls
793 static int create_output_mixers(struct hda_codec *codec, const char **names)
795 struct hda_gspec *spec = codec->spec;
798 for (i = 0; i < spec->pcm_vol_nodes; i++) {
799 err = create_mixer(codec, spec->pcm_vol[i].node,
800 spec->pcm_vol[i].index,
801 names[i], "Playback", 0);
808 static int build_output_controls(struct hda_codec *codec)
810 struct hda_gspec *spec = codec->spec;
811 static const char *types_speaker[] = { "Speaker", "Headphone" };
812 static const char *types_line[] = { "Front", "Headphone" };
814 switch (spec->pcm_vol_nodes) {
816 return create_mixer(codec, spec->pcm_vol[0].node,
817 spec->pcm_vol[0].index,
818 "Master", "Playback", 0);
820 if (defcfg_type(spec->out_pin_node[0]) == AC_JACK_SPEAKER)
821 return create_output_mixers(codec, types_speaker);
823 return create_output_mixers(codec, types_line);
828 /* create capture volume/switch */
829 static int build_input_controls(struct hda_codec *codec)
831 struct hda_gspec *spec = codec->spec;
832 struct hda_gnode *adc_node = spec->adc_node;
834 static struct snd_kcontrol_new cap_sel = {
835 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
836 .name = "Capture Source",
837 .info = capture_source_info,
838 .get = capture_source_get,
839 .put = capture_source_put,
842 if (! adc_node || ! spec->input_mux.num_items)
843 return 0; /* not found */
845 spec->cur_cap_src = 0;
846 select_input_connection(codec, adc_node,
847 spec->input_mux.items[0].index);
849 /* create capture volume and switch controls if the ADC has an amp */
850 /* do we have only a single item? */
851 if (spec->input_mux.num_items == 1) {
852 err = create_mixer(codec, adc_node,
853 spec->input_mux.items[0].index,
860 /* create input MUX if multiple sources are available */
861 if ((err = snd_ctl_add(codec->bus->card,
862 snd_ctl_new1(&cap_sel, codec))) < 0)
865 /* no volume control? */
866 if (! (adc_node->wid_caps & AC_WCAP_IN_AMP) ||
867 ! (adc_node->amp_in_caps & AC_AMPCAP_NUM_STEPS))
870 for (i = 0; i < spec->input_mux.num_items; i++) {
871 struct snd_kcontrol_new knew;
873 sprintf(name, "%s Capture Volume",
874 spec->input_mux.items[i].label);
875 knew = (struct snd_kcontrol_new)
876 HDA_CODEC_VOLUME(name, adc_node->nid,
877 spec->input_mux.items[i].index,
879 if ((err = snd_ctl_add(codec->bus->card,
880 snd_ctl_new1(&knew, codec))) < 0)
889 * parse the nodes recursively until reach to the output PIN.
891 * returns 0 - if not found,
892 * 1 - if found, but no mixer is created
893 * 2 - if found and mixer was already created, (just skip)
894 * a negative error code
896 static int parse_loopback_path(struct hda_codec *codec, struct hda_gspec *spec,
897 struct hda_gnode *node, struct hda_gnode *dest_node,
906 if (node == dest_node) {
907 /* loopback connection found */
911 for (i = 0; i < node->nconns; i++) {
912 struct hda_gnode *child = hda_get_node(spec, node->conn_list[i]);
915 err = parse_loopback_path(codec, spec, child, dest_node, type);
920 err = create_mixer(codec, node, i, type,
925 return 2; /* ok, created */
926 /* not created, maybe in the lower path */
929 /* connect and unmute */
930 if (node->nconns > 1)
931 select_input_connection(codec, node, i);
932 unmute_input(codec, node, i);
933 unmute_output(codec, node);
941 * parse the tree and build the loopback controls
943 static int build_loopback_controls(struct hda_codec *codec)
945 struct hda_gspec *spec = codec->spec;
947 struct hda_gnode *node;
951 if (! spec->out_pin_node[0])
954 list_for_each(p, &spec->nid_list) {
955 node = list_entry(p, struct hda_gnode, list);
956 if (node->type != AC_WID_PIN)
959 if (! (node->pin_caps & AC_PINCAP_IN))
961 type = get_input_type(node, NULL);
963 if (check_existing_control(codec, type, "Playback"))
965 clear_check_flags(spec);
966 err = parse_loopback_path(codec, spec,
967 spec->out_pin_node[0],
979 * build mixer controls
981 static int build_generic_controls(struct hda_codec *codec)
985 if ((err = build_input_controls(codec)) < 0 ||
986 (err = build_output_controls(codec)) < 0 ||
987 (err = build_loopback_controls(codec)) < 0)
996 static struct hda_pcm_stream generic_pcm_playback = {
1002 static int generic_pcm2_prepare(struct hda_pcm_stream *hinfo,
1003 struct hda_codec *codec,
1004 unsigned int stream_tag,
1005 unsigned int format,
1006 struct snd_pcm_substream *substream)
1008 struct hda_gspec *spec = codec->spec;
1010 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
1011 snd_hda_codec_setup_stream(codec, spec->dac_node[1]->nid,
1012 stream_tag, 0, format);
1016 static int generic_pcm2_cleanup(struct hda_pcm_stream *hinfo,
1017 struct hda_codec *codec,
1018 struct snd_pcm_substream *substream)
1020 struct hda_gspec *spec = codec->spec;
1022 snd_hda_codec_setup_stream(codec, hinfo->nid, 0, 0, 0);
1023 snd_hda_codec_setup_stream(codec, spec->dac_node[1]->nid, 0, 0, 0);
1027 static int build_generic_pcms(struct hda_codec *codec)
1029 struct hda_gspec *spec = codec->spec;
1030 struct hda_pcm *info = &spec->pcm_rec;
1032 if (! spec->dac_node[0] && ! spec->adc_node) {
1033 snd_printd("hda_generic: no PCM found\n");
1037 codec->num_pcms = 1;
1038 codec->pcm_info = info;
1040 info->name = "HDA Generic";
1041 if (spec->dac_node[0]) {
1042 info->stream[0] = generic_pcm_playback;
1043 info->stream[0].nid = spec->dac_node[0]->nid;
1044 if (spec->dac_node[1]) {
1045 info->stream[0].ops.prepare = generic_pcm2_prepare;
1046 info->stream[0].ops.cleanup = generic_pcm2_cleanup;
1049 if (spec->adc_node) {
1050 info->stream[1] = generic_pcm_playback;
1051 info->stream[1].nid = spec->adc_node->nid;
1057 #ifdef CONFIG_SND_HDA_POWER_SAVE
1058 static int generic_check_power_status(struct hda_codec *codec, hda_nid_t nid)
1060 struct hda_gspec *spec = codec->spec;
1061 return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
1068 static struct hda_codec_ops generic_patch_ops = {
1069 .build_controls = build_generic_controls,
1070 .build_pcms = build_generic_pcms,
1071 .free = snd_hda_generic_free,
1072 #ifdef CONFIG_SND_HDA_POWER_SAVE
1073 .check_power_status = generic_check_power_status,
1078 * the generic parser
1080 int snd_hda_parse_generic_codec(struct hda_codec *codec)
1082 struct hda_gspec *spec;
1088 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
1090 printk(KERN_ERR "hda_generic: can't allocate spec\n");
1094 INIT_LIST_HEAD(&spec->nid_list);
1096 if ((err = build_afg_tree(codec)) < 0)
1099 if ((err = parse_input(codec)) < 0 ||
1100 (err = parse_output(codec)) < 0)
1103 codec->patch_ops = generic_patch_ops;
1108 snd_hda_generic_free(codec);