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" },
51 { 0x1102, "Creative" },
55 { 0x11d4, "Analog Devices" },
56 { 0x13f6, "C-Media" },
57 { 0x14f1, "Conexant" },
58 { 0x17e8, "Chrontel" },
60 { 0x1aec, "Wolfson Microelectronics" },
61 { 0x434d, "C-Media" },
63 { 0x8384, "SigmaTel" },
67 static DEFINE_MUTEX(preset_mutex);
68 static LIST_HEAD(hda_preset_tables);
70 int snd_hda_add_codec_preset(struct hda_codec_preset_list *preset)
72 mutex_lock(&preset_mutex);
73 list_add_tail(&preset->list, &hda_preset_tables);
74 mutex_unlock(&preset_mutex);
77 EXPORT_SYMBOL_HDA(snd_hda_add_codec_preset);
79 int snd_hda_delete_codec_preset(struct hda_codec_preset_list *preset)
81 mutex_lock(&preset_mutex);
82 list_del(&preset->list);
83 mutex_unlock(&preset_mutex);
86 EXPORT_SYMBOL_HDA(snd_hda_delete_codec_preset);
88 #ifdef CONFIG_SND_HDA_POWER_SAVE
89 static void hda_power_work(struct work_struct *work);
90 static void hda_keep_power_on(struct hda_codec *codec);
92 static inline void hda_keep_power_on(struct hda_codec *codec) {}
95 const char *snd_hda_get_jack_location(u32 cfg)
97 static char *bases[7] = {
98 "N/A", "Rear", "Front", "Left", "Right", "Top", "Bottom",
100 static unsigned char specials_idx[] = {
105 static char *specials[] = {
106 "Rear Panel", "Drive Bar",
107 "Riser", "HDMI", "ATAPI",
108 "Mobile-In", "Mobile-Out"
111 cfg = (cfg & AC_DEFCFG_LOCATION) >> AC_DEFCFG_LOCATION_SHIFT;
112 if ((cfg & 0x0f) < 7)
113 return bases[cfg & 0x0f];
114 for (i = 0; i < ARRAY_SIZE(specials_idx); i++) {
115 if (cfg == specials_idx[i])
120 EXPORT_SYMBOL_HDA(snd_hda_get_jack_location);
122 const char *snd_hda_get_jack_connectivity(u32 cfg)
124 static char *jack_locations[4] = { "Ext", "Int", "Sep", "Oth" };
126 return jack_locations[(cfg >> (AC_DEFCFG_LOCATION_SHIFT + 4)) & 3];
128 EXPORT_SYMBOL_HDA(snd_hda_get_jack_connectivity);
130 const char *snd_hda_get_jack_type(u32 cfg)
132 static char *jack_types[16] = {
133 "Line Out", "Speaker", "HP Out", "CD",
134 "SPDIF Out", "Digital Out", "Modem Line", "Modem Hand",
135 "Line In", "Aux", "Mic", "Telephony",
136 "SPDIF In", "Digitial In", "Reserved", "Other"
139 return jack_types[(cfg & AC_DEFCFG_DEVICE)
140 >> AC_DEFCFG_DEVICE_SHIFT];
142 EXPORT_SYMBOL_HDA(snd_hda_get_jack_type);
145 * Compose a 32bit command word to be sent to the HD-audio controller
147 static inline unsigned int
148 make_codec_cmd(struct hda_codec *codec, hda_nid_t nid, int direct,
149 unsigned int verb, unsigned int parm)
153 val = (u32)(codec->addr & 0x0f) << 28;
154 val |= (u32)direct << 27;
155 val |= (u32)nid << 20;
162 * snd_hda_codec_read - send a command and get the response
163 * @codec: the HDA codec
164 * @nid: NID to send the command
165 * @direct: direct flag
166 * @verb: the verb to send
167 * @parm: the parameter for the verb
169 * Send a single command and read the corresponding response.
171 * Returns the obtained response value, or -1 for an error.
173 unsigned int snd_hda_codec_read(struct hda_codec *codec, hda_nid_t nid,
175 unsigned int verb, unsigned int parm)
177 struct hda_bus *bus = codec->bus;
180 res = make_codec_cmd(codec, nid, direct, verb, parm);
181 snd_hda_power_up(codec);
182 mutex_lock(&bus->cmd_mutex);
183 if (!bus->ops.command(bus, res))
184 res = bus->ops.get_response(bus);
186 res = (unsigned int)-1;
187 mutex_unlock(&bus->cmd_mutex);
188 snd_hda_power_down(codec);
191 EXPORT_SYMBOL_HDA(snd_hda_codec_read);
194 * snd_hda_codec_write - send a single command without waiting for response
195 * @codec: the HDA codec
196 * @nid: NID to send the command
197 * @direct: direct flag
198 * @verb: the verb to send
199 * @parm: the parameter for the verb
201 * Send a single command without waiting for response.
203 * Returns 0 if successful, or a negative error code.
205 int snd_hda_codec_write(struct hda_codec *codec, hda_nid_t nid, int direct,
206 unsigned int verb, unsigned int parm)
208 struct hda_bus *bus = codec->bus;
212 res = make_codec_cmd(codec, nid, direct, verb, parm);
213 snd_hda_power_up(codec);
214 mutex_lock(&bus->cmd_mutex);
215 err = bus->ops.command(bus, res);
216 mutex_unlock(&bus->cmd_mutex);
217 snd_hda_power_down(codec);
220 EXPORT_SYMBOL_HDA(snd_hda_codec_write);
223 * snd_hda_sequence_write - sequence writes
224 * @codec: the HDA codec
225 * @seq: VERB array to send
227 * Send the commands sequentially from the given array.
228 * The array must be terminated with NID=0.
230 void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq)
232 for (; seq->nid; seq++)
233 snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param);
235 EXPORT_SYMBOL_HDA(snd_hda_sequence_write);
238 * snd_hda_get_sub_nodes - get the range of sub nodes
239 * @codec: the HDA codec
241 * @start_id: the pointer to store the start NID
243 * Parse the NID and store the start NID of its sub-nodes.
244 * Returns the number of sub-nodes.
246 int snd_hda_get_sub_nodes(struct hda_codec *codec, hda_nid_t nid,
251 parm = snd_hda_param_read(codec, nid, AC_PAR_NODE_COUNT);
254 *start_id = (parm >> 16) & 0x7fff;
255 return (int)(parm & 0x7fff);
257 EXPORT_SYMBOL_HDA(snd_hda_get_sub_nodes);
260 * snd_hda_get_connections - get connection list
261 * @codec: the HDA codec
263 * @conn_list: connection list array
264 * @max_conns: max. number of connections to store
266 * Parses the connection list of the given widget and stores the list
269 * Returns the number of connections, or a negative error code.
271 int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
272 hda_nid_t *conn_list, int max_conns)
275 int i, conn_len, conns;
276 unsigned int shift, num_elems, mask;
279 if (snd_BUG_ON(!conn_list || max_conns <= 0))
282 parm = snd_hda_param_read(codec, nid, AC_PAR_CONNLIST_LEN);
283 if (parm & AC_CLIST_LONG) {
292 conn_len = parm & AC_CLIST_LENGTH;
293 mask = (1 << (shift-1)) - 1;
296 return 0; /* no connection */
299 /* single connection */
300 parm = snd_hda_codec_read(codec, nid, 0,
301 AC_VERB_GET_CONNECT_LIST, 0);
302 conn_list[0] = parm & mask;
306 /* multi connection */
309 for (i = 0; i < conn_len; i++) {
313 if (i % num_elems == 0)
314 parm = snd_hda_codec_read(codec, nid, 0,
315 AC_VERB_GET_CONNECT_LIST, i);
316 range_val = !!(parm & (1 << (shift-1))); /* ranges */
320 /* ranges between the previous and this one */
321 if (!prev_nid || prev_nid >= val) {
322 snd_printk(KERN_WARNING "hda_codec: "
323 "invalid dep_range_val %x:%x\n",
327 for (n = prev_nid + 1; n <= val; n++) {
328 if (conns >= max_conns) {
330 "Too many connections\n");
333 conn_list[conns++] = n;
336 if (conns >= max_conns) {
337 snd_printk(KERN_ERR "Too many connections\n");
340 conn_list[conns++] = val;
346 EXPORT_SYMBOL_HDA(snd_hda_get_connections);
350 * snd_hda_queue_unsol_event - add an unsolicited event to queue
352 * @res: unsolicited event (lower 32bit of RIRB entry)
353 * @res_ex: codec addr and flags (upper 32bit or RIRB entry)
355 * Adds the given event to the queue. The events are processed in
356 * the workqueue asynchronously. Call this function in the interrupt
357 * hanlder when RIRB receives an unsolicited event.
359 * Returns 0 if successful, or a negative error code.
361 int snd_hda_queue_unsol_event(struct hda_bus *bus, u32 res, u32 res_ex)
363 struct hda_bus_unsolicited *unsol;
370 wp = (unsol->wp + 1) % HDA_UNSOL_QUEUE_SIZE;
374 unsol->queue[wp] = res;
375 unsol->queue[wp + 1] = res_ex;
377 queue_work(bus->workq, &unsol->work);
381 EXPORT_SYMBOL_HDA(snd_hda_queue_unsol_event);
384 * process queued unsolicited events
386 static void process_unsol_events(struct work_struct *work)
388 struct hda_bus_unsolicited *unsol =
389 container_of(work, struct hda_bus_unsolicited, work);
390 struct hda_bus *bus = unsol->bus;
391 struct hda_codec *codec;
392 unsigned int rp, caddr, res;
394 while (unsol->rp != unsol->wp) {
395 rp = (unsol->rp + 1) % HDA_UNSOL_QUEUE_SIZE;
398 res = unsol->queue[rp];
399 caddr = unsol->queue[rp + 1];
400 if (!(caddr & (1 << 4))) /* no unsolicited event? */
402 codec = bus->caddr_tbl[caddr & 0x0f];
403 if (codec && codec->patch_ops.unsol_event)
404 codec->patch_ops.unsol_event(codec, res);
409 * initialize unsolicited queue
411 static int init_unsol_queue(struct hda_bus *bus)
413 struct hda_bus_unsolicited *unsol;
415 if (bus->unsol) /* already initialized */
418 unsol = kzalloc(sizeof(*unsol), GFP_KERNEL);
420 snd_printk(KERN_ERR "hda_codec: "
421 "can't allocate unsolicited queue\n");
424 INIT_WORK(&unsol->work, process_unsol_events);
433 static void snd_hda_codec_free(struct hda_codec *codec);
435 static int snd_hda_bus_free(struct hda_bus *bus)
437 struct hda_codec *codec, *n;
442 flush_workqueue(bus->workq);
445 list_for_each_entry_safe(codec, n, &bus->codec_list, list) {
446 snd_hda_codec_free(codec);
448 if (bus->ops.private_free)
449 bus->ops.private_free(bus);
451 destroy_workqueue(bus->workq);
456 static int snd_hda_bus_dev_free(struct snd_device *device)
458 struct hda_bus *bus = device->device_data;
460 return snd_hda_bus_free(bus);
463 #ifdef CONFIG_SND_HDA_HWDEP
464 static int snd_hda_bus_dev_register(struct snd_device *device)
466 struct hda_bus *bus = device->device_data;
467 struct hda_codec *codec;
468 list_for_each_entry(codec, &bus->codec_list, list) {
469 snd_hda_hwdep_add_sysfs(codec);
474 #define snd_hda_bus_dev_register NULL
478 * snd_hda_bus_new - create a HDA bus
479 * @card: the card entry
480 * @temp: the template for hda_bus information
481 * @busp: the pointer to store the created bus instance
483 * Returns 0 if successful, or a negative error code.
485 int /*__devinit*/ snd_hda_bus_new(struct snd_card *card,
486 const struct hda_bus_template *temp,
487 struct hda_bus **busp)
491 static struct snd_device_ops dev_ops = {
492 .dev_register = snd_hda_bus_dev_register,
493 .dev_free = snd_hda_bus_dev_free,
496 if (snd_BUG_ON(!temp))
498 if (snd_BUG_ON(!temp->ops.command || !temp->ops.get_response))
504 bus = kzalloc(sizeof(*bus), GFP_KERNEL);
506 snd_printk(KERN_ERR "can't allocate struct hda_bus\n");
511 bus->private_data = temp->private_data;
512 bus->pci = temp->pci;
513 bus->modelname = temp->modelname;
514 bus->power_save = temp->power_save;
515 bus->ops = temp->ops;
517 mutex_init(&bus->cmd_mutex);
518 INIT_LIST_HEAD(&bus->codec_list);
520 snprintf(bus->workq_name, sizeof(bus->workq_name),
521 "hd-audio%d", card->number);
522 bus->workq = create_singlethread_workqueue(bus->workq_name);
524 snd_printk(KERN_ERR "cannot create workqueue %s\n",
530 err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops);
532 snd_hda_bus_free(bus);
539 EXPORT_SYMBOL_HDA(snd_hda_bus_new);
541 #ifdef CONFIG_SND_HDA_GENERIC
542 #define is_generic_config(codec) \
543 (codec->modelname && !strcmp(codec->modelname, "generic"))
545 #define is_generic_config(codec) 0
549 #define HDA_MODREQ_MAX_COUNT 2 /* two request_modules()'s */
551 #define HDA_MODREQ_MAX_COUNT 0 /* all presets are statically linked */
555 * find a matching codec preset
557 static const struct hda_codec_preset *
558 find_codec_preset(struct hda_codec *codec)
560 struct hda_codec_preset_list *tbl;
561 const struct hda_codec_preset *preset;
562 int mod_requested = 0;
564 if (is_generic_config(codec))
565 return NULL; /* use the generic parser */
568 mutex_lock(&preset_mutex);
569 list_for_each_entry(tbl, &hda_preset_tables, list) {
570 if (!try_module_get(tbl->owner)) {
571 snd_printk(KERN_ERR "hda_codec: cannot module_get\n");
574 for (preset = tbl->preset; preset->id; preset++) {
575 u32 mask = preset->mask;
576 if (preset->afg && preset->afg != codec->afg)
578 if (preset->mfg && preset->mfg != codec->mfg)
582 if (preset->id == (codec->vendor_id & mask) &&
584 preset->rev == codec->revision_id)) {
585 mutex_unlock(&preset_mutex);
586 codec->owner = tbl->owner;
590 module_put(tbl->owner);
592 mutex_unlock(&preset_mutex);
594 if (mod_requested < HDA_MODREQ_MAX_COUNT) {
597 snprintf(name, sizeof(name), "snd-hda-codec-id:%08x",
600 snprintf(name, sizeof(name), "snd-hda-codec-id:%04x*",
601 (codec->vendor_id >> 16) & 0xffff);
602 request_module(name);
610 * get_codec_name - store the codec name
612 static int get_codec_name(struct hda_codec *codec)
614 const struct hda_vendor_id *c;
615 const char *vendor = NULL;
616 u16 vendor_id = codec->vendor_id >> 16;
617 char tmp[16], name[32];
619 for (c = hda_vendor_ids; c->id; c++) {
620 if (c->id == vendor_id) {
626 sprintf(tmp, "Generic %04x", vendor_id);
629 if (codec->preset && codec->preset->name)
630 snprintf(name, sizeof(name), "%s %s", vendor,
631 codec->preset->name);
633 snprintf(name, sizeof(name), "%s ID %x", vendor,
634 codec->vendor_id & 0xffff);
635 codec->name = kstrdup(name, GFP_KERNEL);
642 * look for an AFG and MFG nodes
644 static void /*__devinit*/ setup_fg_nodes(struct hda_codec *codec)
649 total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid);
650 for (i = 0; i < total_nodes; i++, nid++) {
651 codec->function_id = snd_hda_param_read(codec, nid,
652 AC_PAR_FUNCTION_TYPE) & 0xff;
653 switch (codec->function_id) {
654 case AC_GRP_AUDIO_FUNCTION:
657 case AC_GRP_MODEM_FUNCTION:
667 * read widget caps for each widget and store in cache
669 static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
674 codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node,
676 codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL);
679 nid = codec->start_nid;
680 for (i = 0; i < codec->num_nodes; i++, nid++)
681 codec->wcaps[i] = snd_hda_param_read(codec, nid,
682 AC_PAR_AUDIO_WIDGET_CAP);
686 /* read all pin default configurations and save codec->init_pins */
687 static int read_pin_defaults(struct hda_codec *codec)
690 hda_nid_t nid = codec->start_nid;
692 for (i = 0; i < codec->num_nodes; i++, nid++) {
693 struct hda_pincfg *pin;
694 unsigned int wcaps = get_wcaps(codec, nid);
695 unsigned int wid_type = (wcaps & AC_WCAP_TYPE) >>
697 if (wid_type != AC_WID_PIN)
699 pin = snd_array_new(&codec->init_pins);
703 pin->cfg = snd_hda_codec_read(codec, nid, 0,
704 AC_VERB_GET_CONFIG_DEFAULT, 0);
709 /* look up the given pin config list and return the item matching with NID */
710 static struct hda_pincfg *look_up_pincfg(struct hda_codec *codec,
711 struct snd_array *array,
715 for (i = 0; i < array->used; i++) {
716 struct hda_pincfg *pin = snd_array_elem(array, i);
723 /* write a config value for the given NID */
724 static void set_pincfg(struct hda_codec *codec, hda_nid_t nid,
728 for (i = 0; i < 4; i++) {
729 snd_hda_codec_write(codec, nid, 0,
730 AC_VERB_SET_CONFIG_DEFAULT_BYTES_0 + i,
736 /* set the current pin config value for the given NID.
737 * the value is cached, and read via snd_hda_codec_get_pincfg()
739 int snd_hda_add_pincfg(struct hda_codec *codec, struct snd_array *list,
740 hda_nid_t nid, unsigned int cfg)
742 struct hda_pincfg *pin;
745 oldcfg = snd_hda_codec_get_pincfg(codec, nid);
746 pin = look_up_pincfg(codec, list, nid);
748 pin = snd_array_new(list);
755 /* change only when needed; e.g. if the pincfg is already present
756 * in user_pins[], don't write it
758 cfg = snd_hda_codec_get_pincfg(codec, nid);
760 set_pincfg(codec, nid, cfg);
764 int snd_hda_codec_set_pincfg(struct hda_codec *codec,
765 hda_nid_t nid, unsigned int cfg)
767 return snd_hda_add_pincfg(codec, &codec->driver_pins, nid, cfg);
769 EXPORT_SYMBOL_HDA(snd_hda_codec_set_pincfg);
771 /* get the current pin config value of the given pin NID */
772 unsigned int snd_hda_codec_get_pincfg(struct hda_codec *codec, hda_nid_t nid)
774 struct hda_pincfg *pin;
776 #ifdef CONFIG_SND_HDA_HWDEP
777 pin = look_up_pincfg(codec, &codec->user_pins, nid);
781 pin = look_up_pincfg(codec, &codec->driver_pins, nid);
784 pin = look_up_pincfg(codec, &codec->init_pins, nid);
789 EXPORT_SYMBOL_HDA(snd_hda_codec_get_pincfg);
791 /* restore all current pin configs */
792 static void restore_pincfgs(struct hda_codec *codec)
795 for (i = 0; i < codec->init_pins.used; i++) {
796 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
797 set_pincfg(codec, pin->nid,
798 snd_hda_codec_get_pincfg(codec, pin->nid));
802 static void init_hda_cache(struct hda_cache_rec *cache,
803 unsigned int record_size);
804 static void free_hda_cache(struct hda_cache_rec *cache);
806 /* restore the initial pin cfgs and release all pincfg lists */
807 static void restore_init_pincfgs(struct hda_codec *codec)
809 /* first free driver_pins and user_pins, then call restore_pincfg
810 * so that only the values in init_pins are restored
812 snd_array_free(&codec->driver_pins);
813 #ifdef CONFIG_SND_HDA_HWDEP
814 snd_array_free(&codec->user_pins);
816 restore_pincfgs(codec);
817 snd_array_free(&codec->init_pins);
823 static void snd_hda_codec_free(struct hda_codec *codec)
827 restore_init_pincfgs(codec);
828 #ifdef CONFIG_SND_HDA_POWER_SAVE
829 cancel_delayed_work(&codec->power_work);
830 flush_workqueue(codec->bus->workq);
832 list_del(&codec->list);
833 snd_array_free(&codec->mixers);
834 codec->bus->caddr_tbl[codec->addr] = NULL;
835 if (codec->patch_ops.free)
836 codec->patch_ops.free(codec);
837 module_put(codec->owner);
838 free_hda_cache(&codec->amp_cache);
839 free_hda_cache(&codec->cmd_cache);
841 kfree(codec->modelname);
846 static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
847 unsigned int power_state);
850 * snd_hda_codec_new - create a HDA codec
851 * @bus: the bus to assign
852 * @codec_addr: the codec address
853 * @codecp: the pointer to store the generated codec
855 * Returns 0 if successful, or a negative error code.
857 int /*__devinit*/ snd_hda_codec_new(struct hda_bus *bus, unsigned int codec_addr,
858 int do_init, struct hda_codec **codecp)
860 struct hda_codec *codec;
864 if (snd_BUG_ON(!bus))
866 if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS))
869 if (bus->caddr_tbl[codec_addr]) {
870 snd_printk(KERN_ERR "hda_codec: "
871 "address 0x%x is already occupied\n", codec_addr);
875 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
877 snd_printk(KERN_ERR "can't allocate struct hda_codec\n");
882 codec->addr = codec_addr;
883 mutex_init(&codec->spdif_mutex);
884 mutex_init(&codec->control_mutex);
885 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
886 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
887 snd_array_init(&codec->mixers, sizeof(struct snd_kcontrol *), 32);
888 snd_array_init(&codec->init_pins, sizeof(struct hda_pincfg), 16);
889 snd_array_init(&codec->driver_pins, sizeof(struct hda_pincfg), 16);
890 if (codec->bus->modelname) {
891 codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL);
892 if (!codec->modelname) {
893 snd_hda_codec_free(codec);
898 #ifdef CONFIG_SND_HDA_POWER_SAVE
899 INIT_DELAYED_WORK(&codec->power_work, hda_power_work);
900 /* snd_hda_codec_new() marks the codec as power-up, and leave it as is.
901 * the caller has to power down appropriatley after initialization
904 hda_keep_power_on(codec);
907 list_add_tail(&codec->list, &bus->codec_list);
908 bus->caddr_tbl[codec_addr] = codec;
910 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
912 if (codec->vendor_id == -1)
913 /* read again, hopefully the access method was corrected
914 * in the last read...
916 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
918 codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT,
919 AC_PAR_SUBSYSTEM_ID);
920 codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT,
923 setup_fg_nodes(codec);
924 if (!codec->afg && !codec->mfg) {
925 snd_printdd("hda_codec: no AFG or MFG node found\n");
930 err = read_widget_caps(codec, codec->afg ? codec->afg : codec->mfg);
932 snd_printk(KERN_ERR "hda_codec: cannot malloc\n");
935 err = read_pin_defaults(codec);
939 if (!codec->subsystem_id) {
940 hda_nid_t nid = codec->afg ? codec->afg : codec->mfg;
941 codec->subsystem_id =
942 snd_hda_codec_read(codec, nid, 0,
943 AC_VERB_GET_SUBSYSTEM_ID, 0);
946 codec->modelname = kstrdup(bus->modelname, GFP_KERNEL);
948 /* power-up all before initialization */
949 hda_set_power_state(codec,
950 codec->afg ? codec->afg : codec->mfg,
954 err = snd_hda_codec_configure(codec);
958 snd_hda_codec_proc_new(codec);
960 snd_hda_create_hwdep(codec);
962 sprintf(component, "HDA:%08x,%08x,%08x", codec->vendor_id,
963 codec->subsystem_id, codec->revision_id);
964 snd_component_add(codec->bus->card, component);
971 snd_hda_codec_free(codec);
974 EXPORT_SYMBOL_HDA(snd_hda_codec_new);
976 int snd_hda_codec_configure(struct hda_codec *codec)
980 codec->preset = find_codec_preset(codec);
982 err = get_codec_name(codec);
986 /* audio codec should override the mixer name */
987 if (codec->afg || !*codec->bus->card->mixername)
988 strlcpy(codec->bus->card->mixername, codec->name,
989 sizeof(codec->bus->card->mixername));
991 if (is_generic_config(codec)) {
992 err = snd_hda_parse_generic_codec(codec);
995 if (codec->preset && codec->preset->patch) {
996 err = codec->preset->patch(codec);
1000 /* call the default parser */
1001 err = snd_hda_parse_generic_codec(codec);
1003 printk(KERN_ERR "hda-codec: No codec parser is available\n");
1006 if (!err && codec->patch_ops.unsol_event)
1007 err = init_unsol_queue(codec->bus);
1012 * snd_hda_codec_setup_stream - set up the codec for streaming
1013 * @codec: the CODEC to set up
1014 * @nid: the NID to set up
1015 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
1016 * @channel_id: channel id to pass, zero based.
1017 * @format: stream format.
1019 void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
1021 int channel_id, int format)
1026 snd_printdd("hda_codec_setup_stream: "
1027 "NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
1028 nid, stream_tag, channel_id, format);
1029 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID,
1030 (stream_tag << 4) | channel_id);
1032 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, format);
1034 EXPORT_SYMBOL_HDA(snd_hda_codec_setup_stream);
1036 void snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid)
1041 snd_printdd("hda_codec_cleanup_stream: NID=0x%x\n", nid);
1042 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
1043 #if 0 /* keep the format */
1045 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0);
1048 EXPORT_SYMBOL_HDA(snd_hda_codec_cleanup_stream);
1051 * amp access functions
1054 /* FIXME: more better hash key? */
1055 #define HDA_HASH_KEY(nid,dir,idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
1056 #define HDA_HASH_PINCAP_KEY(nid) (u32)((nid) + (0x02 << 24))
1057 #define INFO_AMP_CAPS (1<<0)
1058 #define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
1060 /* initialize the hash table */
1061 static void /*__devinit*/ init_hda_cache(struct hda_cache_rec *cache,
1062 unsigned int record_size)
1064 memset(cache, 0, sizeof(*cache));
1065 memset(cache->hash, 0xff, sizeof(cache->hash));
1066 snd_array_init(&cache->buf, record_size, 64);
1069 static void free_hda_cache(struct hda_cache_rec *cache)
1071 snd_array_free(&cache->buf);
1074 /* query the hash. allocate an entry if not found. */
1075 static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache,
1078 u16 idx = key % (u16)ARRAY_SIZE(cache->hash);
1079 u16 cur = cache->hash[idx];
1080 struct hda_cache_head *info;
1082 while (cur != 0xffff) {
1083 info = snd_array_elem(&cache->buf, cur);
1084 if (info->key == key)
1089 /* add a new hash entry */
1090 info = snd_array_new(&cache->buf);
1093 cur = snd_array_index(&cache->buf, info);
1096 info->next = cache->hash[idx];
1097 cache->hash[idx] = cur;
1102 /* query and allocate an amp hash entry */
1103 static inline struct hda_amp_info *
1104 get_alloc_amp_hash(struct hda_codec *codec, u32 key)
1106 return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key);
1110 * query AMP capabilities for the given widget and direction
1112 u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
1114 struct hda_amp_info *info;
1116 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, 0));
1119 if (!(info->head.val & INFO_AMP_CAPS)) {
1120 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
1122 info->amp_caps = snd_hda_param_read(codec, nid,
1123 direction == HDA_OUTPUT ?
1124 AC_PAR_AMP_OUT_CAP :
1127 info->head.val |= INFO_AMP_CAPS;
1129 return info->amp_caps;
1131 EXPORT_SYMBOL_HDA(query_amp_caps);
1133 int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
1136 struct hda_amp_info *info;
1138 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, dir, 0));
1141 info->amp_caps = caps;
1142 info->head.val |= INFO_AMP_CAPS;
1145 EXPORT_SYMBOL_HDA(snd_hda_override_amp_caps);
1147 u32 snd_hda_query_pin_caps(struct hda_codec *codec, hda_nid_t nid)
1149 struct hda_amp_info *info;
1151 info = get_alloc_amp_hash(codec, HDA_HASH_PINCAP_KEY(nid));
1154 if (!info->head.val) {
1155 info->amp_caps = snd_hda_param_read(codec, nid, AC_PAR_PIN_CAP);
1156 info->head.val |= INFO_AMP_CAPS;
1158 return info->amp_caps;
1160 EXPORT_SYMBOL_HDA(snd_hda_query_pin_caps);
1163 * read the current volume to info
1164 * if the cache exists, read the cache value.
1166 static unsigned int get_vol_mute(struct hda_codec *codec,
1167 struct hda_amp_info *info, hda_nid_t nid,
1168 int ch, int direction, int index)
1172 if (info->head.val & INFO_AMP_VOL(ch))
1173 return info->vol[ch];
1175 parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
1176 parm |= direction == HDA_OUTPUT ? AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
1178 val = snd_hda_codec_read(codec, nid, 0,
1179 AC_VERB_GET_AMP_GAIN_MUTE, parm);
1180 info->vol[ch] = val & 0xff;
1181 info->head.val |= INFO_AMP_VOL(ch);
1182 return info->vol[ch];
1186 * write the current volume in info to the h/w and update the cache
1188 static void put_vol_mute(struct hda_codec *codec, struct hda_amp_info *info,
1189 hda_nid_t nid, int ch, int direction, int index,
1194 parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
1195 parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
1196 parm |= index << AC_AMP_SET_INDEX_SHIFT;
1198 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
1199 info->vol[ch] = val;
1203 * read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
1205 int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
1206 int direction, int index)
1208 struct hda_amp_info *info;
1209 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
1212 return get_vol_mute(codec, info, nid, ch, direction, index);
1214 EXPORT_SYMBOL_HDA(snd_hda_codec_amp_read);
1217 * update the AMP value, mask = bit mask to set, val = the value
1219 int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
1220 int direction, int idx, int mask, int val)
1222 struct hda_amp_info *info;
1224 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, idx));
1228 val |= get_vol_mute(codec, info, nid, ch, direction, idx) & ~mask;
1229 if (info->vol[ch] == val)
1231 put_vol_mute(codec, info, nid, ch, direction, idx, val);
1234 EXPORT_SYMBOL_HDA(snd_hda_codec_amp_update);
1237 * update the AMP stereo with the same mask and value
1239 int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
1240 int direction, int idx, int mask, int val)
1243 for (ch = 0; ch < 2; ch++)
1244 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
1248 EXPORT_SYMBOL_HDA(snd_hda_codec_amp_stereo);
1250 #ifdef SND_HDA_NEEDS_RESUME
1251 /* resume the all amp commands from the cache */
1252 void snd_hda_codec_resume_amp(struct hda_codec *codec)
1254 struct hda_amp_info *buffer = codec->amp_cache.buf.list;
1257 for (i = 0; i < codec->amp_cache.buf.used; i++, buffer++) {
1258 u32 key = buffer->head.key;
1260 unsigned int idx, dir, ch;
1264 idx = (key >> 16) & 0xff;
1265 dir = (key >> 24) & 0xff;
1266 for (ch = 0; ch < 2; ch++) {
1267 if (!(buffer->head.val & INFO_AMP_VOL(ch)))
1269 put_vol_mute(codec, buffer, nid, ch, dir, idx,
1274 EXPORT_SYMBOL_HDA(snd_hda_codec_resume_amp);
1275 #endif /* SND_HDA_NEEDS_RESUME */
1278 int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
1279 struct snd_ctl_elem_info *uinfo)
1281 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1282 u16 nid = get_amp_nid(kcontrol);
1283 u8 chs = get_amp_channels(kcontrol);
1284 int dir = get_amp_direction(kcontrol);
1285 unsigned int ofs = get_amp_offset(kcontrol);
1288 caps = query_amp_caps(codec, nid, dir);
1290 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1292 printk(KERN_WARNING "hda_codec: "
1293 "num_steps = 0 for NID=0x%x (ctl = %s)\n", nid,
1299 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1300 uinfo->count = chs == 3 ? 2 : 1;
1301 uinfo->value.integer.min = 0;
1302 uinfo->value.integer.max = caps;
1305 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_info);
1308 static inline unsigned int
1309 read_amp_value(struct hda_codec *codec, hda_nid_t nid,
1310 int ch, int dir, int idx, unsigned int ofs)
1313 val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
1314 val &= HDA_AMP_VOLMASK;
1323 update_amp_value(struct hda_codec *codec, hda_nid_t nid,
1324 int ch, int dir, int idx, unsigned int ofs,
1329 return snd_hda_codec_amp_update(codec, nid, ch, dir, idx,
1330 HDA_AMP_VOLMASK, val);
1333 int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
1334 struct snd_ctl_elem_value *ucontrol)
1336 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1337 hda_nid_t nid = get_amp_nid(kcontrol);
1338 int chs = get_amp_channels(kcontrol);
1339 int dir = get_amp_direction(kcontrol);
1340 int idx = get_amp_index(kcontrol);
1341 unsigned int ofs = get_amp_offset(kcontrol);
1342 long *valp = ucontrol->value.integer.value;
1345 *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs);
1347 *valp = read_amp_value(codec, nid, 1, dir, idx, ofs);
1350 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_get);
1352 int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
1353 struct snd_ctl_elem_value *ucontrol)
1355 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1356 hda_nid_t nid = get_amp_nid(kcontrol);
1357 int chs = get_amp_channels(kcontrol);
1358 int dir = get_amp_direction(kcontrol);
1359 int idx = get_amp_index(kcontrol);
1360 unsigned int ofs = get_amp_offset(kcontrol);
1361 long *valp = ucontrol->value.integer.value;
1364 snd_hda_power_up(codec);
1366 change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp);
1370 change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp);
1371 snd_hda_power_down(codec);
1374 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_put);
1376 int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
1377 unsigned int size, unsigned int __user *_tlv)
1379 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1380 hda_nid_t nid = get_amp_nid(kcontrol);
1381 int dir = get_amp_direction(kcontrol);
1382 unsigned int ofs = get_amp_offset(kcontrol);
1383 u32 caps, val1, val2;
1385 if (size < 4 * sizeof(unsigned int))
1387 caps = query_amp_caps(codec, nid, dir);
1388 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
1389 val2 = (val2 + 1) * 25;
1390 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
1392 val1 = ((int)val1) * ((int)val2);
1393 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
1395 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
1397 if (put_user(val1, _tlv + 2))
1399 if (put_user(val2, _tlv + 3))
1403 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_tlv);
1406 * set (static) TLV for virtual master volume; recalculated as max 0dB
1408 void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
1414 caps = query_amp_caps(codec, nid, dir);
1415 nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1416 step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
1417 step = (step + 1) * 25;
1418 tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
1419 tlv[1] = 2 * sizeof(unsigned int);
1420 tlv[2] = -nums * step;
1423 EXPORT_SYMBOL_HDA(snd_hda_set_vmaster_tlv);
1425 /* find a mixer control element with the given name */
1426 static struct snd_kcontrol *
1427 _snd_hda_find_mixer_ctl(struct hda_codec *codec,
1428 const char *name, int idx)
1430 struct snd_ctl_elem_id id;
1431 memset(&id, 0, sizeof(id));
1432 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1434 if (snd_BUG_ON(strlen(name) >= sizeof(id.name)))
1436 strcpy(id.name, name);
1437 return snd_ctl_find_id(codec->bus->card, &id);
1440 struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
1443 return _snd_hda_find_mixer_ctl(codec, name, 0);
1445 EXPORT_SYMBOL_HDA(snd_hda_find_mixer_ctl);
1447 /* Add a control element and assign to the codec */
1448 int snd_hda_ctl_add(struct hda_codec *codec, struct snd_kcontrol *kctl)
1451 struct snd_kcontrol **knewp;
1453 err = snd_ctl_add(codec->bus->card, kctl);
1456 knewp = snd_array_new(&codec->mixers);
1462 EXPORT_SYMBOL_HDA(snd_hda_ctl_add);
1464 /* Clear all controls assigned to the given codec */
1465 void snd_hda_ctls_clear(struct hda_codec *codec)
1468 struct snd_kcontrol **kctls = codec->mixers.list;
1469 for (i = 0; i < codec->mixers.used; i++)
1470 snd_ctl_remove(codec->bus->card, kctls[i]);
1471 snd_array_free(&codec->mixers);
1474 /* pseudo device locking
1475 * toggle card->shutdown to allow/disallow the device access (as a hack)
1477 static int hda_lock_devices(struct snd_card *card)
1479 spin_lock(&card->files_lock);
1480 if (card->shutdown) {
1481 spin_unlock(&card->files_lock);
1485 spin_unlock(&card->files_lock);
1489 static void hda_unlock_devices(struct snd_card *card)
1491 spin_lock(&card->files_lock);
1493 spin_unlock(&card->files_lock);
1496 int snd_hda_codec_reset(struct hda_codec *codec)
1498 struct snd_card *card = codec->bus->card;
1501 if (hda_lock_devices(card) < 0)
1503 /* check whether the codec isn't used by any mixer or PCM streams */
1504 if (!list_empty(&card->ctl_files)) {
1505 hda_unlock_devices(card);
1508 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
1509 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
1512 if (cpcm->pcm->streams[0].substream_opened ||
1513 cpcm->pcm->streams[1].substream_opened) {
1514 hda_unlock_devices(card);
1519 /* OK, let it free */
1521 #ifdef CONFIG_SND_HDA_POWER_SAVE
1522 cancel_delayed_work(&codec->power_work);
1523 flush_workqueue(codec->bus->workq);
1525 snd_hda_ctls_clear(codec);
1527 for (i = 0; i < codec->num_pcms; i++) {
1528 if (codec->pcm_info[i].pcm) {
1529 snd_device_free(card, codec->pcm_info[i].pcm);
1530 clear_bit(codec->pcm_info[i].device,
1531 codec->bus->pcm_dev_bits);
1534 if (codec->patch_ops.free)
1535 codec->patch_ops.free(codec);
1536 codec->proc_widget_hook = NULL;
1538 free_hda_cache(&codec->amp_cache);
1539 free_hda_cache(&codec->cmd_cache);
1540 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
1541 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
1542 /* free only driver_pins so that init_pins + user_pins are restored */
1543 snd_array_free(&codec->driver_pins);
1544 restore_pincfgs(codec);
1545 codec->num_pcms = 0;
1546 codec->pcm_info = NULL;
1547 codec->preset = NULL;
1548 memset(&codec->patch_ops, 0, sizeof(codec->patch_ops));
1549 codec->slave_dig_outs = NULL;
1550 codec->spdif_status_reset = 0;
1551 module_put(codec->owner);
1552 codec->owner = NULL;
1554 /* allow device access again */
1555 hda_unlock_devices(card);
1559 /* create a virtual master control and add slaves */
1560 int snd_hda_add_vmaster(struct hda_codec *codec, char *name,
1561 unsigned int *tlv, const char **slaves)
1563 struct snd_kcontrol *kctl;
1567 for (s = slaves; *s && !snd_hda_find_mixer_ctl(codec, *s); s++)
1570 snd_printdd("No slave found for %s\n", name);
1573 kctl = snd_ctl_make_virtual_master(name, tlv);
1576 err = snd_hda_ctl_add(codec, kctl);
1580 for (s = slaves; *s; s++) {
1581 struct snd_kcontrol *sctl;
1584 sctl = _snd_hda_find_mixer_ctl(codec, *s, i);
1587 snd_printdd("Cannot find slave %s, "
1591 err = snd_ctl_add_slave(kctl, sctl);
1599 EXPORT_SYMBOL_HDA(snd_hda_add_vmaster);
1602 int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
1603 struct snd_ctl_elem_info *uinfo)
1605 int chs = get_amp_channels(kcontrol);
1607 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1608 uinfo->count = chs == 3 ? 2 : 1;
1609 uinfo->value.integer.min = 0;
1610 uinfo->value.integer.max = 1;
1613 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_info);
1615 int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
1616 struct snd_ctl_elem_value *ucontrol)
1618 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1619 hda_nid_t nid = get_amp_nid(kcontrol);
1620 int chs = get_amp_channels(kcontrol);
1621 int dir = get_amp_direction(kcontrol);
1622 int idx = get_amp_index(kcontrol);
1623 long *valp = ucontrol->value.integer.value;
1626 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
1627 HDA_AMP_MUTE) ? 0 : 1;
1629 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
1630 HDA_AMP_MUTE) ? 0 : 1;
1633 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_get);
1635 int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
1636 struct snd_ctl_elem_value *ucontrol)
1638 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1639 hda_nid_t nid = get_amp_nid(kcontrol);
1640 int chs = get_amp_channels(kcontrol);
1641 int dir = get_amp_direction(kcontrol);
1642 int idx = get_amp_index(kcontrol);
1643 long *valp = ucontrol->value.integer.value;
1646 snd_hda_power_up(codec);
1648 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
1650 *valp ? 0 : HDA_AMP_MUTE);
1654 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
1656 *valp ? 0 : HDA_AMP_MUTE);
1657 #ifdef CONFIG_SND_HDA_POWER_SAVE
1658 if (codec->patch_ops.check_power_status)
1659 codec->patch_ops.check_power_status(codec, nid);
1661 snd_hda_power_down(codec);
1664 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put);
1667 * bound volume controls
1669 * bind multiple volumes (# indices, from 0)
1672 #define AMP_VAL_IDX_SHIFT 19
1673 #define AMP_VAL_IDX_MASK (0x0f<<19)
1675 int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
1676 struct snd_ctl_elem_value *ucontrol)
1678 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1682 mutex_lock(&codec->control_mutex);
1683 pval = kcontrol->private_value;
1684 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
1685 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
1686 kcontrol->private_value = pval;
1687 mutex_unlock(&codec->control_mutex);
1690 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_get);
1692 int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
1693 struct snd_ctl_elem_value *ucontrol)
1695 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1697 int i, indices, err = 0, change = 0;
1699 mutex_lock(&codec->control_mutex);
1700 pval = kcontrol->private_value;
1701 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
1702 for (i = 0; i < indices; i++) {
1703 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
1704 (i << AMP_VAL_IDX_SHIFT);
1705 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
1710 kcontrol->private_value = pval;
1711 mutex_unlock(&codec->control_mutex);
1712 return err < 0 ? err : change;
1714 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_put);
1717 * generic bound volume/swtich controls
1719 int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
1720 struct snd_ctl_elem_info *uinfo)
1722 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1723 struct hda_bind_ctls *c;
1726 mutex_lock(&codec->control_mutex);
1727 c = (struct hda_bind_ctls *)kcontrol->private_value;
1728 kcontrol->private_value = *c->values;
1729 err = c->ops->info(kcontrol, uinfo);
1730 kcontrol->private_value = (long)c;
1731 mutex_unlock(&codec->control_mutex);
1734 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_info);
1736 int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
1737 struct snd_ctl_elem_value *ucontrol)
1739 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1740 struct hda_bind_ctls *c;
1743 mutex_lock(&codec->control_mutex);
1744 c = (struct hda_bind_ctls *)kcontrol->private_value;
1745 kcontrol->private_value = *c->values;
1746 err = c->ops->get(kcontrol, ucontrol);
1747 kcontrol->private_value = (long)c;
1748 mutex_unlock(&codec->control_mutex);
1751 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_get);
1753 int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
1754 struct snd_ctl_elem_value *ucontrol)
1756 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1757 struct hda_bind_ctls *c;
1758 unsigned long *vals;
1759 int err = 0, change = 0;
1761 mutex_lock(&codec->control_mutex);
1762 c = (struct hda_bind_ctls *)kcontrol->private_value;
1763 for (vals = c->values; *vals; vals++) {
1764 kcontrol->private_value = *vals;
1765 err = c->ops->put(kcontrol, ucontrol);
1770 kcontrol->private_value = (long)c;
1771 mutex_unlock(&codec->control_mutex);
1772 return err < 0 ? err : change;
1774 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_put);
1776 int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
1777 unsigned int size, unsigned int __user *tlv)
1779 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1780 struct hda_bind_ctls *c;
1783 mutex_lock(&codec->control_mutex);
1784 c = (struct hda_bind_ctls *)kcontrol->private_value;
1785 kcontrol->private_value = *c->values;
1786 err = c->ops->tlv(kcontrol, op_flag, size, tlv);
1787 kcontrol->private_value = (long)c;
1788 mutex_unlock(&codec->control_mutex);
1791 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_tlv);
1793 struct hda_ctl_ops snd_hda_bind_vol = {
1794 .info = snd_hda_mixer_amp_volume_info,
1795 .get = snd_hda_mixer_amp_volume_get,
1796 .put = snd_hda_mixer_amp_volume_put,
1797 .tlv = snd_hda_mixer_amp_tlv
1799 EXPORT_SYMBOL_HDA(snd_hda_bind_vol);
1801 struct hda_ctl_ops snd_hda_bind_sw = {
1802 .info = snd_hda_mixer_amp_switch_info,
1803 .get = snd_hda_mixer_amp_switch_get,
1804 .put = snd_hda_mixer_amp_switch_put,
1805 .tlv = snd_hda_mixer_amp_tlv
1807 EXPORT_SYMBOL_HDA(snd_hda_bind_sw);
1810 * SPDIF out controls
1813 static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
1814 struct snd_ctl_elem_info *uinfo)
1816 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1821 static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
1822 struct snd_ctl_elem_value *ucontrol)
1824 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
1825 IEC958_AES0_NONAUDIO |
1826 IEC958_AES0_CON_EMPHASIS_5015 |
1827 IEC958_AES0_CON_NOT_COPYRIGHT;
1828 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
1829 IEC958_AES1_CON_ORIGINAL;
1833 static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
1834 struct snd_ctl_elem_value *ucontrol)
1836 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
1837 IEC958_AES0_NONAUDIO |
1838 IEC958_AES0_PRO_EMPHASIS_5015;
1842 static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
1843 struct snd_ctl_elem_value *ucontrol)
1845 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1847 ucontrol->value.iec958.status[0] = codec->spdif_status & 0xff;
1848 ucontrol->value.iec958.status[1] = (codec->spdif_status >> 8) & 0xff;
1849 ucontrol->value.iec958.status[2] = (codec->spdif_status >> 16) & 0xff;
1850 ucontrol->value.iec958.status[3] = (codec->spdif_status >> 24) & 0xff;
1855 /* convert from SPDIF status bits to HDA SPDIF bits
1856 * bit 0 (DigEn) is always set zero (to be filled later)
1858 static unsigned short convert_from_spdif_status(unsigned int sbits)
1860 unsigned short val = 0;
1862 if (sbits & IEC958_AES0_PROFESSIONAL)
1863 val |= AC_DIG1_PROFESSIONAL;
1864 if (sbits & IEC958_AES0_NONAUDIO)
1865 val |= AC_DIG1_NONAUDIO;
1866 if (sbits & IEC958_AES0_PROFESSIONAL) {
1867 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
1868 IEC958_AES0_PRO_EMPHASIS_5015)
1869 val |= AC_DIG1_EMPHASIS;
1871 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
1872 IEC958_AES0_CON_EMPHASIS_5015)
1873 val |= AC_DIG1_EMPHASIS;
1874 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
1875 val |= AC_DIG1_COPYRIGHT;
1876 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
1877 val |= AC_DIG1_LEVEL;
1878 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
1883 /* convert to SPDIF status bits from HDA SPDIF bits
1885 static unsigned int convert_to_spdif_status(unsigned short val)
1887 unsigned int sbits = 0;
1889 if (val & AC_DIG1_NONAUDIO)
1890 sbits |= IEC958_AES0_NONAUDIO;
1891 if (val & AC_DIG1_PROFESSIONAL)
1892 sbits |= IEC958_AES0_PROFESSIONAL;
1893 if (sbits & IEC958_AES0_PROFESSIONAL) {
1894 if (sbits & AC_DIG1_EMPHASIS)
1895 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
1897 if (val & AC_DIG1_EMPHASIS)
1898 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
1899 if (!(val & AC_DIG1_COPYRIGHT))
1900 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
1901 if (val & AC_DIG1_LEVEL)
1902 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
1903 sbits |= val & (0x7f << 8);
1908 /* set digital convert verbs both for the given NID and its slaves */
1909 static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
1914 snd_hda_codec_write_cache(codec, nid, 0, verb, val);
1915 d = codec->slave_dig_outs;
1919 snd_hda_codec_write_cache(codec, *d, 0, verb, val);
1922 static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
1926 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1);
1928 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2);
1931 static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
1932 struct snd_ctl_elem_value *ucontrol)
1934 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1935 hda_nid_t nid = kcontrol->private_value;
1939 mutex_lock(&codec->spdif_mutex);
1940 codec->spdif_status = ucontrol->value.iec958.status[0] |
1941 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
1942 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
1943 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
1944 val = convert_from_spdif_status(codec->spdif_status);
1945 val |= codec->spdif_ctls & 1;
1946 change = codec->spdif_ctls != val;
1947 codec->spdif_ctls = val;
1950 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
1952 mutex_unlock(&codec->spdif_mutex);
1956 #define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
1958 static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
1959 struct snd_ctl_elem_value *ucontrol)
1961 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1963 ucontrol->value.integer.value[0] = codec->spdif_ctls & AC_DIG1_ENABLE;
1967 static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
1968 struct snd_ctl_elem_value *ucontrol)
1970 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1971 hda_nid_t nid = kcontrol->private_value;
1975 mutex_lock(&codec->spdif_mutex);
1976 val = codec->spdif_ctls & ~AC_DIG1_ENABLE;
1977 if (ucontrol->value.integer.value[0])
1978 val |= AC_DIG1_ENABLE;
1979 change = codec->spdif_ctls != val;
1981 codec->spdif_ctls = val;
1982 set_dig_out_convert(codec, nid, val & 0xff, -1);
1983 /* unmute amp switch (if any) */
1984 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
1985 (val & AC_DIG1_ENABLE))
1986 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
1989 mutex_unlock(&codec->spdif_mutex);
1993 static struct snd_kcontrol_new dig_mixes[] = {
1995 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1996 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1997 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,CON_MASK),
1998 .info = snd_hda_spdif_mask_info,
1999 .get = snd_hda_spdif_cmask_get,
2002 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2003 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2004 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,PRO_MASK),
2005 .info = snd_hda_spdif_mask_info,
2006 .get = snd_hda_spdif_pmask_get,
2009 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2010 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
2011 .info = snd_hda_spdif_mask_info,
2012 .get = snd_hda_spdif_default_get,
2013 .put = snd_hda_spdif_default_put,
2016 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2017 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,SWITCH),
2018 .info = snd_hda_spdif_out_switch_info,
2019 .get = snd_hda_spdif_out_switch_get,
2020 .put = snd_hda_spdif_out_switch_put,
2025 #define SPDIF_MAX_IDX 4 /* 4 instances should be enough to probe */
2028 * snd_hda_create_spdif_out_ctls - create Output SPDIF-related controls
2029 * @codec: the HDA codec
2030 * @nid: audio out widget NID
2032 * Creates controls related with the SPDIF output.
2033 * Called from each patch supporting the SPDIF out.
2035 * Returns 0 if successful, or a negative error code.
2037 int snd_hda_create_spdif_out_ctls(struct hda_codec *codec, hda_nid_t nid)
2040 struct snd_kcontrol *kctl;
2041 struct snd_kcontrol_new *dig_mix;
2044 for (idx = 0; idx < SPDIF_MAX_IDX; idx++) {
2045 if (!_snd_hda_find_mixer_ctl(codec, "IEC958 Playback Switch",
2049 if (idx >= SPDIF_MAX_IDX) {
2050 printk(KERN_ERR "hda_codec: too many IEC958 outputs\n");
2053 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
2054 kctl = snd_ctl_new1(dig_mix, codec);
2057 kctl->id.index = idx;
2058 kctl->private_value = nid;
2059 err = snd_hda_ctl_add(codec, kctl);
2064 snd_hda_codec_read(codec, nid, 0,
2065 AC_VERB_GET_DIGI_CONVERT_1, 0);
2066 codec->spdif_status = convert_to_spdif_status(codec->spdif_ctls);
2069 EXPORT_SYMBOL_HDA(snd_hda_create_spdif_out_ctls);
2072 * SPDIF sharing with analog output
2074 static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
2075 struct snd_ctl_elem_value *ucontrol)
2077 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
2078 ucontrol->value.integer.value[0] = mout->share_spdif;
2082 static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
2083 struct snd_ctl_elem_value *ucontrol)
2085 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
2086 mout->share_spdif = !!ucontrol->value.integer.value[0];
2090 static struct snd_kcontrol_new spdif_share_sw = {
2091 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2092 .name = "IEC958 Default PCM Playback Switch",
2093 .info = snd_ctl_boolean_mono_info,
2094 .get = spdif_share_sw_get,
2095 .put = spdif_share_sw_put,
2098 int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
2099 struct hda_multi_out *mout)
2101 if (!mout->dig_out_nid)
2103 /* ATTENTION: here mout is passed as private_data, instead of codec */
2104 return snd_hda_ctl_add(codec,
2105 snd_ctl_new1(&spdif_share_sw, mout));
2107 EXPORT_SYMBOL_HDA(snd_hda_create_spdif_share_sw);
2113 #define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
2115 static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
2116 struct snd_ctl_elem_value *ucontrol)
2118 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2120 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
2124 static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
2125 struct snd_ctl_elem_value *ucontrol)
2127 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2128 hda_nid_t nid = kcontrol->private_value;
2129 unsigned int val = !!ucontrol->value.integer.value[0];
2132 mutex_lock(&codec->spdif_mutex);
2133 change = codec->spdif_in_enable != val;
2135 codec->spdif_in_enable = val;
2136 snd_hda_codec_write_cache(codec, nid, 0,
2137 AC_VERB_SET_DIGI_CONVERT_1, val);
2139 mutex_unlock(&codec->spdif_mutex);
2143 static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
2144 struct snd_ctl_elem_value *ucontrol)
2146 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2147 hda_nid_t nid = kcontrol->private_value;
2151 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0);
2152 sbits = convert_to_spdif_status(val);
2153 ucontrol->value.iec958.status[0] = sbits;
2154 ucontrol->value.iec958.status[1] = sbits >> 8;
2155 ucontrol->value.iec958.status[2] = sbits >> 16;
2156 ucontrol->value.iec958.status[3] = sbits >> 24;
2160 static struct snd_kcontrol_new dig_in_ctls[] = {
2162 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2163 .name = SNDRV_CTL_NAME_IEC958("",CAPTURE,SWITCH),
2164 .info = snd_hda_spdif_in_switch_info,
2165 .get = snd_hda_spdif_in_switch_get,
2166 .put = snd_hda_spdif_in_switch_put,
2169 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2170 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2171 .name = SNDRV_CTL_NAME_IEC958("",CAPTURE,DEFAULT),
2172 .info = snd_hda_spdif_mask_info,
2173 .get = snd_hda_spdif_in_status_get,
2179 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
2180 * @codec: the HDA codec
2181 * @nid: audio in widget NID
2183 * Creates controls related with the SPDIF input.
2184 * Called from each patch supporting the SPDIF in.
2186 * Returns 0 if successful, or a negative error code.
2188 int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
2191 struct snd_kcontrol *kctl;
2192 struct snd_kcontrol_new *dig_mix;
2195 for (idx = 0; idx < SPDIF_MAX_IDX; idx++) {
2196 if (!_snd_hda_find_mixer_ctl(codec, "IEC958 Capture Switch",
2200 if (idx >= SPDIF_MAX_IDX) {
2201 printk(KERN_ERR "hda_codec: too many IEC958 inputs\n");
2204 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
2205 kctl = snd_ctl_new1(dig_mix, codec);
2208 kctl->private_value = nid;
2209 err = snd_hda_ctl_add(codec, kctl);
2213 codec->spdif_in_enable =
2214 snd_hda_codec_read(codec, nid, 0,
2215 AC_VERB_GET_DIGI_CONVERT_1, 0) &
2219 EXPORT_SYMBOL_HDA(snd_hda_create_spdif_in_ctls);
2221 #ifdef SND_HDA_NEEDS_RESUME
2226 /* build a 32bit cache key with the widget id and the command parameter */
2227 #define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
2228 #define get_cmd_cache_nid(key) ((key) & 0xff)
2229 #define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
2232 * snd_hda_codec_write_cache - send a single command with caching
2233 * @codec: the HDA codec
2234 * @nid: NID to send the command
2235 * @direct: direct flag
2236 * @verb: the verb to send
2237 * @parm: the parameter for the verb
2239 * Send a single command without waiting for response.
2241 * Returns 0 if successful, or a negative error code.
2243 int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
2244 int direct, unsigned int verb, unsigned int parm)
2246 struct hda_bus *bus = codec->bus;
2250 res = make_codec_cmd(codec, nid, direct, verb, parm);
2251 snd_hda_power_up(codec);
2252 mutex_lock(&bus->cmd_mutex);
2253 err = bus->ops.command(bus, res);
2255 struct hda_cache_head *c;
2257 /* parm may contain the verb stuff for get/set amp */
2258 verb = verb | (parm >> 8);
2260 key = build_cmd_cache_key(nid, verb);
2261 c = get_alloc_hash(&codec->cmd_cache, key);
2265 mutex_unlock(&bus->cmd_mutex);
2266 snd_hda_power_down(codec);
2269 EXPORT_SYMBOL_HDA(snd_hda_codec_write_cache);
2271 /* resume the all commands from the cache */
2272 void snd_hda_codec_resume_cache(struct hda_codec *codec)
2274 struct hda_cache_head *buffer = codec->cmd_cache.buf.list;
2277 for (i = 0; i < codec->cmd_cache.buf.used; i++, buffer++) {
2278 u32 key = buffer->key;
2281 snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
2282 get_cmd_cache_cmd(key), buffer->val);
2285 EXPORT_SYMBOL_HDA(snd_hda_codec_resume_cache);
2288 * snd_hda_sequence_write_cache - sequence writes with caching
2289 * @codec: the HDA codec
2290 * @seq: VERB array to send
2292 * Send the commands sequentially from the given array.
2293 * Thte commands are recorded on cache for power-save and resume.
2294 * The array must be terminated with NID=0.
2296 void snd_hda_sequence_write_cache(struct hda_codec *codec,
2297 const struct hda_verb *seq)
2299 for (; seq->nid; seq++)
2300 snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
2303 EXPORT_SYMBOL_HDA(snd_hda_sequence_write_cache);
2304 #endif /* SND_HDA_NEEDS_RESUME */
2307 * set power state of the codec
2309 static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
2310 unsigned int power_state)
2315 snd_hda_codec_write(codec, fg, 0, AC_VERB_SET_POWER_STATE,
2317 msleep(10); /* partial workaround for "azx_get_response timeout" */
2319 nid = codec->start_nid;
2320 for (i = 0; i < codec->num_nodes; i++, nid++) {
2321 unsigned int wcaps = get_wcaps(codec, nid);
2322 if (wcaps & AC_WCAP_POWER) {
2323 unsigned int wid_type = (wcaps & AC_WCAP_TYPE) >>
2325 if (wid_type == AC_WID_PIN) {
2326 unsigned int pincap;
2328 * don't power down the widget if it controls
2329 * eapd and EAPD_BTLENABLE is set.
2331 pincap = snd_hda_query_pin_caps(codec, nid);
2332 if (pincap & AC_PINCAP_EAPD) {
2333 int eapd = snd_hda_codec_read(codec,
2335 AC_VERB_GET_EAPD_BTLENABLE, 0);
2337 if (power_state == AC_PWRST_D3 && eapd)
2341 snd_hda_codec_write(codec, nid, 0,
2342 AC_VERB_SET_POWER_STATE,
2347 if (power_state == AC_PWRST_D0) {
2348 unsigned long end_time;
2351 /* wait until the codec reachs to D0 */
2352 end_time = jiffies + msecs_to_jiffies(500);
2354 state = snd_hda_codec_read(codec, fg, 0,
2355 AC_VERB_GET_POWER_STATE, 0);
2356 if (state == power_state)
2359 } while (time_after_eq(end_time, jiffies));
2363 #ifdef CONFIG_SND_HDA_HWDEP
2364 /* execute additional init verbs */
2365 static void hda_exec_init_verbs(struct hda_codec *codec)
2367 if (codec->init_verbs.list)
2368 snd_hda_sequence_write(codec, codec->init_verbs.list);
2371 static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
2374 #ifdef SND_HDA_NEEDS_RESUME
2376 * call suspend and power-down; used both from PM and power-save
2378 static void hda_call_codec_suspend(struct hda_codec *codec)
2380 if (codec->patch_ops.suspend)
2381 codec->patch_ops.suspend(codec, PMSG_SUSPEND);
2382 hda_set_power_state(codec,
2383 codec->afg ? codec->afg : codec->mfg,
2385 #ifdef CONFIG_SND_HDA_POWER_SAVE
2386 cancel_delayed_work(&codec->power_work);
2387 codec->power_on = 0;
2388 codec->power_transition = 0;
2393 * kick up codec; used both from PM and power-save
2395 static void hda_call_codec_resume(struct hda_codec *codec)
2397 hda_set_power_state(codec,
2398 codec->afg ? codec->afg : codec->mfg,
2400 restore_pincfgs(codec); /* restore all current pin configs */
2401 hda_exec_init_verbs(codec);
2402 if (codec->patch_ops.resume)
2403 codec->patch_ops.resume(codec);
2405 if (codec->patch_ops.init)
2406 codec->patch_ops.init(codec);
2407 snd_hda_codec_resume_amp(codec);
2408 snd_hda_codec_resume_cache(codec);
2411 #endif /* SND_HDA_NEEDS_RESUME */
2415 * snd_hda_build_controls - build mixer controls
2418 * Creates mixer controls for each codec included in the bus.
2420 * Returns 0 if successful, otherwise a negative error code.
2422 int /*__devinit*/ snd_hda_build_controls(struct hda_bus *bus)
2424 struct hda_codec *codec;
2426 list_for_each_entry(codec, &bus->codec_list, list) {
2427 int err = snd_hda_codec_build_controls(codec);
2429 printk(KERN_ERR "hda_codec: cannot build controls"
2430 "for #%d (error %d)\n", codec->addr, err);
2431 err = snd_hda_codec_reset(codec);
2434 "hda_codec: cannot revert codec\n");
2441 EXPORT_SYMBOL_HDA(snd_hda_build_controls);
2443 int snd_hda_codec_build_controls(struct hda_codec *codec)
2446 hda_exec_init_verbs(codec);
2447 /* continue to initialize... */
2448 if (codec->patch_ops.init)
2449 err = codec->patch_ops.init(codec);
2450 if (!err && codec->patch_ops.build_controls)
2451 err = codec->patch_ops.build_controls(codec);
2460 struct hda_rate_tbl {
2462 unsigned int alsa_bits;
2463 unsigned int hda_fmt;
2466 static struct hda_rate_tbl rate_bits[] = {
2467 /* rate in Hz, ALSA rate bitmask, HDA format value */
2469 /* autodetected value used in snd_hda_query_supported_pcm */
2470 { 8000, SNDRV_PCM_RATE_8000, 0x0500 }, /* 1/6 x 48 */
2471 { 11025, SNDRV_PCM_RATE_11025, 0x4300 }, /* 1/4 x 44 */
2472 { 16000, SNDRV_PCM_RATE_16000, 0x0200 }, /* 1/3 x 48 */
2473 { 22050, SNDRV_PCM_RATE_22050, 0x4100 }, /* 1/2 x 44 */
2474 { 32000, SNDRV_PCM_RATE_32000, 0x0a00 }, /* 2/3 x 48 */
2475 { 44100, SNDRV_PCM_RATE_44100, 0x4000 }, /* 44 */
2476 { 48000, SNDRV_PCM_RATE_48000, 0x0000 }, /* 48 */
2477 { 88200, SNDRV_PCM_RATE_88200, 0x4800 }, /* 2 x 44 */
2478 { 96000, SNDRV_PCM_RATE_96000, 0x0800 }, /* 2 x 48 */
2479 { 176400, SNDRV_PCM_RATE_176400, 0x5800 },/* 4 x 44 */
2480 { 192000, SNDRV_PCM_RATE_192000, 0x1800 }, /* 4 x 48 */
2481 #define AC_PAR_PCM_RATE_BITS 11
2482 /* up to bits 10, 384kHZ isn't supported properly */
2484 /* not autodetected value */
2485 { 9600, SNDRV_PCM_RATE_KNOT, 0x0400 }, /* 1/5 x 48 */
2487 { 0 } /* terminator */
2491 * snd_hda_calc_stream_format - calculate format bitset
2492 * @rate: the sample rate
2493 * @channels: the number of channels
2494 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
2495 * @maxbps: the max. bps
2497 * Calculate the format bitset from the given rate, channels and th PCM format.
2499 * Return zero if invalid.
2501 unsigned int snd_hda_calc_stream_format(unsigned int rate,
2502 unsigned int channels,
2503 unsigned int format,
2504 unsigned int maxbps)
2507 unsigned int val = 0;
2509 for (i = 0; rate_bits[i].hz; i++)
2510 if (rate_bits[i].hz == rate) {
2511 val = rate_bits[i].hda_fmt;
2514 if (!rate_bits[i].hz) {
2515 snd_printdd("invalid rate %d\n", rate);
2519 if (channels == 0 || channels > 8) {
2520 snd_printdd("invalid channels %d\n", channels);
2523 val |= channels - 1;
2525 switch (snd_pcm_format_width(format)) {
2526 case 8: val |= 0x00; break;
2527 case 16: val |= 0x10; break;
2533 else if (maxbps >= 24)
2539 snd_printdd("invalid format width %d\n",
2540 snd_pcm_format_width(format));
2546 EXPORT_SYMBOL_HDA(snd_hda_calc_stream_format);
2549 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
2550 * @codec: the HDA codec
2551 * @nid: NID to query
2552 * @ratesp: the pointer to store the detected rate bitflags
2553 * @formatsp: the pointer to store the detected formats
2554 * @bpsp: the pointer to store the detected format widths
2556 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
2557 * or @bsps argument is ignored.
2559 * Returns 0 if successful, otherwise a negative error code.
2561 static int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
2562 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
2564 unsigned int i, val, wcaps;
2567 wcaps = get_wcaps(codec, nid);
2568 if (nid != codec->afg && (wcaps & AC_WCAP_FORMAT_OVRD)) {
2569 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
2574 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
2578 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
2580 rates |= rate_bits[i].alsa_bits;
2583 snd_printk(KERN_ERR "hda_codec: rates == 0 "
2584 "(nid=0x%x, val=0x%x, ovrd=%i)\n",
2586 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0);
2592 if (formatsp || bpsp) {
2594 unsigned int streams, bps;
2596 streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
2600 streams = snd_hda_param_read(codec, codec->afg,
2607 if (streams & AC_SUPFMT_PCM) {
2608 if (val & AC_SUPPCM_BITS_8) {
2609 formats |= SNDRV_PCM_FMTBIT_U8;
2612 if (val & AC_SUPPCM_BITS_16) {
2613 formats |= SNDRV_PCM_FMTBIT_S16_LE;
2616 if (wcaps & AC_WCAP_DIGITAL) {
2617 if (val & AC_SUPPCM_BITS_32)
2618 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
2619 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
2620 formats |= SNDRV_PCM_FMTBIT_S32_LE;
2621 if (val & AC_SUPPCM_BITS_24)
2623 else if (val & AC_SUPPCM_BITS_20)
2625 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
2626 AC_SUPPCM_BITS_32)) {
2627 formats |= SNDRV_PCM_FMTBIT_S32_LE;
2628 if (val & AC_SUPPCM_BITS_32)
2630 else if (val & AC_SUPPCM_BITS_24)
2632 else if (val & AC_SUPPCM_BITS_20)
2636 else if (streams == AC_SUPFMT_FLOAT32) {
2637 /* should be exclusive */
2638 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
2640 } else if (streams == AC_SUPFMT_AC3) {
2641 /* should be exclusive */
2642 /* temporary hack: we have still no proper support
2643 * for the direct AC3 stream...
2645 formats |= SNDRV_PCM_FMTBIT_U8;
2649 snd_printk(KERN_ERR "hda_codec: formats == 0 "
2650 "(nid=0x%x, val=0x%x, ovrd=%i, "
2653 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0,
2658 *formatsp = formats;
2667 * snd_hda_is_supported_format - check whether the given node supports
2670 * Returns 1 if supported, 0 if not.
2672 int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
2673 unsigned int format)
2676 unsigned int val = 0, rate, stream;
2678 if (nid != codec->afg &&
2679 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD)) {
2680 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
2685 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
2690 rate = format & 0xff00;
2691 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
2692 if (rate_bits[i].hda_fmt == rate) {
2697 if (i >= AC_PAR_PCM_RATE_BITS)
2700 stream = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
2703 if (!stream && nid != codec->afg)
2704 stream = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
2705 if (!stream || stream == -1)
2708 if (stream & AC_SUPFMT_PCM) {
2709 switch (format & 0xf0) {
2711 if (!(val & AC_SUPPCM_BITS_8))
2715 if (!(val & AC_SUPPCM_BITS_16))
2719 if (!(val & AC_SUPPCM_BITS_20))
2723 if (!(val & AC_SUPPCM_BITS_24))
2727 if (!(val & AC_SUPPCM_BITS_32))
2734 /* FIXME: check for float32 and AC3? */
2739 EXPORT_SYMBOL_HDA(snd_hda_is_supported_format);
2744 static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
2745 struct hda_codec *codec,
2746 struct snd_pcm_substream *substream)
2751 static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
2752 struct hda_codec *codec,
2753 unsigned int stream_tag,
2754 unsigned int format,
2755 struct snd_pcm_substream *substream)
2757 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
2761 static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
2762 struct hda_codec *codec,
2763 struct snd_pcm_substream *substream)
2765 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
2769 static int set_pcm_default_values(struct hda_codec *codec,
2770 struct hda_pcm_stream *info)
2774 /* query support PCM information from the given NID */
2775 if (info->nid && (!info->rates || !info->formats)) {
2776 err = snd_hda_query_supported_pcm(codec, info->nid,
2777 info->rates ? NULL : &info->rates,
2778 info->formats ? NULL : &info->formats,
2779 info->maxbps ? NULL : &info->maxbps);
2783 if (info->ops.open == NULL)
2784 info->ops.open = hda_pcm_default_open_close;
2785 if (info->ops.close == NULL)
2786 info->ops.close = hda_pcm_default_open_close;
2787 if (info->ops.prepare == NULL) {
2788 if (snd_BUG_ON(!info->nid))
2790 info->ops.prepare = hda_pcm_default_prepare;
2792 if (info->ops.cleanup == NULL) {
2793 if (snd_BUG_ON(!info->nid))
2795 info->ops.cleanup = hda_pcm_default_cleanup;
2801 * get the empty PCM device number to assign
2803 static int get_empty_pcm_device(struct hda_bus *bus, int type)
2805 static const char *dev_name[HDA_PCM_NTYPES] = {
2806 "Audio", "SPDIF", "HDMI", "Modem"
2808 /* starting device index for each PCM type */
2809 static int dev_idx[HDA_PCM_NTYPES] = {
2810 [HDA_PCM_TYPE_AUDIO] = 0,
2811 [HDA_PCM_TYPE_SPDIF] = 1,
2812 [HDA_PCM_TYPE_HDMI] = 3,
2813 [HDA_PCM_TYPE_MODEM] = 6
2815 /* normal audio device indices; not linear to keep compatibility */
2816 static int audio_idx[4] = { 0, 2, 4, 5 };
2820 case HDA_PCM_TYPE_AUDIO:
2821 for (i = 0; i < ARRAY_SIZE(audio_idx); i++) {
2823 if (!test_bit(dev, bus->pcm_dev_bits))
2826 snd_printk(KERN_WARNING "Too many audio devices\n");
2828 case HDA_PCM_TYPE_SPDIF:
2829 case HDA_PCM_TYPE_HDMI:
2830 case HDA_PCM_TYPE_MODEM:
2831 dev = dev_idx[type];
2832 if (test_bit(dev, bus->pcm_dev_bits)) {
2833 snd_printk(KERN_WARNING "%s already defined\n",
2839 snd_printk(KERN_WARNING "Invalid PCM type %d\n", type);
2843 set_bit(dev, bus->pcm_dev_bits);
2848 * attach a new PCM stream
2850 static int snd_hda_attach_pcm(struct hda_codec *codec, struct hda_pcm *pcm)
2852 struct hda_bus *bus = codec->bus;
2853 struct hda_pcm_stream *info;
2856 if (snd_BUG_ON(!pcm->name))
2858 for (stream = 0; stream < 2; stream++) {
2859 info = &pcm->stream[stream];
2860 if (info->substreams) {
2861 err = set_pcm_default_values(codec, info);
2866 return bus->ops.attach_pcm(bus, codec, pcm);
2869 /* assign all PCMs of the given codec */
2870 int snd_hda_codec_build_pcms(struct hda_codec *codec)
2875 if (!codec->num_pcms) {
2876 if (!codec->patch_ops.build_pcms)
2878 err = codec->patch_ops.build_pcms(codec);
2880 printk(KERN_ERR "hda_codec: cannot build PCMs"
2881 "for #%d (error %d)\n", codec->addr, err);
2882 err = snd_hda_codec_reset(codec);
2885 "hda_codec: cannot revert codec\n");
2890 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
2891 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
2894 if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
2895 continue; /* no substreams assigned */
2898 dev = get_empty_pcm_device(codec->bus, cpcm->pcm_type);
2900 continue; /* no fatal error */
2902 err = snd_hda_attach_pcm(codec, cpcm);
2904 printk(KERN_ERR "hda_codec: cannot attach "
2905 "PCM stream %d for codec #%d\n",
2907 continue; /* no fatal error */
2915 * snd_hda_build_pcms - build PCM information
2918 * Create PCM information for each codec included in the bus.
2920 * The build_pcms codec patch is requested to set up codec->num_pcms and
2921 * codec->pcm_info properly. The array is referred by the top-level driver
2922 * to create its PCM instances.
2923 * The allocated codec->pcm_info should be released in codec->patch_ops.free
2926 * At least, substreams, channels_min and channels_max must be filled for
2927 * each stream. substreams = 0 indicates that the stream doesn't exist.
2928 * When rates and/or formats are zero, the supported values are queried
2929 * from the given nid. The nid is used also by the default ops.prepare
2930 * and ops.cleanup callbacks.
2932 * The driver needs to call ops.open in its open callback. Similarly,
2933 * ops.close is supposed to be called in the close callback.
2934 * ops.prepare should be called in the prepare or hw_params callback
2935 * with the proper parameters for set up.
2936 * ops.cleanup should be called in hw_free for clean up of streams.
2938 * This function returns 0 if successfull, or a negative error code.
2940 int __devinit snd_hda_build_pcms(struct hda_bus *bus)
2942 struct hda_codec *codec;
2944 list_for_each_entry(codec, &bus->codec_list, list) {
2945 int err = snd_hda_codec_build_pcms(codec);
2951 EXPORT_SYMBOL_HDA(snd_hda_build_pcms);
2954 * snd_hda_check_board_config - compare the current codec with the config table
2955 * @codec: the HDA codec
2956 * @num_configs: number of config enums
2957 * @models: array of model name strings
2958 * @tbl: configuration table, terminated by null entries
2960 * Compares the modelname or PCI subsystem id of the current codec with the
2961 * given configuration table. If a matching entry is found, returns its
2962 * config value (supposed to be 0 or positive).
2964 * If no entries are matching, the function returns a negative value.
2966 int snd_hda_check_board_config(struct hda_codec *codec,
2967 int num_configs, const char **models,
2968 const struct snd_pci_quirk *tbl)
2970 if (codec->modelname && models) {
2972 for (i = 0; i < num_configs; i++) {
2974 !strcmp(codec->modelname, models[i])) {
2975 snd_printd(KERN_INFO "hda_codec: model '%s' is "
2976 "selected\n", models[i]);
2982 if (!codec->bus->pci || !tbl)
2985 tbl = snd_pci_quirk_lookup(codec->bus->pci, tbl);
2988 if (tbl->value >= 0 && tbl->value < num_configs) {
2989 #ifdef CONFIG_SND_DEBUG_VERBOSE
2991 const char *model = NULL;
2993 model = models[tbl->value];
2995 sprintf(tmp, "#%d", tbl->value);
2998 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
2999 "for config %x:%x (%s)\n",
3000 model, tbl->subvendor, tbl->subdevice,
3001 (tbl->name ? tbl->name : "Unknown device"));
3007 EXPORT_SYMBOL_HDA(snd_hda_check_board_config);
3010 * snd_hda_check_board_codec_sid_config - compare the current codec
3011 subsystem ID with the
3014 This is important for Gateway notebooks with SB450 HDA Audio
3015 where the vendor ID of the PCI device is:
3016 ATI Technologies Inc SB450 HDA Audio [1002:437b]
3017 and the vendor/subvendor are found only at the codec.
3019 * @codec: the HDA codec
3020 * @num_configs: number of config enums
3021 * @models: array of model name strings
3022 * @tbl: configuration table, terminated by null entries
3024 * Compares the modelname or PCI subsystem id of the current codec with the
3025 * given configuration table. If a matching entry is found, returns its
3026 * config value (supposed to be 0 or positive).
3028 * If no entries are matching, the function returns a negative value.
3030 int snd_hda_check_board_codec_sid_config(struct hda_codec *codec,
3031 int num_configs, const char **models,
3032 const struct snd_pci_quirk *tbl)
3034 const struct snd_pci_quirk *q;
3036 /* Search for codec ID */
3037 for (q = tbl; q->subvendor; q++) {
3038 unsigned long vendorid = (q->subdevice) | (q->subvendor << 16);
3040 if (vendorid == codec->subsystem_id)
3049 if (tbl->value >= 0 && tbl->value < num_configs) {
3050 #ifdef CONFIG_SND_DEBUG_DETECT
3052 const char *model = NULL;
3054 model = models[tbl->value];
3056 sprintf(tmp, "#%d", tbl->value);
3059 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
3060 "for config %x:%x (%s)\n",
3061 model, tbl->subvendor, tbl->subdevice,
3062 (tbl->name ? tbl->name : "Unknown device"));
3068 EXPORT_SYMBOL_HDA(snd_hda_check_board_codec_sid_config);
3071 * snd_hda_add_new_ctls - create controls from the array
3072 * @codec: the HDA codec
3073 * @knew: the array of struct snd_kcontrol_new
3075 * This helper function creates and add new controls in the given array.
3076 * The array must be terminated with an empty entry as terminator.
3078 * Returns 0 if successful, or a negative error code.
3080 int snd_hda_add_new_ctls(struct hda_codec *codec, struct snd_kcontrol_new *knew)
3084 for (; knew->name; knew++) {
3085 struct snd_kcontrol *kctl;
3086 kctl = snd_ctl_new1(knew, codec);
3089 err = snd_hda_ctl_add(codec, kctl);
3093 kctl = snd_ctl_new1(knew, codec);
3096 kctl->id.device = codec->addr;
3097 err = snd_hda_ctl_add(codec, kctl);
3104 EXPORT_SYMBOL_HDA(snd_hda_add_new_ctls);
3106 #ifdef CONFIG_SND_HDA_POWER_SAVE
3107 static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
3108 unsigned int power_state);
3110 static void hda_power_work(struct work_struct *work)
3112 struct hda_codec *codec =
3113 container_of(work, struct hda_codec, power_work.work);
3114 struct hda_bus *bus = codec->bus;
3116 if (!codec->power_on || codec->power_count) {
3117 codec->power_transition = 0;
3121 hda_call_codec_suspend(codec);
3122 if (bus->ops.pm_notify)
3123 bus->ops.pm_notify(bus);
3126 static void hda_keep_power_on(struct hda_codec *codec)
3128 codec->power_count++;
3129 codec->power_on = 1;
3132 void snd_hda_power_up(struct hda_codec *codec)
3134 struct hda_bus *bus = codec->bus;
3136 codec->power_count++;
3137 if (codec->power_on || codec->power_transition)
3140 codec->power_on = 1;
3141 if (bus->ops.pm_notify)
3142 bus->ops.pm_notify(bus);
3143 hda_call_codec_resume(codec);
3144 cancel_delayed_work(&codec->power_work);
3145 codec->power_transition = 0;
3147 EXPORT_SYMBOL_HDA(snd_hda_power_up);
3149 #define power_save(codec) \
3150 ((codec)->bus->power_save ? *(codec)->bus->power_save : 0)
3152 #define power_save(codec) \
3153 ((codec)->bus->power_save ? *(codec)->bus->power_save : 0)
3155 void snd_hda_power_down(struct hda_codec *codec)
3157 --codec->power_count;
3158 if (!codec->power_on || codec->power_count || codec->power_transition)
3160 if (power_save(codec)) {
3161 codec->power_transition = 1; /* avoid reentrance */
3162 queue_delayed_work(codec->bus->workq, &codec->power_work,
3163 msecs_to_jiffies(power_save(codec) * 1000));
3166 EXPORT_SYMBOL_HDA(snd_hda_power_down);
3168 int snd_hda_check_amp_list_power(struct hda_codec *codec,
3169 struct hda_loopback_check *check,
3172 struct hda_amp_list *p;
3175 if (!check->amplist)
3177 for (p = check->amplist; p->nid; p++) {
3182 return 0; /* nothing changed */
3184 for (p = check->amplist; p->nid; p++) {
3185 for (ch = 0; ch < 2; ch++) {
3186 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
3188 if (!(v & HDA_AMP_MUTE) && v > 0) {
3189 if (!check->power_on) {
3190 check->power_on = 1;
3191 snd_hda_power_up(codec);
3197 if (check->power_on) {
3198 check->power_on = 0;
3199 snd_hda_power_down(codec);
3203 EXPORT_SYMBOL_HDA(snd_hda_check_amp_list_power);
3207 * Channel mode helper
3209 int snd_hda_ch_mode_info(struct hda_codec *codec,
3210 struct snd_ctl_elem_info *uinfo,
3211 const struct hda_channel_mode *chmode,
3214 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
3216 uinfo->value.enumerated.items = num_chmodes;
3217 if (uinfo->value.enumerated.item >= num_chmodes)
3218 uinfo->value.enumerated.item = num_chmodes - 1;
3219 sprintf(uinfo->value.enumerated.name, "%dch",
3220 chmode[uinfo->value.enumerated.item].channels);
3223 EXPORT_SYMBOL_HDA(snd_hda_ch_mode_info);
3225 int snd_hda_ch_mode_get(struct hda_codec *codec,
3226 struct snd_ctl_elem_value *ucontrol,
3227 const struct hda_channel_mode *chmode,
3233 for (i = 0; i < num_chmodes; i++) {
3234 if (max_channels == chmode[i].channels) {
3235 ucontrol->value.enumerated.item[0] = i;
3241 EXPORT_SYMBOL_HDA(snd_hda_ch_mode_get);
3243 int snd_hda_ch_mode_put(struct hda_codec *codec,
3244 struct snd_ctl_elem_value *ucontrol,
3245 const struct hda_channel_mode *chmode,
3251 mode = ucontrol->value.enumerated.item[0];
3252 if (mode >= num_chmodes)
3254 if (*max_channelsp == chmode[mode].channels)
3256 /* change the current channel setting */
3257 *max_channelsp = chmode[mode].channels;
3258 if (chmode[mode].sequence)
3259 snd_hda_sequence_write_cache(codec, chmode[mode].sequence);
3262 EXPORT_SYMBOL_HDA(snd_hda_ch_mode_put);
3267 int snd_hda_input_mux_info(const struct hda_input_mux *imux,
3268 struct snd_ctl_elem_info *uinfo)
3272 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
3274 uinfo->value.enumerated.items = imux->num_items;
3275 if (!imux->num_items)
3277 index = uinfo->value.enumerated.item;
3278 if (index >= imux->num_items)
3279 index = imux->num_items - 1;
3280 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
3283 EXPORT_SYMBOL_HDA(snd_hda_input_mux_info);
3285 int snd_hda_input_mux_put(struct hda_codec *codec,
3286 const struct hda_input_mux *imux,
3287 struct snd_ctl_elem_value *ucontrol,
3289 unsigned int *cur_val)
3293 if (!imux->num_items)
3295 idx = ucontrol->value.enumerated.item[0];
3296 if (idx >= imux->num_items)
3297 idx = imux->num_items - 1;
3298 if (*cur_val == idx)
3300 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
3301 imux->items[idx].index);
3305 EXPORT_SYMBOL_HDA(snd_hda_input_mux_put);
3309 * Multi-channel / digital-out PCM helper functions
3312 /* setup SPDIF output stream */
3313 static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
3314 unsigned int stream_tag, unsigned int format)
3316 /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */
3317 if (codec->spdif_status_reset && (codec->spdif_ctls & AC_DIG1_ENABLE))
3318 set_dig_out_convert(codec, nid,
3319 codec->spdif_ctls & ~AC_DIG1_ENABLE & 0xff,
3321 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
3322 if (codec->slave_dig_outs) {
3324 for (d = codec->slave_dig_outs; *d; d++)
3325 snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
3328 /* turn on again (if needed) */
3329 if (codec->spdif_status_reset && (codec->spdif_ctls & AC_DIG1_ENABLE))
3330 set_dig_out_convert(codec, nid,
3331 codec->spdif_ctls & 0xff, -1);
3334 static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
3336 snd_hda_codec_cleanup_stream(codec, nid);
3337 if (codec->slave_dig_outs) {
3339 for (d = codec->slave_dig_outs; *d; d++)
3340 snd_hda_codec_cleanup_stream(codec, *d);
3345 * open the digital out in the exclusive mode
3347 int snd_hda_multi_out_dig_open(struct hda_codec *codec,
3348 struct hda_multi_out *mout)
3350 mutex_lock(&codec->spdif_mutex);
3351 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
3352 /* already opened as analog dup; reset it once */
3353 cleanup_dig_out_stream(codec, mout->dig_out_nid);
3354 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
3355 mutex_unlock(&codec->spdif_mutex);
3358 EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_open);
3360 int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
3361 struct hda_multi_out *mout,
3362 unsigned int stream_tag,
3363 unsigned int format,
3364 struct snd_pcm_substream *substream)
3366 mutex_lock(&codec->spdif_mutex);
3367 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
3368 mutex_unlock(&codec->spdif_mutex);
3371 EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_prepare);
3373 int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec,
3374 struct hda_multi_out *mout)
3376 mutex_lock(&codec->spdif_mutex);
3377 cleanup_dig_out_stream(codec, mout->dig_out_nid);
3378 mutex_unlock(&codec->spdif_mutex);
3381 EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_cleanup);
3384 * release the digital out
3386 int snd_hda_multi_out_dig_close(struct hda_codec *codec,
3387 struct hda_multi_out *mout)
3389 mutex_lock(&codec->spdif_mutex);
3390 mout->dig_out_used = 0;
3391 mutex_unlock(&codec->spdif_mutex);
3394 EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_close);
3397 * set up more restrictions for analog out
3399 int snd_hda_multi_out_analog_open(struct hda_codec *codec,
3400 struct hda_multi_out *mout,
3401 struct snd_pcm_substream *substream,
3402 struct hda_pcm_stream *hinfo)
3404 struct snd_pcm_runtime *runtime = substream->runtime;
3405 runtime->hw.channels_max = mout->max_channels;
3406 if (mout->dig_out_nid) {
3407 if (!mout->analog_rates) {
3408 mout->analog_rates = hinfo->rates;
3409 mout->analog_formats = hinfo->formats;
3410 mout->analog_maxbps = hinfo->maxbps;
3412 runtime->hw.rates = mout->analog_rates;
3413 runtime->hw.formats = mout->analog_formats;
3414 hinfo->maxbps = mout->analog_maxbps;
3416 if (!mout->spdif_rates) {
3417 snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
3419 &mout->spdif_formats,
3420 &mout->spdif_maxbps);
3422 mutex_lock(&codec->spdif_mutex);
3423 if (mout->share_spdif) {
3424 runtime->hw.rates &= mout->spdif_rates;
3425 runtime->hw.formats &= mout->spdif_formats;
3426 if (mout->spdif_maxbps < hinfo->maxbps)
3427 hinfo->maxbps = mout->spdif_maxbps;
3429 mutex_unlock(&codec->spdif_mutex);
3431 return snd_pcm_hw_constraint_step(substream->runtime, 0,
3432 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
3434 EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_open);
3437 * set up the i/o for analog out
3438 * when the digital out is available, copy the front out to digital out, too.
3440 int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
3441 struct hda_multi_out *mout,
3442 unsigned int stream_tag,
3443 unsigned int format,
3444 struct snd_pcm_substream *substream)
3446 hda_nid_t *nids = mout->dac_nids;
3447 int chs = substream->runtime->channels;
3450 mutex_lock(&codec->spdif_mutex);
3451 if (mout->dig_out_nid && mout->share_spdif &&
3452 mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
3454 snd_hda_is_supported_format(codec, mout->dig_out_nid,
3456 !(codec->spdif_status & IEC958_AES0_NONAUDIO)) {
3457 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
3458 setup_dig_out_stream(codec, mout->dig_out_nid,
3459 stream_tag, format);
3461 mout->dig_out_used = 0;
3462 cleanup_dig_out_stream(codec, mout->dig_out_nid);
3465 mutex_unlock(&codec->spdif_mutex);
3468 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
3470 if (!mout->no_share_stream &&
3471 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
3472 /* headphone out will just decode front left/right (stereo) */
3473 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
3475 /* extra outputs copied from front */
3476 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
3477 if (!mout->no_share_stream && mout->extra_out_nid[i])
3478 snd_hda_codec_setup_stream(codec,
3479 mout->extra_out_nid[i],
3480 stream_tag, 0, format);
3483 for (i = 1; i < mout->num_dacs; i++) {
3484 if (chs >= (i + 1) * 2) /* independent out */
3485 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
3487 else if (!mout->no_share_stream) /* copy front */
3488 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
3493 EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_prepare);
3496 * clean up the setting for analog out
3498 int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
3499 struct hda_multi_out *mout)
3501 hda_nid_t *nids = mout->dac_nids;
3504 for (i = 0; i < mout->num_dacs; i++)
3505 snd_hda_codec_cleanup_stream(codec, nids[i]);
3507 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
3508 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
3509 if (mout->extra_out_nid[i])
3510 snd_hda_codec_cleanup_stream(codec,
3511 mout->extra_out_nid[i]);
3512 mutex_lock(&codec->spdif_mutex);
3513 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
3514 cleanup_dig_out_stream(codec, mout->dig_out_nid);
3515 mout->dig_out_used = 0;
3517 mutex_unlock(&codec->spdif_mutex);
3520 EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_cleanup);
3523 * Helper for automatic pin configuration
3526 static int is_in_nid_list(hda_nid_t nid, hda_nid_t *list)
3528 for (; *list; list++)
3536 * Sort an associated group of pins according to their sequence numbers.
3538 static void sort_pins_by_sequence(hda_nid_t * pins, short * sequences,
3545 for (i = 0; i < num_pins; i++) {
3546 for (j = i + 1; j < num_pins; j++) {
3547 if (sequences[i] > sequences[j]) {
3549 sequences[i] = sequences[j];
3561 * Parse all pin widgets and store the useful pin nids to cfg
3563 * The number of line-outs or any primary output is stored in line_outs,
3564 * and the corresponding output pins are assigned to line_out_pins[],
3565 * in the order of front, rear, CLFE, side, ...
3567 * If more extra outputs (speaker and headphone) are found, the pins are
3568 * assisnged to hp_pins[] and speaker_pins[], respectively. If no line-out jack
3569 * is detected, one of speaker of HP pins is assigned as the primary
3570 * output, i.e. to line_out_pins[0]. So, line_outs is always positive
3571 * if any analog output exists.
3573 * The analog input pins are assigned to input_pins array.
3574 * The digital input/output pins are assigned to dig_in_pin and dig_out_pin,
3577 int snd_hda_parse_pin_def_config(struct hda_codec *codec,
3578 struct auto_pin_cfg *cfg,
3579 hda_nid_t *ignore_nids)
3581 hda_nid_t nid, end_nid;
3582 short seq, assoc_line_out, assoc_speaker;
3583 short sequences_line_out[ARRAY_SIZE(cfg->line_out_pins)];
3584 short sequences_speaker[ARRAY_SIZE(cfg->speaker_pins)];
3585 short sequences_hp[ARRAY_SIZE(cfg->hp_pins)];
3587 memset(cfg, 0, sizeof(*cfg));
3589 memset(sequences_line_out, 0, sizeof(sequences_line_out));
3590 memset(sequences_speaker, 0, sizeof(sequences_speaker));
3591 memset(sequences_hp, 0, sizeof(sequences_hp));
3592 assoc_line_out = assoc_speaker = 0;
3594 end_nid = codec->start_nid + codec->num_nodes;
3595 for (nid = codec->start_nid; nid < end_nid; nid++) {
3596 unsigned int wid_caps = get_wcaps(codec, nid);
3597 unsigned int wid_type =
3598 (wid_caps & AC_WCAP_TYPE) >> AC_WCAP_TYPE_SHIFT;
3599 unsigned int def_conf;
3602 /* read all default configuration for pin complex */
3603 if (wid_type != AC_WID_PIN)
3605 /* ignore the given nids (e.g. pc-beep returns error) */
3606 if (ignore_nids && is_in_nid_list(nid, ignore_nids))
3609 def_conf = snd_hda_codec_get_pincfg(codec, nid);
3610 if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE)
3612 loc = get_defcfg_location(def_conf);
3613 switch (get_defcfg_device(def_conf)) {
3614 case AC_JACK_LINE_OUT:
3615 seq = get_defcfg_sequence(def_conf);
3616 assoc = get_defcfg_association(def_conf);
3618 if (!(wid_caps & AC_WCAP_STEREO))
3619 if (!cfg->mono_out_pin)
3620 cfg->mono_out_pin = nid;
3623 if (!assoc_line_out)
3624 assoc_line_out = assoc;
3625 else if (assoc_line_out != assoc)
3627 if (cfg->line_outs >= ARRAY_SIZE(cfg->line_out_pins))
3629 cfg->line_out_pins[cfg->line_outs] = nid;
3630 sequences_line_out[cfg->line_outs] = seq;
3633 case AC_JACK_SPEAKER:
3634 seq = get_defcfg_sequence(def_conf);
3635 assoc = get_defcfg_association(def_conf);
3638 if (! assoc_speaker)
3639 assoc_speaker = assoc;
3640 else if (assoc_speaker != assoc)
3642 if (cfg->speaker_outs >= ARRAY_SIZE(cfg->speaker_pins))
3644 cfg->speaker_pins[cfg->speaker_outs] = nid;
3645 sequences_speaker[cfg->speaker_outs] = seq;
3646 cfg->speaker_outs++;
3648 case AC_JACK_HP_OUT:
3649 seq = get_defcfg_sequence(def_conf);
3650 assoc = get_defcfg_association(def_conf);
3651 if (cfg->hp_outs >= ARRAY_SIZE(cfg->hp_pins))
3653 cfg->hp_pins[cfg->hp_outs] = nid;
3654 sequences_hp[cfg->hp_outs] = (assoc << 4) | seq;
3657 case AC_JACK_MIC_IN: {
3659 if (loc == AC_JACK_LOC_FRONT) {
3660 preferred = AUTO_PIN_FRONT_MIC;
3663 preferred = AUTO_PIN_MIC;
3664 alt = AUTO_PIN_FRONT_MIC;
3666 if (!cfg->input_pins[preferred])
3667 cfg->input_pins[preferred] = nid;
3668 else if (!cfg->input_pins[alt])
3669 cfg->input_pins[alt] = nid;
3672 case AC_JACK_LINE_IN:
3673 if (loc == AC_JACK_LOC_FRONT)
3674 cfg->input_pins[AUTO_PIN_FRONT_LINE] = nid;
3676 cfg->input_pins[AUTO_PIN_LINE] = nid;
3679 cfg->input_pins[AUTO_PIN_CD] = nid;
3682 cfg->input_pins[AUTO_PIN_AUX] = nid;
3684 case AC_JACK_SPDIF_OUT:
3685 case AC_JACK_DIG_OTHER_OUT:
3686 if (cfg->dig_outs >= ARRAY_SIZE(cfg->dig_out_pins))
3688 cfg->dig_out_pins[cfg->dig_outs] = nid;
3689 cfg->dig_out_type[cfg->dig_outs] =
3690 (loc == AC_JACK_LOC_HDMI) ?
3691 HDA_PCM_TYPE_HDMI : HDA_PCM_TYPE_SPDIF;
3694 case AC_JACK_SPDIF_IN:
3695 case AC_JACK_DIG_OTHER_IN:
3696 cfg->dig_in_pin = nid;
3697 if (loc == AC_JACK_LOC_HDMI)
3698 cfg->dig_in_type = HDA_PCM_TYPE_HDMI;
3700 cfg->dig_in_type = HDA_PCM_TYPE_SPDIF;
3706 * If no line-out is defined but multiple HPs are found,
3707 * some of them might be the real line-outs.
3709 if (!cfg->line_outs && cfg->hp_outs > 1) {
3711 while (i < cfg->hp_outs) {
3712 /* The real HPs should have the sequence 0x0f */
3713 if ((sequences_hp[i] & 0x0f) == 0x0f) {
3717 /* Move it to the line-out table */
3718 cfg->line_out_pins[cfg->line_outs] = cfg->hp_pins[i];
3719 sequences_line_out[cfg->line_outs] = sequences_hp[i];
3722 memmove(cfg->hp_pins + i, cfg->hp_pins + i + 1,
3723 sizeof(cfg->hp_pins[0]) * (cfg->hp_outs - i));
3724 memmove(sequences_hp + i - 1, sequences_hp + i,
3725 sizeof(sequences_hp[0]) * (cfg->hp_outs - i));
3729 /* sort by sequence */
3730 sort_pins_by_sequence(cfg->line_out_pins, sequences_line_out,
3732 sort_pins_by_sequence(cfg->speaker_pins, sequences_speaker,
3734 sort_pins_by_sequence(cfg->hp_pins, sequences_hp,
3737 /* if we have only one mic, make it AUTO_PIN_MIC */
3738 if (!cfg->input_pins[AUTO_PIN_MIC] &&
3739 cfg->input_pins[AUTO_PIN_FRONT_MIC]) {
3740 cfg->input_pins[AUTO_PIN_MIC] =
3741 cfg->input_pins[AUTO_PIN_FRONT_MIC];
3742 cfg->input_pins[AUTO_PIN_FRONT_MIC] = 0;
3744 /* ditto for line-in */
3745 if (!cfg->input_pins[AUTO_PIN_LINE] &&
3746 cfg->input_pins[AUTO_PIN_FRONT_LINE]) {
3747 cfg->input_pins[AUTO_PIN_LINE] =
3748 cfg->input_pins[AUTO_PIN_FRONT_LINE];
3749 cfg->input_pins[AUTO_PIN_FRONT_LINE] = 0;
3753 * FIX-UP: if no line-outs are detected, try to use speaker or HP pin
3754 * as a primary output
3756 if (!cfg->line_outs) {
3757 if (cfg->speaker_outs) {
3758 cfg->line_outs = cfg->speaker_outs;
3759 memcpy(cfg->line_out_pins, cfg->speaker_pins,
3760 sizeof(cfg->speaker_pins));
3761 cfg->speaker_outs = 0;
3762 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
3763 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
3764 } else if (cfg->hp_outs) {
3765 cfg->line_outs = cfg->hp_outs;
3766 memcpy(cfg->line_out_pins, cfg->hp_pins,
3767 sizeof(cfg->hp_pins));
3769 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
3770 cfg->line_out_type = AUTO_PIN_HP_OUT;
3774 /* Reorder the surround channels
3775 * ALSA sequence is front/surr/clfe/side
3777 * 4-ch: front/surr => OK as it is
3778 * 6-ch: front/clfe/surr
3779 * 8-ch: front/clfe/rear/side|fc
3781 switch (cfg->line_outs) {
3784 nid = cfg->line_out_pins[1];
3785 cfg->line_out_pins[1] = cfg->line_out_pins[2];
3786 cfg->line_out_pins[2] = nid;
3791 * debug prints of the parsed results
3793 snd_printd("autoconfig: line_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
3794 cfg->line_outs, cfg->line_out_pins[0], cfg->line_out_pins[1],
3795 cfg->line_out_pins[2], cfg->line_out_pins[3],
3796 cfg->line_out_pins[4]);
3797 snd_printd(" speaker_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
3798 cfg->speaker_outs, cfg->speaker_pins[0],
3799 cfg->speaker_pins[1], cfg->speaker_pins[2],
3800 cfg->speaker_pins[3], cfg->speaker_pins[4]);
3801 snd_printd(" hp_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
3802 cfg->hp_outs, cfg->hp_pins[0],
3803 cfg->hp_pins[1], cfg->hp_pins[2],
3804 cfg->hp_pins[3], cfg->hp_pins[4]);
3805 snd_printd(" mono: mono_out=0x%x\n", cfg->mono_out_pin);
3807 snd_printd(" dig-out=0x%x/0x%x\n",
3808 cfg->dig_out_pins[0], cfg->dig_out_pins[1]);
3809 snd_printd(" inputs: mic=0x%x, fmic=0x%x, line=0x%x, fline=0x%x,"
3810 " cd=0x%x, aux=0x%x\n",
3811 cfg->input_pins[AUTO_PIN_MIC],
3812 cfg->input_pins[AUTO_PIN_FRONT_MIC],
3813 cfg->input_pins[AUTO_PIN_LINE],
3814 cfg->input_pins[AUTO_PIN_FRONT_LINE],
3815 cfg->input_pins[AUTO_PIN_CD],
3816 cfg->input_pins[AUTO_PIN_AUX]);
3817 if (cfg->dig_in_pin)
3818 snd_printd(" dig-in=0x%x\n", cfg->dig_in_pin);
3822 EXPORT_SYMBOL_HDA(snd_hda_parse_pin_def_config);
3824 /* labels for input pins */
3825 const char *auto_pin_cfg_labels[AUTO_PIN_LAST] = {
3826 "Mic", "Front Mic", "Line", "Front Line", "CD", "Aux"
3828 EXPORT_SYMBOL_HDA(auto_pin_cfg_labels);
3837 * snd_hda_suspend - suspend the codecs
3839 * @state: suspsend state
3841 * Returns 0 if successful.
3843 int snd_hda_suspend(struct hda_bus *bus, pm_message_t state)
3845 struct hda_codec *codec;
3847 list_for_each_entry(codec, &bus->codec_list, list) {
3848 #ifdef CONFIG_SND_HDA_POWER_SAVE
3849 if (!codec->power_on)
3852 hda_call_codec_suspend(codec);
3856 EXPORT_SYMBOL_HDA(snd_hda_suspend);
3859 * snd_hda_resume - resume the codecs
3862 * Returns 0 if successful.
3864 * This fucntion is defined only when POWER_SAVE isn't set.
3865 * In the power-save mode, the codec is resumed dynamically.
3867 int snd_hda_resume(struct hda_bus *bus)
3869 struct hda_codec *codec;
3871 list_for_each_entry(codec, &bus->codec_list, list) {
3872 if (snd_hda_codec_needs_resume(codec))
3873 hda_call_codec_resume(codec);
3877 EXPORT_SYMBOL_HDA(snd_hda_resume);
3878 #endif /* CONFIG_PM */
3884 /* get a new element from the given array
3885 * if it exceeds the pre-allocated array size, re-allocate the array
3887 void *snd_array_new(struct snd_array *array)
3889 if (array->used >= array->alloced) {
3890 int num = array->alloced + array->alloc_align;
3892 if (snd_BUG_ON(num >= 4096))
3894 nlist = kcalloc(num + 1, array->elem_size, GFP_KERNEL);
3898 memcpy(nlist, array->list,
3899 array->elem_size * array->alloced);
3902 array->list = nlist;
3903 array->alloced = num;
3905 return snd_array_elem(array, array->used++);
3907 EXPORT_SYMBOL_HDA(snd_array_new);
3909 /* free the given array elements */
3910 void snd_array_free(struct snd_array *array)
3917 EXPORT_SYMBOL_HDA(snd_array_free);
3920 * used by hda_proc.c and hda_eld.c
3922 void snd_print_pcm_rates(int pcm, char *buf, int buflen)
3924 static unsigned int rates[] = {
3925 8000, 11025, 16000, 22050, 32000, 44100, 48000, 88200,
3926 96000, 176400, 192000, 384000
3930 for (i = 0, j = 0; i < ARRAY_SIZE(rates); i++)
3932 j += snprintf(buf + j, buflen - j, " %d", rates[i]);
3934 buf[j] = '\0'; /* necessary when j == 0 */
3936 EXPORT_SYMBOL_HDA(snd_print_pcm_rates);
3938 void snd_print_pcm_bits(int pcm, char *buf, int buflen)
3940 static unsigned int bits[] = { 8, 16, 20, 24, 32 };
3943 for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
3944 if (pcm & (AC_SUPPCM_BITS_8 << i))
3945 j += snprintf(buf + j, buflen - j, " %d", bits[i]);
3947 buf[j] = '\0'; /* necessary when j == 0 */
3949 EXPORT_SYMBOL_HDA(snd_print_pcm_bits);
3951 MODULE_DESCRIPTION("HDA codec core");
3952 MODULE_LICENSE("GPL");