ALSA: hda - Add Nvidia vendor id string
[linux-2.6/kvm.git] / sound / pci / hda / hda_codec.c
blobf6832e16068407ac855be219338592065b83524c
1 /*
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"
33 #include <sound/hda_hwdep.h>
36 * vendor / preset table
39 struct hda_vendor_id {
40 unsigned int id;
41 const char *name;
44 /* codec vendor labels */
45 static struct hda_vendor_id hda_vendor_ids[] = {
46 { 0x1002, "ATI" },
47 { 0x1057, "Motorola" },
48 { 0x1095, "Silicon Image" },
49 { 0x10de, "Nvidia" },
50 { 0x10ec, "Realtek" },
51 { 0x1106, "VIA" },
52 { 0x111d, "IDT" },
53 { 0x11c1, "LSI" },
54 { 0x11d4, "Analog Devices" },
55 { 0x13f6, "C-Media" },
56 { 0x14f1, "Conexant" },
57 { 0x17e8, "Chrontel" },
58 { 0x1854, "LG" },
59 { 0x1aec, "Wolfson Microelectronics" },
60 { 0x434d, "C-Media" },
61 { 0x8384, "SigmaTel" },
62 {} /* terminator */
65 static DEFINE_MUTEX(preset_mutex);
66 static LIST_HEAD(hda_preset_tables);
68 int snd_hda_add_codec_preset(struct hda_codec_preset_list *preset)
70 mutex_lock(&preset_mutex);
71 list_add_tail(&preset->list, &hda_preset_tables);
72 mutex_unlock(&preset_mutex);
73 return 0;
75 EXPORT_SYMBOL_HDA(snd_hda_add_codec_preset);
77 int snd_hda_delete_codec_preset(struct hda_codec_preset_list *preset)
79 mutex_lock(&preset_mutex);
80 list_del(&preset->list);
81 mutex_unlock(&preset_mutex);
82 return 0;
84 EXPORT_SYMBOL_HDA(snd_hda_delete_codec_preset);
86 #ifdef CONFIG_SND_HDA_POWER_SAVE
87 static void hda_power_work(struct work_struct *work);
88 static void hda_keep_power_on(struct hda_codec *codec);
89 #else
90 static inline void hda_keep_power_on(struct hda_codec *codec) {}
91 #endif
93 const char *snd_hda_get_jack_location(u32 cfg)
95 static char *bases[7] = {
96 "N/A", "Rear", "Front", "Left", "Right", "Top", "Bottom",
98 static unsigned char specials_idx[] = {
99 0x07, 0x08,
100 0x17, 0x18, 0x19,
101 0x37, 0x38
103 static char *specials[] = {
104 "Rear Panel", "Drive Bar",
105 "Riser", "HDMI", "ATAPI",
106 "Mobile-In", "Mobile-Out"
108 int i;
109 cfg = (cfg & AC_DEFCFG_LOCATION) >> AC_DEFCFG_LOCATION_SHIFT;
110 if ((cfg & 0x0f) < 7)
111 return bases[cfg & 0x0f];
112 for (i = 0; i < ARRAY_SIZE(specials_idx); i++) {
113 if (cfg == specials_idx[i])
114 return specials[i];
116 return "UNKNOWN";
118 EXPORT_SYMBOL_HDA(snd_hda_get_jack_location);
120 const char *snd_hda_get_jack_connectivity(u32 cfg)
122 static char *jack_locations[4] = { "Ext", "Int", "Sep", "Oth" };
124 return jack_locations[(cfg >> (AC_DEFCFG_LOCATION_SHIFT + 4)) & 3];
126 EXPORT_SYMBOL_HDA(snd_hda_get_jack_connectivity);
128 const char *snd_hda_get_jack_type(u32 cfg)
130 static char *jack_types[16] = {
131 "Line Out", "Speaker", "HP Out", "CD",
132 "SPDIF Out", "Digital Out", "Modem Line", "Modem Hand",
133 "Line In", "Aux", "Mic", "Telephony",
134 "SPDIF In", "Digitial In", "Reserved", "Other"
137 return jack_types[(cfg & AC_DEFCFG_DEVICE)
138 >> AC_DEFCFG_DEVICE_SHIFT];
140 EXPORT_SYMBOL_HDA(snd_hda_get_jack_type);
143 * Compose a 32bit command word to be sent to the HD-audio controller
145 static inline unsigned int
146 make_codec_cmd(struct hda_codec *codec, hda_nid_t nid, int direct,
147 unsigned int verb, unsigned int parm)
149 u32 val;
151 val = (u32)(codec->addr & 0x0f) << 28;
152 val |= (u32)direct << 27;
153 val |= (u32)nid << 20;
154 val |= verb << 8;
155 val |= parm;
156 return val;
160 * snd_hda_codec_read - send a command and get the response
161 * @codec: the HDA codec
162 * @nid: NID to send the command
163 * @direct: direct flag
164 * @verb: the verb to send
165 * @parm: the parameter for the verb
167 * Send a single command and read the corresponding response.
169 * Returns the obtained response value, or -1 for an error.
171 unsigned int snd_hda_codec_read(struct hda_codec *codec, hda_nid_t nid,
172 int direct,
173 unsigned int verb, unsigned int parm)
175 struct hda_bus *bus = codec->bus;
176 unsigned int res;
178 res = make_codec_cmd(codec, nid, direct, verb, parm);
179 snd_hda_power_up(codec);
180 mutex_lock(&bus->cmd_mutex);
181 if (!bus->ops.command(bus, res))
182 res = bus->ops.get_response(bus);
183 else
184 res = (unsigned int)-1;
185 mutex_unlock(&bus->cmd_mutex);
186 snd_hda_power_down(codec);
187 return res;
189 EXPORT_SYMBOL_HDA(snd_hda_codec_read);
192 * snd_hda_codec_write - send a single command without waiting for response
193 * @codec: the HDA codec
194 * @nid: NID to send the command
195 * @direct: direct flag
196 * @verb: the verb to send
197 * @parm: the parameter for the verb
199 * Send a single command without waiting for response.
201 * Returns 0 if successful, or a negative error code.
203 int snd_hda_codec_write(struct hda_codec *codec, hda_nid_t nid, int direct,
204 unsigned int verb, unsigned int parm)
206 struct hda_bus *bus = codec->bus;
207 unsigned int res;
208 int err;
210 res = make_codec_cmd(codec, nid, direct, verb, parm);
211 snd_hda_power_up(codec);
212 mutex_lock(&bus->cmd_mutex);
213 err = bus->ops.command(bus, res);
214 mutex_unlock(&bus->cmd_mutex);
215 snd_hda_power_down(codec);
216 return err;
218 EXPORT_SYMBOL_HDA(snd_hda_codec_write);
221 * snd_hda_sequence_write - sequence writes
222 * @codec: the HDA codec
223 * @seq: VERB array to send
225 * Send the commands sequentially from the given array.
226 * The array must be terminated with NID=0.
228 void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq)
230 for (; seq->nid; seq++)
231 snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param);
233 EXPORT_SYMBOL_HDA(snd_hda_sequence_write);
236 * snd_hda_get_sub_nodes - get the range of sub nodes
237 * @codec: the HDA codec
238 * @nid: NID to parse
239 * @start_id: the pointer to store the start NID
241 * Parse the NID and store the start NID of its sub-nodes.
242 * Returns the number of sub-nodes.
244 int snd_hda_get_sub_nodes(struct hda_codec *codec, hda_nid_t nid,
245 hda_nid_t *start_id)
247 unsigned int parm;
249 parm = snd_hda_param_read(codec, nid, AC_PAR_NODE_COUNT);
250 if (parm == -1)
251 return 0;
252 *start_id = (parm >> 16) & 0x7fff;
253 return (int)(parm & 0x7fff);
255 EXPORT_SYMBOL_HDA(snd_hda_get_sub_nodes);
258 * snd_hda_get_connections - get connection list
259 * @codec: the HDA codec
260 * @nid: NID to parse
261 * @conn_list: connection list array
262 * @max_conns: max. number of connections to store
264 * Parses the connection list of the given widget and stores the list
265 * of NIDs.
267 * Returns the number of connections, or a negative error code.
269 int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
270 hda_nid_t *conn_list, int max_conns)
272 unsigned int parm;
273 int i, conn_len, conns;
274 unsigned int shift, num_elems, mask;
275 hda_nid_t prev_nid;
277 if (snd_BUG_ON(!conn_list || max_conns <= 0))
278 return -EINVAL;
280 parm = snd_hda_param_read(codec, nid, AC_PAR_CONNLIST_LEN);
281 if (parm & AC_CLIST_LONG) {
282 /* long form */
283 shift = 16;
284 num_elems = 2;
285 } else {
286 /* short form */
287 shift = 8;
288 num_elems = 4;
290 conn_len = parm & AC_CLIST_LENGTH;
291 mask = (1 << (shift-1)) - 1;
293 if (!conn_len)
294 return 0; /* no connection */
296 if (conn_len == 1) {
297 /* single connection */
298 parm = snd_hda_codec_read(codec, nid, 0,
299 AC_VERB_GET_CONNECT_LIST, 0);
300 conn_list[0] = parm & mask;
301 return 1;
304 /* multi connection */
305 conns = 0;
306 prev_nid = 0;
307 for (i = 0; i < conn_len; i++) {
308 int range_val;
309 hda_nid_t val, n;
311 if (i % num_elems == 0)
312 parm = snd_hda_codec_read(codec, nid, 0,
313 AC_VERB_GET_CONNECT_LIST, i);
314 range_val = !!(parm & (1 << (shift-1))); /* ranges */
315 val = parm & mask;
316 parm >>= shift;
317 if (range_val) {
318 /* ranges between the previous and this one */
319 if (!prev_nid || prev_nid >= val) {
320 snd_printk(KERN_WARNING "hda_codec: "
321 "invalid dep_range_val %x:%x\n",
322 prev_nid, val);
323 continue;
325 for (n = prev_nid + 1; n <= val; n++) {
326 if (conns >= max_conns) {
327 snd_printk(KERN_ERR
328 "Too many connections\n");
329 return -EINVAL;
331 conn_list[conns++] = n;
333 } else {
334 if (conns >= max_conns) {
335 snd_printk(KERN_ERR "Too many connections\n");
336 return -EINVAL;
338 conn_list[conns++] = val;
340 prev_nid = val;
342 return conns;
344 EXPORT_SYMBOL_HDA(snd_hda_get_connections);
348 * snd_hda_queue_unsol_event - add an unsolicited event to queue
349 * @bus: the BUS
350 * @res: unsolicited event (lower 32bit of RIRB entry)
351 * @res_ex: codec addr and flags (upper 32bit or RIRB entry)
353 * Adds the given event to the queue. The events are processed in
354 * the workqueue asynchronously. Call this function in the interrupt
355 * hanlder when RIRB receives an unsolicited event.
357 * Returns 0 if successful, or a negative error code.
359 int snd_hda_queue_unsol_event(struct hda_bus *bus, u32 res, u32 res_ex)
361 struct hda_bus_unsolicited *unsol;
362 unsigned int wp;
364 unsol = bus->unsol;
365 if (!unsol)
366 return 0;
368 wp = (unsol->wp + 1) % HDA_UNSOL_QUEUE_SIZE;
369 unsol->wp = wp;
371 wp <<= 1;
372 unsol->queue[wp] = res;
373 unsol->queue[wp + 1] = res_ex;
375 schedule_work(&unsol->work);
377 return 0;
379 EXPORT_SYMBOL_HDA(snd_hda_queue_unsol_event);
382 * process queued unsolicited events
384 static void process_unsol_events(struct work_struct *work)
386 struct hda_bus_unsolicited *unsol =
387 container_of(work, struct hda_bus_unsolicited, work);
388 struct hda_bus *bus = unsol->bus;
389 struct hda_codec *codec;
390 unsigned int rp, caddr, res;
392 while (unsol->rp != unsol->wp) {
393 rp = (unsol->rp + 1) % HDA_UNSOL_QUEUE_SIZE;
394 unsol->rp = rp;
395 rp <<= 1;
396 res = unsol->queue[rp];
397 caddr = unsol->queue[rp + 1];
398 if (!(caddr & (1 << 4))) /* no unsolicited event? */
399 continue;
400 codec = bus->caddr_tbl[caddr & 0x0f];
401 if (codec && codec->patch_ops.unsol_event)
402 codec->patch_ops.unsol_event(codec, res);
407 * initialize unsolicited queue
409 static int init_unsol_queue(struct hda_bus *bus)
411 struct hda_bus_unsolicited *unsol;
413 if (bus->unsol) /* already initialized */
414 return 0;
416 unsol = kzalloc(sizeof(*unsol), GFP_KERNEL);
417 if (!unsol) {
418 snd_printk(KERN_ERR "hda_codec: "
419 "can't allocate unsolicited queue\n");
420 return -ENOMEM;
422 INIT_WORK(&unsol->work, process_unsol_events);
423 unsol->bus = bus;
424 bus->unsol = unsol;
425 return 0;
429 * destructor
431 static void snd_hda_codec_free(struct hda_codec *codec);
433 static int snd_hda_bus_free(struct hda_bus *bus)
435 struct hda_codec *codec, *n;
437 if (!bus)
438 return 0;
439 if (bus->unsol) {
440 flush_scheduled_work();
441 kfree(bus->unsol);
443 list_for_each_entry_safe(codec, n, &bus->codec_list, list) {
444 snd_hda_codec_free(codec);
446 if (bus->ops.private_free)
447 bus->ops.private_free(bus);
448 kfree(bus);
449 return 0;
452 static int snd_hda_bus_dev_free(struct snd_device *device)
454 struct hda_bus *bus = device->device_data;
455 bus->shutdown = 1;
456 return snd_hda_bus_free(bus);
459 #ifdef CONFIG_SND_HDA_HWDEP
460 static int snd_hda_bus_dev_register(struct snd_device *device)
462 struct hda_bus *bus = device->device_data;
463 struct hda_codec *codec;
464 list_for_each_entry(codec, &bus->codec_list, list) {
465 snd_hda_hwdep_add_sysfs(codec);
467 return 0;
469 #else
470 #define snd_hda_bus_dev_register NULL
471 #endif
474 * snd_hda_bus_new - create a HDA bus
475 * @card: the card entry
476 * @temp: the template for hda_bus information
477 * @busp: the pointer to store the created bus instance
479 * Returns 0 if successful, or a negative error code.
481 int /*__devinit*/ snd_hda_bus_new(struct snd_card *card,
482 const struct hda_bus_template *temp,
483 struct hda_bus **busp)
485 struct hda_bus *bus;
486 int err;
487 static struct snd_device_ops dev_ops = {
488 .dev_register = snd_hda_bus_dev_register,
489 .dev_free = snd_hda_bus_dev_free,
492 if (snd_BUG_ON(!temp))
493 return -EINVAL;
494 if (snd_BUG_ON(!temp->ops.command || !temp->ops.get_response))
495 return -EINVAL;
497 if (busp)
498 *busp = NULL;
500 bus = kzalloc(sizeof(*bus), GFP_KERNEL);
501 if (bus == NULL) {
502 snd_printk(KERN_ERR "can't allocate struct hda_bus\n");
503 return -ENOMEM;
506 bus->card = card;
507 bus->private_data = temp->private_data;
508 bus->pci = temp->pci;
509 bus->modelname = temp->modelname;
510 bus->power_save = temp->power_save;
511 bus->ops = temp->ops;
513 mutex_init(&bus->cmd_mutex);
514 INIT_LIST_HEAD(&bus->codec_list);
516 err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops);
517 if (err < 0) {
518 snd_hda_bus_free(bus);
519 return err;
521 if (busp)
522 *busp = bus;
523 return 0;
525 EXPORT_SYMBOL_HDA(snd_hda_bus_new);
527 #ifdef CONFIG_SND_HDA_GENERIC
528 #define is_generic_config(codec) \
529 (codec->modelname && !strcmp(codec->modelname, "generic"))
530 #else
531 #define is_generic_config(codec) 0
532 #endif
534 #ifdef MODULE
535 #define HDA_MODREQ_MAX_COUNT 2 /* two request_modules()'s */
536 #else
537 #define HDA_MODREQ_MAX_COUNT 0 /* all presets are statically linked */
538 #endif
541 * find a matching codec preset
543 static const struct hda_codec_preset *
544 find_codec_preset(struct hda_codec *codec)
546 struct hda_codec_preset_list *tbl;
547 const struct hda_codec_preset *preset;
548 int mod_requested = 0;
550 if (is_generic_config(codec))
551 return NULL; /* use the generic parser */
553 again:
554 mutex_lock(&preset_mutex);
555 list_for_each_entry(tbl, &hda_preset_tables, list) {
556 if (!try_module_get(tbl->owner)) {
557 snd_printk(KERN_ERR "hda_codec: cannot module_get\n");
558 continue;
560 for (preset = tbl->preset; preset->id; preset++) {
561 u32 mask = preset->mask;
562 if (preset->afg && preset->afg != codec->afg)
563 continue;
564 if (preset->mfg && preset->mfg != codec->mfg)
565 continue;
566 if (!mask)
567 mask = ~0;
568 if (preset->id == (codec->vendor_id & mask) &&
569 (!preset->rev ||
570 preset->rev == codec->revision_id)) {
571 mutex_unlock(&preset_mutex);
572 codec->owner = tbl->owner;
573 return preset;
576 module_put(tbl->owner);
578 mutex_unlock(&preset_mutex);
580 if (mod_requested < HDA_MODREQ_MAX_COUNT) {
581 char name[32];
582 if (!mod_requested)
583 snprintf(name, sizeof(name), "snd-hda-codec-id:%08x",
584 codec->vendor_id);
585 else
586 snprintf(name, sizeof(name), "snd-hda-codec-id:%04x*",
587 (codec->vendor_id >> 16) & 0xffff);
588 request_module(name);
589 mod_requested++;
590 goto again;
592 return NULL;
596 * get_codec_name - store the codec name
598 static int get_codec_name(struct hda_codec *codec)
600 const struct hda_vendor_id *c;
601 const char *vendor = NULL;
602 u16 vendor_id = codec->vendor_id >> 16;
603 char tmp[16], name[32];
605 for (c = hda_vendor_ids; c->id; c++) {
606 if (c->id == vendor_id) {
607 vendor = c->name;
608 break;
611 if (!vendor) {
612 sprintf(tmp, "Generic %04x", vendor_id);
613 vendor = tmp;
615 if (codec->preset && codec->preset->name)
616 snprintf(name, sizeof(name), "%s %s", vendor,
617 codec->preset->name);
618 else
619 snprintf(name, sizeof(name), "%s ID %x", vendor,
620 codec->vendor_id & 0xffff);
621 codec->name = kstrdup(name, GFP_KERNEL);
622 if (!codec->name)
623 return -ENOMEM;
624 return 0;
628 * look for an AFG and MFG nodes
630 static void /*__devinit*/ setup_fg_nodes(struct hda_codec *codec)
632 int i, total_nodes;
633 hda_nid_t nid;
635 total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid);
636 for (i = 0; i < total_nodes; i++, nid++) {
637 unsigned int func;
638 func = snd_hda_param_read(codec, nid, AC_PAR_FUNCTION_TYPE);
639 switch (func & 0xff) {
640 case AC_GRP_AUDIO_FUNCTION:
641 codec->afg = nid;
642 break;
643 case AC_GRP_MODEM_FUNCTION:
644 codec->mfg = nid;
645 break;
646 default:
647 break;
653 * read widget caps for each widget and store in cache
655 static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
657 int i;
658 hda_nid_t nid;
660 codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node,
661 &codec->start_nid);
662 codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL);
663 if (!codec->wcaps)
664 return -ENOMEM;
665 nid = codec->start_nid;
666 for (i = 0; i < codec->num_nodes; i++, nid++)
667 codec->wcaps[i] = snd_hda_param_read(codec, nid,
668 AC_PAR_AUDIO_WIDGET_CAP);
669 return 0;
673 static void init_hda_cache(struct hda_cache_rec *cache,
674 unsigned int record_size);
675 static void free_hda_cache(struct hda_cache_rec *cache);
678 * codec destructor
680 static void snd_hda_codec_free(struct hda_codec *codec)
682 if (!codec)
683 return;
684 #ifdef CONFIG_SND_HDA_POWER_SAVE
685 cancel_delayed_work(&codec->power_work);
686 flush_scheduled_work();
687 #endif
688 list_del(&codec->list);
689 snd_array_free(&codec->mixers);
690 codec->bus->caddr_tbl[codec->addr] = NULL;
691 if (codec->patch_ops.free)
692 codec->patch_ops.free(codec);
693 module_put(codec->owner);
694 free_hda_cache(&codec->amp_cache);
695 free_hda_cache(&codec->cmd_cache);
696 kfree(codec->name);
697 kfree(codec->modelname);
698 kfree(codec->wcaps);
699 kfree(codec);
703 * snd_hda_codec_new - create a HDA codec
704 * @bus: the bus to assign
705 * @codec_addr: the codec address
706 * @codecp: the pointer to store the generated codec
708 * Returns 0 if successful, or a negative error code.
710 int /*__devinit*/ snd_hda_codec_new(struct hda_bus *bus, unsigned int codec_addr,
711 struct hda_codec **codecp)
713 struct hda_codec *codec;
714 char component[31];
715 int err;
717 if (snd_BUG_ON(!bus))
718 return -EINVAL;
719 if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS))
720 return -EINVAL;
722 if (bus->caddr_tbl[codec_addr]) {
723 snd_printk(KERN_ERR "hda_codec: "
724 "address 0x%x is already occupied\n", codec_addr);
725 return -EBUSY;
728 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
729 if (codec == NULL) {
730 snd_printk(KERN_ERR "can't allocate struct hda_codec\n");
731 return -ENOMEM;
734 codec->bus = bus;
735 codec->addr = codec_addr;
736 mutex_init(&codec->spdif_mutex);
737 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
738 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
739 snd_array_init(&codec->mixers, sizeof(struct snd_kcontrol *), 32);
740 if (codec->bus->modelname) {
741 codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL);
742 if (!codec->modelname) {
743 snd_hda_codec_free(codec);
744 return -ENODEV;
748 #ifdef CONFIG_SND_HDA_POWER_SAVE
749 INIT_DELAYED_WORK(&codec->power_work, hda_power_work);
750 /* snd_hda_codec_new() marks the codec as power-up, and leave it as is.
751 * the caller has to power down appropriatley after initialization
752 * phase.
754 hda_keep_power_on(codec);
755 #endif
757 list_add_tail(&codec->list, &bus->codec_list);
758 bus->caddr_tbl[codec_addr] = codec;
760 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
761 AC_PAR_VENDOR_ID);
762 if (codec->vendor_id == -1)
763 /* read again, hopefully the access method was corrected
764 * in the last read...
766 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
767 AC_PAR_VENDOR_ID);
768 codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT,
769 AC_PAR_SUBSYSTEM_ID);
770 codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT,
771 AC_PAR_REV_ID);
773 setup_fg_nodes(codec);
774 if (!codec->afg && !codec->mfg) {
775 snd_printdd("hda_codec: no AFG or MFG node found\n");
776 snd_hda_codec_free(codec);
777 return -ENODEV;
780 if (read_widget_caps(codec, codec->afg ? codec->afg : codec->mfg) < 0) {
781 snd_printk(KERN_ERR "hda_codec: cannot malloc\n");
782 snd_hda_codec_free(codec);
783 return -ENOMEM;
786 if (!codec->subsystem_id) {
787 hda_nid_t nid = codec->afg ? codec->afg : codec->mfg;
788 codec->subsystem_id =
789 snd_hda_codec_read(codec, nid, 0,
790 AC_VERB_GET_SUBSYSTEM_ID, 0);
792 if (bus->modelname)
793 codec->modelname = kstrdup(bus->modelname, GFP_KERNEL);
795 err = snd_hda_codec_configure(codec);
796 if (err < 0) {
797 snd_hda_codec_free(codec);
798 return err;
800 snd_hda_codec_proc_new(codec);
802 snd_hda_create_hwdep(codec);
804 sprintf(component, "HDA:%08x,%08x,%08x", codec->vendor_id,
805 codec->subsystem_id, codec->revision_id);
806 snd_component_add(codec->bus->card, component);
808 if (codecp)
809 *codecp = codec;
810 return 0;
812 EXPORT_SYMBOL_HDA(snd_hda_codec_new);
814 int snd_hda_codec_configure(struct hda_codec *codec)
816 int err;
818 codec->preset = find_codec_preset(codec);
819 if (!codec->name) {
820 err = get_codec_name(codec);
821 if (err < 0)
822 return err;
824 /* audio codec should override the mixer name */
825 if (codec->afg || !*codec->bus->card->mixername)
826 strlcpy(codec->bus->card->mixername, codec->name,
827 sizeof(codec->bus->card->mixername));
829 if (is_generic_config(codec)) {
830 err = snd_hda_parse_generic_codec(codec);
831 goto patched;
833 if (codec->preset && codec->preset->patch) {
834 err = codec->preset->patch(codec);
835 goto patched;
838 /* call the default parser */
839 err = snd_hda_parse_generic_codec(codec);
840 if (err < 0)
841 printk(KERN_ERR "hda-codec: No codec parser is available\n");
843 patched:
844 if (!err && codec->patch_ops.unsol_event)
845 err = init_unsol_queue(codec->bus);
846 return err;
850 * snd_hda_codec_setup_stream - set up the codec for streaming
851 * @codec: the CODEC to set up
852 * @nid: the NID to set up
853 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
854 * @channel_id: channel id to pass, zero based.
855 * @format: stream format.
857 void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
858 u32 stream_tag,
859 int channel_id, int format)
861 if (!nid)
862 return;
864 snd_printdd("hda_codec_setup_stream: "
865 "NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
866 nid, stream_tag, channel_id, format);
867 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID,
868 (stream_tag << 4) | channel_id);
869 msleep(1);
870 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, format);
872 EXPORT_SYMBOL_HDA(snd_hda_codec_setup_stream);
874 void snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid)
876 if (!nid)
877 return;
879 snd_printdd("hda_codec_cleanup_stream: NID=0x%x\n", nid);
880 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
881 #if 0 /* keep the format */
882 msleep(1);
883 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0);
884 #endif
886 EXPORT_SYMBOL_HDA(snd_hda_codec_cleanup_stream);
889 * amp access functions
892 /* FIXME: more better hash key? */
893 #define HDA_HASH_KEY(nid,dir,idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
894 #define INFO_AMP_CAPS (1<<0)
895 #define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
897 /* initialize the hash table */
898 static void /*__devinit*/ init_hda_cache(struct hda_cache_rec *cache,
899 unsigned int record_size)
901 memset(cache, 0, sizeof(*cache));
902 memset(cache->hash, 0xff, sizeof(cache->hash));
903 snd_array_init(&cache->buf, record_size, 64);
906 static void free_hda_cache(struct hda_cache_rec *cache)
908 snd_array_free(&cache->buf);
911 /* query the hash. allocate an entry if not found. */
912 static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache,
913 u32 key)
915 u16 idx = key % (u16)ARRAY_SIZE(cache->hash);
916 u16 cur = cache->hash[idx];
917 struct hda_cache_head *info;
919 while (cur != 0xffff) {
920 info = snd_array_elem(&cache->buf, cur);
921 if (info->key == key)
922 return info;
923 cur = info->next;
926 /* add a new hash entry */
927 info = snd_array_new(&cache->buf);
928 if (!info)
929 return NULL;
930 cur = snd_array_index(&cache->buf, info);
931 info->key = key;
932 info->val = 0;
933 info->next = cache->hash[idx];
934 cache->hash[idx] = cur;
936 return info;
939 /* query and allocate an amp hash entry */
940 static inline struct hda_amp_info *
941 get_alloc_amp_hash(struct hda_codec *codec, u32 key)
943 return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key);
947 * query AMP capabilities for the given widget and direction
949 u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
951 struct hda_amp_info *info;
953 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, 0));
954 if (!info)
955 return 0;
956 if (!(info->head.val & INFO_AMP_CAPS)) {
957 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
958 nid = codec->afg;
959 info->amp_caps = snd_hda_param_read(codec, nid,
960 direction == HDA_OUTPUT ?
961 AC_PAR_AMP_OUT_CAP :
962 AC_PAR_AMP_IN_CAP);
963 if (info->amp_caps)
964 info->head.val |= INFO_AMP_CAPS;
966 return info->amp_caps;
968 EXPORT_SYMBOL_HDA(query_amp_caps);
970 int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
971 unsigned int caps)
973 struct hda_amp_info *info;
975 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, dir, 0));
976 if (!info)
977 return -EINVAL;
978 info->amp_caps = caps;
979 info->head.val |= INFO_AMP_CAPS;
980 return 0;
982 EXPORT_SYMBOL_HDA(snd_hda_override_amp_caps);
985 * read the current volume to info
986 * if the cache exists, read the cache value.
988 static unsigned int get_vol_mute(struct hda_codec *codec,
989 struct hda_amp_info *info, hda_nid_t nid,
990 int ch, int direction, int index)
992 u32 val, parm;
994 if (info->head.val & INFO_AMP_VOL(ch))
995 return info->vol[ch];
997 parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
998 parm |= direction == HDA_OUTPUT ? AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
999 parm |= index;
1000 val = snd_hda_codec_read(codec, nid, 0,
1001 AC_VERB_GET_AMP_GAIN_MUTE, parm);
1002 info->vol[ch] = val & 0xff;
1003 info->head.val |= INFO_AMP_VOL(ch);
1004 return info->vol[ch];
1008 * write the current volume in info to the h/w and update the cache
1010 static void put_vol_mute(struct hda_codec *codec, struct hda_amp_info *info,
1011 hda_nid_t nid, int ch, int direction, int index,
1012 int val)
1014 u32 parm;
1016 parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
1017 parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
1018 parm |= index << AC_AMP_SET_INDEX_SHIFT;
1019 parm |= val;
1020 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
1021 info->vol[ch] = val;
1025 * read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
1027 int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
1028 int direction, int index)
1030 struct hda_amp_info *info;
1031 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
1032 if (!info)
1033 return 0;
1034 return get_vol_mute(codec, info, nid, ch, direction, index);
1036 EXPORT_SYMBOL_HDA(snd_hda_codec_amp_read);
1039 * update the AMP value, mask = bit mask to set, val = the value
1041 int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
1042 int direction, int idx, int mask, int val)
1044 struct hda_amp_info *info;
1046 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, idx));
1047 if (!info)
1048 return 0;
1049 val &= mask;
1050 val |= get_vol_mute(codec, info, nid, ch, direction, idx) & ~mask;
1051 if (info->vol[ch] == val)
1052 return 0;
1053 put_vol_mute(codec, info, nid, ch, direction, idx, val);
1054 return 1;
1056 EXPORT_SYMBOL_HDA(snd_hda_codec_amp_update);
1059 * update the AMP stereo with the same mask and value
1061 int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
1062 int direction, int idx, int mask, int val)
1064 int ch, ret = 0;
1065 for (ch = 0; ch < 2; ch++)
1066 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
1067 idx, mask, val);
1068 return ret;
1070 EXPORT_SYMBOL_HDA(snd_hda_codec_amp_stereo);
1072 #ifdef SND_HDA_NEEDS_RESUME
1073 /* resume the all amp commands from the cache */
1074 void snd_hda_codec_resume_amp(struct hda_codec *codec)
1076 struct hda_amp_info *buffer = codec->amp_cache.buf.list;
1077 int i;
1079 for (i = 0; i < codec->amp_cache.buf.used; i++, buffer++) {
1080 u32 key = buffer->head.key;
1081 hda_nid_t nid;
1082 unsigned int idx, dir, ch;
1083 if (!key)
1084 continue;
1085 nid = key & 0xff;
1086 idx = (key >> 16) & 0xff;
1087 dir = (key >> 24) & 0xff;
1088 for (ch = 0; ch < 2; ch++) {
1089 if (!(buffer->head.val & INFO_AMP_VOL(ch)))
1090 continue;
1091 put_vol_mute(codec, buffer, nid, ch, dir, idx,
1092 buffer->vol[ch]);
1096 EXPORT_SYMBOL_HDA(snd_hda_codec_resume_amp);
1097 #endif /* SND_HDA_NEEDS_RESUME */
1099 /* volume */
1100 int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
1101 struct snd_ctl_elem_info *uinfo)
1103 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1104 u16 nid = get_amp_nid(kcontrol);
1105 u8 chs = get_amp_channels(kcontrol);
1106 int dir = get_amp_direction(kcontrol);
1107 u32 caps;
1109 caps = query_amp_caps(codec, nid, dir);
1110 /* num steps */
1111 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1112 if (!caps) {
1113 printk(KERN_WARNING "hda_codec: "
1114 "num_steps = 0 for NID=0x%x (ctl = %s)\n", nid,
1115 kcontrol->id.name);
1116 return -EINVAL;
1118 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1119 uinfo->count = chs == 3 ? 2 : 1;
1120 uinfo->value.integer.min = 0;
1121 uinfo->value.integer.max = caps;
1122 return 0;
1124 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_info);
1126 int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
1127 struct snd_ctl_elem_value *ucontrol)
1129 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1130 hda_nid_t nid = get_amp_nid(kcontrol);
1131 int chs = get_amp_channels(kcontrol);
1132 int dir = get_amp_direction(kcontrol);
1133 int idx = get_amp_index(kcontrol);
1134 long *valp = ucontrol->value.integer.value;
1136 if (chs & 1)
1137 *valp++ = snd_hda_codec_amp_read(codec, nid, 0, dir, idx)
1138 & HDA_AMP_VOLMASK;
1139 if (chs & 2)
1140 *valp = snd_hda_codec_amp_read(codec, nid, 1, dir, idx)
1141 & HDA_AMP_VOLMASK;
1142 return 0;
1144 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_get);
1146 int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
1147 struct snd_ctl_elem_value *ucontrol)
1149 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1150 hda_nid_t nid = get_amp_nid(kcontrol);
1151 int chs = get_amp_channels(kcontrol);
1152 int dir = get_amp_direction(kcontrol);
1153 int idx = get_amp_index(kcontrol);
1154 long *valp = ucontrol->value.integer.value;
1155 int change = 0;
1157 snd_hda_power_up(codec);
1158 if (chs & 1) {
1159 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
1160 0x7f, *valp);
1161 valp++;
1163 if (chs & 2)
1164 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
1165 0x7f, *valp);
1166 snd_hda_power_down(codec);
1167 return change;
1169 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_put);
1171 int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
1172 unsigned int size, unsigned int __user *_tlv)
1174 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1175 hda_nid_t nid = get_amp_nid(kcontrol);
1176 int dir = get_amp_direction(kcontrol);
1177 u32 caps, val1, val2;
1179 if (size < 4 * sizeof(unsigned int))
1180 return -ENOMEM;
1181 caps = query_amp_caps(codec, nid, dir);
1182 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
1183 val2 = (val2 + 1) * 25;
1184 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
1185 val1 = ((int)val1) * ((int)val2);
1186 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
1187 return -EFAULT;
1188 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
1189 return -EFAULT;
1190 if (put_user(val1, _tlv + 2))
1191 return -EFAULT;
1192 if (put_user(val2, _tlv + 3))
1193 return -EFAULT;
1194 return 0;
1196 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_tlv);
1199 * set (static) TLV for virtual master volume; recalculated as max 0dB
1201 void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
1202 unsigned int *tlv)
1204 u32 caps;
1205 int nums, step;
1207 caps = query_amp_caps(codec, nid, dir);
1208 nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1209 step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
1210 step = (step + 1) * 25;
1211 tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
1212 tlv[1] = 2 * sizeof(unsigned int);
1213 tlv[2] = -nums * step;
1214 tlv[3] = step;
1216 EXPORT_SYMBOL_HDA(snd_hda_set_vmaster_tlv);
1218 /* find a mixer control element with the given name */
1219 static struct snd_kcontrol *
1220 _snd_hda_find_mixer_ctl(struct hda_codec *codec,
1221 const char *name, int idx)
1223 struct snd_ctl_elem_id id;
1224 memset(&id, 0, sizeof(id));
1225 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1226 id.index = idx;
1227 strcpy(id.name, name);
1228 return snd_ctl_find_id(codec->bus->card, &id);
1231 struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
1232 const char *name)
1234 return _snd_hda_find_mixer_ctl(codec, name, 0);
1236 EXPORT_SYMBOL_HDA(snd_hda_find_mixer_ctl);
1238 /* Add a control element and assign to the codec */
1239 int snd_hda_ctl_add(struct hda_codec *codec, struct snd_kcontrol *kctl)
1241 int err;
1242 struct snd_kcontrol **knewp;
1244 err = snd_ctl_add(codec->bus->card, kctl);
1245 if (err < 0)
1246 return err;
1247 knewp = snd_array_new(&codec->mixers);
1248 if (!knewp)
1249 return -ENOMEM;
1250 *knewp = kctl;
1251 return 0;
1253 EXPORT_SYMBOL_HDA(snd_hda_ctl_add);
1255 #ifdef CONFIG_SND_HDA_RECONFIG
1256 /* Clear all controls assigned to the given codec */
1257 void snd_hda_ctls_clear(struct hda_codec *codec)
1259 int i;
1260 struct snd_kcontrol **kctls = codec->mixers.list;
1261 for (i = 0; i < codec->mixers.used; i++)
1262 snd_ctl_remove(codec->bus->card, kctls[i]);
1263 snd_array_free(&codec->mixers);
1266 void snd_hda_codec_reset(struct hda_codec *codec)
1268 int i;
1270 #ifdef CONFIG_SND_HDA_POWER_SAVE
1271 cancel_delayed_work(&codec->power_work);
1272 flush_scheduled_work();
1273 #endif
1274 snd_hda_ctls_clear(codec);
1275 /* relase PCMs */
1276 for (i = 0; i < codec->num_pcms; i++) {
1277 if (codec->pcm_info[i].pcm) {
1278 snd_device_free(codec->bus->card,
1279 codec->pcm_info[i].pcm);
1280 clear_bit(codec->pcm_info[i].device,
1281 codec->bus->pcm_dev_bits);
1284 if (codec->patch_ops.free)
1285 codec->patch_ops.free(codec);
1286 codec->proc_widget_hook = NULL;
1287 codec->spec = NULL;
1288 free_hda_cache(&codec->amp_cache);
1289 free_hda_cache(&codec->cmd_cache);
1290 codec->num_pcms = 0;
1291 codec->pcm_info = NULL;
1292 codec->preset = NULL;
1293 module_put(codec->owner);
1294 codec->owner = NULL;
1296 #endif /* CONFIG_SND_HDA_RECONFIG */
1298 /* create a virtual master control and add slaves */
1299 int snd_hda_add_vmaster(struct hda_codec *codec, char *name,
1300 unsigned int *tlv, const char **slaves)
1302 struct snd_kcontrol *kctl;
1303 const char **s;
1304 int err;
1306 for (s = slaves; *s && !snd_hda_find_mixer_ctl(codec, *s); s++)
1308 if (!*s) {
1309 snd_printdd("No slave found for %s\n", name);
1310 return 0;
1312 kctl = snd_ctl_make_virtual_master(name, tlv);
1313 if (!kctl)
1314 return -ENOMEM;
1315 err = snd_hda_ctl_add(codec, kctl);
1316 if (err < 0)
1317 return err;
1319 for (s = slaves; *s; s++) {
1320 struct snd_kcontrol *sctl;
1322 sctl = snd_hda_find_mixer_ctl(codec, *s);
1323 if (!sctl) {
1324 snd_printdd("Cannot find slave %s, skipped\n", *s);
1325 continue;
1327 err = snd_ctl_add_slave(kctl, sctl);
1328 if (err < 0)
1329 return err;
1331 return 0;
1333 EXPORT_SYMBOL_HDA(snd_hda_add_vmaster);
1335 /* switch */
1336 int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
1337 struct snd_ctl_elem_info *uinfo)
1339 int chs = get_amp_channels(kcontrol);
1341 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1342 uinfo->count = chs == 3 ? 2 : 1;
1343 uinfo->value.integer.min = 0;
1344 uinfo->value.integer.max = 1;
1345 return 0;
1347 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_info);
1349 int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
1350 struct snd_ctl_elem_value *ucontrol)
1352 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1353 hda_nid_t nid = get_amp_nid(kcontrol);
1354 int chs = get_amp_channels(kcontrol);
1355 int dir = get_amp_direction(kcontrol);
1356 int idx = get_amp_index(kcontrol);
1357 long *valp = ucontrol->value.integer.value;
1359 if (chs & 1)
1360 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
1361 HDA_AMP_MUTE) ? 0 : 1;
1362 if (chs & 2)
1363 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
1364 HDA_AMP_MUTE) ? 0 : 1;
1365 return 0;
1367 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_get);
1369 int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
1370 struct snd_ctl_elem_value *ucontrol)
1372 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1373 hda_nid_t nid = get_amp_nid(kcontrol);
1374 int chs = get_amp_channels(kcontrol);
1375 int dir = get_amp_direction(kcontrol);
1376 int idx = get_amp_index(kcontrol);
1377 long *valp = ucontrol->value.integer.value;
1378 int change = 0;
1380 snd_hda_power_up(codec);
1381 if (chs & 1) {
1382 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
1383 HDA_AMP_MUTE,
1384 *valp ? 0 : HDA_AMP_MUTE);
1385 valp++;
1387 if (chs & 2)
1388 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
1389 HDA_AMP_MUTE,
1390 *valp ? 0 : HDA_AMP_MUTE);
1391 #ifdef CONFIG_SND_HDA_POWER_SAVE
1392 if (codec->patch_ops.check_power_status)
1393 codec->patch_ops.check_power_status(codec, nid);
1394 #endif
1395 snd_hda_power_down(codec);
1396 return change;
1398 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put);
1401 * bound volume controls
1403 * bind multiple volumes (# indices, from 0)
1406 #define AMP_VAL_IDX_SHIFT 19
1407 #define AMP_VAL_IDX_MASK (0x0f<<19)
1409 int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
1410 struct snd_ctl_elem_value *ucontrol)
1412 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1413 unsigned long pval;
1414 int err;
1416 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
1417 pval = kcontrol->private_value;
1418 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
1419 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
1420 kcontrol->private_value = pval;
1421 mutex_unlock(&codec->spdif_mutex);
1422 return err;
1424 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_get);
1426 int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
1427 struct snd_ctl_elem_value *ucontrol)
1429 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1430 unsigned long pval;
1431 int i, indices, err = 0, change = 0;
1433 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
1434 pval = kcontrol->private_value;
1435 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
1436 for (i = 0; i < indices; i++) {
1437 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
1438 (i << AMP_VAL_IDX_SHIFT);
1439 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
1440 if (err < 0)
1441 break;
1442 change |= err;
1444 kcontrol->private_value = pval;
1445 mutex_unlock(&codec->spdif_mutex);
1446 return err < 0 ? err : change;
1448 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_put);
1451 * generic bound volume/swtich controls
1453 int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
1454 struct snd_ctl_elem_info *uinfo)
1456 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1457 struct hda_bind_ctls *c;
1458 int err;
1460 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
1461 c = (struct hda_bind_ctls *)kcontrol->private_value;
1462 kcontrol->private_value = *c->values;
1463 err = c->ops->info(kcontrol, uinfo);
1464 kcontrol->private_value = (long)c;
1465 mutex_unlock(&codec->spdif_mutex);
1466 return err;
1468 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_info);
1470 int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
1471 struct snd_ctl_elem_value *ucontrol)
1473 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1474 struct hda_bind_ctls *c;
1475 int err;
1477 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
1478 c = (struct hda_bind_ctls *)kcontrol->private_value;
1479 kcontrol->private_value = *c->values;
1480 err = c->ops->get(kcontrol, ucontrol);
1481 kcontrol->private_value = (long)c;
1482 mutex_unlock(&codec->spdif_mutex);
1483 return err;
1485 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_get);
1487 int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
1488 struct snd_ctl_elem_value *ucontrol)
1490 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1491 struct hda_bind_ctls *c;
1492 unsigned long *vals;
1493 int err = 0, change = 0;
1495 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
1496 c = (struct hda_bind_ctls *)kcontrol->private_value;
1497 for (vals = c->values; *vals; vals++) {
1498 kcontrol->private_value = *vals;
1499 err = c->ops->put(kcontrol, ucontrol);
1500 if (err < 0)
1501 break;
1502 change |= err;
1504 kcontrol->private_value = (long)c;
1505 mutex_unlock(&codec->spdif_mutex);
1506 return err < 0 ? err : change;
1508 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_put);
1510 int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
1511 unsigned int size, unsigned int __user *tlv)
1513 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1514 struct hda_bind_ctls *c;
1515 int err;
1517 mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
1518 c = (struct hda_bind_ctls *)kcontrol->private_value;
1519 kcontrol->private_value = *c->values;
1520 err = c->ops->tlv(kcontrol, op_flag, size, tlv);
1521 kcontrol->private_value = (long)c;
1522 mutex_unlock(&codec->spdif_mutex);
1523 return err;
1525 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_tlv);
1527 struct hda_ctl_ops snd_hda_bind_vol = {
1528 .info = snd_hda_mixer_amp_volume_info,
1529 .get = snd_hda_mixer_amp_volume_get,
1530 .put = snd_hda_mixer_amp_volume_put,
1531 .tlv = snd_hda_mixer_amp_tlv
1533 EXPORT_SYMBOL_HDA(snd_hda_bind_vol);
1535 struct hda_ctl_ops snd_hda_bind_sw = {
1536 .info = snd_hda_mixer_amp_switch_info,
1537 .get = snd_hda_mixer_amp_switch_get,
1538 .put = snd_hda_mixer_amp_switch_put,
1539 .tlv = snd_hda_mixer_amp_tlv
1541 EXPORT_SYMBOL_HDA(snd_hda_bind_sw);
1544 * SPDIF out controls
1547 static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
1548 struct snd_ctl_elem_info *uinfo)
1550 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1551 uinfo->count = 1;
1552 return 0;
1555 static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
1556 struct snd_ctl_elem_value *ucontrol)
1558 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
1559 IEC958_AES0_NONAUDIO |
1560 IEC958_AES0_CON_EMPHASIS_5015 |
1561 IEC958_AES0_CON_NOT_COPYRIGHT;
1562 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
1563 IEC958_AES1_CON_ORIGINAL;
1564 return 0;
1567 static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
1568 struct snd_ctl_elem_value *ucontrol)
1570 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
1571 IEC958_AES0_NONAUDIO |
1572 IEC958_AES0_PRO_EMPHASIS_5015;
1573 return 0;
1576 static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
1577 struct snd_ctl_elem_value *ucontrol)
1579 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1581 ucontrol->value.iec958.status[0] = codec->spdif_status & 0xff;
1582 ucontrol->value.iec958.status[1] = (codec->spdif_status >> 8) & 0xff;
1583 ucontrol->value.iec958.status[2] = (codec->spdif_status >> 16) & 0xff;
1584 ucontrol->value.iec958.status[3] = (codec->spdif_status >> 24) & 0xff;
1586 return 0;
1589 /* convert from SPDIF status bits to HDA SPDIF bits
1590 * bit 0 (DigEn) is always set zero (to be filled later)
1592 static unsigned short convert_from_spdif_status(unsigned int sbits)
1594 unsigned short val = 0;
1596 if (sbits & IEC958_AES0_PROFESSIONAL)
1597 val |= AC_DIG1_PROFESSIONAL;
1598 if (sbits & IEC958_AES0_NONAUDIO)
1599 val |= AC_DIG1_NONAUDIO;
1600 if (sbits & IEC958_AES0_PROFESSIONAL) {
1601 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
1602 IEC958_AES0_PRO_EMPHASIS_5015)
1603 val |= AC_DIG1_EMPHASIS;
1604 } else {
1605 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
1606 IEC958_AES0_CON_EMPHASIS_5015)
1607 val |= AC_DIG1_EMPHASIS;
1608 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
1609 val |= AC_DIG1_COPYRIGHT;
1610 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
1611 val |= AC_DIG1_LEVEL;
1612 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
1614 return val;
1617 /* convert to SPDIF status bits from HDA SPDIF bits
1619 static unsigned int convert_to_spdif_status(unsigned short val)
1621 unsigned int sbits = 0;
1623 if (val & AC_DIG1_NONAUDIO)
1624 sbits |= IEC958_AES0_NONAUDIO;
1625 if (val & AC_DIG1_PROFESSIONAL)
1626 sbits |= IEC958_AES0_PROFESSIONAL;
1627 if (sbits & IEC958_AES0_PROFESSIONAL) {
1628 if (sbits & AC_DIG1_EMPHASIS)
1629 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
1630 } else {
1631 if (val & AC_DIG1_EMPHASIS)
1632 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
1633 if (!(val & AC_DIG1_COPYRIGHT))
1634 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
1635 if (val & AC_DIG1_LEVEL)
1636 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
1637 sbits |= val & (0x7f << 8);
1639 return sbits;
1642 /* set digital convert verbs both for the given NID and its slaves */
1643 static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
1644 int verb, int val)
1646 hda_nid_t *d;
1648 snd_hda_codec_write_cache(codec, nid, 0, verb, val);
1649 d = codec->slave_dig_outs;
1650 if (!d)
1651 return;
1652 for (; *d; d++)
1653 snd_hda_codec_write_cache(codec, *d, 0, verb, val);
1656 static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
1657 int dig1, int dig2)
1659 if (dig1 != -1)
1660 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1);
1661 if (dig2 != -1)
1662 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2);
1665 static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
1666 struct snd_ctl_elem_value *ucontrol)
1668 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1669 hda_nid_t nid = kcontrol->private_value;
1670 unsigned short val;
1671 int change;
1673 mutex_lock(&codec->spdif_mutex);
1674 codec->spdif_status = ucontrol->value.iec958.status[0] |
1675 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
1676 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
1677 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
1678 val = convert_from_spdif_status(codec->spdif_status);
1679 val |= codec->spdif_ctls & 1;
1680 change = codec->spdif_ctls != val;
1681 codec->spdif_ctls = val;
1683 if (change)
1684 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
1686 mutex_unlock(&codec->spdif_mutex);
1687 return change;
1690 #define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
1692 static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
1693 struct snd_ctl_elem_value *ucontrol)
1695 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1697 ucontrol->value.integer.value[0] = codec->spdif_ctls & AC_DIG1_ENABLE;
1698 return 0;
1701 static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
1702 struct snd_ctl_elem_value *ucontrol)
1704 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1705 hda_nid_t nid = kcontrol->private_value;
1706 unsigned short val;
1707 int change;
1709 mutex_lock(&codec->spdif_mutex);
1710 val = codec->spdif_ctls & ~AC_DIG1_ENABLE;
1711 if (ucontrol->value.integer.value[0])
1712 val |= AC_DIG1_ENABLE;
1713 change = codec->spdif_ctls != val;
1714 if (change) {
1715 codec->spdif_ctls = val;
1716 set_dig_out_convert(codec, nid, val & 0xff, -1);
1717 /* unmute amp switch (if any) */
1718 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
1719 (val & AC_DIG1_ENABLE))
1720 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
1721 HDA_AMP_MUTE, 0);
1723 mutex_unlock(&codec->spdif_mutex);
1724 return change;
1727 static struct snd_kcontrol_new dig_mixes[] = {
1729 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1730 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1731 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,CON_MASK),
1732 .info = snd_hda_spdif_mask_info,
1733 .get = snd_hda_spdif_cmask_get,
1736 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1737 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1738 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,PRO_MASK),
1739 .info = snd_hda_spdif_mask_info,
1740 .get = snd_hda_spdif_pmask_get,
1743 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1744 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
1745 .info = snd_hda_spdif_mask_info,
1746 .get = snd_hda_spdif_default_get,
1747 .put = snd_hda_spdif_default_put,
1750 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1751 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,SWITCH),
1752 .info = snd_hda_spdif_out_switch_info,
1753 .get = snd_hda_spdif_out_switch_get,
1754 .put = snd_hda_spdif_out_switch_put,
1756 { } /* end */
1759 #define SPDIF_MAX_IDX 4 /* 4 instances should be enough to probe */
1762 * snd_hda_create_spdif_out_ctls - create Output SPDIF-related controls
1763 * @codec: the HDA codec
1764 * @nid: audio out widget NID
1766 * Creates controls related with the SPDIF output.
1767 * Called from each patch supporting the SPDIF out.
1769 * Returns 0 if successful, or a negative error code.
1771 int snd_hda_create_spdif_out_ctls(struct hda_codec *codec, hda_nid_t nid)
1773 int err;
1774 struct snd_kcontrol *kctl;
1775 struct snd_kcontrol_new *dig_mix;
1776 int idx;
1778 for (idx = 0; idx < SPDIF_MAX_IDX; idx++) {
1779 if (!_snd_hda_find_mixer_ctl(codec, "IEC958 Playback Switch",
1780 idx))
1781 break;
1783 if (idx >= SPDIF_MAX_IDX) {
1784 printk(KERN_ERR "hda_codec: too many IEC958 outputs\n");
1785 return -EBUSY;
1787 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
1788 kctl = snd_ctl_new1(dig_mix, codec);
1789 if (!kctl)
1790 return -ENOMEM;
1791 kctl->id.index = idx;
1792 kctl->private_value = nid;
1793 err = snd_hda_ctl_add(codec, kctl);
1794 if (err < 0)
1795 return err;
1797 codec->spdif_ctls =
1798 snd_hda_codec_read(codec, nid, 0,
1799 AC_VERB_GET_DIGI_CONVERT_1, 0);
1800 codec->spdif_status = convert_to_spdif_status(codec->spdif_ctls);
1801 return 0;
1803 EXPORT_SYMBOL_HDA(snd_hda_create_spdif_out_ctls);
1806 * SPDIF sharing with analog output
1808 static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
1809 struct snd_ctl_elem_value *ucontrol)
1811 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
1812 ucontrol->value.integer.value[0] = mout->share_spdif;
1813 return 0;
1816 static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
1817 struct snd_ctl_elem_value *ucontrol)
1819 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
1820 mout->share_spdif = !!ucontrol->value.integer.value[0];
1821 return 0;
1824 static struct snd_kcontrol_new spdif_share_sw = {
1825 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1826 .name = "IEC958 Default PCM Playback Switch",
1827 .info = snd_ctl_boolean_mono_info,
1828 .get = spdif_share_sw_get,
1829 .put = spdif_share_sw_put,
1832 int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
1833 struct hda_multi_out *mout)
1835 if (!mout->dig_out_nid)
1836 return 0;
1837 /* ATTENTION: here mout is passed as private_data, instead of codec */
1838 return snd_hda_ctl_add(codec,
1839 snd_ctl_new1(&spdif_share_sw, mout));
1841 EXPORT_SYMBOL_HDA(snd_hda_create_spdif_share_sw);
1844 * SPDIF input
1847 #define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
1849 static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
1850 struct snd_ctl_elem_value *ucontrol)
1852 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1854 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
1855 return 0;
1858 static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
1859 struct snd_ctl_elem_value *ucontrol)
1861 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1862 hda_nid_t nid = kcontrol->private_value;
1863 unsigned int val = !!ucontrol->value.integer.value[0];
1864 int change;
1866 mutex_lock(&codec->spdif_mutex);
1867 change = codec->spdif_in_enable != val;
1868 if (change) {
1869 codec->spdif_in_enable = val;
1870 snd_hda_codec_write_cache(codec, nid, 0,
1871 AC_VERB_SET_DIGI_CONVERT_1, val);
1873 mutex_unlock(&codec->spdif_mutex);
1874 return change;
1877 static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
1878 struct snd_ctl_elem_value *ucontrol)
1880 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1881 hda_nid_t nid = kcontrol->private_value;
1882 unsigned short val;
1883 unsigned int sbits;
1885 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0);
1886 sbits = convert_to_spdif_status(val);
1887 ucontrol->value.iec958.status[0] = sbits;
1888 ucontrol->value.iec958.status[1] = sbits >> 8;
1889 ucontrol->value.iec958.status[2] = sbits >> 16;
1890 ucontrol->value.iec958.status[3] = sbits >> 24;
1891 return 0;
1894 static struct snd_kcontrol_new dig_in_ctls[] = {
1896 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1897 .name = SNDRV_CTL_NAME_IEC958("",CAPTURE,SWITCH),
1898 .info = snd_hda_spdif_in_switch_info,
1899 .get = snd_hda_spdif_in_switch_get,
1900 .put = snd_hda_spdif_in_switch_put,
1903 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1904 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1905 .name = SNDRV_CTL_NAME_IEC958("",CAPTURE,DEFAULT),
1906 .info = snd_hda_spdif_mask_info,
1907 .get = snd_hda_spdif_in_status_get,
1909 { } /* end */
1913 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
1914 * @codec: the HDA codec
1915 * @nid: audio in widget NID
1917 * Creates controls related with the SPDIF input.
1918 * Called from each patch supporting the SPDIF in.
1920 * Returns 0 if successful, or a negative error code.
1922 int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
1924 int err;
1925 struct snd_kcontrol *kctl;
1926 struct snd_kcontrol_new *dig_mix;
1927 int idx;
1929 for (idx = 0; idx < SPDIF_MAX_IDX; idx++) {
1930 if (!_snd_hda_find_mixer_ctl(codec, "IEC958 Capture Switch",
1931 idx))
1932 break;
1934 if (idx >= SPDIF_MAX_IDX) {
1935 printk(KERN_ERR "hda_codec: too many IEC958 inputs\n");
1936 return -EBUSY;
1938 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
1939 kctl = snd_ctl_new1(dig_mix, codec);
1940 kctl->private_value = nid;
1941 err = snd_hda_ctl_add(codec, kctl);
1942 if (err < 0)
1943 return err;
1945 codec->spdif_in_enable =
1946 snd_hda_codec_read(codec, nid, 0,
1947 AC_VERB_GET_DIGI_CONVERT_1, 0) &
1948 AC_DIG1_ENABLE;
1949 return 0;
1951 EXPORT_SYMBOL_HDA(snd_hda_create_spdif_in_ctls);
1953 #ifdef SND_HDA_NEEDS_RESUME
1955 * command cache
1958 /* build a 32bit cache key with the widget id and the command parameter */
1959 #define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
1960 #define get_cmd_cache_nid(key) ((key) & 0xff)
1961 #define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
1964 * snd_hda_codec_write_cache - send a single command with caching
1965 * @codec: the HDA codec
1966 * @nid: NID to send the command
1967 * @direct: direct flag
1968 * @verb: the verb to send
1969 * @parm: the parameter for the verb
1971 * Send a single command without waiting for response.
1973 * Returns 0 if successful, or a negative error code.
1975 int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
1976 int direct, unsigned int verb, unsigned int parm)
1978 struct hda_bus *bus = codec->bus;
1979 unsigned int res;
1980 int err;
1982 res = make_codec_cmd(codec, nid, direct, verb, parm);
1983 snd_hda_power_up(codec);
1984 mutex_lock(&bus->cmd_mutex);
1985 err = bus->ops.command(bus, res);
1986 if (!err) {
1987 struct hda_cache_head *c;
1988 u32 key = build_cmd_cache_key(nid, verb);
1989 c = get_alloc_hash(&codec->cmd_cache, key);
1990 if (c)
1991 c->val = parm;
1993 mutex_unlock(&bus->cmd_mutex);
1994 snd_hda_power_down(codec);
1995 return err;
1997 EXPORT_SYMBOL_HDA(snd_hda_codec_write_cache);
1999 /* resume the all commands from the cache */
2000 void snd_hda_codec_resume_cache(struct hda_codec *codec)
2002 struct hda_cache_head *buffer = codec->cmd_cache.buf.list;
2003 int i;
2005 for (i = 0; i < codec->cmd_cache.buf.used; i++, buffer++) {
2006 u32 key = buffer->key;
2007 if (!key)
2008 continue;
2009 snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
2010 get_cmd_cache_cmd(key), buffer->val);
2013 EXPORT_SYMBOL_HDA(snd_hda_codec_resume_cache);
2016 * snd_hda_sequence_write_cache - sequence writes with caching
2017 * @codec: the HDA codec
2018 * @seq: VERB array to send
2020 * Send the commands sequentially from the given array.
2021 * Thte commands are recorded on cache for power-save and resume.
2022 * The array must be terminated with NID=0.
2024 void snd_hda_sequence_write_cache(struct hda_codec *codec,
2025 const struct hda_verb *seq)
2027 for (; seq->nid; seq++)
2028 snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
2029 seq->param);
2031 EXPORT_SYMBOL_HDA(snd_hda_sequence_write_cache);
2032 #endif /* SND_HDA_NEEDS_RESUME */
2035 * set power state of the codec
2037 static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
2038 unsigned int power_state)
2040 hda_nid_t nid;
2041 int i;
2043 snd_hda_codec_write(codec, fg, 0, AC_VERB_SET_POWER_STATE,
2044 power_state);
2045 msleep(10); /* partial workaround for "azx_get_response timeout" */
2047 nid = codec->start_nid;
2048 for (i = 0; i < codec->num_nodes; i++, nid++) {
2049 unsigned int wcaps = get_wcaps(codec, nid);
2050 if (wcaps & AC_WCAP_POWER) {
2051 unsigned int wid_type = (wcaps & AC_WCAP_TYPE) >>
2052 AC_WCAP_TYPE_SHIFT;
2053 if (wid_type == AC_WID_PIN) {
2054 unsigned int pincap;
2056 * don't power down the widget if it controls
2057 * eapd and EAPD_BTLENABLE is set.
2059 pincap = snd_hda_param_read(codec, nid,
2060 AC_PAR_PIN_CAP);
2061 if (pincap & AC_PINCAP_EAPD) {
2062 int eapd = snd_hda_codec_read(codec,
2063 nid, 0,
2064 AC_VERB_GET_EAPD_BTLENABLE, 0);
2065 eapd &= 0x02;
2066 if (power_state == AC_PWRST_D3 && eapd)
2067 continue;
2070 snd_hda_codec_write(codec, nid, 0,
2071 AC_VERB_SET_POWER_STATE,
2072 power_state);
2076 if (power_state == AC_PWRST_D0) {
2077 unsigned long end_time;
2078 int state;
2079 msleep(10);
2080 /* wait until the codec reachs to D0 */
2081 end_time = jiffies + msecs_to_jiffies(500);
2082 do {
2083 state = snd_hda_codec_read(codec, fg, 0,
2084 AC_VERB_GET_POWER_STATE, 0);
2085 if (state == power_state)
2086 break;
2087 msleep(1);
2088 } while (time_after_eq(end_time, jiffies));
2092 #ifdef CONFIG_SND_HDA_HWDEP
2093 /* execute additional init verbs */
2094 static void hda_exec_init_verbs(struct hda_codec *codec)
2096 if (codec->init_verbs.list)
2097 snd_hda_sequence_write(codec, codec->init_verbs.list);
2099 #else
2100 static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
2101 #endif
2103 #ifdef SND_HDA_NEEDS_RESUME
2105 * call suspend and power-down; used both from PM and power-save
2107 static void hda_call_codec_suspend(struct hda_codec *codec)
2109 if (codec->patch_ops.suspend)
2110 codec->patch_ops.suspend(codec, PMSG_SUSPEND);
2111 hda_set_power_state(codec,
2112 codec->afg ? codec->afg : codec->mfg,
2113 AC_PWRST_D3);
2114 #ifdef CONFIG_SND_HDA_POWER_SAVE
2115 cancel_delayed_work(&codec->power_work);
2116 codec->power_on = 0;
2117 codec->power_transition = 0;
2118 #endif
2122 * kick up codec; used both from PM and power-save
2124 static void hda_call_codec_resume(struct hda_codec *codec)
2126 hda_set_power_state(codec,
2127 codec->afg ? codec->afg : codec->mfg,
2128 AC_PWRST_D0);
2129 hda_exec_init_verbs(codec);
2130 if (codec->patch_ops.resume)
2131 codec->patch_ops.resume(codec);
2132 else {
2133 if (codec->patch_ops.init)
2134 codec->patch_ops.init(codec);
2135 snd_hda_codec_resume_amp(codec);
2136 snd_hda_codec_resume_cache(codec);
2139 #endif /* SND_HDA_NEEDS_RESUME */
2143 * snd_hda_build_controls - build mixer controls
2144 * @bus: the BUS
2146 * Creates mixer controls for each codec included in the bus.
2148 * Returns 0 if successful, otherwise a negative error code.
2150 int /*__devinit*/ snd_hda_build_controls(struct hda_bus *bus)
2152 struct hda_codec *codec;
2154 list_for_each_entry(codec, &bus->codec_list, list) {
2155 int err = snd_hda_codec_build_controls(codec);
2156 if (err < 0)
2157 return err;
2159 return 0;
2161 EXPORT_SYMBOL_HDA(snd_hda_build_controls);
2163 int snd_hda_codec_build_controls(struct hda_codec *codec)
2165 int err = 0;
2166 /* fake as if already powered-on */
2167 hda_keep_power_on(codec);
2168 /* then fire up */
2169 hda_set_power_state(codec,
2170 codec->afg ? codec->afg : codec->mfg,
2171 AC_PWRST_D0);
2172 hda_exec_init_verbs(codec);
2173 /* continue to initialize... */
2174 if (codec->patch_ops.init)
2175 err = codec->patch_ops.init(codec);
2176 if (!err && codec->patch_ops.build_controls)
2177 err = codec->patch_ops.build_controls(codec);
2178 snd_hda_power_down(codec);
2179 if (err < 0)
2180 return err;
2181 return 0;
2185 * stream formats
2187 struct hda_rate_tbl {
2188 unsigned int hz;
2189 unsigned int alsa_bits;
2190 unsigned int hda_fmt;
2193 static struct hda_rate_tbl rate_bits[] = {
2194 /* rate in Hz, ALSA rate bitmask, HDA format value */
2196 /* autodetected value used in snd_hda_query_supported_pcm */
2197 { 8000, SNDRV_PCM_RATE_8000, 0x0500 }, /* 1/6 x 48 */
2198 { 11025, SNDRV_PCM_RATE_11025, 0x4300 }, /* 1/4 x 44 */
2199 { 16000, SNDRV_PCM_RATE_16000, 0x0200 }, /* 1/3 x 48 */
2200 { 22050, SNDRV_PCM_RATE_22050, 0x4100 }, /* 1/2 x 44 */
2201 { 32000, SNDRV_PCM_RATE_32000, 0x0a00 }, /* 2/3 x 48 */
2202 { 44100, SNDRV_PCM_RATE_44100, 0x4000 }, /* 44 */
2203 { 48000, SNDRV_PCM_RATE_48000, 0x0000 }, /* 48 */
2204 { 88200, SNDRV_PCM_RATE_88200, 0x4800 }, /* 2 x 44 */
2205 { 96000, SNDRV_PCM_RATE_96000, 0x0800 }, /* 2 x 48 */
2206 { 176400, SNDRV_PCM_RATE_176400, 0x5800 },/* 4 x 44 */
2207 { 192000, SNDRV_PCM_RATE_192000, 0x1800 }, /* 4 x 48 */
2208 #define AC_PAR_PCM_RATE_BITS 11
2209 /* up to bits 10, 384kHZ isn't supported properly */
2211 /* not autodetected value */
2212 { 9600, SNDRV_PCM_RATE_KNOT, 0x0400 }, /* 1/5 x 48 */
2214 { 0 } /* terminator */
2218 * snd_hda_calc_stream_format - calculate format bitset
2219 * @rate: the sample rate
2220 * @channels: the number of channels
2221 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
2222 * @maxbps: the max. bps
2224 * Calculate the format bitset from the given rate, channels and th PCM format.
2226 * Return zero if invalid.
2228 unsigned int snd_hda_calc_stream_format(unsigned int rate,
2229 unsigned int channels,
2230 unsigned int format,
2231 unsigned int maxbps)
2233 int i;
2234 unsigned int val = 0;
2236 for (i = 0; rate_bits[i].hz; i++)
2237 if (rate_bits[i].hz == rate) {
2238 val = rate_bits[i].hda_fmt;
2239 break;
2241 if (!rate_bits[i].hz) {
2242 snd_printdd("invalid rate %d\n", rate);
2243 return 0;
2246 if (channels == 0 || channels > 8) {
2247 snd_printdd("invalid channels %d\n", channels);
2248 return 0;
2250 val |= channels - 1;
2252 switch (snd_pcm_format_width(format)) {
2253 case 8: val |= 0x00; break;
2254 case 16: val |= 0x10; break;
2255 case 20:
2256 case 24:
2257 case 32:
2258 if (maxbps >= 32)
2259 val |= 0x40;
2260 else if (maxbps >= 24)
2261 val |= 0x30;
2262 else
2263 val |= 0x20;
2264 break;
2265 default:
2266 snd_printdd("invalid format width %d\n",
2267 snd_pcm_format_width(format));
2268 return 0;
2271 return val;
2273 EXPORT_SYMBOL_HDA(snd_hda_calc_stream_format);
2276 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
2277 * @codec: the HDA codec
2278 * @nid: NID to query
2279 * @ratesp: the pointer to store the detected rate bitflags
2280 * @formatsp: the pointer to store the detected formats
2281 * @bpsp: the pointer to store the detected format widths
2283 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
2284 * or @bsps argument is ignored.
2286 * Returns 0 if successful, otherwise a negative error code.
2288 static int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
2289 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
2291 int i;
2292 unsigned int val, streams;
2294 val = 0;
2295 if (nid != codec->afg &&
2296 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD)) {
2297 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
2298 if (val == -1)
2299 return -EIO;
2301 if (!val)
2302 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
2304 if (ratesp) {
2305 u32 rates = 0;
2306 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
2307 if (val & (1 << i))
2308 rates |= rate_bits[i].alsa_bits;
2310 *ratesp = rates;
2313 if (formatsp || bpsp) {
2314 u64 formats = 0;
2315 unsigned int bps;
2316 unsigned int wcaps;
2318 wcaps = get_wcaps(codec, nid);
2319 streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
2320 if (streams == -1)
2321 return -EIO;
2322 if (!streams) {
2323 streams = snd_hda_param_read(codec, codec->afg,
2324 AC_PAR_STREAM);
2325 if (streams == -1)
2326 return -EIO;
2329 bps = 0;
2330 if (streams & AC_SUPFMT_PCM) {
2331 if (val & AC_SUPPCM_BITS_8) {
2332 formats |= SNDRV_PCM_FMTBIT_U8;
2333 bps = 8;
2335 if (val & AC_SUPPCM_BITS_16) {
2336 formats |= SNDRV_PCM_FMTBIT_S16_LE;
2337 bps = 16;
2339 if (wcaps & AC_WCAP_DIGITAL) {
2340 if (val & AC_SUPPCM_BITS_32)
2341 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
2342 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
2343 formats |= SNDRV_PCM_FMTBIT_S32_LE;
2344 if (val & AC_SUPPCM_BITS_24)
2345 bps = 24;
2346 else if (val & AC_SUPPCM_BITS_20)
2347 bps = 20;
2348 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
2349 AC_SUPPCM_BITS_32)) {
2350 formats |= SNDRV_PCM_FMTBIT_S32_LE;
2351 if (val & AC_SUPPCM_BITS_32)
2352 bps = 32;
2353 else if (val & AC_SUPPCM_BITS_24)
2354 bps = 24;
2355 else if (val & AC_SUPPCM_BITS_20)
2356 bps = 20;
2359 else if (streams == AC_SUPFMT_FLOAT32) {
2360 /* should be exclusive */
2361 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
2362 bps = 32;
2363 } else if (streams == AC_SUPFMT_AC3) {
2364 /* should be exclusive */
2365 /* temporary hack: we have still no proper support
2366 * for the direct AC3 stream...
2368 formats |= SNDRV_PCM_FMTBIT_U8;
2369 bps = 8;
2371 if (formatsp)
2372 *formatsp = formats;
2373 if (bpsp)
2374 *bpsp = bps;
2377 return 0;
2381 * snd_hda_is_supported_format - check whether the given node supports
2382 * the format val
2384 * Returns 1 if supported, 0 if not.
2386 int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
2387 unsigned int format)
2389 int i;
2390 unsigned int val = 0, rate, stream;
2392 if (nid != codec->afg &&
2393 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD)) {
2394 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
2395 if (val == -1)
2396 return 0;
2398 if (!val) {
2399 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
2400 if (val == -1)
2401 return 0;
2404 rate = format & 0xff00;
2405 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
2406 if (rate_bits[i].hda_fmt == rate) {
2407 if (val & (1 << i))
2408 break;
2409 return 0;
2411 if (i >= AC_PAR_PCM_RATE_BITS)
2412 return 0;
2414 stream = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
2415 if (stream == -1)
2416 return 0;
2417 if (!stream && nid != codec->afg)
2418 stream = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
2419 if (!stream || stream == -1)
2420 return 0;
2422 if (stream & AC_SUPFMT_PCM) {
2423 switch (format & 0xf0) {
2424 case 0x00:
2425 if (!(val & AC_SUPPCM_BITS_8))
2426 return 0;
2427 break;
2428 case 0x10:
2429 if (!(val & AC_SUPPCM_BITS_16))
2430 return 0;
2431 break;
2432 case 0x20:
2433 if (!(val & AC_SUPPCM_BITS_20))
2434 return 0;
2435 break;
2436 case 0x30:
2437 if (!(val & AC_SUPPCM_BITS_24))
2438 return 0;
2439 break;
2440 case 0x40:
2441 if (!(val & AC_SUPPCM_BITS_32))
2442 return 0;
2443 break;
2444 default:
2445 return 0;
2447 } else {
2448 /* FIXME: check for float32 and AC3? */
2451 return 1;
2453 EXPORT_SYMBOL_HDA(snd_hda_is_supported_format);
2456 * PCM stuff
2458 static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
2459 struct hda_codec *codec,
2460 struct snd_pcm_substream *substream)
2462 return 0;
2465 static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
2466 struct hda_codec *codec,
2467 unsigned int stream_tag,
2468 unsigned int format,
2469 struct snd_pcm_substream *substream)
2471 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
2472 return 0;
2475 static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
2476 struct hda_codec *codec,
2477 struct snd_pcm_substream *substream)
2479 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
2480 return 0;
2483 static int set_pcm_default_values(struct hda_codec *codec,
2484 struct hda_pcm_stream *info)
2486 /* query support PCM information from the given NID */
2487 if (info->nid && (!info->rates || !info->formats)) {
2488 snd_hda_query_supported_pcm(codec, info->nid,
2489 info->rates ? NULL : &info->rates,
2490 info->formats ? NULL : &info->formats,
2491 info->maxbps ? NULL : &info->maxbps);
2493 if (info->ops.open == NULL)
2494 info->ops.open = hda_pcm_default_open_close;
2495 if (info->ops.close == NULL)
2496 info->ops.close = hda_pcm_default_open_close;
2497 if (info->ops.prepare == NULL) {
2498 if (snd_BUG_ON(!info->nid))
2499 return -EINVAL;
2500 info->ops.prepare = hda_pcm_default_prepare;
2502 if (info->ops.cleanup == NULL) {
2503 if (snd_BUG_ON(!info->nid))
2504 return -EINVAL;
2505 info->ops.cleanup = hda_pcm_default_cleanup;
2507 return 0;
2511 * get the empty PCM device number to assign
2513 static int get_empty_pcm_device(struct hda_bus *bus, int type)
2515 static const char *dev_name[HDA_PCM_NTYPES] = {
2516 "Audio", "SPDIF", "HDMI", "Modem"
2518 /* starting device index for each PCM type */
2519 static int dev_idx[HDA_PCM_NTYPES] = {
2520 [HDA_PCM_TYPE_AUDIO] = 0,
2521 [HDA_PCM_TYPE_SPDIF] = 1,
2522 [HDA_PCM_TYPE_HDMI] = 3,
2523 [HDA_PCM_TYPE_MODEM] = 6
2525 /* normal audio device indices; not linear to keep compatibility */
2526 static int audio_idx[4] = { 0, 2, 4, 5 };
2527 int i, dev;
2529 switch (type) {
2530 case HDA_PCM_TYPE_AUDIO:
2531 for (i = 0; i < ARRAY_SIZE(audio_idx); i++) {
2532 dev = audio_idx[i];
2533 if (!test_bit(dev, bus->pcm_dev_bits))
2534 break;
2536 if (i >= ARRAY_SIZE(audio_idx)) {
2537 snd_printk(KERN_WARNING "Too many audio devices\n");
2538 return -EAGAIN;
2540 break;
2541 case HDA_PCM_TYPE_SPDIF:
2542 case HDA_PCM_TYPE_HDMI:
2543 case HDA_PCM_TYPE_MODEM:
2544 dev = dev_idx[type];
2545 if (test_bit(dev, bus->pcm_dev_bits)) {
2546 snd_printk(KERN_WARNING "%s already defined\n",
2547 dev_name[type]);
2548 return -EAGAIN;
2550 break;
2551 default:
2552 snd_printk(KERN_WARNING "Invalid PCM type %d\n", type);
2553 return -EINVAL;
2555 set_bit(dev, bus->pcm_dev_bits);
2556 return dev;
2560 * attach a new PCM stream
2562 static int snd_hda_attach_pcm(struct hda_codec *codec, struct hda_pcm *pcm)
2564 struct hda_bus *bus = codec->bus;
2565 struct hda_pcm_stream *info;
2566 int stream, err;
2568 if (snd_BUG_ON(!pcm->name))
2569 return -EINVAL;
2570 for (stream = 0; stream < 2; stream++) {
2571 info = &pcm->stream[stream];
2572 if (info->substreams) {
2573 err = set_pcm_default_values(codec, info);
2574 if (err < 0)
2575 return err;
2578 return bus->ops.attach_pcm(bus, codec, pcm);
2581 /* assign all PCMs of the given codec */
2582 int snd_hda_codec_build_pcms(struct hda_codec *codec)
2584 unsigned int pcm;
2585 int err;
2587 if (!codec->num_pcms) {
2588 if (!codec->patch_ops.build_pcms)
2589 return 0;
2590 err = codec->patch_ops.build_pcms(codec);
2591 if (err < 0)
2592 return err;
2594 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
2595 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
2596 int dev;
2598 if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
2599 return 0; /* no substreams assigned */
2601 if (!cpcm->pcm) {
2602 dev = get_empty_pcm_device(codec->bus, cpcm->pcm_type);
2603 if (dev < 0)
2604 return 0;
2605 cpcm->device = dev;
2606 err = snd_hda_attach_pcm(codec, cpcm);
2607 if (err < 0)
2608 return err;
2611 return 0;
2615 * snd_hda_build_pcms - build PCM information
2616 * @bus: the BUS
2618 * Create PCM information for each codec included in the bus.
2620 * The build_pcms codec patch is requested to set up codec->num_pcms and
2621 * codec->pcm_info properly. The array is referred by the top-level driver
2622 * to create its PCM instances.
2623 * The allocated codec->pcm_info should be released in codec->patch_ops.free
2624 * callback.
2626 * At least, substreams, channels_min and channels_max must be filled for
2627 * each stream. substreams = 0 indicates that the stream doesn't exist.
2628 * When rates and/or formats are zero, the supported values are queried
2629 * from the given nid. The nid is used also by the default ops.prepare
2630 * and ops.cleanup callbacks.
2632 * The driver needs to call ops.open in its open callback. Similarly,
2633 * ops.close is supposed to be called in the close callback.
2634 * ops.prepare should be called in the prepare or hw_params callback
2635 * with the proper parameters for set up.
2636 * ops.cleanup should be called in hw_free for clean up of streams.
2638 * This function returns 0 if successfull, or a negative error code.
2640 int __devinit snd_hda_build_pcms(struct hda_bus *bus)
2642 struct hda_codec *codec;
2644 list_for_each_entry(codec, &bus->codec_list, list) {
2645 int err = snd_hda_codec_build_pcms(codec);
2646 if (err < 0)
2647 return err;
2649 return 0;
2651 EXPORT_SYMBOL_HDA(snd_hda_build_pcms);
2654 * snd_hda_check_board_config - compare the current codec with the config table
2655 * @codec: the HDA codec
2656 * @num_configs: number of config enums
2657 * @models: array of model name strings
2658 * @tbl: configuration table, terminated by null entries
2660 * Compares the modelname or PCI subsystem id of the current codec with the
2661 * given configuration table. If a matching entry is found, returns its
2662 * config value (supposed to be 0 or positive).
2664 * If no entries are matching, the function returns a negative value.
2666 int snd_hda_check_board_config(struct hda_codec *codec,
2667 int num_configs, const char **models,
2668 const struct snd_pci_quirk *tbl)
2670 if (codec->modelname && models) {
2671 int i;
2672 for (i = 0; i < num_configs; i++) {
2673 if (models[i] &&
2674 !strcmp(codec->modelname, models[i])) {
2675 snd_printd(KERN_INFO "hda_codec: model '%s' is "
2676 "selected\n", models[i]);
2677 return i;
2682 if (!codec->bus->pci || !tbl)
2683 return -1;
2685 tbl = snd_pci_quirk_lookup(codec->bus->pci, tbl);
2686 if (!tbl)
2687 return -1;
2688 if (tbl->value >= 0 && tbl->value < num_configs) {
2689 #ifdef CONFIG_SND_DEBUG_VERBOSE
2690 char tmp[10];
2691 const char *model = NULL;
2692 if (models)
2693 model = models[tbl->value];
2694 if (!model) {
2695 sprintf(tmp, "#%d", tbl->value);
2696 model = tmp;
2698 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
2699 "for config %x:%x (%s)\n",
2700 model, tbl->subvendor, tbl->subdevice,
2701 (tbl->name ? tbl->name : "Unknown device"));
2702 #endif
2703 return tbl->value;
2705 return -1;
2707 EXPORT_SYMBOL_HDA(snd_hda_check_board_config);
2710 * snd_hda_add_new_ctls - create controls from the array
2711 * @codec: the HDA codec
2712 * @knew: the array of struct snd_kcontrol_new
2714 * This helper function creates and add new controls in the given array.
2715 * The array must be terminated with an empty entry as terminator.
2717 * Returns 0 if successful, or a negative error code.
2719 int snd_hda_add_new_ctls(struct hda_codec *codec, struct snd_kcontrol_new *knew)
2721 int err;
2723 for (; knew->name; knew++) {
2724 struct snd_kcontrol *kctl;
2725 kctl = snd_ctl_new1(knew, codec);
2726 if (!kctl)
2727 return -ENOMEM;
2728 err = snd_hda_ctl_add(codec, kctl);
2729 if (err < 0) {
2730 if (!codec->addr)
2731 return err;
2732 kctl = snd_ctl_new1(knew, codec);
2733 if (!kctl)
2734 return -ENOMEM;
2735 kctl->id.device = codec->addr;
2736 err = snd_hda_ctl_add(codec, kctl);
2737 if (err < 0)
2738 return err;
2741 return 0;
2743 EXPORT_SYMBOL_HDA(snd_hda_add_new_ctls);
2745 #ifdef CONFIG_SND_HDA_POWER_SAVE
2746 static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
2747 unsigned int power_state);
2749 static void hda_power_work(struct work_struct *work)
2751 struct hda_codec *codec =
2752 container_of(work, struct hda_codec, power_work.work);
2753 struct hda_bus *bus = codec->bus;
2755 if (!codec->power_on || codec->power_count) {
2756 codec->power_transition = 0;
2757 return;
2760 hda_call_codec_suspend(codec);
2761 if (bus->ops.pm_notify)
2762 bus->ops.pm_notify(bus);
2765 static void hda_keep_power_on(struct hda_codec *codec)
2767 codec->power_count++;
2768 codec->power_on = 1;
2771 void snd_hda_power_up(struct hda_codec *codec)
2773 struct hda_bus *bus = codec->bus;
2775 codec->power_count++;
2776 if (codec->power_on || codec->power_transition)
2777 return;
2779 codec->power_on = 1;
2780 if (bus->ops.pm_notify)
2781 bus->ops.pm_notify(bus);
2782 hda_call_codec_resume(codec);
2783 cancel_delayed_work(&codec->power_work);
2784 codec->power_transition = 0;
2786 EXPORT_SYMBOL_HDA(snd_hda_power_up);
2788 #define power_save(codec) \
2789 ((codec)->bus->power_save ? *(codec)->bus->power_save : 0)
2791 #define power_save(codec) \
2792 ((codec)->bus->power_save ? *(codec)->bus->power_save : 0)
2794 void snd_hda_power_down(struct hda_codec *codec)
2796 --codec->power_count;
2797 if (!codec->power_on || codec->power_count || codec->power_transition)
2798 return;
2799 if (power_save(codec)) {
2800 codec->power_transition = 1; /* avoid reentrance */
2801 schedule_delayed_work(&codec->power_work,
2802 msecs_to_jiffies(power_save(codec) * 1000));
2805 EXPORT_SYMBOL_HDA(snd_hda_power_down);
2807 int snd_hda_check_amp_list_power(struct hda_codec *codec,
2808 struct hda_loopback_check *check,
2809 hda_nid_t nid)
2811 struct hda_amp_list *p;
2812 int ch, v;
2814 if (!check->amplist)
2815 return 0;
2816 for (p = check->amplist; p->nid; p++) {
2817 if (p->nid == nid)
2818 break;
2820 if (!p->nid)
2821 return 0; /* nothing changed */
2823 for (p = check->amplist; p->nid; p++) {
2824 for (ch = 0; ch < 2; ch++) {
2825 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
2826 p->idx);
2827 if (!(v & HDA_AMP_MUTE) && v > 0) {
2828 if (!check->power_on) {
2829 check->power_on = 1;
2830 snd_hda_power_up(codec);
2832 return 1;
2836 if (check->power_on) {
2837 check->power_on = 0;
2838 snd_hda_power_down(codec);
2840 return 0;
2842 EXPORT_SYMBOL_HDA(snd_hda_check_amp_list_power);
2843 #endif
2846 * Channel mode helper
2848 int snd_hda_ch_mode_info(struct hda_codec *codec,
2849 struct snd_ctl_elem_info *uinfo,
2850 const struct hda_channel_mode *chmode,
2851 int num_chmodes)
2853 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2854 uinfo->count = 1;
2855 uinfo->value.enumerated.items = num_chmodes;
2856 if (uinfo->value.enumerated.item >= num_chmodes)
2857 uinfo->value.enumerated.item = num_chmodes - 1;
2858 sprintf(uinfo->value.enumerated.name, "%dch",
2859 chmode[uinfo->value.enumerated.item].channels);
2860 return 0;
2862 EXPORT_SYMBOL_HDA(snd_hda_ch_mode_info);
2864 int snd_hda_ch_mode_get(struct hda_codec *codec,
2865 struct snd_ctl_elem_value *ucontrol,
2866 const struct hda_channel_mode *chmode,
2867 int num_chmodes,
2868 int max_channels)
2870 int i;
2872 for (i = 0; i < num_chmodes; i++) {
2873 if (max_channels == chmode[i].channels) {
2874 ucontrol->value.enumerated.item[0] = i;
2875 break;
2878 return 0;
2880 EXPORT_SYMBOL_HDA(snd_hda_ch_mode_get);
2882 int snd_hda_ch_mode_put(struct hda_codec *codec,
2883 struct snd_ctl_elem_value *ucontrol,
2884 const struct hda_channel_mode *chmode,
2885 int num_chmodes,
2886 int *max_channelsp)
2888 unsigned int mode;
2890 mode = ucontrol->value.enumerated.item[0];
2891 if (mode >= num_chmodes)
2892 return -EINVAL;
2893 if (*max_channelsp == chmode[mode].channels)
2894 return 0;
2895 /* change the current channel setting */
2896 *max_channelsp = chmode[mode].channels;
2897 if (chmode[mode].sequence)
2898 snd_hda_sequence_write_cache(codec, chmode[mode].sequence);
2899 return 1;
2901 EXPORT_SYMBOL_HDA(snd_hda_ch_mode_put);
2904 * input MUX helper
2906 int snd_hda_input_mux_info(const struct hda_input_mux *imux,
2907 struct snd_ctl_elem_info *uinfo)
2909 unsigned int index;
2911 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2912 uinfo->count = 1;
2913 uinfo->value.enumerated.items = imux->num_items;
2914 if (!imux->num_items)
2915 return 0;
2916 index = uinfo->value.enumerated.item;
2917 if (index >= imux->num_items)
2918 index = imux->num_items - 1;
2919 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
2920 return 0;
2922 EXPORT_SYMBOL_HDA(snd_hda_input_mux_info);
2924 int snd_hda_input_mux_put(struct hda_codec *codec,
2925 const struct hda_input_mux *imux,
2926 struct snd_ctl_elem_value *ucontrol,
2927 hda_nid_t nid,
2928 unsigned int *cur_val)
2930 unsigned int idx;
2932 if (!imux->num_items)
2933 return 0;
2934 idx = ucontrol->value.enumerated.item[0];
2935 if (idx >= imux->num_items)
2936 idx = imux->num_items - 1;
2937 if (*cur_val == idx)
2938 return 0;
2939 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
2940 imux->items[idx].index);
2941 *cur_val = idx;
2942 return 1;
2944 EXPORT_SYMBOL_HDA(snd_hda_input_mux_put);
2948 * Multi-channel / digital-out PCM helper functions
2951 /* setup SPDIF output stream */
2952 static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
2953 unsigned int stream_tag, unsigned int format)
2955 /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */
2956 if (codec->spdif_status_reset && (codec->spdif_ctls & AC_DIG1_ENABLE))
2957 set_dig_out_convert(codec, nid,
2958 codec->spdif_ctls & ~AC_DIG1_ENABLE & 0xff,
2959 -1);
2960 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
2961 if (codec->slave_dig_outs) {
2962 hda_nid_t *d;
2963 for (d = codec->slave_dig_outs; *d; d++)
2964 snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
2965 format);
2967 /* turn on again (if needed) */
2968 if (codec->spdif_status_reset && (codec->spdif_ctls & AC_DIG1_ENABLE))
2969 set_dig_out_convert(codec, nid,
2970 codec->spdif_ctls & 0xff, -1);
2973 static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
2975 snd_hda_codec_cleanup_stream(codec, nid);
2976 if (codec->slave_dig_outs) {
2977 hda_nid_t *d;
2978 for (d = codec->slave_dig_outs; *d; d++)
2979 snd_hda_codec_cleanup_stream(codec, *d);
2984 * open the digital out in the exclusive mode
2986 int snd_hda_multi_out_dig_open(struct hda_codec *codec,
2987 struct hda_multi_out *mout)
2989 mutex_lock(&codec->spdif_mutex);
2990 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
2991 /* already opened as analog dup; reset it once */
2992 cleanup_dig_out_stream(codec, mout->dig_out_nid);
2993 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
2994 mutex_unlock(&codec->spdif_mutex);
2995 return 0;
2997 EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_open);
2999 int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
3000 struct hda_multi_out *mout,
3001 unsigned int stream_tag,
3002 unsigned int format,
3003 struct snd_pcm_substream *substream)
3005 mutex_lock(&codec->spdif_mutex);
3006 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
3007 mutex_unlock(&codec->spdif_mutex);
3008 return 0;
3010 EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_prepare);
3013 * release the digital out
3015 int snd_hda_multi_out_dig_close(struct hda_codec *codec,
3016 struct hda_multi_out *mout)
3018 mutex_lock(&codec->spdif_mutex);
3019 mout->dig_out_used = 0;
3020 mutex_unlock(&codec->spdif_mutex);
3021 return 0;
3023 EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_close);
3026 * set up more restrictions for analog out
3028 int snd_hda_multi_out_analog_open(struct hda_codec *codec,
3029 struct hda_multi_out *mout,
3030 struct snd_pcm_substream *substream,
3031 struct hda_pcm_stream *hinfo)
3033 struct snd_pcm_runtime *runtime = substream->runtime;
3034 runtime->hw.channels_max = mout->max_channels;
3035 if (mout->dig_out_nid) {
3036 if (!mout->analog_rates) {
3037 mout->analog_rates = hinfo->rates;
3038 mout->analog_formats = hinfo->formats;
3039 mout->analog_maxbps = hinfo->maxbps;
3040 } else {
3041 runtime->hw.rates = mout->analog_rates;
3042 runtime->hw.formats = mout->analog_formats;
3043 hinfo->maxbps = mout->analog_maxbps;
3045 if (!mout->spdif_rates) {
3046 snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
3047 &mout->spdif_rates,
3048 &mout->spdif_formats,
3049 &mout->spdif_maxbps);
3051 mutex_lock(&codec->spdif_mutex);
3052 if (mout->share_spdif) {
3053 runtime->hw.rates &= mout->spdif_rates;
3054 runtime->hw.formats &= mout->spdif_formats;
3055 if (mout->spdif_maxbps < hinfo->maxbps)
3056 hinfo->maxbps = mout->spdif_maxbps;
3058 mutex_unlock(&codec->spdif_mutex);
3060 return snd_pcm_hw_constraint_step(substream->runtime, 0,
3061 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
3063 EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_open);
3066 * set up the i/o for analog out
3067 * when the digital out is available, copy the front out to digital out, too.
3069 int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
3070 struct hda_multi_out *mout,
3071 unsigned int stream_tag,
3072 unsigned int format,
3073 struct snd_pcm_substream *substream)
3075 hda_nid_t *nids = mout->dac_nids;
3076 int chs = substream->runtime->channels;
3077 int i;
3079 mutex_lock(&codec->spdif_mutex);
3080 if (mout->dig_out_nid && mout->share_spdif &&
3081 mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
3082 if (chs == 2 &&
3083 snd_hda_is_supported_format(codec, mout->dig_out_nid,
3084 format) &&
3085 !(codec->spdif_status & IEC958_AES0_NONAUDIO)) {
3086 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
3087 setup_dig_out_stream(codec, mout->dig_out_nid,
3088 stream_tag, format);
3089 } else {
3090 mout->dig_out_used = 0;
3091 cleanup_dig_out_stream(codec, mout->dig_out_nid);
3094 mutex_unlock(&codec->spdif_mutex);
3096 /* front */
3097 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
3098 0, format);
3099 if (!mout->no_share_stream &&
3100 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
3101 /* headphone out will just decode front left/right (stereo) */
3102 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
3103 0, format);
3104 /* extra outputs copied from front */
3105 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
3106 if (!mout->no_share_stream && mout->extra_out_nid[i])
3107 snd_hda_codec_setup_stream(codec,
3108 mout->extra_out_nid[i],
3109 stream_tag, 0, format);
3111 /* surrounds */
3112 for (i = 1; i < mout->num_dacs; i++) {
3113 if (chs >= (i + 1) * 2) /* independent out */
3114 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
3115 i * 2, format);
3116 else if (!mout->no_share_stream) /* copy front */
3117 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
3118 0, format);
3120 return 0;
3122 EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_prepare);
3125 * clean up the setting for analog out
3127 int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
3128 struct hda_multi_out *mout)
3130 hda_nid_t *nids = mout->dac_nids;
3131 int i;
3133 for (i = 0; i < mout->num_dacs; i++)
3134 snd_hda_codec_cleanup_stream(codec, nids[i]);
3135 if (mout->hp_nid)
3136 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
3137 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
3138 if (mout->extra_out_nid[i])
3139 snd_hda_codec_cleanup_stream(codec,
3140 mout->extra_out_nid[i]);
3141 mutex_lock(&codec->spdif_mutex);
3142 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
3143 cleanup_dig_out_stream(codec, mout->dig_out_nid);
3144 mout->dig_out_used = 0;
3146 mutex_unlock(&codec->spdif_mutex);
3147 return 0;
3149 EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_cleanup);
3152 * Helper for automatic pin configuration
3155 static int is_in_nid_list(hda_nid_t nid, hda_nid_t *list)
3157 for (; *list; list++)
3158 if (*list == nid)
3159 return 1;
3160 return 0;
3165 * Sort an associated group of pins according to their sequence numbers.
3167 static void sort_pins_by_sequence(hda_nid_t * pins, short * sequences,
3168 int num_pins)
3170 int i, j;
3171 short seq;
3172 hda_nid_t nid;
3174 for (i = 0; i < num_pins; i++) {
3175 for (j = i + 1; j < num_pins; j++) {
3176 if (sequences[i] > sequences[j]) {
3177 seq = sequences[i];
3178 sequences[i] = sequences[j];
3179 sequences[j] = seq;
3180 nid = pins[i];
3181 pins[i] = pins[j];
3182 pins[j] = nid;
3190 * Parse all pin widgets and store the useful pin nids to cfg
3192 * The number of line-outs or any primary output is stored in line_outs,
3193 * and the corresponding output pins are assigned to line_out_pins[],
3194 * in the order of front, rear, CLFE, side, ...
3196 * If more extra outputs (speaker and headphone) are found, the pins are
3197 * assisnged to hp_pins[] and speaker_pins[], respectively. If no line-out jack
3198 * is detected, one of speaker of HP pins is assigned as the primary
3199 * output, i.e. to line_out_pins[0]. So, line_outs is always positive
3200 * if any analog output exists.
3202 * The analog input pins are assigned to input_pins array.
3203 * The digital input/output pins are assigned to dig_in_pin and dig_out_pin,
3204 * respectively.
3206 int snd_hda_parse_pin_def_config(struct hda_codec *codec,
3207 struct auto_pin_cfg *cfg,
3208 hda_nid_t *ignore_nids)
3210 hda_nid_t nid, end_nid;
3211 short seq, assoc_line_out, assoc_speaker;
3212 short sequences_line_out[ARRAY_SIZE(cfg->line_out_pins)];
3213 short sequences_speaker[ARRAY_SIZE(cfg->speaker_pins)];
3214 short sequences_hp[ARRAY_SIZE(cfg->hp_pins)];
3216 memset(cfg, 0, sizeof(*cfg));
3218 memset(sequences_line_out, 0, sizeof(sequences_line_out));
3219 memset(sequences_speaker, 0, sizeof(sequences_speaker));
3220 memset(sequences_hp, 0, sizeof(sequences_hp));
3221 assoc_line_out = assoc_speaker = 0;
3223 end_nid = codec->start_nid + codec->num_nodes;
3224 for (nid = codec->start_nid; nid < end_nid; nid++) {
3225 unsigned int wid_caps = get_wcaps(codec, nid);
3226 unsigned int wid_type =
3227 (wid_caps & AC_WCAP_TYPE) >> AC_WCAP_TYPE_SHIFT;
3228 unsigned int def_conf;
3229 short assoc, loc;
3231 /* read all default configuration for pin complex */
3232 if (wid_type != AC_WID_PIN)
3233 continue;
3234 /* ignore the given nids (e.g. pc-beep returns error) */
3235 if (ignore_nids && is_in_nid_list(nid, ignore_nids))
3236 continue;
3238 def_conf = snd_hda_codec_read(codec, nid, 0,
3239 AC_VERB_GET_CONFIG_DEFAULT, 0);
3240 if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE)
3241 continue;
3242 loc = get_defcfg_location(def_conf);
3243 switch (get_defcfg_device(def_conf)) {
3244 case AC_JACK_LINE_OUT:
3245 seq = get_defcfg_sequence(def_conf);
3246 assoc = get_defcfg_association(def_conf);
3248 if (!(wid_caps & AC_WCAP_STEREO))
3249 if (!cfg->mono_out_pin)
3250 cfg->mono_out_pin = nid;
3251 if (!assoc)
3252 continue;
3253 if (!assoc_line_out)
3254 assoc_line_out = assoc;
3255 else if (assoc_line_out != assoc)
3256 continue;
3257 if (cfg->line_outs >= ARRAY_SIZE(cfg->line_out_pins))
3258 continue;
3259 cfg->line_out_pins[cfg->line_outs] = nid;
3260 sequences_line_out[cfg->line_outs] = seq;
3261 cfg->line_outs++;
3262 break;
3263 case AC_JACK_SPEAKER:
3264 seq = get_defcfg_sequence(def_conf);
3265 assoc = get_defcfg_association(def_conf);
3266 if (! assoc)
3267 continue;
3268 if (! assoc_speaker)
3269 assoc_speaker = assoc;
3270 else if (assoc_speaker != assoc)
3271 continue;
3272 if (cfg->speaker_outs >= ARRAY_SIZE(cfg->speaker_pins))
3273 continue;
3274 cfg->speaker_pins[cfg->speaker_outs] = nid;
3275 sequences_speaker[cfg->speaker_outs] = seq;
3276 cfg->speaker_outs++;
3277 break;
3278 case AC_JACK_HP_OUT:
3279 seq = get_defcfg_sequence(def_conf);
3280 assoc = get_defcfg_association(def_conf);
3281 if (cfg->hp_outs >= ARRAY_SIZE(cfg->hp_pins))
3282 continue;
3283 cfg->hp_pins[cfg->hp_outs] = nid;
3284 sequences_hp[cfg->hp_outs] = (assoc << 4) | seq;
3285 cfg->hp_outs++;
3286 break;
3287 case AC_JACK_MIC_IN: {
3288 int preferred, alt;
3289 if (loc == AC_JACK_LOC_FRONT) {
3290 preferred = AUTO_PIN_FRONT_MIC;
3291 alt = AUTO_PIN_MIC;
3292 } else {
3293 preferred = AUTO_PIN_MIC;
3294 alt = AUTO_PIN_FRONT_MIC;
3296 if (!cfg->input_pins[preferred])
3297 cfg->input_pins[preferred] = nid;
3298 else if (!cfg->input_pins[alt])
3299 cfg->input_pins[alt] = nid;
3300 break;
3302 case AC_JACK_LINE_IN:
3303 if (loc == AC_JACK_LOC_FRONT)
3304 cfg->input_pins[AUTO_PIN_FRONT_LINE] = nid;
3305 else
3306 cfg->input_pins[AUTO_PIN_LINE] = nid;
3307 break;
3308 case AC_JACK_CD:
3309 cfg->input_pins[AUTO_PIN_CD] = nid;
3310 break;
3311 case AC_JACK_AUX:
3312 cfg->input_pins[AUTO_PIN_AUX] = nid;
3313 break;
3314 case AC_JACK_SPDIF_OUT:
3315 cfg->dig_out_pin = nid;
3316 break;
3317 case AC_JACK_SPDIF_IN:
3318 cfg->dig_in_pin = nid;
3319 break;
3323 /* FIX-UP:
3324 * If no line-out is defined but multiple HPs are found,
3325 * some of them might be the real line-outs.
3327 if (!cfg->line_outs && cfg->hp_outs > 1) {
3328 int i = 0;
3329 while (i < cfg->hp_outs) {
3330 /* The real HPs should have the sequence 0x0f */
3331 if ((sequences_hp[i] & 0x0f) == 0x0f) {
3332 i++;
3333 continue;
3335 /* Move it to the line-out table */
3336 cfg->line_out_pins[cfg->line_outs] = cfg->hp_pins[i];
3337 sequences_line_out[cfg->line_outs] = sequences_hp[i];
3338 cfg->line_outs++;
3339 cfg->hp_outs--;
3340 memmove(cfg->hp_pins + i, cfg->hp_pins + i + 1,
3341 sizeof(cfg->hp_pins[0]) * (cfg->hp_outs - i));
3342 memmove(sequences_hp + i - 1, sequences_hp + i,
3343 sizeof(sequences_hp[0]) * (cfg->hp_outs - i));
3347 /* sort by sequence */
3348 sort_pins_by_sequence(cfg->line_out_pins, sequences_line_out,
3349 cfg->line_outs);
3350 sort_pins_by_sequence(cfg->speaker_pins, sequences_speaker,
3351 cfg->speaker_outs);
3352 sort_pins_by_sequence(cfg->hp_pins, sequences_hp,
3353 cfg->hp_outs);
3355 /* if we have only one mic, make it AUTO_PIN_MIC */
3356 if (!cfg->input_pins[AUTO_PIN_MIC] &&
3357 cfg->input_pins[AUTO_PIN_FRONT_MIC]) {
3358 cfg->input_pins[AUTO_PIN_MIC] =
3359 cfg->input_pins[AUTO_PIN_FRONT_MIC];
3360 cfg->input_pins[AUTO_PIN_FRONT_MIC] = 0;
3362 /* ditto for line-in */
3363 if (!cfg->input_pins[AUTO_PIN_LINE] &&
3364 cfg->input_pins[AUTO_PIN_FRONT_LINE]) {
3365 cfg->input_pins[AUTO_PIN_LINE] =
3366 cfg->input_pins[AUTO_PIN_FRONT_LINE];
3367 cfg->input_pins[AUTO_PIN_FRONT_LINE] = 0;
3371 * FIX-UP: if no line-outs are detected, try to use speaker or HP pin
3372 * as a primary output
3374 if (!cfg->line_outs) {
3375 if (cfg->speaker_outs) {
3376 cfg->line_outs = cfg->speaker_outs;
3377 memcpy(cfg->line_out_pins, cfg->speaker_pins,
3378 sizeof(cfg->speaker_pins));
3379 cfg->speaker_outs = 0;
3380 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
3381 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
3382 } else if (cfg->hp_outs) {
3383 cfg->line_outs = cfg->hp_outs;
3384 memcpy(cfg->line_out_pins, cfg->hp_pins,
3385 sizeof(cfg->hp_pins));
3386 cfg->hp_outs = 0;
3387 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
3388 cfg->line_out_type = AUTO_PIN_HP_OUT;
3392 /* Reorder the surround channels
3393 * ALSA sequence is front/surr/clfe/side
3394 * HDA sequence is:
3395 * 4-ch: front/surr => OK as it is
3396 * 6-ch: front/clfe/surr
3397 * 8-ch: front/clfe/rear/side|fc
3399 switch (cfg->line_outs) {
3400 case 3:
3401 case 4:
3402 nid = cfg->line_out_pins[1];
3403 cfg->line_out_pins[1] = cfg->line_out_pins[2];
3404 cfg->line_out_pins[2] = nid;
3405 break;
3409 * debug prints of the parsed results
3411 snd_printd("autoconfig: line_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
3412 cfg->line_outs, cfg->line_out_pins[0], cfg->line_out_pins[1],
3413 cfg->line_out_pins[2], cfg->line_out_pins[3],
3414 cfg->line_out_pins[4]);
3415 snd_printd(" speaker_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
3416 cfg->speaker_outs, cfg->speaker_pins[0],
3417 cfg->speaker_pins[1], cfg->speaker_pins[2],
3418 cfg->speaker_pins[3], cfg->speaker_pins[4]);
3419 snd_printd(" hp_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
3420 cfg->hp_outs, cfg->hp_pins[0],
3421 cfg->hp_pins[1], cfg->hp_pins[2],
3422 cfg->hp_pins[3], cfg->hp_pins[4]);
3423 snd_printd(" mono: mono_out=0x%x\n", cfg->mono_out_pin);
3424 snd_printd(" inputs: mic=0x%x, fmic=0x%x, line=0x%x, fline=0x%x,"
3425 " cd=0x%x, aux=0x%x\n",
3426 cfg->input_pins[AUTO_PIN_MIC],
3427 cfg->input_pins[AUTO_PIN_FRONT_MIC],
3428 cfg->input_pins[AUTO_PIN_LINE],
3429 cfg->input_pins[AUTO_PIN_FRONT_LINE],
3430 cfg->input_pins[AUTO_PIN_CD],
3431 cfg->input_pins[AUTO_PIN_AUX]);
3433 return 0;
3435 EXPORT_SYMBOL_HDA(snd_hda_parse_pin_def_config);
3437 /* labels for input pins */
3438 const char *auto_pin_cfg_labels[AUTO_PIN_LAST] = {
3439 "Mic", "Front Mic", "Line", "Front Line", "CD", "Aux"
3441 EXPORT_SYMBOL_HDA(auto_pin_cfg_labels);
3444 #ifdef CONFIG_PM
3446 * power management
3450 * snd_hda_suspend - suspend the codecs
3451 * @bus: the HDA bus
3452 * @state: suspsend state
3454 * Returns 0 if successful.
3456 int snd_hda_suspend(struct hda_bus *bus, pm_message_t state)
3458 struct hda_codec *codec;
3460 list_for_each_entry(codec, &bus->codec_list, list) {
3461 #ifdef CONFIG_SND_HDA_POWER_SAVE
3462 if (!codec->power_on)
3463 continue;
3464 #endif
3465 hda_call_codec_suspend(codec);
3467 return 0;
3469 EXPORT_SYMBOL_HDA(snd_hda_suspend);
3472 * snd_hda_resume - resume the codecs
3473 * @bus: the HDA bus
3475 * Returns 0 if successful.
3477 * This fucntion is defined only when POWER_SAVE isn't set.
3478 * In the power-save mode, the codec is resumed dynamically.
3480 int snd_hda_resume(struct hda_bus *bus)
3482 struct hda_codec *codec;
3484 list_for_each_entry(codec, &bus->codec_list, list) {
3485 if (snd_hda_codec_needs_resume(codec))
3486 hda_call_codec_resume(codec);
3488 return 0;
3490 EXPORT_SYMBOL_HDA(snd_hda_resume);
3491 #endif /* CONFIG_PM */
3494 * generic arrays
3497 /* get a new element from the given array
3498 * if it exceeds the pre-allocated array size, re-allocate the array
3500 void *snd_array_new(struct snd_array *array)
3502 if (array->used >= array->alloced) {
3503 int num = array->alloced + array->alloc_align;
3504 void *nlist;
3505 if (snd_BUG_ON(num >= 4096))
3506 return NULL;
3507 nlist = kcalloc(num + 1, array->elem_size, GFP_KERNEL);
3508 if (!nlist)
3509 return NULL;
3510 if (array->list) {
3511 memcpy(nlist, array->list,
3512 array->elem_size * array->alloced);
3513 kfree(array->list);
3515 array->list = nlist;
3516 array->alloced = num;
3518 return snd_array_elem(array, array->used++);
3520 EXPORT_SYMBOL_HDA(snd_array_new);
3522 /* free the given array elements */
3523 void snd_array_free(struct snd_array *array)
3525 kfree(array->list);
3526 array->used = 0;
3527 array->alloced = 0;
3528 array->list = NULL;
3530 EXPORT_SYMBOL_HDA(snd_array_free);
3533 * used by hda_proc.c and hda_eld.c
3535 void snd_print_pcm_rates(int pcm, char *buf, int buflen)
3537 static unsigned int rates[] = {
3538 8000, 11025, 16000, 22050, 32000, 44100, 48000, 88200,
3539 96000, 176400, 192000, 384000
3541 int i, j;
3543 for (i = 0, j = 0; i < ARRAY_SIZE(rates); i++)
3544 if (pcm & (1 << i))
3545 j += snprintf(buf + j, buflen - j, " %d", rates[i]);
3547 buf[j] = '\0'; /* necessary when j == 0 */
3549 EXPORT_SYMBOL_HDA(snd_print_pcm_rates);
3551 void snd_print_pcm_bits(int pcm, char *buf, int buflen)
3553 static unsigned int bits[] = { 8, 16, 20, 24, 32 };
3554 int i, j;
3556 for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
3557 if (pcm & (AC_SUPPCM_BITS_8 << i))
3558 j += snprintf(buf + j, buflen - j, " %d", bits[i]);
3560 buf[j] = '\0'; /* necessary when j == 0 */
3562 EXPORT_SYMBOL_HDA(snd_print_pcm_bits);
3564 MODULE_DESCRIPTION("HDA codec core");
3565 MODULE_LICENSE("GPL");