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