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>
36 * vendor / preset table
39 struct hda_vendor_id {
44 /* codec vendor labels */
45 static struct hda_vendor_id hda_vendor_ids[] = {
47 { 0x1057, "Motorola" },
48 { 0x1095, "Silicon Image" },
50 { 0x10ec, "Realtek" },
54 { 0x11d4, "Analog Devices" },
55 { 0x13f6, "C-Media" },
56 { 0x14f1, "Conexant" },
57 { 0x17e8, "Chrontel" },
59 { 0x1aec, "Wolfson Microelectronics" },
60 { 0x434d, "C-Media" },
62 { 0x8384, "SigmaTel" },
66 static DEFINE_MUTEX(preset_mutex);
67 static LIST_HEAD(hda_preset_tables);
69 int snd_hda_add_codec_preset(struct hda_codec_preset_list *preset)
71 mutex_lock(&preset_mutex);
72 list_add_tail(&preset->list, &hda_preset_tables);
73 mutex_unlock(&preset_mutex);
76 EXPORT_SYMBOL_HDA(snd_hda_add_codec_preset);
78 int snd_hda_delete_codec_preset(struct hda_codec_preset_list *preset)
80 mutex_lock(&preset_mutex);
81 list_del(&preset->list);
82 mutex_unlock(&preset_mutex);
85 EXPORT_SYMBOL_HDA(snd_hda_delete_codec_preset);
87 #ifdef CONFIG_SND_HDA_POWER_SAVE
88 static void hda_power_work(struct work_struct *work);
89 static void hda_keep_power_on(struct hda_codec *codec);
91 static inline void hda_keep_power_on(struct hda_codec *codec) {}
94 const char *snd_hda_get_jack_location(u32 cfg)
96 static char *bases[7] = {
97 "N/A", "Rear", "Front", "Left", "Right", "Top", "Bottom",
99 static unsigned char specials_idx[] = {
104 static char *specials[] = {
105 "Rear Panel", "Drive Bar",
106 "Riser", "HDMI", "ATAPI",
107 "Mobile-In", "Mobile-Out"
110 cfg = (cfg & AC_DEFCFG_LOCATION) >> AC_DEFCFG_LOCATION_SHIFT;
111 if ((cfg & 0x0f) < 7)
112 return bases[cfg & 0x0f];
113 for (i = 0; i < ARRAY_SIZE(specials_idx); i++) {
114 if (cfg == specials_idx[i])
119 EXPORT_SYMBOL_HDA(snd_hda_get_jack_location);
121 const char *snd_hda_get_jack_connectivity(u32 cfg)
123 static char *jack_locations[4] = { "Ext", "Int", "Sep", "Oth" };
125 return jack_locations[(cfg >> (AC_DEFCFG_LOCATION_SHIFT + 4)) & 3];
127 EXPORT_SYMBOL_HDA(snd_hda_get_jack_connectivity);
129 const char *snd_hda_get_jack_type(u32 cfg)
131 static char *jack_types[16] = {
132 "Line Out", "Speaker", "HP Out", "CD",
133 "SPDIF Out", "Digital Out", "Modem Line", "Modem Hand",
134 "Line In", "Aux", "Mic", "Telephony",
135 "SPDIF In", "Digitial In", "Reserved", "Other"
138 return jack_types[(cfg & AC_DEFCFG_DEVICE)
139 >> AC_DEFCFG_DEVICE_SHIFT];
141 EXPORT_SYMBOL_HDA(snd_hda_get_jack_type);
144 * Compose a 32bit command word to be sent to the HD-audio controller
146 static inline unsigned int
147 make_codec_cmd(struct hda_codec *codec, hda_nid_t nid, int direct,
148 unsigned int verb, unsigned int parm)
152 val = (u32)(codec->addr & 0x0f) << 28;
153 val |= (u32)direct << 27;
154 val |= (u32)nid << 20;
161 * snd_hda_codec_read - send a command and get the response
162 * @codec: the HDA codec
163 * @nid: NID to send the command
164 * @direct: direct flag
165 * @verb: the verb to send
166 * @parm: the parameter for the verb
168 * Send a single command and read the corresponding response.
170 * Returns the obtained response value, or -1 for an error.
172 unsigned int snd_hda_codec_read(struct hda_codec *codec, hda_nid_t nid,
174 unsigned int verb, unsigned int parm)
176 struct hda_bus *bus = codec->bus;
179 res = make_codec_cmd(codec, nid, direct, verb, parm);
180 snd_hda_power_up(codec);
181 mutex_lock(&bus->cmd_mutex);
182 if (!bus->ops.command(bus, res))
183 res = bus->ops.get_response(bus);
185 res = (unsigned int)-1;
186 mutex_unlock(&bus->cmd_mutex);
187 snd_hda_power_down(codec);
190 EXPORT_SYMBOL_HDA(snd_hda_codec_read);
193 * snd_hda_codec_write - send a single command without waiting for response
194 * @codec: the HDA codec
195 * @nid: NID to send the command
196 * @direct: direct flag
197 * @verb: the verb to send
198 * @parm: the parameter for the verb
200 * Send a single command without waiting for response.
202 * Returns 0 if successful, or a negative error code.
204 int snd_hda_codec_write(struct hda_codec *codec, hda_nid_t nid, int direct,
205 unsigned int verb, unsigned int parm)
207 struct hda_bus *bus = codec->bus;
211 res = make_codec_cmd(codec, nid, direct, verb, parm);
212 snd_hda_power_up(codec);
213 mutex_lock(&bus->cmd_mutex);
214 err = bus->ops.command(bus, res);
215 mutex_unlock(&bus->cmd_mutex);
216 snd_hda_power_down(codec);
219 EXPORT_SYMBOL_HDA(snd_hda_codec_write);
222 * snd_hda_sequence_write - sequence writes
223 * @codec: the HDA codec
224 * @seq: VERB array to send
226 * Send the commands sequentially from the given array.
227 * The array must be terminated with NID=0.
229 void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq)
231 for (; seq->nid; seq++)
232 snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param);
234 EXPORT_SYMBOL_HDA(snd_hda_sequence_write);
237 * snd_hda_get_sub_nodes - get the range of sub nodes
238 * @codec: the HDA codec
240 * @start_id: the pointer to store the start NID
242 * Parse the NID and store the start NID of its sub-nodes.
243 * Returns the number of sub-nodes.
245 int snd_hda_get_sub_nodes(struct hda_codec *codec, hda_nid_t nid,
250 parm = snd_hda_param_read(codec, nid, AC_PAR_NODE_COUNT);
253 *start_id = (parm >> 16) & 0x7fff;
254 return (int)(parm & 0x7fff);
256 EXPORT_SYMBOL_HDA(snd_hda_get_sub_nodes);
259 * snd_hda_get_connections - get connection list
260 * @codec: the HDA codec
262 * @conn_list: connection list array
263 * @max_conns: max. number of connections to store
265 * Parses the connection list of the given widget and stores the list
268 * Returns the number of connections, or a negative error code.
270 int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
271 hda_nid_t *conn_list, int max_conns)
274 int i, conn_len, conns;
275 unsigned int shift, num_elems, mask;
278 if (snd_BUG_ON(!conn_list || max_conns <= 0))
281 parm = snd_hda_param_read(codec, nid, AC_PAR_CONNLIST_LEN);
282 if (parm & AC_CLIST_LONG) {
291 conn_len = parm & AC_CLIST_LENGTH;
292 mask = (1 << (shift-1)) - 1;
295 return 0; /* no connection */
298 /* single connection */
299 parm = snd_hda_codec_read(codec, nid, 0,
300 AC_VERB_GET_CONNECT_LIST, 0);
301 conn_list[0] = parm & mask;
305 /* multi connection */
308 for (i = 0; i < conn_len; i++) {
312 if (i % num_elems == 0)
313 parm = snd_hda_codec_read(codec, nid, 0,
314 AC_VERB_GET_CONNECT_LIST, i);
315 range_val = !!(parm & (1 << (shift-1))); /* ranges */
319 /* ranges between the previous and this one */
320 if (!prev_nid || prev_nid >= val) {
321 snd_printk(KERN_WARNING "hda_codec: "
322 "invalid dep_range_val %x:%x\n",
326 for (n = prev_nid + 1; n <= val; n++) {
327 if (conns >= max_conns) {
329 "Too many connections\n");
332 conn_list[conns++] = n;
335 if (conns >= max_conns) {
336 snd_printk(KERN_ERR "Too many connections\n");
339 conn_list[conns++] = val;
345 EXPORT_SYMBOL_HDA(snd_hda_get_connections);
349 * snd_hda_queue_unsol_event - add an unsolicited event to queue
351 * @res: unsolicited event (lower 32bit of RIRB entry)
352 * @res_ex: codec addr and flags (upper 32bit or RIRB entry)
354 * Adds the given event to the queue. The events are processed in
355 * the workqueue asynchronously. Call this function in the interrupt
356 * hanlder when RIRB receives an unsolicited event.
358 * Returns 0 if successful, or a negative error code.
360 int snd_hda_queue_unsol_event(struct hda_bus *bus, u32 res, u32 res_ex)
362 struct hda_bus_unsolicited *unsol;
369 wp = (unsol->wp + 1) % HDA_UNSOL_QUEUE_SIZE;
373 unsol->queue[wp] = res;
374 unsol->queue[wp + 1] = res_ex;
376 schedule_work(&unsol->work);
380 EXPORT_SYMBOL_HDA(snd_hda_queue_unsol_event);
383 * process queued unsolicited events
385 static void process_unsol_events(struct work_struct *work)
387 struct hda_bus_unsolicited *unsol =
388 container_of(work, struct hda_bus_unsolicited, work);
389 struct hda_bus *bus = unsol->bus;
390 struct hda_codec *codec;
391 unsigned int rp, caddr, res;
393 while (unsol->rp != unsol->wp) {
394 rp = (unsol->rp + 1) % HDA_UNSOL_QUEUE_SIZE;
397 res = unsol->queue[rp];
398 caddr = unsol->queue[rp + 1];
399 if (!(caddr & (1 << 4))) /* no unsolicited event? */
401 codec = bus->caddr_tbl[caddr & 0x0f];
402 if (codec && codec->patch_ops.unsol_event)
403 codec->patch_ops.unsol_event(codec, res);
408 * initialize unsolicited queue
410 static int init_unsol_queue(struct hda_bus *bus)
412 struct hda_bus_unsolicited *unsol;
414 if (bus->unsol) /* already initialized */
417 unsol = kzalloc(sizeof(*unsol), GFP_KERNEL);
419 snd_printk(KERN_ERR "hda_codec: "
420 "can't allocate unsolicited queue\n");
423 INIT_WORK(&unsol->work, process_unsol_events);
432 static void snd_hda_codec_free(struct hda_codec *codec);
434 static int snd_hda_bus_free(struct hda_bus *bus)
436 struct hda_codec *codec, *n;
441 flush_scheduled_work();
444 list_for_each_entry_safe(codec, n, &bus->codec_list, list) {
445 snd_hda_codec_free(codec);
447 if (bus->ops.private_free)
448 bus->ops.private_free(bus);
453 static int snd_hda_bus_dev_free(struct snd_device *device)
455 struct hda_bus *bus = device->device_data;
457 return snd_hda_bus_free(bus);
460 #ifdef CONFIG_SND_HDA_HWDEP
461 static int snd_hda_bus_dev_register(struct snd_device *device)
463 struct hda_bus *bus = device->device_data;
464 struct hda_codec *codec;
465 list_for_each_entry(codec, &bus->codec_list, list) {
466 snd_hda_hwdep_add_sysfs(codec);
471 #define snd_hda_bus_dev_register NULL
475 * snd_hda_bus_new - create a HDA bus
476 * @card: the card entry
477 * @temp: the template for hda_bus information
478 * @busp: the pointer to store the created bus instance
480 * Returns 0 if successful, or a negative error code.
482 int /*__devinit*/ snd_hda_bus_new(struct snd_card *card,
483 const struct hda_bus_template *temp,
484 struct hda_bus **busp)
488 static struct snd_device_ops dev_ops = {
489 .dev_register = snd_hda_bus_dev_register,
490 .dev_free = snd_hda_bus_dev_free,
493 if (snd_BUG_ON(!temp))
495 if (snd_BUG_ON(!temp->ops.command || !temp->ops.get_response))
501 bus = kzalloc(sizeof(*bus), GFP_KERNEL);
503 snd_printk(KERN_ERR "can't allocate struct hda_bus\n");
508 bus->private_data = temp->private_data;
509 bus->pci = temp->pci;
510 bus->modelname = temp->modelname;
511 bus->power_save = temp->power_save;
512 bus->ops = temp->ops;
514 mutex_init(&bus->cmd_mutex);
515 INIT_LIST_HEAD(&bus->codec_list);
517 err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops);
519 snd_hda_bus_free(bus);
526 EXPORT_SYMBOL_HDA(snd_hda_bus_new);
528 #ifdef CONFIG_SND_HDA_GENERIC
529 #define is_generic_config(codec) \
530 (codec->modelname && !strcmp(codec->modelname, "generic"))
532 #define is_generic_config(codec) 0
536 #define HDA_MODREQ_MAX_COUNT 2 /* two request_modules()'s */
538 #define HDA_MODREQ_MAX_COUNT 0 /* all presets are statically linked */
542 * find a matching codec preset
544 static const struct hda_codec_preset *
545 find_codec_preset(struct hda_codec *codec)
547 struct hda_codec_preset_list *tbl;
548 const struct hda_codec_preset *preset;
549 int mod_requested = 0;
551 if (is_generic_config(codec))
552 return NULL; /* use the generic parser */
555 mutex_lock(&preset_mutex);
556 list_for_each_entry(tbl, &hda_preset_tables, list) {
557 if (!try_module_get(tbl->owner)) {
558 snd_printk(KERN_ERR "hda_codec: cannot module_get\n");
561 for (preset = tbl->preset; preset->id; preset++) {
562 u32 mask = preset->mask;
563 if (preset->afg && preset->afg != codec->afg)
565 if (preset->mfg && preset->mfg != codec->mfg)
569 if (preset->id == (codec->vendor_id & mask) &&
571 preset->rev == codec->revision_id)) {
572 mutex_unlock(&preset_mutex);
573 codec->owner = tbl->owner;
577 module_put(tbl->owner);
579 mutex_unlock(&preset_mutex);
581 if (mod_requested < HDA_MODREQ_MAX_COUNT) {
584 snprintf(name, sizeof(name), "snd-hda-codec-id:%08x",
587 snprintf(name, sizeof(name), "snd-hda-codec-id:%04x*",
588 (codec->vendor_id >> 16) & 0xffff);
589 request_module(name);
597 * get_codec_name - store the codec name
599 static int get_codec_name(struct hda_codec *codec)
601 const struct hda_vendor_id *c;
602 const char *vendor = NULL;
603 u16 vendor_id = codec->vendor_id >> 16;
604 char tmp[16], name[32];
606 for (c = hda_vendor_ids; c->id; c++) {
607 if (c->id == vendor_id) {
613 sprintf(tmp, "Generic %04x", vendor_id);
616 if (codec->preset && codec->preset->name)
617 snprintf(name, sizeof(name), "%s %s", vendor,
618 codec->preset->name);
620 snprintf(name, sizeof(name), "%s ID %x", vendor,
621 codec->vendor_id & 0xffff);
622 codec->name = kstrdup(name, GFP_KERNEL);
629 * look for an AFG and MFG nodes
631 static void /*__devinit*/ setup_fg_nodes(struct hda_codec *codec)
636 total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid);
637 for (i = 0; i < total_nodes; i++, nid++) {
639 func = snd_hda_param_read(codec, nid, AC_PAR_FUNCTION_TYPE);
640 switch (func & 0xff) {
641 case AC_GRP_AUDIO_FUNCTION:
644 case AC_GRP_MODEM_FUNCTION:
654 * read widget caps for each widget and store in cache
656 static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
661 codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node,
663 codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL);
666 nid = codec->start_nid;
667 for (i = 0; i < codec->num_nodes; i++, nid++)
668 codec->wcaps[i] = snd_hda_param_read(codec, nid,
669 AC_PAR_AUDIO_WIDGET_CAP);
674 static void init_hda_cache(struct hda_cache_rec *cache,
675 unsigned int record_size);
676 static void free_hda_cache(struct hda_cache_rec *cache);
681 static void snd_hda_codec_free(struct hda_codec *codec)
685 #ifdef CONFIG_SND_HDA_POWER_SAVE
686 cancel_delayed_work(&codec->power_work);
687 flush_scheduled_work();
689 list_del(&codec->list);
690 snd_array_free(&codec->mixers);
691 codec->bus->caddr_tbl[codec->addr] = NULL;
692 if (codec->patch_ops.free)
693 codec->patch_ops.free(codec);
694 module_put(codec->owner);
695 free_hda_cache(&codec->amp_cache);
696 free_hda_cache(&codec->cmd_cache);
698 kfree(codec->modelname);
704 * snd_hda_codec_new - create a HDA codec
705 * @bus: the bus to assign
706 * @codec_addr: the codec address
707 * @codecp: the pointer to store the generated codec
709 * Returns 0 if successful, or a negative error code.
711 int /*__devinit*/ snd_hda_codec_new(struct hda_bus *bus, unsigned int codec_addr,
712 int do_init, struct hda_codec **codecp)
714 struct hda_codec *codec;
718 if (snd_BUG_ON(!bus))
720 if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS))
723 if (bus->caddr_tbl[codec_addr]) {
724 snd_printk(KERN_ERR "hda_codec: "
725 "address 0x%x is already occupied\n", codec_addr);
729 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
731 snd_printk(KERN_ERR "can't allocate struct hda_codec\n");
736 codec->addr = codec_addr;
737 mutex_init(&codec->spdif_mutex);
738 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
739 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
740 snd_array_init(&codec->mixers, sizeof(struct snd_kcontrol *), 32);
741 if (codec->bus->modelname) {
742 codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL);
743 if (!codec->modelname) {
744 snd_hda_codec_free(codec);
749 #ifdef CONFIG_SND_HDA_POWER_SAVE
750 INIT_DELAYED_WORK(&codec->power_work, hda_power_work);
751 /* snd_hda_codec_new() marks the codec as power-up, and leave it as is.
752 * the caller has to power down appropriatley after initialization
755 hda_keep_power_on(codec);
758 list_add_tail(&codec->list, &bus->codec_list);
759 bus->caddr_tbl[codec_addr] = codec;
761 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
763 if (codec->vendor_id == -1)
764 /* read again, hopefully the access method was corrected
765 * in the last read...
767 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
769 codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT,
770 AC_PAR_SUBSYSTEM_ID);
771 codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT,
774 setup_fg_nodes(codec);
775 if (!codec->afg && !codec->mfg) {
776 snd_printdd("hda_codec: no AFG or MFG node found\n");
777 snd_hda_codec_free(codec);
781 if (read_widget_caps(codec, codec->afg ? codec->afg : codec->mfg) < 0) {
782 snd_printk(KERN_ERR "hda_codec: cannot malloc\n");
783 snd_hda_codec_free(codec);
787 if (!codec->subsystem_id) {
788 hda_nid_t nid = codec->afg ? codec->afg : codec->mfg;
789 codec->subsystem_id =
790 snd_hda_codec_read(codec, nid, 0,
791 AC_VERB_GET_SUBSYSTEM_ID, 0);
794 codec->modelname = kstrdup(bus->modelname, GFP_KERNEL);
797 err = snd_hda_codec_configure(codec);
799 snd_hda_codec_free(codec);
803 snd_hda_codec_proc_new(codec);
805 snd_hda_create_hwdep(codec);
807 sprintf(component, "HDA:%08x,%08x,%08x", codec->vendor_id,
808 codec->subsystem_id, codec->revision_id);
809 snd_component_add(codec->bus->card, component);
815 EXPORT_SYMBOL_HDA(snd_hda_codec_new);
817 int snd_hda_codec_configure(struct hda_codec *codec)
821 codec->preset = find_codec_preset(codec);
823 err = get_codec_name(codec);
827 /* audio codec should override the mixer name */
828 if (codec->afg || !*codec->bus->card->mixername)
829 strlcpy(codec->bus->card->mixername, codec->name,
830 sizeof(codec->bus->card->mixername));
832 if (is_generic_config(codec)) {
833 err = snd_hda_parse_generic_codec(codec);
836 if (codec->preset && codec->preset->patch) {
837 err = codec->preset->patch(codec);
841 /* call the default parser */
842 err = snd_hda_parse_generic_codec(codec);
844 printk(KERN_ERR "hda-codec: No codec parser is available\n");
847 if (!err && codec->patch_ops.unsol_event)
848 err = init_unsol_queue(codec->bus);
853 * snd_hda_codec_setup_stream - set up the codec for streaming
854 * @codec: the CODEC to set up
855 * @nid: the NID to set up
856 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
857 * @channel_id: channel id to pass, zero based.
858 * @format: stream format.
860 void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
862 int channel_id, int format)
867 snd_printdd("hda_codec_setup_stream: "
868 "NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
869 nid, stream_tag, channel_id, format);
870 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID,
871 (stream_tag << 4) | channel_id);
873 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, format);
875 EXPORT_SYMBOL_HDA(snd_hda_codec_setup_stream);
877 void snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid)
882 snd_printdd("hda_codec_cleanup_stream: NID=0x%x\n", nid);
883 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
884 #if 0 /* keep the format */
886 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0);
889 EXPORT_SYMBOL_HDA(snd_hda_codec_cleanup_stream);
892 * amp access functions
895 /* FIXME: more better hash key? */
896 #define HDA_HASH_KEY(nid,dir,idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
897 #define INFO_AMP_CAPS (1<<0)
898 #define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
900 /* initialize the hash table */
901 static void /*__devinit*/ init_hda_cache(struct hda_cache_rec *cache,
902 unsigned int record_size)
904 memset(cache, 0, sizeof(*cache));
905 memset(cache->hash, 0xff, sizeof(cache->hash));
906 snd_array_init(&cache->buf, record_size, 64);
909 static void free_hda_cache(struct hda_cache_rec *cache)
911 snd_array_free(&cache->buf);
914 /* query the hash. allocate an entry if not found. */
915 static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache,
918 u16 idx = key % (u16)ARRAY_SIZE(cache->hash);
919 u16 cur = cache->hash[idx];
920 struct hda_cache_head *info;
922 while (cur != 0xffff) {
923 info = snd_array_elem(&cache->buf, cur);
924 if (info->key == key)
929 /* add a new hash entry */
930 info = snd_array_new(&cache->buf);
933 cur = snd_array_index(&cache->buf, info);
936 info->next = cache->hash[idx];
937 cache->hash[idx] = cur;
942 /* query and allocate an amp hash entry */
943 static inline struct hda_amp_info *
944 get_alloc_amp_hash(struct hda_codec *codec, u32 key)
946 return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key);
950 * query AMP capabilities for the given widget and direction
952 u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
954 struct hda_amp_info *info;
956 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, 0));
959 if (!(info->head.val & INFO_AMP_CAPS)) {
960 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
962 info->amp_caps = snd_hda_param_read(codec, nid,
963 direction == HDA_OUTPUT ?
967 info->head.val |= INFO_AMP_CAPS;
969 return info->amp_caps;
971 EXPORT_SYMBOL_HDA(query_amp_caps);
973 int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
976 struct hda_amp_info *info;
978 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, dir, 0));
981 info->amp_caps = caps;
982 info->head.val |= INFO_AMP_CAPS;
985 EXPORT_SYMBOL_HDA(snd_hda_override_amp_caps);
988 * read the current volume to info
989 * if the cache exists, read the cache value.
991 static unsigned int get_vol_mute(struct hda_codec *codec,
992 struct hda_amp_info *info, hda_nid_t nid,
993 int ch, int direction, int index)
997 if (info->head.val & INFO_AMP_VOL(ch))
998 return info->vol[ch];
1000 parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
1001 parm |= direction == HDA_OUTPUT ? AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
1003 val = snd_hda_codec_read(codec, nid, 0,
1004 AC_VERB_GET_AMP_GAIN_MUTE, parm);
1005 info->vol[ch] = val & 0xff;
1006 info->head.val |= INFO_AMP_VOL(ch);
1007 return info->vol[ch];
1011 * write the current volume in info to the h/w and update the cache
1013 static void put_vol_mute(struct hda_codec *codec, struct hda_amp_info *info,
1014 hda_nid_t nid, int ch, int direction, int index,
1019 parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
1020 parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
1021 parm |= index << AC_AMP_SET_INDEX_SHIFT;
1023 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
1024 info->vol[ch] = val;
1028 * read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
1030 int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
1031 int direction, int index)
1033 struct hda_amp_info *info;
1034 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
1037 return get_vol_mute(codec, info, nid, ch, direction, index);
1039 EXPORT_SYMBOL_HDA(snd_hda_codec_amp_read);
1042 * update the AMP value, mask = bit mask to set, val = the value
1044 int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
1045 int direction, int idx, int mask, int val)
1047 struct hda_amp_info *info;
1049 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, idx));
1053 val |= get_vol_mute(codec, info, nid, ch, direction, idx) & ~mask;
1054 if (info->vol[ch] == val)
1056 put_vol_mute(codec, info, nid, ch, direction, idx, val);
1059 EXPORT_SYMBOL_HDA(snd_hda_codec_amp_update);
1062 * update the AMP stereo with the same mask and value
1064 int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
1065 int direction, int idx, int mask, int val)
1068 for (ch = 0; ch < 2; ch++)
1069 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
1073 EXPORT_SYMBOL_HDA(snd_hda_codec_amp_stereo);
1075 #ifdef SND_HDA_NEEDS_RESUME
1076 /* resume the all amp commands from the cache */
1077 void snd_hda_codec_resume_amp(struct hda_codec *codec)
1079 struct hda_amp_info *buffer = codec->amp_cache.buf.list;
1082 for (i = 0; i < codec->amp_cache.buf.used; i++, buffer++) {
1083 u32 key = buffer->head.key;
1085 unsigned int idx, dir, ch;
1089 idx = (key >> 16) & 0xff;
1090 dir = (key >> 24) & 0xff;
1091 for (ch = 0; ch < 2; ch++) {
1092 if (!(buffer->head.val & INFO_AMP_VOL(ch)))
1094 put_vol_mute(codec, buffer, nid, ch, dir, idx,
1099 EXPORT_SYMBOL_HDA(snd_hda_codec_resume_amp);
1100 #endif /* SND_HDA_NEEDS_RESUME */
1103 int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
1104 struct snd_ctl_elem_info *uinfo)
1106 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1107 u16 nid = get_amp_nid(kcontrol);
1108 u8 chs = get_amp_channels(kcontrol);
1109 int dir = get_amp_direction(kcontrol);
1112 caps = query_amp_caps(codec, nid, dir);
1114 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1116 printk(KERN_WARNING "hda_codec: "
1117 "num_steps = 0 for NID=0x%x (ctl = %s)\n", nid,
1121 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1122 uinfo->count = chs == 3 ? 2 : 1;
1123 uinfo->value.integer.min = 0;
1124 uinfo->value.integer.max = caps;
1127 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_info);
1129 int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
1130 struct snd_ctl_elem_value *ucontrol)
1132 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1133 hda_nid_t nid = get_amp_nid(kcontrol);
1134 int chs = get_amp_channels(kcontrol);
1135 int dir = get_amp_direction(kcontrol);
1136 int idx = get_amp_index(kcontrol);
1137 long *valp = ucontrol->value.integer.value;
1140 *valp++ = snd_hda_codec_amp_read(codec, nid, 0, dir, idx)
1143 *valp = snd_hda_codec_amp_read(codec, nid, 1, dir, idx)
1147 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_get);
1149 int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
1150 struct snd_ctl_elem_value *ucontrol)
1152 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1153 hda_nid_t nid = get_amp_nid(kcontrol);
1154 int chs = get_amp_channels(kcontrol);
1155 int dir = get_amp_direction(kcontrol);
1156 int idx = get_amp_index(kcontrol);
1157 long *valp = ucontrol->value.integer.value;
1160 snd_hda_power_up(codec);
1162 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
1167 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
1169 snd_hda_power_down(codec);
1172 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_put);
1174 int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
1175 unsigned int size, unsigned int __user *_tlv)
1177 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1178 hda_nid_t nid = get_amp_nid(kcontrol);
1179 int dir = get_amp_direction(kcontrol);
1180 u32 caps, val1, val2;
1182 if (size < 4 * sizeof(unsigned int))
1184 caps = query_amp_caps(codec, nid, dir);
1185 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
1186 val2 = (val2 + 1) * 25;
1187 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
1188 val1 = ((int)val1) * ((int)val2);
1189 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
1191 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
1193 if (put_user(val1, _tlv + 2))
1195 if (put_user(val2, _tlv + 3))
1199 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_tlv);
1202 * set (static) TLV for virtual master volume; recalculated as max 0dB
1204 void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
1210 caps = query_amp_caps(codec, nid, dir);
1211 nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1212 step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
1213 step = (step + 1) * 25;
1214 tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
1215 tlv[1] = 2 * sizeof(unsigned int);
1216 tlv[2] = -nums * step;
1219 EXPORT_SYMBOL_HDA(snd_hda_set_vmaster_tlv);
1221 /* find a mixer control element with the given name */
1222 static struct snd_kcontrol *
1223 _snd_hda_find_mixer_ctl(struct hda_codec *codec,
1224 const char *name, int idx)
1226 struct snd_ctl_elem_id id;
1227 memset(&id, 0, sizeof(id));
1228 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1230 strcpy(id.name, name);
1231 return snd_ctl_find_id(codec->bus->card, &id);
1234 struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
1237 return _snd_hda_find_mixer_ctl(codec, name, 0);
1239 EXPORT_SYMBOL_HDA(snd_hda_find_mixer_ctl);
1241 /* Add a control element and assign to the codec */
1242 int snd_hda_ctl_add(struct hda_codec *codec, struct snd_kcontrol *kctl)
1245 struct snd_kcontrol **knewp;
1247 err = snd_ctl_add(codec->bus->card, kctl);
1250 knewp = snd_array_new(&codec->mixers);
1256 EXPORT_SYMBOL_HDA(snd_hda_ctl_add);
1258 #ifdef CONFIG_SND_HDA_RECONFIG
1259 /* Clear all controls assigned to the given codec */
1260 void snd_hda_ctls_clear(struct hda_codec *codec)
1263 struct snd_kcontrol **kctls = codec->mixers.list;
1264 for (i = 0; i < codec->mixers.used; i++)
1265 snd_ctl_remove(codec->bus->card, kctls[i]);
1266 snd_array_free(&codec->mixers);
1269 void snd_hda_codec_reset(struct hda_codec *codec)
1273 #ifdef CONFIG_SND_HDA_POWER_SAVE
1274 cancel_delayed_work(&codec->power_work);
1275 flush_scheduled_work();
1277 snd_hda_ctls_clear(codec);
1279 for (i = 0; i < codec->num_pcms; i++) {
1280 if (codec->pcm_info[i].pcm) {
1281 snd_device_free(codec->bus->card,
1282 codec->pcm_info[i].pcm);
1283 clear_bit(codec->pcm_info[i].device,
1284 codec->bus->pcm_dev_bits);
1287 if (codec->patch_ops.free)
1288 codec->patch_ops.free(codec);
1289 codec->proc_widget_hook = NULL;
1291 free_hda_cache(&codec->amp_cache);
1292 free_hda_cache(&codec->cmd_cache);
1293 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
1294 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
1295 codec->num_pcms = 0;
1296 codec->pcm_info = NULL;
1297 codec->preset = NULL;
1298 module_put(codec->owner);
1299 codec->owner = NULL;
1301 #endif /* CONFIG_SND_HDA_RECONFIG */
1303 /* create a virtual master control and add slaves */
1304 int snd_hda_add_vmaster(struct hda_codec *codec, char *name,
1305 unsigned int *tlv, const char **slaves)
1307 struct snd_kcontrol *kctl;
1311 for (s = slaves; *s && !snd_hda_find_mixer_ctl(codec, *s); s++)
1314 snd_printdd("No slave found for %s\n", name);
1317 kctl = snd_ctl_make_virtual_master(name, tlv);
1320 err = snd_hda_ctl_add(codec, kctl);
1324 for (s = slaves; *s; s++) {
1325 struct snd_kcontrol *sctl;
1327 sctl = snd_hda_find_mixer_ctl(codec, *s);
1329 snd_printdd("Cannot find slave %s, skipped\n", *s);
1332 err = snd_ctl_add_slave(kctl, sctl);
1338 EXPORT_SYMBOL_HDA(snd_hda_add_vmaster);
1341 int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
1342 struct snd_ctl_elem_info *uinfo)
1344 int chs = get_amp_channels(kcontrol);
1346 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1347 uinfo->count = chs == 3 ? 2 : 1;
1348 uinfo->value.integer.min = 0;
1349 uinfo->value.integer.max = 1;
1352 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_info);
1354 int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
1355 struct snd_ctl_elem_value *ucontrol)
1357 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1358 hda_nid_t nid = get_amp_nid(kcontrol);
1359 int chs = get_amp_channels(kcontrol);
1360 int dir = get_amp_direction(kcontrol);
1361 int idx = get_amp_index(kcontrol);
1362 long *valp = ucontrol->value.integer.value;
1365 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
1366 HDA_AMP_MUTE) ? 0 : 1;
1368 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
1369 HDA_AMP_MUTE) ? 0 : 1;
1372 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_get);
1374 int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
1375 struct snd_ctl_elem_value *ucontrol)
1377 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1378 hda_nid_t nid = get_amp_nid(kcontrol);
1379 int chs = get_amp_channels(kcontrol);
1380 int dir = get_amp_direction(kcontrol);
1381 int idx = get_amp_index(kcontrol);
1382 long *valp = ucontrol->value.integer.value;
1385 snd_hda_power_up(codec);
1387 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
1389 *valp ? 0 : HDA_AMP_MUTE);
1393 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
1395 *valp ? 0 : HDA_AMP_MUTE);
1396 #ifdef CONFIG_SND_HDA_POWER_SAVE
1397 if (codec->patch_ops.check_power_status)
1398 codec->patch_ops.check_power_status(codec, nid);
1400 snd_hda_power_down(codec);
1403 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put);
1406 * bound volume controls
1408 * bind multiple volumes (# indices, from 0)
1411 #define AMP_VAL_IDX_SHIFT 19
1412 #define AMP_VAL_IDX_MASK (0x0f<<19)
1414 int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
1415 struct snd_ctl_elem_value *ucontrol)
1417 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1421 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
1422 pval = kcontrol->private_value;
1423 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
1424 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
1425 kcontrol->private_value = pval;
1426 mutex_unlock(&codec->spdif_mutex);
1429 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_get);
1431 int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
1432 struct snd_ctl_elem_value *ucontrol)
1434 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1436 int i, indices, err = 0, change = 0;
1438 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
1439 pval = kcontrol->private_value;
1440 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
1441 for (i = 0; i < indices; i++) {
1442 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
1443 (i << AMP_VAL_IDX_SHIFT);
1444 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
1449 kcontrol->private_value = pval;
1450 mutex_unlock(&codec->spdif_mutex);
1451 return err < 0 ? err : change;
1453 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_put);
1456 * generic bound volume/swtich controls
1458 int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
1459 struct snd_ctl_elem_info *uinfo)
1461 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1462 struct hda_bind_ctls *c;
1465 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
1466 c = (struct hda_bind_ctls *)kcontrol->private_value;
1467 kcontrol->private_value = *c->values;
1468 err = c->ops->info(kcontrol, uinfo);
1469 kcontrol->private_value = (long)c;
1470 mutex_unlock(&codec->spdif_mutex);
1473 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_info);
1475 int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
1476 struct snd_ctl_elem_value *ucontrol)
1478 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1479 struct hda_bind_ctls *c;
1482 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
1483 c = (struct hda_bind_ctls *)kcontrol->private_value;
1484 kcontrol->private_value = *c->values;
1485 err = c->ops->get(kcontrol, ucontrol);
1486 kcontrol->private_value = (long)c;
1487 mutex_unlock(&codec->spdif_mutex);
1490 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_get);
1492 int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
1493 struct snd_ctl_elem_value *ucontrol)
1495 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1496 struct hda_bind_ctls *c;
1497 unsigned long *vals;
1498 int err = 0, change = 0;
1500 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
1501 c = (struct hda_bind_ctls *)kcontrol->private_value;
1502 for (vals = c->values; *vals; vals++) {
1503 kcontrol->private_value = *vals;
1504 err = c->ops->put(kcontrol, ucontrol);
1509 kcontrol->private_value = (long)c;
1510 mutex_unlock(&codec->spdif_mutex);
1511 return err < 0 ? err : change;
1513 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_put);
1515 int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
1516 unsigned int size, unsigned int __user *tlv)
1518 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1519 struct hda_bind_ctls *c;
1522 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
1523 c = (struct hda_bind_ctls *)kcontrol->private_value;
1524 kcontrol->private_value = *c->values;
1525 err = c->ops->tlv(kcontrol, op_flag, size, tlv);
1526 kcontrol->private_value = (long)c;
1527 mutex_unlock(&codec->spdif_mutex);
1530 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_tlv);
1532 struct hda_ctl_ops snd_hda_bind_vol = {
1533 .info = snd_hda_mixer_amp_volume_info,
1534 .get = snd_hda_mixer_amp_volume_get,
1535 .put = snd_hda_mixer_amp_volume_put,
1536 .tlv = snd_hda_mixer_amp_tlv
1538 EXPORT_SYMBOL_HDA(snd_hda_bind_vol);
1540 struct hda_ctl_ops snd_hda_bind_sw = {
1541 .info = snd_hda_mixer_amp_switch_info,
1542 .get = snd_hda_mixer_amp_switch_get,
1543 .put = snd_hda_mixer_amp_switch_put,
1544 .tlv = snd_hda_mixer_amp_tlv
1546 EXPORT_SYMBOL_HDA(snd_hda_bind_sw);
1549 * SPDIF out controls
1552 static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
1553 struct snd_ctl_elem_info *uinfo)
1555 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1560 static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
1561 struct snd_ctl_elem_value *ucontrol)
1563 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
1564 IEC958_AES0_NONAUDIO |
1565 IEC958_AES0_CON_EMPHASIS_5015 |
1566 IEC958_AES0_CON_NOT_COPYRIGHT;
1567 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
1568 IEC958_AES1_CON_ORIGINAL;
1572 static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
1573 struct snd_ctl_elem_value *ucontrol)
1575 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
1576 IEC958_AES0_NONAUDIO |
1577 IEC958_AES0_PRO_EMPHASIS_5015;
1581 static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
1582 struct snd_ctl_elem_value *ucontrol)
1584 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1586 ucontrol->value.iec958.status[0] = codec->spdif_status & 0xff;
1587 ucontrol->value.iec958.status[1] = (codec->spdif_status >> 8) & 0xff;
1588 ucontrol->value.iec958.status[2] = (codec->spdif_status >> 16) & 0xff;
1589 ucontrol->value.iec958.status[3] = (codec->spdif_status >> 24) & 0xff;
1594 /* convert from SPDIF status bits to HDA SPDIF bits
1595 * bit 0 (DigEn) is always set zero (to be filled later)
1597 static unsigned short convert_from_spdif_status(unsigned int sbits)
1599 unsigned short val = 0;
1601 if (sbits & IEC958_AES0_PROFESSIONAL)
1602 val |= AC_DIG1_PROFESSIONAL;
1603 if (sbits & IEC958_AES0_NONAUDIO)
1604 val |= AC_DIG1_NONAUDIO;
1605 if (sbits & IEC958_AES0_PROFESSIONAL) {
1606 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
1607 IEC958_AES0_PRO_EMPHASIS_5015)
1608 val |= AC_DIG1_EMPHASIS;
1610 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
1611 IEC958_AES0_CON_EMPHASIS_5015)
1612 val |= AC_DIG1_EMPHASIS;
1613 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
1614 val |= AC_DIG1_COPYRIGHT;
1615 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
1616 val |= AC_DIG1_LEVEL;
1617 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
1622 /* convert to SPDIF status bits from HDA SPDIF bits
1624 static unsigned int convert_to_spdif_status(unsigned short val)
1626 unsigned int sbits = 0;
1628 if (val & AC_DIG1_NONAUDIO)
1629 sbits |= IEC958_AES0_NONAUDIO;
1630 if (val & AC_DIG1_PROFESSIONAL)
1631 sbits |= IEC958_AES0_PROFESSIONAL;
1632 if (sbits & IEC958_AES0_PROFESSIONAL) {
1633 if (sbits & AC_DIG1_EMPHASIS)
1634 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
1636 if (val & AC_DIG1_EMPHASIS)
1637 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
1638 if (!(val & AC_DIG1_COPYRIGHT))
1639 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
1640 if (val & AC_DIG1_LEVEL)
1641 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
1642 sbits |= val & (0x7f << 8);
1647 /* set digital convert verbs both for the given NID and its slaves */
1648 static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
1653 snd_hda_codec_write_cache(codec, nid, 0, verb, val);
1654 d = codec->slave_dig_outs;
1658 snd_hda_codec_write_cache(codec, *d, 0, verb, val);
1661 static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
1665 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1);
1667 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2);
1670 static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
1671 struct snd_ctl_elem_value *ucontrol)
1673 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1674 hda_nid_t nid = kcontrol->private_value;
1678 mutex_lock(&codec->spdif_mutex);
1679 codec->spdif_status = ucontrol->value.iec958.status[0] |
1680 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
1681 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
1682 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
1683 val = convert_from_spdif_status(codec->spdif_status);
1684 val |= codec->spdif_ctls & 1;
1685 change = codec->spdif_ctls != val;
1686 codec->spdif_ctls = val;
1689 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
1691 mutex_unlock(&codec->spdif_mutex);
1695 #define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
1697 static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
1698 struct snd_ctl_elem_value *ucontrol)
1700 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1702 ucontrol->value.integer.value[0] = codec->spdif_ctls & AC_DIG1_ENABLE;
1706 static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
1707 struct snd_ctl_elem_value *ucontrol)
1709 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1710 hda_nid_t nid = kcontrol->private_value;
1714 mutex_lock(&codec->spdif_mutex);
1715 val = codec->spdif_ctls & ~AC_DIG1_ENABLE;
1716 if (ucontrol->value.integer.value[0])
1717 val |= AC_DIG1_ENABLE;
1718 change = codec->spdif_ctls != val;
1720 codec->spdif_ctls = val;
1721 set_dig_out_convert(codec, nid, val & 0xff, -1);
1722 /* unmute amp switch (if any) */
1723 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
1724 (val & AC_DIG1_ENABLE))
1725 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
1728 mutex_unlock(&codec->spdif_mutex);
1732 static struct snd_kcontrol_new dig_mixes[] = {
1734 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1735 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1736 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,CON_MASK),
1737 .info = snd_hda_spdif_mask_info,
1738 .get = snd_hda_spdif_cmask_get,
1741 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1742 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1743 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,PRO_MASK),
1744 .info = snd_hda_spdif_mask_info,
1745 .get = snd_hda_spdif_pmask_get,
1748 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1749 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
1750 .info = snd_hda_spdif_mask_info,
1751 .get = snd_hda_spdif_default_get,
1752 .put = snd_hda_spdif_default_put,
1755 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1756 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,SWITCH),
1757 .info = snd_hda_spdif_out_switch_info,
1758 .get = snd_hda_spdif_out_switch_get,
1759 .put = snd_hda_spdif_out_switch_put,
1764 #define SPDIF_MAX_IDX 4 /* 4 instances should be enough to probe */
1767 * snd_hda_create_spdif_out_ctls - create Output SPDIF-related controls
1768 * @codec: the HDA codec
1769 * @nid: audio out widget NID
1771 * Creates controls related with the SPDIF output.
1772 * Called from each patch supporting the SPDIF out.
1774 * Returns 0 if successful, or a negative error code.
1776 int snd_hda_create_spdif_out_ctls(struct hda_codec *codec, hda_nid_t nid)
1779 struct snd_kcontrol *kctl;
1780 struct snd_kcontrol_new *dig_mix;
1783 for (idx = 0; idx < SPDIF_MAX_IDX; idx++) {
1784 if (!_snd_hda_find_mixer_ctl(codec, "IEC958 Playback Switch",
1788 if (idx >= SPDIF_MAX_IDX) {
1789 printk(KERN_ERR "hda_codec: too many IEC958 outputs\n");
1792 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
1793 kctl = snd_ctl_new1(dig_mix, codec);
1796 kctl->id.index = idx;
1797 kctl->private_value = nid;
1798 err = snd_hda_ctl_add(codec, kctl);
1803 snd_hda_codec_read(codec, nid, 0,
1804 AC_VERB_GET_DIGI_CONVERT_1, 0);
1805 codec->spdif_status = convert_to_spdif_status(codec->spdif_ctls);
1808 EXPORT_SYMBOL_HDA(snd_hda_create_spdif_out_ctls);
1811 * SPDIF sharing with analog output
1813 static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
1814 struct snd_ctl_elem_value *ucontrol)
1816 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
1817 ucontrol->value.integer.value[0] = mout->share_spdif;
1821 static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
1822 struct snd_ctl_elem_value *ucontrol)
1824 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
1825 mout->share_spdif = !!ucontrol->value.integer.value[0];
1829 static struct snd_kcontrol_new spdif_share_sw = {
1830 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1831 .name = "IEC958 Default PCM Playback Switch",
1832 .info = snd_ctl_boolean_mono_info,
1833 .get = spdif_share_sw_get,
1834 .put = spdif_share_sw_put,
1837 int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
1838 struct hda_multi_out *mout)
1840 if (!mout->dig_out_nid)
1842 /* ATTENTION: here mout is passed as private_data, instead of codec */
1843 return snd_hda_ctl_add(codec,
1844 snd_ctl_new1(&spdif_share_sw, mout));
1846 EXPORT_SYMBOL_HDA(snd_hda_create_spdif_share_sw);
1852 #define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
1854 static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
1855 struct snd_ctl_elem_value *ucontrol)
1857 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1859 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
1863 static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
1864 struct snd_ctl_elem_value *ucontrol)
1866 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1867 hda_nid_t nid = kcontrol->private_value;
1868 unsigned int val = !!ucontrol->value.integer.value[0];
1871 mutex_lock(&codec->spdif_mutex);
1872 change = codec->spdif_in_enable != val;
1874 codec->spdif_in_enable = val;
1875 snd_hda_codec_write_cache(codec, nid, 0,
1876 AC_VERB_SET_DIGI_CONVERT_1, val);
1878 mutex_unlock(&codec->spdif_mutex);
1882 static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
1883 struct snd_ctl_elem_value *ucontrol)
1885 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1886 hda_nid_t nid = kcontrol->private_value;
1890 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0);
1891 sbits = convert_to_spdif_status(val);
1892 ucontrol->value.iec958.status[0] = sbits;
1893 ucontrol->value.iec958.status[1] = sbits >> 8;
1894 ucontrol->value.iec958.status[2] = sbits >> 16;
1895 ucontrol->value.iec958.status[3] = sbits >> 24;
1899 static struct snd_kcontrol_new dig_in_ctls[] = {
1901 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1902 .name = SNDRV_CTL_NAME_IEC958("",CAPTURE,SWITCH),
1903 .info = snd_hda_spdif_in_switch_info,
1904 .get = snd_hda_spdif_in_switch_get,
1905 .put = snd_hda_spdif_in_switch_put,
1908 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1909 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1910 .name = SNDRV_CTL_NAME_IEC958("",CAPTURE,DEFAULT),
1911 .info = snd_hda_spdif_mask_info,
1912 .get = snd_hda_spdif_in_status_get,
1918 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
1919 * @codec: the HDA codec
1920 * @nid: audio in widget NID
1922 * Creates controls related with the SPDIF input.
1923 * Called from each patch supporting the SPDIF in.
1925 * Returns 0 if successful, or a negative error code.
1927 int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
1930 struct snd_kcontrol *kctl;
1931 struct snd_kcontrol_new *dig_mix;
1934 for (idx = 0; idx < SPDIF_MAX_IDX; idx++) {
1935 if (!_snd_hda_find_mixer_ctl(codec, "IEC958 Capture Switch",
1939 if (idx >= SPDIF_MAX_IDX) {
1940 printk(KERN_ERR "hda_codec: too many IEC958 inputs\n");
1943 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
1944 kctl = snd_ctl_new1(dig_mix, codec);
1945 kctl->private_value = nid;
1946 err = snd_hda_ctl_add(codec, kctl);
1950 codec->spdif_in_enable =
1951 snd_hda_codec_read(codec, nid, 0,
1952 AC_VERB_GET_DIGI_CONVERT_1, 0) &
1956 EXPORT_SYMBOL_HDA(snd_hda_create_spdif_in_ctls);
1958 #ifdef SND_HDA_NEEDS_RESUME
1963 /* build a 32bit cache key with the widget id and the command parameter */
1964 #define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
1965 #define get_cmd_cache_nid(key) ((key) & 0xff)
1966 #define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
1969 * snd_hda_codec_write_cache - send a single command with caching
1970 * @codec: the HDA codec
1971 * @nid: NID to send the command
1972 * @direct: direct flag
1973 * @verb: the verb to send
1974 * @parm: the parameter for the verb
1976 * Send a single command without waiting for response.
1978 * Returns 0 if successful, or a negative error code.
1980 int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
1981 int direct, unsigned int verb, unsigned int parm)
1983 struct hda_bus *bus = codec->bus;
1987 res = make_codec_cmd(codec, nid, direct, verb, parm);
1988 snd_hda_power_up(codec);
1989 mutex_lock(&bus->cmd_mutex);
1990 err = bus->ops.command(bus, res);
1992 struct hda_cache_head *c;
1993 u32 key = build_cmd_cache_key(nid, verb);
1994 c = get_alloc_hash(&codec->cmd_cache, key);
1998 mutex_unlock(&bus->cmd_mutex);
1999 snd_hda_power_down(codec);
2002 EXPORT_SYMBOL_HDA(snd_hda_codec_write_cache);
2004 /* resume the all commands from the cache */
2005 void snd_hda_codec_resume_cache(struct hda_codec *codec)
2007 struct hda_cache_head *buffer = codec->cmd_cache.buf.list;
2010 for (i = 0; i < codec->cmd_cache.buf.used; i++, buffer++) {
2011 u32 key = buffer->key;
2014 snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
2015 get_cmd_cache_cmd(key), buffer->val);
2018 EXPORT_SYMBOL_HDA(snd_hda_codec_resume_cache);
2021 * snd_hda_sequence_write_cache - sequence writes with caching
2022 * @codec: the HDA codec
2023 * @seq: VERB array to send
2025 * Send the commands sequentially from the given array.
2026 * Thte commands are recorded on cache for power-save and resume.
2027 * The array must be terminated with NID=0.
2029 void snd_hda_sequence_write_cache(struct hda_codec *codec,
2030 const struct hda_verb *seq)
2032 for (; seq->nid; seq++)
2033 snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
2036 EXPORT_SYMBOL_HDA(snd_hda_sequence_write_cache);
2037 #endif /* SND_HDA_NEEDS_RESUME */
2040 * set power state of the codec
2042 static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
2043 unsigned int power_state)
2048 snd_hda_codec_write(codec, fg, 0, AC_VERB_SET_POWER_STATE,
2050 msleep(10); /* partial workaround for "azx_get_response timeout" */
2052 nid = codec->start_nid;
2053 for (i = 0; i < codec->num_nodes; i++, nid++) {
2054 unsigned int wcaps = get_wcaps(codec, nid);
2055 if (wcaps & AC_WCAP_POWER) {
2056 unsigned int wid_type = (wcaps & AC_WCAP_TYPE) >>
2058 if (wid_type == AC_WID_PIN) {
2059 unsigned int pincap;
2061 * don't power down the widget if it controls
2062 * eapd and EAPD_BTLENABLE is set.
2064 pincap = snd_hda_param_read(codec, nid,
2066 if (pincap & AC_PINCAP_EAPD) {
2067 int eapd = snd_hda_codec_read(codec,
2069 AC_VERB_GET_EAPD_BTLENABLE, 0);
2071 if (power_state == AC_PWRST_D3 && eapd)
2075 snd_hda_codec_write(codec, nid, 0,
2076 AC_VERB_SET_POWER_STATE,
2081 if (power_state == AC_PWRST_D0) {
2082 unsigned long end_time;
2085 /* wait until the codec reachs to D0 */
2086 end_time = jiffies + msecs_to_jiffies(500);
2088 state = snd_hda_codec_read(codec, fg, 0,
2089 AC_VERB_GET_POWER_STATE, 0);
2090 if (state == power_state)
2093 } while (time_after_eq(end_time, jiffies));
2097 #ifdef CONFIG_SND_HDA_HWDEP
2098 /* execute additional init verbs */
2099 static void hda_exec_init_verbs(struct hda_codec *codec)
2101 if (codec->init_verbs.list)
2102 snd_hda_sequence_write(codec, codec->init_verbs.list);
2105 static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
2108 #ifdef SND_HDA_NEEDS_RESUME
2110 * call suspend and power-down; used both from PM and power-save
2112 static void hda_call_codec_suspend(struct hda_codec *codec)
2114 if (codec->patch_ops.suspend)
2115 codec->patch_ops.suspend(codec, PMSG_SUSPEND);
2116 hda_set_power_state(codec,
2117 codec->afg ? codec->afg : codec->mfg,
2119 #ifdef CONFIG_SND_HDA_POWER_SAVE
2120 cancel_delayed_work(&codec->power_work);
2121 codec->power_on = 0;
2122 codec->power_transition = 0;
2127 * kick up codec; used both from PM and power-save
2129 static void hda_call_codec_resume(struct hda_codec *codec)
2131 hda_set_power_state(codec,
2132 codec->afg ? codec->afg : codec->mfg,
2134 hda_exec_init_verbs(codec);
2135 if (codec->patch_ops.resume)
2136 codec->patch_ops.resume(codec);
2138 if (codec->patch_ops.init)
2139 codec->patch_ops.init(codec);
2140 snd_hda_codec_resume_amp(codec);
2141 snd_hda_codec_resume_cache(codec);
2144 #endif /* SND_HDA_NEEDS_RESUME */
2148 * snd_hda_build_controls - build mixer controls
2151 * Creates mixer controls for each codec included in the bus.
2153 * Returns 0 if successful, otherwise a negative error code.
2155 int /*__devinit*/ snd_hda_build_controls(struct hda_bus *bus)
2157 struct hda_codec *codec;
2159 list_for_each_entry(codec, &bus->codec_list, list) {
2160 int err = snd_hda_codec_build_controls(codec);
2166 EXPORT_SYMBOL_HDA(snd_hda_build_controls);
2168 int snd_hda_codec_build_controls(struct hda_codec *codec)
2171 /* fake as if already powered-on */
2172 hda_keep_power_on(codec);
2174 hda_set_power_state(codec,
2175 codec->afg ? codec->afg : codec->mfg,
2177 hda_exec_init_verbs(codec);
2178 /* continue to initialize... */
2179 if (codec->patch_ops.init)
2180 err = codec->patch_ops.init(codec);
2181 if (!err && codec->patch_ops.build_controls)
2182 err = codec->patch_ops.build_controls(codec);
2183 snd_hda_power_down(codec);
2192 struct hda_rate_tbl {
2194 unsigned int alsa_bits;
2195 unsigned int hda_fmt;
2198 static struct hda_rate_tbl rate_bits[] = {
2199 /* rate in Hz, ALSA rate bitmask, HDA format value */
2201 /* autodetected value used in snd_hda_query_supported_pcm */
2202 { 8000, SNDRV_PCM_RATE_8000, 0x0500 }, /* 1/6 x 48 */
2203 { 11025, SNDRV_PCM_RATE_11025, 0x4300 }, /* 1/4 x 44 */
2204 { 16000, SNDRV_PCM_RATE_16000, 0x0200 }, /* 1/3 x 48 */
2205 { 22050, SNDRV_PCM_RATE_22050, 0x4100 }, /* 1/2 x 44 */
2206 { 32000, SNDRV_PCM_RATE_32000, 0x0a00 }, /* 2/3 x 48 */
2207 { 44100, SNDRV_PCM_RATE_44100, 0x4000 }, /* 44 */
2208 { 48000, SNDRV_PCM_RATE_48000, 0x0000 }, /* 48 */
2209 { 88200, SNDRV_PCM_RATE_88200, 0x4800 }, /* 2 x 44 */
2210 { 96000, SNDRV_PCM_RATE_96000, 0x0800 }, /* 2 x 48 */
2211 { 176400, SNDRV_PCM_RATE_176400, 0x5800 },/* 4 x 44 */
2212 { 192000, SNDRV_PCM_RATE_192000, 0x1800 }, /* 4 x 48 */
2213 #define AC_PAR_PCM_RATE_BITS 11
2214 /* up to bits 10, 384kHZ isn't supported properly */
2216 /* not autodetected value */
2217 { 9600, SNDRV_PCM_RATE_KNOT, 0x0400 }, /* 1/5 x 48 */
2219 { 0 } /* terminator */
2223 * snd_hda_calc_stream_format - calculate format bitset
2224 * @rate: the sample rate
2225 * @channels: the number of channels
2226 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
2227 * @maxbps: the max. bps
2229 * Calculate the format bitset from the given rate, channels and th PCM format.
2231 * Return zero if invalid.
2233 unsigned int snd_hda_calc_stream_format(unsigned int rate,
2234 unsigned int channels,
2235 unsigned int format,
2236 unsigned int maxbps)
2239 unsigned int val = 0;
2241 for (i = 0; rate_bits[i].hz; i++)
2242 if (rate_bits[i].hz == rate) {
2243 val = rate_bits[i].hda_fmt;
2246 if (!rate_bits[i].hz) {
2247 snd_printdd("invalid rate %d\n", rate);
2251 if (channels == 0 || channels > 8) {
2252 snd_printdd("invalid channels %d\n", channels);
2255 val |= channels - 1;
2257 switch (snd_pcm_format_width(format)) {
2258 case 8: val |= 0x00; break;
2259 case 16: val |= 0x10; break;
2265 else if (maxbps >= 24)
2271 snd_printdd("invalid format width %d\n",
2272 snd_pcm_format_width(format));
2278 EXPORT_SYMBOL_HDA(snd_hda_calc_stream_format);
2281 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
2282 * @codec: the HDA codec
2283 * @nid: NID to query
2284 * @ratesp: the pointer to store the detected rate bitflags
2285 * @formatsp: the pointer to store the detected formats
2286 * @bpsp: the pointer to store the detected format widths
2288 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
2289 * or @bsps argument is ignored.
2291 * Returns 0 if successful, otherwise a negative error code.
2293 static int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
2294 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
2297 unsigned int val, streams;
2300 if (nid != codec->afg &&
2301 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD)) {
2302 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
2307 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
2311 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
2313 rates |= rate_bits[i].alsa_bits;
2318 if (formatsp || bpsp) {
2323 wcaps = get_wcaps(codec, nid);
2324 streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
2328 streams = snd_hda_param_read(codec, codec->afg,
2335 if (streams & AC_SUPFMT_PCM) {
2336 if (val & AC_SUPPCM_BITS_8) {
2337 formats |= SNDRV_PCM_FMTBIT_U8;
2340 if (val & AC_SUPPCM_BITS_16) {
2341 formats |= SNDRV_PCM_FMTBIT_S16_LE;
2344 if (wcaps & AC_WCAP_DIGITAL) {
2345 if (val & AC_SUPPCM_BITS_32)
2346 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
2347 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
2348 formats |= SNDRV_PCM_FMTBIT_S32_LE;
2349 if (val & AC_SUPPCM_BITS_24)
2351 else if (val & AC_SUPPCM_BITS_20)
2353 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
2354 AC_SUPPCM_BITS_32)) {
2355 formats |= SNDRV_PCM_FMTBIT_S32_LE;
2356 if (val & AC_SUPPCM_BITS_32)
2358 else if (val & AC_SUPPCM_BITS_24)
2360 else if (val & AC_SUPPCM_BITS_20)
2364 else if (streams == AC_SUPFMT_FLOAT32) {
2365 /* should be exclusive */
2366 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
2368 } else if (streams == AC_SUPFMT_AC3) {
2369 /* should be exclusive */
2370 /* temporary hack: we have still no proper support
2371 * for the direct AC3 stream...
2373 formats |= SNDRV_PCM_FMTBIT_U8;
2377 *formatsp = formats;
2386 * snd_hda_is_supported_format - check whether the given node supports
2389 * Returns 1 if supported, 0 if not.
2391 int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
2392 unsigned int format)
2395 unsigned int val = 0, rate, stream;
2397 if (nid != codec->afg &&
2398 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD)) {
2399 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
2404 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
2409 rate = format & 0xff00;
2410 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
2411 if (rate_bits[i].hda_fmt == rate) {
2416 if (i >= AC_PAR_PCM_RATE_BITS)
2419 stream = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
2422 if (!stream && nid != codec->afg)
2423 stream = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
2424 if (!stream || stream == -1)
2427 if (stream & AC_SUPFMT_PCM) {
2428 switch (format & 0xf0) {
2430 if (!(val & AC_SUPPCM_BITS_8))
2434 if (!(val & AC_SUPPCM_BITS_16))
2438 if (!(val & AC_SUPPCM_BITS_20))
2442 if (!(val & AC_SUPPCM_BITS_24))
2446 if (!(val & AC_SUPPCM_BITS_32))
2453 /* FIXME: check for float32 and AC3? */
2458 EXPORT_SYMBOL_HDA(snd_hda_is_supported_format);
2463 static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
2464 struct hda_codec *codec,
2465 struct snd_pcm_substream *substream)
2470 static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
2471 struct hda_codec *codec,
2472 unsigned int stream_tag,
2473 unsigned int format,
2474 struct snd_pcm_substream *substream)
2476 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
2480 static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
2481 struct hda_codec *codec,
2482 struct snd_pcm_substream *substream)
2484 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
2488 static int set_pcm_default_values(struct hda_codec *codec,
2489 struct hda_pcm_stream *info)
2491 /* query support PCM information from the given NID */
2492 if (info->nid && (!info->rates || !info->formats)) {
2493 snd_hda_query_supported_pcm(codec, info->nid,
2494 info->rates ? NULL : &info->rates,
2495 info->formats ? NULL : &info->formats,
2496 info->maxbps ? NULL : &info->maxbps);
2498 if (info->ops.open == NULL)
2499 info->ops.open = hda_pcm_default_open_close;
2500 if (info->ops.close == NULL)
2501 info->ops.close = hda_pcm_default_open_close;
2502 if (info->ops.prepare == NULL) {
2503 if (snd_BUG_ON(!info->nid))
2505 info->ops.prepare = hda_pcm_default_prepare;
2507 if (info->ops.cleanup == NULL) {
2508 if (snd_BUG_ON(!info->nid))
2510 info->ops.cleanup = hda_pcm_default_cleanup;
2516 * get the empty PCM device number to assign
2518 static int get_empty_pcm_device(struct hda_bus *bus, int type)
2520 static const char *dev_name[HDA_PCM_NTYPES] = {
2521 "Audio", "SPDIF", "HDMI", "Modem"
2523 /* starting device index for each PCM type */
2524 static int dev_idx[HDA_PCM_NTYPES] = {
2525 [HDA_PCM_TYPE_AUDIO] = 0,
2526 [HDA_PCM_TYPE_SPDIF] = 1,
2527 [HDA_PCM_TYPE_HDMI] = 3,
2528 [HDA_PCM_TYPE_MODEM] = 6
2530 /* normal audio device indices; not linear to keep compatibility */
2531 static int audio_idx[4] = { 0, 2, 4, 5 };
2535 case HDA_PCM_TYPE_AUDIO:
2536 for (i = 0; i < ARRAY_SIZE(audio_idx); i++) {
2538 if (!test_bit(dev, bus->pcm_dev_bits))
2541 if (i >= ARRAY_SIZE(audio_idx)) {
2542 snd_printk(KERN_WARNING "Too many audio devices\n");
2546 case HDA_PCM_TYPE_SPDIF:
2547 case HDA_PCM_TYPE_HDMI:
2548 case HDA_PCM_TYPE_MODEM:
2549 dev = dev_idx[type];
2550 if (test_bit(dev, bus->pcm_dev_bits)) {
2551 snd_printk(KERN_WARNING "%s already defined\n",
2557 snd_printk(KERN_WARNING "Invalid PCM type %d\n", type);
2560 set_bit(dev, bus->pcm_dev_bits);
2565 * attach a new PCM stream
2567 static int snd_hda_attach_pcm(struct hda_codec *codec, struct hda_pcm *pcm)
2569 struct hda_bus *bus = codec->bus;
2570 struct hda_pcm_stream *info;
2573 if (snd_BUG_ON(!pcm->name))
2575 for (stream = 0; stream < 2; stream++) {
2576 info = &pcm->stream[stream];
2577 if (info->substreams) {
2578 err = set_pcm_default_values(codec, info);
2583 return bus->ops.attach_pcm(bus, codec, pcm);
2586 /* assign all PCMs of the given codec */
2587 int snd_hda_codec_build_pcms(struct hda_codec *codec)
2592 if (!codec->num_pcms) {
2593 if (!codec->patch_ops.build_pcms)
2595 err = codec->patch_ops.build_pcms(codec);
2599 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
2600 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
2603 if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
2604 return 0; /* no substreams assigned */
2607 dev = get_empty_pcm_device(codec->bus, cpcm->pcm_type);
2611 err = snd_hda_attach_pcm(codec, cpcm);
2620 * snd_hda_build_pcms - build PCM information
2623 * Create PCM information for each codec included in the bus.
2625 * The build_pcms codec patch is requested to set up codec->num_pcms and
2626 * codec->pcm_info properly. The array is referred by the top-level driver
2627 * to create its PCM instances.
2628 * The allocated codec->pcm_info should be released in codec->patch_ops.free
2631 * At least, substreams, channels_min and channels_max must be filled for
2632 * each stream. substreams = 0 indicates that the stream doesn't exist.
2633 * When rates and/or formats are zero, the supported values are queried
2634 * from the given nid. The nid is used also by the default ops.prepare
2635 * and ops.cleanup callbacks.
2637 * The driver needs to call ops.open in its open callback. Similarly,
2638 * ops.close is supposed to be called in the close callback.
2639 * ops.prepare should be called in the prepare or hw_params callback
2640 * with the proper parameters for set up.
2641 * ops.cleanup should be called in hw_free for clean up of streams.
2643 * This function returns 0 if successfull, or a negative error code.
2645 int __devinit snd_hda_build_pcms(struct hda_bus *bus)
2647 struct hda_codec *codec;
2649 list_for_each_entry(codec, &bus->codec_list, list) {
2650 int err = snd_hda_codec_build_pcms(codec);
2656 EXPORT_SYMBOL_HDA(snd_hda_build_pcms);
2659 * snd_hda_check_board_config - compare the current codec with the config table
2660 * @codec: the HDA codec
2661 * @num_configs: number of config enums
2662 * @models: array of model name strings
2663 * @tbl: configuration table, terminated by null entries
2665 * Compares the modelname or PCI subsystem id of the current codec with the
2666 * given configuration table. If a matching entry is found, returns its
2667 * config value (supposed to be 0 or positive).
2669 * If no entries are matching, the function returns a negative value.
2671 int snd_hda_check_board_config(struct hda_codec *codec,
2672 int num_configs, const char **models,
2673 const struct snd_pci_quirk *tbl)
2675 if (codec->modelname && models) {
2677 for (i = 0; i < num_configs; i++) {
2679 !strcmp(codec->modelname, models[i])) {
2680 snd_printd(KERN_INFO "hda_codec: model '%s' is "
2681 "selected\n", models[i]);
2687 if (!codec->bus->pci || !tbl)
2690 tbl = snd_pci_quirk_lookup(codec->bus->pci, tbl);
2693 if (tbl->value >= 0 && tbl->value < num_configs) {
2694 #ifdef CONFIG_SND_DEBUG_VERBOSE
2696 const char *model = NULL;
2698 model = models[tbl->value];
2700 sprintf(tmp, "#%d", tbl->value);
2703 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
2704 "for config %x:%x (%s)\n",
2705 model, tbl->subvendor, tbl->subdevice,
2706 (tbl->name ? tbl->name : "Unknown device"));
2712 EXPORT_SYMBOL_HDA(snd_hda_check_board_config);
2715 * snd_hda_add_new_ctls - create controls from the array
2716 * @codec: the HDA codec
2717 * @knew: the array of struct snd_kcontrol_new
2719 * This helper function creates and add new controls in the given array.
2720 * The array must be terminated with an empty entry as terminator.
2722 * Returns 0 if successful, or a negative error code.
2724 int snd_hda_add_new_ctls(struct hda_codec *codec, struct snd_kcontrol_new *knew)
2728 for (; knew->name; knew++) {
2729 struct snd_kcontrol *kctl;
2730 kctl = snd_ctl_new1(knew, codec);
2733 err = snd_hda_ctl_add(codec, kctl);
2737 kctl = snd_ctl_new1(knew, codec);
2740 kctl->id.device = codec->addr;
2741 err = snd_hda_ctl_add(codec, kctl);
2748 EXPORT_SYMBOL_HDA(snd_hda_add_new_ctls);
2750 #ifdef CONFIG_SND_HDA_POWER_SAVE
2751 static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
2752 unsigned int power_state);
2754 static void hda_power_work(struct work_struct *work)
2756 struct hda_codec *codec =
2757 container_of(work, struct hda_codec, power_work.work);
2758 struct hda_bus *bus = codec->bus;
2760 if (!codec->power_on || codec->power_count) {
2761 codec->power_transition = 0;
2765 hda_call_codec_suspend(codec);
2766 if (bus->ops.pm_notify)
2767 bus->ops.pm_notify(bus);
2770 static void hda_keep_power_on(struct hda_codec *codec)
2772 codec->power_count++;
2773 codec->power_on = 1;
2776 void snd_hda_power_up(struct hda_codec *codec)
2778 struct hda_bus *bus = codec->bus;
2780 codec->power_count++;
2781 if (codec->power_on || codec->power_transition)
2784 codec->power_on = 1;
2785 if (bus->ops.pm_notify)
2786 bus->ops.pm_notify(bus);
2787 hda_call_codec_resume(codec);
2788 cancel_delayed_work(&codec->power_work);
2789 codec->power_transition = 0;
2791 EXPORT_SYMBOL_HDA(snd_hda_power_up);
2793 #define power_save(codec) \
2794 ((codec)->bus->power_save ? *(codec)->bus->power_save : 0)
2796 #define power_save(codec) \
2797 ((codec)->bus->power_save ? *(codec)->bus->power_save : 0)
2799 void snd_hda_power_down(struct hda_codec *codec)
2801 --codec->power_count;
2802 if (!codec->power_on || codec->power_count || codec->power_transition)
2804 if (power_save(codec)) {
2805 codec->power_transition = 1; /* avoid reentrance */
2806 schedule_delayed_work(&codec->power_work,
2807 msecs_to_jiffies(power_save(codec) * 1000));
2810 EXPORT_SYMBOL_HDA(snd_hda_power_down);
2812 int snd_hda_check_amp_list_power(struct hda_codec *codec,
2813 struct hda_loopback_check *check,
2816 struct hda_amp_list *p;
2819 if (!check->amplist)
2821 for (p = check->amplist; p->nid; p++) {
2826 return 0; /* nothing changed */
2828 for (p = check->amplist; p->nid; p++) {
2829 for (ch = 0; ch < 2; ch++) {
2830 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
2832 if (!(v & HDA_AMP_MUTE) && v > 0) {
2833 if (!check->power_on) {
2834 check->power_on = 1;
2835 snd_hda_power_up(codec);
2841 if (check->power_on) {
2842 check->power_on = 0;
2843 snd_hda_power_down(codec);
2847 EXPORT_SYMBOL_HDA(snd_hda_check_amp_list_power);
2851 * Channel mode helper
2853 int snd_hda_ch_mode_info(struct hda_codec *codec,
2854 struct snd_ctl_elem_info *uinfo,
2855 const struct hda_channel_mode *chmode,
2858 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2860 uinfo->value.enumerated.items = num_chmodes;
2861 if (uinfo->value.enumerated.item >= num_chmodes)
2862 uinfo->value.enumerated.item = num_chmodes - 1;
2863 sprintf(uinfo->value.enumerated.name, "%dch",
2864 chmode[uinfo->value.enumerated.item].channels);
2867 EXPORT_SYMBOL_HDA(snd_hda_ch_mode_info);
2869 int snd_hda_ch_mode_get(struct hda_codec *codec,
2870 struct snd_ctl_elem_value *ucontrol,
2871 const struct hda_channel_mode *chmode,
2877 for (i = 0; i < num_chmodes; i++) {
2878 if (max_channels == chmode[i].channels) {
2879 ucontrol->value.enumerated.item[0] = i;
2885 EXPORT_SYMBOL_HDA(snd_hda_ch_mode_get);
2887 int snd_hda_ch_mode_put(struct hda_codec *codec,
2888 struct snd_ctl_elem_value *ucontrol,
2889 const struct hda_channel_mode *chmode,
2895 mode = ucontrol->value.enumerated.item[0];
2896 if (mode >= num_chmodes)
2898 if (*max_channelsp == chmode[mode].channels)
2900 /* change the current channel setting */
2901 *max_channelsp = chmode[mode].channels;
2902 if (chmode[mode].sequence)
2903 snd_hda_sequence_write_cache(codec, chmode[mode].sequence);
2906 EXPORT_SYMBOL_HDA(snd_hda_ch_mode_put);
2911 int snd_hda_input_mux_info(const struct hda_input_mux *imux,
2912 struct snd_ctl_elem_info *uinfo)
2916 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2918 uinfo->value.enumerated.items = imux->num_items;
2919 if (!imux->num_items)
2921 index = uinfo->value.enumerated.item;
2922 if (index >= imux->num_items)
2923 index = imux->num_items - 1;
2924 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
2927 EXPORT_SYMBOL_HDA(snd_hda_input_mux_info);
2929 int snd_hda_input_mux_put(struct hda_codec *codec,
2930 const struct hda_input_mux *imux,
2931 struct snd_ctl_elem_value *ucontrol,
2933 unsigned int *cur_val)
2937 if (!imux->num_items)
2939 idx = ucontrol->value.enumerated.item[0];
2940 if (idx >= imux->num_items)
2941 idx = imux->num_items - 1;
2942 if (*cur_val == idx)
2944 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
2945 imux->items[idx].index);
2949 EXPORT_SYMBOL_HDA(snd_hda_input_mux_put);
2953 * Multi-channel / digital-out PCM helper functions
2956 /* setup SPDIF output stream */
2957 static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
2958 unsigned int stream_tag, unsigned int format)
2960 /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */
2961 if (codec->spdif_status_reset && (codec->spdif_ctls & AC_DIG1_ENABLE))
2962 set_dig_out_convert(codec, nid,
2963 codec->spdif_ctls & ~AC_DIG1_ENABLE & 0xff,
2965 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
2966 if (codec->slave_dig_outs) {
2968 for (d = codec->slave_dig_outs; *d; d++)
2969 snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
2972 /* turn on again (if needed) */
2973 if (codec->spdif_status_reset && (codec->spdif_ctls & AC_DIG1_ENABLE))
2974 set_dig_out_convert(codec, nid,
2975 codec->spdif_ctls & 0xff, -1);
2978 static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
2980 snd_hda_codec_cleanup_stream(codec, nid);
2981 if (codec->slave_dig_outs) {
2983 for (d = codec->slave_dig_outs; *d; d++)
2984 snd_hda_codec_cleanup_stream(codec, *d);
2989 * open the digital out in the exclusive mode
2991 int snd_hda_multi_out_dig_open(struct hda_codec *codec,
2992 struct hda_multi_out *mout)
2994 mutex_lock(&codec->spdif_mutex);
2995 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
2996 /* already opened as analog dup; reset it once */
2997 cleanup_dig_out_stream(codec, mout->dig_out_nid);
2998 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
2999 mutex_unlock(&codec->spdif_mutex);
3002 EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_open);
3004 int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
3005 struct hda_multi_out *mout,
3006 unsigned int stream_tag,
3007 unsigned int format,
3008 struct snd_pcm_substream *substream)
3010 mutex_lock(&codec->spdif_mutex);
3011 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
3012 mutex_unlock(&codec->spdif_mutex);
3015 EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_prepare);
3018 * release the digital out
3020 int snd_hda_multi_out_dig_close(struct hda_codec *codec,
3021 struct hda_multi_out *mout)
3023 mutex_lock(&codec->spdif_mutex);
3024 mout->dig_out_used = 0;
3025 mutex_unlock(&codec->spdif_mutex);
3028 EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_close);
3031 * set up more restrictions for analog out
3033 int snd_hda_multi_out_analog_open(struct hda_codec *codec,
3034 struct hda_multi_out *mout,
3035 struct snd_pcm_substream *substream,
3036 struct hda_pcm_stream *hinfo)
3038 struct snd_pcm_runtime *runtime = substream->runtime;
3039 runtime->hw.channels_max = mout->max_channels;
3040 if (mout->dig_out_nid) {
3041 if (!mout->analog_rates) {
3042 mout->analog_rates = hinfo->rates;
3043 mout->analog_formats = hinfo->formats;
3044 mout->analog_maxbps = hinfo->maxbps;
3046 runtime->hw.rates = mout->analog_rates;
3047 runtime->hw.formats = mout->analog_formats;
3048 hinfo->maxbps = mout->analog_maxbps;
3050 if (!mout->spdif_rates) {
3051 snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
3053 &mout->spdif_formats,
3054 &mout->spdif_maxbps);
3056 mutex_lock(&codec->spdif_mutex);
3057 if (mout->share_spdif) {
3058 runtime->hw.rates &= mout->spdif_rates;
3059 runtime->hw.formats &= mout->spdif_formats;
3060 if (mout->spdif_maxbps < hinfo->maxbps)
3061 hinfo->maxbps = mout->spdif_maxbps;
3063 mutex_unlock(&codec->spdif_mutex);
3065 return snd_pcm_hw_constraint_step(substream->runtime, 0,
3066 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
3068 EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_open);
3071 * set up the i/o for analog out
3072 * when the digital out is available, copy the front out to digital out, too.
3074 int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
3075 struct hda_multi_out *mout,
3076 unsigned int stream_tag,
3077 unsigned int format,
3078 struct snd_pcm_substream *substream)
3080 hda_nid_t *nids = mout->dac_nids;
3081 int chs = substream->runtime->channels;
3084 mutex_lock(&codec->spdif_mutex);
3085 if (mout->dig_out_nid && mout->share_spdif &&
3086 mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
3088 snd_hda_is_supported_format(codec, mout->dig_out_nid,
3090 !(codec->spdif_status & IEC958_AES0_NONAUDIO)) {
3091 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
3092 setup_dig_out_stream(codec, mout->dig_out_nid,
3093 stream_tag, format);
3095 mout->dig_out_used = 0;
3096 cleanup_dig_out_stream(codec, mout->dig_out_nid);
3099 mutex_unlock(&codec->spdif_mutex);
3102 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
3104 if (!mout->no_share_stream &&
3105 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
3106 /* headphone out will just decode front left/right (stereo) */
3107 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
3109 /* extra outputs copied from front */
3110 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
3111 if (!mout->no_share_stream && mout->extra_out_nid[i])
3112 snd_hda_codec_setup_stream(codec,
3113 mout->extra_out_nid[i],
3114 stream_tag, 0, format);
3117 for (i = 1; i < mout->num_dacs; i++) {
3118 if (chs >= (i + 1) * 2) /* independent out */
3119 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
3121 else if (!mout->no_share_stream) /* copy front */
3122 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
3127 EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_prepare);
3130 * clean up the setting for analog out
3132 int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
3133 struct hda_multi_out *mout)
3135 hda_nid_t *nids = mout->dac_nids;
3138 for (i = 0; i < mout->num_dacs; i++)
3139 snd_hda_codec_cleanup_stream(codec, nids[i]);
3141 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
3142 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
3143 if (mout->extra_out_nid[i])
3144 snd_hda_codec_cleanup_stream(codec,
3145 mout->extra_out_nid[i]);
3146 mutex_lock(&codec->spdif_mutex);
3147 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
3148 cleanup_dig_out_stream(codec, mout->dig_out_nid);
3149 mout->dig_out_used = 0;
3151 mutex_unlock(&codec->spdif_mutex);
3154 EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_cleanup);
3157 * Helper for automatic pin configuration
3160 static int is_in_nid_list(hda_nid_t nid, hda_nid_t *list)
3162 for (; *list; list++)
3170 * Sort an associated group of pins according to their sequence numbers.
3172 static void sort_pins_by_sequence(hda_nid_t * pins, short * sequences,
3179 for (i = 0; i < num_pins; i++) {
3180 for (j = i + 1; j < num_pins; j++) {
3181 if (sequences[i] > sequences[j]) {
3183 sequences[i] = sequences[j];
3195 * Parse all pin widgets and store the useful pin nids to cfg
3197 * The number of line-outs or any primary output is stored in line_outs,
3198 * and the corresponding output pins are assigned to line_out_pins[],
3199 * in the order of front, rear, CLFE, side, ...
3201 * If more extra outputs (speaker and headphone) are found, the pins are
3202 * assisnged to hp_pins[] and speaker_pins[], respectively. If no line-out jack
3203 * is detected, one of speaker of HP pins is assigned as the primary
3204 * output, i.e. to line_out_pins[0]. So, line_outs is always positive
3205 * if any analog output exists.
3207 * The analog input pins are assigned to input_pins array.
3208 * The digital input/output pins are assigned to dig_in_pin and dig_out_pin,
3211 int snd_hda_parse_pin_def_config(struct hda_codec *codec,
3212 struct auto_pin_cfg *cfg,
3213 hda_nid_t *ignore_nids)
3215 hda_nid_t nid, end_nid;
3216 short seq, assoc_line_out, assoc_speaker;
3217 short sequences_line_out[ARRAY_SIZE(cfg->line_out_pins)];
3218 short sequences_speaker[ARRAY_SIZE(cfg->speaker_pins)];
3219 short sequences_hp[ARRAY_SIZE(cfg->hp_pins)];
3221 memset(cfg, 0, sizeof(*cfg));
3223 memset(sequences_line_out, 0, sizeof(sequences_line_out));
3224 memset(sequences_speaker, 0, sizeof(sequences_speaker));
3225 memset(sequences_hp, 0, sizeof(sequences_hp));
3226 assoc_line_out = assoc_speaker = 0;
3228 end_nid = codec->start_nid + codec->num_nodes;
3229 for (nid = codec->start_nid; nid < end_nid; nid++) {
3230 unsigned int wid_caps = get_wcaps(codec, nid);
3231 unsigned int wid_type =
3232 (wid_caps & AC_WCAP_TYPE) >> AC_WCAP_TYPE_SHIFT;
3233 unsigned int def_conf;
3236 /* read all default configuration for pin complex */
3237 if (wid_type != AC_WID_PIN)
3239 /* ignore the given nids (e.g. pc-beep returns error) */
3240 if (ignore_nids && is_in_nid_list(nid, ignore_nids))
3243 def_conf = snd_hda_codec_read(codec, nid, 0,
3244 AC_VERB_GET_CONFIG_DEFAULT, 0);
3245 if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE)
3247 loc = get_defcfg_location(def_conf);
3248 switch (get_defcfg_device(def_conf)) {
3249 case AC_JACK_LINE_OUT:
3250 seq = get_defcfg_sequence(def_conf);
3251 assoc = get_defcfg_association(def_conf);
3253 if (!(wid_caps & AC_WCAP_STEREO))
3254 if (!cfg->mono_out_pin)
3255 cfg->mono_out_pin = nid;
3258 if (!assoc_line_out)
3259 assoc_line_out = assoc;
3260 else if (assoc_line_out != assoc)
3262 if (cfg->line_outs >= ARRAY_SIZE(cfg->line_out_pins))
3264 cfg->line_out_pins[cfg->line_outs] = nid;
3265 sequences_line_out[cfg->line_outs] = seq;
3268 case AC_JACK_SPEAKER:
3269 seq = get_defcfg_sequence(def_conf);
3270 assoc = get_defcfg_association(def_conf);
3273 if (! assoc_speaker)
3274 assoc_speaker = assoc;
3275 else if (assoc_speaker != assoc)
3277 if (cfg->speaker_outs >= ARRAY_SIZE(cfg->speaker_pins))
3279 cfg->speaker_pins[cfg->speaker_outs] = nid;
3280 sequences_speaker[cfg->speaker_outs] = seq;
3281 cfg->speaker_outs++;
3283 case AC_JACK_HP_OUT:
3284 seq = get_defcfg_sequence(def_conf);
3285 assoc = get_defcfg_association(def_conf);
3286 if (cfg->hp_outs >= ARRAY_SIZE(cfg->hp_pins))
3288 cfg->hp_pins[cfg->hp_outs] = nid;
3289 sequences_hp[cfg->hp_outs] = (assoc << 4) | seq;
3292 case AC_JACK_MIC_IN: {
3294 if (loc == AC_JACK_LOC_FRONT) {
3295 preferred = AUTO_PIN_FRONT_MIC;
3298 preferred = AUTO_PIN_MIC;
3299 alt = AUTO_PIN_FRONT_MIC;
3301 if (!cfg->input_pins[preferred])
3302 cfg->input_pins[preferred] = nid;
3303 else if (!cfg->input_pins[alt])
3304 cfg->input_pins[alt] = nid;
3307 case AC_JACK_LINE_IN:
3308 if (loc == AC_JACK_LOC_FRONT)
3309 cfg->input_pins[AUTO_PIN_FRONT_LINE] = nid;
3311 cfg->input_pins[AUTO_PIN_LINE] = nid;
3314 cfg->input_pins[AUTO_PIN_CD] = nid;
3317 cfg->input_pins[AUTO_PIN_AUX] = nid;
3319 case AC_JACK_SPDIF_OUT:
3320 cfg->dig_out_pin = nid;
3322 case AC_JACK_SPDIF_IN:
3323 cfg->dig_in_pin = nid;
3329 * If no line-out is defined but multiple HPs are found,
3330 * some of them might be the real line-outs.
3332 if (!cfg->line_outs && cfg->hp_outs > 1) {
3334 while (i < cfg->hp_outs) {
3335 /* The real HPs should have the sequence 0x0f */
3336 if ((sequences_hp[i] & 0x0f) == 0x0f) {
3340 /* Move it to the line-out table */
3341 cfg->line_out_pins[cfg->line_outs] = cfg->hp_pins[i];
3342 sequences_line_out[cfg->line_outs] = sequences_hp[i];
3345 memmove(cfg->hp_pins + i, cfg->hp_pins + i + 1,
3346 sizeof(cfg->hp_pins[0]) * (cfg->hp_outs - i));
3347 memmove(sequences_hp + i - 1, sequences_hp + i,
3348 sizeof(sequences_hp[0]) * (cfg->hp_outs - i));
3352 /* sort by sequence */
3353 sort_pins_by_sequence(cfg->line_out_pins, sequences_line_out,
3355 sort_pins_by_sequence(cfg->speaker_pins, sequences_speaker,
3357 sort_pins_by_sequence(cfg->hp_pins, sequences_hp,
3360 /* if we have only one mic, make it AUTO_PIN_MIC */
3361 if (!cfg->input_pins[AUTO_PIN_MIC] &&
3362 cfg->input_pins[AUTO_PIN_FRONT_MIC]) {
3363 cfg->input_pins[AUTO_PIN_MIC] =
3364 cfg->input_pins[AUTO_PIN_FRONT_MIC];
3365 cfg->input_pins[AUTO_PIN_FRONT_MIC] = 0;
3367 /* ditto for line-in */
3368 if (!cfg->input_pins[AUTO_PIN_LINE] &&
3369 cfg->input_pins[AUTO_PIN_FRONT_LINE]) {
3370 cfg->input_pins[AUTO_PIN_LINE] =
3371 cfg->input_pins[AUTO_PIN_FRONT_LINE];
3372 cfg->input_pins[AUTO_PIN_FRONT_LINE] = 0;
3376 * FIX-UP: if no line-outs are detected, try to use speaker or HP pin
3377 * as a primary output
3379 if (!cfg->line_outs) {
3380 if (cfg->speaker_outs) {
3381 cfg->line_outs = cfg->speaker_outs;
3382 memcpy(cfg->line_out_pins, cfg->speaker_pins,
3383 sizeof(cfg->speaker_pins));
3384 cfg->speaker_outs = 0;
3385 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
3386 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
3387 } else if (cfg->hp_outs) {
3388 cfg->line_outs = cfg->hp_outs;
3389 memcpy(cfg->line_out_pins, cfg->hp_pins,
3390 sizeof(cfg->hp_pins));
3392 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
3393 cfg->line_out_type = AUTO_PIN_HP_OUT;
3397 /* Reorder the surround channels
3398 * ALSA sequence is front/surr/clfe/side
3400 * 4-ch: front/surr => OK as it is
3401 * 6-ch: front/clfe/surr
3402 * 8-ch: front/clfe/rear/side|fc
3404 switch (cfg->line_outs) {
3407 nid = cfg->line_out_pins[1];
3408 cfg->line_out_pins[1] = cfg->line_out_pins[2];
3409 cfg->line_out_pins[2] = nid;
3414 * debug prints of the parsed results
3416 snd_printd("autoconfig: line_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
3417 cfg->line_outs, cfg->line_out_pins[0], cfg->line_out_pins[1],
3418 cfg->line_out_pins[2], cfg->line_out_pins[3],
3419 cfg->line_out_pins[4]);
3420 snd_printd(" speaker_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
3421 cfg->speaker_outs, cfg->speaker_pins[0],
3422 cfg->speaker_pins[1], cfg->speaker_pins[2],
3423 cfg->speaker_pins[3], cfg->speaker_pins[4]);
3424 snd_printd(" hp_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
3425 cfg->hp_outs, cfg->hp_pins[0],
3426 cfg->hp_pins[1], cfg->hp_pins[2],
3427 cfg->hp_pins[3], cfg->hp_pins[4]);
3428 snd_printd(" mono: mono_out=0x%x\n", cfg->mono_out_pin);
3429 snd_printd(" inputs: mic=0x%x, fmic=0x%x, line=0x%x, fline=0x%x,"
3430 " cd=0x%x, aux=0x%x\n",
3431 cfg->input_pins[AUTO_PIN_MIC],
3432 cfg->input_pins[AUTO_PIN_FRONT_MIC],
3433 cfg->input_pins[AUTO_PIN_LINE],
3434 cfg->input_pins[AUTO_PIN_FRONT_LINE],
3435 cfg->input_pins[AUTO_PIN_CD],
3436 cfg->input_pins[AUTO_PIN_AUX]);
3440 EXPORT_SYMBOL_HDA(snd_hda_parse_pin_def_config);
3442 /* labels for input pins */
3443 const char *auto_pin_cfg_labels[AUTO_PIN_LAST] = {
3444 "Mic", "Front Mic", "Line", "Front Line", "CD", "Aux"
3446 EXPORT_SYMBOL_HDA(auto_pin_cfg_labels);
3455 * snd_hda_suspend - suspend the codecs
3457 * @state: suspsend state
3459 * Returns 0 if successful.
3461 int snd_hda_suspend(struct hda_bus *bus, pm_message_t state)
3463 struct hda_codec *codec;
3465 list_for_each_entry(codec, &bus->codec_list, list) {
3466 #ifdef CONFIG_SND_HDA_POWER_SAVE
3467 if (!codec->power_on)
3470 hda_call_codec_suspend(codec);
3474 EXPORT_SYMBOL_HDA(snd_hda_suspend);
3477 * snd_hda_resume - resume the codecs
3480 * Returns 0 if successful.
3482 * This fucntion is defined only when POWER_SAVE isn't set.
3483 * In the power-save mode, the codec is resumed dynamically.
3485 int snd_hda_resume(struct hda_bus *bus)
3487 struct hda_codec *codec;
3489 list_for_each_entry(codec, &bus->codec_list, list) {
3490 if (snd_hda_codec_needs_resume(codec))
3491 hda_call_codec_resume(codec);
3495 EXPORT_SYMBOL_HDA(snd_hda_resume);
3496 #endif /* CONFIG_PM */
3502 /* get a new element from the given array
3503 * if it exceeds the pre-allocated array size, re-allocate the array
3505 void *snd_array_new(struct snd_array *array)
3507 if (array->used >= array->alloced) {
3508 int num = array->alloced + array->alloc_align;
3510 if (snd_BUG_ON(num >= 4096))
3512 nlist = kcalloc(num + 1, array->elem_size, GFP_KERNEL);
3516 memcpy(nlist, array->list,
3517 array->elem_size * array->alloced);
3520 array->list = nlist;
3521 array->alloced = num;
3523 return snd_array_elem(array, array->used++);
3525 EXPORT_SYMBOL_HDA(snd_array_new);
3527 /* free the given array elements */
3528 void snd_array_free(struct snd_array *array)
3535 EXPORT_SYMBOL_HDA(snd_array_free);
3538 * used by hda_proc.c and hda_eld.c
3540 void snd_print_pcm_rates(int pcm, char *buf, int buflen)
3542 static unsigned int rates[] = {
3543 8000, 11025, 16000, 22050, 32000, 44100, 48000, 88200,
3544 96000, 176400, 192000, 384000
3548 for (i = 0, j = 0; i < ARRAY_SIZE(rates); i++)
3550 j += snprintf(buf + j, buflen - j, " %d", rates[i]);
3552 buf[j] = '\0'; /* necessary when j == 0 */
3554 EXPORT_SYMBOL_HDA(snd_print_pcm_rates);
3556 void snd_print_pcm_bits(int pcm, char *buf, int buflen)
3558 static unsigned int bits[] = { 8, 16, 20, 24, 32 };
3561 for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
3562 if (pcm & (AC_SUPPCM_BITS_8 << i))
3563 j += snprintf(buf + j, buflen - j, " %d", bits[i]);
3565 buf[j] = '\0'; /* necessary when j == 0 */
3567 EXPORT_SYMBOL_HDA(snd_print_pcm_bits);
3569 MODULE_DESCRIPTION("HDA codec core");
3570 MODULE_LICENSE("GPL");