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"
34 #include <sound/hda_hwdep.h>
38 * vendor / preset table
41 struct hda_vendor_id {
46 /* codec vendor labels */
47 static struct hda_vendor_id hda_vendor_ids[] = {
48 { 0x10ec, "Realtek" },
49 { 0x1057, "Motorola" },
51 { 0x11d4, "Analog Devices" },
52 { 0x13f6, "C-Media" },
53 { 0x14f1, "Conexant" },
54 { 0x434d, "C-Media" },
55 { 0x8384, "SigmaTel" },
60 #include "hda_patch.h"
64 * snd_hda_codec_read - send a command and get the response
65 * @codec: the HDA codec
66 * @nid: NID to send the command
67 * @direct: direct flag
68 * @verb: the verb to send
69 * @parm: the parameter for the verb
71 * Send a single command and read the corresponding response.
73 * Returns the obtained response value, or -1 for an error.
75 unsigned int snd_hda_codec_read(struct hda_codec *codec, hda_nid_t nid,
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,
139 parm = snd_hda_param_read(codec, nid, AC_PAR_NODE_COUNT);
140 *start_id = (parm >> 16) & 0x7fff;
141 return (int)(parm & 0x7fff);
145 * snd_hda_get_connections - get connection list
146 * @codec: the HDA codec
148 * @conn_list: connection list array
149 * @max_conns: max. number of connections to store
151 * Parses the connection list of the given widget and stores the list
154 * Returns the number of connections, or a negative error code.
156 int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
157 hda_nid_t *conn_list, int max_conns)
160 int i, conn_len, conns;
161 unsigned int shift, num_elems, mask;
164 snd_assert(conn_list && max_conns > 0, return -EINVAL);
166 parm = snd_hda_param_read(codec, nid, AC_PAR_CONNLIST_LEN);
167 if (parm & AC_CLIST_LONG) {
176 conn_len = parm & AC_CLIST_LENGTH;
177 mask = (1 << (shift-1)) - 1;
180 return 0; /* no connection */
183 /* single connection */
184 parm = snd_hda_codec_read(codec, nid, 0,
185 AC_VERB_GET_CONNECT_LIST, 0);
186 conn_list[0] = parm & mask;
190 /* multi connection */
193 for (i = 0; i < conn_len; i++) {
197 if (i % num_elems == 0)
198 parm = snd_hda_codec_read(codec, nid, 0,
199 AC_VERB_GET_CONNECT_LIST, i);
200 range_val = !!(parm & (1 << (shift-1))); /* ranges */
204 /* ranges between the previous and this one */
205 if (!prev_nid || prev_nid >= val) {
206 snd_printk(KERN_WARNING "hda_codec: "
207 "invalid dep_range_val %x:%x\n",
211 for (n = prev_nid + 1; n <= val; n++) {
212 if (conns >= max_conns) {
214 "Too many connections\n");
217 conn_list[conns++] = n;
220 if (conns >= max_conns) {
221 snd_printk(KERN_ERR "Too many connections\n");
224 conn_list[conns++] = val;
233 * snd_hda_queue_unsol_event - add an unsolicited event to queue
235 * @res: unsolicited event (lower 32bit of RIRB entry)
236 * @res_ex: codec addr and flags (upper 32bit or RIRB entry)
238 * Adds the given event to the queue. The events are processed in
239 * the workqueue asynchronously. Call this function in the interrupt
240 * hanlder when RIRB receives an unsolicited event.
242 * Returns 0 if successful, or a negative error code.
244 int snd_hda_queue_unsol_event(struct hda_bus *bus, u32 res, u32 res_ex)
246 struct hda_bus_unsolicited *unsol;
253 wp = (unsol->wp + 1) % HDA_UNSOL_QUEUE_SIZE;
257 unsol->queue[wp] = res;
258 unsol->queue[wp + 1] = res_ex;
260 schedule_work(&unsol->work);
266 * process queueud unsolicited events
268 static void process_unsol_events(struct work_struct *work)
270 struct hda_bus_unsolicited *unsol =
271 container_of(work, struct hda_bus_unsolicited, work);
272 struct hda_bus *bus = unsol->bus;
273 struct hda_codec *codec;
274 unsigned int rp, caddr, res;
276 while (unsol->rp != unsol->wp) {
277 rp = (unsol->rp + 1) % HDA_UNSOL_QUEUE_SIZE;
280 res = unsol->queue[rp];
281 caddr = unsol->queue[rp + 1];
282 if (!(caddr & (1 << 4))) /* no unsolicited event? */
284 codec = bus->caddr_tbl[caddr & 0x0f];
285 if (codec && codec->patch_ops.unsol_event)
286 codec->patch_ops.unsol_event(codec, res);
291 * initialize unsolicited queue
293 static int __devinit init_unsol_queue(struct hda_bus *bus)
295 struct hda_bus_unsolicited *unsol;
297 if (bus->unsol) /* already initialized */
300 unsol = kzalloc(sizeof(*unsol), GFP_KERNEL);
302 snd_printk(KERN_ERR "hda_codec: "
303 "can't allocate unsolicited queue\n");
306 INIT_WORK(&unsol->work, process_unsol_events);
315 static void snd_hda_codec_free(struct hda_codec *codec);
317 static int snd_hda_bus_free(struct hda_bus *bus)
319 struct hda_codec *codec, *n;
324 flush_scheduled_work();
327 list_for_each_entry_safe(codec, n, &bus->codec_list, list) {
328 snd_hda_codec_free(codec);
330 if (bus->ops.private_free)
331 bus->ops.private_free(bus);
336 static int snd_hda_bus_dev_free(struct snd_device *device)
338 struct hda_bus *bus = device->device_data;
339 return snd_hda_bus_free(bus);
343 * snd_hda_bus_new - create a HDA bus
344 * @card: the card entry
345 * @temp: the template for hda_bus information
346 * @busp: the pointer to store the created bus instance
348 * Returns 0 if successful, or a negative error code.
350 int __devinit snd_hda_bus_new(struct snd_card *card,
351 const struct hda_bus_template *temp,
352 struct hda_bus **busp)
356 static struct snd_device_ops dev_ops = {
357 .dev_free = snd_hda_bus_dev_free,
360 snd_assert(temp, return -EINVAL);
361 snd_assert(temp->ops.command && temp->ops.get_response, return -EINVAL);
366 bus = kzalloc(sizeof(*bus), GFP_KERNEL);
368 snd_printk(KERN_ERR "can't allocate struct hda_bus\n");
373 bus->private_data = temp->private_data;
374 bus->pci = temp->pci;
375 bus->modelname = temp->modelname;
376 bus->ops = temp->ops;
378 mutex_init(&bus->cmd_mutex);
379 INIT_LIST_HEAD(&bus->codec_list);
381 err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops);
383 snd_hda_bus_free(bus);
391 #ifdef CONFIG_SND_HDA_GENERIC
392 #define is_generic_config(codec) \
393 (codec->bus->modelname && !strcmp(codec->bus->modelname, "generic"))
395 #define is_generic_config(codec) 0
399 * find a matching codec preset
401 static const struct hda_codec_preset __devinit *
402 find_codec_preset(struct hda_codec *codec)
404 const struct hda_codec_preset **tbl, *preset;
406 if (is_generic_config(codec))
407 return NULL; /* use the generic parser */
409 for (tbl = hda_preset_tables; *tbl; tbl++) {
410 for (preset = *tbl; preset->id; preset++) {
411 u32 mask = preset->mask;
414 if (preset->id == (codec->vendor_id & mask) &&
416 preset->rev == codec->revision_id))
424 * snd_hda_get_codec_name - store the codec name
426 void snd_hda_get_codec_name(struct hda_codec *codec,
427 char *name, int namelen)
429 const struct hda_vendor_id *c;
430 const char *vendor = NULL;
431 u16 vendor_id = codec->vendor_id >> 16;
434 for (c = hda_vendor_ids; c->id; c++) {
435 if (c->id == vendor_id) {
441 sprintf(tmp, "Generic %04x", vendor_id);
444 if (codec->preset && codec->preset->name)
445 snprintf(name, namelen, "%s %s", vendor, codec->preset->name);
447 snprintf(name, namelen, "%s ID %x", vendor,
448 codec->vendor_id & 0xffff);
452 * look for an AFG and MFG nodes
454 static void __devinit setup_fg_nodes(struct hda_codec *codec)
459 total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid);
460 for (i = 0; i < total_nodes; i++, nid++) {
462 func = snd_hda_param_read(codec, nid, AC_PAR_FUNCTION_TYPE);
463 switch (func & 0xff) {
464 case AC_GRP_AUDIO_FUNCTION:
467 case AC_GRP_MODEM_FUNCTION:
477 * read widget caps for each widget and store in cache
479 static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
484 codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node,
486 codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL);
489 nid = codec->start_nid;
490 for (i = 0; i < codec->num_nodes; i++, nid++)
491 codec->wcaps[i] = snd_hda_param_read(codec, nid,
492 AC_PAR_AUDIO_WIDGET_CAP);
497 static void init_hda_cache(struct hda_cache_rec *cache,
498 unsigned int record_size);
499 static inline void free_hda_cache(struct hda_cache_rec *cache);
504 static void snd_hda_codec_free(struct hda_codec *codec)
508 list_del(&codec->list);
509 codec->bus->caddr_tbl[codec->addr] = NULL;
510 if (codec->patch_ops.free)
511 codec->patch_ops.free(codec);
512 free_hda_cache(&codec->amp_cache);
513 free_hda_cache(&codec->cmd_cache);
519 * snd_hda_codec_new - create a HDA codec
520 * @bus: the bus to assign
521 * @codec_addr: the codec address
522 * @codecp: the pointer to store the generated codec
524 * Returns 0 if successful, or a negative error code.
526 int __devinit snd_hda_codec_new(struct hda_bus *bus, unsigned int codec_addr,
527 struct hda_codec **codecp)
529 struct hda_codec *codec;
533 snd_assert(bus, return -EINVAL);
534 snd_assert(codec_addr <= HDA_MAX_CODEC_ADDRESS, return -EINVAL);
536 if (bus->caddr_tbl[codec_addr]) {
537 snd_printk(KERN_ERR "hda_codec: "
538 "address 0x%x is already occupied\n", codec_addr);
542 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
544 snd_printk(KERN_ERR "can't allocate struct hda_codec\n");
549 codec->addr = codec_addr;
550 mutex_init(&codec->spdif_mutex);
551 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
552 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
554 list_add_tail(&codec->list, &bus->codec_list);
555 bus->caddr_tbl[codec_addr] = codec;
557 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
559 if (codec->vendor_id == -1)
560 /* read again, hopefully the access method was corrected
561 * in the last read...
563 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
565 codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT,
566 AC_PAR_SUBSYSTEM_ID);
567 codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT,
570 setup_fg_nodes(codec);
571 if (!codec->afg && !codec->mfg) {
572 snd_printdd("hda_codec: no AFG or MFG node found\n");
573 snd_hda_codec_free(codec);
577 if (read_widget_caps(codec, codec->afg ? codec->afg : codec->mfg) < 0) {
578 snd_printk(KERN_ERR "hda_codec: cannot malloc\n");
579 snd_hda_codec_free(codec);
583 if (!codec->subsystem_id) {
584 hda_nid_t nid = codec->afg ? codec->afg : codec->mfg;
585 codec->subsystem_id =
586 snd_hda_codec_read(codec, nid, 0,
587 AC_VERB_GET_SUBSYSTEM_ID, 0);
590 codec->preset = find_codec_preset(codec);
591 /* audio codec should override the mixer name */
592 if (codec->afg || !*bus->card->mixername)
593 snd_hda_get_codec_name(codec, bus->card->mixername,
594 sizeof(bus->card->mixername));
596 #ifdef CONFIG_SND_HDA_GENERIC
597 if (is_generic_config(codec)) {
598 err = snd_hda_parse_generic_codec(codec);
602 if (codec->preset && codec->preset->patch) {
603 err = codec->preset->patch(codec);
607 /* call the default parser */
608 #ifdef CONFIG_SND_HDA_GENERIC
609 err = snd_hda_parse_generic_codec(codec);
611 printk(KERN_ERR "hda-codec: No codec parser is available\n");
617 snd_hda_codec_free(codec);
621 if (codec->patch_ops.unsol_event)
622 init_unsol_queue(bus);
624 snd_hda_codec_proc_new(codec);
625 #ifdef CONFIG_SND_HDA_HWDEP
626 snd_hda_create_hwdep(codec);
629 sprintf(component, "HDA:%08x", codec->vendor_id);
630 snd_component_add(codec->bus->card, component);
638 * snd_hda_codec_setup_stream - set up the codec for streaming
639 * @codec: the CODEC to set up
640 * @nid: the NID to set up
641 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
642 * @channel_id: channel id to pass, zero based.
643 * @format: stream format.
645 void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
647 int channel_id, int format)
652 snd_printdd("hda_codec_setup_stream: "
653 "NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
654 nid, stream_tag, channel_id, format);
655 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID,
656 (stream_tag << 4) | channel_id);
658 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, format);
662 * amp access functions
665 /* FIXME: more better hash key? */
666 #define HDA_HASH_KEY(nid,dir,idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
667 #define INFO_AMP_CAPS (1<<0)
668 #define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
670 /* initialize the hash table */
671 static void __devinit init_hda_cache(struct hda_cache_rec *cache,
672 unsigned int record_size)
674 memset(cache, 0, sizeof(*cache));
675 memset(cache->hash, 0xff, sizeof(cache->hash));
676 cache->record_size = record_size;
679 static inline void free_hda_cache(struct hda_cache_rec *cache)
681 kfree(cache->buffer);
684 /* query the hash. allocate an entry if not found. */
685 static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache,
688 u16 idx = key % (u16)ARRAY_SIZE(cache->hash);
689 u16 cur = cache->hash[idx];
690 struct hda_cache_head *info;
692 while (cur != 0xffff) {
693 info = (struct hda_cache_head *)(cache->buffer +
694 cur * cache->record_size);
695 if (info->key == key)
700 /* add a new hash entry */
701 if (cache->num_entries >= cache->size) {
702 /* reallocate the array */
703 unsigned int new_size = cache->size + 64;
705 new_buffer = kcalloc(new_size, cache->record_size, GFP_KERNEL);
707 snd_printk(KERN_ERR "hda_codec: "
708 "can't malloc amp_info\n");
712 memcpy(new_buffer, cache->buffer,
713 cache->size * cache->record_size);
714 kfree(cache->buffer);
716 cache->size = new_size;
717 cache->buffer = new_buffer;
719 cur = cache->num_entries++;
720 info = (struct hda_cache_head *)(cache->buffer +
721 cur * cache->record_size);
724 info->next = cache->hash[idx];
725 cache->hash[idx] = cur;
730 /* query and allocate an amp hash entry */
731 static inline struct hda_amp_info *
732 get_alloc_amp_hash(struct hda_codec *codec, u32 key)
734 return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key);
738 * query AMP capabilities for the given widget and direction
740 static u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
742 struct hda_amp_info *info;
744 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, 0));
747 if (!(info->head.val & INFO_AMP_CAPS)) {
748 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
750 info->amp_caps = snd_hda_param_read(codec, nid,
751 direction == HDA_OUTPUT ?
755 info->head.val |= INFO_AMP_CAPS;
757 return info->amp_caps;
760 int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
763 struct hda_amp_info *info;
765 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, dir, 0));
768 info->amp_caps = caps;
769 info->head.val |= INFO_AMP_CAPS;
774 * read the current volume to info
775 * if the cache exists, read the cache value.
777 static unsigned int get_vol_mute(struct hda_codec *codec,
778 struct hda_amp_info *info, hda_nid_t nid,
779 int ch, int direction, int index)
783 if (info->head.val & INFO_AMP_VOL(ch))
784 return info->vol[ch];
786 parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
787 parm |= direction == HDA_OUTPUT ? AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
789 val = snd_hda_codec_read(codec, nid, 0,
790 AC_VERB_GET_AMP_GAIN_MUTE, parm);
791 info->vol[ch] = val & 0xff;
792 info->head.val |= INFO_AMP_VOL(ch);
793 return info->vol[ch];
797 * write the current volume in info to the h/w and update the cache
799 static void put_vol_mute(struct hda_codec *codec, struct hda_amp_info *info,
800 hda_nid_t nid, int ch, int direction, int index,
805 parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
806 parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
807 parm |= index << AC_AMP_SET_INDEX_SHIFT;
809 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
814 * read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
816 int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
817 int direction, int index)
819 struct hda_amp_info *info;
820 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
823 return get_vol_mute(codec, info, nid, ch, direction, index);
827 * update the AMP value, mask = bit mask to set, val = the value
829 int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
830 int direction, int idx, int mask, int val)
832 struct hda_amp_info *info;
834 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, idx));
838 val |= get_vol_mute(codec, info, nid, ch, direction, idx) & ~mask;
839 if (info->vol[ch] == val && !codec->in_resume)
841 put_vol_mute(codec, info, nid, ch, direction, idx, val);
845 /* resume the all amp commands from the cache */
846 void snd_hda_codec_resume_amp(struct hda_codec *codec)
848 struct hda_amp_info *buffer = codec->amp_cache.buffer;
851 for (i = 0; i < codec->amp_cache.size; i++, buffer++) {
852 u32 key = buffer->head.key;
854 unsigned int idx, dir, ch;
858 idx = (key >> 16) & 0xff;
859 dir = (key >> 24) & 0xff;
860 for (ch = 0; ch < 2; ch++) {
861 if (!(buffer->head.val & INFO_AMP_VOL(ch)))
863 put_vol_mute(codec, buffer, nid, ch, dir, idx,
870 * AMP control callbacks
872 /* retrieve parameters from private_value */
873 #define get_amp_nid(kc) ((kc)->private_value & 0xffff)
874 #define get_amp_channels(kc) (((kc)->private_value >> 16) & 0x3)
875 #define get_amp_direction(kc) (((kc)->private_value >> 18) & 0x1)
876 #define get_amp_index(kc) (((kc)->private_value >> 19) & 0xf)
879 int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
880 struct snd_ctl_elem_info *uinfo)
882 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
883 u16 nid = get_amp_nid(kcontrol);
884 u8 chs = get_amp_channels(kcontrol);
885 int dir = get_amp_direction(kcontrol);
888 caps = query_amp_caps(codec, nid, dir);
890 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
892 printk(KERN_WARNING "hda_codec: "
893 "num_steps = 0 for NID=0x%x\n", nid);
896 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
897 uinfo->count = chs == 3 ? 2 : 1;
898 uinfo->value.integer.min = 0;
899 uinfo->value.integer.max = caps;
903 int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
904 struct snd_ctl_elem_value *ucontrol)
906 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
907 hda_nid_t nid = get_amp_nid(kcontrol);
908 int chs = get_amp_channels(kcontrol);
909 int dir = get_amp_direction(kcontrol);
910 int idx = get_amp_index(kcontrol);
911 long *valp = ucontrol->value.integer.value;
914 *valp++ = snd_hda_codec_amp_read(codec, nid, 0, dir, idx) & 0x7f;
916 *valp = snd_hda_codec_amp_read(codec, nid, 1, dir, idx) & 0x7f;
920 int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
921 struct snd_ctl_elem_value *ucontrol)
923 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
924 hda_nid_t nid = get_amp_nid(kcontrol);
925 int chs = get_amp_channels(kcontrol);
926 int dir = get_amp_direction(kcontrol);
927 int idx = get_amp_index(kcontrol);
928 long *valp = ucontrol->value.integer.value;
932 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
937 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
942 int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
943 unsigned int size, unsigned int __user *_tlv)
945 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
946 hda_nid_t nid = get_amp_nid(kcontrol);
947 int dir = get_amp_direction(kcontrol);
948 u32 caps, val1, val2;
950 if (size < 4 * sizeof(unsigned int))
952 caps = query_amp_caps(codec, nid, dir);
953 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
954 val2 = (val2 + 1) * 25;
955 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
956 val1 = ((int)val1) * ((int)val2);
957 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
959 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
961 if (put_user(val1, _tlv + 2))
963 if (put_user(val2, _tlv + 3))
969 int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
970 struct snd_ctl_elem_info *uinfo)
972 int chs = get_amp_channels(kcontrol);
974 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
975 uinfo->count = chs == 3 ? 2 : 1;
976 uinfo->value.integer.min = 0;
977 uinfo->value.integer.max = 1;
981 int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
982 struct snd_ctl_elem_value *ucontrol)
984 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
985 hda_nid_t nid = get_amp_nid(kcontrol);
986 int chs = get_amp_channels(kcontrol);
987 int dir = get_amp_direction(kcontrol);
988 int idx = get_amp_index(kcontrol);
989 long *valp = ucontrol->value.integer.value;
992 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
995 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
1000 int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
1001 struct snd_ctl_elem_value *ucontrol)
1003 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1004 hda_nid_t nid = get_amp_nid(kcontrol);
1005 int chs = get_amp_channels(kcontrol);
1006 int dir = get_amp_direction(kcontrol);
1007 int idx = get_amp_index(kcontrol);
1008 long *valp = ucontrol->value.integer.value;
1012 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
1013 0x80, *valp ? 0 : 0x80);
1017 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
1018 0x80, *valp ? 0 : 0x80);
1024 * bound volume controls
1026 * bind multiple volumes (# indices, from 0)
1029 #define AMP_VAL_IDX_SHIFT 19
1030 #define AMP_VAL_IDX_MASK (0x0f<<19)
1032 int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
1033 struct snd_ctl_elem_value *ucontrol)
1035 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1039 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
1040 pval = kcontrol->private_value;
1041 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
1042 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
1043 kcontrol->private_value = pval;
1044 mutex_unlock(&codec->spdif_mutex);
1048 int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
1049 struct snd_ctl_elem_value *ucontrol)
1051 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1053 int i, indices, err = 0, change = 0;
1055 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
1056 pval = kcontrol->private_value;
1057 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
1058 for (i = 0; i < indices; i++) {
1059 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
1060 (i << AMP_VAL_IDX_SHIFT);
1061 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
1066 kcontrol->private_value = pval;
1067 mutex_unlock(&codec->spdif_mutex);
1068 return err < 0 ? err : change;
1072 * generic bound volume/swtich controls
1074 int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
1075 struct snd_ctl_elem_info *uinfo)
1077 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1078 struct hda_bind_ctls *c;
1081 c = (struct hda_bind_ctls *)kcontrol->private_value;
1082 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
1083 kcontrol->private_value = *c->values;
1084 err = c->ops->info(kcontrol, uinfo);
1085 kcontrol->private_value = (long)c;
1086 mutex_unlock(&codec->spdif_mutex);
1090 int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
1091 struct snd_ctl_elem_value *ucontrol)
1093 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1094 struct hda_bind_ctls *c;
1097 c = (struct hda_bind_ctls *)kcontrol->private_value;
1098 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
1099 kcontrol->private_value = *c->values;
1100 err = c->ops->get(kcontrol, ucontrol);
1101 kcontrol->private_value = (long)c;
1102 mutex_unlock(&codec->spdif_mutex);
1106 int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
1107 struct snd_ctl_elem_value *ucontrol)
1109 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1110 struct hda_bind_ctls *c;
1111 unsigned long *vals;
1112 int err = 0, change = 0;
1114 c = (struct hda_bind_ctls *)kcontrol->private_value;
1115 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
1116 for (vals = c->values; *vals; vals++) {
1117 kcontrol->private_value = *vals;
1118 err = c->ops->put(kcontrol, ucontrol);
1123 kcontrol->private_value = (long)c;
1124 mutex_unlock(&codec->spdif_mutex);
1125 return err < 0 ? err : change;
1128 int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
1129 unsigned int size, unsigned int __user *tlv)
1131 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1132 struct hda_bind_ctls *c;
1135 c = (struct hda_bind_ctls *)kcontrol->private_value;
1136 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
1137 kcontrol->private_value = *c->values;
1138 err = c->ops->tlv(kcontrol, op_flag, size, tlv);
1139 kcontrol->private_value = (long)c;
1140 mutex_unlock(&codec->spdif_mutex);
1144 struct hda_ctl_ops snd_hda_bind_vol = {
1145 .info = snd_hda_mixer_amp_volume_info,
1146 .get = snd_hda_mixer_amp_volume_get,
1147 .put = snd_hda_mixer_amp_volume_put,
1148 .tlv = snd_hda_mixer_amp_tlv
1151 struct hda_ctl_ops snd_hda_bind_sw = {
1152 .info = snd_hda_mixer_amp_switch_info,
1153 .get = snd_hda_mixer_amp_switch_get,
1154 .put = snd_hda_mixer_amp_switch_put,
1155 .tlv = snd_hda_mixer_amp_tlv
1159 * SPDIF out controls
1162 static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
1163 struct snd_ctl_elem_info *uinfo)
1165 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1170 static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
1171 struct snd_ctl_elem_value *ucontrol)
1173 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
1174 IEC958_AES0_NONAUDIO |
1175 IEC958_AES0_CON_EMPHASIS_5015 |
1176 IEC958_AES0_CON_NOT_COPYRIGHT;
1177 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
1178 IEC958_AES1_CON_ORIGINAL;
1182 static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
1183 struct snd_ctl_elem_value *ucontrol)
1185 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
1186 IEC958_AES0_NONAUDIO |
1187 IEC958_AES0_PRO_EMPHASIS_5015;
1191 static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
1192 struct snd_ctl_elem_value *ucontrol)
1194 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1196 ucontrol->value.iec958.status[0] = codec->spdif_status & 0xff;
1197 ucontrol->value.iec958.status[1] = (codec->spdif_status >> 8) & 0xff;
1198 ucontrol->value.iec958.status[2] = (codec->spdif_status >> 16) & 0xff;
1199 ucontrol->value.iec958.status[3] = (codec->spdif_status >> 24) & 0xff;
1204 /* convert from SPDIF status bits to HDA SPDIF bits
1205 * bit 0 (DigEn) is always set zero (to be filled later)
1207 static unsigned short convert_from_spdif_status(unsigned int sbits)
1209 unsigned short val = 0;
1211 if (sbits & IEC958_AES0_PROFESSIONAL)
1212 val |= AC_DIG1_PROFESSIONAL;
1213 if (sbits & IEC958_AES0_NONAUDIO)
1214 val |= AC_DIG1_NONAUDIO;
1215 if (sbits & IEC958_AES0_PROFESSIONAL) {
1216 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
1217 IEC958_AES0_PRO_EMPHASIS_5015)
1218 val |= AC_DIG1_EMPHASIS;
1220 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
1221 IEC958_AES0_CON_EMPHASIS_5015)
1222 val |= AC_DIG1_EMPHASIS;
1223 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
1224 val |= AC_DIG1_COPYRIGHT;
1225 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
1226 val |= AC_DIG1_LEVEL;
1227 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
1232 /* convert to SPDIF status bits from HDA SPDIF bits
1234 static unsigned int convert_to_spdif_status(unsigned short val)
1236 unsigned int sbits = 0;
1238 if (val & AC_DIG1_NONAUDIO)
1239 sbits |= IEC958_AES0_NONAUDIO;
1240 if (val & AC_DIG1_PROFESSIONAL)
1241 sbits |= IEC958_AES0_PROFESSIONAL;
1242 if (sbits & IEC958_AES0_PROFESSIONAL) {
1243 if (sbits & AC_DIG1_EMPHASIS)
1244 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
1246 if (val & AC_DIG1_EMPHASIS)
1247 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
1248 if (!(val & AC_DIG1_COPYRIGHT))
1249 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
1250 if (val & AC_DIG1_LEVEL)
1251 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
1252 sbits |= val & (0x7f << 8);
1257 static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
1258 struct snd_ctl_elem_value *ucontrol)
1260 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1261 hda_nid_t nid = kcontrol->private_value;
1265 mutex_lock(&codec->spdif_mutex);
1266 codec->spdif_status = ucontrol->value.iec958.status[0] |
1267 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
1268 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
1269 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
1270 val = convert_from_spdif_status(codec->spdif_status);
1271 val |= codec->spdif_ctls & 1;
1272 change = codec->spdif_ctls != val;
1273 codec->spdif_ctls = val;
1275 if (change || codec->in_resume) {
1276 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_DIGI_CONVERT_1,
1278 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_DIGI_CONVERT_2,
1282 mutex_unlock(&codec->spdif_mutex);
1286 #define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
1288 static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
1289 struct snd_ctl_elem_value *ucontrol)
1291 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1293 ucontrol->value.integer.value[0] = codec->spdif_ctls & AC_DIG1_ENABLE;
1297 static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
1298 struct snd_ctl_elem_value *ucontrol)
1300 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1301 hda_nid_t nid = kcontrol->private_value;
1305 mutex_lock(&codec->spdif_mutex);
1306 val = codec->spdif_ctls & ~AC_DIG1_ENABLE;
1307 if (ucontrol->value.integer.value[0])
1308 val |= AC_DIG1_ENABLE;
1309 change = codec->spdif_ctls != val;
1310 if (change || codec->in_resume) {
1311 codec->spdif_ctls = val;
1312 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_DIGI_CONVERT_1,
1314 /* unmute amp switch (if any) */
1315 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
1316 (val & AC_DIG1_ENABLE))
1317 snd_hda_codec_write(codec, nid, 0,
1318 AC_VERB_SET_AMP_GAIN_MUTE,
1319 AC_AMP_SET_RIGHT | AC_AMP_SET_LEFT |
1322 mutex_unlock(&codec->spdif_mutex);
1326 static struct snd_kcontrol_new dig_mixes[] = {
1328 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1329 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1330 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,CON_MASK),
1331 .info = snd_hda_spdif_mask_info,
1332 .get = snd_hda_spdif_cmask_get,
1335 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1336 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1337 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,PRO_MASK),
1338 .info = snd_hda_spdif_mask_info,
1339 .get = snd_hda_spdif_pmask_get,
1342 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1343 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
1344 .info = snd_hda_spdif_mask_info,
1345 .get = snd_hda_spdif_default_get,
1346 .put = snd_hda_spdif_default_put,
1349 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1350 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,SWITCH),
1351 .info = snd_hda_spdif_out_switch_info,
1352 .get = snd_hda_spdif_out_switch_get,
1353 .put = snd_hda_spdif_out_switch_put,
1359 * snd_hda_create_spdif_out_ctls - create Output SPDIF-related controls
1360 * @codec: the HDA codec
1361 * @nid: audio out widget NID
1363 * Creates controls related with the SPDIF output.
1364 * Called from each patch supporting the SPDIF out.
1366 * Returns 0 if successful, or a negative error code.
1368 int snd_hda_create_spdif_out_ctls(struct hda_codec *codec, hda_nid_t nid)
1371 struct snd_kcontrol *kctl;
1372 struct snd_kcontrol_new *dig_mix;
1374 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
1375 kctl = snd_ctl_new1(dig_mix, codec);
1376 kctl->private_value = nid;
1377 err = snd_ctl_add(codec->bus->card, kctl);
1382 snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT, 0);
1383 codec->spdif_status = convert_to_spdif_status(codec->spdif_ctls);
1391 #define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
1393 static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
1394 struct snd_ctl_elem_value *ucontrol)
1396 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1398 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
1402 static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
1403 struct snd_ctl_elem_value *ucontrol)
1405 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1406 hda_nid_t nid = kcontrol->private_value;
1407 unsigned int val = !!ucontrol->value.integer.value[0];
1410 mutex_lock(&codec->spdif_mutex);
1411 change = codec->spdif_in_enable != val;
1412 if (change || codec->in_resume) {
1413 codec->spdif_in_enable = val;
1414 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_DIGI_CONVERT_1,
1417 mutex_unlock(&codec->spdif_mutex);
1421 static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
1422 struct snd_ctl_elem_value *ucontrol)
1424 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1425 hda_nid_t nid = kcontrol->private_value;
1429 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT, 0);
1430 sbits = convert_to_spdif_status(val);
1431 ucontrol->value.iec958.status[0] = sbits;
1432 ucontrol->value.iec958.status[1] = sbits >> 8;
1433 ucontrol->value.iec958.status[2] = sbits >> 16;
1434 ucontrol->value.iec958.status[3] = sbits >> 24;
1438 static struct snd_kcontrol_new dig_in_ctls[] = {
1440 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1441 .name = SNDRV_CTL_NAME_IEC958("",CAPTURE,SWITCH),
1442 .info = snd_hda_spdif_in_switch_info,
1443 .get = snd_hda_spdif_in_switch_get,
1444 .put = snd_hda_spdif_in_switch_put,
1447 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1448 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1449 .name = SNDRV_CTL_NAME_IEC958("",CAPTURE,DEFAULT),
1450 .info = snd_hda_spdif_mask_info,
1451 .get = snd_hda_spdif_in_status_get,
1457 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
1458 * @codec: the HDA codec
1459 * @nid: audio in widget NID
1461 * Creates controls related with the SPDIF input.
1462 * Called from each patch supporting the SPDIF in.
1464 * Returns 0 if successful, or a negative error code.
1466 int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
1469 struct snd_kcontrol *kctl;
1470 struct snd_kcontrol_new *dig_mix;
1472 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
1473 kctl = snd_ctl_new1(dig_mix, codec);
1474 kctl->private_value = nid;
1475 err = snd_ctl_add(codec->bus->card, kctl);
1479 codec->spdif_in_enable =
1480 snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT, 0) &
1486 /* build a 32bit cache key with the widget id and the command parameter */
1487 #define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
1488 #define get_cmd_cache_nid(key) ((key) & 0xff)
1489 #define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
1492 * snd_hda_codec_write_cache - send a single command with caching
1493 * @codec: the HDA codec
1494 * @nid: NID to send the command
1495 * @direct: direct flag
1496 * @verb: the verb to send
1497 * @parm: the parameter for the verb
1499 * Send a single command without waiting for response.
1501 * Returns 0 if successful, or a negative error code.
1503 int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
1504 int direct, unsigned int verb, unsigned int parm)
1507 mutex_lock(&codec->bus->cmd_mutex);
1508 err = codec->bus->ops.command(codec, nid, direct, verb, parm);
1510 struct hda_cache_head *c;
1511 u32 key = build_cmd_cache_key(nid, verb);
1512 c = get_alloc_hash(&codec->cmd_cache, key);
1516 mutex_unlock(&codec->bus->cmd_mutex);
1520 /* resume the all commands from the cache */
1521 void snd_hda_codec_resume_cache(struct hda_codec *codec)
1523 struct hda_cache_head *buffer = codec->cmd_cache.buffer;
1526 for (i = 0; i < codec->cmd_cache.size; i++, buffer++) {
1527 u32 key = buffer->key;
1530 snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
1531 get_cmd_cache_cmd(key), buffer->val);
1536 * snd_hda_sequence_write_cache - sequence writes with caching
1537 * @codec: the HDA codec
1538 * @seq: VERB array to send
1540 * Send the commands sequentially from the given array.
1541 * Thte commands are recorded on cache for power-save and resume.
1542 * The array must be terminated with NID=0.
1544 void snd_hda_sequence_write_cache(struct hda_codec *codec,
1545 const struct hda_verb *seq)
1547 for (; seq->nid; seq++)
1548 snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
1553 * set power state of the codec
1555 static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
1556 unsigned int power_state)
1558 hda_nid_t nid, nid_start;
1561 snd_hda_codec_write(codec, fg, 0, AC_VERB_SET_POWER_STATE,
1564 nodes = snd_hda_get_sub_nodes(codec, fg, &nid_start);
1565 for (nid = nid_start; nid < nodes + nid_start; nid++) {
1566 if (get_wcaps(codec, nid) & AC_WCAP_POWER)
1567 snd_hda_codec_write(codec, nid, 0,
1568 AC_VERB_SET_POWER_STATE,
1572 if (power_state == AC_PWRST_D0)
1578 * snd_hda_build_controls - build mixer controls
1581 * Creates mixer controls for each codec included in the bus.
1583 * Returns 0 if successful, otherwise a negative error code.
1585 int __devinit snd_hda_build_controls(struct hda_bus *bus)
1587 struct hda_codec *codec;
1589 /* build controls */
1590 list_for_each_entry(codec, &bus->codec_list, list) {
1592 if (!codec->patch_ops.build_controls)
1594 err = codec->patch_ops.build_controls(codec);
1600 list_for_each_entry(codec, &bus->codec_list, list) {
1602 hda_set_power_state(codec,
1603 codec->afg ? codec->afg : codec->mfg,
1605 if (!codec->patch_ops.init)
1607 err = codec->patch_ops.init(codec);
1617 struct hda_rate_tbl {
1619 unsigned int alsa_bits;
1620 unsigned int hda_fmt;
1623 static struct hda_rate_tbl rate_bits[] = {
1624 /* rate in Hz, ALSA rate bitmask, HDA format value */
1626 /* autodetected value used in snd_hda_query_supported_pcm */
1627 { 8000, SNDRV_PCM_RATE_8000, 0x0500 }, /* 1/6 x 48 */
1628 { 11025, SNDRV_PCM_RATE_11025, 0x4300 }, /* 1/4 x 44 */
1629 { 16000, SNDRV_PCM_RATE_16000, 0x0200 }, /* 1/3 x 48 */
1630 { 22050, SNDRV_PCM_RATE_22050, 0x4100 }, /* 1/2 x 44 */
1631 { 32000, SNDRV_PCM_RATE_32000, 0x0a00 }, /* 2/3 x 48 */
1632 { 44100, SNDRV_PCM_RATE_44100, 0x4000 }, /* 44 */
1633 { 48000, SNDRV_PCM_RATE_48000, 0x0000 }, /* 48 */
1634 { 88200, SNDRV_PCM_RATE_88200, 0x4800 }, /* 2 x 44 */
1635 { 96000, SNDRV_PCM_RATE_96000, 0x0800 }, /* 2 x 48 */
1636 { 176400, SNDRV_PCM_RATE_176400, 0x5800 },/* 4 x 44 */
1637 { 192000, SNDRV_PCM_RATE_192000, 0x1800 }, /* 4 x 48 */
1638 #define AC_PAR_PCM_RATE_BITS 11
1639 /* up to bits 10, 384kHZ isn't supported properly */
1641 /* not autodetected value */
1642 { 9600, SNDRV_PCM_RATE_KNOT, 0x0400 }, /* 1/5 x 48 */
1644 { 0 } /* terminator */
1648 * snd_hda_calc_stream_format - calculate format bitset
1649 * @rate: the sample rate
1650 * @channels: the number of channels
1651 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
1652 * @maxbps: the max. bps
1654 * Calculate the format bitset from the given rate, channels and th PCM format.
1656 * Return zero if invalid.
1658 unsigned int snd_hda_calc_stream_format(unsigned int rate,
1659 unsigned int channels,
1660 unsigned int format,
1661 unsigned int maxbps)
1664 unsigned int val = 0;
1666 for (i = 0; rate_bits[i].hz; i++)
1667 if (rate_bits[i].hz == rate) {
1668 val = rate_bits[i].hda_fmt;
1671 if (!rate_bits[i].hz) {
1672 snd_printdd("invalid rate %d\n", rate);
1676 if (channels == 0 || channels > 8) {
1677 snd_printdd("invalid channels %d\n", channels);
1680 val |= channels - 1;
1682 switch (snd_pcm_format_width(format)) {
1683 case 8: val |= 0x00; break;
1684 case 16: val |= 0x10; break;
1690 else if (maxbps >= 24)
1696 snd_printdd("invalid format width %d\n",
1697 snd_pcm_format_width(format));
1705 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
1706 * @codec: the HDA codec
1707 * @nid: NID to query
1708 * @ratesp: the pointer to store the detected rate bitflags
1709 * @formatsp: the pointer to store the detected formats
1710 * @bpsp: the pointer to store the detected format widths
1712 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
1713 * or @bsps argument is ignored.
1715 * Returns 0 if successful, otherwise a negative error code.
1717 int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
1718 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
1721 unsigned int val, streams;
1724 if (nid != codec->afg &&
1725 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD)) {
1726 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
1731 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
1735 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
1737 rates |= rate_bits[i].alsa_bits;
1742 if (formatsp || bpsp) {
1747 wcaps = get_wcaps(codec, nid);
1748 streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
1752 streams = snd_hda_param_read(codec, codec->afg,
1759 if (streams & AC_SUPFMT_PCM) {
1760 if (val & AC_SUPPCM_BITS_8) {
1761 formats |= SNDRV_PCM_FMTBIT_U8;
1764 if (val & AC_SUPPCM_BITS_16) {
1765 formats |= SNDRV_PCM_FMTBIT_S16_LE;
1768 if (wcaps & AC_WCAP_DIGITAL) {
1769 if (val & AC_SUPPCM_BITS_32)
1770 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
1771 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
1772 formats |= SNDRV_PCM_FMTBIT_S32_LE;
1773 if (val & AC_SUPPCM_BITS_24)
1775 else if (val & AC_SUPPCM_BITS_20)
1777 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
1778 AC_SUPPCM_BITS_32)) {
1779 formats |= SNDRV_PCM_FMTBIT_S32_LE;
1780 if (val & AC_SUPPCM_BITS_32)
1782 else if (val & AC_SUPPCM_BITS_24)
1784 else if (val & AC_SUPPCM_BITS_20)
1788 else if (streams == AC_SUPFMT_FLOAT32) {
1789 /* should be exclusive */
1790 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
1792 } else if (streams == AC_SUPFMT_AC3) {
1793 /* should be exclusive */
1794 /* temporary hack: we have still no proper support
1795 * for the direct AC3 stream...
1797 formats |= SNDRV_PCM_FMTBIT_U8;
1801 *formatsp = formats;
1810 * snd_hda_is_supported_format - check whether the given node supports
1813 * Returns 1 if supported, 0 if not.
1815 int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
1816 unsigned int format)
1819 unsigned int val = 0, rate, stream;
1821 if (nid != codec->afg &&
1822 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD)) {
1823 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
1828 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
1833 rate = format & 0xff00;
1834 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
1835 if (rate_bits[i].hda_fmt == rate) {
1840 if (i >= AC_PAR_PCM_RATE_BITS)
1843 stream = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
1846 if (!stream && nid != codec->afg)
1847 stream = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
1848 if (!stream || stream == -1)
1851 if (stream & AC_SUPFMT_PCM) {
1852 switch (format & 0xf0) {
1854 if (!(val & AC_SUPPCM_BITS_8))
1858 if (!(val & AC_SUPPCM_BITS_16))
1862 if (!(val & AC_SUPPCM_BITS_20))
1866 if (!(val & AC_SUPPCM_BITS_24))
1870 if (!(val & AC_SUPPCM_BITS_32))
1877 /* FIXME: check for float32 and AC3? */
1886 static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
1887 struct hda_codec *codec,
1888 struct snd_pcm_substream *substream)
1893 static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
1894 struct hda_codec *codec,
1895 unsigned int stream_tag,
1896 unsigned int format,
1897 struct snd_pcm_substream *substream)
1899 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
1903 static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
1904 struct hda_codec *codec,
1905 struct snd_pcm_substream *substream)
1907 snd_hda_codec_setup_stream(codec, hinfo->nid, 0, 0, 0);
1911 static int __devinit set_pcm_default_values(struct hda_codec *codec,
1912 struct hda_pcm_stream *info)
1914 /* query support PCM information from the given NID */
1915 if (info->nid && (!info->rates || !info->formats)) {
1916 snd_hda_query_supported_pcm(codec, info->nid,
1917 info->rates ? NULL : &info->rates,
1918 info->formats ? NULL : &info->formats,
1919 info->maxbps ? NULL : &info->maxbps);
1921 if (info->ops.open == NULL)
1922 info->ops.open = hda_pcm_default_open_close;
1923 if (info->ops.close == NULL)
1924 info->ops.close = hda_pcm_default_open_close;
1925 if (info->ops.prepare == NULL) {
1926 snd_assert(info->nid, return -EINVAL);
1927 info->ops.prepare = hda_pcm_default_prepare;
1929 if (info->ops.cleanup == NULL) {
1930 snd_assert(info->nid, return -EINVAL);
1931 info->ops.cleanup = hda_pcm_default_cleanup;
1937 * snd_hda_build_pcms - build PCM information
1940 * Create PCM information for each codec included in the bus.
1942 * The build_pcms codec patch is requested to set up codec->num_pcms and
1943 * codec->pcm_info properly. The array is referred by the top-level driver
1944 * to create its PCM instances.
1945 * The allocated codec->pcm_info should be released in codec->patch_ops.free
1948 * At least, substreams, channels_min and channels_max must be filled for
1949 * each stream. substreams = 0 indicates that the stream doesn't exist.
1950 * When rates and/or formats are zero, the supported values are queried
1951 * from the given nid. The nid is used also by the default ops.prepare
1952 * and ops.cleanup callbacks.
1954 * The driver needs to call ops.open in its open callback. Similarly,
1955 * ops.close is supposed to be called in the close callback.
1956 * ops.prepare should be called in the prepare or hw_params callback
1957 * with the proper parameters for set up.
1958 * ops.cleanup should be called in hw_free for clean up of streams.
1960 * This function returns 0 if successfull, or a negative error code.
1962 int __devinit snd_hda_build_pcms(struct hda_bus *bus)
1964 struct hda_codec *codec;
1966 list_for_each_entry(codec, &bus->codec_list, list) {
1967 unsigned int pcm, s;
1969 if (!codec->patch_ops.build_pcms)
1971 err = codec->patch_ops.build_pcms(codec);
1974 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
1975 for (s = 0; s < 2; s++) {
1976 struct hda_pcm_stream *info;
1977 info = &codec->pcm_info[pcm].stream[s];
1978 if (!info->substreams)
1980 err = set_pcm_default_values(codec, info);
1990 * snd_hda_check_board_config - compare the current codec with the config table
1991 * @codec: the HDA codec
1992 * @num_configs: number of config enums
1993 * @models: array of model name strings
1994 * @tbl: configuration table, terminated by null entries
1996 * Compares the modelname or PCI subsystem id of the current codec with the
1997 * given configuration table. If a matching entry is found, returns its
1998 * config value (supposed to be 0 or positive).
2000 * If no entries are matching, the function returns a negative value.
2002 int snd_hda_check_board_config(struct hda_codec *codec,
2003 int num_configs, const char **models,
2004 const struct snd_pci_quirk *tbl)
2006 if (codec->bus->modelname && models) {
2008 for (i = 0; i < num_configs; i++) {
2010 !strcmp(codec->bus->modelname, models[i])) {
2011 snd_printd(KERN_INFO "hda_codec: model '%s' is "
2012 "selected\n", models[i]);
2018 if (!codec->bus->pci || !tbl)
2021 tbl = snd_pci_quirk_lookup(codec->bus->pci, tbl);
2024 if (tbl->value >= 0 && tbl->value < num_configs) {
2025 #ifdef CONFIG_SND_DEBUG_DETECT
2027 const char *model = NULL;
2029 model = models[tbl->value];
2031 sprintf(tmp, "#%d", tbl->value);
2034 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
2035 "for config %x:%x (%s)\n",
2036 model, tbl->subvendor, tbl->subdevice,
2037 (tbl->name ? tbl->name : "Unknown device"));
2045 * snd_hda_add_new_ctls - create controls from the array
2046 * @codec: the HDA codec
2047 * @knew: the array of struct snd_kcontrol_new
2049 * This helper function creates and add new controls in the given array.
2050 * The array must be terminated with an empty entry as terminator.
2052 * Returns 0 if successful, or a negative error code.
2054 int snd_hda_add_new_ctls(struct hda_codec *codec, struct snd_kcontrol_new *knew)
2058 for (; knew->name; knew++) {
2059 struct snd_kcontrol *kctl;
2060 kctl = snd_ctl_new1(knew, codec);
2063 err = snd_ctl_add(codec->bus->card, kctl);
2067 kctl = snd_ctl_new1(knew, codec);
2070 kctl->id.device = codec->addr;
2071 err = snd_ctl_add(codec->bus->card, kctl);
2081 * Channel mode helper
2083 int snd_hda_ch_mode_info(struct hda_codec *codec,
2084 struct snd_ctl_elem_info *uinfo,
2085 const struct hda_channel_mode *chmode,
2088 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2090 uinfo->value.enumerated.items = num_chmodes;
2091 if (uinfo->value.enumerated.item >= num_chmodes)
2092 uinfo->value.enumerated.item = num_chmodes - 1;
2093 sprintf(uinfo->value.enumerated.name, "%dch",
2094 chmode[uinfo->value.enumerated.item].channels);
2098 int snd_hda_ch_mode_get(struct hda_codec *codec,
2099 struct snd_ctl_elem_value *ucontrol,
2100 const struct hda_channel_mode *chmode,
2106 for (i = 0; i < num_chmodes; i++) {
2107 if (max_channels == chmode[i].channels) {
2108 ucontrol->value.enumerated.item[0] = i;
2115 int snd_hda_ch_mode_put(struct hda_codec *codec,
2116 struct snd_ctl_elem_value *ucontrol,
2117 const struct hda_channel_mode *chmode,
2123 mode = ucontrol->value.enumerated.item[0];
2124 snd_assert(mode < num_chmodes, return -EINVAL);
2125 if (*max_channelsp == chmode[mode].channels && !codec->in_resume)
2127 /* change the current channel setting */
2128 *max_channelsp = chmode[mode].channels;
2129 if (chmode[mode].sequence)
2130 snd_hda_sequence_write(codec, chmode[mode].sequence);
2137 int snd_hda_input_mux_info(const struct hda_input_mux *imux,
2138 struct snd_ctl_elem_info *uinfo)
2142 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2144 uinfo->value.enumerated.items = imux->num_items;
2145 index = uinfo->value.enumerated.item;
2146 if (index >= imux->num_items)
2147 index = imux->num_items - 1;
2148 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
2152 int snd_hda_input_mux_put(struct hda_codec *codec,
2153 const struct hda_input_mux *imux,
2154 struct snd_ctl_elem_value *ucontrol,
2156 unsigned int *cur_val)
2160 idx = ucontrol->value.enumerated.item[0];
2161 if (idx >= imux->num_items)
2162 idx = imux->num_items - 1;
2163 if (*cur_val == idx && !codec->in_resume)
2165 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
2166 imux->items[idx].index);
2173 * Multi-channel / digital-out PCM helper functions
2176 /* setup SPDIF output stream */
2177 static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
2178 unsigned int stream_tag, unsigned int format)
2180 /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */
2181 if (codec->spdif_ctls & AC_DIG1_ENABLE)
2182 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_DIGI_CONVERT_1,
2183 codec->spdif_ctls & ~AC_DIG1_ENABLE & 0xff);
2184 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
2185 /* turn on again (if needed) */
2186 if (codec->spdif_ctls & AC_DIG1_ENABLE)
2187 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_DIGI_CONVERT_1,
2188 codec->spdif_ctls & 0xff);
2192 * open the digital out in the exclusive mode
2194 int snd_hda_multi_out_dig_open(struct hda_codec *codec,
2195 struct hda_multi_out *mout)
2197 mutex_lock(&codec->spdif_mutex);
2198 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
2199 /* already opened as analog dup; reset it once */
2200 snd_hda_codec_setup_stream(codec, mout->dig_out_nid, 0, 0, 0);
2201 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
2202 mutex_unlock(&codec->spdif_mutex);
2206 int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
2207 struct hda_multi_out *mout,
2208 unsigned int stream_tag,
2209 unsigned int format,
2210 struct snd_pcm_substream *substream)
2212 mutex_lock(&codec->spdif_mutex);
2213 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
2214 mutex_unlock(&codec->spdif_mutex);
2219 * release the digital out
2221 int snd_hda_multi_out_dig_close(struct hda_codec *codec,
2222 struct hda_multi_out *mout)
2224 mutex_lock(&codec->spdif_mutex);
2225 mout->dig_out_used = 0;
2226 mutex_unlock(&codec->spdif_mutex);
2231 * set up more restrictions for analog out
2233 int snd_hda_multi_out_analog_open(struct hda_codec *codec,
2234 struct hda_multi_out *mout,
2235 struct snd_pcm_substream *substream)
2237 substream->runtime->hw.channels_max = mout->max_channels;
2238 return snd_pcm_hw_constraint_step(substream->runtime, 0,
2239 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
2243 * set up the i/o for analog out
2244 * when the digital out is available, copy the front out to digital out, too.
2246 int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
2247 struct hda_multi_out *mout,
2248 unsigned int stream_tag,
2249 unsigned int format,
2250 struct snd_pcm_substream *substream)
2252 hda_nid_t *nids = mout->dac_nids;
2253 int chs = substream->runtime->channels;
2256 mutex_lock(&codec->spdif_mutex);
2257 if (mout->dig_out_nid && mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
2259 snd_hda_is_supported_format(codec, mout->dig_out_nid,
2261 !(codec->spdif_status & IEC958_AES0_NONAUDIO)) {
2262 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
2263 setup_dig_out_stream(codec, mout->dig_out_nid,
2264 stream_tag, format);
2266 mout->dig_out_used = 0;
2267 snd_hda_codec_setup_stream(codec, mout->dig_out_nid,
2271 mutex_unlock(&codec->spdif_mutex);
2274 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
2276 if (mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
2277 /* headphone out will just decode front left/right (stereo) */
2278 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
2280 /* extra outputs copied from front */
2281 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
2282 if (mout->extra_out_nid[i])
2283 snd_hda_codec_setup_stream(codec,
2284 mout->extra_out_nid[i],
2285 stream_tag, 0, format);
2288 for (i = 1; i < mout->num_dacs; i++) {
2289 if (chs >= (i + 1) * 2) /* independent out */
2290 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
2292 else /* copy front */
2293 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
2300 * clean up the setting for analog out
2302 int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
2303 struct hda_multi_out *mout)
2305 hda_nid_t *nids = mout->dac_nids;
2308 for (i = 0; i < mout->num_dacs; i++)
2309 snd_hda_codec_setup_stream(codec, nids[i], 0, 0, 0);
2311 snd_hda_codec_setup_stream(codec, mout->hp_nid, 0, 0, 0);
2312 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
2313 if (mout->extra_out_nid[i])
2314 snd_hda_codec_setup_stream(codec,
2315 mout->extra_out_nid[i],
2317 mutex_lock(&codec->spdif_mutex);
2318 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
2319 snd_hda_codec_setup_stream(codec, mout->dig_out_nid, 0, 0, 0);
2320 mout->dig_out_used = 0;
2322 mutex_unlock(&codec->spdif_mutex);
2327 * Helper for automatic ping configuration
2330 static int is_in_nid_list(hda_nid_t nid, hda_nid_t *list)
2332 for (; *list; list++)
2340 * Sort an associated group of pins according to their sequence numbers.
2342 static void sort_pins_by_sequence(hda_nid_t * pins, short * sequences,
2349 for (i = 0; i < num_pins; i++) {
2350 for (j = i + 1; j < num_pins; j++) {
2351 if (sequences[i] > sequences[j]) {
2353 sequences[i] = sequences[j];
2365 * Parse all pin widgets and store the useful pin nids to cfg
2367 * The number of line-outs or any primary output is stored in line_outs,
2368 * and the corresponding output pins are assigned to line_out_pins[],
2369 * in the order of front, rear, CLFE, side, ...
2371 * If more extra outputs (speaker and headphone) are found, the pins are
2372 * assisnged to hp_pins[] and speaker_pins[], respectively. If no line-out jack
2373 * is detected, one of speaker of HP pins is assigned as the primary
2374 * output, i.e. to line_out_pins[0]. So, line_outs is always positive
2375 * if any analog output exists.
2377 * The analog input pins are assigned to input_pins array.
2378 * The digital input/output pins are assigned to dig_in_pin and dig_out_pin,
2381 int snd_hda_parse_pin_def_config(struct hda_codec *codec,
2382 struct auto_pin_cfg *cfg,
2383 hda_nid_t *ignore_nids)
2385 hda_nid_t nid, nid_start;
2387 short seq, assoc_line_out, assoc_speaker;
2388 short sequences_line_out[ARRAY_SIZE(cfg->line_out_pins)];
2389 short sequences_speaker[ARRAY_SIZE(cfg->speaker_pins)];
2391 memset(cfg, 0, sizeof(*cfg));
2393 memset(sequences_line_out, 0, sizeof(sequences_line_out));
2394 memset(sequences_speaker, 0, sizeof(sequences_speaker));
2395 assoc_line_out = assoc_speaker = 0;
2397 nodes = snd_hda_get_sub_nodes(codec, codec->afg, &nid_start);
2398 for (nid = nid_start; nid < nodes + nid_start; nid++) {
2399 unsigned int wid_caps = get_wcaps(codec, nid);
2400 unsigned int wid_type =
2401 (wid_caps & AC_WCAP_TYPE) >> AC_WCAP_TYPE_SHIFT;
2402 unsigned int def_conf;
2405 /* read all default configuration for pin complex */
2406 if (wid_type != AC_WID_PIN)
2408 /* ignore the given nids (e.g. pc-beep returns error) */
2409 if (ignore_nids && is_in_nid_list(nid, ignore_nids))
2412 def_conf = snd_hda_codec_read(codec, nid, 0,
2413 AC_VERB_GET_CONFIG_DEFAULT, 0);
2414 if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE)
2416 loc = get_defcfg_location(def_conf);
2417 switch (get_defcfg_device(def_conf)) {
2418 case AC_JACK_LINE_OUT:
2419 seq = get_defcfg_sequence(def_conf);
2420 assoc = get_defcfg_association(def_conf);
2423 if (!assoc_line_out)
2424 assoc_line_out = assoc;
2425 else if (assoc_line_out != assoc)
2427 if (cfg->line_outs >= ARRAY_SIZE(cfg->line_out_pins))
2429 cfg->line_out_pins[cfg->line_outs] = nid;
2430 sequences_line_out[cfg->line_outs] = seq;
2433 case AC_JACK_SPEAKER:
2434 seq = get_defcfg_sequence(def_conf);
2435 assoc = get_defcfg_association(def_conf);
2438 if (! assoc_speaker)
2439 assoc_speaker = assoc;
2440 else if (assoc_speaker != assoc)
2442 if (cfg->speaker_outs >= ARRAY_SIZE(cfg->speaker_pins))
2444 cfg->speaker_pins[cfg->speaker_outs] = nid;
2445 sequences_speaker[cfg->speaker_outs] = seq;
2446 cfg->speaker_outs++;
2448 case AC_JACK_HP_OUT:
2449 if (cfg->hp_outs >= ARRAY_SIZE(cfg->hp_pins))
2451 cfg->hp_pins[cfg->hp_outs] = nid;
2454 case AC_JACK_MIC_IN: {
2456 if (loc == AC_JACK_LOC_FRONT) {
2457 preferred = AUTO_PIN_FRONT_MIC;
2460 preferred = AUTO_PIN_MIC;
2461 alt = AUTO_PIN_FRONT_MIC;
2463 if (!cfg->input_pins[preferred])
2464 cfg->input_pins[preferred] = nid;
2465 else if (!cfg->input_pins[alt])
2466 cfg->input_pins[alt] = nid;
2469 case AC_JACK_LINE_IN:
2470 if (loc == AC_JACK_LOC_FRONT)
2471 cfg->input_pins[AUTO_PIN_FRONT_LINE] = nid;
2473 cfg->input_pins[AUTO_PIN_LINE] = nid;
2476 cfg->input_pins[AUTO_PIN_CD] = nid;
2479 cfg->input_pins[AUTO_PIN_AUX] = nid;
2481 case AC_JACK_SPDIF_OUT:
2482 cfg->dig_out_pin = nid;
2484 case AC_JACK_SPDIF_IN:
2485 cfg->dig_in_pin = nid;
2490 /* sort by sequence */
2491 sort_pins_by_sequence(cfg->line_out_pins, sequences_line_out,
2493 sort_pins_by_sequence(cfg->speaker_pins, sequences_speaker,
2497 * FIX-UP: if no line-outs are detected, try to use speaker or HP pin
2498 * as a primary output
2500 if (!cfg->line_outs) {
2501 if (cfg->speaker_outs) {
2502 cfg->line_outs = cfg->speaker_outs;
2503 memcpy(cfg->line_out_pins, cfg->speaker_pins,
2504 sizeof(cfg->speaker_pins));
2505 cfg->speaker_outs = 0;
2506 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
2507 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
2508 } else if (cfg->hp_outs) {
2509 cfg->line_outs = cfg->hp_outs;
2510 memcpy(cfg->line_out_pins, cfg->hp_pins,
2511 sizeof(cfg->hp_pins));
2513 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
2514 cfg->line_out_type = AUTO_PIN_HP_OUT;
2518 /* Reorder the surround channels
2519 * ALSA sequence is front/surr/clfe/side
2521 * 4-ch: front/surr => OK as it is
2522 * 6-ch: front/clfe/surr
2523 * 8-ch: front/clfe/rear/side|fc
2525 switch (cfg->line_outs) {
2528 nid = cfg->line_out_pins[1];
2529 cfg->line_out_pins[1] = cfg->line_out_pins[2];
2530 cfg->line_out_pins[2] = nid;
2535 * debug prints of the parsed results
2537 snd_printd("autoconfig: line_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
2538 cfg->line_outs, cfg->line_out_pins[0], cfg->line_out_pins[1],
2539 cfg->line_out_pins[2], cfg->line_out_pins[3],
2540 cfg->line_out_pins[4]);
2541 snd_printd(" speaker_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
2542 cfg->speaker_outs, cfg->speaker_pins[0],
2543 cfg->speaker_pins[1], cfg->speaker_pins[2],
2544 cfg->speaker_pins[3], cfg->speaker_pins[4]);
2545 snd_printd(" hp_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
2546 cfg->hp_outs, cfg->hp_pins[0],
2547 cfg->hp_pins[1], cfg->hp_pins[2],
2548 cfg->hp_pins[3], cfg->hp_pins[4]);
2549 snd_printd(" inputs: mic=0x%x, fmic=0x%x, line=0x%x, fline=0x%x,"
2550 " cd=0x%x, aux=0x%x\n",
2551 cfg->input_pins[AUTO_PIN_MIC],
2552 cfg->input_pins[AUTO_PIN_FRONT_MIC],
2553 cfg->input_pins[AUTO_PIN_LINE],
2554 cfg->input_pins[AUTO_PIN_FRONT_LINE],
2555 cfg->input_pins[AUTO_PIN_CD],
2556 cfg->input_pins[AUTO_PIN_AUX]);
2561 /* labels for input pins */
2562 const char *auto_pin_cfg_labels[AUTO_PIN_LAST] = {
2563 "Mic", "Front Mic", "Line", "Front Line", "CD", "Aux"
2573 * snd_hda_suspend - suspend the codecs
2575 * @state: suspsend state
2577 * Returns 0 if successful.
2579 int snd_hda_suspend(struct hda_bus *bus, pm_message_t state)
2581 struct hda_codec *codec;
2583 /* FIXME: should handle power widget capabilities */
2584 list_for_each_entry(codec, &bus->codec_list, list) {
2585 if (codec->patch_ops.suspend)
2586 codec->patch_ops.suspend(codec, state);
2587 hda_set_power_state(codec,
2588 codec->afg ? codec->afg : codec->mfg,
2595 * snd_hda_resume - resume the codecs
2597 * @state: resume state
2599 * Returns 0 if successful.
2601 int snd_hda_resume(struct hda_bus *bus)
2603 struct hda_codec *codec;
2605 list_for_each_entry(codec, &bus->codec_list, list) {
2606 hda_set_power_state(codec,
2607 codec->afg ? codec->afg : codec->mfg,
2609 if (codec->patch_ops.resume)
2610 codec->patch_ops.resume(codec);
2616 * snd_hda_resume_ctls - resume controls in the new control list
2617 * @codec: the HDA codec
2618 * @knew: the array of struct snd_kcontrol_new
2620 * This function resumes the mixer controls in the struct snd_kcontrol_new array,
2621 * originally for snd_hda_add_new_ctls().
2622 * The array must be terminated with an empty entry as terminator.
2624 int snd_hda_resume_ctls(struct hda_codec *codec, struct snd_kcontrol_new *knew)
2626 struct snd_ctl_elem_value *val;
2628 val = kmalloc(sizeof(*val), GFP_KERNEL);
2631 codec->in_resume = 1;
2632 for (; knew->name; knew++) {
2634 count = knew->count ? knew->count : 1;
2635 for (i = 0; i < count; i++) {
2636 memset(val, 0, sizeof(*val));
2637 val->id.iface = knew->iface;
2638 val->id.device = knew->device;
2639 val->id.subdevice = knew->subdevice;
2640 strcpy(val->id.name, knew->name);
2641 val->id.index = knew->index ? knew->index : i;
2642 /* Assume that get callback reads only from cache,
2643 * not accessing to the real hardware
2645 if (snd_ctl_elem_read(codec->bus->card, val) < 0)
2647 snd_ctl_elem_write(codec->bus->card, NULL, val);
2650 codec->in_resume = 0;
2656 * snd_hda_resume_spdif_out - resume the digital out
2657 * @codec: the HDA codec
2659 int snd_hda_resume_spdif_out(struct hda_codec *codec)
2661 return snd_hda_resume_ctls(codec, dig_mixes);
2665 * snd_hda_resume_spdif_in - resume the digital in
2666 * @codec: the HDA codec
2668 int snd_hda_resume_spdif_in(struct hda_codec *codec)
2670 return snd_hda_resume_ctls(codec, dig_in_ctls);