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/mutex.h>
27 #include <linux/module.h>
28 #include <linux/async.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" },
71 { 0x434d, "C-Media" },
73 { 0x8384, "SigmaTel" },
77 static DEFINE_MUTEX(preset_mutex);
78 static LIST_HEAD(hda_preset_tables);
80 int snd_hda_add_codec_preset(struct hda_codec_preset_list *preset)
82 mutex_lock(&preset_mutex);
83 list_add_tail(&preset->list, &hda_preset_tables);
84 mutex_unlock(&preset_mutex);
87 EXPORT_SYMBOL_GPL(snd_hda_add_codec_preset);
89 int snd_hda_delete_codec_preset(struct hda_codec_preset_list *preset)
91 mutex_lock(&preset_mutex);
92 list_del(&preset->list);
93 mutex_unlock(&preset_mutex);
96 EXPORT_SYMBOL_GPL(snd_hda_delete_codec_preset);
99 #define codec_in_pm(codec) ((codec)->in_pm)
100 static void hda_power_work(struct work_struct *work);
101 static void hda_keep_power_on(struct hda_codec *codec);
102 #define hda_codec_is_power_on(codec) ((codec)->power_on)
104 static void hda_call_pm_notify(struct hda_codec *codec, bool power_up)
106 struct hda_bus *bus = codec->bus;
108 if ((power_up && codec->pm_up_notified) ||
109 (!power_up && !codec->pm_up_notified))
111 if (bus->ops.pm_notify)
112 bus->ops.pm_notify(bus, power_up);
113 codec->pm_up_notified = power_up;
117 #define codec_in_pm(codec) 0
118 static inline void hda_keep_power_on(struct hda_codec *codec) {}
119 #define hda_codec_is_power_on(codec) 1
120 #define hda_call_pm_notify(codec, state) {}
124 * snd_hda_get_jack_location - Give a location string of the jack
125 * @cfg: pin default config value
127 * Parse the pin default config value and returns the string of the
128 * jack location, e.g. "Rear", "Front", etc.
130 const char *snd_hda_get_jack_location(u32 cfg)
132 static char *bases[7] = {
133 "N/A", "Rear", "Front", "Left", "Right", "Top", "Bottom",
135 static unsigned char specials_idx[] = {
140 static char *specials[] = {
141 "Rear Panel", "Drive Bar",
142 "Riser", "HDMI", "ATAPI",
143 "Mobile-In", "Mobile-Out"
146 cfg = (cfg & AC_DEFCFG_LOCATION) >> AC_DEFCFG_LOCATION_SHIFT;
147 if ((cfg & 0x0f) < 7)
148 return bases[cfg & 0x0f];
149 for (i = 0; i < ARRAY_SIZE(specials_idx); i++) {
150 if (cfg == specials_idx[i])
155 EXPORT_SYMBOL_GPL(snd_hda_get_jack_location);
158 * snd_hda_get_jack_connectivity - Give a connectivity string of the jack
159 * @cfg: pin default config value
161 * Parse the pin default config value and returns the string of the
162 * jack connectivity, i.e. external or internal connection.
164 const char *snd_hda_get_jack_connectivity(u32 cfg)
166 static char *jack_locations[4] = { "Ext", "Int", "Sep", "Oth" };
168 return jack_locations[(cfg >> (AC_DEFCFG_LOCATION_SHIFT + 4)) & 3];
170 EXPORT_SYMBOL_GPL(snd_hda_get_jack_connectivity);
173 * snd_hda_get_jack_type - Give a type string of the jack
174 * @cfg: pin default config value
176 * Parse the pin default config value and returns the string of the
177 * jack type, i.e. the purpose of the jack, such as Line-Out or CD.
179 const char *snd_hda_get_jack_type(u32 cfg)
181 static char *jack_types[16] = {
182 "Line Out", "Speaker", "HP Out", "CD",
183 "SPDIF Out", "Digital Out", "Modem Line", "Modem Hand",
184 "Line In", "Aux", "Mic", "Telephony",
185 "SPDIF In", "Digital In", "Reserved", "Other"
188 return jack_types[(cfg & AC_DEFCFG_DEVICE)
189 >> AC_DEFCFG_DEVICE_SHIFT];
191 EXPORT_SYMBOL_GPL(snd_hda_get_jack_type);
194 * Compose a 32bit command word to be sent to the HD-audio controller
196 static inline unsigned int
197 make_codec_cmd(struct hda_codec *codec, hda_nid_t nid, int flags,
198 unsigned int verb, unsigned int parm)
202 if ((codec->addr & ~0xf) || (nid & ~0x7f) ||
203 (verb & ~0xfff) || (parm & ~0xffff)) {
204 codec_err(codec, "hda-codec: out of range cmd %x:%x:%x:%x\n",
205 codec->addr, nid, verb, parm);
209 val = (u32)codec->addr << 28;
210 val |= (u32)nid << 20;
217 * Send and receive a verb
219 static int codec_exec_verb(struct hda_codec *codec, unsigned int cmd,
220 int flags, unsigned int *res)
222 struct hda_bus *bus = codec->bus;
231 snd_hda_power_up(codec);
232 mutex_lock(&bus->cmd_mutex);
233 if (flags & HDA_RW_NO_RESPONSE_FALLBACK)
234 bus->no_response_fallback = 1;
236 trace_hda_send_cmd(codec, cmd);
237 err = bus->ops.command(bus, cmd);
240 /* process pending verbs */
241 bus->ops.get_response(bus, codec->addr);
244 *res = bus->ops.get_response(bus, codec->addr);
245 trace_hda_get_response(codec, *res);
247 bus->no_response_fallback = 0;
248 mutex_unlock(&bus->cmd_mutex);
249 snd_hda_power_down(codec);
250 if (!codec_in_pm(codec) && res && *res == -1 && bus->rirb_error) {
251 if (bus->response_reset) {
253 "resetting BUS due to fatal communication error\n");
254 trace_hda_bus_reset(bus);
255 bus->ops.bus_reset(bus);
259 /* clear reset-flag when the communication gets recovered */
260 if (!err || codec_in_pm(codec))
261 bus->response_reset = 0;
266 * snd_hda_codec_read - send a command and get the response
267 * @codec: the HDA codec
268 * @nid: NID to send the command
269 * @flags: optional bit flags
270 * @verb: the verb to send
271 * @parm: the parameter for the verb
273 * Send a single command and read the corresponding response.
275 * Returns the obtained response value, or -1 for an error.
277 unsigned int snd_hda_codec_read(struct hda_codec *codec, hda_nid_t nid,
279 unsigned int verb, unsigned int parm)
281 unsigned cmd = make_codec_cmd(codec, nid, flags, verb, parm);
283 if (codec_exec_verb(codec, cmd, flags, &res))
287 EXPORT_SYMBOL_GPL(snd_hda_codec_read);
290 * snd_hda_codec_write - send a single command without waiting for response
291 * @codec: the HDA codec
292 * @nid: NID to send the command
293 * @flags: optional bit flags
294 * @verb: the verb to send
295 * @parm: the parameter for the verb
297 * Send a single command without waiting for response.
299 * Returns 0 if successful, or a negative error code.
301 int snd_hda_codec_write(struct hda_codec *codec, hda_nid_t nid, int flags,
302 unsigned int verb, unsigned int parm)
304 unsigned int cmd = make_codec_cmd(codec, nid, flags, verb, parm);
306 return codec_exec_verb(codec, cmd, flags,
307 codec->bus->sync_write ? &res : NULL);
309 EXPORT_SYMBOL_GPL(snd_hda_codec_write);
312 * snd_hda_sequence_write - sequence writes
313 * @codec: the HDA codec
314 * @seq: VERB array to send
316 * Send the commands sequentially from the given array.
317 * The array must be terminated with NID=0.
319 void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq)
321 for (; seq->nid; seq++)
322 snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param);
324 EXPORT_SYMBOL_GPL(snd_hda_sequence_write);
327 * snd_hda_get_sub_nodes - get the range of sub nodes
328 * @codec: the HDA codec
330 * @start_id: the pointer to store the start NID
332 * Parse the NID and store the start NID of its sub-nodes.
333 * Returns the number of sub-nodes.
335 int snd_hda_get_sub_nodes(struct hda_codec *codec, hda_nid_t nid,
340 parm = snd_hda_param_read(codec, nid, AC_PAR_NODE_COUNT);
343 *start_id = (parm >> 16) & 0x7fff;
344 return (int)(parm & 0x7fff);
346 EXPORT_SYMBOL_GPL(snd_hda_get_sub_nodes);
348 /* connection list element */
349 struct hda_conn_list {
350 struct list_head list;
356 /* look up the cached results */
357 static struct hda_conn_list *
358 lookup_conn_list(struct hda_codec *codec, hda_nid_t nid)
360 struct hda_conn_list *p;
361 list_for_each_entry(p, &codec->conn_list, list) {
368 static int add_conn_list(struct hda_codec *codec, hda_nid_t nid, int len,
369 const hda_nid_t *list)
371 struct hda_conn_list *p;
373 p = kmalloc(sizeof(*p) + len * sizeof(hda_nid_t), GFP_KERNEL);
378 memcpy(p->conns, list, len * sizeof(hda_nid_t));
379 list_add(&p->list, &codec->conn_list);
383 static void remove_conn_list(struct hda_codec *codec)
385 while (!list_empty(&codec->conn_list)) {
386 struct hda_conn_list *p;
387 p = list_first_entry(&codec->conn_list, typeof(*p), list);
393 /* read the connection and add to the cache */
394 static int read_and_add_raw_conns(struct hda_codec *codec, hda_nid_t nid)
397 hda_nid_t *result = list;
400 len = snd_hda_get_raw_connections(codec, nid, list, ARRAY_SIZE(list));
401 if (len == -ENOSPC) {
402 len = snd_hda_get_num_raw_conns(codec, nid);
403 result = kmalloc(sizeof(hda_nid_t) * len, GFP_KERNEL);
406 len = snd_hda_get_raw_connections(codec, nid, result, len);
409 len = snd_hda_override_conn_list(codec, nid, len, result);
416 * snd_hda_get_conn_list - get connection list
417 * @codec: the HDA codec
419 * @len: number of connection list entries
420 * @listp: the pointer to store NID list
422 * Parses the connection list of the given widget and stores the pointer
423 * to the list of NIDs.
425 * Returns the number of connections, or a negative error code.
427 * Note that the returned pointer isn't protected against the list
428 * modification. If snd_hda_override_conn_list() might be called
429 * concurrently, protect with a mutex appropriately.
431 int snd_hda_get_conn_list(struct hda_codec *codec, hda_nid_t nid,
432 const hda_nid_t **listp)
438 const struct hda_conn_list *p;
440 /* if the connection-list is already cached, read it */
441 p = lookup_conn_list(codec, nid);
447 if (snd_BUG_ON(added))
450 err = read_and_add_raw_conns(codec, nid);
456 EXPORT_SYMBOL_GPL(snd_hda_get_conn_list);
459 * snd_hda_get_connections - copy connection list
460 * @codec: the HDA codec
462 * @conn_list: connection list array; when NULL, checks only the size
463 * @max_conns: max. number of connections to store
465 * Parses the connection list of the given widget and stores the list
468 * Returns the number of connections, or a negative error code.
470 int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
471 hda_nid_t *conn_list, int max_conns)
473 const hda_nid_t *list;
474 int len = snd_hda_get_conn_list(codec, nid, &list);
476 if (len > 0 && conn_list) {
477 if (len > max_conns) {
478 codec_err(codec, "Too many connections %d for NID 0x%x\n",
482 memcpy(conn_list, list, len * sizeof(hda_nid_t));
487 EXPORT_SYMBOL_GPL(snd_hda_get_connections);
489 /* return CONNLIST_LEN parameter of the given widget */
490 static unsigned int get_num_conns(struct hda_codec *codec, hda_nid_t nid)
492 unsigned int wcaps = get_wcaps(codec, nid);
495 if (!(wcaps & AC_WCAP_CONN_LIST) &&
496 get_wcaps_type(wcaps) != AC_WID_VOL_KNB)
499 parm = snd_hda_param_read(codec, nid, AC_PAR_CONNLIST_LEN);
505 int snd_hda_get_num_raw_conns(struct hda_codec *codec, hda_nid_t nid)
507 return snd_hda_get_raw_connections(codec, nid, NULL, 0);
511 * snd_hda_get_raw_connections - copy connection list without cache
512 * @codec: the HDA codec
514 * @conn_list: connection list array
515 * @max_conns: max. number of connections to store
517 * Like snd_hda_get_connections(), copy the connection list but without
518 * checking through the connection-list cache.
519 * Currently called only from hda_proc.c, so not exported.
521 int snd_hda_get_raw_connections(struct hda_codec *codec, hda_nid_t nid,
522 hda_nid_t *conn_list, int max_conns)
525 int i, conn_len, conns;
526 unsigned int shift, num_elems, mask;
530 parm = get_num_conns(codec, nid);
534 if (parm & AC_CLIST_LONG) {
543 conn_len = parm & AC_CLIST_LENGTH;
544 mask = (1 << (shift-1)) - 1;
547 return 0; /* no connection */
550 /* single connection */
551 parm = snd_hda_codec_read(codec, nid, 0,
552 AC_VERB_GET_CONNECT_LIST, 0);
553 if (parm == -1 && codec->bus->rirb_error)
556 conn_list[0] = parm & mask;
560 /* multi connection */
563 for (i = 0; i < conn_len; i++) {
567 if (i % num_elems == 0) {
568 parm = snd_hda_codec_read(codec, nid, 0,
569 AC_VERB_GET_CONNECT_LIST, i);
570 if (parm == -1 && codec->bus->rirb_error)
573 range_val = !!(parm & (1 << (shift-1))); /* ranges */
575 if (val == 0 && null_count++) { /* no second chance */
577 "invalid CONNECT_LIST verb %x[%i]:%x\n",
583 /* ranges between the previous and this one */
584 if (!prev_nid || prev_nid >= val) {
586 "invalid dep_range_val %x:%x\n",
590 for (n = prev_nid + 1; n <= val; n++) {
592 if (conns >= max_conns)
594 conn_list[conns] = n;
600 if (conns >= max_conns)
602 conn_list[conns] = val;
612 * snd_hda_override_conn_list - add/modify the connection-list to cache
613 * @codec: the HDA codec
615 * @len: number of connection list entries
616 * @list: the list of connection entries
618 * Add or modify the given connection-list to the cache. If the corresponding
619 * cache already exists, invalidate it and append a new one.
621 * Returns zero or a negative error code.
623 int snd_hda_override_conn_list(struct hda_codec *codec, hda_nid_t nid, int len,
624 const hda_nid_t *list)
626 struct hda_conn_list *p;
628 p = lookup_conn_list(codec, nid);
634 return add_conn_list(codec, nid, len, list);
636 EXPORT_SYMBOL_GPL(snd_hda_override_conn_list);
639 * snd_hda_get_conn_index - get the connection index of the given NID
640 * @codec: the HDA codec
641 * @mux: NID containing the list
642 * @nid: NID to select
643 * @recursive: 1 when searching NID recursively, otherwise 0
645 * Parses the connection list of the widget @mux and checks whether the
646 * widget @nid is present. If it is, return the connection index.
647 * Otherwise it returns -1.
649 int snd_hda_get_conn_index(struct hda_codec *codec, hda_nid_t mux,
650 hda_nid_t nid, int recursive)
652 const hda_nid_t *conn;
655 nums = snd_hda_get_conn_list(codec, mux, &conn);
656 for (i = 0; i < nums; i++)
661 if (recursive > 10) {
662 codec_dbg(codec, "too deep connection for 0x%x\n", nid);
666 for (i = 0; i < nums; i++) {
667 unsigned int type = get_wcaps_type(get_wcaps(codec, conn[i]));
668 if (type == AC_WID_PIN || type == AC_WID_AUD_OUT)
670 if (snd_hda_get_conn_index(codec, conn[i], nid, recursive) >= 0)
675 EXPORT_SYMBOL_GPL(snd_hda_get_conn_index);
678 /* return DEVLIST_LEN parameter of the given widget */
679 static unsigned int get_num_devices(struct hda_codec *codec, hda_nid_t nid)
681 unsigned int wcaps = get_wcaps(codec, nid);
684 if (!codec->dp_mst || !(wcaps & AC_WCAP_DIGITAL) ||
685 get_wcaps_type(wcaps) != AC_WID_PIN)
688 parm = snd_hda_param_read(codec, nid, AC_PAR_DEVLIST_LEN);
689 if (parm == -1 && codec->bus->rirb_error)
691 return parm & AC_DEV_LIST_LEN_MASK;
695 * snd_hda_get_devices - copy device list without cache
696 * @codec: the HDA codec
697 * @nid: NID of the pin to parse
698 * @dev_list: device list array
699 * @max_devices: max. number of devices to store
701 * Copy the device list. This info is dynamic and so not cached.
702 * Currently called only from hda_proc.c, so not exported.
704 int snd_hda_get_devices(struct hda_codec *codec, hda_nid_t nid,
705 u8 *dev_list, int max_devices)
708 int i, dev_len, devices;
710 parm = get_num_devices(codec, nid);
711 if (!parm) /* not multi-stream capable */
715 dev_len = dev_len < max_devices ? dev_len : max_devices;
718 while (devices < dev_len) {
719 parm = snd_hda_codec_read(codec, nid, 0,
720 AC_VERB_GET_DEVICE_LIST, devices);
721 if (parm == -1 && codec->bus->rirb_error)
724 for (i = 0; i < 8; i++) {
725 dev_list[devices] = (u8)parm;
728 if (devices >= dev_len)
736 * snd_hda_queue_unsol_event - add an unsolicited event to queue
738 * @res: unsolicited event (lower 32bit of RIRB entry)
739 * @res_ex: codec addr and flags (upper 32bit or RIRB entry)
741 * Adds the given event to the queue. The events are processed in
742 * the workqueue asynchronously. Call this function in the interrupt
743 * hanlder when RIRB receives an unsolicited event.
745 * Returns 0 if successful, or a negative error code.
747 int snd_hda_queue_unsol_event(struct hda_bus *bus, u32 res, u32 res_ex)
749 struct hda_bus_unsolicited *unsol;
752 if (!bus || !bus->workq)
755 trace_hda_unsol_event(bus, res, res_ex);
760 wp = (unsol->wp + 1) % HDA_UNSOL_QUEUE_SIZE;
764 unsol->queue[wp] = res;
765 unsol->queue[wp + 1] = res_ex;
767 queue_work(bus->workq, &unsol->work);
771 EXPORT_SYMBOL_GPL(snd_hda_queue_unsol_event);
774 * process queued unsolicited events
776 static void process_unsol_events(struct work_struct *work)
778 struct hda_bus_unsolicited *unsol =
779 container_of(work, struct hda_bus_unsolicited, work);
780 struct hda_bus *bus = unsol->bus;
781 struct hda_codec *codec;
782 unsigned int rp, caddr, res;
784 while (unsol->rp != unsol->wp) {
785 rp = (unsol->rp + 1) % HDA_UNSOL_QUEUE_SIZE;
788 res = unsol->queue[rp];
789 caddr = unsol->queue[rp + 1];
790 if (!(caddr & (1 << 4))) /* no unsolicited event? */
792 codec = bus->caddr_tbl[caddr & 0x0f];
793 if (codec && codec->patch_ops.unsol_event)
794 codec->patch_ops.unsol_event(codec, res);
799 * initialize unsolicited queue
801 static int init_unsol_queue(struct hda_bus *bus)
803 struct hda_bus_unsolicited *unsol;
805 if (bus->unsol) /* already initialized */
808 unsol = kzalloc(sizeof(*unsol), GFP_KERNEL);
810 dev_err(bus->card->dev, "can't allocate unsolicited queue\n");
813 INIT_WORK(&unsol->work, process_unsol_events);
822 static void snd_hda_bus_free(struct hda_bus *bus)
827 WARN_ON(!list_empty(&bus->codec_list));
829 flush_workqueue(bus->workq);
832 if (bus->ops.private_free)
833 bus->ops.private_free(bus);
835 destroy_workqueue(bus->workq);
840 static int snd_hda_bus_dev_free(struct snd_device *device)
842 snd_hda_bus_free(device->device_data);
846 static int snd_hda_bus_dev_disconnect(struct snd_device *device)
848 struct hda_bus *bus = device->device_data;
854 * snd_hda_bus_new - create a HDA bus
855 * @card: the card entry
856 * @temp: the template for hda_bus information
857 * @busp: the pointer to store the created bus instance
859 * Returns 0 if successful, or a negative error code.
861 int snd_hda_bus_new(struct snd_card *card,
862 const struct hda_bus_template *temp,
863 struct hda_bus **busp)
867 static struct snd_device_ops dev_ops = {
868 .dev_disconnect = snd_hda_bus_dev_disconnect,
869 .dev_free = snd_hda_bus_dev_free,
872 if (snd_BUG_ON(!temp))
874 if (snd_BUG_ON(!temp->ops.command || !temp->ops.get_response))
880 bus = kzalloc(sizeof(*bus), GFP_KERNEL);
882 dev_err(card->dev, "can't allocate struct hda_bus\n");
887 bus->private_data = temp->private_data;
888 bus->pci = temp->pci;
889 bus->modelname = temp->modelname;
890 bus->power_save = temp->power_save;
891 bus->ops = temp->ops;
893 mutex_init(&bus->cmd_mutex);
894 mutex_init(&bus->prepare_mutex);
895 INIT_LIST_HEAD(&bus->codec_list);
897 snprintf(bus->workq_name, sizeof(bus->workq_name),
898 "hd-audio%d", card->number);
899 bus->workq = create_singlethread_workqueue(bus->workq_name);
901 dev_err(card->dev, "cannot create workqueue %s\n",
907 err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops);
909 snd_hda_bus_free(bus);
916 EXPORT_SYMBOL_GPL(snd_hda_bus_new);
918 #if IS_ENABLED(CONFIG_SND_HDA_GENERIC)
919 #define is_generic_config(codec) \
920 (codec->modelname && !strcmp(codec->modelname, "generic"))
922 #define is_generic_config(codec) 0
926 #define HDA_MODREQ_MAX_COUNT 2 /* two request_modules()'s */
928 #define HDA_MODREQ_MAX_COUNT 0 /* all presets are statically linked */
932 * find a matching codec preset
934 static const struct hda_codec_preset *
935 find_codec_preset(struct hda_codec *codec)
937 struct hda_codec_preset_list *tbl;
938 const struct hda_codec_preset *preset;
939 unsigned int mod_requested = 0;
942 mutex_lock(&preset_mutex);
943 list_for_each_entry(tbl, &hda_preset_tables, list) {
944 if (!try_module_get(tbl->owner)) {
945 codec_err(codec, "cannot module_get\n");
948 for (preset = tbl->preset; preset->id; preset++) {
949 u32 mask = preset->mask;
950 if (preset->afg && preset->afg != codec->afg)
952 if (preset->mfg && preset->mfg != codec->mfg)
956 if (preset->id == (codec->vendor_id & mask) &&
958 preset->rev == codec->revision_id)) {
959 mutex_unlock(&preset_mutex);
960 codec->owner = tbl->owner;
964 module_put(tbl->owner);
966 mutex_unlock(&preset_mutex);
968 if (mod_requested < HDA_MODREQ_MAX_COUNT) {
971 snprintf(name, sizeof(name), "snd-hda-codec-id:%08x",
974 snprintf(name, sizeof(name), "snd-hda-codec-id:%04x*",
975 (codec->vendor_id >> 16) & 0xffff);
976 request_module(name);
984 * get_codec_name - store the codec name
986 static int get_codec_name(struct hda_codec *codec)
988 const struct hda_vendor_id *c;
989 const char *vendor = NULL;
990 u16 vendor_id = codec->vendor_id >> 16;
993 if (codec->vendor_name)
996 for (c = hda_vendor_ids; c->id; c++) {
997 if (c->id == vendor_id) {
1003 sprintf(tmp, "Generic %04x", vendor_id);
1006 codec->vendor_name = kstrdup(vendor, GFP_KERNEL);
1007 if (!codec->vendor_name)
1011 if (codec->chip_name)
1014 if (codec->preset && codec->preset->name)
1015 codec->chip_name = kstrdup(codec->preset->name, GFP_KERNEL);
1017 sprintf(tmp, "ID %x", codec->vendor_id & 0xffff);
1018 codec->chip_name = kstrdup(tmp, GFP_KERNEL);
1020 if (!codec->chip_name)
1026 * look for an AFG and MFG nodes
1028 static void setup_fg_nodes(struct hda_codec *codec)
1030 int i, total_nodes, function_id;
1033 total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid);
1034 for (i = 0; i < total_nodes; i++, nid++) {
1035 function_id = snd_hda_param_read(codec, nid,
1036 AC_PAR_FUNCTION_TYPE);
1037 switch (function_id & 0xff) {
1038 case AC_GRP_AUDIO_FUNCTION:
1040 codec->afg_function_id = function_id & 0xff;
1041 codec->afg_unsol = (function_id >> 8) & 1;
1043 case AC_GRP_MODEM_FUNCTION:
1045 codec->mfg_function_id = function_id & 0xff;
1046 codec->mfg_unsol = (function_id >> 8) & 1;
1055 * read widget caps for each widget and store in cache
1057 static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
1062 codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node,
1064 codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL);
1067 nid = codec->start_nid;
1068 for (i = 0; i < codec->num_nodes; i++, nid++)
1069 codec->wcaps[i] = snd_hda_param_read(codec, nid,
1070 AC_PAR_AUDIO_WIDGET_CAP);
1074 /* read all pin default configurations and save codec->init_pins */
1075 static int read_pin_defaults(struct hda_codec *codec)
1078 hda_nid_t nid = codec->start_nid;
1080 for (i = 0; i < codec->num_nodes; i++, nid++) {
1081 struct hda_pincfg *pin;
1082 unsigned int wcaps = get_wcaps(codec, nid);
1083 unsigned int wid_type = get_wcaps_type(wcaps);
1084 if (wid_type != AC_WID_PIN)
1086 pin = snd_array_new(&codec->init_pins);
1090 pin->cfg = snd_hda_codec_read(codec, nid, 0,
1091 AC_VERB_GET_CONFIG_DEFAULT, 0);
1092 pin->ctrl = snd_hda_codec_read(codec, nid, 0,
1093 AC_VERB_GET_PIN_WIDGET_CONTROL,
1099 /* look up the given pin config list and return the item matching with NID */
1100 static struct hda_pincfg *look_up_pincfg(struct hda_codec *codec,
1101 struct snd_array *array,
1105 for (i = 0; i < array->used; i++) {
1106 struct hda_pincfg *pin = snd_array_elem(array, i);
1107 if (pin->nid == nid)
1113 /* set the current pin config value for the given NID.
1114 * the value is cached, and read via snd_hda_codec_get_pincfg()
1116 int snd_hda_add_pincfg(struct hda_codec *codec, struct snd_array *list,
1117 hda_nid_t nid, unsigned int cfg)
1119 struct hda_pincfg *pin;
1121 /* the check below may be invalid when pins are added by a fixup
1122 * dynamically (e.g. via snd_hda_codec_update_widgets()), so disabled
1126 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN)
1130 pin = look_up_pincfg(codec, list, nid);
1132 pin = snd_array_new(list);
1142 * snd_hda_codec_set_pincfg - Override a pin default configuration
1143 * @codec: the HDA codec
1144 * @nid: NID to set the pin config
1145 * @cfg: the pin default config value
1147 * Override a pin default configuration value in the cache.
1148 * This value can be read by snd_hda_codec_get_pincfg() in a higher
1149 * priority than the real hardware value.
1151 int snd_hda_codec_set_pincfg(struct hda_codec *codec,
1152 hda_nid_t nid, unsigned int cfg)
1154 return snd_hda_add_pincfg(codec, &codec->driver_pins, nid, cfg);
1156 EXPORT_SYMBOL_GPL(snd_hda_codec_set_pincfg);
1159 * snd_hda_codec_get_pincfg - Obtain a pin-default configuration
1160 * @codec: the HDA codec
1161 * @nid: NID to get the pin config
1163 * Get the current pin config value of the given pin NID.
1164 * If the pincfg value is cached or overridden via sysfs or driver,
1165 * returns the cached value.
1167 unsigned int snd_hda_codec_get_pincfg(struct hda_codec *codec, hda_nid_t nid)
1169 struct hda_pincfg *pin;
1171 #ifdef CONFIG_SND_HDA_RECONFIG
1173 unsigned int cfg = 0;
1174 mutex_lock(&codec->user_mutex);
1175 pin = look_up_pincfg(codec, &codec->user_pins, nid);
1178 mutex_unlock(&codec->user_mutex);
1183 pin = look_up_pincfg(codec, &codec->driver_pins, nid);
1186 pin = look_up_pincfg(codec, &codec->init_pins, nid);
1191 EXPORT_SYMBOL_GPL(snd_hda_codec_get_pincfg);
1193 /* remember the current pinctl target value */
1194 int snd_hda_codec_set_pin_target(struct hda_codec *codec, hda_nid_t nid,
1197 struct hda_pincfg *pin;
1199 pin = look_up_pincfg(codec, &codec->init_pins, nid);
1205 EXPORT_SYMBOL_GPL(snd_hda_codec_set_pin_target);
1207 /* return the current pinctl target value */
1208 int snd_hda_codec_get_pin_target(struct hda_codec *codec, hda_nid_t nid)
1210 struct hda_pincfg *pin;
1212 pin = look_up_pincfg(codec, &codec->init_pins, nid);
1217 EXPORT_SYMBOL_GPL(snd_hda_codec_get_pin_target);
1220 * snd_hda_shutup_pins - Shut up all pins
1221 * @codec: the HDA codec
1223 * Clear all pin controls to shup up before suspend for avoiding click noise.
1224 * The controls aren't cached so that they can be resumed properly.
1226 void snd_hda_shutup_pins(struct hda_codec *codec)
1229 /* don't shut up pins when unloading the driver; otherwise it breaks
1230 * the default pin setup at the next load of the driver
1232 if (codec->bus->shutdown)
1234 for (i = 0; i < codec->init_pins.used; i++) {
1235 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1236 /* use read here for syncing after issuing each verb */
1237 snd_hda_codec_read(codec, pin->nid, 0,
1238 AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
1240 codec->pins_shutup = 1;
1242 EXPORT_SYMBOL_GPL(snd_hda_shutup_pins);
1245 /* Restore the pin controls cleared previously via snd_hda_shutup_pins() */
1246 static void restore_shutup_pins(struct hda_codec *codec)
1249 if (!codec->pins_shutup)
1251 if (codec->bus->shutdown)
1253 for (i = 0; i < codec->init_pins.used; i++) {
1254 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1255 snd_hda_codec_write(codec, pin->nid, 0,
1256 AC_VERB_SET_PIN_WIDGET_CONTROL,
1259 codec->pins_shutup = 0;
1263 static void hda_jackpoll_work(struct work_struct *work)
1265 struct hda_codec *codec =
1266 container_of(work, struct hda_codec, jackpoll_work.work);
1268 snd_hda_jack_set_dirty_all(codec);
1269 snd_hda_jack_poll_all(codec);
1271 if (!codec->jackpoll_interval)
1274 queue_delayed_work(codec->bus->workq, &codec->jackpoll_work,
1275 codec->jackpoll_interval);
1278 static void init_hda_cache(struct hda_cache_rec *cache,
1279 unsigned int record_size);
1280 static void free_hda_cache(struct hda_cache_rec *cache);
1282 /* release all pincfg lists */
1283 static void free_init_pincfgs(struct hda_codec *codec)
1285 snd_array_free(&codec->driver_pins);
1286 #ifdef CONFIG_SND_HDA_RECONFIG
1287 snd_array_free(&codec->user_pins);
1289 snd_array_free(&codec->init_pins);
1293 * audio-converter setup caches
1295 struct hda_cvt_setup {
1300 unsigned char active; /* cvt is currently used */
1301 unsigned char dirty; /* setups should be cleared */
1304 /* get or create a cache entry for the given audio converter NID */
1305 static struct hda_cvt_setup *
1306 get_hda_cvt_setup(struct hda_codec *codec, hda_nid_t nid)
1308 struct hda_cvt_setup *p;
1311 for (i = 0; i < codec->cvt_setups.used; i++) {
1312 p = snd_array_elem(&codec->cvt_setups, i);
1316 p = snd_array_new(&codec->cvt_setups);
1323 * Dynamic symbol binding for the codec parsers
1326 #define load_parser(codec, sym) \
1327 ((codec)->parser = (int (*)(struct hda_codec *))symbol_request(sym))
1329 static void unload_parser(struct hda_codec *codec)
1332 symbol_put_addr(codec->parser);
1333 codec->parser = NULL;
1339 static void snd_hda_codec_free(struct hda_codec *codec)
1343 cancel_delayed_work_sync(&codec->jackpoll_work);
1344 snd_hda_jack_tbl_clear(codec);
1345 free_init_pincfgs(codec);
1347 cancel_delayed_work(&codec->power_work);
1348 flush_workqueue(codec->bus->workq);
1350 list_del(&codec->list);
1351 snd_array_free(&codec->mixers);
1352 snd_array_free(&codec->nids);
1353 snd_array_free(&codec->cvt_setups);
1354 snd_array_free(&codec->spdif_out);
1355 remove_conn_list(codec);
1356 codec->bus->caddr_tbl[codec->addr] = NULL;
1357 if (codec->patch_ops.free)
1358 codec->patch_ops.free(codec);
1359 hda_call_pm_notify(codec, false); /* cancel leftover refcounts */
1360 snd_hda_sysfs_clear(codec);
1361 unload_parser(codec);
1362 module_put(codec->owner);
1363 free_hda_cache(&codec->amp_cache);
1364 free_hda_cache(&codec->cmd_cache);
1365 kfree(codec->vendor_name);
1366 kfree(codec->chip_name);
1367 kfree(codec->modelname);
1368 kfree(codec->wcaps);
1369 codec->bus->num_codecs--;
1370 put_device(&codec->dev);
1373 static bool snd_hda_codec_get_supported_ps(struct hda_codec *codec,
1374 hda_nid_t fg, unsigned int power_state);
1376 static unsigned int hda_set_power_state(struct hda_codec *codec,
1377 unsigned int power_state);
1379 static int snd_hda_codec_dev_register(struct snd_device *device)
1381 struct hda_codec *codec = device->device_data;
1382 int err = device_add(&codec->dev);
1386 snd_hda_register_beep_device(codec);
1390 static int snd_hda_codec_dev_disconnect(struct snd_device *device)
1392 struct hda_codec *codec = device->device_data;
1394 snd_hda_detach_beep_device(codec);
1395 device_del(&codec->dev);
1399 static int snd_hda_codec_dev_free(struct snd_device *device)
1401 snd_hda_codec_free(device->device_data);
1405 /* just free the container */
1406 static void snd_hda_codec_dev_release(struct device *dev)
1408 kfree(container_of(dev, struct hda_codec, dev));
1412 * snd_hda_codec_new - create a HDA codec
1413 * @bus: the bus to assign
1414 * @codec_addr: the codec address
1415 * @codecp: the pointer to store the generated codec
1417 * Returns 0 if successful, or a negative error code.
1419 int snd_hda_codec_new(struct hda_bus *bus,
1420 unsigned int codec_addr,
1421 struct hda_codec **codecp)
1423 struct hda_codec *codec;
1427 static struct snd_device_ops dev_ops = {
1428 .dev_register = snd_hda_codec_dev_register,
1429 .dev_disconnect = snd_hda_codec_dev_disconnect,
1430 .dev_free = snd_hda_codec_dev_free,
1433 if (snd_BUG_ON(!bus))
1435 if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS))
1438 if (bus->caddr_tbl[codec_addr]) {
1439 dev_err(bus->card->dev,
1440 "address 0x%x is already occupied\n",
1445 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
1446 if (codec == NULL) {
1447 dev_err(bus->card->dev, "can't allocate struct hda_codec\n");
1451 device_initialize(&codec->dev);
1452 codec->dev.parent = &bus->card->card_dev;
1453 codec->dev.class = sound_class;
1454 codec->dev.release = snd_hda_codec_dev_release;
1455 codec->dev.groups = snd_hda_dev_attr_groups;
1456 dev_set_name(&codec->dev, "hdaudioC%dD%d", bus->card->number,
1458 dev_set_drvdata(&codec->dev, codec); /* for sysfs */
1461 codec->addr = codec_addr;
1462 mutex_init(&codec->spdif_mutex);
1463 mutex_init(&codec->control_mutex);
1464 mutex_init(&codec->hash_mutex);
1465 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
1466 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
1467 snd_array_init(&codec->mixers, sizeof(struct hda_nid_item), 32);
1468 snd_array_init(&codec->nids, sizeof(struct hda_nid_item), 32);
1469 snd_array_init(&codec->init_pins, sizeof(struct hda_pincfg), 16);
1470 snd_array_init(&codec->driver_pins, sizeof(struct hda_pincfg), 16);
1471 snd_array_init(&codec->cvt_setups, sizeof(struct hda_cvt_setup), 8);
1472 snd_array_init(&codec->spdif_out, sizeof(struct hda_spdif_out), 16);
1473 snd_array_init(&codec->jacktbl, sizeof(struct hda_jack_tbl), 16);
1474 snd_array_init(&codec->verbs, sizeof(struct hda_verb *), 8);
1475 INIT_LIST_HEAD(&codec->conn_list);
1477 INIT_DELAYED_WORK(&codec->jackpoll_work, hda_jackpoll_work);
1478 codec->depop_delay = -1;
1479 codec->fixup_id = HDA_FIXUP_ID_NOT_SET;
1482 spin_lock_init(&codec->power_lock);
1483 INIT_DELAYED_WORK(&codec->power_work, hda_power_work);
1484 /* snd_hda_codec_new() marks the codec as power-up, and leave it as is.
1485 * the caller has to power down appropriatley after initialization
1488 hda_keep_power_on(codec);
1491 snd_hda_sysfs_init(codec);
1493 if (codec->bus->modelname) {
1494 codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL);
1495 if (!codec->modelname) {
1501 list_add_tail(&codec->list, &bus->codec_list);
1504 bus->caddr_tbl[codec_addr] = codec;
1506 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1508 if (codec->vendor_id == -1)
1509 /* read again, hopefully the access method was corrected
1510 * in the last read...
1512 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1514 codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1515 AC_PAR_SUBSYSTEM_ID);
1516 codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1519 setup_fg_nodes(codec);
1520 if (!codec->afg && !codec->mfg) {
1521 dev_err(bus->card->dev, "no AFG or MFG node found\n");
1526 fg = codec->afg ? codec->afg : codec->mfg;
1527 err = read_widget_caps(codec, fg);
1529 dev_err(bus->card->dev, "cannot malloc\n");
1532 err = read_pin_defaults(codec);
1536 if (!codec->subsystem_id) {
1537 codec->subsystem_id =
1538 snd_hda_codec_read(codec, fg, 0,
1539 AC_VERB_GET_SUBSYSTEM_ID, 0);
1543 codec->d3_stop_clk = snd_hda_codec_get_supported_ps(codec, fg,
1546 codec->epss = snd_hda_codec_get_supported_ps(codec, fg,
1549 if (!codec->d3_stop_clk || !codec->epss)
1550 bus->power_keep_link_on = 1;
1554 /* power-up all before initialization */
1555 hda_set_power_state(codec, AC_PWRST_D0);
1557 snd_hda_codec_proc_new(codec);
1559 snd_hda_create_hwdep(codec);
1561 sprintf(component, "HDA:%08x,%08x,%08x", codec->vendor_id,
1562 codec->subsystem_id, codec->revision_id);
1563 snd_component_add(codec->bus->card, component);
1565 err = snd_device_new(bus->card, SNDRV_DEV_CODEC, codec, &dev_ops);
1574 snd_hda_codec_free(codec);
1577 EXPORT_SYMBOL_GPL(snd_hda_codec_new);
1579 int snd_hda_codec_update_widgets(struct hda_codec *codec)
1584 /* Assume the function group node does not change,
1585 * only the widget nodes may change.
1587 kfree(codec->wcaps);
1588 fg = codec->afg ? codec->afg : codec->mfg;
1589 err = read_widget_caps(codec, fg);
1591 codec_err(codec, "cannot malloc\n");
1595 snd_array_free(&codec->init_pins);
1596 err = read_pin_defaults(codec);
1600 EXPORT_SYMBOL_GPL(snd_hda_codec_update_widgets);
1603 #if IS_ENABLED(CONFIG_SND_HDA_CODEC_HDMI)
1604 /* if all audio out widgets are digital, let's assume the codec as a HDMI/DP */
1605 static bool is_likely_hdmi_codec(struct hda_codec *codec)
1607 hda_nid_t nid = codec->start_nid;
1610 for (i = 0; i < codec->num_nodes; i++, nid++) {
1611 unsigned int wcaps = get_wcaps(codec, nid);
1612 switch (get_wcaps_type(wcaps)) {
1614 return false; /* HDMI parser supports only HDMI out */
1615 case AC_WID_AUD_OUT:
1616 if (!(wcaps & AC_WCAP_DIGITAL))
1624 /* no HDMI codec parser support */
1625 #define is_likely_hdmi_codec(codec) false
1626 #endif /* CONFIG_SND_HDA_CODEC_HDMI */
1629 * snd_hda_codec_configure - (Re-)configure the HD-audio codec
1630 * @codec: the HDA codec
1632 * Start parsing of the given codec tree and (re-)initialize the whole
1635 * Returns 0 if successful or a negative error code.
1637 int snd_hda_codec_configure(struct hda_codec *codec)
1639 int (*patch)(struct hda_codec *) = NULL;
1642 codec->preset = find_codec_preset(codec);
1643 if (!codec->vendor_name || !codec->chip_name) {
1644 err = get_codec_name(codec);
1649 if (!is_generic_config(codec) && codec->preset)
1650 patch = codec->preset->patch;
1652 unload_parser(codec); /* to be sure */
1653 if (is_likely_hdmi_codec(codec)) {
1654 #if IS_MODULE(CONFIG_SND_HDA_CODEC_HDMI)
1655 patch = load_parser(codec, snd_hda_parse_hdmi_codec);
1656 #elif IS_BUILTIN(CONFIG_SND_HDA_CODEC_HDMI)
1657 patch = snd_hda_parse_hdmi_codec;
1661 #if IS_MODULE(CONFIG_SND_HDA_GENERIC)
1662 patch = load_parser(codec, snd_hda_parse_generic_codec);
1663 #elif IS_BUILTIN(CONFIG_SND_HDA_GENERIC)
1664 patch = snd_hda_parse_generic_codec;
1668 codec_err(codec, "No codec parser is available\n");
1675 unload_parser(codec);
1679 if (codec->patch_ops.unsol_event) {
1680 err = init_unsol_queue(codec->bus);
1685 /* audio codec should override the mixer name */
1686 if (codec->afg || !*codec->bus->card->mixername)
1687 snprintf(codec->bus->card->mixername,
1688 sizeof(codec->bus->card->mixername),
1689 "%s %s", codec->vendor_name, codec->chip_name);
1692 EXPORT_SYMBOL_GPL(snd_hda_codec_configure);
1694 /* update the stream-id if changed */
1695 static void update_pcm_stream_id(struct hda_codec *codec,
1696 struct hda_cvt_setup *p, hda_nid_t nid,
1697 u32 stream_tag, int channel_id)
1699 unsigned int oldval, newval;
1701 if (p->stream_tag != stream_tag || p->channel_id != channel_id) {
1702 oldval = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0);
1703 newval = (stream_tag << 4) | channel_id;
1704 if (oldval != newval)
1705 snd_hda_codec_write(codec, nid, 0,
1706 AC_VERB_SET_CHANNEL_STREAMID,
1708 p->stream_tag = stream_tag;
1709 p->channel_id = channel_id;
1713 /* update the format-id if changed */
1714 static void update_pcm_format(struct hda_codec *codec, struct hda_cvt_setup *p,
1715 hda_nid_t nid, int format)
1717 unsigned int oldval;
1719 if (p->format_id != format) {
1720 oldval = snd_hda_codec_read(codec, nid, 0,
1721 AC_VERB_GET_STREAM_FORMAT, 0);
1722 if (oldval != format) {
1724 snd_hda_codec_write(codec, nid, 0,
1725 AC_VERB_SET_STREAM_FORMAT,
1728 p->format_id = format;
1733 * snd_hda_codec_setup_stream - set up the codec for streaming
1734 * @codec: the CODEC to set up
1735 * @nid: the NID to set up
1736 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
1737 * @channel_id: channel id to pass, zero based.
1738 * @format: stream format.
1740 void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
1742 int channel_id, int format)
1744 struct hda_codec *c;
1745 struct hda_cvt_setup *p;
1753 "hda_codec_setup_stream: NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
1754 nid, stream_tag, channel_id, format);
1755 p = get_hda_cvt_setup(codec, nid);
1759 if (codec->pcm_format_first)
1760 update_pcm_format(codec, p, nid, format);
1761 update_pcm_stream_id(codec, p, nid, stream_tag, channel_id);
1762 if (!codec->pcm_format_first)
1763 update_pcm_format(codec, p, nid, format);
1768 /* make other inactive cvts with the same stream-tag dirty */
1769 type = get_wcaps_type(get_wcaps(codec, nid));
1770 list_for_each_entry(c, &codec->bus->codec_list, list) {
1771 for (i = 0; i < c->cvt_setups.used; i++) {
1772 p = snd_array_elem(&c->cvt_setups, i);
1773 if (!p->active && p->stream_tag == stream_tag &&
1774 get_wcaps_type(get_wcaps(c, p->nid)) == type)
1779 EXPORT_SYMBOL_GPL(snd_hda_codec_setup_stream);
1781 static void really_cleanup_stream(struct hda_codec *codec,
1782 struct hda_cvt_setup *q);
1785 * __snd_hda_codec_cleanup_stream - clean up the codec for closing
1786 * @codec: the CODEC to clean up
1787 * @nid: the NID to clean up
1788 * @do_now: really clean up the stream instead of clearing the active flag
1790 void __snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid,
1793 struct hda_cvt_setup *p;
1798 if (codec->no_sticky_stream)
1801 codec_dbg(codec, "hda_codec_cleanup_stream: NID=0x%x\n", nid);
1802 p = get_hda_cvt_setup(codec, nid);
1804 /* here we just clear the active flag when do_now isn't set;
1805 * actual clean-ups will be done later in
1806 * purify_inactive_streams() called from snd_hda_codec_prpapre()
1809 really_cleanup_stream(codec, p);
1814 EXPORT_SYMBOL_GPL(__snd_hda_codec_cleanup_stream);
1816 static void really_cleanup_stream(struct hda_codec *codec,
1817 struct hda_cvt_setup *q)
1819 hda_nid_t nid = q->nid;
1820 if (q->stream_tag || q->channel_id)
1821 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
1823 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0
1825 memset(q, 0, sizeof(*q));
1829 /* clean up the all conflicting obsolete streams */
1830 static void purify_inactive_streams(struct hda_codec *codec)
1832 struct hda_codec *c;
1835 list_for_each_entry(c, &codec->bus->codec_list, list) {
1836 for (i = 0; i < c->cvt_setups.used; i++) {
1837 struct hda_cvt_setup *p;
1838 p = snd_array_elem(&c->cvt_setups, i);
1840 really_cleanup_stream(c, p);
1846 /* clean up all streams; called from suspend */
1847 static void hda_cleanup_all_streams(struct hda_codec *codec)
1851 for (i = 0; i < codec->cvt_setups.used; i++) {
1852 struct hda_cvt_setup *p = snd_array_elem(&codec->cvt_setups, i);
1854 really_cleanup_stream(codec, p);
1860 * amp access functions
1863 /* FIXME: more better hash key? */
1864 #define HDA_HASH_KEY(nid, dir, idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
1865 #define HDA_HASH_PINCAP_KEY(nid) (u32)((nid) + (0x02 << 24))
1866 #define HDA_HASH_PARPCM_KEY(nid) (u32)((nid) + (0x03 << 24))
1867 #define HDA_HASH_PARSTR_KEY(nid) (u32)((nid) + (0x04 << 24))
1868 #define INFO_AMP_CAPS (1<<0)
1869 #define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
1871 /* initialize the hash table */
1872 static void init_hda_cache(struct hda_cache_rec *cache,
1873 unsigned int record_size)
1875 memset(cache, 0, sizeof(*cache));
1876 memset(cache->hash, 0xff, sizeof(cache->hash));
1877 snd_array_init(&cache->buf, record_size, 64);
1880 static void free_hda_cache(struct hda_cache_rec *cache)
1882 snd_array_free(&cache->buf);
1885 /* query the hash. allocate an entry if not found. */
1886 static struct hda_cache_head *get_hash(struct hda_cache_rec *cache, u32 key)
1888 u16 idx = key % (u16)ARRAY_SIZE(cache->hash);
1889 u16 cur = cache->hash[idx];
1890 struct hda_cache_head *info;
1892 while (cur != 0xffff) {
1893 info = snd_array_elem(&cache->buf, cur);
1894 if (info->key == key)
1901 /* query the hash. allocate an entry if not found. */
1902 static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache,
1905 struct hda_cache_head *info = get_hash(cache, key);
1908 /* add a new hash entry */
1909 info = snd_array_new(&cache->buf);
1912 cur = snd_array_index(&cache->buf, info);
1916 idx = key % (u16)ARRAY_SIZE(cache->hash);
1917 info->next = cache->hash[idx];
1918 cache->hash[idx] = cur;
1923 /* query and allocate an amp hash entry */
1924 static inline struct hda_amp_info *
1925 get_alloc_amp_hash(struct hda_codec *codec, u32 key)
1927 return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key);
1930 /* overwrite the value with the key in the caps hash */
1931 static int write_caps_hash(struct hda_codec *codec, u32 key, unsigned int val)
1933 struct hda_amp_info *info;
1935 mutex_lock(&codec->hash_mutex);
1936 info = get_alloc_amp_hash(codec, key);
1938 mutex_unlock(&codec->hash_mutex);
1941 info->amp_caps = val;
1942 info->head.val |= INFO_AMP_CAPS;
1943 mutex_unlock(&codec->hash_mutex);
1947 /* query the value from the caps hash; if not found, fetch the current
1948 * value from the given function and store in the hash
1951 query_caps_hash(struct hda_codec *codec, hda_nid_t nid, int dir, u32 key,
1952 unsigned int (*func)(struct hda_codec *, hda_nid_t, int))
1954 struct hda_amp_info *info;
1957 mutex_lock(&codec->hash_mutex);
1958 info = get_alloc_amp_hash(codec, key);
1960 mutex_unlock(&codec->hash_mutex);
1963 if (!(info->head.val & INFO_AMP_CAPS)) {
1964 mutex_unlock(&codec->hash_mutex); /* for reentrance */
1965 val = func(codec, nid, dir);
1966 write_caps_hash(codec, key, val);
1968 val = info->amp_caps;
1969 mutex_unlock(&codec->hash_mutex);
1974 static unsigned int read_amp_cap(struct hda_codec *codec, hda_nid_t nid,
1977 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
1979 return snd_hda_param_read(codec, nid,
1980 direction == HDA_OUTPUT ?
1981 AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP);
1985 * query_amp_caps - query AMP capabilities
1986 * @codec: the HD-auio codec
1987 * @nid: the NID to query
1988 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1990 * Query AMP capabilities for the given widget and direction.
1991 * Returns the obtained capability bits.
1993 * When cap bits have been already read, this doesn't read again but
1994 * returns the cached value.
1996 u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
1998 return query_caps_hash(codec, nid, direction,
1999 HDA_HASH_KEY(nid, direction, 0),
2002 EXPORT_SYMBOL_GPL(query_amp_caps);
2005 * snd_hda_check_amp_caps - query AMP capabilities
2006 * @codec: the HD-audio codec
2007 * @nid: the NID to query
2008 * @dir: either #HDA_INPUT or #HDA_OUTPUT
2010 * Check whether the widget has the given amp capability for the direction.
2012 bool snd_hda_check_amp_caps(struct hda_codec *codec, hda_nid_t nid,
2013 int dir, unsigned int bits)
2017 if (get_wcaps(codec, nid) & (1 << (dir + 1)))
2018 if (query_amp_caps(codec, nid, dir) & bits)
2022 EXPORT_SYMBOL_GPL(snd_hda_check_amp_caps);
2025 * snd_hda_override_amp_caps - Override the AMP capabilities
2026 * @codec: the CODEC to clean up
2027 * @nid: the NID to clean up
2028 * @direction: either #HDA_INPUT or #HDA_OUTPUT
2029 * @caps: the capability bits to set
2031 * Override the cached AMP caps bits value by the given one.
2032 * This function is useful if the driver needs to adjust the AMP ranges,
2033 * e.g. limit to 0dB, etc.
2035 * Returns zero if successful or a negative error code.
2037 int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
2040 return write_caps_hash(codec, HDA_HASH_KEY(nid, dir, 0), caps);
2042 EXPORT_SYMBOL_GPL(snd_hda_override_amp_caps);
2044 static unsigned int read_pin_cap(struct hda_codec *codec, hda_nid_t nid,
2047 return snd_hda_param_read(codec, nid, AC_PAR_PIN_CAP);
2051 * snd_hda_query_pin_caps - Query PIN capabilities
2052 * @codec: the HD-auio codec
2053 * @nid: the NID to query
2055 * Query PIN capabilities for the given widget.
2056 * Returns the obtained capability bits.
2058 * When cap bits have been already read, this doesn't read again but
2059 * returns the cached value.
2061 u32 snd_hda_query_pin_caps(struct hda_codec *codec, hda_nid_t nid)
2063 return query_caps_hash(codec, nid, 0, HDA_HASH_PINCAP_KEY(nid),
2066 EXPORT_SYMBOL_GPL(snd_hda_query_pin_caps);
2069 * snd_hda_override_pin_caps - Override the pin capabilities
2071 * @nid: the NID to override
2072 * @caps: the capability bits to set
2074 * Override the cached PIN capabilitiy bits value by the given one.
2076 * Returns zero if successful or a negative error code.
2078 int snd_hda_override_pin_caps(struct hda_codec *codec, hda_nid_t nid,
2081 return write_caps_hash(codec, HDA_HASH_PINCAP_KEY(nid), caps);
2083 EXPORT_SYMBOL_GPL(snd_hda_override_pin_caps);
2085 /* read or sync the hash value with the current value;
2086 * call within hash_mutex
2088 static struct hda_amp_info *
2089 update_amp_hash(struct hda_codec *codec, hda_nid_t nid, int ch,
2090 int direction, int index, bool init_only)
2092 struct hda_amp_info *info;
2093 unsigned int parm, val = 0;
2094 bool val_read = false;
2097 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
2100 if (!(info->head.val & INFO_AMP_VOL(ch))) {
2102 mutex_unlock(&codec->hash_mutex);
2103 parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
2104 parm |= direction == HDA_OUTPUT ?
2105 AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
2107 val = snd_hda_codec_read(codec, nid, 0,
2108 AC_VERB_GET_AMP_GAIN_MUTE, parm);
2111 mutex_lock(&codec->hash_mutex);
2114 info->vol[ch] = val;
2115 info->head.val |= INFO_AMP_VOL(ch);
2116 } else if (init_only)
2122 * write the current volume in info to the h/w
2124 static void put_vol_mute(struct hda_codec *codec, unsigned int amp_caps,
2125 hda_nid_t nid, int ch, int direction, int index,
2130 parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
2131 parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
2132 parm |= index << AC_AMP_SET_INDEX_SHIFT;
2133 if ((val & HDA_AMP_MUTE) && !(amp_caps & AC_AMPCAP_MUTE) &&
2134 (amp_caps & AC_AMPCAP_MIN_MUTE))
2135 ; /* set the zero value as a fake mute */
2138 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
2142 * snd_hda_codec_amp_read - Read AMP value
2143 * @codec: HD-audio codec
2144 * @nid: NID to read the AMP value
2145 * @ch: channel (left=0 or right=1)
2146 * @direction: #HDA_INPUT or #HDA_OUTPUT
2147 * @index: the index value (only for input direction)
2149 * Read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
2151 int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
2152 int direction, int index)
2154 struct hda_amp_info *info;
2155 unsigned int val = 0;
2157 mutex_lock(&codec->hash_mutex);
2158 info = update_amp_hash(codec, nid, ch, direction, index, false);
2160 val = info->vol[ch];
2161 mutex_unlock(&codec->hash_mutex);
2164 EXPORT_SYMBOL_GPL(snd_hda_codec_amp_read);
2166 static int codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
2167 int direction, int idx, int mask, int val,
2170 struct hda_amp_info *info;
2172 unsigned int cache_only;
2174 if (snd_BUG_ON(mask & ~0xff))
2178 mutex_lock(&codec->hash_mutex);
2179 info = update_amp_hash(codec, nid, ch, direction, idx, init_only);
2181 mutex_unlock(&codec->hash_mutex);
2184 val |= info->vol[ch] & ~mask;
2185 if (info->vol[ch] == val) {
2186 mutex_unlock(&codec->hash_mutex);
2189 info->vol[ch] = val;
2190 cache_only = info->head.dirty = codec->cached_write;
2191 caps = info->amp_caps;
2192 mutex_unlock(&codec->hash_mutex);
2194 put_vol_mute(codec, caps, nid, ch, direction, idx, val);
2199 * snd_hda_codec_amp_update - update the AMP value
2200 * @codec: HD-audio codec
2201 * @nid: NID to read the AMP value
2202 * @ch: channel (left=0 or right=1)
2203 * @direction: #HDA_INPUT or #HDA_OUTPUT
2204 * @idx: the index value (only for input direction)
2205 * @mask: bit mask to set
2206 * @val: the bits value to set
2208 * Update the AMP value with a bit mask.
2209 * Returns 0 if the value is unchanged, 1 if changed.
2211 int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
2212 int direction, int idx, int mask, int val)
2214 return codec_amp_update(codec, nid, ch, direction, idx, mask, val, false);
2216 EXPORT_SYMBOL_GPL(snd_hda_codec_amp_update);
2219 * snd_hda_codec_amp_stereo - update the AMP stereo values
2220 * @codec: HD-audio codec
2221 * @nid: NID to read the AMP value
2222 * @direction: #HDA_INPUT or #HDA_OUTPUT
2223 * @idx: the index value (only for input direction)
2224 * @mask: bit mask to set
2225 * @val: the bits value to set
2227 * Update the AMP values like snd_hda_codec_amp_update(), but for a
2228 * stereo widget with the same mask and value.
2230 int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
2231 int direction, int idx, int mask, int val)
2235 if (snd_BUG_ON(mask & ~0xff))
2237 for (ch = 0; ch < 2; ch++)
2238 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
2242 EXPORT_SYMBOL_GPL(snd_hda_codec_amp_stereo);
2244 /* Works like snd_hda_codec_amp_update() but it writes the value only at
2245 * the first access. If the amp was already initialized / updated beforehand,
2246 * this does nothing.
2248 int snd_hda_codec_amp_init(struct hda_codec *codec, hda_nid_t nid, int ch,
2249 int dir, int idx, int mask, int val)
2251 return codec_amp_update(codec, nid, ch, dir, idx, mask, val, true);
2253 EXPORT_SYMBOL_GPL(snd_hda_codec_amp_init);
2255 int snd_hda_codec_amp_init_stereo(struct hda_codec *codec, hda_nid_t nid,
2256 int dir, int idx, int mask, int val)
2260 if (snd_BUG_ON(mask & ~0xff))
2262 for (ch = 0; ch < 2; ch++)
2263 ret |= snd_hda_codec_amp_init(codec, nid, ch, dir,
2267 EXPORT_SYMBOL_GPL(snd_hda_codec_amp_init_stereo);
2270 * snd_hda_codec_resume_amp - Resume all AMP commands from the cache
2271 * @codec: HD-audio codec
2273 * Resume the all amp commands from the cache.
2275 void snd_hda_codec_resume_amp(struct hda_codec *codec)
2279 mutex_lock(&codec->hash_mutex);
2280 codec->cached_write = 0;
2281 for (i = 0; i < codec->amp_cache.buf.used; i++) {
2282 struct hda_amp_info *buffer;
2285 unsigned int idx, dir, ch;
2286 struct hda_amp_info info;
2288 buffer = snd_array_elem(&codec->amp_cache.buf, i);
2289 if (!buffer->head.dirty)
2291 buffer->head.dirty = 0;
2293 key = info.head.key;
2297 idx = (key >> 16) & 0xff;
2298 dir = (key >> 24) & 0xff;
2299 for (ch = 0; ch < 2; ch++) {
2300 if (!(info.head.val & INFO_AMP_VOL(ch)))
2302 mutex_unlock(&codec->hash_mutex);
2303 put_vol_mute(codec, info.amp_caps, nid, ch, dir, idx,
2305 mutex_lock(&codec->hash_mutex);
2308 mutex_unlock(&codec->hash_mutex);
2310 EXPORT_SYMBOL_GPL(snd_hda_codec_resume_amp);
2312 static u32 get_amp_max_value(struct hda_codec *codec, hda_nid_t nid, int dir,
2315 u32 caps = query_amp_caps(codec, nid, dir);
2317 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
2324 * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer
2326 * The control element is supposed to have the private_value field
2327 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2329 int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
2330 struct snd_ctl_elem_info *uinfo)
2332 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2333 u16 nid = get_amp_nid(kcontrol);
2334 u8 chs = get_amp_channels(kcontrol);
2335 int dir = get_amp_direction(kcontrol);
2336 unsigned int ofs = get_amp_offset(kcontrol);
2338 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2339 uinfo->count = chs == 3 ? 2 : 1;
2340 uinfo->value.integer.min = 0;
2341 uinfo->value.integer.max = get_amp_max_value(codec, nid, dir, ofs);
2342 if (!uinfo->value.integer.max) {
2344 "num_steps = 0 for NID=0x%x (ctl = %s)\n",
2345 nid, kcontrol->id.name);
2350 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_info);
2353 static inline unsigned int
2354 read_amp_value(struct hda_codec *codec, hda_nid_t nid,
2355 int ch, int dir, int idx, unsigned int ofs)
2358 val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
2359 val &= HDA_AMP_VOLMASK;
2368 update_amp_value(struct hda_codec *codec, hda_nid_t nid,
2369 int ch, int dir, int idx, unsigned int ofs,
2372 unsigned int maxval;
2376 /* ofs = 0: raw max value */
2377 maxval = get_amp_max_value(codec, nid, dir, 0);
2380 return snd_hda_codec_amp_update(codec, nid, ch, dir, idx,
2381 HDA_AMP_VOLMASK, val);
2385 * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume
2387 * The control element is supposed to have the private_value field
2388 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2390 int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
2391 struct snd_ctl_elem_value *ucontrol)
2393 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2394 hda_nid_t nid = get_amp_nid(kcontrol);
2395 int chs = get_amp_channels(kcontrol);
2396 int dir = get_amp_direction(kcontrol);
2397 int idx = get_amp_index(kcontrol);
2398 unsigned int ofs = get_amp_offset(kcontrol);
2399 long *valp = ucontrol->value.integer.value;
2402 *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs);
2404 *valp = read_amp_value(codec, nid, 1, dir, idx, ofs);
2407 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_get);
2410 * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume
2412 * The control element is supposed to have the private_value field
2413 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2415 int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
2416 struct snd_ctl_elem_value *ucontrol)
2418 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2419 hda_nid_t nid = get_amp_nid(kcontrol);
2420 int chs = get_amp_channels(kcontrol);
2421 int dir = get_amp_direction(kcontrol);
2422 int idx = get_amp_index(kcontrol);
2423 unsigned int ofs = get_amp_offset(kcontrol);
2424 long *valp = ucontrol->value.integer.value;
2427 snd_hda_power_up(codec);
2429 change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp);
2433 change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp);
2434 snd_hda_power_down(codec);
2437 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_put);
2440 * snd_hda_mixer_amp_volume_put - TLV callback for a standard AMP mixer volume
2442 * The control element is supposed to have the private_value field
2443 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2445 int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2446 unsigned int size, unsigned int __user *_tlv)
2448 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2449 hda_nid_t nid = get_amp_nid(kcontrol);
2450 int dir = get_amp_direction(kcontrol);
2451 unsigned int ofs = get_amp_offset(kcontrol);
2452 bool min_mute = get_amp_min_mute(kcontrol);
2453 u32 caps, val1, val2;
2455 if (size < 4 * sizeof(unsigned int))
2457 caps = query_amp_caps(codec, nid, dir);
2458 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2459 val2 = (val2 + 1) * 25;
2460 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
2462 val1 = ((int)val1) * ((int)val2);
2463 if (min_mute || (caps & AC_AMPCAP_MIN_MUTE))
2464 val2 |= TLV_DB_SCALE_MUTE;
2465 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
2467 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
2469 if (put_user(val1, _tlv + 2))
2471 if (put_user(val2, _tlv + 3))
2475 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_tlv);
2478 * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control
2479 * @codec: HD-audio codec
2480 * @nid: NID of a reference widget
2481 * @dir: #HDA_INPUT or #HDA_OUTPUT
2482 * @tlv: TLV data to be stored, at least 4 elements
2484 * Set (static) TLV data for a virtual master volume using the AMP caps
2485 * obtained from the reference NID.
2486 * The volume range is recalculated as if the max volume is 0dB.
2488 void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
2494 caps = query_amp_caps(codec, nid, dir);
2495 nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
2496 step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2497 step = (step + 1) * 25;
2498 tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
2499 tlv[1] = 2 * sizeof(unsigned int);
2500 tlv[2] = -nums * step;
2503 EXPORT_SYMBOL_GPL(snd_hda_set_vmaster_tlv);
2505 /* find a mixer control element with the given name */
2506 static struct snd_kcontrol *
2507 find_mixer_ctl(struct hda_codec *codec, const char *name, int dev, int idx)
2509 struct snd_ctl_elem_id id;
2510 memset(&id, 0, sizeof(id));
2511 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
2514 if (snd_BUG_ON(strlen(name) >= sizeof(id.name)))
2516 strcpy(id.name, name);
2517 return snd_ctl_find_id(codec->bus->card, &id);
2521 * snd_hda_find_mixer_ctl - Find a mixer control element with the given name
2522 * @codec: HD-audio codec
2523 * @name: ctl id name string
2525 * Get the control element with the given id string and IFACE_MIXER.
2527 struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
2530 return find_mixer_ctl(codec, name, 0, 0);
2532 EXPORT_SYMBOL_GPL(snd_hda_find_mixer_ctl);
2534 static int find_empty_mixer_ctl_idx(struct hda_codec *codec, const char *name,
2538 /* 16 ctlrs should be large enough */
2539 for (i = 0, idx = start_idx; i < 16; i++, idx++) {
2540 if (!find_mixer_ctl(codec, name, 0, idx))
2547 * snd_hda_ctl_add - Add a control element and assign to the codec
2548 * @codec: HD-audio codec
2549 * @nid: corresponding NID (optional)
2550 * @kctl: the control element to assign
2552 * Add the given control element to an array inside the codec instance.
2553 * All control elements belonging to a codec are supposed to be added
2554 * by this function so that a proper clean-up works at the free or
2555 * reconfiguration time.
2557 * If non-zero @nid is passed, the NID is assigned to the control element.
2558 * The assignment is shown in the codec proc file.
2560 * snd_hda_ctl_add() checks the control subdev id field whether
2561 * #HDA_SUBDEV_NID_FLAG bit is set. If set (and @nid is zero), the lower
2562 * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit
2563 * specifies if kctl->private_value is a HDA amplifier value.
2565 int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid,
2566 struct snd_kcontrol *kctl)
2569 unsigned short flags = 0;
2570 struct hda_nid_item *item;
2572 if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) {
2573 flags |= HDA_NID_ITEM_AMP;
2575 nid = get_amp_nid_(kctl->private_value);
2577 if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0)
2578 nid = kctl->id.subdevice & 0xffff;
2579 if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG))
2580 kctl->id.subdevice = 0;
2581 err = snd_ctl_add(codec->bus->card, kctl);
2584 item = snd_array_new(&codec->mixers);
2589 item->flags = flags;
2592 EXPORT_SYMBOL_GPL(snd_hda_ctl_add);
2595 * snd_hda_add_nid - Assign a NID to a control element
2596 * @codec: HD-audio codec
2597 * @nid: corresponding NID (optional)
2598 * @kctl: the control element to assign
2599 * @index: index to kctl
2601 * Add the given control element to an array inside the codec instance.
2602 * This function is used when #snd_hda_ctl_add cannot be used for 1:1
2603 * NID:KCTL mapping - for example "Capture Source" selector.
2605 int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl,
2606 unsigned int index, hda_nid_t nid)
2608 struct hda_nid_item *item;
2611 item = snd_array_new(&codec->nids);
2615 item->index = index;
2619 codec_err(codec, "no NID for mapping control %s:%d:%d\n",
2620 kctl->id.name, kctl->id.index, index);
2623 EXPORT_SYMBOL_GPL(snd_hda_add_nid);
2626 * snd_hda_ctls_clear - Clear all controls assigned to the given codec
2627 * @codec: HD-audio codec
2629 void snd_hda_ctls_clear(struct hda_codec *codec)
2632 struct hda_nid_item *items = codec->mixers.list;
2633 for (i = 0; i < codec->mixers.used; i++)
2634 snd_ctl_remove(codec->bus->card, items[i].kctl);
2635 snd_array_free(&codec->mixers);
2636 snd_array_free(&codec->nids);
2639 /* pseudo device locking
2640 * toggle card->shutdown to allow/disallow the device access (as a hack)
2642 int snd_hda_lock_devices(struct hda_bus *bus)
2644 struct snd_card *card = bus->card;
2645 struct hda_codec *codec;
2647 spin_lock(&card->files_lock);
2651 if (!list_empty(&card->ctl_files))
2654 list_for_each_entry(codec, &bus->codec_list, list) {
2656 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
2657 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
2660 if (cpcm->pcm->streams[0].substream_opened ||
2661 cpcm->pcm->streams[1].substream_opened)
2665 spin_unlock(&card->files_lock);
2671 spin_unlock(&card->files_lock);
2674 EXPORT_SYMBOL_GPL(snd_hda_lock_devices);
2676 void snd_hda_unlock_devices(struct hda_bus *bus)
2678 struct snd_card *card = bus->card;
2681 spin_lock(&card->files_lock);
2683 spin_unlock(&card->files_lock);
2685 EXPORT_SYMBOL_GPL(snd_hda_unlock_devices);
2688 * snd_hda_codec_reset - Clear all objects assigned to the codec
2689 * @codec: HD-audio codec
2691 * This frees the all PCM and control elements assigned to the codec, and
2692 * clears the caches and restores the pin default configurations.
2694 * When a device is being used, it returns -EBSY. If successfully freed,
2697 int snd_hda_codec_reset(struct hda_codec *codec)
2699 struct hda_bus *bus = codec->bus;
2700 struct snd_card *card = bus->card;
2703 if (snd_hda_lock_devices(bus) < 0)
2706 /* OK, let it free */
2707 cancel_delayed_work_sync(&codec->jackpoll_work);
2709 cancel_delayed_work_sync(&codec->power_work);
2710 flush_workqueue(bus->workq);
2712 snd_hda_ctls_clear(codec);
2714 for (i = 0; i < codec->num_pcms; i++) {
2715 if (codec->pcm_info[i].pcm) {
2716 snd_device_free(card, codec->pcm_info[i].pcm);
2717 clear_bit(codec->pcm_info[i].device,
2721 snd_hda_detach_beep_device(codec);
2722 if (codec->patch_ops.free)
2723 codec->patch_ops.free(codec);
2724 memset(&codec->patch_ops, 0, sizeof(codec->patch_ops));
2725 snd_hda_jack_tbl_clear(codec);
2726 codec->proc_widget_hook = NULL;
2728 free_hda_cache(&codec->amp_cache);
2729 free_hda_cache(&codec->cmd_cache);
2730 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
2731 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
2732 /* free only driver_pins so that init_pins + user_pins are restored */
2733 snd_array_free(&codec->driver_pins);
2734 snd_array_free(&codec->cvt_setups);
2735 snd_array_free(&codec->spdif_out);
2736 snd_array_free(&codec->verbs);
2737 codec->num_pcms = 0;
2738 codec->pcm_info = NULL;
2739 codec->preset = NULL;
2740 codec->slave_dig_outs = NULL;
2741 codec->spdif_status_reset = 0;
2742 unload_parser(codec);
2743 module_put(codec->owner);
2744 codec->owner = NULL;
2746 /* allow device access again */
2747 snd_hda_unlock_devices(bus);
2751 typedef int (*map_slave_func_t)(struct hda_codec *, void *, struct snd_kcontrol *);
2753 /* apply the function to all matching slave ctls in the mixer list */
2754 static int map_slaves(struct hda_codec *codec, const char * const *slaves,
2755 const char *suffix, map_slave_func_t func, void *data)
2757 struct hda_nid_item *items;
2758 const char * const *s;
2761 items = codec->mixers.list;
2762 for (i = 0; i < codec->mixers.used; i++) {
2763 struct snd_kcontrol *sctl = items[i].kctl;
2764 if (!sctl || sctl->id.iface != SNDRV_CTL_ELEM_IFACE_MIXER)
2766 for (s = slaves; *s; s++) {
2767 char tmpname[sizeof(sctl->id.name)];
2768 const char *name = *s;
2770 snprintf(tmpname, sizeof(tmpname), "%s %s",
2774 if (!strcmp(sctl->id.name, name)) {
2775 err = func(codec, data, sctl);
2785 static int check_slave_present(struct hda_codec *codec,
2786 void *data, struct snd_kcontrol *sctl)
2791 /* guess the value corresponding to 0dB */
2792 static int get_kctl_0dB_offset(struct hda_codec *codec,
2793 struct snd_kcontrol *kctl, int *step_to_check)
2796 const int *tlv = NULL;
2799 if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
2800 /* FIXME: set_fs() hack for obtaining user-space TLV data */
2801 mm_segment_t fs = get_fs();
2803 if (!kctl->tlv.c(kctl, 0, sizeof(_tlv), _tlv))
2806 } else if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_READ)
2808 if (tlv && tlv[0] == SNDRV_CTL_TLVT_DB_SCALE) {
2810 step &= ~TLV_DB_SCALE_MUTE;
2813 if (*step_to_check && *step_to_check != step) {
2814 codec_err(codec, "Mismatching dB step for vmaster slave (%d!=%d)\n",
2815 - *step_to_check, step);
2818 *step_to_check = step;
2819 val = -tlv[2] / step;
2824 /* call kctl->put with the given value(s) */
2825 static int put_kctl_with_value(struct snd_kcontrol *kctl, int val)
2827 struct snd_ctl_elem_value *ucontrol;
2828 ucontrol = kzalloc(sizeof(*ucontrol), GFP_KERNEL);
2831 ucontrol->value.integer.value[0] = val;
2832 ucontrol->value.integer.value[1] = val;
2833 kctl->put(kctl, ucontrol);
2838 /* initialize the slave volume with 0dB */
2839 static int init_slave_0dB(struct hda_codec *codec,
2840 void *data, struct snd_kcontrol *slave)
2842 int offset = get_kctl_0dB_offset(codec, slave, data);
2844 put_kctl_with_value(slave, offset);
2848 /* unmute the slave */
2849 static int init_slave_unmute(struct hda_codec *codec,
2850 void *data, struct snd_kcontrol *slave)
2852 return put_kctl_with_value(slave, 1);
2855 static int add_slave(struct hda_codec *codec,
2856 void *data, struct snd_kcontrol *slave)
2858 return snd_ctl_add_slave(data, slave);
2862 * snd_hda_add_vmaster - create a virtual master control and add slaves
2863 * @codec: HD-audio codec
2864 * @name: vmaster control name
2865 * @tlv: TLV data (optional)
2866 * @slaves: slave control names (optional)
2867 * @suffix: suffix string to each slave name (optional)
2868 * @init_slave_vol: initialize slaves to unmute/0dB
2869 * @ctl_ret: store the vmaster kcontrol in return
2871 * Create a virtual master control with the given name. The TLV data
2872 * must be either NULL or a valid data.
2874 * @slaves is a NULL-terminated array of strings, each of which is a
2875 * slave control name. All controls with these names are assigned to
2876 * the new virtual master control.
2878 * This function returns zero if successful or a negative error code.
2880 int __snd_hda_add_vmaster(struct hda_codec *codec, char *name,
2881 unsigned int *tlv, const char * const *slaves,
2882 const char *suffix, bool init_slave_vol,
2883 struct snd_kcontrol **ctl_ret)
2885 struct snd_kcontrol *kctl;
2891 err = map_slaves(codec, slaves, suffix, check_slave_present, NULL);
2893 codec_dbg(codec, "No slave found for %s\n", name);
2896 kctl = snd_ctl_make_virtual_master(name, tlv);
2899 err = snd_hda_ctl_add(codec, 0, kctl);
2903 err = map_slaves(codec, slaves, suffix, add_slave, kctl);
2907 /* init with master mute & zero volume */
2908 put_kctl_with_value(kctl, 0);
2909 if (init_slave_vol) {
2911 map_slaves(codec, slaves, suffix,
2912 tlv ? init_slave_0dB : init_slave_unmute, &step);
2919 EXPORT_SYMBOL_GPL(__snd_hda_add_vmaster);
2922 * mute-LED control using vmaster
2924 static int vmaster_mute_mode_info(struct snd_kcontrol *kcontrol,
2925 struct snd_ctl_elem_info *uinfo)
2927 static const char * const texts[] = {
2928 "On", "Off", "Follow Master"
2931 return snd_ctl_enum_info(uinfo, 1, 3, texts);
2934 static int vmaster_mute_mode_get(struct snd_kcontrol *kcontrol,
2935 struct snd_ctl_elem_value *ucontrol)
2937 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2938 ucontrol->value.enumerated.item[0] = hook->mute_mode;
2942 static int vmaster_mute_mode_put(struct snd_kcontrol *kcontrol,
2943 struct snd_ctl_elem_value *ucontrol)
2945 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2946 unsigned int old_mode = hook->mute_mode;
2948 hook->mute_mode = ucontrol->value.enumerated.item[0];
2949 if (hook->mute_mode > HDA_VMUTE_FOLLOW_MASTER)
2950 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
2951 if (old_mode == hook->mute_mode)
2953 snd_hda_sync_vmaster_hook(hook);
2957 static struct snd_kcontrol_new vmaster_mute_mode = {
2958 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2959 .name = "Mute-LED Mode",
2960 .info = vmaster_mute_mode_info,
2961 .get = vmaster_mute_mode_get,
2962 .put = vmaster_mute_mode_put,
2966 * Add a mute-LED hook with the given vmaster switch kctl
2967 * "Mute-LED Mode" control is automatically created and associated with
2970 int snd_hda_add_vmaster_hook(struct hda_codec *codec,
2971 struct hda_vmaster_mute_hook *hook,
2972 bool expose_enum_ctl)
2974 struct snd_kcontrol *kctl;
2976 if (!hook->hook || !hook->sw_kctl)
2978 snd_ctl_add_vmaster_hook(hook->sw_kctl, hook->hook, codec);
2979 hook->codec = codec;
2980 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
2981 if (!expose_enum_ctl)
2983 kctl = snd_ctl_new1(&vmaster_mute_mode, hook);
2986 return snd_hda_ctl_add(codec, 0, kctl);
2988 EXPORT_SYMBOL_GPL(snd_hda_add_vmaster_hook);
2991 * Call the hook with the current value for synchronization
2992 * Should be called in init callback
2994 void snd_hda_sync_vmaster_hook(struct hda_vmaster_mute_hook *hook)
2996 if (!hook->hook || !hook->codec)
2998 /* don't call vmaster hook in the destructor since it might have
2999 * been already destroyed
3001 if (hook->codec->bus->shutdown)
3003 switch (hook->mute_mode) {
3004 case HDA_VMUTE_FOLLOW_MASTER:
3005 snd_ctl_sync_vmaster_hook(hook->sw_kctl);
3008 hook->hook(hook->codec, hook->mute_mode);
3012 EXPORT_SYMBOL_GPL(snd_hda_sync_vmaster_hook);
3016 * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch
3018 * The control element is supposed to have the private_value field
3019 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
3021 int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
3022 struct snd_ctl_elem_info *uinfo)
3024 int chs = get_amp_channels(kcontrol);
3026 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
3027 uinfo->count = chs == 3 ? 2 : 1;
3028 uinfo->value.integer.min = 0;
3029 uinfo->value.integer.max = 1;
3032 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_info);
3035 * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch
3037 * The control element is supposed to have the private_value field
3038 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
3040 int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
3041 struct snd_ctl_elem_value *ucontrol)
3043 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3044 hda_nid_t nid = get_amp_nid(kcontrol);
3045 int chs = get_amp_channels(kcontrol);
3046 int dir = get_amp_direction(kcontrol);
3047 int idx = get_amp_index(kcontrol);
3048 long *valp = ucontrol->value.integer.value;
3051 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
3052 HDA_AMP_MUTE) ? 0 : 1;
3054 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
3055 HDA_AMP_MUTE) ? 0 : 1;
3058 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_get);
3061 * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch
3063 * The control element is supposed to have the private_value field
3064 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
3066 int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
3067 struct snd_ctl_elem_value *ucontrol)
3069 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3070 hda_nid_t nid = get_amp_nid(kcontrol);
3071 int chs = get_amp_channels(kcontrol);
3072 int dir = get_amp_direction(kcontrol);
3073 int idx = get_amp_index(kcontrol);
3074 long *valp = ucontrol->value.integer.value;
3077 snd_hda_power_up(codec);
3079 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
3081 *valp ? 0 : HDA_AMP_MUTE);
3085 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
3087 *valp ? 0 : HDA_AMP_MUTE);
3088 hda_call_check_power_status(codec, nid);
3089 snd_hda_power_down(codec);
3092 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_put);
3095 * bound volume controls
3097 * bind multiple volumes (# indices, from 0)
3100 #define AMP_VAL_IDX_SHIFT 19
3101 #define AMP_VAL_IDX_MASK (0x0f<<19)
3104 * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control
3106 * The control element is supposed to have the private_value field
3107 * set up via HDA_BIND_MUTE*() macros.
3109 int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
3110 struct snd_ctl_elem_value *ucontrol)
3112 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3116 mutex_lock(&codec->control_mutex);
3117 pval = kcontrol->private_value;
3118 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
3119 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
3120 kcontrol->private_value = pval;
3121 mutex_unlock(&codec->control_mutex);
3124 EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_switch_get);
3127 * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control
3129 * The control element is supposed to have the private_value field
3130 * set up via HDA_BIND_MUTE*() macros.
3132 int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
3133 struct snd_ctl_elem_value *ucontrol)
3135 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3137 int i, indices, err = 0, change = 0;
3139 mutex_lock(&codec->control_mutex);
3140 pval = kcontrol->private_value;
3141 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
3142 for (i = 0; i < indices; i++) {
3143 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
3144 (i << AMP_VAL_IDX_SHIFT);
3145 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
3150 kcontrol->private_value = pval;
3151 mutex_unlock(&codec->control_mutex);
3152 return err < 0 ? err : change;
3154 EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_switch_put);
3157 * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control
3159 * The control element is supposed to have the private_value field
3160 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
3162 int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
3163 struct snd_ctl_elem_info *uinfo)
3165 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3166 struct hda_bind_ctls *c;
3169 mutex_lock(&codec->control_mutex);
3170 c = (struct hda_bind_ctls *)kcontrol->private_value;
3171 kcontrol->private_value = *c->values;
3172 err = c->ops->info(kcontrol, uinfo);
3173 kcontrol->private_value = (long)c;
3174 mutex_unlock(&codec->control_mutex);
3177 EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_info);
3180 * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control
3182 * The control element is supposed to have the private_value field
3183 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
3185 int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
3186 struct snd_ctl_elem_value *ucontrol)
3188 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3189 struct hda_bind_ctls *c;
3192 mutex_lock(&codec->control_mutex);
3193 c = (struct hda_bind_ctls *)kcontrol->private_value;
3194 kcontrol->private_value = *c->values;
3195 err = c->ops->get(kcontrol, ucontrol);
3196 kcontrol->private_value = (long)c;
3197 mutex_unlock(&codec->control_mutex);
3200 EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_get);
3203 * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control
3205 * The control element is supposed to have the private_value field
3206 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
3208 int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
3209 struct snd_ctl_elem_value *ucontrol)
3211 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3212 struct hda_bind_ctls *c;
3213 unsigned long *vals;
3214 int err = 0, change = 0;
3216 mutex_lock(&codec->control_mutex);
3217 c = (struct hda_bind_ctls *)kcontrol->private_value;
3218 for (vals = c->values; *vals; vals++) {
3219 kcontrol->private_value = *vals;
3220 err = c->ops->put(kcontrol, ucontrol);
3225 kcontrol->private_value = (long)c;
3226 mutex_unlock(&codec->control_mutex);
3227 return err < 0 ? err : change;
3229 EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_put);
3232 * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control
3234 * The control element is supposed to have the private_value field
3235 * set up via HDA_BIND_VOL() macro.
3237 int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
3238 unsigned int size, unsigned int __user *tlv)
3240 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3241 struct hda_bind_ctls *c;
3244 mutex_lock(&codec->control_mutex);
3245 c = (struct hda_bind_ctls *)kcontrol->private_value;
3246 kcontrol->private_value = *c->values;
3247 err = c->ops->tlv(kcontrol, op_flag, size, tlv);
3248 kcontrol->private_value = (long)c;
3249 mutex_unlock(&codec->control_mutex);
3252 EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_tlv);
3254 struct hda_ctl_ops snd_hda_bind_vol = {
3255 .info = snd_hda_mixer_amp_volume_info,
3256 .get = snd_hda_mixer_amp_volume_get,
3257 .put = snd_hda_mixer_amp_volume_put,
3258 .tlv = snd_hda_mixer_amp_tlv
3260 EXPORT_SYMBOL_GPL(snd_hda_bind_vol);
3262 struct hda_ctl_ops snd_hda_bind_sw = {
3263 .info = snd_hda_mixer_amp_switch_info,
3264 .get = snd_hda_mixer_amp_switch_get,
3265 .put = snd_hda_mixer_amp_switch_put,
3266 .tlv = snd_hda_mixer_amp_tlv
3268 EXPORT_SYMBOL_GPL(snd_hda_bind_sw);
3271 * SPDIF out controls
3274 static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
3275 struct snd_ctl_elem_info *uinfo)
3277 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
3282 static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
3283 struct snd_ctl_elem_value *ucontrol)
3285 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
3286 IEC958_AES0_NONAUDIO |
3287 IEC958_AES0_CON_EMPHASIS_5015 |
3288 IEC958_AES0_CON_NOT_COPYRIGHT;
3289 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
3290 IEC958_AES1_CON_ORIGINAL;
3294 static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
3295 struct snd_ctl_elem_value *ucontrol)
3297 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
3298 IEC958_AES0_NONAUDIO |
3299 IEC958_AES0_PRO_EMPHASIS_5015;
3303 static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
3304 struct snd_ctl_elem_value *ucontrol)
3306 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3307 int idx = kcontrol->private_value;
3308 struct hda_spdif_out *spdif;
3310 mutex_lock(&codec->spdif_mutex);
3311 spdif = snd_array_elem(&codec->spdif_out, idx);
3312 ucontrol->value.iec958.status[0] = spdif->status & 0xff;
3313 ucontrol->value.iec958.status[1] = (spdif->status >> 8) & 0xff;
3314 ucontrol->value.iec958.status[2] = (spdif->status >> 16) & 0xff;
3315 ucontrol->value.iec958.status[3] = (spdif->status >> 24) & 0xff;
3316 mutex_unlock(&codec->spdif_mutex);
3321 /* convert from SPDIF status bits to HDA SPDIF bits
3322 * bit 0 (DigEn) is always set zero (to be filled later)
3324 static unsigned short convert_from_spdif_status(unsigned int sbits)
3326 unsigned short val = 0;
3328 if (sbits & IEC958_AES0_PROFESSIONAL)
3329 val |= AC_DIG1_PROFESSIONAL;
3330 if (sbits & IEC958_AES0_NONAUDIO)
3331 val |= AC_DIG1_NONAUDIO;
3332 if (sbits & IEC958_AES0_PROFESSIONAL) {
3333 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
3334 IEC958_AES0_PRO_EMPHASIS_5015)
3335 val |= AC_DIG1_EMPHASIS;
3337 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
3338 IEC958_AES0_CON_EMPHASIS_5015)
3339 val |= AC_DIG1_EMPHASIS;
3340 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
3341 val |= AC_DIG1_COPYRIGHT;
3342 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
3343 val |= AC_DIG1_LEVEL;
3344 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
3349 /* convert to SPDIF status bits from HDA SPDIF bits
3351 static unsigned int convert_to_spdif_status(unsigned short val)
3353 unsigned int sbits = 0;
3355 if (val & AC_DIG1_NONAUDIO)
3356 sbits |= IEC958_AES0_NONAUDIO;
3357 if (val & AC_DIG1_PROFESSIONAL)
3358 sbits |= IEC958_AES0_PROFESSIONAL;
3359 if (sbits & IEC958_AES0_PROFESSIONAL) {
3360 if (val & AC_DIG1_EMPHASIS)
3361 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
3363 if (val & AC_DIG1_EMPHASIS)
3364 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
3365 if (!(val & AC_DIG1_COPYRIGHT))
3366 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
3367 if (val & AC_DIG1_LEVEL)
3368 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
3369 sbits |= val & (0x7f << 8);
3374 /* set digital convert verbs both for the given NID and its slaves */
3375 static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
3380 snd_hda_codec_write_cache(codec, nid, 0, verb, val);
3381 d = codec->slave_dig_outs;
3385 snd_hda_codec_write_cache(codec, *d, 0, verb, val);
3388 static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
3392 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1);
3394 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2);
3397 static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
3398 struct snd_ctl_elem_value *ucontrol)
3400 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3401 int idx = kcontrol->private_value;
3402 struct hda_spdif_out *spdif;
3407 mutex_lock(&codec->spdif_mutex);
3408 spdif = snd_array_elem(&codec->spdif_out, idx);
3410 spdif->status = ucontrol->value.iec958.status[0] |
3411 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
3412 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
3413 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
3414 val = convert_from_spdif_status(spdif->status);
3415 val |= spdif->ctls & 1;
3416 change = spdif->ctls != val;
3418 if (change && nid != (u16)-1)
3419 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
3420 mutex_unlock(&codec->spdif_mutex);
3424 #define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
3426 static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
3427 struct snd_ctl_elem_value *ucontrol)
3429 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3430 int idx = kcontrol->private_value;
3431 struct hda_spdif_out *spdif;
3433 mutex_lock(&codec->spdif_mutex);
3434 spdif = snd_array_elem(&codec->spdif_out, idx);
3435 ucontrol->value.integer.value[0] = spdif->ctls & AC_DIG1_ENABLE;
3436 mutex_unlock(&codec->spdif_mutex);
3440 static inline void set_spdif_ctls(struct hda_codec *codec, hda_nid_t nid,
3443 set_dig_out_convert(codec, nid, dig1, dig2);
3444 /* unmute amp switch (if any) */
3445 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
3446 (dig1 & AC_DIG1_ENABLE))
3447 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
3451 static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
3452 struct snd_ctl_elem_value *ucontrol)
3454 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3455 int idx = kcontrol->private_value;
3456 struct hda_spdif_out *spdif;
3461 mutex_lock(&codec->spdif_mutex);
3462 spdif = snd_array_elem(&codec->spdif_out, idx);
3464 val = spdif->ctls & ~AC_DIG1_ENABLE;
3465 if (ucontrol->value.integer.value[0])
3466 val |= AC_DIG1_ENABLE;
3467 change = spdif->ctls != val;
3469 if (change && nid != (u16)-1)
3470 set_spdif_ctls(codec, nid, val & 0xff, -1);
3471 mutex_unlock(&codec->spdif_mutex);
3475 static struct snd_kcontrol_new dig_mixes[] = {
3477 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3478 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3479 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
3480 .info = snd_hda_spdif_mask_info,
3481 .get = snd_hda_spdif_cmask_get,
3484 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3485 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3486 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK),
3487 .info = snd_hda_spdif_mask_info,
3488 .get = snd_hda_spdif_pmask_get,
3491 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3492 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
3493 .info = snd_hda_spdif_mask_info,
3494 .get = snd_hda_spdif_default_get,
3495 .put = snd_hda_spdif_default_put,
3498 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3499 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
3500 .info = snd_hda_spdif_out_switch_info,
3501 .get = snd_hda_spdif_out_switch_get,
3502 .put = snd_hda_spdif_out_switch_put,
3508 * snd_hda_create_dig_out_ctls - create Output SPDIF-related controls
3509 * @codec: the HDA codec
3510 * @associated_nid: NID that new ctls associated with
3511 * @cvt_nid: converter NID
3512 * @type: HDA_PCM_TYPE_*
3513 * Creates controls related with the digital output.
3514 * Called from each patch supporting the digital out.
3516 * Returns 0 if successful, or a negative error code.
3518 int snd_hda_create_dig_out_ctls(struct hda_codec *codec,
3519 hda_nid_t associated_nid,
3524 struct snd_kcontrol *kctl;
3525 struct snd_kcontrol_new *dig_mix;
3527 const int spdif_index = 16;
3528 struct hda_spdif_out *spdif;
3529 struct hda_bus *bus = codec->bus;
3531 if (bus->primary_dig_out_type == HDA_PCM_TYPE_HDMI &&
3532 type == HDA_PCM_TYPE_SPDIF) {
3534 } else if (bus->primary_dig_out_type == HDA_PCM_TYPE_SPDIF &&
3535 type == HDA_PCM_TYPE_HDMI) {
3536 /* suppose a single SPDIF device */
3537 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
3538 kctl = find_mixer_ctl(codec, dig_mix->name, 0, 0);
3541 kctl->id.index = spdif_index;
3543 bus->primary_dig_out_type = HDA_PCM_TYPE_HDMI;
3545 if (!bus->primary_dig_out_type)
3546 bus->primary_dig_out_type = type;
3548 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Playback Switch", idx);
3550 codec_err(codec, "too many IEC958 outputs\n");
3553 spdif = snd_array_new(&codec->spdif_out);
3556 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
3557 kctl = snd_ctl_new1(dig_mix, codec);
3560 kctl->id.index = idx;
3561 kctl->private_value = codec->spdif_out.used - 1;
3562 err = snd_hda_ctl_add(codec, associated_nid, kctl);
3566 spdif->nid = cvt_nid;
3567 spdif->ctls = snd_hda_codec_read(codec, cvt_nid, 0,
3568 AC_VERB_GET_DIGI_CONVERT_1, 0);
3569 spdif->status = convert_to_spdif_status(spdif->ctls);
3572 EXPORT_SYMBOL_GPL(snd_hda_create_dig_out_ctls);
3574 /* get the hda_spdif_out entry from the given NID
3575 * call within spdif_mutex lock
3577 struct hda_spdif_out *snd_hda_spdif_out_of_nid(struct hda_codec *codec,
3581 for (i = 0; i < codec->spdif_out.used; i++) {
3582 struct hda_spdif_out *spdif =
3583 snd_array_elem(&codec->spdif_out, i);
3584 if (spdif->nid == nid)
3589 EXPORT_SYMBOL_GPL(snd_hda_spdif_out_of_nid);
3591 void snd_hda_spdif_ctls_unassign(struct hda_codec *codec, int idx)
3593 struct hda_spdif_out *spdif;
3595 mutex_lock(&codec->spdif_mutex);
3596 spdif = snd_array_elem(&codec->spdif_out, idx);
3597 spdif->nid = (u16)-1;
3598 mutex_unlock(&codec->spdif_mutex);
3600 EXPORT_SYMBOL_GPL(snd_hda_spdif_ctls_unassign);
3602 void snd_hda_spdif_ctls_assign(struct hda_codec *codec, int idx, hda_nid_t nid)
3604 struct hda_spdif_out *spdif;
3607 mutex_lock(&codec->spdif_mutex);
3608 spdif = snd_array_elem(&codec->spdif_out, idx);
3609 if (spdif->nid != nid) {
3612 set_spdif_ctls(codec, nid, val & 0xff, (val >> 8) & 0xff);
3614 mutex_unlock(&codec->spdif_mutex);
3616 EXPORT_SYMBOL_GPL(snd_hda_spdif_ctls_assign);
3619 * SPDIF sharing with analog output
3621 static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
3622 struct snd_ctl_elem_value *ucontrol)
3624 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3625 ucontrol->value.integer.value[0] = mout->share_spdif;
3629 static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
3630 struct snd_ctl_elem_value *ucontrol)
3632 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3633 mout->share_spdif = !!ucontrol->value.integer.value[0];
3637 static struct snd_kcontrol_new spdif_share_sw = {
3638 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3639 .name = "IEC958 Default PCM Playback Switch",
3640 .info = snd_ctl_boolean_mono_info,
3641 .get = spdif_share_sw_get,
3642 .put = spdif_share_sw_put,
3646 * snd_hda_create_spdif_share_sw - create Default PCM switch
3647 * @codec: the HDA codec
3648 * @mout: multi-out instance
3650 int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
3651 struct hda_multi_out *mout)
3653 struct snd_kcontrol *kctl;
3655 if (!mout->dig_out_nid)
3658 kctl = snd_ctl_new1(&spdif_share_sw, mout);
3661 /* ATTENTION: here mout is passed as private_data, instead of codec */
3662 return snd_hda_ctl_add(codec, mout->dig_out_nid, kctl);
3664 EXPORT_SYMBOL_GPL(snd_hda_create_spdif_share_sw);
3670 #define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
3672 static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
3673 struct snd_ctl_elem_value *ucontrol)
3675 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3677 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
3681 static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
3682 struct snd_ctl_elem_value *ucontrol)
3684 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3685 hda_nid_t nid = kcontrol->private_value;
3686 unsigned int val = !!ucontrol->value.integer.value[0];
3689 mutex_lock(&codec->spdif_mutex);
3690 change = codec->spdif_in_enable != val;
3692 codec->spdif_in_enable = val;
3693 snd_hda_codec_write_cache(codec, nid, 0,
3694 AC_VERB_SET_DIGI_CONVERT_1, val);
3696 mutex_unlock(&codec->spdif_mutex);
3700 static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
3701 struct snd_ctl_elem_value *ucontrol)
3703 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3704 hda_nid_t nid = kcontrol->private_value;
3708 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0);
3709 sbits = convert_to_spdif_status(val);
3710 ucontrol->value.iec958.status[0] = sbits;
3711 ucontrol->value.iec958.status[1] = sbits >> 8;
3712 ucontrol->value.iec958.status[2] = sbits >> 16;
3713 ucontrol->value.iec958.status[3] = sbits >> 24;
3717 static struct snd_kcontrol_new dig_in_ctls[] = {
3719 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3720 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH),
3721 .info = snd_hda_spdif_in_switch_info,
3722 .get = snd_hda_spdif_in_switch_get,
3723 .put = snd_hda_spdif_in_switch_put,
3726 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3727 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3728 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
3729 .info = snd_hda_spdif_mask_info,
3730 .get = snd_hda_spdif_in_status_get,
3736 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
3737 * @codec: the HDA codec
3738 * @nid: audio in widget NID
3740 * Creates controls related with the SPDIF input.
3741 * Called from each patch supporting the SPDIF in.
3743 * Returns 0 if successful, or a negative error code.
3745 int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
3748 struct snd_kcontrol *kctl;
3749 struct snd_kcontrol_new *dig_mix;
3752 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Capture Switch", 0);
3754 codec_err(codec, "too many IEC958 inputs\n");
3757 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
3758 kctl = snd_ctl_new1(dig_mix, codec);
3761 kctl->private_value = nid;
3762 err = snd_hda_ctl_add(codec, nid, kctl);
3766 codec->spdif_in_enable =
3767 snd_hda_codec_read(codec, nid, 0,
3768 AC_VERB_GET_DIGI_CONVERT_1, 0) &
3772 EXPORT_SYMBOL_GPL(snd_hda_create_spdif_in_ctls);
3778 /* build a 31bit cache key with the widget id and the command parameter */
3779 #define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
3780 #define get_cmd_cache_nid(key) ((key) & 0xff)
3781 #define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
3784 * snd_hda_codec_write_cache - send a single command with caching
3785 * @codec: the HDA codec
3786 * @nid: NID to send the command
3787 * @flags: optional bit flags
3788 * @verb: the verb to send
3789 * @parm: the parameter for the verb
3791 * Send a single command without waiting for response.
3793 * Returns 0 if successful, or a negative error code.
3795 int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
3796 int flags, unsigned int verb, unsigned int parm)
3799 struct hda_cache_head *c;
3801 unsigned int cache_only;
3803 cache_only = codec->cached_write;
3805 err = snd_hda_codec_write(codec, nid, flags, verb, parm);
3810 /* parm may contain the verb stuff for get/set amp */
3811 verb = verb | (parm >> 8);
3813 key = build_cmd_cache_key(nid, verb);
3814 mutex_lock(&codec->bus->cmd_mutex);
3815 c = get_alloc_hash(&codec->cmd_cache, key);
3818 c->dirty = cache_only;
3820 mutex_unlock(&codec->bus->cmd_mutex);
3823 EXPORT_SYMBOL_GPL(snd_hda_codec_write_cache);
3826 * snd_hda_codec_update_cache - check cache and write the cmd only when needed
3827 * @codec: the HDA codec
3828 * @nid: NID to send the command
3829 * @flags: optional bit flags
3830 * @verb: the verb to send
3831 * @parm: the parameter for the verb
3833 * This function works like snd_hda_codec_write_cache(), but it doesn't send
3834 * command if the parameter is already identical with the cached value.
3835 * If not, it sends the command and refreshes the cache.
3837 * Returns 0 if successful, or a negative error code.
3839 int snd_hda_codec_update_cache(struct hda_codec *codec, hda_nid_t nid,
3840 int flags, unsigned int verb, unsigned int parm)
3842 struct hda_cache_head *c;
3845 /* parm may contain the verb stuff for get/set amp */
3846 verb = verb | (parm >> 8);
3848 key = build_cmd_cache_key(nid, verb);
3849 mutex_lock(&codec->bus->cmd_mutex);
3850 c = get_hash(&codec->cmd_cache, key);
3851 if (c && c->val == parm) {
3852 mutex_unlock(&codec->bus->cmd_mutex);
3855 mutex_unlock(&codec->bus->cmd_mutex);
3856 return snd_hda_codec_write_cache(codec, nid, flags, verb, parm);
3858 EXPORT_SYMBOL_GPL(snd_hda_codec_update_cache);
3861 * snd_hda_codec_resume_cache - Resume the all commands from the cache
3862 * @codec: HD-audio codec
3864 * Execute all verbs recorded in the command caches to resume.
3866 void snd_hda_codec_resume_cache(struct hda_codec *codec)
3870 mutex_lock(&codec->hash_mutex);
3871 codec->cached_write = 0;
3872 for (i = 0; i < codec->cmd_cache.buf.used; i++) {
3873 struct hda_cache_head *buffer;
3876 buffer = snd_array_elem(&codec->cmd_cache.buf, i);
3883 mutex_unlock(&codec->hash_mutex);
3884 snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
3885 get_cmd_cache_cmd(key), buffer->val);
3886 mutex_lock(&codec->hash_mutex);
3888 mutex_unlock(&codec->hash_mutex);
3890 EXPORT_SYMBOL_GPL(snd_hda_codec_resume_cache);
3893 * snd_hda_sequence_write_cache - sequence writes with caching
3894 * @codec: the HDA codec
3895 * @seq: VERB array to send
3897 * Send the commands sequentially from the given array.
3898 * Thte commands are recorded on cache for power-save and resume.
3899 * The array must be terminated with NID=0.
3901 void snd_hda_sequence_write_cache(struct hda_codec *codec,
3902 const struct hda_verb *seq)
3904 for (; seq->nid; seq++)
3905 snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
3908 EXPORT_SYMBOL_GPL(snd_hda_sequence_write_cache);
3911 * snd_hda_codec_flush_cache - Execute all pending (cached) amps / verbs
3912 * @codec: HD-audio codec
3914 void snd_hda_codec_flush_cache(struct hda_codec *codec)
3916 snd_hda_codec_resume_amp(codec);
3917 snd_hda_codec_resume_cache(codec);
3919 EXPORT_SYMBOL_GPL(snd_hda_codec_flush_cache);
3921 void snd_hda_codec_set_power_to_all(struct hda_codec *codec, hda_nid_t fg,
3922 unsigned int power_state)
3924 hda_nid_t nid = codec->start_nid;
3927 for (i = 0; i < codec->num_nodes; i++, nid++) {
3928 unsigned int wcaps = get_wcaps(codec, nid);
3929 unsigned int state = power_state;
3930 if (!(wcaps & AC_WCAP_POWER))
3932 if (codec->power_filter) {
3933 state = codec->power_filter(codec, nid, power_state);
3934 if (state != power_state && power_state == AC_PWRST_D3)
3937 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE,
3941 EXPORT_SYMBOL_GPL(snd_hda_codec_set_power_to_all);
3944 * supported power states check
3946 static bool snd_hda_codec_get_supported_ps(struct hda_codec *codec, hda_nid_t fg,
3947 unsigned int power_state)
3949 int sup = snd_hda_param_read(codec, fg, AC_PAR_POWER_STATE);
3953 if (sup & power_state)
3960 * wait until the state is reached, returns the current state
3962 static unsigned int hda_sync_power_state(struct hda_codec *codec,
3964 unsigned int power_state)
3966 unsigned long end_time = jiffies + msecs_to_jiffies(500);
3967 unsigned int state, actual_state;
3970 state = snd_hda_codec_read(codec, fg, 0,
3971 AC_VERB_GET_POWER_STATE, 0);
3972 if (state & AC_PWRST_ERROR)
3974 actual_state = (state >> 4) & 0x0f;
3975 if (actual_state == power_state)
3977 if (time_after_eq(jiffies, end_time))
3979 /* wait until the codec reachs to the target state */
3985 /* don't power down the widget if it controls eapd and EAPD_BTLENABLE is set */
3986 unsigned int snd_hda_codec_eapd_power_filter(struct hda_codec *codec,
3988 unsigned int power_state)
3990 if (nid == codec->afg || nid == codec->mfg)
3992 if (power_state == AC_PWRST_D3 &&
3993 get_wcaps_type(get_wcaps(codec, nid)) == AC_WID_PIN &&
3994 (snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD)) {
3995 int eapd = snd_hda_codec_read(codec, nid, 0,
3996 AC_VERB_GET_EAPD_BTLENABLE, 0);
4002 EXPORT_SYMBOL_GPL(snd_hda_codec_eapd_power_filter);
4005 * set power state of the codec, and return the power state
4007 static unsigned int hda_set_power_state(struct hda_codec *codec,
4008 unsigned int power_state)
4010 hda_nid_t fg = codec->afg ? codec->afg : codec->mfg;
4015 /* this delay seems necessary to avoid click noise at power-down */
4016 if (power_state == AC_PWRST_D3) {
4017 if (codec->depop_delay < 0)
4018 msleep(codec->epss ? 10 : 100);
4019 else if (codec->depop_delay > 0)
4020 msleep(codec->depop_delay);
4021 flags = HDA_RW_NO_RESPONSE_FALLBACK;
4024 /* repeat power states setting at most 10 times*/
4025 for (count = 0; count < 10; count++) {
4026 if (codec->patch_ops.set_power_state)
4027 codec->patch_ops.set_power_state(codec, fg,
4030 state = power_state;
4031 if (codec->power_filter)
4032 state = codec->power_filter(codec, fg, state);
4033 if (state == power_state || power_state != AC_PWRST_D3)
4034 snd_hda_codec_read(codec, fg, flags,
4035 AC_VERB_SET_POWER_STATE,
4037 snd_hda_codec_set_power_to_all(codec, fg, power_state);
4039 state = hda_sync_power_state(codec, fg, power_state);
4040 if (!(state & AC_PWRST_ERROR))
4047 /* sync power states of all widgets;
4048 * this is called at the end of codec parsing
4050 static void sync_power_up_states(struct hda_codec *codec)
4052 hda_nid_t nid = codec->start_nid;
4055 /* don't care if no filter is used */
4056 if (!codec->power_filter)
4059 for (i = 0; i < codec->num_nodes; i++, nid++) {
4060 unsigned int wcaps = get_wcaps(codec, nid);
4061 unsigned int target;
4062 if (!(wcaps & AC_WCAP_POWER))
4064 target = codec->power_filter(codec, nid, AC_PWRST_D0);
4065 if (target == AC_PWRST_D0)
4067 if (!snd_hda_check_power_state(codec, nid, target))
4068 snd_hda_codec_write(codec, nid, 0,
4069 AC_VERB_SET_POWER_STATE, target);
4073 #ifdef CONFIG_SND_HDA_RECONFIG
4074 /* execute additional init verbs */
4075 static void hda_exec_init_verbs(struct hda_codec *codec)
4077 if (codec->init_verbs.list)
4078 snd_hda_sequence_write(codec, codec->init_verbs.list);
4081 static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
4086 * call suspend and power-down; used both from PM and power-save
4087 * this function returns the power state in the end
4089 static unsigned int hda_call_codec_suspend(struct hda_codec *codec, bool in_wq)
4095 if (codec->patch_ops.suspend)
4096 codec->patch_ops.suspend(codec);
4097 hda_cleanup_all_streams(codec);
4098 state = hda_set_power_state(codec, AC_PWRST_D3);
4099 /* Cancel delayed work if we aren't currently running from it. */
4101 cancel_delayed_work_sync(&codec->power_work);
4102 spin_lock(&codec->power_lock);
4103 snd_hda_update_power_acct(codec);
4104 trace_hda_power_down(codec);
4105 codec->power_on = 0;
4106 codec->power_transition = 0;
4107 codec->power_jiffies = jiffies;
4108 spin_unlock(&codec->power_lock);
4113 /* mark all entries of cmd and amp caches dirty */
4114 static void hda_mark_cmd_cache_dirty(struct hda_codec *codec)
4117 for (i = 0; i < codec->cmd_cache.buf.used; i++) {
4118 struct hda_cache_head *cmd;
4119 cmd = snd_array_elem(&codec->cmd_cache.buf, i);
4122 for (i = 0; i < codec->amp_cache.buf.used; i++) {
4123 struct hda_amp_info *amp;
4124 amp = snd_array_elem(&codec->amp_cache.buf, i);
4125 amp->head.dirty = 1;
4130 * kick up codec; used both from PM and power-save
4132 static void hda_call_codec_resume(struct hda_codec *codec)
4136 hda_mark_cmd_cache_dirty(codec);
4138 /* set as if powered on for avoiding re-entering the resume
4139 * in the resume / power-save sequence
4141 hda_keep_power_on(codec);
4142 hda_set_power_state(codec, AC_PWRST_D0);
4143 restore_shutup_pins(codec);
4144 hda_exec_init_verbs(codec);
4145 snd_hda_jack_set_dirty_all(codec);
4146 if (codec->patch_ops.resume)
4147 codec->patch_ops.resume(codec);
4149 if (codec->patch_ops.init)
4150 codec->patch_ops.init(codec);
4151 snd_hda_codec_resume_amp(codec);
4152 snd_hda_codec_resume_cache(codec);
4155 if (codec->jackpoll_interval)
4156 hda_jackpoll_work(&codec->jackpoll_work.work);
4158 snd_hda_jack_report_sync(codec);
4161 snd_hda_power_down(codec); /* flag down before returning */
4163 #endif /* CONFIG_PM */
4167 * snd_hda_build_controls - build mixer controls
4170 * Creates mixer controls for each codec included in the bus.
4172 * Returns 0 if successful, otherwise a negative error code.
4174 int snd_hda_build_controls(struct hda_bus *bus)
4176 struct hda_codec *codec;
4178 list_for_each_entry(codec, &bus->codec_list, list) {
4179 int err = snd_hda_codec_build_controls(codec);
4182 "cannot build controls for #%d (error %d)\n",
4184 err = snd_hda_codec_reset(codec);
4187 "cannot revert codec\n");
4194 EXPORT_SYMBOL_GPL(snd_hda_build_controls);
4197 * add standard channel maps if not specified
4199 static int add_std_chmaps(struct hda_codec *codec)
4203 for (i = 0; i < codec->num_pcms; i++) {
4204 for (str = 0; str < 2; str++) {
4205 struct snd_pcm *pcm = codec->pcm_info[i].pcm;
4206 struct hda_pcm_stream *hinfo =
4207 &codec->pcm_info[i].stream[str];
4208 struct snd_pcm_chmap *chmap;
4209 const struct snd_pcm_chmap_elem *elem;
4211 if (codec->pcm_info[i].own_chmap)
4213 if (!pcm || !hinfo->substreams)
4215 elem = hinfo->chmap ? hinfo->chmap : snd_pcm_std_chmaps;
4216 err = snd_pcm_add_chmap_ctls(pcm, str, elem,
4217 hinfo->channels_max,
4221 chmap->channel_mask = SND_PCM_CHMAP_MASK_2468;
4227 /* default channel maps for 2.1 speakers;
4228 * since HD-audio supports only stereo, odd number channels are omitted
4230 const struct snd_pcm_chmap_elem snd_pcm_2_1_chmaps[] = {
4232 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } },
4234 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
4235 SNDRV_CHMAP_LFE, SNDRV_CHMAP_LFE } },
4238 EXPORT_SYMBOL_GPL(snd_pcm_2_1_chmaps);
4240 int snd_hda_codec_build_controls(struct hda_codec *codec)
4243 hda_exec_init_verbs(codec);
4244 /* continue to initialize... */
4245 if (codec->patch_ops.init)
4246 err = codec->patch_ops.init(codec);
4247 if (!err && codec->patch_ops.build_controls)
4248 err = codec->patch_ops.build_controls(codec);
4252 /* we create chmaps here instead of build_pcms */
4253 err = add_std_chmaps(codec);
4257 if (codec->jackpoll_interval)
4258 hda_jackpoll_work(&codec->jackpoll_work.work);
4260 snd_hda_jack_report_sync(codec); /* call at the last init point */
4261 sync_power_up_states(codec);
4268 struct hda_rate_tbl {
4270 unsigned int alsa_bits;
4271 unsigned int hda_fmt;
4274 /* rate = base * mult / div */
4275 #define HDA_RATE(base, mult, div) \
4276 (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \
4277 (((div) - 1) << AC_FMT_DIV_SHIFT))
4279 static struct hda_rate_tbl rate_bits[] = {
4280 /* rate in Hz, ALSA rate bitmask, HDA format value */
4282 /* autodetected value used in snd_hda_query_supported_pcm */
4283 { 8000, SNDRV_PCM_RATE_8000, HDA_RATE(48, 1, 6) },
4284 { 11025, SNDRV_PCM_RATE_11025, HDA_RATE(44, 1, 4) },
4285 { 16000, SNDRV_PCM_RATE_16000, HDA_RATE(48, 1, 3) },
4286 { 22050, SNDRV_PCM_RATE_22050, HDA_RATE(44, 1, 2) },
4287 { 32000, SNDRV_PCM_RATE_32000, HDA_RATE(48, 2, 3) },
4288 { 44100, SNDRV_PCM_RATE_44100, HDA_RATE(44, 1, 1) },
4289 { 48000, SNDRV_PCM_RATE_48000, HDA_RATE(48, 1, 1) },
4290 { 88200, SNDRV_PCM_RATE_88200, HDA_RATE(44, 2, 1) },
4291 { 96000, SNDRV_PCM_RATE_96000, HDA_RATE(48, 2, 1) },
4292 { 176400, SNDRV_PCM_RATE_176400, HDA_RATE(44, 4, 1) },
4293 { 192000, SNDRV_PCM_RATE_192000, HDA_RATE(48, 4, 1) },
4294 #define AC_PAR_PCM_RATE_BITS 11
4295 /* up to bits 10, 384kHZ isn't supported properly */
4297 /* not autodetected value */
4298 { 9600, SNDRV_PCM_RATE_KNOT, HDA_RATE(48, 1, 5) },
4300 { 0 } /* terminator */
4304 * snd_hda_calc_stream_format - calculate format bitset
4305 * @codec: HD-audio codec
4306 * @rate: the sample rate
4307 * @channels: the number of channels
4308 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
4309 * @maxbps: the max. bps
4311 * Calculate the format bitset from the given rate, channels and th PCM format.
4313 * Return zero if invalid.
4315 unsigned int snd_hda_calc_stream_format(struct hda_codec *codec,
4317 unsigned int channels,
4318 unsigned int format,
4319 unsigned int maxbps,
4320 unsigned short spdif_ctls)
4323 unsigned int val = 0;
4325 for (i = 0; rate_bits[i].hz; i++)
4326 if (rate_bits[i].hz == rate) {
4327 val = rate_bits[i].hda_fmt;
4330 if (!rate_bits[i].hz) {
4331 codec_dbg(codec, "invalid rate %d\n", rate);
4335 if (channels == 0 || channels > 8) {
4336 codec_dbg(codec, "invalid channels %d\n", channels);
4339 val |= channels - 1;
4341 switch (snd_pcm_format_width(format)) {
4343 val |= AC_FMT_BITS_8;
4346 val |= AC_FMT_BITS_16;
4351 if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE)
4352 val |= AC_FMT_BITS_32;
4353 else if (maxbps >= 24)
4354 val |= AC_FMT_BITS_24;
4356 val |= AC_FMT_BITS_20;
4359 codec_dbg(codec, "invalid format width %d\n",
4360 snd_pcm_format_width(format));
4364 if (spdif_ctls & AC_DIG1_NONAUDIO)
4365 val |= AC_FMT_TYPE_NON_PCM;
4369 EXPORT_SYMBOL_GPL(snd_hda_calc_stream_format);
4371 static unsigned int get_pcm_param(struct hda_codec *codec, hda_nid_t nid,
4374 unsigned int val = 0;
4375 if (nid != codec->afg &&
4376 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD))
4377 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
4378 if (!val || val == -1)
4379 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
4380 if (!val || val == -1)
4385 static unsigned int query_pcm_param(struct hda_codec *codec, hda_nid_t nid)
4387 return query_caps_hash(codec, nid, 0, HDA_HASH_PARPCM_KEY(nid),
4391 static unsigned int get_stream_param(struct hda_codec *codec, hda_nid_t nid,
4394 unsigned int streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
4395 if (!streams || streams == -1)
4396 streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
4397 if (!streams || streams == -1)
4402 static unsigned int query_stream_param(struct hda_codec *codec, hda_nid_t nid)
4404 return query_caps_hash(codec, nid, 0, HDA_HASH_PARSTR_KEY(nid),
4409 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
4410 * @codec: the HDA codec
4411 * @nid: NID to query
4412 * @ratesp: the pointer to store the detected rate bitflags
4413 * @formatsp: the pointer to store the detected formats
4414 * @bpsp: the pointer to store the detected format widths
4416 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
4417 * or @bsps argument is ignored.
4419 * Returns 0 if successful, otherwise a negative error code.
4421 int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
4422 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
4424 unsigned int i, val, wcaps;
4426 wcaps = get_wcaps(codec, nid);
4427 val = query_pcm_param(codec, nid);
4431 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
4433 rates |= rate_bits[i].alsa_bits;
4437 "rates == 0 (nid=0x%x, val=0x%x, ovrd=%i)\n",
4439 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0);
4445 if (formatsp || bpsp) {
4447 unsigned int streams, bps;
4449 streams = query_stream_param(codec, nid);
4454 if (streams & AC_SUPFMT_PCM) {
4455 if (val & AC_SUPPCM_BITS_8) {
4456 formats |= SNDRV_PCM_FMTBIT_U8;
4459 if (val & AC_SUPPCM_BITS_16) {
4460 formats |= SNDRV_PCM_FMTBIT_S16_LE;
4463 if (wcaps & AC_WCAP_DIGITAL) {
4464 if (val & AC_SUPPCM_BITS_32)
4465 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
4466 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
4467 formats |= SNDRV_PCM_FMTBIT_S32_LE;
4468 if (val & AC_SUPPCM_BITS_24)
4470 else if (val & AC_SUPPCM_BITS_20)
4472 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
4473 AC_SUPPCM_BITS_32)) {
4474 formats |= SNDRV_PCM_FMTBIT_S32_LE;
4475 if (val & AC_SUPPCM_BITS_32)
4477 else if (val & AC_SUPPCM_BITS_24)
4479 else if (val & AC_SUPPCM_BITS_20)
4483 #if 0 /* FIXME: CS4206 doesn't work, which is the only codec supporting float */
4484 if (streams & AC_SUPFMT_FLOAT32) {
4485 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
4490 if (streams == AC_SUPFMT_AC3) {
4491 /* should be exclusive */
4492 /* temporary hack: we have still no proper support
4493 * for the direct AC3 stream...
4495 formats |= SNDRV_PCM_FMTBIT_U8;
4500 "formats == 0 (nid=0x%x, val=0x%x, ovrd=%i, streams=0x%x)\n",
4502 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0,
4507 *formatsp = formats;
4514 EXPORT_SYMBOL_GPL(snd_hda_query_supported_pcm);
4517 * snd_hda_is_supported_format - Check the validity of the format
4518 * @codec: HD-audio codec
4519 * @nid: NID to check
4520 * @format: the HD-audio format value to check
4522 * Check whether the given node supports the format value.
4524 * Returns 1 if supported, 0 if not.
4526 int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
4527 unsigned int format)
4530 unsigned int val = 0, rate, stream;
4532 val = query_pcm_param(codec, nid);
4536 rate = format & 0xff00;
4537 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
4538 if (rate_bits[i].hda_fmt == rate) {
4543 if (i >= AC_PAR_PCM_RATE_BITS)
4546 stream = query_stream_param(codec, nid);
4550 if (stream & AC_SUPFMT_PCM) {
4551 switch (format & 0xf0) {
4553 if (!(val & AC_SUPPCM_BITS_8))
4557 if (!(val & AC_SUPPCM_BITS_16))
4561 if (!(val & AC_SUPPCM_BITS_20))
4565 if (!(val & AC_SUPPCM_BITS_24))
4569 if (!(val & AC_SUPPCM_BITS_32))
4576 /* FIXME: check for float32 and AC3? */
4581 EXPORT_SYMBOL_GPL(snd_hda_is_supported_format);
4586 static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
4587 struct hda_codec *codec,
4588 struct snd_pcm_substream *substream)
4593 static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
4594 struct hda_codec *codec,
4595 unsigned int stream_tag,
4596 unsigned int format,
4597 struct snd_pcm_substream *substream)
4599 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4603 static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
4604 struct hda_codec *codec,
4605 struct snd_pcm_substream *substream)
4607 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
4611 static int set_pcm_default_values(struct hda_codec *codec,
4612 struct hda_pcm_stream *info)
4616 /* query support PCM information from the given NID */
4617 if (info->nid && (!info->rates || !info->formats)) {
4618 err = snd_hda_query_supported_pcm(codec, info->nid,
4619 info->rates ? NULL : &info->rates,
4620 info->formats ? NULL : &info->formats,
4621 info->maxbps ? NULL : &info->maxbps);
4625 if (info->ops.open == NULL)
4626 info->ops.open = hda_pcm_default_open_close;
4627 if (info->ops.close == NULL)
4628 info->ops.close = hda_pcm_default_open_close;
4629 if (info->ops.prepare == NULL) {
4630 if (snd_BUG_ON(!info->nid))
4632 info->ops.prepare = hda_pcm_default_prepare;
4634 if (info->ops.cleanup == NULL) {
4635 if (snd_BUG_ON(!info->nid))
4637 info->ops.cleanup = hda_pcm_default_cleanup;
4643 * codec prepare/cleanup entries
4645 int snd_hda_codec_prepare(struct hda_codec *codec,
4646 struct hda_pcm_stream *hinfo,
4647 unsigned int stream,
4648 unsigned int format,
4649 struct snd_pcm_substream *substream)
4652 mutex_lock(&codec->bus->prepare_mutex);
4653 ret = hinfo->ops.prepare(hinfo, codec, stream, format, substream);
4655 purify_inactive_streams(codec);
4656 mutex_unlock(&codec->bus->prepare_mutex);
4659 EXPORT_SYMBOL_GPL(snd_hda_codec_prepare);
4661 void snd_hda_codec_cleanup(struct hda_codec *codec,
4662 struct hda_pcm_stream *hinfo,
4663 struct snd_pcm_substream *substream)
4665 mutex_lock(&codec->bus->prepare_mutex);
4666 hinfo->ops.cleanup(hinfo, codec, substream);
4667 mutex_unlock(&codec->bus->prepare_mutex);
4669 EXPORT_SYMBOL_GPL(snd_hda_codec_cleanup);
4672 const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = {
4673 "Audio", "SPDIF", "HDMI", "Modem"
4677 * get the empty PCM device number to assign
4679 static int get_empty_pcm_device(struct hda_bus *bus, unsigned int type)
4681 /* audio device indices; not linear to keep compatibility */
4682 /* assigned to static slots up to dev#10; if more needed, assign
4683 * the later slot dynamically (when CONFIG_SND_DYNAMIC_MINORS=y)
4685 static int audio_idx[HDA_PCM_NTYPES][5] = {
4686 [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 },
4687 [HDA_PCM_TYPE_SPDIF] = { 1, -1 },
4688 [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 },
4689 [HDA_PCM_TYPE_MODEM] = { 6, -1 },
4693 if (type >= HDA_PCM_NTYPES) {
4694 dev_err(bus->card->dev, "Invalid PCM type %d\n", type);
4698 for (i = 0; audio_idx[type][i] >= 0; i++) {
4699 #ifndef CONFIG_SND_DYNAMIC_MINORS
4700 if (audio_idx[type][i] >= 8)
4703 if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits))
4704 return audio_idx[type][i];
4707 #ifdef CONFIG_SND_DYNAMIC_MINORS
4708 /* non-fixed slots starting from 10 */
4709 for (i = 10; i < 32; i++) {
4710 if (!test_and_set_bit(i, bus->pcm_dev_bits))
4715 dev_warn(bus->card->dev, "Too many %s devices\n",
4716 snd_hda_pcm_type_name[type]);
4717 #ifndef CONFIG_SND_DYNAMIC_MINORS
4718 dev_warn(bus->card->dev,
4719 "Consider building the kernel with CONFIG_SND_DYNAMIC_MINORS=y\n");
4725 * attach a new PCM stream
4727 static int snd_hda_attach_pcm(struct hda_codec *codec, struct hda_pcm *pcm)
4729 struct hda_bus *bus = codec->bus;
4730 struct hda_pcm_stream *info;
4733 if (snd_BUG_ON(!pcm->name))
4735 for (stream = 0; stream < 2; stream++) {
4736 info = &pcm->stream[stream];
4737 if (info->substreams) {
4738 err = set_pcm_default_values(codec, info);
4743 return bus->ops.attach_pcm(bus, codec, pcm);
4746 /* assign all PCMs of the given codec */
4747 int snd_hda_codec_build_pcms(struct hda_codec *codec)
4752 if (!codec->num_pcms) {
4753 if (!codec->patch_ops.build_pcms)
4755 err = codec->patch_ops.build_pcms(codec);
4758 "cannot build PCMs for #%d (error %d)\n",
4760 err = snd_hda_codec_reset(codec);
4763 "cannot revert codec\n");
4768 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
4769 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
4772 if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
4773 continue; /* no substreams assigned */
4776 dev = get_empty_pcm_device(codec->bus, cpcm->pcm_type);
4778 continue; /* no fatal error */
4780 err = snd_hda_attach_pcm(codec, cpcm);
4783 "cannot attach PCM stream %d for codec #%d\n",
4785 continue; /* no fatal error */
4793 * snd_hda_build_pcms - build PCM information
4796 * Create PCM information for each codec included in the bus.
4798 * The build_pcms codec patch is requested to set up codec->num_pcms and
4799 * codec->pcm_info properly. The array is referred by the top-level driver
4800 * to create its PCM instances.
4801 * The allocated codec->pcm_info should be released in codec->patch_ops.free
4804 * At least, substreams, channels_min and channels_max must be filled for
4805 * each stream. substreams = 0 indicates that the stream doesn't exist.
4806 * When rates and/or formats are zero, the supported values are queried
4807 * from the given nid. The nid is used also by the default ops.prepare
4808 * and ops.cleanup callbacks.
4810 * The driver needs to call ops.open in its open callback. Similarly,
4811 * ops.close is supposed to be called in the close callback.
4812 * ops.prepare should be called in the prepare or hw_params callback
4813 * with the proper parameters for set up.
4814 * ops.cleanup should be called in hw_free for clean up of streams.
4816 * This function returns 0 if successful, or a negative error code.
4818 int snd_hda_build_pcms(struct hda_bus *bus)
4820 struct hda_codec *codec;
4822 list_for_each_entry(codec, &bus->codec_list, list) {
4823 int err = snd_hda_codec_build_pcms(codec);
4829 EXPORT_SYMBOL_GPL(snd_hda_build_pcms);
4832 * snd_hda_add_new_ctls - create controls from the array
4833 * @codec: the HDA codec
4834 * @knew: the array of struct snd_kcontrol_new
4836 * This helper function creates and add new controls in the given array.
4837 * The array must be terminated with an empty entry as terminator.
4839 * Returns 0 if successful, or a negative error code.
4841 int snd_hda_add_new_ctls(struct hda_codec *codec,
4842 const struct snd_kcontrol_new *knew)
4846 for (; knew->name; knew++) {
4847 struct snd_kcontrol *kctl;
4848 int addr = 0, idx = 0;
4849 if (knew->iface == -1) /* skip this codec private value */
4852 kctl = snd_ctl_new1(knew, codec);
4856 kctl->id.device = addr;
4858 kctl->id.index = idx;
4859 err = snd_hda_ctl_add(codec, 0, kctl);
4862 /* try first with another device index corresponding to
4863 * the codec addr; if it still fails (or it's the
4864 * primary codec), then try another control index
4866 if (!addr && codec->addr)
4868 else if (!idx && !knew->index) {
4869 idx = find_empty_mixer_ctl_idx(codec,
4879 EXPORT_SYMBOL_GPL(snd_hda_add_new_ctls);
4882 static void hda_power_work(struct work_struct *work)
4884 struct hda_codec *codec =
4885 container_of(work, struct hda_codec, power_work.work);
4886 struct hda_bus *bus = codec->bus;
4889 spin_lock(&codec->power_lock);
4890 if (codec->power_transition > 0) { /* during power-up sequence? */
4891 spin_unlock(&codec->power_lock);
4894 if (!codec->power_on || codec->power_count) {
4895 codec->power_transition = 0;
4896 spin_unlock(&codec->power_lock);
4899 spin_unlock(&codec->power_lock);
4901 state = hda_call_codec_suspend(codec, true);
4902 if (!bus->power_keep_link_on && (state & AC_PWRST_CLK_STOP_OK))
4903 hda_call_pm_notify(codec, false);
4906 static void hda_keep_power_on(struct hda_codec *codec)
4908 spin_lock(&codec->power_lock);
4909 codec->power_count++;
4910 codec->power_on = 1;
4911 codec->power_jiffies = jiffies;
4912 spin_unlock(&codec->power_lock);
4913 hda_call_pm_notify(codec, true);
4916 /* update the power on/off account with the current jiffies */
4917 void snd_hda_update_power_acct(struct hda_codec *codec)
4919 unsigned long delta = jiffies - codec->power_jiffies;
4920 if (codec->power_on)
4921 codec->power_on_acct += delta;
4923 codec->power_off_acct += delta;
4924 codec->power_jiffies += delta;
4927 /* Transition to powered up, if wait_power_down then wait for a pending
4928 * transition to D3 to complete. A pending D3 transition is indicated
4929 * with power_transition == -1. */
4930 /* call this with codec->power_lock held! */
4931 static void __snd_hda_power_up(struct hda_codec *codec, bool wait_power_down)
4933 /* Return if power_on or transitioning to power_on, unless currently
4935 if ((codec->power_on || codec->power_transition > 0) &&
4936 !(wait_power_down && codec->power_transition < 0))
4938 spin_unlock(&codec->power_lock);
4940 cancel_delayed_work_sync(&codec->power_work);
4942 spin_lock(&codec->power_lock);
4943 /* If the power down delayed work was cancelled above before starting,
4944 * then there is no need to go through power up here.
4946 if (codec->power_on) {
4947 if (codec->power_transition < 0)
4948 codec->power_transition = 0;
4952 trace_hda_power_up(codec);
4953 snd_hda_update_power_acct(codec);
4954 codec->power_on = 1;
4955 codec->power_jiffies = jiffies;
4956 codec->power_transition = 1; /* avoid reentrance */
4957 spin_unlock(&codec->power_lock);
4959 hda_call_codec_resume(codec);
4961 spin_lock(&codec->power_lock);
4962 codec->power_transition = 0;
4965 #define power_save(codec) \
4966 ((codec)->bus->power_save ? *(codec)->bus->power_save : 0)
4968 /* Transition to powered down */
4969 static void __snd_hda_power_down(struct hda_codec *codec)
4971 if (!codec->power_on || codec->power_count || codec->power_transition)
4974 if (power_save(codec)) {
4975 codec->power_transition = -1; /* avoid reentrance */
4976 queue_delayed_work(codec->bus->workq, &codec->power_work,
4977 msecs_to_jiffies(power_save(codec) * 1000));
4982 * snd_hda_power_save - Power-up/down/sync the codec
4983 * @codec: HD-audio codec
4984 * @delta: the counter delta to change
4986 * Change the power-up counter via @delta, and power up or down the hardware
4987 * appropriately. For the power-down, queue to the delayed action.
4988 * Passing zero to @delta means to synchronize the power state.
4990 void snd_hda_power_save(struct hda_codec *codec, int delta, bool d3wait)
4992 spin_lock(&codec->power_lock);
4993 codec->power_count += delta;
4994 trace_hda_power_count(codec);
4996 __snd_hda_power_up(codec, d3wait);
4998 __snd_hda_power_down(codec);
4999 spin_unlock(&codec->power_lock);
5001 EXPORT_SYMBOL_GPL(snd_hda_power_save);
5004 * snd_hda_check_amp_list_power - Check the amp list and update the power
5005 * @codec: HD-audio codec
5006 * @check: the object containing an AMP list and the status
5007 * @nid: NID to check / update
5009 * Check whether the given NID is in the amp list. If it's in the list,
5010 * check the current AMP status, and update the the power-status according
5011 * to the mute status.
5013 * This function is supposed to be set or called from the check_power_status
5016 int snd_hda_check_amp_list_power(struct hda_codec *codec,
5017 struct hda_loopback_check *check,
5020 const struct hda_amp_list *p;
5023 if (!check->amplist)
5025 for (p = check->amplist; p->nid; p++) {
5030 return 0; /* nothing changed */
5032 for (p = check->amplist; p->nid; p++) {
5033 for (ch = 0; ch < 2; ch++) {
5034 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
5036 if (!(v & HDA_AMP_MUTE) && v > 0) {
5037 if (!check->power_on) {
5038 check->power_on = 1;
5039 snd_hda_power_up(codec);
5045 if (check->power_on) {
5046 check->power_on = 0;
5047 snd_hda_power_down(codec);
5051 EXPORT_SYMBOL_GPL(snd_hda_check_amp_list_power);
5055 * Channel mode helper
5059 * snd_hda_ch_mode_info - Info callback helper for the channel mode enum
5061 int snd_hda_ch_mode_info(struct hda_codec *codec,
5062 struct snd_ctl_elem_info *uinfo,
5063 const struct hda_channel_mode *chmode,
5066 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
5068 uinfo->value.enumerated.items = num_chmodes;
5069 if (uinfo->value.enumerated.item >= num_chmodes)
5070 uinfo->value.enumerated.item = num_chmodes - 1;
5071 sprintf(uinfo->value.enumerated.name, "%dch",
5072 chmode[uinfo->value.enumerated.item].channels);
5075 EXPORT_SYMBOL_GPL(snd_hda_ch_mode_info);
5078 * snd_hda_ch_mode_get - Get callback helper for the channel mode enum
5080 int snd_hda_ch_mode_get(struct hda_codec *codec,
5081 struct snd_ctl_elem_value *ucontrol,
5082 const struct hda_channel_mode *chmode,
5088 for (i = 0; i < num_chmodes; i++) {
5089 if (max_channels == chmode[i].channels) {
5090 ucontrol->value.enumerated.item[0] = i;
5096 EXPORT_SYMBOL_GPL(snd_hda_ch_mode_get);
5099 * snd_hda_ch_mode_put - Put callback helper for the channel mode enum
5101 int snd_hda_ch_mode_put(struct hda_codec *codec,
5102 struct snd_ctl_elem_value *ucontrol,
5103 const struct hda_channel_mode *chmode,
5109 mode = ucontrol->value.enumerated.item[0];
5110 if (mode >= num_chmodes)
5112 if (*max_channelsp == chmode[mode].channels)
5114 /* change the current channel setting */
5115 *max_channelsp = chmode[mode].channels;
5116 if (chmode[mode].sequence)
5117 snd_hda_sequence_write_cache(codec, chmode[mode].sequence);
5120 EXPORT_SYMBOL_GPL(snd_hda_ch_mode_put);
5127 * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum
5129 int snd_hda_input_mux_info(const struct hda_input_mux *imux,
5130 struct snd_ctl_elem_info *uinfo)
5134 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
5136 uinfo->value.enumerated.items = imux->num_items;
5137 if (!imux->num_items)
5139 index = uinfo->value.enumerated.item;
5140 if (index >= imux->num_items)
5141 index = imux->num_items - 1;
5142 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
5145 EXPORT_SYMBOL_GPL(snd_hda_input_mux_info);
5148 * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum
5150 int snd_hda_input_mux_put(struct hda_codec *codec,
5151 const struct hda_input_mux *imux,
5152 struct snd_ctl_elem_value *ucontrol,
5154 unsigned int *cur_val)
5158 if (!imux->num_items)
5160 idx = ucontrol->value.enumerated.item[0];
5161 if (idx >= imux->num_items)
5162 idx = imux->num_items - 1;
5163 if (*cur_val == idx)
5165 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
5166 imux->items[idx].index);
5170 EXPORT_SYMBOL_GPL(snd_hda_input_mux_put);
5174 * process kcontrol info callback of a simple string enum array
5175 * when @num_items is 0 or @texts is NULL, assume a boolean enum array
5177 int snd_hda_enum_helper_info(struct snd_kcontrol *kcontrol,
5178 struct snd_ctl_elem_info *uinfo,
5179 int num_items, const char * const *texts)
5181 static const char * const texts_default[] = {
5182 "Disabled", "Enabled"
5185 if (!texts || !num_items) {
5187 texts = texts_default;
5190 return snd_ctl_enum_info(uinfo, 1, num_items, texts);
5192 EXPORT_SYMBOL_GPL(snd_hda_enum_helper_info);
5195 * Multi-channel / digital-out PCM helper functions
5198 /* setup SPDIF output stream */
5199 static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
5200 unsigned int stream_tag, unsigned int format)
5202 struct hda_spdif_out *spdif;
5203 unsigned int curr_fmt;
5206 spdif = snd_hda_spdif_out_of_nid(codec, nid);
5207 curr_fmt = snd_hda_codec_read(codec, nid, 0,
5208 AC_VERB_GET_STREAM_FORMAT, 0);
5209 reset = codec->spdif_status_reset &&
5210 (spdif->ctls & AC_DIG1_ENABLE) &&
5213 /* turn off SPDIF if needed; otherwise the IEC958 bits won't be
5216 set_dig_out_convert(codec, nid,
5217 spdif->ctls & ~AC_DIG1_ENABLE & 0xff,
5219 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
5220 if (codec->slave_dig_outs) {
5222 for (d = codec->slave_dig_outs; *d; d++)
5223 snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
5226 /* turn on again (if needed) */
5228 set_dig_out_convert(codec, nid,
5229 spdif->ctls & 0xff, -1);
5232 static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
5234 snd_hda_codec_cleanup_stream(codec, nid);
5235 if (codec->slave_dig_outs) {
5237 for (d = codec->slave_dig_outs; *d; d++)
5238 snd_hda_codec_cleanup_stream(codec, *d);
5243 * snd_hda_bus_reboot_notify - call the reboot notifier of each codec
5244 * @bus: HD-audio bus
5246 void snd_hda_bus_reboot_notify(struct hda_bus *bus)
5248 struct hda_codec *codec;
5252 list_for_each_entry(codec, &bus->codec_list, list) {
5253 if (hda_codec_is_power_on(codec) &&
5254 codec->patch_ops.reboot_notify)
5255 codec->patch_ops.reboot_notify(codec);
5258 EXPORT_SYMBOL_GPL(snd_hda_bus_reboot_notify);
5261 * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode
5263 int snd_hda_multi_out_dig_open(struct hda_codec *codec,
5264 struct hda_multi_out *mout)
5266 mutex_lock(&codec->spdif_mutex);
5267 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
5268 /* already opened as analog dup; reset it once */
5269 cleanup_dig_out_stream(codec, mout->dig_out_nid);
5270 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
5271 mutex_unlock(&codec->spdif_mutex);
5274 EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_open);
5277 * snd_hda_multi_out_dig_prepare - prepare the digital out stream
5279 int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
5280 struct hda_multi_out *mout,
5281 unsigned int stream_tag,
5282 unsigned int format,
5283 struct snd_pcm_substream *substream)
5285 mutex_lock(&codec->spdif_mutex);
5286 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
5287 mutex_unlock(&codec->spdif_mutex);
5290 EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_prepare);
5293 * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream
5295 int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec,
5296 struct hda_multi_out *mout)
5298 mutex_lock(&codec->spdif_mutex);
5299 cleanup_dig_out_stream(codec, mout->dig_out_nid);
5300 mutex_unlock(&codec->spdif_mutex);
5303 EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_cleanup);
5306 * snd_hda_multi_out_dig_close - release the digital out stream
5308 int snd_hda_multi_out_dig_close(struct hda_codec *codec,
5309 struct hda_multi_out *mout)
5311 mutex_lock(&codec->spdif_mutex);
5312 mout->dig_out_used = 0;
5313 mutex_unlock(&codec->spdif_mutex);
5316 EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_close);
5319 * snd_hda_multi_out_analog_open - open analog outputs
5321 * Open analog outputs and set up the hw-constraints.
5322 * If the digital outputs can be opened as slave, open the digital
5325 int snd_hda_multi_out_analog_open(struct hda_codec *codec,
5326 struct hda_multi_out *mout,
5327 struct snd_pcm_substream *substream,
5328 struct hda_pcm_stream *hinfo)
5330 struct snd_pcm_runtime *runtime = substream->runtime;
5331 runtime->hw.channels_max = mout->max_channels;
5332 if (mout->dig_out_nid) {
5333 if (!mout->analog_rates) {
5334 mout->analog_rates = hinfo->rates;
5335 mout->analog_formats = hinfo->formats;
5336 mout->analog_maxbps = hinfo->maxbps;
5338 runtime->hw.rates = mout->analog_rates;
5339 runtime->hw.formats = mout->analog_formats;
5340 hinfo->maxbps = mout->analog_maxbps;
5342 if (!mout->spdif_rates) {
5343 snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
5345 &mout->spdif_formats,
5346 &mout->spdif_maxbps);
5348 mutex_lock(&codec->spdif_mutex);
5349 if (mout->share_spdif) {
5350 if ((runtime->hw.rates & mout->spdif_rates) &&
5351 (runtime->hw.formats & mout->spdif_formats)) {
5352 runtime->hw.rates &= mout->spdif_rates;
5353 runtime->hw.formats &= mout->spdif_formats;
5354 if (mout->spdif_maxbps < hinfo->maxbps)
5355 hinfo->maxbps = mout->spdif_maxbps;
5357 mout->share_spdif = 0;
5358 /* FIXME: need notify? */
5361 mutex_unlock(&codec->spdif_mutex);
5363 return snd_pcm_hw_constraint_step(substream->runtime, 0,
5364 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
5366 EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_open);
5369 * snd_hda_multi_out_analog_prepare - Preapre the analog outputs.
5371 * Set up the i/o for analog out.
5372 * When the digital out is available, copy the front out to digital out, too.
5374 int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
5375 struct hda_multi_out *mout,
5376 unsigned int stream_tag,
5377 unsigned int format,
5378 struct snd_pcm_substream *substream)
5380 const hda_nid_t *nids = mout->dac_nids;
5381 int chs = substream->runtime->channels;
5382 struct hda_spdif_out *spdif;
5385 mutex_lock(&codec->spdif_mutex);
5386 spdif = snd_hda_spdif_out_of_nid(codec, mout->dig_out_nid);
5387 if (mout->dig_out_nid && mout->share_spdif &&
5388 mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
5390 snd_hda_is_supported_format(codec, mout->dig_out_nid,
5392 !(spdif->status & IEC958_AES0_NONAUDIO)) {
5393 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
5394 setup_dig_out_stream(codec, mout->dig_out_nid,
5395 stream_tag, format);
5397 mout->dig_out_used = 0;
5398 cleanup_dig_out_stream(codec, mout->dig_out_nid);
5401 mutex_unlock(&codec->spdif_mutex);
5404 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
5406 if (!mout->no_share_stream &&
5407 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
5408 /* headphone out will just decode front left/right (stereo) */
5409 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
5411 /* extra outputs copied from front */
5412 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
5413 if (!mout->no_share_stream && mout->hp_out_nid[i])
5414 snd_hda_codec_setup_stream(codec,
5415 mout->hp_out_nid[i],
5416 stream_tag, 0, format);
5419 for (i = 1; i < mout->num_dacs; i++) {
5420 if (chs >= (i + 1) * 2) /* independent out */
5421 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
5423 else if (!mout->no_share_stream) /* copy front */
5424 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
5428 /* extra surrounds */
5429 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++) {
5431 if (!mout->extra_out_nid[i])
5433 if (chs >= (i + 1) * 2)
5435 else if (!mout->no_share_stream)
5437 snd_hda_codec_setup_stream(codec, mout->extra_out_nid[i],
5438 stream_tag, ch, format);
5443 EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_prepare);
5446 * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out
5448 int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
5449 struct hda_multi_out *mout)
5451 const hda_nid_t *nids = mout->dac_nids;
5454 for (i = 0; i < mout->num_dacs; i++)
5455 snd_hda_codec_cleanup_stream(codec, nids[i]);
5457 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
5458 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
5459 if (mout->hp_out_nid[i])
5460 snd_hda_codec_cleanup_stream(codec,
5461 mout->hp_out_nid[i]);
5462 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
5463 if (mout->extra_out_nid[i])
5464 snd_hda_codec_cleanup_stream(codec,
5465 mout->extra_out_nid[i]);
5466 mutex_lock(&codec->spdif_mutex);
5467 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
5468 cleanup_dig_out_stream(codec, mout->dig_out_nid);
5469 mout->dig_out_used = 0;
5471 mutex_unlock(&codec->spdif_mutex);
5474 EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_cleanup);
5477 * snd_hda_get_default_vref - Get the default (mic) VREF pin bits
5479 * Guess the suitable VREF pin bits to be set as the pin-control value.
5480 * Note: the function doesn't set the AC_PINCTL_IN_EN bit.
5482 unsigned int snd_hda_get_default_vref(struct hda_codec *codec, hda_nid_t pin)
5484 unsigned int pincap;
5485 unsigned int oldval;
5486 oldval = snd_hda_codec_read(codec, pin, 0,
5487 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
5488 pincap = snd_hda_query_pin_caps(codec, pin);
5489 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
5490 /* Exception: if the default pin setup is vref50, we give it priority */
5491 if ((pincap & AC_PINCAP_VREF_80) && oldval != PIN_VREF50)
5492 return AC_PINCTL_VREF_80;
5493 else if (pincap & AC_PINCAP_VREF_50)
5494 return AC_PINCTL_VREF_50;
5495 else if (pincap & AC_PINCAP_VREF_100)
5496 return AC_PINCTL_VREF_100;
5497 else if (pincap & AC_PINCAP_VREF_GRD)
5498 return AC_PINCTL_VREF_GRD;
5499 return AC_PINCTL_VREF_HIZ;
5501 EXPORT_SYMBOL_GPL(snd_hda_get_default_vref);
5503 /* correct the pin ctl value for matching with the pin cap */
5504 unsigned int snd_hda_correct_pin_ctl(struct hda_codec *codec,
5505 hda_nid_t pin, unsigned int val)
5507 static unsigned int cap_lists[][2] = {
5508 { AC_PINCTL_VREF_100, AC_PINCAP_VREF_100 },
5509 { AC_PINCTL_VREF_80, AC_PINCAP_VREF_80 },
5510 { AC_PINCTL_VREF_50, AC_PINCAP_VREF_50 },
5511 { AC_PINCTL_VREF_GRD, AC_PINCAP_VREF_GRD },
5517 cap = snd_hda_query_pin_caps(codec, pin);
5519 return val; /* don't know what to do... */
5521 if (val & AC_PINCTL_OUT_EN) {
5522 if (!(cap & AC_PINCAP_OUT))
5523 val &= ~(AC_PINCTL_OUT_EN | AC_PINCTL_HP_EN);
5524 else if ((val & AC_PINCTL_HP_EN) && !(cap & AC_PINCAP_HP_DRV))
5525 val &= ~AC_PINCTL_HP_EN;
5528 if (val & AC_PINCTL_IN_EN) {
5529 if (!(cap & AC_PINCAP_IN))
5530 val &= ~(AC_PINCTL_IN_EN | AC_PINCTL_VREFEN);
5532 unsigned int vcap, vref;
5534 vcap = (cap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
5535 vref = val & AC_PINCTL_VREFEN;
5536 for (i = 0; i < ARRAY_SIZE(cap_lists); i++) {
5537 if (vref == cap_lists[i][0] &&
5538 !(vcap & cap_lists[i][1])) {
5539 if (i == ARRAY_SIZE(cap_lists) - 1)
5540 vref = AC_PINCTL_VREF_HIZ;
5542 vref = cap_lists[i + 1][0];
5545 val &= ~AC_PINCTL_VREFEN;
5552 EXPORT_SYMBOL_GPL(snd_hda_correct_pin_ctl);
5554 int _snd_hda_set_pin_ctl(struct hda_codec *codec, hda_nid_t pin,
5555 unsigned int val, bool cached)
5557 val = snd_hda_correct_pin_ctl(codec, pin, val);
5558 snd_hda_codec_set_pin_target(codec, pin, val);
5560 return snd_hda_codec_update_cache(codec, pin, 0,
5561 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
5563 return snd_hda_codec_write(codec, pin, 0,
5564 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
5566 EXPORT_SYMBOL_GPL(_snd_hda_set_pin_ctl);
5569 * snd_hda_add_imux_item - Add an item to input_mux
5571 * When the same label is used already in the existing items, the number
5572 * suffix is appended to the label. This label index number is stored
5573 * to type_idx when non-NULL pointer is given.
5575 int snd_hda_add_imux_item(struct hda_codec *codec,
5576 struct hda_input_mux *imux, const char *label,
5577 int index, int *type_idx)
5579 int i, label_idx = 0;
5580 if (imux->num_items >= HDA_MAX_NUM_INPUTS) {
5581 codec_err(codec, "hda_codec: Too many imux items!\n");
5584 for (i = 0; i < imux->num_items; i++) {
5585 if (!strncmp(label, imux->items[i].label, strlen(label)))
5589 *type_idx = label_idx;
5591 snprintf(imux->items[imux->num_items].label,
5592 sizeof(imux->items[imux->num_items].label),
5593 "%s %d", label, label_idx);
5595 strlcpy(imux->items[imux->num_items].label, label,
5596 sizeof(imux->items[imux->num_items].label));
5597 imux->items[imux->num_items].index = index;
5601 EXPORT_SYMBOL_GPL(snd_hda_add_imux_item);
5610 static void hda_async_suspend(void *data, async_cookie_t cookie)
5612 hda_call_codec_suspend(data, false);
5615 static void hda_async_resume(void *data, async_cookie_t cookie)
5617 hda_call_codec_resume(data);
5621 * snd_hda_suspend - suspend the codecs
5624 * Returns 0 if successful.
5626 int snd_hda_suspend(struct hda_bus *bus)
5628 struct hda_codec *codec;
5629 ASYNC_DOMAIN_EXCLUSIVE(domain);
5631 list_for_each_entry(codec, &bus->codec_list, list) {
5632 cancel_delayed_work_sync(&codec->jackpoll_work);
5633 if (hda_codec_is_power_on(codec)) {
5634 if (bus->num_codecs > 1)
5635 async_schedule_domain(hda_async_suspend, codec,
5638 hda_call_codec_suspend(codec, false);
5642 if (bus->num_codecs > 1)
5643 async_synchronize_full_domain(&domain);
5647 EXPORT_SYMBOL_GPL(snd_hda_suspend);
5650 * snd_hda_resume - resume the codecs
5653 * Returns 0 if successful.
5655 int snd_hda_resume(struct hda_bus *bus)
5657 struct hda_codec *codec;
5658 ASYNC_DOMAIN_EXCLUSIVE(domain);
5660 list_for_each_entry(codec, &bus->codec_list, list) {
5661 if (bus->num_codecs > 1)
5662 async_schedule_domain(hda_async_resume, codec, &domain);
5664 hda_call_codec_resume(codec);
5667 if (bus->num_codecs > 1)
5668 async_synchronize_full_domain(&domain);
5672 EXPORT_SYMBOL_GPL(snd_hda_resume);
5673 #endif /* CONFIG_PM */
5680 * snd_array_new - get a new element from the given array
5681 * @array: the array object
5683 * Get a new element from the given array. If it exceeds the
5684 * pre-allocated array size, re-allocate the array.
5686 * Returns NULL if allocation failed.
5688 void *snd_array_new(struct snd_array *array)
5690 if (snd_BUG_ON(!array->elem_size))
5692 if (array->used >= array->alloced) {
5693 int num = array->alloced + array->alloc_align;
5694 int size = (num + 1) * array->elem_size;
5696 if (snd_BUG_ON(num >= 4096))
5698 nlist = krealloc(array->list, size, GFP_KERNEL | __GFP_ZERO);
5701 array->list = nlist;
5702 array->alloced = num;
5704 return snd_array_elem(array, array->used++);
5706 EXPORT_SYMBOL_GPL(snd_array_new);
5709 * snd_array_free - free the given array elements
5710 * @array: the array object
5712 void snd_array_free(struct snd_array *array)
5719 EXPORT_SYMBOL_GPL(snd_array_free);
5722 * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer
5723 * @pcm: PCM caps bits
5724 * @buf: the string buffer to write
5725 * @buflen: the max buffer length
5727 * used by hda_proc.c and hda_eld.c
5729 void snd_print_pcm_bits(int pcm, char *buf, int buflen)
5731 static unsigned int bits[] = { 8, 16, 20, 24, 32 };
5734 for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
5735 if (pcm & (AC_SUPPCM_BITS_8 << i))
5736 j += snprintf(buf + j, buflen - j, " %d", bits[i]);
5738 buf[j] = '\0'; /* necessary when j == 0 */
5740 EXPORT_SYMBOL_GPL(snd_print_pcm_bits);
5742 MODULE_DESCRIPTION("HDA codec core");
5743 MODULE_LICENSE("GPL");