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 <linux/mutex.h>
29 #include <sound/core.h>
30 #include "hda_codec.h"
31 #include <sound/asoundef.h>
32 #include <sound/initval.h>
33 #include "hda_local.h"
36 MODULE_AUTHOR("Takashi Iwai <tiwai@suse.de>");
37 MODULE_DESCRIPTION("Universal interface for High Definition Audio Codec");
38 MODULE_LICENSE("GPL");
42 * vendor / preset table
45 struct hda_vendor_id {
50 /* codec vendor labels */
51 static struct hda_vendor_id hda_vendor_ids[] = {
52 { 0x10ec, "Realtek" },
53 { 0x11d4, "Analog Devices" },
54 { 0x13f6, "C-Media" },
55 { 0x434d, "C-Media" },
56 { 0x8384, "SigmaTel" },
61 #include "hda_patch.h"
65 * snd_hda_codec_read - send a command and get the response
66 * @codec: the HDA codec
67 * @nid: NID to send the command
68 * @direct: direct flag
69 * @verb: the verb to send
70 * @parm: the parameter for the verb
72 * Send a single command and read the corresponding response.
74 * Returns the obtained response value, or -1 for an error.
76 unsigned int snd_hda_codec_read(struct hda_codec *codec, hda_nid_t nid, int direct,
77 unsigned int verb, unsigned int parm)
80 mutex_lock(&codec->bus->cmd_mutex);
81 if (! codec->bus->ops.command(codec, nid, direct, verb, parm))
82 res = codec->bus->ops.get_response(codec);
84 res = (unsigned int)-1;
85 mutex_unlock(&codec->bus->cmd_mutex);
90 * snd_hda_codec_write - send a single command without waiting for response
91 * @codec: the HDA codec
92 * @nid: NID to send the command
93 * @direct: direct flag
94 * @verb: the verb to send
95 * @parm: the parameter for the verb
97 * Send a single command without waiting for response.
99 * Returns 0 if successful, or a negative error code.
101 int snd_hda_codec_write(struct hda_codec *codec, hda_nid_t nid, int direct,
102 unsigned int verb, unsigned int parm)
105 mutex_lock(&codec->bus->cmd_mutex);
106 err = codec->bus->ops.command(codec, nid, direct, verb, parm);
107 mutex_unlock(&codec->bus->cmd_mutex);
112 * snd_hda_sequence_write - sequence writes
113 * @codec: the HDA codec
114 * @seq: VERB array to send
116 * Send the commands sequentially from the given array.
117 * The array must be terminated with NID=0.
119 void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq)
121 for (; seq->nid; seq++)
122 snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param);
126 * snd_hda_get_sub_nodes - get the range of sub nodes
127 * @codec: the HDA codec
129 * @start_id: the pointer to store the start NID
131 * Parse the NID and store the start NID of its sub-nodes.
132 * Returns the number of sub-nodes.
134 int snd_hda_get_sub_nodes(struct hda_codec *codec, hda_nid_t nid, hda_nid_t *start_id)
138 parm = snd_hda_param_read(codec, nid, AC_PAR_NODE_COUNT);
139 *start_id = (parm >> 16) & 0x7fff;
140 return (int)(parm & 0x7fff);
144 * snd_hda_get_connections - get connection list
145 * @codec: the HDA codec
147 * @conn_list: connection list array
148 * @max_conns: max. number of connections to store
150 * Parses the connection list of the given widget and stores the list
153 * Returns the number of connections, or a negative error code.
155 int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
156 hda_nid_t *conn_list, int max_conns)
159 int i, conn_len, conns;
160 unsigned int shift, num_elems, mask;
163 snd_assert(conn_list && max_conns > 0, return -EINVAL);
165 parm = snd_hda_param_read(codec, nid, AC_PAR_CONNLIST_LEN);
166 if (parm & AC_CLIST_LONG) {
175 conn_len = parm & AC_CLIST_LENGTH;
176 mask = (1 << (shift-1)) - 1;
179 return 0; /* no connection */
182 /* single connection */
183 parm = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONNECT_LIST, 0);
184 conn_list[0] = parm & mask;
188 /* multi connection */
191 for (i = 0; i < conn_len; i++) {
195 if (i % num_elems == 0)
196 parm = snd_hda_codec_read(codec, nid, 0,
197 AC_VERB_GET_CONNECT_LIST, i);
198 range_val = !! (parm & (1 << (shift-1))); /* ranges */
202 /* ranges between the previous and this one */
203 if (! prev_nid || prev_nid >= val) {
204 snd_printk(KERN_WARNING "hda_codec: invalid dep_range_val %x:%x\n", prev_nid, val);
207 for (n = prev_nid + 1; n <= val; n++) {
208 if (conns >= max_conns) {
209 snd_printk(KERN_ERR "Too many connections\n");
212 conn_list[conns++] = n;
215 if (conns >= max_conns) {
216 snd_printk(KERN_ERR "Too many connections\n");
219 conn_list[conns++] = val;
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 if (bus->unsol) /* already initialized */
293 unsol = kzalloc(sizeof(*unsol), GFP_KERNEL);
295 snd_printk(KERN_ERR "hda_codec: can't allocate unsolicited queue\n");
298 unsol->workq = create_singlethread_workqueue("hda_codec");
299 if (! unsol->workq) {
300 snd_printk(KERN_ERR "hda_codec: can't create workqueue\n");
304 INIT_WORK(&unsol->work, process_unsol_events, bus);
312 static void snd_hda_codec_free(struct hda_codec *codec);
314 static int snd_hda_bus_free(struct hda_bus *bus)
316 struct list_head *p, *n;
321 destroy_workqueue(bus->unsol->workq);
324 list_for_each_safe(p, n, &bus->codec_list) {
325 struct hda_codec *codec = list_entry(p, struct hda_codec, list);
326 snd_hda_codec_free(codec);
328 if (bus->ops.private_free)
329 bus->ops.private_free(bus);
334 static int snd_hda_bus_dev_free(struct snd_device *device)
336 struct hda_bus *bus = device->device_data;
337 return snd_hda_bus_free(bus);
341 * snd_hda_bus_new - create a HDA bus
342 * @card: the card entry
343 * @temp: the template for hda_bus information
344 * @busp: the pointer to store the created bus instance
346 * Returns 0 if successful, or a negative error code.
348 int snd_hda_bus_new(struct snd_card *card, const struct hda_bus_template *temp,
349 struct hda_bus **busp)
353 static struct snd_device_ops dev_ops = {
354 .dev_free = snd_hda_bus_dev_free,
357 snd_assert(temp, return -EINVAL);
358 snd_assert(temp->ops.command && temp->ops.get_response, return -EINVAL);
363 bus = kzalloc(sizeof(*bus), GFP_KERNEL);
365 snd_printk(KERN_ERR "can't allocate struct hda_bus\n");
370 bus->private_data = temp->private_data;
371 bus->pci = temp->pci;
372 bus->modelname = temp->modelname;
373 bus->ops = temp->ops;
375 mutex_init(&bus->cmd_mutex);
376 INIT_LIST_HEAD(&bus->codec_list);
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:
458 * read widget caps for each widget and store in cache
460 static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
465 codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node,
467 codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL);
470 nid = codec->start_nid;
471 for (i = 0; i < codec->num_nodes; i++, nid++)
472 codec->wcaps[i] = snd_hda_param_read(codec, nid,
473 AC_PAR_AUDIO_WIDGET_CAP);
481 static void snd_hda_codec_free(struct hda_codec *codec)
485 list_del(&codec->list);
486 codec->bus->caddr_tbl[codec->addr] = NULL;
487 if (codec->patch_ops.free)
488 codec->patch_ops.free(codec);
489 kfree(codec->amp_info);
494 static void init_amp_hash(struct hda_codec *codec);
497 * snd_hda_codec_new - create a HDA codec
498 * @bus: the bus to assign
499 * @codec_addr: the codec address
500 * @codecp: the pointer to store the generated codec
502 * Returns 0 if successful, or a negative error code.
504 int snd_hda_codec_new(struct hda_bus *bus, unsigned int codec_addr,
505 struct hda_codec **codecp)
507 struct hda_codec *codec;
511 snd_assert(bus, return -EINVAL);
512 snd_assert(codec_addr <= HDA_MAX_CODEC_ADDRESS, return -EINVAL);
514 if (bus->caddr_tbl[codec_addr]) {
515 snd_printk(KERN_ERR "hda_codec: address 0x%x is already occupied\n", codec_addr);
519 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
521 snd_printk(KERN_ERR "can't allocate struct hda_codec\n");
526 codec->addr = codec_addr;
527 mutex_init(&codec->spdif_mutex);
528 init_amp_hash(codec);
530 list_add_tail(&codec->list, &bus->codec_list);
531 bus->caddr_tbl[codec_addr] = codec;
533 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT, AC_PAR_VENDOR_ID);
534 if (codec->vendor_id == -1)
535 /* read again, hopefully the access method was corrected
536 * in the last read...
538 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
540 codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT, AC_PAR_SUBSYSTEM_ID);
541 codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT, AC_PAR_REV_ID);
543 setup_fg_nodes(codec);
544 if (! codec->afg && ! codec->mfg) {
545 snd_printdd("hda_codec: no AFG or MFG node found\n");
546 snd_hda_codec_free(codec);
550 if (read_widget_caps(codec, codec->afg ? codec->afg : codec->mfg) < 0) {
551 snd_printk(KERN_ERR "hda_codec: cannot malloc\n");
552 snd_hda_codec_free(codec);
556 if (! codec->subsystem_id) {
557 hda_nid_t nid = codec->afg ? codec->afg : codec->mfg;
558 codec->subsystem_id = snd_hda_codec_read(codec, nid, 0,
559 AC_VERB_GET_SUBSYSTEM_ID,
563 codec->preset = find_codec_preset(codec);
564 if (! *bus->card->mixername)
565 snd_hda_get_codec_name(codec, bus->card->mixername,
566 sizeof(bus->card->mixername));
568 if (codec->preset && codec->preset->patch)
569 err = codec->preset->patch(codec);
571 err = snd_hda_parse_generic_codec(codec);
573 snd_hda_codec_free(codec);
577 if (codec->patch_ops.unsol_event)
578 init_unsol_queue(bus);
580 snd_hda_codec_proc_new(codec);
582 sprintf(component, "HDA:%08x", codec->vendor_id);
583 snd_component_add(codec->bus->card, component);
591 * snd_hda_codec_setup_stream - set up the codec for streaming
592 * @codec: the CODEC to set up
593 * @nid: the NID to set up
594 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
595 * @channel_id: channel id to pass, zero based.
596 * @format: stream format.
598 void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid, u32 stream_tag,
599 int channel_id, int format)
604 snd_printdd("hda_codec_setup_stream: NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
605 nid, stream_tag, channel_id, format);
606 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID,
607 (stream_tag << 4) | channel_id);
609 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, format);
614 * amp access functions
617 /* FIXME: more better hash key? */
618 #define HDA_HASH_KEY(nid,dir,idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
619 #define INFO_AMP_CAPS (1<<0)
620 #define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
622 /* initialize the hash table */
623 static void init_amp_hash(struct hda_codec *codec)
625 memset(codec->amp_hash, 0xff, sizeof(codec->amp_hash));
626 codec->num_amp_entries = 0;
627 codec->amp_info_size = 0;
628 codec->amp_info = NULL;
631 /* query the hash. allocate an entry if not found. */
632 static struct hda_amp_info *get_alloc_amp_hash(struct hda_codec *codec, u32 key)
634 u16 idx = key % (u16)ARRAY_SIZE(codec->amp_hash);
635 u16 cur = codec->amp_hash[idx];
636 struct hda_amp_info *info;
638 while (cur != 0xffff) {
639 info = &codec->amp_info[cur];
640 if (info->key == key)
645 /* add a new hash entry */
646 if (codec->num_amp_entries >= codec->amp_info_size) {
647 /* reallocate the array */
648 int new_size = codec->amp_info_size + 64;
649 struct hda_amp_info *new_info = kcalloc(new_size, sizeof(struct hda_amp_info),
652 snd_printk(KERN_ERR "hda_codec: can't malloc amp_info\n");
655 if (codec->amp_info) {
656 memcpy(new_info, codec->amp_info,
657 codec->amp_info_size * sizeof(struct hda_amp_info));
658 kfree(codec->amp_info);
660 codec->amp_info_size = new_size;
661 codec->amp_info = new_info;
663 cur = codec->num_amp_entries++;
664 info = &codec->amp_info[cur];
666 info->status = 0; /* not initialized yet */
667 info->next = codec->amp_hash[idx];
668 codec->amp_hash[idx] = cur;
674 * query AMP capabilities for the given widget and direction
676 static u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
678 struct hda_amp_info *info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, 0));
682 if (! (info->status & INFO_AMP_CAPS)) {
683 if (! (get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
685 info->amp_caps = snd_hda_param_read(codec, nid, direction == HDA_OUTPUT ?
686 AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP);
687 info->status |= INFO_AMP_CAPS;
689 return info->amp_caps;
693 * read the current volume to info
694 * if the cache exists, read the cache value.
696 static unsigned int get_vol_mute(struct hda_codec *codec, struct hda_amp_info *info,
697 hda_nid_t nid, int ch, int direction, int index)
701 if (info->status & INFO_AMP_VOL(ch))
702 return info->vol[ch];
704 parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
705 parm |= direction == HDA_OUTPUT ? AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
707 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_AMP_GAIN_MUTE, parm);
708 info->vol[ch] = val & 0xff;
709 info->status |= INFO_AMP_VOL(ch);
710 return info->vol[ch];
714 * write the current volume in info to the h/w and update the cache
716 static void put_vol_mute(struct hda_codec *codec, struct hda_amp_info *info,
717 hda_nid_t nid, int ch, int direction, int index, int val)
721 parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
722 parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
723 parm |= index << AC_AMP_SET_INDEX_SHIFT;
725 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
730 * read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
732 int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
733 int direction, int index)
735 struct hda_amp_info *info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
738 return get_vol_mute(codec, info, nid, ch, direction, index);
742 * update the AMP value, mask = bit mask to set, val = the value
744 int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
745 int direction, int idx, int mask, int val)
747 struct hda_amp_info *info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, idx));
752 val |= get_vol_mute(codec, info, nid, ch, direction, idx) & ~mask;
753 if (info->vol[ch] == val && ! codec->in_resume)
755 put_vol_mute(codec, info, nid, ch, direction, idx, val);
761 * AMP control callbacks
763 /* retrieve parameters from private_value */
764 #define get_amp_nid(kc) ((kc)->private_value & 0xffff)
765 #define get_amp_channels(kc) (((kc)->private_value >> 16) & 0x3)
766 #define get_amp_direction(kc) (((kc)->private_value >> 18) & 0x1)
767 #define get_amp_index(kc) (((kc)->private_value >> 19) & 0xf)
770 int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
772 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
773 u16 nid = get_amp_nid(kcontrol);
774 u8 chs = get_amp_channels(kcontrol);
775 int dir = get_amp_direction(kcontrol);
778 caps = query_amp_caps(codec, nid, dir);
779 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT; /* num steps */
781 printk(KERN_WARNING "hda_codec: num_steps = 0 for NID=0x%x\n", nid);
784 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
785 uinfo->count = chs == 3 ? 2 : 1;
786 uinfo->value.integer.min = 0;
787 uinfo->value.integer.max = caps;
791 int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
793 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
794 hda_nid_t nid = get_amp_nid(kcontrol);
795 int chs = get_amp_channels(kcontrol);
796 int dir = get_amp_direction(kcontrol);
797 int idx = get_amp_index(kcontrol);
798 long *valp = ucontrol->value.integer.value;
801 *valp++ = snd_hda_codec_amp_read(codec, nid, 0, dir, idx) & 0x7f;
803 *valp = snd_hda_codec_amp_read(codec, nid, 1, dir, idx) & 0x7f;
807 int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
809 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
810 hda_nid_t nid = get_amp_nid(kcontrol);
811 int chs = get_amp_channels(kcontrol);
812 int dir = get_amp_direction(kcontrol);
813 int idx = get_amp_index(kcontrol);
814 long *valp = ucontrol->value.integer.value;
818 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
823 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
829 int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
831 int chs = get_amp_channels(kcontrol);
833 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
834 uinfo->count = chs == 3 ? 2 : 1;
835 uinfo->value.integer.min = 0;
836 uinfo->value.integer.max = 1;
840 int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
842 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
843 hda_nid_t nid = get_amp_nid(kcontrol);
844 int chs = get_amp_channels(kcontrol);
845 int dir = get_amp_direction(kcontrol);
846 int idx = get_amp_index(kcontrol);
847 long *valp = ucontrol->value.integer.value;
850 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) & 0x80) ? 0 : 1;
852 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) & 0x80) ? 0 : 1;
856 int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
858 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
859 hda_nid_t nid = get_amp_nid(kcontrol);
860 int chs = get_amp_channels(kcontrol);
861 int dir = get_amp_direction(kcontrol);
862 int idx = get_amp_index(kcontrol);
863 long *valp = ucontrol->value.integer.value;
867 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
868 0x80, *valp ? 0 : 0x80);
872 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
873 0x80, *valp ? 0 : 0x80);
879 * bound volume controls
881 * bind multiple volumes (# indices, from 0)
884 #define AMP_VAL_IDX_SHIFT 19
885 #define AMP_VAL_IDX_MASK (0x0f<<19)
887 int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
889 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
893 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
894 pval = kcontrol->private_value;
895 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
896 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
897 kcontrol->private_value = pval;
898 mutex_unlock(&codec->spdif_mutex);
902 int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
904 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
906 int i, indices, err = 0, change = 0;
908 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
909 pval = kcontrol->private_value;
910 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
911 for (i = 0; i < indices; i++) {
912 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) | (i << AMP_VAL_IDX_SHIFT);
913 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
918 kcontrol->private_value = pval;
919 mutex_unlock(&codec->spdif_mutex);
920 return err < 0 ? err : change;
927 static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
929 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
934 static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
936 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
937 IEC958_AES0_NONAUDIO |
938 IEC958_AES0_CON_EMPHASIS_5015 |
939 IEC958_AES0_CON_NOT_COPYRIGHT;
940 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
941 IEC958_AES1_CON_ORIGINAL;
945 static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
947 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
948 IEC958_AES0_NONAUDIO |
949 IEC958_AES0_PRO_EMPHASIS_5015;
953 static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
955 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
957 ucontrol->value.iec958.status[0] = codec->spdif_status & 0xff;
958 ucontrol->value.iec958.status[1] = (codec->spdif_status >> 8) & 0xff;
959 ucontrol->value.iec958.status[2] = (codec->spdif_status >> 16) & 0xff;
960 ucontrol->value.iec958.status[3] = (codec->spdif_status >> 24) & 0xff;
965 /* convert from SPDIF status bits to HDA SPDIF bits
966 * bit 0 (DigEn) is always set zero (to be filled later)
968 static unsigned short convert_from_spdif_status(unsigned int sbits)
970 unsigned short val = 0;
972 if (sbits & IEC958_AES0_PROFESSIONAL)
974 if (sbits & IEC958_AES0_NONAUDIO)
976 if (sbits & IEC958_AES0_PROFESSIONAL) {
977 if ((sbits & IEC958_AES0_PRO_EMPHASIS) == IEC958_AES0_PRO_EMPHASIS_5015)
980 if ((sbits & IEC958_AES0_CON_EMPHASIS) == IEC958_AES0_CON_EMPHASIS_5015)
982 if (! (sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
984 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
986 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
991 /* convert to SPDIF status bits from HDA SPDIF bits
993 static unsigned int convert_to_spdif_status(unsigned short val)
995 unsigned int sbits = 0;
998 sbits |= IEC958_AES0_NONAUDIO;
1000 sbits |= IEC958_AES0_PROFESSIONAL;
1001 if (sbits & IEC958_AES0_PROFESSIONAL) {
1002 if (sbits & (1 << 3))
1003 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
1006 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
1007 if (! (val & (1 << 4)))
1008 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
1010 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
1011 sbits |= val & (0x7f << 8);
1016 static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1018 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1019 hda_nid_t nid = kcontrol->private_value;
1023 mutex_lock(&codec->spdif_mutex);
1024 codec->spdif_status = ucontrol->value.iec958.status[0] |
1025 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
1026 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
1027 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
1028 val = convert_from_spdif_status(codec->spdif_status);
1029 val |= codec->spdif_ctls & 1;
1030 change = codec->spdif_ctls != val;
1031 codec->spdif_ctls = val;
1033 if (change || codec->in_resume) {
1034 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_DIGI_CONVERT_1, val & 0xff);
1035 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_DIGI_CONVERT_2, val >> 8);
1038 mutex_unlock(&codec->spdif_mutex);
1042 static int snd_hda_spdif_out_switch_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1044 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1046 uinfo->value.integer.min = 0;
1047 uinfo->value.integer.max = 1;
1051 static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1053 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1055 ucontrol->value.integer.value[0] = codec->spdif_ctls & 1;
1059 static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1061 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1062 hda_nid_t nid = kcontrol->private_value;
1066 mutex_lock(&codec->spdif_mutex);
1067 val = codec->spdif_ctls & ~1;
1068 if (ucontrol->value.integer.value[0])
1070 change = codec->spdif_ctls != val;
1071 if (change || codec->in_resume) {
1072 codec->spdif_ctls = val;
1073 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_DIGI_CONVERT_1, val & 0xff);
1074 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE,
1075 AC_AMP_SET_RIGHT | AC_AMP_SET_LEFT |
1076 AC_AMP_SET_OUTPUT | ((val & 1) ? 0 : 0x80));
1078 mutex_unlock(&codec->spdif_mutex);
1082 static struct snd_kcontrol_new dig_mixes[] = {
1084 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1085 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1086 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,CON_MASK),
1087 .info = snd_hda_spdif_mask_info,
1088 .get = snd_hda_spdif_cmask_get,
1091 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1092 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1093 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,PRO_MASK),
1094 .info = snd_hda_spdif_mask_info,
1095 .get = snd_hda_spdif_pmask_get,
1098 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1099 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
1100 .info = snd_hda_spdif_mask_info,
1101 .get = snd_hda_spdif_default_get,
1102 .put = snd_hda_spdif_default_put,
1105 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1106 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,SWITCH),
1107 .info = snd_hda_spdif_out_switch_info,
1108 .get = snd_hda_spdif_out_switch_get,
1109 .put = snd_hda_spdif_out_switch_put,
1115 * snd_hda_create_spdif_out_ctls - create Output SPDIF-related controls
1116 * @codec: the HDA codec
1117 * @nid: audio out widget NID
1119 * Creates controls related with the SPDIF output.
1120 * Called from each patch supporting the SPDIF out.
1122 * Returns 0 if successful, or a negative error code.
1124 int snd_hda_create_spdif_out_ctls(struct hda_codec *codec, hda_nid_t nid)
1127 struct snd_kcontrol *kctl;
1128 struct snd_kcontrol_new *dig_mix;
1130 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
1131 kctl = snd_ctl_new1(dig_mix, codec);
1132 kctl->private_value = nid;
1133 if ((err = snd_ctl_add(codec->bus->card, kctl)) < 0)
1136 codec->spdif_ctls = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT, 0);
1137 codec->spdif_status = convert_to_spdif_status(codec->spdif_ctls);
1145 #define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
1147 static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1149 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1151 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
1155 static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1157 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1158 hda_nid_t nid = kcontrol->private_value;
1159 unsigned int val = !!ucontrol->value.integer.value[0];
1162 mutex_lock(&codec->spdif_mutex);
1163 change = codec->spdif_in_enable != val;
1164 if (change || codec->in_resume) {
1165 codec->spdif_in_enable = val;
1166 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_DIGI_CONVERT_1, val);
1168 mutex_unlock(&codec->spdif_mutex);
1172 static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1174 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1175 hda_nid_t nid = kcontrol->private_value;
1179 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT, 0);
1180 sbits = convert_to_spdif_status(val);
1181 ucontrol->value.iec958.status[0] = sbits;
1182 ucontrol->value.iec958.status[1] = sbits >> 8;
1183 ucontrol->value.iec958.status[2] = sbits >> 16;
1184 ucontrol->value.iec958.status[3] = sbits >> 24;
1188 static struct snd_kcontrol_new dig_in_ctls[] = {
1190 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1191 .name = SNDRV_CTL_NAME_IEC958("",CAPTURE,SWITCH),
1192 .info = snd_hda_spdif_in_switch_info,
1193 .get = snd_hda_spdif_in_switch_get,
1194 .put = snd_hda_spdif_in_switch_put,
1197 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1198 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1199 .name = SNDRV_CTL_NAME_IEC958("",CAPTURE,DEFAULT),
1200 .info = snd_hda_spdif_mask_info,
1201 .get = snd_hda_spdif_in_status_get,
1207 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
1208 * @codec: the HDA codec
1209 * @nid: audio in widget NID
1211 * Creates controls related with the SPDIF input.
1212 * Called from each patch supporting the SPDIF in.
1214 * Returns 0 if successful, or a negative error code.
1216 int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
1219 struct snd_kcontrol *kctl;
1220 struct snd_kcontrol_new *dig_mix;
1222 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
1223 kctl = snd_ctl_new1(dig_mix, codec);
1224 kctl->private_value = nid;
1225 if ((err = snd_ctl_add(codec->bus->card, kctl)) < 0)
1228 codec->spdif_in_enable = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT, 0) & 1;
1234 * set power state of the codec
1236 static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
1237 unsigned int power_state)
1239 hda_nid_t nid, nid_start;
1242 snd_hda_codec_write(codec, fg, 0, AC_VERB_SET_POWER_STATE,
1245 nodes = snd_hda_get_sub_nodes(codec, fg, &nid_start);
1246 for (nid = nid_start; nid < nodes + nid_start; nid++) {
1247 if (get_wcaps(codec, nid) & AC_WCAP_POWER)
1248 snd_hda_codec_write(codec, nid, 0,
1249 AC_VERB_SET_POWER_STATE,
1253 if (power_state == AC_PWRST_D0)
1259 * snd_hda_build_controls - build mixer controls
1262 * Creates mixer controls for each codec included in the bus.
1264 * Returns 0 if successful, otherwise a negative error code.
1266 int snd_hda_build_controls(struct hda_bus *bus)
1268 struct list_head *p;
1270 /* build controls */
1271 list_for_each(p, &bus->codec_list) {
1272 struct hda_codec *codec = list_entry(p, struct hda_codec, list);
1274 if (! codec->patch_ops.build_controls)
1276 err = codec->patch_ops.build_controls(codec);
1282 list_for_each(p, &bus->codec_list) {
1283 struct hda_codec *codec = list_entry(p, struct hda_codec, list);
1285 hda_set_power_state(codec,
1286 codec->afg ? codec->afg : codec->mfg,
1288 if (! codec->patch_ops.init)
1290 err = codec->patch_ops.init(codec);
1301 struct hda_rate_tbl {
1303 unsigned int alsa_bits;
1304 unsigned int hda_fmt;
1307 static struct hda_rate_tbl rate_bits[] = {
1308 /* rate in Hz, ALSA rate bitmask, HDA format value */
1310 /* autodetected value used in snd_hda_query_supported_pcm */
1311 { 8000, SNDRV_PCM_RATE_8000, 0x0500 }, /* 1/6 x 48 */
1312 { 11025, SNDRV_PCM_RATE_11025, 0x4300 }, /* 1/4 x 44 */
1313 { 16000, SNDRV_PCM_RATE_16000, 0x0200 }, /* 1/3 x 48 */
1314 { 22050, SNDRV_PCM_RATE_22050, 0x4100 }, /* 1/2 x 44 */
1315 { 32000, SNDRV_PCM_RATE_32000, 0x0a00 }, /* 2/3 x 48 */
1316 { 44100, SNDRV_PCM_RATE_44100, 0x4000 }, /* 44 */
1317 { 48000, SNDRV_PCM_RATE_48000, 0x0000 }, /* 48 */
1318 { 88200, SNDRV_PCM_RATE_88200, 0x4800 }, /* 2 x 44 */
1319 { 96000, SNDRV_PCM_RATE_96000, 0x0800 }, /* 2 x 48 */
1320 { 176400, SNDRV_PCM_RATE_176400, 0x5800 },/* 4 x 44 */
1321 { 192000, SNDRV_PCM_RATE_192000, 0x1800 }, /* 4 x 48 */
1323 /* not autodetected value */
1324 { 9600, SNDRV_PCM_RATE_KNOT, 0x0400 }, /* 1/5 x 48 */
1326 { 0 } /* terminator */
1330 * snd_hda_calc_stream_format - calculate format bitset
1331 * @rate: the sample rate
1332 * @channels: the number of channels
1333 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
1334 * @maxbps: the max. bps
1336 * Calculate the format bitset from the given rate, channels and th PCM format.
1338 * Return zero if invalid.
1340 unsigned int snd_hda_calc_stream_format(unsigned int rate,
1341 unsigned int channels,
1342 unsigned int format,
1343 unsigned int maxbps)
1346 unsigned int val = 0;
1348 for (i = 0; rate_bits[i].hz; i++)
1349 if (rate_bits[i].hz == rate) {
1350 val = rate_bits[i].hda_fmt;
1353 if (! rate_bits[i].hz) {
1354 snd_printdd("invalid rate %d\n", rate);
1358 if (channels == 0 || channels > 8) {
1359 snd_printdd("invalid channels %d\n", channels);
1362 val |= channels - 1;
1364 switch (snd_pcm_format_width(format)) {
1365 case 8: val |= 0x00; break;
1366 case 16: val |= 0x10; break;
1372 else if (maxbps >= 24)
1378 snd_printdd("invalid format width %d\n", snd_pcm_format_width(format));
1386 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
1387 * @codec: the HDA codec
1388 * @nid: NID to query
1389 * @ratesp: the pointer to store the detected rate bitflags
1390 * @formatsp: the pointer to store the detected formats
1391 * @bpsp: the pointer to store the detected format widths
1393 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
1394 * or @bsps argument is ignored.
1396 * Returns 0 if successful, otherwise a negative error code.
1398 int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
1399 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
1402 unsigned int val, streams;
1405 if (nid != codec->afg &&
1406 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD)) {
1407 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
1412 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
1416 for (i = 0; rate_bits[i].hz; i++) {
1418 rates |= rate_bits[i].alsa_bits;
1423 if (formatsp || bpsp) {
1428 wcaps = get_wcaps(codec, nid);
1429 streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
1433 streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
1439 if (streams & AC_SUPFMT_PCM) {
1440 if (val & AC_SUPPCM_BITS_8) {
1441 formats |= SNDRV_PCM_FMTBIT_U8;
1444 if (val & AC_SUPPCM_BITS_16) {
1445 formats |= SNDRV_PCM_FMTBIT_S16_LE;
1448 if (wcaps & AC_WCAP_DIGITAL) {
1449 if (val & AC_SUPPCM_BITS_32)
1450 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
1451 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
1452 formats |= SNDRV_PCM_FMTBIT_S32_LE;
1453 if (val & AC_SUPPCM_BITS_24)
1455 else if (val & AC_SUPPCM_BITS_20)
1457 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|AC_SUPPCM_BITS_32)) {
1458 formats |= SNDRV_PCM_FMTBIT_S32_LE;
1459 if (val & AC_SUPPCM_BITS_32)
1461 else if (val & AC_SUPPCM_BITS_20)
1463 else if (val & AC_SUPPCM_BITS_24)
1467 else if (streams == AC_SUPFMT_FLOAT32) { /* should be exclusive */
1468 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
1470 } else if (streams == AC_SUPFMT_AC3) { /* should be exclusive */
1471 /* temporary hack: we have still no proper support
1472 * for the direct AC3 stream...
1474 formats |= SNDRV_PCM_FMTBIT_U8;
1478 *formatsp = formats;
1487 * snd_hda_is_supported_format - check whether the given node supports the format val
1489 * Returns 1 if supported, 0 if not.
1491 int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
1492 unsigned int format)
1495 unsigned int val = 0, rate, stream;
1497 if (nid != codec->afg &&
1498 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD)) {
1499 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
1504 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
1509 rate = format & 0xff00;
1510 for (i = 0; rate_bits[i].hz; i++)
1511 if (rate_bits[i].hda_fmt == rate) {
1516 if (! rate_bits[i].hz)
1519 stream = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
1522 if (! stream && nid != codec->afg)
1523 stream = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
1524 if (! stream || stream == -1)
1527 if (stream & AC_SUPFMT_PCM) {
1528 switch (format & 0xf0) {
1530 if (! (val & AC_SUPPCM_BITS_8))
1534 if (! (val & AC_SUPPCM_BITS_16))
1538 if (! (val & AC_SUPPCM_BITS_20))
1542 if (! (val & AC_SUPPCM_BITS_24))
1546 if (! (val & AC_SUPPCM_BITS_32))
1553 /* FIXME: check for float32 and AC3? */
1562 static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
1563 struct hda_codec *codec,
1564 struct snd_pcm_substream *substream)
1569 static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
1570 struct hda_codec *codec,
1571 unsigned int stream_tag,
1572 unsigned int format,
1573 struct snd_pcm_substream *substream)
1575 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
1579 static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
1580 struct hda_codec *codec,
1581 struct snd_pcm_substream *substream)
1583 snd_hda_codec_setup_stream(codec, hinfo->nid, 0, 0, 0);
1587 static int set_pcm_default_values(struct hda_codec *codec, struct hda_pcm_stream *info)
1590 /* query support PCM information from the given NID */
1591 if (! info->rates || ! info->formats)
1592 snd_hda_query_supported_pcm(codec, info->nid,
1593 info->rates ? NULL : &info->rates,
1594 info->formats ? NULL : &info->formats,
1595 info->maxbps ? NULL : &info->maxbps);
1597 if (info->ops.open == NULL)
1598 info->ops.open = hda_pcm_default_open_close;
1599 if (info->ops.close == NULL)
1600 info->ops.close = hda_pcm_default_open_close;
1601 if (info->ops.prepare == NULL) {
1602 snd_assert(info->nid, return -EINVAL);
1603 info->ops.prepare = hda_pcm_default_prepare;
1605 if (info->ops.cleanup == NULL) {
1606 snd_assert(info->nid, return -EINVAL);
1607 info->ops.cleanup = hda_pcm_default_cleanup;
1613 * snd_hda_build_pcms - build PCM information
1616 * Create PCM information for each codec included in the bus.
1618 * The build_pcms codec patch is requested to set up codec->num_pcms and
1619 * codec->pcm_info properly. The array is referred by the top-level driver
1620 * to create its PCM instances.
1621 * The allocated codec->pcm_info should be released in codec->patch_ops.free
1624 * At least, substreams, channels_min and channels_max must be filled for
1625 * each stream. substreams = 0 indicates that the stream doesn't exist.
1626 * When rates and/or formats are zero, the supported values are queried
1627 * from the given nid. The nid is used also by the default ops.prepare
1628 * and ops.cleanup callbacks.
1630 * The driver needs to call ops.open in its open callback. Similarly,
1631 * ops.close is supposed to be called in the close callback.
1632 * ops.prepare should be called in the prepare or hw_params callback
1633 * with the proper parameters for set up.
1634 * ops.cleanup should be called in hw_free for clean up of streams.
1636 * This function returns 0 if successfull, or a negative error code.
1638 int snd_hda_build_pcms(struct hda_bus *bus)
1640 struct list_head *p;
1642 list_for_each(p, &bus->codec_list) {
1643 struct hda_codec *codec = list_entry(p, struct hda_codec, list);
1644 unsigned int pcm, s;
1646 if (! codec->patch_ops.build_pcms)
1648 err = codec->patch_ops.build_pcms(codec);
1651 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
1652 for (s = 0; s < 2; s++) {
1653 struct hda_pcm_stream *info;
1654 info = &codec->pcm_info[pcm].stream[s];
1655 if (! info->substreams)
1657 err = set_pcm_default_values(codec, info);
1668 * snd_hda_check_board_config - compare the current codec with the config table
1669 * @codec: the HDA codec
1670 * @tbl: configuration table, terminated by null entries
1672 * Compares the modelname or PCI subsystem id of the current codec with the
1673 * given configuration table. If a matching entry is found, returns its
1674 * config value (supposed to be 0 or positive).
1676 * If no entries are matching, the function returns a negative value.
1678 int snd_hda_check_board_config(struct hda_codec *codec, const struct hda_board_config *tbl)
1680 const struct hda_board_config *c;
1682 if (codec->bus->modelname) {
1683 for (c = tbl; c->modelname || c->pci_subvendor; c++) {
1685 ! strcmp(codec->bus->modelname, c->modelname)) {
1686 snd_printd(KERN_INFO "hda_codec: model '%s' is selected\n", c->modelname);
1692 if (codec->bus->pci) {
1693 u16 subsystem_vendor, subsystem_device;
1694 pci_read_config_word(codec->bus->pci, PCI_SUBSYSTEM_VENDOR_ID, &subsystem_vendor);
1695 pci_read_config_word(codec->bus->pci, PCI_SUBSYSTEM_ID, &subsystem_device);
1696 for (c = tbl; c->modelname || c->pci_subvendor; c++) {
1697 if (c->pci_subvendor == subsystem_vendor &&
1698 (! c->pci_subdevice /* all match */||
1699 (c->pci_subdevice == subsystem_device))) {
1700 snd_printdd(KERN_INFO "hda_codec: PCI %x:%x, codec config %d is selected\n",
1701 subsystem_vendor, subsystem_device, c->config);
1710 * snd_hda_add_new_ctls - create controls from the array
1711 * @codec: the HDA codec
1712 * @knew: the array of struct snd_kcontrol_new
1714 * This helper function creates and add new controls in the given array.
1715 * The array must be terminated with an empty entry as terminator.
1717 * Returns 0 if successful, or a negative error code.
1719 int snd_hda_add_new_ctls(struct hda_codec *codec, struct snd_kcontrol_new *knew)
1723 for (; knew->name; knew++) {
1724 struct snd_kcontrol *kctl;
1725 kctl = snd_ctl_new1(knew, codec);
1728 err = snd_ctl_add(codec->bus->card, kctl);
1732 kctl = snd_ctl_new1(knew, codec);
1735 kctl->id.device = codec->addr;
1736 if ((err = snd_ctl_add(codec->bus->card, kctl)) < 0)
1745 * Channel mode helper
1747 int snd_hda_ch_mode_info(struct hda_codec *codec, struct snd_ctl_elem_info *uinfo,
1748 const struct hda_channel_mode *chmode, int num_chmodes)
1750 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1752 uinfo->value.enumerated.items = num_chmodes;
1753 if (uinfo->value.enumerated.item >= num_chmodes)
1754 uinfo->value.enumerated.item = num_chmodes - 1;
1755 sprintf(uinfo->value.enumerated.name, "%dch",
1756 chmode[uinfo->value.enumerated.item].channels);
1760 int snd_hda_ch_mode_get(struct hda_codec *codec, struct snd_ctl_elem_value *ucontrol,
1761 const struct hda_channel_mode *chmode, int num_chmodes,
1766 for (i = 0; i < num_chmodes; i++) {
1767 if (max_channels == chmode[i].channels) {
1768 ucontrol->value.enumerated.item[0] = i;
1775 int snd_hda_ch_mode_put(struct hda_codec *codec, struct snd_ctl_elem_value *ucontrol,
1776 const struct hda_channel_mode *chmode, int num_chmodes,
1781 mode = ucontrol->value.enumerated.item[0];
1782 snd_assert(mode < num_chmodes, return -EINVAL);
1783 if (*max_channelsp == chmode[mode].channels && ! codec->in_resume)
1785 /* change the current channel setting */
1786 *max_channelsp = chmode[mode].channels;
1787 if (chmode[mode].sequence)
1788 snd_hda_sequence_write(codec, chmode[mode].sequence);
1795 int snd_hda_input_mux_info(const struct hda_input_mux *imux, struct snd_ctl_elem_info *uinfo)
1799 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1801 uinfo->value.enumerated.items = imux->num_items;
1802 index = uinfo->value.enumerated.item;
1803 if (index >= imux->num_items)
1804 index = imux->num_items - 1;
1805 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
1809 int snd_hda_input_mux_put(struct hda_codec *codec, const struct hda_input_mux *imux,
1810 struct snd_ctl_elem_value *ucontrol, hda_nid_t nid,
1811 unsigned int *cur_val)
1815 idx = ucontrol->value.enumerated.item[0];
1816 if (idx >= imux->num_items)
1817 idx = imux->num_items - 1;
1818 if (*cur_val == idx && ! codec->in_resume)
1820 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
1821 imux->items[idx].index);
1828 * Multi-channel / digital-out PCM helper functions
1832 * open the digital out in the exclusive mode
1834 int snd_hda_multi_out_dig_open(struct hda_codec *codec, struct hda_multi_out *mout)
1836 mutex_lock(&codec->spdif_mutex);
1837 if (mout->dig_out_used) {
1838 mutex_unlock(&codec->spdif_mutex);
1839 return -EBUSY; /* already being used */
1841 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
1842 mutex_unlock(&codec->spdif_mutex);
1847 * release the digital out
1849 int snd_hda_multi_out_dig_close(struct hda_codec *codec, struct hda_multi_out *mout)
1851 mutex_lock(&codec->spdif_mutex);
1852 mout->dig_out_used = 0;
1853 mutex_unlock(&codec->spdif_mutex);
1858 * set up more restrictions for analog out
1860 int snd_hda_multi_out_analog_open(struct hda_codec *codec, struct hda_multi_out *mout,
1861 struct snd_pcm_substream *substream)
1863 substream->runtime->hw.channels_max = mout->max_channels;
1864 return snd_pcm_hw_constraint_step(substream->runtime, 0,
1865 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
1869 * set up the i/o for analog out
1870 * when the digital out is available, copy the front out to digital out, too.
1872 int snd_hda_multi_out_analog_prepare(struct hda_codec *codec, struct hda_multi_out *mout,
1873 unsigned int stream_tag,
1874 unsigned int format,
1875 struct snd_pcm_substream *substream)
1877 hda_nid_t *nids = mout->dac_nids;
1878 int chs = substream->runtime->channels;
1881 mutex_lock(&codec->spdif_mutex);
1882 if (mout->dig_out_nid && mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
1884 snd_hda_is_supported_format(codec, mout->dig_out_nid, format) &&
1885 ! (codec->spdif_status & IEC958_AES0_NONAUDIO)) {
1886 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
1887 /* setup digital receiver */
1888 snd_hda_codec_setup_stream(codec, mout->dig_out_nid,
1889 stream_tag, 0, format);
1891 mout->dig_out_used = 0;
1892 snd_hda_codec_setup_stream(codec, mout->dig_out_nid, 0, 0, 0);
1895 mutex_unlock(&codec->spdif_mutex);
1898 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag, 0, format);
1900 /* headphone out will just decode front left/right (stereo) */
1901 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag, 0, format);
1902 /* extra outputs copied from front */
1903 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
1904 if (mout->extra_out_nid[i])
1905 snd_hda_codec_setup_stream(codec,
1906 mout->extra_out_nid[i],
1907 stream_tag, 0, format);
1910 for (i = 1; i < mout->num_dacs; i++) {
1911 if (chs >= (i + 1) * 2) /* independent out */
1912 snd_hda_codec_setup_stream(codec, nids[i], stream_tag, i * 2,
1914 else /* copy front */
1915 snd_hda_codec_setup_stream(codec, nids[i], stream_tag, 0,
1922 * clean up the setting for analog out
1924 int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec, struct hda_multi_out *mout)
1926 hda_nid_t *nids = mout->dac_nids;
1929 for (i = 0; i < mout->num_dacs; i++)
1930 snd_hda_codec_setup_stream(codec, nids[i], 0, 0, 0);
1932 snd_hda_codec_setup_stream(codec, mout->hp_nid, 0, 0, 0);
1933 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
1934 if (mout->extra_out_nid[i])
1935 snd_hda_codec_setup_stream(codec,
1936 mout->extra_out_nid[i],
1938 mutex_lock(&codec->spdif_mutex);
1939 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
1940 snd_hda_codec_setup_stream(codec, mout->dig_out_nid, 0, 0, 0);
1941 mout->dig_out_used = 0;
1943 mutex_unlock(&codec->spdif_mutex);
1948 * Helper for automatic ping configuration
1951 static int is_in_nid_list(hda_nid_t nid, hda_nid_t *list)
1953 for (; *list; list++)
1960 * Parse all pin widgets and store the useful pin nids to cfg
1962 * The number of line-outs or any primary output is stored in line_outs,
1963 * and the corresponding output pins are assigned to line_out_pins[],
1964 * in the order of front, rear, CLFE, side, ...
1966 * If more extra outputs (speaker and headphone) are found, the pins are
1967 * assisnged to hp_pin and speaker_pins[], respectively. If no line-out jack
1968 * is detected, one of speaker of HP pins is assigned as the primary
1969 * output, i.e. to line_out_pins[0]. So, line_outs is always positive
1970 * if any analog output exists.
1972 * The analog input pins are assigned to input_pins array.
1973 * The digital input/output pins are assigned to dig_in_pin and dig_out_pin,
1976 int snd_hda_parse_pin_def_config(struct hda_codec *codec, struct auto_pin_cfg *cfg,
1977 hda_nid_t *ignore_nids)
1979 hda_nid_t nid, nid_start;
1981 short seq, assoc_line_out, sequences[ARRAY_SIZE(cfg->line_out_pins)];
1983 memset(cfg, 0, sizeof(*cfg));
1985 memset(sequences, 0, sizeof(sequences));
1988 nodes = snd_hda_get_sub_nodes(codec, codec->afg, &nid_start);
1989 for (nid = nid_start; nid < nodes + nid_start; nid++) {
1990 unsigned int wid_caps = get_wcaps(codec, nid);
1991 unsigned int wid_type = (wid_caps & AC_WCAP_TYPE) >> AC_WCAP_TYPE_SHIFT;
1992 unsigned int def_conf;
1995 /* read all default configuration for pin complex */
1996 if (wid_type != AC_WID_PIN)
1998 /* ignore the given nids (e.g. pc-beep returns error) */
1999 if (ignore_nids && is_in_nid_list(nid, ignore_nids))
2002 def_conf = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONFIG_DEFAULT, 0);
2003 if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE)
2005 loc = get_defcfg_location(def_conf);
2006 switch (get_defcfg_device(def_conf)) {
2007 case AC_JACK_LINE_OUT:
2008 seq = get_defcfg_sequence(def_conf);
2009 assoc = get_defcfg_association(def_conf);
2012 if (! assoc_line_out)
2013 assoc_line_out = assoc;
2014 else if (assoc_line_out != assoc)
2016 if (cfg->line_outs >= ARRAY_SIZE(cfg->line_out_pins))
2018 cfg->line_out_pins[cfg->line_outs] = nid;
2019 sequences[cfg->line_outs] = seq;
2022 case AC_JACK_SPEAKER:
2023 if (cfg->speaker_outs >= ARRAY_SIZE(cfg->speaker_pins))
2025 cfg->speaker_pins[cfg->speaker_outs] = nid;
2026 cfg->speaker_outs++;
2028 case AC_JACK_HP_OUT:
2031 case AC_JACK_MIC_IN:
2032 if (loc == AC_JACK_LOC_FRONT)
2033 cfg->input_pins[AUTO_PIN_FRONT_MIC] = nid;
2035 cfg->input_pins[AUTO_PIN_MIC] = nid;
2037 case AC_JACK_LINE_IN:
2038 if (loc == AC_JACK_LOC_FRONT)
2039 cfg->input_pins[AUTO_PIN_FRONT_LINE] = nid;
2041 cfg->input_pins[AUTO_PIN_LINE] = nid;
2044 cfg->input_pins[AUTO_PIN_CD] = nid;
2047 cfg->input_pins[AUTO_PIN_AUX] = nid;
2049 case AC_JACK_SPDIF_OUT:
2050 cfg->dig_out_pin = nid;
2052 case AC_JACK_SPDIF_IN:
2053 cfg->dig_in_pin = nid;
2058 /* sort by sequence */
2059 for (i = 0; i < cfg->line_outs; i++)
2060 for (j = i + 1; j < cfg->line_outs; j++)
2061 if (sequences[i] > sequences[j]) {
2063 sequences[i] = sequences[j];
2065 nid = cfg->line_out_pins[i];
2066 cfg->line_out_pins[i] = cfg->line_out_pins[j];
2067 cfg->line_out_pins[j] = nid;
2070 /* Reorder the surround channels
2071 * ALSA sequence is front/surr/clfe/side
2073 * 4-ch: front/surr => OK as it is
2074 * 6-ch: front/clfe/surr
2075 * 8-ch: front/clfe/side/surr
2077 switch (cfg->line_outs) {
2079 nid = cfg->line_out_pins[1];
2080 cfg->line_out_pins[1] = cfg->line_out_pins[2];
2081 cfg->line_out_pins[2] = nid;
2084 nid = cfg->line_out_pins[1];
2085 cfg->line_out_pins[1] = cfg->line_out_pins[3];
2086 cfg->line_out_pins[3] = cfg->line_out_pins[2];
2087 cfg->line_out_pins[2] = nid;
2092 * debug prints of the parsed results
2094 snd_printd("autoconfig: line_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
2095 cfg->line_outs, cfg->line_out_pins[0], cfg->line_out_pins[1],
2096 cfg->line_out_pins[2], cfg->line_out_pins[3],
2097 cfg->line_out_pins[4]);
2098 snd_printd(" speaker_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
2099 cfg->speaker_outs, cfg->speaker_pins[0],
2100 cfg->speaker_pins[1], cfg->speaker_pins[2],
2101 cfg->speaker_pins[3], cfg->speaker_pins[4]);
2102 snd_printd(" hp=0x%x, dig_out=0x%x, din_in=0x%x\n",
2103 cfg->hp_pin, cfg->dig_out_pin, cfg->dig_in_pin);
2104 snd_printd(" inputs: mic=0x%x, fmic=0x%x, line=0x%x, fline=0x%x,"
2105 " cd=0x%x, aux=0x%x\n",
2106 cfg->input_pins[AUTO_PIN_MIC],
2107 cfg->input_pins[AUTO_PIN_FRONT_MIC],
2108 cfg->input_pins[AUTO_PIN_LINE],
2109 cfg->input_pins[AUTO_PIN_FRONT_LINE],
2110 cfg->input_pins[AUTO_PIN_CD],
2111 cfg->input_pins[AUTO_PIN_AUX]);
2114 * FIX-UP: if no line-outs are detected, try to use speaker or HP pin
2115 * as a primary output
2117 if (! cfg->line_outs) {
2118 if (cfg->speaker_outs) {
2119 cfg->line_outs = cfg->speaker_outs;
2120 memcpy(cfg->line_out_pins, cfg->speaker_pins,
2121 sizeof(cfg->speaker_pins));
2122 cfg->speaker_outs = 0;
2123 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
2124 } else if (cfg->hp_pin) {
2126 cfg->line_out_pins[0] = cfg->hp_pin;
2134 /* labels for input pins */
2135 const char *auto_pin_cfg_labels[AUTO_PIN_LAST] = {
2136 "Mic", "Front Mic", "Line", "Front Line", "CD", "Aux"
2146 * snd_hda_suspend - suspend the codecs
2148 * @state: suspsend state
2150 * Returns 0 if successful.
2152 int snd_hda_suspend(struct hda_bus *bus, pm_message_t state)
2154 struct list_head *p;
2156 /* FIXME: should handle power widget capabilities */
2157 list_for_each(p, &bus->codec_list) {
2158 struct hda_codec *codec = list_entry(p, struct hda_codec, list);
2159 if (codec->patch_ops.suspend)
2160 codec->patch_ops.suspend(codec, state);
2161 hda_set_power_state(codec,
2162 codec->afg ? codec->afg : codec->mfg,
2169 * snd_hda_resume - resume the codecs
2171 * @state: resume state
2173 * Returns 0 if successful.
2175 int snd_hda_resume(struct hda_bus *bus)
2177 struct list_head *p;
2179 list_for_each(p, &bus->codec_list) {
2180 struct hda_codec *codec = list_entry(p, struct hda_codec, list);
2181 hda_set_power_state(codec,
2182 codec->afg ? codec->afg : codec->mfg,
2184 if (codec->patch_ops.resume)
2185 codec->patch_ops.resume(codec);
2191 * snd_hda_resume_ctls - resume controls in the new control list
2192 * @codec: the HDA codec
2193 * @knew: the array of struct snd_kcontrol_new
2195 * This function resumes the mixer controls in the struct snd_kcontrol_new array,
2196 * originally for snd_hda_add_new_ctls().
2197 * The array must be terminated with an empty entry as terminator.
2199 int snd_hda_resume_ctls(struct hda_codec *codec, struct snd_kcontrol_new *knew)
2201 struct snd_ctl_elem_value *val;
2203 val = kmalloc(sizeof(*val), GFP_KERNEL);
2206 codec->in_resume = 1;
2207 for (; knew->name; knew++) {
2209 count = knew->count ? knew->count : 1;
2210 for (i = 0; i < count; i++) {
2211 memset(val, 0, sizeof(*val));
2212 val->id.iface = knew->iface;
2213 val->id.device = knew->device;
2214 val->id.subdevice = knew->subdevice;
2215 strcpy(val->id.name, knew->name);
2216 val->id.index = knew->index ? knew->index : i;
2217 /* Assume that get callback reads only from cache,
2218 * not accessing to the real hardware
2220 if (snd_ctl_elem_read(codec->bus->card, val) < 0)
2222 snd_ctl_elem_write(codec->bus->card, NULL, val);
2225 codec->in_resume = 0;
2231 * snd_hda_resume_spdif_out - resume the digital out
2232 * @codec: the HDA codec
2234 int snd_hda_resume_spdif_out(struct hda_codec *codec)
2236 return snd_hda_resume_ctls(codec, dig_mixes);
2240 * snd_hda_resume_spdif_in - resume the digital in
2241 * @codec: the HDA codec
2243 int snd_hda_resume_spdif_in(struct hda_codec *codec)
2245 return snd_hda_resume_ctls(codec, dig_in_ctls);
2250 * symbols exported for controller modules
2252 EXPORT_SYMBOL(snd_hda_codec_read);
2253 EXPORT_SYMBOL(snd_hda_codec_write);
2254 EXPORT_SYMBOL(snd_hda_sequence_write);
2255 EXPORT_SYMBOL(snd_hda_get_sub_nodes);
2256 EXPORT_SYMBOL(snd_hda_queue_unsol_event);
2257 EXPORT_SYMBOL(snd_hda_bus_new);
2258 EXPORT_SYMBOL(snd_hda_codec_new);
2259 EXPORT_SYMBOL(snd_hda_codec_setup_stream);
2260 EXPORT_SYMBOL(snd_hda_calc_stream_format);
2261 EXPORT_SYMBOL(snd_hda_build_pcms);
2262 EXPORT_SYMBOL(snd_hda_build_controls);
2264 EXPORT_SYMBOL(snd_hda_suspend);
2265 EXPORT_SYMBOL(snd_hda_resume);
2272 static int __init alsa_hda_init(void)
2277 static void __exit alsa_hda_exit(void)
2281 module_init(alsa_hda_init)
2282 module_exit(alsa_hda_exit)