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