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/mutex.h>
28 #include <sound/core.h>
29 #include "hda_codec.h"
30 #include <sound/asoundef.h>
31 #include <sound/tlv.h>
32 #include <sound/initval.h>
33 #include "hda_local.h"
37 * vendor / preset table
40 struct hda_vendor_id {
45 /* codec vendor labels */
46 static struct hda_vendor_id hda_vendor_ids[] = {
47 { 0x10ec, "Realtek" },
48 { 0x1057, "Motorola" },
50 { 0x11d4, "Analog Devices" },
51 { 0x13f6, "C-Media" },
52 { 0x14f1, "Conexant" },
53 { 0x434d, "C-Media" },
54 { 0x8384, "SigmaTel" },
59 #include "hda_patch.h"
63 * snd_hda_codec_read - send a command and get the response
64 * @codec: the HDA codec
65 * @nid: NID to send the command
66 * @direct: direct flag
67 * @verb: the verb to send
68 * @parm: the parameter for the verb
70 * Send a single command and read the corresponding response.
72 * Returns the obtained response value, or -1 for an error.
74 unsigned int snd_hda_codec_read(struct hda_codec *codec, hda_nid_t nid,
76 unsigned int verb, unsigned int parm)
79 mutex_lock(&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 mutex_unlock(&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 mutex_lock(&codec->bus->cmd_mutex);
105 err = codec->bus->ops.command(codec, nid, direct, verb, parm);
106 mutex_unlock(&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,
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,
184 AC_VERB_GET_CONNECT_LIST, 0);
185 conn_list[0] = parm & mask;
189 /* multi connection */
192 for (i = 0; i < conn_len; i++) {
196 if (i % num_elems == 0)
197 parm = snd_hda_codec_read(codec, nid, 0,
198 AC_VERB_GET_CONNECT_LIST, i);
199 range_val = !!(parm & (1 << (shift-1))); /* ranges */
203 /* ranges between the previous and this one */
204 if (!prev_nid || prev_nid >= val) {
205 snd_printk(KERN_WARNING "hda_codec: "
206 "invalid dep_range_val %x:%x\n",
210 for (n = prev_nid + 1; n <= val; n++) {
211 if (conns >= max_conns) {
213 "Too many connections\n");
216 conn_list[conns++] = n;
219 if (conns >= max_conns) {
220 snd_printk(KERN_ERR "Too many connections\n");
223 conn_list[conns++] = val;
232 * snd_hda_queue_unsol_event - add an unsolicited event to queue
234 * @res: unsolicited event (lower 32bit of RIRB entry)
235 * @res_ex: codec addr and flags (upper 32bit or RIRB entry)
237 * Adds the given event to the queue. The events are processed in
238 * the workqueue asynchronously. Call this function in the interrupt
239 * hanlder when RIRB receives an unsolicited event.
241 * Returns 0 if successful, or a negative error code.
243 int snd_hda_queue_unsol_event(struct hda_bus *bus, u32 res, u32 res_ex)
245 struct hda_bus_unsolicited *unsol;
252 wp = (unsol->wp + 1) % HDA_UNSOL_QUEUE_SIZE;
256 unsol->queue[wp] = res;
257 unsol->queue[wp + 1] = res_ex;
259 schedule_work(&unsol->work);
265 * process queueud unsolicited events
267 static void process_unsol_events(struct work_struct *work)
269 struct hda_bus_unsolicited *unsol =
270 container_of(work, struct hda_bus_unsolicited, work);
271 struct hda_bus *bus = unsol->bus;
272 struct hda_codec *codec;
273 unsigned int rp, caddr, res;
275 while (unsol->rp != unsol->wp) {
276 rp = (unsol->rp + 1) % HDA_UNSOL_QUEUE_SIZE;
279 res = unsol->queue[rp];
280 caddr = unsol->queue[rp + 1];
281 if (!(caddr & (1 << 4))) /* no unsolicited event? */
283 codec = bus->caddr_tbl[caddr & 0x0f];
284 if (codec && codec->patch_ops.unsol_event)
285 codec->patch_ops.unsol_event(codec, res);
290 * initialize unsolicited queue
292 static int __devinit init_unsol_queue(struct hda_bus *bus)
294 struct hda_bus_unsolicited *unsol;
296 if (bus->unsol) /* already initialized */
299 unsol = kzalloc(sizeof(*unsol), GFP_KERNEL);
301 snd_printk(KERN_ERR "hda_codec: "
302 "can't allocate unsolicited queue\n");
305 INIT_WORK(&unsol->work, process_unsol_events);
314 static void snd_hda_codec_free(struct hda_codec *codec);
316 static int snd_hda_bus_free(struct hda_bus *bus)
318 struct hda_codec *codec, *n;
323 flush_scheduled_work();
326 list_for_each_entry_safe(codec, n, &bus->codec_list, list) {
327 snd_hda_codec_free(codec);
329 if (bus->ops.private_free)
330 bus->ops.private_free(bus);
335 static int snd_hda_bus_dev_free(struct snd_device *device)
337 struct hda_bus *bus = device->device_data;
338 return snd_hda_bus_free(bus);
342 * snd_hda_bus_new - create a HDA bus
343 * @card: the card entry
344 * @temp: the template for hda_bus information
345 * @busp: the pointer to store the created bus instance
347 * Returns 0 if successful, or a negative error code.
349 int __devinit snd_hda_bus_new(struct snd_card *card,
350 const struct hda_bus_template *temp,
351 struct hda_bus **busp)
355 static struct snd_device_ops dev_ops = {
356 .dev_free = snd_hda_bus_dev_free,
359 snd_assert(temp, return -EINVAL);
360 snd_assert(temp->ops.command && temp->ops.get_response, return -EINVAL);
365 bus = kzalloc(sizeof(*bus), GFP_KERNEL);
367 snd_printk(KERN_ERR "can't allocate struct hda_bus\n");
372 bus->private_data = temp->private_data;
373 bus->pci = temp->pci;
374 bus->modelname = temp->modelname;
375 bus->ops = temp->ops;
377 mutex_init(&bus->cmd_mutex);
378 INIT_LIST_HEAD(&bus->codec_list);
380 err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops);
382 snd_hda_bus_free(bus);
391 * find a matching codec preset
393 static const struct hda_codec_preset __devinit *
394 find_codec_preset(struct hda_codec *codec)
396 const struct hda_codec_preset **tbl, *preset;
398 if (codec->bus->modelname && !strcmp(codec->bus->modelname, "generic"))
399 return NULL; /* use the generic parser */
401 for (tbl = hda_preset_tables; *tbl; tbl++) {
402 for (preset = *tbl; preset->id; preset++) {
403 u32 mask = preset->mask;
406 if (preset->id == (codec->vendor_id & mask) &&
408 preset->rev == codec->revision_id))
416 * snd_hda_get_codec_name - store the codec name
418 void snd_hda_get_codec_name(struct hda_codec *codec,
419 char *name, int namelen)
421 const struct hda_vendor_id *c;
422 const char *vendor = NULL;
423 u16 vendor_id = codec->vendor_id >> 16;
426 for (c = hda_vendor_ids; c->id; c++) {
427 if (c->id == vendor_id) {
433 sprintf(tmp, "Generic %04x", vendor_id);
436 if (codec->preset && codec->preset->name)
437 snprintf(name, namelen, "%s %s", vendor, codec->preset->name);
439 snprintf(name, namelen, "%s ID %x", vendor,
440 codec->vendor_id & 0xffff);
444 * look for an AFG and MFG nodes
446 static void __devinit setup_fg_nodes(struct hda_codec *codec)
451 total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid);
452 for (i = 0; i < total_nodes; i++, nid++) {
454 func = snd_hda_param_read(codec, nid, AC_PAR_FUNCTION_TYPE);
455 switch (func & 0xff) {
456 case AC_GRP_AUDIO_FUNCTION:
459 case AC_GRP_MODEM_FUNCTION:
469 * read widget caps for each widget and store in cache
471 static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
476 codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node,
478 codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL);
481 nid = codec->start_nid;
482 for (i = 0; i < codec->num_nodes; i++, nid++)
483 codec->wcaps[i] = snd_hda_param_read(codec, nid,
484 AC_PAR_AUDIO_WIDGET_CAP);
492 static void snd_hda_codec_free(struct hda_codec *codec)
496 list_del(&codec->list);
497 codec->bus->caddr_tbl[codec->addr] = NULL;
498 if (codec->patch_ops.free)
499 codec->patch_ops.free(codec);
500 kfree(codec->amp_info);
505 static void init_amp_hash(struct hda_codec *codec);
508 * snd_hda_codec_new - create a HDA codec
509 * @bus: the bus to assign
510 * @codec_addr: the codec address
511 * @codecp: the pointer to store the generated codec
513 * Returns 0 if successful, or a negative error code.
515 int __devinit snd_hda_codec_new(struct hda_bus *bus, unsigned int codec_addr,
516 struct hda_codec **codecp)
518 struct hda_codec *codec;
522 snd_assert(bus, return -EINVAL);
523 snd_assert(codec_addr <= HDA_MAX_CODEC_ADDRESS, return -EINVAL);
525 if (bus->caddr_tbl[codec_addr]) {
526 snd_printk(KERN_ERR "hda_codec: "
527 "address 0x%x is already occupied\n", codec_addr);
531 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
533 snd_printk(KERN_ERR "can't allocate struct hda_codec\n");
538 codec->addr = codec_addr;
539 mutex_init(&codec->spdif_mutex);
540 init_amp_hash(codec);
542 list_add_tail(&codec->list, &bus->codec_list);
543 bus->caddr_tbl[codec_addr] = codec;
545 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
547 if (codec->vendor_id == -1)
548 /* read again, hopefully the access method was corrected
549 * in the last read...
551 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
553 codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT,
554 AC_PAR_SUBSYSTEM_ID);
555 codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT,
558 setup_fg_nodes(codec);
559 if (!codec->afg && !codec->mfg) {
560 snd_printdd("hda_codec: no AFG or MFG node found\n");
561 snd_hda_codec_free(codec);
565 if (read_widget_caps(codec, codec->afg ? codec->afg : codec->mfg) < 0) {
566 snd_printk(KERN_ERR "hda_codec: cannot malloc\n");
567 snd_hda_codec_free(codec);
571 if (!codec->subsystem_id) {
572 hda_nid_t nid = codec->afg ? codec->afg : codec->mfg;
573 codec->subsystem_id =
574 snd_hda_codec_read(codec, nid, 0,
575 AC_VERB_GET_SUBSYSTEM_ID, 0);
578 codec->preset = find_codec_preset(codec);
579 /* audio codec should override the mixer name */
580 if (codec->afg || !*bus->card->mixername)
581 snd_hda_get_codec_name(codec, bus->card->mixername,
582 sizeof(bus->card->mixername));
584 if (codec->preset && codec->preset->patch)
585 err = codec->preset->patch(codec);
587 err = snd_hda_parse_generic_codec(codec);
589 snd_hda_codec_free(codec);
593 if (codec->patch_ops.unsol_event)
594 init_unsol_queue(bus);
596 snd_hda_codec_proc_new(codec);
598 sprintf(component, "HDA:%08x", codec->vendor_id);
599 snd_component_add(codec->bus->card, component);
607 * snd_hda_codec_setup_stream - set up the codec for streaming
608 * @codec: the CODEC to set up
609 * @nid: the NID to set up
610 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
611 * @channel_id: channel id to pass, zero based.
612 * @format: stream format.
614 void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
616 int channel_id, int format)
621 snd_printdd("hda_codec_setup_stream: "
622 "NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
623 nid, stream_tag, channel_id, format);
624 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID,
625 (stream_tag << 4) | channel_id);
627 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, format);
631 * amp access functions
634 /* FIXME: more better hash key? */
635 #define HDA_HASH_KEY(nid,dir,idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
636 #define INFO_AMP_CAPS (1<<0)
637 #define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
639 /* initialize the hash table */
640 static void __devinit init_amp_hash(struct hda_codec *codec)
642 memset(codec->amp_hash, 0xff, sizeof(codec->amp_hash));
643 codec->num_amp_entries = 0;
644 codec->amp_info_size = 0;
645 codec->amp_info = NULL;
648 /* query the hash. allocate an entry if not found. */
649 static struct hda_amp_info *get_alloc_amp_hash(struct hda_codec *codec, u32 key)
651 u16 idx = key % (u16)ARRAY_SIZE(codec->amp_hash);
652 u16 cur = codec->amp_hash[idx];
653 struct hda_amp_info *info;
655 while (cur != 0xffff) {
656 info = &codec->amp_info[cur];
657 if (info->key == key)
662 /* add a new hash entry */
663 if (codec->num_amp_entries >= codec->amp_info_size) {
664 /* reallocate the array */
665 int new_size = codec->amp_info_size + 64;
666 struct hda_amp_info *new_info;
667 new_info = kcalloc(new_size, sizeof(struct hda_amp_info),
670 snd_printk(KERN_ERR "hda_codec: "
671 "can't malloc amp_info\n");
674 if (codec->amp_info) {
675 memcpy(new_info, codec->amp_info,
676 codec->amp_info_size *
677 sizeof(struct hda_amp_info));
678 kfree(codec->amp_info);
680 codec->amp_info_size = new_size;
681 codec->amp_info = new_info;
683 cur = codec->num_amp_entries++;
684 info = &codec->amp_info[cur];
686 info->status = 0; /* not initialized yet */
687 info->next = codec->amp_hash[idx];
688 codec->amp_hash[idx] = cur;
694 * query AMP capabilities for the given widget and direction
696 static u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
698 struct hda_amp_info *info;
700 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, 0));
703 if (!(info->status & INFO_AMP_CAPS)) {
704 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
706 info->amp_caps = snd_hda_param_read(codec, nid,
707 direction == HDA_OUTPUT ?
711 info->status |= INFO_AMP_CAPS;
713 return info->amp_caps;
716 int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
719 struct hda_amp_info *info;
721 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, dir, 0));
724 info->amp_caps = caps;
725 info->status |= INFO_AMP_CAPS;
730 * read the current volume to info
731 * if the cache exists, read the cache value.
733 static unsigned int get_vol_mute(struct hda_codec *codec,
734 struct hda_amp_info *info, hda_nid_t nid,
735 int ch, int direction, int index)
739 if (info->status & INFO_AMP_VOL(ch))
740 return info->vol[ch];
742 parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
743 parm |= direction == HDA_OUTPUT ? AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
745 val = snd_hda_codec_read(codec, nid, 0,
746 AC_VERB_GET_AMP_GAIN_MUTE, parm);
747 info->vol[ch] = val & 0xff;
748 info->status |= INFO_AMP_VOL(ch);
749 return info->vol[ch];
753 * write the current volume in info to the h/w and update the cache
755 static void put_vol_mute(struct hda_codec *codec, struct hda_amp_info *info,
756 hda_nid_t nid, int ch, int direction, int index,
761 parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
762 parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
763 parm |= index << AC_AMP_SET_INDEX_SHIFT;
765 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
770 * read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
772 int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
773 int direction, int index)
775 struct hda_amp_info *info;
776 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
779 return get_vol_mute(codec, info, nid, ch, direction, index);
783 * update the AMP value, mask = bit mask to set, val = the value
785 int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
786 int direction, int idx, int mask, int val)
788 struct hda_amp_info *info;
790 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, idx));
794 val |= get_vol_mute(codec, info, nid, ch, direction, idx) & ~mask;
795 if (info->vol[ch] == val && !codec->in_resume)
797 put_vol_mute(codec, info, nid, ch, direction, idx, val);
803 * AMP control callbacks
805 /* retrieve parameters from private_value */
806 #define get_amp_nid(kc) ((kc)->private_value & 0xffff)
807 #define get_amp_channels(kc) (((kc)->private_value >> 16) & 0x3)
808 #define get_amp_direction(kc) (((kc)->private_value >> 18) & 0x1)
809 #define get_amp_index(kc) (((kc)->private_value >> 19) & 0xf)
812 int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
813 struct snd_ctl_elem_info *uinfo)
815 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
816 u16 nid = get_amp_nid(kcontrol);
817 u8 chs = get_amp_channels(kcontrol);
818 int dir = get_amp_direction(kcontrol);
821 caps = query_amp_caps(codec, nid, dir);
823 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
825 printk(KERN_WARNING "hda_codec: "
826 "num_steps = 0 for NID=0x%x\n", nid);
829 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
830 uinfo->count = chs == 3 ? 2 : 1;
831 uinfo->value.integer.min = 0;
832 uinfo->value.integer.max = caps;
836 int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
837 struct snd_ctl_elem_value *ucontrol)
839 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
840 hda_nid_t nid = get_amp_nid(kcontrol);
841 int chs = get_amp_channels(kcontrol);
842 int dir = get_amp_direction(kcontrol);
843 int idx = get_amp_index(kcontrol);
844 long *valp = ucontrol->value.integer.value;
847 *valp++ = snd_hda_codec_amp_read(codec, nid, 0, dir, idx) & 0x7f;
849 *valp = snd_hda_codec_amp_read(codec, nid, 1, dir, idx) & 0x7f;
853 int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
854 struct snd_ctl_elem_value *ucontrol)
856 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
857 hda_nid_t nid = get_amp_nid(kcontrol);
858 int chs = get_amp_channels(kcontrol);
859 int dir = get_amp_direction(kcontrol);
860 int idx = get_amp_index(kcontrol);
861 long *valp = ucontrol->value.integer.value;
865 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
870 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
875 int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
876 unsigned int size, unsigned int __user *_tlv)
878 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
879 hda_nid_t nid = get_amp_nid(kcontrol);
880 int dir = get_amp_direction(kcontrol);
881 u32 caps, val1, val2;
883 if (size < 4 * sizeof(unsigned int))
885 caps = query_amp_caps(codec, nid, dir);
886 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
887 val2 = (val2 + 1) * 25;
888 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
889 val1 = ((int)val1) * ((int)val2);
890 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
892 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
894 if (put_user(val1, _tlv + 2))
896 if (put_user(val2, _tlv + 3))
902 int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
903 struct snd_ctl_elem_info *uinfo)
905 int chs = get_amp_channels(kcontrol);
907 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
908 uinfo->count = chs == 3 ? 2 : 1;
909 uinfo->value.integer.min = 0;
910 uinfo->value.integer.max = 1;
914 int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
915 struct snd_ctl_elem_value *ucontrol)
917 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
918 hda_nid_t nid = get_amp_nid(kcontrol);
919 int chs = get_amp_channels(kcontrol);
920 int dir = get_amp_direction(kcontrol);
921 int idx = get_amp_index(kcontrol);
922 long *valp = ucontrol->value.integer.value;
925 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
928 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
933 int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
934 struct snd_ctl_elem_value *ucontrol)
936 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
937 hda_nid_t nid = get_amp_nid(kcontrol);
938 int chs = get_amp_channels(kcontrol);
939 int dir = get_amp_direction(kcontrol);
940 int idx = get_amp_index(kcontrol);
941 long *valp = ucontrol->value.integer.value;
945 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
946 0x80, *valp ? 0 : 0x80);
950 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
951 0x80, *valp ? 0 : 0x80);
957 * bound volume controls
959 * bind multiple volumes (# indices, from 0)
962 #define AMP_VAL_IDX_SHIFT 19
963 #define AMP_VAL_IDX_MASK (0x0f<<19)
965 int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
966 struct snd_ctl_elem_value *ucontrol)
968 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
972 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
973 pval = kcontrol->private_value;
974 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
975 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
976 kcontrol->private_value = pval;
977 mutex_unlock(&codec->spdif_mutex);
981 int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
982 struct snd_ctl_elem_value *ucontrol)
984 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
986 int i, indices, err = 0, change = 0;
988 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
989 pval = kcontrol->private_value;
990 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
991 for (i = 0; i < indices; i++) {
992 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
993 (i << AMP_VAL_IDX_SHIFT);
994 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
999 kcontrol->private_value = pval;
1000 mutex_unlock(&codec->spdif_mutex);
1001 return err < 0 ? err : change;
1005 * SPDIF out controls
1008 static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
1009 struct snd_ctl_elem_info *uinfo)
1011 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1016 static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
1017 struct snd_ctl_elem_value *ucontrol)
1019 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
1020 IEC958_AES0_NONAUDIO |
1021 IEC958_AES0_CON_EMPHASIS_5015 |
1022 IEC958_AES0_CON_NOT_COPYRIGHT;
1023 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
1024 IEC958_AES1_CON_ORIGINAL;
1028 static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
1029 struct snd_ctl_elem_value *ucontrol)
1031 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
1032 IEC958_AES0_NONAUDIO |
1033 IEC958_AES0_PRO_EMPHASIS_5015;
1037 static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
1038 struct snd_ctl_elem_value *ucontrol)
1040 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1042 ucontrol->value.iec958.status[0] = codec->spdif_status & 0xff;
1043 ucontrol->value.iec958.status[1] = (codec->spdif_status >> 8) & 0xff;
1044 ucontrol->value.iec958.status[2] = (codec->spdif_status >> 16) & 0xff;
1045 ucontrol->value.iec958.status[3] = (codec->spdif_status >> 24) & 0xff;
1050 /* convert from SPDIF status bits to HDA SPDIF bits
1051 * bit 0 (DigEn) is always set zero (to be filled later)
1053 static unsigned short convert_from_spdif_status(unsigned int sbits)
1055 unsigned short val = 0;
1057 if (sbits & IEC958_AES0_PROFESSIONAL)
1058 val |= AC_DIG1_PROFESSIONAL;
1059 if (sbits & IEC958_AES0_NONAUDIO)
1060 val |= AC_DIG1_NONAUDIO;
1061 if (sbits & IEC958_AES0_PROFESSIONAL) {
1062 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
1063 IEC958_AES0_PRO_EMPHASIS_5015)
1064 val |= AC_DIG1_EMPHASIS;
1066 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
1067 IEC958_AES0_CON_EMPHASIS_5015)
1068 val |= AC_DIG1_EMPHASIS;
1069 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
1070 val |= AC_DIG1_COPYRIGHT;
1071 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
1072 val |= AC_DIG1_LEVEL;
1073 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
1078 /* convert to SPDIF status bits from HDA SPDIF bits
1080 static unsigned int convert_to_spdif_status(unsigned short val)
1082 unsigned int sbits = 0;
1084 if (val & AC_DIG1_NONAUDIO)
1085 sbits |= IEC958_AES0_NONAUDIO;
1086 if (val & AC_DIG1_PROFESSIONAL)
1087 sbits |= IEC958_AES0_PROFESSIONAL;
1088 if (sbits & IEC958_AES0_PROFESSIONAL) {
1089 if (sbits & AC_DIG1_EMPHASIS)
1090 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
1092 if (val & AC_DIG1_EMPHASIS)
1093 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
1094 if (!(val & AC_DIG1_COPYRIGHT))
1095 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
1096 if (val & AC_DIG1_LEVEL)
1097 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
1098 sbits |= val & (0x7f << 8);
1103 static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
1104 struct snd_ctl_elem_value *ucontrol)
1106 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1107 hda_nid_t nid = kcontrol->private_value;
1111 mutex_lock(&codec->spdif_mutex);
1112 codec->spdif_status = ucontrol->value.iec958.status[0] |
1113 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
1114 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
1115 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
1116 val = convert_from_spdif_status(codec->spdif_status);
1117 val |= codec->spdif_ctls & 1;
1118 change = codec->spdif_ctls != val;
1119 codec->spdif_ctls = val;
1121 if (change || codec->in_resume) {
1122 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_DIGI_CONVERT_1,
1124 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_DIGI_CONVERT_2,
1128 mutex_unlock(&codec->spdif_mutex);
1132 #define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
1134 static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
1135 struct snd_ctl_elem_value *ucontrol)
1137 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1139 ucontrol->value.integer.value[0] = codec->spdif_ctls & AC_DIG1_ENABLE;
1143 static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
1144 struct snd_ctl_elem_value *ucontrol)
1146 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1147 hda_nid_t nid = kcontrol->private_value;
1151 mutex_lock(&codec->spdif_mutex);
1152 val = codec->spdif_ctls & ~AC_DIG1_ENABLE;
1153 if (ucontrol->value.integer.value[0])
1154 val |= AC_DIG1_ENABLE;
1155 change = codec->spdif_ctls != val;
1156 if (change || codec->in_resume) {
1157 codec->spdif_ctls = val;
1158 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_DIGI_CONVERT_1,
1160 /* unmute amp switch (if any) */
1161 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
1162 (val & AC_DIG1_ENABLE))
1163 snd_hda_codec_write(codec, nid, 0,
1164 AC_VERB_SET_AMP_GAIN_MUTE,
1165 AC_AMP_SET_RIGHT | AC_AMP_SET_LEFT |
1168 mutex_unlock(&codec->spdif_mutex);
1172 static struct snd_kcontrol_new dig_mixes[] = {
1174 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1175 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1176 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,CON_MASK),
1177 .info = snd_hda_spdif_mask_info,
1178 .get = snd_hda_spdif_cmask_get,
1181 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1182 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1183 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,PRO_MASK),
1184 .info = snd_hda_spdif_mask_info,
1185 .get = snd_hda_spdif_pmask_get,
1188 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1189 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
1190 .info = snd_hda_spdif_mask_info,
1191 .get = snd_hda_spdif_default_get,
1192 .put = snd_hda_spdif_default_put,
1195 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1196 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,SWITCH),
1197 .info = snd_hda_spdif_out_switch_info,
1198 .get = snd_hda_spdif_out_switch_get,
1199 .put = snd_hda_spdif_out_switch_put,
1205 * snd_hda_create_spdif_out_ctls - create Output SPDIF-related controls
1206 * @codec: the HDA codec
1207 * @nid: audio out widget NID
1209 * Creates controls related with the SPDIF output.
1210 * Called from each patch supporting the SPDIF out.
1212 * Returns 0 if successful, or a negative error code.
1214 int __devinit snd_hda_create_spdif_out_ctls(struct hda_codec *codec,
1218 struct snd_kcontrol *kctl;
1219 struct snd_kcontrol_new *dig_mix;
1221 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
1222 kctl = snd_ctl_new1(dig_mix, codec);
1223 kctl->private_value = nid;
1224 err = snd_ctl_add(codec->bus->card, kctl);
1229 snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT, 0);
1230 codec->spdif_status = convert_to_spdif_status(codec->spdif_ctls);
1238 #define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
1240 static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
1241 struct snd_ctl_elem_value *ucontrol)
1243 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1245 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
1249 static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
1250 struct snd_ctl_elem_value *ucontrol)
1252 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1253 hda_nid_t nid = kcontrol->private_value;
1254 unsigned int val = !!ucontrol->value.integer.value[0];
1257 mutex_lock(&codec->spdif_mutex);
1258 change = codec->spdif_in_enable != val;
1259 if (change || codec->in_resume) {
1260 codec->spdif_in_enable = val;
1261 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_DIGI_CONVERT_1,
1264 mutex_unlock(&codec->spdif_mutex);
1268 static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
1269 struct snd_ctl_elem_value *ucontrol)
1271 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1272 hda_nid_t nid = kcontrol->private_value;
1276 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT, 0);
1277 sbits = convert_to_spdif_status(val);
1278 ucontrol->value.iec958.status[0] = sbits;
1279 ucontrol->value.iec958.status[1] = sbits >> 8;
1280 ucontrol->value.iec958.status[2] = sbits >> 16;
1281 ucontrol->value.iec958.status[3] = sbits >> 24;
1285 static struct snd_kcontrol_new dig_in_ctls[] = {
1287 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1288 .name = SNDRV_CTL_NAME_IEC958("",CAPTURE,SWITCH),
1289 .info = snd_hda_spdif_in_switch_info,
1290 .get = snd_hda_spdif_in_switch_get,
1291 .put = snd_hda_spdif_in_switch_put,
1294 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1295 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1296 .name = SNDRV_CTL_NAME_IEC958("",CAPTURE,DEFAULT),
1297 .info = snd_hda_spdif_mask_info,
1298 .get = snd_hda_spdif_in_status_get,
1304 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
1305 * @codec: the HDA codec
1306 * @nid: audio in widget NID
1308 * Creates controls related with the SPDIF input.
1309 * Called from each patch supporting the SPDIF in.
1311 * Returns 0 if successful, or a negative error code.
1313 int __devinit snd_hda_create_spdif_in_ctls(struct hda_codec *codec,
1317 struct snd_kcontrol *kctl;
1318 struct snd_kcontrol_new *dig_mix;
1320 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
1321 kctl = snd_ctl_new1(dig_mix, codec);
1322 kctl->private_value = nid;
1323 err = snd_ctl_add(codec->bus->card, kctl);
1327 codec->spdif_in_enable =
1328 snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT, 0) &
1335 * set power state of the codec
1337 static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
1338 unsigned int power_state)
1340 hda_nid_t nid, nid_start;
1343 snd_hda_codec_write(codec, fg, 0, AC_VERB_SET_POWER_STATE,
1346 nodes = snd_hda_get_sub_nodes(codec, fg, &nid_start);
1347 for (nid = nid_start; nid < nodes + nid_start; nid++) {
1348 if (get_wcaps(codec, nid) & AC_WCAP_POWER)
1349 snd_hda_codec_write(codec, nid, 0,
1350 AC_VERB_SET_POWER_STATE,
1354 if (power_state == AC_PWRST_D0)
1360 * snd_hda_build_controls - build mixer controls
1363 * Creates mixer controls for each codec included in the bus.
1365 * Returns 0 if successful, otherwise a negative error code.
1367 int __devinit snd_hda_build_controls(struct hda_bus *bus)
1369 struct hda_codec *codec;
1371 /* build controls */
1372 list_for_each_entry(codec, &bus->codec_list, list) {
1374 if (!codec->patch_ops.build_controls)
1376 err = codec->patch_ops.build_controls(codec);
1382 list_for_each_entry(codec, &bus->codec_list, list) {
1384 hda_set_power_state(codec,
1385 codec->afg ? codec->afg : codec->mfg,
1387 if (!codec->patch_ops.init)
1389 err = codec->patch_ops.init(codec);
1399 struct hda_rate_tbl {
1401 unsigned int alsa_bits;
1402 unsigned int hda_fmt;
1405 static struct hda_rate_tbl rate_bits[] = {
1406 /* rate in Hz, ALSA rate bitmask, HDA format value */
1408 /* autodetected value used in snd_hda_query_supported_pcm */
1409 { 8000, SNDRV_PCM_RATE_8000, 0x0500 }, /* 1/6 x 48 */
1410 { 11025, SNDRV_PCM_RATE_11025, 0x4300 }, /* 1/4 x 44 */
1411 { 16000, SNDRV_PCM_RATE_16000, 0x0200 }, /* 1/3 x 48 */
1412 { 22050, SNDRV_PCM_RATE_22050, 0x4100 }, /* 1/2 x 44 */
1413 { 32000, SNDRV_PCM_RATE_32000, 0x0a00 }, /* 2/3 x 48 */
1414 { 44100, SNDRV_PCM_RATE_44100, 0x4000 }, /* 44 */
1415 { 48000, SNDRV_PCM_RATE_48000, 0x0000 }, /* 48 */
1416 { 88200, SNDRV_PCM_RATE_88200, 0x4800 }, /* 2 x 44 */
1417 { 96000, SNDRV_PCM_RATE_96000, 0x0800 }, /* 2 x 48 */
1418 { 176400, SNDRV_PCM_RATE_176400, 0x5800 },/* 4 x 44 */
1419 { 192000, SNDRV_PCM_RATE_192000, 0x1800 }, /* 4 x 48 */
1420 #define AC_PAR_PCM_RATE_BITS 11
1421 /* up to bits 10, 384kHZ isn't supported properly */
1423 /* not autodetected value */
1424 { 9600, SNDRV_PCM_RATE_KNOT, 0x0400 }, /* 1/5 x 48 */
1426 { 0 } /* terminator */
1430 * snd_hda_calc_stream_format - calculate format bitset
1431 * @rate: the sample rate
1432 * @channels: the number of channels
1433 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
1434 * @maxbps: the max. bps
1436 * Calculate the format bitset from the given rate, channels and th PCM format.
1438 * Return zero if invalid.
1440 unsigned int snd_hda_calc_stream_format(unsigned int rate,
1441 unsigned int channels,
1442 unsigned int format,
1443 unsigned int maxbps)
1446 unsigned int val = 0;
1448 for (i = 0; rate_bits[i].hz; i++)
1449 if (rate_bits[i].hz == rate) {
1450 val = rate_bits[i].hda_fmt;
1453 if (!rate_bits[i].hz) {
1454 snd_printdd("invalid rate %d\n", rate);
1458 if (channels == 0 || channels > 8) {
1459 snd_printdd("invalid channels %d\n", channels);
1462 val |= channels - 1;
1464 switch (snd_pcm_format_width(format)) {
1465 case 8: val |= 0x00; break;
1466 case 16: val |= 0x10; break;
1472 else if (maxbps >= 24)
1478 snd_printdd("invalid format width %d\n",
1479 snd_pcm_format_width(format));
1487 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
1488 * @codec: the HDA codec
1489 * @nid: NID to query
1490 * @ratesp: the pointer to store the detected rate bitflags
1491 * @formatsp: the pointer to store the detected formats
1492 * @bpsp: the pointer to store the detected format widths
1494 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
1495 * or @bsps argument is ignored.
1497 * Returns 0 if successful, otherwise a negative error code.
1499 int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
1500 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
1503 unsigned int val, streams;
1506 if (nid != codec->afg &&
1507 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD)) {
1508 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
1513 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
1517 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
1519 rates |= rate_bits[i].alsa_bits;
1524 if (formatsp || bpsp) {
1529 wcaps = get_wcaps(codec, nid);
1530 streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
1534 streams = snd_hda_param_read(codec, codec->afg,
1541 if (streams & AC_SUPFMT_PCM) {
1542 if (val & AC_SUPPCM_BITS_8) {
1543 formats |= SNDRV_PCM_FMTBIT_U8;
1546 if (val & AC_SUPPCM_BITS_16) {
1547 formats |= SNDRV_PCM_FMTBIT_S16_LE;
1550 if (wcaps & AC_WCAP_DIGITAL) {
1551 if (val & AC_SUPPCM_BITS_32)
1552 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
1553 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
1554 formats |= SNDRV_PCM_FMTBIT_S32_LE;
1555 if (val & AC_SUPPCM_BITS_24)
1557 else if (val & AC_SUPPCM_BITS_20)
1559 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
1560 AC_SUPPCM_BITS_32)) {
1561 formats |= SNDRV_PCM_FMTBIT_S32_LE;
1562 if (val & AC_SUPPCM_BITS_32)
1564 else if (val & AC_SUPPCM_BITS_24)
1566 else if (val & AC_SUPPCM_BITS_20)
1570 else if (streams == AC_SUPFMT_FLOAT32) {
1571 /* should be exclusive */
1572 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
1574 } else if (streams == AC_SUPFMT_AC3) {
1575 /* should be exclusive */
1576 /* temporary hack: we have still no proper support
1577 * for the direct AC3 stream...
1579 formats |= SNDRV_PCM_FMTBIT_U8;
1583 *formatsp = formats;
1592 * snd_hda_is_supported_format - check whether the given node supports
1595 * Returns 1 if supported, 0 if not.
1597 int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
1598 unsigned int format)
1601 unsigned int val = 0, rate, stream;
1603 if (nid != codec->afg &&
1604 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD)) {
1605 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
1610 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
1615 rate = format & 0xff00;
1616 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
1617 if (rate_bits[i].hda_fmt == rate) {
1622 if (i >= AC_PAR_PCM_RATE_BITS)
1625 stream = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
1628 if (!stream && nid != codec->afg)
1629 stream = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
1630 if (!stream || stream == -1)
1633 if (stream & AC_SUPFMT_PCM) {
1634 switch (format & 0xf0) {
1636 if (!(val & AC_SUPPCM_BITS_8))
1640 if (!(val & AC_SUPPCM_BITS_16))
1644 if (!(val & AC_SUPPCM_BITS_20))
1648 if (!(val & AC_SUPPCM_BITS_24))
1652 if (!(val & AC_SUPPCM_BITS_32))
1659 /* FIXME: check for float32 and AC3? */
1668 static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
1669 struct hda_codec *codec,
1670 struct snd_pcm_substream *substream)
1675 static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
1676 struct hda_codec *codec,
1677 unsigned int stream_tag,
1678 unsigned int format,
1679 struct snd_pcm_substream *substream)
1681 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
1685 static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
1686 struct hda_codec *codec,
1687 struct snd_pcm_substream *substream)
1689 snd_hda_codec_setup_stream(codec, hinfo->nid, 0, 0, 0);
1693 static int __devinit set_pcm_default_values(struct hda_codec *codec,
1694 struct hda_pcm_stream *info)
1696 /* query support PCM information from the given NID */
1697 if (info->nid && (!info->rates || !info->formats)) {
1698 snd_hda_query_supported_pcm(codec, info->nid,
1699 info->rates ? NULL : &info->rates,
1700 info->formats ? NULL : &info->formats,
1701 info->maxbps ? NULL : &info->maxbps);
1703 if (info->ops.open == NULL)
1704 info->ops.open = hda_pcm_default_open_close;
1705 if (info->ops.close == NULL)
1706 info->ops.close = hda_pcm_default_open_close;
1707 if (info->ops.prepare == NULL) {
1708 snd_assert(info->nid, return -EINVAL);
1709 info->ops.prepare = hda_pcm_default_prepare;
1711 if (info->ops.cleanup == NULL) {
1712 snd_assert(info->nid, return -EINVAL);
1713 info->ops.cleanup = hda_pcm_default_cleanup;
1719 * snd_hda_build_pcms - build PCM information
1722 * Create PCM information for each codec included in the bus.
1724 * The build_pcms codec patch is requested to set up codec->num_pcms and
1725 * codec->pcm_info properly. The array is referred by the top-level driver
1726 * to create its PCM instances.
1727 * The allocated codec->pcm_info should be released in codec->patch_ops.free
1730 * At least, substreams, channels_min and channels_max must be filled for
1731 * each stream. substreams = 0 indicates that the stream doesn't exist.
1732 * When rates and/or formats are zero, the supported values are queried
1733 * from the given nid. The nid is used also by the default ops.prepare
1734 * and ops.cleanup callbacks.
1736 * The driver needs to call ops.open in its open callback. Similarly,
1737 * ops.close is supposed to be called in the close callback.
1738 * ops.prepare should be called in the prepare or hw_params callback
1739 * with the proper parameters for set up.
1740 * ops.cleanup should be called in hw_free for clean up of streams.
1742 * This function returns 0 if successfull, or a negative error code.
1744 int __devinit snd_hda_build_pcms(struct hda_bus *bus)
1746 struct hda_codec *codec;
1748 list_for_each_entry(codec, &bus->codec_list, list) {
1749 unsigned int pcm, s;
1751 if (!codec->patch_ops.build_pcms)
1753 err = codec->patch_ops.build_pcms(codec);
1756 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
1757 for (s = 0; s < 2; s++) {
1758 struct hda_pcm_stream *info;
1759 info = &codec->pcm_info[pcm].stream[s];
1760 if (!info->substreams)
1762 err = set_pcm_default_values(codec, info);
1772 * snd_hda_check_board_config - compare the current codec with the config table
1773 * @codec: the HDA codec
1774 * @num_configs: number of config enums
1775 * @models: array of model name strings
1776 * @tbl: configuration table, terminated by null entries
1778 * Compares the modelname or PCI subsystem id of the current codec with the
1779 * given configuration table. If a matching entry is found, returns its
1780 * config value (supposed to be 0 or positive).
1782 * If no entries are matching, the function returns a negative value.
1784 int __devinit snd_hda_check_board_config(struct hda_codec *codec,
1785 int num_configs, const char **models,
1786 const struct snd_pci_quirk *tbl)
1788 if (codec->bus->modelname && models) {
1790 for (i = 0; i < num_configs; i++) {
1792 !strcmp(codec->bus->modelname, models[i])) {
1793 snd_printd(KERN_INFO "hda_codec: model '%s' is "
1794 "selected\n", models[i]);
1800 if (!codec->bus->pci || !tbl)
1803 tbl = snd_pci_quirk_lookup(codec->bus->pci, tbl);
1806 if (tbl->value >= 0 && tbl->value < num_configs) {
1807 #ifdef CONFIG_SND_DEBUG_DETECT
1809 const char *model = NULL;
1811 model = models[tbl->value];
1813 sprintf(tmp, "#%d", tbl->value);
1816 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
1817 "for config %x:%x (%s)\n",
1818 model, tbl->subvendor, tbl->subdevice,
1819 (tbl->name ? tbl->name : "Unknown device"));
1827 * snd_hda_add_new_ctls - create controls from the array
1828 * @codec: the HDA codec
1829 * @knew: the array of struct snd_kcontrol_new
1831 * This helper function creates and add new controls in the given array.
1832 * The array must be terminated with an empty entry as terminator.
1834 * Returns 0 if successful, or a negative error code.
1836 int __devinit snd_hda_add_new_ctls(struct hda_codec *codec,
1837 struct snd_kcontrol_new *knew)
1841 for (; knew->name; knew++) {
1842 struct snd_kcontrol *kctl;
1843 kctl = snd_ctl_new1(knew, codec);
1846 err = snd_ctl_add(codec->bus->card, kctl);
1850 kctl = snd_ctl_new1(knew, codec);
1853 kctl->id.device = codec->addr;
1854 err = snd_ctl_add(codec->bus->card, kctl);
1864 * Channel mode helper
1866 int snd_hda_ch_mode_info(struct hda_codec *codec,
1867 struct snd_ctl_elem_info *uinfo,
1868 const struct hda_channel_mode *chmode,
1871 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1873 uinfo->value.enumerated.items = num_chmodes;
1874 if (uinfo->value.enumerated.item >= num_chmodes)
1875 uinfo->value.enumerated.item = num_chmodes - 1;
1876 sprintf(uinfo->value.enumerated.name, "%dch",
1877 chmode[uinfo->value.enumerated.item].channels);
1881 int snd_hda_ch_mode_get(struct hda_codec *codec,
1882 struct snd_ctl_elem_value *ucontrol,
1883 const struct hda_channel_mode *chmode,
1889 for (i = 0; i < num_chmodes; i++) {
1890 if (max_channels == chmode[i].channels) {
1891 ucontrol->value.enumerated.item[0] = i;
1898 int snd_hda_ch_mode_put(struct hda_codec *codec,
1899 struct snd_ctl_elem_value *ucontrol,
1900 const struct hda_channel_mode *chmode,
1906 mode = ucontrol->value.enumerated.item[0];
1907 snd_assert(mode < num_chmodes, return -EINVAL);
1908 if (*max_channelsp == chmode[mode].channels && !codec->in_resume)
1910 /* change the current channel setting */
1911 *max_channelsp = chmode[mode].channels;
1912 if (chmode[mode].sequence)
1913 snd_hda_sequence_write(codec, chmode[mode].sequence);
1920 int snd_hda_input_mux_info(const struct hda_input_mux *imux,
1921 struct snd_ctl_elem_info *uinfo)
1925 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1927 uinfo->value.enumerated.items = imux->num_items;
1928 index = uinfo->value.enumerated.item;
1929 if (index >= imux->num_items)
1930 index = imux->num_items - 1;
1931 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
1935 int snd_hda_input_mux_put(struct hda_codec *codec,
1936 const struct hda_input_mux *imux,
1937 struct snd_ctl_elem_value *ucontrol,
1939 unsigned int *cur_val)
1943 idx = ucontrol->value.enumerated.item[0];
1944 if (idx >= imux->num_items)
1945 idx = imux->num_items - 1;
1946 if (*cur_val == idx && !codec->in_resume)
1948 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
1949 imux->items[idx].index);
1956 * Multi-channel / digital-out PCM helper functions
1959 /* setup SPDIF output stream */
1960 static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
1961 unsigned int stream_tag, unsigned int format)
1963 /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */
1964 if (codec->spdif_ctls & AC_DIG1_ENABLE)
1965 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_DIGI_CONVERT_1,
1966 codec->spdif_ctls & ~AC_DIG1_ENABLE & 0xff);
1967 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
1968 /* turn on again (if needed) */
1969 if (codec->spdif_ctls & AC_DIG1_ENABLE)
1970 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_DIGI_CONVERT_1,
1971 codec->spdif_ctls & 0xff);
1975 * open the digital out in the exclusive mode
1977 int snd_hda_multi_out_dig_open(struct hda_codec *codec,
1978 struct hda_multi_out *mout)
1980 mutex_lock(&codec->spdif_mutex);
1981 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
1982 /* already opened as analog dup; reset it once */
1983 snd_hda_codec_setup_stream(codec, mout->dig_out_nid, 0, 0, 0);
1984 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
1985 mutex_unlock(&codec->spdif_mutex);
1989 int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
1990 struct hda_multi_out *mout,
1991 unsigned int stream_tag,
1992 unsigned int format,
1993 struct snd_pcm_substream *substream)
1995 mutex_lock(&codec->spdif_mutex);
1996 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
1997 mutex_unlock(&codec->spdif_mutex);
2002 * release the digital out
2004 int snd_hda_multi_out_dig_close(struct hda_codec *codec,
2005 struct hda_multi_out *mout)
2007 mutex_lock(&codec->spdif_mutex);
2008 mout->dig_out_used = 0;
2009 mutex_unlock(&codec->spdif_mutex);
2014 * set up more restrictions for analog out
2016 int snd_hda_multi_out_analog_open(struct hda_codec *codec,
2017 struct hda_multi_out *mout,
2018 struct snd_pcm_substream *substream)
2020 substream->runtime->hw.channels_max = mout->max_channels;
2021 return snd_pcm_hw_constraint_step(substream->runtime, 0,
2022 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
2026 * set up the i/o for analog out
2027 * when the digital out is available, copy the front out to digital out, too.
2029 int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
2030 struct hda_multi_out *mout,
2031 unsigned int stream_tag,
2032 unsigned int format,
2033 struct snd_pcm_substream *substream)
2035 hda_nid_t *nids = mout->dac_nids;
2036 int chs = substream->runtime->channels;
2039 mutex_lock(&codec->spdif_mutex);
2040 if (mout->dig_out_nid && mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
2042 snd_hda_is_supported_format(codec, mout->dig_out_nid,
2044 !(codec->spdif_status & IEC958_AES0_NONAUDIO)) {
2045 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
2046 setup_dig_out_stream(codec, mout->dig_out_nid,
2047 stream_tag, format);
2049 mout->dig_out_used = 0;
2050 snd_hda_codec_setup_stream(codec, mout->dig_out_nid,
2054 mutex_unlock(&codec->spdif_mutex);
2057 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
2059 if (mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
2060 /* headphone out will just decode front left/right (stereo) */
2061 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
2063 /* extra outputs copied from front */
2064 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
2065 if (mout->extra_out_nid[i])
2066 snd_hda_codec_setup_stream(codec,
2067 mout->extra_out_nid[i],
2068 stream_tag, 0, format);
2071 for (i = 1; i < mout->num_dacs; i++) {
2072 if (chs >= (i + 1) * 2) /* independent out */
2073 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
2075 else /* copy front */
2076 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
2083 * clean up the setting for analog out
2085 int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
2086 struct hda_multi_out *mout)
2088 hda_nid_t *nids = mout->dac_nids;
2091 for (i = 0; i < mout->num_dacs; i++)
2092 snd_hda_codec_setup_stream(codec, nids[i], 0, 0, 0);
2094 snd_hda_codec_setup_stream(codec, mout->hp_nid, 0, 0, 0);
2095 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
2096 if (mout->extra_out_nid[i])
2097 snd_hda_codec_setup_stream(codec,
2098 mout->extra_out_nid[i],
2100 mutex_lock(&codec->spdif_mutex);
2101 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
2102 snd_hda_codec_setup_stream(codec, mout->dig_out_nid, 0, 0, 0);
2103 mout->dig_out_used = 0;
2105 mutex_unlock(&codec->spdif_mutex);
2110 * Helper for automatic ping configuration
2113 static int __devinit is_in_nid_list(hda_nid_t nid, hda_nid_t *list)
2115 for (; *list; list++)
2123 * Sort an associated group of pins according to their sequence numbers.
2125 static void sort_pins_by_sequence(hda_nid_t * pins, short * sequences,
2132 for (i = 0; i < num_pins; i++) {
2133 for (j = i + 1; j < num_pins; j++) {
2134 if (sequences[i] > sequences[j]) {
2136 sequences[i] = sequences[j];
2148 * Parse all pin widgets and store the useful pin nids to cfg
2150 * The number of line-outs or any primary output is stored in line_outs,
2151 * and the corresponding output pins are assigned to line_out_pins[],
2152 * in the order of front, rear, CLFE, side, ...
2154 * If more extra outputs (speaker and headphone) are found, the pins are
2155 * assisnged to hp_pins[] and speaker_pins[], respectively. If no line-out jack
2156 * is detected, one of speaker of HP pins is assigned as the primary
2157 * output, i.e. to line_out_pins[0]. So, line_outs is always positive
2158 * if any analog output exists.
2160 * The analog input pins are assigned to input_pins array.
2161 * The digital input/output pins are assigned to dig_in_pin and dig_out_pin,
2164 int __devinit snd_hda_parse_pin_def_config(struct hda_codec *codec,
2165 struct auto_pin_cfg *cfg,
2166 hda_nid_t *ignore_nids)
2168 hda_nid_t nid, nid_start;
2170 short seq, assoc_line_out, assoc_speaker;
2171 short sequences_line_out[ARRAY_SIZE(cfg->line_out_pins)];
2172 short sequences_speaker[ARRAY_SIZE(cfg->speaker_pins)];
2174 memset(cfg, 0, sizeof(*cfg));
2176 memset(sequences_line_out, 0, sizeof(sequences_line_out));
2177 memset(sequences_speaker, 0, sizeof(sequences_speaker));
2178 assoc_line_out = assoc_speaker = 0;
2180 nodes = snd_hda_get_sub_nodes(codec, codec->afg, &nid_start);
2181 for (nid = nid_start; nid < nodes + nid_start; nid++) {
2182 unsigned int wid_caps = get_wcaps(codec, nid);
2183 unsigned int wid_type =
2184 (wid_caps & AC_WCAP_TYPE) >> AC_WCAP_TYPE_SHIFT;
2185 unsigned int def_conf;
2188 /* read all default configuration for pin complex */
2189 if (wid_type != AC_WID_PIN)
2191 /* ignore the given nids (e.g. pc-beep returns error) */
2192 if (ignore_nids && is_in_nid_list(nid, ignore_nids))
2195 def_conf = snd_hda_codec_read(codec, nid, 0,
2196 AC_VERB_GET_CONFIG_DEFAULT, 0);
2197 if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE)
2199 loc = get_defcfg_location(def_conf);
2200 switch (get_defcfg_device(def_conf)) {
2201 case AC_JACK_LINE_OUT:
2202 seq = get_defcfg_sequence(def_conf);
2203 assoc = get_defcfg_association(def_conf);
2206 if (!assoc_line_out)
2207 assoc_line_out = assoc;
2208 else if (assoc_line_out != assoc)
2210 if (cfg->line_outs >= ARRAY_SIZE(cfg->line_out_pins))
2212 cfg->line_out_pins[cfg->line_outs] = nid;
2213 sequences_line_out[cfg->line_outs] = seq;
2216 case AC_JACK_SPEAKER:
2217 seq = get_defcfg_sequence(def_conf);
2218 assoc = get_defcfg_association(def_conf);
2221 if (! assoc_speaker)
2222 assoc_speaker = assoc;
2223 else if (assoc_speaker != assoc)
2225 if (cfg->speaker_outs >= ARRAY_SIZE(cfg->speaker_pins))
2227 cfg->speaker_pins[cfg->speaker_outs] = nid;
2228 sequences_speaker[cfg->speaker_outs] = seq;
2229 cfg->speaker_outs++;
2231 case AC_JACK_HP_OUT:
2232 if (cfg->hp_outs >= ARRAY_SIZE(cfg->hp_pins))
2234 cfg->hp_pins[cfg->hp_outs] = nid;
2237 case AC_JACK_MIC_IN: {
2239 if (loc == AC_JACK_LOC_FRONT) {
2240 preferred = AUTO_PIN_FRONT_MIC;
2243 preferred = AUTO_PIN_MIC;
2244 alt = AUTO_PIN_FRONT_MIC;
2246 if (!cfg->input_pins[preferred])
2247 cfg->input_pins[preferred] = nid;
2248 else if (!cfg->input_pins[alt])
2249 cfg->input_pins[alt] = nid;
2252 case AC_JACK_LINE_IN:
2253 if (loc == AC_JACK_LOC_FRONT)
2254 cfg->input_pins[AUTO_PIN_FRONT_LINE] = nid;
2256 cfg->input_pins[AUTO_PIN_LINE] = nid;
2259 cfg->input_pins[AUTO_PIN_CD] = nid;
2262 cfg->input_pins[AUTO_PIN_AUX] = nid;
2264 case AC_JACK_SPDIF_OUT:
2265 cfg->dig_out_pin = nid;
2267 case AC_JACK_SPDIF_IN:
2268 cfg->dig_in_pin = nid;
2273 /* sort by sequence */
2274 sort_pins_by_sequence(cfg->line_out_pins, sequences_line_out,
2276 sort_pins_by_sequence(cfg->speaker_pins, sequences_speaker,
2280 * FIX-UP: if no line-outs are detected, try to use speaker or HP pin
2281 * as a primary output
2283 if (!cfg->line_outs) {
2284 if (cfg->speaker_outs) {
2285 cfg->line_outs = cfg->speaker_outs;
2286 memcpy(cfg->line_out_pins, cfg->speaker_pins,
2287 sizeof(cfg->speaker_pins));
2288 cfg->speaker_outs = 0;
2289 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
2290 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
2291 } else if (cfg->hp_outs) {
2292 cfg->line_outs = cfg->hp_outs;
2293 memcpy(cfg->line_out_pins, cfg->hp_pins,
2294 sizeof(cfg->hp_pins));
2296 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
2297 cfg->line_out_type = AUTO_PIN_HP_OUT;
2301 /* Reorder the surround channels
2302 * ALSA sequence is front/surr/clfe/side
2304 * 4-ch: front/surr => OK as it is
2305 * 6-ch: front/clfe/surr
2306 * 8-ch: front/clfe/rear/side|fc
2308 switch (cfg->line_outs) {
2311 nid = cfg->line_out_pins[1];
2312 cfg->line_out_pins[1] = cfg->line_out_pins[2];
2313 cfg->line_out_pins[2] = nid;
2318 * debug prints of the parsed results
2320 snd_printd("autoconfig: line_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
2321 cfg->line_outs, cfg->line_out_pins[0], cfg->line_out_pins[1],
2322 cfg->line_out_pins[2], cfg->line_out_pins[3],
2323 cfg->line_out_pins[4]);
2324 snd_printd(" speaker_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
2325 cfg->speaker_outs, cfg->speaker_pins[0],
2326 cfg->speaker_pins[1], cfg->speaker_pins[2],
2327 cfg->speaker_pins[3], cfg->speaker_pins[4]);
2328 snd_printd(" hp_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
2329 cfg->hp_outs, cfg->hp_pins[0],
2330 cfg->hp_pins[1], cfg->hp_pins[2],
2331 cfg->hp_pins[3], cfg->hp_pins[4]);
2332 snd_printd(" inputs: mic=0x%x, fmic=0x%x, line=0x%x, fline=0x%x,"
2333 " cd=0x%x, aux=0x%x\n",
2334 cfg->input_pins[AUTO_PIN_MIC],
2335 cfg->input_pins[AUTO_PIN_FRONT_MIC],
2336 cfg->input_pins[AUTO_PIN_LINE],
2337 cfg->input_pins[AUTO_PIN_FRONT_LINE],
2338 cfg->input_pins[AUTO_PIN_CD],
2339 cfg->input_pins[AUTO_PIN_AUX]);
2344 /* labels for input pins */
2345 const char *auto_pin_cfg_labels[AUTO_PIN_LAST] = {
2346 "Mic", "Front Mic", "Line", "Front Line", "CD", "Aux"
2356 * snd_hda_suspend - suspend the codecs
2358 * @state: suspsend state
2360 * Returns 0 if successful.
2362 int snd_hda_suspend(struct hda_bus *bus, pm_message_t state)
2364 struct hda_codec *codec;
2366 /* FIXME: should handle power widget capabilities */
2367 list_for_each_entry(codec, &bus->codec_list, list) {
2368 if (codec->patch_ops.suspend)
2369 codec->patch_ops.suspend(codec, state);
2370 hda_set_power_state(codec,
2371 codec->afg ? codec->afg : codec->mfg,
2378 * snd_hda_resume - resume the codecs
2380 * @state: resume state
2382 * Returns 0 if successful.
2384 int snd_hda_resume(struct hda_bus *bus)
2386 struct hda_codec *codec;
2388 list_for_each_entry(codec, &bus->codec_list, list) {
2389 hda_set_power_state(codec,
2390 codec->afg ? codec->afg : codec->mfg,
2392 if (codec->patch_ops.resume)
2393 codec->patch_ops.resume(codec);
2399 * snd_hda_resume_ctls - resume controls in the new control list
2400 * @codec: the HDA codec
2401 * @knew: the array of struct snd_kcontrol_new
2403 * This function resumes the mixer controls in the struct snd_kcontrol_new array,
2404 * originally for snd_hda_add_new_ctls().
2405 * The array must be terminated with an empty entry as terminator.
2407 int snd_hda_resume_ctls(struct hda_codec *codec, struct snd_kcontrol_new *knew)
2409 struct snd_ctl_elem_value *val;
2411 val = kmalloc(sizeof(*val), GFP_KERNEL);
2414 codec->in_resume = 1;
2415 for (; knew->name; knew++) {
2417 count = knew->count ? knew->count : 1;
2418 for (i = 0; i < count; i++) {
2419 memset(val, 0, sizeof(*val));
2420 val->id.iface = knew->iface;
2421 val->id.device = knew->device;
2422 val->id.subdevice = knew->subdevice;
2423 strcpy(val->id.name, knew->name);
2424 val->id.index = knew->index ? knew->index : i;
2425 /* Assume that get callback reads only from cache,
2426 * not accessing to the real hardware
2428 if (snd_ctl_elem_read(codec->bus->card, val) < 0)
2430 snd_ctl_elem_write(codec->bus->card, NULL, val);
2433 codec->in_resume = 0;
2439 * snd_hda_resume_spdif_out - resume the digital out
2440 * @codec: the HDA codec
2442 int snd_hda_resume_spdif_out(struct hda_codec *codec)
2444 return snd_hda_resume_ctls(codec, dig_mixes);
2448 * snd_hda_resume_spdif_in - resume the digital in
2449 * @codec: the HDA codec
2451 int snd_hda_resume_spdif_in(struct hda_codec *codec)
2453 return snd_hda_resume_ctls(codec, dig_in_ctls);