]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - sound/pci/hda/hda_codec.c
ALSA: hda - Fix / clean-up slave digital out codes
[linux-2.6-omap-h63xx.git] / sound / pci / hda / hda_codec.c
1 /*
2  * Universal Interface for Intel High Definition Audio Codec
3  *
4  * Copyright (c) 2004 Takashi Iwai <tiwai@suse.de>
5  *
6  *
7  *  This driver is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation; either version 2 of the License, or
10  *  (at your option) any later version.
11  *
12  *  This driver is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with this program; if not, write to the Free Software
19  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
20  */
21
22 #include <linux/init.h>
23 #include <linux/delay.h>
24 #include <linux/slab.h>
25 #include <linux/pci.h>
26 #include <linux/mutex.h>
27 #include <sound/core.h>
28 #include "hda_codec.h"
29 #include <sound/asoundef.h>
30 #include <sound/tlv.h>
31 #include <sound/initval.h>
32 #include "hda_local.h"
33 #include <sound/hda_hwdep.h>
34 #include "hda_patch.h"  /* codec presets */
35
36 #ifdef CONFIG_SND_HDA_POWER_SAVE
37 /* define this option here to hide as static */
38 static int power_save = CONFIG_SND_HDA_POWER_SAVE_DEFAULT;
39 module_param(power_save, int, 0644);
40 MODULE_PARM_DESC(power_save, "Automatic power-saving timeout "
41                  "(in second, 0 = disable).");
42 #endif
43
44 /*
45  * vendor / preset table
46  */
47
48 struct hda_vendor_id {
49         unsigned int id;
50         const char *name;
51 };
52
53 /* codec vendor labels */
54 static struct hda_vendor_id hda_vendor_ids[] = {
55         { 0x1002, "ATI" },
56         { 0x1057, "Motorola" },
57         { 0x1095, "Silicon Image" },
58         { 0x10ec, "Realtek" },
59         { 0x1106, "VIA" },
60         { 0x111d, "IDT" },
61         { 0x11c1, "LSI" },
62         { 0x11d4, "Analog Devices" },
63         { 0x13f6, "C-Media" },
64         { 0x14f1, "Conexant" },
65         { 0x17e8, "Chrontel" },
66         { 0x1854, "LG" },
67         { 0x434d, "C-Media" },
68         { 0x8384, "SigmaTel" },
69         {} /* terminator */
70 };
71
72 static const struct hda_codec_preset *hda_preset_tables[] = {
73 #ifdef CONFIG_SND_HDA_CODEC_REALTEK
74         snd_hda_preset_realtek,
75 #endif
76 #ifdef CONFIG_SND_HDA_CODEC_CMEDIA
77         snd_hda_preset_cmedia,
78 #endif
79 #ifdef CONFIG_SND_HDA_CODEC_ANALOG
80         snd_hda_preset_analog,
81 #endif
82 #ifdef CONFIG_SND_HDA_CODEC_SIGMATEL
83         snd_hda_preset_sigmatel,
84 #endif
85 #ifdef CONFIG_SND_HDA_CODEC_SI3054
86         snd_hda_preset_si3054,
87 #endif
88 #ifdef CONFIG_SND_HDA_CODEC_ATIHDMI
89         snd_hda_preset_atihdmi,
90 #endif
91 #ifdef CONFIG_SND_HDA_CODEC_CONEXANT
92         snd_hda_preset_conexant,
93 #endif
94 #ifdef CONFIG_SND_HDA_CODEC_VIA
95         snd_hda_preset_via,
96 #endif
97         NULL
98 };
99
100 #ifdef CONFIG_SND_HDA_POWER_SAVE
101 static void hda_power_work(struct work_struct *work);
102 static void hda_keep_power_on(struct hda_codec *codec);
103 #else
104 static inline void hda_keep_power_on(struct hda_codec *codec) {}
105 #endif
106
107 /**
108  * snd_hda_codec_read - send a command and get the response
109  * @codec: the HDA codec
110  * @nid: NID to send the command
111  * @direct: direct flag
112  * @verb: the verb to send
113  * @parm: the parameter for the verb
114  *
115  * Send a single command and read the corresponding response.
116  *
117  * Returns the obtained response value, or -1 for an error.
118  */
119 unsigned int snd_hda_codec_read(struct hda_codec *codec, hda_nid_t nid,
120                                 int direct,
121                                 unsigned int verb, unsigned int parm)
122 {
123         unsigned int res;
124         snd_hda_power_up(codec);
125         mutex_lock(&codec->bus->cmd_mutex);
126         if (!codec->bus->ops.command(codec, nid, direct, verb, parm))
127                 res = codec->bus->ops.get_response(codec);
128         else
129                 res = (unsigned int)-1;
130         mutex_unlock(&codec->bus->cmd_mutex);
131         snd_hda_power_down(codec);
132         return res;
133 }
134
135 /**
136  * snd_hda_codec_write - send a single command without waiting for response
137  * @codec: the HDA codec
138  * @nid: NID to send the command
139  * @direct: direct flag
140  * @verb: the verb to send
141  * @parm: the parameter for the verb
142  *
143  * Send a single command without waiting for response.
144  *
145  * Returns 0 if successful, or a negative error code.
146  */
147 int snd_hda_codec_write(struct hda_codec *codec, hda_nid_t nid, int direct,
148                          unsigned int verb, unsigned int parm)
149 {
150         int err;
151         snd_hda_power_up(codec);
152         mutex_lock(&codec->bus->cmd_mutex);
153         err = codec->bus->ops.command(codec, nid, direct, verb, parm);
154         mutex_unlock(&codec->bus->cmd_mutex);
155         snd_hda_power_down(codec);
156         return err;
157 }
158
159 /**
160  * snd_hda_sequence_write - sequence writes
161  * @codec: the HDA codec
162  * @seq: VERB array to send
163  *
164  * Send the commands sequentially from the given array.
165  * The array must be terminated with NID=0.
166  */
167 void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq)
168 {
169         for (; seq->nid; seq++)
170                 snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param);
171 }
172
173 /**
174  * snd_hda_get_sub_nodes - get the range of sub nodes
175  * @codec: the HDA codec
176  * @nid: NID to parse
177  * @start_id: the pointer to store the start NID
178  *
179  * Parse the NID and store the start NID of its sub-nodes.
180  * Returns the number of sub-nodes.
181  */
182 int snd_hda_get_sub_nodes(struct hda_codec *codec, hda_nid_t nid,
183                           hda_nid_t *start_id)
184 {
185         unsigned int parm;
186
187         parm = snd_hda_param_read(codec, nid, AC_PAR_NODE_COUNT);
188         if (parm == -1)
189                 return 0;
190         *start_id = (parm >> 16) & 0x7fff;
191         return (int)(parm & 0x7fff);
192 }
193
194 /**
195  * snd_hda_get_connections - get connection list
196  * @codec: the HDA codec
197  * @nid: NID to parse
198  * @conn_list: connection list array
199  * @max_conns: max. number of connections to store
200  *
201  * Parses the connection list of the given widget and stores the list
202  * of NIDs.
203  *
204  * Returns the number of connections, or a negative error code.
205  */
206 int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
207                             hda_nid_t *conn_list, int max_conns)
208 {
209         unsigned int parm;
210         int i, conn_len, conns;
211         unsigned int shift, num_elems, mask;
212         hda_nid_t prev_nid;
213
214         if (snd_BUG_ON(!conn_list || max_conns <= 0))
215                 return -EINVAL;
216
217         parm = snd_hda_param_read(codec, nid, AC_PAR_CONNLIST_LEN);
218         if (parm & AC_CLIST_LONG) {
219                 /* long form */
220                 shift = 16;
221                 num_elems = 2;
222         } else {
223                 /* short form */
224                 shift = 8;
225                 num_elems = 4;
226         }
227         conn_len = parm & AC_CLIST_LENGTH;
228         mask = (1 << (shift-1)) - 1;
229
230         if (!conn_len)
231                 return 0; /* no connection */
232
233         if (conn_len == 1) {
234                 /* single connection */
235                 parm = snd_hda_codec_read(codec, nid, 0,
236                                           AC_VERB_GET_CONNECT_LIST, 0);
237                 conn_list[0] = parm & mask;
238                 return 1;
239         }
240
241         /* multi connection */
242         conns = 0;
243         prev_nid = 0;
244         for (i = 0; i < conn_len; i++) {
245                 int range_val;
246                 hda_nid_t val, n;
247
248                 if (i % num_elems == 0)
249                         parm = snd_hda_codec_read(codec, nid, 0,
250                                                   AC_VERB_GET_CONNECT_LIST, i);
251                 range_val = !!(parm & (1 << (shift-1))); /* ranges */
252                 val = parm & mask;
253                 parm >>= shift;
254                 if (range_val) {
255                         /* ranges between the previous and this one */
256                         if (!prev_nid || prev_nid >= val) {
257                                 snd_printk(KERN_WARNING "hda_codec: "
258                                            "invalid dep_range_val %x:%x\n",
259                                            prev_nid, val);
260                                 continue;
261                         }
262                         for (n = prev_nid + 1; n <= val; n++) {
263                                 if (conns >= max_conns) {
264                                         snd_printk(KERN_ERR
265                                                    "Too many connections\n");
266                                         return -EINVAL;
267                                 }
268                                 conn_list[conns++] = n;
269                         }
270                 } else {
271                         if (conns >= max_conns) {
272                                 snd_printk(KERN_ERR "Too many connections\n");
273                                 return -EINVAL;
274                         }
275                         conn_list[conns++] = val;
276                 }
277                 prev_nid = val;
278         }
279         return conns;
280 }
281
282
283 /**
284  * snd_hda_queue_unsol_event - add an unsolicited event to queue
285  * @bus: the BUS
286  * @res: unsolicited event (lower 32bit of RIRB entry)
287  * @res_ex: codec addr and flags (upper 32bit or RIRB entry)
288  *
289  * Adds the given event to the queue.  The events are processed in
290  * the workqueue asynchronously.  Call this function in the interrupt
291  * hanlder when RIRB receives an unsolicited event.
292  *
293  * Returns 0 if successful, or a negative error code.
294  */
295 int snd_hda_queue_unsol_event(struct hda_bus *bus, u32 res, u32 res_ex)
296 {
297         struct hda_bus_unsolicited *unsol;
298         unsigned int wp;
299
300         unsol = bus->unsol;
301         if (!unsol)
302                 return 0;
303
304         wp = (unsol->wp + 1) % HDA_UNSOL_QUEUE_SIZE;
305         unsol->wp = wp;
306
307         wp <<= 1;
308         unsol->queue[wp] = res;
309         unsol->queue[wp + 1] = res_ex;
310
311         schedule_work(&unsol->work);
312
313         return 0;
314 }
315
316 /*
317  * process queueud unsolicited events
318  */
319 static void process_unsol_events(struct work_struct *work)
320 {
321         struct hda_bus_unsolicited *unsol =
322                 container_of(work, struct hda_bus_unsolicited, work);
323         struct hda_bus *bus = unsol->bus;
324         struct hda_codec *codec;
325         unsigned int rp, caddr, res;
326
327         while (unsol->rp != unsol->wp) {
328                 rp = (unsol->rp + 1) % HDA_UNSOL_QUEUE_SIZE;
329                 unsol->rp = rp;
330                 rp <<= 1;
331                 res = unsol->queue[rp];
332                 caddr = unsol->queue[rp + 1];
333                 if (!(caddr & (1 << 4))) /* no unsolicited event? */
334                         continue;
335                 codec = bus->caddr_tbl[caddr & 0x0f];
336                 if (codec && codec->patch_ops.unsol_event)
337                         codec->patch_ops.unsol_event(codec, res);
338         }
339 }
340
341 /*
342  * initialize unsolicited queue
343  */
344 static int __devinit init_unsol_queue(struct hda_bus *bus)
345 {
346         struct hda_bus_unsolicited *unsol;
347
348         if (bus->unsol) /* already initialized */
349                 return 0;
350
351         unsol = kzalloc(sizeof(*unsol), GFP_KERNEL);
352         if (!unsol) {
353                 snd_printk(KERN_ERR "hda_codec: "
354                            "can't allocate unsolicited queue\n");
355                 return -ENOMEM;
356         }
357         INIT_WORK(&unsol->work, process_unsol_events);
358         unsol->bus = bus;
359         bus->unsol = unsol;
360         return 0;
361 }
362
363 /*
364  * destructor
365  */
366 static void snd_hda_codec_free(struct hda_codec *codec);
367
368 static int snd_hda_bus_free(struct hda_bus *bus)
369 {
370         struct hda_codec *codec, *n;
371
372         if (!bus)
373                 return 0;
374         if (bus->unsol) {
375                 flush_scheduled_work();
376                 kfree(bus->unsol);
377         }
378         list_for_each_entry_safe(codec, n, &bus->codec_list, list) {
379                 snd_hda_codec_free(codec);
380         }
381         if (bus->ops.private_free)
382                 bus->ops.private_free(bus);
383         kfree(bus);
384         return 0;
385 }
386
387 static int snd_hda_bus_dev_free(struct snd_device *device)
388 {
389         struct hda_bus *bus = device->device_data;
390         return snd_hda_bus_free(bus);
391 }
392
393 /**
394  * snd_hda_bus_new - create a HDA bus
395  * @card: the card entry
396  * @temp: the template for hda_bus information
397  * @busp: the pointer to store the created bus instance
398  *
399  * Returns 0 if successful, or a negative error code.
400  */
401 int __devinit snd_hda_bus_new(struct snd_card *card,
402                               const struct hda_bus_template *temp,
403                               struct hda_bus **busp)
404 {
405         struct hda_bus *bus;
406         int err;
407         static struct snd_device_ops dev_ops = {
408                 .dev_free = snd_hda_bus_dev_free,
409         };
410
411         if (snd_BUG_ON(!temp))
412                 return -EINVAL;
413         if (snd_BUG_ON(!temp->ops.command || !temp->ops.get_response))
414                 return -EINVAL;
415
416         if (busp)
417                 *busp = NULL;
418
419         bus = kzalloc(sizeof(*bus), GFP_KERNEL);
420         if (bus == NULL) {
421                 snd_printk(KERN_ERR "can't allocate struct hda_bus\n");
422                 return -ENOMEM;
423         }
424
425         bus->card = card;
426         bus->private_data = temp->private_data;
427         bus->pci = temp->pci;
428         bus->modelname = temp->modelname;
429         bus->ops = temp->ops;
430
431         mutex_init(&bus->cmd_mutex);
432         INIT_LIST_HEAD(&bus->codec_list);
433
434         err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops);
435         if (err < 0) {
436                 snd_hda_bus_free(bus);
437                 return err;
438         }
439         if (busp)
440                 *busp = bus;
441         return 0;
442 }
443
444 #ifdef CONFIG_SND_HDA_GENERIC
445 #define is_generic_config(codec) \
446         (codec->bus->modelname && !strcmp(codec->bus->modelname, "generic"))
447 #else
448 #define is_generic_config(codec)        0
449 #endif
450
451 /*
452  * find a matching codec preset
453  */
454 static const struct hda_codec_preset __devinit *
455 find_codec_preset(struct hda_codec *codec)
456 {
457         const struct hda_codec_preset **tbl, *preset;
458
459         if (is_generic_config(codec))
460                 return NULL; /* use the generic parser */
461
462         for (tbl = hda_preset_tables; *tbl; tbl++) {
463                 for (preset = *tbl; preset->id; preset++) {
464                         u32 mask = preset->mask;
465                         if (preset->afg && preset->afg != codec->afg)
466                                 continue;
467                         if (preset->mfg && preset->mfg != codec->mfg)
468                                 continue;
469                         if (!mask)
470                                 mask = ~0;
471                         if (preset->id == (codec->vendor_id & mask) &&
472                             (!preset->rev ||
473                              preset->rev == codec->revision_id))
474                                 return preset;
475                 }
476         }
477         return NULL;
478 }
479
480 /*
481  * snd_hda_get_codec_name - store the codec name
482  */
483 void snd_hda_get_codec_name(struct hda_codec *codec,
484                             char *name, int namelen)
485 {
486         const struct hda_vendor_id *c;
487         const char *vendor = NULL;
488         u16 vendor_id = codec->vendor_id >> 16;
489         char tmp[16];
490
491         for (c = hda_vendor_ids; c->id; c++) {
492                 if (c->id == vendor_id) {
493                         vendor = c->name;
494                         break;
495                 }
496         }
497         if (!vendor) {
498                 sprintf(tmp, "Generic %04x", vendor_id);
499                 vendor = tmp;
500         }
501         if (codec->preset && codec->preset->name)
502                 snprintf(name, namelen, "%s %s", vendor, codec->preset->name);
503         else
504                 snprintf(name, namelen, "%s ID %x", vendor,
505                          codec->vendor_id & 0xffff);
506 }
507
508 /*
509  * look for an AFG and MFG nodes
510  */
511 static void __devinit setup_fg_nodes(struct hda_codec *codec)
512 {
513         int i, total_nodes;
514         hda_nid_t nid;
515
516         total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid);
517         for (i = 0; i < total_nodes; i++, nid++) {
518                 unsigned int func;
519                 func = snd_hda_param_read(codec, nid, AC_PAR_FUNCTION_TYPE);
520                 switch (func & 0xff) {
521                 case AC_GRP_AUDIO_FUNCTION:
522                         codec->afg = nid;
523                         break;
524                 case AC_GRP_MODEM_FUNCTION:
525                         codec->mfg = nid;
526                         break;
527                 default:
528                         break;
529                 }
530         }
531 }
532
533 /*
534  * read widget caps for each widget and store in cache
535  */
536 static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
537 {
538         int i;
539         hda_nid_t nid;
540
541         codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node,
542                                                  &codec->start_nid);
543         codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL);
544         if (!codec->wcaps)
545                 return -ENOMEM;
546         nid = codec->start_nid;
547         for (i = 0; i < codec->num_nodes; i++, nid++)
548                 codec->wcaps[i] = snd_hda_param_read(codec, nid,
549                                                      AC_PAR_AUDIO_WIDGET_CAP);
550         return 0;
551 }
552
553
554 static void init_hda_cache(struct hda_cache_rec *cache,
555                            unsigned int record_size);
556 static void free_hda_cache(struct hda_cache_rec *cache);
557
558 /*
559  * codec destructor
560  */
561 static void snd_hda_codec_free(struct hda_codec *codec)
562 {
563         if (!codec)
564                 return;
565 #ifdef CONFIG_SND_HDA_POWER_SAVE
566         cancel_delayed_work(&codec->power_work);
567         flush_scheduled_work();
568 #endif
569         list_del(&codec->list);
570         codec->bus->caddr_tbl[codec->addr] = NULL;
571         if (codec->patch_ops.free)
572                 codec->patch_ops.free(codec);
573         free_hda_cache(&codec->amp_cache);
574         free_hda_cache(&codec->cmd_cache);
575         kfree(codec->wcaps);
576         kfree(codec);
577 }
578
579 /**
580  * snd_hda_codec_new - create a HDA codec
581  * @bus: the bus to assign
582  * @codec_addr: the codec address
583  * @codecp: the pointer to store the generated codec
584  *
585  * Returns 0 if successful, or a negative error code.
586  */
587 int __devinit snd_hda_codec_new(struct hda_bus *bus, unsigned int codec_addr,
588                                 struct hda_codec **codecp)
589 {
590         struct hda_codec *codec;
591         char component[31];
592         int err;
593
594         if (snd_BUG_ON(!bus))
595                 return -EINVAL;
596         if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS))
597                 return -EINVAL;
598
599         if (bus->caddr_tbl[codec_addr]) {
600                 snd_printk(KERN_ERR "hda_codec: "
601                            "address 0x%x is already occupied\n", codec_addr);
602                 return -EBUSY;
603         }
604
605         codec = kzalloc(sizeof(*codec), GFP_KERNEL);
606         if (codec == NULL) {
607                 snd_printk(KERN_ERR "can't allocate struct hda_codec\n");
608                 return -ENOMEM;
609         }
610
611         codec->bus = bus;
612         codec->addr = codec_addr;
613         mutex_init(&codec->spdif_mutex);
614         init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
615         init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
616
617 #ifdef CONFIG_SND_HDA_POWER_SAVE
618         INIT_DELAYED_WORK(&codec->power_work, hda_power_work);
619         /* snd_hda_codec_new() marks the codec as power-up, and leave it as is.
620          * the caller has to power down appropriatley after initialization
621          * phase.
622          */
623         hda_keep_power_on(codec);
624 #endif
625
626         list_add_tail(&codec->list, &bus->codec_list);
627         bus->caddr_tbl[codec_addr] = codec;
628
629         codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
630                                               AC_PAR_VENDOR_ID);
631         if (codec->vendor_id == -1)
632                 /* read again, hopefully the access method was corrected
633                  * in the last read...
634                  */
635                 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
636                                                       AC_PAR_VENDOR_ID);
637         codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT,
638                                                  AC_PAR_SUBSYSTEM_ID);
639         codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT,
640                                                 AC_PAR_REV_ID);
641
642         setup_fg_nodes(codec);
643         if (!codec->afg && !codec->mfg) {
644                 snd_printdd("hda_codec: no AFG or MFG node found\n");
645                 snd_hda_codec_free(codec);
646                 return -ENODEV;
647         }
648
649         if (read_widget_caps(codec, codec->afg ? codec->afg : codec->mfg) < 0) {
650                 snd_printk(KERN_ERR "hda_codec: cannot malloc\n");
651                 snd_hda_codec_free(codec);
652                 return -ENOMEM;
653         }
654
655         if (!codec->subsystem_id) {
656                 hda_nid_t nid = codec->afg ? codec->afg : codec->mfg;
657                 codec->subsystem_id =
658                         snd_hda_codec_read(codec, nid, 0,
659                                            AC_VERB_GET_SUBSYSTEM_ID, 0);
660         }
661
662         codec->preset = find_codec_preset(codec);
663         /* audio codec should override the mixer name */
664         if (codec->afg || !*bus->card->mixername)
665                 snd_hda_get_codec_name(codec, bus->card->mixername,
666                                        sizeof(bus->card->mixername));
667
668         if (is_generic_config(codec)) {
669                 err = snd_hda_parse_generic_codec(codec);
670                 goto patched;
671         }
672         if (codec->preset && codec->preset->patch) {
673                 err = codec->preset->patch(codec);
674                 goto patched;
675         }
676
677         /* call the default parser */
678         err = snd_hda_parse_generic_codec(codec);
679         if (err < 0)
680                 printk(KERN_ERR "hda-codec: No codec parser is available\n");
681
682  patched:
683         if (err < 0) {
684                 snd_hda_codec_free(codec);
685                 return err;
686         }
687
688         if (codec->patch_ops.unsol_event)
689                 init_unsol_queue(bus);
690
691         snd_hda_codec_proc_new(codec);
692 #ifdef CONFIG_SND_HDA_HWDEP
693         snd_hda_create_hwdep(codec);
694 #endif
695
696         sprintf(component, "HDA:%08x,%08x,%08x", codec->vendor_id, codec->subsystem_id, codec->revision_id);
697         snd_component_add(codec->bus->card, component);
698
699         if (codecp)
700                 *codecp = codec;
701         return 0;
702 }
703
704 /**
705  * snd_hda_codec_setup_stream - set up the codec for streaming
706  * @codec: the CODEC to set up
707  * @nid: the NID to set up
708  * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
709  * @channel_id: channel id to pass, zero based.
710  * @format: stream format.
711  */
712 void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
713                                 u32 stream_tag,
714                                 int channel_id, int format)
715 {
716         if (!nid)
717                 return;
718
719         snd_printdd("hda_codec_setup_stream: "
720                     "NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
721                     nid, stream_tag, channel_id, format);
722         snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID,
723                             (stream_tag << 4) | channel_id);
724         msleep(1);
725         snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, format);
726 }
727
728 void snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid)
729 {
730         if (!nid)
731                 return;
732
733         snd_printdd("hda_codec_cleanup_stream: NID=0x%x\n", nid);
734         snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
735 #if 0 /* keep the format */
736         msleep(1);
737         snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0);
738 #endif
739 }
740
741 /*
742  * amp access functions
743  */
744
745 /* FIXME: more better hash key? */
746 #define HDA_HASH_KEY(nid,dir,idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
747 #define INFO_AMP_CAPS   (1<<0)
748 #define INFO_AMP_VOL(ch)        (1 << (1 + (ch)))
749
750 /* initialize the hash table */
751 static void __devinit init_hda_cache(struct hda_cache_rec *cache,
752                                      unsigned int record_size)
753 {
754         memset(cache, 0, sizeof(*cache));
755         memset(cache->hash, 0xff, sizeof(cache->hash));
756         cache->record_size = record_size;
757 }
758
759 static void free_hda_cache(struct hda_cache_rec *cache)
760 {
761         kfree(cache->buffer);
762 }
763
764 /* query the hash.  allocate an entry if not found. */
765 static struct hda_cache_head  *get_alloc_hash(struct hda_cache_rec *cache,
766                                               u32 key)
767 {
768         u16 idx = key % (u16)ARRAY_SIZE(cache->hash);
769         u16 cur = cache->hash[idx];
770         struct hda_cache_head *info;
771
772         while (cur != 0xffff) {
773                 info = (struct hda_cache_head *)(cache->buffer +
774                                                  cur * cache->record_size);
775                 if (info->key == key)
776                         return info;
777                 cur = info->next;
778         }
779
780         /* add a new hash entry */
781         if (cache->num_entries >= cache->size) {
782                 /* reallocate the array */
783                 unsigned int new_size = cache->size + 64;
784                 void *new_buffer;
785                 new_buffer = kcalloc(new_size, cache->record_size, GFP_KERNEL);
786                 if (!new_buffer) {
787                         snd_printk(KERN_ERR "hda_codec: "
788                                    "can't malloc amp_info\n");
789                         return NULL;
790                 }
791                 if (cache->buffer) {
792                         memcpy(new_buffer, cache->buffer,
793                                cache->size * cache->record_size);
794                         kfree(cache->buffer);
795                 }
796                 cache->size = new_size;
797                 cache->buffer = new_buffer;
798         }
799         cur = cache->num_entries++;
800         info = (struct hda_cache_head *)(cache->buffer +
801                                          cur * cache->record_size);
802         info->key = key;
803         info->val = 0;
804         info->next = cache->hash[idx];
805         cache->hash[idx] = cur;
806
807         return info;
808 }
809
810 /* query and allocate an amp hash entry */
811 static inline struct hda_amp_info *
812 get_alloc_amp_hash(struct hda_codec *codec, u32 key)
813 {
814         return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key);
815 }
816
817 /*
818  * query AMP capabilities for the given widget and direction
819  */
820 u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
821 {
822         struct hda_amp_info *info;
823
824         info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, 0));
825         if (!info)
826                 return 0;
827         if (!(info->head.val & INFO_AMP_CAPS)) {
828                 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
829                         nid = codec->afg;
830                 info->amp_caps = snd_hda_param_read(codec, nid,
831                                                     direction == HDA_OUTPUT ?
832                                                     AC_PAR_AMP_OUT_CAP :
833                                                     AC_PAR_AMP_IN_CAP);
834                 if (info->amp_caps)
835                         info->head.val |= INFO_AMP_CAPS;
836         }
837         return info->amp_caps;
838 }
839
840 int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
841                               unsigned int caps)
842 {
843         struct hda_amp_info *info;
844
845         info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, dir, 0));
846         if (!info)
847                 return -EINVAL;
848         info->amp_caps = caps;
849         info->head.val |= INFO_AMP_CAPS;
850         return 0;
851 }
852
853 /*
854  * read the current volume to info
855  * if the cache exists, read the cache value.
856  */
857 static unsigned int get_vol_mute(struct hda_codec *codec,
858                                  struct hda_amp_info *info, hda_nid_t nid,
859                                  int ch, int direction, int index)
860 {
861         u32 val, parm;
862
863         if (info->head.val & INFO_AMP_VOL(ch))
864                 return info->vol[ch];
865
866         parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
867         parm |= direction == HDA_OUTPUT ? AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
868         parm |= index;
869         val = snd_hda_codec_read(codec, nid, 0,
870                                  AC_VERB_GET_AMP_GAIN_MUTE, parm);
871         info->vol[ch] = val & 0xff;
872         info->head.val |= INFO_AMP_VOL(ch);
873         return info->vol[ch];
874 }
875
876 /*
877  * write the current volume in info to the h/w and update the cache
878  */
879 static void put_vol_mute(struct hda_codec *codec, struct hda_amp_info *info,
880                          hda_nid_t nid, int ch, int direction, int index,
881                          int val)
882 {
883         u32 parm;
884
885         parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
886         parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
887         parm |= index << AC_AMP_SET_INDEX_SHIFT;
888         parm |= val;
889         snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
890         info->vol[ch] = val;
891 }
892
893 /*
894  * read AMP value.  The volume is between 0 to 0x7f, 0x80 = mute bit.
895  */
896 int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
897                            int direction, int index)
898 {
899         struct hda_amp_info *info;
900         info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
901         if (!info)
902                 return 0;
903         return get_vol_mute(codec, info, nid, ch, direction, index);
904 }
905
906 /*
907  * update the AMP value, mask = bit mask to set, val = the value
908  */
909 int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
910                              int direction, int idx, int mask, int val)
911 {
912         struct hda_amp_info *info;
913
914         info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, idx));
915         if (!info)
916                 return 0;
917         val &= mask;
918         val |= get_vol_mute(codec, info, nid, ch, direction, idx) & ~mask;
919         if (info->vol[ch] == val)
920                 return 0;
921         put_vol_mute(codec, info, nid, ch, direction, idx, val);
922         return 1;
923 }
924
925 /*
926  * update the AMP stereo with the same mask and value
927  */
928 int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
929                              int direction, int idx, int mask, int val)
930 {
931         int ch, ret = 0;
932         for (ch = 0; ch < 2; ch++)
933                 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
934                                                 idx, mask, val);
935         return ret;
936 }
937
938 #ifdef SND_HDA_NEEDS_RESUME
939 /* resume the all amp commands from the cache */
940 void snd_hda_codec_resume_amp(struct hda_codec *codec)
941 {
942         struct hda_amp_info *buffer = codec->amp_cache.buffer;
943         int i;
944
945         for (i = 0; i < codec->amp_cache.size; i++, buffer++) {
946                 u32 key = buffer->head.key;
947                 hda_nid_t nid;
948                 unsigned int idx, dir, ch;
949                 if (!key)
950                         continue;
951                 nid = key & 0xff;
952                 idx = (key >> 16) & 0xff;
953                 dir = (key >> 24) & 0xff;
954                 for (ch = 0; ch < 2; ch++) {
955                         if (!(buffer->head.val & INFO_AMP_VOL(ch)))
956                                 continue;
957                         put_vol_mute(codec, buffer, nid, ch, dir, idx,
958                                      buffer->vol[ch]);
959                 }
960         }
961 }
962 #endif /* SND_HDA_NEEDS_RESUME */
963
964 /* volume */
965 int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
966                                   struct snd_ctl_elem_info *uinfo)
967 {
968         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
969         u16 nid = get_amp_nid(kcontrol);
970         u8 chs = get_amp_channels(kcontrol);
971         int dir = get_amp_direction(kcontrol);
972         u32 caps;
973
974         caps = query_amp_caps(codec, nid, dir);
975         /* num steps */
976         caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
977         if (!caps) {
978                 printk(KERN_WARNING "hda_codec: "
979                        "num_steps = 0 for NID=0x%x (ctl = %s)\n", nid,
980                        kcontrol->id.name);
981                 return -EINVAL;
982         }
983         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
984         uinfo->count = chs == 3 ? 2 : 1;
985         uinfo->value.integer.min = 0;
986         uinfo->value.integer.max = caps;
987         return 0;
988 }
989
990 int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
991                                  struct snd_ctl_elem_value *ucontrol)
992 {
993         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
994         hda_nid_t nid = get_amp_nid(kcontrol);
995         int chs = get_amp_channels(kcontrol);
996         int dir = get_amp_direction(kcontrol);
997         int idx = get_amp_index(kcontrol);
998         long *valp = ucontrol->value.integer.value;
999
1000         if (chs & 1)
1001                 *valp++ = snd_hda_codec_amp_read(codec, nid, 0, dir, idx)
1002                         & HDA_AMP_VOLMASK;
1003         if (chs & 2)
1004                 *valp = snd_hda_codec_amp_read(codec, nid, 1, dir, idx)
1005                         & HDA_AMP_VOLMASK;
1006         return 0;
1007 }
1008
1009 int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
1010                                  struct snd_ctl_elem_value *ucontrol)
1011 {
1012         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1013         hda_nid_t nid = get_amp_nid(kcontrol);
1014         int chs = get_amp_channels(kcontrol);
1015         int dir = get_amp_direction(kcontrol);
1016         int idx = get_amp_index(kcontrol);
1017         long *valp = ucontrol->value.integer.value;
1018         int change = 0;
1019
1020         snd_hda_power_up(codec);
1021         if (chs & 1) {
1022                 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
1023                                                   0x7f, *valp);
1024                 valp++;
1025         }
1026         if (chs & 2)
1027                 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
1028                                                    0x7f, *valp);
1029         snd_hda_power_down(codec);
1030         return change;
1031 }
1032
1033 int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
1034                           unsigned int size, unsigned int __user *_tlv)
1035 {
1036         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1037         hda_nid_t nid = get_amp_nid(kcontrol);
1038         int dir = get_amp_direction(kcontrol);
1039         u32 caps, val1, val2;
1040
1041         if (size < 4 * sizeof(unsigned int))
1042                 return -ENOMEM;
1043         caps = query_amp_caps(codec, nid, dir);
1044         val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
1045         val2 = (val2 + 1) * 25;
1046         val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
1047         val1 = ((int)val1) * ((int)val2);
1048         if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
1049                 return -EFAULT;
1050         if (put_user(2 * sizeof(unsigned int), _tlv + 1))
1051                 return -EFAULT;
1052         if (put_user(val1, _tlv + 2))
1053                 return -EFAULT;
1054         if (put_user(val2, _tlv + 3))
1055                 return -EFAULT;
1056         return 0;
1057 }
1058
1059 /*
1060  * set (static) TLV for virtual master volume; recalculated as max 0dB
1061  */
1062 void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
1063                              unsigned int *tlv)
1064 {
1065         u32 caps;
1066         int nums, step;
1067
1068         caps = query_amp_caps(codec, nid, dir);
1069         nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1070         step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
1071         step = (step + 1) * 25;
1072         tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
1073         tlv[1] = 2 * sizeof(unsigned int);
1074         tlv[2] = -nums * step;
1075         tlv[3] = step;
1076 }
1077
1078 /* find a mixer control element with the given name */
1079 static struct snd_kcontrol *
1080 _snd_hda_find_mixer_ctl(struct hda_codec *codec,
1081                         const char *name, int idx)
1082 {
1083         struct snd_ctl_elem_id id;
1084         memset(&id, 0, sizeof(id));
1085         id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1086         id.index = idx;
1087         strcpy(id.name, name);
1088         return snd_ctl_find_id(codec->bus->card, &id);
1089 }
1090
1091 struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
1092                                             const char *name)
1093 {
1094         return _snd_hda_find_mixer_ctl(codec, name, 0);
1095 }
1096
1097 /* create a virtual master control and add slaves */
1098 int snd_hda_add_vmaster(struct hda_codec *codec, char *name,
1099                         unsigned int *tlv, const char **slaves)
1100 {
1101         struct snd_kcontrol *kctl;
1102         const char **s;
1103         int err;
1104
1105         for (s = slaves; *s && !snd_hda_find_mixer_ctl(codec, *s); s++)
1106                 ;
1107         if (!*s) {
1108                 snd_printdd("No slave found for %s\n", name);
1109                 return 0;
1110         }
1111         kctl = snd_ctl_make_virtual_master(name, tlv);
1112         if (!kctl)
1113                 return -ENOMEM;
1114         err = snd_ctl_add(codec->bus->card, kctl);
1115         if (err < 0)
1116                 return err;
1117         
1118         for (s = slaves; *s; s++) {
1119                 struct snd_kcontrol *sctl;
1120
1121                 sctl = snd_hda_find_mixer_ctl(codec, *s);
1122                 if (!sctl) {
1123                         snd_printdd("Cannot find slave %s, skipped\n", *s);
1124                         continue;
1125                 }
1126                 err = snd_ctl_add_slave(kctl, sctl);
1127                 if (err < 0)
1128                         return err;
1129         }
1130         return 0;
1131 }
1132
1133 /* switch */
1134 int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
1135                                   struct snd_ctl_elem_info *uinfo)
1136 {
1137         int chs = get_amp_channels(kcontrol);
1138
1139         uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1140         uinfo->count = chs == 3 ? 2 : 1;
1141         uinfo->value.integer.min = 0;
1142         uinfo->value.integer.max = 1;
1143         return 0;
1144 }
1145
1146 int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
1147                                  struct snd_ctl_elem_value *ucontrol)
1148 {
1149         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1150         hda_nid_t nid = get_amp_nid(kcontrol);
1151         int chs = get_amp_channels(kcontrol);
1152         int dir = get_amp_direction(kcontrol);
1153         int idx = get_amp_index(kcontrol);
1154         long *valp = ucontrol->value.integer.value;
1155
1156         if (chs & 1)
1157                 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
1158                            HDA_AMP_MUTE) ? 0 : 1;
1159         if (chs & 2)
1160                 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
1161                          HDA_AMP_MUTE) ? 0 : 1;
1162         return 0;
1163 }
1164
1165 int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
1166                                  struct snd_ctl_elem_value *ucontrol)
1167 {
1168         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1169         hda_nid_t nid = get_amp_nid(kcontrol);
1170         int chs = get_amp_channels(kcontrol);
1171         int dir = get_amp_direction(kcontrol);
1172         int idx = get_amp_index(kcontrol);
1173         long *valp = ucontrol->value.integer.value;
1174         int change = 0;
1175
1176         snd_hda_power_up(codec);
1177         if (chs & 1) {
1178                 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
1179                                                   HDA_AMP_MUTE,
1180                                                   *valp ? 0 : HDA_AMP_MUTE);
1181                 valp++;
1182         }
1183         if (chs & 2)
1184                 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
1185                                                    HDA_AMP_MUTE,
1186                                                    *valp ? 0 : HDA_AMP_MUTE);
1187 #ifdef CONFIG_SND_HDA_POWER_SAVE
1188         if (codec->patch_ops.check_power_status)
1189                 codec->patch_ops.check_power_status(codec, nid);
1190 #endif
1191         snd_hda_power_down(codec);
1192         return change;
1193 }
1194
1195 /*
1196  * bound volume controls
1197  *
1198  * bind multiple volumes (# indices, from 0)
1199  */
1200
1201 #define AMP_VAL_IDX_SHIFT       19
1202 #define AMP_VAL_IDX_MASK        (0x0f<<19)
1203
1204 int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
1205                                   struct snd_ctl_elem_value *ucontrol)
1206 {
1207         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1208         unsigned long pval;
1209         int err;
1210
1211         mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
1212         pval = kcontrol->private_value;
1213         kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
1214         err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
1215         kcontrol->private_value = pval;
1216         mutex_unlock(&codec->spdif_mutex);
1217         return err;
1218 }
1219
1220 int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
1221                                   struct snd_ctl_elem_value *ucontrol)
1222 {
1223         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1224         unsigned long pval;
1225         int i, indices, err = 0, change = 0;
1226
1227         mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
1228         pval = kcontrol->private_value;
1229         indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
1230         for (i = 0; i < indices; i++) {
1231                 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
1232                         (i << AMP_VAL_IDX_SHIFT);
1233                 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
1234                 if (err < 0)
1235                         break;
1236                 change |= err;
1237         }
1238         kcontrol->private_value = pval;
1239         mutex_unlock(&codec->spdif_mutex);
1240         return err < 0 ? err : change;
1241 }
1242
1243 /*
1244  * generic bound volume/swtich controls
1245  */
1246 int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
1247                                  struct snd_ctl_elem_info *uinfo)
1248 {
1249         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1250         struct hda_bind_ctls *c;
1251         int err;
1252
1253         mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
1254         c = (struct hda_bind_ctls *)kcontrol->private_value;
1255         kcontrol->private_value = *c->values;
1256         err = c->ops->info(kcontrol, uinfo);
1257         kcontrol->private_value = (long)c;
1258         mutex_unlock(&codec->spdif_mutex);
1259         return err;
1260 }
1261
1262 int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
1263                                 struct snd_ctl_elem_value *ucontrol)
1264 {
1265         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1266         struct hda_bind_ctls *c;
1267         int err;
1268
1269         mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
1270         c = (struct hda_bind_ctls *)kcontrol->private_value;
1271         kcontrol->private_value = *c->values;
1272         err = c->ops->get(kcontrol, ucontrol);
1273         kcontrol->private_value = (long)c;
1274         mutex_unlock(&codec->spdif_mutex);
1275         return err;
1276 }
1277
1278 int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
1279                                 struct snd_ctl_elem_value *ucontrol)
1280 {
1281         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1282         struct hda_bind_ctls *c;
1283         unsigned long *vals;
1284         int err = 0, change = 0;
1285
1286         mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
1287         c = (struct hda_bind_ctls *)kcontrol->private_value;
1288         for (vals = c->values; *vals; vals++) {
1289                 kcontrol->private_value = *vals;
1290                 err = c->ops->put(kcontrol, ucontrol);
1291                 if (err < 0)
1292                         break;
1293                 change |= err;
1294         }
1295         kcontrol->private_value = (long)c;
1296         mutex_unlock(&codec->spdif_mutex);
1297         return err < 0 ? err : change;
1298 }
1299
1300 int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
1301                            unsigned int size, unsigned int __user *tlv)
1302 {
1303         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1304         struct hda_bind_ctls *c;
1305         int err;
1306
1307         mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
1308         c = (struct hda_bind_ctls *)kcontrol->private_value;
1309         kcontrol->private_value = *c->values;
1310         err = c->ops->tlv(kcontrol, op_flag, size, tlv);
1311         kcontrol->private_value = (long)c;
1312         mutex_unlock(&codec->spdif_mutex);
1313         return err;
1314 }
1315
1316 struct hda_ctl_ops snd_hda_bind_vol = {
1317         .info = snd_hda_mixer_amp_volume_info,
1318         .get = snd_hda_mixer_amp_volume_get,
1319         .put = snd_hda_mixer_amp_volume_put,
1320         .tlv = snd_hda_mixer_amp_tlv
1321 };
1322
1323 struct hda_ctl_ops snd_hda_bind_sw = {
1324         .info = snd_hda_mixer_amp_switch_info,
1325         .get = snd_hda_mixer_amp_switch_get,
1326         .put = snd_hda_mixer_amp_switch_put,
1327         .tlv = snd_hda_mixer_amp_tlv
1328 };
1329
1330 /*
1331  * SPDIF out controls
1332  */
1333
1334 static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
1335                                    struct snd_ctl_elem_info *uinfo)
1336 {
1337         uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1338         uinfo->count = 1;
1339         return 0;
1340 }
1341
1342 static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
1343                                    struct snd_ctl_elem_value *ucontrol)
1344 {
1345         ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
1346                                            IEC958_AES0_NONAUDIO |
1347                                            IEC958_AES0_CON_EMPHASIS_5015 |
1348                                            IEC958_AES0_CON_NOT_COPYRIGHT;
1349         ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
1350                                            IEC958_AES1_CON_ORIGINAL;
1351         return 0;
1352 }
1353
1354 static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
1355                                    struct snd_ctl_elem_value *ucontrol)
1356 {
1357         ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
1358                                            IEC958_AES0_NONAUDIO |
1359                                            IEC958_AES0_PRO_EMPHASIS_5015;
1360         return 0;
1361 }
1362
1363 static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
1364                                      struct snd_ctl_elem_value *ucontrol)
1365 {
1366         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1367
1368         ucontrol->value.iec958.status[0] = codec->spdif_status & 0xff;
1369         ucontrol->value.iec958.status[1] = (codec->spdif_status >> 8) & 0xff;
1370         ucontrol->value.iec958.status[2] = (codec->spdif_status >> 16) & 0xff;
1371         ucontrol->value.iec958.status[3] = (codec->spdif_status >> 24) & 0xff;
1372
1373         return 0;
1374 }
1375
1376 /* convert from SPDIF status bits to HDA SPDIF bits
1377  * bit 0 (DigEn) is always set zero (to be filled later)
1378  */
1379 static unsigned short convert_from_spdif_status(unsigned int sbits)
1380 {
1381         unsigned short val = 0;
1382
1383         if (sbits & IEC958_AES0_PROFESSIONAL)
1384                 val |= AC_DIG1_PROFESSIONAL;
1385         if (sbits & IEC958_AES0_NONAUDIO)
1386                 val |= AC_DIG1_NONAUDIO;
1387         if (sbits & IEC958_AES0_PROFESSIONAL) {
1388                 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
1389                     IEC958_AES0_PRO_EMPHASIS_5015)
1390                         val |= AC_DIG1_EMPHASIS;
1391         } else {
1392                 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
1393                     IEC958_AES0_CON_EMPHASIS_5015)
1394                         val |= AC_DIG1_EMPHASIS;
1395                 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
1396                         val |= AC_DIG1_COPYRIGHT;
1397                 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
1398                         val |= AC_DIG1_LEVEL;
1399                 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
1400         }
1401         return val;
1402 }
1403
1404 /* convert to SPDIF status bits from HDA SPDIF bits
1405  */
1406 static unsigned int convert_to_spdif_status(unsigned short val)
1407 {
1408         unsigned int sbits = 0;
1409
1410         if (val & AC_DIG1_NONAUDIO)
1411                 sbits |= IEC958_AES0_NONAUDIO;
1412         if (val & AC_DIG1_PROFESSIONAL)
1413                 sbits |= IEC958_AES0_PROFESSIONAL;
1414         if (sbits & IEC958_AES0_PROFESSIONAL) {
1415                 if (sbits & AC_DIG1_EMPHASIS)
1416                         sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
1417         } else {
1418                 if (val & AC_DIG1_EMPHASIS)
1419                         sbits |= IEC958_AES0_CON_EMPHASIS_5015;
1420                 if (!(val & AC_DIG1_COPYRIGHT))
1421                         sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
1422                 if (val & AC_DIG1_LEVEL)
1423                         sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
1424                 sbits |= val & (0x7f << 8);
1425         }
1426         return sbits;
1427 }
1428
1429 /* set digital convert verbs both for the given NID and its slaves */
1430 static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
1431                         int verb, int val)
1432 {
1433         hda_nid_t *d;
1434
1435         snd_hda_codec_write(codec, nid, 0, verb, val);
1436         d = codec->slave_dig_outs;
1437         if (!d)
1438                 return;
1439         for (; *d; d++)
1440                 snd_hda_codec_write(codec, *d, 0, verb, val);
1441 }
1442
1443 static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
1444                                        int dig1, int dig2)
1445 {
1446         if (dig1 != -1)
1447                 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1);
1448         if (dig2 != -1)
1449                 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2);
1450 }
1451
1452 static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
1453                                      struct snd_ctl_elem_value *ucontrol)
1454 {
1455         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1456         hda_nid_t nid = kcontrol->private_value;
1457         unsigned short val;
1458         int change;
1459
1460         mutex_lock(&codec->spdif_mutex);
1461         codec->spdif_status = ucontrol->value.iec958.status[0] |
1462                 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
1463                 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
1464                 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
1465         val = convert_from_spdif_status(codec->spdif_status);
1466         val |= codec->spdif_ctls & 1;
1467         change = codec->spdif_ctls != val;
1468         codec->spdif_ctls = val;
1469
1470         if (change)
1471                 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
1472
1473         mutex_unlock(&codec->spdif_mutex);
1474         return change;
1475 }
1476
1477 #define snd_hda_spdif_out_switch_info   snd_ctl_boolean_mono_info
1478
1479 static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
1480                                         struct snd_ctl_elem_value *ucontrol)
1481 {
1482         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1483
1484         ucontrol->value.integer.value[0] = codec->spdif_ctls & AC_DIG1_ENABLE;
1485         return 0;
1486 }
1487
1488 static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
1489                                         struct snd_ctl_elem_value *ucontrol)
1490 {
1491         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1492         hda_nid_t nid = kcontrol->private_value;
1493         unsigned short val;
1494         int change;
1495
1496         mutex_lock(&codec->spdif_mutex);
1497         val = codec->spdif_ctls & ~AC_DIG1_ENABLE;
1498         if (ucontrol->value.integer.value[0])
1499                 val |= AC_DIG1_ENABLE;
1500         change = codec->spdif_ctls != val;
1501         if (change) {
1502                 codec->spdif_ctls = val;
1503                 set_dig_out_convert(codec, nid, val & 0xff, -1);
1504                 /* unmute amp switch (if any) */
1505                 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
1506                     (val & AC_DIG1_ENABLE))
1507                         snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
1508                                                  HDA_AMP_MUTE, 0);
1509         }
1510         mutex_unlock(&codec->spdif_mutex);
1511         return change;
1512 }
1513
1514 static struct snd_kcontrol_new dig_mixes[] = {
1515         {
1516                 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1517                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1518                 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,CON_MASK),
1519                 .info = snd_hda_spdif_mask_info,
1520                 .get = snd_hda_spdif_cmask_get,
1521         },
1522         {
1523                 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1524                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1525                 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,PRO_MASK),
1526                 .info = snd_hda_spdif_mask_info,
1527                 .get = snd_hda_spdif_pmask_get,
1528         },
1529         {
1530                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1531                 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
1532                 .info = snd_hda_spdif_mask_info,
1533                 .get = snd_hda_spdif_default_get,
1534                 .put = snd_hda_spdif_default_put,
1535         },
1536         {
1537                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1538                 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,SWITCH),
1539                 .info = snd_hda_spdif_out_switch_info,
1540                 .get = snd_hda_spdif_out_switch_get,
1541                 .put = snd_hda_spdif_out_switch_put,
1542         },
1543         { } /* end */
1544 };
1545
1546 #define SPDIF_MAX_IDX   4       /* 4 instances should be enough to probe */
1547
1548 /**
1549  * snd_hda_create_spdif_out_ctls - create Output SPDIF-related controls
1550  * @codec: the HDA codec
1551  * @nid: audio out widget NID
1552  *
1553  * Creates controls related with the SPDIF output.
1554  * Called from each patch supporting the SPDIF out.
1555  *
1556  * Returns 0 if successful, or a negative error code.
1557  */
1558 int snd_hda_create_spdif_out_ctls(struct hda_codec *codec, hda_nid_t nid)
1559 {
1560         int err;
1561         struct snd_kcontrol *kctl;
1562         struct snd_kcontrol_new *dig_mix;
1563         int idx;
1564
1565         for (idx = 0; idx < SPDIF_MAX_IDX; idx++) {
1566                 if (!_snd_hda_find_mixer_ctl(codec, "IEC958 Playback Switch",
1567                                              idx))
1568                         break;
1569         }
1570         if (idx >= SPDIF_MAX_IDX) {
1571                 printk(KERN_ERR "hda_codec: too many IEC958 outputs\n");
1572                 return -EBUSY;
1573         }
1574         for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
1575                 kctl = snd_ctl_new1(dig_mix, codec);
1576                 kctl->id.index = idx;
1577                 kctl->private_value = nid;
1578                 err = snd_ctl_add(codec->bus->card, kctl);
1579                 if (err < 0)
1580                         return err;
1581         }
1582         codec->spdif_ctls =
1583                 snd_hda_codec_read(codec, nid, 0,
1584                                    AC_VERB_GET_DIGI_CONVERT_1, 0);
1585         codec->spdif_status = convert_to_spdif_status(codec->spdif_ctls);
1586         return 0;
1587 }
1588
1589 /*
1590  * SPDIF sharing with analog output
1591  */
1592 static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
1593                               struct snd_ctl_elem_value *ucontrol)
1594 {
1595         struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
1596         ucontrol->value.integer.value[0] = mout->share_spdif;
1597         return 0;
1598 }
1599
1600 static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
1601                               struct snd_ctl_elem_value *ucontrol)
1602 {
1603         struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
1604         mout->share_spdif = !!ucontrol->value.integer.value[0];
1605         return 0;
1606 }
1607
1608 static struct snd_kcontrol_new spdif_share_sw = {
1609         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1610         .name = "IEC958 Default PCM Playback Switch",
1611         .info = snd_ctl_boolean_mono_info,
1612         .get = spdif_share_sw_get,
1613         .put = spdif_share_sw_put,
1614 };
1615
1616 int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
1617                                   struct hda_multi_out *mout)
1618 {
1619         if (!mout->dig_out_nid)
1620                 return 0;
1621         /* ATTENTION: here mout is passed as private_data, instead of codec */
1622         return snd_ctl_add(codec->bus->card,
1623                            snd_ctl_new1(&spdif_share_sw, mout));
1624 }
1625
1626 /*
1627  * SPDIF input
1628  */
1629
1630 #define snd_hda_spdif_in_switch_info    snd_hda_spdif_out_switch_info
1631
1632 static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
1633                                        struct snd_ctl_elem_value *ucontrol)
1634 {
1635         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1636
1637         ucontrol->value.integer.value[0] = codec->spdif_in_enable;
1638         return 0;
1639 }
1640
1641 static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
1642                                        struct snd_ctl_elem_value *ucontrol)
1643 {
1644         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1645         hda_nid_t nid = kcontrol->private_value;
1646         unsigned int val = !!ucontrol->value.integer.value[0];
1647         int change;
1648
1649         mutex_lock(&codec->spdif_mutex);
1650         change = codec->spdif_in_enable != val;
1651         if (change) {
1652                 codec->spdif_in_enable = val;
1653                 snd_hda_codec_write_cache(codec, nid, 0,
1654                                           AC_VERB_SET_DIGI_CONVERT_1, val);
1655         }
1656         mutex_unlock(&codec->spdif_mutex);
1657         return change;
1658 }
1659
1660 static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
1661                                        struct snd_ctl_elem_value *ucontrol)
1662 {
1663         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1664         hda_nid_t nid = kcontrol->private_value;
1665         unsigned short val;
1666         unsigned int sbits;
1667
1668         val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0);
1669         sbits = convert_to_spdif_status(val);
1670         ucontrol->value.iec958.status[0] = sbits;
1671         ucontrol->value.iec958.status[1] = sbits >> 8;
1672         ucontrol->value.iec958.status[2] = sbits >> 16;
1673         ucontrol->value.iec958.status[3] = sbits >> 24;
1674         return 0;
1675 }
1676
1677 static struct snd_kcontrol_new dig_in_ctls[] = {
1678         {
1679                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1680                 .name = SNDRV_CTL_NAME_IEC958("",CAPTURE,SWITCH),
1681                 .info = snd_hda_spdif_in_switch_info,
1682                 .get = snd_hda_spdif_in_switch_get,
1683                 .put = snd_hda_spdif_in_switch_put,
1684         },
1685         {
1686                 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1687                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1688                 .name = SNDRV_CTL_NAME_IEC958("",CAPTURE,DEFAULT),
1689                 .info = snd_hda_spdif_mask_info,
1690                 .get = snd_hda_spdif_in_status_get,
1691         },
1692         { } /* end */
1693 };
1694
1695 /**
1696  * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
1697  * @codec: the HDA codec
1698  * @nid: audio in widget NID
1699  *
1700  * Creates controls related with the SPDIF input.
1701  * Called from each patch supporting the SPDIF in.
1702  *
1703  * Returns 0 if successful, or a negative error code.
1704  */
1705 int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
1706 {
1707         int err;
1708         struct snd_kcontrol *kctl;
1709         struct snd_kcontrol_new *dig_mix;
1710         int idx;
1711
1712         for (idx = 0; idx < SPDIF_MAX_IDX; idx++) {
1713                 if (!_snd_hda_find_mixer_ctl(codec, "IEC958 Capture Switch",
1714                                              idx))
1715                         break;
1716         }
1717         if (idx >= SPDIF_MAX_IDX) {
1718                 printk(KERN_ERR "hda_codec: too many IEC958 inputs\n");
1719                 return -EBUSY;
1720         }
1721         for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
1722                 kctl = snd_ctl_new1(dig_mix, codec);
1723                 kctl->private_value = nid;
1724                 err = snd_ctl_add(codec->bus->card, kctl);
1725                 if (err < 0)
1726                         return err;
1727         }
1728         codec->spdif_in_enable =
1729                 snd_hda_codec_read(codec, nid, 0,
1730                                    AC_VERB_GET_DIGI_CONVERT_1, 0) &
1731                 AC_DIG1_ENABLE;
1732         return 0;
1733 }
1734
1735 #ifdef SND_HDA_NEEDS_RESUME
1736 /*
1737  * command cache
1738  */
1739
1740 /* build a 32bit cache key with the widget id and the command parameter */
1741 #define build_cmd_cache_key(nid, verb)  ((verb << 8) | nid)
1742 #define get_cmd_cache_nid(key)          ((key) & 0xff)
1743 #define get_cmd_cache_cmd(key)          (((key) >> 8) & 0xffff)
1744
1745 /**
1746  * snd_hda_codec_write_cache - send a single command with caching
1747  * @codec: the HDA codec
1748  * @nid: NID to send the command
1749  * @direct: direct flag
1750  * @verb: the verb to send
1751  * @parm: the parameter for the verb
1752  *
1753  * Send a single command without waiting for response.
1754  *
1755  * Returns 0 if successful, or a negative error code.
1756  */
1757 int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
1758                               int direct, unsigned int verb, unsigned int parm)
1759 {
1760         int err;
1761         snd_hda_power_up(codec);
1762         mutex_lock(&codec->bus->cmd_mutex);
1763         err = codec->bus->ops.command(codec, nid, direct, verb, parm);
1764         if (!err) {
1765                 struct hda_cache_head *c;
1766                 u32 key = build_cmd_cache_key(nid, verb);
1767                 c = get_alloc_hash(&codec->cmd_cache, key);
1768                 if (c)
1769                         c->val = parm;
1770         }
1771         mutex_unlock(&codec->bus->cmd_mutex);
1772         snd_hda_power_down(codec);
1773         return err;
1774 }
1775
1776 /* resume the all commands from the cache */
1777 void snd_hda_codec_resume_cache(struct hda_codec *codec)
1778 {
1779         struct hda_cache_head *buffer = codec->cmd_cache.buffer;
1780         int i;
1781
1782         for (i = 0; i < codec->cmd_cache.size; i++, buffer++) {
1783                 u32 key = buffer->key;
1784                 if (!key)
1785                         continue;
1786                 snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
1787                                     get_cmd_cache_cmd(key), buffer->val);
1788         }
1789 }
1790
1791 /**
1792  * snd_hda_sequence_write_cache - sequence writes with caching
1793  * @codec: the HDA codec
1794  * @seq: VERB array to send
1795  *
1796  * Send the commands sequentially from the given array.
1797  * Thte commands are recorded on cache for power-save and resume.
1798  * The array must be terminated with NID=0.
1799  */
1800 void snd_hda_sequence_write_cache(struct hda_codec *codec,
1801                                   const struct hda_verb *seq)
1802 {
1803         for (; seq->nid; seq++)
1804                 snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
1805                                           seq->param);
1806 }
1807 #endif /* SND_HDA_NEEDS_RESUME */
1808
1809 /*
1810  * set power state of the codec
1811  */
1812 static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
1813                                 unsigned int power_state)
1814 {
1815         hda_nid_t nid;
1816         int i;
1817
1818         snd_hda_codec_write(codec, fg, 0, AC_VERB_SET_POWER_STATE,
1819                             power_state);
1820         msleep(10); /* partial workaround for "azx_get_response timeout" */
1821
1822         nid = codec->start_nid;
1823         for (i = 0; i < codec->num_nodes; i++, nid++) {
1824                 unsigned int wcaps = get_wcaps(codec, nid);
1825                 if (wcaps & AC_WCAP_POWER) {
1826                         unsigned int wid_type = (wcaps & AC_WCAP_TYPE) >>
1827                                 AC_WCAP_TYPE_SHIFT;
1828                         if (wid_type == AC_WID_PIN) {
1829                                 unsigned int pincap;
1830                                 /*
1831                                  * don't power down the widget if it controls
1832                                  * eapd and EAPD_BTLENABLE is set.
1833                                  */
1834                                 pincap = snd_hda_param_read(codec, nid,
1835                                                             AC_PAR_PIN_CAP);
1836                                 if (pincap & AC_PINCAP_EAPD) {
1837                                         int eapd = snd_hda_codec_read(codec,
1838                                                 nid, 0,
1839                                                 AC_VERB_GET_EAPD_BTLENABLE, 0);
1840                                         eapd &= 0x02;
1841                                         if (power_state == AC_PWRST_D3 && eapd)
1842                                                 continue;
1843                                 }
1844                         }
1845                         snd_hda_codec_write(codec, nid, 0,
1846                                             AC_VERB_SET_POWER_STATE,
1847                                             power_state);
1848                 }
1849         }
1850
1851         if (power_state == AC_PWRST_D0) {
1852                 unsigned long end_time;
1853                 int state;
1854                 msleep(10);
1855                 /* wait until the codec reachs to D0 */
1856                 end_time = jiffies + msecs_to_jiffies(500);
1857                 do {
1858                         state = snd_hda_codec_read(codec, fg, 0,
1859                                                    AC_VERB_GET_POWER_STATE, 0);
1860                         if (state == power_state)
1861                                 break;
1862                         msleep(1);
1863                 } while (time_after_eq(end_time, jiffies));
1864         }
1865 }
1866
1867 #ifdef SND_HDA_NEEDS_RESUME
1868 /*
1869  * call suspend and power-down; used both from PM and power-save
1870  */
1871 static void hda_call_codec_suspend(struct hda_codec *codec)
1872 {
1873         if (codec->patch_ops.suspend)
1874                 codec->patch_ops.suspend(codec, PMSG_SUSPEND);
1875         hda_set_power_state(codec,
1876                             codec->afg ? codec->afg : codec->mfg,
1877                             AC_PWRST_D3);
1878 #ifdef CONFIG_SND_HDA_POWER_SAVE
1879         cancel_delayed_work(&codec->power_work);
1880         codec->power_on = 0;
1881         codec->power_transition = 0;
1882 #endif
1883 }
1884
1885 /*
1886  * kick up codec; used both from PM and power-save
1887  */
1888 static void hda_call_codec_resume(struct hda_codec *codec)
1889 {
1890         hda_set_power_state(codec,
1891                             codec->afg ? codec->afg : codec->mfg,
1892                             AC_PWRST_D0);
1893         if (codec->patch_ops.resume)
1894                 codec->patch_ops.resume(codec);
1895         else {
1896                 if (codec->patch_ops.init)
1897                         codec->patch_ops.init(codec);
1898                 snd_hda_codec_resume_amp(codec);
1899                 snd_hda_codec_resume_cache(codec);
1900         }
1901 }
1902 #endif /* SND_HDA_NEEDS_RESUME */
1903
1904
1905 /**
1906  * snd_hda_build_controls - build mixer controls
1907  * @bus: the BUS
1908  *
1909  * Creates mixer controls for each codec included in the bus.
1910  *
1911  * Returns 0 if successful, otherwise a negative error code.
1912  */
1913 int __devinit snd_hda_build_controls(struct hda_bus *bus)
1914 {
1915         struct hda_codec *codec;
1916
1917         list_for_each_entry(codec, &bus->codec_list, list) {
1918                 int err = 0;
1919                 /* fake as if already powered-on */
1920                 hda_keep_power_on(codec);
1921                 /* then fire up */
1922                 hda_set_power_state(codec,
1923                                     codec->afg ? codec->afg : codec->mfg,
1924                                     AC_PWRST_D0);
1925                 /* continue to initialize... */
1926                 if (codec->patch_ops.init)
1927                         err = codec->patch_ops.init(codec);
1928                 if (!err && codec->patch_ops.build_controls)
1929                         err = codec->patch_ops.build_controls(codec);
1930                 snd_hda_power_down(codec);
1931                 if (err < 0)
1932                         return err;
1933         }
1934
1935         return 0;
1936 }
1937
1938 /*
1939  * stream formats
1940  */
1941 struct hda_rate_tbl {
1942         unsigned int hz;
1943         unsigned int alsa_bits;
1944         unsigned int hda_fmt;
1945 };
1946
1947 static struct hda_rate_tbl rate_bits[] = {
1948         /* rate in Hz, ALSA rate bitmask, HDA format value */
1949
1950         /* autodetected value used in snd_hda_query_supported_pcm */
1951         { 8000, SNDRV_PCM_RATE_8000, 0x0500 }, /* 1/6 x 48 */
1952         { 11025, SNDRV_PCM_RATE_11025, 0x4300 }, /* 1/4 x 44 */
1953         { 16000, SNDRV_PCM_RATE_16000, 0x0200 }, /* 1/3 x 48 */
1954         { 22050, SNDRV_PCM_RATE_22050, 0x4100 }, /* 1/2 x 44 */
1955         { 32000, SNDRV_PCM_RATE_32000, 0x0a00 }, /* 2/3 x 48 */
1956         { 44100, SNDRV_PCM_RATE_44100, 0x4000 }, /* 44 */
1957         { 48000, SNDRV_PCM_RATE_48000, 0x0000 }, /* 48 */
1958         { 88200, SNDRV_PCM_RATE_88200, 0x4800 }, /* 2 x 44 */
1959         { 96000, SNDRV_PCM_RATE_96000, 0x0800 }, /* 2 x 48 */
1960         { 176400, SNDRV_PCM_RATE_176400, 0x5800 },/* 4 x 44 */
1961         { 192000, SNDRV_PCM_RATE_192000, 0x1800 }, /* 4 x 48 */
1962 #define AC_PAR_PCM_RATE_BITS    11
1963         /* up to bits 10, 384kHZ isn't supported properly */
1964
1965         /* not autodetected value */
1966         { 9600, SNDRV_PCM_RATE_KNOT, 0x0400 }, /* 1/5 x 48 */
1967
1968         { 0 } /* terminator */
1969 };
1970
1971 /**
1972  * snd_hda_calc_stream_format - calculate format bitset
1973  * @rate: the sample rate
1974  * @channels: the number of channels
1975  * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
1976  * @maxbps: the max. bps
1977  *
1978  * Calculate the format bitset from the given rate, channels and th PCM format.
1979  *
1980  * Return zero if invalid.
1981  */
1982 unsigned int snd_hda_calc_stream_format(unsigned int rate,
1983                                         unsigned int channels,
1984                                         unsigned int format,
1985                                         unsigned int maxbps)
1986 {
1987         int i;
1988         unsigned int val = 0;
1989
1990         for (i = 0; rate_bits[i].hz; i++)
1991                 if (rate_bits[i].hz == rate) {
1992                         val = rate_bits[i].hda_fmt;
1993                         break;
1994                 }
1995         if (!rate_bits[i].hz) {
1996                 snd_printdd("invalid rate %d\n", rate);
1997                 return 0;
1998         }
1999
2000         if (channels == 0 || channels > 8) {
2001                 snd_printdd("invalid channels %d\n", channels);
2002                 return 0;
2003         }
2004         val |= channels - 1;
2005
2006         switch (snd_pcm_format_width(format)) {
2007         case 8:  val |= 0x00; break;
2008         case 16: val |= 0x10; break;
2009         case 20:
2010         case 24:
2011         case 32:
2012                 if (maxbps >= 32)
2013                         val |= 0x40;
2014                 else if (maxbps >= 24)
2015                         val |= 0x30;
2016                 else
2017                         val |= 0x20;
2018                 break;
2019         default:
2020                 snd_printdd("invalid format width %d\n",
2021                             snd_pcm_format_width(format));
2022                 return 0;
2023         }
2024
2025         return val;
2026 }
2027
2028 /**
2029  * snd_hda_query_supported_pcm - query the supported PCM rates and formats
2030  * @codec: the HDA codec
2031  * @nid: NID to query
2032  * @ratesp: the pointer to store the detected rate bitflags
2033  * @formatsp: the pointer to store the detected formats
2034  * @bpsp: the pointer to store the detected format widths
2035  *
2036  * Queries the supported PCM rates and formats.  The NULL @ratesp, @formatsp
2037  * or @bsps argument is ignored.
2038  *
2039  * Returns 0 if successful, otherwise a negative error code.
2040  */
2041 int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
2042                                 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
2043 {
2044         int i;
2045         unsigned int val, streams;
2046
2047         val = 0;
2048         if (nid != codec->afg &&
2049             (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD)) {
2050                 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
2051                 if (val == -1)
2052                         return -EIO;
2053         }
2054         if (!val)
2055                 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
2056
2057         if (ratesp) {
2058                 u32 rates = 0;
2059                 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
2060                         if (val & (1 << i))
2061                                 rates |= rate_bits[i].alsa_bits;
2062                 }
2063                 *ratesp = rates;
2064         }
2065
2066         if (formatsp || bpsp) {
2067                 u64 formats = 0;
2068                 unsigned int bps;
2069                 unsigned int wcaps;
2070
2071                 wcaps = get_wcaps(codec, nid);
2072                 streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
2073                 if (streams == -1)
2074                         return -EIO;
2075                 if (!streams) {
2076                         streams = snd_hda_param_read(codec, codec->afg,
2077                                                      AC_PAR_STREAM);
2078                         if (streams == -1)
2079                                 return -EIO;
2080                 }
2081
2082                 bps = 0;
2083                 if (streams & AC_SUPFMT_PCM) {
2084                         if (val & AC_SUPPCM_BITS_8) {
2085                                 formats |= SNDRV_PCM_FMTBIT_U8;
2086                                 bps = 8;
2087                         }
2088                         if (val & AC_SUPPCM_BITS_16) {
2089                                 formats |= SNDRV_PCM_FMTBIT_S16_LE;
2090                                 bps = 16;
2091                         }
2092                         if (wcaps & AC_WCAP_DIGITAL) {
2093                                 if (val & AC_SUPPCM_BITS_32)
2094                                         formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
2095                                 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
2096                                         formats |= SNDRV_PCM_FMTBIT_S32_LE;
2097                                 if (val & AC_SUPPCM_BITS_24)
2098                                         bps = 24;
2099                                 else if (val & AC_SUPPCM_BITS_20)
2100                                         bps = 20;
2101                         } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
2102                                           AC_SUPPCM_BITS_32)) {
2103                                 formats |= SNDRV_PCM_FMTBIT_S32_LE;
2104                                 if (val & AC_SUPPCM_BITS_32)
2105                                         bps = 32;
2106                                 else if (val & AC_SUPPCM_BITS_24)
2107                                         bps = 24;
2108                                 else if (val & AC_SUPPCM_BITS_20)
2109                                         bps = 20;
2110                         }
2111                 }
2112                 else if (streams == AC_SUPFMT_FLOAT32) {
2113                         /* should be exclusive */
2114                         formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
2115                         bps = 32;
2116                 } else if (streams == AC_SUPFMT_AC3) {
2117                         /* should be exclusive */
2118                         /* temporary hack: we have still no proper support
2119                          * for the direct AC3 stream...
2120                          */
2121                         formats |= SNDRV_PCM_FMTBIT_U8;
2122                         bps = 8;
2123                 }
2124                 if (formatsp)
2125                         *formatsp = formats;
2126                 if (bpsp)
2127                         *bpsp = bps;
2128         }
2129
2130         return 0;
2131 }
2132
2133 /**
2134  * snd_hda_is_supported_format - check whether the given node supports
2135  * the format val
2136  *
2137  * Returns 1 if supported, 0 if not.
2138  */
2139 int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
2140                                 unsigned int format)
2141 {
2142         int i;
2143         unsigned int val = 0, rate, stream;
2144
2145         if (nid != codec->afg &&
2146             (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD)) {
2147                 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
2148                 if (val == -1)
2149                         return 0;
2150         }
2151         if (!val) {
2152                 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
2153                 if (val == -1)
2154                         return 0;
2155         }
2156
2157         rate = format & 0xff00;
2158         for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
2159                 if (rate_bits[i].hda_fmt == rate) {
2160                         if (val & (1 << i))
2161                                 break;
2162                         return 0;
2163                 }
2164         if (i >= AC_PAR_PCM_RATE_BITS)
2165                 return 0;
2166
2167         stream = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
2168         if (stream == -1)
2169                 return 0;
2170         if (!stream && nid != codec->afg)
2171                 stream = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
2172         if (!stream || stream == -1)
2173                 return 0;
2174
2175         if (stream & AC_SUPFMT_PCM) {
2176                 switch (format & 0xf0) {
2177                 case 0x00:
2178                         if (!(val & AC_SUPPCM_BITS_8))
2179                                 return 0;
2180                         break;
2181                 case 0x10:
2182                         if (!(val & AC_SUPPCM_BITS_16))
2183                                 return 0;
2184                         break;
2185                 case 0x20:
2186                         if (!(val & AC_SUPPCM_BITS_20))
2187                                 return 0;
2188                         break;
2189                 case 0x30:
2190                         if (!(val & AC_SUPPCM_BITS_24))
2191                                 return 0;
2192                         break;
2193                 case 0x40:
2194                         if (!(val & AC_SUPPCM_BITS_32))
2195                                 return 0;
2196                         break;
2197                 default:
2198                         return 0;
2199                 }
2200         } else {
2201                 /* FIXME: check for float32 and AC3? */
2202         }
2203
2204         return 1;
2205 }
2206
2207 /*
2208  * PCM stuff
2209  */
2210 static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
2211                                       struct hda_codec *codec,
2212                                       struct snd_pcm_substream *substream)
2213 {
2214         return 0;
2215 }
2216
2217 static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
2218                                    struct hda_codec *codec,
2219                                    unsigned int stream_tag,
2220                                    unsigned int format,
2221                                    struct snd_pcm_substream *substream)
2222 {
2223         snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
2224         return 0;
2225 }
2226
2227 static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
2228                                    struct hda_codec *codec,
2229                                    struct snd_pcm_substream *substream)
2230 {
2231         snd_hda_codec_cleanup_stream(codec, hinfo->nid);
2232         return 0;
2233 }
2234
2235 static int __devinit set_pcm_default_values(struct hda_codec *codec,
2236                                             struct hda_pcm_stream *info)
2237 {
2238         /* query support PCM information from the given NID */
2239         if (info->nid && (!info->rates || !info->formats)) {
2240                 snd_hda_query_supported_pcm(codec, info->nid,
2241                                 info->rates ? NULL : &info->rates,
2242                                 info->formats ? NULL : &info->formats,
2243                                 info->maxbps ? NULL : &info->maxbps);
2244         }
2245         if (info->ops.open == NULL)
2246                 info->ops.open = hda_pcm_default_open_close;
2247         if (info->ops.close == NULL)
2248                 info->ops.close = hda_pcm_default_open_close;
2249         if (info->ops.prepare == NULL) {
2250                 if (snd_BUG_ON(!info->nid))
2251                         return -EINVAL;
2252                 info->ops.prepare = hda_pcm_default_prepare;
2253         }
2254         if (info->ops.cleanup == NULL) {
2255                 if (snd_BUG_ON(!info->nid))
2256                         return -EINVAL;
2257                 info->ops.cleanup = hda_pcm_default_cleanup;
2258         }
2259         return 0;
2260 }
2261
2262 /**
2263  * snd_hda_build_pcms - build PCM information
2264  * @bus: the BUS
2265  *
2266  * Create PCM information for each codec included in the bus.
2267  *
2268  * The build_pcms codec patch is requested to set up codec->num_pcms and
2269  * codec->pcm_info properly.  The array is referred by the top-level driver
2270  * to create its PCM instances.
2271  * The allocated codec->pcm_info should be released in codec->patch_ops.free
2272  * callback.
2273  *
2274  * At least, substreams, channels_min and channels_max must be filled for
2275  * each stream.  substreams = 0 indicates that the stream doesn't exist.
2276  * When rates and/or formats are zero, the supported values are queried
2277  * from the given nid.  The nid is used also by the default ops.prepare
2278  * and ops.cleanup callbacks.
2279  *
2280  * The driver needs to call ops.open in its open callback.  Similarly,
2281  * ops.close is supposed to be called in the close callback.
2282  * ops.prepare should be called in the prepare or hw_params callback
2283  * with the proper parameters for set up.
2284  * ops.cleanup should be called in hw_free for clean up of streams.
2285  *
2286  * This function returns 0 if successfull, or a negative error code.
2287  */
2288 int __devinit snd_hda_build_pcms(struct hda_bus *bus)
2289 {
2290         struct hda_codec *codec;
2291
2292         list_for_each_entry(codec, &bus->codec_list, list) {
2293                 unsigned int pcm, s;
2294                 int err;
2295                 if (!codec->patch_ops.build_pcms)
2296                         continue;
2297                 err = codec->patch_ops.build_pcms(codec);
2298                 if (err < 0)
2299                         return err;
2300                 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
2301                         for (s = 0; s < 2; s++) {
2302                                 struct hda_pcm_stream *info;
2303                                 info = &codec->pcm_info[pcm].stream[s];
2304                                 if (!info->substreams)
2305                                         continue;
2306                                 err = set_pcm_default_values(codec, info);
2307                                 if (err < 0)
2308                                         return err;
2309                         }
2310                 }
2311         }
2312         return 0;
2313 }
2314
2315 /**
2316  * snd_hda_check_board_config - compare the current codec with the config table
2317  * @codec: the HDA codec
2318  * @num_configs: number of config enums
2319  * @models: array of model name strings
2320  * @tbl: configuration table, terminated by null entries
2321  *
2322  * Compares the modelname or PCI subsystem id of the current codec with the
2323  * given configuration table.  If a matching entry is found, returns its
2324  * config value (supposed to be 0 or positive).
2325  *
2326  * If no entries are matching, the function returns a negative value.
2327  */
2328 int snd_hda_check_board_config(struct hda_codec *codec,
2329                                int num_configs, const char **models,
2330                                const struct snd_pci_quirk *tbl)
2331 {
2332         if (codec->bus->modelname && models) {
2333                 int i;
2334                 for (i = 0; i < num_configs; i++) {
2335                         if (models[i] &&
2336                             !strcmp(codec->bus->modelname, models[i])) {
2337                                 snd_printd(KERN_INFO "hda_codec: model '%s' is "
2338                                            "selected\n", models[i]);
2339                                 return i;
2340                         }
2341                 }
2342         }
2343
2344         if (!codec->bus->pci || !tbl)
2345                 return -1;
2346
2347         tbl = snd_pci_quirk_lookup(codec->bus->pci, tbl);
2348         if (!tbl)
2349                 return -1;
2350         if (tbl->value >= 0 && tbl->value < num_configs) {
2351 #ifdef CONFIG_SND_DEBUG_VERBOSE
2352                 char tmp[10];
2353                 const char *model = NULL;
2354                 if (models)
2355                         model = models[tbl->value];
2356                 if (!model) {
2357                         sprintf(tmp, "#%d", tbl->value);
2358                         model = tmp;
2359                 }
2360                 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
2361                             "for config %x:%x (%s)\n",
2362                             model, tbl->subvendor, tbl->subdevice,
2363                             (tbl->name ? tbl->name : "Unknown device"));
2364 #endif
2365                 return tbl->value;
2366         }
2367         return -1;
2368 }
2369
2370 /**
2371  * snd_hda_add_new_ctls - create controls from the array
2372  * @codec: the HDA codec
2373  * @knew: the array of struct snd_kcontrol_new
2374  *
2375  * This helper function creates and add new controls in the given array.
2376  * The array must be terminated with an empty entry as terminator.
2377  *
2378  * Returns 0 if successful, or a negative error code.
2379  */
2380 int snd_hda_add_new_ctls(struct hda_codec *codec, struct snd_kcontrol_new *knew)
2381 {
2382         int err;
2383
2384         for (; knew->name; knew++) {
2385                 struct snd_kcontrol *kctl;
2386                 kctl = snd_ctl_new1(knew, codec);
2387                 if (!kctl)
2388                         return -ENOMEM;
2389                 err = snd_ctl_add(codec->bus->card, kctl);
2390                 if (err < 0) {
2391                         if (!codec->addr)
2392                                 return err;
2393                         kctl = snd_ctl_new1(knew, codec);
2394                         if (!kctl)
2395                                 return -ENOMEM;
2396                         kctl->id.device = codec->addr;
2397                         err = snd_ctl_add(codec->bus->card, kctl);
2398                         if (err < 0)
2399                                 return err;
2400                 }
2401         }
2402         return 0;
2403 }
2404
2405 #ifdef CONFIG_SND_HDA_POWER_SAVE
2406 static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
2407                                 unsigned int power_state);
2408
2409 static void hda_power_work(struct work_struct *work)
2410 {
2411         struct hda_codec *codec =
2412                 container_of(work, struct hda_codec, power_work.work);
2413
2414         if (!codec->power_on || codec->power_count) {
2415                 codec->power_transition = 0;
2416                 return;
2417         }
2418
2419         hda_call_codec_suspend(codec);
2420         if (codec->bus->ops.pm_notify)
2421                 codec->bus->ops.pm_notify(codec);
2422 }
2423
2424 static void hda_keep_power_on(struct hda_codec *codec)
2425 {
2426         codec->power_count++;
2427         codec->power_on = 1;
2428 }
2429
2430 void snd_hda_power_up(struct hda_codec *codec)
2431 {
2432         codec->power_count++;
2433         if (codec->power_on || codec->power_transition)
2434                 return;
2435
2436         codec->power_on = 1;
2437         if (codec->bus->ops.pm_notify)
2438                 codec->bus->ops.pm_notify(codec);
2439         hda_call_codec_resume(codec);
2440         cancel_delayed_work(&codec->power_work);
2441         codec->power_transition = 0;
2442 }
2443
2444 void snd_hda_power_down(struct hda_codec *codec)
2445 {
2446         --codec->power_count;
2447         if (!codec->power_on || codec->power_count || codec->power_transition)
2448                 return;
2449         if (power_save) {
2450                 codec->power_transition = 1; /* avoid reentrance */
2451                 schedule_delayed_work(&codec->power_work,
2452                                       msecs_to_jiffies(power_save * 1000));
2453         }
2454 }
2455
2456 int snd_hda_check_amp_list_power(struct hda_codec *codec,
2457                                  struct hda_loopback_check *check,
2458                                  hda_nid_t nid)
2459 {
2460         struct hda_amp_list *p;
2461         int ch, v;
2462
2463         if (!check->amplist)
2464                 return 0;
2465         for (p = check->amplist; p->nid; p++) {
2466                 if (p->nid == nid)
2467                         break;
2468         }
2469         if (!p->nid)
2470                 return 0; /* nothing changed */
2471
2472         for (p = check->amplist; p->nid; p++) {
2473                 for (ch = 0; ch < 2; ch++) {
2474                         v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
2475                                                    p->idx);
2476                         if (!(v & HDA_AMP_MUTE) && v > 0) {
2477                                 if (!check->power_on) {
2478                                         check->power_on = 1;
2479                                         snd_hda_power_up(codec);
2480                                 }
2481                                 return 1;
2482                         }
2483                 }
2484         }
2485         if (check->power_on) {
2486                 check->power_on = 0;
2487                 snd_hda_power_down(codec);
2488         }
2489         return 0;
2490 }
2491 #endif
2492
2493 /*
2494  * Channel mode helper
2495  */
2496 int snd_hda_ch_mode_info(struct hda_codec *codec,
2497                          struct snd_ctl_elem_info *uinfo,
2498                          const struct hda_channel_mode *chmode,
2499                          int num_chmodes)
2500 {
2501         uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2502         uinfo->count = 1;
2503         uinfo->value.enumerated.items = num_chmodes;
2504         if (uinfo->value.enumerated.item >= num_chmodes)
2505                 uinfo->value.enumerated.item = num_chmodes - 1;
2506         sprintf(uinfo->value.enumerated.name, "%dch",
2507                 chmode[uinfo->value.enumerated.item].channels);
2508         return 0;
2509 }
2510
2511 int snd_hda_ch_mode_get(struct hda_codec *codec,
2512                         struct snd_ctl_elem_value *ucontrol,
2513                         const struct hda_channel_mode *chmode,
2514                         int num_chmodes,
2515                         int max_channels)
2516 {
2517         int i;
2518
2519         for (i = 0; i < num_chmodes; i++) {
2520                 if (max_channels == chmode[i].channels) {
2521                         ucontrol->value.enumerated.item[0] = i;
2522                         break;
2523                 }
2524         }
2525         return 0;
2526 }
2527
2528 int snd_hda_ch_mode_put(struct hda_codec *codec,
2529                         struct snd_ctl_elem_value *ucontrol,
2530                         const struct hda_channel_mode *chmode,
2531                         int num_chmodes,
2532                         int *max_channelsp)
2533 {
2534         unsigned int mode;
2535
2536         mode = ucontrol->value.enumerated.item[0];
2537         if (mode >= num_chmodes)
2538                 return -EINVAL;
2539         if (*max_channelsp == chmode[mode].channels)
2540                 return 0;
2541         /* change the current channel setting */
2542         *max_channelsp = chmode[mode].channels;
2543         if (chmode[mode].sequence)
2544                 snd_hda_sequence_write_cache(codec, chmode[mode].sequence);
2545         return 1;
2546 }
2547
2548 /*
2549  * input MUX helper
2550  */
2551 int snd_hda_input_mux_info(const struct hda_input_mux *imux,
2552                            struct snd_ctl_elem_info *uinfo)
2553 {
2554         unsigned int index;
2555
2556         uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2557         uinfo->count = 1;
2558         uinfo->value.enumerated.items = imux->num_items;
2559         if (!imux->num_items)
2560                 return 0;
2561         index = uinfo->value.enumerated.item;
2562         if (index >= imux->num_items)
2563                 index = imux->num_items - 1;
2564         strcpy(uinfo->value.enumerated.name, imux->items[index].label);
2565         return 0;
2566 }
2567
2568 int snd_hda_input_mux_put(struct hda_codec *codec,
2569                           const struct hda_input_mux *imux,
2570                           struct snd_ctl_elem_value *ucontrol,
2571                           hda_nid_t nid,
2572                           unsigned int *cur_val)
2573 {
2574         unsigned int idx;
2575
2576         if (!imux->num_items)
2577                 return 0;
2578         idx = ucontrol->value.enumerated.item[0];
2579         if (idx >= imux->num_items)
2580                 idx = imux->num_items - 1;
2581         if (*cur_val == idx)
2582                 return 0;
2583         snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
2584                                   imux->items[idx].index);
2585         *cur_val = idx;
2586         return 1;
2587 }
2588
2589
2590 /*
2591  * Multi-channel / digital-out PCM helper functions
2592  */
2593
2594 /* setup SPDIF output stream */
2595 static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
2596                                  unsigned int stream_tag, unsigned int format)
2597 {
2598         /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */
2599         if (codec->spdif_status_reset && (codec->spdif_ctls & AC_DIG1_ENABLE))
2600                 set_dig_out_convert(codec, nid, 
2601                                     codec->spdif_ctls & ~AC_DIG1_ENABLE & 0xff,
2602                                     -1);
2603         snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
2604         if (codec->slave_dig_outs) {
2605                 hda_nid_t *d;
2606                 for (d = codec->slave_dig_outs; *d; d++)
2607                         snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
2608                                                    format);
2609         }
2610         /* turn on again (if needed) */
2611         if (codec->spdif_status_reset && (codec->spdif_ctls & AC_DIG1_ENABLE))
2612                 set_dig_out_convert(codec, nid,
2613                                     codec->spdif_ctls & 0xff, -1);
2614 }
2615
2616 static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
2617 {
2618         snd_hda_codec_cleanup_stream(codec, nid);
2619         if (codec->slave_dig_outs) {
2620                 hda_nid_t *d;
2621                 for (d = codec->slave_dig_outs; *d; d++)
2622                         snd_hda_codec_cleanup_stream(codec, *d);
2623         }
2624 }
2625
2626 /*
2627  * open the digital out in the exclusive mode
2628  */
2629 int snd_hda_multi_out_dig_open(struct hda_codec *codec,
2630                                struct hda_multi_out *mout)
2631 {
2632         mutex_lock(&codec->spdif_mutex);
2633         if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
2634                 /* already opened as analog dup; reset it once */
2635                 cleanup_dig_out_stream(codec, mout->dig_out_nid);
2636         mout->dig_out_used = HDA_DIG_EXCLUSIVE;
2637         mutex_unlock(&codec->spdif_mutex);
2638         return 0;
2639 }
2640
2641 int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
2642                                   struct hda_multi_out *mout,
2643                                   unsigned int stream_tag,
2644                                   unsigned int format,
2645                                   struct snd_pcm_substream *substream)
2646 {
2647         mutex_lock(&codec->spdif_mutex);
2648         setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
2649         mutex_unlock(&codec->spdif_mutex);
2650         return 0;
2651 }
2652
2653 /*
2654  * release the digital out
2655  */
2656 int snd_hda_multi_out_dig_close(struct hda_codec *codec,
2657                                 struct hda_multi_out *mout)
2658 {
2659         mutex_lock(&codec->spdif_mutex);
2660         mout->dig_out_used = 0;
2661         mutex_unlock(&codec->spdif_mutex);
2662         return 0;
2663 }
2664
2665 /*
2666  * set up more restrictions for analog out
2667  */
2668 int snd_hda_multi_out_analog_open(struct hda_codec *codec,
2669                                   struct hda_multi_out *mout,
2670                                   struct snd_pcm_substream *substream,
2671                                   struct hda_pcm_stream *hinfo)
2672 {
2673         struct snd_pcm_runtime *runtime = substream->runtime;
2674         runtime->hw.channels_max = mout->max_channels;
2675         if (mout->dig_out_nid) {
2676                 if (!mout->analog_rates) {
2677                         mout->analog_rates = hinfo->rates;
2678                         mout->analog_formats = hinfo->formats;
2679                         mout->analog_maxbps = hinfo->maxbps;
2680                 } else {
2681                         runtime->hw.rates = mout->analog_rates;
2682                         runtime->hw.formats = mout->analog_formats;
2683                         hinfo->maxbps = mout->analog_maxbps;
2684                 }
2685                 if (!mout->spdif_rates) {
2686                         snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
2687                                                     &mout->spdif_rates,
2688                                                     &mout->spdif_formats,
2689                                                     &mout->spdif_maxbps);
2690                 }
2691                 mutex_lock(&codec->spdif_mutex);
2692                 if (mout->share_spdif) {
2693                         runtime->hw.rates &= mout->spdif_rates;
2694                         runtime->hw.formats &= mout->spdif_formats;
2695                         if (mout->spdif_maxbps < hinfo->maxbps)
2696                                 hinfo->maxbps = mout->spdif_maxbps;
2697                 }
2698                 mutex_unlock(&codec->spdif_mutex);
2699         }
2700         return snd_pcm_hw_constraint_step(substream->runtime, 0,
2701                                           SNDRV_PCM_HW_PARAM_CHANNELS, 2);
2702 }
2703
2704 /*
2705  * set up the i/o for analog out
2706  * when the digital out is available, copy the front out to digital out, too.
2707  */
2708 int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
2709                                      struct hda_multi_out *mout,
2710                                      unsigned int stream_tag,
2711                                      unsigned int format,
2712                                      struct snd_pcm_substream *substream)
2713 {
2714         hda_nid_t *nids = mout->dac_nids;
2715         int chs = substream->runtime->channels;
2716         int i;
2717
2718         mutex_lock(&codec->spdif_mutex);
2719         if (mout->dig_out_nid && mout->share_spdif &&
2720             mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
2721                 if (chs == 2 &&
2722                     snd_hda_is_supported_format(codec, mout->dig_out_nid,
2723                                                 format) &&
2724                     !(codec->spdif_status & IEC958_AES0_NONAUDIO)) {
2725                         mout->dig_out_used = HDA_DIG_ANALOG_DUP;
2726                         setup_dig_out_stream(codec, mout->dig_out_nid,
2727                                              stream_tag, format);
2728                 } else {
2729                         mout->dig_out_used = 0;
2730                         cleanup_dig_out_stream(codec, mout->dig_out_nid);
2731                 }
2732         }
2733         mutex_unlock(&codec->spdif_mutex);
2734
2735         /* front */
2736         snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
2737                                    0, format);
2738         if (!mout->no_share_stream &&
2739             mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
2740                 /* headphone out will just decode front left/right (stereo) */
2741                 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
2742                                            0, format);
2743         /* extra outputs copied from front */
2744         for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
2745                 if (!mout->no_share_stream && mout->extra_out_nid[i])
2746                         snd_hda_codec_setup_stream(codec,
2747                                                    mout->extra_out_nid[i],
2748                                                    stream_tag, 0, format);
2749
2750         /* surrounds */
2751         for (i = 1; i < mout->num_dacs; i++) {
2752                 if (chs >= (i + 1) * 2) /* independent out */
2753                         snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
2754                                                    i * 2, format);
2755                 else if (!mout->no_share_stream) /* copy front */
2756                         snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
2757                                                    0, format);
2758         }
2759         return 0;
2760 }
2761
2762 /*
2763  * clean up the setting for analog out
2764  */
2765 int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
2766                                      struct hda_multi_out *mout)
2767 {
2768         hda_nid_t *nids = mout->dac_nids;
2769         int i;
2770
2771         for (i = 0; i < mout->num_dacs; i++)
2772                 snd_hda_codec_cleanup_stream(codec, nids[i]);
2773         if (mout->hp_nid)
2774                 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
2775         for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
2776                 if (mout->extra_out_nid[i])
2777                         snd_hda_codec_cleanup_stream(codec,
2778                                                      mout->extra_out_nid[i]);
2779         mutex_lock(&codec->spdif_mutex);
2780         if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
2781                 cleanup_dig_out_stream(codec, mout->dig_out_nid);
2782                 mout->dig_out_used = 0;
2783         }
2784         mutex_unlock(&codec->spdif_mutex);
2785         return 0;
2786 }
2787
2788 /*
2789  * Helper for automatic ping configuration
2790  */
2791
2792 static int is_in_nid_list(hda_nid_t nid, hda_nid_t *list)
2793 {
2794         for (; *list; list++)
2795                 if (*list == nid)
2796                         return 1;
2797         return 0;
2798 }
2799
2800
2801 /*
2802  * Sort an associated group of pins according to their sequence numbers.
2803  */
2804 static void sort_pins_by_sequence(hda_nid_t * pins, short * sequences,
2805                                   int num_pins)
2806 {
2807         int i, j;
2808         short seq;
2809         hda_nid_t nid;
2810         
2811         for (i = 0; i < num_pins; i++) {
2812                 for (j = i + 1; j < num_pins; j++) {
2813                         if (sequences[i] > sequences[j]) {
2814                                 seq = sequences[i];
2815                                 sequences[i] = sequences[j];
2816                                 sequences[j] = seq;
2817                                 nid = pins[i];
2818                                 pins[i] = pins[j];
2819                                 pins[j] = nid;
2820                         }
2821                 }
2822         }
2823 }
2824
2825
2826 /*
2827  * Parse all pin widgets and store the useful pin nids to cfg
2828  *
2829  * The number of line-outs or any primary output is stored in line_outs,
2830  * and the corresponding output pins are assigned to line_out_pins[],
2831  * in the order of front, rear, CLFE, side, ...
2832  *
2833  * If more extra outputs (speaker and headphone) are found, the pins are
2834  * assisnged to hp_pins[] and speaker_pins[], respectively.  If no line-out jack
2835  * is detected, one of speaker of HP pins is assigned as the primary
2836  * output, i.e. to line_out_pins[0].  So, line_outs is always positive
2837  * if any analog output exists.
2838  * 
2839  * The analog input pins are assigned to input_pins array.
2840  * The digital input/output pins are assigned to dig_in_pin and dig_out_pin,
2841  * respectively.
2842  */
2843 int snd_hda_parse_pin_def_config(struct hda_codec *codec,
2844                                  struct auto_pin_cfg *cfg,
2845                                  hda_nid_t *ignore_nids)
2846 {
2847         hda_nid_t nid, end_nid;
2848         short seq, assoc_line_out, assoc_speaker;
2849         short sequences_line_out[ARRAY_SIZE(cfg->line_out_pins)];
2850         short sequences_speaker[ARRAY_SIZE(cfg->speaker_pins)];
2851         short sequences_hp[ARRAY_SIZE(cfg->hp_pins)];
2852
2853         memset(cfg, 0, sizeof(*cfg));
2854
2855         memset(sequences_line_out, 0, sizeof(sequences_line_out));
2856         memset(sequences_speaker, 0, sizeof(sequences_speaker));
2857         memset(sequences_hp, 0, sizeof(sequences_hp));
2858         assoc_line_out = assoc_speaker = 0;
2859
2860         end_nid = codec->start_nid + codec->num_nodes;
2861         for (nid = codec->start_nid; nid < end_nid; nid++) {
2862                 unsigned int wid_caps = get_wcaps(codec, nid);
2863                 unsigned int wid_type =
2864                         (wid_caps & AC_WCAP_TYPE) >> AC_WCAP_TYPE_SHIFT;
2865                 unsigned int def_conf;
2866                 short assoc, loc;
2867
2868                 /* read all default configuration for pin complex */
2869                 if (wid_type != AC_WID_PIN)
2870                         continue;
2871                 /* ignore the given nids (e.g. pc-beep returns error) */
2872                 if (ignore_nids && is_in_nid_list(nid, ignore_nids))
2873                         continue;
2874
2875                 def_conf = snd_hda_codec_read(codec, nid, 0,
2876                                               AC_VERB_GET_CONFIG_DEFAULT, 0);
2877                 if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE)
2878                         continue;
2879                 loc = get_defcfg_location(def_conf);
2880                 switch (get_defcfg_device(def_conf)) {
2881                 case AC_JACK_LINE_OUT:
2882                         seq = get_defcfg_sequence(def_conf);
2883                         assoc = get_defcfg_association(def_conf);
2884
2885                         if (!(wid_caps & AC_WCAP_STEREO))
2886                                 if (!cfg->mono_out_pin)
2887                                         cfg->mono_out_pin = nid;
2888                         if (!assoc)
2889                                 continue;
2890                         if (!assoc_line_out)
2891                                 assoc_line_out = assoc;
2892                         else if (assoc_line_out != assoc)
2893                                 continue;
2894                         if (cfg->line_outs >= ARRAY_SIZE(cfg->line_out_pins))
2895                                 continue;
2896                         cfg->line_out_pins[cfg->line_outs] = nid;
2897                         sequences_line_out[cfg->line_outs] = seq;
2898                         cfg->line_outs++;
2899                         break;
2900                 case AC_JACK_SPEAKER:
2901                         seq = get_defcfg_sequence(def_conf);
2902                         assoc = get_defcfg_association(def_conf);
2903                         if (! assoc)
2904                                 continue;
2905                         if (! assoc_speaker)
2906                                 assoc_speaker = assoc;
2907                         else if (assoc_speaker != assoc)
2908                                 continue;
2909                         if (cfg->speaker_outs >= ARRAY_SIZE(cfg->speaker_pins))
2910                                 continue;
2911                         cfg->speaker_pins[cfg->speaker_outs] = nid;
2912                         sequences_speaker[cfg->speaker_outs] = seq;
2913                         cfg->speaker_outs++;
2914                         break;
2915                 case AC_JACK_HP_OUT:
2916                         seq = get_defcfg_sequence(def_conf);
2917                         assoc = get_defcfg_association(def_conf);
2918                         if (cfg->hp_outs >= ARRAY_SIZE(cfg->hp_pins))
2919                                 continue;
2920                         cfg->hp_pins[cfg->hp_outs] = nid;
2921                         sequences_hp[cfg->hp_outs] = (assoc << 4) | seq;
2922                         cfg->hp_outs++;
2923                         break;
2924                 case AC_JACK_MIC_IN: {
2925                         int preferred, alt;
2926                         if (loc == AC_JACK_LOC_FRONT) {
2927                                 preferred = AUTO_PIN_FRONT_MIC;
2928                                 alt = AUTO_PIN_MIC;
2929                         } else {
2930                                 preferred = AUTO_PIN_MIC;
2931                                 alt = AUTO_PIN_FRONT_MIC;
2932                         }
2933                         if (!cfg->input_pins[preferred])
2934                                 cfg->input_pins[preferred] = nid;
2935                         else if (!cfg->input_pins[alt])
2936                                 cfg->input_pins[alt] = nid;
2937                         break;
2938                 }
2939                 case AC_JACK_LINE_IN:
2940                         if (loc == AC_JACK_LOC_FRONT)
2941                                 cfg->input_pins[AUTO_PIN_FRONT_LINE] = nid;
2942                         else
2943                                 cfg->input_pins[AUTO_PIN_LINE] = nid;
2944                         break;
2945                 case AC_JACK_CD:
2946                         cfg->input_pins[AUTO_PIN_CD] = nid;
2947                         break;
2948                 case AC_JACK_AUX:
2949                         cfg->input_pins[AUTO_PIN_AUX] = nid;
2950                         break;
2951                 case AC_JACK_SPDIF_OUT:
2952                         cfg->dig_out_pin = nid;
2953                         break;
2954                 case AC_JACK_SPDIF_IN:
2955                         cfg->dig_in_pin = nid;
2956                         break;
2957                 }
2958         }
2959
2960         /* FIX-UP:
2961          * If no line-out is defined but multiple HPs are found,
2962          * some of them might be the real line-outs.
2963          */
2964         if (!cfg->line_outs && cfg->hp_outs > 1) {
2965                 int i = 0;
2966                 while (i < cfg->hp_outs) {
2967                         /* The real HPs should have the sequence 0x0f */
2968                         if ((sequences_hp[i] & 0x0f) == 0x0f) {
2969                                 i++;
2970                                 continue;
2971                         }
2972                         /* Move it to the line-out table */
2973                         cfg->line_out_pins[cfg->line_outs] = cfg->hp_pins[i];
2974                         sequences_line_out[cfg->line_outs] = sequences_hp[i];
2975                         cfg->line_outs++;
2976                         cfg->hp_outs--;
2977                         memmove(cfg->hp_pins + i, cfg->hp_pins + i + 1,
2978                                 sizeof(cfg->hp_pins[0]) * (cfg->hp_outs - i));
2979                         memmove(sequences_hp + i - 1, sequences_hp + i,
2980                                 sizeof(sequences_hp[0]) * (cfg->hp_outs - i));
2981                 }
2982         }
2983
2984         /* sort by sequence */
2985         sort_pins_by_sequence(cfg->line_out_pins, sequences_line_out,
2986                               cfg->line_outs);
2987         sort_pins_by_sequence(cfg->speaker_pins, sequences_speaker,
2988                               cfg->speaker_outs);
2989         sort_pins_by_sequence(cfg->hp_pins, sequences_hp,
2990                               cfg->hp_outs);
2991         
2992         /* if we have only one mic, make it AUTO_PIN_MIC */
2993         if (!cfg->input_pins[AUTO_PIN_MIC] &&
2994             cfg->input_pins[AUTO_PIN_FRONT_MIC]) {
2995                 cfg->input_pins[AUTO_PIN_MIC] =
2996                         cfg->input_pins[AUTO_PIN_FRONT_MIC];
2997                 cfg->input_pins[AUTO_PIN_FRONT_MIC] = 0;
2998         }
2999         /* ditto for line-in */
3000         if (!cfg->input_pins[AUTO_PIN_LINE] &&
3001             cfg->input_pins[AUTO_PIN_FRONT_LINE]) {
3002                 cfg->input_pins[AUTO_PIN_LINE] =
3003                         cfg->input_pins[AUTO_PIN_FRONT_LINE];
3004                 cfg->input_pins[AUTO_PIN_FRONT_LINE] = 0;
3005         }
3006
3007         /*
3008          * FIX-UP: if no line-outs are detected, try to use speaker or HP pin
3009          * as a primary output
3010          */
3011         if (!cfg->line_outs) {
3012                 if (cfg->speaker_outs) {
3013                         cfg->line_outs = cfg->speaker_outs;
3014                         memcpy(cfg->line_out_pins, cfg->speaker_pins,
3015                                sizeof(cfg->speaker_pins));
3016                         cfg->speaker_outs = 0;
3017                         memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
3018                         cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
3019                 } else if (cfg->hp_outs) {
3020                         cfg->line_outs = cfg->hp_outs;
3021                         memcpy(cfg->line_out_pins, cfg->hp_pins,
3022                                sizeof(cfg->hp_pins));
3023                         cfg->hp_outs = 0;
3024                         memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
3025                         cfg->line_out_type = AUTO_PIN_HP_OUT;
3026                 }
3027         }
3028
3029         /* Reorder the surround channels
3030          * ALSA sequence is front/surr/clfe/side
3031          * HDA sequence is:
3032          *    4-ch: front/surr  =>  OK as it is
3033          *    6-ch: front/clfe/surr
3034          *    8-ch: front/clfe/rear/side|fc
3035          */
3036         switch (cfg->line_outs) {
3037         case 3:
3038         case 4:
3039                 nid = cfg->line_out_pins[1];
3040                 cfg->line_out_pins[1] = cfg->line_out_pins[2];
3041                 cfg->line_out_pins[2] = nid;
3042                 break;
3043         }
3044
3045         /*
3046          * debug prints of the parsed results
3047          */
3048         snd_printd("autoconfig: line_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
3049                    cfg->line_outs, cfg->line_out_pins[0], cfg->line_out_pins[1],
3050                    cfg->line_out_pins[2], cfg->line_out_pins[3],
3051                    cfg->line_out_pins[4]);
3052         snd_printd("   speaker_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
3053                    cfg->speaker_outs, cfg->speaker_pins[0],
3054                    cfg->speaker_pins[1], cfg->speaker_pins[2],
3055                    cfg->speaker_pins[3], cfg->speaker_pins[4]);
3056         snd_printd("   hp_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
3057                    cfg->hp_outs, cfg->hp_pins[0],
3058                    cfg->hp_pins[1], cfg->hp_pins[2],
3059                    cfg->hp_pins[3], cfg->hp_pins[4]);
3060         snd_printd("   mono: mono_out=0x%x\n", cfg->mono_out_pin);
3061         snd_printd("   inputs: mic=0x%x, fmic=0x%x, line=0x%x, fline=0x%x,"
3062                    " cd=0x%x, aux=0x%x\n",
3063                    cfg->input_pins[AUTO_PIN_MIC],
3064                    cfg->input_pins[AUTO_PIN_FRONT_MIC],
3065                    cfg->input_pins[AUTO_PIN_LINE],
3066                    cfg->input_pins[AUTO_PIN_FRONT_LINE],
3067                    cfg->input_pins[AUTO_PIN_CD],
3068                    cfg->input_pins[AUTO_PIN_AUX]);
3069
3070         return 0;
3071 }
3072
3073 /* labels for input pins */
3074 const char *auto_pin_cfg_labels[AUTO_PIN_LAST] = {
3075         "Mic", "Front Mic", "Line", "Front Line", "CD", "Aux"
3076 };
3077
3078
3079 #ifdef CONFIG_PM
3080 /*
3081  * power management
3082  */
3083
3084 /**
3085  * snd_hda_suspend - suspend the codecs
3086  * @bus: the HDA bus
3087  * @state: suspsend state
3088  *
3089  * Returns 0 if successful.
3090  */
3091 int snd_hda_suspend(struct hda_bus *bus, pm_message_t state)
3092 {
3093         struct hda_codec *codec;
3094
3095         list_for_each_entry(codec, &bus->codec_list, list) {
3096 #ifdef CONFIG_SND_HDA_POWER_SAVE
3097                 if (!codec->power_on)
3098                         continue;
3099 #endif
3100                 hda_call_codec_suspend(codec);
3101         }
3102         return 0;
3103 }
3104
3105 /**
3106  * snd_hda_resume - resume the codecs
3107  * @bus: the HDA bus
3108  * @state: resume state
3109  *
3110  * Returns 0 if successful.
3111  *
3112  * This fucntion is defined only when POWER_SAVE isn't set.
3113  * In the power-save mode, the codec is resumed dynamically.
3114  */
3115 int snd_hda_resume(struct hda_bus *bus)
3116 {
3117         struct hda_codec *codec;
3118
3119         list_for_each_entry(codec, &bus->codec_list, list) {
3120                 if (snd_hda_codec_needs_resume(codec))
3121                         hda_call_codec_resume(codec);
3122         }
3123         return 0;
3124 }
3125 #ifdef CONFIG_SND_HDA_POWER_SAVE
3126 int snd_hda_codecs_inuse(struct hda_bus *bus)
3127 {
3128         struct hda_codec *codec;
3129
3130         list_for_each_entry(codec, &bus->codec_list, list) {
3131                 if (snd_hda_codec_needs_resume(codec))
3132                         return 1;
3133         }
3134         return 0;
3135 }
3136 #endif
3137 #endif