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 { 0x11d4, "Analog Devices" },
53 { 0x13f6, "C-Media" },
54 { 0x434d, "C-Media" },
55 { 0x8384, "SigmaTel" },
60 #include "hda_patch.h"
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
71 * Send a single command and read the corresponding response.
73 * Returns the obtained response value, or -1 for an error.
75 unsigned int snd_hda_codec_read(struct hda_codec *codec, hda_nid_t nid, int direct,
76 unsigned int verb, unsigned int parm)
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);
83 res = (unsigned int)-1;
84 up(&codec->bus->cmd_mutex);
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
96 * Send a single command without waiting for response.
98 * Returns 0 if successful, or a negative error code.
100 int snd_hda_codec_write(struct hda_codec *codec, hda_nid_t nid, int direct,
101 unsigned int verb, unsigned int parm)
104 down(&codec->bus->cmd_mutex);
105 err = codec->bus->ops.command(codec, nid, direct, verb, parm);
106 up(&codec->bus->cmd_mutex);
111 * snd_hda_sequence_write - sequence writes
112 * @codec: the HDA codec
113 * @seq: VERB array to send
115 * Send the commands sequentially from the given array.
116 * The array must be terminated with NID=0.
118 void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq)
120 for (; seq->nid; seq++)
121 snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param);
125 * snd_hda_get_sub_nodes - get the range of sub nodes
126 * @codec: the HDA codec
128 * @start_id: the pointer to store the start NID
130 * Parse the NID and store the start NID of its sub-nodes.
131 * Returns the number of sub-nodes.
133 int snd_hda_get_sub_nodes(struct hda_codec *codec, hda_nid_t nid, hda_nid_t *start_id)
137 parm = snd_hda_param_read(codec, nid, AC_PAR_NODE_COUNT);
138 *start_id = (parm >> 16) & 0x7fff;
139 return (int)(parm & 0x7fff);
143 * snd_hda_get_connections - get connection list
144 * @codec: the HDA codec
146 * @conn_list: connection list array
147 * @max_conns: max. number of connections to store
149 * Parses the connection list of the given widget and stores the list
152 * Returns the number of connections, or a negative error code.
154 int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
155 hda_nid_t *conn_list, int max_conns)
158 int i, j, conn_len, num_tupples, conns;
159 unsigned int shift, num_elems, mask;
161 snd_assert(conn_list && max_conns > 0, return -EINVAL);
163 parm = snd_hda_param_read(codec, nid, AC_PAR_CONNLIST_LEN);
164 if (parm & AC_CLIST_LONG) {
173 conn_len = parm & AC_CLIST_LENGTH;
174 num_tupples = num_elems / 2;
175 mask = (1 << (shift-1)) - 1;
178 return 0; /* no connection */
181 /* single connection */
182 parm = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONNECT_LIST, 0);
183 conn_list[0] = parm & mask;
187 /* multi connection */
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++) {
193 hda_nid_t val1, val2, n;
194 range_val = parm & (1 << (shift-1)); /* ranges */
200 /* ranges between val1 and val2 */
202 snd_printk(KERN_WARNING "hda_codec: invalid dep_range_val %x:%x\n", val1, val2);
205 for (n = val1; n <= val2; n++) {
206 if (conns >= max_conns)
208 conn_list[conns++] = n;
213 if (conns >= max_conns)
215 conn_list[conns++] = val1;
218 if (conns >= max_conns)
220 conn_list[conns++] = val2;
229 * snd_hda_queue_unsol_event - add an unsolicited event to queue
231 * @res: unsolicited event (lower 32bit of RIRB entry)
232 * @res_ex: codec addr and flags (upper 32bit or RIRB entry)
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.
238 * Returns 0 if successful, or a negative error code.
240 int snd_hda_queue_unsol_event(struct hda_bus *bus, u32 res, u32 res_ex)
242 struct hda_bus_unsolicited *unsol;
245 if ((unsol = bus->unsol) == NULL)
248 wp = (unsol->wp + 1) % HDA_UNSOL_QUEUE_SIZE;
252 unsol->queue[wp] = res;
253 unsol->queue[wp + 1] = res_ex;
255 queue_work(unsol->workq, &unsol->work);
261 * process queueud unsolicited events
263 static void process_unsol_events(void *data)
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;
270 while (unsol->rp != unsol->wp) {
271 rp = (unsol->rp + 1) % HDA_UNSOL_QUEUE_SIZE;
274 res = unsol->queue[rp];
275 caddr = unsol->queue[rp + 1];
276 if (! (caddr & (1 << 4))) /* no unsolicited event? */
278 codec = bus->caddr_tbl[caddr & 0x0f];
279 if (codec && codec->patch_ops.unsol_event)
280 codec->patch_ops.unsol_event(codec, res);
285 * initialize unsolicited queue
287 static int init_unsol_queue(struct hda_bus *bus)
289 struct hda_bus_unsolicited *unsol;
291 unsol = kzalloc(sizeof(*unsol), GFP_KERNEL);
293 snd_printk(KERN_ERR "hda_codec: can't allocate unsolicited queue\n");
296 unsol->workq = create_workqueue("hda_codec");
297 if (! unsol->workq) {
298 snd_printk(KERN_ERR "hda_codec: can't create workqueue\n");
302 INIT_WORK(&unsol->work, process_unsol_events, bus);
310 static void snd_hda_codec_free(struct hda_codec *codec);
312 static int snd_hda_bus_free(struct hda_bus *bus)
314 struct list_head *p, *n;
319 destroy_workqueue(bus->unsol->workq);
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);
326 if (bus->ops.private_free)
327 bus->ops.private_free(bus);
332 static int snd_hda_bus_dev_free(snd_device_t *device)
334 struct hda_bus *bus = device->device_data;
335 return snd_hda_bus_free(bus);
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
344 * Returns 0 if successful, or a negative error code.
346 int snd_hda_bus_new(snd_card_t *card, const struct hda_bus_template *temp,
347 struct hda_bus **busp)
351 static snd_device_ops_t dev_ops = {
352 .dev_free = snd_hda_bus_dev_free,
355 snd_assert(temp, return -EINVAL);
356 snd_assert(temp->ops.command && temp->ops.get_response, return -EINVAL);
361 bus = kzalloc(sizeof(*bus), GFP_KERNEL);
363 snd_printk(KERN_ERR "can't allocate struct hda_bus\n");
368 bus->private_data = temp->private_data;
369 bus->pci = temp->pci;
370 bus->modelname = temp->modelname;
371 bus->ops = temp->ops;
373 init_MUTEX(&bus->cmd_mutex);
374 INIT_LIST_HEAD(&bus->codec_list);
376 init_unsol_queue(bus);
378 if ((err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops)) < 0) {
379 snd_hda_bus_free(bus);
389 * find a matching codec preset
391 static const struct hda_codec_preset *find_codec_preset(struct hda_codec *codec)
393 const struct hda_codec_preset **tbl, *preset;
395 for (tbl = hda_preset_tables; *tbl; tbl++) {
396 for (preset = *tbl; preset->id; preset++) {
397 u32 mask = preset->mask;
400 if (preset->id == (codec->vendor_id & mask))
408 * snd_hda_get_codec_name - store the codec name
410 void snd_hda_get_codec_name(struct hda_codec *codec,
411 char *name, int namelen)
413 const struct hda_vendor_id *c;
414 const char *vendor = NULL;
415 u16 vendor_id = codec->vendor_id >> 16;
418 for (c = hda_vendor_ids; c->id; c++) {
419 if (c->id == vendor_id) {
425 sprintf(tmp, "Generic %04x", vendor_id);
428 if (codec->preset && codec->preset->name)
429 snprintf(name, namelen, "%s %s", vendor, codec->preset->name);
431 snprintf(name, namelen, "%s ID %x", vendor, codec->vendor_id & 0xffff);
435 * look for an AFG and MFG nodes
437 static void setup_fg_nodes(struct hda_codec *codec)
442 total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid);
443 for (i = 0; i < total_nodes; i++, nid++) {
444 switch((snd_hda_param_read(codec, nid, AC_PAR_FUNCTION_TYPE) & 0xff)) {
445 case AC_GRP_AUDIO_FUNCTION:
448 case AC_GRP_MODEM_FUNCTION:
460 static void snd_hda_codec_free(struct hda_codec *codec)
464 list_del(&codec->list);
465 codec->bus->caddr_tbl[codec->addr] = NULL;
466 if (codec->patch_ops.free)
467 codec->patch_ops.free(codec);
471 static void init_amp_hash(struct hda_codec *codec);
474 * snd_hda_codec_new - create a HDA codec
475 * @bus: the bus to assign
476 * @codec_addr: the codec address
477 * @codecp: the pointer to store the generated codec
479 * Returns 0 if successful, or a negative error code.
481 int snd_hda_codec_new(struct hda_bus *bus, unsigned int codec_addr,
482 struct hda_codec **codecp)
484 struct hda_codec *codec;
488 snd_assert(bus, return -EINVAL);
489 snd_assert(codec_addr <= HDA_MAX_CODEC_ADDRESS, return -EINVAL);
491 if (bus->caddr_tbl[codec_addr]) {
492 snd_printk(KERN_ERR "hda_codec: address 0x%x is already occupied\n", codec_addr);
496 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
498 snd_printk(KERN_ERR "can't allocate struct hda_codec\n");
503 codec->addr = codec_addr;
504 init_MUTEX(&codec->spdif_mutex);
505 init_amp_hash(codec);
507 list_add_tail(&codec->list, &bus->codec_list);
508 bus->caddr_tbl[codec_addr] = codec;
510 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT, AC_PAR_VENDOR_ID);
511 codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT, AC_PAR_SUBSYSTEM_ID);
512 codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT, AC_PAR_REV_ID);
514 setup_fg_nodes(codec);
515 if (! codec->afg && ! codec->mfg) {
516 snd_printdd("hda_codec: no AFG or MFG node found\n");
517 snd_hda_codec_free(codec);
521 if (! codec->subsystem_id) {
522 hda_nid_t nid = codec->afg ? codec->afg : codec->mfg;
523 codec->subsystem_id = snd_hda_codec_read(codec, nid, 0,
524 AC_VERB_GET_SUBSYSTEM_ID,
528 codec->preset = find_codec_preset(codec);
529 if (! *bus->card->mixername)
530 snd_hda_get_codec_name(codec, bus->card->mixername,
531 sizeof(bus->card->mixername));
533 if (codec->preset && codec->preset->patch)
534 err = codec->preset->patch(codec);
536 err = snd_hda_parse_generic_codec(codec);
538 snd_hda_codec_free(codec);
542 snd_hda_codec_proc_new(codec);
544 sprintf(component, "HDA:%08x", codec->vendor_id);
545 snd_component_add(codec->bus->card, component);
553 * snd_hda_codec_setup_stream - set up the codec for streaming
554 * @codec: the CODEC to set up
555 * @nid: the NID to set up
556 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
557 * @channel_id: channel id to pass, zero based.
558 * @format: stream format.
560 void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid, u32 stream_tag,
561 int channel_id, int format)
566 snd_printdd("hda_codec_setup_stream: NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
567 nid, stream_tag, channel_id, format);
568 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID,
569 (stream_tag << 4) | channel_id);
571 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, format);
576 * amp access functions
579 /* FIXME: more better hash key? */
580 #define HDA_HASH_KEY(nid,dir,idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
581 #define INFO_AMP_CAPS (1<<0)
582 #define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
584 /* initialize the hash table */
585 static void init_amp_hash(struct hda_codec *codec)
587 memset(codec->amp_hash, 0xff, sizeof(codec->amp_hash));
588 codec->num_amp_entries = 0;
591 /* query the hash. allocate an entry if not found. */
592 static struct hda_amp_info *get_alloc_amp_hash(struct hda_codec *codec, u32 key)
594 u16 idx = key % (u16)ARRAY_SIZE(codec->amp_hash);
595 u16 cur = codec->amp_hash[idx];
596 struct hda_amp_info *info;
598 while (cur != 0xffff) {
599 info = &codec->amp_info[cur];
600 if (info->key == key)
605 /* add a new hash entry */
606 if (codec->num_amp_entries >= ARRAY_SIZE(codec->amp_info)) {
607 snd_printk(KERN_ERR "hda_codec: Tooooo many amps!\n");
610 cur = codec->num_amp_entries++;
611 info = &codec->amp_info[cur];
613 info->status = 0; /* not initialized yet */
614 info->next = codec->amp_hash[idx];
615 codec->amp_hash[idx] = cur;
621 * query AMP capabilities for the given widget and direction
623 static u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
625 struct hda_amp_info *info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, 0));
629 if (! (info->status & INFO_AMP_CAPS)) {
630 if (!(snd_hda_param_read(codec, nid, AC_PAR_AUDIO_WIDGET_CAP) & AC_WCAP_AMP_OVRD))
632 info->amp_caps = snd_hda_param_read(codec, nid, direction == HDA_OUTPUT ?
633 AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP);
634 info->status |= INFO_AMP_CAPS;
636 return info->amp_caps;
640 * read the current volume to info
641 * if the cache exists, read the cache value.
643 static unsigned int get_vol_mute(struct hda_codec *codec, struct hda_amp_info *info,
644 hda_nid_t nid, int ch, int direction, int index)
648 if (info->status & INFO_AMP_VOL(ch))
649 return info->vol[ch];
651 parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
652 parm |= direction == HDA_OUTPUT ? AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
654 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_AMP_GAIN_MUTE, parm);
655 info->vol[ch] = val & 0xff;
656 info->status |= INFO_AMP_VOL(ch);
657 return info->vol[ch];
661 * write the current volume in info to the h/w and update the cache
663 static void put_vol_mute(struct hda_codec *codec, struct hda_amp_info *info,
664 hda_nid_t nid, int ch, int direction, int index, int val)
668 parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
669 parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
670 parm |= index << AC_AMP_SET_INDEX_SHIFT;
672 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
677 * read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
679 static int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch, int direction, int index)
681 struct hda_amp_info *info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
684 return get_vol_mute(codec, info, nid, ch, direction, index);
688 * update the AMP value, mask = bit mask to set, val = the value
690 static int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch, int direction, int idx, int mask, int val)
692 struct hda_amp_info *info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, idx));
697 val |= get_vol_mute(codec, info, nid, ch, direction, idx) & ~mask;
698 if (info->vol[ch] == val && ! codec->in_resume)
700 put_vol_mute(codec, info, nid, ch, direction, idx, val);
706 * AMP control callbacks
708 /* retrieve parameters from private_value */
709 #define get_amp_nid(kc) ((kc)->private_value & 0xffff)
710 #define get_amp_channels(kc) (((kc)->private_value >> 16) & 0x3)
711 #define get_amp_direction(kc) (((kc)->private_value >> 18) & 0x1)
712 #define get_amp_index(kc) (((kc)->private_value >> 19) & 0xf)
715 int snd_hda_mixer_amp_volume_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
717 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
718 u16 nid = get_amp_nid(kcontrol);
719 u8 chs = get_amp_channels(kcontrol);
720 int dir = get_amp_direction(kcontrol);
723 caps = query_amp_caps(codec, nid, dir);
724 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT; /* num steps */
726 printk(KERN_WARNING "hda_codec: num_steps = 0 for NID=0x%x\n", nid);
729 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
730 uinfo->count = chs == 3 ? 2 : 1;
731 uinfo->value.integer.min = 0;
732 uinfo->value.integer.max = caps;
736 int snd_hda_mixer_amp_volume_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
738 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
739 hda_nid_t nid = get_amp_nid(kcontrol);
740 int chs = get_amp_channels(kcontrol);
741 int dir = get_amp_direction(kcontrol);
742 int idx = get_amp_index(kcontrol);
743 long *valp = ucontrol->value.integer.value;
746 *valp++ = snd_hda_codec_amp_read(codec, nid, 0, dir, idx) & 0x7f;
748 *valp = snd_hda_codec_amp_read(codec, nid, 1, dir, idx) & 0x7f;
752 int snd_hda_mixer_amp_volume_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
754 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
755 hda_nid_t nid = get_amp_nid(kcontrol);
756 int chs = get_amp_channels(kcontrol);
757 int dir = get_amp_direction(kcontrol);
758 int idx = get_amp_index(kcontrol);
759 long *valp = ucontrol->value.integer.value;
763 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
768 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
774 int snd_hda_mixer_amp_switch_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
776 int chs = get_amp_channels(kcontrol);
778 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
779 uinfo->count = chs == 3 ? 2 : 1;
780 uinfo->value.integer.min = 0;
781 uinfo->value.integer.max = 1;
785 int snd_hda_mixer_amp_switch_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
787 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
788 hda_nid_t nid = get_amp_nid(kcontrol);
789 int chs = get_amp_channels(kcontrol);
790 int dir = get_amp_direction(kcontrol);
791 int idx = get_amp_index(kcontrol);
792 long *valp = ucontrol->value.integer.value;
795 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) & 0x80) ? 0 : 1;
797 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) & 0x80) ? 0 : 1;
801 int snd_hda_mixer_amp_switch_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
803 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
804 hda_nid_t nid = get_amp_nid(kcontrol);
805 int chs = get_amp_channels(kcontrol);
806 int dir = get_amp_direction(kcontrol);
807 int idx = get_amp_index(kcontrol);
808 long *valp = ucontrol->value.integer.value;
812 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
813 0x80, *valp ? 0 : 0x80);
817 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
818 0x80, *valp ? 0 : 0x80);
824 * bound volume controls
826 * bind multiple volumes (# indices, from 0)
829 #define AMP_VAL_IDX_SHIFT 19
830 #define AMP_VAL_IDX_MASK (0x0f<<19)
832 int snd_hda_mixer_bind_switch_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
834 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
838 down(&codec->spdif_mutex); /* reuse spdif_mutex */
839 pval = kcontrol->private_value;
840 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
841 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
842 kcontrol->private_value = pval;
843 up(&codec->spdif_mutex);
847 int snd_hda_mixer_bind_switch_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
849 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
851 int i, indices, err = 0, change = 0;
853 down(&codec->spdif_mutex); /* reuse spdif_mutex */
854 pval = kcontrol->private_value;
855 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
856 for (i = 0; i < indices; i++) {
857 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) | (i << AMP_VAL_IDX_SHIFT);
858 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
863 kcontrol->private_value = pval;
864 up(&codec->spdif_mutex);
865 return err < 0 ? err : change;
872 static int snd_hda_spdif_mask_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
874 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
879 static int snd_hda_spdif_cmask_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
881 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
882 IEC958_AES0_NONAUDIO |
883 IEC958_AES0_CON_EMPHASIS_5015 |
884 IEC958_AES0_CON_NOT_COPYRIGHT;
885 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
886 IEC958_AES1_CON_ORIGINAL;
890 static int snd_hda_spdif_pmask_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
892 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
893 IEC958_AES0_NONAUDIO |
894 IEC958_AES0_PRO_EMPHASIS_5015;
898 static int snd_hda_spdif_default_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
900 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
902 ucontrol->value.iec958.status[0] = codec->spdif_status & 0xff;
903 ucontrol->value.iec958.status[1] = (codec->spdif_status >> 8) & 0xff;
904 ucontrol->value.iec958.status[2] = (codec->spdif_status >> 16) & 0xff;
905 ucontrol->value.iec958.status[3] = (codec->spdif_status >> 24) & 0xff;
910 /* convert from SPDIF status bits to HDA SPDIF bits
911 * bit 0 (DigEn) is always set zero (to be filled later)
913 static unsigned short convert_from_spdif_status(unsigned int sbits)
915 unsigned short val = 0;
917 if (sbits & IEC958_AES0_PROFESSIONAL)
919 if (sbits & IEC958_AES0_NONAUDIO)
921 if (sbits & IEC958_AES0_PROFESSIONAL) {
922 if ((sbits & IEC958_AES0_PRO_EMPHASIS) == IEC958_AES0_PRO_EMPHASIS_5015)
925 if ((sbits & IEC958_AES0_CON_EMPHASIS) == IEC958_AES0_CON_EMPHASIS_5015)
927 if (! (sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
929 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
931 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
936 /* convert to SPDIF status bits from HDA SPDIF bits
938 static unsigned int convert_to_spdif_status(unsigned short val)
940 unsigned int sbits = 0;
943 sbits |= IEC958_AES0_NONAUDIO;
945 sbits |= IEC958_AES0_PROFESSIONAL;
946 if (sbits & IEC958_AES0_PROFESSIONAL) {
947 if (sbits & (1 << 3))
948 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
951 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
952 if (! (val & (1 << 4)))
953 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
955 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
956 sbits |= val & (0x7f << 8);
961 static int snd_hda_spdif_default_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
963 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
964 hda_nid_t nid = kcontrol->private_value;
968 down(&codec->spdif_mutex);
969 codec->spdif_status = ucontrol->value.iec958.status[0] |
970 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
971 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
972 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
973 val = convert_from_spdif_status(codec->spdif_status);
974 val |= codec->spdif_ctls & 1;
975 change = codec->spdif_ctls != val;
976 codec->spdif_ctls = val;
978 if (change || codec->in_resume) {
979 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_DIGI_CONVERT_1, val & 0xff);
980 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_DIGI_CONVERT_2, val >> 8);
983 up(&codec->spdif_mutex);
987 static int snd_hda_spdif_out_switch_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
989 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
991 uinfo->value.integer.min = 0;
992 uinfo->value.integer.max = 1;
996 static int snd_hda_spdif_out_switch_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
998 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1000 ucontrol->value.integer.value[0] = codec->spdif_ctls & 1;
1004 static int snd_hda_spdif_out_switch_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
1006 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1007 hda_nid_t nid = kcontrol->private_value;
1011 down(&codec->spdif_mutex);
1012 val = codec->spdif_ctls & ~1;
1013 if (ucontrol->value.integer.value[0])
1015 change = codec->spdif_ctls != val;
1016 if (change || codec->in_resume) {
1017 codec->spdif_ctls = val;
1018 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_DIGI_CONVERT_1, val & 0xff);
1019 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE,
1020 AC_AMP_SET_RIGHT | AC_AMP_SET_LEFT |
1021 AC_AMP_SET_OUTPUT | ((val & 1) ? 0 : 0x80));
1023 up(&codec->spdif_mutex);
1027 static snd_kcontrol_new_t dig_mixes[] = {
1029 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1030 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1031 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,CON_MASK),
1032 .info = snd_hda_spdif_mask_info,
1033 .get = snd_hda_spdif_cmask_get,
1036 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1037 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1038 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,PRO_MASK),
1039 .info = snd_hda_spdif_mask_info,
1040 .get = snd_hda_spdif_pmask_get,
1043 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1044 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
1045 .info = snd_hda_spdif_mask_info,
1046 .get = snd_hda_spdif_default_get,
1047 .put = snd_hda_spdif_default_put,
1050 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1051 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,SWITCH),
1052 .info = snd_hda_spdif_out_switch_info,
1053 .get = snd_hda_spdif_out_switch_get,
1054 .put = snd_hda_spdif_out_switch_put,
1060 * snd_hda_create_spdif_out_ctls - create Output SPDIF-related controls
1061 * @codec: the HDA codec
1062 * @nid: audio out widget NID
1064 * Creates controls related with the SPDIF output.
1065 * Called from each patch supporting the SPDIF out.
1067 * Returns 0 if successful, or a negative error code.
1069 int snd_hda_create_spdif_out_ctls(struct hda_codec *codec, hda_nid_t nid)
1072 snd_kcontrol_t *kctl;
1073 snd_kcontrol_new_t *dig_mix;
1075 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
1076 kctl = snd_ctl_new1(dig_mix, codec);
1077 kctl->private_value = nid;
1078 if ((err = snd_ctl_add(codec->bus->card, kctl)) < 0)
1081 codec->spdif_ctls = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT, 0);
1082 codec->spdif_status = convert_to_spdif_status(codec->spdif_ctls);
1090 #define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
1092 static int snd_hda_spdif_in_switch_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
1094 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1096 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
1100 static int snd_hda_spdif_in_switch_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
1102 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1103 hda_nid_t nid = kcontrol->private_value;
1104 unsigned int val = !!ucontrol->value.integer.value[0];
1107 down(&codec->spdif_mutex);
1108 change = codec->spdif_in_enable != val;
1109 if (change || codec->in_resume) {
1110 codec->spdif_in_enable = val;
1111 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_DIGI_CONVERT_1, val);
1113 up(&codec->spdif_mutex);
1117 static int snd_hda_spdif_in_status_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
1119 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1120 hda_nid_t nid = kcontrol->private_value;
1124 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT, 0);
1125 sbits = convert_to_spdif_status(val);
1126 ucontrol->value.iec958.status[0] = sbits;
1127 ucontrol->value.iec958.status[1] = sbits >> 8;
1128 ucontrol->value.iec958.status[2] = sbits >> 16;
1129 ucontrol->value.iec958.status[3] = sbits >> 24;
1133 static snd_kcontrol_new_t dig_in_ctls[] = {
1135 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1136 .name = SNDRV_CTL_NAME_IEC958("",CAPTURE,SWITCH),
1137 .info = snd_hda_spdif_in_switch_info,
1138 .get = snd_hda_spdif_in_switch_get,
1139 .put = snd_hda_spdif_in_switch_put,
1142 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1143 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1144 .name = SNDRV_CTL_NAME_IEC958("",CAPTURE,DEFAULT),
1145 .info = snd_hda_spdif_mask_info,
1146 .get = snd_hda_spdif_in_status_get,
1152 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
1153 * @codec: the HDA codec
1154 * @nid: audio in widget NID
1156 * Creates controls related with the SPDIF input.
1157 * Called from each patch supporting the SPDIF in.
1159 * Returns 0 if successful, or a negative error code.
1161 int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
1164 snd_kcontrol_t *kctl;
1165 snd_kcontrol_new_t *dig_mix;
1167 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
1168 kctl = snd_ctl_new1(dig_mix, codec);
1169 kctl->private_value = nid;
1170 if ((err = snd_ctl_add(codec->bus->card, kctl)) < 0)
1173 codec->spdif_in_enable = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT, 0) & 1;
1179 * snd_hda_build_controls - build mixer controls
1182 * Creates mixer controls for each codec included in the bus.
1184 * Returns 0 if successful, otherwise a negative error code.
1186 int snd_hda_build_controls(struct hda_bus *bus)
1188 struct list_head *p;
1190 /* build controls */
1191 list_for_each(p, &bus->codec_list) {
1192 struct hda_codec *codec = list_entry(p, struct hda_codec, list);
1194 if (! codec->patch_ops.build_controls)
1196 err = codec->patch_ops.build_controls(codec);
1202 list_for_each(p, &bus->codec_list) {
1203 struct hda_codec *codec = list_entry(p, struct hda_codec, list);
1205 if (! codec->patch_ops.init)
1207 err = codec->patch_ops.init(codec);
1218 struct hda_rate_tbl {
1220 unsigned int alsa_bits;
1221 unsigned int hda_fmt;
1224 static struct hda_rate_tbl rate_bits[] = {
1225 /* rate in Hz, ALSA rate bitmask, HDA format value */
1227 /* autodetected value used in snd_hda_query_supported_pcm */
1228 { 8000, SNDRV_PCM_RATE_8000, 0x0500 }, /* 1/6 x 48 */
1229 { 11025, SNDRV_PCM_RATE_11025, 0x4300 }, /* 1/4 x 44 */
1230 { 16000, SNDRV_PCM_RATE_16000, 0x0200 }, /* 1/3 x 48 */
1231 { 22050, SNDRV_PCM_RATE_22050, 0x4100 }, /* 1/2 x 44 */
1232 { 32000, SNDRV_PCM_RATE_32000, 0x0a00 }, /* 2/3 x 48 */
1233 { 44100, SNDRV_PCM_RATE_44100, 0x4000 }, /* 44 */
1234 { 48000, SNDRV_PCM_RATE_48000, 0x0000 }, /* 48 */
1235 { 88200, SNDRV_PCM_RATE_88200, 0x4800 }, /* 2 x 44 */
1236 { 96000, SNDRV_PCM_RATE_96000, 0x0800 }, /* 2 x 48 */
1237 { 176400, SNDRV_PCM_RATE_176400, 0x5800 },/* 4 x 44 */
1238 { 192000, SNDRV_PCM_RATE_192000, 0x1800 }, /* 4 x 48 */
1240 /* not autodetected value */
1241 { 9600, SNDRV_PCM_RATE_KNOT, 0x0400 }, /* 1/5 x 48 */
1243 { 0 } /* terminator */
1247 * snd_hda_calc_stream_format - calculate format bitset
1248 * @rate: the sample rate
1249 * @channels: the number of channels
1250 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
1251 * @maxbps: the max. bps
1253 * Calculate the format bitset from the given rate, channels and th PCM format.
1255 * Return zero if invalid.
1257 unsigned int snd_hda_calc_stream_format(unsigned int rate,
1258 unsigned int channels,
1259 unsigned int format,
1260 unsigned int maxbps)
1263 unsigned int val = 0;
1265 for (i = 0; rate_bits[i].hz; i++)
1266 if (rate_bits[i].hz == rate) {
1267 val = rate_bits[i].hda_fmt;
1270 if (! rate_bits[i].hz) {
1271 snd_printdd("invalid rate %d\n", rate);
1275 if (channels == 0 || channels > 8) {
1276 snd_printdd("invalid channels %d\n", channels);
1279 val |= channels - 1;
1281 switch (snd_pcm_format_width(format)) {
1282 case 8: val |= 0x00; break;
1283 case 16: val |= 0x10; break;
1289 else if (maxbps >= 24)
1295 snd_printdd("invalid format width %d\n", snd_pcm_format_width(format));
1303 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
1304 * @codec: the HDA codec
1305 * @nid: NID to query
1306 * @ratesp: the pointer to store the detected rate bitflags
1307 * @formatsp: the pointer to store the detected formats
1308 * @bpsp: the pointer to store the detected format widths
1310 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
1311 * or @bsps argument is ignored.
1313 * Returns 0 if successful, otherwise a negative error code.
1315 int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
1316 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
1319 unsigned int val, streams;
1322 if (nid != codec->afg &&
1323 snd_hda_param_read(codec, nid, AC_PAR_AUDIO_WIDGET_CAP) & AC_WCAP_FORMAT_OVRD) {
1324 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
1329 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
1333 for (i = 0; rate_bits[i].hz; i++) {
1335 rates |= rate_bits[i].alsa_bits;
1340 if (formatsp || bpsp) {
1345 wcaps = snd_hda_param_read(codec, nid, AC_PAR_AUDIO_WIDGET_CAP);
1346 streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
1350 streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
1356 if (streams & AC_SUPFMT_PCM) {
1357 if (val & AC_SUPPCM_BITS_8) {
1358 formats |= SNDRV_PCM_FMTBIT_U8;
1361 if (val & AC_SUPPCM_BITS_16) {
1362 formats |= SNDRV_PCM_FMTBIT_S16_LE;
1365 if (wcaps & AC_WCAP_DIGITAL) {
1366 if (val & AC_SUPPCM_BITS_32)
1367 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
1368 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
1369 formats |= SNDRV_PCM_FMTBIT_S32_LE;
1370 if (val & AC_SUPPCM_BITS_24)
1372 else if (val & AC_SUPPCM_BITS_20)
1374 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|AC_SUPPCM_BITS_32)) {
1375 formats |= SNDRV_PCM_FMTBIT_S32_LE;
1376 if (val & AC_SUPPCM_BITS_32)
1378 else if (val & AC_SUPPCM_BITS_20)
1380 else if (val & AC_SUPPCM_BITS_24)
1384 else if (streams == AC_SUPFMT_FLOAT32) { /* should be exclusive */
1385 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
1387 } else if (streams == AC_SUPFMT_AC3) { /* should be exclusive */
1388 /* temporary hack: we have still no proper support
1389 * for the direct AC3 stream...
1391 formats |= SNDRV_PCM_FMTBIT_U8;
1395 *formatsp = formats;
1404 * snd_hda_is_supported_format - check whether the given node supports the format val
1406 * Returns 1 if supported, 0 if not.
1408 int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
1409 unsigned int format)
1412 unsigned int val = 0, rate, stream;
1414 if (nid != codec->afg &&
1415 snd_hda_param_read(codec, nid, AC_PAR_AUDIO_WIDGET_CAP) & AC_WCAP_FORMAT_OVRD) {
1416 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
1421 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
1426 rate = format & 0xff00;
1427 for (i = 0; rate_bits[i].hz; i++)
1428 if (rate_bits[i].hda_fmt == rate) {
1433 if (! rate_bits[i].hz)
1436 stream = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
1439 if (! stream && nid != codec->afg)
1440 stream = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
1441 if (! stream || stream == -1)
1444 if (stream & AC_SUPFMT_PCM) {
1445 switch (format & 0xf0) {
1447 if (! (val & AC_SUPPCM_BITS_8))
1451 if (! (val & AC_SUPPCM_BITS_16))
1455 if (! (val & AC_SUPPCM_BITS_20))
1459 if (! (val & AC_SUPPCM_BITS_24))
1463 if (! (val & AC_SUPPCM_BITS_32))
1470 /* FIXME: check for float32 and AC3? */
1479 static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
1480 struct hda_codec *codec,
1481 snd_pcm_substream_t *substream)
1486 static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
1487 struct hda_codec *codec,
1488 unsigned int stream_tag,
1489 unsigned int format,
1490 snd_pcm_substream_t *substream)
1492 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
1496 static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
1497 struct hda_codec *codec,
1498 snd_pcm_substream_t *substream)
1500 snd_hda_codec_setup_stream(codec, hinfo->nid, 0, 0, 0);
1504 static int set_pcm_default_values(struct hda_codec *codec, struct hda_pcm_stream *info)
1507 /* query support PCM information from the given NID */
1508 if (! info->rates || ! info->formats)
1509 snd_hda_query_supported_pcm(codec, info->nid,
1510 info->rates ? NULL : &info->rates,
1511 info->formats ? NULL : &info->formats,
1512 info->maxbps ? NULL : &info->maxbps);
1514 if (info->ops.open == NULL)
1515 info->ops.open = hda_pcm_default_open_close;
1516 if (info->ops.close == NULL)
1517 info->ops.close = hda_pcm_default_open_close;
1518 if (info->ops.prepare == NULL) {
1519 snd_assert(info->nid, return -EINVAL);
1520 info->ops.prepare = hda_pcm_default_prepare;
1522 if (info->ops.cleanup == NULL) {
1523 snd_assert(info->nid, return -EINVAL);
1524 info->ops.cleanup = hda_pcm_default_cleanup;
1530 * snd_hda_build_pcms - build PCM information
1533 * Create PCM information for each codec included in the bus.
1535 * The build_pcms codec patch is requested to set up codec->num_pcms and
1536 * codec->pcm_info properly. The array is referred by the top-level driver
1537 * to create its PCM instances.
1538 * The allocated codec->pcm_info should be released in codec->patch_ops.free
1541 * At least, substreams, channels_min and channels_max must be filled for
1542 * each stream. substreams = 0 indicates that the stream doesn't exist.
1543 * When rates and/or formats are zero, the supported values are queried
1544 * from the given nid. The nid is used also by the default ops.prepare
1545 * and ops.cleanup callbacks.
1547 * The driver needs to call ops.open in its open callback. Similarly,
1548 * ops.close is supposed to be called in the close callback.
1549 * ops.prepare should be called in the prepare or hw_params callback
1550 * with the proper parameters for set up.
1551 * ops.cleanup should be called in hw_free for clean up of streams.
1553 * This function returns 0 if successfull, or a negative error code.
1555 int snd_hda_build_pcms(struct hda_bus *bus)
1557 struct list_head *p;
1559 list_for_each(p, &bus->codec_list) {
1560 struct hda_codec *codec = list_entry(p, struct hda_codec, list);
1561 unsigned int pcm, s;
1563 if (! codec->patch_ops.build_pcms)
1565 err = codec->patch_ops.build_pcms(codec);
1568 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
1569 for (s = 0; s < 2; s++) {
1570 struct hda_pcm_stream *info;
1571 info = &codec->pcm_info[pcm].stream[s];
1572 if (! info->substreams)
1574 err = set_pcm_default_values(codec, info);
1585 * snd_hda_check_board_config - compare the current codec with the config table
1586 * @codec: the HDA codec
1587 * @tbl: configuration table, terminated by null entries
1589 * Compares the modelname or PCI subsystem id of the current codec with the
1590 * given configuration table. If a matching entry is found, returns its
1591 * config value (supposed to be 0 or positive).
1593 * If no entries are matching, the function returns a negative value.
1595 int snd_hda_check_board_config(struct hda_codec *codec, const struct hda_board_config *tbl)
1597 const struct hda_board_config *c;
1599 if (codec->bus->modelname) {
1600 for (c = tbl; c->modelname || c->pci_subvendor; c++) {
1602 ! strcmp(codec->bus->modelname, c->modelname)) {
1603 snd_printd(KERN_INFO "hda_codec: model '%s' is selected\n", c->modelname);
1609 if (codec->bus->pci) {
1610 u16 subsystem_vendor, subsystem_device;
1611 pci_read_config_word(codec->bus->pci, PCI_SUBSYSTEM_VENDOR_ID, &subsystem_vendor);
1612 pci_read_config_word(codec->bus->pci, PCI_SUBSYSTEM_ID, &subsystem_device);
1613 for (c = tbl; c->modelname || c->pci_subvendor; c++) {
1614 if (c->pci_subvendor == subsystem_vendor &&
1615 (! c->pci_subdevice /* all match */||
1616 (c->pci_subdevice == subsystem_device))) {
1617 snd_printdd(KERN_INFO "hda_codec: PCI %x:%x, codec config %d is selected\n",
1618 subsystem_vendor, subsystem_device, c->config);
1627 * snd_hda_add_new_ctls - create controls from the array
1628 * @codec: the HDA codec
1629 * @knew: the array of snd_kcontrol_new_t
1631 * This helper function creates and add new controls in the given array.
1632 * The array must be terminated with an empty entry as terminator.
1634 * Returns 0 if successful, or a negative error code.
1636 int snd_hda_add_new_ctls(struct hda_codec *codec, snd_kcontrol_new_t *knew)
1640 for (; knew->name; knew++) {
1641 err = snd_ctl_add(codec->bus->card, snd_ctl_new1(knew, codec));
1652 int snd_hda_input_mux_info(const struct hda_input_mux *imux, snd_ctl_elem_info_t *uinfo)
1656 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1658 uinfo->value.enumerated.items = imux->num_items;
1659 index = uinfo->value.enumerated.item;
1660 if (index >= imux->num_items)
1661 index = imux->num_items - 1;
1662 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
1666 int snd_hda_input_mux_put(struct hda_codec *codec, const struct hda_input_mux *imux,
1667 snd_ctl_elem_value_t *ucontrol, hda_nid_t nid,
1668 unsigned int *cur_val)
1672 idx = ucontrol->value.enumerated.item[0];
1673 if (idx >= imux->num_items)
1674 idx = imux->num_items - 1;
1675 if (*cur_val == idx && ! codec->in_resume)
1677 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
1678 imux->items[idx].index);
1685 * Multi-channel / digital-out PCM helper functions
1689 * open the digital out in the exclusive mode
1691 int snd_hda_multi_out_dig_open(struct hda_codec *codec, struct hda_multi_out *mout)
1693 down(&codec->spdif_mutex);
1694 if (mout->dig_out_used) {
1695 up(&codec->spdif_mutex);
1696 return -EBUSY; /* already being used */
1698 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
1699 up(&codec->spdif_mutex);
1704 * release the digital out
1706 int snd_hda_multi_out_dig_close(struct hda_codec *codec, struct hda_multi_out *mout)
1708 down(&codec->spdif_mutex);
1709 mout->dig_out_used = 0;
1710 up(&codec->spdif_mutex);
1715 * set up more restrictions for analog out
1717 int snd_hda_multi_out_analog_open(struct hda_codec *codec, struct hda_multi_out *mout,
1718 snd_pcm_substream_t *substream)
1720 substream->runtime->hw.channels_max = mout->max_channels;
1721 return snd_pcm_hw_constraint_step(substream->runtime, 0,
1722 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
1726 * set up the i/o for analog out
1727 * when the digital out is available, copy the front out to digital out, too.
1729 int snd_hda_multi_out_analog_prepare(struct hda_codec *codec, struct hda_multi_out *mout,
1730 unsigned int stream_tag,
1731 unsigned int format,
1732 snd_pcm_substream_t *substream)
1734 hda_nid_t *nids = mout->dac_nids;
1735 int chs = substream->runtime->channels;
1738 down(&codec->spdif_mutex);
1739 if (mout->dig_out_nid && mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
1741 snd_hda_is_supported_format(codec, mout->dig_out_nid, format) &&
1742 ! (codec->spdif_status & IEC958_AES0_NONAUDIO)) {
1743 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
1744 /* setup digital receiver */
1745 snd_hda_codec_setup_stream(codec, mout->dig_out_nid,
1746 stream_tag, 0, format);
1748 mout->dig_out_used = 0;
1749 snd_hda_codec_setup_stream(codec, mout->dig_out_nid, 0, 0, 0);
1752 up(&codec->spdif_mutex);
1755 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag, 0, format);
1757 /* headphone out will just decode front left/right (stereo) */
1758 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag, 0, format);
1760 for (i = 1; i < mout->num_dacs; i++) {
1761 if (chs >= (i + 1) * 2) /* independent out */
1762 snd_hda_codec_setup_stream(codec, nids[i], stream_tag, i * 2,
1764 else /* copy front */
1765 snd_hda_codec_setup_stream(codec, nids[i], stream_tag, 0,
1772 * clean up the setting for analog out
1774 int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec, struct hda_multi_out *mout)
1776 hda_nid_t *nids = mout->dac_nids;
1779 for (i = 0; i < mout->num_dacs; i++)
1780 snd_hda_codec_setup_stream(codec, nids[i], 0, 0, 0);
1782 snd_hda_codec_setup_stream(codec, mout->hp_nid, 0, 0, 0);
1783 down(&codec->spdif_mutex);
1784 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
1785 snd_hda_codec_setup_stream(codec, mout->dig_out_nid, 0, 0, 0);
1786 mout->dig_out_used = 0;
1788 up(&codec->spdif_mutex);
1793 * Helper for automatic ping configuration
1795 /* parse all pin widgets and store the useful pin nids to cfg */
1796 int snd_hda_parse_pin_def_config(struct hda_codec *codec, struct auto_pin_cfg *cfg)
1798 hda_nid_t nid, nid_start;
1800 short seq, sequences[4], assoc_line_out;
1802 memset(cfg, 0, sizeof(*cfg));
1804 memset(sequences, 0, sizeof(sequences));
1807 nodes = snd_hda_get_sub_nodes(codec, codec->afg, &nid_start);
1808 for (nid = nid_start; nid < nodes + nid_start; nid++) {
1809 unsigned int wid_caps = snd_hda_param_read(codec, nid,
1810 AC_PAR_AUDIO_WIDGET_CAP);
1811 unsigned int wid_type = (wid_caps & AC_WCAP_TYPE) >> AC_WCAP_TYPE_SHIFT;
1812 unsigned int def_conf;
1815 /* read all default configuration for pin complex */
1816 if (wid_type != AC_WID_PIN)
1818 def_conf = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONFIG_DEFAULT, 0);
1819 if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE)
1821 loc = get_defcfg_location(def_conf);
1822 switch (get_defcfg_device(def_conf)) {
1823 case AC_JACK_LINE_OUT:
1824 case AC_JACK_SPEAKER:
1825 seq = get_defcfg_sequence(def_conf);
1826 assoc = get_defcfg_association(def_conf);
1829 if (! assoc_line_out)
1830 assoc_line_out = assoc;
1831 else if (assoc_line_out != assoc)
1833 if (cfg->line_outs >= ARRAY_SIZE(cfg->line_out_pins))
1835 cfg->line_out_pins[cfg->line_outs] = nid;
1836 sequences[cfg->line_outs] = seq;
1839 case AC_JACK_HP_OUT:
1842 case AC_JACK_MIC_IN:
1843 if (loc == AC_JACK_LOC_FRONT)
1844 cfg->input_pins[AUTO_PIN_FRONT_MIC] = nid;
1846 cfg->input_pins[AUTO_PIN_MIC] = nid;
1848 case AC_JACK_LINE_IN:
1849 if (loc == AC_JACK_LOC_FRONT)
1850 cfg->input_pins[AUTO_PIN_FRONT_LINE] = nid;
1852 cfg->input_pins[AUTO_PIN_LINE] = nid;
1855 cfg->input_pins[AUTO_PIN_CD] = nid;
1858 cfg->input_pins[AUTO_PIN_AUX] = nid;
1860 case AC_JACK_SPDIF_OUT:
1861 cfg->dig_out_pin = nid;
1863 case AC_JACK_SPDIF_IN:
1864 cfg->dig_in_pin = nid;
1869 /* sort by sequence */
1870 for (i = 0; i < cfg->line_outs; i++)
1871 for (j = i + 1; j < cfg->line_outs; j++)
1872 if (sequences[i] > sequences[j]) {
1874 sequences[i] = sequences[j];
1876 nid = cfg->line_out_pins[i];
1877 cfg->line_out_pins[i] = cfg->line_out_pins[j];
1878 cfg->line_out_pins[j] = nid;
1881 /* Reorder the surround channels
1882 * ALSA sequence is front/surr/clfe/side
1884 * 4-ch: front/surr => OK as it is
1885 * 6-ch: front/clfe/surr
1886 * 8-ch: front/clfe/side/surr
1888 switch (cfg->line_outs) {
1890 nid = cfg->line_out_pins[1];
1891 cfg->line_out_pins[1] = cfg->line_out_pins[2];
1892 cfg->line_out_pins[2] = nid;
1895 nid = cfg->line_out_pins[1];
1896 cfg->line_out_pins[1] = cfg->line_out_pins[3];
1897 cfg->line_out_pins[3] = cfg->line_out_pins[2];
1898 cfg->line_out_pins[2] = nid;
1911 * snd_hda_suspend - suspend the codecs
1913 * @state: suspsend state
1915 * Returns 0 if successful.
1917 int snd_hda_suspend(struct hda_bus *bus, pm_message_t state)
1919 struct list_head *p;
1921 /* FIXME: should handle power widget capabilities */
1922 list_for_each(p, &bus->codec_list) {
1923 struct hda_codec *codec = list_entry(p, struct hda_codec, list);
1924 if (codec->patch_ops.suspend)
1925 codec->patch_ops.suspend(codec, state);
1931 * snd_hda_resume - resume the codecs
1933 * @state: resume state
1935 * Returns 0 if successful.
1937 int snd_hda_resume(struct hda_bus *bus)
1939 struct list_head *p;
1941 list_for_each(p, &bus->codec_list) {
1942 struct hda_codec *codec = list_entry(p, struct hda_codec, list);
1943 if (codec->patch_ops.resume)
1944 codec->patch_ops.resume(codec);
1950 * snd_hda_resume_ctls - resume controls in the new control list
1951 * @codec: the HDA codec
1952 * @knew: the array of snd_kcontrol_new_t
1954 * This function resumes the mixer controls in the snd_kcontrol_new_t array,
1955 * originally for snd_hda_add_new_ctls().
1956 * The array must be terminated with an empty entry as terminator.
1958 int snd_hda_resume_ctls(struct hda_codec *codec, snd_kcontrol_new_t *knew)
1960 snd_ctl_elem_value_t *val;
1962 val = kmalloc(sizeof(*val), GFP_KERNEL);
1965 codec->in_resume = 1;
1966 for (; knew->name; knew++) {
1968 count = knew->count ? knew->count : 1;
1969 for (i = 0; i < count; i++) {
1970 memset(val, 0, sizeof(*val));
1971 val->id.iface = knew->iface;
1972 val->id.device = knew->device;
1973 val->id.subdevice = knew->subdevice;
1974 strcpy(val->id.name, knew->name);
1975 val->id.index = knew->index ? knew->index : i;
1976 /* Assume that get callback reads only from cache,
1977 * not accessing to the real hardware
1979 if (snd_ctl_elem_read(codec->bus->card, val) < 0)
1981 snd_ctl_elem_write(codec->bus->card, NULL, val);
1984 codec->in_resume = 0;
1990 * snd_hda_resume_spdif_out - resume the digital out
1991 * @codec: the HDA codec
1993 int snd_hda_resume_spdif_out(struct hda_codec *codec)
1995 return snd_hda_resume_ctls(codec, dig_mixes);
1999 * snd_hda_resume_spdif_in - resume the digital in
2000 * @codec: the HDA codec
2002 int snd_hda_resume_spdif_in(struct hda_codec *codec)
2004 return snd_hda_resume_ctls(codec, dig_in_ctls);
2009 * symbols exported for controller modules
2011 EXPORT_SYMBOL(snd_hda_codec_read);
2012 EXPORT_SYMBOL(snd_hda_codec_write);
2013 EXPORT_SYMBOL(snd_hda_sequence_write);
2014 EXPORT_SYMBOL(snd_hda_get_sub_nodes);
2015 EXPORT_SYMBOL(snd_hda_queue_unsol_event);
2016 EXPORT_SYMBOL(snd_hda_bus_new);
2017 EXPORT_SYMBOL(snd_hda_codec_new);
2018 EXPORT_SYMBOL(snd_hda_codec_setup_stream);
2019 EXPORT_SYMBOL(snd_hda_calc_stream_format);
2020 EXPORT_SYMBOL(snd_hda_build_pcms);
2021 EXPORT_SYMBOL(snd_hda_build_controls);
2023 EXPORT_SYMBOL(snd_hda_suspend);
2024 EXPORT_SYMBOL(snd_hda_resume);
2031 static int __init alsa_hda_init(void)
2036 static void __exit alsa_hda_exit(void)
2040 module_init(alsa_hda_init)
2041 module_exit(alsa_hda_exit)