]> git.proxmox.com Git - mirror_ubuntu-focal-kernel.git/blame - sound/pci/hda/hda_generic.c
[ALSA] hda-codec - restore HDA sigmatel pin configs on resume
[mirror_ubuntu-focal-kernel.git] / sound / pci / hda / hda_generic.c
CommitLineData
1da177e4
LT
1/*
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 */
d2569505
TI
35 hda_nid_t *conn_list;
36 hda_nid_t slist[2]; /* temporay list */
1da177e4
LT
37 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
c3132925 48/* patch-specific record */
1da177e4 49struct hda_gspec {
97ec558a
TI
50 struct hda_gnode *dac_node[2]; /* DAC node */
51 struct hda_gnode *out_pin_node[2]; /* Output pin (Line-Out) node */
52 struct hda_gnode *pcm_vol_node[2]; /* Node for PCM volume */
53 unsigned int pcm_vol_index[2]; /* connection of PCM volume */
1da177e4
LT
54
55 struct hda_gnode *adc_node; /* ADC node */
56 struct hda_gnode *cap_vol_node; /* Node for capture volume */
57 unsigned int cur_cap_src; /* current capture source */
58 struct hda_input_mux input_mux;
59 char cap_labels[HDA_MAX_NUM_INPUTS][16];
60
61 unsigned int def_amp_in_caps;
62 unsigned int def_amp_out_caps;
63
64 struct hda_pcm pcm_rec; /* PCM information */
65
66 struct list_head nid_list; /* list of widgets */
67};
68
69/*
70 * retrieve the default device type from the default config value
71 */
97ec558a
TI
72#define defcfg_type(node) (((node)->def_cfg & AC_DEFCFG_DEVICE) >> \
73 AC_DEFCFG_DEVICE_SHIFT)
74#define defcfg_location(node) (((node)->def_cfg & AC_DEFCFG_LOCATION) >> \
75 AC_DEFCFG_LOCATION_SHIFT)
76#define defcfg_port_conn(node) (((node)->def_cfg & AC_DEFCFG_PORT_CONN) >> \
77 AC_DEFCFG_PORT_CONN_SHIFT)
1da177e4
LT
78
79/*
80 * destructor
81 */
82static void snd_hda_generic_free(struct hda_codec *codec)
83{
84 struct hda_gspec *spec = codec->spec;
85 struct list_head *p, *n;
86
87 if (! spec)
88 return;
89 /* free all widgets */
90 list_for_each_safe(p, n, &spec->nid_list) {
91 struct hda_gnode *node = list_entry(p, struct hda_gnode, list);
d2569505
TI
92 if (node->conn_list != node->slist)
93 kfree(node->conn_list);
1da177e4
LT
94 kfree(node);
95 }
96 kfree(spec);
97}
98
99
100/*
101 * add a new widget node and read its attributes
102 */
103static int add_new_node(struct hda_codec *codec, struct hda_gspec *spec, hda_nid_t nid)
104{
105 struct hda_gnode *node;
106 int nconns;
d2569505 107 hda_nid_t conn_list[HDA_MAX_CONNECTIONS];
1da177e4 108
e560d8d8 109 node = kzalloc(sizeof(*node), GFP_KERNEL);
1da177e4
LT
110 if (node == NULL)
111 return -ENOMEM;
112 node->nid = nid;
d2569505
TI
113 nconns = snd_hda_get_connections(codec, nid, conn_list,
114 HDA_MAX_CONNECTIONS);
1da177e4
LT
115 if (nconns < 0) {
116 kfree(node);
117 return nconns;
118 }
d2569505
TI
119 if (nconns <= ARRAY_SIZE(node->slist))
120 node->conn_list = node->slist;
121 else {
122 node->conn_list = kmalloc(sizeof(hda_nid_t) * nconns,
123 GFP_KERNEL);
124 if (! node->conn_list) {
125 snd_printk(KERN_ERR "hda-generic: cannot malloc\n");
126 kfree(node);
127 return -ENOMEM;
128 }
129 }
130 memcpy(node->conn_list, conn_list, nconns);
1da177e4 131 node->nconns = nconns;
d2569505 132 node->wid_caps = get_wcaps(codec, nid);
1da177e4
LT
133 node->type = (node->wid_caps & AC_WCAP_TYPE) >> AC_WCAP_TYPE_SHIFT;
134
135 if (node->type == AC_WID_PIN) {
136 node->pin_caps = snd_hda_param_read(codec, node->nid, AC_PAR_PIN_CAP);
137 node->pin_ctl = snd_hda_codec_read(codec, node->nid, 0, AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
138 node->def_cfg = snd_hda_codec_read(codec, node->nid, 0, AC_VERB_GET_CONFIG_DEFAULT, 0);
139 }
140
141 if (node->wid_caps & AC_WCAP_OUT_AMP) {
142 if (node->wid_caps & AC_WCAP_AMP_OVRD)
143 node->amp_out_caps = snd_hda_param_read(codec, node->nid, AC_PAR_AMP_OUT_CAP);
144 if (! node->amp_out_caps)
145 node->amp_out_caps = spec->def_amp_out_caps;
146 }
147 if (node->wid_caps & AC_WCAP_IN_AMP) {
148 if (node->wid_caps & AC_WCAP_AMP_OVRD)
149 node->amp_in_caps = snd_hda_param_read(codec, node->nid, AC_PAR_AMP_IN_CAP);
150 if (! node->amp_in_caps)
151 node->amp_in_caps = spec->def_amp_in_caps;
152 }
153 list_add_tail(&node->list, &spec->nid_list);
154 return 0;
155}
156
157/*
158 * build the AFG subtree
159 */
160static int build_afg_tree(struct hda_codec *codec)
161{
162 struct hda_gspec *spec = codec->spec;
163 int i, nodes, err;
164 hda_nid_t nid;
165
166 snd_assert(spec, return -EINVAL);
167
168 spec->def_amp_out_caps = snd_hda_param_read(codec, codec->afg, AC_PAR_AMP_OUT_CAP);
169 spec->def_amp_in_caps = snd_hda_param_read(codec, codec->afg, AC_PAR_AMP_IN_CAP);
170
171 nodes = snd_hda_get_sub_nodes(codec, codec->afg, &nid);
172 if (! nid || nodes < 0) {
173 printk(KERN_ERR "Invalid AFG subtree\n");
174 return -EINVAL;
175 }
176
177 /* parse all nodes belonging to the AFG */
178 for (i = 0; i < nodes; i++, nid++) {
179 if ((err = add_new_node(codec, spec, nid)) < 0)
180 return err;
181 }
182
183 return 0;
184}
185
186
187/*
188 * look for the node record for the given NID
189 */
190/* FIXME: should avoid the braindead linear search */
191static struct hda_gnode *hda_get_node(struct hda_gspec *spec, hda_nid_t nid)
192{
193 struct list_head *p;
194 struct hda_gnode *node;
195
196 list_for_each(p, &spec->nid_list) {
197 node = list_entry(p, struct hda_gnode, list);
198 if (node->nid == nid)
199 return node;
200 }
201 return NULL;
202}
203
204/*
205 * unmute (and set max vol) the output amplifier
206 */
207static int unmute_output(struct hda_codec *codec, struct hda_gnode *node)
208{
209 unsigned int val, ofs;
210 snd_printdd("UNMUTE OUT: NID=0x%x\n", node->nid);
211 val = (node->amp_out_caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
212 ofs = (node->amp_out_caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT;
213 if (val >= ofs)
214 val -= ofs;
215 val |= AC_AMP_SET_LEFT | AC_AMP_SET_RIGHT;
216 val |= AC_AMP_SET_OUTPUT;
217 return snd_hda_codec_write(codec, node->nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, val);
218}
219
220/*
221 * unmute (and set max vol) the input amplifier
222 */
223static int unmute_input(struct hda_codec *codec, struct hda_gnode *node, unsigned int index)
224{
225 unsigned int val, ofs;
226 snd_printdd("UNMUTE IN: NID=0x%x IDX=0x%x\n", node->nid, index);
227 val = (node->amp_in_caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
228 ofs = (node->amp_in_caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT;
229 if (val >= ofs)
230 val -= ofs;
231 val |= AC_AMP_SET_LEFT | AC_AMP_SET_RIGHT;
232 val |= AC_AMP_SET_INPUT;
233 // awk added - fixed to allow unmuting of indexed amps
234 val |= index << AC_AMP_SET_INDEX_SHIFT;
235 return snd_hda_codec_write(codec, node->nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, val);
236}
237
238/*
239 * select the input connection of the given node.
240 */
241static int select_input_connection(struct hda_codec *codec, struct hda_gnode *node,
242 unsigned int index)
243{
244 snd_printdd("CONNECT: NID=0x%x IDX=0x%x\n", node->nid, index);
245 return snd_hda_codec_write(codec, node->nid, 0, AC_VERB_SET_CONNECT_SEL, index);
246}
247
248/*
249 * clear checked flag of each node in the node list
250 */
251static void clear_check_flags(struct hda_gspec *spec)
252{
253 struct list_head *p;
254 struct hda_gnode *node;
255
256 list_for_each(p, &spec->nid_list) {
257 node = list_entry(p, struct hda_gnode, list);
258 node->checked = 0;
259 }
260}
261
262/*
263 * parse the output path recursively until reach to an audio output widget
264 *
265 * returns 0 if not found, 1 if found, or a negative error code.
266 */
267static int parse_output_path(struct hda_codec *codec, struct hda_gspec *spec,
97ec558a 268 struct hda_gnode *node, int dac_idx)
1da177e4
LT
269{
270 int i, err;
271 struct hda_gnode *child;
272
273 if (node->checked)
274 return 0;
275
276 node->checked = 1;
277 if (node->type == AC_WID_AUD_OUT) {
278 if (node->wid_caps & AC_WCAP_DIGITAL) {
279 snd_printdd("Skip Digital OUT node %x\n", node->nid);
280 return 0;
281 }
282 snd_printdd("AUD_OUT found %x\n", node->nid);
97ec558a 283 if (spec->dac_node[dac_idx]) {
1da177e4 284 /* already DAC node is assigned, just unmute & connect */
97ec558a 285 return node == spec->dac_node[dac_idx];
1da177e4 286 }
97ec558a 287 spec->dac_node[dac_idx] = node;
1da177e4 288 if (node->wid_caps & AC_WCAP_OUT_AMP) {
97ec558a
TI
289 spec->pcm_vol_node[dac_idx] = node;
290 spec->pcm_vol_index[dac_idx] = 0;
1da177e4
LT
291 }
292 return 1; /* found */
293 }
294
295 for (i = 0; i < node->nconns; i++) {
296 child = hda_get_node(spec, node->conn_list[i]);
297 if (! child)
298 continue;
97ec558a 299 err = parse_output_path(codec, spec, child, dac_idx);
1da177e4
LT
300 if (err < 0)
301 return err;
302 else if (err > 0) {
303 /* found one,
304 * select the path, unmute both input and output
305 */
306 if (node->nconns > 1)
307 select_input_connection(codec, node, i);
308 unmute_input(codec, node, i);
309 unmute_output(codec, node);
97ec558a 310 if (! spec->pcm_vol_node[dac_idx]) {
1da177e4 311 if (node->wid_caps & AC_WCAP_IN_AMP) {
97ec558a
TI
312 spec->pcm_vol_node[dac_idx] = node;
313 spec->pcm_vol_index[dac_idx] = i;
1da177e4 314 } else if (node->wid_caps & AC_WCAP_OUT_AMP) {
97ec558a
TI
315 spec->pcm_vol_node[dac_idx] = node;
316 spec->pcm_vol_index[dac_idx] = 0;
1da177e4
LT
317 }
318 }
319 return 1;
320 }
321 }
322 return 0;
323}
324
325/*
326 * Look for the output PIN widget with the given jack type
327 * and parse the output path to that PIN.
328 *
329 * Returns the PIN node when the path to DAC is established.
330 */
331static struct hda_gnode *parse_output_jack(struct hda_codec *codec,
332 struct hda_gspec *spec,
333 int jack_type)
334{
335 struct list_head *p;
336 struct hda_gnode *node;
337 int err;
338
339 list_for_each(p, &spec->nid_list) {
340 node = list_entry(p, struct hda_gnode, list);
341 if (node->type != AC_WID_PIN)
342 continue;
343 /* output capable? */
344 if (! (node->pin_caps & AC_PINCAP_OUT))
345 continue;
97ec558a
TI
346 if (defcfg_port_conn(node) == AC_JACK_PORT_NONE)
347 continue; /* unconnected */
1da177e4 348 if (jack_type >= 0) {
e9edcee0 349 if (jack_type != defcfg_type(node))
1da177e4
LT
350 continue;
351 if (node->wid_caps & AC_WCAP_DIGITAL)
352 continue; /* skip SPDIF */
353 } else {
354 /* output as default? */
355 if (! (node->pin_ctl & AC_PINCTL_OUT_EN))
356 continue;
357 }
358 clear_check_flags(spec);
97ec558a 359 err = parse_output_path(codec, spec, node, 0);
1da177e4
LT
360 if (err < 0)
361 return NULL;
97ec558a
TI
362 if (! err && spec->out_pin_node[0]) {
363 err = parse_output_path(codec, spec, node, 1);
364 if (err < 0)
365 return NULL;
366 }
367 if (err > 0) {
1da177e4
LT
368 /* unmute the PIN output */
369 unmute_output(codec, node);
370 /* set PIN-Out enable */
371 snd_hda_codec_write(codec, node->nid, 0,
372 AC_VERB_SET_PIN_WIDGET_CONTROL,
373 AC_PINCTL_OUT_EN | AC_PINCTL_HP_EN);
374 return node;
375 }
376 }
377 return NULL;
378}
379
380
381/*
382 * parse outputs
383 */
384static int parse_output(struct hda_codec *codec)
385{
386 struct hda_gspec *spec = codec->spec;
387 struct hda_gnode *node;
388
389 /*
390 * Look for the output PIN widget
391 */
392 /* first, look for the line-out pin */
393 node = parse_output_jack(codec, spec, AC_JACK_LINE_OUT);
394 if (node) /* found, remember the PIN node */
97ec558a
TI
395 spec->out_pin_node[0] = node;
396 else {
397 /* if no line-out is found, try speaker out */
398 node = parse_output_jack(codec, spec, AC_JACK_SPEAKER);
399 if (node)
400 spec->out_pin_node[0] = node;
401 }
1da177e4
LT
402 /* look for the HP-out pin */
403 node = parse_output_jack(codec, spec, AC_JACK_HP_OUT);
404 if (node) {
97ec558a
TI
405 if (! spec->out_pin_node[0])
406 spec->out_pin_node[0] = node;
407 else
408 spec->out_pin_node[1] = node;
1da177e4
LT
409 }
410
97ec558a 411 if (! spec->out_pin_node[0]) {
1da177e4
LT
412 /* no line-out or HP pins found,
413 * then choose for the first output pin
414 */
97ec558a
TI
415 spec->out_pin_node[0] = parse_output_jack(codec, spec, -1);
416 if (! spec->out_pin_node[0])
1da177e4
LT
417 snd_printd("hda_generic: no proper output path found\n");
418 }
419
420 return 0;
421}
422
423/*
424 * input MUX
425 */
426
427/* control callbacks */
c8b6bf9b 428static int capture_source_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1da177e4
LT
429{
430 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
431 struct hda_gspec *spec = codec->spec;
432 return snd_hda_input_mux_info(&spec->input_mux, uinfo);
433}
434
c8b6bf9b 435static int capture_source_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1da177e4
LT
436{
437 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
438 struct hda_gspec *spec = codec->spec;
439
440 ucontrol->value.enumerated.item[0] = spec->cur_cap_src;
441 return 0;
442}
443
c8b6bf9b 444static int capture_source_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1da177e4
LT
445{
446 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
447 struct hda_gspec *spec = codec->spec;
448 return snd_hda_input_mux_put(codec, &spec->input_mux, ucontrol,
449 spec->adc_node->nid, &spec->cur_cap_src);
450}
451
452/*
453 * return the string name of the given input PIN widget
454 */
455static const char *get_input_type(struct hda_gnode *node, unsigned int *pinctl)
456{
e9edcee0
TI
457 unsigned int location = defcfg_location(node);
458 switch (defcfg_type(node)) {
1da177e4
LT
459 case AC_JACK_LINE_IN:
460 if ((location & 0x0f) == AC_JACK_LOC_FRONT)
461 return "Front Line";
462 return "Line";
463 case AC_JACK_CD:
464 if (pinctl)
1a12de1e 465 *pinctl |= AC_PINCTL_VREF_GRD;
1da177e4
LT
466 return "CD";
467 case AC_JACK_AUX:
468 if ((location & 0x0f) == AC_JACK_LOC_FRONT)
469 return "Front Aux";
470 return "Aux";
471 case AC_JACK_MIC_IN:
472 if ((location & 0x0f) == AC_JACK_LOC_FRONT)
473 return "Front Mic";
474 return "Mic";
475 case AC_JACK_SPDIF_IN:
476 return "SPDIF";
477 case AC_JACK_DIG_OTHER_IN:
478 return "Digital";
479 }
480 return NULL;
481}
482
483/*
484 * parse the nodes recursively until reach to the input PIN
485 *
486 * returns 0 if not found, 1 if found, or a negative error code.
487 */
488static int parse_adc_sub_nodes(struct hda_codec *codec, struct hda_gspec *spec,
489 struct hda_gnode *node)
490{
491 int i, err;
492 unsigned int pinctl;
493 char *label;
494 const char *type;
495
496 if (node->checked)
497 return 0;
498
499 node->checked = 1;
500 if (node->type != AC_WID_PIN) {
501 for (i = 0; i < node->nconns; i++) {
502 struct hda_gnode *child;
503 child = hda_get_node(spec, node->conn_list[i]);
504 if (! child)
505 continue;
506 err = parse_adc_sub_nodes(codec, spec, child);
507 if (err < 0)
508 return err;
509 if (err > 0) {
510 /* found one,
511 * select the path, unmute both input and output
512 */
513 if (node->nconns > 1)
514 select_input_connection(codec, node, i);
515 unmute_input(codec, node, i);
516 unmute_output(codec, node);
517 return err;
518 }
519 }
520 return 0;
521 }
522
523 /* input capable? */
524 if (! (node->pin_caps & AC_PINCAP_IN))
525 return 0;
526
97ec558a
TI
527 if (defcfg_port_conn(node) == AC_JACK_PORT_NONE)
528 return 0; /* unconnected */
529
1da177e4
LT
530 if (node->wid_caps & AC_WCAP_DIGITAL)
531 return 0; /* skip SPDIF */
532
533 if (spec->input_mux.num_items >= HDA_MAX_NUM_INPUTS) {
534 snd_printk(KERN_ERR "hda_generic: Too many items for capture\n");
535 return -EINVAL;
536 }
537
538 pinctl = AC_PINCTL_IN_EN;
539 /* create a proper capture source label */
540 type = get_input_type(node, &pinctl);
541 if (! type) {
542 /* input as default? */
543 if (! (node->pin_ctl & AC_PINCTL_IN_EN))
544 return 0;
545 type = "Input";
546 }
547 label = spec->cap_labels[spec->input_mux.num_items];
548 strcpy(label, type);
549 spec->input_mux.items[spec->input_mux.num_items].label = label;
550
551 /* unmute the PIN external input */
552 unmute_input(codec, node, 0); /* index = 0? */
553 /* set PIN-In enable */
554 snd_hda_codec_write(codec, node->nid, 0, AC_VERB_SET_PIN_WIDGET_CONTROL, pinctl);
555
556 return 1; /* found */
557}
558
559/*
560 * parse input
561 */
562static int parse_input_path(struct hda_codec *codec, struct hda_gnode *adc_node)
563{
564 struct hda_gspec *spec = codec->spec;
565 struct hda_gnode *node;
566 int i, err;
567
568 snd_printdd("AUD_IN = %x\n", adc_node->nid);
569 clear_check_flags(spec);
570
571 // awk added - fixed no recording due to muted widget
572 unmute_input(codec, adc_node, 0);
573
574 /*
575 * check each connection of the ADC
576 * if it reaches to a proper input PIN, add the path as the
577 * input path.
578 */
579 for (i = 0; i < adc_node->nconns; i++) {
580 node = hda_get_node(spec, adc_node->conn_list[i]);
581 if (! node)
582 continue;
583 err = parse_adc_sub_nodes(codec, spec, node);
584 if (err < 0)
585 return err;
586 else if (err > 0) {
587 struct hda_input_mux_item *csrc = &spec->input_mux.items[spec->input_mux.num_items];
588 char *buf = spec->cap_labels[spec->input_mux.num_items];
589 int ocap;
590 for (ocap = 0; ocap < spec->input_mux.num_items; ocap++) {
591 if (! strcmp(buf, spec->cap_labels[ocap])) {
592 /* same label already exists,
593 * put the index number to be unique
594 */
595 sprintf(buf, "%s %d", spec->cap_labels[ocap],
596 spec->input_mux.num_items);
597 }
598 }
599 csrc->index = i;
600 spec->input_mux.num_items++;
601 }
602 }
603
604 if (! spec->input_mux.num_items)
605 return 0; /* no input path found... */
606
607 snd_printdd("[Capture Source] NID=0x%x, #SRC=%d\n", adc_node->nid, spec->input_mux.num_items);
608 for (i = 0; i < spec->input_mux.num_items; i++)
609 snd_printdd(" [%s] IDX=0x%x\n", spec->input_mux.items[i].label,
610 spec->input_mux.items[i].index);
611
612 spec->adc_node = adc_node;
613 return 1;
614}
615
616/*
617 * parse input
618 */
619static int parse_input(struct hda_codec *codec)
620{
621 struct hda_gspec *spec = codec->spec;
622 struct list_head *p;
623 struct hda_gnode *node;
624 int err;
625
626 /*
627 * At first we look for an audio input widget.
628 * If it reaches to certain input PINs, we take it as the
629 * input path.
630 */
631 list_for_each(p, &spec->nid_list) {
632 node = list_entry(p, struct hda_gnode, list);
633 if (node->wid_caps & AC_WCAP_DIGITAL)
634 continue; /* skip SPDIF */
635 if (node->type == AC_WID_AUD_IN) {
636 err = parse_input_path(codec, node);
637 if (err < 0)
638 return err;
639 else if (err > 0)
640 return 0;
641 }
642 }
643 snd_printd("hda_generic: no proper input path found\n");
644 return 0;
645}
646
647/*
648 * create mixer controls if possible
649 */
650#define DIR_OUT 0x1
651#define DIR_IN 0x2
652
653static int create_mixer(struct hda_codec *codec, struct hda_gnode *node,
654 unsigned int index, const char *type, const char *dir_sfx)
655{
656 char name[32];
657 int err;
658 int created = 0;
c8b6bf9b 659 struct snd_kcontrol_new knew;
1da177e4
LT
660
661 if (type)
662 sprintf(name, "%s %s Switch", type, dir_sfx);
663 else
664 sprintf(name, "%s Switch", dir_sfx);
665 if ((node->wid_caps & AC_WCAP_IN_AMP) &&
666 (node->amp_in_caps & AC_AMPCAP_MUTE)) {
c8b6bf9b 667 knew = (struct snd_kcontrol_new)HDA_CODEC_MUTE(name, node->nid, index, HDA_INPUT);
1da177e4
LT
668 snd_printdd("[%s] NID=0x%x, DIR=IN, IDX=0x%x\n", name, node->nid, index);
669 if ((err = snd_ctl_add(codec->bus->card, snd_ctl_new1(&knew, codec))) < 0)
670 return err;
671 created = 1;
672 } else if ((node->wid_caps & AC_WCAP_OUT_AMP) &&
673 (node->amp_out_caps & AC_AMPCAP_MUTE)) {
c8b6bf9b 674 knew = (struct snd_kcontrol_new)HDA_CODEC_MUTE(name, node->nid, 0, HDA_OUTPUT);
1da177e4
LT
675 snd_printdd("[%s] NID=0x%x, DIR=OUT\n", name, node->nid);
676 if ((err = snd_ctl_add(codec->bus->card, snd_ctl_new1(&knew, codec))) < 0)
677 return err;
678 created = 1;
679 }
680
681 if (type)
682 sprintf(name, "%s %s Volume", type, dir_sfx);
683 else
684 sprintf(name, "%s Volume", dir_sfx);
685 if ((node->wid_caps & AC_WCAP_IN_AMP) &&
686 (node->amp_in_caps & AC_AMPCAP_NUM_STEPS)) {
c8b6bf9b 687 knew = (struct snd_kcontrol_new)HDA_CODEC_VOLUME(name, node->nid, index, HDA_INPUT);
1da177e4
LT
688 snd_printdd("[%s] NID=0x%x, DIR=IN, IDX=0x%x\n", name, node->nid, index);
689 if ((err = snd_ctl_add(codec->bus->card, snd_ctl_new1(&knew, codec))) < 0)
690 return err;
691 created = 1;
692 } else if ((node->wid_caps & AC_WCAP_OUT_AMP) &&
693 (node->amp_out_caps & AC_AMPCAP_NUM_STEPS)) {
c8b6bf9b 694 knew = (struct snd_kcontrol_new)HDA_CODEC_VOLUME(name, node->nid, 0, HDA_OUTPUT);
1da177e4
LT
695 snd_printdd("[%s] NID=0x%x, DIR=OUT\n", name, node->nid);
696 if ((err = snd_ctl_add(codec->bus->card, snd_ctl_new1(&knew, codec))) < 0)
697 return err;
698 created = 1;
699 }
700
701 return created;
702}
703
704/*
705 * check whether the controls with the given name and direction suffix already exist
706 */
707static int check_existing_control(struct hda_codec *codec, const char *type, const char *dir)
708{
c8b6bf9b 709 struct snd_ctl_elem_id id;
1da177e4
LT
710 memset(&id, 0, sizeof(id));
711 sprintf(id.name, "%s %s Volume", type, dir);
712 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
713 if (snd_ctl_find_id(codec->bus->card, &id))
714 return 1;
715 sprintf(id.name, "%s %s Switch", type, dir);
716 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
717 if (snd_ctl_find_id(codec->bus->card, &id))
718 return 1;
719 return 0;
720}
721
722/*
723 * build output mixer controls
724 */
725static int build_output_controls(struct hda_codec *codec)
726{
727 struct hda_gspec *spec = codec->spec;
97ec558a
TI
728 static const char *types[2] = { "Master", "Headphone" };
729 int i, err;
1da177e4 730
97ec558a
TI
731 for (i = 0; i < 2 && spec->pcm_vol_node[i]; i++) {
732 err = create_mixer(codec, spec->pcm_vol_node[i],
733 spec->pcm_vol_index[i],
734 types[i], "Playback");
735 if (err < 0)
736 return err;
737 }
1da177e4
LT
738 return 0;
739}
740
741/* create capture volume/switch */
742static int build_input_controls(struct hda_codec *codec)
743{
744 struct hda_gspec *spec = codec->spec;
745 struct hda_gnode *adc_node = spec->adc_node;
746 int err;
747
748 if (! adc_node)
749 return 0; /* not found */
750
751 /* create capture volume and switch controls if the ADC has an amp */
752 err = create_mixer(codec, adc_node, 0, NULL, "Capture");
753
754 /* create input MUX if multiple sources are available */
755 if (spec->input_mux.num_items > 1) {
c8b6bf9b 756 static struct snd_kcontrol_new cap_sel = {
1da177e4
LT
757 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
758 .name = "Capture Source",
759 .info = capture_source_info,
760 .get = capture_source_get,
761 .put = capture_source_put,
762 };
763 if ((err = snd_ctl_add(codec->bus->card, snd_ctl_new1(&cap_sel, codec))) < 0)
764 return err;
765 spec->cur_cap_src = 0;
766 select_input_connection(codec, adc_node, spec->input_mux.items[0].index);
767 }
768 return 0;
769}
770
771
772/*
773 * parse the nodes recursively until reach to the output PIN.
774 *
775 * returns 0 - if not found,
776 * 1 - if found, but no mixer is created
777 * 2 - if found and mixer was already created, (just skip)
778 * a negative error code
779 */
780static int parse_loopback_path(struct hda_codec *codec, struct hda_gspec *spec,
781 struct hda_gnode *node, struct hda_gnode *dest_node,
782 const char *type)
783{
784 int i, err;
785
786 if (node->checked)
787 return 0;
788
789 node->checked = 1;
790 if (node == dest_node) {
791 /* loopback connection found */
792 return 1;
793 }
794
795 for (i = 0; i < node->nconns; i++) {
796 struct hda_gnode *child = hda_get_node(spec, node->conn_list[i]);
797 if (! child)
798 continue;
799 err = parse_loopback_path(codec, spec, child, dest_node, type);
800 if (err < 0)
801 return err;
802 else if (err >= 1) {
803 if (err == 1) {
804 err = create_mixer(codec, node, i, type, "Playback");
805 if (err < 0)
806 return err;
807 if (err > 0)
808 return 2; /* ok, created */
809 /* not created, maybe in the lower path */
810 err = 1;
811 }
812 /* connect and unmute */
813 if (node->nconns > 1)
814 select_input_connection(codec, node, i);
815 unmute_input(codec, node, i);
816 unmute_output(codec, node);
817 return err;
818 }
819 }
820 return 0;
821}
822
823/*
824 * parse the tree and build the loopback controls
825 */
826static int build_loopback_controls(struct hda_codec *codec)
827{
828 struct hda_gspec *spec = codec->spec;
829 struct list_head *p;
830 struct hda_gnode *node;
831 int err;
832 const char *type;
833
97ec558a 834 if (! spec->out_pin_node[0])
1da177e4
LT
835 return 0;
836
837 list_for_each(p, &spec->nid_list) {
838 node = list_entry(p, struct hda_gnode, list);
839 if (node->type != AC_WID_PIN)
840 continue;
841 /* input capable? */
842 if (! (node->pin_caps & AC_PINCAP_IN))
843 return 0;
844 type = get_input_type(node, NULL);
845 if (type) {
846 if (check_existing_control(codec, type, "Playback"))
847 continue;
848 clear_check_flags(spec);
97ec558a
TI
849 err = parse_loopback_path(codec, spec,
850 spec->out_pin_node[0],
1da177e4
LT
851 node, type);
852 if (err < 0)
853 return err;
854 if (! err)
855 continue;
856 }
857 }
858 return 0;
859}
860
861/*
862 * build mixer controls
863 */
864static int build_generic_controls(struct hda_codec *codec)
865{
866 int err;
867
868 if ((err = build_input_controls(codec)) < 0 ||
869 (err = build_output_controls(codec)) < 0 ||
870 (err = build_loopback_controls(codec)) < 0)
871 return err;
872
873 return 0;
874}
875
876/*
877 * PCM
878 */
879static struct hda_pcm_stream generic_pcm_playback = {
880 .substreams = 1,
881 .channels_min = 2,
882 .channels_max = 2,
883};
884
97ec558a
TI
885static int generic_pcm2_prepare(struct hda_pcm_stream *hinfo,
886 struct hda_codec *codec,
887 unsigned int stream_tag,
888 unsigned int format,
889 struct snd_pcm_substream *substream)
890{
891 struct hda_gspec *spec = codec->spec;
892
893 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
894 snd_hda_codec_setup_stream(codec, spec->dac_node[1]->nid,
895 stream_tag, 0, format);
896 return 0;
897}
898
899static int generic_pcm2_cleanup(struct hda_pcm_stream *hinfo,
900 struct hda_codec *codec,
901 struct snd_pcm_substream *substream)
902{
903 struct hda_gspec *spec = codec->spec;
904
905 snd_hda_codec_setup_stream(codec, hinfo->nid, 0, 0, 0);
906 snd_hda_codec_setup_stream(codec, spec->dac_node[1]->nid, 0, 0, 0);
907 return 0;
908}
909
1da177e4
LT
910static int build_generic_pcms(struct hda_codec *codec)
911{
912 struct hda_gspec *spec = codec->spec;
913 struct hda_pcm *info = &spec->pcm_rec;
914
97ec558a 915 if (! spec->dac_node[0] && ! spec->adc_node) {
1da177e4
LT
916 snd_printd("hda_generic: no PCM found\n");
917 return 0;
918 }
919
920 codec->num_pcms = 1;
921 codec->pcm_info = info;
922
923 info->name = "HDA Generic";
97ec558a 924 if (spec->dac_node[0]) {
1da177e4 925 info->stream[0] = generic_pcm_playback;
97ec558a
TI
926 info->stream[0].nid = spec->dac_node[0]->nid;
927 if (spec->dac_node[1]) {
928 info->stream[0].ops.prepare = generic_pcm2_prepare;
929 info->stream[0].ops.cleanup = generic_pcm2_cleanup;
930 }
1da177e4
LT
931 }
932 if (spec->adc_node) {
933 info->stream[1] = generic_pcm_playback;
934 info->stream[1].nid = spec->adc_node->nid;
935 }
936
937 return 0;
938}
939
940
941/*
942 */
943static struct hda_codec_ops generic_patch_ops = {
944 .build_controls = build_generic_controls,
945 .build_pcms = build_generic_pcms,
946 .free = snd_hda_generic_free,
947};
948
949/*
950 * the generic parser
951 */
952int snd_hda_parse_generic_codec(struct hda_codec *codec)
953{
954 struct hda_gspec *spec;
955 int err;
956
84802f0d
SK
957 if(!codec->afg)
958 return 0;
673b683a 959
e560d8d8 960 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
1da177e4
LT
961 if (spec == NULL) {
962 printk(KERN_ERR "hda_generic: can't allocate spec\n");
963 return -ENOMEM;
964 }
965 codec->spec = spec;
966 INIT_LIST_HEAD(&spec->nid_list);
967
968 if ((err = build_afg_tree(codec)) < 0)
969 goto error;
970
971 if ((err = parse_input(codec)) < 0 ||
972 (err = parse_output(codec)) < 0)
973 goto error;
974
975 codec->patch_ops = generic_patch_ops;
976
977 return 0;
978
979 error:
980 snd_hda_generic_free(codec);
981 return err;
982}