2 * Universal Interface for Intel High Definition Audio Codec
4 * Copyright (c) 2004 Takashi Iwai <tiwai@suse.de>
7 * This driver is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This driver is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include <linux/init.h>
24 #include <linux/delay.h>
25 #include <linux/slab.h>
26 #include <linux/pci.h>
27 #include <linux/mutex.h>
28 #include <linux/module.h>
29 #include <sound/core.h>
30 #include "hda_codec.h"
31 #include <sound/asoundef.h>
32 #include <sound/tlv.h>
33 #include <sound/initval.h>
34 #include <sound/jack.h>
35 #include "hda_local.h"
38 #include <sound/hda_hwdep.h>
40 #define CREATE_TRACE_POINTS
41 #include "hda_trace.h"
44 * vendor / preset table
47 struct hda_vendor_id {
52 /* codec vendor labels */
53 static struct hda_vendor_id hda_vendor_ids[] = {
55 { 0x1013, "Cirrus Logic" },
56 { 0x1057, "Motorola" },
57 { 0x1095, "Silicon Image" },
59 { 0x10ec, "Realtek" },
60 { 0x1102, "Creative" },
64 { 0x11d4, "Analog Devices" },
65 { 0x13f6, "C-Media" },
66 { 0x14f1, "Conexant" },
67 { 0x17e8, "Chrontel" },
69 { 0x1aec, "Wolfson Microelectronics" },
70 { 0x434d, "C-Media" },
72 { 0x8384, "SigmaTel" },
76 static DEFINE_MUTEX(preset_mutex);
77 static LIST_HEAD(hda_preset_tables);
79 int snd_hda_add_codec_preset(struct hda_codec_preset_list *preset)
81 mutex_lock(&preset_mutex);
82 list_add_tail(&preset->list, &hda_preset_tables);
83 mutex_unlock(&preset_mutex);
86 EXPORT_SYMBOL_HDA(snd_hda_add_codec_preset);
88 int snd_hda_delete_codec_preset(struct hda_codec_preset_list *preset)
90 mutex_lock(&preset_mutex);
91 list_del(&preset->list);
92 mutex_unlock(&preset_mutex);
95 EXPORT_SYMBOL_HDA(snd_hda_delete_codec_preset);
98 #define codec_in_pm(codec) ((codec)->in_pm)
99 static void hda_power_work(struct work_struct *work);
100 static void hda_keep_power_on(struct hda_codec *codec);
101 #define hda_codec_is_power_on(codec) ((codec)->power_on)
102 static inline void hda_call_pm_notify(struct hda_bus *bus, bool power_up)
104 if (bus->ops.pm_notify)
105 bus->ops.pm_notify(bus, power_up);
108 #define codec_in_pm(codec) 0
109 static inline void hda_keep_power_on(struct hda_codec *codec) {}
110 #define hda_codec_is_power_on(codec) 1
111 #define hda_call_pm_notify(bus, state) {}
115 * snd_hda_get_jack_location - Give a location string of the jack
116 * @cfg: pin default config value
118 * Parse the pin default config value and returns the string of the
119 * jack location, e.g. "Rear", "Front", etc.
121 const char *snd_hda_get_jack_location(u32 cfg)
123 static char *bases[7] = {
124 "N/A", "Rear", "Front", "Left", "Right", "Top", "Bottom",
126 static unsigned char specials_idx[] = {
131 static char *specials[] = {
132 "Rear Panel", "Drive Bar",
133 "Riser", "HDMI", "ATAPI",
134 "Mobile-In", "Mobile-Out"
137 cfg = (cfg & AC_DEFCFG_LOCATION) >> AC_DEFCFG_LOCATION_SHIFT;
138 if ((cfg & 0x0f) < 7)
139 return bases[cfg & 0x0f];
140 for (i = 0; i < ARRAY_SIZE(specials_idx); i++) {
141 if (cfg == specials_idx[i])
146 EXPORT_SYMBOL_HDA(snd_hda_get_jack_location);
149 * snd_hda_get_jack_connectivity - Give a connectivity string of the jack
150 * @cfg: pin default config value
152 * Parse the pin default config value and returns the string of the
153 * jack connectivity, i.e. external or internal connection.
155 const char *snd_hda_get_jack_connectivity(u32 cfg)
157 static char *jack_locations[4] = { "Ext", "Int", "Sep", "Oth" };
159 return jack_locations[(cfg >> (AC_DEFCFG_LOCATION_SHIFT + 4)) & 3];
161 EXPORT_SYMBOL_HDA(snd_hda_get_jack_connectivity);
164 * snd_hda_get_jack_type - Give a type string of the jack
165 * @cfg: pin default config value
167 * Parse the pin default config value and returns the string of the
168 * jack type, i.e. the purpose of the jack, such as Line-Out or CD.
170 const char *snd_hda_get_jack_type(u32 cfg)
172 static char *jack_types[16] = {
173 "Line Out", "Speaker", "HP Out", "CD",
174 "SPDIF Out", "Digital Out", "Modem Line", "Modem Hand",
175 "Line In", "Aux", "Mic", "Telephony",
176 "SPDIF In", "Digital In", "Reserved", "Other"
179 return jack_types[(cfg & AC_DEFCFG_DEVICE)
180 >> AC_DEFCFG_DEVICE_SHIFT];
182 EXPORT_SYMBOL_HDA(snd_hda_get_jack_type);
185 * Compose a 32bit command word to be sent to the HD-audio controller
187 static inline unsigned int
188 make_codec_cmd(struct hda_codec *codec, hda_nid_t nid, int flags,
189 unsigned int verb, unsigned int parm)
193 if ((codec->addr & ~0xf) || (nid & ~0x7f) ||
194 (verb & ~0xfff) || (parm & ~0xffff)) {
195 printk(KERN_ERR "hda-codec: out of range cmd %x:%x:%x:%x\n",
196 codec->addr, nid, verb, parm);
200 val = (u32)codec->addr << 28;
201 val |= (u32)nid << 20;
208 * Send and receive a verb
210 static int codec_exec_verb(struct hda_codec *codec, unsigned int cmd,
211 int flags, unsigned int *res)
213 struct hda_bus *bus = codec->bus;
222 snd_hda_power_up(codec);
223 mutex_lock(&bus->cmd_mutex);
225 trace_hda_send_cmd(codec, cmd);
226 err = bus->ops.command(bus, cmd);
229 /* process pending verbs */
230 bus->ops.get_response(bus, codec->addr);
233 *res = bus->ops.get_response(bus, codec->addr);
234 trace_hda_get_response(codec, *res);
236 mutex_unlock(&bus->cmd_mutex);
237 snd_hda_power_down(codec);
238 if (!codec_in_pm(codec) && res && *res == -1 && bus->rirb_error) {
239 if (bus->response_reset) {
240 snd_printd("hda_codec: resetting BUS due to "
241 "fatal communication error\n");
242 trace_hda_bus_reset(bus);
243 bus->ops.bus_reset(bus);
247 /* clear reset-flag when the communication gets recovered */
248 if (!err || codec_in_pm(codec))
249 bus->response_reset = 0;
254 * snd_hda_codec_read - send a command and get the response
255 * @codec: the HDA codec
256 * @nid: NID to send the command
257 * @flags: optional bit flags
258 * @verb: the verb to send
259 * @parm: the parameter for the verb
261 * Send a single command and read the corresponding response.
263 * Returns the obtained response value, or -1 for an error.
265 unsigned int snd_hda_codec_read(struct hda_codec *codec, hda_nid_t nid,
267 unsigned int verb, unsigned int parm)
269 unsigned cmd = make_codec_cmd(codec, nid, flags, verb, parm);
271 if (codec_exec_verb(codec, cmd, flags, &res))
275 EXPORT_SYMBOL_HDA(snd_hda_codec_read);
278 * snd_hda_codec_write - send a single command without waiting for response
279 * @codec: the HDA codec
280 * @nid: NID to send the command
281 * @flags: optional bit flags
282 * @verb: the verb to send
283 * @parm: the parameter for the verb
285 * Send a single command without waiting for response.
287 * Returns 0 if successful, or a negative error code.
289 int snd_hda_codec_write(struct hda_codec *codec, hda_nid_t nid, int flags,
290 unsigned int verb, unsigned int parm)
292 unsigned int cmd = make_codec_cmd(codec, nid, flags, verb, parm);
294 return codec_exec_verb(codec, cmd, flags,
295 codec->bus->sync_write ? &res : NULL);
297 EXPORT_SYMBOL_HDA(snd_hda_codec_write);
300 * snd_hda_sequence_write - sequence writes
301 * @codec: the HDA codec
302 * @seq: VERB array to send
304 * Send the commands sequentially from the given array.
305 * The array must be terminated with NID=0.
307 void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq)
309 for (; seq->nid; seq++)
310 snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param);
312 EXPORT_SYMBOL_HDA(snd_hda_sequence_write);
315 * snd_hda_get_sub_nodes - get the range of sub nodes
316 * @codec: the HDA codec
318 * @start_id: the pointer to store the start NID
320 * Parse the NID and store the start NID of its sub-nodes.
321 * Returns the number of sub-nodes.
323 int snd_hda_get_sub_nodes(struct hda_codec *codec, hda_nid_t nid,
328 parm = snd_hda_param_read(codec, nid, AC_PAR_NODE_COUNT);
331 *start_id = (parm >> 16) & 0x7fff;
332 return (int)(parm & 0x7fff);
334 EXPORT_SYMBOL_HDA(snd_hda_get_sub_nodes);
336 /* connection list element */
337 struct hda_conn_list {
338 struct list_head list;
344 /* look up the cached results */
345 static struct hda_conn_list *
346 lookup_conn_list(struct hda_codec *codec, hda_nid_t nid)
348 struct hda_conn_list *p;
349 list_for_each_entry(p, &codec->conn_list, list) {
356 static int add_conn_list(struct hda_codec *codec, hda_nid_t nid, int len,
357 const hda_nid_t *list)
359 struct hda_conn_list *p;
361 p = kmalloc(sizeof(*p) + len * sizeof(hda_nid_t), GFP_KERNEL);
366 memcpy(p->conns, list, len * sizeof(hda_nid_t));
367 list_add(&p->list, &codec->conn_list);
371 static void remove_conn_list(struct hda_codec *codec)
373 while (!list_empty(&codec->conn_list)) {
374 struct hda_conn_list *p;
375 p = list_first_entry(&codec->conn_list, typeof(*p), list);
381 /* read the connection and add to the cache */
382 static int read_and_add_raw_conns(struct hda_codec *codec, hda_nid_t nid)
385 hda_nid_t *result = list;
388 len = snd_hda_get_raw_connections(codec, nid, list, ARRAY_SIZE(list));
389 if (len == -ENOSPC) {
390 len = snd_hda_get_num_raw_conns(codec, nid);
391 result = kmalloc(sizeof(hda_nid_t) * len, GFP_KERNEL);
394 len = snd_hda_get_raw_connections(codec, nid, result, len);
397 len = snd_hda_override_conn_list(codec, nid, len, result);
404 * snd_hda_get_conn_list - get connection list
405 * @codec: the HDA codec
407 * @len: number of connection list entries
408 * @listp: the pointer to store NID list
410 * Parses the connection list of the given widget and stores the pointer
411 * to the list of NIDs.
413 * Returns the number of connections, or a negative error code.
415 * Note that the returned pointer isn't protected against the list
416 * modification. If snd_hda_override_conn_list() might be called
417 * concurrently, protect with a mutex appropriately.
419 int snd_hda_get_conn_list(struct hda_codec *codec, hda_nid_t nid,
420 const hda_nid_t **listp)
426 const struct hda_conn_list *p;
428 /* if the connection-list is already cached, read it */
429 p = lookup_conn_list(codec, nid);
435 if (snd_BUG_ON(added))
438 err = read_and_add_raw_conns(codec, nid);
444 EXPORT_SYMBOL_HDA(snd_hda_get_conn_list);
447 * snd_hda_get_connections - copy connection list
448 * @codec: the HDA codec
450 * @conn_list: connection list array; when NULL, checks only the size
451 * @max_conns: max. number of connections to store
453 * Parses the connection list of the given widget and stores the list
456 * Returns the number of connections, or a negative error code.
458 int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
459 hda_nid_t *conn_list, int max_conns)
461 const hda_nid_t *list;
462 int len = snd_hda_get_conn_list(codec, nid, &list);
464 if (len > 0 && conn_list) {
465 if (len > max_conns) {
466 snd_printk(KERN_ERR "hda_codec: "
467 "Too many connections %d for NID 0x%x\n",
471 memcpy(conn_list, list, len * sizeof(hda_nid_t));
476 EXPORT_SYMBOL_HDA(snd_hda_get_connections);
478 /* return CONNLIST_LEN parameter of the given widget */
479 static unsigned int get_num_conns(struct hda_codec *codec, hda_nid_t nid)
481 unsigned int wcaps = get_wcaps(codec, nid);
484 if (!(wcaps & AC_WCAP_CONN_LIST) &&
485 get_wcaps_type(wcaps) != AC_WID_VOL_KNB)
488 parm = snd_hda_param_read(codec, nid, AC_PAR_CONNLIST_LEN);
494 int snd_hda_get_num_raw_conns(struct hda_codec *codec, hda_nid_t nid)
496 return snd_hda_get_raw_connections(codec, nid, NULL, 0);
500 * snd_hda_get_raw_connections - copy connection list without cache
501 * @codec: the HDA codec
503 * @conn_list: connection list array
504 * @max_conns: max. number of connections to store
506 * Like snd_hda_get_connections(), copy the connection list but without
507 * checking through the connection-list cache.
508 * Currently called only from hda_proc.c, so not exported.
510 int snd_hda_get_raw_connections(struct hda_codec *codec, hda_nid_t nid,
511 hda_nid_t *conn_list, int max_conns)
514 int i, conn_len, conns;
515 unsigned int shift, num_elems, mask;
519 parm = get_num_conns(codec, nid);
523 if (parm & AC_CLIST_LONG) {
532 conn_len = parm & AC_CLIST_LENGTH;
533 mask = (1 << (shift-1)) - 1;
536 return 0; /* no connection */
539 /* single connection */
540 parm = snd_hda_codec_read(codec, nid, 0,
541 AC_VERB_GET_CONNECT_LIST, 0);
542 if (parm == -1 && codec->bus->rirb_error)
545 conn_list[0] = parm & mask;
549 /* multi connection */
552 for (i = 0; i < conn_len; i++) {
556 if (i % num_elems == 0) {
557 parm = snd_hda_codec_read(codec, nid, 0,
558 AC_VERB_GET_CONNECT_LIST, i);
559 if (parm == -1 && codec->bus->rirb_error)
562 range_val = !!(parm & (1 << (shift-1))); /* ranges */
564 if (val == 0 && null_count++) { /* no second chance */
565 snd_printk(KERN_WARNING "hda_codec: "
566 "invalid CONNECT_LIST verb %x[%i]:%x\n",
572 /* ranges between the previous and this one */
573 if (!prev_nid || prev_nid >= val) {
574 snd_printk(KERN_WARNING "hda_codec: "
575 "invalid dep_range_val %x:%x\n",
579 for (n = prev_nid + 1; n <= val; n++) {
581 if (conns >= max_conns)
583 conn_list[conns] = n;
589 if (conns >= max_conns)
591 conn_list[conns] = val;
601 * snd_hda_override_conn_list - add/modify the connection-list to cache
602 * @codec: the HDA codec
604 * @len: number of connection list entries
605 * @list: the list of connection entries
607 * Add or modify the given connection-list to the cache. If the corresponding
608 * cache already exists, invalidate it and append a new one.
610 * Returns zero or a negative error code.
612 int snd_hda_override_conn_list(struct hda_codec *codec, hda_nid_t nid, int len,
613 const hda_nid_t *list)
615 struct hda_conn_list *p;
617 p = lookup_conn_list(codec, nid);
623 return add_conn_list(codec, nid, len, list);
625 EXPORT_SYMBOL_HDA(snd_hda_override_conn_list);
628 * snd_hda_get_conn_index - get the connection index of the given NID
629 * @codec: the HDA codec
630 * @mux: NID containing the list
631 * @nid: NID to select
632 * @recursive: 1 when searching NID recursively, otherwise 0
634 * Parses the connection list of the widget @mux and checks whether the
635 * widget @nid is present. If it is, return the connection index.
636 * Otherwise it returns -1.
638 int snd_hda_get_conn_index(struct hda_codec *codec, hda_nid_t mux,
639 hda_nid_t nid, int recursive)
641 const hda_nid_t *conn;
644 nums = snd_hda_get_conn_list(codec, mux, &conn);
645 for (i = 0; i < nums; i++)
650 if (recursive > 10) {
651 snd_printd("hda_codec: too deep connection for 0x%x\n", nid);
655 for (i = 0; i < nums; i++) {
656 unsigned int type = get_wcaps_type(get_wcaps(codec, conn[i]));
657 if (type == AC_WID_PIN || type == AC_WID_AUD_OUT)
659 if (snd_hda_get_conn_index(codec, conn[i], nid, recursive) >= 0)
664 EXPORT_SYMBOL_HDA(snd_hda_get_conn_index);
667 * snd_hda_queue_unsol_event - add an unsolicited event to queue
669 * @res: unsolicited event (lower 32bit of RIRB entry)
670 * @res_ex: codec addr and flags (upper 32bit or RIRB entry)
672 * Adds the given event to the queue. The events are processed in
673 * the workqueue asynchronously. Call this function in the interrupt
674 * hanlder when RIRB receives an unsolicited event.
676 * Returns 0 if successful, or a negative error code.
678 int snd_hda_queue_unsol_event(struct hda_bus *bus, u32 res, u32 res_ex)
680 struct hda_bus_unsolicited *unsol;
683 if (!bus || !bus->workq)
686 trace_hda_unsol_event(bus, res, res_ex);
691 wp = (unsol->wp + 1) % HDA_UNSOL_QUEUE_SIZE;
695 unsol->queue[wp] = res;
696 unsol->queue[wp + 1] = res_ex;
698 queue_work(bus->workq, &unsol->work);
702 EXPORT_SYMBOL_HDA(snd_hda_queue_unsol_event);
705 * process queued unsolicited events
707 static void process_unsol_events(struct work_struct *work)
709 struct hda_bus_unsolicited *unsol =
710 container_of(work, struct hda_bus_unsolicited, work);
711 struct hda_bus *bus = unsol->bus;
712 struct hda_codec *codec;
713 unsigned int rp, caddr, res;
715 while (unsol->rp != unsol->wp) {
716 rp = (unsol->rp + 1) % HDA_UNSOL_QUEUE_SIZE;
719 res = unsol->queue[rp];
720 caddr = unsol->queue[rp + 1];
721 if (!(caddr & (1 << 4))) /* no unsolicited event? */
723 codec = bus->caddr_tbl[caddr & 0x0f];
724 if (codec && codec->patch_ops.unsol_event)
725 codec->patch_ops.unsol_event(codec, res);
730 * initialize unsolicited queue
732 static int init_unsol_queue(struct hda_bus *bus)
734 struct hda_bus_unsolicited *unsol;
736 if (bus->unsol) /* already initialized */
739 unsol = kzalloc(sizeof(*unsol), GFP_KERNEL);
741 snd_printk(KERN_ERR "hda_codec: "
742 "can't allocate unsolicited queue\n");
745 INIT_WORK(&unsol->work, process_unsol_events);
754 static void snd_hda_codec_free(struct hda_codec *codec);
756 static int snd_hda_bus_free(struct hda_bus *bus)
758 struct hda_codec *codec, *n;
763 flush_workqueue(bus->workq);
766 list_for_each_entry_safe(codec, n, &bus->codec_list, list) {
767 snd_hda_codec_free(codec);
769 if (bus->ops.private_free)
770 bus->ops.private_free(bus);
772 destroy_workqueue(bus->workq);
777 static int snd_hda_bus_dev_free(struct snd_device *device)
779 struct hda_bus *bus = device->device_data;
781 return snd_hda_bus_free(bus);
784 #ifdef CONFIG_SND_HDA_HWDEP
785 static int snd_hda_bus_dev_register(struct snd_device *device)
787 struct hda_bus *bus = device->device_data;
788 struct hda_codec *codec;
789 list_for_each_entry(codec, &bus->codec_list, list) {
790 snd_hda_hwdep_add_sysfs(codec);
791 snd_hda_hwdep_add_power_sysfs(codec);
796 #define snd_hda_bus_dev_register NULL
800 * snd_hda_bus_new - create a HDA bus
801 * @card: the card entry
802 * @temp: the template for hda_bus information
803 * @busp: the pointer to store the created bus instance
805 * Returns 0 if successful, or a negative error code.
807 int snd_hda_bus_new(struct snd_card *card,
808 const struct hda_bus_template *temp,
809 struct hda_bus **busp)
813 static struct snd_device_ops dev_ops = {
814 .dev_register = snd_hda_bus_dev_register,
815 .dev_free = snd_hda_bus_dev_free,
818 if (snd_BUG_ON(!temp))
820 if (snd_BUG_ON(!temp->ops.command || !temp->ops.get_response))
826 bus = kzalloc(sizeof(*bus), GFP_KERNEL);
828 snd_printk(KERN_ERR "can't allocate struct hda_bus\n");
833 bus->private_data = temp->private_data;
834 bus->pci = temp->pci;
835 bus->modelname = temp->modelname;
836 bus->power_save = temp->power_save;
837 bus->ops = temp->ops;
839 mutex_init(&bus->cmd_mutex);
840 mutex_init(&bus->prepare_mutex);
841 INIT_LIST_HEAD(&bus->codec_list);
843 snprintf(bus->workq_name, sizeof(bus->workq_name),
844 "hd-audio%d", card->number);
845 bus->workq = create_singlethread_workqueue(bus->workq_name);
847 snd_printk(KERN_ERR "cannot create workqueue %s\n",
853 err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops);
855 snd_hda_bus_free(bus);
862 EXPORT_SYMBOL_HDA(snd_hda_bus_new);
864 #ifdef CONFIG_SND_HDA_GENERIC
865 #define is_generic_config(codec) \
866 (codec->modelname && !strcmp(codec->modelname, "generic"))
868 #define is_generic_config(codec) 0
872 #define HDA_MODREQ_MAX_COUNT 2 /* two request_modules()'s */
874 #define HDA_MODREQ_MAX_COUNT 0 /* all presets are statically linked */
878 * find a matching codec preset
880 static const struct hda_codec_preset *
881 find_codec_preset(struct hda_codec *codec)
883 struct hda_codec_preset_list *tbl;
884 const struct hda_codec_preset *preset;
885 unsigned int mod_requested = 0;
887 if (is_generic_config(codec))
888 return NULL; /* use the generic parser */
891 mutex_lock(&preset_mutex);
892 list_for_each_entry(tbl, &hda_preset_tables, list) {
893 if (!try_module_get(tbl->owner)) {
894 snd_printk(KERN_ERR "hda_codec: cannot module_get\n");
897 for (preset = tbl->preset; preset->id; preset++) {
898 u32 mask = preset->mask;
899 if (preset->afg && preset->afg != codec->afg)
901 if (preset->mfg && preset->mfg != codec->mfg)
905 if (preset->id == (codec->vendor_id & mask) &&
907 preset->rev == codec->revision_id)) {
908 mutex_unlock(&preset_mutex);
909 codec->owner = tbl->owner;
913 module_put(tbl->owner);
915 mutex_unlock(&preset_mutex);
917 if (mod_requested < HDA_MODREQ_MAX_COUNT) {
920 snprintf(name, sizeof(name), "snd-hda-codec-id:%08x",
923 snprintf(name, sizeof(name), "snd-hda-codec-id:%04x*",
924 (codec->vendor_id >> 16) & 0xffff);
925 request_module(name);
933 * get_codec_name - store the codec name
935 static int get_codec_name(struct hda_codec *codec)
937 const struct hda_vendor_id *c;
938 const char *vendor = NULL;
939 u16 vendor_id = codec->vendor_id >> 16;
942 if (codec->vendor_name)
945 for (c = hda_vendor_ids; c->id; c++) {
946 if (c->id == vendor_id) {
952 sprintf(tmp, "Generic %04x", vendor_id);
955 codec->vendor_name = kstrdup(vendor, GFP_KERNEL);
956 if (!codec->vendor_name)
960 if (codec->chip_name)
963 if (codec->preset && codec->preset->name)
964 codec->chip_name = kstrdup(codec->preset->name, GFP_KERNEL);
966 sprintf(tmp, "ID %x", codec->vendor_id & 0xffff);
967 codec->chip_name = kstrdup(tmp, GFP_KERNEL);
969 if (!codec->chip_name)
975 * look for an AFG and MFG nodes
977 static void setup_fg_nodes(struct hda_codec *codec)
979 int i, total_nodes, function_id;
982 total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid);
983 for (i = 0; i < total_nodes; i++, nid++) {
984 function_id = snd_hda_param_read(codec, nid,
985 AC_PAR_FUNCTION_TYPE);
986 switch (function_id & 0xff) {
987 case AC_GRP_AUDIO_FUNCTION:
989 codec->afg_function_id = function_id & 0xff;
990 codec->afg_unsol = (function_id >> 8) & 1;
992 case AC_GRP_MODEM_FUNCTION:
994 codec->mfg_function_id = function_id & 0xff;
995 codec->mfg_unsol = (function_id >> 8) & 1;
1004 * read widget caps for each widget and store in cache
1006 static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
1011 codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node,
1013 codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL);
1016 nid = codec->start_nid;
1017 for (i = 0; i < codec->num_nodes; i++, nid++)
1018 codec->wcaps[i] = snd_hda_param_read(codec, nid,
1019 AC_PAR_AUDIO_WIDGET_CAP);
1023 /* read all pin default configurations and save codec->init_pins */
1024 static int read_pin_defaults(struct hda_codec *codec)
1027 hda_nid_t nid = codec->start_nid;
1029 for (i = 0; i < codec->num_nodes; i++, nid++) {
1030 struct hda_pincfg *pin;
1031 unsigned int wcaps = get_wcaps(codec, nid);
1032 unsigned int wid_type = get_wcaps_type(wcaps);
1033 if (wid_type != AC_WID_PIN)
1035 pin = snd_array_new(&codec->init_pins);
1039 pin->cfg = snd_hda_codec_read(codec, nid, 0,
1040 AC_VERB_GET_CONFIG_DEFAULT, 0);
1041 pin->ctrl = snd_hda_codec_read(codec, nid, 0,
1042 AC_VERB_GET_PIN_WIDGET_CONTROL,
1048 /* look up the given pin config list and return the item matching with NID */
1049 static struct hda_pincfg *look_up_pincfg(struct hda_codec *codec,
1050 struct snd_array *array,
1054 for (i = 0; i < array->used; i++) {
1055 struct hda_pincfg *pin = snd_array_elem(array, i);
1056 if (pin->nid == nid)
1062 /* set the current pin config value for the given NID.
1063 * the value is cached, and read via snd_hda_codec_get_pincfg()
1065 int snd_hda_add_pincfg(struct hda_codec *codec, struct snd_array *list,
1066 hda_nid_t nid, unsigned int cfg)
1068 struct hda_pincfg *pin;
1070 /* the check below may be invalid when pins are added by a fixup
1071 * dynamically (e.g. via snd_hda_codec_update_widgets()), so disabled
1075 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN)
1079 pin = look_up_pincfg(codec, list, nid);
1081 pin = snd_array_new(list);
1091 * snd_hda_codec_set_pincfg - Override a pin default configuration
1092 * @codec: the HDA codec
1093 * @nid: NID to set the pin config
1094 * @cfg: the pin default config value
1096 * Override a pin default configuration value in the cache.
1097 * This value can be read by snd_hda_codec_get_pincfg() in a higher
1098 * priority than the real hardware value.
1100 int snd_hda_codec_set_pincfg(struct hda_codec *codec,
1101 hda_nid_t nid, unsigned int cfg)
1103 return snd_hda_add_pincfg(codec, &codec->driver_pins, nid, cfg);
1105 EXPORT_SYMBOL_HDA(snd_hda_codec_set_pincfg);
1108 * snd_hda_codec_get_pincfg - Obtain a pin-default configuration
1109 * @codec: the HDA codec
1110 * @nid: NID to get the pin config
1112 * Get the current pin config value of the given pin NID.
1113 * If the pincfg value is cached or overridden via sysfs or driver,
1114 * returns the cached value.
1116 unsigned int snd_hda_codec_get_pincfg(struct hda_codec *codec, hda_nid_t nid)
1118 struct hda_pincfg *pin;
1120 #ifdef CONFIG_SND_HDA_HWDEP
1122 unsigned int cfg = 0;
1123 mutex_lock(&codec->user_mutex);
1124 pin = look_up_pincfg(codec, &codec->user_pins, nid);
1127 mutex_unlock(&codec->user_mutex);
1132 pin = look_up_pincfg(codec, &codec->driver_pins, nid);
1135 pin = look_up_pincfg(codec, &codec->init_pins, nid);
1140 EXPORT_SYMBOL_HDA(snd_hda_codec_get_pincfg);
1142 /* remember the current pinctl target value */
1143 int snd_hda_codec_set_pin_target(struct hda_codec *codec, hda_nid_t nid,
1146 struct hda_pincfg *pin;
1148 pin = look_up_pincfg(codec, &codec->init_pins, nid);
1154 EXPORT_SYMBOL_HDA(snd_hda_codec_set_pin_target);
1156 /* return the current pinctl target value */
1157 int snd_hda_codec_get_pin_target(struct hda_codec *codec, hda_nid_t nid)
1159 struct hda_pincfg *pin;
1161 pin = look_up_pincfg(codec, &codec->init_pins, nid);
1166 EXPORT_SYMBOL_HDA(snd_hda_codec_get_pin_target);
1169 * snd_hda_shutup_pins - Shut up all pins
1170 * @codec: the HDA codec
1172 * Clear all pin controls to shup up before suspend for avoiding click noise.
1173 * The controls aren't cached so that they can be resumed properly.
1175 void snd_hda_shutup_pins(struct hda_codec *codec)
1178 /* don't shut up pins when unloading the driver; otherwise it breaks
1179 * the default pin setup at the next load of the driver
1181 if (codec->bus->shutdown)
1183 for (i = 0; i < codec->init_pins.used; i++) {
1184 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1185 /* use read here for syncing after issuing each verb */
1186 snd_hda_codec_read(codec, pin->nid, 0,
1187 AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
1189 codec->pins_shutup = 1;
1191 EXPORT_SYMBOL_HDA(snd_hda_shutup_pins);
1194 /* Restore the pin controls cleared previously via snd_hda_shutup_pins() */
1195 static void restore_shutup_pins(struct hda_codec *codec)
1198 if (!codec->pins_shutup)
1200 if (codec->bus->shutdown)
1202 for (i = 0; i < codec->init_pins.used; i++) {
1203 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1204 snd_hda_codec_write(codec, pin->nid, 0,
1205 AC_VERB_SET_PIN_WIDGET_CONTROL,
1208 codec->pins_shutup = 0;
1212 static void hda_jackpoll_work(struct work_struct *work)
1214 struct hda_codec *codec =
1215 container_of(work, struct hda_codec, jackpoll_work.work);
1216 if (!codec->jackpoll_interval)
1219 snd_hda_jack_set_dirty_all(codec);
1220 snd_hda_jack_poll_all(codec);
1221 queue_delayed_work(codec->bus->workq, &codec->jackpoll_work,
1222 codec->jackpoll_interval);
1225 static void init_hda_cache(struct hda_cache_rec *cache,
1226 unsigned int record_size);
1227 static void free_hda_cache(struct hda_cache_rec *cache);
1229 /* release all pincfg lists */
1230 static void free_init_pincfgs(struct hda_codec *codec)
1232 snd_array_free(&codec->driver_pins);
1233 #ifdef CONFIG_SND_HDA_HWDEP
1234 snd_array_free(&codec->user_pins);
1236 snd_array_free(&codec->init_pins);
1240 * audio-converter setup caches
1242 struct hda_cvt_setup {
1247 unsigned char active; /* cvt is currently used */
1248 unsigned char dirty; /* setups should be cleared */
1251 /* get or create a cache entry for the given audio converter NID */
1252 static struct hda_cvt_setup *
1253 get_hda_cvt_setup(struct hda_codec *codec, hda_nid_t nid)
1255 struct hda_cvt_setup *p;
1258 for (i = 0; i < codec->cvt_setups.used; i++) {
1259 p = snd_array_elem(&codec->cvt_setups, i);
1263 p = snd_array_new(&codec->cvt_setups);
1272 static void snd_hda_codec_free(struct hda_codec *codec)
1276 cancel_delayed_work_sync(&codec->jackpoll_work);
1277 snd_hda_jack_tbl_clear(codec);
1278 free_init_pincfgs(codec);
1280 cancel_delayed_work(&codec->power_work);
1281 flush_workqueue(codec->bus->workq);
1283 list_del(&codec->list);
1284 snd_array_free(&codec->mixers);
1285 snd_array_free(&codec->nids);
1286 snd_array_free(&codec->cvt_setups);
1287 snd_array_free(&codec->spdif_out);
1288 remove_conn_list(codec);
1289 codec->bus->caddr_tbl[codec->addr] = NULL;
1290 if (codec->patch_ops.free)
1291 codec->patch_ops.free(codec);
1293 if (!codec->pm_down_notified) /* cancel leftover refcounts */
1294 hda_call_pm_notify(codec->bus, false);
1296 module_put(codec->owner);
1297 free_hda_cache(&codec->amp_cache);
1298 free_hda_cache(&codec->cmd_cache);
1299 kfree(codec->vendor_name);
1300 kfree(codec->chip_name);
1301 kfree(codec->modelname);
1302 kfree(codec->wcaps);
1306 static bool snd_hda_codec_get_supported_ps(struct hda_codec *codec,
1307 hda_nid_t fg, unsigned int power_state);
1309 static unsigned int hda_set_power_state(struct hda_codec *codec,
1310 unsigned int power_state);
1313 * snd_hda_codec_new - create a HDA codec
1314 * @bus: the bus to assign
1315 * @codec_addr: the codec address
1316 * @codecp: the pointer to store the generated codec
1318 * Returns 0 if successful, or a negative error code.
1320 int snd_hda_codec_new(struct hda_bus *bus,
1321 unsigned int codec_addr,
1322 struct hda_codec **codecp)
1324 struct hda_codec *codec;
1329 if (snd_BUG_ON(!bus))
1331 if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS))
1334 if (bus->caddr_tbl[codec_addr]) {
1335 snd_printk(KERN_ERR "hda_codec: "
1336 "address 0x%x is already occupied\n", codec_addr);
1340 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
1341 if (codec == NULL) {
1342 snd_printk(KERN_ERR "can't allocate struct hda_codec\n");
1347 codec->addr = codec_addr;
1348 mutex_init(&codec->spdif_mutex);
1349 mutex_init(&codec->control_mutex);
1350 mutex_init(&codec->hash_mutex);
1351 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
1352 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
1353 snd_array_init(&codec->mixers, sizeof(struct hda_nid_item), 32);
1354 snd_array_init(&codec->nids, sizeof(struct hda_nid_item), 32);
1355 snd_array_init(&codec->init_pins, sizeof(struct hda_pincfg), 16);
1356 snd_array_init(&codec->driver_pins, sizeof(struct hda_pincfg), 16);
1357 snd_array_init(&codec->cvt_setups, sizeof(struct hda_cvt_setup), 8);
1358 snd_array_init(&codec->spdif_out, sizeof(struct hda_spdif_out), 16);
1359 snd_array_init(&codec->jacktbl, sizeof(struct hda_jack_tbl), 16);
1360 snd_array_init(&codec->verbs, sizeof(struct hda_verb *), 8);
1361 INIT_LIST_HEAD(&codec->conn_list);
1363 INIT_DELAYED_WORK(&codec->jackpoll_work, hda_jackpoll_work);
1366 spin_lock_init(&codec->power_lock);
1367 INIT_DELAYED_WORK(&codec->power_work, hda_power_work);
1368 /* snd_hda_codec_new() marks the codec as power-up, and leave it as is.
1369 * the caller has to power down appropriatley after initialization
1372 hda_keep_power_on(codec);
1373 hda_call_pm_notify(bus, true);
1376 if (codec->bus->modelname) {
1377 codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL);
1378 if (!codec->modelname) {
1379 snd_hda_codec_free(codec);
1384 list_add_tail(&codec->list, &bus->codec_list);
1385 bus->caddr_tbl[codec_addr] = codec;
1387 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1389 if (codec->vendor_id == -1)
1390 /* read again, hopefully the access method was corrected
1391 * in the last read...
1393 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1395 codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1396 AC_PAR_SUBSYSTEM_ID);
1397 codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1400 setup_fg_nodes(codec);
1401 if (!codec->afg && !codec->mfg) {
1402 snd_printdd("hda_codec: no AFG or MFG node found\n");
1407 fg = codec->afg ? codec->afg : codec->mfg;
1408 err = read_widget_caps(codec, fg);
1410 snd_printk(KERN_ERR "hda_codec: cannot malloc\n");
1413 err = read_pin_defaults(codec);
1417 if (!codec->subsystem_id) {
1418 codec->subsystem_id =
1419 snd_hda_codec_read(codec, fg, 0,
1420 AC_VERB_GET_SUBSYSTEM_ID, 0);
1424 codec->d3_stop_clk = snd_hda_codec_get_supported_ps(codec, fg,
1426 if (!codec->d3_stop_clk)
1427 bus->power_keep_link_on = 1;
1429 codec->epss = snd_hda_codec_get_supported_ps(codec, fg,
1432 /* power-up all before initialization */
1433 hda_set_power_state(codec, AC_PWRST_D0);
1435 snd_hda_codec_proc_new(codec);
1437 snd_hda_create_hwdep(codec);
1439 sprintf(component, "HDA:%08x,%08x,%08x", codec->vendor_id,
1440 codec->subsystem_id, codec->revision_id);
1441 snd_component_add(codec->bus->card, component);
1448 snd_hda_codec_free(codec);
1451 EXPORT_SYMBOL_HDA(snd_hda_codec_new);
1453 int snd_hda_codec_update_widgets(struct hda_codec *codec)
1458 /* Assume the function group node does not change,
1459 * only the widget nodes may change.
1461 kfree(codec->wcaps);
1462 fg = codec->afg ? codec->afg : codec->mfg;
1463 err = read_widget_caps(codec, fg);
1465 snd_printk(KERN_ERR "hda_codec: cannot malloc\n");
1469 snd_array_free(&codec->init_pins);
1470 err = read_pin_defaults(codec);
1474 EXPORT_SYMBOL_HDA(snd_hda_codec_update_widgets);
1478 * snd_hda_codec_configure - (Re-)configure the HD-audio codec
1479 * @codec: the HDA codec
1481 * Start parsing of the given codec tree and (re-)initialize the whole
1484 * Returns 0 if successful or a negative error code.
1486 int snd_hda_codec_configure(struct hda_codec *codec)
1490 codec->preset = find_codec_preset(codec);
1491 if (!codec->vendor_name || !codec->chip_name) {
1492 err = get_codec_name(codec);
1497 if (is_generic_config(codec)) {
1498 err = snd_hda_parse_generic_codec(codec);
1501 if (codec->preset && codec->preset->patch) {
1502 err = codec->preset->patch(codec);
1506 /* call the default parser */
1507 err = snd_hda_parse_generic_codec(codec);
1509 printk(KERN_ERR "hda-codec: No codec parser is available\n");
1512 if (!err && codec->patch_ops.unsol_event)
1513 err = init_unsol_queue(codec->bus);
1514 /* audio codec should override the mixer name */
1515 if (!err && (codec->afg || !*codec->bus->card->mixername))
1516 snprintf(codec->bus->card->mixername,
1517 sizeof(codec->bus->card->mixername),
1518 "%s %s", codec->vendor_name, codec->chip_name);
1521 EXPORT_SYMBOL_HDA(snd_hda_codec_configure);
1523 /* update the stream-id if changed */
1524 static void update_pcm_stream_id(struct hda_codec *codec,
1525 struct hda_cvt_setup *p, hda_nid_t nid,
1526 u32 stream_tag, int channel_id)
1528 unsigned int oldval, newval;
1530 if (p->stream_tag != stream_tag || p->channel_id != channel_id) {
1531 oldval = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0);
1532 newval = (stream_tag << 4) | channel_id;
1533 if (oldval != newval)
1534 snd_hda_codec_write(codec, nid, 0,
1535 AC_VERB_SET_CHANNEL_STREAMID,
1537 p->stream_tag = stream_tag;
1538 p->channel_id = channel_id;
1542 /* update the format-id if changed */
1543 static void update_pcm_format(struct hda_codec *codec, struct hda_cvt_setup *p,
1544 hda_nid_t nid, int format)
1546 unsigned int oldval;
1548 if (p->format_id != format) {
1549 oldval = snd_hda_codec_read(codec, nid, 0,
1550 AC_VERB_GET_STREAM_FORMAT, 0);
1551 if (oldval != format) {
1553 snd_hda_codec_write(codec, nid, 0,
1554 AC_VERB_SET_STREAM_FORMAT,
1557 p->format_id = format;
1562 * snd_hda_codec_setup_stream - set up the codec for streaming
1563 * @codec: the CODEC to set up
1564 * @nid: the NID to set up
1565 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
1566 * @channel_id: channel id to pass, zero based.
1567 * @format: stream format.
1569 void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
1571 int channel_id, int format)
1573 struct hda_codec *c;
1574 struct hda_cvt_setup *p;
1581 snd_printdd("hda_codec_setup_stream: "
1582 "NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
1583 nid, stream_tag, channel_id, format);
1584 p = get_hda_cvt_setup(codec, nid);
1588 if (codec->pcm_format_first)
1589 update_pcm_format(codec, p, nid, format);
1590 update_pcm_stream_id(codec, p, nid, stream_tag, channel_id);
1591 if (!codec->pcm_format_first)
1592 update_pcm_format(codec, p, nid, format);
1597 /* make other inactive cvts with the same stream-tag dirty */
1598 type = get_wcaps_type(get_wcaps(codec, nid));
1599 list_for_each_entry(c, &codec->bus->codec_list, list) {
1600 for (i = 0; i < c->cvt_setups.used; i++) {
1601 p = snd_array_elem(&c->cvt_setups, i);
1602 if (!p->active && p->stream_tag == stream_tag &&
1603 get_wcaps_type(get_wcaps(c, p->nid)) == type)
1608 EXPORT_SYMBOL_HDA(snd_hda_codec_setup_stream);
1610 static void really_cleanup_stream(struct hda_codec *codec,
1611 struct hda_cvt_setup *q);
1614 * __snd_hda_codec_cleanup_stream - clean up the codec for closing
1615 * @codec: the CODEC to clean up
1616 * @nid: the NID to clean up
1617 * @do_now: really clean up the stream instead of clearing the active flag
1619 void __snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid,
1622 struct hda_cvt_setup *p;
1627 if (codec->no_sticky_stream)
1630 snd_printdd("hda_codec_cleanup_stream: NID=0x%x\n", nid);
1631 p = get_hda_cvt_setup(codec, nid);
1633 /* here we just clear the active flag when do_now isn't set;
1634 * actual clean-ups will be done later in
1635 * purify_inactive_streams() called from snd_hda_codec_prpapre()
1638 really_cleanup_stream(codec, p);
1643 EXPORT_SYMBOL_HDA(__snd_hda_codec_cleanup_stream);
1645 static void really_cleanup_stream(struct hda_codec *codec,
1646 struct hda_cvt_setup *q)
1648 hda_nid_t nid = q->nid;
1649 if (q->stream_tag || q->channel_id)
1650 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
1652 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0
1654 memset(q, 0, sizeof(*q));
1658 /* clean up the all conflicting obsolete streams */
1659 static void purify_inactive_streams(struct hda_codec *codec)
1661 struct hda_codec *c;
1664 list_for_each_entry(c, &codec->bus->codec_list, list) {
1665 for (i = 0; i < c->cvt_setups.used; i++) {
1666 struct hda_cvt_setup *p;
1667 p = snd_array_elem(&c->cvt_setups, i);
1669 really_cleanup_stream(c, p);
1675 /* clean up all streams; called from suspend */
1676 static void hda_cleanup_all_streams(struct hda_codec *codec)
1680 for (i = 0; i < codec->cvt_setups.used; i++) {
1681 struct hda_cvt_setup *p = snd_array_elem(&codec->cvt_setups, i);
1683 really_cleanup_stream(codec, p);
1689 * amp access functions
1692 /* FIXME: more better hash key? */
1693 #define HDA_HASH_KEY(nid, dir, idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
1694 #define HDA_HASH_PINCAP_KEY(nid) (u32)((nid) + (0x02 << 24))
1695 #define HDA_HASH_PARPCM_KEY(nid) (u32)((nid) + (0x03 << 24))
1696 #define HDA_HASH_PARSTR_KEY(nid) (u32)((nid) + (0x04 << 24))
1697 #define INFO_AMP_CAPS (1<<0)
1698 #define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
1700 /* initialize the hash table */
1701 static void init_hda_cache(struct hda_cache_rec *cache,
1702 unsigned int record_size)
1704 memset(cache, 0, sizeof(*cache));
1705 memset(cache->hash, 0xff, sizeof(cache->hash));
1706 snd_array_init(&cache->buf, record_size, 64);
1709 static void free_hda_cache(struct hda_cache_rec *cache)
1711 snd_array_free(&cache->buf);
1714 /* query the hash. allocate an entry if not found. */
1715 static struct hda_cache_head *get_hash(struct hda_cache_rec *cache, u32 key)
1717 u16 idx = key % (u16)ARRAY_SIZE(cache->hash);
1718 u16 cur = cache->hash[idx];
1719 struct hda_cache_head *info;
1721 while (cur != 0xffff) {
1722 info = snd_array_elem(&cache->buf, cur);
1723 if (info->key == key)
1730 /* query the hash. allocate an entry if not found. */
1731 static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache,
1734 struct hda_cache_head *info = get_hash(cache, key);
1737 /* add a new hash entry */
1738 info = snd_array_new(&cache->buf);
1741 cur = snd_array_index(&cache->buf, info);
1745 idx = key % (u16)ARRAY_SIZE(cache->hash);
1746 info->next = cache->hash[idx];
1747 cache->hash[idx] = cur;
1752 /* query and allocate an amp hash entry */
1753 static inline struct hda_amp_info *
1754 get_alloc_amp_hash(struct hda_codec *codec, u32 key)
1756 return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key);
1759 /* overwrite the value with the key in the caps hash */
1760 static int write_caps_hash(struct hda_codec *codec, u32 key, unsigned int val)
1762 struct hda_amp_info *info;
1764 mutex_lock(&codec->hash_mutex);
1765 info = get_alloc_amp_hash(codec, key);
1767 mutex_unlock(&codec->hash_mutex);
1770 info->amp_caps = val;
1771 info->head.val |= INFO_AMP_CAPS;
1772 mutex_unlock(&codec->hash_mutex);
1776 /* query the value from the caps hash; if not found, fetch the current
1777 * value from the given function and store in the hash
1780 query_caps_hash(struct hda_codec *codec, hda_nid_t nid, int dir, u32 key,
1781 unsigned int (*func)(struct hda_codec *, hda_nid_t, int))
1783 struct hda_amp_info *info;
1786 mutex_lock(&codec->hash_mutex);
1787 info = get_alloc_amp_hash(codec, key);
1789 mutex_unlock(&codec->hash_mutex);
1792 if (!(info->head.val & INFO_AMP_CAPS)) {
1793 mutex_unlock(&codec->hash_mutex); /* for reentrance */
1794 val = func(codec, nid, dir);
1795 write_caps_hash(codec, key, val);
1797 val = info->amp_caps;
1798 mutex_unlock(&codec->hash_mutex);
1803 static unsigned int read_amp_cap(struct hda_codec *codec, hda_nid_t nid,
1806 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
1808 return snd_hda_param_read(codec, nid,
1809 direction == HDA_OUTPUT ?
1810 AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP);
1814 * query_amp_caps - query AMP capabilities
1815 * @codec: the HD-auio codec
1816 * @nid: the NID to query
1817 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1819 * Query AMP capabilities for the given widget and direction.
1820 * Returns the obtained capability bits.
1822 * When cap bits have been already read, this doesn't read again but
1823 * returns the cached value.
1825 u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
1827 return query_caps_hash(codec, nid, direction,
1828 HDA_HASH_KEY(nid, direction, 0),
1831 EXPORT_SYMBOL_HDA(query_amp_caps);
1834 * snd_hda_override_amp_caps - Override the AMP capabilities
1835 * @codec: the CODEC to clean up
1836 * @nid: the NID to clean up
1837 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1838 * @caps: the capability bits to set
1840 * Override the cached AMP caps bits value by the given one.
1841 * This function is useful if the driver needs to adjust the AMP ranges,
1842 * e.g. limit to 0dB, etc.
1844 * Returns zero if successful or a negative error code.
1846 int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
1849 return write_caps_hash(codec, HDA_HASH_KEY(nid, dir, 0), caps);
1851 EXPORT_SYMBOL_HDA(snd_hda_override_amp_caps);
1853 static unsigned int read_pin_cap(struct hda_codec *codec, hda_nid_t nid,
1856 return snd_hda_param_read(codec, nid, AC_PAR_PIN_CAP);
1860 * snd_hda_query_pin_caps - Query PIN capabilities
1861 * @codec: the HD-auio codec
1862 * @nid: the NID to query
1864 * Query PIN capabilities for the given widget.
1865 * Returns the obtained capability bits.
1867 * When cap bits have been already read, this doesn't read again but
1868 * returns the cached value.
1870 u32 snd_hda_query_pin_caps(struct hda_codec *codec, hda_nid_t nid)
1872 return query_caps_hash(codec, nid, 0, HDA_HASH_PINCAP_KEY(nid),
1875 EXPORT_SYMBOL_HDA(snd_hda_query_pin_caps);
1878 * snd_hda_override_pin_caps - Override the pin capabilities
1880 * @nid: the NID to override
1881 * @caps: the capability bits to set
1883 * Override the cached PIN capabilitiy bits value by the given one.
1885 * Returns zero if successful or a negative error code.
1887 int snd_hda_override_pin_caps(struct hda_codec *codec, hda_nid_t nid,
1890 return write_caps_hash(codec, HDA_HASH_PINCAP_KEY(nid), caps);
1892 EXPORT_SYMBOL_HDA(snd_hda_override_pin_caps);
1894 /* read or sync the hash value with the current value;
1895 * call within hash_mutex
1897 static struct hda_amp_info *
1898 update_amp_hash(struct hda_codec *codec, hda_nid_t nid, int ch,
1899 int direction, int index, bool init_only)
1901 struct hda_amp_info *info;
1902 unsigned int parm, val = 0;
1903 bool val_read = false;
1906 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
1909 if (!(info->head.val & INFO_AMP_VOL(ch))) {
1911 mutex_unlock(&codec->hash_mutex);
1912 parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
1913 parm |= direction == HDA_OUTPUT ?
1914 AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
1916 val = snd_hda_codec_read(codec, nid, 0,
1917 AC_VERB_GET_AMP_GAIN_MUTE, parm);
1920 mutex_lock(&codec->hash_mutex);
1923 info->vol[ch] = val;
1924 info->head.val |= INFO_AMP_VOL(ch);
1925 } else if (init_only)
1931 * write the current volume in info to the h/w
1933 static void put_vol_mute(struct hda_codec *codec, unsigned int amp_caps,
1934 hda_nid_t nid, int ch, int direction, int index,
1939 parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
1940 parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
1941 parm |= index << AC_AMP_SET_INDEX_SHIFT;
1942 if ((val & HDA_AMP_MUTE) && !(amp_caps & AC_AMPCAP_MUTE) &&
1943 (amp_caps & AC_AMPCAP_MIN_MUTE))
1944 ; /* set the zero value as a fake mute */
1947 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
1951 * snd_hda_codec_amp_read - Read AMP value
1952 * @codec: HD-audio codec
1953 * @nid: NID to read the AMP value
1954 * @ch: channel (left=0 or right=1)
1955 * @direction: #HDA_INPUT or #HDA_OUTPUT
1956 * @index: the index value (only for input direction)
1958 * Read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
1960 int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
1961 int direction, int index)
1963 struct hda_amp_info *info;
1964 unsigned int val = 0;
1966 mutex_lock(&codec->hash_mutex);
1967 info = update_amp_hash(codec, nid, ch, direction, index, false);
1969 val = info->vol[ch];
1970 mutex_unlock(&codec->hash_mutex);
1973 EXPORT_SYMBOL_HDA(snd_hda_codec_amp_read);
1975 static int codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
1976 int direction, int idx, int mask, int val,
1979 struct hda_amp_info *info;
1981 unsigned int cache_only;
1983 if (snd_BUG_ON(mask & ~0xff))
1987 mutex_lock(&codec->hash_mutex);
1988 info = update_amp_hash(codec, nid, ch, direction, idx, init_only);
1990 mutex_unlock(&codec->hash_mutex);
1993 val |= info->vol[ch] & ~mask;
1994 if (info->vol[ch] == val) {
1995 mutex_unlock(&codec->hash_mutex);
1998 info->vol[ch] = val;
1999 cache_only = info->head.dirty = codec->cached_write;
2000 caps = info->amp_caps;
2001 mutex_unlock(&codec->hash_mutex);
2003 put_vol_mute(codec, caps, nid, ch, direction, idx, val);
2008 * snd_hda_codec_amp_update - update the AMP value
2009 * @codec: HD-audio codec
2010 * @nid: NID to read the AMP value
2011 * @ch: channel (left=0 or right=1)
2012 * @direction: #HDA_INPUT or #HDA_OUTPUT
2013 * @idx: the index value (only for input direction)
2014 * @mask: bit mask to set
2015 * @val: the bits value to set
2017 * Update the AMP value with a bit mask.
2018 * Returns 0 if the value is unchanged, 1 if changed.
2020 int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
2021 int direction, int idx, int mask, int val)
2023 return codec_amp_update(codec, nid, ch, direction, idx, mask, val, false);
2025 EXPORT_SYMBOL_HDA(snd_hda_codec_amp_update);
2028 * snd_hda_codec_amp_stereo - update the AMP stereo values
2029 * @codec: HD-audio codec
2030 * @nid: NID to read the AMP value
2031 * @direction: #HDA_INPUT or #HDA_OUTPUT
2032 * @idx: the index value (only for input direction)
2033 * @mask: bit mask to set
2034 * @val: the bits value to set
2036 * Update the AMP values like snd_hda_codec_amp_update(), but for a
2037 * stereo widget with the same mask and value.
2039 int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
2040 int direction, int idx, int mask, int val)
2044 if (snd_BUG_ON(mask & ~0xff))
2046 for (ch = 0; ch < 2; ch++)
2047 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
2051 EXPORT_SYMBOL_HDA(snd_hda_codec_amp_stereo);
2053 /* Works like snd_hda_codec_amp_update() but it writes the value only at
2054 * the first access. If the amp was already initialized / updated beforehand,
2055 * this does nothing.
2057 int snd_hda_codec_amp_init(struct hda_codec *codec, hda_nid_t nid, int ch,
2058 int dir, int idx, int mask, int val)
2060 return codec_amp_update(codec, nid, ch, dir, idx, mask, val, true);
2062 EXPORT_SYMBOL_HDA(snd_hda_codec_amp_init);
2064 int snd_hda_codec_amp_init_stereo(struct hda_codec *codec, hda_nid_t nid,
2065 int dir, int idx, int mask, int val)
2069 if (snd_BUG_ON(mask & ~0xff))
2071 for (ch = 0; ch < 2; ch++)
2072 ret |= snd_hda_codec_amp_init(codec, nid, ch, dir,
2076 EXPORT_SYMBOL_HDA(snd_hda_codec_amp_init_stereo);
2079 * snd_hda_codec_resume_amp - Resume all AMP commands from the cache
2080 * @codec: HD-audio codec
2082 * Resume the all amp commands from the cache.
2084 void snd_hda_codec_resume_amp(struct hda_codec *codec)
2088 mutex_lock(&codec->hash_mutex);
2089 codec->cached_write = 0;
2090 for (i = 0; i < codec->amp_cache.buf.used; i++) {
2091 struct hda_amp_info *buffer;
2094 unsigned int idx, dir, ch;
2095 struct hda_amp_info info;
2097 buffer = snd_array_elem(&codec->amp_cache.buf, i);
2098 if (!buffer->head.dirty)
2100 buffer->head.dirty = 0;
2102 key = info.head.key;
2106 idx = (key >> 16) & 0xff;
2107 dir = (key >> 24) & 0xff;
2108 for (ch = 0; ch < 2; ch++) {
2109 if (!(info.head.val & INFO_AMP_VOL(ch)))
2111 mutex_unlock(&codec->hash_mutex);
2112 put_vol_mute(codec, info.amp_caps, nid, ch, dir, idx,
2114 mutex_lock(&codec->hash_mutex);
2117 mutex_unlock(&codec->hash_mutex);
2119 EXPORT_SYMBOL_HDA(snd_hda_codec_resume_amp);
2121 static u32 get_amp_max_value(struct hda_codec *codec, hda_nid_t nid, int dir,
2124 u32 caps = query_amp_caps(codec, nid, dir);
2126 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
2133 * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer
2135 * The control element is supposed to have the private_value field
2136 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2138 int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
2139 struct snd_ctl_elem_info *uinfo)
2141 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2142 u16 nid = get_amp_nid(kcontrol);
2143 u8 chs = get_amp_channels(kcontrol);
2144 int dir = get_amp_direction(kcontrol);
2145 unsigned int ofs = get_amp_offset(kcontrol);
2147 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2148 uinfo->count = chs == 3 ? 2 : 1;
2149 uinfo->value.integer.min = 0;
2150 uinfo->value.integer.max = get_amp_max_value(codec, nid, dir, ofs);
2151 if (!uinfo->value.integer.max) {
2152 printk(KERN_WARNING "hda_codec: "
2153 "num_steps = 0 for NID=0x%x (ctl = %s)\n", nid,
2159 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_info);
2162 static inline unsigned int
2163 read_amp_value(struct hda_codec *codec, hda_nid_t nid,
2164 int ch, int dir, int idx, unsigned int ofs)
2167 val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
2168 val &= HDA_AMP_VOLMASK;
2177 update_amp_value(struct hda_codec *codec, hda_nid_t nid,
2178 int ch, int dir, int idx, unsigned int ofs,
2181 unsigned int maxval;
2185 /* ofs = 0: raw max value */
2186 maxval = get_amp_max_value(codec, nid, dir, 0);
2189 return snd_hda_codec_amp_update(codec, nid, ch, dir, idx,
2190 HDA_AMP_VOLMASK, val);
2194 * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume
2196 * The control element is supposed to have the private_value field
2197 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2199 int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
2200 struct snd_ctl_elem_value *ucontrol)
2202 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2203 hda_nid_t nid = get_amp_nid(kcontrol);
2204 int chs = get_amp_channels(kcontrol);
2205 int dir = get_amp_direction(kcontrol);
2206 int idx = get_amp_index(kcontrol);
2207 unsigned int ofs = get_amp_offset(kcontrol);
2208 long *valp = ucontrol->value.integer.value;
2211 *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs);
2213 *valp = read_amp_value(codec, nid, 1, dir, idx, ofs);
2216 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_get);
2219 * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume
2221 * The control element is supposed to have the private_value field
2222 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2224 int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
2225 struct snd_ctl_elem_value *ucontrol)
2227 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2228 hda_nid_t nid = get_amp_nid(kcontrol);
2229 int chs = get_amp_channels(kcontrol);
2230 int dir = get_amp_direction(kcontrol);
2231 int idx = get_amp_index(kcontrol);
2232 unsigned int ofs = get_amp_offset(kcontrol);
2233 long *valp = ucontrol->value.integer.value;
2236 snd_hda_power_up(codec);
2238 change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp);
2242 change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp);
2243 snd_hda_power_down(codec);
2246 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_put);
2249 * snd_hda_mixer_amp_volume_put - TLV callback for a standard AMP mixer volume
2251 * The control element is supposed to have the private_value field
2252 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2254 int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2255 unsigned int size, unsigned int __user *_tlv)
2257 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2258 hda_nid_t nid = get_amp_nid(kcontrol);
2259 int dir = get_amp_direction(kcontrol);
2260 unsigned int ofs = get_amp_offset(kcontrol);
2261 bool min_mute = get_amp_min_mute(kcontrol);
2262 u32 caps, val1, val2;
2264 if (size < 4 * sizeof(unsigned int))
2266 caps = query_amp_caps(codec, nid, dir);
2267 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2268 val2 = (val2 + 1) * 25;
2269 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
2271 val1 = ((int)val1) * ((int)val2);
2272 if (min_mute || (caps & AC_AMPCAP_MIN_MUTE))
2273 val2 |= TLV_DB_SCALE_MUTE;
2274 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
2276 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
2278 if (put_user(val1, _tlv + 2))
2280 if (put_user(val2, _tlv + 3))
2284 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_tlv);
2287 * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control
2288 * @codec: HD-audio codec
2289 * @nid: NID of a reference widget
2290 * @dir: #HDA_INPUT or #HDA_OUTPUT
2291 * @tlv: TLV data to be stored, at least 4 elements
2293 * Set (static) TLV data for a virtual master volume using the AMP caps
2294 * obtained from the reference NID.
2295 * The volume range is recalculated as if the max volume is 0dB.
2297 void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
2303 caps = query_amp_caps(codec, nid, dir);
2304 nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
2305 step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2306 step = (step + 1) * 25;
2307 tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
2308 tlv[1] = 2 * sizeof(unsigned int);
2309 tlv[2] = -nums * step;
2312 EXPORT_SYMBOL_HDA(snd_hda_set_vmaster_tlv);
2314 /* find a mixer control element with the given name */
2315 static struct snd_kcontrol *
2316 find_mixer_ctl(struct hda_codec *codec, const char *name, int dev, int idx)
2318 struct snd_ctl_elem_id id;
2319 memset(&id, 0, sizeof(id));
2320 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
2323 if (snd_BUG_ON(strlen(name) >= sizeof(id.name)))
2325 strcpy(id.name, name);
2326 return snd_ctl_find_id(codec->bus->card, &id);
2330 * snd_hda_find_mixer_ctl - Find a mixer control element with the given name
2331 * @codec: HD-audio codec
2332 * @name: ctl id name string
2334 * Get the control element with the given id string and IFACE_MIXER.
2336 struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
2339 return find_mixer_ctl(codec, name, 0, 0);
2341 EXPORT_SYMBOL_HDA(snd_hda_find_mixer_ctl);
2343 static int find_empty_mixer_ctl_idx(struct hda_codec *codec, const char *name,
2347 /* 16 ctlrs should be large enough */
2348 for (i = 0, idx = start_idx; i < 16; i++, idx++) {
2349 if (!find_mixer_ctl(codec, name, 0, idx))
2356 * snd_hda_ctl_add - Add a control element and assign to the codec
2357 * @codec: HD-audio codec
2358 * @nid: corresponding NID (optional)
2359 * @kctl: the control element to assign
2361 * Add the given control element to an array inside the codec instance.
2362 * All control elements belonging to a codec are supposed to be added
2363 * by this function so that a proper clean-up works at the free or
2364 * reconfiguration time.
2366 * If non-zero @nid is passed, the NID is assigned to the control element.
2367 * The assignment is shown in the codec proc file.
2369 * snd_hda_ctl_add() checks the control subdev id field whether
2370 * #HDA_SUBDEV_NID_FLAG bit is set. If set (and @nid is zero), the lower
2371 * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit
2372 * specifies if kctl->private_value is a HDA amplifier value.
2374 int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid,
2375 struct snd_kcontrol *kctl)
2378 unsigned short flags = 0;
2379 struct hda_nid_item *item;
2381 if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) {
2382 flags |= HDA_NID_ITEM_AMP;
2384 nid = get_amp_nid_(kctl->private_value);
2386 if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0)
2387 nid = kctl->id.subdevice & 0xffff;
2388 if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG))
2389 kctl->id.subdevice = 0;
2390 err = snd_ctl_add(codec->bus->card, kctl);
2393 item = snd_array_new(&codec->mixers);
2398 item->flags = flags;
2401 EXPORT_SYMBOL_HDA(snd_hda_ctl_add);
2404 * snd_hda_add_nid - Assign a NID to a control element
2405 * @codec: HD-audio codec
2406 * @nid: corresponding NID (optional)
2407 * @kctl: the control element to assign
2408 * @index: index to kctl
2410 * Add the given control element to an array inside the codec instance.
2411 * This function is used when #snd_hda_ctl_add cannot be used for 1:1
2412 * NID:KCTL mapping - for example "Capture Source" selector.
2414 int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl,
2415 unsigned int index, hda_nid_t nid)
2417 struct hda_nid_item *item;
2420 item = snd_array_new(&codec->nids);
2424 item->index = index;
2428 printk(KERN_ERR "hda-codec: no NID for mapping control %s:%d:%d\n",
2429 kctl->id.name, kctl->id.index, index);
2432 EXPORT_SYMBOL_HDA(snd_hda_add_nid);
2435 * snd_hda_ctls_clear - Clear all controls assigned to the given codec
2436 * @codec: HD-audio codec
2438 void snd_hda_ctls_clear(struct hda_codec *codec)
2441 struct hda_nid_item *items = codec->mixers.list;
2442 for (i = 0; i < codec->mixers.used; i++)
2443 snd_ctl_remove(codec->bus->card, items[i].kctl);
2444 snd_array_free(&codec->mixers);
2445 snd_array_free(&codec->nids);
2448 /* pseudo device locking
2449 * toggle card->shutdown to allow/disallow the device access (as a hack)
2451 int snd_hda_lock_devices(struct hda_bus *bus)
2453 struct snd_card *card = bus->card;
2454 struct hda_codec *codec;
2456 spin_lock(&card->files_lock);
2460 if (!list_empty(&card->ctl_files))
2463 list_for_each_entry(codec, &bus->codec_list, list) {
2465 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
2466 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
2469 if (cpcm->pcm->streams[0].substream_opened ||
2470 cpcm->pcm->streams[1].substream_opened)
2474 spin_unlock(&card->files_lock);
2480 spin_unlock(&card->files_lock);
2483 EXPORT_SYMBOL_HDA(snd_hda_lock_devices);
2485 void snd_hda_unlock_devices(struct hda_bus *bus)
2487 struct snd_card *card = bus->card;
2490 spin_lock(&card->files_lock);
2492 spin_unlock(&card->files_lock);
2494 EXPORT_SYMBOL_HDA(snd_hda_unlock_devices);
2497 * snd_hda_codec_reset - Clear all objects assigned to the codec
2498 * @codec: HD-audio codec
2500 * This frees the all PCM and control elements assigned to the codec, and
2501 * clears the caches and restores the pin default configurations.
2503 * When a device is being used, it returns -EBSY. If successfully freed,
2506 int snd_hda_codec_reset(struct hda_codec *codec)
2508 struct hda_bus *bus = codec->bus;
2509 struct snd_card *card = bus->card;
2512 if (snd_hda_lock_devices(bus) < 0)
2515 /* OK, let it free */
2516 cancel_delayed_work_sync(&codec->jackpoll_work);
2518 cancel_delayed_work_sync(&codec->power_work);
2519 codec->power_on = 0;
2520 codec->power_transition = 0;
2521 codec->power_jiffies = jiffies;
2522 flush_workqueue(bus->workq);
2524 snd_hda_ctls_clear(codec);
2526 for (i = 0; i < codec->num_pcms; i++) {
2527 if (codec->pcm_info[i].pcm) {
2528 snd_device_free(card, codec->pcm_info[i].pcm);
2529 clear_bit(codec->pcm_info[i].device,
2533 if (codec->patch_ops.free)
2534 codec->patch_ops.free(codec);
2535 memset(&codec->patch_ops, 0, sizeof(codec->patch_ops));
2536 snd_hda_jack_tbl_clear(codec);
2537 codec->proc_widget_hook = NULL;
2539 free_hda_cache(&codec->amp_cache);
2540 free_hda_cache(&codec->cmd_cache);
2541 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
2542 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
2543 /* free only driver_pins so that init_pins + user_pins are restored */
2544 snd_array_free(&codec->driver_pins);
2545 snd_array_free(&codec->cvt_setups);
2546 snd_array_free(&codec->spdif_out);
2547 snd_array_free(&codec->verbs);
2548 codec->num_pcms = 0;
2549 codec->pcm_info = NULL;
2550 codec->preset = NULL;
2551 codec->slave_dig_outs = NULL;
2552 codec->spdif_status_reset = 0;
2553 module_put(codec->owner);
2554 codec->owner = NULL;
2556 /* allow device access again */
2557 snd_hda_unlock_devices(bus);
2561 typedef int (*map_slave_func_t)(void *, struct snd_kcontrol *);
2563 /* apply the function to all matching slave ctls in the mixer list */
2564 static int map_slaves(struct hda_codec *codec, const char * const *slaves,
2565 const char *suffix, map_slave_func_t func, void *data)
2567 struct hda_nid_item *items;
2568 const char * const *s;
2571 items = codec->mixers.list;
2572 for (i = 0; i < codec->mixers.used; i++) {
2573 struct snd_kcontrol *sctl = items[i].kctl;
2574 if (!sctl || !sctl->id.name ||
2575 sctl->id.iface != SNDRV_CTL_ELEM_IFACE_MIXER)
2577 for (s = slaves; *s; s++) {
2578 char tmpname[sizeof(sctl->id.name)];
2579 const char *name = *s;
2581 snprintf(tmpname, sizeof(tmpname), "%s %s",
2585 if (!strcmp(sctl->id.name, name)) {
2586 err = func(data, sctl);
2596 static int check_slave_present(void *data, struct snd_kcontrol *sctl)
2601 /* guess the value corresponding to 0dB */
2602 static int get_kctl_0dB_offset(struct snd_kcontrol *kctl)
2605 const int *tlv = NULL;
2608 if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
2609 /* FIXME: set_fs() hack for obtaining user-space TLV data */
2610 mm_segment_t fs = get_fs();
2612 if (!kctl->tlv.c(kctl, 0, sizeof(_tlv), _tlv))
2615 } else if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_READ)
2617 if (tlv && tlv[0] == SNDRV_CTL_TLVT_DB_SCALE)
2618 val = -tlv[2] / tlv[3];
2622 /* call kctl->put with the given value(s) */
2623 static int put_kctl_with_value(struct snd_kcontrol *kctl, int val)
2625 struct snd_ctl_elem_value *ucontrol;
2626 ucontrol = kzalloc(sizeof(*ucontrol), GFP_KERNEL);
2629 ucontrol->value.integer.value[0] = val;
2630 ucontrol->value.integer.value[1] = val;
2631 kctl->put(kctl, ucontrol);
2636 /* initialize the slave volume with 0dB */
2637 static int init_slave_0dB(void *data, struct snd_kcontrol *slave)
2639 int offset = get_kctl_0dB_offset(slave);
2641 put_kctl_with_value(slave, offset);
2645 /* unmute the slave */
2646 static int init_slave_unmute(void *data, struct snd_kcontrol *slave)
2648 return put_kctl_with_value(slave, 1);
2652 * snd_hda_add_vmaster - create a virtual master control and add slaves
2653 * @codec: HD-audio codec
2654 * @name: vmaster control name
2655 * @tlv: TLV data (optional)
2656 * @slaves: slave control names (optional)
2657 * @suffix: suffix string to each slave name (optional)
2658 * @init_slave_vol: initialize slaves to unmute/0dB
2659 * @ctl_ret: store the vmaster kcontrol in return
2661 * Create a virtual master control with the given name. The TLV data
2662 * must be either NULL or a valid data.
2664 * @slaves is a NULL-terminated array of strings, each of which is a
2665 * slave control name. All controls with these names are assigned to
2666 * the new virtual master control.
2668 * This function returns zero if successful or a negative error code.
2670 int __snd_hda_add_vmaster(struct hda_codec *codec, char *name,
2671 unsigned int *tlv, const char * const *slaves,
2672 const char *suffix, bool init_slave_vol,
2673 struct snd_kcontrol **ctl_ret)
2675 struct snd_kcontrol *kctl;
2681 err = map_slaves(codec, slaves, suffix, check_slave_present, NULL);
2683 snd_printdd("No slave found for %s\n", name);
2686 kctl = snd_ctl_make_virtual_master(name, tlv);
2689 err = snd_hda_ctl_add(codec, 0, kctl);
2693 err = map_slaves(codec, slaves, suffix,
2694 (map_slave_func_t)snd_ctl_add_slave, kctl);
2698 /* init with master mute & zero volume */
2699 put_kctl_with_value(kctl, 0);
2701 map_slaves(codec, slaves, suffix,
2702 tlv ? init_slave_0dB : init_slave_unmute, kctl);
2708 EXPORT_SYMBOL_HDA(__snd_hda_add_vmaster);
2711 * mute-LED control using vmaster
2713 static int vmaster_mute_mode_info(struct snd_kcontrol *kcontrol,
2714 struct snd_ctl_elem_info *uinfo)
2716 static const char * const texts[] = {
2717 "On", "Off", "Follow Master"
2721 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2723 uinfo->value.enumerated.items = 3;
2724 index = uinfo->value.enumerated.item;
2727 strcpy(uinfo->value.enumerated.name, texts[index]);
2731 static int vmaster_mute_mode_get(struct snd_kcontrol *kcontrol,
2732 struct snd_ctl_elem_value *ucontrol)
2734 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2735 ucontrol->value.enumerated.item[0] = hook->mute_mode;
2739 static int vmaster_mute_mode_put(struct snd_kcontrol *kcontrol,
2740 struct snd_ctl_elem_value *ucontrol)
2742 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2743 unsigned int old_mode = hook->mute_mode;
2745 hook->mute_mode = ucontrol->value.enumerated.item[0];
2746 if (hook->mute_mode > HDA_VMUTE_FOLLOW_MASTER)
2747 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
2748 if (old_mode == hook->mute_mode)
2750 snd_hda_sync_vmaster_hook(hook);
2754 static struct snd_kcontrol_new vmaster_mute_mode = {
2755 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2756 .name = "Mute-LED Mode",
2757 .info = vmaster_mute_mode_info,
2758 .get = vmaster_mute_mode_get,
2759 .put = vmaster_mute_mode_put,
2763 * Add a mute-LED hook with the given vmaster switch kctl
2764 * "Mute-LED Mode" control is automatically created and associated with
2767 int snd_hda_add_vmaster_hook(struct hda_codec *codec,
2768 struct hda_vmaster_mute_hook *hook,
2769 bool expose_enum_ctl)
2771 struct snd_kcontrol *kctl;
2773 if (!hook->hook || !hook->sw_kctl)
2775 snd_ctl_add_vmaster_hook(hook->sw_kctl, hook->hook, codec);
2776 hook->codec = codec;
2777 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
2778 if (!expose_enum_ctl)
2780 kctl = snd_ctl_new1(&vmaster_mute_mode, hook);
2783 return snd_hda_ctl_add(codec, 0, kctl);
2785 EXPORT_SYMBOL_HDA(snd_hda_add_vmaster_hook);
2788 * Call the hook with the current value for synchronization
2789 * Should be called in init callback
2791 void snd_hda_sync_vmaster_hook(struct hda_vmaster_mute_hook *hook)
2793 if (!hook->hook || !hook->codec)
2795 /* don't call vmaster hook in the destructor since it might have
2796 * been already destroyed
2798 if (hook->codec->bus->shutdown)
2800 switch (hook->mute_mode) {
2801 case HDA_VMUTE_FOLLOW_MASTER:
2802 snd_ctl_sync_vmaster_hook(hook->sw_kctl);
2805 hook->hook(hook->codec, hook->mute_mode);
2809 EXPORT_SYMBOL_HDA(snd_hda_sync_vmaster_hook);
2813 * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch
2815 * The control element is supposed to have the private_value field
2816 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2818 int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
2819 struct snd_ctl_elem_info *uinfo)
2821 int chs = get_amp_channels(kcontrol);
2823 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2824 uinfo->count = chs == 3 ? 2 : 1;
2825 uinfo->value.integer.min = 0;
2826 uinfo->value.integer.max = 1;
2829 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_info);
2832 * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch
2834 * The control element is supposed to have the private_value field
2835 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2837 int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
2838 struct snd_ctl_elem_value *ucontrol)
2840 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2841 hda_nid_t nid = get_amp_nid(kcontrol);
2842 int chs = get_amp_channels(kcontrol);
2843 int dir = get_amp_direction(kcontrol);
2844 int idx = get_amp_index(kcontrol);
2845 long *valp = ucontrol->value.integer.value;
2848 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
2849 HDA_AMP_MUTE) ? 0 : 1;
2851 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
2852 HDA_AMP_MUTE) ? 0 : 1;
2855 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_get);
2858 * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch
2860 * The control element is supposed to have the private_value field
2861 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2863 int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
2864 struct snd_ctl_elem_value *ucontrol)
2866 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2867 hda_nid_t nid = get_amp_nid(kcontrol);
2868 int chs = get_amp_channels(kcontrol);
2869 int dir = get_amp_direction(kcontrol);
2870 int idx = get_amp_index(kcontrol);
2871 long *valp = ucontrol->value.integer.value;
2874 snd_hda_power_up(codec);
2876 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
2878 *valp ? 0 : HDA_AMP_MUTE);
2882 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
2884 *valp ? 0 : HDA_AMP_MUTE);
2885 hda_call_check_power_status(codec, nid);
2886 snd_hda_power_down(codec);
2889 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put);
2892 * bound volume controls
2894 * bind multiple volumes (# indices, from 0)
2897 #define AMP_VAL_IDX_SHIFT 19
2898 #define AMP_VAL_IDX_MASK (0x0f<<19)
2901 * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control
2903 * The control element is supposed to have the private_value field
2904 * set up via HDA_BIND_MUTE*() macros.
2906 int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
2907 struct snd_ctl_elem_value *ucontrol)
2909 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2913 mutex_lock(&codec->control_mutex);
2914 pval = kcontrol->private_value;
2915 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
2916 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
2917 kcontrol->private_value = pval;
2918 mutex_unlock(&codec->control_mutex);
2921 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_get);
2924 * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control
2926 * The control element is supposed to have the private_value field
2927 * set up via HDA_BIND_MUTE*() macros.
2929 int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
2930 struct snd_ctl_elem_value *ucontrol)
2932 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2934 int i, indices, err = 0, change = 0;
2936 mutex_lock(&codec->control_mutex);
2937 pval = kcontrol->private_value;
2938 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
2939 for (i = 0; i < indices; i++) {
2940 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
2941 (i << AMP_VAL_IDX_SHIFT);
2942 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2947 kcontrol->private_value = pval;
2948 mutex_unlock(&codec->control_mutex);
2949 return err < 0 ? err : change;
2951 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_put);
2954 * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control
2956 * The control element is supposed to have the private_value field
2957 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2959 int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
2960 struct snd_ctl_elem_info *uinfo)
2962 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2963 struct hda_bind_ctls *c;
2966 mutex_lock(&codec->control_mutex);
2967 c = (struct hda_bind_ctls *)kcontrol->private_value;
2968 kcontrol->private_value = *c->values;
2969 err = c->ops->info(kcontrol, uinfo);
2970 kcontrol->private_value = (long)c;
2971 mutex_unlock(&codec->control_mutex);
2974 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_info);
2977 * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control
2979 * The control element is supposed to have the private_value field
2980 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2982 int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
2983 struct snd_ctl_elem_value *ucontrol)
2985 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2986 struct hda_bind_ctls *c;
2989 mutex_lock(&codec->control_mutex);
2990 c = (struct hda_bind_ctls *)kcontrol->private_value;
2991 kcontrol->private_value = *c->values;
2992 err = c->ops->get(kcontrol, ucontrol);
2993 kcontrol->private_value = (long)c;
2994 mutex_unlock(&codec->control_mutex);
2997 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_get);
3000 * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control
3002 * The control element is supposed to have the private_value field
3003 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
3005 int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
3006 struct snd_ctl_elem_value *ucontrol)
3008 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3009 struct hda_bind_ctls *c;
3010 unsigned long *vals;
3011 int err = 0, change = 0;
3013 mutex_lock(&codec->control_mutex);
3014 c = (struct hda_bind_ctls *)kcontrol->private_value;
3015 for (vals = c->values; *vals; vals++) {
3016 kcontrol->private_value = *vals;
3017 err = c->ops->put(kcontrol, ucontrol);
3022 kcontrol->private_value = (long)c;
3023 mutex_unlock(&codec->control_mutex);
3024 return err < 0 ? err : change;
3026 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_put);
3029 * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control
3031 * The control element is supposed to have the private_value field
3032 * set up via HDA_BIND_VOL() macro.
3034 int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
3035 unsigned int size, unsigned int __user *tlv)
3037 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3038 struct hda_bind_ctls *c;
3041 mutex_lock(&codec->control_mutex);
3042 c = (struct hda_bind_ctls *)kcontrol->private_value;
3043 kcontrol->private_value = *c->values;
3044 err = c->ops->tlv(kcontrol, op_flag, size, tlv);
3045 kcontrol->private_value = (long)c;
3046 mutex_unlock(&codec->control_mutex);
3049 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_tlv);
3051 struct hda_ctl_ops snd_hda_bind_vol = {
3052 .info = snd_hda_mixer_amp_volume_info,
3053 .get = snd_hda_mixer_amp_volume_get,
3054 .put = snd_hda_mixer_amp_volume_put,
3055 .tlv = snd_hda_mixer_amp_tlv
3057 EXPORT_SYMBOL_HDA(snd_hda_bind_vol);
3059 struct hda_ctl_ops snd_hda_bind_sw = {
3060 .info = snd_hda_mixer_amp_switch_info,
3061 .get = snd_hda_mixer_amp_switch_get,
3062 .put = snd_hda_mixer_amp_switch_put,
3063 .tlv = snd_hda_mixer_amp_tlv
3065 EXPORT_SYMBOL_HDA(snd_hda_bind_sw);
3068 * SPDIF out controls
3071 static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
3072 struct snd_ctl_elem_info *uinfo)
3074 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
3079 static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
3080 struct snd_ctl_elem_value *ucontrol)
3082 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
3083 IEC958_AES0_NONAUDIO |
3084 IEC958_AES0_CON_EMPHASIS_5015 |
3085 IEC958_AES0_CON_NOT_COPYRIGHT;
3086 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
3087 IEC958_AES1_CON_ORIGINAL;
3091 static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
3092 struct snd_ctl_elem_value *ucontrol)
3094 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
3095 IEC958_AES0_NONAUDIO |
3096 IEC958_AES0_PRO_EMPHASIS_5015;
3100 static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
3101 struct snd_ctl_elem_value *ucontrol)
3103 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3104 int idx = kcontrol->private_value;
3105 struct hda_spdif_out *spdif;
3107 mutex_lock(&codec->spdif_mutex);
3108 spdif = snd_array_elem(&codec->spdif_out, idx);
3109 ucontrol->value.iec958.status[0] = spdif->status & 0xff;
3110 ucontrol->value.iec958.status[1] = (spdif->status >> 8) & 0xff;
3111 ucontrol->value.iec958.status[2] = (spdif->status >> 16) & 0xff;
3112 ucontrol->value.iec958.status[3] = (spdif->status >> 24) & 0xff;
3113 mutex_unlock(&codec->spdif_mutex);
3118 /* convert from SPDIF status bits to HDA SPDIF bits
3119 * bit 0 (DigEn) is always set zero (to be filled later)
3121 static unsigned short convert_from_spdif_status(unsigned int sbits)
3123 unsigned short val = 0;
3125 if (sbits & IEC958_AES0_PROFESSIONAL)
3126 val |= AC_DIG1_PROFESSIONAL;
3127 if (sbits & IEC958_AES0_NONAUDIO)
3128 val |= AC_DIG1_NONAUDIO;
3129 if (sbits & IEC958_AES0_PROFESSIONAL) {
3130 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
3131 IEC958_AES0_PRO_EMPHASIS_5015)
3132 val |= AC_DIG1_EMPHASIS;
3134 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
3135 IEC958_AES0_CON_EMPHASIS_5015)
3136 val |= AC_DIG1_EMPHASIS;
3137 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
3138 val |= AC_DIG1_COPYRIGHT;
3139 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
3140 val |= AC_DIG1_LEVEL;
3141 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
3146 /* convert to SPDIF status bits from HDA SPDIF bits
3148 static unsigned int convert_to_spdif_status(unsigned short val)
3150 unsigned int sbits = 0;
3152 if (val & AC_DIG1_NONAUDIO)
3153 sbits |= IEC958_AES0_NONAUDIO;
3154 if (val & AC_DIG1_PROFESSIONAL)
3155 sbits |= IEC958_AES0_PROFESSIONAL;
3156 if (sbits & IEC958_AES0_PROFESSIONAL) {
3157 if (val & AC_DIG1_EMPHASIS)
3158 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
3160 if (val & AC_DIG1_EMPHASIS)
3161 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
3162 if (!(val & AC_DIG1_COPYRIGHT))
3163 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
3164 if (val & AC_DIG1_LEVEL)
3165 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
3166 sbits |= val & (0x7f << 8);
3171 /* set digital convert verbs both for the given NID and its slaves */
3172 static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
3177 snd_hda_codec_write_cache(codec, nid, 0, verb, val);
3178 d = codec->slave_dig_outs;
3182 snd_hda_codec_write_cache(codec, *d, 0, verb, val);
3185 static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
3189 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1);
3191 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2);
3194 static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
3195 struct snd_ctl_elem_value *ucontrol)
3197 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3198 int idx = kcontrol->private_value;
3199 struct hda_spdif_out *spdif;
3204 mutex_lock(&codec->spdif_mutex);
3205 spdif = snd_array_elem(&codec->spdif_out, idx);
3207 spdif->status = ucontrol->value.iec958.status[0] |
3208 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
3209 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
3210 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
3211 val = convert_from_spdif_status(spdif->status);
3212 val |= spdif->ctls & 1;
3213 change = spdif->ctls != val;
3215 if (change && nid != (u16)-1)
3216 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
3217 mutex_unlock(&codec->spdif_mutex);
3221 #define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
3223 static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
3224 struct snd_ctl_elem_value *ucontrol)
3226 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3227 int idx = kcontrol->private_value;
3228 struct hda_spdif_out *spdif;
3230 mutex_lock(&codec->spdif_mutex);
3231 spdif = snd_array_elem(&codec->spdif_out, idx);
3232 ucontrol->value.integer.value[0] = spdif->ctls & AC_DIG1_ENABLE;
3233 mutex_unlock(&codec->spdif_mutex);
3237 static inline void set_spdif_ctls(struct hda_codec *codec, hda_nid_t nid,
3240 set_dig_out_convert(codec, nid, dig1, dig2);
3241 /* unmute amp switch (if any) */
3242 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
3243 (dig1 & AC_DIG1_ENABLE))
3244 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
3248 static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
3249 struct snd_ctl_elem_value *ucontrol)
3251 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3252 int idx = kcontrol->private_value;
3253 struct hda_spdif_out *spdif;
3258 mutex_lock(&codec->spdif_mutex);
3259 spdif = snd_array_elem(&codec->spdif_out, idx);
3261 val = spdif->ctls & ~AC_DIG1_ENABLE;
3262 if (ucontrol->value.integer.value[0])
3263 val |= AC_DIG1_ENABLE;
3264 change = spdif->ctls != val;
3266 if (change && nid != (u16)-1)
3267 set_spdif_ctls(codec, nid, val & 0xff, -1);
3268 mutex_unlock(&codec->spdif_mutex);
3272 static struct snd_kcontrol_new dig_mixes[] = {
3274 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3275 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3276 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
3277 .info = snd_hda_spdif_mask_info,
3278 .get = snd_hda_spdif_cmask_get,
3281 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3282 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3283 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK),
3284 .info = snd_hda_spdif_mask_info,
3285 .get = snd_hda_spdif_pmask_get,
3288 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3289 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
3290 .info = snd_hda_spdif_mask_info,
3291 .get = snd_hda_spdif_default_get,
3292 .put = snd_hda_spdif_default_put,
3295 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3296 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
3297 .info = snd_hda_spdif_out_switch_info,
3298 .get = snd_hda_spdif_out_switch_get,
3299 .put = snd_hda_spdif_out_switch_put,
3305 * snd_hda_create_dig_out_ctls - create Output SPDIF-related controls
3306 * @codec: the HDA codec
3307 * @associated_nid: NID that new ctls associated with
3308 * @cvt_nid: converter NID
3309 * @type: HDA_PCM_TYPE_*
3310 * Creates controls related with the digital output.
3311 * Called from each patch supporting the digital out.
3313 * Returns 0 if successful, or a negative error code.
3315 int snd_hda_create_dig_out_ctls(struct hda_codec *codec,
3316 hda_nid_t associated_nid,
3321 struct snd_kcontrol *kctl;
3322 struct snd_kcontrol_new *dig_mix;
3324 const int spdif_index = 16;
3325 struct hda_spdif_out *spdif;
3326 struct hda_bus *bus = codec->bus;
3328 if (bus->primary_dig_out_type == HDA_PCM_TYPE_HDMI &&
3329 type == HDA_PCM_TYPE_SPDIF) {
3331 } else if (bus->primary_dig_out_type == HDA_PCM_TYPE_SPDIF &&
3332 type == HDA_PCM_TYPE_HDMI) {
3333 /* suppose a single SPDIF device */
3334 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
3335 kctl = find_mixer_ctl(codec, dig_mix->name, 0, 0);
3338 kctl->id.index = spdif_index;
3340 bus->primary_dig_out_type = HDA_PCM_TYPE_HDMI;
3342 if (!bus->primary_dig_out_type)
3343 bus->primary_dig_out_type = type;
3345 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Playback Switch", idx);
3347 printk(KERN_ERR "hda_codec: too many IEC958 outputs\n");
3350 spdif = snd_array_new(&codec->spdif_out);
3353 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
3354 kctl = snd_ctl_new1(dig_mix, codec);
3357 kctl->id.index = idx;
3358 kctl->private_value = codec->spdif_out.used - 1;
3359 err = snd_hda_ctl_add(codec, associated_nid, kctl);
3363 spdif->nid = cvt_nid;
3364 spdif->ctls = snd_hda_codec_read(codec, cvt_nid, 0,
3365 AC_VERB_GET_DIGI_CONVERT_1, 0);
3366 spdif->status = convert_to_spdif_status(spdif->ctls);
3369 EXPORT_SYMBOL_HDA(snd_hda_create_dig_out_ctls);
3371 /* get the hda_spdif_out entry from the given NID
3372 * call within spdif_mutex lock
3374 struct hda_spdif_out *snd_hda_spdif_out_of_nid(struct hda_codec *codec,
3378 for (i = 0; i < codec->spdif_out.used; i++) {
3379 struct hda_spdif_out *spdif =
3380 snd_array_elem(&codec->spdif_out, i);
3381 if (spdif->nid == nid)
3386 EXPORT_SYMBOL_HDA(snd_hda_spdif_out_of_nid);
3388 void snd_hda_spdif_ctls_unassign(struct hda_codec *codec, int idx)
3390 struct hda_spdif_out *spdif;
3392 mutex_lock(&codec->spdif_mutex);
3393 spdif = snd_array_elem(&codec->spdif_out, idx);
3394 spdif->nid = (u16)-1;
3395 mutex_unlock(&codec->spdif_mutex);
3397 EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_unassign);
3399 void snd_hda_spdif_ctls_assign(struct hda_codec *codec, int idx, hda_nid_t nid)
3401 struct hda_spdif_out *spdif;
3404 mutex_lock(&codec->spdif_mutex);
3405 spdif = snd_array_elem(&codec->spdif_out, idx);
3406 if (spdif->nid != nid) {
3409 set_spdif_ctls(codec, nid, val & 0xff, (val >> 8) & 0xff);
3411 mutex_unlock(&codec->spdif_mutex);
3413 EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_assign);
3416 * SPDIF sharing with analog output
3418 static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
3419 struct snd_ctl_elem_value *ucontrol)
3421 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3422 ucontrol->value.integer.value[0] = mout->share_spdif;
3426 static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
3427 struct snd_ctl_elem_value *ucontrol)
3429 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3430 mout->share_spdif = !!ucontrol->value.integer.value[0];
3434 static struct snd_kcontrol_new spdif_share_sw = {
3435 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3436 .name = "IEC958 Default PCM Playback Switch",
3437 .info = snd_ctl_boolean_mono_info,
3438 .get = spdif_share_sw_get,
3439 .put = spdif_share_sw_put,
3443 * snd_hda_create_spdif_share_sw - create Default PCM switch
3444 * @codec: the HDA codec
3445 * @mout: multi-out instance
3447 int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
3448 struct hda_multi_out *mout)
3450 struct snd_kcontrol *kctl;
3452 if (!mout->dig_out_nid)
3455 kctl = snd_ctl_new1(&spdif_share_sw, mout);
3458 /* ATTENTION: here mout is passed as private_data, instead of codec */
3459 return snd_hda_ctl_add(codec, mout->dig_out_nid, kctl);
3461 EXPORT_SYMBOL_HDA(snd_hda_create_spdif_share_sw);
3467 #define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
3469 static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
3470 struct snd_ctl_elem_value *ucontrol)
3472 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3474 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
3478 static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
3479 struct snd_ctl_elem_value *ucontrol)
3481 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3482 hda_nid_t nid = kcontrol->private_value;
3483 unsigned int val = !!ucontrol->value.integer.value[0];
3486 mutex_lock(&codec->spdif_mutex);
3487 change = codec->spdif_in_enable != val;
3489 codec->spdif_in_enable = val;
3490 snd_hda_codec_write_cache(codec, nid, 0,
3491 AC_VERB_SET_DIGI_CONVERT_1, val);
3493 mutex_unlock(&codec->spdif_mutex);
3497 static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
3498 struct snd_ctl_elem_value *ucontrol)
3500 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3501 hda_nid_t nid = kcontrol->private_value;
3505 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0);
3506 sbits = convert_to_spdif_status(val);
3507 ucontrol->value.iec958.status[0] = sbits;
3508 ucontrol->value.iec958.status[1] = sbits >> 8;
3509 ucontrol->value.iec958.status[2] = sbits >> 16;
3510 ucontrol->value.iec958.status[3] = sbits >> 24;
3514 static struct snd_kcontrol_new dig_in_ctls[] = {
3516 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3517 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH),
3518 .info = snd_hda_spdif_in_switch_info,
3519 .get = snd_hda_spdif_in_switch_get,
3520 .put = snd_hda_spdif_in_switch_put,
3523 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3524 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3525 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
3526 .info = snd_hda_spdif_mask_info,
3527 .get = snd_hda_spdif_in_status_get,
3533 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
3534 * @codec: the HDA codec
3535 * @nid: audio in widget NID
3537 * Creates controls related with the SPDIF input.
3538 * Called from each patch supporting the SPDIF in.
3540 * Returns 0 if successful, or a negative error code.
3542 int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
3545 struct snd_kcontrol *kctl;
3546 struct snd_kcontrol_new *dig_mix;
3549 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Capture Switch", 0);
3551 printk(KERN_ERR "hda_codec: too many IEC958 inputs\n");
3554 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
3555 kctl = snd_ctl_new1(dig_mix, codec);
3558 kctl->private_value = nid;
3559 err = snd_hda_ctl_add(codec, nid, kctl);
3563 codec->spdif_in_enable =
3564 snd_hda_codec_read(codec, nid, 0,
3565 AC_VERB_GET_DIGI_CONVERT_1, 0) &
3569 EXPORT_SYMBOL_HDA(snd_hda_create_spdif_in_ctls);
3575 /* build a 31bit cache key with the widget id and the command parameter */
3576 #define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
3577 #define get_cmd_cache_nid(key) ((key) & 0xff)
3578 #define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
3581 * snd_hda_codec_write_cache - send a single command with caching
3582 * @codec: the HDA codec
3583 * @nid: NID to send the command
3584 * @flags: optional bit flags
3585 * @verb: the verb to send
3586 * @parm: the parameter for the verb
3588 * Send a single command without waiting for response.
3590 * Returns 0 if successful, or a negative error code.
3592 int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
3593 int flags, unsigned int verb, unsigned int parm)
3596 struct hda_cache_head *c;
3598 unsigned int cache_only;
3600 cache_only = codec->cached_write;
3602 err = snd_hda_codec_write(codec, nid, flags, verb, parm);
3607 /* parm may contain the verb stuff for get/set amp */
3608 verb = verb | (parm >> 8);
3610 key = build_cmd_cache_key(nid, verb);
3611 mutex_lock(&codec->bus->cmd_mutex);
3612 c = get_alloc_hash(&codec->cmd_cache, key);
3615 c->dirty = cache_only;
3617 mutex_unlock(&codec->bus->cmd_mutex);
3620 EXPORT_SYMBOL_HDA(snd_hda_codec_write_cache);
3623 * snd_hda_codec_update_cache - check cache and write the cmd only when needed
3624 * @codec: the HDA codec
3625 * @nid: NID to send the command
3626 * @flags: optional bit flags
3627 * @verb: the verb to send
3628 * @parm: the parameter for the verb
3630 * This function works like snd_hda_codec_write_cache(), but it doesn't send
3631 * command if the parameter is already identical with the cached value.
3632 * If not, it sends the command and refreshes the cache.
3634 * Returns 0 if successful, or a negative error code.
3636 int snd_hda_codec_update_cache(struct hda_codec *codec, hda_nid_t nid,
3637 int flags, unsigned int verb, unsigned int parm)
3639 struct hda_cache_head *c;
3642 /* parm may contain the verb stuff for get/set amp */
3643 verb = verb | (parm >> 8);
3645 key = build_cmd_cache_key(nid, verb);
3646 mutex_lock(&codec->bus->cmd_mutex);
3647 c = get_hash(&codec->cmd_cache, key);
3648 if (c && c->val == parm) {
3649 mutex_unlock(&codec->bus->cmd_mutex);
3652 mutex_unlock(&codec->bus->cmd_mutex);
3653 return snd_hda_codec_write_cache(codec, nid, flags, verb, parm);
3655 EXPORT_SYMBOL_HDA(snd_hda_codec_update_cache);
3658 * snd_hda_codec_resume_cache - Resume the all commands from the cache
3659 * @codec: HD-audio codec
3661 * Execute all verbs recorded in the command caches to resume.
3663 void snd_hda_codec_resume_cache(struct hda_codec *codec)
3667 mutex_lock(&codec->hash_mutex);
3668 codec->cached_write = 0;
3669 for (i = 0; i < codec->cmd_cache.buf.used; i++) {
3670 struct hda_cache_head *buffer;
3673 buffer = snd_array_elem(&codec->cmd_cache.buf, i);
3680 mutex_unlock(&codec->hash_mutex);
3681 snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
3682 get_cmd_cache_cmd(key), buffer->val);
3683 mutex_lock(&codec->hash_mutex);
3685 mutex_unlock(&codec->hash_mutex);
3687 EXPORT_SYMBOL_HDA(snd_hda_codec_resume_cache);
3690 * snd_hda_sequence_write_cache - sequence writes with caching
3691 * @codec: the HDA codec
3692 * @seq: VERB array to send
3694 * Send the commands sequentially from the given array.
3695 * Thte commands are recorded on cache for power-save and resume.
3696 * The array must be terminated with NID=0.
3698 void snd_hda_sequence_write_cache(struct hda_codec *codec,
3699 const struct hda_verb *seq)
3701 for (; seq->nid; seq++)
3702 snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
3705 EXPORT_SYMBOL_HDA(snd_hda_sequence_write_cache);
3708 * snd_hda_codec_flush_cache - Execute all pending (cached) amps / verbs
3709 * @codec: HD-audio codec
3711 void snd_hda_codec_flush_cache(struct hda_codec *codec)
3713 snd_hda_codec_resume_amp(codec);
3714 snd_hda_codec_resume_cache(codec);
3716 EXPORT_SYMBOL_HDA(snd_hda_codec_flush_cache);
3718 void snd_hda_codec_set_power_to_all(struct hda_codec *codec, hda_nid_t fg,
3719 unsigned int power_state)
3721 hda_nid_t nid = codec->start_nid;
3724 for (i = 0; i < codec->num_nodes; i++, nid++) {
3725 unsigned int wcaps = get_wcaps(codec, nid);
3726 unsigned int state = power_state;
3727 if (!(wcaps & AC_WCAP_POWER))
3729 if (codec->power_filter) {
3730 state = codec->power_filter(codec, nid, power_state);
3731 if (state != power_state && power_state == AC_PWRST_D3)
3734 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE,
3738 EXPORT_SYMBOL_HDA(snd_hda_codec_set_power_to_all);
3741 * supported power states check
3743 static bool snd_hda_codec_get_supported_ps(struct hda_codec *codec, hda_nid_t fg,
3744 unsigned int power_state)
3746 int sup = snd_hda_param_read(codec, fg, AC_PAR_POWER_STATE);
3750 if (sup & power_state)
3757 * wait until the state is reached, returns the current state
3759 static unsigned int hda_sync_power_state(struct hda_codec *codec,
3761 unsigned int power_state)
3763 unsigned long end_time = jiffies + msecs_to_jiffies(500);
3764 unsigned int state, actual_state;
3767 state = snd_hda_codec_read(codec, fg, 0,
3768 AC_VERB_GET_POWER_STATE, 0);
3769 if (state & AC_PWRST_ERROR)
3771 actual_state = (state >> 4) & 0x0f;
3772 if (actual_state == power_state)
3774 if (time_after_eq(jiffies, end_time))
3776 /* wait until the codec reachs to the target state */
3782 /* don't power down the widget if it controls eapd and EAPD_BTLENABLE is set */
3783 unsigned int snd_hda_codec_eapd_power_filter(struct hda_codec *codec,
3785 unsigned int power_state)
3787 if (power_state == AC_PWRST_D3 &&
3788 get_wcaps_type(get_wcaps(codec, nid)) == AC_WID_PIN &&
3789 (snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD)) {
3790 int eapd = snd_hda_codec_read(codec, nid, 0,
3791 AC_VERB_GET_EAPD_BTLENABLE, 0);
3797 EXPORT_SYMBOL_HDA(snd_hda_codec_eapd_power_filter);
3800 * set power state of the codec, and return the power state
3802 static unsigned int hda_set_power_state(struct hda_codec *codec,
3803 unsigned int power_state)
3805 hda_nid_t fg = codec->afg ? codec->afg : codec->mfg;
3809 /* this delay seems necessary to avoid click noise at power-down */
3810 if (power_state == AC_PWRST_D3) {
3811 /* transition time less than 10ms for power down */
3812 msleep(codec->epss ? 10 : 100);
3815 /* repeat power states setting at most 10 times*/
3816 for (count = 0; count < 10; count++) {
3817 if (codec->patch_ops.set_power_state)
3818 codec->patch_ops.set_power_state(codec, fg,
3821 snd_hda_codec_read(codec, fg, 0,
3822 AC_VERB_SET_POWER_STATE,
3824 snd_hda_codec_set_power_to_all(codec, fg, power_state);
3826 state = hda_sync_power_state(codec, fg, power_state);
3827 if (!(state & AC_PWRST_ERROR))
3834 /* sync power states of all widgets;
3835 * this is called at the end of codec parsing
3837 static void sync_power_up_states(struct hda_codec *codec)
3839 hda_nid_t nid = codec->start_nid;
3842 /* don't care if no filter is used */
3843 if (!codec->power_filter)
3846 for (i = 0; i < codec->num_nodes; i++, nid++) {
3847 unsigned int wcaps = get_wcaps(codec, nid);
3848 unsigned int target;
3849 if (!(wcaps & AC_WCAP_POWER))
3851 target = codec->power_filter(codec, nid, AC_PWRST_D0);
3852 if (target == AC_PWRST_D0)
3854 if (!snd_hda_check_power_state(codec, nid, target))
3855 snd_hda_codec_write(codec, nid, 0,
3856 AC_VERB_SET_POWER_STATE, target);
3860 #ifdef CONFIG_SND_HDA_HWDEP
3861 /* execute additional init verbs */
3862 static void hda_exec_init_verbs(struct hda_codec *codec)
3864 if (codec->init_verbs.list)
3865 snd_hda_sequence_write(codec, codec->init_verbs.list);
3868 static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
3873 * call suspend and power-down; used both from PM and power-save
3874 * this function returns the power state in the end
3876 static unsigned int hda_call_codec_suspend(struct hda_codec *codec, bool in_wq)
3882 if (codec->patch_ops.suspend)
3883 codec->patch_ops.suspend(codec);
3884 hda_cleanup_all_streams(codec);
3885 state = hda_set_power_state(codec, AC_PWRST_D3);
3886 /* Cancel delayed work if we aren't currently running from it. */
3888 cancel_delayed_work_sync(&codec->power_work);
3889 spin_lock(&codec->power_lock);
3890 snd_hda_update_power_acct(codec);
3891 trace_hda_power_down(codec);
3892 codec->power_on = 0;
3893 codec->power_transition = 0;
3894 codec->power_jiffies = jiffies;
3895 spin_unlock(&codec->power_lock);
3900 /* mark all entries of cmd and amp caches dirty */
3901 static void hda_mark_cmd_cache_dirty(struct hda_codec *codec)
3904 for (i = 0; i < codec->cmd_cache.buf.used; i++) {
3905 struct hda_cache_head *cmd;
3906 cmd = snd_array_elem(&codec->cmd_cache.buf, i);
3909 for (i = 0; i < codec->amp_cache.buf.used; i++) {
3910 struct hda_amp_info *amp;
3911 amp = snd_array_elem(&codec->amp_cache.buf, i);
3912 amp->head.dirty = 1;
3917 * kick up codec; used both from PM and power-save
3919 static void hda_call_codec_resume(struct hda_codec *codec)
3923 hda_mark_cmd_cache_dirty(codec);
3925 /* set as if powered on for avoiding re-entering the resume
3926 * in the resume / power-save sequence
3928 hda_keep_power_on(codec);
3929 hda_set_power_state(codec, AC_PWRST_D0);
3930 restore_shutup_pins(codec);
3931 hda_exec_init_verbs(codec);
3932 snd_hda_jack_set_dirty_all(codec);
3933 if (codec->patch_ops.resume)
3934 codec->patch_ops.resume(codec);
3936 if (codec->patch_ops.init)
3937 codec->patch_ops.init(codec);
3938 snd_hda_codec_resume_amp(codec);
3939 snd_hda_codec_resume_cache(codec);
3942 if (codec->jackpoll_interval)
3943 hda_jackpoll_work(&codec->jackpoll_work.work);
3945 snd_hda_jack_report_sync(codec);
3948 snd_hda_power_down(codec); /* flag down before returning */
3950 #endif /* CONFIG_PM */
3954 * snd_hda_build_controls - build mixer controls
3957 * Creates mixer controls for each codec included in the bus.
3959 * Returns 0 if successful, otherwise a negative error code.
3961 int snd_hda_build_controls(struct hda_bus *bus)
3963 struct hda_codec *codec;
3965 list_for_each_entry(codec, &bus->codec_list, list) {
3966 int err = snd_hda_codec_build_controls(codec);
3968 printk(KERN_ERR "hda_codec: cannot build controls "
3969 "for #%d (error %d)\n", codec->addr, err);
3970 err = snd_hda_codec_reset(codec);
3973 "hda_codec: cannot revert codec\n");
3980 EXPORT_SYMBOL_HDA(snd_hda_build_controls);
3983 * add standard channel maps if not specified
3985 static int add_std_chmaps(struct hda_codec *codec)
3989 for (i = 0; i < codec->num_pcms; i++) {
3990 for (str = 0; str < 2; str++) {
3991 struct snd_pcm *pcm = codec->pcm_info[i].pcm;
3992 struct hda_pcm_stream *hinfo =
3993 &codec->pcm_info[i].stream[str];
3994 struct snd_pcm_chmap *chmap;
3995 const struct snd_pcm_chmap_elem *elem;
3997 if (codec->pcm_info[i].own_chmap)
3999 if (!pcm || !hinfo->substreams)
4001 elem = hinfo->chmap ? hinfo->chmap : snd_pcm_std_chmaps;
4002 err = snd_pcm_add_chmap_ctls(pcm, str, elem,
4003 hinfo->channels_max,
4007 chmap->channel_mask = SND_PCM_CHMAP_MASK_2468;
4013 /* default channel maps for 2.1 speakers;
4014 * since HD-audio supports only stereo, odd number channels are omitted
4016 const struct snd_pcm_chmap_elem snd_pcm_2_1_chmaps[] = {
4018 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } },
4020 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
4021 SNDRV_CHMAP_LFE, SNDRV_CHMAP_LFE } },
4024 EXPORT_SYMBOL_GPL(snd_pcm_2_1_chmaps);
4026 int snd_hda_codec_build_controls(struct hda_codec *codec)
4029 hda_exec_init_verbs(codec);
4030 /* continue to initialize... */
4031 if (codec->patch_ops.init)
4032 err = codec->patch_ops.init(codec);
4033 if (!err && codec->patch_ops.build_controls)
4034 err = codec->patch_ops.build_controls(codec);
4038 /* we create chmaps here instead of build_pcms */
4039 err = add_std_chmaps(codec);
4043 if (codec->jackpoll_interval)
4044 hda_jackpoll_work(&codec->jackpoll_work.work);
4046 snd_hda_jack_report_sync(codec); /* call at the last init point */
4047 sync_power_up_states(codec);
4054 struct hda_rate_tbl {
4056 unsigned int alsa_bits;
4057 unsigned int hda_fmt;
4060 /* rate = base * mult / div */
4061 #define HDA_RATE(base, mult, div) \
4062 (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \
4063 (((div) - 1) << AC_FMT_DIV_SHIFT))
4065 static struct hda_rate_tbl rate_bits[] = {
4066 /* rate in Hz, ALSA rate bitmask, HDA format value */
4068 /* autodetected value used in snd_hda_query_supported_pcm */
4069 { 8000, SNDRV_PCM_RATE_8000, HDA_RATE(48, 1, 6) },
4070 { 11025, SNDRV_PCM_RATE_11025, HDA_RATE(44, 1, 4) },
4071 { 16000, SNDRV_PCM_RATE_16000, HDA_RATE(48, 1, 3) },
4072 { 22050, SNDRV_PCM_RATE_22050, HDA_RATE(44, 1, 2) },
4073 { 32000, SNDRV_PCM_RATE_32000, HDA_RATE(48, 2, 3) },
4074 { 44100, SNDRV_PCM_RATE_44100, HDA_RATE(44, 1, 1) },
4075 { 48000, SNDRV_PCM_RATE_48000, HDA_RATE(48, 1, 1) },
4076 { 88200, SNDRV_PCM_RATE_88200, HDA_RATE(44, 2, 1) },
4077 { 96000, SNDRV_PCM_RATE_96000, HDA_RATE(48, 2, 1) },
4078 { 176400, SNDRV_PCM_RATE_176400, HDA_RATE(44, 4, 1) },
4079 { 192000, SNDRV_PCM_RATE_192000, HDA_RATE(48, 4, 1) },
4080 #define AC_PAR_PCM_RATE_BITS 11
4081 /* up to bits 10, 384kHZ isn't supported properly */
4083 /* not autodetected value */
4084 { 9600, SNDRV_PCM_RATE_KNOT, HDA_RATE(48, 1, 5) },
4086 { 0 } /* terminator */
4090 * snd_hda_calc_stream_format - calculate format bitset
4091 * @rate: the sample rate
4092 * @channels: the number of channels
4093 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
4094 * @maxbps: the max. bps
4096 * Calculate the format bitset from the given rate, channels and th PCM format.
4098 * Return zero if invalid.
4100 unsigned int snd_hda_calc_stream_format(unsigned int rate,
4101 unsigned int channels,
4102 unsigned int format,
4103 unsigned int maxbps,
4104 unsigned short spdif_ctls)
4107 unsigned int val = 0;
4109 for (i = 0; rate_bits[i].hz; i++)
4110 if (rate_bits[i].hz == rate) {
4111 val = rate_bits[i].hda_fmt;
4114 if (!rate_bits[i].hz) {
4115 snd_printdd("invalid rate %d\n", rate);
4119 if (channels == 0 || channels > 8) {
4120 snd_printdd("invalid channels %d\n", channels);
4123 val |= channels - 1;
4125 switch (snd_pcm_format_width(format)) {
4127 val |= AC_FMT_BITS_8;
4130 val |= AC_FMT_BITS_16;
4135 if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE)
4136 val |= AC_FMT_BITS_32;
4137 else if (maxbps >= 24)
4138 val |= AC_FMT_BITS_24;
4140 val |= AC_FMT_BITS_20;
4143 snd_printdd("invalid format width %d\n",
4144 snd_pcm_format_width(format));
4148 if (spdif_ctls & AC_DIG1_NONAUDIO)
4149 val |= AC_FMT_TYPE_NON_PCM;
4153 EXPORT_SYMBOL_HDA(snd_hda_calc_stream_format);
4155 static unsigned int get_pcm_param(struct hda_codec *codec, hda_nid_t nid,
4158 unsigned int val = 0;
4159 if (nid != codec->afg &&
4160 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD))
4161 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
4162 if (!val || val == -1)
4163 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
4164 if (!val || val == -1)
4169 static unsigned int query_pcm_param(struct hda_codec *codec, hda_nid_t nid)
4171 return query_caps_hash(codec, nid, 0, HDA_HASH_PARPCM_KEY(nid),
4175 static unsigned int get_stream_param(struct hda_codec *codec, hda_nid_t nid,
4178 unsigned int streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
4179 if (!streams || streams == -1)
4180 streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
4181 if (!streams || streams == -1)
4186 static unsigned int query_stream_param(struct hda_codec *codec, hda_nid_t nid)
4188 return query_caps_hash(codec, nid, 0, HDA_HASH_PARSTR_KEY(nid),
4193 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
4194 * @codec: the HDA codec
4195 * @nid: NID to query
4196 * @ratesp: the pointer to store the detected rate bitflags
4197 * @formatsp: the pointer to store the detected formats
4198 * @bpsp: the pointer to store the detected format widths
4200 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
4201 * or @bsps argument is ignored.
4203 * Returns 0 if successful, otherwise a negative error code.
4205 int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
4206 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
4208 unsigned int i, val, wcaps;
4210 wcaps = get_wcaps(codec, nid);
4211 val = query_pcm_param(codec, nid);
4215 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
4217 rates |= rate_bits[i].alsa_bits;
4220 snd_printk(KERN_ERR "hda_codec: rates == 0 "
4221 "(nid=0x%x, val=0x%x, ovrd=%i)\n",
4223 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0);
4229 if (formatsp || bpsp) {
4231 unsigned int streams, bps;
4233 streams = query_stream_param(codec, nid);
4238 if (streams & AC_SUPFMT_PCM) {
4239 if (val & AC_SUPPCM_BITS_8) {
4240 formats |= SNDRV_PCM_FMTBIT_U8;
4243 if (val & AC_SUPPCM_BITS_16) {
4244 formats |= SNDRV_PCM_FMTBIT_S16_LE;
4247 if (wcaps & AC_WCAP_DIGITAL) {
4248 if (val & AC_SUPPCM_BITS_32)
4249 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
4250 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
4251 formats |= SNDRV_PCM_FMTBIT_S32_LE;
4252 if (val & AC_SUPPCM_BITS_24)
4254 else if (val & AC_SUPPCM_BITS_20)
4256 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
4257 AC_SUPPCM_BITS_32)) {
4258 formats |= SNDRV_PCM_FMTBIT_S32_LE;
4259 if (val & AC_SUPPCM_BITS_32)
4261 else if (val & AC_SUPPCM_BITS_24)
4263 else if (val & AC_SUPPCM_BITS_20)
4267 #if 0 /* FIXME: CS4206 doesn't work, which is the only codec supporting float */
4268 if (streams & AC_SUPFMT_FLOAT32) {
4269 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
4274 if (streams == AC_SUPFMT_AC3) {
4275 /* should be exclusive */
4276 /* temporary hack: we have still no proper support
4277 * for the direct AC3 stream...
4279 formats |= SNDRV_PCM_FMTBIT_U8;
4283 snd_printk(KERN_ERR "hda_codec: formats == 0 "
4284 "(nid=0x%x, val=0x%x, ovrd=%i, "
4287 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0,
4292 *formatsp = formats;
4299 EXPORT_SYMBOL_HDA(snd_hda_query_supported_pcm);
4302 * snd_hda_is_supported_format - Check the validity of the format
4303 * @codec: HD-audio codec
4304 * @nid: NID to check
4305 * @format: the HD-audio format value to check
4307 * Check whether the given node supports the format value.
4309 * Returns 1 if supported, 0 if not.
4311 int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
4312 unsigned int format)
4315 unsigned int val = 0, rate, stream;
4317 val = query_pcm_param(codec, nid);
4321 rate = format & 0xff00;
4322 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
4323 if (rate_bits[i].hda_fmt == rate) {
4328 if (i >= AC_PAR_PCM_RATE_BITS)
4331 stream = query_stream_param(codec, nid);
4335 if (stream & AC_SUPFMT_PCM) {
4336 switch (format & 0xf0) {
4338 if (!(val & AC_SUPPCM_BITS_8))
4342 if (!(val & AC_SUPPCM_BITS_16))
4346 if (!(val & AC_SUPPCM_BITS_20))
4350 if (!(val & AC_SUPPCM_BITS_24))
4354 if (!(val & AC_SUPPCM_BITS_32))
4361 /* FIXME: check for float32 and AC3? */
4366 EXPORT_SYMBOL_HDA(snd_hda_is_supported_format);
4371 static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
4372 struct hda_codec *codec,
4373 struct snd_pcm_substream *substream)
4378 static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
4379 struct hda_codec *codec,
4380 unsigned int stream_tag,
4381 unsigned int format,
4382 struct snd_pcm_substream *substream)
4384 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4388 static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
4389 struct hda_codec *codec,
4390 struct snd_pcm_substream *substream)
4392 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
4396 static int set_pcm_default_values(struct hda_codec *codec,
4397 struct hda_pcm_stream *info)
4401 /* query support PCM information from the given NID */
4402 if (info->nid && (!info->rates || !info->formats)) {
4403 err = snd_hda_query_supported_pcm(codec, info->nid,
4404 info->rates ? NULL : &info->rates,
4405 info->formats ? NULL : &info->formats,
4406 info->maxbps ? NULL : &info->maxbps);
4410 if (info->ops.open == NULL)
4411 info->ops.open = hda_pcm_default_open_close;
4412 if (info->ops.close == NULL)
4413 info->ops.close = hda_pcm_default_open_close;
4414 if (info->ops.prepare == NULL) {
4415 if (snd_BUG_ON(!info->nid))
4417 info->ops.prepare = hda_pcm_default_prepare;
4419 if (info->ops.cleanup == NULL) {
4420 if (snd_BUG_ON(!info->nid))
4422 info->ops.cleanup = hda_pcm_default_cleanup;
4428 * codec prepare/cleanup entries
4430 int snd_hda_codec_prepare(struct hda_codec *codec,
4431 struct hda_pcm_stream *hinfo,
4432 unsigned int stream,
4433 unsigned int format,
4434 struct snd_pcm_substream *substream)
4437 mutex_lock(&codec->bus->prepare_mutex);
4438 ret = hinfo->ops.prepare(hinfo, codec, stream, format, substream);
4440 purify_inactive_streams(codec);
4441 mutex_unlock(&codec->bus->prepare_mutex);
4444 EXPORT_SYMBOL_HDA(snd_hda_codec_prepare);
4446 void snd_hda_codec_cleanup(struct hda_codec *codec,
4447 struct hda_pcm_stream *hinfo,
4448 struct snd_pcm_substream *substream)
4450 mutex_lock(&codec->bus->prepare_mutex);
4451 hinfo->ops.cleanup(hinfo, codec, substream);
4452 mutex_unlock(&codec->bus->prepare_mutex);
4454 EXPORT_SYMBOL_HDA(snd_hda_codec_cleanup);
4457 const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = {
4458 "Audio", "SPDIF", "HDMI", "Modem"
4462 * get the empty PCM device number to assign
4464 static int get_empty_pcm_device(struct hda_bus *bus, unsigned int type)
4466 /* audio device indices; not linear to keep compatibility */
4467 /* assigned to static slots up to dev#10; if more needed, assign
4468 * the later slot dynamically (when CONFIG_SND_DYNAMIC_MINORS=y)
4470 static int audio_idx[HDA_PCM_NTYPES][5] = {
4471 [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 },
4472 [HDA_PCM_TYPE_SPDIF] = { 1, -1 },
4473 [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 },
4474 [HDA_PCM_TYPE_MODEM] = { 6, -1 },
4478 if (type >= HDA_PCM_NTYPES) {
4479 snd_printk(KERN_WARNING "Invalid PCM type %d\n", type);
4483 for (i = 0; audio_idx[type][i] >= 0; i++) {
4484 #ifndef CONFIG_SND_DYNAMIC_MINORS
4485 if (audio_idx[type][i] >= 8)
4488 if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits))
4489 return audio_idx[type][i];
4492 #ifdef CONFIG_SND_DYNAMIC_MINORS
4493 /* non-fixed slots starting from 10 */
4494 for (i = 10; i < 32; i++) {
4495 if (!test_and_set_bit(i, bus->pcm_dev_bits))
4500 snd_printk(KERN_WARNING "Too many %s devices\n",
4501 snd_hda_pcm_type_name[type]);
4502 #ifndef CONFIG_SND_DYNAMIC_MINORS
4503 snd_printk(KERN_WARNING "Consider building the kernel with CONFIG_SND_DYNAMIC_MINORS=y\n");
4509 * attach a new PCM stream
4511 static int snd_hda_attach_pcm(struct hda_codec *codec, struct hda_pcm *pcm)
4513 struct hda_bus *bus = codec->bus;
4514 struct hda_pcm_stream *info;
4517 if (snd_BUG_ON(!pcm->name))
4519 for (stream = 0; stream < 2; stream++) {
4520 info = &pcm->stream[stream];
4521 if (info->substreams) {
4522 err = set_pcm_default_values(codec, info);
4527 return bus->ops.attach_pcm(bus, codec, pcm);
4530 /* assign all PCMs of the given codec */
4531 int snd_hda_codec_build_pcms(struct hda_codec *codec)
4536 if (!codec->num_pcms) {
4537 if (!codec->patch_ops.build_pcms)
4539 err = codec->patch_ops.build_pcms(codec);
4541 printk(KERN_ERR "hda_codec: cannot build PCMs"
4542 "for #%d (error %d)\n", codec->addr, err);
4543 err = snd_hda_codec_reset(codec);
4546 "hda_codec: cannot revert codec\n");
4551 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
4552 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
4555 if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
4556 continue; /* no substreams assigned */
4559 dev = get_empty_pcm_device(codec->bus, cpcm->pcm_type);
4561 continue; /* no fatal error */
4563 err = snd_hda_attach_pcm(codec, cpcm);
4565 printk(KERN_ERR "hda_codec: cannot attach "
4566 "PCM stream %d for codec #%d\n",
4568 continue; /* no fatal error */
4576 * snd_hda_build_pcms - build PCM information
4579 * Create PCM information for each codec included in the bus.
4581 * The build_pcms codec patch is requested to set up codec->num_pcms and
4582 * codec->pcm_info properly. The array is referred by the top-level driver
4583 * to create its PCM instances.
4584 * The allocated codec->pcm_info should be released in codec->patch_ops.free
4587 * At least, substreams, channels_min and channels_max must be filled for
4588 * each stream. substreams = 0 indicates that the stream doesn't exist.
4589 * When rates and/or formats are zero, the supported values are queried
4590 * from the given nid. The nid is used also by the default ops.prepare
4591 * and ops.cleanup callbacks.
4593 * The driver needs to call ops.open in its open callback. Similarly,
4594 * ops.close is supposed to be called in the close callback.
4595 * ops.prepare should be called in the prepare or hw_params callback
4596 * with the proper parameters for set up.
4597 * ops.cleanup should be called in hw_free for clean up of streams.
4599 * This function returns 0 if successful, or a negative error code.
4601 int snd_hda_build_pcms(struct hda_bus *bus)
4603 struct hda_codec *codec;
4605 list_for_each_entry(codec, &bus->codec_list, list) {
4606 int err = snd_hda_codec_build_pcms(codec);
4612 EXPORT_SYMBOL_HDA(snd_hda_build_pcms);
4615 * snd_hda_check_board_config - compare the current codec with the config table
4616 * @codec: the HDA codec
4617 * @num_configs: number of config enums
4618 * @models: array of model name strings
4619 * @tbl: configuration table, terminated by null entries
4621 * Compares the modelname or PCI subsystem id of the current codec with the
4622 * given configuration table. If a matching entry is found, returns its
4623 * config value (supposed to be 0 or positive).
4625 * If no entries are matching, the function returns a negative value.
4627 int snd_hda_check_board_config(struct hda_codec *codec,
4628 int num_configs, const char * const *models,
4629 const struct snd_pci_quirk *tbl)
4631 if (codec->modelname && models) {
4633 for (i = 0; i < num_configs; i++) {
4635 !strcmp(codec->modelname, models[i])) {
4636 snd_printd(KERN_INFO "hda_codec: model '%s' is "
4637 "selected\n", models[i]);
4643 if (!codec->bus->pci || !tbl)
4646 tbl = snd_pci_quirk_lookup(codec->bus->pci, tbl);
4649 if (tbl->value >= 0 && tbl->value < num_configs) {
4650 #ifdef CONFIG_SND_DEBUG_VERBOSE
4652 const char *model = NULL;
4654 model = models[tbl->value];
4656 sprintf(tmp, "#%d", tbl->value);
4659 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
4660 "for config %x:%x (%s)\n",
4661 model, tbl->subvendor, tbl->subdevice,
4662 (tbl->name ? tbl->name : "Unknown device"));
4668 EXPORT_SYMBOL_HDA(snd_hda_check_board_config);
4671 * snd_hda_check_board_codec_sid_config - compare the current codec
4672 subsystem ID with the
4675 This is important for Gateway notebooks with SB450 HDA Audio
4676 where the vendor ID of the PCI device is:
4677 ATI Technologies Inc SB450 HDA Audio [1002:437b]
4678 and the vendor/subvendor are found only at the codec.
4680 * @codec: the HDA codec
4681 * @num_configs: number of config enums
4682 * @models: array of model name strings
4683 * @tbl: configuration table, terminated by null entries
4685 * Compares the modelname or PCI subsystem id of the current codec with the
4686 * given configuration table. If a matching entry is found, returns its
4687 * config value (supposed to be 0 or positive).
4689 * If no entries are matching, the function returns a negative value.
4691 int snd_hda_check_board_codec_sid_config(struct hda_codec *codec,
4692 int num_configs, const char * const *models,
4693 const struct snd_pci_quirk *tbl)
4695 const struct snd_pci_quirk *q;
4697 /* Search for codec ID */
4698 for (q = tbl; q->subvendor; q++) {
4699 unsigned int mask = 0xffff0000 | q->subdevice_mask;
4700 unsigned int id = (q->subdevice | (q->subvendor << 16)) & mask;
4701 if ((codec->subsystem_id & mask) == id)
4710 if (tbl->value >= 0 && tbl->value < num_configs) {
4711 #ifdef CONFIG_SND_DEBUG_VERBOSE
4713 const char *model = NULL;
4715 model = models[tbl->value];
4717 sprintf(tmp, "#%d", tbl->value);
4720 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
4721 "for config %x:%x (%s)\n",
4722 model, tbl->subvendor, tbl->subdevice,
4723 (tbl->name ? tbl->name : "Unknown device"));
4729 EXPORT_SYMBOL_HDA(snd_hda_check_board_codec_sid_config);
4732 * snd_hda_add_new_ctls - create controls from the array
4733 * @codec: the HDA codec
4734 * @knew: the array of struct snd_kcontrol_new
4736 * This helper function creates and add new controls in the given array.
4737 * The array must be terminated with an empty entry as terminator.
4739 * Returns 0 if successful, or a negative error code.
4741 int snd_hda_add_new_ctls(struct hda_codec *codec,
4742 const struct snd_kcontrol_new *knew)
4746 for (; knew->name; knew++) {
4747 struct snd_kcontrol *kctl;
4748 int addr = 0, idx = 0;
4749 if (knew->iface == -1) /* skip this codec private value */
4752 kctl = snd_ctl_new1(knew, codec);
4756 kctl->id.device = addr;
4758 kctl->id.index = idx;
4759 err = snd_hda_ctl_add(codec, 0, kctl);
4762 /* try first with another device index corresponding to
4763 * the codec addr; if it still fails (or it's the
4764 * primary codec), then try another control index
4766 if (!addr && codec->addr)
4768 else if (!idx && !knew->index) {
4769 idx = find_empty_mixer_ctl_idx(codec,
4779 EXPORT_SYMBOL_HDA(snd_hda_add_new_ctls);
4782 static void hda_power_work(struct work_struct *work)
4784 struct hda_codec *codec =
4785 container_of(work, struct hda_codec, power_work.work);
4786 struct hda_bus *bus = codec->bus;
4789 spin_lock(&codec->power_lock);
4790 if (codec->power_transition > 0) { /* during power-up sequence? */
4791 spin_unlock(&codec->power_lock);
4794 if (!codec->power_on || codec->power_count) {
4795 codec->power_transition = 0;
4796 spin_unlock(&codec->power_lock);
4799 spin_unlock(&codec->power_lock);
4801 state = hda_call_codec_suspend(codec, true);
4802 codec->pm_down_notified = 0;
4803 if (!bus->power_keep_link_on && (state & AC_PWRST_CLK_STOP_OK)) {
4804 codec->pm_down_notified = 1;
4805 hda_call_pm_notify(bus, false);
4809 static void hda_keep_power_on(struct hda_codec *codec)
4811 spin_lock(&codec->power_lock);
4812 codec->power_count++;
4813 codec->power_on = 1;
4814 codec->power_jiffies = jiffies;
4815 spin_unlock(&codec->power_lock);
4818 /* update the power on/off account with the current jiffies */
4819 void snd_hda_update_power_acct(struct hda_codec *codec)
4821 unsigned long delta = jiffies - codec->power_jiffies;
4822 if (codec->power_on)
4823 codec->power_on_acct += delta;
4825 codec->power_off_acct += delta;
4826 codec->power_jiffies += delta;
4829 /* Transition to powered up, if wait_power_down then wait for a pending
4830 * transition to D3 to complete. A pending D3 transition is indicated
4831 * with power_transition == -1. */
4832 /* call this with codec->power_lock held! */
4833 static void __snd_hda_power_up(struct hda_codec *codec, bool wait_power_down)
4835 struct hda_bus *bus = codec->bus;
4837 /* Return if power_on or transitioning to power_on, unless currently
4839 if ((codec->power_on || codec->power_transition > 0) &&
4840 !(wait_power_down && codec->power_transition < 0))
4842 spin_unlock(&codec->power_lock);
4844 cancel_delayed_work_sync(&codec->power_work);
4846 spin_lock(&codec->power_lock);
4847 /* If the power down delayed work was cancelled above before starting,
4848 * then there is no need to go through power up here.
4850 if (codec->power_on) {
4851 if (codec->power_transition < 0)
4852 codec->power_transition = 0;
4856 trace_hda_power_up(codec);
4857 snd_hda_update_power_acct(codec);
4858 codec->power_on = 1;
4859 codec->power_jiffies = jiffies;
4860 codec->power_transition = 1; /* avoid reentrance */
4861 spin_unlock(&codec->power_lock);
4863 if (codec->pm_down_notified) {
4864 codec->pm_down_notified = 0;
4865 hda_call_pm_notify(bus, true);
4868 hda_call_codec_resume(codec);
4870 spin_lock(&codec->power_lock);
4871 codec->power_transition = 0;
4874 #define power_save(codec) \
4875 ((codec)->bus->power_save ? *(codec)->bus->power_save : 0)
4877 /* Transition to powered down */
4878 static void __snd_hda_power_down(struct hda_codec *codec)
4880 if (!codec->power_on || codec->power_count || codec->power_transition)
4883 if (power_save(codec)) {
4884 codec->power_transition = -1; /* avoid reentrance */
4885 queue_delayed_work(codec->bus->workq, &codec->power_work,
4886 msecs_to_jiffies(power_save(codec) * 1000));
4891 * snd_hda_power_save - Power-up/down/sync the codec
4892 * @codec: HD-audio codec
4893 * @delta: the counter delta to change
4895 * Change the power-up counter via @delta, and power up or down the hardware
4896 * appropriately. For the power-down, queue to the delayed action.
4897 * Passing zero to @delta means to synchronize the power state.
4899 void snd_hda_power_save(struct hda_codec *codec, int delta, bool d3wait)
4901 spin_lock(&codec->power_lock);
4902 codec->power_count += delta;
4903 trace_hda_power_count(codec);
4905 __snd_hda_power_up(codec, d3wait);
4907 __snd_hda_power_down(codec);
4908 spin_unlock(&codec->power_lock);
4910 EXPORT_SYMBOL_HDA(snd_hda_power_save);
4913 * snd_hda_check_amp_list_power - Check the amp list and update the power
4914 * @codec: HD-audio codec
4915 * @check: the object containing an AMP list and the status
4916 * @nid: NID to check / update
4918 * Check whether the given NID is in the amp list. If it's in the list,
4919 * check the current AMP status, and update the the power-status according
4920 * to the mute status.
4922 * This function is supposed to be set or called from the check_power_status
4925 int snd_hda_check_amp_list_power(struct hda_codec *codec,
4926 struct hda_loopback_check *check,
4929 const struct hda_amp_list *p;
4932 if (!check->amplist)
4934 for (p = check->amplist; p->nid; p++) {
4939 return 0; /* nothing changed */
4941 for (p = check->amplist; p->nid; p++) {
4942 for (ch = 0; ch < 2; ch++) {
4943 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
4945 if (!(v & HDA_AMP_MUTE) && v > 0) {
4946 if (!check->power_on) {
4947 check->power_on = 1;
4948 snd_hda_power_up(codec);
4954 if (check->power_on) {
4955 check->power_on = 0;
4956 snd_hda_power_down(codec);
4960 EXPORT_SYMBOL_HDA(snd_hda_check_amp_list_power);
4964 * Channel mode helper
4968 * snd_hda_ch_mode_info - Info callback helper for the channel mode enum
4970 int snd_hda_ch_mode_info(struct hda_codec *codec,
4971 struct snd_ctl_elem_info *uinfo,
4972 const struct hda_channel_mode *chmode,
4975 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4977 uinfo->value.enumerated.items = num_chmodes;
4978 if (uinfo->value.enumerated.item >= num_chmodes)
4979 uinfo->value.enumerated.item = num_chmodes - 1;
4980 sprintf(uinfo->value.enumerated.name, "%dch",
4981 chmode[uinfo->value.enumerated.item].channels);
4984 EXPORT_SYMBOL_HDA(snd_hda_ch_mode_info);
4987 * snd_hda_ch_mode_get - Get callback helper for the channel mode enum
4989 int snd_hda_ch_mode_get(struct hda_codec *codec,
4990 struct snd_ctl_elem_value *ucontrol,
4991 const struct hda_channel_mode *chmode,
4997 for (i = 0; i < num_chmodes; i++) {
4998 if (max_channels == chmode[i].channels) {
4999 ucontrol->value.enumerated.item[0] = i;
5005 EXPORT_SYMBOL_HDA(snd_hda_ch_mode_get);
5008 * snd_hda_ch_mode_put - Put callback helper for the channel mode enum
5010 int snd_hda_ch_mode_put(struct hda_codec *codec,
5011 struct snd_ctl_elem_value *ucontrol,
5012 const struct hda_channel_mode *chmode,
5018 mode = ucontrol->value.enumerated.item[0];
5019 if (mode >= num_chmodes)
5021 if (*max_channelsp == chmode[mode].channels)
5023 /* change the current channel setting */
5024 *max_channelsp = chmode[mode].channels;
5025 if (chmode[mode].sequence)
5026 snd_hda_sequence_write_cache(codec, chmode[mode].sequence);
5029 EXPORT_SYMBOL_HDA(snd_hda_ch_mode_put);
5036 * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum
5038 int snd_hda_input_mux_info(const struct hda_input_mux *imux,
5039 struct snd_ctl_elem_info *uinfo)
5043 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
5045 uinfo->value.enumerated.items = imux->num_items;
5046 if (!imux->num_items)
5048 index = uinfo->value.enumerated.item;
5049 if (index >= imux->num_items)
5050 index = imux->num_items - 1;
5051 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
5054 EXPORT_SYMBOL_HDA(snd_hda_input_mux_info);
5057 * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum
5059 int snd_hda_input_mux_put(struct hda_codec *codec,
5060 const struct hda_input_mux *imux,
5061 struct snd_ctl_elem_value *ucontrol,
5063 unsigned int *cur_val)
5067 if (!imux->num_items)
5069 idx = ucontrol->value.enumerated.item[0];
5070 if (idx >= imux->num_items)
5071 idx = imux->num_items - 1;
5072 if (*cur_val == idx)
5074 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
5075 imux->items[idx].index);
5079 EXPORT_SYMBOL_HDA(snd_hda_input_mux_put);
5083 * process kcontrol info callback of a simple string enum array
5084 * when @num_items is 0 or @texts is NULL, assume a boolean enum array
5086 int snd_hda_enum_helper_info(struct snd_kcontrol *kcontrol,
5087 struct snd_ctl_elem_info *uinfo,
5088 int num_items, const char * const *texts)
5090 static const char * const texts_default[] = {
5091 "Disabled", "Enabled"
5094 if (!texts || !num_items) {
5096 texts = texts_default;
5099 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
5101 uinfo->value.enumerated.items = num_items;
5102 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
5103 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
5104 strcpy(uinfo->value.enumerated.name,
5105 texts[uinfo->value.enumerated.item]);
5108 EXPORT_SYMBOL_HDA(snd_hda_enum_helper_info);
5111 * Multi-channel / digital-out PCM helper functions
5114 /* setup SPDIF output stream */
5115 static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
5116 unsigned int stream_tag, unsigned int format)
5118 struct hda_spdif_out *spdif;
5119 unsigned int curr_fmt;
5122 spdif = snd_hda_spdif_out_of_nid(codec, nid);
5123 curr_fmt = snd_hda_codec_read(codec, nid, 0,
5124 AC_VERB_GET_STREAM_FORMAT, 0);
5125 reset = codec->spdif_status_reset &&
5126 (spdif->ctls & AC_DIG1_ENABLE) &&
5129 /* turn off SPDIF if needed; otherwise the IEC958 bits won't be
5132 set_dig_out_convert(codec, nid,
5133 spdif->ctls & ~AC_DIG1_ENABLE & 0xff,
5135 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
5136 if (codec->slave_dig_outs) {
5138 for (d = codec->slave_dig_outs; *d; d++)
5139 snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
5142 /* turn on again (if needed) */
5144 set_dig_out_convert(codec, nid,
5145 spdif->ctls & 0xff, -1);
5148 static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
5150 snd_hda_codec_cleanup_stream(codec, nid);
5151 if (codec->slave_dig_outs) {
5153 for (d = codec->slave_dig_outs; *d; d++)
5154 snd_hda_codec_cleanup_stream(codec, *d);
5159 * snd_hda_bus_reboot_notify - call the reboot notifier of each codec
5160 * @bus: HD-audio bus
5162 void snd_hda_bus_reboot_notify(struct hda_bus *bus)
5164 struct hda_codec *codec;
5168 list_for_each_entry(codec, &bus->codec_list, list) {
5169 if (hda_codec_is_power_on(codec) &&
5170 codec->patch_ops.reboot_notify)
5171 codec->patch_ops.reboot_notify(codec);
5174 EXPORT_SYMBOL_HDA(snd_hda_bus_reboot_notify);
5177 * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode
5179 int snd_hda_multi_out_dig_open(struct hda_codec *codec,
5180 struct hda_multi_out *mout)
5182 mutex_lock(&codec->spdif_mutex);
5183 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
5184 /* already opened as analog dup; reset it once */
5185 cleanup_dig_out_stream(codec, mout->dig_out_nid);
5186 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
5187 mutex_unlock(&codec->spdif_mutex);
5190 EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_open);
5193 * snd_hda_multi_out_dig_prepare - prepare the digital out stream
5195 int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
5196 struct hda_multi_out *mout,
5197 unsigned int stream_tag,
5198 unsigned int format,
5199 struct snd_pcm_substream *substream)
5201 mutex_lock(&codec->spdif_mutex);
5202 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
5203 mutex_unlock(&codec->spdif_mutex);
5206 EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_prepare);
5209 * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream
5211 int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec,
5212 struct hda_multi_out *mout)
5214 mutex_lock(&codec->spdif_mutex);
5215 cleanup_dig_out_stream(codec, mout->dig_out_nid);
5216 mutex_unlock(&codec->spdif_mutex);
5219 EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_cleanup);
5222 * snd_hda_multi_out_dig_close - release the digital out stream
5224 int snd_hda_multi_out_dig_close(struct hda_codec *codec,
5225 struct hda_multi_out *mout)
5227 mutex_lock(&codec->spdif_mutex);
5228 mout->dig_out_used = 0;
5229 mutex_unlock(&codec->spdif_mutex);
5232 EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_close);
5235 * snd_hda_multi_out_analog_open - open analog outputs
5237 * Open analog outputs and set up the hw-constraints.
5238 * If the digital outputs can be opened as slave, open the digital
5241 int snd_hda_multi_out_analog_open(struct hda_codec *codec,
5242 struct hda_multi_out *mout,
5243 struct snd_pcm_substream *substream,
5244 struct hda_pcm_stream *hinfo)
5246 struct snd_pcm_runtime *runtime = substream->runtime;
5247 runtime->hw.channels_max = mout->max_channels;
5248 if (mout->dig_out_nid) {
5249 if (!mout->analog_rates) {
5250 mout->analog_rates = hinfo->rates;
5251 mout->analog_formats = hinfo->formats;
5252 mout->analog_maxbps = hinfo->maxbps;
5254 runtime->hw.rates = mout->analog_rates;
5255 runtime->hw.formats = mout->analog_formats;
5256 hinfo->maxbps = mout->analog_maxbps;
5258 if (!mout->spdif_rates) {
5259 snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
5261 &mout->spdif_formats,
5262 &mout->spdif_maxbps);
5264 mutex_lock(&codec->spdif_mutex);
5265 if (mout->share_spdif) {
5266 if ((runtime->hw.rates & mout->spdif_rates) &&
5267 (runtime->hw.formats & mout->spdif_formats)) {
5268 runtime->hw.rates &= mout->spdif_rates;
5269 runtime->hw.formats &= mout->spdif_formats;
5270 if (mout->spdif_maxbps < hinfo->maxbps)
5271 hinfo->maxbps = mout->spdif_maxbps;
5273 mout->share_spdif = 0;
5274 /* FIXME: need notify? */
5277 mutex_unlock(&codec->spdif_mutex);
5279 return snd_pcm_hw_constraint_step(substream->runtime, 0,
5280 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
5282 EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_open);
5285 * snd_hda_multi_out_analog_prepare - Preapre the analog outputs.
5287 * Set up the i/o for analog out.
5288 * When the digital out is available, copy the front out to digital out, too.
5290 int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
5291 struct hda_multi_out *mout,
5292 unsigned int stream_tag,
5293 unsigned int format,
5294 struct snd_pcm_substream *substream)
5296 const hda_nid_t *nids = mout->dac_nids;
5297 int chs = substream->runtime->channels;
5298 struct hda_spdif_out *spdif;
5301 mutex_lock(&codec->spdif_mutex);
5302 spdif = snd_hda_spdif_out_of_nid(codec, mout->dig_out_nid);
5303 if (mout->dig_out_nid && mout->share_spdif &&
5304 mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
5306 snd_hda_is_supported_format(codec, mout->dig_out_nid,
5308 !(spdif->status & IEC958_AES0_NONAUDIO)) {
5309 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
5310 setup_dig_out_stream(codec, mout->dig_out_nid,
5311 stream_tag, format);
5313 mout->dig_out_used = 0;
5314 cleanup_dig_out_stream(codec, mout->dig_out_nid);
5317 mutex_unlock(&codec->spdif_mutex);
5320 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
5322 if (!mout->no_share_stream &&
5323 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
5324 /* headphone out will just decode front left/right (stereo) */
5325 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
5327 /* extra outputs copied from front */
5328 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
5329 if (!mout->no_share_stream && mout->hp_out_nid[i])
5330 snd_hda_codec_setup_stream(codec,
5331 mout->hp_out_nid[i],
5332 stream_tag, 0, format);
5333 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
5334 if (!mout->no_share_stream && mout->extra_out_nid[i])
5335 snd_hda_codec_setup_stream(codec,
5336 mout->extra_out_nid[i],
5337 stream_tag, 0, format);
5340 for (i = 1; i < mout->num_dacs; i++) {
5341 if (chs >= (i + 1) * 2) /* independent out */
5342 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
5344 else if (!mout->no_share_stream) /* copy front */
5345 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
5350 EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_prepare);
5353 * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out
5355 int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
5356 struct hda_multi_out *mout)
5358 const hda_nid_t *nids = mout->dac_nids;
5361 for (i = 0; i < mout->num_dacs; i++)
5362 snd_hda_codec_cleanup_stream(codec, nids[i]);
5364 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
5365 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
5366 if (mout->hp_out_nid[i])
5367 snd_hda_codec_cleanup_stream(codec,
5368 mout->hp_out_nid[i]);
5369 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
5370 if (mout->extra_out_nid[i])
5371 snd_hda_codec_cleanup_stream(codec,
5372 mout->extra_out_nid[i]);
5373 mutex_lock(&codec->spdif_mutex);
5374 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
5375 cleanup_dig_out_stream(codec, mout->dig_out_nid);
5376 mout->dig_out_used = 0;
5378 mutex_unlock(&codec->spdif_mutex);
5381 EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_cleanup);
5384 * snd_hda_get_default_vref - Get the default (mic) VREF pin bits
5386 * Guess the suitable VREF pin bits to be set as the pin-control value.
5387 * Note: the function doesn't set the AC_PINCTL_IN_EN bit.
5389 unsigned int snd_hda_get_default_vref(struct hda_codec *codec, hda_nid_t pin)
5391 unsigned int pincap;
5392 unsigned int oldval;
5393 oldval = snd_hda_codec_read(codec, pin, 0,
5394 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
5395 pincap = snd_hda_query_pin_caps(codec, pin);
5396 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
5397 /* Exception: if the default pin setup is vref50, we give it priority */
5398 if ((pincap & AC_PINCAP_VREF_80) && oldval != PIN_VREF50)
5399 return AC_PINCTL_VREF_80;
5400 else if (pincap & AC_PINCAP_VREF_50)
5401 return AC_PINCTL_VREF_50;
5402 else if (pincap & AC_PINCAP_VREF_100)
5403 return AC_PINCTL_VREF_100;
5404 else if (pincap & AC_PINCAP_VREF_GRD)
5405 return AC_PINCTL_VREF_GRD;
5406 return AC_PINCTL_VREF_HIZ;
5408 EXPORT_SYMBOL_HDA(snd_hda_get_default_vref);
5410 /* correct the pin ctl value for matching with the pin cap */
5411 unsigned int snd_hda_correct_pin_ctl(struct hda_codec *codec,
5412 hda_nid_t pin, unsigned int val)
5414 static unsigned int cap_lists[][2] = {
5415 { AC_PINCTL_VREF_100, AC_PINCAP_VREF_100 },
5416 { AC_PINCTL_VREF_80, AC_PINCAP_VREF_80 },
5417 { AC_PINCTL_VREF_50, AC_PINCAP_VREF_50 },
5418 { AC_PINCTL_VREF_GRD, AC_PINCAP_VREF_GRD },
5424 cap = snd_hda_query_pin_caps(codec, pin);
5426 return val; /* don't know what to do... */
5428 if (val & AC_PINCTL_OUT_EN) {
5429 if (!(cap & AC_PINCAP_OUT))
5430 val &= ~(AC_PINCTL_OUT_EN | AC_PINCTL_HP_EN);
5431 else if ((val & AC_PINCTL_HP_EN) && !(cap & AC_PINCAP_HP_DRV))
5432 val &= ~AC_PINCTL_HP_EN;
5435 if (val & AC_PINCTL_IN_EN) {
5436 if (!(cap & AC_PINCAP_IN))
5437 val &= ~(AC_PINCTL_IN_EN | AC_PINCTL_VREFEN);
5439 unsigned int vcap, vref;
5441 vcap = (cap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
5442 vref = val & AC_PINCTL_VREFEN;
5443 for (i = 0; i < ARRAY_SIZE(cap_lists); i++) {
5444 if (vref == cap_lists[i][0] &&
5445 !(vcap & cap_lists[i][1])) {
5446 if (i == ARRAY_SIZE(cap_lists) - 1)
5447 vref = AC_PINCTL_VREF_HIZ;
5449 vref = cap_lists[i + 1][0];
5452 val &= ~AC_PINCTL_VREFEN;
5459 EXPORT_SYMBOL_HDA(snd_hda_correct_pin_ctl);
5461 int _snd_hda_set_pin_ctl(struct hda_codec *codec, hda_nid_t pin,
5462 unsigned int val, bool cached)
5464 val = snd_hda_correct_pin_ctl(codec, pin, val);
5465 snd_hda_codec_set_pin_target(codec, pin, val);
5467 return snd_hda_codec_update_cache(codec, pin, 0,
5468 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
5470 return snd_hda_codec_write(codec, pin, 0,
5471 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
5473 EXPORT_SYMBOL_HDA(_snd_hda_set_pin_ctl);
5476 * snd_hda_add_imux_item - Add an item to input_mux
5478 * When the same label is used already in the existing items, the number
5479 * suffix is appended to the label. This label index number is stored
5480 * to type_idx when non-NULL pointer is given.
5482 int snd_hda_add_imux_item(struct hda_input_mux *imux, const char *label,
5483 int index, int *type_idx)
5485 int i, label_idx = 0;
5486 if (imux->num_items >= HDA_MAX_NUM_INPUTS) {
5487 snd_printd(KERN_ERR "hda_codec: Too many imux items!\n");
5490 for (i = 0; i < imux->num_items; i++) {
5491 if (!strncmp(label, imux->items[i].label, strlen(label)))
5495 *type_idx = label_idx;
5497 snprintf(imux->items[imux->num_items].label,
5498 sizeof(imux->items[imux->num_items].label),
5499 "%s %d", label, label_idx);
5501 strlcpy(imux->items[imux->num_items].label, label,
5502 sizeof(imux->items[imux->num_items].label));
5503 imux->items[imux->num_items].index = index;
5507 EXPORT_SYMBOL_HDA(snd_hda_add_imux_item);
5516 * snd_hda_suspend - suspend the codecs
5519 * Returns 0 if successful.
5521 int snd_hda_suspend(struct hda_bus *bus)
5523 struct hda_codec *codec;
5525 list_for_each_entry(codec, &bus->codec_list, list) {
5526 cancel_delayed_work_sync(&codec->jackpoll_work);
5527 if (hda_codec_is_power_on(codec))
5528 hda_call_codec_suspend(codec, false);
5532 EXPORT_SYMBOL_HDA(snd_hda_suspend);
5535 * snd_hda_resume - resume the codecs
5538 * Returns 0 if successful.
5540 int snd_hda_resume(struct hda_bus *bus)
5542 struct hda_codec *codec;
5544 list_for_each_entry(codec, &bus->codec_list, list) {
5545 hda_call_codec_resume(codec);
5549 EXPORT_SYMBOL_HDA(snd_hda_resume);
5550 #endif /* CONFIG_PM */
5557 * snd_array_new - get a new element from the given array
5558 * @array: the array object
5560 * Get a new element from the given array. If it exceeds the
5561 * pre-allocated array size, re-allocate the array.
5563 * Returns NULL if allocation failed.
5565 void *snd_array_new(struct snd_array *array)
5567 if (snd_BUG_ON(!array->elem_size))
5569 if (array->used >= array->alloced) {
5570 int num = array->alloced + array->alloc_align;
5571 int size = (num + 1) * array->elem_size;
5573 if (snd_BUG_ON(num >= 4096))
5575 nlist = krealloc(array->list, size, GFP_KERNEL | __GFP_ZERO);
5578 array->list = nlist;
5579 array->alloced = num;
5581 return snd_array_elem(array, array->used++);
5583 EXPORT_SYMBOL_HDA(snd_array_new);
5586 * snd_array_free - free the given array elements
5587 * @array: the array object
5589 void snd_array_free(struct snd_array *array)
5596 EXPORT_SYMBOL_HDA(snd_array_free);
5599 * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer
5600 * @pcm: PCM caps bits
5601 * @buf: the string buffer to write
5602 * @buflen: the max buffer length
5604 * used by hda_proc.c and hda_eld.c
5606 void snd_print_pcm_bits(int pcm, char *buf, int buflen)
5608 static unsigned int bits[] = { 8, 16, 20, 24, 32 };
5611 for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
5612 if (pcm & (AC_SUPPCM_BITS_8 << i))
5613 j += snprintf(buf + j, buflen - j, " %d", bits[i]);
5615 buf[j] = '\0'; /* necessary when j == 0 */
5617 EXPORT_SYMBOL_HDA(snd_print_pcm_bits);
5619 MODULE_DESCRIPTION("HDA codec core");
5620 MODULE_LICENSE("GPL");