2 * Universal Interface for Intel High Definition Audio Codec
4 * Copyright (c) 2004 Takashi Iwai <tiwai@suse.de>
7 * This driver is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This driver is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include <linux/init.h>
24 #include <linux/delay.h>
25 #include <linux/slab.h>
26 #include <linux/pci.h>
27 #include <linux/mutex.h>
28 #include <linux/module.h>
29 #include <sound/core.h>
30 #include "hda_codec.h"
31 #include <sound/asoundef.h>
32 #include <sound/tlv.h>
33 #include <sound/initval.h>
34 #include <sound/jack.h>
35 #include "hda_local.h"
38 #include <sound/hda_hwdep.h>
40 #define CREATE_TRACE_POINTS
41 #include "hda_trace.h"
44 * vendor / preset table
47 struct hda_vendor_id {
52 /* codec vendor labels */
53 static struct hda_vendor_id hda_vendor_ids[] = {
55 { 0x1013, "Cirrus Logic" },
56 { 0x1057, "Motorola" },
57 { 0x1095, "Silicon Image" },
59 { 0x10ec, "Realtek" },
60 { 0x1102, "Creative" },
64 { 0x11d4, "Analog Devices" },
65 { 0x13f6, "C-Media" },
66 { 0x14f1, "Conexant" },
67 { 0x17e8, "Chrontel" },
69 { 0x1aec, "Wolfson Microelectronics" },
70 { 0x434d, "C-Media" },
72 { 0x8384, "SigmaTel" },
76 static DEFINE_MUTEX(preset_mutex);
77 static LIST_HEAD(hda_preset_tables);
79 int snd_hda_add_codec_preset(struct hda_codec_preset_list *preset)
81 mutex_lock(&preset_mutex);
82 list_add_tail(&preset->list, &hda_preset_tables);
83 mutex_unlock(&preset_mutex);
86 EXPORT_SYMBOL_HDA(snd_hda_add_codec_preset);
88 int snd_hda_delete_codec_preset(struct hda_codec_preset_list *preset)
90 mutex_lock(&preset_mutex);
91 list_del(&preset->list);
92 mutex_unlock(&preset_mutex);
95 EXPORT_SYMBOL_HDA(snd_hda_delete_codec_preset);
98 #define codec_in_pm(codec) ((codec)->in_pm)
99 static void hda_power_work(struct work_struct *work);
100 static void hda_keep_power_on(struct hda_codec *codec);
101 #define hda_codec_is_power_on(codec) ((codec)->power_on)
102 static inline void hda_call_pm_notify(struct hda_bus *bus, bool power_up)
104 if (bus->ops.pm_notify)
105 bus->ops.pm_notify(bus, power_up);
108 #define codec_in_pm(codec) 0
109 static inline void hda_keep_power_on(struct hda_codec *codec) {}
110 #define hda_codec_is_power_on(codec) 1
111 #define hda_call_pm_notify(bus, state) {}
115 * snd_hda_get_jack_location - Give a location string of the jack
116 * @cfg: pin default config value
118 * Parse the pin default config value and returns the string of the
119 * jack location, e.g. "Rear", "Front", etc.
121 const char *snd_hda_get_jack_location(u32 cfg)
123 static char *bases[7] = {
124 "N/A", "Rear", "Front", "Left", "Right", "Top", "Bottom",
126 static unsigned char specials_idx[] = {
131 static char *specials[] = {
132 "Rear Panel", "Drive Bar",
133 "Riser", "HDMI", "ATAPI",
134 "Mobile-In", "Mobile-Out"
137 cfg = (cfg & AC_DEFCFG_LOCATION) >> AC_DEFCFG_LOCATION_SHIFT;
138 if ((cfg & 0x0f) < 7)
139 return bases[cfg & 0x0f];
140 for (i = 0; i < ARRAY_SIZE(specials_idx); i++) {
141 if (cfg == specials_idx[i])
146 EXPORT_SYMBOL_HDA(snd_hda_get_jack_location);
149 * snd_hda_get_jack_connectivity - Give a connectivity string of the jack
150 * @cfg: pin default config value
152 * Parse the pin default config value and returns the string of the
153 * jack connectivity, i.e. external or internal connection.
155 const char *snd_hda_get_jack_connectivity(u32 cfg)
157 static char *jack_locations[4] = { "Ext", "Int", "Sep", "Oth" };
159 return jack_locations[(cfg >> (AC_DEFCFG_LOCATION_SHIFT + 4)) & 3];
161 EXPORT_SYMBOL_HDA(snd_hda_get_jack_connectivity);
164 * snd_hda_get_jack_type - Give a type string of the jack
165 * @cfg: pin default config value
167 * Parse the pin default config value and returns the string of the
168 * jack type, i.e. the purpose of the jack, such as Line-Out or CD.
170 const char *snd_hda_get_jack_type(u32 cfg)
172 static char *jack_types[16] = {
173 "Line Out", "Speaker", "HP Out", "CD",
174 "SPDIF Out", "Digital Out", "Modem Line", "Modem Hand",
175 "Line In", "Aux", "Mic", "Telephony",
176 "SPDIF In", "Digitial In", "Reserved", "Other"
179 return jack_types[(cfg & AC_DEFCFG_DEVICE)
180 >> AC_DEFCFG_DEVICE_SHIFT];
182 EXPORT_SYMBOL_HDA(snd_hda_get_jack_type);
185 * Compose a 32bit command word to be sent to the HD-audio controller
187 static inline unsigned int
188 make_codec_cmd(struct hda_codec *codec, hda_nid_t nid, int direct,
189 unsigned int verb, unsigned int parm)
193 if ((codec->addr & ~0xf) || (direct & ~1) || (nid & ~0x7f) ||
194 (verb & ~0xfff) || (parm & ~0xffff)) {
195 printk(KERN_ERR "hda-codec: out of range cmd %x:%x:%x:%x:%x\n",
196 codec->addr, direct, nid, verb, parm);
200 val = (u32)codec->addr << 28;
201 val |= (u32)direct << 27;
202 val |= (u32)nid << 20;
209 * Send and receive a verb
211 static int codec_exec_verb(struct hda_codec *codec, unsigned int cmd,
214 struct hda_bus *bus = codec->bus;
223 snd_hda_power_up(codec);
224 mutex_lock(&bus->cmd_mutex);
226 trace_hda_send_cmd(codec, cmd);
227 err = bus->ops.command(bus, cmd);
230 /* process pending verbs */
231 bus->ops.get_response(bus, codec->addr);
234 *res = bus->ops.get_response(bus, codec->addr);
235 trace_hda_get_response(codec, *res);
237 mutex_unlock(&bus->cmd_mutex);
238 snd_hda_power_down(codec);
239 if (!codec_in_pm(codec) && res && *res == -1 && bus->rirb_error) {
240 if (bus->response_reset) {
241 snd_printd("hda_codec: resetting BUS due to "
242 "fatal communication error\n");
243 trace_hda_bus_reset(bus);
244 bus->ops.bus_reset(bus);
248 /* clear reset-flag when the communication gets recovered */
249 if (!err || codec_in_pm(codec))
250 bus->response_reset = 0;
255 * snd_hda_codec_read - send a command and get the response
256 * @codec: the HDA codec
257 * @nid: NID to send the command
258 * @direct: direct flag
259 * @verb: the verb to send
260 * @parm: the parameter for the verb
262 * Send a single command and read the corresponding response.
264 * Returns the obtained response value, or -1 for an error.
266 unsigned int snd_hda_codec_read(struct hda_codec *codec, hda_nid_t nid,
268 unsigned int verb, unsigned int parm)
270 unsigned cmd = make_codec_cmd(codec, nid, direct, verb, parm);
272 if (codec_exec_verb(codec, cmd, &res))
276 EXPORT_SYMBOL_HDA(snd_hda_codec_read);
279 * snd_hda_codec_write - send a single command without waiting for response
280 * @codec: the HDA codec
281 * @nid: NID to send the command
282 * @direct: direct flag
283 * @verb: the verb to send
284 * @parm: the parameter for the verb
286 * Send a single command without waiting for response.
288 * Returns 0 if successful, or a negative error code.
290 int snd_hda_codec_write(struct hda_codec *codec, hda_nid_t nid, int direct,
291 unsigned int verb, unsigned int parm)
293 unsigned int cmd = make_codec_cmd(codec, nid, direct, verb, parm);
295 return codec_exec_verb(codec, cmd,
296 codec->bus->sync_write ? &res : NULL);
298 EXPORT_SYMBOL_HDA(snd_hda_codec_write);
301 * snd_hda_sequence_write - sequence writes
302 * @codec: the HDA codec
303 * @seq: VERB array to send
305 * Send the commands sequentially from the given array.
306 * The array must be terminated with NID=0.
308 void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq)
310 for (; seq->nid; seq++)
311 snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param);
313 EXPORT_SYMBOL_HDA(snd_hda_sequence_write);
316 * snd_hda_get_sub_nodes - get the range of sub nodes
317 * @codec: the HDA codec
319 * @start_id: the pointer to store the start NID
321 * Parse the NID and store the start NID of its sub-nodes.
322 * Returns the number of sub-nodes.
324 int snd_hda_get_sub_nodes(struct hda_codec *codec, hda_nid_t nid,
329 parm = snd_hda_param_read(codec, nid, AC_PAR_NODE_COUNT);
332 *start_id = (parm >> 16) & 0x7fff;
333 return (int)(parm & 0x7fff);
335 EXPORT_SYMBOL_HDA(snd_hda_get_sub_nodes);
337 /* look up the cached results */
338 static hda_nid_t *lookup_conn_list(struct snd_array *array, hda_nid_t nid)
341 for (i = 0; i < array->used; ) {
342 hda_nid_t *p = snd_array_elem(array, i);
351 /* read the connection and add to the cache */
352 static int read_and_add_raw_conns(struct hda_codec *codec, hda_nid_t nid)
354 hda_nid_t list[HDA_MAX_CONNECTIONS];
357 len = snd_hda_get_raw_connections(codec, nid, list, ARRAY_SIZE(list));
360 return snd_hda_override_conn_list(codec, nid, len, list);
364 * snd_hda_get_connections - copy connection list
365 * @codec: the HDA codec
367 * @conn_list: connection list array; when NULL, checks only the size
368 * @max_conns: max. number of connections to store
370 * Parses the connection list of the given widget and stores the list
373 * Returns the number of connections, or a negative error code.
375 int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
376 hda_nid_t *conn_list, int max_conns)
378 struct snd_array *array = &codec->conn_lists;
384 mutex_lock(&codec->hash_mutex);
386 /* if the connection-list is already cached, read it */
387 p = lookup_conn_list(array, nid);
390 if (conn_list && len > max_conns) {
391 snd_printk(KERN_ERR "hda_codec: "
392 "Too many connections %d for NID 0x%x\n",
394 mutex_unlock(&codec->hash_mutex);
397 if (conn_list && len)
398 memcpy(conn_list, p + 2, len * sizeof(hda_nid_t));
400 mutex_unlock(&codec->hash_mutex);
403 if (snd_BUG_ON(added))
406 len = read_and_add_raw_conns(codec, nid);
412 EXPORT_SYMBOL_HDA(snd_hda_get_connections);
415 * snd_hda_get_raw_connections - copy connection list without cache
416 * @codec: the HDA codec
418 * @conn_list: connection list array
419 * @max_conns: max. number of connections to store
421 * Like snd_hda_get_connections(), copy the connection list but without
422 * checking through the connection-list cache.
423 * Currently called only from hda_proc.c, so not exported.
425 int snd_hda_get_raw_connections(struct hda_codec *codec, hda_nid_t nid,
426 hda_nid_t *conn_list, int max_conns)
429 int i, conn_len, conns;
430 unsigned int shift, num_elems, mask;
435 if (snd_BUG_ON(!conn_list || max_conns <= 0))
438 wcaps = get_wcaps(codec, nid);
439 if (!(wcaps & AC_WCAP_CONN_LIST) &&
440 get_wcaps_type(wcaps) != AC_WID_VOL_KNB)
443 parm = snd_hda_param_read(codec, nid, AC_PAR_CONNLIST_LEN);
444 if (parm & AC_CLIST_LONG) {
453 conn_len = parm & AC_CLIST_LENGTH;
454 mask = (1 << (shift-1)) - 1;
457 return 0; /* no connection */
460 /* single connection */
461 parm = snd_hda_codec_read(codec, nid, 0,
462 AC_VERB_GET_CONNECT_LIST, 0);
463 if (parm == -1 && codec->bus->rirb_error)
465 conn_list[0] = parm & mask;
469 /* multi connection */
472 for (i = 0; i < conn_len; i++) {
476 if (i % num_elems == 0) {
477 parm = snd_hda_codec_read(codec, nid, 0,
478 AC_VERB_GET_CONNECT_LIST, i);
479 if (parm == -1 && codec->bus->rirb_error)
482 range_val = !!(parm & (1 << (shift-1))); /* ranges */
484 if (val == 0 && null_count++) { /* no second chance */
485 snd_printk(KERN_WARNING "hda_codec: "
486 "invalid CONNECT_LIST verb %x[%i]:%x\n",
492 /* ranges between the previous and this one */
493 if (!prev_nid || prev_nid >= val) {
494 snd_printk(KERN_WARNING "hda_codec: "
495 "invalid dep_range_val %x:%x\n",
499 for (n = prev_nid + 1; n <= val; n++) {
500 if (conns >= max_conns) {
501 snd_printk(KERN_ERR "hda_codec: "
502 "Too many connections %d for NID 0x%x\n",
506 conn_list[conns++] = n;
509 if (conns >= max_conns) {
510 snd_printk(KERN_ERR "hda_codec: "
511 "Too many connections %d for NID 0x%x\n",
515 conn_list[conns++] = val;
522 static bool add_conn_list(struct snd_array *array, hda_nid_t nid)
524 hda_nid_t *p = snd_array_new(array);
532 * snd_hda_override_conn_list - add/modify the connection-list to cache
533 * @codec: the HDA codec
535 * @len: number of connection list entries
536 * @list: the list of connection entries
538 * Add or modify the given connection-list to the cache. If the corresponding
539 * cache already exists, invalidate it and append a new one.
541 * Returns zero or a negative error code.
543 int snd_hda_override_conn_list(struct hda_codec *codec, hda_nid_t nid, int len,
544 const hda_nid_t *list)
546 struct snd_array *array = &codec->conn_lists;
550 mutex_lock(&codec->hash_mutex);
551 p = lookup_conn_list(array, nid);
553 *p = -1; /* invalidate the old entry */
555 old_used = array->used;
556 if (!add_conn_list(array, nid) || !add_conn_list(array, len))
558 for (i = 0; i < len; i++)
559 if (!add_conn_list(array, list[i]))
561 mutex_unlock(&codec->hash_mutex);
565 array->used = old_used;
566 mutex_unlock(&codec->hash_mutex);
569 EXPORT_SYMBOL_HDA(snd_hda_override_conn_list);
572 * snd_hda_get_conn_index - get the connection index of the given NID
573 * @codec: the HDA codec
574 * @mux: NID containing the list
575 * @nid: NID to select
576 * @recursive: 1 when searching NID recursively, otherwise 0
578 * Parses the connection list of the widget @mux and checks whether the
579 * widget @nid is present. If it is, return the connection index.
580 * Otherwise it returns -1.
582 int snd_hda_get_conn_index(struct hda_codec *codec, hda_nid_t mux,
583 hda_nid_t nid, int recursive)
585 hda_nid_t conn[HDA_MAX_NUM_INPUTS];
588 nums = snd_hda_get_connections(codec, mux, conn, ARRAY_SIZE(conn));
589 for (i = 0; i < nums; i++)
594 if (recursive > 10) {
595 snd_printd("hda_codec: too deep connection for 0x%x\n", nid);
599 for (i = 0; i < nums; i++) {
600 unsigned int type = get_wcaps_type(get_wcaps(codec, conn[i]));
601 if (type == AC_WID_PIN || type == AC_WID_AUD_OUT)
603 if (snd_hda_get_conn_index(codec, conn[i], nid, recursive) >= 0)
608 EXPORT_SYMBOL_HDA(snd_hda_get_conn_index);
611 * snd_hda_queue_unsol_event - add an unsolicited event to queue
613 * @res: unsolicited event (lower 32bit of RIRB entry)
614 * @res_ex: codec addr and flags (upper 32bit or RIRB entry)
616 * Adds the given event to the queue. The events are processed in
617 * the workqueue asynchronously. Call this function in the interrupt
618 * hanlder when RIRB receives an unsolicited event.
620 * Returns 0 if successful, or a negative error code.
622 int snd_hda_queue_unsol_event(struct hda_bus *bus, u32 res, u32 res_ex)
624 struct hda_bus_unsolicited *unsol;
627 trace_hda_unsol_event(bus, res, res_ex);
632 wp = (unsol->wp + 1) % HDA_UNSOL_QUEUE_SIZE;
636 unsol->queue[wp] = res;
637 unsol->queue[wp + 1] = res_ex;
639 queue_work(bus->workq, &unsol->work);
643 EXPORT_SYMBOL_HDA(snd_hda_queue_unsol_event);
646 * process queued unsolicited events
648 static void process_unsol_events(struct work_struct *work)
650 struct hda_bus_unsolicited *unsol =
651 container_of(work, struct hda_bus_unsolicited, work);
652 struct hda_bus *bus = unsol->bus;
653 struct hda_codec *codec;
654 unsigned int rp, caddr, res;
656 while (unsol->rp != unsol->wp) {
657 rp = (unsol->rp + 1) % HDA_UNSOL_QUEUE_SIZE;
660 res = unsol->queue[rp];
661 caddr = unsol->queue[rp + 1];
662 if (!(caddr & (1 << 4))) /* no unsolicited event? */
664 codec = bus->caddr_tbl[caddr & 0x0f];
665 if (codec && codec->patch_ops.unsol_event)
666 codec->patch_ops.unsol_event(codec, res);
671 * initialize unsolicited queue
673 static int init_unsol_queue(struct hda_bus *bus)
675 struct hda_bus_unsolicited *unsol;
677 if (bus->unsol) /* already initialized */
680 unsol = kzalloc(sizeof(*unsol), GFP_KERNEL);
682 snd_printk(KERN_ERR "hda_codec: "
683 "can't allocate unsolicited queue\n");
686 INIT_WORK(&unsol->work, process_unsol_events);
695 static void snd_hda_codec_free(struct hda_codec *codec);
697 static int snd_hda_bus_free(struct hda_bus *bus)
699 struct hda_codec *codec, *n;
704 flush_workqueue(bus->workq);
707 list_for_each_entry_safe(codec, n, &bus->codec_list, list) {
708 snd_hda_codec_free(codec);
710 if (bus->ops.private_free)
711 bus->ops.private_free(bus);
713 destroy_workqueue(bus->workq);
718 static int snd_hda_bus_dev_free(struct snd_device *device)
720 struct hda_bus *bus = device->device_data;
722 return snd_hda_bus_free(bus);
725 #ifdef CONFIG_SND_HDA_HWDEP
726 static int snd_hda_bus_dev_register(struct snd_device *device)
728 struct hda_bus *bus = device->device_data;
729 struct hda_codec *codec;
730 list_for_each_entry(codec, &bus->codec_list, list) {
731 snd_hda_hwdep_add_sysfs(codec);
732 snd_hda_hwdep_add_power_sysfs(codec);
737 #define snd_hda_bus_dev_register NULL
741 * snd_hda_bus_new - create a HDA bus
742 * @card: the card entry
743 * @temp: the template for hda_bus information
744 * @busp: the pointer to store the created bus instance
746 * Returns 0 if successful, or a negative error code.
748 int snd_hda_bus_new(struct snd_card *card,
749 const struct hda_bus_template *temp,
750 struct hda_bus **busp)
754 static struct snd_device_ops dev_ops = {
755 .dev_register = snd_hda_bus_dev_register,
756 .dev_free = snd_hda_bus_dev_free,
759 if (snd_BUG_ON(!temp))
761 if (snd_BUG_ON(!temp->ops.command || !temp->ops.get_response))
767 bus = kzalloc(sizeof(*bus), GFP_KERNEL);
769 snd_printk(KERN_ERR "can't allocate struct hda_bus\n");
774 bus->private_data = temp->private_data;
775 bus->pci = temp->pci;
776 bus->modelname = temp->modelname;
777 bus->power_save = temp->power_save;
778 bus->ops = temp->ops;
780 mutex_init(&bus->cmd_mutex);
781 mutex_init(&bus->prepare_mutex);
782 INIT_LIST_HEAD(&bus->codec_list);
784 snprintf(bus->workq_name, sizeof(bus->workq_name),
785 "hd-audio%d", card->number);
786 bus->workq = create_singlethread_workqueue(bus->workq_name);
788 snd_printk(KERN_ERR "cannot create workqueue %s\n",
794 err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops);
796 snd_hda_bus_free(bus);
803 EXPORT_SYMBOL_HDA(snd_hda_bus_new);
805 #ifdef CONFIG_SND_HDA_GENERIC
806 #define is_generic_config(codec) \
807 (codec->modelname && !strcmp(codec->modelname, "generic"))
809 #define is_generic_config(codec) 0
813 #define HDA_MODREQ_MAX_COUNT 2 /* two request_modules()'s */
815 #define HDA_MODREQ_MAX_COUNT 0 /* all presets are statically linked */
819 * find a matching codec preset
821 static const struct hda_codec_preset *
822 find_codec_preset(struct hda_codec *codec)
824 struct hda_codec_preset_list *tbl;
825 const struct hda_codec_preset *preset;
826 unsigned int mod_requested = 0;
828 if (is_generic_config(codec))
829 return NULL; /* use the generic parser */
832 mutex_lock(&preset_mutex);
833 list_for_each_entry(tbl, &hda_preset_tables, list) {
834 if (!try_module_get(tbl->owner)) {
835 snd_printk(KERN_ERR "hda_codec: cannot module_get\n");
838 for (preset = tbl->preset; preset->id; preset++) {
839 u32 mask = preset->mask;
840 if (preset->afg && preset->afg != codec->afg)
842 if (preset->mfg && preset->mfg != codec->mfg)
846 if (preset->id == (codec->vendor_id & mask) &&
848 preset->rev == codec->revision_id)) {
849 mutex_unlock(&preset_mutex);
850 codec->owner = tbl->owner;
854 module_put(tbl->owner);
856 mutex_unlock(&preset_mutex);
858 if (mod_requested < HDA_MODREQ_MAX_COUNT) {
861 snprintf(name, sizeof(name), "snd-hda-codec-id:%08x",
864 snprintf(name, sizeof(name), "snd-hda-codec-id:%04x*",
865 (codec->vendor_id >> 16) & 0xffff);
866 request_module(name);
874 * get_codec_name - store the codec name
876 static int get_codec_name(struct hda_codec *codec)
878 const struct hda_vendor_id *c;
879 const char *vendor = NULL;
880 u16 vendor_id = codec->vendor_id >> 16;
883 if (codec->vendor_name)
886 for (c = hda_vendor_ids; c->id; c++) {
887 if (c->id == vendor_id) {
893 sprintf(tmp, "Generic %04x", vendor_id);
896 codec->vendor_name = kstrdup(vendor, GFP_KERNEL);
897 if (!codec->vendor_name)
901 if (codec->chip_name)
904 if (codec->preset && codec->preset->name)
905 codec->chip_name = kstrdup(codec->preset->name, GFP_KERNEL);
907 sprintf(tmp, "ID %x", codec->vendor_id & 0xffff);
908 codec->chip_name = kstrdup(tmp, GFP_KERNEL);
910 if (!codec->chip_name)
916 * look for an AFG and MFG nodes
918 static void setup_fg_nodes(struct hda_codec *codec)
920 int i, total_nodes, function_id;
923 total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid);
924 for (i = 0; i < total_nodes; i++, nid++) {
925 function_id = snd_hda_param_read(codec, nid,
926 AC_PAR_FUNCTION_TYPE);
927 switch (function_id & 0xff) {
928 case AC_GRP_AUDIO_FUNCTION:
930 codec->afg_function_id = function_id & 0xff;
931 codec->afg_unsol = (function_id >> 8) & 1;
933 case AC_GRP_MODEM_FUNCTION:
935 codec->mfg_function_id = function_id & 0xff;
936 codec->mfg_unsol = (function_id >> 8) & 1;
945 * read widget caps for each widget and store in cache
947 static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
952 codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node,
954 codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL);
957 nid = codec->start_nid;
958 for (i = 0; i < codec->num_nodes; i++, nid++)
959 codec->wcaps[i] = snd_hda_param_read(codec, nid,
960 AC_PAR_AUDIO_WIDGET_CAP);
964 /* read all pin default configurations and save codec->init_pins */
965 static int read_pin_defaults(struct hda_codec *codec)
968 hda_nid_t nid = codec->start_nid;
970 for (i = 0; i < codec->num_nodes; i++, nid++) {
971 struct hda_pincfg *pin;
972 unsigned int wcaps = get_wcaps(codec, nid);
973 unsigned int wid_type = get_wcaps_type(wcaps);
974 if (wid_type != AC_WID_PIN)
976 pin = snd_array_new(&codec->init_pins);
980 pin->cfg = snd_hda_codec_read(codec, nid, 0,
981 AC_VERB_GET_CONFIG_DEFAULT, 0);
982 pin->ctrl = snd_hda_codec_read(codec, nid, 0,
983 AC_VERB_GET_PIN_WIDGET_CONTROL,
989 /* look up the given pin config list and return the item matching with NID */
990 static struct hda_pincfg *look_up_pincfg(struct hda_codec *codec,
991 struct snd_array *array,
995 for (i = 0; i < array->used; i++) {
996 struct hda_pincfg *pin = snd_array_elem(array, i);
1003 /* set the current pin config value for the given NID.
1004 * the value is cached, and read via snd_hda_codec_get_pincfg()
1006 int snd_hda_add_pincfg(struct hda_codec *codec, struct snd_array *list,
1007 hda_nid_t nid, unsigned int cfg)
1009 struct hda_pincfg *pin;
1011 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN)
1014 pin = look_up_pincfg(codec, list, nid);
1016 pin = snd_array_new(list);
1026 * snd_hda_codec_set_pincfg - Override a pin default configuration
1027 * @codec: the HDA codec
1028 * @nid: NID to set the pin config
1029 * @cfg: the pin default config value
1031 * Override a pin default configuration value in the cache.
1032 * This value can be read by snd_hda_codec_get_pincfg() in a higher
1033 * priority than the real hardware value.
1035 int snd_hda_codec_set_pincfg(struct hda_codec *codec,
1036 hda_nid_t nid, unsigned int cfg)
1038 return snd_hda_add_pincfg(codec, &codec->driver_pins, nid, cfg);
1040 EXPORT_SYMBOL_HDA(snd_hda_codec_set_pincfg);
1043 * snd_hda_codec_get_pincfg - Obtain a pin-default configuration
1044 * @codec: the HDA codec
1045 * @nid: NID to get the pin config
1047 * Get the current pin config value of the given pin NID.
1048 * If the pincfg value is cached or overridden via sysfs or driver,
1049 * returns the cached value.
1051 unsigned int snd_hda_codec_get_pincfg(struct hda_codec *codec, hda_nid_t nid)
1053 struct hda_pincfg *pin;
1055 #ifdef CONFIG_SND_HDA_HWDEP
1056 pin = look_up_pincfg(codec, &codec->user_pins, nid);
1060 pin = look_up_pincfg(codec, &codec->driver_pins, nid);
1063 pin = look_up_pincfg(codec, &codec->init_pins, nid);
1068 EXPORT_SYMBOL_HDA(snd_hda_codec_get_pincfg);
1071 * snd_hda_shutup_pins - Shut up all pins
1072 * @codec: the HDA codec
1074 * Clear all pin controls to shup up before suspend for avoiding click noise.
1075 * The controls aren't cached so that they can be resumed properly.
1077 void snd_hda_shutup_pins(struct hda_codec *codec)
1080 /* don't shut up pins when unloading the driver; otherwise it breaks
1081 * the default pin setup at the next load of the driver
1083 if (codec->bus->shutdown)
1085 for (i = 0; i < codec->init_pins.used; i++) {
1086 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1087 /* use read here for syncing after issuing each verb */
1088 snd_hda_codec_read(codec, pin->nid, 0,
1089 AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
1091 codec->pins_shutup = 1;
1093 EXPORT_SYMBOL_HDA(snd_hda_shutup_pins);
1096 /* Restore the pin controls cleared previously via snd_hda_shutup_pins() */
1097 static void restore_shutup_pins(struct hda_codec *codec)
1100 if (!codec->pins_shutup)
1102 if (codec->bus->shutdown)
1104 for (i = 0; i < codec->init_pins.used; i++) {
1105 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1106 snd_hda_codec_write(codec, pin->nid, 0,
1107 AC_VERB_SET_PIN_WIDGET_CONTROL,
1110 codec->pins_shutup = 0;
1114 static void hda_jackpoll_work(struct work_struct *work)
1116 struct hda_codec *codec =
1117 container_of(work, struct hda_codec, jackpoll_work.work);
1118 if (!codec->jackpoll_interval)
1121 snd_hda_jack_set_dirty_all(codec);
1122 snd_hda_jack_poll_all(codec);
1123 queue_delayed_work(codec->bus->workq, &codec->jackpoll_work,
1124 codec->jackpoll_interval);
1127 static void init_hda_cache(struct hda_cache_rec *cache,
1128 unsigned int record_size);
1129 static void free_hda_cache(struct hda_cache_rec *cache);
1131 /* release all pincfg lists */
1132 static void free_init_pincfgs(struct hda_codec *codec)
1134 snd_array_free(&codec->driver_pins);
1135 #ifdef CONFIG_SND_HDA_HWDEP
1136 snd_array_free(&codec->user_pins);
1138 snd_array_free(&codec->init_pins);
1142 * audio-converter setup caches
1144 struct hda_cvt_setup {
1149 unsigned char active; /* cvt is currently used */
1150 unsigned char dirty; /* setups should be cleared */
1153 /* get or create a cache entry for the given audio converter NID */
1154 static struct hda_cvt_setup *
1155 get_hda_cvt_setup(struct hda_codec *codec, hda_nid_t nid)
1157 struct hda_cvt_setup *p;
1160 for (i = 0; i < codec->cvt_setups.used; i++) {
1161 p = snd_array_elem(&codec->cvt_setups, i);
1165 p = snd_array_new(&codec->cvt_setups);
1174 static void snd_hda_codec_free(struct hda_codec *codec)
1178 cancel_delayed_work_sync(&codec->jackpoll_work);
1179 snd_hda_jack_tbl_clear(codec);
1180 free_init_pincfgs(codec);
1182 cancel_delayed_work(&codec->power_work);
1183 flush_workqueue(codec->bus->workq);
1185 list_del(&codec->list);
1186 snd_array_free(&codec->mixers);
1187 snd_array_free(&codec->nids);
1188 snd_array_free(&codec->cvt_setups);
1189 snd_array_free(&codec->conn_lists);
1190 snd_array_free(&codec->spdif_out);
1191 codec->bus->caddr_tbl[codec->addr] = NULL;
1192 if (codec->patch_ops.free)
1193 codec->patch_ops.free(codec);
1195 if (!codec->pm_down_notified) /* cancel leftover refcounts */
1196 hda_call_pm_notify(codec->bus, false);
1198 module_put(codec->owner);
1199 free_hda_cache(&codec->amp_cache);
1200 free_hda_cache(&codec->cmd_cache);
1201 kfree(codec->vendor_name);
1202 kfree(codec->chip_name);
1203 kfree(codec->modelname);
1204 kfree(codec->wcaps);
1208 static bool snd_hda_codec_get_supported_ps(struct hda_codec *codec,
1209 hda_nid_t fg, unsigned int power_state);
1211 static unsigned int hda_set_power_state(struct hda_codec *codec,
1212 unsigned int power_state);
1215 * snd_hda_codec_new - create a HDA codec
1216 * @bus: the bus to assign
1217 * @codec_addr: the codec address
1218 * @codecp: the pointer to store the generated codec
1220 * Returns 0 if successful, or a negative error code.
1222 int snd_hda_codec_new(struct hda_bus *bus,
1223 unsigned int codec_addr,
1224 struct hda_codec **codecp)
1226 struct hda_codec *codec;
1231 if (snd_BUG_ON(!bus))
1233 if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS))
1236 if (bus->caddr_tbl[codec_addr]) {
1237 snd_printk(KERN_ERR "hda_codec: "
1238 "address 0x%x is already occupied\n", codec_addr);
1242 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
1243 if (codec == NULL) {
1244 snd_printk(KERN_ERR "can't allocate struct hda_codec\n");
1249 codec->addr = codec_addr;
1250 mutex_init(&codec->spdif_mutex);
1251 mutex_init(&codec->control_mutex);
1252 mutex_init(&codec->hash_mutex);
1253 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
1254 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
1255 snd_array_init(&codec->mixers, sizeof(struct hda_nid_item), 32);
1256 snd_array_init(&codec->nids, sizeof(struct hda_nid_item), 32);
1257 snd_array_init(&codec->init_pins, sizeof(struct hda_pincfg), 16);
1258 snd_array_init(&codec->driver_pins, sizeof(struct hda_pincfg), 16);
1259 snd_array_init(&codec->cvt_setups, sizeof(struct hda_cvt_setup), 8);
1260 snd_array_init(&codec->conn_lists, sizeof(hda_nid_t), 64);
1261 snd_array_init(&codec->spdif_out, sizeof(struct hda_spdif_out), 16);
1262 snd_array_init(&codec->jacktbl, sizeof(struct hda_jack_tbl), 16);
1263 snd_array_init(&codec->verbs, sizeof(struct hda_verb *), 8);
1264 INIT_DELAYED_WORK(&codec->jackpoll_work, hda_jackpoll_work);
1267 spin_lock_init(&codec->power_lock);
1268 INIT_DELAYED_WORK(&codec->power_work, hda_power_work);
1269 /* snd_hda_codec_new() marks the codec as power-up, and leave it as is.
1270 * the caller has to power down appropriatley after initialization
1273 hda_keep_power_on(codec);
1274 hda_call_pm_notify(bus, true);
1277 if (codec->bus->modelname) {
1278 codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL);
1279 if (!codec->modelname) {
1280 snd_hda_codec_free(codec);
1285 list_add_tail(&codec->list, &bus->codec_list);
1286 bus->caddr_tbl[codec_addr] = codec;
1288 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1290 if (codec->vendor_id == -1)
1291 /* read again, hopefully the access method was corrected
1292 * in the last read...
1294 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1296 codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1297 AC_PAR_SUBSYSTEM_ID);
1298 codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1301 setup_fg_nodes(codec);
1302 if (!codec->afg && !codec->mfg) {
1303 snd_printdd("hda_codec: no AFG or MFG node found\n");
1308 fg = codec->afg ? codec->afg : codec->mfg;
1309 err = read_widget_caps(codec, fg);
1311 snd_printk(KERN_ERR "hda_codec: cannot malloc\n");
1314 err = read_pin_defaults(codec);
1318 if (!codec->subsystem_id) {
1319 codec->subsystem_id =
1320 snd_hda_codec_read(codec, fg, 0,
1321 AC_VERB_GET_SUBSYSTEM_ID, 0);
1325 codec->d3_stop_clk = snd_hda_codec_get_supported_ps(codec, fg,
1327 if (!codec->d3_stop_clk)
1328 bus->power_keep_link_on = 1;
1330 codec->epss = snd_hda_codec_get_supported_ps(codec, fg,
1333 /* power-up all before initialization */
1334 hda_set_power_state(codec, AC_PWRST_D0);
1336 snd_hda_codec_proc_new(codec);
1338 snd_hda_create_hwdep(codec);
1340 sprintf(component, "HDA:%08x,%08x,%08x", codec->vendor_id,
1341 codec->subsystem_id, codec->revision_id);
1342 snd_component_add(codec->bus->card, component);
1349 snd_hda_codec_free(codec);
1352 EXPORT_SYMBOL_HDA(snd_hda_codec_new);
1355 * snd_hda_codec_configure - (Re-)configure the HD-audio codec
1356 * @codec: the HDA codec
1358 * Start parsing of the given codec tree and (re-)initialize the whole
1361 * Returns 0 if successful or a negative error code.
1363 int snd_hda_codec_configure(struct hda_codec *codec)
1367 codec->preset = find_codec_preset(codec);
1368 if (!codec->vendor_name || !codec->chip_name) {
1369 err = get_codec_name(codec);
1374 if (is_generic_config(codec)) {
1375 err = snd_hda_parse_generic_codec(codec);
1378 if (codec->preset && codec->preset->patch) {
1379 err = codec->preset->patch(codec);
1383 /* call the default parser */
1384 err = snd_hda_parse_generic_codec(codec);
1386 printk(KERN_ERR "hda-codec: No codec parser is available\n");
1389 if (!err && codec->patch_ops.unsol_event)
1390 err = init_unsol_queue(codec->bus);
1391 /* audio codec should override the mixer name */
1392 if (!err && (codec->afg || !*codec->bus->card->mixername))
1393 snprintf(codec->bus->card->mixername,
1394 sizeof(codec->bus->card->mixername),
1395 "%s %s", codec->vendor_name, codec->chip_name);
1398 EXPORT_SYMBOL_HDA(snd_hda_codec_configure);
1400 /* update the stream-id if changed */
1401 static void update_pcm_stream_id(struct hda_codec *codec,
1402 struct hda_cvt_setup *p, hda_nid_t nid,
1403 u32 stream_tag, int channel_id)
1405 unsigned int oldval, newval;
1407 if (p->stream_tag != stream_tag || p->channel_id != channel_id) {
1408 oldval = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0);
1409 newval = (stream_tag << 4) | channel_id;
1410 if (oldval != newval)
1411 snd_hda_codec_write(codec, nid, 0,
1412 AC_VERB_SET_CHANNEL_STREAMID,
1414 p->stream_tag = stream_tag;
1415 p->channel_id = channel_id;
1419 /* update the format-id if changed */
1420 static void update_pcm_format(struct hda_codec *codec, struct hda_cvt_setup *p,
1421 hda_nid_t nid, int format)
1423 unsigned int oldval;
1425 if (p->format_id != format) {
1426 oldval = snd_hda_codec_read(codec, nid, 0,
1427 AC_VERB_GET_STREAM_FORMAT, 0);
1428 if (oldval != format) {
1430 snd_hda_codec_write(codec, nid, 0,
1431 AC_VERB_SET_STREAM_FORMAT,
1434 p->format_id = format;
1439 * snd_hda_codec_setup_stream - set up the codec for streaming
1440 * @codec: the CODEC to set up
1441 * @nid: the NID to set up
1442 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
1443 * @channel_id: channel id to pass, zero based.
1444 * @format: stream format.
1446 void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
1448 int channel_id, int format)
1450 struct hda_codec *c;
1451 struct hda_cvt_setup *p;
1458 snd_printdd("hda_codec_setup_stream: "
1459 "NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
1460 nid, stream_tag, channel_id, format);
1461 p = get_hda_cvt_setup(codec, nid);
1465 if (codec->pcm_format_first)
1466 update_pcm_format(codec, p, nid, format);
1467 update_pcm_stream_id(codec, p, nid, stream_tag, channel_id);
1468 if (!codec->pcm_format_first)
1469 update_pcm_format(codec, p, nid, format);
1474 /* make other inactive cvts with the same stream-tag dirty */
1475 type = get_wcaps_type(get_wcaps(codec, nid));
1476 list_for_each_entry(c, &codec->bus->codec_list, list) {
1477 for (i = 0; i < c->cvt_setups.used; i++) {
1478 p = snd_array_elem(&c->cvt_setups, i);
1479 if (!p->active && p->stream_tag == stream_tag &&
1480 get_wcaps_type(get_wcaps(c, p->nid)) == type)
1485 EXPORT_SYMBOL_HDA(snd_hda_codec_setup_stream);
1487 static void really_cleanup_stream(struct hda_codec *codec,
1488 struct hda_cvt_setup *q);
1491 * __snd_hda_codec_cleanup_stream - clean up the codec for closing
1492 * @codec: the CODEC to clean up
1493 * @nid: the NID to clean up
1494 * @do_now: really clean up the stream instead of clearing the active flag
1496 void __snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid,
1499 struct hda_cvt_setup *p;
1504 if (codec->no_sticky_stream)
1507 snd_printdd("hda_codec_cleanup_stream: NID=0x%x\n", nid);
1508 p = get_hda_cvt_setup(codec, nid);
1510 /* here we just clear the active flag when do_now isn't set;
1511 * actual clean-ups will be done later in
1512 * purify_inactive_streams() called from snd_hda_codec_prpapre()
1515 really_cleanup_stream(codec, p);
1520 EXPORT_SYMBOL_HDA(__snd_hda_codec_cleanup_stream);
1522 static void really_cleanup_stream(struct hda_codec *codec,
1523 struct hda_cvt_setup *q)
1525 hda_nid_t nid = q->nid;
1526 if (q->stream_tag || q->channel_id)
1527 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
1529 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0
1531 memset(q, 0, sizeof(*q));
1535 /* clean up the all conflicting obsolete streams */
1536 static void purify_inactive_streams(struct hda_codec *codec)
1538 struct hda_codec *c;
1541 list_for_each_entry(c, &codec->bus->codec_list, list) {
1542 for (i = 0; i < c->cvt_setups.used; i++) {
1543 struct hda_cvt_setup *p;
1544 p = snd_array_elem(&c->cvt_setups, i);
1546 really_cleanup_stream(c, p);
1552 /* clean up all streams; called from suspend */
1553 static void hda_cleanup_all_streams(struct hda_codec *codec)
1557 for (i = 0; i < codec->cvt_setups.used; i++) {
1558 struct hda_cvt_setup *p = snd_array_elem(&codec->cvt_setups, i);
1560 really_cleanup_stream(codec, p);
1566 * amp access functions
1569 /* FIXME: more better hash key? */
1570 #define HDA_HASH_KEY(nid, dir, idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
1571 #define HDA_HASH_PINCAP_KEY(nid) (u32)((nid) + (0x02 << 24))
1572 #define HDA_HASH_PARPCM_KEY(nid) (u32)((nid) + (0x03 << 24))
1573 #define HDA_HASH_PARSTR_KEY(nid) (u32)((nid) + (0x04 << 24))
1574 #define INFO_AMP_CAPS (1<<0)
1575 #define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
1577 /* initialize the hash table */
1578 static void init_hda_cache(struct hda_cache_rec *cache,
1579 unsigned int record_size)
1581 memset(cache, 0, sizeof(*cache));
1582 memset(cache->hash, 0xff, sizeof(cache->hash));
1583 snd_array_init(&cache->buf, record_size, 64);
1586 static void free_hda_cache(struct hda_cache_rec *cache)
1588 snd_array_free(&cache->buf);
1591 /* query the hash. allocate an entry if not found. */
1592 static struct hda_cache_head *get_hash(struct hda_cache_rec *cache, u32 key)
1594 u16 idx = key % (u16)ARRAY_SIZE(cache->hash);
1595 u16 cur = cache->hash[idx];
1596 struct hda_cache_head *info;
1598 while (cur != 0xffff) {
1599 info = snd_array_elem(&cache->buf, cur);
1600 if (info->key == key)
1607 /* query the hash. allocate an entry if not found. */
1608 static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache,
1611 struct hda_cache_head *info = get_hash(cache, key);
1614 /* add a new hash entry */
1615 info = snd_array_new(&cache->buf);
1618 cur = snd_array_index(&cache->buf, info);
1622 idx = key % (u16)ARRAY_SIZE(cache->hash);
1623 info->next = cache->hash[idx];
1624 cache->hash[idx] = cur;
1629 /* query and allocate an amp hash entry */
1630 static inline struct hda_amp_info *
1631 get_alloc_amp_hash(struct hda_codec *codec, u32 key)
1633 return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key);
1636 /* overwrite the value with the key in the caps hash */
1637 static int write_caps_hash(struct hda_codec *codec, u32 key, unsigned int val)
1639 struct hda_amp_info *info;
1641 mutex_lock(&codec->hash_mutex);
1642 info = get_alloc_amp_hash(codec, key);
1644 mutex_unlock(&codec->hash_mutex);
1647 info->amp_caps = val;
1648 info->head.val |= INFO_AMP_CAPS;
1649 mutex_unlock(&codec->hash_mutex);
1653 /* query the value from the caps hash; if not found, fetch the current
1654 * value from the given function and store in the hash
1657 query_caps_hash(struct hda_codec *codec, hda_nid_t nid, int dir, u32 key,
1658 unsigned int (*func)(struct hda_codec *, hda_nid_t, int))
1660 struct hda_amp_info *info;
1663 mutex_lock(&codec->hash_mutex);
1664 info = get_alloc_amp_hash(codec, key);
1666 mutex_unlock(&codec->hash_mutex);
1669 if (!(info->head.val & INFO_AMP_CAPS)) {
1670 mutex_unlock(&codec->hash_mutex); /* for reentrance */
1671 val = func(codec, nid, dir);
1672 write_caps_hash(codec, key, val);
1674 val = info->amp_caps;
1675 mutex_unlock(&codec->hash_mutex);
1680 static unsigned int read_amp_cap(struct hda_codec *codec, hda_nid_t nid,
1683 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
1685 return snd_hda_param_read(codec, nid,
1686 direction == HDA_OUTPUT ?
1687 AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP);
1691 * query_amp_caps - query AMP capabilities
1692 * @codec: the HD-auio codec
1693 * @nid: the NID to query
1694 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1696 * Query AMP capabilities for the given widget and direction.
1697 * Returns the obtained capability bits.
1699 * When cap bits have been already read, this doesn't read again but
1700 * returns the cached value.
1702 u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
1704 return query_caps_hash(codec, nid, direction,
1705 HDA_HASH_KEY(nid, direction, 0),
1708 EXPORT_SYMBOL_HDA(query_amp_caps);
1711 * snd_hda_override_amp_caps - Override the AMP capabilities
1712 * @codec: the CODEC to clean up
1713 * @nid: the NID to clean up
1714 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1715 * @caps: the capability bits to set
1717 * Override the cached AMP caps bits value by the given one.
1718 * This function is useful if the driver needs to adjust the AMP ranges,
1719 * e.g. limit to 0dB, etc.
1721 * Returns zero if successful or a negative error code.
1723 int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
1726 return write_caps_hash(codec, HDA_HASH_KEY(nid, dir, 0), caps);
1728 EXPORT_SYMBOL_HDA(snd_hda_override_amp_caps);
1730 static unsigned int read_pin_cap(struct hda_codec *codec, hda_nid_t nid,
1733 return snd_hda_param_read(codec, nid, AC_PAR_PIN_CAP);
1737 * snd_hda_query_pin_caps - Query PIN capabilities
1738 * @codec: the HD-auio codec
1739 * @nid: the NID to query
1741 * Query PIN capabilities for the given widget.
1742 * Returns the obtained capability bits.
1744 * When cap bits have been already read, this doesn't read again but
1745 * returns the cached value.
1747 u32 snd_hda_query_pin_caps(struct hda_codec *codec, hda_nid_t nid)
1749 return query_caps_hash(codec, nid, 0, HDA_HASH_PINCAP_KEY(nid),
1752 EXPORT_SYMBOL_HDA(snd_hda_query_pin_caps);
1755 * snd_hda_override_pin_caps - Override the pin capabilities
1757 * @nid: the NID to override
1758 * @caps: the capability bits to set
1760 * Override the cached PIN capabilitiy bits value by the given one.
1762 * Returns zero if successful or a negative error code.
1764 int snd_hda_override_pin_caps(struct hda_codec *codec, hda_nid_t nid,
1767 return write_caps_hash(codec, HDA_HASH_PINCAP_KEY(nid), caps);
1769 EXPORT_SYMBOL_HDA(snd_hda_override_pin_caps);
1771 /* read or sync the hash value with the current value;
1772 * call within hash_mutex
1774 static struct hda_amp_info *
1775 update_amp_hash(struct hda_codec *codec, hda_nid_t nid, int ch,
1776 int direction, int index, bool init_only)
1778 struct hda_amp_info *info;
1779 unsigned int parm, val = 0;
1780 bool val_read = false;
1783 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
1786 if (!(info->head.val & INFO_AMP_VOL(ch))) {
1788 mutex_unlock(&codec->hash_mutex);
1789 parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
1790 parm |= direction == HDA_OUTPUT ?
1791 AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
1793 val = snd_hda_codec_read(codec, nid, 0,
1794 AC_VERB_GET_AMP_GAIN_MUTE, parm);
1797 mutex_lock(&codec->hash_mutex);
1800 info->vol[ch] = val;
1801 info->head.val |= INFO_AMP_VOL(ch);
1802 } else if (init_only)
1808 * write the current volume in info to the h/w
1810 static void put_vol_mute(struct hda_codec *codec, unsigned int amp_caps,
1811 hda_nid_t nid, int ch, int direction, int index,
1816 parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
1817 parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
1818 parm |= index << AC_AMP_SET_INDEX_SHIFT;
1819 if ((val & HDA_AMP_MUTE) && !(amp_caps & AC_AMPCAP_MUTE) &&
1820 (amp_caps & AC_AMPCAP_MIN_MUTE))
1821 ; /* set the zero value as a fake mute */
1824 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
1828 * snd_hda_codec_amp_read - Read AMP value
1829 * @codec: HD-audio codec
1830 * @nid: NID to read the AMP value
1831 * @ch: channel (left=0 or right=1)
1832 * @direction: #HDA_INPUT or #HDA_OUTPUT
1833 * @index: the index value (only for input direction)
1835 * Read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
1837 int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
1838 int direction, int index)
1840 struct hda_amp_info *info;
1841 unsigned int val = 0;
1843 mutex_lock(&codec->hash_mutex);
1844 info = update_amp_hash(codec, nid, ch, direction, index, false);
1846 val = info->vol[ch];
1847 mutex_unlock(&codec->hash_mutex);
1850 EXPORT_SYMBOL_HDA(snd_hda_codec_amp_read);
1852 static int codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
1853 int direction, int idx, int mask, int val,
1856 struct hda_amp_info *info;
1858 unsigned int cache_only;
1860 if (snd_BUG_ON(mask & ~0xff))
1864 mutex_lock(&codec->hash_mutex);
1865 info = update_amp_hash(codec, nid, ch, direction, idx, init_only);
1867 mutex_unlock(&codec->hash_mutex);
1870 val |= info->vol[ch] & ~mask;
1871 if (info->vol[ch] == val) {
1872 mutex_unlock(&codec->hash_mutex);
1875 info->vol[ch] = val;
1876 cache_only = info->head.dirty = codec->cached_write;
1877 caps = info->amp_caps;
1878 mutex_unlock(&codec->hash_mutex);
1880 put_vol_mute(codec, caps, nid, ch, direction, idx, val);
1885 * snd_hda_codec_amp_update - update the AMP value
1886 * @codec: HD-audio codec
1887 * @nid: NID to read the AMP value
1888 * @ch: channel (left=0 or right=1)
1889 * @direction: #HDA_INPUT or #HDA_OUTPUT
1890 * @idx: the index value (only for input direction)
1891 * @mask: bit mask to set
1892 * @val: the bits value to set
1894 * Update the AMP value with a bit mask.
1895 * Returns 0 if the value is unchanged, 1 if changed.
1897 int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
1898 int direction, int idx, int mask, int val)
1900 return codec_amp_update(codec, nid, ch, direction, idx, mask, val, false);
1902 EXPORT_SYMBOL_HDA(snd_hda_codec_amp_update);
1905 * snd_hda_codec_amp_stereo - update the AMP stereo values
1906 * @codec: HD-audio codec
1907 * @nid: NID to read the AMP value
1908 * @direction: #HDA_INPUT or #HDA_OUTPUT
1909 * @idx: the index value (only for input direction)
1910 * @mask: bit mask to set
1911 * @val: the bits value to set
1913 * Update the AMP values like snd_hda_codec_amp_update(), but for a
1914 * stereo widget with the same mask and value.
1916 int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
1917 int direction, int idx, int mask, int val)
1921 if (snd_BUG_ON(mask & ~0xff))
1923 for (ch = 0; ch < 2; ch++)
1924 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
1928 EXPORT_SYMBOL_HDA(snd_hda_codec_amp_stereo);
1930 /* Works like snd_hda_codec_amp_update() but it writes the value only at
1931 * the first access. If the amp was already initialized / updated beforehand,
1932 * this does nothing.
1934 int snd_hda_codec_amp_init(struct hda_codec *codec, hda_nid_t nid, int ch,
1935 int dir, int idx, int mask, int val)
1937 return codec_amp_update(codec, nid, ch, dir, idx, mask, val, true);
1939 EXPORT_SYMBOL_HDA(snd_hda_codec_amp_init);
1941 int snd_hda_codec_amp_init_stereo(struct hda_codec *codec, hda_nid_t nid,
1942 int dir, int idx, int mask, int val)
1946 if (snd_BUG_ON(mask & ~0xff))
1948 for (ch = 0; ch < 2; ch++)
1949 ret |= snd_hda_codec_amp_init(codec, nid, ch, dir,
1953 EXPORT_SYMBOL_HDA(snd_hda_codec_amp_init_stereo);
1956 * snd_hda_codec_resume_amp - Resume all AMP commands from the cache
1957 * @codec: HD-audio codec
1959 * Resume the all amp commands from the cache.
1961 void snd_hda_codec_resume_amp(struct hda_codec *codec)
1965 mutex_lock(&codec->hash_mutex);
1966 codec->cached_write = 0;
1967 for (i = 0; i < codec->amp_cache.buf.used; i++) {
1968 struct hda_amp_info *buffer;
1971 unsigned int idx, dir, ch;
1972 struct hda_amp_info info;
1974 buffer = snd_array_elem(&codec->amp_cache.buf, i);
1975 if (!buffer->head.dirty)
1977 buffer->head.dirty = 0;
1979 key = info.head.key;
1983 idx = (key >> 16) & 0xff;
1984 dir = (key >> 24) & 0xff;
1985 for (ch = 0; ch < 2; ch++) {
1986 if (!(info.head.val & INFO_AMP_VOL(ch)))
1988 mutex_unlock(&codec->hash_mutex);
1989 put_vol_mute(codec, info.amp_caps, nid, ch, dir, idx,
1991 mutex_lock(&codec->hash_mutex);
1994 mutex_unlock(&codec->hash_mutex);
1996 EXPORT_SYMBOL_HDA(snd_hda_codec_resume_amp);
1998 static u32 get_amp_max_value(struct hda_codec *codec, hda_nid_t nid, int dir,
2001 u32 caps = query_amp_caps(codec, nid, dir);
2003 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
2010 * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer
2012 * The control element is supposed to have the private_value field
2013 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2015 int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
2016 struct snd_ctl_elem_info *uinfo)
2018 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2019 u16 nid = get_amp_nid(kcontrol);
2020 u8 chs = get_amp_channels(kcontrol);
2021 int dir = get_amp_direction(kcontrol);
2022 unsigned int ofs = get_amp_offset(kcontrol);
2024 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2025 uinfo->count = chs == 3 ? 2 : 1;
2026 uinfo->value.integer.min = 0;
2027 uinfo->value.integer.max = get_amp_max_value(codec, nid, dir, ofs);
2028 if (!uinfo->value.integer.max) {
2029 printk(KERN_WARNING "hda_codec: "
2030 "num_steps = 0 for NID=0x%x (ctl = %s)\n", nid,
2036 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_info);
2039 static inline unsigned int
2040 read_amp_value(struct hda_codec *codec, hda_nid_t nid,
2041 int ch, int dir, int idx, unsigned int ofs)
2044 val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
2045 val &= HDA_AMP_VOLMASK;
2054 update_amp_value(struct hda_codec *codec, hda_nid_t nid,
2055 int ch, int dir, int idx, unsigned int ofs,
2058 unsigned int maxval;
2062 /* ofs = 0: raw max value */
2063 maxval = get_amp_max_value(codec, nid, dir, 0);
2066 return snd_hda_codec_amp_update(codec, nid, ch, dir, idx,
2067 HDA_AMP_VOLMASK, val);
2071 * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume
2073 * The control element is supposed to have the private_value field
2074 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2076 int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
2077 struct snd_ctl_elem_value *ucontrol)
2079 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2080 hda_nid_t nid = get_amp_nid(kcontrol);
2081 int chs = get_amp_channels(kcontrol);
2082 int dir = get_amp_direction(kcontrol);
2083 int idx = get_amp_index(kcontrol);
2084 unsigned int ofs = get_amp_offset(kcontrol);
2085 long *valp = ucontrol->value.integer.value;
2088 *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs);
2090 *valp = read_amp_value(codec, nid, 1, dir, idx, ofs);
2093 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_get);
2096 * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume
2098 * The control element is supposed to have the private_value field
2099 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2101 int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
2102 struct snd_ctl_elem_value *ucontrol)
2104 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2105 hda_nid_t nid = get_amp_nid(kcontrol);
2106 int chs = get_amp_channels(kcontrol);
2107 int dir = get_amp_direction(kcontrol);
2108 int idx = get_amp_index(kcontrol);
2109 unsigned int ofs = get_amp_offset(kcontrol);
2110 long *valp = ucontrol->value.integer.value;
2113 snd_hda_power_up(codec);
2115 change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp);
2119 change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp);
2120 snd_hda_power_down(codec);
2123 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_put);
2126 * snd_hda_mixer_amp_volume_put - TLV callback for a standard AMP mixer volume
2128 * The control element is supposed to have the private_value field
2129 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2131 int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2132 unsigned int size, unsigned int __user *_tlv)
2134 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2135 hda_nid_t nid = get_amp_nid(kcontrol);
2136 int dir = get_amp_direction(kcontrol);
2137 unsigned int ofs = get_amp_offset(kcontrol);
2138 bool min_mute = get_amp_min_mute(kcontrol);
2139 u32 caps, val1, val2;
2141 if (size < 4 * sizeof(unsigned int))
2143 caps = query_amp_caps(codec, nid, dir);
2144 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2145 val2 = (val2 + 1) * 25;
2146 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
2148 val1 = ((int)val1) * ((int)val2);
2149 if (min_mute || (caps & AC_AMPCAP_MIN_MUTE))
2150 val2 |= TLV_DB_SCALE_MUTE;
2151 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
2153 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
2155 if (put_user(val1, _tlv + 2))
2157 if (put_user(val2, _tlv + 3))
2161 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_tlv);
2164 * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control
2165 * @codec: HD-audio codec
2166 * @nid: NID of a reference widget
2167 * @dir: #HDA_INPUT or #HDA_OUTPUT
2168 * @tlv: TLV data to be stored, at least 4 elements
2170 * Set (static) TLV data for a virtual master volume using the AMP caps
2171 * obtained from the reference NID.
2172 * The volume range is recalculated as if the max volume is 0dB.
2174 void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
2180 caps = query_amp_caps(codec, nid, dir);
2181 nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
2182 step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2183 step = (step + 1) * 25;
2184 tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
2185 tlv[1] = 2 * sizeof(unsigned int);
2186 tlv[2] = -nums * step;
2189 EXPORT_SYMBOL_HDA(snd_hda_set_vmaster_tlv);
2191 /* find a mixer control element with the given name */
2192 static struct snd_kcontrol *
2193 find_mixer_ctl(struct hda_codec *codec, const char *name, int dev, int idx)
2195 struct snd_ctl_elem_id id;
2196 memset(&id, 0, sizeof(id));
2197 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
2200 if (snd_BUG_ON(strlen(name) >= sizeof(id.name)))
2202 strcpy(id.name, name);
2203 return snd_ctl_find_id(codec->bus->card, &id);
2207 * snd_hda_find_mixer_ctl - Find a mixer control element with the given name
2208 * @codec: HD-audio codec
2209 * @name: ctl id name string
2211 * Get the control element with the given id string and IFACE_MIXER.
2213 struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
2216 return find_mixer_ctl(codec, name, 0, 0);
2218 EXPORT_SYMBOL_HDA(snd_hda_find_mixer_ctl);
2220 static int find_empty_mixer_ctl_idx(struct hda_codec *codec, const char *name,
2224 for (idx = 0; idx < 16; idx++) { /* 16 ctlrs should be large enough */
2225 if (!find_mixer_ctl(codec, name, dev, idx))
2232 * snd_hda_ctl_add - Add a control element and assign to the codec
2233 * @codec: HD-audio codec
2234 * @nid: corresponding NID (optional)
2235 * @kctl: the control element to assign
2237 * Add the given control element to an array inside the codec instance.
2238 * All control elements belonging to a codec are supposed to be added
2239 * by this function so that a proper clean-up works at the free or
2240 * reconfiguration time.
2242 * If non-zero @nid is passed, the NID is assigned to the control element.
2243 * The assignment is shown in the codec proc file.
2245 * snd_hda_ctl_add() checks the control subdev id field whether
2246 * #HDA_SUBDEV_NID_FLAG bit is set. If set (and @nid is zero), the lower
2247 * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit
2248 * specifies if kctl->private_value is a HDA amplifier value.
2250 int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid,
2251 struct snd_kcontrol *kctl)
2254 unsigned short flags = 0;
2255 struct hda_nid_item *item;
2257 if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) {
2258 flags |= HDA_NID_ITEM_AMP;
2260 nid = get_amp_nid_(kctl->private_value);
2262 if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0)
2263 nid = kctl->id.subdevice & 0xffff;
2264 if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG))
2265 kctl->id.subdevice = 0;
2266 err = snd_ctl_add(codec->bus->card, kctl);
2269 item = snd_array_new(&codec->mixers);
2274 item->flags = flags;
2277 EXPORT_SYMBOL_HDA(snd_hda_ctl_add);
2280 * snd_hda_add_nid - Assign a NID to a control element
2281 * @codec: HD-audio codec
2282 * @nid: corresponding NID (optional)
2283 * @kctl: the control element to assign
2284 * @index: index to kctl
2286 * Add the given control element to an array inside the codec instance.
2287 * This function is used when #snd_hda_ctl_add cannot be used for 1:1
2288 * NID:KCTL mapping - for example "Capture Source" selector.
2290 int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl,
2291 unsigned int index, hda_nid_t nid)
2293 struct hda_nid_item *item;
2296 item = snd_array_new(&codec->nids);
2300 item->index = index;
2304 printk(KERN_ERR "hda-codec: no NID for mapping control %s:%d:%d\n",
2305 kctl->id.name, kctl->id.index, index);
2308 EXPORT_SYMBOL_HDA(snd_hda_add_nid);
2311 * snd_hda_ctls_clear - Clear all controls assigned to the given codec
2312 * @codec: HD-audio codec
2314 void snd_hda_ctls_clear(struct hda_codec *codec)
2317 struct hda_nid_item *items = codec->mixers.list;
2318 for (i = 0; i < codec->mixers.used; i++)
2319 snd_ctl_remove(codec->bus->card, items[i].kctl);
2320 snd_array_free(&codec->mixers);
2321 snd_array_free(&codec->nids);
2324 /* pseudo device locking
2325 * toggle card->shutdown to allow/disallow the device access (as a hack)
2327 int snd_hda_lock_devices(struct hda_bus *bus)
2329 struct snd_card *card = bus->card;
2330 struct hda_codec *codec;
2332 spin_lock(&card->files_lock);
2336 if (!list_empty(&card->ctl_files))
2339 list_for_each_entry(codec, &bus->codec_list, list) {
2341 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
2342 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
2345 if (cpcm->pcm->streams[0].substream_opened ||
2346 cpcm->pcm->streams[1].substream_opened)
2350 spin_unlock(&card->files_lock);
2356 spin_unlock(&card->files_lock);
2359 EXPORT_SYMBOL_HDA(snd_hda_lock_devices);
2361 void snd_hda_unlock_devices(struct hda_bus *bus)
2363 struct snd_card *card = bus->card;
2366 spin_lock(&card->files_lock);
2368 spin_unlock(&card->files_lock);
2370 EXPORT_SYMBOL_HDA(snd_hda_unlock_devices);
2373 * snd_hda_codec_reset - Clear all objects assigned to the codec
2374 * @codec: HD-audio codec
2376 * This frees the all PCM and control elements assigned to the codec, and
2377 * clears the caches and restores the pin default configurations.
2379 * When a device is being used, it returns -EBSY. If successfully freed,
2382 int snd_hda_codec_reset(struct hda_codec *codec)
2384 struct hda_bus *bus = codec->bus;
2385 struct snd_card *card = bus->card;
2388 if (snd_hda_lock_devices(bus) < 0)
2391 /* OK, let it free */
2392 cancel_delayed_work_sync(&codec->jackpoll_work);
2394 cancel_delayed_work_sync(&codec->power_work);
2395 codec->power_on = 0;
2396 codec->power_transition = 0;
2397 codec->power_jiffies = jiffies;
2398 flush_workqueue(bus->workq);
2400 snd_hda_ctls_clear(codec);
2402 for (i = 0; i < codec->num_pcms; i++) {
2403 if (codec->pcm_info[i].pcm) {
2404 snd_device_free(card, codec->pcm_info[i].pcm);
2405 clear_bit(codec->pcm_info[i].device,
2409 if (codec->patch_ops.free)
2410 codec->patch_ops.free(codec);
2411 memset(&codec->patch_ops, 0, sizeof(codec->patch_ops));
2412 snd_hda_jack_tbl_clear(codec);
2413 codec->proc_widget_hook = NULL;
2415 free_hda_cache(&codec->amp_cache);
2416 free_hda_cache(&codec->cmd_cache);
2417 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
2418 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
2419 /* free only driver_pins so that init_pins + user_pins are restored */
2420 snd_array_free(&codec->driver_pins);
2421 snd_array_free(&codec->cvt_setups);
2422 snd_array_free(&codec->spdif_out);
2423 snd_array_free(&codec->verbs);
2424 codec->num_pcms = 0;
2425 codec->pcm_info = NULL;
2426 codec->preset = NULL;
2427 codec->slave_dig_outs = NULL;
2428 codec->spdif_status_reset = 0;
2429 module_put(codec->owner);
2430 codec->owner = NULL;
2432 /* allow device access again */
2433 snd_hda_unlock_devices(bus);
2437 typedef int (*map_slave_func_t)(void *, struct snd_kcontrol *);
2439 /* apply the function to all matching slave ctls in the mixer list */
2440 static int map_slaves(struct hda_codec *codec, const char * const *slaves,
2441 const char *suffix, map_slave_func_t func, void *data)
2443 struct hda_nid_item *items;
2444 const char * const *s;
2447 items = codec->mixers.list;
2448 for (i = 0; i < codec->mixers.used; i++) {
2449 struct snd_kcontrol *sctl = items[i].kctl;
2450 if (!sctl || !sctl->id.name ||
2451 sctl->id.iface != SNDRV_CTL_ELEM_IFACE_MIXER)
2453 for (s = slaves; *s; s++) {
2454 char tmpname[sizeof(sctl->id.name)];
2455 const char *name = *s;
2457 snprintf(tmpname, sizeof(tmpname), "%s %s",
2461 if (!strcmp(sctl->id.name, name)) {
2462 err = func(data, sctl);
2472 static int check_slave_present(void *data, struct snd_kcontrol *sctl)
2477 /* guess the value corresponding to 0dB */
2478 static int get_kctl_0dB_offset(struct snd_kcontrol *kctl)
2481 const int *tlv = NULL;
2484 if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
2485 /* FIXME: set_fs() hack for obtaining user-space TLV data */
2486 mm_segment_t fs = get_fs();
2488 if (!kctl->tlv.c(kctl, 0, sizeof(_tlv), _tlv))
2491 } else if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_READ)
2493 if (tlv && tlv[0] == SNDRV_CTL_TLVT_DB_SCALE)
2494 val = -tlv[2] / tlv[3];
2498 /* call kctl->put with the given value(s) */
2499 static int put_kctl_with_value(struct snd_kcontrol *kctl, int val)
2501 struct snd_ctl_elem_value *ucontrol;
2502 ucontrol = kzalloc(sizeof(*ucontrol), GFP_KERNEL);
2505 ucontrol->value.integer.value[0] = val;
2506 ucontrol->value.integer.value[1] = val;
2507 kctl->put(kctl, ucontrol);
2512 /* initialize the slave volume with 0dB */
2513 static int init_slave_0dB(void *data, struct snd_kcontrol *slave)
2515 int offset = get_kctl_0dB_offset(slave);
2517 put_kctl_with_value(slave, offset);
2521 /* unmute the slave */
2522 static int init_slave_unmute(void *data, struct snd_kcontrol *slave)
2524 return put_kctl_with_value(slave, 1);
2528 * snd_hda_add_vmaster - create a virtual master control and add slaves
2529 * @codec: HD-audio codec
2530 * @name: vmaster control name
2531 * @tlv: TLV data (optional)
2532 * @slaves: slave control names (optional)
2533 * @suffix: suffix string to each slave name (optional)
2534 * @init_slave_vol: initialize slaves to unmute/0dB
2535 * @ctl_ret: store the vmaster kcontrol in return
2537 * Create a virtual master control with the given name. The TLV data
2538 * must be either NULL or a valid data.
2540 * @slaves is a NULL-terminated array of strings, each of which is a
2541 * slave control name. All controls with these names are assigned to
2542 * the new virtual master control.
2544 * This function returns zero if successful or a negative error code.
2546 int __snd_hda_add_vmaster(struct hda_codec *codec, char *name,
2547 unsigned int *tlv, const char * const *slaves,
2548 const char *suffix, bool init_slave_vol,
2549 struct snd_kcontrol **ctl_ret)
2551 struct snd_kcontrol *kctl;
2557 err = map_slaves(codec, slaves, suffix, check_slave_present, NULL);
2559 snd_printdd("No slave found for %s\n", name);
2562 kctl = snd_ctl_make_virtual_master(name, tlv);
2565 err = snd_hda_ctl_add(codec, 0, kctl);
2569 err = map_slaves(codec, slaves, suffix,
2570 (map_slave_func_t)snd_ctl_add_slave, kctl);
2574 /* init with master mute & zero volume */
2575 put_kctl_with_value(kctl, 0);
2577 map_slaves(codec, slaves, suffix,
2578 tlv ? init_slave_0dB : init_slave_unmute, kctl);
2584 EXPORT_SYMBOL_HDA(__snd_hda_add_vmaster);
2587 * mute-LED control using vmaster
2589 static int vmaster_mute_mode_info(struct snd_kcontrol *kcontrol,
2590 struct snd_ctl_elem_info *uinfo)
2592 static const char * const texts[] = {
2593 "On", "Off", "Follow Master"
2597 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2599 uinfo->value.enumerated.items = 3;
2600 index = uinfo->value.enumerated.item;
2603 strcpy(uinfo->value.enumerated.name, texts[index]);
2607 static int vmaster_mute_mode_get(struct snd_kcontrol *kcontrol,
2608 struct snd_ctl_elem_value *ucontrol)
2610 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2611 ucontrol->value.enumerated.item[0] = hook->mute_mode;
2615 static int vmaster_mute_mode_put(struct snd_kcontrol *kcontrol,
2616 struct snd_ctl_elem_value *ucontrol)
2618 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2619 unsigned int old_mode = hook->mute_mode;
2621 hook->mute_mode = ucontrol->value.enumerated.item[0];
2622 if (hook->mute_mode > HDA_VMUTE_FOLLOW_MASTER)
2623 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
2624 if (old_mode == hook->mute_mode)
2626 snd_hda_sync_vmaster_hook(hook);
2630 static struct snd_kcontrol_new vmaster_mute_mode = {
2631 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2632 .name = "Mute-LED Mode",
2633 .info = vmaster_mute_mode_info,
2634 .get = vmaster_mute_mode_get,
2635 .put = vmaster_mute_mode_put,
2639 * Add a mute-LED hook with the given vmaster switch kctl
2640 * "Mute-LED Mode" control is automatically created and associated with
2643 int snd_hda_add_vmaster_hook(struct hda_codec *codec,
2644 struct hda_vmaster_mute_hook *hook,
2645 bool expose_enum_ctl)
2647 struct snd_kcontrol *kctl;
2649 if (!hook->hook || !hook->sw_kctl)
2651 snd_ctl_add_vmaster_hook(hook->sw_kctl, hook->hook, codec);
2652 hook->codec = codec;
2653 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
2654 if (!expose_enum_ctl)
2656 kctl = snd_ctl_new1(&vmaster_mute_mode, hook);
2659 return snd_hda_ctl_add(codec, 0, kctl);
2661 EXPORT_SYMBOL_HDA(snd_hda_add_vmaster_hook);
2664 * Call the hook with the current value for synchronization
2665 * Should be called in init callback
2667 void snd_hda_sync_vmaster_hook(struct hda_vmaster_mute_hook *hook)
2669 if (!hook->hook || !hook->codec)
2671 switch (hook->mute_mode) {
2672 case HDA_VMUTE_FOLLOW_MASTER:
2673 snd_ctl_sync_vmaster_hook(hook->sw_kctl);
2676 hook->hook(hook->codec, hook->mute_mode);
2680 EXPORT_SYMBOL_HDA(snd_hda_sync_vmaster_hook);
2684 * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch
2686 * The control element is supposed to have the private_value field
2687 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2689 int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
2690 struct snd_ctl_elem_info *uinfo)
2692 int chs = get_amp_channels(kcontrol);
2694 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2695 uinfo->count = chs == 3 ? 2 : 1;
2696 uinfo->value.integer.min = 0;
2697 uinfo->value.integer.max = 1;
2700 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_info);
2703 * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch
2705 * The control element is supposed to have the private_value field
2706 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2708 int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
2709 struct snd_ctl_elem_value *ucontrol)
2711 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2712 hda_nid_t nid = get_amp_nid(kcontrol);
2713 int chs = get_amp_channels(kcontrol);
2714 int dir = get_amp_direction(kcontrol);
2715 int idx = get_amp_index(kcontrol);
2716 long *valp = ucontrol->value.integer.value;
2719 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
2720 HDA_AMP_MUTE) ? 0 : 1;
2722 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
2723 HDA_AMP_MUTE) ? 0 : 1;
2726 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_get);
2729 * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch
2731 * The control element is supposed to have the private_value field
2732 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2734 int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
2735 struct snd_ctl_elem_value *ucontrol)
2737 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2738 hda_nid_t nid = get_amp_nid(kcontrol);
2739 int chs = get_amp_channels(kcontrol);
2740 int dir = get_amp_direction(kcontrol);
2741 int idx = get_amp_index(kcontrol);
2742 long *valp = ucontrol->value.integer.value;
2745 snd_hda_power_up(codec);
2747 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
2749 *valp ? 0 : HDA_AMP_MUTE);
2753 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
2755 *valp ? 0 : HDA_AMP_MUTE);
2756 hda_call_check_power_status(codec, nid);
2757 snd_hda_power_down(codec);
2760 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put);
2763 * bound volume controls
2765 * bind multiple volumes (# indices, from 0)
2768 #define AMP_VAL_IDX_SHIFT 19
2769 #define AMP_VAL_IDX_MASK (0x0f<<19)
2772 * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control
2774 * The control element is supposed to have the private_value field
2775 * set up via HDA_BIND_MUTE*() macros.
2777 int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
2778 struct snd_ctl_elem_value *ucontrol)
2780 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2784 mutex_lock(&codec->control_mutex);
2785 pval = kcontrol->private_value;
2786 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
2787 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
2788 kcontrol->private_value = pval;
2789 mutex_unlock(&codec->control_mutex);
2792 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_get);
2795 * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control
2797 * The control element is supposed to have the private_value field
2798 * set up via HDA_BIND_MUTE*() macros.
2800 int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
2801 struct snd_ctl_elem_value *ucontrol)
2803 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2805 int i, indices, err = 0, change = 0;
2807 mutex_lock(&codec->control_mutex);
2808 pval = kcontrol->private_value;
2809 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
2810 for (i = 0; i < indices; i++) {
2811 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
2812 (i << AMP_VAL_IDX_SHIFT);
2813 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2818 kcontrol->private_value = pval;
2819 mutex_unlock(&codec->control_mutex);
2820 return err < 0 ? err : change;
2822 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_put);
2825 * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control
2827 * The control element is supposed to have the private_value field
2828 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2830 int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
2831 struct snd_ctl_elem_info *uinfo)
2833 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2834 struct hda_bind_ctls *c;
2837 mutex_lock(&codec->control_mutex);
2838 c = (struct hda_bind_ctls *)kcontrol->private_value;
2839 kcontrol->private_value = *c->values;
2840 err = c->ops->info(kcontrol, uinfo);
2841 kcontrol->private_value = (long)c;
2842 mutex_unlock(&codec->control_mutex);
2845 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_info);
2848 * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control
2850 * The control element is supposed to have the private_value field
2851 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2853 int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
2854 struct snd_ctl_elem_value *ucontrol)
2856 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2857 struct hda_bind_ctls *c;
2860 mutex_lock(&codec->control_mutex);
2861 c = (struct hda_bind_ctls *)kcontrol->private_value;
2862 kcontrol->private_value = *c->values;
2863 err = c->ops->get(kcontrol, ucontrol);
2864 kcontrol->private_value = (long)c;
2865 mutex_unlock(&codec->control_mutex);
2868 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_get);
2871 * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control
2873 * The control element is supposed to have the private_value field
2874 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2876 int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
2877 struct snd_ctl_elem_value *ucontrol)
2879 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2880 struct hda_bind_ctls *c;
2881 unsigned long *vals;
2882 int err = 0, change = 0;
2884 mutex_lock(&codec->control_mutex);
2885 c = (struct hda_bind_ctls *)kcontrol->private_value;
2886 for (vals = c->values; *vals; vals++) {
2887 kcontrol->private_value = *vals;
2888 err = c->ops->put(kcontrol, ucontrol);
2893 kcontrol->private_value = (long)c;
2894 mutex_unlock(&codec->control_mutex);
2895 return err < 0 ? err : change;
2897 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_put);
2900 * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control
2902 * The control element is supposed to have the private_value field
2903 * set up via HDA_BIND_VOL() macro.
2905 int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2906 unsigned int size, unsigned int __user *tlv)
2908 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2909 struct hda_bind_ctls *c;
2912 mutex_lock(&codec->control_mutex);
2913 c = (struct hda_bind_ctls *)kcontrol->private_value;
2914 kcontrol->private_value = *c->values;
2915 err = c->ops->tlv(kcontrol, op_flag, size, tlv);
2916 kcontrol->private_value = (long)c;
2917 mutex_unlock(&codec->control_mutex);
2920 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_tlv);
2922 struct hda_ctl_ops snd_hda_bind_vol = {
2923 .info = snd_hda_mixer_amp_volume_info,
2924 .get = snd_hda_mixer_amp_volume_get,
2925 .put = snd_hda_mixer_amp_volume_put,
2926 .tlv = snd_hda_mixer_amp_tlv
2928 EXPORT_SYMBOL_HDA(snd_hda_bind_vol);
2930 struct hda_ctl_ops snd_hda_bind_sw = {
2931 .info = snd_hda_mixer_amp_switch_info,
2932 .get = snd_hda_mixer_amp_switch_get,
2933 .put = snd_hda_mixer_amp_switch_put,
2934 .tlv = snd_hda_mixer_amp_tlv
2936 EXPORT_SYMBOL_HDA(snd_hda_bind_sw);
2939 * SPDIF out controls
2942 static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
2943 struct snd_ctl_elem_info *uinfo)
2945 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
2950 static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
2951 struct snd_ctl_elem_value *ucontrol)
2953 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2954 IEC958_AES0_NONAUDIO |
2955 IEC958_AES0_CON_EMPHASIS_5015 |
2956 IEC958_AES0_CON_NOT_COPYRIGHT;
2957 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
2958 IEC958_AES1_CON_ORIGINAL;
2962 static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
2963 struct snd_ctl_elem_value *ucontrol)
2965 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2966 IEC958_AES0_NONAUDIO |
2967 IEC958_AES0_PRO_EMPHASIS_5015;
2971 static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
2972 struct snd_ctl_elem_value *ucontrol)
2974 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2975 int idx = kcontrol->private_value;
2976 struct hda_spdif_out *spdif;
2978 mutex_lock(&codec->spdif_mutex);
2979 spdif = snd_array_elem(&codec->spdif_out, idx);
2980 ucontrol->value.iec958.status[0] = spdif->status & 0xff;
2981 ucontrol->value.iec958.status[1] = (spdif->status >> 8) & 0xff;
2982 ucontrol->value.iec958.status[2] = (spdif->status >> 16) & 0xff;
2983 ucontrol->value.iec958.status[3] = (spdif->status >> 24) & 0xff;
2984 mutex_unlock(&codec->spdif_mutex);
2989 /* convert from SPDIF status bits to HDA SPDIF bits
2990 * bit 0 (DigEn) is always set zero (to be filled later)
2992 static unsigned short convert_from_spdif_status(unsigned int sbits)
2994 unsigned short val = 0;
2996 if (sbits & IEC958_AES0_PROFESSIONAL)
2997 val |= AC_DIG1_PROFESSIONAL;
2998 if (sbits & IEC958_AES0_NONAUDIO)
2999 val |= AC_DIG1_NONAUDIO;
3000 if (sbits & IEC958_AES0_PROFESSIONAL) {
3001 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
3002 IEC958_AES0_PRO_EMPHASIS_5015)
3003 val |= AC_DIG1_EMPHASIS;
3005 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
3006 IEC958_AES0_CON_EMPHASIS_5015)
3007 val |= AC_DIG1_EMPHASIS;
3008 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
3009 val |= AC_DIG1_COPYRIGHT;
3010 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
3011 val |= AC_DIG1_LEVEL;
3012 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
3017 /* convert to SPDIF status bits from HDA SPDIF bits
3019 static unsigned int convert_to_spdif_status(unsigned short val)
3021 unsigned int sbits = 0;
3023 if (val & AC_DIG1_NONAUDIO)
3024 sbits |= IEC958_AES0_NONAUDIO;
3025 if (val & AC_DIG1_PROFESSIONAL)
3026 sbits |= IEC958_AES0_PROFESSIONAL;
3027 if (sbits & IEC958_AES0_PROFESSIONAL) {
3028 if (sbits & AC_DIG1_EMPHASIS)
3029 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
3031 if (val & AC_DIG1_EMPHASIS)
3032 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
3033 if (!(val & AC_DIG1_COPYRIGHT))
3034 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
3035 if (val & AC_DIG1_LEVEL)
3036 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
3037 sbits |= val & (0x7f << 8);
3042 /* set digital convert verbs both for the given NID and its slaves */
3043 static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
3048 snd_hda_codec_write_cache(codec, nid, 0, verb, val);
3049 d = codec->slave_dig_outs;
3053 snd_hda_codec_write_cache(codec, *d, 0, verb, val);
3056 static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
3060 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1);
3062 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2);
3065 static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
3066 struct snd_ctl_elem_value *ucontrol)
3068 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3069 int idx = kcontrol->private_value;
3070 struct hda_spdif_out *spdif;
3075 mutex_lock(&codec->spdif_mutex);
3076 spdif = snd_array_elem(&codec->spdif_out, idx);
3078 spdif->status = ucontrol->value.iec958.status[0] |
3079 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
3080 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
3081 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
3082 val = convert_from_spdif_status(spdif->status);
3083 val |= spdif->ctls & 1;
3084 change = spdif->ctls != val;
3086 if (change && nid != (u16)-1)
3087 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
3088 mutex_unlock(&codec->spdif_mutex);
3092 #define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
3094 static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
3095 struct snd_ctl_elem_value *ucontrol)
3097 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3098 int idx = kcontrol->private_value;
3099 struct hda_spdif_out *spdif;
3101 mutex_lock(&codec->spdif_mutex);
3102 spdif = snd_array_elem(&codec->spdif_out, idx);
3103 ucontrol->value.integer.value[0] = spdif->ctls & AC_DIG1_ENABLE;
3104 mutex_unlock(&codec->spdif_mutex);
3108 static inline void set_spdif_ctls(struct hda_codec *codec, hda_nid_t nid,
3111 set_dig_out_convert(codec, nid, dig1, dig2);
3112 /* unmute amp switch (if any) */
3113 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
3114 (dig1 & AC_DIG1_ENABLE))
3115 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
3119 static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
3120 struct snd_ctl_elem_value *ucontrol)
3122 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3123 int idx = kcontrol->private_value;
3124 struct hda_spdif_out *spdif;
3129 mutex_lock(&codec->spdif_mutex);
3130 spdif = snd_array_elem(&codec->spdif_out, idx);
3132 val = spdif->ctls & ~AC_DIG1_ENABLE;
3133 if (ucontrol->value.integer.value[0])
3134 val |= AC_DIG1_ENABLE;
3135 change = spdif->ctls != val;
3137 if (change && nid != (u16)-1)
3138 set_spdif_ctls(codec, nid, val & 0xff, -1);
3139 mutex_unlock(&codec->spdif_mutex);
3143 static struct snd_kcontrol_new dig_mixes[] = {
3145 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3146 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3147 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
3148 .info = snd_hda_spdif_mask_info,
3149 .get = snd_hda_spdif_cmask_get,
3152 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3153 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3154 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK),
3155 .info = snd_hda_spdif_mask_info,
3156 .get = snd_hda_spdif_pmask_get,
3159 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3160 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
3161 .info = snd_hda_spdif_mask_info,
3162 .get = snd_hda_spdif_default_get,
3163 .put = snd_hda_spdif_default_put,
3166 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3167 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
3168 .info = snd_hda_spdif_out_switch_info,
3169 .get = snd_hda_spdif_out_switch_get,
3170 .put = snd_hda_spdif_out_switch_put,
3176 * snd_hda_create_dig_out_ctls - create Output SPDIF-related controls
3177 * @codec: the HDA codec
3178 * @associated_nid: NID that new ctls associated with
3179 * @cvt_nid: converter NID
3180 * @type: HDA_PCM_TYPE_*
3181 * Creates controls related with the digital output.
3182 * Called from each patch supporting the digital out.
3184 * Returns 0 if successful, or a negative error code.
3186 int snd_hda_create_dig_out_ctls(struct hda_codec *codec,
3187 hda_nid_t associated_nid,
3192 struct snd_kcontrol *kctl;
3193 struct snd_kcontrol_new *dig_mix;
3195 const int spdif_pcm_dev = 1;
3196 struct hda_spdif_out *spdif;
3198 if (codec->primary_dig_out_type == HDA_PCM_TYPE_HDMI &&
3199 type == HDA_PCM_TYPE_SPDIF) {
3200 dev = spdif_pcm_dev;
3201 } else if (codec->primary_dig_out_type == HDA_PCM_TYPE_SPDIF &&
3202 type == HDA_PCM_TYPE_HDMI) {
3203 for (idx = 0; idx < codec->spdif_out.used; idx++) {
3204 spdif = snd_array_elem(&codec->spdif_out, idx);
3205 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
3206 kctl = find_mixer_ctl(codec, dig_mix->name, 0, idx);
3209 kctl->id.device = spdif_pcm_dev;
3212 codec->primary_dig_out_type = HDA_PCM_TYPE_HDMI;
3214 if (!codec->primary_dig_out_type)
3215 codec->primary_dig_out_type = type;
3217 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Playback Switch", dev);
3219 printk(KERN_ERR "hda_codec: too many IEC958 outputs\n");
3222 spdif = snd_array_new(&codec->spdif_out);
3223 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
3224 kctl = snd_ctl_new1(dig_mix, codec);
3227 kctl->id.device = dev;
3228 kctl->id.index = idx;
3229 kctl->private_value = codec->spdif_out.used - 1;
3230 err = snd_hda_ctl_add(codec, associated_nid, kctl);
3234 spdif->nid = cvt_nid;
3235 spdif->ctls = snd_hda_codec_read(codec, cvt_nid, 0,
3236 AC_VERB_GET_DIGI_CONVERT_1, 0);
3237 spdif->status = convert_to_spdif_status(spdif->ctls);
3240 EXPORT_SYMBOL_HDA(snd_hda_create_dig_out_ctls);
3242 /* get the hda_spdif_out entry from the given NID
3243 * call within spdif_mutex lock
3245 struct hda_spdif_out *snd_hda_spdif_out_of_nid(struct hda_codec *codec,
3249 for (i = 0; i < codec->spdif_out.used; i++) {
3250 struct hda_spdif_out *spdif =
3251 snd_array_elem(&codec->spdif_out, i);
3252 if (spdif->nid == nid)
3257 EXPORT_SYMBOL_HDA(snd_hda_spdif_out_of_nid);
3259 void snd_hda_spdif_ctls_unassign(struct hda_codec *codec, int idx)
3261 struct hda_spdif_out *spdif;
3263 mutex_lock(&codec->spdif_mutex);
3264 spdif = snd_array_elem(&codec->spdif_out, idx);
3265 spdif->nid = (u16)-1;
3266 mutex_unlock(&codec->spdif_mutex);
3268 EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_unassign);
3270 void snd_hda_spdif_ctls_assign(struct hda_codec *codec, int idx, hda_nid_t nid)
3272 struct hda_spdif_out *spdif;
3275 mutex_lock(&codec->spdif_mutex);
3276 spdif = snd_array_elem(&codec->spdif_out, idx);
3277 if (spdif->nid != nid) {
3280 set_spdif_ctls(codec, nid, val & 0xff, (val >> 8) & 0xff);
3282 mutex_unlock(&codec->spdif_mutex);
3284 EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_assign);
3287 * SPDIF sharing with analog output
3289 static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
3290 struct snd_ctl_elem_value *ucontrol)
3292 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3293 ucontrol->value.integer.value[0] = mout->share_spdif;
3297 static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
3298 struct snd_ctl_elem_value *ucontrol)
3300 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3301 mout->share_spdif = !!ucontrol->value.integer.value[0];
3305 static struct snd_kcontrol_new spdif_share_sw = {
3306 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3307 .name = "IEC958 Default PCM Playback Switch",
3308 .info = snd_ctl_boolean_mono_info,
3309 .get = spdif_share_sw_get,
3310 .put = spdif_share_sw_put,
3314 * snd_hda_create_spdif_share_sw - create Default PCM switch
3315 * @codec: the HDA codec
3316 * @mout: multi-out instance
3318 int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
3319 struct hda_multi_out *mout)
3321 if (!mout->dig_out_nid)
3323 /* ATTENTION: here mout is passed as private_data, instead of codec */
3324 return snd_hda_ctl_add(codec, mout->dig_out_nid,
3325 snd_ctl_new1(&spdif_share_sw, mout));
3327 EXPORT_SYMBOL_HDA(snd_hda_create_spdif_share_sw);
3333 #define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
3335 static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
3336 struct snd_ctl_elem_value *ucontrol)
3338 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3340 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
3344 static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
3345 struct snd_ctl_elem_value *ucontrol)
3347 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3348 hda_nid_t nid = kcontrol->private_value;
3349 unsigned int val = !!ucontrol->value.integer.value[0];
3352 mutex_lock(&codec->spdif_mutex);
3353 change = codec->spdif_in_enable != val;
3355 codec->spdif_in_enable = val;
3356 snd_hda_codec_write_cache(codec, nid, 0,
3357 AC_VERB_SET_DIGI_CONVERT_1, val);
3359 mutex_unlock(&codec->spdif_mutex);
3363 static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
3364 struct snd_ctl_elem_value *ucontrol)
3366 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3367 hda_nid_t nid = kcontrol->private_value;
3371 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0);
3372 sbits = convert_to_spdif_status(val);
3373 ucontrol->value.iec958.status[0] = sbits;
3374 ucontrol->value.iec958.status[1] = sbits >> 8;
3375 ucontrol->value.iec958.status[2] = sbits >> 16;
3376 ucontrol->value.iec958.status[3] = sbits >> 24;
3380 static struct snd_kcontrol_new dig_in_ctls[] = {
3382 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3383 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH),
3384 .info = snd_hda_spdif_in_switch_info,
3385 .get = snd_hda_spdif_in_switch_get,
3386 .put = snd_hda_spdif_in_switch_put,
3389 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3390 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3391 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
3392 .info = snd_hda_spdif_mask_info,
3393 .get = snd_hda_spdif_in_status_get,
3399 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
3400 * @codec: the HDA codec
3401 * @nid: audio in widget NID
3403 * Creates controls related with the SPDIF input.
3404 * Called from each patch supporting the SPDIF in.
3406 * Returns 0 if successful, or a negative error code.
3408 int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
3411 struct snd_kcontrol *kctl;
3412 struct snd_kcontrol_new *dig_mix;
3415 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Capture Switch", 0);
3417 printk(KERN_ERR "hda_codec: too many IEC958 inputs\n");
3420 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
3421 kctl = snd_ctl_new1(dig_mix, codec);
3424 kctl->private_value = nid;
3425 err = snd_hda_ctl_add(codec, nid, kctl);
3429 codec->spdif_in_enable =
3430 snd_hda_codec_read(codec, nid, 0,
3431 AC_VERB_GET_DIGI_CONVERT_1, 0) &
3435 EXPORT_SYMBOL_HDA(snd_hda_create_spdif_in_ctls);
3441 /* build a 31bit cache key with the widget id and the command parameter */
3442 #define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
3443 #define get_cmd_cache_nid(key) ((key) & 0xff)
3444 #define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
3447 * snd_hda_codec_write_cache - send a single command with caching
3448 * @codec: the HDA codec
3449 * @nid: NID to send the command
3450 * @direct: direct flag
3451 * @verb: the verb to send
3452 * @parm: the parameter for the verb
3454 * Send a single command without waiting for response.
3456 * Returns 0 if successful, or a negative error code.
3458 int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
3459 int direct, unsigned int verb, unsigned int parm)
3462 struct hda_cache_head *c;
3464 unsigned int cache_only;
3466 cache_only = codec->cached_write;
3468 err = snd_hda_codec_write(codec, nid, direct, verb, parm);
3473 /* parm may contain the verb stuff for get/set amp */
3474 verb = verb | (parm >> 8);
3476 key = build_cmd_cache_key(nid, verb);
3477 mutex_lock(&codec->bus->cmd_mutex);
3478 c = get_alloc_hash(&codec->cmd_cache, key);
3481 c->dirty = cache_only;
3483 mutex_unlock(&codec->bus->cmd_mutex);
3486 EXPORT_SYMBOL_HDA(snd_hda_codec_write_cache);
3489 * snd_hda_codec_update_cache - check cache and write the cmd only when needed
3490 * @codec: the HDA codec
3491 * @nid: NID to send the command
3492 * @direct: direct flag
3493 * @verb: the verb to send
3494 * @parm: the parameter for the verb
3496 * This function works like snd_hda_codec_write_cache(), but it doesn't send
3497 * command if the parameter is already identical with the cached value.
3498 * If not, it sends the command and refreshes the cache.
3500 * Returns 0 if successful, or a negative error code.
3502 int snd_hda_codec_update_cache(struct hda_codec *codec, hda_nid_t nid,
3503 int direct, unsigned int verb, unsigned int parm)
3505 struct hda_cache_head *c;
3508 /* parm may contain the verb stuff for get/set amp */
3509 verb = verb | (parm >> 8);
3511 key = build_cmd_cache_key(nid, verb);
3512 mutex_lock(&codec->bus->cmd_mutex);
3513 c = get_hash(&codec->cmd_cache, key);
3514 if (c && c->val == parm) {
3515 mutex_unlock(&codec->bus->cmd_mutex);
3518 mutex_unlock(&codec->bus->cmd_mutex);
3519 return snd_hda_codec_write_cache(codec, nid, direct, verb, parm);
3521 EXPORT_SYMBOL_HDA(snd_hda_codec_update_cache);
3524 * snd_hda_codec_resume_cache - Resume the all commands from the cache
3525 * @codec: HD-audio codec
3527 * Execute all verbs recorded in the command caches to resume.
3529 void snd_hda_codec_resume_cache(struct hda_codec *codec)
3533 mutex_lock(&codec->hash_mutex);
3534 codec->cached_write = 0;
3535 for (i = 0; i < codec->cmd_cache.buf.used; i++) {
3536 struct hda_cache_head *buffer;
3539 buffer = snd_array_elem(&codec->cmd_cache.buf, i);
3546 mutex_unlock(&codec->hash_mutex);
3547 snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
3548 get_cmd_cache_cmd(key), buffer->val);
3549 mutex_lock(&codec->hash_mutex);
3551 mutex_unlock(&codec->hash_mutex);
3553 EXPORT_SYMBOL_HDA(snd_hda_codec_resume_cache);
3556 * snd_hda_sequence_write_cache - sequence writes with caching
3557 * @codec: the HDA codec
3558 * @seq: VERB array to send
3560 * Send the commands sequentially from the given array.
3561 * Thte commands are recorded on cache for power-save and resume.
3562 * The array must be terminated with NID=0.
3564 void snd_hda_sequence_write_cache(struct hda_codec *codec,
3565 const struct hda_verb *seq)
3567 for (; seq->nid; seq++)
3568 snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
3571 EXPORT_SYMBOL_HDA(snd_hda_sequence_write_cache);
3573 void snd_hda_codec_set_power_to_all(struct hda_codec *codec, hda_nid_t fg,
3574 unsigned int power_state,
3575 bool eapd_workaround)
3577 hda_nid_t nid = codec->start_nid;
3580 for (i = 0; i < codec->num_nodes; i++, nid++) {
3581 unsigned int wcaps = get_wcaps(codec, nid);
3582 if (!(wcaps & AC_WCAP_POWER))
3584 /* don't power down the widget if it controls eapd and
3585 * EAPD_BTLENABLE is set.
3587 if (eapd_workaround && power_state == AC_PWRST_D3 &&
3588 get_wcaps_type(wcaps) == AC_WID_PIN &&
3589 (snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD)) {
3590 int eapd = snd_hda_codec_read(codec, nid, 0,
3591 AC_VERB_GET_EAPD_BTLENABLE, 0);
3595 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE,
3599 EXPORT_SYMBOL_HDA(snd_hda_codec_set_power_to_all);
3602 * supported power states check
3604 static bool snd_hda_codec_get_supported_ps(struct hda_codec *codec, hda_nid_t fg,
3605 unsigned int power_state)
3607 int sup = snd_hda_param_read(codec, fg, AC_PAR_POWER_STATE);
3611 if (sup & power_state)
3618 * wait until the state is reached, returns the current state
3620 static unsigned int hda_sync_power_state(struct hda_codec *codec,
3622 unsigned int power_state)
3624 unsigned long end_time = jiffies + msecs_to_jiffies(500);
3625 unsigned int state, actual_state;
3628 state = snd_hda_codec_read(codec, fg, 0,
3629 AC_VERB_GET_POWER_STATE, 0);
3630 if (state & AC_PWRST_ERROR)
3632 actual_state = (state >> 4) & 0x0f;
3633 if (actual_state == power_state)
3635 if (time_after_eq(jiffies, end_time))
3637 /* wait until the codec reachs to the target state */
3644 * set power state of the codec, and return the power state
3646 static unsigned int hda_set_power_state(struct hda_codec *codec,
3647 unsigned int power_state)
3649 hda_nid_t fg = codec->afg ? codec->afg : codec->mfg;
3653 /* this delay seems necessary to avoid click noise at power-down */
3654 if (power_state == AC_PWRST_D3) {
3655 /* transition time less than 10ms for power down */
3656 msleep(codec->epss ? 10 : 100);
3659 /* repeat power states setting at most 10 times*/
3660 for (count = 0; count < 10; count++) {
3661 if (codec->patch_ops.set_power_state)
3662 codec->patch_ops.set_power_state(codec, fg,
3665 snd_hda_codec_read(codec, fg, 0,
3666 AC_VERB_SET_POWER_STATE,
3668 snd_hda_codec_set_power_to_all(codec, fg, power_state,
3671 state = hda_sync_power_state(codec, fg, power_state);
3672 if (!(state & AC_PWRST_ERROR))
3679 #ifdef CONFIG_SND_HDA_HWDEP
3680 /* execute additional init verbs */
3681 static void hda_exec_init_verbs(struct hda_codec *codec)
3683 if (codec->init_verbs.list)
3684 snd_hda_sequence_write(codec, codec->init_verbs.list);
3687 static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
3692 * call suspend and power-down; used both from PM and power-save
3693 * this function returns the power state in the end
3695 static unsigned int hda_call_codec_suspend(struct hda_codec *codec, bool in_wq)
3701 if (codec->patch_ops.suspend)
3702 codec->patch_ops.suspend(codec);
3703 hda_cleanup_all_streams(codec);
3704 state = hda_set_power_state(codec, AC_PWRST_D3);
3705 /* Cancel delayed work if we aren't currently running from it. */
3707 cancel_delayed_work_sync(&codec->power_work);
3708 spin_lock(&codec->power_lock);
3709 snd_hda_update_power_acct(codec);
3710 trace_hda_power_down(codec);
3711 codec->power_on = 0;
3712 codec->power_transition = 0;
3713 codec->power_jiffies = jiffies;
3714 spin_unlock(&codec->power_lock);
3719 /* mark all entries of cmd and amp caches dirty */
3720 static void hda_mark_cmd_cache_dirty(struct hda_codec *codec)
3723 for (i = 0; i < codec->cmd_cache.buf.used; i++) {
3724 struct hda_cache_head *cmd;
3725 cmd = snd_array_elem(&codec->cmd_cache.buf, i);
3728 for (i = 0; i < codec->amp_cache.buf.used; i++) {
3729 struct hda_amp_info *amp;
3730 amp = snd_array_elem(&codec->cmd_cache.buf, i);
3731 amp->head.dirty = 1;
3736 * kick up codec; used both from PM and power-save
3738 static void hda_call_codec_resume(struct hda_codec *codec)
3742 hda_mark_cmd_cache_dirty(codec);
3744 /* set as if powered on for avoiding re-entering the resume
3745 * in the resume / power-save sequence
3747 hda_keep_power_on(codec);
3748 hda_set_power_state(codec, AC_PWRST_D0);
3749 restore_shutup_pins(codec);
3750 hda_exec_init_verbs(codec);
3751 if (codec->patch_ops.resume)
3752 codec->patch_ops.resume(codec);
3754 if (codec->patch_ops.init)
3755 codec->patch_ops.init(codec);
3756 snd_hda_codec_resume_amp(codec);
3757 snd_hda_codec_resume_cache(codec);
3760 if (codec->jackpoll_interval)
3761 hda_jackpoll_work(&codec->jackpoll_work.work);
3763 snd_hda_jack_set_dirty_all(codec);
3764 snd_hda_jack_report_sync(codec);
3768 snd_hda_power_down(codec); /* flag down before returning */
3770 #endif /* CONFIG_PM */
3774 * snd_hda_build_controls - build mixer controls
3777 * Creates mixer controls for each codec included in the bus.
3779 * Returns 0 if successful, otherwise a negative error code.
3781 int snd_hda_build_controls(struct hda_bus *bus)
3783 struct hda_codec *codec;
3785 list_for_each_entry(codec, &bus->codec_list, list) {
3786 int err = snd_hda_codec_build_controls(codec);
3788 printk(KERN_ERR "hda_codec: cannot build controls "
3789 "for #%d (error %d)\n", codec->addr, err);
3790 err = snd_hda_codec_reset(codec);
3793 "hda_codec: cannot revert codec\n");
3800 EXPORT_SYMBOL_HDA(snd_hda_build_controls);
3803 * add standard channel maps if not specified
3805 static int add_std_chmaps(struct hda_codec *codec)
3809 for (i = 0; i < codec->num_pcms; i++) {
3810 for (str = 0; str < 2; str++) {
3811 struct snd_pcm *pcm = codec->pcm_info[i].pcm;
3812 struct hda_pcm_stream *hinfo =
3813 &codec->pcm_info[i].stream[str];
3814 struct snd_pcm_chmap *chmap;
3815 const struct snd_pcm_chmap_elem *elem;
3817 if (codec->pcm_info[i].own_chmap)
3819 if (!pcm || !hinfo->substreams)
3821 elem = hinfo->chmap ? hinfo->chmap : snd_pcm_std_chmaps;
3822 err = snd_pcm_add_chmap_ctls(pcm, str, elem,
3823 hinfo->channels_max,
3827 chmap->channel_mask = SND_PCM_CHMAP_MASK_2468;
3833 /* default channel maps for 2.1 speakers;
3834 * since HD-audio supports only stereo, odd number channels are omitted
3836 const struct snd_pcm_chmap_elem snd_pcm_2_1_chmaps[] = {
3838 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } },
3840 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
3841 SNDRV_CHMAP_LFE, SNDRV_CHMAP_LFE } },
3844 EXPORT_SYMBOL_GPL(snd_pcm_2_1_chmaps);
3846 int snd_hda_codec_build_controls(struct hda_codec *codec)
3849 hda_exec_init_verbs(codec);
3850 /* continue to initialize... */
3851 if (codec->patch_ops.init)
3852 err = codec->patch_ops.init(codec);
3853 if (!err && codec->patch_ops.build_controls)
3854 err = codec->patch_ops.build_controls(codec);
3858 /* we create chmaps here instead of build_pcms */
3859 err = add_std_chmaps(codec);
3863 if (codec->jackpoll_interval)
3864 hda_jackpoll_work(&codec->jackpoll_work.work);
3866 snd_hda_jack_report_sync(codec); /* call at the last init point */
3873 struct hda_rate_tbl {
3875 unsigned int alsa_bits;
3876 unsigned int hda_fmt;
3879 /* rate = base * mult / div */
3880 #define HDA_RATE(base, mult, div) \
3881 (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \
3882 (((div) - 1) << AC_FMT_DIV_SHIFT))
3884 static struct hda_rate_tbl rate_bits[] = {
3885 /* rate in Hz, ALSA rate bitmask, HDA format value */
3887 /* autodetected value used in snd_hda_query_supported_pcm */
3888 { 8000, SNDRV_PCM_RATE_8000, HDA_RATE(48, 1, 6) },
3889 { 11025, SNDRV_PCM_RATE_11025, HDA_RATE(44, 1, 4) },
3890 { 16000, SNDRV_PCM_RATE_16000, HDA_RATE(48, 1, 3) },
3891 { 22050, SNDRV_PCM_RATE_22050, HDA_RATE(44, 1, 2) },
3892 { 32000, SNDRV_PCM_RATE_32000, HDA_RATE(48, 2, 3) },
3893 { 44100, SNDRV_PCM_RATE_44100, HDA_RATE(44, 1, 1) },
3894 { 48000, SNDRV_PCM_RATE_48000, HDA_RATE(48, 1, 1) },
3895 { 88200, SNDRV_PCM_RATE_88200, HDA_RATE(44, 2, 1) },
3896 { 96000, SNDRV_PCM_RATE_96000, HDA_RATE(48, 2, 1) },
3897 { 176400, SNDRV_PCM_RATE_176400, HDA_RATE(44, 4, 1) },
3898 { 192000, SNDRV_PCM_RATE_192000, HDA_RATE(48, 4, 1) },
3899 #define AC_PAR_PCM_RATE_BITS 11
3900 /* up to bits 10, 384kHZ isn't supported properly */
3902 /* not autodetected value */
3903 { 9600, SNDRV_PCM_RATE_KNOT, HDA_RATE(48, 1, 5) },
3905 { 0 } /* terminator */
3909 * snd_hda_calc_stream_format - calculate format bitset
3910 * @rate: the sample rate
3911 * @channels: the number of channels
3912 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
3913 * @maxbps: the max. bps
3915 * Calculate the format bitset from the given rate, channels and th PCM format.
3917 * Return zero if invalid.
3919 unsigned int snd_hda_calc_stream_format(unsigned int rate,
3920 unsigned int channels,
3921 unsigned int format,
3922 unsigned int maxbps,
3923 unsigned short spdif_ctls)
3926 unsigned int val = 0;
3928 for (i = 0; rate_bits[i].hz; i++)
3929 if (rate_bits[i].hz == rate) {
3930 val = rate_bits[i].hda_fmt;
3933 if (!rate_bits[i].hz) {
3934 snd_printdd("invalid rate %d\n", rate);
3938 if (channels == 0 || channels > 8) {
3939 snd_printdd("invalid channels %d\n", channels);
3942 val |= channels - 1;
3944 switch (snd_pcm_format_width(format)) {
3946 val |= AC_FMT_BITS_8;
3949 val |= AC_FMT_BITS_16;
3954 if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE)
3955 val |= AC_FMT_BITS_32;
3956 else if (maxbps >= 24)
3957 val |= AC_FMT_BITS_24;
3959 val |= AC_FMT_BITS_20;
3962 snd_printdd("invalid format width %d\n",
3963 snd_pcm_format_width(format));
3967 if (spdif_ctls & AC_DIG1_NONAUDIO)
3968 val |= AC_FMT_TYPE_NON_PCM;
3972 EXPORT_SYMBOL_HDA(snd_hda_calc_stream_format);
3974 static unsigned int get_pcm_param(struct hda_codec *codec, hda_nid_t nid,
3977 unsigned int val = 0;
3978 if (nid != codec->afg &&
3979 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD))
3980 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
3981 if (!val || val == -1)
3982 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
3983 if (!val || val == -1)
3988 static unsigned int query_pcm_param(struct hda_codec *codec, hda_nid_t nid)
3990 return query_caps_hash(codec, nid, 0, HDA_HASH_PARPCM_KEY(nid),
3994 static unsigned int get_stream_param(struct hda_codec *codec, hda_nid_t nid,
3997 unsigned int streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
3998 if (!streams || streams == -1)
3999 streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
4000 if (!streams || streams == -1)
4005 static unsigned int query_stream_param(struct hda_codec *codec, hda_nid_t nid)
4007 return query_caps_hash(codec, nid, 0, HDA_HASH_PARSTR_KEY(nid),
4012 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
4013 * @codec: the HDA codec
4014 * @nid: NID to query
4015 * @ratesp: the pointer to store the detected rate bitflags
4016 * @formatsp: the pointer to store the detected formats
4017 * @bpsp: the pointer to store the detected format widths
4019 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
4020 * or @bsps argument is ignored.
4022 * Returns 0 if successful, otherwise a negative error code.
4024 int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
4025 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
4027 unsigned int i, val, wcaps;
4029 wcaps = get_wcaps(codec, nid);
4030 val = query_pcm_param(codec, nid);
4034 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
4036 rates |= rate_bits[i].alsa_bits;
4039 snd_printk(KERN_ERR "hda_codec: rates == 0 "
4040 "(nid=0x%x, val=0x%x, ovrd=%i)\n",
4042 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0);
4048 if (formatsp || bpsp) {
4050 unsigned int streams, bps;
4052 streams = query_stream_param(codec, nid);
4057 if (streams & AC_SUPFMT_PCM) {
4058 if (val & AC_SUPPCM_BITS_8) {
4059 formats |= SNDRV_PCM_FMTBIT_U8;
4062 if (val & AC_SUPPCM_BITS_16) {
4063 formats |= SNDRV_PCM_FMTBIT_S16_LE;
4066 if (wcaps & AC_WCAP_DIGITAL) {
4067 if (val & AC_SUPPCM_BITS_32)
4068 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
4069 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
4070 formats |= SNDRV_PCM_FMTBIT_S32_LE;
4071 if (val & AC_SUPPCM_BITS_24)
4073 else if (val & AC_SUPPCM_BITS_20)
4075 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
4076 AC_SUPPCM_BITS_32)) {
4077 formats |= SNDRV_PCM_FMTBIT_S32_LE;
4078 if (val & AC_SUPPCM_BITS_32)
4080 else if (val & AC_SUPPCM_BITS_24)
4082 else if (val & AC_SUPPCM_BITS_20)
4086 #if 0 /* FIXME: CS4206 doesn't work, which is the only codec supporting float */
4087 if (streams & AC_SUPFMT_FLOAT32) {
4088 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
4093 if (streams == AC_SUPFMT_AC3) {
4094 /* should be exclusive */
4095 /* temporary hack: we have still no proper support
4096 * for the direct AC3 stream...
4098 formats |= SNDRV_PCM_FMTBIT_U8;
4102 snd_printk(KERN_ERR "hda_codec: formats == 0 "
4103 "(nid=0x%x, val=0x%x, ovrd=%i, "
4106 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0,
4111 *formatsp = formats;
4118 EXPORT_SYMBOL_HDA(snd_hda_query_supported_pcm);
4121 * snd_hda_is_supported_format - Check the validity of the format
4122 * @codec: HD-audio codec
4123 * @nid: NID to check
4124 * @format: the HD-audio format value to check
4126 * Check whether the given node supports the format value.
4128 * Returns 1 if supported, 0 if not.
4130 int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
4131 unsigned int format)
4134 unsigned int val = 0, rate, stream;
4136 val = query_pcm_param(codec, nid);
4140 rate = format & 0xff00;
4141 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
4142 if (rate_bits[i].hda_fmt == rate) {
4147 if (i >= AC_PAR_PCM_RATE_BITS)
4150 stream = query_stream_param(codec, nid);
4154 if (stream & AC_SUPFMT_PCM) {
4155 switch (format & 0xf0) {
4157 if (!(val & AC_SUPPCM_BITS_8))
4161 if (!(val & AC_SUPPCM_BITS_16))
4165 if (!(val & AC_SUPPCM_BITS_20))
4169 if (!(val & AC_SUPPCM_BITS_24))
4173 if (!(val & AC_SUPPCM_BITS_32))
4180 /* FIXME: check for float32 and AC3? */
4185 EXPORT_SYMBOL_HDA(snd_hda_is_supported_format);
4190 static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
4191 struct hda_codec *codec,
4192 struct snd_pcm_substream *substream)
4197 static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
4198 struct hda_codec *codec,
4199 unsigned int stream_tag,
4200 unsigned int format,
4201 struct snd_pcm_substream *substream)
4203 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4207 static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
4208 struct hda_codec *codec,
4209 struct snd_pcm_substream *substream)
4211 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
4215 static int set_pcm_default_values(struct hda_codec *codec,
4216 struct hda_pcm_stream *info)
4220 /* query support PCM information from the given NID */
4221 if (info->nid && (!info->rates || !info->formats)) {
4222 err = snd_hda_query_supported_pcm(codec, info->nid,
4223 info->rates ? NULL : &info->rates,
4224 info->formats ? NULL : &info->formats,
4225 info->maxbps ? NULL : &info->maxbps);
4229 if (info->ops.open == NULL)
4230 info->ops.open = hda_pcm_default_open_close;
4231 if (info->ops.close == NULL)
4232 info->ops.close = hda_pcm_default_open_close;
4233 if (info->ops.prepare == NULL) {
4234 if (snd_BUG_ON(!info->nid))
4236 info->ops.prepare = hda_pcm_default_prepare;
4238 if (info->ops.cleanup == NULL) {
4239 if (snd_BUG_ON(!info->nid))
4241 info->ops.cleanup = hda_pcm_default_cleanup;
4247 * codec prepare/cleanup entries
4249 int snd_hda_codec_prepare(struct hda_codec *codec,
4250 struct hda_pcm_stream *hinfo,
4251 unsigned int stream,
4252 unsigned int format,
4253 struct snd_pcm_substream *substream)
4256 mutex_lock(&codec->bus->prepare_mutex);
4257 ret = hinfo->ops.prepare(hinfo, codec, stream, format, substream);
4259 purify_inactive_streams(codec);
4260 mutex_unlock(&codec->bus->prepare_mutex);
4263 EXPORT_SYMBOL_HDA(snd_hda_codec_prepare);
4265 void snd_hda_codec_cleanup(struct hda_codec *codec,
4266 struct hda_pcm_stream *hinfo,
4267 struct snd_pcm_substream *substream)
4269 mutex_lock(&codec->bus->prepare_mutex);
4270 hinfo->ops.cleanup(hinfo, codec, substream);
4271 mutex_unlock(&codec->bus->prepare_mutex);
4273 EXPORT_SYMBOL_HDA(snd_hda_codec_cleanup);
4276 const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = {
4277 "Audio", "SPDIF", "HDMI", "Modem"
4281 * get the empty PCM device number to assign
4283 * note the max device number is limited by HDA_MAX_PCMS, currently 10
4285 static int get_empty_pcm_device(struct hda_bus *bus, int type)
4287 /* audio device indices; not linear to keep compatibility */
4288 static int audio_idx[HDA_PCM_NTYPES][5] = {
4289 [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 },
4290 [HDA_PCM_TYPE_SPDIF] = { 1, -1 },
4291 [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 },
4292 [HDA_PCM_TYPE_MODEM] = { 6, -1 },
4296 if (type >= HDA_PCM_NTYPES) {
4297 snd_printk(KERN_WARNING "Invalid PCM type %d\n", type);
4301 for (i = 0; audio_idx[type][i] >= 0 ; i++)
4302 if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits))
4303 return audio_idx[type][i];
4305 /* non-fixed slots starting from 10 */
4306 for (i = 10; i < 32; i++) {
4307 if (!test_and_set_bit(i, bus->pcm_dev_bits))
4311 snd_printk(KERN_WARNING "Too many %s devices\n",
4312 snd_hda_pcm_type_name[type]);
4317 * attach a new PCM stream
4319 static int snd_hda_attach_pcm(struct hda_codec *codec, struct hda_pcm *pcm)
4321 struct hda_bus *bus = codec->bus;
4322 struct hda_pcm_stream *info;
4325 if (snd_BUG_ON(!pcm->name))
4327 for (stream = 0; stream < 2; stream++) {
4328 info = &pcm->stream[stream];
4329 if (info->substreams) {
4330 err = set_pcm_default_values(codec, info);
4335 return bus->ops.attach_pcm(bus, codec, pcm);
4338 /* assign all PCMs of the given codec */
4339 int snd_hda_codec_build_pcms(struct hda_codec *codec)
4344 if (!codec->num_pcms) {
4345 if (!codec->patch_ops.build_pcms)
4347 err = codec->patch_ops.build_pcms(codec);
4349 printk(KERN_ERR "hda_codec: cannot build PCMs"
4350 "for #%d (error %d)\n", codec->addr, err);
4351 err = snd_hda_codec_reset(codec);
4354 "hda_codec: cannot revert codec\n");
4359 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
4360 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
4363 if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
4364 continue; /* no substreams assigned */
4367 dev = get_empty_pcm_device(codec->bus, cpcm->pcm_type);
4369 continue; /* no fatal error */
4371 err = snd_hda_attach_pcm(codec, cpcm);
4373 printk(KERN_ERR "hda_codec: cannot attach "
4374 "PCM stream %d for codec #%d\n",
4376 continue; /* no fatal error */
4384 * snd_hda_build_pcms - build PCM information
4387 * Create PCM information for each codec included in the bus.
4389 * The build_pcms codec patch is requested to set up codec->num_pcms and
4390 * codec->pcm_info properly. The array is referred by the top-level driver
4391 * to create its PCM instances.
4392 * The allocated codec->pcm_info should be released in codec->patch_ops.free
4395 * At least, substreams, channels_min and channels_max must be filled for
4396 * each stream. substreams = 0 indicates that the stream doesn't exist.
4397 * When rates and/or formats are zero, the supported values are queried
4398 * from the given nid. The nid is used also by the default ops.prepare
4399 * and ops.cleanup callbacks.
4401 * The driver needs to call ops.open in its open callback. Similarly,
4402 * ops.close is supposed to be called in the close callback.
4403 * ops.prepare should be called in the prepare or hw_params callback
4404 * with the proper parameters for set up.
4405 * ops.cleanup should be called in hw_free for clean up of streams.
4407 * This function returns 0 if successful, or a negative error code.
4409 int snd_hda_build_pcms(struct hda_bus *bus)
4411 struct hda_codec *codec;
4413 list_for_each_entry(codec, &bus->codec_list, list) {
4414 int err = snd_hda_codec_build_pcms(codec);
4420 EXPORT_SYMBOL_HDA(snd_hda_build_pcms);
4423 * snd_hda_check_board_config - compare the current codec with the config table
4424 * @codec: the HDA codec
4425 * @num_configs: number of config enums
4426 * @models: array of model name strings
4427 * @tbl: configuration table, terminated by null entries
4429 * Compares the modelname or PCI subsystem id of the current codec with the
4430 * given configuration table. If a matching entry is found, returns its
4431 * config value (supposed to be 0 or positive).
4433 * If no entries are matching, the function returns a negative value.
4435 int snd_hda_check_board_config(struct hda_codec *codec,
4436 int num_configs, const char * const *models,
4437 const struct snd_pci_quirk *tbl)
4439 if (codec->modelname && models) {
4441 for (i = 0; i < num_configs; i++) {
4443 !strcmp(codec->modelname, models[i])) {
4444 snd_printd(KERN_INFO "hda_codec: model '%s' is "
4445 "selected\n", models[i]);
4451 if (!codec->bus->pci || !tbl)
4454 tbl = snd_pci_quirk_lookup(codec->bus->pci, tbl);
4457 if (tbl->value >= 0 && tbl->value < num_configs) {
4458 #ifdef CONFIG_SND_DEBUG_VERBOSE
4460 const char *model = NULL;
4462 model = models[tbl->value];
4464 sprintf(tmp, "#%d", tbl->value);
4467 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
4468 "for config %x:%x (%s)\n",
4469 model, tbl->subvendor, tbl->subdevice,
4470 (tbl->name ? tbl->name : "Unknown device"));
4476 EXPORT_SYMBOL_HDA(snd_hda_check_board_config);
4479 * snd_hda_check_board_codec_sid_config - compare the current codec
4480 subsystem ID with the
4483 This is important for Gateway notebooks with SB450 HDA Audio
4484 where the vendor ID of the PCI device is:
4485 ATI Technologies Inc SB450 HDA Audio [1002:437b]
4486 and the vendor/subvendor are found only at the codec.
4488 * @codec: the HDA codec
4489 * @num_configs: number of config enums
4490 * @models: array of model name strings
4491 * @tbl: configuration table, terminated by null entries
4493 * Compares the modelname or PCI subsystem id of the current codec with the
4494 * given configuration table. If a matching entry is found, returns its
4495 * config value (supposed to be 0 or positive).
4497 * If no entries are matching, the function returns a negative value.
4499 int snd_hda_check_board_codec_sid_config(struct hda_codec *codec,
4500 int num_configs, const char * const *models,
4501 const struct snd_pci_quirk *tbl)
4503 const struct snd_pci_quirk *q;
4505 /* Search for codec ID */
4506 for (q = tbl; q->subvendor; q++) {
4507 unsigned int mask = 0xffff0000 | q->subdevice_mask;
4508 unsigned int id = (q->subdevice | (q->subvendor << 16)) & mask;
4509 if ((codec->subsystem_id & mask) == id)
4518 if (tbl->value >= 0 && tbl->value < num_configs) {
4519 #ifdef CONFIG_SND_DEBUG_VERBOSE
4521 const char *model = NULL;
4523 model = models[tbl->value];
4525 sprintf(tmp, "#%d", tbl->value);
4528 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
4529 "for config %x:%x (%s)\n",
4530 model, tbl->subvendor, tbl->subdevice,
4531 (tbl->name ? tbl->name : "Unknown device"));
4537 EXPORT_SYMBOL_HDA(snd_hda_check_board_codec_sid_config);
4540 * snd_hda_add_new_ctls - create controls from the array
4541 * @codec: the HDA codec
4542 * @knew: the array of struct snd_kcontrol_new
4544 * This helper function creates and add new controls in the given array.
4545 * The array must be terminated with an empty entry as terminator.
4547 * Returns 0 if successful, or a negative error code.
4549 int snd_hda_add_new_ctls(struct hda_codec *codec,
4550 const struct snd_kcontrol_new *knew)
4554 for (; knew->name; knew++) {
4555 struct snd_kcontrol *kctl;
4556 int addr = 0, idx = 0;
4557 if (knew->iface == -1) /* skip this codec private value */
4560 kctl = snd_ctl_new1(knew, codec);
4564 kctl->id.device = addr;
4566 kctl->id.index = idx;
4567 err = snd_hda_ctl_add(codec, 0, kctl);
4570 /* try first with another device index corresponding to
4571 * the codec addr; if it still fails (or it's the
4572 * primary codec), then try another control index
4574 if (!addr && codec->addr)
4576 else if (!idx && !knew->index) {
4577 idx = find_empty_mixer_ctl_idx(codec,
4587 EXPORT_SYMBOL_HDA(snd_hda_add_new_ctls);
4590 static void hda_power_work(struct work_struct *work)
4592 struct hda_codec *codec =
4593 container_of(work, struct hda_codec, power_work.work);
4594 struct hda_bus *bus = codec->bus;
4597 spin_lock(&codec->power_lock);
4598 if (codec->power_transition > 0) { /* during power-up sequence? */
4599 spin_unlock(&codec->power_lock);
4602 if (!codec->power_on || codec->power_count) {
4603 codec->power_transition = 0;
4604 spin_unlock(&codec->power_lock);
4607 spin_unlock(&codec->power_lock);
4609 state = hda_call_codec_suspend(codec, true);
4610 codec->pm_down_notified = 0;
4611 if (!bus->power_keep_link_on && (state & AC_PWRST_CLK_STOP_OK)) {
4612 codec->pm_down_notified = 1;
4613 hda_call_pm_notify(bus, false);
4617 static void hda_keep_power_on(struct hda_codec *codec)
4619 spin_lock(&codec->power_lock);
4620 codec->power_count++;
4621 codec->power_on = 1;
4622 codec->power_jiffies = jiffies;
4623 spin_unlock(&codec->power_lock);
4626 /* update the power on/off account with the current jiffies */
4627 void snd_hda_update_power_acct(struct hda_codec *codec)
4629 unsigned long delta = jiffies - codec->power_jiffies;
4630 if (codec->power_on)
4631 codec->power_on_acct += delta;
4633 codec->power_off_acct += delta;
4634 codec->power_jiffies += delta;
4637 /* Transition to powered up, if wait_power_down then wait for a pending
4638 * transition to D3 to complete. A pending D3 transition is indicated
4639 * with power_transition == -1. */
4640 /* call this with codec->power_lock held! */
4641 static void __snd_hda_power_up(struct hda_codec *codec, bool wait_power_down)
4643 struct hda_bus *bus = codec->bus;
4645 /* Return if power_on or transitioning to power_on, unless currently
4647 if ((codec->power_on || codec->power_transition > 0) &&
4648 !(wait_power_down && codec->power_transition < 0))
4650 spin_unlock(&codec->power_lock);
4652 cancel_delayed_work_sync(&codec->power_work);
4654 spin_lock(&codec->power_lock);
4655 /* If the power down delayed work was cancelled above before starting,
4656 * then there is no need to go through power up here.
4658 if (codec->power_on) {
4659 if (codec->power_transition < 0)
4660 codec->power_transition = 0;
4664 trace_hda_power_up(codec);
4665 snd_hda_update_power_acct(codec);
4666 codec->power_on = 1;
4667 codec->power_jiffies = jiffies;
4668 codec->power_transition = 1; /* avoid reentrance */
4669 spin_unlock(&codec->power_lock);
4671 if (codec->pm_down_notified) {
4672 codec->pm_down_notified = 0;
4673 hda_call_pm_notify(bus, true);
4676 hda_call_codec_resume(codec);
4678 spin_lock(&codec->power_lock);
4679 codec->power_transition = 0;
4682 #define power_save(codec) \
4683 ((codec)->bus->power_save ? *(codec)->bus->power_save : 0)
4685 /* Transition to powered down */
4686 static void __snd_hda_power_down(struct hda_codec *codec)
4688 if (!codec->power_on || codec->power_count || codec->power_transition)
4691 if (power_save(codec)) {
4692 codec->power_transition = -1; /* avoid reentrance */
4693 queue_delayed_work(codec->bus->workq, &codec->power_work,
4694 msecs_to_jiffies(power_save(codec) * 1000));
4699 * snd_hda_power_save - Power-up/down/sync the codec
4700 * @codec: HD-audio codec
4701 * @delta: the counter delta to change
4703 * Change the power-up counter via @delta, and power up or down the hardware
4704 * appropriately. For the power-down, queue to the delayed action.
4705 * Passing zero to @delta means to synchronize the power state.
4707 void snd_hda_power_save(struct hda_codec *codec, int delta, bool d3wait)
4709 spin_lock(&codec->power_lock);
4710 codec->power_count += delta;
4711 trace_hda_power_count(codec);
4713 __snd_hda_power_up(codec, d3wait);
4715 __snd_hda_power_down(codec);
4716 spin_unlock(&codec->power_lock);
4718 EXPORT_SYMBOL_HDA(snd_hda_power_save);
4721 * snd_hda_check_amp_list_power - Check the amp list and update the power
4722 * @codec: HD-audio codec
4723 * @check: the object containing an AMP list and the status
4724 * @nid: NID to check / update
4726 * Check whether the given NID is in the amp list. If it's in the list,
4727 * check the current AMP status, and update the the power-status according
4728 * to the mute status.
4730 * This function is supposed to be set or called from the check_power_status
4733 int snd_hda_check_amp_list_power(struct hda_codec *codec,
4734 struct hda_loopback_check *check,
4737 const struct hda_amp_list *p;
4740 if (!check->amplist)
4742 for (p = check->amplist; p->nid; p++) {
4747 return 0; /* nothing changed */
4749 for (p = check->amplist; p->nid; p++) {
4750 for (ch = 0; ch < 2; ch++) {
4751 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
4753 if (!(v & HDA_AMP_MUTE) && v > 0) {
4754 if (!check->power_on) {
4755 check->power_on = 1;
4756 snd_hda_power_up(codec);
4762 if (check->power_on) {
4763 check->power_on = 0;
4764 snd_hda_power_down(codec);
4768 EXPORT_SYMBOL_HDA(snd_hda_check_amp_list_power);
4772 * Channel mode helper
4776 * snd_hda_ch_mode_info - Info callback helper for the channel mode enum
4778 int snd_hda_ch_mode_info(struct hda_codec *codec,
4779 struct snd_ctl_elem_info *uinfo,
4780 const struct hda_channel_mode *chmode,
4783 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4785 uinfo->value.enumerated.items = num_chmodes;
4786 if (uinfo->value.enumerated.item >= num_chmodes)
4787 uinfo->value.enumerated.item = num_chmodes - 1;
4788 sprintf(uinfo->value.enumerated.name, "%dch",
4789 chmode[uinfo->value.enumerated.item].channels);
4792 EXPORT_SYMBOL_HDA(snd_hda_ch_mode_info);
4795 * snd_hda_ch_mode_get - Get callback helper for the channel mode enum
4797 int snd_hda_ch_mode_get(struct hda_codec *codec,
4798 struct snd_ctl_elem_value *ucontrol,
4799 const struct hda_channel_mode *chmode,
4805 for (i = 0; i < num_chmodes; i++) {
4806 if (max_channels == chmode[i].channels) {
4807 ucontrol->value.enumerated.item[0] = i;
4813 EXPORT_SYMBOL_HDA(snd_hda_ch_mode_get);
4816 * snd_hda_ch_mode_put - Put callback helper for the channel mode enum
4818 int snd_hda_ch_mode_put(struct hda_codec *codec,
4819 struct snd_ctl_elem_value *ucontrol,
4820 const struct hda_channel_mode *chmode,
4826 mode = ucontrol->value.enumerated.item[0];
4827 if (mode >= num_chmodes)
4829 if (*max_channelsp == chmode[mode].channels)
4831 /* change the current channel setting */
4832 *max_channelsp = chmode[mode].channels;
4833 if (chmode[mode].sequence)
4834 snd_hda_sequence_write_cache(codec, chmode[mode].sequence);
4837 EXPORT_SYMBOL_HDA(snd_hda_ch_mode_put);
4844 * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum
4846 int snd_hda_input_mux_info(const struct hda_input_mux *imux,
4847 struct snd_ctl_elem_info *uinfo)
4851 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4853 uinfo->value.enumerated.items = imux->num_items;
4854 if (!imux->num_items)
4856 index = uinfo->value.enumerated.item;
4857 if (index >= imux->num_items)
4858 index = imux->num_items - 1;
4859 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
4862 EXPORT_SYMBOL_HDA(snd_hda_input_mux_info);
4865 * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum
4867 int snd_hda_input_mux_put(struct hda_codec *codec,
4868 const struct hda_input_mux *imux,
4869 struct snd_ctl_elem_value *ucontrol,
4871 unsigned int *cur_val)
4875 if (!imux->num_items)
4877 idx = ucontrol->value.enumerated.item[0];
4878 if (idx >= imux->num_items)
4879 idx = imux->num_items - 1;
4880 if (*cur_val == idx)
4882 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
4883 imux->items[idx].index);
4887 EXPORT_SYMBOL_HDA(snd_hda_input_mux_put);
4891 * process kcontrol info callback of a simple string enum array
4892 * when @num_items is 0 or @texts is NULL, assume a boolean enum array
4894 int snd_hda_enum_helper_info(struct snd_kcontrol *kcontrol,
4895 struct snd_ctl_elem_info *uinfo,
4896 int num_items, const char * const *texts)
4898 static const char * const texts_default[] = {
4899 "Disabled", "Enabled"
4902 if (!texts || !num_items) {
4904 texts = texts_default;
4907 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4909 uinfo->value.enumerated.items = num_items;
4910 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
4911 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
4912 strcpy(uinfo->value.enumerated.name,
4913 texts[uinfo->value.enumerated.item]);
4916 EXPORT_SYMBOL_HDA(snd_hda_enum_helper_info);
4919 * Multi-channel / digital-out PCM helper functions
4922 /* setup SPDIF output stream */
4923 static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
4924 unsigned int stream_tag, unsigned int format)
4926 struct hda_spdif_out *spdif;
4927 unsigned int curr_fmt;
4930 spdif = snd_hda_spdif_out_of_nid(codec, nid);
4931 curr_fmt = snd_hda_codec_read(codec, nid, 0,
4932 AC_VERB_GET_STREAM_FORMAT, 0);
4933 reset = codec->spdif_status_reset &&
4934 (spdif->ctls & AC_DIG1_ENABLE) &&
4937 /* turn off SPDIF if needed; otherwise the IEC958 bits won't be
4940 set_dig_out_convert(codec, nid,
4941 spdif->ctls & ~AC_DIG1_ENABLE & 0xff,
4943 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
4944 if (codec->slave_dig_outs) {
4946 for (d = codec->slave_dig_outs; *d; d++)
4947 snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
4950 /* turn on again (if needed) */
4952 set_dig_out_convert(codec, nid,
4953 spdif->ctls & 0xff, -1);
4956 static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
4958 snd_hda_codec_cleanup_stream(codec, nid);
4959 if (codec->slave_dig_outs) {
4961 for (d = codec->slave_dig_outs; *d; d++)
4962 snd_hda_codec_cleanup_stream(codec, *d);
4967 * snd_hda_bus_reboot_notify - call the reboot notifier of each codec
4968 * @bus: HD-audio bus
4970 void snd_hda_bus_reboot_notify(struct hda_bus *bus)
4972 struct hda_codec *codec;
4976 list_for_each_entry(codec, &bus->codec_list, list) {
4977 if (hda_codec_is_power_on(codec) &&
4978 codec->patch_ops.reboot_notify)
4979 codec->patch_ops.reboot_notify(codec);
4982 EXPORT_SYMBOL_HDA(snd_hda_bus_reboot_notify);
4985 * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode
4987 int snd_hda_multi_out_dig_open(struct hda_codec *codec,
4988 struct hda_multi_out *mout)
4990 mutex_lock(&codec->spdif_mutex);
4991 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
4992 /* already opened as analog dup; reset it once */
4993 cleanup_dig_out_stream(codec, mout->dig_out_nid);
4994 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
4995 mutex_unlock(&codec->spdif_mutex);
4998 EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_open);
5001 * snd_hda_multi_out_dig_prepare - prepare the digital out stream
5003 int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
5004 struct hda_multi_out *mout,
5005 unsigned int stream_tag,
5006 unsigned int format,
5007 struct snd_pcm_substream *substream)
5009 mutex_lock(&codec->spdif_mutex);
5010 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
5011 mutex_unlock(&codec->spdif_mutex);
5014 EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_prepare);
5017 * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream
5019 int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec,
5020 struct hda_multi_out *mout)
5022 mutex_lock(&codec->spdif_mutex);
5023 cleanup_dig_out_stream(codec, mout->dig_out_nid);
5024 mutex_unlock(&codec->spdif_mutex);
5027 EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_cleanup);
5030 * snd_hda_multi_out_dig_close - release the digital out stream
5032 int snd_hda_multi_out_dig_close(struct hda_codec *codec,
5033 struct hda_multi_out *mout)
5035 mutex_lock(&codec->spdif_mutex);
5036 mout->dig_out_used = 0;
5037 mutex_unlock(&codec->spdif_mutex);
5040 EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_close);
5043 * snd_hda_multi_out_analog_open - open analog outputs
5045 * Open analog outputs and set up the hw-constraints.
5046 * If the digital outputs can be opened as slave, open the digital
5049 int snd_hda_multi_out_analog_open(struct hda_codec *codec,
5050 struct hda_multi_out *mout,
5051 struct snd_pcm_substream *substream,
5052 struct hda_pcm_stream *hinfo)
5054 struct snd_pcm_runtime *runtime = substream->runtime;
5055 runtime->hw.channels_max = mout->max_channels;
5056 if (mout->dig_out_nid) {
5057 if (!mout->analog_rates) {
5058 mout->analog_rates = hinfo->rates;
5059 mout->analog_formats = hinfo->formats;
5060 mout->analog_maxbps = hinfo->maxbps;
5062 runtime->hw.rates = mout->analog_rates;
5063 runtime->hw.formats = mout->analog_formats;
5064 hinfo->maxbps = mout->analog_maxbps;
5066 if (!mout->spdif_rates) {
5067 snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
5069 &mout->spdif_formats,
5070 &mout->spdif_maxbps);
5072 mutex_lock(&codec->spdif_mutex);
5073 if (mout->share_spdif) {
5074 if ((runtime->hw.rates & mout->spdif_rates) &&
5075 (runtime->hw.formats & mout->spdif_formats)) {
5076 runtime->hw.rates &= mout->spdif_rates;
5077 runtime->hw.formats &= mout->spdif_formats;
5078 if (mout->spdif_maxbps < hinfo->maxbps)
5079 hinfo->maxbps = mout->spdif_maxbps;
5081 mout->share_spdif = 0;
5082 /* FIXME: need notify? */
5085 mutex_unlock(&codec->spdif_mutex);
5087 return snd_pcm_hw_constraint_step(substream->runtime, 0,
5088 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
5090 EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_open);
5093 * snd_hda_multi_out_analog_prepare - Preapre the analog outputs.
5095 * Set up the i/o for analog out.
5096 * When the digital out is available, copy the front out to digital out, too.
5098 int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
5099 struct hda_multi_out *mout,
5100 unsigned int stream_tag,
5101 unsigned int format,
5102 struct snd_pcm_substream *substream)
5104 const hda_nid_t *nids = mout->dac_nids;
5105 int chs = substream->runtime->channels;
5106 struct hda_spdif_out *spdif;
5109 mutex_lock(&codec->spdif_mutex);
5110 spdif = snd_hda_spdif_out_of_nid(codec, mout->dig_out_nid);
5111 if (mout->dig_out_nid && mout->share_spdif &&
5112 mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
5114 snd_hda_is_supported_format(codec, mout->dig_out_nid,
5116 !(spdif->status & IEC958_AES0_NONAUDIO)) {
5117 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
5118 setup_dig_out_stream(codec, mout->dig_out_nid,
5119 stream_tag, format);
5121 mout->dig_out_used = 0;
5122 cleanup_dig_out_stream(codec, mout->dig_out_nid);
5125 mutex_unlock(&codec->spdif_mutex);
5128 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
5130 if (!mout->no_share_stream &&
5131 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
5132 /* headphone out will just decode front left/right (stereo) */
5133 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
5135 /* extra outputs copied from front */
5136 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
5137 if (!mout->no_share_stream && mout->hp_out_nid[i])
5138 snd_hda_codec_setup_stream(codec,
5139 mout->hp_out_nid[i],
5140 stream_tag, 0, format);
5141 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
5142 if (!mout->no_share_stream && mout->extra_out_nid[i])
5143 snd_hda_codec_setup_stream(codec,
5144 mout->extra_out_nid[i],
5145 stream_tag, 0, format);
5148 for (i = 1; i < mout->num_dacs; i++) {
5149 if (chs >= (i + 1) * 2) /* independent out */
5150 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
5152 else if (!mout->no_share_stream) /* copy front */
5153 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
5158 EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_prepare);
5161 * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out
5163 int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
5164 struct hda_multi_out *mout)
5166 const hda_nid_t *nids = mout->dac_nids;
5169 for (i = 0; i < mout->num_dacs; i++)
5170 snd_hda_codec_cleanup_stream(codec, nids[i]);
5172 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
5173 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
5174 if (mout->hp_out_nid[i])
5175 snd_hda_codec_cleanup_stream(codec,
5176 mout->hp_out_nid[i]);
5177 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
5178 if (mout->extra_out_nid[i])
5179 snd_hda_codec_cleanup_stream(codec,
5180 mout->extra_out_nid[i]);
5181 mutex_lock(&codec->spdif_mutex);
5182 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
5183 cleanup_dig_out_stream(codec, mout->dig_out_nid);
5184 mout->dig_out_used = 0;
5186 mutex_unlock(&codec->spdif_mutex);
5189 EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_cleanup);
5192 * snd_hda_get_default_vref - Get the default (mic) VREF pin bits
5194 * Guess the suitable VREF pin bits to be set as the pin-control value.
5195 * Note: the function doesn't set the AC_PINCTL_IN_EN bit.
5197 unsigned int snd_hda_get_default_vref(struct hda_codec *codec, hda_nid_t pin)
5199 unsigned int pincap;
5200 unsigned int oldval;
5201 oldval = snd_hda_codec_read(codec, pin, 0,
5202 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
5203 pincap = snd_hda_query_pin_caps(codec, pin);
5204 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
5205 /* Exception: if the default pin setup is vref50, we give it priority */
5206 if ((pincap & AC_PINCAP_VREF_80) && oldval != PIN_VREF50)
5207 return AC_PINCTL_VREF_80;
5208 else if (pincap & AC_PINCAP_VREF_50)
5209 return AC_PINCTL_VREF_50;
5210 else if (pincap & AC_PINCAP_VREF_100)
5211 return AC_PINCTL_VREF_100;
5212 else if (pincap & AC_PINCAP_VREF_GRD)
5213 return AC_PINCTL_VREF_GRD;
5214 return AC_PINCTL_VREF_HIZ;
5216 EXPORT_SYMBOL_HDA(snd_hda_get_default_vref);
5218 int _snd_hda_set_pin_ctl(struct hda_codec *codec, hda_nid_t pin,
5219 unsigned int val, bool cached)
5222 unsigned int cap = snd_hda_query_pin_caps(codec, pin);
5223 if (cap && (val & AC_PINCTL_OUT_EN)) {
5224 if (!(cap & AC_PINCAP_OUT))
5225 val &= ~(AC_PINCTL_OUT_EN | AC_PINCTL_HP_EN);
5226 else if ((val & AC_PINCTL_HP_EN) &&
5227 !(cap & AC_PINCAP_HP_DRV))
5228 val &= ~AC_PINCTL_HP_EN;
5230 if (cap && (val & AC_PINCTL_IN_EN)) {
5231 if (!(cap & AC_PINCAP_IN))
5232 val &= ~(AC_PINCTL_IN_EN | AC_PINCTL_VREFEN);
5236 return snd_hda_codec_update_cache(codec, pin, 0,
5237 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
5239 return snd_hda_codec_write(codec, pin, 0,
5240 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
5242 EXPORT_SYMBOL_HDA(_snd_hda_set_pin_ctl);
5245 * snd_hda_add_imux_item - Add an item to input_mux
5247 * When the same label is used already in the existing items, the number
5248 * suffix is appended to the label. This label index number is stored
5249 * to type_idx when non-NULL pointer is given.
5251 int snd_hda_add_imux_item(struct hda_input_mux *imux, const char *label,
5252 int index, int *type_idx)
5254 int i, label_idx = 0;
5255 if (imux->num_items >= HDA_MAX_NUM_INPUTS) {
5256 snd_printd(KERN_ERR "hda_codec: Too many imux items!\n");
5259 for (i = 0; i < imux->num_items; i++) {
5260 if (!strncmp(label, imux->items[i].label, strlen(label)))
5264 *type_idx = label_idx;
5266 snprintf(imux->items[imux->num_items].label,
5267 sizeof(imux->items[imux->num_items].label),
5268 "%s %d", label, label_idx);
5270 strlcpy(imux->items[imux->num_items].label, label,
5271 sizeof(imux->items[imux->num_items].label));
5272 imux->items[imux->num_items].index = index;
5276 EXPORT_SYMBOL_HDA(snd_hda_add_imux_item);
5285 * snd_hda_suspend - suspend the codecs
5288 * Returns 0 if successful.
5290 int snd_hda_suspend(struct hda_bus *bus)
5292 struct hda_codec *codec;
5294 list_for_each_entry(codec, &bus->codec_list, list) {
5295 cancel_delayed_work_sync(&codec->jackpoll_work);
5296 if (hda_codec_is_power_on(codec))
5297 hda_call_codec_suspend(codec, false);
5301 EXPORT_SYMBOL_HDA(snd_hda_suspend);
5304 * snd_hda_resume - resume the codecs
5307 * Returns 0 if successful.
5309 int snd_hda_resume(struct hda_bus *bus)
5311 struct hda_codec *codec;
5313 list_for_each_entry(codec, &bus->codec_list, list) {
5314 hda_call_codec_resume(codec);
5318 EXPORT_SYMBOL_HDA(snd_hda_resume);
5319 #endif /* CONFIG_PM */
5326 * snd_array_new - get a new element from the given array
5327 * @array: the array object
5329 * Get a new element from the given array. If it exceeds the
5330 * pre-allocated array size, re-allocate the array.
5332 * Returns NULL if allocation failed.
5334 void *snd_array_new(struct snd_array *array)
5336 if (snd_BUG_ON(!array->elem_size))
5338 if (array->used >= array->alloced) {
5339 int num = array->alloced + array->alloc_align;
5340 int size = (num + 1) * array->elem_size;
5341 int oldsize = array->alloced * array->elem_size;
5343 if (snd_BUG_ON(num >= 4096))
5345 nlist = krealloc(array->list, size, GFP_KERNEL);
5348 memset(nlist + oldsize, 0, size - oldsize);
5349 array->list = nlist;
5350 array->alloced = num;
5352 return snd_array_elem(array, array->used++);
5354 EXPORT_SYMBOL_HDA(snd_array_new);
5357 * snd_array_free - free the given array elements
5358 * @array: the array object
5360 void snd_array_free(struct snd_array *array)
5367 EXPORT_SYMBOL_HDA(snd_array_free);
5370 * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer
5371 * @pcm: PCM caps bits
5372 * @buf: the string buffer to write
5373 * @buflen: the max buffer length
5375 * used by hda_proc.c and hda_eld.c
5377 void snd_print_pcm_bits(int pcm, char *buf, int buflen)
5379 static unsigned int bits[] = { 8, 16, 20, 24, 32 };
5382 for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
5383 if (pcm & (AC_SUPPCM_BITS_8 << i))
5384 j += snprintf(buf + j, buflen - j, " %d", bits[i]);
5386 buf[j] = '\0'; /* necessary when j == 0 */
5388 EXPORT_SYMBOL_HDA(snd_print_pcm_bits);
5390 MODULE_DESCRIPTION("HDA codec core");
5391 MODULE_LICENSE("GPL");