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
22 #include <linux/init.h>
23 #include <linux/delay.h>
24 #include <linux/slab.h>
25 #include <linux/pci.h>
26 #include <linux/mutex.h>
27 #include <sound/core.h>
28 #include "hda_codec.h"
29 #include <sound/asoundef.h>
30 #include <sound/tlv.h>
31 #include <sound/initval.h>
32 #include "hda_local.h"
34 #include <sound/hda_hwdep.h>
37 * vendor / preset table
40 struct hda_vendor_id {
45 /* codec vendor labels */
46 static struct hda_vendor_id hda_vendor_ids[] = {
48 { 0x1013, "Cirrus Logic" },
49 { 0x1057, "Motorola" },
50 { 0x1095, "Silicon Image" },
52 { 0x10ec, "Realtek" },
53 { 0x1102, "Creative" },
57 { 0x11d4, "Analog Devices" },
58 { 0x13f6, "C-Media" },
59 { 0x14f1, "Conexant" },
60 { 0x17e8, "Chrontel" },
62 { 0x1aec, "Wolfson Microelectronics" },
63 { 0x434d, "C-Media" },
65 { 0x8384, "SigmaTel" },
69 static DEFINE_MUTEX(preset_mutex);
70 static LIST_HEAD(hda_preset_tables);
72 int snd_hda_add_codec_preset(struct hda_codec_preset_list *preset)
74 mutex_lock(&preset_mutex);
75 list_add_tail(&preset->list, &hda_preset_tables);
76 mutex_unlock(&preset_mutex);
79 EXPORT_SYMBOL_HDA(snd_hda_add_codec_preset);
81 int snd_hda_delete_codec_preset(struct hda_codec_preset_list *preset)
83 mutex_lock(&preset_mutex);
84 list_del(&preset->list);
85 mutex_unlock(&preset_mutex);
88 EXPORT_SYMBOL_HDA(snd_hda_delete_codec_preset);
90 #ifdef CONFIG_SND_HDA_POWER_SAVE
91 static void hda_power_work(struct work_struct *work);
92 static void hda_keep_power_on(struct hda_codec *codec);
94 static inline void hda_keep_power_on(struct hda_codec *codec) {}
98 * snd_hda_get_jack_location - Give a location string of the jack
99 * @cfg: pin default config value
101 * Parse the pin default config value and returns the string of the
102 * jack location, e.g. "Rear", "Front", etc.
104 const char *snd_hda_get_jack_location(u32 cfg)
106 static char *bases[7] = {
107 "N/A", "Rear", "Front", "Left", "Right", "Top", "Bottom",
109 static unsigned char specials_idx[] = {
114 static char *specials[] = {
115 "Rear Panel", "Drive Bar",
116 "Riser", "HDMI", "ATAPI",
117 "Mobile-In", "Mobile-Out"
120 cfg = (cfg & AC_DEFCFG_LOCATION) >> AC_DEFCFG_LOCATION_SHIFT;
121 if ((cfg & 0x0f) < 7)
122 return bases[cfg & 0x0f];
123 for (i = 0; i < ARRAY_SIZE(specials_idx); i++) {
124 if (cfg == specials_idx[i])
129 EXPORT_SYMBOL_HDA(snd_hda_get_jack_location);
132 * snd_hda_get_jack_connectivity - Give a connectivity string of the jack
133 * @cfg: pin default config value
135 * Parse the pin default config value and returns the string of the
136 * jack connectivity, i.e. external or internal connection.
138 const char *snd_hda_get_jack_connectivity(u32 cfg)
140 static char *jack_locations[4] = { "Ext", "Int", "Sep", "Oth" };
142 return jack_locations[(cfg >> (AC_DEFCFG_LOCATION_SHIFT + 4)) & 3];
144 EXPORT_SYMBOL_HDA(snd_hda_get_jack_connectivity);
147 * snd_hda_get_jack_type - Give a type string of the jack
148 * @cfg: pin default config value
150 * Parse the pin default config value and returns the string of the
151 * jack type, i.e. the purpose of the jack, such as Line-Out or CD.
153 const char *snd_hda_get_jack_type(u32 cfg)
155 static char *jack_types[16] = {
156 "Line Out", "Speaker", "HP Out", "CD",
157 "SPDIF Out", "Digital Out", "Modem Line", "Modem Hand",
158 "Line In", "Aux", "Mic", "Telephony",
159 "SPDIF In", "Digitial In", "Reserved", "Other"
162 return jack_types[(cfg & AC_DEFCFG_DEVICE)
163 >> AC_DEFCFG_DEVICE_SHIFT];
165 EXPORT_SYMBOL_HDA(snd_hda_get_jack_type);
168 * Compose a 32bit command word to be sent to the HD-audio controller
170 static inline unsigned int
171 make_codec_cmd(struct hda_codec *codec, hda_nid_t nid, int direct,
172 unsigned int verb, unsigned int parm)
176 if ((codec->addr & ~0xf) || (direct & ~1) || (nid & ~0x7f) ||
177 (verb & ~0xfff) || (parm & ~0xffff)) {
178 printk(KERN_ERR "hda-codec: out of range cmd %x:%x:%x:%x:%x\n",
179 codec->addr, direct, nid, verb, parm);
183 val = (u32)codec->addr << 28;
184 val |= (u32)direct << 27;
185 val |= (u32)nid << 20;
192 * Send and receive a verb
194 static int codec_exec_verb(struct hda_codec *codec, unsigned int cmd,
197 struct hda_bus *bus = codec->bus;
206 snd_hda_power_up(codec);
207 mutex_lock(&bus->cmd_mutex);
208 err = bus->ops.command(bus, cmd);
210 *res = bus->ops.get_response(bus, codec->addr);
211 mutex_unlock(&bus->cmd_mutex);
212 snd_hda_power_down(codec);
213 if (res && *res == -1 && bus->rirb_error) {
214 if (bus->response_reset) {
215 snd_printd("hda_codec: resetting BUS due to "
216 "fatal communication error\n");
217 bus->ops.bus_reset(bus);
221 /* clear reset-flag when the communication gets recovered */
223 bus->response_reset = 0;
228 * snd_hda_codec_read - send a command and get the response
229 * @codec: the HDA codec
230 * @nid: NID to send the command
231 * @direct: direct flag
232 * @verb: the verb to send
233 * @parm: the parameter for the verb
235 * Send a single command and read the corresponding response.
237 * Returns the obtained response value, or -1 for an error.
239 unsigned int snd_hda_codec_read(struct hda_codec *codec, hda_nid_t nid,
241 unsigned int verb, unsigned int parm)
243 unsigned cmd = make_codec_cmd(codec, nid, direct, verb, parm);
245 codec_exec_verb(codec, cmd, &res);
248 EXPORT_SYMBOL_HDA(snd_hda_codec_read);
251 * snd_hda_codec_write - send a single command without waiting for response
252 * @codec: the HDA codec
253 * @nid: NID to send the command
254 * @direct: direct flag
255 * @verb: the verb to send
256 * @parm: the parameter for the verb
258 * Send a single command without waiting for response.
260 * Returns 0 if successful, or a negative error code.
262 int snd_hda_codec_write(struct hda_codec *codec, hda_nid_t nid, int direct,
263 unsigned int verb, unsigned int parm)
265 unsigned int cmd = make_codec_cmd(codec, nid, direct, verb, parm);
267 return codec_exec_verb(codec, cmd,
268 codec->bus->sync_write ? &res : NULL);
270 EXPORT_SYMBOL_HDA(snd_hda_codec_write);
273 * snd_hda_sequence_write - sequence writes
274 * @codec: the HDA codec
275 * @seq: VERB array to send
277 * Send the commands sequentially from the given array.
278 * The array must be terminated with NID=0.
280 void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq)
282 for (; seq->nid; seq++)
283 snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param);
285 EXPORT_SYMBOL_HDA(snd_hda_sequence_write);
288 * snd_hda_get_sub_nodes - get the range of sub nodes
289 * @codec: the HDA codec
291 * @start_id: the pointer to store the start NID
293 * Parse the NID and store the start NID of its sub-nodes.
294 * Returns the number of sub-nodes.
296 int snd_hda_get_sub_nodes(struct hda_codec *codec, hda_nid_t nid,
301 parm = snd_hda_param_read(codec, nid, AC_PAR_NODE_COUNT);
304 *start_id = (parm >> 16) & 0x7fff;
305 return (int)(parm & 0x7fff);
307 EXPORT_SYMBOL_HDA(snd_hda_get_sub_nodes);
310 * snd_hda_get_connections - get connection list
311 * @codec: the HDA codec
313 * @conn_list: connection list array
314 * @max_conns: max. number of connections to store
316 * Parses the connection list of the given widget and stores the list
319 * Returns the number of connections, or a negative error code.
321 int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
322 hda_nid_t *conn_list, int max_conns)
325 int i, conn_len, conns;
326 unsigned int shift, num_elems, mask;
330 if (snd_BUG_ON(!conn_list || max_conns <= 0))
333 wcaps = get_wcaps(codec, nid);
334 if (!(wcaps & AC_WCAP_CONN_LIST) &&
335 get_wcaps_type(wcaps) != AC_WID_VOL_KNB) {
336 snd_printk(KERN_WARNING "hda_codec: "
337 "connection list not available for 0x%x\n", nid);
341 parm = snd_hda_param_read(codec, nid, AC_PAR_CONNLIST_LEN);
342 if (parm & AC_CLIST_LONG) {
351 conn_len = parm & AC_CLIST_LENGTH;
352 mask = (1 << (shift-1)) - 1;
355 return 0; /* no connection */
358 /* single connection */
359 parm = snd_hda_codec_read(codec, nid, 0,
360 AC_VERB_GET_CONNECT_LIST, 0);
361 if (parm == -1 && codec->bus->rirb_error)
363 conn_list[0] = parm & mask;
367 /* multi connection */
370 for (i = 0; i < conn_len; i++) {
374 if (i % num_elems == 0) {
375 parm = snd_hda_codec_read(codec, nid, 0,
376 AC_VERB_GET_CONNECT_LIST, i);
377 if (parm == -1 && codec->bus->rirb_error)
380 range_val = !!(parm & (1 << (shift-1))); /* ranges */
383 snd_printk(KERN_WARNING "hda_codec: "
384 "invalid CONNECT_LIST verb %x[%i]:%x\n",
390 /* ranges between the previous and this one */
391 if (!prev_nid || prev_nid >= val) {
392 snd_printk(KERN_WARNING "hda_codec: "
393 "invalid dep_range_val %x:%x\n",
397 for (n = prev_nid + 1; n <= val; n++) {
398 if (conns >= max_conns) {
399 snd_printk(KERN_ERR "hda_codec: "
400 "Too many connections %d for NID 0x%x\n",
404 conn_list[conns++] = n;
407 if (conns >= max_conns) {
408 snd_printk(KERN_ERR "hda_codec: "
409 "Too many connections %d for NID 0x%x\n",
413 conn_list[conns++] = val;
419 EXPORT_SYMBOL_HDA(snd_hda_get_connections);
423 * snd_hda_queue_unsol_event - add an unsolicited event to queue
425 * @res: unsolicited event (lower 32bit of RIRB entry)
426 * @res_ex: codec addr and flags (upper 32bit or RIRB entry)
428 * Adds the given event to the queue. The events are processed in
429 * the workqueue asynchronously. Call this function in the interrupt
430 * hanlder when RIRB receives an unsolicited event.
432 * Returns 0 if successful, or a negative error code.
434 int snd_hda_queue_unsol_event(struct hda_bus *bus, u32 res, u32 res_ex)
436 struct hda_bus_unsolicited *unsol;
443 wp = (unsol->wp + 1) % HDA_UNSOL_QUEUE_SIZE;
447 unsol->queue[wp] = res;
448 unsol->queue[wp + 1] = res_ex;
450 queue_work(bus->workq, &unsol->work);
454 EXPORT_SYMBOL_HDA(snd_hda_queue_unsol_event);
457 * process queued unsolicited events
459 static void process_unsol_events(struct work_struct *work)
461 struct hda_bus_unsolicited *unsol =
462 container_of(work, struct hda_bus_unsolicited, work);
463 struct hda_bus *bus = unsol->bus;
464 struct hda_codec *codec;
465 unsigned int rp, caddr, res;
467 while (unsol->rp != unsol->wp) {
468 rp = (unsol->rp + 1) % HDA_UNSOL_QUEUE_SIZE;
471 res = unsol->queue[rp];
472 caddr = unsol->queue[rp + 1];
473 if (!(caddr & (1 << 4))) /* no unsolicited event? */
475 codec = bus->caddr_tbl[caddr & 0x0f];
476 if (codec && codec->patch_ops.unsol_event)
477 codec->patch_ops.unsol_event(codec, res);
482 * initialize unsolicited queue
484 static int init_unsol_queue(struct hda_bus *bus)
486 struct hda_bus_unsolicited *unsol;
488 if (bus->unsol) /* already initialized */
491 unsol = kzalloc(sizeof(*unsol), GFP_KERNEL);
493 snd_printk(KERN_ERR "hda_codec: "
494 "can't allocate unsolicited queue\n");
497 INIT_WORK(&unsol->work, process_unsol_events);
506 static void snd_hda_codec_free(struct hda_codec *codec);
508 static int snd_hda_bus_free(struct hda_bus *bus)
510 struct hda_codec *codec, *n;
515 flush_workqueue(bus->workq);
518 list_for_each_entry_safe(codec, n, &bus->codec_list, list) {
519 snd_hda_codec_free(codec);
521 if (bus->ops.private_free)
522 bus->ops.private_free(bus);
524 destroy_workqueue(bus->workq);
529 static int snd_hda_bus_dev_free(struct snd_device *device)
531 struct hda_bus *bus = device->device_data;
533 return snd_hda_bus_free(bus);
536 #ifdef CONFIG_SND_HDA_HWDEP
537 static int snd_hda_bus_dev_register(struct snd_device *device)
539 struct hda_bus *bus = device->device_data;
540 struct hda_codec *codec;
541 list_for_each_entry(codec, &bus->codec_list, list) {
542 snd_hda_hwdep_add_sysfs(codec);
543 snd_hda_hwdep_add_power_sysfs(codec);
548 #define snd_hda_bus_dev_register NULL
552 * snd_hda_bus_new - create a HDA bus
553 * @card: the card entry
554 * @temp: the template for hda_bus information
555 * @busp: the pointer to store the created bus instance
557 * Returns 0 if successful, or a negative error code.
559 int /*__devinit*/ snd_hda_bus_new(struct snd_card *card,
560 const struct hda_bus_template *temp,
561 struct hda_bus **busp)
565 static struct snd_device_ops dev_ops = {
566 .dev_register = snd_hda_bus_dev_register,
567 .dev_free = snd_hda_bus_dev_free,
570 if (snd_BUG_ON(!temp))
572 if (snd_BUG_ON(!temp->ops.command || !temp->ops.get_response))
578 bus = kzalloc(sizeof(*bus), GFP_KERNEL);
580 snd_printk(KERN_ERR "can't allocate struct hda_bus\n");
585 bus->private_data = temp->private_data;
586 bus->pci = temp->pci;
587 bus->modelname = temp->modelname;
588 bus->power_save = temp->power_save;
589 bus->ops = temp->ops;
591 mutex_init(&bus->cmd_mutex);
592 mutex_init(&bus->prepare_mutex);
593 INIT_LIST_HEAD(&bus->codec_list);
595 snprintf(bus->workq_name, sizeof(bus->workq_name),
596 "hd-audio%d", card->number);
597 bus->workq = create_singlethread_workqueue(bus->workq_name);
599 snd_printk(KERN_ERR "cannot create workqueue %s\n",
605 err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops);
607 snd_hda_bus_free(bus);
614 EXPORT_SYMBOL_HDA(snd_hda_bus_new);
616 #ifdef CONFIG_SND_HDA_GENERIC
617 #define is_generic_config(codec) \
618 (codec->modelname && !strcmp(codec->modelname, "generic"))
620 #define is_generic_config(codec) 0
624 #define HDA_MODREQ_MAX_COUNT 2 /* two request_modules()'s */
626 #define HDA_MODREQ_MAX_COUNT 0 /* all presets are statically linked */
630 * find a matching codec preset
632 static const struct hda_codec_preset *
633 find_codec_preset(struct hda_codec *codec)
635 struct hda_codec_preset_list *tbl;
636 const struct hda_codec_preset *preset;
637 int mod_requested = 0;
639 if (is_generic_config(codec))
640 return NULL; /* use the generic parser */
643 mutex_lock(&preset_mutex);
644 list_for_each_entry(tbl, &hda_preset_tables, list) {
645 if (!try_module_get(tbl->owner)) {
646 snd_printk(KERN_ERR "hda_codec: cannot module_get\n");
649 for (preset = tbl->preset; preset->id; preset++) {
650 u32 mask = preset->mask;
651 if (preset->afg && preset->afg != codec->afg)
653 if (preset->mfg && preset->mfg != codec->mfg)
657 if (preset->id == (codec->vendor_id & mask) &&
659 preset->rev == codec->revision_id)) {
660 mutex_unlock(&preset_mutex);
661 codec->owner = tbl->owner;
665 module_put(tbl->owner);
667 mutex_unlock(&preset_mutex);
669 if (mod_requested < HDA_MODREQ_MAX_COUNT) {
672 snprintf(name, sizeof(name), "snd-hda-codec-id:%08x",
675 snprintf(name, sizeof(name), "snd-hda-codec-id:%04x*",
676 (codec->vendor_id >> 16) & 0xffff);
677 request_module(name);
685 * get_codec_name - store the codec name
687 static int get_codec_name(struct hda_codec *codec)
689 const struct hda_vendor_id *c;
690 const char *vendor = NULL;
691 u16 vendor_id = codec->vendor_id >> 16;
694 if (codec->vendor_name)
697 for (c = hda_vendor_ids; c->id; c++) {
698 if (c->id == vendor_id) {
704 sprintf(tmp, "Generic %04x", vendor_id);
707 codec->vendor_name = kstrdup(vendor, GFP_KERNEL);
708 if (!codec->vendor_name)
712 if (codec->chip_name)
715 if (codec->preset && codec->preset->name)
716 codec->chip_name = kstrdup(codec->preset->name, GFP_KERNEL);
718 sprintf(tmp, "ID %x", codec->vendor_id & 0xffff);
719 codec->chip_name = kstrdup(tmp, GFP_KERNEL);
721 if (!codec->chip_name)
727 * look for an AFG and MFG nodes
729 static void /*__devinit*/ setup_fg_nodes(struct hda_codec *codec)
731 int i, total_nodes, function_id;
734 total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid);
735 for (i = 0; i < total_nodes; i++, nid++) {
736 function_id = snd_hda_param_read(codec, nid,
737 AC_PAR_FUNCTION_TYPE);
738 switch (function_id & 0xff) {
739 case AC_GRP_AUDIO_FUNCTION:
741 codec->afg_function_id = function_id & 0xff;
742 codec->afg_unsol = (function_id >> 8) & 1;
744 case AC_GRP_MODEM_FUNCTION:
746 codec->mfg_function_id = function_id & 0xff;
747 codec->mfg_unsol = (function_id >> 8) & 1;
756 * read widget caps for each widget and store in cache
758 static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
763 codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node,
765 codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL);
768 nid = codec->start_nid;
769 for (i = 0; i < codec->num_nodes; i++, nid++)
770 codec->wcaps[i] = snd_hda_param_read(codec, nid,
771 AC_PAR_AUDIO_WIDGET_CAP);
775 /* read all pin default configurations and save codec->init_pins */
776 static int read_pin_defaults(struct hda_codec *codec)
779 hda_nid_t nid = codec->start_nid;
781 for (i = 0; i < codec->num_nodes; i++, nid++) {
782 struct hda_pincfg *pin;
783 unsigned int wcaps = get_wcaps(codec, nid);
784 unsigned int wid_type = get_wcaps_type(wcaps);
785 if (wid_type != AC_WID_PIN)
787 pin = snd_array_new(&codec->init_pins);
791 pin->cfg = snd_hda_codec_read(codec, nid, 0,
792 AC_VERB_GET_CONFIG_DEFAULT, 0);
793 pin->ctrl = snd_hda_codec_read(codec, nid, 0,
794 AC_VERB_GET_PIN_WIDGET_CONTROL,
800 /* look up the given pin config list and return the item matching with NID */
801 static struct hda_pincfg *look_up_pincfg(struct hda_codec *codec,
802 struct snd_array *array,
806 for (i = 0; i < array->used; i++) {
807 struct hda_pincfg *pin = snd_array_elem(array, i);
814 /* write a config value for the given NID */
815 static void set_pincfg(struct hda_codec *codec, hda_nid_t nid,
819 for (i = 0; i < 4; i++) {
820 snd_hda_codec_write(codec, nid, 0,
821 AC_VERB_SET_CONFIG_DEFAULT_BYTES_0 + i,
827 /* set the current pin config value for the given NID.
828 * the value is cached, and read via snd_hda_codec_get_pincfg()
830 int snd_hda_add_pincfg(struct hda_codec *codec, struct snd_array *list,
831 hda_nid_t nid, unsigned int cfg)
833 struct hda_pincfg *pin;
836 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN)
839 oldcfg = snd_hda_codec_get_pincfg(codec, nid);
840 pin = look_up_pincfg(codec, list, nid);
842 pin = snd_array_new(list);
849 /* change only when needed; e.g. if the pincfg is already present
850 * in user_pins[], don't write it
852 cfg = snd_hda_codec_get_pincfg(codec, nid);
854 set_pincfg(codec, nid, cfg);
859 * snd_hda_codec_set_pincfg - Override a pin default configuration
860 * @codec: the HDA codec
861 * @nid: NID to set the pin config
862 * @cfg: the pin default config value
864 * Override a pin default configuration value in the cache.
865 * This value can be read by snd_hda_codec_get_pincfg() in a higher
866 * priority than the real hardware value.
868 int snd_hda_codec_set_pincfg(struct hda_codec *codec,
869 hda_nid_t nid, unsigned int cfg)
871 return snd_hda_add_pincfg(codec, &codec->driver_pins, nid, cfg);
873 EXPORT_SYMBOL_HDA(snd_hda_codec_set_pincfg);
876 * snd_hda_codec_get_pincfg - Obtain a pin-default configuration
877 * @codec: the HDA codec
878 * @nid: NID to get the pin config
880 * Get the current pin config value of the given pin NID.
881 * If the pincfg value is cached or overridden via sysfs or driver,
882 * returns the cached value.
884 unsigned int snd_hda_codec_get_pincfg(struct hda_codec *codec, hda_nid_t nid)
886 struct hda_pincfg *pin;
888 #ifdef CONFIG_SND_HDA_HWDEP
889 pin = look_up_pincfg(codec, &codec->user_pins, nid);
893 pin = look_up_pincfg(codec, &codec->driver_pins, nid);
896 pin = look_up_pincfg(codec, &codec->init_pins, nid);
901 EXPORT_SYMBOL_HDA(snd_hda_codec_get_pincfg);
903 /* restore all current pin configs */
904 static void restore_pincfgs(struct hda_codec *codec)
907 for (i = 0; i < codec->init_pins.used; i++) {
908 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
909 set_pincfg(codec, pin->nid,
910 snd_hda_codec_get_pincfg(codec, pin->nid));
915 * snd_hda_shutup_pins - Shut up all pins
916 * @codec: the HDA codec
918 * Clear all pin controls to shup up before suspend for avoiding click noise.
919 * The controls aren't cached so that they can be resumed properly.
921 void snd_hda_shutup_pins(struct hda_codec *codec)
924 /* don't shut up pins when unloading the driver; otherwise it breaks
925 * the default pin setup at the next load of the driver
927 if (codec->bus->shutdown)
929 for (i = 0; i < codec->init_pins.used; i++) {
930 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
931 /* use read here for syncing after issuing each verb */
932 snd_hda_codec_read(codec, pin->nid, 0,
933 AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
935 codec->pins_shutup = 1;
937 EXPORT_SYMBOL_HDA(snd_hda_shutup_pins);
939 /* Restore the pin controls cleared previously via snd_hda_shutup_pins() */
940 static void restore_shutup_pins(struct hda_codec *codec)
943 if (!codec->pins_shutup)
945 if (codec->bus->shutdown)
947 for (i = 0; i < codec->init_pins.used; i++) {
948 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
949 snd_hda_codec_write(codec, pin->nid, 0,
950 AC_VERB_SET_PIN_WIDGET_CONTROL,
953 codec->pins_shutup = 0;
956 static void init_hda_cache(struct hda_cache_rec *cache,
957 unsigned int record_size);
958 static void free_hda_cache(struct hda_cache_rec *cache);
960 /* restore the initial pin cfgs and release all pincfg lists */
961 static void restore_init_pincfgs(struct hda_codec *codec)
963 /* first free driver_pins and user_pins, then call restore_pincfg
964 * so that only the values in init_pins are restored
966 snd_array_free(&codec->driver_pins);
967 #ifdef CONFIG_SND_HDA_HWDEP
968 snd_array_free(&codec->user_pins);
970 restore_pincfgs(codec);
971 snd_array_free(&codec->init_pins);
975 * audio-converter setup caches
977 struct hda_cvt_setup {
982 unsigned char active; /* cvt is currently used */
983 unsigned char dirty; /* setups should be cleared */
986 /* get or create a cache entry for the given audio converter NID */
987 static struct hda_cvt_setup *
988 get_hda_cvt_setup(struct hda_codec *codec, hda_nid_t nid)
990 struct hda_cvt_setup *p;
993 for (i = 0; i < codec->cvt_setups.used; i++) {
994 p = snd_array_elem(&codec->cvt_setups, i);
998 p = snd_array_new(&codec->cvt_setups);
1007 static void snd_hda_codec_free(struct hda_codec *codec)
1011 restore_init_pincfgs(codec);
1012 #ifdef CONFIG_SND_HDA_POWER_SAVE
1013 cancel_delayed_work(&codec->power_work);
1014 flush_workqueue(codec->bus->workq);
1016 list_del(&codec->list);
1017 snd_array_free(&codec->mixers);
1018 snd_array_free(&codec->nids);
1019 codec->bus->caddr_tbl[codec->addr] = NULL;
1020 if (codec->patch_ops.free)
1021 codec->patch_ops.free(codec);
1022 module_put(codec->owner);
1023 free_hda_cache(&codec->amp_cache);
1024 free_hda_cache(&codec->cmd_cache);
1025 kfree(codec->vendor_name);
1026 kfree(codec->chip_name);
1027 kfree(codec->modelname);
1028 kfree(codec->wcaps);
1032 static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
1033 unsigned int power_state);
1036 * snd_hda_codec_new - create a HDA codec
1037 * @bus: the bus to assign
1038 * @codec_addr: the codec address
1039 * @codecp: the pointer to store the generated codec
1041 * Returns 0 if successful, or a negative error code.
1043 int /*__devinit*/ snd_hda_codec_new(struct hda_bus *bus,
1044 unsigned int codec_addr,
1045 struct hda_codec **codecp)
1047 struct hda_codec *codec;
1051 if (snd_BUG_ON(!bus))
1053 if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS))
1056 if (bus->caddr_tbl[codec_addr]) {
1057 snd_printk(KERN_ERR "hda_codec: "
1058 "address 0x%x is already occupied\n", codec_addr);
1062 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
1063 if (codec == NULL) {
1064 snd_printk(KERN_ERR "can't allocate struct hda_codec\n");
1069 codec->addr = codec_addr;
1070 mutex_init(&codec->spdif_mutex);
1071 mutex_init(&codec->control_mutex);
1072 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
1073 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
1074 snd_array_init(&codec->mixers, sizeof(struct hda_nid_item), 32);
1075 snd_array_init(&codec->nids, sizeof(struct hda_nid_item), 32);
1076 snd_array_init(&codec->init_pins, sizeof(struct hda_pincfg), 16);
1077 snd_array_init(&codec->driver_pins, sizeof(struct hda_pincfg), 16);
1078 snd_array_init(&codec->cvt_setups, sizeof(struct hda_cvt_setup), 8);
1079 if (codec->bus->modelname) {
1080 codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL);
1081 if (!codec->modelname) {
1082 snd_hda_codec_free(codec);
1087 #ifdef CONFIG_SND_HDA_POWER_SAVE
1088 INIT_DELAYED_WORK(&codec->power_work, hda_power_work);
1089 /* snd_hda_codec_new() marks the codec as power-up, and leave it as is.
1090 * the caller has to power down appropriatley after initialization
1093 hda_keep_power_on(codec);
1096 list_add_tail(&codec->list, &bus->codec_list);
1097 bus->caddr_tbl[codec_addr] = codec;
1099 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1101 if (codec->vendor_id == -1)
1102 /* read again, hopefully the access method was corrected
1103 * in the last read...
1105 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1107 codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1108 AC_PAR_SUBSYSTEM_ID);
1109 codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1112 setup_fg_nodes(codec);
1113 if (!codec->afg && !codec->mfg) {
1114 snd_printdd("hda_codec: no AFG or MFG node found\n");
1119 err = read_widget_caps(codec, codec->afg ? codec->afg : codec->mfg);
1121 snd_printk(KERN_ERR "hda_codec: cannot malloc\n");
1124 err = read_pin_defaults(codec);
1128 if (!codec->subsystem_id) {
1129 hda_nid_t nid = codec->afg ? codec->afg : codec->mfg;
1130 codec->subsystem_id =
1131 snd_hda_codec_read(codec, nid, 0,
1132 AC_VERB_GET_SUBSYSTEM_ID, 0);
1135 /* power-up all before initialization */
1136 hda_set_power_state(codec,
1137 codec->afg ? codec->afg : codec->mfg,
1140 snd_hda_codec_proc_new(codec);
1142 snd_hda_create_hwdep(codec);
1144 sprintf(component, "HDA:%08x,%08x,%08x", codec->vendor_id,
1145 codec->subsystem_id, codec->revision_id);
1146 snd_component_add(codec->bus->card, component);
1153 snd_hda_codec_free(codec);
1156 EXPORT_SYMBOL_HDA(snd_hda_codec_new);
1159 * snd_hda_codec_configure - (Re-)configure the HD-audio codec
1160 * @codec: the HDA codec
1162 * Start parsing of the given codec tree and (re-)initialize the whole
1165 * Returns 0 if successful or a negative error code.
1167 int snd_hda_codec_configure(struct hda_codec *codec)
1171 codec->preset = find_codec_preset(codec);
1172 if (!codec->vendor_name || !codec->chip_name) {
1173 err = get_codec_name(codec);
1178 if (is_generic_config(codec)) {
1179 err = snd_hda_parse_generic_codec(codec);
1182 if (codec->preset && codec->preset->patch) {
1183 err = codec->preset->patch(codec);
1187 /* call the default parser */
1188 err = snd_hda_parse_generic_codec(codec);
1190 printk(KERN_ERR "hda-codec: No codec parser is available\n");
1193 if (!err && codec->patch_ops.unsol_event)
1194 err = init_unsol_queue(codec->bus);
1195 /* audio codec should override the mixer name */
1196 if (!err && (codec->afg || !*codec->bus->card->mixername))
1197 snprintf(codec->bus->card->mixername,
1198 sizeof(codec->bus->card->mixername),
1199 "%s %s", codec->vendor_name, codec->chip_name);
1202 EXPORT_SYMBOL_HDA(snd_hda_codec_configure);
1205 * snd_hda_codec_setup_stream - set up the codec for streaming
1206 * @codec: the CODEC to set up
1207 * @nid: the NID to set up
1208 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
1209 * @channel_id: channel id to pass, zero based.
1210 * @format: stream format.
1212 void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
1214 int channel_id, int format)
1216 struct hda_codec *c;
1217 struct hda_cvt_setup *p;
1218 unsigned int oldval, newval;
1225 snd_printdd("hda_codec_setup_stream: "
1226 "NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
1227 nid, stream_tag, channel_id, format);
1228 p = get_hda_cvt_setup(codec, nid);
1231 /* update the stream-id if changed */
1232 if (p->stream_tag != stream_tag || p->channel_id != channel_id) {
1233 oldval = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0);
1234 newval = (stream_tag << 4) | channel_id;
1235 if (oldval != newval)
1236 snd_hda_codec_write(codec, nid, 0,
1237 AC_VERB_SET_CHANNEL_STREAMID,
1239 p->stream_tag = stream_tag;
1240 p->channel_id = channel_id;
1242 /* update the format-id if changed */
1243 if (p->format_id != format) {
1244 oldval = snd_hda_codec_read(codec, nid, 0,
1245 AC_VERB_GET_STREAM_FORMAT, 0);
1246 if (oldval != format) {
1248 snd_hda_codec_write(codec, nid, 0,
1249 AC_VERB_SET_STREAM_FORMAT,
1252 p->format_id = format;
1257 /* make other inactive cvts with the same stream-tag dirty */
1258 type = get_wcaps_type(get_wcaps(codec, nid));
1259 list_for_each_entry(c, &codec->bus->codec_list, list) {
1260 for (i = 0; i < c->cvt_setups.used; i++) {
1261 p = snd_array_elem(&c->cvt_setups, i);
1262 if (!p->active && p->stream_tag == stream_tag &&
1263 get_wcaps_type(get_wcaps(codec, p->nid)) == type)
1268 EXPORT_SYMBOL_HDA(snd_hda_codec_setup_stream);
1270 static void really_cleanup_stream(struct hda_codec *codec,
1271 struct hda_cvt_setup *q);
1274 * __snd_hda_codec_cleanup_stream - clean up the codec for closing
1275 * @codec: the CODEC to clean up
1276 * @nid: the NID to clean up
1277 * @do_now: really clean up the stream instead of clearing the active flag
1279 void __snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid,
1282 struct hda_cvt_setup *p;
1287 snd_printdd("hda_codec_cleanup_stream: NID=0x%x\n", nid);
1288 p = get_hda_cvt_setup(codec, nid);
1290 /* here we just clear the active flag when do_now isn't set;
1291 * actual clean-ups will be done later in
1292 * purify_inactive_streams() called from snd_hda_codec_prpapre()
1295 really_cleanup_stream(codec, p);
1300 EXPORT_SYMBOL_HDA(__snd_hda_codec_cleanup_stream);
1302 static void really_cleanup_stream(struct hda_codec *codec,
1303 struct hda_cvt_setup *q)
1305 hda_nid_t nid = q->nid;
1306 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
1307 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0);
1308 memset(q, 0, sizeof(*q));
1312 /* clean up the all conflicting obsolete streams */
1313 static void purify_inactive_streams(struct hda_codec *codec)
1315 struct hda_codec *c;
1318 list_for_each_entry(c, &codec->bus->codec_list, list) {
1319 for (i = 0; i < c->cvt_setups.used; i++) {
1320 struct hda_cvt_setup *p;
1321 p = snd_array_elem(&c->cvt_setups, i);
1323 really_cleanup_stream(c, p);
1328 /* clean up all streams; called from suspend */
1329 static void hda_cleanup_all_streams(struct hda_codec *codec)
1333 for (i = 0; i < codec->cvt_setups.used; i++) {
1334 struct hda_cvt_setup *p = snd_array_elem(&codec->cvt_setups, i);
1336 really_cleanup_stream(codec, p);
1341 * amp access functions
1344 /* FIXME: more better hash key? */
1345 #define HDA_HASH_KEY(nid, dir, idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
1346 #define HDA_HASH_PINCAP_KEY(nid) (u32)((nid) + (0x02 << 24))
1347 #define HDA_HASH_PARPCM_KEY(nid) (u32)((nid) + (0x03 << 24))
1348 #define HDA_HASH_PARSTR_KEY(nid) (u32)((nid) + (0x04 << 24))
1349 #define INFO_AMP_CAPS (1<<0)
1350 #define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
1352 /* initialize the hash table */
1353 static void /*__devinit*/ init_hda_cache(struct hda_cache_rec *cache,
1354 unsigned int record_size)
1356 memset(cache, 0, sizeof(*cache));
1357 memset(cache->hash, 0xff, sizeof(cache->hash));
1358 snd_array_init(&cache->buf, record_size, 64);
1361 static void free_hda_cache(struct hda_cache_rec *cache)
1363 snd_array_free(&cache->buf);
1366 /* query the hash. allocate an entry if not found. */
1367 static struct hda_cache_head *get_hash(struct hda_cache_rec *cache, u32 key)
1369 u16 idx = key % (u16)ARRAY_SIZE(cache->hash);
1370 u16 cur = cache->hash[idx];
1371 struct hda_cache_head *info;
1373 while (cur != 0xffff) {
1374 info = snd_array_elem(&cache->buf, cur);
1375 if (info->key == key)
1382 /* query the hash. allocate an entry if not found. */
1383 static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache,
1386 struct hda_cache_head *info = get_hash(cache, key);
1389 /* add a new hash entry */
1390 info = snd_array_new(&cache->buf);
1393 cur = snd_array_index(&cache->buf, info);
1396 idx = key % (u16)ARRAY_SIZE(cache->hash);
1397 info->next = cache->hash[idx];
1398 cache->hash[idx] = cur;
1403 /* query and allocate an amp hash entry */
1404 static inline struct hda_amp_info *
1405 get_alloc_amp_hash(struct hda_codec *codec, u32 key)
1407 return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key);
1411 * query_amp_caps - query AMP capabilities
1412 * @codec: the HD-auio codec
1413 * @nid: the NID to query
1414 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1416 * Query AMP capabilities for the given widget and direction.
1417 * Returns the obtained capability bits.
1419 * When cap bits have been already read, this doesn't read again but
1420 * returns the cached value.
1422 u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
1424 struct hda_amp_info *info;
1426 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, 0));
1429 if (!(info->head.val & INFO_AMP_CAPS)) {
1430 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
1432 info->amp_caps = snd_hda_param_read(codec, nid,
1433 direction == HDA_OUTPUT ?
1434 AC_PAR_AMP_OUT_CAP :
1437 info->head.val |= INFO_AMP_CAPS;
1439 return info->amp_caps;
1441 EXPORT_SYMBOL_HDA(query_amp_caps);
1444 * snd_hda_override_amp_caps - Override the AMP capabilities
1445 * @codec: the CODEC to clean up
1446 * @nid: the NID to clean up
1447 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1448 * @caps: the capability bits to set
1450 * Override the cached AMP caps bits value by the given one.
1451 * This function is useful if the driver needs to adjust the AMP ranges,
1452 * e.g. limit to 0dB, etc.
1454 * Returns zero if successful or a negative error code.
1456 int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
1459 struct hda_amp_info *info;
1461 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, dir, 0));
1464 info->amp_caps = caps;
1465 info->head.val |= INFO_AMP_CAPS;
1468 EXPORT_SYMBOL_HDA(snd_hda_override_amp_caps);
1471 query_caps_hash(struct hda_codec *codec, hda_nid_t nid, u32 key,
1472 unsigned int (*func)(struct hda_codec *, hda_nid_t))
1474 struct hda_amp_info *info;
1476 info = get_alloc_amp_hash(codec, key);
1479 if (!info->head.val) {
1480 info->head.val |= INFO_AMP_CAPS;
1481 info->amp_caps = func(codec, nid);
1483 return info->amp_caps;
1486 static unsigned int read_pin_cap(struct hda_codec *codec, hda_nid_t nid)
1488 return snd_hda_param_read(codec, nid, AC_PAR_PIN_CAP);
1492 * snd_hda_query_pin_caps - Query PIN capabilities
1493 * @codec: the HD-auio codec
1494 * @nid: the NID to query
1496 * Query PIN capabilities for the given widget.
1497 * Returns the obtained capability bits.
1499 * When cap bits have been already read, this doesn't read again but
1500 * returns the cached value.
1502 u32 snd_hda_query_pin_caps(struct hda_codec *codec, hda_nid_t nid)
1504 return query_caps_hash(codec, nid, HDA_HASH_PINCAP_KEY(nid),
1507 EXPORT_SYMBOL_HDA(snd_hda_query_pin_caps);
1510 * snd_hda_pin_sense - execute pin sense measurement
1511 * @codec: the CODEC to sense
1512 * @nid: the pin NID to sense
1514 * Execute necessary pin sense measurement and return its Presence Detect,
1515 * Impedance, ELD Valid etc. status bits.
1517 u32 snd_hda_pin_sense(struct hda_codec *codec, hda_nid_t nid)
1521 if (!codec->no_trigger_sense) {
1522 pincap = snd_hda_query_pin_caps(codec, nid);
1523 if (pincap & AC_PINCAP_TRIG_REQ) /* need trigger? */
1524 snd_hda_codec_read(codec, nid, 0,
1525 AC_VERB_SET_PIN_SENSE, 0);
1527 return snd_hda_codec_read(codec, nid, 0,
1528 AC_VERB_GET_PIN_SENSE, 0);
1530 EXPORT_SYMBOL_HDA(snd_hda_pin_sense);
1533 * snd_hda_jack_detect - query pin Presence Detect status
1534 * @codec: the CODEC to sense
1535 * @nid: the pin NID to sense
1537 * Query and return the pin's Presence Detect status.
1539 int snd_hda_jack_detect(struct hda_codec *codec, hda_nid_t nid)
1541 u32 sense = snd_hda_pin_sense(codec, nid);
1542 return !!(sense & AC_PINSENSE_PRESENCE);
1544 EXPORT_SYMBOL_HDA(snd_hda_jack_detect);
1547 * read the current volume to info
1548 * if the cache exists, read the cache value.
1550 static unsigned int get_vol_mute(struct hda_codec *codec,
1551 struct hda_amp_info *info, hda_nid_t nid,
1552 int ch, int direction, int index)
1556 if (info->head.val & INFO_AMP_VOL(ch))
1557 return info->vol[ch];
1559 parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
1560 parm |= direction == HDA_OUTPUT ? AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
1562 val = snd_hda_codec_read(codec, nid, 0,
1563 AC_VERB_GET_AMP_GAIN_MUTE, parm);
1564 info->vol[ch] = val & 0xff;
1565 info->head.val |= INFO_AMP_VOL(ch);
1566 return info->vol[ch];
1570 * write the current volume in info to the h/w and update the cache
1572 static void put_vol_mute(struct hda_codec *codec, struct hda_amp_info *info,
1573 hda_nid_t nid, int ch, int direction, int index,
1578 parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
1579 parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
1580 parm |= index << AC_AMP_SET_INDEX_SHIFT;
1582 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
1583 info->vol[ch] = val;
1587 * snd_hda_codec_amp_read - Read AMP value
1588 * @codec: HD-audio codec
1589 * @nid: NID to read the AMP value
1590 * @ch: channel (left=0 or right=1)
1591 * @direction: #HDA_INPUT or #HDA_OUTPUT
1592 * @index: the index value (only for input direction)
1594 * Read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
1596 int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
1597 int direction, int index)
1599 struct hda_amp_info *info;
1600 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
1603 return get_vol_mute(codec, info, nid, ch, direction, index);
1605 EXPORT_SYMBOL_HDA(snd_hda_codec_amp_read);
1608 * snd_hda_codec_amp_update - update the AMP value
1609 * @codec: HD-audio codec
1610 * @nid: NID to read the AMP value
1611 * @ch: channel (left=0 or right=1)
1612 * @direction: #HDA_INPUT or #HDA_OUTPUT
1613 * @idx: the index value (only for input direction)
1614 * @mask: bit mask to set
1615 * @val: the bits value to set
1617 * Update the AMP value with a bit mask.
1618 * Returns 0 if the value is unchanged, 1 if changed.
1620 int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
1621 int direction, int idx, int mask, int val)
1623 struct hda_amp_info *info;
1625 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, idx));
1628 if (snd_BUG_ON(mask & ~0xff))
1631 val |= get_vol_mute(codec, info, nid, ch, direction, idx) & ~mask;
1632 if (info->vol[ch] == val)
1634 put_vol_mute(codec, info, nid, ch, direction, idx, val);
1637 EXPORT_SYMBOL_HDA(snd_hda_codec_amp_update);
1640 * snd_hda_codec_amp_stereo - update the AMP stereo values
1641 * @codec: HD-audio codec
1642 * @nid: NID to read the AMP value
1643 * @direction: #HDA_INPUT or #HDA_OUTPUT
1644 * @idx: the index value (only for input direction)
1645 * @mask: bit mask to set
1646 * @val: the bits value to set
1648 * Update the AMP values like snd_hda_codec_amp_update(), but for a
1649 * stereo widget with the same mask and value.
1651 int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
1652 int direction, int idx, int mask, int val)
1656 if (snd_BUG_ON(mask & ~0xff))
1658 for (ch = 0; ch < 2; ch++)
1659 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
1663 EXPORT_SYMBOL_HDA(snd_hda_codec_amp_stereo);
1665 #ifdef SND_HDA_NEEDS_RESUME
1667 * snd_hda_codec_resume_amp - Resume all AMP commands from the cache
1668 * @codec: HD-audio codec
1670 * Resume the all amp commands from the cache.
1672 void snd_hda_codec_resume_amp(struct hda_codec *codec)
1674 struct hda_amp_info *buffer = codec->amp_cache.buf.list;
1677 for (i = 0; i < codec->amp_cache.buf.used; i++, buffer++) {
1678 u32 key = buffer->head.key;
1680 unsigned int idx, dir, ch;
1684 idx = (key >> 16) & 0xff;
1685 dir = (key >> 24) & 0xff;
1686 for (ch = 0; ch < 2; ch++) {
1687 if (!(buffer->head.val & INFO_AMP_VOL(ch)))
1689 put_vol_mute(codec, buffer, nid, ch, dir, idx,
1694 EXPORT_SYMBOL_HDA(snd_hda_codec_resume_amp);
1695 #endif /* SND_HDA_NEEDS_RESUME */
1697 static u32 get_amp_max_value(struct hda_codec *codec, hda_nid_t nid, int dir,
1700 u32 caps = query_amp_caps(codec, nid, dir);
1702 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1709 * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer
1711 * The control element is supposed to have the private_value field
1712 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1714 int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
1715 struct snd_ctl_elem_info *uinfo)
1717 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1718 u16 nid = get_amp_nid(kcontrol);
1719 u8 chs = get_amp_channels(kcontrol);
1720 int dir = get_amp_direction(kcontrol);
1721 unsigned int ofs = get_amp_offset(kcontrol);
1723 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1724 uinfo->count = chs == 3 ? 2 : 1;
1725 uinfo->value.integer.min = 0;
1726 uinfo->value.integer.max = get_amp_max_value(codec, nid, dir, ofs);
1727 if (!uinfo->value.integer.max) {
1728 printk(KERN_WARNING "hda_codec: "
1729 "num_steps = 0 for NID=0x%x (ctl = %s)\n", nid,
1735 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_info);
1738 static inline unsigned int
1739 read_amp_value(struct hda_codec *codec, hda_nid_t nid,
1740 int ch, int dir, int idx, unsigned int ofs)
1743 val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
1744 val &= HDA_AMP_VOLMASK;
1753 update_amp_value(struct hda_codec *codec, hda_nid_t nid,
1754 int ch, int dir, int idx, unsigned int ofs,
1757 unsigned int maxval;
1761 /* ofs = 0: raw max value */
1762 maxval = get_amp_max_value(codec, nid, dir, 0);
1765 return snd_hda_codec_amp_update(codec, nid, ch, dir, idx,
1766 HDA_AMP_VOLMASK, val);
1770 * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume
1772 * The control element is supposed to have the private_value field
1773 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1775 int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
1776 struct snd_ctl_elem_value *ucontrol)
1778 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1779 hda_nid_t nid = get_amp_nid(kcontrol);
1780 int chs = get_amp_channels(kcontrol);
1781 int dir = get_amp_direction(kcontrol);
1782 int idx = get_amp_index(kcontrol);
1783 unsigned int ofs = get_amp_offset(kcontrol);
1784 long *valp = ucontrol->value.integer.value;
1787 *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs);
1789 *valp = read_amp_value(codec, nid, 1, dir, idx, ofs);
1792 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_get);
1795 * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume
1797 * The control element is supposed to have the private_value field
1798 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1800 int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
1801 struct snd_ctl_elem_value *ucontrol)
1803 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1804 hda_nid_t nid = get_amp_nid(kcontrol);
1805 int chs = get_amp_channels(kcontrol);
1806 int dir = get_amp_direction(kcontrol);
1807 int idx = get_amp_index(kcontrol);
1808 unsigned int ofs = get_amp_offset(kcontrol);
1809 long *valp = ucontrol->value.integer.value;
1812 snd_hda_power_up(codec);
1814 change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp);
1818 change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp);
1819 snd_hda_power_down(codec);
1822 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_put);
1825 * snd_hda_mixer_amp_volume_put - TLV callback for a standard AMP mixer volume
1827 * The control element is supposed to have the private_value field
1828 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1830 int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
1831 unsigned int size, unsigned int __user *_tlv)
1833 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1834 hda_nid_t nid = get_amp_nid(kcontrol);
1835 int dir = get_amp_direction(kcontrol);
1836 unsigned int ofs = get_amp_offset(kcontrol);
1837 bool min_mute = get_amp_min_mute(kcontrol);
1838 u32 caps, val1, val2;
1840 if (size < 4 * sizeof(unsigned int))
1842 caps = query_amp_caps(codec, nid, dir);
1843 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
1844 val2 = (val2 + 1) * 25;
1845 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
1847 val1 = ((int)val1) * ((int)val2);
1849 val2 |= TLV_DB_SCALE_MUTE;
1850 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
1852 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
1854 if (put_user(val1, _tlv + 2))
1856 if (put_user(val2, _tlv + 3))
1860 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_tlv);
1863 * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control
1864 * @codec: HD-audio codec
1865 * @nid: NID of a reference widget
1866 * @dir: #HDA_INPUT or #HDA_OUTPUT
1867 * @tlv: TLV data to be stored, at least 4 elements
1869 * Set (static) TLV data for a virtual master volume using the AMP caps
1870 * obtained from the reference NID.
1871 * The volume range is recalculated as if the max volume is 0dB.
1873 void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
1879 caps = query_amp_caps(codec, nid, dir);
1880 nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1881 step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
1882 step = (step + 1) * 25;
1883 tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
1884 tlv[1] = 2 * sizeof(unsigned int);
1885 tlv[2] = -nums * step;
1888 EXPORT_SYMBOL_HDA(snd_hda_set_vmaster_tlv);
1890 /* find a mixer control element with the given name */
1891 static struct snd_kcontrol *
1892 _snd_hda_find_mixer_ctl(struct hda_codec *codec,
1893 const char *name, int idx)
1895 struct snd_ctl_elem_id id;
1896 memset(&id, 0, sizeof(id));
1897 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1899 if (snd_BUG_ON(strlen(name) >= sizeof(id.name)))
1901 strcpy(id.name, name);
1902 return snd_ctl_find_id(codec->bus->card, &id);
1906 * snd_hda_find_mixer_ctl - Find a mixer control element with the given name
1907 * @codec: HD-audio codec
1908 * @name: ctl id name string
1910 * Get the control element with the given id string and IFACE_MIXER.
1912 struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
1915 return _snd_hda_find_mixer_ctl(codec, name, 0);
1917 EXPORT_SYMBOL_HDA(snd_hda_find_mixer_ctl);
1920 * snd_hda_ctl_add - Add a control element and assign to the codec
1921 * @codec: HD-audio codec
1922 * @nid: corresponding NID (optional)
1923 * @kctl: the control element to assign
1925 * Add the given control element to an array inside the codec instance.
1926 * All control elements belonging to a codec are supposed to be added
1927 * by this function so that a proper clean-up works at the free or
1928 * reconfiguration time.
1930 * If non-zero @nid is passed, the NID is assigned to the control element.
1931 * The assignment is shown in the codec proc file.
1933 * snd_hda_ctl_add() checks the control subdev id field whether
1934 * #HDA_SUBDEV_NID_FLAG bit is set. If set (and @nid is zero), the lower
1935 * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit
1936 * specifies if kctl->private_value is a HDA amplifier value.
1938 int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid,
1939 struct snd_kcontrol *kctl)
1942 unsigned short flags = 0;
1943 struct hda_nid_item *item;
1945 if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) {
1946 flags |= HDA_NID_ITEM_AMP;
1948 nid = get_amp_nid_(kctl->private_value);
1950 if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0)
1951 nid = kctl->id.subdevice & 0xffff;
1952 if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG))
1953 kctl->id.subdevice = 0;
1954 err = snd_ctl_add(codec->bus->card, kctl);
1957 item = snd_array_new(&codec->mixers);
1962 item->flags = flags;
1965 EXPORT_SYMBOL_HDA(snd_hda_ctl_add);
1968 * snd_hda_add_nid - Assign a NID to a control element
1969 * @codec: HD-audio codec
1970 * @nid: corresponding NID (optional)
1971 * @kctl: the control element to assign
1972 * @index: index to kctl
1974 * Add the given control element to an array inside the codec instance.
1975 * This function is used when #snd_hda_ctl_add cannot be used for 1:1
1976 * NID:KCTL mapping - for example "Capture Source" selector.
1978 int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl,
1979 unsigned int index, hda_nid_t nid)
1981 struct hda_nid_item *item;
1984 item = snd_array_new(&codec->nids);
1988 item->index = index;
1992 printk(KERN_ERR "hda-codec: no NID for mapping control %s:%d:%d\n",
1993 kctl->id.name, kctl->id.index, index);
1996 EXPORT_SYMBOL_HDA(snd_hda_add_nid);
1999 * snd_hda_ctls_clear - Clear all controls assigned to the given codec
2000 * @codec: HD-audio codec
2002 void snd_hda_ctls_clear(struct hda_codec *codec)
2005 struct hda_nid_item *items = codec->mixers.list;
2006 for (i = 0; i < codec->mixers.used; i++)
2007 snd_ctl_remove(codec->bus->card, items[i].kctl);
2008 snd_array_free(&codec->mixers);
2009 snd_array_free(&codec->nids);
2012 /* pseudo device locking
2013 * toggle card->shutdown to allow/disallow the device access (as a hack)
2015 static int hda_lock_devices(struct snd_card *card)
2017 spin_lock(&card->files_lock);
2018 if (card->shutdown) {
2019 spin_unlock(&card->files_lock);
2023 spin_unlock(&card->files_lock);
2027 static void hda_unlock_devices(struct snd_card *card)
2029 spin_lock(&card->files_lock);
2031 spin_unlock(&card->files_lock);
2035 * snd_hda_codec_reset - Clear all objects assigned to the codec
2036 * @codec: HD-audio codec
2038 * This frees the all PCM and control elements assigned to the codec, and
2039 * clears the caches and restores the pin default configurations.
2041 * When a device is being used, it returns -EBSY. If successfully freed,
2044 int snd_hda_codec_reset(struct hda_codec *codec)
2046 struct snd_card *card = codec->bus->card;
2049 if (hda_lock_devices(card) < 0)
2051 /* check whether the codec isn't used by any mixer or PCM streams */
2052 if (!list_empty(&card->ctl_files)) {
2053 hda_unlock_devices(card);
2056 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
2057 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
2060 if (cpcm->pcm->streams[0].substream_opened ||
2061 cpcm->pcm->streams[1].substream_opened) {
2062 hda_unlock_devices(card);
2067 /* OK, let it free */
2069 #ifdef CONFIG_SND_HDA_POWER_SAVE
2070 cancel_delayed_work(&codec->power_work);
2071 flush_workqueue(codec->bus->workq);
2073 snd_hda_ctls_clear(codec);
2075 for (i = 0; i < codec->num_pcms; i++) {
2076 if (codec->pcm_info[i].pcm) {
2077 snd_device_free(card, codec->pcm_info[i].pcm);
2078 clear_bit(codec->pcm_info[i].device,
2079 codec->bus->pcm_dev_bits);
2082 if (codec->patch_ops.free)
2083 codec->patch_ops.free(codec);
2084 codec->proc_widget_hook = NULL;
2086 free_hda_cache(&codec->amp_cache);
2087 free_hda_cache(&codec->cmd_cache);
2088 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
2089 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
2090 /* free only driver_pins so that init_pins + user_pins are restored */
2091 snd_array_free(&codec->driver_pins);
2092 restore_pincfgs(codec);
2093 codec->num_pcms = 0;
2094 codec->pcm_info = NULL;
2095 codec->preset = NULL;
2096 memset(&codec->patch_ops, 0, sizeof(codec->patch_ops));
2097 codec->slave_dig_outs = NULL;
2098 codec->spdif_status_reset = 0;
2099 module_put(codec->owner);
2100 codec->owner = NULL;
2102 /* allow device access again */
2103 hda_unlock_devices(card);
2108 * snd_hda_add_vmaster - create a virtual master control and add slaves
2109 * @codec: HD-audio codec
2110 * @name: vmaster control name
2111 * @tlv: TLV data (optional)
2112 * @slaves: slave control names (optional)
2114 * Create a virtual master control with the given name. The TLV data
2115 * must be either NULL or a valid data.
2117 * @slaves is a NULL-terminated array of strings, each of which is a
2118 * slave control name. All controls with these names are assigned to
2119 * the new virtual master control.
2121 * This function returns zero if successful or a negative error code.
2123 int snd_hda_add_vmaster(struct hda_codec *codec, char *name,
2124 unsigned int *tlv, const char **slaves)
2126 struct snd_kcontrol *kctl;
2130 for (s = slaves; *s && !snd_hda_find_mixer_ctl(codec, *s); s++)
2133 snd_printdd("No slave found for %s\n", name);
2136 kctl = snd_ctl_make_virtual_master(name, tlv);
2139 err = snd_hda_ctl_add(codec, 0, kctl);
2143 for (s = slaves; *s; s++) {
2144 struct snd_kcontrol *sctl;
2147 sctl = _snd_hda_find_mixer_ctl(codec, *s, i);
2150 snd_printdd("Cannot find slave %s, "
2154 err = snd_ctl_add_slave(kctl, sctl);
2162 EXPORT_SYMBOL_HDA(snd_hda_add_vmaster);
2165 * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch
2167 * The control element is supposed to have the private_value field
2168 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2170 int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
2171 struct snd_ctl_elem_info *uinfo)
2173 int chs = get_amp_channels(kcontrol);
2175 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2176 uinfo->count = chs == 3 ? 2 : 1;
2177 uinfo->value.integer.min = 0;
2178 uinfo->value.integer.max = 1;
2181 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_info);
2184 * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch
2186 * The control element is supposed to have the private_value field
2187 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2189 int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
2190 struct snd_ctl_elem_value *ucontrol)
2192 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2193 hda_nid_t nid = get_amp_nid(kcontrol);
2194 int chs = get_amp_channels(kcontrol);
2195 int dir = get_amp_direction(kcontrol);
2196 int idx = get_amp_index(kcontrol);
2197 long *valp = ucontrol->value.integer.value;
2200 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
2201 HDA_AMP_MUTE) ? 0 : 1;
2203 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
2204 HDA_AMP_MUTE) ? 0 : 1;
2207 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_get);
2210 * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch
2212 * The control element is supposed to have the private_value field
2213 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2215 int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
2216 struct snd_ctl_elem_value *ucontrol)
2218 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2219 hda_nid_t nid = get_amp_nid(kcontrol);
2220 int chs = get_amp_channels(kcontrol);
2221 int dir = get_amp_direction(kcontrol);
2222 int idx = get_amp_index(kcontrol);
2223 long *valp = ucontrol->value.integer.value;
2226 snd_hda_power_up(codec);
2228 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
2230 *valp ? 0 : HDA_AMP_MUTE);
2234 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
2236 *valp ? 0 : HDA_AMP_MUTE);
2237 hda_call_check_power_status(codec, nid);
2238 snd_hda_power_down(codec);
2241 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put);
2243 #ifdef CONFIG_SND_HDA_INPUT_BEEP
2245 * snd_hda_mixer_amp_switch_put_beep - Put callback for a beep AMP switch
2247 * This function calls snd_hda_enable_beep_device(), which behaves differently
2248 * depending on beep_mode option.
2250 int snd_hda_mixer_amp_switch_put_beep(struct snd_kcontrol *kcontrol,
2251 struct snd_ctl_elem_value *ucontrol)
2253 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2254 long *valp = ucontrol->value.integer.value;
2256 snd_hda_enable_beep_device(codec, *valp);
2257 return snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2259 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put_beep);
2260 #endif /* CONFIG_SND_HDA_INPUT_BEEP */
2263 * bound volume controls
2265 * bind multiple volumes (# indices, from 0)
2268 #define AMP_VAL_IDX_SHIFT 19
2269 #define AMP_VAL_IDX_MASK (0x0f<<19)
2272 * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control
2274 * The control element is supposed to have the private_value field
2275 * set up via HDA_BIND_MUTE*() macros.
2277 int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
2278 struct snd_ctl_elem_value *ucontrol)
2280 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2284 mutex_lock(&codec->control_mutex);
2285 pval = kcontrol->private_value;
2286 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
2287 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
2288 kcontrol->private_value = pval;
2289 mutex_unlock(&codec->control_mutex);
2292 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_get);
2295 * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control
2297 * The control element is supposed to have the private_value field
2298 * set up via HDA_BIND_MUTE*() macros.
2300 int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
2301 struct snd_ctl_elem_value *ucontrol)
2303 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2305 int i, indices, err = 0, change = 0;
2307 mutex_lock(&codec->control_mutex);
2308 pval = kcontrol->private_value;
2309 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
2310 for (i = 0; i < indices; i++) {
2311 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
2312 (i << AMP_VAL_IDX_SHIFT);
2313 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2318 kcontrol->private_value = pval;
2319 mutex_unlock(&codec->control_mutex);
2320 return err < 0 ? err : change;
2322 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_put);
2325 * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control
2327 * The control element is supposed to have the private_value field
2328 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2330 int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
2331 struct snd_ctl_elem_info *uinfo)
2333 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2334 struct hda_bind_ctls *c;
2337 mutex_lock(&codec->control_mutex);
2338 c = (struct hda_bind_ctls *)kcontrol->private_value;
2339 kcontrol->private_value = *c->values;
2340 err = c->ops->info(kcontrol, uinfo);
2341 kcontrol->private_value = (long)c;
2342 mutex_unlock(&codec->control_mutex);
2345 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_info);
2348 * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control
2350 * The control element is supposed to have the private_value field
2351 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2353 int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
2354 struct snd_ctl_elem_value *ucontrol)
2356 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2357 struct hda_bind_ctls *c;
2360 mutex_lock(&codec->control_mutex);
2361 c = (struct hda_bind_ctls *)kcontrol->private_value;
2362 kcontrol->private_value = *c->values;
2363 err = c->ops->get(kcontrol, ucontrol);
2364 kcontrol->private_value = (long)c;
2365 mutex_unlock(&codec->control_mutex);
2368 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_get);
2371 * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control
2373 * The control element is supposed to have the private_value field
2374 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2376 int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
2377 struct snd_ctl_elem_value *ucontrol)
2379 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2380 struct hda_bind_ctls *c;
2381 unsigned long *vals;
2382 int err = 0, change = 0;
2384 mutex_lock(&codec->control_mutex);
2385 c = (struct hda_bind_ctls *)kcontrol->private_value;
2386 for (vals = c->values; *vals; vals++) {
2387 kcontrol->private_value = *vals;
2388 err = c->ops->put(kcontrol, ucontrol);
2393 kcontrol->private_value = (long)c;
2394 mutex_unlock(&codec->control_mutex);
2395 return err < 0 ? err : change;
2397 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_put);
2400 * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control
2402 * The control element is supposed to have the private_value field
2403 * set up via HDA_BIND_VOL() macro.
2405 int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2406 unsigned int size, unsigned int __user *tlv)
2408 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2409 struct hda_bind_ctls *c;
2412 mutex_lock(&codec->control_mutex);
2413 c = (struct hda_bind_ctls *)kcontrol->private_value;
2414 kcontrol->private_value = *c->values;
2415 err = c->ops->tlv(kcontrol, op_flag, size, tlv);
2416 kcontrol->private_value = (long)c;
2417 mutex_unlock(&codec->control_mutex);
2420 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_tlv);
2422 struct hda_ctl_ops snd_hda_bind_vol = {
2423 .info = snd_hda_mixer_amp_volume_info,
2424 .get = snd_hda_mixer_amp_volume_get,
2425 .put = snd_hda_mixer_amp_volume_put,
2426 .tlv = snd_hda_mixer_amp_tlv
2428 EXPORT_SYMBOL_HDA(snd_hda_bind_vol);
2430 struct hda_ctl_ops snd_hda_bind_sw = {
2431 .info = snd_hda_mixer_amp_switch_info,
2432 .get = snd_hda_mixer_amp_switch_get,
2433 .put = snd_hda_mixer_amp_switch_put,
2434 .tlv = snd_hda_mixer_amp_tlv
2436 EXPORT_SYMBOL_HDA(snd_hda_bind_sw);
2439 * SPDIF out controls
2442 static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
2443 struct snd_ctl_elem_info *uinfo)
2445 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
2450 static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
2451 struct snd_ctl_elem_value *ucontrol)
2453 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2454 IEC958_AES0_NONAUDIO |
2455 IEC958_AES0_CON_EMPHASIS_5015 |
2456 IEC958_AES0_CON_NOT_COPYRIGHT;
2457 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
2458 IEC958_AES1_CON_ORIGINAL;
2462 static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
2463 struct snd_ctl_elem_value *ucontrol)
2465 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2466 IEC958_AES0_NONAUDIO |
2467 IEC958_AES0_PRO_EMPHASIS_5015;
2471 static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
2472 struct snd_ctl_elem_value *ucontrol)
2474 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2476 ucontrol->value.iec958.status[0] = codec->spdif_status & 0xff;
2477 ucontrol->value.iec958.status[1] = (codec->spdif_status >> 8) & 0xff;
2478 ucontrol->value.iec958.status[2] = (codec->spdif_status >> 16) & 0xff;
2479 ucontrol->value.iec958.status[3] = (codec->spdif_status >> 24) & 0xff;
2484 /* convert from SPDIF status bits to HDA SPDIF bits
2485 * bit 0 (DigEn) is always set zero (to be filled later)
2487 static unsigned short convert_from_spdif_status(unsigned int sbits)
2489 unsigned short val = 0;
2491 if (sbits & IEC958_AES0_PROFESSIONAL)
2492 val |= AC_DIG1_PROFESSIONAL;
2493 if (sbits & IEC958_AES0_NONAUDIO)
2494 val |= AC_DIG1_NONAUDIO;
2495 if (sbits & IEC958_AES0_PROFESSIONAL) {
2496 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
2497 IEC958_AES0_PRO_EMPHASIS_5015)
2498 val |= AC_DIG1_EMPHASIS;
2500 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
2501 IEC958_AES0_CON_EMPHASIS_5015)
2502 val |= AC_DIG1_EMPHASIS;
2503 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
2504 val |= AC_DIG1_COPYRIGHT;
2505 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
2506 val |= AC_DIG1_LEVEL;
2507 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
2512 /* convert to SPDIF status bits from HDA SPDIF bits
2514 static unsigned int convert_to_spdif_status(unsigned short val)
2516 unsigned int sbits = 0;
2518 if (val & AC_DIG1_NONAUDIO)
2519 sbits |= IEC958_AES0_NONAUDIO;
2520 if (val & AC_DIG1_PROFESSIONAL)
2521 sbits |= IEC958_AES0_PROFESSIONAL;
2522 if (sbits & IEC958_AES0_PROFESSIONAL) {
2523 if (sbits & AC_DIG1_EMPHASIS)
2524 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
2526 if (val & AC_DIG1_EMPHASIS)
2527 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
2528 if (!(val & AC_DIG1_COPYRIGHT))
2529 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
2530 if (val & AC_DIG1_LEVEL)
2531 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
2532 sbits |= val & (0x7f << 8);
2537 /* set digital convert verbs both for the given NID and its slaves */
2538 static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
2543 snd_hda_codec_write_cache(codec, nid, 0, verb, val);
2544 d = codec->slave_dig_outs;
2548 snd_hda_codec_write_cache(codec, *d, 0, verb, val);
2551 static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
2555 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1);
2557 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2);
2560 static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
2561 struct snd_ctl_elem_value *ucontrol)
2563 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2564 hda_nid_t nid = kcontrol->private_value;
2568 mutex_lock(&codec->spdif_mutex);
2569 codec->spdif_status = ucontrol->value.iec958.status[0] |
2570 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
2571 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
2572 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
2573 val = convert_from_spdif_status(codec->spdif_status);
2574 val |= codec->spdif_ctls & 1;
2575 change = codec->spdif_ctls != val;
2576 codec->spdif_ctls = val;
2579 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
2581 mutex_unlock(&codec->spdif_mutex);
2585 #define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
2587 static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
2588 struct snd_ctl_elem_value *ucontrol)
2590 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2592 ucontrol->value.integer.value[0] = codec->spdif_ctls & AC_DIG1_ENABLE;
2596 static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
2597 struct snd_ctl_elem_value *ucontrol)
2599 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2600 hda_nid_t nid = kcontrol->private_value;
2604 mutex_lock(&codec->spdif_mutex);
2605 val = codec->spdif_ctls & ~AC_DIG1_ENABLE;
2606 if (ucontrol->value.integer.value[0])
2607 val |= AC_DIG1_ENABLE;
2608 change = codec->spdif_ctls != val;
2610 codec->spdif_ctls = val;
2611 set_dig_out_convert(codec, nid, val & 0xff, -1);
2612 /* unmute amp switch (if any) */
2613 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
2614 (val & AC_DIG1_ENABLE))
2615 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
2618 mutex_unlock(&codec->spdif_mutex);
2622 static struct snd_kcontrol_new dig_mixes[] = {
2624 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2625 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2626 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
2627 .info = snd_hda_spdif_mask_info,
2628 .get = snd_hda_spdif_cmask_get,
2631 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2632 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2633 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK),
2634 .info = snd_hda_spdif_mask_info,
2635 .get = snd_hda_spdif_pmask_get,
2638 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2639 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
2640 .info = snd_hda_spdif_mask_info,
2641 .get = snd_hda_spdif_default_get,
2642 .put = snd_hda_spdif_default_put,
2645 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2646 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
2647 .info = snd_hda_spdif_out_switch_info,
2648 .get = snd_hda_spdif_out_switch_get,
2649 .put = snd_hda_spdif_out_switch_put,
2654 #define SPDIF_MAX_IDX 4 /* 4 instances should be enough to probe */
2657 * snd_hda_create_spdif_out_ctls - create Output SPDIF-related controls
2658 * @codec: the HDA codec
2659 * @nid: audio out widget NID
2661 * Creates controls related with the SPDIF output.
2662 * Called from each patch supporting the SPDIF out.
2664 * Returns 0 if successful, or a negative error code.
2666 int snd_hda_create_spdif_out_ctls(struct hda_codec *codec, hda_nid_t nid)
2669 struct snd_kcontrol *kctl;
2670 struct snd_kcontrol_new *dig_mix;
2673 for (idx = 0; idx < SPDIF_MAX_IDX; idx++) {
2674 if (!_snd_hda_find_mixer_ctl(codec, "IEC958 Playback Switch",
2678 if (idx >= SPDIF_MAX_IDX) {
2679 printk(KERN_ERR "hda_codec: too many IEC958 outputs\n");
2682 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
2683 kctl = snd_ctl_new1(dig_mix, codec);
2686 kctl->id.index = idx;
2687 kctl->private_value = nid;
2688 err = snd_hda_ctl_add(codec, nid, kctl);
2693 snd_hda_codec_read(codec, nid, 0,
2694 AC_VERB_GET_DIGI_CONVERT_1, 0);
2695 codec->spdif_status = convert_to_spdif_status(codec->spdif_ctls);
2698 EXPORT_SYMBOL_HDA(snd_hda_create_spdif_out_ctls);
2701 * SPDIF sharing with analog output
2703 static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
2704 struct snd_ctl_elem_value *ucontrol)
2706 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
2707 ucontrol->value.integer.value[0] = mout->share_spdif;
2711 static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
2712 struct snd_ctl_elem_value *ucontrol)
2714 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
2715 mout->share_spdif = !!ucontrol->value.integer.value[0];
2719 static struct snd_kcontrol_new spdif_share_sw = {
2720 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2721 .name = "IEC958 Default PCM Playback Switch",
2722 .info = snd_ctl_boolean_mono_info,
2723 .get = spdif_share_sw_get,
2724 .put = spdif_share_sw_put,
2728 * snd_hda_create_spdif_share_sw - create Default PCM switch
2729 * @codec: the HDA codec
2730 * @mout: multi-out instance
2732 int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
2733 struct hda_multi_out *mout)
2735 if (!mout->dig_out_nid)
2737 /* ATTENTION: here mout is passed as private_data, instead of codec */
2738 return snd_hda_ctl_add(codec, mout->dig_out_nid,
2739 snd_ctl_new1(&spdif_share_sw, mout));
2741 EXPORT_SYMBOL_HDA(snd_hda_create_spdif_share_sw);
2747 #define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
2749 static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
2750 struct snd_ctl_elem_value *ucontrol)
2752 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2754 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
2758 static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
2759 struct snd_ctl_elem_value *ucontrol)
2761 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2762 hda_nid_t nid = kcontrol->private_value;
2763 unsigned int val = !!ucontrol->value.integer.value[0];
2766 mutex_lock(&codec->spdif_mutex);
2767 change = codec->spdif_in_enable != val;
2769 codec->spdif_in_enable = val;
2770 snd_hda_codec_write_cache(codec, nid, 0,
2771 AC_VERB_SET_DIGI_CONVERT_1, val);
2773 mutex_unlock(&codec->spdif_mutex);
2777 static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
2778 struct snd_ctl_elem_value *ucontrol)
2780 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2781 hda_nid_t nid = kcontrol->private_value;
2785 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0);
2786 sbits = convert_to_spdif_status(val);
2787 ucontrol->value.iec958.status[0] = sbits;
2788 ucontrol->value.iec958.status[1] = sbits >> 8;
2789 ucontrol->value.iec958.status[2] = sbits >> 16;
2790 ucontrol->value.iec958.status[3] = sbits >> 24;
2794 static struct snd_kcontrol_new dig_in_ctls[] = {
2796 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2797 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH),
2798 .info = snd_hda_spdif_in_switch_info,
2799 .get = snd_hda_spdif_in_switch_get,
2800 .put = snd_hda_spdif_in_switch_put,
2803 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2804 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2805 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
2806 .info = snd_hda_spdif_mask_info,
2807 .get = snd_hda_spdif_in_status_get,
2813 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
2814 * @codec: the HDA codec
2815 * @nid: audio in widget NID
2817 * Creates controls related with the SPDIF input.
2818 * Called from each patch supporting the SPDIF in.
2820 * Returns 0 if successful, or a negative error code.
2822 int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
2825 struct snd_kcontrol *kctl;
2826 struct snd_kcontrol_new *dig_mix;
2829 for (idx = 0; idx < SPDIF_MAX_IDX; idx++) {
2830 if (!_snd_hda_find_mixer_ctl(codec, "IEC958 Capture Switch",
2834 if (idx >= SPDIF_MAX_IDX) {
2835 printk(KERN_ERR "hda_codec: too many IEC958 inputs\n");
2838 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
2839 kctl = snd_ctl_new1(dig_mix, codec);
2842 kctl->private_value = nid;
2843 err = snd_hda_ctl_add(codec, nid, kctl);
2847 codec->spdif_in_enable =
2848 snd_hda_codec_read(codec, nid, 0,
2849 AC_VERB_GET_DIGI_CONVERT_1, 0) &
2853 EXPORT_SYMBOL_HDA(snd_hda_create_spdif_in_ctls);
2855 #ifdef SND_HDA_NEEDS_RESUME
2860 /* build a 32bit cache key with the widget id and the command parameter */
2861 #define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
2862 #define get_cmd_cache_nid(key) ((key) & 0xff)
2863 #define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
2866 * snd_hda_codec_write_cache - send a single command with caching
2867 * @codec: the HDA codec
2868 * @nid: NID to send the command
2869 * @direct: direct flag
2870 * @verb: the verb to send
2871 * @parm: the parameter for the verb
2873 * Send a single command without waiting for response.
2875 * Returns 0 if successful, or a negative error code.
2877 int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
2878 int direct, unsigned int verb, unsigned int parm)
2880 int err = snd_hda_codec_write(codec, nid, direct, verb, parm);
2881 struct hda_cache_head *c;
2886 /* parm may contain the verb stuff for get/set amp */
2887 verb = verb | (parm >> 8);
2889 key = build_cmd_cache_key(nid, verb);
2890 mutex_lock(&codec->bus->cmd_mutex);
2891 c = get_alloc_hash(&codec->cmd_cache, key);
2894 mutex_unlock(&codec->bus->cmd_mutex);
2897 EXPORT_SYMBOL_HDA(snd_hda_codec_write_cache);
2900 * snd_hda_codec_update_cache - check cache and write the cmd only when needed
2901 * @codec: the HDA codec
2902 * @nid: NID to send the command
2903 * @direct: direct flag
2904 * @verb: the verb to send
2905 * @parm: the parameter for the verb
2907 * This function works like snd_hda_codec_write_cache(), but it doesn't send
2908 * command if the parameter is already identical with the cached value.
2909 * If not, it sends the command and refreshes the cache.
2911 * Returns 0 if successful, or a negative error code.
2913 int snd_hda_codec_update_cache(struct hda_codec *codec, hda_nid_t nid,
2914 int direct, unsigned int verb, unsigned int parm)
2916 struct hda_cache_head *c;
2919 /* parm may contain the verb stuff for get/set amp */
2920 verb = verb | (parm >> 8);
2922 key = build_cmd_cache_key(nid, verb);
2923 mutex_lock(&codec->bus->cmd_mutex);
2924 c = get_hash(&codec->cmd_cache, key);
2925 if (c && c->val == parm) {
2926 mutex_unlock(&codec->bus->cmd_mutex);
2929 mutex_unlock(&codec->bus->cmd_mutex);
2930 return snd_hda_codec_write_cache(codec, nid, direct, verb, parm);
2932 EXPORT_SYMBOL_HDA(snd_hda_codec_update_cache);
2935 * snd_hda_codec_resume_cache - Resume the all commands from the cache
2936 * @codec: HD-audio codec
2938 * Execute all verbs recorded in the command caches to resume.
2940 void snd_hda_codec_resume_cache(struct hda_codec *codec)
2942 struct hda_cache_head *buffer = codec->cmd_cache.buf.list;
2945 for (i = 0; i < codec->cmd_cache.buf.used; i++, buffer++) {
2946 u32 key = buffer->key;
2949 snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
2950 get_cmd_cache_cmd(key), buffer->val);
2953 EXPORT_SYMBOL_HDA(snd_hda_codec_resume_cache);
2956 * snd_hda_sequence_write_cache - sequence writes with caching
2957 * @codec: the HDA codec
2958 * @seq: VERB array to send
2960 * Send the commands sequentially from the given array.
2961 * Thte commands are recorded on cache for power-save and resume.
2962 * The array must be terminated with NID=0.
2964 void snd_hda_sequence_write_cache(struct hda_codec *codec,
2965 const struct hda_verb *seq)
2967 for (; seq->nid; seq++)
2968 snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
2971 EXPORT_SYMBOL_HDA(snd_hda_sequence_write_cache);
2972 #endif /* SND_HDA_NEEDS_RESUME */
2975 * set power state of the codec
2977 static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
2978 unsigned int power_state)
2983 /* this delay seems necessary to avoid click noise at power-down */
2984 if (power_state == AC_PWRST_D3)
2986 snd_hda_codec_read(codec, fg, 0, AC_VERB_SET_POWER_STATE,
2988 /* partial workaround for "azx_get_response timeout" */
2989 if (power_state == AC_PWRST_D0 &&
2990 (codec->vendor_id & 0xffff0000) == 0x14f10000)
2993 nid = codec->start_nid;
2994 for (i = 0; i < codec->num_nodes; i++, nid++) {
2995 unsigned int wcaps = get_wcaps(codec, nid);
2996 if (wcaps & AC_WCAP_POWER) {
2997 unsigned int wid_type = get_wcaps_type(wcaps);
2998 if (power_state == AC_PWRST_D3 &&
2999 wid_type == AC_WID_PIN) {
3000 unsigned int pincap;
3002 * don't power down the widget if it controls
3003 * eapd and EAPD_BTLENABLE is set.
3005 pincap = snd_hda_query_pin_caps(codec, nid);
3006 if (pincap & AC_PINCAP_EAPD) {
3007 int eapd = snd_hda_codec_read(codec,
3009 AC_VERB_GET_EAPD_BTLENABLE, 0);
3015 snd_hda_codec_write(codec, nid, 0,
3016 AC_VERB_SET_POWER_STATE,
3021 if (power_state == AC_PWRST_D0) {
3022 unsigned long end_time;
3024 /* wait until the codec reachs to D0 */
3025 end_time = jiffies + msecs_to_jiffies(500);
3027 state = snd_hda_codec_read(codec, fg, 0,
3028 AC_VERB_GET_POWER_STATE, 0);
3029 if (state == power_state)
3032 } while (time_after_eq(end_time, jiffies));
3036 #ifdef CONFIG_SND_HDA_HWDEP
3037 /* execute additional init verbs */
3038 static void hda_exec_init_verbs(struct hda_codec *codec)
3040 if (codec->init_verbs.list)
3041 snd_hda_sequence_write(codec, codec->init_verbs.list);
3044 static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
3047 #ifdef SND_HDA_NEEDS_RESUME
3049 * call suspend and power-down; used both from PM and power-save
3051 static void hda_call_codec_suspend(struct hda_codec *codec)
3053 if (codec->patch_ops.suspend)
3054 codec->patch_ops.suspend(codec, PMSG_SUSPEND);
3055 hda_cleanup_all_streams(codec);
3056 hda_set_power_state(codec,
3057 codec->afg ? codec->afg : codec->mfg,
3059 #ifdef CONFIG_SND_HDA_POWER_SAVE
3060 snd_hda_update_power_acct(codec);
3061 cancel_delayed_work(&codec->power_work);
3062 codec->power_on = 0;
3063 codec->power_transition = 0;
3064 codec->power_jiffies = jiffies;
3069 * kick up codec; used both from PM and power-save
3071 static void hda_call_codec_resume(struct hda_codec *codec)
3073 hda_set_power_state(codec,
3074 codec->afg ? codec->afg : codec->mfg,
3076 restore_pincfgs(codec); /* restore all current pin configs */
3077 restore_shutup_pins(codec);
3078 hda_exec_init_verbs(codec);
3079 if (codec->patch_ops.resume)
3080 codec->patch_ops.resume(codec);
3082 if (codec->patch_ops.init)
3083 codec->patch_ops.init(codec);
3084 snd_hda_codec_resume_amp(codec);
3085 snd_hda_codec_resume_cache(codec);
3088 #endif /* SND_HDA_NEEDS_RESUME */
3092 * snd_hda_build_controls - build mixer controls
3095 * Creates mixer controls for each codec included in the bus.
3097 * Returns 0 if successful, otherwise a negative error code.
3099 int /*__devinit*/ snd_hda_build_controls(struct hda_bus *bus)
3101 struct hda_codec *codec;
3103 list_for_each_entry(codec, &bus->codec_list, list) {
3104 int err = snd_hda_codec_build_controls(codec);
3106 printk(KERN_ERR "hda_codec: cannot build controls "
3107 "for #%d (error %d)\n", codec->addr, err);
3108 err = snd_hda_codec_reset(codec);
3111 "hda_codec: cannot revert codec\n");
3118 EXPORT_SYMBOL_HDA(snd_hda_build_controls);
3120 int snd_hda_codec_build_controls(struct hda_codec *codec)
3123 hda_exec_init_verbs(codec);
3124 /* continue to initialize... */
3125 if (codec->patch_ops.init)
3126 err = codec->patch_ops.init(codec);
3127 if (!err && codec->patch_ops.build_controls)
3128 err = codec->patch_ops.build_controls(codec);
3137 struct hda_rate_tbl {
3139 unsigned int alsa_bits;
3140 unsigned int hda_fmt;
3143 /* rate = base * mult / div */
3144 #define HDA_RATE(base, mult, div) \
3145 (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \
3146 (((div) - 1) << AC_FMT_DIV_SHIFT))
3148 static struct hda_rate_tbl rate_bits[] = {
3149 /* rate in Hz, ALSA rate bitmask, HDA format value */
3151 /* autodetected value used in snd_hda_query_supported_pcm */
3152 { 8000, SNDRV_PCM_RATE_8000, HDA_RATE(48, 1, 6) },
3153 { 11025, SNDRV_PCM_RATE_11025, HDA_RATE(44, 1, 4) },
3154 { 16000, SNDRV_PCM_RATE_16000, HDA_RATE(48, 1, 3) },
3155 { 22050, SNDRV_PCM_RATE_22050, HDA_RATE(44, 1, 2) },
3156 { 32000, SNDRV_PCM_RATE_32000, HDA_RATE(48, 2, 3) },
3157 { 44100, SNDRV_PCM_RATE_44100, HDA_RATE(44, 1, 1) },
3158 { 48000, SNDRV_PCM_RATE_48000, HDA_RATE(48, 1, 1) },
3159 { 88200, SNDRV_PCM_RATE_88200, HDA_RATE(44, 2, 1) },
3160 { 96000, SNDRV_PCM_RATE_96000, HDA_RATE(48, 2, 1) },
3161 { 176400, SNDRV_PCM_RATE_176400, HDA_RATE(44, 4, 1) },
3162 { 192000, SNDRV_PCM_RATE_192000, HDA_RATE(48, 4, 1) },
3163 #define AC_PAR_PCM_RATE_BITS 11
3164 /* up to bits 10, 384kHZ isn't supported properly */
3166 /* not autodetected value */
3167 { 9600, SNDRV_PCM_RATE_KNOT, HDA_RATE(48, 1, 5) },
3169 { 0 } /* terminator */
3173 * snd_hda_calc_stream_format - calculate format bitset
3174 * @rate: the sample rate
3175 * @channels: the number of channels
3176 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
3177 * @maxbps: the max. bps
3179 * Calculate the format bitset from the given rate, channels and th PCM format.
3181 * Return zero if invalid.
3183 unsigned int snd_hda_calc_stream_format(unsigned int rate,
3184 unsigned int channels,
3185 unsigned int format,
3186 unsigned int maxbps,
3187 unsigned short spdif_ctls)
3190 unsigned int val = 0;
3192 for (i = 0; rate_bits[i].hz; i++)
3193 if (rate_bits[i].hz == rate) {
3194 val = rate_bits[i].hda_fmt;
3197 if (!rate_bits[i].hz) {
3198 snd_printdd("invalid rate %d\n", rate);
3202 if (channels == 0 || channels > 8) {
3203 snd_printdd("invalid channels %d\n", channels);
3206 val |= channels - 1;
3208 switch (snd_pcm_format_width(format)) {
3210 val |= AC_FMT_BITS_8;
3213 val |= AC_FMT_BITS_16;
3218 if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE)
3219 val |= AC_FMT_BITS_32;
3220 else if (maxbps >= 24)
3221 val |= AC_FMT_BITS_24;
3223 val |= AC_FMT_BITS_20;
3226 snd_printdd("invalid format width %d\n",
3227 snd_pcm_format_width(format));
3231 if (spdif_ctls & AC_DIG1_NONAUDIO)
3232 val |= AC_FMT_TYPE_NON_PCM;
3236 EXPORT_SYMBOL_HDA(snd_hda_calc_stream_format);
3238 static unsigned int get_pcm_param(struct hda_codec *codec, hda_nid_t nid)
3240 unsigned int val = 0;
3241 if (nid != codec->afg &&
3242 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD))
3243 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
3244 if (!val || val == -1)
3245 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
3246 if (!val || val == -1)
3251 static unsigned int query_pcm_param(struct hda_codec *codec, hda_nid_t nid)
3253 return query_caps_hash(codec, nid, HDA_HASH_PARPCM_KEY(nid),
3257 static unsigned int get_stream_param(struct hda_codec *codec, hda_nid_t nid)
3259 unsigned int streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
3260 if (!streams || streams == -1)
3261 streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
3262 if (!streams || streams == -1)
3267 static unsigned int query_stream_param(struct hda_codec *codec, hda_nid_t nid)
3269 return query_caps_hash(codec, nid, HDA_HASH_PARSTR_KEY(nid),
3274 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
3275 * @codec: the HDA codec
3276 * @nid: NID to query
3277 * @ratesp: the pointer to store the detected rate bitflags
3278 * @formatsp: the pointer to store the detected formats
3279 * @bpsp: the pointer to store the detected format widths
3281 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
3282 * or @bsps argument is ignored.
3284 * Returns 0 if successful, otherwise a negative error code.
3286 static int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
3287 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
3289 unsigned int i, val, wcaps;
3291 wcaps = get_wcaps(codec, nid);
3292 val = query_pcm_param(codec, nid);
3296 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
3298 rates |= rate_bits[i].alsa_bits;
3301 snd_printk(KERN_ERR "hda_codec: rates == 0 "
3302 "(nid=0x%x, val=0x%x, ovrd=%i)\n",
3304 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0);
3310 if (formatsp || bpsp) {
3312 unsigned int streams, bps;
3314 streams = query_stream_param(codec, nid);
3319 if (streams & AC_SUPFMT_PCM) {
3320 if (val & AC_SUPPCM_BITS_8) {
3321 formats |= SNDRV_PCM_FMTBIT_U8;
3324 if (val & AC_SUPPCM_BITS_16) {
3325 formats |= SNDRV_PCM_FMTBIT_S16_LE;
3328 if (wcaps & AC_WCAP_DIGITAL) {
3329 if (val & AC_SUPPCM_BITS_32)
3330 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
3331 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
3332 formats |= SNDRV_PCM_FMTBIT_S32_LE;
3333 if (val & AC_SUPPCM_BITS_24)
3335 else if (val & AC_SUPPCM_BITS_20)
3337 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
3338 AC_SUPPCM_BITS_32)) {
3339 formats |= SNDRV_PCM_FMTBIT_S32_LE;
3340 if (val & AC_SUPPCM_BITS_32)
3342 else if (val & AC_SUPPCM_BITS_24)
3344 else if (val & AC_SUPPCM_BITS_20)
3348 if (streams & AC_SUPFMT_FLOAT32) {
3349 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
3353 if (streams == AC_SUPFMT_AC3) {
3354 /* should be exclusive */
3355 /* temporary hack: we have still no proper support
3356 * for the direct AC3 stream...
3358 formats |= SNDRV_PCM_FMTBIT_U8;
3362 snd_printk(KERN_ERR "hda_codec: formats == 0 "
3363 "(nid=0x%x, val=0x%x, ovrd=%i, "
3366 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0,
3371 *formatsp = formats;
3380 * snd_hda_is_supported_format - Check the validity of the format
3381 * @codec: HD-audio codec
3382 * @nid: NID to check
3383 * @format: the HD-audio format value to check
3385 * Check whether the given node supports the format value.
3387 * Returns 1 if supported, 0 if not.
3389 int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
3390 unsigned int format)
3393 unsigned int val = 0, rate, stream;
3395 val = query_pcm_param(codec, nid);
3399 rate = format & 0xff00;
3400 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
3401 if (rate_bits[i].hda_fmt == rate) {
3406 if (i >= AC_PAR_PCM_RATE_BITS)
3409 stream = query_stream_param(codec, nid);
3413 if (stream & AC_SUPFMT_PCM) {
3414 switch (format & 0xf0) {
3416 if (!(val & AC_SUPPCM_BITS_8))
3420 if (!(val & AC_SUPPCM_BITS_16))
3424 if (!(val & AC_SUPPCM_BITS_20))
3428 if (!(val & AC_SUPPCM_BITS_24))
3432 if (!(val & AC_SUPPCM_BITS_32))
3439 /* FIXME: check for float32 and AC3? */
3444 EXPORT_SYMBOL_HDA(snd_hda_is_supported_format);
3449 static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
3450 struct hda_codec *codec,
3451 struct snd_pcm_substream *substream)
3456 static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
3457 struct hda_codec *codec,
3458 unsigned int stream_tag,
3459 unsigned int format,
3460 struct snd_pcm_substream *substream)
3462 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
3466 static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
3467 struct hda_codec *codec,
3468 struct snd_pcm_substream *substream)
3470 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
3474 static int set_pcm_default_values(struct hda_codec *codec,
3475 struct hda_pcm_stream *info)
3479 /* query support PCM information from the given NID */
3480 if (info->nid && (!info->rates || !info->formats)) {
3481 err = snd_hda_query_supported_pcm(codec, info->nid,
3482 info->rates ? NULL : &info->rates,
3483 info->formats ? NULL : &info->formats,
3484 info->maxbps ? NULL : &info->maxbps);
3488 if (info->ops.open == NULL)
3489 info->ops.open = hda_pcm_default_open_close;
3490 if (info->ops.close == NULL)
3491 info->ops.close = hda_pcm_default_open_close;
3492 if (info->ops.prepare == NULL) {
3493 if (snd_BUG_ON(!info->nid))
3495 info->ops.prepare = hda_pcm_default_prepare;
3497 if (info->ops.cleanup == NULL) {
3498 if (snd_BUG_ON(!info->nid))
3500 info->ops.cleanup = hda_pcm_default_cleanup;
3506 * codec prepare/cleanup entries
3508 int snd_hda_codec_prepare(struct hda_codec *codec,
3509 struct hda_pcm_stream *hinfo,
3510 unsigned int stream,
3511 unsigned int format,
3512 struct snd_pcm_substream *substream)
3515 mutex_lock(&codec->bus->prepare_mutex);
3516 ret = hinfo->ops.prepare(hinfo, codec, stream, format, substream);
3518 purify_inactive_streams(codec);
3519 mutex_unlock(&codec->bus->prepare_mutex);
3522 EXPORT_SYMBOL_HDA(snd_hda_codec_prepare);
3524 void snd_hda_codec_cleanup(struct hda_codec *codec,
3525 struct hda_pcm_stream *hinfo,
3526 struct snd_pcm_substream *substream)
3528 mutex_lock(&codec->bus->prepare_mutex);
3529 hinfo->ops.cleanup(hinfo, codec, substream);
3530 mutex_unlock(&codec->bus->prepare_mutex);
3532 EXPORT_SYMBOL_HDA(snd_hda_codec_cleanup);
3535 const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = {
3536 "Audio", "SPDIF", "HDMI", "Modem"
3540 * get the empty PCM device number to assign
3542 * note the max device number is limited by HDA_MAX_PCMS, currently 10
3544 static int get_empty_pcm_device(struct hda_bus *bus, int type)
3546 /* audio device indices; not linear to keep compatibility */
3547 static int audio_idx[HDA_PCM_NTYPES][5] = {
3548 [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 },
3549 [HDA_PCM_TYPE_SPDIF] = { 1, -1 },
3550 [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 },
3551 [HDA_PCM_TYPE_MODEM] = { 6, -1 },
3555 if (type >= HDA_PCM_NTYPES) {
3556 snd_printk(KERN_WARNING "Invalid PCM type %d\n", type);
3560 for (i = 0; audio_idx[type][i] >= 0 ; i++)
3561 if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits))
3562 return audio_idx[type][i];
3564 snd_printk(KERN_WARNING "Too many %s devices\n",
3565 snd_hda_pcm_type_name[type]);
3570 * attach a new PCM stream
3572 static int snd_hda_attach_pcm(struct hda_codec *codec, struct hda_pcm *pcm)
3574 struct hda_bus *bus = codec->bus;
3575 struct hda_pcm_stream *info;
3578 if (snd_BUG_ON(!pcm->name))
3580 for (stream = 0; stream < 2; stream++) {
3581 info = &pcm->stream[stream];
3582 if (info->substreams) {
3583 err = set_pcm_default_values(codec, info);
3588 return bus->ops.attach_pcm(bus, codec, pcm);
3591 /* assign all PCMs of the given codec */
3592 int snd_hda_codec_build_pcms(struct hda_codec *codec)
3597 if (!codec->num_pcms) {
3598 if (!codec->patch_ops.build_pcms)
3600 err = codec->patch_ops.build_pcms(codec);
3602 printk(KERN_ERR "hda_codec: cannot build PCMs"
3603 "for #%d (error %d)\n", codec->addr, err);
3604 err = snd_hda_codec_reset(codec);
3607 "hda_codec: cannot revert codec\n");
3612 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
3613 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
3616 if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
3617 continue; /* no substreams assigned */
3620 dev = get_empty_pcm_device(codec->bus, cpcm->pcm_type);
3622 continue; /* no fatal error */
3624 err = snd_hda_attach_pcm(codec, cpcm);
3626 printk(KERN_ERR "hda_codec: cannot attach "
3627 "PCM stream %d for codec #%d\n",
3629 continue; /* no fatal error */
3637 * snd_hda_build_pcms - build PCM information
3640 * Create PCM information for each codec included in the bus.
3642 * The build_pcms codec patch is requested to set up codec->num_pcms and
3643 * codec->pcm_info properly. The array is referred by the top-level driver
3644 * to create its PCM instances.
3645 * The allocated codec->pcm_info should be released in codec->patch_ops.free
3648 * At least, substreams, channels_min and channels_max must be filled for
3649 * each stream. substreams = 0 indicates that the stream doesn't exist.
3650 * When rates and/or formats are zero, the supported values are queried
3651 * from the given nid. The nid is used also by the default ops.prepare
3652 * and ops.cleanup callbacks.
3654 * The driver needs to call ops.open in its open callback. Similarly,
3655 * ops.close is supposed to be called in the close callback.
3656 * ops.prepare should be called in the prepare or hw_params callback
3657 * with the proper parameters for set up.
3658 * ops.cleanup should be called in hw_free for clean up of streams.
3660 * This function returns 0 if successfull, or a negative error code.
3662 int __devinit snd_hda_build_pcms(struct hda_bus *bus)
3664 struct hda_codec *codec;
3666 list_for_each_entry(codec, &bus->codec_list, list) {
3667 int err = snd_hda_codec_build_pcms(codec);
3673 EXPORT_SYMBOL_HDA(snd_hda_build_pcms);
3676 * snd_hda_check_board_config - compare the current codec with the config table
3677 * @codec: the HDA codec
3678 * @num_configs: number of config enums
3679 * @models: array of model name strings
3680 * @tbl: configuration table, terminated by null entries
3682 * Compares the modelname or PCI subsystem id of the current codec with the
3683 * given configuration table. If a matching entry is found, returns its
3684 * config value (supposed to be 0 or positive).
3686 * If no entries are matching, the function returns a negative value.
3688 int snd_hda_check_board_config(struct hda_codec *codec,
3689 int num_configs, const char **models,
3690 const struct snd_pci_quirk *tbl)
3692 if (codec->modelname && models) {
3694 for (i = 0; i < num_configs; i++) {
3696 !strcmp(codec->modelname, models[i])) {
3697 snd_printd(KERN_INFO "hda_codec: model '%s' is "
3698 "selected\n", models[i]);
3704 if (!codec->bus->pci || !tbl)
3707 tbl = snd_pci_quirk_lookup(codec->bus->pci, tbl);
3710 if (tbl->value >= 0 && tbl->value < num_configs) {
3711 #ifdef CONFIG_SND_DEBUG_VERBOSE
3713 const char *model = NULL;
3715 model = models[tbl->value];
3717 sprintf(tmp, "#%d", tbl->value);
3720 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
3721 "for config %x:%x (%s)\n",
3722 model, tbl->subvendor, tbl->subdevice,
3723 (tbl->name ? tbl->name : "Unknown device"));
3729 EXPORT_SYMBOL_HDA(snd_hda_check_board_config);
3732 * snd_hda_check_board_codec_sid_config - compare the current codec
3733 subsystem ID with the
3736 This is important for Gateway notebooks with SB450 HDA Audio
3737 where the vendor ID of the PCI device is:
3738 ATI Technologies Inc SB450 HDA Audio [1002:437b]
3739 and the vendor/subvendor are found only at the codec.
3741 * @codec: the HDA codec
3742 * @num_configs: number of config enums
3743 * @models: array of model name strings
3744 * @tbl: configuration table, terminated by null entries
3746 * Compares the modelname or PCI subsystem id of the current codec with the
3747 * given configuration table. If a matching entry is found, returns its
3748 * config value (supposed to be 0 or positive).
3750 * If no entries are matching, the function returns a negative value.
3752 int snd_hda_check_board_codec_sid_config(struct hda_codec *codec,
3753 int num_configs, const char **models,
3754 const struct snd_pci_quirk *tbl)
3756 const struct snd_pci_quirk *q;
3758 /* Search for codec ID */
3759 for (q = tbl; q->subvendor; q++) {
3760 unsigned long vendorid = (q->subdevice) | (q->subvendor << 16);
3762 if (vendorid == codec->subsystem_id)
3771 if (tbl->value >= 0 && tbl->value < num_configs) {
3772 #ifdef CONFIG_SND_DEBUG_VERBOSE
3774 const char *model = NULL;
3776 model = models[tbl->value];
3778 sprintf(tmp, "#%d", tbl->value);
3781 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
3782 "for config %x:%x (%s)\n",
3783 model, tbl->subvendor, tbl->subdevice,
3784 (tbl->name ? tbl->name : "Unknown device"));
3790 EXPORT_SYMBOL_HDA(snd_hda_check_board_codec_sid_config);
3793 * snd_hda_add_new_ctls - create controls from the array
3794 * @codec: the HDA codec
3795 * @knew: the array of struct snd_kcontrol_new
3797 * This helper function creates and add new controls in the given array.
3798 * The array must be terminated with an empty entry as terminator.
3800 * Returns 0 if successful, or a negative error code.
3802 int snd_hda_add_new_ctls(struct hda_codec *codec, struct snd_kcontrol_new *knew)
3806 for (; knew->name; knew++) {
3807 struct snd_kcontrol *kctl;
3808 if (knew->iface == -1) /* skip this codec private value */
3810 kctl = snd_ctl_new1(knew, codec);
3813 err = snd_hda_ctl_add(codec, 0, kctl);
3817 kctl = snd_ctl_new1(knew, codec);
3820 kctl->id.device = codec->addr;
3821 err = snd_hda_ctl_add(codec, 0, kctl);
3828 EXPORT_SYMBOL_HDA(snd_hda_add_new_ctls);
3830 #ifdef CONFIG_SND_HDA_POWER_SAVE
3831 static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
3832 unsigned int power_state);
3834 static void hda_power_work(struct work_struct *work)
3836 struct hda_codec *codec =
3837 container_of(work, struct hda_codec, power_work.work);
3838 struct hda_bus *bus = codec->bus;
3840 if (!codec->power_on || codec->power_count) {
3841 codec->power_transition = 0;
3845 hda_call_codec_suspend(codec);
3846 if (bus->ops.pm_notify)
3847 bus->ops.pm_notify(bus);
3850 static void hda_keep_power_on(struct hda_codec *codec)
3852 codec->power_count++;
3853 codec->power_on = 1;
3854 codec->power_jiffies = jiffies;
3857 /* update the power on/off account with the current jiffies */
3858 void snd_hda_update_power_acct(struct hda_codec *codec)
3860 unsigned long delta = jiffies - codec->power_jiffies;
3861 if (codec->power_on)
3862 codec->power_on_acct += delta;
3864 codec->power_off_acct += delta;
3865 codec->power_jiffies += delta;
3869 * snd_hda_power_up - Power-up the codec
3870 * @codec: HD-audio codec
3872 * Increment the power-up counter and power up the hardware really when
3873 * not turned on yet.
3875 void snd_hda_power_up(struct hda_codec *codec)
3877 struct hda_bus *bus = codec->bus;
3879 codec->power_count++;
3880 if (codec->power_on || codec->power_transition)
3883 snd_hda_update_power_acct(codec);
3884 codec->power_on = 1;
3885 codec->power_jiffies = jiffies;
3886 if (bus->ops.pm_notify)
3887 bus->ops.pm_notify(bus);
3888 hda_call_codec_resume(codec);
3889 cancel_delayed_work(&codec->power_work);
3890 codec->power_transition = 0;
3892 EXPORT_SYMBOL_HDA(snd_hda_power_up);
3894 #define power_save(codec) \
3895 ((codec)->bus->power_save ? *(codec)->bus->power_save : 0)
3898 * snd_hda_power_down - Power-down the codec
3899 * @codec: HD-audio codec
3901 * Decrement the power-up counter and schedules the power-off work if
3902 * the counter rearches to zero.
3904 void snd_hda_power_down(struct hda_codec *codec)
3906 --codec->power_count;
3907 if (!codec->power_on || codec->power_count || codec->power_transition)
3909 if (power_save(codec)) {
3910 codec->power_transition = 1; /* avoid reentrance */
3911 queue_delayed_work(codec->bus->workq, &codec->power_work,
3912 msecs_to_jiffies(power_save(codec) * 1000));
3915 EXPORT_SYMBOL_HDA(snd_hda_power_down);
3918 * snd_hda_check_amp_list_power - Check the amp list and update the power
3919 * @codec: HD-audio codec
3920 * @check: the object containing an AMP list and the status
3921 * @nid: NID to check / update
3923 * Check whether the given NID is in the amp list. If it's in the list,
3924 * check the current AMP status, and update the the power-status according
3925 * to the mute status.
3927 * This function is supposed to be set or called from the check_power_status
3930 int snd_hda_check_amp_list_power(struct hda_codec *codec,
3931 struct hda_loopback_check *check,
3934 struct hda_amp_list *p;
3937 if (!check->amplist)
3939 for (p = check->amplist; p->nid; p++) {
3944 return 0; /* nothing changed */
3946 for (p = check->amplist; p->nid; p++) {
3947 for (ch = 0; ch < 2; ch++) {
3948 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
3950 if (!(v & HDA_AMP_MUTE) && v > 0) {
3951 if (!check->power_on) {
3952 check->power_on = 1;
3953 snd_hda_power_up(codec);
3959 if (check->power_on) {
3960 check->power_on = 0;
3961 snd_hda_power_down(codec);
3965 EXPORT_SYMBOL_HDA(snd_hda_check_amp_list_power);
3969 * Channel mode helper
3973 * snd_hda_ch_mode_info - Info callback helper for the channel mode enum
3975 int snd_hda_ch_mode_info(struct hda_codec *codec,
3976 struct snd_ctl_elem_info *uinfo,
3977 const struct hda_channel_mode *chmode,
3980 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
3982 uinfo->value.enumerated.items = num_chmodes;
3983 if (uinfo->value.enumerated.item >= num_chmodes)
3984 uinfo->value.enumerated.item = num_chmodes - 1;
3985 sprintf(uinfo->value.enumerated.name, "%dch",
3986 chmode[uinfo->value.enumerated.item].channels);
3989 EXPORT_SYMBOL_HDA(snd_hda_ch_mode_info);
3992 * snd_hda_ch_mode_get - Get callback helper for the channel mode enum
3994 int snd_hda_ch_mode_get(struct hda_codec *codec,
3995 struct snd_ctl_elem_value *ucontrol,
3996 const struct hda_channel_mode *chmode,
4002 for (i = 0; i < num_chmodes; i++) {
4003 if (max_channels == chmode[i].channels) {
4004 ucontrol->value.enumerated.item[0] = i;
4010 EXPORT_SYMBOL_HDA(snd_hda_ch_mode_get);
4013 * snd_hda_ch_mode_put - Put callback helper for the channel mode enum
4015 int snd_hda_ch_mode_put(struct hda_codec *codec,
4016 struct snd_ctl_elem_value *ucontrol,
4017 const struct hda_channel_mode *chmode,
4023 mode = ucontrol->value.enumerated.item[0];
4024 if (mode >= num_chmodes)
4026 if (*max_channelsp == chmode[mode].channels)
4028 /* change the current channel setting */
4029 *max_channelsp = chmode[mode].channels;
4030 if (chmode[mode].sequence)
4031 snd_hda_sequence_write_cache(codec, chmode[mode].sequence);
4034 EXPORT_SYMBOL_HDA(snd_hda_ch_mode_put);
4041 * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum
4043 int snd_hda_input_mux_info(const struct hda_input_mux *imux,
4044 struct snd_ctl_elem_info *uinfo)
4048 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4050 uinfo->value.enumerated.items = imux->num_items;
4051 if (!imux->num_items)
4053 index = uinfo->value.enumerated.item;
4054 if (index >= imux->num_items)
4055 index = imux->num_items - 1;
4056 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
4059 EXPORT_SYMBOL_HDA(snd_hda_input_mux_info);
4062 * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum
4064 int snd_hda_input_mux_put(struct hda_codec *codec,
4065 const struct hda_input_mux *imux,
4066 struct snd_ctl_elem_value *ucontrol,
4068 unsigned int *cur_val)
4072 if (!imux->num_items)
4074 idx = ucontrol->value.enumerated.item[0];
4075 if (idx >= imux->num_items)
4076 idx = imux->num_items - 1;
4077 if (*cur_val == idx)
4079 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
4080 imux->items[idx].index);
4084 EXPORT_SYMBOL_HDA(snd_hda_input_mux_put);
4088 * Multi-channel / digital-out PCM helper functions
4091 /* setup SPDIF output stream */
4092 static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
4093 unsigned int stream_tag, unsigned int format)
4095 /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */
4096 if (codec->spdif_status_reset && (codec->spdif_ctls & AC_DIG1_ENABLE))
4097 set_dig_out_convert(codec, nid,
4098 codec->spdif_ctls & ~AC_DIG1_ENABLE & 0xff,
4100 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
4101 if (codec->slave_dig_outs) {
4103 for (d = codec->slave_dig_outs; *d; d++)
4104 snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
4107 /* turn on again (if needed) */
4108 if (codec->spdif_status_reset && (codec->spdif_ctls & AC_DIG1_ENABLE))
4109 set_dig_out_convert(codec, nid,
4110 codec->spdif_ctls & 0xff, -1);
4113 static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
4115 snd_hda_codec_cleanup_stream(codec, nid);
4116 if (codec->slave_dig_outs) {
4118 for (d = codec->slave_dig_outs; *d; d++)
4119 snd_hda_codec_cleanup_stream(codec, *d);
4124 * snd_hda_bus_reboot_notify - call the reboot notifier of each codec
4125 * @bus: HD-audio bus
4127 void snd_hda_bus_reboot_notify(struct hda_bus *bus)
4129 struct hda_codec *codec;
4133 list_for_each_entry(codec, &bus->codec_list, list) {
4134 #ifdef CONFIG_SND_HDA_POWER_SAVE
4135 if (!codec->power_on)
4138 if (codec->patch_ops.reboot_notify)
4139 codec->patch_ops.reboot_notify(codec);
4142 EXPORT_SYMBOL_HDA(snd_hda_bus_reboot_notify);
4145 * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode
4147 int snd_hda_multi_out_dig_open(struct hda_codec *codec,
4148 struct hda_multi_out *mout)
4150 mutex_lock(&codec->spdif_mutex);
4151 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
4152 /* already opened as analog dup; reset it once */
4153 cleanup_dig_out_stream(codec, mout->dig_out_nid);
4154 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
4155 mutex_unlock(&codec->spdif_mutex);
4158 EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_open);
4161 * snd_hda_multi_out_dig_prepare - prepare the digital out stream
4163 int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
4164 struct hda_multi_out *mout,
4165 unsigned int stream_tag,
4166 unsigned int format,
4167 struct snd_pcm_substream *substream)
4169 mutex_lock(&codec->spdif_mutex);
4170 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
4171 mutex_unlock(&codec->spdif_mutex);
4174 EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_prepare);
4177 * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream
4179 int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec,
4180 struct hda_multi_out *mout)
4182 mutex_lock(&codec->spdif_mutex);
4183 cleanup_dig_out_stream(codec, mout->dig_out_nid);
4184 mutex_unlock(&codec->spdif_mutex);
4187 EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_cleanup);
4190 * snd_hda_multi_out_dig_close - release the digital out stream
4192 int snd_hda_multi_out_dig_close(struct hda_codec *codec,
4193 struct hda_multi_out *mout)
4195 mutex_lock(&codec->spdif_mutex);
4196 mout->dig_out_used = 0;
4197 mutex_unlock(&codec->spdif_mutex);
4200 EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_close);
4203 * snd_hda_multi_out_analog_open - open analog outputs
4205 * Open analog outputs and set up the hw-constraints.
4206 * If the digital outputs can be opened as slave, open the digital
4209 int snd_hda_multi_out_analog_open(struct hda_codec *codec,
4210 struct hda_multi_out *mout,
4211 struct snd_pcm_substream *substream,
4212 struct hda_pcm_stream *hinfo)
4214 struct snd_pcm_runtime *runtime = substream->runtime;
4215 runtime->hw.channels_max = mout->max_channels;
4216 if (mout->dig_out_nid) {
4217 if (!mout->analog_rates) {
4218 mout->analog_rates = hinfo->rates;
4219 mout->analog_formats = hinfo->formats;
4220 mout->analog_maxbps = hinfo->maxbps;
4222 runtime->hw.rates = mout->analog_rates;
4223 runtime->hw.formats = mout->analog_formats;
4224 hinfo->maxbps = mout->analog_maxbps;
4226 if (!mout->spdif_rates) {
4227 snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
4229 &mout->spdif_formats,
4230 &mout->spdif_maxbps);
4232 mutex_lock(&codec->spdif_mutex);
4233 if (mout->share_spdif) {
4234 if ((runtime->hw.rates & mout->spdif_rates) &&
4235 (runtime->hw.formats & mout->spdif_formats)) {
4236 runtime->hw.rates &= mout->spdif_rates;
4237 runtime->hw.formats &= mout->spdif_formats;
4238 if (mout->spdif_maxbps < hinfo->maxbps)
4239 hinfo->maxbps = mout->spdif_maxbps;
4241 mout->share_spdif = 0;
4242 /* FIXME: need notify? */
4245 mutex_unlock(&codec->spdif_mutex);
4247 return snd_pcm_hw_constraint_step(substream->runtime, 0,
4248 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
4250 EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_open);
4253 * snd_hda_multi_out_analog_prepare - Preapre the analog outputs.
4255 * Set up the i/o for analog out.
4256 * When the digital out is available, copy the front out to digital out, too.
4258 int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
4259 struct hda_multi_out *mout,
4260 unsigned int stream_tag,
4261 unsigned int format,
4262 struct snd_pcm_substream *substream)
4264 hda_nid_t *nids = mout->dac_nids;
4265 int chs = substream->runtime->channels;
4268 mutex_lock(&codec->spdif_mutex);
4269 if (mout->dig_out_nid && mout->share_spdif &&
4270 mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
4272 snd_hda_is_supported_format(codec, mout->dig_out_nid,
4274 !(codec->spdif_status & IEC958_AES0_NONAUDIO)) {
4275 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
4276 setup_dig_out_stream(codec, mout->dig_out_nid,
4277 stream_tag, format);
4279 mout->dig_out_used = 0;
4280 cleanup_dig_out_stream(codec, mout->dig_out_nid);
4283 mutex_unlock(&codec->spdif_mutex);
4286 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
4288 if (!mout->no_share_stream &&
4289 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
4290 /* headphone out will just decode front left/right (stereo) */
4291 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
4293 /* extra outputs copied from front */
4294 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
4295 if (!mout->no_share_stream && mout->extra_out_nid[i])
4296 snd_hda_codec_setup_stream(codec,
4297 mout->extra_out_nid[i],
4298 stream_tag, 0, format);
4301 for (i = 1; i < mout->num_dacs; i++) {
4302 if (chs >= (i + 1) * 2) /* independent out */
4303 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
4305 else if (!mout->no_share_stream) /* copy front */
4306 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
4311 EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_prepare);
4314 * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out
4316 int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
4317 struct hda_multi_out *mout)
4319 hda_nid_t *nids = mout->dac_nids;
4322 for (i = 0; i < mout->num_dacs; i++)
4323 snd_hda_codec_cleanup_stream(codec, nids[i]);
4325 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
4326 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
4327 if (mout->extra_out_nid[i])
4328 snd_hda_codec_cleanup_stream(codec,
4329 mout->extra_out_nid[i]);
4330 mutex_lock(&codec->spdif_mutex);
4331 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
4332 cleanup_dig_out_stream(codec, mout->dig_out_nid);
4333 mout->dig_out_used = 0;
4335 mutex_unlock(&codec->spdif_mutex);
4338 EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_cleanup);
4341 * Helper for automatic pin configuration
4344 static int is_in_nid_list(hda_nid_t nid, hda_nid_t *list)
4346 for (; *list; list++)
4354 * Sort an associated group of pins according to their sequence numbers.
4356 static void sort_pins_by_sequence(hda_nid_t *pins, short *sequences,
4363 for (i = 0; i < num_pins; i++) {
4364 for (j = i + 1; j < num_pins; j++) {
4365 if (sequences[i] > sequences[j]) {
4367 sequences[i] = sequences[j];
4378 /* add the found input-pin to the cfg->inputs[] table */
4379 static void add_auto_cfg_input_pin(struct auto_pin_cfg *cfg, hda_nid_t nid,
4382 if (cfg->num_inputs < AUTO_CFG_MAX_INS) {
4383 cfg->inputs[cfg->num_inputs].pin = nid;
4384 cfg->inputs[cfg->num_inputs].type = type;
4389 /* sort inputs in the order of AUTO_PIN_* type */
4390 static void sort_autocfg_input_pins(struct auto_pin_cfg *cfg)
4394 for (i = 0; i < cfg->num_inputs; i++) {
4395 for (j = i + 1; j < cfg->num_inputs; j++) {
4396 if (cfg->inputs[i].type > cfg->inputs[j].type) {
4397 struct auto_pin_cfg_item tmp;
4398 tmp = cfg->inputs[i];
4399 cfg->inputs[i] = cfg->inputs[j];
4400 cfg->inputs[j] = tmp;
4407 * Parse all pin widgets and store the useful pin nids to cfg
4409 * The number of line-outs or any primary output is stored in line_outs,
4410 * and the corresponding output pins are assigned to line_out_pins[],
4411 * in the order of front, rear, CLFE, side, ...
4413 * If more extra outputs (speaker and headphone) are found, the pins are
4414 * assisnged to hp_pins[] and speaker_pins[], respectively. If no line-out jack
4415 * is detected, one of speaker of HP pins is assigned as the primary
4416 * output, i.e. to line_out_pins[0]. So, line_outs is always positive
4417 * if any analog output exists.
4419 * The analog input pins are assigned to inputs array.
4420 * The digital input/output pins are assigned to dig_in_pin and dig_out_pin,
4423 int snd_hda_parse_pin_def_config(struct hda_codec *codec,
4424 struct auto_pin_cfg *cfg,
4425 hda_nid_t *ignore_nids)
4427 hda_nid_t nid, end_nid;
4428 short seq, assoc_line_out, assoc_speaker;
4429 short sequences_line_out[ARRAY_SIZE(cfg->line_out_pins)];
4430 short sequences_speaker[ARRAY_SIZE(cfg->speaker_pins)];
4431 short sequences_hp[ARRAY_SIZE(cfg->hp_pins)];
4434 memset(cfg, 0, sizeof(*cfg));
4436 memset(sequences_line_out, 0, sizeof(sequences_line_out));
4437 memset(sequences_speaker, 0, sizeof(sequences_speaker));
4438 memset(sequences_hp, 0, sizeof(sequences_hp));
4439 assoc_line_out = assoc_speaker = 0;
4441 end_nid = codec->start_nid + codec->num_nodes;
4442 for (nid = codec->start_nid; nid < end_nid; nid++) {
4443 unsigned int wid_caps = get_wcaps(codec, nid);
4444 unsigned int wid_type = get_wcaps_type(wid_caps);
4445 unsigned int def_conf;
4448 /* read all default configuration for pin complex */
4449 if (wid_type != AC_WID_PIN)
4451 /* ignore the given nids (e.g. pc-beep returns error) */
4452 if (ignore_nids && is_in_nid_list(nid, ignore_nids))
4455 def_conf = snd_hda_codec_get_pincfg(codec, nid);
4456 if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE)
4458 loc = get_defcfg_location(def_conf);
4459 switch (get_defcfg_device(def_conf)) {
4460 case AC_JACK_LINE_OUT:
4461 seq = get_defcfg_sequence(def_conf);
4462 assoc = get_defcfg_association(def_conf);
4464 if (!(wid_caps & AC_WCAP_STEREO))
4465 if (!cfg->mono_out_pin)
4466 cfg->mono_out_pin = nid;
4469 if (!assoc_line_out)
4470 assoc_line_out = assoc;
4471 else if (assoc_line_out != assoc)
4473 if (cfg->line_outs >= ARRAY_SIZE(cfg->line_out_pins))
4475 cfg->line_out_pins[cfg->line_outs] = nid;
4476 sequences_line_out[cfg->line_outs] = seq;
4479 case AC_JACK_SPEAKER:
4480 seq = get_defcfg_sequence(def_conf);
4481 assoc = get_defcfg_association(def_conf);
4485 assoc_speaker = assoc;
4486 else if (assoc_speaker != assoc)
4488 if (cfg->speaker_outs >= ARRAY_SIZE(cfg->speaker_pins))
4490 cfg->speaker_pins[cfg->speaker_outs] = nid;
4491 sequences_speaker[cfg->speaker_outs] = seq;
4492 cfg->speaker_outs++;
4494 case AC_JACK_HP_OUT:
4495 seq = get_defcfg_sequence(def_conf);
4496 assoc = get_defcfg_association(def_conf);
4497 if (cfg->hp_outs >= ARRAY_SIZE(cfg->hp_pins))
4499 cfg->hp_pins[cfg->hp_outs] = nid;
4500 sequences_hp[cfg->hp_outs] = (assoc << 4) | seq;
4503 case AC_JACK_MIC_IN:
4504 add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_MIC);
4506 case AC_JACK_LINE_IN:
4507 add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_LINE_IN);
4510 add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_CD);
4513 add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_AUX);
4515 case AC_JACK_SPDIF_OUT:
4516 case AC_JACK_DIG_OTHER_OUT:
4517 if (cfg->dig_outs >= ARRAY_SIZE(cfg->dig_out_pins))
4519 cfg->dig_out_pins[cfg->dig_outs] = nid;
4520 cfg->dig_out_type[cfg->dig_outs] =
4521 (loc == AC_JACK_LOC_HDMI) ?
4522 HDA_PCM_TYPE_HDMI : HDA_PCM_TYPE_SPDIF;
4525 case AC_JACK_SPDIF_IN:
4526 case AC_JACK_DIG_OTHER_IN:
4527 cfg->dig_in_pin = nid;
4528 if (loc == AC_JACK_LOC_HDMI)
4529 cfg->dig_in_type = HDA_PCM_TYPE_HDMI;
4531 cfg->dig_in_type = HDA_PCM_TYPE_SPDIF;
4537 * If no line-out is defined but multiple HPs are found,
4538 * some of them might be the real line-outs.
4540 if (!cfg->line_outs && cfg->hp_outs > 1) {
4542 while (i < cfg->hp_outs) {
4543 /* The real HPs should have the sequence 0x0f */
4544 if ((sequences_hp[i] & 0x0f) == 0x0f) {
4548 /* Move it to the line-out table */
4549 cfg->line_out_pins[cfg->line_outs] = cfg->hp_pins[i];
4550 sequences_line_out[cfg->line_outs] = sequences_hp[i];
4553 memmove(cfg->hp_pins + i, cfg->hp_pins + i + 1,
4554 sizeof(cfg->hp_pins[0]) * (cfg->hp_outs - i));
4555 memmove(sequences_hp + i, sequences_hp + i + 1,
4556 sizeof(sequences_hp[0]) * (cfg->hp_outs - i));
4558 memset(cfg->hp_pins + cfg->hp_outs, 0,
4559 sizeof(hda_nid_t) * (AUTO_CFG_MAX_OUTS - cfg->hp_outs));
4562 /* sort by sequence */
4563 sort_pins_by_sequence(cfg->line_out_pins, sequences_line_out,
4565 sort_pins_by_sequence(cfg->speaker_pins, sequences_speaker,
4567 sort_pins_by_sequence(cfg->hp_pins, sequences_hp,
4571 * FIX-UP: if no line-outs are detected, try to use speaker or HP pin
4572 * as a primary output
4574 if (!cfg->line_outs) {
4575 if (cfg->speaker_outs) {
4576 cfg->line_outs = cfg->speaker_outs;
4577 memcpy(cfg->line_out_pins, cfg->speaker_pins,
4578 sizeof(cfg->speaker_pins));
4579 cfg->speaker_outs = 0;
4580 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
4581 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
4582 } else if (cfg->hp_outs) {
4583 cfg->line_outs = cfg->hp_outs;
4584 memcpy(cfg->line_out_pins, cfg->hp_pins,
4585 sizeof(cfg->hp_pins));
4587 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
4588 cfg->line_out_type = AUTO_PIN_HP_OUT;
4592 /* Reorder the surround channels
4593 * ALSA sequence is front/surr/clfe/side
4595 * 4-ch: front/surr => OK as it is
4596 * 6-ch: front/clfe/surr
4597 * 8-ch: front/clfe/rear/side|fc
4599 switch (cfg->line_outs) {
4602 nid = cfg->line_out_pins[1];
4603 cfg->line_out_pins[1] = cfg->line_out_pins[2];
4604 cfg->line_out_pins[2] = nid;
4608 sort_autocfg_input_pins(cfg);
4611 * debug prints of the parsed results
4613 snd_printd("autoconfig: line_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
4614 cfg->line_outs, cfg->line_out_pins[0], cfg->line_out_pins[1],
4615 cfg->line_out_pins[2], cfg->line_out_pins[3],
4616 cfg->line_out_pins[4]);
4617 snd_printd(" speaker_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
4618 cfg->speaker_outs, cfg->speaker_pins[0],
4619 cfg->speaker_pins[1], cfg->speaker_pins[2],
4620 cfg->speaker_pins[3], cfg->speaker_pins[4]);
4621 snd_printd(" hp_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
4622 cfg->hp_outs, cfg->hp_pins[0],
4623 cfg->hp_pins[1], cfg->hp_pins[2],
4624 cfg->hp_pins[3], cfg->hp_pins[4]);
4625 snd_printd(" mono: mono_out=0x%x\n", cfg->mono_out_pin);
4627 snd_printd(" dig-out=0x%x/0x%x\n",
4628 cfg->dig_out_pins[0], cfg->dig_out_pins[1]);
4629 snd_printd(" inputs:");
4630 for (i = 0; i < cfg->num_inputs; i++) {
4631 snd_printdd(" %s=0x%x",
4632 hda_get_autocfg_input_label(codec, cfg, i),
4633 cfg->inputs[i].pin);
4636 if (cfg->dig_in_pin)
4637 snd_printd(" dig-in=0x%x\n", cfg->dig_in_pin);
4641 EXPORT_SYMBOL_HDA(snd_hda_parse_pin_def_config);
4643 int snd_hda_get_input_pin_attr(unsigned int def_conf)
4645 unsigned int loc = get_defcfg_location(def_conf);
4646 unsigned int conn = get_defcfg_connect(def_conf);
4647 if (conn == AC_JACK_PORT_NONE)
4648 return INPUT_PIN_ATTR_UNUSED;
4649 /* Windows may claim the internal mic to be BOTH, too */
4650 if (conn == AC_JACK_PORT_FIXED || conn == AC_JACK_PORT_BOTH)
4651 return INPUT_PIN_ATTR_INT;
4652 if ((loc & 0x30) == AC_JACK_LOC_INTERNAL)
4653 return INPUT_PIN_ATTR_INT;
4654 if ((loc & 0x30) == AC_JACK_LOC_SEPARATE)
4655 return INPUT_PIN_ATTR_DOCK;
4656 if (loc == AC_JACK_LOC_REAR)
4657 return INPUT_PIN_ATTR_REAR;
4658 if (loc == AC_JACK_LOC_FRONT)
4659 return INPUT_PIN_ATTR_FRONT;
4660 return INPUT_PIN_ATTR_NORMAL;
4662 EXPORT_SYMBOL_HDA(snd_hda_get_input_pin_attr);
4665 * hda_get_input_pin_label - Give a label for the given input pin
4667 * When check_location is true, the function checks the pin location
4668 * for mic and line-in pins, and set an appropriate prefix like "Front",
4669 * "Rear", "Internal".
4672 const char *hda_get_input_pin_label(struct hda_codec *codec, hda_nid_t pin,
4675 unsigned int def_conf;
4676 static const char *mic_names[] = {
4677 "Internal Mic", "Dock Mic", "Mic", "Front Mic", "Rear Mic",
4681 def_conf = snd_hda_codec_get_pincfg(codec, pin);
4683 switch (get_defcfg_device(def_conf)) {
4684 case AC_JACK_MIC_IN:
4685 if (!check_location)
4687 attr = snd_hda_get_input_pin_attr(def_conf);
4690 return mic_names[attr - 1];
4691 case AC_JACK_LINE_IN:
4692 if (!check_location)
4694 attr = snd_hda_get_input_pin_attr(def_conf);
4697 if (attr == INPUT_PIN_ATTR_DOCK)
4704 case AC_JACK_SPDIF_IN:
4706 case AC_JACK_DIG_OTHER_IN:
4707 return "Digital In";
4712 EXPORT_SYMBOL_HDA(hda_get_input_pin_label);
4714 /* Check whether the location prefix needs to be added to the label.
4715 * If all mic-jacks are in the same location (e.g. rear panel), we don't
4716 * have to put "Front" prefix to each label. In such a case, returns false.
4718 static int check_mic_location_need(struct hda_codec *codec,
4719 const struct auto_pin_cfg *cfg,
4725 defc = snd_hda_codec_get_pincfg(codec, cfg->inputs[input].pin);
4726 attr = snd_hda_get_input_pin_attr(defc);
4727 /* for internal or docking mics, we need locations */
4728 if (attr <= INPUT_PIN_ATTR_NORMAL)
4732 for (i = 0; i < cfg->num_inputs; i++) {
4733 defc = snd_hda_codec_get_pincfg(codec, cfg->inputs[i].pin);
4734 attr2 = snd_hda_get_input_pin_attr(defc);
4735 if (attr2 >= INPUT_PIN_ATTR_NORMAL) {
4736 if (attr && attr != attr2)
4737 return 1; /* different locations found */
4745 * hda_get_autocfg_input_label - Get a label for the given input
4747 * Get a label for the given input pin defined by the autocfg item.
4748 * Unlike hda_get_input_pin_label(), this function checks all inputs
4749 * defined in autocfg and avoids the redundant mic/line prefix as much as
4752 const char *hda_get_autocfg_input_label(struct hda_codec *codec,
4753 const struct auto_pin_cfg *cfg,
4756 int type = cfg->inputs[input].type;
4757 int has_multiple_pins = 0;
4759 if ((input > 0 && cfg->inputs[input - 1].type == type) ||
4760 (input < cfg->num_inputs - 1 && cfg->inputs[input + 1].type == type))
4761 has_multiple_pins = 1;
4762 if (has_multiple_pins && type == AUTO_PIN_MIC)
4763 has_multiple_pins &= check_mic_location_need(codec, cfg, input);
4764 return hda_get_input_pin_label(codec, cfg->inputs[input].pin,
4767 EXPORT_SYMBOL_HDA(hda_get_autocfg_input_label);
4770 * snd_hda_add_imux_item - Add an item to input_mux
4772 * When the same label is used already in the existing items, the number
4773 * suffix is appended to the label. This label index number is stored
4774 * to type_idx when non-NULL pointer is given.
4776 int snd_hda_add_imux_item(struct hda_input_mux *imux, const char *label,
4777 int index, int *type_idx)
4779 int i, label_idx = 0;
4780 if (imux->num_items >= HDA_MAX_NUM_INPUTS) {
4781 snd_printd(KERN_ERR "hda_codec: Too many imux items!\n");
4784 for (i = 0; i < imux->num_items; i++) {
4785 if (!strncmp(label, imux->items[i].label, strlen(label)))
4789 *type_idx = label_idx;
4791 snprintf(imux->items[imux->num_items].label,
4792 sizeof(imux->items[imux->num_items].label),
4793 "%s %d", label, label_idx);
4795 strlcpy(imux->items[imux->num_items].label, label,
4796 sizeof(imux->items[imux->num_items].label));
4797 imux->items[imux->num_items].index = index;
4801 EXPORT_SYMBOL_HDA(snd_hda_add_imux_item);
4810 * snd_hda_suspend - suspend the codecs
4813 * Returns 0 if successful.
4815 int snd_hda_suspend(struct hda_bus *bus)
4817 struct hda_codec *codec;
4819 list_for_each_entry(codec, &bus->codec_list, list) {
4820 #ifdef CONFIG_SND_HDA_POWER_SAVE
4821 if (!codec->power_on)
4824 hda_call_codec_suspend(codec);
4828 EXPORT_SYMBOL_HDA(snd_hda_suspend);
4831 * snd_hda_resume - resume the codecs
4834 * Returns 0 if successful.
4836 * This fucntion is defined only when POWER_SAVE isn't set.
4837 * In the power-save mode, the codec is resumed dynamically.
4839 int snd_hda_resume(struct hda_bus *bus)
4841 struct hda_codec *codec;
4843 list_for_each_entry(codec, &bus->codec_list, list) {
4844 if (snd_hda_codec_needs_resume(codec))
4845 hda_call_codec_resume(codec);
4849 EXPORT_SYMBOL_HDA(snd_hda_resume);
4850 #endif /* CONFIG_PM */
4857 * snd_array_new - get a new element from the given array
4858 * @array: the array object
4860 * Get a new element from the given array. If it exceeds the
4861 * pre-allocated array size, re-allocate the array.
4863 * Returns NULL if allocation failed.
4865 void *snd_array_new(struct snd_array *array)
4867 if (array->used >= array->alloced) {
4868 int num = array->alloced + array->alloc_align;
4870 if (snd_BUG_ON(num >= 4096))
4872 nlist = kcalloc(num + 1, array->elem_size, GFP_KERNEL);
4876 memcpy(nlist, array->list,
4877 array->elem_size * array->alloced);
4880 array->list = nlist;
4881 array->alloced = num;
4883 return snd_array_elem(array, array->used++);
4885 EXPORT_SYMBOL_HDA(snd_array_new);
4888 * snd_array_free - free the given array elements
4889 * @array: the array object
4891 void snd_array_free(struct snd_array *array)
4898 EXPORT_SYMBOL_HDA(snd_array_free);
4901 * snd_print_pcm_rates - Print the supported PCM rates to the string buffer
4902 * @pcm: PCM caps bits
4903 * @buf: the string buffer to write
4904 * @buflen: the max buffer length
4906 * used by hda_proc.c and hda_eld.c
4908 void snd_print_pcm_rates(int pcm, char *buf, int buflen)
4910 static unsigned int rates[] = {
4911 8000, 11025, 16000, 22050, 32000, 44100, 48000, 88200,
4912 96000, 176400, 192000, 384000
4916 for (i = 0, j = 0; i < ARRAY_SIZE(rates); i++)
4918 j += snprintf(buf + j, buflen - j, " %d", rates[i]);
4920 buf[j] = '\0'; /* necessary when j == 0 */
4922 EXPORT_SYMBOL_HDA(snd_print_pcm_rates);
4925 * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer
4926 * @pcm: PCM caps bits
4927 * @buf: the string buffer to write
4928 * @buflen: the max buffer length
4930 * used by hda_proc.c and hda_eld.c
4932 void snd_print_pcm_bits(int pcm, char *buf, int buflen)
4934 static unsigned int bits[] = { 8, 16, 20, 24, 32 };
4937 for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
4938 if (pcm & (AC_SUPPCM_BITS_8 << i))
4939 j += snprintf(buf + j, buflen - j, " %d", bits[i]);
4941 buf[j] = '\0'; /* necessary when j == 0 */
4943 EXPORT_SYMBOL_HDA(snd_print_pcm_bits);
4945 MODULE_DESCRIPTION("HDA codec core");
4946 MODULE_LICENSE("GPL");