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 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);
396 #ifdef CONFIG_SND_HDA_HWDEP
397 static int snd_hda_bus_dev_register(struct snd_device *device)
399 struct hda_bus *bus = device->device_data;
400 struct hda_codec *codec;
401 list_for_each_entry(codec, &bus->codec_list, list) {
402 snd_hda_hwdep_add_sysfs(codec);
407 #define snd_hda_bus_dev_register NULL
411 * snd_hda_bus_new - create a HDA bus
412 * @card: the card entry
413 * @temp: the template for hda_bus information
414 * @busp: the pointer to store the created bus instance
416 * Returns 0 if successful, or a negative error code.
418 int __devinit snd_hda_bus_new(struct snd_card *card,
419 const struct hda_bus_template *temp,
420 struct hda_bus **busp)
424 static struct snd_device_ops dev_ops = {
425 .dev_register = snd_hda_bus_dev_register,
426 .dev_free = snd_hda_bus_dev_free,
429 if (snd_BUG_ON(!temp))
431 if (snd_BUG_ON(!temp->ops.command || !temp->ops.get_response))
437 bus = kzalloc(sizeof(*bus), GFP_KERNEL);
439 snd_printk(KERN_ERR "can't allocate struct hda_bus\n");
444 bus->private_data = temp->private_data;
445 bus->pci = temp->pci;
446 bus->modelname = temp->modelname;
447 bus->ops = temp->ops;
449 mutex_init(&bus->cmd_mutex);
450 INIT_LIST_HEAD(&bus->codec_list);
452 err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops);
454 snd_hda_bus_free(bus);
462 #ifdef CONFIG_SND_HDA_GENERIC
463 #define is_generic_config(codec) \
464 (codec->modelname && !strcmp(codec->modelname, "generic"))
466 #define is_generic_config(codec) 0
470 * find a matching codec preset
472 static const struct hda_codec_preset *
473 find_codec_preset(struct hda_codec *codec)
475 const struct hda_codec_preset **tbl, *preset;
477 if (is_generic_config(codec))
478 return NULL; /* use the generic parser */
480 for (tbl = hda_preset_tables; *tbl; tbl++) {
481 for (preset = *tbl; preset->id; preset++) {
482 u32 mask = preset->mask;
483 if (preset->afg && preset->afg != codec->afg)
485 if (preset->mfg && preset->mfg != codec->mfg)
489 if (preset->id == (codec->vendor_id & mask) &&
491 preset->rev == codec->revision_id))
499 * get_codec_name - store the codec name
501 static int get_codec_name(struct hda_codec *codec)
503 const struct hda_vendor_id *c;
504 const char *vendor = NULL;
505 u16 vendor_id = codec->vendor_id >> 16;
506 char tmp[16], name[32];
508 for (c = hda_vendor_ids; c->id; c++) {
509 if (c->id == vendor_id) {
515 sprintf(tmp, "Generic %04x", vendor_id);
518 if (codec->preset && codec->preset->name)
519 snprintf(name, sizeof(name), "%s %s", vendor,
520 codec->preset->name);
522 snprintf(name, sizeof(name), "%s ID %x", vendor,
523 codec->vendor_id & 0xffff);
524 codec->name = kstrdup(name, GFP_KERNEL);
531 * look for an AFG and MFG nodes
533 static void __devinit setup_fg_nodes(struct hda_codec *codec)
538 total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid);
539 for (i = 0; i < total_nodes; i++, nid++) {
541 func = snd_hda_param_read(codec, nid, AC_PAR_FUNCTION_TYPE);
542 switch (func & 0xff) {
543 case AC_GRP_AUDIO_FUNCTION:
546 case AC_GRP_MODEM_FUNCTION:
556 * read widget caps for each widget and store in cache
558 static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
563 codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node,
565 codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL);
568 nid = codec->start_nid;
569 for (i = 0; i < codec->num_nodes; i++, nid++)
570 codec->wcaps[i] = snd_hda_param_read(codec, nid,
571 AC_PAR_AUDIO_WIDGET_CAP);
576 static void init_hda_cache(struct hda_cache_rec *cache,
577 unsigned int record_size);
578 static void free_hda_cache(struct hda_cache_rec *cache);
583 static void snd_hda_codec_free(struct hda_codec *codec)
587 #ifdef CONFIG_SND_HDA_POWER_SAVE
588 cancel_delayed_work(&codec->power_work);
589 flush_scheduled_work();
591 list_del(&codec->list);
592 snd_array_free(&codec->mixers);
593 codec->bus->caddr_tbl[codec->addr] = NULL;
594 if (codec->patch_ops.free)
595 codec->patch_ops.free(codec);
596 free_hda_cache(&codec->amp_cache);
597 free_hda_cache(&codec->cmd_cache);
599 kfree(codec->modelname);
605 * snd_hda_codec_new - create a HDA codec
606 * @bus: the bus to assign
607 * @codec_addr: the codec address
608 * @codecp: the pointer to store the generated codec
610 * Returns 0 if successful, or a negative error code.
612 int __devinit snd_hda_codec_new(struct hda_bus *bus, unsigned int codec_addr,
613 struct hda_codec **codecp)
615 struct hda_codec *codec;
619 if (snd_BUG_ON(!bus))
621 if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS))
624 if (bus->caddr_tbl[codec_addr]) {
625 snd_printk(KERN_ERR "hda_codec: "
626 "address 0x%x is already occupied\n", codec_addr);
630 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
632 snd_printk(KERN_ERR "can't allocate struct hda_codec\n");
637 codec->addr = codec_addr;
638 mutex_init(&codec->spdif_mutex);
639 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
640 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
641 snd_array_init(&codec->mixers, sizeof(struct snd_kcontrol *), 32);
642 if (codec->bus->modelname) {
643 codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL);
644 if (!codec->modelname) {
645 snd_hda_codec_free(codec);
650 #ifdef CONFIG_SND_HDA_POWER_SAVE
651 INIT_DELAYED_WORK(&codec->power_work, hda_power_work);
652 /* snd_hda_codec_new() marks the codec as power-up, and leave it as is.
653 * the caller has to power down appropriatley after initialization
656 hda_keep_power_on(codec);
659 list_add_tail(&codec->list, &bus->codec_list);
660 bus->caddr_tbl[codec_addr] = codec;
662 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
664 if (codec->vendor_id == -1)
665 /* read again, hopefully the access method was corrected
666 * in the last read...
668 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
670 codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT,
671 AC_PAR_SUBSYSTEM_ID);
672 codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT,
675 setup_fg_nodes(codec);
676 if (!codec->afg && !codec->mfg) {
677 snd_printdd("hda_codec: no AFG or MFG node found\n");
678 snd_hda_codec_free(codec);
682 if (read_widget_caps(codec, codec->afg ? codec->afg : codec->mfg) < 0) {
683 snd_printk(KERN_ERR "hda_codec: cannot malloc\n");
684 snd_hda_codec_free(codec);
688 if (!codec->subsystem_id) {
689 hda_nid_t nid = codec->afg ? codec->afg : codec->mfg;
690 codec->subsystem_id =
691 snd_hda_codec_read(codec, nid, 0,
692 AC_VERB_GET_SUBSYSTEM_ID, 0);
695 codec->modelname = kstrdup(bus->modelname, GFP_KERNEL);
697 err = snd_hda_codec_configure(codec);
699 snd_hda_codec_free(codec);
702 snd_hda_codec_proc_new(codec);
704 snd_hda_create_hwdep(codec);
706 sprintf(component, "HDA:%08x,%08x,%08x", codec->vendor_id,
707 codec->subsystem_id, codec->revision_id);
708 snd_component_add(codec->bus->card, component);
715 int snd_hda_codec_configure(struct hda_codec *codec)
719 codec->preset = find_codec_preset(codec);
721 err = get_codec_name(codec);
725 /* audio codec should override the mixer name */
726 if (codec->afg || !*codec->bus->card->mixername)
727 strlcpy(codec->bus->card->mixername, codec->name,
728 sizeof(codec->bus->card->mixername));
730 if (is_generic_config(codec)) {
731 err = snd_hda_parse_generic_codec(codec);
734 if (codec->preset && codec->preset->patch) {
735 err = codec->preset->patch(codec);
739 /* call the default parser */
740 err = snd_hda_parse_generic_codec(codec);
742 printk(KERN_ERR "hda-codec: No codec parser is available\n");
745 if (!err && codec->patch_ops.unsol_event)
746 err = init_unsol_queue(codec->bus);
751 * snd_hda_codec_setup_stream - set up the codec for streaming
752 * @codec: the CODEC to set up
753 * @nid: the NID to set up
754 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
755 * @channel_id: channel id to pass, zero based.
756 * @format: stream format.
758 void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
760 int channel_id, int format)
765 snd_printdd("hda_codec_setup_stream: "
766 "NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
767 nid, stream_tag, channel_id, format);
768 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID,
769 (stream_tag << 4) | channel_id);
771 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, format);
774 void snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid)
779 snd_printdd("hda_codec_cleanup_stream: NID=0x%x\n", nid);
780 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
781 #if 0 /* keep the format */
783 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0);
788 * amp access functions
791 /* FIXME: more better hash key? */
792 #define HDA_HASH_KEY(nid,dir,idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
793 #define INFO_AMP_CAPS (1<<0)
794 #define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
796 /* initialize the hash table */
797 static void __devinit init_hda_cache(struct hda_cache_rec *cache,
798 unsigned int record_size)
800 memset(cache, 0, sizeof(*cache));
801 memset(cache->hash, 0xff, sizeof(cache->hash));
802 snd_array_init(&cache->buf, record_size, 64);
805 static void free_hda_cache(struct hda_cache_rec *cache)
807 snd_array_free(&cache->buf);
810 /* query the hash. allocate an entry if not found. */
811 static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache,
814 u16 idx = key % (u16)ARRAY_SIZE(cache->hash);
815 u16 cur = cache->hash[idx];
816 struct hda_cache_head *info_head = cache->buf.list;
817 struct hda_cache_head *info;
819 while (cur != 0xffff) {
820 info = &info_head[cur];
821 if (info->key == key)
826 /* add a new hash entry */
827 info = snd_array_new(&cache->buf);
830 info->next = cache->hash[idx];
831 cache->hash[idx] = cur;
836 /* query and allocate an amp hash entry */
837 static inline struct hda_amp_info *
838 get_alloc_amp_hash(struct hda_codec *codec, u32 key)
840 return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key);
844 * query AMP capabilities for the given widget and direction
846 u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
848 struct hda_amp_info *info;
850 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, 0));
853 if (!(info->head.val & INFO_AMP_CAPS)) {
854 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
856 info->amp_caps = snd_hda_param_read(codec, nid,
857 direction == HDA_OUTPUT ?
861 info->head.val |= INFO_AMP_CAPS;
863 return info->amp_caps;
866 int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
869 struct hda_amp_info *info;
871 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, dir, 0));
874 info->amp_caps = caps;
875 info->head.val |= INFO_AMP_CAPS;
880 * read the current volume to info
881 * if the cache exists, read the cache value.
883 static unsigned int get_vol_mute(struct hda_codec *codec,
884 struct hda_amp_info *info, hda_nid_t nid,
885 int ch, int direction, int index)
889 if (info->head.val & INFO_AMP_VOL(ch))
890 return info->vol[ch];
892 parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
893 parm |= direction == HDA_OUTPUT ? AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
895 val = snd_hda_codec_read(codec, nid, 0,
896 AC_VERB_GET_AMP_GAIN_MUTE, parm);
897 info->vol[ch] = val & 0xff;
898 info->head.val |= INFO_AMP_VOL(ch);
899 return info->vol[ch];
903 * write the current volume in info to the h/w and update the cache
905 static void put_vol_mute(struct hda_codec *codec, struct hda_amp_info *info,
906 hda_nid_t nid, int ch, int direction, int index,
911 parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
912 parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
913 parm |= index << AC_AMP_SET_INDEX_SHIFT;
915 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
920 * read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
922 int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
923 int direction, int index)
925 struct hda_amp_info *info;
926 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
929 return get_vol_mute(codec, info, nid, ch, direction, index);
933 * update the AMP value, mask = bit mask to set, val = the value
935 int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
936 int direction, int idx, int mask, int val)
938 struct hda_amp_info *info;
940 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, idx));
944 val |= get_vol_mute(codec, info, nid, ch, direction, idx) & ~mask;
945 if (info->vol[ch] == val)
947 put_vol_mute(codec, info, nid, ch, direction, idx, val);
952 * update the AMP stereo with the same mask and value
954 int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
955 int direction, int idx, int mask, int val)
958 for (ch = 0; ch < 2; ch++)
959 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
964 #ifdef SND_HDA_NEEDS_RESUME
965 /* resume the all amp commands from the cache */
966 void snd_hda_codec_resume_amp(struct hda_codec *codec)
968 struct hda_amp_info *buffer = codec->amp_cache.buf.list;
971 for (i = 0; i < codec->amp_cache.buf.used; i++, buffer++) {
972 u32 key = buffer->head.key;
974 unsigned int idx, dir, ch;
978 idx = (key >> 16) & 0xff;
979 dir = (key >> 24) & 0xff;
980 for (ch = 0; ch < 2; ch++) {
981 if (!(buffer->head.val & INFO_AMP_VOL(ch)))
983 put_vol_mute(codec, buffer, nid, ch, dir, idx,
988 #endif /* SND_HDA_NEEDS_RESUME */
991 int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
992 struct snd_ctl_elem_info *uinfo)
994 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
995 u16 nid = get_amp_nid(kcontrol);
996 u8 chs = get_amp_channels(kcontrol);
997 int dir = get_amp_direction(kcontrol);
1000 caps = query_amp_caps(codec, nid, dir);
1002 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1004 printk(KERN_WARNING "hda_codec: "
1005 "num_steps = 0 for NID=0x%x (ctl = %s)\n", nid,
1009 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1010 uinfo->count = chs == 3 ? 2 : 1;
1011 uinfo->value.integer.min = 0;
1012 uinfo->value.integer.max = caps;
1016 int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
1017 struct snd_ctl_elem_value *ucontrol)
1019 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1020 hda_nid_t nid = get_amp_nid(kcontrol);
1021 int chs = get_amp_channels(kcontrol);
1022 int dir = get_amp_direction(kcontrol);
1023 int idx = get_amp_index(kcontrol);
1024 long *valp = ucontrol->value.integer.value;
1027 *valp++ = snd_hda_codec_amp_read(codec, nid, 0, dir, idx)
1030 *valp = snd_hda_codec_amp_read(codec, nid, 1, dir, idx)
1035 int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
1036 struct snd_ctl_elem_value *ucontrol)
1038 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1039 hda_nid_t nid = get_amp_nid(kcontrol);
1040 int chs = get_amp_channels(kcontrol);
1041 int dir = get_amp_direction(kcontrol);
1042 int idx = get_amp_index(kcontrol);
1043 long *valp = ucontrol->value.integer.value;
1046 snd_hda_power_up(codec);
1048 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
1053 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
1055 snd_hda_power_down(codec);
1059 int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
1060 unsigned int size, unsigned int __user *_tlv)
1062 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1063 hda_nid_t nid = get_amp_nid(kcontrol);
1064 int dir = get_amp_direction(kcontrol);
1065 u32 caps, val1, val2;
1067 if (size < 4 * sizeof(unsigned int))
1069 caps = query_amp_caps(codec, nid, dir);
1070 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
1071 val2 = (val2 + 1) * 25;
1072 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
1073 val1 = ((int)val1) * ((int)val2);
1074 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
1076 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
1078 if (put_user(val1, _tlv + 2))
1080 if (put_user(val2, _tlv + 3))
1086 * set (static) TLV for virtual master volume; recalculated as max 0dB
1088 void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
1094 caps = query_amp_caps(codec, nid, dir);
1095 nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1096 step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
1097 step = (step + 1) * 25;
1098 tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
1099 tlv[1] = 2 * sizeof(unsigned int);
1100 tlv[2] = -nums * step;
1104 /* find a mixer control element with the given name */
1105 static struct snd_kcontrol *
1106 _snd_hda_find_mixer_ctl(struct hda_codec *codec,
1107 const char *name, int idx)
1109 struct snd_ctl_elem_id id;
1110 memset(&id, 0, sizeof(id));
1111 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1113 strcpy(id.name, name);
1114 return snd_ctl_find_id(codec->bus->card, &id);
1117 struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
1120 return _snd_hda_find_mixer_ctl(codec, name, 0);
1123 /* Add a control element and assign to the codec */
1124 int snd_hda_ctl_add(struct hda_codec *codec, struct snd_kcontrol *kctl)
1127 struct snd_kcontrol **knewp;
1129 err = snd_ctl_add(codec->bus->card, kctl);
1132 knewp = snd_array_new(&codec->mixers);
1139 /* Clear all controls assigned to the given codec */
1140 void snd_hda_ctls_clear(struct hda_codec *codec)
1143 struct snd_kcontrol **kctls = codec->mixers.list;
1144 for (i = 0; i < codec->mixers.used; i++)
1145 snd_ctl_remove(codec->bus->card, kctls[i]);
1146 snd_array_free(&codec->mixers);
1149 void snd_hda_codec_reset(struct hda_codec *codec)
1153 #ifdef CONFIG_SND_HDA_POWER_SAVE
1154 cancel_delayed_work(&codec->power_work);
1155 flush_scheduled_work();
1157 snd_hda_ctls_clear(codec);
1159 for (i = 0; i < codec->num_pcms; i++) {
1160 if (codec->pcm_info[i].pcm)
1161 snd_device_free(codec->bus->card,
1162 codec->pcm_info[i].pcm);
1164 if (codec->patch_ops.free)
1165 codec->patch_ops.free(codec);
1167 free_hda_cache(&codec->amp_cache);
1168 free_hda_cache(&codec->cmd_cache);
1169 codec->num_pcms = 0;
1170 codec->pcm_info = NULL;
1171 codec->preset = NULL;
1174 /* create a virtual master control and add slaves */
1175 int snd_hda_add_vmaster(struct hda_codec *codec, char *name,
1176 unsigned int *tlv, const char **slaves)
1178 struct snd_kcontrol *kctl;
1182 for (s = slaves; *s && !snd_hda_find_mixer_ctl(codec, *s); s++)
1185 snd_printdd("No slave found for %s\n", name);
1188 kctl = snd_ctl_make_virtual_master(name, tlv);
1191 err = snd_hda_ctl_add(codec, kctl);
1195 for (s = slaves; *s; s++) {
1196 struct snd_kcontrol *sctl;
1198 sctl = snd_hda_find_mixer_ctl(codec, *s);
1200 snd_printdd("Cannot find slave %s, skipped\n", *s);
1203 err = snd_ctl_add_slave(kctl, sctl);
1211 int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
1212 struct snd_ctl_elem_info *uinfo)
1214 int chs = get_amp_channels(kcontrol);
1216 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1217 uinfo->count = chs == 3 ? 2 : 1;
1218 uinfo->value.integer.min = 0;
1219 uinfo->value.integer.max = 1;
1223 int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
1224 struct snd_ctl_elem_value *ucontrol)
1226 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1227 hda_nid_t nid = get_amp_nid(kcontrol);
1228 int chs = get_amp_channels(kcontrol);
1229 int dir = get_amp_direction(kcontrol);
1230 int idx = get_amp_index(kcontrol);
1231 long *valp = ucontrol->value.integer.value;
1234 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
1235 HDA_AMP_MUTE) ? 0 : 1;
1237 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
1238 HDA_AMP_MUTE) ? 0 : 1;
1242 int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
1243 struct snd_ctl_elem_value *ucontrol)
1245 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1246 hda_nid_t nid = get_amp_nid(kcontrol);
1247 int chs = get_amp_channels(kcontrol);
1248 int dir = get_amp_direction(kcontrol);
1249 int idx = get_amp_index(kcontrol);
1250 long *valp = ucontrol->value.integer.value;
1253 snd_hda_power_up(codec);
1255 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
1257 *valp ? 0 : HDA_AMP_MUTE);
1261 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
1263 *valp ? 0 : HDA_AMP_MUTE);
1264 #ifdef CONFIG_SND_HDA_POWER_SAVE
1265 if (codec->patch_ops.check_power_status)
1266 codec->patch_ops.check_power_status(codec, nid);
1268 snd_hda_power_down(codec);
1273 * bound volume controls
1275 * bind multiple volumes (# indices, from 0)
1278 #define AMP_VAL_IDX_SHIFT 19
1279 #define AMP_VAL_IDX_MASK (0x0f<<19)
1281 int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
1282 struct snd_ctl_elem_value *ucontrol)
1284 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1288 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
1289 pval = kcontrol->private_value;
1290 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
1291 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
1292 kcontrol->private_value = pval;
1293 mutex_unlock(&codec->spdif_mutex);
1297 int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
1298 struct snd_ctl_elem_value *ucontrol)
1300 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1302 int i, indices, err = 0, change = 0;
1304 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
1305 pval = kcontrol->private_value;
1306 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
1307 for (i = 0; i < indices; i++) {
1308 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
1309 (i << AMP_VAL_IDX_SHIFT);
1310 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
1315 kcontrol->private_value = pval;
1316 mutex_unlock(&codec->spdif_mutex);
1317 return err < 0 ? err : change;
1321 * generic bound volume/swtich controls
1323 int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
1324 struct snd_ctl_elem_info *uinfo)
1326 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1327 struct hda_bind_ctls *c;
1330 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
1331 c = (struct hda_bind_ctls *)kcontrol->private_value;
1332 kcontrol->private_value = *c->values;
1333 err = c->ops->info(kcontrol, uinfo);
1334 kcontrol->private_value = (long)c;
1335 mutex_unlock(&codec->spdif_mutex);
1339 int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
1340 struct snd_ctl_elem_value *ucontrol)
1342 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1343 struct hda_bind_ctls *c;
1346 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
1347 c = (struct hda_bind_ctls *)kcontrol->private_value;
1348 kcontrol->private_value = *c->values;
1349 err = c->ops->get(kcontrol, ucontrol);
1350 kcontrol->private_value = (long)c;
1351 mutex_unlock(&codec->spdif_mutex);
1355 int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
1356 struct snd_ctl_elem_value *ucontrol)
1358 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1359 struct hda_bind_ctls *c;
1360 unsigned long *vals;
1361 int err = 0, change = 0;
1363 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
1364 c = (struct hda_bind_ctls *)kcontrol->private_value;
1365 for (vals = c->values; *vals; vals++) {
1366 kcontrol->private_value = *vals;
1367 err = c->ops->put(kcontrol, ucontrol);
1372 kcontrol->private_value = (long)c;
1373 mutex_unlock(&codec->spdif_mutex);
1374 return err < 0 ? err : change;
1377 int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
1378 unsigned int size, unsigned int __user *tlv)
1380 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1381 struct hda_bind_ctls *c;
1384 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
1385 c = (struct hda_bind_ctls *)kcontrol->private_value;
1386 kcontrol->private_value = *c->values;
1387 err = c->ops->tlv(kcontrol, op_flag, size, tlv);
1388 kcontrol->private_value = (long)c;
1389 mutex_unlock(&codec->spdif_mutex);
1393 struct hda_ctl_ops snd_hda_bind_vol = {
1394 .info = snd_hda_mixer_amp_volume_info,
1395 .get = snd_hda_mixer_amp_volume_get,
1396 .put = snd_hda_mixer_amp_volume_put,
1397 .tlv = snd_hda_mixer_amp_tlv
1400 struct hda_ctl_ops snd_hda_bind_sw = {
1401 .info = snd_hda_mixer_amp_switch_info,
1402 .get = snd_hda_mixer_amp_switch_get,
1403 .put = snd_hda_mixer_amp_switch_put,
1404 .tlv = snd_hda_mixer_amp_tlv
1408 * SPDIF out controls
1411 static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
1412 struct snd_ctl_elem_info *uinfo)
1414 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1419 static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
1420 struct snd_ctl_elem_value *ucontrol)
1422 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
1423 IEC958_AES0_NONAUDIO |
1424 IEC958_AES0_CON_EMPHASIS_5015 |
1425 IEC958_AES0_CON_NOT_COPYRIGHT;
1426 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
1427 IEC958_AES1_CON_ORIGINAL;
1431 static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
1432 struct snd_ctl_elem_value *ucontrol)
1434 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
1435 IEC958_AES0_NONAUDIO |
1436 IEC958_AES0_PRO_EMPHASIS_5015;
1440 static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
1441 struct snd_ctl_elem_value *ucontrol)
1443 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1445 ucontrol->value.iec958.status[0] = codec->spdif_status & 0xff;
1446 ucontrol->value.iec958.status[1] = (codec->spdif_status >> 8) & 0xff;
1447 ucontrol->value.iec958.status[2] = (codec->spdif_status >> 16) & 0xff;
1448 ucontrol->value.iec958.status[3] = (codec->spdif_status >> 24) & 0xff;
1453 /* convert from SPDIF status bits to HDA SPDIF bits
1454 * bit 0 (DigEn) is always set zero (to be filled later)
1456 static unsigned short convert_from_spdif_status(unsigned int sbits)
1458 unsigned short val = 0;
1460 if (sbits & IEC958_AES0_PROFESSIONAL)
1461 val |= AC_DIG1_PROFESSIONAL;
1462 if (sbits & IEC958_AES0_NONAUDIO)
1463 val |= AC_DIG1_NONAUDIO;
1464 if (sbits & IEC958_AES0_PROFESSIONAL) {
1465 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
1466 IEC958_AES0_PRO_EMPHASIS_5015)
1467 val |= AC_DIG1_EMPHASIS;
1469 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
1470 IEC958_AES0_CON_EMPHASIS_5015)
1471 val |= AC_DIG1_EMPHASIS;
1472 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
1473 val |= AC_DIG1_COPYRIGHT;
1474 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
1475 val |= AC_DIG1_LEVEL;
1476 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
1481 /* convert to SPDIF status bits from HDA SPDIF bits
1483 static unsigned int convert_to_spdif_status(unsigned short val)
1485 unsigned int sbits = 0;
1487 if (val & AC_DIG1_NONAUDIO)
1488 sbits |= IEC958_AES0_NONAUDIO;
1489 if (val & AC_DIG1_PROFESSIONAL)
1490 sbits |= IEC958_AES0_PROFESSIONAL;
1491 if (sbits & IEC958_AES0_PROFESSIONAL) {
1492 if (sbits & AC_DIG1_EMPHASIS)
1493 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
1495 if (val & AC_DIG1_EMPHASIS)
1496 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
1497 if (!(val & AC_DIG1_COPYRIGHT))
1498 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
1499 if (val & AC_DIG1_LEVEL)
1500 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
1501 sbits |= val & (0x7f << 8);
1506 /* set digital convert verbs both for the given NID and its slaves */
1507 static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
1512 snd_hda_codec_write(codec, nid, 0, verb, val);
1513 d = codec->slave_dig_outs;
1517 snd_hda_codec_write(codec, *d, 0, verb, val);
1520 static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
1524 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1);
1526 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2);
1529 static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
1530 struct snd_ctl_elem_value *ucontrol)
1532 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1533 hda_nid_t nid = kcontrol->private_value;
1537 mutex_lock(&codec->spdif_mutex);
1538 codec->spdif_status = ucontrol->value.iec958.status[0] |
1539 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
1540 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
1541 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
1542 val = convert_from_spdif_status(codec->spdif_status);
1543 val |= codec->spdif_ctls & 1;
1544 change = codec->spdif_ctls != val;
1545 codec->spdif_ctls = val;
1548 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
1550 mutex_unlock(&codec->spdif_mutex);
1554 #define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
1556 static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
1557 struct snd_ctl_elem_value *ucontrol)
1559 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1561 ucontrol->value.integer.value[0] = codec->spdif_ctls & AC_DIG1_ENABLE;
1565 static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
1566 struct snd_ctl_elem_value *ucontrol)
1568 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1569 hda_nid_t nid = kcontrol->private_value;
1573 mutex_lock(&codec->spdif_mutex);
1574 val = codec->spdif_ctls & ~AC_DIG1_ENABLE;
1575 if (ucontrol->value.integer.value[0])
1576 val |= AC_DIG1_ENABLE;
1577 change = codec->spdif_ctls != val;
1579 codec->spdif_ctls = val;
1580 set_dig_out_convert(codec, nid, val & 0xff, -1);
1581 /* unmute amp switch (if any) */
1582 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
1583 (val & AC_DIG1_ENABLE))
1584 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
1587 mutex_unlock(&codec->spdif_mutex);
1591 static struct snd_kcontrol_new dig_mixes[] = {
1593 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1594 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1595 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,CON_MASK),
1596 .info = snd_hda_spdif_mask_info,
1597 .get = snd_hda_spdif_cmask_get,
1600 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1601 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1602 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,PRO_MASK),
1603 .info = snd_hda_spdif_mask_info,
1604 .get = snd_hda_spdif_pmask_get,
1607 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1608 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
1609 .info = snd_hda_spdif_mask_info,
1610 .get = snd_hda_spdif_default_get,
1611 .put = snd_hda_spdif_default_put,
1614 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1615 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,SWITCH),
1616 .info = snd_hda_spdif_out_switch_info,
1617 .get = snd_hda_spdif_out_switch_get,
1618 .put = snd_hda_spdif_out_switch_put,
1623 #define SPDIF_MAX_IDX 4 /* 4 instances should be enough to probe */
1626 * snd_hda_create_spdif_out_ctls - create Output SPDIF-related controls
1627 * @codec: the HDA codec
1628 * @nid: audio out widget NID
1630 * Creates controls related with the SPDIF output.
1631 * Called from each patch supporting the SPDIF out.
1633 * Returns 0 if successful, or a negative error code.
1635 int snd_hda_create_spdif_out_ctls(struct hda_codec *codec, hda_nid_t nid)
1638 struct snd_kcontrol *kctl;
1639 struct snd_kcontrol_new *dig_mix;
1642 for (idx = 0; idx < SPDIF_MAX_IDX; idx++) {
1643 if (!_snd_hda_find_mixer_ctl(codec, "IEC958 Playback Switch",
1647 if (idx >= SPDIF_MAX_IDX) {
1648 printk(KERN_ERR "hda_codec: too many IEC958 outputs\n");
1651 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
1652 kctl = snd_ctl_new1(dig_mix, codec);
1653 kctl->id.index = idx;
1654 kctl->private_value = nid;
1655 err = snd_hda_ctl_add(codec, kctl);
1660 snd_hda_codec_read(codec, nid, 0,
1661 AC_VERB_GET_DIGI_CONVERT_1, 0);
1662 codec->spdif_status = convert_to_spdif_status(codec->spdif_ctls);
1667 * SPDIF sharing with analog output
1669 static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
1670 struct snd_ctl_elem_value *ucontrol)
1672 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
1673 ucontrol->value.integer.value[0] = mout->share_spdif;
1677 static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
1678 struct snd_ctl_elem_value *ucontrol)
1680 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
1681 mout->share_spdif = !!ucontrol->value.integer.value[0];
1685 static struct snd_kcontrol_new spdif_share_sw = {
1686 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1687 .name = "IEC958 Default PCM Playback Switch",
1688 .info = snd_ctl_boolean_mono_info,
1689 .get = spdif_share_sw_get,
1690 .put = spdif_share_sw_put,
1693 int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
1694 struct hda_multi_out *mout)
1696 if (!mout->dig_out_nid)
1698 /* ATTENTION: here mout is passed as private_data, instead of codec */
1699 return snd_hda_ctl_add(codec,
1700 snd_ctl_new1(&spdif_share_sw, mout));
1707 #define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
1709 static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
1710 struct snd_ctl_elem_value *ucontrol)
1712 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1714 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
1718 static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
1719 struct snd_ctl_elem_value *ucontrol)
1721 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1722 hda_nid_t nid = kcontrol->private_value;
1723 unsigned int val = !!ucontrol->value.integer.value[0];
1726 mutex_lock(&codec->spdif_mutex);
1727 change = codec->spdif_in_enable != val;
1729 codec->spdif_in_enable = val;
1730 snd_hda_codec_write_cache(codec, nid, 0,
1731 AC_VERB_SET_DIGI_CONVERT_1, val);
1733 mutex_unlock(&codec->spdif_mutex);
1737 static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
1738 struct snd_ctl_elem_value *ucontrol)
1740 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1741 hda_nid_t nid = kcontrol->private_value;
1745 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0);
1746 sbits = convert_to_spdif_status(val);
1747 ucontrol->value.iec958.status[0] = sbits;
1748 ucontrol->value.iec958.status[1] = sbits >> 8;
1749 ucontrol->value.iec958.status[2] = sbits >> 16;
1750 ucontrol->value.iec958.status[3] = sbits >> 24;
1754 static struct snd_kcontrol_new dig_in_ctls[] = {
1756 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1757 .name = SNDRV_CTL_NAME_IEC958("",CAPTURE,SWITCH),
1758 .info = snd_hda_spdif_in_switch_info,
1759 .get = snd_hda_spdif_in_switch_get,
1760 .put = snd_hda_spdif_in_switch_put,
1763 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1764 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1765 .name = SNDRV_CTL_NAME_IEC958("",CAPTURE,DEFAULT),
1766 .info = snd_hda_spdif_mask_info,
1767 .get = snd_hda_spdif_in_status_get,
1773 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
1774 * @codec: the HDA codec
1775 * @nid: audio in widget NID
1777 * Creates controls related with the SPDIF input.
1778 * Called from each patch supporting the SPDIF in.
1780 * Returns 0 if successful, or a negative error code.
1782 int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
1785 struct snd_kcontrol *kctl;
1786 struct snd_kcontrol_new *dig_mix;
1789 for (idx = 0; idx < SPDIF_MAX_IDX; idx++) {
1790 if (!_snd_hda_find_mixer_ctl(codec, "IEC958 Capture Switch",
1794 if (idx >= SPDIF_MAX_IDX) {
1795 printk(KERN_ERR "hda_codec: too many IEC958 inputs\n");
1798 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
1799 kctl = snd_ctl_new1(dig_mix, codec);
1800 kctl->private_value = nid;
1801 err = snd_hda_ctl_add(codec, kctl);
1805 codec->spdif_in_enable =
1806 snd_hda_codec_read(codec, nid, 0,
1807 AC_VERB_GET_DIGI_CONVERT_1, 0) &
1812 #ifdef SND_HDA_NEEDS_RESUME
1817 /* build a 32bit cache key with the widget id and the command parameter */
1818 #define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
1819 #define get_cmd_cache_nid(key) ((key) & 0xff)
1820 #define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
1823 * snd_hda_codec_write_cache - send a single command with caching
1824 * @codec: the HDA codec
1825 * @nid: NID to send the command
1826 * @direct: direct flag
1827 * @verb: the verb to send
1828 * @parm: the parameter for the verb
1830 * Send a single command without waiting for response.
1832 * Returns 0 if successful, or a negative error code.
1834 int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
1835 int direct, unsigned int verb, unsigned int parm)
1838 snd_hda_power_up(codec);
1839 mutex_lock(&codec->bus->cmd_mutex);
1840 err = codec->bus->ops.command(codec, nid, direct, verb, parm);
1842 struct hda_cache_head *c;
1843 u32 key = build_cmd_cache_key(nid, verb);
1844 c = get_alloc_hash(&codec->cmd_cache, key);
1848 mutex_unlock(&codec->bus->cmd_mutex);
1849 snd_hda_power_down(codec);
1853 /* resume the all commands from the cache */
1854 void snd_hda_codec_resume_cache(struct hda_codec *codec)
1856 struct hda_cache_head *buffer = codec->cmd_cache.buf.list;
1859 for (i = 0; i < codec->cmd_cache.buf.used; i++, buffer++) {
1860 u32 key = buffer->key;
1863 snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
1864 get_cmd_cache_cmd(key), buffer->val);
1869 * snd_hda_sequence_write_cache - sequence writes with caching
1870 * @codec: the HDA codec
1871 * @seq: VERB array to send
1873 * Send the commands sequentially from the given array.
1874 * Thte commands are recorded on cache for power-save and resume.
1875 * The array must be terminated with NID=0.
1877 void snd_hda_sequence_write_cache(struct hda_codec *codec,
1878 const struct hda_verb *seq)
1880 for (; seq->nid; seq++)
1881 snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
1884 #endif /* SND_HDA_NEEDS_RESUME */
1887 * set power state of the codec
1889 static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
1890 unsigned int power_state)
1895 snd_hda_codec_write(codec, fg, 0, AC_VERB_SET_POWER_STATE,
1897 msleep(10); /* partial workaround for "azx_get_response timeout" */
1899 nid = codec->start_nid;
1900 for (i = 0; i < codec->num_nodes; i++, nid++) {
1901 unsigned int wcaps = get_wcaps(codec, nid);
1902 if (wcaps & AC_WCAP_POWER) {
1903 unsigned int wid_type = (wcaps & AC_WCAP_TYPE) >>
1905 if (wid_type == AC_WID_PIN) {
1906 unsigned int pincap;
1908 * don't power down the widget if it controls
1909 * eapd and EAPD_BTLENABLE is set.
1911 pincap = snd_hda_param_read(codec, nid,
1913 if (pincap & AC_PINCAP_EAPD) {
1914 int eapd = snd_hda_codec_read(codec,
1916 AC_VERB_GET_EAPD_BTLENABLE, 0);
1918 if (power_state == AC_PWRST_D3 && eapd)
1922 snd_hda_codec_write(codec, nid, 0,
1923 AC_VERB_SET_POWER_STATE,
1928 if (power_state == AC_PWRST_D0) {
1929 unsigned long end_time;
1932 /* wait until the codec reachs to D0 */
1933 end_time = jiffies + msecs_to_jiffies(500);
1935 state = snd_hda_codec_read(codec, fg, 0,
1936 AC_VERB_GET_POWER_STATE, 0);
1937 if (state == power_state)
1940 } while (time_after_eq(end_time, jiffies));
1944 #ifdef CONFIG_SND_HDA_HWDEP
1945 /* execute additional init verbs */
1946 static void hda_exec_init_verbs(struct hda_codec *codec)
1948 if (codec->init_verbs.list)
1949 snd_hda_sequence_write(codec, codec->init_verbs.list);
1952 static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
1955 #ifdef SND_HDA_NEEDS_RESUME
1957 * call suspend and power-down; used both from PM and power-save
1959 static void hda_call_codec_suspend(struct hda_codec *codec)
1961 if (codec->patch_ops.suspend)
1962 codec->patch_ops.suspend(codec, PMSG_SUSPEND);
1963 hda_set_power_state(codec,
1964 codec->afg ? codec->afg : codec->mfg,
1966 #ifdef CONFIG_SND_HDA_POWER_SAVE
1967 cancel_delayed_work(&codec->power_work);
1968 codec->power_on = 0;
1969 codec->power_transition = 0;
1974 * kick up codec; used both from PM and power-save
1976 static void hda_call_codec_resume(struct hda_codec *codec)
1978 hda_set_power_state(codec,
1979 codec->afg ? codec->afg : codec->mfg,
1981 hda_exec_init_verbs(codec);
1982 if (codec->patch_ops.resume)
1983 codec->patch_ops.resume(codec);
1985 if (codec->patch_ops.init)
1986 codec->patch_ops.init(codec);
1987 snd_hda_codec_resume_amp(codec);
1988 snd_hda_codec_resume_cache(codec);
1991 #endif /* SND_HDA_NEEDS_RESUME */
1995 * snd_hda_build_controls - build mixer controls
1998 * Creates mixer controls for each codec included in the bus.
2000 * Returns 0 if successful, otherwise a negative error code.
2002 int __devinit snd_hda_build_controls(struct hda_bus *bus)
2004 struct hda_codec *codec;
2006 list_for_each_entry(codec, &bus->codec_list, list) {
2007 int err = snd_hda_codec_build_controls(codec);
2014 int snd_hda_codec_build_controls(struct hda_codec *codec)
2017 /* fake as if already powered-on */
2018 hda_keep_power_on(codec);
2020 hda_set_power_state(codec,
2021 codec->afg ? codec->afg : codec->mfg,
2023 hda_exec_init_verbs(codec);
2024 /* continue to initialize... */
2025 if (codec->patch_ops.init)
2026 err = codec->patch_ops.init(codec);
2027 if (!err && codec->patch_ops.build_controls)
2028 err = codec->patch_ops.build_controls(codec);
2029 snd_hda_power_down(codec);
2038 struct hda_rate_tbl {
2040 unsigned int alsa_bits;
2041 unsigned int hda_fmt;
2044 static struct hda_rate_tbl rate_bits[] = {
2045 /* rate in Hz, ALSA rate bitmask, HDA format value */
2047 /* autodetected value used in snd_hda_query_supported_pcm */
2048 { 8000, SNDRV_PCM_RATE_8000, 0x0500 }, /* 1/6 x 48 */
2049 { 11025, SNDRV_PCM_RATE_11025, 0x4300 }, /* 1/4 x 44 */
2050 { 16000, SNDRV_PCM_RATE_16000, 0x0200 }, /* 1/3 x 48 */
2051 { 22050, SNDRV_PCM_RATE_22050, 0x4100 }, /* 1/2 x 44 */
2052 { 32000, SNDRV_PCM_RATE_32000, 0x0a00 }, /* 2/3 x 48 */
2053 { 44100, SNDRV_PCM_RATE_44100, 0x4000 }, /* 44 */
2054 { 48000, SNDRV_PCM_RATE_48000, 0x0000 }, /* 48 */
2055 { 88200, SNDRV_PCM_RATE_88200, 0x4800 }, /* 2 x 44 */
2056 { 96000, SNDRV_PCM_RATE_96000, 0x0800 }, /* 2 x 48 */
2057 { 176400, SNDRV_PCM_RATE_176400, 0x5800 },/* 4 x 44 */
2058 { 192000, SNDRV_PCM_RATE_192000, 0x1800 }, /* 4 x 48 */
2059 #define AC_PAR_PCM_RATE_BITS 11
2060 /* up to bits 10, 384kHZ isn't supported properly */
2062 /* not autodetected value */
2063 { 9600, SNDRV_PCM_RATE_KNOT, 0x0400 }, /* 1/5 x 48 */
2065 { 0 } /* terminator */
2069 * snd_hda_calc_stream_format - calculate format bitset
2070 * @rate: the sample rate
2071 * @channels: the number of channels
2072 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
2073 * @maxbps: the max. bps
2075 * Calculate the format bitset from the given rate, channels and th PCM format.
2077 * Return zero if invalid.
2079 unsigned int snd_hda_calc_stream_format(unsigned int rate,
2080 unsigned int channels,
2081 unsigned int format,
2082 unsigned int maxbps)
2085 unsigned int val = 0;
2087 for (i = 0; rate_bits[i].hz; i++)
2088 if (rate_bits[i].hz == rate) {
2089 val = rate_bits[i].hda_fmt;
2092 if (!rate_bits[i].hz) {
2093 snd_printdd("invalid rate %d\n", rate);
2097 if (channels == 0 || channels > 8) {
2098 snd_printdd("invalid channels %d\n", channels);
2101 val |= channels - 1;
2103 switch (snd_pcm_format_width(format)) {
2104 case 8: val |= 0x00; break;
2105 case 16: val |= 0x10; break;
2111 else if (maxbps >= 24)
2117 snd_printdd("invalid format width %d\n",
2118 snd_pcm_format_width(format));
2126 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
2127 * @codec: the HDA codec
2128 * @nid: NID to query
2129 * @ratesp: the pointer to store the detected rate bitflags
2130 * @formatsp: the pointer to store the detected formats
2131 * @bpsp: the pointer to store the detected format widths
2133 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
2134 * or @bsps argument is ignored.
2136 * Returns 0 if successful, otherwise a negative error code.
2138 int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
2139 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
2142 unsigned int val, streams;
2145 if (nid != codec->afg &&
2146 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD)) {
2147 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
2152 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
2156 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
2158 rates |= rate_bits[i].alsa_bits;
2163 if (formatsp || bpsp) {
2168 wcaps = get_wcaps(codec, nid);
2169 streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
2173 streams = snd_hda_param_read(codec, codec->afg,
2180 if (streams & AC_SUPFMT_PCM) {
2181 if (val & AC_SUPPCM_BITS_8) {
2182 formats |= SNDRV_PCM_FMTBIT_U8;
2185 if (val & AC_SUPPCM_BITS_16) {
2186 formats |= SNDRV_PCM_FMTBIT_S16_LE;
2189 if (wcaps & AC_WCAP_DIGITAL) {
2190 if (val & AC_SUPPCM_BITS_32)
2191 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
2192 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
2193 formats |= SNDRV_PCM_FMTBIT_S32_LE;
2194 if (val & AC_SUPPCM_BITS_24)
2196 else if (val & AC_SUPPCM_BITS_20)
2198 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
2199 AC_SUPPCM_BITS_32)) {
2200 formats |= SNDRV_PCM_FMTBIT_S32_LE;
2201 if (val & AC_SUPPCM_BITS_32)
2203 else if (val & AC_SUPPCM_BITS_24)
2205 else if (val & AC_SUPPCM_BITS_20)
2209 else if (streams == AC_SUPFMT_FLOAT32) {
2210 /* should be exclusive */
2211 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
2213 } else if (streams == AC_SUPFMT_AC3) {
2214 /* should be exclusive */
2215 /* temporary hack: we have still no proper support
2216 * for the direct AC3 stream...
2218 formats |= SNDRV_PCM_FMTBIT_U8;
2222 *formatsp = formats;
2231 * snd_hda_is_supported_format - check whether the given node supports
2234 * Returns 1 if supported, 0 if not.
2236 int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
2237 unsigned int format)
2240 unsigned int val = 0, rate, stream;
2242 if (nid != codec->afg &&
2243 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD)) {
2244 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
2249 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
2254 rate = format & 0xff00;
2255 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
2256 if (rate_bits[i].hda_fmt == rate) {
2261 if (i >= AC_PAR_PCM_RATE_BITS)
2264 stream = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
2267 if (!stream && nid != codec->afg)
2268 stream = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
2269 if (!stream || stream == -1)
2272 if (stream & AC_SUPFMT_PCM) {
2273 switch (format & 0xf0) {
2275 if (!(val & AC_SUPPCM_BITS_8))
2279 if (!(val & AC_SUPPCM_BITS_16))
2283 if (!(val & AC_SUPPCM_BITS_20))
2287 if (!(val & AC_SUPPCM_BITS_24))
2291 if (!(val & AC_SUPPCM_BITS_32))
2298 /* FIXME: check for float32 and AC3? */
2307 static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
2308 struct hda_codec *codec,
2309 struct snd_pcm_substream *substream)
2314 static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
2315 struct hda_codec *codec,
2316 unsigned int stream_tag,
2317 unsigned int format,
2318 struct snd_pcm_substream *substream)
2320 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
2324 static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
2325 struct hda_codec *codec,
2326 struct snd_pcm_substream *substream)
2328 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
2332 static int set_pcm_default_values(struct hda_codec *codec,
2333 struct hda_pcm_stream *info)
2335 /* query support PCM information from the given NID */
2336 if (info->nid && (!info->rates || !info->formats)) {
2337 snd_hda_query_supported_pcm(codec, info->nid,
2338 info->rates ? NULL : &info->rates,
2339 info->formats ? NULL : &info->formats,
2340 info->maxbps ? NULL : &info->maxbps);
2342 if (info->ops.open == NULL)
2343 info->ops.open = hda_pcm_default_open_close;
2344 if (info->ops.close == NULL)
2345 info->ops.close = hda_pcm_default_open_close;
2346 if (info->ops.prepare == NULL) {
2347 if (snd_BUG_ON(!info->nid))
2349 info->ops.prepare = hda_pcm_default_prepare;
2351 if (info->ops.cleanup == NULL) {
2352 if (snd_BUG_ON(!info->nid))
2354 info->ops.cleanup = hda_pcm_default_cleanup;
2360 * attach a new PCM stream
2362 static int __devinit
2363 snd_hda_attach_pcm(struct hda_codec *codec, struct hda_pcm *pcm)
2365 struct hda_pcm_stream *info;
2370 for (stream = 0; stream < 2; stream++) {
2371 info = &pcm->stream[stream];
2372 if (info->substreams) {
2373 err = set_pcm_default_values(codec, info);
2378 return codec->bus->ops.attach_pcm(codec, pcm);
2382 * snd_hda_build_pcms - build PCM information
2385 * Create PCM information for each codec included in the bus.
2387 * The build_pcms codec patch is requested to set up codec->num_pcms and
2388 * codec->pcm_info properly. The array is referred by the top-level driver
2389 * to create its PCM instances.
2390 * The allocated codec->pcm_info should be released in codec->patch_ops.free
2393 * At least, substreams, channels_min and channels_max must be filled for
2394 * each stream. substreams = 0 indicates that the stream doesn't exist.
2395 * When rates and/or formats are zero, the supported values are queried
2396 * from the given nid. The nid is used also by the default ops.prepare
2397 * and ops.cleanup callbacks.
2399 * The driver needs to call ops.open in its open callback. Similarly,
2400 * ops.close is supposed to be called in the close callback.
2401 * ops.prepare should be called in the prepare or hw_params callback
2402 * with the proper parameters for set up.
2403 * ops.cleanup should be called in hw_free for clean up of streams.
2405 * This function returns 0 if successfull, or a negative error code.
2407 int snd_hda_build_pcms(struct hda_bus *bus)
2409 static const char *dev_name[HDA_PCM_NTYPES] = {
2410 "Audio", "SPDIF", "HDMI", "Modem"
2412 /* starting device index for each PCM type */
2413 static int dev_idx[HDA_PCM_NTYPES] = {
2414 [HDA_PCM_TYPE_AUDIO] = 0,
2415 [HDA_PCM_TYPE_SPDIF] = 1,
2416 [HDA_PCM_TYPE_HDMI] = 3,
2417 [HDA_PCM_TYPE_MODEM] = 6
2419 /* normal audio device indices; not linear to keep compatibility */
2420 static int audio_idx[4] = { 0, 2, 4, 5 };
2421 struct hda_codec *codec;
2422 int num_devs[HDA_PCM_NTYPES];
2424 memset(num_devs, 0, sizeof(num_devs));
2425 list_for_each_entry(codec, &bus->codec_list, list) {
2428 if (!codec->num_pcms) {
2429 if (!codec->patch_ops.build_pcms)
2431 err = codec->patch_ops.build_pcms(codec);
2435 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
2436 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
2437 int type = cpcm->pcm_type;
2440 case HDA_PCM_TYPE_AUDIO:
2441 if (num_devs[type] >= ARRAY_SIZE(audio_idx)) {
2442 snd_printk(KERN_WARNING
2443 "Too many audio devices\n");
2446 dev = audio_idx[num_devs[type]];
2448 case HDA_PCM_TYPE_SPDIF:
2449 case HDA_PCM_TYPE_HDMI:
2450 case HDA_PCM_TYPE_MODEM:
2451 if (num_devs[type]) {
2452 snd_printk(KERN_WARNING
2453 "%s already defined\n",
2457 dev = dev_idx[type];
2460 snd_printk(KERN_WARNING
2461 "Invalid PCM type %d\n", type);
2467 err = snd_hda_attach_pcm(codec, cpcm);
2477 * snd_hda_check_board_config - compare the current codec with the config table
2478 * @codec: the HDA codec
2479 * @num_configs: number of config enums
2480 * @models: array of model name strings
2481 * @tbl: configuration table, terminated by null entries
2483 * Compares the modelname or PCI subsystem id of the current codec with the
2484 * given configuration table. If a matching entry is found, returns its
2485 * config value (supposed to be 0 or positive).
2487 * If no entries are matching, the function returns a negative value.
2489 int snd_hda_check_board_config(struct hda_codec *codec,
2490 int num_configs, const char **models,
2491 const struct snd_pci_quirk *tbl)
2493 if (codec->modelname && models) {
2495 for (i = 0; i < num_configs; i++) {
2497 !strcmp(codec->modelname, models[i])) {
2498 snd_printd(KERN_INFO "hda_codec: model '%s' is "
2499 "selected\n", models[i]);
2505 if (!codec->bus->pci || !tbl)
2508 tbl = snd_pci_quirk_lookup(codec->bus->pci, tbl);
2511 if (tbl->value >= 0 && tbl->value < num_configs) {
2512 #ifdef CONFIG_SND_DEBUG_VERBOSE
2514 const char *model = NULL;
2516 model = models[tbl->value];
2518 sprintf(tmp, "#%d", tbl->value);
2521 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
2522 "for config %x:%x (%s)\n",
2523 model, tbl->subvendor, tbl->subdevice,
2524 (tbl->name ? tbl->name : "Unknown device"));
2532 * snd_hda_add_new_ctls - create controls from the array
2533 * @codec: the HDA codec
2534 * @knew: the array of struct snd_kcontrol_new
2536 * This helper function creates and add new controls in the given array.
2537 * The array must be terminated with an empty entry as terminator.
2539 * Returns 0 if successful, or a negative error code.
2541 int snd_hda_add_new_ctls(struct hda_codec *codec, struct snd_kcontrol_new *knew)
2545 for (; knew->name; knew++) {
2546 struct snd_kcontrol *kctl;
2547 kctl = snd_ctl_new1(knew, codec);
2550 err = snd_hda_ctl_add(codec, kctl);
2554 kctl = snd_ctl_new1(knew, codec);
2557 kctl->id.device = codec->addr;
2558 err = snd_hda_ctl_add(codec, kctl);
2566 #ifdef CONFIG_SND_HDA_POWER_SAVE
2567 static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
2568 unsigned int power_state);
2570 static void hda_power_work(struct work_struct *work)
2572 struct hda_codec *codec =
2573 container_of(work, struct hda_codec, power_work.work);
2575 if (!codec->power_on || codec->power_count) {
2576 codec->power_transition = 0;
2580 hda_call_codec_suspend(codec);
2581 if (codec->bus->ops.pm_notify)
2582 codec->bus->ops.pm_notify(codec);
2585 static void hda_keep_power_on(struct hda_codec *codec)
2587 codec->power_count++;
2588 codec->power_on = 1;
2591 void snd_hda_power_up(struct hda_codec *codec)
2593 codec->power_count++;
2594 if (codec->power_on || codec->power_transition)
2597 codec->power_on = 1;
2598 if (codec->bus->ops.pm_notify)
2599 codec->bus->ops.pm_notify(codec);
2600 hda_call_codec_resume(codec);
2601 cancel_delayed_work(&codec->power_work);
2602 codec->power_transition = 0;
2605 void snd_hda_power_down(struct hda_codec *codec)
2607 --codec->power_count;
2608 if (!codec->power_on || codec->power_count || codec->power_transition)
2611 codec->power_transition = 1; /* avoid reentrance */
2612 schedule_delayed_work(&codec->power_work,
2613 msecs_to_jiffies(power_save * 1000));
2617 int snd_hda_check_amp_list_power(struct hda_codec *codec,
2618 struct hda_loopback_check *check,
2621 struct hda_amp_list *p;
2624 if (!check->amplist)
2626 for (p = check->amplist; p->nid; p++) {
2631 return 0; /* nothing changed */
2633 for (p = check->amplist; p->nid; p++) {
2634 for (ch = 0; ch < 2; ch++) {
2635 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
2637 if (!(v & HDA_AMP_MUTE) && v > 0) {
2638 if (!check->power_on) {
2639 check->power_on = 1;
2640 snd_hda_power_up(codec);
2646 if (check->power_on) {
2647 check->power_on = 0;
2648 snd_hda_power_down(codec);
2655 * Channel mode helper
2657 int snd_hda_ch_mode_info(struct hda_codec *codec,
2658 struct snd_ctl_elem_info *uinfo,
2659 const struct hda_channel_mode *chmode,
2662 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2664 uinfo->value.enumerated.items = num_chmodes;
2665 if (uinfo->value.enumerated.item >= num_chmodes)
2666 uinfo->value.enumerated.item = num_chmodes - 1;
2667 sprintf(uinfo->value.enumerated.name, "%dch",
2668 chmode[uinfo->value.enumerated.item].channels);
2672 int snd_hda_ch_mode_get(struct hda_codec *codec,
2673 struct snd_ctl_elem_value *ucontrol,
2674 const struct hda_channel_mode *chmode,
2680 for (i = 0; i < num_chmodes; i++) {
2681 if (max_channels == chmode[i].channels) {
2682 ucontrol->value.enumerated.item[0] = i;
2689 int snd_hda_ch_mode_put(struct hda_codec *codec,
2690 struct snd_ctl_elem_value *ucontrol,
2691 const struct hda_channel_mode *chmode,
2697 mode = ucontrol->value.enumerated.item[0];
2698 if (mode >= num_chmodes)
2700 if (*max_channelsp == chmode[mode].channels)
2702 /* change the current channel setting */
2703 *max_channelsp = chmode[mode].channels;
2704 if (chmode[mode].sequence)
2705 snd_hda_sequence_write_cache(codec, chmode[mode].sequence);
2712 int snd_hda_input_mux_info(const struct hda_input_mux *imux,
2713 struct snd_ctl_elem_info *uinfo)
2717 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2719 uinfo->value.enumerated.items = imux->num_items;
2720 if (!imux->num_items)
2722 index = uinfo->value.enumerated.item;
2723 if (index >= imux->num_items)
2724 index = imux->num_items - 1;
2725 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
2729 int snd_hda_input_mux_put(struct hda_codec *codec,
2730 const struct hda_input_mux *imux,
2731 struct snd_ctl_elem_value *ucontrol,
2733 unsigned int *cur_val)
2737 if (!imux->num_items)
2739 idx = ucontrol->value.enumerated.item[0];
2740 if (idx >= imux->num_items)
2741 idx = imux->num_items - 1;
2742 if (*cur_val == idx)
2744 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
2745 imux->items[idx].index);
2752 * Multi-channel / digital-out PCM helper functions
2755 /* setup SPDIF output stream */
2756 static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
2757 unsigned int stream_tag, unsigned int format)
2759 /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */
2760 if (codec->spdif_status_reset && (codec->spdif_ctls & AC_DIG1_ENABLE))
2761 set_dig_out_convert(codec, nid,
2762 codec->spdif_ctls & ~AC_DIG1_ENABLE & 0xff,
2764 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
2765 if (codec->slave_dig_outs) {
2767 for (d = codec->slave_dig_outs; *d; d++)
2768 snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
2771 /* turn on again (if needed) */
2772 if (codec->spdif_status_reset && (codec->spdif_ctls & AC_DIG1_ENABLE))
2773 set_dig_out_convert(codec, nid,
2774 codec->spdif_ctls & 0xff, -1);
2777 static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
2779 snd_hda_codec_cleanup_stream(codec, nid);
2780 if (codec->slave_dig_outs) {
2782 for (d = codec->slave_dig_outs; *d; d++)
2783 snd_hda_codec_cleanup_stream(codec, *d);
2788 * open the digital out in the exclusive mode
2790 int snd_hda_multi_out_dig_open(struct hda_codec *codec,
2791 struct hda_multi_out *mout)
2793 mutex_lock(&codec->spdif_mutex);
2794 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
2795 /* already opened as analog dup; reset it once */
2796 cleanup_dig_out_stream(codec, mout->dig_out_nid);
2797 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
2798 mutex_unlock(&codec->spdif_mutex);
2802 int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
2803 struct hda_multi_out *mout,
2804 unsigned int stream_tag,
2805 unsigned int format,
2806 struct snd_pcm_substream *substream)
2808 mutex_lock(&codec->spdif_mutex);
2809 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
2810 mutex_unlock(&codec->spdif_mutex);
2815 * release the digital out
2817 int snd_hda_multi_out_dig_close(struct hda_codec *codec,
2818 struct hda_multi_out *mout)
2820 mutex_lock(&codec->spdif_mutex);
2821 mout->dig_out_used = 0;
2822 mutex_unlock(&codec->spdif_mutex);
2827 * set up more restrictions for analog out
2829 int snd_hda_multi_out_analog_open(struct hda_codec *codec,
2830 struct hda_multi_out *mout,
2831 struct snd_pcm_substream *substream,
2832 struct hda_pcm_stream *hinfo)
2834 struct snd_pcm_runtime *runtime = substream->runtime;
2835 runtime->hw.channels_max = mout->max_channels;
2836 if (mout->dig_out_nid) {
2837 if (!mout->analog_rates) {
2838 mout->analog_rates = hinfo->rates;
2839 mout->analog_formats = hinfo->formats;
2840 mout->analog_maxbps = hinfo->maxbps;
2842 runtime->hw.rates = mout->analog_rates;
2843 runtime->hw.formats = mout->analog_formats;
2844 hinfo->maxbps = mout->analog_maxbps;
2846 if (!mout->spdif_rates) {
2847 snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
2849 &mout->spdif_formats,
2850 &mout->spdif_maxbps);
2852 mutex_lock(&codec->spdif_mutex);
2853 if (mout->share_spdif) {
2854 runtime->hw.rates &= mout->spdif_rates;
2855 runtime->hw.formats &= mout->spdif_formats;
2856 if (mout->spdif_maxbps < hinfo->maxbps)
2857 hinfo->maxbps = mout->spdif_maxbps;
2859 mutex_unlock(&codec->spdif_mutex);
2861 return snd_pcm_hw_constraint_step(substream->runtime, 0,
2862 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
2866 * set up the i/o for analog out
2867 * when the digital out is available, copy the front out to digital out, too.
2869 int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
2870 struct hda_multi_out *mout,
2871 unsigned int stream_tag,
2872 unsigned int format,
2873 struct snd_pcm_substream *substream)
2875 hda_nid_t *nids = mout->dac_nids;
2876 int chs = substream->runtime->channels;
2879 mutex_lock(&codec->spdif_mutex);
2880 if (mout->dig_out_nid && mout->share_spdif &&
2881 mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
2883 snd_hda_is_supported_format(codec, mout->dig_out_nid,
2885 !(codec->spdif_status & IEC958_AES0_NONAUDIO)) {
2886 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
2887 setup_dig_out_stream(codec, mout->dig_out_nid,
2888 stream_tag, format);
2890 mout->dig_out_used = 0;
2891 cleanup_dig_out_stream(codec, mout->dig_out_nid);
2894 mutex_unlock(&codec->spdif_mutex);
2897 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
2899 if (!mout->no_share_stream &&
2900 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
2901 /* headphone out will just decode front left/right (stereo) */
2902 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
2904 /* extra outputs copied from front */
2905 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
2906 if (!mout->no_share_stream && mout->extra_out_nid[i])
2907 snd_hda_codec_setup_stream(codec,
2908 mout->extra_out_nid[i],
2909 stream_tag, 0, format);
2912 for (i = 1; i < mout->num_dacs; i++) {
2913 if (chs >= (i + 1) * 2) /* independent out */
2914 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
2916 else if (!mout->no_share_stream) /* copy front */
2917 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
2924 * clean up the setting for analog out
2926 int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
2927 struct hda_multi_out *mout)
2929 hda_nid_t *nids = mout->dac_nids;
2932 for (i = 0; i < mout->num_dacs; i++)
2933 snd_hda_codec_cleanup_stream(codec, nids[i]);
2935 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
2936 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
2937 if (mout->extra_out_nid[i])
2938 snd_hda_codec_cleanup_stream(codec,
2939 mout->extra_out_nid[i]);
2940 mutex_lock(&codec->spdif_mutex);
2941 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
2942 cleanup_dig_out_stream(codec, mout->dig_out_nid);
2943 mout->dig_out_used = 0;
2945 mutex_unlock(&codec->spdif_mutex);
2950 * Helper for automatic pin configuration
2953 static int is_in_nid_list(hda_nid_t nid, hda_nid_t *list)
2955 for (; *list; list++)
2963 * Sort an associated group of pins according to their sequence numbers.
2965 static void sort_pins_by_sequence(hda_nid_t * pins, short * sequences,
2972 for (i = 0; i < num_pins; i++) {
2973 for (j = i + 1; j < num_pins; j++) {
2974 if (sequences[i] > sequences[j]) {
2976 sequences[i] = sequences[j];
2988 * Parse all pin widgets and store the useful pin nids to cfg
2990 * The number of line-outs or any primary output is stored in line_outs,
2991 * and the corresponding output pins are assigned to line_out_pins[],
2992 * in the order of front, rear, CLFE, side, ...
2994 * If more extra outputs (speaker and headphone) are found, the pins are
2995 * assisnged to hp_pins[] and speaker_pins[], respectively. If no line-out jack
2996 * is detected, one of speaker of HP pins is assigned as the primary
2997 * output, i.e. to line_out_pins[0]. So, line_outs is always positive
2998 * if any analog output exists.
3000 * The analog input pins are assigned to input_pins array.
3001 * The digital input/output pins are assigned to dig_in_pin and dig_out_pin,
3004 int snd_hda_parse_pin_def_config(struct hda_codec *codec,
3005 struct auto_pin_cfg *cfg,
3006 hda_nid_t *ignore_nids)
3008 hda_nid_t nid, end_nid;
3009 short seq, assoc_line_out, assoc_speaker;
3010 short sequences_line_out[ARRAY_SIZE(cfg->line_out_pins)];
3011 short sequences_speaker[ARRAY_SIZE(cfg->speaker_pins)];
3012 short sequences_hp[ARRAY_SIZE(cfg->hp_pins)];
3014 memset(cfg, 0, sizeof(*cfg));
3016 memset(sequences_line_out, 0, sizeof(sequences_line_out));
3017 memset(sequences_speaker, 0, sizeof(sequences_speaker));
3018 memset(sequences_hp, 0, sizeof(sequences_hp));
3019 assoc_line_out = assoc_speaker = 0;
3021 end_nid = codec->start_nid + codec->num_nodes;
3022 for (nid = codec->start_nid; nid < end_nid; nid++) {
3023 unsigned int wid_caps = get_wcaps(codec, nid);
3024 unsigned int wid_type =
3025 (wid_caps & AC_WCAP_TYPE) >> AC_WCAP_TYPE_SHIFT;
3026 unsigned int def_conf;
3029 /* read all default configuration for pin complex */
3030 if (wid_type != AC_WID_PIN)
3032 /* ignore the given nids (e.g. pc-beep returns error) */
3033 if (ignore_nids && is_in_nid_list(nid, ignore_nids))
3036 def_conf = snd_hda_codec_read(codec, nid, 0,
3037 AC_VERB_GET_CONFIG_DEFAULT, 0);
3038 if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE)
3040 loc = get_defcfg_location(def_conf);
3041 switch (get_defcfg_device(def_conf)) {
3042 case AC_JACK_LINE_OUT:
3043 seq = get_defcfg_sequence(def_conf);
3044 assoc = get_defcfg_association(def_conf);
3046 if (!(wid_caps & AC_WCAP_STEREO))
3047 if (!cfg->mono_out_pin)
3048 cfg->mono_out_pin = nid;
3051 if (!assoc_line_out)
3052 assoc_line_out = assoc;
3053 else if (assoc_line_out != assoc)
3055 if (cfg->line_outs >= ARRAY_SIZE(cfg->line_out_pins))
3057 cfg->line_out_pins[cfg->line_outs] = nid;
3058 sequences_line_out[cfg->line_outs] = seq;
3061 case AC_JACK_SPEAKER:
3062 seq = get_defcfg_sequence(def_conf);
3063 assoc = get_defcfg_association(def_conf);
3066 if (! assoc_speaker)
3067 assoc_speaker = assoc;
3068 else if (assoc_speaker != assoc)
3070 if (cfg->speaker_outs >= ARRAY_SIZE(cfg->speaker_pins))
3072 cfg->speaker_pins[cfg->speaker_outs] = nid;
3073 sequences_speaker[cfg->speaker_outs] = seq;
3074 cfg->speaker_outs++;
3076 case AC_JACK_HP_OUT:
3077 seq = get_defcfg_sequence(def_conf);
3078 assoc = get_defcfg_association(def_conf);
3079 if (cfg->hp_outs >= ARRAY_SIZE(cfg->hp_pins))
3081 cfg->hp_pins[cfg->hp_outs] = nid;
3082 sequences_hp[cfg->hp_outs] = (assoc << 4) | seq;
3085 case AC_JACK_MIC_IN: {
3087 if (loc == AC_JACK_LOC_FRONT) {
3088 preferred = AUTO_PIN_FRONT_MIC;
3091 preferred = AUTO_PIN_MIC;
3092 alt = AUTO_PIN_FRONT_MIC;
3094 if (!cfg->input_pins[preferred])
3095 cfg->input_pins[preferred] = nid;
3096 else if (!cfg->input_pins[alt])
3097 cfg->input_pins[alt] = nid;
3100 case AC_JACK_LINE_IN:
3101 if (loc == AC_JACK_LOC_FRONT)
3102 cfg->input_pins[AUTO_PIN_FRONT_LINE] = nid;
3104 cfg->input_pins[AUTO_PIN_LINE] = nid;
3107 cfg->input_pins[AUTO_PIN_CD] = nid;
3110 cfg->input_pins[AUTO_PIN_AUX] = nid;
3112 case AC_JACK_SPDIF_OUT:
3113 cfg->dig_out_pin = nid;
3115 case AC_JACK_SPDIF_IN:
3116 cfg->dig_in_pin = nid;
3122 * If no line-out is defined but multiple HPs are found,
3123 * some of them might be the real line-outs.
3125 if (!cfg->line_outs && cfg->hp_outs > 1) {
3127 while (i < cfg->hp_outs) {
3128 /* The real HPs should have the sequence 0x0f */
3129 if ((sequences_hp[i] & 0x0f) == 0x0f) {
3133 /* Move it to the line-out table */
3134 cfg->line_out_pins[cfg->line_outs] = cfg->hp_pins[i];
3135 sequences_line_out[cfg->line_outs] = sequences_hp[i];
3138 memmove(cfg->hp_pins + i, cfg->hp_pins + i + 1,
3139 sizeof(cfg->hp_pins[0]) * (cfg->hp_outs - i));
3140 memmove(sequences_hp + i - 1, sequences_hp + i,
3141 sizeof(sequences_hp[0]) * (cfg->hp_outs - i));
3145 /* sort by sequence */
3146 sort_pins_by_sequence(cfg->line_out_pins, sequences_line_out,
3148 sort_pins_by_sequence(cfg->speaker_pins, sequences_speaker,
3150 sort_pins_by_sequence(cfg->hp_pins, sequences_hp,
3153 /* if we have only one mic, make it AUTO_PIN_MIC */
3154 if (!cfg->input_pins[AUTO_PIN_MIC] &&
3155 cfg->input_pins[AUTO_PIN_FRONT_MIC]) {
3156 cfg->input_pins[AUTO_PIN_MIC] =
3157 cfg->input_pins[AUTO_PIN_FRONT_MIC];
3158 cfg->input_pins[AUTO_PIN_FRONT_MIC] = 0;
3160 /* ditto for line-in */
3161 if (!cfg->input_pins[AUTO_PIN_LINE] &&
3162 cfg->input_pins[AUTO_PIN_FRONT_LINE]) {
3163 cfg->input_pins[AUTO_PIN_LINE] =
3164 cfg->input_pins[AUTO_PIN_FRONT_LINE];
3165 cfg->input_pins[AUTO_PIN_FRONT_LINE] = 0;
3169 * FIX-UP: if no line-outs are detected, try to use speaker or HP pin
3170 * as a primary output
3172 if (!cfg->line_outs) {
3173 if (cfg->speaker_outs) {
3174 cfg->line_outs = cfg->speaker_outs;
3175 memcpy(cfg->line_out_pins, cfg->speaker_pins,
3176 sizeof(cfg->speaker_pins));
3177 cfg->speaker_outs = 0;
3178 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
3179 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
3180 } else if (cfg->hp_outs) {
3181 cfg->line_outs = cfg->hp_outs;
3182 memcpy(cfg->line_out_pins, cfg->hp_pins,
3183 sizeof(cfg->hp_pins));
3185 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
3186 cfg->line_out_type = AUTO_PIN_HP_OUT;
3190 /* Reorder the surround channels
3191 * ALSA sequence is front/surr/clfe/side
3193 * 4-ch: front/surr => OK as it is
3194 * 6-ch: front/clfe/surr
3195 * 8-ch: front/clfe/rear/side|fc
3197 switch (cfg->line_outs) {
3200 nid = cfg->line_out_pins[1];
3201 cfg->line_out_pins[1] = cfg->line_out_pins[2];
3202 cfg->line_out_pins[2] = nid;
3207 * debug prints of the parsed results
3209 snd_printd("autoconfig: line_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
3210 cfg->line_outs, cfg->line_out_pins[0], cfg->line_out_pins[1],
3211 cfg->line_out_pins[2], cfg->line_out_pins[3],
3212 cfg->line_out_pins[4]);
3213 snd_printd(" speaker_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
3214 cfg->speaker_outs, cfg->speaker_pins[0],
3215 cfg->speaker_pins[1], cfg->speaker_pins[2],
3216 cfg->speaker_pins[3], cfg->speaker_pins[4]);
3217 snd_printd(" hp_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
3218 cfg->hp_outs, cfg->hp_pins[0],
3219 cfg->hp_pins[1], cfg->hp_pins[2],
3220 cfg->hp_pins[3], cfg->hp_pins[4]);
3221 snd_printd(" mono: mono_out=0x%x\n", cfg->mono_out_pin);
3222 snd_printd(" inputs: mic=0x%x, fmic=0x%x, line=0x%x, fline=0x%x,"
3223 " cd=0x%x, aux=0x%x\n",
3224 cfg->input_pins[AUTO_PIN_MIC],
3225 cfg->input_pins[AUTO_PIN_FRONT_MIC],
3226 cfg->input_pins[AUTO_PIN_LINE],
3227 cfg->input_pins[AUTO_PIN_FRONT_LINE],
3228 cfg->input_pins[AUTO_PIN_CD],
3229 cfg->input_pins[AUTO_PIN_AUX]);
3234 /* labels for input pins */
3235 const char *auto_pin_cfg_labels[AUTO_PIN_LAST] = {
3236 "Mic", "Front Mic", "Line", "Front Line", "CD", "Aux"
3246 * snd_hda_suspend - suspend the codecs
3248 * @state: suspsend state
3250 * Returns 0 if successful.
3252 int snd_hda_suspend(struct hda_bus *bus, pm_message_t state)
3254 struct hda_codec *codec;
3256 list_for_each_entry(codec, &bus->codec_list, list) {
3257 #ifdef CONFIG_SND_HDA_POWER_SAVE
3258 if (!codec->power_on)
3261 hda_call_codec_suspend(codec);
3267 * snd_hda_resume - resume the codecs
3269 * @state: resume state
3271 * Returns 0 if successful.
3273 * This fucntion is defined only when POWER_SAVE isn't set.
3274 * In the power-save mode, the codec is resumed dynamically.
3276 int snd_hda_resume(struct hda_bus *bus)
3278 struct hda_codec *codec;
3280 list_for_each_entry(codec, &bus->codec_list, list) {
3281 if (snd_hda_codec_needs_resume(codec))
3282 hda_call_codec_resume(codec);
3286 #ifdef CONFIG_SND_HDA_POWER_SAVE
3287 int snd_hda_codecs_inuse(struct hda_bus *bus)
3289 struct hda_codec *codec;
3291 list_for_each_entry(codec, &bus->codec_list, list) {
3292 if (snd_hda_codec_needs_resume(codec))
3304 /* get a new element from the given array
3305 * if it exceeds the pre-allocated array size, re-allocate the array
3307 void *snd_array_new(struct snd_array *array)
3309 if (array->used >= array->alloced) {
3310 int num = array->alloced + array->alloc_align;
3311 void *nlist = kcalloc(num + 1, array->elem_size, GFP_KERNEL);
3315 memcpy(nlist, array->list,
3316 array->elem_size * array->alloced);
3319 array->list = nlist;
3320 array->alloced = num;
3322 return array->list + (array->used++ * array->elem_size);
3325 /* free the given array elements */
3326 void snd_array_free(struct snd_array *array)