]> git.proxmox.com Git - mirror_ubuntu-focal-kernel.git/blame - sound/pci/hda/hda_generic.c
[ALSA] hda-codec - optimize resume using caches
[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>
1da177e4
LT
26#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 */
d2569505
TI
34 hda_nid_t *conn_list;
35 hda_nid_t slist[2]; /* temporay list */
1da177e4
LT
36 unsigned int wid_caps; /* widget capabilities */
37 unsigned char type; /* widget type */
38 unsigned char pin_ctl; /* pin controls */
39 unsigned char checked; /* the flag indicates that the node is already parsed */
40 unsigned int pin_caps; /* pin widget capabilities */
41 unsigned int def_cfg; /* default configuration */
42 unsigned int amp_out_caps; /* AMP out capabilities */
43 unsigned int amp_in_caps; /* AMP in capabilities */
44 struct list_head list;
45};
46
c3132925 47/* patch-specific record */
a7da6ce5
TI
48
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
1da177e4 55struct hda_gspec {
97ec558a
TI
56 struct hda_gnode *dac_node[2]; /* DAC node */
57 struct hda_gnode *out_pin_node[2]; /* Output pin (Line-Out) node */
a7da6ce5
TI
58 struct pcm_vol pcm_vol[MAX_PCM_VOLS]; /* PCM volumes */
59 unsigned int pcm_vol_nodes; /* number of PCM volumes */
1da177e4
LT
60
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 */
97ec558a
TI
78#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)
1da177e4
LT
84
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);
d2569505
TI
98 if (node->conn_list != node->slist)
99 kfree(node->conn_list);
1da177e4
LT
100 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;
d2569505 113 hda_nid_t conn_list[HDA_MAX_CONNECTIONS];
1da177e4 114
e560d8d8 115 node = kzalloc(sizeof(*node), GFP_KERNEL);
1da177e4
LT
116 if (node == NULL)
117 return -ENOMEM;
118 node->nid = nid;
d2569505
TI
119 nconns = snd_hda_get_connections(codec, nid, conn_list,
120 HDA_MAX_CONNECTIONS);
1da177e4
LT
121 if (nconns < 0) {
122 kfree(node);
123 return nconns;
124 }
d2569505
TI
125 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 }
0bbed758 136 memcpy(node->conn_list, conn_list, nconns * sizeof(hda_nid_t));
1da177e4 137 node->nconns = nconns;
d2569505 138 node->wid_caps = get_wcaps(codec, nid);
1da177e4
LT
139 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;
82beb8fd
TI
221 snd_hda_codec_amp_update(codec, node->nid, 0, HDA_OUTPUT, 0, 0xff, val);
222 snd_hda_codec_amp_update(codec, node->nid, 0, HDA_OUTPUT, 1, 0xff, val);
223 return 0;
1da177e4
LT
224}
225
226/*
227 * unmute (and set max vol) the input amplifier
228 */
229static int unmute_input(struct hda_codec *codec, struct hda_gnode *node, unsigned int index)
230{
231 unsigned int val, ofs;
232 snd_printdd("UNMUTE IN: NID=0x%x IDX=0x%x\n", node->nid, index);
233 val = (node->amp_in_caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
234 ofs = (node->amp_in_caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT;
235 if (val >= ofs)
236 val -= ofs;
82beb8fd
TI
237 snd_hda_codec_amp_update(codec, node->nid, 0, HDA_INPUT, index,
238 0xff, val);
239 snd_hda_codec_amp_update(codec, node->nid, 1, HDA_INPUT, index,
240 0xff, val);
241 return 0;
1da177e4
LT
242}
243
244/*
245 * select the input connection of the given node.
246 */
247static int select_input_connection(struct hda_codec *codec, struct hda_gnode *node,
248 unsigned int index)
249{
250 snd_printdd("CONNECT: NID=0x%x IDX=0x%x\n", node->nid, index);
82beb8fd
TI
251 return snd_hda_codec_write_cache(codec, node->nid, 0,
252 AC_VERB_SET_CONNECT_SEL, index);
1da177e4
LT
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,
97ec558a 275 struct hda_gnode *node, int dac_idx)
1da177e4
LT
276{
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);
97ec558a 290 if (spec->dac_node[dac_idx]) {
1da177e4 291 /* already DAC node is assigned, just unmute & connect */
97ec558a 292 return node == spec->dac_node[dac_idx];
1da177e4 293 }
97ec558a 294 spec->dac_node[dac_idx] = node;
a7da6ce5
TI
295 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++;
1da177e4
LT
300 }
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;
97ec558a 308 err = parse_output_path(codec, spec, child, dac_idx);
1da177e4
LT
309 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);
a7da6ce5
TI
319 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++;
1da177e4
LT
329 }
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;
97ec558a
TI
358 if (defcfg_port_conn(node) == AC_JACK_PORT_NONE)
359 continue; /* unconnected */
1da177e4 360 if (jack_type >= 0) {
e9edcee0 361 if (jack_type != defcfg_type(node))
1da177e4
LT
362 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);
97ec558a 371 err = parse_output_path(codec, spec, node, 0);
1da177e4
LT
372 if (err < 0)
373 return NULL;
97ec558a
TI
374 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) {
1da177e4
LT
380 /* unmute the PIN output */
381 unmute_output(codec, node);
382 /* set PIN-Out enable */
82beb8fd 383 snd_hda_codec_write_cache(codec, node->nid, 0,
1da177e4 384 AC_VERB_SET_PIN_WIDGET_CONTROL,
a7da6ce5
TI
385 AC_PINCTL_OUT_EN |
386 ((node->pin_caps & AC_PINCAP_HP_DRV) ?
387 AC_PINCTL_HP_EN : 0));
1da177e4
LT
388 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 */
97ec558a
TI
409 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 }
1da177e4
LT
416 /* look for the HP-out pin */
417 node = parse_output_jack(codec, spec, AC_JACK_HP_OUT);
418 if (node) {
97ec558a
TI
419 if (! spec->out_pin_node[0])
420 spec->out_pin_node[0] = node;
421 else
422 spec->out_pin_node[1] = node;
1da177e4
LT
423 }
424
97ec558a 425 if (! spec->out_pin_node[0]) {
1da177e4
LT
426 /* no line-out or HP pins found,
427 * then choose for the first output pin
428 */
97ec558a
TI
429 spec->out_pin_node[0] = parse_output_jack(codec, spec, -1);
430 if (! spec->out_pin_node[0])
1da177e4
LT
431 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 */
c8b6bf9b 442static int capture_source_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1da177e4
LT
443{
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
c8b6bf9b 449static int capture_source_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1da177e4
LT
450{
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
c8b6bf9b 458static int capture_source_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1da177e4
LT
459{
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{
e9edcee0
TI
471 unsigned int location = defcfg_location(node);
472 switch (defcfg_type(node)) {
1da177e4
LT
473 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:
071c73ad 478#if 0
1da177e4 479 if (pinctl)
1a12de1e 480 *pinctl |= AC_PINCTL_VREF_GRD;
071c73ad 481#endif
1da177e4
LT
482 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:
6afeb11d
TI
488 if (pinctl &&
489 (node->pin_caps &
490 (AC_PINCAP_VREF_80 << AC_PINCAP_VREF_SHIFT)))
071c73ad 491 *pinctl |= AC_PINCTL_VREF_80;
1da177e4
LT
492 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
97ec558a
TI
547 if (defcfg_port_conn(node) == AC_JACK_PORT_NONE)
548 return 0; /* unconnected */
549
1da177e4
LT
550 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 */
82beb8fd
TI
574 snd_hda_codec_write_cache(codec, node->nid, 0,
575 AC_VERB_SET_PIN_WIDGET_CONTROL, pinctl);
1da177e4
LT
576
577 return 1; /* found */
578}
579
071c73ad
TI
580/* add a capture source element */
581static void add_cap_src(struct hda_gspec *spec, int idx)
582{
583 struct hda_input_mux_item *csrc;
584 char *buf;
585 int num, ocap;
586
587 num = spec->input_mux.num_items;
588 csrc = &spec->input_mux.items[num];
589 buf = spec->cap_labels[num];
590 for (ocap = 0; ocap < num; 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], num);
596 break;
597 }
598 }
599 csrc->index = idx;
600 spec->input_mux.num_items++;
601}
602
1da177e4
LT
603/*
604 * parse input
605 */
606static int parse_input_path(struct hda_codec *codec, struct hda_gnode *adc_node)
607{
608 struct hda_gspec *spec = codec->spec;
609 struct hda_gnode *node;
610 int i, err;
611
612 snd_printdd("AUD_IN = %x\n", adc_node->nid);
613 clear_check_flags(spec);
614
615 // awk added - fixed no recording due to muted widget
616 unmute_input(codec, adc_node, 0);
617
618 /*
619 * check each connection of the ADC
620 * if it reaches to a proper input PIN, add the path as the
621 * input path.
622 */
071c73ad 623 /* first, check the direct connections to PIN widgets */
1da177e4
LT
624 for (i = 0; i < adc_node->nconns; i++) {
625 node = hda_get_node(spec, adc_node->conn_list[i]);
071c73ad
TI
626 if (node && node->type == AC_WID_PIN) {
627 err = parse_adc_sub_nodes(codec, spec, node);
628 if (err < 0)
629 return err;
630 else if (err > 0)
631 add_cap_src(spec, i);
632 }
633 }
634 /* ... then check the rests, more complicated connections */
635 for (i = 0; i < adc_node->nconns; i++) {
636 node = hda_get_node(spec, adc_node->conn_list[i]);
637 if (node && node->type != AC_WID_PIN) {
638 err = parse_adc_sub_nodes(codec, spec, node);
639 if (err < 0)
640 return err;
641 else if (err > 0)
642 add_cap_src(spec, i);
1da177e4
LT
643 }
644 }
645
646 if (! spec->input_mux.num_items)
647 return 0; /* no input path found... */
648
649 snd_printdd("[Capture Source] NID=0x%x, #SRC=%d\n", adc_node->nid, spec->input_mux.num_items);
650 for (i = 0; i < spec->input_mux.num_items; i++)
651 snd_printdd(" [%s] IDX=0x%x\n", spec->input_mux.items[i].label,
652 spec->input_mux.items[i].index);
653
654 spec->adc_node = adc_node;
655 return 1;
656}
657
658/*
659 * parse input
660 */
661static int parse_input(struct hda_codec *codec)
662{
663 struct hda_gspec *spec = codec->spec;
664 struct list_head *p;
665 struct hda_gnode *node;
666 int err;
667
668 /*
669 * At first we look for an audio input widget.
670 * If it reaches to certain input PINs, we take it as the
671 * input path.
672 */
673 list_for_each(p, &spec->nid_list) {
674 node = list_entry(p, struct hda_gnode, list);
675 if (node->wid_caps & AC_WCAP_DIGITAL)
676 continue; /* skip SPDIF */
677 if (node->type == AC_WID_AUD_IN) {
678 err = parse_input_path(codec, node);
679 if (err < 0)
680 return err;
681 else if (err > 0)
682 return 0;
683 }
684 }
685 snd_printd("hda_generic: no proper input path found\n");
686 return 0;
687}
688
689/*
690 * create mixer controls if possible
691 */
1da177e4
LT
692static int create_mixer(struct hda_codec *codec, struct hda_gnode *node,
693 unsigned int index, const char *type, const char *dir_sfx)
694{
695 char name[32];
696 int err;
697 int created = 0;
c8b6bf9b 698 struct snd_kcontrol_new knew;
1da177e4
LT
699
700 if (type)
701 sprintf(name, "%s %s Switch", type, dir_sfx);
702 else
703 sprintf(name, "%s Switch", dir_sfx);
704 if ((node->wid_caps & AC_WCAP_IN_AMP) &&
705 (node->amp_in_caps & AC_AMPCAP_MUTE)) {
c8b6bf9b 706 knew = (struct snd_kcontrol_new)HDA_CODEC_MUTE(name, node->nid, index, HDA_INPUT);
1da177e4
LT
707 snd_printdd("[%s] NID=0x%x, DIR=IN, IDX=0x%x\n", name, node->nid, index);
708 if ((err = snd_ctl_add(codec->bus->card, snd_ctl_new1(&knew, codec))) < 0)
709 return err;
710 created = 1;
711 } else if ((node->wid_caps & AC_WCAP_OUT_AMP) &&
712 (node->amp_out_caps & AC_AMPCAP_MUTE)) {
c8b6bf9b 713 knew = (struct snd_kcontrol_new)HDA_CODEC_MUTE(name, node->nid, 0, HDA_OUTPUT);
1da177e4
LT
714 snd_printdd("[%s] NID=0x%x, DIR=OUT\n", name, node->nid);
715 if ((err = snd_ctl_add(codec->bus->card, snd_ctl_new1(&knew, codec))) < 0)
716 return err;
717 created = 1;
718 }
719
720 if (type)
721 sprintf(name, "%s %s Volume", type, dir_sfx);
722 else
723 sprintf(name, "%s Volume", dir_sfx);
724 if ((node->wid_caps & AC_WCAP_IN_AMP) &&
725 (node->amp_in_caps & AC_AMPCAP_NUM_STEPS)) {
c8b6bf9b 726 knew = (struct snd_kcontrol_new)HDA_CODEC_VOLUME(name, node->nid, index, HDA_INPUT);
1da177e4
LT
727 snd_printdd("[%s] NID=0x%x, DIR=IN, IDX=0x%x\n", name, node->nid, index);
728 if ((err = snd_ctl_add(codec->bus->card, snd_ctl_new1(&knew, codec))) < 0)
729 return err;
730 created = 1;
731 } else if ((node->wid_caps & AC_WCAP_OUT_AMP) &&
732 (node->amp_out_caps & AC_AMPCAP_NUM_STEPS)) {
c8b6bf9b 733 knew = (struct snd_kcontrol_new)HDA_CODEC_VOLUME(name, node->nid, 0, HDA_OUTPUT);
1da177e4
LT
734 snd_printdd("[%s] NID=0x%x, DIR=OUT\n", name, node->nid);
735 if ((err = snd_ctl_add(codec->bus->card, snd_ctl_new1(&knew, codec))) < 0)
736 return err;
737 created = 1;
738 }
739
740 return created;
741}
742
743/*
744 * check whether the controls with the given name and direction suffix already exist
745 */
746static int check_existing_control(struct hda_codec *codec, const char *type, const char *dir)
747{
c8b6bf9b 748 struct snd_ctl_elem_id id;
1da177e4
LT
749 memset(&id, 0, sizeof(id));
750 sprintf(id.name, "%s %s Volume", type, dir);
751 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
752 if (snd_ctl_find_id(codec->bus->card, &id))
753 return 1;
754 sprintf(id.name, "%s %s Switch", type, dir);
755 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
756 if (snd_ctl_find_id(codec->bus->card, &id))
757 return 1;
758 return 0;
759}
760
761/*
762 * build output mixer controls
763 */
a7da6ce5 764static int create_output_mixers(struct hda_codec *codec, const char **names)
1da177e4
LT
765{
766 struct hda_gspec *spec = codec->spec;
97ec558a 767 int i, err;
1da177e4 768
a7da6ce5
TI
769 for (i = 0; i < spec->pcm_vol_nodes; i++) {
770 err = create_mixer(codec, spec->pcm_vol[i].node,
771 spec->pcm_vol[i].index,
772 names[i], "Playback");
97ec558a
TI
773 if (err < 0)
774 return err;
775 }
1da177e4
LT
776 return 0;
777}
778
a7da6ce5
TI
779static int build_output_controls(struct hda_codec *codec)
780{
781 struct hda_gspec *spec = codec->spec;
782 static const char *types_speaker[] = { "Speaker", "Headphone" };
783 static const char *types_line[] = { "Front", "Headphone" };
784
785 switch (spec->pcm_vol_nodes) {
786 case 1:
787 return create_mixer(codec, spec->pcm_vol[0].node,
788 spec->pcm_vol[0].index,
789 "Master", "Playback");
790 case 2:
791 if (defcfg_type(spec->out_pin_node[0]) == AC_JACK_SPEAKER)
792 return create_output_mixers(codec, types_speaker);
793 else
794 return create_output_mixers(codec, types_line);
795 }
796 return 0;
797}
798
1da177e4
LT
799/* create capture volume/switch */
800static int build_input_controls(struct hda_codec *codec)
801{
802 struct hda_gspec *spec = codec->spec;
803 struct hda_gnode *adc_node = spec->adc_node;
071c73ad
TI
804 int i, err;
805 static struct snd_kcontrol_new cap_sel = {
806 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
807 .name = "Capture Source",
808 .info = capture_source_info,
809 .get = capture_source_get,
810 .put = capture_source_put,
811 };
812
813 if (! adc_node || ! spec->input_mux.num_items)
1da177e4
LT
814 return 0; /* not found */
815
071c73ad
TI
816 spec->cur_cap_src = 0;
817 select_input_connection(codec, adc_node,
818 spec->input_mux.items[0].index);
819
1da177e4 820 /* create capture volume and switch controls if the ADC has an amp */
071c73ad
TI
821 /* do we have only a single item? */
822 if (spec->input_mux.num_items == 1) {
823 err = create_mixer(codec, adc_node,
824 spec->input_mux.items[0].index,
825 NULL, "Capture");
826 if (err < 0)
827 return err;
828 return 0;
829 }
1da177e4
LT
830
831 /* create input MUX if multiple sources are available */
071c73ad
TI
832 if ((err = snd_ctl_add(codec->bus->card,
833 snd_ctl_new1(&cap_sel, codec))) < 0)
834 return err;
835
836 /* no volume control? */
837 if (! (adc_node->wid_caps & AC_WCAP_IN_AMP) ||
838 ! (adc_node->amp_in_caps & AC_AMPCAP_NUM_STEPS))
839 return 0;
840
841 for (i = 0; i < spec->input_mux.num_items; i++) {
842 struct snd_kcontrol_new knew;
843 char name[32];
844 sprintf(name, "%s Capture Volume",
845 spec->input_mux.items[i].label);
846 knew = (struct snd_kcontrol_new)
847 HDA_CODEC_VOLUME(name, adc_node->nid,
848 spec->input_mux.items[i].index,
849 HDA_INPUT);
850 if ((err = snd_ctl_add(codec->bus->card,
851 snd_ctl_new1(&knew, codec))) < 0)
1da177e4 852 return err;
1da177e4 853 }
071c73ad 854
1da177e4
LT
855 return 0;
856}
857
858
859/*
860 * parse the nodes recursively until reach to the output PIN.
861 *
862 * returns 0 - if not found,
863 * 1 - if found, but no mixer is created
864 * 2 - if found and mixer was already created, (just skip)
865 * a negative error code
866 */
867static int parse_loopback_path(struct hda_codec *codec, struct hda_gspec *spec,
868 struct hda_gnode *node, struct hda_gnode *dest_node,
869 const char *type)
870{
871 int i, err;
872
873 if (node->checked)
874 return 0;
875
876 node->checked = 1;
877 if (node == dest_node) {
878 /* loopback connection found */
879 return 1;
880 }
881
882 for (i = 0; i < node->nconns; i++) {
883 struct hda_gnode *child = hda_get_node(spec, node->conn_list[i]);
884 if (! child)
885 continue;
886 err = parse_loopback_path(codec, spec, child, dest_node, type);
887 if (err < 0)
888 return err;
889 else if (err >= 1) {
890 if (err == 1) {
891 err = create_mixer(codec, node, i, type, "Playback");
892 if (err < 0)
893 return err;
894 if (err > 0)
895 return 2; /* ok, created */
896 /* not created, maybe in the lower path */
897 err = 1;
898 }
899 /* connect and unmute */
900 if (node->nconns > 1)
901 select_input_connection(codec, node, i);
902 unmute_input(codec, node, i);
903 unmute_output(codec, node);
904 return err;
905 }
906 }
907 return 0;
908}
909
910/*
911 * parse the tree and build the loopback controls
912 */
913static int build_loopback_controls(struct hda_codec *codec)
914{
915 struct hda_gspec *spec = codec->spec;
916 struct list_head *p;
917 struct hda_gnode *node;
918 int err;
919 const char *type;
920
97ec558a 921 if (! spec->out_pin_node[0])
1da177e4
LT
922 return 0;
923
924 list_for_each(p, &spec->nid_list) {
925 node = list_entry(p, struct hda_gnode, list);
926 if (node->type != AC_WID_PIN)
927 continue;
928 /* input capable? */
929 if (! (node->pin_caps & AC_PINCAP_IN))
930 return 0;
931 type = get_input_type(node, NULL);
932 if (type) {
933 if (check_existing_control(codec, type, "Playback"))
934 continue;
935 clear_check_flags(spec);
97ec558a
TI
936 err = parse_loopback_path(codec, spec,
937 spec->out_pin_node[0],
1da177e4
LT
938 node, type);
939 if (err < 0)
940 return err;
941 if (! err)
942 continue;
943 }
944 }
945 return 0;
946}
947
948/*
949 * build mixer controls
950 */
951static int build_generic_controls(struct hda_codec *codec)
952{
953 int err;
954
955 if ((err = build_input_controls(codec)) < 0 ||
956 (err = build_output_controls(codec)) < 0 ||
957 (err = build_loopback_controls(codec)) < 0)
958 return err;
959
960 return 0;
961}
962
963/*
964 * PCM
965 */
966static struct hda_pcm_stream generic_pcm_playback = {
967 .substreams = 1,
968 .channels_min = 2,
969 .channels_max = 2,
970};
971
97ec558a
TI
972static int generic_pcm2_prepare(struct hda_pcm_stream *hinfo,
973 struct hda_codec *codec,
974 unsigned int stream_tag,
975 unsigned int format,
976 struct snd_pcm_substream *substream)
977{
978 struct hda_gspec *spec = codec->spec;
979
980 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
981 snd_hda_codec_setup_stream(codec, spec->dac_node[1]->nid,
982 stream_tag, 0, format);
983 return 0;
984}
985
986static int generic_pcm2_cleanup(struct hda_pcm_stream *hinfo,
987 struct hda_codec *codec,
988 struct snd_pcm_substream *substream)
989{
990 struct hda_gspec *spec = codec->spec;
991
992 snd_hda_codec_setup_stream(codec, hinfo->nid, 0, 0, 0);
993 snd_hda_codec_setup_stream(codec, spec->dac_node[1]->nid, 0, 0, 0);
994 return 0;
995}
996
1da177e4
LT
997static int build_generic_pcms(struct hda_codec *codec)
998{
999 struct hda_gspec *spec = codec->spec;
1000 struct hda_pcm *info = &spec->pcm_rec;
1001
97ec558a 1002 if (! spec->dac_node[0] && ! spec->adc_node) {
1da177e4
LT
1003 snd_printd("hda_generic: no PCM found\n");
1004 return 0;
1005 }
1006
1007 codec->num_pcms = 1;
1008 codec->pcm_info = info;
1009
1010 info->name = "HDA Generic";
97ec558a 1011 if (spec->dac_node[0]) {
1da177e4 1012 info->stream[0] = generic_pcm_playback;
97ec558a
TI
1013 info->stream[0].nid = spec->dac_node[0]->nid;
1014 if (spec->dac_node[1]) {
1015 info->stream[0].ops.prepare = generic_pcm2_prepare;
1016 info->stream[0].ops.cleanup = generic_pcm2_cleanup;
1017 }
1da177e4
LT
1018 }
1019 if (spec->adc_node) {
1020 info->stream[1] = generic_pcm_playback;
1021 info->stream[1].nid = spec->adc_node->nid;
1022 }
1023
1024 return 0;
1025}
1026
1027
1028/*
1029 */
1030static struct hda_codec_ops generic_patch_ops = {
1031 .build_controls = build_generic_controls,
1032 .build_pcms = build_generic_pcms,
1033 .free = snd_hda_generic_free,
1034};
1035
1036/*
1037 * the generic parser
1038 */
1039int snd_hda_parse_generic_codec(struct hda_codec *codec)
1040{
1041 struct hda_gspec *spec;
1042 int err;
1043
84802f0d
SK
1044 if(!codec->afg)
1045 return 0;
673b683a 1046
e560d8d8 1047 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
1da177e4
LT
1048 if (spec == NULL) {
1049 printk(KERN_ERR "hda_generic: can't allocate spec\n");
1050 return -ENOMEM;
1051 }
1052 codec->spec = spec;
1053 INIT_LIST_HEAD(&spec->nid_list);
1054
1055 if ((err = build_afg_tree(codec)) < 0)
1056 goto error;
1057
1058 if ((err = parse_input(codec)) < 0 ||
1059 (err = parse_output(codec)) < 0)
1060 goto error;
1061
1062 codec->patch_ops = generic_patch_ops;
1063
1064 return 0;
1065
1066 error:
1067 snd_hda_generic_free(codec);
1068 return err;
1069}