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 INIT_LIST_HEAD(&bus->codec_list);
594 snprintf(bus->workq_name, sizeof(bus->workq_name),
595 "hd-audio%d", card->number);
596 bus->workq = create_singlethread_workqueue(bus->workq_name);
598 snd_printk(KERN_ERR "cannot create workqueue %s\n",
604 err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops);
606 snd_hda_bus_free(bus);
613 EXPORT_SYMBOL_HDA(snd_hda_bus_new);
615 #ifdef CONFIG_SND_HDA_GENERIC
616 #define is_generic_config(codec) \
617 (codec->modelname && !strcmp(codec->modelname, "generic"))
619 #define is_generic_config(codec) 0
623 #define HDA_MODREQ_MAX_COUNT 2 /* two request_modules()'s */
625 #define HDA_MODREQ_MAX_COUNT 0 /* all presets are statically linked */
629 * find a matching codec preset
631 static const struct hda_codec_preset *
632 find_codec_preset(struct hda_codec *codec)
634 struct hda_codec_preset_list *tbl;
635 const struct hda_codec_preset *preset;
636 int mod_requested = 0;
638 if (is_generic_config(codec))
639 return NULL; /* use the generic parser */
642 mutex_lock(&preset_mutex);
643 list_for_each_entry(tbl, &hda_preset_tables, list) {
644 if (!try_module_get(tbl->owner)) {
645 snd_printk(KERN_ERR "hda_codec: cannot module_get\n");
648 for (preset = tbl->preset; preset->id; preset++) {
649 u32 mask = preset->mask;
650 if (preset->afg && preset->afg != codec->afg)
652 if (preset->mfg && preset->mfg != codec->mfg)
656 if (preset->id == (codec->vendor_id & mask) &&
658 preset->rev == codec->revision_id)) {
659 mutex_unlock(&preset_mutex);
660 codec->owner = tbl->owner;
664 module_put(tbl->owner);
666 mutex_unlock(&preset_mutex);
668 if (mod_requested < HDA_MODREQ_MAX_COUNT) {
671 snprintf(name, sizeof(name), "snd-hda-codec-id:%08x",
674 snprintf(name, sizeof(name), "snd-hda-codec-id:%04x*",
675 (codec->vendor_id >> 16) & 0xffff);
676 request_module(name);
684 * get_codec_name - store the codec name
686 static int get_codec_name(struct hda_codec *codec)
688 const struct hda_vendor_id *c;
689 const char *vendor = NULL;
690 u16 vendor_id = codec->vendor_id >> 16;
693 if (codec->vendor_name)
696 for (c = hda_vendor_ids; c->id; c++) {
697 if (c->id == vendor_id) {
703 sprintf(tmp, "Generic %04x", vendor_id);
706 codec->vendor_name = kstrdup(vendor, GFP_KERNEL);
707 if (!codec->vendor_name)
711 if (codec->chip_name)
714 if (codec->preset && codec->preset->name)
715 codec->chip_name = kstrdup(codec->preset->name, GFP_KERNEL);
717 sprintf(tmp, "ID %x", codec->vendor_id & 0xffff);
718 codec->chip_name = kstrdup(tmp, GFP_KERNEL);
720 if (!codec->chip_name)
726 * look for an AFG and MFG nodes
728 static void /*__devinit*/ setup_fg_nodes(struct hda_codec *codec)
730 int i, total_nodes, function_id;
733 total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid);
734 for (i = 0; i < total_nodes; i++, nid++) {
735 function_id = snd_hda_param_read(codec, nid,
736 AC_PAR_FUNCTION_TYPE);
737 switch (function_id & 0xff) {
738 case AC_GRP_AUDIO_FUNCTION:
740 codec->afg_function_id = function_id & 0xff;
741 codec->afg_unsol = (function_id >> 8) & 1;
743 case AC_GRP_MODEM_FUNCTION:
745 codec->mfg_function_id = function_id & 0xff;
746 codec->mfg_unsol = (function_id >> 8) & 1;
755 * read widget caps for each widget and store in cache
757 static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
762 codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node,
764 codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL);
767 nid = codec->start_nid;
768 for (i = 0; i < codec->num_nodes; i++, nid++)
769 codec->wcaps[i] = snd_hda_param_read(codec, nid,
770 AC_PAR_AUDIO_WIDGET_CAP);
774 /* read all pin default configurations and save codec->init_pins */
775 static int read_pin_defaults(struct hda_codec *codec)
778 hda_nid_t nid = codec->start_nid;
780 for (i = 0; i < codec->num_nodes; i++, nid++) {
781 struct hda_pincfg *pin;
782 unsigned int wcaps = get_wcaps(codec, nid);
783 unsigned int wid_type = get_wcaps_type(wcaps);
784 if (wid_type != AC_WID_PIN)
786 pin = snd_array_new(&codec->init_pins);
790 pin->cfg = snd_hda_codec_read(codec, nid, 0,
791 AC_VERB_GET_CONFIG_DEFAULT, 0);
792 pin->ctrl = snd_hda_codec_read(codec, nid, 0,
793 AC_VERB_GET_PIN_WIDGET_CONTROL,
799 /* look up the given pin config list and return the item matching with NID */
800 static struct hda_pincfg *look_up_pincfg(struct hda_codec *codec,
801 struct snd_array *array,
805 for (i = 0; i < array->used; i++) {
806 struct hda_pincfg *pin = snd_array_elem(array, i);
813 /* write a config value for the given NID */
814 static void set_pincfg(struct hda_codec *codec, hda_nid_t nid,
818 for (i = 0; i < 4; i++) {
819 snd_hda_codec_write(codec, nid, 0,
820 AC_VERB_SET_CONFIG_DEFAULT_BYTES_0 + i,
826 /* set the current pin config value for the given NID.
827 * the value is cached, and read via snd_hda_codec_get_pincfg()
829 int snd_hda_add_pincfg(struct hda_codec *codec, struct snd_array *list,
830 hda_nid_t nid, unsigned int cfg)
832 struct hda_pincfg *pin;
835 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN)
838 oldcfg = snd_hda_codec_get_pincfg(codec, nid);
839 pin = look_up_pincfg(codec, list, nid);
841 pin = snd_array_new(list);
848 /* change only when needed; e.g. if the pincfg is already present
849 * in user_pins[], don't write it
851 cfg = snd_hda_codec_get_pincfg(codec, nid);
853 set_pincfg(codec, nid, cfg);
858 * snd_hda_codec_set_pincfg - Override a pin default configuration
859 * @codec: the HDA codec
860 * @nid: NID to set the pin config
861 * @cfg: the pin default config value
863 * Override a pin default configuration value in the cache.
864 * This value can be read by snd_hda_codec_get_pincfg() in a higher
865 * priority than the real hardware value.
867 int snd_hda_codec_set_pincfg(struct hda_codec *codec,
868 hda_nid_t nid, unsigned int cfg)
870 return snd_hda_add_pincfg(codec, &codec->driver_pins, nid, cfg);
872 EXPORT_SYMBOL_HDA(snd_hda_codec_set_pincfg);
875 * snd_hda_codec_get_pincfg - Obtain a pin-default configuration
876 * @codec: the HDA codec
877 * @nid: NID to get the pin config
879 * Get the current pin config value of the given pin NID.
880 * If the pincfg value is cached or overridden via sysfs or driver,
881 * returns the cached value.
883 unsigned int snd_hda_codec_get_pincfg(struct hda_codec *codec, hda_nid_t nid)
885 struct hda_pincfg *pin;
887 #ifdef CONFIG_SND_HDA_HWDEP
888 pin = look_up_pincfg(codec, &codec->user_pins, nid);
892 pin = look_up_pincfg(codec, &codec->driver_pins, nid);
895 pin = look_up_pincfg(codec, &codec->init_pins, nid);
900 EXPORT_SYMBOL_HDA(snd_hda_codec_get_pincfg);
902 /* restore all current pin configs */
903 static void restore_pincfgs(struct hda_codec *codec)
906 for (i = 0; i < codec->init_pins.used; i++) {
907 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
908 set_pincfg(codec, pin->nid,
909 snd_hda_codec_get_pincfg(codec, pin->nid));
914 * snd_hda_shutup_pins - Shut up all pins
915 * @codec: the HDA codec
917 * Clear all pin controls to shup up before suspend for avoiding click noise.
918 * The controls aren't cached so that they can be resumed properly.
920 void snd_hda_shutup_pins(struct hda_codec *codec)
923 /* don't shut up pins when unloading the driver; otherwise it breaks
924 * the default pin setup at the next load of the driver
926 if (codec->bus->shutdown)
928 for (i = 0; i < codec->init_pins.used; i++) {
929 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
930 /* use read here for syncing after issuing each verb */
931 snd_hda_codec_read(codec, pin->nid, 0,
932 AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
934 codec->pins_shutup = 1;
936 EXPORT_SYMBOL_HDA(snd_hda_shutup_pins);
938 /* Restore the pin controls cleared previously via snd_hda_shutup_pins() */
939 static void restore_shutup_pins(struct hda_codec *codec)
942 if (!codec->pins_shutup)
944 if (codec->bus->shutdown)
946 for (i = 0; i < codec->init_pins.used; i++) {
947 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
948 snd_hda_codec_write(codec, pin->nid, 0,
949 AC_VERB_SET_PIN_WIDGET_CONTROL,
952 codec->pins_shutup = 0;
955 static void init_hda_cache(struct hda_cache_rec *cache,
956 unsigned int record_size);
957 static void free_hda_cache(struct hda_cache_rec *cache);
959 /* restore the initial pin cfgs and release all pincfg lists */
960 static void restore_init_pincfgs(struct hda_codec *codec)
962 /* first free driver_pins and user_pins, then call restore_pincfg
963 * so that only the values in init_pins are restored
965 snd_array_free(&codec->driver_pins);
966 #ifdef CONFIG_SND_HDA_HWDEP
967 snd_array_free(&codec->user_pins);
969 restore_pincfgs(codec);
970 snd_array_free(&codec->init_pins);
974 * audio-converter setup caches
976 struct hda_cvt_setup {
981 unsigned char active; /* cvt is currently used */
982 unsigned char dirty; /* setups should be cleared */
985 /* get or create a cache entry for the given audio converter NID */
986 static struct hda_cvt_setup *
987 get_hda_cvt_setup(struct hda_codec *codec, hda_nid_t nid)
989 struct hda_cvt_setup *p;
992 for (i = 0; i < codec->cvt_setups.used; i++) {
993 p = snd_array_elem(&codec->cvt_setups, i);
997 p = snd_array_new(&codec->cvt_setups);
1006 static void snd_hda_codec_free(struct hda_codec *codec)
1010 restore_init_pincfgs(codec);
1011 #ifdef CONFIG_SND_HDA_POWER_SAVE
1012 cancel_delayed_work(&codec->power_work);
1013 flush_workqueue(codec->bus->workq);
1015 list_del(&codec->list);
1016 snd_array_free(&codec->mixers);
1017 snd_array_free(&codec->nids);
1018 codec->bus->caddr_tbl[codec->addr] = NULL;
1019 if (codec->patch_ops.free)
1020 codec->patch_ops.free(codec);
1021 module_put(codec->owner);
1022 free_hda_cache(&codec->amp_cache);
1023 free_hda_cache(&codec->cmd_cache);
1024 kfree(codec->vendor_name);
1025 kfree(codec->chip_name);
1026 kfree(codec->modelname);
1027 kfree(codec->wcaps);
1031 static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
1032 unsigned int power_state);
1035 * snd_hda_codec_new - create a HDA codec
1036 * @bus: the bus to assign
1037 * @codec_addr: the codec address
1038 * @codecp: the pointer to store the generated codec
1040 * Returns 0 if successful, or a negative error code.
1042 int /*__devinit*/ snd_hda_codec_new(struct hda_bus *bus,
1043 unsigned int codec_addr,
1044 struct hda_codec **codecp)
1046 struct hda_codec *codec;
1050 if (snd_BUG_ON(!bus))
1052 if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS))
1055 if (bus->caddr_tbl[codec_addr]) {
1056 snd_printk(KERN_ERR "hda_codec: "
1057 "address 0x%x is already occupied\n", codec_addr);
1061 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
1062 if (codec == NULL) {
1063 snd_printk(KERN_ERR "can't allocate struct hda_codec\n");
1068 codec->addr = codec_addr;
1069 mutex_init(&codec->spdif_mutex);
1070 mutex_init(&codec->control_mutex);
1071 mutex_init(&codec->prepare_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_cvt_setup *p;
1217 unsigned int oldval, newval;
1223 snd_printdd("hda_codec_setup_stream: "
1224 "NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
1225 nid, stream_tag, channel_id, format);
1226 p = get_hda_cvt_setup(codec, nid);
1229 /* update the stream-id if changed */
1230 if (p->stream_tag != stream_tag || p->channel_id != channel_id) {
1231 oldval = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0);
1232 newval = (stream_tag << 4) | channel_id;
1233 if (oldval != newval)
1234 snd_hda_codec_write(codec, nid, 0,
1235 AC_VERB_SET_CHANNEL_STREAMID,
1237 p->stream_tag = stream_tag;
1238 p->channel_id = channel_id;
1240 /* update the format-id if changed */
1241 if (p->format_id != format) {
1242 oldval = snd_hda_codec_read(codec, nid, 0,
1243 AC_VERB_GET_STREAM_FORMAT, 0);
1244 if (oldval != format) {
1246 snd_hda_codec_write(codec, nid, 0,
1247 AC_VERB_SET_STREAM_FORMAT,
1250 p->format_id = format;
1255 /* make other inactive cvts with the same stream-tag dirty */
1256 for (i = 0; i < codec->cvt_setups.used; i++) {
1257 p = snd_array_elem(&codec->cvt_setups, i);
1258 if (!p->active && p->stream_tag == stream_tag)
1262 EXPORT_SYMBOL_HDA(snd_hda_codec_setup_stream);
1265 * snd_hda_codec_cleanup_stream - clean up the codec for closing
1266 * @codec: the CODEC to clean up
1267 * @nid: the NID to clean up
1269 void snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid)
1271 struct hda_cvt_setup *p;
1276 snd_printdd("hda_codec_cleanup_stream: NID=0x%x\n", nid);
1277 /* here we just clear the active flag; actual clean-ups will be done
1278 * in purify_inactive_streams()
1280 p = get_hda_cvt_setup(codec, nid);
1284 EXPORT_SYMBOL_HDA(snd_hda_codec_cleanup_stream);
1286 static void really_cleanup_stream(struct hda_codec *codec,
1287 struct hda_cvt_setup *q)
1289 hda_nid_t nid = q->nid;
1290 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
1291 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0);
1292 memset(q, 0, sizeof(*q));
1296 /* clean up the all conflicting obsolete streams */
1297 static void purify_inactive_streams(struct hda_codec *codec)
1301 for (i = 0; i < codec->cvt_setups.used; i++) {
1302 struct hda_cvt_setup *p = snd_array_elem(&codec->cvt_setups, i);
1304 really_cleanup_stream(codec, p);
1308 /* clean up all streams; called from suspend */
1309 static void hda_cleanup_all_streams(struct hda_codec *codec)
1313 for (i = 0; i < codec->cvt_setups.used; i++) {
1314 struct hda_cvt_setup *p = snd_array_elem(&codec->cvt_setups, i);
1316 really_cleanup_stream(codec, p);
1321 * amp access functions
1324 /* FIXME: more better hash key? */
1325 #define HDA_HASH_KEY(nid, dir, idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
1326 #define HDA_HASH_PINCAP_KEY(nid) (u32)((nid) + (0x02 << 24))
1327 #define HDA_HASH_PARPCM_KEY(nid) (u32)((nid) + (0x03 << 24))
1328 #define HDA_HASH_PARSTR_KEY(nid) (u32)((nid) + (0x04 << 24))
1329 #define INFO_AMP_CAPS (1<<0)
1330 #define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
1332 /* initialize the hash table */
1333 static void /*__devinit*/ init_hda_cache(struct hda_cache_rec *cache,
1334 unsigned int record_size)
1336 memset(cache, 0, sizeof(*cache));
1337 memset(cache->hash, 0xff, sizeof(cache->hash));
1338 snd_array_init(&cache->buf, record_size, 64);
1341 static void free_hda_cache(struct hda_cache_rec *cache)
1343 snd_array_free(&cache->buf);
1346 /* query the hash. allocate an entry if not found. */
1347 static struct hda_cache_head *get_hash(struct hda_cache_rec *cache, u32 key)
1349 u16 idx = key % (u16)ARRAY_SIZE(cache->hash);
1350 u16 cur = cache->hash[idx];
1351 struct hda_cache_head *info;
1353 while (cur != 0xffff) {
1354 info = snd_array_elem(&cache->buf, cur);
1355 if (info->key == key)
1362 /* query the hash. allocate an entry if not found. */
1363 static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache,
1366 struct hda_cache_head *info = get_hash(cache, key);
1369 /* add a new hash entry */
1370 info = snd_array_new(&cache->buf);
1373 cur = snd_array_index(&cache->buf, info);
1376 idx = key % (u16)ARRAY_SIZE(cache->hash);
1377 info->next = cache->hash[idx];
1378 cache->hash[idx] = cur;
1383 /* query and allocate an amp hash entry */
1384 static inline struct hda_amp_info *
1385 get_alloc_amp_hash(struct hda_codec *codec, u32 key)
1387 return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key);
1391 * query_amp_caps - query AMP capabilities
1392 * @codec: the HD-auio codec
1393 * @nid: the NID to query
1394 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1396 * Query AMP capabilities for the given widget and direction.
1397 * Returns the obtained capability bits.
1399 * When cap bits have been already read, this doesn't read again but
1400 * returns the cached value.
1402 u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
1404 struct hda_amp_info *info;
1406 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, 0));
1409 if (!(info->head.val & INFO_AMP_CAPS)) {
1410 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
1412 info->amp_caps = snd_hda_param_read(codec, nid,
1413 direction == HDA_OUTPUT ?
1414 AC_PAR_AMP_OUT_CAP :
1417 info->head.val |= INFO_AMP_CAPS;
1419 return info->amp_caps;
1421 EXPORT_SYMBOL_HDA(query_amp_caps);
1424 * snd_hda_override_amp_caps - Override the AMP capabilities
1425 * @codec: the CODEC to clean up
1426 * @nid: the NID to clean up
1427 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1428 * @caps: the capability bits to set
1430 * Override the cached AMP caps bits value by the given one.
1431 * This function is useful if the driver needs to adjust the AMP ranges,
1432 * e.g. limit to 0dB, etc.
1434 * Returns zero if successful or a negative error code.
1436 int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
1439 struct hda_amp_info *info;
1441 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, dir, 0));
1444 info->amp_caps = caps;
1445 info->head.val |= INFO_AMP_CAPS;
1448 EXPORT_SYMBOL_HDA(snd_hda_override_amp_caps);
1451 query_caps_hash(struct hda_codec *codec, hda_nid_t nid, u32 key,
1452 unsigned int (*func)(struct hda_codec *, hda_nid_t))
1454 struct hda_amp_info *info;
1456 info = get_alloc_amp_hash(codec, key);
1459 if (!info->head.val) {
1460 info->head.val |= INFO_AMP_CAPS;
1461 info->amp_caps = func(codec, nid);
1463 return info->amp_caps;
1466 static unsigned int read_pin_cap(struct hda_codec *codec, hda_nid_t nid)
1468 return snd_hda_param_read(codec, nid, AC_PAR_PIN_CAP);
1472 * snd_hda_query_pin_caps - Query PIN capabilities
1473 * @codec: the HD-auio codec
1474 * @nid: the NID to query
1476 * Query PIN capabilities for the given widget.
1477 * Returns the obtained capability bits.
1479 * When cap bits have been already read, this doesn't read again but
1480 * returns the cached value.
1482 u32 snd_hda_query_pin_caps(struct hda_codec *codec, hda_nid_t nid)
1484 return query_caps_hash(codec, nid, HDA_HASH_PINCAP_KEY(nid),
1487 EXPORT_SYMBOL_HDA(snd_hda_query_pin_caps);
1490 * snd_hda_pin_sense - execute pin sense measurement
1491 * @codec: the CODEC to sense
1492 * @nid: the pin NID to sense
1494 * Execute necessary pin sense measurement and return its Presence Detect,
1495 * Impedance, ELD Valid etc. status bits.
1497 u32 snd_hda_pin_sense(struct hda_codec *codec, hda_nid_t nid)
1501 if (!codec->no_trigger_sense) {
1502 pincap = snd_hda_query_pin_caps(codec, nid);
1503 if (pincap & AC_PINCAP_TRIG_REQ) /* need trigger? */
1504 snd_hda_codec_read(codec, nid, 0,
1505 AC_VERB_SET_PIN_SENSE, 0);
1507 return snd_hda_codec_read(codec, nid, 0,
1508 AC_VERB_GET_PIN_SENSE, 0);
1510 EXPORT_SYMBOL_HDA(snd_hda_pin_sense);
1513 * snd_hda_jack_detect - query pin Presence Detect status
1514 * @codec: the CODEC to sense
1515 * @nid: the pin NID to sense
1517 * Query and return the pin's Presence Detect status.
1519 int snd_hda_jack_detect(struct hda_codec *codec, hda_nid_t nid)
1521 u32 sense = snd_hda_pin_sense(codec, nid);
1522 return !!(sense & AC_PINSENSE_PRESENCE);
1524 EXPORT_SYMBOL_HDA(snd_hda_jack_detect);
1527 * read the current volume to info
1528 * if the cache exists, read the cache value.
1530 static unsigned int get_vol_mute(struct hda_codec *codec,
1531 struct hda_amp_info *info, hda_nid_t nid,
1532 int ch, int direction, int index)
1536 if (info->head.val & INFO_AMP_VOL(ch))
1537 return info->vol[ch];
1539 parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
1540 parm |= direction == HDA_OUTPUT ? AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
1542 val = snd_hda_codec_read(codec, nid, 0,
1543 AC_VERB_GET_AMP_GAIN_MUTE, parm);
1544 info->vol[ch] = val & 0xff;
1545 info->head.val |= INFO_AMP_VOL(ch);
1546 return info->vol[ch];
1550 * write the current volume in info to the h/w and update the cache
1552 static void put_vol_mute(struct hda_codec *codec, struct hda_amp_info *info,
1553 hda_nid_t nid, int ch, int direction, int index,
1558 parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
1559 parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
1560 parm |= index << AC_AMP_SET_INDEX_SHIFT;
1562 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
1563 info->vol[ch] = val;
1567 * snd_hda_codec_amp_read - Read AMP value
1568 * @codec: HD-audio codec
1569 * @nid: NID to read the AMP value
1570 * @ch: channel (left=0 or right=1)
1571 * @direction: #HDA_INPUT or #HDA_OUTPUT
1572 * @index: the index value (only for input direction)
1574 * Read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
1576 int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
1577 int direction, int index)
1579 struct hda_amp_info *info;
1580 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
1583 return get_vol_mute(codec, info, nid, ch, direction, index);
1585 EXPORT_SYMBOL_HDA(snd_hda_codec_amp_read);
1588 * snd_hda_codec_amp_update - update the AMP value
1589 * @codec: HD-audio codec
1590 * @nid: NID to read the AMP value
1591 * @ch: channel (left=0 or right=1)
1592 * @direction: #HDA_INPUT or #HDA_OUTPUT
1593 * @idx: the index value (only for input direction)
1594 * @mask: bit mask to set
1595 * @val: the bits value to set
1597 * Update the AMP value with a bit mask.
1598 * Returns 0 if the value is unchanged, 1 if changed.
1600 int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
1601 int direction, int idx, int mask, int val)
1603 struct hda_amp_info *info;
1605 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, idx));
1608 if (snd_BUG_ON(mask & ~0xff))
1611 val |= get_vol_mute(codec, info, nid, ch, direction, idx) & ~mask;
1612 if (info->vol[ch] == val)
1614 put_vol_mute(codec, info, nid, ch, direction, idx, val);
1617 EXPORT_SYMBOL_HDA(snd_hda_codec_amp_update);
1620 * snd_hda_codec_amp_stereo - update the AMP stereo values
1621 * @codec: HD-audio codec
1622 * @nid: NID to read the AMP value
1623 * @direction: #HDA_INPUT or #HDA_OUTPUT
1624 * @idx: the index value (only for input direction)
1625 * @mask: bit mask to set
1626 * @val: the bits value to set
1628 * Update the AMP values like snd_hda_codec_amp_update(), but for a
1629 * stereo widget with the same mask and value.
1631 int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
1632 int direction, int idx, int mask, int val)
1636 if (snd_BUG_ON(mask & ~0xff))
1638 for (ch = 0; ch < 2; ch++)
1639 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
1643 EXPORT_SYMBOL_HDA(snd_hda_codec_amp_stereo);
1645 #ifdef SND_HDA_NEEDS_RESUME
1647 * snd_hda_codec_resume_amp - Resume all AMP commands from the cache
1648 * @codec: HD-audio codec
1650 * Resume the all amp commands from the cache.
1652 void snd_hda_codec_resume_amp(struct hda_codec *codec)
1654 struct hda_amp_info *buffer = codec->amp_cache.buf.list;
1657 for (i = 0; i < codec->amp_cache.buf.used; i++, buffer++) {
1658 u32 key = buffer->head.key;
1660 unsigned int idx, dir, ch;
1664 idx = (key >> 16) & 0xff;
1665 dir = (key >> 24) & 0xff;
1666 for (ch = 0; ch < 2; ch++) {
1667 if (!(buffer->head.val & INFO_AMP_VOL(ch)))
1669 put_vol_mute(codec, buffer, nid, ch, dir, idx,
1674 EXPORT_SYMBOL_HDA(snd_hda_codec_resume_amp);
1675 #endif /* SND_HDA_NEEDS_RESUME */
1677 static u32 get_amp_max_value(struct hda_codec *codec, hda_nid_t nid, int dir,
1680 u32 caps = query_amp_caps(codec, nid, dir);
1682 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1689 * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer
1691 * The control element is supposed to have the private_value field
1692 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1694 int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
1695 struct snd_ctl_elem_info *uinfo)
1697 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1698 u16 nid = get_amp_nid(kcontrol);
1699 u8 chs = get_amp_channels(kcontrol);
1700 int dir = get_amp_direction(kcontrol);
1701 unsigned int ofs = get_amp_offset(kcontrol);
1703 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1704 uinfo->count = chs == 3 ? 2 : 1;
1705 uinfo->value.integer.min = 0;
1706 uinfo->value.integer.max = get_amp_max_value(codec, nid, dir, ofs);
1707 if (!uinfo->value.integer.max) {
1708 printk(KERN_WARNING "hda_codec: "
1709 "num_steps = 0 for NID=0x%x (ctl = %s)\n", nid,
1715 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_info);
1718 static inline unsigned int
1719 read_amp_value(struct hda_codec *codec, hda_nid_t nid,
1720 int ch, int dir, int idx, unsigned int ofs)
1723 val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
1724 val &= HDA_AMP_VOLMASK;
1733 update_amp_value(struct hda_codec *codec, hda_nid_t nid,
1734 int ch, int dir, int idx, unsigned int ofs,
1737 unsigned int maxval;
1741 /* ofs = 0: raw max value */
1742 maxval = get_amp_max_value(codec, nid, dir, 0);
1745 return snd_hda_codec_amp_update(codec, nid, ch, dir, idx,
1746 HDA_AMP_VOLMASK, val);
1750 * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume
1752 * The control element is supposed to have the private_value field
1753 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1755 int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
1756 struct snd_ctl_elem_value *ucontrol)
1758 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1759 hda_nid_t nid = get_amp_nid(kcontrol);
1760 int chs = get_amp_channels(kcontrol);
1761 int dir = get_amp_direction(kcontrol);
1762 int idx = get_amp_index(kcontrol);
1763 unsigned int ofs = get_amp_offset(kcontrol);
1764 long *valp = ucontrol->value.integer.value;
1767 *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs);
1769 *valp = read_amp_value(codec, nid, 1, dir, idx, ofs);
1772 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_get);
1775 * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume
1777 * The control element is supposed to have the private_value field
1778 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1780 int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
1781 struct snd_ctl_elem_value *ucontrol)
1783 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1784 hda_nid_t nid = get_amp_nid(kcontrol);
1785 int chs = get_amp_channels(kcontrol);
1786 int dir = get_amp_direction(kcontrol);
1787 int idx = get_amp_index(kcontrol);
1788 unsigned int ofs = get_amp_offset(kcontrol);
1789 long *valp = ucontrol->value.integer.value;
1792 snd_hda_power_up(codec);
1794 change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp);
1798 change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp);
1799 snd_hda_power_down(codec);
1802 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_put);
1805 * snd_hda_mixer_amp_volume_put - TLV callback for a standard AMP mixer volume
1807 * The control element is supposed to have the private_value field
1808 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1810 int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
1811 unsigned int size, unsigned int __user *_tlv)
1813 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1814 hda_nid_t nid = get_amp_nid(kcontrol);
1815 int dir = get_amp_direction(kcontrol);
1816 unsigned int ofs = get_amp_offset(kcontrol);
1817 u32 caps, val1, val2;
1819 if (size < 4 * sizeof(unsigned int))
1821 caps = query_amp_caps(codec, nid, dir);
1822 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
1823 val2 = (val2 + 1) * 25;
1824 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
1826 val1 = ((int)val1) * ((int)val2);
1827 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
1829 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
1831 if (put_user(val1, _tlv + 2))
1833 if (put_user(val2, _tlv + 3))
1837 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_tlv);
1840 * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control
1841 * @codec: HD-audio codec
1842 * @nid: NID of a reference widget
1843 * @dir: #HDA_INPUT or #HDA_OUTPUT
1844 * @tlv: TLV data to be stored, at least 4 elements
1846 * Set (static) TLV data for a virtual master volume using the AMP caps
1847 * obtained from the reference NID.
1848 * The volume range is recalculated as if the max volume is 0dB.
1850 void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
1856 caps = query_amp_caps(codec, nid, dir);
1857 nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1858 step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
1859 step = (step + 1) * 25;
1860 tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
1861 tlv[1] = 2 * sizeof(unsigned int);
1862 tlv[2] = -nums * step;
1865 EXPORT_SYMBOL_HDA(snd_hda_set_vmaster_tlv);
1867 /* find a mixer control element with the given name */
1868 static struct snd_kcontrol *
1869 _snd_hda_find_mixer_ctl(struct hda_codec *codec,
1870 const char *name, int idx)
1872 struct snd_ctl_elem_id id;
1873 memset(&id, 0, sizeof(id));
1874 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1876 if (snd_BUG_ON(strlen(name) >= sizeof(id.name)))
1878 strcpy(id.name, name);
1879 return snd_ctl_find_id(codec->bus->card, &id);
1883 * snd_hda_find_mixer_ctl - Find a mixer control element with the given name
1884 * @codec: HD-audio codec
1885 * @name: ctl id name string
1887 * Get the control element with the given id string and IFACE_MIXER.
1889 struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
1892 return _snd_hda_find_mixer_ctl(codec, name, 0);
1894 EXPORT_SYMBOL_HDA(snd_hda_find_mixer_ctl);
1897 * snd_hda_ctl_add - Add a control element and assign to the codec
1898 * @codec: HD-audio codec
1899 * @nid: corresponding NID (optional)
1900 * @kctl: the control element to assign
1902 * Add the given control element to an array inside the codec instance.
1903 * All control elements belonging to a codec are supposed to be added
1904 * by this function so that a proper clean-up works at the free or
1905 * reconfiguration time.
1907 * If non-zero @nid is passed, the NID is assigned to the control element.
1908 * The assignment is shown in the codec proc file.
1910 * snd_hda_ctl_add() checks the control subdev id field whether
1911 * #HDA_SUBDEV_NID_FLAG bit is set. If set (and @nid is zero), the lower
1912 * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit
1913 * specifies if kctl->private_value is a HDA amplifier value.
1915 int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid,
1916 struct snd_kcontrol *kctl)
1919 unsigned short flags = 0;
1920 struct hda_nid_item *item;
1922 if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) {
1923 flags |= HDA_NID_ITEM_AMP;
1925 nid = get_amp_nid_(kctl->private_value);
1927 if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0)
1928 nid = kctl->id.subdevice & 0xffff;
1929 if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG))
1930 kctl->id.subdevice = 0;
1931 err = snd_ctl_add(codec->bus->card, kctl);
1934 item = snd_array_new(&codec->mixers);
1939 item->flags = flags;
1942 EXPORT_SYMBOL_HDA(snd_hda_ctl_add);
1945 * snd_hda_add_nid - Assign a NID to a control element
1946 * @codec: HD-audio codec
1947 * @nid: corresponding NID (optional)
1948 * @kctl: the control element to assign
1949 * @index: index to kctl
1951 * Add the given control element to an array inside the codec instance.
1952 * This function is used when #snd_hda_ctl_add cannot be used for 1:1
1953 * NID:KCTL mapping - for example "Capture Source" selector.
1955 int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl,
1956 unsigned int index, hda_nid_t nid)
1958 struct hda_nid_item *item;
1961 item = snd_array_new(&codec->nids);
1965 item->index = index;
1969 printk(KERN_ERR "hda-codec: no NID for mapping control %s:%d:%d\n",
1970 kctl->id.name, kctl->id.index, index);
1973 EXPORT_SYMBOL_HDA(snd_hda_add_nid);
1976 * snd_hda_ctls_clear - Clear all controls assigned to the given codec
1977 * @codec: HD-audio codec
1979 void snd_hda_ctls_clear(struct hda_codec *codec)
1982 struct hda_nid_item *items = codec->mixers.list;
1983 for (i = 0; i < codec->mixers.used; i++)
1984 snd_ctl_remove(codec->bus->card, items[i].kctl);
1985 snd_array_free(&codec->mixers);
1986 snd_array_free(&codec->nids);
1989 /* pseudo device locking
1990 * toggle card->shutdown to allow/disallow the device access (as a hack)
1992 static int hda_lock_devices(struct snd_card *card)
1994 spin_lock(&card->files_lock);
1995 if (card->shutdown) {
1996 spin_unlock(&card->files_lock);
2000 spin_unlock(&card->files_lock);
2004 static void hda_unlock_devices(struct snd_card *card)
2006 spin_lock(&card->files_lock);
2008 spin_unlock(&card->files_lock);
2012 * snd_hda_codec_reset - Clear all objects assigned to the codec
2013 * @codec: HD-audio codec
2015 * This frees the all PCM and control elements assigned to the codec, and
2016 * clears the caches and restores the pin default configurations.
2018 * When a device is being used, it returns -EBSY. If successfully freed,
2021 int snd_hda_codec_reset(struct hda_codec *codec)
2023 struct snd_card *card = codec->bus->card;
2026 if (hda_lock_devices(card) < 0)
2028 /* check whether the codec isn't used by any mixer or PCM streams */
2029 if (!list_empty(&card->ctl_files)) {
2030 hda_unlock_devices(card);
2033 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
2034 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
2037 if (cpcm->pcm->streams[0].substream_opened ||
2038 cpcm->pcm->streams[1].substream_opened) {
2039 hda_unlock_devices(card);
2044 /* OK, let it free */
2046 #ifdef CONFIG_SND_HDA_POWER_SAVE
2047 cancel_delayed_work(&codec->power_work);
2048 flush_workqueue(codec->bus->workq);
2050 snd_hda_ctls_clear(codec);
2052 for (i = 0; i < codec->num_pcms; i++) {
2053 if (codec->pcm_info[i].pcm) {
2054 snd_device_free(card, codec->pcm_info[i].pcm);
2055 clear_bit(codec->pcm_info[i].device,
2056 codec->bus->pcm_dev_bits);
2059 if (codec->patch_ops.free)
2060 codec->patch_ops.free(codec);
2061 codec->proc_widget_hook = NULL;
2063 free_hda_cache(&codec->amp_cache);
2064 free_hda_cache(&codec->cmd_cache);
2065 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
2066 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
2067 /* free only driver_pins so that init_pins + user_pins are restored */
2068 snd_array_free(&codec->driver_pins);
2069 restore_pincfgs(codec);
2070 codec->num_pcms = 0;
2071 codec->pcm_info = NULL;
2072 codec->preset = NULL;
2073 memset(&codec->patch_ops, 0, sizeof(codec->patch_ops));
2074 codec->slave_dig_outs = NULL;
2075 codec->spdif_status_reset = 0;
2076 module_put(codec->owner);
2077 codec->owner = NULL;
2079 /* allow device access again */
2080 hda_unlock_devices(card);
2085 * snd_hda_add_vmaster - create a virtual master control and add slaves
2086 * @codec: HD-audio codec
2087 * @name: vmaster control name
2088 * @tlv: TLV data (optional)
2089 * @slaves: slave control names (optional)
2091 * Create a virtual master control with the given name. The TLV data
2092 * must be either NULL or a valid data.
2094 * @slaves is a NULL-terminated array of strings, each of which is a
2095 * slave control name. All controls with these names are assigned to
2096 * the new virtual master control.
2098 * This function returns zero if successful or a negative error code.
2100 int snd_hda_add_vmaster(struct hda_codec *codec, char *name,
2101 unsigned int *tlv, const char **slaves)
2103 struct snd_kcontrol *kctl;
2107 for (s = slaves; *s && !snd_hda_find_mixer_ctl(codec, *s); s++)
2110 snd_printdd("No slave found for %s\n", name);
2113 kctl = snd_ctl_make_virtual_master(name, tlv);
2116 err = snd_hda_ctl_add(codec, 0, kctl);
2120 for (s = slaves; *s; s++) {
2121 struct snd_kcontrol *sctl;
2124 sctl = _snd_hda_find_mixer_ctl(codec, *s, i);
2127 snd_printdd("Cannot find slave %s, "
2131 err = snd_ctl_add_slave(kctl, sctl);
2139 EXPORT_SYMBOL_HDA(snd_hda_add_vmaster);
2142 * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch
2144 * The control element is supposed to have the private_value field
2145 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2147 int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
2148 struct snd_ctl_elem_info *uinfo)
2150 int chs = get_amp_channels(kcontrol);
2152 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2153 uinfo->count = chs == 3 ? 2 : 1;
2154 uinfo->value.integer.min = 0;
2155 uinfo->value.integer.max = 1;
2158 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_info);
2161 * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch
2163 * The control element is supposed to have the private_value field
2164 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2166 int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
2167 struct snd_ctl_elem_value *ucontrol)
2169 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2170 hda_nid_t nid = get_amp_nid(kcontrol);
2171 int chs = get_amp_channels(kcontrol);
2172 int dir = get_amp_direction(kcontrol);
2173 int idx = get_amp_index(kcontrol);
2174 long *valp = ucontrol->value.integer.value;
2177 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
2178 HDA_AMP_MUTE) ? 0 : 1;
2180 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
2181 HDA_AMP_MUTE) ? 0 : 1;
2184 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_get);
2187 * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch
2189 * The control element is supposed to have the private_value field
2190 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2192 int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
2193 struct snd_ctl_elem_value *ucontrol)
2195 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2196 hda_nid_t nid = get_amp_nid(kcontrol);
2197 int chs = get_amp_channels(kcontrol);
2198 int dir = get_amp_direction(kcontrol);
2199 int idx = get_amp_index(kcontrol);
2200 long *valp = ucontrol->value.integer.value;
2203 snd_hda_power_up(codec);
2205 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
2207 *valp ? 0 : HDA_AMP_MUTE);
2211 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
2213 *valp ? 0 : HDA_AMP_MUTE);
2214 #ifdef CONFIG_SND_HDA_POWER_SAVE
2215 if (codec->patch_ops.check_power_status)
2216 codec->patch_ops.check_power_status(codec, nid);
2218 snd_hda_power_down(codec);
2221 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put);
2223 #ifdef CONFIG_SND_HDA_INPUT_BEEP
2225 * snd_hda_mixer_amp_switch_put_beep - Put callback for a beep AMP switch
2227 * This function calls snd_hda_enable_beep_device(), which behaves differently
2228 * depending on beep_mode option.
2230 int snd_hda_mixer_amp_switch_put_beep(struct snd_kcontrol *kcontrol,
2231 struct snd_ctl_elem_value *ucontrol)
2233 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2234 long *valp = ucontrol->value.integer.value;
2236 snd_hda_enable_beep_device(codec, *valp);
2237 return snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2239 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put_beep);
2240 #endif /* CONFIG_SND_HDA_INPUT_BEEP */
2243 * bound volume controls
2245 * bind multiple volumes (# indices, from 0)
2248 #define AMP_VAL_IDX_SHIFT 19
2249 #define AMP_VAL_IDX_MASK (0x0f<<19)
2252 * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control
2254 * The control element is supposed to have the private_value field
2255 * set up via HDA_BIND_MUTE*() macros.
2257 int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
2258 struct snd_ctl_elem_value *ucontrol)
2260 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2264 mutex_lock(&codec->control_mutex);
2265 pval = kcontrol->private_value;
2266 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
2267 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
2268 kcontrol->private_value = pval;
2269 mutex_unlock(&codec->control_mutex);
2272 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_get);
2275 * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control
2277 * The control element is supposed to have the private_value field
2278 * set up via HDA_BIND_MUTE*() macros.
2280 int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
2281 struct snd_ctl_elem_value *ucontrol)
2283 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2285 int i, indices, err = 0, change = 0;
2287 mutex_lock(&codec->control_mutex);
2288 pval = kcontrol->private_value;
2289 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
2290 for (i = 0; i < indices; i++) {
2291 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
2292 (i << AMP_VAL_IDX_SHIFT);
2293 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2298 kcontrol->private_value = pval;
2299 mutex_unlock(&codec->control_mutex);
2300 return err < 0 ? err : change;
2302 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_put);
2305 * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control
2307 * The control element is supposed to have the private_value field
2308 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2310 int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
2311 struct snd_ctl_elem_info *uinfo)
2313 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2314 struct hda_bind_ctls *c;
2317 mutex_lock(&codec->control_mutex);
2318 c = (struct hda_bind_ctls *)kcontrol->private_value;
2319 kcontrol->private_value = *c->values;
2320 err = c->ops->info(kcontrol, uinfo);
2321 kcontrol->private_value = (long)c;
2322 mutex_unlock(&codec->control_mutex);
2325 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_info);
2328 * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control
2330 * The control element is supposed to have the private_value field
2331 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2333 int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
2334 struct snd_ctl_elem_value *ucontrol)
2336 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2337 struct hda_bind_ctls *c;
2340 mutex_lock(&codec->control_mutex);
2341 c = (struct hda_bind_ctls *)kcontrol->private_value;
2342 kcontrol->private_value = *c->values;
2343 err = c->ops->get(kcontrol, ucontrol);
2344 kcontrol->private_value = (long)c;
2345 mutex_unlock(&codec->control_mutex);
2348 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_get);
2351 * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control
2353 * The control element is supposed to have the private_value field
2354 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2356 int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
2357 struct snd_ctl_elem_value *ucontrol)
2359 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2360 struct hda_bind_ctls *c;
2361 unsigned long *vals;
2362 int err = 0, change = 0;
2364 mutex_lock(&codec->control_mutex);
2365 c = (struct hda_bind_ctls *)kcontrol->private_value;
2366 for (vals = c->values; *vals; vals++) {
2367 kcontrol->private_value = *vals;
2368 err = c->ops->put(kcontrol, ucontrol);
2373 kcontrol->private_value = (long)c;
2374 mutex_unlock(&codec->control_mutex);
2375 return err < 0 ? err : change;
2377 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_put);
2380 * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control
2382 * The control element is supposed to have the private_value field
2383 * set up via HDA_BIND_VOL() macro.
2385 int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2386 unsigned int size, unsigned int __user *tlv)
2388 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2389 struct hda_bind_ctls *c;
2392 mutex_lock(&codec->control_mutex);
2393 c = (struct hda_bind_ctls *)kcontrol->private_value;
2394 kcontrol->private_value = *c->values;
2395 err = c->ops->tlv(kcontrol, op_flag, size, tlv);
2396 kcontrol->private_value = (long)c;
2397 mutex_unlock(&codec->control_mutex);
2400 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_tlv);
2402 struct hda_ctl_ops snd_hda_bind_vol = {
2403 .info = snd_hda_mixer_amp_volume_info,
2404 .get = snd_hda_mixer_amp_volume_get,
2405 .put = snd_hda_mixer_amp_volume_put,
2406 .tlv = snd_hda_mixer_amp_tlv
2408 EXPORT_SYMBOL_HDA(snd_hda_bind_vol);
2410 struct hda_ctl_ops snd_hda_bind_sw = {
2411 .info = snd_hda_mixer_amp_switch_info,
2412 .get = snd_hda_mixer_amp_switch_get,
2413 .put = snd_hda_mixer_amp_switch_put,
2414 .tlv = snd_hda_mixer_amp_tlv
2416 EXPORT_SYMBOL_HDA(snd_hda_bind_sw);
2419 * SPDIF out controls
2422 static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
2423 struct snd_ctl_elem_info *uinfo)
2425 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
2430 static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
2431 struct snd_ctl_elem_value *ucontrol)
2433 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2434 IEC958_AES0_NONAUDIO |
2435 IEC958_AES0_CON_EMPHASIS_5015 |
2436 IEC958_AES0_CON_NOT_COPYRIGHT;
2437 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
2438 IEC958_AES1_CON_ORIGINAL;
2442 static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
2443 struct snd_ctl_elem_value *ucontrol)
2445 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2446 IEC958_AES0_NONAUDIO |
2447 IEC958_AES0_PRO_EMPHASIS_5015;
2451 static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
2452 struct snd_ctl_elem_value *ucontrol)
2454 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2456 ucontrol->value.iec958.status[0] = codec->spdif_status & 0xff;
2457 ucontrol->value.iec958.status[1] = (codec->spdif_status >> 8) & 0xff;
2458 ucontrol->value.iec958.status[2] = (codec->spdif_status >> 16) & 0xff;
2459 ucontrol->value.iec958.status[3] = (codec->spdif_status >> 24) & 0xff;
2464 /* convert from SPDIF status bits to HDA SPDIF bits
2465 * bit 0 (DigEn) is always set zero (to be filled later)
2467 static unsigned short convert_from_spdif_status(unsigned int sbits)
2469 unsigned short val = 0;
2471 if (sbits & IEC958_AES0_PROFESSIONAL)
2472 val |= AC_DIG1_PROFESSIONAL;
2473 if (sbits & IEC958_AES0_NONAUDIO)
2474 val |= AC_DIG1_NONAUDIO;
2475 if (sbits & IEC958_AES0_PROFESSIONAL) {
2476 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
2477 IEC958_AES0_PRO_EMPHASIS_5015)
2478 val |= AC_DIG1_EMPHASIS;
2480 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
2481 IEC958_AES0_CON_EMPHASIS_5015)
2482 val |= AC_DIG1_EMPHASIS;
2483 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
2484 val |= AC_DIG1_COPYRIGHT;
2485 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
2486 val |= AC_DIG1_LEVEL;
2487 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
2492 /* convert to SPDIF status bits from HDA SPDIF bits
2494 static unsigned int convert_to_spdif_status(unsigned short val)
2496 unsigned int sbits = 0;
2498 if (val & AC_DIG1_NONAUDIO)
2499 sbits |= IEC958_AES0_NONAUDIO;
2500 if (val & AC_DIG1_PROFESSIONAL)
2501 sbits |= IEC958_AES0_PROFESSIONAL;
2502 if (sbits & IEC958_AES0_PROFESSIONAL) {
2503 if (sbits & AC_DIG1_EMPHASIS)
2504 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
2506 if (val & AC_DIG1_EMPHASIS)
2507 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
2508 if (!(val & AC_DIG1_COPYRIGHT))
2509 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
2510 if (val & AC_DIG1_LEVEL)
2511 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
2512 sbits |= val & (0x7f << 8);
2517 /* set digital convert verbs both for the given NID and its slaves */
2518 static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
2523 snd_hda_codec_write_cache(codec, nid, 0, verb, val);
2524 d = codec->slave_dig_outs;
2528 snd_hda_codec_write_cache(codec, *d, 0, verb, val);
2531 static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
2535 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1);
2537 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2);
2540 static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
2541 struct snd_ctl_elem_value *ucontrol)
2543 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2544 hda_nid_t nid = kcontrol->private_value;
2548 mutex_lock(&codec->spdif_mutex);
2549 codec->spdif_status = ucontrol->value.iec958.status[0] |
2550 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
2551 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
2552 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
2553 val = convert_from_spdif_status(codec->spdif_status);
2554 val |= codec->spdif_ctls & 1;
2555 change = codec->spdif_ctls != val;
2556 codec->spdif_ctls = val;
2559 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
2561 mutex_unlock(&codec->spdif_mutex);
2565 #define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
2567 static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
2568 struct snd_ctl_elem_value *ucontrol)
2570 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2572 ucontrol->value.integer.value[0] = codec->spdif_ctls & AC_DIG1_ENABLE;
2576 static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
2577 struct snd_ctl_elem_value *ucontrol)
2579 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2580 hda_nid_t nid = kcontrol->private_value;
2584 mutex_lock(&codec->spdif_mutex);
2585 val = codec->spdif_ctls & ~AC_DIG1_ENABLE;
2586 if (ucontrol->value.integer.value[0])
2587 val |= AC_DIG1_ENABLE;
2588 change = codec->spdif_ctls != val;
2590 codec->spdif_ctls = val;
2591 set_dig_out_convert(codec, nid, val & 0xff, -1);
2592 /* unmute amp switch (if any) */
2593 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
2594 (val & AC_DIG1_ENABLE))
2595 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
2598 mutex_unlock(&codec->spdif_mutex);
2602 static struct snd_kcontrol_new dig_mixes[] = {
2604 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2605 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2606 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
2607 .info = snd_hda_spdif_mask_info,
2608 .get = snd_hda_spdif_cmask_get,
2611 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2612 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2613 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK),
2614 .info = snd_hda_spdif_mask_info,
2615 .get = snd_hda_spdif_pmask_get,
2618 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2619 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
2620 .info = snd_hda_spdif_mask_info,
2621 .get = snd_hda_spdif_default_get,
2622 .put = snd_hda_spdif_default_put,
2625 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2626 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
2627 .info = snd_hda_spdif_out_switch_info,
2628 .get = snd_hda_spdif_out_switch_get,
2629 .put = snd_hda_spdif_out_switch_put,
2634 #define SPDIF_MAX_IDX 4 /* 4 instances should be enough to probe */
2637 * snd_hda_create_spdif_out_ctls - create Output SPDIF-related controls
2638 * @codec: the HDA codec
2639 * @nid: audio out widget NID
2641 * Creates controls related with the SPDIF output.
2642 * Called from each patch supporting the SPDIF out.
2644 * Returns 0 if successful, or a negative error code.
2646 int snd_hda_create_spdif_out_ctls(struct hda_codec *codec, hda_nid_t nid)
2649 struct snd_kcontrol *kctl;
2650 struct snd_kcontrol_new *dig_mix;
2653 for (idx = 0; idx < SPDIF_MAX_IDX; idx++) {
2654 if (!_snd_hda_find_mixer_ctl(codec, "IEC958 Playback Switch",
2658 if (idx >= SPDIF_MAX_IDX) {
2659 printk(KERN_ERR "hda_codec: too many IEC958 outputs\n");
2662 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
2663 kctl = snd_ctl_new1(dig_mix, codec);
2666 kctl->id.index = idx;
2667 kctl->private_value = nid;
2668 err = snd_hda_ctl_add(codec, nid, kctl);
2673 snd_hda_codec_read(codec, nid, 0,
2674 AC_VERB_GET_DIGI_CONVERT_1, 0);
2675 codec->spdif_status = convert_to_spdif_status(codec->spdif_ctls);
2678 EXPORT_SYMBOL_HDA(snd_hda_create_spdif_out_ctls);
2681 * SPDIF sharing with analog output
2683 static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
2684 struct snd_ctl_elem_value *ucontrol)
2686 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
2687 ucontrol->value.integer.value[0] = mout->share_spdif;
2691 static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
2692 struct snd_ctl_elem_value *ucontrol)
2694 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
2695 mout->share_spdif = !!ucontrol->value.integer.value[0];
2699 static struct snd_kcontrol_new spdif_share_sw = {
2700 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2701 .name = "IEC958 Default PCM Playback Switch",
2702 .info = snd_ctl_boolean_mono_info,
2703 .get = spdif_share_sw_get,
2704 .put = spdif_share_sw_put,
2708 * snd_hda_create_spdif_share_sw - create Default PCM switch
2709 * @codec: the HDA codec
2710 * @mout: multi-out instance
2712 int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
2713 struct hda_multi_out *mout)
2715 if (!mout->dig_out_nid)
2717 /* ATTENTION: here mout is passed as private_data, instead of codec */
2718 return snd_hda_ctl_add(codec, mout->dig_out_nid,
2719 snd_ctl_new1(&spdif_share_sw, mout));
2721 EXPORT_SYMBOL_HDA(snd_hda_create_spdif_share_sw);
2727 #define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
2729 static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
2730 struct snd_ctl_elem_value *ucontrol)
2732 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2734 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
2738 static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
2739 struct snd_ctl_elem_value *ucontrol)
2741 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2742 hda_nid_t nid = kcontrol->private_value;
2743 unsigned int val = !!ucontrol->value.integer.value[0];
2746 mutex_lock(&codec->spdif_mutex);
2747 change = codec->spdif_in_enable != val;
2749 codec->spdif_in_enable = val;
2750 snd_hda_codec_write_cache(codec, nid, 0,
2751 AC_VERB_SET_DIGI_CONVERT_1, val);
2753 mutex_unlock(&codec->spdif_mutex);
2757 static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
2758 struct snd_ctl_elem_value *ucontrol)
2760 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2761 hda_nid_t nid = kcontrol->private_value;
2765 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0);
2766 sbits = convert_to_spdif_status(val);
2767 ucontrol->value.iec958.status[0] = sbits;
2768 ucontrol->value.iec958.status[1] = sbits >> 8;
2769 ucontrol->value.iec958.status[2] = sbits >> 16;
2770 ucontrol->value.iec958.status[3] = sbits >> 24;
2774 static struct snd_kcontrol_new dig_in_ctls[] = {
2776 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2777 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH),
2778 .info = snd_hda_spdif_in_switch_info,
2779 .get = snd_hda_spdif_in_switch_get,
2780 .put = snd_hda_spdif_in_switch_put,
2783 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2784 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2785 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
2786 .info = snd_hda_spdif_mask_info,
2787 .get = snd_hda_spdif_in_status_get,
2793 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
2794 * @codec: the HDA codec
2795 * @nid: audio in widget NID
2797 * Creates controls related with the SPDIF input.
2798 * Called from each patch supporting the SPDIF in.
2800 * Returns 0 if successful, or a negative error code.
2802 int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
2805 struct snd_kcontrol *kctl;
2806 struct snd_kcontrol_new *dig_mix;
2809 for (idx = 0; idx < SPDIF_MAX_IDX; idx++) {
2810 if (!_snd_hda_find_mixer_ctl(codec, "IEC958 Capture Switch",
2814 if (idx >= SPDIF_MAX_IDX) {
2815 printk(KERN_ERR "hda_codec: too many IEC958 inputs\n");
2818 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
2819 kctl = snd_ctl_new1(dig_mix, codec);
2822 kctl->private_value = nid;
2823 err = snd_hda_ctl_add(codec, nid, kctl);
2827 codec->spdif_in_enable =
2828 snd_hda_codec_read(codec, nid, 0,
2829 AC_VERB_GET_DIGI_CONVERT_1, 0) &
2833 EXPORT_SYMBOL_HDA(snd_hda_create_spdif_in_ctls);
2835 #ifdef SND_HDA_NEEDS_RESUME
2840 /* build a 32bit cache key with the widget id and the command parameter */
2841 #define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
2842 #define get_cmd_cache_nid(key) ((key) & 0xff)
2843 #define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
2846 * snd_hda_codec_write_cache - send a single command with caching
2847 * @codec: the HDA codec
2848 * @nid: NID to send the command
2849 * @direct: direct flag
2850 * @verb: the verb to send
2851 * @parm: the parameter for the verb
2853 * Send a single command without waiting for response.
2855 * Returns 0 if successful, or a negative error code.
2857 int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
2858 int direct, unsigned int verb, unsigned int parm)
2860 int err = snd_hda_codec_write(codec, nid, direct, verb, parm);
2861 struct hda_cache_head *c;
2866 /* parm may contain the verb stuff for get/set amp */
2867 verb = verb | (parm >> 8);
2869 key = build_cmd_cache_key(nid, verb);
2870 mutex_lock(&codec->bus->cmd_mutex);
2871 c = get_alloc_hash(&codec->cmd_cache, key);
2874 mutex_unlock(&codec->bus->cmd_mutex);
2877 EXPORT_SYMBOL_HDA(snd_hda_codec_write_cache);
2880 * snd_hda_codec_update_cache - check cache and write the cmd only when needed
2881 * @codec: the HDA codec
2882 * @nid: NID to send the command
2883 * @direct: direct flag
2884 * @verb: the verb to send
2885 * @parm: the parameter for the verb
2887 * This function works like snd_hda_codec_write_cache(), but it doesn't send
2888 * command if the parameter is already identical with the cached value.
2889 * If not, it sends the command and refreshes the cache.
2891 * Returns 0 if successful, or a negative error code.
2893 int snd_hda_codec_update_cache(struct hda_codec *codec, hda_nid_t nid,
2894 int direct, unsigned int verb, unsigned int parm)
2896 struct hda_cache_head *c;
2899 /* parm may contain the verb stuff for get/set amp */
2900 verb = verb | (parm >> 8);
2902 key = build_cmd_cache_key(nid, verb);
2903 mutex_lock(&codec->bus->cmd_mutex);
2904 c = get_hash(&codec->cmd_cache, key);
2905 if (c && c->val == parm) {
2906 mutex_unlock(&codec->bus->cmd_mutex);
2909 mutex_unlock(&codec->bus->cmd_mutex);
2910 return snd_hda_codec_write_cache(codec, nid, direct, verb, parm);
2912 EXPORT_SYMBOL_HDA(snd_hda_codec_update_cache);
2915 * snd_hda_codec_resume_cache - Resume the all commands from the cache
2916 * @codec: HD-audio codec
2918 * Execute all verbs recorded in the command caches to resume.
2920 void snd_hda_codec_resume_cache(struct hda_codec *codec)
2922 struct hda_cache_head *buffer = codec->cmd_cache.buf.list;
2925 for (i = 0; i < codec->cmd_cache.buf.used; i++, buffer++) {
2926 u32 key = buffer->key;
2929 snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
2930 get_cmd_cache_cmd(key), buffer->val);
2933 EXPORT_SYMBOL_HDA(snd_hda_codec_resume_cache);
2936 * snd_hda_sequence_write_cache - sequence writes with caching
2937 * @codec: the HDA codec
2938 * @seq: VERB array to send
2940 * Send the commands sequentially from the given array.
2941 * Thte commands are recorded on cache for power-save and resume.
2942 * The array must be terminated with NID=0.
2944 void snd_hda_sequence_write_cache(struct hda_codec *codec,
2945 const struct hda_verb *seq)
2947 for (; seq->nid; seq++)
2948 snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
2951 EXPORT_SYMBOL_HDA(snd_hda_sequence_write_cache);
2952 #endif /* SND_HDA_NEEDS_RESUME */
2955 * set power state of the codec
2957 static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
2958 unsigned int power_state)
2963 /* this delay seems necessary to avoid click noise at power-down */
2964 if (power_state == AC_PWRST_D3)
2966 snd_hda_codec_read(codec, fg, 0, AC_VERB_SET_POWER_STATE,
2968 /* partial workaround for "azx_get_response timeout" */
2969 if (power_state == AC_PWRST_D0 &&
2970 (codec->vendor_id & 0xffff0000) == 0x14f10000)
2973 nid = codec->start_nid;
2974 for (i = 0; i < codec->num_nodes; i++, nid++) {
2975 unsigned int wcaps = get_wcaps(codec, nid);
2976 if (wcaps & AC_WCAP_POWER) {
2977 unsigned int wid_type = get_wcaps_type(wcaps);
2978 if (power_state == AC_PWRST_D3 &&
2979 wid_type == AC_WID_PIN) {
2980 unsigned int pincap;
2982 * don't power down the widget if it controls
2983 * eapd and EAPD_BTLENABLE is set.
2985 pincap = snd_hda_query_pin_caps(codec, nid);
2986 if (pincap & AC_PINCAP_EAPD) {
2987 int eapd = snd_hda_codec_read(codec,
2989 AC_VERB_GET_EAPD_BTLENABLE, 0);
2995 snd_hda_codec_write(codec, nid, 0,
2996 AC_VERB_SET_POWER_STATE,
3001 if (power_state == AC_PWRST_D0) {
3002 unsigned long end_time;
3004 /* wait until the codec reachs to D0 */
3005 end_time = jiffies + msecs_to_jiffies(500);
3007 state = snd_hda_codec_read(codec, fg, 0,
3008 AC_VERB_GET_POWER_STATE, 0);
3009 if (state == power_state)
3012 } while (time_after_eq(end_time, jiffies));
3016 #ifdef CONFIG_SND_HDA_HWDEP
3017 /* execute additional init verbs */
3018 static void hda_exec_init_verbs(struct hda_codec *codec)
3020 if (codec->init_verbs.list)
3021 snd_hda_sequence_write(codec, codec->init_verbs.list);
3024 static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
3027 #ifdef SND_HDA_NEEDS_RESUME
3029 * call suspend and power-down; used both from PM and power-save
3031 static void hda_call_codec_suspend(struct hda_codec *codec)
3033 if (codec->patch_ops.suspend)
3034 codec->patch_ops.suspend(codec, PMSG_SUSPEND);
3035 hda_cleanup_all_streams(codec);
3036 hda_set_power_state(codec,
3037 codec->afg ? codec->afg : codec->mfg,
3039 #ifdef CONFIG_SND_HDA_POWER_SAVE
3040 snd_hda_update_power_acct(codec);
3041 cancel_delayed_work(&codec->power_work);
3042 codec->power_on = 0;
3043 codec->power_transition = 0;
3044 codec->power_jiffies = jiffies;
3049 * kick up codec; used both from PM and power-save
3051 static void hda_call_codec_resume(struct hda_codec *codec)
3053 hda_set_power_state(codec,
3054 codec->afg ? codec->afg : codec->mfg,
3056 restore_pincfgs(codec); /* restore all current pin configs */
3057 restore_shutup_pins(codec);
3058 hda_exec_init_verbs(codec);
3059 if (codec->patch_ops.resume)
3060 codec->patch_ops.resume(codec);
3062 if (codec->patch_ops.init)
3063 codec->patch_ops.init(codec);
3064 snd_hda_codec_resume_amp(codec);
3065 snd_hda_codec_resume_cache(codec);
3068 #endif /* SND_HDA_NEEDS_RESUME */
3072 * snd_hda_build_controls - build mixer controls
3075 * Creates mixer controls for each codec included in the bus.
3077 * Returns 0 if successful, otherwise a negative error code.
3079 int /*__devinit*/ snd_hda_build_controls(struct hda_bus *bus)
3081 struct hda_codec *codec;
3083 list_for_each_entry(codec, &bus->codec_list, list) {
3084 int err = snd_hda_codec_build_controls(codec);
3086 printk(KERN_ERR "hda_codec: cannot build controls "
3087 "for #%d (error %d)\n", codec->addr, err);
3088 err = snd_hda_codec_reset(codec);
3091 "hda_codec: cannot revert codec\n");
3098 EXPORT_SYMBOL_HDA(snd_hda_build_controls);
3100 int snd_hda_codec_build_controls(struct hda_codec *codec)
3103 hda_exec_init_verbs(codec);
3104 /* continue to initialize... */
3105 if (codec->patch_ops.init)
3106 err = codec->patch_ops.init(codec);
3107 if (!err && codec->patch_ops.build_controls)
3108 err = codec->patch_ops.build_controls(codec);
3117 struct hda_rate_tbl {
3119 unsigned int alsa_bits;
3120 unsigned int hda_fmt;
3123 /* rate = base * mult / div */
3124 #define HDA_RATE(base, mult, div) \
3125 (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \
3126 (((div) - 1) << AC_FMT_DIV_SHIFT))
3128 static struct hda_rate_tbl rate_bits[] = {
3129 /* rate in Hz, ALSA rate bitmask, HDA format value */
3131 /* autodetected value used in snd_hda_query_supported_pcm */
3132 { 8000, SNDRV_PCM_RATE_8000, HDA_RATE(48, 1, 6) },
3133 { 11025, SNDRV_PCM_RATE_11025, HDA_RATE(44, 1, 4) },
3134 { 16000, SNDRV_PCM_RATE_16000, HDA_RATE(48, 1, 3) },
3135 { 22050, SNDRV_PCM_RATE_22050, HDA_RATE(44, 1, 2) },
3136 { 32000, SNDRV_PCM_RATE_32000, HDA_RATE(48, 2, 3) },
3137 { 44100, SNDRV_PCM_RATE_44100, HDA_RATE(44, 1, 1) },
3138 { 48000, SNDRV_PCM_RATE_48000, HDA_RATE(48, 1, 1) },
3139 { 88200, SNDRV_PCM_RATE_88200, HDA_RATE(44, 2, 1) },
3140 { 96000, SNDRV_PCM_RATE_96000, HDA_RATE(48, 2, 1) },
3141 { 176400, SNDRV_PCM_RATE_176400, HDA_RATE(44, 4, 1) },
3142 { 192000, SNDRV_PCM_RATE_192000, HDA_RATE(48, 4, 1) },
3143 #define AC_PAR_PCM_RATE_BITS 11
3144 /* up to bits 10, 384kHZ isn't supported properly */
3146 /* not autodetected value */
3147 { 9600, SNDRV_PCM_RATE_KNOT, HDA_RATE(48, 1, 5) },
3149 { 0 } /* terminator */
3153 * snd_hda_calc_stream_format - calculate format bitset
3154 * @rate: the sample rate
3155 * @channels: the number of channels
3156 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
3157 * @maxbps: the max. bps
3159 * Calculate the format bitset from the given rate, channels and th PCM format.
3161 * Return zero if invalid.
3163 unsigned int snd_hda_calc_stream_format(unsigned int rate,
3164 unsigned int channels,
3165 unsigned int format,
3166 unsigned int maxbps,
3167 unsigned short spdif_ctls)
3170 unsigned int val = 0;
3172 for (i = 0; rate_bits[i].hz; i++)
3173 if (rate_bits[i].hz == rate) {
3174 val = rate_bits[i].hda_fmt;
3177 if (!rate_bits[i].hz) {
3178 snd_printdd("invalid rate %d\n", rate);
3182 if (channels == 0 || channels > 8) {
3183 snd_printdd("invalid channels %d\n", channels);
3186 val |= channels - 1;
3188 switch (snd_pcm_format_width(format)) {
3190 val |= AC_FMT_BITS_8;
3193 val |= AC_FMT_BITS_16;
3198 if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE)
3199 val |= AC_FMT_BITS_32;
3200 else if (maxbps >= 24)
3201 val |= AC_FMT_BITS_24;
3203 val |= AC_FMT_BITS_20;
3206 snd_printdd("invalid format width %d\n",
3207 snd_pcm_format_width(format));
3211 if (spdif_ctls & AC_DIG1_NONAUDIO)
3212 val |= AC_FMT_TYPE_NON_PCM;
3216 EXPORT_SYMBOL_HDA(snd_hda_calc_stream_format);
3218 static unsigned int get_pcm_param(struct hda_codec *codec, hda_nid_t nid)
3220 unsigned int val = 0;
3221 if (nid != codec->afg &&
3222 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD))
3223 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
3224 if (!val || val == -1)
3225 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
3226 if (!val || val == -1)
3231 static unsigned int query_pcm_param(struct hda_codec *codec, hda_nid_t nid)
3233 return query_caps_hash(codec, nid, HDA_HASH_PARPCM_KEY(nid),
3237 static unsigned int get_stream_param(struct hda_codec *codec, hda_nid_t nid)
3239 unsigned int streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
3240 if (!streams || streams == -1)
3241 streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
3242 if (!streams || streams == -1)
3247 static unsigned int query_stream_param(struct hda_codec *codec, hda_nid_t nid)
3249 return query_caps_hash(codec, nid, HDA_HASH_PARSTR_KEY(nid),
3254 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
3255 * @codec: the HDA codec
3256 * @nid: NID to query
3257 * @ratesp: the pointer to store the detected rate bitflags
3258 * @formatsp: the pointer to store the detected formats
3259 * @bpsp: the pointer to store the detected format widths
3261 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
3262 * or @bsps argument is ignored.
3264 * Returns 0 if successful, otherwise a negative error code.
3266 static int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
3267 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
3269 unsigned int i, val, wcaps;
3271 wcaps = get_wcaps(codec, nid);
3272 val = query_pcm_param(codec, nid);
3276 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
3278 rates |= rate_bits[i].alsa_bits;
3281 snd_printk(KERN_ERR "hda_codec: rates == 0 "
3282 "(nid=0x%x, val=0x%x, ovrd=%i)\n",
3284 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0);
3290 if (formatsp || bpsp) {
3292 unsigned int streams, bps;
3294 streams = query_stream_param(codec, nid);
3299 if (streams & AC_SUPFMT_PCM) {
3300 if (val & AC_SUPPCM_BITS_8) {
3301 formats |= SNDRV_PCM_FMTBIT_U8;
3304 if (val & AC_SUPPCM_BITS_16) {
3305 formats |= SNDRV_PCM_FMTBIT_S16_LE;
3308 if (wcaps & AC_WCAP_DIGITAL) {
3309 if (val & AC_SUPPCM_BITS_32)
3310 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
3311 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
3312 formats |= SNDRV_PCM_FMTBIT_S32_LE;
3313 if (val & AC_SUPPCM_BITS_24)
3315 else if (val & AC_SUPPCM_BITS_20)
3317 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
3318 AC_SUPPCM_BITS_32)) {
3319 formats |= SNDRV_PCM_FMTBIT_S32_LE;
3320 if (val & AC_SUPPCM_BITS_32)
3322 else if (val & AC_SUPPCM_BITS_24)
3324 else if (val & AC_SUPPCM_BITS_20)
3328 if (streams & AC_SUPFMT_FLOAT32) {
3329 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
3333 if (streams == AC_SUPFMT_AC3) {
3334 /* should be exclusive */
3335 /* temporary hack: we have still no proper support
3336 * for the direct AC3 stream...
3338 formats |= SNDRV_PCM_FMTBIT_U8;
3342 snd_printk(KERN_ERR "hda_codec: formats == 0 "
3343 "(nid=0x%x, val=0x%x, ovrd=%i, "
3346 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0,
3351 *formatsp = formats;
3360 * snd_hda_is_supported_format - Check the validity of the format
3361 * @codec: HD-audio codec
3362 * @nid: NID to check
3363 * @format: the HD-audio format value to check
3365 * Check whether the given node supports the format value.
3367 * Returns 1 if supported, 0 if not.
3369 int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
3370 unsigned int format)
3373 unsigned int val = 0, rate, stream;
3375 val = query_pcm_param(codec, nid);
3379 rate = format & 0xff00;
3380 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
3381 if (rate_bits[i].hda_fmt == rate) {
3386 if (i >= AC_PAR_PCM_RATE_BITS)
3389 stream = query_stream_param(codec, nid);
3393 if (stream & AC_SUPFMT_PCM) {
3394 switch (format & 0xf0) {
3396 if (!(val & AC_SUPPCM_BITS_8))
3400 if (!(val & AC_SUPPCM_BITS_16))
3404 if (!(val & AC_SUPPCM_BITS_20))
3408 if (!(val & AC_SUPPCM_BITS_24))
3412 if (!(val & AC_SUPPCM_BITS_32))
3419 /* FIXME: check for float32 and AC3? */
3424 EXPORT_SYMBOL_HDA(snd_hda_is_supported_format);
3429 static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
3430 struct hda_codec *codec,
3431 struct snd_pcm_substream *substream)
3436 static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
3437 struct hda_codec *codec,
3438 unsigned int stream_tag,
3439 unsigned int format,
3440 struct snd_pcm_substream *substream)
3442 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
3446 static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
3447 struct hda_codec *codec,
3448 struct snd_pcm_substream *substream)
3450 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
3454 static int set_pcm_default_values(struct hda_codec *codec,
3455 struct hda_pcm_stream *info)
3459 /* query support PCM information from the given NID */
3460 if (info->nid && (!info->rates || !info->formats)) {
3461 err = snd_hda_query_supported_pcm(codec, info->nid,
3462 info->rates ? NULL : &info->rates,
3463 info->formats ? NULL : &info->formats,
3464 info->maxbps ? NULL : &info->maxbps);
3468 if (info->ops.open == NULL)
3469 info->ops.open = hda_pcm_default_open_close;
3470 if (info->ops.close == NULL)
3471 info->ops.close = hda_pcm_default_open_close;
3472 if (info->ops.prepare == NULL) {
3473 if (snd_BUG_ON(!info->nid))
3475 info->ops.prepare = hda_pcm_default_prepare;
3477 if (info->ops.cleanup == NULL) {
3478 if (snd_BUG_ON(!info->nid))
3480 info->ops.cleanup = hda_pcm_default_cleanup;
3486 * codec prepare/cleanup entries
3488 int snd_hda_codec_prepare(struct hda_codec *codec,
3489 struct hda_pcm_stream *hinfo,
3490 unsigned int stream,
3491 unsigned int format,
3492 struct snd_pcm_substream *substream)
3495 mutex_lock(&codec->prepare_mutex);
3496 ret = hinfo->ops.prepare(hinfo, codec, stream, format, substream);
3498 purify_inactive_streams(codec);
3499 mutex_unlock(&codec->prepare_mutex);
3502 EXPORT_SYMBOL_HDA(snd_hda_codec_prepare);
3504 void snd_hda_codec_cleanup(struct hda_codec *codec,
3505 struct hda_pcm_stream *hinfo,
3506 struct snd_pcm_substream *substream)
3508 mutex_lock(&codec->prepare_mutex);
3509 hinfo->ops.cleanup(hinfo, codec, substream);
3510 mutex_unlock(&codec->prepare_mutex);
3512 EXPORT_SYMBOL_HDA(snd_hda_codec_cleanup);
3515 const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = {
3516 "Audio", "SPDIF", "HDMI", "Modem"
3520 * get the empty PCM device number to assign
3522 * note the max device number is limited by HDA_MAX_PCMS, currently 10
3524 static int get_empty_pcm_device(struct hda_bus *bus, int type)
3526 /* audio device indices; not linear to keep compatibility */
3527 static int audio_idx[HDA_PCM_NTYPES][5] = {
3528 [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 },
3529 [HDA_PCM_TYPE_SPDIF] = { 1, -1 },
3530 [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 },
3531 [HDA_PCM_TYPE_MODEM] = { 6, -1 },
3535 if (type >= HDA_PCM_NTYPES) {
3536 snd_printk(KERN_WARNING "Invalid PCM type %d\n", type);
3540 for (i = 0; audio_idx[type][i] >= 0 ; i++)
3541 if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits))
3542 return audio_idx[type][i];
3544 snd_printk(KERN_WARNING "Too many %s devices\n",
3545 snd_hda_pcm_type_name[type]);
3550 * attach a new PCM stream
3552 static int snd_hda_attach_pcm(struct hda_codec *codec, struct hda_pcm *pcm)
3554 struct hda_bus *bus = codec->bus;
3555 struct hda_pcm_stream *info;
3558 if (snd_BUG_ON(!pcm->name))
3560 for (stream = 0; stream < 2; stream++) {
3561 info = &pcm->stream[stream];
3562 if (info->substreams) {
3563 err = set_pcm_default_values(codec, info);
3568 return bus->ops.attach_pcm(bus, codec, pcm);
3571 /* assign all PCMs of the given codec */
3572 int snd_hda_codec_build_pcms(struct hda_codec *codec)
3577 if (!codec->num_pcms) {
3578 if (!codec->patch_ops.build_pcms)
3580 err = codec->patch_ops.build_pcms(codec);
3582 printk(KERN_ERR "hda_codec: cannot build PCMs"
3583 "for #%d (error %d)\n", codec->addr, err);
3584 err = snd_hda_codec_reset(codec);
3587 "hda_codec: cannot revert codec\n");
3592 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
3593 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
3596 if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
3597 continue; /* no substreams assigned */
3600 dev = get_empty_pcm_device(codec->bus, cpcm->pcm_type);
3602 continue; /* no fatal error */
3604 err = snd_hda_attach_pcm(codec, cpcm);
3606 printk(KERN_ERR "hda_codec: cannot attach "
3607 "PCM stream %d for codec #%d\n",
3609 continue; /* no fatal error */
3617 * snd_hda_build_pcms - build PCM information
3620 * Create PCM information for each codec included in the bus.
3622 * The build_pcms codec patch is requested to set up codec->num_pcms and
3623 * codec->pcm_info properly. The array is referred by the top-level driver
3624 * to create its PCM instances.
3625 * The allocated codec->pcm_info should be released in codec->patch_ops.free
3628 * At least, substreams, channels_min and channels_max must be filled for
3629 * each stream. substreams = 0 indicates that the stream doesn't exist.
3630 * When rates and/or formats are zero, the supported values are queried
3631 * from the given nid. The nid is used also by the default ops.prepare
3632 * and ops.cleanup callbacks.
3634 * The driver needs to call ops.open in its open callback. Similarly,
3635 * ops.close is supposed to be called in the close callback.
3636 * ops.prepare should be called in the prepare or hw_params callback
3637 * with the proper parameters for set up.
3638 * ops.cleanup should be called in hw_free for clean up of streams.
3640 * This function returns 0 if successfull, or a negative error code.
3642 int __devinit snd_hda_build_pcms(struct hda_bus *bus)
3644 struct hda_codec *codec;
3646 list_for_each_entry(codec, &bus->codec_list, list) {
3647 int err = snd_hda_codec_build_pcms(codec);
3653 EXPORT_SYMBOL_HDA(snd_hda_build_pcms);
3656 * snd_hda_check_board_config - compare the current codec with the config table
3657 * @codec: the HDA codec
3658 * @num_configs: number of config enums
3659 * @models: array of model name strings
3660 * @tbl: configuration table, terminated by null entries
3662 * Compares the modelname or PCI subsystem id of the current codec with the
3663 * given configuration table. If a matching entry is found, returns its
3664 * config value (supposed to be 0 or positive).
3666 * If no entries are matching, the function returns a negative value.
3668 int snd_hda_check_board_config(struct hda_codec *codec,
3669 int num_configs, const char **models,
3670 const struct snd_pci_quirk *tbl)
3672 if (codec->modelname && models) {
3674 for (i = 0; i < num_configs; i++) {
3676 !strcmp(codec->modelname, models[i])) {
3677 snd_printd(KERN_INFO "hda_codec: model '%s' is "
3678 "selected\n", models[i]);
3684 if (!codec->bus->pci || !tbl)
3687 tbl = snd_pci_quirk_lookup(codec->bus->pci, tbl);
3690 if (tbl->value >= 0 && tbl->value < num_configs) {
3691 #ifdef CONFIG_SND_DEBUG_VERBOSE
3693 const char *model = NULL;
3695 model = models[tbl->value];
3697 sprintf(tmp, "#%d", tbl->value);
3700 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
3701 "for config %x:%x (%s)\n",
3702 model, tbl->subvendor, tbl->subdevice,
3703 (tbl->name ? tbl->name : "Unknown device"));
3709 EXPORT_SYMBOL_HDA(snd_hda_check_board_config);
3712 * snd_hda_check_board_codec_sid_config - compare the current codec
3713 subsystem ID with the
3716 This is important for Gateway notebooks with SB450 HDA Audio
3717 where the vendor ID of the PCI device is:
3718 ATI Technologies Inc SB450 HDA Audio [1002:437b]
3719 and the vendor/subvendor are found only at the codec.
3721 * @codec: the HDA codec
3722 * @num_configs: number of config enums
3723 * @models: array of model name strings
3724 * @tbl: configuration table, terminated by null entries
3726 * Compares the modelname or PCI subsystem id of the current codec with the
3727 * given configuration table. If a matching entry is found, returns its
3728 * config value (supposed to be 0 or positive).
3730 * If no entries are matching, the function returns a negative value.
3732 int snd_hda_check_board_codec_sid_config(struct hda_codec *codec,
3733 int num_configs, const char **models,
3734 const struct snd_pci_quirk *tbl)
3736 const struct snd_pci_quirk *q;
3738 /* Search for codec ID */
3739 for (q = tbl; q->subvendor; q++) {
3740 unsigned long vendorid = (q->subdevice) | (q->subvendor << 16);
3742 if (vendorid == codec->subsystem_id)
3751 if (tbl->value >= 0 && tbl->value < num_configs) {
3752 #ifdef CONFIG_SND_DEBUG_VERBOSE
3754 const char *model = NULL;
3756 model = models[tbl->value];
3758 sprintf(tmp, "#%d", tbl->value);
3761 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
3762 "for config %x:%x (%s)\n",
3763 model, tbl->subvendor, tbl->subdevice,
3764 (tbl->name ? tbl->name : "Unknown device"));
3770 EXPORT_SYMBOL_HDA(snd_hda_check_board_codec_sid_config);
3773 * snd_hda_add_new_ctls - create controls from the array
3774 * @codec: the HDA codec
3775 * @knew: the array of struct snd_kcontrol_new
3777 * This helper function creates and add new controls in the given array.
3778 * The array must be terminated with an empty entry as terminator.
3780 * Returns 0 if successful, or a negative error code.
3782 int snd_hda_add_new_ctls(struct hda_codec *codec, struct snd_kcontrol_new *knew)
3786 for (; knew->name; knew++) {
3787 struct snd_kcontrol *kctl;
3788 if (knew->iface == -1) /* skip this codec private value */
3790 kctl = snd_ctl_new1(knew, codec);
3793 err = snd_hda_ctl_add(codec, 0, kctl);
3797 kctl = snd_ctl_new1(knew, codec);
3800 kctl->id.device = codec->addr;
3801 err = snd_hda_ctl_add(codec, 0, kctl);
3808 EXPORT_SYMBOL_HDA(snd_hda_add_new_ctls);
3810 #ifdef CONFIG_SND_HDA_POWER_SAVE
3811 static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
3812 unsigned int power_state);
3814 static void hda_power_work(struct work_struct *work)
3816 struct hda_codec *codec =
3817 container_of(work, struct hda_codec, power_work.work);
3818 struct hda_bus *bus = codec->bus;
3820 if (!codec->power_on || codec->power_count) {
3821 codec->power_transition = 0;
3825 hda_call_codec_suspend(codec);
3826 if (bus->ops.pm_notify)
3827 bus->ops.pm_notify(bus);
3830 static void hda_keep_power_on(struct hda_codec *codec)
3832 codec->power_count++;
3833 codec->power_on = 1;
3834 codec->power_jiffies = jiffies;
3837 /* update the power on/off account with the current jiffies */
3838 void snd_hda_update_power_acct(struct hda_codec *codec)
3840 unsigned long delta = jiffies - codec->power_jiffies;
3841 if (codec->power_on)
3842 codec->power_on_acct += delta;
3844 codec->power_off_acct += delta;
3845 codec->power_jiffies += delta;
3849 * snd_hda_power_up - Power-up the codec
3850 * @codec: HD-audio codec
3852 * Increment the power-up counter and power up the hardware really when
3853 * not turned on yet.
3855 void snd_hda_power_up(struct hda_codec *codec)
3857 struct hda_bus *bus = codec->bus;
3859 codec->power_count++;
3860 if (codec->power_on || codec->power_transition)
3863 snd_hda_update_power_acct(codec);
3864 codec->power_on = 1;
3865 codec->power_jiffies = jiffies;
3866 if (bus->ops.pm_notify)
3867 bus->ops.pm_notify(bus);
3868 hda_call_codec_resume(codec);
3869 cancel_delayed_work(&codec->power_work);
3870 codec->power_transition = 0;
3872 EXPORT_SYMBOL_HDA(snd_hda_power_up);
3874 #define power_save(codec) \
3875 ((codec)->bus->power_save ? *(codec)->bus->power_save : 0)
3878 * snd_hda_power_down - Power-down the codec
3879 * @codec: HD-audio codec
3881 * Decrement the power-up counter and schedules the power-off work if
3882 * the counter rearches to zero.
3884 void snd_hda_power_down(struct hda_codec *codec)
3886 --codec->power_count;
3887 if (!codec->power_on || codec->power_count || codec->power_transition)
3889 if (power_save(codec)) {
3890 codec->power_transition = 1; /* avoid reentrance */
3891 queue_delayed_work(codec->bus->workq, &codec->power_work,
3892 msecs_to_jiffies(power_save(codec) * 1000));
3895 EXPORT_SYMBOL_HDA(snd_hda_power_down);
3898 * snd_hda_check_amp_list_power - Check the amp list and update the power
3899 * @codec: HD-audio codec
3900 * @check: the object containing an AMP list and the status
3901 * @nid: NID to check / update
3903 * Check whether the given NID is in the amp list. If it's in the list,
3904 * check the current AMP status, and update the the power-status according
3905 * to the mute status.
3907 * This function is supposed to be set or called from the check_power_status
3910 int snd_hda_check_amp_list_power(struct hda_codec *codec,
3911 struct hda_loopback_check *check,
3914 struct hda_amp_list *p;
3917 if (!check->amplist)
3919 for (p = check->amplist; p->nid; p++) {
3924 return 0; /* nothing changed */
3926 for (p = check->amplist; p->nid; p++) {
3927 for (ch = 0; ch < 2; ch++) {
3928 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
3930 if (!(v & HDA_AMP_MUTE) && v > 0) {
3931 if (!check->power_on) {
3932 check->power_on = 1;
3933 snd_hda_power_up(codec);
3939 if (check->power_on) {
3940 check->power_on = 0;
3941 snd_hda_power_down(codec);
3945 EXPORT_SYMBOL_HDA(snd_hda_check_amp_list_power);
3949 * Channel mode helper
3953 * snd_hda_ch_mode_info - Info callback helper for the channel mode enum
3955 int snd_hda_ch_mode_info(struct hda_codec *codec,
3956 struct snd_ctl_elem_info *uinfo,
3957 const struct hda_channel_mode *chmode,
3960 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
3962 uinfo->value.enumerated.items = num_chmodes;
3963 if (uinfo->value.enumerated.item >= num_chmodes)
3964 uinfo->value.enumerated.item = num_chmodes - 1;
3965 sprintf(uinfo->value.enumerated.name, "%dch",
3966 chmode[uinfo->value.enumerated.item].channels);
3969 EXPORT_SYMBOL_HDA(snd_hda_ch_mode_info);
3972 * snd_hda_ch_mode_get - Get callback helper for the channel mode enum
3974 int snd_hda_ch_mode_get(struct hda_codec *codec,
3975 struct snd_ctl_elem_value *ucontrol,
3976 const struct hda_channel_mode *chmode,
3982 for (i = 0; i < num_chmodes; i++) {
3983 if (max_channels == chmode[i].channels) {
3984 ucontrol->value.enumerated.item[0] = i;
3990 EXPORT_SYMBOL_HDA(snd_hda_ch_mode_get);
3993 * snd_hda_ch_mode_put - Put callback helper for the channel mode enum
3995 int snd_hda_ch_mode_put(struct hda_codec *codec,
3996 struct snd_ctl_elem_value *ucontrol,
3997 const struct hda_channel_mode *chmode,
4003 mode = ucontrol->value.enumerated.item[0];
4004 if (mode >= num_chmodes)
4006 if (*max_channelsp == chmode[mode].channels)
4008 /* change the current channel setting */
4009 *max_channelsp = chmode[mode].channels;
4010 if (chmode[mode].sequence)
4011 snd_hda_sequence_write_cache(codec, chmode[mode].sequence);
4014 EXPORT_SYMBOL_HDA(snd_hda_ch_mode_put);
4021 * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum
4023 int snd_hda_input_mux_info(const struct hda_input_mux *imux,
4024 struct snd_ctl_elem_info *uinfo)
4028 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4030 uinfo->value.enumerated.items = imux->num_items;
4031 if (!imux->num_items)
4033 index = uinfo->value.enumerated.item;
4034 if (index >= imux->num_items)
4035 index = imux->num_items - 1;
4036 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
4039 EXPORT_SYMBOL_HDA(snd_hda_input_mux_info);
4042 * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum
4044 int snd_hda_input_mux_put(struct hda_codec *codec,
4045 const struct hda_input_mux *imux,
4046 struct snd_ctl_elem_value *ucontrol,
4048 unsigned int *cur_val)
4052 if (!imux->num_items)
4054 idx = ucontrol->value.enumerated.item[0];
4055 if (idx >= imux->num_items)
4056 idx = imux->num_items - 1;
4057 if (*cur_val == idx)
4059 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
4060 imux->items[idx].index);
4064 EXPORT_SYMBOL_HDA(snd_hda_input_mux_put);
4068 * Multi-channel / digital-out PCM helper functions
4071 /* setup SPDIF output stream */
4072 static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
4073 unsigned int stream_tag, unsigned int format)
4075 /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */
4076 if (codec->spdif_status_reset && (codec->spdif_ctls & AC_DIG1_ENABLE))
4077 set_dig_out_convert(codec, nid,
4078 codec->spdif_ctls & ~AC_DIG1_ENABLE & 0xff,
4080 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
4081 if (codec->slave_dig_outs) {
4083 for (d = codec->slave_dig_outs; *d; d++)
4084 snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
4087 /* turn on again (if needed) */
4088 if (codec->spdif_status_reset && (codec->spdif_ctls & AC_DIG1_ENABLE))
4089 set_dig_out_convert(codec, nid,
4090 codec->spdif_ctls & 0xff, -1);
4093 static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
4095 snd_hda_codec_cleanup_stream(codec, nid);
4096 if (codec->slave_dig_outs) {
4098 for (d = codec->slave_dig_outs; *d; d++)
4099 snd_hda_codec_cleanup_stream(codec, *d);
4104 * snd_hda_bus_reboot_notify - call the reboot notifier of each codec
4105 * @bus: HD-audio bus
4107 void snd_hda_bus_reboot_notify(struct hda_bus *bus)
4109 struct hda_codec *codec;
4113 list_for_each_entry(codec, &bus->codec_list, list) {
4114 #ifdef CONFIG_SND_HDA_POWER_SAVE
4115 if (!codec->power_on)
4118 if (codec->patch_ops.reboot_notify)
4119 codec->patch_ops.reboot_notify(codec);
4122 EXPORT_SYMBOL_HDA(snd_hda_bus_reboot_notify);
4125 * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode
4127 int snd_hda_multi_out_dig_open(struct hda_codec *codec,
4128 struct hda_multi_out *mout)
4130 mutex_lock(&codec->spdif_mutex);
4131 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
4132 /* already opened as analog dup; reset it once */
4133 cleanup_dig_out_stream(codec, mout->dig_out_nid);
4134 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
4135 mutex_unlock(&codec->spdif_mutex);
4138 EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_open);
4141 * snd_hda_multi_out_dig_prepare - prepare the digital out stream
4143 int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
4144 struct hda_multi_out *mout,
4145 unsigned int stream_tag,
4146 unsigned int format,
4147 struct snd_pcm_substream *substream)
4149 mutex_lock(&codec->spdif_mutex);
4150 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
4151 mutex_unlock(&codec->spdif_mutex);
4154 EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_prepare);
4157 * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream
4159 int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec,
4160 struct hda_multi_out *mout)
4162 mutex_lock(&codec->spdif_mutex);
4163 cleanup_dig_out_stream(codec, mout->dig_out_nid);
4164 mutex_unlock(&codec->spdif_mutex);
4167 EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_cleanup);
4170 * snd_hda_multi_out_dig_close - release the digital out stream
4172 int snd_hda_multi_out_dig_close(struct hda_codec *codec,
4173 struct hda_multi_out *mout)
4175 mutex_lock(&codec->spdif_mutex);
4176 mout->dig_out_used = 0;
4177 mutex_unlock(&codec->spdif_mutex);
4180 EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_close);
4183 * snd_hda_multi_out_analog_open - open analog outputs
4185 * Open analog outputs and set up the hw-constraints.
4186 * If the digital outputs can be opened as slave, open the digital
4189 int snd_hda_multi_out_analog_open(struct hda_codec *codec,
4190 struct hda_multi_out *mout,
4191 struct snd_pcm_substream *substream,
4192 struct hda_pcm_stream *hinfo)
4194 struct snd_pcm_runtime *runtime = substream->runtime;
4195 runtime->hw.channels_max = mout->max_channels;
4196 if (mout->dig_out_nid) {
4197 if (!mout->analog_rates) {
4198 mout->analog_rates = hinfo->rates;
4199 mout->analog_formats = hinfo->formats;
4200 mout->analog_maxbps = hinfo->maxbps;
4202 runtime->hw.rates = mout->analog_rates;
4203 runtime->hw.formats = mout->analog_formats;
4204 hinfo->maxbps = mout->analog_maxbps;
4206 if (!mout->spdif_rates) {
4207 snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
4209 &mout->spdif_formats,
4210 &mout->spdif_maxbps);
4212 mutex_lock(&codec->spdif_mutex);
4213 if (mout->share_spdif) {
4214 if ((runtime->hw.rates & mout->spdif_rates) &&
4215 (runtime->hw.formats & mout->spdif_formats)) {
4216 runtime->hw.rates &= mout->spdif_rates;
4217 runtime->hw.formats &= mout->spdif_formats;
4218 if (mout->spdif_maxbps < hinfo->maxbps)
4219 hinfo->maxbps = mout->spdif_maxbps;
4221 mout->share_spdif = 0;
4222 /* FIXME: need notify? */
4225 mutex_unlock(&codec->spdif_mutex);
4227 return snd_pcm_hw_constraint_step(substream->runtime, 0,
4228 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
4230 EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_open);
4233 * snd_hda_multi_out_analog_prepare - Preapre the analog outputs.
4235 * Set up the i/o for analog out.
4236 * When the digital out is available, copy the front out to digital out, too.
4238 int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
4239 struct hda_multi_out *mout,
4240 unsigned int stream_tag,
4241 unsigned int format,
4242 struct snd_pcm_substream *substream)
4244 hda_nid_t *nids = mout->dac_nids;
4245 int chs = substream->runtime->channels;
4248 mutex_lock(&codec->spdif_mutex);
4249 if (mout->dig_out_nid && mout->share_spdif &&
4250 mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
4252 snd_hda_is_supported_format(codec, mout->dig_out_nid,
4254 !(codec->spdif_status & IEC958_AES0_NONAUDIO)) {
4255 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
4256 setup_dig_out_stream(codec, mout->dig_out_nid,
4257 stream_tag, format);
4259 mout->dig_out_used = 0;
4260 cleanup_dig_out_stream(codec, mout->dig_out_nid);
4263 mutex_unlock(&codec->spdif_mutex);
4266 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
4268 if (!mout->no_share_stream &&
4269 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
4270 /* headphone out will just decode front left/right (stereo) */
4271 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
4273 /* extra outputs copied from front */
4274 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
4275 if (!mout->no_share_stream && mout->extra_out_nid[i])
4276 snd_hda_codec_setup_stream(codec,
4277 mout->extra_out_nid[i],
4278 stream_tag, 0, format);
4281 for (i = 1; i < mout->num_dacs; i++) {
4282 if (chs >= (i + 1) * 2) /* independent out */
4283 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
4285 else if (!mout->no_share_stream) /* copy front */
4286 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
4291 EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_prepare);
4294 * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out
4296 int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
4297 struct hda_multi_out *mout)
4299 hda_nid_t *nids = mout->dac_nids;
4302 for (i = 0; i < mout->num_dacs; i++)
4303 snd_hda_codec_cleanup_stream(codec, nids[i]);
4305 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
4306 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
4307 if (mout->extra_out_nid[i])
4308 snd_hda_codec_cleanup_stream(codec,
4309 mout->extra_out_nid[i]);
4310 mutex_lock(&codec->spdif_mutex);
4311 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
4312 cleanup_dig_out_stream(codec, mout->dig_out_nid);
4313 mout->dig_out_used = 0;
4315 mutex_unlock(&codec->spdif_mutex);
4318 EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_cleanup);
4321 * Helper for automatic pin configuration
4324 static int is_in_nid_list(hda_nid_t nid, hda_nid_t *list)
4326 for (; *list; list++)
4334 * Sort an associated group of pins according to their sequence numbers.
4336 static void sort_pins_by_sequence(hda_nid_t *pins, short *sequences,
4343 for (i = 0; i < num_pins; i++) {
4344 for (j = i + 1; j < num_pins; j++) {
4345 if (sequences[i] > sequences[j]) {
4347 sequences[i] = sequences[j];
4359 * Parse all pin widgets and store the useful pin nids to cfg
4361 * The number of line-outs or any primary output is stored in line_outs,
4362 * and the corresponding output pins are assigned to line_out_pins[],
4363 * in the order of front, rear, CLFE, side, ...
4365 * If more extra outputs (speaker and headphone) are found, the pins are
4366 * assisnged to hp_pins[] and speaker_pins[], respectively. If no line-out jack
4367 * is detected, one of speaker of HP pins is assigned as the primary
4368 * output, i.e. to line_out_pins[0]. So, line_outs is always positive
4369 * if any analog output exists.
4371 * The analog input pins are assigned to input_pins array.
4372 * The digital input/output pins are assigned to dig_in_pin and dig_out_pin,
4375 int snd_hda_parse_pin_def_config(struct hda_codec *codec,
4376 struct auto_pin_cfg *cfg,
4377 hda_nid_t *ignore_nids)
4379 hda_nid_t nid, end_nid;
4380 short seq, assoc_line_out, assoc_speaker;
4381 short sequences_line_out[ARRAY_SIZE(cfg->line_out_pins)];
4382 short sequences_speaker[ARRAY_SIZE(cfg->speaker_pins)];
4383 short sequences_hp[ARRAY_SIZE(cfg->hp_pins)];
4385 memset(cfg, 0, sizeof(*cfg));
4387 memset(sequences_line_out, 0, sizeof(sequences_line_out));
4388 memset(sequences_speaker, 0, sizeof(sequences_speaker));
4389 memset(sequences_hp, 0, sizeof(sequences_hp));
4390 assoc_line_out = assoc_speaker = 0;
4392 end_nid = codec->start_nid + codec->num_nodes;
4393 for (nid = codec->start_nid; nid < end_nid; nid++) {
4394 unsigned int wid_caps = get_wcaps(codec, nid);
4395 unsigned int wid_type = get_wcaps_type(wid_caps);
4396 unsigned int def_conf;
4399 /* read all default configuration for pin complex */
4400 if (wid_type != AC_WID_PIN)
4402 /* ignore the given nids (e.g. pc-beep returns error) */
4403 if (ignore_nids && is_in_nid_list(nid, ignore_nids))
4406 def_conf = snd_hda_codec_get_pincfg(codec, nid);
4407 if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE)
4409 loc = get_defcfg_location(def_conf);
4410 switch (get_defcfg_device(def_conf)) {
4411 case AC_JACK_LINE_OUT:
4412 seq = get_defcfg_sequence(def_conf);
4413 assoc = get_defcfg_association(def_conf);
4415 if (!(wid_caps & AC_WCAP_STEREO))
4416 if (!cfg->mono_out_pin)
4417 cfg->mono_out_pin = nid;
4420 if (!assoc_line_out)
4421 assoc_line_out = assoc;
4422 else if (assoc_line_out != assoc)
4424 if (cfg->line_outs >= ARRAY_SIZE(cfg->line_out_pins))
4426 cfg->line_out_pins[cfg->line_outs] = nid;
4427 sequences_line_out[cfg->line_outs] = seq;
4430 case AC_JACK_SPEAKER:
4431 seq = get_defcfg_sequence(def_conf);
4432 assoc = get_defcfg_association(def_conf);
4436 assoc_speaker = assoc;
4437 else if (assoc_speaker != assoc)
4439 if (cfg->speaker_outs >= ARRAY_SIZE(cfg->speaker_pins))
4441 cfg->speaker_pins[cfg->speaker_outs] = nid;
4442 sequences_speaker[cfg->speaker_outs] = seq;
4443 cfg->speaker_outs++;
4445 case AC_JACK_HP_OUT:
4446 seq = get_defcfg_sequence(def_conf);
4447 assoc = get_defcfg_association(def_conf);
4448 if (cfg->hp_outs >= ARRAY_SIZE(cfg->hp_pins))
4450 cfg->hp_pins[cfg->hp_outs] = nid;
4451 sequences_hp[cfg->hp_outs] = (assoc << 4) | seq;
4454 case AC_JACK_MIC_IN: {
4456 if (loc == AC_JACK_LOC_FRONT ||
4457 (loc & 0x30) == AC_JACK_LOC_INTERNAL) {
4458 preferred = AUTO_PIN_FRONT_MIC;
4461 preferred = AUTO_PIN_MIC;
4462 alt = AUTO_PIN_FRONT_MIC;
4464 if (!cfg->input_pins[preferred])
4465 cfg->input_pins[preferred] = nid;
4466 else if (!cfg->input_pins[alt])
4467 cfg->input_pins[alt] = nid;
4470 case AC_JACK_LINE_IN:
4471 if (loc == AC_JACK_LOC_FRONT)
4472 cfg->input_pins[AUTO_PIN_FRONT_LINE] = nid;
4474 cfg->input_pins[AUTO_PIN_LINE] = nid;
4477 cfg->input_pins[AUTO_PIN_CD] = nid;
4480 cfg->input_pins[AUTO_PIN_AUX] = nid;
4482 case AC_JACK_SPDIF_OUT:
4483 case AC_JACK_DIG_OTHER_OUT:
4484 if (cfg->dig_outs >= ARRAY_SIZE(cfg->dig_out_pins))
4486 cfg->dig_out_pins[cfg->dig_outs] = nid;
4487 cfg->dig_out_type[cfg->dig_outs] =
4488 (loc == AC_JACK_LOC_HDMI) ?
4489 HDA_PCM_TYPE_HDMI : HDA_PCM_TYPE_SPDIF;
4492 case AC_JACK_SPDIF_IN:
4493 case AC_JACK_DIG_OTHER_IN:
4494 cfg->dig_in_pin = nid;
4495 if (loc == AC_JACK_LOC_HDMI)
4496 cfg->dig_in_type = HDA_PCM_TYPE_HDMI;
4498 cfg->dig_in_type = HDA_PCM_TYPE_SPDIF;
4504 * If no line-out is defined but multiple HPs are found,
4505 * some of them might be the real line-outs.
4507 if (!cfg->line_outs && cfg->hp_outs > 1) {
4509 while (i < cfg->hp_outs) {
4510 /* The real HPs should have the sequence 0x0f */
4511 if ((sequences_hp[i] & 0x0f) == 0x0f) {
4515 /* Move it to the line-out table */
4516 cfg->line_out_pins[cfg->line_outs] = cfg->hp_pins[i];
4517 sequences_line_out[cfg->line_outs] = sequences_hp[i];
4520 memmove(cfg->hp_pins + i, cfg->hp_pins + i + 1,
4521 sizeof(cfg->hp_pins[0]) * (cfg->hp_outs - i));
4522 memmove(sequences_hp + i - 1, sequences_hp + i,
4523 sizeof(sequences_hp[0]) * (cfg->hp_outs - i));
4527 /* sort by sequence */
4528 sort_pins_by_sequence(cfg->line_out_pins, sequences_line_out,
4530 sort_pins_by_sequence(cfg->speaker_pins, sequences_speaker,
4532 sort_pins_by_sequence(cfg->hp_pins, sequences_hp,
4535 /* if we have only one mic, make it AUTO_PIN_MIC */
4536 if (!cfg->input_pins[AUTO_PIN_MIC] &&
4537 cfg->input_pins[AUTO_PIN_FRONT_MIC]) {
4538 cfg->input_pins[AUTO_PIN_MIC] =
4539 cfg->input_pins[AUTO_PIN_FRONT_MIC];
4540 cfg->input_pins[AUTO_PIN_FRONT_MIC] = 0;
4542 /* ditto for line-in */
4543 if (!cfg->input_pins[AUTO_PIN_LINE] &&
4544 cfg->input_pins[AUTO_PIN_FRONT_LINE]) {
4545 cfg->input_pins[AUTO_PIN_LINE] =
4546 cfg->input_pins[AUTO_PIN_FRONT_LINE];
4547 cfg->input_pins[AUTO_PIN_FRONT_LINE] = 0;
4551 * FIX-UP: if no line-outs are detected, try to use speaker or HP pin
4552 * as a primary output
4554 if (!cfg->line_outs) {
4555 if (cfg->speaker_outs) {
4556 cfg->line_outs = cfg->speaker_outs;
4557 memcpy(cfg->line_out_pins, cfg->speaker_pins,
4558 sizeof(cfg->speaker_pins));
4559 cfg->speaker_outs = 0;
4560 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
4561 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
4562 } else if (cfg->hp_outs) {
4563 cfg->line_outs = cfg->hp_outs;
4564 memcpy(cfg->line_out_pins, cfg->hp_pins,
4565 sizeof(cfg->hp_pins));
4567 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
4568 cfg->line_out_type = AUTO_PIN_HP_OUT;
4572 /* Reorder the surround channels
4573 * ALSA sequence is front/surr/clfe/side
4575 * 4-ch: front/surr => OK as it is
4576 * 6-ch: front/clfe/surr
4577 * 8-ch: front/clfe/rear/side|fc
4579 switch (cfg->line_outs) {
4582 nid = cfg->line_out_pins[1];
4583 cfg->line_out_pins[1] = cfg->line_out_pins[2];
4584 cfg->line_out_pins[2] = nid;
4589 * debug prints of the parsed results
4591 snd_printd("autoconfig: line_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
4592 cfg->line_outs, cfg->line_out_pins[0], cfg->line_out_pins[1],
4593 cfg->line_out_pins[2], cfg->line_out_pins[3],
4594 cfg->line_out_pins[4]);
4595 snd_printd(" speaker_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
4596 cfg->speaker_outs, cfg->speaker_pins[0],
4597 cfg->speaker_pins[1], cfg->speaker_pins[2],
4598 cfg->speaker_pins[3], cfg->speaker_pins[4]);
4599 snd_printd(" hp_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
4600 cfg->hp_outs, cfg->hp_pins[0],
4601 cfg->hp_pins[1], cfg->hp_pins[2],
4602 cfg->hp_pins[3], cfg->hp_pins[4]);
4603 snd_printd(" mono: mono_out=0x%x\n", cfg->mono_out_pin);
4605 snd_printd(" dig-out=0x%x/0x%x\n",
4606 cfg->dig_out_pins[0], cfg->dig_out_pins[1]);
4607 snd_printd(" inputs: mic=0x%x, fmic=0x%x, line=0x%x, fline=0x%x,"
4608 " cd=0x%x, aux=0x%x\n",
4609 cfg->input_pins[AUTO_PIN_MIC],
4610 cfg->input_pins[AUTO_PIN_FRONT_MIC],
4611 cfg->input_pins[AUTO_PIN_LINE],
4612 cfg->input_pins[AUTO_PIN_FRONT_LINE],
4613 cfg->input_pins[AUTO_PIN_CD],
4614 cfg->input_pins[AUTO_PIN_AUX]);
4615 if (cfg->dig_in_pin)
4616 snd_printd(" dig-in=0x%x\n", cfg->dig_in_pin);
4620 EXPORT_SYMBOL_HDA(snd_hda_parse_pin_def_config);
4622 /* labels for input pins */
4623 const char *auto_pin_cfg_labels[AUTO_PIN_LAST] = {
4624 "Mic", "Front Mic", "Line", "Front Line", "CD", "Aux"
4626 EXPORT_SYMBOL_HDA(auto_pin_cfg_labels);
4635 * snd_hda_suspend - suspend the codecs
4638 * Returns 0 if successful.
4640 int snd_hda_suspend(struct hda_bus *bus)
4642 struct hda_codec *codec;
4644 list_for_each_entry(codec, &bus->codec_list, list) {
4645 #ifdef CONFIG_SND_HDA_POWER_SAVE
4646 if (!codec->power_on)
4649 hda_call_codec_suspend(codec);
4653 EXPORT_SYMBOL_HDA(snd_hda_suspend);
4656 * snd_hda_resume - resume the codecs
4659 * Returns 0 if successful.
4661 * This fucntion is defined only when POWER_SAVE isn't set.
4662 * In the power-save mode, the codec is resumed dynamically.
4664 int snd_hda_resume(struct hda_bus *bus)
4666 struct hda_codec *codec;
4668 list_for_each_entry(codec, &bus->codec_list, list) {
4669 if (snd_hda_codec_needs_resume(codec))
4670 hda_call_codec_resume(codec);
4674 EXPORT_SYMBOL_HDA(snd_hda_resume);
4675 #endif /* CONFIG_PM */
4682 * snd_array_new - get a new element from the given array
4683 * @array: the array object
4685 * Get a new element from the given array. If it exceeds the
4686 * pre-allocated array size, re-allocate the array.
4688 * Returns NULL if allocation failed.
4690 void *snd_array_new(struct snd_array *array)
4692 if (array->used >= array->alloced) {
4693 int num = array->alloced + array->alloc_align;
4695 if (snd_BUG_ON(num >= 4096))
4697 nlist = kcalloc(num + 1, array->elem_size, GFP_KERNEL);
4701 memcpy(nlist, array->list,
4702 array->elem_size * array->alloced);
4705 array->list = nlist;
4706 array->alloced = num;
4708 return snd_array_elem(array, array->used++);
4710 EXPORT_SYMBOL_HDA(snd_array_new);
4713 * snd_array_free - free the given array elements
4714 * @array: the array object
4716 void snd_array_free(struct snd_array *array)
4723 EXPORT_SYMBOL_HDA(snd_array_free);
4726 * snd_print_pcm_rates - Print the supported PCM rates to the string buffer
4727 * @pcm: PCM caps bits
4728 * @buf: the string buffer to write
4729 * @buflen: the max buffer length
4731 * used by hda_proc.c and hda_eld.c
4733 void snd_print_pcm_rates(int pcm, char *buf, int buflen)
4735 static unsigned int rates[] = {
4736 8000, 11025, 16000, 22050, 32000, 44100, 48000, 88200,
4737 96000, 176400, 192000, 384000
4741 for (i = 0, j = 0; i < ARRAY_SIZE(rates); i++)
4743 j += snprintf(buf + j, buflen - j, " %d", rates[i]);
4745 buf[j] = '\0'; /* necessary when j == 0 */
4747 EXPORT_SYMBOL_HDA(snd_print_pcm_rates);
4750 * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer
4751 * @pcm: PCM caps bits
4752 * @buf: the string buffer to write
4753 * @buflen: the max buffer length
4755 * used by hda_proc.c and hda_eld.c
4757 void snd_print_pcm_bits(int pcm, char *buf, int buflen)
4759 static unsigned int bits[] = { 8, 16, 20, 24, 32 };
4762 for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
4763 if (pcm & (AC_SUPPCM_BITS_8 << i))
4764 j += snprintf(buf + j, buflen - j, " %d", bits[i]);
4766 buf[j] = '\0'; /* necessary when j == 0 */
4768 EXPORT_SYMBOL_HDA(snd_print_pcm_bits);
4770 MODULE_DESCRIPTION("HDA codec core");
4771 MODULE_LICENSE("GPL");