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