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