blob: 1589d2f2917f23aa3cc31652501e7281a15d5dde [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Universal Interface for Intel High Definition Audio Codec
3 *
4 * Generic widget tree parser
5 *
6 * Copyright (c) 2004 Takashi Iwai <tiwai@suse.de>
7 *
8 * This driver is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This driver is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */
22
23#include <sound/driver.h>
24#include <linux/init.h>
25#include <linux/slab.h>
26#include <linux/pci.h>
27#include <sound/core.h>
28#include "hda_codec.h"
29#include "hda_local.h"
30
31/* widget node for parsing */
32struct hda_gnode {
33 hda_nid_t nid; /* NID of this widget */
34 unsigned short nconns; /* number of input connections */
Takashi Iwaid2569502005-11-21 16:33:51 +010035 hda_nid_t *conn_list;
36 hda_nid_t slist[2]; /* temporay list */
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 unsigned int wid_caps; /* widget capabilities */
38 unsigned char type; /* widget type */
39 unsigned char pin_ctl; /* pin controls */
40 unsigned char checked; /* the flag indicates that the node is already parsed */
41 unsigned int pin_caps; /* pin widget capabilities */
42 unsigned int def_cfg; /* default configuration */
43 unsigned int amp_out_caps; /* AMP out capabilities */
44 unsigned int amp_in_caps; /* AMP in capabilities */
45 struct list_head list;
46};
47
Takashi Iwaic3132922005-04-20 13:43:00 +020048/* patch-specific record */
Takashi Iwaia7da6ce2006-09-06 14:03:14 +020049
50#define MAX_PCM_VOLS 2
51struct pcm_vol {
52 struct hda_gnode *node; /* Node for PCM volume */
53 unsigned int index; /* connection of PCM volume */
54};
55
Linus Torvalds1da177e2005-04-16 15:20:36 -070056struct hda_gspec {
Takashi Iwai97ec5582006-03-21 11:29:07 +010057 struct hda_gnode *dac_node[2]; /* DAC node */
58 struct hda_gnode *out_pin_node[2]; /* Output pin (Line-Out) node */
Takashi Iwaia7da6ce2006-09-06 14:03:14 +020059 struct pcm_vol pcm_vol[MAX_PCM_VOLS]; /* PCM volumes */
60 unsigned int pcm_vol_nodes; /* number of PCM volumes */
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
62 struct hda_gnode *adc_node; /* ADC node */
63 struct hda_gnode *cap_vol_node; /* Node for capture volume */
64 unsigned int cur_cap_src; /* current capture source */
65 struct hda_input_mux input_mux;
66 char cap_labels[HDA_MAX_NUM_INPUTS][16];
67
68 unsigned int def_amp_in_caps;
69 unsigned int def_amp_out_caps;
70
71 struct hda_pcm pcm_rec; /* PCM information */
72
73 struct list_head nid_list; /* list of widgets */
74};
75
76/*
77 * retrieve the default device type from the default config value
78 */
Takashi Iwai97ec5582006-03-21 11:29:07 +010079#define defcfg_type(node) (((node)->def_cfg & AC_DEFCFG_DEVICE) >> \
80 AC_DEFCFG_DEVICE_SHIFT)
81#define defcfg_location(node) (((node)->def_cfg & AC_DEFCFG_LOCATION) >> \
82 AC_DEFCFG_LOCATION_SHIFT)
83#define defcfg_port_conn(node) (((node)->def_cfg & AC_DEFCFG_PORT_CONN) >> \
84 AC_DEFCFG_PORT_CONN_SHIFT)
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
86/*
87 * destructor
88 */
89static void snd_hda_generic_free(struct hda_codec *codec)
90{
91 struct hda_gspec *spec = codec->spec;
92 struct list_head *p, *n;
93
94 if (! spec)
95 return;
96 /* free all widgets */
97 list_for_each_safe(p, n, &spec->nid_list) {
98 struct hda_gnode *node = list_entry(p, struct hda_gnode, list);
Takashi Iwaid2569502005-11-21 16:33:51 +010099 if (node->conn_list != node->slist)
100 kfree(node->conn_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 kfree(node);
102 }
103 kfree(spec);
104}
105
106
107/*
108 * add a new widget node and read its attributes
109 */
110static int add_new_node(struct hda_codec *codec, struct hda_gspec *spec, hda_nid_t nid)
111{
112 struct hda_gnode *node;
113 int nconns;
Takashi Iwaid2569502005-11-21 16:33:51 +0100114 hda_nid_t conn_list[HDA_MAX_CONNECTIONS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200116 node = kzalloc(sizeof(*node), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 if (node == NULL)
118 return -ENOMEM;
119 node->nid = nid;
Takashi Iwaid2569502005-11-21 16:33:51 +0100120 nconns = snd_hda_get_connections(codec, nid, conn_list,
121 HDA_MAX_CONNECTIONS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 if (nconns < 0) {
123 kfree(node);
124 return nconns;
125 }
Takashi Iwaid2569502005-11-21 16:33:51 +0100126 if (nconns <= ARRAY_SIZE(node->slist))
127 node->conn_list = node->slist;
128 else {
129 node->conn_list = kmalloc(sizeof(hda_nid_t) * nconns,
130 GFP_KERNEL);
131 if (! node->conn_list) {
132 snd_printk(KERN_ERR "hda-generic: cannot malloc\n");
133 kfree(node);
134 return -ENOMEM;
135 }
136 }
137 memcpy(node->conn_list, conn_list, nconns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 node->nconns = nconns;
Takashi Iwaid2569502005-11-21 16:33:51 +0100139 node->wid_caps = get_wcaps(codec, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 node->type = (node->wid_caps & AC_WCAP_TYPE) >> AC_WCAP_TYPE_SHIFT;
141
142 if (node->type == AC_WID_PIN) {
143 node->pin_caps = snd_hda_param_read(codec, node->nid, AC_PAR_PIN_CAP);
144 node->pin_ctl = snd_hda_codec_read(codec, node->nid, 0, AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
145 node->def_cfg = snd_hda_codec_read(codec, node->nid, 0, AC_VERB_GET_CONFIG_DEFAULT, 0);
146 }
147
148 if (node->wid_caps & AC_WCAP_OUT_AMP) {
149 if (node->wid_caps & AC_WCAP_AMP_OVRD)
150 node->amp_out_caps = snd_hda_param_read(codec, node->nid, AC_PAR_AMP_OUT_CAP);
151 if (! node->amp_out_caps)
152 node->amp_out_caps = spec->def_amp_out_caps;
153 }
154 if (node->wid_caps & AC_WCAP_IN_AMP) {
155 if (node->wid_caps & AC_WCAP_AMP_OVRD)
156 node->amp_in_caps = snd_hda_param_read(codec, node->nid, AC_PAR_AMP_IN_CAP);
157 if (! node->amp_in_caps)
158 node->amp_in_caps = spec->def_amp_in_caps;
159 }
160 list_add_tail(&node->list, &spec->nid_list);
161 return 0;
162}
163
164/*
165 * build the AFG subtree
166 */
167static int build_afg_tree(struct hda_codec *codec)
168{
169 struct hda_gspec *spec = codec->spec;
170 int i, nodes, err;
171 hda_nid_t nid;
172
173 snd_assert(spec, return -EINVAL);
174
175 spec->def_amp_out_caps = snd_hda_param_read(codec, codec->afg, AC_PAR_AMP_OUT_CAP);
176 spec->def_amp_in_caps = snd_hda_param_read(codec, codec->afg, AC_PAR_AMP_IN_CAP);
177
178 nodes = snd_hda_get_sub_nodes(codec, codec->afg, &nid);
179 if (! nid || nodes < 0) {
180 printk(KERN_ERR "Invalid AFG subtree\n");
181 return -EINVAL;
182 }
183
184 /* parse all nodes belonging to the AFG */
185 for (i = 0; i < nodes; i++, nid++) {
186 if ((err = add_new_node(codec, spec, nid)) < 0)
187 return err;
188 }
189
190 return 0;
191}
192
193
194/*
195 * look for the node record for the given NID
196 */
197/* FIXME: should avoid the braindead linear search */
198static struct hda_gnode *hda_get_node(struct hda_gspec *spec, hda_nid_t nid)
199{
200 struct list_head *p;
201 struct hda_gnode *node;
202
203 list_for_each(p, &spec->nid_list) {
204 node = list_entry(p, struct hda_gnode, list);
205 if (node->nid == nid)
206 return node;
207 }
208 return NULL;
209}
210
211/*
212 * unmute (and set max vol) the output amplifier
213 */
214static int unmute_output(struct hda_codec *codec, struct hda_gnode *node)
215{
216 unsigned int val, ofs;
217 snd_printdd("UNMUTE OUT: NID=0x%x\n", node->nid);
218 val = (node->amp_out_caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
219 ofs = (node->amp_out_caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT;
220 if (val >= ofs)
221 val -= ofs;
222 val |= AC_AMP_SET_LEFT | AC_AMP_SET_RIGHT;
223 val |= AC_AMP_SET_OUTPUT;
224 return snd_hda_codec_write(codec, node->nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, val);
225}
226
227/*
228 * unmute (and set max vol) the input amplifier
229 */
230static int unmute_input(struct hda_codec *codec, struct hda_gnode *node, unsigned int index)
231{
232 unsigned int val, ofs;
233 snd_printdd("UNMUTE IN: NID=0x%x IDX=0x%x\n", node->nid, index);
234 val = (node->amp_in_caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
235 ofs = (node->amp_in_caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT;
236 if (val >= ofs)
237 val -= ofs;
238 val |= AC_AMP_SET_LEFT | AC_AMP_SET_RIGHT;
239 val |= AC_AMP_SET_INPUT;
240 // awk added - fixed to allow unmuting of indexed amps
241 val |= index << AC_AMP_SET_INDEX_SHIFT;
242 return snd_hda_codec_write(codec, node->nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, val);
243}
244
245/*
246 * select the input connection of the given node.
247 */
248static int select_input_connection(struct hda_codec *codec, struct hda_gnode *node,
249 unsigned int index)
250{
251 snd_printdd("CONNECT: NID=0x%x IDX=0x%x\n", node->nid, index);
252 return snd_hda_codec_write(codec, node->nid, 0, AC_VERB_SET_CONNECT_SEL, index);
253}
254
255/*
256 * clear checked flag of each node in the node list
257 */
258static void clear_check_flags(struct hda_gspec *spec)
259{
260 struct list_head *p;
261 struct hda_gnode *node;
262
263 list_for_each(p, &spec->nid_list) {
264 node = list_entry(p, struct hda_gnode, list);
265 node->checked = 0;
266 }
267}
268
269/*
270 * parse the output path recursively until reach to an audio output widget
271 *
272 * returns 0 if not found, 1 if found, or a negative error code.
273 */
274static int parse_output_path(struct hda_codec *codec, struct hda_gspec *spec,
Takashi Iwai97ec5582006-03-21 11:29:07 +0100275 struct hda_gnode *node, int dac_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276{
277 int i, err;
278 struct hda_gnode *child;
279
280 if (node->checked)
281 return 0;
282
283 node->checked = 1;
284 if (node->type == AC_WID_AUD_OUT) {
285 if (node->wid_caps & AC_WCAP_DIGITAL) {
286 snd_printdd("Skip Digital OUT node %x\n", node->nid);
287 return 0;
288 }
289 snd_printdd("AUD_OUT found %x\n", node->nid);
Takashi Iwai97ec5582006-03-21 11:29:07 +0100290 if (spec->dac_node[dac_idx]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 /* already DAC node is assigned, just unmute & connect */
Takashi Iwai97ec5582006-03-21 11:29:07 +0100292 return node == spec->dac_node[dac_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 }
Takashi Iwai97ec5582006-03-21 11:29:07 +0100294 spec->dac_node[dac_idx] = node;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +0200295 if ((node->wid_caps & AC_WCAP_OUT_AMP) &&
296 spec->pcm_vol_nodes < MAX_PCM_VOLS) {
297 spec->pcm_vol[spec->pcm_vol_nodes].node = node;
298 spec->pcm_vol[spec->pcm_vol_nodes].index = 0;
299 spec->pcm_vol_nodes++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 }
301 return 1; /* found */
302 }
303
304 for (i = 0; i < node->nconns; i++) {
305 child = hda_get_node(spec, node->conn_list[i]);
306 if (! child)
307 continue;
Takashi Iwai97ec5582006-03-21 11:29:07 +0100308 err = parse_output_path(codec, spec, child, dac_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 if (err < 0)
310 return err;
311 else if (err > 0) {
312 /* found one,
313 * select the path, unmute both input and output
314 */
315 if (node->nconns > 1)
316 select_input_connection(codec, node, i);
317 unmute_input(codec, node, i);
318 unmute_output(codec, node);
Takashi Iwaia7da6ce2006-09-06 14:03:14 +0200319 if (spec->dac_node[dac_idx] &&
320 spec->pcm_vol_nodes < MAX_PCM_VOLS &&
321 !(spec->dac_node[dac_idx]->wid_caps &
322 AC_WCAP_OUT_AMP)) {
323 if ((node->wid_caps & AC_WCAP_IN_AMP) ||
324 (node->wid_caps & AC_WCAP_OUT_AMP)) {
325 int n = spec->pcm_vol_nodes;
326 spec->pcm_vol[n].node = node;
327 spec->pcm_vol[n].index = i;
328 spec->pcm_vol_nodes++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 }
330 }
331 return 1;
332 }
333 }
334 return 0;
335}
336
337/*
338 * Look for the output PIN widget with the given jack type
339 * and parse the output path to that PIN.
340 *
341 * Returns the PIN node when the path to DAC is established.
342 */
343static struct hda_gnode *parse_output_jack(struct hda_codec *codec,
344 struct hda_gspec *spec,
345 int jack_type)
346{
347 struct list_head *p;
348 struct hda_gnode *node;
349 int err;
350
351 list_for_each(p, &spec->nid_list) {
352 node = list_entry(p, struct hda_gnode, list);
353 if (node->type != AC_WID_PIN)
354 continue;
355 /* output capable? */
356 if (! (node->pin_caps & AC_PINCAP_OUT))
357 continue;
Takashi Iwai97ec5582006-03-21 11:29:07 +0100358 if (defcfg_port_conn(node) == AC_JACK_PORT_NONE)
359 continue; /* unconnected */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 if (jack_type >= 0) {
Takashi Iwaie9edcee2005-06-13 14:16:38 +0200361 if (jack_type != defcfg_type(node))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 continue;
363 if (node->wid_caps & AC_WCAP_DIGITAL)
364 continue; /* skip SPDIF */
365 } else {
366 /* output as default? */
367 if (! (node->pin_ctl & AC_PINCTL_OUT_EN))
368 continue;
369 }
370 clear_check_flags(spec);
Takashi Iwai97ec5582006-03-21 11:29:07 +0100371 err = parse_output_path(codec, spec, node, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 if (err < 0)
373 return NULL;
Takashi Iwai97ec5582006-03-21 11:29:07 +0100374 if (! err && spec->out_pin_node[0]) {
375 err = parse_output_path(codec, spec, node, 1);
376 if (err < 0)
377 return NULL;
378 }
379 if (err > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 /* unmute the PIN output */
381 unmute_output(codec, node);
382 /* set PIN-Out enable */
383 snd_hda_codec_write(codec, node->nid, 0,
384 AC_VERB_SET_PIN_WIDGET_CONTROL,
Takashi Iwaia7da6ce2006-09-06 14:03:14 +0200385 AC_PINCTL_OUT_EN |
386 ((node->pin_caps & AC_PINCAP_HP_DRV) ?
387 AC_PINCTL_HP_EN : 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 return node;
389 }
390 }
391 return NULL;
392}
393
394
395/*
396 * parse outputs
397 */
398static int parse_output(struct hda_codec *codec)
399{
400 struct hda_gspec *spec = codec->spec;
401 struct hda_gnode *node;
402
403 /*
404 * Look for the output PIN widget
405 */
406 /* first, look for the line-out pin */
407 node = parse_output_jack(codec, spec, AC_JACK_LINE_OUT);
408 if (node) /* found, remember the PIN node */
Takashi Iwai97ec5582006-03-21 11:29:07 +0100409 spec->out_pin_node[0] = node;
410 else {
411 /* if no line-out is found, try speaker out */
412 node = parse_output_jack(codec, spec, AC_JACK_SPEAKER);
413 if (node)
414 spec->out_pin_node[0] = node;
415 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 /* look for the HP-out pin */
417 node = parse_output_jack(codec, spec, AC_JACK_HP_OUT);
418 if (node) {
Takashi Iwai97ec5582006-03-21 11:29:07 +0100419 if (! spec->out_pin_node[0])
420 spec->out_pin_node[0] = node;
421 else
422 spec->out_pin_node[1] = node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 }
424
Takashi Iwai97ec5582006-03-21 11:29:07 +0100425 if (! spec->out_pin_node[0]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 /* no line-out or HP pins found,
427 * then choose for the first output pin
428 */
Takashi Iwai97ec5582006-03-21 11:29:07 +0100429 spec->out_pin_node[0] = parse_output_jack(codec, spec, -1);
430 if (! spec->out_pin_node[0])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 snd_printd("hda_generic: no proper output path found\n");
432 }
433
434 return 0;
435}
436
437/*
438 * input MUX
439 */
440
441/* control callbacks */
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +0100442static int capture_source_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443{
444 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
445 struct hda_gspec *spec = codec->spec;
446 return snd_hda_input_mux_info(&spec->input_mux, uinfo);
447}
448
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +0100449static int capture_source_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450{
451 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
452 struct hda_gspec *spec = codec->spec;
453
454 ucontrol->value.enumerated.item[0] = spec->cur_cap_src;
455 return 0;
456}
457
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +0100458static int capture_source_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459{
460 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
461 struct hda_gspec *spec = codec->spec;
462 return snd_hda_input_mux_put(codec, &spec->input_mux, ucontrol,
463 spec->adc_node->nid, &spec->cur_cap_src);
464}
465
466/*
467 * return the string name of the given input PIN widget
468 */
469static const char *get_input_type(struct hda_gnode *node, unsigned int *pinctl)
470{
Takashi Iwaie9edcee2005-06-13 14:16:38 +0200471 unsigned int location = defcfg_location(node);
472 switch (defcfg_type(node)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 case AC_JACK_LINE_IN:
474 if ((location & 0x0f) == AC_JACK_LOC_FRONT)
475 return "Front Line";
476 return "Line";
477 case AC_JACK_CD:
Takashi Iwai071c73a2006-08-23 18:34:06 +0200478#if 0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 if (pinctl)
Matt1a12de1e2005-04-13 14:37:50 +0200480 *pinctl |= AC_PINCTL_VREF_GRD;
Takashi Iwai071c73a2006-08-23 18:34:06 +0200481#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 return "CD";
483 case AC_JACK_AUX:
484 if ((location & 0x0f) == AC_JACK_LOC_FRONT)
485 return "Front Aux";
486 return "Aux";
487 case AC_JACK_MIC_IN:
Takashi Iwai6afeb112006-12-18 16:16:04 +0100488 if (pinctl &&
489 (node->pin_caps &
490 (AC_PINCAP_VREF_80 << AC_PINCAP_VREF_SHIFT)))
Takashi Iwai071c73a2006-08-23 18:34:06 +0200491 *pinctl |= AC_PINCTL_VREF_80;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 if ((location & 0x0f) == AC_JACK_LOC_FRONT)
493 return "Front Mic";
494 return "Mic";
495 case AC_JACK_SPDIF_IN:
496 return "SPDIF";
497 case AC_JACK_DIG_OTHER_IN:
498 return "Digital";
499 }
500 return NULL;
501}
502
503/*
504 * parse the nodes recursively until reach to the input PIN
505 *
506 * returns 0 if not found, 1 if found, or a negative error code.
507 */
508static int parse_adc_sub_nodes(struct hda_codec *codec, struct hda_gspec *spec,
509 struct hda_gnode *node)
510{
511 int i, err;
512 unsigned int pinctl;
513 char *label;
514 const char *type;
515
516 if (node->checked)
517 return 0;
518
519 node->checked = 1;
520 if (node->type != AC_WID_PIN) {
521 for (i = 0; i < node->nconns; i++) {
522 struct hda_gnode *child;
523 child = hda_get_node(spec, node->conn_list[i]);
524 if (! child)
525 continue;
526 err = parse_adc_sub_nodes(codec, spec, child);
527 if (err < 0)
528 return err;
529 if (err > 0) {
530 /* found one,
531 * select the path, unmute both input and output
532 */
533 if (node->nconns > 1)
534 select_input_connection(codec, node, i);
535 unmute_input(codec, node, i);
536 unmute_output(codec, node);
537 return err;
538 }
539 }
540 return 0;
541 }
542
543 /* input capable? */
544 if (! (node->pin_caps & AC_PINCAP_IN))
545 return 0;
546
Takashi Iwai97ec5582006-03-21 11:29:07 +0100547 if (defcfg_port_conn(node) == AC_JACK_PORT_NONE)
548 return 0; /* unconnected */
549
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 if (node->wid_caps & AC_WCAP_DIGITAL)
551 return 0; /* skip SPDIF */
552
553 if (spec->input_mux.num_items >= HDA_MAX_NUM_INPUTS) {
554 snd_printk(KERN_ERR "hda_generic: Too many items for capture\n");
555 return -EINVAL;
556 }
557
558 pinctl = AC_PINCTL_IN_EN;
559 /* create a proper capture source label */
560 type = get_input_type(node, &pinctl);
561 if (! type) {
562 /* input as default? */
563 if (! (node->pin_ctl & AC_PINCTL_IN_EN))
564 return 0;
565 type = "Input";
566 }
567 label = spec->cap_labels[spec->input_mux.num_items];
568 strcpy(label, type);
569 spec->input_mux.items[spec->input_mux.num_items].label = label;
570
571 /* unmute the PIN external input */
572 unmute_input(codec, node, 0); /* index = 0? */
573 /* set PIN-In enable */
574 snd_hda_codec_write(codec, node->nid, 0, AC_VERB_SET_PIN_WIDGET_CONTROL, pinctl);
575
576 return 1; /* found */
577}
578
Takashi Iwai071c73a2006-08-23 18:34:06 +0200579/* add a capture source element */
580static void add_cap_src(struct hda_gspec *spec, int idx)
581{
582 struct hda_input_mux_item *csrc;
583 char *buf;
584 int num, ocap;
585
586 num = spec->input_mux.num_items;
587 csrc = &spec->input_mux.items[num];
588 buf = spec->cap_labels[num];
589 for (ocap = 0; ocap < num; ocap++) {
590 if (! strcmp(buf, spec->cap_labels[ocap])) {
591 /* same label already exists,
592 * put the index number to be unique
593 */
594 sprintf(buf, "%s %d", spec->cap_labels[ocap], num);
595 break;
596 }
597 }
598 csrc->index = idx;
599 spec->input_mux.num_items++;
600}
601
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602/*
603 * parse input
604 */
605static int parse_input_path(struct hda_codec *codec, struct hda_gnode *adc_node)
606{
607 struct hda_gspec *spec = codec->spec;
608 struct hda_gnode *node;
609 int i, err;
610
611 snd_printdd("AUD_IN = %x\n", adc_node->nid);
612 clear_check_flags(spec);
613
614 // awk added - fixed no recording due to muted widget
615 unmute_input(codec, adc_node, 0);
616
617 /*
618 * check each connection of the ADC
619 * if it reaches to a proper input PIN, add the path as the
620 * input path.
621 */
Takashi Iwai071c73a2006-08-23 18:34:06 +0200622 /* first, check the direct connections to PIN widgets */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 for (i = 0; i < adc_node->nconns; i++) {
624 node = hda_get_node(spec, adc_node->conn_list[i]);
Takashi Iwai071c73a2006-08-23 18:34:06 +0200625 if (node && node->type == AC_WID_PIN) {
626 err = parse_adc_sub_nodes(codec, spec, node);
627 if (err < 0)
628 return err;
629 else if (err > 0)
630 add_cap_src(spec, i);
631 }
632 }
633 /* ... then check the rests, more complicated connections */
634 for (i = 0; i < adc_node->nconns; i++) {
635 node = hda_get_node(spec, adc_node->conn_list[i]);
636 if (node && node->type != AC_WID_PIN) {
637 err = parse_adc_sub_nodes(codec, spec, node);
638 if (err < 0)
639 return err;
640 else if (err > 0)
641 add_cap_src(spec, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 }
643 }
644
645 if (! spec->input_mux.num_items)
646 return 0; /* no input path found... */
647
648 snd_printdd("[Capture Source] NID=0x%x, #SRC=%d\n", adc_node->nid, spec->input_mux.num_items);
649 for (i = 0; i < spec->input_mux.num_items; i++)
650 snd_printdd(" [%s] IDX=0x%x\n", spec->input_mux.items[i].label,
651 spec->input_mux.items[i].index);
652
653 spec->adc_node = adc_node;
654 return 1;
655}
656
657/*
658 * parse input
659 */
660static int parse_input(struct hda_codec *codec)
661{
662 struct hda_gspec *spec = codec->spec;
663 struct list_head *p;
664 struct hda_gnode *node;
665 int err;
666
667 /*
668 * At first we look for an audio input widget.
669 * If it reaches to certain input PINs, we take it as the
670 * input path.
671 */
672 list_for_each(p, &spec->nid_list) {
673 node = list_entry(p, struct hda_gnode, list);
674 if (node->wid_caps & AC_WCAP_DIGITAL)
675 continue; /* skip SPDIF */
676 if (node->type == AC_WID_AUD_IN) {
677 err = parse_input_path(codec, node);
678 if (err < 0)
679 return err;
680 else if (err > 0)
681 return 0;
682 }
683 }
684 snd_printd("hda_generic: no proper input path found\n");
685 return 0;
686}
687
688/*
689 * create mixer controls if possible
690 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691static int create_mixer(struct hda_codec *codec, struct hda_gnode *node,
692 unsigned int index, const char *type, const char *dir_sfx)
693{
694 char name[32];
695 int err;
696 int created = 0;
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +0100697 struct snd_kcontrol_new knew;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
699 if (type)
700 sprintf(name, "%s %s Switch", type, dir_sfx);
701 else
702 sprintf(name, "%s Switch", dir_sfx);
703 if ((node->wid_caps & AC_WCAP_IN_AMP) &&
704 (node->amp_in_caps & AC_AMPCAP_MUTE)) {
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +0100705 knew = (struct snd_kcontrol_new)HDA_CODEC_MUTE(name, node->nid, index, HDA_INPUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 snd_printdd("[%s] NID=0x%x, DIR=IN, IDX=0x%x\n", name, node->nid, index);
707 if ((err = snd_ctl_add(codec->bus->card, snd_ctl_new1(&knew, codec))) < 0)
708 return err;
709 created = 1;
710 } else if ((node->wid_caps & AC_WCAP_OUT_AMP) &&
711 (node->amp_out_caps & AC_AMPCAP_MUTE)) {
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +0100712 knew = (struct snd_kcontrol_new)HDA_CODEC_MUTE(name, node->nid, 0, HDA_OUTPUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 snd_printdd("[%s] NID=0x%x, DIR=OUT\n", name, node->nid);
714 if ((err = snd_ctl_add(codec->bus->card, snd_ctl_new1(&knew, codec))) < 0)
715 return err;
716 created = 1;
717 }
718
719 if (type)
720 sprintf(name, "%s %s Volume", type, dir_sfx);
721 else
722 sprintf(name, "%s Volume", dir_sfx);
723 if ((node->wid_caps & AC_WCAP_IN_AMP) &&
724 (node->amp_in_caps & AC_AMPCAP_NUM_STEPS)) {
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +0100725 knew = (struct snd_kcontrol_new)HDA_CODEC_VOLUME(name, node->nid, index, HDA_INPUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 snd_printdd("[%s] NID=0x%x, DIR=IN, IDX=0x%x\n", name, node->nid, index);
727 if ((err = snd_ctl_add(codec->bus->card, snd_ctl_new1(&knew, codec))) < 0)
728 return err;
729 created = 1;
730 } else if ((node->wid_caps & AC_WCAP_OUT_AMP) &&
731 (node->amp_out_caps & AC_AMPCAP_NUM_STEPS)) {
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +0100732 knew = (struct snd_kcontrol_new)HDA_CODEC_VOLUME(name, node->nid, 0, HDA_OUTPUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 snd_printdd("[%s] NID=0x%x, DIR=OUT\n", name, node->nid);
734 if ((err = snd_ctl_add(codec->bus->card, snd_ctl_new1(&knew, codec))) < 0)
735 return err;
736 created = 1;
737 }
738
739 return created;
740}
741
742/*
743 * check whether the controls with the given name and direction suffix already exist
744 */
745static int check_existing_control(struct hda_codec *codec, const char *type, const char *dir)
746{
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +0100747 struct snd_ctl_elem_id id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 memset(&id, 0, sizeof(id));
749 sprintf(id.name, "%s %s Volume", type, dir);
750 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
751 if (snd_ctl_find_id(codec->bus->card, &id))
752 return 1;
753 sprintf(id.name, "%s %s Switch", type, dir);
754 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
755 if (snd_ctl_find_id(codec->bus->card, &id))
756 return 1;
757 return 0;
758}
759
760/*
761 * build output mixer controls
762 */
Takashi Iwaia7da6ce2006-09-06 14:03:14 +0200763static int create_output_mixers(struct hda_codec *codec, const char **names)
764{
765 struct hda_gspec *spec = codec->spec;
766 int i, err;
767
768 for (i = 0; i < spec->pcm_vol_nodes; i++) {
769 err = create_mixer(codec, spec->pcm_vol[i].node,
770 spec->pcm_vol[i].index,
771 names[i], "Playback");
772 if (err < 0)
773 return err;
774 }
775 return 0;
776}
777
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778static int build_output_controls(struct hda_codec *codec)
779{
780 struct hda_gspec *spec = codec->spec;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +0200781 static const char *types_speaker[] = { "Speaker", "Headphone" };
782 static const char *types_line[] = { "Front", "Headphone" };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783
Takashi Iwaia7da6ce2006-09-06 14:03:14 +0200784 switch (spec->pcm_vol_nodes) {
785 case 1:
786 return create_mixer(codec, spec->pcm_vol[0].node,
787 spec->pcm_vol[0].index,
788 "Master", "Playback");
789 case 2:
790 if (defcfg_type(spec->out_pin_node[0]) == AC_JACK_SPEAKER)
791 return create_output_mixers(codec, types_speaker);
792 else
793 return create_output_mixers(codec, types_line);
Takashi Iwai97ec5582006-03-21 11:29:07 +0100794 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 return 0;
796}
797
798/* create capture volume/switch */
799static int build_input_controls(struct hda_codec *codec)
800{
801 struct hda_gspec *spec = codec->spec;
802 struct hda_gnode *adc_node = spec->adc_node;
Takashi Iwai071c73a2006-08-23 18:34:06 +0200803 int i, err;
804 static struct snd_kcontrol_new cap_sel = {
805 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
806 .name = "Capture Source",
807 .info = capture_source_info,
808 .get = capture_source_get,
809 .put = capture_source_put,
810 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811
Takashi Iwai071c73a2006-08-23 18:34:06 +0200812 if (! adc_node || ! spec->input_mux.num_items)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 return 0; /* not found */
814
Takashi Iwai071c73a2006-08-23 18:34:06 +0200815 spec->cur_cap_src = 0;
816 select_input_connection(codec, adc_node,
817 spec->input_mux.items[0].index);
818
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 /* create capture volume and switch controls if the ADC has an amp */
Takashi Iwai071c73a2006-08-23 18:34:06 +0200820 /* do we have only a single item? */
821 if (spec->input_mux.num_items == 1) {
822 err = create_mixer(codec, adc_node,
823 spec->input_mux.items[0].index,
824 NULL, "Capture");
825 if (err < 0)
826 return err;
827 return 0;
828 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829
830 /* create input MUX if multiple sources are available */
Takashi Iwai071c73a2006-08-23 18:34:06 +0200831 if ((err = snd_ctl_add(codec->bus->card,
832 snd_ctl_new1(&cap_sel, codec))) < 0)
833 return err;
834
835 /* no volume control? */
836 if (! (adc_node->wid_caps & AC_WCAP_IN_AMP) ||
837 ! (adc_node->amp_in_caps & AC_AMPCAP_NUM_STEPS))
838 return 0;
839
840 for (i = 0; i < spec->input_mux.num_items; i++) {
841 struct snd_kcontrol_new knew;
842 char name[32];
843 sprintf(name, "%s Capture Volume",
844 spec->input_mux.items[i].label);
845 knew = (struct snd_kcontrol_new)
846 HDA_CODEC_VOLUME(name, adc_node->nid,
847 spec->input_mux.items[i].index,
848 HDA_INPUT);
849 if ((err = snd_ctl_add(codec->bus->card,
850 snd_ctl_new1(&knew, codec))) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 }
Takashi Iwai071c73a2006-08-23 18:34:06 +0200853
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 return 0;
855}
856
857
858/*
859 * parse the nodes recursively until reach to the output PIN.
860 *
861 * returns 0 - if not found,
862 * 1 - if found, but no mixer is created
863 * 2 - if found and mixer was already created, (just skip)
864 * a negative error code
865 */
866static int parse_loopback_path(struct hda_codec *codec, struct hda_gspec *spec,
867 struct hda_gnode *node, struct hda_gnode *dest_node,
868 const char *type)
869{
870 int i, err;
871
872 if (node->checked)
873 return 0;
874
875 node->checked = 1;
876 if (node == dest_node) {
877 /* loopback connection found */
878 return 1;
879 }
880
881 for (i = 0; i < node->nconns; i++) {
882 struct hda_gnode *child = hda_get_node(spec, node->conn_list[i]);
883 if (! child)
884 continue;
885 err = parse_loopback_path(codec, spec, child, dest_node, type);
886 if (err < 0)
887 return err;
888 else if (err >= 1) {
889 if (err == 1) {
890 err = create_mixer(codec, node, i, type, "Playback");
891 if (err < 0)
892 return err;
893 if (err > 0)
894 return 2; /* ok, created */
895 /* not created, maybe in the lower path */
896 err = 1;
897 }
898 /* connect and unmute */
899 if (node->nconns > 1)
900 select_input_connection(codec, node, i);
901 unmute_input(codec, node, i);
902 unmute_output(codec, node);
903 return err;
904 }
905 }
906 return 0;
907}
908
909/*
910 * parse the tree and build the loopback controls
911 */
912static int build_loopback_controls(struct hda_codec *codec)
913{
914 struct hda_gspec *spec = codec->spec;
915 struct list_head *p;
916 struct hda_gnode *node;
917 int err;
918 const char *type;
919
Takashi Iwai97ec5582006-03-21 11:29:07 +0100920 if (! spec->out_pin_node[0])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 return 0;
922
923 list_for_each(p, &spec->nid_list) {
924 node = list_entry(p, struct hda_gnode, list);
925 if (node->type != AC_WID_PIN)
926 continue;
927 /* input capable? */
928 if (! (node->pin_caps & AC_PINCAP_IN))
929 return 0;
930 type = get_input_type(node, NULL);
931 if (type) {
932 if (check_existing_control(codec, type, "Playback"))
933 continue;
934 clear_check_flags(spec);
Takashi Iwai97ec5582006-03-21 11:29:07 +0100935 err = parse_loopback_path(codec, spec,
936 spec->out_pin_node[0],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 node, type);
938 if (err < 0)
939 return err;
940 if (! err)
941 continue;
942 }
943 }
944 return 0;
945}
946
947/*
948 * build mixer controls
949 */
950static int build_generic_controls(struct hda_codec *codec)
951{
952 int err;
953
954 if ((err = build_input_controls(codec)) < 0 ||
955 (err = build_output_controls(codec)) < 0 ||
956 (err = build_loopback_controls(codec)) < 0)
957 return err;
958
959 return 0;
960}
961
962/*
963 * PCM
964 */
965static struct hda_pcm_stream generic_pcm_playback = {
966 .substreams = 1,
967 .channels_min = 2,
968 .channels_max = 2,
969};
970
Takashi Iwai97ec5582006-03-21 11:29:07 +0100971static int generic_pcm2_prepare(struct hda_pcm_stream *hinfo,
972 struct hda_codec *codec,
973 unsigned int stream_tag,
974 unsigned int format,
975 struct snd_pcm_substream *substream)
976{
977 struct hda_gspec *spec = codec->spec;
978
979 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
980 snd_hda_codec_setup_stream(codec, spec->dac_node[1]->nid,
981 stream_tag, 0, format);
982 return 0;
983}
984
985static int generic_pcm2_cleanup(struct hda_pcm_stream *hinfo,
986 struct hda_codec *codec,
987 struct snd_pcm_substream *substream)
988{
989 struct hda_gspec *spec = codec->spec;
990
991 snd_hda_codec_setup_stream(codec, hinfo->nid, 0, 0, 0);
992 snd_hda_codec_setup_stream(codec, spec->dac_node[1]->nid, 0, 0, 0);
993 return 0;
994}
995
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996static int build_generic_pcms(struct hda_codec *codec)
997{
998 struct hda_gspec *spec = codec->spec;
999 struct hda_pcm *info = &spec->pcm_rec;
1000
Takashi Iwai97ec5582006-03-21 11:29:07 +01001001 if (! spec->dac_node[0] && ! spec->adc_node) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 snd_printd("hda_generic: no PCM found\n");
1003 return 0;
1004 }
1005
1006 codec->num_pcms = 1;
1007 codec->pcm_info = info;
1008
1009 info->name = "HDA Generic";
Takashi Iwai97ec5582006-03-21 11:29:07 +01001010 if (spec->dac_node[0]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 info->stream[0] = generic_pcm_playback;
Takashi Iwai97ec5582006-03-21 11:29:07 +01001012 info->stream[0].nid = spec->dac_node[0]->nid;
1013 if (spec->dac_node[1]) {
1014 info->stream[0].ops.prepare = generic_pcm2_prepare;
1015 info->stream[0].ops.cleanup = generic_pcm2_cleanup;
1016 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 }
1018 if (spec->adc_node) {
1019 info->stream[1] = generic_pcm_playback;
1020 info->stream[1].nid = spec->adc_node->nid;
1021 }
1022
1023 return 0;
1024}
1025
1026
1027/*
1028 */
1029static struct hda_codec_ops generic_patch_ops = {
1030 .build_controls = build_generic_controls,
1031 .build_pcms = build_generic_pcms,
1032 .free = snd_hda_generic_free,
1033};
1034
1035/*
1036 * the generic parser
1037 */
1038int snd_hda_parse_generic_codec(struct hda_codec *codec)
1039{
1040 struct hda_gspec *spec;
1041 int err;
1042
Sasha Khapyorsky84802f02005-09-13 11:25:54 +02001043 if(!codec->afg)
1044 return 0;
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001045
Takashi Iwaie560d8d2005-09-09 14:21:46 +02001046 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 if (spec == NULL) {
1048 printk(KERN_ERR "hda_generic: can't allocate spec\n");
1049 return -ENOMEM;
1050 }
1051 codec->spec = spec;
1052 INIT_LIST_HEAD(&spec->nid_list);
1053
1054 if ((err = build_afg_tree(codec)) < 0)
1055 goto error;
1056
1057 if ((err = parse_input(codec)) < 0 ||
1058 (err = parse_output(codec)) < 0)
1059 goto error;
1060
1061 codec->patch_ops = generic_patch_ops;
1062
1063 return 0;
1064
1065 error:
1066 snd_hda_generic_free(codec);
1067 return err;
1068}