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