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 { 0x1aec, "Wolfson Microelectronics" },
68 { 0x434d, "C-Media" },
69 { 0x8384, "SigmaTel" },
73 static const struct hda_codec_preset *hda_preset_tables[] = {
74 #ifdef CONFIG_SND_HDA_CODEC_REALTEK
75 snd_hda_preset_realtek,
77 #ifdef CONFIG_SND_HDA_CODEC_CMEDIA
78 snd_hda_preset_cmedia,
80 #ifdef CONFIG_SND_HDA_CODEC_ANALOG
81 snd_hda_preset_analog,
83 #ifdef CONFIG_SND_HDA_CODEC_SIGMATEL
84 snd_hda_preset_sigmatel,
86 #ifdef CONFIG_SND_HDA_CODEC_SI3054
87 snd_hda_preset_si3054,
89 #ifdef CONFIG_SND_HDA_CODEC_ATIHDMI
90 snd_hda_preset_atihdmi,
92 #ifdef CONFIG_SND_HDA_CODEC_CONEXANT
93 snd_hda_preset_conexant,
95 #ifdef CONFIG_SND_HDA_CODEC_VIA
98 #ifdef CONFIG_SND_HDA_CODEC_NVHDMI
99 snd_hda_preset_nvhdmi,
104 #ifdef CONFIG_SND_HDA_POWER_SAVE
105 static void hda_power_work(struct work_struct *work);
106 static void hda_keep_power_on(struct hda_codec *codec);
108 static inline void hda_keep_power_on(struct hda_codec *codec) {}
111 const char *snd_hda_get_jack_location(u32 cfg)
113 static char *bases[7] = {
114 "N/A", "Rear", "Front", "Left", "Right", "Top", "Bottom",
116 static unsigned char specials_idx[] = {
121 static char *specials[] = {
122 "Rear Panel", "Drive Bar",
123 "Riser", "HDMI", "ATAPI",
124 "Mobile-In", "Mobile-Out"
127 cfg = (cfg & AC_DEFCFG_LOCATION) >> AC_DEFCFG_LOCATION_SHIFT;
128 if ((cfg & 0x0f) < 7)
129 return bases[cfg & 0x0f];
130 for (i = 0; i < ARRAY_SIZE(specials_idx); i++) {
131 if (cfg == specials_idx[i])
137 const char *snd_hda_get_jack_connectivity(u32 cfg)
139 static char *jack_locations[4] = { "Ext", "Int", "Sep", "Oth" };
141 return jack_locations[(cfg >> (AC_DEFCFG_LOCATION_SHIFT + 4)) & 3];
144 const char *snd_hda_get_jack_type(u32 cfg)
146 static char *jack_types[16] = {
147 "Line Out", "Speaker", "HP Out", "CD",
148 "SPDIF Out", "Digital Out", "Modem Line", "Modem Hand",
149 "Line In", "Aux", "Mic", "Telephony",
150 "SPDIF In", "Digitial In", "Reserved", "Other"
153 return jack_types[(cfg & AC_DEFCFG_DEVICE)
154 >> AC_DEFCFG_DEVICE_SHIFT];
158 * snd_hda_codec_read - send a command and get the response
159 * @codec: the HDA codec
160 * @nid: NID to send the command
161 * @direct: direct flag
162 * @verb: the verb to send
163 * @parm: the parameter for the verb
165 * Send a single command and read the corresponding response.
167 * Returns the obtained response value, or -1 for an error.
169 unsigned int snd_hda_codec_read(struct hda_codec *codec, hda_nid_t nid,
171 unsigned int verb, unsigned int parm)
174 snd_hda_power_up(codec);
175 mutex_lock(&codec->bus->cmd_mutex);
176 if (!codec->bus->ops.command(codec, nid, direct, verb, parm))
177 res = codec->bus->ops.get_response(codec);
179 res = (unsigned int)-1;
180 mutex_unlock(&codec->bus->cmd_mutex);
181 snd_hda_power_down(codec);
186 * snd_hda_codec_write - send a single command without waiting for response
187 * @codec: the HDA codec
188 * @nid: NID to send the command
189 * @direct: direct flag
190 * @verb: the verb to send
191 * @parm: the parameter for the verb
193 * Send a single command without waiting for response.
195 * Returns 0 if successful, or a negative error code.
197 int snd_hda_codec_write(struct hda_codec *codec, hda_nid_t nid, int direct,
198 unsigned int verb, unsigned int parm)
201 snd_hda_power_up(codec);
202 mutex_lock(&codec->bus->cmd_mutex);
203 err = codec->bus->ops.command(codec, nid, direct, verb, parm);
204 mutex_unlock(&codec->bus->cmd_mutex);
205 snd_hda_power_down(codec);
210 * snd_hda_sequence_write - sequence writes
211 * @codec: the HDA codec
212 * @seq: VERB array to send
214 * Send the commands sequentially from the given array.
215 * The array must be terminated with NID=0.
217 void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq)
219 for (; seq->nid; seq++)
220 snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param);
224 * snd_hda_get_sub_nodes - get the range of sub nodes
225 * @codec: the HDA codec
227 * @start_id: the pointer to store the start NID
229 * Parse the NID and store the start NID of its sub-nodes.
230 * Returns the number of sub-nodes.
232 int snd_hda_get_sub_nodes(struct hda_codec *codec, hda_nid_t nid,
237 parm = snd_hda_param_read(codec, nid, AC_PAR_NODE_COUNT);
240 *start_id = (parm >> 16) & 0x7fff;
241 return (int)(parm & 0x7fff);
245 * snd_hda_get_connections - get connection list
246 * @codec: the HDA codec
248 * @conn_list: connection list array
249 * @max_conns: max. number of connections to store
251 * Parses the connection list of the given widget and stores the list
254 * Returns the number of connections, or a negative error code.
256 int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
257 hda_nid_t *conn_list, int max_conns)
260 int i, conn_len, conns;
261 unsigned int shift, num_elems, mask;
264 if (snd_BUG_ON(!conn_list || max_conns <= 0))
267 parm = snd_hda_param_read(codec, nid, AC_PAR_CONNLIST_LEN);
268 if (parm & AC_CLIST_LONG) {
277 conn_len = parm & AC_CLIST_LENGTH;
278 mask = (1 << (shift-1)) - 1;
281 return 0; /* no connection */
284 /* single connection */
285 parm = snd_hda_codec_read(codec, nid, 0,
286 AC_VERB_GET_CONNECT_LIST, 0);
287 conn_list[0] = parm & mask;
291 /* multi connection */
294 for (i = 0; i < conn_len; i++) {
298 if (i % num_elems == 0)
299 parm = snd_hda_codec_read(codec, nid, 0,
300 AC_VERB_GET_CONNECT_LIST, i);
301 range_val = !!(parm & (1 << (shift-1))); /* ranges */
305 /* ranges between the previous and this one */
306 if (!prev_nid || prev_nid >= val) {
307 snd_printk(KERN_WARNING "hda_codec: "
308 "invalid dep_range_val %x:%x\n",
312 for (n = prev_nid + 1; n <= val; n++) {
313 if (conns >= max_conns) {
315 "Too many connections\n");
318 conn_list[conns++] = n;
321 if (conns >= max_conns) {
322 snd_printk(KERN_ERR "Too many connections\n");
325 conn_list[conns++] = val;
334 * snd_hda_queue_unsol_event - add an unsolicited event to queue
336 * @res: unsolicited event (lower 32bit of RIRB entry)
337 * @res_ex: codec addr and flags (upper 32bit or RIRB entry)
339 * Adds the given event to the queue. The events are processed in
340 * the workqueue asynchronously. Call this function in the interrupt
341 * hanlder when RIRB receives an unsolicited event.
343 * Returns 0 if successful, or a negative error code.
345 int snd_hda_queue_unsol_event(struct hda_bus *bus, u32 res, u32 res_ex)
347 struct hda_bus_unsolicited *unsol;
354 wp = (unsol->wp + 1) % HDA_UNSOL_QUEUE_SIZE;
358 unsol->queue[wp] = res;
359 unsol->queue[wp + 1] = res_ex;
361 schedule_work(&unsol->work);
367 * process queued unsolicited events
369 static void process_unsol_events(struct work_struct *work)
371 struct hda_bus_unsolicited *unsol =
372 container_of(work, struct hda_bus_unsolicited, work);
373 struct hda_bus *bus = unsol->bus;
374 struct hda_codec *codec;
375 unsigned int rp, caddr, res;
377 while (unsol->rp != unsol->wp) {
378 rp = (unsol->rp + 1) % HDA_UNSOL_QUEUE_SIZE;
381 res = unsol->queue[rp];
382 caddr = unsol->queue[rp + 1];
383 if (!(caddr & (1 << 4))) /* no unsolicited event? */
385 codec = bus->caddr_tbl[caddr & 0x0f];
386 if (codec && codec->patch_ops.unsol_event)
387 codec->patch_ops.unsol_event(codec, res);
392 * initialize unsolicited queue
394 static int init_unsol_queue(struct hda_bus *bus)
396 struct hda_bus_unsolicited *unsol;
398 if (bus->unsol) /* already initialized */
401 unsol = kzalloc(sizeof(*unsol), GFP_KERNEL);
403 snd_printk(KERN_ERR "hda_codec: "
404 "can't allocate unsolicited queue\n");
407 INIT_WORK(&unsol->work, process_unsol_events);
416 static void snd_hda_codec_free(struct hda_codec *codec);
418 static int snd_hda_bus_free(struct hda_bus *bus)
420 struct hda_codec *codec, *n;
425 flush_scheduled_work();
428 list_for_each_entry_safe(codec, n, &bus->codec_list, list) {
429 snd_hda_codec_free(codec);
431 if (bus->ops.private_free)
432 bus->ops.private_free(bus);
437 static int snd_hda_bus_dev_free(struct snd_device *device)
439 struct hda_bus *bus = device->device_data;
440 return snd_hda_bus_free(bus);
443 #ifdef CONFIG_SND_HDA_HWDEP
444 static int snd_hda_bus_dev_register(struct snd_device *device)
446 struct hda_bus *bus = device->device_data;
447 struct hda_codec *codec;
448 list_for_each_entry(codec, &bus->codec_list, list) {
449 snd_hda_hwdep_add_sysfs(codec);
454 #define snd_hda_bus_dev_register NULL
458 * snd_hda_bus_new - create a HDA bus
459 * @card: the card entry
460 * @temp: the template for hda_bus information
461 * @busp: the pointer to store the created bus instance
463 * Returns 0 if successful, or a negative error code.
465 int __devinit snd_hda_bus_new(struct snd_card *card,
466 const struct hda_bus_template *temp,
467 struct hda_bus **busp)
471 static struct snd_device_ops dev_ops = {
472 .dev_register = snd_hda_bus_dev_register,
473 .dev_free = snd_hda_bus_dev_free,
476 if (snd_BUG_ON(!temp))
478 if (snd_BUG_ON(!temp->ops.command || !temp->ops.get_response))
484 bus = kzalloc(sizeof(*bus), GFP_KERNEL);
486 snd_printk(KERN_ERR "can't allocate struct hda_bus\n");
491 bus->private_data = temp->private_data;
492 bus->pci = temp->pci;
493 bus->modelname = temp->modelname;
494 bus->ops = temp->ops;
496 mutex_init(&bus->cmd_mutex);
497 INIT_LIST_HEAD(&bus->codec_list);
499 err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops);
501 snd_hda_bus_free(bus);
509 #ifdef CONFIG_SND_HDA_GENERIC
510 #define is_generic_config(codec) \
511 (codec->modelname && !strcmp(codec->modelname, "generic"))
513 #define is_generic_config(codec) 0
517 * find a matching codec preset
519 static const struct hda_codec_preset *
520 find_codec_preset(struct hda_codec *codec)
522 const struct hda_codec_preset **tbl, *preset;
524 if (is_generic_config(codec))
525 return NULL; /* use the generic parser */
527 for (tbl = hda_preset_tables; *tbl; tbl++) {
528 for (preset = *tbl; preset->id; preset++) {
529 u32 mask = preset->mask;
530 if (preset->afg && preset->afg != codec->afg)
532 if (preset->mfg && preset->mfg != codec->mfg)
536 if (preset->id == (codec->vendor_id & mask) &&
538 preset->rev == codec->revision_id))
546 * get_codec_name - store the codec name
548 static int get_codec_name(struct hda_codec *codec)
550 const struct hda_vendor_id *c;
551 const char *vendor = NULL;
552 u16 vendor_id = codec->vendor_id >> 16;
553 char tmp[16], name[32];
555 for (c = hda_vendor_ids; c->id; c++) {
556 if (c->id == vendor_id) {
562 sprintf(tmp, "Generic %04x", vendor_id);
565 if (codec->preset && codec->preset->name)
566 snprintf(name, sizeof(name), "%s %s", vendor,
567 codec->preset->name);
569 snprintf(name, sizeof(name), "%s ID %x", vendor,
570 codec->vendor_id & 0xffff);
571 codec->name = kstrdup(name, GFP_KERNEL);
578 * look for an AFG and MFG nodes
580 static void __devinit setup_fg_nodes(struct hda_codec *codec)
585 total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid);
586 for (i = 0; i < total_nodes; i++, nid++) {
588 func = snd_hda_param_read(codec, nid, AC_PAR_FUNCTION_TYPE);
589 switch (func & 0xff) {
590 case AC_GRP_AUDIO_FUNCTION:
593 case AC_GRP_MODEM_FUNCTION:
603 * read widget caps for each widget and store in cache
605 static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
610 codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node,
612 codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL);
615 nid = codec->start_nid;
616 for (i = 0; i < codec->num_nodes; i++, nid++)
617 codec->wcaps[i] = snd_hda_param_read(codec, nid,
618 AC_PAR_AUDIO_WIDGET_CAP);
623 static void init_hda_cache(struct hda_cache_rec *cache,
624 unsigned int record_size);
625 static void free_hda_cache(struct hda_cache_rec *cache);
630 static void snd_hda_codec_free(struct hda_codec *codec)
634 #ifdef CONFIG_SND_HDA_POWER_SAVE
635 cancel_delayed_work(&codec->power_work);
636 flush_scheduled_work();
638 list_del(&codec->list);
639 snd_array_free(&codec->mixers);
640 codec->bus->caddr_tbl[codec->addr] = NULL;
641 if (codec->patch_ops.free)
642 codec->patch_ops.free(codec);
643 free_hda_cache(&codec->amp_cache);
644 free_hda_cache(&codec->cmd_cache);
646 kfree(codec->modelname);
652 * snd_hda_codec_new - create a HDA codec
653 * @bus: the bus to assign
654 * @codec_addr: the codec address
655 * @codecp: the pointer to store the generated codec
657 * Returns 0 if successful, or a negative error code.
659 int __devinit snd_hda_codec_new(struct hda_bus *bus, unsigned int codec_addr,
660 struct hda_codec **codecp)
662 struct hda_codec *codec;
666 if (snd_BUG_ON(!bus))
668 if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS))
671 if (bus->caddr_tbl[codec_addr]) {
672 snd_printk(KERN_ERR "hda_codec: "
673 "address 0x%x is already occupied\n", codec_addr);
677 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
679 snd_printk(KERN_ERR "can't allocate struct hda_codec\n");
684 codec->addr = codec_addr;
685 mutex_init(&codec->spdif_mutex);
686 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
687 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
688 snd_array_init(&codec->mixers, sizeof(struct snd_kcontrol *), 32);
689 if (codec->bus->modelname) {
690 codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL);
691 if (!codec->modelname) {
692 snd_hda_codec_free(codec);
697 #ifdef CONFIG_SND_HDA_POWER_SAVE
698 INIT_DELAYED_WORK(&codec->power_work, hda_power_work);
699 /* snd_hda_codec_new() marks the codec as power-up, and leave it as is.
700 * the caller has to power down appropriatley after initialization
703 hda_keep_power_on(codec);
706 list_add_tail(&codec->list, &bus->codec_list);
707 bus->caddr_tbl[codec_addr] = codec;
709 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
711 if (codec->vendor_id == -1)
712 /* read again, hopefully the access method was corrected
713 * in the last read...
715 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
717 codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT,
718 AC_PAR_SUBSYSTEM_ID);
719 codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT,
722 setup_fg_nodes(codec);
723 if (!codec->afg && !codec->mfg) {
724 snd_printdd("hda_codec: no AFG or MFG node found\n");
725 snd_hda_codec_free(codec);
729 if (read_widget_caps(codec, codec->afg ? codec->afg : codec->mfg) < 0) {
730 snd_printk(KERN_ERR "hda_codec: cannot malloc\n");
731 snd_hda_codec_free(codec);
735 if (!codec->subsystem_id) {
736 hda_nid_t nid = codec->afg ? codec->afg : codec->mfg;
737 codec->subsystem_id =
738 snd_hda_codec_read(codec, nid, 0,
739 AC_VERB_GET_SUBSYSTEM_ID, 0);
742 codec->modelname = kstrdup(bus->modelname, GFP_KERNEL);
744 err = snd_hda_codec_configure(codec);
746 snd_hda_codec_free(codec);
749 snd_hda_codec_proc_new(codec);
751 snd_hda_create_hwdep(codec);
753 sprintf(component, "HDA:%08x,%08x,%08x", codec->vendor_id,
754 codec->subsystem_id, codec->revision_id);
755 snd_component_add(codec->bus->card, component);
762 int snd_hda_codec_configure(struct hda_codec *codec)
766 codec->preset = find_codec_preset(codec);
768 err = get_codec_name(codec);
772 /* audio codec should override the mixer name */
773 if (codec->afg || !*codec->bus->card->mixername)
774 strlcpy(codec->bus->card->mixername, codec->name,
775 sizeof(codec->bus->card->mixername));
777 if (is_generic_config(codec)) {
778 err = snd_hda_parse_generic_codec(codec);
781 if (codec->preset && codec->preset->patch) {
782 err = codec->preset->patch(codec);
786 /* call the default parser */
787 err = snd_hda_parse_generic_codec(codec);
789 printk(KERN_ERR "hda-codec: No codec parser is available\n");
792 if (!err && codec->patch_ops.unsol_event)
793 err = init_unsol_queue(codec->bus);
798 * snd_hda_codec_setup_stream - set up the codec for streaming
799 * @codec: the CODEC to set up
800 * @nid: the NID to set up
801 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
802 * @channel_id: channel id to pass, zero based.
803 * @format: stream format.
805 void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
807 int channel_id, int format)
812 snd_printdd("hda_codec_setup_stream: "
813 "NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
814 nid, stream_tag, channel_id, format);
815 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID,
816 (stream_tag << 4) | channel_id);
818 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, format);
821 void snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid)
826 snd_printdd("hda_codec_cleanup_stream: NID=0x%x\n", nid);
827 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
828 #if 0 /* keep the format */
830 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0);
835 * amp access functions
838 /* FIXME: more better hash key? */
839 #define HDA_HASH_KEY(nid,dir,idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
840 #define INFO_AMP_CAPS (1<<0)
841 #define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
843 /* initialize the hash table */
844 static void __devinit init_hda_cache(struct hda_cache_rec *cache,
845 unsigned int record_size)
847 memset(cache, 0, sizeof(*cache));
848 memset(cache->hash, 0xff, sizeof(cache->hash));
849 snd_array_init(&cache->buf, record_size, 64);
852 static void free_hda_cache(struct hda_cache_rec *cache)
854 snd_array_free(&cache->buf);
857 /* query the hash. allocate an entry if not found. */
858 static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache,
861 u16 idx = key % (u16)ARRAY_SIZE(cache->hash);
862 u16 cur = cache->hash[idx];
863 struct hda_cache_head *info_head = cache->buf.list;
864 struct hda_cache_head *info;
866 while (cur != 0xffff) {
867 info = &info_head[cur];
868 if (info->key == key)
873 /* add a new hash entry */
874 info = snd_array_new(&cache->buf);
877 info->next = cache->hash[idx];
878 cache->hash[idx] = cur;
883 /* query and allocate an amp hash entry */
884 static inline struct hda_amp_info *
885 get_alloc_amp_hash(struct hda_codec *codec, u32 key)
887 return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key);
891 * query AMP capabilities for the given widget and direction
893 u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
895 struct hda_amp_info *info;
897 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, 0));
900 if (!(info->head.val & INFO_AMP_CAPS)) {
901 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
903 info->amp_caps = snd_hda_param_read(codec, nid,
904 direction == HDA_OUTPUT ?
908 info->head.val |= INFO_AMP_CAPS;
910 return info->amp_caps;
913 int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
916 struct hda_amp_info *info;
918 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, dir, 0));
921 info->amp_caps = caps;
922 info->head.val |= INFO_AMP_CAPS;
927 * read the current volume to info
928 * if the cache exists, read the cache value.
930 static unsigned int get_vol_mute(struct hda_codec *codec,
931 struct hda_amp_info *info, hda_nid_t nid,
932 int ch, int direction, int index)
936 if (info->head.val & INFO_AMP_VOL(ch))
937 return info->vol[ch];
939 parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
940 parm |= direction == HDA_OUTPUT ? AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
942 val = snd_hda_codec_read(codec, nid, 0,
943 AC_VERB_GET_AMP_GAIN_MUTE, parm);
944 info->vol[ch] = val & 0xff;
945 info->head.val |= INFO_AMP_VOL(ch);
946 return info->vol[ch];
950 * write the current volume in info to the h/w and update the cache
952 static void put_vol_mute(struct hda_codec *codec, struct hda_amp_info *info,
953 hda_nid_t nid, int ch, int direction, int index,
958 parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
959 parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
960 parm |= index << AC_AMP_SET_INDEX_SHIFT;
962 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
967 * read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
969 int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
970 int direction, int index)
972 struct hda_amp_info *info;
973 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
976 return get_vol_mute(codec, info, nid, ch, direction, index);
980 * update the AMP value, mask = bit mask to set, val = the value
982 int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
983 int direction, int idx, int mask, int val)
985 struct hda_amp_info *info;
987 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, idx));
991 val |= get_vol_mute(codec, info, nid, ch, direction, idx) & ~mask;
992 if (info->vol[ch] == val)
994 put_vol_mute(codec, info, nid, ch, direction, idx, val);
999 * update the AMP stereo with the same mask and value
1001 int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
1002 int direction, int idx, int mask, int val)
1005 for (ch = 0; ch < 2; ch++)
1006 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
1011 #ifdef SND_HDA_NEEDS_RESUME
1012 /* resume the all amp commands from the cache */
1013 void snd_hda_codec_resume_amp(struct hda_codec *codec)
1015 struct hda_amp_info *buffer = codec->amp_cache.buf.list;
1018 for (i = 0; i < codec->amp_cache.buf.used; i++, buffer++) {
1019 u32 key = buffer->head.key;
1021 unsigned int idx, dir, ch;
1025 idx = (key >> 16) & 0xff;
1026 dir = (key >> 24) & 0xff;
1027 for (ch = 0; ch < 2; ch++) {
1028 if (!(buffer->head.val & INFO_AMP_VOL(ch)))
1030 put_vol_mute(codec, buffer, nid, ch, dir, idx,
1035 #endif /* SND_HDA_NEEDS_RESUME */
1038 int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
1039 struct snd_ctl_elem_info *uinfo)
1041 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1042 u16 nid = get_amp_nid(kcontrol);
1043 u8 chs = get_amp_channels(kcontrol);
1044 int dir = get_amp_direction(kcontrol);
1047 caps = query_amp_caps(codec, nid, dir);
1049 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1051 printk(KERN_WARNING "hda_codec: "
1052 "num_steps = 0 for NID=0x%x (ctl = %s)\n", nid,
1056 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1057 uinfo->count = chs == 3 ? 2 : 1;
1058 uinfo->value.integer.min = 0;
1059 uinfo->value.integer.max = caps;
1063 int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
1064 struct snd_ctl_elem_value *ucontrol)
1066 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1067 hda_nid_t nid = get_amp_nid(kcontrol);
1068 int chs = get_amp_channels(kcontrol);
1069 int dir = get_amp_direction(kcontrol);
1070 int idx = get_amp_index(kcontrol);
1071 long *valp = ucontrol->value.integer.value;
1074 *valp++ = snd_hda_codec_amp_read(codec, nid, 0, dir, idx)
1077 *valp = snd_hda_codec_amp_read(codec, nid, 1, dir, idx)
1082 int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
1083 struct snd_ctl_elem_value *ucontrol)
1085 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1086 hda_nid_t nid = get_amp_nid(kcontrol);
1087 int chs = get_amp_channels(kcontrol);
1088 int dir = get_amp_direction(kcontrol);
1089 int idx = get_amp_index(kcontrol);
1090 long *valp = ucontrol->value.integer.value;
1093 snd_hda_power_up(codec);
1095 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
1100 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
1102 snd_hda_power_down(codec);
1106 int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
1107 unsigned int size, unsigned int __user *_tlv)
1109 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1110 hda_nid_t nid = get_amp_nid(kcontrol);
1111 int dir = get_amp_direction(kcontrol);
1112 u32 caps, val1, val2;
1114 if (size < 4 * sizeof(unsigned int))
1116 caps = query_amp_caps(codec, nid, dir);
1117 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
1118 val2 = (val2 + 1) * 25;
1119 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
1120 val1 = ((int)val1) * ((int)val2);
1121 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
1123 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
1125 if (put_user(val1, _tlv + 2))
1127 if (put_user(val2, _tlv + 3))
1133 * set (static) TLV for virtual master volume; recalculated as max 0dB
1135 void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
1141 caps = query_amp_caps(codec, nid, dir);
1142 nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1143 step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
1144 step = (step + 1) * 25;
1145 tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
1146 tlv[1] = 2 * sizeof(unsigned int);
1147 tlv[2] = -nums * step;
1151 /* find a mixer control element with the given name */
1152 static struct snd_kcontrol *
1153 _snd_hda_find_mixer_ctl(struct hda_codec *codec,
1154 const char *name, int idx)
1156 struct snd_ctl_elem_id id;
1157 memset(&id, 0, sizeof(id));
1158 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1160 strcpy(id.name, name);
1161 return snd_ctl_find_id(codec->bus->card, &id);
1164 struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
1167 return _snd_hda_find_mixer_ctl(codec, name, 0);
1170 /* Add a control element and assign to the codec */
1171 int snd_hda_ctl_add(struct hda_codec *codec, struct snd_kcontrol *kctl)
1174 struct snd_kcontrol **knewp;
1176 err = snd_ctl_add(codec->bus->card, kctl);
1179 knewp = snd_array_new(&codec->mixers);
1186 /* Clear all controls assigned to the given codec */
1187 void snd_hda_ctls_clear(struct hda_codec *codec)
1190 struct snd_kcontrol **kctls = codec->mixers.list;
1191 for (i = 0; i < codec->mixers.used; i++)
1192 snd_ctl_remove(codec->bus->card, kctls[i]);
1193 snd_array_free(&codec->mixers);
1196 void snd_hda_codec_reset(struct hda_codec *codec)
1200 #ifdef CONFIG_SND_HDA_POWER_SAVE
1201 cancel_delayed_work(&codec->power_work);
1202 flush_scheduled_work();
1204 snd_hda_ctls_clear(codec);
1206 for (i = 0; i < codec->num_pcms; i++) {
1207 if (codec->pcm_info[i].pcm)
1208 snd_device_free(codec->bus->card,
1209 codec->pcm_info[i].pcm);
1211 if (codec->patch_ops.free)
1212 codec->patch_ops.free(codec);
1214 free_hda_cache(&codec->amp_cache);
1215 free_hda_cache(&codec->cmd_cache);
1216 codec->num_pcms = 0;
1217 codec->pcm_info = NULL;
1218 codec->preset = NULL;
1221 /* create a virtual master control and add slaves */
1222 int snd_hda_add_vmaster(struct hda_codec *codec, char *name,
1223 unsigned int *tlv, const char **slaves)
1225 struct snd_kcontrol *kctl;
1229 for (s = slaves; *s && !snd_hda_find_mixer_ctl(codec, *s); s++)
1232 snd_printdd("No slave found for %s\n", name);
1235 kctl = snd_ctl_make_virtual_master(name, tlv);
1238 err = snd_hda_ctl_add(codec, kctl);
1242 for (s = slaves; *s; s++) {
1243 struct snd_kcontrol *sctl;
1245 sctl = snd_hda_find_mixer_ctl(codec, *s);
1247 snd_printdd("Cannot find slave %s, skipped\n", *s);
1250 err = snd_ctl_add_slave(kctl, sctl);
1258 int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
1259 struct snd_ctl_elem_info *uinfo)
1261 int chs = get_amp_channels(kcontrol);
1263 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1264 uinfo->count = chs == 3 ? 2 : 1;
1265 uinfo->value.integer.min = 0;
1266 uinfo->value.integer.max = 1;
1270 int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
1271 struct snd_ctl_elem_value *ucontrol)
1273 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1274 hda_nid_t nid = get_amp_nid(kcontrol);
1275 int chs = get_amp_channels(kcontrol);
1276 int dir = get_amp_direction(kcontrol);
1277 int idx = get_amp_index(kcontrol);
1278 long *valp = ucontrol->value.integer.value;
1281 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
1282 HDA_AMP_MUTE) ? 0 : 1;
1284 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
1285 HDA_AMP_MUTE) ? 0 : 1;
1289 int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
1290 struct snd_ctl_elem_value *ucontrol)
1292 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1293 hda_nid_t nid = get_amp_nid(kcontrol);
1294 int chs = get_amp_channels(kcontrol);
1295 int dir = get_amp_direction(kcontrol);
1296 int idx = get_amp_index(kcontrol);
1297 long *valp = ucontrol->value.integer.value;
1300 snd_hda_power_up(codec);
1302 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
1304 *valp ? 0 : HDA_AMP_MUTE);
1308 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
1310 *valp ? 0 : HDA_AMP_MUTE);
1311 #ifdef CONFIG_SND_HDA_POWER_SAVE
1312 if (codec->patch_ops.check_power_status)
1313 codec->patch_ops.check_power_status(codec, nid);
1315 snd_hda_power_down(codec);
1320 * bound volume controls
1322 * bind multiple volumes (# indices, from 0)
1325 #define AMP_VAL_IDX_SHIFT 19
1326 #define AMP_VAL_IDX_MASK (0x0f<<19)
1328 int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
1329 struct snd_ctl_elem_value *ucontrol)
1331 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1335 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
1336 pval = kcontrol->private_value;
1337 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
1338 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
1339 kcontrol->private_value = pval;
1340 mutex_unlock(&codec->spdif_mutex);
1344 int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
1345 struct snd_ctl_elem_value *ucontrol)
1347 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1349 int i, indices, err = 0, change = 0;
1351 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
1352 pval = kcontrol->private_value;
1353 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
1354 for (i = 0; i < indices; i++) {
1355 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
1356 (i << AMP_VAL_IDX_SHIFT);
1357 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
1362 kcontrol->private_value = pval;
1363 mutex_unlock(&codec->spdif_mutex);
1364 return err < 0 ? err : change;
1368 * generic bound volume/swtich controls
1370 int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
1371 struct snd_ctl_elem_info *uinfo)
1373 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1374 struct hda_bind_ctls *c;
1377 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
1378 c = (struct hda_bind_ctls *)kcontrol->private_value;
1379 kcontrol->private_value = *c->values;
1380 err = c->ops->info(kcontrol, uinfo);
1381 kcontrol->private_value = (long)c;
1382 mutex_unlock(&codec->spdif_mutex);
1386 int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
1387 struct snd_ctl_elem_value *ucontrol)
1389 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1390 struct hda_bind_ctls *c;
1393 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
1394 c = (struct hda_bind_ctls *)kcontrol->private_value;
1395 kcontrol->private_value = *c->values;
1396 err = c->ops->get(kcontrol, ucontrol);
1397 kcontrol->private_value = (long)c;
1398 mutex_unlock(&codec->spdif_mutex);
1402 int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
1403 struct snd_ctl_elem_value *ucontrol)
1405 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1406 struct hda_bind_ctls *c;
1407 unsigned long *vals;
1408 int err = 0, change = 0;
1410 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
1411 c = (struct hda_bind_ctls *)kcontrol->private_value;
1412 for (vals = c->values; *vals; vals++) {
1413 kcontrol->private_value = *vals;
1414 err = c->ops->put(kcontrol, ucontrol);
1419 kcontrol->private_value = (long)c;
1420 mutex_unlock(&codec->spdif_mutex);
1421 return err < 0 ? err : change;
1424 int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
1425 unsigned int size, unsigned int __user *tlv)
1427 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1428 struct hda_bind_ctls *c;
1431 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
1432 c = (struct hda_bind_ctls *)kcontrol->private_value;
1433 kcontrol->private_value = *c->values;
1434 err = c->ops->tlv(kcontrol, op_flag, size, tlv);
1435 kcontrol->private_value = (long)c;
1436 mutex_unlock(&codec->spdif_mutex);
1440 struct hda_ctl_ops snd_hda_bind_vol = {
1441 .info = snd_hda_mixer_amp_volume_info,
1442 .get = snd_hda_mixer_amp_volume_get,
1443 .put = snd_hda_mixer_amp_volume_put,
1444 .tlv = snd_hda_mixer_amp_tlv
1447 struct hda_ctl_ops snd_hda_bind_sw = {
1448 .info = snd_hda_mixer_amp_switch_info,
1449 .get = snd_hda_mixer_amp_switch_get,
1450 .put = snd_hda_mixer_amp_switch_put,
1451 .tlv = snd_hda_mixer_amp_tlv
1455 * SPDIF out controls
1458 static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
1459 struct snd_ctl_elem_info *uinfo)
1461 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1466 static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
1467 struct snd_ctl_elem_value *ucontrol)
1469 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
1470 IEC958_AES0_NONAUDIO |
1471 IEC958_AES0_CON_EMPHASIS_5015 |
1472 IEC958_AES0_CON_NOT_COPYRIGHT;
1473 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
1474 IEC958_AES1_CON_ORIGINAL;
1478 static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
1479 struct snd_ctl_elem_value *ucontrol)
1481 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
1482 IEC958_AES0_NONAUDIO |
1483 IEC958_AES0_PRO_EMPHASIS_5015;
1487 static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
1488 struct snd_ctl_elem_value *ucontrol)
1490 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1492 ucontrol->value.iec958.status[0] = codec->spdif_status & 0xff;
1493 ucontrol->value.iec958.status[1] = (codec->spdif_status >> 8) & 0xff;
1494 ucontrol->value.iec958.status[2] = (codec->spdif_status >> 16) & 0xff;
1495 ucontrol->value.iec958.status[3] = (codec->spdif_status >> 24) & 0xff;
1500 /* convert from SPDIF status bits to HDA SPDIF bits
1501 * bit 0 (DigEn) is always set zero (to be filled later)
1503 static unsigned short convert_from_spdif_status(unsigned int sbits)
1505 unsigned short val = 0;
1507 if (sbits & IEC958_AES0_PROFESSIONAL)
1508 val |= AC_DIG1_PROFESSIONAL;
1509 if (sbits & IEC958_AES0_NONAUDIO)
1510 val |= AC_DIG1_NONAUDIO;
1511 if (sbits & IEC958_AES0_PROFESSIONAL) {
1512 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
1513 IEC958_AES0_PRO_EMPHASIS_5015)
1514 val |= AC_DIG1_EMPHASIS;
1516 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
1517 IEC958_AES0_CON_EMPHASIS_5015)
1518 val |= AC_DIG1_EMPHASIS;
1519 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
1520 val |= AC_DIG1_COPYRIGHT;
1521 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
1522 val |= AC_DIG1_LEVEL;
1523 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
1528 /* convert to SPDIF status bits from HDA SPDIF bits
1530 static unsigned int convert_to_spdif_status(unsigned short val)
1532 unsigned int sbits = 0;
1534 if (val & AC_DIG1_NONAUDIO)
1535 sbits |= IEC958_AES0_NONAUDIO;
1536 if (val & AC_DIG1_PROFESSIONAL)
1537 sbits |= IEC958_AES0_PROFESSIONAL;
1538 if (sbits & IEC958_AES0_PROFESSIONAL) {
1539 if (sbits & AC_DIG1_EMPHASIS)
1540 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
1542 if (val & AC_DIG1_EMPHASIS)
1543 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
1544 if (!(val & AC_DIG1_COPYRIGHT))
1545 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
1546 if (val & AC_DIG1_LEVEL)
1547 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
1548 sbits |= val & (0x7f << 8);
1553 /* set digital convert verbs both for the given NID and its slaves */
1554 static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
1559 snd_hda_codec_write(codec, nid, 0, verb, val);
1560 d = codec->slave_dig_outs;
1564 snd_hda_codec_write(codec, *d, 0, verb, val);
1567 static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
1571 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1);
1573 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2);
1576 static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
1577 struct snd_ctl_elem_value *ucontrol)
1579 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1580 hda_nid_t nid = kcontrol->private_value;
1584 mutex_lock(&codec->spdif_mutex);
1585 codec->spdif_status = ucontrol->value.iec958.status[0] |
1586 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
1587 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
1588 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
1589 val = convert_from_spdif_status(codec->spdif_status);
1590 val |= codec->spdif_ctls & 1;
1591 change = codec->spdif_ctls != val;
1592 codec->spdif_ctls = val;
1595 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
1597 mutex_unlock(&codec->spdif_mutex);
1601 #define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
1603 static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
1604 struct snd_ctl_elem_value *ucontrol)
1606 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1608 ucontrol->value.integer.value[0] = codec->spdif_ctls & AC_DIG1_ENABLE;
1612 static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
1613 struct snd_ctl_elem_value *ucontrol)
1615 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1616 hda_nid_t nid = kcontrol->private_value;
1620 mutex_lock(&codec->spdif_mutex);
1621 val = codec->spdif_ctls & ~AC_DIG1_ENABLE;
1622 if (ucontrol->value.integer.value[0])
1623 val |= AC_DIG1_ENABLE;
1624 change = codec->spdif_ctls != val;
1626 codec->spdif_ctls = val;
1627 set_dig_out_convert(codec, nid, val & 0xff, -1);
1628 /* unmute amp switch (if any) */
1629 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
1630 (val & AC_DIG1_ENABLE))
1631 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
1634 mutex_unlock(&codec->spdif_mutex);
1638 static struct snd_kcontrol_new dig_mixes[] = {
1640 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1641 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1642 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,CON_MASK),
1643 .info = snd_hda_spdif_mask_info,
1644 .get = snd_hda_spdif_cmask_get,
1647 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1648 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1649 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,PRO_MASK),
1650 .info = snd_hda_spdif_mask_info,
1651 .get = snd_hda_spdif_pmask_get,
1654 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1655 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
1656 .info = snd_hda_spdif_mask_info,
1657 .get = snd_hda_spdif_default_get,
1658 .put = snd_hda_spdif_default_put,
1661 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1662 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,SWITCH),
1663 .info = snd_hda_spdif_out_switch_info,
1664 .get = snd_hda_spdif_out_switch_get,
1665 .put = snd_hda_spdif_out_switch_put,
1670 #define SPDIF_MAX_IDX 4 /* 4 instances should be enough to probe */
1673 * snd_hda_create_spdif_out_ctls - create Output SPDIF-related controls
1674 * @codec: the HDA codec
1675 * @nid: audio out widget NID
1677 * Creates controls related with the SPDIF output.
1678 * Called from each patch supporting the SPDIF out.
1680 * Returns 0 if successful, or a negative error code.
1682 int snd_hda_create_spdif_out_ctls(struct hda_codec *codec, hda_nid_t nid)
1685 struct snd_kcontrol *kctl;
1686 struct snd_kcontrol_new *dig_mix;
1689 for (idx = 0; idx < SPDIF_MAX_IDX; idx++) {
1690 if (!_snd_hda_find_mixer_ctl(codec, "IEC958 Playback Switch",
1694 if (idx >= SPDIF_MAX_IDX) {
1695 printk(KERN_ERR "hda_codec: too many IEC958 outputs\n");
1698 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
1699 kctl = snd_ctl_new1(dig_mix, codec);
1702 kctl->id.index = idx;
1703 kctl->private_value = nid;
1704 err = snd_hda_ctl_add(codec, kctl);
1709 snd_hda_codec_read(codec, nid, 0,
1710 AC_VERB_GET_DIGI_CONVERT_1, 0);
1711 codec->spdif_status = convert_to_spdif_status(codec->spdif_ctls);
1716 * SPDIF sharing with analog output
1718 static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
1719 struct snd_ctl_elem_value *ucontrol)
1721 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
1722 ucontrol->value.integer.value[0] = mout->share_spdif;
1726 static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
1727 struct snd_ctl_elem_value *ucontrol)
1729 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
1730 mout->share_spdif = !!ucontrol->value.integer.value[0];
1734 static struct snd_kcontrol_new spdif_share_sw = {
1735 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1736 .name = "IEC958 Default PCM Playback Switch",
1737 .info = snd_ctl_boolean_mono_info,
1738 .get = spdif_share_sw_get,
1739 .put = spdif_share_sw_put,
1742 int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
1743 struct hda_multi_out *mout)
1745 if (!mout->dig_out_nid)
1747 /* ATTENTION: here mout is passed as private_data, instead of codec */
1748 return snd_hda_ctl_add(codec,
1749 snd_ctl_new1(&spdif_share_sw, mout));
1756 #define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
1758 static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
1759 struct snd_ctl_elem_value *ucontrol)
1761 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1763 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
1767 static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
1768 struct snd_ctl_elem_value *ucontrol)
1770 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1771 hda_nid_t nid = kcontrol->private_value;
1772 unsigned int val = !!ucontrol->value.integer.value[0];
1775 mutex_lock(&codec->spdif_mutex);
1776 change = codec->spdif_in_enable != val;
1778 codec->spdif_in_enable = val;
1779 snd_hda_codec_write_cache(codec, nid, 0,
1780 AC_VERB_SET_DIGI_CONVERT_1, val);
1782 mutex_unlock(&codec->spdif_mutex);
1786 static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
1787 struct snd_ctl_elem_value *ucontrol)
1789 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1790 hda_nid_t nid = kcontrol->private_value;
1794 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0);
1795 sbits = convert_to_spdif_status(val);
1796 ucontrol->value.iec958.status[0] = sbits;
1797 ucontrol->value.iec958.status[1] = sbits >> 8;
1798 ucontrol->value.iec958.status[2] = sbits >> 16;
1799 ucontrol->value.iec958.status[3] = sbits >> 24;
1803 static struct snd_kcontrol_new dig_in_ctls[] = {
1805 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1806 .name = SNDRV_CTL_NAME_IEC958("",CAPTURE,SWITCH),
1807 .info = snd_hda_spdif_in_switch_info,
1808 .get = snd_hda_spdif_in_switch_get,
1809 .put = snd_hda_spdif_in_switch_put,
1812 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1813 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1814 .name = SNDRV_CTL_NAME_IEC958("",CAPTURE,DEFAULT),
1815 .info = snd_hda_spdif_mask_info,
1816 .get = snd_hda_spdif_in_status_get,
1822 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
1823 * @codec: the HDA codec
1824 * @nid: audio in widget NID
1826 * Creates controls related with the SPDIF input.
1827 * Called from each patch supporting the SPDIF in.
1829 * Returns 0 if successful, or a negative error code.
1831 int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
1834 struct snd_kcontrol *kctl;
1835 struct snd_kcontrol_new *dig_mix;
1838 for (idx = 0; idx < SPDIF_MAX_IDX; idx++) {
1839 if (!_snd_hda_find_mixer_ctl(codec, "IEC958 Capture Switch",
1843 if (idx >= SPDIF_MAX_IDX) {
1844 printk(KERN_ERR "hda_codec: too many IEC958 inputs\n");
1847 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
1848 kctl = snd_ctl_new1(dig_mix, codec);
1849 kctl->private_value = nid;
1850 err = snd_hda_ctl_add(codec, kctl);
1854 codec->spdif_in_enable =
1855 snd_hda_codec_read(codec, nid, 0,
1856 AC_VERB_GET_DIGI_CONVERT_1, 0) &
1861 #ifdef SND_HDA_NEEDS_RESUME
1866 /* build a 32bit cache key with the widget id and the command parameter */
1867 #define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
1868 #define get_cmd_cache_nid(key) ((key) & 0xff)
1869 #define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
1872 * snd_hda_codec_write_cache - send a single command with caching
1873 * @codec: the HDA codec
1874 * @nid: NID to send the command
1875 * @direct: direct flag
1876 * @verb: the verb to send
1877 * @parm: the parameter for the verb
1879 * Send a single command without waiting for response.
1881 * Returns 0 if successful, or a negative error code.
1883 int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
1884 int direct, unsigned int verb, unsigned int parm)
1887 snd_hda_power_up(codec);
1888 mutex_lock(&codec->bus->cmd_mutex);
1889 err = codec->bus->ops.command(codec, nid, direct, verb, parm);
1891 struct hda_cache_head *c;
1892 u32 key = build_cmd_cache_key(nid, verb);
1893 c = get_alloc_hash(&codec->cmd_cache, key);
1897 mutex_unlock(&codec->bus->cmd_mutex);
1898 snd_hda_power_down(codec);
1902 /* resume the all commands from the cache */
1903 void snd_hda_codec_resume_cache(struct hda_codec *codec)
1905 struct hda_cache_head *buffer = codec->cmd_cache.buf.list;
1908 for (i = 0; i < codec->cmd_cache.buf.used; i++, buffer++) {
1909 u32 key = buffer->key;
1912 snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
1913 get_cmd_cache_cmd(key), buffer->val);
1918 * snd_hda_sequence_write_cache - sequence writes with caching
1919 * @codec: the HDA codec
1920 * @seq: VERB array to send
1922 * Send the commands sequentially from the given array.
1923 * Thte commands are recorded on cache for power-save and resume.
1924 * The array must be terminated with NID=0.
1926 void snd_hda_sequence_write_cache(struct hda_codec *codec,
1927 const struct hda_verb *seq)
1929 for (; seq->nid; seq++)
1930 snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
1933 #endif /* SND_HDA_NEEDS_RESUME */
1936 * set power state of the codec
1938 static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
1939 unsigned int power_state)
1944 snd_hda_codec_write(codec, fg, 0, AC_VERB_SET_POWER_STATE,
1946 msleep(10); /* partial workaround for "azx_get_response timeout" */
1948 nid = codec->start_nid;
1949 for (i = 0; i < codec->num_nodes; i++, nid++) {
1950 unsigned int wcaps = get_wcaps(codec, nid);
1951 if (wcaps & AC_WCAP_POWER) {
1952 unsigned int wid_type = (wcaps & AC_WCAP_TYPE) >>
1954 if (wid_type == AC_WID_PIN) {
1955 unsigned int pincap;
1957 * don't power down the widget if it controls
1958 * eapd and EAPD_BTLENABLE is set.
1960 pincap = snd_hda_param_read(codec, nid,
1962 if (pincap & AC_PINCAP_EAPD) {
1963 int eapd = snd_hda_codec_read(codec,
1965 AC_VERB_GET_EAPD_BTLENABLE, 0);
1967 if (power_state == AC_PWRST_D3 && eapd)
1971 snd_hda_codec_write(codec, nid, 0,
1972 AC_VERB_SET_POWER_STATE,
1977 if (power_state == AC_PWRST_D0) {
1978 unsigned long end_time;
1981 /* wait until the codec reachs to D0 */
1982 end_time = jiffies + msecs_to_jiffies(500);
1984 state = snd_hda_codec_read(codec, fg, 0,
1985 AC_VERB_GET_POWER_STATE, 0);
1986 if (state == power_state)
1989 } while (time_after_eq(end_time, jiffies));
1993 #ifdef CONFIG_SND_HDA_HWDEP
1994 /* execute additional init verbs */
1995 static void hda_exec_init_verbs(struct hda_codec *codec)
1997 if (codec->init_verbs.list)
1998 snd_hda_sequence_write(codec, codec->init_verbs.list);
2001 static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
2004 #ifdef SND_HDA_NEEDS_RESUME
2006 * call suspend and power-down; used both from PM and power-save
2008 static void hda_call_codec_suspend(struct hda_codec *codec)
2010 if (codec->patch_ops.suspend)
2011 codec->patch_ops.suspend(codec, PMSG_SUSPEND);
2012 hda_set_power_state(codec,
2013 codec->afg ? codec->afg : codec->mfg,
2015 #ifdef CONFIG_SND_HDA_POWER_SAVE
2016 cancel_delayed_work(&codec->power_work);
2017 codec->power_on = 0;
2018 codec->power_transition = 0;
2023 * kick up codec; used both from PM and power-save
2025 static void hda_call_codec_resume(struct hda_codec *codec)
2027 hda_set_power_state(codec,
2028 codec->afg ? codec->afg : codec->mfg,
2030 hda_exec_init_verbs(codec);
2031 if (codec->patch_ops.resume)
2032 codec->patch_ops.resume(codec);
2034 if (codec->patch_ops.init)
2035 codec->patch_ops.init(codec);
2036 snd_hda_codec_resume_amp(codec);
2037 snd_hda_codec_resume_cache(codec);
2040 #endif /* SND_HDA_NEEDS_RESUME */
2044 * snd_hda_build_controls - build mixer controls
2047 * Creates mixer controls for each codec included in the bus.
2049 * Returns 0 if successful, otherwise a negative error code.
2051 int __devinit snd_hda_build_controls(struct hda_bus *bus)
2053 struct hda_codec *codec;
2055 list_for_each_entry(codec, &bus->codec_list, list) {
2056 int err = snd_hda_codec_build_controls(codec);
2063 int snd_hda_codec_build_controls(struct hda_codec *codec)
2066 /* fake as if already powered-on */
2067 hda_keep_power_on(codec);
2069 hda_set_power_state(codec,
2070 codec->afg ? codec->afg : codec->mfg,
2072 hda_exec_init_verbs(codec);
2073 /* continue to initialize... */
2074 if (codec->patch_ops.init)
2075 err = codec->patch_ops.init(codec);
2076 if (!err && codec->patch_ops.build_controls)
2077 err = codec->patch_ops.build_controls(codec);
2078 snd_hda_power_down(codec);
2087 struct hda_rate_tbl {
2089 unsigned int alsa_bits;
2090 unsigned int hda_fmt;
2093 static struct hda_rate_tbl rate_bits[] = {
2094 /* rate in Hz, ALSA rate bitmask, HDA format value */
2096 /* autodetected value used in snd_hda_query_supported_pcm */
2097 { 8000, SNDRV_PCM_RATE_8000, 0x0500 }, /* 1/6 x 48 */
2098 { 11025, SNDRV_PCM_RATE_11025, 0x4300 }, /* 1/4 x 44 */
2099 { 16000, SNDRV_PCM_RATE_16000, 0x0200 }, /* 1/3 x 48 */
2100 { 22050, SNDRV_PCM_RATE_22050, 0x4100 }, /* 1/2 x 44 */
2101 { 32000, SNDRV_PCM_RATE_32000, 0x0a00 }, /* 2/3 x 48 */
2102 { 44100, SNDRV_PCM_RATE_44100, 0x4000 }, /* 44 */
2103 { 48000, SNDRV_PCM_RATE_48000, 0x0000 }, /* 48 */
2104 { 88200, SNDRV_PCM_RATE_88200, 0x4800 }, /* 2 x 44 */
2105 { 96000, SNDRV_PCM_RATE_96000, 0x0800 }, /* 2 x 48 */
2106 { 176400, SNDRV_PCM_RATE_176400, 0x5800 },/* 4 x 44 */
2107 { 192000, SNDRV_PCM_RATE_192000, 0x1800 }, /* 4 x 48 */
2108 #define AC_PAR_PCM_RATE_BITS 11
2109 /* up to bits 10, 384kHZ isn't supported properly */
2111 /* not autodetected value */
2112 { 9600, SNDRV_PCM_RATE_KNOT, 0x0400 }, /* 1/5 x 48 */
2114 { 0 } /* terminator */
2118 * snd_hda_calc_stream_format - calculate format bitset
2119 * @rate: the sample rate
2120 * @channels: the number of channels
2121 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
2122 * @maxbps: the max. bps
2124 * Calculate the format bitset from the given rate, channels and th PCM format.
2126 * Return zero if invalid.
2128 unsigned int snd_hda_calc_stream_format(unsigned int rate,
2129 unsigned int channels,
2130 unsigned int format,
2131 unsigned int maxbps)
2134 unsigned int val = 0;
2136 for (i = 0; rate_bits[i].hz; i++)
2137 if (rate_bits[i].hz == rate) {
2138 val = rate_bits[i].hda_fmt;
2141 if (!rate_bits[i].hz) {
2142 snd_printdd("invalid rate %d\n", rate);
2146 if (channels == 0 || channels > 8) {
2147 snd_printdd("invalid channels %d\n", channels);
2150 val |= channels - 1;
2152 switch (snd_pcm_format_width(format)) {
2153 case 8: val |= 0x00; break;
2154 case 16: val |= 0x10; break;
2160 else if (maxbps >= 24)
2166 snd_printdd("invalid format width %d\n",
2167 snd_pcm_format_width(format));
2175 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
2176 * @codec: the HDA codec
2177 * @nid: NID to query
2178 * @ratesp: the pointer to store the detected rate bitflags
2179 * @formatsp: the pointer to store the detected formats
2180 * @bpsp: the pointer to store the detected format widths
2182 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
2183 * or @bsps argument is ignored.
2185 * Returns 0 if successful, otherwise a negative error code.
2187 int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
2188 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
2191 unsigned int val, streams;
2194 if (nid != codec->afg &&
2195 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD)) {
2196 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
2201 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
2205 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
2207 rates |= rate_bits[i].alsa_bits;
2212 if (formatsp || bpsp) {
2217 wcaps = get_wcaps(codec, nid);
2218 streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
2222 streams = snd_hda_param_read(codec, codec->afg,
2229 if (streams & AC_SUPFMT_PCM) {
2230 if (val & AC_SUPPCM_BITS_8) {
2231 formats |= SNDRV_PCM_FMTBIT_U8;
2234 if (val & AC_SUPPCM_BITS_16) {
2235 formats |= SNDRV_PCM_FMTBIT_S16_LE;
2238 if (wcaps & AC_WCAP_DIGITAL) {
2239 if (val & AC_SUPPCM_BITS_32)
2240 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
2241 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
2242 formats |= SNDRV_PCM_FMTBIT_S32_LE;
2243 if (val & AC_SUPPCM_BITS_24)
2245 else if (val & AC_SUPPCM_BITS_20)
2247 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
2248 AC_SUPPCM_BITS_32)) {
2249 formats |= SNDRV_PCM_FMTBIT_S32_LE;
2250 if (val & AC_SUPPCM_BITS_32)
2252 else if (val & AC_SUPPCM_BITS_24)
2254 else if (val & AC_SUPPCM_BITS_20)
2258 else if (streams == AC_SUPFMT_FLOAT32) {
2259 /* should be exclusive */
2260 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
2262 } else if (streams == AC_SUPFMT_AC3) {
2263 /* should be exclusive */
2264 /* temporary hack: we have still no proper support
2265 * for the direct AC3 stream...
2267 formats |= SNDRV_PCM_FMTBIT_U8;
2271 *formatsp = formats;
2280 * snd_hda_is_supported_format - check whether the given node supports
2283 * Returns 1 if supported, 0 if not.
2285 int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
2286 unsigned int format)
2289 unsigned int val = 0, rate, stream;
2291 if (nid != codec->afg &&
2292 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD)) {
2293 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
2298 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
2303 rate = format & 0xff00;
2304 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
2305 if (rate_bits[i].hda_fmt == rate) {
2310 if (i >= AC_PAR_PCM_RATE_BITS)
2313 stream = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
2316 if (!stream && nid != codec->afg)
2317 stream = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
2318 if (!stream || stream == -1)
2321 if (stream & AC_SUPFMT_PCM) {
2322 switch (format & 0xf0) {
2324 if (!(val & AC_SUPPCM_BITS_8))
2328 if (!(val & AC_SUPPCM_BITS_16))
2332 if (!(val & AC_SUPPCM_BITS_20))
2336 if (!(val & AC_SUPPCM_BITS_24))
2340 if (!(val & AC_SUPPCM_BITS_32))
2347 /* FIXME: check for float32 and AC3? */
2356 static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
2357 struct hda_codec *codec,
2358 struct snd_pcm_substream *substream)
2363 static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
2364 struct hda_codec *codec,
2365 unsigned int stream_tag,
2366 unsigned int format,
2367 struct snd_pcm_substream *substream)
2369 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
2373 static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
2374 struct hda_codec *codec,
2375 struct snd_pcm_substream *substream)
2377 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
2381 static int set_pcm_default_values(struct hda_codec *codec,
2382 struct hda_pcm_stream *info)
2384 /* query support PCM information from the given NID */
2385 if (info->nid && (!info->rates || !info->formats)) {
2386 snd_hda_query_supported_pcm(codec, info->nid,
2387 info->rates ? NULL : &info->rates,
2388 info->formats ? NULL : &info->formats,
2389 info->maxbps ? NULL : &info->maxbps);
2391 if (info->ops.open == NULL)
2392 info->ops.open = hda_pcm_default_open_close;
2393 if (info->ops.close == NULL)
2394 info->ops.close = hda_pcm_default_open_close;
2395 if (info->ops.prepare == NULL) {
2396 if (snd_BUG_ON(!info->nid))
2398 info->ops.prepare = hda_pcm_default_prepare;
2400 if (info->ops.cleanup == NULL) {
2401 if (snd_BUG_ON(!info->nid))
2403 info->ops.cleanup = hda_pcm_default_cleanup;
2409 * attach a new PCM stream
2411 static int __devinit
2412 snd_hda_attach_pcm(struct hda_codec *codec, struct hda_pcm *pcm)
2414 struct hda_pcm_stream *info;
2417 if (snd_BUG_ON(!pcm->name))
2419 for (stream = 0; stream < 2; stream++) {
2420 info = &pcm->stream[stream];
2421 if (info->substreams) {
2422 err = set_pcm_default_values(codec, info);
2427 return codec->bus->ops.attach_pcm(codec, pcm);
2431 * snd_hda_build_pcms - build PCM information
2434 * Create PCM information for each codec included in the bus.
2436 * The build_pcms codec patch is requested to set up codec->num_pcms and
2437 * codec->pcm_info properly. The array is referred by the top-level driver
2438 * to create its PCM instances.
2439 * The allocated codec->pcm_info should be released in codec->patch_ops.free
2442 * At least, substreams, channels_min and channels_max must be filled for
2443 * each stream. substreams = 0 indicates that the stream doesn't exist.
2444 * When rates and/or formats are zero, the supported values are queried
2445 * from the given nid. The nid is used also by the default ops.prepare
2446 * and ops.cleanup callbacks.
2448 * The driver needs to call ops.open in its open callback. Similarly,
2449 * ops.close is supposed to be called in the close callback.
2450 * ops.prepare should be called in the prepare or hw_params callback
2451 * with the proper parameters for set up.
2452 * ops.cleanup should be called in hw_free for clean up of streams.
2454 * This function returns 0 if successfull, or a negative error code.
2456 int snd_hda_build_pcms(struct hda_bus *bus)
2458 static const char *dev_name[HDA_PCM_NTYPES] = {
2459 "Audio", "SPDIF", "HDMI", "Modem"
2461 /* starting device index for each PCM type */
2462 static int dev_idx[HDA_PCM_NTYPES] = {
2463 [HDA_PCM_TYPE_AUDIO] = 0,
2464 [HDA_PCM_TYPE_SPDIF] = 1,
2465 [HDA_PCM_TYPE_HDMI] = 3,
2466 [HDA_PCM_TYPE_MODEM] = 6
2468 /* normal audio device indices; not linear to keep compatibility */
2469 static int audio_idx[4] = { 0, 2, 4, 5 };
2470 struct hda_codec *codec;
2471 int num_devs[HDA_PCM_NTYPES];
2473 memset(num_devs, 0, sizeof(num_devs));
2474 list_for_each_entry(codec, &bus->codec_list, list) {
2477 if (!codec->num_pcms) {
2478 if (!codec->patch_ops.build_pcms)
2480 err = codec->patch_ops.build_pcms(codec);
2484 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
2485 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
2486 int type = cpcm->pcm_type;
2489 if (!cpcm->stream[0].substreams &&
2490 !cpcm->stream[1].substreams)
2491 continue; /* no substreams assigned */
2494 case HDA_PCM_TYPE_AUDIO:
2495 if (num_devs[type] >= ARRAY_SIZE(audio_idx)) {
2496 snd_printk(KERN_WARNING
2497 "Too many audio devices\n");
2500 dev = audio_idx[num_devs[type]];
2502 case HDA_PCM_TYPE_SPDIF:
2503 case HDA_PCM_TYPE_HDMI:
2504 case HDA_PCM_TYPE_MODEM:
2505 if (num_devs[type]) {
2506 snd_printk(KERN_WARNING
2507 "%s already defined\n",
2511 dev = dev_idx[type];
2514 snd_printk(KERN_WARNING
2515 "Invalid PCM type %d\n", type);
2521 err = snd_hda_attach_pcm(codec, cpcm);
2531 * snd_hda_check_board_config - compare the current codec with the config table
2532 * @codec: the HDA codec
2533 * @num_configs: number of config enums
2534 * @models: array of model name strings
2535 * @tbl: configuration table, terminated by null entries
2537 * Compares the modelname or PCI subsystem id of the current codec with the
2538 * given configuration table. If a matching entry is found, returns its
2539 * config value (supposed to be 0 or positive).
2541 * If no entries are matching, the function returns a negative value.
2543 int snd_hda_check_board_config(struct hda_codec *codec,
2544 int num_configs, const char **models,
2545 const struct snd_pci_quirk *tbl)
2547 if (codec->modelname && models) {
2549 for (i = 0; i < num_configs; i++) {
2551 !strcmp(codec->modelname, models[i])) {
2552 snd_printd(KERN_INFO "hda_codec: model '%s' is "
2553 "selected\n", models[i]);
2559 if (!codec->bus->pci || !tbl)
2562 tbl = snd_pci_quirk_lookup(codec->bus->pci, tbl);
2565 if (tbl->value >= 0 && tbl->value < num_configs) {
2566 #ifdef CONFIG_SND_DEBUG_VERBOSE
2568 const char *model = NULL;
2570 model = models[tbl->value];
2572 sprintf(tmp, "#%d", tbl->value);
2575 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
2576 "for config %x:%x (%s)\n",
2577 model, tbl->subvendor, tbl->subdevice,
2578 (tbl->name ? tbl->name : "Unknown device"));
2586 * snd_hda_add_new_ctls - create controls from the array
2587 * @codec: the HDA codec
2588 * @knew: the array of struct snd_kcontrol_new
2590 * This helper function creates and add new controls in the given array.
2591 * The array must be terminated with an empty entry as terminator.
2593 * Returns 0 if successful, or a negative error code.
2595 int snd_hda_add_new_ctls(struct hda_codec *codec, struct snd_kcontrol_new *knew)
2599 for (; knew->name; knew++) {
2600 struct snd_kcontrol *kctl;
2601 kctl = snd_ctl_new1(knew, codec);
2604 err = snd_hda_ctl_add(codec, kctl);
2608 kctl = snd_ctl_new1(knew, codec);
2611 kctl->id.device = codec->addr;
2612 err = snd_hda_ctl_add(codec, kctl);
2620 #ifdef CONFIG_SND_HDA_POWER_SAVE
2621 static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
2622 unsigned int power_state);
2624 static void hda_power_work(struct work_struct *work)
2626 struct hda_codec *codec =
2627 container_of(work, struct hda_codec, power_work.work);
2629 if (!codec->power_on || codec->power_count) {
2630 codec->power_transition = 0;
2634 hda_call_codec_suspend(codec);
2635 if (codec->bus->ops.pm_notify)
2636 codec->bus->ops.pm_notify(codec);
2639 static void hda_keep_power_on(struct hda_codec *codec)
2641 codec->power_count++;
2642 codec->power_on = 1;
2645 void snd_hda_power_up(struct hda_codec *codec)
2647 codec->power_count++;
2648 if (codec->power_on || codec->power_transition)
2651 codec->power_on = 1;
2652 if (codec->bus->ops.pm_notify)
2653 codec->bus->ops.pm_notify(codec);
2654 hda_call_codec_resume(codec);
2655 cancel_delayed_work(&codec->power_work);
2656 codec->power_transition = 0;
2659 void snd_hda_power_down(struct hda_codec *codec)
2661 --codec->power_count;
2662 if (!codec->power_on || codec->power_count || codec->power_transition)
2665 codec->power_transition = 1; /* avoid reentrance */
2666 schedule_delayed_work(&codec->power_work,
2667 msecs_to_jiffies(power_save * 1000));
2671 int snd_hda_check_amp_list_power(struct hda_codec *codec,
2672 struct hda_loopback_check *check,
2675 struct hda_amp_list *p;
2678 if (!check->amplist)
2680 for (p = check->amplist; p->nid; p++) {
2685 return 0; /* nothing changed */
2687 for (p = check->amplist; p->nid; p++) {
2688 for (ch = 0; ch < 2; ch++) {
2689 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
2691 if (!(v & HDA_AMP_MUTE) && v > 0) {
2692 if (!check->power_on) {
2693 check->power_on = 1;
2694 snd_hda_power_up(codec);
2700 if (check->power_on) {
2701 check->power_on = 0;
2702 snd_hda_power_down(codec);
2709 * Channel mode helper
2711 int snd_hda_ch_mode_info(struct hda_codec *codec,
2712 struct snd_ctl_elem_info *uinfo,
2713 const struct hda_channel_mode *chmode,
2716 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2718 uinfo->value.enumerated.items = num_chmodes;
2719 if (uinfo->value.enumerated.item >= num_chmodes)
2720 uinfo->value.enumerated.item = num_chmodes - 1;
2721 sprintf(uinfo->value.enumerated.name, "%dch",
2722 chmode[uinfo->value.enumerated.item].channels);
2726 int snd_hda_ch_mode_get(struct hda_codec *codec,
2727 struct snd_ctl_elem_value *ucontrol,
2728 const struct hda_channel_mode *chmode,
2734 for (i = 0; i < num_chmodes; i++) {
2735 if (max_channels == chmode[i].channels) {
2736 ucontrol->value.enumerated.item[0] = i;
2743 int snd_hda_ch_mode_put(struct hda_codec *codec,
2744 struct snd_ctl_elem_value *ucontrol,
2745 const struct hda_channel_mode *chmode,
2751 mode = ucontrol->value.enumerated.item[0];
2752 if (mode >= num_chmodes)
2754 if (*max_channelsp == chmode[mode].channels)
2756 /* change the current channel setting */
2757 *max_channelsp = chmode[mode].channels;
2758 if (chmode[mode].sequence)
2759 snd_hda_sequence_write_cache(codec, chmode[mode].sequence);
2766 int snd_hda_input_mux_info(const struct hda_input_mux *imux,
2767 struct snd_ctl_elem_info *uinfo)
2771 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2773 uinfo->value.enumerated.items = imux->num_items;
2774 if (!imux->num_items)
2776 index = uinfo->value.enumerated.item;
2777 if (index >= imux->num_items)
2778 index = imux->num_items - 1;
2779 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
2783 int snd_hda_input_mux_put(struct hda_codec *codec,
2784 const struct hda_input_mux *imux,
2785 struct snd_ctl_elem_value *ucontrol,
2787 unsigned int *cur_val)
2791 if (!imux->num_items)
2793 idx = ucontrol->value.enumerated.item[0];
2794 if (idx >= imux->num_items)
2795 idx = imux->num_items - 1;
2796 if (*cur_val == idx)
2798 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
2799 imux->items[idx].index);
2806 * Multi-channel / digital-out PCM helper functions
2809 /* setup SPDIF output stream */
2810 static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
2811 unsigned int stream_tag, unsigned int format)
2813 /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */
2814 if (codec->spdif_status_reset && (codec->spdif_ctls & AC_DIG1_ENABLE))
2815 set_dig_out_convert(codec, nid,
2816 codec->spdif_ctls & ~AC_DIG1_ENABLE & 0xff,
2818 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
2819 if (codec->slave_dig_outs) {
2821 for (d = codec->slave_dig_outs; *d; d++)
2822 snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
2825 /* turn on again (if needed) */
2826 if (codec->spdif_status_reset && (codec->spdif_ctls & AC_DIG1_ENABLE))
2827 set_dig_out_convert(codec, nid,
2828 codec->spdif_ctls & 0xff, -1);
2831 static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
2833 snd_hda_codec_cleanup_stream(codec, nid);
2834 if (codec->slave_dig_outs) {
2836 for (d = codec->slave_dig_outs; *d; d++)
2837 snd_hda_codec_cleanup_stream(codec, *d);
2842 * open the digital out in the exclusive mode
2844 int snd_hda_multi_out_dig_open(struct hda_codec *codec,
2845 struct hda_multi_out *mout)
2847 mutex_lock(&codec->spdif_mutex);
2848 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
2849 /* already opened as analog dup; reset it once */
2850 cleanup_dig_out_stream(codec, mout->dig_out_nid);
2851 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
2852 mutex_unlock(&codec->spdif_mutex);
2856 int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
2857 struct hda_multi_out *mout,
2858 unsigned int stream_tag,
2859 unsigned int format,
2860 struct snd_pcm_substream *substream)
2862 mutex_lock(&codec->spdif_mutex);
2863 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
2864 mutex_unlock(&codec->spdif_mutex);
2869 * release the digital out
2871 int snd_hda_multi_out_dig_close(struct hda_codec *codec,
2872 struct hda_multi_out *mout)
2874 mutex_lock(&codec->spdif_mutex);
2875 mout->dig_out_used = 0;
2876 mutex_unlock(&codec->spdif_mutex);
2881 * set up more restrictions for analog out
2883 int snd_hda_multi_out_analog_open(struct hda_codec *codec,
2884 struct hda_multi_out *mout,
2885 struct snd_pcm_substream *substream,
2886 struct hda_pcm_stream *hinfo)
2888 struct snd_pcm_runtime *runtime = substream->runtime;
2889 runtime->hw.channels_max = mout->max_channels;
2890 if (mout->dig_out_nid) {
2891 if (!mout->analog_rates) {
2892 mout->analog_rates = hinfo->rates;
2893 mout->analog_formats = hinfo->formats;
2894 mout->analog_maxbps = hinfo->maxbps;
2896 runtime->hw.rates = mout->analog_rates;
2897 runtime->hw.formats = mout->analog_formats;
2898 hinfo->maxbps = mout->analog_maxbps;
2900 if (!mout->spdif_rates) {
2901 snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
2903 &mout->spdif_formats,
2904 &mout->spdif_maxbps);
2906 mutex_lock(&codec->spdif_mutex);
2907 if (mout->share_spdif) {
2908 runtime->hw.rates &= mout->spdif_rates;
2909 runtime->hw.formats &= mout->spdif_formats;
2910 if (mout->spdif_maxbps < hinfo->maxbps)
2911 hinfo->maxbps = mout->spdif_maxbps;
2913 mutex_unlock(&codec->spdif_mutex);
2915 return snd_pcm_hw_constraint_step(substream->runtime, 0,
2916 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
2920 * set up the i/o for analog out
2921 * when the digital out is available, copy the front out to digital out, too.
2923 int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
2924 struct hda_multi_out *mout,
2925 unsigned int stream_tag,
2926 unsigned int format,
2927 struct snd_pcm_substream *substream)
2929 hda_nid_t *nids = mout->dac_nids;
2930 int chs = substream->runtime->channels;
2933 mutex_lock(&codec->spdif_mutex);
2934 if (mout->dig_out_nid && mout->share_spdif &&
2935 mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
2937 snd_hda_is_supported_format(codec, mout->dig_out_nid,
2939 !(codec->spdif_status & IEC958_AES0_NONAUDIO)) {
2940 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
2941 setup_dig_out_stream(codec, mout->dig_out_nid,
2942 stream_tag, format);
2944 mout->dig_out_used = 0;
2945 cleanup_dig_out_stream(codec, mout->dig_out_nid);
2948 mutex_unlock(&codec->spdif_mutex);
2951 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
2953 if (!mout->no_share_stream &&
2954 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
2955 /* headphone out will just decode front left/right (stereo) */
2956 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
2958 /* extra outputs copied from front */
2959 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
2960 if (!mout->no_share_stream && mout->extra_out_nid[i])
2961 snd_hda_codec_setup_stream(codec,
2962 mout->extra_out_nid[i],
2963 stream_tag, 0, format);
2966 for (i = 1; i < mout->num_dacs; i++) {
2967 if (chs >= (i + 1) * 2) /* independent out */
2968 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
2970 else if (!mout->no_share_stream) /* copy front */
2971 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
2978 * clean up the setting for analog out
2980 int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
2981 struct hda_multi_out *mout)
2983 hda_nid_t *nids = mout->dac_nids;
2986 for (i = 0; i < mout->num_dacs; i++)
2987 snd_hda_codec_cleanup_stream(codec, nids[i]);
2989 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
2990 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
2991 if (mout->extra_out_nid[i])
2992 snd_hda_codec_cleanup_stream(codec,
2993 mout->extra_out_nid[i]);
2994 mutex_lock(&codec->spdif_mutex);
2995 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
2996 cleanup_dig_out_stream(codec, mout->dig_out_nid);
2997 mout->dig_out_used = 0;
2999 mutex_unlock(&codec->spdif_mutex);
3004 * Helper for automatic pin configuration
3007 static int is_in_nid_list(hda_nid_t nid, hda_nid_t *list)
3009 for (; *list; list++)
3017 * Sort an associated group of pins according to their sequence numbers.
3019 static void sort_pins_by_sequence(hda_nid_t * pins, short * sequences,
3026 for (i = 0; i < num_pins; i++) {
3027 for (j = i + 1; j < num_pins; j++) {
3028 if (sequences[i] > sequences[j]) {
3030 sequences[i] = sequences[j];
3042 * Parse all pin widgets and store the useful pin nids to cfg
3044 * The number of line-outs or any primary output is stored in line_outs,
3045 * and the corresponding output pins are assigned to line_out_pins[],
3046 * in the order of front, rear, CLFE, side, ...
3048 * If more extra outputs (speaker and headphone) are found, the pins are
3049 * assisnged to hp_pins[] and speaker_pins[], respectively. If no line-out jack
3050 * is detected, one of speaker of HP pins is assigned as the primary
3051 * output, i.e. to line_out_pins[0]. So, line_outs is always positive
3052 * if any analog output exists.
3054 * The analog input pins are assigned to input_pins array.
3055 * The digital input/output pins are assigned to dig_in_pin and dig_out_pin,
3058 int snd_hda_parse_pin_def_config(struct hda_codec *codec,
3059 struct auto_pin_cfg *cfg,
3060 hda_nid_t *ignore_nids)
3062 hda_nid_t nid, end_nid;
3063 short seq, assoc_line_out, assoc_speaker;
3064 short sequences_line_out[ARRAY_SIZE(cfg->line_out_pins)];
3065 short sequences_speaker[ARRAY_SIZE(cfg->speaker_pins)];
3066 short sequences_hp[ARRAY_SIZE(cfg->hp_pins)];
3068 memset(cfg, 0, sizeof(*cfg));
3070 memset(sequences_line_out, 0, sizeof(sequences_line_out));
3071 memset(sequences_speaker, 0, sizeof(sequences_speaker));
3072 memset(sequences_hp, 0, sizeof(sequences_hp));
3073 assoc_line_out = assoc_speaker = 0;
3075 end_nid = codec->start_nid + codec->num_nodes;
3076 for (nid = codec->start_nid; nid < end_nid; nid++) {
3077 unsigned int wid_caps = get_wcaps(codec, nid);
3078 unsigned int wid_type =
3079 (wid_caps & AC_WCAP_TYPE) >> AC_WCAP_TYPE_SHIFT;
3080 unsigned int def_conf;
3083 /* read all default configuration for pin complex */
3084 if (wid_type != AC_WID_PIN)
3086 /* ignore the given nids (e.g. pc-beep returns error) */
3087 if (ignore_nids && is_in_nid_list(nid, ignore_nids))
3090 def_conf = snd_hda_codec_read(codec, nid, 0,
3091 AC_VERB_GET_CONFIG_DEFAULT, 0);
3092 if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE)
3094 loc = get_defcfg_location(def_conf);
3095 switch (get_defcfg_device(def_conf)) {
3096 case AC_JACK_LINE_OUT:
3097 seq = get_defcfg_sequence(def_conf);
3098 assoc = get_defcfg_association(def_conf);
3100 if (!(wid_caps & AC_WCAP_STEREO))
3101 if (!cfg->mono_out_pin)
3102 cfg->mono_out_pin = nid;
3105 if (!assoc_line_out)
3106 assoc_line_out = assoc;
3107 else if (assoc_line_out != assoc)
3109 if (cfg->line_outs >= ARRAY_SIZE(cfg->line_out_pins))
3111 cfg->line_out_pins[cfg->line_outs] = nid;
3112 sequences_line_out[cfg->line_outs] = seq;
3115 case AC_JACK_SPEAKER:
3116 seq = get_defcfg_sequence(def_conf);
3117 assoc = get_defcfg_association(def_conf);
3120 if (! assoc_speaker)
3121 assoc_speaker = assoc;
3122 else if (assoc_speaker != assoc)
3124 if (cfg->speaker_outs >= ARRAY_SIZE(cfg->speaker_pins))
3126 cfg->speaker_pins[cfg->speaker_outs] = nid;
3127 sequences_speaker[cfg->speaker_outs] = seq;
3128 cfg->speaker_outs++;
3130 case AC_JACK_HP_OUT:
3131 seq = get_defcfg_sequence(def_conf);
3132 assoc = get_defcfg_association(def_conf);
3133 if (cfg->hp_outs >= ARRAY_SIZE(cfg->hp_pins))
3135 cfg->hp_pins[cfg->hp_outs] = nid;
3136 sequences_hp[cfg->hp_outs] = (assoc << 4) | seq;
3139 case AC_JACK_MIC_IN: {
3141 if (loc == AC_JACK_LOC_FRONT) {
3142 preferred = AUTO_PIN_FRONT_MIC;
3145 preferred = AUTO_PIN_MIC;
3146 alt = AUTO_PIN_FRONT_MIC;
3148 if (!cfg->input_pins[preferred])
3149 cfg->input_pins[preferred] = nid;
3150 else if (!cfg->input_pins[alt])
3151 cfg->input_pins[alt] = nid;
3154 case AC_JACK_LINE_IN:
3155 if (loc == AC_JACK_LOC_FRONT)
3156 cfg->input_pins[AUTO_PIN_FRONT_LINE] = nid;
3158 cfg->input_pins[AUTO_PIN_LINE] = nid;
3161 cfg->input_pins[AUTO_PIN_CD] = nid;
3164 cfg->input_pins[AUTO_PIN_AUX] = nid;
3166 case AC_JACK_SPDIF_OUT:
3167 cfg->dig_out_pin = nid;
3169 case AC_JACK_SPDIF_IN:
3170 cfg->dig_in_pin = nid;
3176 * If no line-out is defined but multiple HPs are found,
3177 * some of them might be the real line-outs.
3179 if (!cfg->line_outs && cfg->hp_outs > 1) {
3181 while (i < cfg->hp_outs) {
3182 /* The real HPs should have the sequence 0x0f */
3183 if ((sequences_hp[i] & 0x0f) == 0x0f) {
3187 /* Move it to the line-out table */
3188 cfg->line_out_pins[cfg->line_outs] = cfg->hp_pins[i];
3189 sequences_line_out[cfg->line_outs] = sequences_hp[i];
3192 memmove(cfg->hp_pins + i, cfg->hp_pins + i + 1,
3193 sizeof(cfg->hp_pins[0]) * (cfg->hp_outs - i));
3194 memmove(sequences_hp + i - 1, sequences_hp + i,
3195 sizeof(sequences_hp[0]) * (cfg->hp_outs - i));
3199 /* sort by sequence */
3200 sort_pins_by_sequence(cfg->line_out_pins, sequences_line_out,
3202 sort_pins_by_sequence(cfg->speaker_pins, sequences_speaker,
3204 sort_pins_by_sequence(cfg->hp_pins, sequences_hp,
3207 /* if we have only one mic, make it AUTO_PIN_MIC */
3208 if (!cfg->input_pins[AUTO_PIN_MIC] &&
3209 cfg->input_pins[AUTO_PIN_FRONT_MIC]) {
3210 cfg->input_pins[AUTO_PIN_MIC] =
3211 cfg->input_pins[AUTO_PIN_FRONT_MIC];
3212 cfg->input_pins[AUTO_PIN_FRONT_MIC] = 0;
3214 /* ditto for line-in */
3215 if (!cfg->input_pins[AUTO_PIN_LINE] &&
3216 cfg->input_pins[AUTO_PIN_FRONT_LINE]) {
3217 cfg->input_pins[AUTO_PIN_LINE] =
3218 cfg->input_pins[AUTO_PIN_FRONT_LINE];
3219 cfg->input_pins[AUTO_PIN_FRONT_LINE] = 0;
3223 * FIX-UP: if no line-outs are detected, try to use speaker or HP pin
3224 * as a primary output
3226 if (!cfg->line_outs) {
3227 if (cfg->speaker_outs) {
3228 cfg->line_outs = cfg->speaker_outs;
3229 memcpy(cfg->line_out_pins, cfg->speaker_pins,
3230 sizeof(cfg->speaker_pins));
3231 cfg->speaker_outs = 0;
3232 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
3233 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
3234 } else if (cfg->hp_outs) {
3235 cfg->line_outs = cfg->hp_outs;
3236 memcpy(cfg->line_out_pins, cfg->hp_pins,
3237 sizeof(cfg->hp_pins));
3239 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
3240 cfg->line_out_type = AUTO_PIN_HP_OUT;
3244 /* Reorder the surround channels
3245 * ALSA sequence is front/surr/clfe/side
3247 * 4-ch: front/surr => OK as it is
3248 * 6-ch: front/clfe/surr
3249 * 8-ch: front/clfe/rear/side|fc
3251 switch (cfg->line_outs) {
3254 nid = cfg->line_out_pins[1];
3255 cfg->line_out_pins[1] = cfg->line_out_pins[2];
3256 cfg->line_out_pins[2] = nid;
3261 * debug prints of the parsed results
3263 snd_printd("autoconfig: line_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
3264 cfg->line_outs, cfg->line_out_pins[0], cfg->line_out_pins[1],
3265 cfg->line_out_pins[2], cfg->line_out_pins[3],
3266 cfg->line_out_pins[4]);
3267 snd_printd(" speaker_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
3268 cfg->speaker_outs, cfg->speaker_pins[0],
3269 cfg->speaker_pins[1], cfg->speaker_pins[2],
3270 cfg->speaker_pins[3], cfg->speaker_pins[4]);
3271 snd_printd(" hp_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
3272 cfg->hp_outs, cfg->hp_pins[0],
3273 cfg->hp_pins[1], cfg->hp_pins[2],
3274 cfg->hp_pins[3], cfg->hp_pins[4]);
3275 snd_printd(" mono: mono_out=0x%x\n", cfg->mono_out_pin);
3276 snd_printd(" inputs: mic=0x%x, fmic=0x%x, line=0x%x, fline=0x%x,"
3277 " cd=0x%x, aux=0x%x\n",
3278 cfg->input_pins[AUTO_PIN_MIC],
3279 cfg->input_pins[AUTO_PIN_FRONT_MIC],
3280 cfg->input_pins[AUTO_PIN_LINE],
3281 cfg->input_pins[AUTO_PIN_FRONT_LINE],
3282 cfg->input_pins[AUTO_PIN_CD],
3283 cfg->input_pins[AUTO_PIN_AUX]);
3288 /* labels for input pins */
3289 const char *auto_pin_cfg_labels[AUTO_PIN_LAST] = {
3290 "Mic", "Front Mic", "Line", "Front Line", "CD", "Aux"
3300 * snd_hda_suspend - suspend the codecs
3302 * @state: suspsend state
3304 * Returns 0 if successful.
3306 int snd_hda_suspend(struct hda_bus *bus, pm_message_t state)
3308 struct hda_codec *codec;
3310 list_for_each_entry(codec, &bus->codec_list, list) {
3311 #ifdef CONFIG_SND_HDA_POWER_SAVE
3312 if (!codec->power_on)
3315 hda_call_codec_suspend(codec);
3321 * snd_hda_resume - resume the codecs
3323 * @state: resume state
3325 * Returns 0 if successful.
3327 * This fucntion is defined only when POWER_SAVE isn't set.
3328 * In the power-save mode, the codec is resumed dynamically.
3330 int snd_hda_resume(struct hda_bus *bus)
3332 struct hda_codec *codec;
3334 list_for_each_entry(codec, &bus->codec_list, list) {
3335 if (snd_hda_codec_needs_resume(codec))
3336 hda_call_codec_resume(codec);
3340 #ifdef CONFIG_SND_HDA_POWER_SAVE
3341 int snd_hda_codecs_inuse(struct hda_bus *bus)
3343 struct hda_codec *codec;
3345 list_for_each_entry(codec, &bus->codec_list, list) {
3346 if (snd_hda_codec_needs_resume(codec))
3358 /* get a new element from the given array
3359 * if it exceeds the pre-allocated array size, re-allocate the array
3361 void *snd_array_new(struct snd_array *array)
3363 if (array->used >= array->alloced) {
3364 int num = array->alloced + array->alloc_align;
3365 void *nlist = kcalloc(num + 1, array->elem_size, GFP_KERNEL);
3369 memcpy(nlist, array->list,
3370 array->elem_size * array->alloced);
3373 array->list = nlist;
3374 array->alloced = num;
3376 return array->list + (array->used++ * array->elem_size);
3379 /* free the given array elements */
3380 void snd_array_free(struct snd_array *array)