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)
841 put_vol_mute(codec, info, nid, ch, direction, idx, val);
846 /* resume the all amp commands from the cache */
847 void snd_hda_codec_resume_amp(struct hda_codec *codec)
849 struct hda_amp_info *buffer = codec->amp_cache.buffer;
852 for (i = 0; i < codec->amp_cache.size; i++, buffer++) {
853 u32 key = buffer->head.key;
855 unsigned int idx, dir, ch;
859 idx = (key >> 16) & 0xff;
860 dir = (key >> 24) & 0xff;
861 for (ch = 0; ch < 2; ch++) {
862 if (!(buffer->head.val & INFO_AMP_VOL(ch)))
864 put_vol_mute(codec, buffer, nid, ch, dir, idx,
869 #endif /* CONFIG_PM */
872 * AMP control callbacks
874 /* retrieve parameters from private_value */
875 #define get_amp_nid(kc) ((kc)->private_value & 0xffff)
876 #define get_amp_channels(kc) (((kc)->private_value >> 16) & 0x3)
877 #define get_amp_direction(kc) (((kc)->private_value >> 18) & 0x1)
878 #define get_amp_index(kc) (((kc)->private_value >> 19) & 0xf)
881 int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
882 struct snd_ctl_elem_info *uinfo)
884 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
885 u16 nid = get_amp_nid(kcontrol);
886 u8 chs = get_amp_channels(kcontrol);
887 int dir = get_amp_direction(kcontrol);
890 caps = query_amp_caps(codec, nid, dir);
892 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
894 printk(KERN_WARNING "hda_codec: "
895 "num_steps = 0 for NID=0x%x\n", nid);
898 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
899 uinfo->count = chs == 3 ? 2 : 1;
900 uinfo->value.integer.min = 0;
901 uinfo->value.integer.max = caps;
905 int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
906 struct snd_ctl_elem_value *ucontrol)
908 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
909 hda_nid_t nid = get_amp_nid(kcontrol);
910 int chs = get_amp_channels(kcontrol);
911 int dir = get_amp_direction(kcontrol);
912 int idx = get_amp_index(kcontrol);
913 long *valp = ucontrol->value.integer.value;
916 *valp++ = snd_hda_codec_amp_read(codec, nid, 0, dir, idx) & 0x7f;
918 *valp = snd_hda_codec_amp_read(codec, nid, 1, dir, idx) & 0x7f;
922 int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
923 struct snd_ctl_elem_value *ucontrol)
925 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
926 hda_nid_t nid = get_amp_nid(kcontrol);
927 int chs = get_amp_channels(kcontrol);
928 int dir = get_amp_direction(kcontrol);
929 int idx = get_amp_index(kcontrol);
930 long *valp = ucontrol->value.integer.value;
934 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
939 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
944 int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
945 unsigned int size, unsigned int __user *_tlv)
947 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
948 hda_nid_t nid = get_amp_nid(kcontrol);
949 int dir = get_amp_direction(kcontrol);
950 u32 caps, val1, val2;
952 if (size < 4 * sizeof(unsigned int))
954 caps = query_amp_caps(codec, nid, dir);
955 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
956 val2 = (val2 + 1) * 25;
957 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
958 val1 = ((int)val1) * ((int)val2);
959 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
961 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
963 if (put_user(val1, _tlv + 2))
965 if (put_user(val2, _tlv + 3))
971 int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
972 struct snd_ctl_elem_info *uinfo)
974 int chs = get_amp_channels(kcontrol);
976 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
977 uinfo->count = chs == 3 ? 2 : 1;
978 uinfo->value.integer.min = 0;
979 uinfo->value.integer.max = 1;
983 int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
984 struct snd_ctl_elem_value *ucontrol)
986 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
987 hda_nid_t nid = get_amp_nid(kcontrol);
988 int chs = get_amp_channels(kcontrol);
989 int dir = get_amp_direction(kcontrol);
990 int idx = get_amp_index(kcontrol);
991 long *valp = ucontrol->value.integer.value;
994 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
997 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
1002 int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
1003 struct snd_ctl_elem_value *ucontrol)
1005 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1006 hda_nid_t nid = get_amp_nid(kcontrol);
1007 int chs = get_amp_channels(kcontrol);
1008 int dir = get_amp_direction(kcontrol);
1009 int idx = get_amp_index(kcontrol);
1010 long *valp = ucontrol->value.integer.value;
1014 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
1015 0x80, *valp ? 0 : 0x80);
1019 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
1020 0x80, *valp ? 0 : 0x80);
1026 * bound volume controls
1028 * bind multiple volumes (# indices, from 0)
1031 #define AMP_VAL_IDX_SHIFT 19
1032 #define AMP_VAL_IDX_MASK (0x0f<<19)
1034 int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
1035 struct snd_ctl_elem_value *ucontrol)
1037 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1041 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
1042 pval = kcontrol->private_value;
1043 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
1044 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
1045 kcontrol->private_value = pval;
1046 mutex_unlock(&codec->spdif_mutex);
1050 int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
1051 struct snd_ctl_elem_value *ucontrol)
1053 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1055 int i, indices, err = 0, change = 0;
1057 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
1058 pval = kcontrol->private_value;
1059 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
1060 for (i = 0; i < indices; i++) {
1061 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
1062 (i << AMP_VAL_IDX_SHIFT);
1063 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
1068 kcontrol->private_value = pval;
1069 mutex_unlock(&codec->spdif_mutex);
1070 return err < 0 ? err : change;
1074 * generic bound volume/swtich controls
1076 int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
1077 struct snd_ctl_elem_info *uinfo)
1079 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1080 struct hda_bind_ctls *c;
1083 c = (struct hda_bind_ctls *)kcontrol->private_value;
1084 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
1085 kcontrol->private_value = *c->values;
1086 err = c->ops->info(kcontrol, uinfo);
1087 kcontrol->private_value = (long)c;
1088 mutex_unlock(&codec->spdif_mutex);
1092 int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
1093 struct snd_ctl_elem_value *ucontrol)
1095 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1096 struct hda_bind_ctls *c;
1099 c = (struct hda_bind_ctls *)kcontrol->private_value;
1100 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
1101 kcontrol->private_value = *c->values;
1102 err = c->ops->get(kcontrol, ucontrol);
1103 kcontrol->private_value = (long)c;
1104 mutex_unlock(&codec->spdif_mutex);
1108 int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
1109 struct snd_ctl_elem_value *ucontrol)
1111 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1112 struct hda_bind_ctls *c;
1113 unsigned long *vals;
1114 int err = 0, change = 0;
1116 c = (struct hda_bind_ctls *)kcontrol->private_value;
1117 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
1118 for (vals = c->values; *vals; vals++) {
1119 kcontrol->private_value = *vals;
1120 err = c->ops->put(kcontrol, ucontrol);
1125 kcontrol->private_value = (long)c;
1126 mutex_unlock(&codec->spdif_mutex);
1127 return err < 0 ? err : change;
1130 int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
1131 unsigned int size, unsigned int __user *tlv)
1133 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1134 struct hda_bind_ctls *c;
1137 c = (struct hda_bind_ctls *)kcontrol->private_value;
1138 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
1139 kcontrol->private_value = *c->values;
1140 err = c->ops->tlv(kcontrol, op_flag, size, tlv);
1141 kcontrol->private_value = (long)c;
1142 mutex_unlock(&codec->spdif_mutex);
1146 struct hda_ctl_ops snd_hda_bind_vol = {
1147 .info = snd_hda_mixer_amp_volume_info,
1148 .get = snd_hda_mixer_amp_volume_get,
1149 .put = snd_hda_mixer_amp_volume_put,
1150 .tlv = snd_hda_mixer_amp_tlv
1153 struct hda_ctl_ops snd_hda_bind_sw = {
1154 .info = snd_hda_mixer_amp_switch_info,
1155 .get = snd_hda_mixer_amp_switch_get,
1156 .put = snd_hda_mixer_amp_switch_put,
1157 .tlv = snd_hda_mixer_amp_tlv
1161 * SPDIF out controls
1164 static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
1165 struct snd_ctl_elem_info *uinfo)
1167 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1172 static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
1173 struct snd_ctl_elem_value *ucontrol)
1175 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
1176 IEC958_AES0_NONAUDIO |
1177 IEC958_AES0_CON_EMPHASIS_5015 |
1178 IEC958_AES0_CON_NOT_COPYRIGHT;
1179 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
1180 IEC958_AES1_CON_ORIGINAL;
1184 static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
1185 struct snd_ctl_elem_value *ucontrol)
1187 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
1188 IEC958_AES0_NONAUDIO |
1189 IEC958_AES0_PRO_EMPHASIS_5015;
1193 static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
1194 struct snd_ctl_elem_value *ucontrol)
1196 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1198 ucontrol->value.iec958.status[0] = codec->spdif_status & 0xff;
1199 ucontrol->value.iec958.status[1] = (codec->spdif_status >> 8) & 0xff;
1200 ucontrol->value.iec958.status[2] = (codec->spdif_status >> 16) & 0xff;
1201 ucontrol->value.iec958.status[3] = (codec->spdif_status >> 24) & 0xff;
1206 /* convert from SPDIF status bits to HDA SPDIF bits
1207 * bit 0 (DigEn) is always set zero (to be filled later)
1209 static unsigned short convert_from_spdif_status(unsigned int sbits)
1211 unsigned short val = 0;
1213 if (sbits & IEC958_AES0_PROFESSIONAL)
1214 val |= AC_DIG1_PROFESSIONAL;
1215 if (sbits & IEC958_AES0_NONAUDIO)
1216 val |= AC_DIG1_NONAUDIO;
1217 if (sbits & IEC958_AES0_PROFESSIONAL) {
1218 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
1219 IEC958_AES0_PRO_EMPHASIS_5015)
1220 val |= AC_DIG1_EMPHASIS;
1222 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
1223 IEC958_AES0_CON_EMPHASIS_5015)
1224 val |= AC_DIG1_EMPHASIS;
1225 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
1226 val |= AC_DIG1_COPYRIGHT;
1227 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
1228 val |= AC_DIG1_LEVEL;
1229 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
1234 /* convert to SPDIF status bits from HDA SPDIF bits
1236 static unsigned int convert_to_spdif_status(unsigned short val)
1238 unsigned int sbits = 0;
1240 if (val & AC_DIG1_NONAUDIO)
1241 sbits |= IEC958_AES0_NONAUDIO;
1242 if (val & AC_DIG1_PROFESSIONAL)
1243 sbits |= IEC958_AES0_PROFESSIONAL;
1244 if (sbits & IEC958_AES0_PROFESSIONAL) {
1245 if (sbits & AC_DIG1_EMPHASIS)
1246 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
1248 if (val & AC_DIG1_EMPHASIS)
1249 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
1250 if (!(val & AC_DIG1_COPYRIGHT))
1251 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
1252 if (val & AC_DIG1_LEVEL)
1253 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
1254 sbits |= val & (0x7f << 8);
1259 static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
1260 struct snd_ctl_elem_value *ucontrol)
1262 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1263 hda_nid_t nid = kcontrol->private_value;
1267 mutex_lock(&codec->spdif_mutex);
1268 codec->spdif_status = ucontrol->value.iec958.status[0] |
1269 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
1270 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
1271 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
1272 val = convert_from_spdif_status(codec->spdif_status);
1273 val |= codec->spdif_ctls & 1;
1274 change = codec->spdif_ctls != val;
1275 codec->spdif_ctls = val;
1278 snd_hda_codec_write_cache(codec, nid, 0,
1279 AC_VERB_SET_DIGI_CONVERT_1,
1281 snd_hda_codec_write_cache(codec, nid, 0,
1282 AC_VERB_SET_DIGI_CONVERT_2,
1286 mutex_unlock(&codec->spdif_mutex);
1290 #define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
1292 static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
1293 struct snd_ctl_elem_value *ucontrol)
1295 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1297 ucontrol->value.integer.value[0] = codec->spdif_ctls & AC_DIG1_ENABLE;
1301 static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
1302 struct snd_ctl_elem_value *ucontrol)
1304 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1305 hda_nid_t nid = kcontrol->private_value;
1309 mutex_lock(&codec->spdif_mutex);
1310 val = codec->spdif_ctls & ~AC_DIG1_ENABLE;
1311 if (ucontrol->value.integer.value[0])
1312 val |= AC_DIG1_ENABLE;
1313 change = codec->spdif_ctls != val;
1315 codec->spdif_ctls = val;
1316 snd_hda_codec_write_cache(codec, nid, 0,
1317 AC_VERB_SET_DIGI_CONVERT_1,
1319 /* unmute amp switch (if any) */
1320 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
1321 (val & AC_DIG1_ENABLE)) {
1322 snd_hda_codec_amp_update(codec, nid, 0, HDA_OUTPUT, 0,
1324 snd_hda_codec_amp_update(codec, nid, 1, HDA_OUTPUT, 0,
1328 mutex_unlock(&codec->spdif_mutex);
1332 static struct snd_kcontrol_new dig_mixes[] = {
1334 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1335 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1336 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,CON_MASK),
1337 .info = snd_hda_spdif_mask_info,
1338 .get = snd_hda_spdif_cmask_get,
1341 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1342 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1343 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,PRO_MASK),
1344 .info = snd_hda_spdif_mask_info,
1345 .get = snd_hda_spdif_pmask_get,
1348 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1349 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
1350 .info = snd_hda_spdif_mask_info,
1351 .get = snd_hda_spdif_default_get,
1352 .put = snd_hda_spdif_default_put,
1355 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1356 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,SWITCH),
1357 .info = snd_hda_spdif_out_switch_info,
1358 .get = snd_hda_spdif_out_switch_get,
1359 .put = snd_hda_spdif_out_switch_put,
1365 * snd_hda_create_spdif_out_ctls - create Output SPDIF-related controls
1366 * @codec: the HDA codec
1367 * @nid: audio out widget NID
1369 * Creates controls related with the SPDIF output.
1370 * Called from each patch supporting the SPDIF out.
1372 * Returns 0 if successful, or a negative error code.
1374 int snd_hda_create_spdif_out_ctls(struct hda_codec *codec, hda_nid_t nid)
1377 struct snd_kcontrol *kctl;
1378 struct snd_kcontrol_new *dig_mix;
1380 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
1381 kctl = snd_ctl_new1(dig_mix, codec);
1382 kctl->private_value = nid;
1383 err = snd_ctl_add(codec->bus->card, kctl);
1388 snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT, 0);
1389 codec->spdif_status = convert_to_spdif_status(codec->spdif_ctls);
1397 #define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
1399 static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
1400 struct snd_ctl_elem_value *ucontrol)
1402 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1404 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
1408 static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
1409 struct snd_ctl_elem_value *ucontrol)
1411 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1412 hda_nid_t nid = kcontrol->private_value;
1413 unsigned int val = !!ucontrol->value.integer.value[0];
1416 mutex_lock(&codec->spdif_mutex);
1417 change = codec->spdif_in_enable != val;
1419 codec->spdif_in_enable = val;
1420 snd_hda_codec_write_cache(codec, nid, 0,
1421 AC_VERB_SET_DIGI_CONVERT_1, val);
1423 mutex_unlock(&codec->spdif_mutex);
1427 static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
1428 struct snd_ctl_elem_value *ucontrol)
1430 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1431 hda_nid_t nid = kcontrol->private_value;
1435 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT, 0);
1436 sbits = convert_to_spdif_status(val);
1437 ucontrol->value.iec958.status[0] = sbits;
1438 ucontrol->value.iec958.status[1] = sbits >> 8;
1439 ucontrol->value.iec958.status[2] = sbits >> 16;
1440 ucontrol->value.iec958.status[3] = sbits >> 24;
1444 static struct snd_kcontrol_new dig_in_ctls[] = {
1446 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1447 .name = SNDRV_CTL_NAME_IEC958("",CAPTURE,SWITCH),
1448 .info = snd_hda_spdif_in_switch_info,
1449 .get = snd_hda_spdif_in_switch_get,
1450 .put = snd_hda_spdif_in_switch_put,
1453 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1454 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1455 .name = SNDRV_CTL_NAME_IEC958("",CAPTURE,DEFAULT),
1456 .info = snd_hda_spdif_mask_info,
1457 .get = snd_hda_spdif_in_status_get,
1463 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
1464 * @codec: the HDA codec
1465 * @nid: audio in widget NID
1467 * Creates controls related with the SPDIF input.
1468 * Called from each patch supporting the SPDIF in.
1470 * Returns 0 if successful, or a negative error code.
1472 int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
1475 struct snd_kcontrol *kctl;
1476 struct snd_kcontrol_new *dig_mix;
1478 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
1479 kctl = snd_ctl_new1(dig_mix, codec);
1480 kctl->private_value = nid;
1481 err = snd_ctl_add(codec->bus->card, kctl);
1485 codec->spdif_in_enable =
1486 snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT, 0) &
1496 /* build a 32bit cache key with the widget id and the command parameter */
1497 #define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
1498 #define get_cmd_cache_nid(key) ((key) & 0xff)
1499 #define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
1502 * snd_hda_codec_write_cache - send a single command with caching
1503 * @codec: the HDA codec
1504 * @nid: NID to send the command
1505 * @direct: direct flag
1506 * @verb: the verb to send
1507 * @parm: the parameter for the verb
1509 * Send a single command without waiting for response.
1511 * Returns 0 if successful, or a negative error code.
1513 int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
1514 int direct, unsigned int verb, unsigned int parm)
1517 mutex_lock(&codec->bus->cmd_mutex);
1518 err = codec->bus->ops.command(codec, nid, direct, verb, parm);
1520 struct hda_cache_head *c;
1521 u32 key = build_cmd_cache_key(nid, verb);
1522 c = get_alloc_hash(&codec->cmd_cache, key);
1526 mutex_unlock(&codec->bus->cmd_mutex);
1530 /* resume the all commands from the cache */
1531 void snd_hda_codec_resume_cache(struct hda_codec *codec)
1533 struct hda_cache_head *buffer = codec->cmd_cache.buffer;
1536 for (i = 0; i < codec->cmd_cache.size; i++, buffer++) {
1537 u32 key = buffer->key;
1540 snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
1541 get_cmd_cache_cmd(key), buffer->val);
1546 * snd_hda_sequence_write_cache - sequence writes with caching
1547 * @codec: the HDA codec
1548 * @seq: VERB array to send
1550 * Send the commands sequentially from the given array.
1551 * Thte commands are recorded on cache for power-save and resume.
1552 * The array must be terminated with NID=0.
1554 void snd_hda_sequence_write_cache(struct hda_codec *codec,
1555 const struct hda_verb *seq)
1557 for (; seq->nid; seq++)
1558 snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
1561 #endif /* CONFIG_PM */
1564 * set power state of the codec
1566 static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
1567 unsigned int power_state)
1569 hda_nid_t nid, nid_start;
1572 snd_hda_codec_write(codec, fg, 0, AC_VERB_SET_POWER_STATE,
1575 nodes = snd_hda_get_sub_nodes(codec, fg, &nid_start);
1576 for (nid = nid_start; nid < nodes + nid_start; nid++) {
1577 if (get_wcaps(codec, nid) & AC_WCAP_POWER)
1578 snd_hda_codec_write(codec, nid, 0,
1579 AC_VERB_SET_POWER_STATE,
1583 if (power_state == AC_PWRST_D0)
1589 * snd_hda_build_controls - build mixer controls
1592 * Creates mixer controls for each codec included in the bus.
1594 * Returns 0 if successful, otherwise a negative error code.
1596 int __devinit snd_hda_build_controls(struct hda_bus *bus)
1598 struct hda_codec *codec;
1600 /* build controls */
1601 list_for_each_entry(codec, &bus->codec_list, list) {
1603 if (!codec->patch_ops.build_controls)
1605 err = codec->patch_ops.build_controls(codec);
1611 list_for_each_entry(codec, &bus->codec_list, list) {
1613 hda_set_power_state(codec,
1614 codec->afg ? codec->afg : codec->mfg,
1616 if (!codec->patch_ops.init)
1618 err = codec->patch_ops.init(codec);
1628 struct hda_rate_tbl {
1630 unsigned int alsa_bits;
1631 unsigned int hda_fmt;
1634 static struct hda_rate_tbl rate_bits[] = {
1635 /* rate in Hz, ALSA rate bitmask, HDA format value */
1637 /* autodetected value used in snd_hda_query_supported_pcm */
1638 { 8000, SNDRV_PCM_RATE_8000, 0x0500 }, /* 1/6 x 48 */
1639 { 11025, SNDRV_PCM_RATE_11025, 0x4300 }, /* 1/4 x 44 */
1640 { 16000, SNDRV_PCM_RATE_16000, 0x0200 }, /* 1/3 x 48 */
1641 { 22050, SNDRV_PCM_RATE_22050, 0x4100 }, /* 1/2 x 44 */
1642 { 32000, SNDRV_PCM_RATE_32000, 0x0a00 }, /* 2/3 x 48 */
1643 { 44100, SNDRV_PCM_RATE_44100, 0x4000 }, /* 44 */
1644 { 48000, SNDRV_PCM_RATE_48000, 0x0000 }, /* 48 */
1645 { 88200, SNDRV_PCM_RATE_88200, 0x4800 }, /* 2 x 44 */
1646 { 96000, SNDRV_PCM_RATE_96000, 0x0800 }, /* 2 x 48 */
1647 { 176400, SNDRV_PCM_RATE_176400, 0x5800 },/* 4 x 44 */
1648 { 192000, SNDRV_PCM_RATE_192000, 0x1800 }, /* 4 x 48 */
1649 #define AC_PAR_PCM_RATE_BITS 11
1650 /* up to bits 10, 384kHZ isn't supported properly */
1652 /* not autodetected value */
1653 { 9600, SNDRV_PCM_RATE_KNOT, 0x0400 }, /* 1/5 x 48 */
1655 { 0 } /* terminator */
1659 * snd_hda_calc_stream_format - calculate format bitset
1660 * @rate: the sample rate
1661 * @channels: the number of channels
1662 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
1663 * @maxbps: the max. bps
1665 * Calculate the format bitset from the given rate, channels and th PCM format.
1667 * Return zero if invalid.
1669 unsigned int snd_hda_calc_stream_format(unsigned int rate,
1670 unsigned int channels,
1671 unsigned int format,
1672 unsigned int maxbps)
1675 unsigned int val = 0;
1677 for (i = 0; rate_bits[i].hz; i++)
1678 if (rate_bits[i].hz == rate) {
1679 val = rate_bits[i].hda_fmt;
1682 if (!rate_bits[i].hz) {
1683 snd_printdd("invalid rate %d\n", rate);
1687 if (channels == 0 || channels > 8) {
1688 snd_printdd("invalid channels %d\n", channels);
1691 val |= channels - 1;
1693 switch (snd_pcm_format_width(format)) {
1694 case 8: val |= 0x00; break;
1695 case 16: val |= 0x10; break;
1701 else if (maxbps >= 24)
1707 snd_printdd("invalid format width %d\n",
1708 snd_pcm_format_width(format));
1716 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
1717 * @codec: the HDA codec
1718 * @nid: NID to query
1719 * @ratesp: the pointer to store the detected rate bitflags
1720 * @formatsp: the pointer to store the detected formats
1721 * @bpsp: the pointer to store the detected format widths
1723 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
1724 * or @bsps argument is ignored.
1726 * Returns 0 if successful, otherwise a negative error code.
1728 int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
1729 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
1732 unsigned int val, streams;
1735 if (nid != codec->afg &&
1736 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD)) {
1737 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
1742 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
1746 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
1748 rates |= rate_bits[i].alsa_bits;
1753 if (formatsp || bpsp) {
1758 wcaps = get_wcaps(codec, nid);
1759 streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
1763 streams = snd_hda_param_read(codec, codec->afg,
1770 if (streams & AC_SUPFMT_PCM) {
1771 if (val & AC_SUPPCM_BITS_8) {
1772 formats |= SNDRV_PCM_FMTBIT_U8;
1775 if (val & AC_SUPPCM_BITS_16) {
1776 formats |= SNDRV_PCM_FMTBIT_S16_LE;
1779 if (wcaps & AC_WCAP_DIGITAL) {
1780 if (val & AC_SUPPCM_BITS_32)
1781 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
1782 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
1783 formats |= SNDRV_PCM_FMTBIT_S32_LE;
1784 if (val & AC_SUPPCM_BITS_24)
1786 else if (val & AC_SUPPCM_BITS_20)
1788 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
1789 AC_SUPPCM_BITS_32)) {
1790 formats |= SNDRV_PCM_FMTBIT_S32_LE;
1791 if (val & AC_SUPPCM_BITS_32)
1793 else if (val & AC_SUPPCM_BITS_24)
1795 else if (val & AC_SUPPCM_BITS_20)
1799 else if (streams == AC_SUPFMT_FLOAT32) {
1800 /* should be exclusive */
1801 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
1803 } else if (streams == AC_SUPFMT_AC3) {
1804 /* should be exclusive */
1805 /* temporary hack: we have still no proper support
1806 * for the direct AC3 stream...
1808 formats |= SNDRV_PCM_FMTBIT_U8;
1812 *formatsp = formats;
1821 * snd_hda_is_supported_format - check whether the given node supports
1824 * Returns 1 if supported, 0 if not.
1826 int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
1827 unsigned int format)
1830 unsigned int val = 0, rate, stream;
1832 if (nid != codec->afg &&
1833 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD)) {
1834 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
1839 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
1844 rate = format & 0xff00;
1845 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
1846 if (rate_bits[i].hda_fmt == rate) {
1851 if (i >= AC_PAR_PCM_RATE_BITS)
1854 stream = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
1857 if (!stream && nid != codec->afg)
1858 stream = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
1859 if (!stream || stream == -1)
1862 if (stream & AC_SUPFMT_PCM) {
1863 switch (format & 0xf0) {
1865 if (!(val & AC_SUPPCM_BITS_8))
1869 if (!(val & AC_SUPPCM_BITS_16))
1873 if (!(val & AC_SUPPCM_BITS_20))
1877 if (!(val & AC_SUPPCM_BITS_24))
1881 if (!(val & AC_SUPPCM_BITS_32))
1888 /* FIXME: check for float32 and AC3? */
1897 static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
1898 struct hda_codec *codec,
1899 struct snd_pcm_substream *substream)
1904 static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
1905 struct hda_codec *codec,
1906 unsigned int stream_tag,
1907 unsigned int format,
1908 struct snd_pcm_substream *substream)
1910 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
1914 static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
1915 struct hda_codec *codec,
1916 struct snd_pcm_substream *substream)
1918 snd_hda_codec_setup_stream(codec, hinfo->nid, 0, 0, 0);
1922 static int __devinit set_pcm_default_values(struct hda_codec *codec,
1923 struct hda_pcm_stream *info)
1925 /* query support PCM information from the given NID */
1926 if (info->nid && (!info->rates || !info->formats)) {
1927 snd_hda_query_supported_pcm(codec, info->nid,
1928 info->rates ? NULL : &info->rates,
1929 info->formats ? NULL : &info->formats,
1930 info->maxbps ? NULL : &info->maxbps);
1932 if (info->ops.open == NULL)
1933 info->ops.open = hda_pcm_default_open_close;
1934 if (info->ops.close == NULL)
1935 info->ops.close = hda_pcm_default_open_close;
1936 if (info->ops.prepare == NULL) {
1937 snd_assert(info->nid, return -EINVAL);
1938 info->ops.prepare = hda_pcm_default_prepare;
1940 if (info->ops.cleanup == NULL) {
1941 snd_assert(info->nid, return -EINVAL);
1942 info->ops.cleanup = hda_pcm_default_cleanup;
1948 * snd_hda_build_pcms - build PCM information
1951 * Create PCM information for each codec included in the bus.
1953 * The build_pcms codec patch is requested to set up codec->num_pcms and
1954 * codec->pcm_info properly. The array is referred by the top-level driver
1955 * to create its PCM instances.
1956 * The allocated codec->pcm_info should be released in codec->patch_ops.free
1959 * At least, substreams, channels_min and channels_max must be filled for
1960 * each stream. substreams = 0 indicates that the stream doesn't exist.
1961 * When rates and/or formats are zero, the supported values are queried
1962 * from the given nid. The nid is used also by the default ops.prepare
1963 * and ops.cleanup callbacks.
1965 * The driver needs to call ops.open in its open callback. Similarly,
1966 * ops.close is supposed to be called in the close callback.
1967 * ops.prepare should be called in the prepare or hw_params callback
1968 * with the proper parameters for set up.
1969 * ops.cleanup should be called in hw_free for clean up of streams.
1971 * This function returns 0 if successfull, or a negative error code.
1973 int __devinit snd_hda_build_pcms(struct hda_bus *bus)
1975 struct hda_codec *codec;
1977 list_for_each_entry(codec, &bus->codec_list, list) {
1978 unsigned int pcm, s;
1980 if (!codec->patch_ops.build_pcms)
1982 err = codec->patch_ops.build_pcms(codec);
1985 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
1986 for (s = 0; s < 2; s++) {
1987 struct hda_pcm_stream *info;
1988 info = &codec->pcm_info[pcm].stream[s];
1989 if (!info->substreams)
1991 err = set_pcm_default_values(codec, info);
2001 * snd_hda_check_board_config - compare the current codec with the config table
2002 * @codec: the HDA codec
2003 * @num_configs: number of config enums
2004 * @models: array of model name strings
2005 * @tbl: configuration table, terminated by null entries
2007 * Compares the modelname or PCI subsystem id of the current codec with the
2008 * given configuration table. If a matching entry is found, returns its
2009 * config value (supposed to be 0 or positive).
2011 * If no entries are matching, the function returns a negative value.
2013 int snd_hda_check_board_config(struct hda_codec *codec,
2014 int num_configs, const char **models,
2015 const struct snd_pci_quirk *tbl)
2017 if (codec->bus->modelname && models) {
2019 for (i = 0; i < num_configs; i++) {
2021 !strcmp(codec->bus->modelname, models[i])) {
2022 snd_printd(KERN_INFO "hda_codec: model '%s' is "
2023 "selected\n", models[i]);
2029 if (!codec->bus->pci || !tbl)
2032 tbl = snd_pci_quirk_lookup(codec->bus->pci, tbl);
2035 if (tbl->value >= 0 && tbl->value < num_configs) {
2036 #ifdef CONFIG_SND_DEBUG_DETECT
2038 const char *model = NULL;
2040 model = models[tbl->value];
2042 sprintf(tmp, "#%d", tbl->value);
2045 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
2046 "for config %x:%x (%s)\n",
2047 model, tbl->subvendor, tbl->subdevice,
2048 (tbl->name ? tbl->name : "Unknown device"));
2056 * snd_hda_add_new_ctls - create controls from the array
2057 * @codec: the HDA codec
2058 * @knew: the array of struct snd_kcontrol_new
2060 * This helper function creates and add new controls in the given array.
2061 * The array must be terminated with an empty entry as terminator.
2063 * Returns 0 if successful, or a negative error code.
2065 int snd_hda_add_new_ctls(struct hda_codec *codec, struct snd_kcontrol_new *knew)
2069 for (; knew->name; knew++) {
2070 struct snd_kcontrol *kctl;
2071 kctl = snd_ctl_new1(knew, codec);
2074 err = snd_ctl_add(codec->bus->card, kctl);
2078 kctl = snd_ctl_new1(knew, codec);
2081 kctl->id.device = codec->addr;
2082 err = snd_ctl_add(codec->bus->card, kctl);
2092 * Channel mode helper
2094 int snd_hda_ch_mode_info(struct hda_codec *codec,
2095 struct snd_ctl_elem_info *uinfo,
2096 const struct hda_channel_mode *chmode,
2099 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2101 uinfo->value.enumerated.items = num_chmodes;
2102 if (uinfo->value.enumerated.item >= num_chmodes)
2103 uinfo->value.enumerated.item = num_chmodes - 1;
2104 sprintf(uinfo->value.enumerated.name, "%dch",
2105 chmode[uinfo->value.enumerated.item].channels);
2109 int snd_hda_ch_mode_get(struct hda_codec *codec,
2110 struct snd_ctl_elem_value *ucontrol,
2111 const struct hda_channel_mode *chmode,
2117 for (i = 0; i < num_chmodes; i++) {
2118 if (max_channels == chmode[i].channels) {
2119 ucontrol->value.enumerated.item[0] = i;
2126 int snd_hda_ch_mode_put(struct hda_codec *codec,
2127 struct snd_ctl_elem_value *ucontrol,
2128 const struct hda_channel_mode *chmode,
2134 mode = ucontrol->value.enumerated.item[0];
2135 snd_assert(mode < num_chmodes, return -EINVAL);
2136 if (*max_channelsp == chmode[mode].channels)
2138 /* change the current channel setting */
2139 *max_channelsp = chmode[mode].channels;
2140 if (chmode[mode].sequence)
2141 snd_hda_sequence_write_cache(codec, chmode[mode].sequence);
2148 int snd_hda_input_mux_info(const struct hda_input_mux *imux,
2149 struct snd_ctl_elem_info *uinfo)
2153 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2155 uinfo->value.enumerated.items = imux->num_items;
2156 index = uinfo->value.enumerated.item;
2157 if (index >= imux->num_items)
2158 index = imux->num_items - 1;
2159 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
2163 int snd_hda_input_mux_put(struct hda_codec *codec,
2164 const struct hda_input_mux *imux,
2165 struct snd_ctl_elem_value *ucontrol,
2167 unsigned int *cur_val)
2171 idx = ucontrol->value.enumerated.item[0];
2172 if (idx >= imux->num_items)
2173 idx = imux->num_items - 1;
2174 if (*cur_val == idx)
2176 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
2177 imux->items[idx].index);
2184 * Multi-channel / digital-out PCM helper functions
2187 /* setup SPDIF output stream */
2188 static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
2189 unsigned int stream_tag, unsigned int format)
2191 /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */
2192 if (codec->spdif_ctls & AC_DIG1_ENABLE)
2193 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_DIGI_CONVERT_1,
2194 codec->spdif_ctls & ~AC_DIG1_ENABLE & 0xff);
2195 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
2196 /* turn on again (if needed) */
2197 if (codec->spdif_ctls & AC_DIG1_ENABLE)
2198 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_DIGI_CONVERT_1,
2199 codec->spdif_ctls & 0xff);
2203 * open the digital out in the exclusive mode
2205 int snd_hda_multi_out_dig_open(struct hda_codec *codec,
2206 struct hda_multi_out *mout)
2208 mutex_lock(&codec->spdif_mutex);
2209 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
2210 /* already opened as analog dup; reset it once */
2211 snd_hda_codec_setup_stream(codec, mout->dig_out_nid, 0, 0, 0);
2212 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
2213 mutex_unlock(&codec->spdif_mutex);
2217 int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
2218 struct hda_multi_out *mout,
2219 unsigned int stream_tag,
2220 unsigned int format,
2221 struct snd_pcm_substream *substream)
2223 mutex_lock(&codec->spdif_mutex);
2224 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
2225 mutex_unlock(&codec->spdif_mutex);
2230 * release the digital out
2232 int snd_hda_multi_out_dig_close(struct hda_codec *codec,
2233 struct hda_multi_out *mout)
2235 mutex_lock(&codec->spdif_mutex);
2236 mout->dig_out_used = 0;
2237 mutex_unlock(&codec->spdif_mutex);
2242 * set up more restrictions for analog out
2244 int snd_hda_multi_out_analog_open(struct hda_codec *codec,
2245 struct hda_multi_out *mout,
2246 struct snd_pcm_substream *substream)
2248 substream->runtime->hw.channels_max = mout->max_channels;
2249 return snd_pcm_hw_constraint_step(substream->runtime, 0,
2250 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
2254 * set up the i/o for analog out
2255 * when the digital out is available, copy the front out to digital out, too.
2257 int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
2258 struct hda_multi_out *mout,
2259 unsigned int stream_tag,
2260 unsigned int format,
2261 struct snd_pcm_substream *substream)
2263 hda_nid_t *nids = mout->dac_nids;
2264 int chs = substream->runtime->channels;
2267 mutex_lock(&codec->spdif_mutex);
2268 if (mout->dig_out_nid && mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
2270 snd_hda_is_supported_format(codec, mout->dig_out_nid,
2272 !(codec->spdif_status & IEC958_AES0_NONAUDIO)) {
2273 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
2274 setup_dig_out_stream(codec, mout->dig_out_nid,
2275 stream_tag, format);
2277 mout->dig_out_used = 0;
2278 snd_hda_codec_setup_stream(codec, mout->dig_out_nid,
2282 mutex_unlock(&codec->spdif_mutex);
2285 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
2287 if (mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
2288 /* headphone out will just decode front left/right (stereo) */
2289 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
2291 /* extra outputs copied from front */
2292 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
2293 if (mout->extra_out_nid[i])
2294 snd_hda_codec_setup_stream(codec,
2295 mout->extra_out_nid[i],
2296 stream_tag, 0, format);
2299 for (i = 1; i < mout->num_dacs; i++) {
2300 if (chs >= (i + 1) * 2) /* independent out */
2301 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
2303 else /* copy front */
2304 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
2311 * clean up the setting for analog out
2313 int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
2314 struct hda_multi_out *mout)
2316 hda_nid_t *nids = mout->dac_nids;
2319 for (i = 0; i < mout->num_dacs; i++)
2320 snd_hda_codec_setup_stream(codec, nids[i], 0, 0, 0);
2322 snd_hda_codec_setup_stream(codec, mout->hp_nid, 0, 0, 0);
2323 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
2324 if (mout->extra_out_nid[i])
2325 snd_hda_codec_setup_stream(codec,
2326 mout->extra_out_nid[i],
2328 mutex_lock(&codec->spdif_mutex);
2329 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
2330 snd_hda_codec_setup_stream(codec, mout->dig_out_nid, 0, 0, 0);
2331 mout->dig_out_used = 0;
2333 mutex_unlock(&codec->spdif_mutex);
2338 * Helper for automatic ping configuration
2341 static int is_in_nid_list(hda_nid_t nid, hda_nid_t *list)
2343 for (; *list; list++)
2351 * Sort an associated group of pins according to their sequence numbers.
2353 static void sort_pins_by_sequence(hda_nid_t * pins, short * sequences,
2360 for (i = 0; i < num_pins; i++) {
2361 for (j = i + 1; j < num_pins; j++) {
2362 if (sequences[i] > sequences[j]) {
2364 sequences[i] = sequences[j];
2376 * Parse all pin widgets and store the useful pin nids to cfg
2378 * The number of line-outs or any primary output is stored in line_outs,
2379 * and the corresponding output pins are assigned to line_out_pins[],
2380 * in the order of front, rear, CLFE, side, ...
2382 * If more extra outputs (speaker and headphone) are found, the pins are
2383 * assisnged to hp_pins[] and speaker_pins[], respectively. If no line-out jack
2384 * is detected, one of speaker of HP pins is assigned as the primary
2385 * output, i.e. to line_out_pins[0]. So, line_outs is always positive
2386 * if any analog output exists.
2388 * The analog input pins are assigned to input_pins array.
2389 * The digital input/output pins are assigned to dig_in_pin and dig_out_pin,
2392 int snd_hda_parse_pin_def_config(struct hda_codec *codec,
2393 struct auto_pin_cfg *cfg,
2394 hda_nid_t *ignore_nids)
2396 hda_nid_t nid, nid_start;
2398 short seq, assoc_line_out, assoc_speaker;
2399 short sequences_line_out[ARRAY_SIZE(cfg->line_out_pins)];
2400 short sequences_speaker[ARRAY_SIZE(cfg->speaker_pins)];
2402 memset(cfg, 0, sizeof(*cfg));
2404 memset(sequences_line_out, 0, sizeof(sequences_line_out));
2405 memset(sequences_speaker, 0, sizeof(sequences_speaker));
2406 assoc_line_out = assoc_speaker = 0;
2408 nodes = snd_hda_get_sub_nodes(codec, codec->afg, &nid_start);
2409 for (nid = nid_start; nid < nodes + nid_start; nid++) {
2410 unsigned int wid_caps = get_wcaps(codec, nid);
2411 unsigned int wid_type =
2412 (wid_caps & AC_WCAP_TYPE) >> AC_WCAP_TYPE_SHIFT;
2413 unsigned int def_conf;
2416 /* read all default configuration for pin complex */
2417 if (wid_type != AC_WID_PIN)
2419 /* ignore the given nids (e.g. pc-beep returns error) */
2420 if (ignore_nids && is_in_nid_list(nid, ignore_nids))
2423 def_conf = snd_hda_codec_read(codec, nid, 0,
2424 AC_VERB_GET_CONFIG_DEFAULT, 0);
2425 if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE)
2427 loc = get_defcfg_location(def_conf);
2428 switch (get_defcfg_device(def_conf)) {
2429 case AC_JACK_LINE_OUT:
2430 seq = get_defcfg_sequence(def_conf);
2431 assoc = get_defcfg_association(def_conf);
2434 if (!assoc_line_out)
2435 assoc_line_out = assoc;
2436 else if (assoc_line_out != assoc)
2438 if (cfg->line_outs >= ARRAY_SIZE(cfg->line_out_pins))
2440 cfg->line_out_pins[cfg->line_outs] = nid;
2441 sequences_line_out[cfg->line_outs] = seq;
2444 case AC_JACK_SPEAKER:
2445 seq = get_defcfg_sequence(def_conf);
2446 assoc = get_defcfg_association(def_conf);
2449 if (! assoc_speaker)
2450 assoc_speaker = assoc;
2451 else if (assoc_speaker != assoc)
2453 if (cfg->speaker_outs >= ARRAY_SIZE(cfg->speaker_pins))
2455 cfg->speaker_pins[cfg->speaker_outs] = nid;
2456 sequences_speaker[cfg->speaker_outs] = seq;
2457 cfg->speaker_outs++;
2459 case AC_JACK_HP_OUT:
2460 if (cfg->hp_outs >= ARRAY_SIZE(cfg->hp_pins))
2462 cfg->hp_pins[cfg->hp_outs] = nid;
2465 case AC_JACK_MIC_IN: {
2467 if (loc == AC_JACK_LOC_FRONT) {
2468 preferred = AUTO_PIN_FRONT_MIC;
2471 preferred = AUTO_PIN_MIC;
2472 alt = AUTO_PIN_FRONT_MIC;
2474 if (!cfg->input_pins[preferred])
2475 cfg->input_pins[preferred] = nid;
2476 else if (!cfg->input_pins[alt])
2477 cfg->input_pins[alt] = nid;
2480 case AC_JACK_LINE_IN:
2481 if (loc == AC_JACK_LOC_FRONT)
2482 cfg->input_pins[AUTO_PIN_FRONT_LINE] = nid;
2484 cfg->input_pins[AUTO_PIN_LINE] = nid;
2487 cfg->input_pins[AUTO_PIN_CD] = nid;
2490 cfg->input_pins[AUTO_PIN_AUX] = nid;
2492 case AC_JACK_SPDIF_OUT:
2493 cfg->dig_out_pin = nid;
2495 case AC_JACK_SPDIF_IN:
2496 cfg->dig_in_pin = nid;
2501 /* sort by sequence */
2502 sort_pins_by_sequence(cfg->line_out_pins, sequences_line_out,
2504 sort_pins_by_sequence(cfg->speaker_pins, sequences_speaker,
2508 * FIX-UP: if no line-outs are detected, try to use speaker or HP pin
2509 * as a primary output
2511 if (!cfg->line_outs) {
2512 if (cfg->speaker_outs) {
2513 cfg->line_outs = cfg->speaker_outs;
2514 memcpy(cfg->line_out_pins, cfg->speaker_pins,
2515 sizeof(cfg->speaker_pins));
2516 cfg->speaker_outs = 0;
2517 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
2518 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
2519 } else if (cfg->hp_outs) {
2520 cfg->line_outs = cfg->hp_outs;
2521 memcpy(cfg->line_out_pins, cfg->hp_pins,
2522 sizeof(cfg->hp_pins));
2524 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
2525 cfg->line_out_type = AUTO_PIN_HP_OUT;
2529 /* Reorder the surround channels
2530 * ALSA sequence is front/surr/clfe/side
2532 * 4-ch: front/surr => OK as it is
2533 * 6-ch: front/clfe/surr
2534 * 8-ch: front/clfe/rear/side|fc
2536 switch (cfg->line_outs) {
2539 nid = cfg->line_out_pins[1];
2540 cfg->line_out_pins[1] = cfg->line_out_pins[2];
2541 cfg->line_out_pins[2] = nid;
2546 * debug prints of the parsed results
2548 snd_printd("autoconfig: line_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
2549 cfg->line_outs, cfg->line_out_pins[0], cfg->line_out_pins[1],
2550 cfg->line_out_pins[2], cfg->line_out_pins[3],
2551 cfg->line_out_pins[4]);
2552 snd_printd(" speaker_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
2553 cfg->speaker_outs, cfg->speaker_pins[0],
2554 cfg->speaker_pins[1], cfg->speaker_pins[2],
2555 cfg->speaker_pins[3], cfg->speaker_pins[4]);
2556 snd_printd(" hp_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
2557 cfg->hp_outs, cfg->hp_pins[0],
2558 cfg->hp_pins[1], cfg->hp_pins[2],
2559 cfg->hp_pins[3], cfg->hp_pins[4]);
2560 snd_printd(" inputs: mic=0x%x, fmic=0x%x, line=0x%x, fline=0x%x,"
2561 " cd=0x%x, aux=0x%x\n",
2562 cfg->input_pins[AUTO_PIN_MIC],
2563 cfg->input_pins[AUTO_PIN_FRONT_MIC],
2564 cfg->input_pins[AUTO_PIN_LINE],
2565 cfg->input_pins[AUTO_PIN_FRONT_LINE],
2566 cfg->input_pins[AUTO_PIN_CD],
2567 cfg->input_pins[AUTO_PIN_AUX]);
2572 /* labels for input pins */
2573 const char *auto_pin_cfg_labels[AUTO_PIN_LAST] = {
2574 "Mic", "Front Mic", "Line", "Front Line", "CD", "Aux"
2584 * snd_hda_suspend - suspend the codecs
2586 * @state: suspsend state
2588 * Returns 0 if successful.
2590 int snd_hda_suspend(struct hda_bus *bus, pm_message_t state)
2592 struct hda_codec *codec;
2594 /* FIXME: should handle power widget capabilities */
2595 list_for_each_entry(codec, &bus->codec_list, list) {
2596 if (codec->patch_ops.suspend)
2597 codec->patch_ops.suspend(codec, state);
2598 hda_set_power_state(codec,
2599 codec->afg ? codec->afg : codec->mfg,
2606 * snd_hda_resume - resume the codecs
2608 * @state: resume state
2610 * Returns 0 if successful.
2612 int snd_hda_resume(struct hda_bus *bus)
2614 struct hda_codec *codec;
2616 list_for_each_entry(codec, &bus->codec_list, list) {
2617 hda_set_power_state(codec,
2618 codec->afg ? codec->afg : codec->mfg,
2620 if (codec->patch_ops.resume)
2621 codec->patch_ops.resume(codec);
2623 codec->patch_ops.init(codec);
2624 snd_hda_codec_resume_amp(codec);
2625 snd_hda_codec_resume_cache(codec);