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 hda_gnode *node, *n;
102 /* free all widgets */
103 list_for_each_entry_safe(node, n, &spec->nid_list, list) {
104 if (node->conn_list != node->slist)
105 kfree(node->conn_list);
113 * add a new widget node and read its attributes
115 static int add_new_node(struct hda_codec *codec, struct hda_gspec *spec, hda_nid_t nid)
117 struct hda_gnode *node;
119 hda_nid_t conn_list[HDA_MAX_CONNECTIONS];
121 node = kzalloc(sizeof(*node), GFP_KERNEL);
125 nconns = snd_hda_get_connections(codec, nid, conn_list,
126 HDA_MAX_CONNECTIONS);
131 if (nconns <= ARRAY_SIZE(node->slist))
132 node->conn_list = node->slist;
134 node->conn_list = kmalloc(sizeof(hda_nid_t) * nconns,
136 if (! node->conn_list) {
137 snd_printk(KERN_ERR "hda-generic: cannot malloc\n");
142 memcpy(node->conn_list, conn_list, nconns * sizeof(hda_nid_t));
143 node->nconns = nconns;
144 node->wid_caps = get_wcaps(codec, nid);
145 node->type = (node->wid_caps & AC_WCAP_TYPE) >> AC_WCAP_TYPE_SHIFT;
147 if (node->type == AC_WID_PIN) {
148 node->pin_caps = snd_hda_param_read(codec, node->nid, AC_PAR_PIN_CAP);
149 node->pin_ctl = snd_hda_codec_read(codec, node->nid, 0, AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
150 node->def_cfg = snd_hda_codec_read(codec, node->nid, 0, AC_VERB_GET_CONFIG_DEFAULT, 0);
153 if (node->wid_caps & AC_WCAP_OUT_AMP) {
154 if (node->wid_caps & AC_WCAP_AMP_OVRD)
155 node->amp_out_caps = snd_hda_param_read(codec, node->nid, AC_PAR_AMP_OUT_CAP);
156 if (! node->amp_out_caps)
157 node->amp_out_caps = spec->def_amp_out_caps;
159 if (node->wid_caps & AC_WCAP_IN_AMP) {
160 if (node->wid_caps & AC_WCAP_AMP_OVRD)
161 node->amp_in_caps = snd_hda_param_read(codec, node->nid, AC_PAR_AMP_IN_CAP);
162 if (! node->amp_in_caps)
163 node->amp_in_caps = spec->def_amp_in_caps;
165 list_add_tail(&node->list, &spec->nid_list);
170 * build the AFG subtree
172 static int build_afg_tree(struct hda_codec *codec)
174 struct hda_gspec *spec = codec->spec;
178 snd_assert(spec, return -EINVAL);
180 spec->def_amp_out_caps = snd_hda_param_read(codec, codec->afg, AC_PAR_AMP_OUT_CAP);
181 spec->def_amp_in_caps = snd_hda_param_read(codec, codec->afg, AC_PAR_AMP_IN_CAP);
183 nodes = snd_hda_get_sub_nodes(codec, codec->afg, &nid);
184 if (! nid || nodes < 0) {
185 printk(KERN_ERR "Invalid AFG subtree\n");
189 /* parse all nodes belonging to the AFG */
190 for (i = 0; i < nodes; i++, nid++) {
191 if ((err = add_new_node(codec, spec, nid)) < 0)
200 * look for the node record for the given NID
202 /* FIXME: should avoid the braindead linear search */
203 static struct hda_gnode *hda_get_node(struct hda_gspec *spec, hda_nid_t nid)
205 struct hda_gnode *node;
207 list_for_each_entry(node, &spec->nid_list, list) {
208 if (node->nid == nid)
215 * unmute (and set max vol) the output amplifier
217 static int unmute_output(struct hda_codec *codec, struct hda_gnode *node)
219 unsigned int val, ofs;
220 snd_printdd("UNMUTE OUT: NID=0x%x\n", node->nid);
221 val = (node->amp_out_caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
222 ofs = (node->amp_out_caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT;
225 snd_hda_codec_amp_stereo(codec, node->nid, HDA_OUTPUT, 0, 0xff, val);
230 * unmute (and set max vol) the input amplifier
232 static int unmute_input(struct hda_codec *codec, struct hda_gnode *node, unsigned int index)
234 unsigned int val, ofs;
235 snd_printdd("UNMUTE IN: NID=0x%x IDX=0x%x\n", node->nid, index);
236 val = (node->amp_in_caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
237 ofs = (node->amp_in_caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT;
240 snd_hda_codec_amp_stereo(codec, node->nid, HDA_INPUT, index, 0xff, val);
245 * select the input connection of the given node.
247 static int select_input_connection(struct hda_codec *codec, struct hda_gnode *node,
250 snd_printdd("CONNECT: NID=0x%x IDX=0x%x\n", node->nid, index);
251 return snd_hda_codec_write_cache(codec, node->nid, 0,
252 AC_VERB_SET_CONNECT_SEL, index);
256 * clear checked flag of each node in the node list
258 static void clear_check_flags(struct hda_gspec *spec)
260 struct hda_gnode *node;
262 list_for_each_entry(node, &spec->nid_list, list) {
268 * parse the output path recursively until reach to an audio output widget
270 * returns 0 if not found, 1 if found, or a negative error code.
272 static int parse_output_path(struct hda_codec *codec, struct hda_gspec *spec,
273 struct hda_gnode *node, int dac_idx)
276 struct hda_gnode *child;
282 if (node->type == AC_WID_AUD_OUT) {
283 if (node->wid_caps & AC_WCAP_DIGITAL) {
284 snd_printdd("Skip Digital OUT node %x\n", node->nid);
287 snd_printdd("AUD_OUT found %x\n", node->nid);
288 if (spec->dac_node[dac_idx]) {
289 /* already DAC node is assigned, just unmute & connect */
290 return node == spec->dac_node[dac_idx];
292 spec->dac_node[dac_idx] = node;
293 if ((node->wid_caps & AC_WCAP_OUT_AMP) &&
294 spec->pcm_vol_nodes < MAX_PCM_VOLS) {
295 spec->pcm_vol[spec->pcm_vol_nodes].node = node;
296 spec->pcm_vol[spec->pcm_vol_nodes].index = 0;
297 spec->pcm_vol_nodes++;
299 return 1; /* found */
302 for (i = 0; i < node->nconns; i++) {
303 child = hda_get_node(spec, node->conn_list[i]);
306 err = parse_output_path(codec, spec, child, dac_idx);
311 * select the path, unmute both input and output
313 if (node->nconns > 1)
314 select_input_connection(codec, node, i);
315 unmute_input(codec, node, i);
316 unmute_output(codec, node);
317 if (spec->dac_node[dac_idx] &&
318 spec->pcm_vol_nodes < MAX_PCM_VOLS &&
319 !(spec->dac_node[dac_idx]->wid_caps &
321 if ((node->wid_caps & AC_WCAP_IN_AMP) ||
322 (node->wid_caps & AC_WCAP_OUT_AMP)) {
323 int n = spec->pcm_vol_nodes;
324 spec->pcm_vol[n].node = node;
325 spec->pcm_vol[n].index = i;
326 spec->pcm_vol_nodes++;
336 * Look for the output PIN widget with the given jack type
337 * and parse the output path to that PIN.
339 * Returns the PIN node when the path to DAC is established.
341 static struct hda_gnode *parse_output_jack(struct hda_codec *codec,
342 struct hda_gspec *spec,
345 struct hda_gnode *node;
348 list_for_each_entry(node, &spec->nid_list, list) {
349 if (node->type != AC_WID_PIN)
351 /* output capable? */
352 if (! (node->pin_caps & AC_PINCAP_OUT))
354 if (defcfg_port_conn(node) == AC_JACK_PORT_NONE)
355 continue; /* unconnected */
356 if (jack_type >= 0) {
357 if (jack_type != defcfg_type(node))
359 if (node->wid_caps & AC_WCAP_DIGITAL)
360 continue; /* skip SPDIF */
362 /* output as default? */
363 if (! (node->pin_ctl & AC_PINCTL_OUT_EN))
366 clear_check_flags(spec);
367 err = parse_output_path(codec, spec, node, 0);
370 if (! err && spec->out_pin_node[0]) {
371 err = parse_output_path(codec, spec, node, 1);
376 /* unmute the PIN output */
377 unmute_output(codec, node);
378 /* set PIN-Out enable */
379 snd_hda_codec_write_cache(codec, node->nid, 0,
380 AC_VERB_SET_PIN_WIDGET_CONTROL,
382 ((node->pin_caps & AC_PINCAP_HP_DRV) ?
383 AC_PINCTL_HP_EN : 0));
394 static int parse_output(struct hda_codec *codec)
396 struct hda_gspec *spec = codec->spec;
397 struct hda_gnode *node;
400 * Look for the output PIN widget
402 /* first, look for the line-out pin */
403 node = parse_output_jack(codec, spec, AC_JACK_LINE_OUT);
404 if (node) /* found, remember the PIN node */
405 spec->out_pin_node[0] = node;
407 /* if no line-out is found, try speaker out */
408 node = parse_output_jack(codec, spec, AC_JACK_SPEAKER);
410 spec->out_pin_node[0] = node;
412 /* look for the HP-out pin */
413 node = parse_output_jack(codec, spec, AC_JACK_HP_OUT);
415 if (! spec->out_pin_node[0])
416 spec->out_pin_node[0] = node;
418 spec->out_pin_node[1] = node;
421 if (! spec->out_pin_node[0]) {
422 /* no line-out or HP pins found,
423 * then choose for the first output pin
425 spec->out_pin_node[0] = parse_output_jack(codec, spec, -1);
426 if (! spec->out_pin_node[0])
427 snd_printd("hda_generic: no proper output path found\n");
437 /* control callbacks */
438 static int capture_source_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
440 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
441 struct hda_gspec *spec = codec->spec;
442 return snd_hda_input_mux_info(&spec->input_mux, uinfo);
445 static int capture_source_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
447 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
448 struct hda_gspec *spec = codec->spec;
450 ucontrol->value.enumerated.item[0] = spec->cur_cap_src;
454 static int capture_source_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
456 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
457 struct hda_gspec *spec = codec->spec;
458 return snd_hda_input_mux_put(codec, &spec->input_mux, ucontrol,
459 spec->adc_node->nid, &spec->cur_cap_src);
463 * return the string name of the given input PIN widget
465 static const char *get_input_type(struct hda_gnode *node, unsigned int *pinctl)
467 unsigned int location = defcfg_location(node);
468 switch (defcfg_type(node)) {
469 case AC_JACK_LINE_IN:
470 if ((location & 0x0f) == AC_JACK_LOC_FRONT)
476 *pinctl |= AC_PINCTL_VREF_GRD;
480 if ((location & 0x0f) == AC_JACK_LOC_FRONT)
486 (AC_PINCAP_VREF_80 << AC_PINCAP_VREF_SHIFT)))
487 *pinctl |= AC_PINCTL_VREF_80;
488 if ((location & 0x0f) == AC_JACK_LOC_FRONT)
491 case AC_JACK_SPDIF_IN:
493 case AC_JACK_DIG_OTHER_IN:
500 * parse the nodes recursively until reach to the input PIN
502 * returns 0 if not found, 1 if found, or a negative error code.
504 static int parse_adc_sub_nodes(struct hda_codec *codec, struct hda_gspec *spec,
505 struct hda_gnode *node)
516 if (node->type != AC_WID_PIN) {
517 for (i = 0; i < node->nconns; i++) {
518 struct hda_gnode *child;
519 child = hda_get_node(spec, node->conn_list[i]);
522 err = parse_adc_sub_nodes(codec, spec, child);
527 * select the path, unmute both input and output
529 if (node->nconns > 1)
530 select_input_connection(codec, node, i);
531 unmute_input(codec, node, i);
532 unmute_output(codec, node);
540 if (! (node->pin_caps & AC_PINCAP_IN))
543 if (defcfg_port_conn(node) == AC_JACK_PORT_NONE)
544 return 0; /* unconnected */
546 if (node->wid_caps & AC_WCAP_DIGITAL)
547 return 0; /* skip SPDIF */
549 if (spec->input_mux.num_items >= HDA_MAX_NUM_INPUTS) {
550 snd_printk(KERN_ERR "hda_generic: Too many items for capture\n");
554 pinctl = AC_PINCTL_IN_EN;
555 /* create a proper capture source label */
556 type = get_input_type(node, &pinctl);
558 /* input as default? */
559 if (! (node->pin_ctl & AC_PINCTL_IN_EN))
563 label = spec->cap_labels[spec->input_mux.num_items];
565 spec->input_mux.items[spec->input_mux.num_items].label = label;
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 */
576 /* add a capture source element */
577 static void add_cap_src(struct hda_gspec *spec, int idx)
579 struct hda_input_mux_item *csrc;
583 num = spec->input_mux.num_items;
584 csrc = &spec->input_mux.items[num];
585 buf = spec->cap_labels[num];
586 for (ocap = 0; ocap < num; ocap++) {
587 if (! strcmp(buf, spec->cap_labels[ocap])) {
588 /* same label already exists,
589 * put the index number to be unique
591 sprintf(buf, "%s %d", spec->cap_labels[ocap], num);
596 spec->input_mux.num_items++;
602 static int parse_input_path(struct hda_codec *codec, struct hda_gnode *adc_node)
604 struct hda_gspec *spec = codec->spec;
605 struct hda_gnode *node;
608 snd_printdd("AUD_IN = %x\n", adc_node->nid);
609 clear_check_flags(spec);
611 // awk added - fixed no recording due to muted widget
612 unmute_input(codec, adc_node, 0);
615 * check each connection of the ADC
616 * if it reaches to a proper input PIN, add the path as the
619 /* first, check the direct connections to PIN widgets */
620 for (i = 0; i < adc_node->nconns; i++) {
621 node = hda_get_node(spec, adc_node->conn_list[i]);
622 if (node && node->type == AC_WID_PIN) {
623 err = parse_adc_sub_nodes(codec, spec, node);
627 add_cap_src(spec, i);
630 /* ... then check the rests, more complicated connections */
631 for (i = 0; i < adc_node->nconns; i++) {
632 node = hda_get_node(spec, adc_node->conn_list[i]);
633 if (node && node->type != AC_WID_PIN) {
634 err = parse_adc_sub_nodes(codec, spec, node);
638 add_cap_src(spec, i);
642 if (! spec->input_mux.num_items)
643 return 0; /* no input path found... */
645 snd_printdd("[Capture Source] NID=0x%x, #SRC=%d\n", adc_node->nid, spec->input_mux.num_items);
646 for (i = 0; i < spec->input_mux.num_items; i++)
647 snd_printdd(" [%s] IDX=0x%x\n", spec->input_mux.items[i].label,
648 spec->input_mux.items[i].index);
650 spec->adc_node = adc_node;
657 static int parse_input(struct hda_codec *codec)
659 struct hda_gspec *spec = codec->spec;
660 struct hda_gnode *node;
664 * At first we look for an audio input widget.
665 * If it reaches to certain input PINs, we take it as the
668 list_for_each_entry(node, &spec->nid_list, list) {
669 if (node->wid_caps & AC_WCAP_DIGITAL)
670 continue; /* skip SPDIF */
671 if (node->type == AC_WID_AUD_IN) {
672 err = parse_input_path(codec, node);
679 snd_printd("hda_generic: no proper input path found\n");
683 #ifdef CONFIG_SND_HDA_POWER_SAVE
684 static void add_input_loopback(struct hda_codec *codec, hda_nid_t nid,
687 struct hda_gspec *spec = codec->spec;
688 struct hda_amp_list *p;
690 if (spec->num_loopbacks >= MAX_LOOPBACK_AMPS) {
691 snd_printk(KERN_ERR "hda_generic: Too many loopback ctls\n");
694 p = &spec->loopback_list[spec->num_loopbacks++];
698 spec->loopback.amplist = spec->loopback_list;
701 #define add_input_loopback(codec,nid,dir,idx)
705 * create mixer controls if possible
707 static int create_mixer(struct hda_codec *codec, struct hda_gnode *node,
708 unsigned int index, const char *type,
709 const char *dir_sfx, int is_loopback)
714 struct snd_kcontrol_new knew;
717 sprintf(name, "%s %s Switch", type, dir_sfx);
719 sprintf(name, "%s Switch", dir_sfx);
720 if ((node->wid_caps & AC_WCAP_IN_AMP) &&
721 (node->amp_in_caps & AC_AMPCAP_MUTE)) {
722 knew = (struct snd_kcontrol_new)HDA_CODEC_MUTE(name, node->nid, index, HDA_INPUT);
724 add_input_loopback(codec, node->nid, HDA_INPUT, index);
725 snd_printdd("[%s] NID=0x%x, DIR=IN, IDX=0x%x\n", name, node->nid, index);
726 if ((err = snd_ctl_add(codec->bus->card, snd_ctl_new1(&knew, codec))) < 0)
729 } else if ((node->wid_caps & AC_WCAP_OUT_AMP) &&
730 (node->amp_out_caps & AC_AMPCAP_MUTE)) {
731 knew = (struct snd_kcontrol_new)HDA_CODEC_MUTE(name, node->nid, 0, HDA_OUTPUT);
733 add_input_loopback(codec, node->nid, HDA_OUTPUT, 0);
734 snd_printdd("[%s] NID=0x%x, DIR=OUT\n", name, node->nid);
735 if ((err = snd_ctl_add(codec->bus->card, snd_ctl_new1(&knew, codec))) < 0)
741 sprintf(name, "%s %s Volume", type, dir_sfx);
743 sprintf(name, "%s Volume", dir_sfx);
744 if ((node->wid_caps & AC_WCAP_IN_AMP) &&
745 (node->amp_in_caps & AC_AMPCAP_NUM_STEPS)) {
746 knew = (struct snd_kcontrol_new)HDA_CODEC_VOLUME(name, node->nid, index, HDA_INPUT);
747 snd_printdd("[%s] NID=0x%x, DIR=IN, IDX=0x%x\n", name, node->nid, index);
748 if ((err = snd_ctl_add(codec->bus->card, snd_ctl_new1(&knew, codec))) < 0)
751 } else if ((node->wid_caps & AC_WCAP_OUT_AMP) &&
752 (node->amp_out_caps & AC_AMPCAP_NUM_STEPS)) {
753 knew = (struct snd_kcontrol_new)HDA_CODEC_VOLUME(name, node->nid, 0, HDA_OUTPUT);
754 snd_printdd("[%s] NID=0x%x, DIR=OUT\n", name, node->nid);
755 if ((err = snd_ctl_add(codec->bus->card, snd_ctl_new1(&knew, codec))) < 0)
764 * check whether the controls with the given name and direction suffix already exist
766 static int check_existing_control(struct hda_codec *codec, const char *type, const char *dir)
768 struct snd_ctl_elem_id id;
769 memset(&id, 0, sizeof(id));
770 sprintf(id.name, "%s %s Volume", type, dir);
771 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
772 if (snd_ctl_find_id(codec->bus->card, &id))
774 sprintf(id.name, "%s %s Switch", type, dir);
775 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
776 if (snd_ctl_find_id(codec->bus->card, &id))
782 * build output mixer controls
784 static int create_output_mixers(struct hda_codec *codec, const char **names)
786 struct hda_gspec *spec = codec->spec;
789 for (i = 0; i < spec->pcm_vol_nodes; i++) {
790 err = create_mixer(codec, spec->pcm_vol[i].node,
791 spec->pcm_vol[i].index,
792 names[i], "Playback", 0);
799 static int build_output_controls(struct hda_codec *codec)
801 struct hda_gspec *spec = codec->spec;
802 static const char *types_speaker[] = { "Speaker", "Headphone" };
803 static const char *types_line[] = { "Front", "Headphone" };
805 switch (spec->pcm_vol_nodes) {
807 return create_mixer(codec, spec->pcm_vol[0].node,
808 spec->pcm_vol[0].index,
809 "Master", "Playback", 0);
811 if (defcfg_type(spec->out_pin_node[0]) == AC_JACK_SPEAKER)
812 return create_output_mixers(codec, types_speaker);
814 return create_output_mixers(codec, types_line);
819 /* create capture volume/switch */
820 static int build_input_controls(struct hda_codec *codec)
822 struct hda_gspec *spec = codec->spec;
823 struct hda_gnode *adc_node = spec->adc_node;
825 static struct snd_kcontrol_new cap_sel = {
826 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
827 .name = "Capture Source",
828 .info = capture_source_info,
829 .get = capture_source_get,
830 .put = capture_source_put,
833 if (! adc_node || ! spec->input_mux.num_items)
834 return 0; /* not found */
836 spec->cur_cap_src = 0;
837 select_input_connection(codec, adc_node,
838 spec->input_mux.items[0].index);
840 /* create capture volume and switch controls if the ADC has an amp */
841 /* do we have only a single item? */
842 if (spec->input_mux.num_items == 1) {
843 err = create_mixer(codec, adc_node,
844 spec->input_mux.items[0].index,
851 /* create input MUX if multiple sources are available */
852 if ((err = snd_ctl_add(codec->bus->card,
853 snd_ctl_new1(&cap_sel, codec))) < 0)
856 /* no volume control? */
857 if (! (adc_node->wid_caps & AC_WCAP_IN_AMP) ||
858 ! (adc_node->amp_in_caps & AC_AMPCAP_NUM_STEPS))
861 for (i = 0; i < spec->input_mux.num_items; i++) {
862 struct snd_kcontrol_new knew;
864 sprintf(name, "%s Capture Volume",
865 spec->input_mux.items[i].label);
866 knew = (struct snd_kcontrol_new)
867 HDA_CODEC_VOLUME(name, adc_node->nid,
868 spec->input_mux.items[i].index,
870 if ((err = snd_ctl_add(codec->bus->card,
871 snd_ctl_new1(&knew, codec))) < 0)
880 * parse the nodes recursively until reach to the output PIN.
882 * returns 0 - if not found,
883 * 1 - if found, but no mixer is created
884 * 2 - if found and mixer was already created, (just skip)
885 * a negative error code
887 static int parse_loopback_path(struct hda_codec *codec, struct hda_gspec *spec,
888 struct hda_gnode *node, struct hda_gnode *dest_node,
897 if (node == dest_node) {
898 /* loopback connection found */
902 for (i = 0; i < node->nconns; i++) {
903 struct hda_gnode *child = hda_get_node(spec, node->conn_list[i]);
906 err = parse_loopback_path(codec, spec, child, dest_node, type);
911 err = create_mixer(codec, node, i, type,
916 return 2; /* ok, created */
917 /* not created, maybe in the lower path */
920 /* connect and unmute */
921 if (node->nconns > 1)
922 select_input_connection(codec, node, i);
923 unmute_input(codec, node, i);
924 unmute_output(codec, node);
932 * parse the tree and build the loopback controls
934 static int build_loopback_controls(struct hda_codec *codec)
936 struct hda_gspec *spec = codec->spec;
937 struct hda_gnode *node;
941 if (! spec->out_pin_node[0])
944 list_for_each_entry(node, &spec->nid_list, list) {
945 if (node->type != AC_WID_PIN)
948 if (! (node->pin_caps & AC_PINCAP_IN))
950 type = get_input_type(node, NULL);
952 if (check_existing_control(codec, type, "Playback"))
954 clear_check_flags(spec);
955 err = parse_loopback_path(codec, spec,
956 spec->out_pin_node[0],
968 * build mixer controls
970 static int build_generic_controls(struct hda_codec *codec)
974 if ((err = build_input_controls(codec)) < 0 ||
975 (err = build_output_controls(codec)) < 0 ||
976 (err = build_loopback_controls(codec)) < 0)
985 static struct hda_pcm_stream generic_pcm_playback = {
991 static int generic_pcm2_prepare(struct hda_pcm_stream *hinfo,
992 struct hda_codec *codec,
993 unsigned int stream_tag,
995 struct snd_pcm_substream *substream)
997 struct hda_gspec *spec = codec->spec;
999 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
1000 snd_hda_codec_setup_stream(codec, spec->dac_node[1]->nid,
1001 stream_tag, 0, format);
1005 static int generic_pcm2_cleanup(struct hda_pcm_stream *hinfo,
1006 struct hda_codec *codec,
1007 struct snd_pcm_substream *substream)
1009 struct hda_gspec *spec = codec->spec;
1011 snd_hda_codec_setup_stream(codec, hinfo->nid, 0, 0, 0);
1012 snd_hda_codec_setup_stream(codec, spec->dac_node[1]->nid, 0, 0, 0);
1016 static int build_generic_pcms(struct hda_codec *codec)
1018 struct hda_gspec *spec = codec->spec;
1019 struct hda_pcm *info = &spec->pcm_rec;
1021 if (! spec->dac_node[0] && ! spec->adc_node) {
1022 snd_printd("hda_generic: no PCM found\n");
1026 codec->num_pcms = 1;
1027 codec->pcm_info = info;
1029 info->name = "HDA Generic";
1030 if (spec->dac_node[0]) {
1031 info->stream[0] = generic_pcm_playback;
1032 info->stream[0].nid = spec->dac_node[0]->nid;
1033 if (spec->dac_node[1]) {
1034 info->stream[0].ops.prepare = generic_pcm2_prepare;
1035 info->stream[0].ops.cleanup = generic_pcm2_cleanup;
1038 if (spec->adc_node) {
1039 info->stream[1] = generic_pcm_playback;
1040 info->stream[1].nid = spec->adc_node->nid;
1046 #ifdef CONFIG_SND_HDA_POWER_SAVE
1047 static int generic_check_power_status(struct hda_codec *codec, hda_nid_t nid)
1049 struct hda_gspec *spec = codec->spec;
1050 return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
1057 static struct hda_codec_ops generic_patch_ops = {
1058 .build_controls = build_generic_controls,
1059 .build_pcms = build_generic_pcms,
1060 .free = snd_hda_generic_free,
1061 #ifdef CONFIG_SND_HDA_POWER_SAVE
1062 .check_power_status = generic_check_power_status,
1067 * the generic parser
1069 int snd_hda_parse_generic_codec(struct hda_codec *codec)
1071 struct hda_gspec *spec;
1077 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
1079 printk(KERN_ERR "hda_generic: can't allocate spec\n");
1083 INIT_LIST_HEAD(&spec->nid_list);
1085 if ((err = build_afg_tree(codec)) < 0)
1088 if ((err = parse_input(codec)) < 0 ||
1089 (err = parse_output(codec)) < 0)
1092 codec->patch_ops = generic_patch_ops;
1097 snd_hda_generic_free(codec);