2 * Universal Interface for Intel High Definition Audio Codec
4 * Copyright (c) 2004 Takashi Iwai <tiwai@suse.de>
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.
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.
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
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"
35 MODULE_AUTHOR("Takashi Iwai <tiwai@suse.de>");
36 MODULE_DESCRIPTION("Universal interface for High Definition Audio Codec");
37 MODULE_LICENSE("GPL");
41 * vendor / preset table
44 struct hda_vendor_id {
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" },
59 #include "hda_patch.h"
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
70 * Send a single command and read the corresponding response.
72 * Returns the obtained response value, or -1 for an error.
74 unsigned int snd_hda_codec_read(struct hda_codec *codec, hda_nid_t nid, int direct,
75 unsigned int verb, unsigned int parm)
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);
82 res = (unsigned int)-1;
83 up(&codec->bus->cmd_mutex);
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
95 * Send a single command without waiting for response.
97 * Returns 0 if successful, or a negative error code.
99 int snd_hda_codec_write(struct hda_codec *codec, hda_nid_t nid, int direct,
100 unsigned int verb, unsigned int parm)
103 down(&codec->bus->cmd_mutex);
104 err = codec->bus->ops.command(codec, nid, direct, verb, parm);
105 up(&codec->bus->cmd_mutex);
110 * snd_hda_sequence_write - sequence writes
111 * @codec: the HDA codec
112 * @seq: VERB array to send
114 * Send the commands sequentially from the given array.
115 * The array must be terminated with NID=0.
117 void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq)
119 for (; seq->nid; seq++)
120 snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param);
124 * snd_hda_get_sub_nodes - get the range of sub nodes
125 * @codec: the HDA codec
127 * @start_id: the pointer to store the start NID
129 * Parse the NID and store the start NID of its sub-nodes.
130 * Returns the number of sub-nodes.
132 int snd_hda_get_sub_nodes(struct hda_codec *codec, hda_nid_t nid, hda_nid_t *start_id)
136 parm = snd_hda_param_read(codec, nid, AC_PAR_NODE_COUNT);
137 *start_id = (parm >> 16) & 0x7fff;
138 return (int)(parm & 0x7fff);
142 * snd_hda_get_connections - get connection list
143 * @codec: the HDA codec
145 * @conn_list: connection list array
146 * @max_conns: max. number of connections to store
148 * Parses the connection list of the given widget and stores the list
151 * Returns the number of connections, or a negative error code.
153 int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
154 hda_nid_t *conn_list, int max_conns)
157 int i, j, conn_len, num_tupples, conns;
158 unsigned int shift, num_elems, mask;
160 snd_assert(conn_list && max_conns > 0, return -EINVAL);
162 parm = snd_hda_param_read(codec, nid, AC_PAR_CONNLIST_LEN);
163 if (parm & AC_CLIST_LONG) {
172 conn_len = parm & AC_CLIST_LENGTH;
173 num_tupples = num_elems / 2;
174 mask = (1 << (shift-1)) - 1;
177 return 0; /* no connection */
180 /* single connection */
181 parm = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONNECT_LIST, 0);
182 conn_list[0] = parm & mask;
186 /* multi connection */
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++) {
192 hda_nid_t val1, val2, n;
193 range_val = parm & (1 << (shift-1)); /* ranges */
199 /* ranges between val1 and val2 */
201 snd_printk(KERN_WARNING "hda_codec: invalid dep_range_val %x:%x\n", val1, val2);
204 for (n = val1; n <= val2; n++) {
205 if (conns >= max_conns)
207 conn_list[conns++] = n;
212 if (conns >= max_conns)
214 conn_list[conns++] = val1;
217 if (conns >= max_conns)
219 conn_list[conns++] = val2;
228 * snd_hda_queue_unsol_event - add an unsolicited event to queue
230 * @res: unsolicited event (lower 32bit of RIRB entry)
231 * @res_ex: codec addr and flags (upper 32bit or RIRB entry)
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.
237 * Returns 0 if successful, or a negative error code.
239 int snd_hda_queue_unsol_event(struct hda_bus *bus, u32 res, u32 res_ex)
241 struct hda_bus_unsolicited *unsol;
244 if ((unsol = bus->unsol) == NULL)
247 wp = (unsol->wp + 1) % HDA_UNSOL_QUEUE_SIZE;
251 unsol->queue[wp] = res;
252 unsol->queue[wp + 1] = res_ex;
254 queue_work(unsol->workq, &unsol->work);
260 * process queueud unsolicited events
262 static void process_unsol_events(void *data)
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;
269 while (unsol->rp != unsol->wp) {
270 rp = (unsol->rp + 1) % HDA_UNSOL_QUEUE_SIZE;
273 res = unsol->queue[rp];
274 caddr = unsol->queue[rp + 1];
275 if (! (caddr & (1 << 4))) /* no unsolicited event? */
277 codec = bus->caddr_tbl[caddr & 0x0f];
278 if (codec && codec->patch_ops.unsol_event)
279 codec->patch_ops.unsol_event(codec, res);
284 * initialize unsolicited queue
286 static int init_unsol_queue(struct hda_bus *bus)
288 struct hda_bus_unsolicited *unsol;
290 unsol = kcalloc(1, sizeof(*unsol), GFP_KERNEL);
292 snd_printk(KERN_ERR "hda_codec: can't allocate unsolicited queue\n");
295 unsol->workq = create_workqueue("hda_codec");
296 if (! unsol->workq) {
297 snd_printk(KERN_ERR "hda_codec: can't create workqueue\n");
301 INIT_WORK(&unsol->work, process_unsol_events, bus);
309 static void snd_hda_codec_free(struct hda_codec *codec);
311 static int snd_hda_bus_free(struct hda_bus *bus)
313 struct list_head *p, *n;
318 destroy_workqueue(bus->unsol->workq);
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);
325 if (bus->ops.private_free)
326 bus->ops.private_free(bus);
331 static int snd_hda_bus_dev_free(snd_device_t *device)
333 struct hda_bus *bus = device->device_data;
334 return snd_hda_bus_free(bus);
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
343 * Returns 0 if successful, or a negative error code.
345 int snd_hda_bus_new(snd_card_t *card, const struct hda_bus_template *temp,
346 struct hda_bus **busp)
350 static snd_device_ops_t dev_ops = {
351 .dev_free = snd_hda_bus_dev_free,
354 snd_assert(temp, return -EINVAL);
355 snd_assert(temp->ops.command && temp->ops.get_response, return -EINVAL);
360 bus = kcalloc(1, sizeof(*bus), GFP_KERNEL);
362 snd_printk(KERN_ERR "can't allocate struct hda_bus\n");
367 bus->private_data = temp->private_data;
368 bus->pci = temp->pci;
369 bus->modelname = temp->modelname;
370 bus->ops = temp->ops;
372 init_MUTEX(&bus->cmd_mutex);
373 INIT_LIST_HEAD(&bus->codec_list);
375 init_unsol_queue(bus);
377 if ((err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops)) < 0) {
378 snd_hda_bus_free(bus);
388 * find a matching codec preset
390 static const struct hda_codec_preset *find_codec_preset(struct hda_codec *codec)
392 const struct hda_codec_preset **tbl, *preset;
394 for (tbl = hda_preset_tables; *tbl; tbl++) {
395 for (preset = *tbl; preset->id; preset++) {
396 u32 mask = preset->mask;
399 if (preset->id == (codec->vendor_id & mask))
407 * snd_hda_get_codec_name - store the codec name
409 void snd_hda_get_codec_name(struct hda_codec *codec,
410 char *name, int namelen)
412 const struct hda_vendor_id *c;
413 const char *vendor = NULL;
414 u16 vendor_id = codec->vendor_id >> 16;
417 for (c = hda_vendor_ids; c->id; c++) {
418 if (c->id == vendor_id) {
424 sprintf(tmp, "Generic %04x", vendor_id);
427 if (codec->preset && codec->preset->name)
428 snprintf(name, namelen, "%s %s", vendor, codec->preset->name);
430 snprintf(name, namelen, "%s ID %x", vendor, codec->vendor_id & 0xffff);
434 * look for an AFG node
436 * return 0 if not found
438 static int look_for_afg_node(struct hda_codec *codec)
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)
455 static void snd_hda_codec_free(struct hda_codec *codec)
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);
466 static void init_amp_hash(struct hda_codec *codec);
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
474 * Returns 0 if successful, or a negative error code.
476 int snd_hda_codec_new(struct hda_bus *bus, unsigned int codec_addr,
477 struct hda_codec **codecp)
479 struct hda_codec *codec;
483 snd_assert(bus, return -EINVAL);
484 snd_assert(codec_addr <= HDA_MAX_CODEC_ADDRESS, return -EINVAL);
486 if (bus->caddr_tbl[codec_addr]) {
487 snd_printk(KERN_ERR "hda_codec: address 0x%x is already occupied\n", codec_addr);
491 codec = kcalloc(1, sizeof(*codec), GFP_KERNEL);
493 snd_printk(KERN_ERR "can't allocate struct hda_codec\n");
498 codec->addr = codec_addr;
499 init_MUTEX(&codec->spdif_mutex);
500 init_amp_hash(codec);
502 list_add_tail(&codec->list, &bus->codec_list);
503 bus->caddr_tbl[codec_addr] = codec;
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);
509 /* FIXME: support for multiple AFGs? */
510 codec->afg = look_for_afg_node(codec);
512 snd_printdd("hda_codec: no AFG node found\n");
513 snd_hda_codec_free(codec);
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));
522 if (codec->preset && codec->preset->patch)
523 err = codec->preset->patch(codec);
525 err = snd_hda_parse_generic_codec(codec);
527 snd_hda_codec_free(codec);
531 snd_hda_codec_proc_new(codec);
533 sprintf(component, "HDA:%08x", codec->vendor_id);
534 snd_component_add(codec->bus->card, component);
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.
549 void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid, u32 stream_tag,
550 int channel_id, int format)
555 snd_printdd("hda_codec_setup_stream: NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
556 nid, stream_tag, channel_id, format);
557 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID,
558 (stream_tag << 4) | channel_id);
560 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, format);
565 * amp access functions
568 #define HDA_HASH_KEY(nid,dir,idx) (u32)((nid) + (idx) * 32 + (dir) * 64)
569 #define INFO_AMP_CAPS (1<<0)
570 #define INFO_AMP_VOL (1<<1)
572 /* initialize the hash table */
573 static void init_amp_hash(struct hda_codec *codec)
575 memset(codec->amp_hash, 0xff, sizeof(codec->amp_hash));
576 codec->num_amp_entries = 0;
579 /* query the hash. allocate an entry if not found. */
580 static struct hda_amp_info *get_alloc_amp_hash(struct hda_codec *codec, u32 key)
582 u16 idx = key % (u16)ARRAY_SIZE(codec->amp_hash);
583 u16 cur = codec->amp_hash[idx];
584 struct hda_amp_info *info;
586 while (cur != 0xffff) {
587 info = &codec->amp_info[cur];
588 if (info->key == key)
593 /* add a new hash entry */
594 if (codec->num_amp_entries >= ARRAY_SIZE(codec->amp_info)) {
595 snd_printk(KERN_ERR "hda_codec: Tooooo many amps!\n");
598 cur = codec->num_amp_entries++;
599 info = &codec->amp_info[cur];
601 info->status = 0; /* not initialized yet */
602 info->next = codec->amp_hash[idx];
603 codec->amp_hash[idx] = cur;
609 * query AMP capabilities for the given widget and direction
611 static u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
613 struct hda_amp_info *info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, 0));
617 if (! (info->status & INFO_AMP_CAPS)) {
618 if (!(snd_hda_param_read(codec, nid, AC_PAR_AUDIO_WIDGET_CAP) & AC_WCAP_AMP_OVRD))
620 info->amp_caps = snd_hda_param_read(codec, nid, direction == HDA_OUTPUT ?
621 AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP);
622 info->status |= INFO_AMP_CAPS;
624 return info->amp_caps;
628 * read the current volume to info
629 * if the cache exists, read from the cache.
631 static void get_vol_mute(struct hda_codec *codec, struct hda_amp_info *info,
632 hda_nid_t nid, int ch, int direction, int index)
636 if (info->status & (INFO_AMP_VOL << ch))
639 parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
640 parm |= direction == HDA_OUTPUT ? AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
642 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_AMP_GAIN_MUTE, parm);
643 info->vol[ch] = val & 0xff;
644 info->status |= INFO_AMP_VOL << ch;
648 * write the current volume in info to the h/w
650 static void put_vol_mute(struct hda_codec *codec,
651 hda_nid_t nid, int ch, int direction, int index, int val)
655 parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
656 parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
657 parm |= index << AC_AMP_SET_INDEX_SHIFT;
659 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
663 * read/write AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
665 static int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch, int direction, int index)
667 struct hda_amp_info *info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
670 get_vol_mute(codec, info, nid, ch, direction, index);
671 return info->vol[ch];
674 static int snd_hda_codec_amp_write(struct hda_codec *codec, hda_nid_t nid, int ch, int direction, int idx, int val)
676 struct hda_amp_info *info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, idx));
679 get_vol_mute(codec, info, nid, ch, direction, idx);
680 if (info->vol[ch] == val && ! codec->in_resume)
682 put_vol_mute(codec, nid, ch, direction, idx, val);
689 * AMP control callbacks
691 /* retrieve parameters from private_value */
692 #define get_amp_nid(kc) ((kc)->private_value & 0xffff)
693 #define get_amp_channels(kc) (((kc)->private_value >> 16) & 0x3)
694 #define get_amp_direction(kc) (((kc)->private_value >> 18) & 0x1)
695 #define get_amp_index(kc) (((kc)->private_value >> 19) & 0xf)
698 int snd_hda_mixer_amp_volume_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
700 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
701 u16 nid = get_amp_nid(kcontrol);
702 u8 chs = get_amp_channels(kcontrol);
703 int dir = get_amp_direction(kcontrol);
706 caps = query_amp_caps(codec, nid, dir);
707 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT; /* num steps */
709 printk(KERN_WARNING "hda_codec: num_steps = 0 for NID=0x%x\n", nid);
712 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
713 uinfo->count = chs == 3 ? 2 : 1;
714 uinfo->value.integer.min = 0;
715 uinfo->value.integer.max = caps;
719 int snd_hda_mixer_amp_volume_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
721 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
722 hda_nid_t nid = get_amp_nid(kcontrol);
723 int chs = get_amp_channels(kcontrol);
724 int dir = get_amp_direction(kcontrol);
725 int idx = get_amp_index(kcontrol);
726 long *valp = ucontrol->value.integer.value;
729 *valp++ = snd_hda_codec_amp_read(codec, nid, 0, dir, idx) & 0x7f;
731 *valp = snd_hda_codec_amp_read(codec, nid, 1, dir, idx) & 0x7f;
735 int snd_hda_mixer_amp_volume_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
737 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
738 hda_nid_t nid = get_amp_nid(kcontrol);
739 int chs = get_amp_channels(kcontrol);
740 int dir = get_amp_direction(kcontrol);
741 int idx = get_amp_index(kcontrol);
743 long *valp = ucontrol->value.integer.value;
748 val |= snd_hda_codec_amp_read(codec, nid, 0, dir, idx) & 0x80;
749 change = snd_hda_codec_amp_write(codec, nid, 0, dir, idx, val);
754 val |= snd_hda_codec_amp_read(codec, nid, 1, dir, idx) & 0x80;
755 change |= snd_hda_codec_amp_write(codec, nid, 1, dir, idx, val);
761 int snd_hda_mixer_amp_switch_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
763 int chs = get_amp_channels(kcontrol);
765 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
766 uinfo->count = chs == 3 ? 2 : 1;
767 uinfo->value.integer.min = 0;
768 uinfo->value.integer.max = 1;
772 int snd_hda_mixer_amp_switch_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
774 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
775 hda_nid_t nid = get_amp_nid(kcontrol);
776 int chs = get_amp_channels(kcontrol);
777 int dir = get_amp_direction(kcontrol);
778 int idx = get_amp_index(kcontrol);
779 long *valp = ucontrol->value.integer.value;
782 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) & 0x80) ? 0 : 1;
784 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) & 0x80) ? 0 : 1;
788 int snd_hda_mixer_amp_switch_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
790 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
791 hda_nid_t nid = get_amp_nid(kcontrol);
792 int chs = get_amp_channels(kcontrol);
793 int dir = get_amp_direction(kcontrol);
794 int idx = get_amp_index(kcontrol);
796 long *valp = ucontrol->value.integer.value;
800 val = snd_hda_codec_amp_read(codec, nid, 0, dir, idx) & 0x7f;
801 val |= *valp ? 0 : 0x80;
802 change = snd_hda_codec_amp_write(codec, nid, 0, dir, idx, val);
806 val = snd_hda_codec_amp_read(codec, nid, 1, dir, idx) & 0x7f;
807 val |= *valp ? 0 : 0x80;
808 change = snd_hda_codec_amp_write(codec, nid, 1, dir, idx, val);
817 static int snd_hda_spdif_mask_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
819 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
824 static int snd_hda_spdif_cmask_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
826 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
827 IEC958_AES0_NONAUDIO |
828 IEC958_AES0_CON_EMPHASIS_5015 |
829 IEC958_AES0_CON_NOT_COPYRIGHT;
830 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
831 IEC958_AES1_CON_ORIGINAL;
835 static int snd_hda_spdif_pmask_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
837 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
838 IEC958_AES0_NONAUDIO |
839 IEC958_AES0_PRO_EMPHASIS_5015;
843 static int snd_hda_spdif_default_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
845 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
847 ucontrol->value.iec958.status[0] = codec->spdif_status & 0xff;
848 ucontrol->value.iec958.status[1] = (codec->spdif_status >> 8) & 0xff;
849 ucontrol->value.iec958.status[2] = (codec->spdif_status >> 16) & 0xff;
850 ucontrol->value.iec958.status[3] = (codec->spdif_status >> 24) & 0xff;
855 /* convert from SPDIF status bits to HDA SPDIF bits
856 * bit 0 (DigEn) is always set zero (to be filled later)
858 static unsigned short convert_from_spdif_status(unsigned int sbits)
860 unsigned short val = 0;
862 if (sbits & IEC958_AES0_PROFESSIONAL)
864 if (sbits & IEC958_AES0_NONAUDIO)
866 if (sbits & IEC958_AES0_PROFESSIONAL) {
867 if ((sbits & IEC958_AES0_PRO_EMPHASIS) == IEC958_AES0_PRO_EMPHASIS_5015)
870 if ((sbits & IEC958_AES0_CON_EMPHASIS) == IEC958_AES0_CON_EMPHASIS_5015)
872 if (! (sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
874 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
876 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
881 /* convert to SPDIF status bits from HDA SPDIF bits
883 static unsigned int convert_to_spdif_status(unsigned short val)
885 unsigned int sbits = 0;
888 sbits |= IEC958_AES0_NONAUDIO;
890 sbits |= IEC958_AES0_PROFESSIONAL;
891 if (sbits & IEC958_AES0_PROFESSIONAL) {
892 if (sbits & (1 << 3))
893 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
896 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
897 if (! (val & (1 << 4)))
898 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
900 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
901 sbits |= val & (0x7f << 8);
906 static int snd_hda_spdif_default_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
908 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
909 hda_nid_t nid = kcontrol->private_value;
913 down(&codec->spdif_mutex);
914 codec->spdif_status = ucontrol->value.iec958.status[0] |
915 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
916 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
917 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
918 val = convert_from_spdif_status(codec->spdif_status);
919 val |= codec->spdif_ctls & 1;
920 change = codec->spdif_ctls != val;
921 codec->spdif_ctls = val;
923 if (change || codec->in_resume) {
924 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_DIGI_CONVERT_1, val & 0xff);
925 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_DIGI_CONVERT_2, val >> 8);
928 up(&codec->spdif_mutex);
932 static int snd_hda_spdif_out_switch_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
934 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
936 uinfo->value.integer.min = 0;
937 uinfo->value.integer.max = 1;
941 static int snd_hda_spdif_out_switch_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
943 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
945 ucontrol->value.integer.value[0] = codec->spdif_ctls & 1;
949 static int snd_hda_spdif_out_switch_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
951 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
952 hda_nid_t nid = kcontrol->private_value;
956 down(&codec->spdif_mutex);
957 val = codec->spdif_ctls & ~1;
958 if (ucontrol->value.integer.value[0])
960 change = codec->spdif_ctls != val;
961 if (change || codec->in_resume) {
962 codec->spdif_ctls = val;
963 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_DIGI_CONVERT_1, val & 0xff);
964 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE,
965 AC_AMP_SET_RIGHT | AC_AMP_SET_LEFT |
966 AC_AMP_SET_OUTPUT | ((val & 1) ? 0 : 0x80));
968 up(&codec->spdif_mutex);
972 static snd_kcontrol_new_t dig_mixes[] = {
974 .access = SNDRV_CTL_ELEM_ACCESS_READ,
975 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
976 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,CON_MASK),
977 .info = snd_hda_spdif_mask_info,
978 .get = snd_hda_spdif_cmask_get,
981 .access = SNDRV_CTL_ELEM_ACCESS_READ,
982 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
983 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,PRO_MASK),
984 .info = snd_hda_spdif_mask_info,
985 .get = snd_hda_spdif_pmask_get,
988 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
989 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
990 .info = snd_hda_spdif_mask_info,
991 .get = snd_hda_spdif_default_get,
992 .put = snd_hda_spdif_default_put,
995 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
996 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,SWITCH),
997 .info = snd_hda_spdif_out_switch_info,
998 .get = snd_hda_spdif_out_switch_get,
999 .put = snd_hda_spdif_out_switch_put,
1005 * snd_hda_create_spdif_out_ctls - create Output SPDIF-related controls
1006 * @codec: the HDA codec
1007 * @nid: audio out widget NID
1009 * Creates controls related with the SPDIF output.
1010 * Called from each patch supporting the SPDIF out.
1012 * Returns 0 if successful, or a negative error code.
1014 int snd_hda_create_spdif_out_ctls(struct hda_codec *codec, hda_nid_t nid)
1017 snd_kcontrol_t *kctl;
1018 snd_kcontrol_new_t *dig_mix;
1020 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
1021 kctl = snd_ctl_new1(dig_mix, codec);
1022 kctl->private_value = nid;
1023 if ((err = snd_ctl_add(codec->bus->card, kctl)) < 0)
1026 codec->spdif_ctls = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT, 0);
1027 codec->spdif_status = convert_to_spdif_status(codec->spdif_ctls);
1035 #define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
1037 static int snd_hda_spdif_in_switch_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
1039 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1041 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
1045 static int snd_hda_spdif_in_switch_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
1047 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1048 hda_nid_t nid = kcontrol->private_value;
1049 unsigned int val = !!ucontrol->value.integer.value[0];
1052 down(&codec->spdif_mutex);
1053 change = codec->spdif_in_enable != val;
1054 if (change || codec->in_resume) {
1055 codec->spdif_in_enable = val;
1056 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_DIGI_CONVERT_1, val);
1058 up(&codec->spdif_mutex);
1062 static int snd_hda_spdif_in_status_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
1064 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1065 hda_nid_t nid = kcontrol->private_value;
1069 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT, 0);
1070 sbits = convert_to_spdif_status(val);
1071 ucontrol->value.iec958.status[0] = sbits;
1072 ucontrol->value.iec958.status[1] = sbits >> 8;
1073 ucontrol->value.iec958.status[2] = sbits >> 16;
1074 ucontrol->value.iec958.status[3] = sbits >> 24;
1078 static snd_kcontrol_new_t dig_in_ctls[] = {
1080 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1081 .name = SNDRV_CTL_NAME_IEC958("",CAPTURE,SWITCH),
1082 .info = snd_hda_spdif_in_switch_info,
1083 .get = snd_hda_spdif_in_switch_get,
1084 .put = snd_hda_spdif_in_switch_put,
1087 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1088 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1089 .name = SNDRV_CTL_NAME_IEC958("",CAPTURE,DEFAULT),
1090 .info = snd_hda_spdif_mask_info,
1091 .get = snd_hda_spdif_in_status_get,
1097 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
1098 * @codec: the HDA codec
1099 * @nid: audio in widget NID
1101 * Creates controls related with the SPDIF input.
1102 * Called from each patch supporting the SPDIF in.
1104 * Returns 0 if successful, or a negative error code.
1106 int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
1109 snd_kcontrol_t *kctl;
1110 snd_kcontrol_new_t *dig_mix;
1112 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
1113 kctl = snd_ctl_new1(dig_mix, codec);
1114 kctl->private_value = nid;
1115 if ((err = snd_ctl_add(codec->bus->card, kctl)) < 0)
1118 codec->spdif_in_enable = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT, 0) & 1;
1124 * snd_hda_build_controls - build mixer controls
1127 * Creates mixer controls for each codec included in the bus.
1129 * Returns 0 if successful, otherwise a negative error code.
1131 int snd_hda_build_controls(struct hda_bus *bus)
1133 struct list_head *p;
1135 /* build controls */
1136 list_for_each(p, &bus->codec_list) {
1137 struct hda_codec *codec = list_entry(p, struct hda_codec, list);
1139 if (! codec->patch_ops.build_controls)
1141 err = codec->patch_ops.build_controls(codec);
1147 list_for_each(p, &bus->codec_list) {
1148 struct hda_codec *codec = list_entry(p, struct hda_codec, list);
1150 if (! codec->patch_ops.init)
1152 err = codec->patch_ops.init(codec);
1163 static unsigned int rate_bits[][3] = {
1164 /* rate in Hz, ALSA rate bitmask, HDA format value */
1165 { 8000, SNDRV_PCM_RATE_8000, 0x0500 }, /* 1/6 x 48 */
1166 { 11025, SNDRV_PCM_RATE_11025, 0x4300 }, /* 1/4 x 44 */
1167 { 16000, SNDRV_PCM_RATE_16000, 0x0200 }, /* 1/3 x 48 */
1168 { 22050, SNDRV_PCM_RATE_22050, 0x4100 }, /* 1/2 x 44 */
1169 { 32000, SNDRV_PCM_RATE_32000, 0x0a00 }, /* 2/3 x 48 */
1170 { 44100, SNDRV_PCM_RATE_44100, 0x4000 }, /* 44 */
1171 { 48000, SNDRV_PCM_RATE_48000, 0x0000 }, /* 48 */
1172 { 88200, SNDRV_PCM_RATE_88200, 0x4800 }, /* 2 x 44 */
1173 { 96000, SNDRV_PCM_RATE_96000, 0x0800 }, /* 2 x 48 */
1174 { 176400, SNDRV_PCM_RATE_176400, 0x5800 },/* 4 x 44 */
1175 { 192000, SNDRV_PCM_RATE_192000, 0x1800 }, /* 4 x 48 */
1180 * snd_hda_calc_stream_format - calculate format bitset
1181 * @rate: the sample rate
1182 * @channels: the number of channels
1183 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
1184 * @maxbps: the max. bps
1186 * Calculate the format bitset from the given rate, channels and th PCM format.
1188 * Return zero if invalid.
1190 unsigned int snd_hda_calc_stream_format(unsigned int rate,
1191 unsigned int channels,
1192 unsigned int format,
1193 unsigned int maxbps)
1196 unsigned int val = 0;
1198 for (i = 0; rate_bits[i][0]; i++)
1199 if (rate_bits[i][0] == rate) {
1200 val = rate_bits[i][2];
1203 if (! rate_bits[i][0]) {
1204 snd_printdd("invalid rate %d\n", rate);
1208 if (channels == 0 || channels > 8) {
1209 snd_printdd("invalid channels %d\n", channels);
1212 val |= channels - 1;
1214 switch (snd_pcm_format_width(format)) {
1215 case 8: val |= 0x00; break;
1216 case 16: val |= 0x10; break;
1222 else if (maxbps >= 24)
1228 snd_printdd("invalid format width %d\n", snd_pcm_format_width(format));
1236 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
1237 * @codec: the HDA codec
1238 * @nid: NID to query
1239 * @ratesp: the pointer to store the detected rate bitflags
1240 * @formatsp: the pointer to store the detected formats
1241 * @bpsp: the pointer to store the detected format widths
1243 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
1244 * or @bsps argument is ignored.
1246 * Returns 0 if successful, otherwise a negative error code.
1248 int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
1249 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
1252 unsigned int val, streams;
1255 if (nid != codec->afg &&
1256 snd_hda_param_read(codec, nid, AC_PAR_AUDIO_WIDGET_CAP) & AC_WCAP_FORMAT_OVRD) {
1257 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
1262 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
1266 for (i = 0; rate_bits[i][0]; i++) {
1268 rates |= rate_bits[i][1];
1273 if (formatsp || bpsp) {
1278 wcaps = snd_hda_param_read(codec, nid, AC_PAR_AUDIO_WIDGET_CAP);
1279 streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
1283 streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
1289 if (streams & AC_SUPFMT_PCM) {
1290 if (val & AC_SUPPCM_BITS_8) {
1291 formats |= SNDRV_PCM_FMTBIT_U8;
1294 if (val & AC_SUPPCM_BITS_16) {
1295 formats |= SNDRV_PCM_FMTBIT_S16_LE;
1298 if (wcaps & AC_WCAP_DIGITAL) {
1299 if (val & AC_SUPPCM_BITS_32)
1300 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
1301 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
1302 formats |= SNDRV_PCM_FMTBIT_S32_LE;
1303 if (val & AC_SUPPCM_BITS_24)
1305 else if (val & AC_SUPPCM_BITS_20)
1307 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|AC_SUPPCM_BITS_32)) {
1308 formats |= SNDRV_PCM_FMTBIT_S32_LE;
1309 if (val & AC_SUPPCM_BITS_32)
1311 else if (val & AC_SUPPCM_BITS_20)
1313 else if (val & AC_SUPPCM_BITS_24)
1317 else if (streams == AC_SUPFMT_FLOAT32) { /* should be exclusive */
1318 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
1320 } else if (streams == AC_SUPFMT_AC3) { /* should be exclusive */
1321 /* temporary hack: we have still no proper support
1322 * for the direct AC3 stream...
1324 formats |= SNDRV_PCM_FMTBIT_U8;
1328 *formatsp = formats;
1337 * snd_hda_is_supported_format - check whether the given node supports the format val
1339 * Returns 1 if supported, 0 if not.
1341 int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
1342 unsigned int format)
1345 unsigned int val = 0, rate, stream;
1347 if (nid != codec->afg &&
1348 snd_hda_param_read(codec, nid, AC_PAR_AUDIO_WIDGET_CAP) & AC_WCAP_FORMAT_OVRD) {
1349 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
1354 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
1359 rate = format & 0xff00;
1360 for (i = 0; rate_bits[i][0]; i++)
1361 if (rate_bits[i][2] == rate) {
1366 if (! rate_bits[i][0])
1369 stream = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
1372 if (! stream && nid != codec->afg)
1373 stream = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
1374 if (! stream || stream == -1)
1377 if (stream & AC_SUPFMT_PCM) {
1378 switch (format & 0xf0) {
1380 if (! (val & AC_SUPPCM_BITS_8))
1384 if (! (val & AC_SUPPCM_BITS_16))
1388 if (! (val & AC_SUPPCM_BITS_20))
1392 if (! (val & AC_SUPPCM_BITS_24))
1396 if (! (val & AC_SUPPCM_BITS_32))
1403 /* FIXME: check for float32 and AC3? */
1412 static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
1413 struct hda_codec *codec,
1414 snd_pcm_substream_t *substream)
1419 static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
1420 struct hda_codec *codec,
1421 unsigned int stream_tag,
1422 unsigned int format,
1423 snd_pcm_substream_t *substream)
1425 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
1429 static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
1430 struct hda_codec *codec,
1431 snd_pcm_substream_t *substream)
1433 snd_hda_codec_setup_stream(codec, hinfo->nid, 0, 0, 0);
1437 static int set_pcm_default_values(struct hda_codec *codec, struct hda_pcm_stream *info)
1440 /* query support PCM information from the given NID */
1441 if (! info->rates || ! info->formats)
1442 snd_hda_query_supported_pcm(codec, info->nid,
1443 info->rates ? NULL : &info->rates,
1444 info->formats ? NULL : &info->formats,
1445 info->maxbps ? NULL : &info->maxbps);
1447 if (info->ops.open == NULL)
1448 info->ops.open = hda_pcm_default_open_close;
1449 if (info->ops.close == NULL)
1450 info->ops.close = hda_pcm_default_open_close;
1451 if (info->ops.prepare == NULL) {
1452 snd_assert(info->nid, return -EINVAL);
1453 info->ops.prepare = hda_pcm_default_prepare;
1455 if (info->ops.cleanup == NULL) {
1456 snd_assert(info->nid, return -EINVAL);
1457 info->ops.cleanup = hda_pcm_default_cleanup;
1463 * snd_hda_build_pcms - build PCM information
1466 * Create PCM information for each codec included in the bus.
1468 * The build_pcms codec patch is requested to set up codec->num_pcms and
1469 * codec->pcm_info properly. The array is referred by the top-level driver
1470 * to create its PCM instances.
1471 * The allocated codec->pcm_info should be released in codec->patch_ops.free
1474 * At least, substreams, channels_min and channels_max must be filled for
1475 * each stream. substreams = 0 indicates that the stream doesn't exist.
1476 * When rates and/or formats are zero, the supported values are queried
1477 * from the given nid. The nid is used also by the default ops.prepare
1478 * and ops.cleanup callbacks.
1480 * The driver needs to call ops.open in its open callback. Similarly,
1481 * ops.close is supposed to be called in the close callback.
1482 * ops.prepare should be called in the prepare or hw_params callback
1483 * with the proper parameters for set up.
1484 * ops.cleanup should be called in hw_free for clean up of streams.
1486 * This function returns 0 if successfull, or a negative error code.
1488 int snd_hda_build_pcms(struct hda_bus *bus)
1490 struct list_head *p;
1492 list_for_each(p, &bus->codec_list) {
1493 struct hda_codec *codec = list_entry(p, struct hda_codec, list);
1494 unsigned int pcm, s;
1496 if (! codec->patch_ops.build_pcms)
1498 err = codec->patch_ops.build_pcms(codec);
1501 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
1502 for (s = 0; s < 2; s++) {
1503 struct hda_pcm_stream *info;
1504 info = &codec->pcm_info[pcm].stream[s];
1505 if (! info->substreams)
1507 err = set_pcm_default_values(codec, info);
1518 * snd_hda_check_board_config - compare the current codec with the config table
1519 * @codec: the HDA codec
1520 * @tbl: configuration table, terminated by null entries
1522 * Compares the modelname or PCI subsystem id of the current codec with the
1523 * given configuration table. If a matching entry is found, returns its
1524 * config value (supposed to be 0 or positive).
1526 * If no entries are matching, the function returns a negative value.
1528 int snd_hda_check_board_config(struct hda_codec *codec, struct hda_board_config *tbl)
1530 struct hda_board_config *c;
1532 if (codec->bus->modelname) {
1533 for (c = tbl; c->modelname || c->pci_subvendor; c++) {
1535 ! strcmp(codec->bus->modelname, c->modelname)) {
1536 snd_printd(KERN_INFO "hda_codec: model '%s' is selected\n", c->modelname);
1542 if (codec->bus->pci) {
1543 u16 subsystem_vendor, subsystem_device;
1544 pci_read_config_word(codec->bus->pci, PCI_SUBSYSTEM_VENDOR_ID, &subsystem_vendor);
1545 pci_read_config_word(codec->bus->pci, PCI_SUBSYSTEM_ID, &subsystem_device);
1546 for (c = tbl; c->modelname || c->pci_subvendor; c++) {
1547 if (c->pci_subvendor == subsystem_vendor &&
1548 c->pci_subdevice == subsystem_device)
1556 * snd_hda_add_new_ctls - create controls from the array
1557 * @codec: the HDA codec
1558 * @knew: the array of snd_kcontrol_new_t
1560 * This helper function creates and add new controls in the given array.
1561 * The array must be terminated with an empty entry as terminator.
1563 * Returns 0 if successful, or a negative error code.
1565 int snd_hda_add_new_ctls(struct hda_codec *codec, snd_kcontrol_new_t *knew)
1569 for (; knew->name; knew++) {
1570 err = snd_ctl_add(codec->bus->card, snd_ctl_new1(knew, codec));
1581 int snd_hda_input_mux_info(const struct hda_input_mux *imux, snd_ctl_elem_info_t *uinfo)
1585 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1587 uinfo->value.enumerated.items = imux->num_items;
1588 index = uinfo->value.enumerated.item;
1589 if (index >= imux->num_items)
1590 index = imux->num_items - 1;
1591 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
1595 int snd_hda_input_mux_put(struct hda_codec *codec, const struct hda_input_mux *imux,
1596 snd_ctl_elem_value_t *ucontrol, hda_nid_t nid,
1597 unsigned int *cur_val)
1601 idx = ucontrol->value.enumerated.item[0];
1602 if (idx >= imux->num_items)
1603 idx = imux->num_items - 1;
1604 if (*cur_val == idx && ! codec->in_resume)
1606 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
1607 imux->items[idx].index);
1614 * Multi-channel / digital-out PCM helper functions
1618 * open the digital out in the exclusive mode
1620 int snd_hda_multi_out_dig_open(struct hda_codec *codec, struct hda_multi_out *mout)
1622 down(&codec->spdif_mutex);
1623 if (mout->dig_out_used) {
1624 up(&codec->spdif_mutex);
1625 return -EBUSY; /* already being used */
1627 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
1628 up(&codec->spdif_mutex);
1633 * release the digital out
1635 int snd_hda_multi_out_dig_close(struct hda_codec *codec, struct hda_multi_out *mout)
1637 down(&codec->spdif_mutex);
1638 mout->dig_out_used = 0;
1639 up(&codec->spdif_mutex);
1644 * set up more restrictions for analog out
1646 int snd_hda_multi_out_analog_open(struct hda_codec *codec, struct hda_multi_out *mout,
1647 snd_pcm_substream_t *substream)
1649 substream->runtime->hw.channels_max = mout->max_channels;
1650 return snd_pcm_hw_constraint_step(substream->runtime, 0,
1651 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
1655 * set up the i/o for analog out
1656 * when the digital out is available, copy the front out to digital out, too.
1658 int snd_hda_multi_out_analog_prepare(struct hda_codec *codec, struct hda_multi_out *mout,
1659 unsigned int stream_tag,
1660 unsigned int format,
1661 snd_pcm_substream_t *substream)
1663 hda_nid_t *nids = mout->dac_nids;
1664 int chs = substream->runtime->channels;
1667 down(&codec->spdif_mutex);
1668 if (mout->dig_out_nid && mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
1670 snd_hda_is_supported_format(codec, mout->dig_out_nid, format) &&
1671 ! (codec->spdif_status & IEC958_AES0_NONAUDIO)) {
1672 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
1673 /* setup digital receiver */
1674 snd_hda_codec_setup_stream(codec, mout->dig_out_nid,
1675 stream_tag, 0, format);
1677 mout->dig_out_used = 0;
1678 snd_hda_codec_setup_stream(codec, mout->dig_out_nid, 0, 0, 0);
1681 up(&codec->spdif_mutex);
1684 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag, 0, format);
1686 /* headphone out will just decode front left/right (stereo) */
1687 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag, 0, format);
1689 for (i = 1; i < mout->num_dacs; i++) {
1690 if (i == HDA_REAR && chs == 2) /* copy front to rear */
1691 snd_hda_codec_setup_stream(codec, nids[i], stream_tag, 0, format);
1692 else if (chs >= (i + 1) * 2) /* independent out */
1693 snd_hda_codec_setup_stream(codec, nids[i], stream_tag, i * 2,
1700 * clean up the setting for analog out
1702 int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec, struct hda_multi_out *mout)
1704 hda_nid_t *nids = mout->dac_nids;
1707 for (i = 0; i < mout->num_dacs; i++)
1708 snd_hda_codec_setup_stream(codec, nids[i], 0, 0, 0);
1710 snd_hda_codec_setup_stream(codec, mout->hp_nid, 0, 0, 0);
1711 down(&codec->spdif_mutex);
1712 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
1713 snd_hda_codec_setup_stream(codec, mout->dig_out_nid, 0, 0, 0);
1714 mout->dig_out_used = 0;
1716 up(&codec->spdif_mutex);
1726 * snd_hda_suspend - suspend the codecs
1728 * @state: suspsend state
1730 * Returns 0 if successful.
1732 int snd_hda_suspend(struct hda_bus *bus, pm_message_t state)
1734 struct list_head *p;
1736 /* FIXME: should handle power widget capabilities */
1737 list_for_each(p, &bus->codec_list) {
1738 struct hda_codec *codec = list_entry(p, struct hda_codec, list);
1739 if (codec->patch_ops.suspend)
1740 codec->patch_ops.suspend(codec, state);
1746 * snd_hda_resume - resume the codecs
1748 * @state: resume state
1750 * Returns 0 if successful.
1752 int snd_hda_resume(struct hda_bus *bus)
1754 struct list_head *p;
1756 list_for_each(p, &bus->codec_list) {
1757 struct hda_codec *codec = list_entry(p, struct hda_codec, list);
1758 if (codec->patch_ops.resume)
1759 codec->patch_ops.resume(codec);
1765 * snd_hda_resume_ctls - resume controls in the new control list
1766 * @codec: the HDA codec
1767 * @knew: the array of snd_kcontrol_new_t
1769 * This function resumes the mixer controls in the snd_kcontrol_new_t array,
1770 * originally for snd_hda_add_new_ctls().
1771 * The array must be terminated with an empty entry as terminator.
1773 int snd_hda_resume_ctls(struct hda_codec *codec, snd_kcontrol_new_t *knew)
1775 snd_ctl_elem_value_t *val;
1777 val = kmalloc(sizeof(*val), GFP_KERNEL);
1780 codec->in_resume = 1;
1781 for (; knew->name; knew++) {
1783 count = knew->count ? knew->count : 1;
1784 for (i = 0; i < count; i++) {
1785 memset(val, 0, sizeof(*val));
1786 val->id.iface = knew->iface;
1787 val->id.device = knew->device;
1788 val->id.subdevice = knew->subdevice;
1789 strcpy(val->id.name, knew->name);
1790 val->id.index = knew->index ? knew->index : i;
1791 /* Assume that get callback reads only from cache,
1792 * not accessing to the real hardware
1794 if (snd_ctl_elem_read(codec->bus->card, val) < 0)
1796 snd_ctl_elem_write(codec->bus->card, NULL, val);
1799 codec->in_resume = 0;
1805 * snd_hda_resume_spdif_out - resume the digital out
1806 * @codec: the HDA codec
1808 int snd_hda_resume_spdif_out(struct hda_codec *codec)
1810 return snd_hda_resume_ctls(codec, dig_mixes);
1814 * snd_hda_resume_spdif_in - resume the digital in
1815 * @codec: the HDA codec
1817 int snd_hda_resume_spdif_in(struct hda_codec *codec)
1819 return snd_hda_resume_ctls(codec, dig_in_ctls);
1824 * symbols exported for controller modules
1826 EXPORT_SYMBOL(snd_hda_codec_read);
1827 EXPORT_SYMBOL(snd_hda_codec_write);
1828 EXPORT_SYMBOL(snd_hda_sequence_write);
1829 EXPORT_SYMBOL(snd_hda_get_sub_nodes);
1830 EXPORT_SYMBOL(snd_hda_queue_unsol_event);
1831 EXPORT_SYMBOL(snd_hda_bus_new);
1832 EXPORT_SYMBOL(snd_hda_codec_new);
1833 EXPORT_SYMBOL(snd_hda_codec_setup_stream);
1834 EXPORT_SYMBOL(snd_hda_calc_stream_format);
1835 EXPORT_SYMBOL(snd_hda_build_pcms);
1836 EXPORT_SYMBOL(snd_hda_build_controls);
1838 EXPORT_SYMBOL(snd_hda_suspend);
1839 EXPORT_SYMBOL(snd_hda_resume);
1846 static int __init alsa_hda_init(void)
1851 static void __exit alsa_hda_exit(void)
1855 module_init(alsa_hda_init)
1856 module_exit(alsa_hda_exit)