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
23 #include <linux/init.h>
24 #include <linux/delay.h>
25 #include <linux/slab.h>
26 #include <linux/pci.h>
27 #include <linux/mutex.h>
28 #include <linux/module.h>
29 #include <sound/core.h>
30 #include "hda_codec.h"
31 #include <sound/asoundef.h>
32 #include <sound/tlv.h>
33 #include <sound/initval.h>
34 #include <sound/jack.h>
35 #include "hda_local.h"
38 #include <sound/hda_hwdep.h>
40 #define CREATE_TRACE_POINTS
41 #include "hda_trace.h"
44 * vendor / preset table
47 struct hda_vendor_id {
52 /* codec vendor labels */
53 static struct hda_vendor_id hda_vendor_ids[] = {
55 { 0x1013, "Cirrus Logic" },
56 { 0x1057, "Motorola" },
57 { 0x1095, "Silicon Image" },
59 { 0x10ec, "Realtek" },
60 { 0x1102, "Creative" },
64 { 0x11d4, "Analog Devices" },
65 { 0x13f6, "C-Media" },
66 { 0x14f1, "Conexant" },
67 { 0x17e8, "Chrontel" },
69 { 0x1aec, "Wolfson Microelectronics" },
70 { 0x434d, "C-Media" },
72 { 0x8384, "SigmaTel" },
76 static DEFINE_MUTEX(preset_mutex);
77 static LIST_HEAD(hda_preset_tables);
79 int snd_hda_add_codec_preset(struct hda_codec_preset_list *preset)
81 mutex_lock(&preset_mutex);
82 list_add_tail(&preset->list, &hda_preset_tables);
83 mutex_unlock(&preset_mutex);
86 EXPORT_SYMBOL_HDA(snd_hda_add_codec_preset);
88 int snd_hda_delete_codec_preset(struct hda_codec_preset_list *preset)
90 mutex_lock(&preset_mutex);
91 list_del(&preset->list);
92 mutex_unlock(&preset_mutex);
95 EXPORT_SYMBOL_HDA(snd_hda_delete_codec_preset);
97 #ifdef CONFIG_SND_HDA_POWER_SAVE
98 static void hda_power_work(struct work_struct *work);
99 static void hda_keep_power_on(struct hda_codec *codec);
100 #define hda_codec_is_power_on(codec) ((codec)->power_on)
102 static inline void hda_keep_power_on(struct hda_codec *codec) {}
103 #define hda_codec_is_power_on(codec) 1
107 * snd_hda_get_jack_location - Give a location string of the jack
108 * @cfg: pin default config value
110 * Parse the pin default config value and returns the string of the
111 * jack location, e.g. "Rear", "Front", etc.
113 const char *snd_hda_get_jack_location(u32 cfg)
115 static char *bases[7] = {
116 "N/A", "Rear", "Front", "Left", "Right", "Top", "Bottom",
118 static unsigned char specials_idx[] = {
123 static char *specials[] = {
124 "Rear Panel", "Drive Bar",
125 "Riser", "HDMI", "ATAPI",
126 "Mobile-In", "Mobile-Out"
129 cfg = (cfg & AC_DEFCFG_LOCATION) >> AC_DEFCFG_LOCATION_SHIFT;
130 if ((cfg & 0x0f) < 7)
131 return bases[cfg & 0x0f];
132 for (i = 0; i < ARRAY_SIZE(specials_idx); i++) {
133 if (cfg == specials_idx[i])
138 EXPORT_SYMBOL_HDA(snd_hda_get_jack_location);
141 * snd_hda_get_jack_connectivity - Give a connectivity string of the jack
142 * @cfg: pin default config value
144 * Parse the pin default config value and returns the string of the
145 * jack connectivity, i.e. external or internal connection.
147 const char *snd_hda_get_jack_connectivity(u32 cfg)
149 static char *jack_locations[4] = { "Ext", "Int", "Sep", "Oth" };
151 return jack_locations[(cfg >> (AC_DEFCFG_LOCATION_SHIFT + 4)) & 3];
153 EXPORT_SYMBOL_HDA(snd_hda_get_jack_connectivity);
156 * snd_hda_get_jack_type - Give a type string of the jack
157 * @cfg: pin default config value
159 * Parse the pin default config value and returns the string of the
160 * jack type, i.e. the purpose of the jack, such as Line-Out or CD.
162 const char *snd_hda_get_jack_type(u32 cfg)
164 static char *jack_types[16] = {
165 "Line Out", "Speaker", "HP Out", "CD",
166 "SPDIF Out", "Digital Out", "Modem Line", "Modem Hand",
167 "Line In", "Aux", "Mic", "Telephony",
168 "SPDIF In", "Digitial In", "Reserved", "Other"
171 return jack_types[(cfg & AC_DEFCFG_DEVICE)
172 >> AC_DEFCFG_DEVICE_SHIFT];
174 EXPORT_SYMBOL_HDA(snd_hda_get_jack_type);
177 * Compose a 32bit command word to be sent to the HD-audio controller
179 static inline unsigned int
180 make_codec_cmd(struct hda_codec *codec, hda_nid_t nid, int direct,
181 unsigned int verb, unsigned int parm)
185 if ((codec->addr & ~0xf) || (direct & ~1) || (nid & ~0x7f) ||
186 (verb & ~0xfff) || (parm & ~0xffff)) {
187 printk(KERN_ERR "hda-codec: out of range cmd %x:%x:%x:%x:%x\n",
188 codec->addr, direct, nid, verb, parm);
192 val = (u32)codec->addr << 28;
193 val |= (u32)direct << 27;
194 val |= (u32)nid << 20;
201 * Send and receive a verb
203 static int codec_exec_verb(struct hda_codec *codec, unsigned int cmd,
206 struct hda_bus *bus = codec->bus;
215 snd_hda_power_up(codec);
216 mutex_lock(&bus->cmd_mutex);
217 trace_hda_send_cmd(codec, cmd);
218 err = bus->ops.command(bus, cmd);
220 *res = bus->ops.get_response(bus, codec->addr);
221 trace_hda_get_response(codec, *res);
223 mutex_unlock(&bus->cmd_mutex);
224 snd_hda_power_down(codec);
225 if (res && *res == -1 && bus->rirb_error) {
226 if (bus->response_reset) {
227 snd_printd("hda_codec: resetting BUS due to "
228 "fatal communication error\n");
229 trace_hda_bus_reset(bus);
230 bus->ops.bus_reset(bus);
234 /* clear reset-flag when the communication gets recovered */
236 bus->response_reset = 0;
241 * snd_hda_codec_read - send a command and get the response
242 * @codec: the HDA codec
243 * @nid: NID to send the command
244 * @direct: direct flag
245 * @verb: the verb to send
246 * @parm: the parameter for the verb
248 * Send a single command and read the corresponding response.
250 * Returns the obtained response value, or -1 for an error.
252 unsigned int snd_hda_codec_read(struct hda_codec *codec, hda_nid_t nid,
254 unsigned int verb, unsigned int parm)
256 unsigned cmd = make_codec_cmd(codec, nid, direct, verb, parm);
258 if (codec_exec_verb(codec, cmd, &res))
262 EXPORT_SYMBOL_HDA(snd_hda_codec_read);
265 * snd_hda_codec_write - send a single command without waiting for response
266 * @codec: the HDA codec
267 * @nid: NID to send the command
268 * @direct: direct flag
269 * @verb: the verb to send
270 * @parm: the parameter for the verb
272 * Send a single command without waiting for response.
274 * Returns 0 if successful, or a negative error code.
276 int snd_hda_codec_write(struct hda_codec *codec, hda_nid_t nid, int direct,
277 unsigned int verb, unsigned int parm)
279 unsigned int cmd = make_codec_cmd(codec, nid, direct, verb, parm);
281 return codec_exec_verb(codec, cmd,
282 codec->bus->sync_write ? &res : NULL);
284 EXPORT_SYMBOL_HDA(snd_hda_codec_write);
287 * snd_hda_sequence_write - sequence writes
288 * @codec: the HDA codec
289 * @seq: VERB array to send
291 * Send the commands sequentially from the given array.
292 * The array must be terminated with NID=0.
294 void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq)
296 for (; seq->nid; seq++)
297 snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param);
299 EXPORT_SYMBOL_HDA(snd_hda_sequence_write);
302 * snd_hda_get_sub_nodes - get the range of sub nodes
303 * @codec: the HDA codec
305 * @start_id: the pointer to store the start NID
307 * Parse the NID and store the start NID of its sub-nodes.
308 * Returns the number of sub-nodes.
310 int snd_hda_get_sub_nodes(struct hda_codec *codec, hda_nid_t nid,
315 parm = snd_hda_param_read(codec, nid, AC_PAR_NODE_COUNT);
318 *start_id = (parm >> 16) & 0x7fff;
319 return (int)(parm & 0x7fff);
321 EXPORT_SYMBOL_HDA(snd_hda_get_sub_nodes);
323 /* look up the cached results */
324 static hda_nid_t *lookup_conn_list(struct snd_array *array, hda_nid_t nid)
327 for (i = 0; i < array->used; ) {
328 hda_nid_t *p = snd_array_elem(array, i);
338 * snd_hda_get_conn_list - get connection list
339 * @codec: the HDA codec
341 * @listp: the pointer to store NID list
343 * Parses the connection list of the given widget and stores the list
346 * Returns the number of connections, or a negative error code.
348 int snd_hda_get_conn_list(struct hda_codec *codec, hda_nid_t nid,
349 const hda_nid_t **listp)
351 struct snd_array *array = &codec->conn_lists;
353 hda_nid_t list[HDA_MAX_CONNECTIONS];
358 /* if the connection-list is already cached, read it */
359 p = lookup_conn_list(array, nid);
365 if (snd_BUG_ON(added))
368 /* read the connection and add to the cache */
369 len = snd_hda_get_raw_connections(codec, nid, list, HDA_MAX_CONNECTIONS);
372 err = snd_hda_override_conn_list(codec, nid, len, list);
378 EXPORT_SYMBOL_HDA(snd_hda_get_conn_list);
381 * snd_hda_get_connections - copy connection list
382 * @codec: the HDA codec
384 * @conn_list: connection list array
385 * @max_conns: max. number of connections to store
387 * Parses the connection list of the given widget and stores the list
390 * Returns the number of connections, or a negative error code.
392 int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
393 hda_nid_t *conn_list, int max_conns)
395 const hda_nid_t *list;
396 int len = snd_hda_get_conn_list(codec, nid, &list);
400 if (len > max_conns) {
401 snd_printk(KERN_ERR "hda_codec: "
402 "Too many connections %d for NID 0x%x\n",
406 memcpy(conn_list, list, len * sizeof(hda_nid_t));
409 EXPORT_SYMBOL_HDA(snd_hda_get_connections);
412 * snd_hda_get_raw_connections - copy connection list without cache
413 * @codec: the HDA codec
415 * @conn_list: connection list array
416 * @max_conns: max. number of connections to store
418 * Like snd_hda_get_connections(), copy the connection list but without
419 * checking through the connection-list cache.
420 * Currently called only from hda_proc.c, so not exported.
422 int snd_hda_get_raw_connections(struct hda_codec *codec, hda_nid_t nid,
423 hda_nid_t *conn_list, int max_conns)
426 int i, conn_len, conns;
427 unsigned int shift, num_elems, mask;
431 if (snd_BUG_ON(!conn_list || max_conns <= 0))
434 wcaps = get_wcaps(codec, nid);
435 if (!(wcaps & AC_WCAP_CONN_LIST) &&
436 get_wcaps_type(wcaps) != AC_WID_VOL_KNB)
439 parm = snd_hda_param_read(codec, nid, AC_PAR_CONNLIST_LEN);
440 if (parm & AC_CLIST_LONG) {
449 conn_len = parm & AC_CLIST_LENGTH;
450 mask = (1 << (shift-1)) - 1;
453 return 0; /* no connection */
456 /* single connection */
457 parm = snd_hda_codec_read(codec, nid, 0,
458 AC_VERB_GET_CONNECT_LIST, 0);
459 if (parm == -1 && codec->bus->rirb_error)
461 conn_list[0] = parm & mask;
465 /* multi connection */
468 for (i = 0; i < conn_len; i++) {
472 if (i % num_elems == 0) {
473 parm = snd_hda_codec_read(codec, nid, 0,
474 AC_VERB_GET_CONNECT_LIST, i);
475 if (parm == -1 && codec->bus->rirb_error)
478 range_val = !!(parm & (1 << (shift-1))); /* ranges */
481 snd_printk(KERN_WARNING "hda_codec: "
482 "invalid CONNECT_LIST verb %x[%i]:%x\n",
488 /* ranges between the previous and this one */
489 if (!prev_nid || prev_nid >= val) {
490 snd_printk(KERN_WARNING "hda_codec: "
491 "invalid dep_range_val %x:%x\n",
495 for (n = prev_nid + 1; n <= val; n++) {
496 if (conns >= max_conns) {
497 snd_printk(KERN_ERR "hda_codec: "
498 "Too many connections %d for NID 0x%x\n",
502 conn_list[conns++] = n;
505 if (conns >= max_conns) {
506 snd_printk(KERN_ERR "hda_codec: "
507 "Too many connections %d for NID 0x%x\n",
511 conn_list[conns++] = val;
518 static bool add_conn_list(struct snd_array *array, hda_nid_t nid)
520 hda_nid_t *p = snd_array_new(array);
528 * snd_hda_override_conn_list - add/modify the connection-list to cache
529 * @codec: the HDA codec
531 * @len: number of connection list entries
532 * @list: the list of connection entries
534 * Add or modify the given connection-list to the cache. If the corresponding
535 * cache already exists, invalidate it and append a new one.
537 * Returns zero or a negative error code.
539 int snd_hda_override_conn_list(struct hda_codec *codec, hda_nid_t nid, int len,
540 const hda_nid_t *list)
542 struct snd_array *array = &codec->conn_lists;
546 p = lookup_conn_list(array, nid);
548 *p = -1; /* invalidate the old entry */
550 old_used = array->used;
551 if (!add_conn_list(array, nid) || !add_conn_list(array, len))
553 for (i = 0; i < len; i++)
554 if (!add_conn_list(array, list[i]))
559 array->used = old_used;
562 EXPORT_SYMBOL_HDA(snd_hda_override_conn_list);
565 * snd_hda_get_conn_index - get the connection index of the given NID
566 * @codec: the HDA codec
567 * @mux: NID containing the list
568 * @nid: NID to select
569 * @recursive: 1 when searching NID recursively, otherwise 0
571 * Parses the connection list of the widget @mux and checks whether the
572 * widget @nid is present. If it is, return the connection index.
573 * Otherwise it returns -1.
575 int snd_hda_get_conn_index(struct hda_codec *codec, hda_nid_t mux,
576 hda_nid_t nid, int recursive)
578 hda_nid_t conn[HDA_MAX_NUM_INPUTS];
581 nums = snd_hda_get_connections(codec, mux, conn, ARRAY_SIZE(conn));
582 for (i = 0; i < nums; i++)
588 snd_printd("hda_codec: too deep connection for 0x%x\n", nid);
592 for (i = 0; i < nums; i++) {
593 unsigned int type = get_wcaps_type(get_wcaps(codec, conn[i]));
594 if (type == AC_WID_PIN || type == AC_WID_AUD_OUT)
596 if (snd_hda_get_conn_index(codec, conn[i], nid, recursive) >= 0)
601 EXPORT_SYMBOL_HDA(snd_hda_get_conn_index);
604 * snd_hda_queue_unsol_event - add an unsolicited event to queue
606 * @res: unsolicited event (lower 32bit of RIRB entry)
607 * @res_ex: codec addr and flags (upper 32bit or RIRB entry)
609 * Adds the given event to the queue. The events are processed in
610 * the workqueue asynchronously. Call this function in the interrupt
611 * hanlder when RIRB receives an unsolicited event.
613 * Returns 0 if successful, or a negative error code.
615 int snd_hda_queue_unsol_event(struct hda_bus *bus, u32 res, u32 res_ex)
617 struct hda_bus_unsolicited *unsol;
620 trace_hda_unsol_event(bus, res, res_ex);
625 wp = (unsol->wp + 1) % HDA_UNSOL_QUEUE_SIZE;
629 unsol->queue[wp] = res;
630 unsol->queue[wp + 1] = res_ex;
632 queue_work(bus->workq, &unsol->work);
636 EXPORT_SYMBOL_HDA(snd_hda_queue_unsol_event);
639 * process queued unsolicited events
641 static void process_unsol_events(struct work_struct *work)
643 struct hda_bus_unsolicited *unsol =
644 container_of(work, struct hda_bus_unsolicited, work);
645 struct hda_bus *bus = unsol->bus;
646 struct hda_codec *codec;
647 unsigned int rp, caddr, res;
649 while (unsol->rp != unsol->wp) {
650 rp = (unsol->rp + 1) % HDA_UNSOL_QUEUE_SIZE;
653 res = unsol->queue[rp];
654 caddr = unsol->queue[rp + 1];
655 if (!(caddr & (1 << 4))) /* no unsolicited event? */
657 codec = bus->caddr_tbl[caddr & 0x0f];
658 if (codec && codec->patch_ops.unsol_event)
659 codec->patch_ops.unsol_event(codec, res);
664 * initialize unsolicited queue
666 static int init_unsol_queue(struct hda_bus *bus)
668 struct hda_bus_unsolicited *unsol;
670 if (bus->unsol) /* already initialized */
673 unsol = kzalloc(sizeof(*unsol), GFP_KERNEL);
675 snd_printk(KERN_ERR "hda_codec: "
676 "can't allocate unsolicited queue\n");
679 INIT_WORK(&unsol->work, process_unsol_events);
688 static void snd_hda_codec_free(struct hda_codec *codec);
690 static int snd_hda_bus_free(struct hda_bus *bus)
692 struct hda_codec *codec, *n;
697 flush_workqueue(bus->workq);
700 list_for_each_entry_safe(codec, n, &bus->codec_list, list) {
701 snd_hda_codec_free(codec);
703 if (bus->ops.private_free)
704 bus->ops.private_free(bus);
706 destroy_workqueue(bus->workq);
711 static int snd_hda_bus_dev_free(struct snd_device *device)
713 struct hda_bus *bus = device->device_data;
715 return snd_hda_bus_free(bus);
718 #ifdef CONFIG_SND_HDA_HWDEP
719 static int snd_hda_bus_dev_register(struct snd_device *device)
721 struct hda_bus *bus = device->device_data;
722 struct hda_codec *codec;
723 list_for_each_entry(codec, &bus->codec_list, list) {
724 snd_hda_hwdep_add_sysfs(codec);
725 snd_hda_hwdep_add_power_sysfs(codec);
730 #define snd_hda_bus_dev_register NULL
734 * snd_hda_bus_new - create a HDA bus
735 * @card: the card entry
736 * @temp: the template for hda_bus information
737 * @busp: the pointer to store the created bus instance
739 * Returns 0 if successful, or a negative error code.
741 int /*__devinit*/ snd_hda_bus_new(struct snd_card *card,
742 const struct hda_bus_template *temp,
743 struct hda_bus **busp)
747 static struct snd_device_ops dev_ops = {
748 .dev_register = snd_hda_bus_dev_register,
749 .dev_free = snd_hda_bus_dev_free,
752 if (snd_BUG_ON(!temp))
754 if (snd_BUG_ON(!temp->ops.command || !temp->ops.get_response))
760 bus = kzalloc(sizeof(*bus), GFP_KERNEL);
762 snd_printk(KERN_ERR "can't allocate struct hda_bus\n");
767 bus->private_data = temp->private_data;
768 bus->pci = temp->pci;
769 bus->modelname = temp->modelname;
770 bus->power_save = temp->power_save;
771 bus->ops = temp->ops;
773 mutex_init(&bus->cmd_mutex);
774 mutex_init(&bus->prepare_mutex);
775 INIT_LIST_HEAD(&bus->codec_list);
777 snprintf(bus->workq_name, sizeof(bus->workq_name),
778 "hd-audio%d", card->number);
779 bus->workq = create_singlethread_workqueue(bus->workq_name);
781 snd_printk(KERN_ERR "cannot create workqueue %s\n",
787 err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops);
789 snd_hda_bus_free(bus);
796 EXPORT_SYMBOL_HDA(snd_hda_bus_new);
798 #ifdef CONFIG_SND_HDA_GENERIC
799 #define is_generic_config(codec) \
800 (codec->modelname && !strcmp(codec->modelname, "generic"))
802 #define is_generic_config(codec) 0
806 #define HDA_MODREQ_MAX_COUNT 2 /* two request_modules()'s */
808 #define HDA_MODREQ_MAX_COUNT 0 /* all presets are statically linked */
812 * find a matching codec preset
814 static const struct hda_codec_preset *
815 find_codec_preset(struct hda_codec *codec)
817 struct hda_codec_preset_list *tbl;
818 const struct hda_codec_preset *preset;
819 int mod_requested = 0;
821 if (is_generic_config(codec))
822 return NULL; /* use the generic parser */
825 mutex_lock(&preset_mutex);
826 list_for_each_entry(tbl, &hda_preset_tables, list) {
827 if (!try_module_get(tbl->owner)) {
828 snd_printk(KERN_ERR "hda_codec: cannot module_get\n");
831 for (preset = tbl->preset; preset->id; preset++) {
832 u32 mask = preset->mask;
833 if (preset->afg && preset->afg != codec->afg)
835 if (preset->mfg && preset->mfg != codec->mfg)
839 if (preset->id == (codec->vendor_id & mask) &&
841 preset->rev == codec->revision_id)) {
842 mutex_unlock(&preset_mutex);
843 codec->owner = tbl->owner;
847 module_put(tbl->owner);
849 mutex_unlock(&preset_mutex);
851 if (mod_requested < HDA_MODREQ_MAX_COUNT) {
854 snprintf(name, sizeof(name), "snd-hda-codec-id:%08x",
857 snprintf(name, sizeof(name), "snd-hda-codec-id:%04x*",
858 (codec->vendor_id >> 16) & 0xffff);
859 request_module(name);
867 * get_codec_name - store the codec name
869 static int get_codec_name(struct hda_codec *codec)
871 const struct hda_vendor_id *c;
872 const char *vendor = NULL;
873 u16 vendor_id = codec->vendor_id >> 16;
876 if (codec->vendor_name)
879 for (c = hda_vendor_ids; c->id; c++) {
880 if (c->id == vendor_id) {
886 sprintf(tmp, "Generic %04x", vendor_id);
889 codec->vendor_name = kstrdup(vendor, GFP_KERNEL);
890 if (!codec->vendor_name)
894 if (codec->chip_name)
897 if (codec->preset && codec->preset->name)
898 codec->chip_name = kstrdup(codec->preset->name, GFP_KERNEL);
900 sprintf(tmp, "ID %x", codec->vendor_id & 0xffff);
901 codec->chip_name = kstrdup(tmp, GFP_KERNEL);
903 if (!codec->chip_name)
909 * look for an AFG and MFG nodes
911 static void /*__devinit*/ setup_fg_nodes(struct hda_codec *codec)
913 int i, total_nodes, function_id;
916 total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid);
917 for (i = 0; i < total_nodes; i++, nid++) {
918 function_id = snd_hda_param_read(codec, nid,
919 AC_PAR_FUNCTION_TYPE);
920 switch (function_id & 0xff) {
921 case AC_GRP_AUDIO_FUNCTION:
923 codec->afg_function_id = function_id & 0xff;
924 codec->afg_unsol = (function_id >> 8) & 1;
926 case AC_GRP_MODEM_FUNCTION:
928 codec->mfg_function_id = function_id & 0xff;
929 codec->mfg_unsol = (function_id >> 8) & 1;
938 * read widget caps for each widget and store in cache
940 static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
945 codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node,
947 codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL);
950 nid = codec->start_nid;
951 for (i = 0; i < codec->num_nodes; i++, nid++)
952 codec->wcaps[i] = snd_hda_param_read(codec, nid,
953 AC_PAR_AUDIO_WIDGET_CAP);
957 /* read all pin default configurations and save codec->init_pins */
958 static int read_pin_defaults(struct hda_codec *codec)
961 hda_nid_t nid = codec->start_nid;
963 for (i = 0; i < codec->num_nodes; i++, nid++) {
964 struct hda_pincfg *pin;
965 unsigned int wcaps = get_wcaps(codec, nid);
966 unsigned int wid_type = get_wcaps_type(wcaps);
967 if (wid_type != AC_WID_PIN)
969 pin = snd_array_new(&codec->init_pins);
973 pin->cfg = snd_hda_codec_read(codec, nid, 0,
974 AC_VERB_GET_CONFIG_DEFAULT, 0);
975 pin->ctrl = snd_hda_codec_read(codec, nid, 0,
976 AC_VERB_GET_PIN_WIDGET_CONTROL,
982 /* look up the given pin config list and return the item matching with NID */
983 static struct hda_pincfg *look_up_pincfg(struct hda_codec *codec,
984 struct snd_array *array,
988 for (i = 0; i < array->used; i++) {
989 struct hda_pincfg *pin = snd_array_elem(array, i);
996 /* write a config value for the given NID */
997 static void set_pincfg(struct hda_codec *codec, hda_nid_t nid,
1001 for (i = 0; i < 4; i++) {
1002 snd_hda_codec_write(codec, nid, 0,
1003 AC_VERB_SET_CONFIG_DEFAULT_BYTES_0 + i,
1009 /* set the current pin config value for the given NID.
1010 * the value is cached, and read via snd_hda_codec_get_pincfg()
1012 int snd_hda_add_pincfg(struct hda_codec *codec, struct snd_array *list,
1013 hda_nid_t nid, unsigned int cfg)
1015 struct hda_pincfg *pin;
1016 unsigned int oldcfg;
1018 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN)
1021 oldcfg = snd_hda_codec_get_pincfg(codec, nid);
1022 pin = look_up_pincfg(codec, list, nid);
1024 pin = snd_array_new(list);
1031 /* change only when needed; e.g. if the pincfg is already present
1032 * in user_pins[], don't write it
1034 cfg = snd_hda_codec_get_pincfg(codec, nid);
1036 set_pincfg(codec, nid, cfg);
1041 * snd_hda_codec_set_pincfg - Override a pin default configuration
1042 * @codec: the HDA codec
1043 * @nid: NID to set the pin config
1044 * @cfg: the pin default config value
1046 * Override a pin default configuration value in the cache.
1047 * This value can be read by snd_hda_codec_get_pincfg() in a higher
1048 * priority than the real hardware value.
1050 int snd_hda_codec_set_pincfg(struct hda_codec *codec,
1051 hda_nid_t nid, unsigned int cfg)
1053 return snd_hda_add_pincfg(codec, &codec->driver_pins, nid, cfg);
1055 EXPORT_SYMBOL_HDA(snd_hda_codec_set_pincfg);
1058 * snd_hda_codec_get_pincfg - Obtain a pin-default configuration
1059 * @codec: the HDA codec
1060 * @nid: NID to get the pin config
1062 * Get the current pin config value of the given pin NID.
1063 * If the pincfg value is cached or overridden via sysfs or driver,
1064 * returns the cached value.
1066 unsigned int snd_hda_codec_get_pincfg(struct hda_codec *codec, hda_nid_t nid)
1068 struct hda_pincfg *pin;
1070 #ifdef CONFIG_SND_HDA_HWDEP
1071 pin = look_up_pincfg(codec, &codec->user_pins, nid);
1075 pin = look_up_pincfg(codec, &codec->driver_pins, nid);
1078 pin = look_up_pincfg(codec, &codec->init_pins, nid);
1083 EXPORT_SYMBOL_HDA(snd_hda_codec_get_pincfg);
1085 /* restore all current pin configs */
1086 static void restore_pincfgs(struct hda_codec *codec)
1089 for (i = 0; i < codec->init_pins.used; i++) {
1090 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1091 set_pincfg(codec, pin->nid,
1092 snd_hda_codec_get_pincfg(codec, pin->nid));
1097 * snd_hda_shutup_pins - Shut up all pins
1098 * @codec: the HDA codec
1100 * Clear all pin controls to shup up before suspend for avoiding click noise.
1101 * The controls aren't cached so that they can be resumed properly.
1103 void snd_hda_shutup_pins(struct hda_codec *codec)
1106 /* don't shut up pins when unloading the driver; otherwise it breaks
1107 * the default pin setup at the next load of the driver
1109 if (codec->bus->shutdown)
1111 for (i = 0; i < codec->init_pins.used; i++) {
1112 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1113 /* use read here for syncing after issuing each verb */
1114 snd_hda_codec_read(codec, pin->nid, 0,
1115 AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
1117 codec->pins_shutup = 1;
1119 EXPORT_SYMBOL_HDA(snd_hda_shutup_pins);
1122 /* Restore the pin controls cleared previously via snd_hda_shutup_pins() */
1123 static void restore_shutup_pins(struct hda_codec *codec)
1126 if (!codec->pins_shutup)
1128 if (codec->bus->shutdown)
1130 for (i = 0; i < codec->init_pins.used; i++) {
1131 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1132 snd_hda_codec_write(codec, pin->nid, 0,
1133 AC_VERB_SET_PIN_WIDGET_CONTROL,
1136 codec->pins_shutup = 0;
1140 static void init_hda_cache(struct hda_cache_rec *cache,
1141 unsigned int record_size);
1142 static void free_hda_cache(struct hda_cache_rec *cache);
1144 /* restore the initial pin cfgs and release all pincfg lists */
1145 static void restore_init_pincfgs(struct hda_codec *codec)
1147 /* first free driver_pins and user_pins, then call restore_pincfg
1148 * so that only the values in init_pins are restored
1150 snd_array_free(&codec->driver_pins);
1151 #ifdef CONFIG_SND_HDA_HWDEP
1152 snd_array_free(&codec->user_pins);
1154 restore_pincfgs(codec);
1155 snd_array_free(&codec->init_pins);
1159 * audio-converter setup caches
1161 struct hda_cvt_setup {
1166 unsigned char active; /* cvt is currently used */
1167 unsigned char dirty; /* setups should be cleared */
1170 /* get or create a cache entry for the given audio converter NID */
1171 static struct hda_cvt_setup *
1172 get_hda_cvt_setup(struct hda_codec *codec, hda_nid_t nid)
1174 struct hda_cvt_setup *p;
1177 for (i = 0; i < codec->cvt_setups.used; i++) {
1178 p = snd_array_elem(&codec->cvt_setups, i);
1182 p = snd_array_new(&codec->cvt_setups);
1191 static void snd_hda_codec_free(struct hda_codec *codec)
1195 restore_init_pincfgs(codec);
1196 #ifdef CONFIG_SND_HDA_POWER_SAVE
1197 cancel_delayed_work(&codec->power_work);
1198 flush_workqueue(codec->bus->workq);
1200 list_del(&codec->list);
1201 snd_array_free(&codec->mixers);
1202 snd_array_free(&codec->nids);
1203 snd_array_free(&codec->conn_lists);
1204 snd_array_free(&codec->spdif_out);
1205 codec->bus->caddr_tbl[codec->addr] = NULL;
1206 if (codec->patch_ops.free)
1207 codec->patch_ops.free(codec);
1208 module_put(codec->owner);
1209 free_hda_cache(&codec->amp_cache);
1210 free_hda_cache(&codec->cmd_cache);
1211 kfree(codec->vendor_name);
1212 kfree(codec->chip_name);
1213 kfree(codec->modelname);
1214 kfree(codec->wcaps);
1218 static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
1219 unsigned int power_state);
1222 * snd_hda_codec_new - create a HDA codec
1223 * @bus: the bus to assign
1224 * @codec_addr: the codec address
1225 * @codecp: the pointer to store the generated codec
1227 * Returns 0 if successful, or a negative error code.
1229 int /*__devinit*/ snd_hda_codec_new(struct hda_bus *bus,
1230 unsigned int codec_addr,
1231 struct hda_codec **codecp)
1233 struct hda_codec *codec;
1237 if (snd_BUG_ON(!bus))
1239 if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS))
1242 if (bus->caddr_tbl[codec_addr]) {
1243 snd_printk(KERN_ERR "hda_codec: "
1244 "address 0x%x is already occupied\n", codec_addr);
1248 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
1249 if (codec == NULL) {
1250 snd_printk(KERN_ERR "can't allocate struct hda_codec\n");
1255 codec->addr = codec_addr;
1256 mutex_init(&codec->spdif_mutex);
1257 mutex_init(&codec->control_mutex);
1258 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
1259 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
1260 snd_array_init(&codec->mixers, sizeof(struct hda_nid_item), 32);
1261 snd_array_init(&codec->nids, sizeof(struct hda_nid_item), 32);
1262 snd_array_init(&codec->init_pins, sizeof(struct hda_pincfg), 16);
1263 snd_array_init(&codec->driver_pins, sizeof(struct hda_pincfg), 16);
1264 snd_array_init(&codec->cvt_setups, sizeof(struct hda_cvt_setup), 8);
1265 snd_array_init(&codec->conn_lists, sizeof(hda_nid_t), 64);
1266 snd_array_init(&codec->spdif_out, sizeof(struct hda_spdif_out), 16);
1267 if (codec->bus->modelname) {
1268 codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL);
1269 if (!codec->modelname) {
1270 snd_hda_codec_free(codec);
1275 #ifdef CONFIG_SND_HDA_POWER_SAVE
1276 INIT_DELAYED_WORK(&codec->power_work, hda_power_work);
1277 /* snd_hda_codec_new() marks the codec as power-up, and leave it as is.
1278 * the caller has to power down appropriatley after initialization
1281 hda_keep_power_on(codec);
1284 list_add_tail(&codec->list, &bus->codec_list);
1285 bus->caddr_tbl[codec_addr] = codec;
1287 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1289 if (codec->vendor_id == -1)
1290 /* read again, hopefully the access method was corrected
1291 * in the last read...
1293 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1295 codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1296 AC_PAR_SUBSYSTEM_ID);
1297 codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1300 setup_fg_nodes(codec);
1301 if (!codec->afg && !codec->mfg) {
1302 snd_printdd("hda_codec: no AFG or MFG node found\n");
1307 err = read_widget_caps(codec, codec->afg ? codec->afg : codec->mfg);
1309 snd_printk(KERN_ERR "hda_codec: cannot malloc\n");
1312 err = read_pin_defaults(codec);
1316 if (!codec->subsystem_id) {
1317 hda_nid_t nid = codec->afg ? codec->afg : codec->mfg;
1318 codec->subsystem_id =
1319 snd_hda_codec_read(codec, nid, 0,
1320 AC_VERB_GET_SUBSYSTEM_ID, 0);
1323 /* power-up all before initialization */
1324 hda_set_power_state(codec,
1325 codec->afg ? codec->afg : codec->mfg,
1328 snd_hda_codec_proc_new(codec);
1330 snd_hda_create_hwdep(codec);
1332 sprintf(component, "HDA:%08x,%08x,%08x", codec->vendor_id,
1333 codec->subsystem_id, codec->revision_id);
1334 snd_component_add(codec->bus->card, component);
1341 snd_hda_codec_free(codec);
1344 EXPORT_SYMBOL_HDA(snd_hda_codec_new);
1347 * snd_hda_codec_configure - (Re-)configure the HD-audio codec
1348 * @codec: the HDA codec
1350 * Start parsing of the given codec tree and (re-)initialize the whole
1353 * Returns 0 if successful or a negative error code.
1355 int snd_hda_codec_configure(struct hda_codec *codec)
1359 codec->preset = find_codec_preset(codec);
1360 if (!codec->vendor_name || !codec->chip_name) {
1361 err = get_codec_name(codec);
1366 if (is_generic_config(codec)) {
1367 err = snd_hda_parse_generic_codec(codec);
1370 if (codec->preset && codec->preset->patch) {
1371 err = codec->preset->patch(codec);
1375 /* call the default parser */
1376 err = snd_hda_parse_generic_codec(codec);
1378 printk(KERN_ERR "hda-codec: No codec parser is available\n");
1381 if (!err && codec->patch_ops.unsol_event)
1382 err = init_unsol_queue(codec->bus);
1383 /* audio codec should override the mixer name */
1384 if (!err && (codec->afg || !*codec->bus->card->mixername))
1385 snprintf(codec->bus->card->mixername,
1386 sizeof(codec->bus->card->mixername),
1387 "%s %s", codec->vendor_name, codec->chip_name);
1390 EXPORT_SYMBOL_HDA(snd_hda_codec_configure);
1393 * snd_hda_codec_setup_stream - set up the codec for streaming
1394 * @codec: the CODEC to set up
1395 * @nid: the NID to set up
1396 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
1397 * @channel_id: channel id to pass, zero based.
1398 * @format: stream format.
1400 void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
1402 int channel_id, int format)
1404 struct hda_codec *c;
1405 struct hda_cvt_setup *p;
1406 unsigned int oldval, newval;
1413 snd_printdd("hda_codec_setup_stream: "
1414 "NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
1415 nid, stream_tag, channel_id, format);
1416 p = get_hda_cvt_setup(codec, nid);
1419 /* update the stream-id if changed */
1420 if (p->stream_tag != stream_tag || p->channel_id != channel_id) {
1421 oldval = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0);
1422 newval = (stream_tag << 4) | channel_id;
1423 if (oldval != newval)
1424 snd_hda_codec_write(codec, nid, 0,
1425 AC_VERB_SET_CHANNEL_STREAMID,
1427 p->stream_tag = stream_tag;
1428 p->channel_id = channel_id;
1430 /* update the format-id if changed */
1431 if (p->format_id != format) {
1432 oldval = snd_hda_codec_read(codec, nid, 0,
1433 AC_VERB_GET_STREAM_FORMAT, 0);
1434 if (oldval != format) {
1436 snd_hda_codec_write(codec, nid, 0,
1437 AC_VERB_SET_STREAM_FORMAT,
1440 p->format_id = format;
1445 /* make other inactive cvts with the same stream-tag dirty */
1446 type = get_wcaps_type(get_wcaps(codec, nid));
1447 list_for_each_entry(c, &codec->bus->codec_list, list) {
1448 for (i = 0; i < c->cvt_setups.used; i++) {
1449 p = snd_array_elem(&c->cvt_setups, i);
1450 if (!p->active && p->stream_tag == stream_tag &&
1451 get_wcaps_type(get_wcaps(c, p->nid)) == type)
1456 EXPORT_SYMBOL_HDA(snd_hda_codec_setup_stream);
1458 static void really_cleanup_stream(struct hda_codec *codec,
1459 struct hda_cvt_setup *q);
1462 * __snd_hda_codec_cleanup_stream - clean up the codec for closing
1463 * @codec: the CODEC to clean up
1464 * @nid: the NID to clean up
1465 * @do_now: really clean up the stream instead of clearing the active flag
1467 void __snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid,
1470 struct hda_cvt_setup *p;
1475 if (codec->no_sticky_stream)
1478 snd_printdd("hda_codec_cleanup_stream: NID=0x%x\n", nid);
1479 p = get_hda_cvt_setup(codec, nid);
1481 /* here we just clear the active flag when do_now isn't set;
1482 * actual clean-ups will be done later in
1483 * purify_inactive_streams() called from snd_hda_codec_prpapre()
1486 really_cleanup_stream(codec, p);
1491 EXPORT_SYMBOL_HDA(__snd_hda_codec_cleanup_stream);
1493 static void really_cleanup_stream(struct hda_codec *codec,
1494 struct hda_cvt_setup *q)
1496 hda_nid_t nid = q->nid;
1497 if (q->stream_tag || q->channel_id)
1498 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
1500 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0
1502 memset(q, 0, sizeof(*q));
1506 /* clean up the all conflicting obsolete streams */
1507 static void purify_inactive_streams(struct hda_codec *codec)
1509 struct hda_codec *c;
1512 list_for_each_entry(c, &codec->bus->codec_list, list) {
1513 for (i = 0; i < c->cvt_setups.used; i++) {
1514 struct hda_cvt_setup *p;
1515 p = snd_array_elem(&c->cvt_setups, i);
1517 really_cleanup_stream(c, p);
1523 /* clean up all streams; called from suspend */
1524 static void hda_cleanup_all_streams(struct hda_codec *codec)
1528 for (i = 0; i < codec->cvt_setups.used; i++) {
1529 struct hda_cvt_setup *p = snd_array_elem(&codec->cvt_setups, i);
1531 really_cleanup_stream(codec, p);
1537 * amp access functions
1540 /* FIXME: more better hash key? */
1541 #define HDA_HASH_KEY(nid, dir, idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
1542 #define HDA_HASH_PINCAP_KEY(nid) (u32)((nid) + (0x02 << 24))
1543 #define HDA_HASH_PARPCM_KEY(nid) (u32)((nid) + (0x03 << 24))
1544 #define HDA_HASH_PARSTR_KEY(nid) (u32)((nid) + (0x04 << 24))
1545 #define INFO_AMP_CAPS (1<<0)
1546 #define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
1548 /* initialize the hash table */
1549 static void /*__devinit*/ init_hda_cache(struct hda_cache_rec *cache,
1550 unsigned int record_size)
1552 memset(cache, 0, sizeof(*cache));
1553 memset(cache->hash, 0xff, sizeof(cache->hash));
1554 snd_array_init(&cache->buf, record_size, 64);
1557 static void free_hda_cache(struct hda_cache_rec *cache)
1559 snd_array_free(&cache->buf);
1562 /* query the hash. allocate an entry if not found. */
1563 static struct hda_cache_head *get_hash(struct hda_cache_rec *cache, u32 key)
1565 u16 idx = key % (u16)ARRAY_SIZE(cache->hash);
1566 u16 cur = cache->hash[idx];
1567 struct hda_cache_head *info;
1569 while (cur != 0xffff) {
1570 info = snd_array_elem(&cache->buf, cur);
1571 if (info->key == key)
1578 /* query the hash. allocate an entry if not found. */
1579 static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache,
1582 struct hda_cache_head *info = get_hash(cache, key);
1585 /* add a new hash entry */
1586 info = snd_array_new(&cache->buf);
1589 cur = snd_array_index(&cache->buf, info);
1592 idx = key % (u16)ARRAY_SIZE(cache->hash);
1593 info->next = cache->hash[idx];
1594 cache->hash[idx] = cur;
1599 /* query and allocate an amp hash entry */
1600 static inline struct hda_amp_info *
1601 get_alloc_amp_hash(struct hda_codec *codec, u32 key)
1603 return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key);
1607 * query_amp_caps - query AMP capabilities
1608 * @codec: the HD-auio codec
1609 * @nid: the NID to query
1610 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1612 * Query AMP capabilities for the given widget and direction.
1613 * Returns the obtained capability bits.
1615 * When cap bits have been already read, this doesn't read again but
1616 * returns the cached value.
1618 u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
1620 struct hda_amp_info *info;
1622 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, 0));
1625 if (!(info->head.val & INFO_AMP_CAPS)) {
1626 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
1628 info->amp_caps = snd_hda_param_read(codec, nid,
1629 direction == HDA_OUTPUT ?
1630 AC_PAR_AMP_OUT_CAP :
1633 info->head.val |= INFO_AMP_CAPS;
1635 return info->amp_caps;
1637 EXPORT_SYMBOL_HDA(query_amp_caps);
1640 * snd_hda_override_amp_caps - Override the AMP capabilities
1641 * @codec: the CODEC to clean up
1642 * @nid: the NID to clean up
1643 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1644 * @caps: the capability bits to set
1646 * Override the cached AMP caps bits value by the given one.
1647 * This function is useful if the driver needs to adjust the AMP ranges,
1648 * e.g. limit to 0dB, etc.
1650 * Returns zero if successful or a negative error code.
1652 int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
1655 struct hda_amp_info *info;
1657 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, dir, 0));
1660 info->amp_caps = caps;
1661 info->head.val |= INFO_AMP_CAPS;
1664 EXPORT_SYMBOL_HDA(snd_hda_override_amp_caps);
1667 query_caps_hash(struct hda_codec *codec, hda_nid_t nid, u32 key,
1668 unsigned int (*func)(struct hda_codec *, hda_nid_t))
1670 struct hda_amp_info *info;
1672 info = get_alloc_amp_hash(codec, key);
1675 if (!info->head.val) {
1676 info->head.val |= INFO_AMP_CAPS;
1677 info->amp_caps = func(codec, nid);
1679 return info->amp_caps;
1682 static unsigned int read_pin_cap(struct hda_codec *codec, hda_nid_t nid)
1684 return snd_hda_param_read(codec, nid, AC_PAR_PIN_CAP);
1688 * snd_hda_query_pin_caps - Query PIN capabilities
1689 * @codec: the HD-auio codec
1690 * @nid: the NID to query
1692 * Query PIN capabilities for the given widget.
1693 * Returns the obtained capability bits.
1695 * When cap bits have been already read, this doesn't read again but
1696 * returns the cached value.
1698 u32 snd_hda_query_pin_caps(struct hda_codec *codec, hda_nid_t nid)
1700 return query_caps_hash(codec, nid, HDA_HASH_PINCAP_KEY(nid),
1703 EXPORT_SYMBOL_HDA(snd_hda_query_pin_caps);
1706 * snd_hda_override_pin_caps - Override the pin capabilities
1708 * @nid: the NID to override
1709 * @caps: the capability bits to set
1711 * Override the cached PIN capabilitiy bits value by the given one.
1713 * Returns zero if successful or a negative error code.
1715 int snd_hda_override_pin_caps(struct hda_codec *codec, hda_nid_t nid,
1718 struct hda_amp_info *info;
1719 info = get_alloc_amp_hash(codec, HDA_HASH_PINCAP_KEY(nid));
1722 info->amp_caps = caps;
1723 info->head.val |= INFO_AMP_CAPS;
1726 EXPORT_SYMBOL_HDA(snd_hda_override_pin_caps);
1729 * read the current volume to info
1730 * if the cache exists, read the cache value.
1732 static unsigned int get_vol_mute(struct hda_codec *codec,
1733 struct hda_amp_info *info, hda_nid_t nid,
1734 int ch, int direction, int index)
1738 if (info->head.val & INFO_AMP_VOL(ch))
1739 return info->vol[ch];
1741 parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
1742 parm |= direction == HDA_OUTPUT ? AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
1744 val = snd_hda_codec_read(codec, nid, 0,
1745 AC_VERB_GET_AMP_GAIN_MUTE, parm);
1746 info->vol[ch] = val & 0xff;
1747 info->head.val |= INFO_AMP_VOL(ch);
1748 return info->vol[ch];
1752 * write the current volume in info to the h/w and update the cache
1754 static void put_vol_mute(struct hda_codec *codec, struct hda_amp_info *info,
1755 hda_nid_t nid, int ch, int direction, int index,
1760 parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
1761 parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
1762 parm |= index << AC_AMP_SET_INDEX_SHIFT;
1763 if ((val & HDA_AMP_MUTE) && !(info->amp_caps & AC_AMPCAP_MUTE) &&
1764 (info->amp_caps & AC_AMPCAP_MIN_MUTE))
1765 ; /* set the zero value as a fake mute */
1768 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
1769 info->vol[ch] = val;
1773 * snd_hda_codec_amp_read - Read AMP value
1774 * @codec: HD-audio codec
1775 * @nid: NID to read the AMP value
1776 * @ch: channel (left=0 or right=1)
1777 * @direction: #HDA_INPUT or #HDA_OUTPUT
1778 * @index: the index value (only for input direction)
1780 * Read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
1782 int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
1783 int direction, int index)
1785 struct hda_amp_info *info;
1786 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
1789 return get_vol_mute(codec, info, nid, ch, direction, index);
1791 EXPORT_SYMBOL_HDA(snd_hda_codec_amp_read);
1794 * snd_hda_codec_amp_update - update the AMP value
1795 * @codec: HD-audio codec
1796 * @nid: NID to read the AMP value
1797 * @ch: channel (left=0 or right=1)
1798 * @direction: #HDA_INPUT or #HDA_OUTPUT
1799 * @idx: the index value (only for input direction)
1800 * @mask: bit mask to set
1801 * @val: the bits value to set
1803 * Update the AMP value with a bit mask.
1804 * Returns 0 if the value is unchanged, 1 if changed.
1806 int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
1807 int direction, int idx, int mask, int val)
1809 struct hda_amp_info *info;
1811 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, idx));
1814 if (snd_BUG_ON(mask & ~0xff))
1817 val |= get_vol_mute(codec, info, nid, ch, direction, idx) & ~mask;
1818 if (info->vol[ch] == val)
1820 put_vol_mute(codec, info, nid, ch, direction, idx, val);
1823 EXPORT_SYMBOL_HDA(snd_hda_codec_amp_update);
1826 * snd_hda_codec_amp_stereo - update the AMP stereo values
1827 * @codec: HD-audio codec
1828 * @nid: NID to read the AMP value
1829 * @direction: #HDA_INPUT or #HDA_OUTPUT
1830 * @idx: the index value (only for input direction)
1831 * @mask: bit mask to set
1832 * @val: the bits value to set
1834 * Update the AMP values like snd_hda_codec_amp_update(), but for a
1835 * stereo widget with the same mask and value.
1837 int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
1838 int direction, int idx, int mask, int val)
1842 if (snd_BUG_ON(mask & ~0xff))
1844 for (ch = 0; ch < 2; ch++)
1845 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
1849 EXPORT_SYMBOL_HDA(snd_hda_codec_amp_stereo);
1853 * snd_hda_codec_resume_amp - Resume all AMP commands from the cache
1854 * @codec: HD-audio codec
1856 * Resume the all amp commands from the cache.
1858 void snd_hda_codec_resume_amp(struct hda_codec *codec)
1860 struct hda_amp_info *buffer = codec->amp_cache.buf.list;
1863 for (i = 0; i < codec->amp_cache.buf.used; i++, buffer++) {
1864 u32 key = buffer->head.key;
1866 unsigned int idx, dir, ch;
1870 idx = (key >> 16) & 0xff;
1871 dir = (key >> 24) & 0xff;
1872 for (ch = 0; ch < 2; ch++) {
1873 if (!(buffer->head.val & INFO_AMP_VOL(ch)))
1875 put_vol_mute(codec, buffer, nid, ch, dir, idx,
1880 EXPORT_SYMBOL_HDA(snd_hda_codec_resume_amp);
1881 #endif /* CONFIG_PM */
1883 static u32 get_amp_max_value(struct hda_codec *codec, hda_nid_t nid, int dir,
1886 u32 caps = query_amp_caps(codec, nid, dir);
1888 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1895 * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer
1897 * The control element is supposed to have the private_value field
1898 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1900 int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
1901 struct snd_ctl_elem_info *uinfo)
1903 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1904 u16 nid = get_amp_nid(kcontrol);
1905 u8 chs = get_amp_channels(kcontrol);
1906 int dir = get_amp_direction(kcontrol);
1907 unsigned int ofs = get_amp_offset(kcontrol);
1909 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1910 uinfo->count = chs == 3 ? 2 : 1;
1911 uinfo->value.integer.min = 0;
1912 uinfo->value.integer.max = get_amp_max_value(codec, nid, dir, ofs);
1913 if (!uinfo->value.integer.max) {
1914 printk(KERN_WARNING "hda_codec: "
1915 "num_steps = 0 for NID=0x%x (ctl = %s)\n", nid,
1921 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_info);
1924 static inline unsigned int
1925 read_amp_value(struct hda_codec *codec, hda_nid_t nid,
1926 int ch, int dir, int idx, unsigned int ofs)
1929 val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
1930 val &= HDA_AMP_VOLMASK;
1939 update_amp_value(struct hda_codec *codec, hda_nid_t nid,
1940 int ch, int dir, int idx, unsigned int ofs,
1943 unsigned int maxval;
1947 /* ofs = 0: raw max value */
1948 maxval = get_amp_max_value(codec, nid, dir, 0);
1951 return snd_hda_codec_amp_update(codec, nid, ch, dir, idx,
1952 HDA_AMP_VOLMASK, val);
1956 * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume
1958 * The control element is supposed to have the private_value field
1959 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1961 int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
1962 struct snd_ctl_elem_value *ucontrol)
1964 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1965 hda_nid_t nid = get_amp_nid(kcontrol);
1966 int chs = get_amp_channels(kcontrol);
1967 int dir = get_amp_direction(kcontrol);
1968 int idx = get_amp_index(kcontrol);
1969 unsigned int ofs = get_amp_offset(kcontrol);
1970 long *valp = ucontrol->value.integer.value;
1973 *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs);
1975 *valp = read_amp_value(codec, nid, 1, dir, idx, ofs);
1978 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_get);
1981 * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume
1983 * The control element is supposed to have the private_value field
1984 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1986 int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
1987 struct snd_ctl_elem_value *ucontrol)
1989 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1990 hda_nid_t nid = get_amp_nid(kcontrol);
1991 int chs = get_amp_channels(kcontrol);
1992 int dir = get_amp_direction(kcontrol);
1993 int idx = get_amp_index(kcontrol);
1994 unsigned int ofs = get_amp_offset(kcontrol);
1995 long *valp = ucontrol->value.integer.value;
1998 snd_hda_power_up(codec);
2000 change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp);
2004 change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp);
2005 snd_hda_power_down(codec);
2008 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_put);
2011 * snd_hda_mixer_amp_volume_put - TLV callback for a standard AMP mixer volume
2013 * The control element is supposed to have the private_value field
2014 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2016 int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2017 unsigned int size, unsigned int __user *_tlv)
2019 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2020 hda_nid_t nid = get_amp_nid(kcontrol);
2021 int dir = get_amp_direction(kcontrol);
2022 unsigned int ofs = get_amp_offset(kcontrol);
2023 bool min_mute = get_amp_min_mute(kcontrol);
2024 u32 caps, val1, val2;
2026 if (size < 4 * sizeof(unsigned int))
2028 caps = query_amp_caps(codec, nid, dir);
2029 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2030 val2 = (val2 + 1) * 25;
2031 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
2033 val1 = ((int)val1) * ((int)val2);
2034 if (min_mute || (caps & AC_AMPCAP_MIN_MUTE))
2035 val2 |= TLV_DB_SCALE_MUTE;
2036 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
2038 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
2040 if (put_user(val1, _tlv + 2))
2042 if (put_user(val2, _tlv + 3))
2046 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_tlv);
2049 * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control
2050 * @codec: HD-audio codec
2051 * @nid: NID of a reference widget
2052 * @dir: #HDA_INPUT or #HDA_OUTPUT
2053 * @tlv: TLV data to be stored, at least 4 elements
2055 * Set (static) TLV data for a virtual master volume using the AMP caps
2056 * obtained from the reference NID.
2057 * The volume range is recalculated as if the max volume is 0dB.
2059 void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
2065 caps = query_amp_caps(codec, nid, dir);
2066 nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
2067 step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2068 step = (step + 1) * 25;
2069 tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
2070 tlv[1] = 2 * sizeof(unsigned int);
2071 tlv[2] = -nums * step;
2074 EXPORT_SYMBOL_HDA(snd_hda_set_vmaster_tlv);
2076 /* find a mixer control element with the given name */
2077 static struct snd_kcontrol *
2078 _snd_hda_find_mixer_ctl(struct hda_codec *codec,
2079 const char *name, int idx)
2081 struct snd_ctl_elem_id id;
2082 memset(&id, 0, sizeof(id));
2083 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
2085 if (snd_BUG_ON(strlen(name) >= sizeof(id.name)))
2087 strcpy(id.name, name);
2088 return snd_ctl_find_id(codec->bus->card, &id);
2092 * snd_hda_find_mixer_ctl - Find a mixer control element with the given name
2093 * @codec: HD-audio codec
2094 * @name: ctl id name string
2096 * Get the control element with the given id string and IFACE_MIXER.
2098 struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
2101 return _snd_hda_find_mixer_ctl(codec, name, 0);
2103 EXPORT_SYMBOL_HDA(snd_hda_find_mixer_ctl);
2105 static int find_empty_mixer_ctl_idx(struct hda_codec *codec, const char *name)
2108 for (idx = 0; idx < 16; idx++) { /* 16 ctlrs should be large enough */
2109 if (!_snd_hda_find_mixer_ctl(codec, name, idx))
2116 * snd_hda_ctl_add - Add a control element and assign to the codec
2117 * @codec: HD-audio codec
2118 * @nid: corresponding NID (optional)
2119 * @kctl: the control element to assign
2121 * Add the given control element to an array inside the codec instance.
2122 * All control elements belonging to a codec are supposed to be added
2123 * by this function so that a proper clean-up works at the free or
2124 * reconfiguration time.
2126 * If non-zero @nid is passed, the NID is assigned to the control element.
2127 * The assignment is shown in the codec proc file.
2129 * snd_hda_ctl_add() checks the control subdev id field whether
2130 * #HDA_SUBDEV_NID_FLAG bit is set. If set (and @nid is zero), the lower
2131 * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit
2132 * specifies if kctl->private_value is a HDA amplifier value.
2134 int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid,
2135 struct snd_kcontrol *kctl)
2138 unsigned short flags = 0;
2139 struct hda_nid_item *item;
2141 if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) {
2142 flags |= HDA_NID_ITEM_AMP;
2144 nid = get_amp_nid_(kctl->private_value);
2146 if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0)
2147 nid = kctl->id.subdevice & 0xffff;
2148 if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG))
2149 kctl->id.subdevice = 0;
2150 err = snd_ctl_add(codec->bus->card, kctl);
2153 item = snd_array_new(&codec->mixers);
2158 item->flags = flags;
2161 EXPORT_SYMBOL_HDA(snd_hda_ctl_add);
2164 * snd_hda_add_nid - Assign a NID to a control element
2165 * @codec: HD-audio codec
2166 * @nid: corresponding NID (optional)
2167 * @kctl: the control element to assign
2168 * @index: index to kctl
2170 * Add the given control element to an array inside the codec instance.
2171 * This function is used when #snd_hda_ctl_add cannot be used for 1:1
2172 * NID:KCTL mapping - for example "Capture Source" selector.
2174 int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl,
2175 unsigned int index, hda_nid_t nid)
2177 struct hda_nid_item *item;
2180 item = snd_array_new(&codec->nids);
2184 item->index = index;
2188 printk(KERN_ERR "hda-codec: no NID for mapping control %s:%d:%d\n",
2189 kctl->id.name, kctl->id.index, index);
2192 EXPORT_SYMBOL_HDA(snd_hda_add_nid);
2195 * snd_hda_ctls_clear - Clear all controls assigned to the given codec
2196 * @codec: HD-audio codec
2198 void snd_hda_ctls_clear(struct hda_codec *codec)
2201 struct hda_nid_item *items = codec->mixers.list;
2202 for (i = 0; i < codec->mixers.used; i++)
2203 snd_ctl_remove(codec->bus->card, items[i].kctl);
2204 snd_array_free(&codec->mixers);
2205 snd_array_free(&codec->nids);
2208 /* pseudo device locking
2209 * toggle card->shutdown to allow/disallow the device access (as a hack)
2211 static int hda_lock_devices(struct snd_card *card)
2213 spin_lock(&card->files_lock);
2214 if (card->shutdown) {
2215 spin_unlock(&card->files_lock);
2219 spin_unlock(&card->files_lock);
2223 static void hda_unlock_devices(struct snd_card *card)
2225 spin_lock(&card->files_lock);
2227 spin_unlock(&card->files_lock);
2231 * snd_hda_codec_reset - Clear all objects assigned to the codec
2232 * @codec: HD-audio codec
2234 * This frees the all PCM and control elements assigned to the codec, and
2235 * clears the caches and restores the pin default configurations.
2237 * When a device is being used, it returns -EBSY. If successfully freed,
2240 int snd_hda_codec_reset(struct hda_codec *codec)
2242 struct snd_card *card = codec->bus->card;
2245 if (hda_lock_devices(card) < 0)
2247 /* check whether the codec isn't used by any mixer or PCM streams */
2248 if (!list_empty(&card->ctl_files)) {
2249 hda_unlock_devices(card);
2252 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
2253 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
2256 if (cpcm->pcm->streams[0].substream_opened ||
2257 cpcm->pcm->streams[1].substream_opened) {
2258 hda_unlock_devices(card);
2263 /* OK, let it free */
2265 #ifdef CONFIG_SND_HDA_POWER_SAVE
2266 cancel_delayed_work(&codec->power_work);
2267 flush_workqueue(codec->bus->workq);
2269 snd_hda_ctls_clear(codec);
2271 for (i = 0; i < codec->num_pcms; i++) {
2272 if (codec->pcm_info[i].pcm) {
2273 snd_device_free(card, codec->pcm_info[i].pcm);
2274 clear_bit(codec->pcm_info[i].device,
2275 codec->bus->pcm_dev_bits);
2278 if (codec->patch_ops.free)
2279 codec->patch_ops.free(codec);
2280 snd_hda_jack_tbl_clear(codec);
2281 codec->proc_widget_hook = NULL;
2283 free_hda_cache(&codec->amp_cache);
2284 free_hda_cache(&codec->cmd_cache);
2285 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
2286 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
2287 /* free only driver_pins so that init_pins + user_pins are restored */
2288 snd_array_free(&codec->driver_pins);
2289 restore_pincfgs(codec);
2290 codec->num_pcms = 0;
2291 codec->pcm_info = NULL;
2292 codec->preset = NULL;
2293 memset(&codec->patch_ops, 0, sizeof(codec->patch_ops));
2294 codec->slave_dig_outs = NULL;
2295 codec->spdif_status_reset = 0;
2296 module_put(codec->owner);
2297 codec->owner = NULL;
2299 /* allow device access again */
2300 hda_unlock_devices(card);
2304 typedef int (*map_slave_func_t)(void *, struct snd_kcontrol *);
2306 /* apply the function to all matching slave ctls in the mixer list */
2307 static int map_slaves(struct hda_codec *codec, const char * const *slaves,
2308 const char *suffix, map_slave_func_t func, void *data)
2310 struct hda_nid_item *items;
2311 const char * const *s;
2314 items = codec->mixers.list;
2315 for (i = 0; i < codec->mixers.used; i++) {
2316 struct snd_kcontrol *sctl = items[i].kctl;
2317 if (!sctl || !sctl->id.name ||
2318 sctl->id.iface != SNDRV_CTL_ELEM_IFACE_MIXER)
2320 for (s = slaves; *s; s++) {
2321 char tmpname[sizeof(sctl->id.name)];
2322 const char *name = *s;
2324 snprintf(tmpname, sizeof(tmpname), "%s %s",
2328 if (!strcmp(sctl->id.name, name)) {
2329 err = func(data, sctl);
2339 static int check_slave_present(void *data, struct snd_kcontrol *sctl)
2344 /* guess the value corresponding to 0dB */
2345 static int get_kctl_0dB_offset(struct snd_kcontrol *kctl)
2348 const int *tlv = NULL;
2351 if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
2352 /* FIXME: set_fs() hack for obtaining user-space TLV data */
2353 mm_segment_t fs = get_fs();
2355 if (!kctl->tlv.c(kctl, 0, sizeof(_tlv), _tlv))
2358 } else if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_READ)
2360 if (tlv && tlv[0] == SNDRV_CTL_TLVT_DB_SCALE)
2361 val = -tlv[2] / tlv[3];
2365 /* call kctl->put with the given value(s) */
2366 static int put_kctl_with_value(struct snd_kcontrol *kctl, int val)
2368 struct snd_ctl_elem_value *ucontrol;
2369 ucontrol = kzalloc(sizeof(*ucontrol), GFP_KERNEL);
2372 ucontrol->value.integer.value[0] = val;
2373 ucontrol->value.integer.value[1] = val;
2374 kctl->put(kctl, ucontrol);
2379 /* initialize the slave volume with 0dB */
2380 static int init_slave_0dB(void *data, struct snd_kcontrol *slave)
2382 int offset = get_kctl_0dB_offset(slave);
2384 put_kctl_with_value(slave, offset);
2388 /* unmute the slave */
2389 static int init_slave_unmute(void *data, struct snd_kcontrol *slave)
2391 return put_kctl_with_value(slave, 1);
2395 * snd_hda_add_vmaster - create a virtual master control and add slaves
2396 * @codec: HD-audio codec
2397 * @name: vmaster control name
2398 * @tlv: TLV data (optional)
2399 * @slaves: slave control names (optional)
2400 * @suffix: suffix string to each slave name (optional)
2401 * @init_slave_vol: initialize slaves to unmute/0dB
2402 * @ctl_ret: store the vmaster kcontrol in return
2404 * Create a virtual master control with the given name. The TLV data
2405 * must be either NULL or a valid data.
2407 * @slaves is a NULL-terminated array of strings, each of which is a
2408 * slave control name. All controls with these names are assigned to
2409 * the new virtual master control.
2411 * This function returns zero if successful or a negative error code.
2413 int __snd_hda_add_vmaster(struct hda_codec *codec, char *name,
2414 unsigned int *tlv, const char * const *slaves,
2415 const char *suffix, bool init_slave_vol,
2416 struct snd_kcontrol **ctl_ret)
2418 struct snd_kcontrol *kctl;
2424 err = map_slaves(codec, slaves, suffix, check_slave_present, NULL);
2426 snd_printdd("No slave found for %s\n", name);
2429 kctl = snd_ctl_make_virtual_master(name, tlv);
2432 err = snd_hda_ctl_add(codec, 0, kctl);
2436 err = map_slaves(codec, slaves, suffix,
2437 (map_slave_func_t)snd_ctl_add_slave, kctl);
2441 /* init with master mute & zero volume */
2442 put_kctl_with_value(kctl, 0);
2444 map_slaves(codec, slaves, suffix,
2445 tlv ? init_slave_0dB : init_slave_unmute, kctl);
2451 EXPORT_SYMBOL_HDA(__snd_hda_add_vmaster);
2454 * mute-LED control using vmaster
2456 static int vmaster_mute_mode_info(struct snd_kcontrol *kcontrol,
2457 struct snd_ctl_elem_info *uinfo)
2459 static const char * const texts[] = {
2460 "Off", "On", "Follow Master"
2464 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2466 uinfo->value.enumerated.items = 3;
2467 index = uinfo->value.enumerated.item;
2470 strcpy(uinfo->value.enumerated.name, texts[index]);
2474 static int vmaster_mute_mode_get(struct snd_kcontrol *kcontrol,
2475 struct snd_ctl_elem_value *ucontrol)
2477 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2478 ucontrol->value.enumerated.item[0] = hook->mute_mode;
2482 static int vmaster_mute_mode_put(struct snd_kcontrol *kcontrol,
2483 struct snd_ctl_elem_value *ucontrol)
2485 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2486 unsigned int old_mode = hook->mute_mode;
2488 hook->mute_mode = ucontrol->value.enumerated.item[0];
2489 if (hook->mute_mode > HDA_VMUTE_FOLLOW_MASTER)
2490 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
2491 if (old_mode == hook->mute_mode)
2493 snd_hda_sync_vmaster_hook(hook);
2497 static struct snd_kcontrol_new vmaster_mute_mode = {
2498 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2499 .name = "Mute-LED Mode",
2500 .info = vmaster_mute_mode_info,
2501 .get = vmaster_mute_mode_get,
2502 .put = vmaster_mute_mode_put,
2506 * Add a mute-LED hook with the given vmaster switch kctl
2507 * "Mute-LED Mode" control is automatically created and associated with
2510 int snd_hda_add_vmaster_hook(struct hda_codec *codec,
2511 struct hda_vmaster_mute_hook *hook,
2512 bool expose_enum_ctl)
2514 struct snd_kcontrol *kctl;
2516 if (!hook->hook || !hook->sw_kctl)
2518 snd_ctl_add_vmaster_hook(hook->sw_kctl, hook->hook, codec);
2519 hook->codec = codec;
2520 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
2521 if (!expose_enum_ctl)
2523 kctl = snd_ctl_new1(&vmaster_mute_mode, hook);
2526 return snd_hda_ctl_add(codec, 0, kctl);
2528 EXPORT_SYMBOL_HDA(snd_hda_add_vmaster_hook);
2531 * Call the hook with the current value for synchronization
2532 * Should be called in init callback
2534 void snd_hda_sync_vmaster_hook(struct hda_vmaster_mute_hook *hook)
2536 if (!hook->hook || !hook->codec)
2538 switch (hook->mute_mode) {
2539 case HDA_VMUTE_FOLLOW_MASTER:
2540 snd_ctl_sync_vmaster_hook(hook->sw_kctl);
2543 hook->hook(hook->codec, hook->mute_mode);
2547 EXPORT_SYMBOL_HDA(snd_hda_sync_vmaster_hook);
2551 * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch
2553 * The control element is supposed to have the private_value field
2554 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2556 int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
2557 struct snd_ctl_elem_info *uinfo)
2559 int chs = get_amp_channels(kcontrol);
2561 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2562 uinfo->count = chs == 3 ? 2 : 1;
2563 uinfo->value.integer.min = 0;
2564 uinfo->value.integer.max = 1;
2567 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_info);
2570 * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch
2572 * The control element is supposed to have the private_value field
2573 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2575 int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
2576 struct snd_ctl_elem_value *ucontrol)
2578 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2579 hda_nid_t nid = get_amp_nid(kcontrol);
2580 int chs = get_amp_channels(kcontrol);
2581 int dir = get_amp_direction(kcontrol);
2582 int idx = get_amp_index(kcontrol);
2583 long *valp = ucontrol->value.integer.value;
2586 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
2587 HDA_AMP_MUTE) ? 0 : 1;
2589 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
2590 HDA_AMP_MUTE) ? 0 : 1;
2593 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_get);
2596 * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch
2598 * The control element is supposed to have the private_value field
2599 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2601 int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
2602 struct snd_ctl_elem_value *ucontrol)
2604 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2605 hda_nid_t nid = get_amp_nid(kcontrol);
2606 int chs = get_amp_channels(kcontrol);
2607 int dir = get_amp_direction(kcontrol);
2608 int idx = get_amp_index(kcontrol);
2609 long *valp = ucontrol->value.integer.value;
2612 snd_hda_power_up(codec);
2614 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
2616 *valp ? 0 : HDA_AMP_MUTE);
2620 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
2622 *valp ? 0 : HDA_AMP_MUTE);
2623 hda_call_check_power_status(codec, nid);
2624 snd_hda_power_down(codec);
2627 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put);
2629 #ifdef CONFIG_SND_HDA_INPUT_BEEP
2631 * snd_hda_mixer_amp_switch_put_beep - Put callback for a beep AMP switch
2633 * This function calls snd_hda_enable_beep_device(), which behaves differently
2634 * depending on beep_mode option.
2636 int snd_hda_mixer_amp_switch_put_beep(struct snd_kcontrol *kcontrol,
2637 struct snd_ctl_elem_value *ucontrol)
2639 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2640 long *valp = ucontrol->value.integer.value;
2642 snd_hda_enable_beep_device(codec, *valp);
2643 return snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2645 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put_beep);
2646 #endif /* CONFIG_SND_HDA_INPUT_BEEP */
2649 * bound volume controls
2651 * bind multiple volumes (# indices, from 0)
2654 #define AMP_VAL_IDX_SHIFT 19
2655 #define AMP_VAL_IDX_MASK (0x0f<<19)
2658 * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control
2660 * The control element is supposed to have the private_value field
2661 * set up via HDA_BIND_MUTE*() macros.
2663 int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
2664 struct snd_ctl_elem_value *ucontrol)
2666 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2670 mutex_lock(&codec->control_mutex);
2671 pval = kcontrol->private_value;
2672 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
2673 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
2674 kcontrol->private_value = pval;
2675 mutex_unlock(&codec->control_mutex);
2678 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_get);
2681 * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control
2683 * The control element is supposed to have the private_value field
2684 * set up via HDA_BIND_MUTE*() macros.
2686 int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
2687 struct snd_ctl_elem_value *ucontrol)
2689 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2691 int i, indices, err = 0, change = 0;
2693 mutex_lock(&codec->control_mutex);
2694 pval = kcontrol->private_value;
2695 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
2696 for (i = 0; i < indices; i++) {
2697 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
2698 (i << AMP_VAL_IDX_SHIFT);
2699 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2704 kcontrol->private_value = pval;
2705 mutex_unlock(&codec->control_mutex);
2706 return err < 0 ? err : change;
2708 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_put);
2711 * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control
2713 * The control element is supposed to have the private_value field
2714 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2716 int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
2717 struct snd_ctl_elem_info *uinfo)
2719 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2720 struct hda_bind_ctls *c;
2723 mutex_lock(&codec->control_mutex);
2724 c = (struct hda_bind_ctls *)kcontrol->private_value;
2725 kcontrol->private_value = *c->values;
2726 err = c->ops->info(kcontrol, uinfo);
2727 kcontrol->private_value = (long)c;
2728 mutex_unlock(&codec->control_mutex);
2731 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_info);
2734 * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control
2736 * The control element is supposed to have the private_value field
2737 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2739 int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
2740 struct snd_ctl_elem_value *ucontrol)
2742 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2743 struct hda_bind_ctls *c;
2746 mutex_lock(&codec->control_mutex);
2747 c = (struct hda_bind_ctls *)kcontrol->private_value;
2748 kcontrol->private_value = *c->values;
2749 err = c->ops->get(kcontrol, ucontrol);
2750 kcontrol->private_value = (long)c;
2751 mutex_unlock(&codec->control_mutex);
2754 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_get);
2757 * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control
2759 * The control element is supposed to have the private_value field
2760 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2762 int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
2763 struct snd_ctl_elem_value *ucontrol)
2765 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2766 struct hda_bind_ctls *c;
2767 unsigned long *vals;
2768 int err = 0, change = 0;
2770 mutex_lock(&codec->control_mutex);
2771 c = (struct hda_bind_ctls *)kcontrol->private_value;
2772 for (vals = c->values; *vals; vals++) {
2773 kcontrol->private_value = *vals;
2774 err = c->ops->put(kcontrol, ucontrol);
2779 kcontrol->private_value = (long)c;
2780 mutex_unlock(&codec->control_mutex);
2781 return err < 0 ? err : change;
2783 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_put);
2786 * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control
2788 * The control element is supposed to have the private_value field
2789 * set up via HDA_BIND_VOL() macro.
2791 int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2792 unsigned int size, unsigned int __user *tlv)
2794 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2795 struct hda_bind_ctls *c;
2798 mutex_lock(&codec->control_mutex);
2799 c = (struct hda_bind_ctls *)kcontrol->private_value;
2800 kcontrol->private_value = *c->values;
2801 err = c->ops->tlv(kcontrol, op_flag, size, tlv);
2802 kcontrol->private_value = (long)c;
2803 mutex_unlock(&codec->control_mutex);
2806 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_tlv);
2808 struct hda_ctl_ops snd_hda_bind_vol = {
2809 .info = snd_hda_mixer_amp_volume_info,
2810 .get = snd_hda_mixer_amp_volume_get,
2811 .put = snd_hda_mixer_amp_volume_put,
2812 .tlv = snd_hda_mixer_amp_tlv
2814 EXPORT_SYMBOL_HDA(snd_hda_bind_vol);
2816 struct hda_ctl_ops snd_hda_bind_sw = {
2817 .info = snd_hda_mixer_amp_switch_info,
2818 .get = snd_hda_mixer_amp_switch_get,
2819 .put = snd_hda_mixer_amp_switch_put,
2820 .tlv = snd_hda_mixer_amp_tlv
2822 EXPORT_SYMBOL_HDA(snd_hda_bind_sw);
2825 * SPDIF out controls
2828 static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
2829 struct snd_ctl_elem_info *uinfo)
2831 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
2836 static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
2837 struct snd_ctl_elem_value *ucontrol)
2839 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2840 IEC958_AES0_NONAUDIO |
2841 IEC958_AES0_CON_EMPHASIS_5015 |
2842 IEC958_AES0_CON_NOT_COPYRIGHT;
2843 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
2844 IEC958_AES1_CON_ORIGINAL;
2848 static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
2849 struct snd_ctl_elem_value *ucontrol)
2851 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2852 IEC958_AES0_NONAUDIO |
2853 IEC958_AES0_PRO_EMPHASIS_5015;
2857 static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
2858 struct snd_ctl_elem_value *ucontrol)
2860 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2861 int idx = kcontrol->private_value;
2862 struct hda_spdif_out *spdif = snd_array_elem(&codec->spdif_out, idx);
2864 ucontrol->value.iec958.status[0] = spdif->status & 0xff;
2865 ucontrol->value.iec958.status[1] = (spdif->status >> 8) & 0xff;
2866 ucontrol->value.iec958.status[2] = (spdif->status >> 16) & 0xff;
2867 ucontrol->value.iec958.status[3] = (spdif->status >> 24) & 0xff;
2872 /* convert from SPDIF status bits to HDA SPDIF bits
2873 * bit 0 (DigEn) is always set zero (to be filled later)
2875 static unsigned short convert_from_spdif_status(unsigned int sbits)
2877 unsigned short val = 0;
2879 if (sbits & IEC958_AES0_PROFESSIONAL)
2880 val |= AC_DIG1_PROFESSIONAL;
2881 if (sbits & IEC958_AES0_NONAUDIO)
2882 val |= AC_DIG1_NONAUDIO;
2883 if (sbits & IEC958_AES0_PROFESSIONAL) {
2884 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
2885 IEC958_AES0_PRO_EMPHASIS_5015)
2886 val |= AC_DIG1_EMPHASIS;
2888 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
2889 IEC958_AES0_CON_EMPHASIS_5015)
2890 val |= AC_DIG1_EMPHASIS;
2891 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
2892 val |= AC_DIG1_COPYRIGHT;
2893 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
2894 val |= AC_DIG1_LEVEL;
2895 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
2900 /* convert to SPDIF status bits from HDA SPDIF bits
2902 static unsigned int convert_to_spdif_status(unsigned short val)
2904 unsigned int sbits = 0;
2906 if (val & AC_DIG1_NONAUDIO)
2907 sbits |= IEC958_AES0_NONAUDIO;
2908 if (val & AC_DIG1_PROFESSIONAL)
2909 sbits |= IEC958_AES0_PROFESSIONAL;
2910 if (sbits & IEC958_AES0_PROFESSIONAL) {
2911 if (sbits & AC_DIG1_EMPHASIS)
2912 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
2914 if (val & AC_DIG1_EMPHASIS)
2915 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
2916 if (!(val & AC_DIG1_COPYRIGHT))
2917 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
2918 if (val & AC_DIG1_LEVEL)
2919 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
2920 sbits |= val & (0x7f << 8);
2925 /* set digital convert verbs both for the given NID and its slaves */
2926 static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
2931 snd_hda_codec_write_cache(codec, nid, 0, verb, val);
2932 d = codec->slave_dig_outs;
2936 snd_hda_codec_write_cache(codec, *d, 0, verb, val);
2939 static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
2943 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1);
2945 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2);
2948 static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
2949 struct snd_ctl_elem_value *ucontrol)
2951 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2952 int idx = kcontrol->private_value;
2953 struct hda_spdif_out *spdif = snd_array_elem(&codec->spdif_out, idx);
2954 hda_nid_t nid = spdif->nid;
2958 mutex_lock(&codec->spdif_mutex);
2959 spdif->status = ucontrol->value.iec958.status[0] |
2960 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
2961 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
2962 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
2963 val = convert_from_spdif_status(spdif->status);
2964 val |= spdif->ctls & 1;
2965 change = spdif->ctls != val;
2967 if (change && nid != (u16)-1)
2968 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
2969 mutex_unlock(&codec->spdif_mutex);
2973 #define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
2975 static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
2976 struct snd_ctl_elem_value *ucontrol)
2978 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2979 int idx = kcontrol->private_value;
2980 struct hda_spdif_out *spdif = snd_array_elem(&codec->spdif_out, idx);
2982 ucontrol->value.integer.value[0] = spdif->ctls & AC_DIG1_ENABLE;
2986 static inline void set_spdif_ctls(struct hda_codec *codec, hda_nid_t nid,
2989 set_dig_out_convert(codec, nid, dig1, dig2);
2990 /* unmute amp switch (if any) */
2991 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
2992 (dig1 & AC_DIG1_ENABLE))
2993 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
2997 static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
2998 struct snd_ctl_elem_value *ucontrol)
3000 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3001 int idx = kcontrol->private_value;
3002 struct hda_spdif_out *spdif = snd_array_elem(&codec->spdif_out, idx);
3003 hda_nid_t nid = spdif->nid;
3007 mutex_lock(&codec->spdif_mutex);
3008 val = spdif->ctls & ~AC_DIG1_ENABLE;
3009 if (ucontrol->value.integer.value[0])
3010 val |= AC_DIG1_ENABLE;
3011 change = spdif->ctls != val;
3013 if (change && nid != (u16)-1)
3014 set_spdif_ctls(codec, nid, val & 0xff, -1);
3015 mutex_unlock(&codec->spdif_mutex);
3019 static struct snd_kcontrol_new dig_mixes[] = {
3021 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3022 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3023 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
3024 .info = snd_hda_spdif_mask_info,
3025 .get = snd_hda_spdif_cmask_get,
3028 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3029 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3030 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK),
3031 .info = snd_hda_spdif_mask_info,
3032 .get = snd_hda_spdif_pmask_get,
3035 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3036 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
3037 .info = snd_hda_spdif_mask_info,
3038 .get = snd_hda_spdif_default_get,
3039 .put = snd_hda_spdif_default_put,
3042 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3043 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
3044 .info = snd_hda_spdif_out_switch_info,
3045 .get = snd_hda_spdif_out_switch_get,
3046 .put = snd_hda_spdif_out_switch_put,
3052 * snd_hda_create_spdif_out_ctls - create Output SPDIF-related controls
3053 * @codec: the HDA codec
3054 * @nid: audio out widget NID
3056 * Creates controls related with the SPDIF output.
3057 * Called from each patch supporting the SPDIF out.
3059 * Returns 0 if successful, or a negative error code.
3061 int snd_hda_create_spdif_out_ctls(struct hda_codec *codec,
3062 hda_nid_t associated_nid,
3066 struct snd_kcontrol *kctl;
3067 struct snd_kcontrol_new *dig_mix;
3069 struct hda_spdif_out *spdif;
3071 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Playback Switch");
3073 printk(KERN_ERR "hda_codec: too many IEC958 outputs\n");
3076 spdif = snd_array_new(&codec->spdif_out);
3077 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
3078 kctl = snd_ctl_new1(dig_mix, codec);
3081 kctl->id.index = idx;
3082 kctl->private_value = codec->spdif_out.used - 1;
3083 err = snd_hda_ctl_add(codec, associated_nid, kctl);
3087 spdif->nid = cvt_nid;
3088 spdif->ctls = snd_hda_codec_read(codec, cvt_nid, 0,
3089 AC_VERB_GET_DIGI_CONVERT_1, 0);
3090 spdif->status = convert_to_spdif_status(spdif->ctls);
3093 EXPORT_SYMBOL_HDA(snd_hda_create_spdif_out_ctls);
3095 struct hda_spdif_out *snd_hda_spdif_out_of_nid(struct hda_codec *codec,
3099 for (i = 0; i < codec->spdif_out.used; i++) {
3100 struct hda_spdif_out *spdif =
3101 snd_array_elem(&codec->spdif_out, i);
3102 if (spdif->nid == nid)
3107 EXPORT_SYMBOL_HDA(snd_hda_spdif_out_of_nid);
3109 void snd_hda_spdif_ctls_unassign(struct hda_codec *codec, int idx)
3111 struct hda_spdif_out *spdif = snd_array_elem(&codec->spdif_out, idx);
3113 mutex_lock(&codec->spdif_mutex);
3114 spdif->nid = (u16)-1;
3115 mutex_unlock(&codec->spdif_mutex);
3117 EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_unassign);
3119 void snd_hda_spdif_ctls_assign(struct hda_codec *codec, int idx, hda_nid_t nid)
3121 struct hda_spdif_out *spdif = snd_array_elem(&codec->spdif_out, idx);
3124 mutex_lock(&codec->spdif_mutex);
3125 if (spdif->nid != nid) {
3128 set_spdif_ctls(codec, nid, val & 0xff, (val >> 8) & 0xff);
3130 mutex_unlock(&codec->spdif_mutex);
3132 EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_assign);
3135 * SPDIF sharing with analog output
3137 static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
3138 struct snd_ctl_elem_value *ucontrol)
3140 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3141 ucontrol->value.integer.value[0] = mout->share_spdif;
3145 static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
3146 struct snd_ctl_elem_value *ucontrol)
3148 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3149 mout->share_spdif = !!ucontrol->value.integer.value[0];
3153 static struct snd_kcontrol_new spdif_share_sw = {
3154 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3155 .name = "IEC958 Default PCM Playback Switch",
3156 .info = snd_ctl_boolean_mono_info,
3157 .get = spdif_share_sw_get,
3158 .put = spdif_share_sw_put,
3162 * snd_hda_create_spdif_share_sw - create Default PCM switch
3163 * @codec: the HDA codec
3164 * @mout: multi-out instance
3166 int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
3167 struct hda_multi_out *mout)
3169 if (!mout->dig_out_nid)
3171 /* ATTENTION: here mout is passed as private_data, instead of codec */
3172 return snd_hda_ctl_add(codec, mout->dig_out_nid,
3173 snd_ctl_new1(&spdif_share_sw, mout));
3175 EXPORT_SYMBOL_HDA(snd_hda_create_spdif_share_sw);
3181 #define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
3183 static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
3184 struct snd_ctl_elem_value *ucontrol)
3186 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3188 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
3192 static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
3193 struct snd_ctl_elem_value *ucontrol)
3195 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3196 hda_nid_t nid = kcontrol->private_value;
3197 unsigned int val = !!ucontrol->value.integer.value[0];
3200 mutex_lock(&codec->spdif_mutex);
3201 change = codec->spdif_in_enable != val;
3203 codec->spdif_in_enable = val;
3204 snd_hda_codec_write_cache(codec, nid, 0,
3205 AC_VERB_SET_DIGI_CONVERT_1, val);
3207 mutex_unlock(&codec->spdif_mutex);
3211 static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
3212 struct snd_ctl_elem_value *ucontrol)
3214 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3215 hda_nid_t nid = kcontrol->private_value;
3219 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0);
3220 sbits = convert_to_spdif_status(val);
3221 ucontrol->value.iec958.status[0] = sbits;
3222 ucontrol->value.iec958.status[1] = sbits >> 8;
3223 ucontrol->value.iec958.status[2] = sbits >> 16;
3224 ucontrol->value.iec958.status[3] = sbits >> 24;
3228 static struct snd_kcontrol_new dig_in_ctls[] = {
3230 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3231 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH),
3232 .info = snd_hda_spdif_in_switch_info,
3233 .get = snd_hda_spdif_in_switch_get,
3234 .put = snd_hda_spdif_in_switch_put,
3237 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3238 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3239 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
3240 .info = snd_hda_spdif_mask_info,
3241 .get = snd_hda_spdif_in_status_get,
3247 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
3248 * @codec: the HDA codec
3249 * @nid: audio in widget NID
3251 * Creates controls related with the SPDIF input.
3252 * Called from each patch supporting the SPDIF in.
3254 * Returns 0 if successful, or a negative error code.
3256 int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
3259 struct snd_kcontrol *kctl;
3260 struct snd_kcontrol_new *dig_mix;
3263 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Capture Switch");
3265 printk(KERN_ERR "hda_codec: too many IEC958 inputs\n");
3268 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
3269 kctl = snd_ctl_new1(dig_mix, codec);
3272 kctl->private_value = nid;
3273 err = snd_hda_ctl_add(codec, nid, kctl);
3277 codec->spdif_in_enable =
3278 snd_hda_codec_read(codec, nid, 0,
3279 AC_VERB_GET_DIGI_CONVERT_1, 0) &
3283 EXPORT_SYMBOL_HDA(snd_hda_create_spdif_in_ctls);
3290 /* build a 32bit cache key with the widget id and the command parameter */
3291 #define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
3292 #define get_cmd_cache_nid(key) ((key) & 0xff)
3293 #define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
3296 * snd_hda_codec_write_cache - send a single command with caching
3297 * @codec: the HDA codec
3298 * @nid: NID to send the command
3299 * @direct: direct flag
3300 * @verb: the verb to send
3301 * @parm: the parameter for the verb
3303 * Send a single command without waiting for response.
3305 * Returns 0 if successful, or a negative error code.
3307 int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
3308 int direct, unsigned int verb, unsigned int parm)
3310 int err = snd_hda_codec_write(codec, nid, direct, verb, parm);
3311 struct hda_cache_head *c;
3316 /* parm may contain the verb stuff for get/set amp */
3317 verb = verb | (parm >> 8);
3319 key = build_cmd_cache_key(nid, verb);
3320 mutex_lock(&codec->bus->cmd_mutex);
3321 c = get_alloc_hash(&codec->cmd_cache, key);
3324 mutex_unlock(&codec->bus->cmd_mutex);
3327 EXPORT_SYMBOL_HDA(snd_hda_codec_write_cache);
3330 * snd_hda_codec_update_cache - check cache and write the cmd only when needed
3331 * @codec: the HDA codec
3332 * @nid: NID to send the command
3333 * @direct: direct flag
3334 * @verb: the verb to send
3335 * @parm: the parameter for the verb
3337 * This function works like snd_hda_codec_write_cache(), but it doesn't send
3338 * command if the parameter is already identical with the cached value.
3339 * If not, it sends the command and refreshes the cache.
3341 * Returns 0 if successful, or a negative error code.
3343 int snd_hda_codec_update_cache(struct hda_codec *codec, hda_nid_t nid,
3344 int direct, unsigned int verb, unsigned int parm)
3346 struct hda_cache_head *c;
3349 /* parm may contain the verb stuff for get/set amp */
3350 verb = verb | (parm >> 8);
3352 key = build_cmd_cache_key(nid, verb);
3353 mutex_lock(&codec->bus->cmd_mutex);
3354 c = get_hash(&codec->cmd_cache, key);
3355 if (c && c->val == parm) {
3356 mutex_unlock(&codec->bus->cmd_mutex);
3359 mutex_unlock(&codec->bus->cmd_mutex);
3360 return snd_hda_codec_write_cache(codec, nid, direct, verb, parm);
3362 EXPORT_SYMBOL_HDA(snd_hda_codec_update_cache);
3365 * snd_hda_codec_resume_cache - Resume the all commands from the cache
3366 * @codec: HD-audio codec
3368 * Execute all verbs recorded in the command caches to resume.
3370 void snd_hda_codec_resume_cache(struct hda_codec *codec)
3372 struct hda_cache_head *buffer = codec->cmd_cache.buf.list;
3375 for (i = 0; i < codec->cmd_cache.buf.used; i++, buffer++) {
3376 u32 key = buffer->key;
3379 snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
3380 get_cmd_cache_cmd(key), buffer->val);
3383 EXPORT_SYMBOL_HDA(snd_hda_codec_resume_cache);
3386 * snd_hda_sequence_write_cache - sequence writes with caching
3387 * @codec: the HDA codec
3388 * @seq: VERB array to send
3390 * Send the commands sequentially from the given array.
3391 * Thte commands are recorded on cache for power-save and resume.
3392 * The array must be terminated with NID=0.
3394 void snd_hda_sequence_write_cache(struct hda_codec *codec,
3395 const struct hda_verb *seq)
3397 for (; seq->nid; seq++)
3398 snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
3401 EXPORT_SYMBOL_HDA(snd_hda_sequence_write_cache);
3402 #endif /* CONFIG_PM */
3404 void snd_hda_codec_set_power_to_all(struct hda_codec *codec, hda_nid_t fg,
3405 unsigned int power_state,
3406 bool eapd_workaround)
3408 hda_nid_t nid = codec->start_nid;
3411 for (i = 0; i < codec->num_nodes; i++, nid++) {
3412 unsigned int wcaps = get_wcaps(codec, nid);
3413 if (!(wcaps & AC_WCAP_POWER))
3415 /* don't power down the widget if it controls eapd and
3416 * EAPD_BTLENABLE is set.
3418 if (eapd_workaround && power_state == AC_PWRST_D3 &&
3419 get_wcaps_type(wcaps) == AC_WID_PIN &&
3420 (snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD)) {
3421 int eapd = snd_hda_codec_read(codec, nid, 0,
3422 AC_VERB_GET_EAPD_BTLENABLE, 0);
3426 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE,
3430 if (power_state == AC_PWRST_D0) {
3431 unsigned long end_time;
3433 /* wait until the codec reachs to D0 */
3434 end_time = jiffies + msecs_to_jiffies(500);
3436 state = snd_hda_codec_read(codec, fg, 0,
3437 AC_VERB_GET_POWER_STATE, 0);
3438 if (state == power_state)
3441 } while (time_after_eq(end_time, jiffies));
3444 EXPORT_SYMBOL_HDA(snd_hda_codec_set_power_to_all);
3447 * set power state of the codec
3449 static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
3450 unsigned int power_state)
3452 if (codec->patch_ops.set_power_state) {
3453 codec->patch_ops.set_power_state(codec, fg, power_state);
3457 /* this delay seems necessary to avoid click noise at power-down */
3458 if (power_state == AC_PWRST_D3)
3460 snd_hda_codec_read(codec, fg, 0, AC_VERB_SET_POWER_STATE,
3462 snd_hda_codec_set_power_to_all(codec, fg, power_state, true);
3465 #ifdef CONFIG_SND_HDA_HWDEP
3466 /* execute additional init verbs */
3467 static void hda_exec_init_verbs(struct hda_codec *codec)
3469 if (codec->init_verbs.list)
3470 snd_hda_sequence_write(codec, codec->init_verbs.list);
3473 static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
3478 * call suspend and power-down; used both from PM and power-save
3480 static void hda_call_codec_suspend(struct hda_codec *codec)
3482 if (codec->patch_ops.suspend)
3483 codec->patch_ops.suspend(codec, PMSG_SUSPEND);
3484 hda_cleanup_all_streams(codec);
3485 hda_set_power_state(codec,
3486 codec->afg ? codec->afg : codec->mfg,
3488 #ifdef CONFIG_SND_HDA_POWER_SAVE
3489 snd_hda_update_power_acct(codec);
3490 cancel_delayed_work(&codec->power_work);
3491 codec->power_on = 0;
3492 codec->power_transition = 0;
3493 codec->power_jiffies = jiffies;
3498 * kick up codec; used both from PM and power-save
3500 static void hda_call_codec_resume(struct hda_codec *codec)
3502 hda_set_power_state(codec,
3503 codec->afg ? codec->afg : codec->mfg,
3505 restore_pincfgs(codec); /* restore all current pin configs */
3506 restore_shutup_pins(codec);
3507 hda_exec_init_verbs(codec);
3508 snd_hda_jack_set_dirty_all(codec);
3509 if (codec->patch_ops.resume)
3510 codec->patch_ops.resume(codec);
3512 if (codec->patch_ops.init)
3513 codec->patch_ops.init(codec);
3514 snd_hda_codec_resume_amp(codec);
3515 snd_hda_codec_resume_cache(codec);
3518 #endif /* CONFIG_PM */
3522 * snd_hda_build_controls - build mixer controls
3525 * Creates mixer controls for each codec included in the bus.
3527 * Returns 0 if successful, otherwise a negative error code.
3529 int /*__devinit*/ snd_hda_build_controls(struct hda_bus *bus)
3531 struct hda_codec *codec;
3533 list_for_each_entry(codec, &bus->codec_list, list) {
3534 int err = snd_hda_codec_build_controls(codec);
3536 printk(KERN_ERR "hda_codec: cannot build controls "
3537 "for #%d (error %d)\n", codec->addr, err);
3538 err = snd_hda_codec_reset(codec);
3541 "hda_codec: cannot revert codec\n");
3548 EXPORT_SYMBOL_HDA(snd_hda_build_controls);
3550 int snd_hda_codec_build_controls(struct hda_codec *codec)
3553 hda_exec_init_verbs(codec);
3554 /* continue to initialize... */
3555 if (codec->patch_ops.init)
3556 err = codec->patch_ops.init(codec);
3557 if (!err && codec->patch_ops.build_controls)
3558 err = codec->patch_ops.build_controls(codec);
3567 struct hda_rate_tbl {
3569 unsigned int alsa_bits;
3570 unsigned int hda_fmt;
3573 /* rate = base * mult / div */
3574 #define HDA_RATE(base, mult, div) \
3575 (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \
3576 (((div) - 1) << AC_FMT_DIV_SHIFT))
3578 static struct hda_rate_tbl rate_bits[] = {
3579 /* rate in Hz, ALSA rate bitmask, HDA format value */
3581 /* autodetected value used in snd_hda_query_supported_pcm */
3582 { 8000, SNDRV_PCM_RATE_8000, HDA_RATE(48, 1, 6) },
3583 { 11025, SNDRV_PCM_RATE_11025, HDA_RATE(44, 1, 4) },
3584 { 16000, SNDRV_PCM_RATE_16000, HDA_RATE(48, 1, 3) },
3585 { 22050, SNDRV_PCM_RATE_22050, HDA_RATE(44, 1, 2) },
3586 { 32000, SNDRV_PCM_RATE_32000, HDA_RATE(48, 2, 3) },
3587 { 44100, SNDRV_PCM_RATE_44100, HDA_RATE(44, 1, 1) },
3588 { 48000, SNDRV_PCM_RATE_48000, HDA_RATE(48, 1, 1) },
3589 { 88200, SNDRV_PCM_RATE_88200, HDA_RATE(44, 2, 1) },
3590 { 96000, SNDRV_PCM_RATE_96000, HDA_RATE(48, 2, 1) },
3591 { 176400, SNDRV_PCM_RATE_176400, HDA_RATE(44, 4, 1) },
3592 { 192000, SNDRV_PCM_RATE_192000, HDA_RATE(48, 4, 1) },
3593 #define AC_PAR_PCM_RATE_BITS 11
3594 /* up to bits 10, 384kHZ isn't supported properly */
3596 /* not autodetected value */
3597 { 9600, SNDRV_PCM_RATE_KNOT, HDA_RATE(48, 1, 5) },
3599 { 0 } /* terminator */
3603 * snd_hda_calc_stream_format - calculate format bitset
3604 * @rate: the sample rate
3605 * @channels: the number of channels
3606 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
3607 * @maxbps: the max. bps
3609 * Calculate the format bitset from the given rate, channels and th PCM format.
3611 * Return zero if invalid.
3613 unsigned int snd_hda_calc_stream_format(unsigned int rate,
3614 unsigned int channels,
3615 unsigned int format,
3616 unsigned int maxbps,
3617 unsigned short spdif_ctls)
3620 unsigned int val = 0;
3622 for (i = 0; rate_bits[i].hz; i++)
3623 if (rate_bits[i].hz == rate) {
3624 val = rate_bits[i].hda_fmt;
3627 if (!rate_bits[i].hz) {
3628 snd_printdd("invalid rate %d\n", rate);
3632 if (channels == 0 || channels > 8) {
3633 snd_printdd("invalid channels %d\n", channels);
3636 val |= channels - 1;
3638 switch (snd_pcm_format_width(format)) {
3640 val |= AC_FMT_BITS_8;
3643 val |= AC_FMT_BITS_16;
3648 if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE)
3649 val |= AC_FMT_BITS_32;
3650 else if (maxbps >= 24)
3651 val |= AC_FMT_BITS_24;
3653 val |= AC_FMT_BITS_20;
3656 snd_printdd("invalid format width %d\n",
3657 snd_pcm_format_width(format));
3661 if (spdif_ctls & AC_DIG1_NONAUDIO)
3662 val |= AC_FMT_TYPE_NON_PCM;
3666 EXPORT_SYMBOL_HDA(snd_hda_calc_stream_format);
3668 static unsigned int get_pcm_param(struct hda_codec *codec, hda_nid_t nid)
3670 unsigned int val = 0;
3671 if (nid != codec->afg &&
3672 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD))
3673 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
3674 if (!val || val == -1)
3675 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
3676 if (!val || val == -1)
3681 static unsigned int query_pcm_param(struct hda_codec *codec, hda_nid_t nid)
3683 return query_caps_hash(codec, nid, HDA_HASH_PARPCM_KEY(nid),
3687 static unsigned int get_stream_param(struct hda_codec *codec, hda_nid_t nid)
3689 unsigned int streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
3690 if (!streams || streams == -1)
3691 streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
3692 if (!streams || streams == -1)
3697 static unsigned int query_stream_param(struct hda_codec *codec, hda_nid_t nid)
3699 return query_caps_hash(codec, nid, HDA_HASH_PARSTR_KEY(nid),
3704 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
3705 * @codec: the HDA codec
3706 * @nid: NID to query
3707 * @ratesp: the pointer to store the detected rate bitflags
3708 * @formatsp: the pointer to store the detected formats
3709 * @bpsp: the pointer to store the detected format widths
3711 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
3712 * or @bsps argument is ignored.
3714 * Returns 0 if successful, otherwise a negative error code.
3716 int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
3717 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
3719 unsigned int i, val, wcaps;
3721 wcaps = get_wcaps(codec, nid);
3722 val = query_pcm_param(codec, nid);
3726 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
3728 rates |= rate_bits[i].alsa_bits;
3731 snd_printk(KERN_ERR "hda_codec: rates == 0 "
3732 "(nid=0x%x, val=0x%x, ovrd=%i)\n",
3734 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0);
3740 if (formatsp || bpsp) {
3742 unsigned int streams, bps;
3744 streams = query_stream_param(codec, nid);
3749 if (streams & AC_SUPFMT_PCM) {
3750 if (val & AC_SUPPCM_BITS_8) {
3751 formats |= SNDRV_PCM_FMTBIT_U8;
3754 if (val & AC_SUPPCM_BITS_16) {
3755 formats |= SNDRV_PCM_FMTBIT_S16_LE;
3758 if (wcaps & AC_WCAP_DIGITAL) {
3759 if (val & AC_SUPPCM_BITS_32)
3760 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
3761 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
3762 formats |= SNDRV_PCM_FMTBIT_S32_LE;
3763 if (val & AC_SUPPCM_BITS_24)
3765 else if (val & AC_SUPPCM_BITS_20)
3767 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
3768 AC_SUPPCM_BITS_32)) {
3769 formats |= SNDRV_PCM_FMTBIT_S32_LE;
3770 if (val & AC_SUPPCM_BITS_32)
3772 else if (val & AC_SUPPCM_BITS_24)
3774 else if (val & AC_SUPPCM_BITS_20)
3778 if (streams & AC_SUPFMT_FLOAT32) {
3779 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
3783 if (streams == AC_SUPFMT_AC3) {
3784 /* should be exclusive */
3785 /* temporary hack: we have still no proper support
3786 * for the direct AC3 stream...
3788 formats |= SNDRV_PCM_FMTBIT_U8;
3792 snd_printk(KERN_ERR "hda_codec: formats == 0 "
3793 "(nid=0x%x, val=0x%x, ovrd=%i, "
3796 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0,
3801 *formatsp = formats;
3808 EXPORT_SYMBOL_HDA(snd_hda_query_supported_pcm);
3811 * snd_hda_is_supported_format - Check the validity of the format
3812 * @codec: HD-audio codec
3813 * @nid: NID to check
3814 * @format: the HD-audio format value to check
3816 * Check whether the given node supports the format value.
3818 * Returns 1 if supported, 0 if not.
3820 int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
3821 unsigned int format)
3824 unsigned int val = 0, rate, stream;
3826 val = query_pcm_param(codec, nid);
3830 rate = format & 0xff00;
3831 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
3832 if (rate_bits[i].hda_fmt == rate) {
3837 if (i >= AC_PAR_PCM_RATE_BITS)
3840 stream = query_stream_param(codec, nid);
3844 if (stream & AC_SUPFMT_PCM) {
3845 switch (format & 0xf0) {
3847 if (!(val & AC_SUPPCM_BITS_8))
3851 if (!(val & AC_SUPPCM_BITS_16))
3855 if (!(val & AC_SUPPCM_BITS_20))
3859 if (!(val & AC_SUPPCM_BITS_24))
3863 if (!(val & AC_SUPPCM_BITS_32))
3870 /* FIXME: check for float32 and AC3? */
3875 EXPORT_SYMBOL_HDA(snd_hda_is_supported_format);
3880 static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
3881 struct hda_codec *codec,
3882 struct snd_pcm_substream *substream)
3887 static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
3888 struct hda_codec *codec,
3889 unsigned int stream_tag,
3890 unsigned int format,
3891 struct snd_pcm_substream *substream)
3893 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
3897 static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
3898 struct hda_codec *codec,
3899 struct snd_pcm_substream *substream)
3901 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
3905 static int set_pcm_default_values(struct hda_codec *codec,
3906 struct hda_pcm_stream *info)
3910 /* query support PCM information from the given NID */
3911 if (info->nid && (!info->rates || !info->formats)) {
3912 err = snd_hda_query_supported_pcm(codec, info->nid,
3913 info->rates ? NULL : &info->rates,
3914 info->formats ? NULL : &info->formats,
3915 info->maxbps ? NULL : &info->maxbps);
3919 if (info->ops.open == NULL)
3920 info->ops.open = hda_pcm_default_open_close;
3921 if (info->ops.close == NULL)
3922 info->ops.close = hda_pcm_default_open_close;
3923 if (info->ops.prepare == NULL) {
3924 if (snd_BUG_ON(!info->nid))
3926 info->ops.prepare = hda_pcm_default_prepare;
3928 if (info->ops.cleanup == NULL) {
3929 if (snd_BUG_ON(!info->nid))
3931 info->ops.cleanup = hda_pcm_default_cleanup;
3937 * codec prepare/cleanup entries
3939 int snd_hda_codec_prepare(struct hda_codec *codec,
3940 struct hda_pcm_stream *hinfo,
3941 unsigned int stream,
3942 unsigned int format,
3943 struct snd_pcm_substream *substream)
3946 mutex_lock(&codec->bus->prepare_mutex);
3947 ret = hinfo->ops.prepare(hinfo, codec, stream, format, substream);
3949 purify_inactive_streams(codec);
3950 mutex_unlock(&codec->bus->prepare_mutex);
3953 EXPORT_SYMBOL_HDA(snd_hda_codec_prepare);
3955 void snd_hda_codec_cleanup(struct hda_codec *codec,
3956 struct hda_pcm_stream *hinfo,
3957 struct snd_pcm_substream *substream)
3959 mutex_lock(&codec->bus->prepare_mutex);
3960 hinfo->ops.cleanup(hinfo, codec, substream);
3961 mutex_unlock(&codec->bus->prepare_mutex);
3963 EXPORT_SYMBOL_HDA(snd_hda_codec_cleanup);
3966 const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = {
3967 "Audio", "SPDIF", "HDMI", "Modem"
3971 * get the empty PCM device number to assign
3973 * note the max device number is limited by HDA_MAX_PCMS, currently 10
3975 static int get_empty_pcm_device(struct hda_bus *bus, int type)
3977 /* audio device indices; not linear to keep compatibility */
3978 static int audio_idx[HDA_PCM_NTYPES][5] = {
3979 [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 },
3980 [HDA_PCM_TYPE_SPDIF] = { 1, -1 },
3981 [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 },
3982 [HDA_PCM_TYPE_MODEM] = { 6, -1 },
3986 if (type >= HDA_PCM_NTYPES) {
3987 snd_printk(KERN_WARNING "Invalid PCM type %d\n", type);
3991 for (i = 0; audio_idx[type][i] >= 0 ; i++)
3992 if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits))
3993 return audio_idx[type][i];
3995 /* non-fixed slots starting from 10 */
3996 for (i = 10; i < 32; i++) {
3997 if (!test_and_set_bit(i, bus->pcm_dev_bits))
4001 snd_printk(KERN_WARNING "Too many %s devices\n",
4002 snd_hda_pcm_type_name[type]);
4007 * attach a new PCM stream
4009 static int snd_hda_attach_pcm(struct hda_codec *codec, struct hda_pcm *pcm)
4011 struct hda_bus *bus = codec->bus;
4012 struct hda_pcm_stream *info;
4015 if (snd_BUG_ON(!pcm->name))
4017 for (stream = 0; stream < 2; stream++) {
4018 info = &pcm->stream[stream];
4019 if (info->substreams) {
4020 err = set_pcm_default_values(codec, info);
4025 return bus->ops.attach_pcm(bus, codec, pcm);
4028 /* assign all PCMs of the given codec */
4029 int snd_hda_codec_build_pcms(struct hda_codec *codec)
4034 if (!codec->num_pcms) {
4035 if (!codec->patch_ops.build_pcms)
4037 err = codec->patch_ops.build_pcms(codec);
4039 printk(KERN_ERR "hda_codec: cannot build PCMs"
4040 "for #%d (error %d)\n", codec->addr, err);
4041 err = snd_hda_codec_reset(codec);
4044 "hda_codec: cannot revert codec\n");
4049 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
4050 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
4053 if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
4054 continue; /* no substreams assigned */
4057 dev = get_empty_pcm_device(codec->bus, cpcm->pcm_type);
4059 continue; /* no fatal error */
4061 err = snd_hda_attach_pcm(codec, cpcm);
4063 printk(KERN_ERR "hda_codec: cannot attach "
4064 "PCM stream %d for codec #%d\n",
4066 continue; /* no fatal error */
4074 * snd_hda_build_pcms - build PCM information
4077 * Create PCM information for each codec included in the bus.
4079 * The build_pcms codec patch is requested to set up codec->num_pcms and
4080 * codec->pcm_info properly. The array is referred by the top-level driver
4081 * to create its PCM instances.
4082 * The allocated codec->pcm_info should be released in codec->patch_ops.free
4085 * At least, substreams, channels_min and channels_max must be filled for
4086 * each stream. substreams = 0 indicates that the stream doesn't exist.
4087 * When rates and/or formats are zero, the supported values are queried
4088 * from the given nid. The nid is used also by the default ops.prepare
4089 * and ops.cleanup callbacks.
4091 * The driver needs to call ops.open in its open callback. Similarly,
4092 * ops.close is supposed to be called in the close callback.
4093 * ops.prepare should be called in the prepare or hw_params callback
4094 * with the proper parameters for set up.
4095 * ops.cleanup should be called in hw_free for clean up of streams.
4097 * This function returns 0 if successful, or a negative error code.
4099 int __devinit snd_hda_build_pcms(struct hda_bus *bus)
4101 struct hda_codec *codec;
4103 list_for_each_entry(codec, &bus->codec_list, list) {
4104 int err = snd_hda_codec_build_pcms(codec);
4110 EXPORT_SYMBOL_HDA(snd_hda_build_pcms);
4113 * snd_hda_check_board_config - compare the current codec with the config table
4114 * @codec: the HDA codec
4115 * @num_configs: number of config enums
4116 * @models: array of model name strings
4117 * @tbl: configuration table, terminated by null entries
4119 * Compares the modelname or PCI subsystem id of the current codec with the
4120 * given configuration table. If a matching entry is found, returns its
4121 * config value (supposed to be 0 or positive).
4123 * If no entries are matching, the function returns a negative value.
4125 int snd_hda_check_board_config(struct hda_codec *codec,
4126 int num_configs, const char * const *models,
4127 const struct snd_pci_quirk *tbl)
4129 if (codec->modelname && models) {
4131 for (i = 0; i < num_configs; i++) {
4133 !strcmp(codec->modelname, models[i])) {
4134 snd_printd(KERN_INFO "hda_codec: model '%s' is "
4135 "selected\n", models[i]);
4141 if (!codec->bus->pci || !tbl)
4144 tbl = snd_pci_quirk_lookup(codec->bus->pci, tbl);
4147 if (tbl->value >= 0 && tbl->value < num_configs) {
4148 #ifdef CONFIG_SND_DEBUG_VERBOSE
4150 const char *model = NULL;
4152 model = models[tbl->value];
4154 sprintf(tmp, "#%d", tbl->value);
4157 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
4158 "for config %x:%x (%s)\n",
4159 model, tbl->subvendor, tbl->subdevice,
4160 (tbl->name ? tbl->name : "Unknown device"));
4166 EXPORT_SYMBOL_HDA(snd_hda_check_board_config);
4169 * snd_hda_check_board_codec_sid_config - compare the current codec
4170 subsystem ID with the
4173 This is important for Gateway notebooks with SB450 HDA Audio
4174 where the vendor ID of the PCI device is:
4175 ATI Technologies Inc SB450 HDA Audio [1002:437b]
4176 and the vendor/subvendor are found only at the codec.
4178 * @codec: the HDA codec
4179 * @num_configs: number of config enums
4180 * @models: array of model name strings
4181 * @tbl: configuration table, terminated by null entries
4183 * Compares the modelname or PCI subsystem id of the current codec with the
4184 * given configuration table. If a matching entry is found, returns its
4185 * config value (supposed to be 0 or positive).
4187 * If no entries are matching, the function returns a negative value.
4189 int snd_hda_check_board_codec_sid_config(struct hda_codec *codec,
4190 int num_configs, const char * const *models,
4191 const struct snd_pci_quirk *tbl)
4193 const struct snd_pci_quirk *q;
4195 /* Search for codec ID */
4196 for (q = tbl; q->subvendor; q++) {
4197 unsigned int mask = 0xffff0000 | q->subdevice_mask;
4198 unsigned int id = (q->subdevice | (q->subvendor << 16)) & mask;
4199 if ((codec->subsystem_id & mask) == id)
4208 if (tbl->value >= 0 && tbl->value < num_configs) {
4209 #ifdef CONFIG_SND_DEBUG_VERBOSE
4211 const char *model = NULL;
4213 model = models[tbl->value];
4215 sprintf(tmp, "#%d", tbl->value);
4218 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
4219 "for config %x:%x (%s)\n",
4220 model, tbl->subvendor, tbl->subdevice,
4221 (tbl->name ? tbl->name : "Unknown device"));
4227 EXPORT_SYMBOL_HDA(snd_hda_check_board_codec_sid_config);
4230 * snd_hda_add_new_ctls - create controls from the array
4231 * @codec: the HDA codec
4232 * @knew: the array of struct snd_kcontrol_new
4234 * This helper function creates and add new controls in the given array.
4235 * The array must be terminated with an empty entry as terminator.
4237 * Returns 0 if successful, or a negative error code.
4239 int snd_hda_add_new_ctls(struct hda_codec *codec,
4240 const struct snd_kcontrol_new *knew)
4244 for (; knew->name; knew++) {
4245 struct snd_kcontrol *kctl;
4246 int addr = 0, idx = 0;
4247 if (knew->iface == -1) /* skip this codec private value */
4250 kctl = snd_ctl_new1(knew, codec);
4254 kctl->id.device = addr;
4256 kctl->id.index = idx;
4257 err = snd_hda_ctl_add(codec, 0, kctl);
4260 /* try first with another device index corresponding to
4261 * the codec addr; if it still fails (or it's the
4262 * primary codec), then try another control index
4264 if (!addr && codec->addr)
4266 else if (!idx && !knew->index) {
4267 idx = find_empty_mixer_ctl_idx(codec,
4277 EXPORT_SYMBOL_HDA(snd_hda_add_new_ctls);
4279 #ifdef CONFIG_SND_HDA_POWER_SAVE
4280 static void hda_power_work(struct work_struct *work)
4282 struct hda_codec *codec =
4283 container_of(work, struct hda_codec, power_work.work);
4284 struct hda_bus *bus = codec->bus;
4286 if (!codec->power_on || codec->power_count) {
4287 codec->power_transition = 0;
4291 trace_hda_power_down(codec);
4292 hda_call_codec_suspend(codec);
4293 if (bus->ops.pm_notify)
4294 bus->ops.pm_notify(bus);
4297 static void hda_keep_power_on(struct hda_codec *codec)
4299 codec->power_count++;
4300 codec->power_on = 1;
4301 codec->power_jiffies = jiffies;
4304 /* update the power on/off account with the current jiffies */
4305 void snd_hda_update_power_acct(struct hda_codec *codec)
4307 unsigned long delta = jiffies - codec->power_jiffies;
4308 if (codec->power_on)
4309 codec->power_on_acct += delta;
4311 codec->power_off_acct += delta;
4312 codec->power_jiffies += delta;
4316 * snd_hda_power_up - Power-up the codec
4317 * @codec: HD-audio codec
4319 * Increment the power-up counter and power up the hardware really when
4320 * not turned on yet.
4322 void snd_hda_power_up(struct hda_codec *codec)
4324 struct hda_bus *bus = codec->bus;
4326 codec->power_count++;
4327 if (codec->power_on || codec->power_transition)
4330 trace_hda_power_up(codec);
4331 snd_hda_update_power_acct(codec);
4332 codec->power_on = 1;
4333 codec->power_jiffies = jiffies;
4334 if (bus->ops.pm_notify)
4335 bus->ops.pm_notify(bus);
4336 hda_call_codec_resume(codec);
4337 cancel_delayed_work(&codec->power_work);
4338 codec->power_transition = 0;
4340 EXPORT_SYMBOL_HDA(snd_hda_power_up);
4342 #define power_save(codec) \
4343 ((codec)->bus->power_save ? *(codec)->bus->power_save : 0)
4346 * snd_hda_power_down - Power-down the codec
4347 * @codec: HD-audio codec
4349 * Decrement the power-up counter and schedules the power-off work if
4350 * the counter rearches to zero.
4352 void snd_hda_power_down(struct hda_codec *codec)
4354 --codec->power_count;
4355 if (!codec->power_on || codec->power_count || codec->power_transition)
4357 if (power_save(codec)) {
4358 codec->power_transition = 1; /* avoid reentrance */
4359 queue_delayed_work(codec->bus->workq, &codec->power_work,
4360 msecs_to_jiffies(power_save(codec) * 1000));
4363 EXPORT_SYMBOL_HDA(snd_hda_power_down);
4366 * snd_hda_check_amp_list_power - Check the amp list and update the power
4367 * @codec: HD-audio codec
4368 * @check: the object containing an AMP list and the status
4369 * @nid: NID to check / update
4371 * Check whether the given NID is in the amp list. If it's in the list,
4372 * check the current AMP status, and update the the power-status according
4373 * to the mute status.
4375 * This function is supposed to be set or called from the check_power_status
4378 int snd_hda_check_amp_list_power(struct hda_codec *codec,
4379 struct hda_loopback_check *check,
4382 const struct hda_amp_list *p;
4385 if (!check->amplist)
4387 for (p = check->amplist; p->nid; p++) {
4392 return 0; /* nothing changed */
4394 for (p = check->amplist; p->nid; p++) {
4395 for (ch = 0; ch < 2; ch++) {
4396 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
4398 if (!(v & HDA_AMP_MUTE) && v > 0) {
4399 if (!check->power_on) {
4400 check->power_on = 1;
4401 snd_hda_power_up(codec);
4407 if (check->power_on) {
4408 check->power_on = 0;
4409 snd_hda_power_down(codec);
4413 EXPORT_SYMBOL_HDA(snd_hda_check_amp_list_power);
4417 * Channel mode helper
4421 * snd_hda_ch_mode_info - Info callback helper for the channel mode enum
4423 int snd_hda_ch_mode_info(struct hda_codec *codec,
4424 struct snd_ctl_elem_info *uinfo,
4425 const struct hda_channel_mode *chmode,
4428 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4430 uinfo->value.enumerated.items = num_chmodes;
4431 if (uinfo->value.enumerated.item >= num_chmodes)
4432 uinfo->value.enumerated.item = num_chmodes - 1;
4433 sprintf(uinfo->value.enumerated.name, "%dch",
4434 chmode[uinfo->value.enumerated.item].channels);
4437 EXPORT_SYMBOL_HDA(snd_hda_ch_mode_info);
4440 * snd_hda_ch_mode_get - Get callback helper for the channel mode enum
4442 int snd_hda_ch_mode_get(struct hda_codec *codec,
4443 struct snd_ctl_elem_value *ucontrol,
4444 const struct hda_channel_mode *chmode,
4450 for (i = 0; i < num_chmodes; i++) {
4451 if (max_channels == chmode[i].channels) {
4452 ucontrol->value.enumerated.item[0] = i;
4458 EXPORT_SYMBOL_HDA(snd_hda_ch_mode_get);
4461 * snd_hda_ch_mode_put - Put callback helper for the channel mode enum
4463 int snd_hda_ch_mode_put(struct hda_codec *codec,
4464 struct snd_ctl_elem_value *ucontrol,
4465 const struct hda_channel_mode *chmode,
4471 mode = ucontrol->value.enumerated.item[0];
4472 if (mode >= num_chmodes)
4474 if (*max_channelsp == chmode[mode].channels)
4476 /* change the current channel setting */
4477 *max_channelsp = chmode[mode].channels;
4478 if (chmode[mode].sequence)
4479 snd_hda_sequence_write_cache(codec, chmode[mode].sequence);
4482 EXPORT_SYMBOL_HDA(snd_hda_ch_mode_put);
4489 * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum
4491 int snd_hda_input_mux_info(const struct hda_input_mux *imux,
4492 struct snd_ctl_elem_info *uinfo)
4496 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4498 uinfo->value.enumerated.items = imux->num_items;
4499 if (!imux->num_items)
4501 index = uinfo->value.enumerated.item;
4502 if (index >= imux->num_items)
4503 index = imux->num_items - 1;
4504 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
4507 EXPORT_SYMBOL_HDA(snd_hda_input_mux_info);
4510 * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum
4512 int snd_hda_input_mux_put(struct hda_codec *codec,
4513 const struct hda_input_mux *imux,
4514 struct snd_ctl_elem_value *ucontrol,
4516 unsigned int *cur_val)
4520 if (!imux->num_items)
4522 idx = ucontrol->value.enumerated.item[0];
4523 if (idx >= imux->num_items)
4524 idx = imux->num_items - 1;
4525 if (*cur_val == idx)
4527 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
4528 imux->items[idx].index);
4532 EXPORT_SYMBOL_HDA(snd_hda_input_mux_put);
4536 * Multi-channel / digital-out PCM helper functions
4539 /* setup SPDIF output stream */
4540 static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
4541 unsigned int stream_tag, unsigned int format)
4543 struct hda_spdif_out *spdif = snd_hda_spdif_out_of_nid(codec, nid);
4545 /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */
4546 if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE))
4547 set_dig_out_convert(codec, nid,
4548 spdif->ctls & ~AC_DIG1_ENABLE & 0xff,
4550 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
4551 if (codec->slave_dig_outs) {
4553 for (d = codec->slave_dig_outs; *d; d++)
4554 snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
4557 /* turn on again (if needed) */
4558 if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE))
4559 set_dig_out_convert(codec, nid,
4560 spdif->ctls & 0xff, -1);
4563 static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
4565 snd_hda_codec_cleanup_stream(codec, nid);
4566 if (codec->slave_dig_outs) {
4568 for (d = codec->slave_dig_outs; *d; d++)
4569 snd_hda_codec_cleanup_stream(codec, *d);
4574 * snd_hda_bus_reboot_notify - call the reboot notifier of each codec
4575 * @bus: HD-audio bus
4577 void snd_hda_bus_reboot_notify(struct hda_bus *bus)
4579 struct hda_codec *codec;
4583 list_for_each_entry(codec, &bus->codec_list, list) {
4584 if (hda_codec_is_power_on(codec) &&
4585 codec->patch_ops.reboot_notify)
4586 codec->patch_ops.reboot_notify(codec);
4589 EXPORT_SYMBOL_HDA(snd_hda_bus_reboot_notify);
4592 * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode
4594 int snd_hda_multi_out_dig_open(struct hda_codec *codec,
4595 struct hda_multi_out *mout)
4597 mutex_lock(&codec->spdif_mutex);
4598 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
4599 /* already opened as analog dup; reset it once */
4600 cleanup_dig_out_stream(codec, mout->dig_out_nid);
4601 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
4602 mutex_unlock(&codec->spdif_mutex);
4605 EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_open);
4608 * snd_hda_multi_out_dig_prepare - prepare the digital out stream
4610 int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
4611 struct hda_multi_out *mout,
4612 unsigned int stream_tag,
4613 unsigned int format,
4614 struct snd_pcm_substream *substream)
4616 mutex_lock(&codec->spdif_mutex);
4617 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
4618 mutex_unlock(&codec->spdif_mutex);
4621 EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_prepare);
4624 * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream
4626 int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec,
4627 struct hda_multi_out *mout)
4629 mutex_lock(&codec->spdif_mutex);
4630 cleanup_dig_out_stream(codec, mout->dig_out_nid);
4631 mutex_unlock(&codec->spdif_mutex);
4634 EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_cleanup);
4637 * snd_hda_multi_out_dig_close - release the digital out stream
4639 int snd_hda_multi_out_dig_close(struct hda_codec *codec,
4640 struct hda_multi_out *mout)
4642 mutex_lock(&codec->spdif_mutex);
4643 mout->dig_out_used = 0;
4644 mutex_unlock(&codec->spdif_mutex);
4647 EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_close);
4650 * snd_hda_multi_out_analog_open - open analog outputs
4652 * Open analog outputs and set up the hw-constraints.
4653 * If the digital outputs can be opened as slave, open the digital
4656 int snd_hda_multi_out_analog_open(struct hda_codec *codec,
4657 struct hda_multi_out *mout,
4658 struct snd_pcm_substream *substream,
4659 struct hda_pcm_stream *hinfo)
4661 struct snd_pcm_runtime *runtime = substream->runtime;
4662 runtime->hw.channels_max = mout->max_channels;
4663 if (mout->dig_out_nid) {
4664 if (!mout->analog_rates) {
4665 mout->analog_rates = hinfo->rates;
4666 mout->analog_formats = hinfo->formats;
4667 mout->analog_maxbps = hinfo->maxbps;
4669 runtime->hw.rates = mout->analog_rates;
4670 runtime->hw.formats = mout->analog_formats;
4671 hinfo->maxbps = mout->analog_maxbps;
4673 if (!mout->spdif_rates) {
4674 snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
4676 &mout->spdif_formats,
4677 &mout->spdif_maxbps);
4679 mutex_lock(&codec->spdif_mutex);
4680 if (mout->share_spdif) {
4681 if ((runtime->hw.rates & mout->spdif_rates) &&
4682 (runtime->hw.formats & mout->spdif_formats)) {
4683 runtime->hw.rates &= mout->spdif_rates;
4684 runtime->hw.formats &= mout->spdif_formats;
4685 if (mout->spdif_maxbps < hinfo->maxbps)
4686 hinfo->maxbps = mout->spdif_maxbps;
4688 mout->share_spdif = 0;
4689 /* FIXME: need notify? */
4692 mutex_unlock(&codec->spdif_mutex);
4694 return snd_pcm_hw_constraint_step(substream->runtime, 0,
4695 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
4697 EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_open);
4700 * snd_hda_multi_out_analog_prepare - Preapre the analog outputs.
4702 * Set up the i/o for analog out.
4703 * When the digital out is available, copy the front out to digital out, too.
4705 int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
4706 struct hda_multi_out *mout,
4707 unsigned int stream_tag,
4708 unsigned int format,
4709 struct snd_pcm_substream *substream)
4711 const hda_nid_t *nids = mout->dac_nids;
4712 int chs = substream->runtime->channels;
4713 struct hda_spdif_out *spdif =
4714 snd_hda_spdif_out_of_nid(codec, mout->dig_out_nid);
4717 mutex_lock(&codec->spdif_mutex);
4718 if (mout->dig_out_nid && mout->share_spdif &&
4719 mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
4721 snd_hda_is_supported_format(codec, mout->dig_out_nid,
4723 !(spdif->status & IEC958_AES0_NONAUDIO)) {
4724 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
4725 setup_dig_out_stream(codec, mout->dig_out_nid,
4726 stream_tag, format);
4728 mout->dig_out_used = 0;
4729 cleanup_dig_out_stream(codec, mout->dig_out_nid);
4732 mutex_unlock(&codec->spdif_mutex);
4735 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
4737 if (!mout->no_share_stream &&
4738 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
4739 /* headphone out will just decode front left/right (stereo) */
4740 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
4742 /* extra outputs copied from front */
4743 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
4744 if (!mout->no_share_stream && mout->hp_out_nid[i])
4745 snd_hda_codec_setup_stream(codec,
4746 mout->hp_out_nid[i],
4747 stream_tag, 0, format);
4748 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
4749 if (!mout->no_share_stream && mout->extra_out_nid[i])
4750 snd_hda_codec_setup_stream(codec,
4751 mout->extra_out_nid[i],
4752 stream_tag, 0, format);
4755 for (i = 1; i < mout->num_dacs; i++) {
4756 if (chs >= (i + 1) * 2) /* independent out */
4757 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
4759 else if (!mout->no_share_stream) /* copy front */
4760 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
4765 EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_prepare);
4768 * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out
4770 int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
4771 struct hda_multi_out *mout)
4773 const hda_nid_t *nids = mout->dac_nids;
4776 for (i = 0; i < mout->num_dacs; i++)
4777 snd_hda_codec_cleanup_stream(codec, nids[i]);
4779 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
4780 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
4781 if (mout->hp_out_nid[i])
4782 snd_hda_codec_cleanup_stream(codec,
4783 mout->hp_out_nid[i]);
4784 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
4785 if (mout->extra_out_nid[i])
4786 snd_hda_codec_cleanup_stream(codec,
4787 mout->extra_out_nid[i]);
4788 mutex_lock(&codec->spdif_mutex);
4789 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
4790 cleanup_dig_out_stream(codec, mout->dig_out_nid);
4791 mout->dig_out_used = 0;
4793 mutex_unlock(&codec->spdif_mutex);
4796 EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_cleanup);
4799 * Helper for automatic pin configuration
4802 static int is_in_nid_list(hda_nid_t nid, const hda_nid_t *list)
4804 for (; *list; list++)
4812 * Sort an associated group of pins according to their sequence numbers.
4814 static void sort_pins_by_sequence(hda_nid_t *pins, short *sequences,
4821 for (i = 0; i < num_pins; i++) {
4822 for (j = i + 1; j < num_pins; j++) {
4823 if (sequences[i] > sequences[j]) {
4825 sequences[i] = sequences[j];
4836 /* add the found input-pin to the cfg->inputs[] table */
4837 static void add_auto_cfg_input_pin(struct auto_pin_cfg *cfg, hda_nid_t nid,
4840 if (cfg->num_inputs < AUTO_CFG_MAX_INS) {
4841 cfg->inputs[cfg->num_inputs].pin = nid;
4842 cfg->inputs[cfg->num_inputs].type = type;
4847 /* sort inputs in the order of AUTO_PIN_* type */
4848 static void sort_autocfg_input_pins(struct auto_pin_cfg *cfg)
4852 for (i = 0; i < cfg->num_inputs; i++) {
4853 for (j = i + 1; j < cfg->num_inputs; j++) {
4854 if (cfg->inputs[i].type > cfg->inputs[j].type) {
4855 struct auto_pin_cfg_item tmp;
4856 tmp = cfg->inputs[i];
4857 cfg->inputs[i] = cfg->inputs[j];
4858 cfg->inputs[j] = tmp;
4864 /* Reorder the surround channels
4865 * ALSA sequence is front/surr/clfe/side
4867 * 4-ch: front/surr => OK as it is
4868 * 6-ch: front/clfe/surr
4869 * 8-ch: front/clfe/rear/side|fc
4871 static void reorder_outputs(unsigned int nums, hda_nid_t *pins)
4886 * Parse all pin widgets and store the useful pin nids to cfg
4888 * The number of line-outs or any primary output is stored in line_outs,
4889 * and the corresponding output pins are assigned to line_out_pins[],
4890 * in the order of front, rear, CLFE, side, ...
4892 * If more extra outputs (speaker and headphone) are found, the pins are
4893 * assisnged to hp_pins[] and speaker_pins[], respectively. If no line-out jack
4894 * is detected, one of speaker of HP pins is assigned as the primary
4895 * output, i.e. to line_out_pins[0]. So, line_outs is always positive
4896 * if any analog output exists.
4898 * The analog input pins are assigned to inputs array.
4899 * The digital input/output pins are assigned to dig_in_pin and dig_out_pin,
4902 int snd_hda_parse_pin_defcfg(struct hda_codec *codec,
4903 struct auto_pin_cfg *cfg,
4904 const hda_nid_t *ignore_nids,
4905 unsigned int cond_flags)
4907 hda_nid_t nid, end_nid;
4908 short seq, assoc_line_out;
4909 short sequences_line_out[ARRAY_SIZE(cfg->line_out_pins)];
4910 short sequences_speaker[ARRAY_SIZE(cfg->speaker_pins)];
4911 short sequences_hp[ARRAY_SIZE(cfg->hp_pins)];
4914 memset(cfg, 0, sizeof(*cfg));
4916 memset(sequences_line_out, 0, sizeof(sequences_line_out));
4917 memset(sequences_speaker, 0, sizeof(sequences_speaker));
4918 memset(sequences_hp, 0, sizeof(sequences_hp));
4921 codec->ignore_misc_bit = true;
4922 end_nid = codec->start_nid + codec->num_nodes;
4923 for (nid = codec->start_nid; nid < end_nid; nid++) {
4924 unsigned int wid_caps = get_wcaps(codec, nid);
4925 unsigned int wid_type = get_wcaps_type(wid_caps);
4926 unsigned int def_conf;
4927 short assoc, loc, conn, dev;
4929 /* read all default configuration for pin complex */
4930 if (wid_type != AC_WID_PIN)
4932 /* ignore the given nids (e.g. pc-beep returns error) */
4933 if (ignore_nids && is_in_nid_list(nid, ignore_nids))
4936 def_conf = snd_hda_codec_get_pincfg(codec, nid);
4937 if (!(get_defcfg_misc(snd_hda_codec_get_pincfg(codec, nid)) &
4938 AC_DEFCFG_MISC_NO_PRESENCE))
4939 codec->ignore_misc_bit = false;
4940 conn = get_defcfg_connect(def_conf);
4941 if (conn == AC_JACK_PORT_NONE)
4943 loc = get_defcfg_location(def_conf);
4944 dev = get_defcfg_device(def_conf);
4946 /* workaround for buggy BIOS setups */
4947 if (dev == AC_JACK_LINE_OUT) {
4948 if (conn == AC_JACK_PORT_FIXED)
4949 dev = AC_JACK_SPEAKER;
4953 case AC_JACK_LINE_OUT:
4954 seq = get_defcfg_sequence(def_conf);
4955 assoc = get_defcfg_association(def_conf);
4957 if (!(wid_caps & AC_WCAP_STEREO))
4958 if (!cfg->mono_out_pin)
4959 cfg->mono_out_pin = nid;
4962 if (!assoc_line_out)
4963 assoc_line_out = assoc;
4964 else if (assoc_line_out != assoc)
4966 if (cfg->line_outs >= ARRAY_SIZE(cfg->line_out_pins))
4968 cfg->line_out_pins[cfg->line_outs] = nid;
4969 sequences_line_out[cfg->line_outs] = seq;
4972 case AC_JACK_SPEAKER:
4973 seq = get_defcfg_sequence(def_conf);
4974 assoc = get_defcfg_association(def_conf);
4975 if (cfg->speaker_outs >= ARRAY_SIZE(cfg->speaker_pins))
4977 cfg->speaker_pins[cfg->speaker_outs] = nid;
4978 sequences_speaker[cfg->speaker_outs] = (assoc << 4) | seq;
4979 cfg->speaker_outs++;
4981 case AC_JACK_HP_OUT:
4982 seq = get_defcfg_sequence(def_conf);
4983 assoc = get_defcfg_association(def_conf);
4984 if (cfg->hp_outs >= ARRAY_SIZE(cfg->hp_pins))
4986 cfg->hp_pins[cfg->hp_outs] = nid;
4987 sequences_hp[cfg->hp_outs] = (assoc << 4) | seq;
4990 case AC_JACK_MIC_IN:
4991 add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_MIC);
4993 case AC_JACK_LINE_IN:
4994 add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_LINE_IN);
4997 add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_CD);
5000 add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_AUX);
5002 case AC_JACK_SPDIF_OUT:
5003 case AC_JACK_DIG_OTHER_OUT:
5004 if (cfg->dig_outs >= ARRAY_SIZE(cfg->dig_out_pins))
5006 cfg->dig_out_pins[cfg->dig_outs] = nid;
5007 cfg->dig_out_type[cfg->dig_outs] =
5008 (loc == AC_JACK_LOC_HDMI) ?
5009 HDA_PCM_TYPE_HDMI : HDA_PCM_TYPE_SPDIF;
5012 case AC_JACK_SPDIF_IN:
5013 case AC_JACK_DIG_OTHER_IN:
5014 cfg->dig_in_pin = nid;
5015 if (loc == AC_JACK_LOC_HDMI)
5016 cfg->dig_in_type = HDA_PCM_TYPE_HDMI;
5018 cfg->dig_in_type = HDA_PCM_TYPE_SPDIF;
5024 * If no line-out is defined but multiple HPs are found,
5025 * some of them might be the real line-outs.
5027 if (!cfg->line_outs && cfg->hp_outs > 1 &&
5028 !(cond_flags & HDA_PINCFG_NO_HP_FIXUP)) {
5030 while (i < cfg->hp_outs) {
5031 /* The real HPs should have the sequence 0x0f */
5032 if ((sequences_hp[i] & 0x0f) == 0x0f) {
5036 /* Move it to the line-out table */
5037 cfg->line_out_pins[cfg->line_outs] = cfg->hp_pins[i];
5038 sequences_line_out[cfg->line_outs] = sequences_hp[i];
5041 memmove(cfg->hp_pins + i, cfg->hp_pins + i + 1,
5042 sizeof(cfg->hp_pins[0]) * (cfg->hp_outs - i));
5043 memmove(sequences_hp + i, sequences_hp + i + 1,
5044 sizeof(sequences_hp[0]) * (cfg->hp_outs - i));
5046 memset(cfg->hp_pins + cfg->hp_outs, 0,
5047 sizeof(hda_nid_t) * (AUTO_CFG_MAX_OUTS - cfg->hp_outs));
5049 cfg->line_out_type = AUTO_PIN_HP_OUT;
5053 /* sort by sequence */
5054 sort_pins_by_sequence(cfg->line_out_pins, sequences_line_out,
5056 sort_pins_by_sequence(cfg->speaker_pins, sequences_speaker,
5058 sort_pins_by_sequence(cfg->hp_pins, sequences_hp,
5062 * FIX-UP: if no line-outs are detected, try to use speaker or HP pin
5063 * as a primary output
5065 if (!cfg->line_outs &&
5066 !(cond_flags & HDA_PINCFG_NO_LO_FIXUP)) {
5067 if (cfg->speaker_outs) {
5068 cfg->line_outs = cfg->speaker_outs;
5069 memcpy(cfg->line_out_pins, cfg->speaker_pins,
5070 sizeof(cfg->speaker_pins));
5071 cfg->speaker_outs = 0;
5072 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
5073 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
5074 } else if (cfg->hp_outs) {
5075 cfg->line_outs = cfg->hp_outs;
5076 memcpy(cfg->line_out_pins, cfg->hp_pins,
5077 sizeof(cfg->hp_pins));
5079 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
5080 cfg->line_out_type = AUTO_PIN_HP_OUT;
5084 reorder_outputs(cfg->line_outs, cfg->line_out_pins);
5085 reorder_outputs(cfg->hp_outs, cfg->hp_pins);
5086 reorder_outputs(cfg->speaker_outs, cfg->speaker_pins);
5088 sort_autocfg_input_pins(cfg);
5091 * debug prints of the parsed results
5093 snd_printd("autoconfig: line_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x) type:%s\n",
5094 cfg->line_outs, cfg->line_out_pins[0], cfg->line_out_pins[1],
5095 cfg->line_out_pins[2], cfg->line_out_pins[3],
5096 cfg->line_out_pins[4],
5097 cfg->line_out_type == AUTO_PIN_HP_OUT ? "hp" :
5098 (cfg->line_out_type == AUTO_PIN_SPEAKER_OUT ?
5099 "speaker" : "line"));
5100 snd_printd(" speaker_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
5101 cfg->speaker_outs, cfg->speaker_pins[0],
5102 cfg->speaker_pins[1], cfg->speaker_pins[2],
5103 cfg->speaker_pins[3], cfg->speaker_pins[4]);
5104 snd_printd(" hp_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
5105 cfg->hp_outs, cfg->hp_pins[0],
5106 cfg->hp_pins[1], cfg->hp_pins[2],
5107 cfg->hp_pins[3], cfg->hp_pins[4]);
5108 snd_printd(" mono: mono_out=0x%x\n", cfg->mono_out_pin);
5110 snd_printd(" dig-out=0x%x/0x%x\n",
5111 cfg->dig_out_pins[0], cfg->dig_out_pins[1]);
5112 snd_printd(" inputs:");
5113 for (i = 0; i < cfg->num_inputs; i++) {
5114 snd_printd(" %s=0x%x",
5115 hda_get_autocfg_input_label(codec, cfg, i),
5116 cfg->inputs[i].pin);
5119 if (cfg->dig_in_pin)
5120 snd_printd(" dig-in=0x%x\n", cfg->dig_in_pin);
5124 EXPORT_SYMBOL_HDA(snd_hda_parse_pin_defcfg);
5126 int snd_hda_get_input_pin_attr(unsigned int def_conf)
5128 unsigned int loc = get_defcfg_location(def_conf);
5129 unsigned int conn = get_defcfg_connect(def_conf);
5130 if (conn == AC_JACK_PORT_NONE)
5131 return INPUT_PIN_ATTR_UNUSED;
5132 /* Windows may claim the internal mic to be BOTH, too */
5133 if (conn == AC_JACK_PORT_FIXED || conn == AC_JACK_PORT_BOTH)
5134 return INPUT_PIN_ATTR_INT;
5135 if ((loc & 0x30) == AC_JACK_LOC_INTERNAL)
5136 return INPUT_PIN_ATTR_INT;
5137 if ((loc & 0x30) == AC_JACK_LOC_SEPARATE)
5138 return INPUT_PIN_ATTR_DOCK;
5139 if (loc == AC_JACK_LOC_REAR)
5140 return INPUT_PIN_ATTR_REAR;
5141 if (loc == AC_JACK_LOC_FRONT)
5142 return INPUT_PIN_ATTR_FRONT;
5143 return INPUT_PIN_ATTR_NORMAL;
5145 EXPORT_SYMBOL_HDA(snd_hda_get_input_pin_attr);
5148 * hda_get_input_pin_label - Give a label for the given input pin
5150 * When check_location is true, the function checks the pin location
5151 * for mic and line-in pins, and set an appropriate prefix like "Front",
5152 * "Rear", "Internal".
5155 static const char *hda_get_input_pin_label(struct hda_codec *codec,
5156 hda_nid_t pin, bool check_location)
5158 unsigned int def_conf;
5159 static const char * const mic_names[] = {
5160 "Internal Mic", "Dock Mic", "Mic", "Front Mic", "Rear Mic",
5164 def_conf = snd_hda_codec_get_pincfg(codec, pin);
5166 switch (get_defcfg_device(def_conf)) {
5167 case AC_JACK_MIC_IN:
5168 if (!check_location)
5170 attr = snd_hda_get_input_pin_attr(def_conf);
5173 return mic_names[attr - 1];
5174 case AC_JACK_LINE_IN:
5175 if (!check_location)
5177 attr = snd_hda_get_input_pin_attr(def_conf);
5180 if (attr == INPUT_PIN_ATTR_DOCK)
5187 case AC_JACK_SPDIF_IN:
5189 case AC_JACK_DIG_OTHER_IN:
5190 return "Digital In";
5196 /* Check whether the location prefix needs to be added to the label.
5197 * If all mic-jacks are in the same location (e.g. rear panel), we don't
5198 * have to put "Front" prefix to each label. In such a case, returns false.
5200 static int check_mic_location_need(struct hda_codec *codec,
5201 const struct auto_pin_cfg *cfg,
5207 defc = snd_hda_codec_get_pincfg(codec, cfg->inputs[input].pin);
5208 attr = snd_hda_get_input_pin_attr(defc);
5209 /* for internal or docking mics, we need locations */
5210 if (attr <= INPUT_PIN_ATTR_NORMAL)
5214 for (i = 0; i < cfg->num_inputs; i++) {
5215 defc = snd_hda_codec_get_pincfg(codec, cfg->inputs[i].pin);
5216 attr2 = snd_hda_get_input_pin_attr(defc);
5217 if (attr2 >= INPUT_PIN_ATTR_NORMAL) {
5218 if (attr && attr != attr2)
5219 return 1; /* different locations found */
5227 * hda_get_autocfg_input_label - Get a label for the given input
5229 * Get a label for the given input pin defined by the autocfg item.
5230 * Unlike hda_get_input_pin_label(), this function checks all inputs
5231 * defined in autocfg and avoids the redundant mic/line prefix as much as
5234 const char *hda_get_autocfg_input_label(struct hda_codec *codec,
5235 const struct auto_pin_cfg *cfg,
5238 int type = cfg->inputs[input].type;
5239 int has_multiple_pins = 0;
5241 if ((input > 0 && cfg->inputs[input - 1].type == type) ||
5242 (input < cfg->num_inputs - 1 && cfg->inputs[input + 1].type == type))
5243 has_multiple_pins = 1;
5244 if (has_multiple_pins && type == AUTO_PIN_MIC)
5245 has_multiple_pins &= check_mic_location_need(codec, cfg, input);
5246 return hda_get_input_pin_label(codec, cfg->inputs[input].pin,
5249 EXPORT_SYMBOL_HDA(hda_get_autocfg_input_label);
5251 /* return the position of NID in the list, or -1 if not found */
5252 static int find_idx_in_nid_list(hda_nid_t nid, const hda_nid_t *list, int nums)
5255 for (i = 0; i < nums; i++)
5261 /* get a unique suffix or an index number */
5262 static const char *check_output_sfx(hda_nid_t nid, const hda_nid_t *pins,
5263 int num_pins, int *indexp)
5265 static const char * const channel_sfx[] = {
5266 " Front", " Surround", " CLFE", " Side"
5270 i = find_idx_in_nid_list(nid, pins, num_pins);
5275 if (num_pins > ARRAY_SIZE(channel_sfx)) {
5280 return channel_sfx[i];
5283 static int fill_audio_out_name(struct hda_codec *codec, hda_nid_t nid,
5284 const struct auto_pin_cfg *cfg,
5285 const char *name, char *label, int maxlen,
5288 unsigned int def_conf = snd_hda_codec_get_pincfg(codec, nid);
5289 int attr = snd_hda_get_input_pin_attr(def_conf);
5290 const char *pfx = "", *sfx = "";
5292 /* handle as a speaker if it's a fixed line-out */
5293 if (!strcmp(name, "Line Out") && attr == INPUT_PIN_ATTR_INT)
5295 /* check the location */
5297 case INPUT_PIN_ATTR_DOCK:
5300 case INPUT_PIN_ATTR_FRONT:
5305 /* try to give a unique suffix if needed */
5306 sfx = check_output_sfx(nid, cfg->line_out_pins, cfg->line_outs,
5309 sfx = check_output_sfx(nid, cfg->speaker_pins, cfg->speaker_outs,
5312 /* don't add channel suffix for Headphone controls */
5313 int idx = find_idx_in_nid_list(nid, cfg->hp_pins,
5320 snprintf(label, maxlen, "%s%s%s", pfx, name, sfx);
5325 * snd_hda_get_pin_label - Get a label for the given I/O pin
5327 * Get a label for the given pin. This function works for both input and
5328 * output pins. When @cfg is given as non-NULL, the function tries to get
5329 * an optimized label using hda_get_autocfg_input_label().
5331 * This function tries to give a unique label string for the pin as much as
5332 * possible. For example, when the multiple line-outs are present, it adds
5333 * the channel suffix like "Front", "Surround", etc (only when @cfg is given).
5334 * If no unique name with a suffix is available and @indexp is non-NULL, the
5335 * index number is stored in the pointer.
5337 int snd_hda_get_pin_label(struct hda_codec *codec, hda_nid_t nid,
5338 const struct auto_pin_cfg *cfg,
5339 char *label, int maxlen, int *indexp)
5341 unsigned int def_conf = snd_hda_codec_get_pincfg(codec, nid);
5342 const char *name = NULL;
5347 if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE)
5350 switch (get_defcfg_device(def_conf)) {
5351 case AC_JACK_LINE_OUT:
5352 return fill_audio_out_name(codec, nid, cfg, "Line Out",
5353 label, maxlen, indexp);
5354 case AC_JACK_SPEAKER:
5355 return fill_audio_out_name(codec, nid, cfg, "Speaker",
5356 label, maxlen, indexp);
5357 case AC_JACK_HP_OUT:
5358 return fill_audio_out_name(codec, nid, cfg, "Headphone",
5359 label, maxlen, indexp);
5360 case AC_JACK_SPDIF_OUT:
5361 case AC_JACK_DIG_OTHER_OUT:
5362 if (get_defcfg_location(def_conf) == AC_JACK_LOC_HDMI)
5366 if (cfg && indexp) {
5367 i = find_idx_in_nid_list(nid, cfg->dig_out_pins,
5375 for (i = 0; i < cfg->num_inputs; i++) {
5376 if (cfg->inputs[i].pin != nid)
5378 name = hda_get_autocfg_input_label(codec, cfg, i);
5384 name = hda_get_input_pin_label(codec, nid, true);
5389 strlcpy(label, name, maxlen);
5392 EXPORT_SYMBOL_HDA(snd_hda_get_pin_label);
5395 * snd_hda_add_imux_item - Add an item to input_mux
5397 * When the same label is used already in the existing items, the number
5398 * suffix is appended to the label. This label index number is stored
5399 * to type_idx when non-NULL pointer is given.
5401 int snd_hda_add_imux_item(struct hda_input_mux *imux, const char *label,
5402 int index, int *type_idx)
5404 int i, label_idx = 0;
5405 if (imux->num_items >= HDA_MAX_NUM_INPUTS) {
5406 snd_printd(KERN_ERR "hda_codec: Too many imux items!\n");
5409 for (i = 0; i < imux->num_items; i++) {
5410 if (!strncmp(label, imux->items[i].label, strlen(label)))
5414 *type_idx = label_idx;
5416 snprintf(imux->items[imux->num_items].label,
5417 sizeof(imux->items[imux->num_items].label),
5418 "%s %d", label, label_idx);
5420 strlcpy(imux->items[imux->num_items].label, label,
5421 sizeof(imux->items[imux->num_items].label));
5422 imux->items[imux->num_items].index = index;
5426 EXPORT_SYMBOL_HDA(snd_hda_add_imux_item);
5435 * snd_hda_suspend - suspend the codecs
5438 * Returns 0 if successful.
5440 int snd_hda_suspend(struct hda_bus *bus)
5442 struct hda_codec *codec;
5444 list_for_each_entry(codec, &bus->codec_list, list) {
5445 if (hda_codec_is_power_on(codec))
5446 hda_call_codec_suspend(codec);
5447 else /* forcibly change the power to D3 even if not used */
5448 hda_set_power_state(codec,
5449 codec->afg ? codec->afg : codec->mfg,
5451 if (codec->patch_ops.post_suspend)
5452 codec->patch_ops.post_suspend(codec);
5456 EXPORT_SYMBOL_HDA(snd_hda_suspend);
5459 * snd_hda_resume - resume the codecs
5462 * Returns 0 if successful.
5464 * This function is defined only when POWER_SAVE isn't set.
5465 * In the power-save mode, the codec is resumed dynamically.
5467 int snd_hda_resume(struct hda_bus *bus)
5469 struct hda_codec *codec;
5471 list_for_each_entry(codec, &bus->codec_list, list) {
5472 if (codec->patch_ops.pre_resume)
5473 codec->patch_ops.pre_resume(codec);
5474 if (snd_hda_codec_needs_resume(codec))
5475 hda_call_codec_resume(codec);
5479 EXPORT_SYMBOL_HDA(snd_hda_resume);
5480 #endif /* CONFIG_PM */
5487 * snd_array_new - get a new element from the given array
5488 * @array: the array object
5490 * Get a new element from the given array. If it exceeds the
5491 * pre-allocated array size, re-allocate the array.
5493 * Returns NULL if allocation failed.
5495 void *snd_array_new(struct snd_array *array)
5497 if (array->used >= array->alloced) {
5498 int num = array->alloced + array->alloc_align;
5499 int size = (num + 1) * array->elem_size;
5500 int oldsize = array->alloced * array->elem_size;
5502 if (snd_BUG_ON(num >= 4096))
5504 nlist = krealloc(array->list, size, GFP_KERNEL);
5507 memset(nlist + oldsize, 0, size - oldsize);
5508 array->list = nlist;
5509 array->alloced = num;
5511 return snd_array_elem(array, array->used++);
5513 EXPORT_SYMBOL_HDA(snd_array_new);
5516 * snd_array_free - free the given array elements
5517 * @array: the array object
5519 void snd_array_free(struct snd_array *array)
5526 EXPORT_SYMBOL_HDA(snd_array_free);
5529 * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer
5530 * @pcm: PCM caps bits
5531 * @buf: the string buffer to write
5532 * @buflen: the max buffer length
5534 * used by hda_proc.c and hda_eld.c
5536 void snd_print_pcm_bits(int pcm, char *buf, int buflen)
5538 static unsigned int bits[] = { 8, 16, 20, 24, 32 };
5541 for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
5542 if (pcm & (AC_SUPPCM_BITS_8 << i))
5543 j += snprintf(buf + j, buflen - j, " %d", bits[i]);
5545 buf[j] = '\0'; /* necessary when j == 0 */
5547 EXPORT_SYMBOL_HDA(snd_print_pcm_bits);
5549 MODULE_DESCRIPTION("HDA codec core");
5550 MODULE_LICENSE("GPL");