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>
30 #include <linux/pm_runtime.h>
31 #include <sound/core.h>
32 #include "hda_codec.h"
33 #include <sound/asoundef.h>
34 #include <sound/tlv.h>
35 #include <sound/initval.h>
36 #include <sound/jack.h>
37 #include "hda_local.h"
40 #include <sound/hda_hwdep.h>
42 #define CREATE_TRACE_POINTS
43 #include "hda_trace.h"
46 #define codec_in_pm(codec) atomic_read(&(codec)->in_pm)
47 #define hda_codec_is_power_on(codec) \
48 (!pm_runtime_suspended(hda_codec_dev(codec)))
50 #define codec_in_pm(codec) 0
51 #define hda_codec_is_power_on(codec) 1
55 * snd_hda_get_jack_location - Give a location string of the jack
56 * @cfg: pin default config value
58 * Parse the pin default config value and returns the string of the
59 * jack location, e.g. "Rear", "Front", etc.
61 const char *snd_hda_get_jack_location(u32 cfg)
63 static char *bases[7] = {
64 "N/A", "Rear", "Front", "Left", "Right", "Top", "Bottom",
66 static unsigned char specials_idx[] = {
71 static char *specials[] = {
72 "Rear Panel", "Drive Bar",
73 "Riser", "HDMI", "ATAPI",
74 "Mobile-In", "Mobile-Out"
77 cfg = (cfg & AC_DEFCFG_LOCATION) >> AC_DEFCFG_LOCATION_SHIFT;
79 return bases[cfg & 0x0f];
80 for (i = 0; i < ARRAY_SIZE(specials_idx); i++) {
81 if (cfg == specials_idx[i])
86 EXPORT_SYMBOL_GPL(snd_hda_get_jack_location);
89 * snd_hda_get_jack_connectivity - Give a connectivity string of the jack
90 * @cfg: pin default config value
92 * Parse the pin default config value and returns the string of the
93 * jack connectivity, i.e. external or internal connection.
95 const char *snd_hda_get_jack_connectivity(u32 cfg)
97 static char *jack_locations[4] = { "Ext", "Int", "Sep", "Oth" };
99 return jack_locations[(cfg >> (AC_DEFCFG_LOCATION_SHIFT + 4)) & 3];
101 EXPORT_SYMBOL_GPL(snd_hda_get_jack_connectivity);
104 * snd_hda_get_jack_type - Give a type string of the jack
105 * @cfg: pin default config value
107 * Parse the pin default config value and returns the string of the
108 * jack type, i.e. the purpose of the jack, such as Line-Out or CD.
110 const char *snd_hda_get_jack_type(u32 cfg)
112 static char *jack_types[16] = {
113 "Line Out", "Speaker", "HP Out", "CD",
114 "SPDIF Out", "Digital Out", "Modem Line", "Modem Hand",
115 "Line In", "Aux", "Mic", "Telephony",
116 "SPDIF In", "Digital In", "Reserved", "Other"
119 return jack_types[(cfg & AC_DEFCFG_DEVICE)
120 >> AC_DEFCFG_DEVICE_SHIFT];
122 EXPORT_SYMBOL_GPL(snd_hda_get_jack_type);
125 * Compose a 32bit command word to be sent to the HD-audio controller
127 static inline unsigned int
128 make_codec_cmd(struct hda_codec *codec, hda_nid_t nid, int flags,
129 unsigned int verb, unsigned int parm)
133 if ((codec->addr & ~0xf) || (nid & ~0x7f) ||
134 (verb & ~0xfff) || (parm & ~0xffff)) {
135 codec_err(codec, "hda-codec: out of range cmd %x:%x:%x:%x\n",
136 codec->addr, nid, verb, parm);
140 val = (u32)codec->addr << 28;
141 val |= (u32)nid << 20;
148 * Send and receive a verb
150 static int codec_exec_verb(struct hda_codec *codec, unsigned int cmd,
151 int flags, unsigned int *res)
153 struct hda_bus *bus = codec->bus;
162 snd_hda_power_up(codec);
163 mutex_lock(&bus->cmd_mutex);
164 if (flags & HDA_RW_NO_RESPONSE_FALLBACK)
165 bus->no_response_fallback = 1;
167 trace_hda_send_cmd(codec, cmd);
168 err = bus->ops.command(bus, cmd);
171 /* process pending verbs */
172 bus->ops.get_response(bus, codec->addr);
175 *res = bus->ops.get_response(bus, codec->addr);
176 trace_hda_get_response(codec, *res);
178 bus->no_response_fallback = 0;
179 mutex_unlock(&bus->cmd_mutex);
180 snd_hda_power_down(codec);
181 if (!codec_in_pm(codec) && res && *res == -1 && bus->rirb_error) {
182 if (bus->response_reset) {
184 "resetting BUS due to fatal communication error\n");
185 trace_hda_bus_reset(bus);
186 bus->ops.bus_reset(bus);
190 /* clear reset-flag when the communication gets recovered */
191 if (!err || codec_in_pm(codec))
192 bus->response_reset = 0;
197 * snd_hda_codec_read - send a command and get the response
198 * @codec: the HDA codec
199 * @nid: NID to send the command
200 * @flags: optional bit flags
201 * @verb: the verb to send
202 * @parm: the parameter for the verb
204 * Send a single command and read the corresponding response.
206 * Returns the obtained response value, or -1 for an error.
208 unsigned int snd_hda_codec_read(struct hda_codec *codec, hda_nid_t nid,
210 unsigned int verb, unsigned int parm)
212 unsigned cmd = make_codec_cmd(codec, nid, flags, verb, parm);
214 if (codec_exec_verb(codec, cmd, flags, &res))
218 EXPORT_SYMBOL_GPL(snd_hda_codec_read);
221 * snd_hda_codec_write - send a single command without waiting for response
222 * @codec: the HDA codec
223 * @nid: NID to send the command
224 * @flags: optional bit flags
225 * @verb: the verb to send
226 * @parm: the parameter for the verb
228 * Send a single command without waiting for response.
230 * Returns 0 if successful, or a negative error code.
232 int snd_hda_codec_write(struct hda_codec *codec, hda_nid_t nid, int flags,
233 unsigned int verb, unsigned int parm)
235 unsigned int cmd = make_codec_cmd(codec, nid, flags, verb, parm);
237 return codec_exec_verb(codec, cmd, flags,
238 codec->bus->sync_write ? &res : NULL);
240 EXPORT_SYMBOL_GPL(snd_hda_codec_write);
243 * snd_hda_sequence_write - sequence writes
244 * @codec: the HDA codec
245 * @seq: VERB array to send
247 * Send the commands sequentially from the given array.
248 * The array must be terminated with NID=0.
250 void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq)
252 for (; seq->nid; seq++)
253 snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param);
255 EXPORT_SYMBOL_GPL(snd_hda_sequence_write);
258 * snd_hda_get_sub_nodes - get the range of sub nodes
259 * @codec: the HDA codec
261 * @start_id: the pointer to store the start NID
263 * Parse the NID and store the start NID of its sub-nodes.
264 * Returns the number of sub-nodes.
266 int snd_hda_get_sub_nodes(struct hda_codec *codec, hda_nid_t nid,
271 parm = snd_hda_param_read(codec, nid, AC_PAR_NODE_COUNT);
276 *start_id = (parm >> 16) & 0x7fff;
277 return (int)(parm & 0x7fff);
279 EXPORT_SYMBOL_GPL(snd_hda_get_sub_nodes);
281 /* connection list element */
282 struct hda_conn_list {
283 struct list_head list;
289 /* look up the cached results */
290 static struct hda_conn_list *
291 lookup_conn_list(struct hda_codec *codec, hda_nid_t nid)
293 struct hda_conn_list *p;
294 list_for_each_entry(p, &codec->conn_list, list) {
301 static int add_conn_list(struct hda_codec *codec, hda_nid_t nid, int len,
302 const hda_nid_t *list)
304 struct hda_conn_list *p;
306 p = kmalloc(sizeof(*p) + len * sizeof(hda_nid_t), GFP_KERNEL);
311 memcpy(p->conns, list, len * sizeof(hda_nid_t));
312 list_add(&p->list, &codec->conn_list);
316 static void remove_conn_list(struct hda_codec *codec)
318 while (!list_empty(&codec->conn_list)) {
319 struct hda_conn_list *p;
320 p = list_first_entry(&codec->conn_list, typeof(*p), list);
326 /* read the connection and add to the cache */
327 static int read_and_add_raw_conns(struct hda_codec *codec, hda_nid_t nid)
330 hda_nid_t *result = list;
333 len = snd_hda_get_raw_connections(codec, nid, list, ARRAY_SIZE(list));
334 if (len == -ENOSPC) {
335 len = snd_hda_get_num_raw_conns(codec, nid);
336 result = kmalloc(sizeof(hda_nid_t) * len, GFP_KERNEL);
339 len = snd_hda_get_raw_connections(codec, nid, result, len);
342 len = snd_hda_override_conn_list(codec, nid, len, result);
349 * snd_hda_get_conn_list - get connection list
350 * @codec: the HDA codec
352 * @listp: the pointer to store NID list
354 * Parses the connection list of the given widget and stores the pointer
355 * to the list of NIDs.
357 * Returns the number of connections, or a negative error code.
359 * Note that the returned pointer isn't protected against the list
360 * modification. If snd_hda_override_conn_list() might be called
361 * concurrently, protect with a mutex appropriately.
363 int snd_hda_get_conn_list(struct hda_codec *codec, hda_nid_t nid,
364 const hda_nid_t **listp)
370 const struct hda_conn_list *p;
372 /* if the connection-list is already cached, read it */
373 p = lookup_conn_list(codec, nid);
379 if (snd_BUG_ON(added))
382 err = read_and_add_raw_conns(codec, nid);
388 EXPORT_SYMBOL_GPL(snd_hda_get_conn_list);
391 * snd_hda_get_connections - copy connection list
392 * @codec: the HDA codec
394 * @conn_list: connection list array; when NULL, checks only the size
395 * @max_conns: max. number of connections to store
397 * Parses the connection list of the given widget and stores the list
400 * Returns the number of connections, or a negative error code.
402 int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
403 hda_nid_t *conn_list, int max_conns)
405 const hda_nid_t *list;
406 int len = snd_hda_get_conn_list(codec, nid, &list);
408 if (len > 0 && conn_list) {
409 if (len > max_conns) {
410 codec_err(codec, "Too many connections %d for NID 0x%x\n",
414 memcpy(conn_list, list, len * sizeof(hda_nid_t));
419 EXPORT_SYMBOL_GPL(snd_hda_get_connections);
421 /* return CONNLIST_LEN parameter of the given widget */
422 static unsigned int get_num_conns(struct hda_codec *codec, hda_nid_t nid)
424 unsigned int wcaps = get_wcaps(codec, nid);
427 if (!(wcaps & AC_WCAP_CONN_LIST) &&
428 get_wcaps_type(wcaps) != AC_WID_VOL_KNB)
431 parm = snd_hda_param_read(codec, nid, AC_PAR_CONNLIST_LEN);
437 int snd_hda_get_num_raw_conns(struct hda_codec *codec, hda_nid_t nid)
439 return snd_hda_get_raw_connections(codec, nid, NULL, 0);
443 * snd_hda_get_raw_connections - copy connection list without cache
444 * @codec: the HDA codec
446 * @conn_list: connection list array
447 * @max_conns: max. number of connections to store
449 * Like snd_hda_get_connections(), copy the connection list but without
450 * checking through the connection-list cache.
451 * Currently called only from hda_proc.c, so not exported.
453 int snd_hda_get_raw_connections(struct hda_codec *codec, hda_nid_t nid,
454 hda_nid_t *conn_list, int max_conns)
457 int i, conn_len, conns;
458 unsigned int shift, num_elems, mask;
462 parm = get_num_conns(codec, nid);
466 if (parm & AC_CLIST_LONG) {
475 conn_len = parm & AC_CLIST_LENGTH;
476 mask = (1 << (shift-1)) - 1;
479 return 0; /* no connection */
482 /* single connection */
483 parm = snd_hda_codec_read(codec, nid, 0,
484 AC_VERB_GET_CONNECT_LIST, 0);
485 if (parm == -1 && codec->bus->rirb_error)
488 conn_list[0] = parm & mask;
492 /* multi connection */
495 for (i = 0; i < conn_len; i++) {
499 if (i % num_elems == 0) {
500 parm = snd_hda_codec_read(codec, nid, 0,
501 AC_VERB_GET_CONNECT_LIST, i);
502 if (parm == -1 && codec->bus->rirb_error)
505 range_val = !!(parm & (1 << (shift-1))); /* ranges */
507 if (val == 0 && null_count++) { /* no second chance */
509 "invalid CONNECT_LIST verb %x[%i]:%x\n",
515 /* ranges between the previous and this one */
516 if (!prev_nid || prev_nid >= val) {
518 "invalid dep_range_val %x:%x\n",
522 for (n = prev_nid + 1; n <= val; n++) {
524 if (conns >= max_conns)
526 conn_list[conns] = n;
532 if (conns >= max_conns)
534 conn_list[conns] = val;
544 * snd_hda_override_conn_list - add/modify the connection-list to cache
545 * @codec: the HDA codec
547 * @len: number of connection list entries
548 * @list: the list of connection entries
550 * Add or modify the given connection-list to the cache. If the corresponding
551 * cache already exists, invalidate it and append a new one.
553 * Returns zero or a negative error code.
555 int snd_hda_override_conn_list(struct hda_codec *codec, hda_nid_t nid, int len,
556 const hda_nid_t *list)
558 struct hda_conn_list *p;
560 p = lookup_conn_list(codec, nid);
566 return add_conn_list(codec, nid, len, list);
568 EXPORT_SYMBOL_GPL(snd_hda_override_conn_list);
571 * snd_hda_get_conn_index - get the connection index of the given NID
572 * @codec: the HDA codec
573 * @mux: NID containing the list
574 * @nid: NID to select
575 * @recursive: 1 when searching NID recursively, otherwise 0
577 * Parses the connection list of the widget @mux and checks whether the
578 * widget @nid is present. If it is, return the connection index.
579 * Otherwise it returns -1.
581 int snd_hda_get_conn_index(struct hda_codec *codec, hda_nid_t mux,
582 hda_nid_t nid, int recursive)
584 const hda_nid_t *conn;
587 nums = snd_hda_get_conn_list(codec, mux, &conn);
588 for (i = 0; i < nums; i++)
593 if (recursive > 10) {
594 codec_dbg(codec, "too deep connection for 0x%x\n", nid);
598 for (i = 0; i < nums; i++) {
599 unsigned int type = get_wcaps_type(get_wcaps(codec, conn[i]));
600 if (type == AC_WID_PIN || type == AC_WID_AUD_OUT)
602 if (snd_hda_get_conn_index(codec, conn[i], nid, recursive) >= 0)
607 EXPORT_SYMBOL_GPL(snd_hda_get_conn_index);
610 /* return DEVLIST_LEN parameter of the given widget */
611 static unsigned int get_num_devices(struct hda_codec *codec, hda_nid_t nid)
613 unsigned int wcaps = get_wcaps(codec, nid);
616 if (!codec->dp_mst || !(wcaps & AC_WCAP_DIGITAL) ||
617 get_wcaps_type(wcaps) != AC_WID_PIN)
620 parm = snd_hda_param_read(codec, nid, AC_PAR_DEVLIST_LEN);
621 if (parm == -1 && codec->bus->rirb_error)
623 return parm & AC_DEV_LIST_LEN_MASK;
627 * snd_hda_get_devices - copy device list without cache
628 * @codec: the HDA codec
629 * @nid: NID of the pin to parse
630 * @dev_list: device list array
631 * @max_devices: max. number of devices to store
633 * Copy the device list. This info is dynamic and so not cached.
634 * Currently called only from hda_proc.c, so not exported.
636 int snd_hda_get_devices(struct hda_codec *codec, hda_nid_t nid,
637 u8 *dev_list, int max_devices)
640 int i, dev_len, devices;
642 parm = get_num_devices(codec, nid);
643 if (!parm) /* not multi-stream capable */
647 dev_len = dev_len < max_devices ? dev_len : max_devices;
650 while (devices < dev_len) {
651 parm = snd_hda_codec_read(codec, nid, 0,
652 AC_VERB_GET_DEVICE_LIST, devices);
653 if (parm == -1 && codec->bus->rirb_error)
656 for (i = 0; i < 8; i++) {
657 dev_list[devices] = (u8)parm;
660 if (devices >= dev_len)
668 * snd_hda_queue_unsol_event - add an unsolicited event to queue
670 * @res: unsolicited event (lower 32bit of RIRB entry)
671 * @res_ex: codec addr and flags (upper 32bit or RIRB entry)
673 * Adds the given event to the queue. The events are processed in
674 * the workqueue asynchronously. Call this function in the interrupt
675 * hanlder when RIRB receives an unsolicited event.
677 * Returns 0 if successful, or a negative error code.
679 int snd_hda_queue_unsol_event(struct hda_bus *bus, u32 res, u32 res_ex)
681 struct hda_bus_unsolicited *unsol;
684 if (!bus || !bus->workq)
687 trace_hda_unsol_event(bus, res, res_ex);
689 wp = (unsol->wp + 1) % HDA_UNSOL_QUEUE_SIZE;
693 unsol->queue[wp] = res;
694 unsol->queue[wp + 1] = res_ex;
696 queue_work(bus->workq, &unsol->work);
700 EXPORT_SYMBOL_GPL(snd_hda_queue_unsol_event);
703 * process queued unsolicited events
705 static void process_unsol_events(struct work_struct *work)
707 struct hda_bus *bus = container_of(work, struct hda_bus, unsol.work);
708 struct hda_bus_unsolicited *unsol = &bus->unsol;
709 struct hda_codec *codec;
710 unsigned int rp, caddr, res;
712 while (unsol->rp != unsol->wp) {
713 rp = (unsol->rp + 1) % HDA_UNSOL_QUEUE_SIZE;
716 res = unsol->queue[rp];
717 caddr = unsol->queue[rp + 1];
718 if (!(caddr & (1 << 4))) /* no unsolicited event? */
720 codec = bus->caddr_tbl[caddr & 0x0f];
721 if (codec && codec->patch_ops.unsol_event)
722 codec->patch_ops.unsol_event(codec, res);
729 static void snd_hda_bus_free(struct hda_bus *bus)
734 WARN_ON(!list_empty(&bus->codec_list));
736 flush_workqueue(bus->workq);
737 if (bus->ops.private_free)
738 bus->ops.private_free(bus);
740 destroy_workqueue(bus->workq);
745 static int snd_hda_bus_dev_free(struct snd_device *device)
747 snd_hda_bus_free(device->device_data);
751 static int snd_hda_bus_dev_disconnect(struct snd_device *device)
753 struct hda_bus *bus = device->device_data;
759 * snd_hda_bus_new - create a HDA bus
760 * @card: the card entry
761 * @busp: the pointer to store the created bus instance
763 * Returns 0 if successful, or a negative error code.
765 int snd_hda_bus_new(struct snd_card *card,
766 struct hda_bus **busp)
770 static struct snd_device_ops dev_ops = {
771 .dev_disconnect = snd_hda_bus_dev_disconnect,
772 .dev_free = snd_hda_bus_dev_free,
778 bus = kzalloc(sizeof(*bus), GFP_KERNEL);
783 mutex_init(&bus->cmd_mutex);
784 mutex_init(&bus->prepare_mutex);
785 INIT_LIST_HEAD(&bus->codec_list);
786 INIT_WORK(&bus->unsol.work, process_unsol_events);
788 snprintf(bus->workq_name, sizeof(bus->workq_name),
789 "hd-audio%d", card->number);
790 bus->workq = create_singlethread_workqueue(bus->workq_name);
792 dev_err(card->dev, "cannot create workqueue %s\n",
798 err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops);
800 snd_hda_bus_free(bus);
807 EXPORT_SYMBOL_GPL(snd_hda_bus_new);
810 * look for an AFG and MFG nodes
812 static void setup_fg_nodes(struct hda_codec *codec)
814 int i, total_nodes, function_id;
817 total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid);
818 for (i = 0; i < total_nodes; i++, nid++) {
819 function_id = snd_hda_param_read(codec, nid,
820 AC_PAR_FUNCTION_TYPE);
821 switch (function_id & 0xff) {
822 case AC_GRP_AUDIO_FUNCTION:
824 codec->afg_function_id = function_id & 0xff;
825 codec->afg_unsol = (function_id >> 8) & 1;
827 case AC_GRP_MODEM_FUNCTION:
829 codec->mfg_function_id = function_id & 0xff;
830 codec->mfg_unsol = (function_id >> 8) & 1;
839 * read widget caps for each widget and store in cache
841 static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
846 codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node,
848 codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL);
851 nid = codec->start_nid;
852 for (i = 0; i < codec->num_nodes; i++, nid++)
853 codec->wcaps[i] = snd_hda_param_read(codec, nid,
854 AC_PAR_AUDIO_WIDGET_CAP);
858 /* read all pin default configurations and save codec->init_pins */
859 static int read_pin_defaults(struct hda_codec *codec)
862 hda_nid_t nid = codec->start_nid;
864 for (i = 0; i < codec->num_nodes; i++, nid++) {
865 struct hda_pincfg *pin;
866 unsigned int wcaps = get_wcaps(codec, nid);
867 unsigned int wid_type = get_wcaps_type(wcaps);
868 if (wid_type != AC_WID_PIN)
870 pin = snd_array_new(&codec->init_pins);
874 pin->cfg = snd_hda_codec_read(codec, nid, 0,
875 AC_VERB_GET_CONFIG_DEFAULT, 0);
876 pin->ctrl = snd_hda_codec_read(codec, nid, 0,
877 AC_VERB_GET_PIN_WIDGET_CONTROL,
883 /* look up the given pin config list and return the item matching with NID */
884 static struct hda_pincfg *look_up_pincfg(struct hda_codec *codec,
885 struct snd_array *array,
889 for (i = 0; i < array->used; i++) {
890 struct hda_pincfg *pin = snd_array_elem(array, i);
897 /* set the current pin config value for the given NID.
898 * the value is cached, and read via snd_hda_codec_get_pincfg()
900 int snd_hda_add_pincfg(struct hda_codec *codec, struct snd_array *list,
901 hda_nid_t nid, unsigned int cfg)
903 struct hda_pincfg *pin;
905 /* the check below may be invalid when pins are added by a fixup
906 * dynamically (e.g. via snd_hda_codec_update_widgets()), so disabled
910 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN)
914 pin = look_up_pincfg(codec, list, nid);
916 pin = snd_array_new(list);
926 * snd_hda_codec_set_pincfg - Override a pin default configuration
927 * @codec: the HDA codec
928 * @nid: NID to set the pin config
929 * @cfg: the pin default config value
931 * Override a pin default configuration value in the cache.
932 * This value can be read by snd_hda_codec_get_pincfg() in a higher
933 * priority than the real hardware value.
935 int snd_hda_codec_set_pincfg(struct hda_codec *codec,
936 hda_nid_t nid, unsigned int cfg)
938 return snd_hda_add_pincfg(codec, &codec->driver_pins, nid, cfg);
940 EXPORT_SYMBOL_GPL(snd_hda_codec_set_pincfg);
943 * snd_hda_codec_get_pincfg - Obtain a pin-default configuration
944 * @codec: the HDA codec
945 * @nid: NID to get the pin config
947 * Get the current pin config value of the given pin NID.
948 * If the pincfg value is cached or overridden via sysfs or driver,
949 * returns the cached value.
951 unsigned int snd_hda_codec_get_pincfg(struct hda_codec *codec, hda_nid_t nid)
953 struct hda_pincfg *pin;
955 #ifdef CONFIG_SND_HDA_RECONFIG
957 unsigned int cfg = 0;
958 mutex_lock(&codec->user_mutex);
959 pin = look_up_pincfg(codec, &codec->user_pins, nid);
962 mutex_unlock(&codec->user_mutex);
967 pin = look_up_pincfg(codec, &codec->driver_pins, nid);
970 pin = look_up_pincfg(codec, &codec->init_pins, nid);
975 EXPORT_SYMBOL_GPL(snd_hda_codec_get_pincfg);
978 * snd_hda_codec_set_pin_target - remember the current pinctl target value
979 * @codec: the HDA codec
981 * @val: assigned pinctl value
983 * This function stores the given value to a pinctl target value in the
984 * pincfg table. This isn't always as same as the actually written value
985 * but can be referred at any time via snd_hda_codec_get_pin_target().
987 int snd_hda_codec_set_pin_target(struct hda_codec *codec, hda_nid_t nid,
990 struct hda_pincfg *pin;
992 pin = look_up_pincfg(codec, &codec->init_pins, nid);
998 EXPORT_SYMBOL_GPL(snd_hda_codec_set_pin_target);
1001 * snd_hda_codec_get_pin_target - return the current pinctl target value
1002 * @codec: the HDA codec
1005 int snd_hda_codec_get_pin_target(struct hda_codec *codec, hda_nid_t nid)
1007 struct hda_pincfg *pin;
1009 pin = look_up_pincfg(codec, &codec->init_pins, nid);
1014 EXPORT_SYMBOL_GPL(snd_hda_codec_get_pin_target);
1017 * snd_hda_shutup_pins - Shut up all pins
1018 * @codec: the HDA codec
1020 * Clear all pin controls to shup up before suspend for avoiding click noise.
1021 * The controls aren't cached so that they can be resumed properly.
1023 void snd_hda_shutup_pins(struct hda_codec *codec)
1026 /* don't shut up pins when unloading the driver; otherwise it breaks
1027 * the default pin setup at the next load of the driver
1029 if (codec->bus->shutdown)
1031 for (i = 0; i < codec->init_pins.used; i++) {
1032 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1033 /* use read here for syncing after issuing each verb */
1034 snd_hda_codec_read(codec, pin->nid, 0,
1035 AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
1037 codec->pins_shutup = 1;
1039 EXPORT_SYMBOL_GPL(snd_hda_shutup_pins);
1042 /* Restore the pin controls cleared previously via snd_hda_shutup_pins() */
1043 static void restore_shutup_pins(struct hda_codec *codec)
1046 if (!codec->pins_shutup)
1048 if (codec->bus->shutdown)
1050 for (i = 0; i < codec->init_pins.used; i++) {
1051 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1052 snd_hda_codec_write(codec, pin->nid, 0,
1053 AC_VERB_SET_PIN_WIDGET_CONTROL,
1056 codec->pins_shutup = 0;
1060 static void hda_jackpoll_work(struct work_struct *work)
1062 struct hda_codec *codec =
1063 container_of(work, struct hda_codec, jackpoll_work.work);
1065 snd_hda_jack_set_dirty_all(codec);
1066 snd_hda_jack_poll_all(codec);
1068 if (!codec->jackpoll_interval)
1071 queue_delayed_work(codec->bus->workq, &codec->jackpoll_work,
1072 codec->jackpoll_interval);
1075 static void init_hda_cache(struct hda_cache_rec *cache,
1076 unsigned int record_size);
1077 static void free_hda_cache(struct hda_cache_rec *cache);
1079 /* release all pincfg lists */
1080 static void free_init_pincfgs(struct hda_codec *codec)
1082 snd_array_free(&codec->driver_pins);
1083 #ifdef CONFIG_SND_HDA_RECONFIG
1084 snd_array_free(&codec->user_pins);
1086 snd_array_free(&codec->init_pins);
1090 * audio-converter setup caches
1092 struct hda_cvt_setup {
1097 unsigned char active; /* cvt is currently used */
1098 unsigned char dirty; /* setups should be cleared */
1101 /* get or create a cache entry for the given audio converter NID */
1102 static struct hda_cvt_setup *
1103 get_hda_cvt_setup(struct hda_codec *codec, hda_nid_t nid)
1105 struct hda_cvt_setup *p;
1108 for (i = 0; i < codec->cvt_setups.used; i++) {
1109 p = snd_array_elem(&codec->cvt_setups, i);
1113 p = snd_array_new(&codec->cvt_setups);
1122 static void release_pcm(struct kref *kref)
1124 struct hda_pcm *pcm = container_of(kref, struct hda_pcm, kref);
1127 snd_device_free(pcm->codec->card, pcm->pcm);
1128 clear_bit(pcm->device, pcm->codec->bus->pcm_dev_bits);
1133 void snd_hda_codec_pcm_put(struct hda_pcm *pcm)
1135 kref_put(&pcm->kref, release_pcm);
1137 EXPORT_SYMBOL_GPL(snd_hda_codec_pcm_put);
1139 struct hda_pcm *snd_hda_codec_pcm_new(struct hda_codec *codec,
1140 const char *fmt, ...)
1142 struct hda_pcm *pcm;
1145 va_start(args, fmt);
1146 pcm = kzalloc(sizeof(*pcm), GFP_KERNEL);
1151 kref_init(&pcm->kref);
1152 pcm->name = kvasprintf(GFP_KERNEL, fmt, args);
1158 list_add_tail(&pcm->list, &codec->pcm_list_head);
1161 EXPORT_SYMBOL_GPL(snd_hda_codec_pcm_new);
1166 static void codec_release_pcms(struct hda_codec *codec)
1168 struct hda_pcm *pcm, *n;
1170 list_for_each_entry_safe(pcm, n, &codec->pcm_list_head, list) {
1171 list_del_init(&pcm->list);
1173 snd_device_disconnect(codec->card, pcm->pcm);
1174 snd_hda_codec_pcm_put(pcm);
1178 void snd_hda_codec_cleanup_for_unbind(struct hda_codec *codec)
1180 cancel_delayed_work_sync(&codec->jackpoll_work);
1181 flush_workqueue(codec->bus->workq);
1182 if (!codec->in_freeing)
1183 snd_hda_ctls_clear(codec);
1184 codec_release_pcms(codec);
1185 snd_hda_detach_beep_device(codec);
1186 memset(&codec->patch_ops, 0, sizeof(codec->patch_ops));
1187 snd_hda_jack_tbl_clear(codec);
1188 codec->proc_widget_hook = NULL;
1191 free_hda_cache(&codec->amp_cache);
1192 free_hda_cache(&codec->cmd_cache);
1193 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
1194 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
1196 /* free only driver_pins so that init_pins + user_pins are restored */
1197 snd_array_free(&codec->driver_pins);
1198 snd_array_free(&codec->cvt_setups);
1199 snd_array_free(&codec->spdif_out);
1200 snd_array_free(&codec->verbs);
1201 codec->preset = NULL;
1202 codec->slave_dig_outs = NULL;
1203 codec->spdif_status_reset = 0;
1204 snd_array_free(&codec->mixers);
1205 snd_array_free(&codec->nids);
1206 remove_conn_list(codec);
1209 static void snd_hda_codec_free(struct hda_codec *codec)
1213 codec->in_freeing = 1;
1214 if (device_is_registered(hda_codec_dev(codec)))
1215 device_del(hda_codec_dev(codec));
1216 free_init_pincfgs(codec);
1217 flush_workqueue(codec->bus->workq);
1218 list_del(&codec->list);
1219 codec->bus->caddr_tbl[codec->addr] = NULL;
1220 clear_bit(codec->addr, &codec->bus->codec_powered);
1221 snd_hda_sysfs_clear(codec);
1222 free_hda_cache(&codec->amp_cache);
1223 free_hda_cache(&codec->cmd_cache);
1224 kfree(codec->vendor_name);
1225 kfree(codec->chip_name);
1226 kfree(codec->modelname);
1227 kfree(codec->wcaps);
1228 codec->bus->num_codecs--;
1229 put_device(hda_codec_dev(codec));
1232 static bool snd_hda_codec_get_supported_ps(struct hda_codec *codec,
1233 hda_nid_t fg, unsigned int power_state);
1235 static unsigned int hda_set_power_state(struct hda_codec *codec,
1236 unsigned int power_state);
1238 static int snd_hda_codec_dev_register(struct snd_device *device)
1240 struct hda_codec *codec = device->device_data;
1242 snd_hda_register_beep_device(codec);
1243 if (device_is_registered(hda_codec_dev(codec)))
1244 pm_runtime_enable(hda_codec_dev(codec));
1245 /* it was powered up in snd_hda_codec_new(), now all done */
1246 snd_hda_power_down(codec);
1250 static int snd_hda_codec_dev_disconnect(struct snd_device *device)
1252 struct hda_codec *codec = device->device_data;
1254 snd_hda_detach_beep_device(codec);
1258 static int snd_hda_codec_dev_free(struct snd_device *device)
1260 snd_hda_codec_free(device->device_data);
1264 /* just free the container */
1265 static void snd_hda_codec_dev_release(struct device *dev)
1267 kfree(dev_to_hda_codec(dev));
1271 * snd_hda_codec_new - create a HDA codec
1272 * @bus: the bus to assign
1273 * @codec_addr: the codec address
1274 * @codecp: the pointer to store the generated codec
1276 * Returns 0 if successful, or a negative error code.
1278 int snd_hda_codec_new(struct hda_bus *bus, struct snd_card *card,
1279 unsigned int codec_addr, struct hda_codec **codecp)
1281 struct hda_codec *codec;
1286 static struct snd_device_ops dev_ops = {
1287 .dev_register = snd_hda_codec_dev_register,
1288 .dev_disconnect = snd_hda_codec_dev_disconnect,
1289 .dev_free = snd_hda_codec_dev_free,
1292 if (snd_BUG_ON(!bus))
1294 if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS))
1297 if (bus->caddr_tbl[codec_addr]) {
1299 "address 0x%x is already occupied\n",
1304 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
1308 dev = hda_codec_dev(codec);
1309 device_initialize(dev);
1310 dev->parent = card->dev;
1311 dev->bus = &snd_hda_bus_type;
1312 dev->release = snd_hda_codec_dev_release;
1313 dev->groups = snd_hda_dev_attr_groups;
1314 dev_set_name(dev, "hdaudioC%dD%d", card->number, codec_addr);
1315 dev_set_drvdata(dev, codec); /* for sysfs */
1316 device_enable_async_suspend(dev);
1320 codec->addr = codec_addr;
1321 mutex_init(&codec->spdif_mutex);
1322 mutex_init(&codec->control_mutex);
1323 mutex_init(&codec->hash_mutex);
1324 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
1325 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
1326 snd_array_init(&codec->mixers, sizeof(struct hda_nid_item), 32);
1327 snd_array_init(&codec->nids, sizeof(struct hda_nid_item), 32);
1328 snd_array_init(&codec->init_pins, sizeof(struct hda_pincfg), 16);
1329 snd_array_init(&codec->driver_pins, sizeof(struct hda_pincfg), 16);
1330 snd_array_init(&codec->cvt_setups, sizeof(struct hda_cvt_setup), 8);
1331 snd_array_init(&codec->spdif_out, sizeof(struct hda_spdif_out), 16);
1332 snd_array_init(&codec->jacktbl, sizeof(struct hda_jack_tbl), 16);
1333 snd_array_init(&codec->verbs, sizeof(struct hda_verb *), 8);
1334 INIT_LIST_HEAD(&codec->conn_list);
1335 INIT_LIST_HEAD(&codec->pcm_list_head);
1337 INIT_DELAYED_WORK(&codec->jackpoll_work, hda_jackpoll_work);
1338 codec->depop_delay = -1;
1339 codec->fixup_id = HDA_FIXUP_ID_NOT_SET;
1342 /* snd_hda_codec_new() marks the codec as power-up, and leave it as is.
1343 * it's powered down later in snd_hda_codec_dev_register().
1345 set_bit(codec->addr, &bus->codec_powered);
1346 pm_runtime_set_active(hda_codec_dev(codec));
1347 pm_runtime_get_noresume(hda_codec_dev(codec));
1348 codec->power_jiffies = jiffies;
1351 snd_hda_sysfs_init(codec);
1353 if (codec->bus->modelname) {
1354 codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL);
1355 if (!codec->modelname) {
1361 list_add_tail(&codec->list, &bus->codec_list);
1364 bus->caddr_tbl[codec_addr] = codec;
1366 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1368 if (codec->vendor_id == -1)
1369 /* read again, hopefully the access method was corrected
1370 * in the last read...
1372 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1374 codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1375 AC_PAR_SUBSYSTEM_ID);
1376 codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1379 setup_fg_nodes(codec);
1380 if (!codec->afg && !codec->mfg) {
1381 dev_err(card->dev, "no AFG or MFG node found\n");
1386 fg = codec->afg ? codec->afg : codec->mfg;
1387 err = read_widget_caps(codec, fg);
1390 err = read_pin_defaults(codec);
1394 if (!codec->subsystem_id) {
1395 codec->subsystem_id =
1396 snd_hda_codec_read(codec, fg, 0,
1397 AC_VERB_GET_SUBSYSTEM_ID, 0);
1401 codec->d3_stop_clk = snd_hda_codec_get_supported_ps(codec, fg,
1404 codec->epss = snd_hda_codec_get_supported_ps(codec, fg,
1407 /* power-up all before initialization */
1408 hda_set_power_state(codec, AC_PWRST_D0);
1410 snd_hda_codec_proc_new(codec);
1412 snd_hda_create_hwdep(codec);
1414 sprintf(component, "HDA:%08x,%08x,%08x", codec->vendor_id,
1415 codec->subsystem_id, codec->revision_id);
1416 snd_component_add(card, component);
1418 err = snd_device_new(card, SNDRV_DEV_CODEC, codec, &dev_ops);
1427 snd_hda_codec_free(codec);
1430 EXPORT_SYMBOL_GPL(snd_hda_codec_new);
1433 * snd_hda_codec_update_widgets - Refresh widget caps and pin defaults
1434 * @codec: the HDA codec
1436 * Forcibly refresh the all widget caps and the init pin configurations of
1439 int snd_hda_codec_update_widgets(struct hda_codec *codec)
1444 /* Assume the function group node does not change,
1445 * only the widget nodes may change.
1447 kfree(codec->wcaps);
1448 fg = codec->afg ? codec->afg : codec->mfg;
1449 err = read_widget_caps(codec, fg);
1453 snd_array_free(&codec->init_pins);
1454 err = read_pin_defaults(codec);
1458 EXPORT_SYMBOL_GPL(snd_hda_codec_update_widgets);
1460 /* update the stream-id if changed */
1461 static void update_pcm_stream_id(struct hda_codec *codec,
1462 struct hda_cvt_setup *p, hda_nid_t nid,
1463 u32 stream_tag, int channel_id)
1465 unsigned int oldval, newval;
1467 if (p->stream_tag != stream_tag || p->channel_id != channel_id) {
1468 oldval = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0);
1469 newval = (stream_tag << 4) | channel_id;
1470 if (oldval != newval)
1471 snd_hda_codec_write(codec, nid, 0,
1472 AC_VERB_SET_CHANNEL_STREAMID,
1474 p->stream_tag = stream_tag;
1475 p->channel_id = channel_id;
1479 /* update the format-id if changed */
1480 static void update_pcm_format(struct hda_codec *codec, struct hda_cvt_setup *p,
1481 hda_nid_t nid, int format)
1483 unsigned int oldval;
1485 if (p->format_id != format) {
1486 oldval = snd_hda_codec_read(codec, nid, 0,
1487 AC_VERB_GET_STREAM_FORMAT, 0);
1488 if (oldval != format) {
1490 snd_hda_codec_write(codec, nid, 0,
1491 AC_VERB_SET_STREAM_FORMAT,
1494 p->format_id = format;
1499 * snd_hda_codec_setup_stream - set up the codec for streaming
1500 * @codec: the CODEC to set up
1501 * @nid: the NID to set up
1502 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
1503 * @channel_id: channel id to pass, zero based.
1504 * @format: stream format.
1506 void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
1508 int channel_id, int format)
1510 struct hda_codec *c;
1511 struct hda_cvt_setup *p;
1519 "hda_codec_setup_stream: NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
1520 nid, stream_tag, channel_id, format);
1521 p = get_hda_cvt_setup(codec, nid);
1525 if (codec->pcm_format_first)
1526 update_pcm_format(codec, p, nid, format);
1527 update_pcm_stream_id(codec, p, nid, stream_tag, channel_id);
1528 if (!codec->pcm_format_first)
1529 update_pcm_format(codec, p, nid, format);
1534 /* make other inactive cvts with the same stream-tag dirty */
1535 type = get_wcaps_type(get_wcaps(codec, nid));
1536 list_for_each_entry(c, &codec->bus->codec_list, list) {
1537 for (i = 0; i < c->cvt_setups.used; i++) {
1538 p = snd_array_elem(&c->cvt_setups, i);
1539 if (!p->active && p->stream_tag == stream_tag &&
1540 get_wcaps_type(get_wcaps(c, p->nid)) == type)
1545 EXPORT_SYMBOL_GPL(snd_hda_codec_setup_stream);
1547 static void really_cleanup_stream(struct hda_codec *codec,
1548 struct hda_cvt_setup *q);
1551 * __snd_hda_codec_cleanup_stream - clean up the codec for closing
1552 * @codec: the CODEC to clean up
1553 * @nid: the NID to clean up
1554 * @do_now: really clean up the stream instead of clearing the active flag
1556 void __snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid,
1559 struct hda_cvt_setup *p;
1564 if (codec->no_sticky_stream)
1567 codec_dbg(codec, "hda_codec_cleanup_stream: NID=0x%x\n", nid);
1568 p = get_hda_cvt_setup(codec, nid);
1570 /* here we just clear the active flag when do_now isn't set;
1571 * actual clean-ups will be done later in
1572 * purify_inactive_streams() called from snd_hda_codec_prpapre()
1575 really_cleanup_stream(codec, p);
1580 EXPORT_SYMBOL_GPL(__snd_hda_codec_cleanup_stream);
1582 static void really_cleanup_stream(struct hda_codec *codec,
1583 struct hda_cvt_setup *q)
1585 hda_nid_t nid = q->nid;
1586 if (q->stream_tag || q->channel_id)
1587 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
1589 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0
1591 memset(q, 0, sizeof(*q));
1595 /* clean up the all conflicting obsolete streams */
1596 static void purify_inactive_streams(struct hda_codec *codec)
1598 struct hda_codec *c;
1601 list_for_each_entry(c, &codec->bus->codec_list, list) {
1602 for (i = 0; i < c->cvt_setups.used; i++) {
1603 struct hda_cvt_setup *p;
1604 p = snd_array_elem(&c->cvt_setups, i);
1606 really_cleanup_stream(c, p);
1612 /* clean up all streams; called from suspend */
1613 static void hda_cleanup_all_streams(struct hda_codec *codec)
1617 for (i = 0; i < codec->cvt_setups.used; i++) {
1618 struct hda_cvt_setup *p = snd_array_elem(&codec->cvt_setups, i);
1620 really_cleanup_stream(codec, p);
1626 * amp access functions
1629 /* FIXME: more better hash key? */
1630 #define HDA_HASH_KEY(nid, dir, idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
1631 #define HDA_HASH_PINCAP_KEY(nid) (u32)((nid) + (0x02 << 24))
1632 #define HDA_HASH_PARPCM_KEY(nid) (u32)((nid) + (0x03 << 24))
1633 #define HDA_HASH_PARSTR_KEY(nid) (u32)((nid) + (0x04 << 24))
1634 #define INFO_AMP_CAPS (1<<0)
1635 #define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
1637 /* initialize the hash table */
1638 static void init_hda_cache(struct hda_cache_rec *cache,
1639 unsigned int record_size)
1641 memset(cache, 0, sizeof(*cache));
1642 memset(cache->hash, 0xff, sizeof(cache->hash));
1643 snd_array_init(&cache->buf, record_size, 64);
1646 static void free_hda_cache(struct hda_cache_rec *cache)
1648 snd_array_free(&cache->buf);
1651 /* query the hash. allocate an entry if not found. */
1652 static struct hda_cache_head *get_hash(struct hda_cache_rec *cache, u32 key)
1654 u16 idx = key % (u16)ARRAY_SIZE(cache->hash);
1655 u16 cur = cache->hash[idx];
1656 struct hda_cache_head *info;
1658 while (cur != 0xffff) {
1659 info = snd_array_elem(&cache->buf, cur);
1660 if (info->key == key)
1667 /* query the hash. allocate an entry if not found. */
1668 static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache,
1671 struct hda_cache_head *info = get_hash(cache, key);
1674 /* add a new hash entry */
1675 info = snd_array_new(&cache->buf);
1678 cur = snd_array_index(&cache->buf, info);
1682 idx = key % (u16)ARRAY_SIZE(cache->hash);
1683 info->next = cache->hash[idx];
1684 cache->hash[idx] = cur;
1689 /* query and allocate an amp hash entry */
1690 static inline struct hda_amp_info *
1691 get_alloc_amp_hash(struct hda_codec *codec, u32 key)
1693 return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key);
1696 /* overwrite the value with the key in the caps hash */
1697 static int write_caps_hash(struct hda_codec *codec, u32 key, unsigned int val)
1699 struct hda_amp_info *info;
1701 mutex_lock(&codec->hash_mutex);
1702 info = get_alloc_amp_hash(codec, key);
1704 mutex_unlock(&codec->hash_mutex);
1707 info->amp_caps = val;
1708 info->head.val |= INFO_AMP_CAPS;
1709 mutex_unlock(&codec->hash_mutex);
1713 /* query the value from the caps hash; if not found, fetch the current
1714 * value from the given function and store in the hash
1717 query_caps_hash(struct hda_codec *codec, hda_nid_t nid, int dir, u32 key,
1718 unsigned int (*func)(struct hda_codec *, hda_nid_t, int))
1720 struct hda_amp_info *info;
1723 mutex_lock(&codec->hash_mutex);
1724 info = get_alloc_amp_hash(codec, key);
1726 mutex_unlock(&codec->hash_mutex);
1729 if (!(info->head.val & INFO_AMP_CAPS)) {
1730 mutex_unlock(&codec->hash_mutex); /* for reentrance */
1731 val = func(codec, nid, dir);
1732 write_caps_hash(codec, key, val);
1734 val = info->amp_caps;
1735 mutex_unlock(&codec->hash_mutex);
1740 static unsigned int read_amp_cap(struct hda_codec *codec, hda_nid_t nid,
1743 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
1745 return snd_hda_param_read(codec, nid,
1746 direction == HDA_OUTPUT ?
1747 AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP);
1751 * query_amp_caps - query AMP capabilities
1752 * @codec: the HD-auio codec
1753 * @nid: the NID to query
1754 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1756 * Query AMP capabilities for the given widget and direction.
1757 * Returns the obtained capability bits.
1759 * When cap bits have been already read, this doesn't read again but
1760 * returns the cached value.
1762 u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
1764 return query_caps_hash(codec, nid, direction,
1765 HDA_HASH_KEY(nid, direction, 0),
1768 EXPORT_SYMBOL_GPL(query_amp_caps);
1771 * snd_hda_check_amp_caps - query AMP capabilities
1772 * @codec: the HD-audio codec
1773 * @nid: the NID to query
1774 * @dir: either #HDA_INPUT or #HDA_OUTPUT
1775 * @bits: bit mask to check the result
1777 * Check whether the widget has the given amp capability for the direction.
1779 bool snd_hda_check_amp_caps(struct hda_codec *codec, hda_nid_t nid,
1780 int dir, unsigned int bits)
1784 if (get_wcaps(codec, nid) & (1 << (dir + 1)))
1785 if (query_amp_caps(codec, nid, dir) & bits)
1789 EXPORT_SYMBOL_GPL(snd_hda_check_amp_caps);
1792 * snd_hda_override_amp_caps - Override the AMP capabilities
1793 * @codec: the CODEC to clean up
1794 * @nid: the NID to clean up
1795 * @dir: either #HDA_INPUT or #HDA_OUTPUT
1796 * @caps: the capability bits to set
1798 * Override the cached AMP caps bits value by the given one.
1799 * This function is useful if the driver needs to adjust the AMP ranges,
1800 * e.g. limit to 0dB, etc.
1802 * Returns zero if successful or a negative error code.
1804 int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
1807 return write_caps_hash(codec, HDA_HASH_KEY(nid, dir, 0), caps);
1809 EXPORT_SYMBOL_GPL(snd_hda_override_amp_caps);
1811 static unsigned int read_pin_cap(struct hda_codec *codec, hda_nid_t nid,
1814 return snd_hda_param_read(codec, nid, AC_PAR_PIN_CAP);
1818 * snd_hda_query_pin_caps - Query PIN capabilities
1819 * @codec: the HD-auio codec
1820 * @nid: the NID to query
1822 * Query PIN capabilities for the given widget.
1823 * Returns the obtained capability bits.
1825 * When cap bits have been already read, this doesn't read again but
1826 * returns the cached value.
1828 u32 snd_hda_query_pin_caps(struct hda_codec *codec, hda_nid_t nid)
1830 return query_caps_hash(codec, nid, 0, HDA_HASH_PINCAP_KEY(nid),
1833 EXPORT_SYMBOL_GPL(snd_hda_query_pin_caps);
1836 * snd_hda_override_pin_caps - Override the pin capabilities
1838 * @nid: the NID to override
1839 * @caps: the capability bits to set
1841 * Override the cached PIN capabilitiy bits value by the given one.
1843 * Returns zero if successful or a negative error code.
1845 int snd_hda_override_pin_caps(struct hda_codec *codec, hda_nid_t nid,
1848 return write_caps_hash(codec, HDA_HASH_PINCAP_KEY(nid), caps);
1850 EXPORT_SYMBOL_GPL(snd_hda_override_pin_caps);
1852 /* read or sync the hash value with the current value;
1853 * call within hash_mutex
1855 static struct hda_amp_info *
1856 update_amp_hash(struct hda_codec *codec, hda_nid_t nid, int ch,
1857 int direction, int index, bool init_only)
1859 struct hda_amp_info *info;
1860 unsigned int parm, val = 0;
1861 bool val_read = false;
1864 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
1867 if (!(info->head.val & INFO_AMP_VOL(ch))) {
1869 mutex_unlock(&codec->hash_mutex);
1870 parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
1871 parm |= direction == HDA_OUTPUT ?
1872 AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
1874 val = snd_hda_codec_read(codec, nid, 0,
1875 AC_VERB_GET_AMP_GAIN_MUTE, parm);
1878 mutex_lock(&codec->hash_mutex);
1881 info->vol[ch] = val;
1882 info->head.val |= INFO_AMP_VOL(ch);
1883 } else if (init_only)
1889 * write the current volume in info to the h/w
1891 static void put_vol_mute(struct hda_codec *codec, unsigned int amp_caps,
1892 hda_nid_t nid, int ch, int direction, int index,
1897 parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
1898 parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
1899 parm |= index << AC_AMP_SET_INDEX_SHIFT;
1900 if ((val & HDA_AMP_MUTE) && !(amp_caps & AC_AMPCAP_MUTE) &&
1901 (amp_caps & AC_AMPCAP_MIN_MUTE))
1902 ; /* set the zero value as a fake mute */
1905 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
1909 * snd_hda_codec_amp_read - Read AMP value
1910 * @codec: HD-audio codec
1911 * @nid: NID to read the AMP value
1912 * @ch: channel (left=0 or right=1)
1913 * @direction: #HDA_INPUT or #HDA_OUTPUT
1914 * @index: the index value (only for input direction)
1916 * Read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
1918 int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
1919 int direction, int index)
1921 struct hda_amp_info *info;
1922 unsigned int val = 0;
1924 mutex_lock(&codec->hash_mutex);
1925 info = update_amp_hash(codec, nid, ch, direction, index, false);
1927 val = info->vol[ch];
1928 mutex_unlock(&codec->hash_mutex);
1931 EXPORT_SYMBOL_GPL(snd_hda_codec_amp_read);
1933 static int codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
1934 int direction, int idx, int mask, int val,
1935 bool init_only, bool cache_only)
1937 struct hda_amp_info *info;
1940 if (snd_BUG_ON(mask & ~0xff))
1944 mutex_lock(&codec->hash_mutex);
1945 info = update_amp_hash(codec, nid, ch, direction, idx, init_only);
1947 mutex_unlock(&codec->hash_mutex);
1950 val |= info->vol[ch] & ~mask;
1951 if (info->vol[ch] == val) {
1952 mutex_unlock(&codec->hash_mutex);
1955 info->vol[ch] = val;
1956 info->head.dirty |= cache_only;
1957 caps = info->amp_caps;
1958 mutex_unlock(&codec->hash_mutex);
1960 put_vol_mute(codec, caps, nid, ch, direction, idx, val);
1965 * snd_hda_codec_amp_update - update the AMP value
1966 * @codec: HD-audio codec
1967 * @nid: NID to read the AMP value
1968 * @ch: channel (left=0 or right=1)
1969 * @direction: #HDA_INPUT or #HDA_OUTPUT
1970 * @idx: the index value (only for input direction)
1971 * @mask: bit mask to set
1972 * @val: the bits value to set
1974 * Update the AMP value with a bit mask.
1975 * Returns 0 if the value is unchanged, 1 if changed.
1977 int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
1978 int direction, int idx, int mask, int val)
1980 return codec_amp_update(codec, nid, ch, direction, idx, mask, val,
1981 false, codec->cached_write);
1983 EXPORT_SYMBOL_GPL(snd_hda_codec_amp_update);
1986 * snd_hda_codec_amp_stereo - update the AMP stereo values
1987 * @codec: HD-audio codec
1988 * @nid: NID to read the AMP value
1989 * @direction: #HDA_INPUT or #HDA_OUTPUT
1990 * @idx: the index value (only for input direction)
1991 * @mask: bit mask to set
1992 * @val: the bits value to set
1994 * Update the AMP values like snd_hda_codec_amp_update(), but for a
1995 * stereo widget with the same mask and value.
1997 int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
1998 int direction, int idx, int mask, int val)
2002 if (snd_BUG_ON(mask & ~0xff))
2004 for (ch = 0; ch < 2; ch++)
2005 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
2009 EXPORT_SYMBOL_GPL(snd_hda_codec_amp_stereo);
2012 * snd_hda_codec_amp_init - initialize the AMP value
2013 * @codec: the HDA codec
2014 * @nid: NID to read the AMP value
2015 * @ch: channel (left=0 or right=1)
2016 * @dir: #HDA_INPUT or #HDA_OUTPUT
2017 * @idx: the index value (only for input direction)
2018 * @mask: bit mask to set
2019 * @val: the bits value to set
2021 * Works like snd_hda_codec_amp_update() but it writes the value only at
2022 * the first access. If the amp was already initialized / updated beforehand,
2023 * this does nothing.
2025 int snd_hda_codec_amp_init(struct hda_codec *codec, hda_nid_t nid, int ch,
2026 int dir, int idx, int mask, int val)
2028 return codec_amp_update(codec, nid, ch, dir, idx, mask, val, true,
2029 codec->cached_write);
2031 EXPORT_SYMBOL_GPL(snd_hda_codec_amp_init);
2034 * snd_hda_codec_amp_init_stereo - initialize the stereo AMP value
2035 * @codec: the HDA codec
2036 * @nid: NID to read the AMP value
2037 * @dir: #HDA_INPUT or #HDA_OUTPUT
2038 * @idx: the index value (only for input direction)
2039 * @mask: bit mask to set
2040 * @val: the bits value to set
2042 * Call snd_hda_codec_amp_init() for both stereo channels.
2044 int snd_hda_codec_amp_init_stereo(struct hda_codec *codec, hda_nid_t nid,
2045 int dir, int idx, int mask, int val)
2049 if (snd_BUG_ON(mask & ~0xff))
2051 for (ch = 0; ch < 2; ch++)
2052 ret |= snd_hda_codec_amp_init(codec, nid, ch, dir,
2056 EXPORT_SYMBOL_GPL(snd_hda_codec_amp_init_stereo);
2059 * snd_hda_codec_resume_amp - Resume all AMP commands from the cache
2060 * @codec: HD-audio codec
2062 * Resume the all amp commands from the cache.
2064 void snd_hda_codec_resume_amp(struct hda_codec *codec)
2068 mutex_lock(&codec->hash_mutex);
2069 codec->cached_write = 0;
2070 for (i = 0; i < codec->amp_cache.buf.used; i++) {
2071 struct hda_amp_info *buffer;
2074 unsigned int idx, dir, ch;
2075 struct hda_amp_info info;
2077 buffer = snd_array_elem(&codec->amp_cache.buf, i);
2078 if (!buffer->head.dirty)
2080 buffer->head.dirty = 0;
2082 key = info.head.key;
2086 idx = (key >> 16) & 0xff;
2087 dir = (key >> 24) & 0xff;
2088 for (ch = 0; ch < 2; ch++) {
2089 if (!(info.head.val & INFO_AMP_VOL(ch)))
2091 mutex_unlock(&codec->hash_mutex);
2092 put_vol_mute(codec, info.amp_caps, nid, ch, dir, idx,
2094 mutex_lock(&codec->hash_mutex);
2097 mutex_unlock(&codec->hash_mutex);
2099 EXPORT_SYMBOL_GPL(snd_hda_codec_resume_amp);
2101 static u32 get_amp_max_value(struct hda_codec *codec, hda_nid_t nid, int dir,
2104 u32 caps = query_amp_caps(codec, nid, dir);
2106 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
2113 * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer
2114 * @kcontrol: referred ctl element
2115 * @uinfo: pointer to get/store the data
2117 * The control element is supposed to have the private_value field
2118 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2120 int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
2121 struct snd_ctl_elem_info *uinfo)
2123 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2124 u16 nid = get_amp_nid(kcontrol);
2125 u8 chs = get_amp_channels(kcontrol);
2126 int dir = get_amp_direction(kcontrol);
2127 unsigned int ofs = get_amp_offset(kcontrol);
2129 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2130 uinfo->count = chs == 3 ? 2 : 1;
2131 uinfo->value.integer.min = 0;
2132 uinfo->value.integer.max = get_amp_max_value(codec, nid, dir, ofs);
2133 if (!uinfo->value.integer.max) {
2135 "num_steps = 0 for NID=0x%x (ctl = %s)\n",
2136 nid, kcontrol->id.name);
2141 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_info);
2144 static inline unsigned int
2145 read_amp_value(struct hda_codec *codec, hda_nid_t nid,
2146 int ch, int dir, int idx, unsigned int ofs)
2149 val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
2150 val &= HDA_AMP_VOLMASK;
2159 update_amp_value(struct hda_codec *codec, hda_nid_t nid,
2160 int ch, int dir, int idx, unsigned int ofs,
2163 unsigned int maxval;
2167 /* ofs = 0: raw max value */
2168 maxval = get_amp_max_value(codec, nid, dir, 0);
2171 return codec_amp_update(codec, nid, ch, dir, idx, HDA_AMP_VOLMASK, val,
2172 false, !hda_codec_is_power_on(codec));
2176 * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume
2177 * @kcontrol: ctl element
2178 * @ucontrol: pointer to get/store the data
2180 * The control element is supposed to have the private_value field
2181 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2183 int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
2184 struct snd_ctl_elem_value *ucontrol)
2186 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2187 hda_nid_t nid = get_amp_nid(kcontrol);
2188 int chs = get_amp_channels(kcontrol);
2189 int dir = get_amp_direction(kcontrol);
2190 int idx = get_amp_index(kcontrol);
2191 unsigned int ofs = get_amp_offset(kcontrol);
2192 long *valp = ucontrol->value.integer.value;
2195 *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs);
2197 *valp = read_amp_value(codec, nid, 1, dir, idx, ofs);
2200 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_get);
2203 * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume
2204 * @kcontrol: ctl element
2205 * @ucontrol: pointer to get/store the data
2207 * The control element is supposed to have the private_value field
2208 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2210 int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
2211 struct snd_ctl_elem_value *ucontrol)
2213 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2214 hda_nid_t nid = get_amp_nid(kcontrol);
2215 int chs = get_amp_channels(kcontrol);
2216 int dir = get_amp_direction(kcontrol);
2217 int idx = get_amp_index(kcontrol);
2218 unsigned int ofs = get_amp_offset(kcontrol);
2219 long *valp = ucontrol->value.integer.value;
2223 change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp);
2227 change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp);
2230 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_put);
2233 * snd_hda_mixer_amp_volume_put - TLV callback for a standard AMP mixer volume
2234 * @kcontrol: ctl element
2235 * @op_flag: operation flag
2236 * @size: byte size of input TLV
2239 * The control element is supposed to have the private_value field
2240 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2242 int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2243 unsigned int size, unsigned int __user *_tlv)
2245 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2246 hda_nid_t nid = get_amp_nid(kcontrol);
2247 int dir = get_amp_direction(kcontrol);
2248 unsigned int ofs = get_amp_offset(kcontrol);
2249 bool min_mute = get_amp_min_mute(kcontrol);
2250 u32 caps, val1, val2;
2252 if (size < 4 * sizeof(unsigned int))
2254 caps = query_amp_caps(codec, nid, dir);
2255 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2256 val2 = (val2 + 1) * 25;
2257 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
2259 val1 = ((int)val1) * ((int)val2);
2260 if (min_mute || (caps & AC_AMPCAP_MIN_MUTE))
2261 val2 |= TLV_DB_SCALE_MUTE;
2262 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
2264 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
2266 if (put_user(val1, _tlv + 2))
2268 if (put_user(val2, _tlv + 3))
2272 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_tlv);
2275 * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control
2276 * @codec: HD-audio codec
2277 * @nid: NID of a reference widget
2278 * @dir: #HDA_INPUT or #HDA_OUTPUT
2279 * @tlv: TLV data to be stored, at least 4 elements
2281 * Set (static) TLV data for a virtual master volume using the AMP caps
2282 * obtained from the reference NID.
2283 * The volume range is recalculated as if the max volume is 0dB.
2285 void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
2291 caps = query_amp_caps(codec, nid, dir);
2292 nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
2293 step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2294 step = (step + 1) * 25;
2295 tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
2296 tlv[1] = 2 * sizeof(unsigned int);
2297 tlv[2] = -nums * step;
2300 EXPORT_SYMBOL_GPL(snd_hda_set_vmaster_tlv);
2302 /* find a mixer control element with the given name */
2303 static struct snd_kcontrol *
2304 find_mixer_ctl(struct hda_codec *codec, const char *name, int dev, int idx)
2306 struct snd_ctl_elem_id id;
2307 memset(&id, 0, sizeof(id));
2308 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
2311 if (snd_BUG_ON(strlen(name) >= sizeof(id.name)))
2313 strcpy(id.name, name);
2314 return snd_ctl_find_id(codec->card, &id);
2318 * snd_hda_find_mixer_ctl - Find a mixer control element with the given name
2319 * @codec: HD-audio codec
2320 * @name: ctl id name string
2322 * Get the control element with the given id string and IFACE_MIXER.
2324 struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
2327 return find_mixer_ctl(codec, name, 0, 0);
2329 EXPORT_SYMBOL_GPL(snd_hda_find_mixer_ctl);
2331 static int find_empty_mixer_ctl_idx(struct hda_codec *codec, const char *name,
2335 /* 16 ctlrs should be large enough */
2336 for (i = 0, idx = start_idx; i < 16; i++, idx++) {
2337 if (!find_mixer_ctl(codec, name, 0, idx))
2344 * snd_hda_ctl_add - Add a control element and assign to the codec
2345 * @codec: HD-audio codec
2346 * @nid: corresponding NID (optional)
2347 * @kctl: the control element to assign
2349 * Add the given control element to an array inside the codec instance.
2350 * All control elements belonging to a codec are supposed to be added
2351 * by this function so that a proper clean-up works at the free or
2352 * reconfiguration time.
2354 * If non-zero @nid is passed, the NID is assigned to the control element.
2355 * The assignment is shown in the codec proc file.
2357 * snd_hda_ctl_add() checks the control subdev id field whether
2358 * #HDA_SUBDEV_NID_FLAG bit is set. If set (and @nid is zero), the lower
2359 * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit
2360 * specifies if kctl->private_value is a HDA amplifier value.
2362 int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid,
2363 struct snd_kcontrol *kctl)
2366 unsigned short flags = 0;
2367 struct hda_nid_item *item;
2369 if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) {
2370 flags |= HDA_NID_ITEM_AMP;
2372 nid = get_amp_nid_(kctl->private_value);
2374 if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0)
2375 nid = kctl->id.subdevice & 0xffff;
2376 if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG))
2377 kctl->id.subdevice = 0;
2378 err = snd_ctl_add(codec->card, kctl);
2381 item = snd_array_new(&codec->mixers);
2386 item->flags = flags;
2389 EXPORT_SYMBOL_GPL(snd_hda_ctl_add);
2392 * snd_hda_add_nid - Assign a NID to a control element
2393 * @codec: HD-audio codec
2394 * @nid: corresponding NID (optional)
2395 * @kctl: the control element to assign
2396 * @index: index to kctl
2398 * Add the given control element to an array inside the codec instance.
2399 * This function is used when #snd_hda_ctl_add cannot be used for 1:1
2400 * NID:KCTL mapping - for example "Capture Source" selector.
2402 int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl,
2403 unsigned int index, hda_nid_t nid)
2405 struct hda_nid_item *item;
2408 item = snd_array_new(&codec->nids);
2412 item->index = index;
2416 codec_err(codec, "no NID for mapping control %s:%d:%d\n",
2417 kctl->id.name, kctl->id.index, index);
2420 EXPORT_SYMBOL_GPL(snd_hda_add_nid);
2423 * snd_hda_ctls_clear - Clear all controls assigned to the given codec
2424 * @codec: HD-audio codec
2426 void snd_hda_ctls_clear(struct hda_codec *codec)
2429 struct hda_nid_item *items = codec->mixers.list;
2430 for (i = 0; i < codec->mixers.used; i++)
2431 snd_ctl_remove(codec->card, items[i].kctl);
2432 snd_array_free(&codec->mixers);
2433 snd_array_free(&codec->nids);
2437 * snd_hda_lock_devices - pseudo device locking
2440 * toggle card->shutdown to allow/disallow the device access (as a hack)
2442 int snd_hda_lock_devices(struct hda_bus *bus)
2444 struct snd_card *card = bus->card;
2445 struct hda_codec *codec;
2447 spin_lock(&card->files_lock);
2451 if (!list_empty(&card->ctl_files))
2454 list_for_each_entry(codec, &bus->codec_list, list) {
2455 struct hda_pcm *cpcm;
2456 list_for_each_entry(cpcm, &codec->pcm_list_head, list) {
2459 if (cpcm->pcm->streams[0].substream_opened ||
2460 cpcm->pcm->streams[1].substream_opened)
2464 spin_unlock(&card->files_lock);
2470 spin_unlock(&card->files_lock);
2473 EXPORT_SYMBOL_GPL(snd_hda_lock_devices);
2476 * snd_hda_unlock_devices - pseudo device unlocking
2479 void snd_hda_unlock_devices(struct hda_bus *bus)
2481 struct snd_card *card = bus->card;
2484 spin_lock(&card->files_lock);
2486 spin_unlock(&card->files_lock);
2488 EXPORT_SYMBOL_GPL(snd_hda_unlock_devices);
2491 * snd_hda_codec_reset - Clear all objects assigned to the codec
2492 * @codec: HD-audio codec
2494 * This frees the all PCM and control elements assigned to the codec, and
2495 * clears the caches and restores the pin default configurations.
2497 * When a device is being used, it returns -EBSY. If successfully freed,
2500 int snd_hda_codec_reset(struct hda_codec *codec)
2502 struct hda_bus *bus = codec->bus;
2504 if (snd_hda_lock_devices(bus) < 0)
2507 /* OK, let it free */
2508 if (device_is_registered(hda_codec_dev(codec)))
2509 device_del(hda_codec_dev(codec));
2511 /* allow device access again */
2512 snd_hda_unlock_devices(bus);
2516 typedef int (*map_slave_func_t)(struct hda_codec *, void *, struct snd_kcontrol *);
2518 /* apply the function to all matching slave ctls in the mixer list */
2519 static int map_slaves(struct hda_codec *codec, const char * const *slaves,
2520 const char *suffix, map_slave_func_t func, void *data)
2522 struct hda_nid_item *items;
2523 const char * const *s;
2526 items = codec->mixers.list;
2527 for (i = 0; i < codec->mixers.used; i++) {
2528 struct snd_kcontrol *sctl = items[i].kctl;
2529 if (!sctl || sctl->id.iface != SNDRV_CTL_ELEM_IFACE_MIXER)
2531 for (s = slaves; *s; s++) {
2532 char tmpname[sizeof(sctl->id.name)];
2533 const char *name = *s;
2535 snprintf(tmpname, sizeof(tmpname), "%s %s",
2539 if (!strcmp(sctl->id.name, name)) {
2540 err = func(codec, data, sctl);
2550 static int check_slave_present(struct hda_codec *codec,
2551 void *data, struct snd_kcontrol *sctl)
2556 /* guess the value corresponding to 0dB */
2557 static int get_kctl_0dB_offset(struct hda_codec *codec,
2558 struct snd_kcontrol *kctl, int *step_to_check)
2561 const int *tlv = NULL;
2564 if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
2565 /* FIXME: set_fs() hack for obtaining user-space TLV data */
2566 mm_segment_t fs = get_fs();
2568 if (!kctl->tlv.c(kctl, 0, sizeof(_tlv), _tlv))
2571 } else if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_READ)
2573 if (tlv && tlv[0] == SNDRV_CTL_TLVT_DB_SCALE) {
2575 step &= ~TLV_DB_SCALE_MUTE;
2578 if (*step_to_check && *step_to_check != step) {
2579 codec_err(codec, "Mismatching dB step for vmaster slave (%d!=%d)\n",
2580 - *step_to_check, step);
2583 *step_to_check = step;
2584 val = -tlv[2] / step;
2589 /* call kctl->put with the given value(s) */
2590 static int put_kctl_with_value(struct snd_kcontrol *kctl, int val)
2592 struct snd_ctl_elem_value *ucontrol;
2593 ucontrol = kzalloc(sizeof(*ucontrol), GFP_KERNEL);
2596 ucontrol->value.integer.value[0] = val;
2597 ucontrol->value.integer.value[1] = val;
2598 kctl->put(kctl, ucontrol);
2603 /* initialize the slave volume with 0dB */
2604 static int init_slave_0dB(struct hda_codec *codec,
2605 void *data, struct snd_kcontrol *slave)
2607 int offset = get_kctl_0dB_offset(codec, slave, data);
2609 put_kctl_with_value(slave, offset);
2613 /* unmute the slave */
2614 static int init_slave_unmute(struct hda_codec *codec,
2615 void *data, struct snd_kcontrol *slave)
2617 return put_kctl_with_value(slave, 1);
2620 static int add_slave(struct hda_codec *codec,
2621 void *data, struct snd_kcontrol *slave)
2623 return snd_ctl_add_slave(data, slave);
2627 * __snd_hda_add_vmaster - create a virtual master control and add slaves
2628 * @codec: HD-audio codec
2629 * @name: vmaster control name
2630 * @tlv: TLV data (optional)
2631 * @slaves: slave control names (optional)
2632 * @suffix: suffix string to each slave name (optional)
2633 * @init_slave_vol: initialize slaves to unmute/0dB
2634 * @ctl_ret: store the vmaster kcontrol in return
2636 * Create a virtual master control with the given name. The TLV data
2637 * must be either NULL or a valid data.
2639 * @slaves is a NULL-terminated array of strings, each of which is a
2640 * slave control name. All controls with these names are assigned to
2641 * the new virtual master control.
2643 * This function returns zero if successful or a negative error code.
2645 int __snd_hda_add_vmaster(struct hda_codec *codec, char *name,
2646 unsigned int *tlv, const char * const *slaves,
2647 const char *suffix, bool init_slave_vol,
2648 struct snd_kcontrol **ctl_ret)
2650 struct snd_kcontrol *kctl;
2656 err = map_slaves(codec, slaves, suffix, check_slave_present, NULL);
2658 codec_dbg(codec, "No slave found for %s\n", name);
2661 kctl = snd_ctl_make_virtual_master(name, tlv);
2664 err = snd_hda_ctl_add(codec, 0, kctl);
2668 err = map_slaves(codec, slaves, suffix, add_slave, kctl);
2672 /* init with master mute & zero volume */
2673 put_kctl_with_value(kctl, 0);
2674 if (init_slave_vol) {
2676 map_slaves(codec, slaves, suffix,
2677 tlv ? init_slave_0dB : init_slave_unmute, &step);
2684 EXPORT_SYMBOL_GPL(__snd_hda_add_vmaster);
2687 * mute-LED control using vmaster
2689 static int vmaster_mute_mode_info(struct snd_kcontrol *kcontrol,
2690 struct snd_ctl_elem_info *uinfo)
2692 static const char * const texts[] = {
2693 "On", "Off", "Follow Master"
2696 return snd_ctl_enum_info(uinfo, 1, 3, texts);
2699 static int vmaster_mute_mode_get(struct snd_kcontrol *kcontrol,
2700 struct snd_ctl_elem_value *ucontrol)
2702 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2703 ucontrol->value.enumerated.item[0] = hook->mute_mode;
2707 static int vmaster_mute_mode_put(struct snd_kcontrol *kcontrol,
2708 struct snd_ctl_elem_value *ucontrol)
2710 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2711 unsigned int old_mode = hook->mute_mode;
2713 hook->mute_mode = ucontrol->value.enumerated.item[0];
2714 if (hook->mute_mode > HDA_VMUTE_FOLLOW_MASTER)
2715 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
2716 if (old_mode == hook->mute_mode)
2718 snd_hda_sync_vmaster_hook(hook);
2722 static struct snd_kcontrol_new vmaster_mute_mode = {
2723 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2724 .name = "Mute-LED Mode",
2725 .info = vmaster_mute_mode_info,
2726 .get = vmaster_mute_mode_get,
2727 .put = vmaster_mute_mode_put,
2731 * snd_hda_add_vmaster_hook - Add a vmaster hook for mute-LED
2732 * @codec: the HDA codec
2733 * @hook: the vmaster hook object
2734 * @expose_enum_ctl: flag to create an enum ctl
2736 * Add a mute-LED hook with the given vmaster switch kctl.
2737 * When @expose_enum_ctl is set, "Mute-LED Mode" control is automatically
2738 * created and associated with the given hook.
2740 int snd_hda_add_vmaster_hook(struct hda_codec *codec,
2741 struct hda_vmaster_mute_hook *hook,
2742 bool expose_enum_ctl)
2744 struct snd_kcontrol *kctl;
2746 if (!hook->hook || !hook->sw_kctl)
2748 snd_ctl_add_vmaster_hook(hook->sw_kctl, hook->hook, codec);
2749 hook->codec = codec;
2750 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
2751 if (!expose_enum_ctl)
2753 kctl = snd_ctl_new1(&vmaster_mute_mode, hook);
2756 return snd_hda_ctl_add(codec, 0, kctl);
2758 EXPORT_SYMBOL_GPL(snd_hda_add_vmaster_hook);
2761 * snd_hda_sync_vmaster_hook - Sync vmaster hook
2762 * @hook: the vmaster hook
2764 * Call the hook with the current value for synchronization.
2765 * Should be called in init callback.
2767 void snd_hda_sync_vmaster_hook(struct hda_vmaster_mute_hook *hook)
2769 if (!hook->hook || !hook->codec)
2771 /* don't call vmaster hook in the destructor since it might have
2772 * been already destroyed
2774 if (hook->codec->bus->shutdown)
2776 switch (hook->mute_mode) {
2777 case HDA_VMUTE_FOLLOW_MASTER:
2778 snd_ctl_sync_vmaster_hook(hook->sw_kctl);
2781 hook->hook(hook->codec, hook->mute_mode);
2785 EXPORT_SYMBOL_GPL(snd_hda_sync_vmaster_hook);
2789 * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch
2790 * @kcontrol: referred ctl element
2791 * @uinfo: pointer to get/store the data
2793 * The control element is supposed to have the private_value field
2794 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2796 int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
2797 struct snd_ctl_elem_info *uinfo)
2799 int chs = get_amp_channels(kcontrol);
2801 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2802 uinfo->count = chs == 3 ? 2 : 1;
2803 uinfo->value.integer.min = 0;
2804 uinfo->value.integer.max = 1;
2807 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_info);
2810 * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch
2811 * @kcontrol: ctl element
2812 * @ucontrol: pointer to get/store the data
2814 * The control element is supposed to have the private_value field
2815 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2817 int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
2818 struct snd_ctl_elem_value *ucontrol)
2820 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2821 hda_nid_t nid = get_amp_nid(kcontrol);
2822 int chs = get_amp_channels(kcontrol);
2823 int dir = get_amp_direction(kcontrol);
2824 int idx = get_amp_index(kcontrol);
2825 long *valp = ucontrol->value.integer.value;
2828 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
2829 HDA_AMP_MUTE) ? 0 : 1;
2831 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
2832 HDA_AMP_MUTE) ? 0 : 1;
2835 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_get);
2838 * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch
2839 * @kcontrol: ctl element
2840 * @ucontrol: pointer to get/store the data
2842 * The control element is supposed to have the private_value field
2843 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2845 int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
2846 struct snd_ctl_elem_value *ucontrol)
2848 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2849 hda_nid_t nid = get_amp_nid(kcontrol);
2850 int chs = get_amp_channels(kcontrol);
2851 int dir = get_amp_direction(kcontrol);
2852 int idx = get_amp_index(kcontrol);
2853 long *valp = ucontrol->value.integer.value;
2857 change = codec_amp_update(codec, nid, 0, dir, idx,
2859 *valp ? 0 : HDA_AMP_MUTE, false,
2860 !hda_codec_is_power_on(codec));
2864 change |= codec_amp_update(codec, nid, 1, dir, idx,
2866 *valp ? 0 : HDA_AMP_MUTE, false,
2867 !hda_codec_is_power_on(codec));
2868 hda_call_check_power_status(codec, nid);
2871 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_put);
2874 * bound volume controls
2876 * bind multiple volumes (# indices, from 0)
2879 #define AMP_VAL_IDX_SHIFT 19
2880 #define AMP_VAL_IDX_MASK (0x0f<<19)
2883 * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control
2884 * @kcontrol: ctl element
2885 * @ucontrol: pointer to get/store the data
2887 * The control element is supposed to have the private_value field
2888 * set up via HDA_BIND_MUTE*() macros.
2890 int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
2891 struct snd_ctl_elem_value *ucontrol)
2893 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2897 mutex_lock(&codec->control_mutex);
2898 pval = kcontrol->private_value;
2899 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
2900 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
2901 kcontrol->private_value = pval;
2902 mutex_unlock(&codec->control_mutex);
2905 EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_switch_get);
2908 * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control
2909 * @kcontrol: ctl element
2910 * @ucontrol: pointer to get/store the data
2912 * The control element is supposed to have the private_value field
2913 * set up via HDA_BIND_MUTE*() macros.
2915 int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
2916 struct snd_ctl_elem_value *ucontrol)
2918 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2920 int i, indices, err = 0, change = 0;
2922 mutex_lock(&codec->control_mutex);
2923 pval = kcontrol->private_value;
2924 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
2925 for (i = 0; i < indices; i++) {
2926 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
2927 (i << AMP_VAL_IDX_SHIFT);
2928 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2933 kcontrol->private_value = pval;
2934 mutex_unlock(&codec->control_mutex);
2935 return err < 0 ? err : change;
2937 EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_switch_put);
2940 * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control
2941 * @kcontrol: referred ctl element
2942 * @uinfo: pointer to get/store the data
2944 * The control element is supposed to have the private_value field
2945 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2947 int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
2948 struct snd_ctl_elem_info *uinfo)
2950 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2951 struct hda_bind_ctls *c;
2954 mutex_lock(&codec->control_mutex);
2955 c = (struct hda_bind_ctls *)kcontrol->private_value;
2956 kcontrol->private_value = *c->values;
2957 err = c->ops->info(kcontrol, uinfo);
2958 kcontrol->private_value = (long)c;
2959 mutex_unlock(&codec->control_mutex);
2962 EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_info);
2965 * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control
2966 * @kcontrol: ctl element
2967 * @ucontrol: pointer to get/store the data
2969 * The control element is supposed to have the private_value field
2970 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2972 int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
2973 struct snd_ctl_elem_value *ucontrol)
2975 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2976 struct hda_bind_ctls *c;
2979 mutex_lock(&codec->control_mutex);
2980 c = (struct hda_bind_ctls *)kcontrol->private_value;
2981 kcontrol->private_value = *c->values;
2982 err = c->ops->get(kcontrol, ucontrol);
2983 kcontrol->private_value = (long)c;
2984 mutex_unlock(&codec->control_mutex);
2987 EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_get);
2990 * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control
2991 * @kcontrol: ctl element
2992 * @ucontrol: pointer to get/store the data
2994 * The control element is supposed to have the private_value field
2995 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2997 int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
2998 struct snd_ctl_elem_value *ucontrol)
3000 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3001 struct hda_bind_ctls *c;
3002 unsigned long *vals;
3003 int err = 0, change = 0;
3005 mutex_lock(&codec->control_mutex);
3006 c = (struct hda_bind_ctls *)kcontrol->private_value;
3007 for (vals = c->values; *vals; vals++) {
3008 kcontrol->private_value = *vals;
3009 err = c->ops->put(kcontrol, ucontrol);
3014 kcontrol->private_value = (long)c;
3015 mutex_unlock(&codec->control_mutex);
3016 return err < 0 ? err : change;
3018 EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_put);
3021 * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control
3022 * @kcontrol: ctl element
3023 * @op_flag: operation flag
3024 * @size: byte size of input TLV
3027 * The control element is supposed to have the private_value field
3028 * set up via HDA_BIND_VOL() macro.
3030 int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
3031 unsigned int size, unsigned int __user *tlv)
3033 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3034 struct hda_bind_ctls *c;
3037 mutex_lock(&codec->control_mutex);
3038 c = (struct hda_bind_ctls *)kcontrol->private_value;
3039 kcontrol->private_value = *c->values;
3040 err = c->ops->tlv(kcontrol, op_flag, size, tlv);
3041 kcontrol->private_value = (long)c;
3042 mutex_unlock(&codec->control_mutex);
3045 EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_tlv);
3047 struct hda_ctl_ops snd_hda_bind_vol = {
3048 .info = snd_hda_mixer_amp_volume_info,
3049 .get = snd_hda_mixer_amp_volume_get,
3050 .put = snd_hda_mixer_amp_volume_put,
3051 .tlv = snd_hda_mixer_amp_tlv
3053 EXPORT_SYMBOL_GPL(snd_hda_bind_vol);
3055 struct hda_ctl_ops snd_hda_bind_sw = {
3056 .info = snd_hda_mixer_amp_switch_info,
3057 .get = snd_hda_mixer_amp_switch_get,
3058 .put = snd_hda_mixer_amp_switch_put,
3059 .tlv = snd_hda_mixer_amp_tlv
3061 EXPORT_SYMBOL_GPL(snd_hda_bind_sw);
3064 * SPDIF out controls
3067 static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
3068 struct snd_ctl_elem_info *uinfo)
3070 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
3075 static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
3076 struct snd_ctl_elem_value *ucontrol)
3078 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
3079 IEC958_AES0_NONAUDIO |
3080 IEC958_AES0_CON_EMPHASIS_5015 |
3081 IEC958_AES0_CON_NOT_COPYRIGHT;
3082 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
3083 IEC958_AES1_CON_ORIGINAL;
3087 static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
3088 struct snd_ctl_elem_value *ucontrol)
3090 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
3091 IEC958_AES0_NONAUDIO |
3092 IEC958_AES0_PRO_EMPHASIS_5015;
3096 static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
3097 struct snd_ctl_elem_value *ucontrol)
3099 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3100 int idx = kcontrol->private_value;
3101 struct hda_spdif_out *spdif;
3103 mutex_lock(&codec->spdif_mutex);
3104 spdif = snd_array_elem(&codec->spdif_out, idx);
3105 ucontrol->value.iec958.status[0] = spdif->status & 0xff;
3106 ucontrol->value.iec958.status[1] = (spdif->status >> 8) & 0xff;
3107 ucontrol->value.iec958.status[2] = (spdif->status >> 16) & 0xff;
3108 ucontrol->value.iec958.status[3] = (spdif->status >> 24) & 0xff;
3109 mutex_unlock(&codec->spdif_mutex);
3114 /* convert from SPDIF status bits to HDA SPDIF bits
3115 * bit 0 (DigEn) is always set zero (to be filled later)
3117 static unsigned short convert_from_spdif_status(unsigned int sbits)
3119 unsigned short val = 0;
3121 if (sbits & IEC958_AES0_PROFESSIONAL)
3122 val |= AC_DIG1_PROFESSIONAL;
3123 if (sbits & IEC958_AES0_NONAUDIO)
3124 val |= AC_DIG1_NONAUDIO;
3125 if (sbits & IEC958_AES0_PROFESSIONAL) {
3126 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
3127 IEC958_AES0_PRO_EMPHASIS_5015)
3128 val |= AC_DIG1_EMPHASIS;
3130 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
3131 IEC958_AES0_CON_EMPHASIS_5015)
3132 val |= AC_DIG1_EMPHASIS;
3133 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
3134 val |= AC_DIG1_COPYRIGHT;
3135 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
3136 val |= AC_DIG1_LEVEL;
3137 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
3142 /* convert to SPDIF status bits from HDA SPDIF bits
3144 static unsigned int convert_to_spdif_status(unsigned short val)
3146 unsigned int sbits = 0;
3148 if (val & AC_DIG1_NONAUDIO)
3149 sbits |= IEC958_AES0_NONAUDIO;
3150 if (val & AC_DIG1_PROFESSIONAL)
3151 sbits |= IEC958_AES0_PROFESSIONAL;
3152 if (sbits & IEC958_AES0_PROFESSIONAL) {
3153 if (val & AC_DIG1_EMPHASIS)
3154 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
3156 if (val & AC_DIG1_EMPHASIS)
3157 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
3158 if (!(val & AC_DIG1_COPYRIGHT))
3159 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
3160 if (val & AC_DIG1_LEVEL)
3161 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
3162 sbits |= val & (0x7f << 8);
3167 /* set digital convert verbs both for the given NID and its slaves */
3168 static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
3173 snd_hda_codec_write_cache(codec, nid, 0, verb, val);
3174 d = codec->slave_dig_outs;
3178 snd_hda_codec_write_cache(codec, *d, 0, verb, val);
3181 static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
3185 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1);
3187 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2);
3190 static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
3191 struct snd_ctl_elem_value *ucontrol)
3193 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3194 int idx = kcontrol->private_value;
3195 struct hda_spdif_out *spdif;
3200 mutex_lock(&codec->spdif_mutex);
3201 spdif = snd_array_elem(&codec->spdif_out, idx);
3203 spdif->status = ucontrol->value.iec958.status[0] |
3204 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
3205 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
3206 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
3207 val = convert_from_spdif_status(spdif->status);
3208 val |= spdif->ctls & 1;
3209 change = spdif->ctls != val;
3211 if (change && nid != (u16)-1)
3212 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
3213 mutex_unlock(&codec->spdif_mutex);
3217 #define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
3219 static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
3220 struct snd_ctl_elem_value *ucontrol)
3222 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3223 int idx = kcontrol->private_value;
3224 struct hda_spdif_out *spdif;
3226 mutex_lock(&codec->spdif_mutex);
3227 spdif = snd_array_elem(&codec->spdif_out, idx);
3228 ucontrol->value.integer.value[0] = spdif->ctls & AC_DIG1_ENABLE;
3229 mutex_unlock(&codec->spdif_mutex);
3233 static inline void set_spdif_ctls(struct hda_codec *codec, hda_nid_t nid,
3236 set_dig_out_convert(codec, nid, dig1, dig2);
3237 /* unmute amp switch (if any) */
3238 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
3239 (dig1 & AC_DIG1_ENABLE))
3240 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
3244 static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
3245 struct snd_ctl_elem_value *ucontrol)
3247 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3248 int idx = kcontrol->private_value;
3249 struct hda_spdif_out *spdif;
3254 mutex_lock(&codec->spdif_mutex);
3255 spdif = snd_array_elem(&codec->spdif_out, idx);
3257 val = spdif->ctls & ~AC_DIG1_ENABLE;
3258 if (ucontrol->value.integer.value[0])
3259 val |= AC_DIG1_ENABLE;
3260 change = spdif->ctls != val;
3262 if (change && nid != (u16)-1)
3263 set_spdif_ctls(codec, nid, val & 0xff, -1);
3264 mutex_unlock(&codec->spdif_mutex);
3268 static struct snd_kcontrol_new dig_mixes[] = {
3270 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3271 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3272 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
3273 .info = snd_hda_spdif_mask_info,
3274 .get = snd_hda_spdif_cmask_get,
3277 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3278 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3279 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK),
3280 .info = snd_hda_spdif_mask_info,
3281 .get = snd_hda_spdif_pmask_get,
3284 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3285 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
3286 .info = snd_hda_spdif_mask_info,
3287 .get = snd_hda_spdif_default_get,
3288 .put = snd_hda_spdif_default_put,
3291 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3292 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
3293 .info = snd_hda_spdif_out_switch_info,
3294 .get = snd_hda_spdif_out_switch_get,
3295 .put = snd_hda_spdif_out_switch_put,
3301 * snd_hda_create_dig_out_ctls - create Output SPDIF-related controls
3302 * @codec: the HDA codec
3303 * @associated_nid: NID that new ctls associated with
3304 * @cvt_nid: converter NID
3305 * @type: HDA_PCM_TYPE_*
3306 * Creates controls related with the digital output.
3307 * Called from each patch supporting the digital out.
3309 * Returns 0 if successful, or a negative error code.
3311 int snd_hda_create_dig_out_ctls(struct hda_codec *codec,
3312 hda_nid_t associated_nid,
3317 struct snd_kcontrol *kctl;
3318 struct snd_kcontrol_new *dig_mix;
3320 const int spdif_index = 16;
3321 struct hda_spdif_out *spdif;
3322 struct hda_bus *bus = codec->bus;
3324 if (bus->primary_dig_out_type == HDA_PCM_TYPE_HDMI &&
3325 type == HDA_PCM_TYPE_SPDIF) {
3327 } else if (bus->primary_dig_out_type == HDA_PCM_TYPE_SPDIF &&
3328 type == HDA_PCM_TYPE_HDMI) {
3329 /* suppose a single SPDIF device */
3330 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
3331 kctl = find_mixer_ctl(codec, dig_mix->name, 0, 0);
3334 kctl->id.index = spdif_index;
3336 bus->primary_dig_out_type = HDA_PCM_TYPE_HDMI;
3338 if (!bus->primary_dig_out_type)
3339 bus->primary_dig_out_type = type;
3341 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Playback Switch", idx);
3343 codec_err(codec, "too many IEC958 outputs\n");
3346 spdif = snd_array_new(&codec->spdif_out);
3349 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
3350 kctl = snd_ctl_new1(dig_mix, codec);
3353 kctl->id.index = idx;
3354 kctl->private_value = codec->spdif_out.used - 1;
3355 err = snd_hda_ctl_add(codec, associated_nid, kctl);
3359 spdif->nid = cvt_nid;
3360 spdif->ctls = snd_hda_codec_read(codec, cvt_nid, 0,
3361 AC_VERB_GET_DIGI_CONVERT_1, 0);
3362 spdif->status = convert_to_spdif_status(spdif->ctls);
3365 EXPORT_SYMBOL_GPL(snd_hda_create_dig_out_ctls);
3368 * snd_hda_spdif_out_of_nid - get the hda_spdif_out entry from the given NID
3369 * @codec: the HDA codec
3372 * call within spdif_mutex lock
3374 struct hda_spdif_out *snd_hda_spdif_out_of_nid(struct hda_codec *codec,
3378 for (i = 0; i < codec->spdif_out.used; i++) {
3379 struct hda_spdif_out *spdif =
3380 snd_array_elem(&codec->spdif_out, i);
3381 if (spdif->nid == nid)
3386 EXPORT_SYMBOL_GPL(snd_hda_spdif_out_of_nid);
3389 * snd_hda_spdif_ctls_unassign - Unassign the given SPDIF ctl
3390 * @codec: the HDA codec
3391 * @idx: the SPDIF ctl index
3393 * Unassign the widget from the given SPDIF control.
3395 void snd_hda_spdif_ctls_unassign(struct hda_codec *codec, int idx)
3397 struct hda_spdif_out *spdif;
3399 mutex_lock(&codec->spdif_mutex);
3400 spdif = snd_array_elem(&codec->spdif_out, idx);
3401 spdif->nid = (u16)-1;
3402 mutex_unlock(&codec->spdif_mutex);
3404 EXPORT_SYMBOL_GPL(snd_hda_spdif_ctls_unassign);
3407 * snd_hda_spdif_ctls_assign - Assign the SPDIF controls to the given NID
3408 * @codec: the HDA codec
3409 * @idx: the SPDIF ctl idx
3412 * Assign the widget to the SPDIF control with the given index.
3414 void snd_hda_spdif_ctls_assign(struct hda_codec *codec, int idx, hda_nid_t nid)
3416 struct hda_spdif_out *spdif;
3419 mutex_lock(&codec->spdif_mutex);
3420 spdif = snd_array_elem(&codec->spdif_out, idx);
3421 if (spdif->nid != nid) {
3424 set_spdif_ctls(codec, nid, val & 0xff, (val >> 8) & 0xff);
3426 mutex_unlock(&codec->spdif_mutex);
3428 EXPORT_SYMBOL_GPL(snd_hda_spdif_ctls_assign);
3431 * SPDIF sharing with analog output
3433 static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
3434 struct snd_ctl_elem_value *ucontrol)
3436 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3437 ucontrol->value.integer.value[0] = mout->share_spdif;
3441 static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
3442 struct snd_ctl_elem_value *ucontrol)
3444 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3445 mout->share_spdif = !!ucontrol->value.integer.value[0];
3449 static struct snd_kcontrol_new spdif_share_sw = {
3450 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3451 .name = "IEC958 Default PCM Playback Switch",
3452 .info = snd_ctl_boolean_mono_info,
3453 .get = spdif_share_sw_get,
3454 .put = spdif_share_sw_put,
3458 * snd_hda_create_spdif_share_sw - create Default PCM switch
3459 * @codec: the HDA codec
3460 * @mout: multi-out instance
3462 int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
3463 struct hda_multi_out *mout)
3465 struct snd_kcontrol *kctl;
3467 if (!mout->dig_out_nid)
3470 kctl = snd_ctl_new1(&spdif_share_sw, mout);
3473 /* ATTENTION: here mout is passed as private_data, instead of codec */
3474 return snd_hda_ctl_add(codec, mout->dig_out_nid, kctl);
3476 EXPORT_SYMBOL_GPL(snd_hda_create_spdif_share_sw);
3482 #define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
3484 static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
3485 struct snd_ctl_elem_value *ucontrol)
3487 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3489 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
3493 static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
3494 struct snd_ctl_elem_value *ucontrol)
3496 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3497 hda_nid_t nid = kcontrol->private_value;
3498 unsigned int val = !!ucontrol->value.integer.value[0];
3501 mutex_lock(&codec->spdif_mutex);
3502 change = codec->spdif_in_enable != val;
3504 codec->spdif_in_enable = val;
3505 snd_hda_codec_write_cache(codec, nid, 0,
3506 AC_VERB_SET_DIGI_CONVERT_1, val);
3508 mutex_unlock(&codec->spdif_mutex);
3512 static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
3513 struct snd_ctl_elem_value *ucontrol)
3515 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3516 hda_nid_t nid = kcontrol->private_value;
3520 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0);
3521 sbits = convert_to_spdif_status(val);
3522 ucontrol->value.iec958.status[0] = sbits;
3523 ucontrol->value.iec958.status[1] = sbits >> 8;
3524 ucontrol->value.iec958.status[2] = sbits >> 16;
3525 ucontrol->value.iec958.status[3] = sbits >> 24;
3529 static struct snd_kcontrol_new dig_in_ctls[] = {
3531 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3532 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH),
3533 .info = snd_hda_spdif_in_switch_info,
3534 .get = snd_hda_spdif_in_switch_get,
3535 .put = snd_hda_spdif_in_switch_put,
3538 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3539 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3540 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
3541 .info = snd_hda_spdif_mask_info,
3542 .get = snd_hda_spdif_in_status_get,
3548 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
3549 * @codec: the HDA codec
3550 * @nid: audio in widget NID
3552 * Creates controls related with the SPDIF input.
3553 * Called from each patch supporting the SPDIF in.
3555 * Returns 0 if successful, or a negative error code.
3557 int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
3560 struct snd_kcontrol *kctl;
3561 struct snd_kcontrol_new *dig_mix;
3564 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Capture Switch", 0);
3566 codec_err(codec, "too many IEC958 inputs\n");
3569 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
3570 kctl = snd_ctl_new1(dig_mix, codec);
3573 kctl->private_value = nid;
3574 err = snd_hda_ctl_add(codec, nid, kctl);
3578 codec->spdif_in_enable =
3579 snd_hda_codec_read(codec, nid, 0,
3580 AC_VERB_GET_DIGI_CONVERT_1, 0) &
3584 EXPORT_SYMBOL_GPL(snd_hda_create_spdif_in_ctls);
3590 /* build a 31bit cache key with the widget id and the command parameter */
3591 #define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
3592 #define get_cmd_cache_nid(key) ((key) & 0xff)
3593 #define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
3596 * snd_hda_codec_write_cache - send a single command with caching
3597 * @codec: the HDA codec
3598 * @nid: NID to send the command
3599 * @flags: optional bit flags
3600 * @verb: the verb to send
3601 * @parm: the parameter for the verb
3603 * Send a single command without waiting for response.
3605 * Returns 0 if successful, or a negative error code.
3607 int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
3608 int flags, unsigned int verb, unsigned int parm)
3611 struct hda_cache_head *c;
3613 unsigned int cache_only;
3615 cache_only = codec->cached_write;
3617 err = snd_hda_codec_write(codec, nid, flags, verb, parm);
3622 /* parm may contain the verb stuff for get/set amp */
3623 verb = verb | (parm >> 8);
3625 key = build_cmd_cache_key(nid, verb);
3626 mutex_lock(&codec->bus->cmd_mutex);
3627 c = get_alloc_hash(&codec->cmd_cache, key);
3630 c->dirty = cache_only;
3632 mutex_unlock(&codec->bus->cmd_mutex);
3635 EXPORT_SYMBOL_GPL(snd_hda_codec_write_cache);
3638 * snd_hda_codec_update_cache - check cache and write the cmd only when needed
3639 * @codec: the HDA codec
3640 * @nid: NID to send the command
3641 * @flags: optional bit flags
3642 * @verb: the verb to send
3643 * @parm: the parameter for the verb
3645 * This function works like snd_hda_codec_write_cache(), but it doesn't send
3646 * command if the parameter is already identical with the cached value.
3647 * If not, it sends the command and refreshes the cache.
3649 * Returns 0 if successful, or a negative error code.
3651 int snd_hda_codec_update_cache(struct hda_codec *codec, hda_nid_t nid,
3652 int flags, unsigned int verb, unsigned int parm)
3654 struct hda_cache_head *c;
3657 /* parm may contain the verb stuff for get/set amp */
3658 verb = verb | (parm >> 8);
3660 key = build_cmd_cache_key(nid, verb);
3661 mutex_lock(&codec->bus->cmd_mutex);
3662 c = get_hash(&codec->cmd_cache, key);
3663 if (c && c->val == parm) {
3664 mutex_unlock(&codec->bus->cmd_mutex);
3667 mutex_unlock(&codec->bus->cmd_mutex);
3668 return snd_hda_codec_write_cache(codec, nid, flags, verb, parm);
3670 EXPORT_SYMBOL_GPL(snd_hda_codec_update_cache);
3673 * snd_hda_codec_resume_cache - Resume the all commands from the cache
3674 * @codec: HD-audio codec
3676 * Execute all verbs recorded in the command caches to resume.
3678 void snd_hda_codec_resume_cache(struct hda_codec *codec)
3682 mutex_lock(&codec->hash_mutex);
3683 codec->cached_write = 0;
3684 for (i = 0; i < codec->cmd_cache.buf.used; i++) {
3685 struct hda_cache_head *buffer;
3688 buffer = snd_array_elem(&codec->cmd_cache.buf, i);
3695 mutex_unlock(&codec->hash_mutex);
3696 snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
3697 get_cmd_cache_cmd(key), buffer->val);
3698 mutex_lock(&codec->hash_mutex);
3700 mutex_unlock(&codec->hash_mutex);
3702 EXPORT_SYMBOL_GPL(snd_hda_codec_resume_cache);
3705 * snd_hda_sequence_write_cache - sequence writes with caching
3706 * @codec: the HDA codec
3707 * @seq: VERB array to send
3709 * Send the commands sequentially from the given array.
3710 * Thte commands are recorded on cache for power-save and resume.
3711 * The array must be terminated with NID=0.
3713 void snd_hda_sequence_write_cache(struct hda_codec *codec,
3714 const struct hda_verb *seq)
3716 for (; seq->nid; seq++)
3717 snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
3720 EXPORT_SYMBOL_GPL(snd_hda_sequence_write_cache);
3723 * snd_hda_codec_flush_cache - Execute all pending (cached) amps / verbs
3724 * @codec: HD-audio codec
3726 void snd_hda_codec_flush_cache(struct hda_codec *codec)
3728 snd_hda_codec_resume_amp(codec);
3729 snd_hda_codec_resume_cache(codec);
3731 EXPORT_SYMBOL_GPL(snd_hda_codec_flush_cache);
3734 * snd_hda_codec_set_power_to_all - Set the power state to all widgets
3735 * @codec: the HDA codec
3736 * @fg: function group (not used now)
3737 * @power_state: the power state to set (AC_PWRST_*)
3739 * Set the given power state to all widgets that have the power control.
3740 * If the codec has power_filter set, it evaluates the power state and
3741 * filter out if it's unchanged as D3.
3743 void snd_hda_codec_set_power_to_all(struct hda_codec *codec, hda_nid_t fg,
3744 unsigned int power_state)
3746 hda_nid_t nid = codec->start_nid;
3749 for (i = 0; i < codec->num_nodes; i++, nid++) {
3750 unsigned int wcaps = get_wcaps(codec, nid);
3751 unsigned int state = power_state;
3752 if (!(wcaps & AC_WCAP_POWER))
3754 if (codec->power_filter) {
3755 state = codec->power_filter(codec, nid, power_state);
3756 if (state != power_state && power_state == AC_PWRST_D3)
3759 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE,
3763 EXPORT_SYMBOL_GPL(snd_hda_codec_set_power_to_all);
3766 * supported power states check
3768 static bool snd_hda_codec_get_supported_ps(struct hda_codec *codec, hda_nid_t fg,
3769 unsigned int power_state)
3771 int sup = snd_hda_param_read(codec, fg, AC_PAR_POWER_STATE);
3775 if (sup & power_state)
3782 * wait until the state is reached, returns the current state
3784 static unsigned int hda_sync_power_state(struct hda_codec *codec,
3786 unsigned int power_state)
3788 unsigned long end_time = jiffies + msecs_to_jiffies(500);
3789 unsigned int state, actual_state;
3792 state = snd_hda_codec_read(codec, fg, 0,
3793 AC_VERB_GET_POWER_STATE, 0);
3794 if (state & AC_PWRST_ERROR)
3796 actual_state = (state >> 4) & 0x0f;
3797 if (actual_state == power_state)
3799 if (time_after_eq(jiffies, end_time))
3801 /* wait until the codec reachs to the target state */
3808 * snd_hda_codec_eapd_power_filter - A power filter callback for EAPD
3809 * @codec: the HDA codec
3811 * @power_state: power state to evalue
3813 * Don't power down the widget if it controls eapd and EAPD_BTLENABLE is set.
3814 * This can be used a codec power_filter callback.
3816 unsigned int snd_hda_codec_eapd_power_filter(struct hda_codec *codec,
3818 unsigned int power_state)
3820 if (nid == codec->afg || nid == codec->mfg)
3822 if (power_state == AC_PWRST_D3 &&
3823 get_wcaps_type(get_wcaps(codec, nid)) == AC_WID_PIN &&
3824 (snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD)) {
3825 int eapd = snd_hda_codec_read(codec, nid, 0,
3826 AC_VERB_GET_EAPD_BTLENABLE, 0);
3832 EXPORT_SYMBOL_GPL(snd_hda_codec_eapd_power_filter);
3835 * set power state of the codec, and return the power state
3837 static unsigned int hda_set_power_state(struct hda_codec *codec,
3838 unsigned int power_state)
3840 hda_nid_t fg = codec->afg ? codec->afg : codec->mfg;
3845 /* this delay seems necessary to avoid click noise at power-down */
3846 if (power_state == AC_PWRST_D3) {
3847 if (codec->depop_delay < 0)
3848 msleep(codec->epss ? 10 : 100);
3849 else if (codec->depop_delay > 0)
3850 msleep(codec->depop_delay);
3851 flags = HDA_RW_NO_RESPONSE_FALLBACK;
3854 /* repeat power states setting at most 10 times*/
3855 for (count = 0; count < 10; count++) {
3856 if (codec->patch_ops.set_power_state)
3857 codec->patch_ops.set_power_state(codec, fg,
3860 state = power_state;
3861 if (codec->power_filter)
3862 state = codec->power_filter(codec, fg, state);
3863 if (state == power_state || power_state != AC_PWRST_D3)
3864 snd_hda_codec_read(codec, fg, flags,
3865 AC_VERB_SET_POWER_STATE,
3867 snd_hda_codec_set_power_to_all(codec, fg, power_state);
3869 state = hda_sync_power_state(codec, fg, power_state);
3870 if (!(state & AC_PWRST_ERROR))
3877 /* sync power states of all widgets;
3878 * this is called at the end of codec parsing
3880 static void sync_power_up_states(struct hda_codec *codec)
3882 hda_nid_t nid = codec->start_nid;
3885 /* don't care if no filter is used */
3886 if (!codec->power_filter)
3889 for (i = 0; i < codec->num_nodes; i++, nid++) {
3890 unsigned int wcaps = get_wcaps(codec, nid);
3891 unsigned int target;
3892 if (!(wcaps & AC_WCAP_POWER))
3894 target = codec->power_filter(codec, nid, AC_PWRST_D0);
3895 if (target == AC_PWRST_D0)
3897 if (!snd_hda_check_power_state(codec, nid, target))
3898 snd_hda_codec_write(codec, nid, 0,
3899 AC_VERB_SET_POWER_STATE, target);
3903 #ifdef CONFIG_SND_HDA_RECONFIG
3904 /* execute additional init verbs */
3905 static void hda_exec_init_verbs(struct hda_codec *codec)
3907 if (codec->init_verbs.list)
3908 snd_hda_sequence_write(codec, codec->init_verbs.list);
3911 static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
3915 /* update the power on/off account with the current jiffies */
3916 static void update_power_acct(struct hda_codec *codec, bool on)
3918 unsigned long delta = jiffies - codec->power_jiffies;
3921 codec->power_on_acct += delta;
3923 codec->power_off_acct += delta;
3924 codec->power_jiffies += delta;
3927 void snd_hda_update_power_acct(struct hda_codec *codec)
3929 update_power_acct(codec, hda_codec_is_power_on(codec));
3933 * call suspend and power-down; used both from PM and power-save
3934 * this function returns the power state in the end
3936 static unsigned int hda_call_codec_suspend(struct hda_codec *codec)
3940 atomic_inc(&codec->in_pm);
3942 if (codec->patch_ops.suspend)
3943 codec->patch_ops.suspend(codec);
3944 hda_cleanup_all_streams(codec);
3945 state = hda_set_power_state(codec, AC_PWRST_D3);
3946 trace_hda_power_down(codec);
3947 update_power_acct(codec, true);
3948 atomic_dec(&codec->in_pm);
3952 /* mark all entries of cmd and amp caches dirty */
3953 static void hda_mark_cmd_cache_dirty(struct hda_codec *codec)
3956 for (i = 0; i < codec->cmd_cache.buf.used; i++) {
3957 struct hda_cache_head *cmd;
3958 cmd = snd_array_elem(&codec->cmd_cache.buf, i);
3961 for (i = 0; i < codec->amp_cache.buf.used; i++) {
3962 struct hda_amp_info *amp;
3963 amp = snd_array_elem(&codec->amp_cache.buf, i);
3964 amp->head.dirty = 1;
3969 * kick up codec; used both from PM and power-save
3971 static void hda_call_codec_resume(struct hda_codec *codec)
3973 atomic_inc(&codec->in_pm);
3975 trace_hda_power_up(codec);
3976 hda_mark_cmd_cache_dirty(codec);
3978 codec->power_jiffies = jiffies;
3980 hda_set_power_state(codec, AC_PWRST_D0);
3981 restore_shutup_pins(codec);
3982 hda_exec_init_verbs(codec);
3983 snd_hda_jack_set_dirty_all(codec);
3984 if (codec->patch_ops.resume)
3985 codec->patch_ops.resume(codec);
3987 if (codec->patch_ops.init)
3988 codec->patch_ops.init(codec);
3989 snd_hda_codec_resume_amp(codec);
3990 snd_hda_codec_resume_cache(codec);
3993 if (codec->jackpoll_interval)
3994 hda_jackpoll_work(&codec->jackpoll_work.work);
3996 snd_hda_jack_report_sync(codec);
3997 atomic_dec(&codec->in_pm);
4000 static int hda_codec_runtime_suspend(struct device *dev)
4002 struct hda_codec *codec = dev_to_hda_codec(dev);
4003 struct hda_pcm *pcm;
4006 cancel_delayed_work_sync(&codec->jackpoll_work);
4007 list_for_each_entry(pcm, &codec->pcm_list_head, list)
4008 snd_pcm_suspend_all(pcm->pcm);
4009 state = hda_call_codec_suspend(codec);
4010 if (codec->d3_stop_clk && codec->epss && (state & AC_PWRST_CLK_STOP_OK))
4011 clear_bit(codec->addr, &codec->bus->codec_powered);
4015 static int hda_codec_runtime_resume(struct device *dev)
4017 struct hda_codec *codec = dev_to_hda_codec(dev);
4019 set_bit(codec->addr, &codec->bus->codec_powered);
4020 hda_call_codec_resume(codec);
4021 pm_runtime_mark_last_busy(dev);
4024 #endif /* CONFIG_PM */
4026 /* referred in hda_bind.c */
4027 const struct dev_pm_ops hda_codec_driver_pm = {
4028 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
4029 pm_runtime_force_resume)
4030 SET_RUNTIME_PM_OPS(hda_codec_runtime_suspend, hda_codec_runtime_resume,
4035 * snd_hda_build_controls - build mixer controls
4038 * Creates mixer controls for each codec included in the bus.
4040 * Returns 0 if successful, otherwise a negative error code.
4042 int snd_hda_build_controls(struct hda_bus *bus)
4044 struct hda_codec *codec;
4046 list_for_each_entry(codec, &bus->codec_list, list) {
4047 int err = snd_hda_codec_build_controls(codec);
4050 "cannot build controls for #%d (error %d)\n",
4052 err = snd_hda_codec_reset(codec);
4055 "cannot revert codec\n");
4062 EXPORT_SYMBOL_GPL(snd_hda_build_controls);
4065 * add standard channel maps if not specified
4067 static int add_std_chmaps(struct hda_codec *codec)
4069 struct hda_pcm *pcm;
4072 list_for_each_entry(pcm, &codec->pcm_list_head, list) {
4073 for (str = 0; str < 2; str++) {
4074 struct hda_pcm_stream *hinfo = &pcm->stream[str];
4075 struct snd_pcm_chmap *chmap;
4076 const struct snd_pcm_chmap_elem *elem;
4080 if (!pcm || !hinfo->substreams)
4082 elem = hinfo->chmap ? hinfo->chmap : snd_pcm_std_chmaps;
4083 err = snd_pcm_add_chmap_ctls(pcm->pcm, str, elem,
4084 hinfo->channels_max,
4088 chmap->channel_mask = SND_PCM_CHMAP_MASK_2468;
4094 /* default channel maps for 2.1 speakers;
4095 * since HD-audio supports only stereo, odd number channels are omitted
4097 const struct snd_pcm_chmap_elem snd_pcm_2_1_chmaps[] = {
4099 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } },
4101 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
4102 SNDRV_CHMAP_LFE, SNDRV_CHMAP_LFE } },
4105 EXPORT_SYMBOL_GPL(snd_pcm_2_1_chmaps);
4107 int snd_hda_codec_build_controls(struct hda_codec *codec)
4110 hda_exec_init_verbs(codec);
4111 /* continue to initialize... */
4112 if (codec->patch_ops.init)
4113 err = codec->patch_ops.init(codec);
4114 if (!err && codec->patch_ops.build_controls)
4115 err = codec->patch_ops.build_controls(codec);
4119 /* we create chmaps here instead of build_pcms */
4120 err = add_std_chmaps(codec);
4124 if (codec->jackpoll_interval)
4125 hda_jackpoll_work(&codec->jackpoll_work.work);
4127 snd_hda_jack_report_sync(codec); /* call at the last init point */
4128 sync_power_up_states(codec);
4135 struct hda_rate_tbl {
4137 unsigned int alsa_bits;
4138 unsigned int hda_fmt;
4141 /* rate = base * mult / div */
4142 #define HDA_RATE(base, mult, div) \
4143 (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \
4144 (((div) - 1) << AC_FMT_DIV_SHIFT))
4146 static struct hda_rate_tbl rate_bits[] = {
4147 /* rate in Hz, ALSA rate bitmask, HDA format value */
4149 /* autodetected value used in snd_hda_query_supported_pcm */
4150 { 8000, SNDRV_PCM_RATE_8000, HDA_RATE(48, 1, 6) },
4151 { 11025, SNDRV_PCM_RATE_11025, HDA_RATE(44, 1, 4) },
4152 { 16000, SNDRV_PCM_RATE_16000, HDA_RATE(48, 1, 3) },
4153 { 22050, SNDRV_PCM_RATE_22050, HDA_RATE(44, 1, 2) },
4154 { 32000, SNDRV_PCM_RATE_32000, HDA_RATE(48, 2, 3) },
4155 { 44100, SNDRV_PCM_RATE_44100, HDA_RATE(44, 1, 1) },
4156 { 48000, SNDRV_PCM_RATE_48000, HDA_RATE(48, 1, 1) },
4157 { 88200, SNDRV_PCM_RATE_88200, HDA_RATE(44, 2, 1) },
4158 { 96000, SNDRV_PCM_RATE_96000, HDA_RATE(48, 2, 1) },
4159 { 176400, SNDRV_PCM_RATE_176400, HDA_RATE(44, 4, 1) },
4160 { 192000, SNDRV_PCM_RATE_192000, HDA_RATE(48, 4, 1) },
4161 #define AC_PAR_PCM_RATE_BITS 11
4162 /* up to bits 10, 384kHZ isn't supported properly */
4164 /* not autodetected value */
4165 { 9600, SNDRV_PCM_RATE_KNOT, HDA_RATE(48, 1, 5) },
4167 { 0 } /* terminator */
4171 * snd_hda_calc_stream_format - calculate format bitset
4172 * @codec: HD-audio codec
4173 * @rate: the sample rate
4174 * @channels: the number of channels
4175 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
4176 * @maxbps: the max. bps
4177 * @spdif_ctls: HD-audio SPDIF status bits (0 if irrelevant)
4179 * Calculate the format bitset from the given rate, channels and th PCM format.
4181 * Return zero if invalid.
4183 unsigned int snd_hda_calc_stream_format(struct hda_codec *codec,
4185 unsigned int channels,
4186 unsigned int format,
4187 unsigned int maxbps,
4188 unsigned short spdif_ctls)
4191 unsigned int val = 0;
4193 for (i = 0; rate_bits[i].hz; i++)
4194 if (rate_bits[i].hz == rate) {
4195 val = rate_bits[i].hda_fmt;
4198 if (!rate_bits[i].hz) {
4199 codec_dbg(codec, "invalid rate %d\n", rate);
4203 if (channels == 0 || channels > 8) {
4204 codec_dbg(codec, "invalid channels %d\n", channels);
4207 val |= channels - 1;
4209 switch (snd_pcm_format_width(format)) {
4211 val |= AC_FMT_BITS_8;
4214 val |= AC_FMT_BITS_16;
4219 if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE)
4220 val |= AC_FMT_BITS_32;
4221 else if (maxbps >= 24)
4222 val |= AC_FMT_BITS_24;
4224 val |= AC_FMT_BITS_20;
4227 codec_dbg(codec, "invalid format width %d\n",
4228 snd_pcm_format_width(format));
4232 if (spdif_ctls & AC_DIG1_NONAUDIO)
4233 val |= AC_FMT_TYPE_NON_PCM;
4237 EXPORT_SYMBOL_GPL(snd_hda_calc_stream_format);
4239 static unsigned int get_pcm_param(struct hda_codec *codec, hda_nid_t nid,
4242 unsigned int val = 0;
4243 if (nid != codec->afg &&
4244 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD))
4245 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
4246 if (!val || val == -1)
4247 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
4248 if (!val || val == -1)
4253 static unsigned int query_pcm_param(struct hda_codec *codec, hda_nid_t nid)
4255 return query_caps_hash(codec, nid, 0, HDA_HASH_PARPCM_KEY(nid),
4259 static unsigned int get_stream_param(struct hda_codec *codec, hda_nid_t nid,
4262 unsigned int streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
4263 if (!streams || streams == -1)
4264 streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
4265 if (!streams || streams == -1)
4270 static unsigned int query_stream_param(struct hda_codec *codec, hda_nid_t nid)
4272 return query_caps_hash(codec, nid, 0, HDA_HASH_PARSTR_KEY(nid),
4277 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
4278 * @codec: the HDA codec
4279 * @nid: NID to query
4280 * @ratesp: the pointer to store the detected rate bitflags
4281 * @formatsp: the pointer to store the detected formats
4282 * @bpsp: the pointer to store the detected format widths
4284 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
4285 * or @bsps argument is ignored.
4287 * Returns 0 if successful, otherwise a negative error code.
4289 int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
4290 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
4292 unsigned int i, val, wcaps;
4294 wcaps = get_wcaps(codec, nid);
4295 val = query_pcm_param(codec, nid);
4299 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
4301 rates |= rate_bits[i].alsa_bits;
4305 "rates == 0 (nid=0x%x, val=0x%x, ovrd=%i)\n",
4307 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0);
4313 if (formatsp || bpsp) {
4315 unsigned int streams, bps;
4317 streams = query_stream_param(codec, nid);
4322 if (streams & AC_SUPFMT_PCM) {
4323 if (val & AC_SUPPCM_BITS_8) {
4324 formats |= SNDRV_PCM_FMTBIT_U8;
4327 if (val & AC_SUPPCM_BITS_16) {
4328 formats |= SNDRV_PCM_FMTBIT_S16_LE;
4331 if (wcaps & AC_WCAP_DIGITAL) {
4332 if (val & AC_SUPPCM_BITS_32)
4333 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
4334 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
4335 formats |= SNDRV_PCM_FMTBIT_S32_LE;
4336 if (val & AC_SUPPCM_BITS_24)
4338 else if (val & AC_SUPPCM_BITS_20)
4340 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
4341 AC_SUPPCM_BITS_32)) {
4342 formats |= SNDRV_PCM_FMTBIT_S32_LE;
4343 if (val & AC_SUPPCM_BITS_32)
4345 else if (val & AC_SUPPCM_BITS_24)
4347 else if (val & AC_SUPPCM_BITS_20)
4351 #if 0 /* FIXME: CS4206 doesn't work, which is the only codec supporting float */
4352 if (streams & AC_SUPFMT_FLOAT32) {
4353 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
4358 if (streams == AC_SUPFMT_AC3) {
4359 /* should be exclusive */
4360 /* temporary hack: we have still no proper support
4361 * for the direct AC3 stream...
4363 formats |= SNDRV_PCM_FMTBIT_U8;
4368 "formats == 0 (nid=0x%x, val=0x%x, ovrd=%i, streams=0x%x)\n",
4370 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0,
4375 *formatsp = formats;
4382 EXPORT_SYMBOL_GPL(snd_hda_query_supported_pcm);
4385 * snd_hda_is_supported_format - Check the validity of the format
4386 * @codec: HD-audio codec
4387 * @nid: NID to check
4388 * @format: the HD-audio format value to check
4390 * Check whether the given node supports the format value.
4392 * Returns 1 if supported, 0 if not.
4394 int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
4395 unsigned int format)
4398 unsigned int val = 0, rate, stream;
4400 val = query_pcm_param(codec, nid);
4404 rate = format & 0xff00;
4405 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
4406 if (rate_bits[i].hda_fmt == rate) {
4411 if (i >= AC_PAR_PCM_RATE_BITS)
4414 stream = query_stream_param(codec, nid);
4418 if (stream & AC_SUPFMT_PCM) {
4419 switch (format & 0xf0) {
4421 if (!(val & AC_SUPPCM_BITS_8))
4425 if (!(val & AC_SUPPCM_BITS_16))
4429 if (!(val & AC_SUPPCM_BITS_20))
4433 if (!(val & AC_SUPPCM_BITS_24))
4437 if (!(val & AC_SUPPCM_BITS_32))
4444 /* FIXME: check for float32 and AC3? */
4449 EXPORT_SYMBOL_GPL(snd_hda_is_supported_format);
4454 static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
4455 struct hda_codec *codec,
4456 struct snd_pcm_substream *substream)
4461 static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
4462 struct hda_codec *codec,
4463 unsigned int stream_tag,
4464 unsigned int format,
4465 struct snd_pcm_substream *substream)
4467 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4471 static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
4472 struct hda_codec *codec,
4473 struct snd_pcm_substream *substream)
4475 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
4479 static int set_pcm_default_values(struct hda_codec *codec,
4480 struct hda_pcm_stream *info)
4484 /* query support PCM information from the given NID */
4485 if (info->nid && (!info->rates || !info->formats)) {
4486 err = snd_hda_query_supported_pcm(codec, info->nid,
4487 info->rates ? NULL : &info->rates,
4488 info->formats ? NULL : &info->formats,
4489 info->maxbps ? NULL : &info->maxbps);
4493 if (info->ops.open == NULL)
4494 info->ops.open = hda_pcm_default_open_close;
4495 if (info->ops.close == NULL)
4496 info->ops.close = hda_pcm_default_open_close;
4497 if (info->ops.prepare == NULL) {
4498 if (snd_BUG_ON(!info->nid))
4500 info->ops.prepare = hda_pcm_default_prepare;
4502 if (info->ops.cleanup == NULL) {
4503 if (snd_BUG_ON(!info->nid))
4505 info->ops.cleanup = hda_pcm_default_cleanup;
4511 * codec prepare/cleanup entries
4514 * snd_hda_codec_prepare - Prepare a stream
4515 * @codec: the HDA codec
4516 * @hinfo: PCM information
4517 * @stream: stream tag to assign
4518 * @format: format id to assign
4519 * @substream: PCM substream to assign
4521 * Calls the prepare callback set by the codec with the given arguments.
4522 * Clean up the inactive streams when successful.
4524 int snd_hda_codec_prepare(struct hda_codec *codec,
4525 struct hda_pcm_stream *hinfo,
4526 unsigned int stream,
4527 unsigned int format,
4528 struct snd_pcm_substream *substream)
4531 mutex_lock(&codec->bus->prepare_mutex);
4532 if (hinfo->ops.prepare)
4533 ret = hinfo->ops.prepare(hinfo, codec, stream, format,
4538 purify_inactive_streams(codec);
4539 mutex_unlock(&codec->bus->prepare_mutex);
4542 EXPORT_SYMBOL_GPL(snd_hda_codec_prepare);
4545 * snd_hda_codec_cleanup - Prepare a stream
4546 * @codec: the HDA codec
4547 * @hinfo: PCM information
4548 * @substream: PCM substream
4550 * Calls the cleanup callback set by the codec with the given arguments.
4552 void snd_hda_codec_cleanup(struct hda_codec *codec,
4553 struct hda_pcm_stream *hinfo,
4554 struct snd_pcm_substream *substream)
4556 mutex_lock(&codec->bus->prepare_mutex);
4557 if (hinfo->ops.cleanup)
4558 hinfo->ops.cleanup(hinfo, codec, substream);
4559 mutex_unlock(&codec->bus->prepare_mutex);
4561 EXPORT_SYMBOL_GPL(snd_hda_codec_cleanup);
4564 const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = {
4565 "Audio", "SPDIF", "HDMI", "Modem"
4569 * get the empty PCM device number to assign
4571 static int get_empty_pcm_device(struct hda_bus *bus, unsigned int type)
4573 /* audio device indices; not linear to keep compatibility */
4574 /* assigned to static slots up to dev#10; if more needed, assign
4575 * the later slot dynamically (when CONFIG_SND_DYNAMIC_MINORS=y)
4577 static int audio_idx[HDA_PCM_NTYPES][5] = {
4578 [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 },
4579 [HDA_PCM_TYPE_SPDIF] = { 1, -1 },
4580 [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 },
4581 [HDA_PCM_TYPE_MODEM] = { 6, -1 },
4585 if (type >= HDA_PCM_NTYPES) {
4586 dev_err(bus->card->dev, "Invalid PCM type %d\n", type);
4590 for (i = 0; audio_idx[type][i] >= 0; i++) {
4591 #ifndef CONFIG_SND_DYNAMIC_MINORS
4592 if (audio_idx[type][i] >= 8)
4595 if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits))
4596 return audio_idx[type][i];
4599 #ifdef CONFIG_SND_DYNAMIC_MINORS
4600 /* non-fixed slots starting from 10 */
4601 for (i = 10; i < 32; i++) {
4602 if (!test_and_set_bit(i, bus->pcm_dev_bits))
4607 dev_warn(bus->card->dev, "Too many %s devices\n",
4608 snd_hda_pcm_type_name[type]);
4609 #ifndef CONFIG_SND_DYNAMIC_MINORS
4610 dev_warn(bus->card->dev,
4611 "Consider building the kernel with CONFIG_SND_DYNAMIC_MINORS=y\n");
4616 /* call build_pcms ops of the given codec and set up the default parameters */
4617 int snd_hda_codec_parse_pcms(struct hda_codec *codec)
4619 struct hda_pcm *cpcm;
4622 if (!list_empty(&codec->pcm_list_head))
4623 return 0; /* already parsed */
4625 if (!codec->patch_ops.build_pcms)
4628 err = codec->patch_ops.build_pcms(codec);
4630 codec_err(codec, "cannot build PCMs for #%d (error %d)\n",
4635 list_for_each_entry(cpcm, &codec->pcm_list_head, list) {
4638 for (stream = 0; stream < 2; stream++) {
4639 struct hda_pcm_stream *info = &cpcm->stream[stream];
4641 if (!info->substreams)
4643 err = set_pcm_default_values(codec, info);
4646 "fail to setup default for PCM %s\n",
4656 /* assign all PCMs of the given codec */
4657 int snd_hda_codec_build_pcms(struct hda_codec *codec)
4659 struct hda_bus *bus = codec->bus;
4660 struct hda_pcm *cpcm;
4663 if (snd_BUG_ON(!bus->ops.attach_pcm))
4666 err = snd_hda_codec_parse_pcms(codec);
4668 snd_hda_codec_reset(codec);
4672 /* attach a new PCM streams */
4673 list_for_each_entry(cpcm, &codec->pcm_list_head, list) {
4675 continue; /* already attached */
4676 if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
4677 continue; /* no substreams assigned */
4679 dev = get_empty_pcm_device(bus, cpcm->pcm_type);
4681 continue; /* no fatal error */
4683 err = bus->ops.attach_pcm(bus, codec, cpcm);
4686 "cannot attach PCM stream %d for codec #%d\n",
4688 continue; /* no fatal error */
4696 * snd_hda_build_pcms - build PCM information
4699 * Create PCM information for each codec included in the bus.
4701 * The build_pcms codec patch is requested to create and assign new
4702 * hda_pcm objects. The codec is responsible to call snd_hda_codec_pcm_new()
4703 * and fills the fields. Later they are instantiated by this function.
4705 * At least, substreams, channels_min and channels_max must be filled for
4706 * each stream. substreams = 0 indicates that the stream doesn't exist.
4707 * When rates and/or formats are zero, the supported values are queried
4708 * from the given nid. The nid is used also by the default ops.prepare
4709 * and ops.cleanup callbacks.
4711 * The driver needs to call ops.open in its open callback. Similarly,
4712 * ops.close is supposed to be called in the close callback.
4713 * ops.prepare should be called in the prepare or hw_params callback
4714 * with the proper parameters for set up.
4715 * ops.cleanup should be called in hw_free for clean up of streams.
4717 * This function returns 0 if successful, or a negative error code.
4719 int snd_hda_build_pcms(struct hda_bus *bus)
4721 struct hda_codec *codec;
4723 list_for_each_entry(codec, &bus->codec_list, list) {
4724 int err = snd_hda_codec_build_pcms(codec);
4730 EXPORT_SYMBOL_GPL(snd_hda_build_pcms);
4733 * snd_hda_add_new_ctls - create controls from the array
4734 * @codec: the HDA codec
4735 * @knew: the array of struct snd_kcontrol_new
4737 * This helper function creates and add new controls in the given array.
4738 * The array must be terminated with an empty entry as terminator.
4740 * Returns 0 if successful, or a negative error code.
4742 int snd_hda_add_new_ctls(struct hda_codec *codec,
4743 const struct snd_kcontrol_new *knew)
4747 for (; knew->name; knew++) {
4748 struct snd_kcontrol *kctl;
4749 int addr = 0, idx = 0;
4750 if (knew->iface == -1) /* skip this codec private value */
4753 kctl = snd_ctl_new1(knew, codec);
4757 kctl->id.device = addr;
4759 kctl->id.index = idx;
4760 err = snd_hda_ctl_add(codec, 0, kctl);
4763 /* try first with another device index corresponding to
4764 * the codec addr; if it still fails (or it's the
4765 * primary codec), then try another control index
4767 if (!addr && codec->addr)
4769 else if (!idx && !knew->index) {
4770 idx = find_empty_mixer_ctl_idx(codec,
4780 EXPORT_SYMBOL_GPL(snd_hda_add_new_ctls);
4784 * snd_hda_power_up - Power-up the codec
4785 * @codec: HD-audio codec
4787 * Increment the usage counter and resume the device if not done yet.
4789 void snd_hda_power_up(struct hda_codec *codec)
4791 struct device *dev = hda_codec_dev(codec);
4793 if (codec_in_pm(codec))
4795 pm_runtime_get_sync(dev);
4797 EXPORT_SYMBOL_GPL(snd_hda_power_up);
4800 * snd_hda_power_down - Power-down the codec
4801 * @codec: HD-audio codec
4803 * Decrement the usage counter and schedules the autosuspend if none used.
4805 void snd_hda_power_down(struct hda_codec *codec)
4807 struct device *dev = hda_codec_dev(codec);
4809 if (codec_in_pm(codec))
4811 pm_runtime_mark_last_busy(dev);
4812 pm_runtime_put_autosuspend(dev);
4814 EXPORT_SYMBOL_GPL(snd_hda_power_down);
4816 static void codec_set_power_save(struct hda_codec *codec, int delay)
4818 struct device *dev = hda_codec_dev(codec);
4821 pm_runtime_set_autosuspend_delay(dev, delay);
4822 pm_runtime_use_autosuspend(dev);
4823 pm_runtime_allow(dev);
4824 if (!pm_runtime_suspended(dev))
4825 pm_runtime_mark_last_busy(dev);
4827 pm_runtime_dont_use_autosuspend(dev);
4828 pm_runtime_forbid(dev);
4833 * snd_hda_set_power_save - reprogram autosuspend for the given delay
4834 * @bus: HD-audio bus
4835 * @delay: autosuspend delay in msec, 0 = off
4837 * Synchronize the runtime PM autosuspend state from the power_save option.
4839 void snd_hda_set_power_save(struct hda_bus *bus, int delay)
4841 struct hda_codec *c;
4843 list_for_each_entry(c, &bus->codec_list, list)
4844 codec_set_power_save(c, delay);
4846 EXPORT_SYMBOL_GPL(snd_hda_set_power_save);
4849 * snd_hda_check_amp_list_power - Check the amp list and update the power
4850 * @codec: HD-audio codec
4851 * @check: the object containing an AMP list and the status
4852 * @nid: NID to check / update
4854 * Check whether the given NID is in the amp list. If it's in the list,
4855 * check the current AMP status, and update the the power-status according
4856 * to the mute status.
4858 * This function is supposed to be set or called from the check_power_status
4861 int snd_hda_check_amp_list_power(struct hda_codec *codec,
4862 struct hda_loopback_check *check,
4865 const struct hda_amp_list *p;
4868 if (!check->amplist)
4870 for (p = check->amplist; p->nid; p++) {
4875 return 0; /* nothing changed */
4877 for (p = check->amplist; p->nid; p++) {
4878 for (ch = 0; ch < 2; ch++) {
4879 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
4881 if (!(v & HDA_AMP_MUTE) && v > 0) {
4882 if (!check->power_on) {
4883 check->power_on = 1;
4884 snd_hda_power_up(codec);
4890 if (check->power_on) {
4891 check->power_on = 0;
4892 snd_hda_power_down(codec);
4896 EXPORT_SYMBOL_GPL(snd_hda_check_amp_list_power);
4904 * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum
4905 * @imux: imux helper object
4906 * @uinfo: pointer to get/store the data
4908 int snd_hda_input_mux_info(const struct hda_input_mux *imux,
4909 struct snd_ctl_elem_info *uinfo)
4913 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4915 uinfo->value.enumerated.items = imux->num_items;
4916 if (!imux->num_items)
4918 index = uinfo->value.enumerated.item;
4919 if (index >= imux->num_items)
4920 index = imux->num_items - 1;
4921 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
4924 EXPORT_SYMBOL_GPL(snd_hda_input_mux_info);
4927 * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum
4928 * @codec: the HDA codec
4929 * @imux: imux helper object
4930 * @ucontrol: pointer to get/store the data
4931 * @nid: input mux NID
4932 * @cur_val: pointer to get/store the current imux value
4934 int snd_hda_input_mux_put(struct hda_codec *codec,
4935 const struct hda_input_mux *imux,
4936 struct snd_ctl_elem_value *ucontrol,
4938 unsigned int *cur_val)
4942 if (!imux->num_items)
4944 idx = ucontrol->value.enumerated.item[0];
4945 if (idx >= imux->num_items)
4946 idx = imux->num_items - 1;
4947 if (*cur_val == idx)
4949 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
4950 imux->items[idx].index);
4954 EXPORT_SYMBOL_GPL(snd_hda_input_mux_put);
4958 * snd_hda_enum_helper_info - Helper for simple enum ctls
4959 * @kcontrol: ctl element
4960 * @uinfo: pointer to get/store the data
4961 * @num_items: number of enum items
4962 * @texts: enum item string array
4964 * process kcontrol info callback of a simple string enum array
4965 * when @num_items is 0 or @texts is NULL, assume a boolean enum array
4967 int snd_hda_enum_helper_info(struct snd_kcontrol *kcontrol,
4968 struct snd_ctl_elem_info *uinfo,
4969 int num_items, const char * const *texts)
4971 static const char * const texts_default[] = {
4972 "Disabled", "Enabled"
4975 if (!texts || !num_items) {
4977 texts = texts_default;
4980 return snd_ctl_enum_info(uinfo, 1, num_items, texts);
4982 EXPORT_SYMBOL_GPL(snd_hda_enum_helper_info);
4985 * Multi-channel / digital-out PCM helper functions
4988 /* setup SPDIF output stream */
4989 static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
4990 unsigned int stream_tag, unsigned int format)
4992 struct hda_spdif_out *spdif;
4993 unsigned int curr_fmt;
4996 spdif = snd_hda_spdif_out_of_nid(codec, nid);
4997 curr_fmt = snd_hda_codec_read(codec, nid, 0,
4998 AC_VERB_GET_STREAM_FORMAT, 0);
4999 reset = codec->spdif_status_reset &&
5000 (spdif->ctls & AC_DIG1_ENABLE) &&
5003 /* turn off SPDIF if needed; otherwise the IEC958 bits won't be
5006 set_dig_out_convert(codec, nid,
5007 spdif->ctls & ~AC_DIG1_ENABLE & 0xff,
5009 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
5010 if (codec->slave_dig_outs) {
5012 for (d = codec->slave_dig_outs; *d; d++)
5013 snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
5016 /* turn on again (if needed) */
5018 set_dig_out_convert(codec, nid,
5019 spdif->ctls & 0xff, -1);
5022 static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
5024 snd_hda_codec_cleanup_stream(codec, nid);
5025 if (codec->slave_dig_outs) {
5027 for (d = codec->slave_dig_outs; *d; d++)
5028 snd_hda_codec_cleanup_stream(codec, *d);
5033 * snd_hda_bus_reboot_notify - call the reboot notifier of each codec
5034 * @bus: HD-audio bus
5036 void snd_hda_bus_reboot_notify(struct hda_bus *bus)
5038 struct hda_codec *codec;
5042 list_for_each_entry(codec, &bus->codec_list, list) {
5043 if (hda_codec_is_power_on(codec) &&
5044 codec->patch_ops.reboot_notify)
5045 codec->patch_ops.reboot_notify(codec);
5048 EXPORT_SYMBOL_GPL(snd_hda_bus_reboot_notify);
5051 * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode
5052 * @codec: the HDA codec
5053 * @mout: hda_multi_out object
5055 int snd_hda_multi_out_dig_open(struct hda_codec *codec,
5056 struct hda_multi_out *mout)
5058 mutex_lock(&codec->spdif_mutex);
5059 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
5060 /* already opened as analog dup; reset it once */
5061 cleanup_dig_out_stream(codec, mout->dig_out_nid);
5062 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
5063 mutex_unlock(&codec->spdif_mutex);
5066 EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_open);
5069 * snd_hda_multi_out_dig_prepare - prepare the digital out stream
5070 * @codec: the HDA codec
5071 * @mout: hda_multi_out object
5072 * @stream_tag: stream tag to assign
5073 * @format: format id to assign
5074 * @substream: PCM substream to assign
5076 int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
5077 struct hda_multi_out *mout,
5078 unsigned int stream_tag,
5079 unsigned int format,
5080 struct snd_pcm_substream *substream)
5082 mutex_lock(&codec->spdif_mutex);
5083 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
5084 mutex_unlock(&codec->spdif_mutex);
5087 EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_prepare);
5090 * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream
5091 * @codec: the HDA codec
5092 * @mout: hda_multi_out object
5094 int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec,
5095 struct hda_multi_out *mout)
5097 mutex_lock(&codec->spdif_mutex);
5098 cleanup_dig_out_stream(codec, mout->dig_out_nid);
5099 mutex_unlock(&codec->spdif_mutex);
5102 EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_cleanup);
5105 * snd_hda_multi_out_dig_close - release the digital out stream
5106 * @codec: the HDA codec
5107 * @mout: hda_multi_out object
5109 int snd_hda_multi_out_dig_close(struct hda_codec *codec,
5110 struct hda_multi_out *mout)
5112 mutex_lock(&codec->spdif_mutex);
5113 mout->dig_out_used = 0;
5114 mutex_unlock(&codec->spdif_mutex);
5117 EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_close);
5120 * snd_hda_multi_out_analog_open - open analog outputs
5121 * @codec: the HDA codec
5122 * @mout: hda_multi_out object
5123 * @substream: PCM substream to assign
5124 * @hinfo: PCM information to assign
5126 * Open analog outputs and set up the hw-constraints.
5127 * If the digital outputs can be opened as slave, open the digital
5130 int snd_hda_multi_out_analog_open(struct hda_codec *codec,
5131 struct hda_multi_out *mout,
5132 struct snd_pcm_substream *substream,
5133 struct hda_pcm_stream *hinfo)
5135 struct snd_pcm_runtime *runtime = substream->runtime;
5136 runtime->hw.channels_max = mout->max_channels;
5137 if (mout->dig_out_nid) {
5138 if (!mout->analog_rates) {
5139 mout->analog_rates = hinfo->rates;
5140 mout->analog_formats = hinfo->formats;
5141 mout->analog_maxbps = hinfo->maxbps;
5143 runtime->hw.rates = mout->analog_rates;
5144 runtime->hw.formats = mout->analog_formats;
5145 hinfo->maxbps = mout->analog_maxbps;
5147 if (!mout->spdif_rates) {
5148 snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
5150 &mout->spdif_formats,
5151 &mout->spdif_maxbps);
5153 mutex_lock(&codec->spdif_mutex);
5154 if (mout->share_spdif) {
5155 if ((runtime->hw.rates & mout->spdif_rates) &&
5156 (runtime->hw.formats & mout->spdif_formats)) {
5157 runtime->hw.rates &= mout->spdif_rates;
5158 runtime->hw.formats &= mout->spdif_formats;
5159 if (mout->spdif_maxbps < hinfo->maxbps)
5160 hinfo->maxbps = mout->spdif_maxbps;
5162 mout->share_spdif = 0;
5163 /* FIXME: need notify? */
5166 mutex_unlock(&codec->spdif_mutex);
5168 return snd_pcm_hw_constraint_step(substream->runtime, 0,
5169 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
5171 EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_open);
5174 * snd_hda_multi_out_analog_prepare - Preapre the analog outputs.
5175 * @codec: the HDA codec
5176 * @mout: hda_multi_out object
5177 * @stream_tag: stream tag to assign
5178 * @format: format id to assign
5179 * @substream: PCM substream to assign
5181 * Set up the i/o for analog out.
5182 * When the digital out is available, copy the front out to digital out, too.
5184 int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
5185 struct hda_multi_out *mout,
5186 unsigned int stream_tag,
5187 unsigned int format,
5188 struct snd_pcm_substream *substream)
5190 const hda_nid_t *nids = mout->dac_nids;
5191 int chs = substream->runtime->channels;
5192 struct hda_spdif_out *spdif;
5195 mutex_lock(&codec->spdif_mutex);
5196 spdif = snd_hda_spdif_out_of_nid(codec, mout->dig_out_nid);
5197 if (mout->dig_out_nid && mout->share_spdif &&
5198 mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
5200 snd_hda_is_supported_format(codec, mout->dig_out_nid,
5202 !(spdif->status & IEC958_AES0_NONAUDIO)) {
5203 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
5204 setup_dig_out_stream(codec, mout->dig_out_nid,
5205 stream_tag, format);
5207 mout->dig_out_used = 0;
5208 cleanup_dig_out_stream(codec, mout->dig_out_nid);
5211 mutex_unlock(&codec->spdif_mutex);
5214 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
5216 if (!mout->no_share_stream &&
5217 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
5218 /* headphone out will just decode front left/right (stereo) */
5219 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
5221 /* extra outputs copied from front */
5222 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
5223 if (!mout->no_share_stream && mout->hp_out_nid[i])
5224 snd_hda_codec_setup_stream(codec,
5225 mout->hp_out_nid[i],
5226 stream_tag, 0, format);
5229 for (i = 1; i < mout->num_dacs; i++) {
5230 if (chs >= (i + 1) * 2) /* independent out */
5231 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
5233 else if (!mout->no_share_stream) /* copy front */
5234 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
5238 /* extra surrounds */
5239 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++) {
5241 if (!mout->extra_out_nid[i])
5243 if (chs >= (i + 1) * 2)
5245 else if (!mout->no_share_stream)
5247 snd_hda_codec_setup_stream(codec, mout->extra_out_nid[i],
5248 stream_tag, ch, format);
5253 EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_prepare);
5256 * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out
5257 * @codec: the HDA codec
5258 * @mout: hda_multi_out object
5260 int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
5261 struct hda_multi_out *mout)
5263 const hda_nid_t *nids = mout->dac_nids;
5266 for (i = 0; i < mout->num_dacs; i++)
5267 snd_hda_codec_cleanup_stream(codec, nids[i]);
5269 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
5270 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
5271 if (mout->hp_out_nid[i])
5272 snd_hda_codec_cleanup_stream(codec,
5273 mout->hp_out_nid[i]);
5274 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
5275 if (mout->extra_out_nid[i])
5276 snd_hda_codec_cleanup_stream(codec,
5277 mout->extra_out_nid[i]);
5278 mutex_lock(&codec->spdif_mutex);
5279 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
5280 cleanup_dig_out_stream(codec, mout->dig_out_nid);
5281 mout->dig_out_used = 0;
5283 mutex_unlock(&codec->spdif_mutex);
5286 EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_cleanup);
5289 * snd_hda_get_default_vref - Get the default (mic) VREF pin bits
5290 * @codec: the HDA codec
5291 * @pin: referred pin NID
5293 * Guess the suitable VREF pin bits to be set as the pin-control value.
5294 * Note: the function doesn't set the AC_PINCTL_IN_EN bit.
5296 unsigned int snd_hda_get_default_vref(struct hda_codec *codec, hda_nid_t pin)
5298 unsigned int pincap;
5299 unsigned int oldval;
5300 oldval = snd_hda_codec_read(codec, pin, 0,
5301 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
5302 pincap = snd_hda_query_pin_caps(codec, pin);
5303 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
5304 /* Exception: if the default pin setup is vref50, we give it priority */
5305 if ((pincap & AC_PINCAP_VREF_80) && oldval != PIN_VREF50)
5306 return AC_PINCTL_VREF_80;
5307 else if (pincap & AC_PINCAP_VREF_50)
5308 return AC_PINCTL_VREF_50;
5309 else if (pincap & AC_PINCAP_VREF_100)
5310 return AC_PINCTL_VREF_100;
5311 else if (pincap & AC_PINCAP_VREF_GRD)
5312 return AC_PINCTL_VREF_GRD;
5313 return AC_PINCTL_VREF_HIZ;
5315 EXPORT_SYMBOL_GPL(snd_hda_get_default_vref);
5318 * snd_hda_correct_pin_ctl - correct the pin ctl value for matching with the pin cap
5319 * @codec: the HDA codec
5320 * @pin: referred pin NID
5321 * @val: pin ctl value to audit
5323 unsigned int snd_hda_correct_pin_ctl(struct hda_codec *codec,
5324 hda_nid_t pin, unsigned int val)
5326 static unsigned int cap_lists[][2] = {
5327 { AC_PINCTL_VREF_100, AC_PINCAP_VREF_100 },
5328 { AC_PINCTL_VREF_80, AC_PINCAP_VREF_80 },
5329 { AC_PINCTL_VREF_50, AC_PINCAP_VREF_50 },
5330 { AC_PINCTL_VREF_GRD, AC_PINCAP_VREF_GRD },
5336 cap = snd_hda_query_pin_caps(codec, pin);
5338 return val; /* don't know what to do... */
5340 if (val & AC_PINCTL_OUT_EN) {
5341 if (!(cap & AC_PINCAP_OUT))
5342 val &= ~(AC_PINCTL_OUT_EN | AC_PINCTL_HP_EN);
5343 else if ((val & AC_PINCTL_HP_EN) && !(cap & AC_PINCAP_HP_DRV))
5344 val &= ~AC_PINCTL_HP_EN;
5347 if (val & AC_PINCTL_IN_EN) {
5348 if (!(cap & AC_PINCAP_IN))
5349 val &= ~(AC_PINCTL_IN_EN | AC_PINCTL_VREFEN);
5351 unsigned int vcap, vref;
5353 vcap = (cap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
5354 vref = val & AC_PINCTL_VREFEN;
5355 for (i = 0; i < ARRAY_SIZE(cap_lists); i++) {
5356 if (vref == cap_lists[i][0] &&
5357 !(vcap & cap_lists[i][1])) {
5358 if (i == ARRAY_SIZE(cap_lists) - 1)
5359 vref = AC_PINCTL_VREF_HIZ;
5361 vref = cap_lists[i + 1][0];
5364 val &= ~AC_PINCTL_VREFEN;
5371 EXPORT_SYMBOL_GPL(snd_hda_correct_pin_ctl);
5374 * _snd_hda_pin_ctl - Helper to set pin ctl value
5375 * @codec: the HDA codec
5376 * @pin: referred pin NID
5377 * @val: pin control value to set
5378 * @cached: access over codec pinctl cache or direct write
5380 * This function is a helper to set a pin ctl value more safely.
5381 * It corrects the pin ctl value via snd_hda_correct_pin_ctl(), stores the
5382 * value in pin target array via snd_hda_codec_set_pin_target(), then
5383 * actually writes the value via either snd_hda_codec_update_cache() or
5384 * snd_hda_codec_write() depending on @cached flag.
5386 int _snd_hda_set_pin_ctl(struct hda_codec *codec, hda_nid_t pin,
5387 unsigned int val, bool cached)
5389 val = snd_hda_correct_pin_ctl(codec, pin, val);
5390 snd_hda_codec_set_pin_target(codec, pin, val);
5392 return snd_hda_codec_update_cache(codec, pin, 0,
5393 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
5395 return snd_hda_codec_write(codec, pin, 0,
5396 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
5398 EXPORT_SYMBOL_GPL(_snd_hda_set_pin_ctl);
5401 * snd_hda_add_imux_item - Add an item to input_mux
5402 * @codec: the HDA codec
5403 * @imux: imux helper object
5404 * @label: the name of imux item to assign
5405 * @index: index number of imux item to assign
5406 * @type_idx: pointer to store the resultant label index
5408 * When the same label is used already in the existing items, the number
5409 * suffix is appended to the label. This label index number is stored
5410 * to type_idx when non-NULL pointer is given.
5412 int snd_hda_add_imux_item(struct hda_codec *codec,
5413 struct hda_input_mux *imux, const char *label,
5414 int index, int *type_idx)
5416 int i, label_idx = 0;
5417 if (imux->num_items >= HDA_MAX_NUM_INPUTS) {
5418 codec_err(codec, "hda_codec: Too many imux items!\n");
5421 for (i = 0; i < imux->num_items; i++) {
5422 if (!strncmp(label, imux->items[i].label, strlen(label)))
5426 *type_idx = label_idx;
5428 snprintf(imux->items[imux->num_items].label,
5429 sizeof(imux->items[imux->num_items].label),
5430 "%s %d", label, label_idx);
5432 strlcpy(imux->items[imux->num_items].label, label,
5433 sizeof(imux->items[imux->num_items].label));
5434 imux->items[imux->num_items].index = index;
5438 EXPORT_SYMBOL_GPL(snd_hda_add_imux_item);
5441 * snd_hda_bus_reset - Reset the bus
5442 * @bus: HD-audio bus
5444 void snd_hda_bus_reset(struct hda_bus *bus)
5446 struct hda_codec *codec;
5448 list_for_each_entry(codec, &bus->codec_list, list) {
5449 /* FIXME: maybe a better way needed for forced reset */
5450 cancel_delayed_work_sync(&codec->jackpoll_work);
5452 if (hda_codec_is_power_on(codec)) {
5453 hda_call_codec_suspend(codec);
5454 hda_call_codec_resume(codec);
5459 EXPORT_SYMBOL_GPL(snd_hda_bus_reset);
5466 * snd_array_new - get a new element from the given array
5467 * @array: the array object
5469 * Get a new element from the given array. If it exceeds the
5470 * pre-allocated array size, re-allocate the array.
5472 * Returns NULL if allocation failed.
5474 void *snd_array_new(struct snd_array *array)
5476 if (snd_BUG_ON(!array->elem_size))
5478 if (array->used >= array->alloced) {
5479 int num = array->alloced + array->alloc_align;
5480 int size = (num + 1) * array->elem_size;
5482 if (snd_BUG_ON(num >= 4096))
5484 nlist = krealloc(array->list, size, GFP_KERNEL | __GFP_ZERO);
5487 array->list = nlist;
5488 array->alloced = num;
5490 return snd_array_elem(array, array->used++);
5492 EXPORT_SYMBOL_GPL(snd_array_new);
5495 * snd_array_free - free the given array elements
5496 * @array: the array object
5498 void snd_array_free(struct snd_array *array)
5505 EXPORT_SYMBOL_GPL(snd_array_free);
5508 * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer
5509 * @pcm: PCM caps bits
5510 * @buf: the string buffer to write
5511 * @buflen: the max buffer length
5513 * used by hda_proc.c and hda_eld.c
5515 void snd_print_pcm_bits(int pcm, char *buf, int buflen)
5517 static unsigned int bits[] = { 8, 16, 20, 24, 32 };
5520 for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
5521 if (pcm & (AC_SUPPCM_BITS_8 << i))
5522 j += snprintf(buf + j, buflen - j, " %d", bits[i]);
5524 buf[j] = '\0'; /* necessary when j == 0 */
5526 EXPORT_SYMBOL_GPL(snd_print_pcm_bits);
5528 MODULE_DESCRIPTION("HDA codec core");
5529 MODULE_LICENSE("GPL");