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"
34 #include <sound/hda_hwdep.h>
37 * vendor / preset table
40 struct hda_vendor_id {
45 /* codec vendor labels */
46 static struct hda_vendor_id hda_vendor_ids[] = {
48 { 0x1013, "Cirrus Logic" },
49 { 0x1057, "Motorola" },
50 { 0x1095, "Silicon Image" },
52 { 0x10ec, "Realtek" },
53 { 0x1102, "Creative" },
57 { 0x11d4, "Analog Devices" },
58 { 0x13f6, "C-Media" },
59 { 0x14f1, "Conexant" },
60 { 0x17e8, "Chrontel" },
62 { 0x1aec, "Wolfson Microelectronics" },
63 { 0x434d, "C-Media" },
65 { 0x8384, "SigmaTel" },
69 static DEFINE_MUTEX(preset_mutex);
70 static LIST_HEAD(hda_preset_tables);
72 int snd_hda_add_codec_preset(struct hda_codec_preset_list *preset)
74 mutex_lock(&preset_mutex);
75 list_add_tail(&preset->list, &hda_preset_tables);
76 mutex_unlock(&preset_mutex);
79 EXPORT_SYMBOL_HDA(snd_hda_add_codec_preset);
81 int snd_hda_delete_codec_preset(struct hda_codec_preset_list *preset)
83 mutex_lock(&preset_mutex);
84 list_del(&preset->list);
85 mutex_unlock(&preset_mutex);
88 EXPORT_SYMBOL_HDA(snd_hda_delete_codec_preset);
90 #ifdef CONFIG_SND_HDA_POWER_SAVE
91 static void hda_power_work(struct work_struct *work);
92 static void hda_keep_power_on(struct hda_codec *codec);
94 static inline void hda_keep_power_on(struct hda_codec *codec) {}
97 const char *snd_hda_get_jack_location(u32 cfg)
99 static char *bases[7] = {
100 "N/A", "Rear", "Front", "Left", "Right", "Top", "Bottom",
102 static unsigned char specials_idx[] = {
107 static char *specials[] = {
108 "Rear Panel", "Drive Bar",
109 "Riser", "HDMI", "ATAPI",
110 "Mobile-In", "Mobile-Out"
113 cfg = (cfg & AC_DEFCFG_LOCATION) >> AC_DEFCFG_LOCATION_SHIFT;
114 if ((cfg & 0x0f) < 7)
115 return bases[cfg & 0x0f];
116 for (i = 0; i < ARRAY_SIZE(specials_idx); i++) {
117 if (cfg == specials_idx[i])
122 EXPORT_SYMBOL_HDA(snd_hda_get_jack_location);
124 const char *snd_hda_get_jack_connectivity(u32 cfg)
126 static char *jack_locations[4] = { "Ext", "Int", "Sep", "Oth" };
128 return jack_locations[(cfg >> (AC_DEFCFG_LOCATION_SHIFT + 4)) & 3];
130 EXPORT_SYMBOL_HDA(snd_hda_get_jack_connectivity);
132 const char *snd_hda_get_jack_type(u32 cfg)
134 static char *jack_types[16] = {
135 "Line Out", "Speaker", "HP Out", "CD",
136 "SPDIF Out", "Digital Out", "Modem Line", "Modem Hand",
137 "Line In", "Aux", "Mic", "Telephony",
138 "SPDIF In", "Digitial In", "Reserved", "Other"
141 return jack_types[(cfg & AC_DEFCFG_DEVICE)
142 >> AC_DEFCFG_DEVICE_SHIFT];
144 EXPORT_SYMBOL_HDA(snd_hda_get_jack_type);
147 * Compose a 32bit command word to be sent to the HD-audio controller
149 static inline unsigned int
150 make_codec_cmd(struct hda_codec *codec, hda_nid_t nid, int direct,
151 unsigned int verb, unsigned int parm)
155 if ((codec->addr & ~0xf) || (direct & ~1) || (nid & ~0x7f) ||
156 (verb & ~0xfff) || (parm & ~0xffff)) {
157 printk(KERN_ERR "hda-codec: out of range cmd %x:%x:%x:%x:%x\n",
158 codec->addr, direct, nid, verb, parm);
162 val = (u32)codec->addr << 28;
163 val |= (u32)direct << 27;
164 val |= (u32)nid << 20;
171 * Send and receive a verb
173 static int codec_exec_verb(struct hda_codec *codec, unsigned int cmd,
176 struct hda_bus *bus = codec->bus;
185 snd_hda_power_up(codec);
186 mutex_lock(&bus->cmd_mutex);
187 err = bus->ops.command(bus, cmd);
189 *res = bus->ops.get_response(bus, codec->addr);
190 mutex_unlock(&bus->cmd_mutex);
191 snd_hda_power_down(codec);
192 if (res && *res == -1 && bus->rirb_error) {
193 if (bus->response_reset) {
194 snd_printd("hda_codec: resetting BUS due to "
195 "fatal communication error\n");
196 bus->ops.bus_reset(bus);
200 /* clear reset-flag when the communication gets recovered */
202 bus->response_reset = 0;
207 * snd_hda_codec_read - send a command and get the response
208 * @codec: the HDA codec
209 * @nid: NID to send the command
210 * @direct: direct flag
211 * @verb: the verb to send
212 * @parm: the parameter for the verb
214 * Send a single command and read the corresponding response.
216 * Returns the obtained response value, or -1 for an error.
218 unsigned int snd_hda_codec_read(struct hda_codec *codec, hda_nid_t nid,
220 unsigned int verb, unsigned int parm)
222 unsigned cmd = make_codec_cmd(codec, nid, direct, verb, parm);
224 codec_exec_verb(codec, cmd, &res);
227 EXPORT_SYMBOL_HDA(snd_hda_codec_read);
230 * snd_hda_codec_write - send a single command without waiting for response
231 * @codec: the HDA codec
232 * @nid: NID to send the command
233 * @direct: direct flag
234 * @verb: the verb to send
235 * @parm: the parameter for the verb
237 * Send a single command without waiting for response.
239 * Returns 0 if successful, or a negative error code.
241 int snd_hda_codec_write(struct hda_codec *codec, hda_nid_t nid, int direct,
242 unsigned int verb, unsigned int parm)
244 unsigned int cmd = make_codec_cmd(codec, nid, direct, verb, parm);
246 return codec_exec_verb(codec, cmd,
247 codec->bus->sync_write ? &res : NULL);
249 EXPORT_SYMBOL_HDA(snd_hda_codec_write);
252 * snd_hda_sequence_write - sequence writes
253 * @codec: the HDA codec
254 * @seq: VERB array to send
256 * Send the commands sequentially from the given array.
257 * The array must be terminated with NID=0.
259 void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq)
261 for (; seq->nid; seq++)
262 snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param);
264 EXPORT_SYMBOL_HDA(snd_hda_sequence_write);
267 * snd_hda_get_sub_nodes - get the range of sub nodes
268 * @codec: the HDA codec
270 * @start_id: the pointer to store the start NID
272 * Parse the NID and store the start NID of its sub-nodes.
273 * Returns the number of sub-nodes.
275 int snd_hda_get_sub_nodes(struct hda_codec *codec, hda_nid_t nid,
280 parm = snd_hda_param_read(codec, nid, AC_PAR_NODE_COUNT);
283 *start_id = (parm >> 16) & 0x7fff;
284 return (int)(parm & 0x7fff);
286 EXPORT_SYMBOL_HDA(snd_hda_get_sub_nodes);
289 * snd_hda_get_connections - get connection list
290 * @codec: the HDA codec
292 * @conn_list: connection list array
293 * @max_conns: max. number of connections to store
295 * Parses the connection list of the given widget and stores the list
298 * Returns the number of connections, or a negative error code.
300 int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
301 hda_nid_t *conn_list, int max_conns)
304 int i, conn_len, conns;
305 unsigned int shift, num_elems, mask;
309 if (snd_BUG_ON(!conn_list || max_conns <= 0))
312 wcaps = get_wcaps(codec, nid);
313 if (!(wcaps & AC_WCAP_CONN_LIST) &&
314 get_wcaps_type(wcaps) != AC_WID_VOL_KNB) {
315 snd_printk(KERN_WARNING "hda_codec: "
316 "connection list not available for 0x%x\n", nid);
320 parm = snd_hda_param_read(codec, nid, AC_PAR_CONNLIST_LEN);
321 if (parm & AC_CLIST_LONG) {
330 conn_len = parm & AC_CLIST_LENGTH;
331 mask = (1 << (shift-1)) - 1;
334 return 0; /* no connection */
337 /* single connection */
338 parm = snd_hda_codec_read(codec, nid, 0,
339 AC_VERB_GET_CONNECT_LIST, 0);
340 if (parm == -1 && codec->bus->rirb_error)
342 conn_list[0] = parm & mask;
346 /* multi connection */
349 for (i = 0; i < conn_len; i++) {
353 if (i % num_elems == 0) {
354 parm = snd_hda_codec_read(codec, nid, 0,
355 AC_VERB_GET_CONNECT_LIST, i);
356 if (parm == -1 && codec->bus->rirb_error)
359 range_val = !!(parm & (1 << (shift-1))); /* ranges */
362 snd_printk(KERN_WARNING "hda_codec: "
363 "invalid CONNECT_LIST verb %x[%i]:%x\n",
369 /* ranges between the previous and this one */
370 if (!prev_nid || prev_nid >= val) {
371 snd_printk(KERN_WARNING "hda_codec: "
372 "invalid dep_range_val %x:%x\n",
376 for (n = prev_nid + 1; n <= val; n++) {
377 if (conns >= max_conns) {
379 "Too many connections\n");
382 conn_list[conns++] = n;
385 if (conns >= max_conns) {
386 snd_printk(KERN_ERR "Too many connections\n");
389 conn_list[conns++] = val;
395 EXPORT_SYMBOL_HDA(snd_hda_get_connections);
399 * snd_hda_queue_unsol_event - add an unsolicited event to queue
401 * @res: unsolicited event (lower 32bit of RIRB entry)
402 * @res_ex: codec addr and flags (upper 32bit or RIRB entry)
404 * Adds the given event to the queue. The events are processed in
405 * the workqueue asynchronously. Call this function in the interrupt
406 * hanlder when RIRB receives an unsolicited event.
408 * Returns 0 if successful, or a negative error code.
410 int snd_hda_queue_unsol_event(struct hda_bus *bus, u32 res, u32 res_ex)
412 struct hda_bus_unsolicited *unsol;
419 wp = (unsol->wp + 1) % HDA_UNSOL_QUEUE_SIZE;
423 unsol->queue[wp] = res;
424 unsol->queue[wp + 1] = res_ex;
426 queue_work(bus->workq, &unsol->work);
430 EXPORT_SYMBOL_HDA(snd_hda_queue_unsol_event);
433 * process queued unsolicited events
435 static void process_unsol_events(struct work_struct *work)
437 struct hda_bus_unsolicited *unsol =
438 container_of(work, struct hda_bus_unsolicited, work);
439 struct hda_bus *bus = unsol->bus;
440 struct hda_codec *codec;
441 unsigned int rp, caddr, res;
443 while (unsol->rp != unsol->wp) {
444 rp = (unsol->rp + 1) % HDA_UNSOL_QUEUE_SIZE;
447 res = unsol->queue[rp];
448 caddr = unsol->queue[rp + 1];
449 if (!(caddr & (1 << 4))) /* no unsolicited event? */
451 codec = bus->caddr_tbl[caddr & 0x0f];
452 if (codec && codec->patch_ops.unsol_event)
453 codec->patch_ops.unsol_event(codec, res);
458 * initialize unsolicited queue
460 static int init_unsol_queue(struct hda_bus *bus)
462 struct hda_bus_unsolicited *unsol;
464 if (bus->unsol) /* already initialized */
467 unsol = kzalloc(sizeof(*unsol), GFP_KERNEL);
469 snd_printk(KERN_ERR "hda_codec: "
470 "can't allocate unsolicited queue\n");
473 INIT_WORK(&unsol->work, process_unsol_events);
482 static void snd_hda_codec_free(struct hda_codec *codec);
484 static int snd_hda_bus_free(struct hda_bus *bus)
486 struct hda_codec *codec, *n;
491 flush_workqueue(bus->workq);
494 list_for_each_entry_safe(codec, n, &bus->codec_list, list) {
495 snd_hda_codec_free(codec);
497 if (bus->ops.private_free)
498 bus->ops.private_free(bus);
500 destroy_workqueue(bus->workq);
505 static int snd_hda_bus_dev_free(struct snd_device *device)
507 struct hda_bus *bus = device->device_data;
509 return snd_hda_bus_free(bus);
512 #ifdef CONFIG_SND_HDA_HWDEP
513 static int snd_hda_bus_dev_register(struct snd_device *device)
515 struct hda_bus *bus = device->device_data;
516 struct hda_codec *codec;
517 list_for_each_entry(codec, &bus->codec_list, list) {
518 snd_hda_hwdep_add_sysfs(codec);
519 snd_hda_hwdep_add_power_sysfs(codec);
524 #define snd_hda_bus_dev_register NULL
528 * snd_hda_bus_new - create a HDA bus
529 * @card: the card entry
530 * @temp: the template for hda_bus information
531 * @busp: the pointer to store the created bus instance
533 * Returns 0 if successful, or a negative error code.
535 int /*__devinit*/ snd_hda_bus_new(struct snd_card *card,
536 const struct hda_bus_template *temp,
537 struct hda_bus **busp)
541 static struct snd_device_ops dev_ops = {
542 .dev_register = snd_hda_bus_dev_register,
543 .dev_free = snd_hda_bus_dev_free,
546 if (snd_BUG_ON(!temp))
548 if (snd_BUG_ON(!temp->ops.command || !temp->ops.get_response))
554 bus = kzalloc(sizeof(*bus), GFP_KERNEL);
556 snd_printk(KERN_ERR "can't allocate struct hda_bus\n");
561 bus->private_data = temp->private_data;
562 bus->pci = temp->pci;
563 bus->modelname = temp->modelname;
564 bus->power_save = temp->power_save;
565 bus->ops = temp->ops;
567 mutex_init(&bus->cmd_mutex);
568 INIT_LIST_HEAD(&bus->codec_list);
570 snprintf(bus->workq_name, sizeof(bus->workq_name),
571 "hd-audio%d", card->number);
572 bus->workq = create_singlethread_workqueue(bus->workq_name);
574 snd_printk(KERN_ERR "cannot create workqueue %s\n",
580 err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops);
582 snd_hda_bus_free(bus);
589 EXPORT_SYMBOL_HDA(snd_hda_bus_new);
591 #ifdef CONFIG_SND_HDA_GENERIC
592 #define is_generic_config(codec) \
593 (codec->modelname && !strcmp(codec->modelname, "generic"))
595 #define is_generic_config(codec) 0
599 #define HDA_MODREQ_MAX_COUNT 2 /* two request_modules()'s */
601 #define HDA_MODREQ_MAX_COUNT 0 /* all presets are statically linked */
605 * find a matching codec preset
607 static const struct hda_codec_preset *
608 find_codec_preset(struct hda_codec *codec)
610 struct hda_codec_preset_list *tbl;
611 const struct hda_codec_preset *preset;
612 int mod_requested = 0;
614 if (is_generic_config(codec))
615 return NULL; /* use the generic parser */
618 mutex_lock(&preset_mutex);
619 list_for_each_entry(tbl, &hda_preset_tables, list) {
620 if (!try_module_get(tbl->owner)) {
621 snd_printk(KERN_ERR "hda_codec: cannot module_get\n");
624 for (preset = tbl->preset; preset->id; preset++) {
625 u32 mask = preset->mask;
626 if (preset->afg && preset->afg != codec->afg)
628 if (preset->mfg && preset->mfg != codec->mfg)
632 if (preset->id == (codec->vendor_id & mask) &&
634 preset->rev == codec->revision_id)) {
635 mutex_unlock(&preset_mutex);
636 codec->owner = tbl->owner;
640 module_put(tbl->owner);
642 mutex_unlock(&preset_mutex);
644 if (mod_requested < HDA_MODREQ_MAX_COUNT) {
647 snprintf(name, sizeof(name), "snd-hda-codec-id:%08x",
650 snprintf(name, sizeof(name), "snd-hda-codec-id:%04x*",
651 (codec->vendor_id >> 16) & 0xffff);
652 request_module(name);
660 * get_codec_name - store the codec name
662 static int get_codec_name(struct hda_codec *codec)
664 const struct hda_vendor_id *c;
665 const char *vendor = NULL;
666 u16 vendor_id = codec->vendor_id >> 16;
669 if (codec->vendor_name)
672 for (c = hda_vendor_ids; c->id; c++) {
673 if (c->id == vendor_id) {
679 sprintf(tmp, "Generic %04x", vendor_id);
682 codec->vendor_name = kstrdup(vendor, GFP_KERNEL);
683 if (!codec->vendor_name)
687 if (codec->chip_name)
690 if (codec->preset && codec->preset->name)
691 codec->chip_name = kstrdup(codec->preset->name, GFP_KERNEL);
693 sprintf(tmp, "ID %x", codec->vendor_id & 0xffff);
694 codec->chip_name = kstrdup(tmp, GFP_KERNEL);
696 if (!codec->chip_name)
702 * look for an AFG and MFG nodes
704 static void /*__devinit*/ setup_fg_nodes(struct hda_codec *codec)
706 int i, total_nodes, function_id;
709 total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid);
710 for (i = 0; i < total_nodes; i++, nid++) {
711 function_id = snd_hda_param_read(codec, nid,
712 AC_PAR_FUNCTION_TYPE) & 0xff;
713 switch (function_id) {
714 case AC_GRP_AUDIO_FUNCTION:
716 codec->function_id = function_id;
718 case AC_GRP_MODEM_FUNCTION:
720 codec->function_id = function_id;
729 * read widget caps for each widget and store in cache
731 static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
736 codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node,
738 codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL);
741 nid = codec->start_nid;
742 for (i = 0; i < codec->num_nodes; i++, nid++)
743 codec->wcaps[i] = snd_hda_param_read(codec, nid,
744 AC_PAR_AUDIO_WIDGET_CAP);
748 /* read all pin default configurations and save codec->init_pins */
749 static int read_pin_defaults(struct hda_codec *codec)
752 hda_nid_t nid = codec->start_nid;
754 for (i = 0; i < codec->num_nodes; i++, nid++) {
755 struct hda_pincfg *pin;
756 unsigned int wcaps = get_wcaps(codec, nid);
757 unsigned int wid_type = get_wcaps_type(wcaps);
758 if (wid_type != AC_WID_PIN)
760 pin = snd_array_new(&codec->init_pins);
764 pin->cfg = snd_hda_codec_read(codec, nid, 0,
765 AC_VERB_GET_CONFIG_DEFAULT, 0);
770 /* look up the given pin config list and return the item matching with NID */
771 static struct hda_pincfg *look_up_pincfg(struct hda_codec *codec,
772 struct snd_array *array,
776 for (i = 0; i < array->used; i++) {
777 struct hda_pincfg *pin = snd_array_elem(array, i);
784 /* write a config value for the given NID */
785 static void set_pincfg(struct hda_codec *codec, hda_nid_t nid,
789 for (i = 0; i < 4; i++) {
790 snd_hda_codec_write(codec, nid, 0,
791 AC_VERB_SET_CONFIG_DEFAULT_BYTES_0 + i,
797 /* set the current pin config value for the given NID.
798 * the value is cached, and read via snd_hda_codec_get_pincfg()
800 int snd_hda_add_pincfg(struct hda_codec *codec, struct snd_array *list,
801 hda_nid_t nid, unsigned int cfg)
803 struct hda_pincfg *pin;
806 oldcfg = snd_hda_codec_get_pincfg(codec, nid);
807 pin = look_up_pincfg(codec, list, nid);
809 pin = snd_array_new(list);
816 /* change only when needed; e.g. if the pincfg is already present
817 * in user_pins[], don't write it
819 cfg = snd_hda_codec_get_pincfg(codec, nid);
821 set_pincfg(codec, nid, cfg);
825 int snd_hda_codec_set_pincfg(struct hda_codec *codec,
826 hda_nid_t nid, unsigned int cfg)
828 return snd_hda_add_pincfg(codec, &codec->driver_pins, nid, cfg);
830 EXPORT_SYMBOL_HDA(snd_hda_codec_set_pincfg);
832 /* get the current pin config value of the given pin NID */
833 unsigned int snd_hda_codec_get_pincfg(struct hda_codec *codec, hda_nid_t nid)
835 struct hda_pincfg *pin;
837 #ifdef CONFIG_SND_HDA_HWDEP
838 pin = look_up_pincfg(codec, &codec->user_pins, nid);
842 pin = look_up_pincfg(codec, &codec->driver_pins, nid);
845 pin = look_up_pincfg(codec, &codec->init_pins, nid);
850 EXPORT_SYMBOL_HDA(snd_hda_codec_get_pincfg);
852 /* restore all current pin configs */
853 static void restore_pincfgs(struct hda_codec *codec)
856 for (i = 0; i < codec->init_pins.used; i++) {
857 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
858 set_pincfg(codec, pin->nid,
859 snd_hda_codec_get_pincfg(codec, pin->nid));
863 static void init_hda_cache(struct hda_cache_rec *cache,
864 unsigned int record_size);
865 static void free_hda_cache(struct hda_cache_rec *cache);
867 /* restore the initial pin cfgs and release all pincfg lists */
868 static void restore_init_pincfgs(struct hda_codec *codec)
870 /* first free driver_pins and user_pins, then call restore_pincfg
871 * so that only the values in init_pins are restored
873 snd_array_free(&codec->driver_pins);
874 #ifdef CONFIG_SND_HDA_HWDEP
875 snd_array_free(&codec->user_pins);
877 restore_pincfgs(codec);
878 snd_array_free(&codec->init_pins);
884 static void snd_hda_codec_free(struct hda_codec *codec)
888 restore_init_pincfgs(codec);
889 #ifdef CONFIG_SND_HDA_POWER_SAVE
890 cancel_delayed_work(&codec->power_work);
891 flush_workqueue(codec->bus->workq);
893 list_del(&codec->list);
894 snd_array_free(&codec->mixers);
895 codec->bus->caddr_tbl[codec->addr] = NULL;
896 if (codec->patch_ops.free)
897 codec->patch_ops.free(codec);
898 module_put(codec->owner);
899 free_hda_cache(&codec->amp_cache);
900 free_hda_cache(&codec->cmd_cache);
901 kfree(codec->vendor_name);
902 kfree(codec->chip_name);
903 kfree(codec->modelname);
908 static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
909 unsigned int power_state);
912 * snd_hda_codec_new - create a HDA codec
913 * @bus: the bus to assign
914 * @codec_addr: the codec address
915 * @codecp: the pointer to store the generated codec
917 * Returns 0 if successful, or a negative error code.
919 int /*__devinit*/ snd_hda_codec_new(struct hda_bus *bus, unsigned int codec_addr,
920 struct hda_codec **codecp)
922 struct hda_codec *codec;
926 if (snd_BUG_ON(!bus))
928 if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS))
931 if (bus->caddr_tbl[codec_addr]) {
932 snd_printk(KERN_ERR "hda_codec: "
933 "address 0x%x is already occupied\n", codec_addr);
937 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
939 snd_printk(KERN_ERR "can't allocate struct hda_codec\n");
944 codec->addr = codec_addr;
945 mutex_init(&codec->spdif_mutex);
946 mutex_init(&codec->control_mutex);
947 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
948 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
949 snd_array_init(&codec->mixers, sizeof(struct hda_nid_item), 60);
950 snd_array_init(&codec->init_pins, sizeof(struct hda_pincfg), 16);
951 snd_array_init(&codec->driver_pins, sizeof(struct hda_pincfg), 16);
952 if (codec->bus->modelname) {
953 codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL);
954 if (!codec->modelname) {
955 snd_hda_codec_free(codec);
960 #ifdef CONFIG_SND_HDA_POWER_SAVE
961 INIT_DELAYED_WORK(&codec->power_work, hda_power_work);
962 /* snd_hda_codec_new() marks the codec as power-up, and leave it as is.
963 * the caller has to power down appropriatley after initialization
966 hda_keep_power_on(codec);
969 list_add_tail(&codec->list, &bus->codec_list);
970 bus->caddr_tbl[codec_addr] = codec;
972 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
974 if (codec->vendor_id == -1)
975 /* read again, hopefully the access method was corrected
976 * in the last read...
978 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
980 codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT,
981 AC_PAR_SUBSYSTEM_ID);
982 codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT,
985 setup_fg_nodes(codec);
986 if (!codec->afg && !codec->mfg) {
987 snd_printdd("hda_codec: no AFG or MFG node found\n");
992 err = read_widget_caps(codec, codec->afg ? codec->afg : codec->mfg);
994 snd_printk(KERN_ERR "hda_codec: cannot malloc\n");
997 err = read_pin_defaults(codec);
1001 if (!codec->subsystem_id) {
1002 hda_nid_t nid = codec->afg ? codec->afg : codec->mfg;
1003 codec->subsystem_id =
1004 snd_hda_codec_read(codec, nid, 0,
1005 AC_VERB_GET_SUBSYSTEM_ID, 0);
1008 /* power-up all before initialization */
1009 hda_set_power_state(codec,
1010 codec->afg ? codec->afg : codec->mfg,
1013 snd_hda_codec_proc_new(codec);
1015 snd_hda_create_hwdep(codec);
1017 sprintf(component, "HDA:%08x,%08x,%08x", codec->vendor_id,
1018 codec->subsystem_id, codec->revision_id);
1019 snd_component_add(codec->bus->card, component);
1026 snd_hda_codec_free(codec);
1029 EXPORT_SYMBOL_HDA(snd_hda_codec_new);
1031 int snd_hda_codec_configure(struct hda_codec *codec)
1035 codec->preset = find_codec_preset(codec);
1036 if (!codec->vendor_name || !codec->chip_name) {
1037 err = get_codec_name(codec);
1041 /* audio codec should override the mixer name */
1042 if (codec->afg || !*codec->bus->card->mixername)
1043 snprintf(codec->bus->card->mixername,
1044 sizeof(codec->bus->card->mixername),
1045 "%s %s", codec->vendor_name, codec->chip_name);
1047 if (is_generic_config(codec)) {
1048 err = snd_hda_parse_generic_codec(codec);
1051 if (codec->preset && codec->preset->patch) {
1052 err = codec->preset->patch(codec);
1056 /* call the default parser */
1057 err = snd_hda_parse_generic_codec(codec);
1059 printk(KERN_ERR "hda-codec: No codec parser is available\n");
1062 if (!err && codec->patch_ops.unsol_event)
1063 err = init_unsol_queue(codec->bus);
1066 EXPORT_SYMBOL_HDA(snd_hda_codec_configure);
1069 * snd_hda_codec_setup_stream - set up the codec for streaming
1070 * @codec: the CODEC to set up
1071 * @nid: the NID to set up
1072 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
1073 * @channel_id: channel id to pass, zero based.
1074 * @format: stream format.
1076 void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
1078 int channel_id, int format)
1083 snd_printdd("hda_codec_setup_stream: "
1084 "NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
1085 nid, stream_tag, channel_id, format);
1086 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID,
1087 (stream_tag << 4) | channel_id);
1089 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, format);
1091 EXPORT_SYMBOL_HDA(snd_hda_codec_setup_stream);
1093 void snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid)
1098 snd_printdd("hda_codec_cleanup_stream: NID=0x%x\n", nid);
1099 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
1100 #if 0 /* keep the format */
1102 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0);
1105 EXPORT_SYMBOL_HDA(snd_hda_codec_cleanup_stream);
1108 * amp access functions
1111 /* FIXME: more better hash key? */
1112 #define HDA_HASH_KEY(nid,dir,idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
1113 #define HDA_HASH_PINCAP_KEY(nid) (u32)((nid) + (0x02 << 24))
1114 #define HDA_HASH_PARPCM_KEY(nid) (u32)((nid) + (0x03 << 24))
1115 #define HDA_HASH_PARSTR_KEY(nid) (u32)((nid) + (0x04 << 24))
1116 #define INFO_AMP_CAPS (1<<0)
1117 #define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
1119 /* initialize the hash table */
1120 static void /*__devinit*/ init_hda_cache(struct hda_cache_rec *cache,
1121 unsigned int record_size)
1123 memset(cache, 0, sizeof(*cache));
1124 memset(cache->hash, 0xff, sizeof(cache->hash));
1125 snd_array_init(&cache->buf, record_size, 64);
1128 static void free_hda_cache(struct hda_cache_rec *cache)
1130 snd_array_free(&cache->buf);
1133 /* query the hash. allocate an entry if not found. */
1134 static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache,
1137 u16 idx = key % (u16)ARRAY_SIZE(cache->hash);
1138 u16 cur = cache->hash[idx];
1139 struct hda_cache_head *info;
1141 while (cur != 0xffff) {
1142 info = snd_array_elem(&cache->buf, cur);
1143 if (info->key == key)
1148 /* add a new hash entry */
1149 info = snd_array_new(&cache->buf);
1152 cur = snd_array_index(&cache->buf, info);
1155 info->next = cache->hash[idx];
1156 cache->hash[idx] = cur;
1161 /* query and allocate an amp hash entry */
1162 static inline struct hda_amp_info *
1163 get_alloc_amp_hash(struct hda_codec *codec, u32 key)
1165 return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key);
1169 * query AMP capabilities for the given widget and direction
1171 u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
1173 struct hda_amp_info *info;
1175 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, 0));
1178 if (!(info->head.val & INFO_AMP_CAPS)) {
1179 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
1181 info->amp_caps = snd_hda_param_read(codec, nid,
1182 direction == HDA_OUTPUT ?
1183 AC_PAR_AMP_OUT_CAP :
1186 info->head.val |= INFO_AMP_CAPS;
1188 return info->amp_caps;
1190 EXPORT_SYMBOL_HDA(query_amp_caps);
1192 int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
1195 struct hda_amp_info *info;
1197 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, dir, 0));
1200 info->amp_caps = caps;
1201 info->head.val |= INFO_AMP_CAPS;
1204 EXPORT_SYMBOL_HDA(snd_hda_override_amp_caps);
1207 query_caps_hash(struct hda_codec *codec, hda_nid_t nid, u32 key,
1208 unsigned int (*func)(struct hda_codec *, hda_nid_t))
1210 struct hda_amp_info *info;
1212 info = get_alloc_amp_hash(codec, key);
1215 if (!info->head.val) {
1216 info->head.val |= INFO_AMP_CAPS;
1217 info->amp_caps = func(codec, nid);
1219 return info->amp_caps;
1222 static unsigned int read_pin_cap(struct hda_codec *codec, hda_nid_t nid)
1224 return snd_hda_param_read(codec, nid, AC_PAR_PIN_CAP);
1227 u32 snd_hda_query_pin_caps(struct hda_codec *codec, hda_nid_t nid)
1229 return query_caps_hash(codec, nid, HDA_HASH_PINCAP_KEY(nid),
1232 EXPORT_SYMBOL_HDA(snd_hda_query_pin_caps);
1235 * read the current volume to info
1236 * if the cache exists, read the cache value.
1238 static unsigned int get_vol_mute(struct hda_codec *codec,
1239 struct hda_amp_info *info, hda_nid_t nid,
1240 int ch, int direction, int index)
1244 if (info->head.val & INFO_AMP_VOL(ch))
1245 return info->vol[ch];
1247 parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
1248 parm |= direction == HDA_OUTPUT ? AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
1250 val = snd_hda_codec_read(codec, nid, 0,
1251 AC_VERB_GET_AMP_GAIN_MUTE, parm);
1252 info->vol[ch] = val & 0xff;
1253 info->head.val |= INFO_AMP_VOL(ch);
1254 return info->vol[ch];
1258 * write the current volume in info to the h/w and update the cache
1260 static void put_vol_mute(struct hda_codec *codec, struct hda_amp_info *info,
1261 hda_nid_t nid, int ch, int direction, int index,
1266 parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
1267 parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
1268 parm |= index << AC_AMP_SET_INDEX_SHIFT;
1270 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
1271 info->vol[ch] = val;
1275 * read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
1277 int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
1278 int direction, int index)
1280 struct hda_amp_info *info;
1281 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
1284 return get_vol_mute(codec, info, nid, ch, direction, index);
1286 EXPORT_SYMBOL_HDA(snd_hda_codec_amp_read);
1289 * update the AMP value, mask = bit mask to set, val = the value
1291 int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
1292 int direction, int idx, int mask, int val)
1294 struct hda_amp_info *info;
1296 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, idx));
1300 val |= get_vol_mute(codec, info, nid, ch, direction, idx) & ~mask;
1301 if (info->vol[ch] == val)
1303 put_vol_mute(codec, info, nid, ch, direction, idx, val);
1306 EXPORT_SYMBOL_HDA(snd_hda_codec_amp_update);
1309 * update the AMP stereo with the same mask and value
1311 int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
1312 int direction, int idx, int mask, int val)
1315 for (ch = 0; ch < 2; ch++)
1316 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
1320 EXPORT_SYMBOL_HDA(snd_hda_codec_amp_stereo);
1322 #ifdef SND_HDA_NEEDS_RESUME
1323 /* resume the all amp commands from the cache */
1324 void snd_hda_codec_resume_amp(struct hda_codec *codec)
1326 struct hda_amp_info *buffer = codec->amp_cache.buf.list;
1329 for (i = 0; i < codec->amp_cache.buf.used; i++, buffer++) {
1330 u32 key = buffer->head.key;
1332 unsigned int idx, dir, ch;
1336 idx = (key >> 16) & 0xff;
1337 dir = (key >> 24) & 0xff;
1338 for (ch = 0; ch < 2; ch++) {
1339 if (!(buffer->head.val & INFO_AMP_VOL(ch)))
1341 put_vol_mute(codec, buffer, nid, ch, dir, idx,
1346 EXPORT_SYMBOL_HDA(snd_hda_codec_resume_amp);
1347 #endif /* SND_HDA_NEEDS_RESUME */
1350 int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
1351 struct snd_ctl_elem_info *uinfo)
1353 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1354 u16 nid = get_amp_nid(kcontrol);
1355 u8 chs = get_amp_channels(kcontrol);
1356 int dir = get_amp_direction(kcontrol);
1357 unsigned int ofs = get_amp_offset(kcontrol);
1360 caps = query_amp_caps(codec, nid, dir);
1362 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1364 printk(KERN_WARNING "hda_codec: "
1365 "num_steps = 0 for NID=0x%x (ctl = %s)\n", nid,
1371 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1372 uinfo->count = chs == 3 ? 2 : 1;
1373 uinfo->value.integer.min = 0;
1374 uinfo->value.integer.max = caps;
1377 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_info);
1380 static inline unsigned int
1381 read_amp_value(struct hda_codec *codec, hda_nid_t nid,
1382 int ch, int dir, int idx, unsigned int ofs)
1385 val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
1386 val &= HDA_AMP_VOLMASK;
1395 update_amp_value(struct hda_codec *codec, hda_nid_t nid,
1396 int ch, int dir, int idx, unsigned int ofs,
1401 return snd_hda_codec_amp_update(codec, nid, ch, dir, idx,
1402 HDA_AMP_VOLMASK, val);
1405 int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
1406 struct snd_ctl_elem_value *ucontrol)
1408 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1409 hda_nid_t nid = get_amp_nid(kcontrol);
1410 int chs = get_amp_channels(kcontrol);
1411 int dir = get_amp_direction(kcontrol);
1412 int idx = get_amp_index(kcontrol);
1413 unsigned int ofs = get_amp_offset(kcontrol);
1414 long *valp = ucontrol->value.integer.value;
1417 *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs);
1419 *valp = read_amp_value(codec, nid, 1, dir, idx, ofs);
1422 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_get);
1424 int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
1425 struct snd_ctl_elem_value *ucontrol)
1427 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1428 hda_nid_t nid = get_amp_nid(kcontrol);
1429 int chs = get_amp_channels(kcontrol);
1430 int dir = get_amp_direction(kcontrol);
1431 int idx = get_amp_index(kcontrol);
1432 unsigned int ofs = get_amp_offset(kcontrol);
1433 long *valp = ucontrol->value.integer.value;
1436 snd_hda_power_up(codec);
1438 change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp);
1442 change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp);
1443 snd_hda_power_down(codec);
1446 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_put);
1448 int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
1449 unsigned int size, unsigned int __user *_tlv)
1451 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1452 hda_nid_t nid = get_amp_nid(kcontrol);
1453 int dir = get_amp_direction(kcontrol);
1454 unsigned int ofs = get_amp_offset(kcontrol);
1455 u32 caps, val1, val2;
1457 if (size < 4 * sizeof(unsigned int))
1459 caps = query_amp_caps(codec, nid, dir);
1460 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
1461 val2 = (val2 + 1) * 25;
1462 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
1464 val1 = ((int)val1) * ((int)val2);
1465 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
1467 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
1469 if (put_user(val1, _tlv + 2))
1471 if (put_user(val2, _tlv + 3))
1475 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_tlv);
1478 * set (static) TLV for virtual master volume; recalculated as max 0dB
1480 void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
1486 caps = query_amp_caps(codec, nid, dir);
1487 nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1488 step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
1489 step = (step + 1) * 25;
1490 tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
1491 tlv[1] = 2 * sizeof(unsigned int);
1492 tlv[2] = -nums * step;
1495 EXPORT_SYMBOL_HDA(snd_hda_set_vmaster_tlv);
1497 /* find a mixer control element with the given name */
1498 static struct snd_kcontrol *
1499 _snd_hda_find_mixer_ctl(struct hda_codec *codec,
1500 const char *name, int idx)
1502 struct snd_ctl_elem_id id;
1503 memset(&id, 0, sizeof(id));
1504 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1506 if (snd_BUG_ON(strlen(name) >= sizeof(id.name)))
1508 strcpy(id.name, name);
1509 return snd_ctl_find_id(codec->bus->card, &id);
1512 struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
1515 return _snd_hda_find_mixer_ctl(codec, name, 0);
1517 EXPORT_SYMBOL_HDA(snd_hda_find_mixer_ctl);
1519 /* Add a control element and assign to the codec */
1520 int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid,
1521 struct snd_kcontrol *kctl)
1524 struct hda_nid_item *item;
1526 if (kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) {
1528 nid = kctl->id.subdevice & 0xffff;
1529 kctl->id.subdevice = 0;
1531 err = snd_ctl_add(codec->bus->card, kctl);
1534 item = snd_array_new(&codec->mixers);
1541 EXPORT_SYMBOL_HDA(snd_hda_ctl_add);
1543 /* Clear all controls assigned to the given codec */
1544 void snd_hda_ctls_clear(struct hda_codec *codec)
1547 struct hda_nid_item *items = codec->mixers.list;
1548 for (i = 0; i < codec->mixers.used; i++)
1549 snd_ctl_remove(codec->bus->card, items[i].kctl);
1550 snd_array_free(&codec->mixers);
1553 /* pseudo device locking
1554 * toggle card->shutdown to allow/disallow the device access (as a hack)
1556 static int hda_lock_devices(struct snd_card *card)
1558 spin_lock(&card->files_lock);
1559 if (card->shutdown) {
1560 spin_unlock(&card->files_lock);
1564 spin_unlock(&card->files_lock);
1568 static void hda_unlock_devices(struct snd_card *card)
1570 spin_lock(&card->files_lock);
1572 spin_unlock(&card->files_lock);
1575 int snd_hda_codec_reset(struct hda_codec *codec)
1577 struct snd_card *card = codec->bus->card;
1580 if (hda_lock_devices(card) < 0)
1582 /* check whether the codec isn't used by any mixer or PCM streams */
1583 if (!list_empty(&card->ctl_files)) {
1584 hda_unlock_devices(card);
1587 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
1588 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
1591 if (cpcm->pcm->streams[0].substream_opened ||
1592 cpcm->pcm->streams[1].substream_opened) {
1593 hda_unlock_devices(card);
1598 /* OK, let it free */
1600 #ifdef CONFIG_SND_HDA_POWER_SAVE
1601 cancel_delayed_work(&codec->power_work);
1602 flush_workqueue(codec->bus->workq);
1604 snd_hda_ctls_clear(codec);
1606 for (i = 0; i < codec->num_pcms; i++) {
1607 if (codec->pcm_info[i].pcm) {
1608 snd_device_free(card, codec->pcm_info[i].pcm);
1609 clear_bit(codec->pcm_info[i].device,
1610 codec->bus->pcm_dev_bits);
1613 if (codec->patch_ops.free)
1614 codec->patch_ops.free(codec);
1615 codec->proc_widget_hook = NULL;
1617 free_hda_cache(&codec->amp_cache);
1618 free_hda_cache(&codec->cmd_cache);
1619 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
1620 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
1621 /* free only driver_pins so that init_pins + user_pins are restored */
1622 snd_array_free(&codec->driver_pins);
1623 restore_pincfgs(codec);
1624 codec->num_pcms = 0;
1625 codec->pcm_info = NULL;
1626 codec->preset = NULL;
1627 memset(&codec->patch_ops, 0, sizeof(codec->patch_ops));
1628 codec->slave_dig_outs = NULL;
1629 codec->spdif_status_reset = 0;
1630 module_put(codec->owner);
1631 codec->owner = NULL;
1633 /* allow device access again */
1634 hda_unlock_devices(card);
1638 /* create a virtual master control and add slaves */
1639 int snd_hda_add_vmaster(struct hda_codec *codec, char *name,
1640 unsigned int *tlv, const char **slaves)
1642 struct snd_kcontrol *kctl;
1646 for (s = slaves; *s && !snd_hda_find_mixer_ctl(codec, *s); s++)
1649 snd_printdd("No slave found for %s\n", name);
1652 kctl = snd_ctl_make_virtual_master(name, tlv);
1655 err = snd_hda_ctl_add(codec, 0, kctl);
1659 for (s = slaves; *s; s++) {
1660 struct snd_kcontrol *sctl;
1663 sctl = _snd_hda_find_mixer_ctl(codec, *s, i);
1666 snd_printdd("Cannot find slave %s, "
1670 err = snd_ctl_add_slave(kctl, sctl);
1678 EXPORT_SYMBOL_HDA(snd_hda_add_vmaster);
1681 int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
1682 struct snd_ctl_elem_info *uinfo)
1684 int chs = get_amp_channels(kcontrol);
1686 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1687 uinfo->count = chs == 3 ? 2 : 1;
1688 uinfo->value.integer.min = 0;
1689 uinfo->value.integer.max = 1;
1692 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_info);
1694 int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
1695 struct snd_ctl_elem_value *ucontrol)
1697 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1698 hda_nid_t nid = get_amp_nid(kcontrol);
1699 int chs = get_amp_channels(kcontrol);
1700 int dir = get_amp_direction(kcontrol);
1701 int idx = get_amp_index(kcontrol);
1702 long *valp = ucontrol->value.integer.value;
1705 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
1706 HDA_AMP_MUTE) ? 0 : 1;
1708 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
1709 HDA_AMP_MUTE) ? 0 : 1;
1712 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_get);
1714 int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
1715 struct snd_ctl_elem_value *ucontrol)
1717 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1718 hda_nid_t nid = get_amp_nid(kcontrol);
1719 int chs = get_amp_channels(kcontrol);
1720 int dir = get_amp_direction(kcontrol);
1721 int idx = get_amp_index(kcontrol);
1722 long *valp = ucontrol->value.integer.value;
1725 snd_hda_power_up(codec);
1727 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
1729 *valp ? 0 : HDA_AMP_MUTE);
1733 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
1735 *valp ? 0 : HDA_AMP_MUTE);
1736 #ifdef CONFIG_SND_HDA_POWER_SAVE
1737 if (codec->patch_ops.check_power_status)
1738 codec->patch_ops.check_power_status(codec, nid);
1740 snd_hda_power_down(codec);
1743 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put);
1745 int snd_hda_mixer_amp_switch_put_beep(struct snd_kcontrol *kcontrol,
1746 struct snd_ctl_elem_value *ucontrol)
1748 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1749 long *valp = ucontrol->value.integer.value;
1751 snd_hda_enable_beep_device(codec, *valp);
1752 return snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
1754 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put_beep);
1757 * bound volume controls
1759 * bind multiple volumes (# indices, from 0)
1762 #define AMP_VAL_IDX_SHIFT 19
1763 #define AMP_VAL_IDX_MASK (0x0f<<19)
1765 int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
1766 struct snd_ctl_elem_value *ucontrol)
1768 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1772 mutex_lock(&codec->control_mutex);
1773 pval = kcontrol->private_value;
1774 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
1775 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
1776 kcontrol->private_value = pval;
1777 mutex_unlock(&codec->control_mutex);
1780 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_get);
1782 int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
1783 struct snd_ctl_elem_value *ucontrol)
1785 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1787 int i, indices, err = 0, change = 0;
1789 mutex_lock(&codec->control_mutex);
1790 pval = kcontrol->private_value;
1791 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
1792 for (i = 0; i < indices; i++) {
1793 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
1794 (i << AMP_VAL_IDX_SHIFT);
1795 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
1800 kcontrol->private_value = pval;
1801 mutex_unlock(&codec->control_mutex);
1802 return err < 0 ? err : change;
1804 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_put);
1807 * generic bound volume/swtich controls
1809 int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
1810 struct snd_ctl_elem_info *uinfo)
1812 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1813 struct hda_bind_ctls *c;
1816 mutex_lock(&codec->control_mutex);
1817 c = (struct hda_bind_ctls *)kcontrol->private_value;
1818 kcontrol->private_value = *c->values;
1819 err = c->ops->info(kcontrol, uinfo);
1820 kcontrol->private_value = (long)c;
1821 mutex_unlock(&codec->control_mutex);
1824 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_info);
1826 int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
1827 struct snd_ctl_elem_value *ucontrol)
1829 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1830 struct hda_bind_ctls *c;
1833 mutex_lock(&codec->control_mutex);
1834 c = (struct hda_bind_ctls *)kcontrol->private_value;
1835 kcontrol->private_value = *c->values;
1836 err = c->ops->get(kcontrol, ucontrol);
1837 kcontrol->private_value = (long)c;
1838 mutex_unlock(&codec->control_mutex);
1841 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_get);
1843 int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
1844 struct snd_ctl_elem_value *ucontrol)
1846 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1847 struct hda_bind_ctls *c;
1848 unsigned long *vals;
1849 int err = 0, change = 0;
1851 mutex_lock(&codec->control_mutex);
1852 c = (struct hda_bind_ctls *)kcontrol->private_value;
1853 for (vals = c->values; *vals; vals++) {
1854 kcontrol->private_value = *vals;
1855 err = c->ops->put(kcontrol, ucontrol);
1860 kcontrol->private_value = (long)c;
1861 mutex_unlock(&codec->control_mutex);
1862 return err < 0 ? err : change;
1864 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_put);
1866 int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
1867 unsigned int size, unsigned int __user *tlv)
1869 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1870 struct hda_bind_ctls *c;
1873 mutex_lock(&codec->control_mutex);
1874 c = (struct hda_bind_ctls *)kcontrol->private_value;
1875 kcontrol->private_value = *c->values;
1876 err = c->ops->tlv(kcontrol, op_flag, size, tlv);
1877 kcontrol->private_value = (long)c;
1878 mutex_unlock(&codec->control_mutex);
1881 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_tlv);
1883 struct hda_ctl_ops snd_hda_bind_vol = {
1884 .info = snd_hda_mixer_amp_volume_info,
1885 .get = snd_hda_mixer_amp_volume_get,
1886 .put = snd_hda_mixer_amp_volume_put,
1887 .tlv = snd_hda_mixer_amp_tlv
1889 EXPORT_SYMBOL_HDA(snd_hda_bind_vol);
1891 struct hda_ctl_ops snd_hda_bind_sw = {
1892 .info = snd_hda_mixer_amp_switch_info,
1893 .get = snd_hda_mixer_amp_switch_get,
1894 .put = snd_hda_mixer_amp_switch_put,
1895 .tlv = snd_hda_mixer_amp_tlv
1897 EXPORT_SYMBOL_HDA(snd_hda_bind_sw);
1900 * SPDIF out controls
1903 static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
1904 struct snd_ctl_elem_info *uinfo)
1906 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1911 static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
1912 struct snd_ctl_elem_value *ucontrol)
1914 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
1915 IEC958_AES0_NONAUDIO |
1916 IEC958_AES0_CON_EMPHASIS_5015 |
1917 IEC958_AES0_CON_NOT_COPYRIGHT;
1918 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
1919 IEC958_AES1_CON_ORIGINAL;
1923 static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
1924 struct snd_ctl_elem_value *ucontrol)
1926 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
1927 IEC958_AES0_NONAUDIO |
1928 IEC958_AES0_PRO_EMPHASIS_5015;
1932 static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
1933 struct snd_ctl_elem_value *ucontrol)
1935 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1937 ucontrol->value.iec958.status[0] = codec->spdif_status & 0xff;
1938 ucontrol->value.iec958.status[1] = (codec->spdif_status >> 8) & 0xff;
1939 ucontrol->value.iec958.status[2] = (codec->spdif_status >> 16) & 0xff;
1940 ucontrol->value.iec958.status[3] = (codec->spdif_status >> 24) & 0xff;
1945 /* convert from SPDIF status bits to HDA SPDIF bits
1946 * bit 0 (DigEn) is always set zero (to be filled later)
1948 static unsigned short convert_from_spdif_status(unsigned int sbits)
1950 unsigned short val = 0;
1952 if (sbits & IEC958_AES0_PROFESSIONAL)
1953 val |= AC_DIG1_PROFESSIONAL;
1954 if (sbits & IEC958_AES0_NONAUDIO)
1955 val |= AC_DIG1_NONAUDIO;
1956 if (sbits & IEC958_AES0_PROFESSIONAL) {
1957 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
1958 IEC958_AES0_PRO_EMPHASIS_5015)
1959 val |= AC_DIG1_EMPHASIS;
1961 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
1962 IEC958_AES0_CON_EMPHASIS_5015)
1963 val |= AC_DIG1_EMPHASIS;
1964 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
1965 val |= AC_DIG1_COPYRIGHT;
1966 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
1967 val |= AC_DIG1_LEVEL;
1968 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
1973 /* convert to SPDIF status bits from HDA SPDIF bits
1975 static unsigned int convert_to_spdif_status(unsigned short val)
1977 unsigned int sbits = 0;
1979 if (val & AC_DIG1_NONAUDIO)
1980 sbits |= IEC958_AES0_NONAUDIO;
1981 if (val & AC_DIG1_PROFESSIONAL)
1982 sbits |= IEC958_AES0_PROFESSIONAL;
1983 if (sbits & IEC958_AES0_PROFESSIONAL) {
1984 if (sbits & AC_DIG1_EMPHASIS)
1985 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
1987 if (val & AC_DIG1_EMPHASIS)
1988 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
1989 if (!(val & AC_DIG1_COPYRIGHT))
1990 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
1991 if (val & AC_DIG1_LEVEL)
1992 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
1993 sbits |= val & (0x7f << 8);
1998 /* set digital convert verbs both for the given NID and its slaves */
1999 static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
2004 snd_hda_codec_write_cache(codec, nid, 0, verb, val);
2005 d = codec->slave_dig_outs;
2009 snd_hda_codec_write_cache(codec, *d, 0, verb, val);
2012 static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
2016 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1);
2018 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2);
2021 static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
2022 struct snd_ctl_elem_value *ucontrol)
2024 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2025 hda_nid_t nid = kcontrol->private_value;
2029 mutex_lock(&codec->spdif_mutex);
2030 codec->spdif_status = ucontrol->value.iec958.status[0] |
2031 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
2032 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
2033 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
2034 val = convert_from_spdif_status(codec->spdif_status);
2035 val |= codec->spdif_ctls & 1;
2036 change = codec->spdif_ctls != val;
2037 codec->spdif_ctls = val;
2040 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
2042 mutex_unlock(&codec->spdif_mutex);
2046 #define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
2048 static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
2049 struct snd_ctl_elem_value *ucontrol)
2051 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2053 ucontrol->value.integer.value[0] = codec->spdif_ctls & AC_DIG1_ENABLE;
2057 static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
2058 struct snd_ctl_elem_value *ucontrol)
2060 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2061 hda_nid_t nid = kcontrol->private_value;
2065 mutex_lock(&codec->spdif_mutex);
2066 val = codec->spdif_ctls & ~AC_DIG1_ENABLE;
2067 if (ucontrol->value.integer.value[0])
2068 val |= AC_DIG1_ENABLE;
2069 change = codec->spdif_ctls != val;
2071 codec->spdif_ctls = val;
2072 set_dig_out_convert(codec, nid, val & 0xff, -1);
2073 /* unmute amp switch (if any) */
2074 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
2075 (val & AC_DIG1_ENABLE))
2076 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
2079 mutex_unlock(&codec->spdif_mutex);
2083 static struct snd_kcontrol_new dig_mixes[] = {
2085 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2086 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2087 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,CON_MASK),
2088 .info = snd_hda_spdif_mask_info,
2089 .get = snd_hda_spdif_cmask_get,
2092 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2093 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2094 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,PRO_MASK),
2095 .info = snd_hda_spdif_mask_info,
2096 .get = snd_hda_spdif_pmask_get,
2099 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2100 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
2101 .info = snd_hda_spdif_mask_info,
2102 .get = snd_hda_spdif_default_get,
2103 .put = snd_hda_spdif_default_put,
2106 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2107 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,SWITCH),
2108 .info = snd_hda_spdif_out_switch_info,
2109 .get = snd_hda_spdif_out_switch_get,
2110 .put = snd_hda_spdif_out_switch_put,
2115 #define SPDIF_MAX_IDX 4 /* 4 instances should be enough to probe */
2118 * snd_hda_create_spdif_out_ctls - create Output SPDIF-related controls
2119 * @codec: the HDA codec
2120 * @nid: audio out widget NID
2122 * Creates controls related with the SPDIF output.
2123 * Called from each patch supporting the SPDIF out.
2125 * Returns 0 if successful, or a negative error code.
2127 int snd_hda_create_spdif_out_ctls(struct hda_codec *codec, hda_nid_t nid)
2130 struct snd_kcontrol *kctl;
2131 struct snd_kcontrol_new *dig_mix;
2134 for (idx = 0; idx < SPDIF_MAX_IDX; idx++) {
2135 if (!_snd_hda_find_mixer_ctl(codec, "IEC958 Playback Switch",
2139 if (idx >= SPDIF_MAX_IDX) {
2140 printk(KERN_ERR "hda_codec: too many IEC958 outputs\n");
2143 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
2144 kctl = snd_ctl_new1(dig_mix, codec);
2147 kctl->id.index = idx;
2148 kctl->private_value = nid;
2149 err = snd_hda_ctl_add(codec, nid, kctl);
2154 snd_hda_codec_read(codec, nid, 0,
2155 AC_VERB_GET_DIGI_CONVERT_1, 0);
2156 codec->spdif_status = convert_to_spdif_status(codec->spdif_ctls);
2159 EXPORT_SYMBOL_HDA(snd_hda_create_spdif_out_ctls);
2162 * SPDIF sharing with analog output
2164 static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
2165 struct snd_ctl_elem_value *ucontrol)
2167 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
2168 ucontrol->value.integer.value[0] = mout->share_spdif;
2172 static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
2173 struct snd_ctl_elem_value *ucontrol)
2175 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
2176 mout->share_spdif = !!ucontrol->value.integer.value[0];
2180 static struct snd_kcontrol_new spdif_share_sw = {
2181 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2182 .name = "IEC958 Default PCM Playback Switch",
2183 .info = snd_ctl_boolean_mono_info,
2184 .get = spdif_share_sw_get,
2185 .put = spdif_share_sw_put,
2188 int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
2189 struct hda_multi_out *mout)
2191 if (!mout->dig_out_nid)
2193 /* ATTENTION: here mout is passed as private_data, instead of codec */
2194 return snd_hda_ctl_add(codec, mout->dig_out_nid,
2195 snd_ctl_new1(&spdif_share_sw, mout));
2197 EXPORT_SYMBOL_HDA(snd_hda_create_spdif_share_sw);
2203 #define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
2205 static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
2206 struct snd_ctl_elem_value *ucontrol)
2208 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2210 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
2214 static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
2215 struct snd_ctl_elem_value *ucontrol)
2217 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2218 hda_nid_t nid = kcontrol->private_value;
2219 unsigned int val = !!ucontrol->value.integer.value[0];
2222 mutex_lock(&codec->spdif_mutex);
2223 change = codec->spdif_in_enable != val;
2225 codec->spdif_in_enable = val;
2226 snd_hda_codec_write_cache(codec, nid, 0,
2227 AC_VERB_SET_DIGI_CONVERT_1, val);
2229 mutex_unlock(&codec->spdif_mutex);
2233 static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
2234 struct snd_ctl_elem_value *ucontrol)
2236 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2237 hda_nid_t nid = kcontrol->private_value;
2241 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0);
2242 sbits = convert_to_spdif_status(val);
2243 ucontrol->value.iec958.status[0] = sbits;
2244 ucontrol->value.iec958.status[1] = sbits >> 8;
2245 ucontrol->value.iec958.status[2] = sbits >> 16;
2246 ucontrol->value.iec958.status[3] = sbits >> 24;
2250 static struct snd_kcontrol_new dig_in_ctls[] = {
2252 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2253 .name = SNDRV_CTL_NAME_IEC958("",CAPTURE,SWITCH),
2254 .info = snd_hda_spdif_in_switch_info,
2255 .get = snd_hda_spdif_in_switch_get,
2256 .put = snd_hda_spdif_in_switch_put,
2259 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2260 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2261 .name = SNDRV_CTL_NAME_IEC958("",CAPTURE,DEFAULT),
2262 .info = snd_hda_spdif_mask_info,
2263 .get = snd_hda_spdif_in_status_get,
2269 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
2270 * @codec: the HDA codec
2271 * @nid: audio in widget NID
2273 * Creates controls related with the SPDIF input.
2274 * Called from each patch supporting the SPDIF in.
2276 * Returns 0 if successful, or a negative error code.
2278 int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
2281 struct snd_kcontrol *kctl;
2282 struct snd_kcontrol_new *dig_mix;
2285 for (idx = 0; idx < SPDIF_MAX_IDX; idx++) {
2286 if (!_snd_hda_find_mixer_ctl(codec, "IEC958 Capture Switch",
2290 if (idx >= SPDIF_MAX_IDX) {
2291 printk(KERN_ERR "hda_codec: too many IEC958 inputs\n");
2294 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
2295 kctl = snd_ctl_new1(dig_mix, codec);
2298 kctl->private_value = nid;
2299 err = snd_hda_ctl_add(codec, nid, kctl);
2303 codec->spdif_in_enable =
2304 snd_hda_codec_read(codec, nid, 0,
2305 AC_VERB_GET_DIGI_CONVERT_1, 0) &
2309 EXPORT_SYMBOL_HDA(snd_hda_create_spdif_in_ctls);
2311 #ifdef SND_HDA_NEEDS_RESUME
2316 /* build a 32bit cache key with the widget id and the command parameter */
2317 #define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
2318 #define get_cmd_cache_nid(key) ((key) & 0xff)
2319 #define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
2322 * snd_hda_codec_write_cache - send a single command with caching
2323 * @codec: the HDA codec
2324 * @nid: NID to send the command
2325 * @direct: direct flag
2326 * @verb: the verb to send
2327 * @parm: the parameter for the verb
2329 * Send a single command without waiting for response.
2331 * Returns 0 if successful, or a negative error code.
2333 int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
2334 int direct, unsigned int verb, unsigned int parm)
2336 int err = snd_hda_codec_write(codec, nid, direct, verb, parm);
2337 struct hda_cache_head *c;
2342 /* parm may contain the verb stuff for get/set amp */
2343 verb = verb | (parm >> 8);
2345 key = build_cmd_cache_key(nid, verb);
2346 mutex_lock(&codec->bus->cmd_mutex);
2347 c = get_alloc_hash(&codec->cmd_cache, key);
2350 mutex_unlock(&codec->bus->cmd_mutex);
2353 EXPORT_SYMBOL_HDA(snd_hda_codec_write_cache);
2355 /* resume the all commands from the cache */
2356 void snd_hda_codec_resume_cache(struct hda_codec *codec)
2358 struct hda_cache_head *buffer = codec->cmd_cache.buf.list;
2361 for (i = 0; i < codec->cmd_cache.buf.used; i++, buffer++) {
2362 u32 key = buffer->key;
2365 snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
2366 get_cmd_cache_cmd(key), buffer->val);
2369 EXPORT_SYMBOL_HDA(snd_hda_codec_resume_cache);
2372 * snd_hda_sequence_write_cache - sequence writes with caching
2373 * @codec: the HDA codec
2374 * @seq: VERB array to send
2376 * Send the commands sequentially from the given array.
2377 * Thte commands are recorded on cache for power-save and resume.
2378 * The array must be terminated with NID=0.
2380 void snd_hda_sequence_write_cache(struct hda_codec *codec,
2381 const struct hda_verb *seq)
2383 for (; seq->nid; seq++)
2384 snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
2387 EXPORT_SYMBOL_HDA(snd_hda_sequence_write_cache);
2388 #endif /* SND_HDA_NEEDS_RESUME */
2391 * set power state of the codec
2393 static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
2394 unsigned int power_state)
2399 /* this delay seems necessary to avoid click noise at power-down */
2400 if (power_state == AC_PWRST_D3)
2402 snd_hda_codec_read(codec, fg, 0, AC_VERB_SET_POWER_STATE,
2404 /* partial workaround for "azx_get_response timeout" */
2405 if (power_state == AC_PWRST_D0)
2408 nid = codec->start_nid;
2409 for (i = 0; i < codec->num_nodes; i++, nid++) {
2410 unsigned int wcaps = get_wcaps(codec, nid);
2411 if (wcaps & AC_WCAP_POWER) {
2412 unsigned int wid_type = get_wcaps_type(wcaps);
2413 if (power_state == AC_PWRST_D3 &&
2414 wid_type == AC_WID_PIN) {
2415 unsigned int pincap;
2417 * don't power down the widget if it controls
2418 * eapd and EAPD_BTLENABLE is set.
2420 pincap = snd_hda_query_pin_caps(codec, nid);
2421 if (pincap & AC_PINCAP_EAPD) {
2422 int eapd = snd_hda_codec_read(codec,
2424 AC_VERB_GET_EAPD_BTLENABLE, 0);
2430 snd_hda_codec_write(codec, nid, 0,
2431 AC_VERB_SET_POWER_STATE,
2436 if (power_state == AC_PWRST_D0) {
2437 unsigned long end_time;
2440 /* wait until the codec reachs to D0 */
2441 end_time = jiffies + msecs_to_jiffies(500);
2443 state = snd_hda_codec_read(codec, fg, 0,
2444 AC_VERB_GET_POWER_STATE, 0);
2445 if (state == power_state)
2448 } while (time_after_eq(end_time, jiffies));
2452 #ifdef CONFIG_SND_HDA_HWDEP
2453 /* execute additional init verbs */
2454 static void hda_exec_init_verbs(struct hda_codec *codec)
2456 if (codec->init_verbs.list)
2457 snd_hda_sequence_write(codec, codec->init_verbs.list);
2460 static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
2463 #ifdef SND_HDA_NEEDS_RESUME
2465 * call suspend and power-down; used both from PM and power-save
2467 static void hda_call_codec_suspend(struct hda_codec *codec)
2469 if (codec->patch_ops.suspend)
2470 codec->patch_ops.suspend(codec, PMSG_SUSPEND);
2471 hda_set_power_state(codec,
2472 codec->afg ? codec->afg : codec->mfg,
2474 #ifdef CONFIG_SND_HDA_POWER_SAVE
2475 snd_hda_update_power_acct(codec);
2476 cancel_delayed_work(&codec->power_work);
2477 codec->power_on = 0;
2478 codec->power_transition = 0;
2479 codec->power_jiffies = jiffies;
2484 * kick up codec; used both from PM and power-save
2486 static void hda_call_codec_resume(struct hda_codec *codec)
2488 hda_set_power_state(codec,
2489 codec->afg ? codec->afg : codec->mfg,
2491 restore_pincfgs(codec); /* restore all current pin configs */
2492 hda_exec_init_verbs(codec);
2493 if (codec->patch_ops.resume)
2494 codec->patch_ops.resume(codec);
2496 if (codec->patch_ops.init)
2497 codec->patch_ops.init(codec);
2498 snd_hda_codec_resume_amp(codec);
2499 snd_hda_codec_resume_cache(codec);
2502 #endif /* SND_HDA_NEEDS_RESUME */
2506 * snd_hda_build_controls - build mixer controls
2509 * Creates mixer controls for each codec included in the bus.
2511 * Returns 0 if successful, otherwise a negative error code.
2513 int /*__devinit*/ snd_hda_build_controls(struct hda_bus *bus)
2515 struct hda_codec *codec;
2517 list_for_each_entry(codec, &bus->codec_list, list) {
2518 int err = snd_hda_codec_build_controls(codec);
2520 printk(KERN_ERR "hda_codec: cannot build controls"
2521 "for #%d (error %d)\n", codec->addr, err);
2522 err = snd_hda_codec_reset(codec);
2525 "hda_codec: cannot revert codec\n");
2532 EXPORT_SYMBOL_HDA(snd_hda_build_controls);
2534 int snd_hda_codec_build_controls(struct hda_codec *codec)
2537 hda_exec_init_verbs(codec);
2538 /* continue to initialize... */
2539 if (codec->patch_ops.init)
2540 err = codec->patch_ops.init(codec);
2541 if (!err && codec->patch_ops.build_controls)
2542 err = codec->patch_ops.build_controls(codec);
2551 struct hda_rate_tbl {
2553 unsigned int alsa_bits;
2554 unsigned int hda_fmt;
2557 static struct hda_rate_tbl rate_bits[] = {
2558 /* rate in Hz, ALSA rate bitmask, HDA format value */
2560 /* autodetected value used in snd_hda_query_supported_pcm */
2561 { 8000, SNDRV_PCM_RATE_8000, 0x0500 }, /* 1/6 x 48 */
2562 { 11025, SNDRV_PCM_RATE_11025, 0x4300 }, /* 1/4 x 44 */
2563 { 16000, SNDRV_PCM_RATE_16000, 0x0200 }, /* 1/3 x 48 */
2564 { 22050, SNDRV_PCM_RATE_22050, 0x4100 }, /* 1/2 x 44 */
2565 { 32000, SNDRV_PCM_RATE_32000, 0x0a00 }, /* 2/3 x 48 */
2566 { 44100, SNDRV_PCM_RATE_44100, 0x4000 }, /* 44 */
2567 { 48000, SNDRV_PCM_RATE_48000, 0x0000 }, /* 48 */
2568 { 88200, SNDRV_PCM_RATE_88200, 0x4800 }, /* 2 x 44 */
2569 { 96000, SNDRV_PCM_RATE_96000, 0x0800 }, /* 2 x 48 */
2570 { 176400, SNDRV_PCM_RATE_176400, 0x5800 },/* 4 x 44 */
2571 { 192000, SNDRV_PCM_RATE_192000, 0x1800 }, /* 4 x 48 */
2572 #define AC_PAR_PCM_RATE_BITS 11
2573 /* up to bits 10, 384kHZ isn't supported properly */
2575 /* not autodetected value */
2576 { 9600, SNDRV_PCM_RATE_KNOT, 0x0400 }, /* 1/5 x 48 */
2578 { 0 } /* terminator */
2582 * snd_hda_calc_stream_format - calculate format bitset
2583 * @rate: the sample rate
2584 * @channels: the number of channels
2585 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
2586 * @maxbps: the max. bps
2588 * Calculate the format bitset from the given rate, channels and th PCM format.
2590 * Return zero if invalid.
2592 unsigned int snd_hda_calc_stream_format(unsigned int rate,
2593 unsigned int channels,
2594 unsigned int format,
2595 unsigned int maxbps)
2598 unsigned int val = 0;
2600 for (i = 0; rate_bits[i].hz; i++)
2601 if (rate_bits[i].hz == rate) {
2602 val = rate_bits[i].hda_fmt;
2605 if (!rate_bits[i].hz) {
2606 snd_printdd("invalid rate %d\n", rate);
2610 if (channels == 0 || channels > 8) {
2611 snd_printdd("invalid channels %d\n", channels);
2614 val |= channels - 1;
2616 switch (snd_pcm_format_width(format)) {
2617 case 8: val |= 0x00; break;
2618 case 16: val |= 0x10; break;
2622 if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE)
2624 else if (maxbps >= 24)
2630 snd_printdd("invalid format width %d\n",
2631 snd_pcm_format_width(format));
2637 EXPORT_SYMBOL_HDA(snd_hda_calc_stream_format);
2639 static unsigned int get_pcm_param(struct hda_codec *codec, hda_nid_t nid)
2641 unsigned int val = 0;
2642 if (nid != codec->afg &&
2643 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD))
2644 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
2645 if (!val || val == -1)
2646 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
2647 if (!val || val == -1)
2652 static unsigned int query_pcm_param(struct hda_codec *codec, hda_nid_t nid)
2654 return query_caps_hash(codec, nid, HDA_HASH_PARPCM_KEY(nid),
2658 static unsigned int get_stream_param(struct hda_codec *codec, hda_nid_t nid)
2660 unsigned int streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
2661 if (!streams || streams == -1)
2662 streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
2663 if (!streams || streams == -1)
2668 static unsigned int query_stream_param(struct hda_codec *codec, hda_nid_t nid)
2670 return query_caps_hash(codec, nid, HDA_HASH_PARSTR_KEY(nid),
2675 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
2676 * @codec: the HDA codec
2677 * @nid: NID to query
2678 * @ratesp: the pointer to store the detected rate bitflags
2679 * @formatsp: the pointer to store the detected formats
2680 * @bpsp: the pointer to store the detected format widths
2682 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
2683 * or @bsps argument is ignored.
2685 * Returns 0 if successful, otherwise a negative error code.
2687 static int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
2688 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
2690 unsigned int i, val, wcaps;
2692 wcaps = get_wcaps(codec, nid);
2693 val = query_pcm_param(codec, nid);
2697 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
2699 rates |= rate_bits[i].alsa_bits;
2702 snd_printk(KERN_ERR "hda_codec: rates == 0 "
2703 "(nid=0x%x, val=0x%x, ovrd=%i)\n",
2705 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0);
2711 if (formatsp || bpsp) {
2713 unsigned int streams, bps;
2715 streams = query_stream_param(codec, nid);
2720 if (streams & AC_SUPFMT_PCM) {
2721 if (val & AC_SUPPCM_BITS_8) {
2722 formats |= SNDRV_PCM_FMTBIT_U8;
2725 if (val & AC_SUPPCM_BITS_16) {
2726 formats |= SNDRV_PCM_FMTBIT_S16_LE;
2729 if (wcaps & AC_WCAP_DIGITAL) {
2730 if (val & AC_SUPPCM_BITS_32)
2731 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
2732 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
2733 formats |= SNDRV_PCM_FMTBIT_S32_LE;
2734 if (val & AC_SUPPCM_BITS_24)
2736 else if (val & AC_SUPPCM_BITS_20)
2738 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
2739 AC_SUPPCM_BITS_32)) {
2740 formats |= SNDRV_PCM_FMTBIT_S32_LE;
2741 if (val & AC_SUPPCM_BITS_32)
2743 else if (val & AC_SUPPCM_BITS_24)
2745 else if (val & AC_SUPPCM_BITS_20)
2749 if (streams & AC_SUPFMT_FLOAT32) {
2750 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
2754 if (streams == AC_SUPFMT_AC3) {
2755 /* should be exclusive */
2756 /* temporary hack: we have still no proper support
2757 * for the direct AC3 stream...
2759 formats |= SNDRV_PCM_FMTBIT_U8;
2763 snd_printk(KERN_ERR "hda_codec: formats == 0 "
2764 "(nid=0x%x, val=0x%x, ovrd=%i, "
2767 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0,
2772 *formatsp = formats;
2781 * snd_hda_is_supported_format - check whether the given node supports
2784 * Returns 1 if supported, 0 if not.
2786 int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
2787 unsigned int format)
2790 unsigned int val = 0, rate, stream;
2792 val = query_pcm_param(codec, nid);
2796 rate = format & 0xff00;
2797 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
2798 if (rate_bits[i].hda_fmt == rate) {
2803 if (i >= AC_PAR_PCM_RATE_BITS)
2806 stream = query_stream_param(codec, nid);
2810 if (stream & AC_SUPFMT_PCM) {
2811 switch (format & 0xf0) {
2813 if (!(val & AC_SUPPCM_BITS_8))
2817 if (!(val & AC_SUPPCM_BITS_16))
2821 if (!(val & AC_SUPPCM_BITS_20))
2825 if (!(val & AC_SUPPCM_BITS_24))
2829 if (!(val & AC_SUPPCM_BITS_32))
2836 /* FIXME: check for float32 and AC3? */
2841 EXPORT_SYMBOL_HDA(snd_hda_is_supported_format);
2846 static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
2847 struct hda_codec *codec,
2848 struct snd_pcm_substream *substream)
2853 static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
2854 struct hda_codec *codec,
2855 unsigned int stream_tag,
2856 unsigned int format,
2857 struct snd_pcm_substream *substream)
2859 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
2863 static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
2864 struct hda_codec *codec,
2865 struct snd_pcm_substream *substream)
2867 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
2871 static int set_pcm_default_values(struct hda_codec *codec,
2872 struct hda_pcm_stream *info)
2876 /* query support PCM information from the given NID */
2877 if (info->nid && (!info->rates || !info->formats)) {
2878 err = snd_hda_query_supported_pcm(codec, info->nid,
2879 info->rates ? NULL : &info->rates,
2880 info->formats ? NULL : &info->formats,
2881 info->maxbps ? NULL : &info->maxbps);
2885 if (info->ops.open == NULL)
2886 info->ops.open = hda_pcm_default_open_close;
2887 if (info->ops.close == NULL)
2888 info->ops.close = hda_pcm_default_open_close;
2889 if (info->ops.prepare == NULL) {
2890 if (snd_BUG_ON(!info->nid))
2892 info->ops.prepare = hda_pcm_default_prepare;
2894 if (info->ops.cleanup == NULL) {
2895 if (snd_BUG_ON(!info->nid))
2897 info->ops.cleanup = hda_pcm_default_cleanup;
2902 const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = {
2903 "Audio", "SPDIF", "HDMI", "Modem"
2907 * get the empty PCM device number to assign
2909 static int get_empty_pcm_device(struct hda_bus *bus, int type)
2911 /* audio device indices; not linear to keep compatibility */
2912 static int audio_idx[HDA_PCM_NTYPES][5] = {
2913 [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 },
2914 [HDA_PCM_TYPE_SPDIF] = { 1, -1 },
2915 [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 },
2916 [HDA_PCM_TYPE_MODEM] = { 6, -1 },
2920 if (type >= HDA_PCM_NTYPES) {
2921 snd_printk(KERN_WARNING "Invalid PCM type %d\n", type);
2925 for (i = 0; audio_idx[type][i] >= 0 ; i++)
2926 if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits))
2927 return audio_idx[type][i];
2929 snd_printk(KERN_WARNING "Too many %s devices\n", snd_hda_pcm_type_name[type]);
2934 * attach a new PCM stream
2936 static int snd_hda_attach_pcm(struct hda_codec *codec, struct hda_pcm *pcm)
2938 struct hda_bus *bus = codec->bus;
2939 struct hda_pcm_stream *info;
2942 if (snd_BUG_ON(!pcm->name))
2944 for (stream = 0; stream < 2; stream++) {
2945 info = &pcm->stream[stream];
2946 if (info->substreams) {
2947 err = set_pcm_default_values(codec, info);
2952 return bus->ops.attach_pcm(bus, codec, pcm);
2955 /* assign all PCMs of the given codec */
2956 int snd_hda_codec_build_pcms(struct hda_codec *codec)
2961 if (!codec->num_pcms) {
2962 if (!codec->patch_ops.build_pcms)
2964 err = codec->patch_ops.build_pcms(codec);
2966 printk(KERN_ERR "hda_codec: cannot build PCMs"
2967 "for #%d (error %d)\n", codec->addr, err);
2968 err = snd_hda_codec_reset(codec);
2971 "hda_codec: cannot revert codec\n");
2976 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
2977 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
2980 if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
2981 continue; /* no substreams assigned */
2984 dev = get_empty_pcm_device(codec->bus, cpcm->pcm_type);
2986 continue; /* no fatal error */
2988 err = snd_hda_attach_pcm(codec, cpcm);
2990 printk(KERN_ERR "hda_codec: cannot attach "
2991 "PCM stream %d for codec #%d\n",
2993 continue; /* no fatal error */
3001 * snd_hda_build_pcms - build PCM information
3004 * Create PCM information for each codec included in the bus.
3006 * The build_pcms codec patch is requested to set up codec->num_pcms and
3007 * codec->pcm_info properly. The array is referred by the top-level driver
3008 * to create its PCM instances.
3009 * The allocated codec->pcm_info should be released in codec->patch_ops.free
3012 * At least, substreams, channels_min and channels_max must be filled for
3013 * each stream. substreams = 0 indicates that the stream doesn't exist.
3014 * When rates and/or formats are zero, the supported values are queried
3015 * from the given nid. The nid is used also by the default ops.prepare
3016 * and ops.cleanup callbacks.
3018 * The driver needs to call ops.open in its open callback. Similarly,
3019 * ops.close is supposed to be called in the close callback.
3020 * ops.prepare should be called in the prepare or hw_params callback
3021 * with the proper parameters for set up.
3022 * ops.cleanup should be called in hw_free for clean up of streams.
3024 * This function returns 0 if successfull, or a negative error code.
3026 int __devinit snd_hda_build_pcms(struct hda_bus *bus)
3028 struct hda_codec *codec;
3030 list_for_each_entry(codec, &bus->codec_list, list) {
3031 int err = snd_hda_codec_build_pcms(codec);
3037 EXPORT_SYMBOL_HDA(snd_hda_build_pcms);
3040 * snd_hda_check_board_config - compare the current codec with the config table
3041 * @codec: the HDA codec
3042 * @num_configs: number of config enums
3043 * @models: array of model name strings
3044 * @tbl: configuration table, terminated by null entries
3046 * Compares the modelname or PCI subsystem id of the current codec with the
3047 * given configuration table. If a matching entry is found, returns its
3048 * config value (supposed to be 0 or positive).
3050 * If no entries are matching, the function returns a negative value.
3052 int snd_hda_check_board_config(struct hda_codec *codec,
3053 int num_configs, const char **models,
3054 const struct snd_pci_quirk *tbl)
3056 if (codec->modelname && models) {
3058 for (i = 0; i < num_configs; i++) {
3060 !strcmp(codec->modelname, models[i])) {
3061 snd_printd(KERN_INFO "hda_codec: model '%s' is "
3062 "selected\n", models[i]);
3068 if (!codec->bus->pci || !tbl)
3071 tbl = snd_pci_quirk_lookup(codec->bus->pci, tbl);
3074 if (tbl->value >= 0 && tbl->value < num_configs) {
3075 #ifdef CONFIG_SND_DEBUG_VERBOSE
3077 const char *model = NULL;
3079 model = models[tbl->value];
3081 sprintf(tmp, "#%d", tbl->value);
3084 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
3085 "for config %x:%x (%s)\n",
3086 model, tbl->subvendor, tbl->subdevice,
3087 (tbl->name ? tbl->name : "Unknown device"));
3093 EXPORT_SYMBOL_HDA(snd_hda_check_board_config);
3096 * snd_hda_check_board_codec_sid_config - compare the current codec
3097 subsystem ID with the
3100 This is important for Gateway notebooks with SB450 HDA Audio
3101 where the vendor ID of the PCI device is:
3102 ATI Technologies Inc SB450 HDA Audio [1002:437b]
3103 and the vendor/subvendor are found only at the codec.
3105 * @codec: the HDA codec
3106 * @num_configs: number of config enums
3107 * @models: array of model name strings
3108 * @tbl: configuration table, terminated by null entries
3110 * Compares the modelname or PCI subsystem id of the current codec with the
3111 * given configuration table. If a matching entry is found, returns its
3112 * config value (supposed to be 0 or positive).
3114 * If no entries are matching, the function returns a negative value.
3116 int snd_hda_check_board_codec_sid_config(struct hda_codec *codec,
3117 int num_configs, const char **models,
3118 const struct snd_pci_quirk *tbl)
3120 const struct snd_pci_quirk *q;
3122 /* Search for codec ID */
3123 for (q = tbl; q->subvendor; q++) {
3124 unsigned long vendorid = (q->subdevice) | (q->subvendor << 16);
3126 if (vendorid == codec->subsystem_id)
3135 if (tbl->value >= 0 && tbl->value < num_configs) {
3136 #ifdef CONFIG_SND_DEBUG_VERBOSE
3138 const char *model = NULL;
3140 model = models[tbl->value];
3142 sprintf(tmp, "#%d", tbl->value);
3145 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
3146 "for config %x:%x (%s)\n",
3147 model, tbl->subvendor, tbl->subdevice,
3148 (tbl->name ? tbl->name : "Unknown device"));
3154 EXPORT_SYMBOL_HDA(snd_hda_check_board_codec_sid_config);
3157 * snd_hda_add_new_ctls - create controls from the array
3158 * @codec: the HDA codec
3159 * @knew: the array of struct snd_kcontrol_new
3161 * This helper function creates and add new controls in the given array.
3162 * The array must be terminated with an empty entry as terminator.
3164 * Returns 0 if successful, or a negative error code.
3166 int snd_hda_add_new_ctls(struct hda_codec *codec, struct snd_kcontrol_new *knew)
3170 for (; knew->name; knew++) {
3171 struct snd_kcontrol *kctl;
3172 kctl = snd_ctl_new1(knew, codec);
3175 err = snd_hda_ctl_add(codec, 0, kctl);
3179 kctl = snd_ctl_new1(knew, codec);
3182 kctl->id.device = codec->addr;
3183 err = snd_hda_ctl_add(codec, 0, kctl);
3190 EXPORT_SYMBOL_HDA(snd_hda_add_new_ctls);
3192 #ifdef CONFIG_SND_HDA_POWER_SAVE
3193 static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
3194 unsigned int power_state);
3196 static void hda_power_work(struct work_struct *work)
3198 struct hda_codec *codec =
3199 container_of(work, struct hda_codec, power_work.work);
3200 struct hda_bus *bus = codec->bus;
3202 if (!codec->power_on || codec->power_count) {
3203 codec->power_transition = 0;
3207 hda_call_codec_suspend(codec);
3208 if (bus->ops.pm_notify)
3209 bus->ops.pm_notify(bus);
3212 static void hda_keep_power_on(struct hda_codec *codec)
3214 codec->power_count++;
3215 codec->power_on = 1;
3216 codec->power_jiffies = jiffies;
3219 void snd_hda_update_power_acct(struct hda_codec *codec)
3221 unsigned long delta = jiffies - codec->power_jiffies;
3222 if (codec->power_on)
3223 codec->power_on_acct += delta;
3225 codec->power_off_acct += delta;
3226 codec->power_jiffies += delta;
3229 void snd_hda_power_up(struct hda_codec *codec)
3231 struct hda_bus *bus = codec->bus;
3233 codec->power_count++;
3234 if (codec->power_on || codec->power_transition)
3237 snd_hda_update_power_acct(codec);
3238 codec->power_on = 1;
3239 codec->power_jiffies = jiffies;
3240 if (bus->ops.pm_notify)
3241 bus->ops.pm_notify(bus);
3242 hda_call_codec_resume(codec);
3243 cancel_delayed_work(&codec->power_work);
3244 codec->power_transition = 0;
3246 EXPORT_SYMBOL_HDA(snd_hda_power_up);
3248 #define power_save(codec) \
3249 ((codec)->bus->power_save ? *(codec)->bus->power_save : 0)
3251 #define power_save(codec) \
3252 ((codec)->bus->power_save ? *(codec)->bus->power_save : 0)
3254 void snd_hda_power_down(struct hda_codec *codec)
3256 --codec->power_count;
3257 if (!codec->power_on || codec->power_count || codec->power_transition)
3259 if (power_save(codec)) {
3260 codec->power_transition = 1; /* avoid reentrance */
3261 queue_delayed_work(codec->bus->workq, &codec->power_work,
3262 msecs_to_jiffies(power_save(codec) * 1000));
3265 EXPORT_SYMBOL_HDA(snd_hda_power_down);
3267 int snd_hda_check_amp_list_power(struct hda_codec *codec,
3268 struct hda_loopback_check *check,
3271 struct hda_amp_list *p;
3274 if (!check->amplist)
3276 for (p = check->amplist; p->nid; p++) {
3281 return 0; /* nothing changed */
3283 for (p = check->amplist; p->nid; p++) {
3284 for (ch = 0; ch < 2; ch++) {
3285 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
3287 if (!(v & HDA_AMP_MUTE) && v > 0) {
3288 if (!check->power_on) {
3289 check->power_on = 1;
3290 snd_hda_power_up(codec);
3296 if (check->power_on) {
3297 check->power_on = 0;
3298 snd_hda_power_down(codec);
3302 EXPORT_SYMBOL_HDA(snd_hda_check_amp_list_power);
3306 * Channel mode helper
3308 int snd_hda_ch_mode_info(struct hda_codec *codec,
3309 struct snd_ctl_elem_info *uinfo,
3310 const struct hda_channel_mode *chmode,
3313 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
3315 uinfo->value.enumerated.items = num_chmodes;
3316 if (uinfo->value.enumerated.item >= num_chmodes)
3317 uinfo->value.enumerated.item = num_chmodes - 1;
3318 sprintf(uinfo->value.enumerated.name, "%dch",
3319 chmode[uinfo->value.enumerated.item].channels);
3322 EXPORT_SYMBOL_HDA(snd_hda_ch_mode_info);
3324 int snd_hda_ch_mode_get(struct hda_codec *codec,
3325 struct snd_ctl_elem_value *ucontrol,
3326 const struct hda_channel_mode *chmode,
3332 for (i = 0; i < num_chmodes; i++) {
3333 if (max_channels == chmode[i].channels) {
3334 ucontrol->value.enumerated.item[0] = i;
3340 EXPORT_SYMBOL_HDA(snd_hda_ch_mode_get);
3342 int snd_hda_ch_mode_put(struct hda_codec *codec,
3343 struct snd_ctl_elem_value *ucontrol,
3344 const struct hda_channel_mode *chmode,
3350 mode = ucontrol->value.enumerated.item[0];
3351 if (mode >= num_chmodes)
3353 if (*max_channelsp == chmode[mode].channels)
3355 /* change the current channel setting */
3356 *max_channelsp = chmode[mode].channels;
3357 if (chmode[mode].sequence)
3358 snd_hda_sequence_write_cache(codec, chmode[mode].sequence);
3361 EXPORT_SYMBOL_HDA(snd_hda_ch_mode_put);
3366 int snd_hda_input_mux_info(const struct hda_input_mux *imux,
3367 struct snd_ctl_elem_info *uinfo)
3371 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
3373 uinfo->value.enumerated.items = imux->num_items;
3374 if (!imux->num_items)
3376 index = uinfo->value.enumerated.item;
3377 if (index >= imux->num_items)
3378 index = imux->num_items - 1;
3379 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
3382 EXPORT_SYMBOL_HDA(snd_hda_input_mux_info);
3384 int snd_hda_input_mux_put(struct hda_codec *codec,
3385 const struct hda_input_mux *imux,
3386 struct snd_ctl_elem_value *ucontrol,
3388 unsigned int *cur_val)
3392 if (!imux->num_items)
3394 idx = ucontrol->value.enumerated.item[0];
3395 if (idx >= imux->num_items)
3396 idx = imux->num_items - 1;
3397 if (*cur_val == idx)
3399 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
3400 imux->items[idx].index);
3404 EXPORT_SYMBOL_HDA(snd_hda_input_mux_put);
3408 * Multi-channel / digital-out PCM helper functions
3411 /* setup SPDIF output stream */
3412 static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
3413 unsigned int stream_tag, unsigned int format)
3415 /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */
3416 if (codec->spdif_status_reset && (codec->spdif_ctls & AC_DIG1_ENABLE))
3417 set_dig_out_convert(codec, nid,
3418 codec->spdif_ctls & ~AC_DIG1_ENABLE & 0xff,
3420 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
3421 if (codec->slave_dig_outs) {
3423 for (d = codec->slave_dig_outs; *d; d++)
3424 snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
3427 /* turn on again (if needed) */
3428 if (codec->spdif_status_reset && (codec->spdif_ctls & AC_DIG1_ENABLE))
3429 set_dig_out_convert(codec, nid,
3430 codec->spdif_ctls & 0xff, -1);
3433 static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
3435 snd_hda_codec_cleanup_stream(codec, nid);
3436 if (codec->slave_dig_outs) {
3438 for (d = codec->slave_dig_outs; *d; d++)
3439 snd_hda_codec_cleanup_stream(codec, *d);
3443 /* call each reboot notifier */
3444 void snd_hda_bus_reboot_notify(struct hda_bus *bus)
3446 struct hda_codec *codec;
3450 list_for_each_entry(codec, &bus->codec_list, list) {
3451 #ifdef CONFIG_SND_HDA_POWER_SAVE
3452 if (!codec->power_on)
3455 if (codec->patch_ops.reboot_notify)
3456 codec->patch_ops.reboot_notify(codec);
3459 EXPORT_SYMBOL_HDA(snd_hda_bus_reboot_notify);
3462 * open the digital out in the exclusive mode
3464 int snd_hda_multi_out_dig_open(struct hda_codec *codec,
3465 struct hda_multi_out *mout)
3467 mutex_lock(&codec->spdif_mutex);
3468 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
3469 /* already opened as analog dup; reset it once */
3470 cleanup_dig_out_stream(codec, mout->dig_out_nid);
3471 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
3472 mutex_unlock(&codec->spdif_mutex);
3475 EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_open);
3477 int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
3478 struct hda_multi_out *mout,
3479 unsigned int stream_tag,
3480 unsigned int format,
3481 struct snd_pcm_substream *substream)
3483 mutex_lock(&codec->spdif_mutex);
3484 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
3485 mutex_unlock(&codec->spdif_mutex);
3488 EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_prepare);
3490 int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec,
3491 struct hda_multi_out *mout)
3493 mutex_lock(&codec->spdif_mutex);
3494 cleanup_dig_out_stream(codec, mout->dig_out_nid);
3495 mutex_unlock(&codec->spdif_mutex);
3498 EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_cleanup);
3501 * release the digital out
3503 int snd_hda_multi_out_dig_close(struct hda_codec *codec,
3504 struct hda_multi_out *mout)
3506 mutex_lock(&codec->spdif_mutex);
3507 mout->dig_out_used = 0;
3508 mutex_unlock(&codec->spdif_mutex);
3511 EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_close);
3514 * set up more restrictions for analog out
3516 int snd_hda_multi_out_analog_open(struct hda_codec *codec,
3517 struct hda_multi_out *mout,
3518 struct snd_pcm_substream *substream,
3519 struct hda_pcm_stream *hinfo)
3521 struct snd_pcm_runtime *runtime = substream->runtime;
3522 runtime->hw.channels_max = mout->max_channels;
3523 if (mout->dig_out_nid) {
3524 if (!mout->analog_rates) {
3525 mout->analog_rates = hinfo->rates;
3526 mout->analog_formats = hinfo->formats;
3527 mout->analog_maxbps = hinfo->maxbps;
3529 runtime->hw.rates = mout->analog_rates;
3530 runtime->hw.formats = mout->analog_formats;
3531 hinfo->maxbps = mout->analog_maxbps;
3533 if (!mout->spdif_rates) {
3534 snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
3536 &mout->spdif_formats,
3537 &mout->spdif_maxbps);
3539 mutex_lock(&codec->spdif_mutex);
3540 if (mout->share_spdif) {
3541 if ((runtime->hw.rates & mout->spdif_rates) &&
3542 (runtime->hw.formats & mout->spdif_formats)) {
3543 runtime->hw.rates &= mout->spdif_rates;
3544 runtime->hw.formats &= mout->spdif_formats;
3545 if (mout->spdif_maxbps < hinfo->maxbps)
3546 hinfo->maxbps = mout->spdif_maxbps;
3548 mout->share_spdif = 0;
3549 /* FIXME: need notify? */
3552 mutex_unlock(&codec->spdif_mutex);
3554 return snd_pcm_hw_constraint_step(substream->runtime, 0,
3555 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
3557 EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_open);
3560 * set up the i/o for analog out
3561 * when the digital out is available, copy the front out to digital out, too.
3563 int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
3564 struct hda_multi_out *mout,
3565 unsigned int stream_tag,
3566 unsigned int format,
3567 struct snd_pcm_substream *substream)
3569 hda_nid_t *nids = mout->dac_nids;
3570 int chs = substream->runtime->channels;
3573 mutex_lock(&codec->spdif_mutex);
3574 if (mout->dig_out_nid && mout->share_spdif &&
3575 mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
3577 snd_hda_is_supported_format(codec, mout->dig_out_nid,
3579 !(codec->spdif_status & IEC958_AES0_NONAUDIO)) {
3580 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
3581 setup_dig_out_stream(codec, mout->dig_out_nid,
3582 stream_tag, format);
3584 mout->dig_out_used = 0;
3585 cleanup_dig_out_stream(codec, mout->dig_out_nid);
3588 mutex_unlock(&codec->spdif_mutex);
3591 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
3593 if (!mout->no_share_stream &&
3594 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
3595 /* headphone out will just decode front left/right (stereo) */
3596 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
3598 /* extra outputs copied from front */
3599 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
3600 if (!mout->no_share_stream && mout->extra_out_nid[i])
3601 snd_hda_codec_setup_stream(codec,
3602 mout->extra_out_nid[i],
3603 stream_tag, 0, format);
3606 for (i = 1; i < mout->num_dacs; i++) {
3607 if (chs >= (i + 1) * 2) /* independent out */
3608 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
3610 else if (!mout->no_share_stream) /* copy front */
3611 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
3616 EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_prepare);
3619 * clean up the setting for analog out
3621 int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
3622 struct hda_multi_out *mout)
3624 hda_nid_t *nids = mout->dac_nids;
3627 for (i = 0; i < mout->num_dacs; i++)
3628 snd_hda_codec_cleanup_stream(codec, nids[i]);
3630 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
3631 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
3632 if (mout->extra_out_nid[i])
3633 snd_hda_codec_cleanup_stream(codec,
3634 mout->extra_out_nid[i]);
3635 mutex_lock(&codec->spdif_mutex);
3636 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
3637 cleanup_dig_out_stream(codec, mout->dig_out_nid);
3638 mout->dig_out_used = 0;
3640 mutex_unlock(&codec->spdif_mutex);
3643 EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_cleanup);
3646 * Helper for automatic pin configuration
3649 static int is_in_nid_list(hda_nid_t nid, hda_nid_t *list)
3651 for (; *list; list++)
3659 * Sort an associated group of pins according to their sequence numbers.
3661 static void sort_pins_by_sequence(hda_nid_t * pins, short * sequences,
3668 for (i = 0; i < num_pins; i++) {
3669 for (j = i + 1; j < num_pins; j++) {
3670 if (sequences[i] > sequences[j]) {
3672 sequences[i] = sequences[j];
3684 * Parse all pin widgets and store the useful pin nids to cfg
3686 * The number of line-outs or any primary output is stored in line_outs,
3687 * and the corresponding output pins are assigned to line_out_pins[],
3688 * in the order of front, rear, CLFE, side, ...
3690 * If more extra outputs (speaker and headphone) are found, the pins are
3691 * assisnged to hp_pins[] and speaker_pins[], respectively. If no line-out jack
3692 * is detected, one of speaker of HP pins is assigned as the primary
3693 * output, i.e. to line_out_pins[0]. So, line_outs is always positive
3694 * if any analog output exists.
3696 * The analog input pins are assigned to input_pins array.
3697 * The digital input/output pins are assigned to dig_in_pin and dig_out_pin,
3700 int snd_hda_parse_pin_def_config(struct hda_codec *codec,
3701 struct auto_pin_cfg *cfg,
3702 hda_nid_t *ignore_nids)
3704 hda_nid_t nid, end_nid;
3705 short seq, assoc_line_out, assoc_speaker;
3706 short sequences_line_out[ARRAY_SIZE(cfg->line_out_pins)];
3707 short sequences_speaker[ARRAY_SIZE(cfg->speaker_pins)];
3708 short sequences_hp[ARRAY_SIZE(cfg->hp_pins)];
3710 memset(cfg, 0, sizeof(*cfg));
3712 memset(sequences_line_out, 0, sizeof(sequences_line_out));
3713 memset(sequences_speaker, 0, sizeof(sequences_speaker));
3714 memset(sequences_hp, 0, sizeof(sequences_hp));
3715 assoc_line_out = assoc_speaker = 0;
3717 end_nid = codec->start_nid + codec->num_nodes;
3718 for (nid = codec->start_nid; nid < end_nid; nid++) {
3719 unsigned int wid_caps = get_wcaps(codec, nid);
3720 unsigned int wid_type = get_wcaps_type(wid_caps);
3721 unsigned int def_conf;
3724 /* read all default configuration for pin complex */
3725 if (wid_type != AC_WID_PIN)
3727 /* ignore the given nids (e.g. pc-beep returns error) */
3728 if (ignore_nids && is_in_nid_list(nid, ignore_nids))
3731 def_conf = snd_hda_codec_get_pincfg(codec, nid);
3732 if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE)
3734 loc = get_defcfg_location(def_conf);
3735 switch (get_defcfg_device(def_conf)) {
3736 case AC_JACK_LINE_OUT:
3737 seq = get_defcfg_sequence(def_conf);
3738 assoc = get_defcfg_association(def_conf);
3740 if (!(wid_caps & AC_WCAP_STEREO))
3741 if (!cfg->mono_out_pin)
3742 cfg->mono_out_pin = nid;
3745 if (!assoc_line_out)
3746 assoc_line_out = assoc;
3747 else if (assoc_line_out != assoc)
3749 if (cfg->line_outs >= ARRAY_SIZE(cfg->line_out_pins))
3751 cfg->line_out_pins[cfg->line_outs] = nid;
3752 sequences_line_out[cfg->line_outs] = seq;
3755 case AC_JACK_SPEAKER:
3756 seq = get_defcfg_sequence(def_conf);
3757 assoc = get_defcfg_association(def_conf);
3760 if (! assoc_speaker)
3761 assoc_speaker = assoc;
3762 else if (assoc_speaker != assoc)
3764 if (cfg->speaker_outs >= ARRAY_SIZE(cfg->speaker_pins))
3766 cfg->speaker_pins[cfg->speaker_outs] = nid;
3767 sequences_speaker[cfg->speaker_outs] = seq;
3768 cfg->speaker_outs++;
3770 case AC_JACK_HP_OUT:
3771 seq = get_defcfg_sequence(def_conf);
3772 assoc = get_defcfg_association(def_conf);
3773 if (cfg->hp_outs >= ARRAY_SIZE(cfg->hp_pins))
3775 cfg->hp_pins[cfg->hp_outs] = nid;
3776 sequences_hp[cfg->hp_outs] = (assoc << 4) | seq;
3779 case AC_JACK_MIC_IN: {
3781 if (loc == AC_JACK_LOC_FRONT) {
3782 preferred = AUTO_PIN_FRONT_MIC;
3785 preferred = AUTO_PIN_MIC;
3786 alt = AUTO_PIN_FRONT_MIC;
3788 if (!cfg->input_pins[preferred])
3789 cfg->input_pins[preferred] = nid;
3790 else if (!cfg->input_pins[alt])
3791 cfg->input_pins[alt] = nid;
3794 case AC_JACK_LINE_IN:
3795 if (loc == AC_JACK_LOC_FRONT)
3796 cfg->input_pins[AUTO_PIN_FRONT_LINE] = nid;
3798 cfg->input_pins[AUTO_PIN_LINE] = nid;
3801 cfg->input_pins[AUTO_PIN_CD] = nid;
3804 cfg->input_pins[AUTO_PIN_AUX] = nid;
3806 case AC_JACK_SPDIF_OUT:
3807 case AC_JACK_DIG_OTHER_OUT:
3808 if (cfg->dig_outs >= ARRAY_SIZE(cfg->dig_out_pins))
3810 cfg->dig_out_pins[cfg->dig_outs] = nid;
3811 cfg->dig_out_type[cfg->dig_outs] =
3812 (loc == AC_JACK_LOC_HDMI) ?
3813 HDA_PCM_TYPE_HDMI : HDA_PCM_TYPE_SPDIF;
3816 case AC_JACK_SPDIF_IN:
3817 case AC_JACK_DIG_OTHER_IN:
3818 cfg->dig_in_pin = nid;
3819 if (loc == AC_JACK_LOC_HDMI)
3820 cfg->dig_in_type = HDA_PCM_TYPE_HDMI;
3822 cfg->dig_in_type = HDA_PCM_TYPE_SPDIF;
3828 * If no line-out is defined but multiple HPs are found,
3829 * some of them might be the real line-outs.
3831 if (!cfg->line_outs && cfg->hp_outs > 1) {
3833 while (i < cfg->hp_outs) {
3834 /* The real HPs should have the sequence 0x0f */
3835 if ((sequences_hp[i] & 0x0f) == 0x0f) {
3839 /* Move it to the line-out table */
3840 cfg->line_out_pins[cfg->line_outs] = cfg->hp_pins[i];
3841 sequences_line_out[cfg->line_outs] = sequences_hp[i];
3844 memmove(cfg->hp_pins + i, cfg->hp_pins + i + 1,
3845 sizeof(cfg->hp_pins[0]) * (cfg->hp_outs - i));
3846 memmove(sequences_hp + i - 1, sequences_hp + i,
3847 sizeof(sequences_hp[0]) * (cfg->hp_outs - i));
3851 /* sort by sequence */
3852 sort_pins_by_sequence(cfg->line_out_pins, sequences_line_out,
3854 sort_pins_by_sequence(cfg->speaker_pins, sequences_speaker,
3856 sort_pins_by_sequence(cfg->hp_pins, sequences_hp,
3859 /* if we have only one mic, make it AUTO_PIN_MIC */
3860 if (!cfg->input_pins[AUTO_PIN_MIC] &&
3861 cfg->input_pins[AUTO_PIN_FRONT_MIC]) {
3862 cfg->input_pins[AUTO_PIN_MIC] =
3863 cfg->input_pins[AUTO_PIN_FRONT_MIC];
3864 cfg->input_pins[AUTO_PIN_FRONT_MIC] = 0;
3866 /* ditto for line-in */
3867 if (!cfg->input_pins[AUTO_PIN_LINE] &&
3868 cfg->input_pins[AUTO_PIN_FRONT_LINE]) {
3869 cfg->input_pins[AUTO_PIN_LINE] =
3870 cfg->input_pins[AUTO_PIN_FRONT_LINE];
3871 cfg->input_pins[AUTO_PIN_FRONT_LINE] = 0;
3875 * FIX-UP: if no line-outs are detected, try to use speaker or HP pin
3876 * as a primary output
3878 if (!cfg->line_outs) {
3879 if (cfg->speaker_outs) {
3880 cfg->line_outs = cfg->speaker_outs;
3881 memcpy(cfg->line_out_pins, cfg->speaker_pins,
3882 sizeof(cfg->speaker_pins));
3883 cfg->speaker_outs = 0;
3884 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
3885 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
3886 } else if (cfg->hp_outs) {
3887 cfg->line_outs = cfg->hp_outs;
3888 memcpy(cfg->line_out_pins, cfg->hp_pins,
3889 sizeof(cfg->hp_pins));
3891 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
3892 cfg->line_out_type = AUTO_PIN_HP_OUT;
3896 /* Reorder the surround channels
3897 * ALSA sequence is front/surr/clfe/side
3899 * 4-ch: front/surr => OK as it is
3900 * 6-ch: front/clfe/surr
3901 * 8-ch: front/clfe/rear/side|fc
3903 switch (cfg->line_outs) {
3906 nid = cfg->line_out_pins[1];
3907 cfg->line_out_pins[1] = cfg->line_out_pins[2];
3908 cfg->line_out_pins[2] = nid;
3913 * debug prints of the parsed results
3915 snd_printd("autoconfig: line_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
3916 cfg->line_outs, cfg->line_out_pins[0], cfg->line_out_pins[1],
3917 cfg->line_out_pins[2], cfg->line_out_pins[3],
3918 cfg->line_out_pins[4]);
3919 snd_printd(" speaker_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
3920 cfg->speaker_outs, cfg->speaker_pins[0],
3921 cfg->speaker_pins[1], cfg->speaker_pins[2],
3922 cfg->speaker_pins[3], cfg->speaker_pins[4]);
3923 snd_printd(" hp_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
3924 cfg->hp_outs, cfg->hp_pins[0],
3925 cfg->hp_pins[1], cfg->hp_pins[2],
3926 cfg->hp_pins[3], cfg->hp_pins[4]);
3927 snd_printd(" mono: mono_out=0x%x\n", cfg->mono_out_pin);
3929 snd_printd(" dig-out=0x%x/0x%x\n",
3930 cfg->dig_out_pins[0], cfg->dig_out_pins[1]);
3931 snd_printd(" inputs: mic=0x%x, fmic=0x%x, line=0x%x, fline=0x%x,"
3932 " cd=0x%x, aux=0x%x\n",
3933 cfg->input_pins[AUTO_PIN_MIC],
3934 cfg->input_pins[AUTO_PIN_FRONT_MIC],
3935 cfg->input_pins[AUTO_PIN_LINE],
3936 cfg->input_pins[AUTO_PIN_FRONT_LINE],
3937 cfg->input_pins[AUTO_PIN_CD],
3938 cfg->input_pins[AUTO_PIN_AUX]);
3939 if (cfg->dig_in_pin)
3940 snd_printd(" dig-in=0x%x\n", cfg->dig_in_pin);
3944 EXPORT_SYMBOL_HDA(snd_hda_parse_pin_def_config);
3946 /* labels for input pins */
3947 const char *auto_pin_cfg_labels[AUTO_PIN_LAST] = {
3948 "Mic", "Front Mic", "Line", "Front Line", "CD", "Aux"
3950 EXPORT_SYMBOL_HDA(auto_pin_cfg_labels);
3959 * snd_hda_suspend - suspend the codecs
3962 * Returns 0 if successful.
3964 int snd_hda_suspend(struct hda_bus *bus)
3966 struct hda_codec *codec;
3968 list_for_each_entry(codec, &bus->codec_list, list) {
3969 #ifdef CONFIG_SND_HDA_POWER_SAVE
3970 if (!codec->power_on)
3973 hda_call_codec_suspend(codec);
3977 EXPORT_SYMBOL_HDA(snd_hda_suspend);
3980 * snd_hda_resume - resume the codecs
3983 * Returns 0 if successful.
3985 * This fucntion is defined only when POWER_SAVE isn't set.
3986 * In the power-save mode, the codec is resumed dynamically.
3988 int snd_hda_resume(struct hda_bus *bus)
3990 struct hda_codec *codec;
3992 list_for_each_entry(codec, &bus->codec_list, list) {
3993 if (snd_hda_codec_needs_resume(codec))
3994 hda_call_codec_resume(codec);
3998 EXPORT_SYMBOL_HDA(snd_hda_resume);
3999 #endif /* CONFIG_PM */
4005 /* get a new element from the given array
4006 * if it exceeds the pre-allocated array size, re-allocate the array
4008 void *snd_array_new(struct snd_array *array)
4010 if (array->used >= array->alloced) {
4011 int num = array->alloced + array->alloc_align;
4013 if (snd_BUG_ON(num >= 4096))
4015 nlist = kcalloc(num + 1, array->elem_size, GFP_KERNEL);
4019 memcpy(nlist, array->list,
4020 array->elem_size * array->alloced);
4023 array->list = nlist;
4024 array->alloced = num;
4026 return snd_array_elem(array, array->used++);
4028 EXPORT_SYMBOL_HDA(snd_array_new);
4030 /* free the given array elements */
4031 void snd_array_free(struct snd_array *array)
4038 EXPORT_SYMBOL_HDA(snd_array_free);
4041 * used by hda_proc.c and hda_eld.c
4043 void snd_print_pcm_rates(int pcm, char *buf, int buflen)
4045 static unsigned int rates[] = {
4046 8000, 11025, 16000, 22050, 32000, 44100, 48000, 88200,
4047 96000, 176400, 192000, 384000
4051 for (i = 0, j = 0; i < ARRAY_SIZE(rates); i++)
4053 j += snprintf(buf + j, buflen - j, " %d", rates[i]);
4055 buf[j] = '\0'; /* necessary when j == 0 */
4057 EXPORT_SYMBOL_HDA(snd_print_pcm_rates);
4059 void snd_print_pcm_bits(int pcm, char *buf, int buflen)
4061 static unsigned int bits[] = { 8, 16, 20, 24, 32 };
4064 for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
4065 if (pcm & (AC_SUPPCM_BITS_8 << i))
4066 j += snprintf(buf + j, buflen - j, " %d", bits[i]);
4068 buf[j] = '\0'; /* necessary when j == 0 */
4070 EXPORT_SYMBOL_HDA(snd_print_pcm_bits);
4072 MODULE_DESCRIPTION("HDA codec core");
4073 MODULE_LICENSE("GPL");