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_pm_work(struct work_struct *work);
100 static void hda_power_work(struct work_struct *work);
101 static void hda_keep_power_on(struct hda_codec *codec);
102 #define hda_codec_is_power_on(codec) ((codec)->power_on)
104 static void hda_call_pm_notify(struct hda_codec *codec, bool power_up)
106 struct hda_bus *bus = codec->bus;
108 if ((power_up && codec->pm_up_notified) ||
109 (!power_up && !codec->pm_up_notified))
111 if (bus->ops.pm_notify)
112 bus->ops.pm_notify(bus, power_up);
113 codec->pm_up_notified = power_up;
117 #define codec_in_pm(codec) 0
118 static inline void hda_keep_power_on(struct hda_codec *codec) {}
119 #define hda_codec_is_power_on(codec) 1
120 #define hda_call_pm_notify(codec, state) {}
124 * snd_hda_get_jack_location - Give a location string of the jack
125 * @cfg: pin default config value
127 * Parse the pin default config value and returns the string of the
128 * jack location, e.g. "Rear", "Front", etc.
130 const char *snd_hda_get_jack_location(u32 cfg)
132 static char *bases[7] = {
133 "N/A", "Rear", "Front", "Left", "Right", "Top", "Bottom",
135 static unsigned char specials_idx[] = {
140 static char *specials[] = {
141 "Rear Panel", "Drive Bar",
142 "Riser", "HDMI", "ATAPI",
143 "Mobile-In", "Mobile-Out"
146 cfg = (cfg & AC_DEFCFG_LOCATION) >> AC_DEFCFG_LOCATION_SHIFT;
147 if ((cfg & 0x0f) < 7)
148 return bases[cfg & 0x0f];
149 for (i = 0; i < ARRAY_SIZE(specials_idx); i++) {
150 if (cfg == specials_idx[i])
155 EXPORT_SYMBOL_HDA(snd_hda_get_jack_location);
158 * snd_hda_get_jack_connectivity - Give a connectivity string of the jack
159 * @cfg: pin default config value
161 * Parse the pin default config value and returns the string of the
162 * jack connectivity, i.e. external or internal connection.
164 const char *snd_hda_get_jack_connectivity(u32 cfg)
166 static char *jack_locations[4] = { "Ext", "Int", "Sep", "Oth" };
168 return jack_locations[(cfg >> (AC_DEFCFG_LOCATION_SHIFT + 4)) & 3];
170 EXPORT_SYMBOL_HDA(snd_hda_get_jack_connectivity);
173 * snd_hda_get_jack_type - Give a type string of the jack
174 * @cfg: pin default config value
176 * Parse the pin default config value and returns the string of the
177 * jack type, i.e. the purpose of the jack, such as Line-Out or CD.
179 const char *snd_hda_get_jack_type(u32 cfg)
181 static char *jack_types[16] = {
182 "Line Out", "Speaker", "HP Out", "CD",
183 "SPDIF Out", "Digital Out", "Modem Line", "Modem Hand",
184 "Line In", "Aux", "Mic", "Telephony",
185 "SPDIF In", "Digital In", "Reserved", "Other"
188 return jack_types[(cfg & AC_DEFCFG_DEVICE)
189 >> AC_DEFCFG_DEVICE_SHIFT];
191 EXPORT_SYMBOL_HDA(snd_hda_get_jack_type);
194 * Compose a 32bit command word to be sent to the HD-audio controller
196 static inline unsigned int
197 make_codec_cmd(struct hda_codec *codec, hda_nid_t nid, int flags,
198 unsigned int verb, unsigned int parm)
202 if ((codec->addr & ~0xf) || (nid & ~0x7f) ||
203 (verb & ~0xfff) || (parm & ~0xffff)) {
204 printk(KERN_ERR "hda-codec: out of range cmd %x:%x:%x:%x\n",
205 codec->addr, nid, verb, parm);
209 val = (u32)codec->addr << 28;
210 val |= (u32)nid << 20;
217 * Send and receive a verb
219 static int codec_exec_verb(struct hda_codec *codec, unsigned int cmd,
220 int flags, unsigned int *res)
222 struct hda_bus *bus = codec->bus;
231 snd_hda_power_up(codec);
232 mutex_lock(&bus->cmd_mutex);
233 if (flags & HDA_RW_NO_RESPONSE_FALLBACK)
234 bus->no_response_fallback = 1;
236 trace_hda_send_cmd(codec, cmd);
237 err = bus->ops.command(bus, cmd);
240 /* process pending verbs */
241 bus->ops.get_response(bus, codec->addr);
244 *res = bus->ops.get_response(bus, codec->addr);
245 trace_hda_get_response(codec, *res);
247 bus->no_response_fallback = 0;
248 mutex_unlock(&bus->cmd_mutex);
249 snd_hda_power_down(codec);
250 if (!codec_in_pm(codec) && res && *res == -1 && bus->rirb_error) {
251 if (bus->response_reset) {
252 snd_printd("hda_codec: resetting BUS due to "
253 "fatal communication error\n");
254 trace_hda_bus_reset(bus);
255 bus->ops.bus_reset(bus);
259 /* clear reset-flag when the communication gets recovered */
260 if (!err || codec_in_pm(codec))
261 bus->response_reset = 0;
266 * snd_hda_codec_read - send a command and get the response
267 * @codec: the HDA codec
268 * @nid: NID to send the command
269 * @flags: optional bit flags
270 * @verb: the verb to send
271 * @parm: the parameter for the verb
273 * Send a single command and read the corresponding response.
275 * Returns the obtained response value, or -1 for an error.
277 unsigned int snd_hda_codec_read(struct hda_codec *codec, hda_nid_t nid,
279 unsigned int verb, unsigned int parm)
281 unsigned cmd = make_codec_cmd(codec, nid, flags, verb, parm);
283 if (codec_exec_verb(codec, cmd, flags, &res))
287 EXPORT_SYMBOL_HDA(snd_hda_codec_read);
290 * snd_hda_codec_write - send a single command without waiting for response
291 * @codec: the HDA codec
292 * @nid: NID to send the command
293 * @flags: optional bit flags
294 * @verb: the verb to send
295 * @parm: the parameter for the verb
297 * Send a single command without waiting for response.
299 * Returns 0 if successful, or a negative error code.
301 int snd_hda_codec_write(struct hda_codec *codec, hda_nid_t nid, int flags,
302 unsigned int verb, unsigned int parm)
304 unsigned int cmd = make_codec_cmd(codec, nid, flags, verb, parm);
306 return codec_exec_verb(codec, cmd, flags,
307 codec->bus->sync_write ? &res : NULL);
309 EXPORT_SYMBOL_HDA(snd_hda_codec_write);
312 * snd_hda_sequence_write - sequence writes
313 * @codec: the HDA codec
314 * @seq: VERB array to send
316 * Send the commands sequentially from the given array.
317 * The array must be terminated with NID=0.
319 void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq)
321 for (; seq->nid; seq++)
322 snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param);
324 EXPORT_SYMBOL_HDA(snd_hda_sequence_write);
327 * snd_hda_get_sub_nodes - get the range of sub nodes
328 * @codec: the HDA codec
330 * @start_id: the pointer to store the start NID
332 * Parse the NID and store the start NID of its sub-nodes.
333 * Returns the number of sub-nodes.
335 int snd_hda_get_sub_nodes(struct hda_codec *codec, hda_nid_t nid,
340 parm = snd_hda_param_read(codec, nid, AC_PAR_NODE_COUNT);
343 *start_id = (parm >> 16) & 0x7fff;
344 return (int)(parm & 0x7fff);
346 EXPORT_SYMBOL_HDA(snd_hda_get_sub_nodes);
348 /* connection list element */
349 struct hda_conn_list {
350 struct list_head list;
356 /* look up the cached results */
357 static struct hda_conn_list *
358 lookup_conn_list(struct hda_codec *codec, hda_nid_t nid)
360 struct hda_conn_list *p;
361 list_for_each_entry(p, &codec->conn_list, list) {
368 static int add_conn_list(struct hda_codec *codec, hda_nid_t nid, int len,
369 const hda_nid_t *list)
371 struct hda_conn_list *p;
373 p = kmalloc(sizeof(*p) + len * sizeof(hda_nid_t), GFP_KERNEL);
378 memcpy(p->conns, list, len * sizeof(hda_nid_t));
379 list_add(&p->list, &codec->conn_list);
383 static void remove_conn_list(struct hda_codec *codec)
385 while (!list_empty(&codec->conn_list)) {
386 struct hda_conn_list *p;
387 p = list_first_entry(&codec->conn_list, typeof(*p), list);
393 /* read the connection and add to the cache */
394 static int read_and_add_raw_conns(struct hda_codec *codec, hda_nid_t nid)
397 hda_nid_t *result = list;
400 len = snd_hda_get_raw_connections(codec, nid, list, ARRAY_SIZE(list));
401 if (len == -ENOSPC) {
402 len = snd_hda_get_num_raw_conns(codec, nid);
403 result = kmalloc(sizeof(hda_nid_t) * len, GFP_KERNEL);
406 len = snd_hda_get_raw_connections(codec, nid, result, len);
409 len = snd_hda_override_conn_list(codec, nid, len, result);
416 * snd_hda_get_conn_list - get connection list
417 * @codec: the HDA codec
419 * @len: number of connection list entries
420 * @listp: the pointer to store NID list
422 * Parses the connection list of the given widget and stores the pointer
423 * to the list of NIDs.
425 * Returns the number of connections, or a negative error code.
427 * Note that the returned pointer isn't protected against the list
428 * modification. If snd_hda_override_conn_list() might be called
429 * concurrently, protect with a mutex appropriately.
431 int snd_hda_get_conn_list(struct hda_codec *codec, hda_nid_t nid,
432 const hda_nid_t **listp)
438 const struct hda_conn_list *p;
440 /* if the connection-list is already cached, read it */
441 p = lookup_conn_list(codec, nid);
447 if (snd_BUG_ON(added))
450 err = read_and_add_raw_conns(codec, nid);
456 EXPORT_SYMBOL_HDA(snd_hda_get_conn_list);
459 * snd_hda_get_connections - copy connection list
460 * @codec: the HDA codec
462 * @conn_list: connection list array; when NULL, checks only the size
463 * @max_conns: max. number of connections to store
465 * Parses the connection list of the given widget and stores the list
468 * Returns the number of connections, or a negative error code.
470 int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
471 hda_nid_t *conn_list, int max_conns)
473 const hda_nid_t *list;
474 int len = snd_hda_get_conn_list(codec, nid, &list);
476 if (len > 0 && conn_list) {
477 if (len > max_conns) {
478 snd_printk(KERN_ERR "hda_codec: "
479 "Too many connections %d for NID 0x%x\n",
483 memcpy(conn_list, list, len * sizeof(hda_nid_t));
488 EXPORT_SYMBOL_HDA(snd_hda_get_connections);
490 /* return CONNLIST_LEN parameter of the given widget */
491 static unsigned int get_num_conns(struct hda_codec *codec, hda_nid_t nid)
493 unsigned int wcaps = get_wcaps(codec, nid);
496 if (!(wcaps & AC_WCAP_CONN_LIST) &&
497 get_wcaps_type(wcaps) != AC_WID_VOL_KNB)
500 parm = snd_hda_param_read(codec, nid, AC_PAR_CONNLIST_LEN);
506 int snd_hda_get_num_raw_conns(struct hda_codec *codec, hda_nid_t nid)
508 return snd_hda_get_raw_connections(codec, nid, NULL, 0);
512 * snd_hda_get_raw_connections - copy connection list without cache
513 * @codec: the HDA codec
515 * @conn_list: connection list array
516 * @max_conns: max. number of connections to store
518 * Like snd_hda_get_connections(), copy the connection list but without
519 * checking through the connection-list cache.
520 * Currently called only from hda_proc.c, so not exported.
522 int snd_hda_get_raw_connections(struct hda_codec *codec, hda_nid_t nid,
523 hda_nid_t *conn_list, int max_conns)
526 int i, conn_len, conns;
527 unsigned int shift, num_elems, mask;
531 parm = get_num_conns(codec, nid);
535 if (parm & AC_CLIST_LONG) {
544 conn_len = parm & AC_CLIST_LENGTH;
545 mask = (1 << (shift-1)) - 1;
548 return 0; /* no connection */
551 /* single connection */
552 parm = snd_hda_codec_read(codec, nid, 0,
553 AC_VERB_GET_CONNECT_LIST, 0);
554 if (parm == -1 && codec->bus->rirb_error)
557 conn_list[0] = parm & mask;
561 /* multi connection */
564 for (i = 0; i < conn_len; i++) {
568 if (i % num_elems == 0) {
569 parm = snd_hda_codec_read(codec, nid, 0,
570 AC_VERB_GET_CONNECT_LIST, i);
571 if (parm == -1 && codec->bus->rirb_error)
574 range_val = !!(parm & (1 << (shift-1))); /* ranges */
576 if (val == 0 && null_count++) { /* no second chance */
577 snd_printdd("hda_codec: "
578 "invalid CONNECT_LIST verb %x[%i]:%x\n",
584 /* ranges between the previous and this one */
585 if (!prev_nid || prev_nid >= val) {
586 snd_printk(KERN_WARNING "hda_codec: "
587 "invalid dep_range_val %x:%x\n",
591 for (n = prev_nid + 1; n <= val; n++) {
593 if (conns >= max_conns)
595 conn_list[conns] = n;
601 if (conns >= max_conns)
603 conn_list[conns] = val;
613 * snd_hda_override_conn_list - add/modify the connection-list to cache
614 * @codec: the HDA codec
616 * @len: number of connection list entries
617 * @list: the list of connection entries
619 * Add or modify the given connection-list to the cache. If the corresponding
620 * cache already exists, invalidate it and append a new one.
622 * Returns zero or a negative error code.
624 int snd_hda_override_conn_list(struct hda_codec *codec, hda_nid_t nid, int len,
625 const hda_nid_t *list)
627 struct hda_conn_list *p;
629 p = lookup_conn_list(codec, nid);
635 return add_conn_list(codec, nid, len, list);
637 EXPORT_SYMBOL_HDA(snd_hda_override_conn_list);
640 * snd_hda_get_conn_index - get the connection index of the given NID
641 * @codec: the HDA codec
642 * @mux: NID containing the list
643 * @nid: NID to select
644 * @recursive: 1 when searching NID recursively, otherwise 0
646 * Parses the connection list of the widget @mux and checks whether the
647 * widget @nid is present. If it is, return the connection index.
648 * Otherwise it returns -1.
650 int snd_hda_get_conn_index(struct hda_codec *codec, hda_nid_t mux,
651 hda_nid_t nid, int recursive)
653 const hda_nid_t *conn;
656 nums = snd_hda_get_conn_list(codec, mux, &conn);
657 for (i = 0; i < nums; i++)
662 if (recursive > 10) {
663 snd_printd("hda_codec: too deep connection for 0x%x\n", nid);
667 for (i = 0; i < nums; i++) {
668 unsigned int type = get_wcaps_type(get_wcaps(codec, conn[i]));
669 if (type == AC_WID_PIN || type == AC_WID_AUD_OUT)
671 if (snd_hda_get_conn_index(codec, conn[i], nid, recursive) >= 0)
676 EXPORT_SYMBOL_HDA(snd_hda_get_conn_index);
679 /* return DEVLIST_LEN parameter of the given widget */
680 static unsigned int get_num_devices(struct hda_codec *codec, hda_nid_t nid)
682 unsigned int wcaps = get_wcaps(codec, nid);
685 if (!codec->dp_mst || !(wcaps & AC_WCAP_DIGITAL) ||
686 get_wcaps_type(wcaps) != AC_WID_PIN)
689 parm = snd_hda_param_read(codec, nid, AC_PAR_DEVLIST_LEN);
690 if (parm == -1 && codec->bus->rirb_error)
692 return parm & AC_DEV_LIST_LEN_MASK;
696 * snd_hda_get_devices - copy device list without cache
697 * @codec: the HDA codec
698 * @nid: NID of the pin to parse
699 * @dev_list: device list array
700 * @max_devices: max. number of devices to store
702 * Copy the device list. This info is dynamic and so not cached.
703 * Currently called only from hda_proc.c, so not exported.
705 int snd_hda_get_devices(struct hda_codec *codec, hda_nid_t nid,
706 u8 *dev_list, int max_devices)
709 int i, dev_len, devices;
711 parm = get_num_devices(codec, nid);
712 if (!parm) /* not multi-stream capable */
716 dev_len = dev_len < max_devices ? dev_len : max_devices;
719 while (devices < dev_len) {
720 parm = snd_hda_codec_read(codec, nid, 0,
721 AC_VERB_GET_DEVICE_LIST, devices);
722 if (parm == -1 && codec->bus->rirb_error)
725 for (i = 0; i < 8; i++) {
726 dev_list[devices] = (u8)parm;
729 if (devices >= dev_len)
737 * snd_hda_queue_unsol_event - add an unsolicited event to queue
739 * @res: unsolicited event (lower 32bit of RIRB entry)
740 * @res_ex: codec addr and flags (upper 32bit or RIRB entry)
742 * Adds the given event to the queue. The events are processed in
743 * the workqueue asynchronously. Call this function in the interrupt
744 * hanlder when RIRB receives an unsolicited event.
746 * Returns 0 if successful, or a negative error code.
748 int snd_hda_queue_unsol_event(struct hda_bus *bus, u32 res, u32 res_ex)
750 struct hda_bus_unsolicited *unsol;
753 if (!bus || !bus->workq)
756 trace_hda_unsol_event(bus, res, res_ex);
761 wp = (unsol->wp + 1) % HDA_UNSOL_QUEUE_SIZE;
765 unsol->queue[wp] = res;
766 unsol->queue[wp + 1] = res_ex;
768 queue_work(bus->workq, &unsol->work);
772 EXPORT_SYMBOL_HDA(snd_hda_queue_unsol_event);
775 * process queued unsolicited events
777 static void process_unsol_events(struct work_struct *work)
779 struct hda_bus_unsolicited *unsol =
780 container_of(work, struct hda_bus_unsolicited, work);
781 struct hda_bus *bus = unsol->bus;
782 struct hda_codec *codec;
783 unsigned int rp, caddr, res;
785 while (unsol->rp != unsol->wp) {
786 rp = (unsol->rp + 1) % HDA_UNSOL_QUEUE_SIZE;
789 res = unsol->queue[rp];
790 caddr = unsol->queue[rp + 1];
791 if (!(caddr & (1 << 4))) /* no unsolicited event? */
793 codec = bus->caddr_tbl[caddr & 0x0f];
794 if (codec && codec->patch_ops.unsol_event)
795 codec->patch_ops.unsol_event(codec, res);
800 * initialize unsolicited queue
802 static int init_unsol_queue(struct hda_bus *bus)
804 struct hda_bus_unsolicited *unsol;
806 if (bus->unsol) /* already initialized */
809 unsol = kzalloc(sizeof(*unsol), GFP_KERNEL);
811 snd_printk(KERN_ERR "hda_codec: "
812 "can't allocate unsolicited queue\n");
815 INIT_WORK(&unsol->work, process_unsol_events);
824 static void snd_hda_codec_free(struct hda_codec *codec);
826 static int snd_hda_bus_free(struct hda_bus *bus)
828 struct hda_codec *codec, *n;
833 flush_workqueue(bus->workq);
836 list_for_each_entry_safe(codec, n, &bus->codec_list, list) {
837 snd_hda_codec_free(codec);
839 if (bus->ops.private_free)
840 bus->ops.private_free(bus);
842 destroy_workqueue(bus->workq);
846 destroy_workqueue(bus->pm_wq);
853 static int snd_hda_bus_dev_free(struct snd_device *device)
855 struct hda_bus *bus = device->device_data;
857 return snd_hda_bus_free(bus);
860 #ifdef CONFIG_SND_HDA_HWDEP
861 static int snd_hda_bus_dev_register(struct snd_device *device)
863 struct hda_bus *bus = device->device_data;
864 struct hda_codec *codec;
865 list_for_each_entry(codec, &bus->codec_list, list) {
866 snd_hda_hwdep_add_sysfs(codec);
867 snd_hda_hwdep_add_power_sysfs(codec);
872 #define snd_hda_bus_dev_register NULL
876 * snd_hda_bus_new - create a HDA bus
877 * @card: the card entry
878 * @temp: the template for hda_bus information
879 * @busp: the pointer to store the created bus instance
881 * Returns 0 if successful, or a negative error code.
883 int snd_hda_bus_new(struct snd_card *card,
884 const struct hda_bus_template *temp,
885 struct hda_bus **busp)
889 static struct snd_device_ops dev_ops = {
890 .dev_register = snd_hda_bus_dev_register,
891 .dev_free = snd_hda_bus_dev_free,
897 if (snd_BUG_ON(!temp))
899 if (snd_BUG_ON(!temp->ops.command || !temp->ops.get_response))
905 bus = kzalloc(sizeof(*bus), GFP_KERNEL);
907 snd_printk(KERN_ERR "can't allocate struct hda_bus\n");
912 bus->private_data = temp->private_data;
913 bus->pci = temp->pci;
914 bus->modelname = temp->modelname;
915 bus->power_save = temp->power_save;
916 bus->ops = temp->ops;
918 mutex_init(&bus->cmd_mutex);
919 mutex_init(&bus->prepare_mutex);
920 INIT_LIST_HEAD(&bus->codec_list);
922 snprintf(bus->workq_name, sizeof(bus->workq_name),
923 "hd-audio%d", card->number);
924 bus->workq = create_singlethread_workqueue(bus->workq_name);
926 snd_printk(KERN_ERR "cannot create workqueue %s\n",
933 sprintf(wqname, "hda-pm-wq-%d", card->number);
934 bus->pm_wq = create_workqueue(wqname);
936 snd_printk(KERN_ERR "cannot create PM workqueue\n");
937 snd_hda_bus_free(bus);
942 err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops);
944 snd_hda_bus_free(bus);
951 EXPORT_SYMBOL_HDA(snd_hda_bus_new);
953 #ifdef CONFIG_SND_HDA_GENERIC
954 #define is_generic_config(codec) \
955 (codec->modelname && !strcmp(codec->modelname, "generic"))
957 #define is_generic_config(codec) 0
961 #define HDA_MODREQ_MAX_COUNT 2 /* two request_modules()'s */
963 #define HDA_MODREQ_MAX_COUNT 0 /* all presets are statically linked */
967 * find a matching codec preset
969 static const struct hda_codec_preset *
970 find_codec_preset(struct hda_codec *codec)
972 struct hda_codec_preset_list *tbl;
973 const struct hda_codec_preset *preset;
974 unsigned int mod_requested = 0;
977 mutex_lock(&preset_mutex);
978 list_for_each_entry(tbl, &hda_preset_tables, list) {
979 if (!try_module_get(tbl->owner)) {
980 snd_printk(KERN_ERR "hda_codec: cannot module_get\n");
983 for (preset = tbl->preset; preset->id; preset++) {
984 u32 mask = preset->mask;
985 if (preset->afg && preset->afg != codec->afg)
987 if (preset->mfg && preset->mfg != codec->mfg)
991 if (preset->id == (codec->vendor_id & mask) &&
993 preset->rev == codec->revision_id)) {
994 mutex_unlock(&preset_mutex);
995 codec->owner = tbl->owner;
999 module_put(tbl->owner);
1001 mutex_unlock(&preset_mutex);
1003 if (mod_requested < HDA_MODREQ_MAX_COUNT) {
1006 snprintf(name, sizeof(name), "snd-hda-codec-id:%08x",
1009 snprintf(name, sizeof(name), "snd-hda-codec-id:%04x*",
1010 (codec->vendor_id >> 16) & 0xffff);
1011 request_module(name);
1019 * get_codec_name - store the codec name
1021 static int get_codec_name(struct hda_codec *codec)
1023 const struct hda_vendor_id *c;
1024 const char *vendor = NULL;
1025 u16 vendor_id = codec->vendor_id >> 16;
1028 if (codec->vendor_name)
1031 for (c = hda_vendor_ids; c->id; c++) {
1032 if (c->id == vendor_id) {
1038 sprintf(tmp, "Generic %04x", vendor_id);
1041 codec->vendor_name = kstrdup(vendor, GFP_KERNEL);
1042 if (!codec->vendor_name)
1046 if (codec->chip_name)
1049 if (codec->preset && codec->preset->name)
1050 codec->chip_name = kstrdup(codec->preset->name, GFP_KERNEL);
1052 sprintf(tmp, "ID %x", codec->vendor_id & 0xffff);
1053 codec->chip_name = kstrdup(tmp, GFP_KERNEL);
1055 if (!codec->chip_name)
1061 * look for an AFG and MFG nodes
1063 static void setup_fg_nodes(struct hda_codec *codec)
1065 int i, total_nodes, function_id;
1068 total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid);
1069 for (i = 0; i < total_nodes; i++, nid++) {
1070 function_id = snd_hda_param_read(codec, nid,
1071 AC_PAR_FUNCTION_TYPE);
1072 switch (function_id & 0xff) {
1073 case AC_GRP_AUDIO_FUNCTION:
1075 codec->afg_function_id = function_id & 0xff;
1076 codec->afg_unsol = (function_id >> 8) & 1;
1078 case AC_GRP_MODEM_FUNCTION:
1080 codec->mfg_function_id = function_id & 0xff;
1081 codec->mfg_unsol = (function_id >> 8) & 1;
1090 * read widget caps for each widget and store in cache
1092 static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
1097 codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node,
1099 codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL);
1102 nid = codec->start_nid;
1103 for (i = 0; i < codec->num_nodes; i++, nid++)
1104 codec->wcaps[i] = snd_hda_param_read(codec, nid,
1105 AC_PAR_AUDIO_WIDGET_CAP);
1109 /* read all pin default configurations and save codec->init_pins */
1110 static int read_pin_defaults(struct hda_codec *codec)
1113 hda_nid_t nid = codec->start_nid;
1115 for (i = 0; i < codec->num_nodes; i++, nid++) {
1116 struct hda_pincfg *pin;
1117 unsigned int wcaps = get_wcaps(codec, nid);
1118 unsigned int wid_type = get_wcaps_type(wcaps);
1119 if (wid_type != AC_WID_PIN)
1121 pin = snd_array_new(&codec->init_pins);
1125 pin->cfg = snd_hda_codec_read(codec, nid, 0,
1126 AC_VERB_GET_CONFIG_DEFAULT, 0);
1127 pin->ctrl = snd_hda_codec_read(codec, nid, 0,
1128 AC_VERB_GET_PIN_WIDGET_CONTROL,
1134 /* look up the given pin config list and return the item matching with NID */
1135 static struct hda_pincfg *look_up_pincfg(struct hda_codec *codec,
1136 struct snd_array *array,
1140 for (i = 0; i < array->used; i++) {
1141 struct hda_pincfg *pin = snd_array_elem(array, i);
1142 if (pin->nid == nid)
1148 /* set the current pin config value for the given NID.
1149 * the value is cached, and read via snd_hda_codec_get_pincfg()
1151 int snd_hda_add_pincfg(struct hda_codec *codec, struct snd_array *list,
1152 hda_nid_t nid, unsigned int cfg)
1154 struct hda_pincfg *pin;
1156 /* the check below may be invalid when pins are added by a fixup
1157 * dynamically (e.g. via snd_hda_codec_update_widgets()), so disabled
1161 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN)
1165 pin = look_up_pincfg(codec, list, nid);
1167 pin = snd_array_new(list);
1177 * snd_hda_codec_set_pincfg - Override a pin default configuration
1178 * @codec: the HDA codec
1179 * @nid: NID to set the pin config
1180 * @cfg: the pin default config value
1182 * Override a pin default configuration value in the cache.
1183 * This value can be read by snd_hda_codec_get_pincfg() in a higher
1184 * priority than the real hardware value.
1186 int snd_hda_codec_set_pincfg(struct hda_codec *codec,
1187 hda_nid_t nid, unsigned int cfg)
1189 return snd_hda_add_pincfg(codec, &codec->driver_pins, nid, cfg);
1191 EXPORT_SYMBOL_HDA(snd_hda_codec_set_pincfg);
1194 * snd_hda_codec_get_pincfg - Obtain a pin-default configuration
1195 * @codec: the HDA codec
1196 * @nid: NID to get the pin config
1198 * Get the current pin config value of the given pin NID.
1199 * If the pincfg value is cached or overridden via sysfs or driver,
1200 * returns the cached value.
1202 unsigned int snd_hda_codec_get_pincfg(struct hda_codec *codec, hda_nid_t nid)
1204 struct hda_pincfg *pin;
1206 #ifdef CONFIG_SND_HDA_HWDEP
1208 unsigned int cfg = 0;
1209 mutex_lock(&codec->user_mutex);
1210 pin = look_up_pincfg(codec, &codec->user_pins, nid);
1213 mutex_unlock(&codec->user_mutex);
1218 pin = look_up_pincfg(codec, &codec->driver_pins, nid);
1221 pin = look_up_pincfg(codec, &codec->init_pins, nid);
1226 EXPORT_SYMBOL_HDA(snd_hda_codec_get_pincfg);
1228 /* remember the current pinctl target value */
1229 int snd_hda_codec_set_pin_target(struct hda_codec *codec, hda_nid_t nid,
1232 struct hda_pincfg *pin;
1234 pin = look_up_pincfg(codec, &codec->init_pins, nid);
1240 EXPORT_SYMBOL_HDA(snd_hda_codec_set_pin_target);
1242 /* return the current pinctl target value */
1243 int snd_hda_codec_get_pin_target(struct hda_codec *codec, hda_nid_t nid)
1245 struct hda_pincfg *pin;
1247 pin = look_up_pincfg(codec, &codec->init_pins, nid);
1252 EXPORT_SYMBOL_HDA(snd_hda_codec_get_pin_target);
1255 * snd_hda_shutup_pins - Shut up all pins
1256 * @codec: the HDA codec
1258 * Clear all pin controls to shup up before suspend for avoiding click noise.
1259 * The controls aren't cached so that they can be resumed properly.
1261 void snd_hda_shutup_pins(struct hda_codec *codec)
1264 /* don't shut up pins when unloading the driver; otherwise it breaks
1265 * the default pin setup at the next load of the driver
1267 if (codec->bus->shutdown)
1269 for (i = 0; i < codec->init_pins.used; i++) {
1270 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1271 /* use read here for syncing after issuing each verb */
1272 snd_hda_codec_read(codec, pin->nid, 0,
1273 AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
1275 codec->pins_shutup = 1;
1277 EXPORT_SYMBOL_HDA(snd_hda_shutup_pins);
1280 /* Restore the pin controls cleared previously via snd_hda_shutup_pins() */
1281 static void restore_shutup_pins(struct hda_codec *codec)
1284 if (!codec->pins_shutup)
1286 if (codec->bus->shutdown)
1288 for (i = 0; i < codec->init_pins.used; i++) {
1289 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1290 snd_hda_codec_write(codec, pin->nid, 0,
1291 AC_VERB_SET_PIN_WIDGET_CONTROL,
1294 codec->pins_shutup = 0;
1298 static void hda_jackpoll_work(struct work_struct *work)
1300 struct hda_codec *codec =
1301 container_of(work, struct hda_codec, jackpoll_work.work);
1303 snd_hda_jack_set_dirty_all(codec);
1304 snd_hda_jack_poll_all(codec);
1306 if (!codec->jackpoll_interval)
1309 queue_delayed_work(codec->bus->workq, &codec->jackpoll_work,
1310 codec->jackpoll_interval);
1313 static void init_hda_cache(struct hda_cache_rec *cache,
1314 unsigned int record_size);
1315 static void free_hda_cache(struct hda_cache_rec *cache);
1317 /* release all pincfg lists */
1318 static void free_init_pincfgs(struct hda_codec *codec)
1320 snd_array_free(&codec->driver_pins);
1321 #ifdef CONFIG_SND_HDA_HWDEP
1322 snd_array_free(&codec->user_pins);
1324 snd_array_free(&codec->init_pins);
1328 * audio-converter setup caches
1330 struct hda_cvt_setup {
1335 unsigned char active; /* cvt is currently used */
1336 unsigned char dirty; /* setups should be cleared */
1339 /* get or create a cache entry for the given audio converter NID */
1340 static struct hda_cvt_setup *
1341 get_hda_cvt_setup(struct hda_codec *codec, hda_nid_t nid)
1343 struct hda_cvt_setup *p;
1346 for (i = 0; i < codec->cvt_setups.used; i++) {
1347 p = snd_array_elem(&codec->cvt_setups, i);
1351 p = snd_array_new(&codec->cvt_setups);
1358 * Dynamic symbol binding for the codec parsers
1361 #define load_parser_sym(sym) ((int (*)(struct hda_codec *))symbol_request(sym))
1362 #define unload_parser_addr(addr) symbol_put_addr(addr)
1364 #define load_parser_sym(sym) (sym)
1365 #define unload_parser_addr(addr) do {} while (0)
1368 #define load_parser(codec, sym) \
1369 ((codec)->parser = load_parser_sym(sym))
1371 static void unload_parser(struct hda_codec *codec)
1373 if (codec->parser) {
1374 unload_parser_addr(codec->parser);
1375 codec->parser = NULL;
1382 static void snd_hda_codec_free(struct hda_codec *codec)
1386 cancel_delayed_work_sync(&codec->jackpoll_work);
1387 snd_hda_jack_tbl_clear(codec);
1388 free_init_pincfgs(codec);
1390 cancel_delayed_work(&codec->power_work);
1391 flush_workqueue(codec->bus->workq);
1393 list_del(&codec->list);
1394 snd_array_free(&codec->mixers);
1395 snd_array_free(&codec->nids);
1396 snd_array_free(&codec->cvt_setups);
1397 snd_array_free(&codec->spdif_out);
1398 remove_conn_list(codec);
1399 codec->bus->caddr_tbl[codec->addr] = NULL;
1400 if (codec->patch_ops.free)
1401 codec->patch_ops.free(codec);
1402 hda_call_pm_notify(codec, false); /* cancel leftover refcounts */
1403 unload_parser(codec);
1404 module_put(codec->owner);
1405 free_hda_cache(&codec->amp_cache);
1406 free_hda_cache(&codec->cmd_cache);
1407 kfree(codec->vendor_name);
1408 kfree(codec->chip_name);
1409 kfree(codec->modelname);
1410 kfree(codec->wcaps);
1411 codec->bus->num_codecs--;
1415 static bool snd_hda_codec_get_supported_ps(struct hda_codec *codec,
1416 hda_nid_t fg, unsigned int power_state);
1418 static unsigned int hda_set_power_state(struct hda_codec *codec,
1419 unsigned int power_state);
1422 * snd_hda_codec_new - create a HDA codec
1423 * @bus: the bus to assign
1424 * @codec_addr: the codec address
1425 * @codecp: the pointer to store the generated codec
1427 * Returns 0 if successful, or a negative error code.
1429 int snd_hda_codec_new(struct hda_bus *bus,
1430 unsigned int codec_addr,
1431 struct hda_codec **codecp)
1433 struct hda_codec *codec;
1438 if (snd_BUG_ON(!bus))
1440 if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS))
1443 if (bus->caddr_tbl[codec_addr]) {
1444 snd_printk(KERN_ERR "hda_codec: "
1445 "address 0x%x is already occupied\n", codec_addr);
1449 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
1450 if (codec == NULL) {
1451 snd_printk(KERN_ERR "can't allocate struct hda_codec\n");
1456 codec->addr = codec_addr;
1457 mutex_init(&codec->spdif_mutex);
1458 mutex_init(&codec->control_mutex);
1459 mutex_init(&codec->hash_mutex);
1460 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
1461 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
1462 snd_array_init(&codec->mixers, sizeof(struct hda_nid_item), 32);
1463 snd_array_init(&codec->nids, sizeof(struct hda_nid_item), 32);
1464 snd_array_init(&codec->init_pins, sizeof(struct hda_pincfg), 16);
1465 snd_array_init(&codec->driver_pins, sizeof(struct hda_pincfg), 16);
1466 snd_array_init(&codec->cvt_setups, sizeof(struct hda_cvt_setup), 8);
1467 snd_array_init(&codec->spdif_out, sizeof(struct hda_spdif_out), 16);
1468 snd_array_init(&codec->jacktbl, sizeof(struct hda_jack_tbl), 16);
1469 snd_array_init(&codec->verbs, sizeof(struct hda_verb *), 8);
1470 INIT_LIST_HEAD(&codec->conn_list);
1472 INIT_DELAYED_WORK(&codec->jackpoll_work, hda_jackpoll_work);
1475 spin_lock_init(&codec->power_lock);
1476 INIT_DELAYED_WORK(&codec->power_work, hda_power_work);
1477 INIT_WORK(&codec->pm_work, hda_pm_work);
1478 /* snd_hda_codec_new() marks the codec as power-up, and leave it as is.
1479 * the caller has to power down appropriatley after initialization
1482 hda_keep_power_on(codec);
1485 if (codec->bus->modelname) {
1486 codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL);
1487 if (!codec->modelname) {
1488 snd_hda_codec_free(codec);
1493 list_add_tail(&codec->list, &bus->codec_list);
1496 workqueue_set_max_active(bus->pm_wq, bus->num_codecs);
1499 bus->caddr_tbl[codec_addr] = codec;
1501 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1503 if (codec->vendor_id == -1)
1504 /* read again, hopefully the access method was corrected
1505 * in the last read...
1507 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1509 codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1510 AC_PAR_SUBSYSTEM_ID);
1511 codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1514 setup_fg_nodes(codec);
1515 if (!codec->afg && !codec->mfg) {
1516 snd_printdd("hda_codec: no AFG or MFG node found\n");
1521 fg = codec->afg ? codec->afg : codec->mfg;
1522 err = read_widget_caps(codec, fg);
1524 snd_printk(KERN_ERR "hda_codec: cannot malloc\n");
1527 err = read_pin_defaults(codec);
1531 if (!codec->subsystem_id) {
1532 codec->subsystem_id =
1533 snd_hda_codec_read(codec, fg, 0,
1534 AC_VERB_GET_SUBSYSTEM_ID, 0);
1538 codec->d3_stop_clk = snd_hda_codec_get_supported_ps(codec, fg,
1541 codec->epss = snd_hda_codec_get_supported_ps(codec, fg,
1544 if (!codec->d3_stop_clk || !codec->epss)
1545 bus->power_keep_link_on = 1;
1549 /* power-up all before initialization */
1550 hda_set_power_state(codec, AC_PWRST_D0);
1552 snd_hda_codec_proc_new(codec);
1554 snd_hda_create_hwdep(codec);
1556 sprintf(component, "HDA:%08x,%08x,%08x", codec->vendor_id,
1557 codec->subsystem_id, codec->revision_id);
1558 snd_component_add(codec->bus->card, component);
1565 snd_hda_codec_free(codec);
1568 EXPORT_SYMBOL_HDA(snd_hda_codec_new);
1570 int snd_hda_codec_update_widgets(struct hda_codec *codec)
1575 /* Assume the function group node does not change,
1576 * only the widget nodes may change.
1578 kfree(codec->wcaps);
1579 fg = codec->afg ? codec->afg : codec->mfg;
1580 err = read_widget_caps(codec, fg);
1582 snd_printk(KERN_ERR "hda_codec: cannot malloc\n");
1586 snd_array_free(&codec->init_pins);
1587 err = read_pin_defaults(codec);
1591 EXPORT_SYMBOL_HDA(snd_hda_codec_update_widgets);
1594 #ifdef CONFIG_SND_HDA_CODEC_HDMI
1595 /* if all audio out widgets are digital, let's assume the codec as a HDMI/DP */
1596 static bool is_likely_hdmi_codec(struct hda_codec *codec)
1598 hda_nid_t nid = codec->start_nid;
1601 for (i = 0; i < codec->num_nodes; i++, nid++) {
1602 unsigned int wcaps = get_wcaps(codec, nid);
1603 switch (get_wcaps_type(wcaps)) {
1605 return false; /* HDMI parser supports only HDMI out */
1606 case AC_WID_AUD_OUT:
1607 if (!(wcaps & AC_WCAP_DIGITAL))
1615 /* no HDMI codec parser support */
1616 #define is_likely_hdmi_codec(codec) false
1617 #endif /* CONFIG_SND_HDA_CODEC_HDMI */
1620 * snd_hda_codec_configure - (Re-)configure the HD-audio codec
1621 * @codec: the HDA codec
1623 * Start parsing of the given codec tree and (re-)initialize the whole
1626 * Returns 0 if successful or a negative error code.
1628 int snd_hda_codec_configure(struct hda_codec *codec)
1630 int (*patch)(struct hda_codec *) = NULL;
1633 codec->preset = find_codec_preset(codec);
1634 if (!codec->vendor_name || !codec->chip_name) {
1635 err = get_codec_name(codec);
1640 if (!is_generic_config(codec) && codec->preset)
1641 patch = codec->preset->patch;
1643 unload_parser(codec); /* to be sure */
1644 if (is_likely_hdmi_codec(codec))
1645 patch = load_parser(codec, snd_hda_parse_hdmi_codec);
1646 #ifdef CONFIG_SND_HDA_GENERIC
1648 patch = load_parser(codec, snd_hda_parse_generic_codec);
1651 printk(KERN_ERR "hda-codec: No codec parser is available\n");
1658 unload_parser(codec);
1662 if (codec->patch_ops.unsol_event) {
1663 err = init_unsol_queue(codec->bus);
1668 /* audio codec should override the mixer name */
1669 if (codec->afg || !*codec->bus->card->mixername)
1670 snprintf(codec->bus->card->mixername,
1671 sizeof(codec->bus->card->mixername),
1672 "%s %s", codec->vendor_name, codec->chip_name);
1675 EXPORT_SYMBOL_HDA(snd_hda_codec_configure);
1677 /* update the stream-id if changed */
1678 static void update_pcm_stream_id(struct hda_codec *codec,
1679 struct hda_cvt_setup *p, hda_nid_t nid,
1680 u32 stream_tag, int channel_id)
1682 unsigned int oldval, newval;
1684 if (p->stream_tag != stream_tag || p->channel_id != channel_id) {
1685 oldval = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0);
1686 newval = (stream_tag << 4) | channel_id;
1687 if (oldval != newval)
1688 snd_hda_codec_write(codec, nid, 0,
1689 AC_VERB_SET_CHANNEL_STREAMID,
1691 p->stream_tag = stream_tag;
1692 p->channel_id = channel_id;
1696 /* update the format-id if changed */
1697 static void update_pcm_format(struct hda_codec *codec, struct hda_cvt_setup *p,
1698 hda_nid_t nid, int format)
1700 unsigned int oldval;
1702 if (p->format_id != format) {
1703 oldval = snd_hda_codec_read(codec, nid, 0,
1704 AC_VERB_GET_STREAM_FORMAT, 0);
1705 if (oldval != format) {
1707 snd_hda_codec_write(codec, nid, 0,
1708 AC_VERB_SET_STREAM_FORMAT,
1711 p->format_id = format;
1716 * snd_hda_codec_setup_stream - set up the codec for streaming
1717 * @codec: the CODEC to set up
1718 * @nid: the NID to set up
1719 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
1720 * @channel_id: channel id to pass, zero based.
1721 * @format: stream format.
1723 void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
1725 int channel_id, int format)
1727 struct hda_codec *c;
1728 struct hda_cvt_setup *p;
1735 snd_printdd("hda_codec_setup_stream: "
1736 "NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
1737 nid, stream_tag, channel_id, format);
1738 p = get_hda_cvt_setup(codec, nid);
1742 if (codec->pcm_format_first)
1743 update_pcm_format(codec, p, nid, format);
1744 update_pcm_stream_id(codec, p, nid, stream_tag, channel_id);
1745 if (!codec->pcm_format_first)
1746 update_pcm_format(codec, p, nid, format);
1751 /* make other inactive cvts with the same stream-tag dirty */
1752 type = get_wcaps_type(get_wcaps(codec, nid));
1753 list_for_each_entry(c, &codec->bus->codec_list, list) {
1754 for (i = 0; i < c->cvt_setups.used; i++) {
1755 p = snd_array_elem(&c->cvt_setups, i);
1756 if (!p->active && p->stream_tag == stream_tag &&
1757 get_wcaps_type(get_wcaps(c, p->nid)) == type)
1762 EXPORT_SYMBOL_HDA(snd_hda_codec_setup_stream);
1764 static void really_cleanup_stream(struct hda_codec *codec,
1765 struct hda_cvt_setup *q);
1768 * __snd_hda_codec_cleanup_stream - clean up the codec for closing
1769 * @codec: the CODEC to clean up
1770 * @nid: the NID to clean up
1771 * @do_now: really clean up the stream instead of clearing the active flag
1773 void __snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid,
1776 struct hda_cvt_setup *p;
1781 if (codec->no_sticky_stream)
1784 snd_printdd("hda_codec_cleanup_stream: NID=0x%x\n", nid);
1785 p = get_hda_cvt_setup(codec, nid);
1787 /* here we just clear the active flag when do_now isn't set;
1788 * actual clean-ups will be done later in
1789 * purify_inactive_streams() called from snd_hda_codec_prpapre()
1792 really_cleanup_stream(codec, p);
1797 EXPORT_SYMBOL_HDA(__snd_hda_codec_cleanup_stream);
1799 static void really_cleanup_stream(struct hda_codec *codec,
1800 struct hda_cvt_setup *q)
1802 hda_nid_t nid = q->nid;
1803 if (q->stream_tag || q->channel_id)
1804 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
1806 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0
1808 memset(q, 0, sizeof(*q));
1812 /* clean up the all conflicting obsolete streams */
1813 static void purify_inactive_streams(struct hda_codec *codec)
1815 struct hda_codec *c;
1818 list_for_each_entry(c, &codec->bus->codec_list, list) {
1819 for (i = 0; i < c->cvt_setups.used; i++) {
1820 struct hda_cvt_setup *p;
1821 p = snd_array_elem(&c->cvt_setups, i);
1823 really_cleanup_stream(c, p);
1829 /* clean up all streams; called from suspend */
1830 static void hda_cleanup_all_streams(struct hda_codec *codec)
1834 for (i = 0; i < codec->cvt_setups.used; i++) {
1835 struct hda_cvt_setup *p = snd_array_elem(&codec->cvt_setups, i);
1837 really_cleanup_stream(codec, p);
1843 * amp access functions
1846 /* FIXME: more better hash key? */
1847 #define HDA_HASH_KEY(nid, dir, idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
1848 #define HDA_HASH_PINCAP_KEY(nid) (u32)((nid) + (0x02 << 24))
1849 #define HDA_HASH_PARPCM_KEY(nid) (u32)((nid) + (0x03 << 24))
1850 #define HDA_HASH_PARSTR_KEY(nid) (u32)((nid) + (0x04 << 24))
1851 #define INFO_AMP_CAPS (1<<0)
1852 #define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
1854 /* initialize the hash table */
1855 static void init_hda_cache(struct hda_cache_rec *cache,
1856 unsigned int record_size)
1858 memset(cache, 0, sizeof(*cache));
1859 memset(cache->hash, 0xff, sizeof(cache->hash));
1860 snd_array_init(&cache->buf, record_size, 64);
1863 static void free_hda_cache(struct hda_cache_rec *cache)
1865 snd_array_free(&cache->buf);
1868 /* query the hash. allocate an entry if not found. */
1869 static struct hda_cache_head *get_hash(struct hda_cache_rec *cache, u32 key)
1871 u16 idx = key % (u16)ARRAY_SIZE(cache->hash);
1872 u16 cur = cache->hash[idx];
1873 struct hda_cache_head *info;
1875 while (cur != 0xffff) {
1876 info = snd_array_elem(&cache->buf, cur);
1877 if (info->key == key)
1884 /* query the hash. allocate an entry if not found. */
1885 static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache,
1888 struct hda_cache_head *info = get_hash(cache, key);
1891 /* add a new hash entry */
1892 info = snd_array_new(&cache->buf);
1895 cur = snd_array_index(&cache->buf, info);
1899 idx = key % (u16)ARRAY_SIZE(cache->hash);
1900 info->next = cache->hash[idx];
1901 cache->hash[idx] = cur;
1906 /* query and allocate an amp hash entry */
1907 static inline struct hda_amp_info *
1908 get_alloc_amp_hash(struct hda_codec *codec, u32 key)
1910 return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key);
1913 /* overwrite the value with the key in the caps hash */
1914 static int write_caps_hash(struct hda_codec *codec, u32 key, unsigned int val)
1916 struct hda_amp_info *info;
1918 mutex_lock(&codec->hash_mutex);
1919 info = get_alloc_amp_hash(codec, key);
1921 mutex_unlock(&codec->hash_mutex);
1924 info->amp_caps = val;
1925 info->head.val |= INFO_AMP_CAPS;
1926 mutex_unlock(&codec->hash_mutex);
1930 /* query the value from the caps hash; if not found, fetch the current
1931 * value from the given function and store in the hash
1934 query_caps_hash(struct hda_codec *codec, hda_nid_t nid, int dir, u32 key,
1935 unsigned int (*func)(struct hda_codec *, hda_nid_t, int))
1937 struct hda_amp_info *info;
1940 mutex_lock(&codec->hash_mutex);
1941 info = get_alloc_amp_hash(codec, key);
1943 mutex_unlock(&codec->hash_mutex);
1946 if (!(info->head.val & INFO_AMP_CAPS)) {
1947 mutex_unlock(&codec->hash_mutex); /* for reentrance */
1948 val = func(codec, nid, dir);
1949 write_caps_hash(codec, key, val);
1951 val = info->amp_caps;
1952 mutex_unlock(&codec->hash_mutex);
1957 static unsigned int read_amp_cap(struct hda_codec *codec, hda_nid_t nid,
1960 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
1962 return snd_hda_param_read(codec, nid,
1963 direction == HDA_OUTPUT ?
1964 AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP);
1968 * query_amp_caps - query AMP capabilities
1969 * @codec: the HD-auio codec
1970 * @nid: the NID to query
1971 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1973 * Query AMP capabilities for the given widget and direction.
1974 * Returns the obtained capability bits.
1976 * When cap bits have been already read, this doesn't read again but
1977 * returns the cached value.
1979 u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
1981 return query_caps_hash(codec, nid, direction,
1982 HDA_HASH_KEY(nid, direction, 0),
1985 EXPORT_SYMBOL_HDA(query_amp_caps);
1988 * snd_hda_override_amp_caps - Override the AMP capabilities
1989 * @codec: the CODEC to clean up
1990 * @nid: the NID to clean up
1991 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1992 * @caps: the capability bits to set
1994 * Override the cached AMP caps bits value by the given one.
1995 * This function is useful if the driver needs to adjust the AMP ranges,
1996 * e.g. limit to 0dB, etc.
1998 * Returns zero if successful or a negative error code.
2000 int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
2003 return write_caps_hash(codec, HDA_HASH_KEY(nid, dir, 0), caps);
2005 EXPORT_SYMBOL_HDA(snd_hda_override_amp_caps);
2007 static unsigned int read_pin_cap(struct hda_codec *codec, hda_nid_t nid,
2010 return snd_hda_param_read(codec, nid, AC_PAR_PIN_CAP);
2014 * snd_hda_query_pin_caps - Query PIN capabilities
2015 * @codec: the HD-auio codec
2016 * @nid: the NID to query
2018 * Query PIN capabilities for the given widget.
2019 * Returns the obtained capability bits.
2021 * When cap bits have been already read, this doesn't read again but
2022 * returns the cached value.
2024 u32 snd_hda_query_pin_caps(struct hda_codec *codec, hda_nid_t nid)
2026 return query_caps_hash(codec, nid, 0, HDA_HASH_PINCAP_KEY(nid),
2029 EXPORT_SYMBOL_HDA(snd_hda_query_pin_caps);
2032 * snd_hda_override_pin_caps - Override the pin capabilities
2034 * @nid: the NID to override
2035 * @caps: the capability bits to set
2037 * Override the cached PIN capabilitiy bits value by the given one.
2039 * Returns zero if successful or a negative error code.
2041 int snd_hda_override_pin_caps(struct hda_codec *codec, hda_nid_t nid,
2044 return write_caps_hash(codec, HDA_HASH_PINCAP_KEY(nid), caps);
2046 EXPORT_SYMBOL_HDA(snd_hda_override_pin_caps);
2048 /* read or sync the hash value with the current value;
2049 * call within hash_mutex
2051 static struct hda_amp_info *
2052 update_amp_hash(struct hda_codec *codec, hda_nid_t nid, int ch,
2053 int direction, int index, bool init_only)
2055 struct hda_amp_info *info;
2056 unsigned int parm, val = 0;
2057 bool val_read = false;
2060 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
2063 if (!(info->head.val & INFO_AMP_VOL(ch))) {
2065 mutex_unlock(&codec->hash_mutex);
2066 parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
2067 parm |= direction == HDA_OUTPUT ?
2068 AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
2070 val = snd_hda_codec_read(codec, nid, 0,
2071 AC_VERB_GET_AMP_GAIN_MUTE, parm);
2074 mutex_lock(&codec->hash_mutex);
2077 info->vol[ch] = val;
2078 info->head.val |= INFO_AMP_VOL(ch);
2079 } else if (init_only)
2085 * write the current volume in info to the h/w
2087 static void put_vol_mute(struct hda_codec *codec, unsigned int amp_caps,
2088 hda_nid_t nid, int ch, int direction, int index,
2093 parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
2094 parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
2095 parm |= index << AC_AMP_SET_INDEX_SHIFT;
2096 if ((val & HDA_AMP_MUTE) && !(amp_caps & AC_AMPCAP_MUTE) &&
2097 (amp_caps & AC_AMPCAP_MIN_MUTE))
2098 ; /* set the zero value as a fake mute */
2101 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
2105 * snd_hda_codec_amp_read - Read AMP value
2106 * @codec: HD-audio codec
2107 * @nid: NID to read the AMP value
2108 * @ch: channel (left=0 or right=1)
2109 * @direction: #HDA_INPUT or #HDA_OUTPUT
2110 * @index: the index value (only for input direction)
2112 * Read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
2114 int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
2115 int direction, int index)
2117 struct hda_amp_info *info;
2118 unsigned int val = 0;
2120 mutex_lock(&codec->hash_mutex);
2121 info = update_amp_hash(codec, nid, ch, direction, index, false);
2123 val = info->vol[ch];
2124 mutex_unlock(&codec->hash_mutex);
2127 EXPORT_SYMBOL_HDA(snd_hda_codec_amp_read);
2129 static int codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
2130 int direction, int idx, int mask, int val,
2133 struct hda_amp_info *info;
2135 unsigned int cache_only;
2137 if (snd_BUG_ON(mask & ~0xff))
2141 mutex_lock(&codec->hash_mutex);
2142 info = update_amp_hash(codec, nid, ch, direction, idx, init_only);
2144 mutex_unlock(&codec->hash_mutex);
2147 val |= info->vol[ch] & ~mask;
2148 if (info->vol[ch] == val) {
2149 mutex_unlock(&codec->hash_mutex);
2152 info->vol[ch] = val;
2153 cache_only = info->head.dirty = codec->cached_write;
2154 caps = info->amp_caps;
2155 mutex_unlock(&codec->hash_mutex);
2157 put_vol_mute(codec, caps, nid, ch, direction, idx, val);
2162 * snd_hda_codec_amp_update - update the AMP value
2163 * @codec: HD-audio codec
2164 * @nid: NID to read the AMP value
2165 * @ch: channel (left=0 or right=1)
2166 * @direction: #HDA_INPUT or #HDA_OUTPUT
2167 * @idx: the index value (only for input direction)
2168 * @mask: bit mask to set
2169 * @val: the bits value to set
2171 * Update the AMP value with a bit mask.
2172 * Returns 0 if the value is unchanged, 1 if changed.
2174 int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
2175 int direction, int idx, int mask, int val)
2177 return codec_amp_update(codec, nid, ch, direction, idx, mask, val, false);
2179 EXPORT_SYMBOL_HDA(snd_hda_codec_amp_update);
2182 * snd_hda_codec_amp_stereo - update the AMP stereo values
2183 * @codec: HD-audio codec
2184 * @nid: NID to read the AMP value
2185 * @direction: #HDA_INPUT or #HDA_OUTPUT
2186 * @idx: the index value (only for input direction)
2187 * @mask: bit mask to set
2188 * @val: the bits value to set
2190 * Update the AMP values like snd_hda_codec_amp_update(), but for a
2191 * stereo widget with the same mask and value.
2193 int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
2194 int direction, int idx, int mask, int val)
2198 if (snd_BUG_ON(mask & ~0xff))
2200 for (ch = 0; ch < 2; ch++)
2201 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
2205 EXPORT_SYMBOL_HDA(snd_hda_codec_amp_stereo);
2207 /* Works like snd_hda_codec_amp_update() but it writes the value only at
2208 * the first access. If the amp was already initialized / updated beforehand,
2209 * this does nothing.
2211 int snd_hda_codec_amp_init(struct hda_codec *codec, hda_nid_t nid, int ch,
2212 int dir, int idx, int mask, int val)
2214 return codec_amp_update(codec, nid, ch, dir, idx, mask, val, true);
2216 EXPORT_SYMBOL_HDA(snd_hda_codec_amp_init);
2218 int snd_hda_codec_amp_init_stereo(struct hda_codec *codec, hda_nid_t nid,
2219 int dir, int idx, int mask, int val)
2223 if (snd_BUG_ON(mask & ~0xff))
2225 for (ch = 0; ch < 2; ch++)
2226 ret |= snd_hda_codec_amp_init(codec, nid, ch, dir,
2230 EXPORT_SYMBOL_HDA(snd_hda_codec_amp_init_stereo);
2233 * snd_hda_codec_resume_amp - Resume all AMP commands from the cache
2234 * @codec: HD-audio codec
2236 * Resume the all amp commands from the cache.
2238 void snd_hda_codec_resume_amp(struct hda_codec *codec)
2242 mutex_lock(&codec->hash_mutex);
2243 codec->cached_write = 0;
2244 for (i = 0; i < codec->amp_cache.buf.used; i++) {
2245 struct hda_amp_info *buffer;
2248 unsigned int idx, dir, ch;
2249 struct hda_amp_info info;
2251 buffer = snd_array_elem(&codec->amp_cache.buf, i);
2252 if (!buffer->head.dirty)
2254 buffer->head.dirty = 0;
2256 key = info.head.key;
2260 idx = (key >> 16) & 0xff;
2261 dir = (key >> 24) & 0xff;
2262 for (ch = 0; ch < 2; ch++) {
2263 if (!(info.head.val & INFO_AMP_VOL(ch)))
2265 mutex_unlock(&codec->hash_mutex);
2266 put_vol_mute(codec, info.amp_caps, nid, ch, dir, idx,
2268 mutex_lock(&codec->hash_mutex);
2271 mutex_unlock(&codec->hash_mutex);
2273 EXPORT_SYMBOL_HDA(snd_hda_codec_resume_amp);
2275 static u32 get_amp_max_value(struct hda_codec *codec, hda_nid_t nid, int dir,
2278 u32 caps = query_amp_caps(codec, nid, dir);
2280 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
2287 * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer
2289 * The control element is supposed to have the private_value field
2290 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2292 int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
2293 struct snd_ctl_elem_info *uinfo)
2295 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2296 u16 nid = get_amp_nid(kcontrol);
2297 u8 chs = get_amp_channels(kcontrol);
2298 int dir = get_amp_direction(kcontrol);
2299 unsigned int ofs = get_amp_offset(kcontrol);
2301 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2302 uinfo->count = chs == 3 ? 2 : 1;
2303 uinfo->value.integer.min = 0;
2304 uinfo->value.integer.max = get_amp_max_value(codec, nid, dir, ofs);
2305 if (!uinfo->value.integer.max) {
2306 printk(KERN_WARNING "hda_codec: "
2307 "num_steps = 0 for NID=0x%x (ctl = %s)\n", nid,
2313 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_info);
2316 static inline unsigned int
2317 read_amp_value(struct hda_codec *codec, hda_nid_t nid,
2318 int ch, int dir, int idx, unsigned int ofs)
2321 val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
2322 val &= HDA_AMP_VOLMASK;
2331 update_amp_value(struct hda_codec *codec, hda_nid_t nid,
2332 int ch, int dir, int idx, unsigned int ofs,
2335 unsigned int maxval;
2339 /* ofs = 0: raw max value */
2340 maxval = get_amp_max_value(codec, nid, dir, 0);
2343 return snd_hda_codec_amp_update(codec, nid, ch, dir, idx,
2344 HDA_AMP_VOLMASK, val);
2348 * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume
2350 * The control element is supposed to have the private_value field
2351 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2353 int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
2354 struct snd_ctl_elem_value *ucontrol)
2356 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2357 hda_nid_t nid = get_amp_nid(kcontrol);
2358 int chs = get_amp_channels(kcontrol);
2359 int dir = get_amp_direction(kcontrol);
2360 int idx = get_amp_index(kcontrol);
2361 unsigned int ofs = get_amp_offset(kcontrol);
2362 long *valp = ucontrol->value.integer.value;
2365 *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs);
2367 *valp = read_amp_value(codec, nid, 1, dir, idx, ofs);
2370 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_get);
2373 * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume
2375 * The control element is supposed to have the private_value field
2376 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2378 int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
2379 struct snd_ctl_elem_value *ucontrol)
2381 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2382 hda_nid_t nid = get_amp_nid(kcontrol);
2383 int chs = get_amp_channels(kcontrol);
2384 int dir = get_amp_direction(kcontrol);
2385 int idx = get_amp_index(kcontrol);
2386 unsigned int ofs = get_amp_offset(kcontrol);
2387 long *valp = ucontrol->value.integer.value;
2390 snd_hda_power_up(codec);
2392 change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp);
2396 change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp);
2397 snd_hda_power_down(codec);
2400 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_put);
2403 * snd_hda_mixer_amp_volume_put - TLV callback for a standard AMP mixer volume
2405 * The control element is supposed to have the private_value field
2406 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2408 int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2409 unsigned int size, unsigned int __user *_tlv)
2411 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2412 hda_nid_t nid = get_amp_nid(kcontrol);
2413 int dir = get_amp_direction(kcontrol);
2414 unsigned int ofs = get_amp_offset(kcontrol);
2415 bool min_mute = get_amp_min_mute(kcontrol);
2416 u32 caps, val1, val2;
2418 if (size < 4 * sizeof(unsigned int))
2420 caps = query_amp_caps(codec, nid, dir);
2421 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2422 val2 = (val2 + 1) * 25;
2423 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
2425 val1 = ((int)val1) * ((int)val2);
2426 if (min_mute || (caps & AC_AMPCAP_MIN_MUTE))
2427 val2 |= TLV_DB_SCALE_MUTE;
2428 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
2430 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
2432 if (put_user(val1, _tlv + 2))
2434 if (put_user(val2, _tlv + 3))
2438 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_tlv);
2441 * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control
2442 * @codec: HD-audio codec
2443 * @nid: NID of a reference widget
2444 * @dir: #HDA_INPUT or #HDA_OUTPUT
2445 * @tlv: TLV data to be stored, at least 4 elements
2447 * Set (static) TLV data for a virtual master volume using the AMP caps
2448 * obtained from the reference NID.
2449 * The volume range is recalculated as if the max volume is 0dB.
2451 void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
2457 caps = query_amp_caps(codec, nid, dir);
2458 nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
2459 step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2460 step = (step + 1) * 25;
2461 tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
2462 tlv[1] = 2 * sizeof(unsigned int);
2463 tlv[2] = -nums * step;
2466 EXPORT_SYMBOL_HDA(snd_hda_set_vmaster_tlv);
2468 /* find a mixer control element with the given name */
2469 static struct snd_kcontrol *
2470 find_mixer_ctl(struct hda_codec *codec, const char *name, int dev, int idx)
2472 struct snd_ctl_elem_id id;
2473 memset(&id, 0, sizeof(id));
2474 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
2477 if (snd_BUG_ON(strlen(name) >= sizeof(id.name)))
2479 strcpy(id.name, name);
2480 return snd_ctl_find_id(codec->bus->card, &id);
2484 * snd_hda_find_mixer_ctl - Find a mixer control element with the given name
2485 * @codec: HD-audio codec
2486 * @name: ctl id name string
2488 * Get the control element with the given id string and IFACE_MIXER.
2490 struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
2493 return find_mixer_ctl(codec, name, 0, 0);
2495 EXPORT_SYMBOL_HDA(snd_hda_find_mixer_ctl);
2497 static int find_empty_mixer_ctl_idx(struct hda_codec *codec, const char *name,
2501 /* 16 ctlrs should be large enough */
2502 for (i = 0, idx = start_idx; i < 16; i++, idx++) {
2503 if (!find_mixer_ctl(codec, name, 0, idx))
2510 * snd_hda_ctl_add - Add a control element and assign to the codec
2511 * @codec: HD-audio codec
2512 * @nid: corresponding NID (optional)
2513 * @kctl: the control element to assign
2515 * Add the given control element to an array inside the codec instance.
2516 * All control elements belonging to a codec are supposed to be added
2517 * by this function so that a proper clean-up works at the free or
2518 * reconfiguration time.
2520 * If non-zero @nid is passed, the NID is assigned to the control element.
2521 * The assignment is shown in the codec proc file.
2523 * snd_hda_ctl_add() checks the control subdev id field whether
2524 * #HDA_SUBDEV_NID_FLAG bit is set. If set (and @nid is zero), the lower
2525 * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit
2526 * specifies if kctl->private_value is a HDA amplifier value.
2528 int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid,
2529 struct snd_kcontrol *kctl)
2532 unsigned short flags = 0;
2533 struct hda_nid_item *item;
2535 if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) {
2536 flags |= HDA_NID_ITEM_AMP;
2538 nid = get_amp_nid_(kctl->private_value);
2540 if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0)
2541 nid = kctl->id.subdevice & 0xffff;
2542 if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG))
2543 kctl->id.subdevice = 0;
2544 err = snd_ctl_add(codec->bus->card, kctl);
2547 item = snd_array_new(&codec->mixers);
2552 item->flags = flags;
2555 EXPORT_SYMBOL_HDA(snd_hda_ctl_add);
2558 * snd_hda_add_nid - Assign a NID to a control element
2559 * @codec: HD-audio codec
2560 * @nid: corresponding NID (optional)
2561 * @kctl: the control element to assign
2562 * @index: index to kctl
2564 * Add the given control element to an array inside the codec instance.
2565 * This function is used when #snd_hda_ctl_add cannot be used for 1:1
2566 * NID:KCTL mapping - for example "Capture Source" selector.
2568 int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl,
2569 unsigned int index, hda_nid_t nid)
2571 struct hda_nid_item *item;
2574 item = snd_array_new(&codec->nids);
2578 item->index = index;
2582 printk(KERN_ERR "hda-codec: no NID for mapping control %s:%d:%d\n",
2583 kctl->id.name, kctl->id.index, index);
2586 EXPORT_SYMBOL_HDA(snd_hda_add_nid);
2589 * snd_hda_ctls_clear - Clear all controls assigned to the given codec
2590 * @codec: HD-audio codec
2592 void snd_hda_ctls_clear(struct hda_codec *codec)
2595 struct hda_nid_item *items = codec->mixers.list;
2596 for (i = 0; i < codec->mixers.used; i++)
2597 snd_ctl_remove(codec->bus->card, items[i].kctl);
2598 snd_array_free(&codec->mixers);
2599 snd_array_free(&codec->nids);
2602 /* pseudo device locking
2603 * toggle card->shutdown to allow/disallow the device access (as a hack)
2605 int snd_hda_lock_devices(struct hda_bus *bus)
2607 struct snd_card *card = bus->card;
2608 struct hda_codec *codec;
2610 spin_lock(&card->files_lock);
2614 if (!list_empty(&card->ctl_files))
2617 list_for_each_entry(codec, &bus->codec_list, list) {
2619 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
2620 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
2623 if (cpcm->pcm->streams[0].substream_opened ||
2624 cpcm->pcm->streams[1].substream_opened)
2628 spin_unlock(&card->files_lock);
2634 spin_unlock(&card->files_lock);
2637 EXPORT_SYMBOL_HDA(snd_hda_lock_devices);
2639 void snd_hda_unlock_devices(struct hda_bus *bus)
2641 struct snd_card *card = bus->card;
2644 spin_lock(&card->files_lock);
2646 spin_unlock(&card->files_lock);
2648 EXPORT_SYMBOL_HDA(snd_hda_unlock_devices);
2651 * snd_hda_codec_reset - Clear all objects assigned to the codec
2652 * @codec: HD-audio codec
2654 * This frees the all PCM and control elements assigned to the codec, and
2655 * clears the caches and restores the pin default configurations.
2657 * When a device is being used, it returns -EBSY. If successfully freed,
2660 int snd_hda_codec_reset(struct hda_codec *codec)
2662 struct hda_bus *bus = codec->bus;
2663 struct snd_card *card = bus->card;
2666 if (snd_hda_lock_devices(bus) < 0)
2669 /* OK, let it free */
2670 cancel_delayed_work_sync(&codec->jackpoll_work);
2672 cancel_delayed_work_sync(&codec->power_work);
2673 flush_workqueue(bus->workq);
2675 snd_hda_ctls_clear(codec);
2677 for (i = 0; i < codec->num_pcms; i++) {
2678 if (codec->pcm_info[i].pcm) {
2679 snd_device_free(card, codec->pcm_info[i].pcm);
2680 clear_bit(codec->pcm_info[i].device,
2684 if (codec->patch_ops.free)
2685 codec->patch_ops.free(codec);
2686 memset(&codec->patch_ops, 0, sizeof(codec->patch_ops));
2687 snd_hda_jack_tbl_clear(codec);
2688 codec->proc_widget_hook = NULL;
2690 free_hda_cache(&codec->amp_cache);
2691 free_hda_cache(&codec->cmd_cache);
2692 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
2693 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
2694 /* free only driver_pins so that init_pins + user_pins are restored */
2695 snd_array_free(&codec->driver_pins);
2696 snd_array_free(&codec->cvt_setups);
2697 snd_array_free(&codec->spdif_out);
2698 snd_array_free(&codec->verbs);
2699 codec->num_pcms = 0;
2700 codec->pcm_info = NULL;
2701 codec->preset = NULL;
2702 codec->slave_dig_outs = NULL;
2703 codec->spdif_status_reset = 0;
2704 unload_parser(codec);
2705 module_put(codec->owner);
2706 codec->owner = NULL;
2708 /* allow device access again */
2709 snd_hda_unlock_devices(bus);
2713 typedef int (*map_slave_func_t)(void *, struct snd_kcontrol *);
2715 /* apply the function to all matching slave ctls in the mixer list */
2716 static int map_slaves(struct hda_codec *codec, const char * const *slaves,
2717 const char *suffix, map_slave_func_t func, void *data)
2719 struct hda_nid_item *items;
2720 const char * const *s;
2723 items = codec->mixers.list;
2724 for (i = 0; i < codec->mixers.used; i++) {
2725 struct snd_kcontrol *sctl = items[i].kctl;
2726 if (!sctl || sctl->id.iface != SNDRV_CTL_ELEM_IFACE_MIXER)
2728 for (s = slaves; *s; s++) {
2729 char tmpname[sizeof(sctl->id.name)];
2730 const char *name = *s;
2732 snprintf(tmpname, sizeof(tmpname), "%s %s",
2736 if (!strcmp(sctl->id.name, name)) {
2737 err = func(data, sctl);
2747 static int check_slave_present(void *data, struct snd_kcontrol *sctl)
2752 /* guess the value corresponding to 0dB */
2753 static int get_kctl_0dB_offset(struct snd_kcontrol *kctl, int *step_to_check)
2756 const int *tlv = NULL;
2759 if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
2760 /* FIXME: set_fs() hack for obtaining user-space TLV data */
2761 mm_segment_t fs = get_fs();
2763 if (!kctl->tlv.c(kctl, 0, sizeof(_tlv), _tlv))
2766 } else if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_READ)
2768 if (tlv && tlv[0] == SNDRV_CTL_TLVT_DB_SCALE) {
2770 step &= ~TLV_DB_SCALE_MUTE;
2773 if (*step_to_check && *step_to_check != step) {
2774 snd_printk(KERN_ERR "hda_codec: Mismatching dB step for vmaster slave (%d!=%d)\n",
2775 *step_to_check, step);
2778 *step_to_check = step;
2779 val = -tlv[2] / step;
2784 /* call kctl->put with the given value(s) */
2785 static int put_kctl_with_value(struct snd_kcontrol *kctl, int val)
2787 struct snd_ctl_elem_value *ucontrol;
2788 ucontrol = kzalloc(sizeof(*ucontrol), GFP_KERNEL);
2791 ucontrol->value.integer.value[0] = val;
2792 ucontrol->value.integer.value[1] = val;
2793 kctl->put(kctl, ucontrol);
2798 /* initialize the slave volume with 0dB */
2799 static int init_slave_0dB(void *data, struct snd_kcontrol *slave)
2801 int offset = get_kctl_0dB_offset(slave, data);
2803 put_kctl_with_value(slave, offset);
2807 /* unmute the slave */
2808 static int init_slave_unmute(void *data, struct snd_kcontrol *slave)
2810 return put_kctl_with_value(slave, 1);
2814 * snd_hda_add_vmaster - create a virtual master control and add slaves
2815 * @codec: HD-audio codec
2816 * @name: vmaster control name
2817 * @tlv: TLV data (optional)
2818 * @slaves: slave control names (optional)
2819 * @suffix: suffix string to each slave name (optional)
2820 * @init_slave_vol: initialize slaves to unmute/0dB
2821 * @ctl_ret: store the vmaster kcontrol in return
2823 * Create a virtual master control with the given name. The TLV data
2824 * must be either NULL or a valid data.
2826 * @slaves is a NULL-terminated array of strings, each of which is a
2827 * slave control name. All controls with these names are assigned to
2828 * the new virtual master control.
2830 * This function returns zero if successful or a negative error code.
2832 int __snd_hda_add_vmaster(struct hda_codec *codec, char *name,
2833 unsigned int *tlv, const char * const *slaves,
2834 const char *suffix, bool init_slave_vol,
2835 struct snd_kcontrol **ctl_ret)
2837 struct snd_kcontrol *kctl;
2843 err = map_slaves(codec, slaves, suffix, check_slave_present, NULL);
2845 snd_printdd("No slave found for %s\n", name);
2848 kctl = snd_ctl_make_virtual_master(name, tlv);
2851 err = snd_hda_ctl_add(codec, 0, kctl);
2855 err = map_slaves(codec, slaves, suffix,
2856 (map_slave_func_t)snd_ctl_add_slave, kctl);
2860 /* init with master mute & zero volume */
2861 put_kctl_with_value(kctl, 0);
2862 if (init_slave_vol) {
2864 map_slaves(codec, slaves, suffix,
2865 tlv ? init_slave_0dB : init_slave_unmute, &step);
2872 EXPORT_SYMBOL_HDA(__snd_hda_add_vmaster);
2875 * mute-LED control using vmaster
2877 static int vmaster_mute_mode_info(struct snd_kcontrol *kcontrol,
2878 struct snd_ctl_elem_info *uinfo)
2880 static const char * const texts[] = {
2881 "On", "Off", "Follow Master"
2885 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2887 uinfo->value.enumerated.items = 3;
2888 index = uinfo->value.enumerated.item;
2891 strcpy(uinfo->value.enumerated.name, texts[index]);
2895 static int vmaster_mute_mode_get(struct snd_kcontrol *kcontrol,
2896 struct snd_ctl_elem_value *ucontrol)
2898 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2899 ucontrol->value.enumerated.item[0] = hook->mute_mode;
2903 static int vmaster_mute_mode_put(struct snd_kcontrol *kcontrol,
2904 struct snd_ctl_elem_value *ucontrol)
2906 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2907 unsigned int old_mode = hook->mute_mode;
2909 hook->mute_mode = ucontrol->value.enumerated.item[0];
2910 if (hook->mute_mode > HDA_VMUTE_FOLLOW_MASTER)
2911 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
2912 if (old_mode == hook->mute_mode)
2914 snd_hda_sync_vmaster_hook(hook);
2918 static struct snd_kcontrol_new vmaster_mute_mode = {
2919 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2920 .name = "Mute-LED Mode",
2921 .info = vmaster_mute_mode_info,
2922 .get = vmaster_mute_mode_get,
2923 .put = vmaster_mute_mode_put,
2927 * Add a mute-LED hook with the given vmaster switch kctl
2928 * "Mute-LED Mode" control is automatically created and associated with
2931 int snd_hda_add_vmaster_hook(struct hda_codec *codec,
2932 struct hda_vmaster_mute_hook *hook,
2933 bool expose_enum_ctl)
2935 struct snd_kcontrol *kctl;
2937 if (!hook->hook || !hook->sw_kctl)
2939 snd_ctl_add_vmaster_hook(hook->sw_kctl, hook->hook, codec);
2940 hook->codec = codec;
2941 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
2942 if (!expose_enum_ctl)
2944 kctl = snd_ctl_new1(&vmaster_mute_mode, hook);
2947 return snd_hda_ctl_add(codec, 0, kctl);
2949 EXPORT_SYMBOL_HDA(snd_hda_add_vmaster_hook);
2952 * Call the hook with the current value for synchronization
2953 * Should be called in init callback
2955 void snd_hda_sync_vmaster_hook(struct hda_vmaster_mute_hook *hook)
2957 if (!hook->hook || !hook->codec)
2959 /* don't call vmaster hook in the destructor since it might have
2960 * been already destroyed
2962 if (hook->codec->bus->shutdown)
2964 switch (hook->mute_mode) {
2965 case HDA_VMUTE_FOLLOW_MASTER:
2966 snd_ctl_sync_vmaster_hook(hook->sw_kctl);
2969 hook->hook(hook->codec, hook->mute_mode);
2973 EXPORT_SYMBOL_HDA(snd_hda_sync_vmaster_hook);
2977 * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch
2979 * The control element is supposed to have the private_value field
2980 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2982 int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
2983 struct snd_ctl_elem_info *uinfo)
2985 int chs = get_amp_channels(kcontrol);
2987 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2988 uinfo->count = chs == 3 ? 2 : 1;
2989 uinfo->value.integer.min = 0;
2990 uinfo->value.integer.max = 1;
2993 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_info);
2996 * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch
2998 * The control element is supposed to have the private_value field
2999 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
3001 int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
3002 struct snd_ctl_elem_value *ucontrol)
3004 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3005 hda_nid_t nid = get_amp_nid(kcontrol);
3006 int chs = get_amp_channels(kcontrol);
3007 int dir = get_amp_direction(kcontrol);
3008 int idx = get_amp_index(kcontrol);
3009 long *valp = ucontrol->value.integer.value;
3012 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
3013 HDA_AMP_MUTE) ? 0 : 1;
3015 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
3016 HDA_AMP_MUTE) ? 0 : 1;
3019 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_get);
3022 * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch
3024 * The control element is supposed to have the private_value field
3025 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
3027 int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
3028 struct snd_ctl_elem_value *ucontrol)
3030 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3031 hda_nid_t nid = get_amp_nid(kcontrol);
3032 int chs = get_amp_channels(kcontrol);
3033 int dir = get_amp_direction(kcontrol);
3034 int idx = get_amp_index(kcontrol);
3035 long *valp = ucontrol->value.integer.value;
3038 snd_hda_power_up(codec);
3040 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
3042 *valp ? 0 : HDA_AMP_MUTE);
3046 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
3048 *valp ? 0 : HDA_AMP_MUTE);
3049 hda_call_check_power_status(codec, nid);
3050 snd_hda_power_down(codec);
3053 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put);
3056 * bound volume controls
3058 * bind multiple volumes (# indices, from 0)
3061 #define AMP_VAL_IDX_SHIFT 19
3062 #define AMP_VAL_IDX_MASK (0x0f<<19)
3065 * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control
3067 * The control element is supposed to have the private_value field
3068 * set up via HDA_BIND_MUTE*() macros.
3070 int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
3071 struct snd_ctl_elem_value *ucontrol)
3073 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3077 mutex_lock(&codec->control_mutex);
3078 pval = kcontrol->private_value;
3079 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
3080 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
3081 kcontrol->private_value = pval;
3082 mutex_unlock(&codec->control_mutex);
3085 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_get);
3088 * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control
3090 * The control element is supposed to have the private_value field
3091 * set up via HDA_BIND_MUTE*() macros.
3093 int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
3094 struct snd_ctl_elem_value *ucontrol)
3096 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3098 int i, indices, err = 0, change = 0;
3100 mutex_lock(&codec->control_mutex);
3101 pval = kcontrol->private_value;
3102 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
3103 for (i = 0; i < indices; i++) {
3104 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
3105 (i << AMP_VAL_IDX_SHIFT);
3106 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
3111 kcontrol->private_value = pval;
3112 mutex_unlock(&codec->control_mutex);
3113 return err < 0 ? err : change;
3115 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_put);
3118 * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control
3120 * The control element is supposed to have the private_value field
3121 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
3123 int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
3124 struct snd_ctl_elem_info *uinfo)
3126 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3127 struct hda_bind_ctls *c;
3130 mutex_lock(&codec->control_mutex);
3131 c = (struct hda_bind_ctls *)kcontrol->private_value;
3132 kcontrol->private_value = *c->values;
3133 err = c->ops->info(kcontrol, uinfo);
3134 kcontrol->private_value = (long)c;
3135 mutex_unlock(&codec->control_mutex);
3138 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_info);
3141 * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control
3143 * The control element is supposed to have the private_value field
3144 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
3146 int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
3147 struct snd_ctl_elem_value *ucontrol)
3149 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3150 struct hda_bind_ctls *c;
3153 mutex_lock(&codec->control_mutex);
3154 c = (struct hda_bind_ctls *)kcontrol->private_value;
3155 kcontrol->private_value = *c->values;
3156 err = c->ops->get(kcontrol, ucontrol);
3157 kcontrol->private_value = (long)c;
3158 mutex_unlock(&codec->control_mutex);
3161 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_get);
3164 * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control
3166 * The control element is supposed to have the private_value field
3167 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
3169 int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
3170 struct snd_ctl_elem_value *ucontrol)
3172 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3173 struct hda_bind_ctls *c;
3174 unsigned long *vals;
3175 int err = 0, change = 0;
3177 mutex_lock(&codec->control_mutex);
3178 c = (struct hda_bind_ctls *)kcontrol->private_value;
3179 for (vals = c->values; *vals; vals++) {
3180 kcontrol->private_value = *vals;
3181 err = c->ops->put(kcontrol, ucontrol);
3186 kcontrol->private_value = (long)c;
3187 mutex_unlock(&codec->control_mutex);
3188 return err < 0 ? err : change;
3190 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_put);
3193 * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control
3195 * The control element is supposed to have the private_value field
3196 * set up via HDA_BIND_VOL() macro.
3198 int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
3199 unsigned int size, unsigned int __user *tlv)
3201 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3202 struct hda_bind_ctls *c;
3205 mutex_lock(&codec->control_mutex);
3206 c = (struct hda_bind_ctls *)kcontrol->private_value;
3207 kcontrol->private_value = *c->values;
3208 err = c->ops->tlv(kcontrol, op_flag, size, tlv);
3209 kcontrol->private_value = (long)c;
3210 mutex_unlock(&codec->control_mutex);
3213 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_tlv);
3215 struct hda_ctl_ops snd_hda_bind_vol = {
3216 .info = snd_hda_mixer_amp_volume_info,
3217 .get = snd_hda_mixer_amp_volume_get,
3218 .put = snd_hda_mixer_amp_volume_put,
3219 .tlv = snd_hda_mixer_amp_tlv
3221 EXPORT_SYMBOL_HDA(snd_hda_bind_vol);
3223 struct hda_ctl_ops snd_hda_bind_sw = {
3224 .info = snd_hda_mixer_amp_switch_info,
3225 .get = snd_hda_mixer_amp_switch_get,
3226 .put = snd_hda_mixer_amp_switch_put,
3227 .tlv = snd_hda_mixer_amp_tlv
3229 EXPORT_SYMBOL_HDA(snd_hda_bind_sw);
3232 * SPDIF out controls
3235 static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
3236 struct snd_ctl_elem_info *uinfo)
3238 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
3243 static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
3244 struct snd_ctl_elem_value *ucontrol)
3246 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
3247 IEC958_AES0_NONAUDIO |
3248 IEC958_AES0_CON_EMPHASIS_5015 |
3249 IEC958_AES0_CON_NOT_COPYRIGHT;
3250 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
3251 IEC958_AES1_CON_ORIGINAL;
3255 static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
3256 struct snd_ctl_elem_value *ucontrol)
3258 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
3259 IEC958_AES0_NONAUDIO |
3260 IEC958_AES0_PRO_EMPHASIS_5015;
3264 static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
3265 struct snd_ctl_elem_value *ucontrol)
3267 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3268 int idx = kcontrol->private_value;
3269 struct hda_spdif_out *spdif;
3271 mutex_lock(&codec->spdif_mutex);
3272 spdif = snd_array_elem(&codec->spdif_out, idx);
3273 ucontrol->value.iec958.status[0] = spdif->status & 0xff;
3274 ucontrol->value.iec958.status[1] = (spdif->status >> 8) & 0xff;
3275 ucontrol->value.iec958.status[2] = (spdif->status >> 16) & 0xff;
3276 ucontrol->value.iec958.status[3] = (spdif->status >> 24) & 0xff;
3277 mutex_unlock(&codec->spdif_mutex);
3282 /* convert from SPDIF status bits to HDA SPDIF bits
3283 * bit 0 (DigEn) is always set zero (to be filled later)
3285 static unsigned short convert_from_spdif_status(unsigned int sbits)
3287 unsigned short val = 0;
3289 if (sbits & IEC958_AES0_PROFESSIONAL)
3290 val |= AC_DIG1_PROFESSIONAL;
3291 if (sbits & IEC958_AES0_NONAUDIO)
3292 val |= AC_DIG1_NONAUDIO;
3293 if (sbits & IEC958_AES0_PROFESSIONAL) {
3294 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
3295 IEC958_AES0_PRO_EMPHASIS_5015)
3296 val |= AC_DIG1_EMPHASIS;
3298 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
3299 IEC958_AES0_CON_EMPHASIS_5015)
3300 val |= AC_DIG1_EMPHASIS;
3301 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
3302 val |= AC_DIG1_COPYRIGHT;
3303 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
3304 val |= AC_DIG1_LEVEL;
3305 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
3310 /* convert to SPDIF status bits from HDA SPDIF bits
3312 static unsigned int convert_to_spdif_status(unsigned short val)
3314 unsigned int sbits = 0;
3316 if (val & AC_DIG1_NONAUDIO)
3317 sbits |= IEC958_AES0_NONAUDIO;
3318 if (val & AC_DIG1_PROFESSIONAL)
3319 sbits |= IEC958_AES0_PROFESSIONAL;
3320 if (sbits & IEC958_AES0_PROFESSIONAL) {
3321 if (val & AC_DIG1_EMPHASIS)
3322 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
3324 if (val & AC_DIG1_EMPHASIS)
3325 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
3326 if (!(val & AC_DIG1_COPYRIGHT))
3327 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
3328 if (val & AC_DIG1_LEVEL)
3329 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
3330 sbits |= val & (0x7f << 8);
3335 /* set digital convert verbs both for the given NID and its slaves */
3336 static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
3341 snd_hda_codec_write_cache(codec, nid, 0, verb, val);
3342 d = codec->slave_dig_outs;
3346 snd_hda_codec_write_cache(codec, *d, 0, verb, val);
3349 static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
3353 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1);
3355 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2);
3358 static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
3359 struct snd_ctl_elem_value *ucontrol)
3361 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3362 int idx = kcontrol->private_value;
3363 struct hda_spdif_out *spdif;
3368 mutex_lock(&codec->spdif_mutex);
3369 spdif = snd_array_elem(&codec->spdif_out, idx);
3371 spdif->status = ucontrol->value.iec958.status[0] |
3372 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
3373 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
3374 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
3375 val = convert_from_spdif_status(spdif->status);
3376 val |= spdif->ctls & 1;
3377 change = spdif->ctls != val;
3379 if (change && nid != (u16)-1)
3380 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
3381 mutex_unlock(&codec->spdif_mutex);
3385 #define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
3387 static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
3388 struct snd_ctl_elem_value *ucontrol)
3390 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3391 int idx = kcontrol->private_value;
3392 struct hda_spdif_out *spdif;
3394 mutex_lock(&codec->spdif_mutex);
3395 spdif = snd_array_elem(&codec->spdif_out, idx);
3396 ucontrol->value.integer.value[0] = spdif->ctls & AC_DIG1_ENABLE;
3397 mutex_unlock(&codec->spdif_mutex);
3401 static inline void set_spdif_ctls(struct hda_codec *codec, hda_nid_t nid,
3404 set_dig_out_convert(codec, nid, dig1, dig2);
3405 /* unmute amp switch (if any) */
3406 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
3407 (dig1 & AC_DIG1_ENABLE))
3408 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
3412 static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
3413 struct snd_ctl_elem_value *ucontrol)
3415 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3416 int idx = kcontrol->private_value;
3417 struct hda_spdif_out *spdif;
3422 mutex_lock(&codec->spdif_mutex);
3423 spdif = snd_array_elem(&codec->spdif_out, idx);
3425 val = spdif->ctls & ~AC_DIG1_ENABLE;
3426 if (ucontrol->value.integer.value[0])
3427 val |= AC_DIG1_ENABLE;
3428 change = spdif->ctls != val;
3430 if (change && nid != (u16)-1)
3431 set_spdif_ctls(codec, nid, val & 0xff, -1);
3432 mutex_unlock(&codec->spdif_mutex);
3436 static struct snd_kcontrol_new dig_mixes[] = {
3438 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3439 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3440 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
3441 .info = snd_hda_spdif_mask_info,
3442 .get = snd_hda_spdif_cmask_get,
3445 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3446 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3447 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK),
3448 .info = snd_hda_spdif_mask_info,
3449 .get = snd_hda_spdif_pmask_get,
3452 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3453 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
3454 .info = snd_hda_spdif_mask_info,
3455 .get = snd_hda_spdif_default_get,
3456 .put = snd_hda_spdif_default_put,
3459 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3460 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
3461 .info = snd_hda_spdif_out_switch_info,
3462 .get = snd_hda_spdif_out_switch_get,
3463 .put = snd_hda_spdif_out_switch_put,
3469 * snd_hda_create_dig_out_ctls - create Output SPDIF-related controls
3470 * @codec: the HDA codec
3471 * @associated_nid: NID that new ctls associated with
3472 * @cvt_nid: converter NID
3473 * @type: HDA_PCM_TYPE_*
3474 * Creates controls related with the digital output.
3475 * Called from each patch supporting the digital out.
3477 * Returns 0 if successful, or a negative error code.
3479 int snd_hda_create_dig_out_ctls(struct hda_codec *codec,
3480 hda_nid_t associated_nid,
3485 struct snd_kcontrol *kctl;
3486 struct snd_kcontrol_new *dig_mix;
3488 const int spdif_index = 16;
3489 struct hda_spdif_out *spdif;
3490 struct hda_bus *bus = codec->bus;
3492 if (bus->primary_dig_out_type == HDA_PCM_TYPE_HDMI &&
3493 type == HDA_PCM_TYPE_SPDIF) {
3495 } else if (bus->primary_dig_out_type == HDA_PCM_TYPE_SPDIF &&
3496 type == HDA_PCM_TYPE_HDMI) {
3497 /* suppose a single SPDIF device */
3498 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
3499 kctl = find_mixer_ctl(codec, dig_mix->name, 0, 0);
3502 kctl->id.index = spdif_index;
3504 bus->primary_dig_out_type = HDA_PCM_TYPE_HDMI;
3506 if (!bus->primary_dig_out_type)
3507 bus->primary_dig_out_type = type;
3509 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Playback Switch", idx);
3511 printk(KERN_ERR "hda_codec: too many IEC958 outputs\n");
3514 spdif = snd_array_new(&codec->spdif_out);
3517 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
3518 kctl = snd_ctl_new1(dig_mix, codec);
3521 kctl->id.index = idx;
3522 kctl->private_value = codec->spdif_out.used - 1;
3523 err = snd_hda_ctl_add(codec, associated_nid, kctl);
3527 spdif->nid = cvt_nid;
3528 spdif->ctls = snd_hda_codec_read(codec, cvt_nid, 0,
3529 AC_VERB_GET_DIGI_CONVERT_1, 0);
3530 spdif->status = convert_to_spdif_status(spdif->ctls);
3533 EXPORT_SYMBOL_HDA(snd_hda_create_dig_out_ctls);
3535 /* get the hda_spdif_out entry from the given NID
3536 * call within spdif_mutex lock
3538 struct hda_spdif_out *snd_hda_spdif_out_of_nid(struct hda_codec *codec,
3542 for (i = 0; i < codec->spdif_out.used; i++) {
3543 struct hda_spdif_out *spdif =
3544 snd_array_elem(&codec->spdif_out, i);
3545 if (spdif->nid == nid)
3550 EXPORT_SYMBOL_HDA(snd_hda_spdif_out_of_nid);
3552 void snd_hda_spdif_ctls_unassign(struct hda_codec *codec, int idx)
3554 struct hda_spdif_out *spdif;
3556 mutex_lock(&codec->spdif_mutex);
3557 spdif = snd_array_elem(&codec->spdif_out, idx);
3558 spdif->nid = (u16)-1;
3559 mutex_unlock(&codec->spdif_mutex);
3561 EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_unassign);
3563 void snd_hda_spdif_ctls_assign(struct hda_codec *codec, int idx, hda_nid_t nid)
3565 struct hda_spdif_out *spdif;
3568 mutex_lock(&codec->spdif_mutex);
3569 spdif = snd_array_elem(&codec->spdif_out, idx);
3570 if (spdif->nid != nid) {
3573 set_spdif_ctls(codec, nid, val & 0xff, (val >> 8) & 0xff);
3575 mutex_unlock(&codec->spdif_mutex);
3577 EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_assign);
3580 * SPDIF sharing with analog output
3582 static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
3583 struct snd_ctl_elem_value *ucontrol)
3585 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3586 ucontrol->value.integer.value[0] = mout->share_spdif;
3590 static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
3591 struct snd_ctl_elem_value *ucontrol)
3593 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3594 mout->share_spdif = !!ucontrol->value.integer.value[0];
3598 static struct snd_kcontrol_new spdif_share_sw = {
3599 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3600 .name = "IEC958 Default PCM Playback Switch",
3601 .info = snd_ctl_boolean_mono_info,
3602 .get = spdif_share_sw_get,
3603 .put = spdif_share_sw_put,
3607 * snd_hda_create_spdif_share_sw - create Default PCM switch
3608 * @codec: the HDA codec
3609 * @mout: multi-out instance
3611 int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
3612 struct hda_multi_out *mout)
3614 struct snd_kcontrol *kctl;
3616 if (!mout->dig_out_nid)
3619 kctl = snd_ctl_new1(&spdif_share_sw, mout);
3622 /* ATTENTION: here mout is passed as private_data, instead of codec */
3623 return snd_hda_ctl_add(codec, mout->dig_out_nid, kctl);
3625 EXPORT_SYMBOL_HDA(snd_hda_create_spdif_share_sw);
3631 #define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
3633 static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
3634 struct snd_ctl_elem_value *ucontrol)
3636 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3638 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
3642 static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
3643 struct snd_ctl_elem_value *ucontrol)
3645 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3646 hda_nid_t nid = kcontrol->private_value;
3647 unsigned int val = !!ucontrol->value.integer.value[0];
3650 mutex_lock(&codec->spdif_mutex);
3651 change = codec->spdif_in_enable != val;
3653 codec->spdif_in_enable = val;
3654 snd_hda_codec_write_cache(codec, nid, 0,
3655 AC_VERB_SET_DIGI_CONVERT_1, val);
3657 mutex_unlock(&codec->spdif_mutex);
3661 static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
3662 struct snd_ctl_elem_value *ucontrol)
3664 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3665 hda_nid_t nid = kcontrol->private_value;
3669 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0);
3670 sbits = convert_to_spdif_status(val);
3671 ucontrol->value.iec958.status[0] = sbits;
3672 ucontrol->value.iec958.status[1] = sbits >> 8;
3673 ucontrol->value.iec958.status[2] = sbits >> 16;
3674 ucontrol->value.iec958.status[3] = sbits >> 24;
3678 static struct snd_kcontrol_new dig_in_ctls[] = {
3680 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3681 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH),
3682 .info = snd_hda_spdif_in_switch_info,
3683 .get = snd_hda_spdif_in_switch_get,
3684 .put = snd_hda_spdif_in_switch_put,
3687 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3688 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3689 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
3690 .info = snd_hda_spdif_mask_info,
3691 .get = snd_hda_spdif_in_status_get,
3697 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
3698 * @codec: the HDA codec
3699 * @nid: audio in widget NID
3701 * Creates controls related with the SPDIF input.
3702 * Called from each patch supporting the SPDIF in.
3704 * Returns 0 if successful, or a negative error code.
3706 int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
3709 struct snd_kcontrol *kctl;
3710 struct snd_kcontrol_new *dig_mix;
3713 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Capture Switch", 0);
3715 printk(KERN_ERR "hda_codec: too many IEC958 inputs\n");
3718 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
3719 kctl = snd_ctl_new1(dig_mix, codec);
3722 kctl->private_value = nid;
3723 err = snd_hda_ctl_add(codec, nid, kctl);
3727 codec->spdif_in_enable =
3728 snd_hda_codec_read(codec, nid, 0,
3729 AC_VERB_GET_DIGI_CONVERT_1, 0) &
3733 EXPORT_SYMBOL_HDA(snd_hda_create_spdif_in_ctls);
3739 /* build a 31bit cache key with the widget id and the command parameter */
3740 #define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
3741 #define get_cmd_cache_nid(key) ((key) & 0xff)
3742 #define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
3745 * snd_hda_codec_write_cache - send a single command with caching
3746 * @codec: the HDA codec
3747 * @nid: NID to send the command
3748 * @flags: optional bit flags
3749 * @verb: the verb to send
3750 * @parm: the parameter for the verb
3752 * Send a single command without waiting for response.
3754 * Returns 0 if successful, or a negative error code.
3756 int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
3757 int flags, unsigned int verb, unsigned int parm)
3760 struct hda_cache_head *c;
3762 unsigned int cache_only;
3764 cache_only = codec->cached_write;
3766 err = snd_hda_codec_write(codec, nid, flags, verb, parm);
3771 /* parm may contain the verb stuff for get/set amp */
3772 verb = verb | (parm >> 8);
3774 key = build_cmd_cache_key(nid, verb);
3775 mutex_lock(&codec->bus->cmd_mutex);
3776 c = get_alloc_hash(&codec->cmd_cache, key);
3779 c->dirty = cache_only;
3781 mutex_unlock(&codec->bus->cmd_mutex);
3784 EXPORT_SYMBOL_HDA(snd_hda_codec_write_cache);
3787 * snd_hda_codec_update_cache - check cache and write the cmd only when needed
3788 * @codec: the HDA codec
3789 * @nid: NID to send the command
3790 * @flags: optional bit flags
3791 * @verb: the verb to send
3792 * @parm: the parameter for the verb
3794 * This function works like snd_hda_codec_write_cache(), but it doesn't send
3795 * command if the parameter is already identical with the cached value.
3796 * If not, it sends the command and refreshes the cache.
3798 * Returns 0 if successful, or a negative error code.
3800 int snd_hda_codec_update_cache(struct hda_codec *codec, hda_nid_t nid,
3801 int flags, unsigned int verb, unsigned int parm)
3803 struct hda_cache_head *c;
3806 /* parm may contain the verb stuff for get/set amp */
3807 verb = verb | (parm >> 8);
3809 key = build_cmd_cache_key(nid, verb);
3810 mutex_lock(&codec->bus->cmd_mutex);
3811 c = get_hash(&codec->cmd_cache, key);
3812 if (c && c->val == parm) {
3813 mutex_unlock(&codec->bus->cmd_mutex);
3816 mutex_unlock(&codec->bus->cmd_mutex);
3817 return snd_hda_codec_write_cache(codec, nid, flags, verb, parm);
3819 EXPORT_SYMBOL_HDA(snd_hda_codec_update_cache);
3822 * snd_hda_codec_resume_cache - Resume the all commands from the cache
3823 * @codec: HD-audio codec
3825 * Execute all verbs recorded in the command caches to resume.
3827 void snd_hda_codec_resume_cache(struct hda_codec *codec)
3831 mutex_lock(&codec->hash_mutex);
3832 codec->cached_write = 0;
3833 for (i = 0; i < codec->cmd_cache.buf.used; i++) {
3834 struct hda_cache_head *buffer;
3837 buffer = snd_array_elem(&codec->cmd_cache.buf, i);
3844 mutex_unlock(&codec->hash_mutex);
3845 snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
3846 get_cmd_cache_cmd(key), buffer->val);
3847 mutex_lock(&codec->hash_mutex);
3849 mutex_unlock(&codec->hash_mutex);
3851 EXPORT_SYMBOL_HDA(snd_hda_codec_resume_cache);
3854 * snd_hda_sequence_write_cache - sequence writes with caching
3855 * @codec: the HDA codec
3856 * @seq: VERB array to send
3858 * Send the commands sequentially from the given array.
3859 * Thte commands are recorded on cache for power-save and resume.
3860 * The array must be terminated with NID=0.
3862 void snd_hda_sequence_write_cache(struct hda_codec *codec,
3863 const struct hda_verb *seq)
3865 for (; seq->nid; seq++)
3866 snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
3869 EXPORT_SYMBOL_HDA(snd_hda_sequence_write_cache);
3872 * snd_hda_codec_flush_cache - Execute all pending (cached) amps / verbs
3873 * @codec: HD-audio codec
3875 void snd_hda_codec_flush_cache(struct hda_codec *codec)
3877 snd_hda_codec_resume_amp(codec);
3878 snd_hda_codec_resume_cache(codec);
3880 EXPORT_SYMBOL_HDA(snd_hda_codec_flush_cache);
3882 void snd_hda_codec_set_power_to_all(struct hda_codec *codec, hda_nid_t fg,
3883 unsigned int power_state)
3885 hda_nid_t nid = codec->start_nid;
3888 for (i = 0; i < codec->num_nodes; i++, nid++) {
3889 unsigned int wcaps = get_wcaps(codec, nid);
3890 unsigned int state = power_state;
3891 if (!(wcaps & AC_WCAP_POWER))
3893 if (codec->power_filter) {
3894 state = codec->power_filter(codec, nid, power_state);
3895 if (state != power_state && power_state == AC_PWRST_D3)
3898 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE,
3902 EXPORT_SYMBOL_HDA(snd_hda_codec_set_power_to_all);
3905 * supported power states check
3907 static bool snd_hda_codec_get_supported_ps(struct hda_codec *codec, hda_nid_t fg,
3908 unsigned int power_state)
3910 int sup = snd_hda_param_read(codec, fg, AC_PAR_POWER_STATE);
3914 if (sup & power_state)
3921 * wait until the state is reached, returns the current state
3923 static unsigned int hda_sync_power_state(struct hda_codec *codec,
3925 unsigned int power_state)
3927 unsigned long end_time = jiffies + msecs_to_jiffies(500);
3928 unsigned int state, actual_state;
3931 state = snd_hda_codec_read(codec, fg, 0,
3932 AC_VERB_GET_POWER_STATE, 0);
3933 if (state & AC_PWRST_ERROR)
3935 actual_state = (state >> 4) & 0x0f;
3936 if (actual_state == power_state)
3938 if (time_after_eq(jiffies, end_time))
3940 /* wait until the codec reachs to the target state */
3946 /* don't power down the widget if it controls eapd and EAPD_BTLENABLE is set */
3947 unsigned int snd_hda_codec_eapd_power_filter(struct hda_codec *codec,
3949 unsigned int power_state)
3951 if (power_state == AC_PWRST_D3 &&
3952 get_wcaps_type(get_wcaps(codec, nid)) == AC_WID_PIN &&
3953 (snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD)) {
3954 int eapd = snd_hda_codec_read(codec, nid, 0,
3955 AC_VERB_GET_EAPD_BTLENABLE, 0);
3961 EXPORT_SYMBOL_HDA(snd_hda_codec_eapd_power_filter);
3964 * set power state of the codec, and return the power state
3966 static unsigned int hda_set_power_state(struct hda_codec *codec,
3967 unsigned int power_state)
3969 hda_nid_t fg = codec->afg ? codec->afg : codec->mfg;
3974 /* this delay seems necessary to avoid click noise at power-down */
3975 if (power_state == AC_PWRST_D3) {
3976 /* transition time less than 10ms for power down */
3977 msleep(codec->epss ? 10 : 100);
3978 flags = HDA_RW_NO_RESPONSE_FALLBACK;
3981 /* repeat power states setting at most 10 times*/
3982 for (count = 0; count < 10; count++) {
3983 if (codec->patch_ops.set_power_state)
3984 codec->patch_ops.set_power_state(codec, fg,
3987 snd_hda_codec_read(codec, fg, flags,
3988 AC_VERB_SET_POWER_STATE,
3990 snd_hda_codec_set_power_to_all(codec, fg, power_state);
3992 state = hda_sync_power_state(codec, fg, power_state);
3993 if (!(state & AC_PWRST_ERROR))
4000 /* sync power states of all widgets;
4001 * this is called at the end of codec parsing
4003 static void sync_power_up_states(struct hda_codec *codec)
4005 hda_nid_t nid = codec->start_nid;
4008 /* don't care if no filter is used */
4009 if (!codec->power_filter)
4012 for (i = 0; i < codec->num_nodes; i++, nid++) {
4013 unsigned int wcaps = get_wcaps(codec, nid);
4014 unsigned int target;
4015 if (!(wcaps & AC_WCAP_POWER))
4017 target = codec->power_filter(codec, nid, AC_PWRST_D0);
4018 if (target == AC_PWRST_D0)
4020 if (!snd_hda_check_power_state(codec, nid, target))
4021 snd_hda_codec_write(codec, nid, 0,
4022 AC_VERB_SET_POWER_STATE, target);
4026 #ifdef CONFIG_SND_HDA_HWDEP
4027 /* execute additional init verbs */
4028 static void hda_exec_init_verbs(struct hda_codec *codec)
4030 if (codec->init_verbs.list)
4031 snd_hda_sequence_write(codec, codec->init_verbs.list);
4034 static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
4039 * call suspend and power-down; used both from PM and power-save
4040 * this function returns the power state in the end
4042 static unsigned int hda_call_codec_suspend(struct hda_codec *codec, bool in_wq)
4048 if (codec->patch_ops.suspend)
4049 codec->patch_ops.suspend(codec);
4050 hda_cleanup_all_streams(codec);
4051 state = hda_set_power_state(codec, AC_PWRST_D3);
4052 /* Cancel delayed work if we aren't currently running from it. */
4054 cancel_delayed_work_sync(&codec->power_work);
4055 spin_lock(&codec->power_lock);
4056 snd_hda_update_power_acct(codec);
4057 trace_hda_power_down(codec);
4058 codec->power_on = 0;
4059 codec->power_transition = 0;
4060 codec->power_jiffies = jiffies;
4061 spin_unlock(&codec->power_lock);
4066 /* mark all entries of cmd and amp caches dirty */
4067 static void hda_mark_cmd_cache_dirty(struct hda_codec *codec)
4070 for (i = 0; i < codec->cmd_cache.buf.used; i++) {
4071 struct hda_cache_head *cmd;
4072 cmd = snd_array_elem(&codec->cmd_cache.buf, i);
4075 for (i = 0; i < codec->amp_cache.buf.used; i++) {
4076 struct hda_amp_info *amp;
4077 amp = snd_array_elem(&codec->amp_cache.buf, i);
4078 amp->head.dirty = 1;
4083 * kick up codec; used both from PM and power-save
4085 static void hda_call_codec_resume(struct hda_codec *codec)
4089 hda_mark_cmd_cache_dirty(codec);
4091 /* set as if powered on for avoiding re-entering the resume
4092 * in the resume / power-save sequence
4094 hda_keep_power_on(codec);
4095 hda_set_power_state(codec, AC_PWRST_D0);
4096 restore_shutup_pins(codec);
4097 hda_exec_init_verbs(codec);
4098 snd_hda_jack_set_dirty_all(codec);
4099 if (codec->patch_ops.resume)
4100 codec->patch_ops.resume(codec);
4102 if (codec->patch_ops.init)
4103 codec->patch_ops.init(codec);
4104 snd_hda_codec_resume_amp(codec);
4105 snd_hda_codec_resume_cache(codec);
4108 if (codec->jackpoll_interval)
4109 hda_jackpoll_work(&codec->jackpoll_work.work);
4111 snd_hda_jack_report_sync(codec);
4114 snd_hda_power_down(codec); /* flag down before returning */
4116 #endif /* CONFIG_PM */
4120 * snd_hda_build_controls - build mixer controls
4123 * Creates mixer controls for each codec included in the bus.
4125 * Returns 0 if successful, otherwise a negative error code.
4127 int snd_hda_build_controls(struct hda_bus *bus)
4129 struct hda_codec *codec;
4131 list_for_each_entry(codec, &bus->codec_list, list) {
4132 int err = snd_hda_codec_build_controls(codec);
4134 printk(KERN_ERR "hda_codec: cannot build controls "
4135 "for #%d (error %d)\n", codec->addr, err);
4136 err = snd_hda_codec_reset(codec);
4139 "hda_codec: cannot revert codec\n");
4146 EXPORT_SYMBOL_HDA(snd_hda_build_controls);
4149 * add standard channel maps if not specified
4151 static int add_std_chmaps(struct hda_codec *codec)
4155 for (i = 0; i < codec->num_pcms; i++) {
4156 for (str = 0; str < 2; str++) {
4157 struct snd_pcm *pcm = codec->pcm_info[i].pcm;
4158 struct hda_pcm_stream *hinfo =
4159 &codec->pcm_info[i].stream[str];
4160 struct snd_pcm_chmap *chmap;
4161 const struct snd_pcm_chmap_elem *elem;
4163 if (codec->pcm_info[i].own_chmap)
4165 if (!pcm || !hinfo->substreams)
4167 elem = hinfo->chmap ? hinfo->chmap : snd_pcm_std_chmaps;
4168 err = snd_pcm_add_chmap_ctls(pcm, str, elem,
4169 hinfo->channels_max,
4173 chmap->channel_mask = SND_PCM_CHMAP_MASK_2468;
4179 /* default channel maps for 2.1 speakers;
4180 * since HD-audio supports only stereo, odd number channels are omitted
4182 const struct snd_pcm_chmap_elem snd_pcm_2_1_chmaps[] = {
4184 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } },
4186 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
4187 SNDRV_CHMAP_LFE, SNDRV_CHMAP_LFE } },
4190 EXPORT_SYMBOL_GPL(snd_pcm_2_1_chmaps);
4192 int snd_hda_codec_build_controls(struct hda_codec *codec)
4195 hda_exec_init_verbs(codec);
4196 /* continue to initialize... */
4197 if (codec->patch_ops.init)
4198 err = codec->patch_ops.init(codec);
4199 if (!err && codec->patch_ops.build_controls)
4200 err = codec->patch_ops.build_controls(codec);
4204 /* we create chmaps here instead of build_pcms */
4205 err = add_std_chmaps(codec);
4209 if (codec->jackpoll_interval)
4210 hda_jackpoll_work(&codec->jackpoll_work.work);
4212 snd_hda_jack_report_sync(codec); /* call at the last init point */
4213 sync_power_up_states(codec);
4220 struct hda_rate_tbl {
4222 unsigned int alsa_bits;
4223 unsigned int hda_fmt;
4226 /* rate = base * mult / div */
4227 #define HDA_RATE(base, mult, div) \
4228 (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \
4229 (((div) - 1) << AC_FMT_DIV_SHIFT))
4231 static struct hda_rate_tbl rate_bits[] = {
4232 /* rate in Hz, ALSA rate bitmask, HDA format value */
4234 /* autodetected value used in snd_hda_query_supported_pcm */
4235 { 8000, SNDRV_PCM_RATE_8000, HDA_RATE(48, 1, 6) },
4236 { 11025, SNDRV_PCM_RATE_11025, HDA_RATE(44, 1, 4) },
4237 { 16000, SNDRV_PCM_RATE_16000, HDA_RATE(48, 1, 3) },
4238 { 22050, SNDRV_PCM_RATE_22050, HDA_RATE(44, 1, 2) },
4239 { 32000, SNDRV_PCM_RATE_32000, HDA_RATE(48, 2, 3) },
4240 { 44100, SNDRV_PCM_RATE_44100, HDA_RATE(44, 1, 1) },
4241 { 48000, SNDRV_PCM_RATE_48000, HDA_RATE(48, 1, 1) },
4242 { 88200, SNDRV_PCM_RATE_88200, HDA_RATE(44, 2, 1) },
4243 { 96000, SNDRV_PCM_RATE_96000, HDA_RATE(48, 2, 1) },
4244 { 176400, SNDRV_PCM_RATE_176400, HDA_RATE(44, 4, 1) },
4245 { 192000, SNDRV_PCM_RATE_192000, HDA_RATE(48, 4, 1) },
4246 #define AC_PAR_PCM_RATE_BITS 11
4247 /* up to bits 10, 384kHZ isn't supported properly */
4249 /* not autodetected value */
4250 { 9600, SNDRV_PCM_RATE_KNOT, HDA_RATE(48, 1, 5) },
4252 { 0 } /* terminator */
4256 * snd_hda_calc_stream_format - calculate format bitset
4257 * @rate: the sample rate
4258 * @channels: the number of channels
4259 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
4260 * @maxbps: the max. bps
4262 * Calculate the format bitset from the given rate, channels and th PCM format.
4264 * Return zero if invalid.
4266 unsigned int snd_hda_calc_stream_format(unsigned int rate,
4267 unsigned int channels,
4268 unsigned int format,
4269 unsigned int maxbps,
4270 unsigned short spdif_ctls)
4273 unsigned int val = 0;
4275 for (i = 0; rate_bits[i].hz; i++)
4276 if (rate_bits[i].hz == rate) {
4277 val = rate_bits[i].hda_fmt;
4280 if (!rate_bits[i].hz) {
4281 snd_printdd("invalid rate %d\n", rate);
4285 if (channels == 0 || channels > 8) {
4286 snd_printdd("invalid channels %d\n", channels);
4289 val |= channels - 1;
4291 switch (snd_pcm_format_width(format)) {
4293 val |= AC_FMT_BITS_8;
4296 val |= AC_FMT_BITS_16;
4301 if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE)
4302 val |= AC_FMT_BITS_32;
4303 else if (maxbps >= 24)
4304 val |= AC_FMT_BITS_24;
4306 val |= AC_FMT_BITS_20;
4309 snd_printdd("invalid format width %d\n",
4310 snd_pcm_format_width(format));
4314 if (spdif_ctls & AC_DIG1_NONAUDIO)
4315 val |= AC_FMT_TYPE_NON_PCM;
4319 EXPORT_SYMBOL_HDA(snd_hda_calc_stream_format);
4321 static unsigned int get_pcm_param(struct hda_codec *codec, hda_nid_t nid,
4324 unsigned int val = 0;
4325 if (nid != codec->afg &&
4326 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD))
4327 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
4328 if (!val || val == -1)
4329 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
4330 if (!val || val == -1)
4335 static unsigned int query_pcm_param(struct hda_codec *codec, hda_nid_t nid)
4337 return query_caps_hash(codec, nid, 0, HDA_HASH_PARPCM_KEY(nid),
4341 static unsigned int get_stream_param(struct hda_codec *codec, hda_nid_t nid,
4344 unsigned int streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
4345 if (!streams || streams == -1)
4346 streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
4347 if (!streams || streams == -1)
4352 static unsigned int query_stream_param(struct hda_codec *codec, hda_nid_t nid)
4354 return query_caps_hash(codec, nid, 0, HDA_HASH_PARSTR_KEY(nid),
4359 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
4360 * @codec: the HDA codec
4361 * @nid: NID to query
4362 * @ratesp: the pointer to store the detected rate bitflags
4363 * @formatsp: the pointer to store the detected formats
4364 * @bpsp: the pointer to store the detected format widths
4366 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
4367 * or @bsps argument is ignored.
4369 * Returns 0 if successful, otherwise a negative error code.
4371 int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
4372 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
4374 unsigned int i, val, wcaps;
4376 wcaps = get_wcaps(codec, nid);
4377 val = query_pcm_param(codec, nid);
4381 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
4383 rates |= rate_bits[i].alsa_bits;
4386 snd_printk(KERN_ERR "hda_codec: rates == 0 "
4387 "(nid=0x%x, val=0x%x, ovrd=%i)\n",
4389 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0);
4395 if (formatsp || bpsp) {
4397 unsigned int streams, bps;
4399 streams = query_stream_param(codec, nid);
4404 if (streams & AC_SUPFMT_PCM) {
4405 if (val & AC_SUPPCM_BITS_8) {
4406 formats |= SNDRV_PCM_FMTBIT_U8;
4409 if (val & AC_SUPPCM_BITS_16) {
4410 formats |= SNDRV_PCM_FMTBIT_S16_LE;
4413 if (wcaps & AC_WCAP_DIGITAL) {
4414 if (val & AC_SUPPCM_BITS_32)
4415 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
4416 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
4417 formats |= SNDRV_PCM_FMTBIT_S32_LE;
4418 if (val & AC_SUPPCM_BITS_24)
4420 else if (val & AC_SUPPCM_BITS_20)
4422 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
4423 AC_SUPPCM_BITS_32)) {
4424 formats |= SNDRV_PCM_FMTBIT_S32_LE;
4425 if (val & AC_SUPPCM_BITS_32)
4427 else if (val & AC_SUPPCM_BITS_24)
4429 else if (val & AC_SUPPCM_BITS_20)
4433 #if 0 /* FIXME: CS4206 doesn't work, which is the only codec supporting float */
4434 if (streams & AC_SUPFMT_FLOAT32) {
4435 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
4440 if (streams == AC_SUPFMT_AC3) {
4441 /* should be exclusive */
4442 /* temporary hack: we have still no proper support
4443 * for the direct AC3 stream...
4445 formats |= SNDRV_PCM_FMTBIT_U8;
4449 snd_printk(KERN_ERR "hda_codec: formats == 0 "
4450 "(nid=0x%x, val=0x%x, ovrd=%i, "
4453 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0,
4458 *formatsp = formats;
4465 EXPORT_SYMBOL_HDA(snd_hda_query_supported_pcm);
4468 * snd_hda_is_supported_format - Check the validity of the format
4469 * @codec: HD-audio codec
4470 * @nid: NID to check
4471 * @format: the HD-audio format value to check
4473 * Check whether the given node supports the format value.
4475 * Returns 1 if supported, 0 if not.
4477 int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
4478 unsigned int format)
4481 unsigned int val = 0, rate, stream;
4483 val = query_pcm_param(codec, nid);
4487 rate = format & 0xff00;
4488 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
4489 if (rate_bits[i].hda_fmt == rate) {
4494 if (i >= AC_PAR_PCM_RATE_BITS)
4497 stream = query_stream_param(codec, nid);
4501 if (stream & AC_SUPFMT_PCM) {
4502 switch (format & 0xf0) {
4504 if (!(val & AC_SUPPCM_BITS_8))
4508 if (!(val & AC_SUPPCM_BITS_16))
4512 if (!(val & AC_SUPPCM_BITS_20))
4516 if (!(val & AC_SUPPCM_BITS_24))
4520 if (!(val & AC_SUPPCM_BITS_32))
4527 /* FIXME: check for float32 and AC3? */
4532 EXPORT_SYMBOL_HDA(snd_hda_is_supported_format);
4537 static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
4538 struct hda_codec *codec,
4539 struct snd_pcm_substream *substream)
4544 static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
4545 struct hda_codec *codec,
4546 unsigned int stream_tag,
4547 unsigned int format,
4548 struct snd_pcm_substream *substream)
4550 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4554 static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
4555 struct hda_codec *codec,
4556 struct snd_pcm_substream *substream)
4558 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
4562 static int set_pcm_default_values(struct hda_codec *codec,
4563 struct hda_pcm_stream *info)
4567 /* query support PCM information from the given NID */
4568 if (info->nid && (!info->rates || !info->formats)) {
4569 err = snd_hda_query_supported_pcm(codec, info->nid,
4570 info->rates ? NULL : &info->rates,
4571 info->formats ? NULL : &info->formats,
4572 info->maxbps ? NULL : &info->maxbps);
4576 if (info->ops.open == NULL)
4577 info->ops.open = hda_pcm_default_open_close;
4578 if (info->ops.close == NULL)
4579 info->ops.close = hda_pcm_default_open_close;
4580 if (info->ops.prepare == NULL) {
4581 if (snd_BUG_ON(!info->nid))
4583 info->ops.prepare = hda_pcm_default_prepare;
4585 if (info->ops.cleanup == NULL) {
4586 if (snd_BUG_ON(!info->nid))
4588 info->ops.cleanup = hda_pcm_default_cleanup;
4594 * codec prepare/cleanup entries
4596 int snd_hda_codec_prepare(struct hda_codec *codec,
4597 struct hda_pcm_stream *hinfo,
4598 unsigned int stream,
4599 unsigned int format,
4600 struct snd_pcm_substream *substream)
4603 mutex_lock(&codec->bus->prepare_mutex);
4604 ret = hinfo->ops.prepare(hinfo, codec, stream, format, substream);
4606 purify_inactive_streams(codec);
4607 mutex_unlock(&codec->bus->prepare_mutex);
4610 EXPORT_SYMBOL_HDA(snd_hda_codec_prepare);
4612 void snd_hda_codec_cleanup(struct hda_codec *codec,
4613 struct hda_pcm_stream *hinfo,
4614 struct snd_pcm_substream *substream)
4616 mutex_lock(&codec->bus->prepare_mutex);
4617 hinfo->ops.cleanup(hinfo, codec, substream);
4618 mutex_unlock(&codec->bus->prepare_mutex);
4620 EXPORT_SYMBOL_HDA(snd_hda_codec_cleanup);
4623 const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = {
4624 "Audio", "SPDIF", "HDMI", "Modem"
4628 * get the empty PCM device number to assign
4630 static int get_empty_pcm_device(struct hda_bus *bus, unsigned int type)
4632 /* audio device indices; not linear to keep compatibility */
4633 /* assigned to static slots up to dev#10; if more needed, assign
4634 * the later slot dynamically (when CONFIG_SND_DYNAMIC_MINORS=y)
4636 static int audio_idx[HDA_PCM_NTYPES][5] = {
4637 [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 },
4638 [HDA_PCM_TYPE_SPDIF] = { 1, -1 },
4639 [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 },
4640 [HDA_PCM_TYPE_MODEM] = { 6, -1 },
4644 if (type >= HDA_PCM_NTYPES) {
4645 snd_printk(KERN_WARNING "Invalid PCM type %d\n", type);
4649 for (i = 0; audio_idx[type][i] >= 0; i++) {
4650 #ifndef CONFIG_SND_DYNAMIC_MINORS
4651 if (audio_idx[type][i] >= 8)
4654 if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits))
4655 return audio_idx[type][i];
4658 #ifdef CONFIG_SND_DYNAMIC_MINORS
4659 /* non-fixed slots starting from 10 */
4660 for (i = 10; i < 32; i++) {
4661 if (!test_and_set_bit(i, bus->pcm_dev_bits))
4666 snd_printk(KERN_WARNING "Too many %s devices\n",
4667 snd_hda_pcm_type_name[type]);
4668 #ifndef CONFIG_SND_DYNAMIC_MINORS
4669 snd_printk(KERN_WARNING "Consider building the kernel with CONFIG_SND_DYNAMIC_MINORS=y\n");
4675 * attach a new PCM stream
4677 static int snd_hda_attach_pcm(struct hda_codec *codec, struct hda_pcm *pcm)
4679 struct hda_bus *bus = codec->bus;
4680 struct hda_pcm_stream *info;
4683 if (snd_BUG_ON(!pcm->name))
4685 for (stream = 0; stream < 2; stream++) {
4686 info = &pcm->stream[stream];
4687 if (info->substreams) {
4688 err = set_pcm_default_values(codec, info);
4693 return bus->ops.attach_pcm(bus, codec, pcm);
4696 /* assign all PCMs of the given codec */
4697 int snd_hda_codec_build_pcms(struct hda_codec *codec)
4702 if (!codec->num_pcms) {
4703 if (!codec->patch_ops.build_pcms)
4705 err = codec->patch_ops.build_pcms(codec);
4707 printk(KERN_ERR "hda_codec: cannot build PCMs"
4708 "for #%d (error %d)\n", codec->addr, err);
4709 err = snd_hda_codec_reset(codec);
4712 "hda_codec: cannot revert codec\n");
4717 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
4718 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
4721 if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
4722 continue; /* no substreams assigned */
4725 dev = get_empty_pcm_device(codec->bus, cpcm->pcm_type);
4727 continue; /* no fatal error */
4729 err = snd_hda_attach_pcm(codec, cpcm);
4731 printk(KERN_ERR "hda_codec: cannot attach "
4732 "PCM stream %d for codec #%d\n",
4734 continue; /* no fatal error */
4742 * snd_hda_build_pcms - build PCM information
4745 * Create PCM information for each codec included in the bus.
4747 * The build_pcms codec patch is requested to set up codec->num_pcms and
4748 * codec->pcm_info properly. The array is referred by the top-level driver
4749 * to create its PCM instances.
4750 * The allocated codec->pcm_info should be released in codec->patch_ops.free
4753 * At least, substreams, channels_min and channels_max must be filled for
4754 * each stream. substreams = 0 indicates that the stream doesn't exist.
4755 * When rates and/or formats are zero, the supported values are queried
4756 * from the given nid. The nid is used also by the default ops.prepare
4757 * and ops.cleanup callbacks.
4759 * The driver needs to call ops.open in its open callback. Similarly,
4760 * ops.close is supposed to be called in the close callback.
4761 * ops.prepare should be called in the prepare or hw_params callback
4762 * with the proper parameters for set up.
4763 * ops.cleanup should be called in hw_free for clean up of streams.
4765 * This function returns 0 if successful, or a negative error code.
4767 int snd_hda_build_pcms(struct hda_bus *bus)
4769 struct hda_codec *codec;
4771 list_for_each_entry(codec, &bus->codec_list, list) {
4772 int err = snd_hda_codec_build_pcms(codec);
4778 EXPORT_SYMBOL_HDA(snd_hda_build_pcms);
4781 * snd_hda_check_board_config - compare the current codec with the config table
4782 * @codec: the HDA codec
4783 * @num_configs: number of config enums
4784 * @models: array of model name strings
4785 * @tbl: configuration table, terminated by null entries
4787 * Compares the modelname or PCI subsystem id of the current codec with the
4788 * given configuration table. If a matching entry is found, returns its
4789 * config value (supposed to be 0 or positive).
4791 * If no entries are matching, the function returns a negative value.
4793 int snd_hda_check_board_config(struct hda_codec *codec,
4794 int num_configs, const char * const *models,
4795 const struct snd_pci_quirk *tbl)
4797 if (codec->modelname && models) {
4799 for (i = 0; i < num_configs; i++) {
4801 !strcmp(codec->modelname, models[i])) {
4802 snd_printd(KERN_INFO "hda_codec: model '%s' is "
4803 "selected\n", models[i]);
4809 if (!codec->bus->pci || !tbl)
4812 tbl = snd_pci_quirk_lookup(codec->bus->pci, tbl);
4815 if (tbl->value >= 0 && tbl->value < num_configs) {
4816 #ifdef CONFIG_SND_DEBUG_VERBOSE
4818 const char *model = NULL;
4820 model = models[tbl->value];
4822 sprintf(tmp, "#%d", tbl->value);
4825 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
4826 "for config %x:%x (%s)\n",
4827 model, tbl->subvendor, tbl->subdevice,
4828 (tbl->name ? tbl->name : "Unknown device"));
4834 EXPORT_SYMBOL_HDA(snd_hda_check_board_config);
4837 * snd_hda_check_board_codec_sid_config - compare the current codec
4838 subsystem ID with the
4841 This is important for Gateway notebooks with SB450 HDA Audio
4842 where the vendor ID of the PCI device is:
4843 ATI Technologies Inc SB450 HDA Audio [1002:437b]
4844 and the vendor/subvendor are found only at the codec.
4846 * @codec: the HDA codec
4847 * @num_configs: number of config enums
4848 * @models: array of model name strings
4849 * @tbl: configuration table, terminated by null entries
4851 * Compares the modelname or PCI subsystem id of the current codec with the
4852 * given configuration table. If a matching entry is found, returns its
4853 * config value (supposed to be 0 or positive).
4855 * If no entries are matching, the function returns a negative value.
4857 int snd_hda_check_board_codec_sid_config(struct hda_codec *codec,
4858 int num_configs, const char * const *models,
4859 const struct snd_pci_quirk *tbl)
4861 const struct snd_pci_quirk *q;
4863 /* Search for codec ID */
4864 for (q = tbl; q->subvendor; q++) {
4865 unsigned int mask = 0xffff0000 | q->subdevice_mask;
4866 unsigned int id = (q->subdevice | (q->subvendor << 16)) & mask;
4867 if ((codec->subsystem_id & mask) == id)
4876 if (tbl->value >= 0 && tbl->value < num_configs) {
4877 #ifdef CONFIG_SND_DEBUG_VERBOSE
4879 const char *model = NULL;
4881 model = models[tbl->value];
4883 sprintf(tmp, "#%d", tbl->value);
4886 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
4887 "for config %x:%x (%s)\n",
4888 model, tbl->subvendor, tbl->subdevice,
4889 (tbl->name ? tbl->name : "Unknown device"));
4895 EXPORT_SYMBOL_HDA(snd_hda_check_board_codec_sid_config);
4898 * snd_hda_add_new_ctls - create controls from the array
4899 * @codec: the HDA codec
4900 * @knew: the array of struct snd_kcontrol_new
4902 * This helper function creates and add new controls in the given array.
4903 * The array must be terminated with an empty entry as terminator.
4905 * Returns 0 if successful, or a negative error code.
4907 int snd_hda_add_new_ctls(struct hda_codec *codec,
4908 const struct snd_kcontrol_new *knew)
4912 for (; knew->name; knew++) {
4913 struct snd_kcontrol *kctl;
4914 int addr = 0, idx = 0;
4915 if (knew->iface == -1) /* skip this codec private value */
4918 kctl = snd_ctl_new1(knew, codec);
4922 kctl->id.device = addr;
4924 kctl->id.index = idx;
4925 err = snd_hda_ctl_add(codec, 0, kctl);
4928 /* try first with another device index corresponding to
4929 * the codec addr; if it still fails (or it's the
4930 * primary codec), then try another control index
4932 if (!addr && codec->addr)
4934 else if (!idx && !knew->index) {
4935 idx = find_empty_mixer_ctl_idx(codec,
4945 EXPORT_SYMBOL_HDA(snd_hda_add_new_ctls);
4948 static void hda_power_work(struct work_struct *work)
4950 struct hda_codec *codec =
4951 container_of(work, struct hda_codec, power_work.work);
4952 struct hda_bus *bus = codec->bus;
4955 spin_lock(&codec->power_lock);
4956 if (codec->power_transition > 0) { /* during power-up sequence? */
4957 spin_unlock(&codec->power_lock);
4960 if (!codec->power_on || codec->power_count) {
4961 codec->power_transition = 0;
4962 spin_unlock(&codec->power_lock);
4965 spin_unlock(&codec->power_lock);
4967 state = hda_call_codec_suspend(codec, true);
4968 if (!bus->power_keep_link_on && (state & AC_PWRST_CLK_STOP_OK))
4969 hda_call_pm_notify(codec, false);
4972 static void hda_keep_power_on(struct hda_codec *codec)
4974 spin_lock(&codec->power_lock);
4975 codec->power_count++;
4976 codec->power_on = 1;
4977 codec->power_jiffies = jiffies;
4978 spin_unlock(&codec->power_lock);
4979 hda_call_pm_notify(codec, true);
4982 /* update the power on/off account with the current jiffies */
4983 void snd_hda_update_power_acct(struct hda_codec *codec)
4985 unsigned long delta = jiffies - codec->power_jiffies;
4986 if (codec->power_on)
4987 codec->power_on_acct += delta;
4989 codec->power_off_acct += delta;
4990 codec->power_jiffies += delta;
4993 /* Transition to powered up, if wait_power_down then wait for a pending
4994 * transition to D3 to complete. A pending D3 transition is indicated
4995 * with power_transition == -1. */
4996 /* call this with codec->power_lock held! */
4997 static void __snd_hda_power_up(struct hda_codec *codec, bool wait_power_down)
4999 /* Return if power_on or transitioning to power_on, unless currently
5001 if ((codec->power_on || codec->power_transition > 0) &&
5002 !(wait_power_down && codec->power_transition < 0))
5004 spin_unlock(&codec->power_lock);
5006 cancel_delayed_work_sync(&codec->power_work);
5008 spin_lock(&codec->power_lock);
5009 /* If the power down delayed work was cancelled above before starting,
5010 * then there is no need to go through power up here.
5012 if (codec->power_on) {
5013 if (codec->power_transition < 0)
5014 codec->power_transition = 0;
5018 trace_hda_power_up(codec);
5019 snd_hda_update_power_acct(codec);
5020 codec->power_on = 1;
5021 codec->power_jiffies = jiffies;
5022 codec->power_transition = 1; /* avoid reentrance */
5023 spin_unlock(&codec->power_lock);
5025 hda_call_codec_resume(codec);
5027 spin_lock(&codec->power_lock);
5028 codec->power_transition = 0;
5031 #define power_save(codec) \
5032 ((codec)->bus->power_save ? *(codec)->bus->power_save : 0)
5034 /* Transition to powered down */
5035 static void __snd_hda_power_down(struct hda_codec *codec)
5037 if (!codec->power_on || codec->power_count || codec->power_transition)
5040 if (power_save(codec)) {
5041 codec->power_transition = -1; /* avoid reentrance */
5042 queue_delayed_work(codec->bus->workq, &codec->power_work,
5043 msecs_to_jiffies(power_save(codec) * 1000));
5048 * snd_hda_power_save - Power-up/down/sync the codec
5049 * @codec: HD-audio codec
5050 * @delta: the counter delta to change
5052 * Change the power-up counter via @delta, and power up or down the hardware
5053 * appropriately. For the power-down, queue to the delayed action.
5054 * Passing zero to @delta means to synchronize the power state.
5056 void snd_hda_power_save(struct hda_codec *codec, int delta, bool d3wait)
5058 spin_lock(&codec->power_lock);
5059 codec->power_count += delta;
5060 trace_hda_power_count(codec);
5062 __snd_hda_power_up(codec, d3wait);
5064 __snd_hda_power_down(codec);
5065 spin_unlock(&codec->power_lock);
5067 EXPORT_SYMBOL_HDA(snd_hda_power_save);
5070 * snd_hda_check_amp_list_power - Check the amp list and update the power
5071 * @codec: HD-audio codec
5072 * @check: the object containing an AMP list and the status
5073 * @nid: NID to check / update
5075 * Check whether the given NID is in the amp list. If it's in the list,
5076 * check the current AMP status, and update the the power-status according
5077 * to the mute status.
5079 * This function is supposed to be set or called from the check_power_status
5082 int snd_hda_check_amp_list_power(struct hda_codec *codec,
5083 struct hda_loopback_check *check,
5086 const struct hda_amp_list *p;
5089 if (!check->amplist)
5091 for (p = check->amplist; p->nid; p++) {
5096 return 0; /* nothing changed */
5098 for (p = check->amplist; p->nid; p++) {
5099 for (ch = 0; ch < 2; ch++) {
5100 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
5102 if (!(v & HDA_AMP_MUTE) && v > 0) {
5103 if (!check->power_on) {
5104 check->power_on = 1;
5105 snd_hda_power_up(codec);
5111 if (check->power_on) {
5112 check->power_on = 0;
5113 snd_hda_power_down(codec);
5117 EXPORT_SYMBOL_HDA(snd_hda_check_amp_list_power);
5119 static void hda_pm_work(struct work_struct *work)
5121 struct hda_codec *codec =
5122 container_of(work, struct hda_codec, pm_work);
5124 hda_call_codec_suspend(codec, false);
5129 * Channel mode helper
5133 * snd_hda_ch_mode_info - Info callback helper for the channel mode enum
5135 int snd_hda_ch_mode_info(struct hda_codec *codec,
5136 struct snd_ctl_elem_info *uinfo,
5137 const struct hda_channel_mode *chmode,
5140 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
5142 uinfo->value.enumerated.items = num_chmodes;
5143 if (uinfo->value.enumerated.item >= num_chmodes)
5144 uinfo->value.enumerated.item = num_chmodes - 1;
5145 sprintf(uinfo->value.enumerated.name, "%dch",
5146 chmode[uinfo->value.enumerated.item].channels);
5149 EXPORT_SYMBOL_HDA(snd_hda_ch_mode_info);
5152 * snd_hda_ch_mode_get - Get callback helper for the channel mode enum
5154 int snd_hda_ch_mode_get(struct hda_codec *codec,
5155 struct snd_ctl_elem_value *ucontrol,
5156 const struct hda_channel_mode *chmode,
5162 for (i = 0; i < num_chmodes; i++) {
5163 if (max_channels == chmode[i].channels) {
5164 ucontrol->value.enumerated.item[0] = i;
5170 EXPORT_SYMBOL_HDA(snd_hda_ch_mode_get);
5173 * snd_hda_ch_mode_put - Put callback helper for the channel mode enum
5175 int snd_hda_ch_mode_put(struct hda_codec *codec,
5176 struct snd_ctl_elem_value *ucontrol,
5177 const struct hda_channel_mode *chmode,
5183 mode = ucontrol->value.enumerated.item[0];
5184 if (mode >= num_chmodes)
5186 if (*max_channelsp == chmode[mode].channels)
5188 /* change the current channel setting */
5189 *max_channelsp = chmode[mode].channels;
5190 if (chmode[mode].sequence)
5191 snd_hda_sequence_write_cache(codec, chmode[mode].sequence);
5194 EXPORT_SYMBOL_HDA(snd_hda_ch_mode_put);
5201 * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum
5203 int snd_hda_input_mux_info(const struct hda_input_mux *imux,
5204 struct snd_ctl_elem_info *uinfo)
5208 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
5210 uinfo->value.enumerated.items = imux->num_items;
5211 if (!imux->num_items)
5213 index = uinfo->value.enumerated.item;
5214 if (index >= imux->num_items)
5215 index = imux->num_items - 1;
5216 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
5219 EXPORT_SYMBOL_HDA(snd_hda_input_mux_info);
5222 * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum
5224 int snd_hda_input_mux_put(struct hda_codec *codec,
5225 const struct hda_input_mux *imux,
5226 struct snd_ctl_elem_value *ucontrol,
5228 unsigned int *cur_val)
5232 if (!imux->num_items)
5234 idx = ucontrol->value.enumerated.item[0];
5235 if (idx >= imux->num_items)
5236 idx = imux->num_items - 1;
5237 if (*cur_val == idx)
5239 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
5240 imux->items[idx].index);
5244 EXPORT_SYMBOL_HDA(snd_hda_input_mux_put);
5248 * process kcontrol info callback of a simple string enum array
5249 * when @num_items is 0 or @texts is NULL, assume a boolean enum array
5251 int snd_hda_enum_helper_info(struct snd_kcontrol *kcontrol,
5252 struct snd_ctl_elem_info *uinfo,
5253 int num_items, const char * const *texts)
5255 static const char * const texts_default[] = {
5256 "Disabled", "Enabled"
5259 if (!texts || !num_items) {
5261 texts = texts_default;
5264 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
5266 uinfo->value.enumerated.items = num_items;
5267 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
5268 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
5269 strcpy(uinfo->value.enumerated.name,
5270 texts[uinfo->value.enumerated.item]);
5273 EXPORT_SYMBOL_HDA(snd_hda_enum_helper_info);
5276 * Multi-channel / digital-out PCM helper functions
5279 /* setup SPDIF output stream */
5280 static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
5281 unsigned int stream_tag, unsigned int format)
5283 struct hda_spdif_out *spdif;
5284 unsigned int curr_fmt;
5287 spdif = snd_hda_spdif_out_of_nid(codec, nid);
5288 curr_fmt = snd_hda_codec_read(codec, nid, 0,
5289 AC_VERB_GET_STREAM_FORMAT, 0);
5290 reset = codec->spdif_status_reset &&
5291 (spdif->ctls & AC_DIG1_ENABLE) &&
5294 /* turn off SPDIF if needed; otherwise the IEC958 bits won't be
5297 set_dig_out_convert(codec, nid,
5298 spdif->ctls & ~AC_DIG1_ENABLE & 0xff,
5300 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
5301 if (codec->slave_dig_outs) {
5303 for (d = codec->slave_dig_outs; *d; d++)
5304 snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
5307 /* turn on again (if needed) */
5309 set_dig_out_convert(codec, nid,
5310 spdif->ctls & 0xff, -1);
5313 static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
5315 snd_hda_codec_cleanup_stream(codec, nid);
5316 if (codec->slave_dig_outs) {
5318 for (d = codec->slave_dig_outs; *d; d++)
5319 snd_hda_codec_cleanup_stream(codec, *d);
5324 * snd_hda_bus_reboot_notify - call the reboot notifier of each codec
5325 * @bus: HD-audio bus
5327 void snd_hda_bus_reboot_notify(struct hda_bus *bus)
5329 struct hda_codec *codec;
5333 list_for_each_entry(codec, &bus->codec_list, list) {
5334 if (hda_codec_is_power_on(codec) &&
5335 codec->patch_ops.reboot_notify)
5336 codec->patch_ops.reboot_notify(codec);
5339 EXPORT_SYMBOL_HDA(snd_hda_bus_reboot_notify);
5342 * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode
5344 int snd_hda_multi_out_dig_open(struct hda_codec *codec,
5345 struct hda_multi_out *mout)
5347 mutex_lock(&codec->spdif_mutex);
5348 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
5349 /* already opened as analog dup; reset it once */
5350 cleanup_dig_out_stream(codec, mout->dig_out_nid);
5351 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
5352 mutex_unlock(&codec->spdif_mutex);
5355 EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_open);
5358 * snd_hda_multi_out_dig_prepare - prepare the digital out stream
5360 int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
5361 struct hda_multi_out *mout,
5362 unsigned int stream_tag,
5363 unsigned int format,
5364 struct snd_pcm_substream *substream)
5366 mutex_lock(&codec->spdif_mutex);
5367 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
5368 mutex_unlock(&codec->spdif_mutex);
5371 EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_prepare);
5374 * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream
5376 int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec,
5377 struct hda_multi_out *mout)
5379 mutex_lock(&codec->spdif_mutex);
5380 cleanup_dig_out_stream(codec, mout->dig_out_nid);
5381 mutex_unlock(&codec->spdif_mutex);
5384 EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_cleanup);
5387 * snd_hda_multi_out_dig_close - release the digital out stream
5389 int snd_hda_multi_out_dig_close(struct hda_codec *codec,
5390 struct hda_multi_out *mout)
5392 mutex_lock(&codec->spdif_mutex);
5393 mout->dig_out_used = 0;
5394 mutex_unlock(&codec->spdif_mutex);
5397 EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_close);
5400 * snd_hda_multi_out_analog_open - open analog outputs
5402 * Open analog outputs and set up the hw-constraints.
5403 * If the digital outputs can be opened as slave, open the digital
5406 int snd_hda_multi_out_analog_open(struct hda_codec *codec,
5407 struct hda_multi_out *mout,
5408 struct snd_pcm_substream *substream,
5409 struct hda_pcm_stream *hinfo)
5411 struct snd_pcm_runtime *runtime = substream->runtime;
5412 runtime->hw.channels_max = mout->max_channels;
5413 if (mout->dig_out_nid) {
5414 if (!mout->analog_rates) {
5415 mout->analog_rates = hinfo->rates;
5416 mout->analog_formats = hinfo->formats;
5417 mout->analog_maxbps = hinfo->maxbps;
5419 runtime->hw.rates = mout->analog_rates;
5420 runtime->hw.formats = mout->analog_formats;
5421 hinfo->maxbps = mout->analog_maxbps;
5423 if (!mout->spdif_rates) {
5424 snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
5426 &mout->spdif_formats,
5427 &mout->spdif_maxbps);
5429 mutex_lock(&codec->spdif_mutex);
5430 if (mout->share_spdif) {
5431 if ((runtime->hw.rates & mout->spdif_rates) &&
5432 (runtime->hw.formats & mout->spdif_formats)) {
5433 runtime->hw.rates &= mout->spdif_rates;
5434 runtime->hw.formats &= mout->spdif_formats;
5435 if (mout->spdif_maxbps < hinfo->maxbps)
5436 hinfo->maxbps = mout->spdif_maxbps;
5438 mout->share_spdif = 0;
5439 /* FIXME: need notify? */
5442 mutex_unlock(&codec->spdif_mutex);
5444 return snd_pcm_hw_constraint_step(substream->runtime, 0,
5445 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
5447 EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_open);
5450 * snd_hda_multi_out_analog_prepare - Preapre the analog outputs.
5452 * Set up the i/o for analog out.
5453 * When the digital out is available, copy the front out to digital out, too.
5455 int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
5456 struct hda_multi_out *mout,
5457 unsigned int stream_tag,
5458 unsigned int format,
5459 struct snd_pcm_substream *substream)
5461 const hda_nid_t *nids = mout->dac_nids;
5462 int chs = substream->runtime->channels;
5463 struct hda_spdif_out *spdif;
5466 mutex_lock(&codec->spdif_mutex);
5467 spdif = snd_hda_spdif_out_of_nid(codec, mout->dig_out_nid);
5468 if (mout->dig_out_nid && mout->share_spdif &&
5469 mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
5471 snd_hda_is_supported_format(codec, mout->dig_out_nid,
5473 !(spdif->status & IEC958_AES0_NONAUDIO)) {
5474 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
5475 setup_dig_out_stream(codec, mout->dig_out_nid,
5476 stream_tag, format);
5478 mout->dig_out_used = 0;
5479 cleanup_dig_out_stream(codec, mout->dig_out_nid);
5482 mutex_unlock(&codec->spdif_mutex);
5485 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
5487 if (!mout->no_share_stream &&
5488 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
5489 /* headphone out will just decode front left/right (stereo) */
5490 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
5492 /* extra outputs copied from front */
5493 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
5494 if (!mout->no_share_stream && mout->hp_out_nid[i])
5495 snd_hda_codec_setup_stream(codec,
5496 mout->hp_out_nid[i],
5497 stream_tag, 0, format);
5500 for (i = 1; i < mout->num_dacs; i++) {
5501 if (chs >= (i + 1) * 2) /* independent out */
5502 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
5504 else if (!mout->no_share_stream) /* copy front */
5505 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
5509 /* extra surrounds */
5510 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++) {
5512 if (!mout->extra_out_nid[i])
5514 if (chs >= (i + 1) * 2)
5516 else if (!mout->no_share_stream)
5518 snd_hda_codec_setup_stream(codec, mout->extra_out_nid[i],
5519 stream_tag, ch, format);
5524 EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_prepare);
5527 * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out
5529 int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
5530 struct hda_multi_out *mout)
5532 const hda_nid_t *nids = mout->dac_nids;
5535 for (i = 0; i < mout->num_dacs; i++)
5536 snd_hda_codec_cleanup_stream(codec, nids[i]);
5538 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
5539 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
5540 if (mout->hp_out_nid[i])
5541 snd_hda_codec_cleanup_stream(codec,
5542 mout->hp_out_nid[i]);
5543 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
5544 if (mout->extra_out_nid[i])
5545 snd_hda_codec_cleanup_stream(codec,
5546 mout->extra_out_nid[i]);
5547 mutex_lock(&codec->spdif_mutex);
5548 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
5549 cleanup_dig_out_stream(codec, mout->dig_out_nid);
5550 mout->dig_out_used = 0;
5552 mutex_unlock(&codec->spdif_mutex);
5555 EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_cleanup);
5558 * snd_hda_get_default_vref - Get the default (mic) VREF pin bits
5560 * Guess the suitable VREF pin bits to be set as the pin-control value.
5561 * Note: the function doesn't set the AC_PINCTL_IN_EN bit.
5563 unsigned int snd_hda_get_default_vref(struct hda_codec *codec, hda_nid_t pin)
5565 unsigned int pincap;
5566 unsigned int oldval;
5567 oldval = snd_hda_codec_read(codec, pin, 0,
5568 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
5569 pincap = snd_hda_query_pin_caps(codec, pin);
5570 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
5571 /* Exception: if the default pin setup is vref50, we give it priority */
5572 if ((pincap & AC_PINCAP_VREF_80) && oldval != PIN_VREF50)
5573 return AC_PINCTL_VREF_80;
5574 else if (pincap & AC_PINCAP_VREF_50)
5575 return AC_PINCTL_VREF_50;
5576 else if (pincap & AC_PINCAP_VREF_100)
5577 return AC_PINCTL_VREF_100;
5578 else if (pincap & AC_PINCAP_VREF_GRD)
5579 return AC_PINCTL_VREF_GRD;
5580 return AC_PINCTL_VREF_HIZ;
5582 EXPORT_SYMBOL_HDA(snd_hda_get_default_vref);
5584 /* correct the pin ctl value for matching with the pin cap */
5585 unsigned int snd_hda_correct_pin_ctl(struct hda_codec *codec,
5586 hda_nid_t pin, unsigned int val)
5588 static unsigned int cap_lists[][2] = {
5589 { AC_PINCTL_VREF_100, AC_PINCAP_VREF_100 },
5590 { AC_PINCTL_VREF_80, AC_PINCAP_VREF_80 },
5591 { AC_PINCTL_VREF_50, AC_PINCAP_VREF_50 },
5592 { AC_PINCTL_VREF_GRD, AC_PINCAP_VREF_GRD },
5598 cap = snd_hda_query_pin_caps(codec, pin);
5600 return val; /* don't know what to do... */
5602 if (val & AC_PINCTL_OUT_EN) {
5603 if (!(cap & AC_PINCAP_OUT))
5604 val &= ~(AC_PINCTL_OUT_EN | AC_PINCTL_HP_EN);
5605 else if ((val & AC_PINCTL_HP_EN) && !(cap & AC_PINCAP_HP_DRV))
5606 val &= ~AC_PINCTL_HP_EN;
5609 if (val & AC_PINCTL_IN_EN) {
5610 if (!(cap & AC_PINCAP_IN))
5611 val &= ~(AC_PINCTL_IN_EN | AC_PINCTL_VREFEN);
5613 unsigned int vcap, vref;
5615 vcap = (cap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
5616 vref = val & AC_PINCTL_VREFEN;
5617 for (i = 0; i < ARRAY_SIZE(cap_lists); i++) {
5618 if (vref == cap_lists[i][0] &&
5619 !(vcap & cap_lists[i][1])) {
5620 if (i == ARRAY_SIZE(cap_lists) - 1)
5621 vref = AC_PINCTL_VREF_HIZ;
5623 vref = cap_lists[i + 1][0];
5626 val &= ~AC_PINCTL_VREFEN;
5633 EXPORT_SYMBOL_HDA(snd_hda_correct_pin_ctl);
5635 int _snd_hda_set_pin_ctl(struct hda_codec *codec, hda_nid_t pin,
5636 unsigned int val, bool cached)
5638 val = snd_hda_correct_pin_ctl(codec, pin, val);
5639 snd_hda_codec_set_pin_target(codec, pin, val);
5641 return snd_hda_codec_update_cache(codec, pin, 0,
5642 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
5644 return snd_hda_codec_write(codec, pin, 0,
5645 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
5647 EXPORT_SYMBOL_HDA(_snd_hda_set_pin_ctl);
5650 * snd_hda_add_imux_item - Add an item to input_mux
5652 * When the same label is used already in the existing items, the number
5653 * suffix is appended to the label. This label index number is stored
5654 * to type_idx when non-NULL pointer is given.
5656 int snd_hda_add_imux_item(struct hda_input_mux *imux, const char *label,
5657 int index, int *type_idx)
5659 int i, label_idx = 0;
5660 if (imux->num_items >= HDA_MAX_NUM_INPUTS) {
5661 snd_printd(KERN_ERR "hda_codec: Too many imux items!\n");
5664 for (i = 0; i < imux->num_items; i++) {
5665 if (!strncmp(label, imux->items[i].label, strlen(label)))
5669 *type_idx = label_idx;
5671 snprintf(imux->items[imux->num_items].label,
5672 sizeof(imux->items[imux->num_items].label),
5673 "%s %d", label, label_idx);
5675 strlcpy(imux->items[imux->num_items].label, label,
5676 sizeof(imux->items[imux->num_items].label));
5677 imux->items[imux->num_items].index = index;
5681 EXPORT_SYMBOL_HDA(snd_hda_add_imux_item);
5690 * snd_hda_suspend - suspend the codecs
5693 * Returns 0 if successful.
5695 int snd_hda_suspend(struct hda_bus *bus)
5697 struct hda_codec *codec;
5699 list_for_each_entry(codec, &bus->codec_list, list) {
5700 cancel_delayed_work_sync(&codec->jackpoll_work);
5701 if (hda_codec_is_power_on(codec)) {
5702 if (bus->num_codecs > 1)
5703 queue_work(bus->pm_wq, &codec->pm_work);
5705 hda_call_codec_suspend(codec, false);
5709 if (bus->num_codecs > 1)
5710 flush_workqueue(bus->pm_wq);
5714 EXPORT_SYMBOL_HDA(snd_hda_suspend);
5717 * snd_hda_resume - resume the codecs
5720 * Returns 0 if successful.
5722 int snd_hda_resume(struct hda_bus *bus)
5724 struct hda_codec *codec;
5726 list_for_each_entry(codec, &bus->codec_list, list) {
5727 hda_call_codec_resume(codec);
5731 EXPORT_SYMBOL_HDA(snd_hda_resume);
5732 #endif /* CONFIG_PM */
5739 * snd_array_new - get a new element from the given array
5740 * @array: the array object
5742 * Get a new element from the given array. If it exceeds the
5743 * pre-allocated array size, re-allocate the array.
5745 * Returns NULL if allocation failed.
5747 void *snd_array_new(struct snd_array *array)
5749 if (snd_BUG_ON(!array->elem_size))
5751 if (array->used >= array->alloced) {
5752 int num = array->alloced + array->alloc_align;
5753 int size = (num + 1) * array->elem_size;
5755 if (snd_BUG_ON(num >= 4096))
5757 nlist = krealloc(array->list, size, GFP_KERNEL | __GFP_ZERO);
5760 array->list = nlist;
5761 array->alloced = num;
5763 return snd_array_elem(array, array->used++);
5765 EXPORT_SYMBOL_HDA(snd_array_new);
5768 * snd_array_free - free the given array elements
5769 * @array: the array object
5771 void snd_array_free(struct snd_array *array)
5778 EXPORT_SYMBOL_HDA(snd_array_free);
5781 * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer
5782 * @pcm: PCM caps bits
5783 * @buf: the string buffer to write
5784 * @buflen: the max buffer length
5786 * used by hda_proc.c and hda_eld.c
5788 void snd_print_pcm_bits(int pcm, char *buf, int buflen)
5790 static unsigned int bits[] = { 8, 16, 20, 24, 32 };
5793 for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
5794 if (pcm & (AC_SUPPCM_BITS_8 << i))
5795 j += snprintf(buf + j, buflen - j, " %d", bits[i]);
5797 buf[j] = '\0'; /* necessary when j == 0 */
5799 EXPORT_SYMBOL_HDA(snd_print_pcm_bits);
5801 MODULE_DESCRIPTION("HDA codec core");
5802 MODULE_LICENSE("GPL");