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 if ((codec->addr & ~0xf) || (direct & ~1) || (nid & ~0x7f) ||
154 (verb & ~0xfff) || (parm & ~0xffff)) {
155 printk(KERN_ERR "hda-codec: out of range cmd %x:%x:%x:%x:%x\n",
156 codec->addr, direct, nid, verb, parm);
160 val = (u32)codec->addr << 28;
161 val |= (u32)direct << 27;
162 val |= (u32)nid << 20;
169 * Send and receive a verb
171 static int codec_exec_verb(struct hda_codec *codec, unsigned int cmd,
174 struct hda_bus *bus = codec->bus;
183 snd_hda_power_up(codec);
184 mutex_lock(&bus->cmd_mutex);
185 err = bus->ops.command(bus, cmd);
187 *res = bus->ops.get_response(bus);
188 mutex_unlock(&bus->cmd_mutex);
189 snd_hda_power_down(codec);
190 if (res && *res == -1 && bus->rirb_error) {
191 if (bus->response_reset) {
192 snd_printd("hda_codec: resetting BUS due to "
193 "fatal communication error\n");
194 bus->ops.bus_reset(bus);
198 /* clear reset-flag when the communication gets recovered */
200 bus->response_reset = 0;
205 * snd_hda_codec_read - send a command and get the response
206 * @codec: the HDA codec
207 * @nid: NID to send the command
208 * @direct: direct flag
209 * @verb: the verb to send
210 * @parm: the parameter for the verb
212 * Send a single command and read the corresponding response.
214 * Returns the obtained response value, or -1 for an error.
216 unsigned int snd_hda_codec_read(struct hda_codec *codec, hda_nid_t nid,
218 unsigned int verb, unsigned int parm)
220 unsigned cmd = make_codec_cmd(codec, nid, direct, verb, parm);
222 codec_exec_verb(codec, cmd, &res);
225 EXPORT_SYMBOL_HDA(snd_hda_codec_read);
228 * snd_hda_codec_write - send a single command without waiting for response
229 * @codec: the HDA codec
230 * @nid: NID to send the command
231 * @direct: direct flag
232 * @verb: the verb to send
233 * @parm: the parameter for the verb
235 * Send a single command without waiting for response.
237 * Returns 0 if successful, or a negative error code.
239 int snd_hda_codec_write(struct hda_codec *codec, hda_nid_t nid, int direct,
240 unsigned int verb, unsigned int parm)
242 unsigned int cmd = make_codec_cmd(codec, nid, direct, verb, parm);
244 return codec_exec_verb(codec, cmd,
245 codec->bus->sync_write ? &res : NULL);
247 EXPORT_SYMBOL_HDA(snd_hda_codec_write);
250 * snd_hda_sequence_write - sequence writes
251 * @codec: the HDA codec
252 * @seq: VERB array to send
254 * Send the commands sequentially from the given array.
255 * The array must be terminated with NID=0.
257 void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq)
259 for (; seq->nid; seq++)
260 snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param);
262 EXPORT_SYMBOL_HDA(snd_hda_sequence_write);
265 * snd_hda_get_sub_nodes - get the range of sub nodes
266 * @codec: the HDA codec
268 * @start_id: the pointer to store the start NID
270 * Parse the NID and store the start NID of its sub-nodes.
271 * Returns the number of sub-nodes.
273 int snd_hda_get_sub_nodes(struct hda_codec *codec, hda_nid_t nid,
278 parm = snd_hda_param_read(codec, nid, AC_PAR_NODE_COUNT);
281 *start_id = (parm >> 16) & 0x7fff;
282 return (int)(parm & 0x7fff);
284 EXPORT_SYMBOL_HDA(snd_hda_get_sub_nodes);
287 * snd_hda_get_connections - get connection list
288 * @codec: the HDA codec
290 * @conn_list: connection list array
291 * @max_conns: max. number of connections to store
293 * Parses the connection list of the given widget and stores the list
296 * Returns the number of connections, or a negative error code.
298 int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
299 hda_nid_t *conn_list, int max_conns)
302 int i, conn_len, conns;
303 unsigned int shift, num_elems, mask;
307 if (snd_BUG_ON(!conn_list || max_conns <= 0))
310 wcaps = get_wcaps(codec, nid);
311 if (!(wcaps & AC_WCAP_CONN_LIST) &&
312 get_wcaps_type(wcaps) != AC_WID_VOL_KNB) {
313 snd_printk(KERN_WARNING "hda_codec: "
314 "connection list not available for 0x%x\n", nid);
318 parm = snd_hda_param_read(codec, nid, AC_PAR_CONNLIST_LEN);
319 if (parm & AC_CLIST_LONG) {
328 conn_len = parm & AC_CLIST_LENGTH;
329 mask = (1 << (shift-1)) - 1;
332 return 0; /* no connection */
335 /* single connection */
336 parm = snd_hda_codec_read(codec, nid, 0,
337 AC_VERB_GET_CONNECT_LIST, 0);
338 if (parm == -1 && codec->bus->rirb_error)
340 conn_list[0] = parm & mask;
344 /* multi connection */
347 for (i = 0; i < conn_len; i++) {
351 if (i % num_elems == 0) {
352 parm = snd_hda_codec_read(codec, nid, 0,
353 AC_VERB_GET_CONNECT_LIST, i);
354 if (parm == -1 && codec->bus->rirb_error)
357 range_val = !!(parm & (1 << (shift-1))); /* ranges */
360 snd_printk(KERN_WARNING "hda_codec: "
361 "invalid CONNECT_LIST verb %x[%i]:%x\n",
367 /* ranges between the previous and this one */
368 if (!prev_nid || prev_nid >= val) {
369 snd_printk(KERN_WARNING "hda_codec: "
370 "invalid dep_range_val %x:%x\n",
374 for (n = prev_nid + 1; n <= val; n++) {
375 if (conns >= max_conns) {
377 "Too many connections\n");
380 conn_list[conns++] = n;
383 if (conns >= max_conns) {
384 snd_printk(KERN_ERR "Too many connections\n");
387 conn_list[conns++] = val;
393 EXPORT_SYMBOL_HDA(snd_hda_get_connections);
397 * snd_hda_queue_unsol_event - add an unsolicited event to queue
399 * @res: unsolicited event (lower 32bit of RIRB entry)
400 * @res_ex: codec addr and flags (upper 32bit or RIRB entry)
402 * Adds the given event to the queue. The events are processed in
403 * the workqueue asynchronously. Call this function in the interrupt
404 * hanlder when RIRB receives an unsolicited event.
406 * Returns 0 if successful, or a negative error code.
408 int snd_hda_queue_unsol_event(struct hda_bus *bus, u32 res, u32 res_ex)
410 struct hda_bus_unsolicited *unsol;
417 wp = (unsol->wp + 1) % HDA_UNSOL_QUEUE_SIZE;
421 unsol->queue[wp] = res;
422 unsol->queue[wp + 1] = res_ex;
424 queue_work(bus->workq, &unsol->work);
428 EXPORT_SYMBOL_HDA(snd_hda_queue_unsol_event);
431 * process queued unsolicited events
433 static void process_unsol_events(struct work_struct *work)
435 struct hda_bus_unsolicited *unsol =
436 container_of(work, struct hda_bus_unsolicited, work);
437 struct hda_bus *bus = unsol->bus;
438 struct hda_codec *codec;
439 unsigned int rp, caddr, res;
441 while (unsol->rp != unsol->wp) {
442 rp = (unsol->rp + 1) % HDA_UNSOL_QUEUE_SIZE;
445 res = unsol->queue[rp];
446 caddr = unsol->queue[rp + 1];
447 if (!(caddr & (1 << 4))) /* no unsolicited event? */
449 codec = bus->caddr_tbl[caddr & 0x0f];
450 if (codec && codec->patch_ops.unsol_event)
451 codec->patch_ops.unsol_event(codec, res);
456 * initialize unsolicited queue
458 static int init_unsol_queue(struct hda_bus *bus)
460 struct hda_bus_unsolicited *unsol;
462 if (bus->unsol) /* already initialized */
465 unsol = kzalloc(sizeof(*unsol), GFP_KERNEL);
467 snd_printk(KERN_ERR "hda_codec: "
468 "can't allocate unsolicited queue\n");
471 INIT_WORK(&unsol->work, process_unsol_events);
480 static void snd_hda_codec_free(struct hda_codec *codec);
482 static int snd_hda_bus_free(struct hda_bus *bus)
484 struct hda_codec *codec, *n;
489 flush_workqueue(bus->workq);
492 list_for_each_entry_safe(codec, n, &bus->codec_list, list) {
493 snd_hda_codec_free(codec);
495 if (bus->ops.private_free)
496 bus->ops.private_free(bus);
498 destroy_workqueue(bus->workq);
503 static int snd_hda_bus_dev_free(struct snd_device *device)
505 struct hda_bus *bus = device->device_data;
507 return snd_hda_bus_free(bus);
510 #ifdef CONFIG_SND_HDA_HWDEP
511 static int snd_hda_bus_dev_register(struct snd_device *device)
513 struct hda_bus *bus = device->device_data;
514 struct hda_codec *codec;
515 list_for_each_entry(codec, &bus->codec_list, list) {
516 snd_hda_hwdep_add_sysfs(codec);
521 #define snd_hda_bus_dev_register NULL
525 * snd_hda_bus_new - create a HDA bus
526 * @card: the card entry
527 * @temp: the template for hda_bus information
528 * @busp: the pointer to store the created bus instance
530 * Returns 0 if successful, or a negative error code.
532 int /*__devinit*/ snd_hda_bus_new(struct snd_card *card,
533 const struct hda_bus_template *temp,
534 struct hda_bus **busp)
538 static struct snd_device_ops dev_ops = {
539 .dev_register = snd_hda_bus_dev_register,
540 .dev_free = snd_hda_bus_dev_free,
543 if (snd_BUG_ON(!temp))
545 if (snd_BUG_ON(!temp->ops.command || !temp->ops.get_response))
551 bus = kzalloc(sizeof(*bus), GFP_KERNEL);
553 snd_printk(KERN_ERR "can't allocate struct hda_bus\n");
558 bus->private_data = temp->private_data;
559 bus->pci = temp->pci;
560 bus->modelname = temp->modelname;
561 bus->power_save = temp->power_save;
562 bus->ops = temp->ops;
564 mutex_init(&bus->cmd_mutex);
565 INIT_LIST_HEAD(&bus->codec_list);
567 snprintf(bus->workq_name, sizeof(bus->workq_name),
568 "hd-audio%d", card->number);
569 bus->workq = create_singlethread_workqueue(bus->workq_name);
571 snd_printk(KERN_ERR "cannot create workqueue %s\n",
577 err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops);
579 snd_hda_bus_free(bus);
586 EXPORT_SYMBOL_HDA(snd_hda_bus_new);
588 #ifdef CONFIG_SND_HDA_GENERIC
589 #define is_generic_config(codec) \
590 (codec->modelname && !strcmp(codec->modelname, "generic"))
592 #define is_generic_config(codec) 0
596 #define HDA_MODREQ_MAX_COUNT 2 /* two request_modules()'s */
598 #define HDA_MODREQ_MAX_COUNT 0 /* all presets are statically linked */
602 * find a matching codec preset
604 static const struct hda_codec_preset *
605 find_codec_preset(struct hda_codec *codec)
607 struct hda_codec_preset_list *tbl;
608 const struct hda_codec_preset *preset;
609 int mod_requested = 0;
611 if (is_generic_config(codec))
612 return NULL; /* use the generic parser */
615 mutex_lock(&preset_mutex);
616 list_for_each_entry(tbl, &hda_preset_tables, list) {
617 if (!try_module_get(tbl->owner)) {
618 snd_printk(KERN_ERR "hda_codec: cannot module_get\n");
621 for (preset = tbl->preset; preset->id; preset++) {
622 u32 mask = preset->mask;
623 if (preset->afg && preset->afg != codec->afg)
625 if (preset->mfg && preset->mfg != codec->mfg)
629 if (preset->id == (codec->vendor_id & mask) &&
631 preset->rev == codec->revision_id)) {
632 mutex_unlock(&preset_mutex);
633 codec->owner = tbl->owner;
637 module_put(tbl->owner);
639 mutex_unlock(&preset_mutex);
641 if (mod_requested < HDA_MODREQ_MAX_COUNT) {
644 snprintf(name, sizeof(name), "snd-hda-codec-id:%08x",
647 snprintf(name, sizeof(name), "snd-hda-codec-id:%04x*",
648 (codec->vendor_id >> 16) & 0xffff);
649 request_module(name);
657 * get_codec_name - store the codec name
659 static int get_codec_name(struct hda_codec *codec)
661 const struct hda_vendor_id *c;
662 const char *vendor = NULL;
663 u16 vendor_id = codec->vendor_id >> 16;
666 if (codec->vendor_name)
669 for (c = hda_vendor_ids; c->id; c++) {
670 if (c->id == vendor_id) {
676 sprintf(tmp, "Generic %04x", vendor_id);
679 codec->vendor_name = kstrdup(vendor, GFP_KERNEL);
680 if (!codec->vendor_name)
684 if (codec->chip_name)
687 if (codec->preset && codec->preset->name)
688 codec->chip_name = kstrdup(codec->preset->name, GFP_KERNEL);
690 sprintf(tmp, "ID %x", codec->vendor_id & 0xffff);
691 codec->chip_name = kstrdup(tmp, GFP_KERNEL);
693 if (!codec->chip_name)
699 * look for an AFG and MFG nodes
701 static void /*__devinit*/ setup_fg_nodes(struct hda_codec *codec)
703 int i, total_nodes, function_id;
706 total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid);
707 for (i = 0; i < total_nodes; i++, nid++) {
708 function_id = snd_hda_param_read(codec, nid,
709 AC_PAR_FUNCTION_TYPE) & 0xff;
710 switch (function_id) {
711 case AC_GRP_AUDIO_FUNCTION:
713 codec->function_id = function_id;
715 case AC_GRP_MODEM_FUNCTION:
717 codec->function_id = function_id;
726 * read widget caps for each widget and store in cache
728 static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
733 codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node,
735 codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL);
738 nid = codec->start_nid;
739 for (i = 0; i < codec->num_nodes; i++, nid++)
740 codec->wcaps[i] = snd_hda_param_read(codec, nid,
741 AC_PAR_AUDIO_WIDGET_CAP);
745 /* read all pin default configurations and save codec->init_pins */
746 static int read_pin_defaults(struct hda_codec *codec)
749 hda_nid_t nid = codec->start_nid;
751 for (i = 0; i < codec->num_nodes; i++, nid++) {
752 struct hda_pincfg *pin;
753 unsigned int wcaps = get_wcaps(codec, nid);
754 unsigned int wid_type = get_wcaps_type(wcaps);
755 if (wid_type != AC_WID_PIN)
757 pin = snd_array_new(&codec->init_pins);
761 pin->cfg = snd_hda_codec_read(codec, nid, 0,
762 AC_VERB_GET_CONFIG_DEFAULT, 0);
767 /* look up the given pin config list and return the item matching with NID */
768 static struct hda_pincfg *look_up_pincfg(struct hda_codec *codec,
769 struct snd_array *array,
773 for (i = 0; i < array->used; i++) {
774 struct hda_pincfg *pin = snd_array_elem(array, i);
781 /* write a config value for the given NID */
782 static void set_pincfg(struct hda_codec *codec, hda_nid_t nid,
786 for (i = 0; i < 4; i++) {
787 snd_hda_codec_write(codec, nid, 0,
788 AC_VERB_SET_CONFIG_DEFAULT_BYTES_0 + i,
794 /* set the current pin config value for the given NID.
795 * the value is cached, and read via snd_hda_codec_get_pincfg()
797 int snd_hda_add_pincfg(struct hda_codec *codec, struct snd_array *list,
798 hda_nid_t nid, unsigned int cfg)
800 struct hda_pincfg *pin;
803 oldcfg = snd_hda_codec_get_pincfg(codec, nid);
804 pin = look_up_pincfg(codec, list, nid);
806 pin = snd_array_new(list);
813 /* change only when needed; e.g. if the pincfg is already present
814 * in user_pins[], don't write it
816 cfg = snd_hda_codec_get_pincfg(codec, nid);
818 set_pincfg(codec, nid, cfg);
822 int snd_hda_codec_set_pincfg(struct hda_codec *codec,
823 hda_nid_t nid, unsigned int cfg)
825 return snd_hda_add_pincfg(codec, &codec->driver_pins, nid, cfg);
827 EXPORT_SYMBOL_HDA(snd_hda_codec_set_pincfg);
829 /* get the current pin config value of the given pin NID */
830 unsigned int snd_hda_codec_get_pincfg(struct hda_codec *codec, hda_nid_t nid)
832 struct hda_pincfg *pin;
834 #ifdef CONFIG_SND_HDA_HWDEP
835 pin = look_up_pincfg(codec, &codec->user_pins, nid);
839 pin = look_up_pincfg(codec, &codec->driver_pins, nid);
842 pin = look_up_pincfg(codec, &codec->init_pins, nid);
847 EXPORT_SYMBOL_HDA(snd_hda_codec_get_pincfg);
849 /* restore all current pin configs */
850 static void restore_pincfgs(struct hda_codec *codec)
853 for (i = 0; i < codec->init_pins.used; i++) {
854 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
855 set_pincfg(codec, pin->nid,
856 snd_hda_codec_get_pincfg(codec, pin->nid));
860 static void init_hda_cache(struct hda_cache_rec *cache,
861 unsigned int record_size);
862 static void free_hda_cache(struct hda_cache_rec *cache);
864 /* restore the initial pin cfgs and release all pincfg lists */
865 static void restore_init_pincfgs(struct hda_codec *codec)
867 /* first free driver_pins and user_pins, then call restore_pincfg
868 * so that only the values in init_pins are restored
870 snd_array_free(&codec->driver_pins);
871 #ifdef CONFIG_SND_HDA_HWDEP
872 snd_array_free(&codec->user_pins);
874 restore_pincfgs(codec);
875 snd_array_free(&codec->init_pins);
881 static void snd_hda_codec_free(struct hda_codec *codec)
885 restore_init_pincfgs(codec);
886 #ifdef CONFIG_SND_HDA_POWER_SAVE
887 cancel_delayed_work(&codec->power_work);
888 flush_workqueue(codec->bus->workq);
890 list_del(&codec->list);
891 snd_array_free(&codec->mixers);
892 codec->bus->caddr_tbl[codec->addr] = NULL;
893 if (codec->patch_ops.free)
894 codec->patch_ops.free(codec);
895 module_put(codec->owner);
896 free_hda_cache(&codec->amp_cache);
897 free_hda_cache(&codec->cmd_cache);
898 kfree(codec->vendor_name);
899 kfree(codec->chip_name);
900 kfree(codec->modelname);
905 static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
906 unsigned int power_state);
909 * snd_hda_codec_new - create a HDA codec
910 * @bus: the bus to assign
911 * @codec_addr: the codec address
912 * @codecp: the pointer to store the generated codec
914 * Returns 0 if successful, or a negative error code.
916 int /*__devinit*/ snd_hda_codec_new(struct hda_bus *bus, unsigned int codec_addr,
917 struct hda_codec **codecp)
919 struct hda_codec *codec;
923 if (snd_BUG_ON(!bus))
925 if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS))
928 if (bus->caddr_tbl[codec_addr]) {
929 snd_printk(KERN_ERR "hda_codec: "
930 "address 0x%x is already occupied\n", codec_addr);
934 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
936 snd_printk(KERN_ERR "can't allocate struct hda_codec\n");
941 codec->addr = codec_addr;
942 mutex_init(&codec->spdif_mutex);
943 mutex_init(&codec->control_mutex);
944 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
945 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
946 snd_array_init(&codec->mixers, sizeof(struct snd_kcontrol *), 32);
947 snd_array_init(&codec->init_pins, sizeof(struct hda_pincfg), 16);
948 snd_array_init(&codec->driver_pins, sizeof(struct hda_pincfg), 16);
949 if (codec->bus->modelname) {
950 codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL);
951 if (!codec->modelname) {
952 snd_hda_codec_free(codec);
957 #ifdef CONFIG_SND_HDA_POWER_SAVE
958 INIT_DELAYED_WORK(&codec->power_work, hda_power_work);
959 /* snd_hda_codec_new() marks the codec as power-up, and leave it as is.
960 * the caller has to power down appropriatley after initialization
963 hda_keep_power_on(codec);
966 list_add_tail(&codec->list, &bus->codec_list);
967 bus->caddr_tbl[codec_addr] = codec;
969 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
971 if (codec->vendor_id == -1)
972 /* read again, hopefully the access method was corrected
973 * in the last read...
975 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
977 codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT,
978 AC_PAR_SUBSYSTEM_ID);
979 codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT,
982 setup_fg_nodes(codec);
983 if (!codec->afg && !codec->mfg) {
984 snd_printdd("hda_codec: no AFG or MFG node found\n");
989 err = read_widget_caps(codec, codec->afg ? codec->afg : codec->mfg);
991 snd_printk(KERN_ERR "hda_codec: cannot malloc\n");
994 err = read_pin_defaults(codec);
998 if (!codec->subsystem_id) {
999 hda_nid_t nid = codec->afg ? codec->afg : codec->mfg;
1000 codec->subsystem_id =
1001 snd_hda_codec_read(codec, nid, 0,
1002 AC_VERB_GET_SUBSYSTEM_ID, 0);
1005 /* power-up all before initialization */
1006 hda_set_power_state(codec,
1007 codec->afg ? codec->afg : codec->mfg,
1010 snd_hda_codec_proc_new(codec);
1012 snd_hda_create_hwdep(codec);
1014 sprintf(component, "HDA:%08x,%08x,%08x", codec->vendor_id,
1015 codec->subsystem_id, codec->revision_id);
1016 snd_component_add(codec->bus->card, component);
1023 snd_hda_codec_free(codec);
1026 EXPORT_SYMBOL_HDA(snd_hda_codec_new);
1028 int snd_hda_codec_configure(struct hda_codec *codec)
1032 codec->preset = find_codec_preset(codec);
1033 if (!codec->vendor_name || !codec->chip_name) {
1034 err = get_codec_name(codec);
1038 /* audio codec should override the mixer name */
1039 if (codec->afg || !*codec->bus->card->mixername)
1040 snprintf(codec->bus->card->mixername,
1041 sizeof(codec->bus->card->mixername),
1042 "%s %s", codec->vendor_name, codec->chip_name);
1044 if (is_generic_config(codec)) {
1045 err = snd_hda_parse_generic_codec(codec);
1048 if (codec->preset && codec->preset->patch) {
1049 err = codec->preset->patch(codec);
1053 /* call the default parser */
1054 err = snd_hda_parse_generic_codec(codec);
1056 printk(KERN_ERR "hda-codec: No codec parser is available\n");
1059 if (!err && codec->patch_ops.unsol_event)
1060 err = init_unsol_queue(codec->bus);
1063 EXPORT_SYMBOL_HDA(snd_hda_codec_configure);
1066 * snd_hda_codec_setup_stream - set up the codec for streaming
1067 * @codec: the CODEC to set up
1068 * @nid: the NID to set up
1069 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
1070 * @channel_id: channel id to pass, zero based.
1071 * @format: stream format.
1073 void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
1075 int channel_id, int format)
1080 snd_printdd("hda_codec_setup_stream: "
1081 "NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
1082 nid, stream_tag, channel_id, format);
1083 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID,
1084 (stream_tag << 4) | channel_id);
1086 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, format);
1088 EXPORT_SYMBOL_HDA(snd_hda_codec_setup_stream);
1090 void snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid)
1095 snd_printdd("hda_codec_cleanup_stream: NID=0x%x\n", nid);
1096 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
1097 #if 0 /* keep the format */
1099 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0);
1102 EXPORT_SYMBOL_HDA(snd_hda_codec_cleanup_stream);
1105 * amp access functions
1108 /* FIXME: more better hash key? */
1109 #define HDA_HASH_KEY(nid,dir,idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
1110 #define HDA_HASH_PINCAP_KEY(nid) (u32)((nid) + (0x02 << 24))
1111 #define HDA_HASH_PARPCM_KEY(nid) (u32)((nid) + (0x03 << 24))
1112 #define HDA_HASH_PARSTR_KEY(nid) (u32)((nid) + (0x04 << 24))
1113 #define INFO_AMP_CAPS (1<<0)
1114 #define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
1116 /* initialize the hash table */
1117 static void /*__devinit*/ init_hda_cache(struct hda_cache_rec *cache,
1118 unsigned int record_size)
1120 memset(cache, 0, sizeof(*cache));
1121 memset(cache->hash, 0xff, sizeof(cache->hash));
1122 snd_array_init(&cache->buf, record_size, 64);
1125 static void free_hda_cache(struct hda_cache_rec *cache)
1127 snd_array_free(&cache->buf);
1130 /* query the hash. allocate an entry if not found. */
1131 static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache,
1134 u16 idx = key % (u16)ARRAY_SIZE(cache->hash);
1135 u16 cur = cache->hash[idx];
1136 struct hda_cache_head *info;
1138 while (cur != 0xffff) {
1139 info = snd_array_elem(&cache->buf, cur);
1140 if (info->key == key)
1145 /* add a new hash entry */
1146 info = snd_array_new(&cache->buf);
1149 cur = snd_array_index(&cache->buf, info);
1152 info->next = cache->hash[idx];
1153 cache->hash[idx] = cur;
1158 /* query and allocate an amp hash entry */
1159 static inline struct hda_amp_info *
1160 get_alloc_amp_hash(struct hda_codec *codec, u32 key)
1162 return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key);
1166 * query AMP capabilities for the given widget and direction
1168 u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
1170 struct hda_amp_info *info;
1172 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, 0));
1175 if (!(info->head.val & INFO_AMP_CAPS)) {
1176 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
1178 info->amp_caps = snd_hda_param_read(codec, nid,
1179 direction == HDA_OUTPUT ?
1180 AC_PAR_AMP_OUT_CAP :
1183 info->head.val |= INFO_AMP_CAPS;
1185 return info->amp_caps;
1187 EXPORT_SYMBOL_HDA(query_amp_caps);
1189 int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
1192 struct hda_amp_info *info;
1194 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, dir, 0));
1197 info->amp_caps = caps;
1198 info->head.val |= INFO_AMP_CAPS;
1201 EXPORT_SYMBOL_HDA(snd_hda_override_amp_caps);
1204 query_caps_hash(struct hda_codec *codec, hda_nid_t nid, u32 key,
1205 unsigned int (*func)(struct hda_codec *, hda_nid_t))
1207 struct hda_amp_info *info;
1209 info = get_alloc_amp_hash(codec, key);
1212 if (!info->head.val) {
1213 info->head.val |= INFO_AMP_CAPS;
1214 info->amp_caps = func(codec, nid);
1216 return info->amp_caps;
1219 static unsigned int read_pin_cap(struct hda_codec *codec, hda_nid_t nid)
1221 return snd_hda_param_read(codec, nid, AC_PAR_PIN_CAP);
1224 u32 snd_hda_query_pin_caps(struct hda_codec *codec, hda_nid_t nid)
1226 return query_caps_hash(codec, nid, HDA_HASH_PINCAP_KEY(nid),
1229 EXPORT_SYMBOL_HDA(snd_hda_query_pin_caps);
1232 * read the current volume to info
1233 * if the cache exists, read the cache value.
1235 static unsigned int get_vol_mute(struct hda_codec *codec,
1236 struct hda_amp_info *info, hda_nid_t nid,
1237 int ch, int direction, int index)
1241 if (info->head.val & INFO_AMP_VOL(ch))
1242 return info->vol[ch];
1244 parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
1245 parm |= direction == HDA_OUTPUT ? AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
1247 val = snd_hda_codec_read(codec, nid, 0,
1248 AC_VERB_GET_AMP_GAIN_MUTE, parm);
1249 info->vol[ch] = val & 0xff;
1250 info->head.val |= INFO_AMP_VOL(ch);
1251 return info->vol[ch];
1255 * write the current volume in info to the h/w and update the cache
1257 static void put_vol_mute(struct hda_codec *codec, struct hda_amp_info *info,
1258 hda_nid_t nid, int ch, int direction, int index,
1263 parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
1264 parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
1265 parm |= index << AC_AMP_SET_INDEX_SHIFT;
1267 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
1268 info->vol[ch] = val;
1272 * read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
1274 int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
1275 int direction, int index)
1277 struct hda_amp_info *info;
1278 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
1281 return get_vol_mute(codec, info, nid, ch, direction, index);
1283 EXPORT_SYMBOL_HDA(snd_hda_codec_amp_read);
1286 * update the AMP value, mask = bit mask to set, val = the value
1288 int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
1289 int direction, int idx, int mask, int val)
1291 struct hda_amp_info *info;
1293 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, idx));
1297 val |= get_vol_mute(codec, info, nid, ch, direction, idx) & ~mask;
1298 if (info->vol[ch] == val)
1300 put_vol_mute(codec, info, nid, ch, direction, idx, val);
1303 EXPORT_SYMBOL_HDA(snd_hda_codec_amp_update);
1306 * update the AMP stereo with the same mask and value
1308 int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
1309 int direction, int idx, int mask, int val)
1312 for (ch = 0; ch < 2; ch++)
1313 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
1317 EXPORT_SYMBOL_HDA(snd_hda_codec_amp_stereo);
1319 #ifdef SND_HDA_NEEDS_RESUME
1320 /* resume the all amp commands from the cache */
1321 void snd_hda_codec_resume_amp(struct hda_codec *codec)
1323 struct hda_amp_info *buffer = codec->amp_cache.buf.list;
1326 for (i = 0; i < codec->amp_cache.buf.used; i++, buffer++) {
1327 u32 key = buffer->head.key;
1329 unsigned int idx, dir, ch;
1333 idx = (key >> 16) & 0xff;
1334 dir = (key >> 24) & 0xff;
1335 for (ch = 0; ch < 2; ch++) {
1336 if (!(buffer->head.val & INFO_AMP_VOL(ch)))
1338 put_vol_mute(codec, buffer, nid, ch, dir, idx,
1343 EXPORT_SYMBOL_HDA(snd_hda_codec_resume_amp);
1344 #endif /* SND_HDA_NEEDS_RESUME */
1347 int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
1348 struct snd_ctl_elem_info *uinfo)
1350 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1351 u16 nid = get_amp_nid(kcontrol);
1352 u8 chs = get_amp_channels(kcontrol);
1353 int dir = get_amp_direction(kcontrol);
1354 unsigned int ofs = get_amp_offset(kcontrol);
1357 caps = query_amp_caps(codec, nid, dir);
1359 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1361 printk(KERN_WARNING "hda_codec: "
1362 "num_steps = 0 for NID=0x%x (ctl = %s)\n", nid,
1368 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1369 uinfo->count = chs == 3 ? 2 : 1;
1370 uinfo->value.integer.min = 0;
1371 uinfo->value.integer.max = caps;
1374 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_info);
1377 static inline unsigned int
1378 read_amp_value(struct hda_codec *codec, hda_nid_t nid,
1379 int ch, int dir, int idx, unsigned int ofs)
1382 val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
1383 val &= HDA_AMP_VOLMASK;
1392 update_amp_value(struct hda_codec *codec, hda_nid_t nid,
1393 int ch, int dir, int idx, unsigned int ofs,
1398 return snd_hda_codec_amp_update(codec, nid, ch, dir, idx,
1399 HDA_AMP_VOLMASK, val);
1402 int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
1403 struct snd_ctl_elem_value *ucontrol)
1405 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1406 hda_nid_t nid = get_amp_nid(kcontrol);
1407 int chs = get_amp_channels(kcontrol);
1408 int dir = get_amp_direction(kcontrol);
1409 int idx = get_amp_index(kcontrol);
1410 unsigned int ofs = get_amp_offset(kcontrol);
1411 long *valp = ucontrol->value.integer.value;
1414 *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs);
1416 *valp = read_amp_value(codec, nid, 1, dir, idx, ofs);
1419 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_get);
1421 int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
1422 struct snd_ctl_elem_value *ucontrol)
1424 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1425 hda_nid_t nid = get_amp_nid(kcontrol);
1426 int chs = get_amp_channels(kcontrol);
1427 int dir = get_amp_direction(kcontrol);
1428 int idx = get_amp_index(kcontrol);
1429 unsigned int ofs = get_amp_offset(kcontrol);
1430 long *valp = ucontrol->value.integer.value;
1433 snd_hda_power_up(codec);
1435 change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp);
1439 change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp);
1440 snd_hda_power_down(codec);
1443 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_put);
1445 int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
1446 unsigned int size, unsigned int __user *_tlv)
1448 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1449 hda_nid_t nid = get_amp_nid(kcontrol);
1450 int dir = get_amp_direction(kcontrol);
1451 unsigned int ofs = get_amp_offset(kcontrol);
1452 u32 caps, val1, val2;
1454 if (size < 4 * sizeof(unsigned int))
1456 caps = query_amp_caps(codec, nid, dir);
1457 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
1458 val2 = (val2 + 1) * 25;
1459 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
1461 val1 = ((int)val1) * ((int)val2);
1462 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
1464 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
1466 if (put_user(val1, _tlv + 2))
1468 if (put_user(val2, _tlv + 3))
1472 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_tlv);
1475 * set (static) TLV for virtual master volume; recalculated as max 0dB
1477 void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
1483 caps = query_amp_caps(codec, nid, dir);
1484 nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1485 step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
1486 step = (step + 1) * 25;
1487 tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
1488 tlv[1] = 2 * sizeof(unsigned int);
1489 tlv[2] = -nums * step;
1492 EXPORT_SYMBOL_HDA(snd_hda_set_vmaster_tlv);
1494 /* find a mixer control element with the given name */
1495 static struct snd_kcontrol *
1496 _snd_hda_find_mixer_ctl(struct hda_codec *codec,
1497 const char *name, int idx)
1499 struct snd_ctl_elem_id id;
1500 memset(&id, 0, sizeof(id));
1501 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1503 if (snd_BUG_ON(strlen(name) >= sizeof(id.name)))
1505 strcpy(id.name, name);
1506 return snd_ctl_find_id(codec->bus->card, &id);
1509 struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
1512 return _snd_hda_find_mixer_ctl(codec, name, 0);
1514 EXPORT_SYMBOL_HDA(snd_hda_find_mixer_ctl);
1516 /* Add a control element and assign to the codec */
1517 int snd_hda_ctl_add(struct hda_codec *codec, struct snd_kcontrol *kctl)
1520 struct snd_kcontrol **knewp;
1522 err = snd_ctl_add(codec->bus->card, kctl);
1525 knewp = snd_array_new(&codec->mixers);
1531 EXPORT_SYMBOL_HDA(snd_hda_ctl_add);
1533 /* Clear all controls assigned to the given codec */
1534 void snd_hda_ctls_clear(struct hda_codec *codec)
1537 struct snd_kcontrol **kctls = codec->mixers.list;
1538 for (i = 0; i < codec->mixers.used; i++)
1539 snd_ctl_remove(codec->bus->card, kctls[i]);
1540 snd_array_free(&codec->mixers);
1543 /* pseudo device locking
1544 * toggle card->shutdown to allow/disallow the device access (as a hack)
1546 static int hda_lock_devices(struct snd_card *card)
1548 spin_lock(&card->files_lock);
1549 if (card->shutdown) {
1550 spin_unlock(&card->files_lock);
1554 spin_unlock(&card->files_lock);
1558 static void hda_unlock_devices(struct snd_card *card)
1560 spin_lock(&card->files_lock);
1562 spin_unlock(&card->files_lock);
1565 int snd_hda_codec_reset(struct hda_codec *codec)
1567 struct snd_card *card = codec->bus->card;
1570 if (hda_lock_devices(card) < 0)
1572 /* check whether the codec isn't used by any mixer or PCM streams */
1573 if (!list_empty(&card->ctl_files)) {
1574 hda_unlock_devices(card);
1577 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
1578 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
1581 if (cpcm->pcm->streams[0].substream_opened ||
1582 cpcm->pcm->streams[1].substream_opened) {
1583 hda_unlock_devices(card);
1588 /* OK, let it free */
1590 #ifdef CONFIG_SND_HDA_POWER_SAVE
1591 cancel_delayed_work(&codec->power_work);
1592 flush_workqueue(codec->bus->workq);
1594 snd_hda_ctls_clear(codec);
1596 for (i = 0; i < codec->num_pcms; i++) {
1597 if (codec->pcm_info[i].pcm) {
1598 snd_device_free(card, codec->pcm_info[i].pcm);
1599 clear_bit(codec->pcm_info[i].device,
1600 codec->bus->pcm_dev_bits);
1603 if (codec->patch_ops.free)
1604 codec->patch_ops.free(codec);
1605 codec->proc_widget_hook = NULL;
1607 free_hda_cache(&codec->amp_cache);
1608 free_hda_cache(&codec->cmd_cache);
1609 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
1610 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
1611 /* free only driver_pins so that init_pins + user_pins are restored */
1612 snd_array_free(&codec->driver_pins);
1613 restore_pincfgs(codec);
1614 codec->num_pcms = 0;
1615 codec->pcm_info = NULL;
1616 codec->preset = NULL;
1617 memset(&codec->patch_ops, 0, sizeof(codec->patch_ops));
1618 codec->slave_dig_outs = NULL;
1619 codec->spdif_status_reset = 0;
1620 module_put(codec->owner);
1621 codec->owner = NULL;
1623 /* allow device access again */
1624 hda_unlock_devices(card);
1628 /* create a virtual master control and add slaves */
1629 int snd_hda_add_vmaster(struct hda_codec *codec, char *name,
1630 unsigned int *tlv, const char **slaves)
1632 struct snd_kcontrol *kctl;
1636 for (s = slaves; *s && !snd_hda_find_mixer_ctl(codec, *s); s++)
1639 snd_printdd("No slave found for %s\n", name);
1642 kctl = snd_ctl_make_virtual_master(name, tlv);
1645 err = snd_hda_ctl_add(codec, kctl);
1649 for (s = slaves; *s; s++) {
1650 struct snd_kcontrol *sctl;
1653 sctl = _snd_hda_find_mixer_ctl(codec, *s, i);
1656 snd_printdd("Cannot find slave %s, "
1660 err = snd_ctl_add_slave(kctl, sctl);
1668 EXPORT_SYMBOL_HDA(snd_hda_add_vmaster);
1671 int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
1672 struct snd_ctl_elem_info *uinfo)
1674 int chs = get_amp_channels(kcontrol);
1676 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1677 uinfo->count = chs == 3 ? 2 : 1;
1678 uinfo->value.integer.min = 0;
1679 uinfo->value.integer.max = 1;
1682 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_info);
1684 int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
1685 struct snd_ctl_elem_value *ucontrol)
1687 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1688 hda_nid_t nid = get_amp_nid(kcontrol);
1689 int chs = get_amp_channels(kcontrol);
1690 int dir = get_amp_direction(kcontrol);
1691 int idx = get_amp_index(kcontrol);
1692 long *valp = ucontrol->value.integer.value;
1695 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
1696 HDA_AMP_MUTE) ? 0 : 1;
1698 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
1699 HDA_AMP_MUTE) ? 0 : 1;
1702 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_get);
1704 int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
1705 struct snd_ctl_elem_value *ucontrol)
1707 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1708 hda_nid_t nid = get_amp_nid(kcontrol);
1709 int chs = get_amp_channels(kcontrol);
1710 int dir = get_amp_direction(kcontrol);
1711 int idx = get_amp_index(kcontrol);
1712 long *valp = ucontrol->value.integer.value;
1715 snd_hda_power_up(codec);
1717 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
1719 *valp ? 0 : HDA_AMP_MUTE);
1723 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
1725 *valp ? 0 : HDA_AMP_MUTE);
1726 #ifdef CONFIG_SND_HDA_POWER_SAVE
1727 if (codec->patch_ops.check_power_status)
1728 codec->patch_ops.check_power_status(codec, nid);
1730 snd_hda_power_down(codec);
1733 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put);
1736 * bound volume controls
1738 * bind multiple volumes (# indices, from 0)
1741 #define AMP_VAL_IDX_SHIFT 19
1742 #define AMP_VAL_IDX_MASK (0x0f<<19)
1744 int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
1745 struct snd_ctl_elem_value *ucontrol)
1747 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1751 mutex_lock(&codec->control_mutex);
1752 pval = kcontrol->private_value;
1753 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
1754 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
1755 kcontrol->private_value = pval;
1756 mutex_unlock(&codec->control_mutex);
1759 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_get);
1761 int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
1762 struct snd_ctl_elem_value *ucontrol)
1764 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1766 int i, indices, err = 0, change = 0;
1768 mutex_lock(&codec->control_mutex);
1769 pval = kcontrol->private_value;
1770 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
1771 for (i = 0; i < indices; i++) {
1772 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
1773 (i << AMP_VAL_IDX_SHIFT);
1774 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
1779 kcontrol->private_value = pval;
1780 mutex_unlock(&codec->control_mutex);
1781 return err < 0 ? err : change;
1783 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_put);
1786 * generic bound volume/swtich controls
1788 int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
1789 struct snd_ctl_elem_info *uinfo)
1791 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1792 struct hda_bind_ctls *c;
1795 mutex_lock(&codec->control_mutex);
1796 c = (struct hda_bind_ctls *)kcontrol->private_value;
1797 kcontrol->private_value = *c->values;
1798 err = c->ops->info(kcontrol, uinfo);
1799 kcontrol->private_value = (long)c;
1800 mutex_unlock(&codec->control_mutex);
1803 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_info);
1805 int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
1806 struct snd_ctl_elem_value *ucontrol)
1808 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1809 struct hda_bind_ctls *c;
1812 mutex_lock(&codec->control_mutex);
1813 c = (struct hda_bind_ctls *)kcontrol->private_value;
1814 kcontrol->private_value = *c->values;
1815 err = c->ops->get(kcontrol, ucontrol);
1816 kcontrol->private_value = (long)c;
1817 mutex_unlock(&codec->control_mutex);
1820 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_get);
1822 int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
1823 struct snd_ctl_elem_value *ucontrol)
1825 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1826 struct hda_bind_ctls *c;
1827 unsigned long *vals;
1828 int err = 0, change = 0;
1830 mutex_lock(&codec->control_mutex);
1831 c = (struct hda_bind_ctls *)kcontrol->private_value;
1832 for (vals = c->values; *vals; vals++) {
1833 kcontrol->private_value = *vals;
1834 err = c->ops->put(kcontrol, ucontrol);
1839 kcontrol->private_value = (long)c;
1840 mutex_unlock(&codec->control_mutex);
1841 return err < 0 ? err : change;
1843 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_put);
1845 int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
1846 unsigned int size, unsigned int __user *tlv)
1848 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1849 struct hda_bind_ctls *c;
1852 mutex_lock(&codec->control_mutex);
1853 c = (struct hda_bind_ctls *)kcontrol->private_value;
1854 kcontrol->private_value = *c->values;
1855 err = c->ops->tlv(kcontrol, op_flag, size, tlv);
1856 kcontrol->private_value = (long)c;
1857 mutex_unlock(&codec->control_mutex);
1860 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_tlv);
1862 struct hda_ctl_ops snd_hda_bind_vol = {
1863 .info = snd_hda_mixer_amp_volume_info,
1864 .get = snd_hda_mixer_amp_volume_get,
1865 .put = snd_hda_mixer_amp_volume_put,
1866 .tlv = snd_hda_mixer_amp_tlv
1868 EXPORT_SYMBOL_HDA(snd_hda_bind_vol);
1870 struct hda_ctl_ops snd_hda_bind_sw = {
1871 .info = snd_hda_mixer_amp_switch_info,
1872 .get = snd_hda_mixer_amp_switch_get,
1873 .put = snd_hda_mixer_amp_switch_put,
1874 .tlv = snd_hda_mixer_amp_tlv
1876 EXPORT_SYMBOL_HDA(snd_hda_bind_sw);
1879 * SPDIF out controls
1882 static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
1883 struct snd_ctl_elem_info *uinfo)
1885 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1890 static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
1891 struct snd_ctl_elem_value *ucontrol)
1893 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
1894 IEC958_AES0_NONAUDIO |
1895 IEC958_AES0_CON_EMPHASIS_5015 |
1896 IEC958_AES0_CON_NOT_COPYRIGHT;
1897 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
1898 IEC958_AES1_CON_ORIGINAL;
1902 static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
1903 struct snd_ctl_elem_value *ucontrol)
1905 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
1906 IEC958_AES0_NONAUDIO |
1907 IEC958_AES0_PRO_EMPHASIS_5015;
1911 static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
1912 struct snd_ctl_elem_value *ucontrol)
1914 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1916 ucontrol->value.iec958.status[0] = codec->spdif_status & 0xff;
1917 ucontrol->value.iec958.status[1] = (codec->spdif_status >> 8) & 0xff;
1918 ucontrol->value.iec958.status[2] = (codec->spdif_status >> 16) & 0xff;
1919 ucontrol->value.iec958.status[3] = (codec->spdif_status >> 24) & 0xff;
1924 /* convert from SPDIF status bits to HDA SPDIF bits
1925 * bit 0 (DigEn) is always set zero (to be filled later)
1927 static unsigned short convert_from_spdif_status(unsigned int sbits)
1929 unsigned short val = 0;
1931 if (sbits & IEC958_AES0_PROFESSIONAL)
1932 val |= AC_DIG1_PROFESSIONAL;
1933 if (sbits & IEC958_AES0_NONAUDIO)
1934 val |= AC_DIG1_NONAUDIO;
1935 if (sbits & IEC958_AES0_PROFESSIONAL) {
1936 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
1937 IEC958_AES0_PRO_EMPHASIS_5015)
1938 val |= AC_DIG1_EMPHASIS;
1940 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
1941 IEC958_AES0_CON_EMPHASIS_5015)
1942 val |= AC_DIG1_EMPHASIS;
1943 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
1944 val |= AC_DIG1_COPYRIGHT;
1945 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
1946 val |= AC_DIG1_LEVEL;
1947 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
1952 /* convert to SPDIF status bits from HDA SPDIF bits
1954 static unsigned int convert_to_spdif_status(unsigned short val)
1956 unsigned int sbits = 0;
1958 if (val & AC_DIG1_NONAUDIO)
1959 sbits |= IEC958_AES0_NONAUDIO;
1960 if (val & AC_DIG1_PROFESSIONAL)
1961 sbits |= IEC958_AES0_PROFESSIONAL;
1962 if (sbits & IEC958_AES0_PROFESSIONAL) {
1963 if (sbits & AC_DIG1_EMPHASIS)
1964 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
1966 if (val & AC_DIG1_EMPHASIS)
1967 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
1968 if (!(val & AC_DIG1_COPYRIGHT))
1969 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
1970 if (val & AC_DIG1_LEVEL)
1971 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
1972 sbits |= val & (0x7f << 8);
1977 /* set digital convert verbs both for the given NID and its slaves */
1978 static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
1983 snd_hda_codec_write_cache(codec, nid, 0, verb, val);
1984 d = codec->slave_dig_outs;
1988 snd_hda_codec_write_cache(codec, *d, 0, verb, val);
1991 static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
1995 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1);
1997 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2);
2000 static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
2001 struct snd_ctl_elem_value *ucontrol)
2003 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2004 hda_nid_t nid = kcontrol->private_value;
2008 mutex_lock(&codec->spdif_mutex);
2009 codec->spdif_status = ucontrol->value.iec958.status[0] |
2010 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
2011 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
2012 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
2013 val = convert_from_spdif_status(codec->spdif_status);
2014 val |= codec->spdif_ctls & 1;
2015 change = codec->spdif_ctls != val;
2016 codec->spdif_ctls = val;
2019 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
2021 mutex_unlock(&codec->spdif_mutex);
2025 #define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
2027 static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
2028 struct snd_ctl_elem_value *ucontrol)
2030 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2032 ucontrol->value.integer.value[0] = codec->spdif_ctls & AC_DIG1_ENABLE;
2036 static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
2037 struct snd_ctl_elem_value *ucontrol)
2039 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2040 hda_nid_t nid = kcontrol->private_value;
2044 mutex_lock(&codec->spdif_mutex);
2045 val = codec->spdif_ctls & ~AC_DIG1_ENABLE;
2046 if (ucontrol->value.integer.value[0])
2047 val |= AC_DIG1_ENABLE;
2048 change = codec->spdif_ctls != val;
2050 codec->spdif_ctls = val;
2051 set_dig_out_convert(codec, nid, val & 0xff, -1);
2052 /* unmute amp switch (if any) */
2053 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
2054 (val & AC_DIG1_ENABLE))
2055 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
2058 mutex_unlock(&codec->spdif_mutex);
2062 static struct snd_kcontrol_new dig_mixes[] = {
2064 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2065 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2066 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,CON_MASK),
2067 .info = snd_hda_spdif_mask_info,
2068 .get = snd_hda_spdif_cmask_get,
2071 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2072 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2073 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,PRO_MASK),
2074 .info = snd_hda_spdif_mask_info,
2075 .get = snd_hda_spdif_pmask_get,
2078 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2079 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
2080 .info = snd_hda_spdif_mask_info,
2081 .get = snd_hda_spdif_default_get,
2082 .put = snd_hda_spdif_default_put,
2085 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2086 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,SWITCH),
2087 .info = snd_hda_spdif_out_switch_info,
2088 .get = snd_hda_spdif_out_switch_get,
2089 .put = snd_hda_spdif_out_switch_put,
2094 #define SPDIF_MAX_IDX 4 /* 4 instances should be enough to probe */
2097 * snd_hda_create_spdif_out_ctls - create Output SPDIF-related controls
2098 * @codec: the HDA codec
2099 * @nid: audio out widget NID
2101 * Creates controls related with the SPDIF output.
2102 * Called from each patch supporting the SPDIF out.
2104 * Returns 0 if successful, or a negative error code.
2106 int snd_hda_create_spdif_out_ctls(struct hda_codec *codec, hda_nid_t nid)
2109 struct snd_kcontrol *kctl;
2110 struct snd_kcontrol_new *dig_mix;
2113 for (idx = 0; idx < SPDIF_MAX_IDX; idx++) {
2114 if (!_snd_hda_find_mixer_ctl(codec, "IEC958 Playback Switch",
2118 if (idx >= SPDIF_MAX_IDX) {
2119 printk(KERN_ERR "hda_codec: too many IEC958 outputs\n");
2122 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
2123 kctl = snd_ctl_new1(dig_mix, codec);
2126 kctl->id.index = idx;
2127 kctl->private_value = nid;
2128 err = snd_hda_ctl_add(codec, kctl);
2133 snd_hda_codec_read(codec, nid, 0,
2134 AC_VERB_GET_DIGI_CONVERT_1, 0);
2135 codec->spdif_status = convert_to_spdif_status(codec->spdif_ctls);
2138 EXPORT_SYMBOL_HDA(snd_hda_create_spdif_out_ctls);
2141 * SPDIF sharing with analog output
2143 static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
2144 struct snd_ctl_elem_value *ucontrol)
2146 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
2147 ucontrol->value.integer.value[0] = mout->share_spdif;
2151 static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
2152 struct snd_ctl_elem_value *ucontrol)
2154 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
2155 mout->share_spdif = !!ucontrol->value.integer.value[0];
2159 static struct snd_kcontrol_new spdif_share_sw = {
2160 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2161 .name = "IEC958 Default PCM Playback Switch",
2162 .info = snd_ctl_boolean_mono_info,
2163 .get = spdif_share_sw_get,
2164 .put = spdif_share_sw_put,
2167 int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
2168 struct hda_multi_out *mout)
2170 if (!mout->dig_out_nid)
2172 /* ATTENTION: here mout is passed as private_data, instead of codec */
2173 return snd_hda_ctl_add(codec,
2174 snd_ctl_new1(&spdif_share_sw, mout));
2176 EXPORT_SYMBOL_HDA(snd_hda_create_spdif_share_sw);
2182 #define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
2184 static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
2185 struct snd_ctl_elem_value *ucontrol)
2187 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2189 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
2193 static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
2194 struct snd_ctl_elem_value *ucontrol)
2196 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2197 hda_nid_t nid = kcontrol->private_value;
2198 unsigned int val = !!ucontrol->value.integer.value[0];
2201 mutex_lock(&codec->spdif_mutex);
2202 change = codec->spdif_in_enable != val;
2204 codec->spdif_in_enable = val;
2205 snd_hda_codec_write_cache(codec, nid, 0,
2206 AC_VERB_SET_DIGI_CONVERT_1, val);
2208 mutex_unlock(&codec->spdif_mutex);
2212 static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
2213 struct snd_ctl_elem_value *ucontrol)
2215 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2216 hda_nid_t nid = kcontrol->private_value;
2220 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0);
2221 sbits = convert_to_spdif_status(val);
2222 ucontrol->value.iec958.status[0] = sbits;
2223 ucontrol->value.iec958.status[1] = sbits >> 8;
2224 ucontrol->value.iec958.status[2] = sbits >> 16;
2225 ucontrol->value.iec958.status[3] = sbits >> 24;
2229 static struct snd_kcontrol_new dig_in_ctls[] = {
2231 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2232 .name = SNDRV_CTL_NAME_IEC958("",CAPTURE,SWITCH),
2233 .info = snd_hda_spdif_in_switch_info,
2234 .get = snd_hda_spdif_in_switch_get,
2235 .put = snd_hda_spdif_in_switch_put,
2238 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2239 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2240 .name = SNDRV_CTL_NAME_IEC958("",CAPTURE,DEFAULT),
2241 .info = snd_hda_spdif_mask_info,
2242 .get = snd_hda_spdif_in_status_get,
2248 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
2249 * @codec: the HDA codec
2250 * @nid: audio in widget NID
2252 * Creates controls related with the SPDIF input.
2253 * Called from each patch supporting the SPDIF in.
2255 * Returns 0 if successful, or a negative error code.
2257 int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
2260 struct snd_kcontrol *kctl;
2261 struct snd_kcontrol_new *dig_mix;
2264 for (idx = 0; idx < SPDIF_MAX_IDX; idx++) {
2265 if (!_snd_hda_find_mixer_ctl(codec, "IEC958 Capture Switch",
2269 if (idx >= SPDIF_MAX_IDX) {
2270 printk(KERN_ERR "hda_codec: too many IEC958 inputs\n");
2273 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
2274 kctl = snd_ctl_new1(dig_mix, codec);
2277 kctl->private_value = nid;
2278 err = snd_hda_ctl_add(codec, kctl);
2282 codec->spdif_in_enable =
2283 snd_hda_codec_read(codec, nid, 0,
2284 AC_VERB_GET_DIGI_CONVERT_1, 0) &
2288 EXPORT_SYMBOL_HDA(snd_hda_create_spdif_in_ctls);
2290 #ifdef SND_HDA_NEEDS_RESUME
2295 /* build a 32bit cache key with the widget id and the command parameter */
2296 #define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
2297 #define get_cmd_cache_nid(key) ((key) & 0xff)
2298 #define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
2301 * snd_hda_codec_write_cache - send a single command with caching
2302 * @codec: the HDA codec
2303 * @nid: NID to send the command
2304 * @direct: direct flag
2305 * @verb: the verb to send
2306 * @parm: the parameter for the verb
2308 * Send a single command without waiting for response.
2310 * Returns 0 if successful, or a negative error code.
2312 int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
2313 int direct, unsigned int verb, unsigned int parm)
2315 int err = snd_hda_codec_write(codec, nid, direct, verb, parm);
2316 struct hda_cache_head *c;
2321 /* parm may contain the verb stuff for get/set amp */
2322 verb = verb | (parm >> 8);
2324 key = build_cmd_cache_key(nid, verb);
2325 mutex_lock(&codec->bus->cmd_mutex);
2326 c = get_alloc_hash(&codec->cmd_cache, key);
2329 mutex_unlock(&codec->bus->cmd_mutex);
2332 EXPORT_SYMBOL_HDA(snd_hda_codec_write_cache);
2334 /* resume the all commands from the cache */
2335 void snd_hda_codec_resume_cache(struct hda_codec *codec)
2337 struct hda_cache_head *buffer = codec->cmd_cache.buf.list;
2340 for (i = 0; i < codec->cmd_cache.buf.used; i++, buffer++) {
2341 u32 key = buffer->key;
2344 snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
2345 get_cmd_cache_cmd(key), buffer->val);
2348 EXPORT_SYMBOL_HDA(snd_hda_codec_resume_cache);
2351 * snd_hda_sequence_write_cache - sequence writes with caching
2352 * @codec: the HDA codec
2353 * @seq: VERB array to send
2355 * Send the commands sequentially from the given array.
2356 * Thte commands are recorded on cache for power-save and resume.
2357 * The array must be terminated with NID=0.
2359 void snd_hda_sequence_write_cache(struct hda_codec *codec,
2360 const struct hda_verb *seq)
2362 for (; seq->nid; seq++)
2363 snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
2366 EXPORT_SYMBOL_HDA(snd_hda_sequence_write_cache);
2367 #endif /* SND_HDA_NEEDS_RESUME */
2370 * set power state of the codec
2372 static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
2373 unsigned int power_state)
2378 /* this delay seems necessary to avoid click noise at power-down */
2379 if (power_state == AC_PWRST_D3)
2381 snd_hda_codec_read(codec, fg, 0, AC_VERB_SET_POWER_STATE,
2383 /* partial workaround for "azx_get_response timeout" */
2384 if (power_state == AC_PWRST_D0)
2387 nid = codec->start_nid;
2388 for (i = 0; i < codec->num_nodes; i++, nid++) {
2389 unsigned int wcaps = get_wcaps(codec, nid);
2390 if (wcaps & AC_WCAP_POWER) {
2391 unsigned int wid_type = get_wcaps_type(wcaps);
2392 if (power_state == AC_PWRST_D3 &&
2393 wid_type == AC_WID_PIN) {
2394 unsigned int pincap;
2396 * don't power down the widget if it controls
2397 * eapd and EAPD_BTLENABLE is set.
2399 pincap = snd_hda_query_pin_caps(codec, nid);
2400 if (pincap & AC_PINCAP_EAPD) {
2401 int eapd = snd_hda_codec_read(codec,
2403 AC_VERB_GET_EAPD_BTLENABLE, 0);
2409 snd_hda_codec_write(codec, nid, 0,
2410 AC_VERB_SET_POWER_STATE,
2415 if (power_state == AC_PWRST_D0) {
2416 unsigned long end_time;
2419 /* wait until the codec reachs to D0 */
2420 end_time = jiffies + msecs_to_jiffies(500);
2422 state = snd_hda_codec_read(codec, fg, 0,
2423 AC_VERB_GET_POWER_STATE, 0);
2424 if (state == power_state)
2427 } while (time_after_eq(end_time, jiffies));
2431 #ifdef CONFIG_SND_HDA_HWDEP
2432 /* execute additional init verbs */
2433 static void hda_exec_init_verbs(struct hda_codec *codec)
2435 if (codec->init_verbs.list)
2436 snd_hda_sequence_write(codec, codec->init_verbs.list);
2439 static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
2442 #ifdef SND_HDA_NEEDS_RESUME
2444 * call suspend and power-down; used both from PM and power-save
2446 static void hda_call_codec_suspend(struct hda_codec *codec)
2448 if (codec->patch_ops.suspend)
2449 codec->patch_ops.suspend(codec, PMSG_SUSPEND);
2450 hda_set_power_state(codec,
2451 codec->afg ? codec->afg : codec->mfg,
2453 #ifdef CONFIG_SND_HDA_POWER_SAVE
2454 cancel_delayed_work(&codec->power_work);
2455 codec->power_on = 0;
2456 codec->power_transition = 0;
2461 * kick up codec; used both from PM and power-save
2463 static void hda_call_codec_resume(struct hda_codec *codec)
2465 hda_set_power_state(codec,
2466 codec->afg ? codec->afg : codec->mfg,
2468 restore_pincfgs(codec); /* restore all current pin configs */
2469 hda_exec_init_verbs(codec);
2470 if (codec->patch_ops.resume)
2471 codec->patch_ops.resume(codec);
2473 if (codec->patch_ops.init)
2474 codec->patch_ops.init(codec);
2475 snd_hda_codec_resume_amp(codec);
2476 snd_hda_codec_resume_cache(codec);
2479 #endif /* SND_HDA_NEEDS_RESUME */
2483 * snd_hda_build_controls - build mixer controls
2486 * Creates mixer controls for each codec included in the bus.
2488 * Returns 0 if successful, otherwise a negative error code.
2490 int /*__devinit*/ snd_hda_build_controls(struct hda_bus *bus)
2492 struct hda_codec *codec;
2494 list_for_each_entry(codec, &bus->codec_list, list) {
2495 int err = snd_hda_codec_build_controls(codec);
2497 printk(KERN_ERR "hda_codec: cannot build controls"
2498 "for #%d (error %d)\n", codec->addr, err);
2499 err = snd_hda_codec_reset(codec);
2502 "hda_codec: cannot revert codec\n");
2509 EXPORT_SYMBOL_HDA(snd_hda_build_controls);
2511 int snd_hda_codec_build_controls(struct hda_codec *codec)
2514 hda_exec_init_verbs(codec);
2515 /* continue to initialize... */
2516 if (codec->patch_ops.init)
2517 err = codec->patch_ops.init(codec);
2518 if (!err && codec->patch_ops.build_controls)
2519 err = codec->patch_ops.build_controls(codec);
2528 struct hda_rate_tbl {
2530 unsigned int alsa_bits;
2531 unsigned int hda_fmt;
2534 static struct hda_rate_tbl rate_bits[] = {
2535 /* rate in Hz, ALSA rate bitmask, HDA format value */
2537 /* autodetected value used in snd_hda_query_supported_pcm */
2538 { 8000, SNDRV_PCM_RATE_8000, 0x0500 }, /* 1/6 x 48 */
2539 { 11025, SNDRV_PCM_RATE_11025, 0x4300 }, /* 1/4 x 44 */
2540 { 16000, SNDRV_PCM_RATE_16000, 0x0200 }, /* 1/3 x 48 */
2541 { 22050, SNDRV_PCM_RATE_22050, 0x4100 }, /* 1/2 x 44 */
2542 { 32000, SNDRV_PCM_RATE_32000, 0x0a00 }, /* 2/3 x 48 */
2543 { 44100, SNDRV_PCM_RATE_44100, 0x4000 }, /* 44 */
2544 { 48000, SNDRV_PCM_RATE_48000, 0x0000 }, /* 48 */
2545 { 88200, SNDRV_PCM_RATE_88200, 0x4800 }, /* 2 x 44 */
2546 { 96000, SNDRV_PCM_RATE_96000, 0x0800 }, /* 2 x 48 */
2547 { 176400, SNDRV_PCM_RATE_176400, 0x5800 },/* 4 x 44 */
2548 { 192000, SNDRV_PCM_RATE_192000, 0x1800 }, /* 4 x 48 */
2549 #define AC_PAR_PCM_RATE_BITS 11
2550 /* up to bits 10, 384kHZ isn't supported properly */
2552 /* not autodetected value */
2553 { 9600, SNDRV_PCM_RATE_KNOT, 0x0400 }, /* 1/5 x 48 */
2555 { 0 } /* terminator */
2559 * snd_hda_calc_stream_format - calculate format bitset
2560 * @rate: the sample rate
2561 * @channels: the number of channels
2562 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
2563 * @maxbps: the max. bps
2565 * Calculate the format bitset from the given rate, channels and th PCM format.
2567 * Return zero if invalid.
2569 unsigned int snd_hda_calc_stream_format(unsigned int rate,
2570 unsigned int channels,
2571 unsigned int format,
2572 unsigned int maxbps)
2575 unsigned int val = 0;
2577 for (i = 0; rate_bits[i].hz; i++)
2578 if (rate_bits[i].hz == rate) {
2579 val = rate_bits[i].hda_fmt;
2582 if (!rate_bits[i].hz) {
2583 snd_printdd("invalid rate %d\n", rate);
2587 if (channels == 0 || channels > 8) {
2588 snd_printdd("invalid channels %d\n", channels);
2591 val |= channels - 1;
2593 switch (snd_pcm_format_width(format)) {
2594 case 8: val |= 0x00; break;
2595 case 16: val |= 0x10; break;
2599 if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE)
2601 else if (maxbps >= 24)
2607 snd_printdd("invalid format width %d\n",
2608 snd_pcm_format_width(format));
2614 EXPORT_SYMBOL_HDA(snd_hda_calc_stream_format);
2616 static unsigned int get_pcm_param(struct hda_codec *codec, hda_nid_t nid)
2618 unsigned int val = 0;
2619 if (nid != codec->afg &&
2620 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD))
2621 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
2622 if (!val || val == -1)
2623 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
2624 if (!val || val == -1)
2629 static unsigned int query_pcm_param(struct hda_codec *codec, hda_nid_t nid)
2631 return query_caps_hash(codec, nid, HDA_HASH_PARPCM_KEY(nid),
2635 static unsigned int get_stream_param(struct hda_codec *codec, hda_nid_t nid)
2637 unsigned int streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
2638 if (!streams || streams == -1)
2639 streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
2640 if (!streams || streams == -1)
2645 static unsigned int query_stream_param(struct hda_codec *codec, hda_nid_t nid)
2647 return query_caps_hash(codec, nid, HDA_HASH_PARSTR_KEY(nid),
2652 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
2653 * @codec: the HDA codec
2654 * @nid: NID to query
2655 * @ratesp: the pointer to store the detected rate bitflags
2656 * @formatsp: the pointer to store the detected formats
2657 * @bpsp: the pointer to store the detected format widths
2659 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
2660 * or @bsps argument is ignored.
2662 * Returns 0 if successful, otherwise a negative error code.
2664 static int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
2665 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
2667 unsigned int i, val, wcaps;
2669 wcaps = get_wcaps(codec, nid);
2670 val = query_pcm_param(codec, nid);
2674 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
2676 rates |= rate_bits[i].alsa_bits;
2679 snd_printk(KERN_ERR "hda_codec: rates == 0 "
2680 "(nid=0x%x, val=0x%x, ovrd=%i)\n",
2682 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0);
2688 if (formatsp || bpsp) {
2690 unsigned int streams, bps;
2692 streams = query_stream_param(codec, nid);
2697 if (streams & AC_SUPFMT_PCM) {
2698 if (val & AC_SUPPCM_BITS_8) {
2699 formats |= SNDRV_PCM_FMTBIT_U8;
2702 if (val & AC_SUPPCM_BITS_16) {
2703 formats |= SNDRV_PCM_FMTBIT_S16_LE;
2706 if (wcaps & AC_WCAP_DIGITAL) {
2707 if (val & AC_SUPPCM_BITS_32)
2708 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
2709 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
2710 formats |= SNDRV_PCM_FMTBIT_S32_LE;
2711 if (val & AC_SUPPCM_BITS_24)
2713 else if (val & AC_SUPPCM_BITS_20)
2715 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
2716 AC_SUPPCM_BITS_32)) {
2717 formats |= SNDRV_PCM_FMTBIT_S32_LE;
2718 if (val & AC_SUPPCM_BITS_32)
2720 else if (val & AC_SUPPCM_BITS_24)
2722 else if (val & AC_SUPPCM_BITS_20)
2726 if (streams & AC_SUPFMT_FLOAT32) {
2727 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
2731 if (streams == AC_SUPFMT_AC3) {
2732 /* should be exclusive */
2733 /* temporary hack: we have still no proper support
2734 * for the direct AC3 stream...
2736 formats |= SNDRV_PCM_FMTBIT_U8;
2740 snd_printk(KERN_ERR "hda_codec: formats == 0 "
2741 "(nid=0x%x, val=0x%x, ovrd=%i, "
2744 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0,
2749 *formatsp = formats;
2758 * snd_hda_is_supported_format - check whether the given node supports
2761 * Returns 1 if supported, 0 if not.
2763 int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
2764 unsigned int format)
2767 unsigned int val = 0, rate, stream;
2769 val = query_pcm_param(codec, nid);
2773 rate = format & 0xff00;
2774 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
2775 if (rate_bits[i].hda_fmt == rate) {
2780 if (i >= AC_PAR_PCM_RATE_BITS)
2783 stream = query_stream_param(codec, nid);
2787 if (stream & AC_SUPFMT_PCM) {
2788 switch (format & 0xf0) {
2790 if (!(val & AC_SUPPCM_BITS_8))
2794 if (!(val & AC_SUPPCM_BITS_16))
2798 if (!(val & AC_SUPPCM_BITS_20))
2802 if (!(val & AC_SUPPCM_BITS_24))
2806 if (!(val & AC_SUPPCM_BITS_32))
2813 /* FIXME: check for float32 and AC3? */
2818 EXPORT_SYMBOL_HDA(snd_hda_is_supported_format);
2823 static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
2824 struct hda_codec *codec,
2825 struct snd_pcm_substream *substream)
2830 static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
2831 struct hda_codec *codec,
2832 unsigned int stream_tag,
2833 unsigned int format,
2834 struct snd_pcm_substream *substream)
2836 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
2840 static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
2841 struct hda_codec *codec,
2842 struct snd_pcm_substream *substream)
2844 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
2848 static int set_pcm_default_values(struct hda_codec *codec,
2849 struct hda_pcm_stream *info)
2853 /* query support PCM information from the given NID */
2854 if (info->nid && (!info->rates || !info->formats)) {
2855 err = snd_hda_query_supported_pcm(codec, info->nid,
2856 info->rates ? NULL : &info->rates,
2857 info->formats ? NULL : &info->formats,
2858 info->maxbps ? NULL : &info->maxbps);
2862 if (info->ops.open == NULL)
2863 info->ops.open = hda_pcm_default_open_close;
2864 if (info->ops.close == NULL)
2865 info->ops.close = hda_pcm_default_open_close;
2866 if (info->ops.prepare == NULL) {
2867 if (snd_BUG_ON(!info->nid))
2869 info->ops.prepare = hda_pcm_default_prepare;
2871 if (info->ops.cleanup == NULL) {
2872 if (snd_BUG_ON(!info->nid))
2874 info->ops.cleanup = hda_pcm_default_cleanup;
2880 * get the empty PCM device number to assign
2882 static int get_empty_pcm_device(struct hda_bus *bus, int type)
2884 static const char *dev_name[HDA_PCM_NTYPES] = {
2885 "Audio", "SPDIF", "HDMI", "Modem"
2887 /* starting device index for each PCM type */
2888 static int dev_idx[HDA_PCM_NTYPES] = {
2889 [HDA_PCM_TYPE_AUDIO] = 0,
2890 [HDA_PCM_TYPE_SPDIF] = 1,
2891 [HDA_PCM_TYPE_HDMI] = 3,
2892 [HDA_PCM_TYPE_MODEM] = 6
2894 /* normal audio device indices; not linear to keep compatibility */
2895 static int audio_idx[4] = { 0, 2, 4, 5 };
2899 case HDA_PCM_TYPE_AUDIO:
2900 for (i = 0; i < ARRAY_SIZE(audio_idx); i++) {
2902 if (!test_bit(dev, bus->pcm_dev_bits))
2905 snd_printk(KERN_WARNING "Too many audio devices\n");
2907 case HDA_PCM_TYPE_SPDIF:
2908 case HDA_PCM_TYPE_HDMI:
2909 case HDA_PCM_TYPE_MODEM:
2910 dev = dev_idx[type];
2911 if (test_bit(dev, bus->pcm_dev_bits)) {
2912 snd_printk(KERN_WARNING "%s already defined\n",
2918 snd_printk(KERN_WARNING "Invalid PCM type %d\n", type);
2922 set_bit(dev, bus->pcm_dev_bits);
2927 * attach a new PCM stream
2929 static int snd_hda_attach_pcm(struct hda_codec *codec, struct hda_pcm *pcm)
2931 struct hda_bus *bus = codec->bus;
2932 struct hda_pcm_stream *info;
2935 if (snd_BUG_ON(!pcm->name))
2937 for (stream = 0; stream < 2; stream++) {
2938 info = &pcm->stream[stream];
2939 if (info->substreams) {
2940 err = set_pcm_default_values(codec, info);
2945 return bus->ops.attach_pcm(bus, codec, pcm);
2948 /* assign all PCMs of the given codec */
2949 int snd_hda_codec_build_pcms(struct hda_codec *codec)
2954 if (!codec->num_pcms) {
2955 if (!codec->patch_ops.build_pcms)
2957 err = codec->patch_ops.build_pcms(codec);
2959 printk(KERN_ERR "hda_codec: cannot build PCMs"
2960 "for #%d (error %d)\n", codec->addr, err);
2961 err = snd_hda_codec_reset(codec);
2964 "hda_codec: cannot revert codec\n");
2969 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
2970 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
2973 if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
2974 continue; /* no substreams assigned */
2977 dev = get_empty_pcm_device(codec->bus, cpcm->pcm_type);
2979 continue; /* no fatal error */
2981 err = snd_hda_attach_pcm(codec, cpcm);
2983 printk(KERN_ERR "hda_codec: cannot attach "
2984 "PCM stream %d for codec #%d\n",
2986 continue; /* no fatal error */
2994 * snd_hda_build_pcms - build PCM information
2997 * Create PCM information for each codec included in the bus.
2999 * The build_pcms codec patch is requested to set up codec->num_pcms and
3000 * codec->pcm_info properly. The array is referred by the top-level driver
3001 * to create its PCM instances.
3002 * The allocated codec->pcm_info should be released in codec->patch_ops.free
3005 * At least, substreams, channels_min and channels_max must be filled for
3006 * each stream. substreams = 0 indicates that the stream doesn't exist.
3007 * When rates and/or formats are zero, the supported values are queried
3008 * from the given nid. The nid is used also by the default ops.prepare
3009 * and ops.cleanup callbacks.
3011 * The driver needs to call ops.open in its open callback. Similarly,
3012 * ops.close is supposed to be called in the close callback.
3013 * ops.prepare should be called in the prepare or hw_params callback
3014 * with the proper parameters for set up.
3015 * ops.cleanup should be called in hw_free for clean up of streams.
3017 * This function returns 0 if successfull, or a negative error code.
3019 int __devinit snd_hda_build_pcms(struct hda_bus *bus)
3021 struct hda_codec *codec;
3023 list_for_each_entry(codec, &bus->codec_list, list) {
3024 int err = snd_hda_codec_build_pcms(codec);
3030 EXPORT_SYMBOL_HDA(snd_hda_build_pcms);
3033 * snd_hda_check_board_config - compare the current codec with the config table
3034 * @codec: the HDA codec
3035 * @num_configs: number of config enums
3036 * @models: array of model name strings
3037 * @tbl: configuration table, terminated by null entries
3039 * Compares the modelname or PCI subsystem id of the current codec with the
3040 * given configuration table. If a matching entry is found, returns its
3041 * config value (supposed to be 0 or positive).
3043 * If no entries are matching, the function returns a negative value.
3045 int snd_hda_check_board_config(struct hda_codec *codec,
3046 int num_configs, const char **models,
3047 const struct snd_pci_quirk *tbl)
3049 if (codec->modelname && models) {
3051 for (i = 0; i < num_configs; i++) {
3053 !strcmp(codec->modelname, models[i])) {
3054 snd_printd(KERN_INFO "hda_codec: model '%s' is "
3055 "selected\n", models[i]);
3061 if (!codec->bus->pci || !tbl)
3064 tbl = snd_pci_quirk_lookup(codec->bus->pci, tbl);
3067 if (tbl->value >= 0 && tbl->value < num_configs) {
3068 #ifdef CONFIG_SND_DEBUG_VERBOSE
3070 const char *model = NULL;
3072 model = models[tbl->value];
3074 sprintf(tmp, "#%d", tbl->value);
3077 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
3078 "for config %x:%x (%s)\n",
3079 model, tbl->subvendor, tbl->subdevice,
3080 (tbl->name ? tbl->name : "Unknown device"));
3086 EXPORT_SYMBOL_HDA(snd_hda_check_board_config);
3089 * snd_hda_check_board_codec_sid_config - compare the current codec
3090 subsystem ID with the
3093 This is important for Gateway notebooks with SB450 HDA Audio
3094 where the vendor ID of the PCI device is:
3095 ATI Technologies Inc SB450 HDA Audio [1002:437b]
3096 and the vendor/subvendor are found only at the codec.
3098 * @codec: the HDA codec
3099 * @num_configs: number of config enums
3100 * @models: array of model name strings
3101 * @tbl: configuration table, terminated by null entries
3103 * Compares the modelname or PCI subsystem id of the current codec with the
3104 * given configuration table. If a matching entry is found, returns its
3105 * config value (supposed to be 0 or positive).
3107 * If no entries are matching, the function returns a negative value.
3109 int snd_hda_check_board_codec_sid_config(struct hda_codec *codec,
3110 int num_configs, const char **models,
3111 const struct snd_pci_quirk *tbl)
3113 const struct snd_pci_quirk *q;
3115 /* Search for codec ID */
3116 for (q = tbl; q->subvendor; q++) {
3117 unsigned long vendorid = (q->subdevice) | (q->subvendor << 16);
3119 if (vendorid == codec->subsystem_id)
3128 if (tbl->value >= 0 && tbl->value < num_configs) {
3129 #ifdef CONFIG_SND_DEBUG_DETECT
3131 const char *model = NULL;
3133 model = models[tbl->value];
3135 sprintf(tmp, "#%d", tbl->value);
3138 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
3139 "for config %x:%x (%s)\n",
3140 model, tbl->subvendor, tbl->subdevice,
3141 (tbl->name ? tbl->name : "Unknown device"));
3147 EXPORT_SYMBOL_HDA(snd_hda_check_board_codec_sid_config);
3150 * snd_hda_add_new_ctls - create controls from the array
3151 * @codec: the HDA codec
3152 * @knew: the array of struct snd_kcontrol_new
3154 * This helper function creates and add new controls in the given array.
3155 * The array must be terminated with an empty entry as terminator.
3157 * Returns 0 if successful, or a negative error code.
3159 int snd_hda_add_new_ctls(struct hda_codec *codec, struct snd_kcontrol_new *knew)
3163 for (; knew->name; knew++) {
3164 struct snd_kcontrol *kctl;
3165 kctl = snd_ctl_new1(knew, codec);
3168 err = snd_hda_ctl_add(codec, kctl);
3172 kctl = snd_ctl_new1(knew, codec);
3175 kctl->id.device = codec->addr;
3176 err = snd_hda_ctl_add(codec, kctl);
3183 EXPORT_SYMBOL_HDA(snd_hda_add_new_ctls);
3185 #ifdef CONFIG_SND_HDA_POWER_SAVE
3186 static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
3187 unsigned int power_state);
3189 static void hda_power_work(struct work_struct *work)
3191 struct hda_codec *codec =
3192 container_of(work, struct hda_codec, power_work.work);
3193 struct hda_bus *bus = codec->bus;
3195 if (!codec->power_on || codec->power_count) {
3196 codec->power_transition = 0;
3200 hda_call_codec_suspend(codec);
3201 if (bus->ops.pm_notify)
3202 bus->ops.pm_notify(bus);
3205 static void hda_keep_power_on(struct hda_codec *codec)
3207 codec->power_count++;
3208 codec->power_on = 1;
3211 void snd_hda_power_up(struct hda_codec *codec)
3213 struct hda_bus *bus = codec->bus;
3215 codec->power_count++;
3216 if (codec->power_on || codec->power_transition)
3219 codec->power_on = 1;
3220 if (bus->ops.pm_notify)
3221 bus->ops.pm_notify(bus);
3222 hda_call_codec_resume(codec);
3223 cancel_delayed_work(&codec->power_work);
3224 codec->power_transition = 0;
3226 EXPORT_SYMBOL_HDA(snd_hda_power_up);
3228 #define power_save(codec) \
3229 ((codec)->bus->power_save ? *(codec)->bus->power_save : 0)
3231 #define power_save(codec) \
3232 ((codec)->bus->power_save ? *(codec)->bus->power_save : 0)
3234 void snd_hda_power_down(struct hda_codec *codec)
3236 --codec->power_count;
3237 if (!codec->power_on || codec->power_count || codec->power_transition)
3239 if (power_save(codec)) {
3240 codec->power_transition = 1; /* avoid reentrance */
3241 queue_delayed_work(codec->bus->workq, &codec->power_work,
3242 msecs_to_jiffies(power_save(codec) * 1000));
3245 EXPORT_SYMBOL_HDA(snd_hda_power_down);
3247 int snd_hda_check_amp_list_power(struct hda_codec *codec,
3248 struct hda_loopback_check *check,
3251 struct hda_amp_list *p;
3254 if (!check->amplist)
3256 for (p = check->amplist; p->nid; p++) {
3261 return 0; /* nothing changed */
3263 for (p = check->amplist; p->nid; p++) {
3264 for (ch = 0; ch < 2; ch++) {
3265 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
3267 if (!(v & HDA_AMP_MUTE) && v > 0) {
3268 if (!check->power_on) {
3269 check->power_on = 1;
3270 snd_hda_power_up(codec);
3276 if (check->power_on) {
3277 check->power_on = 0;
3278 snd_hda_power_down(codec);
3282 EXPORT_SYMBOL_HDA(snd_hda_check_amp_list_power);
3286 * Channel mode helper
3288 int snd_hda_ch_mode_info(struct hda_codec *codec,
3289 struct snd_ctl_elem_info *uinfo,
3290 const struct hda_channel_mode *chmode,
3293 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
3295 uinfo->value.enumerated.items = num_chmodes;
3296 if (uinfo->value.enumerated.item >= num_chmodes)
3297 uinfo->value.enumerated.item = num_chmodes - 1;
3298 sprintf(uinfo->value.enumerated.name, "%dch",
3299 chmode[uinfo->value.enumerated.item].channels);
3302 EXPORT_SYMBOL_HDA(snd_hda_ch_mode_info);
3304 int snd_hda_ch_mode_get(struct hda_codec *codec,
3305 struct snd_ctl_elem_value *ucontrol,
3306 const struct hda_channel_mode *chmode,
3312 for (i = 0; i < num_chmodes; i++) {
3313 if (max_channels == chmode[i].channels) {
3314 ucontrol->value.enumerated.item[0] = i;
3320 EXPORT_SYMBOL_HDA(snd_hda_ch_mode_get);
3322 int snd_hda_ch_mode_put(struct hda_codec *codec,
3323 struct snd_ctl_elem_value *ucontrol,
3324 const struct hda_channel_mode *chmode,
3330 mode = ucontrol->value.enumerated.item[0];
3331 if (mode >= num_chmodes)
3333 if (*max_channelsp == chmode[mode].channels)
3335 /* change the current channel setting */
3336 *max_channelsp = chmode[mode].channels;
3337 if (chmode[mode].sequence)
3338 snd_hda_sequence_write_cache(codec, chmode[mode].sequence);
3341 EXPORT_SYMBOL_HDA(snd_hda_ch_mode_put);
3346 int snd_hda_input_mux_info(const struct hda_input_mux *imux,
3347 struct snd_ctl_elem_info *uinfo)
3351 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
3353 uinfo->value.enumerated.items = imux->num_items;
3354 if (!imux->num_items)
3356 index = uinfo->value.enumerated.item;
3357 if (index >= imux->num_items)
3358 index = imux->num_items - 1;
3359 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
3362 EXPORT_SYMBOL_HDA(snd_hda_input_mux_info);
3364 int snd_hda_input_mux_put(struct hda_codec *codec,
3365 const struct hda_input_mux *imux,
3366 struct snd_ctl_elem_value *ucontrol,
3368 unsigned int *cur_val)
3372 if (!imux->num_items)
3374 idx = ucontrol->value.enumerated.item[0];
3375 if (idx >= imux->num_items)
3376 idx = imux->num_items - 1;
3377 if (*cur_val == idx)
3379 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
3380 imux->items[idx].index);
3384 EXPORT_SYMBOL_HDA(snd_hda_input_mux_put);
3388 * Multi-channel / digital-out PCM helper functions
3391 /* setup SPDIF output stream */
3392 static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
3393 unsigned int stream_tag, unsigned int format)
3395 /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */
3396 if (codec->spdif_status_reset && (codec->spdif_ctls & AC_DIG1_ENABLE))
3397 set_dig_out_convert(codec, nid,
3398 codec->spdif_ctls & ~AC_DIG1_ENABLE & 0xff,
3400 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
3401 if (codec->slave_dig_outs) {
3403 for (d = codec->slave_dig_outs; *d; d++)
3404 snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
3407 /* turn on again (if needed) */
3408 if (codec->spdif_status_reset && (codec->spdif_ctls & AC_DIG1_ENABLE))
3409 set_dig_out_convert(codec, nid,
3410 codec->spdif_ctls & 0xff, -1);
3413 static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
3415 snd_hda_codec_cleanup_stream(codec, nid);
3416 if (codec->slave_dig_outs) {
3418 for (d = codec->slave_dig_outs; *d; d++)
3419 snd_hda_codec_cleanup_stream(codec, *d);
3424 * open the digital out in the exclusive mode
3426 int snd_hda_multi_out_dig_open(struct hda_codec *codec,
3427 struct hda_multi_out *mout)
3429 mutex_lock(&codec->spdif_mutex);
3430 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
3431 /* already opened as analog dup; reset it once */
3432 cleanup_dig_out_stream(codec, mout->dig_out_nid);
3433 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
3434 mutex_unlock(&codec->spdif_mutex);
3437 EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_open);
3439 int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
3440 struct hda_multi_out *mout,
3441 unsigned int stream_tag,
3442 unsigned int format,
3443 struct snd_pcm_substream *substream)
3445 mutex_lock(&codec->spdif_mutex);
3446 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
3447 mutex_unlock(&codec->spdif_mutex);
3450 EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_prepare);
3452 int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec,
3453 struct hda_multi_out *mout)
3455 mutex_lock(&codec->spdif_mutex);
3456 cleanup_dig_out_stream(codec, mout->dig_out_nid);
3457 mutex_unlock(&codec->spdif_mutex);
3460 EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_cleanup);
3463 * release the digital out
3465 int snd_hda_multi_out_dig_close(struct hda_codec *codec,
3466 struct hda_multi_out *mout)
3468 mutex_lock(&codec->spdif_mutex);
3469 mout->dig_out_used = 0;
3470 mutex_unlock(&codec->spdif_mutex);
3473 EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_close);
3476 * set up more restrictions for analog out
3478 int snd_hda_multi_out_analog_open(struct hda_codec *codec,
3479 struct hda_multi_out *mout,
3480 struct snd_pcm_substream *substream,
3481 struct hda_pcm_stream *hinfo)
3483 struct snd_pcm_runtime *runtime = substream->runtime;
3484 runtime->hw.channels_max = mout->max_channels;
3485 if (mout->dig_out_nid) {
3486 if (!mout->analog_rates) {
3487 mout->analog_rates = hinfo->rates;
3488 mout->analog_formats = hinfo->formats;
3489 mout->analog_maxbps = hinfo->maxbps;
3491 runtime->hw.rates = mout->analog_rates;
3492 runtime->hw.formats = mout->analog_formats;
3493 hinfo->maxbps = mout->analog_maxbps;
3495 if (!mout->spdif_rates) {
3496 snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
3498 &mout->spdif_formats,
3499 &mout->spdif_maxbps);
3501 mutex_lock(&codec->spdif_mutex);
3502 if (mout->share_spdif) {
3503 if ((runtime->hw.rates & mout->spdif_rates) &&
3504 (runtime->hw.formats & mout->spdif_formats)) {
3505 runtime->hw.rates &= mout->spdif_rates;
3506 runtime->hw.formats &= mout->spdif_formats;
3507 if (mout->spdif_maxbps < hinfo->maxbps)
3508 hinfo->maxbps = mout->spdif_maxbps;
3510 mout->share_spdif = 0;
3511 /* FIXME: need notify? */
3514 mutex_unlock(&codec->spdif_mutex);
3516 return snd_pcm_hw_constraint_step(substream->runtime, 0,
3517 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
3519 EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_open);
3522 * set up the i/o for analog out
3523 * when the digital out is available, copy the front out to digital out, too.
3525 int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
3526 struct hda_multi_out *mout,
3527 unsigned int stream_tag,
3528 unsigned int format,
3529 struct snd_pcm_substream *substream)
3531 hda_nid_t *nids = mout->dac_nids;
3532 int chs = substream->runtime->channels;
3535 mutex_lock(&codec->spdif_mutex);
3536 if (mout->dig_out_nid && mout->share_spdif &&
3537 mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
3539 snd_hda_is_supported_format(codec, mout->dig_out_nid,
3541 !(codec->spdif_status & IEC958_AES0_NONAUDIO)) {
3542 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
3543 setup_dig_out_stream(codec, mout->dig_out_nid,
3544 stream_tag, format);
3546 mout->dig_out_used = 0;
3547 cleanup_dig_out_stream(codec, mout->dig_out_nid);
3550 mutex_unlock(&codec->spdif_mutex);
3553 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
3555 if (!mout->no_share_stream &&
3556 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
3557 /* headphone out will just decode front left/right (stereo) */
3558 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
3560 /* extra outputs copied from front */
3561 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
3562 if (!mout->no_share_stream && mout->extra_out_nid[i])
3563 snd_hda_codec_setup_stream(codec,
3564 mout->extra_out_nid[i],
3565 stream_tag, 0, format);
3568 for (i = 1; i < mout->num_dacs; i++) {
3569 if (chs >= (i + 1) * 2) /* independent out */
3570 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
3572 else if (!mout->no_share_stream) /* copy front */
3573 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
3578 EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_prepare);
3581 * clean up the setting for analog out
3583 int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
3584 struct hda_multi_out *mout)
3586 hda_nid_t *nids = mout->dac_nids;
3589 for (i = 0; i < mout->num_dacs; i++)
3590 snd_hda_codec_cleanup_stream(codec, nids[i]);
3592 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
3593 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
3594 if (mout->extra_out_nid[i])
3595 snd_hda_codec_cleanup_stream(codec,
3596 mout->extra_out_nid[i]);
3597 mutex_lock(&codec->spdif_mutex);
3598 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
3599 cleanup_dig_out_stream(codec, mout->dig_out_nid);
3600 mout->dig_out_used = 0;
3602 mutex_unlock(&codec->spdif_mutex);
3605 EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_cleanup);
3608 * Helper for automatic pin configuration
3611 static int is_in_nid_list(hda_nid_t nid, hda_nid_t *list)
3613 for (; *list; list++)
3621 * Sort an associated group of pins according to their sequence numbers.
3623 static void sort_pins_by_sequence(hda_nid_t * pins, short * sequences,
3630 for (i = 0; i < num_pins; i++) {
3631 for (j = i + 1; j < num_pins; j++) {
3632 if (sequences[i] > sequences[j]) {
3634 sequences[i] = sequences[j];
3646 * Parse all pin widgets and store the useful pin nids to cfg
3648 * The number of line-outs or any primary output is stored in line_outs,
3649 * and the corresponding output pins are assigned to line_out_pins[],
3650 * in the order of front, rear, CLFE, side, ...
3652 * If more extra outputs (speaker and headphone) are found, the pins are
3653 * assisnged to hp_pins[] and speaker_pins[], respectively. If no line-out jack
3654 * is detected, one of speaker of HP pins is assigned as the primary
3655 * output, i.e. to line_out_pins[0]. So, line_outs is always positive
3656 * if any analog output exists.
3658 * The analog input pins are assigned to input_pins array.
3659 * The digital input/output pins are assigned to dig_in_pin and dig_out_pin,
3662 int snd_hda_parse_pin_def_config(struct hda_codec *codec,
3663 struct auto_pin_cfg *cfg,
3664 hda_nid_t *ignore_nids)
3666 hda_nid_t nid, end_nid;
3667 short seq, assoc_line_out, assoc_speaker;
3668 short sequences_line_out[ARRAY_SIZE(cfg->line_out_pins)];
3669 short sequences_speaker[ARRAY_SIZE(cfg->speaker_pins)];
3670 short sequences_hp[ARRAY_SIZE(cfg->hp_pins)];
3672 memset(cfg, 0, sizeof(*cfg));
3674 memset(sequences_line_out, 0, sizeof(sequences_line_out));
3675 memset(sequences_speaker, 0, sizeof(sequences_speaker));
3676 memset(sequences_hp, 0, sizeof(sequences_hp));
3677 assoc_line_out = assoc_speaker = 0;
3679 end_nid = codec->start_nid + codec->num_nodes;
3680 for (nid = codec->start_nid; nid < end_nid; nid++) {
3681 unsigned int wid_caps = get_wcaps(codec, nid);
3682 unsigned int wid_type = get_wcaps_type(wid_caps);
3683 unsigned int def_conf;
3686 /* read all default configuration for pin complex */
3687 if (wid_type != AC_WID_PIN)
3689 /* ignore the given nids (e.g. pc-beep returns error) */
3690 if (ignore_nids && is_in_nid_list(nid, ignore_nids))
3693 def_conf = snd_hda_codec_get_pincfg(codec, nid);
3694 if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE)
3696 loc = get_defcfg_location(def_conf);
3697 switch (get_defcfg_device(def_conf)) {
3698 case AC_JACK_LINE_OUT:
3699 seq = get_defcfg_sequence(def_conf);
3700 assoc = get_defcfg_association(def_conf);
3702 if (!(wid_caps & AC_WCAP_STEREO))
3703 if (!cfg->mono_out_pin)
3704 cfg->mono_out_pin = nid;
3707 if (!assoc_line_out)
3708 assoc_line_out = assoc;
3709 else if (assoc_line_out != assoc)
3711 if (cfg->line_outs >= ARRAY_SIZE(cfg->line_out_pins))
3713 cfg->line_out_pins[cfg->line_outs] = nid;
3714 sequences_line_out[cfg->line_outs] = seq;
3717 case AC_JACK_SPEAKER:
3718 seq = get_defcfg_sequence(def_conf);
3719 assoc = get_defcfg_association(def_conf);
3722 if (! assoc_speaker)
3723 assoc_speaker = assoc;
3724 else if (assoc_speaker != assoc)
3726 if (cfg->speaker_outs >= ARRAY_SIZE(cfg->speaker_pins))
3728 cfg->speaker_pins[cfg->speaker_outs] = nid;
3729 sequences_speaker[cfg->speaker_outs] = seq;
3730 cfg->speaker_outs++;
3732 case AC_JACK_HP_OUT:
3733 seq = get_defcfg_sequence(def_conf);
3734 assoc = get_defcfg_association(def_conf);
3735 if (cfg->hp_outs >= ARRAY_SIZE(cfg->hp_pins))
3737 cfg->hp_pins[cfg->hp_outs] = nid;
3738 sequences_hp[cfg->hp_outs] = (assoc << 4) | seq;
3741 case AC_JACK_MIC_IN: {
3743 if (loc == AC_JACK_LOC_FRONT) {
3744 preferred = AUTO_PIN_FRONT_MIC;
3747 preferred = AUTO_PIN_MIC;
3748 alt = AUTO_PIN_FRONT_MIC;
3750 if (!cfg->input_pins[preferred])
3751 cfg->input_pins[preferred] = nid;
3752 else if (!cfg->input_pins[alt])
3753 cfg->input_pins[alt] = nid;
3756 case AC_JACK_LINE_IN:
3757 if (loc == AC_JACK_LOC_FRONT)
3758 cfg->input_pins[AUTO_PIN_FRONT_LINE] = nid;
3760 cfg->input_pins[AUTO_PIN_LINE] = nid;
3763 cfg->input_pins[AUTO_PIN_CD] = nid;
3766 cfg->input_pins[AUTO_PIN_AUX] = nid;
3768 case AC_JACK_SPDIF_OUT:
3769 case AC_JACK_DIG_OTHER_OUT:
3770 if (cfg->dig_outs >= ARRAY_SIZE(cfg->dig_out_pins))
3772 cfg->dig_out_pins[cfg->dig_outs] = nid;
3773 cfg->dig_out_type[cfg->dig_outs] =
3774 (loc == AC_JACK_LOC_HDMI) ?
3775 HDA_PCM_TYPE_HDMI : HDA_PCM_TYPE_SPDIF;
3778 case AC_JACK_SPDIF_IN:
3779 case AC_JACK_DIG_OTHER_IN:
3780 cfg->dig_in_pin = nid;
3781 if (loc == AC_JACK_LOC_HDMI)
3782 cfg->dig_in_type = HDA_PCM_TYPE_HDMI;
3784 cfg->dig_in_type = HDA_PCM_TYPE_SPDIF;
3790 * If no line-out is defined but multiple HPs are found,
3791 * some of them might be the real line-outs.
3793 if (!cfg->line_outs && cfg->hp_outs > 1) {
3795 while (i < cfg->hp_outs) {
3796 /* The real HPs should have the sequence 0x0f */
3797 if ((sequences_hp[i] & 0x0f) == 0x0f) {
3801 /* Move it to the line-out table */
3802 cfg->line_out_pins[cfg->line_outs] = cfg->hp_pins[i];
3803 sequences_line_out[cfg->line_outs] = sequences_hp[i];
3806 memmove(cfg->hp_pins + i, cfg->hp_pins + i + 1,
3807 sizeof(cfg->hp_pins[0]) * (cfg->hp_outs - i));
3808 memmove(sequences_hp + i - 1, sequences_hp + i,
3809 sizeof(sequences_hp[0]) * (cfg->hp_outs - i));
3813 /* sort by sequence */
3814 sort_pins_by_sequence(cfg->line_out_pins, sequences_line_out,
3816 sort_pins_by_sequence(cfg->speaker_pins, sequences_speaker,
3818 sort_pins_by_sequence(cfg->hp_pins, sequences_hp,
3821 /* if we have only one mic, make it AUTO_PIN_MIC */
3822 if (!cfg->input_pins[AUTO_PIN_MIC] &&
3823 cfg->input_pins[AUTO_PIN_FRONT_MIC]) {
3824 cfg->input_pins[AUTO_PIN_MIC] =
3825 cfg->input_pins[AUTO_PIN_FRONT_MIC];
3826 cfg->input_pins[AUTO_PIN_FRONT_MIC] = 0;
3828 /* ditto for line-in */
3829 if (!cfg->input_pins[AUTO_PIN_LINE] &&
3830 cfg->input_pins[AUTO_PIN_FRONT_LINE]) {
3831 cfg->input_pins[AUTO_PIN_LINE] =
3832 cfg->input_pins[AUTO_PIN_FRONT_LINE];
3833 cfg->input_pins[AUTO_PIN_FRONT_LINE] = 0;
3837 * FIX-UP: if no line-outs are detected, try to use speaker or HP pin
3838 * as a primary output
3840 if (!cfg->line_outs) {
3841 if (cfg->speaker_outs) {
3842 cfg->line_outs = cfg->speaker_outs;
3843 memcpy(cfg->line_out_pins, cfg->speaker_pins,
3844 sizeof(cfg->speaker_pins));
3845 cfg->speaker_outs = 0;
3846 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
3847 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
3848 } else if (cfg->hp_outs) {
3849 cfg->line_outs = cfg->hp_outs;
3850 memcpy(cfg->line_out_pins, cfg->hp_pins,
3851 sizeof(cfg->hp_pins));
3853 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
3854 cfg->line_out_type = AUTO_PIN_HP_OUT;
3858 /* Reorder the surround channels
3859 * ALSA sequence is front/surr/clfe/side
3861 * 4-ch: front/surr => OK as it is
3862 * 6-ch: front/clfe/surr
3863 * 8-ch: front/clfe/rear/side|fc
3865 switch (cfg->line_outs) {
3868 nid = cfg->line_out_pins[1];
3869 cfg->line_out_pins[1] = cfg->line_out_pins[2];
3870 cfg->line_out_pins[2] = nid;
3875 * debug prints of the parsed results
3877 snd_printd("autoconfig: line_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
3878 cfg->line_outs, cfg->line_out_pins[0], cfg->line_out_pins[1],
3879 cfg->line_out_pins[2], cfg->line_out_pins[3],
3880 cfg->line_out_pins[4]);
3881 snd_printd(" speaker_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
3882 cfg->speaker_outs, cfg->speaker_pins[0],
3883 cfg->speaker_pins[1], cfg->speaker_pins[2],
3884 cfg->speaker_pins[3], cfg->speaker_pins[4]);
3885 snd_printd(" hp_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
3886 cfg->hp_outs, cfg->hp_pins[0],
3887 cfg->hp_pins[1], cfg->hp_pins[2],
3888 cfg->hp_pins[3], cfg->hp_pins[4]);
3889 snd_printd(" mono: mono_out=0x%x\n", cfg->mono_out_pin);
3891 snd_printd(" dig-out=0x%x/0x%x\n",
3892 cfg->dig_out_pins[0], cfg->dig_out_pins[1]);
3893 snd_printd(" inputs: mic=0x%x, fmic=0x%x, line=0x%x, fline=0x%x,"
3894 " cd=0x%x, aux=0x%x\n",
3895 cfg->input_pins[AUTO_PIN_MIC],
3896 cfg->input_pins[AUTO_PIN_FRONT_MIC],
3897 cfg->input_pins[AUTO_PIN_LINE],
3898 cfg->input_pins[AUTO_PIN_FRONT_LINE],
3899 cfg->input_pins[AUTO_PIN_CD],
3900 cfg->input_pins[AUTO_PIN_AUX]);
3901 if (cfg->dig_in_pin)
3902 snd_printd(" dig-in=0x%x\n", cfg->dig_in_pin);
3906 EXPORT_SYMBOL_HDA(snd_hda_parse_pin_def_config);
3908 /* labels for input pins */
3909 const char *auto_pin_cfg_labels[AUTO_PIN_LAST] = {
3910 "Mic", "Front Mic", "Line", "Front Line", "CD", "Aux"
3912 EXPORT_SYMBOL_HDA(auto_pin_cfg_labels);
3921 * snd_hda_suspend - suspend the codecs
3924 * Returns 0 if successful.
3926 int snd_hda_suspend(struct hda_bus *bus)
3928 struct hda_codec *codec;
3930 list_for_each_entry(codec, &bus->codec_list, list) {
3931 #ifdef CONFIG_SND_HDA_POWER_SAVE
3932 if (!codec->power_on)
3935 hda_call_codec_suspend(codec);
3939 EXPORT_SYMBOL_HDA(snd_hda_suspend);
3942 * snd_hda_resume - resume the codecs
3945 * Returns 0 if successful.
3947 * This fucntion is defined only when POWER_SAVE isn't set.
3948 * In the power-save mode, the codec is resumed dynamically.
3950 int snd_hda_resume(struct hda_bus *bus)
3952 struct hda_codec *codec;
3954 list_for_each_entry(codec, &bus->codec_list, list) {
3955 if (snd_hda_codec_needs_resume(codec))
3956 hda_call_codec_resume(codec);
3960 EXPORT_SYMBOL_HDA(snd_hda_resume);
3961 #endif /* CONFIG_PM */
3967 /* get a new element from the given array
3968 * if it exceeds the pre-allocated array size, re-allocate the array
3970 void *snd_array_new(struct snd_array *array)
3972 if (array->used >= array->alloced) {
3973 int num = array->alloced + array->alloc_align;
3975 if (snd_BUG_ON(num >= 4096))
3977 nlist = kcalloc(num + 1, array->elem_size, GFP_KERNEL);
3981 memcpy(nlist, array->list,
3982 array->elem_size * array->alloced);
3985 array->list = nlist;
3986 array->alloced = num;
3988 return snd_array_elem(array, array->used++);
3990 EXPORT_SYMBOL_HDA(snd_array_new);
3992 /* free the given array elements */
3993 void snd_array_free(struct snd_array *array)
4000 EXPORT_SYMBOL_HDA(snd_array_free);
4003 * used by hda_proc.c and hda_eld.c
4005 void snd_print_pcm_rates(int pcm, char *buf, int buflen)
4007 static unsigned int rates[] = {
4008 8000, 11025, 16000, 22050, 32000, 44100, 48000, 88200,
4009 96000, 176400, 192000, 384000
4013 for (i = 0, j = 0; i < ARRAY_SIZE(rates); i++)
4015 j += snprintf(buf + j, buflen - j, " %d", rates[i]);
4017 buf[j] = '\0'; /* necessary when j == 0 */
4019 EXPORT_SYMBOL_HDA(snd_print_pcm_rates);
4021 void snd_print_pcm_bits(int pcm, char *buf, int buflen)
4023 static unsigned int bits[] = { 8, 16, 20, 24, 32 };
4026 for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
4027 if (pcm & (AC_SUPPCM_BITS_8 << i))
4028 j += snprintf(buf + j, buflen - j, " %d", bits[i]);
4030 buf[j] = '\0'; /* necessary when j == 0 */
4032 EXPORT_SYMBOL_HDA(snd_print_pcm_bits);
4034 MODULE_DESCRIPTION("HDA codec core");
4035 MODULE_LICENSE("GPL");