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;
1481 spin_lock_init(&codec->power_lock);
1482 INIT_DELAYED_WORK(&codec->power_work, hda_power_work);
1483 /* snd_hda_codec_new() marks the codec as power-up, and leave it as is.
1484 * the caller has to power down appropriatley after initialization
1487 hda_keep_power_on(codec);
1490 snd_hda_sysfs_init(codec);
1492 if (codec->bus->modelname) {
1493 codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL);
1494 if (!codec->modelname) {
1500 list_add_tail(&codec->list, &bus->codec_list);
1503 bus->caddr_tbl[codec_addr] = codec;
1505 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1507 if (codec->vendor_id == -1)
1508 /* read again, hopefully the access method was corrected
1509 * in the last read...
1511 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1513 codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1514 AC_PAR_SUBSYSTEM_ID);
1515 codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1518 setup_fg_nodes(codec);
1519 if (!codec->afg && !codec->mfg) {
1520 dev_err(bus->card->dev, "no AFG or MFG node found\n");
1525 fg = codec->afg ? codec->afg : codec->mfg;
1526 err = read_widget_caps(codec, fg);
1528 dev_err(bus->card->dev, "cannot malloc\n");
1531 err = read_pin_defaults(codec);
1535 if (!codec->subsystem_id) {
1536 codec->subsystem_id =
1537 snd_hda_codec_read(codec, fg, 0,
1538 AC_VERB_GET_SUBSYSTEM_ID, 0);
1542 codec->d3_stop_clk = snd_hda_codec_get_supported_ps(codec, fg,
1545 codec->epss = snd_hda_codec_get_supported_ps(codec, fg,
1548 if (!codec->d3_stop_clk || !codec->epss)
1549 bus->power_keep_link_on = 1;
1553 /* power-up all before initialization */
1554 hda_set_power_state(codec, AC_PWRST_D0);
1556 snd_hda_codec_proc_new(codec);
1558 snd_hda_create_hwdep(codec);
1560 sprintf(component, "HDA:%08x,%08x,%08x", codec->vendor_id,
1561 codec->subsystem_id, codec->revision_id);
1562 snd_component_add(codec->bus->card, component);
1564 err = snd_device_new(bus->card, SNDRV_DEV_CODEC, codec, &dev_ops);
1573 snd_hda_codec_free(codec);
1576 EXPORT_SYMBOL_GPL(snd_hda_codec_new);
1578 int snd_hda_codec_update_widgets(struct hda_codec *codec)
1583 /* Assume the function group node does not change,
1584 * only the widget nodes may change.
1586 kfree(codec->wcaps);
1587 fg = codec->afg ? codec->afg : codec->mfg;
1588 err = read_widget_caps(codec, fg);
1590 codec_err(codec, "cannot malloc\n");
1594 snd_array_free(&codec->init_pins);
1595 err = read_pin_defaults(codec);
1599 EXPORT_SYMBOL_GPL(snd_hda_codec_update_widgets);
1602 #if IS_ENABLED(CONFIG_SND_HDA_CODEC_HDMI)
1603 /* if all audio out widgets are digital, let's assume the codec as a HDMI/DP */
1604 static bool is_likely_hdmi_codec(struct hda_codec *codec)
1606 hda_nid_t nid = codec->start_nid;
1609 for (i = 0; i < codec->num_nodes; i++, nid++) {
1610 unsigned int wcaps = get_wcaps(codec, nid);
1611 switch (get_wcaps_type(wcaps)) {
1613 return false; /* HDMI parser supports only HDMI out */
1614 case AC_WID_AUD_OUT:
1615 if (!(wcaps & AC_WCAP_DIGITAL))
1623 /* no HDMI codec parser support */
1624 #define is_likely_hdmi_codec(codec) false
1625 #endif /* CONFIG_SND_HDA_CODEC_HDMI */
1628 * snd_hda_codec_configure - (Re-)configure the HD-audio codec
1629 * @codec: the HDA codec
1631 * Start parsing of the given codec tree and (re-)initialize the whole
1634 * Returns 0 if successful or a negative error code.
1636 int snd_hda_codec_configure(struct hda_codec *codec)
1638 int (*patch)(struct hda_codec *) = NULL;
1641 codec->preset = find_codec_preset(codec);
1642 if (!codec->vendor_name || !codec->chip_name) {
1643 err = get_codec_name(codec);
1648 if (!is_generic_config(codec) && codec->preset)
1649 patch = codec->preset->patch;
1651 unload_parser(codec); /* to be sure */
1652 if (is_likely_hdmi_codec(codec)) {
1653 #if IS_MODULE(CONFIG_SND_HDA_CODEC_HDMI)
1654 patch = load_parser(codec, snd_hda_parse_hdmi_codec);
1655 #elif IS_BUILTIN(CONFIG_SND_HDA_CODEC_HDMI)
1656 patch = snd_hda_parse_hdmi_codec;
1660 #if IS_MODULE(CONFIG_SND_HDA_GENERIC)
1661 patch = load_parser(codec, snd_hda_parse_generic_codec);
1662 #elif IS_BUILTIN(CONFIG_SND_HDA_GENERIC)
1663 patch = snd_hda_parse_generic_codec;
1667 codec_err(codec, "No codec parser is available\n");
1674 unload_parser(codec);
1678 if (codec->patch_ops.unsol_event) {
1679 err = init_unsol_queue(codec->bus);
1684 /* audio codec should override the mixer name */
1685 if (codec->afg || !*codec->bus->card->mixername)
1686 snprintf(codec->bus->card->mixername,
1687 sizeof(codec->bus->card->mixername),
1688 "%s %s", codec->vendor_name, codec->chip_name);
1691 EXPORT_SYMBOL_GPL(snd_hda_codec_configure);
1693 /* update the stream-id if changed */
1694 static void update_pcm_stream_id(struct hda_codec *codec,
1695 struct hda_cvt_setup *p, hda_nid_t nid,
1696 u32 stream_tag, int channel_id)
1698 unsigned int oldval, newval;
1700 if (p->stream_tag != stream_tag || p->channel_id != channel_id) {
1701 oldval = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0);
1702 newval = (stream_tag << 4) | channel_id;
1703 if (oldval != newval)
1704 snd_hda_codec_write(codec, nid, 0,
1705 AC_VERB_SET_CHANNEL_STREAMID,
1707 p->stream_tag = stream_tag;
1708 p->channel_id = channel_id;
1712 /* update the format-id if changed */
1713 static void update_pcm_format(struct hda_codec *codec, struct hda_cvt_setup *p,
1714 hda_nid_t nid, int format)
1716 unsigned int oldval;
1718 if (p->format_id != format) {
1719 oldval = snd_hda_codec_read(codec, nid, 0,
1720 AC_VERB_GET_STREAM_FORMAT, 0);
1721 if (oldval != format) {
1723 snd_hda_codec_write(codec, nid, 0,
1724 AC_VERB_SET_STREAM_FORMAT,
1727 p->format_id = format;
1732 * snd_hda_codec_setup_stream - set up the codec for streaming
1733 * @codec: the CODEC to set up
1734 * @nid: the NID to set up
1735 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
1736 * @channel_id: channel id to pass, zero based.
1737 * @format: stream format.
1739 void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
1741 int channel_id, int format)
1743 struct hda_codec *c;
1744 struct hda_cvt_setup *p;
1752 "hda_codec_setup_stream: NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
1753 nid, stream_tag, channel_id, format);
1754 p = get_hda_cvt_setup(codec, nid);
1758 if (codec->pcm_format_first)
1759 update_pcm_format(codec, p, nid, format);
1760 update_pcm_stream_id(codec, p, nid, stream_tag, channel_id);
1761 if (!codec->pcm_format_first)
1762 update_pcm_format(codec, p, nid, format);
1767 /* make other inactive cvts with the same stream-tag dirty */
1768 type = get_wcaps_type(get_wcaps(codec, nid));
1769 list_for_each_entry(c, &codec->bus->codec_list, list) {
1770 for (i = 0; i < c->cvt_setups.used; i++) {
1771 p = snd_array_elem(&c->cvt_setups, i);
1772 if (!p->active && p->stream_tag == stream_tag &&
1773 get_wcaps_type(get_wcaps(c, p->nid)) == type)
1778 EXPORT_SYMBOL_GPL(snd_hda_codec_setup_stream);
1780 static void really_cleanup_stream(struct hda_codec *codec,
1781 struct hda_cvt_setup *q);
1784 * __snd_hda_codec_cleanup_stream - clean up the codec for closing
1785 * @codec: the CODEC to clean up
1786 * @nid: the NID to clean up
1787 * @do_now: really clean up the stream instead of clearing the active flag
1789 void __snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid,
1792 struct hda_cvt_setup *p;
1797 if (codec->no_sticky_stream)
1800 codec_dbg(codec, "hda_codec_cleanup_stream: NID=0x%x\n", nid);
1801 p = get_hda_cvt_setup(codec, nid);
1803 /* here we just clear the active flag when do_now isn't set;
1804 * actual clean-ups will be done later in
1805 * purify_inactive_streams() called from snd_hda_codec_prpapre()
1808 really_cleanup_stream(codec, p);
1813 EXPORT_SYMBOL_GPL(__snd_hda_codec_cleanup_stream);
1815 static void really_cleanup_stream(struct hda_codec *codec,
1816 struct hda_cvt_setup *q)
1818 hda_nid_t nid = q->nid;
1819 if (q->stream_tag || q->channel_id)
1820 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
1822 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0
1824 memset(q, 0, sizeof(*q));
1828 /* clean up the all conflicting obsolete streams */
1829 static void purify_inactive_streams(struct hda_codec *codec)
1831 struct hda_codec *c;
1834 list_for_each_entry(c, &codec->bus->codec_list, list) {
1835 for (i = 0; i < c->cvt_setups.used; i++) {
1836 struct hda_cvt_setup *p;
1837 p = snd_array_elem(&c->cvt_setups, i);
1839 really_cleanup_stream(c, p);
1845 /* clean up all streams; called from suspend */
1846 static void hda_cleanup_all_streams(struct hda_codec *codec)
1850 for (i = 0; i < codec->cvt_setups.used; i++) {
1851 struct hda_cvt_setup *p = snd_array_elem(&codec->cvt_setups, i);
1853 really_cleanup_stream(codec, p);
1859 * amp access functions
1862 /* FIXME: more better hash key? */
1863 #define HDA_HASH_KEY(nid, dir, idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
1864 #define HDA_HASH_PINCAP_KEY(nid) (u32)((nid) + (0x02 << 24))
1865 #define HDA_HASH_PARPCM_KEY(nid) (u32)((nid) + (0x03 << 24))
1866 #define HDA_HASH_PARSTR_KEY(nid) (u32)((nid) + (0x04 << 24))
1867 #define INFO_AMP_CAPS (1<<0)
1868 #define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
1870 /* initialize the hash table */
1871 static void init_hda_cache(struct hda_cache_rec *cache,
1872 unsigned int record_size)
1874 memset(cache, 0, sizeof(*cache));
1875 memset(cache->hash, 0xff, sizeof(cache->hash));
1876 snd_array_init(&cache->buf, record_size, 64);
1879 static void free_hda_cache(struct hda_cache_rec *cache)
1881 snd_array_free(&cache->buf);
1884 /* query the hash. allocate an entry if not found. */
1885 static struct hda_cache_head *get_hash(struct hda_cache_rec *cache, u32 key)
1887 u16 idx = key % (u16)ARRAY_SIZE(cache->hash);
1888 u16 cur = cache->hash[idx];
1889 struct hda_cache_head *info;
1891 while (cur != 0xffff) {
1892 info = snd_array_elem(&cache->buf, cur);
1893 if (info->key == key)
1900 /* query the hash. allocate an entry if not found. */
1901 static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache,
1904 struct hda_cache_head *info = get_hash(cache, key);
1907 /* add a new hash entry */
1908 info = snd_array_new(&cache->buf);
1911 cur = snd_array_index(&cache->buf, info);
1915 idx = key % (u16)ARRAY_SIZE(cache->hash);
1916 info->next = cache->hash[idx];
1917 cache->hash[idx] = cur;
1922 /* query and allocate an amp hash entry */
1923 static inline struct hda_amp_info *
1924 get_alloc_amp_hash(struct hda_codec *codec, u32 key)
1926 return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key);
1929 /* overwrite the value with the key in the caps hash */
1930 static int write_caps_hash(struct hda_codec *codec, u32 key, unsigned int val)
1932 struct hda_amp_info *info;
1934 mutex_lock(&codec->hash_mutex);
1935 info = get_alloc_amp_hash(codec, key);
1937 mutex_unlock(&codec->hash_mutex);
1940 info->amp_caps = val;
1941 info->head.val |= INFO_AMP_CAPS;
1942 mutex_unlock(&codec->hash_mutex);
1946 /* query the value from the caps hash; if not found, fetch the current
1947 * value from the given function and store in the hash
1950 query_caps_hash(struct hda_codec *codec, hda_nid_t nid, int dir, u32 key,
1951 unsigned int (*func)(struct hda_codec *, hda_nid_t, int))
1953 struct hda_amp_info *info;
1956 mutex_lock(&codec->hash_mutex);
1957 info = get_alloc_amp_hash(codec, key);
1959 mutex_unlock(&codec->hash_mutex);
1962 if (!(info->head.val & INFO_AMP_CAPS)) {
1963 mutex_unlock(&codec->hash_mutex); /* for reentrance */
1964 val = func(codec, nid, dir);
1965 write_caps_hash(codec, key, val);
1967 val = info->amp_caps;
1968 mutex_unlock(&codec->hash_mutex);
1973 static unsigned int read_amp_cap(struct hda_codec *codec, hda_nid_t nid,
1976 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
1978 return snd_hda_param_read(codec, nid,
1979 direction == HDA_OUTPUT ?
1980 AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP);
1984 * query_amp_caps - query AMP capabilities
1985 * @codec: the HD-auio codec
1986 * @nid: the NID to query
1987 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1989 * Query AMP capabilities for the given widget and direction.
1990 * Returns the obtained capability bits.
1992 * When cap bits have been already read, this doesn't read again but
1993 * returns the cached value.
1995 u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
1997 return query_caps_hash(codec, nid, direction,
1998 HDA_HASH_KEY(nid, direction, 0),
2001 EXPORT_SYMBOL_GPL(query_amp_caps);
2004 * snd_hda_override_amp_caps - Override the AMP capabilities
2005 * @codec: the CODEC to clean up
2006 * @nid: the NID to clean up
2007 * @direction: either #HDA_INPUT or #HDA_OUTPUT
2008 * @caps: the capability bits to set
2010 * Override the cached AMP caps bits value by the given one.
2011 * This function is useful if the driver needs to adjust the AMP ranges,
2012 * e.g. limit to 0dB, etc.
2014 * Returns zero if successful or a negative error code.
2016 int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
2019 return write_caps_hash(codec, HDA_HASH_KEY(nid, dir, 0), caps);
2021 EXPORT_SYMBOL_GPL(snd_hda_override_amp_caps);
2023 static unsigned int read_pin_cap(struct hda_codec *codec, hda_nid_t nid,
2026 return snd_hda_param_read(codec, nid, AC_PAR_PIN_CAP);
2030 * snd_hda_query_pin_caps - Query PIN capabilities
2031 * @codec: the HD-auio codec
2032 * @nid: the NID to query
2034 * Query PIN capabilities for the given widget.
2035 * Returns the obtained capability bits.
2037 * When cap bits have been already read, this doesn't read again but
2038 * returns the cached value.
2040 u32 snd_hda_query_pin_caps(struct hda_codec *codec, hda_nid_t nid)
2042 return query_caps_hash(codec, nid, 0, HDA_HASH_PINCAP_KEY(nid),
2045 EXPORT_SYMBOL_GPL(snd_hda_query_pin_caps);
2048 * snd_hda_override_pin_caps - Override the pin capabilities
2050 * @nid: the NID to override
2051 * @caps: the capability bits to set
2053 * Override the cached PIN capabilitiy bits value by the given one.
2055 * Returns zero if successful or a negative error code.
2057 int snd_hda_override_pin_caps(struct hda_codec *codec, hda_nid_t nid,
2060 return write_caps_hash(codec, HDA_HASH_PINCAP_KEY(nid), caps);
2062 EXPORT_SYMBOL_GPL(snd_hda_override_pin_caps);
2064 /* read or sync the hash value with the current value;
2065 * call within hash_mutex
2067 static struct hda_amp_info *
2068 update_amp_hash(struct hda_codec *codec, hda_nid_t nid, int ch,
2069 int direction, int index, bool init_only)
2071 struct hda_amp_info *info;
2072 unsigned int parm, val = 0;
2073 bool val_read = false;
2076 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
2079 if (!(info->head.val & INFO_AMP_VOL(ch))) {
2081 mutex_unlock(&codec->hash_mutex);
2082 parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
2083 parm |= direction == HDA_OUTPUT ?
2084 AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
2086 val = snd_hda_codec_read(codec, nid, 0,
2087 AC_VERB_GET_AMP_GAIN_MUTE, parm);
2090 mutex_lock(&codec->hash_mutex);
2093 info->vol[ch] = val;
2094 info->head.val |= INFO_AMP_VOL(ch);
2095 } else if (init_only)
2101 * write the current volume in info to the h/w
2103 static void put_vol_mute(struct hda_codec *codec, unsigned int amp_caps,
2104 hda_nid_t nid, int ch, int direction, int index,
2109 parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
2110 parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
2111 parm |= index << AC_AMP_SET_INDEX_SHIFT;
2112 if ((val & HDA_AMP_MUTE) && !(amp_caps & AC_AMPCAP_MUTE) &&
2113 (amp_caps & AC_AMPCAP_MIN_MUTE))
2114 ; /* set the zero value as a fake mute */
2117 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
2121 * snd_hda_codec_amp_read - Read AMP value
2122 * @codec: HD-audio codec
2123 * @nid: NID to read the AMP value
2124 * @ch: channel (left=0 or right=1)
2125 * @direction: #HDA_INPUT or #HDA_OUTPUT
2126 * @index: the index value (only for input direction)
2128 * Read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
2130 int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
2131 int direction, int index)
2133 struct hda_amp_info *info;
2134 unsigned int val = 0;
2136 mutex_lock(&codec->hash_mutex);
2137 info = update_amp_hash(codec, nid, ch, direction, index, false);
2139 val = info->vol[ch];
2140 mutex_unlock(&codec->hash_mutex);
2143 EXPORT_SYMBOL_GPL(snd_hda_codec_amp_read);
2145 static int codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
2146 int direction, int idx, int mask, int val,
2149 struct hda_amp_info *info;
2151 unsigned int cache_only;
2153 if (snd_BUG_ON(mask & ~0xff))
2157 mutex_lock(&codec->hash_mutex);
2158 info = update_amp_hash(codec, nid, ch, direction, idx, init_only);
2160 mutex_unlock(&codec->hash_mutex);
2163 val |= info->vol[ch] & ~mask;
2164 if (info->vol[ch] == val) {
2165 mutex_unlock(&codec->hash_mutex);
2168 info->vol[ch] = val;
2169 cache_only = info->head.dirty = codec->cached_write;
2170 caps = info->amp_caps;
2171 mutex_unlock(&codec->hash_mutex);
2173 put_vol_mute(codec, caps, nid, ch, direction, idx, val);
2178 * snd_hda_codec_amp_update - update the AMP value
2179 * @codec: HD-audio codec
2180 * @nid: NID to read the AMP value
2181 * @ch: channel (left=0 or right=1)
2182 * @direction: #HDA_INPUT or #HDA_OUTPUT
2183 * @idx: the index value (only for input direction)
2184 * @mask: bit mask to set
2185 * @val: the bits value to set
2187 * Update the AMP value with a bit mask.
2188 * Returns 0 if the value is unchanged, 1 if changed.
2190 int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
2191 int direction, int idx, int mask, int val)
2193 return codec_amp_update(codec, nid, ch, direction, idx, mask, val, false);
2195 EXPORT_SYMBOL_GPL(snd_hda_codec_amp_update);
2198 * snd_hda_codec_amp_stereo - update the AMP stereo values
2199 * @codec: HD-audio codec
2200 * @nid: NID to read the AMP value
2201 * @direction: #HDA_INPUT or #HDA_OUTPUT
2202 * @idx: the index value (only for input direction)
2203 * @mask: bit mask to set
2204 * @val: the bits value to set
2206 * Update the AMP values like snd_hda_codec_amp_update(), but for a
2207 * stereo widget with the same mask and value.
2209 int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
2210 int direction, int idx, int mask, int val)
2214 if (snd_BUG_ON(mask & ~0xff))
2216 for (ch = 0; ch < 2; ch++)
2217 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
2221 EXPORT_SYMBOL_GPL(snd_hda_codec_amp_stereo);
2223 /* Works like snd_hda_codec_amp_update() but it writes the value only at
2224 * the first access. If the amp was already initialized / updated beforehand,
2225 * this does nothing.
2227 int snd_hda_codec_amp_init(struct hda_codec *codec, hda_nid_t nid, int ch,
2228 int dir, int idx, int mask, int val)
2230 return codec_amp_update(codec, nid, ch, dir, idx, mask, val, true);
2232 EXPORT_SYMBOL_GPL(snd_hda_codec_amp_init);
2234 int snd_hda_codec_amp_init_stereo(struct hda_codec *codec, hda_nid_t nid,
2235 int dir, int idx, int mask, int val)
2239 if (snd_BUG_ON(mask & ~0xff))
2241 for (ch = 0; ch < 2; ch++)
2242 ret |= snd_hda_codec_amp_init(codec, nid, ch, dir,
2246 EXPORT_SYMBOL_GPL(snd_hda_codec_amp_init_stereo);
2249 * snd_hda_codec_resume_amp - Resume all AMP commands from the cache
2250 * @codec: HD-audio codec
2252 * Resume the all amp commands from the cache.
2254 void snd_hda_codec_resume_amp(struct hda_codec *codec)
2258 mutex_lock(&codec->hash_mutex);
2259 codec->cached_write = 0;
2260 for (i = 0; i < codec->amp_cache.buf.used; i++) {
2261 struct hda_amp_info *buffer;
2264 unsigned int idx, dir, ch;
2265 struct hda_amp_info info;
2267 buffer = snd_array_elem(&codec->amp_cache.buf, i);
2268 if (!buffer->head.dirty)
2270 buffer->head.dirty = 0;
2272 key = info.head.key;
2276 idx = (key >> 16) & 0xff;
2277 dir = (key >> 24) & 0xff;
2278 for (ch = 0; ch < 2; ch++) {
2279 if (!(info.head.val & INFO_AMP_VOL(ch)))
2281 mutex_unlock(&codec->hash_mutex);
2282 put_vol_mute(codec, info.amp_caps, nid, ch, dir, idx,
2284 mutex_lock(&codec->hash_mutex);
2287 mutex_unlock(&codec->hash_mutex);
2289 EXPORT_SYMBOL_GPL(snd_hda_codec_resume_amp);
2291 static u32 get_amp_max_value(struct hda_codec *codec, hda_nid_t nid, int dir,
2294 u32 caps = query_amp_caps(codec, nid, dir);
2296 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
2303 * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer
2305 * The control element is supposed to have the private_value field
2306 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2308 int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
2309 struct snd_ctl_elem_info *uinfo)
2311 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2312 u16 nid = get_amp_nid(kcontrol);
2313 u8 chs = get_amp_channels(kcontrol);
2314 int dir = get_amp_direction(kcontrol);
2315 unsigned int ofs = get_amp_offset(kcontrol);
2317 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2318 uinfo->count = chs == 3 ? 2 : 1;
2319 uinfo->value.integer.min = 0;
2320 uinfo->value.integer.max = get_amp_max_value(codec, nid, dir, ofs);
2321 if (!uinfo->value.integer.max) {
2323 "num_steps = 0 for NID=0x%x (ctl = %s)\n",
2324 nid, kcontrol->id.name);
2329 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_info);
2332 static inline unsigned int
2333 read_amp_value(struct hda_codec *codec, hda_nid_t nid,
2334 int ch, int dir, int idx, unsigned int ofs)
2337 val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
2338 val &= HDA_AMP_VOLMASK;
2347 update_amp_value(struct hda_codec *codec, hda_nid_t nid,
2348 int ch, int dir, int idx, unsigned int ofs,
2351 unsigned int maxval;
2355 /* ofs = 0: raw max value */
2356 maxval = get_amp_max_value(codec, nid, dir, 0);
2359 return snd_hda_codec_amp_update(codec, nid, ch, dir, idx,
2360 HDA_AMP_VOLMASK, val);
2364 * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume
2366 * The control element is supposed to have the private_value field
2367 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2369 int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
2370 struct snd_ctl_elem_value *ucontrol)
2372 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2373 hda_nid_t nid = get_amp_nid(kcontrol);
2374 int chs = get_amp_channels(kcontrol);
2375 int dir = get_amp_direction(kcontrol);
2376 int idx = get_amp_index(kcontrol);
2377 unsigned int ofs = get_amp_offset(kcontrol);
2378 long *valp = ucontrol->value.integer.value;
2381 *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs);
2383 *valp = read_amp_value(codec, nid, 1, dir, idx, ofs);
2386 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_get);
2389 * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume
2391 * The control element is supposed to have the private_value field
2392 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2394 int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
2395 struct snd_ctl_elem_value *ucontrol)
2397 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2398 hda_nid_t nid = get_amp_nid(kcontrol);
2399 int chs = get_amp_channels(kcontrol);
2400 int dir = get_amp_direction(kcontrol);
2401 int idx = get_amp_index(kcontrol);
2402 unsigned int ofs = get_amp_offset(kcontrol);
2403 long *valp = ucontrol->value.integer.value;
2406 snd_hda_power_up(codec);
2408 change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp);
2412 change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp);
2413 snd_hda_power_down(codec);
2416 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_put);
2419 * snd_hda_mixer_amp_volume_put - TLV callback for a standard AMP mixer volume
2421 * The control element is supposed to have the private_value field
2422 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2424 int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2425 unsigned int size, unsigned int __user *_tlv)
2427 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2428 hda_nid_t nid = get_amp_nid(kcontrol);
2429 int dir = get_amp_direction(kcontrol);
2430 unsigned int ofs = get_amp_offset(kcontrol);
2431 bool min_mute = get_amp_min_mute(kcontrol);
2432 u32 caps, val1, val2;
2434 if (size < 4 * sizeof(unsigned int))
2436 caps = query_amp_caps(codec, nid, dir);
2437 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2438 val2 = (val2 + 1) * 25;
2439 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
2441 val1 = ((int)val1) * ((int)val2);
2442 if (min_mute || (caps & AC_AMPCAP_MIN_MUTE))
2443 val2 |= TLV_DB_SCALE_MUTE;
2444 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
2446 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
2448 if (put_user(val1, _tlv + 2))
2450 if (put_user(val2, _tlv + 3))
2454 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_tlv);
2457 * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control
2458 * @codec: HD-audio codec
2459 * @nid: NID of a reference widget
2460 * @dir: #HDA_INPUT or #HDA_OUTPUT
2461 * @tlv: TLV data to be stored, at least 4 elements
2463 * Set (static) TLV data for a virtual master volume using the AMP caps
2464 * obtained from the reference NID.
2465 * The volume range is recalculated as if the max volume is 0dB.
2467 void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
2473 caps = query_amp_caps(codec, nid, dir);
2474 nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
2475 step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2476 step = (step + 1) * 25;
2477 tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
2478 tlv[1] = 2 * sizeof(unsigned int);
2479 tlv[2] = -nums * step;
2482 EXPORT_SYMBOL_GPL(snd_hda_set_vmaster_tlv);
2484 /* find a mixer control element with the given name */
2485 static struct snd_kcontrol *
2486 find_mixer_ctl(struct hda_codec *codec, const char *name, int dev, int idx)
2488 struct snd_ctl_elem_id id;
2489 memset(&id, 0, sizeof(id));
2490 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
2493 if (snd_BUG_ON(strlen(name) >= sizeof(id.name)))
2495 strcpy(id.name, name);
2496 return snd_ctl_find_id(codec->bus->card, &id);
2500 * snd_hda_find_mixer_ctl - Find a mixer control element with the given name
2501 * @codec: HD-audio codec
2502 * @name: ctl id name string
2504 * Get the control element with the given id string and IFACE_MIXER.
2506 struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
2509 return find_mixer_ctl(codec, name, 0, 0);
2511 EXPORT_SYMBOL_GPL(snd_hda_find_mixer_ctl);
2513 static int find_empty_mixer_ctl_idx(struct hda_codec *codec, const char *name,
2517 /* 16 ctlrs should be large enough */
2518 for (i = 0, idx = start_idx; i < 16; i++, idx++) {
2519 if (!find_mixer_ctl(codec, name, 0, idx))
2526 * snd_hda_ctl_add - Add a control element and assign to the codec
2527 * @codec: HD-audio codec
2528 * @nid: corresponding NID (optional)
2529 * @kctl: the control element to assign
2531 * Add the given control element to an array inside the codec instance.
2532 * All control elements belonging to a codec are supposed to be added
2533 * by this function so that a proper clean-up works at the free or
2534 * reconfiguration time.
2536 * If non-zero @nid is passed, the NID is assigned to the control element.
2537 * The assignment is shown in the codec proc file.
2539 * snd_hda_ctl_add() checks the control subdev id field whether
2540 * #HDA_SUBDEV_NID_FLAG bit is set. If set (and @nid is zero), the lower
2541 * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit
2542 * specifies if kctl->private_value is a HDA amplifier value.
2544 int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid,
2545 struct snd_kcontrol *kctl)
2548 unsigned short flags = 0;
2549 struct hda_nid_item *item;
2551 if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) {
2552 flags |= HDA_NID_ITEM_AMP;
2554 nid = get_amp_nid_(kctl->private_value);
2556 if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0)
2557 nid = kctl->id.subdevice & 0xffff;
2558 if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG))
2559 kctl->id.subdevice = 0;
2560 err = snd_ctl_add(codec->bus->card, kctl);
2563 item = snd_array_new(&codec->mixers);
2568 item->flags = flags;
2571 EXPORT_SYMBOL_GPL(snd_hda_ctl_add);
2574 * snd_hda_add_nid - Assign a NID to a control element
2575 * @codec: HD-audio codec
2576 * @nid: corresponding NID (optional)
2577 * @kctl: the control element to assign
2578 * @index: index to kctl
2580 * Add the given control element to an array inside the codec instance.
2581 * This function is used when #snd_hda_ctl_add cannot be used for 1:1
2582 * NID:KCTL mapping - for example "Capture Source" selector.
2584 int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl,
2585 unsigned int index, hda_nid_t nid)
2587 struct hda_nid_item *item;
2590 item = snd_array_new(&codec->nids);
2594 item->index = index;
2598 codec_err(codec, "no NID for mapping control %s:%d:%d\n",
2599 kctl->id.name, kctl->id.index, index);
2602 EXPORT_SYMBOL_GPL(snd_hda_add_nid);
2605 * snd_hda_ctls_clear - Clear all controls assigned to the given codec
2606 * @codec: HD-audio codec
2608 void snd_hda_ctls_clear(struct hda_codec *codec)
2611 struct hda_nid_item *items = codec->mixers.list;
2612 for (i = 0; i < codec->mixers.used; i++)
2613 snd_ctl_remove(codec->bus->card, items[i].kctl);
2614 snd_array_free(&codec->mixers);
2615 snd_array_free(&codec->nids);
2618 /* pseudo device locking
2619 * toggle card->shutdown to allow/disallow the device access (as a hack)
2621 int snd_hda_lock_devices(struct hda_bus *bus)
2623 struct snd_card *card = bus->card;
2624 struct hda_codec *codec;
2626 spin_lock(&card->files_lock);
2630 if (!list_empty(&card->ctl_files))
2633 list_for_each_entry(codec, &bus->codec_list, list) {
2635 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
2636 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
2639 if (cpcm->pcm->streams[0].substream_opened ||
2640 cpcm->pcm->streams[1].substream_opened)
2644 spin_unlock(&card->files_lock);
2650 spin_unlock(&card->files_lock);
2653 EXPORT_SYMBOL_GPL(snd_hda_lock_devices);
2655 void snd_hda_unlock_devices(struct hda_bus *bus)
2657 struct snd_card *card = bus->card;
2660 spin_lock(&card->files_lock);
2662 spin_unlock(&card->files_lock);
2664 EXPORT_SYMBOL_GPL(snd_hda_unlock_devices);
2667 * snd_hda_codec_reset - Clear all objects assigned to the codec
2668 * @codec: HD-audio codec
2670 * This frees the all PCM and control elements assigned to the codec, and
2671 * clears the caches and restores the pin default configurations.
2673 * When a device is being used, it returns -EBSY. If successfully freed,
2676 int snd_hda_codec_reset(struct hda_codec *codec)
2678 struct hda_bus *bus = codec->bus;
2679 struct snd_card *card = bus->card;
2682 if (snd_hda_lock_devices(bus) < 0)
2685 /* OK, let it free */
2686 cancel_delayed_work_sync(&codec->jackpoll_work);
2688 cancel_delayed_work_sync(&codec->power_work);
2689 flush_workqueue(bus->workq);
2691 snd_hda_ctls_clear(codec);
2693 for (i = 0; i < codec->num_pcms; i++) {
2694 if (codec->pcm_info[i].pcm) {
2695 snd_device_free(card, codec->pcm_info[i].pcm);
2696 clear_bit(codec->pcm_info[i].device,
2700 snd_hda_detach_beep_device(codec);
2701 if (codec->patch_ops.free)
2702 codec->patch_ops.free(codec);
2703 memset(&codec->patch_ops, 0, sizeof(codec->patch_ops));
2704 snd_hda_jack_tbl_clear(codec);
2705 codec->proc_widget_hook = NULL;
2707 free_hda_cache(&codec->amp_cache);
2708 free_hda_cache(&codec->cmd_cache);
2709 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
2710 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
2711 /* free only driver_pins so that init_pins + user_pins are restored */
2712 snd_array_free(&codec->driver_pins);
2713 snd_array_free(&codec->cvt_setups);
2714 snd_array_free(&codec->spdif_out);
2715 snd_array_free(&codec->verbs);
2716 codec->num_pcms = 0;
2717 codec->pcm_info = NULL;
2718 codec->preset = NULL;
2719 codec->slave_dig_outs = NULL;
2720 codec->spdif_status_reset = 0;
2721 unload_parser(codec);
2722 module_put(codec->owner);
2723 codec->owner = NULL;
2725 /* allow device access again */
2726 snd_hda_unlock_devices(bus);
2730 typedef int (*map_slave_func_t)(void *, struct snd_kcontrol *);
2732 /* apply the function to all matching slave ctls in the mixer list */
2733 static int map_slaves(struct hda_codec *codec, const char * const *slaves,
2734 const char *suffix, map_slave_func_t func, void *data)
2736 struct hda_nid_item *items;
2737 const char * const *s;
2740 items = codec->mixers.list;
2741 for (i = 0; i < codec->mixers.used; i++) {
2742 struct snd_kcontrol *sctl = items[i].kctl;
2743 if (!sctl || sctl->id.iface != SNDRV_CTL_ELEM_IFACE_MIXER)
2745 for (s = slaves; *s; s++) {
2746 char tmpname[sizeof(sctl->id.name)];
2747 const char *name = *s;
2749 snprintf(tmpname, sizeof(tmpname), "%s %s",
2753 if (!strcmp(sctl->id.name, name)) {
2754 err = func(data, sctl);
2764 static int check_slave_present(void *data, struct snd_kcontrol *sctl)
2769 /* guess the value corresponding to 0dB */
2770 static int get_kctl_0dB_offset(struct snd_kcontrol *kctl, int *step_to_check)
2773 const int *tlv = NULL;
2776 if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
2777 /* FIXME: set_fs() hack for obtaining user-space TLV data */
2778 mm_segment_t fs = get_fs();
2780 if (!kctl->tlv.c(kctl, 0, sizeof(_tlv), _tlv))
2783 } else if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_READ)
2785 if (tlv && tlv[0] == SNDRV_CTL_TLVT_DB_SCALE) {
2787 step &= ~TLV_DB_SCALE_MUTE;
2790 if (*step_to_check && *step_to_check != step) {
2791 snd_printk(KERN_ERR "hda_codec: Mismatching dB step for vmaster slave (%d!=%d)\n",
2792 - *step_to_check, step);
2795 *step_to_check = step;
2796 val = -tlv[2] / step;
2801 /* call kctl->put with the given value(s) */
2802 static int put_kctl_with_value(struct snd_kcontrol *kctl, int val)
2804 struct snd_ctl_elem_value *ucontrol;
2805 ucontrol = kzalloc(sizeof(*ucontrol), GFP_KERNEL);
2808 ucontrol->value.integer.value[0] = val;
2809 ucontrol->value.integer.value[1] = val;
2810 kctl->put(kctl, ucontrol);
2815 /* initialize the slave volume with 0dB */
2816 static int init_slave_0dB(void *data, struct snd_kcontrol *slave)
2818 int offset = get_kctl_0dB_offset(slave, data);
2820 put_kctl_with_value(slave, offset);
2824 /* unmute the slave */
2825 static int init_slave_unmute(void *data, struct snd_kcontrol *slave)
2827 return put_kctl_with_value(slave, 1);
2831 * snd_hda_add_vmaster - create a virtual master control and add slaves
2832 * @codec: HD-audio codec
2833 * @name: vmaster control name
2834 * @tlv: TLV data (optional)
2835 * @slaves: slave control names (optional)
2836 * @suffix: suffix string to each slave name (optional)
2837 * @init_slave_vol: initialize slaves to unmute/0dB
2838 * @ctl_ret: store the vmaster kcontrol in return
2840 * Create a virtual master control with the given name. The TLV data
2841 * must be either NULL or a valid data.
2843 * @slaves is a NULL-terminated array of strings, each of which is a
2844 * slave control name. All controls with these names are assigned to
2845 * the new virtual master control.
2847 * This function returns zero if successful or a negative error code.
2849 int __snd_hda_add_vmaster(struct hda_codec *codec, char *name,
2850 unsigned int *tlv, const char * const *slaves,
2851 const char *suffix, bool init_slave_vol,
2852 struct snd_kcontrol **ctl_ret)
2854 struct snd_kcontrol *kctl;
2860 err = map_slaves(codec, slaves, suffix, check_slave_present, NULL);
2862 codec_dbg(codec, "No slave found for %s\n", name);
2865 kctl = snd_ctl_make_virtual_master(name, tlv);
2868 err = snd_hda_ctl_add(codec, 0, kctl);
2872 err = map_slaves(codec, slaves, suffix,
2873 (map_slave_func_t)snd_ctl_add_slave, kctl);
2877 /* init with master mute & zero volume */
2878 put_kctl_with_value(kctl, 0);
2879 if (init_slave_vol) {
2881 map_slaves(codec, slaves, suffix,
2882 tlv ? init_slave_0dB : init_slave_unmute, &step);
2889 EXPORT_SYMBOL_GPL(__snd_hda_add_vmaster);
2892 * mute-LED control using vmaster
2894 static int vmaster_mute_mode_info(struct snd_kcontrol *kcontrol,
2895 struct snd_ctl_elem_info *uinfo)
2897 static const char * const texts[] = {
2898 "On", "Off", "Follow Master"
2902 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2904 uinfo->value.enumerated.items = 3;
2905 index = uinfo->value.enumerated.item;
2908 strcpy(uinfo->value.enumerated.name, texts[index]);
2912 static int vmaster_mute_mode_get(struct snd_kcontrol *kcontrol,
2913 struct snd_ctl_elem_value *ucontrol)
2915 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2916 ucontrol->value.enumerated.item[0] = hook->mute_mode;
2920 static int vmaster_mute_mode_put(struct snd_kcontrol *kcontrol,
2921 struct snd_ctl_elem_value *ucontrol)
2923 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2924 unsigned int old_mode = hook->mute_mode;
2926 hook->mute_mode = ucontrol->value.enumerated.item[0];
2927 if (hook->mute_mode > HDA_VMUTE_FOLLOW_MASTER)
2928 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
2929 if (old_mode == hook->mute_mode)
2931 snd_hda_sync_vmaster_hook(hook);
2935 static struct snd_kcontrol_new vmaster_mute_mode = {
2936 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2937 .name = "Mute-LED Mode",
2938 .info = vmaster_mute_mode_info,
2939 .get = vmaster_mute_mode_get,
2940 .put = vmaster_mute_mode_put,
2944 * Add a mute-LED hook with the given vmaster switch kctl
2945 * "Mute-LED Mode" control is automatically created and associated with
2948 int snd_hda_add_vmaster_hook(struct hda_codec *codec,
2949 struct hda_vmaster_mute_hook *hook,
2950 bool expose_enum_ctl)
2952 struct snd_kcontrol *kctl;
2954 if (!hook->hook || !hook->sw_kctl)
2956 snd_ctl_add_vmaster_hook(hook->sw_kctl, hook->hook, codec);
2957 hook->codec = codec;
2958 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
2959 if (!expose_enum_ctl)
2961 kctl = snd_ctl_new1(&vmaster_mute_mode, hook);
2964 return snd_hda_ctl_add(codec, 0, kctl);
2966 EXPORT_SYMBOL_GPL(snd_hda_add_vmaster_hook);
2969 * Call the hook with the current value for synchronization
2970 * Should be called in init callback
2972 void snd_hda_sync_vmaster_hook(struct hda_vmaster_mute_hook *hook)
2974 if (!hook->hook || !hook->codec)
2976 /* don't call vmaster hook in the destructor since it might have
2977 * been already destroyed
2979 if (hook->codec->bus->shutdown)
2981 switch (hook->mute_mode) {
2982 case HDA_VMUTE_FOLLOW_MASTER:
2983 snd_ctl_sync_vmaster_hook(hook->sw_kctl);
2986 hook->hook(hook->codec, hook->mute_mode);
2990 EXPORT_SYMBOL_GPL(snd_hda_sync_vmaster_hook);
2994 * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch
2996 * The control element is supposed to have the private_value field
2997 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2999 int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
3000 struct snd_ctl_elem_info *uinfo)
3002 int chs = get_amp_channels(kcontrol);
3004 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
3005 uinfo->count = chs == 3 ? 2 : 1;
3006 uinfo->value.integer.min = 0;
3007 uinfo->value.integer.max = 1;
3010 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_info);
3013 * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch
3015 * The control element is supposed to have the private_value field
3016 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
3018 int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
3019 struct snd_ctl_elem_value *ucontrol)
3021 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3022 hda_nid_t nid = get_amp_nid(kcontrol);
3023 int chs = get_amp_channels(kcontrol);
3024 int dir = get_amp_direction(kcontrol);
3025 int idx = get_amp_index(kcontrol);
3026 long *valp = ucontrol->value.integer.value;
3029 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
3030 HDA_AMP_MUTE) ? 0 : 1;
3032 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
3033 HDA_AMP_MUTE) ? 0 : 1;
3036 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_get);
3039 * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch
3041 * The control element is supposed to have the private_value field
3042 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
3044 int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
3045 struct snd_ctl_elem_value *ucontrol)
3047 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3048 hda_nid_t nid = get_amp_nid(kcontrol);
3049 int chs = get_amp_channels(kcontrol);
3050 int dir = get_amp_direction(kcontrol);
3051 int idx = get_amp_index(kcontrol);
3052 long *valp = ucontrol->value.integer.value;
3055 snd_hda_power_up(codec);
3057 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
3059 *valp ? 0 : HDA_AMP_MUTE);
3063 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
3065 *valp ? 0 : HDA_AMP_MUTE);
3066 hda_call_check_power_status(codec, nid);
3067 snd_hda_power_down(codec);
3070 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_put);
3073 * bound volume controls
3075 * bind multiple volumes (# indices, from 0)
3078 #define AMP_VAL_IDX_SHIFT 19
3079 #define AMP_VAL_IDX_MASK (0x0f<<19)
3082 * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control
3084 * The control element is supposed to have the private_value field
3085 * set up via HDA_BIND_MUTE*() macros.
3087 int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
3088 struct snd_ctl_elem_value *ucontrol)
3090 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3094 mutex_lock(&codec->control_mutex);
3095 pval = kcontrol->private_value;
3096 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
3097 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
3098 kcontrol->private_value = pval;
3099 mutex_unlock(&codec->control_mutex);
3102 EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_switch_get);
3105 * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control
3107 * The control element is supposed to have the private_value field
3108 * set up via HDA_BIND_MUTE*() macros.
3110 int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
3111 struct snd_ctl_elem_value *ucontrol)
3113 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3115 int i, indices, err = 0, change = 0;
3117 mutex_lock(&codec->control_mutex);
3118 pval = kcontrol->private_value;
3119 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
3120 for (i = 0; i < indices; i++) {
3121 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
3122 (i << AMP_VAL_IDX_SHIFT);
3123 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
3128 kcontrol->private_value = pval;
3129 mutex_unlock(&codec->control_mutex);
3130 return err < 0 ? err : change;
3132 EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_switch_put);
3135 * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control
3137 * The control element is supposed to have the private_value field
3138 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
3140 int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
3141 struct snd_ctl_elem_info *uinfo)
3143 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3144 struct hda_bind_ctls *c;
3147 mutex_lock(&codec->control_mutex);
3148 c = (struct hda_bind_ctls *)kcontrol->private_value;
3149 kcontrol->private_value = *c->values;
3150 err = c->ops->info(kcontrol, uinfo);
3151 kcontrol->private_value = (long)c;
3152 mutex_unlock(&codec->control_mutex);
3155 EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_info);
3158 * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control
3160 * The control element is supposed to have the private_value field
3161 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
3163 int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
3164 struct snd_ctl_elem_value *ucontrol)
3166 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3167 struct hda_bind_ctls *c;
3170 mutex_lock(&codec->control_mutex);
3171 c = (struct hda_bind_ctls *)kcontrol->private_value;
3172 kcontrol->private_value = *c->values;
3173 err = c->ops->get(kcontrol, ucontrol);
3174 kcontrol->private_value = (long)c;
3175 mutex_unlock(&codec->control_mutex);
3178 EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_get);
3181 * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control
3183 * The control element is supposed to have the private_value field
3184 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
3186 int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
3187 struct snd_ctl_elem_value *ucontrol)
3189 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3190 struct hda_bind_ctls *c;
3191 unsigned long *vals;
3192 int err = 0, change = 0;
3194 mutex_lock(&codec->control_mutex);
3195 c = (struct hda_bind_ctls *)kcontrol->private_value;
3196 for (vals = c->values; *vals; vals++) {
3197 kcontrol->private_value = *vals;
3198 err = c->ops->put(kcontrol, ucontrol);
3203 kcontrol->private_value = (long)c;
3204 mutex_unlock(&codec->control_mutex);
3205 return err < 0 ? err : change;
3207 EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_put);
3210 * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control
3212 * The control element is supposed to have the private_value field
3213 * set up via HDA_BIND_VOL() macro.
3215 int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
3216 unsigned int size, unsigned int __user *tlv)
3218 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3219 struct hda_bind_ctls *c;
3222 mutex_lock(&codec->control_mutex);
3223 c = (struct hda_bind_ctls *)kcontrol->private_value;
3224 kcontrol->private_value = *c->values;
3225 err = c->ops->tlv(kcontrol, op_flag, size, tlv);
3226 kcontrol->private_value = (long)c;
3227 mutex_unlock(&codec->control_mutex);
3230 EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_tlv);
3232 struct hda_ctl_ops snd_hda_bind_vol = {
3233 .info = snd_hda_mixer_amp_volume_info,
3234 .get = snd_hda_mixer_amp_volume_get,
3235 .put = snd_hda_mixer_amp_volume_put,
3236 .tlv = snd_hda_mixer_amp_tlv
3238 EXPORT_SYMBOL_GPL(snd_hda_bind_vol);
3240 struct hda_ctl_ops snd_hda_bind_sw = {
3241 .info = snd_hda_mixer_amp_switch_info,
3242 .get = snd_hda_mixer_amp_switch_get,
3243 .put = snd_hda_mixer_amp_switch_put,
3244 .tlv = snd_hda_mixer_amp_tlv
3246 EXPORT_SYMBOL_GPL(snd_hda_bind_sw);
3249 * SPDIF out controls
3252 static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
3253 struct snd_ctl_elem_info *uinfo)
3255 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
3260 static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
3261 struct snd_ctl_elem_value *ucontrol)
3263 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
3264 IEC958_AES0_NONAUDIO |
3265 IEC958_AES0_CON_EMPHASIS_5015 |
3266 IEC958_AES0_CON_NOT_COPYRIGHT;
3267 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
3268 IEC958_AES1_CON_ORIGINAL;
3272 static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
3273 struct snd_ctl_elem_value *ucontrol)
3275 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
3276 IEC958_AES0_NONAUDIO |
3277 IEC958_AES0_PRO_EMPHASIS_5015;
3281 static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
3282 struct snd_ctl_elem_value *ucontrol)
3284 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3285 int idx = kcontrol->private_value;
3286 struct hda_spdif_out *spdif;
3288 mutex_lock(&codec->spdif_mutex);
3289 spdif = snd_array_elem(&codec->spdif_out, idx);
3290 ucontrol->value.iec958.status[0] = spdif->status & 0xff;
3291 ucontrol->value.iec958.status[1] = (spdif->status >> 8) & 0xff;
3292 ucontrol->value.iec958.status[2] = (spdif->status >> 16) & 0xff;
3293 ucontrol->value.iec958.status[3] = (spdif->status >> 24) & 0xff;
3294 mutex_unlock(&codec->spdif_mutex);
3299 /* convert from SPDIF status bits to HDA SPDIF bits
3300 * bit 0 (DigEn) is always set zero (to be filled later)
3302 static unsigned short convert_from_spdif_status(unsigned int sbits)
3304 unsigned short val = 0;
3306 if (sbits & IEC958_AES0_PROFESSIONAL)
3307 val |= AC_DIG1_PROFESSIONAL;
3308 if (sbits & IEC958_AES0_NONAUDIO)
3309 val |= AC_DIG1_NONAUDIO;
3310 if (sbits & IEC958_AES0_PROFESSIONAL) {
3311 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
3312 IEC958_AES0_PRO_EMPHASIS_5015)
3313 val |= AC_DIG1_EMPHASIS;
3315 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
3316 IEC958_AES0_CON_EMPHASIS_5015)
3317 val |= AC_DIG1_EMPHASIS;
3318 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
3319 val |= AC_DIG1_COPYRIGHT;
3320 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
3321 val |= AC_DIG1_LEVEL;
3322 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
3327 /* convert to SPDIF status bits from HDA SPDIF bits
3329 static unsigned int convert_to_spdif_status(unsigned short val)
3331 unsigned int sbits = 0;
3333 if (val & AC_DIG1_NONAUDIO)
3334 sbits |= IEC958_AES0_NONAUDIO;
3335 if (val & AC_DIG1_PROFESSIONAL)
3336 sbits |= IEC958_AES0_PROFESSIONAL;
3337 if (sbits & IEC958_AES0_PROFESSIONAL) {
3338 if (val & AC_DIG1_EMPHASIS)
3339 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
3341 if (val & AC_DIG1_EMPHASIS)
3342 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
3343 if (!(val & AC_DIG1_COPYRIGHT))
3344 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
3345 if (val & AC_DIG1_LEVEL)
3346 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
3347 sbits |= val & (0x7f << 8);
3352 /* set digital convert verbs both for the given NID and its slaves */
3353 static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
3358 snd_hda_codec_write_cache(codec, nid, 0, verb, val);
3359 d = codec->slave_dig_outs;
3363 snd_hda_codec_write_cache(codec, *d, 0, verb, val);
3366 static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
3370 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1);
3372 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2);
3375 static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
3376 struct snd_ctl_elem_value *ucontrol)
3378 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3379 int idx = kcontrol->private_value;
3380 struct hda_spdif_out *spdif;
3385 mutex_lock(&codec->spdif_mutex);
3386 spdif = snd_array_elem(&codec->spdif_out, idx);
3388 spdif->status = ucontrol->value.iec958.status[0] |
3389 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
3390 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
3391 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
3392 val = convert_from_spdif_status(spdif->status);
3393 val |= spdif->ctls & 1;
3394 change = spdif->ctls != val;
3396 if (change && nid != (u16)-1)
3397 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
3398 mutex_unlock(&codec->spdif_mutex);
3402 #define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
3404 static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
3405 struct snd_ctl_elem_value *ucontrol)
3407 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3408 int idx = kcontrol->private_value;
3409 struct hda_spdif_out *spdif;
3411 mutex_lock(&codec->spdif_mutex);
3412 spdif = snd_array_elem(&codec->spdif_out, idx);
3413 ucontrol->value.integer.value[0] = spdif->ctls & AC_DIG1_ENABLE;
3414 mutex_unlock(&codec->spdif_mutex);
3418 static inline void set_spdif_ctls(struct hda_codec *codec, hda_nid_t nid,
3421 set_dig_out_convert(codec, nid, dig1, dig2);
3422 /* unmute amp switch (if any) */
3423 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
3424 (dig1 & AC_DIG1_ENABLE))
3425 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
3429 static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
3430 struct snd_ctl_elem_value *ucontrol)
3432 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3433 int idx = kcontrol->private_value;
3434 struct hda_spdif_out *spdif;
3439 mutex_lock(&codec->spdif_mutex);
3440 spdif = snd_array_elem(&codec->spdif_out, idx);
3442 val = spdif->ctls & ~AC_DIG1_ENABLE;
3443 if (ucontrol->value.integer.value[0])
3444 val |= AC_DIG1_ENABLE;
3445 change = spdif->ctls != val;
3447 if (change && nid != (u16)-1)
3448 set_spdif_ctls(codec, nid, val & 0xff, -1);
3449 mutex_unlock(&codec->spdif_mutex);
3453 static struct snd_kcontrol_new dig_mixes[] = {
3455 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3456 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3457 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
3458 .info = snd_hda_spdif_mask_info,
3459 .get = snd_hda_spdif_cmask_get,
3462 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3463 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3464 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK),
3465 .info = snd_hda_spdif_mask_info,
3466 .get = snd_hda_spdif_pmask_get,
3469 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3470 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
3471 .info = snd_hda_spdif_mask_info,
3472 .get = snd_hda_spdif_default_get,
3473 .put = snd_hda_spdif_default_put,
3476 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3477 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
3478 .info = snd_hda_spdif_out_switch_info,
3479 .get = snd_hda_spdif_out_switch_get,
3480 .put = snd_hda_spdif_out_switch_put,
3486 * snd_hda_create_dig_out_ctls - create Output SPDIF-related controls
3487 * @codec: the HDA codec
3488 * @associated_nid: NID that new ctls associated with
3489 * @cvt_nid: converter NID
3490 * @type: HDA_PCM_TYPE_*
3491 * Creates controls related with the digital output.
3492 * Called from each patch supporting the digital out.
3494 * Returns 0 if successful, or a negative error code.
3496 int snd_hda_create_dig_out_ctls(struct hda_codec *codec,
3497 hda_nid_t associated_nid,
3502 struct snd_kcontrol *kctl;
3503 struct snd_kcontrol_new *dig_mix;
3505 const int spdif_index = 16;
3506 struct hda_spdif_out *spdif;
3507 struct hda_bus *bus = codec->bus;
3509 if (bus->primary_dig_out_type == HDA_PCM_TYPE_HDMI &&
3510 type == HDA_PCM_TYPE_SPDIF) {
3512 } else if (bus->primary_dig_out_type == HDA_PCM_TYPE_SPDIF &&
3513 type == HDA_PCM_TYPE_HDMI) {
3514 /* suppose a single SPDIF device */
3515 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
3516 kctl = find_mixer_ctl(codec, dig_mix->name, 0, 0);
3519 kctl->id.index = spdif_index;
3521 bus->primary_dig_out_type = HDA_PCM_TYPE_HDMI;
3523 if (!bus->primary_dig_out_type)
3524 bus->primary_dig_out_type = type;
3526 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Playback Switch", idx);
3528 codec_err(codec, "too many IEC958 outputs\n");
3531 spdif = snd_array_new(&codec->spdif_out);
3534 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
3535 kctl = snd_ctl_new1(dig_mix, codec);
3538 kctl->id.index = idx;
3539 kctl->private_value = codec->spdif_out.used - 1;
3540 err = snd_hda_ctl_add(codec, associated_nid, kctl);
3544 spdif->nid = cvt_nid;
3545 spdif->ctls = snd_hda_codec_read(codec, cvt_nid, 0,
3546 AC_VERB_GET_DIGI_CONVERT_1, 0);
3547 spdif->status = convert_to_spdif_status(spdif->ctls);
3550 EXPORT_SYMBOL_GPL(snd_hda_create_dig_out_ctls);
3552 /* get the hda_spdif_out entry from the given NID
3553 * call within spdif_mutex lock
3555 struct hda_spdif_out *snd_hda_spdif_out_of_nid(struct hda_codec *codec,
3559 for (i = 0; i < codec->spdif_out.used; i++) {
3560 struct hda_spdif_out *spdif =
3561 snd_array_elem(&codec->spdif_out, i);
3562 if (spdif->nid == nid)
3567 EXPORT_SYMBOL_GPL(snd_hda_spdif_out_of_nid);
3569 void snd_hda_spdif_ctls_unassign(struct hda_codec *codec, int idx)
3571 struct hda_spdif_out *spdif;
3573 mutex_lock(&codec->spdif_mutex);
3574 spdif = snd_array_elem(&codec->spdif_out, idx);
3575 spdif->nid = (u16)-1;
3576 mutex_unlock(&codec->spdif_mutex);
3578 EXPORT_SYMBOL_GPL(snd_hda_spdif_ctls_unassign);
3580 void snd_hda_spdif_ctls_assign(struct hda_codec *codec, int idx, hda_nid_t nid)
3582 struct hda_spdif_out *spdif;
3585 mutex_lock(&codec->spdif_mutex);
3586 spdif = snd_array_elem(&codec->spdif_out, idx);
3587 if (spdif->nid != nid) {
3590 set_spdif_ctls(codec, nid, val & 0xff, (val >> 8) & 0xff);
3592 mutex_unlock(&codec->spdif_mutex);
3594 EXPORT_SYMBOL_GPL(snd_hda_spdif_ctls_assign);
3597 * SPDIF sharing with analog output
3599 static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
3600 struct snd_ctl_elem_value *ucontrol)
3602 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3603 ucontrol->value.integer.value[0] = mout->share_spdif;
3607 static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
3608 struct snd_ctl_elem_value *ucontrol)
3610 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3611 mout->share_spdif = !!ucontrol->value.integer.value[0];
3615 static struct snd_kcontrol_new spdif_share_sw = {
3616 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3617 .name = "IEC958 Default PCM Playback Switch",
3618 .info = snd_ctl_boolean_mono_info,
3619 .get = spdif_share_sw_get,
3620 .put = spdif_share_sw_put,
3624 * snd_hda_create_spdif_share_sw - create Default PCM switch
3625 * @codec: the HDA codec
3626 * @mout: multi-out instance
3628 int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
3629 struct hda_multi_out *mout)
3631 struct snd_kcontrol *kctl;
3633 if (!mout->dig_out_nid)
3636 kctl = snd_ctl_new1(&spdif_share_sw, mout);
3639 /* ATTENTION: here mout is passed as private_data, instead of codec */
3640 return snd_hda_ctl_add(codec, mout->dig_out_nid, kctl);
3642 EXPORT_SYMBOL_GPL(snd_hda_create_spdif_share_sw);
3648 #define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
3650 static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
3651 struct snd_ctl_elem_value *ucontrol)
3653 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3655 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
3659 static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
3660 struct snd_ctl_elem_value *ucontrol)
3662 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3663 hda_nid_t nid = kcontrol->private_value;
3664 unsigned int val = !!ucontrol->value.integer.value[0];
3667 mutex_lock(&codec->spdif_mutex);
3668 change = codec->spdif_in_enable != val;
3670 codec->spdif_in_enable = val;
3671 snd_hda_codec_write_cache(codec, nid, 0,
3672 AC_VERB_SET_DIGI_CONVERT_1, val);
3674 mutex_unlock(&codec->spdif_mutex);
3678 static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
3679 struct snd_ctl_elem_value *ucontrol)
3681 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3682 hda_nid_t nid = kcontrol->private_value;
3686 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0);
3687 sbits = convert_to_spdif_status(val);
3688 ucontrol->value.iec958.status[0] = sbits;
3689 ucontrol->value.iec958.status[1] = sbits >> 8;
3690 ucontrol->value.iec958.status[2] = sbits >> 16;
3691 ucontrol->value.iec958.status[3] = sbits >> 24;
3695 static struct snd_kcontrol_new dig_in_ctls[] = {
3697 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3698 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH),
3699 .info = snd_hda_spdif_in_switch_info,
3700 .get = snd_hda_spdif_in_switch_get,
3701 .put = snd_hda_spdif_in_switch_put,
3704 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3705 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3706 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
3707 .info = snd_hda_spdif_mask_info,
3708 .get = snd_hda_spdif_in_status_get,
3714 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
3715 * @codec: the HDA codec
3716 * @nid: audio in widget NID
3718 * Creates controls related with the SPDIF input.
3719 * Called from each patch supporting the SPDIF in.
3721 * Returns 0 if successful, or a negative error code.
3723 int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
3726 struct snd_kcontrol *kctl;
3727 struct snd_kcontrol_new *dig_mix;
3730 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Capture Switch", 0);
3732 codec_err(codec, "too many IEC958 inputs\n");
3735 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
3736 kctl = snd_ctl_new1(dig_mix, codec);
3739 kctl->private_value = nid;
3740 err = snd_hda_ctl_add(codec, nid, kctl);
3744 codec->spdif_in_enable =
3745 snd_hda_codec_read(codec, nid, 0,
3746 AC_VERB_GET_DIGI_CONVERT_1, 0) &
3750 EXPORT_SYMBOL_GPL(snd_hda_create_spdif_in_ctls);
3756 /* build a 31bit cache key with the widget id and the command parameter */
3757 #define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
3758 #define get_cmd_cache_nid(key) ((key) & 0xff)
3759 #define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
3762 * snd_hda_codec_write_cache - send a single command with caching
3763 * @codec: the HDA codec
3764 * @nid: NID to send the command
3765 * @flags: optional bit flags
3766 * @verb: the verb to send
3767 * @parm: the parameter for the verb
3769 * Send a single command without waiting for response.
3771 * Returns 0 if successful, or a negative error code.
3773 int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
3774 int flags, unsigned int verb, unsigned int parm)
3777 struct hda_cache_head *c;
3779 unsigned int cache_only;
3781 cache_only = codec->cached_write;
3783 err = snd_hda_codec_write(codec, nid, flags, verb, parm);
3788 /* parm may contain the verb stuff for get/set amp */
3789 verb = verb | (parm >> 8);
3791 key = build_cmd_cache_key(nid, verb);
3792 mutex_lock(&codec->bus->cmd_mutex);
3793 c = get_alloc_hash(&codec->cmd_cache, key);
3796 c->dirty = cache_only;
3798 mutex_unlock(&codec->bus->cmd_mutex);
3801 EXPORT_SYMBOL_GPL(snd_hda_codec_write_cache);
3804 * snd_hda_codec_update_cache - check cache and write the cmd only when needed
3805 * @codec: the HDA codec
3806 * @nid: NID to send the command
3807 * @flags: optional bit flags
3808 * @verb: the verb to send
3809 * @parm: the parameter for the verb
3811 * This function works like snd_hda_codec_write_cache(), but it doesn't send
3812 * command if the parameter is already identical with the cached value.
3813 * If not, it sends the command and refreshes the cache.
3815 * Returns 0 if successful, or a negative error code.
3817 int snd_hda_codec_update_cache(struct hda_codec *codec, hda_nid_t nid,
3818 int flags, unsigned int verb, unsigned int parm)
3820 struct hda_cache_head *c;
3823 /* parm may contain the verb stuff for get/set amp */
3824 verb = verb | (parm >> 8);
3826 key = build_cmd_cache_key(nid, verb);
3827 mutex_lock(&codec->bus->cmd_mutex);
3828 c = get_hash(&codec->cmd_cache, key);
3829 if (c && c->val == parm) {
3830 mutex_unlock(&codec->bus->cmd_mutex);
3833 mutex_unlock(&codec->bus->cmd_mutex);
3834 return snd_hda_codec_write_cache(codec, nid, flags, verb, parm);
3836 EXPORT_SYMBOL_GPL(snd_hda_codec_update_cache);
3839 * snd_hda_codec_resume_cache - Resume the all commands from the cache
3840 * @codec: HD-audio codec
3842 * Execute all verbs recorded in the command caches to resume.
3844 void snd_hda_codec_resume_cache(struct hda_codec *codec)
3848 mutex_lock(&codec->hash_mutex);
3849 codec->cached_write = 0;
3850 for (i = 0; i < codec->cmd_cache.buf.used; i++) {
3851 struct hda_cache_head *buffer;
3854 buffer = snd_array_elem(&codec->cmd_cache.buf, i);
3861 mutex_unlock(&codec->hash_mutex);
3862 snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
3863 get_cmd_cache_cmd(key), buffer->val);
3864 mutex_lock(&codec->hash_mutex);
3866 mutex_unlock(&codec->hash_mutex);
3868 EXPORT_SYMBOL_GPL(snd_hda_codec_resume_cache);
3871 * snd_hda_sequence_write_cache - sequence writes with caching
3872 * @codec: the HDA codec
3873 * @seq: VERB array to send
3875 * Send the commands sequentially from the given array.
3876 * Thte commands are recorded on cache for power-save and resume.
3877 * The array must be terminated with NID=0.
3879 void snd_hda_sequence_write_cache(struct hda_codec *codec,
3880 const struct hda_verb *seq)
3882 for (; seq->nid; seq++)
3883 snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
3886 EXPORT_SYMBOL_GPL(snd_hda_sequence_write_cache);
3889 * snd_hda_codec_flush_cache - Execute all pending (cached) amps / verbs
3890 * @codec: HD-audio codec
3892 void snd_hda_codec_flush_cache(struct hda_codec *codec)
3894 snd_hda_codec_resume_amp(codec);
3895 snd_hda_codec_resume_cache(codec);
3897 EXPORT_SYMBOL_GPL(snd_hda_codec_flush_cache);
3899 void snd_hda_codec_set_power_to_all(struct hda_codec *codec, hda_nid_t fg,
3900 unsigned int power_state)
3902 hda_nid_t nid = codec->start_nid;
3905 for (i = 0; i < codec->num_nodes; i++, nid++) {
3906 unsigned int wcaps = get_wcaps(codec, nid);
3907 unsigned int state = power_state;
3908 if (!(wcaps & AC_WCAP_POWER))
3910 if (codec->power_filter) {
3911 state = codec->power_filter(codec, nid, power_state);
3912 if (state != power_state && power_state == AC_PWRST_D3)
3915 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE,
3919 EXPORT_SYMBOL_GPL(snd_hda_codec_set_power_to_all);
3922 * supported power states check
3924 static bool snd_hda_codec_get_supported_ps(struct hda_codec *codec, hda_nid_t fg,
3925 unsigned int power_state)
3927 int sup = snd_hda_param_read(codec, fg, AC_PAR_POWER_STATE);
3931 if (sup & power_state)
3938 * wait until the state is reached, returns the current state
3940 static unsigned int hda_sync_power_state(struct hda_codec *codec,
3942 unsigned int power_state)
3944 unsigned long end_time = jiffies + msecs_to_jiffies(500);
3945 unsigned int state, actual_state;
3948 state = snd_hda_codec_read(codec, fg, 0,
3949 AC_VERB_GET_POWER_STATE, 0);
3950 if (state & AC_PWRST_ERROR)
3952 actual_state = (state >> 4) & 0x0f;
3953 if (actual_state == power_state)
3955 if (time_after_eq(jiffies, end_time))
3957 /* wait until the codec reachs to the target state */
3963 /* don't power down the widget if it controls eapd and EAPD_BTLENABLE is set */
3964 unsigned int snd_hda_codec_eapd_power_filter(struct hda_codec *codec,
3966 unsigned int power_state)
3968 if (nid == codec->afg || nid == codec->mfg)
3970 if (power_state == AC_PWRST_D3 &&
3971 get_wcaps_type(get_wcaps(codec, nid)) == AC_WID_PIN &&
3972 (snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD)) {
3973 int eapd = snd_hda_codec_read(codec, nid, 0,
3974 AC_VERB_GET_EAPD_BTLENABLE, 0);
3980 EXPORT_SYMBOL_GPL(snd_hda_codec_eapd_power_filter);
3983 * set power state of the codec, and return the power state
3985 static unsigned int hda_set_power_state(struct hda_codec *codec,
3986 unsigned int power_state)
3988 hda_nid_t fg = codec->afg ? codec->afg : codec->mfg;
3993 /* this delay seems necessary to avoid click noise at power-down */
3994 if (power_state == AC_PWRST_D3) {
3995 if (codec->depop_delay < 0)
3996 msleep(codec->epss ? 10 : 100);
3997 else if (codec->depop_delay > 0)
3998 msleep(codec->depop_delay);
3999 flags = HDA_RW_NO_RESPONSE_FALLBACK;
4002 /* repeat power states setting at most 10 times*/
4003 for (count = 0; count < 10; count++) {
4004 if (codec->patch_ops.set_power_state)
4005 codec->patch_ops.set_power_state(codec, fg,
4008 state = power_state;
4009 if (codec->power_filter)
4010 state = codec->power_filter(codec, fg, state);
4011 if (state == power_state || power_state != AC_PWRST_D3)
4012 snd_hda_codec_read(codec, fg, flags,
4013 AC_VERB_SET_POWER_STATE,
4015 snd_hda_codec_set_power_to_all(codec, fg, power_state);
4017 state = hda_sync_power_state(codec, fg, power_state);
4018 if (!(state & AC_PWRST_ERROR))
4025 /* sync power states of all widgets;
4026 * this is called at the end of codec parsing
4028 static void sync_power_up_states(struct hda_codec *codec)
4030 hda_nid_t nid = codec->start_nid;
4033 /* don't care if no filter is used */
4034 if (!codec->power_filter)
4037 for (i = 0; i < codec->num_nodes; i++, nid++) {
4038 unsigned int wcaps = get_wcaps(codec, nid);
4039 unsigned int target;
4040 if (!(wcaps & AC_WCAP_POWER))
4042 target = codec->power_filter(codec, nid, AC_PWRST_D0);
4043 if (target == AC_PWRST_D0)
4045 if (!snd_hda_check_power_state(codec, nid, target))
4046 snd_hda_codec_write(codec, nid, 0,
4047 AC_VERB_SET_POWER_STATE, target);
4051 #ifdef CONFIG_SND_HDA_RECONFIG
4052 /* execute additional init verbs */
4053 static void hda_exec_init_verbs(struct hda_codec *codec)
4055 if (codec->init_verbs.list)
4056 snd_hda_sequence_write(codec, codec->init_verbs.list);
4059 static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
4064 * call suspend and power-down; used both from PM and power-save
4065 * this function returns the power state in the end
4067 static unsigned int hda_call_codec_suspend(struct hda_codec *codec, bool in_wq)
4073 if (codec->patch_ops.suspend)
4074 codec->patch_ops.suspend(codec);
4075 hda_cleanup_all_streams(codec);
4076 state = hda_set_power_state(codec, AC_PWRST_D3);
4077 /* Cancel delayed work if we aren't currently running from it. */
4079 cancel_delayed_work_sync(&codec->power_work);
4080 spin_lock(&codec->power_lock);
4081 snd_hda_update_power_acct(codec);
4082 trace_hda_power_down(codec);
4083 codec->power_on = 0;
4084 codec->power_transition = 0;
4085 codec->power_jiffies = jiffies;
4086 spin_unlock(&codec->power_lock);
4091 /* mark all entries of cmd and amp caches dirty */
4092 static void hda_mark_cmd_cache_dirty(struct hda_codec *codec)
4095 for (i = 0; i < codec->cmd_cache.buf.used; i++) {
4096 struct hda_cache_head *cmd;
4097 cmd = snd_array_elem(&codec->cmd_cache.buf, i);
4100 for (i = 0; i < codec->amp_cache.buf.used; i++) {
4101 struct hda_amp_info *amp;
4102 amp = snd_array_elem(&codec->amp_cache.buf, i);
4103 amp->head.dirty = 1;
4108 * kick up codec; used both from PM and power-save
4110 static void hda_call_codec_resume(struct hda_codec *codec)
4114 hda_mark_cmd_cache_dirty(codec);
4116 /* set as if powered on for avoiding re-entering the resume
4117 * in the resume / power-save sequence
4119 hda_keep_power_on(codec);
4120 hda_set_power_state(codec, AC_PWRST_D0);
4121 restore_shutup_pins(codec);
4122 hda_exec_init_verbs(codec);
4123 snd_hda_jack_set_dirty_all(codec);
4124 if (codec->patch_ops.resume)
4125 codec->patch_ops.resume(codec);
4127 if (codec->patch_ops.init)
4128 codec->patch_ops.init(codec);
4129 snd_hda_codec_resume_amp(codec);
4130 snd_hda_codec_resume_cache(codec);
4133 if (codec->jackpoll_interval)
4134 hda_jackpoll_work(&codec->jackpoll_work.work);
4136 snd_hda_jack_report_sync(codec);
4139 snd_hda_power_down(codec); /* flag down before returning */
4141 #endif /* CONFIG_PM */
4145 * snd_hda_build_controls - build mixer controls
4148 * Creates mixer controls for each codec included in the bus.
4150 * Returns 0 if successful, otherwise a negative error code.
4152 int snd_hda_build_controls(struct hda_bus *bus)
4154 struct hda_codec *codec;
4156 list_for_each_entry(codec, &bus->codec_list, list) {
4157 int err = snd_hda_codec_build_controls(codec);
4160 "cannot build controls for #%d (error %d)\n",
4162 err = snd_hda_codec_reset(codec);
4165 "cannot revert codec\n");
4172 EXPORT_SYMBOL_GPL(snd_hda_build_controls);
4175 * add standard channel maps if not specified
4177 static int add_std_chmaps(struct hda_codec *codec)
4181 for (i = 0; i < codec->num_pcms; i++) {
4182 for (str = 0; str < 2; str++) {
4183 struct snd_pcm *pcm = codec->pcm_info[i].pcm;
4184 struct hda_pcm_stream *hinfo =
4185 &codec->pcm_info[i].stream[str];
4186 struct snd_pcm_chmap *chmap;
4187 const struct snd_pcm_chmap_elem *elem;
4189 if (codec->pcm_info[i].own_chmap)
4191 if (!pcm || !hinfo->substreams)
4193 elem = hinfo->chmap ? hinfo->chmap : snd_pcm_std_chmaps;
4194 err = snd_pcm_add_chmap_ctls(pcm, str, elem,
4195 hinfo->channels_max,
4199 chmap->channel_mask = SND_PCM_CHMAP_MASK_2468;
4205 /* default channel maps for 2.1 speakers;
4206 * since HD-audio supports only stereo, odd number channels are omitted
4208 const struct snd_pcm_chmap_elem snd_pcm_2_1_chmaps[] = {
4210 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } },
4212 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
4213 SNDRV_CHMAP_LFE, SNDRV_CHMAP_LFE } },
4216 EXPORT_SYMBOL_GPL(snd_pcm_2_1_chmaps);
4218 int snd_hda_codec_build_controls(struct hda_codec *codec)
4221 hda_exec_init_verbs(codec);
4222 /* continue to initialize... */
4223 if (codec->patch_ops.init)
4224 err = codec->patch_ops.init(codec);
4225 if (!err && codec->patch_ops.build_controls)
4226 err = codec->patch_ops.build_controls(codec);
4230 /* we create chmaps here instead of build_pcms */
4231 err = add_std_chmaps(codec);
4235 if (codec->jackpoll_interval)
4236 hda_jackpoll_work(&codec->jackpoll_work.work);
4238 snd_hda_jack_report_sync(codec); /* call at the last init point */
4239 sync_power_up_states(codec);
4246 struct hda_rate_tbl {
4248 unsigned int alsa_bits;
4249 unsigned int hda_fmt;
4252 /* rate = base * mult / div */
4253 #define HDA_RATE(base, mult, div) \
4254 (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \
4255 (((div) - 1) << AC_FMT_DIV_SHIFT))
4257 static struct hda_rate_tbl rate_bits[] = {
4258 /* rate in Hz, ALSA rate bitmask, HDA format value */
4260 /* autodetected value used in snd_hda_query_supported_pcm */
4261 { 8000, SNDRV_PCM_RATE_8000, HDA_RATE(48, 1, 6) },
4262 { 11025, SNDRV_PCM_RATE_11025, HDA_RATE(44, 1, 4) },
4263 { 16000, SNDRV_PCM_RATE_16000, HDA_RATE(48, 1, 3) },
4264 { 22050, SNDRV_PCM_RATE_22050, HDA_RATE(44, 1, 2) },
4265 { 32000, SNDRV_PCM_RATE_32000, HDA_RATE(48, 2, 3) },
4266 { 44100, SNDRV_PCM_RATE_44100, HDA_RATE(44, 1, 1) },
4267 { 48000, SNDRV_PCM_RATE_48000, HDA_RATE(48, 1, 1) },
4268 { 88200, SNDRV_PCM_RATE_88200, HDA_RATE(44, 2, 1) },
4269 { 96000, SNDRV_PCM_RATE_96000, HDA_RATE(48, 2, 1) },
4270 { 176400, SNDRV_PCM_RATE_176400, HDA_RATE(44, 4, 1) },
4271 { 192000, SNDRV_PCM_RATE_192000, HDA_RATE(48, 4, 1) },
4272 #define AC_PAR_PCM_RATE_BITS 11
4273 /* up to bits 10, 384kHZ isn't supported properly */
4275 /* not autodetected value */
4276 { 9600, SNDRV_PCM_RATE_KNOT, HDA_RATE(48, 1, 5) },
4278 { 0 } /* terminator */
4282 * snd_hda_calc_stream_format - calculate format bitset
4283 * @rate: the sample rate
4284 * @channels: the number of channels
4285 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
4286 * @maxbps: the max. bps
4288 * Calculate the format bitset from the given rate, channels and th PCM format.
4290 * Return zero if invalid.
4292 unsigned int snd_hda_calc_stream_format(unsigned int rate,
4293 unsigned int channels,
4294 unsigned int format,
4295 unsigned int maxbps,
4296 unsigned short spdif_ctls)
4299 unsigned int val = 0;
4301 for (i = 0; rate_bits[i].hz; i++)
4302 if (rate_bits[i].hz == rate) {
4303 val = rate_bits[i].hda_fmt;
4306 if (!rate_bits[i].hz) {
4307 snd_printdd("invalid rate %d\n", rate);
4311 if (channels == 0 || channels > 8) {
4312 snd_printdd("invalid channels %d\n", channels);
4315 val |= channels - 1;
4317 switch (snd_pcm_format_width(format)) {
4319 val |= AC_FMT_BITS_8;
4322 val |= AC_FMT_BITS_16;
4327 if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE)
4328 val |= AC_FMT_BITS_32;
4329 else if (maxbps >= 24)
4330 val |= AC_FMT_BITS_24;
4332 val |= AC_FMT_BITS_20;
4335 snd_printdd("invalid format width %d\n",
4336 snd_pcm_format_width(format));
4340 if (spdif_ctls & AC_DIG1_NONAUDIO)
4341 val |= AC_FMT_TYPE_NON_PCM;
4345 EXPORT_SYMBOL_GPL(snd_hda_calc_stream_format);
4347 static unsigned int get_pcm_param(struct hda_codec *codec, hda_nid_t nid,
4350 unsigned int val = 0;
4351 if (nid != codec->afg &&
4352 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD))
4353 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
4354 if (!val || val == -1)
4355 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
4356 if (!val || val == -1)
4361 static unsigned int query_pcm_param(struct hda_codec *codec, hda_nid_t nid)
4363 return query_caps_hash(codec, nid, 0, HDA_HASH_PARPCM_KEY(nid),
4367 static unsigned int get_stream_param(struct hda_codec *codec, hda_nid_t nid,
4370 unsigned int streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
4371 if (!streams || streams == -1)
4372 streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
4373 if (!streams || streams == -1)
4378 static unsigned int query_stream_param(struct hda_codec *codec, hda_nid_t nid)
4380 return query_caps_hash(codec, nid, 0, HDA_HASH_PARSTR_KEY(nid),
4385 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
4386 * @codec: the HDA codec
4387 * @nid: NID to query
4388 * @ratesp: the pointer to store the detected rate bitflags
4389 * @formatsp: the pointer to store the detected formats
4390 * @bpsp: the pointer to store the detected format widths
4392 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
4393 * or @bsps argument is ignored.
4395 * Returns 0 if successful, otherwise a negative error code.
4397 int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
4398 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
4400 unsigned int i, val, wcaps;
4402 wcaps = get_wcaps(codec, nid);
4403 val = query_pcm_param(codec, nid);
4407 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
4409 rates |= rate_bits[i].alsa_bits;
4413 "rates == 0 (nid=0x%x, val=0x%x, ovrd=%i)\n",
4415 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0);
4421 if (formatsp || bpsp) {
4423 unsigned int streams, bps;
4425 streams = query_stream_param(codec, nid);
4430 if (streams & AC_SUPFMT_PCM) {
4431 if (val & AC_SUPPCM_BITS_8) {
4432 formats |= SNDRV_PCM_FMTBIT_U8;
4435 if (val & AC_SUPPCM_BITS_16) {
4436 formats |= SNDRV_PCM_FMTBIT_S16_LE;
4439 if (wcaps & AC_WCAP_DIGITAL) {
4440 if (val & AC_SUPPCM_BITS_32)
4441 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
4442 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
4443 formats |= SNDRV_PCM_FMTBIT_S32_LE;
4444 if (val & AC_SUPPCM_BITS_24)
4446 else if (val & AC_SUPPCM_BITS_20)
4448 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
4449 AC_SUPPCM_BITS_32)) {
4450 formats |= SNDRV_PCM_FMTBIT_S32_LE;
4451 if (val & AC_SUPPCM_BITS_32)
4453 else if (val & AC_SUPPCM_BITS_24)
4455 else if (val & AC_SUPPCM_BITS_20)
4459 #if 0 /* FIXME: CS4206 doesn't work, which is the only codec supporting float */
4460 if (streams & AC_SUPFMT_FLOAT32) {
4461 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
4466 if (streams == AC_SUPFMT_AC3) {
4467 /* should be exclusive */
4468 /* temporary hack: we have still no proper support
4469 * for the direct AC3 stream...
4471 formats |= SNDRV_PCM_FMTBIT_U8;
4476 "formats == 0 (nid=0x%x, val=0x%x, ovrd=%i, streams=0x%x)\n",
4478 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0,
4483 *formatsp = formats;
4490 EXPORT_SYMBOL_GPL(snd_hda_query_supported_pcm);
4493 * snd_hda_is_supported_format - Check the validity of the format
4494 * @codec: HD-audio codec
4495 * @nid: NID to check
4496 * @format: the HD-audio format value to check
4498 * Check whether the given node supports the format value.
4500 * Returns 1 if supported, 0 if not.
4502 int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
4503 unsigned int format)
4506 unsigned int val = 0, rate, stream;
4508 val = query_pcm_param(codec, nid);
4512 rate = format & 0xff00;
4513 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
4514 if (rate_bits[i].hda_fmt == rate) {
4519 if (i >= AC_PAR_PCM_RATE_BITS)
4522 stream = query_stream_param(codec, nid);
4526 if (stream & AC_SUPFMT_PCM) {
4527 switch (format & 0xf0) {
4529 if (!(val & AC_SUPPCM_BITS_8))
4533 if (!(val & AC_SUPPCM_BITS_16))
4537 if (!(val & AC_SUPPCM_BITS_20))
4541 if (!(val & AC_SUPPCM_BITS_24))
4545 if (!(val & AC_SUPPCM_BITS_32))
4552 /* FIXME: check for float32 and AC3? */
4557 EXPORT_SYMBOL_GPL(snd_hda_is_supported_format);
4562 static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
4563 struct hda_codec *codec,
4564 struct snd_pcm_substream *substream)
4569 static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
4570 struct hda_codec *codec,
4571 unsigned int stream_tag,
4572 unsigned int format,
4573 struct snd_pcm_substream *substream)
4575 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4579 static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
4580 struct hda_codec *codec,
4581 struct snd_pcm_substream *substream)
4583 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
4587 static int set_pcm_default_values(struct hda_codec *codec,
4588 struct hda_pcm_stream *info)
4592 /* query support PCM information from the given NID */
4593 if (info->nid && (!info->rates || !info->formats)) {
4594 err = snd_hda_query_supported_pcm(codec, info->nid,
4595 info->rates ? NULL : &info->rates,
4596 info->formats ? NULL : &info->formats,
4597 info->maxbps ? NULL : &info->maxbps);
4601 if (info->ops.open == NULL)
4602 info->ops.open = hda_pcm_default_open_close;
4603 if (info->ops.close == NULL)
4604 info->ops.close = hda_pcm_default_open_close;
4605 if (info->ops.prepare == NULL) {
4606 if (snd_BUG_ON(!info->nid))
4608 info->ops.prepare = hda_pcm_default_prepare;
4610 if (info->ops.cleanup == NULL) {
4611 if (snd_BUG_ON(!info->nid))
4613 info->ops.cleanup = hda_pcm_default_cleanup;
4619 * codec prepare/cleanup entries
4621 int snd_hda_codec_prepare(struct hda_codec *codec,
4622 struct hda_pcm_stream *hinfo,
4623 unsigned int stream,
4624 unsigned int format,
4625 struct snd_pcm_substream *substream)
4628 mutex_lock(&codec->bus->prepare_mutex);
4629 ret = hinfo->ops.prepare(hinfo, codec, stream, format, substream);
4631 purify_inactive_streams(codec);
4632 mutex_unlock(&codec->bus->prepare_mutex);
4635 EXPORT_SYMBOL_GPL(snd_hda_codec_prepare);
4637 void snd_hda_codec_cleanup(struct hda_codec *codec,
4638 struct hda_pcm_stream *hinfo,
4639 struct snd_pcm_substream *substream)
4641 mutex_lock(&codec->bus->prepare_mutex);
4642 hinfo->ops.cleanup(hinfo, codec, substream);
4643 mutex_unlock(&codec->bus->prepare_mutex);
4645 EXPORT_SYMBOL_GPL(snd_hda_codec_cleanup);
4648 const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = {
4649 "Audio", "SPDIF", "HDMI", "Modem"
4653 * get the empty PCM device number to assign
4655 static int get_empty_pcm_device(struct hda_bus *bus, unsigned int type)
4657 /* audio device indices; not linear to keep compatibility */
4658 /* assigned to static slots up to dev#10; if more needed, assign
4659 * the later slot dynamically (when CONFIG_SND_DYNAMIC_MINORS=y)
4661 static int audio_idx[HDA_PCM_NTYPES][5] = {
4662 [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 },
4663 [HDA_PCM_TYPE_SPDIF] = { 1, -1 },
4664 [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 },
4665 [HDA_PCM_TYPE_MODEM] = { 6, -1 },
4669 if (type >= HDA_PCM_NTYPES) {
4670 dev_err(bus->card->dev, "Invalid PCM type %d\n", type);
4674 for (i = 0; audio_idx[type][i] >= 0; i++) {
4675 #ifndef CONFIG_SND_DYNAMIC_MINORS
4676 if (audio_idx[type][i] >= 8)
4679 if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits))
4680 return audio_idx[type][i];
4683 #ifdef CONFIG_SND_DYNAMIC_MINORS
4684 /* non-fixed slots starting from 10 */
4685 for (i = 10; i < 32; i++) {
4686 if (!test_and_set_bit(i, bus->pcm_dev_bits))
4691 dev_warn(bus->card->dev, "Too many %s devices\n",
4692 snd_hda_pcm_type_name[type]);
4693 #ifndef CONFIG_SND_DYNAMIC_MINORS
4694 dev_warn(bus->card->dev,
4695 "Consider building the kernel with CONFIG_SND_DYNAMIC_MINORS=y\n");
4701 * attach a new PCM stream
4703 static int snd_hda_attach_pcm(struct hda_codec *codec, struct hda_pcm *pcm)
4705 struct hda_bus *bus = codec->bus;
4706 struct hda_pcm_stream *info;
4709 if (snd_BUG_ON(!pcm->name))
4711 for (stream = 0; stream < 2; stream++) {
4712 info = &pcm->stream[stream];
4713 if (info->substreams) {
4714 err = set_pcm_default_values(codec, info);
4719 return bus->ops.attach_pcm(bus, codec, pcm);
4722 /* assign all PCMs of the given codec */
4723 int snd_hda_codec_build_pcms(struct hda_codec *codec)
4728 if (!codec->num_pcms) {
4729 if (!codec->patch_ops.build_pcms)
4731 err = codec->patch_ops.build_pcms(codec);
4734 "cannot build PCMs for #%d (error %d)\n",
4736 err = snd_hda_codec_reset(codec);
4739 "cannot revert codec\n");
4744 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
4745 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
4748 if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
4749 continue; /* no substreams assigned */
4752 dev = get_empty_pcm_device(codec->bus, cpcm->pcm_type);
4754 continue; /* no fatal error */
4756 err = snd_hda_attach_pcm(codec, cpcm);
4759 "cannot attach PCM stream %d for codec #%d\n",
4761 continue; /* no fatal error */
4769 * snd_hda_build_pcms - build PCM information
4772 * Create PCM information for each codec included in the bus.
4774 * The build_pcms codec patch is requested to set up codec->num_pcms and
4775 * codec->pcm_info properly. The array is referred by the top-level driver
4776 * to create its PCM instances.
4777 * The allocated codec->pcm_info should be released in codec->patch_ops.free
4780 * At least, substreams, channels_min and channels_max must be filled for
4781 * each stream. substreams = 0 indicates that the stream doesn't exist.
4782 * When rates and/or formats are zero, the supported values are queried
4783 * from the given nid. The nid is used also by the default ops.prepare
4784 * and ops.cleanup callbacks.
4786 * The driver needs to call ops.open in its open callback. Similarly,
4787 * ops.close is supposed to be called in the close callback.
4788 * ops.prepare should be called in the prepare or hw_params callback
4789 * with the proper parameters for set up.
4790 * ops.cleanup should be called in hw_free for clean up of streams.
4792 * This function returns 0 if successful, or a negative error code.
4794 int snd_hda_build_pcms(struct hda_bus *bus)
4796 struct hda_codec *codec;
4798 list_for_each_entry(codec, &bus->codec_list, list) {
4799 int err = snd_hda_codec_build_pcms(codec);
4805 EXPORT_SYMBOL_GPL(snd_hda_build_pcms);
4808 * snd_hda_check_board_config - compare the current codec with the config table
4809 * @codec: the HDA codec
4810 * @num_configs: number of config enums
4811 * @models: array of model name strings
4812 * @tbl: configuration table, terminated by null entries
4814 * Compares the modelname or PCI subsystem id of the current codec with the
4815 * given configuration table. If a matching entry is found, returns its
4816 * config value (supposed to be 0 or positive).
4818 * If no entries are matching, the function returns a negative value.
4820 int snd_hda_check_board_config(struct hda_codec *codec,
4821 int num_configs, const char * const *models,
4822 const struct snd_pci_quirk *tbl)
4824 if (codec->modelname && models) {
4826 for (i = 0; i < num_configs; i++) {
4828 !strcmp(codec->modelname, models[i])) {
4829 codec_info(codec, "model '%s' is selected\n",
4836 if (!codec->bus->pci || !tbl)
4839 tbl = snd_pci_quirk_lookup(codec->bus->pci, tbl);
4842 if (tbl->value >= 0 && tbl->value < num_configs) {
4843 #ifdef CONFIG_SND_DEBUG_VERBOSE
4845 const char *model = NULL;
4847 model = models[tbl->value];
4849 sprintf(tmp, "#%d", tbl->value);
4852 codec_info(codec, "model '%s' is selected for config %x:%x (%s)\n",
4853 model, tbl->subvendor, tbl->subdevice,
4854 (tbl->name ? tbl->name : "Unknown device"));
4860 EXPORT_SYMBOL_GPL(snd_hda_check_board_config);
4863 * snd_hda_check_board_codec_sid_config - compare the current codec
4864 subsystem ID with the
4867 This is important for Gateway notebooks with SB450 HDA Audio
4868 where the vendor ID of the PCI device is:
4869 ATI Technologies Inc SB450 HDA Audio [1002:437b]
4870 and the vendor/subvendor are found only at the codec.
4872 * @codec: the HDA codec
4873 * @num_configs: number of config enums
4874 * @models: array of model name strings
4875 * @tbl: configuration table, terminated by null entries
4877 * Compares the modelname or PCI subsystem id of the current codec with the
4878 * given configuration table. If a matching entry is found, returns its
4879 * config value (supposed to be 0 or positive).
4881 * If no entries are matching, the function returns a negative value.
4883 int snd_hda_check_board_codec_sid_config(struct hda_codec *codec,
4884 int num_configs, const char * const *models,
4885 const struct snd_pci_quirk *tbl)
4887 const struct snd_pci_quirk *q;
4889 /* Search for codec ID */
4890 for (q = tbl; q->subvendor; q++) {
4891 unsigned int mask = 0xffff0000 | q->subdevice_mask;
4892 unsigned int id = (q->subdevice | (q->subvendor << 16)) & mask;
4893 if ((codec->subsystem_id & mask) == id)
4902 if (tbl->value >= 0 && tbl->value < num_configs) {
4903 #ifdef CONFIG_SND_DEBUG_VERBOSE
4905 const char *model = NULL;
4907 model = models[tbl->value];
4909 sprintf(tmp, "#%d", tbl->value);
4912 codec_info(codec, "model '%s' is selected for config %x:%x (%s)\n",
4913 model, tbl->subvendor, tbl->subdevice,
4914 (tbl->name ? tbl->name : "Unknown device"));
4920 EXPORT_SYMBOL_GPL(snd_hda_check_board_codec_sid_config);
4923 * snd_hda_add_new_ctls - create controls from the array
4924 * @codec: the HDA codec
4925 * @knew: the array of struct snd_kcontrol_new
4927 * This helper function creates and add new controls in the given array.
4928 * The array must be terminated with an empty entry as terminator.
4930 * Returns 0 if successful, or a negative error code.
4932 int snd_hda_add_new_ctls(struct hda_codec *codec,
4933 const struct snd_kcontrol_new *knew)
4937 for (; knew->name; knew++) {
4938 struct snd_kcontrol *kctl;
4939 int addr = 0, idx = 0;
4940 if (knew->iface == -1) /* skip this codec private value */
4943 kctl = snd_ctl_new1(knew, codec);
4947 kctl->id.device = addr;
4949 kctl->id.index = idx;
4950 err = snd_hda_ctl_add(codec, 0, kctl);
4953 /* try first with another device index corresponding to
4954 * the codec addr; if it still fails (or it's the
4955 * primary codec), then try another control index
4957 if (!addr && codec->addr)
4959 else if (!idx && !knew->index) {
4960 idx = find_empty_mixer_ctl_idx(codec,
4970 EXPORT_SYMBOL_GPL(snd_hda_add_new_ctls);
4973 static void hda_power_work(struct work_struct *work)
4975 struct hda_codec *codec =
4976 container_of(work, struct hda_codec, power_work.work);
4977 struct hda_bus *bus = codec->bus;
4980 spin_lock(&codec->power_lock);
4981 if (codec->power_transition > 0) { /* during power-up sequence? */
4982 spin_unlock(&codec->power_lock);
4985 if (!codec->power_on || codec->power_count) {
4986 codec->power_transition = 0;
4987 spin_unlock(&codec->power_lock);
4990 spin_unlock(&codec->power_lock);
4992 state = hda_call_codec_suspend(codec, true);
4993 if (!bus->power_keep_link_on && (state & AC_PWRST_CLK_STOP_OK))
4994 hda_call_pm_notify(codec, false);
4997 static void hda_keep_power_on(struct hda_codec *codec)
4999 spin_lock(&codec->power_lock);
5000 codec->power_count++;
5001 codec->power_on = 1;
5002 codec->power_jiffies = jiffies;
5003 spin_unlock(&codec->power_lock);
5004 hda_call_pm_notify(codec, true);
5007 /* update the power on/off account with the current jiffies */
5008 void snd_hda_update_power_acct(struct hda_codec *codec)
5010 unsigned long delta = jiffies - codec->power_jiffies;
5011 if (codec->power_on)
5012 codec->power_on_acct += delta;
5014 codec->power_off_acct += delta;
5015 codec->power_jiffies += delta;
5018 /* Transition to powered up, if wait_power_down then wait for a pending
5019 * transition to D3 to complete. A pending D3 transition is indicated
5020 * with power_transition == -1. */
5021 /* call this with codec->power_lock held! */
5022 static void __snd_hda_power_up(struct hda_codec *codec, bool wait_power_down)
5024 /* Return if power_on or transitioning to power_on, unless currently
5026 if ((codec->power_on || codec->power_transition > 0) &&
5027 !(wait_power_down && codec->power_transition < 0))
5029 spin_unlock(&codec->power_lock);
5031 cancel_delayed_work_sync(&codec->power_work);
5033 spin_lock(&codec->power_lock);
5034 /* If the power down delayed work was cancelled above before starting,
5035 * then there is no need to go through power up here.
5037 if (codec->power_on) {
5038 if (codec->power_transition < 0)
5039 codec->power_transition = 0;
5043 trace_hda_power_up(codec);
5044 snd_hda_update_power_acct(codec);
5045 codec->power_on = 1;
5046 codec->power_jiffies = jiffies;
5047 codec->power_transition = 1; /* avoid reentrance */
5048 spin_unlock(&codec->power_lock);
5050 hda_call_codec_resume(codec);
5052 spin_lock(&codec->power_lock);
5053 codec->power_transition = 0;
5056 #define power_save(codec) \
5057 ((codec)->bus->power_save ? *(codec)->bus->power_save : 0)
5059 /* Transition to powered down */
5060 static void __snd_hda_power_down(struct hda_codec *codec)
5062 if (!codec->power_on || codec->power_count || codec->power_transition)
5065 if (power_save(codec)) {
5066 codec->power_transition = -1; /* avoid reentrance */
5067 queue_delayed_work(codec->bus->workq, &codec->power_work,
5068 msecs_to_jiffies(power_save(codec) * 1000));
5073 * snd_hda_power_save - Power-up/down/sync the codec
5074 * @codec: HD-audio codec
5075 * @delta: the counter delta to change
5077 * Change the power-up counter via @delta, and power up or down the hardware
5078 * appropriately. For the power-down, queue to the delayed action.
5079 * Passing zero to @delta means to synchronize the power state.
5081 void snd_hda_power_save(struct hda_codec *codec, int delta, bool d3wait)
5083 spin_lock(&codec->power_lock);
5084 codec->power_count += delta;
5085 trace_hda_power_count(codec);
5087 __snd_hda_power_up(codec, d3wait);
5089 __snd_hda_power_down(codec);
5090 spin_unlock(&codec->power_lock);
5092 EXPORT_SYMBOL_GPL(snd_hda_power_save);
5095 * snd_hda_check_amp_list_power - Check the amp list and update the power
5096 * @codec: HD-audio codec
5097 * @check: the object containing an AMP list and the status
5098 * @nid: NID to check / update
5100 * Check whether the given NID is in the amp list. If it's in the list,
5101 * check the current AMP status, and update the the power-status according
5102 * to the mute status.
5104 * This function is supposed to be set or called from the check_power_status
5107 int snd_hda_check_amp_list_power(struct hda_codec *codec,
5108 struct hda_loopback_check *check,
5111 const struct hda_amp_list *p;
5114 if (!check->amplist)
5116 for (p = check->amplist; p->nid; p++) {
5121 return 0; /* nothing changed */
5123 for (p = check->amplist; p->nid; p++) {
5124 for (ch = 0; ch < 2; ch++) {
5125 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
5127 if (!(v & HDA_AMP_MUTE) && v > 0) {
5128 if (!check->power_on) {
5129 check->power_on = 1;
5130 snd_hda_power_up(codec);
5136 if (check->power_on) {
5137 check->power_on = 0;
5138 snd_hda_power_down(codec);
5142 EXPORT_SYMBOL_GPL(snd_hda_check_amp_list_power);
5146 * Channel mode helper
5150 * snd_hda_ch_mode_info - Info callback helper for the channel mode enum
5152 int snd_hda_ch_mode_info(struct hda_codec *codec,
5153 struct snd_ctl_elem_info *uinfo,
5154 const struct hda_channel_mode *chmode,
5157 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
5159 uinfo->value.enumerated.items = num_chmodes;
5160 if (uinfo->value.enumerated.item >= num_chmodes)
5161 uinfo->value.enumerated.item = num_chmodes - 1;
5162 sprintf(uinfo->value.enumerated.name, "%dch",
5163 chmode[uinfo->value.enumerated.item].channels);
5166 EXPORT_SYMBOL_GPL(snd_hda_ch_mode_info);
5169 * snd_hda_ch_mode_get - Get callback helper for the channel mode enum
5171 int snd_hda_ch_mode_get(struct hda_codec *codec,
5172 struct snd_ctl_elem_value *ucontrol,
5173 const struct hda_channel_mode *chmode,
5179 for (i = 0; i < num_chmodes; i++) {
5180 if (max_channels == chmode[i].channels) {
5181 ucontrol->value.enumerated.item[0] = i;
5187 EXPORT_SYMBOL_GPL(snd_hda_ch_mode_get);
5190 * snd_hda_ch_mode_put - Put callback helper for the channel mode enum
5192 int snd_hda_ch_mode_put(struct hda_codec *codec,
5193 struct snd_ctl_elem_value *ucontrol,
5194 const struct hda_channel_mode *chmode,
5200 mode = ucontrol->value.enumerated.item[0];
5201 if (mode >= num_chmodes)
5203 if (*max_channelsp == chmode[mode].channels)
5205 /* change the current channel setting */
5206 *max_channelsp = chmode[mode].channels;
5207 if (chmode[mode].sequence)
5208 snd_hda_sequence_write_cache(codec, chmode[mode].sequence);
5211 EXPORT_SYMBOL_GPL(snd_hda_ch_mode_put);
5218 * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum
5220 int snd_hda_input_mux_info(const struct hda_input_mux *imux,
5221 struct snd_ctl_elem_info *uinfo)
5225 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
5227 uinfo->value.enumerated.items = imux->num_items;
5228 if (!imux->num_items)
5230 index = uinfo->value.enumerated.item;
5231 if (index >= imux->num_items)
5232 index = imux->num_items - 1;
5233 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
5236 EXPORT_SYMBOL_GPL(snd_hda_input_mux_info);
5239 * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum
5241 int snd_hda_input_mux_put(struct hda_codec *codec,
5242 const struct hda_input_mux *imux,
5243 struct snd_ctl_elem_value *ucontrol,
5245 unsigned int *cur_val)
5249 if (!imux->num_items)
5251 idx = ucontrol->value.enumerated.item[0];
5252 if (idx >= imux->num_items)
5253 idx = imux->num_items - 1;
5254 if (*cur_val == idx)
5256 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
5257 imux->items[idx].index);
5261 EXPORT_SYMBOL_GPL(snd_hda_input_mux_put);
5265 * process kcontrol info callback of a simple string enum array
5266 * when @num_items is 0 or @texts is NULL, assume a boolean enum array
5268 int snd_hda_enum_helper_info(struct snd_kcontrol *kcontrol,
5269 struct snd_ctl_elem_info *uinfo,
5270 int num_items, const char * const *texts)
5272 static const char * const texts_default[] = {
5273 "Disabled", "Enabled"
5276 if (!texts || !num_items) {
5278 texts = texts_default;
5281 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
5283 uinfo->value.enumerated.items = num_items;
5284 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
5285 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
5286 strcpy(uinfo->value.enumerated.name,
5287 texts[uinfo->value.enumerated.item]);
5290 EXPORT_SYMBOL_GPL(snd_hda_enum_helper_info);
5293 * Multi-channel / digital-out PCM helper functions
5296 /* setup SPDIF output stream */
5297 static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
5298 unsigned int stream_tag, unsigned int format)
5300 struct hda_spdif_out *spdif;
5301 unsigned int curr_fmt;
5304 spdif = snd_hda_spdif_out_of_nid(codec, nid);
5305 curr_fmt = snd_hda_codec_read(codec, nid, 0,
5306 AC_VERB_GET_STREAM_FORMAT, 0);
5307 reset = codec->spdif_status_reset &&
5308 (spdif->ctls & AC_DIG1_ENABLE) &&
5311 /* turn off SPDIF if needed; otherwise the IEC958 bits won't be
5314 set_dig_out_convert(codec, nid,
5315 spdif->ctls & ~AC_DIG1_ENABLE & 0xff,
5317 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
5318 if (codec->slave_dig_outs) {
5320 for (d = codec->slave_dig_outs; *d; d++)
5321 snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
5324 /* turn on again (if needed) */
5326 set_dig_out_convert(codec, nid,
5327 spdif->ctls & 0xff, -1);
5330 static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
5332 snd_hda_codec_cleanup_stream(codec, nid);
5333 if (codec->slave_dig_outs) {
5335 for (d = codec->slave_dig_outs; *d; d++)
5336 snd_hda_codec_cleanup_stream(codec, *d);
5341 * snd_hda_bus_reboot_notify - call the reboot notifier of each codec
5342 * @bus: HD-audio bus
5344 void snd_hda_bus_reboot_notify(struct hda_bus *bus)
5346 struct hda_codec *codec;
5350 list_for_each_entry(codec, &bus->codec_list, list) {
5351 if (hda_codec_is_power_on(codec) &&
5352 codec->patch_ops.reboot_notify)
5353 codec->patch_ops.reboot_notify(codec);
5356 EXPORT_SYMBOL_GPL(snd_hda_bus_reboot_notify);
5359 * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode
5361 int snd_hda_multi_out_dig_open(struct hda_codec *codec,
5362 struct hda_multi_out *mout)
5364 mutex_lock(&codec->spdif_mutex);
5365 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
5366 /* already opened as analog dup; reset it once */
5367 cleanup_dig_out_stream(codec, mout->dig_out_nid);
5368 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
5369 mutex_unlock(&codec->spdif_mutex);
5372 EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_open);
5375 * snd_hda_multi_out_dig_prepare - prepare the digital out stream
5377 int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
5378 struct hda_multi_out *mout,
5379 unsigned int stream_tag,
5380 unsigned int format,
5381 struct snd_pcm_substream *substream)
5383 mutex_lock(&codec->spdif_mutex);
5384 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
5385 mutex_unlock(&codec->spdif_mutex);
5388 EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_prepare);
5391 * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream
5393 int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec,
5394 struct hda_multi_out *mout)
5396 mutex_lock(&codec->spdif_mutex);
5397 cleanup_dig_out_stream(codec, mout->dig_out_nid);
5398 mutex_unlock(&codec->spdif_mutex);
5401 EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_cleanup);
5404 * snd_hda_multi_out_dig_close - release the digital out stream
5406 int snd_hda_multi_out_dig_close(struct hda_codec *codec,
5407 struct hda_multi_out *mout)
5409 mutex_lock(&codec->spdif_mutex);
5410 mout->dig_out_used = 0;
5411 mutex_unlock(&codec->spdif_mutex);
5414 EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_close);
5417 * snd_hda_multi_out_analog_open - open analog outputs
5419 * Open analog outputs and set up the hw-constraints.
5420 * If the digital outputs can be opened as slave, open the digital
5423 int snd_hda_multi_out_analog_open(struct hda_codec *codec,
5424 struct hda_multi_out *mout,
5425 struct snd_pcm_substream *substream,
5426 struct hda_pcm_stream *hinfo)
5428 struct snd_pcm_runtime *runtime = substream->runtime;
5429 runtime->hw.channels_max = mout->max_channels;
5430 if (mout->dig_out_nid) {
5431 if (!mout->analog_rates) {
5432 mout->analog_rates = hinfo->rates;
5433 mout->analog_formats = hinfo->formats;
5434 mout->analog_maxbps = hinfo->maxbps;
5436 runtime->hw.rates = mout->analog_rates;
5437 runtime->hw.formats = mout->analog_formats;
5438 hinfo->maxbps = mout->analog_maxbps;
5440 if (!mout->spdif_rates) {
5441 snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
5443 &mout->spdif_formats,
5444 &mout->spdif_maxbps);
5446 mutex_lock(&codec->spdif_mutex);
5447 if (mout->share_spdif) {
5448 if ((runtime->hw.rates & mout->spdif_rates) &&
5449 (runtime->hw.formats & mout->spdif_formats)) {
5450 runtime->hw.rates &= mout->spdif_rates;
5451 runtime->hw.formats &= mout->spdif_formats;
5452 if (mout->spdif_maxbps < hinfo->maxbps)
5453 hinfo->maxbps = mout->spdif_maxbps;
5455 mout->share_spdif = 0;
5456 /* FIXME: need notify? */
5459 mutex_unlock(&codec->spdif_mutex);
5461 return snd_pcm_hw_constraint_step(substream->runtime, 0,
5462 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
5464 EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_open);
5467 * snd_hda_multi_out_analog_prepare - Preapre the analog outputs.
5469 * Set up the i/o for analog out.
5470 * When the digital out is available, copy the front out to digital out, too.
5472 int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
5473 struct hda_multi_out *mout,
5474 unsigned int stream_tag,
5475 unsigned int format,
5476 struct snd_pcm_substream *substream)
5478 const hda_nid_t *nids = mout->dac_nids;
5479 int chs = substream->runtime->channels;
5480 struct hda_spdif_out *spdif;
5483 mutex_lock(&codec->spdif_mutex);
5484 spdif = snd_hda_spdif_out_of_nid(codec, mout->dig_out_nid);
5485 if (mout->dig_out_nid && mout->share_spdif &&
5486 mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
5488 snd_hda_is_supported_format(codec, mout->dig_out_nid,
5490 !(spdif->status & IEC958_AES0_NONAUDIO)) {
5491 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
5492 setup_dig_out_stream(codec, mout->dig_out_nid,
5493 stream_tag, format);
5495 mout->dig_out_used = 0;
5496 cleanup_dig_out_stream(codec, mout->dig_out_nid);
5499 mutex_unlock(&codec->spdif_mutex);
5502 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
5504 if (!mout->no_share_stream &&
5505 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
5506 /* headphone out will just decode front left/right (stereo) */
5507 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
5509 /* extra outputs copied from front */
5510 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
5511 if (!mout->no_share_stream && mout->hp_out_nid[i])
5512 snd_hda_codec_setup_stream(codec,
5513 mout->hp_out_nid[i],
5514 stream_tag, 0, format);
5517 for (i = 1; i < mout->num_dacs; i++) {
5518 if (chs >= (i + 1) * 2) /* independent out */
5519 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
5521 else if (!mout->no_share_stream) /* copy front */
5522 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
5526 /* extra surrounds */
5527 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++) {
5529 if (!mout->extra_out_nid[i])
5531 if (chs >= (i + 1) * 2)
5533 else if (!mout->no_share_stream)
5535 snd_hda_codec_setup_stream(codec, mout->extra_out_nid[i],
5536 stream_tag, ch, format);
5541 EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_prepare);
5544 * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out
5546 int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
5547 struct hda_multi_out *mout)
5549 const hda_nid_t *nids = mout->dac_nids;
5552 for (i = 0; i < mout->num_dacs; i++)
5553 snd_hda_codec_cleanup_stream(codec, nids[i]);
5555 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
5556 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
5557 if (mout->hp_out_nid[i])
5558 snd_hda_codec_cleanup_stream(codec,
5559 mout->hp_out_nid[i]);
5560 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
5561 if (mout->extra_out_nid[i])
5562 snd_hda_codec_cleanup_stream(codec,
5563 mout->extra_out_nid[i]);
5564 mutex_lock(&codec->spdif_mutex);
5565 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
5566 cleanup_dig_out_stream(codec, mout->dig_out_nid);
5567 mout->dig_out_used = 0;
5569 mutex_unlock(&codec->spdif_mutex);
5572 EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_cleanup);
5575 * snd_hda_get_default_vref - Get the default (mic) VREF pin bits
5577 * Guess the suitable VREF pin bits to be set as the pin-control value.
5578 * Note: the function doesn't set the AC_PINCTL_IN_EN bit.
5580 unsigned int snd_hda_get_default_vref(struct hda_codec *codec, hda_nid_t pin)
5582 unsigned int pincap;
5583 unsigned int oldval;
5584 oldval = snd_hda_codec_read(codec, pin, 0,
5585 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
5586 pincap = snd_hda_query_pin_caps(codec, pin);
5587 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
5588 /* Exception: if the default pin setup is vref50, we give it priority */
5589 if ((pincap & AC_PINCAP_VREF_80) && oldval != PIN_VREF50)
5590 return AC_PINCTL_VREF_80;
5591 else if (pincap & AC_PINCAP_VREF_50)
5592 return AC_PINCTL_VREF_50;
5593 else if (pincap & AC_PINCAP_VREF_100)
5594 return AC_PINCTL_VREF_100;
5595 else if (pincap & AC_PINCAP_VREF_GRD)
5596 return AC_PINCTL_VREF_GRD;
5597 return AC_PINCTL_VREF_HIZ;
5599 EXPORT_SYMBOL_GPL(snd_hda_get_default_vref);
5601 /* correct the pin ctl value for matching with the pin cap */
5602 unsigned int snd_hda_correct_pin_ctl(struct hda_codec *codec,
5603 hda_nid_t pin, unsigned int val)
5605 static unsigned int cap_lists[][2] = {
5606 { AC_PINCTL_VREF_100, AC_PINCAP_VREF_100 },
5607 { AC_PINCTL_VREF_80, AC_PINCAP_VREF_80 },
5608 { AC_PINCTL_VREF_50, AC_PINCAP_VREF_50 },
5609 { AC_PINCTL_VREF_GRD, AC_PINCAP_VREF_GRD },
5615 cap = snd_hda_query_pin_caps(codec, pin);
5617 return val; /* don't know what to do... */
5619 if (val & AC_PINCTL_OUT_EN) {
5620 if (!(cap & AC_PINCAP_OUT))
5621 val &= ~(AC_PINCTL_OUT_EN | AC_PINCTL_HP_EN);
5622 else if ((val & AC_PINCTL_HP_EN) && !(cap & AC_PINCAP_HP_DRV))
5623 val &= ~AC_PINCTL_HP_EN;
5626 if (val & AC_PINCTL_IN_EN) {
5627 if (!(cap & AC_PINCAP_IN))
5628 val &= ~(AC_PINCTL_IN_EN | AC_PINCTL_VREFEN);
5630 unsigned int vcap, vref;
5632 vcap = (cap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
5633 vref = val & AC_PINCTL_VREFEN;
5634 for (i = 0; i < ARRAY_SIZE(cap_lists); i++) {
5635 if (vref == cap_lists[i][0] &&
5636 !(vcap & cap_lists[i][1])) {
5637 if (i == ARRAY_SIZE(cap_lists) - 1)
5638 vref = AC_PINCTL_VREF_HIZ;
5640 vref = cap_lists[i + 1][0];
5643 val &= ~AC_PINCTL_VREFEN;
5650 EXPORT_SYMBOL_GPL(snd_hda_correct_pin_ctl);
5652 int _snd_hda_set_pin_ctl(struct hda_codec *codec, hda_nid_t pin,
5653 unsigned int val, bool cached)
5655 val = snd_hda_correct_pin_ctl(codec, pin, val);
5656 snd_hda_codec_set_pin_target(codec, pin, val);
5658 return snd_hda_codec_update_cache(codec, pin, 0,
5659 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
5661 return snd_hda_codec_write(codec, pin, 0,
5662 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
5664 EXPORT_SYMBOL_GPL(_snd_hda_set_pin_ctl);
5667 * snd_hda_add_imux_item - Add an item to input_mux
5669 * When the same label is used already in the existing items, the number
5670 * suffix is appended to the label. This label index number is stored
5671 * to type_idx when non-NULL pointer is given.
5673 int snd_hda_add_imux_item(struct hda_input_mux *imux, const char *label,
5674 int index, int *type_idx)
5676 int i, label_idx = 0;
5677 if (imux->num_items >= HDA_MAX_NUM_INPUTS) {
5678 snd_printd(KERN_ERR "hda_codec: Too many imux items!\n");
5681 for (i = 0; i < imux->num_items; i++) {
5682 if (!strncmp(label, imux->items[i].label, strlen(label)))
5686 *type_idx = label_idx;
5688 snprintf(imux->items[imux->num_items].label,
5689 sizeof(imux->items[imux->num_items].label),
5690 "%s %d", label, label_idx);
5692 strlcpy(imux->items[imux->num_items].label, label,
5693 sizeof(imux->items[imux->num_items].label));
5694 imux->items[imux->num_items].index = index;
5698 EXPORT_SYMBOL_GPL(snd_hda_add_imux_item);
5707 static void hda_async_suspend(void *data, async_cookie_t cookie)
5709 hda_call_codec_suspend(data, false);
5712 static void hda_async_resume(void *data, async_cookie_t cookie)
5714 hda_call_codec_resume(data);
5718 * snd_hda_suspend - suspend the codecs
5721 * Returns 0 if successful.
5723 int snd_hda_suspend(struct hda_bus *bus)
5725 struct hda_codec *codec;
5726 ASYNC_DOMAIN_EXCLUSIVE(domain);
5728 list_for_each_entry(codec, &bus->codec_list, list) {
5729 cancel_delayed_work_sync(&codec->jackpoll_work);
5730 if (hda_codec_is_power_on(codec)) {
5731 if (bus->num_codecs > 1)
5732 async_schedule_domain(hda_async_suspend, codec,
5735 hda_call_codec_suspend(codec, false);
5739 if (bus->num_codecs > 1)
5740 async_synchronize_full_domain(&domain);
5744 EXPORT_SYMBOL_GPL(snd_hda_suspend);
5747 * snd_hda_resume - resume the codecs
5750 * Returns 0 if successful.
5752 int snd_hda_resume(struct hda_bus *bus)
5754 struct hda_codec *codec;
5755 ASYNC_DOMAIN_EXCLUSIVE(domain);
5757 list_for_each_entry(codec, &bus->codec_list, list) {
5758 if (bus->num_codecs > 1)
5759 async_schedule_domain(hda_async_resume, codec, &domain);
5761 hda_call_codec_resume(codec);
5764 if (bus->num_codecs > 1)
5765 async_synchronize_full_domain(&domain);
5769 EXPORT_SYMBOL_GPL(snd_hda_resume);
5770 #endif /* CONFIG_PM */
5777 * snd_array_new - get a new element from the given array
5778 * @array: the array object
5780 * Get a new element from the given array. If it exceeds the
5781 * pre-allocated array size, re-allocate the array.
5783 * Returns NULL if allocation failed.
5785 void *snd_array_new(struct snd_array *array)
5787 if (snd_BUG_ON(!array->elem_size))
5789 if (array->used >= array->alloced) {
5790 int num = array->alloced + array->alloc_align;
5791 int size = (num + 1) * array->elem_size;
5793 if (snd_BUG_ON(num >= 4096))
5795 nlist = krealloc(array->list, size, GFP_KERNEL | __GFP_ZERO);
5798 array->list = nlist;
5799 array->alloced = num;
5801 return snd_array_elem(array, array->used++);
5803 EXPORT_SYMBOL_GPL(snd_array_new);
5806 * snd_array_free - free the given array elements
5807 * @array: the array object
5809 void snd_array_free(struct snd_array *array)
5816 EXPORT_SYMBOL_GPL(snd_array_free);
5819 * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer
5820 * @pcm: PCM caps bits
5821 * @buf: the string buffer to write
5822 * @buflen: the max buffer length
5824 * used by hda_proc.c and hda_eld.c
5826 void snd_print_pcm_bits(int pcm, char *buf, int buflen)
5828 static unsigned int bits[] = { 8, 16, 20, 24, 32 };
5831 for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
5832 if (pcm & (AC_SUPPCM_BITS_8 << i))
5833 j += snprintf(buf + j, buflen - j, " %d", bits[i]);
5835 buf[j] = '\0'; /* necessary when j == 0 */
5837 EXPORT_SYMBOL_GPL(snd_print_pcm_bits);
5839 MODULE_DESCRIPTION("HDA codec core");
5840 MODULE_LICENSE("GPL");