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", "Digitial 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 direct,
189 unsigned int verb, unsigned int parm)
193 if ((codec->addr & ~0xf) || (direct & ~1) || (nid & ~0x7f) ||
194 (verb & ~0xfff) || (parm & ~0xffff)) {
195 printk(KERN_ERR "hda-codec: out of range cmd %x:%x:%x:%x:%x\n",
196 codec->addr, direct, nid, verb, parm);
200 val = (u32)codec->addr << 28;
201 val |= (u32)direct << 27;
202 val |= (u32)nid << 20;
209 * Send and receive a verb
211 static int codec_exec_verb(struct hda_codec *codec, unsigned int cmd,
214 struct hda_bus *bus = codec->bus;
223 snd_hda_power_up(codec);
224 mutex_lock(&bus->cmd_mutex);
226 trace_hda_send_cmd(codec, cmd);
227 err = bus->ops.command(bus, cmd);
230 /* process pending verbs */
231 bus->ops.get_response(bus, codec->addr);
234 *res = bus->ops.get_response(bus, codec->addr);
235 trace_hda_get_response(codec, *res);
237 mutex_unlock(&bus->cmd_mutex);
238 snd_hda_power_down(codec);
239 if (!codec_in_pm(codec) && res && *res == -1 && bus->rirb_error) {
240 if (bus->response_reset) {
241 snd_printd("hda_codec: resetting BUS due to "
242 "fatal communication error\n");
243 trace_hda_bus_reset(bus);
244 bus->ops.bus_reset(bus);
248 /* clear reset-flag when the communication gets recovered */
249 if (!err || codec_in_pm(codec))
250 bus->response_reset = 0;
255 * snd_hda_codec_read - send a command and get the response
256 * @codec: the HDA codec
257 * @nid: NID to send the command
258 * @direct: direct flag
259 * @verb: the verb to send
260 * @parm: the parameter for the verb
262 * Send a single command and read the corresponding response.
264 * Returns the obtained response value, or -1 for an error.
266 unsigned int snd_hda_codec_read(struct hda_codec *codec, hda_nid_t nid,
268 unsigned int verb, unsigned int parm)
270 unsigned cmd = make_codec_cmd(codec, nid, direct, verb, parm);
272 if (codec_exec_verb(codec, cmd, &res))
276 EXPORT_SYMBOL_HDA(snd_hda_codec_read);
279 * snd_hda_codec_write - send a single command without waiting for response
280 * @codec: the HDA codec
281 * @nid: NID to send the command
282 * @direct: direct flag
283 * @verb: the verb to send
284 * @parm: the parameter for the verb
286 * Send a single command without waiting for response.
288 * Returns 0 if successful, or a negative error code.
290 int snd_hda_codec_write(struct hda_codec *codec, hda_nid_t nid, int direct,
291 unsigned int verb, unsigned int parm)
293 unsigned int cmd = make_codec_cmd(codec, nid, direct, verb, parm);
295 return codec_exec_verb(codec, cmd,
296 codec->bus->sync_write ? &res : NULL);
298 EXPORT_SYMBOL_HDA(snd_hda_codec_write);
301 * snd_hda_sequence_write - sequence writes
302 * @codec: the HDA codec
303 * @seq: VERB array to send
305 * Send the commands sequentially from the given array.
306 * The array must be terminated with NID=0.
308 void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq)
310 for (; seq->nid; seq++)
311 snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param);
313 EXPORT_SYMBOL_HDA(snd_hda_sequence_write);
316 * snd_hda_get_sub_nodes - get the range of sub nodes
317 * @codec: the HDA codec
319 * @start_id: the pointer to store the start NID
321 * Parse the NID and store the start NID of its sub-nodes.
322 * Returns the number of sub-nodes.
324 int snd_hda_get_sub_nodes(struct hda_codec *codec, hda_nid_t nid,
329 parm = snd_hda_param_read(codec, nid, AC_PAR_NODE_COUNT);
332 *start_id = (parm >> 16) & 0x7fff;
333 return (int)(parm & 0x7fff);
335 EXPORT_SYMBOL_HDA(snd_hda_get_sub_nodes);
337 /* connection list element */
338 struct hda_conn_list {
339 struct list_head list;
345 /* look up the cached results */
346 static struct hda_conn_list *
347 lookup_conn_list(struct hda_codec *codec, hda_nid_t nid)
349 struct hda_conn_list *p;
350 list_for_each_entry(p, &codec->conn_list, list) {
357 static int add_conn_list(struct hda_codec *codec, hda_nid_t nid, int len,
358 const hda_nid_t *list)
360 struct hda_conn_list *p;
362 p = kmalloc(sizeof(*p) + len * sizeof(hda_nid_t), GFP_KERNEL);
367 memcpy(p->conns, list, len * sizeof(hda_nid_t));
368 list_add(&p->list, &codec->conn_list);
372 static void remove_conn_list(struct hda_codec *codec)
374 while (!list_empty(&codec->conn_list)) {
375 struct hda_conn_list *p;
376 p = list_first_entry(&codec->conn_list, typeof(*p), list);
382 /* read the connection and add to the cache */
383 static int read_and_add_raw_conns(struct hda_codec *codec, hda_nid_t nid)
385 hda_nid_t list[HDA_MAX_CONNECTIONS];
388 len = snd_hda_get_raw_connections(codec, nid, list, ARRAY_SIZE(list));
391 return snd_hda_override_conn_list(codec, nid, len, list);
395 * snd_hda_get_conn_list - get connection list
396 * @codec: the HDA codec
398 * @len: number of connection list entries
399 * @listp: the pointer to store NID list
401 * Parses the connection list of the given widget and stores the pointer
402 * to the list of NIDs.
404 * Returns the number of connections, or a negative error code.
406 * Note that the returned pointer isn't protected against the list
407 * modification. If snd_hda_override_conn_list() might be called
408 * concurrently, protect with a mutex appropriately.
410 int snd_hda_get_conn_list(struct hda_codec *codec, hda_nid_t nid,
411 const hda_nid_t **listp)
417 const struct hda_conn_list *p;
419 /* if the connection-list is already cached, read it */
420 p = lookup_conn_list(codec, nid);
426 if (snd_BUG_ON(added))
429 err = read_and_add_raw_conns(codec, nid);
435 EXPORT_SYMBOL_HDA(snd_hda_get_conn_list);
438 * snd_hda_get_connections - copy connection list
439 * @codec: the HDA codec
441 * @conn_list: connection list array; when NULL, checks only the size
442 * @max_conns: max. number of connections to store
444 * Parses the connection list of the given widget and stores the list
447 * Returns the number of connections, or a negative error code.
449 int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
450 hda_nid_t *conn_list, int max_conns)
452 const hda_nid_t *list;
453 int len = snd_hda_get_conn_list(codec, nid, &list);
455 if (len > 0 && conn_list) {
456 if (len > max_conns) {
457 snd_printk(KERN_ERR "hda_codec: "
458 "Too many connections %d for NID 0x%x\n",
462 memcpy(conn_list, list, len * sizeof(hda_nid_t));
467 EXPORT_SYMBOL_HDA(snd_hda_get_connections);
470 * snd_hda_get_raw_connections - copy connection list without cache
471 * @codec: the HDA codec
473 * @conn_list: connection list array
474 * @max_conns: max. number of connections to store
476 * Like snd_hda_get_connections(), copy the connection list but without
477 * checking through the connection-list cache.
478 * Currently called only from hda_proc.c, so not exported.
480 int snd_hda_get_raw_connections(struct hda_codec *codec, hda_nid_t nid,
481 hda_nid_t *conn_list, int max_conns)
484 int i, conn_len, conns;
485 unsigned int shift, num_elems, mask;
490 if (snd_BUG_ON(!conn_list || max_conns <= 0))
493 wcaps = get_wcaps(codec, nid);
494 if (!(wcaps & AC_WCAP_CONN_LIST) &&
495 get_wcaps_type(wcaps) != AC_WID_VOL_KNB)
498 parm = snd_hda_param_read(codec, nid, AC_PAR_CONNLIST_LEN);
499 if (parm & AC_CLIST_LONG) {
508 conn_len = parm & AC_CLIST_LENGTH;
509 mask = (1 << (shift-1)) - 1;
512 return 0; /* no connection */
515 /* single connection */
516 parm = snd_hda_codec_read(codec, nid, 0,
517 AC_VERB_GET_CONNECT_LIST, 0);
518 if (parm == -1 && codec->bus->rirb_error)
520 conn_list[0] = parm & mask;
524 /* multi connection */
527 for (i = 0; i < conn_len; i++) {
531 if (i % num_elems == 0) {
532 parm = snd_hda_codec_read(codec, nid, 0,
533 AC_VERB_GET_CONNECT_LIST, i);
534 if (parm == -1 && codec->bus->rirb_error)
537 range_val = !!(parm & (1 << (shift-1))); /* ranges */
539 if (val == 0 && null_count++) { /* no second chance */
540 snd_printk(KERN_WARNING "hda_codec: "
541 "invalid CONNECT_LIST verb %x[%i]:%x\n",
547 /* ranges between the previous and this one */
548 if (!prev_nid || prev_nid >= val) {
549 snd_printk(KERN_WARNING "hda_codec: "
550 "invalid dep_range_val %x:%x\n",
554 for (n = prev_nid + 1; n <= val; n++) {
555 if (conns >= max_conns) {
556 snd_printk(KERN_ERR "hda_codec: "
557 "Too many connections %d for NID 0x%x\n",
561 conn_list[conns++] = n;
564 if (conns >= max_conns) {
565 snd_printk(KERN_ERR "hda_codec: "
566 "Too many connections %d for NID 0x%x\n",
570 conn_list[conns++] = val;
578 * snd_hda_override_conn_list - add/modify the connection-list to cache
579 * @codec: the HDA codec
581 * @len: number of connection list entries
582 * @list: the list of connection entries
584 * Add or modify the given connection-list to the cache. If the corresponding
585 * cache already exists, invalidate it and append a new one.
587 * Returns zero or a negative error code.
589 int snd_hda_override_conn_list(struct hda_codec *codec, hda_nid_t nid, int len,
590 const hda_nid_t *list)
592 struct hda_conn_list *p;
594 p = lookup_conn_list(codec, nid);
600 return add_conn_list(codec, nid, len, list);
602 EXPORT_SYMBOL_HDA(snd_hda_override_conn_list);
605 * snd_hda_get_conn_index - get the connection index of the given NID
606 * @codec: the HDA codec
607 * @mux: NID containing the list
608 * @nid: NID to select
609 * @recursive: 1 when searching NID recursively, otherwise 0
611 * Parses the connection list of the widget @mux and checks whether the
612 * widget @nid is present. If it is, return the connection index.
613 * Otherwise it returns -1.
615 int snd_hda_get_conn_index(struct hda_codec *codec, hda_nid_t mux,
616 hda_nid_t nid, int recursive)
618 const hda_nid_t *conn;
621 nums = snd_hda_get_conn_list(codec, mux, &conn);
622 for (i = 0; i < nums; i++)
627 if (recursive > 10) {
628 snd_printd("hda_codec: too deep connection for 0x%x\n", nid);
632 for (i = 0; i < nums; i++) {
633 unsigned int type = get_wcaps_type(get_wcaps(codec, conn[i]));
634 if (type == AC_WID_PIN || type == AC_WID_AUD_OUT)
636 if (snd_hda_get_conn_index(codec, conn[i], nid, recursive) >= 0)
641 EXPORT_SYMBOL_HDA(snd_hda_get_conn_index);
644 * snd_hda_queue_unsol_event - add an unsolicited event to queue
646 * @res: unsolicited event (lower 32bit of RIRB entry)
647 * @res_ex: codec addr and flags (upper 32bit or RIRB entry)
649 * Adds the given event to the queue. The events are processed in
650 * the workqueue asynchronously. Call this function in the interrupt
651 * hanlder when RIRB receives an unsolicited event.
653 * Returns 0 if successful, or a negative error code.
655 int snd_hda_queue_unsol_event(struct hda_bus *bus, u32 res, u32 res_ex)
657 struct hda_bus_unsolicited *unsol;
660 trace_hda_unsol_event(bus, res, res_ex);
665 wp = (unsol->wp + 1) % HDA_UNSOL_QUEUE_SIZE;
669 unsol->queue[wp] = res;
670 unsol->queue[wp + 1] = res_ex;
672 queue_work(bus->workq, &unsol->work);
676 EXPORT_SYMBOL_HDA(snd_hda_queue_unsol_event);
679 * process queued unsolicited events
681 static void process_unsol_events(struct work_struct *work)
683 struct hda_bus_unsolicited *unsol =
684 container_of(work, struct hda_bus_unsolicited, work);
685 struct hda_bus *bus = unsol->bus;
686 struct hda_codec *codec;
687 unsigned int rp, caddr, res;
689 while (unsol->rp != unsol->wp) {
690 rp = (unsol->rp + 1) % HDA_UNSOL_QUEUE_SIZE;
693 res = unsol->queue[rp];
694 caddr = unsol->queue[rp + 1];
695 if (!(caddr & (1 << 4))) /* no unsolicited event? */
697 codec = bus->caddr_tbl[caddr & 0x0f];
698 if (codec && codec->patch_ops.unsol_event)
699 codec->patch_ops.unsol_event(codec, res);
704 * initialize unsolicited queue
706 static int init_unsol_queue(struct hda_bus *bus)
708 struct hda_bus_unsolicited *unsol;
710 if (bus->unsol) /* already initialized */
713 unsol = kzalloc(sizeof(*unsol), GFP_KERNEL);
715 snd_printk(KERN_ERR "hda_codec: "
716 "can't allocate unsolicited queue\n");
719 INIT_WORK(&unsol->work, process_unsol_events);
728 static void snd_hda_codec_free(struct hda_codec *codec);
730 static int snd_hda_bus_free(struct hda_bus *bus)
732 struct hda_codec *codec, *n;
737 flush_workqueue(bus->workq);
740 list_for_each_entry_safe(codec, n, &bus->codec_list, list) {
741 snd_hda_codec_free(codec);
743 if (bus->ops.private_free)
744 bus->ops.private_free(bus);
746 destroy_workqueue(bus->workq);
751 static int snd_hda_bus_dev_free(struct snd_device *device)
753 struct hda_bus *bus = device->device_data;
755 return snd_hda_bus_free(bus);
758 #ifdef CONFIG_SND_HDA_HWDEP
759 static int snd_hda_bus_dev_register(struct snd_device *device)
761 struct hda_bus *bus = device->device_data;
762 struct hda_codec *codec;
763 list_for_each_entry(codec, &bus->codec_list, list) {
764 snd_hda_hwdep_add_sysfs(codec);
765 snd_hda_hwdep_add_power_sysfs(codec);
770 #define snd_hda_bus_dev_register NULL
774 * snd_hda_bus_new - create a HDA bus
775 * @card: the card entry
776 * @temp: the template for hda_bus information
777 * @busp: the pointer to store the created bus instance
779 * Returns 0 if successful, or a negative error code.
781 int snd_hda_bus_new(struct snd_card *card,
782 const struct hda_bus_template *temp,
783 struct hda_bus **busp)
787 static struct snd_device_ops dev_ops = {
788 .dev_register = snd_hda_bus_dev_register,
789 .dev_free = snd_hda_bus_dev_free,
792 if (snd_BUG_ON(!temp))
794 if (snd_BUG_ON(!temp->ops.command || !temp->ops.get_response))
800 bus = kzalloc(sizeof(*bus), GFP_KERNEL);
802 snd_printk(KERN_ERR "can't allocate struct hda_bus\n");
807 bus->private_data = temp->private_data;
808 bus->pci = temp->pci;
809 bus->modelname = temp->modelname;
810 bus->power_save = temp->power_save;
811 bus->ops = temp->ops;
813 mutex_init(&bus->cmd_mutex);
814 mutex_init(&bus->prepare_mutex);
815 INIT_LIST_HEAD(&bus->codec_list);
817 snprintf(bus->workq_name, sizeof(bus->workq_name),
818 "hd-audio%d", card->number);
819 bus->workq = create_singlethread_workqueue(bus->workq_name);
821 snd_printk(KERN_ERR "cannot create workqueue %s\n",
827 err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops);
829 snd_hda_bus_free(bus);
836 EXPORT_SYMBOL_HDA(snd_hda_bus_new);
838 #ifdef CONFIG_SND_HDA_GENERIC
839 #define is_generic_config(codec) \
840 (codec->modelname && !strcmp(codec->modelname, "generic"))
842 #define is_generic_config(codec) 0
846 #define HDA_MODREQ_MAX_COUNT 2 /* two request_modules()'s */
848 #define HDA_MODREQ_MAX_COUNT 0 /* all presets are statically linked */
852 * find a matching codec preset
854 static const struct hda_codec_preset *
855 find_codec_preset(struct hda_codec *codec)
857 struct hda_codec_preset_list *tbl;
858 const struct hda_codec_preset *preset;
859 unsigned int mod_requested = 0;
861 if (is_generic_config(codec))
862 return NULL; /* use the generic parser */
865 mutex_lock(&preset_mutex);
866 list_for_each_entry(tbl, &hda_preset_tables, list) {
867 if (!try_module_get(tbl->owner)) {
868 snd_printk(KERN_ERR "hda_codec: cannot module_get\n");
871 for (preset = tbl->preset; preset->id; preset++) {
872 u32 mask = preset->mask;
873 if (preset->afg && preset->afg != codec->afg)
875 if (preset->mfg && preset->mfg != codec->mfg)
879 if (preset->id == (codec->vendor_id & mask) &&
881 preset->rev == codec->revision_id)) {
882 mutex_unlock(&preset_mutex);
883 codec->owner = tbl->owner;
887 module_put(tbl->owner);
889 mutex_unlock(&preset_mutex);
891 if (mod_requested < HDA_MODREQ_MAX_COUNT) {
894 snprintf(name, sizeof(name), "snd-hda-codec-id:%08x",
897 snprintf(name, sizeof(name), "snd-hda-codec-id:%04x*",
898 (codec->vendor_id >> 16) & 0xffff);
899 request_module(name);
907 * get_codec_name - store the codec name
909 static int get_codec_name(struct hda_codec *codec)
911 const struct hda_vendor_id *c;
912 const char *vendor = NULL;
913 u16 vendor_id = codec->vendor_id >> 16;
916 if (codec->vendor_name)
919 for (c = hda_vendor_ids; c->id; c++) {
920 if (c->id == vendor_id) {
926 sprintf(tmp, "Generic %04x", vendor_id);
929 codec->vendor_name = kstrdup(vendor, GFP_KERNEL);
930 if (!codec->vendor_name)
934 if (codec->chip_name)
937 if (codec->preset && codec->preset->name)
938 codec->chip_name = kstrdup(codec->preset->name, GFP_KERNEL);
940 sprintf(tmp, "ID %x", codec->vendor_id & 0xffff);
941 codec->chip_name = kstrdup(tmp, GFP_KERNEL);
943 if (!codec->chip_name)
949 * look for an AFG and MFG nodes
951 static void setup_fg_nodes(struct hda_codec *codec)
953 int i, total_nodes, function_id;
956 total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid);
957 for (i = 0; i < total_nodes; i++, nid++) {
958 function_id = snd_hda_param_read(codec, nid,
959 AC_PAR_FUNCTION_TYPE);
960 switch (function_id & 0xff) {
961 case AC_GRP_AUDIO_FUNCTION:
963 codec->afg_function_id = function_id & 0xff;
964 codec->afg_unsol = (function_id >> 8) & 1;
966 case AC_GRP_MODEM_FUNCTION:
968 codec->mfg_function_id = function_id & 0xff;
969 codec->mfg_unsol = (function_id >> 8) & 1;
978 * read widget caps for each widget and store in cache
980 static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
985 codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node,
987 codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL);
990 nid = codec->start_nid;
991 for (i = 0; i < codec->num_nodes; i++, nid++)
992 codec->wcaps[i] = snd_hda_param_read(codec, nid,
993 AC_PAR_AUDIO_WIDGET_CAP);
997 /* read all pin default configurations and save codec->init_pins */
998 static int read_pin_defaults(struct hda_codec *codec)
1001 hda_nid_t nid = codec->start_nid;
1003 for (i = 0; i < codec->num_nodes; i++, nid++) {
1004 struct hda_pincfg *pin;
1005 unsigned int wcaps = get_wcaps(codec, nid);
1006 unsigned int wid_type = get_wcaps_type(wcaps);
1007 if (wid_type != AC_WID_PIN)
1009 pin = snd_array_new(&codec->init_pins);
1013 pin->cfg = snd_hda_codec_read(codec, nid, 0,
1014 AC_VERB_GET_CONFIG_DEFAULT, 0);
1015 pin->ctrl = snd_hda_codec_read(codec, nid, 0,
1016 AC_VERB_GET_PIN_WIDGET_CONTROL,
1022 /* look up the given pin config list and return the item matching with NID */
1023 static struct hda_pincfg *look_up_pincfg(struct hda_codec *codec,
1024 struct snd_array *array,
1028 for (i = 0; i < array->used; i++) {
1029 struct hda_pincfg *pin = snd_array_elem(array, i);
1030 if (pin->nid == nid)
1036 /* set the current pin config value for the given NID.
1037 * the value is cached, and read via snd_hda_codec_get_pincfg()
1039 int snd_hda_add_pincfg(struct hda_codec *codec, struct snd_array *list,
1040 hda_nid_t nid, unsigned int cfg)
1042 struct hda_pincfg *pin;
1044 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN)
1047 pin = look_up_pincfg(codec, list, nid);
1049 pin = snd_array_new(list);
1059 * snd_hda_codec_set_pincfg - Override a pin default configuration
1060 * @codec: the HDA codec
1061 * @nid: NID to set the pin config
1062 * @cfg: the pin default config value
1064 * Override a pin default configuration value in the cache.
1065 * This value can be read by snd_hda_codec_get_pincfg() in a higher
1066 * priority than the real hardware value.
1068 int snd_hda_codec_set_pincfg(struct hda_codec *codec,
1069 hda_nid_t nid, unsigned int cfg)
1071 return snd_hda_add_pincfg(codec, &codec->driver_pins, nid, cfg);
1073 EXPORT_SYMBOL_HDA(snd_hda_codec_set_pincfg);
1076 * snd_hda_codec_get_pincfg - Obtain a pin-default configuration
1077 * @codec: the HDA codec
1078 * @nid: NID to get the pin config
1080 * Get the current pin config value of the given pin NID.
1081 * If the pincfg value is cached or overridden via sysfs or driver,
1082 * returns the cached value.
1084 unsigned int snd_hda_codec_get_pincfg(struct hda_codec *codec, hda_nid_t nid)
1086 struct hda_pincfg *pin;
1088 #ifdef CONFIG_SND_HDA_HWDEP
1090 unsigned int cfg = 0;
1091 mutex_lock(&codec->user_mutex);
1092 pin = look_up_pincfg(codec, &codec->user_pins, nid);
1095 mutex_unlock(&codec->user_mutex);
1100 pin = look_up_pincfg(codec, &codec->driver_pins, nid);
1103 pin = look_up_pincfg(codec, &codec->init_pins, nid);
1108 EXPORT_SYMBOL_HDA(snd_hda_codec_get_pincfg);
1110 /* remember the current pinctl target value */
1111 int snd_hda_codec_set_pin_target(struct hda_codec *codec, hda_nid_t nid,
1114 struct hda_pincfg *pin;
1116 pin = look_up_pincfg(codec, &codec->init_pins, nid);
1122 EXPORT_SYMBOL_HDA(snd_hda_codec_set_pin_target);
1124 /* return the current pinctl target value */
1125 int snd_hda_codec_get_pin_target(struct hda_codec *codec, hda_nid_t nid)
1127 struct hda_pincfg *pin;
1129 pin = look_up_pincfg(codec, &codec->init_pins, nid);
1134 EXPORT_SYMBOL_HDA(snd_hda_codec_get_pin_target);
1137 * snd_hda_shutup_pins - Shut up all pins
1138 * @codec: the HDA codec
1140 * Clear all pin controls to shup up before suspend for avoiding click noise.
1141 * The controls aren't cached so that they can be resumed properly.
1143 void snd_hda_shutup_pins(struct hda_codec *codec)
1146 /* don't shut up pins when unloading the driver; otherwise it breaks
1147 * the default pin setup at the next load of the driver
1149 if (codec->bus->shutdown)
1151 for (i = 0; i < codec->init_pins.used; i++) {
1152 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1153 /* use read here for syncing after issuing each verb */
1154 snd_hda_codec_read(codec, pin->nid, 0,
1155 AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
1157 codec->pins_shutup = 1;
1159 EXPORT_SYMBOL_HDA(snd_hda_shutup_pins);
1162 /* Restore the pin controls cleared previously via snd_hda_shutup_pins() */
1163 static void restore_shutup_pins(struct hda_codec *codec)
1166 if (!codec->pins_shutup)
1168 if (codec->bus->shutdown)
1170 for (i = 0; i < codec->init_pins.used; i++) {
1171 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1172 snd_hda_codec_write(codec, pin->nid, 0,
1173 AC_VERB_SET_PIN_WIDGET_CONTROL,
1176 codec->pins_shutup = 0;
1180 static void hda_jackpoll_work(struct work_struct *work)
1182 struct hda_codec *codec =
1183 container_of(work, struct hda_codec, jackpoll_work.work);
1184 if (!codec->jackpoll_interval)
1187 snd_hda_jack_set_dirty_all(codec);
1188 snd_hda_jack_poll_all(codec);
1189 queue_delayed_work(codec->bus->workq, &codec->jackpoll_work,
1190 codec->jackpoll_interval);
1193 static void init_hda_cache(struct hda_cache_rec *cache,
1194 unsigned int record_size);
1195 static void free_hda_cache(struct hda_cache_rec *cache);
1197 /* release all pincfg lists */
1198 static void free_init_pincfgs(struct hda_codec *codec)
1200 snd_array_free(&codec->driver_pins);
1201 #ifdef CONFIG_SND_HDA_HWDEP
1202 snd_array_free(&codec->user_pins);
1204 snd_array_free(&codec->init_pins);
1208 * audio-converter setup caches
1210 struct hda_cvt_setup {
1215 unsigned char active; /* cvt is currently used */
1216 unsigned char dirty; /* setups should be cleared */
1219 /* get or create a cache entry for the given audio converter NID */
1220 static struct hda_cvt_setup *
1221 get_hda_cvt_setup(struct hda_codec *codec, hda_nid_t nid)
1223 struct hda_cvt_setup *p;
1226 for (i = 0; i < codec->cvt_setups.used; i++) {
1227 p = snd_array_elem(&codec->cvt_setups, i);
1231 p = snd_array_new(&codec->cvt_setups);
1240 static void snd_hda_codec_free(struct hda_codec *codec)
1244 cancel_delayed_work_sync(&codec->jackpoll_work);
1245 snd_hda_jack_tbl_clear(codec);
1246 free_init_pincfgs(codec);
1248 cancel_delayed_work(&codec->power_work);
1249 flush_workqueue(codec->bus->workq);
1251 list_del(&codec->list);
1252 snd_array_free(&codec->mixers);
1253 snd_array_free(&codec->nids);
1254 snd_array_free(&codec->cvt_setups);
1255 snd_array_free(&codec->spdif_out);
1256 remove_conn_list(codec);
1257 codec->bus->caddr_tbl[codec->addr] = NULL;
1258 if (codec->patch_ops.free)
1259 codec->patch_ops.free(codec);
1261 if (!codec->pm_down_notified) /* cancel leftover refcounts */
1262 hda_call_pm_notify(codec->bus, false);
1264 module_put(codec->owner);
1265 free_hda_cache(&codec->amp_cache);
1266 free_hda_cache(&codec->cmd_cache);
1267 kfree(codec->vendor_name);
1268 kfree(codec->chip_name);
1269 kfree(codec->modelname);
1270 kfree(codec->wcaps);
1274 static bool snd_hda_codec_get_supported_ps(struct hda_codec *codec,
1275 hda_nid_t fg, unsigned int power_state);
1277 static unsigned int hda_set_power_state(struct hda_codec *codec,
1278 unsigned int power_state);
1281 * snd_hda_codec_new - create a HDA codec
1282 * @bus: the bus to assign
1283 * @codec_addr: the codec address
1284 * @codecp: the pointer to store the generated codec
1286 * Returns 0 if successful, or a negative error code.
1288 int snd_hda_codec_new(struct hda_bus *bus,
1289 unsigned int codec_addr,
1290 struct hda_codec **codecp)
1292 struct hda_codec *codec;
1297 if (snd_BUG_ON(!bus))
1299 if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS))
1302 if (bus->caddr_tbl[codec_addr]) {
1303 snd_printk(KERN_ERR "hda_codec: "
1304 "address 0x%x is already occupied\n", codec_addr);
1308 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
1309 if (codec == NULL) {
1310 snd_printk(KERN_ERR "can't allocate struct hda_codec\n");
1315 codec->addr = codec_addr;
1316 mutex_init(&codec->spdif_mutex);
1317 mutex_init(&codec->control_mutex);
1318 mutex_init(&codec->hash_mutex);
1319 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
1320 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
1321 snd_array_init(&codec->mixers, sizeof(struct hda_nid_item), 32);
1322 snd_array_init(&codec->nids, sizeof(struct hda_nid_item), 32);
1323 snd_array_init(&codec->init_pins, sizeof(struct hda_pincfg), 16);
1324 snd_array_init(&codec->driver_pins, sizeof(struct hda_pincfg), 16);
1325 snd_array_init(&codec->cvt_setups, sizeof(struct hda_cvt_setup), 8);
1326 snd_array_init(&codec->spdif_out, sizeof(struct hda_spdif_out), 16);
1327 snd_array_init(&codec->jacktbl, sizeof(struct hda_jack_tbl), 16);
1328 snd_array_init(&codec->verbs, sizeof(struct hda_verb *), 8);
1329 INIT_LIST_HEAD(&codec->conn_list);
1331 INIT_DELAYED_WORK(&codec->jackpoll_work, hda_jackpoll_work);
1334 spin_lock_init(&codec->power_lock);
1335 INIT_DELAYED_WORK(&codec->power_work, hda_power_work);
1336 /* snd_hda_codec_new() marks the codec as power-up, and leave it as is.
1337 * the caller has to power down appropriatley after initialization
1340 hda_keep_power_on(codec);
1341 hda_call_pm_notify(bus, true);
1344 if (codec->bus->modelname) {
1345 codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL);
1346 if (!codec->modelname) {
1347 snd_hda_codec_free(codec);
1352 list_add_tail(&codec->list, &bus->codec_list);
1353 bus->caddr_tbl[codec_addr] = codec;
1355 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1357 if (codec->vendor_id == -1)
1358 /* read again, hopefully the access method was corrected
1359 * in the last read...
1361 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1363 codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1364 AC_PAR_SUBSYSTEM_ID);
1365 codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1368 setup_fg_nodes(codec);
1369 if (!codec->afg && !codec->mfg) {
1370 snd_printdd("hda_codec: no AFG or MFG node found\n");
1375 fg = codec->afg ? codec->afg : codec->mfg;
1376 err = read_widget_caps(codec, fg);
1378 snd_printk(KERN_ERR "hda_codec: cannot malloc\n");
1381 err = read_pin_defaults(codec);
1385 if (!codec->subsystem_id) {
1386 codec->subsystem_id =
1387 snd_hda_codec_read(codec, fg, 0,
1388 AC_VERB_GET_SUBSYSTEM_ID, 0);
1392 codec->d3_stop_clk = snd_hda_codec_get_supported_ps(codec, fg,
1394 if (!codec->d3_stop_clk)
1395 bus->power_keep_link_on = 1;
1397 codec->epss = snd_hda_codec_get_supported_ps(codec, fg,
1400 /* power-up all before initialization */
1401 hda_set_power_state(codec, AC_PWRST_D0);
1403 snd_hda_codec_proc_new(codec);
1405 snd_hda_create_hwdep(codec);
1407 sprintf(component, "HDA:%08x,%08x,%08x", codec->vendor_id,
1408 codec->subsystem_id, codec->revision_id);
1409 snd_component_add(codec->bus->card, component);
1416 snd_hda_codec_free(codec);
1419 EXPORT_SYMBOL_HDA(snd_hda_codec_new);
1422 * snd_hda_codec_configure - (Re-)configure the HD-audio codec
1423 * @codec: the HDA codec
1425 * Start parsing of the given codec tree and (re-)initialize the whole
1428 * Returns 0 if successful or a negative error code.
1430 int snd_hda_codec_configure(struct hda_codec *codec)
1434 codec->preset = find_codec_preset(codec);
1435 if (!codec->vendor_name || !codec->chip_name) {
1436 err = get_codec_name(codec);
1441 if (is_generic_config(codec)) {
1442 err = snd_hda_parse_generic_codec(codec);
1445 if (codec->preset && codec->preset->patch) {
1446 err = codec->preset->patch(codec);
1450 /* call the default parser */
1451 err = snd_hda_parse_generic_codec(codec);
1453 printk(KERN_ERR "hda-codec: No codec parser is available\n");
1456 if (!err && codec->patch_ops.unsol_event)
1457 err = init_unsol_queue(codec->bus);
1458 /* audio codec should override the mixer name */
1459 if (!err && (codec->afg || !*codec->bus->card->mixername))
1460 snprintf(codec->bus->card->mixername,
1461 sizeof(codec->bus->card->mixername),
1462 "%s %s", codec->vendor_name, codec->chip_name);
1465 EXPORT_SYMBOL_HDA(snd_hda_codec_configure);
1467 /* update the stream-id if changed */
1468 static void update_pcm_stream_id(struct hda_codec *codec,
1469 struct hda_cvt_setup *p, hda_nid_t nid,
1470 u32 stream_tag, int channel_id)
1472 unsigned int oldval, newval;
1474 if (p->stream_tag != stream_tag || p->channel_id != channel_id) {
1475 oldval = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0);
1476 newval = (stream_tag << 4) | channel_id;
1477 if (oldval != newval)
1478 snd_hda_codec_write(codec, nid, 0,
1479 AC_VERB_SET_CHANNEL_STREAMID,
1481 p->stream_tag = stream_tag;
1482 p->channel_id = channel_id;
1486 /* update the format-id if changed */
1487 static void update_pcm_format(struct hda_codec *codec, struct hda_cvt_setup *p,
1488 hda_nid_t nid, int format)
1490 unsigned int oldval;
1492 if (p->format_id != format) {
1493 oldval = snd_hda_codec_read(codec, nid, 0,
1494 AC_VERB_GET_STREAM_FORMAT, 0);
1495 if (oldval != format) {
1497 snd_hda_codec_write(codec, nid, 0,
1498 AC_VERB_SET_STREAM_FORMAT,
1501 p->format_id = format;
1506 * snd_hda_codec_setup_stream - set up the codec for streaming
1507 * @codec: the CODEC to set up
1508 * @nid: the NID to set up
1509 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
1510 * @channel_id: channel id to pass, zero based.
1511 * @format: stream format.
1513 void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
1515 int channel_id, int format)
1517 struct hda_codec *c;
1518 struct hda_cvt_setup *p;
1525 snd_printdd("hda_codec_setup_stream: "
1526 "NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
1527 nid, stream_tag, channel_id, format);
1528 p = get_hda_cvt_setup(codec, nid);
1529 if (!p || p->active)
1532 if (codec->pcm_format_first)
1533 update_pcm_format(codec, p, nid, format);
1534 update_pcm_stream_id(codec, p, nid, stream_tag, channel_id);
1535 if (!codec->pcm_format_first)
1536 update_pcm_format(codec, p, nid, format);
1541 /* make other inactive cvts with the same stream-tag dirty */
1542 type = get_wcaps_type(get_wcaps(codec, nid));
1543 list_for_each_entry(c, &codec->bus->codec_list, list) {
1544 for (i = 0; i < c->cvt_setups.used; i++) {
1545 p = snd_array_elem(&c->cvt_setups, i);
1546 if (!p->active && p->stream_tag == stream_tag &&
1547 get_wcaps_type(get_wcaps(c, p->nid)) == type)
1552 EXPORT_SYMBOL_HDA(snd_hda_codec_setup_stream);
1554 static void really_cleanup_stream(struct hda_codec *codec,
1555 struct hda_cvt_setup *q);
1558 * __snd_hda_codec_cleanup_stream - clean up the codec for closing
1559 * @codec: the CODEC to clean up
1560 * @nid: the NID to clean up
1561 * @do_now: really clean up the stream instead of clearing the active flag
1563 void __snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid,
1566 struct hda_cvt_setup *p;
1571 if (codec->no_sticky_stream)
1574 snd_printdd("hda_codec_cleanup_stream: NID=0x%x\n", nid);
1575 p = get_hda_cvt_setup(codec, nid);
1576 if (p && p->active) {
1577 /* here we just clear the active flag when do_now isn't set;
1578 * actual clean-ups will be done later in
1579 * purify_inactive_streams() called from snd_hda_codec_prpapre()
1582 really_cleanup_stream(codec, p);
1587 EXPORT_SYMBOL_HDA(__snd_hda_codec_cleanup_stream);
1589 static void really_cleanup_stream(struct hda_codec *codec,
1590 struct hda_cvt_setup *q)
1592 hda_nid_t nid = q->nid;
1593 if (q->stream_tag || q->channel_id)
1594 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
1596 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0
1598 memset(q, 0, sizeof(*q));
1602 /* clean up the all conflicting obsolete streams */
1603 static void purify_inactive_streams(struct hda_codec *codec)
1605 struct hda_codec *c;
1608 list_for_each_entry(c, &codec->bus->codec_list, list) {
1609 for (i = 0; i < c->cvt_setups.used; i++) {
1610 struct hda_cvt_setup *p;
1611 p = snd_array_elem(&c->cvt_setups, i);
1613 really_cleanup_stream(c, p);
1619 /* clean up all streams; called from suspend */
1620 static void hda_cleanup_all_streams(struct hda_codec *codec)
1624 for (i = 0; i < codec->cvt_setups.used; i++) {
1625 struct hda_cvt_setup *p = snd_array_elem(&codec->cvt_setups, i);
1627 really_cleanup_stream(codec, p);
1633 * amp access functions
1636 /* FIXME: more better hash key? */
1637 #define HDA_HASH_KEY(nid, dir, idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
1638 #define HDA_HASH_PINCAP_KEY(nid) (u32)((nid) + (0x02 << 24))
1639 #define HDA_HASH_PARPCM_KEY(nid) (u32)((nid) + (0x03 << 24))
1640 #define HDA_HASH_PARSTR_KEY(nid) (u32)((nid) + (0x04 << 24))
1641 #define INFO_AMP_CAPS (1<<0)
1642 #define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
1644 /* initialize the hash table */
1645 static void init_hda_cache(struct hda_cache_rec *cache,
1646 unsigned int record_size)
1648 memset(cache, 0, sizeof(*cache));
1649 memset(cache->hash, 0xff, sizeof(cache->hash));
1650 snd_array_init(&cache->buf, record_size, 64);
1653 static void free_hda_cache(struct hda_cache_rec *cache)
1655 snd_array_free(&cache->buf);
1658 /* query the hash. allocate an entry if not found. */
1659 static struct hda_cache_head *get_hash(struct hda_cache_rec *cache, u32 key)
1661 u16 idx = key % (u16)ARRAY_SIZE(cache->hash);
1662 u16 cur = cache->hash[idx];
1663 struct hda_cache_head *info;
1665 while (cur != 0xffff) {
1666 info = snd_array_elem(&cache->buf, cur);
1667 if (info->key == key)
1674 /* query the hash. allocate an entry if not found. */
1675 static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache,
1678 struct hda_cache_head *info = get_hash(cache, key);
1681 /* add a new hash entry */
1682 info = snd_array_new(&cache->buf);
1685 cur = snd_array_index(&cache->buf, info);
1689 idx = key % (u16)ARRAY_SIZE(cache->hash);
1690 info->next = cache->hash[idx];
1691 cache->hash[idx] = cur;
1696 /* query and allocate an amp hash entry */
1697 static inline struct hda_amp_info *
1698 get_alloc_amp_hash(struct hda_codec *codec, u32 key)
1700 return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key);
1703 /* overwrite the value with the key in the caps hash */
1704 static int write_caps_hash(struct hda_codec *codec, u32 key, unsigned int val)
1706 struct hda_amp_info *info;
1708 mutex_lock(&codec->hash_mutex);
1709 info = get_alloc_amp_hash(codec, key);
1711 mutex_unlock(&codec->hash_mutex);
1714 info->amp_caps = val;
1715 info->head.val |= INFO_AMP_CAPS;
1716 mutex_unlock(&codec->hash_mutex);
1720 /* query the value from the caps hash; if not found, fetch the current
1721 * value from the given function and store in the hash
1724 query_caps_hash(struct hda_codec *codec, hda_nid_t nid, int dir, u32 key,
1725 unsigned int (*func)(struct hda_codec *, hda_nid_t, int))
1727 struct hda_amp_info *info;
1730 mutex_lock(&codec->hash_mutex);
1731 info = get_alloc_amp_hash(codec, key);
1733 mutex_unlock(&codec->hash_mutex);
1736 if (!(info->head.val & INFO_AMP_CAPS)) {
1737 mutex_unlock(&codec->hash_mutex); /* for reentrance */
1738 val = func(codec, nid, dir);
1739 write_caps_hash(codec, key, val);
1741 val = info->amp_caps;
1742 mutex_unlock(&codec->hash_mutex);
1747 static unsigned int read_amp_cap(struct hda_codec *codec, hda_nid_t nid,
1750 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
1752 return snd_hda_param_read(codec, nid,
1753 direction == HDA_OUTPUT ?
1754 AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP);
1758 * query_amp_caps - query AMP capabilities
1759 * @codec: the HD-auio codec
1760 * @nid: the NID to query
1761 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1763 * Query AMP capabilities for the given widget and direction.
1764 * Returns the obtained capability bits.
1766 * When cap bits have been already read, this doesn't read again but
1767 * returns the cached value.
1769 u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
1771 return query_caps_hash(codec, nid, direction,
1772 HDA_HASH_KEY(nid, direction, 0),
1775 EXPORT_SYMBOL_HDA(query_amp_caps);
1778 * snd_hda_override_amp_caps - Override the AMP capabilities
1779 * @codec: the CODEC to clean up
1780 * @nid: the NID to clean up
1781 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1782 * @caps: the capability bits to set
1784 * Override the cached AMP caps bits value by the given one.
1785 * This function is useful if the driver needs to adjust the AMP ranges,
1786 * e.g. limit to 0dB, etc.
1788 * Returns zero if successful or a negative error code.
1790 int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
1793 return write_caps_hash(codec, HDA_HASH_KEY(nid, dir, 0), caps);
1795 EXPORT_SYMBOL_HDA(snd_hda_override_amp_caps);
1797 static unsigned int read_pin_cap(struct hda_codec *codec, hda_nid_t nid,
1800 return snd_hda_param_read(codec, nid, AC_PAR_PIN_CAP);
1804 * snd_hda_query_pin_caps - Query PIN capabilities
1805 * @codec: the HD-auio codec
1806 * @nid: the NID to query
1808 * Query PIN capabilities for the given widget.
1809 * Returns the obtained capability bits.
1811 * When cap bits have been already read, this doesn't read again but
1812 * returns the cached value.
1814 u32 snd_hda_query_pin_caps(struct hda_codec *codec, hda_nid_t nid)
1816 return query_caps_hash(codec, nid, 0, HDA_HASH_PINCAP_KEY(nid),
1819 EXPORT_SYMBOL_HDA(snd_hda_query_pin_caps);
1822 * snd_hda_override_pin_caps - Override the pin capabilities
1824 * @nid: the NID to override
1825 * @caps: the capability bits to set
1827 * Override the cached PIN capabilitiy bits value by the given one.
1829 * Returns zero if successful or a negative error code.
1831 int snd_hda_override_pin_caps(struct hda_codec *codec, hda_nid_t nid,
1834 return write_caps_hash(codec, HDA_HASH_PINCAP_KEY(nid), caps);
1836 EXPORT_SYMBOL_HDA(snd_hda_override_pin_caps);
1838 /* read or sync the hash value with the current value;
1839 * call within hash_mutex
1841 static struct hda_amp_info *
1842 update_amp_hash(struct hda_codec *codec, hda_nid_t nid, int ch,
1843 int direction, int index, bool init_only)
1845 struct hda_amp_info *info;
1846 unsigned int parm, val = 0;
1847 bool val_read = false;
1850 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
1853 if (!(info->head.val & INFO_AMP_VOL(ch))) {
1855 mutex_unlock(&codec->hash_mutex);
1856 parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
1857 parm |= direction == HDA_OUTPUT ?
1858 AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
1860 val = snd_hda_codec_read(codec, nid, 0,
1861 AC_VERB_GET_AMP_GAIN_MUTE, parm);
1864 mutex_lock(&codec->hash_mutex);
1867 info->vol[ch] = val;
1868 info->head.val |= INFO_AMP_VOL(ch);
1869 } else if (init_only)
1875 * write the current volume in info to the h/w
1877 static void put_vol_mute(struct hda_codec *codec, unsigned int amp_caps,
1878 hda_nid_t nid, int ch, int direction, int index,
1883 parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
1884 parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
1885 parm |= index << AC_AMP_SET_INDEX_SHIFT;
1886 if ((val & HDA_AMP_MUTE) && !(amp_caps & AC_AMPCAP_MUTE) &&
1887 (amp_caps & AC_AMPCAP_MIN_MUTE))
1888 ; /* set the zero value as a fake mute */
1891 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
1895 * snd_hda_codec_amp_read - Read AMP value
1896 * @codec: HD-audio codec
1897 * @nid: NID to read the AMP value
1898 * @ch: channel (left=0 or right=1)
1899 * @direction: #HDA_INPUT or #HDA_OUTPUT
1900 * @index: the index value (only for input direction)
1902 * Read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
1904 int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
1905 int direction, int index)
1907 struct hda_amp_info *info;
1908 unsigned int val = 0;
1910 mutex_lock(&codec->hash_mutex);
1911 info = update_amp_hash(codec, nid, ch, direction, index, false);
1913 val = info->vol[ch];
1914 mutex_unlock(&codec->hash_mutex);
1917 EXPORT_SYMBOL_HDA(snd_hda_codec_amp_read);
1919 static int codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
1920 int direction, int idx, int mask, int val,
1923 struct hda_amp_info *info;
1925 unsigned int cache_only;
1927 if (snd_BUG_ON(mask & ~0xff))
1931 mutex_lock(&codec->hash_mutex);
1932 info = update_amp_hash(codec, nid, ch, direction, idx, init_only);
1934 mutex_unlock(&codec->hash_mutex);
1937 val |= info->vol[ch] & ~mask;
1938 if (info->vol[ch] == val) {
1939 mutex_unlock(&codec->hash_mutex);
1942 info->vol[ch] = val;
1943 cache_only = info->head.dirty = codec->cached_write;
1944 caps = info->amp_caps;
1945 mutex_unlock(&codec->hash_mutex);
1947 put_vol_mute(codec, caps, nid, ch, direction, idx, val);
1952 * snd_hda_codec_amp_update - update the AMP value
1953 * @codec: HD-audio codec
1954 * @nid: NID to read the AMP value
1955 * @ch: channel (left=0 or right=1)
1956 * @direction: #HDA_INPUT or #HDA_OUTPUT
1957 * @idx: the index value (only for input direction)
1958 * @mask: bit mask to set
1959 * @val: the bits value to set
1961 * Update the AMP value with a bit mask.
1962 * Returns 0 if the value is unchanged, 1 if changed.
1964 int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
1965 int direction, int idx, int mask, int val)
1967 return codec_amp_update(codec, nid, ch, direction, idx, mask, val, false);
1969 EXPORT_SYMBOL_HDA(snd_hda_codec_amp_update);
1972 * snd_hda_codec_amp_stereo - update the AMP stereo values
1973 * @codec: HD-audio codec
1974 * @nid: NID to read the AMP value
1975 * @direction: #HDA_INPUT or #HDA_OUTPUT
1976 * @idx: the index value (only for input direction)
1977 * @mask: bit mask to set
1978 * @val: the bits value to set
1980 * Update the AMP values like snd_hda_codec_amp_update(), but for a
1981 * stereo widget with the same mask and value.
1983 int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
1984 int direction, int idx, int mask, int val)
1988 if (snd_BUG_ON(mask & ~0xff))
1990 for (ch = 0; ch < 2; ch++)
1991 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
1995 EXPORT_SYMBOL_HDA(snd_hda_codec_amp_stereo);
1997 /* Works like snd_hda_codec_amp_update() but it writes the value only at
1998 * the first access. If the amp was already initialized / updated beforehand,
1999 * this does nothing.
2001 int snd_hda_codec_amp_init(struct hda_codec *codec, hda_nid_t nid, int ch,
2002 int dir, int idx, int mask, int val)
2004 return codec_amp_update(codec, nid, ch, dir, idx, mask, val, true);
2006 EXPORT_SYMBOL_HDA(snd_hda_codec_amp_init);
2008 int snd_hda_codec_amp_init_stereo(struct hda_codec *codec, hda_nid_t nid,
2009 int dir, int idx, int mask, int val)
2013 if (snd_BUG_ON(mask & ~0xff))
2015 for (ch = 0; ch < 2; ch++)
2016 ret |= snd_hda_codec_amp_init(codec, nid, ch, dir,
2020 EXPORT_SYMBOL_HDA(snd_hda_codec_amp_init_stereo);
2023 * snd_hda_codec_resume_amp - Resume all AMP commands from the cache
2024 * @codec: HD-audio codec
2026 * Resume the all amp commands from the cache.
2028 void snd_hda_codec_resume_amp(struct hda_codec *codec)
2032 mutex_lock(&codec->hash_mutex);
2033 codec->cached_write = 0;
2034 for (i = 0; i < codec->amp_cache.buf.used; i++) {
2035 struct hda_amp_info *buffer;
2038 unsigned int idx, dir, ch;
2039 struct hda_amp_info info;
2041 buffer = snd_array_elem(&codec->amp_cache.buf, i);
2042 if (!buffer->head.dirty)
2044 buffer->head.dirty = 0;
2046 key = info.head.key;
2050 idx = (key >> 16) & 0xff;
2051 dir = (key >> 24) & 0xff;
2052 for (ch = 0; ch < 2; ch++) {
2053 if (!(info.head.val & INFO_AMP_VOL(ch)))
2055 mutex_unlock(&codec->hash_mutex);
2056 put_vol_mute(codec, info.amp_caps, nid, ch, dir, idx,
2058 mutex_lock(&codec->hash_mutex);
2061 mutex_unlock(&codec->hash_mutex);
2063 EXPORT_SYMBOL_HDA(snd_hda_codec_resume_amp);
2065 static u32 get_amp_max_value(struct hda_codec *codec, hda_nid_t nid, int dir,
2068 u32 caps = query_amp_caps(codec, nid, dir);
2070 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
2077 * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer
2079 * The control element is supposed to have the private_value field
2080 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2082 int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
2083 struct snd_ctl_elem_info *uinfo)
2085 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2086 u16 nid = get_amp_nid(kcontrol);
2087 u8 chs = get_amp_channels(kcontrol);
2088 int dir = get_amp_direction(kcontrol);
2089 unsigned int ofs = get_amp_offset(kcontrol);
2091 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2092 uinfo->count = chs == 3 ? 2 : 1;
2093 uinfo->value.integer.min = 0;
2094 uinfo->value.integer.max = get_amp_max_value(codec, nid, dir, ofs);
2095 if (!uinfo->value.integer.max) {
2096 printk(KERN_WARNING "hda_codec: "
2097 "num_steps = 0 for NID=0x%x (ctl = %s)\n", nid,
2103 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_info);
2106 static inline unsigned int
2107 read_amp_value(struct hda_codec *codec, hda_nid_t nid,
2108 int ch, int dir, int idx, unsigned int ofs)
2111 val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
2112 val &= HDA_AMP_VOLMASK;
2121 update_amp_value(struct hda_codec *codec, hda_nid_t nid,
2122 int ch, int dir, int idx, unsigned int ofs,
2125 unsigned int maxval;
2129 /* ofs = 0: raw max value */
2130 maxval = get_amp_max_value(codec, nid, dir, 0);
2133 return snd_hda_codec_amp_update(codec, nid, ch, dir, idx,
2134 HDA_AMP_VOLMASK, val);
2138 * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume
2140 * The control element is supposed to have the private_value field
2141 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2143 int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
2144 struct snd_ctl_elem_value *ucontrol)
2146 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2147 hda_nid_t nid = get_amp_nid(kcontrol);
2148 int chs = get_amp_channels(kcontrol);
2149 int dir = get_amp_direction(kcontrol);
2150 int idx = get_amp_index(kcontrol);
2151 unsigned int ofs = get_amp_offset(kcontrol);
2152 long *valp = ucontrol->value.integer.value;
2155 *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs);
2157 *valp = read_amp_value(codec, nid, 1, dir, idx, ofs);
2160 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_get);
2163 * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume
2165 * The control element is supposed to have the private_value field
2166 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2168 int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
2169 struct snd_ctl_elem_value *ucontrol)
2171 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2172 hda_nid_t nid = get_amp_nid(kcontrol);
2173 int chs = get_amp_channels(kcontrol);
2174 int dir = get_amp_direction(kcontrol);
2175 int idx = get_amp_index(kcontrol);
2176 unsigned int ofs = get_amp_offset(kcontrol);
2177 long *valp = ucontrol->value.integer.value;
2180 snd_hda_power_up(codec);
2182 change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp);
2186 change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp);
2187 snd_hda_power_down(codec);
2190 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_put);
2193 * snd_hda_mixer_amp_volume_put - TLV callback for a standard AMP mixer volume
2195 * The control element is supposed to have the private_value field
2196 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2198 int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2199 unsigned int size, unsigned int __user *_tlv)
2201 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2202 hda_nid_t nid = get_amp_nid(kcontrol);
2203 int dir = get_amp_direction(kcontrol);
2204 unsigned int ofs = get_amp_offset(kcontrol);
2205 bool min_mute = get_amp_min_mute(kcontrol);
2206 u32 caps, val1, val2;
2208 if (size < 4 * sizeof(unsigned int))
2210 caps = query_amp_caps(codec, nid, dir);
2211 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2212 val2 = (val2 + 1) * 25;
2213 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
2215 val1 = ((int)val1) * ((int)val2);
2216 if (min_mute || (caps & AC_AMPCAP_MIN_MUTE))
2217 val2 |= TLV_DB_SCALE_MUTE;
2218 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
2220 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
2222 if (put_user(val1, _tlv + 2))
2224 if (put_user(val2, _tlv + 3))
2228 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_tlv);
2231 * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control
2232 * @codec: HD-audio codec
2233 * @nid: NID of a reference widget
2234 * @dir: #HDA_INPUT or #HDA_OUTPUT
2235 * @tlv: TLV data to be stored, at least 4 elements
2237 * Set (static) TLV data for a virtual master volume using the AMP caps
2238 * obtained from the reference NID.
2239 * The volume range is recalculated as if the max volume is 0dB.
2241 void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
2247 caps = query_amp_caps(codec, nid, dir);
2248 nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
2249 step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2250 step = (step + 1) * 25;
2251 tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
2252 tlv[1] = 2 * sizeof(unsigned int);
2253 tlv[2] = -nums * step;
2256 EXPORT_SYMBOL_HDA(snd_hda_set_vmaster_tlv);
2258 /* find a mixer control element with the given name */
2259 static struct snd_kcontrol *
2260 find_mixer_ctl(struct hda_codec *codec, const char *name, int dev, int idx)
2262 struct snd_ctl_elem_id id;
2263 memset(&id, 0, sizeof(id));
2264 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
2267 if (snd_BUG_ON(strlen(name) >= sizeof(id.name)))
2269 strcpy(id.name, name);
2270 return snd_ctl_find_id(codec->bus->card, &id);
2274 * snd_hda_find_mixer_ctl - Find a mixer control element with the given name
2275 * @codec: HD-audio codec
2276 * @name: ctl id name string
2278 * Get the control element with the given id string and IFACE_MIXER.
2280 struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
2283 return find_mixer_ctl(codec, name, 0, 0);
2285 EXPORT_SYMBOL_HDA(snd_hda_find_mixer_ctl);
2287 static int find_empty_mixer_ctl_idx(struct hda_codec *codec, const char *name,
2291 for (idx = 0; idx < 16; idx++) { /* 16 ctlrs should be large enough */
2292 if (!find_mixer_ctl(codec, name, dev, idx))
2299 * snd_hda_ctl_add - Add a control element and assign to the codec
2300 * @codec: HD-audio codec
2301 * @nid: corresponding NID (optional)
2302 * @kctl: the control element to assign
2304 * Add the given control element to an array inside the codec instance.
2305 * All control elements belonging to a codec are supposed to be added
2306 * by this function so that a proper clean-up works at the free or
2307 * reconfiguration time.
2309 * If non-zero @nid is passed, the NID is assigned to the control element.
2310 * The assignment is shown in the codec proc file.
2312 * snd_hda_ctl_add() checks the control subdev id field whether
2313 * #HDA_SUBDEV_NID_FLAG bit is set. If set (and @nid is zero), the lower
2314 * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit
2315 * specifies if kctl->private_value is a HDA amplifier value.
2317 int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid,
2318 struct snd_kcontrol *kctl)
2321 unsigned short flags = 0;
2322 struct hda_nid_item *item;
2324 if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) {
2325 flags |= HDA_NID_ITEM_AMP;
2327 nid = get_amp_nid_(kctl->private_value);
2329 if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0)
2330 nid = kctl->id.subdevice & 0xffff;
2331 if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG))
2332 kctl->id.subdevice = 0;
2333 err = snd_ctl_add(codec->bus->card, kctl);
2336 item = snd_array_new(&codec->mixers);
2341 item->flags = flags;
2344 EXPORT_SYMBOL_HDA(snd_hda_ctl_add);
2347 * snd_hda_add_nid - Assign a NID to a control element
2348 * @codec: HD-audio codec
2349 * @nid: corresponding NID (optional)
2350 * @kctl: the control element to assign
2351 * @index: index to kctl
2353 * Add the given control element to an array inside the codec instance.
2354 * This function is used when #snd_hda_ctl_add cannot be used for 1:1
2355 * NID:KCTL mapping - for example "Capture Source" selector.
2357 int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl,
2358 unsigned int index, hda_nid_t nid)
2360 struct hda_nid_item *item;
2363 item = snd_array_new(&codec->nids);
2367 item->index = index;
2371 printk(KERN_ERR "hda-codec: no NID for mapping control %s:%d:%d\n",
2372 kctl->id.name, kctl->id.index, index);
2375 EXPORT_SYMBOL_HDA(snd_hda_add_nid);
2378 * snd_hda_ctls_clear - Clear all controls assigned to the given codec
2379 * @codec: HD-audio codec
2381 void snd_hda_ctls_clear(struct hda_codec *codec)
2384 struct hda_nid_item *items = codec->mixers.list;
2385 for (i = 0; i < codec->mixers.used; i++)
2386 snd_ctl_remove(codec->bus->card, items[i].kctl);
2387 snd_array_free(&codec->mixers);
2388 snd_array_free(&codec->nids);
2391 /* pseudo device locking
2392 * toggle card->shutdown to allow/disallow the device access (as a hack)
2394 int snd_hda_lock_devices(struct hda_bus *bus)
2396 struct snd_card *card = bus->card;
2397 struct hda_codec *codec;
2399 spin_lock(&card->files_lock);
2403 if (!list_empty(&card->ctl_files))
2406 list_for_each_entry(codec, &bus->codec_list, list) {
2408 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
2409 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
2412 if (cpcm->pcm->streams[0].substream_opened ||
2413 cpcm->pcm->streams[1].substream_opened)
2417 spin_unlock(&card->files_lock);
2423 spin_unlock(&card->files_lock);
2426 EXPORT_SYMBOL_HDA(snd_hda_lock_devices);
2428 void snd_hda_unlock_devices(struct hda_bus *bus)
2430 struct snd_card *card = bus->card;
2433 spin_lock(&card->files_lock);
2435 spin_unlock(&card->files_lock);
2437 EXPORT_SYMBOL_HDA(snd_hda_unlock_devices);
2440 * snd_hda_codec_reset - Clear all objects assigned to the codec
2441 * @codec: HD-audio codec
2443 * This frees the all PCM and control elements assigned to the codec, and
2444 * clears the caches and restores the pin default configurations.
2446 * When a device is being used, it returns -EBSY. If successfully freed,
2449 int snd_hda_codec_reset(struct hda_codec *codec)
2451 struct hda_bus *bus = codec->bus;
2452 struct snd_card *card = bus->card;
2455 if (snd_hda_lock_devices(bus) < 0)
2458 /* OK, let it free */
2459 cancel_delayed_work_sync(&codec->jackpoll_work);
2461 cancel_delayed_work_sync(&codec->power_work);
2462 codec->power_on = 0;
2463 codec->power_transition = 0;
2464 codec->power_jiffies = jiffies;
2465 flush_workqueue(bus->workq);
2467 snd_hda_ctls_clear(codec);
2469 for (i = 0; i < codec->num_pcms; i++) {
2470 if (codec->pcm_info[i].pcm) {
2471 snd_device_free(card, codec->pcm_info[i].pcm);
2472 clear_bit(codec->pcm_info[i].device,
2476 if (codec->patch_ops.free)
2477 codec->patch_ops.free(codec);
2478 memset(&codec->patch_ops, 0, sizeof(codec->patch_ops));
2479 snd_hda_jack_tbl_clear(codec);
2480 codec->proc_widget_hook = NULL;
2482 free_hda_cache(&codec->amp_cache);
2483 free_hda_cache(&codec->cmd_cache);
2484 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
2485 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
2486 /* free only driver_pins so that init_pins + user_pins are restored */
2487 snd_array_free(&codec->driver_pins);
2488 snd_array_free(&codec->cvt_setups);
2489 snd_array_free(&codec->spdif_out);
2490 snd_array_free(&codec->verbs);
2491 codec->num_pcms = 0;
2492 codec->pcm_info = NULL;
2493 codec->preset = NULL;
2494 codec->slave_dig_outs = NULL;
2495 codec->spdif_status_reset = 0;
2496 module_put(codec->owner);
2497 codec->owner = NULL;
2499 /* allow device access again */
2500 snd_hda_unlock_devices(bus);
2504 typedef int (*map_slave_func_t)(void *, struct snd_kcontrol *);
2506 /* apply the function to all matching slave ctls in the mixer list */
2507 static int map_slaves(struct hda_codec *codec, const char * const *slaves,
2508 const char *suffix, map_slave_func_t func, void *data)
2510 struct hda_nid_item *items;
2511 const char * const *s;
2514 items = codec->mixers.list;
2515 for (i = 0; i < codec->mixers.used; i++) {
2516 struct snd_kcontrol *sctl = items[i].kctl;
2517 if (!sctl || !sctl->id.name ||
2518 sctl->id.iface != SNDRV_CTL_ELEM_IFACE_MIXER)
2520 for (s = slaves; *s; s++) {
2521 char tmpname[sizeof(sctl->id.name)];
2522 const char *name = *s;
2524 snprintf(tmpname, sizeof(tmpname), "%s %s",
2528 if (!strcmp(sctl->id.name, name)) {
2529 err = func(data, sctl);
2539 static int check_slave_present(void *data, struct snd_kcontrol *sctl)
2544 /* guess the value corresponding to 0dB */
2545 static int get_kctl_0dB_offset(struct snd_kcontrol *kctl)
2548 const int *tlv = NULL;
2551 if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
2552 /* FIXME: set_fs() hack for obtaining user-space TLV data */
2553 mm_segment_t fs = get_fs();
2555 if (!kctl->tlv.c(kctl, 0, sizeof(_tlv), _tlv))
2558 } else if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_READ)
2560 if (tlv && tlv[0] == SNDRV_CTL_TLVT_DB_SCALE)
2561 val = -tlv[2] / tlv[3];
2565 /* call kctl->put with the given value(s) */
2566 static int put_kctl_with_value(struct snd_kcontrol *kctl, int val)
2568 struct snd_ctl_elem_value *ucontrol;
2569 ucontrol = kzalloc(sizeof(*ucontrol), GFP_KERNEL);
2572 ucontrol->value.integer.value[0] = val;
2573 ucontrol->value.integer.value[1] = val;
2574 kctl->put(kctl, ucontrol);
2579 /* initialize the slave volume with 0dB */
2580 static int init_slave_0dB(void *data, struct snd_kcontrol *slave)
2582 int offset = get_kctl_0dB_offset(slave);
2584 put_kctl_with_value(slave, offset);
2588 /* unmute the slave */
2589 static int init_slave_unmute(void *data, struct snd_kcontrol *slave)
2591 return put_kctl_with_value(slave, 1);
2595 * snd_hda_add_vmaster - create a virtual master control and add slaves
2596 * @codec: HD-audio codec
2597 * @name: vmaster control name
2598 * @tlv: TLV data (optional)
2599 * @slaves: slave control names (optional)
2600 * @suffix: suffix string to each slave name (optional)
2601 * @init_slave_vol: initialize slaves to unmute/0dB
2602 * @ctl_ret: store the vmaster kcontrol in return
2604 * Create a virtual master control with the given name. The TLV data
2605 * must be either NULL or a valid data.
2607 * @slaves is a NULL-terminated array of strings, each of which is a
2608 * slave control name. All controls with these names are assigned to
2609 * the new virtual master control.
2611 * This function returns zero if successful or a negative error code.
2613 int __snd_hda_add_vmaster(struct hda_codec *codec, char *name,
2614 unsigned int *tlv, const char * const *slaves,
2615 const char *suffix, bool init_slave_vol,
2616 struct snd_kcontrol **ctl_ret)
2618 struct snd_kcontrol *kctl;
2624 err = map_slaves(codec, slaves, suffix, check_slave_present, NULL);
2626 snd_printdd("No slave found for %s\n", name);
2629 kctl = snd_ctl_make_virtual_master(name, tlv);
2632 err = snd_hda_ctl_add(codec, 0, kctl);
2636 err = map_slaves(codec, slaves, suffix,
2637 (map_slave_func_t)snd_ctl_add_slave, kctl);
2641 /* init with master mute & zero volume */
2642 put_kctl_with_value(kctl, 0);
2644 map_slaves(codec, slaves, suffix,
2645 tlv ? init_slave_0dB : init_slave_unmute, kctl);
2651 EXPORT_SYMBOL_HDA(__snd_hda_add_vmaster);
2654 * mute-LED control using vmaster
2656 static int vmaster_mute_mode_info(struct snd_kcontrol *kcontrol,
2657 struct snd_ctl_elem_info *uinfo)
2659 static const char * const texts[] = {
2660 "On", "Off", "Follow Master"
2664 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2666 uinfo->value.enumerated.items = 3;
2667 index = uinfo->value.enumerated.item;
2670 strcpy(uinfo->value.enumerated.name, texts[index]);
2674 static int vmaster_mute_mode_get(struct snd_kcontrol *kcontrol,
2675 struct snd_ctl_elem_value *ucontrol)
2677 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2678 ucontrol->value.enumerated.item[0] = hook->mute_mode;
2682 static int vmaster_mute_mode_put(struct snd_kcontrol *kcontrol,
2683 struct snd_ctl_elem_value *ucontrol)
2685 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2686 unsigned int old_mode = hook->mute_mode;
2688 hook->mute_mode = ucontrol->value.enumerated.item[0];
2689 if (hook->mute_mode > HDA_VMUTE_FOLLOW_MASTER)
2690 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
2691 if (old_mode == hook->mute_mode)
2693 snd_hda_sync_vmaster_hook(hook);
2697 static struct snd_kcontrol_new vmaster_mute_mode = {
2698 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2699 .name = "Mute-LED Mode",
2700 .info = vmaster_mute_mode_info,
2701 .get = vmaster_mute_mode_get,
2702 .put = vmaster_mute_mode_put,
2706 * Add a mute-LED hook with the given vmaster switch kctl
2707 * "Mute-LED Mode" control is automatically created and associated with
2710 int snd_hda_add_vmaster_hook(struct hda_codec *codec,
2711 struct hda_vmaster_mute_hook *hook,
2712 bool expose_enum_ctl)
2714 struct snd_kcontrol *kctl;
2716 if (!hook->hook || !hook->sw_kctl)
2718 snd_ctl_add_vmaster_hook(hook->sw_kctl, hook->hook, codec);
2719 hook->codec = codec;
2720 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
2721 if (!expose_enum_ctl)
2723 kctl = snd_ctl_new1(&vmaster_mute_mode, hook);
2726 return snd_hda_ctl_add(codec, 0, kctl);
2728 EXPORT_SYMBOL_HDA(snd_hda_add_vmaster_hook);
2731 * Call the hook with the current value for synchronization
2732 * Should be called in init callback
2734 void snd_hda_sync_vmaster_hook(struct hda_vmaster_mute_hook *hook)
2736 if (!hook->hook || !hook->codec)
2738 switch (hook->mute_mode) {
2739 case HDA_VMUTE_FOLLOW_MASTER:
2740 snd_ctl_sync_vmaster_hook(hook->sw_kctl);
2743 hook->hook(hook->codec, hook->mute_mode);
2747 EXPORT_SYMBOL_HDA(snd_hda_sync_vmaster_hook);
2751 * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch
2753 * The control element is supposed to have the private_value field
2754 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2756 int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
2757 struct snd_ctl_elem_info *uinfo)
2759 int chs = get_amp_channels(kcontrol);
2761 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2762 uinfo->count = chs == 3 ? 2 : 1;
2763 uinfo->value.integer.min = 0;
2764 uinfo->value.integer.max = 1;
2767 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_info);
2770 * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch
2772 * The control element is supposed to have the private_value field
2773 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2775 int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
2776 struct snd_ctl_elem_value *ucontrol)
2778 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2779 hda_nid_t nid = get_amp_nid(kcontrol);
2780 int chs = get_amp_channels(kcontrol);
2781 int dir = get_amp_direction(kcontrol);
2782 int idx = get_amp_index(kcontrol);
2783 long *valp = ucontrol->value.integer.value;
2786 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
2787 HDA_AMP_MUTE) ? 0 : 1;
2789 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
2790 HDA_AMP_MUTE) ? 0 : 1;
2793 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_get);
2796 * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch
2798 * The control element is supposed to have the private_value field
2799 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2801 int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
2802 struct snd_ctl_elem_value *ucontrol)
2804 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2805 hda_nid_t nid = get_amp_nid(kcontrol);
2806 int chs = get_amp_channels(kcontrol);
2807 int dir = get_amp_direction(kcontrol);
2808 int idx = get_amp_index(kcontrol);
2809 long *valp = ucontrol->value.integer.value;
2812 snd_hda_power_up(codec);
2814 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
2816 *valp ? 0 : HDA_AMP_MUTE);
2820 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
2822 *valp ? 0 : HDA_AMP_MUTE);
2823 hda_call_check_power_status(codec, nid);
2824 snd_hda_power_down(codec);
2827 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put);
2830 * bound volume controls
2832 * bind multiple volumes (# indices, from 0)
2835 #define AMP_VAL_IDX_SHIFT 19
2836 #define AMP_VAL_IDX_MASK (0x0f<<19)
2839 * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control
2841 * The control element is supposed to have the private_value field
2842 * set up via HDA_BIND_MUTE*() macros.
2844 int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
2845 struct snd_ctl_elem_value *ucontrol)
2847 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2851 mutex_lock(&codec->control_mutex);
2852 pval = kcontrol->private_value;
2853 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
2854 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
2855 kcontrol->private_value = pval;
2856 mutex_unlock(&codec->control_mutex);
2859 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_get);
2862 * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control
2864 * The control element is supposed to have the private_value field
2865 * set up via HDA_BIND_MUTE*() macros.
2867 int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
2868 struct snd_ctl_elem_value *ucontrol)
2870 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2872 int i, indices, err = 0, change = 0;
2874 mutex_lock(&codec->control_mutex);
2875 pval = kcontrol->private_value;
2876 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
2877 for (i = 0; i < indices; i++) {
2878 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
2879 (i << AMP_VAL_IDX_SHIFT);
2880 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2885 kcontrol->private_value = pval;
2886 mutex_unlock(&codec->control_mutex);
2887 return err < 0 ? err : change;
2889 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_put);
2892 * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control
2894 * The control element is supposed to have the private_value field
2895 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2897 int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
2898 struct snd_ctl_elem_info *uinfo)
2900 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2901 struct hda_bind_ctls *c;
2904 mutex_lock(&codec->control_mutex);
2905 c = (struct hda_bind_ctls *)kcontrol->private_value;
2906 kcontrol->private_value = *c->values;
2907 err = c->ops->info(kcontrol, uinfo);
2908 kcontrol->private_value = (long)c;
2909 mutex_unlock(&codec->control_mutex);
2912 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_info);
2915 * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control
2917 * The control element is supposed to have the private_value field
2918 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2920 int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
2921 struct snd_ctl_elem_value *ucontrol)
2923 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2924 struct hda_bind_ctls *c;
2927 mutex_lock(&codec->control_mutex);
2928 c = (struct hda_bind_ctls *)kcontrol->private_value;
2929 kcontrol->private_value = *c->values;
2930 err = c->ops->get(kcontrol, ucontrol);
2931 kcontrol->private_value = (long)c;
2932 mutex_unlock(&codec->control_mutex);
2935 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_get);
2938 * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control
2940 * The control element is supposed to have the private_value field
2941 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2943 int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
2944 struct snd_ctl_elem_value *ucontrol)
2946 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2947 struct hda_bind_ctls *c;
2948 unsigned long *vals;
2949 int err = 0, change = 0;
2951 mutex_lock(&codec->control_mutex);
2952 c = (struct hda_bind_ctls *)kcontrol->private_value;
2953 for (vals = c->values; *vals; vals++) {
2954 kcontrol->private_value = *vals;
2955 err = c->ops->put(kcontrol, ucontrol);
2960 kcontrol->private_value = (long)c;
2961 mutex_unlock(&codec->control_mutex);
2962 return err < 0 ? err : change;
2964 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_put);
2967 * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control
2969 * The control element is supposed to have the private_value field
2970 * set up via HDA_BIND_VOL() macro.
2972 int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2973 unsigned int size, unsigned int __user *tlv)
2975 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2976 struct hda_bind_ctls *c;
2979 mutex_lock(&codec->control_mutex);
2980 c = (struct hda_bind_ctls *)kcontrol->private_value;
2981 kcontrol->private_value = *c->values;
2982 err = c->ops->tlv(kcontrol, op_flag, size, tlv);
2983 kcontrol->private_value = (long)c;
2984 mutex_unlock(&codec->control_mutex);
2987 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_tlv);
2989 struct hda_ctl_ops snd_hda_bind_vol = {
2990 .info = snd_hda_mixer_amp_volume_info,
2991 .get = snd_hda_mixer_amp_volume_get,
2992 .put = snd_hda_mixer_amp_volume_put,
2993 .tlv = snd_hda_mixer_amp_tlv
2995 EXPORT_SYMBOL_HDA(snd_hda_bind_vol);
2997 struct hda_ctl_ops snd_hda_bind_sw = {
2998 .info = snd_hda_mixer_amp_switch_info,
2999 .get = snd_hda_mixer_amp_switch_get,
3000 .put = snd_hda_mixer_amp_switch_put,
3001 .tlv = snd_hda_mixer_amp_tlv
3003 EXPORT_SYMBOL_HDA(snd_hda_bind_sw);
3006 * SPDIF out controls
3009 static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
3010 struct snd_ctl_elem_info *uinfo)
3012 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
3017 static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
3018 struct snd_ctl_elem_value *ucontrol)
3020 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
3021 IEC958_AES0_NONAUDIO |
3022 IEC958_AES0_CON_EMPHASIS_5015 |
3023 IEC958_AES0_CON_NOT_COPYRIGHT;
3024 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
3025 IEC958_AES1_CON_ORIGINAL;
3029 static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
3030 struct snd_ctl_elem_value *ucontrol)
3032 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
3033 IEC958_AES0_NONAUDIO |
3034 IEC958_AES0_PRO_EMPHASIS_5015;
3038 static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
3039 struct snd_ctl_elem_value *ucontrol)
3041 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3042 int idx = kcontrol->private_value;
3043 struct hda_spdif_out *spdif;
3045 mutex_lock(&codec->spdif_mutex);
3046 spdif = snd_array_elem(&codec->spdif_out, idx);
3047 ucontrol->value.iec958.status[0] = spdif->status & 0xff;
3048 ucontrol->value.iec958.status[1] = (spdif->status >> 8) & 0xff;
3049 ucontrol->value.iec958.status[2] = (spdif->status >> 16) & 0xff;
3050 ucontrol->value.iec958.status[3] = (spdif->status >> 24) & 0xff;
3051 mutex_unlock(&codec->spdif_mutex);
3056 /* convert from SPDIF status bits to HDA SPDIF bits
3057 * bit 0 (DigEn) is always set zero (to be filled later)
3059 static unsigned short convert_from_spdif_status(unsigned int sbits)
3061 unsigned short val = 0;
3063 if (sbits & IEC958_AES0_PROFESSIONAL)
3064 val |= AC_DIG1_PROFESSIONAL;
3065 if (sbits & IEC958_AES0_NONAUDIO)
3066 val |= AC_DIG1_NONAUDIO;
3067 if (sbits & IEC958_AES0_PROFESSIONAL) {
3068 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
3069 IEC958_AES0_PRO_EMPHASIS_5015)
3070 val |= AC_DIG1_EMPHASIS;
3072 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
3073 IEC958_AES0_CON_EMPHASIS_5015)
3074 val |= AC_DIG1_EMPHASIS;
3075 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
3076 val |= AC_DIG1_COPYRIGHT;
3077 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
3078 val |= AC_DIG1_LEVEL;
3079 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
3084 /* convert to SPDIF status bits from HDA SPDIF bits
3086 static unsigned int convert_to_spdif_status(unsigned short val)
3088 unsigned int sbits = 0;
3090 if (val & AC_DIG1_NONAUDIO)
3091 sbits |= IEC958_AES0_NONAUDIO;
3092 if (val & AC_DIG1_PROFESSIONAL)
3093 sbits |= IEC958_AES0_PROFESSIONAL;
3094 if (sbits & IEC958_AES0_PROFESSIONAL) {
3095 if (sbits & AC_DIG1_EMPHASIS)
3096 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
3098 if (val & AC_DIG1_EMPHASIS)
3099 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
3100 if (!(val & AC_DIG1_COPYRIGHT))
3101 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
3102 if (val & AC_DIG1_LEVEL)
3103 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
3104 sbits |= val & (0x7f << 8);
3109 /* set digital convert verbs both for the given NID and its slaves */
3110 static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
3115 snd_hda_codec_write_cache(codec, nid, 0, verb, val);
3116 d = codec->slave_dig_outs;
3120 snd_hda_codec_write_cache(codec, *d, 0, verb, val);
3123 static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
3127 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1);
3129 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2);
3132 static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
3133 struct snd_ctl_elem_value *ucontrol)
3135 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3136 int idx = kcontrol->private_value;
3137 struct hda_spdif_out *spdif;
3142 mutex_lock(&codec->spdif_mutex);
3143 spdif = snd_array_elem(&codec->spdif_out, idx);
3145 spdif->status = ucontrol->value.iec958.status[0] |
3146 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
3147 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
3148 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
3149 val = convert_from_spdif_status(spdif->status);
3150 val |= spdif->ctls & 1;
3151 change = spdif->ctls != val;
3153 if (change && nid != (u16)-1)
3154 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
3155 mutex_unlock(&codec->spdif_mutex);
3159 #define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
3161 static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
3162 struct snd_ctl_elem_value *ucontrol)
3164 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3165 int idx = kcontrol->private_value;
3166 struct hda_spdif_out *spdif;
3168 mutex_lock(&codec->spdif_mutex);
3169 spdif = snd_array_elem(&codec->spdif_out, idx);
3170 ucontrol->value.integer.value[0] = spdif->ctls & AC_DIG1_ENABLE;
3171 mutex_unlock(&codec->spdif_mutex);
3175 static inline void set_spdif_ctls(struct hda_codec *codec, hda_nid_t nid,
3178 set_dig_out_convert(codec, nid, dig1, dig2);
3179 /* unmute amp switch (if any) */
3180 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
3181 (dig1 & AC_DIG1_ENABLE))
3182 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
3186 static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
3187 struct snd_ctl_elem_value *ucontrol)
3189 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3190 int idx = kcontrol->private_value;
3191 struct hda_spdif_out *spdif;
3196 mutex_lock(&codec->spdif_mutex);
3197 spdif = snd_array_elem(&codec->spdif_out, idx);
3199 val = spdif->ctls & ~AC_DIG1_ENABLE;
3200 if (ucontrol->value.integer.value[0])
3201 val |= AC_DIG1_ENABLE;
3202 change = spdif->ctls != val;
3204 if (change && nid != (u16)-1)
3205 set_spdif_ctls(codec, nid, val & 0xff, -1);
3206 mutex_unlock(&codec->spdif_mutex);
3210 static struct snd_kcontrol_new dig_mixes[] = {
3212 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3213 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3214 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
3215 .info = snd_hda_spdif_mask_info,
3216 .get = snd_hda_spdif_cmask_get,
3219 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3220 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3221 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK),
3222 .info = snd_hda_spdif_mask_info,
3223 .get = snd_hda_spdif_pmask_get,
3226 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3227 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
3228 .info = snd_hda_spdif_mask_info,
3229 .get = snd_hda_spdif_default_get,
3230 .put = snd_hda_spdif_default_put,
3233 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3234 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
3235 .info = snd_hda_spdif_out_switch_info,
3236 .get = snd_hda_spdif_out_switch_get,
3237 .put = snd_hda_spdif_out_switch_put,
3243 * snd_hda_create_dig_out_ctls - create Output SPDIF-related controls
3244 * @codec: the HDA codec
3245 * @associated_nid: NID that new ctls associated with
3246 * @cvt_nid: converter NID
3247 * @type: HDA_PCM_TYPE_*
3248 * Creates controls related with the digital output.
3249 * Called from each patch supporting the digital out.
3251 * Returns 0 if successful, or a negative error code.
3253 int snd_hda_create_dig_out_ctls(struct hda_codec *codec,
3254 hda_nid_t associated_nid,
3259 struct snd_kcontrol *kctl;
3260 struct snd_kcontrol_new *dig_mix;
3262 const int spdif_pcm_dev = 1;
3263 struct hda_spdif_out *spdif;
3265 if (codec->primary_dig_out_type == HDA_PCM_TYPE_HDMI &&
3266 type == HDA_PCM_TYPE_SPDIF) {
3267 dev = spdif_pcm_dev;
3268 } else if (codec->primary_dig_out_type == HDA_PCM_TYPE_SPDIF &&
3269 type == HDA_PCM_TYPE_HDMI) {
3270 for (idx = 0; idx < codec->spdif_out.used; idx++) {
3271 spdif = snd_array_elem(&codec->spdif_out, idx);
3272 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
3273 kctl = find_mixer_ctl(codec, dig_mix->name, 0, idx);
3276 kctl->id.device = spdif_pcm_dev;
3279 codec->primary_dig_out_type = HDA_PCM_TYPE_HDMI;
3281 if (!codec->primary_dig_out_type)
3282 codec->primary_dig_out_type = type;
3284 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Playback Switch", dev);
3286 printk(KERN_ERR "hda_codec: too many IEC958 outputs\n");
3289 spdif = snd_array_new(&codec->spdif_out);
3290 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
3291 kctl = snd_ctl_new1(dig_mix, codec);
3294 kctl->id.device = dev;
3295 kctl->id.index = idx;
3296 kctl->private_value = codec->spdif_out.used - 1;
3297 err = snd_hda_ctl_add(codec, associated_nid, kctl);
3301 spdif->nid = cvt_nid;
3302 spdif->ctls = snd_hda_codec_read(codec, cvt_nid, 0,
3303 AC_VERB_GET_DIGI_CONVERT_1, 0);
3304 spdif->status = convert_to_spdif_status(spdif->ctls);
3307 EXPORT_SYMBOL_HDA(snd_hda_create_dig_out_ctls);
3309 /* get the hda_spdif_out entry from the given NID
3310 * call within spdif_mutex lock
3312 struct hda_spdif_out *snd_hda_spdif_out_of_nid(struct hda_codec *codec,
3316 for (i = 0; i < codec->spdif_out.used; i++) {
3317 struct hda_spdif_out *spdif =
3318 snd_array_elem(&codec->spdif_out, i);
3319 if (spdif->nid == nid)
3324 EXPORT_SYMBOL_HDA(snd_hda_spdif_out_of_nid);
3326 void snd_hda_spdif_ctls_unassign(struct hda_codec *codec, int idx)
3328 struct hda_spdif_out *spdif;
3330 mutex_lock(&codec->spdif_mutex);
3331 spdif = snd_array_elem(&codec->spdif_out, idx);
3332 spdif->nid = (u16)-1;
3333 mutex_unlock(&codec->spdif_mutex);
3335 EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_unassign);
3337 void snd_hda_spdif_ctls_assign(struct hda_codec *codec, int idx, hda_nid_t nid)
3339 struct hda_spdif_out *spdif;
3342 mutex_lock(&codec->spdif_mutex);
3343 spdif = snd_array_elem(&codec->spdif_out, idx);
3344 if (spdif->nid != nid) {
3347 set_spdif_ctls(codec, nid, val & 0xff, (val >> 8) & 0xff);
3349 mutex_unlock(&codec->spdif_mutex);
3351 EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_assign);
3354 * SPDIF sharing with analog output
3356 static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
3357 struct snd_ctl_elem_value *ucontrol)
3359 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3360 ucontrol->value.integer.value[0] = mout->share_spdif;
3364 static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
3365 struct snd_ctl_elem_value *ucontrol)
3367 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3368 mout->share_spdif = !!ucontrol->value.integer.value[0];
3372 static struct snd_kcontrol_new spdif_share_sw = {
3373 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3374 .name = "IEC958 Default PCM Playback Switch",
3375 .info = snd_ctl_boolean_mono_info,
3376 .get = spdif_share_sw_get,
3377 .put = spdif_share_sw_put,
3381 * snd_hda_create_spdif_share_sw - create Default PCM switch
3382 * @codec: the HDA codec
3383 * @mout: multi-out instance
3385 int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
3386 struct hda_multi_out *mout)
3388 if (!mout->dig_out_nid)
3390 /* ATTENTION: here mout is passed as private_data, instead of codec */
3391 return snd_hda_ctl_add(codec, mout->dig_out_nid,
3392 snd_ctl_new1(&spdif_share_sw, mout));
3394 EXPORT_SYMBOL_HDA(snd_hda_create_spdif_share_sw);
3400 #define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
3402 static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
3403 struct snd_ctl_elem_value *ucontrol)
3405 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3407 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
3411 static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
3412 struct snd_ctl_elem_value *ucontrol)
3414 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3415 hda_nid_t nid = kcontrol->private_value;
3416 unsigned int val = !!ucontrol->value.integer.value[0];
3419 mutex_lock(&codec->spdif_mutex);
3420 change = codec->spdif_in_enable != val;
3422 codec->spdif_in_enable = val;
3423 snd_hda_codec_write_cache(codec, nid, 0,
3424 AC_VERB_SET_DIGI_CONVERT_1, val);
3426 mutex_unlock(&codec->spdif_mutex);
3430 static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
3431 struct snd_ctl_elem_value *ucontrol)
3433 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3434 hda_nid_t nid = kcontrol->private_value;
3438 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0);
3439 sbits = convert_to_spdif_status(val);
3440 ucontrol->value.iec958.status[0] = sbits;
3441 ucontrol->value.iec958.status[1] = sbits >> 8;
3442 ucontrol->value.iec958.status[2] = sbits >> 16;
3443 ucontrol->value.iec958.status[3] = sbits >> 24;
3447 static struct snd_kcontrol_new dig_in_ctls[] = {
3449 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3450 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH),
3451 .info = snd_hda_spdif_in_switch_info,
3452 .get = snd_hda_spdif_in_switch_get,
3453 .put = snd_hda_spdif_in_switch_put,
3456 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3457 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3458 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
3459 .info = snd_hda_spdif_mask_info,
3460 .get = snd_hda_spdif_in_status_get,
3466 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
3467 * @codec: the HDA codec
3468 * @nid: audio in widget NID
3470 * Creates controls related with the SPDIF input.
3471 * Called from each patch supporting the SPDIF in.
3473 * Returns 0 if successful, or a negative error code.
3475 int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
3478 struct snd_kcontrol *kctl;
3479 struct snd_kcontrol_new *dig_mix;
3482 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Capture Switch", 0);
3484 printk(KERN_ERR "hda_codec: too many IEC958 inputs\n");
3487 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
3488 kctl = snd_ctl_new1(dig_mix, codec);
3491 kctl->private_value = nid;
3492 err = snd_hda_ctl_add(codec, nid, kctl);
3496 codec->spdif_in_enable =
3497 snd_hda_codec_read(codec, nid, 0,
3498 AC_VERB_GET_DIGI_CONVERT_1, 0) &
3502 EXPORT_SYMBOL_HDA(snd_hda_create_spdif_in_ctls);
3508 /* build a 31bit cache key with the widget id and the command parameter */
3509 #define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
3510 #define get_cmd_cache_nid(key) ((key) & 0xff)
3511 #define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
3514 * snd_hda_codec_write_cache - send a single command with caching
3515 * @codec: the HDA codec
3516 * @nid: NID to send the command
3517 * @direct: direct flag
3518 * @verb: the verb to send
3519 * @parm: the parameter for the verb
3521 * Send a single command without waiting for response.
3523 * Returns 0 if successful, or a negative error code.
3525 int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
3526 int direct, unsigned int verb, unsigned int parm)
3529 struct hda_cache_head *c;
3531 unsigned int cache_only;
3533 cache_only = codec->cached_write;
3535 err = snd_hda_codec_write(codec, nid, direct, verb, parm);
3540 /* parm may contain the verb stuff for get/set amp */
3541 verb = verb | (parm >> 8);
3543 key = build_cmd_cache_key(nid, verb);
3544 mutex_lock(&codec->bus->cmd_mutex);
3545 c = get_alloc_hash(&codec->cmd_cache, key);
3548 c->dirty = cache_only;
3550 mutex_unlock(&codec->bus->cmd_mutex);
3553 EXPORT_SYMBOL_HDA(snd_hda_codec_write_cache);
3556 * snd_hda_codec_update_cache - check cache and write the cmd only when needed
3557 * @codec: the HDA codec
3558 * @nid: NID to send the command
3559 * @direct: direct flag
3560 * @verb: the verb to send
3561 * @parm: the parameter for the verb
3563 * This function works like snd_hda_codec_write_cache(), but it doesn't send
3564 * command if the parameter is already identical with the cached value.
3565 * If not, it sends the command and refreshes the cache.
3567 * Returns 0 if successful, or a negative error code.
3569 int snd_hda_codec_update_cache(struct hda_codec *codec, hda_nid_t nid,
3570 int direct, unsigned int verb, unsigned int parm)
3572 struct hda_cache_head *c;
3575 /* parm may contain the verb stuff for get/set amp */
3576 verb = verb | (parm >> 8);
3578 key = build_cmd_cache_key(nid, verb);
3579 mutex_lock(&codec->bus->cmd_mutex);
3580 c = get_hash(&codec->cmd_cache, key);
3581 if (c && c->val == parm) {
3582 mutex_unlock(&codec->bus->cmd_mutex);
3585 mutex_unlock(&codec->bus->cmd_mutex);
3586 return snd_hda_codec_write_cache(codec, nid, direct, verb, parm);
3588 EXPORT_SYMBOL_HDA(snd_hda_codec_update_cache);
3591 * snd_hda_codec_resume_cache - Resume the all commands from the cache
3592 * @codec: HD-audio codec
3594 * Execute all verbs recorded in the command caches to resume.
3596 void snd_hda_codec_resume_cache(struct hda_codec *codec)
3600 mutex_lock(&codec->hash_mutex);
3601 codec->cached_write = 0;
3602 for (i = 0; i < codec->cmd_cache.buf.used; i++) {
3603 struct hda_cache_head *buffer;
3606 buffer = snd_array_elem(&codec->cmd_cache.buf, i);
3613 mutex_unlock(&codec->hash_mutex);
3614 snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
3615 get_cmd_cache_cmd(key), buffer->val);
3616 mutex_lock(&codec->hash_mutex);
3618 mutex_unlock(&codec->hash_mutex);
3620 EXPORT_SYMBOL_HDA(snd_hda_codec_resume_cache);
3623 * snd_hda_sequence_write_cache - sequence writes with caching
3624 * @codec: the HDA codec
3625 * @seq: VERB array to send
3627 * Send the commands sequentially from the given array.
3628 * Thte commands are recorded on cache for power-save and resume.
3629 * The array must be terminated with NID=0.
3631 void snd_hda_sequence_write_cache(struct hda_codec *codec,
3632 const struct hda_verb *seq)
3634 for (; seq->nid; seq++)
3635 snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
3638 EXPORT_SYMBOL_HDA(snd_hda_sequence_write_cache);
3640 void snd_hda_codec_set_power_to_all(struct hda_codec *codec, hda_nid_t fg,
3641 unsigned int power_state,
3642 bool eapd_workaround)
3644 hda_nid_t nid = codec->start_nid;
3647 for (i = 0; i < codec->num_nodes; i++, nid++) {
3648 unsigned int wcaps = get_wcaps(codec, nid);
3649 if (!(wcaps & AC_WCAP_POWER))
3651 /* don't power down the widget if it controls eapd and
3652 * EAPD_BTLENABLE is set.
3654 if (eapd_workaround && power_state == AC_PWRST_D3 &&
3655 get_wcaps_type(wcaps) == AC_WID_PIN &&
3656 (snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD)) {
3657 int eapd = snd_hda_codec_read(codec, nid, 0,
3658 AC_VERB_GET_EAPD_BTLENABLE, 0);
3662 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE,
3666 EXPORT_SYMBOL_HDA(snd_hda_codec_set_power_to_all);
3669 * supported power states check
3671 static bool snd_hda_codec_get_supported_ps(struct hda_codec *codec, hda_nid_t fg,
3672 unsigned int power_state)
3674 int sup = snd_hda_param_read(codec, fg, AC_PAR_POWER_STATE);
3678 if (sup & power_state)
3685 * wait until the state is reached, returns the current state
3687 static unsigned int hda_sync_power_state(struct hda_codec *codec,
3689 unsigned int power_state)
3691 unsigned long end_time = jiffies + msecs_to_jiffies(500);
3692 unsigned int state, actual_state;
3695 state = snd_hda_codec_read(codec, fg, 0,
3696 AC_VERB_GET_POWER_STATE, 0);
3697 if (state & AC_PWRST_ERROR)
3699 actual_state = (state >> 4) & 0x0f;
3700 if (actual_state == power_state)
3702 if (time_after_eq(jiffies, end_time))
3704 /* wait until the codec reachs to the target state */
3711 * set power state of the codec, and return the power state
3713 static unsigned int hda_set_power_state(struct hda_codec *codec,
3714 unsigned int power_state)
3716 hda_nid_t fg = codec->afg ? codec->afg : codec->mfg;
3720 /* this delay seems necessary to avoid click noise at power-down */
3721 if (power_state == AC_PWRST_D3) {
3722 /* transition time less than 10ms for power down */
3723 msleep(codec->epss ? 10 : 100);
3726 /* repeat power states setting at most 10 times*/
3727 for (count = 0; count < 10; count++) {
3728 if (codec->patch_ops.set_power_state)
3729 codec->patch_ops.set_power_state(codec, fg,
3732 snd_hda_codec_read(codec, fg, 0,
3733 AC_VERB_SET_POWER_STATE,
3735 snd_hda_codec_set_power_to_all(codec, fg, power_state,
3738 state = hda_sync_power_state(codec, fg, power_state);
3739 if (!(state & AC_PWRST_ERROR))
3746 #ifdef CONFIG_SND_HDA_HWDEP
3747 /* execute additional init verbs */
3748 static void hda_exec_init_verbs(struct hda_codec *codec)
3750 if (codec->init_verbs.list)
3751 snd_hda_sequence_write(codec, codec->init_verbs.list);
3754 static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
3759 * call suspend and power-down; used both from PM and power-save
3760 * this function returns the power state in the end
3762 static unsigned int hda_call_codec_suspend(struct hda_codec *codec, bool in_wq)
3768 if (codec->patch_ops.suspend)
3769 codec->patch_ops.suspend(codec);
3770 hda_cleanup_all_streams(codec);
3771 state = hda_set_power_state(codec, AC_PWRST_D3);
3772 /* Cancel delayed work if we aren't currently running from it. */
3774 cancel_delayed_work_sync(&codec->power_work);
3775 spin_lock(&codec->power_lock);
3776 snd_hda_update_power_acct(codec);
3777 trace_hda_power_down(codec);
3778 codec->power_on = 0;
3779 codec->power_transition = 0;
3780 codec->power_jiffies = jiffies;
3781 spin_unlock(&codec->power_lock);
3786 /* mark all entries of cmd and amp caches dirty */
3787 static void hda_mark_cmd_cache_dirty(struct hda_codec *codec)
3790 for (i = 0; i < codec->cmd_cache.buf.used; i++) {
3791 struct hda_cache_head *cmd;
3792 cmd = snd_array_elem(&codec->cmd_cache.buf, i);
3795 for (i = 0; i < codec->amp_cache.buf.used; i++) {
3796 struct hda_amp_info *amp;
3797 amp = snd_array_elem(&codec->cmd_cache.buf, i);
3798 amp->head.dirty = 1;
3803 * kick up codec; used both from PM and power-save
3805 static void hda_call_codec_resume(struct hda_codec *codec)
3809 hda_mark_cmd_cache_dirty(codec);
3811 /* set as if powered on for avoiding re-entering the resume
3812 * in the resume / power-save sequence
3814 hda_keep_power_on(codec);
3815 hda_set_power_state(codec, AC_PWRST_D0);
3816 restore_shutup_pins(codec);
3817 hda_exec_init_verbs(codec);
3818 if (codec->patch_ops.resume)
3819 codec->patch_ops.resume(codec);
3821 if (codec->patch_ops.init)
3822 codec->patch_ops.init(codec);
3823 snd_hda_codec_resume_amp(codec);
3824 snd_hda_codec_resume_cache(codec);
3827 if (codec->jackpoll_interval)
3828 hda_jackpoll_work(&codec->jackpoll_work.work);
3830 snd_hda_jack_set_dirty_all(codec);
3831 snd_hda_jack_report_sync(codec);
3835 snd_hda_power_down(codec); /* flag down before returning */
3837 #endif /* CONFIG_PM */
3841 * snd_hda_build_controls - build mixer controls
3844 * Creates mixer controls for each codec included in the bus.
3846 * Returns 0 if successful, otherwise a negative error code.
3848 int snd_hda_build_controls(struct hda_bus *bus)
3850 struct hda_codec *codec;
3852 list_for_each_entry(codec, &bus->codec_list, list) {
3853 int err = snd_hda_codec_build_controls(codec);
3855 printk(KERN_ERR "hda_codec: cannot build controls "
3856 "for #%d (error %d)\n", codec->addr, err);
3857 err = snd_hda_codec_reset(codec);
3860 "hda_codec: cannot revert codec\n");
3867 EXPORT_SYMBOL_HDA(snd_hda_build_controls);
3870 * add standard channel maps if not specified
3872 static int add_std_chmaps(struct hda_codec *codec)
3876 for (i = 0; i < codec->num_pcms; i++) {
3877 for (str = 0; str < 2; str++) {
3878 struct snd_pcm *pcm = codec->pcm_info[i].pcm;
3879 struct hda_pcm_stream *hinfo =
3880 &codec->pcm_info[i].stream[str];
3881 struct snd_pcm_chmap *chmap;
3882 const struct snd_pcm_chmap_elem *elem;
3884 if (codec->pcm_info[i].own_chmap)
3886 if (!pcm || !hinfo->substreams)
3888 elem = hinfo->chmap ? hinfo->chmap : snd_pcm_std_chmaps;
3889 err = snd_pcm_add_chmap_ctls(pcm, str, elem,
3890 hinfo->channels_max,
3894 chmap->channel_mask = SND_PCM_CHMAP_MASK_2468;
3900 /* default channel maps for 2.1 speakers;
3901 * since HD-audio supports only stereo, odd number channels are omitted
3903 const struct snd_pcm_chmap_elem snd_pcm_2_1_chmaps[] = {
3905 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } },
3907 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
3908 SNDRV_CHMAP_LFE, SNDRV_CHMAP_LFE } },
3911 EXPORT_SYMBOL_GPL(snd_pcm_2_1_chmaps);
3913 int snd_hda_codec_build_controls(struct hda_codec *codec)
3916 hda_exec_init_verbs(codec);
3917 /* continue to initialize... */
3918 if (codec->patch_ops.init)
3919 err = codec->patch_ops.init(codec);
3920 if (!err && codec->patch_ops.build_controls)
3921 err = codec->patch_ops.build_controls(codec);
3925 /* we create chmaps here instead of build_pcms */
3926 err = add_std_chmaps(codec);
3930 if (codec->jackpoll_interval)
3931 hda_jackpoll_work(&codec->jackpoll_work.work);
3933 snd_hda_jack_report_sync(codec); /* call at the last init point */
3940 struct hda_rate_tbl {
3942 unsigned int alsa_bits;
3943 unsigned int hda_fmt;
3946 /* rate = base * mult / div */
3947 #define HDA_RATE(base, mult, div) \
3948 (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \
3949 (((div) - 1) << AC_FMT_DIV_SHIFT))
3951 static struct hda_rate_tbl rate_bits[] = {
3952 /* rate in Hz, ALSA rate bitmask, HDA format value */
3954 /* autodetected value used in snd_hda_query_supported_pcm */
3955 { 8000, SNDRV_PCM_RATE_8000, HDA_RATE(48, 1, 6) },
3956 { 11025, SNDRV_PCM_RATE_11025, HDA_RATE(44, 1, 4) },
3957 { 16000, SNDRV_PCM_RATE_16000, HDA_RATE(48, 1, 3) },
3958 { 22050, SNDRV_PCM_RATE_22050, HDA_RATE(44, 1, 2) },
3959 { 32000, SNDRV_PCM_RATE_32000, HDA_RATE(48, 2, 3) },
3960 { 44100, SNDRV_PCM_RATE_44100, HDA_RATE(44, 1, 1) },
3961 { 48000, SNDRV_PCM_RATE_48000, HDA_RATE(48, 1, 1) },
3962 { 88200, SNDRV_PCM_RATE_88200, HDA_RATE(44, 2, 1) },
3963 { 96000, SNDRV_PCM_RATE_96000, HDA_RATE(48, 2, 1) },
3964 { 176400, SNDRV_PCM_RATE_176400, HDA_RATE(44, 4, 1) },
3965 { 192000, SNDRV_PCM_RATE_192000, HDA_RATE(48, 4, 1) },
3966 #define AC_PAR_PCM_RATE_BITS 11
3967 /* up to bits 10, 384kHZ isn't supported properly */
3969 /* not autodetected value */
3970 { 9600, SNDRV_PCM_RATE_KNOT, HDA_RATE(48, 1, 5) },
3972 { 0 } /* terminator */
3976 * snd_hda_calc_stream_format - calculate format bitset
3977 * @rate: the sample rate
3978 * @channels: the number of channels
3979 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
3980 * @maxbps: the max. bps
3982 * Calculate the format bitset from the given rate, channels and th PCM format.
3984 * Return zero if invalid.
3986 unsigned int snd_hda_calc_stream_format(unsigned int rate,
3987 unsigned int channels,
3988 unsigned int format,
3989 unsigned int maxbps,
3990 unsigned short spdif_ctls)
3993 unsigned int val = 0;
3995 for (i = 0; rate_bits[i].hz; i++)
3996 if (rate_bits[i].hz == rate) {
3997 val = rate_bits[i].hda_fmt;
4000 if (!rate_bits[i].hz) {
4001 snd_printdd("invalid rate %d\n", rate);
4005 if (channels == 0 || channels > 8) {
4006 snd_printdd("invalid channels %d\n", channels);
4009 val |= channels - 1;
4011 switch (snd_pcm_format_width(format)) {
4013 val |= AC_FMT_BITS_8;
4016 val |= AC_FMT_BITS_16;
4021 if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE)
4022 val |= AC_FMT_BITS_32;
4023 else if (maxbps >= 24)
4024 val |= AC_FMT_BITS_24;
4026 val |= AC_FMT_BITS_20;
4029 snd_printdd("invalid format width %d\n",
4030 snd_pcm_format_width(format));
4034 if (spdif_ctls & AC_DIG1_NONAUDIO)
4035 val |= AC_FMT_TYPE_NON_PCM;
4039 EXPORT_SYMBOL_HDA(snd_hda_calc_stream_format);
4041 static unsigned int get_pcm_param(struct hda_codec *codec, hda_nid_t nid,
4044 unsigned int val = 0;
4045 if (nid != codec->afg &&
4046 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD))
4047 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
4048 if (!val || val == -1)
4049 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
4050 if (!val || val == -1)
4055 static unsigned int query_pcm_param(struct hda_codec *codec, hda_nid_t nid)
4057 return query_caps_hash(codec, nid, 0, HDA_HASH_PARPCM_KEY(nid),
4061 static unsigned int get_stream_param(struct hda_codec *codec, hda_nid_t nid,
4064 unsigned int streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
4065 if (!streams || streams == -1)
4066 streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
4067 if (!streams || streams == -1)
4072 static unsigned int query_stream_param(struct hda_codec *codec, hda_nid_t nid)
4074 return query_caps_hash(codec, nid, 0, HDA_HASH_PARSTR_KEY(nid),
4079 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
4080 * @codec: the HDA codec
4081 * @nid: NID to query
4082 * @ratesp: the pointer to store the detected rate bitflags
4083 * @formatsp: the pointer to store the detected formats
4084 * @bpsp: the pointer to store the detected format widths
4086 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
4087 * or @bsps argument is ignored.
4089 * Returns 0 if successful, otherwise a negative error code.
4091 int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
4092 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
4094 unsigned int i, val, wcaps;
4096 wcaps = get_wcaps(codec, nid);
4097 val = query_pcm_param(codec, nid);
4101 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
4103 rates |= rate_bits[i].alsa_bits;
4106 snd_printk(KERN_ERR "hda_codec: rates == 0 "
4107 "(nid=0x%x, val=0x%x, ovrd=%i)\n",
4109 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0);
4115 if (formatsp || bpsp) {
4117 unsigned int streams, bps;
4119 streams = query_stream_param(codec, nid);
4124 if (streams & AC_SUPFMT_PCM) {
4125 if (val & AC_SUPPCM_BITS_8) {
4126 formats |= SNDRV_PCM_FMTBIT_U8;
4129 if (val & AC_SUPPCM_BITS_16) {
4130 formats |= SNDRV_PCM_FMTBIT_S16_LE;
4133 if (wcaps & AC_WCAP_DIGITAL) {
4134 if (val & AC_SUPPCM_BITS_32)
4135 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
4136 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
4137 formats |= SNDRV_PCM_FMTBIT_S32_LE;
4138 if (val & AC_SUPPCM_BITS_24)
4140 else if (val & AC_SUPPCM_BITS_20)
4142 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
4143 AC_SUPPCM_BITS_32)) {
4144 formats |= SNDRV_PCM_FMTBIT_S32_LE;
4145 if (val & AC_SUPPCM_BITS_32)
4147 else if (val & AC_SUPPCM_BITS_24)
4149 else if (val & AC_SUPPCM_BITS_20)
4153 #if 0 /* FIXME: CS4206 doesn't work, which is the only codec supporting float */
4154 if (streams & AC_SUPFMT_FLOAT32) {
4155 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
4160 if (streams == AC_SUPFMT_AC3) {
4161 /* should be exclusive */
4162 /* temporary hack: we have still no proper support
4163 * for the direct AC3 stream...
4165 formats |= SNDRV_PCM_FMTBIT_U8;
4169 snd_printk(KERN_ERR "hda_codec: formats == 0 "
4170 "(nid=0x%x, val=0x%x, ovrd=%i, "
4173 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0,
4178 *formatsp = formats;
4185 EXPORT_SYMBOL_HDA(snd_hda_query_supported_pcm);
4188 * snd_hda_is_supported_format - Check the validity of the format
4189 * @codec: HD-audio codec
4190 * @nid: NID to check
4191 * @format: the HD-audio format value to check
4193 * Check whether the given node supports the format value.
4195 * Returns 1 if supported, 0 if not.
4197 int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
4198 unsigned int format)
4201 unsigned int val = 0, rate, stream;
4203 val = query_pcm_param(codec, nid);
4207 rate = format & 0xff00;
4208 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
4209 if (rate_bits[i].hda_fmt == rate) {
4214 if (i >= AC_PAR_PCM_RATE_BITS)
4217 stream = query_stream_param(codec, nid);
4221 if (stream & AC_SUPFMT_PCM) {
4222 switch (format & 0xf0) {
4224 if (!(val & AC_SUPPCM_BITS_8))
4228 if (!(val & AC_SUPPCM_BITS_16))
4232 if (!(val & AC_SUPPCM_BITS_20))
4236 if (!(val & AC_SUPPCM_BITS_24))
4240 if (!(val & AC_SUPPCM_BITS_32))
4247 /* FIXME: check for float32 and AC3? */
4252 EXPORT_SYMBOL_HDA(snd_hda_is_supported_format);
4257 static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
4258 struct hda_codec *codec,
4259 struct snd_pcm_substream *substream)
4264 static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
4265 struct hda_codec *codec,
4266 unsigned int stream_tag,
4267 unsigned int format,
4268 struct snd_pcm_substream *substream)
4270 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4274 static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
4275 struct hda_codec *codec,
4276 struct snd_pcm_substream *substream)
4278 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
4282 static int set_pcm_default_values(struct hda_codec *codec,
4283 struct hda_pcm_stream *info)
4287 /* query support PCM information from the given NID */
4288 if (info->nid && (!info->rates || !info->formats)) {
4289 err = snd_hda_query_supported_pcm(codec, info->nid,
4290 info->rates ? NULL : &info->rates,
4291 info->formats ? NULL : &info->formats,
4292 info->maxbps ? NULL : &info->maxbps);
4296 if (info->ops.open == NULL)
4297 info->ops.open = hda_pcm_default_open_close;
4298 if (info->ops.close == NULL)
4299 info->ops.close = hda_pcm_default_open_close;
4300 if (info->ops.prepare == NULL) {
4301 if (snd_BUG_ON(!info->nid))
4303 info->ops.prepare = hda_pcm_default_prepare;
4305 if (info->ops.cleanup == NULL) {
4306 if (snd_BUG_ON(!info->nid))
4308 info->ops.cleanup = hda_pcm_default_cleanup;
4314 * codec prepare/cleanup entries
4316 int snd_hda_codec_prepare(struct hda_codec *codec,
4317 struct hda_pcm_stream *hinfo,
4318 unsigned int stream,
4319 unsigned int format,
4320 struct snd_pcm_substream *substream)
4323 mutex_lock(&codec->bus->prepare_mutex);
4324 ret = hinfo->ops.prepare(hinfo, codec, stream, format, substream);
4326 purify_inactive_streams(codec);
4327 mutex_unlock(&codec->bus->prepare_mutex);
4330 EXPORT_SYMBOL_HDA(snd_hda_codec_prepare);
4332 void snd_hda_codec_cleanup(struct hda_codec *codec,
4333 struct hda_pcm_stream *hinfo,
4334 struct snd_pcm_substream *substream)
4336 mutex_lock(&codec->bus->prepare_mutex);
4337 hinfo->ops.cleanup(hinfo, codec, substream);
4338 mutex_unlock(&codec->bus->prepare_mutex);
4340 EXPORT_SYMBOL_HDA(snd_hda_codec_cleanup);
4343 const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = {
4344 "Audio", "SPDIF", "HDMI", "Modem"
4348 * get the empty PCM device number to assign
4350 * note the max device number is limited by HDA_MAX_PCMS, currently 10
4352 static int get_empty_pcm_device(struct hda_bus *bus, int type)
4354 /* audio device indices; not linear to keep compatibility */
4355 static int audio_idx[HDA_PCM_NTYPES][5] = {
4356 [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 },
4357 [HDA_PCM_TYPE_SPDIF] = { 1, -1 },
4358 [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 },
4359 [HDA_PCM_TYPE_MODEM] = { 6, -1 },
4363 if (type >= HDA_PCM_NTYPES) {
4364 snd_printk(KERN_WARNING "Invalid PCM type %d\n", type);
4368 for (i = 0; audio_idx[type][i] >= 0 ; i++)
4369 if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits))
4370 return audio_idx[type][i];
4372 /* non-fixed slots starting from 10 */
4373 for (i = 10; i < 32; i++) {
4374 if (!test_and_set_bit(i, bus->pcm_dev_bits))
4378 snd_printk(KERN_WARNING "Too many %s devices\n",
4379 snd_hda_pcm_type_name[type]);
4384 * attach a new PCM stream
4386 static int snd_hda_attach_pcm(struct hda_codec *codec, struct hda_pcm *pcm)
4388 struct hda_bus *bus = codec->bus;
4389 struct hda_pcm_stream *info;
4392 if (snd_BUG_ON(!pcm->name))
4394 for (stream = 0; stream < 2; stream++) {
4395 info = &pcm->stream[stream];
4396 if (info->substreams) {
4397 err = set_pcm_default_values(codec, info);
4402 return bus->ops.attach_pcm(bus, codec, pcm);
4405 /* assign all PCMs of the given codec */
4406 int snd_hda_codec_build_pcms(struct hda_codec *codec)
4411 if (!codec->num_pcms) {
4412 if (!codec->patch_ops.build_pcms)
4414 err = codec->patch_ops.build_pcms(codec);
4416 printk(KERN_ERR "hda_codec: cannot build PCMs"
4417 "for #%d (error %d)\n", codec->addr, err);
4418 err = snd_hda_codec_reset(codec);
4421 "hda_codec: cannot revert codec\n");
4426 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
4427 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
4430 if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
4431 continue; /* no substreams assigned */
4434 dev = get_empty_pcm_device(codec->bus, cpcm->pcm_type);
4436 continue; /* no fatal error */
4438 err = snd_hda_attach_pcm(codec, cpcm);
4440 printk(KERN_ERR "hda_codec: cannot attach "
4441 "PCM stream %d for codec #%d\n",
4443 continue; /* no fatal error */
4451 * snd_hda_build_pcms - build PCM information
4454 * Create PCM information for each codec included in the bus.
4456 * The build_pcms codec patch is requested to set up codec->num_pcms and
4457 * codec->pcm_info properly. The array is referred by the top-level driver
4458 * to create its PCM instances.
4459 * The allocated codec->pcm_info should be released in codec->patch_ops.free
4462 * At least, substreams, channels_min and channels_max must be filled for
4463 * each stream. substreams = 0 indicates that the stream doesn't exist.
4464 * When rates and/or formats are zero, the supported values are queried
4465 * from the given nid. The nid is used also by the default ops.prepare
4466 * and ops.cleanup callbacks.
4468 * The driver needs to call ops.open in its open callback. Similarly,
4469 * ops.close is supposed to be called in the close callback.
4470 * ops.prepare should be called in the prepare or hw_params callback
4471 * with the proper parameters for set up.
4472 * ops.cleanup should be called in hw_free for clean up of streams.
4474 * This function returns 0 if successful, or a negative error code.
4476 int snd_hda_build_pcms(struct hda_bus *bus)
4478 struct hda_codec *codec;
4480 list_for_each_entry(codec, &bus->codec_list, list) {
4481 int err = snd_hda_codec_build_pcms(codec);
4487 EXPORT_SYMBOL_HDA(snd_hda_build_pcms);
4490 * snd_hda_check_board_config - compare the current codec with the config table
4491 * @codec: the HDA codec
4492 * @num_configs: number of config enums
4493 * @models: array of model name strings
4494 * @tbl: configuration table, terminated by null entries
4496 * Compares the modelname or PCI subsystem id of the current codec with the
4497 * given configuration table. If a matching entry is found, returns its
4498 * config value (supposed to be 0 or positive).
4500 * If no entries are matching, the function returns a negative value.
4502 int snd_hda_check_board_config(struct hda_codec *codec,
4503 int num_configs, const char * const *models,
4504 const struct snd_pci_quirk *tbl)
4506 if (codec->modelname && models) {
4508 for (i = 0; i < num_configs; i++) {
4510 !strcmp(codec->modelname, models[i])) {
4511 snd_printd(KERN_INFO "hda_codec: model '%s' is "
4512 "selected\n", models[i]);
4518 if (!codec->bus->pci || !tbl)
4521 tbl = snd_pci_quirk_lookup(codec->bus->pci, tbl);
4524 if (tbl->value >= 0 && tbl->value < num_configs) {
4525 #ifdef CONFIG_SND_DEBUG_VERBOSE
4527 const char *model = NULL;
4529 model = models[tbl->value];
4531 sprintf(tmp, "#%d", tbl->value);
4534 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
4535 "for config %x:%x (%s)\n",
4536 model, tbl->subvendor, tbl->subdevice,
4537 (tbl->name ? tbl->name : "Unknown device"));
4543 EXPORT_SYMBOL_HDA(snd_hda_check_board_config);
4546 * snd_hda_check_board_codec_sid_config - compare the current codec
4547 subsystem ID with the
4550 This is important for Gateway notebooks with SB450 HDA Audio
4551 where the vendor ID of the PCI device is:
4552 ATI Technologies Inc SB450 HDA Audio [1002:437b]
4553 and the vendor/subvendor are found only at the codec.
4555 * @codec: the HDA codec
4556 * @num_configs: number of config enums
4557 * @models: array of model name strings
4558 * @tbl: configuration table, terminated by null entries
4560 * Compares the modelname or PCI subsystem id of the current codec with the
4561 * given configuration table. If a matching entry is found, returns its
4562 * config value (supposed to be 0 or positive).
4564 * If no entries are matching, the function returns a negative value.
4566 int snd_hda_check_board_codec_sid_config(struct hda_codec *codec,
4567 int num_configs, const char * const *models,
4568 const struct snd_pci_quirk *tbl)
4570 const struct snd_pci_quirk *q;
4572 /* Search for codec ID */
4573 for (q = tbl; q->subvendor; q++) {
4574 unsigned int mask = 0xffff0000 | q->subdevice_mask;
4575 unsigned int id = (q->subdevice | (q->subvendor << 16)) & mask;
4576 if ((codec->subsystem_id & mask) == id)
4585 if (tbl->value >= 0 && tbl->value < num_configs) {
4586 #ifdef CONFIG_SND_DEBUG_VERBOSE
4588 const char *model = NULL;
4590 model = models[tbl->value];
4592 sprintf(tmp, "#%d", tbl->value);
4595 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
4596 "for config %x:%x (%s)\n",
4597 model, tbl->subvendor, tbl->subdevice,
4598 (tbl->name ? tbl->name : "Unknown device"));
4604 EXPORT_SYMBOL_HDA(snd_hda_check_board_codec_sid_config);
4607 * snd_hda_add_new_ctls - create controls from the array
4608 * @codec: the HDA codec
4609 * @knew: the array of struct snd_kcontrol_new
4611 * This helper function creates and add new controls in the given array.
4612 * The array must be terminated with an empty entry as terminator.
4614 * Returns 0 if successful, or a negative error code.
4616 int snd_hda_add_new_ctls(struct hda_codec *codec,
4617 const struct snd_kcontrol_new *knew)
4621 for (; knew->name; knew++) {
4622 struct snd_kcontrol *kctl;
4623 int addr = 0, idx = 0;
4624 if (knew->iface == -1) /* skip this codec private value */
4627 kctl = snd_ctl_new1(knew, codec);
4631 kctl->id.device = addr;
4633 kctl->id.index = idx;
4634 err = snd_hda_ctl_add(codec, 0, kctl);
4637 /* try first with another device index corresponding to
4638 * the codec addr; if it still fails (or it's the
4639 * primary codec), then try another control index
4641 if (!addr && codec->addr)
4643 else if (!idx && !knew->index) {
4644 idx = find_empty_mixer_ctl_idx(codec,
4654 EXPORT_SYMBOL_HDA(snd_hda_add_new_ctls);
4657 static void hda_power_work(struct work_struct *work)
4659 struct hda_codec *codec =
4660 container_of(work, struct hda_codec, power_work.work);
4661 struct hda_bus *bus = codec->bus;
4664 spin_lock(&codec->power_lock);
4665 if (codec->power_transition > 0) { /* during power-up sequence? */
4666 spin_unlock(&codec->power_lock);
4669 if (!codec->power_on || codec->power_count) {
4670 codec->power_transition = 0;
4671 spin_unlock(&codec->power_lock);
4674 spin_unlock(&codec->power_lock);
4676 state = hda_call_codec_suspend(codec, true);
4677 codec->pm_down_notified = 0;
4678 if (!bus->power_keep_link_on && (state & AC_PWRST_CLK_STOP_OK)) {
4679 codec->pm_down_notified = 1;
4680 hda_call_pm_notify(bus, false);
4684 static void hda_keep_power_on(struct hda_codec *codec)
4686 spin_lock(&codec->power_lock);
4687 codec->power_count++;
4688 codec->power_on = 1;
4689 codec->power_jiffies = jiffies;
4690 spin_unlock(&codec->power_lock);
4693 /* update the power on/off account with the current jiffies */
4694 void snd_hda_update_power_acct(struct hda_codec *codec)
4696 unsigned long delta = jiffies - codec->power_jiffies;
4697 if (codec->power_on)
4698 codec->power_on_acct += delta;
4700 codec->power_off_acct += delta;
4701 codec->power_jiffies += delta;
4704 /* Transition to powered up, if wait_power_down then wait for a pending
4705 * transition to D3 to complete. A pending D3 transition is indicated
4706 * with power_transition == -1. */
4707 /* call this with codec->power_lock held! */
4708 static void __snd_hda_power_up(struct hda_codec *codec, bool wait_power_down)
4710 struct hda_bus *bus = codec->bus;
4712 /* Return if power_on or transitioning to power_on, unless currently
4714 if ((codec->power_on || codec->power_transition > 0) &&
4715 !(wait_power_down && codec->power_transition < 0))
4717 spin_unlock(&codec->power_lock);
4719 cancel_delayed_work_sync(&codec->power_work);
4721 spin_lock(&codec->power_lock);
4722 /* If the power down delayed work was cancelled above before starting,
4723 * then there is no need to go through power up here.
4725 if (codec->power_on) {
4726 if (codec->power_transition < 0)
4727 codec->power_transition = 0;
4731 trace_hda_power_up(codec);
4732 snd_hda_update_power_acct(codec);
4733 codec->power_on = 1;
4734 codec->power_jiffies = jiffies;
4735 codec->power_transition = 1; /* avoid reentrance */
4736 spin_unlock(&codec->power_lock);
4738 if (codec->pm_down_notified) {
4739 codec->pm_down_notified = 0;
4740 hda_call_pm_notify(bus, true);
4743 hda_call_codec_resume(codec);
4745 spin_lock(&codec->power_lock);
4746 codec->power_transition = 0;
4749 #define power_save(codec) \
4750 ((codec)->bus->power_save ? *(codec)->bus->power_save : 0)
4752 /* Transition to powered down */
4753 static void __snd_hda_power_down(struct hda_codec *codec)
4755 if (!codec->power_on || codec->power_count || codec->power_transition)
4758 if (power_save(codec)) {
4759 codec->power_transition = -1; /* avoid reentrance */
4760 queue_delayed_work(codec->bus->workq, &codec->power_work,
4761 msecs_to_jiffies(power_save(codec) * 1000));
4766 * snd_hda_power_save - Power-up/down/sync the codec
4767 * @codec: HD-audio codec
4768 * @delta: the counter delta to change
4770 * Change the power-up counter via @delta, and power up or down the hardware
4771 * appropriately. For the power-down, queue to the delayed action.
4772 * Passing zero to @delta means to synchronize the power state.
4774 void snd_hda_power_save(struct hda_codec *codec, int delta, bool d3wait)
4776 spin_lock(&codec->power_lock);
4777 codec->power_count += delta;
4778 trace_hda_power_count(codec);
4780 __snd_hda_power_up(codec, d3wait);
4782 __snd_hda_power_down(codec);
4783 spin_unlock(&codec->power_lock);
4785 EXPORT_SYMBOL_HDA(snd_hda_power_save);
4788 * snd_hda_check_amp_list_power - Check the amp list and update the power
4789 * @codec: HD-audio codec
4790 * @check: the object containing an AMP list and the status
4791 * @nid: NID to check / update
4793 * Check whether the given NID is in the amp list. If it's in the list,
4794 * check the current AMP status, and update the the power-status according
4795 * to the mute status.
4797 * This function is supposed to be set or called from the check_power_status
4800 int snd_hda_check_amp_list_power(struct hda_codec *codec,
4801 struct hda_loopback_check *check,
4804 const struct hda_amp_list *p;
4807 if (!check->amplist)
4809 for (p = check->amplist; p->nid; p++) {
4814 return 0; /* nothing changed */
4816 for (p = check->amplist; p->nid; p++) {
4817 for (ch = 0; ch < 2; ch++) {
4818 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
4820 if (!(v & HDA_AMP_MUTE) && v > 0) {
4821 if (!check->power_on) {
4822 check->power_on = 1;
4823 snd_hda_power_up(codec);
4829 if (check->power_on) {
4830 check->power_on = 0;
4831 snd_hda_power_down(codec);
4835 EXPORT_SYMBOL_HDA(snd_hda_check_amp_list_power);
4839 * Channel mode helper
4843 * snd_hda_ch_mode_info - Info callback helper for the channel mode enum
4845 int snd_hda_ch_mode_info(struct hda_codec *codec,
4846 struct snd_ctl_elem_info *uinfo,
4847 const struct hda_channel_mode *chmode,
4850 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4852 uinfo->value.enumerated.items = num_chmodes;
4853 if (uinfo->value.enumerated.item >= num_chmodes)
4854 uinfo->value.enumerated.item = num_chmodes - 1;
4855 sprintf(uinfo->value.enumerated.name, "%dch",
4856 chmode[uinfo->value.enumerated.item].channels);
4859 EXPORT_SYMBOL_HDA(snd_hda_ch_mode_info);
4862 * snd_hda_ch_mode_get - Get callback helper for the channel mode enum
4864 int snd_hda_ch_mode_get(struct hda_codec *codec,
4865 struct snd_ctl_elem_value *ucontrol,
4866 const struct hda_channel_mode *chmode,
4872 for (i = 0; i < num_chmodes; i++) {
4873 if (max_channels == chmode[i].channels) {
4874 ucontrol->value.enumerated.item[0] = i;
4880 EXPORT_SYMBOL_HDA(snd_hda_ch_mode_get);
4883 * snd_hda_ch_mode_put - Put callback helper for the channel mode enum
4885 int snd_hda_ch_mode_put(struct hda_codec *codec,
4886 struct snd_ctl_elem_value *ucontrol,
4887 const struct hda_channel_mode *chmode,
4893 mode = ucontrol->value.enumerated.item[0];
4894 if (mode >= num_chmodes)
4896 if (*max_channelsp == chmode[mode].channels)
4898 /* change the current channel setting */
4899 *max_channelsp = chmode[mode].channels;
4900 if (chmode[mode].sequence)
4901 snd_hda_sequence_write_cache(codec, chmode[mode].sequence);
4904 EXPORT_SYMBOL_HDA(snd_hda_ch_mode_put);
4911 * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum
4913 int snd_hda_input_mux_info(const struct hda_input_mux *imux,
4914 struct snd_ctl_elem_info *uinfo)
4918 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4920 uinfo->value.enumerated.items = imux->num_items;
4921 if (!imux->num_items)
4923 index = uinfo->value.enumerated.item;
4924 if (index >= imux->num_items)
4925 index = imux->num_items - 1;
4926 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
4929 EXPORT_SYMBOL_HDA(snd_hda_input_mux_info);
4932 * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum
4934 int snd_hda_input_mux_put(struct hda_codec *codec,
4935 const struct hda_input_mux *imux,
4936 struct snd_ctl_elem_value *ucontrol,
4938 unsigned int *cur_val)
4942 if (!imux->num_items)
4944 idx = ucontrol->value.enumerated.item[0];
4945 if (idx >= imux->num_items)
4946 idx = imux->num_items - 1;
4947 if (*cur_val == idx)
4949 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
4950 imux->items[idx].index);
4954 EXPORT_SYMBOL_HDA(snd_hda_input_mux_put);
4958 * process kcontrol info callback of a simple string enum array
4959 * when @num_items is 0 or @texts is NULL, assume a boolean enum array
4961 int snd_hda_enum_helper_info(struct snd_kcontrol *kcontrol,
4962 struct snd_ctl_elem_info *uinfo,
4963 int num_items, const char * const *texts)
4965 static const char * const texts_default[] = {
4966 "Disabled", "Enabled"
4969 if (!texts || !num_items) {
4971 texts = texts_default;
4974 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4976 uinfo->value.enumerated.items = num_items;
4977 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
4978 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
4979 strcpy(uinfo->value.enumerated.name,
4980 texts[uinfo->value.enumerated.item]);
4983 EXPORT_SYMBOL_HDA(snd_hda_enum_helper_info);
4986 * Multi-channel / digital-out PCM helper functions
4989 /* setup SPDIF output stream */
4990 static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
4991 unsigned int stream_tag, unsigned int format)
4993 struct hda_spdif_out *spdif;
4994 unsigned int curr_fmt;
4997 spdif = snd_hda_spdif_out_of_nid(codec, nid);
4998 curr_fmt = snd_hda_codec_read(codec, nid, 0,
4999 AC_VERB_GET_STREAM_FORMAT, 0);
5000 reset = codec->spdif_status_reset &&
5001 (spdif->ctls & AC_DIG1_ENABLE) &&
5004 /* turn off SPDIF if needed; otherwise the IEC958 bits won't be
5007 set_dig_out_convert(codec, nid,
5008 spdif->ctls & ~AC_DIG1_ENABLE & 0xff,
5010 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
5011 if (codec->slave_dig_outs) {
5013 for (d = codec->slave_dig_outs; *d; d++)
5014 snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
5017 /* turn on again (if needed) */
5019 set_dig_out_convert(codec, nid,
5020 spdif->ctls & 0xff, -1);
5023 static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
5025 snd_hda_codec_cleanup_stream(codec, nid);
5026 if (codec->slave_dig_outs) {
5028 for (d = codec->slave_dig_outs; *d; d++)
5029 snd_hda_codec_cleanup_stream(codec, *d);
5034 * snd_hda_bus_reboot_notify - call the reboot notifier of each codec
5035 * @bus: HD-audio bus
5037 void snd_hda_bus_reboot_notify(struct hda_bus *bus)
5039 struct hda_codec *codec;
5043 list_for_each_entry(codec, &bus->codec_list, list) {
5044 if (hda_codec_is_power_on(codec) &&
5045 codec->patch_ops.reboot_notify)
5046 codec->patch_ops.reboot_notify(codec);
5049 EXPORT_SYMBOL_HDA(snd_hda_bus_reboot_notify);
5052 * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode
5054 int snd_hda_multi_out_dig_open(struct hda_codec *codec,
5055 struct hda_multi_out *mout)
5057 mutex_lock(&codec->spdif_mutex);
5058 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
5059 /* already opened as analog dup; reset it once */
5060 cleanup_dig_out_stream(codec, mout->dig_out_nid);
5061 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
5062 mutex_unlock(&codec->spdif_mutex);
5065 EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_open);
5068 * snd_hda_multi_out_dig_prepare - prepare the digital out stream
5070 int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
5071 struct hda_multi_out *mout,
5072 unsigned int stream_tag,
5073 unsigned int format,
5074 struct snd_pcm_substream *substream)
5076 mutex_lock(&codec->spdif_mutex);
5077 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
5078 mutex_unlock(&codec->spdif_mutex);
5081 EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_prepare);
5084 * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream
5086 int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec,
5087 struct hda_multi_out *mout)
5089 mutex_lock(&codec->spdif_mutex);
5090 cleanup_dig_out_stream(codec, mout->dig_out_nid);
5091 mutex_unlock(&codec->spdif_mutex);
5094 EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_cleanup);
5097 * snd_hda_multi_out_dig_close - release the digital out stream
5099 int snd_hda_multi_out_dig_close(struct hda_codec *codec,
5100 struct hda_multi_out *mout)
5102 mutex_lock(&codec->spdif_mutex);
5103 mout->dig_out_used = 0;
5104 mutex_unlock(&codec->spdif_mutex);
5107 EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_close);
5110 * snd_hda_multi_out_analog_open - open analog outputs
5112 * Open analog outputs and set up the hw-constraints.
5113 * If the digital outputs can be opened as slave, open the digital
5116 int snd_hda_multi_out_analog_open(struct hda_codec *codec,
5117 struct hda_multi_out *mout,
5118 struct snd_pcm_substream *substream,
5119 struct hda_pcm_stream *hinfo)
5121 struct snd_pcm_runtime *runtime = substream->runtime;
5122 runtime->hw.channels_max = mout->max_channels;
5123 if (mout->dig_out_nid) {
5124 if (!mout->analog_rates) {
5125 mout->analog_rates = hinfo->rates;
5126 mout->analog_formats = hinfo->formats;
5127 mout->analog_maxbps = hinfo->maxbps;
5129 runtime->hw.rates = mout->analog_rates;
5130 runtime->hw.formats = mout->analog_formats;
5131 hinfo->maxbps = mout->analog_maxbps;
5133 if (!mout->spdif_rates) {
5134 snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
5136 &mout->spdif_formats,
5137 &mout->spdif_maxbps);
5139 mutex_lock(&codec->spdif_mutex);
5140 if (mout->share_spdif) {
5141 if ((runtime->hw.rates & mout->spdif_rates) &&
5142 (runtime->hw.formats & mout->spdif_formats)) {
5143 runtime->hw.rates &= mout->spdif_rates;
5144 runtime->hw.formats &= mout->spdif_formats;
5145 if (mout->spdif_maxbps < hinfo->maxbps)
5146 hinfo->maxbps = mout->spdif_maxbps;
5148 mout->share_spdif = 0;
5149 /* FIXME: need notify? */
5152 mutex_unlock(&codec->spdif_mutex);
5154 return snd_pcm_hw_constraint_step(substream->runtime, 0,
5155 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
5157 EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_open);
5160 * snd_hda_multi_out_analog_prepare - Preapre the analog outputs.
5162 * Set up the i/o for analog out.
5163 * When the digital out is available, copy the front out to digital out, too.
5165 int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
5166 struct hda_multi_out *mout,
5167 unsigned int stream_tag,
5168 unsigned int format,
5169 struct snd_pcm_substream *substream)
5171 const hda_nid_t *nids = mout->dac_nids;
5172 int chs = substream->runtime->channels;
5173 struct hda_spdif_out *spdif;
5176 mutex_lock(&codec->spdif_mutex);
5177 spdif = snd_hda_spdif_out_of_nid(codec, mout->dig_out_nid);
5178 if (mout->dig_out_nid && mout->share_spdif &&
5179 mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
5181 snd_hda_is_supported_format(codec, mout->dig_out_nid,
5183 !(spdif->status & IEC958_AES0_NONAUDIO)) {
5184 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
5185 setup_dig_out_stream(codec, mout->dig_out_nid,
5186 stream_tag, format);
5188 mout->dig_out_used = 0;
5189 cleanup_dig_out_stream(codec, mout->dig_out_nid);
5192 mutex_unlock(&codec->spdif_mutex);
5195 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
5197 if (!mout->no_share_stream &&
5198 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
5199 /* headphone out will just decode front left/right (stereo) */
5200 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
5202 /* extra outputs copied from front */
5203 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
5204 if (!mout->no_share_stream && mout->hp_out_nid[i])
5205 snd_hda_codec_setup_stream(codec,
5206 mout->hp_out_nid[i],
5207 stream_tag, 0, format);
5208 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
5209 if (!mout->no_share_stream && mout->extra_out_nid[i])
5210 snd_hda_codec_setup_stream(codec,
5211 mout->extra_out_nid[i],
5212 stream_tag, 0, format);
5215 for (i = 1; i < mout->num_dacs; i++) {
5216 if (chs >= (i + 1) * 2) /* independent out */
5217 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
5219 else if (!mout->no_share_stream) /* copy front */
5220 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
5225 EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_prepare);
5228 * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out
5230 int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
5231 struct hda_multi_out *mout)
5233 const hda_nid_t *nids = mout->dac_nids;
5236 for (i = 0; i < mout->num_dacs; i++)
5237 snd_hda_codec_cleanup_stream(codec, nids[i]);
5239 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
5240 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
5241 if (mout->hp_out_nid[i])
5242 snd_hda_codec_cleanup_stream(codec,
5243 mout->hp_out_nid[i]);
5244 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
5245 if (mout->extra_out_nid[i])
5246 snd_hda_codec_cleanup_stream(codec,
5247 mout->extra_out_nid[i]);
5248 mutex_lock(&codec->spdif_mutex);
5249 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
5250 cleanup_dig_out_stream(codec, mout->dig_out_nid);
5251 mout->dig_out_used = 0;
5253 mutex_unlock(&codec->spdif_mutex);
5256 EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_cleanup);
5259 * snd_hda_get_default_vref - Get the default (mic) VREF pin bits
5261 * Guess the suitable VREF pin bits to be set as the pin-control value.
5262 * Note: the function doesn't set the AC_PINCTL_IN_EN bit.
5264 unsigned int snd_hda_get_default_vref(struct hda_codec *codec, hda_nid_t pin)
5266 unsigned int pincap;
5267 unsigned int oldval;
5268 oldval = snd_hda_codec_read(codec, pin, 0,
5269 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
5270 pincap = snd_hda_query_pin_caps(codec, pin);
5271 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
5272 /* Exception: if the default pin setup is vref50, we give it priority */
5273 if ((pincap & AC_PINCAP_VREF_80) && oldval != PIN_VREF50)
5274 return AC_PINCTL_VREF_80;
5275 else if (pincap & AC_PINCAP_VREF_50)
5276 return AC_PINCTL_VREF_50;
5277 else if (pincap & AC_PINCAP_VREF_100)
5278 return AC_PINCTL_VREF_100;
5279 else if (pincap & AC_PINCAP_VREF_GRD)
5280 return AC_PINCTL_VREF_GRD;
5281 return AC_PINCTL_VREF_HIZ;
5283 EXPORT_SYMBOL_HDA(snd_hda_get_default_vref);
5285 /* correct the pin ctl value for matching with the pin cap */
5286 unsigned int snd_hda_correct_pin_ctl(struct hda_codec *codec,
5287 hda_nid_t pin, unsigned int val)
5289 static unsigned int cap_lists[][2] = {
5290 { AC_PINCTL_VREF_100, AC_PINCAP_VREF_100 },
5291 { AC_PINCTL_VREF_80, AC_PINCAP_VREF_80 },
5292 { AC_PINCTL_VREF_50, AC_PINCAP_VREF_50 },
5293 { AC_PINCTL_VREF_GRD, AC_PINCAP_VREF_GRD },
5299 cap = snd_hda_query_pin_caps(codec, pin);
5301 return val; /* don't know what to do... */
5303 if (val & AC_PINCTL_OUT_EN) {
5304 if (!(cap & AC_PINCAP_OUT))
5305 val &= ~(AC_PINCTL_OUT_EN | AC_PINCTL_HP_EN);
5306 else if ((val & AC_PINCTL_HP_EN) && !(cap & AC_PINCAP_HP_DRV))
5307 val &= ~AC_PINCTL_HP_EN;
5310 if (val & AC_PINCTL_IN_EN) {
5311 if (!(cap & AC_PINCAP_IN))
5312 val &= ~(AC_PINCTL_IN_EN | AC_PINCTL_VREFEN);
5314 unsigned int vcap, vref;
5316 vcap = (cap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
5317 vref = val & AC_PINCTL_VREFEN;
5318 for (i = 0; i < ARRAY_SIZE(cap_lists); i++) {
5319 if (vref == cap_lists[i][0] &&
5320 !(vcap & cap_lists[i][1])) {
5321 if (i == ARRAY_SIZE(cap_lists) - 1)
5322 vref = AC_PINCTL_VREF_HIZ;
5324 vref = cap_lists[i + 1][0];
5327 val &= ~AC_PINCTL_VREFEN;
5334 EXPORT_SYMBOL_HDA(snd_hda_correct_pin_ctl);
5336 int _snd_hda_set_pin_ctl(struct hda_codec *codec, hda_nid_t pin,
5337 unsigned int val, bool cached)
5339 val = snd_hda_correct_pin_ctl(codec, pin, val);
5340 snd_hda_codec_set_pin_target(codec, pin, val);
5342 return snd_hda_codec_update_cache(codec, pin, 0,
5343 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
5345 return snd_hda_codec_write(codec, pin, 0,
5346 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
5348 EXPORT_SYMBOL_HDA(_snd_hda_set_pin_ctl);
5351 * snd_hda_add_imux_item - Add an item to input_mux
5353 * When the same label is used already in the existing items, the number
5354 * suffix is appended to the label. This label index number is stored
5355 * to type_idx when non-NULL pointer is given.
5357 int snd_hda_add_imux_item(struct hda_input_mux *imux, const char *label,
5358 int index, int *type_idx)
5360 int i, label_idx = 0;
5361 if (imux->num_items >= HDA_MAX_NUM_INPUTS) {
5362 snd_printd(KERN_ERR "hda_codec: Too many imux items!\n");
5365 for (i = 0; i < imux->num_items; i++) {
5366 if (!strncmp(label, imux->items[i].label, strlen(label)))
5370 *type_idx = label_idx;
5372 snprintf(imux->items[imux->num_items].label,
5373 sizeof(imux->items[imux->num_items].label),
5374 "%s %d", label, label_idx);
5376 strlcpy(imux->items[imux->num_items].label, label,
5377 sizeof(imux->items[imux->num_items].label));
5378 imux->items[imux->num_items].index = index;
5382 EXPORT_SYMBOL_HDA(snd_hda_add_imux_item);
5391 * snd_hda_suspend - suspend the codecs
5394 * Returns 0 if successful.
5396 int snd_hda_suspend(struct hda_bus *bus)
5398 struct hda_codec *codec;
5400 list_for_each_entry(codec, &bus->codec_list, list) {
5401 cancel_delayed_work_sync(&codec->jackpoll_work);
5402 if (hda_codec_is_power_on(codec))
5403 hda_call_codec_suspend(codec, false);
5407 EXPORT_SYMBOL_HDA(snd_hda_suspend);
5410 * snd_hda_resume - resume the codecs
5413 * Returns 0 if successful.
5415 int snd_hda_resume(struct hda_bus *bus)
5417 struct hda_codec *codec;
5419 list_for_each_entry(codec, &bus->codec_list, list) {
5420 hda_call_codec_resume(codec);
5424 EXPORT_SYMBOL_HDA(snd_hda_resume);
5425 #endif /* CONFIG_PM */
5432 * snd_array_new - get a new element from the given array
5433 * @array: the array object
5435 * Get a new element from the given array. If it exceeds the
5436 * pre-allocated array size, re-allocate the array.
5438 * Returns NULL if allocation failed.
5440 void *snd_array_new(struct snd_array *array)
5442 if (snd_BUG_ON(!array->elem_size))
5444 if (array->used >= array->alloced) {
5445 int num = array->alloced + array->alloc_align;
5446 int size = (num + 1) * array->elem_size;
5447 int oldsize = array->alloced * array->elem_size;
5449 if (snd_BUG_ON(num >= 4096))
5451 nlist = krealloc(array->list, size, GFP_KERNEL);
5454 memset(nlist + oldsize, 0, size - oldsize);
5455 array->list = nlist;
5456 array->alloced = num;
5458 return snd_array_elem(array, array->used++);
5460 EXPORT_SYMBOL_HDA(snd_array_new);
5463 * snd_array_free - free the given array elements
5464 * @array: the array object
5466 void snd_array_free(struct snd_array *array)
5473 EXPORT_SYMBOL_HDA(snd_array_free);
5476 * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer
5477 * @pcm: PCM caps bits
5478 * @buf: the string buffer to write
5479 * @buflen: the max buffer length
5481 * used by hda_proc.c and hda_eld.c
5483 void snd_print_pcm_bits(int pcm, char *buf, int buflen)
5485 static unsigned int bits[] = { 8, 16, 20, 24, 32 };
5488 for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
5489 if (pcm & (AC_SUPPCM_BITS_8 << i))
5490 j += snprintf(buf + j, buflen - j, " %d", bits[i]);
5492 buf[j] = '\0'; /* necessary when j == 0 */
5494 EXPORT_SYMBOL_HDA(snd_print_pcm_bits);
5496 MODULE_DESCRIPTION("HDA codec core");
5497 MODULE_LICENSE("GPL");