2 * Universal Interface for Intel High Definition Audio Codec
4 * Copyright (c) 2004 Takashi Iwai <tiwai@suse.de>
7 * This driver is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This driver is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include <linux/init.h>
24 #include <linux/delay.h>
25 #include <linux/slab.h>
26 #include <linux/mutex.h>
27 #include <linux/module.h>
28 #include <linux/async.h>
29 #include <sound/core.h>
30 #include "hda_codec.h"
31 #include <sound/asoundef.h>
32 #include <sound/tlv.h>
33 #include <sound/initval.h>
34 #include <sound/jack.h>
35 #include "hda_local.h"
38 #include <sound/hda_hwdep.h>
40 #define CREATE_TRACE_POINTS
41 #include "hda_trace.h"
44 #define codec_in_pm(codec) ((codec)->in_pm)
45 static void hda_power_work(struct work_struct *work);
46 static void hda_keep_power_on(struct hda_codec *codec);
47 #define hda_codec_is_power_on(codec) ((codec)->power_on)
49 static void hda_call_pm_notify(struct hda_codec *codec, bool power_up)
51 struct hda_bus *bus = codec->bus;
53 if ((power_up && codec->pm_up_notified) ||
54 (!power_up && !codec->pm_up_notified))
56 if (bus->ops.pm_notify)
57 bus->ops.pm_notify(bus, power_up);
58 codec->pm_up_notified = power_up;
62 #define codec_in_pm(codec) 0
63 static inline void hda_keep_power_on(struct hda_codec *codec) {}
64 #define hda_codec_is_power_on(codec) 1
65 #define hda_call_pm_notify(codec, state) {}
69 * snd_hda_get_jack_location - Give a location string of the jack
70 * @cfg: pin default config value
72 * Parse the pin default config value and returns the string of the
73 * jack location, e.g. "Rear", "Front", etc.
75 const char *snd_hda_get_jack_location(u32 cfg)
77 static char *bases[7] = {
78 "N/A", "Rear", "Front", "Left", "Right", "Top", "Bottom",
80 static unsigned char specials_idx[] = {
85 static char *specials[] = {
86 "Rear Panel", "Drive Bar",
87 "Riser", "HDMI", "ATAPI",
88 "Mobile-In", "Mobile-Out"
91 cfg = (cfg & AC_DEFCFG_LOCATION) >> AC_DEFCFG_LOCATION_SHIFT;
93 return bases[cfg & 0x0f];
94 for (i = 0; i < ARRAY_SIZE(specials_idx); i++) {
95 if (cfg == specials_idx[i])
100 EXPORT_SYMBOL_GPL(snd_hda_get_jack_location);
103 * snd_hda_get_jack_connectivity - Give a connectivity string of the jack
104 * @cfg: pin default config value
106 * Parse the pin default config value and returns the string of the
107 * jack connectivity, i.e. external or internal connection.
109 const char *snd_hda_get_jack_connectivity(u32 cfg)
111 static char *jack_locations[4] = { "Ext", "Int", "Sep", "Oth" };
113 return jack_locations[(cfg >> (AC_DEFCFG_LOCATION_SHIFT + 4)) & 3];
115 EXPORT_SYMBOL_GPL(snd_hda_get_jack_connectivity);
118 * snd_hda_get_jack_type - Give a type string of the jack
119 * @cfg: pin default config value
121 * Parse the pin default config value and returns the string of the
122 * jack type, i.e. the purpose of the jack, such as Line-Out or CD.
124 const char *snd_hda_get_jack_type(u32 cfg)
126 static char *jack_types[16] = {
127 "Line Out", "Speaker", "HP Out", "CD",
128 "SPDIF Out", "Digital Out", "Modem Line", "Modem Hand",
129 "Line In", "Aux", "Mic", "Telephony",
130 "SPDIF In", "Digital In", "Reserved", "Other"
133 return jack_types[(cfg & AC_DEFCFG_DEVICE)
134 >> AC_DEFCFG_DEVICE_SHIFT];
136 EXPORT_SYMBOL_GPL(snd_hda_get_jack_type);
139 * Compose a 32bit command word to be sent to the HD-audio controller
141 static inline unsigned int
142 make_codec_cmd(struct hda_codec *codec, hda_nid_t nid, int flags,
143 unsigned int verb, unsigned int parm)
147 if ((codec->addr & ~0xf) || (nid & ~0x7f) ||
148 (verb & ~0xfff) || (parm & ~0xffff)) {
149 codec_err(codec, "hda-codec: out of range cmd %x:%x:%x:%x\n",
150 codec->addr, nid, verb, parm);
154 val = (u32)codec->addr << 28;
155 val |= (u32)nid << 20;
162 * Send and receive a verb
164 static int codec_exec_verb(struct hda_codec *codec, unsigned int cmd,
165 int flags, unsigned int *res)
167 struct hda_bus *bus = codec->bus;
176 snd_hda_power_up(codec);
177 mutex_lock(&bus->cmd_mutex);
178 if (flags & HDA_RW_NO_RESPONSE_FALLBACK)
179 bus->no_response_fallback = 1;
181 trace_hda_send_cmd(codec, cmd);
182 err = bus->ops.command(bus, cmd);
185 /* process pending verbs */
186 bus->ops.get_response(bus, codec->addr);
189 *res = bus->ops.get_response(bus, codec->addr);
190 trace_hda_get_response(codec, *res);
192 bus->no_response_fallback = 0;
193 mutex_unlock(&bus->cmd_mutex);
194 snd_hda_power_down(codec);
195 if (!codec_in_pm(codec) && res && *res == -1 && bus->rirb_error) {
196 if (bus->response_reset) {
198 "resetting BUS due to fatal communication error\n");
199 trace_hda_bus_reset(bus);
200 bus->ops.bus_reset(bus);
204 /* clear reset-flag when the communication gets recovered */
205 if (!err || codec_in_pm(codec))
206 bus->response_reset = 0;
211 * snd_hda_codec_read - send a command and get the response
212 * @codec: the HDA codec
213 * @nid: NID to send the command
214 * @flags: optional bit flags
215 * @verb: the verb to send
216 * @parm: the parameter for the verb
218 * Send a single command and read the corresponding response.
220 * Returns the obtained response value, or -1 for an error.
222 unsigned int snd_hda_codec_read(struct hda_codec *codec, hda_nid_t nid,
224 unsigned int verb, unsigned int parm)
226 unsigned cmd = make_codec_cmd(codec, nid, flags, verb, parm);
228 if (codec_exec_verb(codec, cmd, flags, &res))
232 EXPORT_SYMBOL_GPL(snd_hda_codec_read);
235 * snd_hda_codec_write - send a single command without waiting for response
236 * @codec: the HDA codec
237 * @nid: NID to send the command
238 * @flags: optional bit flags
239 * @verb: the verb to send
240 * @parm: the parameter for the verb
242 * Send a single command without waiting for response.
244 * Returns 0 if successful, or a negative error code.
246 int snd_hda_codec_write(struct hda_codec *codec, hda_nid_t nid, int flags,
247 unsigned int verb, unsigned int parm)
249 unsigned int cmd = make_codec_cmd(codec, nid, flags, verb, parm);
251 return codec_exec_verb(codec, cmd, flags,
252 codec->bus->sync_write ? &res : NULL);
254 EXPORT_SYMBOL_GPL(snd_hda_codec_write);
257 * snd_hda_sequence_write - sequence writes
258 * @codec: the HDA codec
259 * @seq: VERB array to send
261 * Send the commands sequentially from the given array.
262 * The array must be terminated with NID=0.
264 void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq)
266 for (; seq->nid; seq++)
267 snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param);
269 EXPORT_SYMBOL_GPL(snd_hda_sequence_write);
272 * snd_hda_get_sub_nodes - get the range of sub nodes
273 * @codec: the HDA codec
275 * @start_id: the pointer to store the start NID
277 * Parse the NID and store the start NID of its sub-nodes.
278 * Returns the number of sub-nodes.
280 int snd_hda_get_sub_nodes(struct hda_codec *codec, hda_nid_t nid,
285 parm = snd_hda_param_read(codec, nid, AC_PAR_NODE_COUNT);
290 *start_id = (parm >> 16) & 0x7fff;
291 return (int)(parm & 0x7fff);
293 EXPORT_SYMBOL_GPL(snd_hda_get_sub_nodes);
295 /* connection list element */
296 struct hda_conn_list {
297 struct list_head list;
303 /* look up the cached results */
304 static struct hda_conn_list *
305 lookup_conn_list(struct hda_codec *codec, hda_nid_t nid)
307 struct hda_conn_list *p;
308 list_for_each_entry(p, &codec->conn_list, list) {
315 static int add_conn_list(struct hda_codec *codec, hda_nid_t nid, int len,
316 const hda_nid_t *list)
318 struct hda_conn_list *p;
320 p = kmalloc(sizeof(*p) + len * sizeof(hda_nid_t), GFP_KERNEL);
325 memcpy(p->conns, list, len * sizeof(hda_nid_t));
326 list_add(&p->list, &codec->conn_list);
330 static void remove_conn_list(struct hda_codec *codec)
332 while (!list_empty(&codec->conn_list)) {
333 struct hda_conn_list *p;
334 p = list_first_entry(&codec->conn_list, typeof(*p), list);
340 /* read the connection and add to the cache */
341 static int read_and_add_raw_conns(struct hda_codec *codec, hda_nid_t nid)
344 hda_nid_t *result = list;
347 len = snd_hda_get_raw_connections(codec, nid, list, ARRAY_SIZE(list));
348 if (len == -ENOSPC) {
349 len = snd_hda_get_num_raw_conns(codec, nid);
350 result = kmalloc(sizeof(hda_nid_t) * len, GFP_KERNEL);
353 len = snd_hda_get_raw_connections(codec, nid, result, len);
356 len = snd_hda_override_conn_list(codec, nid, len, result);
363 * snd_hda_get_conn_list - get connection list
364 * @codec: the HDA codec
366 * @listp: the pointer to store NID list
368 * Parses the connection list of the given widget and stores the pointer
369 * to the list of NIDs.
371 * Returns the number of connections, or a negative error code.
373 * Note that the returned pointer isn't protected against the list
374 * modification. If snd_hda_override_conn_list() might be called
375 * concurrently, protect with a mutex appropriately.
377 int snd_hda_get_conn_list(struct hda_codec *codec, hda_nid_t nid,
378 const hda_nid_t **listp)
384 const struct hda_conn_list *p;
386 /* if the connection-list is already cached, read it */
387 p = lookup_conn_list(codec, nid);
393 if (snd_BUG_ON(added))
396 err = read_and_add_raw_conns(codec, nid);
402 EXPORT_SYMBOL_GPL(snd_hda_get_conn_list);
405 * snd_hda_get_connections - copy connection list
406 * @codec: the HDA codec
408 * @conn_list: connection list array; when NULL, checks only the size
409 * @max_conns: max. number of connections to store
411 * Parses the connection list of the given widget and stores the list
414 * Returns the number of connections, or a negative error code.
416 int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
417 hda_nid_t *conn_list, int max_conns)
419 const hda_nid_t *list;
420 int len = snd_hda_get_conn_list(codec, nid, &list);
422 if (len > 0 && conn_list) {
423 if (len > max_conns) {
424 codec_err(codec, "Too many connections %d for NID 0x%x\n",
428 memcpy(conn_list, list, len * sizeof(hda_nid_t));
433 EXPORT_SYMBOL_GPL(snd_hda_get_connections);
435 /* return CONNLIST_LEN parameter of the given widget */
436 static unsigned int get_num_conns(struct hda_codec *codec, hda_nid_t nid)
438 unsigned int wcaps = get_wcaps(codec, nid);
441 if (!(wcaps & AC_WCAP_CONN_LIST) &&
442 get_wcaps_type(wcaps) != AC_WID_VOL_KNB)
445 parm = snd_hda_param_read(codec, nid, AC_PAR_CONNLIST_LEN);
451 int snd_hda_get_num_raw_conns(struct hda_codec *codec, hda_nid_t nid)
453 return snd_hda_get_raw_connections(codec, nid, NULL, 0);
457 * snd_hda_get_raw_connections - copy connection list without cache
458 * @codec: the HDA codec
460 * @conn_list: connection list array
461 * @max_conns: max. number of connections to store
463 * Like snd_hda_get_connections(), copy the connection list but without
464 * checking through the connection-list cache.
465 * Currently called only from hda_proc.c, so not exported.
467 int snd_hda_get_raw_connections(struct hda_codec *codec, hda_nid_t nid,
468 hda_nid_t *conn_list, int max_conns)
471 int i, conn_len, conns;
472 unsigned int shift, num_elems, mask;
476 parm = get_num_conns(codec, nid);
480 if (parm & AC_CLIST_LONG) {
489 conn_len = parm & AC_CLIST_LENGTH;
490 mask = (1 << (shift-1)) - 1;
493 return 0; /* no connection */
496 /* single connection */
497 parm = snd_hda_codec_read(codec, nid, 0,
498 AC_VERB_GET_CONNECT_LIST, 0);
499 if (parm == -1 && codec->bus->rirb_error)
502 conn_list[0] = parm & mask;
506 /* multi connection */
509 for (i = 0; i < conn_len; i++) {
513 if (i % num_elems == 0) {
514 parm = snd_hda_codec_read(codec, nid, 0,
515 AC_VERB_GET_CONNECT_LIST, i);
516 if (parm == -1 && codec->bus->rirb_error)
519 range_val = !!(parm & (1 << (shift-1))); /* ranges */
521 if (val == 0 && null_count++) { /* no second chance */
523 "invalid CONNECT_LIST verb %x[%i]:%x\n",
529 /* ranges between the previous and this one */
530 if (!prev_nid || prev_nid >= val) {
532 "invalid dep_range_val %x:%x\n",
536 for (n = prev_nid + 1; n <= val; n++) {
538 if (conns >= max_conns)
540 conn_list[conns] = n;
546 if (conns >= max_conns)
548 conn_list[conns] = val;
558 * snd_hda_override_conn_list - add/modify the connection-list to cache
559 * @codec: the HDA codec
561 * @len: number of connection list entries
562 * @list: the list of connection entries
564 * Add or modify the given connection-list to the cache. If the corresponding
565 * cache already exists, invalidate it and append a new one.
567 * Returns zero or a negative error code.
569 int snd_hda_override_conn_list(struct hda_codec *codec, hda_nid_t nid, int len,
570 const hda_nid_t *list)
572 struct hda_conn_list *p;
574 p = lookup_conn_list(codec, nid);
580 return add_conn_list(codec, nid, len, list);
582 EXPORT_SYMBOL_GPL(snd_hda_override_conn_list);
585 * snd_hda_get_conn_index - get the connection index of the given NID
586 * @codec: the HDA codec
587 * @mux: NID containing the list
588 * @nid: NID to select
589 * @recursive: 1 when searching NID recursively, otherwise 0
591 * Parses the connection list of the widget @mux and checks whether the
592 * widget @nid is present. If it is, return the connection index.
593 * Otherwise it returns -1.
595 int snd_hda_get_conn_index(struct hda_codec *codec, hda_nid_t mux,
596 hda_nid_t nid, int recursive)
598 const hda_nid_t *conn;
601 nums = snd_hda_get_conn_list(codec, mux, &conn);
602 for (i = 0; i < nums; i++)
607 if (recursive > 10) {
608 codec_dbg(codec, "too deep connection for 0x%x\n", nid);
612 for (i = 0; i < nums; i++) {
613 unsigned int type = get_wcaps_type(get_wcaps(codec, conn[i]));
614 if (type == AC_WID_PIN || type == AC_WID_AUD_OUT)
616 if (snd_hda_get_conn_index(codec, conn[i], nid, recursive) >= 0)
621 EXPORT_SYMBOL_GPL(snd_hda_get_conn_index);
624 /* return DEVLIST_LEN parameter of the given widget */
625 static unsigned int get_num_devices(struct hda_codec *codec, hda_nid_t nid)
627 unsigned int wcaps = get_wcaps(codec, nid);
630 if (!codec->dp_mst || !(wcaps & AC_WCAP_DIGITAL) ||
631 get_wcaps_type(wcaps) != AC_WID_PIN)
634 parm = snd_hda_param_read(codec, nid, AC_PAR_DEVLIST_LEN);
635 if (parm == -1 && codec->bus->rirb_error)
637 return parm & AC_DEV_LIST_LEN_MASK;
641 * snd_hda_get_devices - copy device list without cache
642 * @codec: the HDA codec
643 * @nid: NID of the pin to parse
644 * @dev_list: device list array
645 * @max_devices: max. number of devices to store
647 * Copy the device list. This info is dynamic and so not cached.
648 * Currently called only from hda_proc.c, so not exported.
650 int snd_hda_get_devices(struct hda_codec *codec, hda_nid_t nid,
651 u8 *dev_list, int max_devices)
654 int i, dev_len, devices;
656 parm = get_num_devices(codec, nid);
657 if (!parm) /* not multi-stream capable */
661 dev_len = dev_len < max_devices ? dev_len : max_devices;
664 while (devices < dev_len) {
665 parm = snd_hda_codec_read(codec, nid, 0,
666 AC_VERB_GET_DEVICE_LIST, devices);
667 if (parm == -1 && codec->bus->rirb_error)
670 for (i = 0; i < 8; i++) {
671 dev_list[devices] = (u8)parm;
674 if (devices >= dev_len)
682 * snd_hda_queue_unsol_event - add an unsolicited event to queue
684 * @res: unsolicited event (lower 32bit of RIRB entry)
685 * @res_ex: codec addr and flags (upper 32bit or RIRB entry)
687 * Adds the given event to the queue. The events are processed in
688 * the workqueue asynchronously. Call this function in the interrupt
689 * hanlder when RIRB receives an unsolicited event.
691 * Returns 0 if successful, or a negative error code.
693 int snd_hda_queue_unsol_event(struct hda_bus *bus, u32 res, u32 res_ex)
695 struct hda_bus_unsolicited *unsol;
698 if (!bus || !bus->workq)
701 trace_hda_unsol_event(bus, res, res_ex);
703 wp = (unsol->wp + 1) % HDA_UNSOL_QUEUE_SIZE;
707 unsol->queue[wp] = res;
708 unsol->queue[wp + 1] = res_ex;
710 queue_work(bus->workq, &unsol->work);
714 EXPORT_SYMBOL_GPL(snd_hda_queue_unsol_event);
717 * process queued unsolicited events
719 static void process_unsol_events(struct work_struct *work)
721 struct hda_bus *bus = container_of(work, struct hda_bus, unsol.work);
722 struct hda_bus_unsolicited *unsol = &bus->unsol;
723 struct hda_codec *codec;
724 unsigned int rp, caddr, res;
726 while (unsol->rp != unsol->wp) {
727 rp = (unsol->rp + 1) % HDA_UNSOL_QUEUE_SIZE;
730 res = unsol->queue[rp];
731 caddr = unsol->queue[rp + 1];
732 if (!(caddr & (1 << 4))) /* no unsolicited event? */
734 codec = bus->caddr_tbl[caddr & 0x0f];
735 if (codec && codec->patch_ops.unsol_event)
736 codec->patch_ops.unsol_event(codec, res);
743 static void snd_hda_bus_free(struct hda_bus *bus)
748 WARN_ON(!list_empty(&bus->codec_list));
750 flush_workqueue(bus->workq);
751 if (bus->ops.private_free)
752 bus->ops.private_free(bus);
754 destroy_workqueue(bus->workq);
759 static int snd_hda_bus_dev_free(struct snd_device *device)
761 snd_hda_bus_free(device->device_data);
765 static int snd_hda_bus_dev_disconnect(struct snd_device *device)
767 struct hda_bus *bus = device->device_data;
773 * snd_hda_bus_new - create a HDA bus
774 * @card: the card entry
775 * @busp: the pointer to store the created bus instance
777 * Returns 0 if successful, or a negative error code.
779 int snd_hda_bus_new(struct snd_card *card,
780 struct hda_bus **busp)
784 static struct snd_device_ops dev_ops = {
785 .dev_disconnect = snd_hda_bus_dev_disconnect,
786 .dev_free = snd_hda_bus_dev_free,
792 bus = kzalloc(sizeof(*bus), GFP_KERNEL);
794 dev_err(card->dev, "can't allocate struct hda_bus\n");
799 mutex_init(&bus->cmd_mutex);
800 mutex_init(&bus->prepare_mutex);
801 INIT_LIST_HEAD(&bus->codec_list);
802 INIT_WORK(&bus->unsol.work, process_unsol_events);
804 snprintf(bus->workq_name, sizeof(bus->workq_name),
805 "hd-audio%d", card->number);
806 bus->workq = create_singlethread_workqueue(bus->workq_name);
808 dev_err(card->dev, "cannot create workqueue %s\n",
814 err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops);
816 snd_hda_bus_free(bus);
823 EXPORT_SYMBOL_GPL(snd_hda_bus_new);
826 * look for an AFG and MFG nodes
828 static void setup_fg_nodes(struct hda_codec *codec)
830 int i, total_nodes, function_id;
833 total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid);
834 for (i = 0; i < total_nodes; i++, nid++) {
835 function_id = snd_hda_param_read(codec, nid,
836 AC_PAR_FUNCTION_TYPE);
837 switch (function_id & 0xff) {
838 case AC_GRP_AUDIO_FUNCTION:
840 codec->afg_function_id = function_id & 0xff;
841 codec->afg_unsol = (function_id >> 8) & 1;
843 case AC_GRP_MODEM_FUNCTION:
845 codec->mfg_function_id = function_id & 0xff;
846 codec->mfg_unsol = (function_id >> 8) & 1;
855 * read widget caps for each widget and store in cache
857 static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
862 codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node,
864 codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL);
867 nid = codec->start_nid;
868 for (i = 0; i < codec->num_nodes; i++, nid++)
869 codec->wcaps[i] = snd_hda_param_read(codec, nid,
870 AC_PAR_AUDIO_WIDGET_CAP);
874 /* read all pin default configurations and save codec->init_pins */
875 static int read_pin_defaults(struct hda_codec *codec)
878 hda_nid_t nid = codec->start_nid;
880 for (i = 0; i < codec->num_nodes; i++, nid++) {
881 struct hda_pincfg *pin;
882 unsigned int wcaps = get_wcaps(codec, nid);
883 unsigned int wid_type = get_wcaps_type(wcaps);
884 if (wid_type != AC_WID_PIN)
886 pin = snd_array_new(&codec->init_pins);
890 pin->cfg = snd_hda_codec_read(codec, nid, 0,
891 AC_VERB_GET_CONFIG_DEFAULT, 0);
892 pin->ctrl = snd_hda_codec_read(codec, nid, 0,
893 AC_VERB_GET_PIN_WIDGET_CONTROL,
899 /* look up the given pin config list and return the item matching with NID */
900 static struct hda_pincfg *look_up_pincfg(struct hda_codec *codec,
901 struct snd_array *array,
905 for (i = 0; i < array->used; i++) {
906 struct hda_pincfg *pin = snd_array_elem(array, i);
913 /* set the current pin config value for the given NID.
914 * the value is cached, and read via snd_hda_codec_get_pincfg()
916 int snd_hda_add_pincfg(struct hda_codec *codec, struct snd_array *list,
917 hda_nid_t nid, unsigned int cfg)
919 struct hda_pincfg *pin;
921 /* the check below may be invalid when pins are added by a fixup
922 * dynamically (e.g. via snd_hda_codec_update_widgets()), so disabled
926 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN)
930 pin = look_up_pincfg(codec, list, nid);
932 pin = snd_array_new(list);
942 * snd_hda_codec_set_pincfg - Override a pin default configuration
943 * @codec: the HDA codec
944 * @nid: NID to set the pin config
945 * @cfg: the pin default config value
947 * Override a pin default configuration value in the cache.
948 * This value can be read by snd_hda_codec_get_pincfg() in a higher
949 * priority than the real hardware value.
951 int snd_hda_codec_set_pincfg(struct hda_codec *codec,
952 hda_nid_t nid, unsigned int cfg)
954 return snd_hda_add_pincfg(codec, &codec->driver_pins, nid, cfg);
956 EXPORT_SYMBOL_GPL(snd_hda_codec_set_pincfg);
959 * snd_hda_codec_get_pincfg - Obtain a pin-default configuration
960 * @codec: the HDA codec
961 * @nid: NID to get the pin config
963 * Get the current pin config value of the given pin NID.
964 * If the pincfg value is cached or overridden via sysfs or driver,
965 * returns the cached value.
967 unsigned int snd_hda_codec_get_pincfg(struct hda_codec *codec, hda_nid_t nid)
969 struct hda_pincfg *pin;
971 #ifdef CONFIG_SND_HDA_RECONFIG
973 unsigned int cfg = 0;
974 mutex_lock(&codec->user_mutex);
975 pin = look_up_pincfg(codec, &codec->user_pins, nid);
978 mutex_unlock(&codec->user_mutex);
983 pin = look_up_pincfg(codec, &codec->driver_pins, nid);
986 pin = look_up_pincfg(codec, &codec->init_pins, nid);
991 EXPORT_SYMBOL_GPL(snd_hda_codec_get_pincfg);
994 * snd_hda_codec_set_pin_target - remember the current pinctl target value
995 * @codec: the HDA codec
997 * @val: assigned pinctl value
999 * This function stores the given value to a pinctl target value in the
1000 * pincfg table. This isn't always as same as the actually written value
1001 * but can be referred at any time via snd_hda_codec_get_pin_target().
1003 int snd_hda_codec_set_pin_target(struct hda_codec *codec, hda_nid_t nid,
1006 struct hda_pincfg *pin;
1008 pin = look_up_pincfg(codec, &codec->init_pins, nid);
1014 EXPORT_SYMBOL_GPL(snd_hda_codec_set_pin_target);
1017 * snd_hda_codec_get_pin_target - return the current pinctl target value
1018 * @codec: the HDA codec
1021 int snd_hda_codec_get_pin_target(struct hda_codec *codec, hda_nid_t nid)
1023 struct hda_pincfg *pin;
1025 pin = look_up_pincfg(codec, &codec->init_pins, nid);
1030 EXPORT_SYMBOL_GPL(snd_hda_codec_get_pin_target);
1033 * snd_hda_shutup_pins - Shut up all pins
1034 * @codec: the HDA codec
1036 * Clear all pin controls to shup up before suspend for avoiding click noise.
1037 * The controls aren't cached so that they can be resumed properly.
1039 void snd_hda_shutup_pins(struct hda_codec *codec)
1042 /* don't shut up pins when unloading the driver; otherwise it breaks
1043 * the default pin setup at the next load of the driver
1045 if (codec->bus->shutdown)
1047 for (i = 0; i < codec->init_pins.used; i++) {
1048 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1049 /* use read here for syncing after issuing each verb */
1050 snd_hda_codec_read(codec, pin->nid, 0,
1051 AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
1053 codec->pins_shutup = 1;
1055 EXPORT_SYMBOL_GPL(snd_hda_shutup_pins);
1058 /* Restore the pin controls cleared previously via snd_hda_shutup_pins() */
1059 static void restore_shutup_pins(struct hda_codec *codec)
1062 if (!codec->pins_shutup)
1064 if (codec->bus->shutdown)
1066 for (i = 0; i < codec->init_pins.used; i++) {
1067 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1068 snd_hda_codec_write(codec, pin->nid, 0,
1069 AC_VERB_SET_PIN_WIDGET_CONTROL,
1072 codec->pins_shutup = 0;
1076 static void hda_jackpoll_work(struct work_struct *work)
1078 struct hda_codec *codec =
1079 container_of(work, struct hda_codec, jackpoll_work.work);
1081 snd_hda_jack_set_dirty_all(codec);
1082 snd_hda_jack_poll_all(codec);
1084 if (!codec->jackpoll_interval)
1087 queue_delayed_work(codec->bus->workq, &codec->jackpoll_work,
1088 codec->jackpoll_interval);
1091 static void init_hda_cache(struct hda_cache_rec *cache,
1092 unsigned int record_size);
1093 static void free_hda_cache(struct hda_cache_rec *cache);
1095 /* release all pincfg lists */
1096 static void free_init_pincfgs(struct hda_codec *codec)
1098 snd_array_free(&codec->driver_pins);
1099 #ifdef CONFIG_SND_HDA_RECONFIG
1100 snd_array_free(&codec->user_pins);
1102 snd_array_free(&codec->init_pins);
1106 * audio-converter setup caches
1108 struct hda_cvt_setup {
1113 unsigned char active; /* cvt is currently used */
1114 unsigned char dirty; /* setups should be cleared */
1117 /* get or create a cache entry for the given audio converter NID */
1118 static struct hda_cvt_setup *
1119 get_hda_cvt_setup(struct hda_codec *codec, hda_nid_t nid)
1121 struct hda_cvt_setup *p;
1124 for (i = 0; i < codec->cvt_setups.used; i++) {
1125 p = snd_array_elem(&codec->cvt_setups, i);
1129 p = snd_array_new(&codec->cvt_setups);
1138 static void snd_hda_codec_free(struct hda_codec *codec)
1142 cancel_delayed_work_sync(&codec->jackpoll_work);
1143 if (device_is_registered(hda_codec_dev(codec)))
1144 device_del(hda_codec_dev(codec));
1145 snd_hda_jack_tbl_clear(codec);
1146 free_init_pincfgs(codec);
1148 cancel_delayed_work(&codec->power_work);
1149 flush_workqueue(codec->bus->workq);
1151 list_del(&codec->list);
1152 snd_array_free(&codec->mixers);
1153 snd_array_free(&codec->nids);
1154 snd_array_free(&codec->cvt_setups);
1155 snd_array_free(&codec->spdif_out);
1156 remove_conn_list(codec);
1157 codec->bus->caddr_tbl[codec->addr] = NULL;
1158 hda_call_pm_notify(codec, false); /* cancel leftover refcounts */
1159 snd_hda_sysfs_clear(codec);
1160 free_hda_cache(&codec->amp_cache);
1161 free_hda_cache(&codec->cmd_cache);
1162 kfree(codec->vendor_name);
1163 kfree(codec->chip_name);
1164 kfree(codec->modelname);
1165 kfree(codec->wcaps);
1166 codec->bus->num_codecs--;
1167 put_device(hda_codec_dev(codec));
1170 static bool snd_hda_codec_get_supported_ps(struct hda_codec *codec,
1171 hda_nid_t fg, unsigned int power_state);
1173 static unsigned int hda_set_power_state(struct hda_codec *codec,
1174 unsigned int power_state);
1176 static int snd_hda_codec_dev_register(struct snd_device *device)
1178 struct hda_codec *codec = device->device_data;
1180 snd_hda_register_beep_device(codec);
1184 static int snd_hda_codec_dev_disconnect(struct snd_device *device)
1186 struct hda_codec *codec = device->device_data;
1188 snd_hda_detach_beep_device(codec);
1192 static int snd_hda_codec_dev_free(struct snd_device *device)
1194 snd_hda_codec_free(device->device_data);
1198 /* just free the container */
1199 static void snd_hda_codec_dev_release(struct device *dev)
1201 kfree(dev_to_hda_codec(dev));
1205 * snd_hda_codec_new - create a HDA codec
1206 * @bus: the bus to assign
1207 * @codec_addr: the codec address
1208 * @codecp: the pointer to store the generated codec
1210 * Returns 0 if successful, or a negative error code.
1212 int snd_hda_codec_new(struct hda_bus *bus,
1213 unsigned int codec_addr,
1214 struct hda_codec **codecp)
1216 struct hda_codec *codec;
1221 static struct snd_device_ops dev_ops = {
1222 .dev_register = snd_hda_codec_dev_register,
1223 .dev_disconnect = snd_hda_codec_dev_disconnect,
1224 .dev_free = snd_hda_codec_dev_free,
1227 if (snd_BUG_ON(!bus))
1229 if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS))
1232 if (bus->caddr_tbl[codec_addr]) {
1233 dev_err(bus->card->dev,
1234 "address 0x%x is already occupied\n",
1239 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
1240 if (codec == NULL) {
1241 dev_err(bus->card->dev, "can't allocate struct hda_codec\n");
1245 dev = hda_codec_dev(codec);
1246 device_initialize(dev);
1247 dev->parent = bus->card->dev;
1248 dev->bus = &snd_hda_bus_type;
1249 dev->release = snd_hda_codec_dev_release;
1250 dev->groups = snd_hda_dev_attr_groups;
1251 dev_set_name(dev, "hdaudioC%dD%d", bus->card->number, codec_addr);
1252 dev_set_drvdata(dev, codec); /* for sysfs */
1255 codec->addr = codec_addr;
1256 mutex_init(&codec->spdif_mutex);
1257 mutex_init(&codec->control_mutex);
1258 mutex_init(&codec->hash_mutex);
1259 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
1260 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
1261 snd_array_init(&codec->mixers, sizeof(struct hda_nid_item), 32);
1262 snd_array_init(&codec->nids, sizeof(struct hda_nid_item), 32);
1263 snd_array_init(&codec->init_pins, sizeof(struct hda_pincfg), 16);
1264 snd_array_init(&codec->driver_pins, sizeof(struct hda_pincfg), 16);
1265 snd_array_init(&codec->cvt_setups, sizeof(struct hda_cvt_setup), 8);
1266 snd_array_init(&codec->spdif_out, sizeof(struct hda_spdif_out), 16);
1267 snd_array_init(&codec->jacktbl, sizeof(struct hda_jack_tbl), 16);
1268 snd_array_init(&codec->verbs, sizeof(struct hda_verb *), 8);
1269 INIT_LIST_HEAD(&codec->conn_list);
1271 INIT_DELAYED_WORK(&codec->jackpoll_work, hda_jackpoll_work);
1272 codec->depop_delay = -1;
1273 codec->fixup_id = HDA_FIXUP_ID_NOT_SET;
1276 spin_lock_init(&codec->power_lock);
1277 INIT_DELAYED_WORK(&codec->power_work, hda_power_work);
1278 /* snd_hda_codec_new() marks the codec as power-up, and leave it as is.
1279 * the caller has to power down appropriatley after initialization
1282 hda_keep_power_on(codec);
1285 snd_hda_sysfs_init(codec);
1287 if (codec->bus->modelname) {
1288 codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL);
1289 if (!codec->modelname) {
1295 list_add_tail(&codec->list, &bus->codec_list);
1298 bus->caddr_tbl[codec_addr] = codec;
1300 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1302 if (codec->vendor_id == -1)
1303 /* read again, hopefully the access method was corrected
1304 * in the last read...
1306 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1308 codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1309 AC_PAR_SUBSYSTEM_ID);
1310 codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1313 setup_fg_nodes(codec);
1314 if (!codec->afg && !codec->mfg) {
1315 dev_err(bus->card->dev, "no AFG or MFG node found\n");
1320 fg = codec->afg ? codec->afg : codec->mfg;
1321 err = read_widget_caps(codec, fg);
1323 dev_err(bus->card->dev, "cannot malloc\n");
1326 err = read_pin_defaults(codec);
1330 if (!codec->subsystem_id) {
1331 codec->subsystem_id =
1332 snd_hda_codec_read(codec, fg, 0,
1333 AC_VERB_GET_SUBSYSTEM_ID, 0);
1337 codec->d3_stop_clk = snd_hda_codec_get_supported_ps(codec, fg,
1340 codec->epss = snd_hda_codec_get_supported_ps(codec, fg,
1343 if (!codec->d3_stop_clk || !codec->epss)
1344 bus->power_keep_link_on = 1;
1348 /* power-up all before initialization */
1349 hda_set_power_state(codec, AC_PWRST_D0);
1351 snd_hda_codec_proc_new(codec);
1353 snd_hda_create_hwdep(codec);
1355 sprintf(component, "HDA:%08x,%08x,%08x", codec->vendor_id,
1356 codec->subsystem_id, codec->revision_id);
1357 snd_component_add(codec->bus->card, component);
1359 err = snd_device_new(bus->card, SNDRV_DEV_CODEC, codec, &dev_ops);
1368 snd_hda_codec_free(codec);
1371 EXPORT_SYMBOL_GPL(snd_hda_codec_new);
1374 * snd_hda_codec_update_widgets - Refresh widget caps and pin defaults
1375 * @codec: the HDA codec
1377 * Forcibly refresh the all widget caps and the init pin configurations of
1380 int snd_hda_codec_update_widgets(struct hda_codec *codec)
1385 /* Assume the function group node does not change,
1386 * only the widget nodes may change.
1388 kfree(codec->wcaps);
1389 fg = codec->afg ? codec->afg : codec->mfg;
1390 err = read_widget_caps(codec, fg);
1392 codec_err(codec, "cannot malloc\n");
1396 snd_array_free(&codec->init_pins);
1397 err = read_pin_defaults(codec);
1401 EXPORT_SYMBOL_GPL(snd_hda_codec_update_widgets);
1403 /* update the stream-id if changed */
1404 static void update_pcm_stream_id(struct hda_codec *codec,
1405 struct hda_cvt_setup *p, hda_nid_t nid,
1406 u32 stream_tag, int channel_id)
1408 unsigned int oldval, newval;
1410 if (p->stream_tag != stream_tag || p->channel_id != channel_id) {
1411 oldval = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0);
1412 newval = (stream_tag << 4) | channel_id;
1413 if (oldval != newval)
1414 snd_hda_codec_write(codec, nid, 0,
1415 AC_VERB_SET_CHANNEL_STREAMID,
1417 p->stream_tag = stream_tag;
1418 p->channel_id = channel_id;
1422 /* update the format-id if changed */
1423 static void update_pcm_format(struct hda_codec *codec, struct hda_cvt_setup *p,
1424 hda_nid_t nid, int format)
1426 unsigned int oldval;
1428 if (p->format_id != format) {
1429 oldval = snd_hda_codec_read(codec, nid, 0,
1430 AC_VERB_GET_STREAM_FORMAT, 0);
1431 if (oldval != format) {
1433 snd_hda_codec_write(codec, nid, 0,
1434 AC_VERB_SET_STREAM_FORMAT,
1437 p->format_id = format;
1442 * snd_hda_codec_setup_stream - set up the codec for streaming
1443 * @codec: the CODEC to set up
1444 * @nid: the NID to set up
1445 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
1446 * @channel_id: channel id to pass, zero based.
1447 * @format: stream format.
1449 void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
1451 int channel_id, int format)
1453 struct hda_codec *c;
1454 struct hda_cvt_setup *p;
1462 "hda_codec_setup_stream: NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
1463 nid, stream_tag, channel_id, format);
1464 p = get_hda_cvt_setup(codec, nid);
1468 if (codec->pcm_format_first)
1469 update_pcm_format(codec, p, nid, format);
1470 update_pcm_stream_id(codec, p, nid, stream_tag, channel_id);
1471 if (!codec->pcm_format_first)
1472 update_pcm_format(codec, p, nid, format);
1477 /* make other inactive cvts with the same stream-tag dirty */
1478 type = get_wcaps_type(get_wcaps(codec, nid));
1479 list_for_each_entry(c, &codec->bus->codec_list, list) {
1480 for (i = 0; i < c->cvt_setups.used; i++) {
1481 p = snd_array_elem(&c->cvt_setups, i);
1482 if (!p->active && p->stream_tag == stream_tag &&
1483 get_wcaps_type(get_wcaps(c, p->nid)) == type)
1488 EXPORT_SYMBOL_GPL(snd_hda_codec_setup_stream);
1490 static void really_cleanup_stream(struct hda_codec *codec,
1491 struct hda_cvt_setup *q);
1494 * __snd_hda_codec_cleanup_stream - clean up the codec for closing
1495 * @codec: the CODEC to clean up
1496 * @nid: the NID to clean up
1497 * @do_now: really clean up the stream instead of clearing the active flag
1499 void __snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid,
1502 struct hda_cvt_setup *p;
1507 if (codec->no_sticky_stream)
1510 codec_dbg(codec, "hda_codec_cleanup_stream: NID=0x%x\n", nid);
1511 p = get_hda_cvt_setup(codec, nid);
1513 /* here we just clear the active flag when do_now isn't set;
1514 * actual clean-ups will be done later in
1515 * purify_inactive_streams() called from snd_hda_codec_prpapre()
1518 really_cleanup_stream(codec, p);
1523 EXPORT_SYMBOL_GPL(__snd_hda_codec_cleanup_stream);
1525 static void really_cleanup_stream(struct hda_codec *codec,
1526 struct hda_cvt_setup *q)
1528 hda_nid_t nid = q->nid;
1529 if (q->stream_tag || q->channel_id)
1530 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
1532 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0
1534 memset(q, 0, sizeof(*q));
1538 /* clean up the all conflicting obsolete streams */
1539 static void purify_inactive_streams(struct hda_codec *codec)
1541 struct hda_codec *c;
1544 list_for_each_entry(c, &codec->bus->codec_list, list) {
1545 for (i = 0; i < c->cvt_setups.used; i++) {
1546 struct hda_cvt_setup *p;
1547 p = snd_array_elem(&c->cvt_setups, i);
1549 really_cleanup_stream(c, p);
1555 /* clean up all streams; called from suspend */
1556 static void hda_cleanup_all_streams(struct hda_codec *codec)
1560 for (i = 0; i < codec->cvt_setups.used; i++) {
1561 struct hda_cvt_setup *p = snd_array_elem(&codec->cvt_setups, i);
1563 really_cleanup_stream(codec, p);
1569 * amp access functions
1572 /* FIXME: more better hash key? */
1573 #define HDA_HASH_KEY(nid, dir, idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
1574 #define HDA_HASH_PINCAP_KEY(nid) (u32)((nid) + (0x02 << 24))
1575 #define HDA_HASH_PARPCM_KEY(nid) (u32)((nid) + (0x03 << 24))
1576 #define HDA_HASH_PARSTR_KEY(nid) (u32)((nid) + (0x04 << 24))
1577 #define INFO_AMP_CAPS (1<<0)
1578 #define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
1580 /* initialize the hash table */
1581 static void init_hda_cache(struct hda_cache_rec *cache,
1582 unsigned int record_size)
1584 memset(cache, 0, sizeof(*cache));
1585 memset(cache->hash, 0xff, sizeof(cache->hash));
1586 snd_array_init(&cache->buf, record_size, 64);
1589 static void free_hda_cache(struct hda_cache_rec *cache)
1591 snd_array_free(&cache->buf);
1594 /* query the hash. allocate an entry if not found. */
1595 static struct hda_cache_head *get_hash(struct hda_cache_rec *cache, u32 key)
1597 u16 idx = key % (u16)ARRAY_SIZE(cache->hash);
1598 u16 cur = cache->hash[idx];
1599 struct hda_cache_head *info;
1601 while (cur != 0xffff) {
1602 info = snd_array_elem(&cache->buf, cur);
1603 if (info->key == key)
1610 /* query the hash. allocate an entry if not found. */
1611 static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache,
1614 struct hda_cache_head *info = get_hash(cache, key);
1617 /* add a new hash entry */
1618 info = snd_array_new(&cache->buf);
1621 cur = snd_array_index(&cache->buf, info);
1625 idx = key % (u16)ARRAY_SIZE(cache->hash);
1626 info->next = cache->hash[idx];
1627 cache->hash[idx] = cur;
1632 /* query and allocate an amp hash entry */
1633 static inline struct hda_amp_info *
1634 get_alloc_amp_hash(struct hda_codec *codec, u32 key)
1636 return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key);
1639 /* overwrite the value with the key in the caps hash */
1640 static int write_caps_hash(struct hda_codec *codec, u32 key, unsigned int val)
1642 struct hda_amp_info *info;
1644 mutex_lock(&codec->hash_mutex);
1645 info = get_alloc_amp_hash(codec, key);
1647 mutex_unlock(&codec->hash_mutex);
1650 info->amp_caps = val;
1651 info->head.val |= INFO_AMP_CAPS;
1652 mutex_unlock(&codec->hash_mutex);
1656 /* query the value from the caps hash; if not found, fetch the current
1657 * value from the given function and store in the hash
1660 query_caps_hash(struct hda_codec *codec, hda_nid_t nid, int dir, u32 key,
1661 unsigned int (*func)(struct hda_codec *, hda_nid_t, int))
1663 struct hda_amp_info *info;
1666 mutex_lock(&codec->hash_mutex);
1667 info = get_alloc_amp_hash(codec, key);
1669 mutex_unlock(&codec->hash_mutex);
1672 if (!(info->head.val & INFO_AMP_CAPS)) {
1673 mutex_unlock(&codec->hash_mutex); /* for reentrance */
1674 val = func(codec, nid, dir);
1675 write_caps_hash(codec, key, val);
1677 val = info->amp_caps;
1678 mutex_unlock(&codec->hash_mutex);
1683 static unsigned int read_amp_cap(struct hda_codec *codec, hda_nid_t nid,
1686 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
1688 return snd_hda_param_read(codec, nid,
1689 direction == HDA_OUTPUT ?
1690 AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP);
1694 * query_amp_caps - query AMP capabilities
1695 * @codec: the HD-auio codec
1696 * @nid: the NID to query
1697 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1699 * Query AMP capabilities for the given widget and direction.
1700 * Returns the obtained capability bits.
1702 * When cap bits have been already read, this doesn't read again but
1703 * returns the cached value.
1705 u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
1707 return query_caps_hash(codec, nid, direction,
1708 HDA_HASH_KEY(nid, direction, 0),
1711 EXPORT_SYMBOL_GPL(query_amp_caps);
1714 * snd_hda_check_amp_caps - query AMP capabilities
1715 * @codec: the HD-audio codec
1716 * @nid: the NID to query
1717 * @dir: either #HDA_INPUT or #HDA_OUTPUT
1718 * @bits: bit mask to check the result
1720 * Check whether the widget has the given amp capability for the direction.
1722 bool snd_hda_check_amp_caps(struct hda_codec *codec, hda_nid_t nid,
1723 int dir, unsigned int bits)
1727 if (get_wcaps(codec, nid) & (1 << (dir + 1)))
1728 if (query_amp_caps(codec, nid, dir) & bits)
1732 EXPORT_SYMBOL_GPL(snd_hda_check_amp_caps);
1735 * snd_hda_override_amp_caps - Override the AMP capabilities
1736 * @codec: the CODEC to clean up
1737 * @nid: the NID to clean up
1738 * @dir: either #HDA_INPUT or #HDA_OUTPUT
1739 * @caps: the capability bits to set
1741 * Override the cached AMP caps bits value by the given one.
1742 * This function is useful if the driver needs to adjust the AMP ranges,
1743 * e.g. limit to 0dB, etc.
1745 * Returns zero if successful or a negative error code.
1747 int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
1750 return write_caps_hash(codec, HDA_HASH_KEY(nid, dir, 0), caps);
1752 EXPORT_SYMBOL_GPL(snd_hda_override_amp_caps);
1754 static unsigned int read_pin_cap(struct hda_codec *codec, hda_nid_t nid,
1757 return snd_hda_param_read(codec, nid, AC_PAR_PIN_CAP);
1761 * snd_hda_query_pin_caps - Query PIN capabilities
1762 * @codec: the HD-auio codec
1763 * @nid: the NID to query
1765 * Query PIN capabilities for the given widget.
1766 * Returns the obtained capability bits.
1768 * When cap bits have been already read, this doesn't read again but
1769 * returns the cached value.
1771 u32 snd_hda_query_pin_caps(struct hda_codec *codec, hda_nid_t nid)
1773 return query_caps_hash(codec, nid, 0, HDA_HASH_PINCAP_KEY(nid),
1776 EXPORT_SYMBOL_GPL(snd_hda_query_pin_caps);
1779 * snd_hda_override_pin_caps - Override the pin capabilities
1781 * @nid: the NID to override
1782 * @caps: the capability bits to set
1784 * Override the cached PIN capabilitiy bits value by the given one.
1786 * Returns zero if successful or a negative error code.
1788 int snd_hda_override_pin_caps(struct hda_codec *codec, hda_nid_t nid,
1791 return write_caps_hash(codec, HDA_HASH_PINCAP_KEY(nid), caps);
1793 EXPORT_SYMBOL_GPL(snd_hda_override_pin_caps);
1795 /* read or sync the hash value with the current value;
1796 * call within hash_mutex
1798 static struct hda_amp_info *
1799 update_amp_hash(struct hda_codec *codec, hda_nid_t nid, int ch,
1800 int direction, int index, bool init_only)
1802 struct hda_amp_info *info;
1803 unsigned int parm, val = 0;
1804 bool val_read = false;
1807 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
1810 if (!(info->head.val & INFO_AMP_VOL(ch))) {
1812 mutex_unlock(&codec->hash_mutex);
1813 parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
1814 parm |= direction == HDA_OUTPUT ?
1815 AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
1817 val = snd_hda_codec_read(codec, nid, 0,
1818 AC_VERB_GET_AMP_GAIN_MUTE, parm);
1821 mutex_lock(&codec->hash_mutex);
1824 info->vol[ch] = val;
1825 info->head.val |= INFO_AMP_VOL(ch);
1826 } else if (init_only)
1832 * write the current volume in info to the h/w
1834 static void put_vol_mute(struct hda_codec *codec, unsigned int amp_caps,
1835 hda_nid_t nid, int ch, int direction, int index,
1840 parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
1841 parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
1842 parm |= index << AC_AMP_SET_INDEX_SHIFT;
1843 if ((val & HDA_AMP_MUTE) && !(amp_caps & AC_AMPCAP_MUTE) &&
1844 (amp_caps & AC_AMPCAP_MIN_MUTE))
1845 ; /* set the zero value as a fake mute */
1848 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
1852 * snd_hda_codec_amp_read - Read AMP value
1853 * @codec: HD-audio codec
1854 * @nid: NID to read the AMP value
1855 * @ch: channel (left=0 or right=1)
1856 * @direction: #HDA_INPUT or #HDA_OUTPUT
1857 * @index: the index value (only for input direction)
1859 * Read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
1861 int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
1862 int direction, int index)
1864 struct hda_amp_info *info;
1865 unsigned int val = 0;
1867 mutex_lock(&codec->hash_mutex);
1868 info = update_amp_hash(codec, nid, ch, direction, index, false);
1870 val = info->vol[ch];
1871 mutex_unlock(&codec->hash_mutex);
1874 EXPORT_SYMBOL_GPL(snd_hda_codec_amp_read);
1876 static int codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
1877 int direction, int idx, int mask, int val,
1878 bool init_only, bool cache_only)
1880 struct hda_amp_info *info;
1883 if (snd_BUG_ON(mask & ~0xff))
1887 mutex_lock(&codec->hash_mutex);
1888 info = update_amp_hash(codec, nid, ch, direction, idx, init_only);
1890 mutex_unlock(&codec->hash_mutex);
1893 val |= info->vol[ch] & ~mask;
1894 if (info->vol[ch] == val) {
1895 mutex_unlock(&codec->hash_mutex);
1898 info->vol[ch] = val;
1899 info->head.dirty |= cache_only;
1900 caps = info->amp_caps;
1901 mutex_unlock(&codec->hash_mutex);
1903 put_vol_mute(codec, caps, nid, ch, direction, idx, val);
1908 * snd_hda_codec_amp_update - update the AMP value
1909 * @codec: HD-audio codec
1910 * @nid: NID to read the AMP value
1911 * @ch: channel (left=0 or right=1)
1912 * @direction: #HDA_INPUT or #HDA_OUTPUT
1913 * @idx: the index value (only for input direction)
1914 * @mask: bit mask to set
1915 * @val: the bits value to set
1917 * Update the AMP value with a bit mask.
1918 * Returns 0 if the value is unchanged, 1 if changed.
1920 int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
1921 int direction, int idx, int mask, int val)
1923 return codec_amp_update(codec, nid, ch, direction, idx, mask, val,
1924 false, codec->cached_write);
1926 EXPORT_SYMBOL_GPL(snd_hda_codec_amp_update);
1929 * snd_hda_codec_amp_stereo - update the AMP stereo values
1930 * @codec: HD-audio codec
1931 * @nid: NID to read the AMP value
1932 * @direction: #HDA_INPUT or #HDA_OUTPUT
1933 * @idx: the index value (only for input direction)
1934 * @mask: bit mask to set
1935 * @val: the bits value to set
1937 * Update the AMP values like snd_hda_codec_amp_update(), but for a
1938 * stereo widget with the same mask and value.
1940 int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
1941 int direction, int idx, int mask, int val)
1945 if (snd_BUG_ON(mask & ~0xff))
1947 for (ch = 0; ch < 2; ch++)
1948 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
1952 EXPORT_SYMBOL_GPL(snd_hda_codec_amp_stereo);
1955 * snd_hda_codec_amp_init - initialize the AMP value
1956 * @codec: the HDA codec
1957 * @nid: NID to read the AMP value
1958 * @ch: channel (left=0 or right=1)
1959 * @dir: #HDA_INPUT or #HDA_OUTPUT
1960 * @idx: the index value (only for input direction)
1961 * @mask: bit mask to set
1962 * @val: the bits value to set
1964 * Works like snd_hda_codec_amp_update() but it writes the value only at
1965 * the first access. If the amp was already initialized / updated beforehand,
1966 * this does nothing.
1968 int snd_hda_codec_amp_init(struct hda_codec *codec, hda_nid_t nid, int ch,
1969 int dir, int idx, int mask, int val)
1971 return codec_amp_update(codec, nid, ch, dir, idx, mask, val, true,
1972 codec->cached_write);
1974 EXPORT_SYMBOL_GPL(snd_hda_codec_amp_init);
1977 * snd_hda_codec_amp_init_stereo - initialize the stereo AMP value
1978 * @codec: the HDA codec
1979 * @nid: NID to read the AMP value
1980 * @dir: #HDA_INPUT or #HDA_OUTPUT
1981 * @idx: the index value (only for input direction)
1982 * @mask: bit mask to set
1983 * @val: the bits value to set
1985 * Call snd_hda_codec_amp_init() for both stereo channels.
1987 int snd_hda_codec_amp_init_stereo(struct hda_codec *codec, hda_nid_t nid,
1988 int dir, int idx, int mask, int val)
1992 if (snd_BUG_ON(mask & ~0xff))
1994 for (ch = 0; ch < 2; ch++)
1995 ret |= snd_hda_codec_amp_init(codec, nid, ch, dir,
1999 EXPORT_SYMBOL_GPL(snd_hda_codec_amp_init_stereo);
2002 * snd_hda_codec_resume_amp - Resume all AMP commands from the cache
2003 * @codec: HD-audio codec
2005 * Resume the all amp commands from the cache.
2007 void snd_hda_codec_resume_amp(struct hda_codec *codec)
2011 mutex_lock(&codec->hash_mutex);
2012 codec->cached_write = 0;
2013 for (i = 0; i < codec->amp_cache.buf.used; i++) {
2014 struct hda_amp_info *buffer;
2017 unsigned int idx, dir, ch;
2018 struct hda_amp_info info;
2020 buffer = snd_array_elem(&codec->amp_cache.buf, i);
2021 if (!buffer->head.dirty)
2023 buffer->head.dirty = 0;
2025 key = info.head.key;
2029 idx = (key >> 16) & 0xff;
2030 dir = (key >> 24) & 0xff;
2031 for (ch = 0; ch < 2; ch++) {
2032 if (!(info.head.val & INFO_AMP_VOL(ch)))
2034 mutex_unlock(&codec->hash_mutex);
2035 put_vol_mute(codec, info.amp_caps, nid, ch, dir, idx,
2037 mutex_lock(&codec->hash_mutex);
2040 mutex_unlock(&codec->hash_mutex);
2042 EXPORT_SYMBOL_GPL(snd_hda_codec_resume_amp);
2044 static u32 get_amp_max_value(struct hda_codec *codec, hda_nid_t nid, int dir,
2047 u32 caps = query_amp_caps(codec, nid, dir);
2049 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
2056 * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer
2057 * @kcontrol: referred ctl element
2058 * @uinfo: pointer to get/store the data
2060 * The control element is supposed to have the private_value field
2061 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2063 int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
2064 struct snd_ctl_elem_info *uinfo)
2066 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2067 u16 nid = get_amp_nid(kcontrol);
2068 u8 chs = get_amp_channels(kcontrol);
2069 int dir = get_amp_direction(kcontrol);
2070 unsigned int ofs = get_amp_offset(kcontrol);
2072 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2073 uinfo->count = chs == 3 ? 2 : 1;
2074 uinfo->value.integer.min = 0;
2075 uinfo->value.integer.max = get_amp_max_value(codec, nid, dir, ofs);
2076 if (!uinfo->value.integer.max) {
2078 "num_steps = 0 for NID=0x%x (ctl = %s)\n",
2079 nid, kcontrol->id.name);
2084 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_info);
2087 static inline unsigned int
2088 read_amp_value(struct hda_codec *codec, hda_nid_t nid,
2089 int ch, int dir, int idx, unsigned int ofs)
2092 val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
2093 val &= HDA_AMP_VOLMASK;
2102 update_amp_value(struct hda_codec *codec, hda_nid_t nid,
2103 int ch, int dir, int idx, unsigned int ofs,
2106 unsigned int maxval;
2110 /* ofs = 0: raw max value */
2111 maxval = get_amp_max_value(codec, nid, dir, 0);
2114 return codec_amp_update(codec, nid, ch, dir, idx, HDA_AMP_VOLMASK, val,
2115 false, !hda_codec_is_power_on(codec));
2119 * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume
2120 * @kcontrol: ctl element
2121 * @ucontrol: pointer to get/store the data
2123 * The control element is supposed to have the private_value field
2124 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2126 int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
2127 struct snd_ctl_elem_value *ucontrol)
2129 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2130 hda_nid_t nid = get_amp_nid(kcontrol);
2131 int chs = get_amp_channels(kcontrol);
2132 int dir = get_amp_direction(kcontrol);
2133 int idx = get_amp_index(kcontrol);
2134 unsigned int ofs = get_amp_offset(kcontrol);
2135 long *valp = ucontrol->value.integer.value;
2138 *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs);
2140 *valp = read_amp_value(codec, nid, 1, dir, idx, ofs);
2143 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_get);
2146 * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume
2147 * @kcontrol: ctl element
2148 * @ucontrol: pointer to get/store the data
2150 * The control element is supposed to have the private_value field
2151 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2153 int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
2154 struct snd_ctl_elem_value *ucontrol)
2156 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2157 hda_nid_t nid = get_amp_nid(kcontrol);
2158 int chs = get_amp_channels(kcontrol);
2159 int dir = get_amp_direction(kcontrol);
2160 int idx = get_amp_index(kcontrol);
2161 unsigned int ofs = get_amp_offset(kcontrol);
2162 long *valp = ucontrol->value.integer.value;
2166 change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp);
2170 change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp);
2173 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_put);
2176 * snd_hda_mixer_amp_volume_put - TLV callback for a standard AMP mixer volume
2177 * @kcontrol: ctl element
2178 * @op_flag: operation flag
2179 * @size: byte size of input TLV
2182 * The control element is supposed to have the private_value field
2183 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2185 int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2186 unsigned int size, unsigned int __user *_tlv)
2188 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2189 hda_nid_t nid = get_amp_nid(kcontrol);
2190 int dir = get_amp_direction(kcontrol);
2191 unsigned int ofs = get_amp_offset(kcontrol);
2192 bool min_mute = get_amp_min_mute(kcontrol);
2193 u32 caps, val1, val2;
2195 if (size < 4 * sizeof(unsigned int))
2197 caps = query_amp_caps(codec, nid, dir);
2198 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2199 val2 = (val2 + 1) * 25;
2200 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
2202 val1 = ((int)val1) * ((int)val2);
2203 if (min_mute || (caps & AC_AMPCAP_MIN_MUTE))
2204 val2 |= TLV_DB_SCALE_MUTE;
2205 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
2207 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
2209 if (put_user(val1, _tlv + 2))
2211 if (put_user(val2, _tlv + 3))
2215 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_tlv);
2218 * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control
2219 * @codec: HD-audio codec
2220 * @nid: NID of a reference widget
2221 * @dir: #HDA_INPUT or #HDA_OUTPUT
2222 * @tlv: TLV data to be stored, at least 4 elements
2224 * Set (static) TLV data for a virtual master volume using the AMP caps
2225 * obtained from the reference NID.
2226 * The volume range is recalculated as if the max volume is 0dB.
2228 void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
2234 caps = query_amp_caps(codec, nid, dir);
2235 nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
2236 step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2237 step = (step + 1) * 25;
2238 tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
2239 tlv[1] = 2 * sizeof(unsigned int);
2240 tlv[2] = -nums * step;
2243 EXPORT_SYMBOL_GPL(snd_hda_set_vmaster_tlv);
2245 /* find a mixer control element with the given name */
2246 static struct snd_kcontrol *
2247 find_mixer_ctl(struct hda_codec *codec, const char *name, int dev, int idx)
2249 struct snd_ctl_elem_id id;
2250 memset(&id, 0, sizeof(id));
2251 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
2254 if (snd_BUG_ON(strlen(name) >= sizeof(id.name)))
2256 strcpy(id.name, name);
2257 return snd_ctl_find_id(codec->bus->card, &id);
2261 * snd_hda_find_mixer_ctl - Find a mixer control element with the given name
2262 * @codec: HD-audio codec
2263 * @name: ctl id name string
2265 * Get the control element with the given id string and IFACE_MIXER.
2267 struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
2270 return find_mixer_ctl(codec, name, 0, 0);
2272 EXPORT_SYMBOL_GPL(snd_hda_find_mixer_ctl);
2274 static int find_empty_mixer_ctl_idx(struct hda_codec *codec, const char *name,
2278 /* 16 ctlrs should be large enough */
2279 for (i = 0, idx = start_idx; i < 16; i++, idx++) {
2280 if (!find_mixer_ctl(codec, name, 0, idx))
2287 * snd_hda_ctl_add - Add a control element and assign to the codec
2288 * @codec: HD-audio codec
2289 * @nid: corresponding NID (optional)
2290 * @kctl: the control element to assign
2292 * Add the given control element to an array inside the codec instance.
2293 * All control elements belonging to a codec are supposed to be added
2294 * by this function so that a proper clean-up works at the free or
2295 * reconfiguration time.
2297 * If non-zero @nid is passed, the NID is assigned to the control element.
2298 * The assignment is shown in the codec proc file.
2300 * snd_hda_ctl_add() checks the control subdev id field whether
2301 * #HDA_SUBDEV_NID_FLAG bit is set. If set (and @nid is zero), the lower
2302 * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit
2303 * specifies if kctl->private_value is a HDA amplifier value.
2305 int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid,
2306 struct snd_kcontrol *kctl)
2309 unsigned short flags = 0;
2310 struct hda_nid_item *item;
2312 if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) {
2313 flags |= HDA_NID_ITEM_AMP;
2315 nid = get_amp_nid_(kctl->private_value);
2317 if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0)
2318 nid = kctl->id.subdevice & 0xffff;
2319 if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG))
2320 kctl->id.subdevice = 0;
2321 err = snd_ctl_add(codec->bus->card, kctl);
2324 item = snd_array_new(&codec->mixers);
2329 item->flags = flags;
2332 EXPORT_SYMBOL_GPL(snd_hda_ctl_add);
2335 * snd_hda_add_nid - Assign a NID to a control element
2336 * @codec: HD-audio codec
2337 * @nid: corresponding NID (optional)
2338 * @kctl: the control element to assign
2339 * @index: index to kctl
2341 * Add the given control element to an array inside the codec instance.
2342 * This function is used when #snd_hda_ctl_add cannot be used for 1:1
2343 * NID:KCTL mapping - for example "Capture Source" selector.
2345 int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl,
2346 unsigned int index, hda_nid_t nid)
2348 struct hda_nid_item *item;
2351 item = snd_array_new(&codec->nids);
2355 item->index = index;
2359 codec_err(codec, "no NID for mapping control %s:%d:%d\n",
2360 kctl->id.name, kctl->id.index, index);
2363 EXPORT_SYMBOL_GPL(snd_hda_add_nid);
2366 * snd_hda_ctls_clear - Clear all controls assigned to the given codec
2367 * @codec: HD-audio codec
2369 void snd_hda_ctls_clear(struct hda_codec *codec)
2372 struct hda_nid_item *items = codec->mixers.list;
2373 for (i = 0; i < codec->mixers.used; i++)
2374 snd_ctl_remove(codec->bus->card, items[i].kctl);
2375 snd_array_free(&codec->mixers);
2376 snd_array_free(&codec->nids);
2380 * snd_hda_lock_devices - pseudo device locking
2383 * toggle card->shutdown to allow/disallow the device access (as a hack)
2385 int snd_hda_lock_devices(struct hda_bus *bus)
2387 struct snd_card *card = bus->card;
2388 struct hda_codec *codec;
2390 spin_lock(&card->files_lock);
2394 if (!list_empty(&card->ctl_files))
2397 list_for_each_entry(codec, &bus->codec_list, list) {
2399 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
2400 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
2403 if (cpcm->pcm->streams[0].substream_opened ||
2404 cpcm->pcm->streams[1].substream_opened)
2408 spin_unlock(&card->files_lock);
2414 spin_unlock(&card->files_lock);
2417 EXPORT_SYMBOL_GPL(snd_hda_lock_devices);
2420 * snd_hda_unlock_devices - pseudo device unlocking
2423 void snd_hda_unlock_devices(struct hda_bus *bus)
2425 struct snd_card *card = bus->card;
2428 spin_lock(&card->files_lock);
2430 spin_unlock(&card->files_lock);
2432 EXPORT_SYMBOL_GPL(snd_hda_unlock_devices);
2435 * snd_hda_codec_reset - Clear all objects assigned to the codec
2436 * @codec: HD-audio codec
2438 * This frees the all PCM and control elements assigned to the codec, and
2439 * clears the caches and restores the pin default configurations.
2441 * When a device is being used, it returns -EBSY. If successfully freed,
2444 int snd_hda_codec_reset(struct hda_codec *codec)
2446 struct hda_bus *bus = codec->bus;
2447 struct snd_card *card = bus->card;
2450 if (snd_hda_lock_devices(bus) < 0)
2453 /* OK, let it free */
2454 cancel_delayed_work_sync(&codec->jackpoll_work);
2456 cancel_delayed_work_sync(&codec->power_work);
2457 flush_workqueue(bus->workq);
2459 snd_hda_ctls_clear(codec);
2461 for (i = 0; i < codec->num_pcms; i++) {
2462 if (codec->pcm_info[i].pcm) {
2463 snd_device_free(card, codec->pcm_info[i].pcm);
2464 clear_bit(codec->pcm_info[i].device,
2468 snd_hda_detach_beep_device(codec);
2469 if (device_is_registered(hda_codec_dev(codec)))
2470 device_del(hda_codec_dev(codec));
2472 memset(&codec->patch_ops, 0, sizeof(codec->patch_ops));
2473 snd_hda_jack_tbl_clear(codec);
2474 codec->proc_widget_hook = NULL;
2476 free_hda_cache(&codec->amp_cache);
2477 free_hda_cache(&codec->cmd_cache);
2478 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
2479 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
2480 /* free only driver_pins so that init_pins + user_pins are restored */
2481 snd_array_free(&codec->driver_pins);
2482 snd_array_free(&codec->cvt_setups);
2483 snd_array_free(&codec->spdif_out);
2484 snd_array_free(&codec->verbs);
2485 codec->num_pcms = 0;
2486 codec->pcm_info = NULL;
2487 codec->preset = NULL;
2488 codec->slave_dig_outs = NULL;
2489 codec->spdif_status_reset = 0;
2491 /* allow device access again */
2492 snd_hda_unlock_devices(bus);
2496 typedef int (*map_slave_func_t)(struct hda_codec *, void *, struct snd_kcontrol *);
2498 /* apply the function to all matching slave ctls in the mixer list */
2499 static int map_slaves(struct hda_codec *codec, const char * const *slaves,
2500 const char *suffix, map_slave_func_t func, void *data)
2502 struct hda_nid_item *items;
2503 const char * const *s;
2506 items = codec->mixers.list;
2507 for (i = 0; i < codec->mixers.used; i++) {
2508 struct snd_kcontrol *sctl = items[i].kctl;
2509 if (!sctl || sctl->id.iface != SNDRV_CTL_ELEM_IFACE_MIXER)
2511 for (s = slaves; *s; s++) {
2512 char tmpname[sizeof(sctl->id.name)];
2513 const char *name = *s;
2515 snprintf(tmpname, sizeof(tmpname), "%s %s",
2519 if (!strcmp(sctl->id.name, name)) {
2520 err = func(codec, data, sctl);
2530 static int check_slave_present(struct hda_codec *codec,
2531 void *data, struct snd_kcontrol *sctl)
2536 /* guess the value corresponding to 0dB */
2537 static int get_kctl_0dB_offset(struct hda_codec *codec,
2538 struct snd_kcontrol *kctl, int *step_to_check)
2541 const int *tlv = NULL;
2544 if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
2545 /* FIXME: set_fs() hack for obtaining user-space TLV data */
2546 mm_segment_t fs = get_fs();
2548 if (!kctl->tlv.c(kctl, 0, sizeof(_tlv), _tlv))
2551 } else if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_READ)
2553 if (tlv && tlv[0] == SNDRV_CTL_TLVT_DB_SCALE) {
2555 step &= ~TLV_DB_SCALE_MUTE;
2558 if (*step_to_check && *step_to_check != step) {
2559 codec_err(codec, "Mismatching dB step for vmaster slave (%d!=%d)\n",
2560 - *step_to_check, step);
2563 *step_to_check = step;
2564 val = -tlv[2] / step;
2569 /* call kctl->put with the given value(s) */
2570 static int put_kctl_with_value(struct snd_kcontrol *kctl, int val)
2572 struct snd_ctl_elem_value *ucontrol;
2573 ucontrol = kzalloc(sizeof(*ucontrol), GFP_KERNEL);
2576 ucontrol->value.integer.value[0] = val;
2577 ucontrol->value.integer.value[1] = val;
2578 kctl->put(kctl, ucontrol);
2583 /* initialize the slave volume with 0dB */
2584 static int init_slave_0dB(struct hda_codec *codec,
2585 void *data, struct snd_kcontrol *slave)
2587 int offset = get_kctl_0dB_offset(codec, slave, data);
2589 put_kctl_with_value(slave, offset);
2593 /* unmute the slave */
2594 static int init_slave_unmute(struct hda_codec *codec,
2595 void *data, struct snd_kcontrol *slave)
2597 return put_kctl_with_value(slave, 1);
2600 static int add_slave(struct hda_codec *codec,
2601 void *data, struct snd_kcontrol *slave)
2603 return snd_ctl_add_slave(data, slave);
2607 * __snd_hda_add_vmaster - create a virtual master control and add slaves
2608 * @codec: HD-audio codec
2609 * @name: vmaster control name
2610 * @tlv: TLV data (optional)
2611 * @slaves: slave control names (optional)
2612 * @suffix: suffix string to each slave name (optional)
2613 * @init_slave_vol: initialize slaves to unmute/0dB
2614 * @ctl_ret: store the vmaster kcontrol in return
2616 * Create a virtual master control with the given name. The TLV data
2617 * must be either NULL or a valid data.
2619 * @slaves is a NULL-terminated array of strings, each of which is a
2620 * slave control name. All controls with these names are assigned to
2621 * the new virtual master control.
2623 * This function returns zero if successful or a negative error code.
2625 int __snd_hda_add_vmaster(struct hda_codec *codec, char *name,
2626 unsigned int *tlv, const char * const *slaves,
2627 const char *suffix, bool init_slave_vol,
2628 struct snd_kcontrol **ctl_ret)
2630 struct snd_kcontrol *kctl;
2636 err = map_slaves(codec, slaves, suffix, check_slave_present, NULL);
2638 codec_dbg(codec, "No slave found for %s\n", name);
2641 kctl = snd_ctl_make_virtual_master(name, tlv);
2644 err = snd_hda_ctl_add(codec, 0, kctl);
2648 err = map_slaves(codec, slaves, suffix, add_slave, kctl);
2652 /* init with master mute & zero volume */
2653 put_kctl_with_value(kctl, 0);
2654 if (init_slave_vol) {
2656 map_slaves(codec, slaves, suffix,
2657 tlv ? init_slave_0dB : init_slave_unmute, &step);
2664 EXPORT_SYMBOL_GPL(__snd_hda_add_vmaster);
2667 * mute-LED control using vmaster
2669 static int vmaster_mute_mode_info(struct snd_kcontrol *kcontrol,
2670 struct snd_ctl_elem_info *uinfo)
2672 static const char * const texts[] = {
2673 "On", "Off", "Follow Master"
2676 return snd_ctl_enum_info(uinfo, 1, 3, texts);
2679 static int vmaster_mute_mode_get(struct snd_kcontrol *kcontrol,
2680 struct snd_ctl_elem_value *ucontrol)
2682 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2683 ucontrol->value.enumerated.item[0] = hook->mute_mode;
2687 static int vmaster_mute_mode_put(struct snd_kcontrol *kcontrol,
2688 struct snd_ctl_elem_value *ucontrol)
2690 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2691 unsigned int old_mode = hook->mute_mode;
2693 hook->mute_mode = ucontrol->value.enumerated.item[0];
2694 if (hook->mute_mode > HDA_VMUTE_FOLLOW_MASTER)
2695 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
2696 if (old_mode == hook->mute_mode)
2698 snd_hda_sync_vmaster_hook(hook);
2702 static struct snd_kcontrol_new vmaster_mute_mode = {
2703 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2704 .name = "Mute-LED Mode",
2705 .info = vmaster_mute_mode_info,
2706 .get = vmaster_mute_mode_get,
2707 .put = vmaster_mute_mode_put,
2711 * snd_hda_add_vmaster_hook - Add a vmaster hook for mute-LED
2712 * @codec: the HDA codec
2713 * @hook: the vmaster hook object
2714 * @expose_enum_ctl: flag to create an enum ctl
2716 * Add a mute-LED hook with the given vmaster switch kctl.
2717 * When @expose_enum_ctl is set, "Mute-LED Mode" control is automatically
2718 * created and associated with the given hook.
2720 int snd_hda_add_vmaster_hook(struct hda_codec *codec,
2721 struct hda_vmaster_mute_hook *hook,
2722 bool expose_enum_ctl)
2724 struct snd_kcontrol *kctl;
2726 if (!hook->hook || !hook->sw_kctl)
2728 snd_ctl_add_vmaster_hook(hook->sw_kctl, hook->hook, codec);
2729 hook->codec = codec;
2730 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
2731 if (!expose_enum_ctl)
2733 kctl = snd_ctl_new1(&vmaster_mute_mode, hook);
2736 return snd_hda_ctl_add(codec, 0, kctl);
2738 EXPORT_SYMBOL_GPL(snd_hda_add_vmaster_hook);
2741 * snd_hda_sync_vmaster_hook - Sync vmaster hook
2742 * @hook: the vmaster hook
2744 * Call the hook with the current value for synchronization.
2745 * Should be called in init callback.
2747 void snd_hda_sync_vmaster_hook(struct hda_vmaster_mute_hook *hook)
2749 if (!hook->hook || !hook->codec)
2751 /* don't call vmaster hook in the destructor since it might have
2752 * been already destroyed
2754 if (hook->codec->bus->shutdown)
2756 switch (hook->mute_mode) {
2757 case HDA_VMUTE_FOLLOW_MASTER:
2758 snd_ctl_sync_vmaster_hook(hook->sw_kctl);
2761 hook->hook(hook->codec, hook->mute_mode);
2765 EXPORT_SYMBOL_GPL(snd_hda_sync_vmaster_hook);
2769 * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch
2770 * @kcontrol: referred ctl element
2771 * @uinfo: pointer to get/store the data
2773 * The control element is supposed to have the private_value field
2774 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2776 int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
2777 struct snd_ctl_elem_info *uinfo)
2779 int chs = get_amp_channels(kcontrol);
2781 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2782 uinfo->count = chs == 3 ? 2 : 1;
2783 uinfo->value.integer.min = 0;
2784 uinfo->value.integer.max = 1;
2787 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_info);
2790 * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch
2791 * @kcontrol: ctl element
2792 * @ucontrol: pointer to get/store the data
2794 * The control element is supposed to have the private_value field
2795 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2797 int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
2798 struct snd_ctl_elem_value *ucontrol)
2800 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2801 hda_nid_t nid = get_amp_nid(kcontrol);
2802 int chs = get_amp_channels(kcontrol);
2803 int dir = get_amp_direction(kcontrol);
2804 int idx = get_amp_index(kcontrol);
2805 long *valp = ucontrol->value.integer.value;
2808 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
2809 HDA_AMP_MUTE) ? 0 : 1;
2811 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
2812 HDA_AMP_MUTE) ? 0 : 1;
2815 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_get);
2818 * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch
2819 * @kcontrol: ctl element
2820 * @ucontrol: pointer to get/store the data
2822 * The control element is supposed to have the private_value field
2823 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2825 int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
2826 struct snd_ctl_elem_value *ucontrol)
2828 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2829 hda_nid_t nid = get_amp_nid(kcontrol);
2830 int chs = get_amp_channels(kcontrol);
2831 int dir = get_amp_direction(kcontrol);
2832 int idx = get_amp_index(kcontrol);
2833 long *valp = ucontrol->value.integer.value;
2837 change = codec_amp_update(codec, nid, 0, dir, idx,
2839 *valp ? 0 : HDA_AMP_MUTE, false,
2840 !hda_codec_is_power_on(codec));
2844 change |= codec_amp_update(codec, nid, 1, dir, idx,
2846 *valp ? 0 : HDA_AMP_MUTE, false,
2847 !hda_codec_is_power_on(codec));
2848 hda_call_check_power_status(codec, nid);
2851 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_put);
2854 * bound volume controls
2856 * bind multiple volumes (# indices, from 0)
2859 #define AMP_VAL_IDX_SHIFT 19
2860 #define AMP_VAL_IDX_MASK (0x0f<<19)
2863 * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control
2864 * @kcontrol: ctl element
2865 * @ucontrol: pointer to get/store the data
2867 * The control element is supposed to have the private_value field
2868 * set up via HDA_BIND_MUTE*() macros.
2870 int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
2871 struct snd_ctl_elem_value *ucontrol)
2873 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2877 mutex_lock(&codec->control_mutex);
2878 pval = kcontrol->private_value;
2879 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
2880 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
2881 kcontrol->private_value = pval;
2882 mutex_unlock(&codec->control_mutex);
2885 EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_switch_get);
2888 * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control
2889 * @kcontrol: ctl element
2890 * @ucontrol: pointer to get/store the data
2892 * The control element is supposed to have the private_value field
2893 * set up via HDA_BIND_MUTE*() macros.
2895 int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
2896 struct snd_ctl_elem_value *ucontrol)
2898 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2900 int i, indices, err = 0, change = 0;
2902 mutex_lock(&codec->control_mutex);
2903 pval = kcontrol->private_value;
2904 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
2905 for (i = 0; i < indices; i++) {
2906 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
2907 (i << AMP_VAL_IDX_SHIFT);
2908 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2913 kcontrol->private_value = pval;
2914 mutex_unlock(&codec->control_mutex);
2915 return err < 0 ? err : change;
2917 EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_switch_put);
2920 * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control
2921 * @kcontrol: referred ctl element
2922 * @uinfo: pointer to get/store the data
2924 * The control element is supposed to have the private_value field
2925 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2927 int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
2928 struct snd_ctl_elem_info *uinfo)
2930 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2931 struct hda_bind_ctls *c;
2934 mutex_lock(&codec->control_mutex);
2935 c = (struct hda_bind_ctls *)kcontrol->private_value;
2936 kcontrol->private_value = *c->values;
2937 err = c->ops->info(kcontrol, uinfo);
2938 kcontrol->private_value = (long)c;
2939 mutex_unlock(&codec->control_mutex);
2942 EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_info);
2945 * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control
2946 * @kcontrol: ctl element
2947 * @ucontrol: pointer to get/store the data
2949 * The control element is supposed to have the private_value field
2950 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2952 int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
2953 struct snd_ctl_elem_value *ucontrol)
2955 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2956 struct hda_bind_ctls *c;
2959 mutex_lock(&codec->control_mutex);
2960 c = (struct hda_bind_ctls *)kcontrol->private_value;
2961 kcontrol->private_value = *c->values;
2962 err = c->ops->get(kcontrol, ucontrol);
2963 kcontrol->private_value = (long)c;
2964 mutex_unlock(&codec->control_mutex);
2967 EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_get);
2970 * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control
2971 * @kcontrol: ctl element
2972 * @ucontrol: pointer to get/store the data
2974 * The control element is supposed to have the private_value field
2975 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2977 int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
2978 struct snd_ctl_elem_value *ucontrol)
2980 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2981 struct hda_bind_ctls *c;
2982 unsigned long *vals;
2983 int err = 0, change = 0;
2985 mutex_lock(&codec->control_mutex);
2986 c = (struct hda_bind_ctls *)kcontrol->private_value;
2987 for (vals = c->values; *vals; vals++) {
2988 kcontrol->private_value = *vals;
2989 err = c->ops->put(kcontrol, ucontrol);
2994 kcontrol->private_value = (long)c;
2995 mutex_unlock(&codec->control_mutex);
2996 return err < 0 ? err : change;
2998 EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_put);
3001 * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control
3002 * @kcontrol: ctl element
3003 * @op_flag: operation flag
3004 * @size: byte size of input TLV
3007 * The control element is supposed to have the private_value field
3008 * set up via HDA_BIND_VOL() macro.
3010 int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
3011 unsigned int size, unsigned int __user *tlv)
3013 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3014 struct hda_bind_ctls *c;
3017 mutex_lock(&codec->control_mutex);
3018 c = (struct hda_bind_ctls *)kcontrol->private_value;
3019 kcontrol->private_value = *c->values;
3020 err = c->ops->tlv(kcontrol, op_flag, size, tlv);
3021 kcontrol->private_value = (long)c;
3022 mutex_unlock(&codec->control_mutex);
3025 EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_tlv);
3027 struct hda_ctl_ops snd_hda_bind_vol = {
3028 .info = snd_hda_mixer_amp_volume_info,
3029 .get = snd_hda_mixer_amp_volume_get,
3030 .put = snd_hda_mixer_amp_volume_put,
3031 .tlv = snd_hda_mixer_amp_tlv
3033 EXPORT_SYMBOL_GPL(snd_hda_bind_vol);
3035 struct hda_ctl_ops snd_hda_bind_sw = {
3036 .info = snd_hda_mixer_amp_switch_info,
3037 .get = snd_hda_mixer_amp_switch_get,
3038 .put = snd_hda_mixer_amp_switch_put,
3039 .tlv = snd_hda_mixer_amp_tlv
3041 EXPORT_SYMBOL_GPL(snd_hda_bind_sw);
3044 * SPDIF out controls
3047 static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
3048 struct snd_ctl_elem_info *uinfo)
3050 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
3055 static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
3056 struct snd_ctl_elem_value *ucontrol)
3058 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
3059 IEC958_AES0_NONAUDIO |
3060 IEC958_AES0_CON_EMPHASIS_5015 |
3061 IEC958_AES0_CON_NOT_COPYRIGHT;
3062 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
3063 IEC958_AES1_CON_ORIGINAL;
3067 static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
3068 struct snd_ctl_elem_value *ucontrol)
3070 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
3071 IEC958_AES0_NONAUDIO |
3072 IEC958_AES0_PRO_EMPHASIS_5015;
3076 static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
3077 struct snd_ctl_elem_value *ucontrol)
3079 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3080 int idx = kcontrol->private_value;
3081 struct hda_spdif_out *spdif;
3083 mutex_lock(&codec->spdif_mutex);
3084 spdif = snd_array_elem(&codec->spdif_out, idx);
3085 ucontrol->value.iec958.status[0] = spdif->status & 0xff;
3086 ucontrol->value.iec958.status[1] = (spdif->status >> 8) & 0xff;
3087 ucontrol->value.iec958.status[2] = (spdif->status >> 16) & 0xff;
3088 ucontrol->value.iec958.status[3] = (spdif->status >> 24) & 0xff;
3089 mutex_unlock(&codec->spdif_mutex);
3094 /* convert from SPDIF status bits to HDA SPDIF bits
3095 * bit 0 (DigEn) is always set zero (to be filled later)
3097 static unsigned short convert_from_spdif_status(unsigned int sbits)
3099 unsigned short val = 0;
3101 if (sbits & IEC958_AES0_PROFESSIONAL)
3102 val |= AC_DIG1_PROFESSIONAL;
3103 if (sbits & IEC958_AES0_NONAUDIO)
3104 val |= AC_DIG1_NONAUDIO;
3105 if (sbits & IEC958_AES0_PROFESSIONAL) {
3106 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
3107 IEC958_AES0_PRO_EMPHASIS_5015)
3108 val |= AC_DIG1_EMPHASIS;
3110 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
3111 IEC958_AES0_CON_EMPHASIS_5015)
3112 val |= AC_DIG1_EMPHASIS;
3113 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
3114 val |= AC_DIG1_COPYRIGHT;
3115 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
3116 val |= AC_DIG1_LEVEL;
3117 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
3122 /* convert to SPDIF status bits from HDA SPDIF bits
3124 static unsigned int convert_to_spdif_status(unsigned short val)
3126 unsigned int sbits = 0;
3128 if (val & AC_DIG1_NONAUDIO)
3129 sbits |= IEC958_AES0_NONAUDIO;
3130 if (val & AC_DIG1_PROFESSIONAL)
3131 sbits |= IEC958_AES0_PROFESSIONAL;
3132 if (sbits & IEC958_AES0_PROFESSIONAL) {
3133 if (val & AC_DIG1_EMPHASIS)
3134 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
3136 if (val & AC_DIG1_EMPHASIS)
3137 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
3138 if (!(val & AC_DIG1_COPYRIGHT))
3139 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
3140 if (val & AC_DIG1_LEVEL)
3141 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
3142 sbits |= val & (0x7f << 8);
3147 /* set digital convert verbs both for the given NID and its slaves */
3148 static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
3153 snd_hda_codec_write_cache(codec, nid, 0, verb, val);
3154 d = codec->slave_dig_outs;
3158 snd_hda_codec_write_cache(codec, *d, 0, verb, val);
3161 static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
3165 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1);
3167 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2);
3170 static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
3171 struct snd_ctl_elem_value *ucontrol)
3173 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3174 int idx = kcontrol->private_value;
3175 struct hda_spdif_out *spdif;
3180 mutex_lock(&codec->spdif_mutex);
3181 spdif = snd_array_elem(&codec->spdif_out, idx);
3183 spdif->status = ucontrol->value.iec958.status[0] |
3184 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
3185 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
3186 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
3187 val = convert_from_spdif_status(spdif->status);
3188 val |= spdif->ctls & 1;
3189 change = spdif->ctls != val;
3191 if (change && nid != (u16)-1)
3192 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
3193 mutex_unlock(&codec->spdif_mutex);
3197 #define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
3199 static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
3200 struct snd_ctl_elem_value *ucontrol)
3202 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3203 int idx = kcontrol->private_value;
3204 struct hda_spdif_out *spdif;
3206 mutex_lock(&codec->spdif_mutex);
3207 spdif = snd_array_elem(&codec->spdif_out, idx);
3208 ucontrol->value.integer.value[0] = spdif->ctls & AC_DIG1_ENABLE;
3209 mutex_unlock(&codec->spdif_mutex);
3213 static inline void set_spdif_ctls(struct hda_codec *codec, hda_nid_t nid,
3216 set_dig_out_convert(codec, nid, dig1, dig2);
3217 /* unmute amp switch (if any) */
3218 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
3219 (dig1 & AC_DIG1_ENABLE))
3220 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
3224 static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
3225 struct snd_ctl_elem_value *ucontrol)
3227 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3228 int idx = kcontrol->private_value;
3229 struct hda_spdif_out *spdif;
3234 mutex_lock(&codec->spdif_mutex);
3235 spdif = snd_array_elem(&codec->spdif_out, idx);
3237 val = spdif->ctls & ~AC_DIG1_ENABLE;
3238 if (ucontrol->value.integer.value[0])
3239 val |= AC_DIG1_ENABLE;
3240 change = spdif->ctls != val;
3242 if (change && nid != (u16)-1)
3243 set_spdif_ctls(codec, nid, val & 0xff, -1);
3244 mutex_unlock(&codec->spdif_mutex);
3248 static struct snd_kcontrol_new dig_mixes[] = {
3250 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3251 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3252 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
3253 .info = snd_hda_spdif_mask_info,
3254 .get = snd_hda_spdif_cmask_get,
3257 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3258 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3259 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK),
3260 .info = snd_hda_spdif_mask_info,
3261 .get = snd_hda_spdif_pmask_get,
3264 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3265 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
3266 .info = snd_hda_spdif_mask_info,
3267 .get = snd_hda_spdif_default_get,
3268 .put = snd_hda_spdif_default_put,
3271 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3272 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
3273 .info = snd_hda_spdif_out_switch_info,
3274 .get = snd_hda_spdif_out_switch_get,
3275 .put = snd_hda_spdif_out_switch_put,
3281 * snd_hda_create_dig_out_ctls - create Output SPDIF-related controls
3282 * @codec: the HDA codec
3283 * @associated_nid: NID that new ctls associated with
3284 * @cvt_nid: converter NID
3285 * @type: HDA_PCM_TYPE_*
3286 * Creates controls related with the digital output.
3287 * Called from each patch supporting the digital out.
3289 * Returns 0 if successful, or a negative error code.
3291 int snd_hda_create_dig_out_ctls(struct hda_codec *codec,
3292 hda_nid_t associated_nid,
3297 struct snd_kcontrol *kctl;
3298 struct snd_kcontrol_new *dig_mix;
3300 const int spdif_index = 16;
3301 struct hda_spdif_out *spdif;
3302 struct hda_bus *bus = codec->bus;
3304 if (bus->primary_dig_out_type == HDA_PCM_TYPE_HDMI &&
3305 type == HDA_PCM_TYPE_SPDIF) {
3307 } else if (bus->primary_dig_out_type == HDA_PCM_TYPE_SPDIF &&
3308 type == HDA_PCM_TYPE_HDMI) {
3309 /* suppose a single SPDIF device */
3310 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
3311 kctl = find_mixer_ctl(codec, dig_mix->name, 0, 0);
3314 kctl->id.index = spdif_index;
3316 bus->primary_dig_out_type = HDA_PCM_TYPE_HDMI;
3318 if (!bus->primary_dig_out_type)
3319 bus->primary_dig_out_type = type;
3321 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Playback Switch", idx);
3323 codec_err(codec, "too many IEC958 outputs\n");
3326 spdif = snd_array_new(&codec->spdif_out);
3329 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
3330 kctl = snd_ctl_new1(dig_mix, codec);
3333 kctl->id.index = idx;
3334 kctl->private_value = codec->spdif_out.used - 1;
3335 err = snd_hda_ctl_add(codec, associated_nid, kctl);
3339 spdif->nid = cvt_nid;
3340 spdif->ctls = snd_hda_codec_read(codec, cvt_nid, 0,
3341 AC_VERB_GET_DIGI_CONVERT_1, 0);
3342 spdif->status = convert_to_spdif_status(spdif->ctls);
3345 EXPORT_SYMBOL_GPL(snd_hda_create_dig_out_ctls);
3348 * snd_hda_spdif_out_of_nid - get the hda_spdif_out entry from the given NID
3349 * @codec: the HDA codec
3352 * call within spdif_mutex lock
3354 struct hda_spdif_out *snd_hda_spdif_out_of_nid(struct hda_codec *codec,
3358 for (i = 0; i < codec->spdif_out.used; i++) {
3359 struct hda_spdif_out *spdif =
3360 snd_array_elem(&codec->spdif_out, i);
3361 if (spdif->nid == nid)
3366 EXPORT_SYMBOL_GPL(snd_hda_spdif_out_of_nid);
3369 * snd_hda_spdif_ctls_unassign - Unassign the given SPDIF ctl
3370 * @codec: the HDA codec
3371 * @idx: the SPDIF ctl index
3373 * Unassign the widget from the given SPDIF control.
3375 void snd_hda_spdif_ctls_unassign(struct hda_codec *codec, int idx)
3377 struct hda_spdif_out *spdif;
3379 mutex_lock(&codec->spdif_mutex);
3380 spdif = snd_array_elem(&codec->spdif_out, idx);
3381 spdif->nid = (u16)-1;
3382 mutex_unlock(&codec->spdif_mutex);
3384 EXPORT_SYMBOL_GPL(snd_hda_spdif_ctls_unassign);
3387 * snd_hda_spdif_ctls_assign - Assign the SPDIF controls to the given NID
3388 * @codec: the HDA codec
3389 * @idx: the SPDIF ctl idx
3392 * Assign the widget to the SPDIF control with the given index.
3394 void snd_hda_spdif_ctls_assign(struct hda_codec *codec, int idx, hda_nid_t nid)
3396 struct hda_spdif_out *spdif;
3399 mutex_lock(&codec->spdif_mutex);
3400 spdif = snd_array_elem(&codec->spdif_out, idx);
3401 if (spdif->nid != nid) {
3404 set_spdif_ctls(codec, nid, val & 0xff, (val >> 8) & 0xff);
3406 mutex_unlock(&codec->spdif_mutex);
3408 EXPORT_SYMBOL_GPL(snd_hda_spdif_ctls_assign);
3411 * SPDIF sharing with analog output
3413 static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
3414 struct snd_ctl_elem_value *ucontrol)
3416 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3417 ucontrol->value.integer.value[0] = mout->share_spdif;
3421 static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
3422 struct snd_ctl_elem_value *ucontrol)
3424 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3425 mout->share_spdif = !!ucontrol->value.integer.value[0];
3429 static struct snd_kcontrol_new spdif_share_sw = {
3430 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3431 .name = "IEC958 Default PCM Playback Switch",
3432 .info = snd_ctl_boolean_mono_info,
3433 .get = spdif_share_sw_get,
3434 .put = spdif_share_sw_put,
3438 * snd_hda_create_spdif_share_sw - create Default PCM switch
3439 * @codec: the HDA codec
3440 * @mout: multi-out instance
3442 int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
3443 struct hda_multi_out *mout)
3445 struct snd_kcontrol *kctl;
3447 if (!mout->dig_out_nid)
3450 kctl = snd_ctl_new1(&spdif_share_sw, mout);
3453 /* ATTENTION: here mout is passed as private_data, instead of codec */
3454 return snd_hda_ctl_add(codec, mout->dig_out_nid, kctl);
3456 EXPORT_SYMBOL_GPL(snd_hda_create_spdif_share_sw);
3462 #define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
3464 static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
3465 struct snd_ctl_elem_value *ucontrol)
3467 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3469 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
3473 static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
3474 struct snd_ctl_elem_value *ucontrol)
3476 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3477 hda_nid_t nid = kcontrol->private_value;
3478 unsigned int val = !!ucontrol->value.integer.value[0];
3481 mutex_lock(&codec->spdif_mutex);
3482 change = codec->spdif_in_enable != val;
3484 codec->spdif_in_enable = val;
3485 snd_hda_codec_write_cache(codec, nid, 0,
3486 AC_VERB_SET_DIGI_CONVERT_1, val);
3488 mutex_unlock(&codec->spdif_mutex);
3492 static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
3493 struct snd_ctl_elem_value *ucontrol)
3495 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3496 hda_nid_t nid = kcontrol->private_value;
3500 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0);
3501 sbits = convert_to_spdif_status(val);
3502 ucontrol->value.iec958.status[0] = sbits;
3503 ucontrol->value.iec958.status[1] = sbits >> 8;
3504 ucontrol->value.iec958.status[2] = sbits >> 16;
3505 ucontrol->value.iec958.status[3] = sbits >> 24;
3509 static struct snd_kcontrol_new dig_in_ctls[] = {
3511 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3512 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH),
3513 .info = snd_hda_spdif_in_switch_info,
3514 .get = snd_hda_spdif_in_switch_get,
3515 .put = snd_hda_spdif_in_switch_put,
3518 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3519 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3520 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
3521 .info = snd_hda_spdif_mask_info,
3522 .get = snd_hda_spdif_in_status_get,
3528 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
3529 * @codec: the HDA codec
3530 * @nid: audio in widget NID
3532 * Creates controls related with the SPDIF input.
3533 * Called from each patch supporting the SPDIF in.
3535 * Returns 0 if successful, or a negative error code.
3537 int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
3540 struct snd_kcontrol *kctl;
3541 struct snd_kcontrol_new *dig_mix;
3544 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Capture Switch", 0);
3546 codec_err(codec, "too many IEC958 inputs\n");
3549 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
3550 kctl = snd_ctl_new1(dig_mix, codec);
3553 kctl->private_value = nid;
3554 err = snd_hda_ctl_add(codec, nid, kctl);
3558 codec->spdif_in_enable =
3559 snd_hda_codec_read(codec, nid, 0,
3560 AC_VERB_GET_DIGI_CONVERT_1, 0) &
3564 EXPORT_SYMBOL_GPL(snd_hda_create_spdif_in_ctls);
3570 /* build a 31bit cache key with the widget id and the command parameter */
3571 #define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
3572 #define get_cmd_cache_nid(key) ((key) & 0xff)
3573 #define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
3576 * snd_hda_codec_write_cache - send a single command with caching
3577 * @codec: the HDA codec
3578 * @nid: NID to send the command
3579 * @flags: optional bit flags
3580 * @verb: the verb to send
3581 * @parm: the parameter for the verb
3583 * Send a single command without waiting for response.
3585 * Returns 0 if successful, or a negative error code.
3587 int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
3588 int flags, unsigned int verb, unsigned int parm)
3591 struct hda_cache_head *c;
3593 unsigned int cache_only;
3595 cache_only = codec->cached_write;
3597 err = snd_hda_codec_write(codec, nid, flags, verb, parm);
3602 /* parm may contain the verb stuff for get/set amp */
3603 verb = verb | (parm >> 8);
3605 key = build_cmd_cache_key(nid, verb);
3606 mutex_lock(&codec->bus->cmd_mutex);
3607 c = get_alloc_hash(&codec->cmd_cache, key);
3610 c->dirty = cache_only;
3612 mutex_unlock(&codec->bus->cmd_mutex);
3615 EXPORT_SYMBOL_GPL(snd_hda_codec_write_cache);
3618 * snd_hda_codec_update_cache - check cache and write the cmd only when needed
3619 * @codec: the HDA codec
3620 * @nid: NID to send the command
3621 * @flags: optional bit flags
3622 * @verb: the verb to send
3623 * @parm: the parameter for the verb
3625 * This function works like snd_hda_codec_write_cache(), but it doesn't send
3626 * command if the parameter is already identical with the cached value.
3627 * If not, it sends the command and refreshes the cache.
3629 * Returns 0 if successful, or a negative error code.
3631 int snd_hda_codec_update_cache(struct hda_codec *codec, hda_nid_t nid,
3632 int flags, unsigned int verb, unsigned int parm)
3634 struct hda_cache_head *c;
3637 /* parm may contain the verb stuff for get/set amp */
3638 verb = verb | (parm >> 8);
3640 key = build_cmd_cache_key(nid, verb);
3641 mutex_lock(&codec->bus->cmd_mutex);
3642 c = get_hash(&codec->cmd_cache, key);
3643 if (c && c->val == parm) {
3644 mutex_unlock(&codec->bus->cmd_mutex);
3647 mutex_unlock(&codec->bus->cmd_mutex);
3648 return snd_hda_codec_write_cache(codec, nid, flags, verb, parm);
3650 EXPORT_SYMBOL_GPL(snd_hda_codec_update_cache);
3653 * snd_hda_codec_resume_cache - Resume the all commands from the cache
3654 * @codec: HD-audio codec
3656 * Execute all verbs recorded in the command caches to resume.
3658 void snd_hda_codec_resume_cache(struct hda_codec *codec)
3662 mutex_lock(&codec->hash_mutex);
3663 codec->cached_write = 0;
3664 for (i = 0; i < codec->cmd_cache.buf.used; i++) {
3665 struct hda_cache_head *buffer;
3668 buffer = snd_array_elem(&codec->cmd_cache.buf, i);
3675 mutex_unlock(&codec->hash_mutex);
3676 snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
3677 get_cmd_cache_cmd(key), buffer->val);
3678 mutex_lock(&codec->hash_mutex);
3680 mutex_unlock(&codec->hash_mutex);
3682 EXPORT_SYMBOL_GPL(snd_hda_codec_resume_cache);
3685 * snd_hda_sequence_write_cache - sequence writes with caching
3686 * @codec: the HDA codec
3687 * @seq: VERB array to send
3689 * Send the commands sequentially from the given array.
3690 * Thte commands are recorded on cache for power-save and resume.
3691 * The array must be terminated with NID=0.
3693 void snd_hda_sequence_write_cache(struct hda_codec *codec,
3694 const struct hda_verb *seq)
3696 for (; seq->nid; seq++)
3697 snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
3700 EXPORT_SYMBOL_GPL(snd_hda_sequence_write_cache);
3703 * snd_hda_codec_flush_cache - Execute all pending (cached) amps / verbs
3704 * @codec: HD-audio codec
3706 void snd_hda_codec_flush_cache(struct hda_codec *codec)
3708 snd_hda_codec_resume_amp(codec);
3709 snd_hda_codec_resume_cache(codec);
3711 EXPORT_SYMBOL_GPL(snd_hda_codec_flush_cache);
3714 * snd_hda_codec_set_power_to_all - Set the power state to all widgets
3715 * @codec: the HDA codec
3716 * @fg: function group (not used now)
3717 * @power_state: the power state to set (AC_PWRST_*)
3719 * Set the given power state to all widgets that have the power control.
3720 * If the codec has power_filter set, it evaluates the power state and
3721 * filter out if it's unchanged as D3.
3723 void snd_hda_codec_set_power_to_all(struct hda_codec *codec, hda_nid_t fg,
3724 unsigned int power_state)
3726 hda_nid_t nid = codec->start_nid;
3729 for (i = 0; i < codec->num_nodes; i++, nid++) {
3730 unsigned int wcaps = get_wcaps(codec, nid);
3731 unsigned int state = power_state;
3732 if (!(wcaps & AC_WCAP_POWER))
3734 if (codec->power_filter) {
3735 state = codec->power_filter(codec, nid, power_state);
3736 if (state != power_state && power_state == AC_PWRST_D3)
3739 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE,
3743 EXPORT_SYMBOL_GPL(snd_hda_codec_set_power_to_all);
3746 * supported power states check
3748 static bool snd_hda_codec_get_supported_ps(struct hda_codec *codec, hda_nid_t fg,
3749 unsigned int power_state)
3751 int sup = snd_hda_param_read(codec, fg, AC_PAR_POWER_STATE);
3755 if (sup & power_state)
3762 * wait until the state is reached, returns the current state
3764 static unsigned int hda_sync_power_state(struct hda_codec *codec,
3766 unsigned int power_state)
3768 unsigned long end_time = jiffies + msecs_to_jiffies(500);
3769 unsigned int state, actual_state;
3772 state = snd_hda_codec_read(codec, fg, 0,
3773 AC_VERB_GET_POWER_STATE, 0);
3774 if (state & AC_PWRST_ERROR)
3776 actual_state = (state >> 4) & 0x0f;
3777 if (actual_state == power_state)
3779 if (time_after_eq(jiffies, end_time))
3781 /* wait until the codec reachs to the target state */
3788 * snd_hda_codec_eapd_power_filter - A power filter callback for EAPD
3789 * @codec: the HDA codec
3791 * @power_state: power state to evalue
3793 * Don't power down the widget if it controls eapd and EAPD_BTLENABLE is set.
3794 * This can be used a codec power_filter callback.
3796 unsigned int snd_hda_codec_eapd_power_filter(struct hda_codec *codec,
3798 unsigned int power_state)
3800 if (nid == codec->afg || nid == codec->mfg)
3802 if (power_state == AC_PWRST_D3 &&
3803 get_wcaps_type(get_wcaps(codec, nid)) == AC_WID_PIN &&
3804 (snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD)) {
3805 int eapd = snd_hda_codec_read(codec, nid, 0,
3806 AC_VERB_GET_EAPD_BTLENABLE, 0);
3812 EXPORT_SYMBOL_GPL(snd_hda_codec_eapd_power_filter);
3815 * set power state of the codec, and return the power state
3817 static unsigned int hda_set_power_state(struct hda_codec *codec,
3818 unsigned int power_state)
3820 hda_nid_t fg = codec->afg ? codec->afg : codec->mfg;
3825 /* this delay seems necessary to avoid click noise at power-down */
3826 if (power_state == AC_PWRST_D3) {
3827 if (codec->depop_delay < 0)
3828 msleep(codec->epss ? 10 : 100);
3829 else if (codec->depop_delay > 0)
3830 msleep(codec->depop_delay);
3831 flags = HDA_RW_NO_RESPONSE_FALLBACK;
3834 /* repeat power states setting at most 10 times*/
3835 for (count = 0; count < 10; count++) {
3836 if (codec->patch_ops.set_power_state)
3837 codec->patch_ops.set_power_state(codec, fg,
3840 state = power_state;
3841 if (codec->power_filter)
3842 state = codec->power_filter(codec, fg, state);
3843 if (state == power_state || power_state != AC_PWRST_D3)
3844 snd_hda_codec_read(codec, fg, flags,
3845 AC_VERB_SET_POWER_STATE,
3847 snd_hda_codec_set_power_to_all(codec, fg, power_state);
3849 state = hda_sync_power_state(codec, fg, power_state);
3850 if (!(state & AC_PWRST_ERROR))
3857 /* sync power states of all widgets;
3858 * this is called at the end of codec parsing
3860 static void sync_power_up_states(struct hda_codec *codec)
3862 hda_nid_t nid = codec->start_nid;
3865 /* don't care if no filter is used */
3866 if (!codec->power_filter)
3869 for (i = 0; i < codec->num_nodes; i++, nid++) {
3870 unsigned int wcaps = get_wcaps(codec, nid);
3871 unsigned int target;
3872 if (!(wcaps & AC_WCAP_POWER))
3874 target = codec->power_filter(codec, nid, AC_PWRST_D0);
3875 if (target == AC_PWRST_D0)
3877 if (!snd_hda_check_power_state(codec, nid, target))
3878 snd_hda_codec_write(codec, nid, 0,
3879 AC_VERB_SET_POWER_STATE, target);
3883 #ifdef CONFIG_SND_HDA_RECONFIG
3884 /* execute additional init verbs */
3885 static void hda_exec_init_verbs(struct hda_codec *codec)
3887 if (codec->init_verbs.list)
3888 snd_hda_sequence_write(codec, codec->init_verbs.list);
3891 static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
3896 * call suspend and power-down; used both from PM and power-save
3897 * this function returns the power state in the end
3899 static unsigned int hda_call_codec_suspend(struct hda_codec *codec, bool in_wq)
3905 if (codec->patch_ops.suspend)
3906 codec->patch_ops.suspend(codec);
3907 hda_cleanup_all_streams(codec);
3908 state = hda_set_power_state(codec, AC_PWRST_D3);
3909 /* Cancel delayed work if we aren't currently running from it. */
3911 cancel_delayed_work_sync(&codec->power_work);
3912 spin_lock(&codec->power_lock);
3913 snd_hda_update_power_acct(codec);
3914 trace_hda_power_down(codec);
3915 codec->power_on = 0;
3916 codec->power_transition = 0;
3917 codec->power_jiffies = jiffies;
3918 spin_unlock(&codec->power_lock);
3923 /* mark all entries of cmd and amp caches dirty */
3924 static void hda_mark_cmd_cache_dirty(struct hda_codec *codec)
3927 for (i = 0; i < codec->cmd_cache.buf.used; i++) {
3928 struct hda_cache_head *cmd;
3929 cmd = snd_array_elem(&codec->cmd_cache.buf, i);
3932 for (i = 0; i < codec->amp_cache.buf.used; i++) {
3933 struct hda_amp_info *amp;
3934 amp = snd_array_elem(&codec->amp_cache.buf, i);
3935 amp->head.dirty = 1;
3940 * kick up codec; used both from PM and power-save
3942 static void hda_call_codec_resume(struct hda_codec *codec)
3946 hda_mark_cmd_cache_dirty(codec);
3948 /* set as if powered on for avoiding re-entering the resume
3949 * in the resume / power-save sequence
3951 hda_keep_power_on(codec);
3952 hda_set_power_state(codec, AC_PWRST_D0);
3953 restore_shutup_pins(codec);
3954 hda_exec_init_verbs(codec);
3955 snd_hda_jack_set_dirty_all(codec);
3956 if (codec->patch_ops.resume)
3957 codec->patch_ops.resume(codec);
3959 if (codec->patch_ops.init)
3960 codec->patch_ops.init(codec);
3961 snd_hda_codec_resume_amp(codec);
3962 snd_hda_codec_resume_cache(codec);
3965 if (codec->jackpoll_interval)
3966 hda_jackpoll_work(&codec->jackpoll_work.work);
3968 snd_hda_jack_report_sync(codec);
3971 snd_hda_power_down(codec); /* flag down before returning */
3973 #endif /* CONFIG_PM */
3977 * snd_hda_build_controls - build mixer controls
3980 * Creates mixer controls for each codec included in the bus.
3982 * Returns 0 if successful, otherwise a negative error code.
3984 int snd_hda_build_controls(struct hda_bus *bus)
3986 struct hda_codec *codec;
3988 list_for_each_entry(codec, &bus->codec_list, list) {
3989 int err = snd_hda_codec_build_controls(codec);
3992 "cannot build controls for #%d (error %d)\n",
3994 err = snd_hda_codec_reset(codec);
3997 "cannot revert codec\n");
4004 EXPORT_SYMBOL_GPL(snd_hda_build_controls);
4007 * add standard channel maps if not specified
4009 static int add_std_chmaps(struct hda_codec *codec)
4013 for (i = 0; i < codec->num_pcms; i++) {
4014 for (str = 0; str < 2; str++) {
4015 struct snd_pcm *pcm = codec->pcm_info[i].pcm;
4016 struct hda_pcm_stream *hinfo =
4017 &codec->pcm_info[i].stream[str];
4018 struct snd_pcm_chmap *chmap;
4019 const struct snd_pcm_chmap_elem *elem;
4021 if (codec->pcm_info[i].own_chmap)
4023 if (!pcm || !hinfo->substreams)
4025 elem = hinfo->chmap ? hinfo->chmap : snd_pcm_std_chmaps;
4026 err = snd_pcm_add_chmap_ctls(pcm, str, elem,
4027 hinfo->channels_max,
4031 chmap->channel_mask = SND_PCM_CHMAP_MASK_2468;
4037 /* default channel maps for 2.1 speakers;
4038 * since HD-audio supports only stereo, odd number channels are omitted
4040 const struct snd_pcm_chmap_elem snd_pcm_2_1_chmaps[] = {
4042 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } },
4044 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
4045 SNDRV_CHMAP_LFE, SNDRV_CHMAP_LFE } },
4048 EXPORT_SYMBOL_GPL(snd_pcm_2_1_chmaps);
4050 int snd_hda_codec_build_controls(struct hda_codec *codec)
4053 hda_exec_init_verbs(codec);
4054 /* continue to initialize... */
4055 if (codec->patch_ops.init)
4056 err = codec->patch_ops.init(codec);
4057 if (!err && codec->patch_ops.build_controls)
4058 err = codec->patch_ops.build_controls(codec);
4062 /* we create chmaps here instead of build_pcms */
4063 err = add_std_chmaps(codec);
4067 if (codec->jackpoll_interval)
4068 hda_jackpoll_work(&codec->jackpoll_work.work);
4070 snd_hda_jack_report_sync(codec); /* call at the last init point */
4071 sync_power_up_states(codec);
4078 struct hda_rate_tbl {
4080 unsigned int alsa_bits;
4081 unsigned int hda_fmt;
4084 /* rate = base * mult / div */
4085 #define HDA_RATE(base, mult, div) \
4086 (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \
4087 (((div) - 1) << AC_FMT_DIV_SHIFT))
4089 static struct hda_rate_tbl rate_bits[] = {
4090 /* rate in Hz, ALSA rate bitmask, HDA format value */
4092 /* autodetected value used in snd_hda_query_supported_pcm */
4093 { 8000, SNDRV_PCM_RATE_8000, HDA_RATE(48, 1, 6) },
4094 { 11025, SNDRV_PCM_RATE_11025, HDA_RATE(44, 1, 4) },
4095 { 16000, SNDRV_PCM_RATE_16000, HDA_RATE(48, 1, 3) },
4096 { 22050, SNDRV_PCM_RATE_22050, HDA_RATE(44, 1, 2) },
4097 { 32000, SNDRV_PCM_RATE_32000, HDA_RATE(48, 2, 3) },
4098 { 44100, SNDRV_PCM_RATE_44100, HDA_RATE(44, 1, 1) },
4099 { 48000, SNDRV_PCM_RATE_48000, HDA_RATE(48, 1, 1) },
4100 { 88200, SNDRV_PCM_RATE_88200, HDA_RATE(44, 2, 1) },
4101 { 96000, SNDRV_PCM_RATE_96000, HDA_RATE(48, 2, 1) },
4102 { 176400, SNDRV_PCM_RATE_176400, HDA_RATE(44, 4, 1) },
4103 { 192000, SNDRV_PCM_RATE_192000, HDA_RATE(48, 4, 1) },
4104 #define AC_PAR_PCM_RATE_BITS 11
4105 /* up to bits 10, 384kHZ isn't supported properly */
4107 /* not autodetected value */
4108 { 9600, SNDRV_PCM_RATE_KNOT, HDA_RATE(48, 1, 5) },
4110 { 0 } /* terminator */
4114 * snd_hda_calc_stream_format - calculate format bitset
4115 * @codec: HD-audio codec
4116 * @rate: the sample rate
4117 * @channels: the number of channels
4118 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
4119 * @maxbps: the max. bps
4120 * @spdif_ctls: HD-audio SPDIF status bits (0 if irrelevant)
4122 * Calculate the format bitset from the given rate, channels and th PCM format.
4124 * Return zero if invalid.
4126 unsigned int snd_hda_calc_stream_format(struct hda_codec *codec,
4128 unsigned int channels,
4129 unsigned int format,
4130 unsigned int maxbps,
4131 unsigned short spdif_ctls)
4134 unsigned int val = 0;
4136 for (i = 0; rate_bits[i].hz; i++)
4137 if (rate_bits[i].hz == rate) {
4138 val = rate_bits[i].hda_fmt;
4141 if (!rate_bits[i].hz) {
4142 codec_dbg(codec, "invalid rate %d\n", rate);
4146 if (channels == 0 || channels > 8) {
4147 codec_dbg(codec, "invalid channels %d\n", channels);
4150 val |= channels - 1;
4152 switch (snd_pcm_format_width(format)) {
4154 val |= AC_FMT_BITS_8;
4157 val |= AC_FMT_BITS_16;
4162 if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE)
4163 val |= AC_FMT_BITS_32;
4164 else if (maxbps >= 24)
4165 val |= AC_FMT_BITS_24;
4167 val |= AC_FMT_BITS_20;
4170 codec_dbg(codec, "invalid format width %d\n",
4171 snd_pcm_format_width(format));
4175 if (spdif_ctls & AC_DIG1_NONAUDIO)
4176 val |= AC_FMT_TYPE_NON_PCM;
4180 EXPORT_SYMBOL_GPL(snd_hda_calc_stream_format);
4182 static unsigned int get_pcm_param(struct hda_codec *codec, hda_nid_t nid,
4185 unsigned int val = 0;
4186 if (nid != codec->afg &&
4187 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD))
4188 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
4189 if (!val || val == -1)
4190 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
4191 if (!val || val == -1)
4196 static unsigned int query_pcm_param(struct hda_codec *codec, hda_nid_t nid)
4198 return query_caps_hash(codec, nid, 0, HDA_HASH_PARPCM_KEY(nid),
4202 static unsigned int get_stream_param(struct hda_codec *codec, hda_nid_t nid,
4205 unsigned int streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
4206 if (!streams || streams == -1)
4207 streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
4208 if (!streams || streams == -1)
4213 static unsigned int query_stream_param(struct hda_codec *codec, hda_nid_t nid)
4215 return query_caps_hash(codec, nid, 0, HDA_HASH_PARSTR_KEY(nid),
4220 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
4221 * @codec: the HDA codec
4222 * @nid: NID to query
4223 * @ratesp: the pointer to store the detected rate bitflags
4224 * @formatsp: the pointer to store the detected formats
4225 * @bpsp: the pointer to store the detected format widths
4227 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
4228 * or @bsps argument is ignored.
4230 * Returns 0 if successful, otherwise a negative error code.
4232 int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
4233 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
4235 unsigned int i, val, wcaps;
4237 wcaps = get_wcaps(codec, nid);
4238 val = query_pcm_param(codec, nid);
4242 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
4244 rates |= rate_bits[i].alsa_bits;
4248 "rates == 0 (nid=0x%x, val=0x%x, ovrd=%i)\n",
4250 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0);
4256 if (formatsp || bpsp) {
4258 unsigned int streams, bps;
4260 streams = query_stream_param(codec, nid);
4265 if (streams & AC_SUPFMT_PCM) {
4266 if (val & AC_SUPPCM_BITS_8) {
4267 formats |= SNDRV_PCM_FMTBIT_U8;
4270 if (val & AC_SUPPCM_BITS_16) {
4271 formats |= SNDRV_PCM_FMTBIT_S16_LE;
4274 if (wcaps & AC_WCAP_DIGITAL) {
4275 if (val & AC_SUPPCM_BITS_32)
4276 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
4277 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
4278 formats |= SNDRV_PCM_FMTBIT_S32_LE;
4279 if (val & AC_SUPPCM_BITS_24)
4281 else if (val & AC_SUPPCM_BITS_20)
4283 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
4284 AC_SUPPCM_BITS_32)) {
4285 formats |= SNDRV_PCM_FMTBIT_S32_LE;
4286 if (val & AC_SUPPCM_BITS_32)
4288 else if (val & AC_SUPPCM_BITS_24)
4290 else if (val & AC_SUPPCM_BITS_20)
4294 #if 0 /* FIXME: CS4206 doesn't work, which is the only codec supporting float */
4295 if (streams & AC_SUPFMT_FLOAT32) {
4296 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
4301 if (streams == AC_SUPFMT_AC3) {
4302 /* should be exclusive */
4303 /* temporary hack: we have still no proper support
4304 * for the direct AC3 stream...
4306 formats |= SNDRV_PCM_FMTBIT_U8;
4311 "formats == 0 (nid=0x%x, val=0x%x, ovrd=%i, streams=0x%x)\n",
4313 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0,
4318 *formatsp = formats;
4325 EXPORT_SYMBOL_GPL(snd_hda_query_supported_pcm);
4328 * snd_hda_is_supported_format - Check the validity of the format
4329 * @codec: HD-audio codec
4330 * @nid: NID to check
4331 * @format: the HD-audio format value to check
4333 * Check whether the given node supports the format value.
4335 * Returns 1 if supported, 0 if not.
4337 int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
4338 unsigned int format)
4341 unsigned int val = 0, rate, stream;
4343 val = query_pcm_param(codec, nid);
4347 rate = format & 0xff00;
4348 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
4349 if (rate_bits[i].hda_fmt == rate) {
4354 if (i >= AC_PAR_PCM_RATE_BITS)
4357 stream = query_stream_param(codec, nid);
4361 if (stream & AC_SUPFMT_PCM) {
4362 switch (format & 0xf0) {
4364 if (!(val & AC_SUPPCM_BITS_8))
4368 if (!(val & AC_SUPPCM_BITS_16))
4372 if (!(val & AC_SUPPCM_BITS_20))
4376 if (!(val & AC_SUPPCM_BITS_24))
4380 if (!(val & AC_SUPPCM_BITS_32))
4387 /* FIXME: check for float32 and AC3? */
4392 EXPORT_SYMBOL_GPL(snd_hda_is_supported_format);
4397 static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
4398 struct hda_codec *codec,
4399 struct snd_pcm_substream *substream)
4404 static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
4405 struct hda_codec *codec,
4406 unsigned int stream_tag,
4407 unsigned int format,
4408 struct snd_pcm_substream *substream)
4410 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4414 static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
4415 struct hda_codec *codec,
4416 struct snd_pcm_substream *substream)
4418 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
4422 static int set_pcm_default_values(struct hda_codec *codec,
4423 struct hda_pcm_stream *info)
4427 /* query support PCM information from the given NID */
4428 if (info->nid && (!info->rates || !info->formats)) {
4429 err = snd_hda_query_supported_pcm(codec, info->nid,
4430 info->rates ? NULL : &info->rates,
4431 info->formats ? NULL : &info->formats,
4432 info->maxbps ? NULL : &info->maxbps);
4436 if (info->ops.open == NULL)
4437 info->ops.open = hda_pcm_default_open_close;
4438 if (info->ops.close == NULL)
4439 info->ops.close = hda_pcm_default_open_close;
4440 if (info->ops.prepare == NULL) {
4441 if (snd_BUG_ON(!info->nid))
4443 info->ops.prepare = hda_pcm_default_prepare;
4445 if (info->ops.cleanup == NULL) {
4446 if (snd_BUG_ON(!info->nid))
4448 info->ops.cleanup = hda_pcm_default_cleanup;
4454 * codec prepare/cleanup entries
4457 * snd_hda_codec_prepare - Prepare a stream
4458 * @codec: the HDA codec
4459 * @hinfo: PCM information
4460 * @stream: stream tag to assign
4461 * @format: format id to assign
4462 * @substream: PCM substream to assign
4464 * Calls the prepare callback set by the codec with the given arguments.
4465 * Clean up the inactive streams when successful.
4467 int snd_hda_codec_prepare(struct hda_codec *codec,
4468 struct hda_pcm_stream *hinfo,
4469 unsigned int stream,
4470 unsigned int format,
4471 struct snd_pcm_substream *substream)
4474 mutex_lock(&codec->bus->prepare_mutex);
4475 ret = hinfo->ops.prepare(hinfo, codec, stream, format, substream);
4477 purify_inactive_streams(codec);
4478 mutex_unlock(&codec->bus->prepare_mutex);
4481 EXPORT_SYMBOL_GPL(snd_hda_codec_prepare);
4484 * snd_hda_codec_cleanup - Prepare a stream
4485 * @codec: the HDA codec
4486 * @hinfo: PCM information
4487 * @substream: PCM substream
4489 * Calls the cleanup callback set by the codec with the given arguments.
4491 void snd_hda_codec_cleanup(struct hda_codec *codec,
4492 struct hda_pcm_stream *hinfo,
4493 struct snd_pcm_substream *substream)
4495 mutex_lock(&codec->bus->prepare_mutex);
4496 hinfo->ops.cleanup(hinfo, codec, substream);
4497 mutex_unlock(&codec->bus->prepare_mutex);
4499 EXPORT_SYMBOL_GPL(snd_hda_codec_cleanup);
4502 const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = {
4503 "Audio", "SPDIF", "HDMI", "Modem"
4507 * get the empty PCM device number to assign
4509 static int get_empty_pcm_device(struct hda_bus *bus, unsigned int type)
4511 /* audio device indices; not linear to keep compatibility */
4512 /* assigned to static slots up to dev#10; if more needed, assign
4513 * the later slot dynamically (when CONFIG_SND_DYNAMIC_MINORS=y)
4515 static int audio_idx[HDA_PCM_NTYPES][5] = {
4516 [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 },
4517 [HDA_PCM_TYPE_SPDIF] = { 1, -1 },
4518 [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 },
4519 [HDA_PCM_TYPE_MODEM] = { 6, -1 },
4523 if (type >= HDA_PCM_NTYPES) {
4524 dev_err(bus->card->dev, "Invalid PCM type %d\n", type);
4528 for (i = 0; audio_idx[type][i] >= 0; i++) {
4529 #ifndef CONFIG_SND_DYNAMIC_MINORS
4530 if (audio_idx[type][i] >= 8)
4533 if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits))
4534 return audio_idx[type][i];
4537 #ifdef CONFIG_SND_DYNAMIC_MINORS
4538 /* non-fixed slots starting from 10 */
4539 for (i = 10; i < 32; i++) {
4540 if (!test_and_set_bit(i, bus->pcm_dev_bits))
4545 dev_warn(bus->card->dev, "Too many %s devices\n",
4546 snd_hda_pcm_type_name[type]);
4547 #ifndef CONFIG_SND_DYNAMIC_MINORS
4548 dev_warn(bus->card->dev,
4549 "Consider building the kernel with CONFIG_SND_DYNAMIC_MINORS=y\n");
4555 * attach a new PCM stream
4557 static int snd_hda_attach_pcm(struct hda_codec *codec, struct hda_pcm *pcm)
4559 struct hda_bus *bus = codec->bus;
4560 struct hda_pcm_stream *info;
4563 if (snd_BUG_ON(!pcm->name))
4565 for (stream = 0; stream < 2; stream++) {
4566 info = &pcm->stream[stream];
4567 if (info->substreams) {
4568 err = set_pcm_default_values(codec, info);
4573 return bus->ops.attach_pcm(bus, codec, pcm);
4576 /* assign all PCMs of the given codec */
4577 int snd_hda_codec_build_pcms(struct hda_codec *codec)
4582 if (!codec->num_pcms) {
4583 if (!codec->patch_ops.build_pcms)
4585 err = codec->patch_ops.build_pcms(codec);
4588 "cannot build PCMs for #%d (error %d)\n",
4590 err = snd_hda_codec_reset(codec);
4593 "cannot revert codec\n");
4598 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
4599 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
4602 if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
4603 continue; /* no substreams assigned */
4606 dev = get_empty_pcm_device(codec->bus, cpcm->pcm_type);
4608 continue; /* no fatal error */
4610 err = snd_hda_attach_pcm(codec, cpcm);
4613 "cannot attach PCM stream %d for codec #%d\n",
4615 continue; /* no fatal error */
4623 * snd_hda_build_pcms - build PCM information
4626 * Create PCM information for each codec included in the bus.
4628 * The build_pcms codec patch is requested to set up codec->num_pcms and
4629 * codec->pcm_info properly. The array is referred by the top-level driver
4630 * to create its PCM instances.
4631 * The allocated codec->pcm_info should be released in codec->patch_ops.free
4634 * At least, substreams, channels_min and channels_max must be filled for
4635 * each stream. substreams = 0 indicates that the stream doesn't exist.
4636 * When rates and/or formats are zero, the supported values are queried
4637 * from the given nid. The nid is used also by the default ops.prepare
4638 * and ops.cleanup callbacks.
4640 * The driver needs to call ops.open in its open callback. Similarly,
4641 * ops.close is supposed to be called in the close callback.
4642 * ops.prepare should be called in the prepare or hw_params callback
4643 * with the proper parameters for set up.
4644 * ops.cleanup should be called in hw_free for clean up of streams.
4646 * This function returns 0 if successful, or a negative error code.
4648 int snd_hda_build_pcms(struct hda_bus *bus)
4650 struct hda_codec *codec;
4652 list_for_each_entry(codec, &bus->codec_list, list) {
4653 int err = snd_hda_codec_build_pcms(codec);
4659 EXPORT_SYMBOL_GPL(snd_hda_build_pcms);
4662 * snd_hda_add_new_ctls - create controls from the array
4663 * @codec: the HDA codec
4664 * @knew: the array of struct snd_kcontrol_new
4666 * This helper function creates and add new controls in the given array.
4667 * The array must be terminated with an empty entry as terminator.
4669 * Returns 0 if successful, or a negative error code.
4671 int snd_hda_add_new_ctls(struct hda_codec *codec,
4672 const struct snd_kcontrol_new *knew)
4676 for (; knew->name; knew++) {
4677 struct snd_kcontrol *kctl;
4678 int addr = 0, idx = 0;
4679 if (knew->iface == -1) /* skip this codec private value */
4682 kctl = snd_ctl_new1(knew, codec);
4686 kctl->id.device = addr;
4688 kctl->id.index = idx;
4689 err = snd_hda_ctl_add(codec, 0, kctl);
4692 /* try first with another device index corresponding to
4693 * the codec addr; if it still fails (or it's the
4694 * primary codec), then try another control index
4696 if (!addr && codec->addr)
4698 else if (!idx && !knew->index) {
4699 idx = find_empty_mixer_ctl_idx(codec,
4709 EXPORT_SYMBOL_GPL(snd_hda_add_new_ctls);
4712 static void hda_power_work(struct work_struct *work)
4714 struct hda_codec *codec =
4715 container_of(work, struct hda_codec, power_work.work);
4716 struct hda_bus *bus = codec->bus;
4719 spin_lock(&codec->power_lock);
4720 if (codec->power_transition > 0) { /* during power-up sequence? */
4721 spin_unlock(&codec->power_lock);
4724 if (!codec->power_on || codec->power_count) {
4725 codec->power_transition = 0;
4726 spin_unlock(&codec->power_lock);
4729 spin_unlock(&codec->power_lock);
4731 state = hda_call_codec_suspend(codec, true);
4732 if (!bus->power_keep_link_on && (state & AC_PWRST_CLK_STOP_OK))
4733 hda_call_pm_notify(codec, false);
4736 static void hda_keep_power_on(struct hda_codec *codec)
4738 spin_lock(&codec->power_lock);
4739 codec->power_count++;
4740 codec->power_on = 1;
4741 codec->power_jiffies = jiffies;
4742 spin_unlock(&codec->power_lock);
4743 hda_call_pm_notify(codec, true);
4746 /* update the power on/off account with the current jiffies */
4747 void snd_hda_update_power_acct(struct hda_codec *codec)
4749 unsigned long delta = jiffies - codec->power_jiffies;
4750 if (codec->power_on)
4751 codec->power_on_acct += delta;
4753 codec->power_off_acct += delta;
4754 codec->power_jiffies += delta;
4757 /* Transition to powered up, if wait_power_down then wait for a pending
4758 * transition to D3 to complete. A pending D3 transition is indicated
4759 * with power_transition == -1. */
4760 /* call this with codec->power_lock held! */
4761 static void __snd_hda_power_up(struct hda_codec *codec, bool wait_power_down)
4763 /* Return if power_on or transitioning to power_on, unless currently
4765 if ((codec->power_on || codec->power_transition > 0) &&
4766 !(wait_power_down && codec->power_transition < 0))
4768 spin_unlock(&codec->power_lock);
4770 cancel_delayed_work_sync(&codec->power_work);
4772 spin_lock(&codec->power_lock);
4773 /* If the power down delayed work was cancelled above before starting,
4774 * then there is no need to go through power up here.
4776 if (codec->power_on) {
4777 if (codec->power_transition < 0)
4778 codec->power_transition = 0;
4782 trace_hda_power_up(codec);
4783 snd_hda_update_power_acct(codec);
4784 codec->power_on = 1;
4785 codec->power_jiffies = jiffies;
4786 codec->power_transition = 1; /* avoid reentrance */
4787 spin_unlock(&codec->power_lock);
4789 hda_call_codec_resume(codec);
4791 spin_lock(&codec->power_lock);
4792 codec->power_transition = 0;
4795 #define power_save(codec) \
4796 ((codec)->bus->power_save ? *(codec)->bus->power_save : 0)
4798 /* Transition to powered down */
4799 static void __snd_hda_power_down(struct hda_codec *codec)
4801 if (!codec->power_on || codec->power_count || codec->power_transition)
4804 if (power_save(codec)) {
4805 codec->power_transition = -1; /* avoid reentrance */
4806 queue_delayed_work(codec->bus->workq, &codec->power_work,
4807 msecs_to_jiffies(power_save(codec) * 1000));
4812 * snd_hda_power_save - Power-up/down/sync the codec
4813 * @codec: HD-audio codec
4814 * @delta: the counter delta to change
4815 * @d3wait: sync for D3 transition complete
4817 * Change the power-up counter via @delta, and power up or down the hardware
4818 * appropriately. For the power-down, queue to the delayed action.
4819 * Passing zero to @delta means to synchronize the power state.
4821 void snd_hda_power_save(struct hda_codec *codec, int delta, bool d3wait)
4823 spin_lock(&codec->power_lock);
4824 codec->power_count += delta;
4825 trace_hda_power_count(codec);
4827 __snd_hda_power_up(codec, d3wait);
4829 __snd_hda_power_down(codec);
4830 spin_unlock(&codec->power_lock);
4832 EXPORT_SYMBOL_GPL(snd_hda_power_save);
4835 * snd_hda_check_amp_list_power - Check the amp list and update the power
4836 * @codec: HD-audio codec
4837 * @check: the object containing an AMP list and the status
4838 * @nid: NID to check / update
4840 * Check whether the given NID is in the amp list. If it's in the list,
4841 * check the current AMP status, and update the the power-status according
4842 * to the mute status.
4844 * This function is supposed to be set or called from the check_power_status
4847 int snd_hda_check_amp_list_power(struct hda_codec *codec,
4848 struct hda_loopback_check *check,
4851 const struct hda_amp_list *p;
4854 if (!check->amplist)
4856 for (p = check->amplist; p->nid; p++) {
4861 return 0; /* nothing changed */
4863 for (p = check->amplist; p->nid; p++) {
4864 for (ch = 0; ch < 2; ch++) {
4865 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
4867 if (!(v & HDA_AMP_MUTE) && v > 0) {
4868 if (!check->power_on) {
4869 check->power_on = 1;
4870 snd_hda_power_up(codec);
4876 if (check->power_on) {
4877 check->power_on = 0;
4878 snd_hda_power_down(codec);
4882 EXPORT_SYMBOL_GPL(snd_hda_check_amp_list_power);
4886 * Channel mode helper
4890 * snd_hda_ch_mode_info - Info callback helper for the channel mode enum
4891 * @codec: the HDA codec
4892 * @uinfo: pointer to get/store the data
4893 * @chmode: channel mode array
4894 * @num_chmodes: channel mode array size
4896 int snd_hda_ch_mode_info(struct hda_codec *codec,
4897 struct snd_ctl_elem_info *uinfo,
4898 const struct hda_channel_mode *chmode,
4901 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4903 uinfo->value.enumerated.items = num_chmodes;
4904 if (uinfo->value.enumerated.item >= num_chmodes)
4905 uinfo->value.enumerated.item = num_chmodes - 1;
4906 sprintf(uinfo->value.enumerated.name, "%dch",
4907 chmode[uinfo->value.enumerated.item].channels);
4910 EXPORT_SYMBOL_GPL(snd_hda_ch_mode_info);
4913 * snd_hda_ch_mode_get - Get callback helper for the channel mode enum
4914 * @codec: the HDA codec
4915 * @ucontrol: pointer to get/store the data
4916 * @chmode: channel mode array
4917 * @num_chmodes: channel mode array size
4918 * @max_channels: max number of channels
4920 int snd_hda_ch_mode_get(struct hda_codec *codec,
4921 struct snd_ctl_elem_value *ucontrol,
4922 const struct hda_channel_mode *chmode,
4928 for (i = 0; i < num_chmodes; i++) {
4929 if (max_channels == chmode[i].channels) {
4930 ucontrol->value.enumerated.item[0] = i;
4936 EXPORT_SYMBOL_GPL(snd_hda_ch_mode_get);
4939 * snd_hda_ch_mode_put - Put callback helper for the channel mode enum
4940 * @codec: the HDA codec
4941 * @ucontrol: pointer to get/store the data
4942 * @chmode: channel mode array
4943 * @num_chmodes: channel mode array size
4944 * @max_channelsp: pointer to store the max channels
4946 int snd_hda_ch_mode_put(struct hda_codec *codec,
4947 struct snd_ctl_elem_value *ucontrol,
4948 const struct hda_channel_mode *chmode,
4954 mode = ucontrol->value.enumerated.item[0];
4955 if (mode >= num_chmodes)
4957 if (*max_channelsp == chmode[mode].channels)
4959 /* change the current channel setting */
4960 *max_channelsp = chmode[mode].channels;
4961 if (chmode[mode].sequence)
4962 snd_hda_sequence_write_cache(codec, chmode[mode].sequence);
4965 EXPORT_SYMBOL_GPL(snd_hda_ch_mode_put);
4972 * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum
4973 * @imux: imux helper object
4974 * @uinfo: pointer to get/store the data
4976 int snd_hda_input_mux_info(const struct hda_input_mux *imux,
4977 struct snd_ctl_elem_info *uinfo)
4981 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4983 uinfo->value.enumerated.items = imux->num_items;
4984 if (!imux->num_items)
4986 index = uinfo->value.enumerated.item;
4987 if (index >= imux->num_items)
4988 index = imux->num_items - 1;
4989 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
4992 EXPORT_SYMBOL_GPL(snd_hda_input_mux_info);
4995 * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum
4996 * @codec: the HDA codec
4997 * @imux: imux helper object
4998 * @ucontrol: pointer to get/store the data
4999 * @nid: input mux NID
5000 * @cur_val: pointer to get/store the current imux value
5002 int snd_hda_input_mux_put(struct hda_codec *codec,
5003 const struct hda_input_mux *imux,
5004 struct snd_ctl_elem_value *ucontrol,
5006 unsigned int *cur_val)
5010 if (!imux->num_items)
5012 idx = ucontrol->value.enumerated.item[0];
5013 if (idx >= imux->num_items)
5014 idx = imux->num_items - 1;
5015 if (*cur_val == idx)
5017 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
5018 imux->items[idx].index);
5022 EXPORT_SYMBOL_GPL(snd_hda_input_mux_put);
5026 * snd_hda_enum_helper_info - Helper for simple enum ctls
5027 * @kcontrol: ctl element
5028 * @uinfo: pointer to get/store the data
5029 * @num_items: number of enum items
5030 * @texts: enum item string array
5032 * process kcontrol info callback of a simple string enum array
5033 * when @num_items is 0 or @texts is NULL, assume a boolean enum array
5035 int snd_hda_enum_helper_info(struct snd_kcontrol *kcontrol,
5036 struct snd_ctl_elem_info *uinfo,
5037 int num_items, const char * const *texts)
5039 static const char * const texts_default[] = {
5040 "Disabled", "Enabled"
5043 if (!texts || !num_items) {
5045 texts = texts_default;
5048 return snd_ctl_enum_info(uinfo, 1, num_items, texts);
5050 EXPORT_SYMBOL_GPL(snd_hda_enum_helper_info);
5053 * Multi-channel / digital-out PCM helper functions
5056 /* setup SPDIF output stream */
5057 static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
5058 unsigned int stream_tag, unsigned int format)
5060 struct hda_spdif_out *spdif;
5061 unsigned int curr_fmt;
5064 spdif = snd_hda_spdif_out_of_nid(codec, nid);
5065 curr_fmt = snd_hda_codec_read(codec, nid, 0,
5066 AC_VERB_GET_STREAM_FORMAT, 0);
5067 reset = codec->spdif_status_reset &&
5068 (spdif->ctls & AC_DIG1_ENABLE) &&
5071 /* turn off SPDIF if needed; otherwise the IEC958 bits won't be
5074 set_dig_out_convert(codec, nid,
5075 spdif->ctls & ~AC_DIG1_ENABLE & 0xff,
5077 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
5078 if (codec->slave_dig_outs) {
5080 for (d = codec->slave_dig_outs; *d; d++)
5081 snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
5084 /* turn on again (if needed) */
5086 set_dig_out_convert(codec, nid,
5087 spdif->ctls & 0xff, -1);
5090 static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
5092 snd_hda_codec_cleanup_stream(codec, nid);
5093 if (codec->slave_dig_outs) {
5095 for (d = codec->slave_dig_outs; *d; d++)
5096 snd_hda_codec_cleanup_stream(codec, *d);
5101 * snd_hda_bus_reboot_notify - call the reboot notifier of each codec
5102 * @bus: HD-audio bus
5104 void snd_hda_bus_reboot_notify(struct hda_bus *bus)
5106 struct hda_codec *codec;
5110 list_for_each_entry(codec, &bus->codec_list, list) {
5111 if (hda_codec_is_power_on(codec) &&
5112 codec->patch_ops.reboot_notify)
5113 codec->patch_ops.reboot_notify(codec);
5116 EXPORT_SYMBOL_GPL(snd_hda_bus_reboot_notify);
5119 * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode
5120 * @codec: the HDA codec
5121 * @mout: hda_multi_out object
5123 int snd_hda_multi_out_dig_open(struct hda_codec *codec,
5124 struct hda_multi_out *mout)
5126 mutex_lock(&codec->spdif_mutex);
5127 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
5128 /* already opened as analog dup; reset it once */
5129 cleanup_dig_out_stream(codec, mout->dig_out_nid);
5130 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
5131 mutex_unlock(&codec->spdif_mutex);
5134 EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_open);
5137 * snd_hda_multi_out_dig_prepare - prepare the digital out stream
5138 * @codec: the HDA codec
5139 * @mout: hda_multi_out object
5140 * @stream_tag: stream tag to assign
5141 * @format: format id to assign
5142 * @substream: PCM substream to assign
5144 int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
5145 struct hda_multi_out *mout,
5146 unsigned int stream_tag,
5147 unsigned int format,
5148 struct snd_pcm_substream *substream)
5150 mutex_lock(&codec->spdif_mutex);
5151 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
5152 mutex_unlock(&codec->spdif_mutex);
5155 EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_prepare);
5158 * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream
5159 * @codec: the HDA codec
5160 * @mout: hda_multi_out object
5162 int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec,
5163 struct hda_multi_out *mout)
5165 mutex_lock(&codec->spdif_mutex);
5166 cleanup_dig_out_stream(codec, mout->dig_out_nid);
5167 mutex_unlock(&codec->spdif_mutex);
5170 EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_cleanup);
5173 * snd_hda_multi_out_dig_close - release the digital out stream
5174 * @codec: the HDA codec
5175 * @mout: hda_multi_out object
5177 int snd_hda_multi_out_dig_close(struct hda_codec *codec,
5178 struct hda_multi_out *mout)
5180 mutex_lock(&codec->spdif_mutex);
5181 mout->dig_out_used = 0;
5182 mutex_unlock(&codec->spdif_mutex);
5185 EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_close);
5188 * snd_hda_multi_out_analog_open - open analog outputs
5189 * @codec: the HDA codec
5190 * @mout: hda_multi_out object
5191 * @substream: PCM substream to assign
5192 * @hinfo: PCM information to assign
5194 * Open analog outputs and set up the hw-constraints.
5195 * If the digital outputs can be opened as slave, open the digital
5198 int snd_hda_multi_out_analog_open(struct hda_codec *codec,
5199 struct hda_multi_out *mout,
5200 struct snd_pcm_substream *substream,
5201 struct hda_pcm_stream *hinfo)
5203 struct snd_pcm_runtime *runtime = substream->runtime;
5204 runtime->hw.channels_max = mout->max_channels;
5205 if (mout->dig_out_nid) {
5206 if (!mout->analog_rates) {
5207 mout->analog_rates = hinfo->rates;
5208 mout->analog_formats = hinfo->formats;
5209 mout->analog_maxbps = hinfo->maxbps;
5211 runtime->hw.rates = mout->analog_rates;
5212 runtime->hw.formats = mout->analog_formats;
5213 hinfo->maxbps = mout->analog_maxbps;
5215 if (!mout->spdif_rates) {
5216 snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
5218 &mout->spdif_formats,
5219 &mout->spdif_maxbps);
5221 mutex_lock(&codec->spdif_mutex);
5222 if (mout->share_spdif) {
5223 if ((runtime->hw.rates & mout->spdif_rates) &&
5224 (runtime->hw.formats & mout->spdif_formats)) {
5225 runtime->hw.rates &= mout->spdif_rates;
5226 runtime->hw.formats &= mout->spdif_formats;
5227 if (mout->spdif_maxbps < hinfo->maxbps)
5228 hinfo->maxbps = mout->spdif_maxbps;
5230 mout->share_spdif = 0;
5231 /* FIXME: need notify? */
5234 mutex_unlock(&codec->spdif_mutex);
5236 return snd_pcm_hw_constraint_step(substream->runtime, 0,
5237 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
5239 EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_open);
5242 * snd_hda_multi_out_analog_prepare - Preapre the analog outputs.
5243 * @codec: the HDA codec
5244 * @mout: hda_multi_out object
5245 * @stream_tag: stream tag to assign
5246 * @format: format id to assign
5247 * @substream: PCM substream to assign
5249 * Set up the i/o for analog out.
5250 * When the digital out is available, copy the front out to digital out, too.
5252 int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
5253 struct hda_multi_out *mout,
5254 unsigned int stream_tag,
5255 unsigned int format,
5256 struct snd_pcm_substream *substream)
5258 const hda_nid_t *nids = mout->dac_nids;
5259 int chs = substream->runtime->channels;
5260 struct hda_spdif_out *spdif;
5263 mutex_lock(&codec->spdif_mutex);
5264 spdif = snd_hda_spdif_out_of_nid(codec, mout->dig_out_nid);
5265 if (mout->dig_out_nid && mout->share_spdif &&
5266 mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
5268 snd_hda_is_supported_format(codec, mout->dig_out_nid,
5270 !(spdif->status & IEC958_AES0_NONAUDIO)) {
5271 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
5272 setup_dig_out_stream(codec, mout->dig_out_nid,
5273 stream_tag, format);
5275 mout->dig_out_used = 0;
5276 cleanup_dig_out_stream(codec, mout->dig_out_nid);
5279 mutex_unlock(&codec->spdif_mutex);
5282 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
5284 if (!mout->no_share_stream &&
5285 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
5286 /* headphone out will just decode front left/right (stereo) */
5287 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
5289 /* extra outputs copied from front */
5290 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
5291 if (!mout->no_share_stream && mout->hp_out_nid[i])
5292 snd_hda_codec_setup_stream(codec,
5293 mout->hp_out_nid[i],
5294 stream_tag, 0, format);
5297 for (i = 1; i < mout->num_dacs; i++) {
5298 if (chs >= (i + 1) * 2) /* independent out */
5299 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
5301 else if (!mout->no_share_stream) /* copy front */
5302 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
5306 /* extra surrounds */
5307 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++) {
5309 if (!mout->extra_out_nid[i])
5311 if (chs >= (i + 1) * 2)
5313 else if (!mout->no_share_stream)
5315 snd_hda_codec_setup_stream(codec, mout->extra_out_nid[i],
5316 stream_tag, ch, format);
5321 EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_prepare);
5324 * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out
5325 * @codec: the HDA codec
5326 * @mout: hda_multi_out object
5328 int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
5329 struct hda_multi_out *mout)
5331 const hda_nid_t *nids = mout->dac_nids;
5334 for (i = 0; i < mout->num_dacs; i++)
5335 snd_hda_codec_cleanup_stream(codec, nids[i]);
5337 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
5338 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
5339 if (mout->hp_out_nid[i])
5340 snd_hda_codec_cleanup_stream(codec,
5341 mout->hp_out_nid[i]);
5342 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
5343 if (mout->extra_out_nid[i])
5344 snd_hda_codec_cleanup_stream(codec,
5345 mout->extra_out_nid[i]);
5346 mutex_lock(&codec->spdif_mutex);
5347 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
5348 cleanup_dig_out_stream(codec, mout->dig_out_nid);
5349 mout->dig_out_used = 0;
5351 mutex_unlock(&codec->spdif_mutex);
5354 EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_cleanup);
5357 * snd_hda_get_default_vref - Get the default (mic) VREF pin bits
5358 * @codec: the HDA codec
5359 * @pin: referred pin NID
5361 * Guess the suitable VREF pin bits to be set as the pin-control value.
5362 * Note: the function doesn't set the AC_PINCTL_IN_EN bit.
5364 unsigned int snd_hda_get_default_vref(struct hda_codec *codec, hda_nid_t pin)
5366 unsigned int pincap;
5367 unsigned int oldval;
5368 oldval = snd_hda_codec_read(codec, pin, 0,
5369 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
5370 pincap = snd_hda_query_pin_caps(codec, pin);
5371 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
5372 /* Exception: if the default pin setup is vref50, we give it priority */
5373 if ((pincap & AC_PINCAP_VREF_80) && oldval != PIN_VREF50)
5374 return AC_PINCTL_VREF_80;
5375 else if (pincap & AC_PINCAP_VREF_50)
5376 return AC_PINCTL_VREF_50;
5377 else if (pincap & AC_PINCAP_VREF_100)
5378 return AC_PINCTL_VREF_100;
5379 else if (pincap & AC_PINCAP_VREF_GRD)
5380 return AC_PINCTL_VREF_GRD;
5381 return AC_PINCTL_VREF_HIZ;
5383 EXPORT_SYMBOL_GPL(snd_hda_get_default_vref);
5386 * snd_hda_correct_pin_ctl - correct the pin ctl value for matching with the pin cap
5387 * @codec: the HDA codec
5388 * @pin: referred pin NID
5389 * @val: pin ctl value to audit
5391 unsigned int snd_hda_correct_pin_ctl(struct hda_codec *codec,
5392 hda_nid_t pin, unsigned int val)
5394 static unsigned int cap_lists[][2] = {
5395 { AC_PINCTL_VREF_100, AC_PINCAP_VREF_100 },
5396 { AC_PINCTL_VREF_80, AC_PINCAP_VREF_80 },
5397 { AC_PINCTL_VREF_50, AC_PINCAP_VREF_50 },
5398 { AC_PINCTL_VREF_GRD, AC_PINCAP_VREF_GRD },
5404 cap = snd_hda_query_pin_caps(codec, pin);
5406 return val; /* don't know what to do... */
5408 if (val & AC_PINCTL_OUT_EN) {
5409 if (!(cap & AC_PINCAP_OUT))
5410 val &= ~(AC_PINCTL_OUT_EN | AC_PINCTL_HP_EN);
5411 else if ((val & AC_PINCTL_HP_EN) && !(cap & AC_PINCAP_HP_DRV))
5412 val &= ~AC_PINCTL_HP_EN;
5415 if (val & AC_PINCTL_IN_EN) {
5416 if (!(cap & AC_PINCAP_IN))
5417 val &= ~(AC_PINCTL_IN_EN | AC_PINCTL_VREFEN);
5419 unsigned int vcap, vref;
5421 vcap = (cap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
5422 vref = val & AC_PINCTL_VREFEN;
5423 for (i = 0; i < ARRAY_SIZE(cap_lists); i++) {
5424 if (vref == cap_lists[i][0] &&
5425 !(vcap & cap_lists[i][1])) {
5426 if (i == ARRAY_SIZE(cap_lists) - 1)
5427 vref = AC_PINCTL_VREF_HIZ;
5429 vref = cap_lists[i + 1][0];
5432 val &= ~AC_PINCTL_VREFEN;
5439 EXPORT_SYMBOL_GPL(snd_hda_correct_pin_ctl);
5442 * _snd_hda_pin_ctl - Helper to set pin ctl value
5443 * @codec: the HDA codec
5444 * @pin: referred pin NID
5445 * @val: pin control value to set
5446 * @cached: access over codec pinctl cache or direct write
5448 * This function is a helper to set a pin ctl value more safely.
5449 * It corrects the pin ctl value via snd_hda_correct_pin_ctl(), stores the
5450 * value in pin target array via snd_hda_codec_set_pin_target(), then
5451 * actually writes the value via either snd_hda_codec_update_cache() or
5452 * snd_hda_codec_write() depending on @cached flag.
5454 int _snd_hda_set_pin_ctl(struct hda_codec *codec, hda_nid_t pin,
5455 unsigned int val, bool cached)
5457 val = snd_hda_correct_pin_ctl(codec, pin, val);
5458 snd_hda_codec_set_pin_target(codec, pin, val);
5460 return snd_hda_codec_update_cache(codec, pin, 0,
5461 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
5463 return snd_hda_codec_write(codec, pin, 0,
5464 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
5466 EXPORT_SYMBOL_GPL(_snd_hda_set_pin_ctl);
5469 * snd_hda_add_imux_item - Add an item to input_mux
5470 * @codec: the HDA codec
5471 * @imux: imux helper object
5472 * @label: the name of imux item to assign
5473 * @index: index number of imux item to assign
5474 * @type_idx: pointer to store the resultant label index
5476 * When the same label is used already in the existing items, the number
5477 * suffix is appended to the label. This label index number is stored
5478 * to type_idx when non-NULL pointer is given.
5480 int snd_hda_add_imux_item(struct hda_codec *codec,
5481 struct hda_input_mux *imux, const char *label,
5482 int index, int *type_idx)
5484 int i, label_idx = 0;
5485 if (imux->num_items >= HDA_MAX_NUM_INPUTS) {
5486 codec_err(codec, "hda_codec: Too many imux items!\n");
5489 for (i = 0; i < imux->num_items; i++) {
5490 if (!strncmp(label, imux->items[i].label, strlen(label)))
5494 *type_idx = label_idx;
5496 snprintf(imux->items[imux->num_items].label,
5497 sizeof(imux->items[imux->num_items].label),
5498 "%s %d", label, label_idx);
5500 strlcpy(imux->items[imux->num_items].label, label,
5501 sizeof(imux->items[imux->num_items].label));
5502 imux->items[imux->num_items].index = index;
5506 EXPORT_SYMBOL_GPL(snd_hda_add_imux_item);
5515 static void hda_async_suspend(void *data, async_cookie_t cookie)
5517 hda_call_codec_suspend(data, false);
5520 static void hda_async_resume(void *data, async_cookie_t cookie)
5522 hda_call_codec_resume(data);
5526 * snd_hda_suspend - suspend the codecs
5529 * Returns 0 if successful.
5531 int snd_hda_suspend(struct hda_bus *bus)
5533 struct hda_codec *codec;
5534 ASYNC_DOMAIN_EXCLUSIVE(domain);
5536 list_for_each_entry(codec, &bus->codec_list, list) {
5537 cancel_delayed_work_sync(&codec->jackpoll_work);
5538 if (hda_codec_is_power_on(codec)) {
5539 if (bus->num_codecs > 1)
5540 async_schedule_domain(hda_async_suspend, codec,
5543 hda_call_codec_suspend(codec, false);
5547 if (bus->num_codecs > 1)
5548 async_synchronize_full_domain(&domain);
5552 EXPORT_SYMBOL_GPL(snd_hda_suspend);
5555 * snd_hda_resume - resume the codecs
5558 * Returns 0 if successful.
5560 int snd_hda_resume(struct hda_bus *bus)
5562 struct hda_codec *codec;
5563 ASYNC_DOMAIN_EXCLUSIVE(domain);
5565 list_for_each_entry(codec, &bus->codec_list, list) {
5566 if (bus->num_codecs > 1)
5567 async_schedule_domain(hda_async_resume, codec, &domain);
5569 hda_call_codec_resume(codec);
5572 if (bus->num_codecs > 1)
5573 async_synchronize_full_domain(&domain);
5577 EXPORT_SYMBOL_GPL(snd_hda_resume);
5578 #endif /* CONFIG_PM */
5585 * snd_array_new - get a new element from the given array
5586 * @array: the array object
5588 * Get a new element from the given array. If it exceeds the
5589 * pre-allocated array size, re-allocate the array.
5591 * Returns NULL if allocation failed.
5593 void *snd_array_new(struct snd_array *array)
5595 if (snd_BUG_ON(!array->elem_size))
5597 if (array->used >= array->alloced) {
5598 int num = array->alloced + array->alloc_align;
5599 int size = (num + 1) * array->elem_size;
5601 if (snd_BUG_ON(num >= 4096))
5603 nlist = krealloc(array->list, size, GFP_KERNEL | __GFP_ZERO);
5606 array->list = nlist;
5607 array->alloced = num;
5609 return snd_array_elem(array, array->used++);
5611 EXPORT_SYMBOL_GPL(snd_array_new);
5614 * snd_array_free - free the given array elements
5615 * @array: the array object
5617 void snd_array_free(struct snd_array *array)
5624 EXPORT_SYMBOL_GPL(snd_array_free);
5627 * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer
5628 * @pcm: PCM caps bits
5629 * @buf: the string buffer to write
5630 * @buflen: the max buffer length
5632 * used by hda_proc.c and hda_eld.c
5634 void snd_print_pcm_bits(int pcm, char *buf, int buflen)
5636 static unsigned int bits[] = { 8, 16, 20, 24, 32 };
5639 for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
5640 if (pcm & (AC_SUPPCM_BITS_8 << i))
5641 j += snprintf(buf + j, buflen - j, " %d", bits[i]);
5643 buf[j] = '\0'; /* necessary when j == 0 */
5645 EXPORT_SYMBOL_GPL(snd_print_pcm_bits);
5647 MODULE_DESCRIPTION("HDA codec core");
5648 MODULE_LICENSE("GPL");