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