[ALSA] hda-codec - Get subsystem ID from AFG/MFG
[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 <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 = kzalloc(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 = kzalloc(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 and MFG nodes
436  */
437 static void setup_fg_nodes(struct hda_codec *codec)
438 {
439         int i, total_nodes;
440         hda_nid_t nid;
441
442         total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid);
443         for (i = 0; i < total_nodes; i++, nid++) {
444                 switch((snd_hda_param_read(codec, nid, AC_PAR_FUNCTION_TYPE) & 0xff)) {
445                 case AC_GRP_AUDIO_FUNCTION:
446                         codec->afg = nid;
447                         break;
448                 case AC_GRP_MODEM_FUNCTION:
449                         codec->mfg = nid;
450                         break;
451                 default:
452                         break;
453                 }
454         }
455 }
456
457 /*
458  * codec destructor
459  */
460 static void snd_hda_codec_free(struct hda_codec *codec)
461 {
462         if (! codec)
463                 return;
464         list_del(&codec->list);
465         codec->bus->caddr_tbl[codec->addr] = NULL;
466         if (codec->patch_ops.free)
467                 codec->patch_ops.free(codec);
468         kfree(codec);
469 }
470
471 static void init_amp_hash(struct hda_codec *codec);
472
473 /**
474  * snd_hda_codec_new - create a HDA codec
475  * @bus: the bus to assign
476  * @codec_addr: the codec address
477  * @codecp: the pointer to store the generated codec
478  *
479  * Returns 0 if successful, or a negative error code.
480  */
481 int snd_hda_codec_new(struct hda_bus *bus, unsigned int codec_addr,
482                       struct hda_codec **codecp)
483 {
484         struct hda_codec *codec;
485         char component[13];
486         int err;
487
488         snd_assert(bus, return -EINVAL);
489         snd_assert(codec_addr <= HDA_MAX_CODEC_ADDRESS, return -EINVAL);
490
491         if (bus->caddr_tbl[codec_addr]) {
492                 snd_printk(KERN_ERR "hda_codec: address 0x%x is already occupied\n", codec_addr);
493                 return -EBUSY;
494         }
495
496         codec = kzalloc(sizeof(*codec), GFP_KERNEL);
497         if (codec == NULL) {
498                 snd_printk(KERN_ERR "can't allocate struct hda_codec\n");
499                 return -ENOMEM;
500         }
501
502         codec->bus = bus;
503         codec->addr = codec_addr;
504         init_MUTEX(&codec->spdif_mutex);
505         init_amp_hash(codec);
506
507         list_add_tail(&codec->list, &bus->codec_list);
508         bus->caddr_tbl[codec_addr] = codec;
509
510         codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT, AC_PAR_VENDOR_ID);
511         codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT, AC_PAR_SUBSYSTEM_ID);
512         codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT, AC_PAR_REV_ID);
513
514         setup_fg_nodes(codec);
515         if (! codec->afg && ! codec->mfg) {
516                 snd_printdd("hda_codec: no AFG or MFG node found\n");
517                 snd_hda_codec_free(codec);
518                 return -ENODEV;
519         }
520
521         if (! codec->subsystem_id) {
522                 hda_nid_t nid = codec->afg ? codec->afg : codec->mfg;
523                 codec->subsystem_id = snd_hda_codec_read(codec, nid, 0,
524                                                          AC_VERB_GET_SUBSYSTEM_ID,
525                                                          0);
526         }
527
528         codec->preset = find_codec_preset(codec);
529         if (! *bus->card->mixername)
530                 snd_hda_get_codec_name(codec, bus->card->mixername,
531                                        sizeof(bus->card->mixername));
532
533         if (codec->preset && codec->preset->patch)
534                 err = codec->preset->patch(codec);
535         else
536                 err = snd_hda_parse_generic_codec(codec);
537         if (err < 0) {
538                 snd_hda_codec_free(codec);
539                 return err;
540         }
541
542         snd_hda_codec_proc_new(codec);
543
544         sprintf(component, "HDA:%08x", codec->vendor_id);
545         snd_component_add(codec->bus->card, component);
546
547         if (codecp)
548                 *codecp = codec;
549         return 0;
550 }
551
552 /**
553  * snd_hda_codec_setup_stream - set up the codec for streaming
554  * @codec: the CODEC to set up
555  * @nid: the NID to set up
556  * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
557  * @channel_id: channel id to pass, zero based.
558  * @format: stream format.
559  */
560 void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid, u32 stream_tag,
561                                 int channel_id, int format)
562 {
563         if (! nid)
564                 return;
565
566         snd_printdd("hda_codec_setup_stream: NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
567                     nid, stream_tag, channel_id, format);
568         snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID,
569                             (stream_tag << 4) | channel_id);
570         msleep(1);
571         snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, format);
572 }
573
574
575 /*
576  * amp access functions
577  */
578
579 /* FIXME: more better hash key? */
580 #define HDA_HASH_KEY(nid,dir,idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
581 #define INFO_AMP_CAPS   (1<<0)
582 #define INFO_AMP_VOL(ch)        (1 << (1 + (ch)))
583
584 /* initialize the hash table */
585 static void init_amp_hash(struct hda_codec *codec)
586 {
587         memset(codec->amp_hash, 0xff, sizeof(codec->amp_hash));
588         codec->num_amp_entries = 0;
589 }
590
591 /* query the hash.  allocate an entry if not found. */
592 static struct hda_amp_info *get_alloc_amp_hash(struct hda_codec *codec, u32 key)
593 {
594         u16 idx = key % (u16)ARRAY_SIZE(codec->amp_hash);
595         u16 cur = codec->amp_hash[idx];
596         struct hda_amp_info *info;
597
598         while (cur != 0xffff) {
599                 info = &codec->amp_info[cur];
600                 if (info->key == key)
601                         return info;
602                 cur = info->next;
603         }
604
605         /* add a new hash entry */
606         if (codec->num_amp_entries >= ARRAY_SIZE(codec->amp_info)) {
607                 snd_printk(KERN_ERR "hda_codec: Tooooo many amps!\n");
608                 return NULL;
609         }
610         cur = codec->num_amp_entries++;
611         info = &codec->amp_info[cur];
612         info->key = key;
613         info->status = 0; /* not initialized yet */
614         info->next = codec->amp_hash[idx];
615         codec->amp_hash[idx] = cur;
616
617         return info;
618 }
619
620 /*
621  * query AMP capabilities for the given widget and direction
622  */
623 static u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
624 {
625         struct hda_amp_info *info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, 0));
626
627         if (! info)
628                 return 0;
629         if (! (info->status & INFO_AMP_CAPS)) {
630                 if (!(snd_hda_param_read(codec, nid, AC_PAR_AUDIO_WIDGET_CAP) & AC_WCAP_AMP_OVRD))
631                         nid = codec->afg;
632                 info->amp_caps = snd_hda_param_read(codec, nid, direction == HDA_OUTPUT ?
633                                                     AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP);
634                 info->status |= INFO_AMP_CAPS;
635         }
636         return info->amp_caps;
637 }
638
639 /*
640  * read the current volume to info
641  * if the cache exists, read the cache value.
642  */
643 static unsigned int get_vol_mute(struct hda_codec *codec, struct hda_amp_info *info,
644                          hda_nid_t nid, int ch, int direction, int index)
645 {
646         u32 val, parm;
647
648         if (info->status & INFO_AMP_VOL(ch))
649                 return info->vol[ch];
650
651         parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
652         parm |= direction == HDA_OUTPUT ? AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
653         parm |= index;
654         val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_AMP_GAIN_MUTE, parm);
655         info->vol[ch] = val & 0xff;
656         info->status |= INFO_AMP_VOL(ch);
657         return info->vol[ch];
658 }
659
660 /*
661  * write the current volume in info to the h/w and update the cache
662  */
663 static void put_vol_mute(struct hda_codec *codec, struct hda_amp_info *info,
664                          hda_nid_t nid, int ch, int direction, int index, int val)
665 {
666         u32 parm;
667
668         parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
669         parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
670         parm |= index << AC_AMP_SET_INDEX_SHIFT;
671         parm |= val;
672         snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
673         info->vol[ch] = val;
674 }
675
676 /*
677  * read AMP value.  The volume is between 0 to 0x7f, 0x80 = mute bit.
678  */
679 static int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch, int direction, int index)
680 {
681         struct hda_amp_info *info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
682         if (! info)
683                 return 0;
684         return get_vol_mute(codec, info, nid, ch, direction, index);
685 }
686
687 /*
688  * update the AMP value, mask = bit mask to set, val = the value
689  */
690 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)
691 {
692         struct hda_amp_info *info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, idx));
693
694         if (! info)
695                 return 0;
696         val &= mask;
697         val |= get_vol_mute(codec, info, nid, ch, direction, idx) & ~mask;
698         if (info->vol[ch] == val && ! codec->in_resume)
699                 return 0;
700         put_vol_mute(codec, info, nid, ch, direction, idx, val);
701         return 1;
702 }
703
704
705 /*
706  * AMP control callbacks
707  */
708 /* retrieve parameters from private_value */
709 #define get_amp_nid(kc)         ((kc)->private_value & 0xffff)
710 #define get_amp_channels(kc)    (((kc)->private_value >> 16) & 0x3)
711 #define get_amp_direction(kc)   (((kc)->private_value >> 18) & 0x1)
712 #define get_amp_index(kc)       (((kc)->private_value >> 19) & 0xf)
713
714 /* volume */
715 int snd_hda_mixer_amp_volume_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
716 {
717         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
718         u16 nid = get_amp_nid(kcontrol);
719         u8 chs = get_amp_channels(kcontrol);
720         int dir = get_amp_direction(kcontrol);
721         u32 caps;
722
723         caps = query_amp_caps(codec, nid, dir);
724         caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT; /* num steps */
725         if (! caps) {
726                 printk(KERN_WARNING "hda_codec: num_steps = 0 for NID=0x%x\n", nid);
727                 return -EINVAL;
728         }
729         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
730         uinfo->count = chs == 3 ? 2 : 1;
731         uinfo->value.integer.min = 0;
732         uinfo->value.integer.max = caps;
733         return 0;
734 }
735
736 int snd_hda_mixer_amp_volume_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
737 {
738         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
739         hda_nid_t nid = get_amp_nid(kcontrol);
740         int chs = get_amp_channels(kcontrol);
741         int dir = get_amp_direction(kcontrol);
742         int idx = get_amp_index(kcontrol);
743         long *valp = ucontrol->value.integer.value;
744
745         if (chs & 1)
746                 *valp++ = snd_hda_codec_amp_read(codec, nid, 0, dir, idx) & 0x7f;
747         if (chs & 2)
748                 *valp = snd_hda_codec_amp_read(codec, nid, 1, dir, idx) & 0x7f;
749         return 0;
750 }
751
752 int snd_hda_mixer_amp_volume_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
753 {
754         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
755         hda_nid_t nid = get_amp_nid(kcontrol);
756         int chs = get_amp_channels(kcontrol);
757         int dir = get_amp_direction(kcontrol);
758         int idx = get_amp_index(kcontrol);
759         long *valp = ucontrol->value.integer.value;
760         int change = 0;
761
762         if (chs & 1) {
763                 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
764                                                   0x7f, *valp);
765                 valp++;
766         }
767         if (chs & 2)
768                 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
769                                                    0x7f, *valp);
770         return change;
771 }
772
773 /* switch */
774 int snd_hda_mixer_amp_switch_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
775 {
776         int chs = get_amp_channels(kcontrol);
777
778         uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
779         uinfo->count = chs == 3 ? 2 : 1;
780         uinfo->value.integer.min = 0;
781         uinfo->value.integer.max = 1;
782         return 0;
783 }
784
785 int snd_hda_mixer_amp_switch_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
786 {
787         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
788         hda_nid_t nid = get_amp_nid(kcontrol);
789         int chs = get_amp_channels(kcontrol);
790         int dir = get_amp_direction(kcontrol);
791         int idx = get_amp_index(kcontrol);
792         long *valp = ucontrol->value.integer.value;
793
794         if (chs & 1)
795                 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) & 0x80) ? 0 : 1;
796         if (chs & 2)
797                 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) & 0x80) ? 0 : 1;
798         return 0;
799 }
800
801 int snd_hda_mixer_amp_switch_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
802 {
803         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
804         hda_nid_t nid = get_amp_nid(kcontrol);
805         int chs = get_amp_channels(kcontrol);
806         int dir = get_amp_direction(kcontrol);
807         int idx = get_amp_index(kcontrol);
808         long *valp = ucontrol->value.integer.value;
809         int change = 0;
810
811         if (chs & 1) {
812                 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
813                                                   0x80, *valp ? 0 : 0x80);
814                 valp++;
815         }
816         if (chs & 2)
817                 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
818                                                    0x80, *valp ? 0 : 0x80);
819         
820         return change;
821 }
822
823 /*
824  * SPDIF out controls
825  */
826
827 static int snd_hda_spdif_mask_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
828 {
829         uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
830         uinfo->count = 1;
831         return 0;
832 }
833
834 static int snd_hda_spdif_cmask_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
835 {
836         ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
837                                            IEC958_AES0_NONAUDIO |
838                                            IEC958_AES0_CON_EMPHASIS_5015 |
839                                            IEC958_AES0_CON_NOT_COPYRIGHT;
840         ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
841                                            IEC958_AES1_CON_ORIGINAL;
842         return 0;
843 }
844
845 static int snd_hda_spdif_pmask_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
846 {
847         ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
848                                            IEC958_AES0_NONAUDIO |
849                                            IEC958_AES0_PRO_EMPHASIS_5015;
850         return 0;
851 }
852
853 static int snd_hda_spdif_default_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
854 {
855         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
856
857         ucontrol->value.iec958.status[0] = codec->spdif_status & 0xff;
858         ucontrol->value.iec958.status[1] = (codec->spdif_status >> 8) & 0xff;
859         ucontrol->value.iec958.status[2] = (codec->spdif_status >> 16) & 0xff;
860         ucontrol->value.iec958.status[3] = (codec->spdif_status >> 24) & 0xff;
861
862         return 0;
863 }
864
865 /* convert from SPDIF status bits to HDA SPDIF bits
866  * bit 0 (DigEn) is always set zero (to be filled later)
867  */
868 static unsigned short convert_from_spdif_status(unsigned int sbits)
869 {
870         unsigned short val = 0;
871
872         if (sbits & IEC958_AES0_PROFESSIONAL)
873                 val |= 1 << 6;
874         if (sbits & IEC958_AES0_NONAUDIO)
875                 val |= 1 << 5;
876         if (sbits & IEC958_AES0_PROFESSIONAL) {
877                 if ((sbits & IEC958_AES0_PRO_EMPHASIS) == IEC958_AES0_PRO_EMPHASIS_5015)
878                         val |= 1 << 3;
879         } else {
880                 if ((sbits & IEC958_AES0_CON_EMPHASIS) == IEC958_AES0_CON_EMPHASIS_5015)
881                         val |= 1 << 3;
882                 if (! (sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
883                         val |= 1 << 4;
884                 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
885                         val |= 1 << 7;
886                 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
887         }
888         return val;
889 }
890
891 /* convert to SPDIF status bits from HDA SPDIF bits
892  */
893 static unsigned int convert_to_spdif_status(unsigned short val)
894 {
895         unsigned int sbits = 0;
896
897         if (val & (1 << 5))
898                 sbits |= IEC958_AES0_NONAUDIO;
899         if (val & (1 << 6))
900                 sbits |= IEC958_AES0_PROFESSIONAL;
901         if (sbits & IEC958_AES0_PROFESSIONAL) {
902                 if (sbits & (1 << 3))
903                         sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
904         } else {
905                 if (val & (1 << 3))
906                         sbits |= IEC958_AES0_CON_EMPHASIS_5015;
907                 if (! (val & (1 << 4)))
908                         sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
909                 if (val & (1 << 7))
910                         sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
911                 sbits |= val & (0x7f << 8);
912         }
913         return sbits;
914 }
915
916 static int snd_hda_spdif_default_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
917 {
918         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
919         hda_nid_t nid = kcontrol->private_value;
920         unsigned short val;
921         int change;
922
923         down(&codec->spdif_mutex);
924         codec->spdif_status = ucontrol->value.iec958.status[0] |
925                 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
926                 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
927                 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
928         val = convert_from_spdif_status(codec->spdif_status);
929         val |= codec->spdif_ctls & 1;
930         change = codec->spdif_ctls != val;
931         codec->spdif_ctls = val;
932
933         if (change || codec->in_resume) {
934                 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_DIGI_CONVERT_1, val & 0xff);
935                 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_DIGI_CONVERT_2, val >> 8);
936         }
937
938         up(&codec->spdif_mutex);
939         return change;
940 }
941
942 static int snd_hda_spdif_out_switch_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
943 {
944         uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
945         uinfo->count = 1;
946         uinfo->value.integer.min = 0;
947         uinfo->value.integer.max = 1;
948         return 0;
949 }
950
951 static int snd_hda_spdif_out_switch_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
952 {
953         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
954
955         ucontrol->value.integer.value[0] = codec->spdif_ctls & 1;
956         return 0;
957 }
958
959 static int snd_hda_spdif_out_switch_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
960 {
961         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
962         hda_nid_t nid = kcontrol->private_value;
963         unsigned short val;
964         int change;
965
966         down(&codec->spdif_mutex);
967         val = codec->spdif_ctls & ~1;
968         if (ucontrol->value.integer.value[0])
969                 val |= 1;
970         change = codec->spdif_ctls != val;
971         if (change || codec->in_resume) {
972                 codec->spdif_ctls = val;
973                 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_DIGI_CONVERT_1, val & 0xff);
974                 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE,
975                                     AC_AMP_SET_RIGHT | AC_AMP_SET_LEFT |
976                                     AC_AMP_SET_OUTPUT | ((val & 1) ? 0 : 0x80));
977         }
978         up(&codec->spdif_mutex);
979         return change;
980 }
981
982 static snd_kcontrol_new_t dig_mixes[] = {
983         {
984                 .access = SNDRV_CTL_ELEM_ACCESS_READ,
985                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
986                 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,CON_MASK),
987                 .info = snd_hda_spdif_mask_info,
988                 .get = snd_hda_spdif_cmask_get,
989         },
990         {
991                 .access = SNDRV_CTL_ELEM_ACCESS_READ,
992                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
993                 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,PRO_MASK),
994                 .info = snd_hda_spdif_mask_info,
995                 .get = snd_hda_spdif_pmask_get,
996         },
997         {
998                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
999                 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
1000                 .info = snd_hda_spdif_mask_info,
1001                 .get = snd_hda_spdif_default_get,
1002                 .put = snd_hda_spdif_default_put,
1003         },
1004         {
1005                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1006                 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,SWITCH),
1007                 .info = snd_hda_spdif_out_switch_info,
1008                 .get = snd_hda_spdif_out_switch_get,
1009                 .put = snd_hda_spdif_out_switch_put,
1010         },
1011         { } /* end */
1012 };
1013
1014 /**
1015  * snd_hda_create_spdif_out_ctls - create Output SPDIF-related controls
1016  * @codec: the HDA codec
1017  * @nid: audio out widget NID
1018  *
1019  * Creates controls related with the SPDIF output.
1020  * Called from each patch supporting the SPDIF out.
1021  *
1022  * Returns 0 if successful, or a negative error code.
1023  */
1024 int snd_hda_create_spdif_out_ctls(struct hda_codec *codec, hda_nid_t nid)
1025 {
1026         int err;
1027         snd_kcontrol_t *kctl;
1028         snd_kcontrol_new_t *dig_mix;
1029
1030         for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
1031                 kctl = snd_ctl_new1(dig_mix, codec);
1032                 kctl->private_value = nid;
1033                 if ((err = snd_ctl_add(codec->bus->card, kctl)) < 0)
1034                         return err;
1035         }
1036         codec->spdif_ctls = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT, 0);
1037         codec->spdif_status = convert_to_spdif_status(codec->spdif_ctls);
1038         return 0;
1039 }
1040
1041 /*
1042  * SPDIF input
1043  */
1044
1045 #define snd_hda_spdif_in_switch_info    snd_hda_spdif_out_switch_info
1046
1047 static int snd_hda_spdif_in_switch_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
1048 {
1049         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1050
1051         ucontrol->value.integer.value[0] = codec->spdif_in_enable;
1052         return 0;
1053 }
1054
1055 static int snd_hda_spdif_in_switch_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
1056 {
1057         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1058         hda_nid_t nid = kcontrol->private_value;
1059         unsigned int val = !!ucontrol->value.integer.value[0];
1060         int change;
1061
1062         down(&codec->spdif_mutex);
1063         change = codec->spdif_in_enable != val;
1064         if (change || codec->in_resume) {
1065                 codec->spdif_in_enable = val;
1066                 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_DIGI_CONVERT_1, val);
1067         }
1068         up(&codec->spdif_mutex);
1069         return change;
1070 }
1071
1072 static int snd_hda_spdif_in_status_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
1073 {
1074         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1075         hda_nid_t nid = kcontrol->private_value;
1076         unsigned short val;
1077         unsigned int sbits;
1078
1079         val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT, 0);
1080         sbits = convert_to_spdif_status(val);
1081         ucontrol->value.iec958.status[0] = sbits;
1082         ucontrol->value.iec958.status[1] = sbits >> 8;
1083         ucontrol->value.iec958.status[2] = sbits >> 16;
1084         ucontrol->value.iec958.status[3] = sbits >> 24;
1085         return 0;
1086 }
1087
1088 static snd_kcontrol_new_t dig_in_ctls[] = {
1089         {
1090                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1091                 .name = SNDRV_CTL_NAME_IEC958("",CAPTURE,SWITCH),
1092                 .info = snd_hda_spdif_in_switch_info,
1093                 .get = snd_hda_spdif_in_switch_get,
1094                 .put = snd_hda_spdif_in_switch_put,
1095         },
1096         {
1097                 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1098                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1099                 .name = SNDRV_CTL_NAME_IEC958("",CAPTURE,DEFAULT),
1100                 .info = snd_hda_spdif_mask_info,
1101                 .get = snd_hda_spdif_in_status_get,
1102         },
1103         { } /* end */
1104 };
1105
1106 /**
1107  * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
1108  * @codec: the HDA codec
1109  * @nid: audio in widget NID
1110  *
1111  * Creates controls related with the SPDIF input.
1112  * Called from each patch supporting the SPDIF in.
1113  *
1114  * Returns 0 if successful, or a negative error code.
1115  */
1116 int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
1117 {
1118         int err;
1119         snd_kcontrol_t *kctl;
1120         snd_kcontrol_new_t *dig_mix;
1121
1122         for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
1123                 kctl = snd_ctl_new1(dig_mix, codec);
1124                 kctl->private_value = nid;
1125                 if ((err = snd_ctl_add(codec->bus->card, kctl)) < 0)
1126                         return err;
1127         }
1128         codec->spdif_in_enable = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT, 0) & 1;
1129         return 0;
1130 }
1131
1132
1133 /**
1134  * snd_hda_build_controls - build mixer controls
1135  * @bus: the BUS
1136  *
1137  * Creates mixer controls for each codec included in the bus.
1138  *
1139  * Returns 0 if successful, otherwise a negative error code.
1140  */
1141 int snd_hda_build_controls(struct hda_bus *bus)
1142 {
1143         struct list_head *p;
1144
1145         /* build controls */
1146         list_for_each(p, &bus->codec_list) {
1147                 struct hda_codec *codec = list_entry(p, struct hda_codec, list);
1148                 int err;
1149                 if (! codec->patch_ops.build_controls)
1150                         continue;
1151                 err = codec->patch_ops.build_controls(codec);
1152                 if (err < 0)
1153                         return err;
1154         }
1155
1156         /* initialize */
1157         list_for_each(p, &bus->codec_list) {
1158                 struct hda_codec *codec = list_entry(p, struct hda_codec, list);
1159                 int err;
1160                 if (! codec->patch_ops.init)
1161                         continue;
1162                 err = codec->patch_ops.init(codec);
1163                 if (err < 0)
1164                         return err;
1165         }
1166         return 0;
1167 }
1168
1169
1170 /*
1171  * stream formats
1172  */
1173 struct hda_rate_tbl {
1174         unsigned int hz;
1175         unsigned int alsa_bits;
1176         unsigned int hda_fmt;
1177 };
1178
1179 static struct hda_rate_tbl rate_bits[] = {
1180         /* rate in Hz, ALSA rate bitmask, HDA format value */
1181
1182         /* autodetected value used in snd_hda_query_supported_pcm */
1183         { 8000, SNDRV_PCM_RATE_8000, 0x0500 }, /* 1/6 x 48 */
1184         { 11025, SNDRV_PCM_RATE_11025, 0x4300 }, /* 1/4 x 44 */
1185         { 16000, SNDRV_PCM_RATE_16000, 0x0200 }, /* 1/3 x 48 */
1186         { 22050, SNDRV_PCM_RATE_22050, 0x4100 }, /* 1/2 x 44 */
1187         { 32000, SNDRV_PCM_RATE_32000, 0x0a00 }, /* 2/3 x 48 */
1188         { 44100, SNDRV_PCM_RATE_44100, 0x4000 }, /* 44 */
1189         { 48000, SNDRV_PCM_RATE_48000, 0x0000 }, /* 48 */
1190         { 88200, SNDRV_PCM_RATE_88200, 0x4800 }, /* 2 x 44 */
1191         { 96000, SNDRV_PCM_RATE_96000, 0x0800 }, /* 2 x 48 */
1192         { 176400, SNDRV_PCM_RATE_176400, 0x5800 },/* 4 x 44 */
1193         { 192000, SNDRV_PCM_RATE_192000, 0x1800 }, /* 4 x 48 */
1194
1195         /* not autodetected value */
1196         { 9600, SNDRV_PCM_RATE_KNOT, 0x0400 }, /* 1/5 x 48 */
1197
1198         { 0 } /* terminator */
1199 };
1200
1201 /**
1202  * snd_hda_calc_stream_format - calculate format bitset
1203  * @rate: the sample rate
1204  * @channels: the number of channels
1205  * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
1206  * @maxbps: the max. bps
1207  *
1208  * Calculate the format bitset from the given rate, channels and th PCM format.
1209  *
1210  * Return zero if invalid.
1211  */
1212 unsigned int snd_hda_calc_stream_format(unsigned int rate,
1213                                         unsigned int channels,
1214                                         unsigned int format,
1215                                         unsigned int maxbps)
1216 {
1217         int i;
1218         unsigned int val = 0;
1219
1220         for (i = 0; rate_bits[i].hz; i++)
1221                 if (rate_bits[i].hz == rate) {
1222                         val = rate_bits[i].hda_fmt;
1223                         break;
1224                 }
1225         if (! rate_bits[i].hz) {
1226                 snd_printdd("invalid rate %d\n", rate);
1227                 return 0;
1228         }
1229
1230         if (channels == 0 || channels > 8) {
1231                 snd_printdd("invalid channels %d\n", channels);
1232                 return 0;
1233         }
1234         val |= channels - 1;
1235
1236         switch (snd_pcm_format_width(format)) {
1237         case 8:  val |= 0x00; break;
1238         case 16: val |= 0x10; break;
1239         case 20:
1240         case 24:
1241         case 32:
1242                 if (maxbps >= 32)
1243                         val |= 0x40;
1244                 else if (maxbps >= 24)
1245                         val |= 0x30;
1246                 else
1247                         val |= 0x20;
1248                 break;
1249         default:
1250                 snd_printdd("invalid format width %d\n", snd_pcm_format_width(format));
1251                 return 0;
1252         }
1253
1254         return val;
1255 }
1256
1257 /**
1258  * snd_hda_query_supported_pcm - query the supported PCM rates and formats
1259  * @codec: the HDA codec
1260  * @nid: NID to query
1261  * @ratesp: the pointer to store the detected rate bitflags
1262  * @formatsp: the pointer to store the detected formats
1263  * @bpsp: the pointer to store the detected format widths
1264  *
1265  * Queries the supported PCM rates and formats.  The NULL @ratesp, @formatsp
1266  * or @bsps argument is ignored.
1267  *
1268  * Returns 0 if successful, otherwise a negative error code.
1269  */
1270 int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
1271                                 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
1272 {
1273         int i;
1274         unsigned int val, streams;
1275
1276         val = 0;
1277         if (nid != codec->afg &&
1278             snd_hda_param_read(codec, nid, AC_PAR_AUDIO_WIDGET_CAP) & AC_WCAP_FORMAT_OVRD) {
1279                 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
1280                 if (val == -1)
1281                         return -EIO;
1282         }
1283         if (! val)
1284                 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
1285
1286         if (ratesp) {
1287                 u32 rates = 0;
1288                 for (i = 0; rate_bits[i].hz; i++) {
1289                         if (val & (1 << i))
1290                                 rates |= rate_bits[i].alsa_bits;
1291                 }
1292                 *ratesp = rates;
1293         }
1294
1295         if (formatsp || bpsp) {
1296                 u64 formats = 0;
1297                 unsigned int bps;
1298                 unsigned int wcaps;
1299
1300                 wcaps = snd_hda_param_read(codec, nid, AC_PAR_AUDIO_WIDGET_CAP);
1301                 streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
1302                 if (streams == -1)
1303                         return -EIO;
1304                 if (! streams) {
1305                         streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
1306                         if (streams == -1)
1307                                 return -EIO;
1308                 }
1309
1310                 bps = 0;
1311                 if (streams & AC_SUPFMT_PCM) {
1312                         if (val & AC_SUPPCM_BITS_8) {
1313                                 formats |= SNDRV_PCM_FMTBIT_U8;
1314                                 bps = 8;
1315                         }
1316                         if (val & AC_SUPPCM_BITS_16) {
1317                                 formats |= SNDRV_PCM_FMTBIT_S16_LE;
1318                                 bps = 16;
1319                         }
1320                         if (wcaps & AC_WCAP_DIGITAL) {
1321                                 if (val & AC_SUPPCM_BITS_32)
1322                                         formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
1323                                 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
1324                                         formats |= SNDRV_PCM_FMTBIT_S32_LE;
1325                                 if (val & AC_SUPPCM_BITS_24)
1326                                         bps = 24;
1327                                 else if (val & AC_SUPPCM_BITS_20)
1328                                         bps = 20;
1329                         } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|AC_SUPPCM_BITS_32)) {
1330                                 formats |= SNDRV_PCM_FMTBIT_S32_LE;
1331                                 if (val & AC_SUPPCM_BITS_32)
1332                                         bps = 32;
1333                                 else if (val & AC_SUPPCM_BITS_20)
1334                                         bps = 20;
1335                                 else if (val & AC_SUPPCM_BITS_24)
1336                                         bps = 24;
1337                         }
1338                 }
1339                 else if (streams == AC_SUPFMT_FLOAT32) { /* should be exclusive */
1340                         formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
1341                         bps = 32;
1342                 } else if (streams == AC_SUPFMT_AC3) { /* should be exclusive */
1343                         /* temporary hack: we have still no proper support
1344                          * for the direct AC3 stream...
1345                          */
1346                         formats |= SNDRV_PCM_FMTBIT_U8;
1347                         bps = 8;
1348                 }
1349                 if (formatsp)
1350                         *formatsp = formats;
1351                 if (bpsp)
1352                         *bpsp = bps;
1353         }
1354
1355         return 0;
1356 }
1357
1358 /**
1359  * snd_hda_is_supported_format - check whether the given node supports the format val
1360  *
1361  * Returns 1 if supported, 0 if not.
1362  */
1363 int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
1364                                 unsigned int format)
1365 {
1366         int i;
1367         unsigned int val = 0, rate, stream;
1368
1369         if (nid != codec->afg &&
1370             snd_hda_param_read(codec, nid, AC_PAR_AUDIO_WIDGET_CAP) & AC_WCAP_FORMAT_OVRD) {
1371                 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
1372                 if (val == -1)
1373                         return 0;
1374         }
1375         if (! val) {
1376                 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
1377                 if (val == -1)
1378                         return 0;
1379         }
1380
1381         rate = format & 0xff00;
1382         for (i = 0; rate_bits[i].hz; i++)
1383                 if (rate_bits[i].hda_fmt == rate) {
1384                         if (val & (1 << i))
1385                                 break;
1386                         return 0;
1387                 }
1388         if (! rate_bits[i].hz)
1389                 return 0;
1390
1391         stream = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
1392         if (stream == -1)
1393                 return 0;
1394         if (! stream && nid != codec->afg)
1395                 stream = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
1396         if (! stream || stream == -1)
1397                 return 0;
1398
1399         if (stream & AC_SUPFMT_PCM) {
1400                 switch (format & 0xf0) {
1401                 case 0x00:
1402                         if (! (val & AC_SUPPCM_BITS_8))
1403                                 return 0;
1404                         break;
1405                 case 0x10:
1406                         if (! (val & AC_SUPPCM_BITS_16))
1407                                 return 0;
1408                         break;
1409                 case 0x20:
1410                         if (! (val & AC_SUPPCM_BITS_20))
1411                                 return 0;
1412                         break;
1413                 case 0x30:
1414                         if (! (val & AC_SUPPCM_BITS_24))
1415                                 return 0;
1416                         break;
1417                 case 0x40:
1418                         if (! (val & AC_SUPPCM_BITS_32))
1419                                 return 0;
1420                         break;
1421                 default:
1422                         return 0;
1423                 }
1424         } else {
1425                 /* FIXME: check for float32 and AC3? */
1426         }
1427
1428         return 1;
1429 }
1430
1431 /*
1432  * PCM stuff
1433  */
1434 static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
1435                                       struct hda_codec *codec,
1436                                       snd_pcm_substream_t *substream)
1437 {
1438         return 0;
1439 }
1440
1441 static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
1442                                    struct hda_codec *codec,
1443                                    unsigned int stream_tag,
1444                                    unsigned int format,
1445                                    snd_pcm_substream_t *substream)
1446 {
1447         snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
1448         return 0;
1449 }
1450
1451 static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
1452                                    struct hda_codec *codec,
1453                                    snd_pcm_substream_t *substream)
1454 {
1455         snd_hda_codec_setup_stream(codec, hinfo->nid, 0, 0, 0);
1456         return 0;
1457 }
1458
1459 static int set_pcm_default_values(struct hda_codec *codec, struct hda_pcm_stream *info)
1460 {
1461         if (info->nid) {
1462                 /* query support PCM information from the given NID */
1463                 if (! info->rates || ! info->formats)
1464                         snd_hda_query_supported_pcm(codec, info->nid,
1465                                                     info->rates ? NULL : &info->rates,
1466                                                     info->formats ? NULL : &info->formats,
1467                                                     info->maxbps ? NULL : &info->maxbps);
1468         }
1469         if (info->ops.open == NULL)
1470                 info->ops.open = hda_pcm_default_open_close;
1471         if (info->ops.close == NULL)
1472                 info->ops.close = hda_pcm_default_open_close;
1473         if (info->ops.prepare == NULL) {
1474                 snd_assert(info->nid, return -EINVAL);
1475                 info->ops.prepare = hda_pcm_default_prepare;
1476         }
1477         if (info->ops.cleanup == NULL) {
1478                 snd_assert(info->nid, return -EINVAL);
1479                 info->ops.cleanup = hda_pcm_default_cleanup;
1480         }
1481         return 0;
1482 }
1483
1484 /**
1485  * snd_hda_build_pcms - build PCM information
1486  * @bus: the BUS
1487  *
1488  * Create PCM information for each codec included in the bus.
1489  *
1490  * The build_pcms codec patch is requested to set up codec->num_pcms and
1491  * codec->pcm_info properly.  The array is referred by the top-level driver
1492  * to create its PCM instances.
1493  * The allocated codec->pcm_info should be released in codec->patch_ops.free
1494  * callback.
1495  *
1496  * At least, substreams, channels_min and channels_max must be filled for
1497  * each stream.  substreams = 0 indicates that the stream doesn't exist.
1498  * When rates and/or formats are zero, the supported values are queried
1499  * from the given nid.  The nid is used also by the default ops.prepare
1500  * and ops.cleanup callbacks.
1501  *
1502  * The driver needs to call ops.open in its open callback.  Similarly,
1503  * ops.close is supposed to be called in the close callback.
1504  * ops.prepare should be called in the prepare or hw_params callback
1505  * with the proper parameters for set up.
1506  * ops.cleanup should be called in hw_free for clean up of streams.
1507  *
1508  * This function returns 0 if successfull, or a negative error code.
1509  */
1510 int snd_hda_build_pcms(struct hda_bus *bus)
1511 {
1512         struct list_head *p;
1513
1514         list_for_each(p, &bus->codec_list) {
1515                 struct hda_codec *codec = list_entry(p, struct hda_codec, list);
1516                 unsigned int pcm, s;
1517                 int err;
1518                 if (! codec->patch_ops.build_pcms)
1519                         continue;
1520                 err = codec->patch_ops.build_pcms(codec);
1521                 if (err < 0)
1522                         return err;
1523                 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
1524                         for (s = 0; s < 2; s++) {
1525                                 struct hda_pcm_stream *info;
1526                                 info = &codec->pcm_info[pcm].stream[s];
1527                                 if (! info->substreams)
1528                                         continue;
1529                                 err = set_pcm_default_values(codec, info);
1530                                 if (err < 0)
1531                                         return err;
1532                         }
1533                 }
1534         }
1535         return 0;
1536 }
1537
1538
1539 /**
1540  * snd_hda_check_board_config - compare the current codec with the config table
1541  * @codec: the HDA codec
1542  * @tbl: configuration table, terminated by null entries
1543  *
1544  * Compares the modelname or PCI subsystem id of the current codec with the
1545  * given configuration table.  If a matching entry is found, returns its
1546  * config value (supposed to be 0 or positive).
1547  *
1548  * If no entries are matching, the function returns a negative value.
1549  */
1550 int snd_hda_check_board_config(struct hda_codec *codec, const struct hda_board_config *tbl)
1551 {
1552         const struct hda_board_config *c;
1553
1554         if (codec->bus->modelname) {
1555                 for (c = tbl; c->modelname || c->pci_subvendor; c++) {
1556                         if (c->modelname &&
1557                             ! strcmp(codec->bus->modelname, c->modelname)) {
1558                                 snd_printd(KERN_INFO "hda_codec: model '%s' is selected\n", c->modelname);
1559                                 return c->config;
1560                         }
1561                 }
1562         }
1563
1564         if (codec->bus->pci) {
1565                 u16 subsystem_vendor, subsystem_device;
1566                 pci_read_config_word(codec->bus->pci, PCI_SUBSYSTEM_VENDOR_ID, &subsystem_vendor);
1567                 pci_read_config_word(codec->bus->pci, PCI_SUBSYSTEM_ID, &subsystem_device);
1568                 for (c = tbl; c->modelname || c->pci_subvendor; c++) {
1569                         if (c->pci_subvendor == subsystem_vendor &&
1570                             (! c->pci_subdevice /* all match */||
1571                              (c->pci_subdevice == subsystem_device))) {
1572                                 snd_printdd(KERN_INFO "hda_codec: PCI %x:%x, codec config %d is selected\n",
1573                                             subsystem_vendor, subsystem_device, c->config);
1574                                 return c->config;
1575                         }
1576                 }
1577         }
1578         return -1;
1579 }
1580
1581 /**
1582  * snd_hda_add_new_ctls - create controls from the array
1583  * @codec: the HDA codec
1584  * @knew: the array of snd_kcontrol_new_t
1585  *
1586  * This helper function creates and add new controls in the given array.
1587  * The array must be terminated with an empty entry as terminator.
1588  *
1589  * Returns 0 if successful, or a negative error code.
1590  */
1591 int snd_hda_add_new_ctls(struct hda_codec *codec, snd_kcontrol_new_t *knew)
1592 {
1593         int err;
1594
1595         for (; knew->name; knew++) {
1596                 err = snd_ctl_add(codec->bus->card, snd_ctl_new1(knew, codec));
1597                 if (err < 0)
1598                         return err;
1599         }
1600         return 0;
1601 }
1602
1603
1604 /*
1605  * input MUX helper
1606  */
1607 int snd_hda_input_mux_info(const struct hda_input_mux *imux, snd_ctl_elem_info_t *uinfo)
1608 {
1609         unsigned int index;
1610
1611         uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1612         uinfo->count = 1;
1613         uinfo->value.enumerated.items = imux->num_items;
1614         index = uinfo->value.enumerated.item;
1615         if (index >= imux->num_items)
1616                 index = imux->num_items - 1;
1617         strcpy(uinfo->value.enumerated.name, imux->items[index].label);
1618         return 0;
1619 }
1620
1621 int snd_hda_input_mux_put(struct hda_codec *codec, const struct hda_input_mux *imux,
1622                           snd_ctl_elem_value_t *ucontrol, hda_nid_t nid,
1623                           unsigned int *cur_val)
1624 {
1625         unsigned int idx;
1626
1627         idx = ucontrol->value.enumerated.item[0];
1628         if (idx >= imux->num_items)
1629                 idx = imux->num_items - 1;
1630         if (*cur_val == idx && ! codec->in_resume)
1631                 return 0;
1632         snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
1633                             imux->items[idx].index);
1634         *cur_val = idx;
1635         return 1;
1636 }
1637
1638
1639 /*
1640  * Multi-channel / digital-out PCM helper functions
1641  */
1642
1643 /*
1644  * open the digital out in the exclusive mode
1645  */
1646 int snd_hda_multi_out_dig_open(struct hda_codec *codec, struct hda_multi_out *mout)
1647 {
1648         down(&codec->spdif_mutex);
1649         if (mout->dig_out_used) {
1650                 up(&codec->spdif_mutex);
1651                 return -EBUSY; /* already being used */
1652         }
1653         mout->dig_out_used = HDA_DIG_EXCLUSIVE;
1654         up(&codec->spdif_mutex);
1655         return 0;
1656 }
1657
1658 /*
1659  * release the digital out
1660  */
1661 int snd_hda_multi_out_dig_close(struct hda_codec *codec, struct hda_multi_out *mout)
1662 {
1663         down(&codec->spdif_mutex);
1664         mout->dig_out_used = 0;
1665         up(&codec->spdif_mutex);
1666         return 0;
1667 }
1668
1669 /*
1670  * set up more restrictions for analog out
1671  */
1672 int snd_hda_multi_out_analog_open(struct hda_codec *codec, struct hda_multi_out *mout,
1673                                   snd_pcm_substream_t *substream)
1674 {
1675         substream->runtime->hw.channels_max = mout->max_channels;
1676         return snd_pcm_hw_constraint_step(substream->runtime, 0,
1677                                           SNDRV_PCM_HW_PARAM_CHANNELS, 2);
1678 }
1679
1680 /*
1681  * set up the i/o for analog out
1682  * when the digital out is available, copy the front out to digital out, too.
1683  */
1684 int snd_hda_multi_out_analog_prepare(struct hda_codec *codec, struct hda_multi_out *mout,
1685                                      unsigned int stream_tag,
1686                                      unsigned int format,
1687                                      snd_pcm_substream_t *substream)
1688 {
1689         hda_nid_t *nids = mout->dac_nids;
1690         int chs = substream->runtime->channels;
1691         int i;
1692
1693         down(&codec->spdif_mutex);
1694         if (mout->dig_out_nid && mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
1695                 if (chs == 2 &&
1696                     snd_hda_is_supported_format(codec, mout->dig_out_nid, format) &&
1697                     ! (codec->spdif_status & IEC958_AES0_NONAUDIO)) {
1698                         mout->dig_out_used = HDA_DIG_ANALOG_DUP;
1699                         /* setup digital receiver */
1700                         snd_hda_codec_setup_stream(codec, mout->dig_out_nid,
1701                                                    stream_tag, 0, format);
1702                 } else {
1703                         mout->dig_out_used = 0;
1704                         snd_hda_codec_setup_stream(codec, mout->dig_out_nid, 0, 0, 0);
1705                 }
1706         }
1707         up(&codec->spdif_mutex);
1708
1709         /* front */
1710         snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag, 0, format);
1711         if (mout->hp_nid)
1712                 /* headphone out will just decode front left/right (stereo) */
1713                 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag, 0, format);
1714         /* surrounds */
1715         for (i = 1; i < mout->num_dacs; i++) {
1716                 if (chs >= (i + 1) * 2) /* independent out */
1717                         snd_hda_codec_setup_stream(codec, nids[i], stream_tag, i * 2,
1718                                                    format);
1719                 else /* copy front */
1720                         snd_hda_codec_setup_stream(codec, nids[i], stream_tag, 0,
1721                                                    format);
1722         }
1723         return 0;
1724 }
1725
1726 /*
1727  * clean up the setting for analog out
1728  */
1729 int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec, struct hda_multi_out *mout)
1730 {
1731         hda_nid_t *nids = mout->dac_nids;
1732         int i;
1733
1734         for (i = 0; i < mout->num_dacs; i++)
1735                 snd_hda_codec_setup_stream(codec, nids[i], 0, 0, 0);
1736         if (mout->hp_nid)
1737                 snd_hda_codec_setup_stream(codec, mout->hp_nid, 0, 0, 0);
1738         down(&codec->spdif_mutex);
1739         if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
1740                 snd_hda_codec_setup_stream(codec, mout->dig_out_nid, 0, 0, 0);
1741                 mout->dig_out_used = 0;
1742         }
1743         up(&codec->spdif_mutex);
1744         return 0;
1745 }
1746
1747 /*
1748  * Helper for automatic ping configuration
1749  */
1750 /* parse all pin widgets and store the useful pin nids to cfg */
1751 int snd_hda_parse_pin_def_config(struct hda_codec *codec, struct auto_pin_cfg *cfg)
1752 {
1753         hda_nid_t nid, nid_start;
1754         int i, j, nodes;
1755         short seq, sequences[4], assoc_line_out;
1756
1757         memset(cfg, 0, sizeof(*cfg));
1758
1759         memset(sequences, 0, sizeof(sequences));
1760         assoc_line_out = 0;
1761
1762         nodes = snd_hda_get_sub_nodes(codec, codec->afg, &nid_start);
1763         for (nid = nid_start; nid < nodes + nid_start; nid++) {
1764                 unsigned int wid_caps = snd_hda_param_read(codec, nid,
1765                                                            AC_PAR_AUDIO_WIDGET_CAP);
1766                 unsigned int wid_type = (wid_caps & AC_WCAP_TYPE) >> AC_WCAP_TYPE_SHIFT;
1767                 unsigned int def_conf;
1768                 short assoc, loc;
1769
1770                 /* read all default configuration for pin complex */
1771                 if (wid_type != AC_WID_PIN)
1772                         continue;
1773                 def_conf = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONFIG_DEFAULT, 0);
1774                 if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE)
1775                         continue;
1776                 loc = get_defcfg_location(def_conf);
1777                 switch (get_defcfg_device(def_conf)) {
1778                 case AC_JACK_LINE_OUT:
1779                 case AC_JACK_SPEAKER:
1780                         seq = get_defcfg_sequence(def_conf);
1781                         assoc = get_defcfg_association(def_conf);
1782                         if (! assoc)
1783                                 continue;
1784                         if (! assoc_line_out)
1785                                 assoc_line_out = assoc;
1786                         else if (assoc_line_out != assoc)
1787                                 continue;
1788                         if (cfg->line_outs >= ARRAY_SIZE(cfg->line_out_pins))
1789                                 continue;
1790                         cfg->line_out_pins[cfg->line_outs] = nid;
1791                         sequences[cfg->line_outs] = seq;
1792                         cfg->line_outs++;
1793                         break;
1794                 case AC_JACK_HP_OUT:
1795                         cfg->hp_pin = nid;
1796                         break;
1797                 case AC_JACK_MIC_IN:
1798                         if (loc == AC_JACK_LOC_FRONT)
1799                                 cfg->input_pins[AUTO_PIN_FRONT_MIC] = nid;
1800                         else
1801                                 cfg->input_pins[AUTO_PIN_MIC] = nid;
1802                         break;
1803                 case AC_JACK_LINE_IN:
1804                         if (loc == AC_JACK_LOC_FRONT)
1805                                 cfg->input_pins[AUTO_PIN_FRONT_LINE] = nid;
1806                         else
1807                                 cfg->input_pins[AUTO_PIN_LINE] = nid;
1808                         break;
1809                 case AC_JACK_CD:
1810                         cfg->input_pins[AUTO_PIN_CD] = nid;
1811                         break;
1812                 case AC_JACK_AUX:
1813                         cfg->input_pins[AUTO_PIN_AUX] = nid;
1814                         break;
1815                 case AC_JACK_SPDIF_OUT:
1816                         cfg->dig_out_pin = nid;
1817                         break;
1818                 case AC_JACK_SPDIF_IN:
1819                         cfg->dig_in_pin = nid;
1820                         break;
1821                 }
1822         }
1823
1824         /* sort by sequence */
1825         for (i = 0; i < cfg->line_outs; i++)
1826                 for (j = i + 1; j < cfg->line_outs; j++)
1827                         if (sequences[i] > sequences[j]) {
1828                                 seq = sequences[i];
1829                                 sequences[i] = sequences[j];
1830                                 sequences[j] = seq;
1831                                 nid = cfg->line_out_pins[i];
1832                                 cfg->line_out_pins[i] = cfg->line_out_pins[j];
1833                                 cfg->line_out_pins[j] = nid;
1834                         }
1835
1836         /* Reorder the surround channels
1837          * ALSA sequence is front/surr/clfe/side
1838          * HDA sequence is:
1839          *    4-ch: front/surr  =>  OK as it is
1840          *    6-ch: front/clfe/surr
1841          *    8-ch: front/clfe/side/surr
1842          */
1843         switch (cfg->line_outs) {
1844         case 3:
1845                 nid = cfg->line_out_pins[1];
1846                 cfg->line_out_pins[1] = cfg->line_out_pins[2];
1847                 cfg->line_out_pins[2] = nid;
1848                 break;
1849         case 4:
1850                 nid = cfg->line_out_pins[1];
1851                 cfg->line_out_pins[1] = cfg->line_out_pins[3];
1852                 cfg->line_out_pins[3] = cfg->line_out_pins[2];
1853                 cfg->line_out_pins[2] = nid;
1854                 break;
1855         }
1856
1857         return 0;
1858 }
1859
1860 #ifdef CONFIG_PM
1861 /*
1862  * power management
1863  */
1864
1865 /**
1866  * snd_hda_suspend - suspend the codecs
1867  * @bus: the HDA bus
1868  * @state: suspsend state
1869  *
1870  * Returns 0 if successful.
1871  */
1872 int snd_hda_suspend(struct hda_bus *bus, pm_message_t state)
1873 {
1874         struct list_head *p;
1875
1876         /* FIXME: should handle power widget capabilities */
1877         list_for_each(p, &bus->codec_list) {
1878                 struct hda_codec *codec = list_entry(p, struct hda_codec, list);
1879                 if (codec->patch_ops.suspend)
1880                         codec->patch_ops.suspend(codec, state);
1881         }
1882         return 0;
1883 }
1884
1885 /**
1886  * snd_hda_resume - resume the codecs
1887  * @bus: the HDA bus
1888  * @state: resume state
1889  *
1890  * Returns 0 if successful.
1891  */
1892 int snd_hda_resume(struct hda_bus *bus)
1893 {
1894         struct list_head *p;
1895
1896         list_for_each(p, &bus->codec_list) {
1897                 struct hda_codec *codec = list_entry(p, struct hda_codec, list);
1898                 if (codec->patch_ops.resume)
1899                         codec->patch_ops.resume(codec);
1900         }
1901         return 0;
1902 }
1903
1904 /**
1905  * snd_hda_resume_ctls - resume controls in the new control list
1906  * @codec: the HDA codec
1907  * @knew: the array of snd_kcontrol_new_t
1908  *
1909  * This function resumes the mixer controls in the snd_kcontrol_new_t array,
1910  * originally for snd_hda_add_new_ctls().
1911  * The array must be terminated with an empty entry as terminator.
1912  */
1913 int snd_hda_resume_ctls(struct hda_codec *codec, snd_kcontrol_new_t *knew)
1914 {
1915         snd_ctl_elem_value_t *val;
1916
1917         val = kmalloc(sizeof(*val), GFP_KERNEL);
1918         if (! val)
1919                 return -ENOMEM;
1920         codec->in_resume = 1;
1921         for (; knew->name; knew++) {
1922                 int i, count;
1923                 count = knew->count ? knew->count : 1;
1924                 for (i = 0; i < count; i++) {
1925                         memset(val, 0, sizeof(*val));
1926                         val->id.iface = knew->iface;
1927                         val->id.device = knew->device;
1928                         val->id.subdevice = knew->subdevice;
1929                         strcpy(val->id.name, knew->name);
1930                         val->id.index = knew->index ? knew->index : i;
1931                         /* Assume that get callback reads only from cache,
1932                          * not accessing to the real hardware
1933                          */
1934                         if (snd_ctl_elem_read(codec->bus->card, val) < 0)
1935                                 continue;
1936                         snd_ctl_elem_write(codec->bus->card, NULL, val);
1937                 }
1938         }
1939         codec->in_resume = 0;
1940         kfree(val);
1941         return 0;
1942 }
1943
1944 /**
1945  * snd_hda_resume_spdif_out - resume the digital out
1946  * @codec: the HDA codec
1947  */
1948 int snd_hda_resume_spdif_out(struct hda_codec *codec)
1949 {
1950         return snd_hda_resume_ctls(codec, dig_mixes);
1951 }
1952
1953 /**
1954  * snd_hda_resume_spdif_in - resume the digital in
1955  * @codec: the HDA codec
1956  */
1957 int snd_hda_resume_spdif_in(struct hda_codec *codec)
1958 {
1959         return snd_hda_resume_ctls(codec, dig_in_ctls);
1960 }
1961 #endif
1962
1963 /*
1964  * symbols exported for controller modules
1965  */
1966 EXPORT_SYMBOL(snd_hda_codec_read);
1967 EXPORT_SYMBOL(snd_hda_codec_write);
1968 EXPORT_SYMBOL(snd_hda_sequence_write);
1969 EXPORT_SYMBOL(snd_hda_get_sub_nodes);
1970 EXPORT_SYMBOL(snd_hda_queue_unsol_event);
1971 EXPORT_SYMBOL(snd_hda_bus_new);
1972 EXPORT_SYMBOL(snd_hda_codec_new);
1973 EXPORT_SYMBOL(snd_hda_codec_setup_stream);
1974 EXPORT_SYMBOL(snd_hda_calc_stream_format);
1975 EXPORT_SYMBOL(snd_hda_build_pcms);
1976 EXPORT_SYMBOL(snd_hda_build_controls);
1977 #ifdef CONFIG_PM
1978 EXPORT_SYMBOL(snd_hda_suspend);
1979 EXPORT_SYMBOL(snd_hda_resume);
1980 #endif
1981
1982 /*
1983  *  INIT part
1984  */
1985
1986 static int __init alsa_hda_init(void)
1987 {
1988         return 0;
1989 }
1990
1991 static void __exit alsa_hda_exit(void)
1992 {
1993 }
1994
1995 module_init(alsa_hda_init)
1996 module_exit(alsa_hda_exit)