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/pci.h>
27 #include <linux/mutex.h>
28 #include <linux/module.h>
29 #include <sound/core.h>
30 #include "hda_codec.h"
31 #include <sound/asoundef.h>
32 #include <sound/tlv.h>
33 #include <sound/initval.h>
34 #include <sound/jack.h>
35 #include "hda_local.h"
38 #include <sound/hda_hwdep.h>
40 #define CREATE_TRACE_POINTS
41 #include "hda_trace.h"
44 * vendor / preset table
47 struct hda_vendor_id {
52 /* codec vendor labels */
53 static struct hda_vendor_id hda_vendor_ids[] = {
55 { 0x1013, "Cirrus Logic" },
56 { 0x1057, "Motorola" },
57 { 0x1095, "Silicon Image" },
59 { 0x10ec, "Realtek" },
60 { 0x1102, "Creative" },
64 { 0x11d4, "Analog Devices" },
65 { 0x13f6, "C-Media" },
66 { 0x14f1, "Conexant" },
67 { 0x17e8, "Chrontel" },
69 { 0x1aec, "Wolfson Microelectronics" },
70 { 0x434d, "C-Media" },
72 { 0x8384, "SigmaTel" },
76 static DEFINE_MUTEX(preset_mutex);
77 static LIST_HEAD(hda_preset_tables);
79 int snd_hda_add_codec_preset(struct hda_codec_preset_list *preset)
81 mutex_lock(&preset_mutex);
82 list_add_tail(&preset->list, &hda_preset_tables);
83 mutex_unlock(&preset_mutex);
86 EXPORT_SYMBOL_HDA(snd_hda_add_codec_preset);
88 int snd_hda_delete_codec_preset(struct hda_codec_preset_list *preset)
90 mutex_lock(&preset_mutex);
91 list_del(&preset->list);
92 mutex_unlock(&preset_mutex);
95 EXPORT_SYMBOL_HDA(snd_hda_delete_codec_preset);
98 #define codec_in_pm(codec) ((codec)->in_pm)
99 static void hda_power_work(struct work_struct *work);
100 static void hda_keep_power_on(struct hda_codec *codec);
101 #define hda_codec_is_power_on(codec) ((codec)->power_on)
102 static inline void hda_call_pm_notify(struct hda_bus *bus, bool power_up)
104 if (bus->ops.pm_notify)
105 bus->ops.pm_notify(bus, power_up);
108 #define codec_in_pm(codec) 0
109 static inline void hda_keep_power_on(struct hda_codec *codec) {}
110 #define hda_codec_is_power_on(codec) 1
111 #define hda_call_pm_notify(bus, state) {}
115 * snd_hda_get_jack_location - Give a location string of the jack
116 * @cfg: pin default config value
118 * Parse the pin default config value and returns the string of the
119 * jack location, e.g. "Rear", "Front", etc.
121 const char *snd_hda_get_jack_location(u32 cfg)
123 static char *bases[7] = {
124 "N/A", "Rear", "Front", "Left", "Right", "Top", "Bottom",
126 static unsigned char specials_idx[] = {
131 static char *specials[] = {
132 "Rear Panel", "Drive Bar",
133 "Riser", "HDMI", "ATAPI",
134 "Mobile-In", "Mobile-Out"
137 cfg = (cfg & AC_DEFCFG_LOCATION) >> AC_DEFCFG_LOCATION_SHIFT;
138 if ((cfg & 0x0f) < 7)
139 return bases[cfg & 0x0f];
140 for (i = 0; i < ARRAY_SIZE(specials_idx); i++) {
141 if (cfg == specials_idx[i])
146 EXPORT_SYMBOL_HDA(snd_hda_get_jack_location);
149 * snd_hda_get_jack_connectivity - Give a connectivity string of the jack
150 * @cfg: pin default config value
152 * Parse the pin default config value and returns the string of the
153 * jack connectivity, i.e. external or internal connection.
155 const char *snd_hda_get_jack_connectivity(u32 cfg)
157 static char *jack_locations[4] = { "Ext", "Int", "Sep", "Oth" };
159 return jack_locations[(cfg >> (AC_DEFCFG_LOCATION_SHIFT + 4)) & 3];
161 EXPORT_SYMBOL_HDA(snd_hda_get_jack_connectivity);
164 * snd_hda_get_jack_type - Give a type string of the jack
165 * @cfg: pin default config value
167 * Parse the pin default config value and returns the string of the
168 * jack type, i.e. the purpose of the jack, such as Line-Out or CD.
170 const char *snd_hda_get_jack_type(u32 cfg)
172 static char *jack_types[16] = {
173 "Line Out", "Speaker", "HP Out", "CD",
174 "SPDIF Out", "Digital Out", "Modem Line", "Modem Hand",
175 "Line In", "Aux", "Mic", "Telephony",
176 "SPDIF In", "Digital In", "Reserved", "Other"
179 return jack_types[(cfg & AC_DEFCFG_DEVICE)
180 >> AC_DEFCFG_DEVICE_SHIFT];
182 EXPORT_SYMBOL_HDA(snd_hda_get_jack_type);
185 * Compose a 32bit command word to be sent to the HD-audio controller
187 static inline unsigned int
188 make_codec_cmd(struct hda_codec *codec, hda_nid_t nid, int flags,
189 unsigned int verb, unsigned int parm)
193 if ((codec->addr & ~0xf) || (nid & ~0x7f) ||
194 (verb & ~0xfff) || (parm & ~0xffff)) {
195 printk(KERN_ERR "hda-codec: out of range cmd %x:%x:%x:%x\n",
196 codec->addr, nid, verb, parm);
200 val = (u32)codec->addr << 28;
201 val |= (u32)nid << 20;
208 * Send and receive a verb
210 static int codec_exec_verb(struct hda_codec *codec, unsigned int cmd,
211 int flags, unsigned int *res)
213 struct hda_bus *bus = codec->bus;
222 snd_hda_power_up(codec);
223 mutex_lock(&bus->cmd_mutex);
224 if (flags & HDA_RW_NO_RESPONSE_FALLBACK)
225 bus->no_response_fallback = 1;
227 trace_hda_send_cmd(codec, cmd);
228 err = bus->ops.command(bus, cmd);
231 /* process pending verbs */
232 bus->ops.get_response(bus, codec->addr);
235 *res = bus->ops.get_response(bus, codec->addr);
236 trace_hda_get_response(codec, *res);
238 bus->no_response_fallback = 0;
239 mutex_unlock(&bus->cmd_mutex);
240 snd_hda_power_down(codec);
241 if (!codec_in_pm(codec) && res && *res == -1 && bus->rirb_error) {
242 if (bus->response_reset) {
243 snd_printd("hda_codec: resetting BUS due to "
244 "fatal communication error\n");
245 trace_hda_bus_reset(bus);
246 bus->ops.bus_reset(bus);
250 /* clear reset-flag when the communication gets recovered */
251 if (!err || codec_in_pm(codec))
252 bus->response_reset = 0;
257 * snd_hda_codec_read - send a command and get the response
258 * @codec: the HDA codec
259 * @nid: NID to send the command
260 * @flags: optional bit flags
261 * @verb: the verb to send
262 * @parm: the parameter for the verb
264 * Send a single command and read the corresponding response.
266 * Returns the obtained response value, or -1 for an error.
268 unsigned int snd_hda_codec_read(struct hda_codec *codec, hda_nid_t nid,
270 unsigned int verb, unsigned int parm)
272 unsigned cmd = make_codec_cmd(codec, nid, flags, verb, parm);
274 if (codec_exec_verb(codec, cmd, flags, &res))
278 EXPORT_SYMBOL_HDA(snd_hda_codec_read);
281 * snd_hda_codec_write - send a single command without waiting for response
282 * @codec: the HDA codec
283 * @nid: NID to send the command
284 * @flags: optional bit flags
285 * @verb: the verb to send
286 * @parm: the parameter for the verb
288 * Send a single command without waiting for response.
290 * Returns 0 if successful, or a negative error code.
292 int snd_hda_codec_write(struct hda_codec *codec, hda_nid_t nid, int flags,
293 unsigned int verb, unsigned int parm)
295 unsigned int cmd = make_codec_cmd(codec, nid, flags, verb, parm);
297 return codec_exec_verb(codec, cmd, flags,
298 codec->bus->sync_write ? &res : NULL);
300 EXPORT_SYMBOL_HDA(snd_hda_codec_write);
303 * snd_hda_sequence_write - sequence writes
304 * @codec: the HDA codec
305 * @seq: VERB array to send
307 * Send the commands sequentially from the given array.
308 * The array must be terminated with NID=0.
310 void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq)
312 for (; seq->nid; seq++)
313 snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param);
315 EXPORT_SYMBOL_HDA(snd_hda_sequence_write);
318 * snd_hda_get_sub_nodes - get the range of sub nodes
319 * @codec: the HDA codec
321 * @start_id: the pointer to store the start NID
323 * Parse the NID and store the start NID of its sub-nodes.
324 * Returns the number of sub-nodes.
326 int snd_hda_get_sub_nodes(struct hda_codec *codec, hda_nid_t nid,
331 parm = snd_hda_param_read(codec, nid, AC_PAR_NODE_COUNT);
334 *start_id = (parm >> 16) & 0x7fff;
335 return (int)(parm & 0x7fff);
337 EXPORT_SYMBOL_HDA(snd_hda_get_sub_nodes);
339 /* connection list element */
340 struct hda_conn_list {
341 struct list_head list;
347 /* look up the cached results */
348 static struct hda_conn_list *
349 lookup_conn_list(struct hda_codec *codec, hda_nid_t nid)
351 struct hda_conn_list *p;
352 list_for_each_entry(p, &codec->conn_list, list) {
359 static int add_conn_list(struct hda_codec *codec, hda_nid_t nid, int len,
360 const hda_nid_t *list)
362 struct hda_conn_list *p;
364 p = kmalloc(sizeof(*p) + len * sizeof(hda_nid_t), GFP_KERNEL);
369 memcpy(p->conns, list, len * sizeof(hda_nid_t));
370 list_add(&p->list, &codec->conn_list);
374 static void remove_conn_list(struct hda_codec *codec)
376 while (!list_empty(&codec->conn_list)) {
377 struct hda_conn_list *p;
378 p = list_first_entry(&codec->conn_list, typeof(*p), list);
384 /* read the connection and add to the cache */
385 static int read_and_add_raw_conns(struct hda_codec *codec, hda_nid_t nid)
388 hda_nid_t *result = list;
391 len = snd_hda_get_raw_connections(codec, nid, list, ARRAY_SIZE(list));
392 if (len == -ENOSPC) {
393 len = snd_hda_get_num_raw_conns(codec, nid);
394 result = kmalloc(sizeof(hda_nid_t) * len, GFP_KERNEL);
397 len = snd_hda_get_raw_connections(codec, nid, result, len);
400 len = snd_hda_override_conn_list(codec, nid, len, result);
407 * snd_hda_get_conn_list - get connection list
408 * @codec: the HDA codec
410 * @len: number of connection list entries
411 * @listp: the pointer to store NID list
413 * Parses the connection list of the given widget and stores the pointer
414 * to the list of NIDs.
416 * Returns the number of connections, or a negative error code.
418 * Note that the returned pointer isn't protected against the list
419 * modification. If snd_hda_override_conn_list() might be called
420 * concurrently, protect with a mutex appropriately.
422 int snd_hda_get_conn_list(struct hda_codec *codec, hda_nid_t nid,
423 const hda_nid_t **listp)
429 const struct hda_conn_list *p;
431 /* if the connection-list is already cached, read it */
432 p = lookup_conn_list(codec, nid);
438 if (snd_BUG_ON(added))
441 err = read_and_add_raw_conns(codec, nid);
447 EXPORT_SYMBOL_HDA(snd_hda_get_conn_list);
450 * snd_hda_get_connections - copy connection list
451 * @codec: the HDA codec
453 * @conn_list: connection list array; when NULL, checks only the size
454 * @max_conns: max. number of connections to store
456 * Parses the connection list of the given widget and stores the list
459 * Returns the number of connections, or a negative error code.
461 int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
462 hda_nid_t *conn_list, int max_conns)
464 const hda_nid_t *list;
465 int len = snd_hda_get_conn_list(codec, nid, &list);
467 if (len > 0 && conn_list) {
468 if (len > max_conns) {
469 snd_printk(KERN_ERR "hda_codec: "
470 "Too many connections %d for NID 0x%x\n",
474 memcpy(conn_list, list, len * sizeof(hda_nid_t));
479 EXPORT_SYMBOL_HDA(snd_hda_get_connections);
481 /* return CONNLIST_LEN parameter of the given widget */
482 static unsigned int get_num_conns(struct hda_codec *codec, hda_nid_t nid)
484 unsigned int wcaps = get_wcaps(codec, nid);
487 if (!(wcaps & AC_WCAP_CONN_LIST) &&
488 get_wcaps_type(wcaps) != AC_WID_VOL_KNB)
491 parm = snd_hda_param_read(codec, nid, AC_PAR_CONNLIST_LEN);
497 int snd_hda_get_num_raw_conns(struct hda_codec *codec, hda_nid_t nid)
499 return snd_hda_get_raw_connections(codec, nid, NULL, 0);
503 * snd_hda_get_raw_connections - copy connection list without cache
504 * @codec: the HDA codec
506 * @conn_list: connection list array
507 * @max_conns: max. number of connections to store
509 * Like snd_hda_get_connections(), copy the connection list but without
510 * checking through the connection-list cache.
511 * Currently called only from hda_proc.c, so not exported.
513 int snd_hda_get_raw_connections(struct hda_codec *codec, hda_nid_t nid,
514 hda_nid_t *conn_list, int max_conns)
517 int i, conn_len, conns;
518 unsigned int shift, num_elems, mask;
522 parm = get_num_conns(codec, nid);
526 if (parm & AC_CLIST_LONG) {
535 conn_len = parm & AC_CLIST_LENGTH;
536 mask = (1 << (shift-1)) - 1;
539 return 0; /* no connection */
542 /* single connection */
543 parm = snd_hda_codec_read(codec, nid, 0,
544 AC_VERB_GET_CONNECT_LIST, 0);
545 if (parm == -1 && codec->bus->rirb_error)
548 conn_list[0] = parm & mask;
552 /* multi connection */
555 for (i = 0; i < conn_len; i++) {
559 if (i % num_elems == 0) {
560 parm = snd_hda_codec_read(codec, nid, 0,
561 AC_VERB_GET_CONNECT_LIST, i);
562 if (parm == -1 && codec->bus->rirb_error)
565 range_val = !!(parm & (1 << (shift-1))); /* ranges */
567 if (val == 0 && null_count++) { /* no second chance */
568 snd_printk(KERN_WARNING "hda_codec: "
569 "invalid CONNECT_LIST verb %x[%i]:%x\n",
575 /* ranges between the previous and this one */
576 if (!prev_nid || prev_nid >= val) {
577 snd_printk(KERN_WARNING "hda_codec: "
578 "invalid dep_range_val %x:%x\n",
582 for (n = prev_nid + 1; n <= val; n++) {
584 if (conns >= max_conns)
586 conn_list[conns] = n;
592 if (conns >= max_conns)
594 conn_list[conns] = val;
604 * snd_hda_override_conn_list - add/modify the connection-list to cache
605 * @codec: the HDA codec
607 * @len: number of connection list entries
608 * @list: the list of connection entries
610 * Add or modify the given connection-list to the cache. If the corresponding
611 * cache already exists, invalidate it and append a new one.
613 * Returns zero or a negative error code.
615 int snd_hda_override_conn_list(struct hda_codec *codec, hda_nid_t nid, int len,
616 const hda_nid_t *list)
618 struct hda_conn_list *p;
620 p = lookup_conn_list(codec, nid);
626 return add_conn_list(codec, nid, len, list);
628 EXPORT_SYMBOL_HDA(snd_hda_override_conn_list);
631 * snd_hda_get_conn_index - get the connection index of the given NID
632 * @codec: the HDA codec
633 * @mux: NID containing the list
634 * @nid: NID to select
635 * @recursive: 1 when searching NID recursively, otherwise 0
637 * Parses the connection list of the widget @mux and checks whether the
638 * widget @nid is present. If it is, return the connection index.
639 * Otherwise it returns -1.
641 int snd_hda_get_conn_index(struct hda_codec *codec, hda_nid_t mux,
642 hda_nid_t nid, int recursive)
644 const hda_nid_t *conn;
647 nums = snd_hda_get_conn_list(codec, mux, &conn);
648 for (i = 0; i < nums; i++)
653 if (recursive > 10) {
654 snd_printd("hda_codec: too deep connection for 0x%x\n", nid);
658 for (i = 0; i < nums; i++) {
659 unsigned int type = get_wcaps_type(get_wcaps(codec, conn[i]));
660 if (type == AC_WID_PIN || type == AC_WID_AUD_OUT)
662 if (snd_hda_get_conn_index(codec, conn[i], nid, recursive) >= 0)
667 EXPORT_SYMBOL_HDA(snd_hda_get_conn_index);
670 /* return DEVLIST_LEN parameter of the given widget */
671 static unsigned int get_num_devices(struct hda_codec *codec, hda_nid_t nid)
673 unsigned int wcaps = get_wcaps(codec, nid);
676 if (!codec->dp_mst || !(wcaps & AC_WCAP_DIGITAL) ||
677 get_wcaps_type(wcaps) != AC_WID_PIN)
680 parm = snd_hda_param_read(codec, nid, AC_PAR_DEVLIST_LEN);
681 if (parm == -1 && codec->bus->rirb_error)
683 return parm & AC_DEV_LIST_LEN_MASK;
687 * snd_hda_get_devices - copy device list without cache
688 * @codec: the HDA codec
689 * @nid: NID of the pin to parse
690 * @dev_list: device list array
691 * @max_devices: max. number of devices to store
693 * Copy the device list. This info is dynamic and so not cached.
694 * Currently called only from hda_proc.c, so not exported.
696 int snd_hda_get_devices(struct hda_codec *codec, hda_nid_t nid,
697 u8 *dev_list, int max_devices)
700 int i, dev_len, devices;
702 parm = get_num_devices(codec, nid);
703 if (!parm) /* not multi-stream capable */
707 dev_len = dev_len < max_devices ? dev_len : max_devices;
710 while (devices < dev_len) {
711 parm = snd_hda_codec_read(codec, nid, 0,
712 AC_VERB_GET_DEVICE_LIST, devices);
713 if (parm == -1 && codec->bus->rirb_error)
716 for (i = 0; i < 8; i++) {
717 dev_list[devices] = (u8)parm;
720 if (devices >= dev_len)
728 * snd_hda_queue_unsol_event - add an unsolicited event to queue
730 * @res: unsolicited event (lower 32bit of RIRB entry)
731 * @res_ex: codec addr and flags (upper 32bit or RIRB entry)
733 * Adds the given event to the queue. The events are processed in
734 * the workqueue asynchronously. Call this function in the interrupt
735 * hanlder when RIRB receives an unsolicited event.
737 * Returns 0 if successful, or a negative error code.
739 int snd_hda_queue_unsol_event(struct hda_bus *bus, u32 res, u32 res_ex)
741 struct hda_bus_unsolicited *unsol;
744 if (!bus || !bus->workq)
747 trace_hda_unsol_event(bus, res, res_ex);
752 wp = (unsol->wp + 1) % HDA_UNSOL_QUEUE_SIZE;
756 unsol->queue[wp] = res;
757 unsol->queue[wp + 1] = res_ex;
759 queue_work(bus->workq, &unsol->work);
763 EXPORT_SYMBOL_HDA(snd_hda_queue_unsol_event);
766 * process queued unsolicited events
768 static void process_unsol_events(struct work_struct *work)
770 struct hda_bus_unsolicited *unsol =
771 container_of(work, struct hda_bus_unsolicited, work);
772 struct hda_bus *bus = unsol->bus;
773 struct hda_codec *codec;
774 unsigned int rp, caddr, res;
776 while (unsol->rp != unsol->wp) {
777 rp = (unsol->rp + 1) % HDA_UNSOL_QUEUE_SIZE;
780 res = unsol->queue[rp];
781 caddr = unsol->queue[rp + 1];
782 if (!(caddr & (1 << 4))) /* no unsolicited event? */
784 codec = bus->caddr_tbl[caddr & 0x0f];
785 if (codec && codec->patch_ops.unsol_event)
786 codec->patch_ops.unsol_event(codec, res);
791 * initialize unsolicited queue
793 static int init_unsol_queue(struct hda_bus *bus)
795 struct hda_bus_unsolicited *unsol;
797 if (bus->unsol) /* already initialized */
800 unsol = kzalloc(sizeof(*unsol), GFP_KERNEL);
802 snd_printk(KERN_ERR "hda_codec: "
803 "can't allocate unsolicited queue\n");
806 INIT_WORK(&unsol->work, process_unsol_events);
815 static void snd_hda_codec_free(struct hda_codec *codec);
817 static int snd_hda_bus_free(struct hda_bus *bus)
819 struct hda_codec *codec, *n;
824 flush_workqueue(bus->workq);
827 list_for_each_entry_safe(codec, n, &bus->codec_list, list) {
828 snd_hda_codec_free(codec);
830 if (bus->ops.private_free)
831 bus->ops.private_free(bus);
833 destroy_workqueue(bus->workq);
838 static int snd_hda_bus_dev_free(struct snd_device *device)
840 struct hda_bus *bus = device->device_data;
842 return snd_hda_bus_free(bus);
845 #ifdef CONFIG_SND_HDA_HWDEP
846 static int snd_hda_bus_dev_register(struct snd_device *device)
848 struct hda_bus *bus = device->device_data;
849 struct hda_codec *codec;
850 list_for_each_entry(codec, &bus->codec_list, list) {
851 snd_hda_hwdep_add_sysfs(codec);
852 snd_hda_hwdep_add_power_sysfs(codec);
857 #define snd_hda_bus_dev_register NULL
861 * snd_hda_bus_new - create a HDA bus
862 * @card: the card entry
863 * @temp: the template for hda_bus information
864 * @busp: the pointer to store the created bus instance
866 * Returns 0 if successful, or a negative error code.
868 int snd_hda_bus_new(struct snd_card *card,
869 const struct hda_bus_template *temp,
870 struct hda_bus **busp)
874 static struct snd_device_ops dev_ops = {
875 .dev_register = snd_hda_bus_dev_register,
876 .dev_free = snd_hda_bus_dev_free,
879 if (snd_BUG_ON(!temp))
881 if (snd_BUG_ON(!temp->ops.command || !temp->ops.get_response))
887 bus = kzalloc(sizeof(*bus), GFP_KERNEL);
889 snd_printk(KERN_ERR "can't allocate struct hda_bus\n");
894 bus->private_data = temp->private_data;
895 bus->pci = temp->pci;
896 bus->modelname = temp->modelname;
897 bus->power_save = temp->power_save;
898 bus->ops = temp->ops;
900 mutex_init(&bus->cmd_mutex);
901 mutex_init(&bus->prepare_mutex);
902 INIT_LIST_HEAD(&bus->codec_list);
904 snprintf(bus->workq_name, sizeof(bus->workq_name),
905 "hd-audio%d", card->number);
906 bus->workq = create_singlethread_workqueue(bus->workq_name);
908 snd_printk(KERN_ERR "cannot create workqueue %s\n",
914 err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops);
916 snd_hda_bus_free(bus);
923 EXPORT_SYMBOL_HDA(snd_hda_bus_new);
925 #ifdef CONFIG_SND_HDA_GENERIC
926 #define is_generic_config(codec) \
927 (codec->modelname && !strcmp(codec->modelname, "generic"))
929 #define is_generic_config(codec) 0
933 #define HDA_MODREQ_MAX_COUNT 2 /* two request_modules()'s */
935 #define HDA_MODREQ_MAX_COUNT 0 /* all presets are statically linked */
939 * find a matching codec preset
941 static const struct hda_codec_preset *
942 find_codec_preset(struct hda_codec *codec)
944 struct hda_codec_preset_list *tbl;
945 const struct hda_codec_preset *preset;
946 unsigned int mod_requested = 0;
948 if (is_generic_config(codec))
949 return NULL; /* use the generic parser */
952 mutex_lock(&preset_mutex);
953 list_for_each_entry(tbl, &hda_preset_tables, list) {
954 if (!try_module_get(tbl->owner)) {
955 snd_printk(KERN_ERR "hda_codec: cannot module_get\n");
958 for (preset = tbl->preset; preset->id; preset++) {
959 u32 mask = preset->mask;
960 if (preset->afg && preset->afg != codec->afg)
962 if (preset->mfg && preset->mfg != codec->mfg)
966 if (preset->id == (codec->vendor_id & mask) &&
968 preset->rev == codec->revision_id)) {
969 mutex_unlock(&preset_mutex);
970 codec->owner = tbl->owner;
974 module_put(tbl->owner);
976 mutex_unlock(&preset_mutex);
978 if (mod_requested < HDA_MODREQ_MAX_COUNT) {
981 snprintf(name, sizeof(name), "snd-hda-codec-id:%08x",
984 snprintf(name, sizeof(name), "snd-hda-codec-id:%04x*",
985 (codec->vendor_id >> 16) & 0xffff);
986 request_module(name);
994 * get_codec_name - store the codec name
996 static int get_codec_name(struct hda_codec *codec)
998 const struct hda_vendor_id *c;
999 const char *vendor = NULL;
1000 u16 vendor_id = codec->vendor_id >> 16;
1003 if (codec->vendor_name)
1006 for (c = hda_vendor_ids; c->id; c++) {
1007 if (c->id == vendor_id) {
1013 sprintf(tmp, "Generic %04x", vendor_id);
1016 codec->vendor_name = kstrdup(vendor, GFP_KERNEL);
1017 if (!codec->vendor_name)
1021 if (codec->chip_name)
1024 if (codec->preset && codec->preset->name)
1025 codec->chip_name = kstrdup(codec->preset->name, GFP_KERNEL);
1027 sprintf(tmp, "ID %x", codec->vendor_id & 0xffff);
1028 codec->chip_name = kstrdup(tmp, GFP_KERNEL);
1030 if (!codec->chip_name)
1036 * look for an AFG and MFG nodes
1038 static void setup_fg_nodes(struct hda_codec *codec)
1040 int i, total_nodes, function_id;
1043 total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid);
1044 for (i = 0; i < total_nodes; i++, nid++) {
1045 function_id = snd_hda_param_read(codec, nid,
1046 AC_PAR_FUNCTION_TYPE);
1047 switch (function_id & 0xff) {
1048 case AC_GRP_AUDIO_FUNCTION:
1050 codec->afg_function_id = function_id & 0xff;
1051 codec->afg_unsol = (function_id >> 8) & 1;
1053 case AC_GRP_MODEM_FUNCTION:
1055 codec->mfg_function_id = function_id & 0xff;
1056 codec->mfg_unsol = (function_id >> 8) & 1;
1065 * read widget caps for each widget and store in cache
1067 static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
1072 codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node,
1074 codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL);
1077 nid = codec->start_nid;
1078 for (i = 0; i < codec->num_nodes; i++, nid++)
1079 codec->wcaps[i] = snd_hda_param_read(codec, nid,
1080 AC_PAR_AUDIO_WIDGET_CAP);
1084 /* read all pin default configurations and save codec->init_pins */
1085 static int read_pin_defaults(struct hda_codec *codec)
1088 hda_nid_t nid = codec->start_nid;
1090 for (i = 0; i < codec->num_nodes; i++, nid++) {
1091 struct hda_pincfg *pin;
1092 unsigned int wcaps = get_wcaps(codec, nid);
1093 unsigned int wid_type = get_wcaps_type(wcaps);
1094 if (wid_type != AC_WID_PIN)
1096 pin = snd_array_new(&codec->init_pins);
1100 pin->cfg = snd_hda_codec_read(codec, nid, 0,
1101 AC_VERB_GET_CONFIG_DEFAULT, 0);
1102 pin->ctrl = snd_hda_codec_read(codec, nid, 0,
1103 AC_VERB_GET_PIN_WIDGET_CONTROL,
1109 /* look up the given pin config list and return the item matching with NID */
1110 static struct hda_pincfg *look_up_pincfg(struct hda_codec *codec,
1111 struct snd_array *array,
1115 for (i = 0; i < array->used; i++) {
1116 struct hda_pincfg *pin = snd_array_elem(array, i);
1117 if (pin->nid == nid)
1123 /* set the current pin config value for the given NID.
1124 * the value is cached, and read via snd_hda_codec_get_pincfg()
1126 int snd_hda_add_pincfg(struct hda_codec *codec, struct snd_array *list,
1127 hda_nid_t nid, unsigned int cfg)
1129 struct hda_pincfg *pin;
1131 /* the check below may be invalid when pins are added by a fixup
1132 * dynamically (e.g. via snd_hda_codec_update_widgets()), so disabled
1136 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN)
1140 pin = look_up_pincfg(codec, list, nid);
1142 pin = snd_array_new(list);
1152 * snd_hda_codec_set_pincfg - Override a pin default configuration
1153 * @codec: the HDA codec
1154 * @nid: NID to set the pin config
1155 * @cfg: the pin default config value
1157 * Override a pin default configuration value in the cache.
1158 * This value can be read by snd_hda_codec_get_pincfg() in a higher
1159 * priority than the real hardware value.
1161 int snd_hda_codec_set_pincfg(struct hda_codec *codec,
1162 hda_nid_t nid, unsigned int cfg)
1164 return snd_hda_add_pincfg(codec, &codec->driver_pins, nid, cfg);
1166 EXPORT_SYMBOL_HDA(snd_hda_codec_set_pincfg);
1169 * snd_hda_codec_get_pincfg - Obtain a pin-default configuration
1170 * @codec: the HDA codec
1171 * @nid: NID to get the pin config
1173 * Get the current pin config value of the given pin NID.
1174 * If the pincfg value is cached or overridden via sysfs or driver,
1175 * returns the cached value.
1177 unsigned int snd_hda_codec_get_pincfg(struct hda_codec *codec, hda_nid_t nid)
1179 struct hda_pincfg *pin;
1181 #ifdef CONFIG_SND_HDA_HWDEP
1183 unsigned int cfg = 0;
1184 mutex_lock(&codec->user_mutex);
1185 pin = look_up_pincfg(codec, &codec->user_pins, nid);
1188 mutex_unlock(&codec->user_mutex);
1193 pin = look_up_pincfg(codec, &codec->driver_pins, nid);
1196 pin = look_up_pincfg(codec, &codec->init_pins, nid);
1201 EXPORT_SYMBOL_HDA(snd_hda_codec_get_pincfg);
1203 /* remember the current pinctl target value */
1204 int snd_hda_codec_set_pin_target(struct hda_codec *codec, hda_nid_t nid,
1207 struct hda_pincfg *pin;
1209 pin = look_up_pincfg(codec, &codec->init_pins, nid);
1215 EXPORT_SYMBOL_HDA(snd_hda_codec_set_pin_target);
1217 /* return the current pinctl target value */
1218 int snd_hda_codec_get_pin_target(struct hda_codec *codec, hda_nid_t nid)
1220 struct hda_pincfg *pin;
1222 pin = look_up_pincfg(codec, &codec->init_pins, nid);
1227 EXPORT_SYMBOL_HDA(snd_hda_codec_get_pin_target);
1230 * snd_hda_shutup_pins - Shut up all pins
1231 * @codec: the HDA codec
1233 * Clear all pin controls to shup up before suspend for avoiding click noise.
1234 * The controls aren't cached so that they can be resumed properly.
1236 void snd_hda_shutup_pins(struct hda_codec *codec)
1239 /* don't shut up pins when unloading the driver; otherwise it breaks
1240 * the default pin setup at the next load of the driver
1242 if (codec->bus->shutdown)
1244 for (i = 0; i < codec->init_pins.used; i++) {
1245 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1246 /* use read here for syncing after issuing each verb */
1247 snd_hda_codec_read(codec, pin->nid, 0,
1248 AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
1250 codec->pins_shutup = 1;
1252 EXPORT_SYMBOL_HDA(snd_hda_shutup_pins);
1255 /* Restore the pin controls cleared previously via snd_hda_shutup_pins() */
1256 static void restore_shutup_pins(struct hda_codec *codec)
1259 if (!codec->pins_shutup)
1261 if (codec->bus->shutdown)
1263 for (i = 0; i < codec->init_pins.used; i++) {
1264 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1265 snd_hda_codec_write(codec, pin->nid, 0,
1266 AC_VERB_SET_PIN_WIDGET_CONTROL,
1269 codec->pins_shutup = 0;
1273 static void hda_jackpoll_work(struct work_struct *work)
1275 struct hda_codec *codec =
1276 container_of(work, struct hda_codec, jackpoll_work.work);
1278 snd_hda_jack_set_dirty_all(codec);
1279 snd_hda_jack_poll_all(codec);
1281 if (!codec->jackpoll_interval)
1284 queue_delayed_work(codec->bus->workq, &codec->jackpoll_work,
1285 codec->jackpoll_interval);
1288 static void init_hda_cache(struct hda_cache_rec *cache,
1289 unsigned int record_size);
1290 static void free_hda_cache(struct hda_cache_rec *cache);
1292 /* release all pincfg lists */
1293 static void free_init_pincfgs(struct hda_codec *codec)
1295 snd_array_free(&codec->driver_pins);
1296 #ifdef CONFIG_SND_HDA_HWDEP
1297 snd_array_free(&codec->user_pins);
1299 snd_array_free(&codec->init_pins);
1303 * audio-converter setup caches
1305 struct hda_cvt_setup {
1310 unsigned char active; /* cvt is currently used */
1311 unsigned char dirty; /* setups should be cleared */
1314 /* get or create a cache entry for the given audio converter NID */
1315 static struct hda_cvt_setup *
1316 get_hda_cvt_setup(struct hda_codec *codec, hda_nid_t nid)
1318 struct hda_cvt_setup *p;
1321 for (i = 0; i < codec->cvt_setups.used; i++) {
1322 p = snd_array_elem(&codec->cvt_setups, i);
1326 p = snd_array_new(&codec->cvt_setups);
1335 static void snd_hda_codec_free(struct hda_codec *codec)
1339 cancel_delayed_work_sync(&codec->jackpoll_work);
1340 snd_hda_jack_tbl_clear(codec);
1341 free_init_pincfgs(codec);
1343 cancel_delayed_work(&codec->power_work);
1344 flush_workqueue(codec->bus->workq);
1346 list_del(&codec->list);
1347 snd_array_free(&codec->mixers);
1348 snd_array_free(&codec->nids);
1349 snd_array_free(&codec->cvt_setups);
1350 snd_array_free(&codec->spdif_out);
1351 remove_conn_list(codec);
1352 codec->bus->caddr_tbl[codec->addr] = NULL;
1353 if (codec->patch_ops.free)
1354 codec->patch_ops.free(codec);
1356 if (!codec->pm_down_notified) /* cancel leftover refcounts */
1357 hda_call_pm_notify(codec->bus, false);
1359 module_put(codec->owner);
1360 free_hda_cache(&codec->amp_cache);
1361 free_hda_cache(&codec->cmd_cache);
1362 kfree(codec->vendor_name);
1363 kfree(codec->chip_name);
1364 kfree(codec->modelname);
1365 kfree(codec->wcaps);
1369 static bool snd_hda_codec_get_supported_ps(struct hda_codec *codec,
1370 hda_nid_t fg, unsigned int power_state);
1372 static unsigned int hda_set_power_state(struct hda_codec *codec,
1373 unsigned int power_state);
1376 * snd_hda_codec_new - create a HDA codec
1377 * @bus: the bus to assign
1378 * @codec_addr: the codec address
1379 * @codecp: the pointer to store the generated codec
1381 * Returns 0 if successful, or a negative error code.
1383 int snd_hda_codec_new(struct hda_bus *bus,
1384 unsigned int codec_addr,
1385 struct hda_codec **codecp)
1387 struct hda_codec *codec;
1392 if (snd_BUG_ON(!bus))
1394 if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS))
1397 if (bus->caddr_tbl[codec_addr]) {
1398 snd_printk(KERN_ERR "hda_codec: "
1399 "address 0x%x is already occupied\n", codec_addr);
1403 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
1404 if (codec == NULL) {
1405 snd_printk(KERN_ERR "can't allocate struct hda_codec\n");
1410 codec->addr = codec_addr;
1411 mutex_init(&codec->spdif_mutex);
1412 mutex_init(&codec->control_mutex);
1413 mutex_init(&codec->hash_mutex);
1414 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
1415 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
1416 snd_array_init(&codec->mixers, sizeof(struct hda_nid_item), 32);
1417 snd_array_init(&codec->nids, sizeof(struct hda_nid_item), 32);
1418 snd_array_init(&codec->init_pins, sizeof(struct hda_pincfg), 16);
1419 snd_array_init(&codec->driver_pins, sizeof(struct hda_pincfg), 16);
1420 snd_array_init(&codec->cvt_setups, sizeof(struct hda_cvt_setup), 8);
1421 snd_array_init(&codec->spdif_out, sizeof(struct hda_spdif_out), 16);
1422 snd_array_init(&codec->jacktbl, sizeof(struct hda_jack_tbl), 16);
1423 snd_array_init(&codec->verbs, sizeof(struct hda_verb *), 8);
1424 INIT_LIST_HEAD(&codec->conn_list);
1426 INIT_DELAYED_WORK(&codec->jackpoll_work, hda_jackpoll_work);
1429 spin_lock_init(&codec->power_lock);
1430 INIT_DELAYED_WORK(&codec->power_work, hda_power_work);
1431 /* snd_hda_codec_new() marks the codec as power-up, and leave it as is.
1432 * the caller has to power down appropriatley after initialization
1435 hda_keep_power_on(codec);
1436 hda_call_pm_notify(bus, true);
1439 if (codec->bus->modelname) {
1440 codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL);
1441 if (!codec->modelname) {
1442 snd_hda_codec_free(codec);
1447 list_add_tail(&codec->list, &bus->codec_list);
1448 bus->caddr_tbl[codec_addr] = codec;
1450 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1452 if (codec->vendor_id == -1)
1453 /* read again, hopefully the access method was corrected
1454 * in the last read...
1456 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1458 codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1459 AC_PAR_SUBSYSTEM_ID);
1460 codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1463 setup_fg_nodes(codec);
1464 if (!codec->afg && !codec->mfg) {
1465 snd_printdd("hda_codec: no AFG or MFG node found\n");
1470 fg = codec->afg ? codec->afg : codec->mfg;
1471 err = read_widget_caps(codec, fg);
1473 snd_printk(KERN_ERR "hda_codec: cannot malloc\n");
1476 err = read_pin_defaults(codec);
1480 if (!codec->subsystem_id) {
1481 codec->subsystem_id =
1482 snd_hda_codec_read(codec, fg, 0,
1483 AC_VERB_GET_SUBSYSTEM_ID, 0);
1487 codec->d3_stop_clk = snd_hda_codec_get_supported_ps(codec, fg,
1489 if (!codec->d3_stop_clk)
1490 bus->power_keep_link_on = 1;
1492 codec->epss = snd_hda_codec_get_supported_ps(codec, fg,
1495 /* power-up all before initialization */
1496 hda_set_power_state(codec, AC_PWRST_D0);
1498 snd_hda_codec_proc_new(codec);
1500 snd_hda_create_hwdep(codec);
1502 sprintf(component, "HDA:%08x,%08x,%08x", codec->vendor_id,
1503 codec->subsystem_id, codec->revision_id);
1504 snd_component_add(codec->bus->card, component);
1511 snd_hda_codec_free(codec);
1514 EXPORT_SYMBOL_HDA(snd_hda_codec_new);
1516 int snd_hda_codec_update_widgets(struct hda_codec *codec)
1521 /* Assume the function group node does not change,
1522 * only the widget nodes may change.
1524 kfree(codec->wcaps);
1525 fg = codec->afg ? codec->afg : codec->mfg;
1526 err = read_widget_caps(codec, fg);
1528 snd_printk(KERN_ERR "hda_codec: cannot malloc\n");
1532 snd_array_free(&codec->init_pins);
1533 err = read_pin_defaults(codec);
1537 EXPORT_SYMBOL_HDA(snd_hda_codec_update_widgets);
1541 * snd_hda_codec_configure - (Re-)configure the HD-audio codec
1542 * @codec: the HDA codec
1544 * Start parsing of the given codec tree and (re-)initialize the whole
1547 * Returns 0 if successful or a negative error code.
1549 int snd_hda_codec_configure(struct hda_codec *codec)
1553 codec->preset = find_codec_preset(codec);
1554 if (!codec->vendor_name || !codec->chip_name) {
1555 err = get_codec_name(codec);
1560 if (is_generic_config(codec)) {
1561 err = snd_hda_parse_generic_codec(codec);
1564 if (codec->preset && codec->preset->patch) {
1565 err = codec->preset->patch(codec);
1569 /* call the default parser */
1570 err = snd_hda_parse_generic_codec(codec);
1572 printk(KERN_ERR "hda-codec: No codec parser is available\n");
1575 if (!err && codec->patch_ops.unsol_event)
1576 err = init_unsol_queue(codec->bus);
1577 /* audio codec should override the mixer name */
1578 if (!err && (codec->afg || !*codec->bus->card->mixername))
1579 snprintf(codec->bus->card->mixername,
1580 sizeof(codec->bus->card->mixername),
1581 "%s %s", codec->vendor_name, codec->chip_name);
1584 EXPORT_SYMBOL_HDA(snd_hda_codec_configure);
1586 /* update the stream-id if changed */
1587 static void update_pcm_stream_id(struct hda_codec *codec,
1588 struct hda_cvt_setup *p, hda_nid_t nid,
1589 u32 stream_tag, int channel_id)
1591 unsigned int oldval, newval;
1593 if (p->stream_tag != stream_tag || p->channel_id != channel_id) {
1594 oldval = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0);
1595 newval = (stream_tag << 4) | channel_id;
1596 if (oldval != newval)
1597 snd_hda_codec_write(codec, nid, 0,
1598 AC_VERB_SET_CHANNEL_STREAMID,
1600 p->stream_tag = stream_tag;
1601 p->channel_id = channel_id;
1605 /* update the format-id if changed */
1606 static void update_pcm_format(struct hda_codec *codec, struct hda_cvt_setup *p,
1607 hda_nid_t nid, int format)
1609 unsigned int oldval;
1611 if (p->format_id != format) {
1612 oldval = snd_hda_codec_read(codec, nid, 0,
1613 AC_VERB_GET_STREAM_FORMAT, 0);
1614 if (oldval != format) {
1616 snd_hda_codec_write(codec, nid, 0,
1617 AC_VERB_SET_STREAM_FORMAT,
1620 p->format_id = format;
1625 * snd_hda_codec_setup_stream - set up the codec for streaming
1626 * @codec: the CODEC to set up
1627 * @nid: the NID to set up
1628 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
1629 * @channel_id: channel id to pass, zero based.
1630 * @format: stream format.
1632 void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
1634 int channel_id, int format)
1636 struct hda_codec *c;
1637 struct hda_cvt_setup *p;
1644 snd_printdd("hda_codec_setup_stream: "
1645 "NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
1646 nid, stream_tag, channel_id, format);
1647 p = get_hda_cvt_setup(codec, nid);
1651 if (codec->pcm_format_first)
1652 update_pcm_format(codec, p, nid, format);
1653 update_pcm_stream_id(codec, p, nid, stream_tag, channel_id);
1654 if (!codec->pcm_format_first)
1655 update_pcm_format(codec, p, nid, format);
1660 /* make other inactive cvts with the same stream-tag dirty */
1661 type = get_wcaps_type(get_wcaps(codec, nid));
1662 list_for_each_entry(c, &codec->bus->codec_list, list) {
1663 for (i = 0; i < c->cvt_setups.used; i++) {
1664 p = snd_array_elem(&c->cvt_setups, i);
1665 if (!p->active && p->stream_tag == stream_tag &&
1666 get_wcaps_type(get_wcaps(c, p->nid)) == type)
1671 EXPORT_SYMBOL_HDA(snd_hda_codec_setup_stream);
1673 static void really_cleanup_stream(struct hda_codec *codec,
1674 struct hda_cvt_setup *q);
1677 * __snd_hda_codec_cleanup_stream - clean up the codec for closing
1678 * @codec: the CODEC to clean up
1679 * @nid: the NID to clean up
1680 * @do_now: really clean up the stream instead of clearing the active flag
1682 void __snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid,
1685 struct hda_cvt_setup *p;
1690 if (codec->no_sticky_stream)
1693 snd_printdd("hda_codec_cleanup_stream: NID=0x%x\n", nid);
1694 p = get_hda_cvt_setup(codec, nid);
1696 /* here we just clear the active flag when do_now isn't set;
1697 * actual clean-ups will be done later in
1698 * purify_inactive_streams() called from snd_hda_codec_prpapre()
1701 really_cleanup_stream(codec, p);
1706 EXPORT_SYMBOL_HDA(__snd_hda_codec_cleanup_stream);
1708 static void really_cleanup_stream(struct hda_codec *codec,
1709 struct hda_cvt_setup *q)
1711 hda_nid_t nid = q->nid;
1712 if (q->stream_tag || q->channel_id)
1713 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
1715 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0
1717 memset(q, 0, sizeof(*q));
1721 /* clean up the all conflicting obsolete streams */
1722 static void purify_inactive_streams(struct hda_codec *codec)
1724 struct hda_codec *c;
1727 list_for_each_entry(c, &codec->bus->codec_list, list) {
1728 for (i = 0; i < c->cvt_setups.used; i++) {
1729 struct hda_cvt_setup *p;
1730 p = snd_array_elem(&c->cvt_setups, i);
1732 really_cleanup_stream(c, p);
1738 /* clean up all streams; called from suspend */
1739 static void hda_cleanup_all_streams(struct hda_codec *codec)
1743 for (i = 0; i < codec->cvt_setups.used; i++) {
1744 struct hda_cvt_setup *p = snd_array_elem(&codec->cvt_setups, i);
1746 really_cleanup_stream(codec, p);
1752 * amp access functions
1755 /* FIXME: more better hash key? */
1756 #define HDA_HASH_KEY(nid, dir, idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
1757 #define HDA_HASH_PINCAP_KEY(nid) (u32)((nid) + (0x02 << 24))
1758 #define HDA_HASH_PARPCM_KEY(nid) (u32)((nid) + (0x03 << 24))
1759 #define HDA_HASH_PARSTR_KEY(nid) (u32)((nid) + (0x04 << 24))
1760 #define INFO_AMP_CAPS (1<<0)
1761 #define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
1763 /* initialize the hash table */
1764 static void init_hda_cache(struct hda_cache_rec *cache,
1765 unsigned int record_size)
1767 memset(cache, 0, sizeof(*cache));
1768 memset(cache->hash, 0xff, sizeof(cache->hash));
1769 snd_array_init(&cache->buf, record_size, 64);
1772 static void free_hda_cache(struct hda_cache_rec *cache)
1774 snd_array_free(&cache->buf);
1777 /* query the hash. allocate an entry if not found. */
1778 static struct hda_cache_head *get_hash(struct hda_cache_rec *cache, u32 key)
1780 u16 idx = key % (u16)ARRAY_SIZE(cache->hash);
1781 u16 cur = cache->hash[idx];
1782 struct hda_cache_head *info;
1784 while (cur != 0xffff) {
1785 info = snd_array_elem(&cache->buf, cur);
1786 if (info->key == key)
1793 /* query the hash. allocate an entry if not found. */
1794 static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache,
1797 struct hda_cache_head *info = get_hash(cache, key);
1800 /* add a new hash entry */
1801 info = snd_array_new(&cache->buf);
1804 cur = snd_array_index(&cache->buf, info);
1808 idx = key % (u16)ARRAY_SIZE(cache->hash);
1809 info->next = cache->hash[idx];
1810 cache->hash[idx] = cur;
1815 /* query and allocate an amp hash entry */
1816 static inline struct hda_amp_info *
1817 get_alloc_amp_hash(struct hda_codec *codec, u32 key)
1819 return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key);
1822 /* overwrite the value with the key in the caps hash */
1823 static int write_caps_hash(struct hda_codec *codec, u32 key, unsigned int val)
1825 struct hda_amp_info *info;
1827 mutex_lock(&codec->hash_mutex);
1828 info = get_alloc_amp_hash(codec, key);
1830 mutex_unlock(&codec->hash_mutex);
1833 info->amp_caps = val;
1834 info->head.val |= INFO_AMP_CAPS;
1835 mutex_unlock(&codec->hash_mutex);
1839 /* query the value from the caps hash; if not found, fetch the current
1840 * value from the given function and store in the hash
1843 query_caps_hash(struct hda_codec *codec, hda_nid_t nid, int dir, u32 key,
1844 unsigned int (*func)(struct hda_codec *, hda_nid_t, int))
1846 struct hda_amp_info *info;
1849 mutex_lock(&codec->hash_mutex);
1850 info = get_alloc_amp_hash(codec, key);
1852 mutex_unlock(&codec->hash_mutex);
1855 if (!(info->head.val & INFO_AMP_CAPS)) {
1856 mutex_unlock(&codec->hash_mutex); /* for reentrance */
1857 val = func(codec, nid, dir);
1858 write_caps_hash(codec, key, val);
1860 val = info->amp_caps;
1861 mutex_unlock(&codec->hash_mutex);
1866 static unsigned int read_amp_cap(struct hda_codec *codec, hda_nid_t nid,
1869 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
1871 return snd_hda_param_read(codec, nid,
1872 direction == HDA_OUTPUT ?
1873 AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP);
1877 * query_amp_caps - query AMP capabilities
1878 * @codec: the HD-auio codec
1879 * @nid: the NID to query
1880 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1882 * Query AMP capabilities for the given widget and direction.
1883 * Returns the obtained capability bits.
1885 * When cap bits have been already read, this doesn't read again but
1886 * returns the cached value.
1888 u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
1890 return query_caps_hash(codec, nid, direction,
1891 HDA_HASH_KEY(nid, direction, 0),
1894 EXPORT_SYMBOL_HDA(query_amp_caps);
1897 * snd_hda_override_amp_caps - Override the AMP capabilities
1898 * @codec: the CODEC to clean up
1899 * @nid: the NID to clean up
1900 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1901 * @caps: the capability bits to set
1903 * Override the cached AMP caps bits value by the given one.
1904 * This function is useful if the driver needs to adjust the AMP ranges,
1905 * e.g. limit to 0dB, etc.
1907 * Returns zero if successful or a negative error code.
1909 int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
1912 return write_caps_hash(codec, HDA_HASH_KEY(nid, dir, 0), caps);
1914 EXPORT_SYMBOL_HDA(snd_hda_override_amp_caps);
1916 static unsigned int read_pin_cap(struct hda_codec *codec, hda_nid_t nid,
1919 return snd_hda_param_read(codec, nid, AC_PAR_PIN_CAP);
1923 * snd_hda_query_pin_caps - Query PIN capabilities
1924 * @codec: the HD-auio codec
1925 * @nid: the NID to query
1927 * Query PIN capabilities for the given widget.
1928 * Returns the obtained capability bits.
1930 * When cap bits have been already read, this doesn't read again but
1931 * returns the cached value.
1933 u32 snd_hda_query_pin_caps(struct hda_codec *codec, hda_nid_t nid)
1935 return query_caps_hash(codec, nid, 0, HDA_HASH_PINCAP_KEY(nid),
1938 EXPORT_SYMBOL_HDA(snd_hda_query_pin_caps);
1941 * snd_hda_override_pin_caps - Override the pin capabilities
1943 * @nid: the NID to override
1944 * @caps: the capability bits to set
1946 * Override the cached PIN capabilitiy bits value by the given one.
1948 * Returns zero if successful or a negative error code.
1950 int snd_hda_override_pin_caps(struct hda_codec *codec, hda_nid_t nid,
1953 return write_caps_hash(codec, HDA_HASH_PINCAP_KEY(nid), caps);
1955 EXPORT_SYMBOL_HDA(snd_hda_override_pin_caps);
1957 /* read or sync the hash value with the current value;
1958 * call within hash_mutex
1960 static struct hda_amp_info *
1961 update_amp_hash(struct hda_codec *codec, hda_nid_t nid, int ch,
1962 int direction, int index, bool init_only)
1964 struct hda_amp_info *info;
1965 unsigned int parm, val = 0;
1966 bool val_read = false;
1969 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
1972 if (!(info->head.val & INFO_AMP_VOL(ch))) {
1974 mutex_unlock(&codec->hash_mutex);
1975 parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
1976 parm |= direction == HDA_OUTPUT ?
1977 AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
1979 val = snd_hda_codec_read(codec, nid, 0,
1980 AC_VERB_GET_AMP_GAIN_MUTE, parm);
1983 mutex_lock(&codec->hash_mutex);
1986 info->vol[ch] = val;
1987 info->head.val |= INFO_AMP_VOL(ch);
1988 } else if (init_only)
1994 * write the current volume in info to the h/w
1996 static void put_vol_mute(struct hda_codec *codec, unsigned int amp_caps,
1997 hda_nid_t nid, int ch, int direction, int index,
2002 parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
2003 parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
2004 parm |= index << AC_AMP_SET_INDEX_SHIFT;
2005 if ((val & HDA_AMP_MUTE) && !(amp_caps & AC_AMPCAP_MUTE) &&
2006 (amp_caps & AC_AMPCAP_MIN_MUTE))
2007 ; /* set the zero value as a fake mute */
2010 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
2014 * snd_hda_codec_amp_read - Read AMP value
2015 * @codec: HD-audio codec
2016 * @nid: NID to read the AMP value
2017 * @ch: channel (left=0 or right=1)
2018 * @direction: #HDA_INPUT or #HDA_OUTPUT
2019 * @index: the index value (only for input direction)
2021 * Read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
2023 int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
2024 int direction, int index)
2026 struct hda_amp_info *info;
2027 unsigned int val = 0;
2029 mutex_lock(&codec->hash_mutex);
2030 info = update_amp_hash(codec, nid, ch, direction, index, false);
2032 val = info->vol[ch];
2033 mutex_unlock(&codec->hash_mutex);
2036 EXPORT_SYMBOL_HDA(snd_hda_codec_amp_read);
2038 static int codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
2039 int direction, int idx, int mask, int val,
2042 struct hda_amp_info *info;
2044 unsigned int cache_only;
2046 if (snd_BUG_ON(mask & ~0xff))
2050 mutex_lock(&codec->hash_mutex);
2051 info = update_amp_hash(codec, nid, ch, direction, idx, init_only);
2053 mutex_unlock(&codec->hash_mutex);
2056 val |= info->vol[ch] & ~mask;
2057 if (info->vol[ch] == val) {
2058 mutex_unlock(&codec->hash_mutex);
2061 info->vol[ch] = val;
2062 cache_only = info->head.dirty = codec->cached_write;
2063 caps = info->amp_caps;
2064 mutex_unlock(&codec->hash_mutex);
2066 put_vol_mute(codec, caps, nid, ch, direction, idx, val);
2071 * snd_hda_codec_amp_update - update the AMP value
2072 * @codec: HD-audio codec
2073 * @nid: NID to read the AMP value
2074 * @ch: channel (left=0 or right=1)
2075 * @direction: #HDA_INPUT or #HDA_OUTPUT
2076 * @idx: the index value (only for input direction)
2077 * @mask: bit mask to set
2078 * @val: the bits value to set
2080 * Update the AMP value with a bit mask.
2081 * Returns 0 if the value is unchanged, 1 if changed.
2083 int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
2084 int direction, int idx, int mask, int val)
2086 return codec_amp_update(codec, nid, ch, direction, idx, mask, val, false);
2088 EXPORT_SYMBOL_HDA(snd_hda_codec_amp_update);
2091 * snd_hda_codec_amp_stereo - update the AMP stereo values
2092 * @codec: HD-audio codec
2093 * @nid: NID to read the AMP value
2094 * @direction: #HDA_INPUT or #HDA_OUTPUT
2095 * @idx: the index value (only for input direction)
2096 * @mask: bit mask to set
2097 * @val: the bits value to set
2099 * Update the AMP values like snd_hda_codec_amp_update(), but for a
2100 * stereo widget with the same mask and value.
2102 int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
2103 int direction, int idx, int mask, int val)
2107 if (snd_BUG_ON(mask & ~0xff))
2109 for (ch = 0; ch < 2; ch++)
2110 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
2114 EXPORT_SYMBOL_HDA(snd_hda_codec_amp_stereo);
2116 /* Works like snd_hda_codec_amp_update() but it writes the value only at
2117 * the first access. If the amp was already initialized / updated beforehand,
2118 * this does nothing.
2120 int snd_hda_codec_amp_init(struct hda_codec *codec, hda_nid_t nid, int ch,
2121 int dir, int idx, int mask, int val)
2123 return codec_amp_update(codec, nid, ch, dir, idx, mask, val, true);
2125 EXPORT_SYMBOL_HDA(snd_hda_codec_amp_init);
2127 int snd_hda_codec_amp_init_stereo(struct hda_codec *codec, hda_nid_t nid,
2128 int dir, int idx, int mask, int val)
2132 if (snd_BUG_ON(mask & ~0xff))
2134 for (ch = 0; ch < 2; ch++)
2135 ret |= snd_hda_codec_amp_init(codec, nid, ch, dir,
2139 EXPORT_SYMBOL_HDA(snd_hda_codec_amp_init_stereo);
2142 * snd_hda_codec_resume_amp - Resume all AMP commands from the cache
2143 * @codec: HD-audio codec
2145 * Resume the all amp commands from the cache.
2147 void snd_hda_codec_resume_amp(struct hda_codec *codec)
2151 mutex_lock(&codec->hash_mutex);
2152 codec->cached_write = 0;
2153 for (i = 0; i < codec->amp_cache.buf.used; i++) {
2154 struct hda_amp_info *buffer;
2157 unsigned int idx, dir, ch;
2158 struct hda_amp_info info;
2160 buffer = snd_array_elem(&codec->amp_cache.buf, i);
2161 if (!buffer->head.dirty)
2163 buffer->head.dirty = 0;
2165 key = info.head.key;
2169 idx = (key >> 16) & 0xff;
2170 dir = (key >> 24) & 0xff;
2171 for (ch = 0; ch < 2; ch++) {
2172 if (!(info.head.val & INFO_AMP_VOL(ch)))
2174 mutex_unlock(&codec->hash_mutex);
2175 put_vol_mute(codec, info.amp_caps, nid, ch, dir, idx,
2177 mutex_lock(&codec->hash_mutex);
2180 mutex_unlock(&codec->hash_mutex);
2182 EXPORT_SYMBOL_HDA(snd_hda_codec_resume_amp);
2184 static u32 get_amp_max_value(struct hda_codec *codec, hda_nid_t nid, int dir,
2187 u32 caps = query_amp_caps(codec, nid, dir);
2189 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
2196 * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer
2198 * The control element is supposed to have the private_value field
2199 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2201 int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
2202 struct snd_ctl_elem_info *uinfo)
2204 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2205 u16 nid = get_amp_nid(kcontrol);
2206 u8 chs = get_amp_channels(kcontrol);
2207 int dir = get_amp_direction(kcontrol);
2208 unsigned int ofs = get_amp_offset(kcontrol);
2210 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2211 uinfo->count = chs == 3 ? 2 : 1;
2212 uinfo->value.integer.min = 0;
2213 uinfo->value.integer.max = get_amp_max_value(codec, nid, dir, ofs);
2214 if (!uinfo->value.integer.max) {
2215 printk(KERN_WARNING "hda_codec: "
2216 "num_steps = 0 for NID=0x%x (ctl = %s)\n", nid,
2222 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_info);
2225 static inline unsigned int
2226 read_amp_value(struct hda_codec *codec, hda_nid_t nid,
2227 int ch, int dir, int idx, unsigned int ofs)
2230 val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
2231 val &= HDA_AMP_VOLMASK;
2240 update_amp_value(struct hda_codec *codec, hda_nid_t nid,
2241 int ch, int dir, int idx, unsigned int ofs,
2244 unsigned int maxval;
2248 /* ofs = 0: raw max value */
2249 maxval = get_amp_max_value(codec, nid, dir, 0);
2252 return snd_hda_codec_amp_update(codec, nid, ch, dir, idx,
2253 HDA_AMP_VOLMASK, val);
2257 * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume
2259 * The control element is supposed to have the private_value field
2260 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2262 int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
2263 struct snd_ctl_elem_value *ucontrol)
2265 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2266 hda_nid_t nid = get_amp_nid(kcontrol);
2267 int chs = get_amp_channels(kcontrol);
2268 int dir = get_amp_direction(kcontrol);
2269 int idx = get_amp_index(kcontrol);
2270 unsigned int ofs = get_amp_offset(kcontrol);
2271 long *valp = ucontrol->value.integer.value;
2274 *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs);
2276 *valp = read_amp_value(codec, nid, 1, dir, idx, ofs);
2279 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_get);
2282 * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume
2284 * The control element is supposed to have the private_value field
2285 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2287 int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
2288 struct snd_ctl_elem_value *ucontrol)
2290 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2291 hda_nid_t nid = get_amp_nid(kcontrol);
2292 int chs = get_amp_channels(kcontrol);
2293 int dir = get_amp_direction(kcontrol);
2294 int idx = get_amp_index(kcontrol);
2295 unsigned int ofs = get_amp_offset(kcontrol);
2296 long *valp = ucontrol->value.integer.value;
2299 snd_hda_power_up(codec);
2301 change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp);
2305 change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp);
2306 snd_hda_power_down(codec);
2309 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_put);
2312 * snd_hda_mixer_amp_volume_put - TLV callback for a standard AMP mixer volume
2314 * The control element is supposed to have the private_value field
2315 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2317 int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2318 unsigned int size, unsigned int __user *_tlv)
2320 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2321 hda_nid_t nid = get_amp_nid(kcontrol);
2322 int dir = get_amp_direction(kcontrol);
2323 unsigned int ofs = get_amp_offset(kcontrol);
2324 bool min_mute = get_amp_min_mute(kcontrol);
2325 u32 caps, val1, val2;
2327 if (size < 4 * sizeof(unsigned int))
2329 caps = query_amp_caps(codec, nid, dir);
2330 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2331 val2 = (val2 + 1) * 25;
2332 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
2334 val1 = ((int)val1) * ((int)val2);
2335 if (min_mute || (caps & AC_AMPCAP_MIN_MUTE))
2336 val2 |= TLV_DB_SCALE_MUTE;
2337 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
2339 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
2341 if (put_user(val1, _tlv + 2))
2343 if (put_user(val2, _tlv + 3))
2347 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_tlv);
2350 * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control
2351 * @codec: HD-audio codec
2352 * @nid: NID of a reference widget
2353 * @dir: #HDA_INPUT or #HDA_OUTPUT
2354 * @tlv: TLV data to be stored, at least 4 elements
2356 * Set (static) TLV data for a virtual master volume using the AMP caps
2357 * obtained from the reference NID.
2358 * The volume range is recalculated as if the max volume is 0dB.
2360 void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
2366 caps = query_amp_caps(codec, nid, dir);
2367 nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
2368 step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2369 step = (step + 1) * 25;
2370 tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
2371 tlv[1] = 2 * sizeof(unsigned int);
2372 tlv[2] = -nums * step;
2375 EXPORT_SYMBOL_HDA(snd_hda_set_vmaster_tlv);
2377 /* find a mixer control element with the given name */
2378 static struct snd_kcontrol *
2379 find_mixer_ctl(struct hda_codec *codec, const char *name, int dev, int idx)
2381 struct snd_ctl_elem_id id;
2382 memset(&id, 0, sizeof(id));
2383 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
2386 if (snd_BUG_ON(strlen(name) >= sizeof(id.name)))
2388 strcpy(id.name, name);
2389 return snd_ctl_find_id(codec->bus->card, &id);
2393 * snd_hda_find_mixer_ctl - Find a mixer control element with the given name
2394 * @codec: HD-audio codec
2395 * @name: ctl id name string
2397 * Get the control element with the given id string and IFACE_MIXER.
2399 struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
2402 return find_mixer_ctl(codec, name, 0, 0);
2404 EXPORT_SYMBOL_HDA(snd_hda_find_mixer_ctl);
2406 static int find_empty_mixer_ctl_idx(struct hda_codec *codec, const char *name,
2410 /* 16 ctlrs should be large enough */
2411 for (i = 0, idx = start_idx; i < 16; i++, idx++) {
2412 if (!find_mixer_ctl(codec, name, 0, idx))
2419 * snd_hda_ctl_add - Add a control element and assign to the codec
2420 * @codec: HD-audio codec
2421 * @nid: corresponding NID (optional)
2422 * @kctl: the control element to assign
2424 * Add the given control element to an array inside the codec instance.
2425 * All control elements belonging to a codec are supposed to be added
2426 * by this function so that a proper clean-up works at the free or
2427 * reconfiguration time.
2429 * If non-zero @nid is passed, the NID is assigned to the control element.
2430 * The assignment is shown in the codec proc file.
2432 * snd_hda_ctl_add() checks the control subdev id field whether
2433 * #HDA_SUBDEV_NID_FLAG bit is set. If set (and @nid is zero), the lower
2434 * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit
2435 * specifies if kctl->private_value is a HDA amplifier value.
2437 int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid,
2438 struct snd_kcontrol *kctl)
2441 unsigned short flags = 0;
2442 struct hda_nid_item *item;
2444 if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) {
2445 flags |= HDA_NID_ITEM_AMP;
2447 nid = get_amp_nid_(kctl->private_value);
2449 if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0)
2450 nid = kctl->id.subdevice & 0xffff;
2451 if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG))
2452 kctl->id.subdevice = 0;
2453 err = snd_ctl_add(codec->bus->card, kctl);
2456 item = snd_array_new(&codec->mixers);
2461 item->flags = flags;
2464 EXPORT_SYMBOL_HDA(snd_hda_ctl_add);
2467 * snd_hda_add_nid - Assign a NID to a control element
2468 * @codec: HD-audio codec
2469 * @nid: corresponding NID (optional)
2470 * @kctl: the control element to assign
2471 * @index: index to kctl
2473 * Add the given control element to an array inside the codec instance.
2474 * This function is used when #snd_hda_ctl_add cannot be used for 1:1
2475 * NID:KCTL mapping - for example "Capture Source" selector.
2477 int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl,
2478 unsigned int index, hda_nid_t nid)
2480 struct hda_nid_item *item;
2483 item = snd_array_new(&codec->nids);
2487 item->index = index;
2491 printk(KERN_ERR "hda-codec: no NID for mapping control %s:%d:%d\n",
2492 kctl->id.name, kctl->id.index, index);
2495 EXPORT_SYMBOL_HDA(snd_hda_add_nid);
2498 * snd_hda_ctls_clear - Clear all controls assigned to the given codec
2499 * @codec: HD-audio codec
2501 void snd_hda_ctls_clear(struct hda_codec *codec)
2504 struct hda_nid_item *items = codec->mixers.list;
2505 for (i = 0; i < codec->mixers.used; i++)
2506 snd_ctl_remove(codec->bus->card, items[i].kctl);
2507 snd_array_free(&codec->mixers);
2508 snd_array_free(&codec->nids);
2511 /* pseudo device locking
2512 * toggle card->shutdown to allow/disallow the device access (as a hack)
2514 int snd_hda_lock_devices(struct hda_bus *bus)
2516 struct snd_card *card = bus->card;
2517 struct hda_codec *codec;
2519 spin_lock(&card->files_lock);
2523 if (!list_empty(&card->ctl_files))
2526 list_for_each_entry(codec, &bus->codec_list, list) {
2528 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
2529 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
2532 if (cpcm->pcm->streams[0].substream_opened ||
2533 cpcm->pcm->streams[1].substream_opened)
2537 spin_unlock(&card->files_lock);
2543 spin_unlock(&card->files_lock);
2546 EXPORT_SYMBOL_HDA(snd_hda_lock_devices);
2548 void snd_hda_unlock_devices(struct hda_bus *bus)
2550 struct snd_card *card = bus->card;
2553 spin_lock(&card->files_lock);
2555 spin_unlock(&card->files_lock);
2557 EXPORT_SYMBOL_HDA(snd_hda_unlock_devices);
2560 * snd_hda_codec_reset - Clear all objects assigned to the codec
2561 * @codec: HD-audio codec
2563 * This frees the all PCM and control elements assigned to the codec, and
2564 * clears the caches and restores the pin default configurations.
2566 * When a device is being used, it returns -EBSY. If successfully freed,
2569 int snd_hda_codec_reset(struct hda_codec *codec)
2571 struct hda_bus *bus = codec->bus;
2572 struct snd_card *card = bus->card;
2575 if (snd_hda_lock_devices(bus) < 0)
2578 /* OK, let it free */
2579 cancel_delayed_work_sync(&codec->jackpoll_work);
2581 cancel_delayed_work_sync(&codec->power_work);
2582 codec->power_on = 0;
2583 codec->power_transition = 0;
2584 codec->power_jiffies = jiffies;
2585 flush_workqueue(bus->workq);
2587 snd_hda_ctls_clear(codec);
2589 for (i = 0; i < codec->num_pcms; i++) {
2590 if (codec->pcm_info[i].pcm) {
2591 snd_device_free(card, codec->pcm_info[i].pcm);
2592 clear_bit(codec->pcm_info[i].device,
2596 if (codec->patch_ops.free)
2597 codec->patch_ops.free(codec);
2598 memset(&codec->patch_ops, 0, sizeof(codec->patch_ops));
2599 snd_hda_jack_tbl_clear(codec);
2600 codec->proc_widget_hook = NULL;
2602 free_hda_cache(&codec->amp_cache);
2603 free_hda_cache(&codec->cmd_cache);
2604 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
2605 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
2606 /* free only driver_pins so that init_pins + user_pins are restored */
2607 snd_array_free(&codec->driver_pins);
2608 snd_array_free(&codec->cvt_setups);
2609 snd_array_free(&codec->spdif_out);
2610 snd_array_free(&codec->verbs);
2611 codec->num_pcms = 0;
2612 codec->pcm_info = NULL;
2613 codec->preset = NULL;
2614 codec->slave_dig_outs = NULL;
2615 codec->spdif_status_reset = 0;
2616 module_put(codec->owner);
2617 codec->owner = NULL;
2619 /* allow device access again */
2620 snd_hda_unlock_devices(bus);
2624 typedef int (*map_slave_func_t)(void *, struct snd_kcontrol *);
2626 /* apply the function to all matching slave ctls in the mixer list */
2627 static int map_slaves(struct hda_codec *codec, const char * const *slaves,
2628 const char *suffix, map_slave_func_t func, void *data)
2630 struct hda_nid_item *items;
2631 const char * const *s;
2634 items = codec->mixers.list;
2635 for (i = 0; i < codec->mixers.used; i++) {
2636 struct snd_kcontrol *sctl = items[i].kctl;
2637 if (!sctl || !sctl->id.name ||
2638 sctl->id.iface != SNDRV_CTL_ELEM_IFACE_MIXER)
2640 for (s = slaves; *s; s++) {
2641 char tmpname[sizeof(sctl->id.name)];
2642 const char *name = *s;
2644 snprintf(tmpname, sizeof(tmpname), "%s %s",
2648 if (!strcmp(sctl->id.name, name)) {
2649 err = func(data, sctl);
2659 static int check_slave_present(void *data, struct snd_kcontrol *sctl)
2664 /* guess the value corresponding to 0dB */
2665 static int get_kctl_0dB_offset(struct snd_kcontrol *kctl)
2668 const int *tlv = NULL;
2671 if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
2672 /* FIXME: set_fs() hack for obtaining user-space TLV data */
2673 mm_segment_t fs = get_fs();
2675 if (!kctl->tlv.c(kctl, 0, sizeof(_tlv), _tlv))
2678 } else if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_READ)
2680 if (tlv && tlv[0] == SNDRV_CTL_TLVT_DB_SCALE)
2681 val = -tlv[2] / tlv[3];
2685 /* call kctl->put with the given value(s) */
2686 static int put_kctl_with_value(struct snd_kcontrol *kctl, int val)
2688 struct snd_ctl_elem_value *ucontrol;
2689 ucontrol = kzalloc(sizeof(*ucontrol), GFP_KERNEL);
2692 ucontrol->value.integer.value[0] = val;
2693 ucontrol->value.integer.value[1] = val;
2694 kctl->put(kctl, ucontrol);
2699 /* initialize the slave volume with 0dB */
2700 static int init_slave_0dB(void *data, struct snd_kcontrol *slave)
2702 int offset = get_kctl_0dB_offset(slave);
2704 put_kctl_with_value(slave, offset);
2708 /* unmute the slave */
2709 static int init_slave_unmute(void *data, struct snd_kcontrol *slave)
2711 return put_kctl_with_value(slave, 1);
2715 * snd_hda_add_vmaster - create a virtual master control and add slaves
2716 * @codec: HD-audio codec
2717 * @name: vmaster control name
2718 * @tlv: TLV data (optional)
2719 * @slaves: slave control names (optional)
2720 * @suffix: suffix string to each slave name (optional)
2721 * @init_slave_vol: initialize slaves to unmute/0dB
2722 * @ctl_ret: store the vmaster kcontrol in return
2724 * Create a virtual master control with the given name. The TLV data
2725 * must be either NULL or a valid data.
2727 * @slaves is a NULL-terminated array of strings, each of which is a
2728 * slave control name. All controls with these names are assigned to
2729 * the new virtual master control.
2731 * This function returns zero if successful or a negative error code.
2733 int __snd_hda_add_vmaster(struct hda_codec *codec, char *name,
2734 unsigned int *tlv, const char * const *slaves,
2735 const char *suffix, bool init_slave_vol,
2736 struct snd_kcontrol **ctl_ret)
2738 struct snd_kcontrol *kctl;
2744 err = map_slaves(codec, slaves, suffix, check_slave_present, NULL);
2746 snd_printdd("No slave found for %s\n", name);
2749 kctl = snd_ctl_make_virtual_master(name, tlv);
2752 err = snd_hda_ctl_add(codec, 0, kctl);
2756 err = map_slaves(codec, slaves, suffix,
2757 (map_slave_func_t)snd_ctl_add_slave, kctl);
2761 /* init with master mute & zero volume */
2762 put_kctl_with_value(kctl, 0);
2764 map_slaves(codec, slaves, suffix,
2765 tlv ? init_slave_0dB : init_slave_unmute, kctl);
2771 EXPORT_SYMBOL_HDA(__snd_hda_add_vmaster);
2774 * mute-LED control using vmaster
2776 static int vmaster_mute_mode_info(struct snd_kcontrol *kcontrol,
2777 struct snd_ctl_elem_info *uinfo)
2779 static const char * const texts[] = {
2780 "On", "Off", "Follow Master"
2784 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2786 uinfo->value.enumerated.items = 3;
2787 index = uinfo->value.enumerated.item;
2790 strcpy(uinfo->value.enumerated.name, texts[index]);
2794 static int vmaster_mute_mode_get(struct snd_kcontrol *kcontrol,
2795 struct snd_ctl_elem_value *ucontrol)
2797 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2798 ucontrol->value.enumerated.item[0] = hook->mute_mode;
2802 static int vmaster_mute_mode_put(struct snd_kcontrol *kcontrol,
2803 struct snd_ctl_elem_value *ucontrol)
2805 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2806 unsigned int old_mode = hook->mute_mode;
2808 hook->mute_mode = ucontrol->value.enumerated.item[0];
2809 if (hook->mute_mode > HDA_VMUTE_FOLLOW_MASTER)
2810 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
2811 if (old_mode == hook->mute_mode)
2813 snd_hda_sync_vmaster_hook(hook);
2817 static struct snd_kcontrol_new vmaster_mute_mode = {
2818 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2819 .name = "Mute-LED Mode",
2820 .info = vmaster_mute_mode_info,
2821 .get = vmaster_mute_mode_get,
2822 .put = vmaster_mute_mode_put,
2826 * Add a mute-LED hook with the given vmaster switch kctl
2827 * "Mute-LED Mode" control is automatically created and associated with
2830 int snd_hda_add_vmaster_hook(struct hda_codec *codec,
2831 struct hda_vmaster_mute_hook *hook,
2832 bool expose_enum_ctl)
2834 struct snd_kcontrol *kctl;
2836 if (!hook->hook || !hook->sw_kctl)
2838 snd_ctl_add_vmaster_hook(hook->sw_kctl, hook->hook, codec);
2839 hook->codec = codec;
2840 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
2841 if (!expose_enum_ctl)
2843 kctl = snd_ctl_new1(&vmaster_mute_mode, hook);
2846 return snd_hda_ctl_add(codec, 0, kctl);
2848 EXPORT_SYMBOL_HDA(snd_hda_add_vmaster_hook);
2851 * Call the hook with the current value for synchronization
2852 * Should be called in init callback
2854 void snd_hda_sync_vmaster_hook(struct hda_vmaster_mute_hook *hook)
2856 if (!hook->hook || !hook->codec)
2858 /* don't call vmaster hook in the destructor since it might have
2859 * been already destroyed
2861 if (hook->codec->bus->shutdown)
2863 switch (hook->mute_mode) {
2864 case HDA_VMUTE_FOLLOW_MASTER:
2865 snd_ctl_sync_vmaster_hook(hook->sw_kctl);
2868 hook->hook(hook->codec, hook->mute_mode);
2872 EXPORT_SYMBOL_HDA(snd_hda_sync_vmaster_hook);
2876 * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch
2878 * The control element is supposed to have the private_value field
2879 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2881 int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
2882 struct snd_ctl_elem_info *uinfo)
2884 int chs = get_amp_channels(kcontrol);
2886 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2887 uinfo->count = chs == 3 ? 2 : 1;
2888 uinfo->value.integer.min = 0;
2889 uinfo->value.integer.max = 1;
2892 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_info);
2895 * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch
2897 * The control element is supposed to have the private_value field
2898 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2900 int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
2901 struct snd_ctl_elem_value *ucontrol)
2903 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2904 hda_nid_t nid = get_amp_nid(kcontrol);
2905 int chs = get_amp_channels(kcontrol);
2906 int dir = get_amp_direction(kcontrol);
2907 int idx = get_amp_index(kcontrol);
2908 long *valp = ucontrol->value.integer.value;
2911 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
2912 HDA_AMP_MUTE) ? 0 : 1;
2914 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
2915 HDA_AMP_MUTE) ? 0 : 1;
2918 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_get);
2921 * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch
2923 * The control element is supposed to have the private_value field
2924 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2926 int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
2927 struct snd_ctl_elem_value *ucontrol)
2929 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2930 hda_nid_t nid = get_amp_nid(kcontrol);
2931 int chs = get_amp_channels(kcontrol);
2932 int dir = get_amp_direction(kcontrol);
2933 int idx = get_amp_index(kcontrol);
2934 long *valp = ucontrol->value.integer.value;
2937 snd_hda_power_up(codec);
2939 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
2941 *valp ? 0 : HDA_AMP_MUTE);
2945 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
2947 *valp ? 0 : HDA_AMP_MUTE);
2948 hda_call_check_power_status(codec, nid);
2949 snd_hda_power_down(codec);
2952 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put);
2955 * bound volume controls
2957 * bind multiple volumes (# indices, from 0)
2960 #define AMP_VAL_IDX_SHIFT 19
2961 #define AMP_VAL_IDX_MASK (0x0f<<19)
2964 * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control
2966 * The control element is supposed to have the private_value field
2967 * set up via HDA_BIND_MUTE*() macros.
2969 int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
2970 struct snd_ctl_elem_value *ucontrol)
2972 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2976 mutex_lock(&codec->control_mutex);
2977 pval = kcontrol->private_value;
2978 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
2979 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
2980 kcontrol->private_value = pval;
2981 mutex_unlock(&codec->control_mutex);
2984 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_get);
2987 * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control
2989 * The control element is supposed to have the private_value field
2990 * set up via HDA_BIND_MUTE*() macros.
2992 int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
2993 struct snd_ctl_elem_value *ucontrol)
2995 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2997 int i, indices, err = 0, change = 0;
2999 mutex_lock(&codec->control_mutex);
3000 pval = kcontrol->private_value;
3001 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
3002 for (i = 0; i < indices; i++) {
3003 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
3004 (i << AMP_VAL_IDX_SHIFT);
3005 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
3010 kcontrol->private_value = pval;
3011 mutex_unlock(&codec->control_mutex);
3012 return err < 0 ? err : change;
3014 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_put);
3017 * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control
3019 * The control element is supposed to have the private_value field
3020 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
3022 int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
3023 struct snd_ctl_elem_info *uinfo)
3025 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3026 struct hda_bind_ctls *c;
3029 mutex_lock(&codec->control_mutex);
3030 c = (struct hda_bind_ctls *)kcontrol->private_value;
3031 kcontrol->private_value = *c->values;
3032 err = c->ops->info(kcontrol, uinfo);
3033 kcontrol->private_value = (long)c;
3034 mutex_unlock(&codec->control_mutex);
3037 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_info);
3040 * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control
3042 * The control element is supposed to have the private_value field
3043 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
3045 int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
3046 struct snd_ctl_elem_value *ucontrol)
3048 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3049 struct hda_bind_ctls *c;
3052 mutex_lock(&codec->control_mutex);
3053 c = (struct hda_bind_ctls *)kcontrol->private_value;
3054 kcontrol->private_value = *c->values;
3055 err = c->ops->get(kcontrol, ucontrol);
3056 kcontrol->private_value = (long)c;
3057 mutex_unlock(&codec->control_mutex);
3060 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_get);
3063 * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control
3065 * The control element is supposed to have the private_value field
3066 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
3068 int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
3069 struct snd_ctl_elem_value *ucontrol)
3071 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3072 struct hda_bind_ctls *c;
3073 unsigned long *vals;
3074 int err = 0, change = 0;
3076 mutex_lock(&codec->control_mutex);
3077 c = (struct hda_bind_ctls *)kcontrol->private_value;
3078 for (vals = c->values; *vals; vals++) {
3079 kcontrol->private_value = *vals;
3080 err = c->ops->put(kcontrol, ucontrol);
3085 kcontrol->private_value = (long)c;
3086 mutex_unlock(&codec->control_mutex);
3087 return err < 0 ? err : change;
3089 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_put);
3092 * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control
3094 * The control element is supposed to have the private_value field
3095 * set up via HDA_BIND_VOL() macro.
3097 int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
3098 unsigned int size, unsigned int __user *tlv)
3100 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3101 struct hda_bind_ctls *c;
3104 mutex_lock(&codec->control_mutex);
3105 c = (struct hda_bind_ctls *)kcontrol->private_value;
3106 kcontrol->private_value = *c->values;
3107 err = c->ops->tlv(kcontrol, op_flag, size, tlv);
3108 kcontrol->private_value = (long)c;
3109 mutex_unlock(&codec->control_mutex);
3112 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_tlv);
3114 struct hda_ctl_ops snd_hda_bind_vol = {
3115 .info = snd_hda_mixer_amp_volume_info,
3116 .get = snd_hda_mixer_amp_volume_get,
3117 .put = snd_hda_mixer_amp_volume_put,
3118 .tlv = snd_hda_mixer_amp_tlv
3120 EXPORT_SYMBOL_HDA(snd_hda_bind_vol);
3122 struct hda_ctl_ops snd_hda_bind_sw = {
3123 .info = snd_hda_mixer_amp_switch_info,
3124 .get = snd_hda_mixer_amp_switch_get,
3125 .put = snd_hda_mixer_amp_switch_put,
3126 .tlv = snd_hda_mixer_amp_tlv
3128 EXPORT_SYMBOL_HDA(snd_hda_bind_sw);
3131 * SPDIF out controls
3134 static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
3135 struct snd_ctl_elem_info *uinfo)
3137 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
3142 static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
3143 struct snd_ctl_elem_value *ucontrol)
3145 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
3146 IEC958_AES0_NONAUDIO |
3147 IEC958_AES0_CON_EMPHASIS_5015 |
3148 IEC958_AES0_CON_NOT_COPYRIGHT;
3149 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
3150 IEC958_AES1_CON_ORIGINAL;
3154 static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
3155 struct snd_ctl_elem_value *ucontrol)
3157 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
3158 IEC958_AES0_NONAUDIO |
3159 IEC958_AES0_PRO_EMPHASIS_5015;
3163 static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
3164 struct snd_ctl_elem_value *ucontrol)
3166 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3167 int idx = kcontrol->private_value;
3168 struct hda_spdif_out *spdif;
3170 mutex_lock(&codec->spdif_mutex);
3171 spdif = snd_array_elem(&codec->spdif_out, idx);
3172 ucontrol->value.iec958.status[0] = spdif->status & 0xff;
3173 ucontrol->value.iec958.status[1] = (spdif->status >> 8) & 0xff;
3174 ucontrol->value.iec958.status[2] = (spdif->status >> 16) & 0xff;
3175 ucontrol->value.iec958.status[3] = (spdif->status >> 24) & 0xff;
3176 mutex_unlock(&codec->spdif_mutex);
3181 /* convert from SPDIF status bits to HDA SPDIF bits
3182 * bit 0 (DigEn) is always set zero (to be filled later)
3184 static unsigned short convert_from_spdif_status(unsigned int sbits)
3186 unsigned short val = 0;
3188 if (sbits & IEC958_AES0_PROFESSIONAL)
3189 val |= AC_DIG1_PROFESSIONAL;
3190 if (sbits & IEC958_AES0_NONAUDIO)
3191 val |= AC_DIG1_NONAUDIO;
3192 if (sbits & IEC958_AES0_PROFESSIONAL) {
3193 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
3194 IEC958_AES0_PRO_EMPHASIS_5015)
3195 val |= AC_DIG1_EMPHASIS;
3197 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
3198 IEC958_AES0_CON_EMPHASIS_5015)
3199 val |= AC_DIG1_EMPHASIS;
3200 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
3201 val |= AC_DIG1_COPYRIGHT;
3202 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
3203 val |= AC_DIG1_LEVEL;
3204 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
3209 /* convert to SPDIF status bits from HDA SPDIF bits
3211 static unsigned int convert_to_spdif_status(unsigned short val)
3213 unsigned int sbits = 0;
3215 if (val & AC_DIG1_NONAUDIO)
3216 sbits |= IEC958_AES0_NONAUDIO;
3217 if (val & AC_DIG1_PROFESSIONAL)
3218 sbits |= IEC958_AES0_PROFESSIONAL;
3219 if (sbits & IEC958_AES0_PROFESSIONAL) {
3220 if (val & AC_DIG1_EMPHASIS)
3221 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
3223 if (val & AC_DIG1_EMPHASIS)
3224 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
3225 if (!(val & AC_DIG1_COPYRIGHT))
3226 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
3227 if (val & AC_DIG1_LEVEL)
3228 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
3229 sbits |= val & (0x7f << 8);
3234 /* set digital convert verbs both for the given NID and its slaves */
3235 static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
3240 snd_hda_codec_write_cache(codec, nid, 0, verb, val);
3241 d = codec->slave_dig_outs;
3245 snd_hda_codec_write_cache(codec, *d, 0, verb, val);
3248 static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
3252 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1);
3254 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2);
3257 static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
3258 struct snd_ctl_elem_value *ucontrol)
3260 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3261 int idx = kcontrol->private_value;
3262 struct hda_spdif_out *spdif;
3267 mutex_lock(&codec->spdif_mutex);
3268 spdif = snd_array_elem(&codec->spdif_out, idx);
3270 spdif->status = ucontrol->value.iec958.status[0] |
3271 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
3272 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
3273 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
3274 val = convert_from_spdif_status(spdif->status);
3275 val |= spdif->ctls & 1;
3276 change = spdif->ctls != val;
3278 if (change && nid != (u16)-1)
3279 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
3280 mutex_unlock(&codec->spdif_mutex);
3284 #define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
3286 static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
3287 struct snd_ctl_elem_value *ucontrol)
3289 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3290 int idx = kcontrol->private_value;
3291 struct hda_spdif_out *spdif;
3293 mutex_lock(&codec->spdif_mutex);
3294 spdif = snd_array_elem(&codec->spdif_out, idx);
3295 ucontrol->value.integer.value[0] = spdif->ctls & AC_DIG1_ENABLE;
3296 mutex_unlock(&codec->spdif_mutex);
3300 static inline void set_spdif_ctls(struct hda_codec *codec, hda_nid_t nid,
3303 set_dig_out_convert(codec, nid, dig1, dig2);
3304 /* unmute amp switch (if any) */
3305 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
3306 (dig1 & AC_DIG1_ENABLE))
3307 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
3311 static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
3312 struct snd_ctl_elem_value *ucontrol)
3314 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3315 int idx = kcontrol->private_value;
3316 struct hda_spdif_out *spdif;
3321 mutex_lock(&codec->spdif_mutex);
3322 spdif = snd_array_elem(&codec->spdif_out, idx);
3324 val = spdif->ctls & ~AC_DIG1_ENABLE;
3325 if (ucontrol->value.integer.value[0])
3326 val |= AC_DIG1_ENABLE;
3327 change = spdif->ctls != val;
3329 if (change && nid != (u16)-1)
3330 set_spdif_ctls(codec, nid, val & 0xff, -1);
3331 mutex_unlock(&codec->spdif_mutex);
3335 static struct snd_kcontrol_new dig_mixes[] = {
3337 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3338 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3339 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
3340 .info = snd_hda_spdif_mask_info,
3341 .get = snd_hda_spdif_cmask_get,
3344 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3345 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3346 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK),
3347 .info = snd_hda_spdif_mask_info,
3348 .get = snd_hda_spdif_pmask_get,
3351 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3352 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
3353 .info = snd_hda_spdif_mask_info,
3354 .get = snd_hda_spdif_default_get,
3355 .put = snd_hda_spdif_default_put,
3358 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3359 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
3360 .info = snd_hda_spdif_out_switch_info,
3361 .get = snd_hda_spdif_out_switch_get,
3362 .put = snd_hda_spdif_out_switch_put,
3368 * snd_hda_create_dig_out_ctls - create Output SPDIF-related controls
3369 * @codec: the HDA codec
3370 * @associated_nid: NID that new ctls associated with
3371 * @cvt_nid: converter NID
3372 * @type: HDA_PCM_TYPE_*
3373 * Creates controls related with the digital output.
3374 * Called from each patch supporting the digital out.
3376 * Returns 0 if successful, or a negative error code.
3378 int snd_hda_create_dig_out_ctls(struct hda_codec *codec,
3379 hda_nid_t associated_nid,
3384 struct snd_kcontrol *kctl;
3385 struct snd_kcontrol_new *dig_mix;
3387 const int spdif_index = 16;
3388 struct hda_spdif_out *spdif;
3389 struct hda_bus *bus = codec->bus;
3391 if (bus->primary_dig_out_type == HDA_PCM_TYPE_HDMI &&
3392 type == HDA_PCM_TYPE_SPDIF) {
3394 } else if (bus->primary_dig_out_type == HDA_PCM_TYPE_SPDIF &&
3395 type == HDA_PCM_TYPE_HDMI) {
3396 /* suppose a single SPDIF device */
3397 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
3398 kctl = find_mixer_ctl(codec, dig_mix->name, 0, 0);
3401 kctl->id.index = spdif_index;
3403 bus->primary_dig_out_type = HDA_PCM_TYPE_HDMI;
3405 if (!bus->primary_dig_out_type)
3406 bus->primary_dig_out_type = type;
3408 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Playback Switch", idx);
3410 printk(KERN_ERR "hda_codec: too many IEC958 outputs\n");
3413 spdif = snd_array_new(&codec->spdif_out);
3416 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
3417 kctl = snd_ctl_new1(dig_mix, codec);
3420 kctl->id.index = idx;
3421 kctl->private_value = codec->spdif_out.used - 1;
3422 err = snd_hda_ctl_add(codec, associated_nid, kctl);
3426 spdif->nid = cvt_nid;
3427 spdif->ctls = snd_hda_codec_read(codec, cvt_nid, 0,
3428 AC_VERB_GET_DIGI_CONVERT_1, 0);
3429 spdif->status = convert_to_spdif_status(spdif->ctls);
3432 EXPORT_SYMBOL_HDA(snd_hda_create_dig_out_ctls);
3434 /* get the hda_spdif_out entry from the given NID
3435 * call within spdif_mutex lock
3437 struct hda_spdif_out *snd_hda_spdif_out_of_nid(struct hda_codec *codec,
3441 for (i = 0; i < codec->spdif_out.used; i++) {
3442 struct hda_spdif_out *spdif =
3443 snd_array_elem(&codec->spdif_out, i);
3444 if (spdif->nid == nid)
3449 EXPORT_SYMBOL_HDA(snd_hda_spdif_out_of_nid);
3451 void snd_hda_spdif_ctls_unassign(struct hda_codec *codec, int idx)
3453 struct hda_spdif_out *spdif;
3455 mutex_lock(&codec->spdif_mutex);
3456 spdif = snd_array_elem(&codec->spdif_out, idx);
3457 spdif->nid = (u16)-1;
3458 mutex_unlock(&codec->spdif_mutex);
3460 EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_unassign);
3462 void snd_hda_spdif_ctls_assign(struct hda_codec *codec, int idx, hda_nid_t nid)
3464 struct hda_spdif_out *spdif;
3467 mutex_lock(&codec->spdif_mutex);
3468 spdif = snd_array_elem(&codec->spdif_out, idx);
3469 if (spdif->nid != nid) {
3472 set_spdif_ctls(codec, nid, val & 0xff, (val >> 8) & 0xff);
3474 mutex_unlock(&codec->spdif_mutex);
3476 EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_assign);
3479 * SPDIF sharing with analog output
3481 static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
3482 struct snd_ctl_elem_value *ucontrol)
3484 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3485 ucontrol->value.integer.value[0] = mout->share_spdif;
3489 static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
3490 struct snd_ctl_elem_value *ucontrol)
3492 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3493 mout->share_spdif = !!ucontrol->value.integer.value[0];
3497 static struct snd_kcontrol_new spdif_share_sw = {
3498 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3499 .name = "IEC958 Default PCM Playback Switch",
3500 .info = snd_ctl_boolean_mono_info,
3501 .get = spdif_share_sw_get,
3502 .put = spdif_share_sw_put,
3506 * snd_hda_create_spdif_share_sw - create Default PCM switch
3507 * @codec: the HDA codec
3508 * @mout: multi-out instance
3510 int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
3511 struct hda_multi_out *mout)
3513 struct snd_kcontrol *kctl;
3515 if (!mout->dig_out_nid)
3518 kctl = snd_ctl_new1(&spdif_share_sw, mout);
3521 /* ATTENTION: here mout is passed as private_data, instead of codec */
3522 return snd_hda_ctl_add(codec, mout->dig_out_nid, kctl);
3524 EXPORT_SYMBOL_HDA(snd_hda_create_spdif_share_sw);
3530 #define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
3532 static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
3533 struct snd_ctl_elem_value *ucontrol)
3535 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3537 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
3541 static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
3542 struct snd_ctl_elem_value *ucontrol)
3544 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3545 hda_nid_t nid = kcontrol->private_value;
3546 unsigned int val = !!ucontrol->value.integer.value[0];
3549 mutex_lock(&codec->spdif_mutex);
3550 change = codec->spdif_in_enable != val;
3552 codec->spdif_in_enable = val;
3553 snd_hda_codec_write_cache(codec, nid, 0,
3554 AC_VERB_SET_DIGI_CONVERT_1, val);
3556 mutex_unlock(&codec->spdif_mutex);
3560 static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
3561 struct snd_ctl_elem_value *ucontrol)
3563 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3564 hda_nid_t nid = kcontrol->private_value;
3568 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0);
3569 sbits = convert_to_spdif_status(val);
3570 ucontrol->value.iec958.status[0] = sbits;
3571 ucontrol->value.iec958.status[1] = sbits >> 8;
3572 ucontrol->value.iec958.status[2] = sbits >> 16;
3573 ucontrol->value.iec958.status[3] = sbits >> 24;
3577 static struct snd_kcontrol_new dig_in_ctls[] = {
3579 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3580 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH),
3581 .info = snd_hda_spdif_in_switch_info,
3582 .get = snd_hda_spdif_in_switch_get,
3583 .put = snd_hda_spdif_in_switch_put,
3586 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3587 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3588 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
3589 .info = snd_hda_spdif_mask_info,
3590 .get = snd_hda_spdif_in_status_get,
3596 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
3597 * @codec: the HDA codec
3598 * @nid: audio in widget NID
3600 * Creates controls related with the SPDIF input.
3601 * Called from each patch supporting the SPDIF in.
3603 * Returns 0 if successful, or a negative error code.
3605 int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
3608 struct snd_kcontrol *kctl;
3609 struct snd_kcontrol_new *dig_mix;
3612 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Capture Switch", 0);
3614 printk(KERN_ERR "hda_codec: too many IEC958 inputs\n");
3617 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
3618 kctl = snd_ctl_new1(dig_mix, codec);
3621 kctl->private_value = nid;
3622 err = snd_hda_ctl_add(codec, nid, kctl);
3626 codec->spdif_in_enable =
3627 snd_hda_codec_read(codec, nid, 0,
3628 AC_VERB_GET_DIGI_CONVERT_1, 0) &
3632 EXPORT_SYMBOL_HDA(snd_hda_create_spdif_in_ctls);
3638 /* build a 31bit cache key with the widget id and the command parameter */
3639 #define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
3640 #define get_cmd_cache_nid(key) ((key) & 0xff)
3641 #define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
3644 * snd_hda_codec_write_cache - send a single command with caching
3645 * @codec: the HDA codec
3646 * @nid: NID to send the command
3647 * @flags: optional bit flags
3648 * @verb: the verb to send
3649 * @parm: the parameter for the verb
3651 * Send a single command without waiting for response.
3653 * Returns 0 if successful, or a negative error code.
3655 int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
3656 int flags, unsigned int verb, unsigned int parm)
3659 struct hda_cache_head *c;
3661 unsigned int cache_only;
3663 cache_only = codec->cached_write;
3665 err = snd_hda_codec_write(codec, nid, flags, verb, parm);
3670 /* parm may contain the verb stuff for get/set amp */
3671 verb = verb | (parm >> 8);
3673 key = build_cmd_cache_key(nid, verb);
3674 mutex_lock(&codec->bus->cmd_mutex);
3675 c = get_alloc_hash(&codec->cmd_cache, key);
3678 c->dirty = cache_only;
3680 mutex_unlock(&codec->bus->cmd_mutex);
3683 EXPORT_SYMBOL_HDA(snd_hda_codec_write_cache);
3686 * snd_hda_codec_update_cache - check cache and write the cmd only when needed
3687 * @codec: the HDA codec
3688 * @nid: NID to send the command
3689 * @flags: optional bit flags
3690 * @verb: the verb to send
3691 * @parm: the parameter for the verb
3693 * This function works like snd_hda_codec_write_cache(), but it doesn't send
3694 * command if the parameter is already identical with the cached value.
3695 * If not, it sends the command and refreshes the cache.
3697 * Returns 0 if successful, or a negative error code.
3699 int snd_hda_codec_update_cache(struct hda_codec *codec, hda_nid_t nid,
3700 int flags, unsigned int verb, unsigned int parm)
3702 struct hda_cache_head *c;
3705 /* parm may contain the verb stuff for get/set amp */
3706 verb = verb | (parm >> 8);
3708 key = build_cmd_cache_key(nid, verb);
3709 mutex_lock(&codec->bus->cmd_mutex);
3710 c = get_hash(&codec->cmd_cache, key);
3711 if (c && c->val == parm) {
3712 mutex_unlock(&codec->bus->cmd_mutex);
3715 mutex_unlock(&codec->bus->cmd_mutex);
3716 return snd_hda_codec_write_cache(codec, nid, flags, verb, parm);
3718 EXPORT_SYMBOL_HDA(snd_hda_codec_update_cache);
3721 * snd_hda_codec_resume_cache - Resume the all commands from the cache
3722 * @codec: HD-audio codec
3724 * Execute all verbs recorded in the command caches to resume.
3726 void snd_hda_codec_resume_cache(struct hda_codec *codec)
3730 mutex_lock(&codec->hash_mutex);
3731 codec->cached_write = 0;
3732 for (i = 0; i < codec->cmd_cache.buf.used; i++) {
3733 struct hda_cache_head *buffer;
3736 buffer = snd_array_elem(&codec->cmd_cache.buf, i);
3743 mutex_unlock(&codec->hash_mutex);
3744 snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
3745 get_cmd_cache_cmd(key), buffer->val);
3746 mutex_lock(&codec->hash_mutex);
3748 mutex_unlock(&codec->hash_mutex);
3750 EXPORT_SYMBOL_HDA(snd_hda_codec_resume_cache);
3753 * snd_hda_sequence_write_cache - sequence writes with caching
3754 * @codec: the HDA codec
3755 * @seq: VERB array to send
3757 * Send the commands sequentially from the given array.
3758 * Thte commands are recorded on cache for power-save and resume.
3759 * The array must be terminated with NID=0.
3761 void snd_hda_sequence_write_cache(struct hda_codec *codec,
3762 const struct hda_verb *seq)
3764 for (; seq->nid; seq++)
3765 snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
3768 EXPORT_SYMBOL_HDA(snd_hda_sequence_write_cache);
3771 * snd_hda_codec_flush_cache - Execute all pending (cached) amps / verbs
3772 * @codec: HD-audio codec
3774 void snd_hda_codec_flush_cache(struct hda_codec *codec)
3776 snd_hda_codec_resume_amp(codec);
3777 snd_hda_codec_resume_cache(codec);
3779 EXPORT_SYMBOL_HDA(snd_hda_codec_flush_cache);
3781 void snd_hda_codec_set_power_to_all(struct hda_codec *codec, hda_nid_t fg,
3782 unsigned int power_state)
3784 hda_nid_t nid = codec->start_nid;
3787 for (i = 0; i < codec->num_nodes; i++, nid++) {
3788 unsigned int wcaps = get_wcaps(codec, nid);
3789 unsigned int state = power_state;
3790 if (!(wcaps & AC_WCAP_POWER))
3792 if (codec->power_filter) {
3793 state = codec->power_filter(codec, nid, power_state);
3794 if (state != power_state && power_state == AC_PWRST_D3)
3797 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE,
3801 EXPORT_SYMBOL_HDA(snd_hda_codec_set_power_to_all);
3804 * supported power states check
3806 static bool snd_hda_codec_get_supported_ps(struct hda_codec *codec, hda_nid_t fg,
3807 unsigned int power_state)
3809 int sup = snd_hda_param_read(codec, fg, AC_PAR_POWER_STATE);
3813 if (sup & power_state)
3820 * wait until the state is reached, returns the current state
3822 static unsigned int hda_sync_power_state(struct hda_codec *codec,
3824 unsigned int power_state)
3826 unsigned long end_time = jiffies + msecs_to_jiffies(500);
3827 unsigned int state, actual_state;
3830 state = snd_hda_codec_read(codec, fg, 0,
3831 AC_VERB_GET_POWER_STATE, 0);
3832 if (state & AC_PWRST_ERROR)
3834 actual_state = (state >> 4) & 0x0f;
3835 if (actual_state == power_state)
3837 if (time_after_eq(jiffies, end_time))
3839 /* wait until the codec reachs to the target state */
3845 /* don't power down the widget if it controls eapd and EAPD_BTLENABLE is set */
3846 unsigned int snd_hda_codec_eapd_power_filter(struct hda_codec *codec,
3848 unsigned int power_state)
3850 if (power_state == AC_PWRST_D3 &&
3851 get_wcaps_type(get_wcaps(codec, nid)) == AC_WID_PIN &&
3852 (snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD)) {
3853 int eapd = snd_hda_codec_read(codec, nid, 0,
3854 AC_VERB_GET_EAPD_BTLENABLE, 0);
3860 EXPORT_SYMBOL_HDA(snd_hda_codec_eapd_power_filter);
3863 * set power state of the codec, and return the power state
3865 static unsigned int hda_set_power_state(struct hda_codec *codec,
3866 unsigned int power_state)
3868 hda_nid_t fg = codec->afg ? codec->afg : codec->mfg;
3873 /* this delay seems necessary to avoid click noise at power-down */
3874 if (power_state == AC_PWRST_D3) {
3875 /* transition time less than 10ms for power down */
3876 msleep(codec->epss ? 10 : 100);
3877 flags = HDA_RW_NO_RESPONSE_FALLBACK;
3880 /* repeat power states setting at most 10 times*/
3881 for (count = 0; count < 10; count++) {
3882 if (codec->patch_ops.set_power_state)
3883 codec->patch_ops.set_power_state(codec, fg,
3886 snd_hda_codec_read(codec, fg, flags,
3887 AC_VERB_SET_POWER_STATE,
3889 snd_hda_codec_set_power_to_all(codec, fg, power_state);
3891 state = hda_sync_power_state(codec, fg, power_state);
3892 if (!(state & AC_PWRST_ERROR))
3899 /* sync power states of all widgets;
3900 * this is called at the end of codec parsing
3902 static void sync_power_up_states(struct hda_codec *codec)
3904 hda_nid_t nid = codec->start_nid;
3907 /* don't care if no filter is used */
3908 if (!codec->power_filter)
3911 for (i = 0; i < codec->num_nodes; i++, nid++) {
3912 unsigned int wcaps = get_wcaps(codec, nid);
3913 unsigned int target;
3914 if (!(wcaps & AC_WCAP_POWER))
3916 target = codec->power_filter(codec, nid, AC_PWRST_D0);
3917 if (target == AC_PWRST_D0)
3919 if (!snd_hda_check_power_state(codec, nid, target))
3920 snd_hda_codec_write(codec, nid, 0,
3921 AC_VERB_SET_POWER_STATE, target);
3925 #ifdef CONFIG_SND_HDA_HWDEP
3926 /* execute additional init verbs */
3927 static void hda_exec_init_verbs(struct hda_codec *codec)
3929 if (codec->init_verbs.list)
3930 snd_hda_sequence_write(codec, codec->init_verbs.list);
3933 static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
3938 * call suspend and power-down; used both from PM and power-save
3939 * this function returns the power state in the end
3941 static unsigned int hda_call_codec_suspend(struct hda_codec *codec, bool in_wq)
3947 if (codec->patch_ops.suspend)
3948 codec->patch_ops.suspend(codec);
3949 hda_cleanup_all_streams(codec);
3950 state = hda_set_power_state(codec, AC_PWRST_D3);
3951 /* Cancel delayed work if we aren't currently running from it. */
3953 cancel_delayed_work_sync(&codec->power_work);
3954 spin_lock(&codec->power_lock);
3955 snd_hda_update_power_acct(codec);
3956 trace_hda_power_down(codec);
3957 codec->power_on = 0;
3958 codec->power_transition = 0;
3959 codec->power_jiffies = jiffies;
3960 spin_unlock(&codec->power_lock);
3965 /* mark all entries of cmd and amp caches dirty */
3966 static void hda_mark_cmd_cache_dirty(struct hda_codec *codec)
3969 for (i = 0; i < codec->cmd_cache.buf.used; i++) {
3970 struct hda_cache_head *cmd;
3971 cmd = snd_array_elem(&codec->cmd_cache.buf, i);
3974 for (i = 0; i < codec->amp_cache.buf.used; i++) {
3975 struct hda_amp_info *amp;
3976 amp = snd_array_elem(&codec->amp_cache.buf, i);
3977 amp->head.dirty = 1;
3982 * kick up codec; used both from PM and power-save
3984 static void hda_call_codec_resume(struct hda_codec *codec)
3988 hda_mark_cmd_cache_dirty(codec);
3990 /* set as if powered on for avoiding re-entering the resume
3991 * in the resume / power-save sequence
3993 hda_keep_power_on(codec);
3994 hda_set_power_state(codec, AC_PWRST_D0);
3995 restore_shutup_pins(codec);
3996 hda_exec_init_verbs(codec);
3997 snd_hda_jack_set_dirty_all(codec);
3998 if (codec->patch_ops.resume)
3999 codec->patch_ops.resume(codec);
4001 if (codec->patch_ops.init)
4002 codec->patch_ops.init(codec);
4003 snd_hda_codec_resume_amp(codec);
4004 snd_hda_codec_resume_cache(codec);
4007 if (codec->jackpoll_interval)
4008 hda_jackpoll_work(&codec->jackpoll_work.work);
4010 snd_hda_jack_report_sync(codec);
4013 snd_hda_power_down(codec); /* flag down before returning */
4015 #endif /* CONFIG_PM */
4019 * snd_hda_build_controls - build mixer controls
4022 * Creates mixer controls for each codec included in the bus.
4024 * Returns 0 if successful, otherwise a negative error code.
4026 int snd_hda_build_controls(struct hda_bus *bus)
4028 struct hda_codec *codec;
4030 list_for_each_entry(codec, &bus->codec_list, list) {
4031 int err = snd_hda_codec_build_controls(codec);
4033 printk(KERN_ERR "hda_codec: cannot build controls "
4034 "for #%d (error %d)\n", codec->addr, err);
4035 err = snd_hda_codec_reset(codec);
4038 "hda_codec: cannot revert codec\n");
4045 EXPORT_SYMBOL_HDA(snd_hda_build_controls);
4048 * add standard channel maps if not specified
4050 static int add_std_chmaps(struct hda_codec *codec)
4054 for (i = 0; i < codec->num_pcms; i++) {
4055 for (str = 0; str < 2; str++) {
4056 struct snd_pcm *pcm = codec->pcm_info[i].pcm;
4057 struct hda_pcm_stream *hinfo =
4058 &codec->pcm_info[i].stream[str];
4059 struct snd_pcm_chmap *chmap;
4060 const struct snd_pcm_chmap_elem *elem;
4062 if (codec->pcm_info[i].own_chmap)
4064 if (!pcm || !hinfo->substreams)
4066 elem = hinfo->chmap ? hinfo->chmap : snd_pcm_std_chmaps;
4067 err = snd_pcm_add_chmap_ctls(pcm, str, elem,
4068 hinfo->channels_max,
4072 chmap->channel_mask = SND_PCM_CHMAP_MASK_2468;
4078 /* default channel maps for 2.1 speakers;
4079 * since HD-audio supports only stereo, odd number channels are omitted
4081 const struct snd_pcm_chmap_elem snd_pcm_2_1_chmaps[] = {
4083 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } },
4085 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
4086 SNDRV_CHMAP_LFE, SNDRV_CHMAP_LFE } },
4089 EXPORT_SYMBOL_GPL(snd_pcm_2_1_chmaps);
4091 int snd_hda_codec_build_controls(struct hda_codec *codec)
4094 hda_exec_init_verbs(codec);
4095 /* continue to initialize... */
4096 if (codec->patch_ops.init)
4097 err = codec->patch_ops.init(codec);
4098 if (!err && codec->patch_ops.build_controls)
4099 err = codec->patch_ops.build_controls(codec);
4103 /* we create chmaps here instead of build_pcms */
4104 err = add_std_chmaps(codec);
4108 if (codec->jackpoll_interval)
4109 hda_jackpoll_work(&codec->jackpoll_work.work);
4111 snd_hda_jack_report_sync(codec); /* call at the last init point */
4112 sync_power_up_states(codec);
4119 struct hda_rate_tbl {
4121 unsigned int alsa_bits;
4122 unsigned int hda_fmt;
4125 /* rate = base * mult / div */
4126 #define HDA_RATE(base, mult, div) \
4127 (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \
4128 (((div) - 1) << AC_FMT_DIV_SHIFT))
4130 static struct hda_rate_tbl rate_bits[] = {
4131 /* rate in Hz, ALSA rate bitmask, HDA format value */
4133 /* autodetected value used in snd_hda_query_supported_pcm */
4134 { 8000, SNDRV_PCM_RATE_8000, HDA_RATE(48, 1, 6) },
4135 { 11025, SNDRV_PCM_RATE_11025, HDA_RATE(44, 1, 4) },
4136 { 16000, SNDRV_PCM_RATE_16000, HDA_RATE(48, 1, 3) },
4137 { 22050, SNDRV_PCM_RATE_22050, HDA_RATE(44, 1, 2) },
4138 { 32000, SNDRV_PCM_RATE_32000, HDA_RATE(48, 2, 3) },
4139 { 44100, SNDRV_PCM_RATE_44100, HDA_RATE(44, 1, 1) },
4140 { 48000, SNDRV_PCM_RATE_48000, HDA_RATE(48, 1, 1) },
4141 { 88200, SNDRV_PCM_RATE_88200, HDA_RATE(44, 2, 1) },
4142 { 96000, SNDRV_PCM_RATE_96000, HDA_RATE(48, 2, 1) },
4143 { 176400, SNDRV_PCM_RATE_176400, HDA_RATE(44, 4, 1) },
4144 { 192000, SNDRV_PCM_RATE_192000, HDA_RATE(48, 4, 1) },
4145 #define AC_PAR_PCM_RATE_BITS 11
4146 /* up to bits 10, 384kHZ isn't supported properly */
4148 /* not autodetected value */
4149 { 9600, SNDRV_PCM_RATE_KNOT, HDA_RATE(48, 1, 5) },
4151 { 0 } /* terminator */
4155 * snd_hda_calc_stream_format - calculate format bitset
4156 * @rate: the sample rate
4157 * @channels: the number of channels
4158 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
4159 * @maxbps: the max. bps
4161 * Calculate the format bitset from the given rate, channels and th PCM format.
4163 * Return zero if invalid.
4165 unsigned int snd_hda_calc_stream_format(unsigned int rate,
4166 unsigned int channels,
4167 unsigned int format,
4168 unsigned int maxbps,
4169 unsigned short spdif_ctls)
4172 unsigned int val = 0;
4174 for (i = 0; rate_bits[i].hz; i++)
4175 if (rate_bits[i].hz == rate) {
4176 val = rate_bits[i].hda_fmt;
4179 if (!rate_bits[i].hz) {
4180 snd_printdd("invalid rate %d\n", rate);
4184 if (channels == 0 || channels > 8) {
4185 snd_printdd("invalid channels %d\n", channels);
4188 val |= channels - 1;
4190 switch (snd_pcm_format_width(format)) {
4192 val |= AC_FMT_BITS_8;
4195 val |= AC_FMT_BITS_16;
4200 if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE)
4201 val |= AC_FMT_BITS_32;
4202 else if (maxbps >= 24)
4203 val |= AC_FMT_BITS_24;
4205 val |= AC_FMT_BITS_20;
4208 snd_printdd("invalid format width %d\n",
4209 snd_pcm_format_width(format));
4213 if (spdif_ctls & AC_DIG1_NONAUDIO)
4214 val |= AC_FMT_TYPE_NON_PCM;
4218 EXPORT_SYMBOL_HDA(snd_hda_calc_stream_format);
4220 static unsigned int get_pcm_param(struct hda_codec *codec, hda_nid_t nid,
4223 unsigned int val = 0;
4224 if (nid != codec->afg &&
4225 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD))
4226 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
4227 if (!val || val == -1)
4228 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
4229 if (!val || val == -1)
4234 static unsigned int query_pcm_param(struct hda_codec *codec, hda_nid_t nid)
4236 return query_caps_hash(codec, nid, 0, HDA_HASH_PARPCM_KEY(nid),
4240 static unsigned int get_stream_param(struct hda_codec *codec, hda_nid_t nid,
4243 unsigned int streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
4244 if (!streams || streams == -1)
4245 streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
4246 if (!streams || streams == -1)
4251 static unsigned int query_stream_param(struct hda_codec *codec, hda_nid_t nid)
4253 return query_caps_hash(codec, nid, 0, HDA_HASH_PARSTR_KEY(nid),
4258 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
4259 * @codec: the HDA codec
4260 * @nid: NID to query
4261 * @ratesp: the pointer to store the detected rate bitflags
4262 * @formatsp: the pointer to store the detected formats
4263 * @bpsp: the pointer to store the detected format widths
4265 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
4266 * or @bsps argument is ignored.
4268 * Returns 0 if successful, otherwise a negative error code.
4270 int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
4271 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
4273 unsigned int i, val, wcaps;
4275 wcaps = get_wcaps(codec, nid);
4276 val = query_pcm_param(codec, nid);
4280 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
4282 rates |= rate_bits[i].alsa_bits;
4285 snd_printk(KERN_ERR "hda_codec: rates == 0 "
4286 "(nid=0x%x, val=0x%x, ovrd=%i)\n",
4288 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0);
4294 if (formatsp || bpsp) {
4296 unsigned int streams, bps;
4298 streams = query_stream_param(codec, nid);
4303 if (streams & AC_SUPFMT_PCM) {
4304 if (val & AC_SUPPCM_BITS_8) {
4305 formats |= SNDRV_PCM_FMTBIT_U8;
4308 if (val & AC_SUPPCM_BITS_16) {
4309 formats |= SNDRV_PCM_FMTBIT_S16_LE;
4312 if (wcaps & AC_WCAP_DIGITAL) {
4313 if (val & AC_SUPPCM_BITS_32)
4314 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
4315 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
4316 formats |= SNDRV_PCM_FMTBIT_S32_LE;
4317 if (val & AC_SUPPCM_BITS_24)
4319 else if (val & AC_SUPPCM_BITS_20)
4321 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
4322 AC_SUPPCM_BITS_32)) {
4323 formats |= SNDRV_PCM_FMTBIT_S32_LE;
4324 if (val & AC_SUPPCM_BITS_32)
4326 else if (val & AC_SUPPCM_BITS_24)
4328 else if (val & AC_SUPPCM_BITS_20)
4332 #if 0 /* FIXME: CS4206 doesn't work, which is the only codec supporting float */
4333 if (streams & AC_SUPFMT_FLOAT32) {
4334 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
4339 if (streams == AC_SUPFMT_AC3) {
4340 /* should be exclusive */
4341 /* temporary hack: we have still no proper support
4342 * for the direct AC3 stream...
4344 formats |= SNDRV_PCM_FMTBIT_U8;
4348 snd_printk(KERN_ERR "hda_codec: formats == 0 "
4349 "(nid=0x%x, val=0x%x, ovrd=%i, "
4352 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0,
4357 *formatsp = formats;
4364 EXPORT_SYMBOL_HDA(snd_hda_query_supported_pcm);
4367 * snd_hda_is_supported_format - Check the validity of the format
4368 * @codec: HD-audio codec
4369 * @nid: NID to check
4370 * @format: the HD-audio format value to check
4372 * Check whether the given node supports the format value.
4374 * Returns 1 if supported, 0 if not.
4376 int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
4377 unsigned int format)
4380 unsigned int val = 0, rate, stream;
4382 val = query_pcm_param(codec, nid);
4386 rate = format & 0xff00;
4387 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
4388 if (rate_bits[i].hda_fmt == rate) {
4393 if (i >= AC_PAR_PCM_RATE_BITS)
4396 stream = query_stream_param(codec, nid);
4400 if (stream & AC_SUPFMT_PCM) {
4401 switch (format & 0xf0) {
4403 if (!(val & AC_SUPPCM_BITS_8))
4407 if (!(val & AC_SUPPCM_BITS_16))
4411 if (!(val & AC_SUPPCM_BITS_20))
4415 if (!(val & AC_SUPPCM_BITS_24))
4419 if (!(val & AC_SUPPCM_BITS_32))
4426 /* FIXME: check for float32 and AC3? */
4431 EXPORT_SYMBOL_HDA(snd_hda_is_supported_format);
4436 static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
4437 struct hda_codec *codec,
4438 struct snd_pcm_substream *substream)
4443 static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
4444 struct hda_codec *codec,
4445 unsigned int stream_tag,
4446 unsigned int format,
4447 struct snd_pcm_substream *substream)
4449 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4453 static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
4454 struct hda_codec *codec,
4455 struct snd_pcm_substream *substream)
4457 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
4461 static int set_pcm_default_values(struct hda_codec *codec,
4462 struct hda_pcm_stream *info)
4466 /* query support PCM information from the given NID */
4467 if (info->nid && (!info->rates || !info->formats)) {
4468 err = snd_hda_query_supported_pcm(codec, info->nid,
4469 info->rates ? NULL : &info->rates,
4470 info->formats ? NULL : &info->formats,
4471 info->maxbps ? NULL : &info->maxbps);
4475 if (info->ops.open == NULL)
4476 info->ops.open = hda_pcm_default_open_close;
4477 if (info->ops.close == NULL)
4478 info->ops.close = hda_pcm_default_open_close;
4479 if (info->ops.prepare == NULL) {
4480 if (snd_BUG_ON(!info->nid))
4482 info->ops.prepare = hda_pcm_default_prepare;
4484 if (info->ops.cleanup == NULL) {
4485 if (snd_BUG_ON(!info->nid))
4487 info->ops.cleanup = hda_pcm_default_cleanup;
4493 * codec prepare/cleanup entries
4495 int snd_hda_codec_prepare(struct hda_codec *codec,
4496 struct hda_pcm_stream *hinfo,
4497 unsigned int stream,
4498 unsigned int format,
4499 struct snd_pcm_substream *substream)
4502 mutex_lock(&codec->bus->prepare_mutex);
4503 ret = hinfo->ops.prepare(hinfo, codec, stream, format, substream);
4505 purify_inactive_streams(codec);
4506 mutex_unlock(&codec->bus->prepare_mutex);
4509 EXPORT_SYMBOL_HDA(snd_hda_codec_prepare);
4511 void snd_hda_codec_cleanup(struct hda_codec *codec,
4512 struct hda_pcm_stream *hinfo,
4513 struct snd_pcm_substream *substream)
4515 mutex_lock(&codec->bus->prepare_mutex);
4516 hinfo->ops.cleanup(hinfo, codec, substream);
4517 mutex_unlock(&codec->bus->prepare_mutex);
4519 EXPORT_SYMBOL_HDA(snd_hda_codec_cleanup);
4522 const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = {
4523 "Audio", "SPDIF", "HDMI", "Modem"
4527 * get the empty PCM device number to assign
4529 static int get_empty_pcm_device(struct hda_bus *bus, unsigned int type)
4531 /* audio device indices; not linear to keep compatibility */
4532 /* assigned to static slots up to dev#10; if more needed, assign
4533 * the later slot dynamically (when CONFIG_SND_DYNAMIC_MINORS=y)
4535 static int audio_idx[HDA_PCM_NTYPES][5] = {
4536 [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 },
4537 [HDA_PCM_TYPE_SPDIF] = { 1, -1 },
4538 [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 },
4539 [HDA_PCM_TYPE_MODEM] = { 6, -1 },
4543 if (type >= HDA_PCM_NTYPES) {
4544 snd_printk(KERN_WARNING "Invalid PCM type %d\n", type);
4548 for (i = 0; audio_idx[type][i] >= 0; i++) {
4549 #ifndef CONFIG_SND_DYNAMIC_MINORS
4550 if (audio_idx[type][i] >= 8)
4553 if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits))
4554 return audio_idx[type][i];
4557 #ifdef CONFIG_SND_DYNAMIC_MINORS
4558 /* non-fixed slots starting from 10 */
4559 for (i = 10; i < 32; i++) {
4560 if (!test_and_set_bit(i, bus->pcm_dev_bits))
4565 snd_printk(KERN_WARNING "Too many %s devices\n",
4566 snd_hda_pcm_type_name[type]);
4567 #ifndef CONFIG_SND_DYNAMIC_MINORS
4568 snd_printk(KERN_WARNING "Consider building the kernel with CONFIG_SND_DYNAMIC_MINORS=y\n");
4574 * attach a new PCM stream
4576 static int snd_hda_attach_pcm(struct hda_codec *codec, struct hda_pcm *pcm)
4578 struct hda_bus *bus = codec->bus;
4579 struct hda_pcm_stream *info;
4582 if (snd_BUG_ON(!pcm->name))
4584 for (stream = 0; stream < 2; stream++) {
4585 info = &pcm->stream[stream];
4586 if (info->substreams) {
4587 err = set_pcm_default_values(codec, info);
4592 return bus->ops.attach_pcm(bus, codec, pcm);
4595 /* assign all PCMs of the given codec */
4596 int snd_hda_codec_build_pcms(struct hda_codec *codec)
4601 if (!codec->num_pcms) {
4602 if (!codec->patch_ops.build_pcms)
4604 err = codec->patch_ops.build_pcms(codec);
4606 printk(KERN_ERR "hda_codec: cannot build PCMs"
4607 "for #%d (error %d)\n", codec->addr, err);
4608 err = snd_hda_codec_reset(codec);
4611 "hda_codec: cannot revert codec\n");
4616 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
4617 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
4620 if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
4621 continue; /* no substreams assigned */
4624 dev = get_empty_pcm_device(codec->bus, cpcm->pcm_type);
4626 continue; /* no fatal error */
4628 err = snd_hda_attach_pcm(codec, cpcm);
4630 printk(KERN_ERR "hda_codec: cannot attach "
4631 "PCM stream %d for codec #%d\n",
4633 continue; /* no fatal error */
4641 * snd_hda_build_pcms - build PCM information
4644 * Create PCM information for each codec included in the bus.
4646 * The build_pcms codec patch is requested to set up codec->num_pcms and
4647 * codec->pcm_info properly. The array is referred by the top-level driver
4648 * to create its PCM instances.
4649 * The allocated codec->pcm_info should be released in codec->patch_ops.free
4652 * At least, substreams, channels_min and channels_max must be filled for
4653 * each stream. substreams = 0 indicates that the stream doesn't exist.
4654 * When rates and/or formats are zero, the supported values are queried
4655 * from the given nid. The nid is used also by the default ops.prepare
4656 * and ops.cleanup callbacks.
4658 * The driver needs to call ops.open in its open callback. Similarly,
4659 * ops.close is supposed to be called in the close callback.
4660 * ops.prepare should be called in the prepare or hw_params callback
4661 * with the proper parameters for set up.
4662 * ops.cleanup should be called in hw_free for clean up of streams.
4664 * This function returns 0 if successful, or a negative error code.
4666 int snd_hda_build_pcms(struct hda_bus *bus)
4668 struct hda_codec *codec;
4670 list_for_each_entry(codec, &bus->codec_list, list) {
4671 int err = snd_hda_codec_build_pcms(codec);
4677 EXPORT_SYMBOL_HDA(snd_hda_build_pcms);
4680 * snd_hda_check_board_config - compare the current codec with the config table
4681 * @codec: the HDA codec
4682 * @num_configs: number of config enums
4683 * @models: array of model name strings
4684 * @tbl: configuration table, terminated by null entries
4686 * Compares the modelname or PCI subsystem id of the current codec with the
4687 * given configuration table. If a matching entry is found, returns its
4688 * config value (supposed to be 0 or positive).
4690 * If no entries are matching, the function returns a negative value.
4692 int snd_hda_check_board_config(struct hda_codec *codec,
4693 int num_configs, const char * const *models,
4694 const struct snd_pci_quirk *tbl)
4696 if (codec->modelname && models) {
4698 for (i = 0; i < num_configs; i++) {
4700 !strcmp(codec->modelname, models[i])) {
4701 snd_printd(KERN_INFO "hda_codec: model '%s' is "
4702 "selected\n", models[i]);
4708 if (!codec->bus->pci || !tbl)
4711 tbl = snd_pci_quirk_lookup(codec->bus->pci, tbl);
4714 if (tbl->value >= 0 && tbl->value < num_configs) {
4715 #ifdef CONFIG_SND_DEBUG_VERBOSE
4717 const char *model = NULL;
4719 model = models[tbl->value];
4721 sprintf(tmp, "#%d", tbl->value);
4724 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
4725 "for config %x:%x (%s)\n",
4726 model, tbl->subvendor, tbl->subdevice,
4727 (tbl->name ? tbl->name : "Unknown device"));
4733 EXPORT_SYMBOL_HDA(snd_hda_check_board_config);
4736 * snd_hda_check_board_codec_sid_config - compare the current codec
4737 subsystem ID with the
4740 This is important for Gateway notebooks with SB450 HDA Audio
4741 where the vendor ID of the PCI device is:
4742 ATI Technologies Inc SB450 HDA Audio [1002:437b]
4743 and the vendor/subvendor are found only at the codec.
4745 * @codec: the HDA codec
4746 * @num_configs: number of config enums
4747 * @models: array of model name strings
4748 * @tbl: configuration table, terminated by null entries
4750 * Compares the modelname or PCI subsystem id of the current codec with the
4751 * given configuration table. If a matching entry is found, returns its
4752 * config value (supposed to be 0 or positive).
4754 * If no entries are matching, the function returns a negative value.
4756 int snd_hda_check_board_codec_sid_config(struct hda_codec *codec,
4757 int num_configs, const char * const *models,
4758 const struct snd_pci_quirk *tbl)
4760 const struct snd_pci_quirk *q;
4762 /* Search for codec ID */
4763 for (q = tbl; q->subvendor; q++) {
4764 unsigned int mask = 0xffff0000 | q->subdevice_mask;
4765 unsigned int id = (q->subdevice | (q->subvendor << 16)) & mask;
4766 if ((codec->subsystem_id & mask) == id)
4775 if (tbl->value >= 0 && tbl->value < num_configs) {
4776 #ifdef CONFIG_SND_DEBUG_VERBOSE
4778 const char *model = NULL;
4780 model = models[tbl->value];
4782 sprintf(tmp, "#%d", tbl->value);
4785 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
4786 "for config %x:%x (%s)\n",
4787 model, tbl->subvendor, tbl->subdevice,
4788 (tbl->name ? tbl->name : "Unknown device"));
4794 EXPORT_SYMBOL_HDA(snd_hda_check_board_codec_sid_config);
4797 * snd_hda_add_new_ctls - create controls from the array
4798 * @codec: the HDA codec
4799 * @knew: the array of struct snd_kcontrol_new
4801 * This helper function creates and add new controls in the given array.
4802 * The array must be terminated with an empty entry as terminator.
4804 * Returns 0 if successful, or a negative error code.
4806 int snd_hda_add_new_ctls(struct hda_codec *codec,
4807 const struct snd_kcontrol_new *knew)
4811 for (; knew->name; knew++) {
4812 struct snd_kcontrol *kctl;
4813 int addr = 0, idx = 0;
4814 if (knew->iface == -1) /* skip this codec private value */
4817 kctl = snd_ctl_new1(knew, codec);
4821 kctl->id.device = addr;
4823 kctl->id.index = idx;
4824 err = snd_hda_ctl_add(codec, 0, kctl);
4827 /* try first with another device index corresponding to
4828 * the codec addr; if it still fails (or it's the
4829 * primary codec), then try another control index
4831 if (!addr && codec->addr)
4833 else if (!idx && !knew->index) {
4834 idx = find_empty_mixer_ctl_idx(codec,
4844 EXPORT_SYMBOL_HDA(snd_hda_add_new_ctls);
4847 static void hda_power_work(struct work_struct *work)
4849 struct hda_codec *codec =
4850 container_of(work, struct hda_codec, power_work.work);
4851 struct hda_bus *bus = codec->bus;
4854 spin_lock(&codec->power_lock);
4855 if (codec->power_transition > 0) { /* during power-up sequence? */
4856 spin_unlock(&codec->power_lock);
4859 if (!codec->power_on || codec->power_count) {
4860 codec->power_transition = 0;
4861 spin_unlock(&codec->power_lock);
4864 spin_unlock(&codec->power_lock);
4866 state = hda_call_codec_suspend(codec, true);
4867 codec->pm_down_notified = 0;
4868 if (!bus->power_keep_link_on && (state & AC_PWRST_CLK_STOP_OK)) {
4869 codec->pm_down_notified = 1;
4870 hda_call_pm_notify(bus, false);
4874 static void hda_keep_power_on(struct hda_codec *codec)
4876 spin_lock(&codec->power_lock);
4877 codec->power_count++;
4878 codec->power_on = 1;
4879 codec->power_jiffies = jiffies;
4880 spin_unlock(&codec->power_lock);
4883 /* update the power on/off account with the current jiffies */
4884 void snd_hda_update_power_acct(struct hda_codec *codec)
4886 unsigned long delta = jiffies - codec->power_jiffies;
4887 if (codec->power_on)
4888 codec->power_on_acct += delta;
4890 codec->power_off_acct += delta;
4891 codec->power_jiffies += delta;
4894 /* Transition to powered up, if wait_power_down then wait for a pending
4895 * transition to D3 to complete. A pending D3 transition is indicated
4896 * with power_transition == -1. */
4897 /* call this with codec->power_lock held! */
4898 static void __snd_hda_power_up(struct hda_codec *codec, bool wait_power_down)
4900 struct hda_bus *bus = codec->bus;
4902 /* Return if power_on or transitioning to power_on, unless currently
4904 if ((codec->power_on || codec->power_transition > 0) &&
4905 !(wait_power_down && codec->power_transition < 0))
4907 spin_unlock(&codec->power_lock);
4909 cancel_delayed_work_sync(&codec->power_work);
4911 spin_lock(&codec->power_lock);
4912 /* If the power down delayed work was cancelled above before starting,
4913 * then there is no need to go through power up here.
4915 if (codec->power_on) {
4916 if (codec->power_transition < 0)
4917 codec->power_transition = 0;
4921 trace_hda_power_up(codec);
4922 snd_hda_update_power_acct(codec);
4923 codec->power_on = 1;
4924 codec->power_jiffies = jiffies;
4925 codec->power_transition = 1; /* avoid reentrance */
4926 spin_unlock(&codec->power_lock);
4928 if (codec->pm_down_notified) {
4929 codec->pm_down_notified = 0;
4930 hda_call_pm_notify(bus, true);
4933 hda_call_codec_resume(codec);
4935 spin_lock(&codec->power_lock);
4936 codec->power_transition = 0;
4939 #define power_save(codec) \
4940 ((codec)->bus->power_save ? *(codec)->bus->power_save : 0)
4942 /* Transition to powered down */
4943 static void __snd_hda_power_down(struct hda_codec *codec)
4945 if (!codec->power_on || codec->power_count || codec->power_transition)
4948 if (power_save(codec)) {
4949 codec->power_transition = -1; /* avoid reentrance */
4950 queue_delayed_work(codec->bus->workq, &codec->power_work,
4951 msecs_to_jiffies(power_save(codec) * 1000));
4956 * snd_hda_power_save - Power-up/down/sync the codec
4957 * @codec: HD-audio codec
4958 * @delta: the counter delta to change
4960 * Change the power-up counter via @delta, and power up or down the hardware
4961 * appropriately. For the power-down, queue to the delayed action.
4962 * Passing zero to @delta means to synchronize the power state.
4964 void snd_hda_power_save(struct hda_codec *codec, int delta, bool d3wait)
4966 spin_lock(&codec->power_lock);
4967 codec->power_count += delta;
4968 trace_hda_power_count(codec);
4970 __snd_hda_power_up(codec, d3wait);
4972 __snd_hda_power_down(codec);
4973 spin_unlock(&codec->power_lock);
4975 EXPORT_SYMBOL_HDA(snd_hda_power_save);
4978 * snd_hda_check_amp_list_power - Check the amp list and update the power
4979 * @codec: HD-audio codec
4980 * @check: the object containing an AMP list and the status
4981 * @nid: NID to check / update
4983 * Check whether the given NID is in the amp list. If it's in the list,
4984 * check the current AMP status, and update the the power-status according
4985 * to the mute status.
4987 * This function is supposed to be set or called from the check_power_status
4990 int snd_hda_check_amp_list_power(struct hda_codec *codec,
4991 struct hda_loopback_check *check,
4994 const struct hda_amp_list *p;
4997 if (!check->amplist)
4999 for (p = check->amplist; p->nid; p++) {
5004 return 0; /* nothing changed */
5006 for (p = check->amplist; p->nid; p++) {
5007 for (ch = 0; ch < 2; ch++) {
5008 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
5010 if (!(v & HDA_AMP_MUTE) && v > 0) {
5011 if (!check->power_on) {
5012 check->power_on = 1;
5013 snd_hda_power_up(codec);
5019 if (check->power_on) {
5020 check->power_on = 0;
5021 snd_hda_power_down(codec);
5025 EXPORT_SYMBOL_HDA(snd_hda_check_amp_list_power);
5029 * Channel mode helper
5033 * snd_hda_ch_mode_info - Info callback helper for the channel mode enum
5035 int snd_hda_ch_mode_info(struct hda_codec *codec,
5036 struct snd_ctl_elem_info *uinfo,
5037 const struct hda_channel_mode *chmode,
5040 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
5042 uinfo->value.enumerated.items = num_chmodes;
5043 if (uinfo->value.enumerated.item >= num_chmodes)
5044 uinfo->value.enumerated.item = num_chmodes - 1;
5045 sprintf(uinfo->value.enumerated.name, "%dch",
5046 chmode[uinfo->value.enumerated.item].channels);
5049 EXPORT_SYMBOL_HDA(snd_hda_ch_mode_info);
5052 * snd_hda_ch_mode_get - Get callback helper for the channel mode enum
5054 int snd_hda_ch_mode_get(struct hda_codec *codec,
5055 struct snd_ctl_elem_value *ucontrol,
5056 const struct hda_channel_mode *chmode,
5062 for (i = 0; i < num_chmodes; i++) {
5063 if (max_channels == chmode[i].channels) {
5064 ucontrol->value.enumerated.item[0] = i;
5070 EXPORT_SYMBOL_HDA(snd_hda_ch_mode_get);
5073 * snd_hda_ch_mode_put - Put callback helper for the channel mode enum
5075 int snd_hda_ch_mode_put(struct hda_codec *codec,
5076 struct snd_ctl_elem_value *ucontrol,
5077 const struct hda_channel_mode *chmode,
5083 mode = ucontrol->value.enumerated.item[0];
5084 if (mode >= num_chmodes)
5086 if (*max_channelsp == chmode[mode].channels)
5088 /* change the current channel setting */
5089 *max_channelsp = chmode[mode].channels;
5090 if (chmode[mode].sequence)
5091 snd_hda_sequence_write_cache(codec, chmode[mode].sequence);
5094 EXPORT_SYMBOL_HDA(snd_hda_ch_mode_put);
5101 * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum
5103 int snd_hda_input_mux_info(const struct hda_input_mux *imux,
5104 struct snd_ctl_elem_info *uinfo)
5108 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
5110 uinfo->value.enumerated.items = imux->num_items;
5111 if (!imux->num_items)
5113 index = uinfo->value.enumerated.item;
5114 if (index >= imux->num_items)
5115 index = imux->num_items - 1;
5116 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
5119 EXPORT_SYMBOL_HDA(snd_hda_input_mux_info);
5122 * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum
5124 int snd_hda_input_mux_put(struct hda_codec *codec,
5125 const struct hda_input_mux *imux,
5126 struct snd_ctl_elem_value *ucontrol,
5128 unsigned int *cur_val)
5132 if (!imux->num_items)
5134 idx = ucontrol->value.enumerated.item[0];
5135 if (idx >= imux->num_items)
5136 idx = imux->num_items - 1;
5137 if (*cur_val == idx)
5139 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
5140 imux->items[idx].index);
5144 EXPORT_SYMBOL_HDA(snd_hda_input_mux_put);
5148 * process kcontrol info callback of a simple string enum array
5149 * when @num_items is 0 or @texts is NULL, assume a boolean enum array
5151 int snd_hda_enum_helper_info(struct snd_kcontrol *kcontrol,
5152 struct snd_ctl_elem_info *uinfo,
5153 int num_items, const char * const *texts)
5155 static const char * const texts_default[] = {
5156 "Disabled", "Enabled"
5159 if (!texts || !num_items) {
5161 texts = texts_default;
5164 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
5166 uinfo->value.enumerated.items = num_items;
5167 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
5168 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
5169 strcpy(uinfo->value.enumerated.name,
5170 texts[uinfo->value.enumerated.item]);
5173 EXPORT_SYMBOL_HDA(snd_hda_enum_helper_info);
5176 * Multi-channel / digital-out PCM helper functions
5179 /* setup SPDIF output stream */
5180 static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
5181 unsigned int stream_tag, unsigned int format)
5183 struct hda_spdif_out *spdif;
5184 unsigned int curr_fmt;
5187 spdif = snd_hda_spdif_out_of_nid(codec, nid);
5188 curr_fmt = snd_hda_codec_read(codec, nid, 0,
5189 AC_VERB_GET_STREAM_FORMAT, 0);
5190 reset = codec->spdif_status_reset &&
5191 (spdif->ctls & AC_DIG1_ENABLE) &&
5194 /* turn off SPDIF if needed; otherwise the IEC958 bits won't be
5197 set_dig_out_convert(codec, nid,
5198 spdif->ctls & ~AC_DIG1_ENABLE & 0xff,
5200 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
5201 if (codec->slave_dig_outs) {
5203 for (d = codec->slave_dig_outs; *d; d++)
5204 snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
5207 /* turn on again (if needed) */
5209 set_dig_out_convert(codec, nid,
5210 spdif->ctls & 0xff, -1);
5213 static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
5215 snd_hda_codec_cleanup_stream(codec, nid);
5216 if (codec->slave_dig_outs) {
5218 for (d = codec->slave_dig_outs; *d; d++)
5219 snd_hda_codec_cleanup_stream(codec, *d);
5224 * snd_hda_bus_reboot_notify - call the reboot notifier of each codec
5225 * @bus: HD-audio bus
5227 void snd_hda_bus_reboot_notify(struct hda_bus *bus)
5229 struct hda_codec *codec;
5233 list_for_each_entry(codec, &bus->codec_list, list) {
5234 if (hda_codec_is_power_on(codec) &&
5235 codec->patch_ops.reboot_notify)
5236 codec->patch_ops.reboot_notify(codec);
5239 EXPORT_SYMBOL_HDA(snd_hda_bus_reboot_notify);
5242 * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode
5244 int snd_hda_multi_out_dig_open(struct hda_codec *codec,
5245 struct hda_multi_out *mout)
5247 mutex_lock(&codec->spdif_mutex);
5248 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
5249 /* already opened as analog dup; reset it once */
5250 cleanup_dig_out_stream(codec, mout->dig_out_nid);
5251 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
5252 mutex_unlock(&codec->spdif_mutex);
5255 EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_open);
5258 * snd_hda_multi_out_dig_prepare - prepare the digital out stream
5260 int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
5261 struct hda_multi_out *mout,
5262 unsigned int stream_tag,
5263 unsigned int format,
5264 struct snd_pcm_substream *substream)
5266 mutex_lock(&codec->spdif_mutex);
5267 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
5268 mutex_unlock(&codec->spdif_mutex);
5271 EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_prepare);
5274 * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream
5276 int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec,
5277 struct hda_multi_out *mout)
5279 mutex_lock(&codec->spdif_mutex);
5280 cleanup_dig_out_stream(codec, mout->dig_out_nid);
5281 mutex_unlock(&codec->spdif_mutex);
5284 EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_cleanup);
5287 * snd_hda_multi_out_dig_close - release the digital out stream
5289 int snd_hda_multi_out_dig_close(struct hda_codec *codec,
5290 struct hda_multi_out *mout)
5292 mutex_lock(&codec->spdif_mutex);
5293 mout->dig_out_used = 0;
5294 mutex_unlock(&codec->spdif_mutex);
5297 EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_close);
5300 * snd_hda_multi_out_analog_open - open analog outputs
5302 * Open analog outputs and set up the hw-constraints.
5303 * If the digital outputs can be opened as slave, open the digital
5306 int snd_hda_multi_out_analog_open(struct hda_codec *codec,
5307 struct hda_multi_out *mout,
5308 struct snd_pcm_substream *substream,
5309 struct hda_pcm_stream *hinfo)
5311 struct snd_pcm_runtime *runtime = substream->runtime;
5312 runtime->hw.channels_max = mout->max_channels;
5313 if (mout->dig_out_nid) {
5314 if (!mout->analog_rates) {
5315 mout->analog_rates = hinfo->rates;
5316 mout->analog_formats = hinfo->formats;
5317 mout->analog_maxbps = hinfo->maxbps;
5319 runtime->hw.rates = mout->analog_rates;
5320 runtime->hw.formats = mout->analog_formats;
5321 hinfo->maxbps = mout->analog_maxbps;
5323 if (!mout->spdif_rates) {
5324 snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
5326 &mout->spdif_formats,
5327 &mout->spdif_maxbps);
5329 mutex_lock(&codec->spdif_mutex);
5330 if (mout->share_spdif) {
5331 if ((runtime->hw.rates & mout->spdif_rates) &&
5332 (runtime->hw.formats & mout->spdif_formats)) {
5333 runtime->hw.rates &= mout->spdif_rates;
5334 runtime->hw.formats &= mout->spdif_formats;
5335 if (mout->spdif_maxbps < hinfo->maxbps)
5336 hinfo->maxbps = mout->spdif_maxbps;
5338 mout->share_spdif = 0;
5339 /* FIXME: need notify? */
5342 mutex_unlock(&codec->spdif_mutex);
5344 return snd_pcm_hw_constraint_step(substream->runtime, 0,
5345 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
5347 EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_open);
5350 * snd_hda_multi_out_analog_prepare - Preapre the analog outputs.
5352 * Set up the i/o for analog out.
5353 * When the digital out is available, copy the front out to digital out, too.
5355 int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
5356 struct hda_multi_out *mout,
5357 unsigned int stream_tag,
5358 unsigned int format,
5359 struct snd_pcm_substream *substream)
5361 const hda_nid_t *nids = mout->dac_nids;
5362 int chs = substream->runtime->channels;
5363 struct hda_spdif_out *spdif;
5366 mutex_lock(&codec->spdif_mutex);
5367 spdif = snd_hda_spdif_out_of_nid(codec, mout->dig_out_nid);
5368 if (mout->dig_out_nid && mout->share_spdif &&
5369 mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
5371 snd_hda_is_supported_format(codec, mout->dig_out_nid,
5373 !(spdif->status & IEC958_AES0_NONAUDIO)) {
5374 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
5375 setup_dig_out_stream(codec, mout->dig_out_nid,
5376 stream_tag, format);
5378 mout->dig_out_used = 0;
5379 cleanup_dig_out_stream(codec, mout->dig_out_nid);
5382 mutex_unlock(&codec->spdif_mutex);
5385 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
5387 if (!mout->no_share_stream &&
5388 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
5389 /* headphone out will just decode front left/right (stereo) */
5390 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
5392 /* extra outputs copied from front */
5393 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
5394 if (!mout->no_share_stream && mout->hp_out_nid[i])
5395 snd_hda_codec_setup_stream(codec,
5396 mout->hp_out_nid[i],
5397 stream_tag, 0, format);
5398 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
5399 if (!mout->no_share_stream && mout->extra_out_nid[i])
5400 snd_hda_codec_setup_stream(codec,
5401 mout->extra_out_nid[i],
5402 stream_tag, 0, format);
5405 for (i = 1; i < mout->num_dacs; i++) {
5406 if (chs >= (i + 1) * 2) /* independent out */
5407 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
5409 else if (!mout->no_share_stream) /* copy front */
5410 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
5415 EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_prepare);
5418 * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out
5420 int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
5421 struct hda_multi_out *mout)
5423 const hda_nid_t *nids = mout->dac_nids;
5426 for (i = 0; i < mout->num_dacs; i++)
5427 snd_hda_codec_cleanup_stream(codec, nids[i]);
5429 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
5430 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
5431 if (mout->hp_out_nid[i])
5432 snd_hda_codec_cleanup_stream(codec,
5433 mout->hp_out_nid[i]);
5434 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
5435 if (mout->extra_out_nid[i])
5436 snd_hda_codec_cleanup_stream(codec,
5437 mout->extra_out_nid[i]);
5438 mutex_lock(&codec->spdif_mutex);
5439 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
5440 cleanup_dig_out_stream(codec, mout->dig_out_nid);
5441 mout->dig_out_used = 0;
5443 mutex_unlock(&codec->spdif_mutex);
5446 EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_cleanup);
5449 * snd_hda_get_default_vref - Get the default (mic) VREF pin bits
5451 * Guess the suitable VREF pin bits to be set as the pin-control value.
5452 * Note: the function doesn't set the AC_PINCTL_IN_EN bit.
5454 unsigned int snd_hda_get_default_vref(struct hda_codec *codec, hda_nid_t pin)
5456 unsigned int pincap;
5457 unsigned int oldval;
5458 oldval = snd_hda_codec_read(codec, pin, 0,
5459 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
5460 pincap = snd_hda_query_pin_caps(codec, pin);
5461 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
5462 /* Exception: if the default pin setup is vref50, we give it priority */
5463 if ((pincap & AC_PINCAP_VREF_80) && oldval != PIN_VREF50)
5464 return AC_PINCTL_VREF_80;
5465 else if (pincap & AC_PINCAP_VREF_50)
5466 return AC_PINCTL_VREF_50;
5467 else if (pincap & AC_PINCAP_VREF_100)
5468 return AC_PINCTL_VREF_100;
5469 else if (pincap & AC_PINCAP_VREF_GRD)
5470 return AC_PINCTL_VREF_GRD;
5471 return AC_PINCTL_VREF_HIZ;
5473 EXPORT_SYMBOL_HDA(snd_hda_get_default_vref);
5475 /* correct the pin ctl value for matching with the pin cap */
5476 unsigned int snd_hda_correct_pin_ctl(struct hda_codec *codec,
5477 hda_nid_t pin, unsigned int val)
5479 static unsigned int cap_lists[][2] = {
5480 { AC_PINCTL_VREF_100, AC_PINCAP_VREF_100 },
5481 { AC_PINCTL_VREF_80, AC_PINCAP_VREF_80 },
5482 { AC_PINCTL_VREF_50, AC_PINCAP_VREF_50 },
5483 { AC_PINCTL_VREF_GRD, AC_PINCAP_VREF_GRD },
5489 cap = snd_hda_query_pin_caps(codec, pin);
5491 return val; /* don't know what to do... */
5493 if (val & AC_PINCTL_OUT_EN) {
5494 if (!(cap & AC_PINCAP_OUT))
5495 val &= ~(AC_PINCTL_OUT_EN | AC_PINCTL_HP_EN);
5496 else if ((val & AC_PINCTL_HP_EN) && !(cap & AC_PINCAP_HP_DRV))
5497 val &= ~AC_PINCTL_HP_EN;
5500 if (val & AC_PINCTL_IN_EN) {
5501 if (!(cap & AC_PINCAP_IN))
5502 val &= ~(AC_PINCTL_IN_EN | AC_PINCTL_VREFEN);
5504 unsigned int vcap, vref;
5506 vcap = (cap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
5507 vref = val & AC_PINCTL_VREFEN;
5508 for (i = 0; i < ARRAY_SIZE(cap_lists); i++) {
5509 if (vref == cap_lists[i][0] &&
5510 !(vcap & cap_lists[i][1])) {
5511 if (i == ARRAY_SIZE(cap_lists) - 1)
5512 vref = AC_PINCTL_VREF_HIZ;
5514 vref = cap_lists[i + 1][0];
5517 val &= ~AC_PINCTL_VREFEN;
5524 EXPORT_SYMBOL_HDA(snd_hda_correct_pin_ctl);
5526 int _snd_hda_set_pin_ctl(struct hda_codec *codec, hda_nid_t pin,
5527 unsigned int val, bool cached)
5529 val = snd_hda_correct_pin_ctl(codec, pin, val);
5530 snd_hda_codec_set_pin_target(codec, pin, val);
5532 return snd_hda_codec_update_cache(codec, pin, 0,
5533 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
5535 return snd_hda_codec_write(codec, pin, 0,
5536 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
5538 EXPORT_SYMBOL_HDA(_snd_hda_set_pin_ctl);
5541 * snd_hda_add_imux_item - Add an item to input_mux
5543 * When the same label is used already in the existing items, the number
5544 * suffix is appended to the label. This label index number is stored
5545 * to type_idx when non-NULL pointer is given.
5547 int snd_hda_add_imux_item(struct hda_input_mux *imux, const char *label,
5548 int index, int *type_idx)
5550 int i, label_idx = 0;
5551 if (imux->num_items >= HDA_MAX_NUM_INPUTS) {
5552 snd_printd(KERN_ERR "hda_codec: Too many imux items!\n");
5555 for (i = 0; i < imux->num_items; i++) {
5556 if (!strncmp(label, imux->items[i].label, strlen(label)))
5560 *type_idx = label_idx;
5562 snprintf(imux->items[imux->num_items].label,
5563 sizeof(imux->items[imux->num_items].label),
5564 "%s %d", label, label_idx);
5566 strlcpy(imux->items[imux->num_items].label, label,
5567 sizeof(imux->items[imux->num_items].label));
5568 imux->items[imux->num_items].index = index;
5572 EXPORT_SYMBOL_HDA(snd_hda_add_imux_item);
5581 * snd_hda_suspend - suspend the codecs
5584 * Returns 0 if successful.
5586 int snd_hda_suspend(struct hda_bus *bus)
5588 struct hda_codec *codec;
5590 list_for_each_entry(codec, &bus->codec_list, list) {
5591 cancel_delayed_work_sync(&codec->jackpoll_work);
5592 if (hda_codec_is_power_on(codec))
5593 hda_call_codec_suspend(codec, false);
5597 EXPORT_SYMBOL_HDA(snd_hda_suspend);
5600 * snd_hda_resume - resume the codecs
5603 * Returns 0 if successful.
5605 int snd_hda_resume(struct hda_bus *bus)
5607 struct hda_codec *codec;
5609 list_for_each_entry(codec, &bus->codec_list, list) {
5610 hda_call_codec_resume(codec);
5614 EXPORT_SYMBOL_HDA(snd_hda_resume);
5615 #endif /* CONFIG_PM */
5622 * snd_array_new - get a new element from the given array
5623 * @array: the array object
5625 * Get a new element from the given array. If it exceeds the
5626 * pre-allocated array size, re-allocate the array.
5628 * Returns NULL if allocation failed.
5630 void *snd_array_new(struct snd_array *array)
5632 if (snd_BUG_ON(!array->elem_size))
5634 if (array->used >= array->alloced) {
5635 int num = array->alloced + array->alloc_align;
5636 int size = (num + 1) * array->elem_size;
5638 if (snd_BUG_ON(num >= 4096))
5640 nlist = krealloc(array->list, size, GFP_KERNEL | __GFP_ZERO);
5643 array->list = nlist;
5644 array->alloced = num;
5646 return snd_array_elem(array, array->used++);
5648 EXPORT_SYMBOL_HDA(snd_array_new);
5651 * snd_array_free - free the given array elements
5652 * @array: the array object
5654 void snd_array_free(struct snd_array *array)
5661 EXPORT_SYMBOL_HDA(snd_array_free);
5664 * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer
5665 * @pcm: PCM caps bits
5666 * @buf: the string buffer to write
5667 * @buflen: the max buffer length
5669 * used by hda_proc.c and hda_eld.c
5671 void snd_print_pcm_bits(int pcm, char *buf, int buflen)
5673 static unsigned int bits[] = { 8, 16, 20, 24, 32 };
5676 for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
5677 if (pcm & (AC_SUPPCM_BITS_8 << i))
5678 j += snprintf(buf + j, buflen - j, " %d", bits[i]);
5680 buf[j] = '\0'; /* necessary when j == 0 */
5682 EXPORT_SYMBOL_HDA(snd_print_pcm_bits);
5684 MODULE_DESCRIPTION("HDA codec core");
5685 MODULE_LICENSE("GPL");