2 * Universal Interface for Intel High Definition Audio Codec
4 * Copyright (c) 2004 Takashi Iwai <tiwai@suse.de>
7 * This driver is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This driver is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include <linux/init.h>
24 #include <linux/delay.h>
25 #include <linux/slab.h>
26 #include <linux/pci.h>
27 #include <linux/mutex.h>
28 #include <linux/module.h>
29 #include <sound/core.h>
30 #include "hda_codec.h"
31 #include <sound/asoundef.h>
32 #include <sound/tlv.h>
33 #include <sound/initval.h>
34 #include <sound/jack.h>
35 #include "hda_local.h"
38 #include <sound/hda_hwdep.h>
40 #define CREATE_TRACE_POINTS
41 #include "hda_trace.h"
44 * vendor / preset table
47 struct hda_vendor_id {
52 /* codec vendor labels */
53 static struct hda_vendor_id hda_vendor_ids[] = {
55 { 0x1013, "Cirrus Logic" },
56 { 0x1057, "Motorola" },
57 { 0x1095, "Silicon Image" },
59 { 0x10ec, "Realtek" },
60 { 0x1102, "Creative" },
64 { 0x11d4, "Analog Devices" },
65 { 0x13f6, "C-Media" },
66 { 0x14f1, "Conexant" },
67 { 0x17e8, "Chrontel" },
69 { 0x1aec, "Wolfson Microelectronics" },
70 { 0x434d, "C-Media" },
72 { 0x8384, "SigmaTel" },
76 static DEFINE_MUTEX(preset_mutex);
77 static LIST_HEAD(hda_preset_tables);
79 int snd_hda_add_codec_preset(struct hda_codec_preset_list *preset)
81 mutex_lock(&preset_mutex);
82 list_add_tail(&preset->list, &hda_preset_tables);
83 mutex_unlock(&preset_mutex);
86 EXPORT_SYMBOL_HDA(snd_hda_add_codec_preset);
88 int snd_hda_delete_codec_preset(struct hda_codec_preset_list *preset)
90 mutex_lock(&preset_mutex);
91 list_del(&preset->list);
92 mutex_unlock(&preset_mutex);
95 EXPORT_SYMBOL_HDA(snd_hda_delete_codec_preset);
98 #define codec_in_pm(codec) ((codec)->in_pm)
99 static void hda_power_work(struct work_struct *work);
100 static void hda_keep_power_on(struct hda_codec *codec);
101 #define hda_codec_is_power_on(codec) ((codec)->power_on)
102 static inline void hda_call_pm_notify(struct hda_bus *bus, bool power_up)
104 if (bus->ops.pm_notify)
105 bus->ops.pm_notify(bus, power_up);
108 #define codec_in_pm(codec) 0
109 static inline void hda_keep_power_on(struct hda_codec *codec) {}
110 #define hda_codec_is_power_on(codec) 1
111 #define hda_call_pm_notify(bus, state) {}
115 * snd_hda_get_jack_location - Give a location string of the jack
116 * @cfg: pin default config value
118 * Parse the pin default config value and returns the string of the
119 * jack location, e.g. "Rear", "Front", etc.
121 const char *snd_hda_get_jack_location(u32 cfg)
123 static char *bases[7] = {
124 "N/A", "Rear", "Front", "Left", "Right", "Top", "Bottom",
126 static unsigned char specials_idx[] = {
131 static char *specials[] = {
132 "Rear Panel", "Drive Bar",
133 "Riser", "HDMI", "ATAPI",
134 "Mobile-In", "Mobile-Out"
137 cfg = (cfg & AC_DEFCFG_LOCATION) >> AC_DEFCFG_LOCATION_SHIFT;
138 if ((cfg & 0x0f) < 7)
139 return bases[cfg & 0x0f];
140 for (i = 0; i < ARRAY_SIZE(specials_idx); i++) {
141 if (cfg == specials_idx[i])
146 EXPORT_SYMBOL_HDA(snd_hda_get_jack_location);
149 * snd_hda_get_jack_connectivity - Give a connectivity string of the jack
150 * @cfg: pin default config value
152 * Parse the pin default config value and returns the string of the
153 * jack connectivity, i.e. external or internal connection.
155 const char *snd_hda_get_jack_connectivity(u32 cfg)
157 static char *jack_locations[4] = { "Ext", "Int", "Sep", "Oth" };
159 return jack_locations[(cfg >> (AC_DEFCFG_LOCATION_SHIFT + 4)) & 3];
161 EXPORT_SYMBOL_HDA(snd_hda_get_jack_connectivity);
164 * snd_hda_get_jack_type - Give a type string of the jack
165 * @cfg: pin default config value
167 * Parse the pin default config value and returns the string of the
168 * jack type, i.e. the purpose of the jack, such as Line-Out or CD.
170 const char *snd_hda_get_jack_type(u32 cfg)
172 static char *jack_types[16] = {
173 "Line Out", "Speaker", "HP Out", "CD",
174 "SPDIF Out", "Digital Out", "Modem Line", "Modem Hand",
175 "Line In", "Aux", "Mic", "Telephony",
176 "SPDIF In", "Digital In", "Reserved", "Other"
179 return jack_types[(cfg & AC_DEFCFG_DEVICE)
180 >> AC_DEFCFG_DEVICE_SHIFT];
182 EXPORT_SYMBOL_HDA(snd_hda_get_jack_type);
185 * Compose a 32bit command word to be sent to the HD-audio controller
187 static inline unsigned int
188 make_codec_cmd(struct hda_codec *codec, hda_nid_t nid, int 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)
386 hda_nid_t *result = list;
389 len = snd_hda_get_raw_connections(codec, nid, list, ARRAY_SIZE(list));
390 if (len == -ENOSPC) {
391 len = snd_hda_get_num_raw_conns(codec, nid);
392 result = kmalloc(sizeof(hda_nid_t) * len, GFP_KERNEL);
395 len = snd_hda_get_raw_connections(codec, nid, result, len);
398 len = snd_hda_override_conn_list(codec, nid, len, result);
405 * snd_hda_get_conn_list - get connection list
406 * @codec: the HDA codec
408 * @len: number of connection list entries
409 * @listp: the pointer to store NID list
411 * Parses the connection list of the given widget and stores the pointer
412 * to the list of NIDs.
414 * Returns the number of connections, or a negative error code.
416 * Note that the returned pointer isn't protected against the list
417 * modification. If snd_hda_override_conn_list() might be called
418 * concurrently, protect with a mutex appropriately.
420 int snd_hda_get_conn_list(struct hda_codec *codec, hda_nid_t nid,
421 const hda_nid_t **listp)
427 const struct hda_conn_list *p;
429 /* if the connection-list is already cached, read it */
430 p = lookup_conn_list(codec, nid);
436 if (snd_BUG_ON(added))
439 err = read_and_add_raw_conns(codec, nid);
445 EXPORT_SYMBOL_HDA(snd_hda_get_conn_list);
448 * snd_hda_get_connections - copy connection list
449 * @codec: the HDA codec
451 * @conn_list: connection list array; when NULL, checks only the size
452 * @max_conns: max. number of connections to store
454 * Parses the connection list of the given widget and stores the list
457 * Returns the number of connections, or a negative error code.
459 int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
460 hda_nid_t *conn_list, int max_conns)
462 const hda_nid_t *list;
463 int len = snd_hda_get_conn_list(codec, nid, &list);
465 if (len > 0 && conn_list) {
466 if (len > max_conns) {
467 snd_printk(KERN_ERR "hda_codec: "
468 "Too many connections %d for NID 0x%x\n",
472 memcpy(conn_list, list, len * sizeof(hda_nid_t));
477 EXPORT_SYMBOL_HDA(snd_hda_get_connections);
479 /* return CONNLIST_LEN parameter of the given widget */
480 static unsigned int get_num_conns(struct hda_codec *codec, hda_nid_t nid)
482 unsigned int wcaps = get_wcaps(codec, nid);
485 if (!(wcaps & AC_WCAP_CONN_LIST) &&
486 get_wcaps_type(wcaps) != AC_WID_VOL_KNB)
489 parm = snd_hda_param_read(codec, nid, AC_PAR_CONNLIST_LEN);
495 int snd_hda_get_num_raw_conns(struct hda_codec *codec, hda_nid_t nid)
497 return snd_hda_get_raw_connections(codec, nid, NULL, 0);
501 * snd_hda_get_raw_connections - copy connection list without cache
502 * @codec: the HDA codec
504 * @conn_list: connection list array
505 * @max_conns: max. number of connections to store
507 * Like snd_hda_get_connections(), copy the connection list but without
508 * checking through the connection-list cache.
509 * Currently called only from hda_proc.c, so not exported.
511 int snd_hda_get_raw_connections(struct hda_codec *codec, hda_nid_t nid,
512 hda_nid_t *conn_list, int max_conns)
515 int i, conn_len, conns;
516 unsigned int shift, num_elems, mask;
520 parm = get_num_conns(codec, nid);
524 if (parm & AC_CLIST_LONG) {
533 conn_len = parm & AC_CLIST_LENGTH;
534 mask = (1 << (shift-1)) - 1;
537 return 0; /* no connection */
540 /* single connection */
541 parm = snd_hda_codec_read(codec, nid, 0,
542 AC_VERB_GET_CONNECT_LIST, 0);
543 if (parm == -1 && codec->bus->rirb_error)
546 conn_list[0] = parm & mask;
550 /* multi connection */
553 for (i = 0; i < conn_len; i++) {
557 if (i % num_elems == 0) {
558 parm = snd_hda_codec_read(codec, nid, 0,
559 AC_VERB_GET_CONNECT_LIST, i);
560 if (parm == -1 && codec->bus->rirb_error)
563 range_val = !!(parm & (1 << (shift-1))); /* ranges */
565 if (val == 0 && null_count++) { /* no second chance */
566 snd_printk(KERN_WARNING "hda_codec: "
567 "invalid CONNECT_LIST verb %x[%i]:%x\n",
573 /* ranges between the previous and this one */
574 if (!prev_nid || prev_nid >= val) {
575 snd_printk(KERN_WARNING "hda_codec: "
576 "invalid dep_range_val %x:%x\n",
580 for (n = prev_nid + 1; n <= val; n++) {
582 if (conns >= max_conns)
584 conn_list[conns] = n;
590 if (conns >= max_conns)
592 conn_list[conns] = val;
602 * snd_hda_override_conn_list - add/modify the connection-list to cache
603 * @codec: the HDA codec
605 * @len: number of connection list entries
606 * @list: the list of connection entries
608 * Add or modify the given connection-list to the cache. If the corresponding
609 * cache already exists, invalidate it and append a new one.
611 * Returns zero or a negative error code.
613 int snd_hda_override_conn_list(struct hda_codec *codec, hda_nid_t nid, int len,
614 const hda_nid_t *list)
616 struct hda_conn_list *p;
618 p = lookup_conn_list(codec, nid);
624 return add_conn_list(codec, nid, len, list);
626 EXPORT_SYMBOL_HDA(snd_hda_override_conn_list);
629 * snd_hda_get_conn_index - get the connection index of the given NID
630 * @codec: the HDA codec
631 * @mux: NID containing the list
632 * @nid: NID to select
633 * @recursive: 1 when searching NID recursively, otherwise 0
635 * Parses the connection list of the widget @mux and checks whether the
636 * widget @nid is present. If it is, return the connection index.
637 * Otherwise it returns -1.
639 int snd_hda_get_conn_index(struct hda_codec *codec, hda_nid_t mux,
640 hda_nid_t nid, int recursive)
642 const hda_nid_t *conn;
645 nums = snd_hda_get_conn_list(codec, mux, &conn);
646 for (i = 0; i < nums; i++)
651 if (recursive > 10) {
652 snd_printd("hda_codec: too deep connection for 0x%x\n", nid);
656 for (i = 0; i < nums; i++) {
657 unsigned int type = get_wcaps_type(get_wcaps(codec, conn[i]));
658 if (type == AC_WID_PIN || type == AC_WID_AUD_OUT)
660 if (snd_hda_get_conn_index(codec, conn[i], nid, recursive) >= 0)
665 EXPORT_SYMBOL_HDA(snd_hda_get_conn_index);
668 * snd_hda_queue_unsol_event - add an unsolicited event to queue
670 * @res: unsolicited event (lower 32bit of RIRB entry)
671 * @res_ex: codec addr and flags (upper 32bit or RIRB entry)
673 * Adds the given event to the queue. The events are processed in
674 * the workqueue asynchronously. Call this function in the interrupt
675 * hanlder when RIRB receives an unsolicited event.
677 * Returns 0 if successful, or a negative error code.
679 int snd_hda_queue_unsol_event(struct hda_bus *bus, u32 res, u32 res_ex)
681 struct hda_bus_unsolicited *unsol;
684 if (!bus || !bus->workq)
687 trace_hda_unsol_event(bus, res, res_ex);
692 wp = (unsol->wp + 1) % HDA_UNSOL_QUEUE_SIZE;
696 unsol->queue[wp] = res;
697 unsol->queue[wp + 1] = res_ex;
699 queue_work(bus->workq, &unsol->work);
703 EXPORT_SYMBOL_HDA(snd_hda_queue_unsol_event);
706 * process queued unsolicited events
708 static void process_unsol_events(struct work_struct *work)
710 struct hda_bus_unsolicited *unsol =
711 container_of(work, struct hda_bus_unsolicited, work);
712 struct hda_bus *bus = unsol->bus;
713 struct hda_codec *codec;
714 unsigned int rp, caddr, res;
716 while (unsol->rp != unsol->wp) {
717 rp = (unsol->rp + 1) % HDA_UNSOL_QUEUE_SIZE;
720 res = unsol->queue[rp];
721 caddr = unsol->queue[rp + 1];
722 if (!(caddr & (1 << 4))) /* no unsolicited event? */
724 codec = bus->caddr_tbl[caddr & 0x0f];
725 if (codec && codec->patch_ops.unsol_event)
726 codec->patch_ops.unsol_event(codec, res);
731 * initialize unsolicited queue
733 static int init_unsol_queue(struct hda_bus *bus)
735 struct hda_bus_unsolicited *unsol;
737 if (bus->unsol) /* already initialized */
740 unsol = kzalloc(sizeof(*unsol), GFP_KERNEL);
742 snd_printk(KERN_ERR "hda_codec: "
743 "can't allocate unsolicited queue\n");
746 INIT_WORK(&unsol->work, process_unsol_events);
755 static void snd_hda_codec_free(struct hda_codec *codec);
757 static int snd_hda_bus_free(struct hda_bus *bus)
759 struct hda_codec *codec, *n;
764 flush_workqueue(bus->workq);
767 list_for_each_entry_safe(codec, n, &bus->codec_list, list) {
768 snd_hda_codec_free(codec);
770 if (bus->ops.private_free)
771 bus->ops.private_free(bus);
773 destroy_workqueue(bus->workq);
778 static int snd_hda_bus_dev_free(struct snd_device *device)
780 struct hda_bus *bus = device->device_data;
782 return snd_hda_bus_free(bus);
785 #ifdef CONFIG_SND_HDA_HWDEP
786 static int snd_hda_bus_dev_register(struct snd_device *device)
788 struct hda_bus *bus = device->device_data;
789 struct hda_codec *codec;
790 list_for_each_entry(codec, &bus->codec_list, list) {
791 snd_hda_hwdep_add_sysfs(codec);
792 snd_hda_hwdep_add_power_sysfs(codec);
797 #define snd_hda_bus_dev_register NULL
801 * snd_hda_bus_new - create a HDA bus
802 * @card: the card entry
803 * @temp: the template for hda_bus information
804 * @busp: the pointer to store the created bus instance
806 * Returns 0 if successful, or a negative error code.
808 int snd_hda_bus_new(struct snd_card *card,
809 const struct hda_bus_template *temp,
810 struct hda_bus **busp)
814 static struct snd_device_ops dev_ops = {
815 .dev_register = snd_hda_bus_dev_register,
816 .dev_free = snd_hda_bus_dev_free,
819 if (snd_BUG_ON(!temp))
821 if (snd_BUG_ON(!temp->ops.command || !temp->ops.get_response))
827 bus = kzalloc(sizeof(*bus), GFP_KERNEL);
829 snd_printk(KERN_ERR "can't allocate struct hda_bus\n");
834 bus->private_data = temp->private_data;
835 bus->pci = temp->pci;
836 bus->modelname = temp->modelname;
837 bus->power_save = temp->power_save;
838 bus->ops = temp->ops;
840 mutex_init(&bus->cmd_mutex);
841 mutex_init(&bus->prepare_mutex);
842 INIT_LIST_HEAD(&bus->codec_list);
844 snprintf(bus->workq_name, sizeof(bus->workq_name),
845 "hd-audio%d", card->number);
846 bus->workq = create_singlethread_workqueue(bus->workq_name);
848 snd_printk(KERN_ERR "cannot create workqueue %s\n",
854 err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops);
856 snd_hda_bus_free(bus);
863 EXPORT_SYMBOL_HDA(snd_hda_bus_new);
865 #ifdef CONFIG_SND_HDA_GENERIC
866 #define is_generic_config(codec) \
867 (codec->modelname && !strcmp(codec->modelname, "generic"))
869 #define is_generic_config(codec) 0
873 #define HDA_MODREQ_MAX_COUNT 2 /* two request_modules()'s */
875 #define HDA_MODREQ_MAX_COUNT 0 /* all presets are statically linked */
879 * find a matching codec preset
881 static const struct hda_codec_preset *
882 find_codec_preset(struct hda_codec *codec)
884 struct hda_codec_preset_list *tbl;
885 const struct hda_codec_preset *preset;
886 unsigned int mod_requested = 0;
888 if (is_generic_config(codec))
889 return NULL; /* use the generic parser */
892 mutex_lock(&preset_mutex);
893 list_for_each_entry(tbl, &hda_preset_tables, list) {
894 if (!try_module_get(tbl->owner)) {
895 snd_printk(KERN_ERR "hda_codec: cannot module_get\n");
898 for (preset = tbl->preset; preset->id; preset++) {
899 u32 mask = preset->mask;
900 if (preset->afg && preset->afg != codec->afg)
902 if (preset->mfg && preset->mfg != codec->mfg)
906 if (preset->id == (codec->vendor_id & mask) &&
908 preset->rev == codec->revision_id)) {
909 mutex_unlock(&preset_mutex);
910 codec->owner = tbl->owner;
914 module_put(tbl->owner);
916 mutex_unlock(&preset_mutex);
918 if (mod_requested < HDA_MODREQ_MAX_COUNT) {
921 snprintf(name, sizeof(name), "snd-hda-codec-id:%08x",
924 snprintf(name, sizeof(name), "snd-hda-codec-id:%04x*",
925 (codec->vendor_id >> 16) & 0xffff);
926 request_module(name);
934 * get_codec_name - store the codec name
936 static int get_codec_name(struct hda_codec *codec)
938 const struct hda_vendor_id *c;
939 const char *vendor = NULL;
940 u16 vendor_id = codec->vendor_id >> 16;
943 if (codec->vendor_name)
946 for (c = hda_vendor_ids; c->id; c++) {
947 if (c->id == vendor_id) {
953 sprintf(tmp, "Generic %04x", vendor_id);
956 codec->vendor_name = kstrdup(vendor, GFP_KERNEL);
957 if (!codec->vendor_name)
961 if (codec->chip_name)
964 if (codec->preset && codec->preset->name)
965 codec->chip_name = kstrdup(codec->preset->name, GFP_KERNEL);
967 sprintf(tmp, "ID %x", codec->vendor_id & 0xffff);
968 codec->chip_name = kstrdup(tmp, GFP_KERNEL);
970 if (!codec->chip_name)
976 * look for an AFG and MFG nodes
978 static void setup_fg_nodes(struct hda_codec *codec)
980 int i, total_nodes, function_id;
983 total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid);
984 for (i = 0; i < total_nodes; i++, nid++) {
985 function_id = snd_hda_param_read(codec, nid,
986 AC_PAR_FUNCTION_TYPE);
987 switch (function_id & 0xff) {
988 case AC_GRP_AUDIO_FUNCTION:
990 codec->afg_function_id = function_id & 0xff;
991 codec->afg_unsol = (function_id >> 8) & 1;
993 case AC_GRP_MODEM_FUNCTION:
995 codec->mfg_function_id = function_id & 0xff;
996 codec->mfg_unsol = (function_id >> 8) & 1;
1005 * read widget caps for each widget and store in cache
1007 static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
1012 codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node,
1014 codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL);
1017 nid = codec->start_nid;
1018 for (i = 0; i < codec->num_nodes; i++, nid++)
1019 codec->wcaps[i] = snd_hda_param_read(codec, nid,
1020 AC_PAR_AUDIO_WIDGET_CAP);
1024 /* read all pin default configurations and save codec->init_pins */
1025 static int read_pin_defaults(struct hda_codec *codec)
1028 hda_nid_t nid = codec->start_nid;
1030 for (i = 0; i < codec->num_nodes; i++, nid++) {
1031 struct hda_pincfg *pin;
1032 unsigned int wcaps = get_wcaps(codec, nid);
1033 unsigned int wid_type = get_wcaps_type(wcaps);
1034 if (wid_type != AC_WID_PIN)
1036 pin = snd_array_new(&codec->init_pins);
1040 pin->cfg = snd_hda_codec_read(codec, nid, 0,
1041 AC_VERB_GET_CONFIG_DEFAULT, 0);
1042 pin->ctrl = snd_hda_codec_read(codec, nid, 0,
1043 AC_VERB_GET_PIN_WIDGET_CONTROL,
1049 /* look up the given pin config list and return the item matching with NID */
1050 static struct hda_pincfg *look_up_pincfg(struct hda_codec *codec,
1051 struct snd_array *array,
1055 for (i = 0; i < array->used; i++) {
1056 struct hda_pincfg *pin = snd_array_elem(array, i);
1057 if (pin->nid == nid)
1063 /* set the current pin config value for the given NID.
1064 * the value is cached, and read via snd_hda_codec_get_pincfg()
1066 int snd_hda_add_pincfg(struct hda_codec *codec, struct snd_array *list,
1067 hda_nid_t nid, unsigned int cfg)
1069 struct hda_pincfg *pin;
1071 /* the check below may be invalid when pins are added by a fixup
1072 * dynamically (e.g. via snd_hda_codec_update_widgets()), so disabled
1076 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN)
1080 pin = look_up_pincfg(codec, list, nid);
1082 pin = snd_array_new(list);
1092 * snd_hda_codec_set_pincfg - Override a pin default configuration
1093 * @codec: the HDA codec
1094 * @nid: NID to set the pin config
1095 * @cfg: the pin default config value
1097 * Override a pin default configuration value in the cache.
1098 * This value can be read by snd_hda_codec_get_pincfg() in a higher
1099 * priority than the real hardware value.
1101 int snd_hda_codec_set_pincfg(struct hda_codec *codec,
1102 hda_nid_t nid, unsigned int cfg)
1104 return snd_hda_add_pincfg(codec, &codec->driver_pins, nid, cfg);
1106 EXPORT_SYMBOL_HDA(snd_hda_codec_set_pincfg);
1109 * snd_hda_codec_get_pincfg - Obtain a pin-default configuration
1110 * @codec: the HDA codec
1111 * @nid: NID to get the pin config
1113 * Get the current pin config value of the given pin NID.
1114 * If the pincfg value is cached or overridden via sysfs or driver,
1115 * returns the cached value.
1117 unsigned int snd_hda_codec_get_pincfg(struct hda_codec *codec, hda_nid_t nid)
1119 struct hda_pincfg *pin;
1121 #ifdef CONFIG_SND_HDA_HWDEP
1123 unsigned int cfg = 0;
1124 mutex_lock(&codec->user_mutex);
1125 pin = look_up_pincfg(codec, &codec->user_pins, nid);
1128 mutex_unlock(&codec->user_mutex);
1133 pin = look_up_pincfg(codec, &codec->driver_pins, nid);
1136 pin = look_up_pincfg(codec, &codec->init_pins, nid);
1141 EXPORT_SYMBOL_HDA(snd_hda_codec_get_pincfg);
1143 /* remember the current pinctl target value */
1144 int snd_hda_codec_set_pin_target(struct hda_codec *codec, hda_nid_t nid,
1147 struct hda_pincfg *pin;
1149 pin = look_up_pincfg(codec, &codec->init_pins, nid);
1155 EXPORT_SYMBOL_HDA(snd_hda_codec_set_pin_target);
1157 /* return the current pinctl target value */
1158 int snd_hda_codec_get_pin_target(struct hda_codec *codec, hda_nid_t nid)
1160 struct hda_pincfg *pin;
1162 pin = look_up_pincfg(codec, &codec->init_pins, nid);
1167 EXPORT_SYMBOL_HDA(snd_hda_codec_get_pin_target);
1170 * snd_hda_shutup_pins - Shut up all pins
1171 * @codec: the HDA codec
1173 * Clear all pin controls to shup up before suspend for avoiding click noise.
1174 * The controls aren't cached so that they can be resumed properly.
1176 void snd_hda_shutup_pins(struct hda_codec *codec)
1179 /* don't shut up pins when unloading the driver; otherwise it breaks
1180 * the default pin setup at the next load of the driver
1182 if (codec->bus->shutdown)
1184 for (i = 0; i < codec->init_pins.used; i++) {
1185 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1186 /* use read here for syncing after issuing each verb */
1187 snd_hda_codec_read(codec, pin->nid, 0,
1188 AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
1190 codec->pins_shutup = 1;
1192 EXPORT_SYMBOL_HDA(snd_hda_shutup_pins);
1195 /* Restore the pin controls cleared previously via snd_hda_shutup_pins() */
1196 static void restore_shutup_pins(struct hda_codec *codec)
1199 if (!codec->pins_shutup)
1201 if (codec->bus->shutdown)
1203 for (i = 0; i < codec->init_pins.used; i++) {
1204 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1205 snd_hda_codec_write(codec, pin->nid, 0,
1206 AC_VERB_SET_PIN_WIDGET_CONTROL,
1209 codec->pins_shutup = 0;
1213 static void hda_jackpoll_work(struct work_struct *work)
1215 struct hda_codec *codec =
1216 container_of(work, struct hda_codec, jackpoll_work.work);
1217 if (!codec->jackpoll_interval)
1220 snd_hda_jack_set_dirty_all(codec);
1221 snd_hda_jack_poll_all(codec);
1222 queue_delayed_work(codec->bus->workq, &codec->jackpoll_work,
1223 codec->jackpoll_interval);
1226 static void init_hda_cache(struct hda_cache_rec *cache,
1227 unsigned int record_size);
1228 static void free_hda_cache(struct hda_cache_rec *cache);
1230 /* release all pincfg lists */
1231 static void free_init_pincfgs(struct hda_codec *codec)
1233 snd_array_free(&codec->driver_pins);
1234 #ifdef CONFIG_SND_HDA_HWDEP
1235 snd_array_free(&codec->user_pins);
1237 snd_array_free(&codec->init_pins);
1241 * audio-converter setup caches
1243 struct hda_cvt_setup {
1248 unsigned char active; /* cvt is currently used */
1249 unsigned char dirty; /* setups should be cleared */
1252 /* get or create a cache entry for the given audio converter NID */
1253 static struct hda_cvt_setup *
1254 get_hda_cvt_setup(struct hda_codec *codec, hda_nid_t nid)
1256 struct hda_cvt_setup *p;
1259 for (i = 0; i < codec->cvt_setups.used; i++) {
1260 p = snd_array_elem(&codec->cvt_setups, i);
1264 p = snd_array_new(&codec->cvt_setups);
1273 static void snd_hda_codec_free(struct hda_codec *codec)
1277 cancel_delayed_work_sync(&codec->jackpoll_work);
1278 snd_hda_jack_tbl_clear(codec);
1279 free_init_pincfgs(codec);
1281 cancel_delayed_work(&codec->power_work);
1282 flush_workqueue(codec->bus->workq);
1284 list_del(&codec->list);
1285 snd_array_free(&codec->mixers);
1286 snd_array_free(&codec->nids);
1287 snd_array_free(&codec->cvt_setups);
1288 snd_array_free(&codec->spdif_out);
1289 remove_conn_list(codec);
1290 codec->bus->caddr_tbl[codec->addr] = NULL;
1291 if (codec->patch_ops.free)
1292 codec->patch_ops.free(codec);
1294 if (!codec->pm_down_notified) /* cancel leftover refcounts */
1295 hda_call_pm_notify(codec->bus, false);
1297 module_put(codec->owner);
1298 free_hda_cache(&codec->amp_cache);
1299 free_hda_cache(&codec->cmd_cache);
1300 kfree(codec->vendor_name);
1301 kfree(codec->chip_name);
1302 kfree(codec->modelname);
1303 kfree(codec->wcaps);
1307 static bool snd_hda_codec_get_supported_ps(struct hda_codec *codec,
1308 hda_nid_t fg, unsigned int power_state);
1310 static unsigned int hda_set_power_state(struct hda_codec *codec,
1311 unsigned int power_state);
1314 * snd_hda_codec_new - create a HDA codec
1315 * @bus: the bus to assign
1316 * @codec_addr: the codec address
1317 * @codecp: the pointer to store the generated codec
1319 * Returns 0 if successful, or a negative error code.
1321 int snd_hda_codec_new(struct hda_bus *bus,
1322 unsigned int codec_addr,
1323 struct hda_codec **codecp)
1325 struct hda_codec *codec;
1330 if (snd_BUG_ON(!bus))
1332 if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS))
1335 if (bus->caddr_tbl[codec_addr]) {
1336 snd_printk(KERN_ERR "hda_codec: "
1337 "address 0x%x is already occupied\n", codec_addr);
1341 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
1342 if (codec == NULL) {
1343 snd_printk(KERN_ERR "can't allocate struct hda_codec\n");
1348 codec->addr = codec_addr;
1349 mutex_init(&codec->spdif_mutex);
1350 mutex_init(&codec->control_mutex);
1351 mutex_init(&codec->hash_mutex);
1352 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
1353 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
1354 snd_array_init(&codec->mixers, sizeof(struct hda_nid_item), 32);
1355 snd_array_init(&codec->nids, sizeof(struct hda_nid_item), 32);
1356 snd_array_init(&codec->init_pins, sizeof(struct hda_pincfg), 16);
1357 snd_array_init(&codec->driver_pins, sizeof(struct hda_pincfg), 16);
1358 snd_array_init(&codec->cvt_setups, sizeof(struct hda_cvt_setup), 8);
1359 snd_array_init(&codec->spdif_out, sizeof(struct hda_spdif_out), 16);
1360 snd_array_init(&codec->jacktbl, sizeof(struct hda_jack_tbl), 16);
1361 snd_array_init(&codec->verbs, sizeof(struct hda_verb *), 8);
1362 INIT_LIST_HEAD(&codec->conn_list);
1364 INIT_DELAYED_WORK(&codec->jackpoll_work, hda_jackpoll_work);
1367 spin_lock_init(&codec->power_lock);
1368 INIT_DELAYED_WORK(&codec->power_work, hda_power_work);
1369 /* snd_hda_codec_new() marks the codec as power-up, and leave it as is.
1370 * the caller has to power down appropriatley after initialization
1373 hda_keep_power_on(codec);
1374 hda_call_pm_notify(bus, true);
1377 if (codec->bus->modelname) {
1378 codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL);
1379 if (!codec->modelname) {
1380 snd_hda_codec_free(codec);
1385 list_add_tail(&codec->list, &bus->codec_list);
1386 bus->caddr_tbl[codec_addr] = codec;
1388 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1390 if (codec->vendor_id == -1)
1391 /* read again, hopefully the access method was corrected
1392 * in the last read...
1394 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1396 codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1397 AC_PAR_SUBSYSTEM_ID);
1398 codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1401 setup_fg_nodes(codec);
1402 if (!codec->afg && !codec->mfg) {
1403 snd_printdd("hda_codec: no AFG or MFG node found\n");
1408 fg = codec->afg ? codec->afg : codec->mfg;
1409 err = read_widget_caps(codec, fg);
1411 snd_printk(KERN_ERR "hda_codec: cannot malloc\n");
1414 err = read_pin_defaults(codec);
1418 if (!codec->subsystem_id) {
1419 codec->subsystem_id =
1420 snd_hda_codec_read(codec, fg, 0,
1421 AC_VERB_GET_SUBSYSTEM_ID, 0);
1425 codec->d3_stop_clk = snd_hda_codec_get_supported_ps(codec, fg,
1427 if (!codec->d3_stop_clk)
1428 bus->power_keep_link_on = 1;
1430 codec->epss = snd_hda_codec_get_supported_ps(codec, fg,
1433 /* power-up all before initialization */
1434 hda_set_power_state(codec, AC_PWRST_D0);
1436 snd_hda_codec_proc_new(codec);
1438 snd_hda_create_hwdep(codec);
1440 sprintf(component, "HDA:%08x,%08x,%08x", codec->vendor_id,
1441 codec->subsystem_id, codec->revision_id);
1442 snd_component_add(codec->bus->card, component);
1449 snd_hda_codec_free(codec);
1452 EXPORT_SYMBOL_HDA(snd_hda_codec_new);
1454 int snd_hda_codec_update_widgets(struct hda_codec *codec)
1459 /* Assume the function group node does not change,
1460 * only the widget nodes may change.
1462 kfree(codec->wcaps);
1463 fg = codec->afg ? codec->afg : codec->mfg;
1464 err = read_widget_caps(codec, fg);
1466 snd_printk(KERN_ERR "hda_codec: cannot malloc\n");
1470 snd_array_free(&codec->init_pins);
1471 err = read_pin_defaults(codec);
1475 EXPORT_SYMBOL_HDA(snd_hda_codec_update_widgets);
1479 * snd_hda_codec_configure - (Re-)configure the HD-audio codec
1480 * @codec: the HDA codec
1482 * Start parsing of the given codec tree and (re-)initialize the whole
1485 * Returns 0 if successful or a negative error code.
1487 int snd_hda_codec_configure(struct hda_codec *codec)
1491 codec->preset = find_codec_preset(codec);
1492 if (!codec->vendor_name || !codec->chip_name) {
1493 err = get_codec_name(codec);
1498 if (is_generic_config(codec)) {
1499 err = snd_hda_parse_generic_codec(codec);
1502 if (codec->preset && codec->preset->patch) {
1503 err = codec->preset->patch(codec);
1507 /* call the default parser */
1508 err = snd_hda_parse_generic_codec(codec);
1510 printk(KERN_ERR "hda-codec: No codec parser is available\n");
1513 if (!err && codec->patch_ops.unsol_event)
1514 err = init_unsol_queue(codec->bus);
1515 /* audio codec should override the mixer name */
1516 if (!err && (codec->afg || !*codec->bus->card->mixername))
1517 snprintf(codec->bus->card->mixername,
1518 sizeof(codec->bus->card->mixername),
1519 "%s %s", codec->vendor_name, codec->chip_name);
1522 EXPORT_SYMBOL_HDA(snd_hda_codec_configure);
1524 /* update the stream-id if changed */
1525 static void update_pcm_stream_id(struct hda_codec *codec,
1526 struct hda_cvt_setup *p, hda_nid_t nid,
1527 u32 stream_tag, int channel_id)
1529 unsigned int oldval, newval;
1531 if (p->stream_tag != stream_tag || p->channel_id != channel_id) {
1532 oldval = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0);
1533 newval = (stream_tag << 4) | channel_id;
1534 if (oldval != newval)
1535 snd_hda_codec_write(codec, nid, 0,
1536 AC_VERB_SET_CHANNEL_STREAMID,
1538 p->stream_tag = stream_tag;
1539 p->channel_id = channel_id;
1543 /* update the format-id if changed */
1544 static void update_pcm_format(struct hda_codec *codec, struct hda_cvt_setup *p,
1545 hda_nid_t nid, int format)
1547 unsigned int oldval;
1549 if (p->format_id != format) {
1550 oldval = snd_hda_codec_read(codec, nid, 0,
1551 AC_VERB_GET_STREAM_FORMAT, 0);
1552 if (oldval != format) {
1554 snd_hda_codec_write(codec, nid, 0,
1555 AC_VERB_SET_STREAM_FORMAT,
1558 p->format_id = format;
1563 * snd_hda_codec_setup_stream - set up the codec for streaming
1564 * @codec: the CODEC to set up
1565 * @nid: the NID to set up
1566 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
1567 * @channel_id: channel id to pass, zero based.
1568 * @format: stream format.
1570 void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
1572 int channel_id, int format)
1574 struct hda_codec *c;
1575 struct hda_cvt_setup *p;
1582 snd_printdd("hda_codec_setup_stream: "
1583 "NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
1584 nid, stream_tag, channel_id, format);
1585 p = get_hda_cvt_setup(codec, nid);
1589 if (codec->pcm_format_first)
1590 update_pcm_format(codec, p, nid, format);
1591 update_pcm_stream_id(codec, p, nid, stream_tag, channel_id);
1592 if (!codec->pcm_format_first)
1593 update_pcm_format(codec, p, nid, format);
1598 /* make other inactive cvts with the same stream-tag dirty */
1599 type = get_wcaps_type(get_wcaps(codec, nid));
1600 list_for_each_entry(c, &codec->bus->codec_list, list) {
1601 for (i = 0; i < c->cvt_setups.used; i++) {
1602 p = snd_array_elem(&c->cvt_setups, i);
1603 if (!p->active && p->stream_tag == stream_tag &&
1604 get_wcaps_type(get_wcaps(c, p->nid)) == type)
1609 EXPORT_SYMBOL_HDA(snd_hda_codec_setup_stream);
1611 static void really_cleanup_stream(struct hda_codec *codec,
1612 struct hda_cvt_setup *q);
1615 * __snd_hda_codec_cleanup_stream - clean up the codec for closing
1616 * @codec: the CODEC to clean up
1617 * @nid: the NID to clean up
1618 * @do_now: really clean up the stream instead of clearing the active flag
1620 void __snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid,
1623 struct hda_cvt_setup *p;
1628 if (codec->no_sticky_stream)
1631 snd_printdd("hda_codec_cleanup_stream: NID=0x%x\n", nid);
1632 p = get_hda_cvt_setup(codec, nid);
1634 /* here we just clear the active flag when do_now isn't set;
1635 * actual clean-ups will be done later in
1636 * purify_inactive_streams() called from snd_hda_codec_prpapre()
1639 really_cleanup_stream(codec, p);
1644 EXPORT_SYMBOL_HDA(__snd_hda_codec_cleanup_stream);
1646 static void really_cleanup_stream(struct hda_codec *codec,
1647 struct hda_cvt_setup *q)
1649 hda_nid_t nid = q->nid;
1650 if (q->stream_tag || q->channel_id)
1651 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
1653 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0
1655 memset(q, 0, sizeof(*q));
1659 /* clean up the all conflicting obsolete streams */
1660 static void purify_inactive_streams(struct hda_codec *codec)
1662 struct hda_codec *c;
1665 list_for_each_entry(c, &codec->bus->codec_list, list) {
1666 for (i = 0; i < c->cvt_setups.used; i++) {
1667 struct hda_cvt_setup *p;
1668 p = snd_array_elem(&c->cvt_setups, i);
1670 really_cleanup_stream(c, p);
1676 /* clean up all streams; called from suspend */
1677 static void hda_cleanup_all_streams(struct hda_codec *codec)
1681 for (i = 0; i < codec->cvt_setups.used; i++) {
1682 struct hda_cvt_setup *p = snd_array_elem(&codec->cvt_setups, i);
1684 really_cleanup_stream(codec, p);
1690 * amp access functions
1693 /* FIXME: more better hash key? */
1694 #define HDA_HASH_KEY(nid, dir, idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
1695 #define HDA_HASH_PINCAP_KEY(nid) (u32)((nid) + (0x02 << 24))
1696 #define HDA_HASH_PARPCM_KEY(nid) (u32)((nid) + (0x03 << 24))
1697 #define HDA_HASH_PARSTR_KEY(nid) (u32)((nid) + (0x04 << 24))
1698 #define INFO_AMP_CAPS (1<<0)
1699 #define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
1701 /* initialize the hash table */
1702 static void init_hda_cache(struct hda_cache_rec *cache,
1703 unsigned int record_size)
1705 memset(cache, 0, sizeof(*cache));
1706 memset(cache->hash, 0xff, sizeof(cache->hash));
1707 snd_array_init(&cache->buf, record_size, 64);
1710 static void free_hda_cache(struct hda_cache_rec *cache)
1712 snd_array_free(&cache->buf);
1715 /* query the hash. allocate an entry if not found. */
1716 static struct hda_cache_head *get_hash(struct hda_cache_rec *cache, u32 key)
1718 u16 idx = key % (u16)ARRAY_SIZE(cache->hash);
1719 u16 cur = cache->hash[idx];
1720 struct hda_cache_head *info;
1722 while (cur != 0xffff) {
1723 info = snd_array_elem(&cache->buf, cur);
1724 if (info->key == key)
1731 /* query the hash. allocate an entry if not found. */
1732 static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache,
1735 struct hda_cache_head *info = get_hash(cache, key);
1738 /* add a new hash entry */
1739 info = snd_array_new(&cache->buf);
1742 cur = snd_array_index(&cache->buf, info);
1746 idx = key % (u16)ARRAY_SIZE(cache->hash);
1747 info->next = cache->hash[idx];
1748 cache->hash[idx] = cur;
1753 /* query and allocate an amp hash entry */
1754 static inline struct hda_amp_info *
1755 get_alloc_amp_hash(struct hda_codec *codec, u32 key)
1757 return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key);
1760 /* overwrite the value with the key in the caps hash */
1761 static int write_caps_hash(struct hda_codec *codec, u32 key, unsigned int val)
1763 struct hda_amp_info *info;
1765 mutex_lock(&codec->hash_mutex);
1766 info = get_alloc_amp_hash(codec, key);
1768 mutex_unlock(&codec->hash_mutex);
1771 info->amp_caps = val;
1772 info->head.val |= INFO_AMP_CAPS;
1773 mutex_unlock(&codec->hash_mutex);
1777 /* query the value from the caps hash; if not found, fetch the current
1778 * value from the given function and store in the hash
1781 query_caps_hash(struct hda_codec *codec, hda_nid_t nid, int dir, u32 key,
1782 unsigned int (*func)(struct hda_codec *, hda_nid_t, int))
1784 struct hda_amp_info *info;
1787 mutex_lock(&codec->hash_mutex);
1788 info = get_alloc_amp_hash(codec, key);
1790 mutex_unlock(&codec->hash_mutex);
1793 if (!(info->head.val & INFO_AMP_CAPS)) {
1794 mutex_unlock(&codec->hash_mutex); /* for reentrance */
1795 val = func(codec, nid, dir);
1796 write_caps_hash(codec, key, val);
1798 val = info->amp_caps;
1799 mutex_unlock(&codec->hash_mutex);
1804 static unsigned int read_amp_cap(struct hda_codec *codec, hda_nid_t nid,
1807 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
1809 return snd_hda_param_read(codec, nid,
1810 direction == HDA_OUTPUT ?
1811 AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP);
1815 * query_amp_caps - query AMP capabilities
1816 * @codec: the HD-auio codec
1817 * @nid: the NID to query
1818 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1820 * Query AMP capabilities for the given widget and direction.
1821 * Returns the obtained capability bits.
1823 * When cap bits have been already read, this doesn't read again but
1824 * returns the cached value.
1826 u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
1828 return query_caps_hash(codec, nid, direction,
1829 HDA_HASH_KEY(nid, direction, 0),
1832 EXPORT_SYMBOL_HDA(query_amp_caps);
1835 * snd_hda_override_amp_caps - Override the AMP capabilities
1836 * @codec: the CODEC to clean up
1837 * @nid: the NID to clean up
1838 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1839 * @caps: the capability bits to set
1841 * Override the cached AMP caps bits value by the given one.
1842 * This function is useful if the driver needs to adjust the AMP ranges,
1843 * e.g. limit to 0dB, etc.
1845 * Returns zero if successful or a negative error code.
1847 int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
1850 return write_caps_hash(codec, HDA_HASH_KEY(nid, dir, 0), caps);
1852 EXPORT_SYMBOL_HDA(snd_hda_override_amp_caps);
1854 static unsigned int read_pin_cap(struct hda_codec *codec, hda_nid_t nid,
1857 return snd_hda_param_read(codec, nid, AC_PAR_PIN_CAP);
1861 * snd_hda_query_pin_caps - Query PIN capabilities
1862 * @codec: the HD-auio codec
1863 * @nid: the NID to query
1865 * Query PIN capabilities for the given widget.
1866 * Returns the obtained capability bits.
1868 * When cap bits have been already read, this doesn't read again but
1869 * returns the cached value.
1871 u32 snd_hda_query_pin_caps(struct hda_codec *codec, hda_nid_t nid)
1873 return query_caps_hash(codec, nid, 0, HDA_HASH_PINCAP_KEY(nid),
1876 EXPORT_SYMBOL_HDA(snd_hda_query_pin_caps);
1879 * snd_hda_override_pin_caps - Override the pin capabilities
1881 * @nid: the NID to override
1882 * @caps: the capability bits to set
1884 * Override the cached PIN capabilitiy bits value by the given one.
1886 * Returns zero if successful or a negative error code.
1888 int snd_hda_override_pin_caps(struct hda_codec *codec, hda_nid_t nid,
1891 return write_caps_hash(codec, HDA_HASH_PINCAP_KEY(nid), caps);
1893 EXPORT_SYMBOL_HDA(snd_hda_override_pin_caps);
1895 /* read or sync the hash value with the current value;
1896 * call within hash_mutex
1898 static struct hda_amp_info *
1899 update_amp_hash(struct hda_codec *codec, hda_nid_t nid, int ch,
1900 int direction, int index, bool init_only)
1902 struct hda_amp_info *info;
1903 unsigned int parm, val = 0;
1904 bool val_read = false;
1907 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
1910 if (!(info->head.val & INFO_AMP_VOL(ch))) {
1912 mutex_unlock(&codec->hash_mutex);
1913 parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
1914 parm |= direction == HDA_OUTPUT ?
1915 AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
1917 val = snd_hda_codec_read(codec, nid, 0,
1918 AC_VERB_GET_AMP_GAIN_MUTE, parm);
1921 mutex_lock(&codec->hash_mutex);
1924 info->vol[ch] = val;
1925 info->head.val |= INFO_AMP_VOL(ch);
1926 } else if (init_only)
1932 * write the current volume in info to the h/w
1934 static void put_vol_mute(struct hda_codec *codec, unsigned int amp_caps,
1935 hda_nid_t nid, int ch, int direction, int index,
1940 parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
1941 parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
1942 parm |= index << AC_AMP_SET_INDEX_SHIFT;
1943 if ((val & HDA_AMP_MUTE) && !(amp_caps & AC_AMPCAP_MUTE) &&
1944 (amp_caps & AC_AMPCAP_MIN_MUTE))
1945 ; /* set the zero value as a fake mute */
1948 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
1952 * snd_hda_codec_amp_read - Read 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 * @index: the index value (only for input direction)
1959 * Read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
1961 int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
1962 int direction, int index)
1964 struct hda_amp_info *info;
1965 unsigned int val = 0;
1967 mutex_lock(&codec->hash_mutex);
1968 info = update_amp_hash(codec, nid, ch, direction, index, false);
1970 val = info->vol[ch];
1971 mutex_unlock(&codec->hash_mutex);
1974 EXPORT_SYMBOL_HDA(snd_hda_codec_amp_read);
1976 static int codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
1977 int direction, int idx, int mask, int val,
1980 struct hda_amp_info *info;
1982 unsigned int cache_only;
1984 if (snd_BUG_ON(mask & ~0xff))
1988 mutex_lock(&codec->hash_mutex);
1989 info = update_amp_hash(codec, nid, ch, direction, idx, init_only);
1991 mutex_unlock(&codec->hash_mutex);
1994 val |= info->vol[ch] & ~mask;
1995 if (info->vol[ch] == val) {
1996 mutex_unlock(&codec->hash_mutex);
1999 info->vol[ch] = val;
2000 cache_only = info->head.dirty = codec->cached_write;
2001 caps = info->amp_caps;
2002 mutex_unlock(&codec->hash_mutex);
2004 put_vol_mute(codec, caps, nid, ch, direction, idx, val);
2009 * snd_hda_codec_amp_update - update the AMP value
2010 * @codec: HD-audio codec
2011 * @nid: NID to read the AMP value
2012 * @ch: channel (left=0 or right=1)
2013 * @direction: #HDA_INPUT or #HDA_OUTPUT
2014 * @idx: the index value (only for input direction)
2015 * @mask: bit mask to set
2016 * @val: the bits value to set
2018 * Update the AMP value with a bit mask.
2019 * Returns 0 if the value is unchanged, 1 if changed.
2021 int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
2022 int direction, int idx, int mask, int val)
2024 return codec_amp_update(codec, nid, ch, direction, idx, mask, val, false);
2026 EXPORT_SYMBOL_HDA(snd_hda_codec_amp_update);
2029 * snd_hda_codec_amp_stereo - update the AMP stereo values
2030 * @codec: HD-audio codec
2031 * @nid: NID to read the AMP value
2032 * @direction: #HDA_INPUT or #HDA_OUTPUT
2033 * @idx: the index value (only for input direction)
2034 * @mask: bit mask to set
2035 * @val: the bits value to set
2037 * Update the AMP values like snd_hda_codec_amp_update(), but for a
2038 * stereo widget with the same mask and value.
2040 int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
2041 int direction, int idx, int mask, int val)
2045 if (snd_BUG_ON(mask & ~0xff))
2047 for (ch = 0; ch < 2; ch++)
2048 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
2052 EXPORT_SYMBOL_HDA(snd_hda_codec_amp_stereo);
2054 /* Works like snd_hda_codec_amp_update() but it writes the value only at
2055 * the first access. If the amp was already initialized / updated beforehand,
2056 * this does nothing.
2058 int snd_hda_codec_amp_init(struct hda_codec *codec, hda_nid_t nid, int ch,
2059 int dir, int idx, int mask, int val)
2061 return codec_amp_update(codec, nid, ch, dir, idx, mask, val, true);
2063 EXPORT_SYMBOL_HDA(snd_hda_codec_amp_init);
2065 int snd_hda_codec_amp_init_stereo(struct hda_codec *codec, hda_nid_t nid,
2066 int dir, int idx, int mask, int val)
2070 if (snd_BUG_ON(mask & ~0xff))
2072 for (ch = 0; ch < 2; ch++)
2073 ret |= snd_hda_codec_amp_init(codec, nid, ch, dir,
2077 EXPORT_SYMBOL_HDA(snd_hda_codec_amp_init_stereo);
2080 * snd_hda_codec_resume_amp - Resume all AMP commands from the cache
2081 * @codec: HD-audio codec
2083 * Resume the all amp commands from the cache.
2085 void snd_hda_codec_resume_amp(struct hda_codec *codec)
2089 mutex_lock(&codec->hash_mutex);
2090 codec->cached_write = 0;
2091 for (i = 0; i < codec->amp_cache.buf.used; i++) {
2092 struct hda_amp_info *buffer;
2095 unsigned int idx, dir, ch;
2096 struct hda_amp_info info;
2098 buffer = snd_array_elem(&codec->amp_cache.buf, i);
2099 if (!buffer->head.dirty)
2101 buffer->head.dirty = 0;
2103 key = info.head.key;
2107 idx = (key >> 16) & 0xff;
2108 dir = (key >> 24) & 0xff;
2109 for (ch = 0; ch < 2; ch++) {
2110 if (!(info.head.val & INFO_AMP_VOL(ch)))
2112 mutex_unlock(&codec->hash_mutex);
2113 put_vol_mute(codec, info.amp_caps, nid, ch, dir, idx,
2115 mutex_lock(&codec->hash_mutex);
2118 mutex_unlock(&codec->hash_mutex);
2120 EXPORT_SYMBOL_HDA(snd_hda_codec_resume_amp);
2122 static u32 get_amp_max_value(struct hda_codec *codec, hda_nid_t nid, int dir,
2125 u32 caps = query_amp_caps(codec, nid, dir);
2127 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
2134 * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer
2136 * The control element is supposed to have the private_value field
2137 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2139 int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
2140 struct snd_ctl_elem_info *uinfo)
2142 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2143 u16 nid = get_amp_nid(kcontrol);
2144 u8 chs = get_amp_channels(kcontrol);
2145 int dir = get_amp_direction(kcontrol);
2146 unsigned int ofs = get_amp_offset(kcontrol);
2148 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2149 uinfo->count = chs == 3 ? 2 : 1;
2150 uinfo->value.integer.min = 0;
2151 uinfo->value.integer.max = get_amp_max_value(codec, nid, dir, ofs);
2152 if (!uinfo->value.integer.max) {
2153 printk(KERN_WARNING "hda_codec: "
2154 "num_steps = 0 for NID=0x%x (ctl = %s)\n", nid,
2160 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_info);
2163 static inline unsigned int
2164 read_amp_value(struct hda_codec *codec, hda_nid_t nid,
2165 int ch, int dir, int idx, unsigned int ofs)
2168 val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
2169 val &= HDA_AMP_VOLMASK;
2178 update_amp_value(struct hda_codec *codec, hda_nid_t nid,
2179 int ch, int dir, int idx, unsigned int ofs,
2182 unsigned int maxval;
2186 /* ofs = 0: raw max value */
2187 maxval = get_amp_max_value(codec, nid, dir, 0);
2190 return snd_hda_codec_amp_update(codec, nid, ch, dir, idx,
2191 HDA_AMP_VOLMASK, val);
2195 * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume
2197 * The control element is supposed to have the private_value field
2198 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2200 int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
2201 struct snd_ctl_elem_value *ucontrol)
2203 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2204 hda_nid_t nid = get_amp_nid(kcontrol);
2205 int chs = get_amp_channels(kcontrol);
2206 int dir = get_amp_direction(kcontrol);
2207 int idx = get_amp_index(kcontrol);
2208 unsigned int ofs = get_amp_offset(kcontrol);
2209 long *valp = ucontrol->value.integer.value;
2212 *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs);
2214 *valp = read_amp_value(codec, nid, 1, dir, idx, ofs);
2217 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_get);
2220 * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume
2222 * The control element is supposed to have the private_value field
2223 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2225 int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
2226 struct snd_ctl_elem_value *ucontrol)
2228 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2229 hda_nid_t nid = get_amp_nid(kcontrol);
2230 int chs = get_amp_channels(kcontrol);
2231 int dir = get_amp_direction(kcontrol);
2232 int idx = get_amp_index(kcontrol);
2233 unsigned int ofs = get_amp_offset(kcontrol);
2234 long *valp = ucontrol->value.integer.value;
2237 snd_hda_power_up(codec);
2239 change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp);
2243 change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp);
2244 snd_hda_power_down(codec);
2247 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_put);
2250 * snd_hda_mixer_amp_volume_put - TLV callback for a standard AMP mixer volume
2252 * The control element is supposed to have the private_value field
2253 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2255 int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2256 unsigned int size, unsigned int __user *_tlv)
2258 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2259 hda_nid_t nid = get_amp_nid(kcontrol);
2260 int dir = get_amp_direction(kcontrol);
2261 unsigned int ofs = get_amp_offset(kcontrol);
2262 bool min_mute = get_amp_min_mute(kcontrol);
2263 u32 caps, val1, val2;
2265 if (size < 4 * sizeof(unsigned int))
2267 caps = query_amp_caps(codec, nid, dir);
2268 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2269 val2 = (val2 + 1) * 25;
2270 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
2272 val1 = ((int)val1) * ((int)val2);
2273 if (min_mute || (caps & AC_AMPCAP_MIN_MUTE))
2274 val2 |= TLV_DB_SCALE_MUTE;
2275 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
2277 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
2279 if (put_user(val1, _tlv + 2))
2281 if (put_user(val2, _tlv + 3))
2285 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_tlv);
2288 * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control
2289 * @codec: HD-audio codec
2290 * @nid: NID of a reference widget
2291 * @dir: #HDA_INPUT or #HDA_OUTPUT
2292 * @tlv: TLV data to be stored, at least 4 elements
2294 * Set (static) TLV data for a virtual master volume using the AMP caps
2295 * obtained from the reference NID.
2296 * The volume range is recalculated as if the max volume is 0dB.
2298 void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
2304 caps = query_amp_caps(codec, nid, dir);
2305 nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
2306 step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2307 step = (step + 1) * 25;
2308 tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
2309 tlv[1] = 2 * sizeof(unsigned int);
2310 tlv[2] = -nums * step;
2313 EXPORT_SYMBOL_HDA(snd_hda_set_vmaster_tlv);
2315 /* find a mixer control element with the given name */
2316 static struct snd_kcontrol *
2317 find_mixer_ctl(struct hda_codec *codec, const char *name, int dev, int idx)
2319 struct snd_ctl_elem_id id;
2320 memset(&id, 0, sizeof(id));
2321 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
2324 if (snd_BUG_ON(strlen(name) >= sizeof(id.name)))
2326 strcpy(id.name, name);
2327 return snd_ctl_find_id(codec->bus->card, &id);
2331 * snd_hda_find_mixer_ctl - Find a mixer control element with the given name
2332 * @codec: HD-audio codec
2333 * @name: ctl id name string
2335 * Get the control element with the given id string and IFACE_MIXER.
2337 struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
2340 return find_mixer_ctl(codec, name, 0, 0);
2342 EXPORT_SYMBOL_HDA(snd_hda_find_mixer_ctl);
2344 static int find_empty_mixer_ctl_idx(struct hda_codec *codec, const char *name,
2348 /* 16 ctlrs should be large enough */
2349 for (i = 0, idx = start_idx; i < 16; i++, idx++) {
2350 if (!find_mixer_ctl(codec, name, 0, idx))
2357 * snd_hda_ctl_add - Add a control element and assign to the codec
2358 * @codec: HD-audio codec
2359 * @nid: corresponding NID (optional)
2360 * @kctl: the control element to assign
2362 * Add the given control element to an array inside the codec instance.
2363 * All control elements belonging to a codec are supposed to be added
2364 * by this function so that a proper clean-up works at the free or
2365 * reconfiguration time.
2367 * If non-zero @nid is passed, the NID is assigned to the control element.
2368 * The assignment is shown in the codec proc file.
2370 * snd_hda_ctl_add() checks the control subdev id field whether
2371 * #HDA_SUBDEV_NID_FLAG bit is set. If set (and @nid is zero), the lower
2372 * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit
2373 * specifies if kctl->private_value is a HDA amplifier value.
2375 int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid,
2376 struct snd_kcontrol *kctl)
2379 unsigned short flags = 0;
2380 struct hda_nid_item *item;
2382 if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) {
2383 flags |= HDA_NID_ITEM_AMP;
2385 nid = get_amp_nid_(kctl->private_value);
2387 if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0)
2388 nid = kctl->id.subdevice & 0xffff;
2389 if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG))
2390 kctl->id.subdevice = 0;
2391 err = snd_ctl_add(codec->bus->card, kctl);
2394 item = snd_array_new(&codec->mixers);
2399 item->flags = flags;
2402 EXPORT_SYMBOL_HDA(snd_hda_ctl_add);
2405 * snd_hda_add_nid - Assign a NID to a control element
2406 * @codec: HD-audio codec
2407 * @nid: corresponding NID (optional)
2408 * @kctl: the control element to assign
2409 * @index: index to kctl
2411 * Add the given control element to an array inside the codec instance.
2412 * This function is used when #snd_hda_ctl_add cannot be used for 1:1
2413 * NID:KCTL mapping - for example "Capture Source" selector.
2415 int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl,
2416 unsigned int index, hda_nid_t nid)
2418 struct hda_nid_item *item;
2421 item = snd_array_new(&codec->nids);
2425 item->index = index;
2429 printk(KERN_ERR "hda-codec: no NID for mapping control %s:%d:%d\n",
2430 kctl->id.name, kctl->id.index, index);
2433 EXPORT_SYMBOL_HDA(snd_hda_add_nid);
2436 * snd_hda_ctls_clear - Clear all controls assigned to the given codec
2437 * @codec: HD-audio codec
2439 void snd_hda_ctls_clear(struct hda_codec *codec)
2442 struct hda_nid_item *items = codec->mixers.list;
2443 for (i = 0; i < codec->mixers.used; i++)
2444 snd_ctl_remove(codec->bus->card, items[i].kctl);
2445 snd_array_free(&codec->mixers);
2446 snd_array_free(&codec->nids);
2449 /* pseudo device locking
2450 * toggle card->shutdown to allow/disallow the device access (as a hack)
2452 int snd_hda_lock_devices(struct hda_bus *bus)
2454 struct snd_card *card = bus->card;
2455 struct hda_codec *codec;
2457 spin_lock(&card->files_lock);
2461 if (!list_empty(&card->ctl_files))
2464 list_for_each_entry(codec, &bus->codec_list, list) {
2466 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
2467 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
2470 if (cpcm->pcm->streams[0].substream_opened ||
2471 cpcm->pcm->streams[1].substream_opened)
2475 spin_unlock(&card->files_lock);
2481 spin_unlock(&card->files_lock);
2484 EXPORT_SYMBOL_HDA(snd_hda_lock_devices);
2486 void snd_hda_unlock_devices(struct hda_bus *bus)
2488 struct snd_card *card = bus->card;
2491 spin_lock(&card->files_lock);
2493 spin_unlock(&card->files_lock);
2495 EXPORT_SYMBOL_HDA(snd_hda_unlock_devices);
2498 * snd_hda_codec_reset - Clear all objects assigned to the codec
2499 * @codec: HD-audio codec
2501 * This frees the all PCM and control elements assigned to the codec, and
2502 * clears the caches and restores the pin default configurations.
2504 * When a device is being used, it returns -EBSY. If successfully freed,
2507 int snd_hda_codec_reset(struct hda_codec *codec)
2509 struct hda_bus *bus = codec->bus;
2510 struct snd_card *card = bus->card;
2513 if (snd_hda_lock_devices(bus) < 0)
2516 /* OK, let it free */
2517 cancel_delayed_work_sync(&codec->jackpoll_work);
2519 cancel_delayed_work_sync(&codec->power_work);
2520 flush_workqueue(bus->workq);
2522 snd_hda_ctls_clear(codec);
2524 for (i = 0; i < codec->num_pcms; i++) {
2525 if (codec->pcm_info[i].pcm) {
2526 snd_device_free(card, codec->pcm_info[i].pcm);
2527 clear_bit(codec->pcm_info[i].device,
2531 if (codec->patch_ops.free)
2532 codec->patch_ops.free(codec);
2533 memset(&codec->patch_ops, 0, sizeof(codec->patch_ops));
2534 snd_hda_jack_tbl_clear(codec);
2535 codec->proc_widget_hook = NULL;
2537 free_hda_cache(&codec->amp_cache);
2538 free_hda_cache(&codec->cmd_cache);
2539 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
2540 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
2541 /* free only driver_pins so that init_pins + user_pins are restored */
2542 snd_array_free(&codec->driver_pins);
2543 snd_array_free(&codec->cvt_setups);
2544 snd_array_free(&codec->spdif_out);
2545 snd_array_free(&codec->verbs);
2546 codec->num_pcms = 0;
2547 codec->pcm_info = NULL;
2548 codec->preset = NULL;
2549 codec->slave_dig_outs = NULL;
2550 codec->spdif_status_reset = 0;
2551 module_put(codec->owner);
2552 codec->owner = NULL;
2554 /* allow device access again */
2555 snd_hda_unlock_devices(bus);
2559 typedef int (*map_slave_func_t)(void *, struct snd_kcontrol *);
2561 /* apply the function to all matching slave ctls in the mixer list */
2562 static int map_slaves(struct hda_codec *codec, const char * const *slaves,
2563 const char *suffix, map_slave_func_t func, void *data)
2565 struct hda_nid_item *items;
2566 const char * const *s;
2569 items = codec->mixers.list;
2570 for (i = 0; i < codec->mixers.used; i++) {
2571 struct snd_kcontrol *sctl = items[i].kctl;
2572 if (!sctl || !sctl->id.name ||
2573 sctl->id.iface != SNDRV_CTL_ELEM_IFACE_MIXER)
2575 for (s = slaves; *s; s++) {
2576 char tmpname[sizeof(sctl->id.name)];
2577 const char *name = *s;
2579 snprintf(tmpname, sizeof(tmpname), "%s %s",
2583 if (!strcmp(sctl->id.name, name)) {
2584 err = func(data, sctl);
2594 static int check_slave_present(void *data, struct snd_kcontrol *sctl)
2599 /* guess the value corresponding to 0dB */
2600 static int get_kctl_0dB_offset(struct snd_kcontrol *kctl)
2603 const int *tlv = NULL;
2606 if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
2607 /* FIXME: set_fs() hack for obtaining user-space TLV data */
2608 mm_segment_t fs = get_fs();
2610 if (!kctl->tlv.c(kctl, 0, sizeof(_tlv), _tlv))
2613 } else if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_READ)
2615 if (tlv && tlv[0] == SNDRV_CTL_TLVT_DB_SCALE)
2616 val = -tlv[2] / tlv[3];
2620 /* call kctl->put with the given value(s) */
2621 static int put_kctl_with_value(struct snd_kcontrol *kctl, int val)
2623 struct snd_ctl_elem_value *ucontrol;
2624 ucontrol = kzalloc(sizeof(*ucontrol), GFP_KERNEL);
2627 ucontrol->value.integer.value[0] = val;
2628 ucontrol->value.integer.value[1] = val;
2629 kctl->put(kctl, ucontrol);
2634 /* initialize the slave volume with 0dB */
2635 static int init_slave_0dB(void *data, struct snd_kcontrol *slave)
2637 int offset = get_kctl_0dB_offset(slave);
2639 put_kctl_with_value(slave, offset);
2643 /* unmute the slave */
2644 static int init_slave_unmute(void *data, struct snd_kcontrol *slave)
2646 return put_kctl_with_value(slave, 1);
2650 * snd_hda_add_vmaster - create a virtual master control and add slaves
2651 * @codec: HD-audio codec
2652 * @name: vmaster control name
2653 * @tlv: TLV data (optional)
2654 * @slaves: slave control names (optional)
2655 * @suffix: suffix string to each slave name (optional)
2656 * @init_slave_vol: initialize slaves to unmute/0dB
2657 * @ctl_ret: store the vmaster kcontrol in return
2659 * Create a virtual master control with the given name. The TLV data
2660 * must be either NULL or a valid data.
2662 * @slaves is a NULL-terminated array of strings, each of which is a
2663 * slave control name. All controls with these names are assigned to
2664 * the new virtual master control.
2666 * This function returns zero if successful or a negative error code.
2668 int __snd_hda_add_vmaster(struct hda_codec *codec, char *name,
2669 unsigned int *tlv, const char * const *slaves,
2670 const char *suffix, bool init_slave_vol,
2671 struct snd_kcontrol **ctl_ret)
2673 struct snd_kcontrol *kctl;
2679 err = map_slaves(codec, slaves, suffix, check_slave_present, NULL);
2681 snd_printdd("No slave found for %s\n", name);
2684 kctl = snd_ctl_make_virtual_master(name, tlv);
2687 err = snd_hda_ctl_add(codec, 0, kctl);
2691 err = map_slaves(codec, slaves, suffix,
2692 (map_slave_func_t)snd_ctl_add_slave, kctl);
2696 /* init with master mute & zero volume */
2697 put_kctl_with_value(kctl, 0);
2699 map_slaves(codec, slaves, suffix,
2700 tlv ? init_slave_0dB : init_slave_unmute, kctl);
2706 EXPORT_SYMBOL_HDA(__snd_hda_add_vmaster);
2709 * mute-LED control using vmaster
2711 static int vmaster_mute_mode_info(struct snd_kcontrol *kcontrol,
2712 struct snd_ctl_elem_info *uinfo)
2714 static const char * const texts[] = {
2715 "On", "Off", "Follow Master"
2719 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2721 uinfo->value.enumerated.items = 3;
2722 index = uinfo->value.enumerated.item;
2725 strcpy(uinfo->value.enumerated.name, texts[index]);
2729 static int vmaster_mute_mode_get(struct snd_kcontrol *kcontrol,
2730 struct snd_ctl_elem_value *ucontrol)
2732 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2733 ucontrol->value.enumerated.item[0] = hook->mute_mode;
2737 static int vmaster_mute_mode_put(struct snd_kcontrol *kcontrol,
2738 struct snd_ctl_elem_value *ucontrol)
2740 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2741 unsigned int old_mode = hook->mute_mode;
2743 hook->mute_mode = ucontrol->value.enumerated.item[0];
2744 if (hook->mute_mode > HDA_VMUTE_FOLLOW_MASTER)
2745 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
2746 if (old_mode == hook->mute_mode)
2748 snd_hda_sync_vmaster_hook(hook);
2752 static struct snd_kcontrol_new vmaster_mute_mode = {
2753 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2754 .name = "Mute-LED Mode",
2755 .info = vmaster_mute_mode_info,
2756 .get = vmaster_mute_mode_get,
2757 .put = vmaster_mute_mode_put,
2761 * Add a mute-LED hook with the given vmaster switch kctl
2762 * "Mute-LED Mode" control is automatically created and associated with
2765 int snd_hda_add_vmaster_hook(struct hda_codec *codec,
2766 struct hda_vmaster_mute_hook *hook,
2767 bool expose_enum_ctl)
2769 struct snd_kcontrol *kctl;
2771 if (!hook->hook || !hook->sw_kctl)
2773 snd_ctl_add_vmaster_hook(hook->sw_kctl, hook->hook, codec);
2774 hook->codec = codec;
2775 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
2776 if (!expose_enum_ctl)
2778 kctl = snd_ctl_new1(&vmaster_mute_mode, hook);
2781 return snd_hda_ctl_add(codec, 0, kctl);
2783 EXPORT_SYMBOL_HDA(snd_hda_add_vmaster_hook);
2786 * Call the hook with the current value for synchronization
2787 * Should be called in init callback
2789 void snd_hda_sync_vmaster_hook(struct hda_vmaster_mute_hook *hook)
2791 if (!hook->hook || !hook->codec)
2793 /* don't call vmaster hook in the destructor since it might have
2794 * been already destroyed
2796 if (hook->codec->bus->shutdown)
2798 switch (hook->mute_mode) {
2799 case HDA_VMUTE_FOLLOW_MASTER:
2800 snd_ctl_sync_vmaster_hook(hook->sw_kctl);
2803 hook->hook(hook->codec, hook->mute_mode);
2807 EXPORT_SYMBOL_HDA(snd_hda_sync_vmaster_hook);
2811 * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch
2813 * The control element is supposed to have the private_value field
2814 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2816 int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
2817 struct snd_ctl_elem_info *uinfo)
2819 int chs = get_amp_channels(kcontrol);
2821 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2822 uinfo->count = chs == 3 ? 2 : 1;
2823 uinfo->value.integer.min = 0;
2824 uinfo->value.integer.max = 1;
2827 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_info);
2830 * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch
2832 * The control element is supposed to have the private_value field
2833 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2835 int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
2836 struct snd_ctl_elem_value *ucontrol)
2838 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2839 hda_nid_t nid = get_amp_nid(kcontrol);
2840 int chs = get_amp_channels(kcontrol);
2841 int dir = get_amp_direction(kcontrol);
2842 int idx = get_amp_index(kcontrol);
2843 long *valp = ucontrol->value.integer.value;
2846 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
2847 HDA_AMP_MUTE) ? 0 : 1;
2849 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
2850 HDA_AMP_MUTE) ? 0 : 1;
2853 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_get);
2856 * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch
2858 * The control element is supposed to have the private_value field
2859 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2861 int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
2862 struct snd_ctl_elem_value *ucontrol)
2864 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2865 hda_nid_t nid = get_amp_nid(kcontrol);
2866 int chs = get_amp_channels(kcontrol);
2867 int dir = get_amp_direction(kcontrol);
2868 int idx = get_amp_index(kcontrol);
2869 long *valp = ucontrol->value.integer.value;
2872 snd_hda_power_up(codec);
2874 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
2876 *valp ? 0 : HDA_AMP_MUTE);
2880 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
2882 *valp ? 0 : HDA_AMP_MUTE);
2883 hda_call_check_power_status(codec, nid);
2884 snd_hda_power_down(codec);
2887 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put);
2890 * bound volume controls
2892 * bind multiple volumes (# indices, from 0)
2895 #define AMP_VAL_IDX_SHIFT 19
2896 #define AMP_VAL_IDX_MASK (0x0f<<19)
2899 * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control
2901 * The control element is supposed to have the private_value field
2902 * set up via HDA_BIND_MUTE*() macros.
2904 int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
2905 struct snd_ctl_elem_value *ucontrol)
2907 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2911 mutex_lock(&codec->control_mutex);
2912 pval = kcontrol->private_value;
2913 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
2914 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
2915 kcontrol->private_value = pval;
2916 mutex_unlock(&codec->control_mutex);
2919 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_get);
2922 * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control
2924 * The control element is supposed to have the private_value field
2925 * set up via HDA_BIND_MUTE*() macros.
2927 int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
2928 struct snd_ctl_elem_value *ucontrol)
2930 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2932 int i, indices, err = 0, change = 0;
2934 mutex_lock(&codec->control_mutex);
2935 pval = kcontrol->private_value;
2936 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
2937 for (i = 0; i < indices; i++) {
2938 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
2939 (i << AMP_VAL_IDX_SHIFT);
2940 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2945 kcontrol->private_value = pval;
2946 mutex_unlock(&codec->control_mutex);
2947 return err < 0 ? err : change;
2949 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_put);
2952 * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control
2954 * The control element is supposed to have the private_value field
2955 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2957 int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
2958 struct snd_ctl_elem_info *uinfo)
2960 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2961 struct hda_bind_ctls *c;
2964 mutex_lock(&codec->control_mutex);
2965 c = (struct hda_bind_ctls *)kcontrol->private_value;
2966 kcontrol->private_value = *c->values;
2967 err = c->ops->info(kcontrol, uinfo);
2968 kcontrol->private_value = (long)c;
2969 mutex_unlock(&codec->control_mutex);
2972 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_info);
2975 * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control
2977 * The control element is supposed to have the private_value field
2978 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2980 int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
2981 struct snd_ctl_elem_value *ucontrol)
2983 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2984 struct hda_bind_ctls *c;
2987 mutex_lock(&codec->control_mutex);
2988 c = (struct hda_bind_ctls *)kcontrol->private_value;
2989 kcontrol->private_value = *c->values;
2990 err = c->ops->get(kcontrol, ucontrol);
2991 kcontrol->private_value = (long)c;
2992 mutex_unlock(&codec->control_mutex);
2995 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_get);
2998 * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control
3000 * The control element is supposed to have the private_value field
3001 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
3003 int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
3004 struct snd_ctl_elem_value *ucontrol)
3006 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3007 struct hda_bind_ctls *c;
3008 unsigned long *vals;
3009 int err = 0, change = 0;
3011 mutex_lock(&codec->control_mutex);
3012 c = (struct hda_bind_ctls *)kcontrol->private_value;
3013 for (vals = c->values; *vals; vals++) {
3014 kcontrol->private_value = *vals;
3015 err = c->ops->put(kcontrol, ucontrol);
3020 kcontrol->private_value = (long)c;
3021 mutex_unlock(&codec->control_mutex);
3022 return err < 0 ? err : change;
3024 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_put);
3027 * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control
3029 * The control element is supposed to have the private_value field
3030 * set up via HDA_BIND_VOL() macro.
3032 int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
3033 unsigned int size, unsigned int __user *tlv)
3035 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3036 struct hda_bind_ctls *c;
3039 mutex_lock(&codec->control_mutex);
3040 c = (struct hda_bind_ctls *)kcontrol->private_value;
3041 kcontrol->private_value = *c->values;
3042 err = c->ops->tlv(kcontrol, op_flag, size, tlv);
3043 kcontrol->private_value = (long)c;
3044 mutex_unlock(&codec->control_mutex);
3047 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_tlv);
3049 struct hda_ctl_ops snd_hda_bind_vol = {
3050 .info = snd_hda_mixer_amp_volume_info,
3051 .get = snd_hda_mixer_amp_volume_get,
3052 .put = snd_hda_mixer_amp_volume_put,
3053 .tlv = snd_hda_mixer_amp_tlv
3055 EXPORT_SYMBOL_HDA(snd_hda_bind_vol);
3057 struct hda_ctl_ops snd_hda_bind_sw = {
3058 .info = snd_hda_mixer_amp_switch_info,
3059 .get = snd_hda_mixer_amp_switch_get,
3060 .put = snd_hda_mixer_amp_switch_put,
3061 .tlv = snd_hda_mixer_amp_tlv
3063 EXPORT_SYMBOL_HDA(snd_hda_bind_sw);
3066 * SPDIF out controls
3069 static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
3070 struct snd_ctl_elem_info *uinfo)
3072 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
3077 static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
3078 struct snd_ctl_elem_value *ucontrol)
3080 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
3081 IEC958_AES0_NONAUDIO |
3082 IEC958_AES0_CON_EMPHASIS_5015 |
3083 IEC958_AES0_CON_NOT_COPYRIGHT;
3084 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
3085 IEC958_AES1_CON_ORIGINAL;
3089 static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
3090 struct snd_ctl_elem_value *ucontrol)
3092 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
3093 IEC958_AES0_NONAUDIO |
3094 IEC958_AES0_PRO_EMPHASIS_5015;
3098 static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
3099 struct snd_ctl_elem_value *ucontrol)
3101 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3102 int idx = kcontrol->private_value;
3103 struct hda_spdif_out *spdif;
3105 mutex_lock(&codec->spdif_mutex);
3106 spdif = snd_array_elem(&codec->spdif_out, idx);
3107 ucontrol->value.iec958.status[0] = spdif->status & 0xff;
3108 ucontrol->value.iec958.status[1] = (spdif->status >> 8) & 0xff;
3109 ucontrol->value.iec958.status[2] = (spdif->status >> 16) & 0xff;
3110 ucontrol->value.iec958.status[3] = (spdif->status >> 24) & 0xff;
3111 mutex_unlock(&codec->spdif_mutex);
3116 /* convert from SPDIF status bits to HDA SPDIF bits
3117 * bit 0 (DigEn) is always set zero (to be filled later)
3119 static unsigned short convert_from_spdif_status(unsigned int sbits)
3121 unsigned short val = 0;
3123 if (sbits & IEC958_AES0_PROFESSIONAL)
3124 val |= AC_DIG1_PROFESSIONAL;
3125 if (sbits & IEC958_AES0_NONAUDIO)
3126 val |= AC_DIG1_NONAUDIO;
3127 if (sbits & IEC958_AES0_PROFESSIONAL) {
3128 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
3129 IEC958_AES0_PRO_EMPHASIS_5015)
3130 val |= AC_DIG1_EMPHASIS;
3132 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
3133 IEC958_AES0_CON_EMPHASIS_5015)
3134 val |= AC_DIG1_EMPHASIS;
3135 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
3136 val |= AC_DIG1_COPYRIGHT;
3137 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
3138 val |= AC_DIG1_LEVEL;
3139 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
3144 /* convert to SPDIF status bits from HDA SPDIF bits
3146 static unsigned int convert_to_spdif_status(unsigned short val)
3148 unsigned int sbits = 0;
3150 if (val & AC_DIG1_NONAUDIO)
3151 sbits |= IEC958_AES0_NONAUDIO;
3152 if (val & AC_DIG1_PROFESSIONAL)
3153 sbits |= IEC958_AES0_PROFESSIONAL;
3154 if (sbits & IEC958_AES0_PROFESSIONAL) {
3155 if (val & AC_DIG1_EMPHASIS)
3156 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
3158 if (val & AC_DIG1_EMPHASIS)
3159 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
3160 if (!(val & AC_DIG1_COPYRIGHT))
3161 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
3162 if (val & AC_DIG1_LEVEL)
3163 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
3164 sbits |= val & (0x7f << 8);
3169 /* set digital convert verbs both for the given NID and its slaves */
3170 static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
3175 snd_hda_codec_write_cache(codec, nid, 0, verb, val);
3176 d = codec->slave_dig_outs;
3180 snd_hda_codec_write_cache(codec, *d, 0, verb, val);
3183 static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
3187 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1);
3189 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2);
3192 static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
3193 struct snd_ctl_elem_value *ucontrol)
3195 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3196 int idx = kcontrol->private_value;
3197 struct hda_spdif_out *spdif;
3202 mutex_lock(&codec->spdif_mutex);
3203 spdif = snd_array_elem(&codec->spdif_out, idx);
3205 spdif->status = ucontrol->value.iec958.status[0] |
3206 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
3207 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
3208 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
3209 val = convert_from_spdif_status(spdif->status);
3210 val |= spdif->ctls & 1;
3211 change = spdif->ctls != val;
3213 if (change && nid != (u16)-1)
3214 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
3215 mutex_unlock(&codec->spdif_mutex);
3219 #define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
3221 static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
3222 struct snd_ctl_elem_value *ucontrol)
3224 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3225 int idx = kcontrol->private_value;
3226 struct hda_spdif_out *spdif;
3228 mutex_lock(&codec->spdif_mutex);
3229 spdif = snd_array_elem(&codec->spdif_out, idx);
3230 ucontrol->value.integer.value[0] = spdif->ctls & AC_DIG1_ENABLE;
3231 mutex_unlock(&codec->spdif_mutex);
3235 static inline void set_spdif_ctls(struct hda_codec *codec, hda_nid_t nid,
3238 set_dig_out_convert(codec, nid, dig1, dig2);
3239 /* unmute amp switch (if any) */
3240 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
3241 (dig1 & AC_DIG1_ENABLE))
3242 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
3246 static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
3247 struct snd_ctl_elem_value *ucontrol)
3249 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3250 int idx = kcontrol->private_value;
3251 struct hda_spdif_out *spdif;
3256 mutex_lock(&codec->spdif_mutex);
3257 spdif = snd_array_elem(&codec->spdif_out, idx);
3259 val = spdif->ctls & ~AC_DIG1_ENABLE;
3260 if (ucontrol->value.integer.value[0])
3261 val |= AC_DIG1_ENABLE;
3262 change = spdif->ctls != val;
3264 if (change && nid != (u16)-1)
3265 set_spdif_ctls(codec, nid, val & 0xff, -1);
3266 mutex_unlock(&codec->spdif_mutex);
3270 static struct snd_kcontrol_new dig_mixes[] = {
3272 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3273 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3274 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
3275 .info = snd_hda_spdif_mask_info,
3276 .get = snd_hda_spdif_cmask_get,
3279 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3280 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3281 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK),
3282 .info = snd_hda_spdif_mask_info,
3283 .get = snd_hda_spdif_pmask_get,
3286 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3287 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
3288 .info = snd_hda_spdif_mask_info,
3289 .get = snd_hda_spdif_default_get,
3290 .put = snd_hda_spdif_default_put,
3293 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3294 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
3295 .info = snd_hda_spdif_out_switch_info,
3296 .get = snd_hda_spdif_out_switch_get,
3297 .put = snd_hda_spdif_out_switch_put,
3303 * snd_hda_create_dig_out_ctls - create Output SPDIF-related controls
3304 * @codec: the HDA codec
3305 * @associated_nid: NID that new ctls associated with
3306 * @cvt_nid: converter NID
3307 * @type: HDA_PCM_TYPE_*
3308 * Creates controls related with the digital output.
3309 * Called from each patch supporting the digital out.
3311 * Returns 0 if successful, or a negative error code.
3313 int snd_hda_create_dig_out_ctls(struct hda_codec *codec,
3314 hda_nid_t associated_nid,
3319 struct snd_kcontrol *kctl;
3320 struct snd_kcontrol_new *dig_mix;
3322 const int spdif_index = 16;
3323 struct hda_spdif_out *spdif;
3324 struct hda_bus *bus = codec->bus;
3326 if (bus->primary_dig_out_type == HDA_PCM_TYPE_HDMI &&
3327 type == HDA_PCM_TYPE_SPDIF) {
3329 } else if (bus->primary_dig_out_type == HDA_PCM_TYPE_SPDIF &&
3330 type == HDA_PCM_TYPE_HDMI) {
3331 /* suppose a single SPDIF device */
3332 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
3333 kctl = find_mixer_ctl(codec, dig_mix->name, 0, 0);
3336 kctl->id.index = spdif_index;
3338 bus->primary_dig_out_type = HDA_PCM_TYPE_HDMI;
3340 if (!bus->primary_dig_out_type)
3341 bus->primary_dig_out_type = type;
3343 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Playback Switch", idx);
3345 printk(KERN_ERR "hda_codec: too many IEC958 outputs\n");
3348 spdif = snd_array_new(&codec->spdif_out);
3351 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
3352 kctl = snd_ctl_new1(dig_mix, codec);
3355 kctl->id.index = idx;
3356 kctl->private_value = codec->spdif_out.used - 1;
3357 err = snd_hda_ctl_add(codec, associated_nid, kctl);
3361 spdif->nid = cvt_nid;
3362 spdif->ctls = snd_hda_codec_read(codec, cvt_nid, 0,
3363 AC_VERB_GET_DIGI_CONVERT_1, 0);
3364 spdif->status = convert_to_spdif_status(spdif->ctls);
3367 EXPORT_SYMBOL_HDA(snd_hda_create_dig_out_ctls);
3369 /* get the hda_spdif_out entry from the given NID
3370 * call within spdif_mutex lock
3372 struct hda_spdif_out *snd_hda_spdif_out_of_nid(struct hda_codec *codec,
3376 for (i = 0; i < codec->spdif_out.used; i++) {
3377 struct hda_spdif_out *spdif =
3378 snd_array_elem(&codec->spdif_out, i);
3379 if (spdif->nid == nid)
3384 EXPORT_SYMBOL_HDA(snd_hda_spdif_out_of_nid);
3386 void snd_hda_spdif_ctls_unassign(struct hda_codec *codec, int idx)
3388 struct hda_spdif_out *spdif;
3390 mutex_lock(&codec->spdif_mutex);
3391 spdif = snd_array_elem(&codec->spdif_out, idx);
3392 spdif->nid = (u16)-1;
3393 mutex_unlock(&codec->spdif_mutex);
3395 EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_unassign);
3397 void snd_hda_spdif_ctls_assign(struct hda_codec *codec, int idx, hda_nid_t nid)
3399 struct hda_spdif_out *spdif;
3402 mutex_lock(&codec->spdif_mutex);
3403 spdif = snd_array_elem(&codec->spdif_out, idx);
3404 if (spdif->nid != nid) {
3407 set_spdif_ctls(codec, nid, val & 0xff, (val >> 8) & 0xff);
3409 mutex_unlock(&codec->spdif_mutex);
3411 EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_assign);
3414 * SPDIF sharing with analog output
3416 static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
3417 struct snd_ctl_elem_value *ucontrol)
3419 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3420 ucontrol->value.integer.value[0] = mout->share_spdif;
3424 static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
3425 struct snd_ctl_elem_value *ucontrol)
3427 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3428 mout->share_spdif = !!ucontrol->value.integer.value[0];
3432 static struct snd_kcontrol_new spdif_share_sw = {
3433 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3434 .name = "IEC958 Default PCM Playback Switch",
3435 .info = snd_ctl_boolean_mono_info,
3436 .get = spdif_share_sw_get,
3437 .put = spdif_share_sw_put,
3441 * snd_hda_create_spdif_share_sw - create Default PCM switch
3442 * @codec: the HDA codec
3443 * @mout: multi-out instance
3445 int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
3446 struct hda_multi_out *mout)
3448 struct snd_kcontrol *kctl;
3450 if (!mout->dig_out_nid)
3453 kctl = snd_ctl_new1(&spdif_share_sw, mout);
3456 /* ATTENTION: here mout is passed as private_data, instead of codec */
3457 return snd_hda_ctl_add(codec, mout->dig_out_nid, kctl);
3459 EXPORT_SYMBOL_HDA(snd_hda_create_spdif_share_sw);
3465 #define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
3467 static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
3468 struct snd_ctl_elem_value *ucontrol)
3470 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3472 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
3476 static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
3477 struct snd_ctl_elem_value *ucontrol)
3479 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3480 hda_nid_t nid = kcontrol->private_value;
3481 unsigned int val = !!ucontrol->value.integer.value[0];
3484 mutex_lock(&codec->spdif_mutex);
3485 change = codec->spdif_in_enable != val;
3487 codec->spdif_in_enable = val;
3488 snd_hda_codec_write_cache(codec, nid, 0,
3489 AC_VERB_SET_DIGI_CONVERT_1, val);
3491 mutex_unlock(&codec->spdif_mutex);
3495 static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
3496 struct snd_ctl_elem_value *ucontrol)
3498 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3499 hda_nid_t nid = kcontrol->private_value;
3503 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0);
3504 sbits = convert_to_spdif_status(val);
3505 ucontrol->value.iec958.status[0] = sbits;
3506 ucontrol->value.iec958.status[1] = sbits >> 8;
3507 ucontrol->value.iec958.status[2] = sbits >> 16;
3508 ucontrol->value.iec958.status[3] = sbits >> 24;
3512 static struct snd_kcontrol_new dig_in_ctls[] = {
3514 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3515 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH),
3516 .info = snd_hda_spdif_in_switch_info,
3517 .get = snd_hda_spdif_in_switch_get,
3518 .put = snd_hda_spdif_in_switch_put,
3521 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3522 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3523 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
3524 .info = snd_hda_spdif_mask_info,
3525 .get = snd_hda_spdif_in_status_get,
3531 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
3532 * @codec: the HDA codec
3533 * @nid: audio in widget NID
3535 * Creates controls related with the SPDIF input.
3536 * Called from each patch supporting the SPDIF in.
3538 * Returns 0 if successful, or a negative error code.
3540 int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
3543 struct snd_kcontrol *kctl;
3544 struct snd_kcontrol_new *dig_mix;
3547 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Capture Switch", 0);
3549 printk(KERN_ERR "hda_codec: too many IEC958 inputs\n");
3552 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
3553 kctl = snd_ctl_new1(dig_mix, codec);
3556 kctl->private_value = nid;
3557 err = snd_hda_ctl_add(codec, nid, kctl);
3561 codec->spdif_in_enable =
3562 snd_hda_codec_read(codec, nid, 0,
3563 AC_VERB_GET_DIGI_CONVERT_1, 0) &
3567 EXPORT_SYMBOL_HDA(snd_hda_create_spdif_in_ctls);
3573 /* build a 31bit cache key with the widget id and the command parameter */
3574 #define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
3575 #define get_cmd_cache_nid(key) ((key) & 0xff)
3576 #define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
3579 * snd_hda_codec_write_cache - send a single command with caching
3580 * @codec: the HDA codec
3581 * @nid: NID to send the command
3582 * @direct: direct flag
3583 * @verb: the verb to send
3584 * @parm: the parameter for the verb
3586 * Send a single command without waiting for response.
3588 * Returns 0 if successful, or a negative error code.
3590 int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
3591 int direct, unsigned int verb, unsigned int parm)
3594 struct hda_cache_head *c;
3596 unsigned int cache_only;
3598 cache_only = codec->cached_write;
3600 err = snd_hda_codec_write(codec, nid, direct, verb, parm);
3605 /* parm may contain the verb stuff for get/set amp */
3606 verb = verb | (parm >> 8);
3608 key = build_cmd_cache_key(nid, verb);
3609 mutex_lock(&codec->bus->cmd_mutex);
3610 c = get_alloc_hash(&codec->cmd_cache, key);
3613 c->dirty = cache_only;
3615 mutex_unlock(&codec->bus->cmd_mutex);
3618 EXPORT_SYMBOL_HDA(snd_hda_codec_write_cache);
3621 * snd_hda_codec_update_cache - check cache and write the cmd only when needed
3622 * @codec: the HDA codec
3623 * @nid: NID to send the command
3624 * @direct: direct flag
3625 * @verb: the verb to send
3626 * @parm: the parameter for the verb
3628 * This function works like snd_hda_codec_write_cache(), but it doesn't send
3629 * command if the parameter is already identical with the cached value.
3630 * If not, it sends the command and refreshes the cache.
3632 * Returns 0 if successful, or a negative error code.
3634 int snd_hda_codec_update_cache(struct hda_codec *codec, hda_nid_t nid,
3635 int direct, unsigned int verb, unsigned int parm)
3637 struct hda_cache_head *c;
3640 /* parm may contain the verb stuff for get/set amp */
3641 verb = verb | (parm >> 8);
3643 key = build_cmd_cache_key(nid, verb);
3644 mutex_lock(&codec->bus->cmd_mutex);
3645 c = get_hash(&codec->cmd_cache, key);
3646 if (c && c->val == parm) {
3647 mutex_unlock(&codec->bus->cmd_mutex);
3650 mutex_unlock(&codec->bus->cmd_mutex);
3651 return snd_hda_codec_write_cache(codec, nid, direct, verb, parm);
3653 EXPORT_SYMBOL_HDA(snd_hda_codec_update_cache);
3656 * snd_hda_codec_resume_cache - Resume the all commands from the cache
3657 * @codec: HD-audio codec
3659 * Execute all verbs recorded in the command caches to resume.
3661 void snd_hda_codec_resume_cache(struct hda_codec *codec)
3665 mutex_lock(&codec->hash_mutex);
3666 codec->cached_write = 0;
3667 for (i = 0; i < codec->cmd_cache.buf.used; i++) {
3668 struct hda_cache_head *buffer;
3671 buffer = snd_array_elem(&codec->cmd_cache.buf, i);
3678 mutex_unlock(&codec->hash_mutex);
3679 snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
3680 get_cmd_cache_cmd(key), buffer->val);
3681 mutex_lock(&codec->hash_mutex);
3683 mutex_unlock(&codec->hash_mutex);
3685 EXPORT_SYMBOL_HDA(snd_hda_codec_resume_cache);
3688 * snd_hda_sequence_write_cache - sequence writes with caching
3689 * @codec: the HDA codec
3690 * @seq: VERB array to send
3692 * Send the commands sequentially from the given array.
3693 * Thte commands are recorded on cache for power-save and resume.
3694 * The array must be terminated with NID=0.
3696 void snd_hda_sequence_write_cache(struct hda_codec *codec,
3697 const struct hda_verb *seq)
3699 for (; seq->nid; seq++)
3700 snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
3703 EXPORT_SYMBOL_HDA(snd_hda_sequence_write_cache);
3706 * snd_hda_codec_flush_cache - Execute all pending (cached) amps / verbs
3707 * @codec: HD-audio codec
3709 void snd_hda_codec_flush_cache(struct hda_codec *codec)
3711 snd_hda_codec_resume_amp(codec);
3712 snd_hda_codec_resume_cache(codec);
3714 EXPORT_SYMBOL_HDA(snd_hda_codec_flush_cache);
3716 void snd_hda_codec_set_power_to_all(struct hda_codec *codec, hda_nid_t fg,
3717 unsigned int power_state)
3719 hda_nid_t nid = codec->start_nid;
3722 for (i = 0; i < codec->num_nodes; i++, nid++) {
3723 unsigned int wcaps = get_wcaps(codec, nid);
3724 unsigned int state = power_state;
3725 if (!(wcaps & AC_WCAP_POWER))
3727 if (codec->power_filter) {
3728 state = codec->power_filter(codec, nid, power_state);
3729 if (state != power_state && power_state == AC_PWRST_D3)
3732 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE,
3736 EXPORT_SYMBOL_HDA(snd_hda_codec_set_power_to_all);
3739 * supported power states check
3741 static bool snd_hda_codec_get_supported_ps(struct hda_codec *codec, hda_nid_t fg,
3742 unsigned int power_state)
3744 int sup = snd_hda_param_read(codec, fg, AC_PAR_POWER_STATE);
3748 if (sup & power_state)
3755 * wait until the state is reached, returns the current state
3757 static unsigned int hda_sync_power_state(struct hda_codec *codec,
3759 unsigned int power_state)
3761 unsigned long end_time = jiffies + msecs_to_jiffies(500);
3762 unsigned int state, actual_state;
3765 state = snd_hda_codec_read(codec, fg, 0,
3766 AC_VERB_GET_POWER_STATE, 0);
3767 if (state & AC_PWRST_ERROR)
3769 actual_state = (state >> 4) & 0x0f;
3770 if (actual_state == power_state)
3772 if (time_after_eq(jiffies, end_time))
3774 /* wait until the codec reachs to the target state */
3780 /* don't power down the widget if it controls eapd and EAPD_BTLENABLE is set */
3781 unsigned int snd_hda_codec_eapd_power_filter(struct hda_codec *codec,
3783 unsigned int power_state)
3785 if (power_state == AC_PWRST_D3 &&
3786 get_wcaps_type(get_wcaps(codec, nid)) == AC_WID_PIN &&
3787 (snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD)) {
3788 int eapd = snd_hda_codec_read(codec, nid, 0,
3789 AC_VERB_GET_EAPD_BTLENABLE, 0);
3795 EXPORT_SYMBOL_HDA(snd_hda_codec_eapd_power_filter);
3798 * set power state of the codec, and return the power state
3800 static unsigned int hda_set_power_state(struct hda_codec *codec,
3801 unsigned int power_state)
3803 hda_nid_t fg = codec->afg ? codec->afg : codec->mfg;
3807 /* this delay seems necessary to avoid click noise at power-down */
3808 if (power_state == AC_PWRST_D3) {
3809 /* transition time less than 10ms for power down */
3810 msleep(codec->epss ? 10 : 100);
3813 /* repeat power states setting at most 10 times*/
3814 for (count = 0; count < 10; count++) {
3815 if (codec->patch_ops.set_power_state)
3816 codec->patch_ops.set_power_state(codec, fg,
3819 snd_hda_codec_read(codec, fg, 0,
3820 AC_VERB_SET_POWER_STATE,
3822 snd_hda_codec_set_power_to_all(codec, fg, power_state);
3824 state = hda_sync_power_state(codec, fg, power_state);
3825 if (!(state & AC_PWRST_ERROR))
3832 /* sync power states of all widgets;
3833 * this is called at the end of codec parsing
3835 static void sync_power_up_states(struct hda_codec *codec)
3837 hda_nid_t nid = codec->start_nid;
3840 /* don't care if no filter is used */
3841 if (!codec->power_filter)
3844 for (i = 0; i < codec->num_nodes; i++, nid++) {
3845 unsigned int wcaps = get_wcaps(codec, nid);
3846 unsigned int target;
3847 if (!(wcaps & AC_WCAP_POWER))
3849 target = codec->power_filter(codec, nid, AC_PWRST_D0);
3850 if (target == AC_PWRST_D0)
3852 if (!snd_hda_check_power_state(codec, nid, target))
3853 snd_hda_codec_write(codec, nid, 0,
3854 AC_VERB_SET_POWER_STATE, target);
3858 #ifdef CONFIG_SND_HDA_HWDEP
3859 /* execute additional init verbs */
3860 static void hda_exec_init_verbs(struct hda_codec *codec)
3862 if (codec->init_verbs.list)
3863 snd_hda_sequence_write(codec, codec->init_verbs.list);
3866 static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
3871 * call suspend and power-down; used both from PM and power-save
3872 * this function returns the power state in the end
3874 static unsigned int hda_call_codec_suspend(struct hda_codec *codec, bool in_wq)
3880 if (codec->patch_ops.suspend)
3881 codec->patch_ops.suspend(codec);
3882 hda_cleanup_all_streams(codec);
3883 state = hda_set_power_state(codec, AC_PWRST_D3);
3884 /* Cancel delayed work if we aren't currently running from it. */
3886 cancel_delayed_work_sync(&codec->power_work);
3887 spin_lock(&codec->power_lock);
3888 snd_hda_update_power_acct(codec);
3889 trace_hda_power_down(codec);
3890 codec->power_on = 0;
3891 codec->power_transition = 0;
3892 codec->power_jiffies = jiffies;
3893 spin_unlock(&codec->power_lock);
3898 /* mark all entries of cmd and amp caches dirty */
3899 static void hda_mark_cmd_cache_dirty(struct hda_codec *codec)
3902 for (i = 0; i < codec->cmd_cache.buf.used; i++) {
3903 struct hda_cache_head *cmd;
3904 cmd = snd_array_elem(&codec->cmd_cache.buf, i);
3907 for (i = 0; i < codec->amp_cache.buf.used; i++) {
3908 struct hda_amp_info *amp;
3909 amp = snd_array_elem(&codec->amp_cache.buf, i);
3910 amp->head.dirty = 1;
3915 * kick up codec; used both from PM and power-save
3917 static void hda_call_codec_resume(struct hda_codec *codec)
3921 hda_mark_cmd_cache_dirty(codec);
3923 /* set as if powered on for avoiding re-entering the resume
3924 * in the resume / power-save sequence
3926 hda_keep_power_on(codec);
3927 if (codec->pm_down_notified) {
3928 codec->pm_down_notified = 0;
3929 hda_call_pm_notify(codec->bus, true);
3931 hda_set_power_state(codec, AC_PWRST_D0);
3932 restore_shutup_pins(codec);
3933 hda_exec_init_verbs(codec);
3934 snd_hda_jack_set_dirty_all(codec);
3935 if (codec->patch_ops.resume)
3936 codec->patch_ops.resume(codec);
3938 if (codec->patch_ops.init)
3939 codec->patch_ops.init(codec);
3940 snd_hda_codec_resume_amp(codec);
3941 snd_hda_codec_resume_cache(codec);
3944 if (codec->jackpoll_interval)
3945 hda_jackpoll_work(&codec->jackpoll_work.work);
3947 snd_hda_jack_report_sync(codec);
3950 snd_hda_power_down(codec); /* flag down before returning */
3952 #endif /* CONFIG_PM */
3956 * snd_hda_build_controls - build mixer controls
3959 * Creates mixer controls for each codec included in the bus.
3961 * Returns 0 if successful, otherwise a negative error code.
3963 int snd_hda_build_controls(struct hda_bus *bus)
3965 struct hda_codec *codec;
3967 list_for_each_entry(codec, &bus->codec_list, list) {
3968 int err = snd_hda_codec_build_controls(codec);
3970 printk(KERN_ERR "hda_codec: cannot build controls "
3971 "for #%d (error %d)\n", codec->addr, err);
3972 err = snd_hda_codec_reset(codec);
3975 "hda_codec: cannot revert codec\n");
3982 EXPORT_SYMBOL_HDA(snd_hda_build_controls);
3985 * add standard channel maps if not specified
3987 static int add_std_chmaps(struct hda_codec *codec)
3991 for (i = 0; i < codec->num_pcms; i++) {
3992 for (str = 0; str < 2; str++) {
3993 struct snd_pcm *pcm = codec->pcm_info[i].pcm;
3994 struct hda_pcm_stream *hinfo =
3995 &codec->pcm_info[i].stream[str];
3996 struct snd_pcm_chmap *chmap;
3997 const struct snd_pcm_chmap_elem *elem;
3999 if (codec->pcm_info[i].own_chmap)
4001 if (!pcm || !hinfo->substreams)
4003 elem = hinfo->chmap ? hinfo->chmap : snd_pcm_std_chmaps;
4004 err = snd_pcm_add_chmap_ctls(pcm, str, elem,
4005 hinfo->channels_max,
4009 chmap->channel_mask = SND_PCM_CHMAP_MASK_2468;
4015 /* default channel maps for 2.1 speakers;
4016 * since HD-audio supports only stereo, odd number channels are omitted
4018 const struct snd_pcm_chmap_elem snd_pcm_2_1_chmaps[] = {
4020 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } },
4022 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
4023 SNDRV_CHMAP_LFE, SNDRV_CHMAP_LFE } },
4026 EXPORT_SYMBOL_GPL(snd_pcm_2_1_chmaps);
4028 int snd_hda_codec_build_controls(struct hda_codec *codec)
4031 hda_exec_init_verbs(codec);
4032 /* continue to initialize... */
4033 if (codec->patch_ops.init)
4034 err = codec->patch_ops.init(codec);
4035 if (!err && codec->patch_ops.build_controls)
4036 err = codec->patch_ops.build_controls(codec);
4040 /* we create chmaps here instead of build_pcms */
4041 err = add_std_chmaps(codec);
4045 if (codec->jackpoll_interval)
4046 hda_jackpoll_work(&codec->jackpoll_work.work);
4048 snd_hda_jack_report_sync(codec); /* call at the last init point */
4049 sync_power_up_states(codec);
4056 struct hda_rate_tbl {
4058 unsigned int alsa_bits;
4059 unsigned int hda_fmt;
4062 /* rate = base * mult / div */
4063 #define HDA_RATE(base, mult, div) \
4064 (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \
4065 (((div) - 1) << AC_FMT_DIV_SHIFT))
4067 static struct hda_rate_tbl rate_bits[] = {
4068 /* rate in Hz, ALSA rate bitmask, HDA format value */
4070 /* autodetected value used in snd_hda_query_supported_pcm */
4071 { 8000, SNDRV_PCM_RATE_8000, HDA_RATE(48, 1, 6) },
4072 { 11025, SNDRV_PCM_RATE_11025, HDA_RATE(44, 1, 4) },
4073 { 16000, SNDRV_PCM_RATE_16000, HDA_RATE(48, 1, 3) },
4074 { 22050, SNDRV_PCM_RATE_22050, HDA_RATE(44, 1, 2) },
4075 { 32000, SNDRV_PCM_RATE_32000, HDA_RATE(48, 2, 3) },
4076 { 44100, SNDRV_PCM_RATE_44100, HDA_RATE(44, 1, 1) },
4077 { 48000, SNDRV_PCM_RATE_48000, HDA_RATE(48, 1, 1) },
4078 { 88200, SNDRV_PCM_RATE_88200, HDA_RATE(44, 2, 1) },
4079 { 96000, SNDRV_PCM_RATE_96000, HDA_RATE(48, 2, 1) },
4080 { 176400, SNDRV_PCM_RATE_176400, HDA_RATE(44, 4, 1) },
4081 { 192000, SNDRV_PCM_RATE_192000, HDA_RATE(48, 4, 1) },
4082 #define AC_PAR_PCM_RATE_BITS 11
4083 /* up to bits 10, 384kHZ isn't supported properly */
4085 /* not autodetected value */
4086 { 9600, SNDRV_PCM_RATE_KNOT, HDA_RATE(48, 1, 5) },
4088 { 0 } /* terminator */
4092 * snd_hda_calc_stream_format - calculate format bitset
4093 * @rate: the sample rate
4094 * @channels: the number of channels
4095 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
4096 * @maxbps: the max. bps
4098 * Calculate the format bitset from the given rate, channels and th PCM format.
4100 * Return zero if invalid.
4102 unsigned int snd_hda_calc_stream_format(unsigned int rate,
4103 unsigned int channels,
4104 unsigned int format,
4105 unsigned int maxbps,
4106 unsigned short spdif_ctls)
4109 unsigned int val = 0;
4111 for (i = 0; rate_bits[i].hz; i++)
4112 if (rate_bits[i].hz == rate) {
4113 val = rate_bits[i].hda_fmt;
4116 if (!rate_bits[i].hz) {
4117 snd_printdd("invalid rate %d\n", rate);
4121 if (channels == 0 || channels > 8) {
4122 snd_printdd("invalid channels %d\n", channels);
4125 val |= channels - 1;
4127 switch (snd_pcm_format_width(format)) {
4129 val |= AC_FMT_BITS_8;
4132 val |= AC_FMT_BITS_16;
4137 if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE)
4138 val |= AC_FMT_BITS_32;
4139 else if (maxbps >= 24)
4140 val |= AC_FMT_BITS_24;
4142 val |= AC_FMT_BITS_20;
4145 snd_printdd("invalid format width %d\n",
4146 snd_pcm_format_width(format));
4150 if (spdif_ctls & AC_DIG1_NONAUDIO)
4151 val |= AC_FMT_TYPE_NON_PCM;
4155 EXPORT_SYMBOL_HDA(snd_hda_calc_stream_format);
4157 static unsigned int get_pcm_param(struct hda_codec *codec, hda_nid_t nid,
4160 unsigned int val = 0;
4161 if (nid != codec->afg &&
4162 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD))
4163 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
4164 if (!val || val == -1)
4165 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
4166 if (!val || val == -1)
4171 static unsigned int query_pcm_param(struct hda_codec *codec, hda_nid_t nid)
4173 return query_caps_hash(codec, nid, 0, HDA_HASH_PARPCM_KEY(nid),
4177 static unsigned int get_stream_param(struct hda_codec *codec, hda_nid_t nid,
4180 unsigned int streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
4181 if (!streams || streams == -1)
4182 streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
4183 if (!streams || streams == -1)
4188 static unsigned int query_stream_param(struct hda_codec *codec, hda_nid_t nid)
4190 return query_caps_hash(codec, nid, 0, HDA_HASH_PARSTR_KEY(nid),
4195 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
4196 * @codec: the HDA codec
4197 * @nid: NID to query
4198 * @ratesp: the pointer to store the detected rate bitflags
4199 * @formatsp: the pointer to store the detected formats
4200 * @bpsp: the pointer to store the detected format widths
4202 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
4203 * or @bsps argument is ignored.
4205 * Returns 0 if successful, otherwise a negative error code.
4207 int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
4208 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
4210 unsigned int i, val, wcaps;
4212 wcaps = get_wcaps(codec, nid);
4213 val = query_pcm_param(codec, nid);
4217 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
4219 rates |= rate_bits[i].alsa_bits;
4222 snd_printk(KERN_ERR "hda_codec: rates == 0 "
4223 "(nid=0x%x, val=0x%x, ovrd=%i)\n",
4225 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0);
4231 if (formatsp || bpsp) {
4233 unsigned int streams, bps;
4235 streams = query_stream_param(codec, nid);
4240 if (streams & AC_SUPFMT_PCM) {
4241 if (val & AC_SUPPCM_BITS_8) {
4242 formats |= SNDRV_PCM_FMTBIT_U8;
4245 if (val & AC_SUPPCM_BITS_16) {
4246 formats |= SNDRV_PCM_FMTBIT_S16_LE;
4249 if (wcaps & AC_WCAP_DIGITAL) {
4250 if (val & AC_SUPPCM_BITS_32)
4251 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
4252 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
4253 formats |= SNDRV_PCM_FMTBIT_S32_LE;
4254 if (val & AC_SUPPCM_BITS_24)
4256 else if (val & AC_SUPPCM_BITS_20)
4258 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
4259 AC_SUPPCM_BITS_32)) {
4260 formats |= SNDRV_PCM_FMTBIT_S32_LE;
4261 if (val & AC_SUPPCM_BITS_32)
4263 else if (val & AC_SUPPCM_BITS_24)
4265 else if (val & AC_SUPPCM_BITS_20)
4269 #if 0 /* FIXME: CS4206 doesn't work, which is the only codec supporting float */
4270 if (streams & AC_SUPFMT_FLOAT32) {
4271 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
4276 if (streams == AC_SUPFMT_AC3) {
4277 /* should be exclusive */
4278 /* temporary hack: we have still no proper support
4279 * for the direct AC3 stream...
4281 formats |= SNDRV_PCM_FMTBIT_U8;
4285 snd_printk(KERN_ERR "hda_codec: formats == 0 "
4286 "(nid=0x%x, val=0x%x, ovrd=%i, "
4289 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0,
4294 *formatsp = formats;
4301 EXPORT_SYMBOL_HDA(snd_hda_query_supported_pcm);
4304 * snd_hda_is_supported_format - Check the validity of the format
4305 * @codec: HD-audio codec
4306 * @nid: NID to check
4307 * @format: the HD-audio format value to check
4309 * Check whether the given node supports the format value.
4311 * Returns 1 if supported, 0 if not.
4313 int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
4314 unsigned int format)
4317 unsigned int val = 0, rate, stream;
4319 val = query_pcm_param(codec, nid);
4323 rate = format & 0xff00;
4324 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
4325 if (rate_bits[i].hda_fmt == rate) {
4330 if (i >= AC_PAR_PCM_RATE_BITS)
4333 stream = query_stream_param(codec, nid);
4337 if (stream & AC_SUPFMT_PCM) {
4338 switch (format & 0xf0) {
4340 if (!(val & AC_SUPPCM_BITS_8))
4344 if (!(val & AC_SUPPCM_BITS_16))
4348 if (!(val & AC_SUPPCM_BITS_20))
4352 if (!(val & AC_SUPPCM_BITS_24))
4356 if (!(val & AC_SUPPCM_BITS_32))
4363 /* FIXME: check for float32 and AC3? */
4368 EXPORT_SYMBOL_HDA(snd_hda_is_supported_format);
4373 static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
4374 struct hda_codec *codec,
4375 struct snd_pcm_substream *substream)
4380 static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
4381 struct hda_codec *codec,
4382 unsigned int stream_tag,
4383 unsigned int format,
4384 struct snd_pcm_substream *substream)
4386 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4390 static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
4391 struct hda_codec *codec,
4392 struct snd_pcm_substream *substream)
4394 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
4398 static int set_pcm_default_values(struct hda_codec *codec,
4399 struct hda_pcm_stream *info)
4403 /* query support PCM information from the given NID */
4404 if (info->nid && (!info->rates || !info->formats)) {
4405 err = snd_hda_query_supported_pcm(codec, info->nid,
4406 info->rates ? NULL : &info->rates,
4407 info->formats ? NULL : &info->formats,
4408 info->maxbps ? NULL : &info->maxbps);
4412 if (info->ops.open == NULL)
4413 info->ops.open = hda_pcm_default_open_close;
4414 if (info->ops.close == NULL)
4415 info->ops.close = hda_pcm_default_open_close;
4416 if (info->ops.prepare == NULL) {
4417 if (snd_BUG_ON(!info->nid))
4419 info->ops.prepare = hda_pcm_default_prepare;
4421 if (info->ops.cleanup == NULL) {
4422 if (snd_BUG_ON(!info->nid))
4424 info->ops.cleanup = hda_pcm_default_cleanup;
4430 * codec prepare/cleanup entries
4432 int snd_hda_codec_prepare(struct hda_codec *codec,
4433 struct hda_pcm_stream *hinfo,
4434 unsigned int stream,
4435 unsigned int format,
4436 struct snd_pcm_substream *substream)
4439 mutex_lock(&codec->bus->prepare_mutex);
4440 ret = hinfo->ops.prepare(hinfo, codec, stream, format, substream);
4442 purify_inactive_streams(codec);
4443 mutex_unlock(&codec->bus->prepare_mutex);
4446 EXPORT_SYMBOL_HDA(snd_hda_codec_prepare);
4448 void snd_hda_codec_cleanup(struct hda_codec *codec,
4449 struct hda_pcm_stream *hinfo,
4450 struct snd_pcm_substream *substream)
4452 mutex_lock(&codec->bus->prepare_mutex);
4453 hinfo->ops.cleanup(hinfo, codec, substream);
4454 mutex_unlock(&codec->bus->prepare_mutex);
4456 EXPORT_SYMBOL_HDA(snd_hda_codec_cleanup);
4459 const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = {
4460 "Audio", "SPDIF", "HDMI", "Modem"
4464 * get the empty PCM device number to assign
4466 * note the max device number is limited by HDA_MAX_PCMS, currently 10
4468 static int get_empty_pcm_device(struct hda_bus *bus, int type)
4470 /* audio device indices; not linear to keep compatibility */
4471 static int audio_idx[HDA_PCM_NTYPES][5] = {
4472 [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 },
4473 [HDA_PCM_TYPE_SPDIF] = { 1, -1 },
4474 [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 },
4475 [HDA_PCM_TYPE_MODEM] = { 6, -1 },
4479 if (type >= HDA_PCM_NTYPES) {
4480 snd_printk(KERN_WARNING "Invalid PCM type %d\n", type);
4484 for (i = 0; audio_idx[type][i] >= 0 ; i++)
4485 if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits))
4486 return audio_idx[type][i];
4488 /* non-fixed slots starting from 10 */
4489 for (i = 10; i < 32; i++) {
4490 if (!test_and_set_bit(i, bus->pcm_dev_bits))
4494 snd_printk(KERN_WARNING "Too many %s devices\n",
4495 snd_hda_pcm_type_name[type]);
4500 * attach a new PCM stream
4502 static int snd_hda_attach_pcm(struct hda_codec *codec, struct hda_pcm *pcm)
4504 struct hda_bus *bus = codec->bus;
4505 struct hda_pcm_stream *info;
4508 if (snd_BUG_ON(!pcm->name))
4510 for (stream = 0; stream < 2; stream++) {
4511 info = &pcm->stream[stream];
4512 if (info->substreams) {
4513 err = set_pcm_default_values(codec, info);
4518 return bus->ops.attach_pcm(bus, codec, pcm);
4521 /* assign all PCMs of the given codec */
4522 int snd_hda_codec_build_pcms(struct hda_codec *codec)
4527 if (!codec->num_pcms) {
4528 if (!codec->patch_ops.build_pcms)
4530 err = codec->patch_ops.build_pcms(codec);
4532 printk(KERN_ERR "hda_codec: cannot build PCMs"
4533 "for #%d (error %d)\n", codec->addr, err);
4534 err = snd_hda_codec_reset(codec);
4537 "hda_codec: cannot revert codec\n");
4542 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
4543 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
4546 if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
4547 continue; /* no substreams assigned */
4550 dev = get_empty_pcm_device(codec->bus, cpcm->pcm_type);
4552 continue; /* no fatal error */
4554 err = snd_hda_attach_pcm(codec, cpcm);
4556 printk(KERN_ERR "hda_codec: cannot attach "
4557 "PCM stream %d for codec #%d\n",
4559 continue; /* no fatal error */
4567 * snd_hda_build_pcms - build PCM information
4570 * Create PCM information for each codec included in the bus.
4572 * The build_pcms codec patch is requested to set up codec->num_pcms and
4573 * codec->pcm_info properly. The array is referred by the top-level driver
4574 * to create its PCM instances.
4575 * The allocated codec->pcm_info should be released in codec->patch_ops.free
4578 * At least, substreams, channels_min and channels_max must be filled for
4579 * each stream. substreams = 0 indicates that the stream doesn't exist.
4580 * When rates and/or formats are zero, the supported values are queried
4581 * from the given nid. The nid is used also by the default ops.prepare
4582 * and ops.cleanup callbacks.
4584 * The driver needs to call ops.open in its open callback. Similarly,
4585 * ops.close is supposed to be called in the close callback.
4586 * ops.prepare should be called in the prepare or hw_params callback
4587 * with the proper parameters for set up.
4588 * ops.cleanup should be called in hw_free for clean up of streams.
4590 * This function returns 0 if successful, or a negative error code.
4592 int snd_hda_build_pcms(struct hda_bus *bus)
4594 struct hda_codec *codec;
4596 list_for_each_entry(codec, &bus->codec_list, list) {
4597 int err = snd_hda_codec_build_pcms(codec);
4603 EXPORT_SYMBOL_HDA(snd_hda_build_pcms);
4606 * snd_hda_check_board_config - compare the current codec with the config table
4607 * @codec: the HDA codec
4608 * @num_configs: number of config enums
4609 * @models: array of model name strings
4610 * @tbl: configuration table, terminated by null entries
4612 * Compares the modelname or PCI subsystem id of the current codec with the
4613 * given configuration table. If a matching entry is found, returns its
4614 * config value (supposed to be 0 or positive).
4616 * If no entries are matching, the function returns a negative value.
4618 int snd_hda_check_board_config(struct hda_codec *codec,
4619 int num_configs, const char * const *models,
4620 const struct snd_pci_quirk *tbl)
4622 if (codec->modelname && models) {
4624 for (i = 0; i < num_configs; i++) {
4626 !strcmp(codec->modelname, models[i])) {
4627 snd_printd(KERN_INFO "hda_codec: model '%s' is "
4628 "selected\n", models[i]);
4634 if (!codec->bus->pci || !tbl)
4637 tbl = snd_pci_quirk_lookup(codec->bus->pci, tbl);
4640 if (tbl->value >= 0 && tbl->value < num_configs) {
4641 #ifdef CONFIG_SND_DEBUG_VERBOSE
4643 const char *model = NULL;
4645 model = models[tbl->value];
4647 sprintf(tmp, "#%d", tbl->value);
4650 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
4651 "for config %x:%x (%s)\n",
4652 model, tbl->subvendor, tbl->subdevice,
4653 (tbl->name ? tbl->name : "Unknown device"));
4659 EXPORT_SYMBOL_HDA(snd_hda_check_board_config);
4662 * snd_hda_check_board_codec_sid_config - compare the current codec
4663 subsystem ID with the
4666 This is important for Gateway notebooks with SB450 HDA Audio
4667 where the vendor ID of the PCI device is:
4668 ATI Technologies Inc SB450 HDA Audio [1002:437b]
4669 and the vendor/subvendor are found only at the codec.
4671 * @codec: the HDA codec
4672 * @num_configs: number of config enums
4673 * @models: array of model name strings
4674 * @tbl: configuration table, terminated by null entries
4676 * Compares the modelname or PCI subsystem id of the current codec with the
4677 * given configuration table. If a matching entry is found, returns its
4678 * config value (supposed to be 0 or positive).
4680 * If no entries are matching, the function returns a negative value.
4682 int snd_hda_check_board_codec_sid_config(struct hda_codec *codec,
4683 int num_configs, const char * const *models,
4684 const struct snd_pci_quirk *tbl)
4686 const struct snd_pci_quirk *q;
4688 /* Search for codec ID */
4689 for (q = tbl; q->subvendor; q++) {
4690 unsigned int mask = 0xffff0000 | q->subdevice_mask;
4691 unsigned int id = (q->subdevice | (q->subvendor << 16)) & mask;
4692 if ((codec->subsystem_id & mask) == id)
4701 if (tbl->value >= 0 && tbl->value < num_configs) {
4702 #ifdef CONFIG_SND_DEBUG_VERBOSE
4704 const char *model = NULL;
4706 model = models[tbl->value];
4708 sprintf(tmp, "#%d", tbl->value);
4711 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
4712 "for config %x:%x (%s)\n",
4713 model, tbl->subvendor, tbl->subdevice,
4714 (tbl->name ? tbl->name : "Unknown device"));
4720 EXPORT_SYMBOL_HDA(snd_hda_check_board_codec_sid_config);
4723 * snd_hda_add_new_ctls - create controls from the array
4724 * @codec: the HDA codec
4725 * @knew: the array of struct snd_kcontrol_new
4727 * This helper function creates and add new controls in the given array.
4728 * The array must be terminated with an empty entry as terminator.
4730 * Returns 0 if successful, or a negative error code.
4732 int snd_hda_add_new_ctls(struct hda_codec *codec,
4733 const struct snd_kcontrol_new *knew)
4737 for (; knew->name; knew++) {
4738 struct snd_kcontrol *kctl;
4739 int addr = 0, idx = 0;
4740 if (knew->iface == -1) /* skip this codec private value */
4743 kctl = snd_ctl_new1(knew, codec);
4747 kctl->id.device = addr;
4749 kctl->id.index = idx;
4750 err = snd_hda_ctl_add(codec, 0, kctl);
4753 /* try first with another device index corresponding to
4754 * the codec addr; if it still fails (or it's the
4755 * primary codec), then try another control index
4757 if (!addr && codec->addr)
4759 else if (!idx && !knew->index) {
4760 idx = find_empty_mixer_ctl_idx(codec,
4770 EXPORT_SYMBOL_HDA(snd_hda_add_new_ctls);
4773 static void hda_power_work(struct work_struct *work)
4775 struct hda_codec *codec =
4776 container_of(work, struct hda_codec, power_work.work);
4777 struct hda_bus *bus = codec->bus;
4780 spin_lock(&codec->power_lock);
4781 if (codec->power_transition > 0) { /* during power-up sequence? */
4782 spin_unlock(&codec->power_lock);
4785 if (!codec->power_on || codec->power_count) {
4786 codec->power_transition = 0;
4787 spin_unlock(&codec->power_lock);
4790 spin_unlock(&codec->power_lock);
4792 state = hda_call_codec_suspend(codec, true);
4793 if (!codec->pm_down_notified &&
4794 !bus->power_keep_link_on && (state & AC_PWRST_CLK_STOP_OK)) {
4795 codec->pm_down_notified = 1;
4796 hda_call_pm_notify(bus, false);
4800 static void hda_keep_power_on(struct hda_codec *codec)
4802 spin_lock(&codec->power_lock);
4803 codec->power_count++;
4804 codec->power_on = 1;
4805 codec->power_jiffies = jiffies;
4806 spin_unlock(&codec->power_lock);
4809 /* update the power on/off account with the current jiffies */
4810 void snd_hda_update_power_acct(struct hda_codec *codec)
4812 unsigned long delta = jiffies - codec->power_jiffies;
4813 if (codec->power_on)
4814 codec->power_on_acct += delta;
4816 codec->power_off_acct += delta;
4817 codec->power_jiffies += delta;
4820 /* Transition to powered up, if wait_power_down then wait for a pending
4821 * transition to D3 to complete. A pending D3 transition is indicated
4822 * with power_transition == -1. */
4823 /* call this with codec->power_lock held! */
4824 static void __snd_hda_power_up(struct hda_codec *codec, bool wait_power_down)
4826 struct hda_bus *bus = codec->bus;
4828 /* Return if power_on or transitioning to power_on, unless currently
4830 if ((codec->power_on || codec->power_transition > 0) &&
4831 !(wait_power_down && codec->power_transition < 0))
4833 spin_unlock(&codec->power_lock);
4835 cancel_delayed_work_sync(&codec->power_work);
4837 spin_lock(&codec->power_lock);
4838 /* If the power down delayed work was cancelled above before starting,
4839 * then there is no need to go through power up here.
4841 if (codec->power_on) {
4842 if (codec->power_transition < 0)
4843 codec->power_transition = 0;
4847 trace_hda_power_up(codec);
4848 snd_hda_update_power_acct(codec);
4849 codec->power_on = 1;
4850 codec->power_jiffies = jiffies;
4851 codec->power_transition = 1; /* avoid reentrance */
4852 spin_unlock(&codec->power_lock);
4854 if (codec->pm_down_notified) {
4855 codec->pm_down_notified = 0;
4856 hda_call_pm_notify(bus, true);
4859 hda_call_codec_resume(codec);
4861 spin_lock(&codec->power_lock);
4862 codec->power_transition = 0;
4865 #define power_save(codec) \
4866 ((codec)->bus->power_save ? *(codec)->bus->power_save : 0)
4868 /* Transition to powered down */
4869 static void __snd_hda_power_down(struct hda_codec *codec)
4871 if (!codec->power_on || codec->power_count || codec->power_transition)
4874 if (power_save(codec)) {
4875 codec->power_transition = -1; /* avoid reentrance */
4876 queue_delayed_work(codec->bus->workq, &codec->power_work,
4877 msecs_to_jiffies(power_save(codec) * 1000));
4882 * snd_hda_power_save - Power-up/down/sync the codec
4883 * @codec: HD-audio codec
4884 * @delta: the counter delta to change
4886 * Change the power-up counter via @delta, and power up or down the hardware
4887 * appropriately. For the power-down, queue to the delayed action.
4888 * Passing zero to @delta means to synchronize the power state.
4890 void snd_hda_power_save(struct hda_codec *codec, int delta, bool d3wait)
4892 spin_lock(&codec->power_lock);
4893 codec->power_count += delta;
4894 trace_hda_power_count(codec);
4896 __snd_hda_power_up(codec, d3wait);
4898 __snd_hda_power_down(codec);
4899 spin_unlock(&codec->power_lock);
4901 EXPORT_SYMBOL_HDA(snd_hda_power_save);
4904 * snd_hda_check_amp_list_power - Check the amp list and update the power
4905 * @codec: HD-audio codec
4906 * @check: the object containing an AMP list and the status
4907 * @nid: NID to check / update
4909 * Check whether the given NID is in the amp list. If it's in the list,
4910 * check the current AMP status, and update the the power-status according
4911 * to the mute status.
4913 * This function is supposed to be set or called from the check_power_status
4916 int snd_hda_check_amp_list_power(struct hda_codec *codec,
4917 struct hda_loopback_check *check,
4920 const struct hda_amp_list *p;
4923 if (!check->amplist)
4925 for (p = check->amplist; p->nid; p++) {
4930 return 0; /* nothing changed */
4932 for (p = check->amplist; p->nid; p++) {
4933 for (ch = 0; ch < 2; ch++) {
4934 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
4936 if (!(v & HDA_AMP_MUTE) && v > 0) {
4937 if (!check->power_on) {
4938 check->power_on = 1;
4939 snd_hda_power_up(codec);
4945 if (check->power_on) {
4946 check->power_on = 0;
4947 snd_hda_power_down(codec);
4951 EXPORT_SYMBOL_HDA(snd_hda_check_amp_list_power);
4955 * Channel mode helper
4959 * snd_hda_ch_mode_info - Info callback helper for the channel mode enum
4961 int snd_hda_ch_mode_info(struct hda_codec *codec,
4962 struct snd_ctl_elem_info *uinfo,
4963 const struct hda_channel_mode *chmode,
4966 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4968 uinfo->value.enumerated.items = num_chmodes;
4969 if (uinfo->value.enumerated.item >= num_chmodes)
4970 uinfo->value.enumerated.item = num_chmodes - 1;
4971 sprintf(uinfo->value.enumerated.name, "%dch",
4972 chmode[uinfo->value.enumerated.item].channels);
4975 EXPORT_SYMBOL_HDA(snd_hda_ch_mode_info);
4978 * snd_hda_ch_mode_get - Get callback helper for the channel mode enum
4980 int snd_hda_ch_mode_get(struct hda_codec *codec,
4981 struct snd_ctl_elem_value *ucontrol,
4982 const struct hda_channel_mode *chmode,
4988 for (i = 0; i < num_chmodes; i++) {
4989 if (max_channels == chmode[i].channels) {
4990 ucontrol->value.enumerated.item[0] = i;
4996 EXPORT_SYMBOL_HDA(snd_hda_ch_mode_get);
4999 * snd_hda_ch_mode_put - Put callback helper for the channel mode enum
5001 int snd_hda_ch_mode_put(struct hda_codec *codec,
5002 struct snd_ctl_elem_value *ucontrol,
5003 const struct hda_channel_mode *chmode,
5009 mode = ucontrol->value.enumerated.item[0];
5010 if (mode >= num_chmodes)
5012 if (*max_channelsp == chmode[mode].channels)
5014 /* change the current channel setting */
5015 *max_channelsp = chmode[mode].channels;
5016 if (chmode[mode].sequence)
5017 snd_hda_sequence_write_cache(codec, chmode[mode].sequence);
5020 EXPORT_SYMBOL_HDA(snd_hda_ch_mode_put);
5027 * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum
5029 int snd_hda_input_mux_info(const struct hda_input_mux *imux,
5030 struct snd_ctl_elem_info *uinfo)
5034 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
5036 uinfo->value.enumerated.items = imux->num_items;
5037 if (!imux->num_items)
5039 index = uinfo->value.enumerated.item;
5040 if (index >= imux->num_items)
5041 index = imux->num_items - 1;
5042 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
5045 EXPORT_SYMBOL_HDA(snd_hda_input_mux_info);
5048 * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum
5050 int snd_hda_input_mux_put(struct hda_codec *codec,
5051 const struct hda_input_mux *imux,
5052 struct snd_ctl_elem_value *ucontrol,
5054 unsigned int *cur_val)
5058 if (!imux->num_items)
5060 idx = ucontrol->value.enumerated.item[0];
5061 if (idx >= imux->num_items)
5062 idx = imux->num_items - 1;
5063 if (*cur_val == idx)
5065 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
5066 imux->items[idx].index);
5070 EXPORT_SYMBOL_HDA(snd_hda_input_mux_put);
5074 * process kcontrol info callback of a simple string enum array
5075 * when @num_items is 0 or @texts is NULL, assume a boolean enum array
5077 int snd_hda_enum_helper_info(struct snd_kcontrol *kcontrol,
5078 struct snd_ctl_elem_info *uinfo,
5079 int num_items, const char * const *texts)
5081 static const char * const texts_default[] = {
5082 "Disabled", "Enabled"
5085 if (!texts || !num_items) {
5087 texts = texts_default;
5090 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
5092 uinfo->value.enumerated.items = num_items;
5093 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
5094 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
5095 strcpy(uinfo->value.enumerated.name,
5096 texts[uinfo->value.enumerated.item]);
5099 EXPORT_SYMBOL_HDA(snd_hda_enum_helper_info);
5102 * Multi-channel / digital-out PCM helper functions
5105 /* setup SPDIF output stream */
5106 static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
5107 unsigned int stream_tag, unsigned int format)
5109 struct hda_spdif_out *spdif;
5110 unsigned int curr_fmt;
5113 spdif = snd_hda_spdif_out_of_nid(codec, nid);
5114 curr_fmt = snd_hda_codec_read(codec, nid, 0,
5115 AC_VERB_GET_STREAM_FORMAT, 0);
5116 reset = codec->spdif_status_reset &&
5117 (spdif->ctls & AC_DIG1_ENABLE) &&
5120 /* turn off SPDIF if needed; otherwise the IEC958 bits won't be
5123 set_dig_out_convert(codec, nid,
5124 spdif->ctls & ~AC_DIG1_ENABLE & 0xff,
5126 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
5127 if (codec->slave_dig_outs) {
5129 for (d = codec->slave_dig_outs; *d; d++)
5130 snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
5133 /* turn on again (if needed) */
5135 set_dig_out_convert(codec, nid,
5136 spdif->ctls & 0xff, -1);
5139 static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
5141 snd_hda_codec_cleanup_stream(codec, nid);
5142 if (codec->slave_dig_outs) {
5144 for (d = codec->slave_dig_outs; *d; d++)
5145 snd_hda_codec_cleanup_stream(codec, *d);
5150 * snd_hda_bus_reboot_notify - call the reboot notifier of each codec
5151 * @bus: HD-audio bus
5153 void snd_hda_bus_reboot_notify(struct hda_bus *bus)
5155 struct hda_codec *codec;
5159 list_for_each_entry(codec, &bus->codec_list, list) {
5160 if (hda_codec_is_power_on(codec) &&
5161 codec->patch_ops.reboot_notify)
5162 codec->patch_ops.reboot_notify(codec);
5165 EXPORT_SYMBOL_HDA(snd_hda_bus_reboot_notify);
5168 * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode
5170 int snd_hda_multi_out_dig_open(struct hda_codec *codec,
5171 struct hda_multi_out *mout)
5173 mutex_lock(&codec->spdif_mutex);
5174 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
5175 /* already opened as analog dup; reset it once */
5176 cleanup_dig_out_stream(codec, mout->dig_out_nid);
5177 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
5178 mutex_unlock(&codec->spdif_mutex);
5181 EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_open);
5184 * snd_hda_multi_out_dig_prepare - prepare the digital out stream
5186 int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
5187 struct hda_multi_out *mout,
5188 unsigned int stream_tag,
5189 unsigned int format,
5190 struct snd_pcm_substream *substream)
5192 mutex_lock(&codec->spdif_mutex);
5193 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
5194 mutex_unlock(&codec->spdif_mutex);
5197 EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_prepare);
5200 * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream
5202 int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec,
5203 struct hda_multi_out *mout)
5205 mutex_lock(&codec->spdif_mutex);
5206 cleanup_dig_out_stream(codec, mout->dig_out_nid);
5207 mutex_unlock(&codec->spdif_mutex);
5210 EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_cleanup);
5213 * snd_hda_multi_out_dig_close - release the digital out stream
5215 int snd_hda_multi_out_dig_close(struct hda_codec *codec,
5216 struct hda_multi_out *mout)
5218 mutex_lock(&codec->spdif_mutex);
5219 mout->dig_out_used = 0;
5220 mutex_unlock(&codec->spdif_mutex);
5223 EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_close);
5226 * snd_hda_multi_out_analog_open - open analog outputs
5228 * Open analog outputs and set up the hw-constraints.
5229 * If the digital outputs can be opened as slave, open the digital
5232 int snd_hda_multi_out_analog_open(struct hda_codec *codec,
5233 struct hda_multi_out *mout,
5234 struct snd_pcm_substream *substream,
5235 struct hda_pcm_stream *hinfo)
5237 struct snd_pcm_runtime *runtime = substream->runtime;
5238 runtime->hw.channels_max = mout->max_channels;
5239 if (mout->dig_out_nid) {
5240 if (!mout->analog_rates) {
5241 mout->analog_rates = hinfo->rates;
5242 mout->analog_formats = hinfo->formats;
5243 mout->analog_maxbps = hinfo->maxbps;
5245 runtime->hw.rates = mout->analog_rates;
5246 runtime->hw.formats = mout->analog_formats;
5247 hinfo->maxbps = mout->analog_maxbps;
5249 if (!mout->spdif_rates) {
5250 snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
5252 &mout->spdif_formats,
5253 &mout->spdif_maxbps);
5255 mutex_lock(&codec->spdif_mutex);
5256 if (mout->share_spdif) {
5257 if ((runtime->hw.rates & mout->spdif_rates) &&
5258 (runtime->hw.formats & mout->spdif_formats)) {
5259 runtime->hw.rates &= mout->spdif_rates;
5260 runtime->hw.formats &= mout->spdif_formats;
5261 if (mout->spdif_maxbps < hinfo->maxbps)
5262 hinfo->maxbps = mout->spdif_maxbps;
5264 mout->share_spdif = 0;
5265 /* FIXME: need notify? */
5268 mutex_unlock(&codec->spdif_mutex);
5270 return snd_pcm_hw_constraint_step(substream->runtime, 0,
5271 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
5273 EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_open);
5276 * snd_hda_multi_out_analog_prepare - Preapre the analog outputs.
5278 * Set up the i/o for analog out.
5279 * When the digital out is available, copy the front out to digital out, too.
5281 int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
5282 struct hda_multi_out *mout,
5283 unsigned int stream_tag,
5284 unsigned int format,
5285 struct snd_pcm_substream *substream)
5287 const hda_nid_t *nids = mout->dac_nids;
5288 int chs = substream->runtime->channels;
5289 struct hda_spdif_out *spdif;
5292 mutex_lock(&codec->spdif_mutex);
5293 spdif = snd_hda_spdif_out_of_nid(codec, mout->dig_out_nid);
5294 if (mout->dig_out_nid && mout->share_spdif &&
5295 mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
5297 snd_hda_is_supported_format(codec, mout->dig_out_nid,
5299 !(spdif->status & IEC958_AES0_NONAUDIO)) {
5300 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
5301 setup_dig_out_stream(codec, mout->dig_out_nid,
5302 stream_tag, format);
5304 mout->dig_out_used = 0;
5305 cleanup_dig_out_stream(codec, mout->dig_out_nid);
5308 mutex_unlock(&codec->spdif_mutex);
5311 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
5313 if (!mout->no_share_stream &&
5314 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
5315 /* headphone out will just decode front left/right (stereo) */
5316 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
5318 /* extra outputs copied from front */
5319 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
5320 if (!mout->no_share_stream && mout->hp_out_nid[i])
5321 snd_hda_codec_setup_stream(codec,
5322 mout->hp_out_nid[i],
5323 stream_tag, 0, format);
5324 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
5325 if (!mout->no_share_stream && mout->extra_out_nid[i])
5326 snd_hda_codec_setup_stream(codec,
5327 mout->extra_out_nid[i],
5328 stream_tag, 0, format);
5331 for (i = 1; i < mout->num_dacs; i++) {
5332 if (chs >= (i + 1) * 2) /* independent out */
5333 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
5335 else if (!mout->no_share_stream) /* copy front */
5336 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
5341 EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_prepare);
5344 * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out
5346 int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
5347 struct hda_multi_out *mout)
5349 const hda_nid_t *nids = mout->dac_nids;
5352 for (i = 0; i < mout->num_dacs; i++)
5353 snd_hda_codec_cleanup_stream(codec, nids[i]);
5355 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
5356 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
5357 if (mout->hp_out_nid[i])
5358 snd_hda_codec_cleanup_stream(codec,
5359 mout->hp_out_nid[i]);
5360 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
5361 if (mout->extra_out_nid[i])
5362 snd_hda_codec_cleanup_stream(codec,
5363 mout->extra_out_nid[i]);
5364 mutex_lock(&codec->spdif_mutex);
5365 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
5366 cleanup_dig_out_stream(codec, mout->dig_out_nid);
5367 mout->dig_out_used = 0;
5369 mutex_unlock(&codec->spdif_mutex);
5372 EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_cleanup);
5375 * snd_hda_get_default_vref - Get the default (mic) VREF pin bits
5377 * Guess the suitable VREF pin bits to be set as the pin-control value.
5378 * Note: the function doesn't set the AC_PINCTL_IN_EN bit.
5380 unsigned int snd_hda_get_default_vref(struct hda_codec *codec, hda_nid_t pin)
5382 unsigned int pincap;
5383 unsigned int oldval;
5384 oldval = snd_hda_codec_read(codec, pin, 0,
5385 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
5386 pincap = snd_hda_query_pin_caps(codec, pin);
5387 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
5388 /* Exception: if the default pin setup is vref50, we give it priority */
5389 if ((pincap & AC_PINCAP_VREF_80) && oldval != PIN_VREF50)
5390 return AC_PINCTL_VREF_80;
5391 else if (pincap & AC_PINCAP_VREF_50)
5392 return AC_PINCTL_VREF_50;
5393 else if (pincap & AC_PINCAP_VREF_100)
5394 return AC_PINCTL_VREF_100;
5395 else if (pincap & AC_PINCAP_VREF_GRD)
5396 return AC_PINCTL_VREF_GRD;
5397 return AC_PINCTL_VREF_HIZ;
5399 EXPORT_SYMBOL_HDA(snd_hda_get_default_vref);
5401 /* correct the pin ctl value for matching with the pin cap */
5402 unsigned int snd_hda_correct_pin_ctl(struct hda_codec *codec,
5403 hda_nid_t pin, unsigned int val)
5405 static unsigned int cap_lists[][2] = {
5406 { AC_PINCTL_VREF_100, AC_PINCAP_VREF_100 },
5407 { AC_PINCTL_VREF_80, AC_PINCAP_VREF_80 },
5408 { AC_PINCTL_VREF_50, AC_PINCAP_VREF_50 },
5409 { AC_PINCTL_VREF_GRD, AC_PINCAP_VREF_GRD },
5415 cap = snd_hda_query_pin_caps(codec, pin);
5417 return val; /* don't know what to do... */
5419 if (val & AC_PINCTL_OUT_EN) {
5420 if (!(cap & AC_PINCAP_OUT))
5421 val &= ~(AC_PINCTL_OUT_EN | AC_PINCTL_HP_EN);
5422 else if ((val & AC_PINCTL_HP_EN) && !(cap & AC_PINCAP_HP_DRV))
5423 val &= ~AC_PINCTL_HP_EN;
5426 if (val & AC_PINCTL_IN_EN) {
5427 if (!(cap & AC_PINCAP_IN))
5428 val &= ~(AC_PINCTL_IN_EN | AC_PINCTL_VREFEN);
5430 unsigned int vcap, vref;
5432 vcap = (cap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
5433 vref = val & AC_PINCTL_VREFEN;
5434 for (i = 0; i < ARRAY_SIZE(cap_lists); i++) {
5435 if (vref == cap_lists[i][0] &&
5436 !(vcap & cap_lists[i][1])) {
5437 if (i == ARRAY_SIZE(cap_lists) - 1)
5438 vref = AC_PINCTL_VREF_HIZ;
5440 vref = cap_lists[i + 1][0];
5443 val &= ~AC_PINCTL_VREFEN;
5450 EXPORT_SYMBOL_HDA(snd_hda_correct_pin_ctl);
5452 int _snd_hda_set_pin_ctl(struct hda_codec *codec, hda_nid_t pin,
5453 unsigned int val, bool cached)
5455 val = snd_hda_correct_pin_ctl(codec, pin, val);
5456 snd_hda_codec_set_pin_target(codec, pin, val);
5458 return snd_hda_codec_update_cache(codec, pin, 0,
5459 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
5461 return snd_hda_codec_write(codec, pin, 0,
5462 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
5464 EXPORT_SYMBOL_HDA(_snd_hda_set_pin_ctl);
5467 * snd_hda_add_imux_item - Add an item to input_mux
5469 * When the same label is used already in the existing items, the number
5470 * suffix is appended to the label. This label index number is stored
5471 * to type_idx when non-NULL pointer is given.
5473 int snd_hda_add_imux_item(struct hda_input_mux *imux, const char *label,
5474 int index, int *type_idx)
5476 int i, label_idx = 0;
5477 if (imux->num_items >= HDA_MAX_NUM_INPUTS) {
5478 snd_printd(KERN_ERR "hda_codec: Too many imux items!\n");
5481 for (i = 0; i < imux->num_items; i++) {
5482 if (!strncmp(label, imux->items[i].label, strlen(label)))
5486 *type_idx = label_idx;
5488 snprintf(imux->items[imux->num_items].label,
5489 sizeof(imux->items[imux->num_items].label),
5490 "%s %d", label, label_idx);
5492 strlcpy(imux->items[imux->num_items].label, label,
5493 sizeof(imux->items[imux->num_items].label));
5494 imux->items[imux->num_items].index = index;
5498 EXPORT_SYMBOL_HDA(snd_hda_add_imux_item);
5507 * snd_hda_suspend - suspend the codecs
5510 * Returns 0 if successful.
5512 int snd_hda_suspend(struct hda_bus *bus)
5514 struct hda_codec *codec;
5516 list_for_each_entry(codec, &bus->codec_list, list) {
5517 cancel_delayed_work_sync(&codec->jackpoll_work);
5518 if (hda_codec_is_power_on(codec))
5519 hda_call_codec_suspend(codec, false);
5523 EXPORT_SYMBOL_HDA(snd_hda_suspend);
5526 * snd_hda_resume - resume the codecs
5529 * Returns 0 if successful.
5531 int snd_hda_resume(struct hda_bus *bus)
5533 struct hda_codec *codec;
5535 list_for_each_entry(codec, &bus->codec_list, list) {
5536 hda_call_codec_resume(codec);
5540 EXPORT_SYMBOL_HDA(snd_hda_resume);
5541 #endif /* CONFIG_PM */
5548 * snd_array_new - get a new element from the given array
5549 * @array: the array object
5551 * Get a new element from the given array. If it exceeds the
5552 * pre-allocated array size, re-allocate the array.
5554 * Returns NULL if allocation failed.
5556 void *snd_array_new(struct snd_array *array)
5558 if (snd_BUG_ON(!array->elem_size))
5560 if (array->used >= array->alloced) {
5561 int num = array->alloced + array->alloc_align;
5562 int size = (num + 1) * array->elem_size;
5564 if (snd_BUG_ON(num >= 4096))
5566 nlist = krealloc(array->list, size, GFP_KERNEL | __GFP_ZERO);
5569 array->list = nlist;
5570 array->alloced = num;
5572 return snd_array_elem(array, array->used++);
5574 EXPORT_SYMBOL_HDA(snd_array_new);
5577 * snd_array_free - free the given array elements
5578 * @array: the array object
5580 void snd_array_free(struct snd_array *array)
5587 EXPORT_SYMBOL_HDA(snd_array_free);
5590 * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer
5591 * @pcm: PCM caps bits
5592 * @buf: the string buffer to write
5593 * @buflen: the max buffer length
5595 * used by hda_proc.c and hda_eld.c
5597 void snd_print_pcm_bits(int pcm, char *buf, int buflen)
5599 static unsigned int bits[] = { 8, 16, 20, 24, 32 };
5602 for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
5603 if (pcm & (AC_SUPPCM_BITS_8 << i))
5604 j += snprintf(buf + j, buflen - j, " %d", bits[i]);
5606 buf[j] = '\0'; /* necessary when j == 0 */
5608 EXPORT_SYMBOL_HDA(snd_print_pcm_bits);
5610 MODULE_DESCRIPTION("HDA codec core");
5611 MODULE_LICENSE("GPL");