2 * Universal Interface for Intel High Definition Audio Codec
4 * Copyright (c) 2004 Takashi Iwai <tiwai@suse.de>
7 * This driver is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This driver is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include <linux/init.h>
23 #include <linux/delay.h>
24 #include <linux/slab.h>
25 #include <linux/pci.h>
26 #include <linux/mutex.h>
27 #include <linux/module.h>
28 #include <sound/core.h>
29 #include "hda_codec.h"
30 #include <sound/asoundef.h>
31 #include <sound/tlv.h>
32 #include <sound/initval.h>
33 #include <sound/jack.h>
34 #include "hda_local.h"
37 #include <sound/hda_hwdep.h>
39 #define CREATE_TRACE_POINTS
40 #include "hda_trace.h"
43 * vendor / preset table
46 struct hda_vendor_id {
51 /* codec vendor labels */
52 static struct hda_vendor_id hda_vendor_ids[] = {
54 { 0x1013, "Cirrus Logic" },
55 { 0x1057, "Motorola" },
56 { 0x1095, "Silicon Image" },
58 { 0x10ec, "Realtek" },
59 { 0x1102, "Creative" },
63 { 0x11d4, "Analog Devices" },
64 { 0x13f6, "C-Media" },
65 { 0x14f1, "Conexant" },
66 { 0x17e8, "Chrontel" },
68 { 0x1aec, "Wolfson Microelectronics" },
69 { 0x434d, "C-Media" },
71 { 0x8384, "SigmaTel" },
75 static DEFINE_MUTEX(preset_mutex);
76 static LIST_HEAD(hda_preset_tables);
78 int snd_hda_add_codec_preset(struct hda_codec_preset_list *preset)
80 mutex_lock(&preset_mutex);
81 list_add_tail(&preset->list, &hda_preset_tables);
82 mutex_unlock(&preset_mutex);
85 EXPORT_SYMBOL_HDA(snd_hda_add_codec_preset);
87 int snd_hda_delete_codec_preset(struct hda_codec_preset_list *preset)
89 mutex_lock(&preset_mutex);
90 list_del(&preset->list);
91 mutex_unlock(&preset_mutex);
94 EXPORT_SYMBOL_HDA(snd_hda_delete_codec_preset);
96 #ifdef CONFIG_SND_HDA_POWER_SAVE
97 static void hda_power_work(struct work_struct *work);
98 static void hda_keep_power_on(struct hda_codec *codec);
99 #define hda_codec_is_power_on(codec) ((codec)->power_on)
101 static inline void hda_keep_power_on(struct hda_codec *codec) {}
102 #define hda_codec_is_power_on(codec) 1
106 * snd_hda_get_jack_location - Give a location string of the jack
107 * @cfg: pin default config value
109 * Parse the pin default config value and returns the string of the
110 * jack location, e.g. "Rear", "Front", etc.
112 const char *snd_hda_get_jack_location(u32 cfg)
114 static char *bases[7] = {
115 "N/A", "Rear", "Front", "Left", "Right", "Top", "Bottom",
117 static unsigned char specials_idx[] = {
122 static char *specials[] = {
123 "Rear Panel", "Drive Bar",
124 "Riser", "HDMI", "ATAPI",
125 "Mobile-In", "Mobile-Out"
128 cfg = (cfg & AC_DEFCFG_LOCATION) >> AC_DEFCFG_LOCATION_SHIFT;
129 if ((cfg & 0x0f) < 7)
130 return bases[cfg & 0x0f];
131 for (i = 0; i < ARRAY_SIZE(specials_idx); i++) {
132 if (cfg == specials_idx[i])
137 EXPORT_SYMBOL_HDA(snd_hda_get_jack_location);
140 * snd_hda_get_jack_connectivity - Give a connectivity string of the jack
141 * @cfg: pin default config value
143 * Parse the pin default config value and returns the string of the
144 * jack connectivity, i.e. external or internal connection.
146 const char *snd_hda_get_jack_connectivity(u32 cfg)
148 static char *jack_locations[4] = { "Ext", "Int", "Sep", "Oth" };
150 return jack_locations[(cfg >> (AC_DEFCFG_LOCATION_SHIFT + 4)) & 3];
152 EXPORT_SYMBOL_HDA(snd_hda_get_jack_connectivity);
155 * snd_hda_get_jack_type - Give a type string of the jack
156 * @cfg: pin default config value
158 * Parse the pin default config value and returns the string of the
159 * jack type, i.e. the purpose of the jack, such as Line-Out or CD.
161 const char *snd_hda_get_jack_type(u32 cfg)
163 static char *jack_types[16] = {
164 "Line Out", "Speaker", "HP Out", "CD",
165 "SPDIF Out", "Digital Out", "Modem Line", "Modem Hand",
166 "Line In", "Aux", "Mic", "Telephony",
167 "SPDIF In", "Digitial In", "Reserved", "Other"
170 return jack_types[(cfg & AC_DEFCFG_DEVICE)
171 >> AC_DEFCFG_DEVICE_SHIFT];
173 EXPORT_SYMBOL_HDA(snd_hda_get_jack_type);
176 * Compose a 32bit command word to be sent to the HD-audio controller
178 static inline unsigned int
179 make_codec_cmd(struct hda_codec *codec, hda_nid_t nid, int direct,
180 unsigned int verb, unsigned int parm)
184 if ((codec->addr & ~0xf) || (direct & ~1) || (nid & ~0x7f) ||
185 (verb & ~0xfff) || (parm & ~0xffff)) {
186 printk(KERN_ERR "hda-codec: out of range cmd %x:%x:%x:%x:%x\n",
187 codec->addr, direct, nid, verb, parm);
191 val = (u32)codec->addr << 28;
192 val |= (u32)direct << 27;
193 val |= (u32)nid << 20;
200 * Send and receive a verb
202 static int codec_exec_verb(struct hda_codec *codec, unsigned int cmd,
205 struct hda_bus *bus = codec->bus;
214 snd_hda_power_up(codec);
215 mutex_lock(&bus->cmd_mutex);
216 trace_hda_send_cmd(codec, cmd);
217 err = bus->ops.command(bus, cmd);
219 *res = bus->ops.get_response(bus, codec->addr);
220 trace_hda_get_response(codec, *res);
222 mutex_unlock(&bus->cmd_mutex);
223 snd_hda_power_down(codec);
224 if (res && *res == -1 && bus->rirb_error) {
225 if (bus->response_reset) {
226 snd_printd("hda_codec: resetting BUS due to "
227 "fatal communication error\n");
228 trace_hda_bus_reset(bus);
229 bus->ops.bus_reset(bus);
233 /* clear reset-flag when the communication gets recovered */
235 bus->response_reset = 0;
240 * snd_hda_codec_read - send a command and get the response
241 * @codec: the HDA codec
242 * @nid: NID to send the command
243 * @direct: direct flag
244 * @verb: the verb to send
245 * @parm: the parameter for the verb
247 * Send a single command and read the corresponding response.
249 * Returns the obtained response value, or -1 for an error.
251 unsigned int snd_hda_codec_read(struct hda_codec *codec, hda_nid_t nid,
253 unsigned int verb, unsigned int parm)
255 unsigned cmd = make_codec_cmd(codec, nid, direct, verb, parm);
257 if (codec_exec_verb(codec, cmd, &res))
261 EXPORT_SYMBOL_HDA(snd_hda_codec_read);
264 * snd_hda_codec_write - send a single command without waiting for response
265 * @codec: the HDA codec
266 * @nid: NID to send the command
267 * @direct: direct flag
268 * @verb: the verb to send
269 * @parm: the parameter for the verb
271 * Send a single command without waiting for response.
273 * Returns 0 if successful, or a negative error code.
275 int snd_hda_codec_write(struct hda_codec *codec, hda_nid_t nid, int direct,
276 unsigned int verb, unsigned int parm)
278 unsigned int cmd = make_codec_cmd(codec, nid, direct, verb, parm);
280 return codec_exec_verb(codec, cmd,
281 codec->bus->sync_write ? &res : NULL);
283 EXPORT_SYMBOL_HDA(snd_hda_codec_write);
286 * snd_hda_sequence_write - sequence writes
287 * @codec: the HDA codec
288 * @seq: VERB array to send
290 * Send the commands sequentially from the given array.
291 * The array must be terminated with NID=0.
293 void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq)
295 for (; seq->nid; seq++)
296 snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param);
298 EXPORT_SYMBOL_HDA(snd_hda_sequence_write);
301 * snd_hda_get_sub_nodes - get the range of sub nodes
302 * @codec: the HDA codec
304 * @start_id: the pointer to store the start NID
306 * Parse the NID and store the start NID of its sub-nodes.
307 * Returns the number of sub-nodes.
309 int snd_hda_get_sub_nodes(struct hda_codec *codec, hda_nid_t nid,
314 parm = snd_hda_param_read(codec, nid, AC_PAR_NODE_COUNT);
317 *start_id = (parm >> 16) & 0x7fff;
318 return (int)(parm & 0x7fff);
320 EXPORT_SYMBOL_HDA(snd_hda_get_sub_nodes);
322 /* look up the cached results */
323 static hda_nid_t *lookup_conn_list(struct snd_array *array, hda_nid_t nid)
326 for (i = 0; i < array->used; ) {
327 hda_nid_t *p = snd_array_elem(array, i);
337 * snd_hda_get_conn_list - get connection list
338 * @codec: the HDA codec
340 * @listp: the pointer to store NID list
342 * Parses the connection list of the given widget and stores the list
345 * Returns the number of connections, or a negative error code.
347 int snd_hda_get_conn_list(struct hda_codec *codec, hda_nid_t nid,
348 const hda_nid_t **listp)
350 struct snd_array *array = &codec->conn_lists;
352 hda_nid_t list[HDA_MAX_CONNECTIONS];
357 /* if the connection-list is already cached, read it */
358 p = lookup_conn_list(array, nid);
364 if (snd_BUG_ON(added))
367 /* read the connection and add to the cache */
368 len = snd_hda_get_raw_connections(codec, nid, list, HDA_MAX_CONNECTIONS);
371 err = snd_hda_override_conn_list(codec, nid, len, list);
377 EXPORT_SYMBOL_HDA(snd_hda_get_conn_list);
380 * snd_hda_get_connections - copy connection list
381 * @codec: the HDA codec
383 * @conn_list: connection list array
384 * @max_conns: max. number of connections to store
386 * Parses the connection list of the given widget and stores the list
389 * Returns the number of connections, or a negative error code.
391 int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
392 hda_nid_t *conn_list, int max_conns)
394 const hda_nid_t *list;
395 int len = snd_hda_get_conn_list(codec, nid, &list);
399 if (len > max_conns) {
400 snd_printk(KERN_ERR "hda_codec: "
401 "Too many connections %d for NID 0x%x\n",
405 memcpy(conn_list, list, len * sizeof(hda_nid_t));
408 EXPORT_SYMBOL_HDA(snd_hda_get_connections);
411 * snd_hda_get_raw_connections - copy connection list without cache
412 * @codec: the HDA codec
414 * @conn_list: connection list array
415 * @max_conns: max. number of connections to store
417 * Like snd_hda_get_connections(), copy the connection list but without
418 * checking through the connection-list cache.
419 * Currently called only from hda_proc.c, so not exported.
421 int snd_hda_get_raw_connections(struct hda_codec *codec, hda_nid_t nid,
422 hda_nid_t *conn_list, int max_conns)
425 int i, conn_len, conns;
426 unsigned int shift, num_elems, mask;
430 if (snd_BUG_ON(!conn_list || max_conns <= 0))
433 wcaps = get_wcaps(codec, nid);
434 if (!(wcaps & AC_WCAP_CONN_LIST) &&
435 get_wcaps_type(wcaps) != AC_WID_VOL_KNB)
438 parm = snd_hda_param_read(codec, nid, AC_PAR_CONNLIST_LEN);
439 if (parm & AC_CLIST_LONG) {
448 conn_len = parm & AC_CLIST_LENGTH;
449 mask = (1 << (shift-1)) - 1;
452 return 0; /* no connection */
455 /* single connection */
456 parm = snd_hda_codec_read(codec, nid, 0,
457 AC_VERB_GET_CONNECT_LIST, 0);
458 if (parm == -1 && codec->bus->rirb_error)
460 conn_list[0] = parm & mask;
464 /* multi connection */
467 for (i = 0; i < conn_len; i++) {
471 if (i % num_elems == 0) {
472 parm = snd_hda_codec_read(codec, nid, 0,
473 AC_VERB_GET_CONNECT_LIST, i);
474 if (parm == -1 && codec->bus->rirb_error)
477 range_val = !!(parm & (1 << (shift-1))); /* ranges */
480 snd_printk(KERN_WARNING "hda_codec: "
481 "invalid CONNECT_LIST verb %x[%i]:%x\n",
487 /* ranges between the previous and this one */
488 if (!prev_nid || prev_nid >= val) {
489 snd_printk(KERN_WARNING "hda_codec: "
490 "invalid dep_range_val %x:%x\n",
494 for (n = prev_nid + 1; n <= val; n++) {
495 if (conns >= max_conns) {
496 snd_printk(KERN_ERR "hda_codec: "
497 "Too many connections %d for NID 0x%x\n",
501 conn_list[conns++] = n;
504 if (conns >= max_conns) {
505 snd_printk(KERN_ERR "hda_codec: "
506 "Too many connections %d for NID 0x%x\n",
510 conn_list[conns++] = val;
517 static bool add_conn_list(struct snd_array *array, hda_nid_t nid)
519 hda_nid_t *p = snd_array_new(array);
527 * snd_hda_override_conn_list - add/modify the connection-list to cache
528 * @codec: the HDA codec
530 * @len: number of connection list entries
531 * @list: the list of connection entries
533 * Add or modify the given connection-list to the cache. If the corresponding
534 * cache already exists, invalidate it and append a new one.
536 * Returns zero or a negative error code.
538 int snd_hda_override_conn_list(struct hda_codec *codec, hda_nid_t nid, int len,
539 const hda_nid_t *list)
541 struct snd_array *array = &codec->conn_lists;
545 p = lookup_conn_list(array, nid);
547 *p = -1; /* invalidate the old entry */
549 old_used = array->used;
550 if (!add_conn_list(array, nid) || !add_conn_list(array, len))
552 for (i = 0; i < len; i++)
553 if (!add_conn_list(array, list[i]))
558 array->used = old_used;
561 EXPORT_SYMBOL_HDA(snd_hda_override_conn_list);
564 * snd_hda_get_conn_index - get the connection index of the given NID
565 * @codec: the HDA codec
566 * @mux: NID containing the list
567 * @nid: NID to select
568 * @recursive: 1 when searching NID recursively, otherwise 0
570 * Parses the connection list of the widget @mux and checks whether the
571 * widget @nid is present. If it is, return the connection index.
572 * Otherwise it returns -1.
574 int snd_hda_get_conn_index(struct hda_codec *codec, hda_nid_t mux,
575 hda_nid_t nid, int recursive)
577 hda_nid_t conn[HDA_MAX_NUM_INPUTS];
580 nums = snd_hda_get_connections(codec, mux, conn, ARRAY_SIZE(conn));
581 for (i = 0; i < nums; i++)
587 snd_printd("hda_codec: too deep connection for 0x%x\n", nid);
591 for (i = 0; i < nums; i++) {
592 unsigned int type = get_wcaps_type(get_wcaps(codec, conn[i]));
593 if (type == AC_WID_PIN || type == AC_WID_AUD_OUT)
595 if (snd_hda_get_conn_index(codec, conn[i], nid, recursive) >= 0)
600 EXPORT_SYMBOL_HDA(snd_hda_get_conn_index);
603 * snd_hda_queue_unsol_event - add an unsolicited event to queue
605 * @res: unsolicited event (lower 32bit of RIRB entry)
606 * @res_ex: codec addr and flags (upper 32bit or RIRB entry)
608 * Adds the given event to the queue. The events are processed in
609 * the workqueue asynchronously. Call this function in the interrupt
610 * hanlder when RIRB receives an unsolicited event.
612 * Returns 0 if successful, or a negative error code.
614 int snd_hda_queue_unsol_event(struct hda_bus *bus, u32 res, u32 res_ex)
616 struct hda_bus_unsolicited *unsol;
619 trace_hda_unsol_event(bus, res, res_ex);
624 wp = (unsol->wp + 1) % HDA_UNSOL_QUEUE_SIZE;
628 unsol->queue[wp] = res;
629 unsol->queue[wp + 1] = res_ex;
631 queue_work(bus->workq, &unsol->work);
635 EXPORT_SYMBOL_HDA(snd_hda_queue_unsol_event);
638 * process queued unsolicited events
640 static void process_unsol_events(struct work_struct *work)
642 struct hda_bus_unsolicited *unsol =
643 container_of(work, struct hda_bus_unsolicited, work);
644 struct hda_bus *bus = unsol->bus;
645 struct hda_codec *codec;
646 unsigned int rp, caddr, res;
648 while (unsol->rp != unsol->wp) {
649 rp = (unsol->rp + 1) % HDA_UNSOL_QUEUE_SIZE;
652 res = unsol->queue[rp];
653 caddr = unsol->queue[rp + 1];
654 if (!(caddr & (1 << 4))) /* no unsolicited event? */
656 codec = bus->caddr_tbl[caddr & 0x0f];
657 if (codec && codec->patch_ops.unsol_event)
658 codec->patch_ops.unsol_event(codec, res);
663 * initialize unsolicited queue
665 static int init_unsol_queue(struct hda_bus *bus)
667 struct hda_bus_unsolicited *unsol;
669 if (bus->unsol) /* already initialized */
672 unsol = kzalloc(sizeof(*unsol), GFP_KERNEL);
674 snd_printk(KERN_ERR "hda_codec: "
675 "can't allocate unsolicited queue\n");
678 INIT_WORK(&unsol->work, process_unsol_events);
687 static void snd_hda_codec_free(struct hda_codec *codec);
689 static int snd_hda_bus_free(struct hda_bus *bus)
691 struct hda_codec *codec, *n;
696 flush_workqueue(bus->workq);
699 list_for_each_entry_safe(codec, n, &bus->codec_list, list) {
700 snd_hda_codec_free(codec);
702 if (bus->ops.private_free)
703 bus->ops.private_free(bus);
705 destroy_workqueue(bus->workq);
710 static int snd_hda_bus_dev_free(struct snd_device *device)
712 struct hda_bus *bus = device->device_data;
714 return snd_hda_bus_free(bus);
717 #ifdef CONFIG_SND_HDA_HWDEP
718 static int snd_hda_bus_dev_register(struct snd_device *device)
720 struct hda_bus *bus = device->device_data;
721 struct hda_codec *codec;
722 list_for_each_entry(codec, &bus->codec_list, list) {
723 snd_hda_hwdep_add_sysfs(codec);
724 snd_hda_hwdep_add_power_sysfs(codec);
729 #define snd_hda_bus_dev_register NULL
733 * snd_hda_bus_new - create a HDA bus
734 * @card: the card entry
735 * @temp: the template for hda_bus information
736 * @busp: the pointer to store the created bus instance
738 * Returns 0 if successful, or a negative error code.
740 int /*__devinit*/ snd_hda_bus_new(struct snd_card *card,
741 const struct hda_bus_template *temp,
742 struct hda_bus **busp)
746 static struct snd_device_ops dev_ops = {
747 .dev_register = snd_hda_bus_dev_register,
748 .dev_free = snd_hda_bus_dev_free,
751 if (snd_BUG_ON(!temp))
753 if (snd_BUG_ON(!temp->ops.command || !temp->ops.get_response))
759 bus = kzalloc(sizeof(*bus), GFP_KERNEL);
761 snd_printk(KERN_ERR "can't allocate struct hda_bus\n");
766 bus->private_data = temp->private_data;
767 bus->pci = temp->pci;
768 bus->modelname = temp->modelname;
769 bus->power_save = temp->power_save;
770 bus->ops = temp->ops;
772 mutex_init(&bus->cmd_mutex);
773 mutex_init(&bus->prepare_mutex);
774 INIT_LIST_HEAD(&bus->codec_list);
776 snprintf(bus->workq_name, sizeof(bus->workq_name),
777 "hd-audio%d", card->number);
778 bus->workq = create_singlethread_workqueue(bus->workq_name);
780 snd_printk(KERN_ERR "cannot create workqueue %s\n",
786 err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops);
788 snd_hda_bus_free(bus);
795 EXPORT_SYMBOL_HDA(snd_hda_bus_new);
797 #ifdef CONFIG_SND_HDA_GENERIC
798 #define is_generic_config(codec) \
799 (codec->modelname && !strcmp(codec->modelname, "generic"))
801 #define is_generic_config(codec) 0
805 #define HDA_MODREQ_MAX_COUNT 2 /* two request_modules()'s */
807 #define HDA_MODREQ_MAX_COUNT 0 /* all presets are statically linked */
811 * find a matching codec preset
813 static const struct hda_codec_preset *
814 find_codec_preset(struct hda_codec *codec)
816 struct hda_codec_preset_list *tbl;
817 const struct hda_codec_preset *preset;
818 int mod_requested = 0;
820 if (is_generic_config(codec))
821 return NULL; /* use the generic parser */
824 mutex_lock(&preset_mutex);
825 list_for_each_entry(tbl, &hda_preset_tables, list) {
826 if (!try_module_get(tbl->owner)) {
827 snd_printk(KERN_ERR "hda_codec: cannot module_get\n");
830 for (preset = tbl->preset; preset->id; preset++) {
831 u32 mask = preset->mask;
832 if (preset->afg && preset->afg != codec->afg)
834 if (preset->mfg && preset->mfg != codec->mfg)
838 if (preset->id == (codec->vendor_id & mask) &&
840 preset->rev == codec->revision_id)) {
841 mutex_unlock(&preset_mutex);
842 codec->owner = tbl->owner;
846 module_put(tbl->owner);
848 mutex_unlock(&preset_mutex);
850 if (mod_requested < HDA_MODREQ_MAX_COUNT) {
853 snprintf(name, sizeof(name), "snd-hda-codec-id:%08x",
856 snprintf(name, sizeof(name), "snd-hda-codec-id:%04x*",
857 (codec->vendor_id >> 16) & 0xffff);
858 request_module(name);
866 * get_codec_name - store the codec name
868 static int get_codec_name(struct hda_codec *codec)
870 const struct hda_vendor_id *c;
871 const char *vendor = NULL;
872 u16 vendor_id = codec->vendor_id >> 16;
875 if (codec->vendor_name)
878 for (c = hda_vendor_ids; c->id; c++) {
879 if (c->id == vendor_id) {
885 sprintf(tmp, "Generic %04x", vendor_id);
888 codec->vendor_name = kstrdup(vendor, GFP_KERNEL);
889 if (!codec->vendor_name)
893 if (codec->chip_name)
896 if (codec->preset && codec->preset->name)
897 codec->chip_name = kstrdup(codec->preset->name, GFP_KERNEL);
899 sprintf(tmp, "ID %x", codec->vendor_id & 0xffff);
900 codec->chip_name = kstrdup(tmp, GFP_KERNEL);
902 if (!codec->chip_name)
908 * look for an AFG and MFG nodes
910 static void /*__devinit*/ setup_fg_nodes(struct hda_codec *codec)
912 int i, total_nodes, function_id;
915 total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid);
916 for (i = 0; i < total_nodes; i++, nid++) {
917 function_id = snd_hda_param_read(codec, nid,
918 AC_PAR_FUNCTION_TYPE);
919 switch (function_id & 0xff) {
920 case AC_GRP_AUDIO_FUNCTION:
922 codec->afg_function_id = function_id & 0xff;
923 codec->afg_unsol = (function_id >> 8) & 1;
925 case AC_GRP_MODEM_FUNCTION:
927 codec->mfg_function_id = function_id & 0xff;
928 codec->mfg_unsol = (function_id >> 8) & 1;
937 * read widget caps for each widget and store in cache
939 static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
944 codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node,
946 codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL);
949 nid = codec->start_nid;
950 for (i = 0; i < codec->num_nodes; i++, nid++)
951 codec->wcaps[i] = snd_hda_param_read(codec, nid,
952 AC_PAR_AUDIO_WIDGET_CAP);
956 /* read all pin default configurations and save codec->init_pins */
957 static int read_pin_defaults(struct hda_codec *codec)
960 hda_nid_t nid = codec->start_nid;
962 for (i = 0; i < codec->num_nodes; i++, nid++) {
963 struct hda_pincfg *pin;
964 unsigned int wcaps = get_wcaps(codec, nid);
965 unsigned int wid_type = get_wcaps_type(wcaps);
966 if (wid_type != AC_WID_PIN)
968 pin = snd_array_new(&codec->init_pins);
972 pin->cfg = snd_hda_codec_read(codec, nid, 0,
973 AC_VERB_GET_CONFIG_DEFAULT, 0);
974 pin->ctrl = snd_hda_codec_read(codec, nid, 0,
975 AC_VERB_GET_PIN_WIDGET_CONTROL,
981 /* look up the given pin config list and return the item matching with NID */
982 static struct hda_pincfg *look_up_pincfg(struct hda_codec *codec,
983 struct snd_array *array,
987 for (i = 0; i < array->used; i++) {
988 struct hda_pincfg *pin = snd_array_elem(array, i);
995 /* write a config value for the given NID */
996 static void set_pincfg(struct hda_codec *codec, hda_nid_t nid,
1000 for (i = 0; i < 4; i++) {
1001 snd_hda_codec_write(codec, nid, 0,
1002 AC_VERB_SET_CONFIG_DEFAULT_BYTES_0 + i,
1008 /* set the current pin config value for the given NID.
1009 * the value is cached, and read via snd_hda_codec_get_pincfg()
1011 int snd_hda_add_pincfg(struct hda_codec *codec, struct snd_array *list,
1012 hda_nid_t nid, unsigned int cfg)
1014 struct hda_pincfg *pin;
1015 unsigned int oldcfg;
1017 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN)
1020 oldcfg = snd_hda_codec_get_pincfg(codec, nid);
1021 pin = look_up_pincfg(codec, list, nid);
1023 pin = snd_array_new(list);
1030 /* change only when needed; e.g. if the pincfg is already present
1031 * in user_pins[], don't write it
1033 cfg = snd_hda_codec_get_pincfg(codec, nid);
1035 set_pincfg(codec, nid, cfg);
1040 * snd_hda_codec_set_pincfg - Override a pin default configuration
1041 * @codec: the HDA codec
1042 * @nid: NID to set the pin config
1043 * @cfg: the pin default config value
1045 * Override a pin default configuration value in the cache.
1046 * This value can be read by snd_hda_codec_get_pincfg() in a higher
1047 * priority than the real hardware value.
1049 int snd_hda_codec_set_pincfg(struct hda_codec *codec,
1050 hda_nid_t nid, unsigned int cfg)
1052 return snd_hda_add_pincfg(codec, &codec->driver_pins, nid, cfg);
1054 EXPORT_SYMBOL_HDA(snd_hda_codec_set_pincfg);
1057 * snd_hda_codec_get_pincfg - Obtain a pin-default configuration
1058 * @codec: the HDA codec
1059 * @nid: NID to get the pin config
1061 * Get the current pin config value of the given pin NID.
1062 * If the pincfg value is cached or overridden via sysfs or driver,
1063 * returns the cached value.
1065 unsigned int snd_hda_codec_get_pincfg(struct hda_codec *codec, hda_nid_t nid)
1067 struct hda_pincfg *pin;
1069 #ifdef CONFIG_SND_HDA_HWDEP
1070 pin = look_up_pincfg(codec, &codec->user_pins, nid);
1074 pin = look_up_pincfg(codec, &codec->driver_pins, nid);
1077 pin = look_up_pincfg(codec, &codec->init_pins, nid);
1082 EXPORT_SYMBOL_HDA(snd_hda_codec_get_pincfg);
1084 /* restore all current pin configs */
1085 static void restore_pincfgs(struct hda_codec *codec)
1088 for (i = 0; i < codec->init_pins.used; i++) {
1089 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1090 set_pincfg(codec, pin->nid,
1091 snd_hda_codec_get_pincfg(codec, pin->nid));
1096 * snd_hda_shutup_pins - Shut up all pins
1097 * @codec: the HDA codec
1099 * Clear all pin controls to shup up before suspend for avoiding click noise.
1100 * The controls aren't cached so that they can be resumed properly.
1102 void snd_hda_shutup_pins(struct hda_codec *codec)
1105 /* don't shut up pins when unloading the driver; otherwise it breaks
1106 * the default pin setup at the next load of the driver
1108 if (codec->bus->shutdown)
1110 for (i = 0; i < codec->init_pins.used; i++) {
1111 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1112 /* use read here for syncing after issuing each verb */
1113 snd_hda_codec_read(codec, pin->nid, 0,
1114 AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
1116 codec->pins_shutup = 1;
1118 EXPORT_SYMBOL_HDA(snd_hda_shutup_pins);
1121 /* Restore the pin controls cleared previously via snd_hda_shutup_pins() */
1122 static void restore_shutup_pins(struct hda_codec *codec)
1125 if (!codec->pins_shutup)
1127 if (codec->bus->shutdown)
1129 for (i = 0; i < codec->init_pins.used; i++) {
1130 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1131 snd_hda_codec_write(codec, pin->nid, 0,
1132 AC_VERB_SET_PIN_WIDGET_CONTROL,
1135 codec->pins_shutup = 0;
1139 static void init_hda_cache(struct hda_cache_rec *cache,
1140 unsigned int record_size);
1141 static void free_hda_cache(struct hda_cache_rec *cache);
1143 /* restore the initial pin cfgs and release all pincfg lists */
1144 static void restore_init_pincfgs(struct hda_codec *codec)
1146 /* first free driver_pins and user_pins, then call restore_pincfg
1147 * so that only the values in init_pins are restored
1149 snd_array_free(&codec->driver_pins);
1150 #ifdef CONFIG_SND_HDA_HWDEP
1151 snd_array_free(&codec->user_pins);
1153 restore_pincfgs(codec);
1154 snd_array_free(&codec->init_pins);
1158 * audio-converter setup caches
1160 struct hda_cvt_setup {
1165 unsigned char active; /* cvt is currently used */
1166 unsigned char dirty; /* setups should be cleared */
1169 /* get or create a cache entry for the given audio converter NID */
1170 static struct hda_cvt_setup *
1171 get_hda_cvt_setup(struct hda_codec *codec, hda_nid_t nid)
1173 struct hda_cvt_setup *p;
1176 for (i = 0; i < codec->cvt_setups.used; i++) {
1177 p = snd_array_elem(&codec->cvt_setups, i);
1181 p = snd_array_new(&codec->cvt_setups);
1190 static void snd_hda_codec_free(struct hda_codec *codec)
1194 restore_init_pincfgs(codec);
1195 #ifdef CONFIG_SND_HDA_POWER_SAVE
1196 cancel_delayed_work(&codec->power_work);
1197 flush_workqueue(codec->bus->workq);
1199 list_del(&codec->list);
1200 snd_array_free(&codec->mixers);
1201 snd_array_free(&codec->nids);
1202 snd_array_free(&codec->conn_lists);
1203 snd_array_free(&codec->spdif_out);
1204 codec->bus->caddr_tbl[codec->addr] = NULL;
1205 if (codec->patch_ops.free)
1206 codec->patch_ops.free(codec);
1207 module_put(codec->owner);
1208 free_hda_cache(&codec->amp_cache);
1209 free_hda_cache(&codec->cmd_cache);
1210 kfree(codec->vendor_name);
1211 kfree(codec->chip_name);
1212 kfree(codec->modelname);
1213 kfree(codec->wcaps);
1217 static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
1218 unsigned int power_state);
1221 * snd_hda_codec_new - create a HDA codec
1222 * @bus: the bus to assign
1223 * @codec_addr: the codec address
1224 * @codecp: the pointer to store the generated codec
1226 * Returns 0 if successful, or a negative error code.
1228 int /*__devinit*/ snd_hda_codec_new(struct hda_bus *bus,
1229 unsigned int codec_addr,
1230 struct hda_codec **codecp)
1232 struct hda_codec *codec;
1236 if (snd_BUG_ON(!bus))
1238 if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS))
1241 if (bus->caddr_tbl[codec_addr]) {
1242 snd_printk(KERN_ERR "hda_codec: "
1243 "address 0x%x is already occupied\n", codec_addr);
1247 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
1248 if (codec == NULL) {
1249 snd_printk(KERN_ERR "can't allocate struct hda_codec\n");
1254 codec->addr = codec_addr;
1255 mutex_init(&codec->spdif_mutex);
1256 mutex_init(&codec->control_mutex);
1257 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
1258 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
1259 snd_array_init(&codec->mixers, sizeof(struct hda_nid_item), 32);
1260 snd_array_init(&codec->nids, sizeof(struct hda_nid_item), 32);
1261 snd_array_init(&codec->init_pins, sizeof(struct hda_pincfg), 16);
1262 snd_array_init(&codec->driver_pins, sizeof(struct hda_pincfg), 16);
1263 snd_array_init(&codec->cvt_setups, sizeof(struct hda_cvt_setup), 8);
1264 snd_array_init(&codec->conn_lists, sizeof(hda_nid_t), 64);
1265 snd_array_init(&codec->spdif_out, sizeof(struct hda_spdif_out), 16);
1266 if (codec->bus->modelname) {
1267 codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL);
1268 if (!codec->modelname) {
1269 snd_hda_codec_free(codec);
1274 #ifdef CONFIG_SND_HDA_POWER_SAVE
1275 INIT_DELAYED_WORK(&codec->power_work, hda_power_work);
1276 /* snd_hda_codec_new() marks the codec as power-up, and leave it as is.
1277 * the caller has to power down appropriatley after initialization
1280 hda_keep_power_on(codec);
1283 list_add_tail(&codec->list, &bus->codec_list);
1284 bus->caddr_tbl[codec_addr] = codec;
1286 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1288 if (codec->vendor_id == -1)
1289 /* read again, hopefully the access method was corrected
1290 * in the last read...
1292 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1294 codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1295 AC_PAR_SUBSYSTEM_ID);
1296 codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1299 setup_fg_nodes(codec);
1300 if (!codec->afg && !codec->mfg) {
1301 snd_printdd("hda_codec: no AFG or MFG node found\n");
1306 err = read_widget_caps(codec, codec->afg ? codec->afg : codec->mfg);
1308 snd_printk(KERN_ERR "hda_codec: cannot malloc\n");
1311 err = read_pin_defaults(codec);
1315 if (!codec->subsystem_id) {
1316 hda_nid_t nid = codec->afg ? codec->afg : codec->mfg;
1317 codec->subsystem_id =
1318 snd_hda_codec_read(codec, nid, 0,
1319 AC_VERB_GET_SUBSYSTEM_ID, 0);
1322 /* power-up all before initialization */
1323 hda_set_power_state(codec,
1324 codec->afg ? codec->afg : codec->mfg,
1327 snd_hda_codec_proc_new(codec);
1329 snd_hda_create_hwdep(codec);
1331 sprintf(component, "HDA:%08x,%08x,%08x", codec->vendor_id,
1332 codec->subsystem_id, codec->revision_id);
1333 snd_component_add(codec->bus->card, component);
1340 snd_hda_codec_free(codec);
1343 EXPORT_SYMBOL_HDA(snd_hda_codec_new);
1346 * snd_hda_codec_configure - (Re-)configure the HD-audio codec
1347 * @codec: the HDA codec
1349 * Start parsing of the given codec tree and (re-)initialize the whole
1352 * Returns 0 if successful or a negative error code.
1354 int snd_hda_codec_configure(struct hda_codec *codec)
1358 codec->preset = find_codec_preset(codec);
1359 if (!codec->vendor_name || !codec->chip_name) {
1360 err = get_codec_name(codec);
1365 if (is_generic_config(codec)) {
1366 err = snd_hda_parse_generic_codec(codec);
1369 if (codec->preset && codec->preset->patch) {
1370 err = codec->preset->patch(codec);
1374 /* call the default parser */
1375 err = snd_hda_parse_generic_codec(codec);
1377 printk(KERN_ERR "hda-codec: No codec parser is available\n");
1380 if (!err && codec->patch_ops.unsol_event)
1381 err = init_unsol_queue(codec->bus);
1382 /* audio codec should override the mixer name */
1383 if (!err && (codec->afg || !*codec->bus->card->mixername))
1384 snprintf(codec->bus->card->mixername,
1385 sizeof(codec->bus->card->mixername),
1386 "%s %s", codec->vendor_name, codec->chip_name);
1389 EXPORT_SYMBOL_HDA(snd_hda_codec_configure);
1392 * snd_hda_codec_setup_stream - set up the codec for streaming
1393 * @codec: the CODEC to set up
1394 * @nid: the NID to set up
1395 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
1396 * @channel_id: channel id to pass, zero based.
1397 * @format: stream format.
1399 void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
1401 int channel_id, int format)
1403 struct hda_codec *c;
1404 struct hda_cvt_setup *p;
1405 unsigned int oldval, newval;
1412 snd_printdd("hda_codec_setup_stream: "
1413 "NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
1414 nid, stream_tag, channel_id, format);
1415 p = get_hda_cvt_setup(codec, nid);
1418 /* update the stream-id if changed */
1419 if (p->stream_tag != stream_tag || p->channel_id != channel_id) {
1420 oldval = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0);
1421 newval = (stream_tag << 4) | channel_id;
1422 if (oldval != newval)
1423 snd_hda_codec_write(codec, nid, 0,
1424 AC_VERB_SET_CHANNEL_STREAMID,
1426 p->stream_tag = stream_tag;
1427 p->channel_id = channel_id;
1429 /* update the format-id if changed */
1430 if (p->format_id != format) {
1431 oldval = snd_hda_codec_read(codec, nid, 0,
1432 AC_VERB_GET_STREAM_FORMAT, 0);
1433 if (oldval != format) {
1435 snd_hda_codec_write(codec, nid, 0,
1436 AC_VERB_SET_STREAM_FORMAT,
1439 p->format_id = format;
1444 /* make other inactive cvts with the same stream-tag dirty */
1445 type = get_wcaps_type(get_wcaps(codec, nid));
1446 list_for_each_entry(c, &codec->bus->codec_list, list) {
1447 for (i = 0; i < c->cvt_setups.used; i++) {
1448 p = snd_array_elem(&c->cvt_setups, i);
1449 if (!p->active && p->stream_tag == stream_tag &&
1450 get_wcaps_type(get_wcaps(c, p->nid)) == type)
1455 EXPORT_SYMBOL_HDA(snd_hda_codec_setup_stream);
1457 static void really_cleanup_stream(struct hda_codec *codec,
1458 struct hda_cvt_setup *q);
1461 * __snd_hda_codec_cleanup_stream - clean up the codec for closing
1462 * @codec: the CODEC to clean up
1463 * @nid: the NID to clean up
1464 * @do_now: really clean up the stream instead of clearing the active flag
1466 void __snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid,
1469 struct hda_cvt_setup *p;
1474 if (codec->no_sticky_stream)
1477 snd_printdd("hda_codec_cleanup_stream: NID=0x%x\n", nid);
1478 p = get_hda_cvt_setup(codec, nid);
1480 /* here we just clear the active flag when do_now isn't set;
1481 * actual clean-ups will be done later in
1482 * purify_inactive_streams() called from snd_hda_codec_prpapre()
1485 really_cleanup_stream(codec, p);
1490 EXPORT_SYMBOL_HDA(__snd_hda_codec_cleanup_stream);
1492 static void really_cleanup_stream(struct hda_codec *codec,
1493 struct hda_cvt_setup *q)
1495 hda_nid_t nid = q->nid;
1496 if (q->stream_tag || q->channel_id)
1497 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
1499 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0
1501 memset(q, 0, sizeof(*q));
1505 /* clean up the all conflicting obsolete streams */
1506 static void purify_inactive_streams(struct hda_codec *codec)
1508 struct hda_codec *c;
1511 list_for_each_entry(c, &codec->bus->codec_list, list) {
1512 for (i = 0; i < c->cvt_setups.used; i++) {
1513 struct hda_cvt_setup *p;
1514 p = snd_array_elem(&c->cvt_setups, i);
1516 really_cleanup_stream(c, p);
1522 /* clean up all streams; called from suspend */
1523 static void hda_cleanup_all_streams(struct hda_codec *codec)
1527 for (i = 0; i < codec->cvt_setups.used; i++) {
1528 struct hda_cvt_setup *p = snd_array_elem(&codec->cvt_setups, i);
1530 really_cleanup_stream(codec, p);
1536 * amp access functions
1539 /* FIXME: more better hash key? */
1540 #define HDA_HASH_KEY(nid, dir, idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
1541 #define HDA_HASH_PINCAP_KEY(nid) (u32)((nid) + (0x02 << 24))
1542 #define HDA_HASH_PARPCM_KEY(nid) (u32)((nid) + (0x03 << 24))
1543 #define HDA_HASH_PARSTR_KEY(nid) (u32)((nid) + (0x04 << 24))
1544 #define INFO_AMP_CAPS (1<<0)
1545 #define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
1547 /* initialize the hash table */
1548 static void /*__devinit*/ init_hda_cache(struct hda_cache_rec *cache,
1549 unsigned int record_size)
1551 memset(cache, 0, sizeof(*cache));
1552 memset(cache->hash, 0xff, sizeof(cache->hash));
1553 snd_array_init(&cache->buf, record_size, 64);
1556 static void free_hda_cache(struct hda_cache_rec *cache)
1558 snd_array_free(&cache->buf);
1561 /* query the hash. allocate an entry if not found. */
1562 static struct hda_cache_head *get_hash(struct hda_cache_rec *cache, u32 key)
1564 u16 idx = key % (u16)ARRAY_SIZE(cache->hash);
1565 u16 cur = cache->hash[idx];
1566 struct hda_cache_head *info;
1568 while (cur != 0xffff) {
1569 info = snd_array_elem(&cache->buf, cur);
1570 if (info->key == key)
1577 /* query the hash. allocate an entry if not found. */
1578 static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache,
1581 struct hda_cache_head *info = get_hash(cache, key);
1584 /* add a new hash entry */
1585 info = snd_array_new(&cache->buf);
1588 cur = snd_array_index(&cache->buf, info);
1591 idx = key % (u16)ARRAY_SIZE(cache->hash);
1592 info->next = cache->hash[idx];
1593 cache->hash[idx] = cur;
1598 /* query and allocate an amp hash entry */
1599 static inline struct hda_amp_info *
1600 get_alloc_amp_hash(struct hda_codec *codec, u32 key)
1602 return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key);
1606 * query_amp_caps - query AMP capabilities
1607 * @codec: the HD-auio codec
1608 * @nid: the NID to query
1609 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1611 * Query AMP capabilities for the given widget and direction.
1612 * Returns the obtained capability bits.
1614 * When cap bits have been already read, this doesn't read again but
1615 * returns the cached value.
1617 u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
1619 struct hda_amp_info *info;
1621 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, 0));
1624 if (!(info->head.val & INFO_AMP_CAPS)) {
1625 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
1627 info->amp_caps = snd_hda_param_read(codec, nid,
1628 direction == HDA_OUTPUT ?
1629 AC_PAR_AMP_OUT_CAP :
1632 info->head.val |= INFO_AMP_CAPS;
1634 return info->amp_caps;
1636 EXPORT_SYMBOL_HDA(query_amp_caps);
1639 * snd_hda_override_amp_caps - Override the AMP capabilities
1640 * @codec: the CODEC to clean up
1641 * @nid: the NID to clean up
1642 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1643 * @caps: the capability bits to set
1645 * Override the cached AMP caps bits value by the given one.
1646 * This function is useful if the driver needs to adjust the AMP ranges,
1647 * e.g. limit to 0dB, etc.
1649 * Returns zero if successful or a negative error code.
1651 int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
1654 struct hda_amp_info *info;
1656 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, dir, 0));
1659 info->amp_caps = caps;
1660 info->head.val |= INFO_AMP_CAPS;
1663 EXPORT_SYMBOL_HDA(snd_hda_override_amp_caps);
1666 query_caps_hash(struct hda_codec *codec, hda_nid_t nid, u32 key,
1667 unsigned int (*func)(struct hda_codec *, hda_nid_t))
1669 struct hda_amp_info *info;
1671 info = get_alloc_amp_hash(codec, key);
1674 if (!info->head.val) {
1675 info->head.val |= INFO_AMP_CAPS;
1676 info->amp_caps = func(codec, nid);
1678 return info->amp_caps;
1681 static unsigned int read_pin_cap(struct hda_codec *codec, hda_nid_t nid)
1683 return snd_hda_param_read(codec, nid, AC_PAR_PIN_CAP);
1687 * snd_hda_query_pin_caps - Query PIN capabilities
1688 * @codec: the HD-auio codec
1689 * @nid: the NID to query
1691 * Query PIN capabilities for the given widget.
1692 * Returns the obtained capability bits.
1694 * When cap bits have been already read, this doesn't read again but
1695 * returns the cached value.
1697 u32 snd_hda_query_pin_caps(struct hda_codec *codec, hda_nid_t nid)
1699 return query_caps_hash(codec, nid, HDA_HASH_PINCAP_KEY(nid),
1702 EXPORT_SYMBOL_HDA(snd_hda_query_pin_caps);
1705 * snd_hda_override_pin_caps - Override the pin capabilities
1707 * @nid: the NID to override
1708 * @caps: the capability bits to set
1710 * Override the cached PIN capabilitiy bits value by the given one.
1712 * Returns zero if successful or a negative error code.
1714 int snd_hda_override_pin_caps(struct hda_codec *codec, hda_nid_t nid,
1717 struct hda_amp_info *info;
1718 info = get_alloc_amp_hash(codec, HDA_HASH_PINCAP_KEY(nid));
1721 info->amp_caps = caps;
1722 info->head.val |= INFO_AMP_CAPS;
1725 EXPORT_SYMBOL_HDA(snd_hda_override_pin_caps);
1728 * read the current volume to info
1729 * if the cache exists, read the cache value.
1731 static unsigned int get_vol_mute(struct hda_codec *codec,
1732 struct hda_amp_info *info, hda_nid_t nid,
1733 int ch, int direction, int index)
1737 if (info->head.val & INFO_AMP_VOL(ch))
1738 return info->vol[ch];
1740 parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
1741 parm |= direction == HDA_OUTPUT ? AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
1743 val = snd_hda_codec_read(codec, nid, 0,
1744 AC_VERB_GET_AMP_GAIN_MUTE, parm);
1745 info->vol[ch] = val & 0xff;
1746 info->head.val |= INFO_AMP_VOL(ch);
1747 return info->vol[ch];
1751 * write the current volume in info to the h/w and update the cache
1753 static void put_vol_mute(struct hda_codec *codec, struct hda_amp_info *info,
1754 hda_nid_t nid, int ch, int direction, int index,
1759 parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
1760 parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
1761 parm |= index << AC_AMP_SET_INDEX_SHIFT;
1762 if ((val & HDA_AMP_MUTE) && !(info->amp_caps & AC_AMPCAP_MUTE) &&
1763 (info->amp_caps & AC_AMPCAP_MIN_MUTE))
1764 ; /* set the zero value as a fake mute */
1767 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
1768 info->vol[ch] = val;
1772 * snd_hda_codec_amp_read - Read AMP value
1773 * @codec: HD-audio codec
1774 * @nid: NID to read the AMP value
1775 * @ch: channel (left=0 or right=1)
1776 * @direction: #HDA_INPUT or #HDA_OUTPUT
1777 * @index: the index value (only for input direction)
1779 * Read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
1781 int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
1782 int direction, int index)
1784 struct hda_amp_info *info;
1785 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
1788 return get_vol_mute(codec, info, nid, ch, direction, index);
1790 EXPORT_SYMBOL_HDA(snd_hda_codec_amp_read);
1793 * snd_hda_codec_amp_update - update the AMP value
1794 * @codec: HD-audio codec
1795 * @nid: NID to read the AMP value
1796 * @ch: channel (left=0 or right=1)
1797 * @direction: #HDA_INPUT or #HDA_OUTPUT
1798 * @idx: the index value (only for input direction)
1799 * @mask: bit mask to set
1800 * @val: the bits value to set
1802 * Update the AMP value with a bit mask.
1803 * Returns 0 if the value is unchanged, 1 if changed.
1805 int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
1806 int direction, int idx, int mask, int val)
1808 struct hda_amp_info *info;
1810 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, idx));
1813 if (snd_BUG_ON(mask & ~0xff))
1816 val |= get_vol_mute(codec, info, nid, ch, direction, idx) & ~mask;
1817 if (info->vol[ch] == val)
1819 put_vol_mute(codec, info, nid, ch, direction, idx, val);
1822 EXPORT_SYMBOL_HDA(snd_hda_codec_amp_update);
1825 * snd_hda_codec_amp_stereo - update the AMP stereo values
1826 * @codec: HD-audio codec
1827 * @nid: NID to read the AMP value
1828 * @direction: #HDA_INPUT or #HDA_OUTPUT
1829 * @idx: the index value (only for input direction)
1830 * @mask: bit mask to set
1831 * @val: the bits value to set
1833 * Update the AMP values like snd_hda_codec_amp_update(), but for a
1834 * stereo widget with the same mask and value.
1836 int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
1837 int direction, int idx, int mask, int val)
1841 if (snd_BUG_ON(mask & ~0xff))
1843 for (ch = 0; ch < 2; ch++)
1844 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
1848 EXPORT_SYMBOL_HDA(snd_hda_codec_amp_stereo);
1852 * snd_hda_codec_resume_amp - Resume all AMP commands from the cache
1853 * @codec: HD-audio codec
1855 * Resume the all amp commands from the cache.
1857 void snd_hda_codec_resume_amp(struct hda_codec *codec)
1859 struct hda_amp_info *buffer = codec->amp_cache.buf.list;
1862 for (i = 0; i < codec->amp_cache.buf.used; i++, buffer++) {
1863 u32 key = buffer->head.key;
1865 unsigned int idx, dir, ch;
1869 idx = (key >> 16) & 0xff;
1870 dir = (key >> 24) & 0xff;
1871 for (ch = 0; ch < 2; ch++) {
1872 if (!(buffer->head.val & INFO_AMP_VOL(ch)))
1874 put_vol_mute(codec, buffer, nid, ch, dir, idx,
1879 EXPORT_SYMBOL_HDA(snd_hda_codec_resume_amp);
1880 #endif /* CONFIG_PM */
1882 static u32 get_amp_max_value(struct hda_codec *codec, hda_nid_t nid, int dir,
1885 u32 caps = query_amp_caps(codec, nid, dir);
1887 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1894 * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer
1896 * The control element is supposed to have the private_value field
1897 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1899 int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
1900 struct snd_ctl_elem_info *uinfo)
1902 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1903 u16 nid = get_amp_nid(kcontrol);
1904 u8 chs = get_amp_channels(kcontrol);
1905 int dir = get_amp_direction(kcontrol);
1906 unsigned int ofs = get_amp_offset(kcontrol);
1908 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1909 uinfo->count = chs == 3 ? 2 : 1;
1910 uinfo->value.integer.min = 0;
1911 uinfo->value.integer.max = get_amp_max_value(codec, nid, dir, ofs);
1912 if (!uinfo->value.integer.max) {
1913 printk(KERN_WARNING "hda_codec: "
1914 "num_steps = 0 for NID=0x%x (ctl = %s)\n", nid,
1920 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_info);
1923 static inline unsigned int
1924 read_amp_value(struct hda_codec *codec, hda_nid_t nid,
1925 int ch, int dir, int idx, unsigned int ofs)
1928 val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
1929 val &= HDA_AMP_VOLMASK;
1938 update_amp_value(struct hda_codec *codec, hda_nid_t nid,
1939 int ch, int dir, int idx, unsigned int ofs,
1942 unsigned int maxval;
1946 /* ofs = 0: raw max value */
1947 maxval = get_amp_max_value(codec, nid, dir, 0);
1950 return snd_hda_codec_amp_update(codec, nid, ch, dir, idx,
1951 HDA_AMP_VOLMASK, val);
1955 * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume
1957 * The control element is supposed to have the private_value field
1958 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1960 int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
1961 struct snd_ctl_elem_value *ucontrol)
1963 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1964 hda_nid_t nid = get_amp_nid(kcontrol);
1965 int chs = get_amp_channels(kcontrol);
1966 int dir = get_amp_direction(kcontrol);
1967 int idx = get_amp_index(kcontrol);
1968 unsigned int ofs = get_amp_offset(kcontrol);
1969 long *valp = ucontrol->value.integer.value;
1972 *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs);
1974 *valp = read_amp_value(codec, nid, 1, dir, idx, ofs);
1977 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_get);
1980 * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume
1982 * The control element is supposed to have the private_value field
1983 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1985 int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
1986 struct snd_ctl_elem_value *ucontrol)
1988 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1989 hda_nid_t nid = get_amp_nid(kcontrol);
1990 int chs = get_amp_channels(kcontrol);
1991 int dir = get_amp_direction(kcontrol);
1992 int idx = get_amp_index(kcontrol);
1993 unsigned int ofs = get_amp_offset(kcontrol);
1994 long *valp = ucontrol->value.integer.value;
1997 snd_hda_power_up(codec);
1999 change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp);
2003 change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp);
2004 snd_hda_power_down(codec);
2007 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_put);
2010 * snd_hda_mixer_amp_volume_put - TLV callback for a standard AMP mixer volume
2012 * The control element is supposed to have the private_value field
2013 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2015 int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2016 unsigned int size, unsigned int __user *_tlv)
2018 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2019 hda_nid_t nid = get_amp_nid(kcontrol);
2020 int dir = get_amp_direction(kcontrol);
2021 unsigned int ofs = get_amp_offset(kcontrol);
2022 bool min_mute = get_amp_min_mute(kcontrol);
2023 u32 caps, val1, val2;
2025 if (size < 4 * sizeof(unsigned int))
2027 caps = query_amp_caps(codec, nid, dir);
2028 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2029 val2 = (val2 + 1) * 25;
2030 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
2032 val1 = ((int)val1) * ((int)val2);
2033 if (min_mute || (caps & AC_AMPCAP_MIN_MUTE))
2034 val2 |= TLV_DB_SCALE_MUTE;
2035 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
2037 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
2039 if (put_user(val1, _tlv + 2))
2041 if (put_user(val2, _tlv + 3))
2045 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_tlv);
2048 * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control
2049 * @codec: HD-audio codec
2050 * @nid: NID of a reference widget
2051 * @dir: #HDA_INPUT or #HDA_OUTPUT
2052 * @tlv: TLV data to be stored, at least 4 elements
2054 * Set (static) TLV data for a virtual master volume using the AMP caps
2055 * obtained from the reference NID.
2056 * The volume range is recalculated as if the max volume is 0dB.
2058 void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
2064 caps = query_amp_caps(codec, nid, dir);
2065 nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
2066 step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2067 step = (step + 1) * 25;
2068 tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
2069 tlv[1] = 2 * sizeof(unsigned int);
2070 tlv[2] = -nums * step;
2073 EXPORT_SYMBOL_HDA(snd_hda_set_vmaster_tlv);
2075 /* find a mixer control element with the given name */
2076 static struct snd_kcontrol *
2077 _snd_hda_find_mixer_ctl(struct hda_codec *codec,
2078 const char *name, int idx)
2080 struct snd_ctl_elem_id id;
2081 memset(&id, 0, sizeof(id));
2082 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
2084 if (snd_BUG_ON(strlen(name) >= sizeof(id.name)))
2086 strcpy(id.name, name);
2087 return snd_ctl_find_id(codec->bus->card, &id);
2091 * snd_hda_find_mixer_ctl - Find a mixer control element with the given name
2092 * @codec: HD-audio codec
2093 * @name: ctl id name string
2095 * Get the control element with the given id string and IFACE_MIXER.
2097 struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
2100 return _snd_hda_find_mixer_ctl(codec, name, 0);
2102 EXPORT_SYMBOL_HDA(snd_hda_find_mixer_ctl);
2104 static int find_empty_mixer_ctl_idx(struct hda_codec *codec, const char *name)
2107 for (idx = 0; idx < 16; idx++) { /* 16 ctlrs should be large enough */
2108 if (!_snd_hda_find_mixer_ctl(codec, name, idx))
2115 * snd_hda_ctl_add - Add a control element and assign to the codec
2116 * @codec: HD-audio codec
2117 * @nid: corresponding NID (optional)
2118 * @kctl: the control element to assign
2120 * Add the given control element to an array inside the codec instance.
2121 * All control elements belonging to a codec are supposed to be added
2122 * by this function so that a proper clean-up works at the free or
2123 * reconfiguration time.
2125 * If non-zero @nid is passed, the NID is assigned to the control element.
2126 * The assignment is shown in the codec proc file.
2128 * snd_hda_ctl_add() checks the control subdev id field whether
2129 * #HDA_SUBDEV_NID_FLAG bit is set. If set (and @nid is zero), the lower
2130 * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit
2131 * specifies if kctl->private_value is a HDA amplifier value.
2133 int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid,
2134 struct snd_kcontrol *kctl)
2137 unsigned short flags = 0;
2138 struct hda_nid_item *item;
2140 if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) {
2141 flags |= HDA_NID_ITEM_AMP;
2143 nid = get_amp_nid_(kctl->private_value);
2145 if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0)
2146 nid = kctl->id.subdevice & 0xffff;
2147 if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG))
2148 kctl->id.subdevice = 0;
2149 err = snd_ctl_add(codec->bus->card, kctl);
2152 item = snd_array_new(&codec->mixers);
2157 item->flags = flags;
2160 EXPORT_SYMBOL_HDA(snd_hda_ctl_add);
2163 * snd_hda_add_nid - Assign a NID to a control element
2164 * @codec: HD-audio codec
2165 * @nid: corresponding NID (optional)
2166 * @kctl: the control element to assign
2167 * @index: index to kctl
2169 * Add the given control element to an array inside the codec instance.
2170 * This function is used when #snd_hda_ctl_add cannot be used for 1:1
2171 * NID:KCTL mapping - for example "Capture Source" selector.
2173 int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl,
2174 unsigned int index, hda_nid_t nid)
2176 struct hda_nid_item *item;
2179 item = snd_array_new(&codec->nids);
2183 item->index = index;
2187 printk(KERN_ERR "hda-codec: no NID for mapping control %s:%d:%d\n",
2188 kctl->id.name, kctl->id.index, index);
2191 EXPORT_SYMBOL_HDA(snd_hda_add_nid);
2194 * snd_hda_ctls_clear - Clear all controls assigned to the given codec
2195 * @codec: HD-audio codec
2197 void snd_hda_ctls_clear(struct hda_codec *codec)
2200 struct hda_nid_item *items = codec->mixers.list;
2201 for (i = 0; i < codec->mixers.used; i++)
2202 snd_ctl_remove(codec->bus->card, items[i].kctl);
2203 snd_array_free(&codec->mixers);
2204 snd_array_free(&codec->nids);
2207 /* pseudo device locking
2208 * toggle card->shutdown to allow/disallow the device access (as a hack)
2210 static int hda_lock_devices(struct snd_card *card)
2212 spin_lock(&card->files_lock);
2213 if (card->shutdown) {
2214 spin_unlock(&card->files_lock);
2218 spin_unlock(&card->files_lock);
2222 static void hda_unlock_devices(struct snd_card *card)
2224 spin_lock(&card->files_lock);
2226 spin_unlock(&card->files_lock);
2230 * snd_hda_codec_reset - Clear all objects assigned to the codec
2231 * @codec: HD-audio codec
2233 * This frees the all PCM and control elements assigned to the codec, and
2234 * clears the caches and restores the pin default configurations.
2236 * When a device is being used, it returns -EBSY. If successfully freed,
2239 int snd_hda_codec_reset(struct hda_codec *codec)
2241 struct snd_card *card = codec->bus->card;
2244 if (hda_lock_devices(card) < 0)
2246 /* check whether the codec isn't used by any mixer or PCM streams */
2247 if (!list_empty(&card->ctl_files)) {
2248 hda_unlock_devices(card);
2251 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
2252 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
2255 if (cpcm->pcm->streams[0].substream_opened ||
2256 cpcm->pcm->streams[1].substream_opened) {
2257 hda_unlock_devices(card);
2262 /* OK, let it free */
2264 #ifdef CONFIG_SND_HDA_POWER_SAVE
2265 cancel_delayed_work(&codec->power_work);
2266 flush_workqueue(codec->bus->workq);
2268 snd_hda_ctls_clear(codec);
2270 for (i = 0; i < codec->num_pcms; i++) {
2271 if (codec->pcm_info[i].pcm) {
2272 snd_device_free(card, codec->pcm_info[i].pcm);
2273 clear_bit(codec->pcm_info[i].device,
2274 codec->bus->pcm_dev_bits);
2277 if (codec->patch_ops.free)
2278 codec->patch_ops.free(codec);
2279 snd_hda_jack_tbl_clear(codec);
2280 codec->proc_widget_hook = NULL;
2282 free_hda_cache(&codec->amp_cache);
2283 free_hda_cache(&codec->cmd_cache);
2284 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
2285 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
2286 /* free only driver_pins so that init_pins + user_pins are restored */
2287 snd_array_free(&codec->driver_pins);
2288 restore_pincfgs(codec);
2289 codec->num_pcms = 0;
2290 codec->pcm_info = NULL;
2291 codec->preset = NULL;
2292 memset(&codec->patch_ops, 0, sizeof(codec->patch_ops));
2293 codec->slave_dig_outs = NULL;
2294 codec->spdif_status_reset = 0;
2295 module_put(codec->owner);
2296 codec->owner = NULL;
2298 /* allow device access again */
2299 hda_unlock_devices(card);
2303 typedef int (*map_slave_func_t)(void *, struct snd_kcontrol *);
2305 /* apply the function to all matching slave ctls in the mixer list */
2306 static int map_slaves(struct hda_codec *codec, const char * const *slaves,
2307 map_slave_func_t func, void *data)
2309 struct hda_nid_item *items;
2310 const char * const *s;
2313 items = codec->mixers.list;
2314 for (i = 0; i < codec->mixers.used; i++) {
2315 struct snd_kcontrol *sctl = items[i].kctl;
2316 if (!sctl || !sctl->id.name ||
2317 sctl->id.iface != SNDRV_CTL_ELEM_IFACE_MIXER)
2319 for (s = slaves; *s; s++) {
2320 if (!strcmp(sctl->id.name, *s)) {
2321 err = func(data, sctl);
2331 static int check_slave_present(void *data, struct snd_kcontrol *sctl)
2337 * snd_hda_add_vmaster - create a virtual master control and add slaves
2338 * @codec: HD-audio codec
2339 * @name: vmaster control name
2340 * @tlv: TLV data (optional)
2341 * @slaves: slave control names (optional)
2343 * Create a virtual master control with the given name. The TLV data
2344 * must be either NULL or a valid data.
2346 * @slaves is a NULL-terminated array of strings, each of which is a
2347 * slave control name. All controls with these names are assigned to
2348 * the new virtual master control.
2350 * This function returns zero if successful or a negative error code.
2352 int snd_hda_add_vmaster(struct hda_codec *codec, char *name,
2353 unsigned int *tlv, const char * const *slaves)
2355 struct snd_kcontrol *kctl;
2358 err = map_slaves(codec, slaves, check_slave_present, NULL);
2360 snd_printdd("No slave found for %s\n", name);
2363 kctl = snd_ctl_make_virtual_master(name, tlv);
2366 err = snd_hda_ctl_add(codec, 0, kctl);
2370 err = map_slaves(codec, slaves, (map_slave_func_t)snd_ctl_add_slave,
2376 EXPORT_SYMBOL_HDA(snd_hda_add_vmaster);
2379 * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch
2381 * The control element is supposed to have the private_value field
2382 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2384 int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
2385 struct snd_ctl_elem_info *uinfo)
2387 int chs = get_amp_channels(kcontrol);
2389 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2390 uinfo->count = chs == 3 ? 2 : 1;
2391 uinfo->value.integer.min = 0;
2392 uinfo->value.integer.max = 1;
2395 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_info);
2398 * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch
2400 * The control element is supposed to have the private_value field
2401 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2403 int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
2404 struct snd_ctl_elem_value *ucontrol)
2406 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2407 hda_nid_t nid = get_amp_nid(kcontrol);
2408 int chs = get_amp_channels(kcontrol);
2409 int dir = get_amp_direction(kcontrol);
2410 int idx = get_amp_index(kcontrol);
2411 long *valp = ucontrol->value.integer.value;
2414 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
2415 HDA_AMP_MUTE) ? 0 : 1;
2417 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
2418 HDA_AMP_MUTE) ? 0 : 1;
2421 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_get);
2424 * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch
2426 * The control element is supposed to have the private_value field
2427 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2429 int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
2430 struct snd_ctl_elem_value *ucontrol)
2432 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2433 hda_nid_t nid = get_amp_nid(kcontrol);
2434 int chs = get_amp_channels(kcontrol);
2435 int dir = get_amp_direction(kcontrol);
2436 int idx = get_amp_index(kcontrol);
2437 long *valp = ucontrol->value.integer.value;
2440 snd_hda_power_up(codec);
2442 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
2444 *valp ? 0 : HDA_AMP_MUTE);
2448 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
2450 *valp ? 0 : HDA_AMP_MUTE);
2451 hda_call_check_power_status(codec, nid);
2452 snd_hda_power_down(codec);
2455 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put);
2457 #ifdef CONFIG_SND_HDA_INPUT_BEEP
2459 * snd_hda_mixer_amp_switch_put_beep - Put callback for a beep AMP switch
2461 * This function calls snd_hda_enable_beep_device(), which behaves differently
2462 * depending on beep_mode option.
2464 int snd_hda_mixer_amp_switch_put_beep(struct snd_kcontrol *kcontrol,
2465 struct snd_ctl_elem_value *ucontrol)
2467 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2468 long *valp = ucontrol->value.integer.value;
2470 snd_hda_enable_beep_device(codec, *valp);
2471 return snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2473 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put_beep);
2474 #endif /* CONFIG_SND_HDA_INPUT_BEEP */
2477 * bound volume controls
2479 * bind multiple volumes (# indices, from 0)
2482 #define AMP_VAL_IDX_SHIFT 19
2483 #define AMP_VAL_IDX_MASK (0x0f<<19)
2486 * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control
2488 * The control element is supposed to have the private_value field
2489 * set up via HDA_BIND_MUTE*() macros.
2491 int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
2492 struct snd_ctl_elem_value *ucontrol)
2494 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2498 mutex_lock(&codec->control_mutex);
2499 pval = kcontrol->private_value;
2500 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
2501 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
2502 kcontrol->private_value = pval;
2503 mutex_unlock(&codec->control_mutex);
2506 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_get);
2509 * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control
2511 * The control element is supposed to have the private_value field
2512 * set up via HDA_BIND_MUTE*() macros.
2514 int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
2515 struct snd_ctl_elem_value *ucontrol)
2517 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2519 int i, indices, err = 0, change = 0;
2521 mutex_lock(&codec->control_mutex);
2522 pval = kcontrol->private_value;
2523 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
2524 for (i = 0; i < indices; i++) {
2525 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
2526 (i << AMP_VAL_IDX_SHIFT);
2527 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2532 kcontrol->private_value = pval;
2533 mutex_unlock(&codec->control_mutex);
2534 return err < 0 ? err : change;
2536 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_put);
2539 * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control
2541 * The control element is supposed to have the private_value field
2542 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2544 int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
2545 struct snd_ctl_elem_info *uinfo)
2547 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2548 struct hda_bind_ctls *c;
2551 mutex_lock(&codec->control_mutex);
2552 c = (struct hda_bind_ctls *)kcontrol->private_value;
2553 kcontrol->private_value = *c->values;
2554 err = c->ops->info(kcontrol, uinfo);
2555 kcontrol->private_value = (long)c;
2556 mutex_unlock(&codec->control_mutex);
2559 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_info);
2562 * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control
2564 * The control element is supposed to have the private_value field
2565 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2567 int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
2568 struct snd_ctl_elem_value *ucontrol)
2570 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2571 struct hda_bind_ctls *c;
2574 mutex_lock(&codec->control_mutex);
2575 c = (struct hda_bind_ctls *)kcontrol->private_value;
2576 kcontrol->private_value = *c->values;
2577 err = c->ops->get(kcontrol, ucontrol);
2578 kcontrol->private_value = (long)c;
2579 mutex_unlock(&codec->control_mutex);
2582 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_get);
2585 * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control
2587 * The control element is supposed to have the private_value field
2588 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2590 int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
2591 struct snd_ctl_elem_value *ucontrol)
2593 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2594 struct hda_bind_ctls *c;
2595 unsigned long *vals;
2596 int err = 0, change = 0;
2598 mutex_lock(&codec->control_mutex);
2599 c = (struct hda_bind_ctls *)kcontrol->private_value;
2600 for (vals = c->values; *vals; vals++) {
2601 kcontrol->private_value = *vals;
2602 err = c->ops->put(kcontrol, ucontrol);
2607 kcontrol->private_value = (long)c;
2608 mutex_unlock(&codec->control_mutex);
2609 return err < 0 ? err : change;
2611 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_put);
2614 * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control
2616 * The control element is supposed to have the private_value field
2617 * set up via HDA_BIND_VOL() macro.
2619 int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2620 unsigned int size, unsigned int __user *tlv)
2622 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2623 struct hda_bind_ctls *c;
2626 mutex_lock(&codec->control_mutex);
2627 c = (struct hda_bind_ctls *)kcontrol->private_value;
2628 kcontrol->private_value = *c->values;
2629 err = c->ops->tlv(kcontrol, op_flag, size, tlv);
2630 kcontrol->private_value = (long)c;
2631 mutex_unlock(&codec->control_mutex);
2634 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_tlv);
2636 struct hda_ctl_ops snd_hda_bind_vol = {
2637 .info = snd_hda_mixer_amp_volume_info,
2638 .get = snd_hda_mixer_amp_volume_get,
2639 .put = snd_hda_mixer_amp_volume_put,
2640 .tlv = snd_hda_mixer_amp_tlv
2642 EXPORT_SYMBOL_HDA(snd_hda_bind_vol);
2644 struct hda_ctl_ops snd_hda_bind_sw = {
2645 .info = snd_hda_mixer_amp_switch_info,
2646 .get = snd_hda_mixer_amp_switch_get,
2647 .put = snd_hda_mixer_amp_switch_put,
2648 .tlv = snd_hda_mixer_amp_tlv
2650 EXPORT_SYMBOL_HDA(snd_hda_bind_sw);
2653 * SPDIF out controls
2656 static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
2657 struct snd_ctl_elem_info *uinfo)
2659 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
2664 static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
2665 struct snd_ctl_elem_value *ucontrol)
2667 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2668 IEC958_AES0_NONAUDIO |
2669 IEC958_AES0_CON_EMPHASIS_5015 |
2670 IEC958_AES0_CON_NOT_COPYRIGHT;
2671 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
2672 IEC958_AES1_CON_ORIGINAL;
2676 static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
2677 struct snd_ctl_elem_value *ucontrol)
2679 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2680 IEC958_AES0_NONAUDIO |
2681 IEC958_AES0_PRO_EMPHASIS_5015;
2685 static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
2686 struct snd_ctl_elem_value *ucontrol)
2688 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2689 int idx = kcontrol->private_value;
2690 struct hda_spdif_out *spdif = snd_array_elem(&codec->spdif_out, idx);
2692 ucontrol->value.iec958.status[0] = spdif->status & 0xff;
2693 ucontrol->value.iec958.status[1] = (spdif->status >> 8) & 0xff;
2694 ucontrol->value.iec958.status[2] = (spdif->status >> 16) & 0xff;
2695 ucontrol->value.iec958.status[3] = (spdif->status >> 24) & 0xff;
2700 /* convert from SPDIF status bits to HDA SPDIF bits
2701 * bit 0 (DigEn) is always set zero (to be filled later)
2703 static unsigned short convert_from_spdif_status(unsigned int sbits)
2705 unsigned short val = 0;
2707 if (sbits & IEC958_AES0_PROFESSIONAL)
2708 val |= AC_DIG1_PROFESSIONAL;
2709 if (sbits & IEC958_AES0_NONAUDIO)
2710 val |= AC_DIG1_NONAUDIO;
2711 if (sbits & IEC958_AES0_PROFESSIONAL) {
2712 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
2713 IEC958_AES0_PRO_EMPHASIS_5015)
2714 val |= AC_DIG1_EMPHASIS;
2716 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
2717 IEC958_AES0_CON_EMPHASIS_5015)
2718 val |= AC_DIG1_EMPHASIS;
2719 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
2720 val |= AC_DIG1_COPYRIGHT;
2721 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
2722 val |= AC_DIG1_LEVEL;
2723 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
2728 /* convert to SPDIF status bits from HDA SPDIF bits
2730 static unsigned int convert_to_spdif_status(unsigned short val)
2732 unsigned int sbits = 0;
2734 if (val & AC_DIG1_NONAUDIO)
2735 sbits |= IEC958_AES0_NONAUDIO;
2736 if (val & AC_DIG1_PROFESSIONAL)
2737 sbits |= IEC958_AES0_PROFESSIONAL;
2738 if (sbits & IEC958_AES0_PROFESSIONAL) {
2739 if (sbits & AC_DIG1_EMPHASIS)
2740 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
2742 if (val & AC_DIG1_EMPHASIS)
2743 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
2744 if (!(val & AC_DIG1_COPYRIGHT))
2745 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
2746 if (val & AC_DIG1_LEVEL)
2747 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
2748 sbits |= val & (0x7f << 8);
2753 /* set digital convert verbs both for the given NID and its slaves */
2754 static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
2759 snd_hda_codec_write_cache(codec, nid, 0, verb, val);
2760 d = codec->slave_dig_outs;
2764 snd_hda_codec_write_cache(codec, *d, 0, verb, val);
2767 static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
2771 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1);
2773 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2);
2776 static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
2777 struct snd_ctl_elem_value *ucontrol)
2779 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2780 int idx = kcontrol->private_value;
2781 struct hda_spdif_out *spdif = snd_array_elem(&codec->spdif_out, idx);
2782 hda_nid_t nid = spdif->nid;
2786 mutex_lock(&codec->spdif_mutex);
2787 spdif->status = ucontrol->value.iec958.status[0] |
2788 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
2789 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
2790 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
2791 val = convert_from_spdif_status(spdif->status);
2792 val |= spdif->ctls & 1;
2793 change = spdif->ctls != val;
2795 if (change && nid != (u16)-1)
2796 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
2797 mutex_unlock(&codec->spdif_mutex);
2801 #define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
2803 static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
2804 struct snd_ctl_elem_value *ucontrol)
2806 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2807 int idx = kcontrol->private_value;
2808 struct hda_spdif_out *spdif = snd_array_elem(&codec->spdif_out, idx);
2810 ucontrol->value.integer.value[0] = spdif->ctls & AC_DIG1_ENABLE;
2814 static inline void set_spdif_ctls(struct hda_codec *codec, hda_nid_t nid,
2817 set_dig_out_convert(codec, nid, dig1, dig2);
2818 /* unmute amp switch (if any) */
2819 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
2820 (dig1 & AC_DIG1_ENABLE))
2821 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
2825 static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
2826 struct snd_ctl_elem_value *ucontrol)
2828 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2829 int idx = kcontrol->private_value;
2830 struct hda_spdif_out *spdif = snd_array_elem(&codec->spdif_out, idx);
2831 hda_nid_t nid = spdif->nid;
2835 mutex_lock(&codec->spdif_mutex);
2836 val = spdif->ctls & ~AC_DIG1_ENABLE;
2837 if (ucontrol->value.integer.value[0])
2838 val |= AC_DIG1_ENABLE;
2839 change = spdif->ctls != val;
2841 if (change && nid != (u16)-1)
2842 set_spdif_ctls(codec, nid, val & 0xff, -1);
2843 mutex_unlock(&codec->spdif_mutex);
2847 static struct snd_kcontrol_new dig_mixes[] = {
2849 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2850 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2851 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
2852 .info = snd_hda_spdif_mask_info,
2853 .get = snd_hda_spdif_cmask_get,
2856 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2857 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2858 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK),
2859 .info = snd_hda_spdif_mask_info,
2860 .get = snd_hda_spdif_pmask_get,
2863 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2864 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
2865 .info = snd_hda_spdif_mask_info,
2866 .get = snd_hda_spdif_default_get,
2867 .put = snd_hda_spdif_default_put,
2870 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2871 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
2872 .info = snd_hda_spdif_out_switch_info,
2873 .get = snd_hda_spdif_out_switch_get,
2874 .put = snd_hda_spdif_out_switch_put,
2880 * snd_hda_create_spdif_out_ctls - create Output SPDIF-related controls
2881 * @codec: the HDA codec
2882 * @nid: audio out widget NID
2884 * Creates controls related with the SPDIF output.
2885 * Called from each patch supporting the SPDIF out.
2887 * Returns 0 if successful, or a negative error code.
2889 int snd_hda_create_spdif_out_ctls(struct hda_codec *codec,
2890 hda_nid_t associated_nid,
2894 struct snd_kcontrol *kctl;
2895 struct snd_kcontrol_new *dig_mix;
2897 struct hda_spdif_out *spdif;
2899 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Playback Switch");
2901 printk(KERN_ERR "hda_codec: too many IEC958 outputs\n");
2904 spdif = snd_array_new(&codec->spdif_out);
2905 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
2906 kctl = snd_ctl_new1(dig_mix, codec);
2909 kctl->id.index = idx;
2910 kctl->private_value = codec->spdif_out.used - 1;
2911 err = snd_hda_ctl_add(codec, associated_nid, kctl);
2915 spdif->nid = cvt_nid;
2916 spdif->ctls = snd_hda_codec_read(codec, cvt_nid, 0,
2917 AC_VERB_GET_DIGI_CONVERT_1, 0);
2918 spdif->status = convert_to_spdif_status(spdif->ctls);
2921 EXPORT_SYMBOL_HDA(snd_hda_create_spdif_out_ctls);
2923 struct hda_spdif_out *snd_hda_spdif_out_of_nid(struct hda_codec *codec,
2927 for (i = 0; i < codec->spdif_out.used; i++) {
2928 struct hda_spdif_out *spdif =
2929 snd_array_elem(&codec->spdif_out, i);
2930 if (spdif->nid == nid)
2935 EXPORT_SYMBOL_HDA(snd_hda_spdif_out_of_nid);
2937 void snd_hda_spdif_ctls_unassign(struct hda_codec *codec, int idx)
2939 struct hda_spdif_out *spdif = snd_array_elem(&codec->spdif_out, idx);
2941 mutex_lock(&codec->spdif_mutex);
2942 spdif->nid = (u16)-1;
2943 mutex_unlock(&codec->spdif_mutex);
2945 EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_unassign);
2947 void snd_hda_spdif_ctls_assign(struct hda_codec *codec, int idx, hda_nid_t nid)
2949 struct hda_spdif_out *spdif = snd_array_elem(&codec->spdif_out, idx);
2952 mutex_lock(&codec->spdif_mutex);
2953 if (spdif->nid != nid) {
2956 set_spdif_ctls(codec, nid, val & 0xff, (val >> 8) & 0xff);
2958 mutex_unlock(&codec->spdif_mutex);
2960 EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_assign);
2963 * SPDIF sharing with analog output
2965 static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
2966 struct snd_ctl_elem_value *ucontrol)
2968 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
2969 ucontrol->value.integer.value[0] = mout->share_spdif;
2973 static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
2974 struct snd_ctl_elem_value *ucontrol)
2976 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
2977 mout->share_spdif = !!ucontrol->value.integer.value[0];
2981 static struct snd_kcontrol_new spdif_share_sw = {
2982 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2983 .name = "IEC958 Default PCM Playback Switch",
2984 .info = snd_ctl_boolean_mono_info,
2985 .get = spdif_share_sw_get,
2986 .put = spdif_share_sw_put,
2990 * snd_hda_create_spdif_share_sw - create Default PCM switch
2991 * @codec: the HDA codec
2992 * @mout: multi-out instance
2994 int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
2995 struct hda_multi_out *mout)
2997 if (!mout->dig_out_nid)
2999 /* ATTENTION: here mout is passed as private_data, instead of codec */
3000 return snd_hda_ctl_add(codec, mout->dig_out_nid,
3001 snd_ctl_new1(&spdif_share_sw, mout));
3003 EXPORT_SYMBOL_HDA(snd_hda_create_spdif_share_sw);
3009 #define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
3011 static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
3012 struct snd_ctl_elem_value *ucontrol)
3014 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3016 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
3020 static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
3021 struct snd_ctl_elem_value *ucontrol)
3023 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3024 hda_nid_t nid = kcontrol->private_value;
3025 unsigned int val = !!ucontrol->value.integer.value[0];
3028 mutex_lock(&codec->spdif_mutex);
3029 change = codec->spdif_in_enable != val;
3031 codec->spdif_in_enable = val;
3032 snd_hda_codec_write_cache(codec, nid, 0,
3033 AC_VERB_SET_DIGI_CONVERT_1, val);
3035 mutex_unlock(&codec->spdif_mutex);
3039 static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
3040 struct snd_ctl_elem_value *ucontrol)
3042 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3043 hda_nid_t nid = kcontrol->private_value;
3047 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0);
3048 sbits = convert_to_spdif_status(val);
3049 ucontrol->value.iec958.status[0] = sbits;
3050 ucontrol->value.iec958.status[1] = sbits >> 8;
3051 ucontrol->value.iec958.status[2] = sbits >> 16;
3052 ucontrol->value.iec958.status[3] = sbits >> 24;
3056 static struct snd_kcontrol_new dig_in_ctls[] = {
3058 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3059 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH),
3060 .info = snd_hda_spdif_in_switch_info,
3061 .get = snd_hda_spdif_in_switch_get,
3062 .put = snd_hda_spdif_in_switch_put,
3065 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3066 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3067 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
3068 .info = snd_hda_spdif_mask_info,
3069 .get = snd_hda_spdif_in_status_get,
3075 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
3076 * @codec: the HDA codec
3077 * @nid: audio in widget NID
3079 * Creates controls related with the SPDIF input.
3080 * Called from each patch supporting the SPDIF in.
3082 * Returns 0 if successful, or a negative error code.
3084 int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
3087 struct snd_kcontrol *kctl;
3088 struct snd_kcontrol_new *dig_mix;
3091 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Capture Switch");
3093 printk(KERN_ERR "hda_codec: too many IEC958 inputs\n");
3096 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
3097 kctl = snd_ctl_new1(dig_mix, codec);
3100 kctl->private_value = nid;
3101 err = snd_hda_ctl_add(codec, nid, kctl);
3105 codec->spdif_in_enable =
3106 snd_hda_codec_read(codec, nid, 0,
3107 AC_VERB_GET_DIGI_CONVERT_1, 0) &
3111 EXPORT_SYMBOL_HDA(snd_hda_create_spdif_in_ctls);
3118 /* build a 32bit cache key with the widget id and the command parameter */
3119 #define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
3120 #define get_cmd_cache_nid(key) ((key) & 0xff)
3121 #define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
3124 * snd_hda_codec_write_cache - send a single command with caching
3125 * @codec: the HDA codec
3126 * @nid: NID to send the command
3127 * @direct: direct flag
3128 * @verb: the verb to send
3129 * @parm: the parameter for the verb
3131 * Send a single command without waiting for response.
3133 * Returns 0 if successful, or a negative error code.
3135 int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
3136 int direct, unsigned int verb, unsigned int parm)
3138 int err = snd_hda_codec_write(codec, nid, direct, verb, parm);
3139 struct hda_cache_head *c;
3144 /* parm may contain the verb stuff for get/set amp */
3145 verb = verb | (parm >> 8);
3147 key = build_cmd_cache_key(nid, verb);
3148 mutex_lock(&codec->bus->cmd_mutex);
3149 c = get_alloc_hash(&codec->cmd_cache, key);
3152 mutex_unlock(&codec->bus->cmd_mutex);
3155 EXPORT_SYMBOL_HDA(snd_hda_codec_write_cache);
3158 * snd_hda_codec_update_cache - check cache and write the cmd only when needed
3159 * @codec: the HDA codec
3160 * @nid: NID to send the command
3161 * @direct: direct flag
3162 * @verb: the verb to send
3163 * @parm: the parameter for the verb
3165 * This function works like snd_hda_codec_write_cache(), but it doesn't send
3166 * command if the parameter is already identical with the cached value.
3167 * If not, it sends the command and refreshes the cache.
3169 * Returns 0 if successful, or a negative error code.
3171 int snd_hda_codec_update_cache(struct hda_codec *codec, hda_nid_t nid,
3172 int direct, unsigned int verb, unsigned int parm)
3174 struct hda_cache_head *c;
3177 /* parm may contain the verb stuff for get/set amp */
3178 verb = verb | (parm >> 8);
3180 key = build_cmd_cache_key(nid, verb);
3181 mutex_lock(&codec->bus->cmd_mutex);
3182 c = get_hash(&codec->cmd_cache, key);
3183 if (c && c->val == parm) {
3184 mutex_unlock(&codec->bus->cmd_mutex);
3187 mutex_unlock(&codec->bus->cmd_mutex);
3188 return snd_hda_codec_write_cache(codec, nid, direct, verb, parm);
3190 EXPORT_SYMBOL_HDA(snd_hda_codec_update_cache);
3193 * snd_hda_codec_resume_cache - Resume the all commands from the cache
3194 * @codec: HD-audio codec
3196 * Execute all verbs recorded in the command caches to resume.
3198 void snd_hda_codec_resume_cache(struct hda_codec *codec)
3200 struct hda_cache_head *buffer = codec->cmd_cache.buf.list;
3203 for (i = 0; i < codec->cmd_cache.buf.used; i++, buffer++) {
3204 u32 key = buffer->key;
3207 snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
3208 get_cmd_cache_cmd(key), buffer->val);
3211 EXPORT_SYMBOL_HDA(snd_hda_codec_resume_cache);
3214 * snd_hda_sequence_write_cache - sequence writes with caching
3215 * @codec: the HDA codec
3216 * @seq: VERB array to send
3218 * Send the commands sequentially from the given array.
3219 * Thte commands are recorded on cache for power-save and resume.
3220 * The array must be terminated with NID=0.
3222 void snd_hda_sequence_write_cache(struct hda_codec *codec,
3223 const struct hda_verb *seq)
3225 for (; seq->nid; seq++)
3226 snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
3229 EXPORT_SYMBOL_HDA(snd_hda_sequence_write_cache);
3230 #endif /* CONFIG_PM */
3232 void snd_hda_codec_set_power_to_all(struct hda_codec *codec, hda_nid_t fg,
3233 unsigned int power_state,
3234 bool eapd_workaround)
3236 hda_nid_t nid = codec->start_nid;
3239 for (i = 0; i < codec->num_nodes; i++, nid++) {
3240 unsigned int wcaps = get_wcaps(codec, nid);
3241 if (!(wcaps & AC_WCAP_POWER))
3243 /* don't power down the widget if it controls eapd and
3244 * EAPD_BTLENABLE is set.
3246 if (eapd_workaround && power_state == AC_PWRST_D3 &&
3247 get_wcaps_type(wcaps) == AC_WID_PIN &&
3248 (snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD)) {
3249 int eapd = snd_hda_codec_read(codec, nid, 0,
3250 AC_VERB_GET_EAPD_BTLENABLE, 0);
3254 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE,
3258 if (power_state == AC_PWRST_D0) {
3259 unsigned long end_time;
3261 /* wait until the codec reachs to D0 */
3262 end_time = jiffies + msecs_to_jiffies(500);
3264 state = snd_hda_codec_read(codec, fg, 0,
3265 AC_VERB_GET_POWER_STATE, 0);
3266 if (state == power_state)
3269 } while (time_after_eq(end_time, jiffies));
3272 EXPORT_SYMBOL_HDA(snd_hda_codec_set_power_to_all);
3275 * set power state of the codec
3277 static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
3278 unsigned int power_state)
3280 if (codec->patch_ops.set_power_state) {
3281 codec->patch_ops.set_power_state(codec, fg, power_state);
3285 /* this delay seems necessary to avoid click noise at power-down */
3286 if (power_state == AC_PWRST_D3)
3288 snd_hda_codec_read(codec, fg, 0, AC_VERB_SET_POWER_STATE,
3290 snd_hda_codec_set_power_to_all(codec, fg, power_state, true);
3293 #ifdef CONFIG_SND_HDA_HWDEP
3294 /* execute additional init verbs */
3295 static void hda_exec_init_verbs(struct hda_codec *codec)
3297 if (codec->init_verbs.list)
3298 snd_hda_sequence_write(codec, codec->init_verbs.list);
3301 static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
3306 * call suspend and power-down; used both from PM and power-save
3308 static void hda_call_codec_suspend(struct hda_codec *codec)
3310 if (codec->patch_ops.suspend)
3311 codec->patch_ops.suspend(codec, PMSG_SUSPEND);
3312 hda_cleanup_all_streams(codec);
3313 hda_set_power_state(codec,
3314 codec->afg ? codec->afg : codec->mfg,
3316 #ifdef CONFIG_SND_HDA_POWER_SAVE
3317 snd_hda_update_power_acct(codec);
3318 cancel_delayed_work(&codec->power_work);
3319 codec->power_on = 0;
3320 codec->power_transition = 0;
3321 codec->power_jiffies = jiffies;
3326 * kick up codec; used both from PM and power-save
3328 static void hda_call_codec_resume(struct hda_codec *codec)
3330 hda_set_power_state(codec,
3331 codec->afg ? codec->afg : codec->mfg,
3333 restore_pincfgs(codec); /* restore all current pin configs */
3334 restore_shutup_pins(codec);
3335 hda_exec_init_verbs(codec);
3336 snd_hda_jack_set_dirty_all(codec);
3337 if (codec->patch_ops.resume)
3338 codec->patch_ops.resume(codec);
3340 if (codec->patch_ops.init)
3341 codec->patch_ops.init(codec);
3342 snd_hda_codec_resume_amp(codec);
3343 snd_hda_codec_resume_cache(codec);
3346 #endif /* CONFIG_PM */
3350 * snd_hda_build_controls - build mixer controls
3353 * Creates mixer controls for each codec included in the bus.
3355 * Returns 0 if successful, otherwise a negative error code.
3357 int /*__devinit*/ snd_hda_build_controls(struct hda_bus *bus)
3359 struct hda_codec *codec;
3361 list_for_each_entry(codec, &bus->codec_list, list) {
3362 int err = snd_hda_codec_build_controls(codec);
3364 printk(KERN_ERR "hda_codec: cannot build controls "
3365 "for #%d (error %d)\n", codec->addr, err);
3366 err = snd_hda_codec_reset(codec);
3369 "hda_codec: cannot revert codec\n");
3376 EXPORT_SYMBOL_HDA(snd_hda_build_controls);
3378 int snd_hda_codec_build_controls(struct hda_codec *codec)
3381 hda_exec_init_verbs(codec);
3382 /* continue to initialize... */
3383 if (codec->patch_ops.init)
3384 err = codec->patch_ops.init(codec);
3385 if (!err && codec->patch_ops.build_controls)
3386 err = codec->patch_ops.build_controls(codec);
3395 struct hda_rate_tbl {
3397 unsigned int alsa_bits;
3398 unsigned int hda_fmt;
3401 /* rate = base * mult / div */
3402 #define HDA_RATE(base, mult, div) \
3403 (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \
3404 (((div) - 1) << AC_FMT_DIV_SHIFT))
3406 static struct hda_rate_tbl rate_bits[] = {
3407 /* rate in Hz, ALSA rate bitmask, HDA format value */
3409 /* autodetected value used in snd_hda_query_supported_pcm */
3410 { 8000, SNDRV_PCM_RATE_8000, HDA_RATE(48, 1, 6) },
3411 { 11025, SNDRV_PCM_RATE_11025, HDA_RATE(44, 1, 4) },
3412 { 16000, SNDRV_PCM_RATE_16000, HDA_RATE(48, 1, 3) },
3413 { 22050, SNDRV_PCM_RATE_22050, HDA_RATE(44, 1, 2) },
3414 { 32000, SNDRV_PCM_RATE_32000, HDA_RATE(48, 2, 3) },
3415 { 44100, SNDRV_PCM_RATE_44100, HDA_RATE(44, 1, 1) },
3416 { 48000, SNDRV_PCM_RATE_48000, HDA_RATE(48, 1, 1) },
3417 { 88200, SNDRV_PCM_RATE_88200, HDA_RATE(44, 2, 1) },
3418 { 96000, SNDRV_PCM_RATE_96000, HDA_RATE(48, 2, 1) },
3419 { 176400, SNDRV_PCM_RATE_176400, HDA_RATE(44, 4, 1) },
3420 { 192000, SNDRV_PCM_RATE_192000, HDA_RATE(48, 4, 1) },
3421 #define AC_PAR_PCM_RATE_BITS 11
3422 /* up to bits 10, 384kHZ isn't supported properly */
3424 /* not autodetected value */
3425 { 9600, SNDRV_PCM_RATE_KNOT, HDA_RATE(48, 1, 5) },
3427 { 0 } /* terminator */
3431 * snd_hda_calc_stream_format - calculate format bitset
3432 * @rate: the sample rate
3433 * @channels: the number of channels
3434 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
3435 * @maxbps: the max. bps
3437 * Calculate the format bitset from the given rate, channels and th PCM format.
3439 * Return zero if invalid.
3441 unsigned int snd_hda_calc_stream_format(unsigned int rate,
3442 unsigned int channels,
3443 unsigned int format,
3444 unsigned int maxbps,
3445 unsigned short spdif_ctls)
3448 unsigned int val = 0;
3450 for (i = 0; rate_bits[i].hz; i++)
3451 if (rate_bits[i].hz == rate) {
3452 val = rate_bits[i].hda_fmt;
3455 if (!rate_bits[i].hz) {
3456 snd_printdd("invalid rate %d\n", rate);
3460 if (channels == 0 || channels > 8) {
3461 snd_printdd("invalid channels %d\n", channels);
3464 val |= channels - 1;
3466 switch (snd_pcm_format_width(format)) {
3468 val |= AC_FMT_BITS_8;
3471 val |= AC_FMT_BITS_16;
3476 if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE)
3477 val |= AC_FMT_BITS_32;
3478 else if (maxbps >= 24)
3479 val |= AC_FMT_BITS_24;
3481 val |= AC_FMT_BITS_20;
3484 snd_printdd("invalid format width %d\n",
3485 snd_pcm_format_width(format));
3489 if (spdif_ctls & AC_DIG1_NONAUDIO)
3490 val |= AC_FMT_TYPE_NON_PCM;
3494 EXPORT_SYMBOL_HDA(snd_hda_calc_stream_format);
3496 static unsigned int get_pcm_param(struct hda_codec *codec, hda_nid_t nid)
3498 unsigned int val = 0;
3499 if (nid != codec->afg &&
3500 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD))
3501 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
3502 if (!val || val == -1)
3503 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
3504 if (!val || val == -1)
3509 static unsigned int query_pcm_param(struct hda_codec *codec, hda_nid_t nid)
3511 return query_caps_hash(codec, nid, HDA_HASH_PARPCM_KEY(nid),
3515 static unsigned int get_stream_param(struct hda_codec *codec, hda_nid_t nid)
3517 unsigned int streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
3518 if (!streams || streams == -1)
3519 streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
3520 if (!streams || streams == -1)
3525 static unsigned int query_stream_param(struct hda_codec *codec, hda_nid_t nid)
3527 return query_caps_hash(codec, nid, HDA_HASH_PARSTR_KEY(nid),
3532 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
3533 * @codec: the HDA codec
3534 * @nid: NID to query
3535 * @ratesp: the pointer to store the detected rate bitflags
3536 * @formatsp: the pointer to store the detected formats
3537 * @bpsp: the pointer to store the detected format widths
3539 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
3540 * or @bsps argument is ignored.
3542 * Returns 0 if successful, otherwise a negative error code.
3544 int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
3545 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
3547 unsigned int i, val, wcaps;
3549 wcaps = get_wcaps(codec, nid);
3550 val = query_pcm_param(codec, nid);
3554 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
3556 rates |= rate_bits[i].alsa_bits;
3559 snd_printk(KERN_ERR "hda_codec: rates == 0 "
3560 "(nid=0x%x, val=0x%x, ovrd=%i)\n",
3562 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0);
3568 if (formatsp || bpsp) {
3570 unsigned int streams, bps;
3572 streams = query_stream_param(codec, nid);
3577 if (streams & AC_SUPFMT_PCM) {
3578 if (val & AC_SUPPCM_BITS_8) {
3579 formats |= SNDRV_PCM_FMTBIT_U8;
3582 if (val & AC_SUPPCM_BITS_16) {
3583 formats |= SNDRV_PCM_FMTBIT_S16_LE;
3586 if (wcaps & AC_WCAP_DIGITAL) {
3587 if (val & AC_SUPPCM_BITS_32)
3588 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
3589 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
3590 formats |= SNDRV_PCM_FMTBIT_S32_LE;
3591 if (val & AC_SUPPCM_BITS_24)
3593 else if (val & AC_SUPPCM_BITS_20)
3595 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
3596 AC_SUPPCM_BITS_32)) {
3597 formats |= SNDRV_PCM_FMTBIT_S32_LE;
3598 if (val & AC_SUPPCM_BITS_32)
3600 else if (val & AC_SUPPCM_BITS_24)
3602 else if (val & AC_SUPPCM_BITS_20)
3606 if (streams & AC_SUPFMT_FLOAT32) {
3607 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
3611 if (streams == AC_SUPFMT_AC3) {
3612 /* should be exclusive */
3613 /* temporary hack: we have still no proper support
3614 * for the direct AC3 stream...
3616 formats |= SNDRV_PCM_FMTBIT_U8;
3620 snd_printk(KERN_ERR "hda_codec: formats == 0 "
3621 "(nid=0x%x, val=0x%x, ovrd=%i, "
3624 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0,
3629 *formatsp = formats;
3636 EXPORT_SYMBOL_HDA(snd_hda_query_supported_pcm);
3639 * snd_hda_is_supported_format - Check the validity of the format
3640 * @codec: HD-audio codec
3641 * @nid: NID to check
3642 * @format: the HD-audio format value to check
3644 * Check whether the given node supports the format value.
3646 * Returns 1 if supported, 0 if not.
3648 int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
3649 unsigned int format)
3652 unsigned int val = 0, rate, stream;
3654 val = query_pcm_param(codec, nid);
3658 rate = format & 0xff00;
3659 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
3660 if (rate_bits[i].hda_fmt == rate) {
3665 if (i >= AC_PAR_PCM_RATE_BITS)
3668 stream = query_stream_param(codec, nid);
3672 if (stream & AC_SUPFMT_PCM) {
3673 switch (format & 0xf0) {
3675 if (!(val & AC_SUPPCM_BITS_8))
3679 if (!(val & AC_SUPPCM_BITS_16))
3683 if (!(val & AC_SUPPCM_BITS_20))
3687 if (!(val & AC_SUPPCM_BITS_24))
3691 if (!(val & AC_SUPPCM_BITS_32))
3698 /* FIXME: check for float32 and AC3? */
3703 EXPORT_SYMBOL_HDA(snd_hda_is_supported_format);
3708 static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
3709 struct hda_codec *codec,
3710 struct snd_pcm_substream *substream)
3715 static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
3716 struct hda_codec *codec,
3717 unsigned int stream_tag,
3718 unsigned int format,
3719 struct snd_pcm_substream *substream)
3721 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
3725 static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
3726 struct hda_codec *codec,
3727 struct snd_pcm_substream *substream)
3729 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
3733 static int set_pcm_default_values(struct hda_codec *codec,
3734 struct hda_pcm_stream *info)
3738 /* query support PCM information from the given NID */
3739 if (info->nid && (!info->rates || !info->formats)) {
3740 err = snd_hda_query_supported_pcm(codec, info->nid,
3741 info->rates ? NULL : &info->rates,
3742 info->formats ? NULL : &info->formats,
3743 info->maxbps ? NULL : &info->maxbps);
3747 if (info->ops.open == NULL)
3748 info->ops.open = hda_pcm_default_open_close;
3749 if (info->ops.close == NULL)
3750 info->ops.close = hda_pcm_default_open_close;
3751 if (info->ops.prepare == NULL) {
3752 if (snd_BUG_ON(!info->nid))
3754 info->ops.prepare = hda_pcm_default_prepare;
3756 if (info->ops.cleanup == NULL) {
3757 if (snd_BUG_ON(!info->nid))
3759 info->ops.cleanup = hda_pcm_default_cleanup;
3765 * codec prepare/cleanup entries
3767 int snd_hda_codec_prepare(struct hda_codec *codec,
3768 struct hda_pcm_stream *hinfo,
3769 unsigned int stream,
3770 unsigned int format,
3771 struct snd_pcm_substream *substream)
3774 mutex_lock(&codec->bus->prepare_mutex);
3775 ret = hinfo->ops.prepare(hinfo, codec, stream, format, substream);
3777 purify_inactive_streams(codec);
3778 mutex_unlock(&codec->bus->prepare_mutex);
3781 EXPORT_SYMBOL_HDA(snd_hda_codec_prepare);
3783 void snd_hda_codec_cleanup(struct hda_codec *codec,
3784 struct hda_pcm_stream *hinfo,
3785 struct snd_pcm_substream *substream)
3787 mutex_lock(&codec->bus->prepare_mutex);
3788 hinfo->ops.cleanup(hinfo, codec, substream);
3789 mutex_unlock(&codec->bus->prepare_mutex);
3791 EXPORT_SYMBOL_HDA(snd_hda_codec_cleanup);
3794 const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = {
3795 "Audio", "SPDIF", "HDMI", "Modem"
3799 * get the empty PCM device number to assign
3801 * note the max device number is limited by HDA_MAX_PCMS, currently 10
3803 static int get_empty_pcm_device(struct hda_bus *bus, int type)
3805 /* audio device indices; not linear to keep compatibility */
3806 static int audio_idx[HDA_PCM_NTYPES][5] = {
3807 [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 },
3808 [HDA_PCM_TYPE_SPDIF] = { 1, -1 },
3809 [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 },
3810 [HDA_PCM_TYPE_MODEM] = { 6, -1 },
3814 if (type >= HDA_PCM_NTYPES) {
3815 snd_printk(KERN_WARNING "Invalid PCM type %d\n", type);
3819 for (i = 0; audio_idx[type][i] >= 0 ; i++)
3820 if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits))
3821 return audio_idx[type][i];
3823 snd_printk(KERN_WARNING "Too many %s devices\n",
3824 snd_hda_pcm_type_name[type]);
3829 * attach a new PCM stream
3831 static int snd_hda_attach_pcm(struct hda_codec *codec, struct hda_pcm *pcm)
3833 struct hda_bus *bus = codec->bus;
3834 struct hda_pcm_stream *info;
3837 if (snd_BUG_ON(!pcm->name))
3839 for (stream = 0; stream < 2; stream++) {
3840 info = &pcm->stream[stream];
3841 if (info->substreams) {
3842 err = set_pcm_default_values(codec, info);
3847 return bus->ops.attach_pcm(bus, codec, pcm);
3850 /* assign all PCMs of the given codec */
3851 int snd_hda_codec_build_pcms(struct hda_codec *codec)
3856 if (!codec->num_pcms) {
3857 if (!codec->patch_ops.build_pcms)
3859 err = codec->patch_ops.build_pcms(codec);
3861 printk(KERN_ERR "hda_codec: cannot build PCMs"
3862 "for #%d (error %d)\n", codec->addr, err);
3863 err = snd_hda_codec_reset(codec);
3866 "hda_codec: cannot revert codec\n");
3871 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
3872 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
3875 if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
3876 continue; /* no substreams assigned */
3879 dev = get_empty_pcm_device(codec->bus, cpcm->pcm_type);
3881 continue; /* no fatal error */
3883 err = snd_hda_attach_pcm(codec, cpcm);
3885 printk(KERN_ERR "hda_codec: cannot attach "
3886 "PCM stream %d for codec #%d\n",
3888 continue; /* no fatal error */
3896 * snd_hda_build_pcms - build PCM information
3899 * Create PCM information for each codec included in the bus.
3901 * The build_pcms codec patch is requested to set up codec->num_pcms and
3902 * codec->pcm_info properly. The array is referred by the top-level driver
3903 * to create its PCM instances.
3904 * The allocated codec->pcm_info should be released in codec->patch_ops.free
3907 * At least, substreams, channels_min and channels_max must be filled for
3908 * each stream. substreams = 0 indicates that the stream doesn't exist.
3909 * When rates and/or formats are zero, the supported values are queried
3910 * from the given nid. The nid is used also by the default ops.prepare
3911 * and ops.cleanup callbacks.
3913 * The driver needs to call ops.open in its open callback. Similarly,
3914 * ops.close is supposed to be called in the close callback.
3915 * ops.prepare should be called in the prepare or hw_params callback
3916 * with the proper parameters for set up.
3917 * ops.cleanup should be called in hw_free for clean up of streams.
3919 * This function returns 0 if successful, or a negative error code.
3921 int __devinit snd_hda_build_pcms(struct hda_bus *bus)
3923 struct hda_codec *codec;
3925 list_for_each_entry(codec, &bus->codec_list, list) {
3926 int err = snd_hda_codec_build_pcms(codec);
3932 EXPORT_SYMBOL_HDA(snd_hda_build_pcms);
3935 * snd_hda_check_board_config - compare the current codec with the config table
3936 * @codec: the HDA codec
3937 * @num_configs: number of config enums
3938 * @models: array of model name strings
3939 * @tbl: configuration table, terminated by null entries
3941 * Compares the modelname or PCI subsystem id of the current codec with the
3942 * given configuration table. If a matching entry is found, returns its
3943 * config value (supposed to be 0 or positive).
3945 * If no entries are matching, the function returns a negative value.
3947 int snd_hda_check_board_config(struct hda_codec *codec,
3948 int num_configs, const char * const *models,
3949 const struct snd_pci_quirk *tbl)
3951 if (codec->modelname && models) {
3953 for (i = 0; i < num_configs; i++) {
3955 !strcmp(codec->modelname, models[i])) {
3956 snd_printd(KERN_INFO "hda_codec: model '%s' is "
3957 "selected\n", models[i]);
3963 if (!codec->bus->pci || !tbl)
3966 tbl = snd_pci_quirk_lookup(codec->bus->pci, tbl);
3969 if (tbl->value >= 0 && tbl->value < num_configs) {
3970 #ifdef CONFIG_SND_DEBUG_VERBOSE
3972 const char *model = NULL;
3974 model = models[tbl->value];
3976 sprintf(tmp, "#%d", tbl->value);
3979 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
3980 "for config %x:%x (%s)\n",
3981 model, tbl->subvendor, tbl->subdevice,
3982 (tbl->name ? tbl->name : "Unknown device"));
3988 EXPORT_SYMBOL_HDA(snd_hda_check_board_config);
3991 * snd_hda_check_board_codec_sid_config - compare the current codec
3992 subsystem ID with the
3995 This is important for Gateway notebooks with SB450 HDA Audio
3996 where the vendor ID of the PCI device is:
3997 ATI Technologies Inc SB450 HDA Audio [1002:437b]
3998 and the vendor/subvendor are found only at the codec.
4000 * @codec: the HDA codec
4001 * @num_configs: number of config enums
4002 * @models: array of model name strings
4003 * @tbl: configuration table, terminated by null entries
4005 * Compares the modelname or PCI subsystem id of the current codec with the
4006 * given configuration table. If a matching entry is found, returns its
4007 * config value (supposed to be 0 or positive).
4009 * If no entries are matching, the function returns a negative value.
4011 int snd_hda_check_board_codec_sid_config(struct hda_codec *codec,
4012 int num_configs, const char * const *models,
4013 const struct snd_pci_quirk *tbl)
4015 const struct snd_pci_quirk *q;
4017 /* Search for codec ID */
4018 for (q = tbl; q->subvendor; q++) {
4019 unsigned int mask = 0xffff0000 | q->subdevice_mask;
4020 unsigned int id = (q->subdevice | (q->subvendor << 16)) & mask;
4021 if ((codec->subsystem_id & mask) == id)
4030 if (tbl->value >= 0 && tbl->value < num_configs) {
4031 #ifdef CONFIG_SND_DEBUG_VERBOSE
4033 const char *model = NULL;
4035 model = models[tbl->value];
4037 sprintf(tmp, "#%d", tbl->value);
4040 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
4041 "for config %x:%x (%s)\n",
4042 model, tbl->subvendor, tbl->subdevice,
4043 (tbl->name ? tbl->name : "Unknown device"));
4049 EXPORT_SYMBOL_HDA(snd_hda_check_board_codec_sid_config);
4052 * snd_hda_add_new_ctls - create controls from the array
4053 * @codec: the HDA codec
4054 * @knew: the array of struct snd_kcontrol_new
4056 * This helper function creates and add new controls in the given array.
4057 * The array must be terminated with an empty entry as terminator.
4059 * Returns 0 if successful, or a negative error code.
4061 int snd_hda_add_new_ctls(struct hda_codec *codec,
4062 const struct snd_kcontrol_new *knew)
4066 for (; knew->name; knew++) {
4067 struct snd_kcontrol *kctl;
4068 int addr = 0, idx = 0;
4069 if (knew->iface == -1) /* skip this codec private value */
4072 kctl = snd_ctl_new1(knew, codec);
4076 kctl->id.device = addr;
4078 kctl->id.index = idx;
4079 err = snd_hda_ctl_add(codec, 0, kctl);
4082 /* try first with another device index corresponding to
4083 * the codec addr; if it still fails (or it's the
4084 * primary codec), then try another control index
4086 if (!addr && codec->addr)
4088 else if (!idx && !knew->index) {
4089 idx = find_empty_mixer_ctl_idx(codec,
4099 EXPORT_SYMBOL_HDA(snd_hda_add_new_ctls);
4101 #ifdef CONFIG_SND_HDA_POWER_SAVE
4102 static void hda_power_work(struct work_struct *work)
4104 struct hda_codec *codec =
4105 container_of(work, struct hda_codec, power_work.work);
4106 struct hda_bus *bus = codec->bus;
4108 if (!codec->power_on || codec->power_count) {
4109 codec->power_transition = 0;
4113 trace_hda_power_down(codec);
4114 hda_call_codec_suspend(codec);
4115 if (bus->ops.pm_notify)
4116 bus->ops.pm_notify(bus);
4119 static void hda_keep_power_on(struct hda_codec *codec)
4121 codec->power_count++;
4122 codec->power_on = 1;
4123 codec->power_jiffies = jiffies;
4126 /* update the power on/off account with the current jiffies */
4127 void snd_hda_update_power_acct(struct hda_codec *codec)
4129 unsigned long delta = jiffies - codec->power_jiffies;
4130 if (codec->power_on)
4131 codec->power_on_acct += delta;
4133 codec->power_off_acct += delta;
4134 codec->power_jiffies += delta;
4138 * snd_hda_power_up - Power-up the codec
4139 * @codec: HD-audio codec
4141 * Increment the power-up counter and power up the hardware really when
4142 * not turned on yet.
4144 void snd_hda_power_up(struct hda_codec *codec)
4146 struct hda_bus *bus = codec->bus;
4148 codec->power_count++;
4149 if (codec->power_on || codec->power_transition)
4152 trace_hda_power_up(codec);
4153 snd_hda_update_power_acct(codec);
4154 codec->power_on = 1;
4155 codec->power_jiffies = jiffies;
4156 if (bus->ops.pm_notify)
4157 bus->ops.pm_notify(bus);
4158 hda_call_codec_resume(codec);
4159 cancel_delayed_work(&codec->power_work);
4160 codec->power_transition = 0;
4162 EXPORT_SYMBOL_HDA(snd_hda_power_up);
4164 #define power_save(codec) \
4165 ((codec)->bus->power_save ? *(codec)->bus->power_save : 0)
4168 * snd_hda_power_down - Power-down the codec
4169 * @codec: HD-audio codec
4171 * Decrement the power-up counter and schedules the power-off work if
4172 * the counter rearches to zero.
4174 void snd_hda_power_down(struct hda_codec *codec)
4176 --codec->power_count;
4177 if (!codec->power_on || codec->power_count || codec->power_transition)
4179 if (power_save(codec)) {
4180 codec->power_transition = 1; /* avoid reentrance */
4181 queue_delayed_work(codec->bus->workq, &codec->power_work,
4182 msecs_to_jiffies(power_save(codec) * 1000));
4185 EXPORT_SYMBOL_HDA(snd_hda_power_down);
4188 * snd_hda_check_amp_list_power - Check the amp list and update the power
4189 * @codec: HD-audio codec
4190 * @check: the object containing an AMP list and the status
4191 * @nid: NID to check / update
4193 * Check whether the given NID is in the amp list. If it's in the list,
4194 * check the current AMP status, and update the the power-status according
4195 * to the mute status.
4197 * This function is supposed to be set or called from the check_power_status
4200 int snd_hda_check_amp_list_power(struct hda_codec *codec,
4201 struct hda_loopback_check *check,
4204 const struct hda_amp_list *p;
4207 if (!check->amplist)
4209 for (p = check->amplist; p->nid; p++) {
4214 return 0; /* nothing changed */
4216 for (p = check->amplist; p->nid; p++) {
4217 for (ch = 0; ch < 2; ch++) {
4218 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
4220 if (!(v & HDA_AMP_MUTE) && v > 0) {
4221 if (!check->power_on) {
4222 check->power_on = 1;
4223 snd_hda_power_up(codec);
4229 if (check->power_on) {
4230 check->power_on = 0;
4231 snd_hda_power_down(codec);
4235 EXPORT_SYMBOL_HDA(snd_hda_check_amp_list_power);
4239 * Channel mode helper
4243 * snd_hda_ch_mode_info - Info callback helper for the channel mode enum
4245 int snd_hda_ch_mode_info(struct hda_codec *codec,
4246 struct snd_ctl_elem_info *uinfo,
4247 const struct hda_channel_mode *chmode,
4250 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4252 uinfo->value.enumerated.items = num_chmodes;
4253 if (uinfo->value.enumerated.item >= num_chmodes)
4254 uinfo->value.enumerated.item = num_chmodes - 1;
4255 sprintf(uinfo->value.enumerated.name, "%dch",
4256 chmode[uinfo->value.enumerated.item].channels);
4259 EXPORT_SYMBOL_HDA(snd_hda_ch_mode_info);
4262 * snd_hda_ch_mode_get - Get callback helper for the channel mode enum
4264 int snd_hda_ch_mode_get(struct hda_codec *codec,
4265 struct snd_ctl_elem_value *ucontrol,
4266 const struct hda_channel_mode *chmode,
4272 for (i = 0; i < num_chmodes; i++) {
4273 if (max_channels == chmode[i].channels) {
4274 ucontrol->value.enumerated.item[0] = i;
4280 EXPORT_SYMBOL_HDA(snd_hda_ch_mode_get);
4283 * snd_hda_ch_mode_put - Put callback helper for the channel mode enum
4285 int snd_hda_ch_mode_put(struct hda_codec *codec,
4286 struct snd_ctl_elem_value *ucontrol,
4287 const struct hda_channel_mode *chmode,
4293 mode = ucontrol->value.enumerated.item[0];
4294 if (mode >= num_chmodes)
4296 if (*max_channelsp == chmode[mode].channels)
4298 /* change the current channel setting */
4299 *max_channelsp = chmode[mode].channels;
4300 if (chmode[mode].sequence)
4301 snd_hda_sequence_write_cache(codec, chmode[mode].sequence);
4304 EXPORT_SYMBOL_HDA(snd_hda_ch_mode_put);
4311 * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum
4313 int snd_hda_input_mux_info(const struct hda_input_mux *imux,
4314 struct snd_ctl_elem_info *uinfo)
4318 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4320 uinfo->value.enumerated.items = imux->num_items;
4321 if (!imux->num_items)
4323 index = uinfo->value.enumerated.item;
4324 if (index >= imux->num_items)
4325 index = imux->num_items - 1;
4326 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
4329 EXPORT_SYMBOL_HDA(snd_hda_input_mux_info);
4332 * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum
4334 int snd_hda_input_mux_put(struct hda_codec *codec,
4335 const struct hda_input_mux *imux,
4336 struct snd_ctl_elem_value *ucontrol,
4338 unsigned int *cur_val)
4342 if (!imux->num_items)
4344 idx = ucontrol->value.enumerated.item[0];
4345 if (idx >= imux->num_items)
4346 idx = imux->num_items - 1;
4347 if (*cur_val == idx)
4349 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
4350 imux->items[idx].index);
4354 EXPORT_SYMBOL_HDA(snd_hda_input_mux_put);
4358 * Multi-channel / digital-out PCM helper functions
4361 /* setup SPDIF output stream */
4362 static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
4363 unsigned int stream_tag, unsigned int format)
4365 struct hda_spdif_out *spdif = snd_hda_spdif_out_of_nid(codec, nid);
4367 /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */
4368 if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE))
4369 set_dig_out_convert(codec, nid,
4370 spdif->ctls & ~AC_DIG1_ENABLE & 0xff,
4372 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
4373 if (codec->slave_dig_outs) {
4375 for (d = codec->slave_dig_outs; *d; d++)
4376 snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
4379 /* turn on again (if needed) */
4380 if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE))
4381 set_dig_out_convert(codec, nid,
4382 spdif->ctls & 0xff, -1);
4385 static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
4387 snd_hda_codec_cleanup_stream(codec, nid);
4388 if (codec->slave_dig_outs) {
4390 for (d = codec->slave_dig_outs; *d; d++)
4391 snd_hda_codec_cleanup_stream(codec, *d);
4396 * snd_hda_bus_reboot_notify - call the reboot notifier of each codec
4397 * @bus: HD-audio bus
4399 void snd_hda_bus_reboot_notify(struct hda_bus *bus)
4401 struct hda_codec *codec;
4405 list_for_each_entry(codec, &bus->codec_list, list) {
4406 if (hda_codec_is_power_on(codec) &&
4407 codec->patch_ops.reboot_notify)
4408 codec->patch_ops.reboot_notify(codec);
4411 EXPORT_SYMBOL_HDA(snd_hda_bus_reboot_notify);
4414 * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode
4416 int snd_hda_multi_out_dig_open(struct hda_codec *codec,
4417 struct hda_multi_out *mout)
4419 mutex_lock(&codec->spdif_mutex);
4420 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
4421 /* already opened as analog dup; reset it once */
4422 cleanup_dig_out_stream(codec, mout->dig_out_nid);
4423 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
4424 mutex_unlock(&codec->spdif_mutex);
4427 EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_open);
4430 * snd_hda_multi_out_dig_prepare - prepare the digital out stream
4432 int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
4433 struct hda_multi_out *mout,
4434 unsigned int stream_tag,
4435 unsigned int format,
4436 struct snd_pcm_substream *substream)
4438 mutex_lock(&codec->spdif_mutex);
4439 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
4440 mutex_unlock(&codec->spdif_mutex);
4443 EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_prepare);
4446 * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream
4448 int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec,
4449 struct hda_multi_out *mout)
4451 mutex_lock(&codec->spdif_mutex);
4452 cleanup_dig_out_stream(codec, mout->dig_out_nid);
4453 mutex_unlock(&codec->spdif_mutex);
4456 EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_cleanup);
4459 * snd_hda_multi_out_dig_close - release the digital out stream
4461 int snd_hda_multi_out_dig_close(struct hda_codec *codec,
4462 struct hda_multi_out *mout)
4464 mutex_lock(&codec->spdif_mutex);
4465 mout->dig_out_used = 0;
4466 mutex_unlock(&codec->spdif_mutex);
4469 EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_close);
4472 * snd_hda_multi_out_analog_open - open analog outputs
4474 * Open analog outputs and set up the hw-constraints.
4475 * If the digital outputs can be opened as slave, open the digital
4478 int snd_hda_multi_out_analog_open(struct hda_codec *codec,
4479 struct hda_multi_out *mout,
4480 struct snd_pcm_substream *substream,
4481 struct hda_pcm_stream *hinfo)
4483 struct snd_pcm_runtime *runtime = substream->runtime;
4484 runtime->hw.channels_max = mout->max_channels;
4485 if (mout->dig_out_nid) {
4486 if (!mout->analog_rates) {
4487 mout->analog_rates = hinfo->rates;
4488 mout->analog_formats = hinfo->formats;
4489 mout->analog_maxbps = hinfo->maxbps;
4491 runtime->hw.rates = mout->analog_rates;
4492 runtime->hw.formats = mout->analog_formats;
4493 hinfo->maxbps = mout->analog_maxbps;
4495 if (!mout->spdif_rates) {
4496 snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
4498 &mout->spdif_formats,
4499 &mout->spdif_maxbps);
4501 mutex_lock(&codec->spdif_mutex);
4502 if (mout->share_spdif) {
4503 if ((runtime->hw.rates & mout->spdif_rates) &&
4504 (runtime->hw.formats & mout->spdif_formats)) {
4505 runtime->hw.rates &= mout->spdif_rates;
4506 runtime->hw.formats &= mout->spdif_formats;
4507 if (mout->spdif_maxbps < hinfo->maxbps)
4508 hinfo->maxbps = mout->spdif_maxbps;
4510 mout->share_spdif = 0;
4511 /* FIXME: need notify? */
4514 mutex_unlock(&codec->spdif_mutex);
4516 return snd_pcm_hw_constraint_step(substream->runtime, 0,
4517 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
4519 EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_open);
4522 * snd_hda_multi_out_analog_prepare - Preapre the analog outputs.
4524 * Set up the i/o for analog out.
4525 * When the digital out is available, copy the front out to digital out, too.
4527 int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
4528 struct hda_multi_out *mout,
4529 unsigned int stream_tag,
4530 unsigned int format,
4531 struct snd_pcm_substream *substream)
4533 const hda_nid_t *nids = mout->dac_nids;
4534 int chs = substream->runtime->channels;
4535 struct hda_spdif_out *spdif =
4536 snd_hda_spdif_out_of_nid(codec, mout->dig_out_nid);
4539 mutex_lock(&codec->spdif_mutex);
4540 if (mout->dig_out_nid && mout->share_spdif &&
4541 mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
4543 snd_hda_is_supported_format(codec, mout->dig_out_nid,
4545 !(spdif->status & IEC958_AES0_NONAUDIO)) {
4546 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
4547 setup_dig_out_stream(codec, mout->dig_out_nid,
4548 stream_tag, format);
4550 mout->dig_out_used = 0;
4551 cleanup_dig_out_stream(codec, mout->dig_out_nid);
4554 mutex_unlock(&codec->spdif_mutex);
4557 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
4559 if (!mout->no_share_stream &&
4560 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
4561 /* headphone out will just decode front left/right (stereo) */
4562 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
4564 /* extra outputs copied from front */
4565 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
4566 if (!mout->no_share_stream && mout->hp_out_nid[i])
4567 snd_hda_codec_setup_stream(codec,
4568 mout->hp_out_nid[i],
4569 stream_tag, 0, format);
4570 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
4571 if (!mout->no_share_stream && mout->extra_out_nid[i])
4572 snd_hda_codec_setup_stream(codec,
4573 mout->extra_out_nid[i],
4574 stream_tag, 0, format);
4577 for (i = 1; i < mout->num_dacs; i++) {
4578 if (chs >= (i + 1) * 2) /* independent out */
4579 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
4581 else if (!mout->no_share_stream) /* copy front */
4582 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
4587 EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_prepare);
4590 * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out
4592 int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
4593 struct hda_multi_out *mout)
4595 const hda_nid_t *nids = mout->dac_nids;
4598 for (i = 0; i < mout->num_dacs; i++)
4599 snd_hda_codec_cleanup_stream(codec, nids[i]);
4601 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
4602 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
4603 if (mout->hp_out_nid[i])
4604 snd_hda_codec_cleanup_stream(codec,
4605 mout->hp_out_nid[i]);
4606 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
4607 if (mout->extra_out_nid[i])
4608 snd_hda_codec_cleanup_stream(codec,
4609 mout->extra_out_nid[i]);
4610 mutex_lock(&codec->spdif_mutex);
4611 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
4612 cleanup_dig_out_stream(codec, mout->dig_out_nid);
4613 mout->dig_out_used = 0;
4615 mutex_unlock(&codec->spdif_mutex);
4618 EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_cleanup);
4621 * Helper for automatic pin configuration
4624 static int is_in_nid_list(hda_nid_t nid, const hda_nid_t *list)
4626 for (; *list; list++)
4634 * Sort an associated group of pins according to their sequence numbers.
4636 static void sort_pins_by_sequence(hda_nid_t *pins, short *sequences,
4643 for (i = 0; i < num_pins; i++) {
4644 for (j = i + 1; j < num_pins; j++) {
4645 if (sequences[i] > sequences[j]) {
4647 sequences[i] = sequences[j];
4658 /* add the found input-pin to the cfg->inputs[] table */
4659 static void add_auto_cfg_input_pin(struct auto_pin_cfg *cfg, hda_nid_t nid,
4662 if (cfg->num_inputs < AUTO_CFG_MAX_INS) {
4663 cfg->inputs[cfg->num_inputs].pin = nid;
4664 cfg->inputs[cfg->num_inputs].type = type;
4669 /* sort inputs in the order of AUTO_PIN_* type */
4670 static void sort_autocfg_input_pins(struct auto_pin_cfg *cfg)
4674 for (i = 0; i < cfg->num_inputs; i++) {
4675 for (j = i + 1; j < cfg->num_inputs; j++) {
4676 if (cfg->inputs[i].type > cfg->inputs[j].type) {
4677 struct auto_pin_cfg_item tmp;
4678 tmp = cfg->inputs[i];
4679 cfg->inputs[i] = cfg->inputs[j];
4680 cfg->inputs[j] = tmp;
4686 /* Reorder the surround channels
4687 * ALSA sequence is front/surr/clfe/side
4689 * 4-ch: front/surr => OK as it is
4690 * 6-ch: front/clfe/surr
4691 * 8-ch: front/clfe/rear/side|fc
4693 static void reorder_outputs(unsigned int nums, hda_nid_t *pins)
4708 * Parse all pin widgets and store the useful pin nids to cfg
4710 * The number of line-outs or any primary output is stored in line_outs,
4711 * and the corresponding output pins are assigned to line_out_pins[],
4712 * in the order of front, rear, CLFE, side, ...
4714 * If more extra outputs (speaker and headphone) are found, the pins are
4715 * assisnged to hp_pins[] and speaker_pins[], respectively. If no line-out jack
4716 * is detected, one of speaker of HP pins is assigned as the primary
4717 * output, i.e. to line_out_pins[0]. So, line_outs is always positive
4718 * if any analog output exists.
4720 * The analog input pins are assigned to inputs array.
4721 * The digital input/output pins are assigned to dig_in_pin and dig_out_pin,
4724 int snd_hda_parse_pin_defcfg(struct hda_codec *codec,
4725 struct auto_pin_cfg *cfg,
4726 const hda_nid_t *ignore_nids,
4727 unsigned int cond_flags)
4729 hda_nid_t nid, end_nid;
4730 short seq, assoc_line_out;
4731 short sequences_line_out[ARRAY_SIZE(cfg->line_out_pins)];
4732 short sequences_speaker[ARRAY_SIZE(cfg->speaker_pins)];
4733 short sequences_hp[ARRAY_SIZE(cfg->hp_pins)];
4736 memset(cfg, 0, sizeof(*cfg));
4738 memset(sequences_line_out, 0, sizeof(sequences_line_out));
4739 memset(sequences_speaker, 0, sizeof(sequences_speaker));
4740 memset(sequences_hp, 0, sizeof(sequences_hp));
4743 codec->ignore_misc_bit = true;
4744 end_nid = codec->start_nid + codec->num_nodes;
4745 for (nid = codec->start_nid; nid < end_nid; nid++) {
4746 unsigned int wid_caps = get_wcaps(codec, nid);
4747 unsigned int wid_type = get_wcaps_type(wid_caps);
4748 unsigned int def_conf;
4749 short assoc, loc, conn, dev;
4751 /* read all default configuration for pin complex */
4752 if (wid_type != AC_WID_PIN)
4754 /* ignore the given nids (e.g. pc-beep returns error) */
4755 if (ignore_nids && is_in_nid_list(nid, ignore_nids))
4758 def_conf = snd_hda_codec_get_pincfg(codec, nid);
4759 if (!(get_defcfg_misc(snd_hda_codec_get_pincfg(codec, nid)) &
4760 AC_DEFCFG_MISC_NO_PRESENCE))
4761 codec->ignore_misc_bit = false;
4762 conn = get_defcfg_connect(def_conf);
4763 if (conn == AC_JACK_PORT_NONE)
4765 loc = get_defcfg_location(def_conf);
4766 dev = get_defcfg_device(def_conf);
4768 /* workaround for buggy BIOS setups */
4769 if (dev == AC_JACK_LINE_OUT) {
4770 if (conn == AC_JACK_PORT_FIXED)
4771 dev = AC_JACK_SPEAKER;
4775 case AC_JACK_LINE_OUT:
4776 seq = get_defcfg_sequence(def_conf);
4777 assoc = get_defcfg_association(def_conf);
4779 if (!(wid_caps & AC_WCAP_STEREO))
4780 if (!cfg->mono_out_pin)
4781 cfg->mono_out_pin = nid;
4784 if (!assoc_line_out)
4785 assoc_line_out = assoc;
4786 else if (assoc_line_out != assoc)
4788 if (cfg->line_outs >= ARRAY_SIZE(cfg->line_out_pins))
4790 cfg->line_out_pins[cfg->line_outs] = nid;
4791 sequences_line_out[cfg->line_outs] = seq;
4794 case AC_JACK_SPEAKER:
4795 seq = get_defcfg_sequence(def_conf);
4796 assoc = get_defcfg_association(def_conf);
4797 if (cfg->speaker_outs >= ARRAY_SIZE(cfg->speaker_pins))
4799 cfg->speaker_pins[cfg->speaker_outs] = nid;
4800 sequences_speaker[cfg->speaker_outs] = (assoc << 4) | seq;
4801 cfg->speaker_outs++;
4803 case AC_JACK_HP_OUT:
4804 seq = get_defcfg_sequence(def_conf);
4805 assoc = get_defcfg_association(def_conf);
4806 if (cfg->hp_outs >= ARRAY_SIZE(cfg->hp_pins))
4808 cfg->hp_pins[cfg->hp_outs] = nid;
4809 sequences_hp[cfg->hp_outs] = (assoc << 4) | seq;
4812 case AC_JACK_MIC_IN:
4813 add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_MIC);
4815 case AC_JACK_LINE_IN:
4816 add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_LINE_IN);
4819 add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_CD);
4822 add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_AUX);
4824 case AC_JACK_SPDIF_OUT:
4825 case AC_JACK_DIG_OTHER_OUT:
4826 if (cfg->dig_outs >= ARRAY_SIZE(cfg->dig_out_pins))
4828 cfg->dig_out_pins[cfg->dig_outs] = nid;
4829 cfg->dig_out_type[cfg->dig_outs] =
4830 (loc == AC_JACK_LOC_HDMI) ?
4831 HDA_PCM_TYPE_HDMI : HDA_PCM_TYPE_SPDIF;
4834 case AC_JACK_SPDIF_IN:
4835 case AC_JACK_DIG_OTHER_IN:
4836 cfg->dig_in_pin = nid;
4837 if (loc == AC_JACK_LOC_HDMI)
4838 cfg->dig_in_type = HDA_PCM_TYPE_HDMI;
4840 cfg->dig_in_type = HDA_PCM_TYPE_SPDIF;
4846 * If no line-out is defined but multiple HPs are found,
4847 * some of them might be the real line-outs.
4849 if (!cfg->line_outs && cfg->hp_outs > 1 &&
4850 !(cond_flags & HDA_PINCFG_NO_HP_FIXUP)) {
4852 while (i < cfg->hp_outs) {
4853 /* The real HPs should have the sequence 0x0f */
4854 if ((sequences_hp[i] & 0x0f) == 0x0f) {
4858 /* Move it to the line-out table */
4859 cfg->line_out_pins[cfg->line_outs] = cfg->hp_pins[i];
4860 sequences_line_out[cfg->line_outs] = sequences_hp[i];
4863 memmove(cfg->hp_pins + i, cfg->hp_pins + i + 1,
4864 sizeof(cfg->hp_pins[0]) * (cfg->hp_outs - i));
4865 memmove(sequences_hp + i, sequences_hp + i + 1,
4866 sizeof(sequences_hp[0]) * (cfg->hp_outs - i));
4868 memset(cfg->hp_pins + cfg->hp_outs, 0,
4869 sizeof(hda_nid_t) * (AUTO_CFG_MAX_OUTS - cfg->hp_outs));
4871 cfg->line_out_type = AUTO_PIN_HP_OUT;
4875 /* sort by sequence */
4876 sort_pins_by_sequence(cfg->line_out_pins, sequences_line_out,
4878 sort_pins_by_sequence(cfg->speaker_pins, sequences_speaker,
4880 sort_pins_by_sequence(cfg->hp_pins, sequences_hp,
4884 * FIX-UP: if no line-outs are detected, try to use speaker or HP pin
4885 * as a primary output
4887 if (!cfg->line_outs &&
4888 !(cond_flags & HDA_PINCFG_NO_LO_FIXUP)) {
4889 if (cfg->speaker_outs) {
4890 cfg->line_outs = cfg->speaker_outs;
4891 memcpy(cfg->line_out_pins, cfg->speaker_pins,
4892 sizeof(cfg->speaker_pins));
4893 cfg->speaker_outs = 0;
4894 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
4895 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
4896 } else if (cfg->hp_outs) {
4897 cfg->line_outs = cfg->hp_outs;
4898 memcpy(cfg->line_out_pins, cfg->hp_pins,
4899 sizeof(cfg->hp_pins));
4901 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
4902 cfg->line_out_type = AUTO_PIN_HP_OUT;
4906 reorder_outputs(cfg->line_outs, cfg->line_out_pins);
4907 reorder_outputs(cfg->hp_outs, cfg->hp_pins);
4908 reorder_outputs(cfg->speaker_outs, cfg->speaker_pins);
4910 sort_autocfg_input_pins(cfg);
4913 * debug prints of the parsed results
4915 snd_printd("autoconfig: line_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x) type:%s\n",
4916 cfg->line_outs, cfg->line_out_pins[0], cfg->line_out_pins[1],
4917 cfg->line_out_pins[2], cfg->line_out_pins[3],
4918 cfg->line_out_pins[4],
4919 cfg->line_out_type == AUTO_PIN_HP_OUT ? "hp" :
4920 (cfg->line_out_type == AUTO_PIN_SPEAKER_OUT ?
4921 "speaker" : "line"));
4922 snd_printd(" speaker_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
4923 cfg->speaker_outs, cfg->speaker_pins[0],
4924 cfg->speaker_pins[1], cfg->speaker_pins[2],
4925 cfg->speaker_pins[3], cfg->speaker_pins[4]);
4926 snd_printd(" hp_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
4927 cfg->hp_outs, cfg->hp_pins[0],
4928 cfg->hp_pins[1], cfg->hp_pins[2],
4929 cfg->hp_pins[3], cfg->hp_pins[4]);
4930 snd_printd(" mono: mono_out=0x%x\n", cfg->mono_out_pin);
4932 snd_printd(" dig-out=0x%x/0x%x\n",
4933 cfg->dig_out_pins[0], cfg->dig_out_pins[1]);
4934 snd_printd(" inputs:");
4935 for (i = 0; i < cfg->num_inputs; i++) {
4936 snd_printd(" %s=0x%x",
4937 hda_get_autocfg_input_label(codec, cfg, i),
4938 cfg->inputs[i].pin);
4941 if (cfg->dig_in_pin)
4942 snd_printd(" dig-in=0x%x\n", cfg->dig_in_pin);
4946 EXPORT_SYMBOL_HDA(snd_hda_parse_pin_defcfg);
4948 int snd_hda_get_input_pin_attr(unsigned int def_conf)
4950 unsigned int loc = get_defcfg_location(def_conf);
4951 unsigned int conn = get_defcfg_connect(def_conf);
4952 if (conn == AC_JACK_PORT_NONE)
4953 return INPUT_PIN_ATTR_UNUSED;
4954 /* Windows may claim the internal mic to be BOTH, too */
4955 if (conn == AC_JACK_PORT_FIXED || conn == AC_JACK_PORT_BOTH)
4956 return INPUT_PIN_ATTR_INT;
4957 if ((loc & 0x30) == AC_JACK_LOC_INTERNAL)
4958 return INPUT_PIN_ATTR_INT;
4959 if ((loc & 0x30) == AC_JACK_LOC_SEPARATE)
4960 return INPUT_PIN_ATTR_DOCK;
4961 if (loc == AC_JACK_LOC_REAR)
4962 return INPUT_PIN_ATTR_REAR;
4963 if (loc == AC_JACK_LOC_FRONT)
4964 return INPUT_PIN_ATTR_FRONT;
4965 return INPUT_PIN_ATTR_NORMAL;
4967 EXPORT_SYMBOL_HDA(snd_hda_get_input_pin_attr);
4970 * hda_get_input_pin_label - Give a label for the given input pin
4972 * When check_location is true, the function checks the pin location
4973 * for mic and line-in pins, and set an appropriate prefix like "Front",
4974 * "Rear", "Internal".
4977 static const char *hda_get_input_pin_label(struct hda_codec *codec,
4978 hda_nid_t pin, bool check_location)
4980 unsigned int def_conf;
4981 static const char * const mic_names[] = {
4982 "Internal Mic", "Dock Mic", "Mic", "Front Mic", "Rear Mic",
4986 def_conf = snd_hda_codec_get_pincfg(codec, pin);
4988 switch (get_defcfg_device(def_conf)) {
4989 case AC_JACK_MIC_IN:
4990 if (!check_location)
4992 attr = snd_hda_get_input_pin_attr(def_conf);
4995 return mic_names[attr - 1];
4996 case AC_JACK_LINE_IN:
4997 if (!check_location)
4999 attr = snd_hda_get_input_pin_attr(def_conf);
5002 if (attr == INPUT_PIN_ATTR_DOCK)
5009 case AC_JACK_SPDIF_IN:
5011 case AC_JACK_DIG_OTHER_IN:
5012 return "Digital In";
5018 /* Check whether the location prefix needs to be added to the label.
5019 * If all mic-jacks are in the same location (e.g. rear panel), we don't
5020 * have to put "Front" prefix to each label. In such a case, returns false.
5022 static int check_mic_location_need(struct hda_codec *codec,
5023 const struct auto_pin_cfg *cfg,
5029 defc = snd_hda_codec_get_pincfg(codec, cfg->inputs[input].pin);
5030 attr = snd_hda_get_input_pin_attr(defc);
5031 /* for internal or docking mics, we need locations */
5032 if (attr <= INPUT_PIN_ATTR_NORMAL)
5036 for (i = 0; i < cfg->num_inputs; i++) {
5037 defc = snd_hda_codec_get_pincfg(codec, cfg->inputs[i].pin);
5038 attr2 = snd_hda_get_input_pin_attr(defc);
5039 if (attr2 >= INPUT_PIN_ATTR_NORMAL) {
5040 if (attr && attr != attr2)
5041 return 1; /* different locations found */
5049 * hda_get_autocfg_input_label - Get a label for the given input
5051 * Get a label for the given input pin defined by the autocfg item.
5052 * Unlike hda_get_input_pin_label(), this function checks all inputs
5053 * defined in autocfg and avoids the redundant mic/line prefix as much as
5056 const char *hda_get_autocfg_input_label(struct hda_codec *codec,
5057 const struct auto_pin_cfg *cfg,
5060 int type = cfg->inputs[input].type;
5061 int has_multiple_pins = 0;
5063 if ((input > 0 && cfg->inputs[input - 1].type == type) ||
5064 (input < cfg->num_inputs - 1 && cfg->inputs[input + 1].type == type))
5065 has_multiple_pins = 1;
5066 if (has_multiple_pins && type == AUTO_PIN_MIC)
5067 has_multiple_pins &= check_mic_location_need(codec, cfg, input);
5068 return hda_get_input_pin_label(codec, cfg->inputs[input].pin,
5071 EXPORT_SYMBOL_HDA(hda_get_autocfg_input_label);
5073 /* get a unique suffix or an index number */
5074 static const char *check_output_sfx(hda_nid_t nid, const hda_nid_t *pins,
5075 int num_pins, int *indexp)
5077 static const char * const channel_sfx[] = {
5078 " Front", " Surround", " CLFE", " Side"
5082 for (i = 0; i < num_pins; i++) {
5083 if (pins[i] == nid) {
5086 if (num_pins > ARRAY_SIZE(channel_sfx)) {
5091 return channel_sfx[i];
5097 static int fill_audio_out_name(struct hda_codec *codec, hda_nid_t nid,
5098 const struct auto_pin_cfg *cfg,
5099 const char *name, char *label, int maxlen,
5102 unsigned int def_conf = snd_hda_codec_get_pincfg(codec, nid);
5103 int attr = snd_hda_get_input_pin_attr(def_conf);
5104 const char *pfx = "", *sfx = "";
5106 /* handle as a speaker if it's a fixed line-out */
5107 if (!strcmp(name, "Line-Out") && attr == INPUT_PIN_ATTR_INT)
5109 /* check the location */
5111 case INPUT_PIN_ATTR_DOCK:
5114 case INPUT_PIN_ATTR_FRONT:
5119 /* try to give a unique suffix if needed */
5120 sfx = check_output_sfx(nid, cfg->line_out_pins, cfg->line_outs,
5123 sfx = check_output_sfx(nid, cfg->hp_pins, cfg->hp_outs,
5126 sfx = check_output_sfx(nid, cfg->speaker_pins, cfg->speaker_outs,
5131 snprintf(label, maxlen, "%s%s%s", pfx, name, sfx);
5136 * snd_hda_get_pin_label - Get a label for the given I/O pin
5138 * Get a label for the given pin. This function works for both input and
5139 * output pins. When @cfg is given as non-NULL, the function tries to get
5140 * an optimized label using hda_get_autocfg_input_label().
5142 * This function tries to give a unique label string for the pin as much as
5143 * possible. For example, when the multiple line-outs are present, it adds
5144 * the channel suffix like "Front", "Surround", etc (only when @cfg is given).
5145 * If no unique name with a suffix is available and @indexp is non-NULL, the
5146 * index number is stored in the pointer.
5148 int snd_hda_get_pin_label(struct hda_codec *codec, hda_nid_t nid,
5149 const struct auto_pin_cfg *cfg,
5150 char *label, int maxlen, int *indexp)
5152 unsigned int def_conf = snd_hda_codec_get_pincfg(codec, nid);
5153 const char *name = NULL;
5158 if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE)
5161 switch (get_defcfg_device(def_conf)) {
5162 case AC_JACK_LINE_OUT:
5163 return fill_audio_out_name(codec, nid, cfg, "Line-Out",
5164 label, maxlen, indexp);
5165 case AC_JACK_SPEAKER:
5166 return fill_audio_out_name(codec, nid, cfg, "Speaker",
5167 label, maxlen, indexp);
5168 case AC_JACK_HP_OUT:
5169 return fill_audio_out_name(codec, nid, cfg, "Headphone",
5170 label, maxlen, indexp);
5171 case AC_JACK_SPDIF_OUT:
5172 case AC_JACK_DIG_OTHER_OUT:
5173 if (get_defcfg_location(def_conf) == AC_JACK_LOC_HDMI)
5177 if (cfg && indexp) {
5178 for (i = 0; i < cfg->dig_outs; i++)
5179 if (cfg->dig_out_pins[i] == nid) {
5187 for (i = 0; i < cfg->num_inputs; i++) {
5188 if (cfg->inputs[i].pin != nid)
5190 name = hda_get_autocfg_input_label(codec, cfg, i);
5196 name = hda_get_input_pin_label(codec, nid, true);
5201 strlcpy(label, name, maxlen);
5204 EXPORT_SYMBOL_HDA(snd_hda_get_pin_label);
5207 * snd_hda_add_imux_item - Add an item to input_mux
5209 * When the same label is used already in the existing items, the number
5210 * suffix is appended to the label. This label index number is stored
5211 * to type_idx when non-NULL pointer is given.
5213 int snd_hda_add_imux_item(struct hda_input_mux *imux, const char *label,
5214 int index, int *type_idx)
5216 int i, label_idx = 0;
5217 if (imux->num_items >= HDA_MAX_NUM_INPUTS) {
5218 snd_printd(KERN_ERR "hda_codec: Too many imux items!\n");
5221 for (i = 0; i < imux->num_items; i++) {
5222 if (!strncmp(label, imux->items[i].label, strlen(label)))
5226 *type_idx = label_idx;
5228 snprintf(imux->items[imux->num_items].label,
5229 sizeof(imux->items[imux->num_items].label),
5230 "%s %d", label, label_idx);
5232 strlcpy(imux->items[imux->num_items].label, label,
5233 sizeof(imux->items[imux->num_items].label));
5234 imux->items[imux->num_items].index = index;
5238 EXPORT_SYMBOL_HDA(snd_hda_add_imux_item);
5247 * snd_hda_suspend - suspend the codecs
5250 * Returns 0 if successful.
5252 int snd_hda_suspend(struct hda_bus *bus)
5254 struct hda_codec *codec;
5256 list_for_each_entry(codec, &bus->codec_list, list) {
5257 if (hda_codec_is_power_on(codec))
5258 hda_call_codec_suspend(codec);
5259 if (codec->patch_ops.post_suspend)
5260 codec->patch_ops.post_suspend(codec);
5264 EXPORT_SYMBOL_HDA(snd_hda_suspend);
5267 * snd_hda_resume - resume the codecs
5270 * Returns 0 if successful.
5272 * This function is defined only when POWER_SAVE isn't set.
5273 * In the power-save mode, the codec is resumed dynamically.
5275 int snd_hda_resume(struct hda_bus *bus)
5277 struct hda_codec *codec;
5279 list_for_each_entry(codec, &bus->codec_list, list) {
5280 if (codec->patch_ops.pre_resume)
5281 codec->patch_ops.pre_resume(codec);
5282 if (snd_hda_codec_needs_resume(codec))
5283 hda_call_codec_resume(codec);
5287 EXPORT_SYMBOL_HDA(snd_hda_resume);
5288 #endif /* CONFIG_PM */
5295 * snd_array_new - get a new element from the given array
5296 * @array: the array object
5298 * Get a new element from the given array. If it exceeds the
5299 * pre-allocated array size, re-allocate the array.
5301 * Returns NULL if allocation failed.
5303 void *snd_array_new(struct snd_array *array)
5305 if (array->used >= array->alloced) {
5306 int num = array->alloced + array->alloc_align;
5307 int size = (num + 1) * array->elem_size;
5308 int oldsize = array->alloced * array->elem_size;
5310 if (snd_BUG_ON(num >= 4096))
5312 nlist = krealloc(array->list, size, GFP_KERNEL);
5315 memset(nlist + oldsize, 0, size - oldsize);
5316 array->list = nlist;
5317 array->alloced = num;
5319 return snd_array_elem(array, array->used++);
5321 EXPORT_SYMBOL_HDA(snd_array_new);
5324 * snd_array_free - free the given array elements
5325 * @array: the array object
5327 void snd_array_free(struct snd_array *array)
5334 EXPORT_SYMBOL_HDA(snd_array_free);
5337 * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer
5338 * @pcm: PCM caps bits
5339 * @buf: the string buffer to write
5340 * @buflen: the max buffer length
5342 * used by hda_proc.c and hda_eld.c
5344 void snd_print_pcm_bits(int pcm, char *buf, int buflen)
5346 static unsigned int bits[] = { 8, 16, 20, 24, 32 };
5349 for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
5350 if (pcm & (AC_SUPPCM_BITS_8 << i))
5351 j += snprintf(buf + j, buflen - j, " %d", bits[i]);
5353 buf[j] = '\0'; /* necessary when j == 0 */
5355 EXPORT_SYMBOL_HDA(snd_print_pcm_bits);
5357 MODULE_DESCRIPTION("HDA codec core");
5358 MODULE_LICENSE("GPL");