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 <linux/init.h>
23 #include <linux/delay.h>
24 #include <linux/slab.h>
25 #include <linux/pci.h>
26 #include <linux/mutex.h>
27 #include <sound/core.h>
28 #include "hda_codec.h"
29 #include <sound/asoundef.h>
30 #include <sound/tlv.h>
31 #include <sound/initval.h>
32 #include "hda_local.h"
33 #include <sound/hda_hwdep.h>
34 #include "hda_patch.h" /* codec presets */
36 #ifdef CONFIG_SND_HDA_POWER_SAVE
37 /* define this option here to hide as static */
38 static int power_save = CONFIG_SND_HDA_POWER_SAVE_DEFAULT;
39 module_param(power_save, int, 0644);
40 MODULE_PARM_DESC(power_save, "Automatic power-saving timeout "
41 "(in second, 0 = disable).");
45 * vendor / preset table
48 struct hda_vendor_id {
53 /* codec vendor labels */
54 static struct hda_vendor_id hda_vendor_ids[] = {
56 { 0x1057, "Motorola" },
57 { 0x1095, "Silicon Image" },
58 { 0x10ec, "Realtek" },
62 { 0x11d4, "Analog Devices" },
63 { 0x13f6, "C-Media" },
64 { 0x14f1, "Conexant" },
65 { 0x17e8, "Chrontel" },
67 { 0x434d, "C-Media" },
68 { 0x8384, "SigmaTel" },
72 static const struct hda_codec_preset *hda_preset_tables[] = {
73 #ifdef CONFIG_SND_HDA_CODEC_REALTEK
74 snd_hda_preset_realtek,
76 #ifdef CONFIG_SND_HDA_CODEC_CMEDIA
77 snd_hda_preset_cmedia,
79 #ifdef CONFIG_SND_HDA_CODEC_ANALOG
80 snd_hda_preset_analog,
82 #ifdef CONFIG_SND_HDA_CODEC_SIGMATEL
83 snd_hda_preset_sigmatel,
85 #ifdef CONFIG_SND_HDA_CODEC_SI3054
86 snd_hda_preset_si3054,
88 #ifdef CONFIG_SND_HDA_CODEC_ATIHDMI
89 snd_hda_preset_atihdmi,
91 #ifdef CONFIG_SND_HDA_CODEC_CONEXANT
92 snd_hda_preset_conexant,
94 #ifdef CONFIG_SND_HDA_CODEC_VIA
97 #ifdef CONFIG_SND_HDA_CODEC_NVHDMI
98 snd_hda_preset_nvhdmi,
103 #ifdef CONFIG_SND_HDA_POWER_SAVE
104 static void hda_power_work(struct work_struct *work);
105 static void hda_keep_power_on(struct hda_codec *codec);
107 static inline void hda_keep_power_on(struct hda_codec *codec) {}
111 * snd_hda_codec_read - send a command and get the response
112 * @codec: the HDA codec
113 * @nid: NID to send the command
114 * @direct: direct flag
115 * @verb: the verb to send
116 * @parm: the parameter for the verb
118 * Send a single command and read the corresponding response.
120 * Returns the obtained response value, or -1 for an error.
122 unsigned int snd_hda_codec_read(struct hda_codec *codec, hda_nid_t nid,
124 unsigned int verb, unsigned int parm)
127 snd_hda_power_up(codec);
128 mutex_lock(&codec->bus->cmd_mutex);
129 if (!codec->bus->ops.command(codec, nid, direct, verb, parm))
130 res = codec->bus->ops.get_response(codec);
132 res = (unsigned int)-1;
133 mutex_unlock(&codec->bus->cmd_mutex);
134 snd_hda_power_down(codec);
139 * snd_hda_codec_write - send a single command without waiting for response
140 * @codec: the HDA codec
141 * @nid: NID to send the command
142 * @direct: direct flag
143 * @verb: the verb to send
144 * @parm: the parameter for the verb
146 * Send a single command without waiting for response.
148 * Returns 0 if successful, or a negative error code.
150 int snd_hda_codec_write(struct hda_codec *codec, hda_nid_t nid, int direct,
151 unsigned int verb, unsigned int parm)
154 snd_hda_power_up(codec);
155 mutex_lock(&codec->bus->cmd_mutex);
156 err = codec->bus->ops.command(codec, nid, direct, verb, parm);
157 mutex_unlock(&codec->bus->cmd_mutex);
158 snd_hda_power_down(codec);
163 * snd_hda_sequence_write - sequence writes
164 * @codec: the HDA codec
165 * @seq: VERB array to send
167 * Send the commands sequentially from the given array.
168 * The array must be terminated with NID=0.
170 void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq)
172 for (; seq->nid; seq++)
173 snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param);
177 * snd_hda_get_sub_nodes - get the range of sub nodes
178 * @codec: the HDA codec
180 * @start_id: the pointer to store the start NID
182 * Parse the NID and store the start NID of its sub-nodes.
183 * Returns the number of sub-nodes.
185 int snd_hda_get_sub_nodes(struct hda_codec *codec, hda_nid_t nid,
190 parm = snd_hda_param_read(codec, nid, AC_PAR_NODE_COUNT);
193 *start_id = (parm >> 16) & 0x7fff;
194 return (int)(parm & 0x7fff);
198 * snd_hda_get_connections - get connection list
199 * @codec: the HDA codec
201 * @conn_list: connection list array
202 * @max_conns: max. number of connections to store
204 * Parses the connection list of the given widget and stores the list
207 * Returns the number of connections, or a negative error code.
209 int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
210 hda_nid_t *conn_list, int max_conns)
213 int i, conn_len, conns;
214 unsigned int shift, num_elems, mask;
217 if (snd_BUG_ON(!conn_list || max_conns <= 0))
220 parm = snd_hda_param_read(codec, nid, AC_PAR_CONNLIST_LEN);
221 if (parm & AC_CLIST_LONG) {
230 conn_len = parm & AC_CLIST_LENGTH;
231 mask = (1 << (shift-1)) - 1;
234 return 0; /* no connection */
237 /* single connection */
238 parm = snd_hda_codec_read(codec, nid, 0,
239 AC_VERB_GET_CONNECT_LIST, 0);
240 conn_list[0] = parm & mask;
244 /* multi connection */
247 for (i = 0; i < conn_len; i++) {
251 if (i % num_elems == 0)
252 parm = snd_hda_codec_read(codec, nid, 0,
253 AC_VERB_GET_CONNECT_LIST, i);
254 range_val = !!(parm & (1 << (shift-1))); /* ranges */
258 /* ranges between the previous and this one */
259 if (!prev_nid || prev_nid >= val) {
260 snd_printk(KERN_WARNING "hda_codec: "
261 "invalid dep_range_val %x:%x\n",
265 for (n = prev_nid + 1; n <= val; n++) {
266 if (conns >= max_conns) {
268 "Too many connections\n");
271 conn_list[conns++] = n;
274 if (conns >= max_conns) {
275 snd_printk(KERN_ERR "Too many connections\n");
278 conn_list[conns++] = val;
287 * snd_hda_queue_unsol_event - add an unsolicited event to queue
289 * @res: unsolicited event (lower 32bit of RIRB entry)
290 * @res_ex: codec addr and flags (upper 32bit or RIRB entry)
292 * Adds the given event to the queue. The events are processed in
293 * the workqueue asynchronously. Call this function in the interrupt
294 * hanlder when RIRB receives an unsolicited event.
296 * Returns 0 if successful, or a negative error code.
298 int snd_hda_queue_unsol_event(struct hda_bus *bus, u32 res, u32 res_ex)
300 struct hda_bus_unsolicited *unsol;
307 wp = (unsol->wp + 1) % HDA_UNSOL_QUEUE_SIZE;
311 unsol->queue[wp] = res;
312 unsol->queue[wp + 1] = res_ex;
314 schedule_work(&unsol->work);
320 * process queued unsolicited events
322 static void process_unsol_events(struct work_struct *work)
324 struct hda_bus_unsolicited *unsol =
325 container_of(work, struct hda_bus_unsolicited, work);
326 struct hda_bus *bus = unsol->bus;
327 struct hda_codec *codec;
328 unsigned int rp, caddr, res;
330 while (unsol->rp != unsol->wp) {
331 rp = (unsol->rp + 1) % HDA_UNSOL_QUEUE_SIZE;
334 res = unsol->queue[rp];
335 caddr = unsol->queue[rp + 1];
336 if (!(caddr & (1 << 4))) /* no unsolicited event? */
338 codec = bus->caddr_tbl[caddr & 0x0f];
339 if (codec && codec->patch_ops.unsol_event)
340 codec->patch_ops.unsol_event(codec, res);
345 * initialize unsolicited queue
347 static int __devinit init_unsol_queue(struct hda_bus *bus)
349 struct hda_bus_unsolicited *unsol;
351 if (bus->unsol) /* already initialized */
354 unsol = kzalloc(sizeof(*unsol), GFP_KERNEL);
356 snd_printk(KERN_ERR "hda_codec: "
357 "can't allocate unsolicited queue\n");
360 INIT_WORK(&unsol->work, process_unsol_events);
369 static void snd_hda_codec_free(struct hda_codec *codec);
371 static int snd_hda_bus_free(struct hda_bus *bus)
373 struct hda_codec *codec, *n;
378 flush_scheduled_work();
381 list_for_each_entry_safe(codec, n, &bus->codec_list, list) {
382 snd_hda_codec_free(codec);
384 if (bus->ops.private_free)
385 bus->ops.private_free(bus);
390 static int snd_hda_bus_dev_free(struct snd_device *device)
392 struct hda_bus *bus = device->device_data;
393 return snd_hda_bus_free(bus);
397 * snd_hda_bus_new - create a HDA bus
398 * @card: the card entry
399 * @temp: the template for hda_bus information
400 * @busp: the pointer to store the created bus instance
402 * Returns 0 if successful, or a negative error code.
404 int __devinit snd_hda_bus_new(struct snd_card *card,
405 const struct hda_bus_template *temp,
406 struct hda_bus **busp)
410 static struct snd_device_ops dev_ops = {
411 .dev_free = snd_hda_bus_dev_free,
414 if (snd_BUG_ON(!temp))
416 if (snd_BUG_ON(!temp->ops.command || !temp->ops.get_response))
422 bus = kzalloc(sizeof(*bus), GFP_KERNEL);
424 snd_printk(KERN_ERR "can't allocate struct hda_bus\n");
429 bus->private_data = temp->private_data;
430 bus->pci = temp->pci;
431 bus->modelname = temp->modelname;
432 bus->ops = temp->ops;
434 mutex_init(&bus->cmd_mutex);
435 INIT_LIST_HEAD(&bus->codec_list);
437 err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops);
439 snd_hda_bus_free(bus);
447 #ifdef CONFIG_SND_HDA_GENERIC
448 #define is_generic_config(codec) \
449 (codec->bus->modelname && !strcmp(codec->bus->modelname, "generic"))
451 #define is_generic_config(codec) 0
455 * find a matching codec preset
457 static const struct hda_codec_preset __devinit *
458 find_codec_preset(struct hda_codec *codec)
460 const struct hda_codec_preset **tbl, *preset;
462 if (is_generic_config(codec))
463 return NULL; /* use the generic parser */
465 for (tbl = hda_preset_tables; *tbl; tbl++) {
466 for (preset = *tbl; preset->id; preset++) {
467 u32 mask = preset->mask;
468 if (preset->afg && preset->afg != codec->afg)
470 if (preset->mfg && preset->mfg != codec->mfg)
474 if (preset->id == (codec->vendor_id & mask) &&
476 preset->rev == codec->revision_id))
484 * snd_hda_get_codec_name - store the codec name
486 void snd_hda_get_codec_name(struct hda_codec *codec,
487 char *name, int namelen)
489 const struct hda_vendor_id *c;
490 const char *vendor = NULL;
491 u16 vendor_id = codec->vendor_id >> 16;
494 for (c = hda_vendor_ids; c->id; c++) {
495 if (c->id == vendor_id) {
501 sprintf(tmp, "Generic %04x", vendor_id);
504 if (codec->preset && codec->preset->name)
505 snprintf(name, namelen, "%s %s", vendor, codec->preset->name);
507 snprintf(name, namelen, "%s ID %x", vendor,
508 codec->vendor_id & 0xffff);
512 * look for an AFG and MFG nodes
514 static void __devinit setup_fg_nodes(struct hda_codec *codec)
519 total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid);
520 for (i = 0; i < total_nodes; i++, nid++) {
522 func = snd_hda_param_read(codec, nid, AC_PAR_FUNCTION_TYPE);
523 switch (func & 0xff) {
524 case AC_GRP_AUDIO_FUNCTION:
527 case AC_GRP_MODEM_FUNCTION:
537 * read widget caps for each widget and store in cache
539 static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
544 codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node,
546 codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL);
549 nid = codec->start_nid;
550 for (i = 0; i < codec->num_nodes; i++, nid++)
551 codec->wcaps[i] = snd_hda_param_read(codec, nid,
552 AC_PAR_AUDIO_WIDGET_CAP);
557 static void init_hda_cache(struct hda_cache_rec *cache,
558 unsigned int record_size);
559 static void free_hda_cache(struct hda_cache_rec *cache);
564 static void snd_hda_codec_free(struct hda_codec *codec)
568 #ifdef CONFIG_SND_HDA_POWER_SAVE
569 cancel_delayed_work(&codec->power_work);
570 flush_scheduled_work();
572 list_del(&codec->list);
573 codec->bus->caddr_tbl[codec->addr] = NULL;
574 if (codec->patch_ops.free)
575 codec->patch_ops.free(codec);
576 free_hda_cache(&codec->amp_cache);
577 free_hda_cache(&codec->cmd_cache);
583 * snd_hda_codec_new - create a HDA codec
584 * @bus: the bus to assign
585 * @codec_addr: the codec address
586 * @codecp: the pointer to store the generated codec
588 * Returns 0 if successful, or a negative error code.
590 int __devinit snd_hda_codec_new(struct hda_bus *bus, unsigned int codec_addr,
591 struct hda_codec **codecp)
593 struct hda_codec *codec;
597 if (snd_BUG_ON(!bus))
599 if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS))
602 if (bus->caddr_tbl[codec_addr]) {
603 snd_printk(KERN_ERR "hda_codec: "
604 "address 0x%x is already occupied\n", codec_addr);
608 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
610 snd_printk(KERN_ERR "can't allocate struct hda_codec\n");
615 codec->addr = codec_addr;
616 mutex_init(&codec->spdif_mutex);
617 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
618 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
620 #ifdef CONFIG_SND_HDA_POWER_SAVE
621 INIT_DELAYED_WORK(&codec->power_work, hda_power_work);
622 /* snd_hda_codec_new() marks the codec as power-up, and leave it as is.
623 * the caller has to power down appropriatley after initialization
626 hda_keep_power_on(codec);
629 list_add_tail(&codec->list, &bus->codec_list);
630 bus->caddr_tbl[codec_addr] = codec;
632 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
634 if (codec->vendor_id == -1)
635 /* read again, hopefully the access method was corrected
636 * in the last read...
638 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
640 codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT,
641 AC_PAR_SUBSYSTEM_ID);
642 codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT,
645 setup_fg_nodes(codec);
646 if (!codec->afg && !codec->mfg) {
647 snd_printdd("hda_codec: no AFG or MFG node found\n");
648 snd_hda_codec_free(codec);
652 if (read_widget_caps(codec, codec->afg ? codec->afg : codec->mfg) < 0) {
653 snd_printk(KERN_ERR "hda_codec: cannot malloc\n");
654 snd_hda_codec_free(codec);
658 if (!codec->subsystem_id) {
659 hda_nid_t nid = codec->afg ? codec->afg : codec->mfg;
660 codec->subsystem_id =
661 snd_hda_codec_read(codec, nid, 0,
662 AC_VERB_GET_SUBSYSTEM_ID, 0);
665 codec->preset = find_codec_preset(codec);
666 /* audio codec should override the mixer name */
667 if (codec->afg || !*bus->card->mixername)
668 snd_hda_get_codec_name(codec, bus->card->mixername,
669 sizeof(bus->card->mixername));
671 if (is_generic_config(codec)) {
672 err = snd_hda_parse_generic_codec(codec);
675 if (codec->preset && codec->preset->patch) {
676 err = codec->preset->patch(codec);
680 /* call the default parser */
681 err = snd_hda_parse_generic_codec(codec);
683 printk(KERN_ERR "hda-codec: No codec parser is available\n");
687 snd_hda_codec_free(codec);
691 if (codec->patch_ops.unsol_event)
692 init_unsol_queue(bus);
694 snd_hda_codec_proc_new(codec);
695 #ifdef CONFIG_SND_HDA_HWDEP
696 snd_hda_create_hwdep(codec);
699 sprintf(component, "HDA:%08x,%08x,%08x", codec->vendor_id, codec->subsystem_id, codec->revision_id);
700 snd_component_add(codec->bus->card, component);
708 * snd_hda_codec_setup_stream - set up the codec for streaming
709 * @codec: the CODEC to set up
710 * @nid: the NID to set up
711 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
712 * @channel_id: channel id to pass, zero based.
713 * @format: stream format.
715 void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
717 int channel_id, int format)
722 snd_printdd("hda_codec_setup_stream: "
723 "NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
724 nid, stream_tag, channel_id, format);
725 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID,
726 (stream_tag << 4) | channel_id);
728 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, format);
731 void snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid)
736 snd_printdd("hda_codec_cleanup_stream: NID=0x%x\n", nid);
737 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
738 #if 0 /* keep the format */
740 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0);
745 * amp access functions
748 /* FIXME: more better hash key? */
749 #define HDA_HASH_KEY(nid,dir,idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
750 #define INFO_AMP_CAPS (1<<0)
751 #define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
753 /* initialize the hash table */
754 static void __devinit init_hda_cache(struct hda_cache_rec *cache,
755 unsigned int record_size)
757 memset(cache, 0, sizeof(*cache));
758 memset(cache->hash, 0xff, sizeof(cache->hash));
759 snd_array_init(&cache->buf, record_size, 64);
762 static void free_hda_cache(struct hda_cache_rec *cache)
764 snd_array_free(&cache->buf);
767 /* query the hash. allocate an entry if not found. */
768 static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache,
771 u16 idx = key % (u16)ARRAY_SIZE(cache->hash);
772 u16 cur = cache->hash[idx];
773 struct hda_cache_head *info_head = cache->buf.list;
774 struct hda_cache_head *info;
776 while (cur != 0xffff) {
777 info = &info_head[cur];
778 if (info->key == key)
783 /* add a new hash entry */
784 info = snd_array_new(&cache->buf);
787 info->next = cache->hash[idx];
788 cache->hash[idx] = cur;
793 /* query and allocate an amp hash entry */
794 static inline struct hda_amp_info *
795 get_alloc_amp_hash(struct hda_codec *codec, u32 key)
797 return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key);
801 * query AMP capabilities for the given widget and direction
803 u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
805 struct hda_amp_info *info;
807 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, 0));
810 if (!(info->head.val & INFO_AMP_CAPS)) {
811 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
813 info->amp_caps = snd_hda_param_read(codec, nid,
814 direction == HDA_OUTPUT ?
818 info->head.val |= INFO_AMP_CAPS;
820 return info->amp_caps;
823 int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
826 struct hda_amp_info *info;
828 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, dir, 0));
831 info->amp_caps = caps;
832 info->head.val |= INFO_AMP_CAPS;
837 * read the current volume to info
838 * if the cache exists, read the cache value.
840 static unsigned int get_vol_mute(struct hda_codec *codec,
841 struct hda_amp_info *info, hda_nid_t nid,
842 int ch, int direction, int index)
846 if (info->head.val & INFO_AMP_VOL(ch))
847 return info->vol[ch];
849 parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
850 parm |= direction == HDA_OUTPUT ? AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
852 val = snd_hda_codec_read(codec, nid, 0,
853 AC_VERB_GET_AMP_GAIN_MUTE, parm);
854 info->vol[ch] = val & 0xff;
855 info->head.val |= INFO_AMP_VOL(ch);
856 return info->vol[ch];
860 * write the current volume in info to the h/w and update the cache
862 static void put_vol_mute(struct hda_codec *codec, struct hda_amp_info *info,
863 hda_nid_t nid, int ch, int direction, int index,
868 parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
869 parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
870 parm |= index << AC_AMP_SET_INDEX_SHIFT;
872 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
877 * read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
879 int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
880 int direction, int index)
882 struct hda_amp_info *info;
883 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
886 return get_vol_mute(codec, info, nid, ch, direction, index);
890 * update the AMP value, mask = bit mask to set, val = the value
892 int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
893 int direction, int idx, int mask, int val)
895 struct hda_amp_info *info;
897 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, idx));
901 val |= get_vol_mute(codec, info, nid, ch, direction, idx) & ~mask;
902 if (info->vol[ch] == val)
904 put_vol_mute(codec, info, nid, ch, direction, idx, val);
909 * update the AMP stereo with the same mask and value
911 int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
912 int direction, int idx, int mask, int val)
915 for (ch = 0; ch < 2; ch++)
916 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
921 #ifdef SND_HDA_NEEDS_RESUME
922 /* resume the all amp commands from the cache */
923 void snd_hda_codec_resume_amp(struct hda_codec *codec)
925 struct hda_amp_info *buffer = codec->amp_cache.buf.list;
928 for (i = 0; i < codec->amp_cache.buf.used; i++, buffer++) {
929 u32 key = buffer->head.key;
931 unsigned int idx, dir, ch;
935 idx = (key >> 16) & 0xff;
936 dir = (key >> 24) & 0xff;
937 for (ch = 0; ch < 2; ch++) {
938 if (!(buffer->head.val & INFO_AMP_VOL(ch)))
940 put_vol_mute(codec, buffer, nid, ch, dir, idx,
945 #endif /* SND_HDA_NEEDS_RESUME */
948 int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
949 struct snd_ctl_elem_info *uinfo)
951 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
952 u16 nid = get_amp_nid(kcontrol);
953 u8 chs = get_amp_channels(kcontrol);
954 int dir = get_amp_direction(kcontrol);
957 caps = query_amp_caps(codec, nid, dir);
959 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
961 printk(KERN_WARNING "hda_codec: "
962 "num_steps = 0 for NID=0x%x (ctl = %s)\n", nid,
966 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
967 uinfo->count = chs == 3 ? 2 : 1;
968 uinfo->value.integer.min = 0;
969 uinfo->value.integer.max = caps;
973 int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
974 struct snd_ctl_elem_value *ucontrol)
976 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
977 hda_nid_t nid = get_amp_nid(kcontrol);
978 int chs = get_amp_channels(kcontrol);
979 int dir = get_amp_direction(kcontrol);
980 int idx = get_amp_index(kcontrol);
981 long *valp = ucontrol->value.integer.value;
984 *valp++ = snd_hda_codec_amp_read(codec, nid, 0, dir, idx)
987 *valp = snd_hda_codec_amp_read(codec, nid, 1, dir, idx)
992 int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
993 struct snd_ctl_elem_value *ucontrol)
995 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
996 hda_nid_t nid = get_amp_nid(kcontrol);
997 int chs = get_amp_channels(kcontrol);
998 int dir = get_amp_direction(kcontrol);
999 int idx = get_amp_index(kcontrol);
1000 long *valp = ucontrol->value.integer.value;
1003 snd_hda_power_up(codec);
1005 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
1010 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
1012 snd_hda_power_down(codec);
1016 int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
1017 unsigned int size, unsigned int __user *_tlv)
1019 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1020 hda_nid_t nid = get_amp_nid(kcontrol);
1021 int dir = get_amp_direction(kcontrol);
1022 u32 caps, val1, val2;
1024 if (size < 4 * sizeof(unsigned int))
1026 caps = query_amp_caps(codec, nid, dir);
1027 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
1028 val2 = (val2 + 1) * 25;
1029 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
1030 val1 = ((int)val1) * ((int)val2);
1031 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
1033 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
1035 if (put_user(val1, _tlv + 2))
1037 if (put_user(val2, _tlv + 3))
1043 * set (static) TLV for virtual master volume; recalculated as max 0dB
1045 void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
1051 caps = query_amp_caps(codec, nid, dir);
1052 nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1053 step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
1054 step = (step + 1) * 25;
1055 tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
1056 tlv[1] = 2 * sizeof(unsigned int);
1057 tlv[2] = -nums * step;
1061 /* find a mixer control element with the given name */
1062 static struct snd_kcontrol *
1063 _snd_hda_find_mixer_ctl(struct hda_codec *codec,
1064 const char *name, int idx)
1066 struct snd_ctl_elem_id id;
1067 memset(&id, 0, sizeof(id));
1068 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1070 strcpy(id.name, name);
1071 return snd_ctl_find_id(codec->bus->card, &id);
1074 struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
1077 return _snd_hda_find_mixer_ctl(codec, name, 0);
1080 /* create a virtual master control and add slaves */
1081 int snd_hda_add_vmaster(struct hda_codec *codec, char *name,
1082 unsigned int *tlv, const char **slaves)
1084 struct snd_kcontrol *kctl;
1088 for (s = slaves; *s && !snd_hda_find_mixer_ctl(codec, *s); s++)
1091 snd_printdd("No slave found for %s\n", name);
1094 kctl = snd_ctl_make_virtual_master(name, tlv);
1097 err = snd_ctl_add(codec->bus->card, kctl);
1101 for (s = slaves; *s; s++) {
1102 struct snd_kcontrol *sctl;
1104 sctl = snd_hda_find_mixer_ctl(codec, *s);
1106 snd_printdd("Cannot find slave %s, skipped\n", *s);
1109 err = snd_ctl_add_slave(kctl, sctl);
1117 int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
1118 struct snd_ctl_elem_info *uinfo)
1120 int chs = get_amp_channels(kcontrol);
1122 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1123 uinfo->count = chs == 3 ? 2 : 1;
1124 uinfo->value.integer.min = 0;
1125 uinfo->value.integer.max = 1;
1129 int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
1130 struct snd_ctl_elem_value *ucontrol)
1132 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1133 hda_nid_t nid = get_amp_nid(kcontrol);
1134 int chs = get_amp_channels(kcontrol);
1135 int dir = get_amp_direction(kcontrol);
1136 int idx = get_amp_index(kcontrol);
1137 long *valp = ucontrol->value.integer.value;
1140 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
1141 HDA_AMP_MUTE) ? 0 : 1;
1143 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
1144 HDA_AMP_MUTE) ? 0 : 1;
1148 int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
1149 struct snd_ctl_elem_value *ucontrol)
1151 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1152 hda_nid_t nid = get_amp_nid(kcontrol);
1153 int chs = get_amp_channels(kcontrol);
1154 int dir = get_amp_direction(kcontrol);
1155 int idx = get_amp_index(kcontrol);
1156 long *valp = ucontrol->value.integer.value;
1159 snd_hda_power_up(codec);
1161 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
1163 *valp ? 0 : HDA_AMP_MUTE);
1167 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
1169 *valp ? 0 : HDA_AMP_MUTE);
1170 #ifdef CONFIG_SND_HDA_POWER_SAVE
1171 if (codec->patch_ops.check_power_status)
1172 codec->patch_ops.check_power_status(codec, nid);
1174 snd_hda_power_down(codec);
1179 * bound volume controls
1181 * bind multiple volumes (# indices, from 0)
1184 #define AMP_VAL_IDX_SHIFT 19
1185 #define AMP_VAL_IDX_MASK (0x0f<<19)
1187 int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
1188 struct snd_ctl_elem_value *ucontrol)
1190 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1194 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
1195 pval = kcontrol->private_value;
1196 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
1197 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
1198 kcontrol->private_value = pval;
1199 mutex_unlock(&codec->spdif_mutex);
1203 int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
1204 struct snd_ctl_elem_value *ucontrol)
1206 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1208 int i, indices, err = 0, change = 0;
1210 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
1211 pval = kcontrol->private_value;
1212 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
1213 for (i = 0; i < indices; i++) {
1214 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
1215 (i << AMP_VAL_IDX_SHIFT);
1216 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
1221 kcontrol->private_value = pval;
1222 mutex_unlock(&codec->spdif_mutex);
1223 return err < 0 ? err : change;
1227 * generic bound volume/swtich controls
1229 int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
1230 struct snd_ctl_elem_info *uinfo)
1232 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1233 struct hda_bind_ctls *c;
1236 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
1237 c = (struct hda_bind_ctls *)kcontrol->private_value;
1238 kcontrol->private_value = *c->values;
1239 err = c->ops->info(kcontrol, uinfo);
1240 kcontrol->private_value = (long)c;
1241 mutex_unlock(&codec->spdif_mutex);
1245 int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
1246 struct snd_ctl_elem_value *ucontrol)
1248 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1249 struct hda_bind_ctls *c;
1252 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
1253 c = (struct hda_bind_ctls *)kcontrol->private_value;
1254 kcontrol->private_value = *c->values;
1255 err = c->ops->get(kcontrol, ucontrol);
1256 kcontrol->private_value = (long)c;
1257 mutex_unlock(&codec->spdif_mutex);
1261 int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
1262 struct snd_ctl_elem_value *ucontrol)
1264 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1265 struct hda_bind_ctls *c;
1266 unsigned long *vals;
1267 int err = 0, change = 0;
1269 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
1270 c = (struct hda_bind_ctls *)kcontrol->private_value;
1271 for (vals = c->values; *vals; vals++) {
1272 kcontrol->private_value = *vals;
1273 err = c->ops->put(kcontrol, ucontrol);
1278 kcontrol->private_value = (long)c;
1279 mutex_unlock(&codec->spdif_mutex);
1280 return err < 0 ? err : change;
1283 int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
1284 unsigned int size, unsigned int __user *tlv)
1286 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1287 struct hda_bind_ctls *c;
1290 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
1291 c = (struct hda_bind_ctls *)kcontrol->private_value;
1292 kcontrol->private_value = *c->values;
1293 err = c->ops->tlv(kcontrol, op_flag, size, tlv);
1294 kcontrol->private_value = (long)c;
1295 mutex_unlock(&codec->spdif_mutex);
1299 struct hda_ctl_ops snd_hda_bind_vol = {
1300 .info = snd_hda_mixer_amp_volume_info,
1301 .get = snd_hda_mixer_amp_volume_get,
1302 .put = snd_hda_mixer_amp_volume_put,
1303 .tlv = snd_hda_mixer_amp_tlv
1306 struct hda_ctl_ops snd_hda_bind_sw = {
1307 .info = snd_hda_mixer_amp_switch_info,
1308 .get = snd_hda_mixer_amp_switch_get,
1309 .put = snd_hda_mixer_amp_switch_put,
1310 .tlv = snd_hda_mixer_amp_tlv
1314 * SPDIF out controls
1317 static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
1318 struct snd_ctl_elem_info *uinfo)
1320 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1325 static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
1326 struct snd_ctl_elem_value *ucontrol)
1328 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
1329 IEC958_AES0_NONAUDIO |
1330 IEC958_AES0_CON_EMPHASIS_5015 |
1331 IEC958_AES0_CON_NOT_COPYRIGHT;
1332 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
1333 IEC958_AES1_CON_ORIGINAL;
1337 static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
1338 struct snd_ctl_elem_value *ucontrol)
1340 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
1341 IEC958_AES0_NONAUDIO |
1342 IEC958_AES0_PRO_EMPHASIS_5015;
1346 static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
1347 struct snd_ctl_elem_value *ucontrol)
1349 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1351 ucontrol->value.iec958.status[0] = codec->spdif_status & 0xff;
1352 ucontrol->value.iec958.status[1] = (codec->spdif_status >> 8) & 0xff;
1353 ucontrol->value.iec958.status[2] = (codec->spdif_status >> 16) & 0xff;
1354 ucontrol->value.iec958.status[3] = (codec->spdif_status >> 24) & 0xff;
1359 /* convert from SPDIF status bits to HDA SPDIF bits
1360 * bit 0 (DigEn) is always set zero (to be filled later)
1362 static unsigned short convert_from_spdif_status(unsigned int sbits)
1364 unsigned short val = 0;
1366 if (sbits & IEC958_AES0_PROFESSIONAL)
1367 val |= AC_DIG1_PROFESSIONAL;
1368 if (sbits & IEC958_AES0_NONAUDIO)
1369 val |= AC_DIG1_NONAUDIO;
1370 if (sbits & IEC958_AES0_PROFESSIONAL) {
1371 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
1372 IEC958_AES0_PRO_EMPHASIS_5015)
1373 val |= AC_DIG1_EMPHASIS;
1375 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
1376 IEC958_AES0_CON_EMPHASIS_5015)
1377 val |= AC_DIG1_EMPHASIS;
1378 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
1379 val |= AC_DIG1_COPYRIGHT;
1380 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
1381 val |= AC_DIG1_LEVEL;
1382 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
1387 /* convert to SPDIF status bits from HDA SPDIF bits
1389 static unsigned int convert_to_spdif_status(unsigned short val)
1391 unsigned int sbits = 0;
1393 if (val & AC_DIG1_NONAUDIO)
1394 sbits |= IEC958_AES0_NONAUDIO;
1395 if (val & AC_DIG1_PROFESSIONAL)
1396 sbits |= IEC958_AES0_PROFESSIONAL;
1397 if (sbits & IEC958_AES0_PROFESSIONAL) {
1398 if (sbits & AC_DIG1_EMPHASIS)
1399 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
1401 if (val & AC_DIG1_EMPHASIS)
1402 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
1403 if (!(val & AC_DIG1_COPYRIGHT))
1404 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
1405 if (val & AC_DIG1_LEVEL)
1406 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
1407 sbits |= val & (0x7f << 8);
1412 /* set digital convert verbs both for the given NID and its slaves */
1413 static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
1418 snd_hda_codec_write(codec, nid, 0, verb, val);
1419 d = codec->slave_dig_outs;
1423 snd_hda_codec_write(codec, *d, 0, verb, val);
1426 static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
1430 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1);
1432 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2);
1435 static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
1436 struct snd_ctl_elem_value *ucontrol)
1438 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1439 hda_nid_t nid = kcontrol->private_value;
1443 mutex_lock(&codec->spdif_mutex);
1444 codec->spdif_status = ucontrol->value.iec958.status[0] |
1445 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
1446 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
1447 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
1448 val = convert_from_spdif_status(codec->spdif_status);
1449 val |= codec->spdif_ctls & 1;
1450 change = codec->spdif_ctls != val;
1451 codec->spdif_ctls = val;
1454 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
1456 mutex_unlock(&codec->spdif_mutex);
1460 #define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
1462 static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
1463 struct snd_ctl_elem_value *ucontrol)
1465 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1467 ucontrol->value.integer.value[0] = codec->spdif_ctls & AC_DIG1_ENABLE;
1471 static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
1472 struct snd_ctl_elem_value *ucontrol)
1474 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1475 hda_nid_t nid = kcontrol->private_value;
1479 mutex_lock(&codec->spdif_mutex);
1480 val = codec->spdif_ctls & ~AC_DIG1_ENABLE;
1481 if (ucontrol->value.integer.value[0])
1482 val |= AC_DIG1_ENABLE;
1483 change = codec->spdif_ctls != val;
1485 codec->spdif_ctls = val;
1486 set_dig_out_convert(codec, nid, val & 0xff, -1);
1487 /* unmute amp switch (if any) */
1488 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
1489 (val & AC_DIG1_ENABLE))
1490 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
1493 mutex_unlock(&codec->spdif_mutex);
1497 static struct snd_kcontrol_new dig_mixes[] = {
1499 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1500 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1501 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,CON_MASK),
1502 .info = snd_hda_spdif_mask_info,
1503 .get = snd_hda_spdif_cmask_get,
1506 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1507 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1508 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,PRO_MASK),
1509 .info = snd_hda_spdif_mask_info,
1510 .get = snd_hda_spdif_pmask_get,
1513 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1514 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
1515 .info = snd_hda_spdif_mask_info,
1516 .get = snd_hda_spdif_default_get,
1517 .put = snd_hda_spdif_default_put,
1520 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1521 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,SWITCH),
1522 .info = snd_hda_spdif_out_switch_info,
1523 .get = snd_hda_spdif_out_switch_get,
1524 .put = snd_hda_spdif_out_switch_put,
1529 #define SPDIF_MAX_IDX 4 /* 4 instances should be enough to probe */
1532 * snd_hda_create_spdif_out_ctls - create Output SPDIF-related controls
1533 * @codec: the HDA codec
1534 * @nid: audio out widget NID
1536 * Creates controls related with the SPDIF output.
1537 * Called from each patch supporting the SPDIF out.
1539 * Returns 0 if successful, or a negative error code.
1541 int snd_hda_create_spdif_out_ctls(struct hda_codec *codec, hda_nid_t nid)
1544 struct snd_kcontrol *kctl;
1545 struct snd_kcontrol_new *dig_mix;
1548 for (idx = 0; idx < SPDIF_MAX_IDX; idx++) {
1549 if (!_snd_hda_find_mixer_ctl(codec, "IEC958 Playback Switch",
1553 if (idx >= SPDIF_MAX_IDX) {
1554 printk(KERN_ERR "hda_codec: too many IEC958 outputs\n");
1557 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
1558 kctl = snd_ctl_new1(dig_mix, codec);
1559 kctl->id.index = idx;
1560 kctl->private_value = nid;
1561 err = snd_ctl_add(codec->bus->card, kctl);
1566 snd_hda_codec_read(codec, nid, 0,
1567 AC_VERB_GET_DIGI_CONVERT_1, 0);
1568 codec->spdif_status = convert_to_spdif_status(codec->spdif_ctls);
1573 * SPDIF sharing with analog output
1575 static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
1576 struct snd_ctl_elem_value *ucontrol)
1578 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
1579 ucontrol->value.integer.value[0] = mout->share_spdif;
1583 static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
1584 struct snd_ctl_elem_value *ucontrol)
1586 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
1587 mout->share_spdif = !!ucontrol->value.integer.value[0];
1591 static struct snd_kcontrol_new spdif_share_sw = {
1592 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1593 .name = "IEC958 Default PCM Playback Switch",
1594 .info = snd_ctl_boolean_mono_info,
1595 .get = spdif_share_sw_get,
1596 .put = spdif_share_sw_put,
1599 int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
1600 struct hda_multi_out *mout)
1602 if (!mout->dig_out_nid)
1604 /* ATTENTION: here mout is passed as private_data, instead of codec */
1605 return snd_ctl_add(codec->bus->card,
1606 snd_ctl_new1(&spdif_share_sw, mout));
1613 #define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
1615 static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
1616 struct snd_ctl_elem_value *ucontrol)
1618 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1620 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
1624 static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
1625 struct snd_ctl_elem_value *ucontrol)
1627 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1628 hda_nid_t nid = kcontrol->private_value;
1629 unsigned int val = !!ucontrol->value.integer.value[0];
1632 mutex_lock(&codec->spdif_mutex);
1633 change = codec->spdif_in_enable != val;
1635 codec->spdif_in_enable = val;
1636 snd_hda_codec_write_cache(codec, nid, 0,
1637 AC_VERB_SET_DIGI_CONVERT_1, val);
1639 mutex_unlock(&codec->spdif_mutex);
1643 static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
1644 struct snd_ctl_elem_value *ucontrol)
1646 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1647 hda_nid_t nid = kcontrol->private_value;
1651 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0);
1652 sbits = convert_to_spdif_status(val);
1653 ucontrol->value.iec958.status[0] = sbits;
1654 ucontrol->value.iec958.status[1] = sbits >> 8;
1655 ucontrol->value.iec958.status[2] = sbits >> 16;
1656 ucontrol->value.iec958.status[3] = sbits >> 24;
1660 static struct snd_kcontrol_new dig_in_ctls[] = {
1662 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1663 .name = SNDRV_CTL_NAME_IEC958("",CAPTURE,SWITCH),
1664 .info = snd_hda_spdif_in_switch_info,
1665 .get = snd_hda_spdif_in_switch_get,
1666 .put = snd_hda_spdif_in_switch_put,
1669 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1670 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1671 .name = SNDRV_CTL_NAME_IEC958("",CAPTURE,DEFAULT),
1672 .info = snd_hda_spdif_mask_info,
1673 .get = snd_hda_spdif_in_status_get,
1679 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
1680 * @codec: the HDA codec
1681 * @nid: audio in widget NID
1683 * Creates controls related with the SPDIF input.
1684 * Called from each patch supporting the SPDIF in.
1686 * Returns 0 if successful, or a negative error code.
1688 int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
1691 struct snd_kcontrol *kctl;
1692 struct snd_kcontrol_new *dig_mix;
1695 for (idx = 0; idx < SPDIF_MAX_IDX; idx++) {
1696 if (!_snd_hda_find_mixer_ctl(codec, "IEC958 Capture Switch",
1700 if (idx >= SPDIF_MAX_IDX) {
1701 printk(KERN_ERR "hda_codec: too many IEC958 inputs\n");
1704 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
1705 kctl = snd_ctl_new1(dig_mix, codec);
1706 kctl->private_value = nid;
1707 err = snd_ctl_add(codec->bus->card, kctl);
1711 codec->spdif_in_enable =
1712 snd_hda_codec_read(codec, nid, 0,
1713 AC_VERB_GET_DIGI_CONVERT_1, 0) &
1718 #ifdef SND_HDA_NEEDS_RESUME
1723 /* build a 32bit cache key with the widget id and the command parameter */
1724 #define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
1725 #define get_cmd_cache_nid(key) ((key) & 0xff)
1726 #define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
1729 * snd_hda_codec_write_cache - send a single command with caching
1730 * @codec: the HDA codec
1731 * @nid: NID to send the command
1732 * @direct: direct flag
1733 * @verb: the verb to send
1734 * @parm: the parameter for the verb
1736 * Send a single command without waiting for response.
1738 * Returns 0 if successful, or a negative error code.
1740 int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
1741 int direct, unsigned int verb, unsigned int parm)
1744 snd_hda_power_up(codec);
1745 mutex_lock(&codec->bus->cmd_mutex);
1746 err = codec->bus->ops.command(codec, nid, direct, verb, parm);
1748 struct hda_cache_head *c;
1749 u32 key = build_cmd_cache_key(nid, verb);
1750 c = get_alloc_hash(&codec->cmd_cache, key);
1754 mutex_unlock(&codec->bus->cmd_mutex);
1755 snd_hda_power_down(codec);
1759 /* resume the all commands from the cache */
1760 void snd_hda_codec_resume_cache(struct hda_codec *codec)
1762 struct hda_cache_head *buffer = codec->cmd_cache.buf.list;
1765 for (i = 0; i < codec->cmd_cache.buf.used; i++, buffer++) {
1766 u32 key = buffer->key;
1769 snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
1770 get_cmd_cache_cmd(key), buffer->val);
1775 * snd_hda_sequence_write_cache - sequence writes with caching
1776 * @codec: the HDA codec
1777 * @seq: VERB array to send
1779 * Send the commands sequentially from the given array.
1780 * Thte commands are recorded on cache for power-save and resume.
1781 * The array must be terminated with NID=0.
1783 void snd_hda_sequence_write_cache(struct hda_codec *codec,
1784 const struct hda_verb *seq)
1786 for (; seq->nid; seq++)
1787 snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
1790 #endif /* SND_HDA_NEEDS_RESUME */
1793 * set power state of the codec
1795 static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
1796 unsigned int power_state)
1801 snd_hda_codec_write(codec, fg, 0, AC_VERB_SET_POWER_STATE,
1803 msleep(10); /* partial workaround for "azx_get_response timeout" */
1805 nid = codec->start_nid;
1806 for (i = 0; i < codec->num_nodes; i++, nid++) {
1807 unsigned int wcaps = get_wcaps(codec, nid);
1808 if (wcaps & AC_WCAP_POWER) {
1809 unsigned int wid_type = (wcaps & AC_WCAP_TYPE) >>
1811 if (wid_type == AC_WID_PIN) {
1812 unsigned int pincap;
1814 * don't power down the widget if it controls
1815 * eapd and EAPD_BTLENABLE is set.
1817 pincap = snd_hda_param_read(codec, nid,
1819 if (pincap & AC_PINCAP_EAPD) {
1820 int eapd = snd_hda_codec_read(codec,
1822 AC_VERB_GET_EAPD_BTLENABLE, 0);
1824 if (power_state == AC_PWRST_D3 && eapd)
1828 snd_hda_codec_write(codec, nid, 0,
1829 AC_VERB_SET_POWER_STATE,
1834 if (power_state == AC_PWRST_D0) {
1835 unsigned long end_time;
1838 /* wait until the codec reachs to D0 */
1839 end_time = jiffies + msecs_to_jiffies(500);
1841 state = snd_hda_codec_read(codec, fg, 0,
1842 AC_VERB_GET_POWER_STATE, 0);
1843 if (state == power_state)
1846 } while (time_after_eq(end_time, jiffies));
1850 #ifdef SND_HDA_NEEDS_RESUME
1852 * call suspend and power-down; used both from PM and power-save
1854 static void hda_call_codec_suspend(struct hda_codec *codec)
1856 if (codec->patch_ops.suspend)
1857 codec->patch_ops.suspend(codec, PMSG_SUSPEND);
1858 hda_set_power_state(codec,
1859 codec->afg ? codec->afg : codec->mfg,
1861 #ifdef CONFIG_SND_HDA_POWER_SAVE
1862 cancel_delayed_work(&codec->power_work);
1863 codec->power_on = 0;
1864 codec->power_transition = 0;
1869 * kick up codec; used both from PM and power-save
1871 static void hda_call_codec_resume(struct hda_codec *codec)
1873 hda_set_power_state(codec,
1874 codec->afg ? codec->afg : codec->mfg,
1876 if (codec->patch_ops.resume)
1877 codec->patch_ops.resume(codec);
1879 if (codec->patch_ops.init)
1880 codec->patch_ops.init(codec);
1881 snd_hda_codec_resume_amp(codec);
1882 snd_hda_codec_resume_cache(codec);
1885 #endif /* SND_HDA_NEEDS_RESUME */
1889 * snd_hda_build_controls - build mixer controls
1892 * Creates mixer controls for each codec included in the bus.
1894 * Returns 0 if successful, otherwise a negative error code.
1896 int __devinit snd_hda_build_controls(struct hda_bus *bus)
1898 struct hda_codec *codec;
1900 list_for_each_entry(codec, &bus->codec_list, list) {
1902 /* fake as if already powered-on */
1903 hda_keep_power_on(codec);
1905 hda_set_power_state(codec,
1906 codec->afg ? codec->afg : codec->mfg,
1908 /* continue to initialize... */
1909 if (codec->patch_ops.init)
1910 err = codec->patch_ops.init(codec);
1911 if (!err && codec->patch_ops.build_controls)
1912 err = codec->patch_ops.build_controls(codec);
1913 snd_hda_power_down(codec);
1924 struct hda_rate_tbl {
1926 unsigned int alsa_bits;
1927 unsigned int hda_fmt;
1930 static struct hda_rate_tbl rate_bits[] = {
1931 /* rate in Hz, ALSA rate bitmask, HDA format value */
1933 /* autodetected value used in snd_hda_query_supported_pcm */
1934 { 8000, SNDRV_PCM_RATE_8000, 0x0500 }, /* 1/6 x 48 */
1935 { 11025, SNDRV_PCM_RATE_11025, 0x4300 }, /* 1/4 x 44 */
1936 { 16000, SNDRV_PCM_RATE_16000, 0x0200 }, /* 1/3 x 48 */
1937 { 22050, SNDRV_PCM_RATE_22050, 0x4100 }, /* 1/2 x 44 */
1938 { 32000, SNDRV_PCM_RATE_32000, 0x0a00 }, /* 2/3 x 48 */
1939 { 44100, SNDRV_PCM_RATE_44100, 0x4000 }, /* 44 */
1940 { 48000, SNDRV_PCM_RATE_48000, 0x0000 }, /* 48 */
1941 { 88200, SNDRV_PCM_RATE_88200, 0x4800 }, /* 2 x 44 */
1942 { 96000, SNDRV_PCM_RATE_96000, 0x0800 }, /* 2 x 48 */
1943 { 176400, SNDRV_PCM_RATE_176400, 0x5800 },/* 4 x 44 */
1944 { 192000, SNDRV_PCM_RATE_192000, 0x1800 }, /* 4 x 48 */
1945 #define AC_PAR_PCM_RATE_BITS 11
1946 /* up to bits 10, 384kHZ isn't supported properly */
1948 /* not autodetected value */
1949 { 9600, SNDRV_PCM_RATE_KNOT, 0x0400 }, /* 1/5 x 48 */
1951 { 0 } /* terminator */
1955 * snd_hda_calc_stream_format - calculate format bitset
1956 * @rate: the sample rate
1957 * @channels: the number of channels
1958 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
1959 * @maxbps: the max. bps
1961 * Calculate the format bitset from the given rate, channels and th PCM format.
1963 * Return zero if invalid.
1965 unsigned int snd_hda_calc_stream_format(unsigned int rate,
1966 unsigned int channels,
1967 unsigned int format,
1968 unsigned int maxbps)
1971 unsigned int val = 0;
1973 for (i = 0; rate_bits[i].hz; i++)
1974 if (rate_bits[i].hz == rate) {
1975 val = rate_bits[i].hda_fmt;
1978 if (!rate_bits[i].hz) {
1979 snd_printdd("invalid rate %d\n", rate);
1983 if (channels == 0 || channels > 8) {
1984 snd_printdd("invalid channels %d\n", channels);
1987 val |= channels - 1;
1989 switch (snd_pcm_format_width(format)) {
1990 case 8: val |= 0x00; break;
1991 case 16: val |= 0x10; break;
1997 else if (maxbps >= 24)
2003 snd_printdd("invalid format width %d\n",
2004 snd_pcm_format_width(format));
2012 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
2013 * @codec: the HDA codec
2014 * @nid: NID to query
2015 * @ratesp: the pointer to store the detected rate bitflags
2016 * @formatsp: the pointer to store the detected formats
2017 * @bpsp: the pointer to store the detected format widths
2019 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
2020 * or @bsps argument is ignored.
2022 * Returns 0 if successful, otherwise a negative error code.
2024 int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
2025 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
2028 unsigned int val, streams;
2031 if (nid != codec->afg &&
2032 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD)) {
2033 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
2038 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
2042 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
2044 rates |= rate_bits[i].alsa_bits;
2049 if (formatsp || bpsp) {
2054 wcaps = get_wcaps(codec, nid);
2055 streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
2059 streams = snd_hda_param_read(codec, codec->afg,
2066 if (streams & AC_SUPFMT_PCM) {
2067 if (val & AC_SUPPCM_BITS_8) {
2068 formats |= SNDRV_PCM_FMTBIT_U8;
2071 if (val & AC_SUPPCM_BITS_16) {
2072 formats |= SNDRV_PCM_FMTBIT_S16_LE;
2075 if (wcaps & AC_WCAP_DIGITAL) {
2076 if (val & AC_SUPPCM_BITS_32)
2077 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
2078 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
2079 formats |= SNDRV_PCM_FMTBIT_S32_LE;
2080 if (val & AC_SUPPCM_BITS_24)
2082 else if (val & AC_SUPPCM_BITS_20)
2084 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
2085 AC_SUPPCM_BITS_32)) {
2086 formats |= SNDRV_PCM_FMTBIT_S32_LE;
2087 if (val & AC_SUPPCM_BITS_32)
2089 else if (val & AC_SUPPCM_BITS_24)
2091 else if (val & AC_SUPPCM_BITS_20)
2095 else if (streams == AC_SUPFMT_FLOAT32) {
2096 /* should be exclusive */
2097 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
2099 } else if (streams == AC_SUPFMT_AC3) {
2100 /* should be exclusive */
2101 /* temporary hack: we have still no proper support
2102 * for the direct AC3 stream...
2104 formats |= SNDRV_PCM_FMTBIT_U8;
2108 *formatsp = formats;
2117 * snd_hda_is_supported_format - check whether the given node supports
2120 * Returns 1 if supported, 0 if not.
2122 int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
2123 unsigned int format)
2126 unsigned int val = 0, rate, stream;
2128 if (nid != codec->afg &&
2129 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD)) {
2130 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
2135 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
2140 rate = format & 0xff00;
2141 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
2142 if (rate_bits[i].hda_fmt == rate) {
2147 if (i >= AC_PAR_PCM_RATE_BITS)
2150 stream = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
2153 if (!stream && nid != codec->afg)
2154 stream = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
2155 if (!stream || stream == -1)
2158 if (stream & AC_SUPFMT_PCM) {
2159 switch (format & 0xf0) {
2161 if (!(val & AC_SUPPCM_BITS_8))
2165 if (!(val & AC_SUPPCM_BITS_16))
2169 if (!(val & AC_SUPPCM_BITS_20))
2173 if (!(val & AC_SUPPCM_BITS_24))
2177 if (!(val & AC_SUPPCM_BITS_32))
2184 /* FIXME: check for float32 and AC3? */
2193 static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
2194 struct hda_codec *codec,
2195 struct snd_pcm_substream *substream)
2200 static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
2201 struct hda_codec *codec,
2202 unsigned int stream_tag,
2203 unsigned int format,
2204 struct snd_pcm_substream *substream)
2206 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
2210 static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
2211 struct hda_codec *codec,
2212 struct snd_pcm_substream *substream)
2214 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
2218 static int __devinit set_pcm_default_values(struct hda_codec *codec,
2219 struct hda_pcm_stream *info)
2221 /* query support PCM information from the given NID */
2222 if (info->nid && (!info->rates || !info->formats)) {
2223 snd_hda_query_supported_pcm(codec, info->nid,
2224 info->rates ? NULL : &info->rates,
2225 info->formats ? NULL : &info->formats,
2226 info->maxbps ? NULL : &info->maxbps);
2228 if (info->ops.open == NULL)
2229 info->ops.open = hda_pcm_default_open_close;
2230 if (info->ops.close == NULL)
2231 info->ops.close = hda_pcm_default_open_close;
2232 if (info->ops.prepare == NULL) {
2233 if (snd_BUG_ON(!info->nid))
2235 info->ops.prepare = hda_pcm_default_prepare;
2237 if (info->ops.cleanup == NULL) {
2238 if (snd_BUG_ON(!info->nid))
2240 info->ops.cleanup = hda_pcm_default_cleanup;
2246 * attach a new PCM stream
2248 static int __devinit
2249 snd_hda_attach_pcm(struct hda_codec *codec, struct hda_pcm *pcm)
2251 struct hda_pcm_stream *info;
2256 for (stream = 0; stream < 2; stream++) {
2257 info = &pcm->stream[stream];
2258 if (info->substreams) {
2259 err = set_pcm_default_values(codec, info);
2264 return codec->bus->ops.attach_pcm(codec, pcm);
2268 * snd_hda_build_pcms - build PCM information
2271 * Create PCM information for each codec included in the bus.
2273 * The build_pcms codec patch is requested to set up codec->num_pcms and
2274 * codec->pcm_info properly. The array is referred by the top-level driver
2275 * to create its PCM instances.
2276 * The allocated codec->pcm_info should be released in codec->patch_ops.free
2279 * At least, substreams, channels_min and channels_max must be filled for
2280 * each stream. substreams = 0 indicates that the stream doesn't exist.
2281 * When rates and/or formats are zero, the supported values are queried
2282 * from the given nid. The nid is used also by the default ops.prepare
2283 * and ops.cleanup callbacks.
2285 * The driver needs to call ops.open in its open callback. Similarly,
2286 * ops.close is supposed to be called in the close callback.
2287 * ops.prepare should be called in the prepare or hw_params callback
2288 * with the proper parameters for set up.
2289 * ops.cleanup should be called in hw_free for clean up of streams.
2291 * This function returns 0 if successfull, or a negative error code.
2293 int __devinit snd_hda_build_pcms(struct hda_bus *bus)
2295 static const char *dev_name[HDA_PCM_NTYPES] = {
2296 "Audio", "SPDIF", "HDMI", "Modem"
2298 /* starting device index for each PCM type */
2299 static int dev_idx[HDA_PCM_NTYPES] = {
2300 [HDA_PCM_TYPE_AUDIO] = 0,
2301 [HDA_PCM_TYPE_SPDIF] = 1,
2302 [HDA_PCM_TYPE_HDMI] = 3,
2303 [HDA_PCM_TYPE_MODEM] = 6
2305 /* normal audio device indices; not linear to keep compatibility */
2306 static int audio_idx[4] = { 0, 2, 4, 5 };
2307 struct hda_codec *codec;
2308 int num_devs[HDA_PCM_NTYPES];
2310 memset(num_devs, 0, sizeof(num_devs));
2311 list_for_each_entry(codec, &bus->codec_list, list) {
2314 if (!codec->patch_ops.build_pcms)
2316 err = codec->patch_ops.build_pcms(codec);
2319 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
2320 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
2321 int type = cpcm->pcm_type;
2323 case HDA_PCM_TYPE_AUDIO:
2324 if (num_devs[type] >= ARRAY_SIZE(audio_idx)) {
2325 snd_printk(KERN_WARNING
2326 "Too many audio devices\n");
2329 cpcm->device = audio_idx[num_devs[type]];
2331 case HDA_PCM_TYPE_SPDIF:
2332 case HDA_PCM_TYPE_HDMI:
2333 case HDA_PCM_TYPE_MODEM:
2334 if (num_devs[type]) {
2335 snd_printk(KERN_WARNING
2336 "%s already defined\n",
2340 cpcm->device = dev_idx[type];
2343 snd_printk(KERN_WARNING
2344 "Invalid PCM type %d\n", type);
2348 err = snd_hda_attach_pcm(codec, cpcm);
2357 * snd_hda_check_board_config - compare the current codec with the config table
2358 * @codec: the HDA codec
2359 * @num_configs: number of config enums
2360 * @models: array of model name strings
2361 * @tbl: configuration table, terminated by null entries
2363 * Compares the modelname or PCI subsystem id of the current codec with the
2364 * given configuration table. If a matching entry is found, returns its
2365 * config value (supposed to be 0 or positive).
2367 * If no entries are matching, the function returns a negative value.
2369 int snd_hda_check_board_config(struct hda_codec *codec,
2370 int num_configs, const char **models,
2371 const struct snd_pci_quirk *tbl)
2373 if (codec->bus->modelname && models) {
2375 for (i = 0; i < num_configs; i++) {
2377 !strcmp(codec->bus->modelname, models[i])) {
2378 snd_printd(KERN_INFO "hda_codec: model '%s' is "
2379 "selected\n", models[i]);
2385 if (!codec->bus->pci || !tbl)
2388 tbl = snd_pci_quirk_lookup(codec->bus->pci, tbl);
2391 if (tbl->value >= 0 && tbl->value < num_configs) {
2392 #ifdef CONFIG_SND_DEBUG_VERBOSE
2394 const char *model = NULL;
2396 model = models[tbl->value];
2398 sprintf(tmp, "#%d", tbl->value);
2401 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
2402 "for config %x:%x (%s)\n",
2403 model, tbl->subvendor, tbl->subdevice,
2404 (tbl->name ? tbl->name : "Unknown device"));
2412 * snd_hda_add_new_ctls - create controls from the array
2413 * @codec: the HDA codec
2414 * @knew: the array of struct snd_kcontrol_new
2416 * This helper function creates and add new controls in the given array.
2417 * The array must be terminated with an empty entry as terminator.
2419 * Returns 0 if successful, or a negative error code.
2421 int snd_hda_add_new_ctls(struct hda_codec *codec, struct snd_kcontrol_new *knew)
2425 for (; knew->name; knew++) {
2426 struct snd_kcontrol *kctl;
2427 kctl = snd_ctl_new1(knew, codec);
2430 err = snd_ctl_add(codec->bus->card, kctl);
2434 kctl = snd_ctl_new1(knew, codec);
2437 kctl->id.device = codec->addr;
2438 err = snd_ctl_add(codec->bus->card, kctl);
2446 #ifdef CONFIG_SND_HDA_POWER_SAVE
2447 static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
2448 unsigned int power_state);
2450 static void hda_power_work(struct work_struct *work)
2452 struct hda_codec *codec =
2453 container_of(work, struct hda_codec, power_work.work);
2455 if (!codec->power_on || codec->power_count) {
2456 codec->power_transition = 0;
2460 hda_call_codec_suspend(codec);
2461 if (codec->bus->ops.pm_notify)
2462 codec->bus->ops.pm_notify(codec);
2465 static void hda_keep_power_on(struct hda_codec *codec)
2467 codec->power_count++;
2468 codec->power_on = 1;
2471 void snd_hda_power_up(struct hda_codec *codec)
2473 codec->power_count++;
2474 if (codec->power_on || codec->power_transition)
2477 codec->power_on = 1;
2478 if (codec->bus->ops.pm_notify)
2479 codec->bus->ops.pm_notify(codec);
2480 hda_call_codec_resume(codec);
2481 cancel_delayed_work(&codec->power_work);
2482 codec->power_transition = 0;
2485 void snd_hda_power_down(struct hda_codec *codec)
2487 --codec->power_count;
2488 if (!codec->power_on || codec->power_count || codec->power_transition)
2491 codec->power_transition = 1; /* avoid reentrance */
2492 schedule_delayed_work(&codec->power_work,
2493 msecs_to_jiffies(power_save * 1000));
2497 int snd_hda_check_amp_list_power(struct hda_codec *codec,
2498 struct hda_loopback_check *check,
2501 struct hda_amp_list *p;
2504 if (!check->amplist)
2506 for (p = check->amplist; p->nid; p++) {
2511 return 0; /* nothing changed */
2513 for (p = check->amplist; p->nid; p++) {
2514 for (ch = 0; ch < 2; ch++) {
2515 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
2517 if (!(v & HDA_AMP_MUTE) && v > 0) {
2518 if (!check->power_on) {
2519 check->power_on = 1;
2520 snd_hda_power_up(codec);
2526 if (check->power_on) {
2527 check->power_on = 0;
2528 snd_hda_power_down(codec);
2535 * Channel mode helper
2537 int snd_hda_ch_mode_info(struct hda_codec *codec,
2538 struct snd_ctl_elem_info *uinfo,
2539 const struct hda_channel_mode *chmode,
2542 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2544 uinfo->value.enumerated.items = num_chmodes;
2545 if (uinfo->value.enumerated.item >= num_chmodes)
2546 uinfo->value.enumerated.item = num_chmodes - 1;
2547 sprintf(uinfo->value.enumerated.name, "%dch",
2548 chmode[uinfo->value.enumerated.item].channels);
2552 int snd_hda_ch_mode_get(struct hda_codec *codec,
2553 struct snd_ctl_elem_value *ucontrol,
2554 const struct hda_channel_mode *chmode,
2560 for (i = 0; i < num_chmodes; i++) {
2561 if (max_channels == chmode[i].channels) {
2562 ucontrol->value.enumerated.item[0] = i;
2569 int snd_hda_ch_mode_put(struct hda_codec *codec,
2570 struct snd_ctl_elem_value *ucontrol,
2571 const struct hda_channel_mode *chmode,
2577 mode = ucontrol->value.enumerated.item[0];
2578 if (mode >= num_chmodes)
2580 if (*max_channelsp == chmode[mode].channels)
2582 /* change the current channel setting */
2583 *max_channelsp = chmode[mode].channels;
2584 if (chmode[mode].sequence)
2585 snd_hda_sequence_write_cache(codec, chmode[mode].sequence);
2592 int snd_hda_input_mux_info(const struct hda_input_mux *imux,
2593 struct snd_ctl_elem_info *uinfo)
2597 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2599 uinfo->value.enumerated.items = imux->num_items;
2600 if (!imux->num_items)
2602 index = uinfo->value.enumerated.item;
2603 if (index >= imux->num_items)
2604 index = imux->num_items - 1;
2605 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
2609 int snd_hda_input_mux_put(struct hda_codec *codec,
2610 const struct hda_input_mux *imux,
2611 struct snd_ctl_elem_value *ucontrol,
2613 unsigned int *cur_val)
2617 if (!imux->num_items)
2619 idx = ucontrol->value.enumerated.item[0];
2620 if (idx >= imux->num_items)
2621 idx = imux->num_items - 1;
2622 if (*cur_val == idx)
2624 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
2625 imux->items[idx].index);
2632 * Multi-channel / digital-out PCM helper functions
2635 /* setup SPDIF output stream */
2636 static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
2637 unsigned int stream_tag, unsigned int format)
2639 /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */
2640 if (codec->spdif_status_reset && (codec->spdif_ctls & AC_DIG1_ENABLE))
2641 set_dig_out_convert(codec, nid,
2642 codec->spdif_ctls & ~AC_DIG1_ENABLE & 0xff,
2644 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
2645 if (codec->slave_dig_outs) {
2647 for (d = codec->slave_dig_outs; *d; d++)
2648 snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
2651 /* turn on again (if needed) */
2652 if (codec->spdif_status_reset && (codec->spdif_ctls & AC_DIG1_ENABLE))
2653 set_dig_out_convert(codec, nid,
2654 codec->spdif_ctls & 0xff, -1);
2657 static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
2659 snd_hda_codec_cleanup_stream(codec, nid);
2660 if (codec->slave_dig_outs) {
2662 for (d = codec->slave_dig_outs; *d; d++)
2663 snd_hda_codec_cleanup_stream(codec, *d);
2668 * open the digital out in the exclusive mode
2670 int snd_hda_multi_out_dig_open(struct hda_codec *codec,
2671 struct hda_multi_out *mout)
2673 mutex_lock(&codec->spdif_mutex);
2674 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
2675 /* already opened as analog dup; reset it once */
2676 cleanup_dig_out_stream(codec, mout->dig_out_nid);
2677 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
2678 mutex_unlock(&codec->spdif_mutex);
2682 int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
2683 struct hda_multi_out *mout,
2684 unsigned int stream_tag,
2685 unsigned int format,
2686 struct snd_pcm_substream *substream)
2688 mutex_lock(&codec->spdif_mutex);
2689 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
2690 mutex_unlock(&codec->spdif_mutex);
2695 * release the digital out
2697 int snd_hda_multi_out_dig_close(struct hda_codec *codec,
2698 struct hda_multi_out *mout)
2700 mutex_lock(&codec->spdif_mutex);
2701 mout->dig_out_used = 0;
2702 mutex_unlock(&codec->spdif_mutex);
2707 * set up more restrictions for analog out
2709 int snd_hda_multi_out_analog_open(struct hda_codec *codec,
2710 struct hda_multi_out *mout,
2711 struct snd_pcm_substream *substream,
2712 struct hda_pcm_stream *hinfo)
2714 struct snd_pcm_runtime *runtime = substream->runtime;
2715 runtime->hw.channels_max = mout->max_channels;
2716 if (mout->dig_out_nid) {
2717 if (!mout->analog_rates) {
2718 mout->analog_rates = hinfo->rates;
2719 mout->analog_formats = hinfo->formats;
2720 mout->analog_maxbps = hinfo->maxbps;
2722 runtime->hw.rates = mout->analog_rates;
2723 runtime->hw.formats = mout->analog_formats;
2724 hinfo->maxbps = mout->analog_maxbps;
2726 if (!mout->spdif_rates) {
2727 snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
2729 &mout->spdif_formats,
2730 &mout->spdif_maxbps);
2732 mutex_lock(&codec->spdif_mutex);
2733 if (mout->share_spdif) {
2734 runtime->hw.rates &= mout->spdif_rates;
2735 runtime->hw.formats &= mout->spdif_formats;
2736 if (mout->spdif_maxbps < hinfo->maxbps)
2737 hinfo->maxbps = mout->spdif_maxbps;
2739 mutex_unlock(&codec->spdif_mutex);
2741 return snd_pcm_hw_constraint_step(substream->runtime, 0,
2742 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
2746 * set up the i/o for analog out
2747 * when the digital out is available, copy the front out to digital out, too.
2749 int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
2750 struct hda_multi_out *mout,
2751 unsigned int stream_tag,
2752 unsigned int format,
2753 struct snd_pcm_substream *substream)
2755 hda_nid_t *nids = mout->dac_nids;
2756 int chs = substream->runtime->channels;
2759 mutex_lock(&codec->spdif_mutex);
2760 if (mout->dig_out_nid && mout->share_spdif &&
2761 mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
2763 snd_hda_is_supported_format(codec, mout->dig_out_nid,
2765 !(codec->spdif_status & IEC958_AES0_NONAUDIO)) {
2766 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
2767 setup_dig_out_stream(codec, mout->dig_out_nid,
2768 stream_tag, format);
2770 mout->dig_out_used = 0;
2771 cleanup_dig_out_stream(codec, mout->dig_out_nid);
2774 mutex_unlock(&codec->spdif_mutex);
2777 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
2779 if (!mout->no_share_stream &&
2780 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
2781 /* headphone out will just decode front left/right (stereo) */
2782 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
2784 /* extra outputs copied from front */
2785 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
2786 if (!mout->no_share_stream && mout->extra_out_nid[i])
2787 snd_hda_codec_setup_stream(codec,
2788 mout->extra_out_nid[i],
2789 stream_tag, 0, format);
2792 for (i = 1; i < mout->num_dacs; i++) {
2793 if (chs >= (i + 1) * 2) /* independent out */
2794 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
2796 else if (!mout->no_share_stream) /* copy front */
2797 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
2804 * clean up the setting for analog out
2806 int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
2807 struct hda_multi_out *mout)
2809 hda_nid_t *nids = mout->dac_nids;
2812 for (i = 0; i < mout->num_dacs; i++)
2813 snd_hda_codec_cleanup_stream(codec, nids[i]);
2815 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
2816 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
2817 if (mout->extra_out_nid[i])
2818 snd_hda_codec_cleanup_stream(codec,
2819 mout->extra_out_nid[i]);
2820 mutex_lock(&codec->spdif_mutex);
2821 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
2822 cleanup_dig_out_stream(codec, mout->dig_out_nid);
2823 mout->dig_out_used = 0;
2825 mutex_unlock(&codec->spdif_mutex);
2830 * Helper for automatic pin configuration
2833 static int is_in_nid_list(hda_nid_t nid, hda_nid_t *list)
2835 for (; *list; list++)
2843 * Sort an associated group of pins according to their sequence numbers.
2845 static void sort_pins_by_sequence(hda_nid_t * pins, short * sequences,
2852 for (i = 0; i < num_pins; i++) {
2853 for (j = i + 1; j < num_pins; j++) {
2854 if (sequences[i] > sequences[j]) {
2856 sequences[i] = sequences[j];
2868 * Parse all pin widgets and store the useful pin nids to cfg
2870 * The number of line-outs or any primary output is stored in line_outs,
2871 * and the corresponding output pins are assigned to line_out_pins[],
2872 * in the order of front, rear, CLFE, side, ...
2874 * If more extra outputs (speaker and headphone) are found, the pins are
2875 * assisnged to hp_pins[] and speaker_pins[], respectively. If no line-out jack
2876 * is detected, one of speaker of HP pins is assigned as the primary
2877 * output, i.e. to line_out_pins[0]. So, line_outs is always positive
2878 * if any analog output exists.
2880 * The analog input pins are assigned to input_pins array.
2881 * The digital input/output pins are assigned to dig_in_pin and dig_out_pin,
2884 int snd_hda_parse_pin_def_config(struct hda_codec *codec,
2885 struct auto_pin_cfg *cfg,
2886 hda_nid_t *ignore_nids)
2888 hda_nid_t nid, end_nid;
2889 short seq, assoc_line_out, assoc_speaker;
2890 short sequences_line_out[ARRAY_SIZE(cfg->line_out_pins)];
2891 short sequences_speaker[ARRAY_SIZE(cfg->speaker_pins)];
2892 short sequences_hp[ARRAY_SIZE(cfg->hp_pins)];
2894 memset(cfg, 0, sizeof(*cfg));
2896 memset(sequences_line_out, 0, sizeof(sequences_line_out));
2897 memset(sequences_speaker, 0, sizeof(sequences_speaker));
2898 memset(sequences_hp, 0, sizeof(sequences_hp));
2899 assoc_line_out = assoc_speaker = 0;
2901 end_nid = codec->start_nid + codec->num_nodes;
2902 for (nid = codec->start_nid; nid < end_nid; nid++) {
2903 unsigned int wid_caps = get_wcaps(codec, nid);
2904 unsigned int wid_type =
2905 (wid_caps & AC_WCAP_TYPE) >> AC_WCAP_TYPE_SHIFT;
2906 unsigned int def_conf;
2909 /* read all default configuration for pin complex */
2910 if (wid_type != AC_WID_PIN)
2912 /* ignore the given nids (e.g. pc-beep returns error) */
2913 if (ignore_nids && is_in_nid_list(nid, ignore_nids))
2916 def_conf = snd_hda_codec_read(codec, nid, 0,
2917 AC_VERB_GET_CONFIG_DEFAULT, 0);
2918 if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE)
2920 loc = get_defcfg_location(def_conf);
2921 switch (get_defcfg_device(def_conf)) {
2922 case AC_JACK_LINE_OUT:
2923 seq = get_defcfg_sequence(def_conf);
2924 assoc = get_defcfg_association(def_conf);
2926 if (!(wid_caps & AC_WCAP_STEREO))
2927 if (!cfg->mono_out_pin)
2928 cfg->mono_out_pin = nid;
2931 if (!assoc_line_out)
2932 assoc_line_out = assoc;
2933 else if (assoc_line_out != assoc)
2935 if (cfg->line_outs >= ARRAY_SIZE(cfg->line_out_pins))
2937 cfg->line_out_pins[cfg->line_outs] = nid;
2938 sequences_line_out[cfg->line_outs] = seq;
2941 case AC_JACK_SPEAKER:
2942 seq = get_defcfg_sequence(def_conf);
2943 assoc = get_defcfg_association(def_conf);
2946 if (! assoc_speaker)
2947 assoc_speaker = assoc;
2948 else if (assoc_speaker != assoc)
2950 if (cfg->speaker_outs >= ARRAY_SIZE(cfg->speaker_pins))
2952 cfg->speaker_pins[cfg->speaker_outs] = nid;
2953 sequences_speaker[cfg->speaker_outs] = seq;
2954 cfg->speaker_outs++;
2956 case AC_JACK_HP_OUT:
2957 seq = get_defcfg_sequence(def_conf);
2958 assoc = get_defcfg_association(def_conf);
2959 if (cfg->hp_outs >= ARRAY_SIZE(cfg->hp_pins))
2961 cfg->hp_pins[cfg->hp_outs] = nid;
2962 sequences_hp[cfg->hp_outs] = (assoc << 4) | seq;
2965 case AC_JACK_MIC_IN: {
2967 if (loc == AC_JACK_LOC_FRONT) {
2968 preferred = AUTO_PIN_FRONT_MIC;
2971 preferred = AUTO_PIN_MIC;
2972 alt = AUTO_PIN_FRONT_MIC;
2974 if (!cfg->input_pins[preferred])
2975 cfg->input_pins[preferred] = nid;
2976 else if (!cfg->input_pins[alt])
2977 cfg->input_pins[alt] = nid;
2980 case AC_JACK_LINE_IN:
2981 if (loc == AC_JACK_LOC_FRONT)
2982 cfg->input_pins[AUTO_PIN_FRONT_LINE] = nid;
2984 cfg->input_pins[AUTO_PIN_LINE] = nid;
2987 cfg->input_pins[AUTO_PIN_CD] = nid;
2990 cfg->input_pins[AUTO_PIN_AUX] = nid;
2992 case AC_JACK_SPDIF_OUT:
2993 cfg->dig_out_pin = nid;
2995 case AC_JACK_SPDIF_IN:
2996 cfg->dig_in_pin = nid;
3002 * If no line-out is defined but multiple HPs are found,
3003 * some of them might be the real line-outs.
3005 if (!cfg->line_outs && cfg->hp_outs > 1) {
3007 while (i < cfg->hp_outs) {
3008 /* The real HPs should have the sequence 0x0f */
3009 if ((sequences_hp[i] & 0x0f) == 0x0f) {
3013 /* Move it to the line-out table */
3014 cfg->line_out_pins[cfg->line_outs] = cfg->hp_pins[i];
3015 sequences_line_out[cfg->line_outs] = sequences_hp[i];
3018 memmove(cfg->hp_pins + i, cfg->hp_pins + i + 1,
3019 sizeof(cfg->hp_pins[0]) * (cfg->hp_outs - i));
3020 memmove(sequences_hp + i - 1, sequences_hp + i,
3021 sizeof(sequences_hp[0]) * (cfg->hp_outs - i));
3025 /* sort by sequence */
3026 sort_pins_by_sequence(cfg->line_out_pins, sequences_line_out,
3028 sort_pins_by_sequence(cfg->speaker_pins, sequences_speaker,
3030 sort_pins_by_sequence(cfg->hp_pins, sequences_hp,
3033 /* if we have only one mic, make it AUTO_PIN_MIC */
3034 if (!cfg->input_pins[AUTO_PIN_MIC] &&
3035 cfg->input_pins[AUTO_PIN_FRONT_MIC]) {
3036 cfg->input_pins[AUTO_PIN_MIC] =
3037 cfg->input_pins[AUTO_PIN_FRONT_MIC];
3038 cfg->input_pins[AUTO_PIN_FRONT_MIC] = 0;
3040 /* ditto for line-in */
3041 if (!cfg->input_pins[AUTO_PIN_LINE] &&
3042 cfg->input_pins[AUTO_PIN_FRONT_LINE]) {
3043 cfg->input_pins[AUTO_PIN_LINE] =
3044 cfg->input_pins[AUTO_PIN_FRONT_LINE];
3045 cfg->input_pins[AUTO_PIN_FRONT_LINE] = 0;
3049 * FIX-UP: if no line-outs are detected, try to use speaker or HP pin
3050 * as a primary output
3052 if (!cfg->line_outs) {
3053 if (cfg->speaker_outs) {
3054 cfg->line_outs = cfg->speaker_outs;
3055 memcpy(cfg->line_out_pins, cfg->speaker_pins,
3056 sizeof(cfg->speaker_pins));
3057 cfg->speaker_outs = 0;
3058 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
3059 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
3060 } else if (cfg->hp_outs) {
3061 cfg->line_outs = cfg->hp_outs;
3062 memcpy(cfg->line_out_pins, cfg->hp_pins,
3063 sizeof(cfg->hp_pins));
3065 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
3066 cfg->line_out_type = AUTO_PIN_HP_OUT;
3070 /* Reorder the surround channels
3071 * ALSA sequence is front/surr/clfe/side
3073 * 4-ch: front/surr => OK as it is
3074 * 6-ch: front/clfe/surr
3075 * 8-ch: front/clfe/rear/side|fc
3077 switch (cfg->line_outs) {
3080 nid = cfg->line_out_pins[1];
3081 cfg->line_out_pins[1] = cfg->line_out_pins[2];
3082 cfg->line_out_pins[2] = nid;
3087 * debug prints of the parsed results
3089 snd_printd("autoconfig: line_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
3090 cfg->line_outs, cfg->line_out_pins[0], cfg->line_out_pins[1],
3091 cfg->line_out_pins[2], cfg->line_out_pins[3],
3092 cfg->line_out_pins[4]);
3093 snd_printd(" speaker_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
3094 cfg->speaker_outs, cfg->speaker_pins[0],
3095 cfg->speaker_pins[1], cfg->speaker_pins[2],
3096 cfg->speaker_pins[3], cfg->speaker_pins[4]);
3097 snd_printd(" hp_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
3098 cfg->hp_outs, cfg->hp_pins[0],
3099 cfg->hp_pins[1], cfg->hp_pins[2],
3100 cfg->hp_pins[3], cfg->hp_pins[4]);
3101 snd_printd(" mono: mono_out=0x%x\n", cfg->mono_out_pin);
3102 snd_printd(" inputs: mic=0x%x, fmic=0x%x, line=0x%x, fline=0x%x,"
3103 " cd=0x%x, aux=0x%x\n",
3104 cfg->input_pins[AUTO_PIN_MIC],
3105 cfg->input_pins[AUTO_PIN_FRONT_MIC],
3106 cfg->input_pins[AUTO_PIN_LINE],
3107 cfg->input_pins[AUTO_PIN_FRONT_LINE],
3108 cfg->input_pins[AUTO_PIN_CD],
3109 cfg->input_pins[AUTO_PIN_AUX]);
3114 /* labels for input pins */
3115 const char *auto_pin_cfg_labels[AUTO_PIN_LAST] = {
3116 "Mic", "Front Mic", "Line", "Front Line", "CD", "Aux"
3126 * snd_hda_suspend - suspend the codecs
3128 * @state: suspsend state
3130 * Returns 0 if successful.
3132 int snd_hda_suspend(struct hda_bus *bus, pm_message_t state)
3134 struct hda_codec *codec;
3136 list_for_each_entry(codec, &bus->codec_list, list) {
3137 #ifdef CONFIG_SND_HDA_POWER_SAVE
3138 if (!codec->power_on)
3141 hda_call_codec_suspend(codec);
3147 * snd_hda_resume - resume the codecs
3149 * @state: resume state
3151 * Returns 0 if successful.
3153 * This fucntion is defined only when POWER_SAVE isn't set.
3154 * In the power-save mode, the codec is resumed dynamically.
3156 int snd_hda_resume(struct hda_bus *bus)
3158 struct hda_codec *codec;
3160 list_for_each_entry(codec, &bus->codec_list, list) {
3161 if (snd_hda_codec_needs_resume(codec))
3162 hda_call_codec_resume(codec);
3166 #ifdef CONFIG_SND_HDA_POWER_SAVE
3167 int snd_hda_codecs_inuse(struct hda_bus *bus)
3169 struct hda_codec *codec;
3171 list_for_each_entry(codec, &bus->codec_list, list) {
3172 if (snd_hda_codec_needs_resume(codec))
3184 /* get a new element from the given array
3185 * if it exceeds the pre-allocated array size, re-allocate the array
3187 void *snd_array_new(struct snd_array *array)
3189 if (array->used >= array->alloced) {
3190 int num = array->alloced + array->alloc_align;
3191 void *nlist = kcalloc(num + 1, array->elem_size, GFP_KERNEL);
3195 memcpy(nlist, array->list,
3196 array->elem_size * array->alloced);
3199 array->list = nlist;
3200 array->alloced = num;
3202 return array->list + (array->used++ * array->elem_size);
3205 /* free the given array elements */
3206 void snd_array_free(struct snd_array *array)