blob: 91cd9b9ea5d156e7b59dab57559d18eb7dbd2f39 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <sound/core.h>
27#include "hda_codec.h"
28#include "hda_local.h"
29
30/* widget node for parsing */
31struct hda_gnode {
32 hda_nid_t nid; /* NID of this widget */
33 unsigned short nconns; /* number of input connections */
Takashi Iwaid2569502005-11-21 16:33:51 +010034 hda_nid_t *conn_list;
35 hda_nid_t slist[2]; /* temporay list */
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 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;
45};
46
Takashi Iwaic3132922005-04-20 13:43:00 +020047/* patch-specific record */
Takashi Iwaia7da6ce2006-09-06 14:03:14 +020048
49#define MAX_PCM_VOLS 2
50struct pcm_vol {
51 struct hda_gnode *node; /* Node for PCM volume */
52 unsigned int index; /* connection of PCM volume */
53};
54
Linus Torvalds1da177e2005-04-16 15:20:36 -070055struct hda_gspec {
Takashi Iwai97ec5582006-03-21 11:29:07 +010056 struct hda_gnode *dac_node[2]; /* DAC node */
57 struct hda_gnode *out_pin_node[2]; /* Output pin (Line-Out) node */
Takashi Iwaia7da6ce2006-09-06 14:03:14 +020058 struct pcm_vol pcm_vol[MAX_PCM_VOLS]; /* PCM volumes */
59 unsigned int pcm_vol_nodes; /* number of PCM volumes */
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
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];
66
67 unsigned int def_amp_in_caps;
68 unsigned int def_amp_out_caps;
69
70 struct hda_pcm pcm_rec; /* PCM information */
71
72 struct list_head nid_list; /* list of widgets */
73};
74
75/*
76 * retrieve the default device type from the default config value
77 */
Takashi Iwai97ec5582006-03-21 11:29:07 +010078#define defcfg_type(node) (((node)->def_cfg & AC_DEFCFG_DEVICE) >> \
79 AC_DEFCFG_DEVICE_SHIFT)
80#define defcfg_location(node) (((node)->def_cfg & AC_DEFCFG_LOCATION) >> \
81 AC_DEFCFG_LOCATION_SHIFT)
82#define defcfg_port_conn(node) (((node)->def_cfg & AC_DEFCFG_PORT_CONN) >> \
83 AC_DEFCFG_PORT_CONN_SHIFT)
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
85/*
86 * destructor
87 */
88static void snd_hda_generic_free(struct hda_codec *codec)
89{
90 struct hda_gspec *spec = codec->spec;
91 struct list_head *p, *n;
92
93 if (! spec)
94 return;
95 /* free all widgets */
96 list_for_each_safe(p, n, &spec->nid_list) {
97 struct hda_gnode *node = list_entry(p, struct hda_gnode, list);
Takashi Iwaid2569502005-11-21 16:33:51 +010098 if (node->conn_list != node->slist)
99 kfree(node->conn_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 kfree(node);
101 }
102 kfree(spec);
103}
104
105
106/*
107 * add a new widget node and read its attributes
108 */
109static int add_new_node(struct hda_codec *codec, struct hda_gspec *spec, hda_nid_t nid)
110{
111 struct hda_gnode *node;
112 int nconns;
Takashi Iwaid2569502005-11-21 16:33:51 +0100113 hda_nid_t conn_list[HDA_MAX_CONNECTIONS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200115 node = kzalloc(sizeof(*node), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 if (node == NULL)
117 return -ENOMEM;
118 node->nid = nid;
Takashi Iwaid2569502005-11-21 16:33:51 +0100119 nconns = snd_hda_get_connections(codec, nid, conn_list,
120 HDA_MAX_CONNECTIONS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 if (nconns < 0) {
122 kfree(node);
123 return nconns;
124 }
Takashi Iwaid2569502005-11-21 16:33:51 +0100125 if (nconns <= ARRAY_SIZE(node->slist))
126 node->conn_list = node->slist;
127 else {
128 node->conn_list = kmalloc(sizeof(hda_nid_t) * nconns,
129 GFP_KERNEL);
130 if (! node->conn_list) {
131 snd_printk(KERN_ERR "hda-generic: cannot malloc\n");
132 kfree(node);
133 return -ENOMEM;
134 }
135 }
Takashi Iwai0bbed752007-05-08 15:17:15 +0200136 memcpy(node->conn_list, conn_list, nconns * sizeof(hda_nid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 node->nconns = nconns;
Takashi Iwaid2569502005-11-21 16:33:51 +0100138 node->wid_caps = get_wcaps(codec, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 node->type = (node->wid_caps & AC_WCAP_TYPE) >> AC_WCAP_TYPE_SHIFT;
140
141 if (node->type == AC_WID_PIN) {
142 node->pin_caps = snd_hda_param_read(codec, node->nid, AC_PAR_PIN_CAP);
143 node->pin_ctl = snd_hda_codec_read(codec, node->nid, 0, AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
144 node->def_cfg = snd_hda_codec_read(codec, node->nid, 0, AC_VERB_GET_CONFIG_DEFAULT, 0);
145 }
146
147 if (node->wid_caps & AC_WCAP_OUT_AMP) {
148 if (node->wid_caps & AC_WCAP_AMP_OVRD)
149 node->amp_out_caps = snd_hda_param_read(codec, node->nid, AC_PAR_AMP_OUT_CAP);
150 if (! node->amp_out_caps)
151 node->amp_out_caps = spec->def_amp_out_caps;
152 }
153 if (node->wid_caps & AC_WCAP_IN_AMP) {
154 if (node->wid_caps & AC_WCAP_AMP_OVRD)
155 node->amp_in_caps = snd_hda_param_read(codec, node->nid, AC_PAR_AMP_IN_CAP);
156 if (! node->amp_in_caps)
157 node->amp_in_caps = spec->def_amp_in_caps;
158 }
159 list_add_tail(&node->list, &spec->nid_list);
160 return 0;
161}
162
163/*
164 * build the AFG subtree
165 */
166static int build_afg_tree(struct hda_codec *codec)
167{
168 struct hda_gspec *spec = codec->spec;
169 int i, nodes, err;
170 hda_nid_t nid;
171
172 snd_assert(spec, return -EINVAL);
173
174 spec->def_amp_out_caps = snd_hda_param_read(codec, codec->afg, AC_PAR_AMP_OUT_CAP);
175 spec->def_amp_in_caps = snd_hda_param_read(codec, codec->afg, AC_PAR_AMP_IN_CAP);
176
177 nodes = snd_hda_get_sub_nodes(codec, codec->afg, &nid);
178 if (! nid || nodes < 0) {
179 printk(KERN_ERR "Invalid AFG subtree\n");
180 return -EINVAL;
181 }
182
183 /* parse all nodes belonging to the AFG */
184 for (i = 0; i < nodes; i++, nid++) {
185 if ((err = add_new_node(codec, spec, nid)) < 0)
186 return err;
187 }
188
189 return 0;
190}
191
192
193/*
194 * look for the node record for the given NID
195 */
196/* FIXME: should avoid the braindead linear search */
197static struct hda_gnode *hda_get_node(struct hda_gspec *spec, hda_nid_t nid)
198{
199 struct list_head *p;
200 struct hda_gnode *node;
201
202 list_for_each(p, &spec->nid_list) {
203 node = list_entry(p, struct hda_gnode, list);
204 if (node->nid == nid)
205 return node;
206 }
207 return NULL;
208}
209
210/*
211 * unmute (and set max vol) the output amplifier
212 */
213static int unmute_output(struct hda_codec *codec, struct hda_gnode *node)
214{
215 unsigned int val, ofs;
216 snd_printdd("UNMUTE OUT: NID=0x%x\n", node->nid);
217 val = (node->amp_out_caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
218 ofs = (node->amp_out_caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT;
219 if (val >= ofs)
220 val -= ofs;
Takashi Iwai47fd8302007-08-10 17:11:07 +0200221 snd_hda_codec_amp_stereo(codec, node->nid, HDA_OUTPUT, 0, 0xff, val);
Takashi Iwai82beb8f2007-08-10 17:09:26 +0200222 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223}
224
225/*
226 * unmute (and set max vol) the input amplifier
227 */
228static int unmute_input(struct hda_codec *codec, struct hda_gnode *node, unsigned int index)
229{
230 unsigned int val, ofs;
231 snd_printdd("UNMUTE IN: NID=0x%x IDX=0x%x\n", node->nid, index);
232 val = (node->amp_in_caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
233 ofs = (node->amp_in_caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT;
234 if (val >= ofs)
235 val -= ofs;
Takashi Iwai47fd8302007-08-10 17:11:07 +0200236 snd_hda_codec_amp_stereo(codec, node->nid, HDA_INPUT, index, 0xff, val);
Takashi Iwai82beb8f2007-08-10 17:09:26 +0200237 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238}
239
240/*
241 * select the input connection of the given node.
242 */
243static int select_input_connection(struct hda_codec *codec, struct hda_gnode *node,
244 unsigned int index)
245{
246 snd_printdd("CONNECT: NID=0x%x IDX=0x%x\n", node->nid, index);
Takashi Iwai82beb8f2007-08-10 17:09:26 +0200247 return snd_hda_codec_write_cache(codec, node->nid, 0,
248 AC_VERB_SET_CONNECT_SEL, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249}
250
251/*
252 * clear checked flag of each node in the node list
253 */
254static void clear_check_flags(struct hda_gspec *spec)
255{
256 struct list_head *p;
257 struct hda_gnode *node;
258
259 list_for_each(p, &spec->nid_list) {
260 node = list_entry(p, struct hda_gnode, list);
261 node->checked = 0;
262 }
263}
264
265/*
266 * parse the output path recursively until reach to an audio output widget
267 *
268 * returns 0 if not found, 1 if found, or a negative error code.
269 */
270static int parse_output_path(struct hda_codec *codec, struct hda_gspec *spec,
Takashi Iwai97ec5582006-03-21 11:29:07 +0100271 struct hda_gnode *node, int dac_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272{
273 int i, err;
274 struct hda_gnode *child;
275
276 if (node->checked)
277 return 0;
278
279 node->checked = 1;
280 if (node->type == AC_WID_AUD_OUT) {
281 if (node->wid_caps & AC_WCAP_DIGITAL) {
282 snd_printdd("Skip Digital OUT node %x\n", node->nid);
283 return 0;
284 }
285 snd_printdd("AUD_OUT found %x\n", node->nid);
Takashi Iwai97ec5582006-03-21 11:29:07 +0100286 if (spec->dac_node[dac_idx]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 /* already DAC node is assigned, just unmute & connect */
Takashi Iwai97ec5582006-03-21 11:29:07 +0100288 return node == spec->dac_node[dac_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 }
Takashi Iwai97ec5582006-03-21 11:29:07 +0100290 spec->dac_node[dac_idx] = node;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +0200291 if ((node->wid_caps & AC_WCAP_OUT_AMP) &&
292 spec->pcm_vol_nodes < MAX_PCM_VOLS) {
293 spec->pcm_vol[spec->pcm_vol_nodes].node = node;
294 spec->pcm_vol[spec->pcm_vol_nodes].index = 0;
295 spec->pcm_vol_nodes++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 }
297 return 1; /* found */
298 }
299
300 for (i = 0; i < node->nconns; i++) {
301 child = hda_get_node(spec, node->conn_list[i]);
302 if (! child)
303 continue;
Takashi Iwai97ec5582006-03-21 11:29:07 +0100304 err = parse_output_path(codec, spec, child, dac_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 if (err < 0)
306 return err;
307 else if (err > 0) {
308 /* found one,
309 * select the path, unmute both input and output
310 */
311 if (node->nconns > 1)
312 select_input_connection(codec, node, i);
313 unmute_input(codec, node, i);
314 unmute_output(codec, node);
Takashi Iwaia7da6ce2006-09-06 14:03:14 +0200315 if (spec->dac_node[dac_idx] &&
316 spec->pcm_vol_nodes < MAX_PCM_VOLS &&
317 !(spec->dac_node[dac_idx]->wid_caps &
318 AC_WCAP_OUT_AMP)) {
319 if ((node->wid_caps & AC_WCAP_IN_AMP) ||
320 (node->wid_caps & AC_WCAP_OUT_AMP)) {
321 int n = spec->pcm_vol_nodes;
322 spec->pcm_vol[n].node = node;
323 spec->pcm_vol[n].index = i;
324 spec->pcm_vol_nodes++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 }
326 }
327 return 1;
328 }
329 }
330 return 0;
331}
332
333/*
334 * Look for the output PIN widget with the given jack type
335 * and parse the output path to that PIN.
336 *
337 * Returns the PIN node when the path to DAC is established.
338 */
339static struct hda_gnode *parse_output_jack(struct hda_codec *codec,
340 struct hda_gspec *spec,
341 int jack_type)
342{
343 struct list_head *p;
344 struct hda_gnode *node;
345 int err;
346
347 list_for_each(p, &spec->nid_list) {
348 node = list_entry(p, struct hda_gnode, list);
349 if (node->type != AC_WID_PIN)
350 continue;
351 /* output capable? */
352 if (! (node->pin_caps & AC_PINCAP_OUT))
353 continue;
Takashi Iwai97ec5582006-03-21 11:29:07 +0100354 if (defcfg_port_conn(node) == AC_JACK_PORT_NONE)
355 continue; /* unconnected */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 if (jack_type >= 0) {
Takashi Iwaie9edcee2005-06-13 14:16:38 +0200357 if (jack_type != defcfg_type(node))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 continue;
359 if (node->wid_caps & AC_WCAP_DIGITAL)
360 continue; /* skip SPDIF */
361 } else {
362 /* output as default? */
363 if (! (node->pin_ctl & AC_PINCTL_OUT_EN))
364 continue;
365 }
366 clear_check_flags(spec);
Takashi Iwai97ec5582006-03-21 11:29:07 +0100367 err = parse_output_path(codec, spec, node, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 if (err < 0)
369 return NULL;
Takashi Iwai97ec5582006-03-21 11:29:07 +0100370 if (! err && spec->out_pin_node[0]) {
371 err = parse_output_path(codec, spec, node, 1);
372 if (err < 0)
373 return NULL;
374 }
375 if (err > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 /* unmute the PIN output */
377 unmute_output(codec, node);
378 /* set PIN-Out enable */
Takashi Iwai82beb8f2007-08-10 17:09:26 +0200379 snd_hda_codec_write_cache(codec, node->nid, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 AC_VERB_SET_PIN_WIDGET_CONTROL,
Takashi Iwaia7da6ce2006-09-06 14:03:14 +0200381 AC_PINCTL_OUT_EN |
382 ((node->pin_caps & AC_PINCAP_HP_DRV) ?
383 AC_PINCTL_HP_EN : 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 return node;
385 }
386 }
387 return NULL;
388}
389
390
391/*
392 * parse outputs
393 */
394static int parse_output(struct hda_codec *codec)
395{
396 struct hda_gspec *spec = codec->spec;
397 struct hda_gnode *node;
398
399 /*
400 * Look for the output PIN widget
401 */
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 */
Takashi Iwai97ec5582006-03-21 11:29:07 +0100405 spec->out_pin_node[0] = node;
406 else {
407 /* if no line-out is found, try speaker out */
408 node = parse_output_jack(codec, spec, AC_JACK_SPEAKER);
409 if (node)
410 spec->out_pin_node[0] = node;
411 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 /* look for the HP-out pin */
413 node = parse_output_jack(codec, spec, AC_JACK_HP_OUT);
414 if (node) {
Takashi Iwai97ec5582006-03-21 11:29:07 +0100415 if (! spec->out_pin_node[0])
416 spec->out_pin_node[0] = node;
417 else
418 spec->out_pin_node[1] = node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 }
420
Takashi Iwai97ec5582006-03-21 11:29:07 +0100421 if (! spec->out_pin_node[0]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 /* no line-out or HP pins found,
423 * then choose for the first output pin
424 */
Takashi Iwai97ec5582006-03-21 11:29:07 +0100425 spec->out_pin_node[0] = parse_output_jack(codec, spec, -1);
426 if (! spec->out_pin_node[0])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 snd_printd("hda_generic: no proper output path found\n");
428 }
429
430 return 0;
431}
432
433/*
434 * input MUX
435 */
436
437/* control callbacks */
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +0100438static int capture_source_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439{
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);
443}
444
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +0100445static int capture_source_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446{
447 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
448 struct hda_gspec *spec = codec->spec;
449
450 ucontrol->value.enumerated.item[0] = spec->cur_cap_src;
451 return 0;
452}
453
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +0100454static int capture_source_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455{
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);
460}
461
462/*
463 * return the string name of the given input PIN widget
464 */
465static const char *get_input_type(struct hda_gnode *node, unsigned int *pinctl)
466{
Takashi Iwaie9edcee2005-06-13 14:16:38 +0200467 unsigned int location = defcfg_location(node);
468 switch (defcfg_type(node)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 case AC_JACK_LINE_IN:
470 if ((location & 0x0f) == AC_JACK_LOC_FRONT)
471 return "Front Line";
472 return "Line";
473 case AC_JACK_CD:
Takashi Iwai071c73a2006-08-23 18:34:06 +0200474#if 0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 if (pinctl)
Matt1a12de1e2005-04-13 14:37:50 +0200476 *pinctl |= AC_PINCTL_VREF_GRD;
Takashi Iwai071c73a2006-08-23 18:34:06 +0200477#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 return "CD";
479 case AC_JACK_AUX:
480 if ((location & 0x0f) == AC_JACK_LOC_FRONT)
481 return "Front Aux";
482 return "Aux";
483 case AC_JACK_MIC_IN:
Takashi Iwai6afeb112006-12-18 16:16:04 +0100484 if (pinctl &&
485 (node->pin_caps &
486 (AC_PINCAP_VREF_80 << AC_PINCAP_VREF_SHIFT)))
Takashi Iwai071c73a2006-08-23 18:34:06 +0200487 *pinctl |= AC_PINCTL_VREF_80;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 if ((location & 0x0f) == AC_JACK_LOC_FRONT)
489 return "Front Mic";
490 return "Mic";
491 case AC_JACK_SPDIF_IN:
492 return "SPDIF";
493 case AC_JACK_DIG_OTHER_IN:
494 return "Digital";
495 }
496 return NULL;
497}
498
499/*
500 * parse the nodes recursively until reach to the input PIN
501 *
502 * returns 0 if not found, 1 if found, or a negative error code.
503 */
504static int parse_adc_sub_nodes(struct hda_codec *codec, struct hda_gspec *spec,
505 struct hda_gnode *node)
506{
507 int i, err;
508 unsigned int pinctl;
509 char *label;
510 const char *type;
511
512 if (node->checked)
513 return 0;
514
515 node->checked = 1;
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]);
520 if (! child)
521 continue;
522 err = parse_adc_sub_nodes(codec, spec, child);
523 if (err < 0)
524 return err;
525 if (err > 0) {
526 /* found one,
527 * select the path, unmute both input and output
528 */
529 if (node->nconns > 1)
530 select_input_connection(codec, node, i);
531 unmute_input(codec, node, i);
532 unmute_output(codec, node);
533 return err;
534 }
535 }
536 return 0;
537 }
538
539 /* input capable? */
540 if (! (node->pin_caps & AC_PINCAP_IN))
541 return 0;
542
Takashi Iwai97ec5582006-03-21 11:29:07 +0100543 if (defcfg_port_conn(node) == AC_JACK_PORT_NONE)
544 return 0; /* unconnected */
545
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 if (node->wid_caps & AC_WCAP_DIGITAL)
547 return 0; /* skip SPDIF */
548
549 if (spec->input_mux.num_items >= HDA_MAX_NUM_INPUTS) {
550 snd_printk(KERN_ERR "hda_generic: Too many items for capture\n");
551 return -EINVAL;
552 }
553
554 pinctl = AC_PINCTL_IN_EN;
555 /* create a proper capture source label */
556 type = get_input_type(node, &pinctl);
557 if (! type) {
558 /* input as default? */
559 if (! (node->pin_ctl & AC_PINCTL_IN_EN))
560 return 0;
561 type = "Input";
562 }
563 label = spec->cap_labels[spec->input_mux.num_items];
564 strcpy(label, type);
565 spec->input_mux.items[spec->input_mux.num_items].label = label;
566
567 /* unmute the PIN external input */
568 unmute_input(codec, node, 0); /* index = 0? */
569 /* set PIN-In enable */
Takashi Iwai82beb8f2007-08-10 17:09:26 +0200570 snd_hda_codec_write_cache(codec, node->nid, 0,
571 AC_VERB_SET_PIN_WIDGET_CONTROL, pinctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
573 return 1; /* found */
574}
575
Takashi Iwai071c73a2006-08-23 18:34:06 +0200576/* add a capture source element */
577static void add_cap_src(struct hda_gspec *spec, int idx)
578{
579 struct hda_input_mux_item *csrc;
580 char *buf;
581 int num, ocap;
582
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
590 */
591 sprintf(buf, "%s %d", spec->cap_labels[ocap], num);
592 break;
593 }
594 }
595 csrc->index = idx;
596 spec->input_mux.num_items++;
597}
598
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599/*
600 * parse input
601 */
602static int parse_input_path(struct hda_codec *codec, struct hda_gnode *adc_node)
603{
604 struct hda_gspec *spec = codec->spec;
605 struct hda_gnode *node;
606 int i, err;
607
608 snd_printdd("AUD_IN = %x\n", adc_node->nid);
609 clear_check_flags(spec);
610
611 // awk added - fixed no recording due to muted widget
612 unmute_input(codec, adc_node, 0);
613
614 /*
615 * check each connection of the ADC
616 * if it reaches to a proper input PIN, add the path as the
617 * input path.
618 */
Takashi Iwai071c73a2006-08-23 18:34:06 +0200619 /* first, check the direct connections to PIN widgets */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 for (i = 0; i < adc_node->nconns; i++) {
621 node = hda_get_node(spec, adc_node->conn_list[i]);
Takashi Iwai071c73a2006-08-23 18:34:06 +0200622 if (node && node->type == AC_WID_PIN) {
623 err = parse_adc_sub_nodes(codec, spec, node);
624 if (err < 0)
625 return err;
626 else if (err > 0)
627 add_cap_src(spec, i);
628 }
629 }
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);
635 if (err < 0)
636 return err;
637 else if (err > 0)
638 add_cap_src(spec, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 }
640 }
641
642 if (! spec->input_mux.num_items)
643 return 0; /* no input path found... */
644
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);
649
650 spec->adc_node = adc_node;
651 return 1;
652}
653
654/*
655 * parse input
656 */
657static int parse_input(struct hda_codec *codec)
658{
659 struct hda_gspec *spec = codec->spec;
660 struct list_head *p;
661 struct hda_gnode *node;
662 int err;
663
664 /*
665 * At first we look for an audio input widget.
666 * If it reaches to certain input PINs, we take it as the
667 * input path.
668 */
669 list_for_each(p, &spec->nid_list) {
670 node = list_entry(p, struct hda_gnode, list);
671 if (node->wid_caps & AC_WCAP_DIGITAL)
672 continue; /* skip SPDIF */
673 if (node->type == AC_WID_AUD_IN) {
674 err = parse_input_path(codec, node);
675 if (err < 0)
676 return err;
677 else if (err > 0)
678 return 0;
679 }
680 }
681 snd_printd("hda_generic: no proper input path found\n");
682 return 0;
683}
684
685/*
686 * create mixer controls if possible
687 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688static int create_mixer(struct hda_codec *codec, struct hda_gnode *node,
689 unsigned int index, const char *type, const char *dir_sfx)
690{
691 char name[32];
692 int err;
693 int created = 0;
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +0100694 struct snd_kcontrol_new knew;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
696 if (type)
697 sprintf(name, "%s %s Switch", type, dir_sfx);
698 else
699 sprintf(name, "%s Switch", dir_sfx);
700 if ((node->wid_caps & AC_WCAP_IN_AMP) &&
701 (node->amp_in_caps & AC_AMPCAP_MUTE)) {
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +0100702 knew = (struct snd_kcontrol_new)HDA_CODEC_MUTE(name, node->nid, index, HDA_INPUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 snd_printdd("[%s] NID=0x%x, DIR=IN, IDX=0x%x\n", name, node->nid, index);
704 if ((err = snd_ctl_add(codec->bus->card, snd_ctl_new1(&knew, codec))) < 0)
705 return err;
706 created = 1;
707 } else if ((node->wid_caps & AC_WCAP_OUT_AMP) &&
708 (node->amp_out_caps & AC_AMPCAP_MUTE)) {
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +0100709 knew = (struct snd_kcontrol_new)HDA_CODEC_MUTE(name, node->nid, 0, HDA_OUTPUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 snd_printdd("[%s] NID=0x%x, DIR=OUT\n", name, node->nid);
711 if ((err = snd_ctl_add(codec->bus->card, snd_ctl_new1(&knew, codec))) < 0)
712 return err;
713 created = 1;
714 }
715
716 if (type)
717 sprintf(name, "%s %s Volume", type, dir_sfx);
718 else
719 sprintf(name, "%s Volume", dir_sfx);
720 if ((node->wid_caps & AC_WCAP_IN_AMP) &&
721 (node->amp_in_caps & AC_AMPCAP_NUM_STEPS)) {
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +0100722 knew = (struct snd_kcontrol_new)HDA_CODEC_VOLUME(name, node->nid, index, HDA_INPUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 snd_printdd("[%s] NID=0x%x, DIR=IN, IDX=0x%x\n", name, node->nid, index);
724 if ((err = snd_ctl_add(codec->bus->card, snd_ctl_new1(&knew, codec))) < 0)
725 return err;
726 created = 1;
727 } else if ((node->wid_caps & AC_WCAP_OUT_AMP) &&
728 (node->amp_out_caps & AC_AMPCAP_NUM_STEPS)) {
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +0100729 knew = (struct snd_kcontrol_new)HDA_CODEC_VOLUME(name, node->nid, 0, HDA_OUTPUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 snd_printdd("[%s] NID=0x%x, DIR=OUT\n", name, node->nid);
731 if ((err = snd_ctl_add(codec->bus->card, snd_ctl_new1(&knew, codec))) < 0)
732 return err;
733 created = 1;
734 }
735
736 return created;
737}
738
739/*
740 * check whether the controls with the given name and direction suffix already exist
741 */
742static int check_existing_control(struct hda_codec *codec, const char *type, const char *dir)
743{
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +0100744 struct snd_ctl_elem_id id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 memset(&id, 0, sizeof(id));
746 sprintf(id.name, "%s %s Volume", type, dir);
747 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
748 if (snd_ctl_find_id(codec->bus->card, &id))
749 return 1;
750 sprintf(id.name, "%s %s Switch", type, dir);
751 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
752 if (snd_ctl_find_id(codec->bus->card, &id))
753 return 1;
754 return 0;
755}
756
757/*
758 * build output mixer controls
759 */
Takashi Iwaia7da6ce2006-09-06 14:03:14 +0200760static int create_output_mixers(struct hda_codec *codec, const char **names)
761{
762 struct hda_gspec *spec = codec->spec;
763 int i, err;
764
765 for (i = 0; i < spec->pcm_vol_nodes; i++) {
766 err = create_mixer(codec, spec->pcm_vol[i].node,
767 spec->pcm_vol[i].index,
768 names[i], "Playback");
769 if (err < 0)
770 return err;
771 }
772 return 0;
773}
774
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775static int build_output_controls(struct hda_codec *codec)
776{
777 struct hda_gspec *spec = codec->spec;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +0200778 static const char *types_speaker[] = { "Speaker", "Headphone" };
779 static const char *types_line[] = { "Front", "Headphone" };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780
Takashi Iwaia7da6ce2006-09-06 14:03:14 +0200781 switch (spec->pcm_vol_nodes) {
782 case 1:
783 return create_mixer(codec, spec->pcm_vol[0].node,
784 spec->pcm_vol[0].index,
785 "Master", "Playback");
786 case 2:
787 if (defcfg_type(spec->out_pin_node[0]) == AC_JACK_SPEAKER)
788 return create_output_mixers(codec, types_speaker);
789 else
790 return create_output_mixers(codec, types_line);
Takashi Iwai97ec5582006-03-21 11:29:07 +0100791 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 return 0;
793}
794
795/* create capture volume/switch */
796static int build_input_controls(struct hda_codec *codec)
797{
798 struct hda_gspec *spec = codec->spec;
799 struct hda_gnode *adc_node = spec->adc_node;
Takashi Iwai071c73a2006-08-23 18:34:06 +0200800 int i, err;
801 static struct snd_kcontrol_new cap_sel = {
802 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
803 .name = "Capture Source",
804 .info = capture_source_info,
805 .get = capture_source_get,
806 .put = capture_source_put,
807 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
Takashi Iwai071c73a2006-08-23 18:34:06 +0200809 if (! adc_node || ! spec->input_mux.num_items)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 return 0; /* not found */
811
Takashi Iwai071c73a2006-08-23 18:34:06 +0200812 spec->cur_cap_src = 0;
813 select_input_connection(codec, adc_node,
814 spec->input_mux.items[0].index);
815
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 /* create capture volume and switch controls if the ADC has an amp */
Takashi Iwai071c73a2006-08-23 18:34:06 +0200817 /* do we have only a single item? */
818 if (spec->input_mux.num_items == 1) {
819 err = create_mixer(codec, adc_node,
820 spec->input_mux.items[0].index,
821 NULL, "Capture");
822 if (err < 0)
823 return err;
824 return 0;
825 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826
827 /* create input MUX if multiple sources are available */
Takashi Iwai071c73a2006-08-23 18:34:06 +0200828 if ((err = snd_ctl_add(codec->bus->card,
829 snd_ctl_new1(&cap_sel, codec))) < 0)
830 return err;
831
832 /* no volume control? */
833 if (! (adc_node->wid_caps & AC_WCAP_IN_AMP) ||
834 ! (adc_node->amp_in_caps & AC_AMPCAP_NUM_STEPS))
835 return 0;
836
837 for (i = 0; i < spec->input_mux.num_items; i++) {
838 struct snd_kcontrol_new knew;
839 char name[32];
840 sprintf(name, "%s Capture Volume",
841 spec->input_mux.items[i].label);
842 knew = (struct snd_kcontrol_new)
843 HDA_CODEC_VOLUME(name, adc_node->nid,
844 spec->input_mux.items[i].index,
845 HDA_INPUT);
846 if ((err = snd_ctl_add(codec->bus->card,
847 snd_ctl_new1(&knew, codec))) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 }
Takashi Iwai071c73a2006-08-23 18:34:06 +0200850
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 return 0;
852}
853
854
855/*
856 * parse the nodes recursively until reach to the output PIN.
857 *
858 * returns 0 - if not found,
859 * 1 - if found, but no mixer is created
860 * 2 - if found and mixer was already created, (just skip)
861 * a negative error code
862 */
863static int parse_loopback_path(struct hda_codec *codec, struct hda_gspec *spec,
864 struct hda_gnode *node, struct hda_gnode *dest_node,
865 const char *type)
866{
867 int i, err;
868
869 if (node->checked)
870 return 0;
871
872 node->checked = 1;
873 if (node == dest_node) {
874 /* loopback connection found */
875 return 1;
876 }
877
878 for (i = 0; i < node->nconns; i++) {
879 struct hda_gnode *child = hda_get_node(spec, node->conn_list[i]);
880 if (! child)
881 continue;
882 err = parse_loopback_path(codec, spec, child, dest_node, type);
883 if (err < 0)
884 return err;
885 else if (err >= 1) {
886 if (err == 1) {
887 err = create_mixer(codec, node, i, type, "Playback");
888 if (err < 0)
889 return err;
890 if (err > 0)
891 return 2; /* ok, created */
892 /* not created, maybe in the lower path */
893 err = 1;
894 }
895 /* connect and unmute */
896 if (node->nconns > 1)
897 select_input_connection(codec, node, i);
898 unmute_input(codec, node, i);
899 unmute_output(codec, node);
900 return err;
901 }
902 }
903 return 0;
904}
905
906/*
907 * parse the tree and build the loopback controls
908 */
909static int build_loopback_controls(struct hda_codec *codec)
910{
911 struct hda_gspec *spec = codec->spec;
912 struct list_head *p;
913 struct hda_gnode *node;
914 int err;
915 const char *type;
916
Takashi Iwai97ec5582006-03-21 11:29:07 +0100917 if (! spec->out_pin_node[0])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 return 0;
919
920 list_for_each(p, &spec->nid_list) {
921 node = list_entry(p, struct hda_gnode, list);
922 if (node->type != AC_WID_PIN)
923 continue;
924 /* input capable? */
925 if (! (node->pin_caps & AC_PINCAP_IN))
926 return 0;
927 type = get_input_type(node, NULL);
928 if (type) {
929 if (check_existing_control(codec, type, "Playback"))
930 continue;
931 clear_check_flags(spec);
Takashi Iwai97ec5582006-03-21 11:29:07 +0100932 err = parse_loopback_path(codec, spec,
933 spec->out_pin_node[0],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 node, type);
935 if (err < 0)
936 return err;
937 if (! err)
938 continue;
939 }
940 }
941 return 0;
942}
943
944/*
945 * build mixer controls
946 */
947static int build_generic_controls(struct hda_codec *codec)
948{
949 int err;
950
951 if ((err = build_input_controls(codec)) < 0 ||
952 (err = build_output_controls(codec)) < 0 ||
953 (err = build_loopback_controls(codec)) < 0)
954 return err;
955
956 return 0;
957}
958
959/*
960 * PCM
961 */
962static struct hda_pcm_stream generic_pcm_playback = {
963 .substreams = 1,
964 .channels_min = 2,
965 .channels_max = 2,
966};
967
Takashi Iwai97ec5582006-03-21 11:29:07 +0100968static int generic_pcm2_prepare(struct hda_pcm_stream *hinfo,
969 struct hda_codec *codec,
970 unsigned int stream_tag,
971 unsigned int format,
972 struct snd_pcm_substream *substream)
973{
974 struct hda_gspec *spec = codec->spec;
975
976 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
977 snd_hda_codec_setup_stream(codec, spec->dac_node[1]->nid,
978 stream_tag, 0, format);
979 return 0;
980}
981
982static int generic_pcm2_cleanup(struct hda_pcm_stream *hinfo,
983 struct hda_codec *codec,
984 struct snd_pcm_substream *substream)
985{
986 struct hda_gspec *spec = codec->spec;
987
988 snd_hda_codec_setup_stream(codec, hinfo->nid, 0, 0, 0);
989 snd_hda_codec_setup_stream(codec, spec->dac_node[1]->nid, 0, 0, 0);
990 return 0;
991}
992
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993static int build_generic_pcms(struct hda_codec *codec)
994{
995 struct hda_gspec *spec = codec->spec;
996 struct hda_pcm *info = &spec->pcm_rec;
997
Takashi Iwai97ec5582006-03-21 11:29:07 +0100998 if (! spec->dac_node[0] && ! spec->adc_node) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 snd_printd("hda_generic: no PCM found\n");
1000 return 0;
1001 }
1002
1003 codec->num_pcms = 1;
1004 codec->pcm_info = info;
1005
1006 info->name = "HDA Generic";
Takashi Iwai97ec5582006-03-21 11:29:07 +01001007 if (spec->dac_node[0]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 info->stream[0] = generic_pcm_playback;
Takashi Iwai97ec5582006-03-21 11:29:07 +01001009 info->stream[0].nid = spec->dac_node[0]->nid;
1010 if (spec->dac_node[1]) {
1011 info->stream[0].ops.prepare = generic_pcm2_prepare;
1012 info->stream[0].ops.cleanup = generic_pcm2_cleanup;
1013 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 }
1015 if (spec->adc_node) {
1016 info->stream[1] = generic_pcm_playback;
1017 info->stream[1].nid = spec->adc_node->nid;
1018 }
1019
1020 return 0;
1021}
1022
1023
1024/*
1025 */
1026static struct hda_codec_ops generic_patch_ops = {
1027 .build_controls = build_generic_controls,
1028 .build_pcms = build_generic_pcms,
1029 .free = snd_hda_generic_free,
1030};
1031
1032/*
1033 * the generic parser
1034 */
1035int snd_hda_parse_generic_codec(struct hda_codec *codec)
1036{
1037 struct hda_gspec *spec;
1038 int err;
1039
Sasha Khapyorsky84802f02005-09-13 11:25:54 +02001040 if(!codec->afg)
1041 return 0;
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001042
Takashi Iwaie560d8d2005-09-09 14:21:46 +02001043 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 if (spec == NULL) {
1045 printk(KERN_ERR "hda_generic: can't allocate spec\n");
1046 return -ENOMEM;
1047 }
1048 codec->spec = spec;
1049 INIT_LIST_HEAD(&spec->nid_list);
1050
1051 if ((err = build_afg_tree(codec)) < 0)
1052 goto error;
1053
1054 if ((err = parse_input(codec)) < 0 ||
1055 (err = parse_output(codec)) < 0)
1056 goto error;
1057
1058 codec->patch_ops = generic_patch_ops;
1059
1060 return 0;
1061
1062 error:
1063 snd_hda_generic_free(codec);
1064 return err;
1065}