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);
81 * snd_hda_add_codec_preset - Add a codec preset to the chain
82 * @preset: codec preset table to add
84 int snd_hda_add_codec_preset(struct hda_codec_preset_list *preset)
86 mutex_lock(&preset_mutex);
87 list_add_tail(&preset->list, &hda_preset_tables);
88 mutex_unlock(&preset_mutex);
91 EXPORT_SYMBOL_GPL(snd_hda_add_codec_preset);
94 * snd_hda_delete_codec_preset - Delete a codec preset from the chain
95 * @preset: codec preset table to delete
97 int snd_hda_delete_codec_preset(struct hda_codec_preset_list *preset)
99 mutex_lock(&preset_mutex);
100 list_del(&preset->list);
101 mutex_unlock(&preset_mutex);
104 EXPORT_SYMBOL_GPL(snd_hda_delete_codec_preset);
107 #define codec_in_pm(codec) ((codec)->in_pm)
108 static void hda_power_work(struct work_struct *work);
109 static void hda_keep_power_on(struct hda_codec *codec);
110 #define hda_codec_is_power_on(codec) ((codec)->power_on)
112 static void hda_call_pm_notify(struct hda_codec *codec, bool power_up)
114 struct hda_bus *bus = codec->bus;
116 if ((power_up && codec->pm_up_notified) ||
117 (!power_up && !codec->pm_up_notified))
119 if (bus->ops.pm_notify)
120 bus->ops.pm_notify(bus, power_up);
121 codec->pm_up_notified = power_up;
125 #define codec_in_pm(codec) 0
126 static inline void hda_keep_power_on(struct hda_codec *codec) {}
127 #define hda_codec_is_power_on(codec) 1
128 #define hda_call_pm_notify(codec, state) {}
132 * snd_hda_get_jack_location - Give a location string of the jack
133 * @cfg: pin default config value
135 * Parse the pin default config value and returns the string of the
136 * jack location, e.g. "Rear", "Front", etc.
138 const char *snd_hda_get_jack_location(u32 cfg)
140 static char *bases[7] = {
141 "N/A", "Rear", "Front", "Left", "Right", "Top", "Bottom",
143 static unsigned char specials_idx[] = {
148 static char *specials[] = {
149 "Rear Panel", "Drive Bar",
150 "Riser", "HDMI", "ATAPI",
151 "Mobile-In", "Mobile-Out"
154 cfg = (cfg & AC_DEFCFG_LOCATION) >> AC_DEFCFG_LOCATION_SHIFT;
155 if ((cfg & 0x0f) < 7)
156 return bases[cfg & 0x0f];
157 for (i = 0; i < ARRAY_SIZE(specials_idx); i++) {
158 if (cfg == specials_idx[i])
163 EXPORT_SYMBOL_GPL(snd_hda_get_jack_location);
166 * snd_hda_get_jack_connectivity - Give a connectivity string of the jack
167 * @cfg: pin default config value
169 * Parse the pin default config value and returns the string of the
170 * jack connectivity, i.e. external or internal connection.
172 const char *snd_hda_get_jack_connectivity(u32 cfg)
174 static char *jack_locations[4] = { "Ext", "Int", "Sep", "Oth" };
176 return jack_locations[(cfg >> (AC_DEFCFG_LOCATION_SHIFT + 4)) & 3];
178 EXPORT_SYMBOL_GPL(snd_hda_get_jack_connectivity);
181 * snd_hda_get_jack_type - Give a type string of the jack
182 * @cfg: pin default config value
184 * Parse the pin default config value and returns the string of the
185 * jack type, i.e. the purpose of the jack, such as Line-Out or CD.
187 const char *snd_hda_get_jack_type(u32 cfg)
189 static char *jack_types[16] = {
190 "Line Out", "Speaker", "HP Out", "CD",
191 "SPDIF Out", "Digital Out", "Modem Line", "Modem Hand",
192 "Line In", "Aux", "Mic", "Telephony",
193 "SPDIF In", "Digital In", "Reserved", "Other"
196 return jack_types[(cfg & AC_DEFCFG_DEVICE)
197 >> AC_DEFCFG_DEVICE_SHIFT];
199 EXPORT_SYMBOL_GPL(snd_hda_get_jack_type);
202 * Compose a 32bit command word to be sent to the HD-audio controller
204 static inline unsigned int
205 make_codec_cmd(struct hda_codec *codec, hda_nid_t nid, int flags,
206 unsigned int verb, unsigned int parm)
210 if ((codec->addr & ~0xf) || (nid & ~0x7f) ||
211 (verb & ~0xfff) || (parm & ~0xffff)) {
212 codec_err(codec, "hda-codec: out of range cmd %x:%x:%x:%x\n",
213 codec->addr, nid, verb, parm);
217 val = (u32)codec->addr << 28;
218 val |= (u32)nid << 20;
225 * Send and receive a verb
227 static int codec_exec_verb(struct hda_codec *codec, unsigned int cmd,
228 int flags, unsigned int *res)
230 struct hda_bus *bus = codec->bus;
239 snd_hda_power_up(codec);
240 mutex_lock(&bus->cmd_mutex);
241 if (flags & HDA_RW_NO_RESPONSE_FALLBACK)
242 bus->no_response_fallback = 1;
244 trace_hda_send_cmd(codec, cmd);
245 err = bus->ops.command(bus, cmd);
248 /* process pending verbs */
249 bus->ops.get_response(bus, codec->addr);
252 *res = bus->ops.get_response(bus, codec->addr);
253 trace_hda_get_response(codec, *res);
255 bus->no_response_fallback = 0;
256 mutex_unlock(&bus->cmd_mutex);
257 snd_hda_power_down(codec);
258 if (!codec_in_pm(codec) && res && *res == -1 && bus->rirb_error) {
259 if (bus->response_reset) {
261 "resetting BUS due to fatal communication error\n");
262 trace_hda_bus_reset(bus);
263 bus->ops.bus_reset(bus);
267 /* clear reset-flag when the communication gets recovered */
268 if (!err || codec_in_pm(codec))
269 bus->response_reset = 0;
274 * snd_hda_codec_read - send a command and get the response
275 * @codec: the HDA codec
276 * @nid: NID to send the command
277 * @flags: optional bit flags
278 * @verb: the verb to send
279 * @parm: the parameter for the verb
281 * Send a single command and read the corresponding response.
283 * Returns the obtained response value, or -1 for an error.
285 unsigned int snd_hda_codec_read(struct hda_codec *codec, hda_nid_t nid,
287 unsigned int verb, unsigned int parm)
289 unsigned cmd = make_codec_cmd(codec, nid, flags, verb, parm);
291 if (codec_exec_verb(codec, cmd, flags, &res))
295 EXPORT_SYMBOL_GPL(snd_hda_codec_read);
298 * snd_hda_codec_write - send a single command without waiting for response
299 * @codec: the HDA codec
300 * @nid: NID to send the command
301 * @flags: optional bit flags
302 * @verb: the verb to send
303 * @parm: the parameter for the verb
305 * Send a single command without waiting for response.
307 * Returns 0 if successful, or a negative error code.
309 int snd_hda_codec_write(struct hda_codec *codec, hda_nid_t nid, int flags,
310 unsigned int verb, unsigned int parm)
312 unsigned int cmd = make_codec_cmd(codec, nid, flags, verb, parm);
314 return codec_exec_verb(codec, cmd, flags,
315 codec->bus->sync_write ? &res : NULL);
317 EXPORT_SYMBOL_GPL(snd_hda_codec_write);
320 * snd_hda_sequence_write - sequence writes
321 * @codec: the HDA codec
322 * @seq: VERB array to send
324 * Send the commands sequentially from the given array.
325 * The array must be terminated with NID=0.
327 void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq)
329 for (; seq->nid; seq++)
330 snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param);
332 EXPORT_SYMBOL_GPL(snd_hda_sequence_write);
335 * snd_hda_get_sub_nodes - get the range of sub nodes
336 * @codec: the HDA codec
338 * @start_id: the pointer to store the start NID
340 * Parse the NID and store the start NID of its sub-nodes.
341 * Returns the number of sub-nodes.
343 int snd_hda_get_sub_nodes(struct hda_codec *codec, hda_nid_t nid,
348 parm = snd_hda_param_read(codec, nid, AC_PAR_NODE_COUNT);
353 *start_id = (parm >> 16) & 0x7fff;
354 return (int)(parm & 0x7fff);
356 EXPORT_SYMBOL_GPL(snd_hda_get_sub_nodes);
358 /* connection list element */
359 struct hda_conn_list {
360 struct list_head list;
366 /* look up the cached results */
367 static struct hda_conn_list *
368 lookup_conn_list(struct hda_codec *codec, hda_nid_t nid)
370 struct hda_conn_list *p;
371 list_for_each_entry(p, &codec->conn_list, list) {
378 static int add_conn_list(struct hda_codec *codec, hda_nid_t nid, int len,
379 const hda_nid_t *list)
381 struct hda_conn_list *p;
383 p = kmalloc(sizeof(*p) + len * sizeof(hda_nid_t), GFP_KERNEL);
388 memcpy(p->conns, list, len * sizeof(hda_nid_t));
389 list_add(&p->list, &codec->conn_list);
393 static void remove_conn_list(struct hda_codec *codec)
395 while (!list_empty(&codec->conn_list)) {
396 struct hda_conn_list *p;
397 p = list_first_entry(&codec->conn_list, typeof(*p), list);
403 /* read the connection and add to the cache */
404 static int read_and_add_raw_conns(struct hda_codec *codec, hda_nid_t nid)
407 hda_nid_t *result = list;
410 len = snd_hda_get_raw_connections(codec, nid, list, ARRAY_SIZE(list));
411 if (len == -ENOSPC) {
412 len = snd_hda_get_num_raw_conns(codec, nid);
413 result = kmalloc(sizeof(hda_nid_t) * len, GFP_KERNEL);
416 len = snd_hda_get_raw_connections(codec, nid, result, len);
419 len = snd_hda_override_conn_list(codec, nid, len, result);
426 * snd_hda_get_conn_list - get connection list
427 * @codec: the HDA codec
429 * @listp: the pointer to store NID list
431 * Parses the connection list of the given widget and stores the pointer
432 * to the list of NIDs.
434 * Returns the number of connections, or a negative error code.
436 * Note that the returned pointer isn't protected against the list
437 * modification. If snd_hda_override_conn_list() might be called
438 * concurrently, protect with a mutex appropriately.
440 int snd_hda_get_conn_list(struct hda_codec *codec, hda_nid_t nid,
441 const hda_nid_t **listp)
447 const struct hda_conn_list *p;
449 /* if the connection-list is already cached, read it */
450 p = lookup_conn_list(codec, nid);
456 if (snd_BUG_ON(added))
459 err = read_and_add_raw_conns(codec, nid);
465 EXPORT_SYMBOL_GPL(snd_hda_get_conn_list);
468 * snd_hda_get_connections - copy connection list
469 * @codec: the HDA codec
471 * @conn_list: connection list array; when NULL, checks only the size
472 * @max_conns: max. number of connections to store
474 * Parses the connection list of the given widget and stores the list
477 * Returns the number of connections, or a negative error code.
479 int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
480 hda_nid_t *conn_list, int max_conns)
482 const hda_nid_t *list;
483 int len = snd_hda_get_conn_list(codec, nid, &list);
485 if (len > 0 && conn_list) {
486 if (len > max_conns) {
487 codec_err(codec, "Too many connections %d for NID 0x%x\n",
491 memcpy(conn_list, list, len * sizeof(hda_nid_t));
496 EXPORT_SYMBOL_GPL(snd_hda_get_connections);
498 /* return CONNLIST_LEN parameter of the given widget */
499 static unsigned int get_num_conns(struct hda_codec *codec, hda_nid_t nid)
501 unsigned int wcaps = get_wcaps(codec, nid);
504 if (!(wcaps & AC_WCAP_CONN_LIST) &&
505 get_wcaps_type(wcaps) != AC_WID_VOL_KNB)
508 parm = snd_hda_param_read(codec, nid, AC_PAR_CONNLIST_LEN);
514 int snd_hda_get_num_raw_conns(struct hda_codec *codec, hda_nid_t nid)
516 return snd_hda_get_raw_connections(codec, nid, NULL, 0);
520 * snd_hda_get_raw_connections - copy connection list without cache
521 * @codec: the HDA codec
523 * @conn_list: connection list array
524 * @max_conns: max. number of connections to store
526 * Like snd_hda_get_connections(), copy the connection list but without
527 * checking through the connection-list cache.
528 * Currently called only from hda_proc.c, so not exported.
530 int snd_hda_get_raw_connections(struct hda_codec *codec, hda_nid_t nid,
531 hda_nid_t *conn_list, int max_conns)
534 int i, conn_len, conns;
535 unsigned int shift, num_elems, mask;
539 parm = get_num_conns(codec, nid);
543 if (parm & AC_CLIST_LONG) {
552 conn_len = parm & AC_CLIST_LENGTH;
553 mask = (1 << (shift-1)) - 1;
556 return 0; /* no connection */
559 /* single connection */
560 parm = snd_hda_codec_read(codec, nid, 0,
561 AC_VERB_GET_CONNECT_LIST, 0);
562 if (parm == -1 && codec->bus->rirb_error)
565 conn_list[0] = parm & mask;
569 /* multi connection */
572 for (i = 0; i < conn_len; i++) {
576 if (i % num_elems == 0) {
577 parm = snd_hda_codec_read(codec, nid, 0,
578 AC_VERB_GET_CONNECT_LIST, i);
579 if (parm == -1 && codec->bus->rirb_error)
582 range_val = !!(parm & (1 << (shift-1))); /* ranges */
584 if (val == 0 && null_count++) { /* no second chance */
586 "invalid CONNECT_LIST verb %x[%i]:%x\n",
592 /* ranges between the previous and this one */
593 if (!prev_nid || prev_nid >= val) {
595 "invalid dep_range_val %x:%x\n",
599 for (n = prev_nid + 1; n <= val; n++) {
601 if (conns >= max_conns)
603 conn_list[conns] = n;
609 if (conns >= max_conns)
611 conn_list[conns] = val;
621 * snd_hda_override_conn_list - add/modify the connection-list to cache
622 * @codec: the HDA codec
624 * @len: number of connection list entries
625 * @list: the list of connection entries
627 * Add or modify the given connection-list to the cache. If the corresponding
628 * cache already exists, invalidate it and append a new one.
630 * Returns zero or a negative error code.
632 int snd_hda_override_conn_list(struct hda_codec *codec, hda_nid_t nid, int len,
633 const hda_nid_t *list)
635 struct hda_conn_list *p;
637 p = lookup_conn_list(codec, nid);
643 return add_conn_list(codec, nid, len, list);
645 EXPORT_SYMBOL_GPL(snd_hda_override_conn_list);
648 * snd_hda_get_conn_index - get the connection index of the given NID
649 * @codec: the HDA codec
650 * @mux: NID containing the list
651 * @nid: NID to select
652 * @recursive: 1 when searching NID recursively, otherwise 0
654 * Parses the connection list of the widget @mux and checks whether the
655 * widget @nid is present. If it is, return the connection index.
656 * Otherwise it returns -1.
658 int snd_hda_get_conn_index(struct hda_codec *codec, hda_nid_t mux,
659 hda_nid_t nid, int recursive)
661 const hda_nid_t *conn;
664 nums = snd_hda_get_conn_list(codec, mux, &conn);
665 for (i = 0; i < nums; i++)
670 if (recursive > 10) {
671 codec_dbg(codec, "too deep connection for 0x%x\n", nid);
675 for (i = 0; i < nums; i++) {
676 unsigned int type = get_wcaps_type(get_wcaps(codec, conn[i]));
677 if (type == AC_WID_PIN || type == AC_WID_AUD_OUT)
679 if (snd_hda_get_conn_index(codec, conn[i], nid, recursive) >= 0)
684 EXPORT_SYMBOL_GPL(snd_hda_get_conn_index);
687 /* return DEVLIST_LEN parameter of the given widget */
688 static unsigned int get_num_devices(struct hda_codec *codec, hda_nid_t nid)
690 unsigned int wcaps = get_wcaps(codec, nid);
693 if (!codec->dp_mst || !(wcaps & AC_WCAP_DIGITAL) ||
694 get_wcaps_type(wcaps) != AC_WID_PIN)
697 parm = snd_hda_param_read(codec, nid, AC_PAR_DEVLIST_LEN);
698 if (parm == -1 && codec->bus->rirb_error)
700 return parm & AC_DEV_LIST_LEN_MASK;
704 * snd_hda_get_devices - copy device list without cache
705 * @codec: the HDA codec
706 * @nid: NID of the pin to parse
707 * @dev_list: device list array
708 * @max_devices: max. number of devices to store
710 * Copy the device list. This info is dynamic and so not cached.
711 * Currently called only from hda_proc.c, so not exported.
713 int snd_hda_get_devices(struct hda_codec *codec, hda_nid_t nid,
714 u8 *dev_list, int max_devices)
717 int i, dev_len, devices;
719 parm = get_num_devices(codec, nid);
720 if (!parm) /* not multi-stream capable */
724 dev_len = dev_len < max_devices ? dev_len : max_devices;
727 while (devices < dev_len) {
728 parm = snd_hda_codec_read(codec, nid, 0,
729 AC_VERB_GET_DEVICE_LIST, devices);
730 if (parm == -1 && codec->bus->rirb_error)
733 for (i = 0; i < 8; i++) {
734 dev_list[devices] = (u8)parm;
737 if (devices >= dev_len)
745 * snd_hda_queue_unsol_event - add an unsolicited event to queue
747 * @res: unsolicited event (lower 32bit of RIRB entry)
748 * @res_ex: codec addr and flags (upper 32bit or RIRB entry)
750 * Adds the given event to the queue. The events are processed in
751 * the workqueue asynchronously. Call this function in the interrupt
752 * hanlder when RIRB receives an unsolicited event.
754 * Returns 0 if successful, or a negative error code.
756 int snd_hda_queue_unsol_event(struct hda_bus *bus, u32 res, u32 res_ex)
758 struct hda_bus_unsolicited *unsol;
761 if (!bus || !bus->workq)
764 trace_hda_unsol_event(bus, res, res_ex);
769 wp = (unsol->wp + 1) % HDA_UNSOL_QUEUE_SIZE;
773 unsol->queue[wp] = res;
774 unsol->queue[wp + 1] = res_ex;
776 queue_work(bus->workq, &unsol->work);
780 EXPORT_SYMBOL_GPL(snd_hda_queue_unsol_event);
783 * process queued unsolicited events
785 static void process_unsol_events(struct work_struct *work)
787 struct hda_bus_unsolicited *unsol =
788 container_of(work, struct hda_bus_unsolicited, work);
789 struct hda_bus *bus = unsol->bus;
790 struct hda_codec *codec;
791 unsigned int rp, caddr, res;
793 while (unsol->rp != unsol->wp) {
794 rp = (unsol->rp + 1) % HDA_UNSOL_QUEUE_SIZE;
797 res = unsol->queue[rp];
798 caddr = unsol->queue[rp + 1];
799 if (!(caddr & (1 << 4))) /* no unsolicited event? */
801 codec = bus->caddr_tbl[caddr & 0x0f];
802 if (codec && codec->patch_ops.unsol_event)
803 codec->patch_ops.unsol_event(codec, res);
808 * initialize unsolicited queue
810 static int init_unsol_queue(struct hda_bus *bus)
812 struct hda_bus_unsolicited *unsol;
814 if (bus->unsol) /* already initialized */
817 unsol = kzalloc(sizeof(*unsol), GFP_KERNEL);
819 dev_err(bus->card->dev, "can't allocate unsolicited queue\n");
822 INIT_WORK(&unsol->work, process_unsol_events);
831 static void snd_hda_bus_free(struct hda_bus *bus)
836 WARN_ON(!list_empty(&bus->codec_list));
838 flush_workqueue(bus->workq);
840 if (bus->ops.private_free)
841 bus->ops.private_free(bus);
843 destroy_workqueue(bus->workq);
848 static int snd_hda_bus_dev_free(struct snd_device *device)
850 snd_hda_bus_free(device->device_data);
854 static int snd_hda_bus_dev_disconnect(struct snd_device *device)
856 struct hda_bus *bus = device->device_data;
862 * snd_hda_bus_new - create a HDA bus
863 * @card: the card entry
864 * @temp: the template for hda_bus information
865 * @busp: the pointer to store the created bus instance
867 * Returns 0 if successful, or a negative error code.
869 int snd_hda_bus_new(struct snd_card *card,
870 const struct hda_bus_template *temp,
871 struct hda_bus **busp)
875 static struct snd_device_ops dev_ops = {
876 .dev_disconnect = snd_hda_bus_dev_disconnect,
877 .dev_free = snd_hda_bus_dev_free,
880 if (snd_BUG_ON(!temp))
882 if (snd_BUG_ON(!temp->ops.command || !temp->ops.get_response))
888 bus = kzalloc(sizeof(*bus), GFP_KERNEL);
890 dev_err(card->dev, "can't allocate struct hda_bus\n");
895 bus->private_data = temp->private_data;
896 bus->pci = temp->pci;
897 bus->modelname = temp->modelname;
898 bus->power_save = temp->power_save;
899 bus->ops = temp->ops;
901 mutex_init(&bus->cmd_mutex);
902 mutex_init(&bus->prepare_mutex);
903 INIT_LIST_HEAD(&bus->codec_list);
905 snprintf(bus->workq_name, sizeof(bus->workq_name),
906 "hd-audio%d", card->number);
907 bus->workq = create_singlethread_workqueue(bus->workq_name);
909 dev_err(card->dev, "cannot create workqueue %s\n",
915 err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops);
917 snd_hda_bus_free(bus);
924 EXPORT_SYMBOL_GPL(snd_hda_bus_new);
926 #if IS_ENABLED(CONFIG_SND_HDA_GENERIC)
927 #define is_generic_config(codec) \
928 (codec->modelname && !strcmp(codec->modelname, "generic"))
930 #define is_generic_config(codec) 0
934 #define HDA_MODREQ_MAX_COUNT 2 /* two request_modules()'s */
936 #define HDA_MODREQ_MAX_COUNT 0 /* all presets are statically linked */
940 * find a matching codec preset
942 static const struct hda_codec_preset *
943 find_codec_preset(struct hda_codec *codec)
945 struct hda_codec_preset_list *tbl;
946 const struct hda_codec_preset *preset;
947 unsigned int mod_requested = 0;
950 mutex_lock(&preset_mutex);
951 list_for_each_entry(tbl, &hda_preset_tables, list) {
952 if (!try_module_get(tbl->owner)) {
953 codec_err(codec, "cannot module_get\n");
956 for (preset = tbl->preset; preset->id; preset++) {
957 u32 mask = preset->mask;
958 if (preset->afg && preset->afg != codec->afg)
960 if (preset->mfg && preset->mfg != codec->mfg)
964 if (preset->id == (codec->vendor_id & mask) &&
966 preset->rev == codec->revision_id)) {
967 mutex_unlock(&preset_mutex);
968 codec->owner = tbl->owner;
972 module_put(tbl->owner);
974 mutex_unlock(&preset_mutex);
976 if (mod_requested < HDA_MODREQ_MAX_COUNT) {
978 request_module("snd-hda-codec-id:%08x",
981 request_module("snd-hda-codec-id:%04x*",
982 (codec->vendor_id >> 16) & 0xffff);
990 * get_codec_name - store the codec name
992 static int get_codec_name(struct hda_codec *codec)
994 const struct hda_vendor_id *c;
995 const char *vendor = NULL;
996 u16 vendor_id = codec->vendor_id >> 16;
999 if (codec->vendor_name)
1002 for (c = hda_vendor_ids; c->id; c++) {
1003 if (c->id == vendor_id) {
1009 sprintf(tmp, "Generic %04x", vendor_id);
1012 codec->vendor_name = kstrdup(vendor, GFP_KERNEL);
1013 if (!codec->vendor_name)
1017 if (codec->chip_name)
1020 if (codec->preset && codec->preset->name)
1021 codec->chip_name = kstrdup(codec->preset->name, GFP_KERNEL);
1023 sprintf(tmp, "ID %x", codec->vendor_id & 0xffff);
1024 codec->chip_name = kstrdup(tmp, GFP_KERNEL);
1026 if (!codec->chip_name)
1032 * look for an AFG and MFG nodes
1034 static void setup_fg_nodes(struct hda_codec *codec)
1036 int i, total_nodes, function_id;
1039 total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid);
1040 for (i = 0; i < total_nodes; i++, nid++) {
1041 function_id = snd_hda_param_read(codec, nid,
1042 AC_PAR_FUNCTION_TYPE);
1043 switch (function_id & 0xff) {
1044 case AC_GRP_AUDIO_FUNCTION:
1046 codec->afg_function_id = function_id & 0xff;
1047 codec->afg_unsol = (function_id >> 8) & 1;
1049 case AC_GRP_MODEM_FUNCTION:
1051 codec->mfg_function_id = function_id & 0xff;
1052 codec->mfg_unsol = (function_id >> 8) & 1;
1061 * read widget caps for each widget and store in cache
1063 static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
1068 codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node,
1070 codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL);
1073 nid = codec->start_nid;
1074 for (i = 0; i < codec->num_nodes; i++, nid++)
1075 codec->wcaps[i] = snd_hda_param_read(codec, nid,
1076 AC_PAR_AUDIO_WIDGET_CAP);
1080 /* read all pin default configurations and save codec->init_pins */
1081 static int read_pin_defaults(struct hda_codec *codec)
1084 hda_nid_t nid = codec->start_nid;
1086 for (i = 0; i < codec->num_nodes; i++, nid++) {
1087 struct hda_pincfg *pin;
1088 unsigned int wcaps = get_wcaps(codec, nid);
1089 unsigned int wid_type = get_wcaps_type(wcaps);
1090 if (wid_type != AC_WID_PIN)
1092 pin = snd_array_new(&codec->init_pins);
1096 pin->cfg = snd_hda_codec_read(codec, nid, 0,
1097 AC_VERB_GET_CONFIG_DEFAULT, 0);
1098 pin->ctrl = snd_hda_codec_read(codec, nid, 0,
1099 AC_VERB_GET_PIN_WIDGET_CONTROL,
1105 /* look up the given pin config list and return the item matching with NID */
1106 static struct hda_pincfg *look_up_pincfg(struct hda_codec *codec,
1107 struct snd_array *array,
1111 for (i = 0; i < array->used; i++) {
1112 struct hda_pincfg *pin = snd_array_elem(array, i);
1113 if (pin->nid == nid)
1119 /* set the current pin config value for the given NID.
1120 * the value is cached, and read via snd_hda_codec_get_pincfg()
1122 int snd_hda_add_pincfg(struct hda_codec *codec, struct snd_array *list,
1123 hda_nid_t nid, unsigned int cfg)
1125 struct hda_pincfg *pin;
1127 /* the check below may be invalid when pins are added by a fixup
1128 * dynamically (e.g. via snd_hda_codec_update_widgets()), so disabled
1132 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN)
1136 pin = look_up_pincfg(codec, list, nid);
1138 pin = snd_array_new(list);
1148 * snd_hda_codec_set_pincfg - Override a pin default configuration
1149 * @codec: the HDA codec
1150 * @nid: NID to set the pin config
1151 * @cfg: the pin default config value
1153 * Override a pin default configuration value in the cache.
1154 * This value can be read by snd_hda_codec_get_pincfg() in a higher
1155 * priority than the real hardware value.
1157 int snd_hda_codec_set_pincfg(struct hda_codec *codec,
1158 hda_nid_t nid, unsigned int cfg)
1160 return snd_hda_add_pincfg(codec, &codec->driver_pins, nid, cfg);
1162 EXPORT_SYMBOL_GPL(snd_hda_codec_set_pincfg);
1165 * snd_hda_codec_get_pincfg - Obtain a pin-default configuration
1166 * @codec: the HDA codec
1167 * @nid: NID to get the pin config
1169 * Get the current pin config value of the given pin NID.
1170 * If the pincfg value is cached or overridden via sysfs or driver,
1171 * returns the cached value.
1173 unsigned int snd_hda_codec_get_pincfg(struct hda_codec *codec, hda_nid_t nid)
1175 struct hda_pincfg *pin;
1177 #ifdef CONFIG_SND_HDA_RECONFIG
1179 unsigned int cfg = 0;
1180 mutex_lock(&codec->user_mutex);
1181 pin = look_up_pincfg(codec, &codec->user_pins, nid);
1184 mutex_unlock(&codec->user_mutex);
1189 pin = look_up_pincfg(codec, &codec->driver_pins, nid);
1192 pin = look_up_pincfg(codec, &codec->init_pins, nid);
1197 EXPORT_SYMBOL_GPL(snd_hda_codec_get_pincfg);
1200 * snd_hda_codec_set_pin_target - remember the current pinctl target value
1201 * @codec: the HDA codec
1203 * @val: assigned pinctl value
1205 * This function stores the given value to a pinctl target value in the
1206 * pincfg table. This isn't always as same as the actually written value
1207 * but can be referred at any time via snd_hda_codec_get_pin_target().
1209 int snd_hda_codec_set_pin_target(struct hda_codec *codec, hda_nid_t nid,
1212 struct hda_pincfg *pin;
1214 pin = look_up_pincfg(codec, &codec->init_pins, nid);
1220 EXPORT_SYMBOL_GPL(snd_hda_codec_set_pin_target);
1223 * snd_hda_codec_get_pin_target - return the current pinctl target value
1224 * @codec: the HDA codec
1227 int snd_hda_codec_get_pin_target(struct hda_codec *codec, hda_nid_t nid)
1229 struct hda_pincfg *pin;
1231 pin = look_up_pincfg(codec, &codec->init_pins, nid);
1236 EXPORT_SYMBOL_GPL(snd_hda_codec_get_pin_target);
1239 * snd_hda_shutup_pins - Shut up all pins
1240 * @codec: the HDA codec
1242 * Clear all pin controls to shup up before suspend for avoiding click noise.
1243 * The controls aren't cached so that they can be resumed properly.
1245 void snd_hda_shutup_pins(struct hda_codec *codec)
1248 /* don't shut up pins when unloading the driver; otherwise it breaks
1249 * the default pin setup at the next load of the driver
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 /* use read here for syncing after issuing each verb */
1256 snd_hda_codec_read(codec, pin->nid, 0,
1257 AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
1259 codec->pins_shutup = 1;
1261 EXPORT_SYMBOL_GPL(snd_hda_shutup_pins);
1264 /* Restore the pin controls cleared previously via snd_hda_shutup_pins() */
1265 static void restore_shutup_pins(struct hda_codec *codec)
1268 if (!codec->pins_shutup)
1270 if (codec->bus->shutdown)
1272 for (i = 0; i < codec->init_pins.used; i++) {
1273 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1274 snd_hda_codec_write(codec, pin->nid, 0,
1275 AC_VERB_SET_PIN_WIDGET_CONTROL,
1278 codec->pins_shutup = 0;
1282 static void hda_jackpoll_work(struct work_struct *work)
1284 struct hda_codec *codec =
1285 container_of(work, struct hda_codec, jackpoll_work.work);
1287 snd_hda_jack_set_dirty_all(codec);
1288 snd_hda_jack_poll_all(codec);
1290 if (!codec->jackpoll_interval)
1293 queue_delayed_work(codec->bus->workq, &codec->jackpoll_work,
1294 codec->jackpoll_interval);
1297 static void init_hda_cache(struct hda_cache_rec *cache,
1298 unsigned int record_size);
1299 static void free_hda_cache(struct hda_cache_rec *cache);
1301 /* release all pincfg lists */
1302 static void free_init_pincfgs(struct hda_codec *codec)
1304 snd_array_free(&codec->driver_pins);
1305 #ifdef CONFIG_SND_HDA_RECONFIG
1306 snd_array_free(&codec->user_pins);
1308 snd_array_free(&codec->init_pins);
1312 * audio-converter setup caches
1314 struct hda_cvt_setup {
1319 unsigned char active; /* cvt is currently used */
1320 unsigned char dirty; /* setups should be cleared */
1323 /* get or create a cache entry for the given audio converter NID */
1324 static struct hda_cvt_setup *
1325 get_hda_cvt_setup(struct hda_codec *codec, hda_nid_t nid)
1327 struct hda_cvt_setup *p;
1330 for (i = 0; i < codec->cvt_setups.used; i++) {
1331 p = snd_array_elem(&codec->cvt_setups, i);
1335 p = snd_array_new(&codec->cvt_setups);
1342 * Dynamic symbol binding for the codec parsers
1345 #define load_parser(codec, sym) \
1346 ((codec)->parser = (int (*)(struct hda_codec *))symbol_request(sym))
1348 static void unload_parser(struct hda_codec *codec)
1351 symbol_put_addr(codec->parser);
1352 codec->parser = NULL;
1358 static void snd_hda_codec_free(struct hda_codec *codec)
1362 cancel_delayed_work_sync(&codec->jackpoll_work);
1363 snd_hda_jack_tbl_clear(codec);
1364 free_init_pincfgs(codec);
1366 cancel_delayed_work(&codec->power_work);
1367 flush_workqueue(codec->bus->workq);
1369 list_del(&codec->list);
1370 snd_array_free(&codec->mixers);
1371 snd_array_free(&codec->nids);
1372 snd_array_free(&codec->cvt_setups);
1373 snd_array_free(&codec->spdif_out);
1374 remove_conn_list(codec);
1375 codec->bus->caddr_tbl[codec->addr] = NULL;
1376 if (codec->patch_ops.free)
1377 codec->patch_ops.free(codec);
1378 hda_call_pm_notify(codec, false); /* cancel leftover refcounts */
1379 snd_hda_sysfs_clear(codec);
1380 unload_parser(codec);
1381 module_put(codec->owner);
1382 free_hda_cache(&codec->amp_cache);
1383 free_hda_cache(&codec->cmd_cache);
1384 kfree(codec->vendor_name);
1385 kfree(codec->chip_name);
1386 kfree(codec->modelname);
1387 kfree(codec->wcaps);
1388 codec->bus->num_codecs--;
1389 put_device(&codec->dev);
1392 static bool snd_hda_codec_get_supported_ps(struct hda_codec *codec,
1393 hda_nid_t fg, unsigned int power_state);
1395 static unsigned int hda_set_power_state(struct hda_codec *codec,
1396 unsigned int power_state);
1398 static int snd_hda_codec_dev_register(struct snd_device *device)
1400 struct hda_codec *codec = device->device_data;
1401 int err = device_add(&codec->dev);
1405 snd_hda_register_beep_device(codec);
1409 static int snd_hda_codec_dev_disconnect(struct snd_device *device)
1411 struct hda_codec *codec = device->device_data;
1413 snd_hda_detach_beep_device(codec);
1414 device_del(&codec->dev);
1418 static int snd_hda_codec_dev_free(struct snd_device *device)
1420 snd_hda_codec_free(device->device_data);
1424 /* just free the container */
1425 static void snd_hda_codec_dev_release(struct device *dev)
1427 kfree(container_of(dev, struct hda_codec, dev));
1431 * snd_hda_codec_new - create a HDA codec
1432 * @bus: the bus to assign
1433 * @codec_addr: the codec address
1434 * @codecp: the pointer to store the generated codec
1436 * Returns 0 if successful, or a negative error code.
1438 int snd_hda_codec_new(struct hda_bus *bus,
1439 unsigned int codec_addr,
1440 struct hda_codec **codecp)
1442 struct hda_codec *codec;
1446 static struct snd_device_ops dev_ops = {
1447 .dev_register = snd_hda_codec_dev_register,
1448 .dev_disconnect = snd_hda_codec_dev_disconnect,
1449 .dev_free = snd_hda_codec_dev_free,
1452 if (snd_BUG_ON(!bus))
1454 if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS))
1457 if (bus->caddr_tbl[codec_addr]) {
1458 dev_err(bus->card->dev,
1459 "address 0x%x is already occupied\n",
1464 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
1465 if (codec == NULL) {
1466 dev_err(bus->card->dev, "can't allocate struct hda_codec\n");
1470 device_initialize(&codec->dev);
1471 codec->dev.parent = &bus->card->card_dev;
1472 codec->dev.class = sound_class;
1473 codec->dev.release = snd_hda_codec_dev_release;
1474 codec->dev.groups = snd_hda_dev_attr_groups;
1475 dev_set_name(&codec->dev, "hdaudioC%dD%d", bus->card->number,
1477 dev_set_drvdata(&codec->dev, codec); /* for sysfs */
1480 codec->addr = codec_addr;
1481 mutex_init(&codec->spdif_mutex);
1482 mutex_init(&codec->control_mutex);
1483 mutex_init(&codec->hash_mutex);
1484 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
1485 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
1486 snd_array_init(&codec->mixers, sizeof(struct hda_nid_item), 32);
1487 snd_array_init(&codec->nids, sizeof(struct hda_nid_item), 32);
1488 snd_array_init(&codec->init_pins, sizeof(struct hda_pincfg), 16);
1489 snd_array_init(&codec->driver_pins, sizeof(struct hda_pincfg), 16);
1490 snd_array_init(&codec->cvt_setups, sizeof(struct hda_cvt_setup), 8);
1491 snd_array_init(&codec->spdif_out, sizeof(struct hda_spdif_out), 16);
1492 snd_array_init(&codec->jacktbl, sizeof(struct hda_jack_tbl), 16);
1493 snd_array_init(&codec->verbs, sizeof(struct hda_verb *), 8);
1494 INIT_LIST_HEAD(&codec->conn_list);
1496 INIT_DELAYED_WORK(&codec->jackpoll_work, hda_jackpoll_work);
1497 codec->depop_delay = -1;
1498 codec->fixup_id = HDA_FIXUP_ID_NOT_SET;
1501 spin_lock_init(&codec->power_lock);
1502 INIT_DELAYED_WORK(&codec->power_work, hda_power_work);
1503 /* snd_hda_codec_new() marks the codec as power-up, and leave it as is.
1504 * the caller has to power down appropriatley after initialization
1507 hda_keep_power_on(codec);
1510 snd_hda_sysfs_init(codec);
1512 if (codec->bus->modelname) {
1513 codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL);
1514 if (!codec->modelname) {
1520 list_add_tail(&codec->list, &bus->codec_list);
1523 bus->caddr_tbl[codec_addr] = codec;
1525 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1527 if (codec->vendor_id == -1)
1528 /* read again, hopefully the access method was corrected
1529 * in the last read...
1531 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1533 codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1534 AC_PAR_SUBSYSTEM_ID);
1535 codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1538 setup_fg_nodes(codec);
1539 if (!codec->afg && !codec->mfg) {
1540 dev_err(bus->card->dev, "no AFG or MFG node found\n");
1545 fg = codec->afg ? codec->afg : codec->mfg;
1546 err = read_widget_caps(codec, fg);
1548 dev_err(bus->card->dev, "cannot malloc\n");
1551 err = read_pin_defaults(codec);
1555 if (!codec->subsystem_id) {
1556 codec->subsystem_id =
1557 snd_hda_codec_read(codec, fg, 0,
1558 AC_VERB_GET_SUBSYSTEM_ID, 0);
1562 codec->d3_stop_clk = snd_hda_codec_get_supported_ps(codec, fg,
1565 codec->epss = snd_hda_codec_get_supported_ps(codec, fg,
1568 if (!codec->d3_stop_clk || !codec->epss)
1569 bus->power_keep_link_on = 1;
1573 /* power-up all before initialization */
1574 hda_set_power_state(codec, AC_PWRST_D0);
1576 snd_hda_codec_proc_new(codec);
1578 snd_hda_create_hwdep(codec);
1580 sprintf(component, "HDA:%08x,%08x,%08x", codec->vendor_id,
1581 codec->subsystem_id, codec->revision_id);
1582 snd_component_add(codec->bus->card, component);
1584 err = snd_device_new(bus->card, SNDRV_DEV_CODEC, codec, &dev_ops);
1593 snd_hda_codec_free(codec);
1596 EXPORT_SYMBOL_GPL(snd_hda_codec_new);
1599 * snd_hda_codec_update_widgets - Refresh widget caps and pin defaults
1600 * @codec: the HDA codec
1602 * Forcibly refresh the all widget caps and the init pin configurations of
1605 int snd_hda_codec_update_widgets(struct hda_codec *codec)
1610 /* Assume the function group node does not change,
1611 * only the widget nodes may change.
1613 kfree(codec->wcaps);
1614 fg = codec->afg ? codec->afg : codec->mfg;
1615 err = read_widget_caps(codec, fg);
1617 codec_err(codec, "cannot malloc\n");
1621 snd_array_free(&codec->init_pins);
1622 err = read_pin_defaults(codec);
1626 EXPORT_SYMBOL_GPL(snd_hda_codec_update_widgets);
1629 #if IS_ENABLED(CONFIG_SND_HDA_CODEC_HDMI)
1630 /* if all audio out widgets are digital, let's assume the codec as a HDMI/DP */
1631 static bool is_likely_hdmi_codec(struct hda_codec *codec)
1633 hda_nid_t nid = codec->start_nid;
1636 for (i = 0; i < codec->num_nodes; i++, nid++) {
1637 unsigned int wcaps = get_wcaps(codec, nid);
1638 switch (get_wcaps_type(wcaps)) {
1640 return false; /* HDMI parser supports only HDMI out */
1641 case AC_WID_AUD_OUT:
1642 if (!(wcaps & AC_WCAP_DIGITAL))
1650 /* no HDMI codec parser support */
1651 #define is_likely_hdmi_codec(codec) false
1652 #endif /* CONFIG_SND_HDA_CODEC_HDMI */
1655 * snd_hda_codec_configure - (Re-)configure the HD-audio codec
1656 * @codec: the HDA codec
1658 * Start parsing of the given codec tree and (re-)initialize the whole
1661 * Returns 0 if successful or a negative error code.
1663 int snd_hda_codec_configure(struct hda_codec *codec)
1665 int (*patch)(struct hda_codec *) = NULL;
1668 codec->preset = find_codec_preset(codec);
1669 if (!codec->vendor_name || !codec->chip_name) {
1670 err = get_codec_name(codec);
1675 if (!is_generic_config(codec) && codec->preset)
1676 patch = codec->preset->patch;
1678 unload_parser(codec); /* to be sure */
1679 if (is_likely_hdmi_codec(codec)) {
1680 #if IS_MODULE(CONFIG_SND_HDA_CODEC_HDMI)
1681 patch = load_parser(codec, snd_hda_parse_hdmi_codec);
1682 #elif IS_BUILTIN(CONFIG_SND_HDA_CODEC_HDMI)
1683 patch = snd_hda_parse_hdmi_codec;
1687 #if IS_MODULE(CONFIG_SND_HDA_GENERIC)
1688 patch = load_parser(codec, snd_hda_parse_generic_codec);
1689 #elif IS_BUILTIN(CONFIG_SND_HDA_GENERIC)
1690 patch = snd_hda_parse_generic_codec;
1694 codec_err(codec, "No codec parser is available\n");
1701 unload_parser(codec);
1705 if (codec->patch_ops.unsol_event) {
1706 err = init_unsol_queue(codec->bus);
1711 /* audio codec should override the mixer name */
1712 if (codec->afg || !*codec->bus->card->mixername)
1713 snprintf(codec->bus->card->mixername,
1714 sizeof(codec->bus->card->mixername),
1715 "%s %s", codec->vendor_name, codec->chip_name);
1718 EXPORT_SYMBOL_GPL(snd_hda_codec_configure);
1720 /* update the stream-id if changed */
1721 static void update_pcm_stream_id(struct hda_codec *codec,
1722 struct hda_cvt_setup *p, hda_nid_t nid,
1723 u32 stream_tag, int channel_id)
1725 unsigned int oldval, newval;
1727 if (p->stream_tag != stream_tag || p->channel_id != channel_id) {
1728 oldval = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0);
1729 newval = (stream_tag << 4) | channel_id;
1730 if (oldval != newval)
1731 snd_hda_codec_write(codec, nid, 0,
1732 AC_VERB_SET_CHANNEL_STREAMID,
1734 p->stream_tag = stream_tag;
1735 p->channel_id = channel_id;
1739 /* update the format-id if changed */
1740 static void update_pcm_format(struct hda_codec *codec, struct hda_cvt_setup *p,
1741 hda_nid_t nid, int format)
1743 unsigned int oldval;
1745 if (p->format_id != format) {
1746 oldval = snd_hda_codec_read(codec, nid, 0,
1747 AC_VERB_GET_STREAM_FORMAT, 0);
1748 if (oldval != format) {
1750 snd_hda_codec_write(codec, nid, 0,
1751 AC_VERB_SET_STREAM_FORMAT,
1754 p->format_id = format;
1759 * snd_hda_codec_setup_stream - set up the codec for streaming
1760 * @codec: the CODEC to set up
1761 * @nid: the NID to set up
1762 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
1763 * @channel_id: channel id to pass, zero based.
1764 * @format: stream format.
1766 void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
1768 int channel_id, int format)
1770 struct hda_codec *c;
1771 struct hda_cvt_setup *p;
1779 "hda_codec_setup_stream: NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
1780 nid, stream_tag, channel_id, format);
1781 p = get_hda_cvt_setup(codec, nid);
1785 if (codec->pcm_format_first)
1786 update_pcm_format(codec, p, nid, format);
1787 update_pcm_stream_id(codec, p, nid, stream_tag, channel_id);
1788 if (!codec->pcm_format_first)
1789 update_pcm_format(codec, p, nid, format);
1794 /* make other inactive cvts with the same stream-tag dirty */
1795 type = get_wcaps_type(get_wcaps(codec, nid));
1796 list_for_each_entry(c, &codec->bus->codec_list, list) {
1797 for (i = 0; i < c->cvt_setups.used; i++) {
1798 p = snd_array_elem(&c->cvt_setups, i);
1799 if (!p->active && p->stream_tag == stream_tag &&
1800 get_wcaps_type(get_wcaps(c, p->nid)) == type)
1805 EXPORT_SYMBOL_GPL(snd_hda_codec_setup_stream);
1807 static void really_cleanup_stream(struct hda_codec *codec,
1808 struct hda_cvt_setup *q);
1811 * __snd_hda_codec_cleanup_stream - clean up the codec for closing
1812 * @codec: the CODEC to clean up
1813 * @nid: the NID to clean up
1814 * @do_now: really clean up the stream instead of clearing the active flag
1816 void __snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid,
1819 struct hda_cvt_setup *p;
1824 if (codec->no_sticky_stream)
1827 codec_dbg(codec, "hda_codec_cleanup_stream: NID=0x%x\n", nid);
1828 p = get_hda_cvt_setup(codec, nid);
1830 /* here we just clear the active flag when do_now isn't set;
1831 * actual clean-ups will be done later in
1832 * purify_inactive_streams() called from snd_hda_codec_prpapre()
1835 really_cleanup_stream(codec, p);
1840 EXPORT_SYMBOL_GPL(__snd_hda_codec_cleanup_stream);
1842 static void really_cleanup_stream(struct hda_codec *codec,
1843 struct hda_cvt_setup *q)
1845 hda_nid_t nid = q->nid;
1846 if (q->stream_tag || q->channel_id)
1847 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
1849 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0
1851 memset(q, 0, sizeof(*q));
1855 /* clean up the all conflicting obsolete streams */
1856 static void purify_inactive_streams(struct hda_codec *codec)
1858 struct hda_codec *c;
1861 list_for_each_entry(c, &codec->bus->codec_list, list) {
1862 for (i = 0; i < c->cvt_setups.used; i++) {
1863 struct hda_cvt_setup *p;
1864 p = snd_array_elem(&c->cvt_setups, i);
1866 really_cleanup_stream(c, p);
1872 /* clean up all streams; called from suspend */
1873 static void hda_cleanup_all_streams(struct hda_codec *codec)
1877 for (i = 0; i < codec->cvt_setups.used; i++) {
1878 struct hda_cvt_setup *p = snd_array_elem(&codec->cvt_setups, i);
1880 really_cleanup_stream(codec, p);
1886 * amp access functions
1889 /* FIXME: more better hash key? */
1890 #define HDA_HASH_KEY(nid, dir, idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
1891 #define HDA_HASH_PINCAP_KEY(nid) (u32)((nid) + (0x02 << 24))
1892 #define HDA_HASH_PARPCM_KEY(nid) (u32)((nid) + (0x03 << 24))
1893 #define HDA_HASH_PARSTR_KEY(nid) (u32)((nid) + (0x04 << 24))
1894 #define INFO_AMP_CAPS (1<<0)
1895 #define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
1897 /* initialize the hash table */
1898 static void init_hda_cache(struct hda_cache_rec *cache,
1899 unsigned int record_size)
1901 memset(cache, 0, sizeof(*cache));
1902 memset(cache->hash, 0xff, sizeof(cache->hash));
1903 snd_array_init(&cache->buf, record_size, 64);
1906 static void free_hda_cache(struct hda_cache_rec *cache)
1908 snd_array_free(&cache->buf);
1911 /* query the hash. allocate an entry if not found. */
1912 static struct hda_cache_head *get_hash(struct hda_cache_rec *cache, u32 key)
1914 u16 idx = key % (u16)ARRAY_SIZE(cache->hash);
1915 u16 cur = cache->hash[idx];
1916 struct hda_cache_head *info;
1918 while (cur != 0xffff) {
1919 info = snd_array_elem(&cache->buf, cur);
1920 if (info->key == key)
1927 /* query the hash. allocate an entry if not found. */
1928 static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache,
1931 struct hda_cache_head *info = get_hash(cache, key);
1934 /* add a new hash entry */
1935 info = snd_array_new(&cache->buf);
1938 cur = snd_array_index(&cache->buf, info);
1942 idx = key % (u16)ARRAY_SIZE(cache->hash);
1943 info->next = cache->hash[idx];
1944 cache->hash[idx] = cur;
1949 /* query and allocate an amp hash entry */
1950 static inline struct hda_amp_info *
1951 get_alloc_amp_hash(struct hda_codec *codec, u32 key)
1953 return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key);
1956 /* overwrite the value with the key in the caps hash */
1957 static int write_caps_hash(struct hda_codec *codec, u32 key, unsigned int val)
1959 struct hda_amp_info *info;
1961 mutex_lock(&codec->hash_mutex);
1962 info = get_alloc_amp_hash(codec, key);
1964 mutex_unlock(&codec->hash_mutex);
1967 info->amp_caps = val;
1968 info->head.val |= INFO_AMP_CAPS;
1969 mutex_unlock(&codec->hash_mutex);
1973 /* query the value from the caps hash; if not found, fetch the current
1974 * value from the given function and store in the hash
1977 query_caps_hash(struct hda_codec *codec, hda_nid_t nid, int dir, u32 key,
1978 unsigned int (*func)(struct hda_codec *, hda_nid_t, int))
1980 struct hda_amp_info *info;
1983 mutex_lock(&codec->hash_mutex);
1984 info = get_alloc_amp_hash(codec, key);
1986 mutex_unlock(&codec->hash_mutex);
1989 if (!(info->head.val & INFO_AMP_CAPS)) {
1990 mutex_unlock(&codec->hash_mutex); /* for reentrance */
1991 val = func(codec, nid, dir);
1992 write_caps_hash(codec, key, val);
1994 val = info->amp_caps;
1995 mutex_unlock(&codec->hash_mutex);
2000 static unsigned int read_amp_cap(struct hda_codec *codec, hda_nid_t nid,
2003 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
2005 return snd_hda_param_read(codec, nid,
2006 direction == HDA_OUTPUT ?
2007 AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP);
2011 * query_amp_caps - query AMP capabilities
2012 * @codec: the HD-auio codec
2013 * @nid: the NID to query
2014 * @direction: either #HDA_INPUT or #HDA_OUTPUT
2016 * Query AMP capabilities for the given widget and direction.
2017 * Returns the obtained capability bits.
2019 * When cap bits have been already read, this doesn't read again but
2020 * returns the cached value.
2022 u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
2024 return query_caps_hash(codec, nid, direction,
2025 HDA_HASH_KEY(nid, direction, 0),
2028 EXPORT_SYMBOL_GPL(query_amp_caps);
2031 * snd_hda_check_amp_caps - query AMP capabilities
2032 * @codec: the HD-audio codec
2033 * @nid: the NID to query
2034 * @dir: either #HDA_INPUT or #HDA_OUTPUT
2035 * @bits: bit mask to check the result
2037 * Check whether the widget has the given amp capability for the direction.
2039 bool snd_hda_check_amp_caps(struct hda_codec *codec, hda_nid_t nid,
2040 int dir, unsigned int bits)
2044 if (get_wcaps(codec, nid) & (1 << (dir + 1)))
2045 if (query_amp_caps(codec, nid, dir) & bits)
2049 EXPORT_SYMBOL_GPL(snd_hda_check_amp_caps);
2052 * snd_hda_override_amp_caps - Override the AMP capabilities
2053 * @codec: the CODEC to clean up
2054 * @nid: the NID to clean up
2055 * @dir: either #HDA_INPUT or #HDA_OUTPUT
2056 * @caps: the capability bits to set
2058 * Override the cached AMP caps bits value by the given one.
2059 * This function is useful if the driver needs to adjust the AMP ranges,
2060 * e.g. limit to 0dB, etc.
2062 * Returns zero if successful or a negative error code.
2064 int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
2067 return write_caps_hash(codec, HDA_HASH_KEY(nid, dir, 0), caps);
2069 EXPORT_SYMBOL_GPL(snd_hda_override_amp_caps);
2071 static unsigned int read_pin_cap(struct hda_codec *codec, hda_nid_t nid,
2074 return snd_hda_param_read(codec, nid, AC_PAR_PIN_CAP);
2078 * snd_hda_query_pin_caps - Query PIN capabilities
2079 * @codec: the HD-auio codec
2080 * @nid: the NID to query
2082 * Query PIN capabilities for the given widget.
2083 * Returns the obtained capability bits.
2085 * When cap bits have been already read, this doesn't read again but
2086 * returns the cached value.
2088 u32 snd_hda_query_pin_caps(struct hda_codec *codec, hda_nid_t nid)
2090 return query_caps_hash(codec, nid, 0, HDA_HASH_PINCAP_KEY(nid),
2093 EXPORT_SYMBOL_GPL(snd_hda_query_pin_caps);
2096 * snd_hda_override_pin_caps - Override the pin capabilities
2098 * @nid: the NID to override
2099 * @caps: the capability bits to set
2101 * Override the cached PIN capabilitiy bits value by the given one.
2103 * Returns zero if successful or a negative error code.
2105 int snd_hda_override_pin_caps(struct hda_codec *codec, hda_nid_t nid,
2108 return write_caps_hash(codec, HDA_HASH_PINCAP_KEY(nid), caps);
2110 EXPORT_SYMBOL_GPL(snd_hda_override_pin_caps);
2112 /* read or sync the hash value with the current value;
2113 * call within hash_mutex
2115 static struct hda_amp_info *
2116 update_amp_hash(struct hda_codec *codec, hda_nid_t nid, int ch,
2117 int direction, int index, bool init_only)
2119 struct hda_amp_info *info;
2120 unsigned int parm, val = 0;
2121 bool val_read = false;
2124 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
2127 if (!(info->head.val & INFO_AMP_VOL(ch))) {
2129 mutex_unlock(&codec->hash_mutex);
2130 parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
2131 parm |= direction == HDA_OUTPUT ?
2132 AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
2134 val = snd_hda_codec_read(codec, nid, 0,
2135 AC_VERB_GET_AMP_GAIN_MUTE, parm);
2138 mutex_lock(&codec->hash_mutex);
2141 info->vol[ch] = val;
2142 info->head.val |= INFO_AMP_VOL(ch);
2143 } else if (init_only)
2149 * write the current volume in info to the h/w
2151 static void put_vol_mute(struct hda_codec *codec, unsigned int amp_caps,
2152 hda_nid_t nid, int ch, int direction, int index,
2157 parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
2158 parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
2159 parm |= index << AC_AMP_SET_INDEX_SHIFT;
2160 if ((val & HDA_AMP_MUTE) && !(amp_caps & AC_AMPCAP_MUTE) &&
2161 (amp_caps & AC_AMPCAP_MIN_MUTE))
2162 ; /* set the zero value as a fake mute */
2165 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
2169 * snd_hda_codec_amp_read - Read AMP value
2170 * @codec: HD-audio codec
2171 * @nid: NID to read the AMP value
2172 * @ch: channel (left=0 or right=1)
2173 * @direction: #HDA_INPUT or #HDA_OUTPUT
2174 * @index: the index value (only for input direction)
2176 * Read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
2178 int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
2179 int direction, int index)
2181 struct hda_amp_info *info;
2182 unsigned int val = 0;
2184 mutex_lock(&codec->hash_mutex);
2185 info = update_amp_hash(codec, nid, ch, direction, index, false);
2187 val = info->vol[ch];
2188 mutex_unlock(&codec->hash_mutex);
2191 EXPORT_SYMBOL_GPL(snd_hda_codec_amp_read);
2193 static int codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
2194 int direction, int idx, int mask, int val,
2197 struct hda_amp_info *info;
2199 unsigned int cache_only;
2201 if (snd_BUG_ON(mask & ~0xff))
2205 mutex_lock(&codec->hash_mutex);
2206 info = update_amp_hash(codec, nid, ch, direction, idx, init_only);
2208 mutex_unlock(&codec->hash_mutex);
2211 val |= info->vol[ch] & ~mask;
2212 if (info->vol[ch] == val) {
2213 mutex_unlock(&codec->hash_mutex);
2216 info->vol[ch] = val;
2217 cache_only = info->head.dirty = codec->cached_write;
2218 caps = info->amp_caps;
2219 mutex_unlock(&codec->hash_mutex);
2221 put_vol_mute(codec, caps, nid, ch, direction, idx, val);
2226 * snd_hda_codec_amp_update - update the AMP value
2227 * @codec: HD-audio codec
2228 * @nid: NID to read the AMP value
2229 * @ch: channel (left=0 or right=1)
2230 * @direction: #HDA_INPUT or #HDA_OUTPUT
2231 * @idx: the index value (only for input direction)
2232 * @mask: bit mask to set
2233 * @val: the bits value to set
2235 * Update the AMP value with a bit mask.
2236 * Returns 0 if the value is unchanged, 1 if changed.
2238 int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
2239 int direction, int idx, int mask, int val)
2241 return codec_amp_update(codec, nid, ch, direction, idx, mask, val, false);
2243 EXPORT_SYMBOL_GPL(snd_hda_codec_amp_update);
2246 * snd_hda_codec_amp_stereo - update the AMP stereo values
2247 * @codec: HD-audio codec
2248 * @nid: NID to read the AMP value
2249 * @direction: #HDA_INPUT or #HDA_OUTPUT
2250 * @idx: the index value (only for input direction)
2251 * @mask: bit mask to set
2252 * @val: the bits value to set
2254 * Update the AMP values like snd_hda_codec_amp_update(), but for a
2255 * stereo widget with the same mask and value.
2257 int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
2258 int direction, int idx, int mask, int val)
2262 if (snd_BUG_ON(mask & ~0xff))
2264 for (ch = 0; ch < 2; ch++)
2265 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
2269 EXPORT_SYMBOL_GPL(snd_hda_codec_amp_stereo);
2272 * snd_hda_codec_amp_init - initialize the AMP value
2273 * @codec: the HDA codec
2274 * @nid: NID to read the AMP value
2275 * @ch: channel (left=0 or right=1)
2276 * @dir: #HDA_INPUT or #HDA_OUTPUT
2277 * @idx: the index value (only for input direction)
2278 * @mask: bit mask to set
2279 * @val: the bits value to set
2281 * Works like snd_hda_codec_amp_update() but it writes the value only at
2282 * the first access. If the amp was already initialized / updated beforehand,
2283 * this does nothing.
2285 int snd_hda_codec_amp_init(struct hda_codec *codec, hda_nid_t nid, int ch,
2286 int dir, int idx, int mask, int val)
2288 return codec_amp_update(codec, nid, ch, dir, idx, mask, val, true);
2290 EXPORT_SYMBOL_GPL(snd_hda_codec_amp_init);
2293 * snd_hda_codec_amp_init_stereo - initialize the stereo AMP value
2294 * @codec: the HDA codec
2295 * @nid: NID to read the AMP value
2296 * @dir: #HDA_INPUT or #HDA_OUTPUT
2297 * @idx: the index value (only for input direction)
2298 * @mask: bit mask to set
2299 * @val: the bits value to set
2301 * Call snd_hda_codec_amp_init() for both stereo channels.
2303 int snd_hda_codec_amp_init_stereo(struct hda_codec *codec, hda_nid_t nid,
2304 int dir, int idx, int mask, int val)
2308 if (snd_BUG_ON(mask & ~0xff))
2310 for (ch = 0; ch < 2; ch++)
2311 ret |= snd_hda_codec_amp_init(codec, nid, ch, dir,
2315 EXPORT_SYMBOL_GPL(snd_hda_codec_amp_init_stereo);
2318 * snd_hda_codec_resume_amp - Resume all AMP commands from the cache
2319 * @codec: HD-audio codec
2321 * Resume the all amp commands from the cache.
2323 void snd_hda_codec_resume_amp(struct hda_codec *codec)
2327 mutex_lock(&codec->hash_mutex);
2328 codec->cached_write = 0;
2329 for (i = 0; i < codec->amp_cache.buf.used; i++) {
2330 struct hda_amp_info *buffer;
2333 unsigned int idx, dir, ch;
2334 struct hda_amp_info info;
2336 buffer = snd_array_elem(&codec->amp_cache.buf, i);
2337 if (!buffer->head.dirty)
2339 buffer->head.dirty = 0;
2341 key = info.head.key;
2345 idx = (key >> 16) & 0xff;
2346 dir = (key >> 24) & 0xff;
2347 for (ch = 0; ch < 2; ch++) {
2348 if (!(info.head.val & INFO_AMP_VOL(ch)))
2350 mutex_unlock(&codec->hash_mutex);
2351 put_vol_mute(codec, info.amp_caps, nid, ch, dir, idx,
2353 mutex_lock(&codec->hash_mutex);
2356 mutex_unlock(&codec->hash_mutex);
2358 EXPORT_SYMBOL_GPL(snd_hda_codec_resume_amp);
2360 static u32 get_amp_max_value(struct hda_codec *codec, hda_nid_t nid, int dir,
2363 u32 caps = query_amp_caps(codec, nid, dir);
2365 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
2372 * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer
2373 * @kcontrol: referred ctl element
2374 * @uinfo: pointer to get/store the data
2376 * The control element is supposed to have the private_value field
2377 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2379 int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
2380 struct snd_ctl_elem_info *uinfo)
2382 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2383 u16 nid = get_amp_nid(kcontrol);
2384 u8 chs = get_amp_channels(kcontrol);
2385 int dir = get_amp_direction(kcontrol);
2386 unsigned int ofs = get_amp_offset(kcontrol);
2388 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2389 uinfo->count = chs == 3 ? 2 : 1;
2390 uinfo->value.integer.min = 0;
2391 uinfo->value.integer.max = get_amp_max_value(codec, nid, dir, ofs);
2392 if (!uinfo->value.integer.max) {
2394 "num_steps = 0 for NID=0x%x (ctl = %s)\n",
2395 nid, kcontrol->id.name);
2400 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_info);
2403 static inline unsigned int
2404 read_amp_value(struct hda_codec *codec, hda_nid_t nid,
2405 int ch, int dir, int idx, unsigned int ofs)
2408 val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
2409 val &= HDA_AMP_VOLMASK;
2418 update_amp_value(struct hda_codec *codec, hda_nid_t nid,
2419 int ch, int dir, int idx, unsigned int ofs,
2422 unsigned int maxval;
2426 /* ofs = 0: raw max value */
2427 maxval = get_amp_max_value(codec, nid, dir, 0);
2430 return snd_hda_codec_amp_update(codec, nid, ch, dir, idx,
2431 HDA_AMP_VOLMASK, val);
2435 * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume
2436 * @kcontrol: ctl element
2437 * @ucontrol: pointer to get/store the data
2439 * The control element is supposed to have the private_value field
2440 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2442 int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
2443 struct snd_ctl_elem_value *ucontrol)
2445 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2446 hda_nid_t nid = get_amp_nid(kcontrol);
2447 int chs = get_amp_channels(kcontrol);
2448 int dir = get_amp_direction(kcontrol);
2449 int idx = get_amp_index(kcontrol);
2450 unsigned int ofs = get_amp_offset(kcontrol);
2451 long *valp = ucontrol->value.integer.value;
2454 *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs);
2456 *valp = read_amp_value(codec, nid, 1, dir, idx, ofs);
2459 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_get);
2462 * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume
2463 * @kcontrol: ctl element
2464 * @ucontrol: pointer to get/store the data
2466 * The control element is supposed to have the private_value field
2467 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2469 int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
2470 struct snd_ctl_elem_value *ucontrol)
2472 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2473 hda_nid_t nid = get_amp_nid(kcontrol);
2474 int chs = get_amp_channels(kcontrol);
2475 int dir = get_amp_direction(kcontrol);
2476 int idx = get_amp_index(kcontrol);
2477 unsigned int ofs = get_amp_offset(kcontrol);
2478 long *valp = ucontrol->value.integer.value;
2481 snd_hda_power_up(codec);
2483 change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp);
2487 change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp);
2488 snd_hda_power_down(codec);
2491 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_put);
2494 * snd_hda_mixer_amp_volume_put - TLV callback for a standard AMP mixer volume
2495 * @kcontrol: ctl element
2496 * @op_flag: operation flag
2497 * @size: byte size of input TLV
2500 * The control element is supposed to have the private_value field
2501 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2503 int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2504 unsigned int size, unsigned int __user *_tlv)
2506 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2507 hda_nid_t nid = get_amp_nid(kcontrol);
2508 int dir = get_amp_direction(kcontrol);
2509 unsigned int ofs = get_amp_offset(kcontrol);
2510 bool min_mute = get_amp_min_mute(kcontrol);
2511 u32 caps, val1, val2;
2513 if (size < 4 * sizeof(unsigned int))
2515 caps = query_amp_caps(codec, nid, dir);
2516 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2517 val2 = (val2 + 1) * 25;
2518 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
2520 val1 = ((int)val1) * ((int)val2);
2521 if (min_mute || (caps & AC_AMPCAP_MIN_MUTE))
2522 val2 |= TLV_DB_SCALE_MUTE;
2523 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
2525 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
2527 if (put_user(val1, _tlv + 2))
2529 if (put_user(val2, _tlv + 3))
2533 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_tlv);
2536 * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control
2537 * @codec: HD-audio codec
2538 * @nid: NID of a reference widget
2539 * @dir: #HDA_INPUT or #HDA_OUTPUT
2540 * @tlv: TLV data to be stored, at least 4 elements
2542 * Set (static) TLV data for a virtual master volume using the AMP caps
2543 * obtained from the reference NID.
2544 * The volume range is recalculated as if the max volume is 0dB.
2546 void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
2552 caps = query_amp_caps(codec, nid, dir);
2553 nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
2554 step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2555 step = (step + 1) * 25;
2556 tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
2557 tlv[1] = 2 * sizeof(unsigned int);
2558 tlv[2] = -nums * step;
2561 EXPORT_SYMBOL_GPL(snd_hda_set_vmaster_tlv);
2563 /* find a mixer control element with the given name */
2564 static struct snd_kcontrol *
2565 find_mixer_ctl(struct hda_codec *codec, const char *name, int dev, int idx)
2567 struct snd_ctl_elem_id id;
2568 memset(&id, 0, sizeof(id));
2569 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
2572 if (snd_BUG_ON(strlen(name) >= sizeof(id.name)))
2574 strcpy(id.name, name);
2575 return snd_ctl_find_id(codec->bus->card, &id);
2579 * snd_hda_find_mixer_ctl - Find a mixer control element with the given name
2580 * @codec: HD-audio codec
2581 * @name: ctl id name string
2583 * Get the control element with the given id string and IFACE_MIXER.
2585 struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
2588 return find_mixer_ctl(codec, name, 0, 0);
2590 EXPORT_SYMBOL_GPL(snd_hda_find_mixer_ctl);
2592 static int find_empty_mixer_ctl_idx(struct hda_codec *codec, const char *name,
2596 /* 16 ctlrs should be large enough */
2597 for (i = 0, idx = start_idx; i < 16; i++, idx++) {
2598 if (!find_mixer_ctl(codec, name, 0, idx))
2605 * snd_hda_ctl_add - Add a control element and assign to the codec
2606 * @codec: HD-audio codec
2607 * @nid: corresponding NID (optional)
2608 * @kctl: the control element to assign
2610 * Add the given control element to an array inside the codec instance.
2611 * All control elements belonging to a codec are supposed to be added
2612 * by this function so that a proper clean-up works at the free or
2613 * reconfiguration time.
2615 * If non-zero @nid is passed, the NID is assigned to the control element.
2616 * The assignment is shown in the codec proc file.
2618 * snd_hda_ctl_add() checks the control subdev id field whether
2619 * #HDA_SUBDEV_NID_FLAG bit is set. If set (and @nid is zero), the lower
2620 * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit
2621 * specifies if kctl->private_value is a HDA amplifier value.
2623 int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid,
2624 struct snd_kcontrol *kctl)
2627 unsigned short flags = 0;
2628 struct hda_nid_item *item;
2630 if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) {
2631 flags |= HDA_NID_ITEM_AMP;
2633 nid = get_amp_nid_(kctl->private_value);
2635 if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0)
2636 nid = kctl->id.subdevice & 0xffff;
2637 if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG))
2638 kctl->id.subdevice = 0;
2639 err = snd_ctl_add(codec->bus->card, kctl);
2642 item = snd_array_new(&codec->mixers);
2647 item->flags = flags;
2650 EXPORT_SYMBOL_GPL(snd_hda_ctl_add);
2653 * snd_hda_add_nid - Assign a NID to a control element
2654 * @codec: HD-audio codec
2655 * @nid: corresponding NID (optional)
2656 * @kctl: the control element to assign
2657 * @index: index to kctl
2659 * Add the given control element to an array inside the codec instance.
2660 * This function is used when #snd_hda_ctl_add cannot be used for 1:1
2661 * NID:KCTL mapping - for example "Capture Source" selector.
2663 int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl,
2664 unsigned int index, hda_nid_t nid)
2666 struct hda_nid_item *item;
2669 item = snd_array_new(&codec->nids);
2673 item->index = index;
2677 codec_err(codec, "no NID for mapping control %s:%d:%d\n",
2678 kctl->id.name, kctl->id.index, index);
2681 EXPORT_SYMBOL_GPL(snd_hda_add_nid);
2684 * snd_hda_ctls_clear - Clear all controls assigned to the given codec
2685 * @codec: HD-audio codec
2687 void snd_hda_ctls_clear(struct hda_codec *codec)
2690 struct hda_nid_item *items = codec->mixers.list;
2691 for (i = 0; i < codec->mixers.used; i++)
2692 snd_ctl_remove(codec->bus->card, items[i].kctl);
2693 snd_array_free(&codec->mixers);
2694 snd_array_free(&codec->nids);
2698 * snd_hda_lock_devices - pseudo device locking
2701 * toggle card->shutdown to allow/disallow the device access (as a hack)
2703 int snd_hda_lock_devices(struct hda_bus *bus)
2705 struct snd_card *card = bus->card;
2706 struct hda_codec *codec;
2708 spin_lock(&card->files_lock);
2712 if (!list_empty(&card->ctl_files))
2715 list_for_each_entry(codec, &bus->codec_list, list) {
2717 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
2718 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
2721 if (cpcm->pcm->streams[0].substream_opened ||
2722 cpcm->pcm->streams[1].substream_opened)
2726 spin_unlock(&card->files_lock);
2732 spin_unlock(&card->files_lock);
2735 EXPORT_SYMBOL_GPL(snd_hda_lock_devices);
2738 * snd_hda_unlock_devices - pseudo device unlocking
2741 void snd_hda_unlock_devices(struct hda_bus *bus)
2743 struct snd_card *card = bus->card;
2746 spin_lock(&card->files_lock);
2748 spin_unlock(&card->files_lock);
2750 EXPORT_SYMBOL_GPL(snd_hda_unlock_devices);
2753 * snd_hda_codec_reset - Clear all objects assigned to the codec
2754 * @codec: HD-audio codec
2756 * This frees the all PCM and control elements assigned to the codec, and
2757 * clears the caches and restores the pin default configurations.
2759 * When a device is being used, it returns -EBSY. If successfully freed,
2762 int snd_hda_codec_reset(struct hda_codec *codec)
2764 struct hda_bus *bus = codec->bus;
2765 struct snd_card *card = bus->card;
2768 if (snd_hda_lock_devices(bus) < 0)
2771 /* OK, let it free */
2772 cancel_delayed_work_sync(&codec->jackpoll_work);
2774 cancel_delayed_work_sync(&codec->power_work);
2775 flush_workqueue(bus->workq);
2777 snd_hda_ctls_clear(codec);
2779 for (i = 0; i < codec->num_pcms; i++) {
2780 if (codec->pcm_info[i].pcm) {
2781 snd_device_free(card, codec->pcm_info[i].pcm);
2782 clear_bit(codec->pcm_info[i].device,
2786 snd_hda_detach_beep_device(codec);
2787 if (codec->patch_ops.free)
2788 codec->patch_ops.free(codec);
2789 memset(&codec->patch_ops, 0, sizeof(codec->patch_ops));
2790 snd_hda_jack_tbl_clear(codec);
2791 codec->proc_widget_hook = NULL;
2793 free_hda_cache(&codec->amp_cache);
2794 free_hda_cache(&codec->cmd_cache);
2795 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
2796 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
2797 /* free only driver_pins so that init_pins + user_pins are restored */
2798 snd_array_free(&codec->driver_pins);
2799 snd_array_free(&codec->cvt_setups);
2800 snd_array_free(&codec->spdif_out);
2801 snd_array_free(&codec->verbs);
2802 codec->num_pcms = 0;
2803 codec->pcm_info = NULL;
2804 codec->preset = NULL;
2805 codec->slave_dig_outs = NULL;
2806 codec->spdif_status_reset = 0;
2807 unload_parser(codec);
2808 module_put(codec->owner);
2809 codec->owner = NULL;
2811 /* allow device access again */
2812 snd_hda_unlock_devices(bus);
2816 typedef int (*map_slave_func_t)(struct hda_codec *, void *, struct snd_kcontrol *);
2818 /* apply the function to all matching slave ctls in the mixer list */
2819 static int map_slaves(struct hda_codec *codec, const char * const *slaves,
2820 const char *suffix, map_slave_func_t func, void *data)
2822 struct hda_nid_item *items;
2823 const char * const *s;
2826 items = codec->mixers.list;
2827 for (i = 0; i < codec->mixers.used; i++) {
2828 struct snd_kcontrol *sctl = items[i].kctl;
2829 if (!sctl || sctl->id.iface != SNDRV_CTL_ELEM_IFACE_MIXER)
2831 for (s = slaves; *s; s++) {
2832 char tmpname[sizeof(sctl->id.name)];
2833 const char *name = *s;
2835 snprintf(tmpname, sizeof(tmpname), "%s %s",
2839 if (!strcmp(sctl->id.name, name)) {
2840 err = func(codec, data, sctl);
2850 static int check_slave_present(struct hda_codec *codec,
2851 void *data, struct snd_kcontrol *sctl)
2856 /* guess the value corresponding to 0dB */
2857 static int get_kctl_0dB_offset(struct hda_codec *codec,
2858 struct snd_kcontrol *kctl, int *step_to_check)
2861 const int *tlv = NULL;
2864 if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
2865 /* FIXME: set_fs() hack for obtaining user-space TLV data */
2866 mm_segment_t fs = get_fs();
2868 if (!kctl->tlv.c(kctl, 0, sizeof(_tlv), _tlv))
2871 } else if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_READ)
2873 if (tlv && tlv[0] == SNDRV_CTL_TLVT_DB_SCALE) {
2875 step &= ~TLV_DB_SCALE_MUTE;
2878 if (*step_to_check && *step_to_check != step) {
2879 codec_err(codec, "Mismatching dB step for vmaster slave (%d!=%d)\n",
2880 - *step_to_check, step);
2883 *step_to_check = step;
2884 val = -tlv[2] / step;
2889 /* call kctl->put with the given value(s) */
2890 static int put_kctl_with_value(struct snd_kcontrol *kctl, int val)
2892 struct snd_ctl_elem_value *ucontrol;
2893 ucontrol = kzalloc(sizeof(*ucontrol), GFP_KERNEL);
2896 ucontrol->value.integer.value[0] = val;
2897 ucontrol->value.integer.value[1] = val;
2898 kctl->put(kctl, ucontrol);
2903 /* initialize the slave volume with 0dB */
2904 static int init_slave_0dB(struct hda_codec *codec,
2905 void *data, struct snd_kcontrol *slave)
2907 int offset = get_kctl_0dB_offset(codec, slave, data);
2909 put_kctl_with_value(slave, offset);
2913 /* unmute the slave */
2914 static int init_slave_unmute(struct hda_codec *codec,
2915 void *data, struct snd_kcontrol *slave)
2917 return put_kctl_with_value(slave, 1);
2920 static int add_slave(struct hda_codec *codec,
2921 void *data, struct snd_kcontrol *slave)
2923 return snd_ctl_add_slave(data, slave);
2927 * __snd_hda_add_vmaster - create a virtual master control and add slaves
2928 * @codec: HD-audio codec
2929 * @name: vmaster control name
2930 * @tlv: TLV data (optional)
2931 * @slaves: slave control names (optional)
2932 * @suffix: suffix string to each slave name (optional)
2933 * @init_slave_vol: initialize slaves to unmute/0dB
2934 * @ctl_ret: store the vmaster kcontrol in return
2936 * Create a virtual master control with the given name. The TLV data
2937 * must be either NULL or a valid data.
2939 * @slaves is a NULL-terminated array of strings, each of which is a
2940 * slave control name. All controls with these names are assigned to
2941 * the new virtual master control.
2943 * This function returns zero if successful or a negative error code.
2945 int __snd_hda_add_vmaster(struct hda_codec *codec, char *name,
2946 unsigned int *tlv, const char * const *slaves,
2947 const char *suffix, bool init_slave_vol,
2948 struct snd_kcontrol **ctl_ret)
2950 struct snd_kcontrol *kctl;
2956 err = map_slaves(codec, slaves, suffix, check_slave_present, NULL);
2958 codec_dbg(codec, "No slave found for %s\n", name);
2961 kctl = snd_ctl_make_virtual_master(name, tlv);
2964 err = snd_hda_ctl_add(codec, 0, kctl);
2968 err = map_slaves(codec, slaves, suffix, add_slave, kctl);
2972 /* init with master mute & zero volume */
2973 put_kctl_with_value(kctl, 0);
2974 if (init_slave_vol) {
2976 map_slaves(codec, slaves, suffix,
2977 tlv ? init_slave_0dB : init_slave_unmute, &step);
2984 EXPORT_SYMBOL_GPL(__snd_hda_add_vmaster);
2987 * mute-LED control using vmaster
2989 static int vmaster_mute_mode_info(struct snd_kcontrol *kcontrol,
2990 struct snd_ctl_elem_info *uinfo)
2992 static const char * const texts[] = {
2993 "On", "Off", "Follow Master"
2996 return snd_ctl_enum_info(uinfo, 1, 3, texts);
2999 static int vmaster_mute_mode_get(struct snd_kcontrol *kcontrol,
3000 struct snd_ctl_elem_value *ucontrol)
3002 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
3003 ucontrol->value.enumerated.item[0] = hook->mute_mode;
3007 static int vmaster_mute_mode_put(struct snd_kcontrol *kcontrol,
3008 struct snd_ctl_elem_value *ucontrol)
3010 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
3011 unsigned int old_mode = hook->mute_mode;
3013 hook->mute_mode = ucontrol->value.enumerated.item[0];
3014 if (hook->mute_mode > HDA_VMUTE_FOLLOW_MASTER)
3015 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
3016 if (old_mode == hook->mute_mode)
3018 snd_hda_sync_vmaster_hook(hook);
3022 static struct snd_kcontrol_new vmaster_mute_mode = {
3023 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3024 .name = "Mute-LED Mode",
3025 .info = vmaster_mute_mode_info,
3026 .get = vmaster_mute_mode_get,
3027 .put = vmaster_mute_mode_put,
3031 * snd_hda_add_vmaster_hook - Add a vmaster hook for mute-LED
3032 * @codec: the HDA codec
3033 * @hook: the vmaster hook object
3034 * @expose_enum_ctl: flag to create an enum ctl
3036 * Add a mute-LED hook with the given vmaster switch kctl.
3037 * When @expose_enum_ctl is set, "Mute-LED Mode" control is automatically
3038 * created and associated with the given hook.
3040 int snd_hda_add_vmaster_hook(struct hda_codec *codec,
3041 struct hda_vmaster_mute_hook *hook,
3042 bool expose_enum_ctl)
3044 struct snd_kcontrol *kctl;
3046 if (!hook->hook || !hook->sw_kctl)
3048 snd_ctl_add_vmaster_hook(hook->sw_kctl, hook->hook, codec);
3049 hook->codec = codec;
3050 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
3051 if (!expose_enum_ctl)
3053 kctl = snd_ctl_new1(&vmaster_mute_mode, hook);
3056 return snd_hda_ctl_add(codec, 0, kctl);
3058 EXPORT_SYMBOL_GPL(snd_hda_add_vmaster_hook);
3061 * snd_hda_sync_vmaster_hook - Sync vmaster hook
3062 * @hook: the vmaster hook
3064 * Call the hook with the current value for synchronization.
3065 * Should be called in init callback.
3067 void snd_hda_sync_vmaster_hook(struct hda_vmaster_mute_hook *hook)
3069 if (!hook->hook || !hook->codec)
3071 /* don't call vmaster hook in the destructor since it might have
3072 * been already destroyed
3074 if (hook->codec->bus->shutdown)
3076 switch (hook->mute_mode) {
3077 case HDA_VMUTE_FOLLOW_MASTER:
3078 snd_ctl_sync_vmaster_hook(hook->sw_kctl);
3081 hook->hook(hook->codec, hook->mute_mode);
3085 EXPORT_SYMBOL_GPL(snd_hda_sync_vmaster_hook);
3089 * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch
3090 * @kcontrol: referred ctl element
3091 * @uinfo: pointer to get/store the data
3093 * The control element is supposed to have the private_value field
3094 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
3096 int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
3097 struct snd_ctl_elem_info *uinfo)
3099 int chs = get_amp_channels(kcontrol);
3101 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
3102 uinfo->count = chs == 3 ? 2 : 1;
3103 uinfo->value.integer.min = 0;
3104 uinfo->value.integer.max = 1;
3107 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_info);
3110 * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch
3111 * @kcontrol: ctl element
3112 * @ucontrol: pointer to get/store the data
3114 * The control element is supposed to have the private_value field
3115 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
3117 int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
3118 struct snd_ctl_elem_value *ucontrol)
3120 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3121 hda_nid_t nid = get_amp_nid(kcontrol);
3122 int chs = get_amp_channels(kcontrol);
3123 int dir = get_amp_direction(kcontrol);
3124 int idx = get_amp_index(kcontrol);
3125 long *valp = ucontrol->value.integer.value;
3128 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
3129 HDA_AMP_MUTE) ? 0 : 1;
3131 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
3132 HDA_AMP_MUTE) ? 0 : 1;
3135 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_get);
3138 * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch
3139 * @kcontrol: ctl element
3140 * @ucontrol: pointer to get/store the data
3142 * The control element is supposed to have the private_value field
3143 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
3145 int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
3146 struct snd_ctl_elem_value *ucontrol)
3148 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3149 hda_nid_t nid = get_amp_nid(kcontrol);
3150 int chs = get_amp_channels(kcontrol);
3151 int dir = get_amp_direction(kcontrol);
3152 int idx = get_amp_index(kcontrol);
3153 long *valp = ucontrol->value.integer.value;
3156 snd_hda_power_up(codec);
3158 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
3160 *valp ? 0 : HDA_AMP_MUTE);
3164 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
3166 *valp ? 0 : HDA_AMP_MUTE);
3167 hda_call_check_power_status(codec, nid);
3168 snd_hda_power_down(codec);
3171 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_put);
3174 * bound volume controls
3176 * bind multiple volumes (# indices, from 0)
3179 #define AMP_VAL_IDX_SHIFT 19
3180 #define AMP_VAL_IDX_MASK (0x0f<<19)
3183 * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control
3184 * @kcontrol: ctl element
3185 * @ucontrol: pointer to get/store the data
3187 * The control element is supposed to have the private_value field
3188 * set up via HDA_BIND_MUTE*() macros.
3190 int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
3191 struct snd_ctl_elem_value *ucontrol)
3193 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3197 mutex_lock(&codec->control_mutex);
3198 pval = kcontrol->private_value;
3199 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
3200 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
3201 kcontrol->private_value = pval;
3202 mutex_unlock(&codec->control_mutex);
3205 EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_switch_get);
3208 * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control
3209 * @kcontrol: ctl element
3210 * @ucontrol: pointer to get/store the data
3212 * The control element is supposed to have the private_value field
3213 * set up via HDA_BIND_MUTE*() macros.
3215 int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
3216 struct snd_ctl_elem_value *ucontrol)
3218 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3220 int i, indices, err = 0, change = 0;
3222 mutex_lock(&codec->control_mutex);
3223 pval = kcontrol->private_value;
3224 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
3225 for (i = 0; i < indices; i++) {
3226 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
3227 (i << AMP_VAL_IDX_SHIFT);
3228 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
3233 kcontrol->private_value = pval;
3234 mutex_unlock(&codec->control_mutex);
3235 return err < 0 ? err : change;
3237 EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_switch_put);
3240 * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control
3241 * @kcontrol: referred ctl element
3242 * @uinfo: pointer to get/store the data
3244 * The control element is supposed to have the private_value field
3245 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
3247 int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
3248 struct snd_ctl_elem_info *uinfo)
3250 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3251 struct hda_bind_ctls *c;
3254 mutex_lock(&codec->control_mutex);
3255 c = (struct hda_bind_ctls *)kcontrol->private_value;
3256 kcontrol->private_value = *c->values;
3257 err = c->ops->info(kcontrol, uinfo);
3258 kcontrol->private_value = (long)c;
3259 mutex_unlock(&codec->control_mutex);
3262 EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_info);
3265 * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control
3266 * @kcontrol: ctl element
3267 * @ucontrol: pointer to get/store the data
3269 * The control element is supposed to have the private_value field
3270 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
3272 int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
3273 struct snd_ctl_elem_value *ucontrol)
3275 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3276 struct hda_bind_ctls *c;
3279 mutex_lock(&codec->control_mutex);
3280 c = (struct hda_bind_ctls *)kcontrol->private_value;
3281 kcontrol->private_value = *c->values;
3282 err = c->ops->get(kcontrol, ucontrol);
3283 kcontrol->private_value = (long)c;
3284 mutex_unlock(&codec->control_mutex);
3287 EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_get);
3290 * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control
3291 * @kcontrol: ctl element
3292 * @ucontrol: pointer to get/store the data
3294 * The control element is supposed to have the private_value field
3295 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
3297 int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
3298 struct snd_ctl_elem_value *ucontrol)
3300 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3301 struct hda_bind_ctls *c;
3302 unsigned long *vals;
3303 int err = 0, change = 0;
3305 mutex_lock(&codec->control_mutex);
3306 c = (struct hda_bind_ctls *)kcontrol->private_value;
3307 for (vals = c->values; *vals; vals++) {
3308 kcontrol->private_value = *vals;
3309 err = c->ops->put(kcontrol, ucontrol);
3314 kcontrol->private_value = (long)c;
3315 mutex_unlock(&codec->control_mutex);
3316 return err < 0 ? err : change;
3318 EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_put);
3321 * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control
3322 * @kcontrol: ctl element
3323 * @op_flag: operation flag
3324 * @size: byte size of input TLV
3327 * The control element is supposed to have the private_value field
3328 * set up via HDA_BIND_VOL() macro.
3330 int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
3331 unsigned int size, unsigned int __user *tlv)
3333 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3334 struct hda_bind_ctls *c;
3337 mutex_lock(&codec->control_mutex);
3338 c = (struct hda_bind_ctls *)kcontrol->private_value;
3339 kcontrol->private_value = *c->values;
3340 err = c->ops->tlv(kcontrol, op_flag, size, tlv);
3341 kcontrol->private_value = (long)c;
3342 mutex_unlock(&codec->control_mutex);
3345 EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_tlv);
3347 struct hda_ctl_ops snd_hda_bind_vol = {
3348 .info = snd_hda_mixer_amp_volume_info,
3349 .get = snd_hda_mixer_amp_volume_get,
3350 .put = snd_hda_mixer_amp_volume_put,
3351 .tlv = snd_hda_mixer_amp_tlv
3353 EXPORT_SYMBOL_GPL(snd_hda_bind_vol);
3355 struct hda_ctl_ops snd_hda_bind_sw = {
3356 .info = snd_hda_mixer_amp_switch_info,
3357 .get = snd_hda_mixer_amp_switch_get,
3358 .put = snd_hda_mixer_amp_switch_put,
3359 .tlv = snd_hda_mixer_amp_tlv
3361 EXPORT_SYMBOL_GPL(snd_hda_bind_sw);
3364 * SPDIF out controls
3367 static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
3368 struct snd_ctl_elem_info *uinfo)
3370 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
3375 static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
3376 struct snd_ctl_elem_value *ucontrol)
3378 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
3379 IEC958_AES0_NONAUDIO |
3380 IEC958_AES0_CON_EMPHASIS_5015 |
3381 IEC958_AES0_CON_NOT_COPYRIGHT;
3382 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
3383 IEC958_AES1_CON_ORIGINAL;
3387 static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
3388 struct snd_ctl_elem_value *ucontrol)
3390 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
3391 IEC958_AES0_NONAUDIO |
3392 IEC958_AES0_PRO_EMPHASIS_5015;
3396 static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
3397 struct snd_ctl_elem_value *ucontrol)
3399 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3400 int idx = kcontrol->private_value;
3401 struct hda_spdif_out *spdif;
3403 mutex_lock(&codec->spdif_mutex);
3404 spdif = snd_array_elem(&codec->spdif_out, idx);
3405 ucontrol->value.iec958.status[0] = spdif->status & 0xff;
3406 ucontrol->value.iec958.status[1] = (spdif->status >> 8) & 0xff;
3407 ucontrol->value.iec958.status[2] = (spdif->status >> 16) & 0xff;
3408 ucontrol->value.iec958.status[3] = (spdif->status >> 24) & 0xff;
3409 mutex_unlock(&codec->spdif_mutex);
3414 /* convert from SPDIF status bits to HDA SPDIF bits
3415 * bit 0 (DigEn) is always set zero (to be filled later)
3417 static unsigned short convert_from_spdif_status(unsigned int sbits)
3419 unsigned short val = 0;
3421 if (sbits & IEC958_AES0_PROFESSIONAL)
3422 val |= AC_DIG1_PROFESSIONAL;
3423 if (sbits & IEC958_AES0_NONAUDIO)
3424 val |= AC_DIG1_NONAUDIO;
3425 if (sbits & IEC958_AES0_PROFESSIONAL) {
3426 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
3427 IEC958_AES0_PRO_EMPHASIS_5015)
3428 val |= AC_DIG1_EMPHASIS;
3430 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
3431 IEC958_AES0_CON_EMPHASIS_5015)
3432 val |= AC_DIG1_EMPHASIS;
3433 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
3434 val |= AC_DIG1_COPYRIGHT;
3435 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
3436 val |= AC_DIG1_LEVEL;
3437 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
3442 /* convert to SPDIF status bits from HDA SPDIF bits
3444 static unsigned int convert_to_spdif_status(unsigned short val)
3446 unsigned int sbits = 0;
3448 if (val & AC_DIG1_NONAUDIO)
3449 sbits |= IEC958_AES0_NONAUDIO;
3450 if (val & AC_DIG1_PROFESSIONAL)
3451 sbits |= IEC958_AES0_PROFESSIONAL;
3452 if (sbits & IEC958_AES0_PROFESSIONAL) {
3453 if (val & AC_DIG1_EMPHASIS)
3454 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
3456 if (val & AC_DIG1_EMPHASIS)
3457 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
3458 if (!(val & AC_DIG1_COPYRIGHT))
3459 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
3460 if (val & AC_DIG1_LEVEL)
3461 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
3462 sbits |= val & (0x7f << 8);
3467 /* set digital convert verbs both for the given NID and its slaves */
3468 static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
3473 snd_hda_codec_write_cache(codec, nid, 0, verb, val);
3474 d = codec->slave_dig_outs;
3478 snd_hda_codec_write_cache(codec, *d, 0, verb, val);
3481 static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
3485 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1);
3487 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2);
3490 static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
3491 struct snd_ctl_elem_value *ucontrol)
3493 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3494 int idx = kcontrol->private_value;
3495 struct hda_spdif_out *spdif;
3500 mutex_lock(&codec->spdif_mutex);
3501 spdif = snd_array_elem(&codec->spdif_out, idx);
3503 spdif->status = ucontrol->value.iec958.status[0] |
3504 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
3505 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
3506 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
3507 val = convert_from_spdif_status(spdif->status);
3508 val |= spdif->ctls & 1;
3509 change = spdif->ctls != val;
3511 if (change && nid != (u16)-1)
3512 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
3513 mutex_unlock(&codec->spdif_mutex);
3517 #define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
3519 static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
3520 struct snd_ctl_elem_value *ucontrol)
3522 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3523 int idx = kcontrol->private_value;
3524 struct hda_spdif_out *spdif;
3526 mutex_lock(&codec->spdif_mutex);
3527 spdif = snd_array_elem(&codec->spdif_out, idx);
3528 ucontrol->value.integer.value[0] = spdif->ctls & AC_DIG1_ENABLE;
3529 mutex_unlock(&codec->spdif_mutex);
3533 static inline void set_spdif_ctls(struct hda_codec *codec, hda_nid_t nid,
3536 set_dig_out_convert(codec, nid, dig1, dig2);
3537 /* unmute amp switch (if any) */
3538 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
3539 (dig1 & AC_DIG1_ENABLE))
3540 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
3544 static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
3545 struct snd_ctl_elem_value *ucontrol)
3547 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3548 int idx = kcontrol->private_value;
3549 struct hda_spdif_out *spdif;
3554 mutex_lock(&codec->spdif_mutex);
3555 spdif = snd_array_elem(&codec->spdif_out, idx);
3557 val = spdif->ctls & ~AC_DIG1_ENABLE;
3558 if (ucontrol->value.integer.value[0])
3559 val |= AC_DIG1_ENABLE;
3560 change = spdif->ctls != val;
3562 if (change && nid != (u16)-1)
3563 set_spdif_ctls(codec, nid, val & 0xff, -1);
3564 mutex_unlock(&codec->spdif_mutex);
3568 static struct snd_kcontrol_new dig_mixes[] = {
3570 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3571 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3572 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
3573 .info = snd_hda_spdif_mask_info,
3574 .get = snd_hda_spdif_cmask_get,
3577 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3578 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3579 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK),
3580 .info = snd_hda_spdif_mask_info,
3581 .get = snd_hda_spdif_pmask_get,
3584 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3585 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
3586 .info = snd_hda_spdif_mask_info,
3587 .get = snd_hda_spdif_default_get,
3588 .put = snd_hda_spdif_default_put,
3591 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3592 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
3593 .info = snd_hda_spdif_out_switch_info,
3594 .get = snd_hda_spdif_out_switch_get,
3595 .put = snd_hda_spdif_out_switch_put,
3601 * snd_hda_create_dig_out_ctls - create Output SPDIF-related controls
3602 * @codec: the HDA codec
3603 * @associated_nid: NID that new ctls associated with
3604 * @cvt_nid: converter NID
3605 * @type: HDA_PCM_TYPE_*
3606 * Creates controls related with the digital output.
3607 * Called from each patch supporting the digital out.
3609 * Returns 0 if successful, or a negative error code.
3611 int snd_hda_create_dig_out_ctls(struct hda_codec *codec,
3612 hda_nid_t associated_nid,
3617 struct snd_kcontrol *kctl;
3618 struct snd_kcontrol_new *dig_mix;
3620 const int spdif_index = 16;
3621 struct hda_spdif_out *spdif;
3622 struct hda_bus *bus = codec->bus;
3624 if (bus->primary_dig_out_type == HDA_PCM_TYPE_HDMI &&
3625 type == HDA_PCM_TYPE_SPDIF) {
3627 } else if (bus->primary_dig_out_type == HDA_PCM_TYPE_SPDIF &&
3628 type == HDA_PCM_TYPE_HDMI) {
3629 /* suppose a single SPDIF device */
3630 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
3631 kctl = find_mixer_ctl(codec, dig_mix->name, 0, 0);
3634 kctl->id.index = spdif_index;
3636 bus->primary_dig_out_type = HDA_PCM_TYPE_HDMI;
3638 if (!bus->primary_dig_out_type)
3639 bus->primary_dig_out_type = type;
3641 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Playback Switch", idx);
3643 codec_err(codec, "too many IEC958 outputs\n");
3646 spdif = snd_array_new(&codec->spdif_out);
3649 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
3650 kctl = snd_ctl_new1(dig_mix, codec);
3653 kctl->id.index = idx;
3654 kctl->private_value = codec->spdif_out.used - 1;
3655 err = snd_hda_ctl_add(codec, associated_nid, kctl);
3659 spdif->nid = cvt_nid;
3660 spdif->ctls = snd_hda_codec_read(codec, cvt_nid, 0,
3661 AC_VERB_GET_DIGI_CONVERT_1, 0);
3662 spdif->status = convert_to_spdif_status(spdif->ctls);
3665 EXPORT_SYMBOL_GPL(snd_hda_create_dig_out_ctls);
3668 * snd_hda_spdif_out_of_nid - get the hda_spdif_out entry from the given NID
3669 * @codec: the HDA codec
3672 * call within spdif_mutex lock
3674 struct hda_spdif_out *snd_hda_spdif_out_of_nid(struct hda_codec *codec,
3678 for (i = 0; i < codec->spdif_out.used; i++) {
3679 struct hda_spdif_out *spdif =
3680 snd_array_elem(&codec->spdif_out, i);
3681 if (spdif->nid == nid)
3686 EXPORT_SYMBOL_GPL(snd_hda_spdif_out_of_nid);
3689 * snd_hda_spdif_ctls_unassign - Unassign the given SPDIF ctl
3690 * @codec: the HDA codec
3691 * @idx: the SPDIF ctl index
3693 * Unassign the widget from the given SPDIF control.
3695 void snd_hda_spdif_ctls_unassign(struct hda_codec *codec, int idx)
3697 struct hda_spdif_out *spdif;
3699 mutex_lock(&codec->spdif_mutex);
3700 spdif = snd_array_elem(&codec->spdif_out, idx);
3701 spdif->nid = (u16)-1;
3702 mutex_unlock(&codec->spdif_mutex);
3704 EXPORT_SYMBOL_GPL(snd_hda_spdif_ctls_unassign);
3707 * snd_hda_spdif_ctls_assign - Assign the SPDIF controls to the given NID
3708 * @codec: the HDA codec
3709 * @idx: the SPDIF ctl idx
3712 * Assign the widget to the SPDIF control with the given index.
3714 void snd_hda_spdif_ctls_assign(struct hda_codec *codec, int idx, hda_nid_t nid)
3716 struct hda_spdif_out *spdif;
3719 mutex_lock(&codec->spdif_mutex);
3720 spdif = snd_array_elem(&codec->spdif_out, idx);
3721 if (spdif->nid != nid) {
3724 set_spdif_ctls(codec, nid, val & 0xff, (val >> 8) & 0xff);
3726 mutex_unlock(&codec->spdif_mutex);
3728 EXPORT_SYMBOL_GPL(snd_hda_spdif_ctls_assign);
3731 * SPDIF sharing with analog output
3733 static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
3734 struct snd_ctl_elem_value *ucontrol)
3736 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3737 ucontrol->value.integer.value[0] = mout->share_spdif;
3741 static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
3742 struct snd_ctl_elem_value *ucontrol)
3744 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3745 mout->share_spdif = !!ucontrol->value.integer.value[0];
3749 static struct snd_kcontrol_new spdif_share_sw = {
3750 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3751 .name = "IEC958 Default PCM Playback Switch",
3752 .info = snd_ctl_boolean_mono_info,
3753 .get = spdif_share_sw_get,
3754 .put = spdif_share_sw_put,
3758 * snd_hda_create_spdif_share_sw - create Default PCM switch
3759 * @codec: the HDA codec
3760 * @mout: multi-out instance
3762 int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
3763 struct hda_multi_out *mout)
3765 struct snd_kcontrol *kctl;
3767 if (!mout->dig_out_nid)
3770 kctl = snd_ctl_new1(&spdif_share_sw, mout);
3773 /* ATTENTION: here mout is passed as private_data, instead of codec */
3774 return snd_hda_ctl_add(codec, mout->dig_out_nid, kctl);
3776 EXPORT_SYMBOL_GPL(snd_hda_create_spdif_share_sw);
3782 #define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
3784 static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
3785 struct snd_ctl_elem_value *ucontrol)
3787 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3789 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
3793 static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
3794 struct snd_ctl_elem_value *ucontrol)
3796 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3797 hda_nid_t nid = kcontrol->private_value;
3798 unsigned int val = !!ucontrol->value.integer.value[0];
3801 mutex_lock(&codec->spdif_mutex);
3802 change = codec->spdif_in_enable != val;
3804 codec->spdif_in_enable = val;
3805 snd_hda_codec_write_cache(codec, nid, 0,
3806 AC_VERB_SET_DIGI_CONVERT_1, val);
3808 mutex_unlock(&codec->spdif_mutex);
3812 static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
3813 struct snd_ctl_elem_value *ucontrol)
3815 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3816 hda_nid_t nid = kcontrol->private_value;
3820 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0);
3821 sbits = convert_to_spdif_status(val);
3822 ucontrol->value.iec958.status[0] = sbits;
3823 ucontrol->value.iec958.status[1] = sbits >> 8;
3824 ucontrol->value.iec958.status[2] = sbits >> 16;
3825 ucontrol->value.iec958.status[3] = sbits >> 24;
3829 static struct snd_kcontrol_new dig_in_ctls[] = {
3831 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3832 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH),
3833 .info = snd_hda_spdif_in_switch_info,
3834 .get = snd_hda_spdif_in_switch_get,
3835 .put = snd_hda_spdif_in_switch_put,
3838 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3839 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3840 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
3841 .info = snd_hda_spdif_mask_info,
3842 .get = snd_hda_spdif_in_status_get,
3848 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
3849 * @codec: the HDA codec
3850 * @nid: audio in widget NID
3852 * Creates controls related with the SPDIF input.
3853 * Called from each patch supporting the SPDIF in.
3855 * Returns 0 if successful, or a negative error code.
3857 int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
3860 struct snd_kcontrol *kctl;
3861 struct snd_kcontrol_new *dig_mix;
3864 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Capture Switch", 0);
3866 codec_err(codec, "too many IEC958 inputs\n");
3869 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
3870 kctl = snd_ctl_new1(dig_mix, codec);
3873 kctl->private_value = nid;
3874 err = snd_hda_ctl_add(codec, nid, kctl);
3878 codec->spdif_in_enable =
3879 snd_hda_codec_read(codec, nid, 0,
3880 AC_VERB_GET_DIGI_CONVERT_1, 0) &
3884 EXPORT_SYMBOL_GPL(snd_hda_create_spdif_in_ctls);
3890 /* build a 31bit cache key with the widget id and the command parameter */
3891 #define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
3892 #define get_cmd_cache_nid(key) ((key) & 0xff)
3893 #define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
3896 * snd_hda_codec_write_cache - send a single command with caching
3897 * @codec: the HDA codec
3898 * @nid: NID to send the command
3899 * @flags: optional bit flags
3900 * @verb: the verb to send
3901 * @parm: the parameter for the verb
3903 * Send a single command without waiting for response.
3905 * Returns 0 if successful, or a negative error code.
3907 int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
3908 int flags, unsigned int verb, unsigned int parm)
3911 struct hda_cache_head *c;
3913 unsigned int cache_only;
3915 cache_only = codec->cached_write;
3917 err = snd_hda_codec_write(codec, nid, flags, verb, parm);
3922 /* parm may contain the verb stuff for get/set amp */
3923 verb = verb | (parm >> 8);
3925 key = build_cmd_cache_key(nid, verb);
3926 mutex_lock(&codec->bus->cmd_mutex);
3927 c = get_alloc_hash(&codec->cmd_cache, key);
3930 c->dirty = cache_only;
3932 mutex_unlock(&codec->bus->cmd_mutex);
3935 EXPORT_SYMBOL_GPL(snd_hda_codec_write_cache);
3938 * snd_hda_codec_update_cache - check cache and write the cmd only when needed
3939 * @codec: the HDA codec
3940 * @nid: NID to send the command
3941 * @flags: optional bit flags
3942 * @verb: the verb to send
3943 * @parm: the parameter for the verb
3945 * This function works like snd_hda_codec_write_cache(), but it doesn't send
3946 * command if the parameter is already identical with the cached value.
3947 * If not, it sends the command and refreshes the cache.
3949 * Returns 0 if successful, or a negative error code.
3951 int snd_hda_codec_update_cache(struct hda_codec *codec, hda_nid_t nid,
3952 int flags, unsigned int verb, unsigned int parm)
3954 struct hda_cache_head *c;
3957 /* parm may contain the verb stuff for get/set amp */
3958 verb = verb | (parm >> 8);
3960 key = build_cmd_cache_key(nid, verb);
3961 mutex_lock(&codec->bus->cmd_mutex);
3962 c = get_hash(&codec->cmd_cache, key);
3963 if (c && c->val == parm) {
3964 mutex_unlock(&codec->bus->cmd_mutex);
3967 mutex_unlock(&codec->bus->cmd_mutex);
3968 return snd_hda_codec_write_cache(codec, nid, flags, verb, parm);
3970 EXPORT_SYMBOL_GPL(snd_hda_codec_update_cache);
3973 * snd_hda_codec_resume_cache - Resume the all commands from the cache
3974 * @codec: HD-audio codec
3976 * Execute all verbs recorded in the command caches to resume.
3978 void snd_hda_codec_resume_cache(struct hda_codec *codec)
3982 mutex_lock(&codec->hash_mutex);
3983 codec->cached_write = 0;
3984 for (i = 0; i < codec->cmd_cache.buf.used; i++) {
3985 struct hda_cache_head *buffer;
3988 buffer = snd_array_elem(&codec->cmd_cache.buf, i);
3995 mutex_unlock(&codec->hash_mutex);
3996 snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
3997 get_cmd_cache_cmd(key), buffer->val);
3998 mutex_lock(&codec->hash_mutex);
4000 mutex_unlock(&codec->hash_mutex);
4002 EXPORT_SYMBOL_GPL(snd_hda_codec_resume_cache);
4005 * snd_hda_sequence_write_cache - sequence writes with caching
4006 * @codec: the HDA codec
4007 * @seq: VERB array to send
4009 * Send the commands sequentially from the given array.
4010 * Thte commands are recorded on cache for power-save and resume.
4011 * The array must be terminated with NID=0.
4013 void snd_hda_sequence_write_cache(struct hda_codec *codec,
4014 const struct hda_verb *seq)
4016 for (; seq->nid; seq++)
4017 snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
4020 EXPORT_SYMBOL_GPL(snd_hda_sequence_write_cache);
4023 * snd_hda_codec_flush_cache - Execute all pending (cached) amps / verbs
4024 * @codec: HD-audio codec
4026 void snd_hda_codec_flush_cache(struct hda_codec *codec)
4028 snd_hda_codec_resume_amp(codec);
4029 snd_hda_codec_resume_cache(codec);
4031 EXPORT_SYMBOL_GPL(snd_hda_codec_flush_cache);
4034 * snd_hda_codec_set_power_to_all - Set the power state to all widgets
4035 * @codec: the HDA codec
4036 * @fg: function group (not used now)
4037 * @power_state: the power state to set (AC_PWRST_*)
4039 * Set the given power state to all widgets that have the power control.
4040 * If the codec has power_filter set, it evaluates the power state and
4041 * filter out if it's unchanged as D3.
4043 void snd_hda_codec_set_power_to_all(struct hda_codec *codec, hda_nid_t fg,
4044 unsigned int power_state)
4046 hda_nid_t nid = codec->start_nid;
4049 for (i = 0; i < codec->num_nodes; i++, nid++) {
4050 unsigned int wcaps = get_wcaps(codec, nid);
4051 unsigned int state = power_state;
4052 if (!(wcaps & AC_WCAP_POWER))
4054 if (codec->power_filter) {
4055 state = codec->power_filter(codec, nid, power_state);
4056 if (state != power_state && power_state == AC_PWRST_D3)
4059 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE,
4063 EXPORT_SYMBOL_GPL(snd_hda_codec_set_power_to_all);
4066 * supported power states check
4068 static bool snd_hda_codec_get_supported_ps(struct hda_codec *codec, hda_nid_t fg,
4069 unsigned int power_state)
4071 int sup = snd_hda_param_read(codec, fg, AC_PAR_POWER_STATE);
4075 if (sup & power_state)
4082 * wait until the state is reached, returns the current state
4084 static unsigned int hda_sync_power_state(struct hda_codec *codec,
4086 unsigned int power_state)
4088 unsigned long end_time = jiffies + msecs_to_jiffies(500);
4089 unsigned int state, actual_state;
4092 state = snd_hda_codec_read(codec, fg, 0,
4093 AC_VERB_GET_POWER_STATE, 0);
4094 if (state & AC_PWRST_ERROR)
4096 actual_state = (state >> 4) & 0x0f;
4097 if (actual_state == power_state)
4099 if (time_after_eq(jiffies, end_time))
4101 /* wait until the codec reachs to the target state */
4108 * snd_hda_codec_eapd_power_filter - A power filter callback for EAPD
4109 * @codec: the HDA codec
4111 * @power_state: power state to evalue
4113 * Don't power down the widget if it controls eapd and EAPD_BTLENABLE is set.
4114 * This can be used a codec power_filter callback.
4116 unsigned int snd_hda_codec_eapd_power_filter(struct hda_codec *codec,
4118 unsigned int power_state)
4120 if (nid == codec->afg || nid == codec->mfg)
4122 if (power_state == AC_PWRST_D3 &&
4123 get_wcaps_type(get_wcaps(codec, nid)) == AC_WID_PIN &&
4124 (snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD)) {
4125 int eapd = snd_hda_codec_read(codec, nid, 0,
4126 AC_VERB_GET_EAPD_BTLENABLE, 0);
4132 EXPORT_SYMBOL_GPL(snd_hda_codec_eapd_power_filter);
4135 * set power state of the codec, and return the power state
4137 static unsigned int hda_set_power_state(struct hda_codec *codec,
4138 unsigned int power_state)
4140 hda_nid_t fg = codec->afg ? codec->afg : codec->mfg;
4145 /* this delay seems necessary to avoid click noise at power-down */
4146 if (power_state == AC_PWRST_D3) {
4147 if (codec->depop_delay < 0)
4148 msleep(codec->epss ? 10 : 100);
4149 else if (codec->depop_delay > 0)
4150 msleep(codec->depop_delay);
4151 flags = HDA_RW_NO_RESPONSE_FALLBACK;
4154 /* repeat power states setting at most 10 times*/
4155 for (count = 0; count < 10; count++) {
4156 if (codec->patch_ops.set_power_state)
4157 codec->patch_ops.set_power_state(codec, fg,
4160 state = power_state;
4161 if (codec->power_filter)
4162 state = codec->power_filter(codec, fg, state);
4163 if (state == power_state || power_state != AC_PWRST_D3)
4164 snd_hda_codec_read(codec, fg, flags,
4165 AC_VERB_SET_POWER_STATE,
4167 snd_hda_codec_set_power_to_all(codec, fg, power_state);
4169 state = hda_sync_power_state(codec, fg, power_state);
4170 if (!(state & AC_PWRST_ERROR))
4177 /* sync power states of all widgets;
4178 * this is called at the end of codec parsing
4180 static void sync_power_up_states(struct hda_codec *codec)
4182 hda_nid_t nid = codec->start_nid;
4185 /* don't care if no filter is used */
4186 if (!codec->power_filter)
4189 for (i = 0; i < codec->num_nodes; i++, nid++) {
4190 unsigned int wcaps = get_wcaps(codec, nid);
4191 unsigned int target;
4192 if (!(wcaps & AC_WCAP_POWER))
4194 target = codec->power_filter(codec, nid, AC_PWRST_D0);
4195 if (target == AC_PWRST_D0)
4197 if (!snd_hda_check_power_state(codec, nid, target))
4198 snd_hda_codec_write(codec, nid, 0,
4199 AC_VERB_SET_POWER_STATE, target);
4203 #ifdef CONFIG_SND_HDA_RECONFIG
4204 /* execute additional init verbs */
4205 static void hda_exec_init_verbs(struct hda_codec *codec)
4207 if (codec->init_verbs.list)
4208 snd_hda_sequence_write(codec, codec->init_verbs.list);
4211 static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
4216 * call suspend and power-down; used both from PM and power-save
4217 * this function returns the power state in the end
4219 static unsigned int hda_call_codec_suspend(struct hda_codec *codec, bool in_wq)
4225 if (codec->patch_ops.suspend)
4226 codec->patch_ops.suspend(codec);
4227 hda_cleanup_all_streams(codec);
4228 state = hda_set_power_state(codec, AC_PWRST_D3);
4229 /* Cancel delayed work if we aren't currently running from it. */
4231 cancel_delayed_work_sync(&codec->power_work);
4232 spin_lock(&codec->power_lock);
4233 snd_hda_update_power_acct(codec);
4234 trace_hda_power_down(codec);
4235 codec->power_on = 0;
4236 codec->power_transition = 0;
4237 codec->power_jiffies = jiffies;
4238 spin_unlock(&codec->power_lock);
4243 /* mark all entries of cmd and amp caches dirty */
4244 static void hda_mark_cmd_cache_dirty(struct hda_codec *codec)
4247 for (i = 0; i < codec->cmd_cache.buf.used; i++) {
4248 struct hda_cache_head *cmd;
4249 cmd = snd_array_elem(&codec->cmd_cache.buf, i);
4252 for (i = 0; i < codec->amp_cache.buf.used; i++) {
4253 struct hda_amp_info *amp;
4254 amp = snd_array_elem(&codec->amp_cache.buf, i);
4255 amp->head.dirty = 1;
4260 * kick up codec; used both from PM and power-save
4262 static void hda_call_codec_resume(struct hda_codec *codec)
4266 hda_mark_cmd_cache_dirty(codec);
4268 /* set as if powered on for avoiding re-entering the resume
4269 * in the resume / power-save sequence
4271 hda_keep_power_on(codec);
4272 hda_set_power_state(codec, AC_PWRST_D0);
4273 restore_shutup_pins(codec);
4274 hda_exec_init_verbs(codec);
4275 snd_hda_jack_set_dirty_all(codec);
4276 if (codec->patch_ops.resume)
4277 codec->patch_ops.resume(codec);
4279 if (codec->patch_ops.init)
4280 codec->patch_ops.init(codec);
4281 snd_hda_codec_resume_amp(codec);
4282 snd_hda_codec_resume_cache(codec);
4285 if (codec->jackpoll_interval)
4286 hda_jackpoll_work(&codec->jackpoll_work.work);
4288 snd_hda_jack_report_sync(codec);
4291 snd_hda_power_down(codec); /* flag down before returning */
4293 #endif /* CONFIG_PM */
4297 * snd_hda_build_controls - build mixer controls
4300 * Creates mixer controls for each codec included in the bus.
4302 * Returns 0 if successful, otherwise a negative error code.
4304 int snd_hda_build_controls(struct hda_bus *bus)
4306 struct hda_codec *codec;
4308 list_for_each_entry(codec, &bus->codec_list, list) {
4309 int err = snd_hda_codec_build_controls(codec);
4312 "cannot build controls for #%d (error %d)\n",
4314 err = snd_hda_codec_reset(codec);
4317 "cannot revert codec\n");
4324 EXPORT_SYMBOL_GPL(snd_hda_build_controls);
4327 * add standard channel maps if not specified
4329 static int add_std_chmaps(struct hda_codec *codec)
4333 for (i = 0; i < codec->num_pcms; i++) {
4334 for (str = 0; str < 2; str++) {
4335 struct snd_pcm *pcm = codec->pcm_info[i].pcm;
4336 struct hda_pcm_stream *hinfo =
4337 &codec->pcm_info[i].stream[str];
4338 struct snd_pcm_chmap *chmap;
4339 const struct snd_pcm_chmap_elem *elem;
4341 if (codec->pcm_info[i].own_chmap)
4343 if (!pcm || !hinfo->substreams)
4345 elem = hinfo->chmap ? hinfo->chmap : snd_pcm_std_chmaps;
4346 err = snd_pcm_add_chmap_ctls(pcm, str, elem,
4347 hinfo->channels_max,
4351 chmap->channel_mask = SND_PCM_CHMAP_MASK_2468;
4357 /* default channel maps for 2.1 speakers;
4358 * since HD-audio supports only stereo, odd number channels are omitted
4360 const struct snd_pcm_chmap_elem snd_pcm_2_1_chmaps[] = {
4362 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } },
4364 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
4365 SNDRV_CHMAP_LFE, SNDRV_CHMAP_LFE } },
4368 EXPORT_SYMBOL_GPL(snd_pcm_2_1_chmaps);
4370 int snd_hda_codec_build_controls(struct hda_codec *codec)
4373 hda_exec_init_verbs(codec);
4374 /* continue to initialize... */
4375 if (codec->patch_ops.init)
4376 err = codec->patch_ops.init(codec);
4377 if (!err && codec->patch_ops.build_controls)
4378 err = codec->patch_ops.build_controls(codec);
4382 /* we create chmaps here instead of build_pcms */
4383 err = add_std_chmaps(codec);
4387 if (codec->jackpoll_interval)
4388 hda_jackpoll_work(&codec->jackpoll_work.work);
4390 snd_hda_jack_report_sync(codec); /* call at the last init point */
4391 sync_power_up_states(codec);
4398 struct hda_rate_tbl {
4400 unsigned int alsa_bits;
4401 unsigned int hda_fmt;
4404 /* rate = base * mult / div */
4405 #define HDA_RATE(base, mult, div) \
4406 (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \
4407 (((div) - 1) << AC_FMT_DIV_SHIFT))
4409 static struct hda_rate_tbl rate_bits[] = {
4410 /* rate in Hz, ALSA rate bitmask, HDA format value */
4412 /* autodetected value used in snd_hda_query_supported_pcm */
4413 { 8000, SNDRV_PCM_RATE_8000, HDA_RATE(48, 1, 6) },
4414 { 11025, SNDRV_PCM_RATE_11025, HDA_RATE(44, 1, 4) },
4415 { 16000, SNDRV_PCM_RATE_16000, HDA_RATE(48, 1, 3) },
4416 { 22050, SNDRV_PCM_RATE_22050, HDA_RATE(44, 1, 2) },
4417 { 32000, SNDRV_PCM_RATE_32000, HDA_RATE(48, 2, 3) },
4418 { 44100, SNDRV_PCM_RATE_44100, HDA_RATE(44, 1, 1) },
4419 { 48000, SNDRV_PCM_RATE_48000, HDA_RATE(48, 1, 1) },
4420 { 88200, SNDRV_PCM_RATE_88200, HDA_RATE(44, 2, 1) },
4421 { 96000, SNDRV_PCM_RATE_96000, HDA_RATE(48, 2, 1) },
4422 { 176400, SNDRV_PCM_RATE_176400, HDA_RATE(44, 4, 1) },
4423 { 192000, SNDRV_PCM_RATE_192000, HDA_RATE(48, 4, 1) },
4424 #define AC_PAR_PCM_RATE_BITS 11
4425 /* up to bits 10, 384kHZ isn't supported properly */
4427 /* not autodetected value */
4428 { 9600, SNDRV_PCM_RATE_KNOT, HDA_RATE(48, 1, 5) },
4430 { 0 } /* terminator */
4434 * snd_hda_calc_stream_format - calculate format bitset
4435 * @codec: HD-audio codec
4436 * @rate: the sample rate
4437 * @channels: the number of channels
4438 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
4439 * @maxbps: the max. bps
4440 * @spdif_ctls: HD-audio SPDIF status bits (0 if irrelevant)
4442 * Calculate the format bitset from the given rate, channels and th PCM format.
4444 * Return zero if invalid.
4446 unsigned int snd_hda_calc_stream_format(struct hda_codec *codec,
4448 unsigned int channels,
4449 unsigned int format,
4450 unsigned int maxbps,
4451 unsigned short spdif_ctls)
4454 unsigned int val = 0;
4456 for (i = 0; rate_bits[i].hz; i++)
4457 if (rate_bits[i].hz == rate) {
4458 val = rate_bits[i].hda_fmt;
4461 if (!rate_bits[i].hz) {
4462 codec_dbg(codec, "invalid rate %d\n", rate);
4466 if (channels == 0 || channels > 8) {
4467 codec_dbg(codec, "invalid channels %d\n", channels);
4470 val |= channels - 1;
4472 switch (snd_pcm_format_width(format)) {
4474 val |= AC_FMT_BITS_8;
4477 val |= AC_FMT_BITS_16;
4482 if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE)
4483 val |= AC_FMT_BITS_32;
4484 else if (maxbps >= 24)
4485 val |= AC_FMT_BITS_24;
4487 val |= AC_FMT_BITS_20;
4490 codec_dbg(codec, "invalid format width %d\n",
4491 snd_pcm_format_width(format));
4495 if (spdif_ctls & AC_DIG1_NONAUDIO)
4496 val |= AC_FMT_TYPE_NON_PCM;
4500 EXPORT_SYMBOL_GPL(snd_hda_calc_stream_format);
4502 static unsigned int get_pcm_param(struct hda_codec *codec, hda_nid_t nid,
4505 unsigned int val = 0;
4506 if (nid != codec->afg &&
4507 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD))
4508 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
4509 if (!val || val == -1)
4510 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
4511 if (!val || val == -1)
4516 static unsigned int query_pcm_param(struct hda_codec *codec, hda_nid_t nid)
4518 return query_caps_hash(codec, nid, 0, HDA_HASH_PARPCM_KEY(nid),
4522 static unsigned int get_stream_param(struct hda_codec *codec, hda_nid_t nid,
4525 unsigned int streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
4526 if (!streams || streams == -1)
4527 streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
4528 if (!streams || streams == -1)
4533 static unsigned int query_stream_param(struct hda_codec *codec, hda_nid_t nid)
4535 return query_caps_hash(codec, nid, 0, HDA_HASH_PARSTR_KEY(nid),
4540 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
4541 * @codec: the HDA codec
4542 * @nid: NID to query
4543 * @ratesp: the pointer to store the detected rate bitflags
4544 * @formatsp: the pointer to store the detected formats
4545 * @bpsp: the pointer to store the detected format widths
4547 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
4548 * or @bsps argument is ignored.
4550 * Returns 0 if successful, otherwise a negative error code.
4552 int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
4553 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
4555 unsigned int i, val, wcaps;
4557 wcaps = get_wcaps(codec, nid);
4558 val = query_pcm_param(codec, nid);
4562 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
4564 rates |= rate_bits[i].alsa_bits;
4568 "rates == 0 (nid=0x%x, val=0x%x, ovrd=%i)\n",
4570 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0);
4576 if (formatsp || bpsp) {
4578 unsigned int streams, bps;
4580 streams = query_stream_param(codec, nid);
4585 if (streams & AC_SUPFMT_PCM) {
4586 if (val & AC_SUPPCM_BITS_8) {
4587 formats |= SNDRV_PCM_FMTBIT_U8;
4590 if (val & AC_SUPPCM_BITS_16) {
4591 formats |= SNDRV_PCM_FMTBIT_S16_LE;
4594 if (wcaps & AC_WCAP_DIGITAL) {
4595 if (val & AC_SUPPCM_BITS_32)
4596 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
4597 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
4598 formats |= SNDRV_PCM_FMTBIT_S32_LE;
4599 if (val & AC_SUPPCM_BITS_24)
4601 else if (val & AC_SUPPCM_BITS_20)
4603 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
4604 AC_SUPPCM_BITS_32)) {
4605 formats |= SNDRV_PCM_FMTBIT_S32_LE;
4606 if (val & AC_SUPPCM_BITS_32)
4608 else if (val & AC_SUPPCM_BITS_24)
4610 else if (val & AC_SUPPCM_BITS_20)
4614 #if 0 /* FIXME: CS4206 doesn't work, which is the only codec supporting float */
4615 if (streams & AC_SUPFMT_FLOAT32) {
4616 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
4621 if (streams == AC_SUPFMT_AC3) {
4622 /* should be exclusive */
4623 /* temporary hack: we have still no proper support
4624 * for the direct AC3 stream...
4626 formats |= SNDRV_PCM_FMTBIT_U8;
4631 "formats == 0 (nid=0x%x, val=0x%x, ovrd=%i, streams=0x%x)\n",
4633 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0,
4638 *formatsp = formats;
4645 EXPORT_SYMBOL_GPL(snd_hda_query_supported_pcm);
4648 * snd_hda_is_supported_format - Check the validity of the format
4649 * @codec: HD-audio codec
4650 * @nid: NID to check
4651 * @format: the HD-audio format value to check
4653 * Check whether the given node supports the format value.
4655 * Returns 1 if supported, 0 if not.
4657 int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
4658 unsigned int format)
4661 unsigned int val = 0, rate, stream;
4663 val = query_pcm_param(codec, nid);
4667 rate = format & 0xff00;
4668 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
4669 if (rate_bits[i].hda_fmt == rate) {
4674 if (i >= AC_PAR_PCM_RATE_BITS)
4677 stream = query_stream_param(codec, nid);
4681 if (stream & AC_SUPFMT_PCM) {
4682 switch (format & 0xf0) {
4684 if (!(val & AC_SUPPCM_BITS_8))
4688 if (!(val & AC_SUPPCM_BITS_16))
4692 if (!(val & AC_SUPPCM_BITS_20))
4696 if (!(val & AC_SUPPCM_BITS_24))
4700 if (!(val & AC_SUPPCM_BITS_32))
4707 /* FIXME: check for float32 and AC3? */
4712 EXPORT_SYMBOL_GPL(snd_hda_is_supported_format);
4717 static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
4718 struct hda_codec *codec,
4719 struct snd_pcm_substream *substream)
4724 static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
4725 struct hda_codec *codec,
4726 unsigned int stream_tag,
4727 unsigned int format,
4728 struct snd_pcm_substream *substream)
4730 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4734 static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
4735 struct hda_codec *codec,
4736 struct snd_pcm_substream *substream)
4738 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
4742 static int set_pcm_default_values(struct hda_codec *codec,
4743 struct hda_pcm_stream *info)
4747 /* query support PCM information from the given NID */
4748 if (info->nid && (!info->rates || !info->formats)) {
4749 err = snd_hda_query_supported_pcm(codec, info->nid,
4750 info->rates ? NULL : &info->rates,
4751 info->formats ? NULL : &info->formats,
4752 info->maxbps ? NULL : &info->maxbps);
4756 if (info->ops.open == NULL)
4757 info->ops.open = hda_pcm_default_open_close;
4758 if (info->ops.close == NULL)
4759 info->ops.close = hda_pcm_default_open_close;
4760 if (info->ops.prepare == NULL) {
4761 if (snd_BUG_ON(!info->nid))
4763 info->ops.prepare = hda_pcm_default_prepare;
4765 if (info->ops.cleanup == NULL) {
4766 if (snd_BUG_ON(!info->nid))
4768 info->ops.cleanup = hda_pcm_default_cleanup;
4774 * codec prepare/cleanup entries
4777 * snd_hda_codec_prepare - Prepare a stream
4778 * @codec: the HDA codec
4779 * @hinfo: PCM information
4780 * @stream: stream tag to assign
4781 * @format: format id to assign
4782 * @substream: PCM substream to assign
4784 * Calls the prepare callback set by the codec with the given arguments.
4785 * Clean up the inactive streams when successful.
4787 int snd_hda_codec_prepare(struct hda_codec *codec,
4788 struct hda_pcm_stream *hinfo,
4789 unsigned int stream,
4790 unsigned int format,
4791 struct snd_pcm_substream *substream)
4794 mutex_lock(&codec->bus->prepare_mutex);
4795 ret = hinfo->ops.prepare(hinfo, codec, stream, format, substream);
4797 purify_inactive_streams(codec);
4798 mutex_unlock(&codec->bus->prepare_mutex);
4801 EXPORT_SYMBOL_GPL(snd_hda_codec_prepare);
4804 * snd_hda_codec_cleanup - Prepare a stream
4805 * @codec: the HDA codec
4806 * @hinfo: PCM information
4807 * @substream: PCM substream
4809 * Calls the cleanup callback set by the codec with the given arguments.
4811 void snd_hda_codec_cleanup(struct hda_codec *codec,
4812 struct hda_pcm_stream *hinfo,
4813 struct snd_pcm_substream *substream)
4815 mutex_lock(&codec->bus->prepare_mutex);
4816 hinfo->ops.cleanup(hinfo, codec, substream);
4817 mutex_unlock(&codec->bus->prepare_mutex);
4819 EXPORT_SYMBOL_GPL(snd_hda_codec_cleanup);
4822 const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = {
4823 "Audio", "SPDIF", "HDMI", "Modem"
4827 * get the empty PCM device number to assign
4829 static int get_empty_pcm_device(struct hda_bus *bus, unsigned int type)
4831 /* audio device indices; not linear to keep compatibility */
4832 /* assigned to static slots up to dev#10; if more needed, assign
4833 * the later slot dynamically (when CONFIG_SND_DYNAMIC_MINORS=y)
4835 static int audio_idx[HDA_PCM_NTYPES][5] = {
4836 [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 },
4837 [HDA_PCM_TYPE_SPDIF] = { 1, -1 },
4838 [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 },
4839 [HDA_PCM_TYPE_MODEM] = { 6, -1 },
4843 if (type >= HDA_PCM_NTYPES) {
4844 dev_err(bus->card->dev, "Invalid PCM type %d\n", type);
4848 for (i = 0; audio_idx[type][i] >= 0; i++) {
4849 #ifndef CONFIG_SND_DYNAMIC_MINORS
4850 if (audio_idx[type][i] >= 8)
4853 if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits))
4854 return audio_idx[type][i];
4857 #ifdef CONFIG_SND_DYNAMIC_MINORS
4858 /* non-fixed slots starting from 10 */
4859 for (i = 10; i < 32; i++) {
4860 if (!test_and_set_bit(i, bus->pcm_dev_bits))
4865 dev_warn(bus->card->dev, "Too many %s devices\n",
4866 snd_hda_pcm_type_name[type]);
4867 #ifndef CONFIG_SND_DYNAMIC_MINORS
4868 dev_warn(bus->card->dev,
4869 "Consider building the kernel with CONFIG_SND_DYNAMIC_MINORS=y\n");
4875 * attach a new PCM stream
4877 static int snd_hda_attach_pcm(struct hda_codec *codec, struct hda_pcm *pcm)
4879 struct hda_bus *bus = codec->bus;
4880 struct hda_pcm_stream *info;
4883 if (snd_BUG_ON(!pcm->name))
4885 for (stream = 0; stream < 2; stream++) {
4886 info = &pcm->stream[stream];
4887 if (info->substreams) {
4888 err = set_pcm_default_values(codec, info);
4893 return bus->ops.attach_pcm(bus, codec, pcm);
4896 /* assign all PCMs of the given codec */
4897 int snd_hda_codec_build_pcms(struct hda_codec *codec)
4902 if (!codec->num_pcms) {
4903 if (!codec->patch_ops.build_pcms)
4905 err = codec->patch_ops.build_pcms(codec);
4908 "cannot build PCMs for #%d (error %d)\n",
4910 err = snd_hda_codec_reset(codec);
4913 "cannot revert codec\n");
4918 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
4919 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
4922 if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
4923 continue; /* no substreams assigned */
4926 dev = get_empty_pcm_device(codec->bus, cpcm->pcm_type);
4928 continue; /* no fatal error */
4930 err = snd_hda_attach_pcm(codec, cpcm);
4933 "cannot attach PCM stream %d for codec #%d\n",
4935 continue; /* no fatal error */
4943 * snd_hda_build_pcms - build PCM information
4946 * Create PCM information for each codec included in the bus.
4948 * The build_pcms codec patch is requested to set up codec->num_pcms and
4949 * codec->pcm_info properly. The array is referred by the top-level driver
4950 * to create its PCM instances.
4951 * The allocated codec->pcm_info should be released in codec->patch_ops.free
4954 * At least, substreams, channels_min and channels_max must be filled for
4955 * each stream. substreams = 0 indicates that the stream doesn't exist.
4956 * When rates and/or formats are zero, the supported values are queried
4957 * from the given nid. The nid is used also by the default ops.prepare
4958 * and ops.cleanup callbacks.
4960 * The driver needs to call ops.open in its open callback. Similarly,
4961 * ops.close is supposed to be called in the close callback.
4962 * ops.prepare should be called in the prepare or hw_params callback
4963 * with the proper parameters for set up.
4964 * ops.cleanup should be called in hw_free for clean up of streams.
4966 * This function returns 0 if successful, or a negative error code.
4968 int snd_hda_build_pcms(struct hda_bus *bus)
4970 struct hda_codec *codec;
4972 list_for_each_entry(codec, &bus->codec_list, list) {
4973 int err = snd_hda_codec_build_pcms(codec);
4979 EXPORT_SYMBOL_GPL(snd_hda_build_pcms);
4982 * snd_hda_add_new_ctls - create controls from the array
4983 * @codec: the HDA codec
4984 * @knew: the array of struct snd_kcontrol_new
4986 * This helper function creates and add new controls in the given array.
4987 * The array must be terminated with an empty entry as terminator.
4989 * Returns 0 if successful, or a negative error code.
4991 int snd_hda_add_new_ctls(struct hda_codec *codec,
4992 const struct snd_kcontrol_new *knew)
4996 for (; knew->name; knew++) {
4997 struct snd_kcontrol *kctl;
4998 int addr = 0, idx = 0;
4999 if (knew->iface == -1) /* skip this codec private value */
5002 kctl = snd_ctl_new1(knew, codec);
5006 kctl->id.device = addr;
5008 kctl->id.index = idx;
5009 err = snd_hda_ctl_add(codec, 0, kctl);
5012 /* try first with another device index corresponding to
5013 * the codec addr; if it still fails (or it's the
5014 * primary codec), then try another control index
5016 if (!addr && codec->addr)
5018 else if (!idx && !knew->index) {
5019 idx = find_empty_mixer_ctl_idx(codec,
5029 EXPORT_SYMBOL_GPL(snd_hda_add_new_ctls);
5032 static void hda_power_work(struct work_struct *work)
5034 struct hda_codec *codec =
5035 container_of(work, struct hda_codec, power_work.work);
5036 struct hda_bus *bus = codec->bus;
5039 spin_lock(&codec->power_lock);
5040 if (codec->power_transition > 0) { /* during power-up sequence? */
5041 spin_unlock(&codec->power_lock);
5044 if (!codec->power_on || codec->power_count) {
5045 codec->power_transition = 0;
5046 spin_unlock(&codec->power_lock);
5049 spin_unlock(&codec->power_lock);
5051 state = hda_call_codec_suspend(codec, true);
5052 if (!bus->power_keep_link_on && (state & AC_PWRST_CLK_STOP_OK))
5053 hda_call_pm_notify(codec, false);
5056 static void hda_keep_power_on(struct hda_codec *codec)
5058 spin_lock(&codec->power_lock);
5059 codec->power_count++;
5060 codec->power_on = 1;
5061 codec->power_jiffies = jiffies;
5062 spin_unlock(&codec->power_lock);
5063 hda_call_pm_notify(codec, true);
5066 /* update the power on/off account with the current jiffies */
5067 void snd_hda_update_power_acct(struct hda_codec *codec)
5069 unsigned long delta = jiffies - codec->power_jiffies;
5070 if (codec->power_on)
5071 codec->power_on_acct += delta;
5073 codec->power_off_acct += delta;
5074 codec->power_jiffies += delta;
5077 /* Transition to powered up, if wait_power_down then wait for a pending
5078 * transition to D3 to complete. A pending D3 transition is indicated
5079 * with power_transition == -1. */
5080 /* call this with codec->power_lock held! */
5081 static void __snd_hda_power_up(struct hda_codec *codec, bool wait_power_down)
5083 /* Return if power_on or transitioning to power_on, unless currently
5085 if ((codec->power_on || codec->power_transition > 0) &&
5086 !(wait_power_down && codec->power_transition < 0))
5088 spin_unlock(&codec->power_lock);
5090 cancel_delayed_work_sync(&codec->power_work);
5092 spin_lock(&codec->power_lock);
5093 /* If the power down delayed work was cancelled above before starting,
5094 * then there is no need to go through power up here.
5096 if (codec->power_on) {
5097 if (codec->power_transition < 0)
5098 codec->power_transition = 0;
5102 trace_hda_power_up(codec);
5103 snd_hda_update_power_acct(codec);
5104 codec->power_on = 1;
5105 codec->power_jiffies = jiffies;
5106 codec->power_transition = 1; /* avoid reentrance */
5107 spin_unlock(&codec->power_lock);
5109 hda_call_codec_resume(codec);
5111 spin_lock(&codec->power_lock);
5112 codec->power_transition = 0;
5115 #define power_save(codec) \
5116 ((codec)->bus->power_save ? *(codec)->bus->power_save : 0)
5118 /* Transition to powered down */
5119 static void __snd_hda_power_down(struct hda_codec *codec)
5121 if (!codec->power_on || codec->power_count || codec->power_transition)
5124 if (power_save(codec)) {
5125 codec->power_transition = -1; /* avoid reentrance */
5126 queue_delayed_work(codec->bus->workq, &codec->power_work,
5127 msecs_to_jiffies(power_save(codec) * 1000));
5132 * snd_hda_power_save - Power-up/down/sync the codec
5133 * @codec: HD-audio codec
5134 * @delta: the counter delta to change
5135 * @d3wait: sync for D3 transition complete
5137 * Change the power-up counter via @delta, and power up or down the hardware
5138 * appropriately. For the power-down, queue to the delayed action.
5139 * Passing zero to @delta means to synchronize the power state.
5141 void snd_hda_power_save(struct hda_codec *codec, int delta, bool d3wait)
5143 spin_lock(&codec->power_lock);
5144 codec->power_count += delta;
5145 trace_hda_power_count(codec);
5147 __snd_hda_power_up(codec, d3wait);
5149 __snd_hda_power_down(codec);
5150 spin_unlock(&codec->power_lock);
5152 EXPORT_SYMBOL_GPL(snd_hda_power_save);
5155 * snd_hda_check_amp_list_power - Check the amp list and update the power
5156 * @codec: HD-audio codec
5157 * @check: the object containing an AMP list and the status
5158 * @nid: NID to check / update
5160 * Check whether the given NID is in the amp list. If it's in the list,
5161 * check the current AMP status, and update the the power-status according
5162 * to the mute status.
5164 * This function is supposed to be set or called from the check_power_status
5167 int snd_hda_check_amp_list_power(struct hda_codec *codec,
5168 struct hda_loopback_check *check,
5171 const struct hda_amp_list *p;
5174 if (!check->amplist)
5176 for (p = check->amplist; p->nid; p++) {
5181 return 0; /* nothing changed */
5183 for (p = check->amplist; p->nid; p++) {
5184 for (ch = 0; ch < 2; ch++) {
5185 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
5187 if (!(v & HDA_AMP_MUTE) && v > 0) {
5188 if (!check->power_on) {
5189 check->power_on = 1;
5190 snd_hda_power_up(codec);
5196 if (check->power_on) {
5197 check->power_on = 0;
5198 snd_hda_power_down(codec);
5202 EXPORT_SYMBOL_GPL(snd_hda_check_amp_list_power);
5206 * Channel mode helper
5210 * snd_hda_ch_mode_info - Info callback helper for the channel mode enum
5211 * @codec: the HDA codec
5212 * @uinfo: pointer to get/store the data
5213 * @chmode: channel mode array
5214 * @num_chmodes: channel mode array size
5216 int snd_hda_ch_mode_info(struct hda_codec *codec,
5217 struct snd_ctl_elem_info *uinfo,
5218 const struct hda_channel_mode *chmode,
5221 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
5223 uinfo->value.enumerated.items = num_chmodes;
5224 if (uinfo->value.enumerated.item >= num_chmodes)
5225 uinfo->value.enumerated.item = num_chmodes - 1;
5226 sprintf(uinfo->value.enumerated.name, "%dch",
5227 chmode[uinfo->value.enumerated.item].channels);
5230 EXPORT_SYMBOL_GPL(snd_hda_ch_mode_info);
5233 * snd_hda_ch_mode_get - Get callback helper for the channel mode enum
5234 * @codec: the HDA codec
5235 * @ucontrol: pointer to get/store the data
5236 * @chmode: channel mode array
5237 * @num_chmodes: channel mode array size
5238 * @max_channels: max number of channels
5240 int snd_hda_ch_mode_get(struct hda_codec *codec,
5241 struct snd_ctl_elem_value *ucontrol,
5242 const struct hda_channel_mode *chmode,
5248 for (i = 0; i < num_chmodes; i++) {
5249 if (max_channels == chmode[i].channels) {
5250 ucontrol->value.enumerated.item[0] = i;
5256 EXPORT_SYMBOL_GPL(snd_hda_ch_mode_get);
5259 * snd_hda_ch_mode_put - Put callback helper for the channel mode enum
5260 * @codec: the HDA codec
5261 * @ucontrol: pointer to get/store the data
5262 * @chmode: channel mode array
5263 * @num_chmodes: channel mode array size
5264 * @max_channelsp: pointer to store the max channels
5266 int snd_hda_ch_mode_put(struct hda_codec *codec,
5267 struct snd_ctl_elem_value *ucontrol,
5268 const struct hda_channel_mode *chmode,
5274 mode = ucontrol->value.enumerated.item[0];
5275 if (mode >= num_chmodes)
5277 if (*max_channelsp == chmode[mode].channels)
5279 /* change the current channel setting */
5280 *max_channelsp = chmode[mode].channels;
5281 if (chmode[mode].sequence)
5282 snd_hda_sequence_write_cache(codec, chmode[mode].sequence);
5285 EXPORT_SYMBOL_GPL(snd_hda_ch_mode_put);
5292 * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum
5293 * @imux: imux helper object
5294 * @uinfo: pointer to get/store the data
5296 int snd_hda_input_mux_info(const struct hda_input_mux *imux,
5297 struct snd_ctl_elem_info *uinfo)
5301 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
5303 uinfo->value.enumerated.items = imux->num_items;
5304 if (!imux->num_items)
5306 index = uinfo->value.enumerated.item;
5307 if (index >= imux->num_items)
5308 index = imux->num_items - 1;
5309 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
5312 EXPORT_SYMBOL_GPL(snd_hda_input_mux_info);
5315 * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum
5316 * @codec: the HDA codec
5317 * @imux: imux helper object
5318 * @ucontrol: pointer to get/store the data
5319 * @nid: input mux NID
5320 * @cur_val: pointer to get/store the current imux value
5322 int snd_hda_input_mux_put(struct hda_codec *codec,
5323 const struct hda_input_mux *imux,
5324 struct snd_ctl_elem_value *ucontrol,
5326 unsigned int *cur_val)
5330 if (!imux->num_items)
5332 idx = ucontrol->value.enumerated.item[0];
5333 if (idx >= imux->num_items)
5334 idx = imux->num_items - 1;
5335 if (*cur_val == idx)
5337 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
5338 imux->items[idx].index);
5342 EXPORT_SYMBOL_GPL(snd_hda_input_mux_put);
5346 * snd_hda_enum_helper_info - Helper for simple enum ctls
5347 * @kcontrol: ctl element
5348 * @uinfo: pointer to get/store the data
5349 * @num_items: number of enum items
5350 * @texts: enum item string array
5352 * process kcontrol info callback of a simple string enum array
5353 * when @num_items is 0 or @texts is NULL, assume a boolean enum array
5355 int snd_hda_enum_helper_info(struct snd_kcontrol *kcontrol,
5356 struct snd_ctl_elem_info *uinfo,
5357 int num_items, const char * const *texts)
5359 static const char * const texts_default[] = {
5360 "Disabled", "Enabled"
5363 if (!texts || !num_items) {
5365 texts = texts_default;
5368 return snd_ctl_enum_info(uinfo, 1, num_items, texts);
5370 EXPORT_SYMBOL_GPL(snd_hda_enum_helper_info);
5373 * Multi-channel / digital-out PCM helper functions
5376 /* setup SPDIF output stream */
5377 static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
5378 unsigned int stream_tag, unsigned int format)
5380 struct hda_spdif_out *spdif;
5381 unsigned int curr_fmt;
5384 spdif = snd_hda_spdif_out_of_nid(codec, nid);
5385 curr_fmt = snd_hda_codec_read(codec, nid, 0,
5386 AC_VERB_GET_STREAM_FORMAT, 0);
5387 reset = codec->spdif_status_reset &&
5388 (spdif->ctls & AC_DIG1_ENABLE) &&
5391 /* turn off SPDIF if needed; otherwise the IEC958 bits won't be
5394 set_dig_out_convert(codec, nid,
5395 spdif->ctls & ~AC_DIG1_ENABLE & 0xff,
5397 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
5398 if (codec->slave_dig_outs) {
5400 for (d = codec->slave_dig_outs; *d; d++)
5401 snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
5404 /* turn on again (if needed) */
5406 set_dig_out_convert(codec, nid,
5407 spdif->ctls & 0xff, -1);
5410 static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
5412 snd_hda_codec_cleanup_stream(codec, nid);
5413 if (codec->slave_dig_outs) {
5415 for (d = codec->slave_dig_outs; *d; d++)
5416 snd_hda_codec_cleanup_stream(codec, *d);
5421 * snd_hda_bus_reboot_notify - call the reboot notifier of each codec
5422 * @bus: HD-audio bus
5424 void snd_hda_bus_reboot_notify(struct hda_bus *bus)
5426 struct hda_codec *codec;
5430 list_for_each_entry(codec, &bus->codec_list, list) {
5431 if (hda_codec_is_power_on(codec) &&
5432 codec->patch_ops.reboot_notify)
5433 codec->patch_ops.reboot_notify(codec);
5436 EXPORT_SYMBOL_GPL(snd_hda_bus_reboot_notify);
5439 * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode
5440 * @codec: the HDA codec
5441 * @mout: hda_multi_out object
5443 int snd_hda_multi_out_dig_open(struct hda_codec *codec,
5444 struct hda_multi_out *mout)
5446 mutex_lock(&codec->spdif_mutex);
5447 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
5448 /* already opened as analog dup; reset it once */
5449 cleanup_dig_out_stream(codec, mout->dig_out_nid);
5450 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
5451 mutex_unlock(&codec->spdif_mutex);
5454 EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_open);
5457 * snd_hda_multi_out_dig_prepare - prepare the digital out stream
5458 * @codec: the HDA codec
5459 * @mout: hda_multi_out object
5460 * @stream_tag: stream tag to assign
5461 * @format: format id to assign
5462 * @substream: PCM substream to assign
5464 int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
5465 struct hda_multi_out *mout,
5466 unsigned int stream_tag,
5467 unsigned int format,
5468 struct snd_pcm_substream *substream)
5470 mutex_lock(&codec->spdif_mutex);
5471 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
5472 mutex_unlock(&codec->spdif_mutex);
5475 EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_prepare);
5478 * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream
5479 * @codec: the HDA codec
5480 * @mout: hda_multi_out object
5482 int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec,
5483 struct hda_multi_out *mout)
5485 mutex_lock(&codec->spdif_mutex);
5486 cleanup_dig_out_stream(codec, mout->dig_out_nid);
5487 mutex_unlock(&codec->spdif_mutex);
5490 EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_cleanup);
5493 * snd_hda_multi_out_dig_close - release the digital out stream
5494 * @codec: the HDA codec
5495 * @mout: hda_multi_out object
5497 int snd_hda_multi_out_dig_close(struct hda_codec *codec,
5498 struct hda_multi_out *mout)
5500 mutex_lock(&codec->spdif_mutex);
5501 mout->dig_out_used = 0;
5502 mutex_unlock(&codec->spdif_mutex);
5505 EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_close);
5508 * snd_hda_multi_out_analog_open - open analog outputs
5509 * @codec: the HDA codec
5510 * @mout: hda_multi_out object
5511 * @substream: PCM substream to assign
5512 * @hinfo: PCM information to assign
5514 * Open analog outputs and set up the hw-constraints.
5515 * If the digital outputs can be opened as slave, open the digital
5518 int snd_hda_multi_out_analog_open(struct hda_codec *codec,
5519 struct hda_multi_out *mout,
5520 struct snd_pcm_substream *substream,
5521 struct hda_pcm_stream *hinfo)
5523 struct snd_pcm_runtime *runtime = substream->runtime;
5524 runtime->hw.channels_max = mout->max_channels;
5525 if (mout->dig_out_nid) {
5526 if (!mout->analog_rates) {
5527 mout->analog_rates = hinfo->rates;
5528 mout->analog_formats = hinfo->formats;
5529 mout->analog_maxbps = hinfo->maxbps;
5531 runtime->hw.rates = mout->analog_rates;
5532 runtime->hw.formats = mout->analog_formats;
5533 hinfo->maxbps = mout->analog_maxbps;
5535 if (!mout->spdif_rates) {
5536 snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
5538 &mout->spdif_formats,
5539 &mout->spdif_maxbps);
5541 mutex_lock(&codec->spdif_mutex);
5542 if (mout->share_spdif) {
5543 if ((runtime->hw.rates & mout->spdif_rates) &&
5544 (runtime->hw.formats & mout->spdif_formats)) {
5545 runtime->hw.rates &= mout->spdif_rates;
5546 runtime->hw.formats &= mout->spdif_formats;
5547 if (mout->spdif_maxbps < hinfo->maxbps)
5548 hinfo->maxbps = mout->spdif_maxbps;
5550 mout->share_spdif = 0;
5551 /* FIXME: need notify? */
5554 mutex_unlock(&codec->spdif_mutex);
5556 return snd_pcm_hw_constraint_step(substream->runtime, 0,
5557 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
5559 EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_open);
5562 * snd_hda_multi_out_analog_prepare - Preapre the analog outputs.
5563 * @codec: the HDA codec
5564 * @mout: hda_multi_out object
5565 * @stream_tag: stream tag to assign
5566 * @format: format id to assign
5567 * @substream: PCM substream to assign
5569 * Set up the i/o for analog out.
5570 * When the digital out is available, copy the front out to digital out, too.
5572 int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
5573 struct hda_multi_out *mout,
5574 unsigned int stream_tag,
5575 unsigned int format,
5576 struct snd_pcm_substream *substream)
5578 const hda_nid_t *nids = mout->dac_nids;
5579 int chs = substream->runtime->channels;
5580 struct hda_spdif_out *spdif;
5583 mutex_lock(&codec->spdif_mutex);
5584 spdif = snd_hda_spdif_out_of_nid(codec, mout->dig_out_nid);
5585 if (mout->dig_out_nid && mout->share_spdif &&
5586 mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
5588 snd_hda_is_supported_format(codec, mout->dig_out_nid,
5590 !(spdif->status & IEC958_AES0_NONAUDIO)) {
5591 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
5592 setup_dig_out_stream(codec, mout->dig_out_nid,
5593 stream_tag, format);
5595 mout->dig_out_used = 0;
5596 cleanup_dig_out_stream(codec, mout->dig_out_nid);
5599 mutex_unlock(&codec->spdif_mutex);
5602 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
5604 if (!mout->no_share_stream &&
5605 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
5606 /* headphone out will just decode front left/right (stereo) */
5607 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
5609 /* extra outputs copied from front */
5610 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
5611 if (!mout->no_share_stream && mout->hp_out_nid[i])
5612 snd_hda_codec_setup_stream(codec,
5613 mout->hp_out_nid[i],
5614 stream_tag, 0, format);
5617 for (i = 1; i < mout->num_dacs; i++) {
5618 if (chs >= (i + 1) * 2) /* independent out */
5619 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
5621 else if (!mout->no_share_stream) /* copy front */
5622 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
5626 /* extra surrounds */
5627 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++) {
5629 if (!mout->extra_out_nid[i])
5631 if (chs >= (i + 1) * 2)
5633 else if (!mout->no_share_stream)
5635 snd_hda_codec_setup_stream(codec, mout->extra_out_nid[i],
5636 stream_tag, ch, format);
5641 EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_prepare);
5644 * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out
5645 * @codec: the HDA codec
5646 * @mout: hda_multi_out object
5648 int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
5649 struct hda_multi_out *mout)
5651 const hda_nid_t *nids = mout->dac_nids;
5654 for (i = 0; i < mout->num_dacs; i++)
5655 snd_hda_codec_cleanup_stream(codec, nids[i]);
5657 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
5658 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
5659 if (mout->hp_out_nid[i])
5660 snd_hda_codec_cleanup_stream(codec,
5661 mout->hp_out_nid[i]);
5662 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
5663 if (mout->extra_out_nid[i])
5664 snd_hda_codec_cleanup_stream(codec,
5665 mout->extra_out_nid[i]);
5666 mutex_lock(&codec->spdif_mutex);
5667 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
5668 cleanup_dig_out_stream(codec, mout->dig_out_nid);
5669 mout->dig_out_used = 0;
5671 mutex_unlock(&codec->spdif_mutex);
5674 EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_cleanup);
5677 * snd_hda_get_default_vref - Get the default (mic) VREF pin bits
5678 * @codec: the HDA codec
5679 * @pin: referred pin NID
5681 * Guess the suitable VREF pin bits to be set as the pin-control value.
5682 * Note: the function doesn't set the AC_PINCTL_IN_EN bit.
5684 unsigned int snd_hda_get_default_vref(struct hda_codec *codec, hda_nid_t pin)
5686 unsigned int pincap;
5687 unsigned int oldval;
5688 oldval = snd_hda_codec_read(codec, pin, 0,
5689 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
5690 pincap = snd_hda_query_pin_caps(codec, pin);
5691 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
5692 /* Exception: if the default pin setup is vref50, we give it priority */
5693 if ((pincap & AC_PINCAP_VREF_80) && oldval != PIN_VREF50)
5694 return AC_PINCTL_VREF_80;
5695 else if (pincap & AC_PINCAP_VREF_50)
5696 return AC_PINCTL_VREF_50;
5697 else if (pincap & AC_PINCAP_VREF_100)
5698 return AC_PINCTL_VREF_100;
5699 else if (pincap & AC_PINCAP_VREF_GRD)
5700 return AC_PINCTL_VREF_GRD;
5701 return AC_PINCTL_VREF_HIZ;
5703 EXPORT_SYMBOL_GPL(snd_hda_get_default_vref);
5706 * snd_hda_correct_pin_ctl - correct the pin ctl value for matching with the pin cap
5707 * @codec: the HDA codec
5708 * @pin: referred pin NID
5709 * @val: pin ctl value to audit
5711 unsigned int snd_hda_correct_pin_ctl(struct hda_codec *codec,
5712 hda_nid_t pin, unsigned int val)
5714 static unsigned int cap_lists[][2] = {
5715 { AC_PINCTL_VREF_100, AC_PINCAP_VREF_100 },
5716 { AC_PINCTL_VREF_80, AC_PINCAP_VREF_80 },
5717 { AC_PINCTL_VREF_50, AC_PINCAP_VREF_50 },
5718 { AC_PINCTL_VREF_GRD, AC_PINCAP_VREF_GRD },
5724 cap = snd_hda_query_pin_caps(codec, pin);
5726 return val; /* don't know what to do... */
5728 if (val & AC_PINCTL_OUT_EN) {
5729 if (!(cap & AC_PINCAP_OUT))
5730 val &= ~(AC_PINCTL_OUT_EN | AC_PINCTL_HP_EN);
5731 else if ((val & AC_PINCTL_HP_EN) && !(cap & AC_PINCAP_HP_DRV))
5732 val &= ~AC_PINCTL_HP_EN;
5735 if (val & AC_PINCTL_IN_EN) {
5736 if (!(cap & AC_PINCAP_IN))
5737 val &= ~(AC_PINCTL_IN_EN | AC_PINCTL_VREFEN);
5739 unsigned int vcap, vref;
5741 vcap = (cap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
5742 vref = val & AC_PINCTL_VREFEN;
5743 for (i = 0; i < ARRAY_SIZE(cap_lists); i++) {
5744 if (vref == cap_lists[i][0] &&
5745 !(vcap & cap_lists[i][1])) {
5746 if (i == ARRAY_SIZE(cap_lists) - 1)
5747 vref = AC_PINCTL_VREF_HIZ;
5749 vref = cap_lists[i + 1][0];
5752 val &= ~AC_PINCTL_VREFEN;
5759 EXPORT_SYMBOL_GPL(snd_hda_correct_pin_ctl);
5762 * _snd_hda_pin_ctl - Helper to set pin ctl value
5763 * @codec: the HDA codec
5764 * @pin: referred pin NID
5765 * @val: pin control value to set
5766 * @cached: access over codec pinctl cache or direct write
5768 * This function is a helper to set a pin ctl value more safely.
5769 * It corrects the pin ctl value via snd_hda_correct_pin_ctl(), stores the
5770 * value in pin target array via snd_hda_codec_set_pin_target(), then
5771 * actually writes the value via either snd_hda_codec_update_cache() or
5772 * snd_hda_codec_write() depending on @cached flag.
5774 int _snd_hda_set_pin_ctl(struct hda_codec *codec, hda_nid_t pin,
5775 unsigned int val, bool cached)
5777 val = snd_hda_correct_pin_ctl(codec, pin, val);
5778 snd_hda_codec_set_pin_target(codec, pin, val);
5780 return snd_hda_codec_update_cache(codec, pin, 0,
5781 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
5783 return snd_hda_codec_write(codec, pin, 0,
5784 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
5786 EXPORT_SYMBOL_GPL(_snd_hda_set_pin_ctl);
5789 * snd_hda_add_imux_item - Add an item to input_mux
5790 * @codec: the HDA codec
5791 * @imux: imux helper object
5792 * @label: the name of imux item to assign
5793 * @index: index number of imux item to assign
5794 * @type_idx: pointer to store the resultant label index
5796 * When the same label is used already in the existing items, the number
5797 * suffix is appended to the label. This label index number is stored
5798 * to type_idx when non-NULL pointer is given.
5800 int snd_hda_add_imux_item(struct hda_codec *codec,
5801 struct hda_input_mux *imux, const char *label,
5802 int index, int *type_idx)
5804 int i, label_idx = 0;
5805 if (imux->num_items >= HDA_MAX_NUM_INPUTS) {
5806 codec_err(codec, "hda_codec: Too many imux items!\n");
5809 for (i = 0; i < imux->num_items; i++) {
5810 if (!strncmp(label, imux->items[i].label, strlen(label)))
5814 *type_idx = label_idx;
5816 snprintf(imux->items[imux->num_items].label,
5817 sizeof(imux->items[imux->num_items].label),
5818 "%s %d", label, label_idx);
5820 strlcpy(imux->items[imux->num_items].label, label,
5821 sizeof(imux->items[imux->num_items].label));
5822 imux->items[imux->num_items].index = index;
5826 EXPORT_SYMBOL_GPL(snd_hda_add_imux_item);
5835 static void hda_async_suspend(void *data, async_cookie_t cookie)
5837 hda_call_codec_suspend(data, false);
5840 static void hda_async_resume(void *data, async_cookie_t cookie)
5842 hda_call_codec_resume(data);
5846 * snd_hda_suspend - suspend the codecs
5849 * Returns 0 if successful.
5851 int snd_hda_suspend(struct hda_bus *bus)
5853 struct hda_codec *codec;
5854 ASYNC_DOMAIN_EXCLUSIVE(domain);
5856 list_for_each_entry(codec, &bus->codec_list, list) {
5857 cancel_delayed_work_sync(&codec->jackpoll_work);
5858 if (hda_codec_is_power_on(codec)) {
5859 if (bus->num_codecs > 1)
5860 async_schedule_domain(hda_async_suspend, codec,
5863 hda_call_codec_suspend(codec, false);
5867 if (bus->num_codecs > 1)
5868 async_synchronize_full_domain(&domain);
5872 EXPORT_SYMBOL_GPL(snd_hda_suspend);
5875 * snd_hda_resume - resume the codecs
5878 * Returns 0 if successful.
5880 int snd_hda_resume(struct hda_bus *bus)
5882 struct hda_codec *codec;
5883 ASYNC_DOMAIN_EXCLUSIVE(domain);
5885 list_for_each_entry(codec, &bus->codec_list, list) {
5886 if (bus->num_codecs > 1)
5887 async_schedule_domain(hda_async_resume, codec, &domain);
5889 hda_call_codec_resume(codec);
5892 if (bus->num_codecs > 1)
5893 async_synchronize_full_domain(&domain);
5897 EXPORT_SYMBOL_GPL(snd_hda_resume);
5898 #endif /* CONFIG_PM */
5905 * snd_array_new - get a new element from the given array
5906 * @array: the array object
5908 * Get a new element from the given array. If it exceeds the
5909 * pre-allocated array size, re-allocate the array.
5911 * Returns NULL if allocation failed.
5913 void *snd_array_new(struct snd_array *array)
5915 if (snd_BUG_ON(!array->elem_size))
5917 if (array->used >= array->alloced) {
5918 int num = array->alloced + array->alloc_align;
5919 int size = (num + 1) * array->elem_size;
5921 if (snd_BUG_ON(num >= 4096))
5923 nlist = krealloc(array->list, size, GFP_KERNEL | __GFP_ZERO);
5926 array->list = nlist;
5927 array->alloced = num;
5929 return snd_array_elem(array, array->used++);
5931 EXPORT_SYMBOL_GPL(snd_array_new);
5934 * snd_array_free - free the given array elements
5935 * @array: the array object
5937 void snd_array_free(struct snd_array *array)
5944 EXPORT_SYMBOL_GPL(snd_array_free);
5947 * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer
5948 * @pcm: PCM caps bits
5949 * @buf: the string buffer to write
5950 * @buflen: the max buffer length
5952 * used by hda_proc.c and hda_eld.c
5954 void snd_print_pcm_bits(int pcm, char *buf, int buflen)
5956 static unsigned int bits[] = { 8, 16, 20, 24, 32 };
5959 for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
5960 if (pcm & (AC_SUPPCM_BITS_8 << i))
5961 j += snprintf(buf + j, buflen - j, " %d", bits[i]);
5963 buf[j] = '\0'; /* necessary when j == 0 */
5965 EXPORT_SYMBOL_GPL(snd_print_pcm_bits);
5967 MODULE_DESCRIPTION("HDA codec core");
5968 MODULE_LICENSE("GPL");