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;
306 if (snd_BUG_ON(!conn_list || max_conns <= 0))
309 parm = snd_hda_param_read(codec, nid, AC_PAR_CONNLIST_LEN);
310 if (parm & AC_CLIST_LONG) {
319 conn_len = parm & AC_CLIST_LENGTH;
320 mask = (1 << (shift-1)) - 1;
323 return 0; /* no connection */
326 /* single connection */
327 parm = snd_hda_codec_read(codec, nid, 0,
328 AC_VERB_GET_CONNECT_LIST, 0);
329 if (parm == -1 && codec->bus->rirb_error)
331 conn_list[0] = parm & mask;
335 /* multi connection */
338 for (i = 0; i < conn_len; i++) {
342 if (i % num_elems == 0) {
343 parm = snd_hda_codec_read(codec, nid, 0,
344 AC_VERB_GET_CONNECT_LIST, i);
345 if (parm == -1 && codec->bus->rirb_error)
348 range_val = !!(parm & (1 << (shift-1))); /* ranges */
352 /* ranges between the previous and this one */
353 if (!prev_nid || prev_nid >= val) {
354 snd_printk(KERN_WARNING "hda_codec: "
355 "invalid dep_range_val %x:%x\n",
359 for (n = prev_nid + 1; n <= val; n++) {
360 if (conns >= max_conns) {
362 "Too many connections\n");
365 conn_list[conns++] = n;
368 if (conns >= max_conns) {
369 snd_printk(KERN_ERR "Too many connections\n");
372 conn_list[conns++] = val;
378 EXPORT_SYMBOL_HDA(snd_hda_get_connections);
382 * snd_hda_queue_unsol_event - add an unsolicited event to queue
384 * @res: unsolicited event (lower 32bit of RIRB entry)
385 * @res_ex: codec addr and flags (upper 32bit or RIRB entry)
387 * Adds the given event to the queue. The events are processed in
388 * the workqueue asynchronously. Call this function in the interrupt
389 * hanlder when RIRB receives an unsolicited event.
391 * Returns 0 if successful, or a negative error code.
393 int snd_hda_queue_unsol_event(struct hda_bus *bus, u32 res, u32 res_ex)
395 struct hda_bus_unsolicited *unsol;
402 wp = (unsol->wp + 1) % HDA_UNSOL_QUEUE_SIZE;
406 unsol->queue[wp] = res;
407 unsol->queue[wp + 1] = res_ex;
409 queue_work(bus->workq, &unsol->work);
413 EXPORT_SYMBOL_HDA(snd_hda_queue_unsol_event);
416 * process queued unsolicited events
418 static void process_unsol_events(struct work_struct *work)
420 struct hda_bus_unsolicited *unsol =
421 container_of(work, struct hda_bus_unsolicited, work);
422 struct hda_bus *bus = unsol->bus;
423 struct hda_codec *codec;
424 unsigned int rp, caddr, res;
426 while (unsol->rp != unsol->wp) {
427 rp = (unsol->rp + 1) % HDA_UNSOL_QUEUE_SIZE;
430 res = unsol->queue[rp];
431 caddr = unsol->queue[rp + 1];
432 if (!(caddr & (1 << 4))) /* no unsolicited event? */
434 codec = bus->caddr_tbl[caddr & 0x0f];
435 if (codec && codec->patch_ops.unsol_event)
436 codec->patch_ops.unsol_event(codec, res);
441 * initialize unsolicited queue
443 static int init_unsol_queue(struct hda_bus *bus)
445 struct hda_bus_unsolicited *unsol;
447 if (bus->unsol) /* already initialized */
450 unsol = kzalloc(sizeof(*unsol), GFP_KERNEL);
452 snd_printk(KERN_ERR "hda_codec: "
453 "can't allocate unsolicited queue\n");
456 INIT_WORK(&unsol->work, process_unsol_events);
465 static void snd_hda_codec_free(struct hda_codec *codec);
467 static int snd_hda_bus_free(struct hda_bus *bus)
469 struct hda_codec *codec, *n;
474 flush_workqueue(bus->workq);
477 list_for_each_entry_safe(codec, n, &bus->codec_list, list) {
478 snd_hda_codec_free(codec);
480 if (bus->ops.private_free)
481 bus->ops.private_free(bus);
483 destroy_workqueue(bus->workq);
488 static int snd_hda_bus_dev_free(struct snd_device *device)
490 struct hda_bus *bus = device->device_data;
492 return snd_hda_bus_free(bus);
495 #ifdef CONFIG_SND_HDA_HWDEP
496 static int snd_hda_bus_dev_register(struct snd_device *device)
498 struct hda_bus *bus = device->device_data;
499 struct hda_codec *codec;
500 list_for_each_entry(codec, &bus->codec_list, list) {
501 snd_hda_hwdep_add_sysfs(codec);
506 #define snd_hda_bus_dev_register NULL
510 * snd_hda_bus_new - create a HDA bus
511 * @card: the card entry
512 * @temp: the template for hda_bus information
513 * @busp: the pointer to store the created bus instance
515 * Returns 0 if successful, or a negative error code.
517 int /*__devinit*/ snd_hda_bus_new(struct snd_card *card,
518 const struct hda_bus_template *temp,
519 struct hda_bus **busp)
523 static struct snd_device_ops dev_ops = {
524 .dev_register = snd_hda_bus_dev_register,
525 .dev_free = snd_hda_bus_dev_free,
528 if (snd_BUG_ON(!temp))
530 if (snd_BUG_ON(!temp->ops.command || !temp->ops.get_response))
536 bus = kzalloc(sizeof(*bus), GFP_KERNEL);
538 snd_printk(KERN_ERR "can't allocate struct hda_bus\n");
543 bus->private_data = temp->private_data;
544 bus->pci = temp->pci;
545 bus->modelname = temp->modelname;
546 bus->power_save = temp->power_save;
547 bus->ops = temp->ops;
549 mutex_init(&bus->cmd_mutex);
550 INIT_LIST_HEAD(&bus->codec_list);
552 snprintf(bus->workq_name, sizeof(bus->workq_name),
553 "hd-audio%d", card->number);
554 bus->workq = create_singlethread_workqueue(bus->workq_name);
556 snd_printk(KERN_ERR "cannot create workqueue %s\n",
562 err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops);
564 snd_hda_bus_free(bus);
571 EXPORT_SYMBOL_HDA(snd_hda_bus_new);
573 #ifdef CONFIG_SND_HDA_GENERIC
574 #define is_generic_config(codec) \
575 (codec->modelname && !strcmp(codec->modelname, "generic"))
577 #define is_generic_config(codec) 0
581 #define HDA_MODREQ_MAX_COUNT 2 /* two request_modules()'s */
583 #define HDA_MODREQ_MAX_COUNT 0 /* all presets are statically linked */
587 * find a matching codec preset
589 static const struct hda_codec_preset *
590 find_codec_preset(struct hda_codec *codec)
592 struct hda_codec_preset_list *tbl;
593 const struct hda_codec_preset *preset;
594 int mod_requested = 0;
596 if (is_generic_config(codec))
597 return NULL; /* use the generic parser */
600 mutex_lock(&preset_mutex);
601 list_for_each_entry(tbl, &hda_preset_tables, list) {
602 if (!try_module_get(tbl->owner)) {
603 snd_printk(KERN_ERR "hda_codec: cannot module_get\n");
606 for (preset = tbl->preset; preset->id; preset++) {
607 u32 mask = preset->mask;
608 if (preset->afg && preset->afg != codec->afg)
610 if (preset->mfg && preset->mfg != codec->mfg)
614 if (preset->id == (codec->vendor_id & mask) &&
616 preset->rev == codec->revision_id)) {
617 mutex_unlock(&preset_mutex);
618 codec->owner = tbl->owner;
622 module_put(tbl->owner);
624 mutex_unlock(&preset_mutex);
626 if (mod_requested < HDA_MODREQ_MAX_COUNT) {
629 snprintf(name, sizeof(name), "snd-hda-codec-id:%08x",
632 snprintf(name, sizeof(name), "snd-hda-codec-id:%04x*",
633 (codec->vendor_id >> 16) & 0xffff);
634 request_module(name);
642 * get_codec_name - store the codec name
644 static int get_codec_name(struct hda_codec *codec)
646 const struct hda_vendor_id *c;
647 const char *vendor = NULL;
648 u16 vendor_id = codec->vendor_id >> 16;
651 if (codec->vendor_name)
654 for (c = hda_vendor_ids; c->id; c++) {
655 if (c->id == vendor_id) {
661 sprintf(tmp, "Generic %04x", vendor_id);
664 codec->vendor_name = kstrdup(vendor, GFP_KERNEL);
665 if (!codec->vendor_name)
669 if (codec->chip_name)
672 if (codec->preset && codec->preset->name)
673 codec->chip_name = kstrdup(codec->preset->name, GFP_KERNEL);
675 sprintf(tmp, "ID %x", codec->vendor_id & 0xffff);
676 codec->chip_name = kstrdup(tmp, GFP_KERNEL);
678 if (!codec->chip_name)
684 * look for an AFG and MFG nodes
686 static void /*__devinit*/ setup_fg_nodes(struct hda_codec *codec)
688 int i, total_nodes, function_id;
691 total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid);
692 for (i = 0; i < total_nodes; i++, nid++) {
693 function_id = snd_hda_param_read(codec, nid,
694 AC_PAR_FUNCTION_TYPE) & 0xff;
695 switch (function_id) {
696 case AC_GRP_AUDIO_FUNCTION:
698 codec->function_id = function_id;
700 case AC_GRP_MODEM_FUNCTION:
702 codec->function_id = function_id;
711 * read widget caps for each widget and store in cache
713 static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
718 codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node,
720 codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL);
723 nid = codec->start_nid;
724 for (i = 0; i < codec->num_nodes; i++, nid++)
725 codec->wcaps[i] = snd_hda_param_read(codec, nid,
726 AC_PAR_AUDIO_WIDGET_CAP);
730 /* read all pin default configurations and save codec->init_pins */
731 static int read_pin_defaults(struct hda_codec *codec)
734 hda_nid_t nid = codec->start_nid;
736 for (i = 0; i < codec->num_nodes; i++, nid++) {
737 struct hda_pincfg *pin;
738 unsigned int wcaps = get_wcaps(codec, nid);
739 unsigned int wid_type = (wcaps & AC_WCAP_TYPE) >>
741 if (wid_type != AC_WID_PIN)
743 pin = snd_array_new(&codec->init_pins);
747 pin->cfg = snd_hda_codec_read(codec, nid, 0,
748 AC_VERB_GET_CONFIG_DEFAULT, 0);
753 /* look up the given pin config list and return the item matching with NID */
754 static struct hda_pincfg *look_up_pincfg(struct hda_codec *codec,
755 struct snd_array *array,
759 for (i = 0; i < array->used; i++) {
760 struct hda_pincfg *pin = snd_array_elem(array, i);
767 /* write a config value for the given NID */
768 static void set_pincfg(struct hda_codec *codec, hda_nid_t nid,
772 for (i = 0; i < 4; i++) {
773 snd_hda_codec_write(codec, nid, 0,
774 AC_VERB_SET_CONFIG_DEFAULT_BYTES_0 + i,
780 /* set the current pin config value for the given NID.
781 * the value is cached, and read via snd_hda_codec_get_pincfg()
783 int snd_hda_add_pincfg(struct hda_codec *codec, struct snd_array *list,
784 hda_nid_t nid, unsigned int cfg)
786 struct hda_pincfg *pin;
789 oldcfg = snd_hda_codec_get_pincfg(codec, nid);
790 pin = look_up_pincfg(codec, list, nid);
792 pin = snd_array_new(list);
799 /* change only when needed; e.g. if the pincfg is already present
800 * in user_pins[], don't write it
802 cfg = snd_hda_codec_get_pincfg(codec, nid);
804 set_pincfg(codec, nid, cfg);
808 int snd_hda_codec_set_pincfg(struct hda_codec *codec,
809 hda_nid_t nid, unsigned int cfg)
811 return snd_hda_add_pincfg(codec, &codec->driver_pins, nid, cfg);
813 EXPORT_SYMBOL_HDA(snd_hda_codec_set_pincfg);
815 /* get the current pin config value of the given pin NID */
816 unsigned int snd_hda_codec_get_pincfg(struct hda_codec *codec, hda_nid_t nid)
818 struct hda_pincfg *pin;
820 #ifdef CONFIG_SND_HDA_HWDEP
821 pin = look_up_pincfg(codec, &codec->user_pins, nid);
825 pin = look_up_pincfg(codec, &codec->driver_pins, nid);
828 pin = look_up_pincfg(codec, &codec->init_pins, nid);
833 EXPORT_SYMBOL_HDA(snd_hda_codec_get_pincfg);
835 /* restore all current pin configs */
836 static void restore_pincfgs(struct hda_codec *codec)
839 for (i = 0; i < codec->init_pins.used; i++) {
840 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
841 set_pincfg(codec, pin->nid,
842 snd_hda_codec_get_pincfg(codec, pin->nid));
846 static void init_hda_cache(struct hda_cache_rec *cache,
847 unsigned int record_size);
848 static void free_hda_cache(struct hda_cache_rec *cache);
850 /* restore the initial pin cfgs and release all pincfg lists */
851 static void restore_init_pincfgs(struct hda_codec *codec)
853 /* first free driver_pins and user_pins, then call restore_pincfg
854 * so that only the values in init_pins are restored
856 snd_array_free(&codec->driver_pins);
857 #ifdef CONFIG_SND_HDA_HWDEP
858 snd_array_free(&codec->user_pins);
860 restore_pincfgs(codec);
861 snd_array_free(&codec->init_pins);
867 static void snd_hda_codec_free(struct hda_codec *codec)
871 restore_init_pincfgs(codec);
872 #ifdef CONFIG_SND_HDA_POWER_SAVE
873 cancel_delayed_work(&codec->power_work);
874 flush_workqueue(codec->bus->workq);
876 list_del(&codec->list);
877 snd_array_free(&codec->mixers);
878 codec->bus->caddr_tbl[codec->addr] = NULL;
879 if (codec->patch_ops.free)
880 codec->patch_ops.free(codec);
881 module_put(codec->owner);
882 free_hda_cache(&codec->amp_cache);
883 free_hda_cache(&codec->cmd_cache);
884 kfree(codec->vendor_name);
885 kfree(codec->chip_name);
886 kfree(codec->modelname);
891 static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
892 unsigned int power_state);
895 * snd_hda_codec_new - create a HDA codec
896 * @bus: the bus to assign
897 * @codec_addr: the codec address
898 * @codecp: the pointer to store the generated codec
900 * Returns 0 if successful, or a negative error code.
902 int /*__devinit*/ snd_hda_codec_new(struct hda_bus *bus, unsigned int codec_addr,
903 struct hda_codec **codecp)
905 struct hda_codec *codec;
909 if (snd_BUG_ON(!bus))
911 if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS))
914 if (bus->caddr_tbl[codec_addr]) {
915 snd_printk(KERN_ERR "hda_codec: "
916 "address 0x%x is already occupied\n", codec_addr);
920 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
922 snd_printk(KERN_ERR "can't allocate struct hda_codec\n");
927 codec->addr = codec_addr;
928 mutex_init(&codec->spdif_mutex);
929 mutex_init(&codec->control_mutex);
930 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
931 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
932 snd_array_init(&codec->mixers, sizeof(struct snd_kcontrol *), 32);
933 snd_array_init(&codec->init_pins, sizeof(struct hda_pincfg), 16);
934 snd_array_init(&codec->driver_pins, sizeof(struct hda_pincfg), 16);
935 if (codec->bus->modelname) {
936 codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL);
937 if (!codec->modelname) {
938 snd_hda_codec_free(codec);
943 #ifdef CONFIG_SND_HDA_POWER_SAVE
944 INIT_DELAYED_WORK(&codec->power_work, hda_power_work);
945 /* snd_hda_codec_new() marks the codec as power-up, and leave it as is.
946 * the caller has to power down appropriatley after initialization
949 hda_keep_power_on(codec);
952 list_add_tail(&codec->list, &bus->codec_list);
953 bus->caddr_tbl[codec_addr] = codec;
955 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
957 if (codec->vendor_id == -1)
958 /* read again, hopefully the access method was corrected
959 * in the last read...
961 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
963 codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT,
964 AC_PAR_SUBSYSTEM_ID);
965 codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT,
968 setup_fg_nodes(codec);
969 if (!codec->afg && !codec->mfg) {
970 snd_printdd("hda_codec: no AFG or MFG node found\n");
975 err = read_widget_caps(codec, codec->afg ? codec->afg : codec->mfg);
977 snd_printk(KERN_ERR "hda_codec: cannot malloc\n");
980 err = read_pin_defaults(codec);
984 if (!codec->subsystem_id) {
985 hda_nid_t nid = codec->afg ? codec->afg : codec->mfg;
986 codec->subsystem_id =
987 snd_hda_codec_read(codec, nid, 0,
988 AC_VERB_GET_SUBSYSTEM_ID, 0);
991 /* power-up all before initialization */
992 hda_set_power_state(codec,
993 codec->afg ? codec->afg : codec->mfg,
996 snd_hda_codec_proc_new(codec);
998 snd_hda_create_hwdep(codec);
1000 sprintf(component, "HDA:%08x,%08x,%08x", codec->vendor_id,
1001 codec->subsystem_id, codec->revision_id);
1002 snd_component_add(codec->bus->card, component);
1009 snd_hda_codec_free(codec);
1012 EXPORT_SYMBOL_HDA(snd_hda_codec_new);
1014 int snd_hda_codec_configure(struct hda_codec *codec)
1018 codec->preset = find_codec_preset(codec);
1019 if (!codec->vendor_name || !codec->chip_name) {
1020 err = get_codec_name(codec);
1024 /* audio codec should override the mixer name */
1025 if (codec->afg || !*codec->bus->card->mixername)
1026 snprintf(codec->bus->card->mixername,
1027 sizeof(codec->bus->card->mixername),
1028 "%s %s", codec->vendor_name, codec->chip_name);
1030 if (is_generic_config(codec)) {
1031 err = snd_hda_parse_generic_codec(codec);
1034 if (codec->preset && codec->preset->patch) {
1035 err = codec->preset->patch(codec);
1039 /* call the default parser */
1040 err = snd_hda_parse_generic_codec(codec);
1042 printk(KERN_ERR "hda-codec: No codec parser is available\n");
1045 if (!err && codec->patch_ops.unsol_event)
1046 err = init_unsol_queue(codec->bus);
1049 EXPORT_SYMBOL_HDA(snd_hda_codec_configure);
1052 * snd_hda_codec_setup_stream - set up the codec for streaming
1053 * @codec: the CODEC to set up
1054 * @nid: the NID to set up
1055 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
1056 * @channel_id: channel id to pass, zero based.
1057 * @format: stream format.
1059 void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
1061 int channel_id, int format)
1066 snd_printdd("hda_codec_setup_stream: "
1067 "NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
1068 nid, stream_tag, channel_id, format);
1069 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID,
1070 (stream_tag << 4) | channel_id);
1072 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, format);
1074 EXPORT_SYMBOL_HDA(snd_hda_codec_setup_stream);
1076 void snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid)
1081 snd_printdd("hda_codec_cleanup_stream: NID=0x%x\n", nid);
1082 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
1083 #if 0 /* keep the format */
1085 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0);
1088 EXPORT_SYMBOL_HDA(snd_hda_codec_cleanup_stream);
1091 * amp access functions
1094 /* FIXME: more better hash key? */
1095 #define HDA_HASH_KEY(nid,dir,idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
1096 #define HDA_HASH_PINCAP_KEY(nid) (u32)((nid) + (0x02 << 24))
1097 #define HDA_HASH_PARPCM_KEY(nid) (u32)((nid) + (0x03 << 24))
1098 #define HDA_HASH_PARSTR_KEY(nid) (u32)((nid) + (0x04 << 24))
1099 #define INFO_AMP_CAPS (1<<0)
1100 #define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
1102 /* initialize the hash table */
1103 static void /*__devinit*/ init_hda_cache(struct hda_cache_rec *cache,
1104 unsigned int record_size)
1106 memset(cache, 0, sizeof(*cache));
1107 memset(cache->hash, 0xff, sizeof(cache->hash));
1108 snd_array_init(&cache->buf, record_size, 64);
1111 static void free_hda_cache(struct hda_cache_rec *cache)
1113 snd_array_free(&cache->buf);
1116 /* query the hash. allocate an entry if not found. */
1117 static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache,
1120 u16 idx = key % (u16)ARRAY_SIZE(cache->hash);
1121 u16 cur = cache->hash[idx];
1122 struct hda_cache_head *info;
1124 while (cur != 0xffff) {
1125 info = snd_array_elem(&cache->buf, cur);
1126 if (info->key == key)
1131 /* add a new hash entry */
1132 info = snd_array_new(&cache->buf);
1135 cur = snd_array_index(&cache->buf, info);
1138 info->next = cache->hash[idx];
1139 cache->hash[idx] = cur;
1144 /* query and allocate an amp hash entry */
1145 static inline struct hda_amp_info *
1146 get_alloc_amp_hash(struct hda_codec *codec, u32 key)
1148 return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key);
1152 * query AMP capabilities for the given widget and direction
1154 u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
1156 struct hda_amp_info *info;
1158 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, 0));
1161 if (!(info->head.val & INFO_AMP_CAPS)) {
1162 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
1164 info->amp_caps = snd_hda_param_read(codec, nid,
1165 direction == HDA_OUTPUT ?
1166 AC_PAR_AMP_OUT_CAP :
1169 info->head.val |= INFO_AMP_CAPS;
1171 return info->amp_caps;
1173 EXPORT_SYMBOL_HDA(query_amp_caps);
1175 int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
1178 struct hda_amp_info *info;
1180 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, dir, 0));
1183 info->amp_caps = caps;
1184 info->head.val |= INFO_AMP_CAPS;
1187 EXPORT_SYMBOL_HDA(snd_hda_override_amp_caps);
1190 query_caps_hash(struct hda_codec *codec, hda_nid_t nid, u32 key,
1191 unsigned int (*func)(struct hda_codec *, hda_nid_t))
1193 struct hda_amp_info *info;
1195 info = get_alloc_amp_hash(codec, key);
1198 if (!info->head.val) {
1199 info->head.val |= INFO_AMP_CAPS;
1200 info->amp_caps = func(codec, nid);
1202 return info->amp_caps;
1205 static unsigned int read_pin_cap(struct hda_codec *codec, hda_nid_t nid)
1207 return snd_hda_param_read(codec, nid, AC_PAR_PIN_CAP);
1210 u32 snd_hda_query_pin_caps(struct hda_codec *codec, hda_nid_t nid)
1212 return query_caps_hash(codec, nid, HDA_HASH_PINCAP_KEY(nid),
1215 EXPORT_SYMBOL_HDA(snd_hda_query_pin_caps);
1218 * read the current volume to info
1219 * if the cache exists, read the cache value.
1221 static unsigned int get_vol_mute(struct hda_codec *codec,
1222 struct hda_amp_info *info, hda_nid_t nid,
1223 int ch, int direction, int index)
1227 if (info->head.val & INFO_AMP_VOL(ch))
1228 return info->vol[ch];
1230 parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
1231 parm |= direction == HDA_OUTPUT ? AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
1233 val = snd_hda_codec_read(codec, nid, 0,
1234 AC_VERB_GET_AMP_GAIN_MUTE, parm);
1235 info->vol[ch] = val & 0xff;
1236 info->head.val |= INFO_AMP_VOL(ch);
1237 return info->vol[ch];
1241 * write the current volume in info to the h/w and update the cache
1243 static void put_vol_mute(struct hda_codec *codec, struct hda_amp_info *info,
1244 hda_nid_t nid, int ch, int direction, int index,
1249 parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
1250 parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
1251 parm |= index << AC_AMP_SET_INDEX_SHIFT;
1253 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
1254 info->vol[ch] = val;
1258 * read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
1260 int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
1261 int direction, int index)
1263 struct hda_amp_info *info;
1264 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
1267 return get_vol_mute(codec, info, nid, ch, direction, index);
1269 EXPORT_SYMBOL_HDA(snd_hda_codec_amp_read);
1272 * update the AMP value, mask = bit mask to set, val = the value
1274 int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
1275 int direction, int idx, int mask, int val)
1277 struct hda_amp_info *info;
1279 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, idx));
1283 val |= get_vol_mute(codec, info, nid, ch, direction, idx) & ~mask;
1284 if (info->vol[ch] == val)
1286 put_vol_mute(codec, info, nid, ch, direction, idx, val);
1289 EXPORT_SYMBOL_HDA(snd_hda_codec_amp_update);
1292 * update the AMP stereo with the same mask and value
1294 int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
1295 int direction, int idx, int mask, int val)
1298 for (ch = 0; ch < 2; ch++)
1299 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
1303 EXPORT_SYMBOL_HDA(snd_hda_codec_amp_stereo);
1305 #ifdef SND_HDA_NEEDS_RESUME
1306 /* resume the all amp commands from the cache */
1307 void snd_hda_codec_resume_amp(struct hda_codec *codec)
1309 struct hda_amp_info *buffer = codec->amp_cache.buf.list;
1312 for (i = 0; i < codec->amp_cache.buf.used; i++, buffer++) {
1313 u32 key = buffer->head.key;
1315 unsigned int idx, dir, ch;
1319 idx = (key >> 16) & 0xff;
1320 dir = (key >> 24) & 0xff;
1321 for (ch = 0; ch < 2; ch++) {
1322 if (!(buffer->head.val & INFO_AMP_VOL(ch)))
1324 put_vol_mute(codec, buffer, nid, ch, dir, idx,
1329 EXPORT_SYMBOL_HDA(snd_hda_codec_resume_amp);
1330 #endif /* SND_HDA_NEEDS_RESUME */
1333 int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
1334 struct snd_ctl_elem_info *uinfo)
1336 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1337 u16 nid = get_amp_nid(kcontrol);
1338 u8 chs = get_amp_channels(kcontrol);
1339 int dir = get_amp_direction(kcontrol);
1340 unsigned int ofs = get_amp_offset(kcontrol);
1343 caps = query_amp_caps(codec, nid, dir);
1345 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1347 printk(KERN_WARNING "hda_codec: "
1348 "num_steps = 0 for NID=0x%x (ctl = %s)\n", nid,
1354 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1355 uinfo->count = chs == 3 ? 2 : 1;
1356 uinfo->value.integer.min = 0;
1357 uinfo->value.integer.max = caps;
1360 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_info);
1363 static inline unsigned int
1364 read_amp_value(struct hda_codec *codec, hda_nid_t nid,
1365 int ch, int dir, int idx, unsigned int ofs)
1368 val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
1369 val &= HDA_AMP_VOLMASK;
1378 update_amp_value(struct hda_codec *codec, hda_nid_t nid,
1379 int ch, int dir, int idx, unsigned int ofs,
1384 return snd_hda_codec_amp_update(codec, nid, ch, dir, idx,
1385 HDA_AMP_VOLMASK, val);
1388 int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
1389 struct snd_ctl_elem_value *ucontrol)
1391 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1392 hda_nid_t nid = get_amp_nid(kcontrol);
1393 int chs = get_amp_channels(kcontrol);
1394 int dir = get_amp_direction(kcontrol);
1395 int idx = get_amp_index(kcontrol);
1396 unsigned int ofs = get_amp_offset(kcontrol);
1397 long *valp = ucontrol->value.integer.value;
1400 *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs);
1402 *valp = read_amp_value(codec, nid, 1, dir, idx, ofs);
1405 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_get);
1407 int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
1408 struct snd_ctl_elem_value *ucontrol)
1410 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1411 hda_nid_t nid = get_amp_nid(kcontrol);
1412 int chs = get_amp_channels(kcontrol);
1413 int dir = get_amp_direction(kcontrol);
1414 int idx = get_amp_index(kcontrol);
1415 unsigned int ofs = get_amp_offset(kcontrol);
1416 long *valp = ucontrol->value.integer.value;
1419 snd_hda_power_up(codec);
1421 change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp);
1425 change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp);
1426 snd_hda_power_down(codec);
1429 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_put);
1431 int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
1432 unsigned int size, unsigned int __user *_tlv)
1434 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1435 hda_nid_t nid = get_amp_nid(kcontrol);
1436 int dir = get_amp_direction(kcontrol);
1437 unsigned int ofs = get_amp_offset(kcontrol);
1438 u32 caps, val1, val2;
1440 if (size < 4 * sizeof(unsigned int))
1442 caps = query_amp_caps(codec, nid, dir);
1443 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
1444 val2 = (val2 + 1) * 25;
1445 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
1447 val1 = ((int)val1) * ((int)val2);
1448 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
1450 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
1452 if (put_user(val1, _tlv + 2))
1454 if (put_user(val2, _tlv + 3))
1458 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_tlv);
1461 * set (static) TLV for virtual master volume; recalculated as max 0dB
1463 void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
1469 caps = query_amp_caps(codec, nid, dir);
1470 nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1471 step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
1472 step = (step + 1) * 25;
1473 tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
1474 tlv[1] = 2 * sizeof(unsigned int);
1475 tlv[2] = -nums * step;
1478 EXPORT_SYMBOL_HDA(snd_hda_set_vmaster_tlv);
1480 /* find a mixer control element with the given name */
1481 static struct snd_kcontrol *
1482 _snd_hda_find_mixer_ctl(struct hda_codec *codec,
1483 const char *name, int idx)
1485 struct snd_ctl_elem_id id;
1486 memset(&id, 0, sizeof(id));
1487 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1489 if (snd_BUG_ON(strlen(name) >= sizeof(id.name)))
1491 strcpy(id.name, name);
1492 return snd_ctl_find_id(codec->bus->card, &id);
1495 struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
1498 return _snd_hda_find_mixer_ctl(codec, name, 0);
1500 EXPORT_SYMBOL_HDA(snd_hda_find_mixer_ctl);
1502 /* Add a control element and assign to the codec */
1503 int snd_hda_ctl_add(struct hda_codec *codec, struct snd_kcontrol *kctl)
1506 struct snd_kcontrol **knewp;
1508 err = snd_ctl_add(codec->bus->card, kctl);
1511 knewp = snd_array_new(&codec->mixers);
1517 EXPORT_SYMBOL_HDA(snd_hda_ctl_add);
1519 /* Clear all controls assigned to the given codec */
1520 void snd_hda_ctls_clear(struct hda_codec *codec)
1523 struct snd_kcontrol **kctls = codec->mixers.list;
1524 for (i = 0; i < codec->mixers.used; i++)
1525 snd_ctl_remove(codec->bus->card, kctls[i]);
1526 snd_array_free(&codec->mixers);
1529 /* pseudo device locking
1530 * toggle card->shutdown to allow/disallow the device access (as a hack)
1532 static int hda_lock_devices(struct snd_card *card)
1534 spin_lock(&card->files_lock);
1535 if (card->shutdown) {
1536 spin_unlock(&card->files_lock);
1540 spin_unlock(&card->files_lock);
1544 static void hda_unlock_devices(struct snd_card *card)
1546 spin_lock(&card->files_lock);
1548 spin_unlock(&card->files_lock);
1551 int snd_hda_codec_reset(struct hda_codec *codec)
1553 struct snd_card *card = codec->bus->card;
1556 if (hda_lock_devices(card) < 0)
1558 /* check whether the codec isn't used by any mixer or PCM streams */
1559 if (!list_empty(&card->ctl_files)) {
1560 hda_unlock_devices(card);
1563 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
1564 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
1567 if (cpcm->pcm->streams[0].substream_opened ||
1568 cpcm->pcm->streams[1].substream_opened) {
1569 hda_unlock_devices(card);
1574 /* OK, let it free */
1576 #ifdef CONFIG_SND_HDA_POWER_SAVE
1577 cancel_delayed_work(&codec->power_work);
1578 flush_workqueue(codec->bus->workq);
1580 snd_hda_ctls_clear(codec);
1582 for (i = 0; i < codec->num_pcms; i++) {
1583 if (codec->pcm_info[i].pcm) {
1584 snd_device_free(card, codec->pcm_info[i].pcm);
1585 clear_bit(codec->pcm_info[i].device,
1586 codec->bus->pcm_dev_bits);
1589 if (codec->patch_ops.free)
1590 codec->patch_ops.free(codec);
1591 codec->proc_widget_hook = NULL;
1593 free_hda_cache(&codec->amp_cache);
1594 free_hda_cache(&codec->cmd_cache);
1595 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
1596 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
1597 /* free only driver_pins so that init_pins + user_pins are restored */
1598 snd_array_free(&codec->driver_pins);
1599 restore_pincfgs(codec);
1600 codec->num_pcms = 0;
1601 codec->pcm_info = NULL;
1602 codec->preset = NULL;
1603 memset(&codec->patch_ops, 0, sizeof(codec->patch_ops));
1604 codec->slave_dig_outs = NULL;
1605 codec->spdif_status_reset = 0;
1606 module_put(codec->owner);
1607 codec->owner = NULL;
1609 /* allow device access again */
1610 hda_unlock_devices(card);
1614 /* create a virtual master control and add slaves */
1615 int snd_hda_add_vmaster(struct hda_codec *codec, char *name,
1616 unsigned int *tlv, const char **slaves)
1618 struct snd_kcontrol *kctl;
1622 for (s = slaves; *s && !snd_hda_find_mixer_ctl(codec, *s); s++)
1625 snd_printdd("No slave found for %s\n", name);
1628 kctl = snd_ctl_make_virtual_master(name, tlv);
1631 err = snd_hda_ctl_add(codec, kctl);
1635 for (s = slaves; *s; s++) {
1636 struct snd_kcontrol *sctl;
1639 sctl = _snd_hda_find_mixer_ctl(codec, *s, i);
1642 snd_printdd("Cannot find slave %s, "
1646 err = snd_ctl_add_slave(kctl, sctl);
1654 EXPORT_SYMBOL_HDA(snd_hda_add_vmaster);
1657 int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
1658 struct snd_ctl_elem_info *uinfo)
1660 int chs = get_amp_channels(kcontrol);
1662 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1663 uinfo->count = chs == 3 ? 2 : 1;
1664 uinfo->value.integer.min = 0;
1665 uinfo->value.integer.max = 1;
1668 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_info);
1670 int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
1671 struct snd_ctl_elem_value *ucontrol)
1673 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1674 hda_nid_t nid = get_amp_nid(kcontrol);
1675 int chs = get_amp_channels(kcontrol);
1676 int dir = get_amp_direction(kcontrol);
1677 int idx = get_amp_index(kcontrol);
1678 long *valp = ucontrol->value.integer.value;
1681 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
1682 HDA_AMP_MUTE) ? 0 : 1;
1684 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
1685 HDA_AMP_MUTE) ? 0 : 1;
1688 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_get);
1690 int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
1691 struct snd_ctl_elem_value *ucontrol)
1693 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1694 hda_nid_t nid = get_amp_nid(kcontrol);
1695 int chs = get_amp_channels(kcontrol);
1696 int dir = get_amp_direction(kcontrol);
1697 int idx = get_amp_index(kcontrol);
1698 long *valp = ucontrol->value.integer.value;
1701 snd_hda_power_up(codec);
1703 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
1705 *valp ? 0 : HDA_AMP_MUTE);
1709 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
1711 *valp ? 0 : HDA_AMP_MUTE);
1712 #ifdef CONFIG_SND_HDA_POWER_SAVE
1713 if (codec->patch_ops.check_power_status)
1714 codec->patch_ops.check_power_status(codec, nid);
1716 snd_hda_power_down(codec);
1719 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put);
1722 * bound volume controls
1724 * bind multiple volumes (# indices, from 0)
1727 #define AMP_VAL_IDX_SHIFT 19
1728 #define AMP_VAL_IDX_MASK (0x0f<<19)
1730 int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
1731 struct snd_ctl_elem_value *ucontrol)
1733 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1737 mutex_lock(&codec->control_mutex);
1738 pval = kcontrol->private_value;
1739 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
1740 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
1741 kcontrol->private_value = pval;
1742 mutex_unlock(&codec->control_mutex);
1745 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_get);
1747 int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
1748 struct snd_ctl_elem_value *ucontrol)
1750 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1752 int i, indices, err = 0, change = 0;
1754 mutex_lock(&codec->control_mutex);
1755 pval = kcontrol->private_value;
1756 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
1757 for (i = 0; i < indices; i++) {
1758 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
1759 (i << AMP_VAL_IDX_SHIFT);
1760 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
1765 kcontrol->private_value = pval;
1766 mutex_unlock(&codec->control_mutex);
1767 return err < 0 ? err : change;
1769 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_put);
1772 * generic bound volume/swtich controls
1774 int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
1775 struct snd_ctl_elem_info *uinfo)
1777 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1778 struct hda_bind_ctls *c;
1781 mutex_lock(&codec->control_mutex);
1782 c = (struct hda_bind_ctls *)kcontrol->private_value;
1783 kcontrol->private_value = *c->values;
1784 err = c->ops->info(kcontrol, uinfo);
1785 kcontrol->private_value = (long)c;
1786 mutex_unlock(&codec->control_mutex);
1789 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_info);
1791 int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
1792 struct snd_ctl_elem_value *ucontrol)
1794 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1795 struct hda_bind_ctls *c;
1798 mutex_lock(&codec->control_mutex);
1799 c = (struct hda_bind_ctls *)kcontrol->private_value;
1800 kcontrol->private_value = *c->values;
1801 err = c->ops->get(kcontrol, ucontrol);
1802 kcontrol->private_value = (long)c;
1803 mutex_unlock(&codec->control_mutex);
1806 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_get);
1808 int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
1809 struct snd_ctl_elem_value *ucontrol)
1811 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1812 struct hda_bind_ctls *c;
1813 unsigned long *vals;
1814 int err = 0, change = 0;
1816 mutex_lock(&codec->control_mutex);
1817 c = (struct hda_bind_ctls *)kcontrol->private_value;
1818 for (vals = c->values; *vals; vals++) {
1819 kcontrol->private_value = *vals;
1820 err = c->ops->put(kcontrol, ucontrol);
1825 kcontrol->private_value = (long)c;
1826 mutex_unlock(&codec->control_mutex);
1827 return err < 0 ? err : change;
1829 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_put);
1831 int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
1832 unsigned int size, unsigned int __user *tlv)
1834 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1835 struct hda_bind_ctls *c;
1838 mutex_lock(&codec->control_mutex);
1839 c = (struct hda_bind_ctls *)kcontrol->private_value;
1840 kcontrol->private_value = *c->values;
1841 err = c->ops->tlv(kcontrol, op_flag, size, tlv);
1842 kcontrol->private_value = (long)c;
1843 mutex_unlock(&codec->control_mutex);
1846 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_tlv);
1848 struct hda_ctl_ops snd_hda_bind_vol = {
1849 .info = snd_hda_mixer_amp_volume_info,
1850 .get = snd_hda_mixer_amp_volume_get,
1851 .put = snd_hda_mixer_amp_volume_put,
1852 .tlv = snd_hda_mixer_amp_tlv
1854 EXPORT_SYMBOL_HDA(snd_hda_bind_vol);
1856 struct hda_ctl_ops snd_hda_bind_sw = {
1857 .info = snd_hda_mixer_amp_switch_info,
1858 .get = snd_hda_mixer_amp_switch_get,
1859 .put = snd_hda_mixer_amp_switch_put,
1860 .tlv = snd_hda_mixer_amp_tlv
1862 EXPORT_SYMBOL_HDA(snd_hda_bind_sw);
1865 * SPDIF out controls
1868 static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
1869 struct snd_ctl_elem_info *uinfo)
1871 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1876 static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
1877 struct snd_ctl_elem_value *ucontrol)
1879 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
1880 IEC958_AES0_NONAUDIO |
1881 IEC958_AES0_CON_EMPHASIS_5015 |
1882 IEC958_AES0_CON_NOT_COPYRIGHT;
1883 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
1884 IEC958_AES1_CON_ORIGINAL;
1888 static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
1889 struct snd_ctl_elem_value *ucontrol)
1891 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
1892 IEC958_AES0_NONAUDIO |
1893 IEC958_AES0_PRO_EMPHASIS_5015;
1897 static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
1898 struct snd_ctl_elem_value *ucontrol)
1900 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1902 ucontrol->value.iec958.status[0] = codec->spdif_status & 0xff;
1903 ucontrol->value.iec958.status[1] = (codec->spdif_status >> 8) & 0xff;
1904 ucontrol->value.iec958.status[2] = (codec->spdif_status >> 16) & 0xff;
1905 ucontrol->value.iec958.status[3] = (codec->spdif_status >> 24) & 0xff;
1910 /* convert from SPDIF status bits to HDA SPDIF bits
1911 * bit 0 (DigEn) is always set zero (to be filled later)
1913 static unsigned short convert_from_spdif_status(unsigned int sbits)
1915 unsigned short val = 0;
1917 if (sbits & IEC958_AES0_PROFESSIONAL)
1918 val |= AC_DIG1_PROFESSIONAL;
1919 if (sbits & IEC958_AES0_NONAUDIO)
1920 val |= AC_DIG1_NONAUDIO;
1921 if (sbits & IEC958_AES0_PROFESSIONAL) {
1922 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
1923 IEC958_AES0_PRO_EMPHASIS_5015)
1924 val |= AC_DIG1_EMPHASIS;
1926 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
1927 IEC958_AES0_CON_EMPHASIS_5015)
1928 val |= AC_DIG1_EMPHASIS;
1929 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
1930 val |= AC_DIG1_COPYRIGHT;
1931 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
1932 val |= AC_DIG1_LEVEL;
1933 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
1938 /* convert to SPDIF status bits from HDA SPDIF bits
1940 static unsigned int convert_to_spdif_status(unsigned short val)
1942 unsigned int sbits = 0;
1944 if (val & AC_DIG1_NONAUDIO)
1945 sbits |= IEC958_AES0_NONAUDIO;
1946 if (val & AC_DIG1_PROFESSIONAL)
1947 sbits |= IEC958_AES0_PROFESSIONAL;
1948 if (sbits & IEC958_AES0_PROFESSIONAL) {
1949 if (sbits & AC_DIG1_EMPHASIS)
1950 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
1952 if (val & AC_DIG1_EMPHASIS)
1953 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
1954 if (!(val & AC_DIG1_COPYRIGHT))
1955 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
1956 if (val & AC_DIG1_LEVEL)
1957 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
1958 sbits |= val & (0x7f << 8);
1963 /* set digital convert verbs both for the given NID and its slaves */
1964 static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
1969 snd_hda_codec_write_cache(codec, nid, 0, verb, val);
1970 d = codec->slave_dig_outs;
1974 snd_hda_codec_write_cache(codec, *d, 0, verb, val);
1977 static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
1981 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1);
1983 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2);
1986 static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
1987 struct snd_ctl_elem_value *ucontrol)
1989 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1990 hda_nid_t nid = kcontrol->private_value;
1994 mutex_lock(&codec->spdif_mutex);
1995 codec->spdif_status = ucontrol->value.iec958.status[0] |
1996 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
1997 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
1998 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
1999 val = convert_from_spdif_status(codec->spdif_status);
2000 val |= codec->spdif_ctls & 1;
2001 change = codec->spdif_ctls != val;
2002 codec->spdif_ctls = val;
2005 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
2007 mutex_unlock(&codec->spdif_mutex);
2011 #define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
2013 static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
2014 struct snd_ctl_elem_value *ucontrol)
2016 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2018 ucontrol->value.integer.value[0] = codec->spdif_ctls & AC_DIG1_ENABLE;
2022 static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
2023 struct snd_ctl_elem_value *ucontrol)
2025 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2026 hda_nid_t nid = kcontrol->private_value;
2030 mutex_lock(&codec->spdif_mutex);
2031 val = codec->spdif_ctls & ~AC_DIG1_ENABLE;
2032 if (ucontrol->value.integer.value[0])
2033 val |= AC_DIG1_ENABLE;
2034 change = codec->spdif_ctls != val;
2036 codec->spdif_ctls = val;
2037 set_dig_out_convert(codec, nid, val & 0xff, -1);
2038 /* unmute amp switch (if any) */
2039 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
2040 (val & AC_DIG1_ENABLE))
2041 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
2044 mutex_unlock(&codec->spdif_mutex);
2048 static struct snd_kcontrol_new dig_mixes[] = {
2050 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2051 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2052 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,CON_MASK),
2053 .info = snd_hda_spdif_mask_info,
2054 .get = snd_hda_spdif_cmask_get,
2057 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2058 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2059 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,PRO_MASK),
2060 .info = snd_hda_spdif_mask_info,
2061 .get = snd_hda_spdif_pmask_get,
2064 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2065 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
2066 .info = snd_hda_spdif_mask_info,
2067 .get = snd_hda_spdif_default_get,
2068 .put = snd_hda_spdif_default_put,
2071 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2072 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,SWITCH),
2073 .info = snd_hda_spdif_out_switch_info,
2074 .get = snd_hda_spdif_out_switch_get,
2075 .put = snd_hda_spdif_out_switch_put,
2080 #define SPDIF_MAX_IDX 4 /* 4 instances should be enough to probe */
2083 * snd_hda_create_spdif_out_ctls - create Output SPDIF-related controls
2084 * @codec: the HDA codec
2085 * @nid: audio out widget NID
2087 * Creates controls related with the SPDIF output.
2088 * Called from each patch supporting the SPDIF out.
2090 * Returns 0 if successful, or a negative error code.
2092 int snd_hda_create_spdif_out_ctls(struct hda_codec *codec, hda_nid_t nid)
2095 struct snd_kcontrol *kctl;
2096 struct snd_kcontrol_new *dig_mix;
2099 for (idx = 0; idx < SPDIF_MAX_IDX; idx++) {
2100 if (!_snd_hda_find_mixer_ctl(codec, "IEC958 Playback Switch",
2104 if (idx >= SPDIF_MAX_IDX) {
2105 printk(KERN_ERR "hda_codec: too many IEC958 outputs\n");
2108 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
2109 kctl = snd_ctl_new1(dig_mix, codec);
2112 kctl->id.index = idx;
2113 kctl->private_value = nid;
2114 err = snd_hda_ctl_add(codec, kctl);
2119 snd_hda_codec_read(codec, nid, 0,
2120 AC_VERB_GET_DIGI_CONVERT_1, 0);
2121 codec->spdif_status = convert_to_spdif_status(codec->spdif_ctls);
2124 EXPORT_SYMBOL_HDA(snd_hda_create_spdif_out_ctls);
2127 * SPDIF sharing with analog output
2129 static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
2130 struct snd_ctl_elem_value *ucontrol)
2132 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
2133 ucontrol->value.integer.value[0] = mout->share_spdif;
2137 static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
2138 struct snd_ctl_elem_value *ucontrol)
2140 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
2141 mout->share_spdif = !!ucontrol->value.integer.value[0];
2145 static struct snd_kcontrol_new spdif_share_sw = {
2146 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2147 .name = "IEC958 Default PCM Playback Switch",
2148 .info = snd_ctl_boolean_mono_info,
2149 .get = spdif_share_sw_get,
2150 .put = spdif_share_sw_put,
2153 int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
2154 struct hda_multi_out *mout)
2156 if (!mout->dig_out_nid)
2158 /* ATTENTION: here mout is passed as private_data, instead of codec */
2159 return snd_hda_ctl_add(codec,
2160 snd_ctl_new1(&spdif_share_sw, mout));
2162 EXPORT_SYMBOL_HDA(snd_hda_create_spdif_share_sw);
2168 #define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
2170 static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
2171 struct snd_ctl_elem_value *ucontrol)
2173 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2175 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
2179 static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
2180 struct snd_ctl_elem_value *ucontrol)
2182 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2183 hda_nid_t nid = kcontrol->private_value;
2184 unsigned int val = !!ucontrol->value.integer.value[0];
2187 mutex_lock(&codec->spdif_mutex);
2188 change = codec->spdif_in_enable != val;
2190 codec->spdif_in_enable = val;
2191 snd_hda_codec_write_cache(codec, nid, 0,
2192 AC_VERB_SET_DIGI_CONVERT_1, val);
2194 mutex_unlock(&codec->spdif_mutex);
2198 static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
2199 struct snd_ctl_elem_value *ucontrol)
2201 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2202 hda_nid_t nid = kcontrol->private_value;
2206 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0);
2207 sbits = convert_to_spdif_status(val);
2208 ucontrol->value.iec958.status[0] = sbits;
2209 ucontrol->value.iec958.status[1] = sbits >> 8;
2210 ucontrol->value.iec958.status[2] = sbits >> 16;
2211 ucontrol->value.iec958.status[3] = sbits >> 24;
2215 static struct snd_kcontrol_new dig_in_ctls[] = {
2217 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2218 .name = SNDRV_CTL_NAME_IEC958("",CAPTURE,SWITCH),
2219 .info = snd_hda_spdif_in_switch_info,
2220 .get = snd_hda_spdif_in_switch_get,
2221 .put = snd_hda_spdif_in_switch_put,
2224 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2225 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2226 .name = SNDRV_CTL_NAME_IEC958("",CAPTURE,DEFAULT),
2227 .info = snd_hda_spdif_mask_info,
2228 .get = snd_hda_spdif_in_status_get,
2234 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
2235 * @codec: the HDA codec
2236 * @nid: audio in widget NID
2238 * Creates controls related with the SPDIF input.
2239 * Called from each patch supporting the SPDIF in.
2241 * Returns 0 if successful, or a negative error code.
2243 int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
2246 struct snd_kcontrol *kctl;
2247 struct snd_kcontrol_new *dig_mix;
2250 for (idx = 0; idx < SPDIF_MAX_IDX; idx++) {
2251 if (!_snd_hda_find_mixer_ctl(codec, "IEC958 Capture Switch",
2255 if (idx >= SPDIF_MAX_IDX) {
2256 printk(KERN_ERR "hda_codec: too many IEC958 inputs\n");
2259 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
2260 kctl = snd_ctl_new1(dig_mix, codec);
2263 kctl->private_value = nid;
2264 err = snd_hda_ctl_add(codec, kctl);
2268 codec->spdif_in_enable =
2269 snd_hda_codec_read(codec, nid, 0,
2270 AC_VERB_GET_DIGI_CONVERT_1, 0) &
2274 EXPORT_SYMBOL_HDA(snd_hda_create_spdif_in_ctls);
2276 #ifdef SND_HDA_NEEDS_RESUME
2281 /* build a 32bit cache key with the widget id and the command parameter */
2282 #define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
2283 #define get_cmd_cache_nid(key) ((key) & 0xff)
2284 #define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
2287 * snd_hda_codec_write_cache - send a single command with caching
2288 * @codec: the HDA codec
2289 * @nid: NID to send the command
2290 * @direct: direct flag
2291 * @verb: the verb to send
2292 * @parm: the parameter for the verb
2294 * Send a single command without waiting for response.
2296 * Returns 0 if successful, or a negative error code.
2298 int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
2299 int direct, unsigned int verb, unsigned int parm)
2301 int err = snd_hda_codec_write(codec, nid, direct, verb, parm);
2302 struct hda_cache_head *c;
2307 /* parm may contain the verb stuff for get/set amp */
2308 verb = verb | (parm >> 8);
2310 key = build_cmd_cache_key(nid, verb);
2311 mutex_lock(&codec->bus->cmd_mutex);
2312 c = get_alloc_hash(&codec->cmd_cache, key);
2315 mutex_unlock(&codec->bus->cmd_mutex);
2318 EXPORT_SYMBOL_HDA(snd_hda_codec_write_cache);
2320 /* resume the all commands from the cache */
2321 void snd_hda_codec_resume_cache(struct hda_codec *codec)
2323 struct hda_cache_head *buffer = codec->cmd_cache.buf.list;
2326 for (i = 0; i < codec->cmd_cache.buf.used; i++, buffer++) {
2327 u32 key = buffer->key;
2330 snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
2331 get_cmd_cache_cmd(key), buffer->val);
2334 EXPORT_SYMBOL_HDA(snd_hda_codec_resume_cache);
2337 * snd_hda_sequence_write_cache - sequence writes with caching
2338 * @codec: the HDA codec
2339 * @seq: VERB array to send
2341 * Send the commands sequentially from the given array.
2342 * Thte commands are recorded on cache for power-save and resume.
2343 * The array must be terminated with NID=0.
2345 void snd_hda_sequence_write_cache(struct hda_codec *codec,
2346 const struct hda_verb *seq)
2348 for (; seq->nid; seq++)
2349 snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
2352 EXPORT_SYMBOL_HDA(snd_hda_sequence_write_cache);
2353 #endif /* SND_HDA_NEEDS_RESUME */
2356 * set power state of the codec
2358 static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
2359 unsigned int power_state)
2364 snd_hda_codec_write(codec, fg, 0, AC_VERB_SET_POWER_STATE,
2366 msleep(10); /* partial workaround for "azx_get_response timeout" */
2368 nid = codec->start_nid;
2369 for (i = 0; i < codec->num_nodes; i++, nid++) {
2370 unsigned int wcaps = get_wcaps(codec, nid);
2371 if (wcaps & AC_WCAP_POWER) {
2372 unsigned int wid_type = (wcaps & AC_WCAP_TYPE) >>
2374 if (power_state == AC_PWRST_D3 &&
2375 wid_type == AC_WID_PIN) {
2376 unsigned int pincap;
2378 * don't power down the widget if it controls
2379 * eapd and EAPD_BTLENABLE is set.
2381 pincap = snd_hda_query_pin_caps(codec, nid);
2382 if (pincap & AC_PINCAP_EAPD) {
2383 int eapd = snd_hda_codec_read(codec,
2385 AC_VERB_GET_EAPD_BTLENABLE, 0);
2391 snd_hda_codec_write(codec, nid, 0,
2392 AC_VERB_SET_POWER_STATE,
2397 if (power_state == AC_PWRST_D0) {
2398 unsigned long end_time;
2401 /* wait until the codec reachs to D0 */
2402 end_time = jiffies + msecs_to_jiffies(500);
2404 state = snd_hda_codec_read(codec, fg, 0,
2405 AC_VERB_GET_POWER_STATE, 0);
2406 if (state == power_state)
2409 } while (time_after_eq(end_time, jiffies));
2413 #ifdef CONFIG_SND_HDA_HWDEP
2414 /* execute additional init verbs */
2415 static void hda_exec_init_verbs(struct hda_codec *codec)
2417 if (codec->init_verbs.list)
2418 snd_hda_sequence_write(codec, codec->init_verbs.list);
2421 static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
2424 #ifdef SND_HDA_NEEDS_RESUME
2426 * call suspend and power-down; used both from PM and power-save
2428 static void hda_call_codec_suspend(struct hda_codec *codec)
2430 if (codec->patch_ops.suspend)
2431 codec->patch_ops.suspend(codec, PMSG_SUSPEND);
2432 hda_set_power_state(codec,
2433 codec->afg ? codec->afg : codec->mfg,
2435 #ifdef CONFIG_SND_HDA_POWER_SAVE
2436 cancel_delayed_work(&codec->power_work);
2437 codec->power_on = 0;
2438 codec->power_transition = 0;
2443 * kick up codec; used both from PM and power-save
2445 static void hda_call_codec_resume(struct hda_codec *codec)
2447 hda_set_power_state(codec,
2448 codec->afg ? codec->afg : codec->mfg,
2450 restore_pincfgs(codec); /* restore all current pin configs */
2451 hda_exec_init_verbs(codec);
2452 if (codec->patch_ops.resume)
2453 codec->patch_ops.resume(codec);
2455 if (codec->patch_ops.init)
2456 codec->patch_ops.init(codec);
2457 snd_hda_codec_resume_amp(codec);
2458 snd_hda_codec_resume_cache(codec);
2461 #endif /* SND_HDA_NEEDS_RESUME */
2465 * snd_hda_build_controls - build mixer controls
2468 * Creates mixer controls for each codec included in the bus.
2470 * Returns 0 if successful, otherwise a negative error code.
2472 int /*__devinit*/ snd_hda_build_controls(struct hda_bus *bus)
2474 struct hda_codec *codec;
2476 list_for_each_entry(codec, &bus->codec_list, list) {
2477 int err = snd_hda_codec_build_controls(codec);
2479 printk(KERN_ERR "hda_codec: cannot build controls"
2480 "for #%d (error %d)\n", codec->addr, err);
2481 err = snd_hda_codec_reset(codec);
2484 "hda_codec: cannot revert codec\n");
2491 EXPORT_SYMBOL_HDA(snd_hda_build_controls);
2493 int snd_hda_codec_build_controls(struct hda_codec *codec)
2496 hda_exec_init_verbs(codec);
2497 /* continue to initialize... */
2498 if (codec->patch_ops.init)
2499 err = codec->patch_ops.init(codec);
2500 if (!err && codec->patch_ops.build_controls)
2501 err = codec->patch_ops.build_controls(codec);
2510 struct hda_rate_tbl {
2512 unsigned int alsa_bits;
2513 unsigned int hda_fmt;
2516 static struct hda_rate_tbl rate_bits[] = {
2517 /* rate in Hz, ALSA rate bitmask, HDA format value */
2519 /* autodetected value used in snd_hda_query_supported_pcm */
2520 { 8000, SNDRV_PCM_RATE_8000, 0x0500 }, /* 1/6 x 48 */
2521 { 11025, SNDRV_PCM_RATE_11025, 0x4300 }, /* 1/4 x 44 */
2522 { 16000, SNDRV_PCM_RATE_16000, 0x0200 }, /* 1/3 x 48 */
2523 { 22050, SNDRV_PCM_RATE_22050, 0x4100 }, /* 1/2 x 44 */
2524 { 32000, SNDRV_PCM_RATE_32000, 0x0a00 }, /* 2/3 x 48 */
2525 { 44100, SNDRV_PCM_RATE_44100, 0x4000 }, /* 44 */
2526 { 48000, SNDRV_PCM_RATE_48000, 0x0000 }, /* 48 */
2527 { 88200, SNDRV_PCM_RATE_88200, 0x4800 }, /* 2 x 44 */
2528 { 96000, SNDRV_PCM_RATE_96000, 0x0800 }, /* 2 x 48 */
2529 { 176400, SNDRV_PCM_RATE_176400, 0x5800 },/* 4 x 44 */
2530 { 192000, SNDRV_PCM_RATE_192000, 0x1800 }, /* 4 x 48 */
2531 #define AC_PAR_PCM_RATE_BITS 11
2532 /* up to bits 10, 384kHZ isn't supported properly */
2534 /* not autodetected value */
2535 { 9600, SNDRV_PCM_RATE_KNOT, 0x0400 }, /* 1/5 x 48 */
2537 { 0 } /* terminator */
2541 * snd_hda_calc_stream_format - calculate format bitset
2542 * @rate: the sample rate
2543 * @channels: the number of channels
2544 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
2545 * @maxbps: the max. bps
2547 * Calculate the format bitset from the given rate, channels and th PCM format.
2549 * Return zero if invalid.
2551 unsigned int snd_hda_calc_stream_format(unsigned int rate,
2552 unsigned int channels,
2553 unsigned int format,
2554 unsigned int maxbps)
2557 unsigned int val = 0;
2559 for (i = 0; rate_bits[i].hz; i++)
2560 if (rate_bits[i].hz == rate) {
2561 val = rate_bits[i].hda_fmt;
2564 if (!rate_bits[i].hz) {
2565 snd_printdd("invalid rate %d\n", rate);
2569 if (channels == 0 || channels > 8) {
2570 snd_printdd("invalid channels %d\n", channels);
2573 val |= channels - 1;
2575 switch (snd_pcm_format_width(format)) {
2576 case 8: val |= 0x00; break;
2577 case 16: val |= 0x10; break;
2581 if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE)
2583 else if (maxbps >= 24)
2589 snd_printdd("invalid format width %d\n",
2590 snd_pcm_format_width(format));
2596 EXPORT_SYMBOL_HDA(snd_hda_calc_stream_format);
2598 static unsigned int get_pcm_param(struct hda_codec *codec, hda_nid_t nid)
2600 unsigned int val = 0;
2601 if (nid != codec->afg &&
2602 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD))
2603 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
2604 if (!val || val == -1)
2605 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
2606 if (!val || val == -1)
2611 static unsigned int query_pcm_param(struct hda_codec *codec, hda_nid_t nid)
2613 return query_caps_hash(codec, nid, HDA_HASH_PARPCM_KEY(nid),
2617 static unsigned int get_stream_param(struct hda_codec *codec, hda_nid_t nid)
2619 unsigned int streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
2620 if (!streams || streams == -1)
2621 streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
2622 if (!streams || streams == -1)
2627 static unsigned int query_stream_param(struct hda_codec *codec, hda_nid_t nid)
2629 return query_caps_hash(codec, nid, HDA_HASH_PARSTR_KEY(nid),
2634 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
2635 * @codec: the HDA codec
2636 * @nid: NID to query
2637 * @ratesp: the pointer to store the detected rate bitflags
2638 * @formatsp: the pointer to store the detected formats
2639 * @bpsp: the pointer to store the detected format widths
2641 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
2642 * or @bsps argument is ignored.
2644 * Returns 0 if successful, otherwise a negative error code.
2646 static int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
2647 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
2649 unsigned int i, val, wcaps;
2651 wcaps = get_wcaps(codec, nid);
2652 val = query_pcm_param(codec, nid);
2656 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
2658 rates |= rate_bits[i].alsa_bits;
2661 snd_printk(KERN_ERR "hda_codec: rates == 0 "
2662 "(nid=0x%x, val=0x%x, ovrd=%i)\n",
2664 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0);
2670 if (formatsp || bpsp) {
2672 unsigned int streams, bps;
2674 streams = query_stream_param(codec, nid);
2679 if (streams & AC_SUPFMT_PCM) {
2680 if (val & AC_SUPPCM_BITS_8) {
2681 formats |= SNDRV_PCM_FMTBIT_U8;
2684 if (val & AC_SUPPCM_BITS_16) {
2685 formats |= SNDRV_PCM_FMTBIT_S16_LE;
2688 if (wcaps & AC_WCAP_DIGITAL) {
2689 if (val & AC_SUPPCM_BITS_32)
2690 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
2691 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
2692 formats |= SNDRV_PCM_FMTBIT_S32_LE;
2693 if (val & AC_SUPPCM_BITS_24)
2695 else if (val & AC_SUPPCM_BITS_20)
2697 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
2698 AC_SUPPCM_BITS_32)) {
2699 formats |= SNDRV_PCM_FMTBIT_S32_LE;
2700 if (val & AC_SUPPCM_BITS_32)
2702 else if (val & AC_SUPPCM_BITS_24)
2704 else if (val & AC_SUPPCM_BITS_20)
2708 if (streams & AC_SUPFMT_FLOAT32) {
2709 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
2713 if (streams == AC_SUPFMT_AC3) {
2714 /* should be exclusive */
2715 /* temporary hack: we have still no proper support
2716 * for the direct AC3 stream...
2718 formats |= SNDRV_PCM_FMTBIT_U8;
2722 snd_printk(KERN_ERR "hda_codec: formats == 0 "
2723 "(nid=0x%x, val=0x%x, ovrd=%i, "
2726 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0,
2731 *formatsp = formats;
2740 * snd_hda_is_supported_format - check whether the given node supports
2743 * Returns 1 if supported, 0 if not.
2745 int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
2746 unsigned int format)
2749 unsigned int val = 0, rate, stream;
2751 val = query_pcm_param(codec, nid);
2755 rate = format & 0xff00;
2756 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
2757 if (rate_bits[i].hda_fmt == rate) {
2762 if (i >= AC_PAR_PCM_RATE_BITS)
2765 stream = query_stream_param(codec, nid);
2769 if (stream & AC_SUPFMT_PCM) {
2770 switch (format & 0xf0) {
2772 if (!(val & AC_SUPPCM_BITS_8))
2776 if (!(val & AC_SUPPCM_BITS_16))
2780 if (!(val & AC_SUPPCM_BITS_20))
2784 if (!(val & AC_SUPPCM_BITS_24))
2788 if (!(val & AC_SUPPCM_BITS_32))
2795 /* FIXME: check for float32 and AC3? */
2800 EXPORT_SYMBOL_HDA(snd_hda_is_supported_format);
2805 static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
2806 struct hda_codec *codec,
2807 struct snd_pcm_substream *substream)
2812 static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
2813 struct hda_codec *codec,
2814 unsigned int stream_tag,
2815 unsigned int format,
2816 struct snd_pcm_substream *substream)
2818 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
2822 static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
2823 struct hda_codec *codec,
2824 struct snd_pcm_substream *substream)
2826 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
2830 static int set_pcm_default_values(struct hda_codec *codec,
2831 struct hda_pcm_stream *info)
2835 /* query support PCM information from the given NID */
2836 if (info->nid && (!info->rates || !info->formats)) {
2837 err = snd_hda_query_supported_pcm(codec, info->nid,
2838 info->rates ? NULL : &info->rates,
2839 info->formats ? NULL : &info->formats,
2840 info->maxbps ? NULL : &info->maxbps);
2844 if (info->ops.open == NULL)
2845 info->ops.open = hda_pcm_default_open_close;
2846 if (info->ops.close == NULL)
2847 info->ops.close = hda_pcm_default_open_close;
2848 if (info->ops.prepare == NULL) {
2849 if (snd_BUG_ON(!info->nid))
2851 info->ops.prepare = hda_pcm_default_prepare;
2853 if (info->ops.cleanup == NULL) {
2854 if (snd_BUG_ON(!info->nid))
2856 info->ops.cleanup = hda_pcm_default_cleanup;
2862 * get the empty PCM device number to assign
2864 static int get_empty_pcm_device(struct hda_bus *bus, int type)
2866 static const char *dev_name[HDA_PCM_NTYPES] = {
2867 "Audio", "SPDIF", "HDMI", "Modem"
2869 /* starting device index for each PCM type */
2870 static int dev_idx[HDA_PCM_NTYPES] = {
2871 [HDA_PCM_TYPE_AUDIO] = 0,
2872 [HDA_PCM_TYPE_SPDIF] = 1,
2873 [HDA_PCM_TYPE_HDMI] = 3,
2874 [HDA_PCM_TYPE_MODEM] = 6
2876 /* normal audio device indices; not linear to keep compatibility */
2877 static int audio_idx[4] = { 0, 2, 4, 5 };
2881 case HDA_PCM_TYPE_AUDIO:
2882 for (i = 0; i < ARRAY_SIZE(audio_idx); i++) {
2884 if (!test_bit(dev, bus->pcm_dev_bits))
2887 snd_printk(KERN_WARNING "Too many audio devices\n");
2889 case HDA_PCM_TYPE_SPDIF:
2890 case HDA_PCM_TYPE_HDMI:
2891 case HDA_PCM_TYPE_MODEM:
2892 dev = dev_idx[type];
2893 if (test_bit(dev, bus->pcm_dev_bits)) {
2894 snd_printk(KERN_WARNING "%s already defined\n",
2900 snd_printk(KERN_WARNING "Invalid PCM type %d\n", type);
2904 set_bit(dev, bus->pcm_dev_bits);
2909 * attach a new PCM stream
2911 static int snd_hda_attach_pcm(struct hda_codec *codec, struct hda_pcm *pcm)
2913 struct hda_bus *bus = codec->bus;
2914 struct hda_pcm_stream *info;
2917 if (snd_BUG_ON(!pcm->name))
2919 for (stream = 0; stream < 2; stream++) {
2920 info = &pcm->stream[stream];
2921 if (info->substreams) {
2922 err = set_pcm_default_values(codec, info);
2927 return bus->ops.attach_pcm(bus, codec, pcm);
2930 /* assign all PCMs of the given codec */
2931 int snd_hda_codec_build_pcms(struct hda_codec *codec)
2936 if (!codec->num_pcms) {
2937 if (!codec->patch_ops.build_pcms)
2939 err = codec->patch_ops.build_pcms(codec);
2941 printk(KERN_ERR "hda_codec: cannot build PCMs"
2942 "for #%d (error %d)\n", codec->addr, err);
2943 err = snd_hda_codec_reset(codec);
2946 "hda_codec: cannot revert codec\n");
2951 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
2952 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
2955 if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
2956 continue; /* no substreams assigned */
2959 dev = get_empty_pcm_device(codec->bus, cpcm->pcm_type);
2961 continue; /* no fatal error */
2963 err = snd_hda_attach_pcm(codec, cpcm);
2965 printk(KERN_ERR "hda_codec: cannot attach "
2966 "PCM stream %d for codec #%d\n",
2968 continue; /* no fatal error */
2976 * snd_hda_build_pcms - build PCM information
2979 * Create PCM information for each codec included in the bus.
2981 * The build_pcms codec patch is requested to set up codec->num_pcms and
2982 * codec->pcm_info properly. The array is referred by the top-level driver
2983 * to create its PCM instances.
2984 * The allocated codec->pcm_info should be released in codec->patch_ops.free
2987 * At least, substreams, channels_min and channels_max must be filled for
2988 * each stream. substreams = 0 indicates that the stream doesn't exist.
2989 * When rates and/or formats are zero, the supported values are queried
2990 * from the given nid. The nid is used also by the default ops.prepare
2991 * and ops.cleanup callbacks.
2993 * The driver needs to call ops.open in its open callback. Similarly,
2994 * ops.close is supposed to be called in the close callback.
2995 * ops.prepare should be called in the prepare or hw_params callback
2996 * with the proper parameters for set up.
2997 * ops.cleanup should be called in hw_free for clean up of streams.
2999 * This function returns 0 if successfull, or a negative error code.
3001 int __devinit snd_hda_build_pcms(struct hda_bus *bus)
3003 struct hda_codec *codec;
3005 list_for_each_entry(codec, &bus->codec_list, list) {
3006 int err = snd_hda_codec_build_pcms(codec);
3012 EXPORT_SYMBOL_HDA(snd_hda_build_pcms);
3015 * snd_hda_check_board_config - compare the current codec with the config table
3016 * @codec: the HDA codec
3017 * @num_configs: number of config enums
3018 * @models: array of model name strings
3019 * @tbl: configuration table, terminated by null entries
3021 * Compares the modelname or PCI subsystem id of the current codec with the
3022 * given configuration table. If a matching entry is found, returns its
3023 * config value (supposed to be 0 or positive).
3025 * If no entries are matching, the function returns a negative value.
3027 int snd_hda_check_board_config(struct hda_codec *codec,
3028 int num_configs, const char **models,
3029 const struct snd_pci_quirk *tbl)
3031 if (codec->modelname && models) {
3033 for (i = 0; i < num_configs; i++) {
3035 !strcmp(codec->modelname, models[i])) {
3036 snd_printd(KERN_INFO "hda_codec: model '%s' is "
3037 "selected\n", models[i]);
3043 if (!codec->bus->pci || !tbl)
3046 tbl = snd_pci_quirk_lookup(codec->bus->pci, tbl);
3049 if (tbl->value >= 0 && tbl->value < num_configs) {
3050 #ifdef CONFIG_SND_DEBUG_VERBOSE
3052 const char *model = NULL;
3054 model = models[tbl->value];
3056 sprintf(tmp, "#%d", tbl->value);
3059 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
3060 "for config %x:%x (%s)\n",
3061 model, tbl->subvendor, tbl->subdevice,
3062 (tbl->name ? tbl->name : "Unknown device"));
3068 EXPORT_SYMBOL_HDA(snd_hda_check_board_config);
3071 * snd_hda_check_board_codec_sid_config - compare the current codec
3072 subsystem ID with the
3075 This is important for Gateway notebooks with SB450 HDA Audio
3076 where the vendor ID of the PCI device is:
3077 ATI Technologies Inc SB450 HDA Audio [1002:437b]
3078 and the vendor/subvendor are found only at the codec.
3080 * @codec: the HDA codec
3081 * @num_configs: number of config enums
3082 * @models: array of model name strings
3083 * @tbl: configuration table, terminated by null entries
3085 * Compares the modelname or PCI subsystem id of the current codec with the
3086 * given configuration table. If a matching entry is found, returns its
3087 * config value (supposed to be 0 or positive).
3089 * If no entries are matching, the function returns a negative value.
3091 int snd_hda_check_board_codec_sid_config(struct hda_codec *codec,
3092 int num_configs, const char **models,
3093 const struct snd_pci_quirk *tbl)
3095 const struct snd_pci_quirk *q;
3097 /* Search for codec ID */
3098 for (q = tbl; q->subvendor; q++) {
3099 unsigned long vendorid = (q->subdevice) | (q->subvendor << 16);
3101 if (vendorid == codec->subsystem_id)
3110 if (tbl->value >= 0 && tbl->value < num_configs) {
3111 #ifdef CONFIG_SND_DEBUG_DETECT
3113 const char *model = NULL;
3115 model = models[tbl->value];
3117 sprintf(tmp, "#%d", tbl->value);
3120 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
3121 "for config %x:%x (%s)\n",
3122 model, tbl->subvendor, tbl->subdevice,
3123 (tbl->name ? tbl->name : "Unknown device"));
3129 EXPORT_SYMBOL_HDA(snd_hda_check_board_codec_sid_config);
3132 * snd_hda_add_new_ctls - create controls from the array
3133 * @codec: the HDA codec
3134 * @knew: the array of struct snd_kcontrol_new
3136 * This helper function creates and add new controls in the given array.
3137 * The array must be terminated with an empty entry as terminator.
3139 * Returns 0 if successful, or a negative error code.
3141 int snd_hda_add_new_ctls(struct hda_codec *codec, struct snd_kcontrol_new *knew)
3145 for (; knew->name; knew++) {
3146 struct snd_kcontrol *kctl;
3147 kctl = snd_ctl_new1(knew, codec);
3150 err = snd_hda_ctl_add(codec, kctl);
3154 kctl = snd_ctl_new1(knew, codec);
3157 kctl->id.device = codec->addr;
3158 err = snd_hda_ctl_add(codec, kctl);
3165 EXPORT_SYMBOL_HDA(snd_hda_add_new_ctls);
3167 #ifdef CONFIG_SND_HDA_POWER_SAVE
3168 static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
3169 unsigned int power_state);
3171 static void hda_power_work(struct work_struct *work)
3173 struct hda_codec *codec =
3174 container_of(work, struct hda_codec, power_work.work);
3175 struct hda_bus *bus = codec->bus;
3177 if (!codec->power_on || codec->power_count) {
3178 codec->power_transition = 0;
3182 hda_call_codec_suspend(codec);
3183 if (bus->ops.pm_notify)
3184 bus->ops.pm_notify(bus);
3187 static void hda_keep_power_on(struct hda_codec *codec)
3189 codec->power_count++;
3190 codec->power_on = 1;
3193 void snd_hda_power_up(struct hda_codec *codec)
3195 struct hda_bus *bus = codec->bus;
3197 codec->power_count++;
3198 if (codec->power_on || codec->power_transition)
3201 codec->power_on = 1;
3202 if (bus->ops.pm_notify)
3203 bus->ops.pm_notify(bus);
3204 hda_call_codec_resume(codec);
3205 cancel_delayed_work(&codec->power_work);
3206 codec->power_transition = 0;
3208 EXPORT_SYMBOL_HDA(snd_hda_power_up);
3210 #define power_save(codec) \
3211 ((codec)->bus->power_save ? *(codec)->bus->power_save : 0)
3213 #define power_save(codec) \
3214 ((codec)->bus->power_save ? *(codec)->bus->power_save : 0)
3216 void snd_hda_power_down(struct hda_codec *codec)
3218 --codec->power_count;
3219 if (!codec->power_on || codec->power_count || codec->power_transition)
3221 if (power_save(codec)) {
3222 codec->power_transition = 1; /* avoid reentrance */
3223 queue_delayed_work(codec->bus->workq, &codec->power_work,
3224 msecs_to_jiffies(power_save(codec) * 1000));
3227 EXPORT_SYMBOL_HDA(snd_hda_power_down);
3229 int snd_hda_check_amp_list_power(struct hda_codec *codec,
3230 struct hda_loopback_check *check,
3233 struct hda_amp_list *p;
3236 if (!check->amplist)
3238 for (p = check->amplist; p->nid; p++) {
3243 return 0; /* nothing changed */
3245 for (p = check->amplist; p->nid; p++) {
3246 for (ch = 0; ch < 2; ch++) {
3247 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
3249 if (!(v & HDA_AMP_MUTE) && v > 0) {
3250 if (!check->power_on) {
3251 check->power_on = 1;
3252 snd_hda_power_up(codec);
3258 if (check->power_on) {
3259 check->power_on = 0;
3260 snd_hda_power_down(codec);
3264 EXPORT_SYMBOL_HDA(snd_hda_check_amp_list_power);
3268 * Channel mode helper
3270 int snd_hda_ch_mode_info(struct hda_codec *codec,
3271 struct snd_ctl_elem_info *uinfo,
3272 const struct hda_channel_mode *chmode,
3275 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
3277 uinfo->value.enumerated.items = num_chmodes;
3278 if (uinfo->value.enumerated.item >= num_chmodes)
3279 uinfo->value.enumerated.item = num_chmodes - 1;
3280 sprintf(uinfo->value.enumerated.name, "%dch",
3281 chmode[uinfo->value.enumerated.item].channels);
3284 EXPORT_SYMBOL_HDA(snd_hda_ch_mode_info);
3286 int snd_hda_ch_mode_get(struct hda_codec *codec,
3287 struct snd_ctl_elem_value *ucontrol,
3288 const struct hda_channel_mode *chmode,
3294 for (i = 0; i < num_chmodes; i++) {
3295 if (max_channels == chmode[i].channels) {
3296 ucontrol->value.enumerated.item[0] = i;
3302 EXPORT_SYMBOL_HDA(snd_hda_ch_mode_get);
3304 int snd_hda_ch_mode_put(struct hda_codec *codec,
3305 struct snd_ctl_elem_value *ucontrol,
3306 const struct hda_channel_mode *chmode,
3312 mode = ucontrol->value.enumerated.item[0];
3313 if (mode >= num_chmodes)
3315 if (*max_channelsp == chmode[mode].channels)
3317 /* change the current channel setting */
3318 *max_channelsp = chmode[mode].channels;
3319 if (chmode[mode].sequence)
3320 snd_hda_sequence_write_cache(codec, chmode[mode].sequence);
3323 EXPORT_SYMBOL_HDA(snd_hda_ch_mode_put);
3328 int snd_hda_input_mux_info(const struct hda_input_mux *imux,
3329 struct snd_ctl_elem_info *uinfo)
3333 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
3335 uinfo->value.enumerated.items = imux->num_items;
3336 if (!imux->num_items)
3338 index = uinfo->value.enumerated.item;
3339 if (index >= imux->num_items)
3340 index = imux->num_items - 1;
3341 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
3344 EXPORT_SYMBOL_HDA(snd_hda_input_mux_info);
3346 int snd_hda_input_mux_put(struct hda_codec *codec,
3347 const struct hda_input_mux *imux,
3348 struct snd_ctl_elem_value *ucontrol,
3350 unsigned int *cur_val)
3354 if (!imux->num_items)
3356 idx = ucontrol->value.enumerated.item[0];
3357 if (idx >= imux->num_items)
3358 idx = imux->num_items - 1;
3359 if (*cur_val == idx)
3361 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
3362 imux->items[idx].index);
3366 EXPORT_SYMBOL_HDA(snd_hda_input_mux_put);
3370 * Multi-channel / digital-out PCM helper functions
3373 /* setup SPDIF output stream */
3374 static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
3375 unsigned int stream_tag, unsigned int format)
3377 /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */
3378 if (codec->spdif_status_reset && (codec->spdif_ctls & AC_DIG1_ENABLE))
3379 set_dig_out_convert(codec, nid,
3380 codec->spdif_ctls & ~AC_DIG1_ENABLE & 0xff,
3382 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
3383 if (codec->slave_dig_outs) {
3385 for (d = codec->slave_dig_outs; *d; d++)
3386 snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
3389 /* turn on again (if needed) */
3390 if (codec->spdif_status_reset && (codec->spdif_ctls & AC_DIG1_ENABLE))
3391 set_dig_out_convert(codec, nid,
3392 codec->spdif_ctls & 0xff, -1);
3395 static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
3397 snd_hda_codec_cleanup_stream(codec, nid);
3398 if (codec->slave_dig_outs) {
3400 for (d = codec->slave_dig_outs; *d; d++)
3401 snd_hda_codec_cleanup_stream(codec, *d);
3406 * open the digital out in the exclusive mode
3408 int snd_hda_multi_out_dig_open(struct hda_codec *codec,
3409 struct hda_multi_out *mout)
3411 mutex_lock(&codec->spdif_mutex);
3412 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
3413 /* already opened as analog dup; reset it once */
3414 cleanup_dig_out_stream(codec, mout->dig_out_nid);
3415 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
3416 mutex_unlock(&codec->spdif_mutex);
3419 EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_open);
3421 int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
3422 struct hda_multi_out *mout,
3423 unsigned int stream_tag,
3424 unsigned int format,
3425 struct snd_pcm_substream *substream)
3427 mutex_lock(&codec->spdif_mutex);
3428 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
3429 mutex_unlock(&codec->spdif_mutex);
3432 EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_prepare);
3434 int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec,
3435 struct hda_multi_out *mout)
3437 mutex_lock(&codec->spdif_mutex);
3438 cleanup_dig_out_stream(codec, mout->dig_out_nid);
3439 mutex_unlock(&codec->spdif_mutex);
3442 EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_cleanup);
3445 * release the digital out
3447 int snd_hda_multi_out_dig_close(struct hda_codec *codec,
3448 struct hda_multi_out *mout)
3450 mutex_lock(&codec->spdif_mutex);
3451 mout->dig_out_used = 0;
3452 mutex_unlock(&codec->spdif_mutex);
3455 EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_close);
3458 * set up more restrictions for analog out
3460 int snd_hda_multi_out_analog_open(struct hda_codec *codec,
3461 struct hda_multi_out *mout,
3462 struct snd_pcm_substream *substream,
3463 struct hda_pcm_stream *hinfo)
3465 struct snd_pcm_runtime *runtime = substream->runtime;
3466 runtime->hw.channels_max = mout->max_channels;
3467 if (mout->dig_out_nid) {
3468 if (!mout->analog_rates) {
3469 mout->analog_rates = hinfo->rates;
3470 mout->analog_formats = hinfo->formats;
3471 mout->analog_maxbps = hinfo->maxbps;
3473 runtime->hw.rates = mout->analog_rates;
3474 runtime->hw.formats = mout->analog_formats;
3475 hinfo->maxbps = mout->analog_maxbps;
3477 if (!mout->spdif_rates) {
3478 snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
3480 &mout->spdif_formats,
3481 &mout->spdif_maxbps);
3483 mutex_lock(&codec->spdif_mutex);
3484 if (mout->share_spdif) {
3485 if ((runtime->hw.rates & mout->spdif_rates) &&
3486 (runtime->hw.formats & mout->spdif_formats)) {
3487 runtime->hw.rates &= mout->spdif_rates;
3488 runtime->hw.formats &= mout->spdif_formats;
3489 if (mout->spdif_maxbps < hinfo->maxbps)
3490 hinfo->maxbps = mout->spdif_maxbps;
3492 mout->share_spdif = 0;
3493 /* FIXME: need notify? */
3496 mutex_unlock(&codec->spdif_mutex);
3498 return snd_pcm_hw_constraint_step(substream->runtime, 0,
3499 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
3501 EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_open);
3504 * set up the i/o for analog out
3505 * when the digital out is available, copy the front out to digital out, too.
3507 int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
3508 struct hda_multi_out *mout,
3509 unsigned int stream_tag,
3510 unsigned int format,
3511 struct snd_pcm_substream *substream)
3513 hda_nid_t *nids = mout->dac_nids;
3514 int chs = substream->runtime->channels;
3517 mutex_lock(&codec->spdif_mutex);
3518 if (mout->dig_out_nid && mout->share_spdif &&
3519 mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
3521 snd_hda_is_supported_format(codec, mout->dig_out_nid,
3523 !(codec->spdif_status & IEC958_AES0_NONAUDIO)) {
3524 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
3525 setup_dig_out_stream(codec, mout->dig_out_nid,
3526 stream_tag, format);
3528 mout->dig_out_used = 0;
3529 cleanup_dig_out_stream(codec, mout->dig_out_nid);
3532 mutex_unlock(&codec->spdif_mutex);
3535 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
3537 if (!mout->no_share_stream &&
3538 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
3539 /* headphone out will just decode front left/right (stereo) */
3540 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
3542 /* extra outputs copied from front */
3543 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
3544 if (!mout->no_share_stream && mout->extra_out_nid[i])
3545 snd_hda_codec_setup_stream(codec,
3546 mout->extra_out_nid[i],
3547 stream_tag, 0, format);
3550 for (i = 1; i < mout->num_dacs; i++) {
3551 if (chs >= (i + 1) * 2) /* independent out */
3552 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
3554 else if (!mout->no_share_stream) /* copy front */
3555 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
3560 EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_prepare);
3563 * clean up the setting for analog out
3565 int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
3566 struct hda_multi_out *mout)
3568 hda_nid_t *nids = mout->dac_nids;
3571 for (i = 0; i < mout->num_dacs; i++)
3572 snd_hda_codec_cleanup_stream(codec, nids[i]);
3574 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
3575 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
3576 if (mout->extra_out_nid[i])
3577 snd_hda_codec_cleanup_stream(codec,
3578 mout->extra_out_nid[i]);
3579 mutex_lock(&codec->spdif_mutex);
3580 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
3581 cleanup_dig_out_stream(codec, mout->dig_out_nid);
3582 mout->dig_out_used = 0;
3584 mutex_unlock(&codec->spdif_mutex);
3587 EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_cleanup);
3590 * Helper for automatic pin configuration
3593 static int is_in_nid_list(hda_nid_t nid, hda_nid_t *list)
3595 for (; *list; list++)
3603 * Sort an associated group of pins according to their sequence numbers.
3605 static void sort_pins_by_sequence(hda_nid_t * pins, short * sequences,
3612 for (i = 0; i < num_pins; i++) {
3613 for (j = i + 1; j < num_pins; j++) {
3614 if (sequences[i] > sequences[j]) {
3616 sequences[i] = sequences[j];
3628 * Parse all pin widgets and store the useful pin nids to cfg
3630 * The number of line-outs or any primary output is stored in line_outs,
3631 * and the corresponding output pins are assigned to line_out_pins[],
3632 * in the order of front, rear, CLFE, side, ...
3634 * If more extra outputs (speaker and headphone) are found, the pins are
3635 * assisnged to hp_pins[] and speaker_pins[], respectively. If no line-out jack
3636 * is detected, one of speaker of HP pins is assigned as the primary
3637 * output, i.e. to line_out_pins[0]. So, line_outs is always positive
3638 * if any analog output exists.
3640 * The analog input pins are assigned to input_pins array.
3641 * The digital input/output pins are assigned to dig_in_pin and dig_out_pin,
3644 int snd_hda_parse_pin_def_config(struct hda_codec *codec,
3645 struct auto_pin_cfg *cfg,
3646 hda_nid_t *ignore_nids)
3648 hda_nid_t nid, end_nid;
3649 short seq, assoc_line_out, assoc_speaker;
3650 short sequences_line_out[ARRAY_SIZE(cfg->line_out_pins)];
3651 short sequences_speaker[ARRAY_SIZE(cfg->speaker_pins)];
3652 short sequences_hp[ARRAY_SIZE(cfg->hp_pins)];
3654 memset(cfg, 0, sizeof(*cfg));
3656 memset(sequences_line_out, 0, sizeof(sequences_line_out));
3657 memset(sequences_speaker, 0, sizeof(sequences_speaker));
3658 memset(sequences_hp, 0, sizeof(sequences_hp));
3659 assoc_line_out = assoc_speaker = 0;
3661 end_nid = codec->start_nid + codec->num_nodes;
3662 for (nid = codec->start_nid; nid < end_nid; nid++) {
3663 unsigned int wid_caps = get_wcaps(codec, nid);
3664 unsigned int wid_type =
3665 (wid_caps & AC_WCAP_TYPE) >> AC_WCAP_TYPE_SHIFT;
3666 unsigned int def_conf;
3669 /* read all default configuration for pin complex */
3670 if (wid_type != AC_WID_PIN)
3672 /* ignore the given nids (e.g. pc-beep returns error) */
3673 if (ignore_nids && is_in_nid_list(nid, ignore_nids))
3676 def_conf = snd_hda_codec_get_pincfg(codec, nid);
3677 if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE)
3679 loc = get_defcfg_location(def_conf);
3680 switch (get_defcfg_device(def_conf)) {
3681 case AC_JACK_LINE_OUT:
3682 seq = get_defcfg_sequence(def_conf);
3683 assoc = get_defcfg_association(def_conf);
3685 if (!(wid_caps & AC_WCAP_STEREO))
3686 if (!cfg->mono_out_pin)
3687 cfg->mono_out_pin = nid;
3690 if (!assoc_line_out)
3691 assoc_line_out = assoc;
3692 else if (assoc_line_out != assoc)
3694 if (cfg->line_outs >= ARRAY_SIZE(cfg->line_out_pins))
3696 cfg->line_out_pins[cfg->line_outs] = nid;
3697 sequences_line_out[cfg->line_outs] = seq;
3700 case AC_JACK_SPEAKER:
3701 seq = get_defcfg_sequence(def_conf);
3702 assoc = get_defcfg_association(def_conf);
3705 if (! assoc_speaker)
3706 assoc_speaker = assoc;
3707 else if (assoc_speaker != assoc)
3709 if (cfg->speaker_outs >= ARRAY_SIZE(cfg->speaker_pins))
3711 cfg->speaker_pins[cfg->speaker_outs] = nid;
3712 sequences_speaker[cfg->speaker_outs] = seq;
3713 cfg->speaker_outs++;
3715 case AC_JACK_HP_OUT:
3716 seq = get_defcfg_sequence(def_conf);
3717 assoc = get_defcfg_association(def_conf);
3718 if (cfg->hp_outs >= ARRAY_SIZE(cfg->hp_pins))
3720 cfg->hp_pins[cfg->hp_outs] = nid;
3721 sequences_hp[cfg->hp_outs] = (assoc << 4) | seq;
3724 case AC_JACK_MIC_IN: {
3726 if (loc == AC_JACK_LOC_FRONT) {
3727 preferred = AUTO_PIN_FRONT_MIC;
3730 preferred = AUTO_PIN_MIC;
3731 alt = AUTO_PIN_FRONT_MIC;
3733 if (!cfg->input_pins[preferred])
3734 cfg->input_pins[preferred] = nid;
3735 else if (!cfg->input_pins[alt])
3736 cfg->input_pins[alt] = nid;
3739 case AC_JACK_LINE_IN:
3740 if (loc == AC_JACK_LOC_FRONT)
3741 cfg->input_pins[AUTO_PIN_FRONT_LINE] = nid;
3743 cfg->input_pins[AUTO_PIN_LINE] = nid;
3746 cfg->input_pins[AUTO_PIN_CD] = nid;
3749 cfg->input_pins[AUTO_PIN_AUX] = nid;
3751 case AC_JACK_SPDIF_OUT:
3752 case AC_JACK_DIG_OTHER_OUT:
3753 if (cfg->dig_outs >= ARRAY_SIZE(cfg->dig_out_pins))
3755 cfg->dig_out_pins[cfg->dig_outs] = nid;
3756 cfg->dig_out_type[cfg->dig_outs] =
3757 (loc == AC_JACK_LOC_HDMI) ?
3758 HDA_PCM_TYPE_HDMI : HDA_PCM_TYPE_SPDIF;
3761 case AC_JACK_SPDIF_IN:
3762 case AC_JACK_DIG_OTHER_IN:
3763 cfg->dig_in_pin = nid;
3764 if (loc == AC_JACK_LOC_HDMI)
3765 cfg->dig_in_type = HDA_PCM_TYPE_HDMI;
3767 cfg->dig_in_type = HDA_PCM_TYPE_SPDIF;
3773 * If no line-out is defined but multiple HPs are found,
3774 * some of them might be the real line-outs.
3776 if (!cfg->line_outs && cfg->hp_outs > 1) {
3778 while (i < cfg->hp_outs) {
3779 /* The real HPs should have the sequence 0x0f */
3780 if ((sequences_hp[i] & 0x0f) == 0x0f) {
3784 /* Move it to the line-out table */
3785 cfg->line_out_pins[cfg->line_outs] = cfg->hp_pins[i];
3786 sequences_line_out[cfg->line_outs] = sequences_hp[i];
3789 memmove(cfg->hp_pins + i, cfg->hp_pins + i + 1,
3790 sizeof(cfg->hp_pins[0]) * (cfg->hp_outs - i));
3791 memmove(sequences_hp + i - 1, sequences_hp + i,
3792 sizeof(sequences_hp[0]) * (cfg->hp_outs - i));
3796 /* sort by sequence */
3797 sort_pins_by_sequence(cfg->line_out_pins, sequences_line_out,
3799 sort_pins_by_sequence(cfg->speaker_pins, sequences_speaker,
3801 sort_pins_by_sequence(cfg->hp_pins, sequences_hp,
3804 /* if we have only one mic, make it AUTO_PIN_MIC */
3805 if (!cfg->input_pins[AUTO_PIN_MIC] &&
3806 cfg->input_pins[AUTO_PIN_FRONT_MIC]) {
3807 cfg->input_pins[AUTO_PIN_MIC] =
3808 cfg->input_pins[AUTO_PIN_FRONT_MIC];
3809 cfg->input_pins[AUTO_PIN_FRONT_MIC] = 0;
3811 /* ditto for line-in */
3812 if (!cfg->input_pins[AUTO_PIN_LINE] &&
3813 cfg->input_pins[AUTO_PIN_FRONT_LINE]) {
3814 cfg->input_pins[AUTO_PIN_LINE] =
3815 cfg->input_pins[AUTO_PIN_FRONT_LINE];
3816 cfg->input_pins[AUTO_PIN_FRONT_LINE] = 0;
3820 * FIX-UP: if no line-outs are detected, try to use speaker or HP pin
3821 * as a primary output
3823 if (!cfg->line_outs) {
3824 if (cfg->speaker_outs) {
3825 cfg->line_outs = cfg->speaker_outs;
3826 memcpy(cfg->line_out_pins, cfg->speaker_pins,
3827 sizeof(cfg->speaker_pins));
3828 cfg->speaker_outs = 0;
3829 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
3830 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
3831 } else if (cfg->hp_outs) {
3832 cfg->line_outs = cfg->hp_outs;
3833 memcpy(cfg->line_out_pins, cfg->hp_pins,
3834 sizeof(cfg->hp_pins));
3836 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
3837 cfg->line_out_type = AUTO_PIN_HP_OUT;
3841 /* Reorder the surround channels
3842 * ALSA sequence is front/surr/clfe/side
3844 * 4-ch: front/surr => OK as it is
3845 * 6-ch: front/clfe/surr
3846 * 8-ch: front/clfe/rear/side|fc
3848 switch (cfg->line_outs) {
3851 nid = cfg->line_out_pins[1];
3852 cfg->line_out_pins[1] = cfg->line_out_pins[2];
3853 cfg->line_out_pins[2] = nid;
3858 * debug prints of the parsed results
3860 snd_printd("autoconfig: line_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
3861 cfg->line_outs, cfg->line_out_pins[0], cfg->line_out_pins[1],
3862 cfg->line_out_pins[2], cfg->line_out_pins[3],
3863 cfg->line_out_pins[4]);
3864 snd_printd(" speaker_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
3865 cfg->speaker_outs, cfg->speaker_pins[0],
3866 cfg->speaker_pins[1], cfg->speaker_pins[2],
3867 cfg->speaker_pins[3], cfg->speaker_pins[4]);
3868 snd_printd(" hp_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
3869 cfg->hp_outs, cfg->hp_pins[0],
3870 cfg->hp_pins[1], cfg->hp_pins[2],
3871 cfg->hp_pins[3], cfg->hp_pins[4]);
3872 snd_printd(" mono: mono_out=0x%x\n", cfg->mono_out_pin);
3874 snd_printd(" dig-out=0x%x/0x%x\n",
3875 cfg->dig_out_pins[0], cfg->dig_out_pins[1]);
3876 snd_printd(" inputs: mic=0x%x, fmic=0x%x, line=0x%x, fline=0x%x,"
3877 " cd=0x%x, aux=0x%x\n",
3878 cfg->input_pins[AUTO_PIN_MIC],
3879 cfg->input_pins[AUTO_PIN_FRONT_MIC],
3880 cfg->input_pins[AUTO_PIN_LINE],
3881 cfg->input_pins[AUTO_PIN_FRONT_LINE],
3882 cfg->input_pins[AUTO_PIN_CD],
3883 cfg->input_pins[AUTO_PIN_AUX]);
3884 if (cfg->dig_in_pin)
3885 snd_printd(" dig-in=0x%x\n", cfg->dig_in_pin);
3889 EXPORT_SYMBOL_HDA(snd_hda_parse_pin_def_config);
3891 /* labels for input pins */
3892 const char *auto_pin_cfg_labels[AUTO_PIN_LAST] = {
3893 "Mic", "Front Mic", "Line", "Front Line", "CD", "Aux"
3895 EXPORT_SYMBOL_HDA(auto_pin_cfg_labels);
3904 * snd_hda_suspend - suspend the codecs
3907 * Returns 0 if successful.
3909 int snd_hda_suspend(struct hda_bus *bus)
3911 struct hda_codec *codec;
3913 list_for_each_entry(codec, &bus->codec_list, list) {
3914 #ifdef CONFIG_SND_HDA_POWER_SAVE
3915 if (!codec->power_on)
3918 hda_call_codec_suspend(codec);
3922 EXPORT_SYMBOL_HDA(snd_hda_suspend);
3925 * snd_hda_resume - resume the codecs
3928 * Returns 0 if successful.
3930 * This fucntion is defined only when POWER_SAVE isn't set.
3931 * In the power-save mode, the codec is resumed dynamically.
3933 int snd_hda_resume(struct hda_bus *bus)
3935 struct hda_codec *codec;
3937 list_for_each_entry(codec, &bus->codec_list, list) {
3938 if (snd_hda_codec_needs_resume(codec))
3939 hda_call_codec_resume(codec);
3943 EXPORT_SYMBOL_HDA(snd_hda_resume);
3944 #endif /* CONFIG_PM */
3950 /* get a new element from the given array
3951 * if it exceeds the pre-allocated array size, re-allocate the array
3953 void *snd_array_new(struct snd_array *array)
3955 if (array->used >= array->alloced) {
3956 int num = array->alloced + array->alloc_align;
3958 if (snd_BUG_ON(num >= 4096))
3960 nlist = kcalloc(num + 1, array->elem_size, GFP_KERNEL);
3964 memcpy(nlist, array->list,
3965 array->elem_size * array->alloced);
3968 array->list = nlist;
3969 array->alloced = num;
3971 return snd_array_elem(array, array->used++);
3973 EXPORT_SYMBOL_HDA(snd_array_new);
3975 /* free the given array elements */
3976 void snd_array_free(struct snd_array *array)
3983 EXPORT_SYMBOL_HDA(snd_array_free);
3986 * used by hda_proc.c and hda_eld.c
3988 void snd_print_pcm_rates(int pcm, char *buf, int buflen)
3990 static unsigned int rates[] = {
3991 8000, 11025, 16000, 22050, 32000, 44100, 48000, 88200,
3992 96000, 176400, 192000, 384000
3996 for (i = 0, j = 0; i < ARRAY_SIZE(rates); i++)
3998 j += snprintf(buf + j, buflen - j, " %d", rates[i]);
4000 buf[j] = '\0'; /* necessary when j == 0 */
4002 EXPORT_SYMBOL_HDA(snd_print_pcm_rates);
4004 void snd_print_pcm_bits(int pcm, char *buf, int buflen)
4006 static unsigned int bits[] = { 8, 16, 20, 24, 32 };
4009 for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
4010 if (pcm & (AC_SUPPCM_BITS_8 << i))
4011 j += snprintf(buf + j, buflen - j, " %d", bits[i]);
4013 buf[j] = '\0'; /* necessary when j == 0 */
4015 EXPORT_SYMBOL_HDA(snd_print_pcm_bits);
4017 MODULE_DESCRIPTION("HDA codec core");
4018 MODULE_LICENSE("GPL");