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