ALSA: hda - proc - introduce Control: lines to show mixer<->NID assignment
[firewire-audio.git] / sound / pci / hda / hda_codec.c
blob1ed1d88e18347cbe2d0e524cde52410a28f77ae7
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 "hda_beep.h"
34 #include <sound/hda_hwdep.h>
37 * vendor / preset table
40 struct hda_vendor_id {
41 unsigned int id;
42 const char *name;
45 /* codec vendor labels */
46 static struct hda_vendor_id hda_vendor_ids[] = {
47 { 0x1002, "ATI" },
48 { 0x1013, "Cirrus Logic" },
49 { 0x1057, "Motorola" },
50 { 0x1095, "Silicon Image" },
51 { 0x10de, "Nvidia" },
52 { 0x10ec, "Realtek" },
53 { 0x1102, "Creative" },
54 { 0x1106, "VIA" },
55 { 0x111d, "IDT" },
56 { 0x11c1, "LSI" },
57 { 0x11d4, "Analog Devices" },
58 { 0x13f6, "C-Media" },
59 { 0x14f1, "Conexant" },
60 { 0x17e8, "Chrontel" },
61 { 0x1854, "LG" },
62 { 0x1aec, "Wolfson Microelectronics" },
63 { 0x434d, "C-Media" },
64 { 0x8086, "Intel" },
65 { 0x8384, "SigmaTel" },
66 {} /* terminator */
69 static DEFINE_MUTEX(preset_mutex);
70 static LIST_HEAD(hda_preset_tables);
72 int snd_hda_add_codec_preset(struct hda_codec_preset_list *preset)
74 mutex_lock(&preset_mutex);
75 list_add_tail(&preset->list, &hda_preset_tables);
76 mutex_unlock(&preset_mutex);
77 return 0;
79 EXPORT_SYMBOL_HDA(snd_hda_add_codec_preset);
81 int snd_hda_delete_codec_preset(struct hda_codec_preset_list *preset)
83 mutex_lock(&preset_mutex);
84 list_del(&preset->list);
85 mutex_unlock(&preset_mutex);
86 return 0;
88 EXPORT_SYMBOL_HDA(snd_hda_delete_codec_preset);
90 #ifdef CONFIG_SND_HDA_POWER_SAVE
91 static void hda_power_work(struct work_struct *work);
92 static void hda_keep_power_on(struct hda_codec *codec);
93 #else
94 static inline void hda_keep_power_on(struct hda_codec *codec) {}
95 #endif
97 const char *snd_hda_get_jack_location(u32 cfg)
99 static char *bases[7] = {
100 "N/A", "Rear", "Front", "Left", "Right", "Top", "Bottom",
102 static unsigned char specials_idx[] = {
103 0x07, 0x08,
104 0x17, 0x18, 0x19,
105 0x37, 0x38
107 static char *specials[] = {
108 "Rear Panel", "Drive Bar",
109 "Riser", "HDMI", "ATAPI",
110 "Mobile-In", "Mobile-Out"
112 int i;
113 cfg = (cfg & AC_DEFCFG_LOCATION) >> AC_DEFCFG_LOCATION_SHIFT;
114 if ((cfg & 0x0f) < 7)
115 return bases[cfg & 0x0f];
116 for (i = 0; i < ARRAY_SIZE(specials_idx); i++) {
117 if (cfg == specials_idx[i])
118 return specials[i];
120 return "UNKNOWN";
122 EXPORT_SYMBOL_HDA(snd_hda_get_jack_location);
124 const char *snd_hda_get_jack_connectivity(u32 cfg)
126 static char *jack_locations[4] = { "Ext", "Int", "Sep", "Oth" };
128 return jack_locations[(cfg >> (AC_DEFCFG_LOCATION_SHIFT + 4)) & 3];
130 EXPORT_SYMBOL_HDA(snd_hda_get_jack_connectivity);
132 const char *snd_hda_get_jack_type(u32 cfg)
134 static char *jack_types[16] = {
135 "Line Out", "Speaker", "HP Out", "CD",
136 "SPDIF Out", "Digital Out", "Modem Line", "Modem Hand",
137 "Line In", "Aux", "Mic", "Telephony",
138 "SPDIF In", "Digitial In", "Reserved", "Other"
141 return jack_types[(cfg & AC_DEFCFG_DEVICE)
142 >> AC_DEFCFG_DEVICE_SHIFT];
144 EXPORT_SYMBOL_HDA(snd_hda_get_jack_type);
147 * Compose a 32bit command word to be sent to the HD-audio controller
149 static inline unsigned int
150 make_codec_cmd(struct hda_codec *codec, hda_nid_t nid, int direct,
151 unsigned int verb, unsigned int parm)
153 u32 val;
155 if ((codec->addr & ~0xf) || (direct & ~1) || (nid & ~0x7f) ||
156 (verb & ~0xfff) || (parm & ~0xffff)) {
157 printk(KERN_ERR "hda-codec: out of range cmd %x:%x:%x:%x:%x\n",
158 codec->addr, direct, nid, verb, parm);
159 return ~0;
162 val = (u32)codec->addr << 28;
163 val |= (u32)direct << 27;
164 val |= (u32)nid << 20;
165 val |= verb << 8;
166 val |= parm;
167 return val;
171 * Send and receive a verb
173 static int codec_exec_verb(struct hda_codec *codec, unsigned int cmd,
174 unsigned int *res)
176 struct hda_bus *bus = codec->bus;
177 int err;
179 if (cmd == ~0)
180 return -1;
182 if (res)
183 *res = -1;
184 again:
185 snd_hda_power_up(codec);
186 mutex_lock(&bus->cmd_mutex);
187 err = bus->ops.command(bus, cmd);
188 if (!err && res)
189 *res = bus->ops.get_response(bus, codec->addr);
190 mutex_unlock(&bus->cmd_mutex);
191 snd_hda_power_down(codec);
192 if (res && *res == -1 && bus->rirb_error) {
193 if (bus->response_reset) {
194 snd_printd("hda_codec: resetting BUS due to "
195 "fatal communication error\n");
196 bus->ops.bus_reset(bus);
198 goto again;
200 /* clear reset-flag when the communication gets recovered */
201 if (!err)
202 bus->response_reset = 0;
203 return err;
207 * snd_hda_codec_read - send a command and get the response
208 * @codec: the HDA codec
209 * @nid: NID to send the command
210 * @direct: direct flag
211 * @verb: the verb to send
212 * @parm: the parameter for the verb
214 * Send a single command and read the corresponding response.
216 * Returns the obtained response value, or -1 for an error.
218 unsigned int snd_hda_codec_read(struct hda_codec *codec, hda_nid_t nid,
219 int direct,
220 unsigned int verb, unsigned int parm)
222 unsigned cmd = make_codec_cmd(codec, nid, direct, verb, parm);
223 unsigned int res;
224 codec_exec_verb(codec, cmd, &res);
225 return res;
227 EXPORT_SYMBOL_HDA(snd_hda_codec_read);
230 * snd_hda_codec_write - send a single command without waiting for response
231 * @codec: the HDA codec
232 * @nid: NID to send the command
233 * @direct: direct flag
234 * @verb: the verb to send
235 * @parm: the parameter for the verb
237 * Send a single command without waiting for response.
239 * Returns 0 if successful, or a negative error code.
241 int snd_hda_codec_write(struct hda_codec *codec, hda_nid_t nid, int direct,
242 unsigned int verb, unsigned int parm)
244 unsigned int cmd = make_codec_cmd(codec, nid, direct, verb, parm);
245 unsigned int res;
246 return codec_exec_verb(codec, cmd,
247 codec->bus->sync_write ? &res : NULL);
249 EXPORT_SYMBOL_HDA(snd_hda_codec_write);
252 * snd_hda_sequence_write - sequence writes
253 * @codec: the HDA codec
254 * @seq: VERB array to send
256 * Send the commands sequentially from the given array.
257 * The array must be terminated with NID=0.
259 void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq)
261 for (; seq->nid; seq++)
262 snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param);
264 EXPORT_SYMBOL_HDA(snd_hda_sequence_write);
267 * snd_hda_get_sub_nodes - get the range of sub nodes
268 * @codec: the HDA codec
269 * @nid: NID to parse
270 * @start_id: the pointer to store the start NID
272 * Parse the NID and store the start NID of its sub-nodes.
273 * Returns the number of sub-nodes.
275 int snd_hda_get_sub_nodes(struct hda_codec *codec, hda_nid_t nid,
276 hda_nid_t *start_id)
278 unsigned int parm;
280 parm = snd_hda_param_read(codec, nid, AC_PAR_NODE_COUNT);
281 if (parm == -1)
282 return 0;
283 *start_id = (parm >> 16) & 0x7fff;
284 return (int)(parm & 0x7fff);
286 EXPORT_SYMBOL_HDA(snd_hda_get_sub_nodes);
289 * snd_hda_get_connections - get connection list
290 * @codec: the HDA codec
291 * @nid: NID to parse
292 * @conn_list: connection list array
293 * @max_conns: max. number of connections to store
295 * Parses the connection list of the given widget and stores the list
296 * of NIDs.
298 * Returns the number of connections, or a negative error code.
300 int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
301 hda_nid_t *conn_list, int max_conns)
303 unsigned int parm;
304 int i, conn_len, conns;
305 unsigned int shift, num_elems, mask;
306 unsigned int wcaps;
307 hda_nid_t prev_nid;
309 if (snd_BUG_ON(!conn_list || max_conns <= 0))
310 return -EINVAL;
312 wcaps = get_wcaps(codec, nid);
313 if (!(wcaps & AC_WCAP_CONN_LIST) &&
314 get_wcaps_type(wcaps) != AC_WID_VOL_KNB) {
315 snd_printk(KERN_WARNING "hda_codec: "
316 "connection list not available for 0x%x\n", nid);
317 return -EINVAL;
320 parm = snd_hda_param_read(codec, nid, AC_PAR_CONNLIST_LEN);
321 if (parm & AC_CLIST_LONG) {
322 /* long form */
323 shift = 16;
324 num_elems = 2;
325 } else {
326 /* short form */
327 shift = 8;
328 num_elems = 4;
330 conn_len = parm & AC_CLIST_LENGTH;
331 mask = (1 << (shift-1)) - 1;
333 if (!conn_len)
334 return 0; /* no connection */
336 if (conn_len == 1) {
337 /* single connection */
338 parm = snd_hda_codec_read(codec, nid, 0,
339 AC_VERB_GET_CONNECT_LIST, 0);
340 if (parm == -1 && codec->bus->rirb_error)
341 return -EIO;
342 conn_list[0] = parm & mask;
343 return 1;
346 /* multi connection */
347 conns = 0;
348 prev_nid = 0;
349 for (i = 0; i < conn_len; i++) {
350 int range_val;
351 hda_nid_t val, n;
353 if (i % num_elems == 0) {
354 parm = snd_hda_codec_read(codec, nid, 0,
355 AC_VERB_GET_CONNECT_LIST, i);
356 if (parm == -1 && codec->bus->rirb_error)
357 return -EIO;
359 range_val = !!(parm & (1 << (shift-1))); /* ranges */
360 val = parm & mask;
361 if (val == 0) {
362 snd_printk(KERN_WARNING "hda_codec: "
363 "invalid CONNECT_LIST verb %x[%i]:%x\n",
364 nid, i, parm);
365 return 0;
367 parm >>= shift;
368 if (range_val) {
369 /* ranges between the previous and this one */
370 if (!prev_nid || prev_nid >= val) {
371 snd_printk(KERN_WARNING "hda_codec: "
372 "invalid dep_range_val %x:%x\n",
373 prev_nid, val);
374 continue;
376 for (n = prev_nid + 1; n <= val; n++) {
377 if (conns >= max_conns) {
378 snd_printk(KERN_ERR
379 "Too many connections\n");
380 return -EINVAL;
382 conn_list[conns++] = n;
384 } else {
385 if (conns >= max_conns) {
386 snd_printk(KERN_ERR "Too many connections\n");
387 return -EINVAL;
389 conn_list[conns++] = val;
391 prev_nid = val;
393 return conns;
395 EXPORT_SYMBOL_HDA(snd_hda_get_connections);
399 * snd_hda_queue_unsol_event - add an unsolicited event to queue
400 * @bus: the BUS
401 * @res: unsolicited event (lower 32bit of RIRB entry)
402 * @res_ex: codec addr and flags (upper 32bit or RIRB entry)
404 * Adds the given event to the queue. The events are processed in
405 * the workqueue asynchronously. Call this function in the interrupt
406 * hanlder when RIRB receives an unsolicited event.
408 * Returns 0 if successful, or a negative error code.
410 int snd_hda_queue_unsol_event(struct hda_bus *bus, u32 res, u32 res_ex)
412 struct hda_bus_unsolicited *unsol;
413 unsigned int wp;
415 unsol = bus->unsol;
416 if (!unsol)
417 return 0;
419 wp = (unsol->wp + 1) % HDA_UNSOL_QUEUE_SIZE;
420 unsol->wp = wp;
422 wp <<= 1;
423 unsol->queue[wp] = res;
424 unsol->queue[wp + 1] = res_ex;
426 queue_work(bus->workq, &unsol->work);
428 return 0;
430 EXPORT_SYMBOL_HDA(snd_hda_queue_unsol_event);
433 * process queued unsolicited events
435 static void process_unsol_events(struct work_struct *work)
437 struct hda_bus_unsolicited *unsol =
438 container_of(work, struct hda_bus_unsolicited, work);
439 struct hda_bus *bus = unsol->bus;
440 struct hda_codec *codec;
441 unsigned int rp, caddr, res;
443 while (unsol->rp != unsol->wp) {
444 rp = (unsol->rp + 1) % HDA_UNSOL_QUEUE_SIZE;
445 unsol->rp = rp;
446 rp <<= 1;
447 res = unsol->queue[rp];
448 caddr = unsol->queue[rp + 1];
449 if (!(caddr & (1 << 4))) /* no unsolicited event? */
450 continue;
451 codec = bus->caddr_tbl[caddr & 0x0f];
452 if (codec && codec->patch_ops.unsol_event)
453 codec->patch_ops.unsol_event(codec, res);
458 * initialize unsolicited queue
460 static int init_unsol_queue(struct hda_bus *bus)
462 struct hda_bus_unsolicited *unsol;
464 if (bus->unsol) /* already initialized */
465 return 0;
467 unsol = kzalloc(sizeof(*unsol), GFP_KERNEL);
468 if (!unsol) {
469 snd_printk(KERN_ERR "hda_codec: "
470 "can't allocate unsolicited queue\n");
471 return -ENOMEM;
473 INIT_WORK(&unsol->work, process_unsol_events);
474 unsol->bus = bus;
475 bus->unsol = unsol;
476 return 0;
480 * destructor
482 static void snd_hda_codec_free(struct hda_codec *codec);
484 static int snd_hda_bus_free(struct hda_bus *bus)
486 struct hda_codec *codec, *n;
488 if (!bus)
489 return 0;
490 if (bus->workq)
491 flush_workqueue(bus->workq);
492 if (bus->unsol)
493 kfree(bus->unsol);
494 list_for_each_entry_safe(codec, n, &bus->codec_list, list) {
495 snd_hda_codec_free(codec);
497 if (bus->ops.private_free)
498 bus->ops.private_free(bus);
499 if (bus->workq)
500 destroy_workqueue(bus->workq);
501 kfree(bus);
502 return 0;
505 static int snd_hda_bus_dev_free(struct snd_device *device)
507 struct hda_bus *bus = device->device_data;
508 bus->shutdown = 1;
509 return snd_hda_bus_free(bus);
512 #ifdef CONFIG_SND_HDA_HWDEP
513 static int snd_hda_bus_dev_register(struct snd_device *device)
515 struct hda_bus *bus = device->device_data;
516 struct hda_codec *codec;
517 list_for_each_entry(codec, &bus->codec_list, list) {
518 snd_hda_hwdep_add_sysfs(codec);
519 snd_hda_hwdep_add_power_sysfs(codec);
521 return 0;
523 #else
524 #define snd_hda_bus_dev_register NULL
525 #endif
528 * snd_hda_bus_new - create a HDA bus
529 * @card: the card entry
530 * @temp: the template for hda_bus information
531 * @busp: the pointer to store the created bus instance
533 * Returns 0 if successful, or a negative error code.
535 int /*__devinit*/ snd_hda_bus_new(struct snd_card *card,
536 const struct hda_bus_template *temp,
537 struct hda_bus **busp)
539 struct hda_bus *bus;
540 int err;
541 static struct snd_device_ops dev_ops = {
542 .dev_register = snd_hda_bus_dev_register,
543 .dev_free = snd_hda_bus_dev_free,
546 if (snd_BUG_ON(!temp))
547 return -EINVAL;
548 if (snd_BUG_ON(!temp->ops.command || !temp->ops.get_response))
549 return -EINVAL;
551 if (busp)
552 *busp = NULL;
554 bus = kzalloc(sizeof(*bus), GFP_KERNEL);
555 if (bus == NULL) {
556 snd_printk(KERN_ERR "can't allocate struct hda_bus\n");
557 return -ENOMEM;
560 bus->card = card;
561 bus->private_data = temp->private_data;
562 bus->pci = temp->pci;
563 bus->modelname = temp->modelname;
564 bus->power_save = temp->power_save;
565 bus->ops = temp->ops;
567 mutex_init(&bus->cmd_mutex);
568 INIT_LIST_HEAD(&bus->codec_list);
570 snprintf(bus->workq_name, sizeof(bus->workq_name),
571 "hd-audio%d", card->number);
572 bus->workq = create_singlethread_workqueue(bus->workq_name);
573 if (!bus->workq) {
574 snd_printk(KERN_ERR "cannot create workqueue %s\n",
575 bus->workq_name);
576 kfree(bus);
577 return -ENOMEM;
580 err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops);
581 if (err < 0) {
582 snd_hda_bus_free(bus);
583 return err;
585 if (busp)
586 *busp = bus;
587 return 0;
589 EXPORT_SYMBOL_HDA(snd_hda_bus_new);
591 #ifdef CONFIG_SND_HDA_GENERIC
592 #define is_generic_config(codec) \
593 (codec->modelname && !strcmp(codec->modelname, "generic"))
594 #else
595 #define is_generic_config(codec) 0
596 #endif
598 #ifdef MODULE
599 #define HDA_MODREQ_MAX_COUNT 2 /* two request_modules()'s */
600 #else
601 #define HDA_MODREQ_MAX_COUNT 0 /* all presets are statically linked */
602 #endif
605 * find a matching codec preset
607 static const struct hda_codec_preset *
608 find_codec_preset(struct hda_codec *codec)
610 struct hda_codec_preset_list *tbl;
611 const struct hda_codec_preset *preset;
612 int mod_requested = 0;
614 if (is_generic_config(codec))
615 return NULL; /* use the generic parser */
617 again:
618 mutex_lock(&preset_mutex);
619 list_for_each_entry(tbl, &hda_preset_tables, list) {
620 if (!try_module_get(tbl->owner)) {
621 snd_printk(KERN_ERR "hda_codec: cannot module_get\n");
622 continue;
624 for (preset = tbl->preset; preset->id; preset++) {
625 u32 mask = preset->mask;
626 if (preset->afg && preset->afg != codec->afg)
627 continue;
628 if (preset->mfg && preset->mfg != codec->mfg)
629 continue;
630 if (!mask)
631 mask = ~0;
632 if (preset->id == (codec->vendor_id & mask) &&
633 (!preset->rev ||
634 preset->rev == codec->revision_id)) {
635 mutex_unlock(&preset_mutex);
636 codec->owner = tbl->owner;
637 return preset;
640 module_put(tbl->owner);
642 mutex_unlock(&preset_mutex);
644 if (mod_requested < HDA_MODREQ_MAX_COUNT) {
645 char name[32];
646 if (!mod_requested)
647 snprintf(name, sizeof(name), "snd-hda-codec-id:%08x",
648 codec->vendor_id);
649 else
650 snprintf(name, sizeof(name), "snd-hda-codec-id:%04x*",
651 (codec->vendor_id >> 16) & 0xffff);
652 request_module(name);
653 mod_requested++;
654 goto again;
656 return NULL;
660 * get_codec_name - store the codec name
662 static int get_codec_name(struct hda_codec *codec)
664 const struct hda_vendor_id *c;
665 const char *vendor = NULL;
666 u16 vendor_id = codec->vendor_id >> 16;
667 char tmp[16];
669 if (codec->vendor_name)
670 goto get_chip_name;
672 for (c = hda_vendor_ids; c->id; c++) {
673 if (c->id == vendor_id) {
674 vendor = c->name;
675 break;
678 if (!vendor) {
679 sprintf(tmp, "Generic %04x", vendor_id);
680 vendor = tmp;
682 codec->vendor_name = kstrdup(vendor, GFP_KERNEL);
683 if (!codec->vendor_name)
684 return -ENOMEM;
686 get_chip_name:
687 if (codec->chip_name)
688 return 0;
690 if (codec->preset && codec->preset->name)
691 codec->chip_name = kstrdup(codec->preset->name, GFP_KERNEL);
692 else {
693 sprintf(tmp, "ID %x", codec->vendor_id & 0xffff);
694 codec->chip_name = kstrdup(tmp, GFP_KERNEL);
696 if (!codec->chip_name)
697 return -ENOMEM;
698 return 0;
702 * look for an AFG and MFG nodes
704 static void /*__devinit*/ setup_fg_nodes(struct hda_codec *codec)
706 int i, total_nodes, function_id;
707 hda_nid_t nid;
709 total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid);
710 for (i = 0; i < total_nodes; i++, nid++) {
711 function_id = snd_hda_param_read(codec, nid,
712 AC_PAR_FUNCTION_TYPE) & 0xff;
713 switch (function_id) {
714 case AC_GRP_AUDIO_FUNCTION:
715 codec->afg = nid;
716 codec->function_id = function_id;
717 break;
718 case AC_GRP_MODEM_FUNCTION:
719 codec->mfg = nid;
720 codec->function_id = function_id;
721 break;
722 default:
723 break;
729 * read widget caps for each widget and store in cache
731 static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
733 int i;
734 hda_nid_t nid;
736 codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node,
737 &codec->start_nid);
738 codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL);
739 if (!codec->wcaps)
740 return -ENOMEM;
741 nid = codec->start_nid;
742 for (i = 0; i < codec->num_nodes; i++, nid++)
743 codec->wcaps[i] = snd_hda_param_read(codec, nid,
744 AC_PAR_AUDIO_WIDGET_CAP);
745 return 0;
748 /* read all pin default configurations and save codec->init_pins */
749 static int read_pin_defaults(struct hda_codec *codec)
751 int i;
752 hda_nid_t nid = codec->start_nid;
754 for (i = 0; i < codec->num_nodes; i++, nid++) {
755 struct hda_pincfg *pin;
756 unsigned int wcaps = get_wcaps(codec, nid);
757 unsigned int wid_type = get_wcaps_type(wcaps);
758 if (wid_type != AC_WID_PIN)
759 continue;
760 pin = snd_array_new(&codec->init_pins);
761 if (!pin)
762 return -ENOMEM;
763 pin->nid = nid;
764 pin->cfg = snd_hda_codec_read(codec, nid, 0,
765 AC_VERB_GET_CONFIG_DEFAULT, 0);
767 return 0;
770 /* look up the given pin config list and return the item matching with NID */
771 static struct hda_pincfg *look_up_pincfg(struct hda_codec *codec,
772 struct snd_array *array,
773 hda_nid_t nid)
775 int i;
776 for (i = 0; i < array->used; i++) {
777 struct hda_pincfg *pin = snd_array_elem(array, i);
778 if (pin->nid == nid)
779 return pin;
781 return NULL;
784 /* write a config value for the given NID */
785 static void set_pincfg(struct hda_codec *codec, hda_nid_t nid,
786 unsigned int cfg)
788 int i;
789 for (i = 0; i < 4; i++) {
790 snd_hda_codec_write(codec, nid, 0,
791 AC_VERB_SET_CONFIG_DEFAULT_BYTES_0 + i,
792 cfg & 0xff);
793 cfg >>= 8;
797 /* set the current pin config value for the given NID.
798 * the value is cached, and read via snd_hda_codec_get_pincfg()
800 int snd_hda_add_pincfg(struct hda_codec *codec, struct snd_array *list,
801 hda_nid_t nid, unsigned int cfg)
803 struct hda_pincfg *pin;
804 unsigned int oldcfg;
806 oldcfg = snd_hda_codec_get_pincfg(codec, nid);
807 pin = look_up_pincfg(codec, list, nid);
808 if (!pin) {
809 pin = snd_array_new(list);
810 if (!pin)
811 return -ENOMEM;
812 pin->nid = nid;
814 pin->cfg = cfg;
816 /* change only when needed; e.g. if the pincfg is already present
817 * in user_pins[], don't write it
819 cfg = snd_hda_codec_get_pincfg(codec, nid);
820 if (oldcfg != cfg)
821 set_pincfg(codec, nid, cfg);
822 return 0;
825 int snd_hda_codec_set_pincfg(struct hda_codec *codec,
826 hda_nid_t nid, unsigned int cfg)
828 return snd_hda_add_pincfg(codec, &codec->driver_pins, nid, cfg);
830 EXPORT_SYMBOL_HDA(snd_hda_codec_set_pincfg);
832 /* get the current pin config value of the given pin NID */
833 unsigned int snd_hda_codec_get_pincfg(struct hda_codec *codec, hda_nid_t nid)
835 struct hda_pincfg *pin;
837 #ifdef CONFIG_SND_HDA_HWDEP
838 pin = look_up_pincfg(codec, &codec->user_pins, nid);
839 if (pin)
840 return pin->cfg;
841 #endif
842 pin = look_up_pincfg(codec, &codec->driver_pins, nid);
843 if (pin)
844 return pin->cfg;
845 pin = look_up_pincfg(codec, &codec->init_pins, nid);
846 if (pin)
847 return pin->cfg;
848 return 0;
850 EXPORT_SYMBOL_HDA(snd_hda_codec_get_pincfg);
852 /* restore all current pin configs */
853 static void restore_pincfgs(struct hda_codec *codec)
855 int i;
856 for (i = 0; i < codec->init_pins.used; i++) {
857 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
858 set_pincfg(codec, pin->nid,
859 snd_hda_codec_get_pincfg(codec, pin->nid));
863 static void init_hda_cache(struct hda_cache_rec *cache,
864 unsigned int record_size);
865 static void free_hda_cache(struct hda_cache_rec *cache);
867 /* restore the initial pin cfgs and release all pincfg lists */
868 static void restore_init_pincfgs(struct hda_codec *codec)
870 /* first free driver_pins and user_pins, then call restore_pincfg
871 * so that only the values in init_pins are restored
873 snd_array_free(&codec->driver_pins);
874 #ifdef CONFIG_SND_HDA_HWDEP
875 snd_array_free(&codec->user_pins);
876 #endif
877 restore_pincfgs(codec);
878 snd_array_free(&codec->init_pins);
882 * codec destructor
884 static void snd_hda_codec_free(struct hda_codec *codec)
886 if (!codec)
887 return;
888 restore_init_pincfgs(codec);
889 #ifdef CONFIG_SND_HDA_POWER_SAVE
890 cancel_delayed_work(&codec->power_work);
891 flush_workqueue(codec->bus->workq);
892 #endif
893 list_del(&codec->list);
894 snd_array_free(&codec->mixers);
895 codec->bus->caddr_tbl[codec->addr] = NULL;
896 if (codec->patch_ops.free)
897 codec->patch_ops.free(codec);
898 module_put(codec->owner);
899 free_hda_cache(&codec->amp_cache);
900 free_hda_cache(&codec->cmd_cache);
901 kfree(codec->vendor_name);
902 kfree(codec->chip_name);
903 kfree(codec->modelname);
904 kfree(codec->wcaps);
905 kfree(codec);
908 static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
909 unsigned int power_state);
912 * snd_hda_codec_new - create a HDA codec
913 * @bus: the bus to assign
914 * @codec_addr: the codec address
915 * @codecp: the pointer to store the generated codec
917 * Returns 0 if successful, or a negative error code.
919 int /*__devinit*/ snd_hda_codec_new(struct hda_bus *bus, unsigned int codec_addr,
920 struct hda_codec **codecp)
922 struct hda_codec *codec;
923 char component[31];
924 int err;
926 if (snd_BUG_ON(!bus))
927 return -EINVAL;
928 if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS))
929 return -EINVAL;
931 if (bus->caddr_tbl[codec_addr]) {
932 snd_printk(KERN_ERR "hda_codec: "
933 "address 0x%x is already occupied\n", codec_addr);
934 return -EBUSY;
937 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
938 if (codec == NULL) {
939 snd_printk(KERN_ERR "can't allocate struct hda_codec\n");
940 return -ENOMEM;
943 codec->bus = bus;
944 codec->addr = codec_addr;
945 mutex_init(&codec->spdif_mutex);
946 mutex_init(&codec->control_mutex);
947 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
948 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
949 snd_array_init(&codec->mixers, sizeof(struct hda_nid_item), 60);
950 snd_array_init(&codec->init_pins, sizeof(struct hda_pincfg), 16);
951 snd_array_init(&codec->driver_pins, sizeof(struct hda_pincfg), 16);
952 if (codec->bus->modelname) {
953 codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL);
954 if (!codec->modelname) {
955 snd_hda_codec_free(codec);
956 return -ENODEV;
960 #ifdef CONFIG_SND_HDA_POWER_SAVE
961 INIT_DELAYED_WORK(&codec->power_work, hda_power_work);
962 /* snd_hda_codec_new() marks the codec as power-up, and leave it as is.
963 * the caller has to power down appropriatley after initialization
964 * phase.
966 hda_keep_power_on(codec);
967 #endif
969 list_add_tail(&codec->list, &bus->codec_list);
970 bus->caddr_tbl[codec_addr] = codec;
972 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
973 AC_PAR_VENDOR_ID);
974 if (codec->vendor_id == -1)
975 /* read again, hopefully the access method was corrected
976 * in the last read...
978 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
979 AC_PAR_VENDOR_ID);
980 codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT,
981 AC_PAR_SUBSYSTEM_ID);
982 codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT,
983 AC_PAR_REV_ID);
985 setup_fg_nodes(codec);
986 if (!codec->afg && !codec->mfg) {
987 snd_printdd("hda_codec: no AFG or MFG node found\n");
988 err = -ENODEV;
989 goto error;
992 err = read_widget_caps(codec, codec->afg ? codec->afg : codec->mfg);
993 if (err < 0) {
994 snd_printk(KERN_ERR "hda_codec: cannot malloc\n");
995 goto error;
997 err = read_pin_defaults(codec);
998 if (err < 0)
999 goto error;
1001 if (!codec->subsystem_id) {
1002 hda_nid_t nid = codec->afg ? codec->afg : codec->mfg;
1003 codec->subsystem_id =
1004 snd_hda_codec_read(codec, nid, 0,
1005 AC_VERB_GET_SUBSYSTEM_ID, 0);
1008 /* power-up all before initialization */
1009 hda_set_power_state(codec,
1010 codec->afg ? codec->afg : codec->mfg,
1011 AC_PWRST_D0);
1013 snd_hda_codec_proc_new(codec);
1015 snd_hda_create_hwdep(codec);
1017 sprintf(component, "HDA:%08x,%08x,%08x", codec->vendor_id,
1018 codec->subsystem_id, codec->revision_id);
1019 snd_component_add(codec->bus->card, component);
1021 if (codecp)
1022 *codecp = codec;
1023 return 0;
1025 error:
1026 snd_hda_codec_free(codec);
1027 return err;
1029 EXPORT_SYMBOL_HDA(snd_hda_codec_new);
1031 int snd_hda_codec_configure(struct hda_codec *codec)
1033 int err;
1035 codec->preset = find_codec_preset(codec);
1036 if (!codec->vendor_name || !codec->chip_name) {
1037 err = get_codec_name(codec);
1038 if (err < 0)
1039 return err;
1041 /* audio codec should override the mixer name */
1042 if (codec->afg || !*codec->bus->card->mixername)
1043 snprintf(codec->bus->card->mixername,
1044 sizeof(codec->bus->card->mixername),
1045 "%s %s", codec->vendor_name, codec->chip_name);
1047 if (is_generic_config(codec)) {
1048 err = snd_hda_parse_generic_codec(codec);
1049 goto patched;
1051 if (codec->preset && codec->preset->patch) {
1052 err = codec->preset->patch(codec);
1053 goto patched;
1056 /* call the default parser */
1057 err = snd_hda_parse_generic_codec(codec);
1058 if (err < 0)
1059 printk(KERN_ERR "hda-codec: No codec parser is available\n");
1061 patched:
1062 if (!err && codec->patch_ops.unsol_event)
1063 err = init_unsol_queue(codec->bus);
1064 return err;
1066 EXPORT_SYMBOL_HDA(snd_hda_codec_configure);
1069 * snd_hda_codec_setup_stream - set up the codec for streaming
1070 * @codec: the CODEC to set up
1071 * @nid: the NID to set up
1072 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
1073 * @channel_id: channel id to pass, zero based.
1074 * @format: stream format.
1076 void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
1077 u32 stream_tag,
1078 int channel_id, int format)
1080 if (!nid)
1081 return;
1083 snd_printdd("hda_codec_setup_stream: "
1084 "NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
1085 nid, stream_tag, channel_id, format);
1086 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID,
1087 (stream_tag << 4) | channel_id);
1088 msleep(1);
1089 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, format);
1091 EXPORT_SYMBOL_HDA(snd_hda_codec_setup_stream);
1093 void snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid)
1095 if (!nid)
1096 return;
1098 snd_printdd("hda_codec_cleanup_stream: NID=0x%x\n", nid);
1099 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
1100 #if 0 /* keep the format */
1101 msleep(1);
1102 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0);
1103 #endif
1105 EXPORT_SYMBOL_HDA(snd_hda_codec_cleanup_stream);
1108 * amp access functions
1111 /* FIXME: more better hash key? */
1112 #define HDA_HASH_KEY(nid,dir,idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
1113 #define HDA_HASH_PINCAP_KEY(nid) (u32)((nid) + (0x02 << 24))
1114 #define HDA_HASH_PARPCM_KEY(nid) (u32)((nid) + (0x03 << 24))
1115 #define HDA_HASH_PARSTR_KEY(nid) (u32)((nid) + (0x04 << 24))
1116 #define INFO_AMP_CAPS (1<<0)
1117 #define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
1119 /* initialize the hash table */
1120 static void /*__devinit*/ init_hda_cache(struct hda_cache_rec *cache,
1121 unsigned int record_size)
1123 memset(cache, 0, sizeof(*cache));
1124 memset(cache->hash, 0xff, sizeof(cache->hash));
1125 snd_array_init(&cache->buf, record_size, 64);
1128 static void free_hda_cache(struct hda_cache_rec *cache)
1130 snd_array_free(&cache->buf);
1133 /* query the hash. allocate an entry if not found. */
1134 static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache,
1135 u32 key)
1137 u16 idx = key % (u16)ARRAY_SIZE(cache->hash);
1138 u16 cur = cache->hash[idx];
1139 struct hda_cache_head *info;
1141 while (cur != 0xffff) {
1142 info = snd_array_elem(&cache->buf, cur);
1143 if (info->key == key)
1144 return info;
1145 cur = info->next;
1148 /* add a new hash entry */
1149 info = snd_array_new(&cache->buf);
1150 if (!info)
1151 return NULL;
1152 cur = snd_array_index(&cache->buf, info);
1153 info->key = key;
1154 info->val = 0;
1155 info->next = cache->hash[idx];
1156 cache->hash[idx] = cur;
1158 return info;
1161 /* query and allocate an amp hash entry */
1162 static inline struct hda_amp_info *
1163 get_alloc_amp_hash(struct hda_codec *codec, u32 key)
1165 return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key);
1169 * query AMP capabilities for the given widget and direction
1171 u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
1173 struct hda_amp_info *info;
1175 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, 0));
1176 if (!info)
1177 return 0;
1178 if (!(info->head.val & INFO_AMP_CAPS)) {
1179 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
1180 nid = codec->afg;
1181 info->amp_caps = snd_hda_param_read(codec, nid,
1182 direction == HDA_OUTPUT ?
1183 AC_PAR_AMP_OUT_CAP :
1184 AC_PAR_AMP_IN_CAP);
1185 if (info->amp_caps)
1186 info->head.val |= INFO_AMP_CAPS;
1188 return info->amp_caps;
1190 EXPORT_SYMBOL_HDA(query_amp_caps);
1192 int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
1193 unsigned int caps)
1195 struct hda_amp_info *info;
1197 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, dir, 0));
1198 if (!info)
1199 return -EINVAL;
1200 info->amp_caps = caps;
1201 info->head.val |= INFO_AMP_CAPS;
1202 return 0;
1204 EXPORT_SYMBOL_HDA(snd_hda_override_amp_caps);
1206 static unsigned int
1207 query_caps_hash(struct hda_codec *codec, hda_nid_t nid, u32 key,
1208 unsigned int (*func)(struct hda_codec *, hda_nid_t))
1210 struct hda_amp_info *info;
1212 info = get_alloc_amp_hash(codec, key);
1213 if (!info)
1214 return 0;
1215 if (!info->head.val) {
1216 info->head.val |= INFO_AMP_CAPS;
1217 info->amp_caps = func(codec, nid);
1219 return info->amp_caps;
1222 static unsigned int read_pin_cap(struct hda_codec *codec, hda_nid_t nid)
1224 return snd_hda_param_read(codec, nid, AC_PAR_PIN_CAP);
1227 u32 snd_hda_query_pin_caps(struct hda_codec *codec, hda_nid_t nid)
1229 return query_caps_hash(codec, nid, HDA_HASH_PINCAP_KEY(nid),
1230 read_pin_cap);
1232 EXPORT_SYMBOL_HDA(snd_hda_query_pin_caps);
1235 * read the current volume to info
1236 * if the cache exists, read the cache value.
1238 static unsigned int get_vol_mute(struct hda_codec *codec,
1239 struct hda_amp_info *info, hda_nid_t nid,
1240 int ch, int direction, int index)
1242 u32 val, parm;
1244 if (info->head.val & INFO_AMP_VOL(ch))
1245 return info->vol[ch];
1247 parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
1248 parm |= direction == HDA_OUTPUT ? AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
1249 parm |= index;
1250 val = snd_hda_codec_read(codec, nid, 0,
1251 AC_VERB_GET_AMP_GAIN_MUTE, parm);
1252 info->vol[ch] = val & 0xff;
1253 info->head.val |= INFO_AMP_VOL(ch);
1254 return info->vol[ch];
1258 * write the current volume in info to the h/w and update the cache
1260 static void put_vol_mute(struct hda_codec *codec, struct hda_amp_info *info,
1261 hda_nid_t nid, int ch, int direction, int index,
1262 int val)
1264 u32 parm;
1266 parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
1267 parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
1268 parm |= index << AC_AMP_SET_INDEX_SHIFT;
1269 parm |= val;
1270 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
1271 info->vol[ch] = val;
1275 * read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
1277 int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
1278 int direction, int index)
1280 struct hda_amp_info *info;
1281 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
1282 if (!info)
1283 return 0;
1284 return get_vol_mute(codec, info, nid, ch, direction, index);
1286 EXPORT_SYMBOL_HDA(snd_hda_codec_amp_read);
1289 * update the AMP value, mask = bit mask to set, val = the value
1291 int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
1292 int direction, int idx, int mask, int val)
1294 struct hda_amp_info *info;
1296 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, idx));
1297 if (!info)
1298 return 0;
1299 val &= mask;
1300 val |= get_vol_mute(codec, info, nid, ch, direction, idx) & ~mask;
1301 if (info->vol[ch] == val)
1302 return 0;
1303 put_vol_mute(codec, info, nid, ch, direction, idx, val);
1304 return 1;
1306 EXPORT_SYMBOL_HDA(snd_hda_codec_amp_update);
1309 * update the AMP stereo with the same mask and value
1311 int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
1312 int direction, int idx, int mask, int val)
1314 int ch, ret = 0;
1315 for (ch = 0; ch < 2; ch++)
1316 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
1317 idx, mask, val);
1318 return ret;
1320 EXPORT_SYMBOL_HDA(snd_hda_codec_amp_stereo);
1322 #ifdef SND_HDA_NEEDS_RESUME
1323 /* resume the all amp commands from the cache */
1324 void snd_hda_codec_resume_amp(struct hda_codec *codec)
1326 struct hda_amp_info *buffer = codec->amp_cache.buf.list;
1327 int i;
1329 for (i = 0; i < codec->amp_cache.buf.used; i++, buffer++) {
1330 u32 key = buffer->head.key;
1331 hda_nid_t nid;
1332 unsigned int idx, dir, ch;
1333 if (!key)
1334 continue;
1335 nid = key & 0xff;
1336 idx = (key >> 16) & 0xff;
1337 dir = (key >> 24) & 0xff;
1338 for (ch = 0; ch < 2; ch++) {
1339 if (!(buffer->head.val & INFO_AMP_VOL(ch)))
1340 continue;
1341 put_vol_mute(codec, buffer, nid, ch, dir, idx,
1342 buffer->vol[ch]);
1346 EXPORT_SYMBOL_HDA(snd_hda_codec_resume_amp);
1347 #endif /* SND_HDA_NEEDS_RESUME */
1349 /* volume */
1350 int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
1351 struct snd_ctl_elem_info *uinfo)
1353 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1354 u16 nid = get_amp_nid(kcontrol);
1355 u8 chs = get_amp_channels(kcontrol);
1356 int dir = get_amp_direction(kcontrol);
1357 unsigned int ofs = get_amp_offset(kcontrol);
1358 u32 caps;
1360 caps = query_amp_caps(codec, nid, dir);
1361 /* num steps */
1362 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1363 if (!caps) {
1364 printk(KERN_WARNING "hda_codec: "
1365 "num_steps = 0 for NID=0x%x (ctl = %s)\n", nid,
1366 kcontrol->id.name);
1367 return -EINVAL;
1369 if (ofs < caps)
1370 caps -= ofs;
1371 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1372 uinfo->count = chs == 3 ? 2 : 1;
1373 uinfo->value.integer.min = 0;
1374 uinfo->value.integer.max = caps;
1375 return 0;
1377 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_info);
1380 static inline unsigned int
1381 read_amp_value(struct hda_codec *codec, hda_nid_t nid,
1382 int ch, int dir, int idx, unsigned int ofs)
1384 unsigned int val;
1385 val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
1386 val &= HDA_AMP_VOLMASK;
1387 if (val >= ofs)
1388 val -= ofs;
1389 else
1390 val = 0;
1391 return val;
1394 static inline int
1395 update_amp_value(struct hda_codec *codec, hda_nid_t nid,
1396 int ch, int dir, int idx, unsigned int ofs,
1397 unsigned int val)
1399 if (val > 0)
1400 val += ofs;
1401 return snd_hda_codec_amp_update(codec, nid, ch, dir, idx,
1402 HDA_AMP_VOLMASK, val);
1405 int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
1406 struct snd_ctl_elem_value *ucontrol)
1408 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1409 hda_nid_t nid = get_amp_nid(kcontrol);
1410 int chs = get_amp_channels(kcontrol);
1411 int dir = get_amp_direction(kcontrol);
1412 int idx = get_amp_index(kcontrol);
1413 unsigned int ofs = get_amp_offset(kcontrol);
1414 long *valp = ucontrol->value.integer.value;
1416 if (chs & 1)
1417 *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs);
1418 if (chs & 2)
1419 *valp = read_amp_value(codec, nid, 1, dir, idx, ofs);
1420 return 0;
1422 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_get);
1424 int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
1425 struct snd_ctl_elem_value *ucontrol)
1427 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1428 hda_nid_t nid = get_amp_nid(kcontrol);
1429 int chs = get_amp_channels(kcontrol);
1430 int dir = get_amp_direction(kcontrol);
1431 int idx = get_amp_index(kcontrol);
1432 unsigned int ofs = get_amp_offset(kcontrol);
1433 long *valp = ucontrol->value.integer.value;
1434 int change = 0;
1436 snd_hda_power_up(codec);
1437 if (chs & 1) {
1438 change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp);
1439 valp++;
1441 if (chs & 2)
1442 change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp);
1443 snd_hda_power_down(codec);
1444 return change;
1446 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_put);
1448 int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
1449 unsigned int size, unsigned int __user *_tlv)
1451 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1452 hda_nid_t nid = get_amp_nid(kcontrol);
1453 int dir = get_amp_direction(kcontrol);
1454 unsigned int ofs = get_amp_offset(kcontrol);
1455 u32 caps, val1, val2;
1457 if (size < 4 * sizeof(unsigned int))
1458 return -ENOMEM;
1459 caps = query_amp_caps(codec, nid, dir);
1460 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
1461 val2 = (val2 + 1) * 25;
1462 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
1463 val1 += ofs;
1464 val1 = ((int)val1) * ((int)val2);
1465 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
1466 return -EFAULT;
1467 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
1468 return -EFAULT;
1469 if (put_user(val1, _tlv + 2))
1470 return -EFAULT;
1471 if (put_user(val2, _tlv + 3))
1472 return -EFAULT;
1473 return 0;
1475 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_tlv);
1478 * set (static) TLV for virtual master volume; recalculated as max 0dB
1480 void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
1481 unsigned int *tlv)
1483 u32 caps;
1484 int nums, step;
1486 caps = query_amp_caps(codec, nid, dir);
1487 nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1488 step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
1489 step = (step + 1) * 25;
1490 tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
1491 tlv[1] = 2 * sizeof(unsigned int);
1492 tlv[2] = -nums * step;
1493 tlv[3] = step;
1495 EXPORT_SYMBOL_HDA(snd_hda_set_vmaster_tlv);
1497 /* find a mixer control element with the given name */
1498 static struct snd_kcontrol *
1499 _snd_hda_find_mixer_ctl(struct hda_codec *codec,
1500 const char *name, int idx)
1502 struct snd_ctl_elem_id id;
1503 memset(&id, 0, sizeof(id));
1504 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1505 id.index = idx;
1506 if (snd_BUG_ON(strlen(name) >= sizeof(id.name)))
1507 return NULL;
1508 strcpy(id.name, name);
1509 return snd_ctl_find_id(codec->bus->card, &id);
1512 struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
1513 const char *name)
1515 return _snd_hda_find_mixer_ctl(codec, name, 0);
1517 EXPORT_SYMBOL_HDA(snd_hda_find_mixer_ctl);
1519 /* Add a control element and assign to the codec */
1520 int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid,
1521 struct snd_kcontrol *kctl)
1523 int err;
1524 struct hda_nid_item *item;
1526 err = snd_ctl_add(codec->bus->card, kctl);
1527 if (err < 0)
1528 return err;
1529 item = snd_array_new(&codec->mixers);
1530 if (!item)
1531 return -ENOMEM;
1532 item->kctl = kctl;
1533 item->nid = nid;
1534 return 0;
1536 EXPORT_SYMBOL_HDA(snd_hda_ctl_add);
1538 /* Clear all controls assigned to the given codec */
1539 void snd_hda_ctls_clear(struct hda_codec *codec)
1541 int i;
1542 struct hda_nid_item *items = codec->mixers.list;
1543 for (i = 0; i < codec->mixers.used; i++)
1544 snd_ctl_remove(codec->bus->card, items[i].kctl);
1545 snd_array_free(&codec->mixers);
1548 /* pseudo device locking
1549 * toggle card->shutdown to allow/disallow the device access (as a hack)
1551 static int hda_lock_devices(struct snd_card *card)
1553 spin_lock(&card->files_lock);
1554 if (card->shutdown) {
1555 spin_unlock(&card->files_lock);
1556 return -EINVAL;
1558 card->shutdown = 1;
1559 spin_unlock(&card->files_lock);
1560 return 0;
1563 static void hda_unlock_devices(struct snd_card *card)
1565 spin_lock(&card->files_lock);
1566 card->shutdown = 0;
1567 spin_unlock(&card->files_lock);
1570 int snd_hda_codec_reset(struct hda_codec *codec)
1572 struct snd_card *card = codec->bus->card;
1573 int i, pcm;
1575 if (hda_lock_devices(card) < 0)
1576 return -EBUSY;
1577 /* check whether the codec isn't used by any mixer or PCM streams */
1578 if (!list_empty(&card->ctl_files)) {
1579 hda_unlock_devices(card);
1580 return -EBUSY;
1582 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
1583 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
1584 if (!cpcm->pcm)
1585 continue;
1586 if (cpcm->pcm->streams[0].substream_opened ||
1587 cpcm->pcm->streams[1].substream_opened) {
1588 hda_unlock_devices(card);
1589 return -EBUSY;
1593 /* OK, let it free */
1595 #ifdef CONFIG_SND_HDA_POWER_SAVE
1596 cancel_delayed_work(&codec->power_work);
1597 flush_workqueue(codec->bus->workq);
1598 #endif
1599 snd_hda_ctls_clear(codec);
1600 /* relase PCMs */
1601 for (i = 0; i < codec->num_pcms; i++) {
1602 if (codec->pcm_info[i].pcm) {
1603 snd_device_free(card, codec->pcm_info[i].pcm);
1604 clear_bit(codec->pcm_info[i].device,
1605 codec->bus->pcm_dev_bits);
1608 if (codec->patch_ops.free)
1609 codec->patch_ops.free(codec);
1610 codec->proc_widget_hook = NULL;
1611 codec->spec = NULL;
1612 free_hda_cache(&codec->amp_cache);
1613 free_hda_cache(&codec->cmd_cache);
1614 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
1615 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
1616 /* free only driver_pins so that init_pins + user_pins are restored */
1617 snd_array_free(&codec->driver_pins);
1618 restore_pincfgs(codec);
1619 codec->num_pcms = 0;
1620 codec->pcm_info = NULL;
1621 codec->preset = NULL;
1622 memset(&codec->patch_ops, 0, sizeof(codec->patch_ops));
1623 codec->slave_dig_outs = NULL;
1624 codec->spdif_status_reset = 0;
1625 module_put(codec->owner);
1626 codec->owner = NULL;
1628 /* allow device access again */
1629 hda_unlock_devices(card);
1630 return 0;
1633 /* create a virtual master control and add slaves */
1634 int snd_hda_add_vmaster(struct hda_codec *codec, char *name,
1635 unsigned int *tlv, const char **slaves)
1637 struct snd_kcontrol *kctl;
1638 const char **s;
1639 int err;
1641 for (s = slaves; *s && !snd_hda_find_mixer_ctl(codec, *s); s++)
1643 if (!*s) {
1644 snd_printdd("No slave found for %s\n", name);
1645 return 0;
1647 kctl = snd_ctl_make_virtual_master(name, tlv);
1648 if (!kctl)
1649 return -ENOMEM;
1650 err = snd_hda_ctl_add(codec, 0, kctl);
1651 if (err < 0)
1652 return err;
1654 for (s = slaves; *s; s++) {
1655 struct snd_kcontrol *sctl;
1656 int i = 0;
1657 for (;;) {
1658 sctl = _snd_hda_find_mixer_ctl(codec, *s, i);
1659 if (!sctl) {
1660 if (!i)
1661 snd_printdd("Cannot find slave %s, "
1662 "skipped\n", *s);
1663 break;
1665 err = snd_ctl_add_slave(kctl, sctl);
1666 if (err < 0)
1667 return err;
1668 i++;
1671 return 0;
1673 EXPORT_SYMBOL_HDA(snd_hda_add_vmaster);
1675 /* switch */
1676 int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
1677 struct snd_ctl_elem_info *uinfo)
1679 int chs = get_amp_channels(kcontrol);
1681 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1682 uinfo->count = chs == 3 ? 2 : 1;
1683 uinfo->value.integer.min = 0;
1684 uinfo->value.integer.max = 1;
1685 return 0;
1687 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_info);
1689 int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
1690 struct snd_ctl_elem_value *ucontrol)
1692 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1693 hda_nid_t nid = get_amp_nid(kcontrol);
1694 int chs = get_amp_channels(kcontrol);
1695 int dir = get_amp_direction(kcontrol);
1696 int idx = get_amp_index(kcontrol);
1697 long *valp = ucontrol->value.integer.value;
1699 if (chs & 1)
1700 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
1701 HDA_AMP_MUTE) ? 0 : 1;
1702 if (chs & 2)
1703 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
1704 HDA_AMP_MUTE) ? 0 : 1;
1705 return 0;
1707 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_get);
1709 int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
1710 struct snd_ctl_elem_value *ucontrol)
1712 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1713 hda_nid_t nid = get_amp_nid(kcontrol);
1714 int chs = get_amp_channels(kcontrol);
1715 int dir = get_amp_direction(kcontrol);
1716 int idx = get_amp_index(kcontrol);
1717 long *valp = ucontrol->value.integer.value;
1718 int change = 0;
1720 snd_hda_power_up(codec);
1721 if (chs & 1) {
1722 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
1723 HDA_AMP_MUTE,
1724 *valp ? 0 : HDA_AMP_MUTE);
1725 valp++;
1727 if (chs & 2)
1728 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
1729 HDA_AMP_MUTE,
1730 *valp ? 0 : HDA_AMP_MUTE);
1731 #ifdef CONFIG_SND_HDA_POWER_SAVE
1732 if (codec->patch_ops.check_power_status)
1733 codec->patch_ops.check_power_status(codec, nid);
1734 #endif
1735 snd_hda_power_down(codec);
1736 return change;
1738 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put);
1740 int snd_hda_mixer_amp_switch_put_beep(struct snd_kcontrol *kcontrol,
1741 struct snd_ctl_elem_value *ucontrol)
1743 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1744 long *valp = ucontrol->value.integer.value;
1746 snd_hda_enable_beep_device(codec, *valp);
1747 return snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
1749 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put_beep);
1752 * bound volume controls
1754 * bind multiple volumes (# indices, from 0)
1757 #define AMP_VAL_IDX_SHIFT 19
1758 #define AMP_VAL_IDX_MASK (0x0f<<19)
1760 int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
1761 struct snd_ctl_elem_value *ucontrol)
1763 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1764 unsigned long pval;
1765 int err;
1767 mutex_lock(&codec->control_mutex);
1768 pval = kcontrol->private_value;
1769 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
1770 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
1771 kcontrol->private_value = pval;
1772 mutex_unlock(&codec->control_mutex);
1773 return err;
1775 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_get);
1777 int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
1778 struct snd_ctl_elem_value *ucontrol)
1780 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1781 unsigned long pval;
1782 int i, indices, err = 0, change = 0;
1784 mutex_lock(&codec->control_mutex);
1785 pval = kcontrol->private_value;
1786 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
1787 for (i = 0; i < indices; i++) {
1788 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
1789 (i << AMP_VAL_IDX_SHIFT);
1790 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
1791 if (err < 0)
1792 break;
1793 change |= err;
1795 kcontrol->private_value = pval;
1796 mutex_unlock(&codec->control_mutex);
1797 return err < 0 ? err : change;
1799 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_put);
1802 * generic bound volume/swtich controls
1804 int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
1805 struct snd_ctl_elem_info *uinfo)
1807 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1808 struct hda_bind_ctls *c;
1809 int err;
1811 mutex_lock(&codec->control_mutex);
1812 c = (struct hda_bind_ctls *)kcontrol->private_value;
1813 kcontrol->private_value = *c->values;
1814 err = c->ops->info(kcontrol, uinfo);
1815 kcontrol->private_value = (long)c;
1816 mutex_unlock(&codec->control_mutex);
1817 return err;
1819 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_info);
1821 int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
1822 struct snd_ctl_elem_value *ucontrol)
1824 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1825 struct hda_bind_ctls *c;
1826 int err;
1828 mutex_lock(&codec->control_mutex);
1829 c = (struct hda_bind_ctls *)kcontrol->private_value;
1830 kcontrol->private_value = *c->values;
1831 err = c->ops->get(kcontrol, ucontrol);
1832 kcontrol->private_value = (long)c;
1833 mutex_unlock(&codec->control_mutex);
1834 return err;
1836 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_get);
1838 int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
1839 struct snd_ctl_elem_value *ucontrol)
1841 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1842 struct hda_bind_ctls *c;
1843 unsigned long *vals;
1844 int err = 0, change = 0;
1846 mutex_lock(&codec->control_mutex);
1847 c = (struct hda_bind_ctls *)kcontrol->private_value;
1848 for (vals = c->values; *vals; vals++) {
1849 kcontrol->private_value = *vals;
1850 err = c->ops->put(kcontrol, ucontrol);
1851 if (err < 0)
1852 break;
1853 change |= err;
1855 kcontrol->private_value = (long)c;
1856 mutex_unlock(&codec->control_mutex);
1857 return err < 0 ? err : change;
1859 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_put);
1861 int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
1862 unsigned int size, unsigned int __user *tlv)
1864 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1865 struct hda_bind_ctls *c;
1866 int err;
1868 mutex_lock(&codec->control_mutex);
1869 c = (struct hda_bind_ctls *)kcontrol->private_value;
1870 kcontrol->private_value = *c->values;
1871 err = c->ops->tlv(kcontrol, op_flag, size, tlv);
1872 kcontrol->private_value = (long)c;
1873 mutex_unlock(&codec->control_mutex);
1874 return err;
1876 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_tlv);
1878 struct hda_ctl_ops snd_hda_bind_vol = {
1879 .info = snd_hda_mixer_amp_volume_info,
1880 .get = snd_hda_mixer_amp_volume_get,
1881 .put = snd_hda_mixer_amp_volume_put,
1882 .tlv = snd_hda_mixer_amp_tlv
1884 EXPORT_SYMBOL_HDA(snd_hda_bind_vol);
1886 struct hda_ctl_ops snd_hda_bind_sw = {
1887 .info = snd_hda_mixer_amp_switch_info,
1888 .get = snd_hda_mixer_amp_switch_get,
1889 .put = snd_hda_mixer_amp_switch_put,
1890 .tlv = snd_hda_mixer_amp_tlv
1892 EXPORT_SYMBOL_HDA(snd_hda_bind_sw);
1895 * SPDIF out controls
1898 static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
1899 struct snd_ctl_elem_info *uinfo)
1901 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1902 uinfo->count = 1;
1903 return 0;
1906 static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
1907 struct snd_ctl_elem_value *ucontrol)
1909 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
1910 IEC958_AES0_NONAUDIO |
1911 IEC958_AES0_CON_EMPHASIS_5015 |
1912 IEC958_AES0_CON_NOT_COPYRIGHT;
1913 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
1914 IEC958_AES1_CON_ORIGINAL;
1915 return 0;
1918 static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
1919 struct snd_ctl_elem_value *ucontrol)
1921 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
1922 IEC958_AES0_NONAUDIO |
1923 IEC958_AES0_PRO_EMPHASIS_5015;
1924 return 0;
1927 static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
1928 struct snd_ctl_elem_value *ucontrol)
1930 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1932 ucontrol->value.iec958.status[0] = codec->spdif_status & 0xff;
1933 ucontrol->value.iec958.status[1] = (codec->spdif_status >> 8) & 0xff;
1934 ucontrol->value.iec958.status[2] = (codec->spdif_status >> 16) & 0xff;
1935 ucontrol->value.iec958.status[3] = (codec->spdif_status >> 24) & 0xff;
1937 return 0;
1940 /* convert from SPDIF status bits to HDA SPDIF bits
1941 * bit 0 (DigEn) is always set zero (to be filled later)
1943 static unsigned short convert_from_spdif_status(unsigned int sbits)
1945 unsigned short val = 0;
1947 if (sbits & IEC958_AES0_PROFESSIONAL)
1948 val |= AC_DIG1_PROFESSIONAL;
1949 if (sbits & IEC958_AES0_NONAUDIO)
1950 val |= AC_DIG1_NONAUDIO;
1951 if (sbits & IEC958_AES0_PROFESSIONAL) {
1952 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
1953 IEC958_AES0_PRO_EMPHASIS_5015)
1954 val |= AC_DIG1_EMPHASIS;
1955 } else {
1956 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
1957 IEC958_AES0_CON_EMPHASIS_5015)
1958 val |= AC_DIG1_EMPHASIS;
1959 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
1960 val |= AC_DIG1_COPYRIGHT;
1961 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
1962 val |= AC_DIG1_LEVEL;
1963 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
1965 return val;
1968 /* convert to SPDIF status bits from HDA SPDIF bits
1970 static unsigned int convert_to_spdif_status(unsigned short val)
1972 unsigned int sbits = 0;
1974 if (val & AC_DIG1_NONAUDIO)
1975 sbits |= IEC958_AES0_NONAUDIO;
1976 if (val & AC_DIG1_PROFESSIONAL)
1977 sbits |= IEC958_AES0_PROFESSIONAL;
1978 if (sbits & IEC958_AES0_PROFESSIONAL) {
1979 if (sbits & AC_DIG1_EMPHASIS)
1980 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
1981 } else {
1982 if (val & AC_DIG1_EMPHASIS)
1983 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
1984 if (!(val & AC_DIG1_COPYRIGHT))
1985 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
1986 if (val & AC_DIG1_LEVEL)
1987 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
1988 sbits |= val & (0x7f << 8);
1990 return sbits;
1993 /* set digital convert verbs both for the given NID and its slaves */
1994 static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
1995 int verb, int val)
1997 hda_nid_t *d;
1999 snd_hda_codec_write_cache(codec, nid, 0, verb, val);
2000 d = codec->slave_dig_outs;
2001 if (!d)
2002 return;
2003 for (; *d; d++)
2004 snd_hda_codec_write_cache(codec, *d, 0, verb, val);
2007 static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
2008 int dig1, int dig2)
2010 if (dig1 != -1)
2011 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1);
2012 if (dig2 != -1)
2013 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2);
2016 static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
2017 struct snd_ctl_elem_value *ucontrol)
2019 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2020 hda_nid_t nid = kcontrol->private_value;
2021 unsigned short val;
2022 int change;
2024 mutex_lock(&codec->spdif_mutex);
2025 codec->spdif_status = ucontrol->value.iec958.status[0] |
2026 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
2027 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
2028 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
2029 val = convert_from_spdif_status(codec->spdif_status);
2030 val |= codec->spdif_ctls & 1;
2031 change = codec->spdif_ctls != val;
2032 codec->spdif_ctls = val;
2034 if (change)
2035 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
2037 mutex_unlock(&codec->spdif_mutex);
2038 return change;
2041 #define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
2043 static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
2044 struct snd_ctl_elem_value *ucontrol)
2046 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2048 ucontrol->value.integer.value[0] = codec->spdif_ctls & AC_DIG1_ENABLE;
2049 return 0;
2052 static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
2053 struct snd_ctl_elem_value *ucontrol)
2055 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2056 hda_nid_t nid = kcontrol->private_value;
2057 unsigned short val;
2058 int change;
2060 mutex_lock(&codec->spdif_mutex);
2061 val = codec->spdif_ctls & ~AC_DIG1_ENABLE;
2062 if (ucontrol->value.integer.value[0])
2063 val |= AC_DIG1_ENABLE;
2064 change = codec->spdif_ctls != val;
2065 if (change) {
2066 codec->spdif_ctls = val;
2067 set_dig_out_convert(codec, nid, val & 0xff, -1);
2068 /* unmute amp switch (if any) */
2069 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
2070 (val & AC_DIG1_ENABLE))
2071 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
2072 HDA_AMP_MUTE, 0);
2074 mutex_unlock(&codec->spdif_mutex);
2075 return change;
2078 static struct snd_kcontrol_new dig_mixes[] = {
2080 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2081 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2082 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,CON_MASK),
2083 .info = snd_hda_spdif_mask_info,
2084 .get = snd_hda_spdif_cmask_get,
2087 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2088 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2089 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,PRO_MASK),
2090 .info = snd_hda_spdif_mask_info,
2091 .get = snd_hda_spdif_pmask_get,
2094 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2095 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
2096 .info = snd_hda_spdif_mask_info,
2097 .get = snd_hda_spdif_default_get,
2098 .put = snd_hda_spdif_default_put,
2101 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2102 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,SWITCH),
2103 .info = snd_hda_spdif_out_switch_info,
2104 .get = snd_hda_spdif_out_switch_get,
2105 .put = snd_hda_spdif_out_switch_put,
2107 { } /* end */
2110 #define SPDIF_MAX_IDX 4 /* 4 instances should be enough to probe */
2113 * snd_hda_create_spdif_out_ctls - create Output SPDIF-related controls
2114 * @codec: the HDA codec
2115 * @nid: audio out widget NID
2117 * Creates controls related with the SPDIF output.
2118 * Called from each patch supporting the SPDIF out.
2120 * Returns 0 if successful, or a negative error code.
2122 int snd_hda_create_spdif_out_ctls(struct hda_codec *codec, hda_nid_t nid)
2124 int err;
2125 struct snd_kcontrol *kctl;
2126 struct snd_kcontrol_new *dig_mix;
2127 int idx;
2129 for (idx = 0; idx < SPDIF_MAX_IDX; idx++) {
2130 if (!_snd_hda_find_mixer_ctl(codec, "IEC958 Playback Switch",
2131 idx))
2132 break;
2134 if (idx >= SPDIF_MAX_IDX) {
2135 printk(KERN_ERR "hda_codec: too many IEC958 outputs\n");
2136 return -EBUSY;
2138 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
2139 kctl = snd_ctl_new1(dig_mix, codec);
2140 if (!kctl)
2141 return -ENOMEM;
2142 kctl->id.index = idx;
2143 kctl->private_value = nid;
2144 err = snd_hda_ctl_add(codec, nid, kctl);
2145 if (err < 0)
2146 return err;
2148 codec->spdif_ctls =
2149 snd_hda_codec_read(codec, nid, 0,
2150 AC_VERB_GET_DIGI_CONVERT_1, 0);
2151 codec->spdif_status = convert_to_spdif_status(codec->spdif_ctls);
2152 return 0;
2154 EXPORT_SYMBOL_HDA(snd_hda_create_spdif_out_ctls);
2157 * SPDIF sharing with analog output
2159 static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
2160 struct snd_ctl_elem_value *ucontrol)
2162 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
2163 ucontrol->value.integer.value[0] = mout->share_spdif;
2164 return 0;
2167 static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
2168 struct snd_ctl_elem_value *ucontrol)
2170 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
2171 mout->share_spdif = !!ucontrol->value.integer.value[0];
2172 return 0;
2175 static struct snd_kcontrol_new spdif_share_sw = {
2176 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2177 .name = "IEC958 Default PCM Playback Switch",
2178 .info = snd_ctl_boolean_mono_info,
2179 .get = spdif_share_sw_get,
2180 .put = spdif_share_sw_put,
2183 int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
2184 struct hda_multi_out *mout)
2186 if (!mout->dig_out_nid)
2187 return 0;
2188 /* ATTENTION: here mout is passed as private_data, instead of codec */
2189 return snd_hda_ctl_add(codec, mout->dig_out_nid,
2190 snd_ctl_new1(&spdif_share_sw, mout));
2192 EXPORT_SYMBOL_HDA(snd_hda_create_spdif_share_sw);
2195 * SPDIF input
2198 #define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
2200 static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
2201 struct snd_ctl_elem_value *ucontrol)
2203 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2205 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
2206 return 0;
2209 static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
2210 struct snd_ctl_elem_value *ucontrol)
2212 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2213 hda_nid_t nid = kcontrol->private_value;
2214 unsigned int val = !!ucontrol->value.integer.value[0];
2215 int change;
2217 mutex_lock(&codec->spdif_mutex);
2218 change = codec->spdif_in_enable != val;
2219 if (change) {
2220 codec->spdif_in_enable = val;
2221 snd_hda_codec_write_cache(codec, nid, 0,
2222 AC_VERB_SET_DIGI_CONVERT_1, val);
2224 mutex_unlock(&codec->spdif_mutex);
2225 return change;
2228 static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
2229 struct snd_ctl_elem_value *ucontrol)
2231 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2232 hda_nid_t nid = kcontrol->private_value;
2233 unsigned short val;
2234 unsigned int sbits;
2236 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0);
2237 sbits = convert_to_spdif_status(val);
2238 ucontrol->value.iec958.status[0] = sbits;
2239 ucontrol->value.iec958.status[1] = sbits >> 8;
2240 ucontrol->value.iec958.status[2] = sbits >> 16;
2241 ucontrol->value.iec958.status[3] = sbits >> 24;
2242 return 0;
2245 static struct snd_kcontrol_new dig_in_ctls[] = {
2247 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2248 .name = SNDRV_CTL_NAME_IEC958("",CAPTURE,SWITCH),
2249 .info = snd_hda_spdif_in_switch_info,
2250 .get = snd_hda_spdif_in_switch_get,
2251 .put = snd_hda_spdif_in_switch_put,
2254 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2255 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2256 .name = SNDRV_CTL_NAME_IEC958("",CAPTURE,DEFAULT),
2257 .info = snd_hda_spdif_mask_info,
2258 .get = snd_hda_spdif_in_status_get,
2260 { } /* end */
2264 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
2265 * @codec: the HDA codec
2266 * @nid: audio in widget NID
2268 * Creates controls related with the SPDIF input.
2269 * Called from each patch supporting the SPDIF in.
2271 * Returns 0 if successful, or a negative error code.
2273 int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
2275 int err;
2276 struct snd_kcontrol *kctl;
2277 struct snd_kcontrol_new *dig_mix;
2278 int idx;
2280 for (idx = 0; idx < SPDIF_MAX_IDX; idx++) {
2281 if (!_snd_hda_find_mixer_ctl(codec, "IEC958 Capture Switch",
2282 idx))
2283 break;
2285 if (idx >= SPDIF_MAX_IDX) {
2286 printk(KERN_ERR "hda_codec: too many IEC958 inputs\n");
2287 return -EBUSY;
2289 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
2290 kctl = snd_ctl_new1(dig_mix, codec);
2291 if (!kctl)
2292 return -ENOMEM;
2293 kctl->private_value = nid;
2294 err = snd_hda_ctl_add(codec, nid, kctl);
2295 if (err < 0)
2296 return err;
2298 codec->spdif_in_enable =
2299 snd_hda_codec_read(codec, nid, 0,
2300 AC_VERB_GET_DIGI_CONVERT_1, 0) &
2301 AC_DIG1_ENABLE;
2302 return 0;
2304 EXPORT_SYMBOL_HDA(snd_hda_create_spdif_in_ctls);
2306 #ifdef SND_HDA_NEEDS_RESUME
2308 * command cache
2311 /* build a 32bit cache key with the widget id and the command parameter */
2312 #define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
2313 #define get_cmd_cache_nid(key) ((key) & 0xff)
2314 #define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
2317 * snd_hda_codec_write_cache - send a single command with caching
2318 * @codec: the HDA codec
2319 * @nid: NID to send the command
2320 * @direct: direct flag
2321 * @verb: the verb to send
2322 * @parm: the parameter for the verb
2324 * Send a single command without waiting for response.
2326 * Returns 0 if successful, or a negative error code.
2328 int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
2329 int direct, unsigned int verb, unsigned int parm)
2331 int err = snd_hda_codec_write(codec, nid, direct, verb, parm);
2332 struct hda_cache_head *c;
2333 u32 key;
2335 if (err < 0)
2336 return err;
2337 /* parm may contain the verb stuff for get/set amp */
2338 verb = verb | (parm >> 8);
2339 parm &= 0xff;
2340 key = build_cmd_cache_key(nid, verb);
2341 mutex_lock(&codec->bus->cmd_mutex);
2342 c = get_alloc_hash(&codec->cmd_cache, key);
2343 if (c)
2344 c->val = parm;
2345 mutex_unlock(&codec->bus->cmd_mutex);
2346 return 0;
2348 EXPORT_SYMBOL_HDA(snd_hda_codec_write_cache);
2350 /* resume the all commands from the cache */
2351 void snd_hda_codec_resume_cache(struct hda_codec *codec)
2353 struct hda_cache_head *buffer = codec->cmd_cache.buf.list;
2354 int i;
2356 for (i = 0; i < codec->cmd_cache.buf.used; i++, buffer++) {
2357 u32 key = buffer->key;
2358 if (!key)
2359 continue;
2360 snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
2361 get_cmd_cache_cmd(key), buffer->val);
2364 EXPORT_SYMBOL_HDA(snd_hda_codec_resume_cache);
2367 * snd_hda_sequence_write_cache - sequence writes with caching
2368 * @codec: the HDA codec
2369 * @seq: VERB array to send
2371 * Send the commands sequentially from the given array.
2372 * Thte commands are recorded on cache for power-save and resume.
2373 * The array must be terminated with NID=0.
2375 void snd_hda_sequence_write_cache(struct hda_codec *codec,
2376 const struct hda_verb *seq)
2378 for (; seq->nid; seq++)
2379 snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
2380 seq->param);
2382 EXPORT_SYMBOL_HDA(snd_hda_sequence_write_cache);
2383 #endif /* SND_HDA_NEEDS_RESUME */
2386 * set power state of the codec
2388 static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
2389 unsigned int power_state)
2391 hda_nid_t nid;
2392 int i;
2394 /* this delay seems necessary to avoid click noise at power-down */
2395 if (power_state == AC_PWRST_D3)
2396 msleep(100);
2397 snd_hda_codec_read(codec, fg, 0, AC_VERB_SET_POWER_STATE,
2398 power_state);
2399 /* partial workaround for "azx_get_response timeout" */
2400 if (power_state == AC_PWRST_D0)
2401 msleep(10);
2403 nid = codec->start_nid;
2404 for (i = 0; i < codec->num_nodes; i++, nid++) {
2405 unsigned int wcaps = get_wcaps(codec, nid);
2406 if (wcaps & AC_WCAP_POWER) {
2407 unsigned int wid_type = get_wcaps_type(wcaps);
2408 if (power_state == AC_PWRST_D3 &&
2409 wid_type == AC_WID_PIN) {
2410 unsigned int pincap;
2412 * don't power down the widget if it controls
2413 * eapd and EAPD_BTLENABLE is set.
2415 pincap = snd_hda_query_pin_caps(codec, nid);
2416 if (pincap & AC_PINCAP_EAPD) {
2417 int eapd = snd_hda_codec_read(codec,
2418 nid, 0,
2419 AC_VERB_GET_EAPD_BTLENABLE, 0);
2420 eapd &= 0x02;
2421 if (eapd)
2422 continue;
2425 snd_hda_codec_write(codec, nid, 0,
2426 AC_VERB_SET_POWER_STATE,
2427 power_state);
2431 if (power_state == AC_PWRST_D0) {
2432 unsigned long end_time;
2433 int state;
2434 msleep(10);
2435 /* wait until the codec reachs to D0 */
2436 end_time = jiffies + msecs_to_jiffies(500);
2437 do {
2438 state = snd_hda_codec_read(codec, fg, 0,
2439 AC_VERB_GET_POWER_STATE, 0);
2440 if (state == power_state)
2441 break;
2442 msleep(1);
2443 } while (time_after_eq(end_time, jiffies));
2447 #ifdef CONFIG_SND_HDA_HWDEP
2448 /* execute additional init verbs */
2449 static void hda_exec_init_verbs(struct hda_codec *codec)
2451 if (codec->init_verbs.list)
2452 snd_hda_sequence_write(codec, codec->init_verbs.list);
2454 #else
2455 static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
2456 #endif
2458 #ifdef SND_HDA_NEEDS_RESUME
2460 * call suspend and power-down; used both from PM and power-save
2462 static void hda_call_codec_suspend(struct hda_codec *codec)
2464 if (codec->patch_ops.suspend)
2465 codec->patch_ops.suspend(codec, PMSG_SUSPEND);
2466 hda_set_power_state(codec,
2467 codec->afg ? codec->afg : codec->mfg,
2468 AC_PWRST_D3);
2469 #ifdef CONFIG_SND_HDA_POWER_SAVE
2470 snd_hda_update_power_acct(codec);
2471 cancel_delayed_work(&codec->power_work);
2472 codec->power_on = 0;
2473 codec->power_transition = 0;
2474 codec->power_jiffies = jiffies;
2475 #endif
2479 * kick up codec; used both from PM and power-save
2481 static void hda_call_codec_resume(struct hda_codec *codec)
2483 hda_set_power_state(codec,
2484 codec->afg ? codec->afg : codec->mfg,
2485 AC_PWRST_D0);
2486 restore_pincfgs(codec); /* restore all current pin configs */
2487 hda_exec_init_verbs(codec);
2488 if (codec->patch_ops.resume)
2489 codec->patch_ops.resume(codec);
2490 else {
2491 if (codec->patch_ops.init)
2492 codec->patch_ops.init(codec);
2493 snd_hda_codec_resume_amp(codec);
2494 snd_hda_codec_resume_cache(codec);
2497 #endif /* SND_HDA_NEEDS_RESUME */
2501 * snd_hda_build_controls - build mixer controls
2502 * @bus: the BUS
2504 * Creates mixer controls for each codec included in the bus.
2506 * Returns 0 if successful, otherwise a negative error code.
2508 int /*__devinit*/ snd_hda_build_controls(struct hda_bus *bus)
2510 struct hda_codec *codec;
2512 list_for_each_entry(codec, &bus->codec_list, list) {
2513 int err = snd_hda_codec_build_controls(codec);
2514 if (err < 0) {
2515 printk(KERN_ERR "hda_codec: cannot build controls"
2516 "for #%d (error %d)\n", codec->addr, err);
2517 err = snd_hda_codec_reset(codec);
2518 if (err < 0) {
2519 printk(KERN_ERR
2520 "hda_codec: cannot revert codec\n");
2521 return err;
2525 return 0;
2527 EXPORT_SYMBOL_HDA(snd_hda_build_controls);
2529 int snd_hda_codec_build_controls(struct hda_codec *codec)
2531 int err = 0;
2532 hda_exec_init_verbs(codec);
2533 /* continue to initialize... */
2534 if (codec->patch_ops.init)
2535 err = codec->patch_ops.init(codec);
2536 if (!err && codec->patch_ops.build_controls)
2537 err = codec->patch_ops.build_controls(codec);
2538 if (err < 0)
2539 return err;
2540 return 0;
2544 * stream formats
2546 struct hda_rate_tbl {
2547 unsigned int hz;
2548 unsigned int alsa_bits;
2549 unsigned int hda_fmt;
2552 static struct hda_rate_tbl rate_bits[] = {
2553 /* rate in Hz, ALSA rate bitmask, HDA format value */
2555 /* autodetected value used in snd_hda_query_supported_pcm */
2556 { 8000, SNDRV_PCM_RATE_8000, 0x0500 }, /* 1/6 x 48 */
2557 { 11025, SNDRV_PCM_RATE_11025, 0x4300 }, /* 1/4 x 44 */
2558 { 16000, SNDRV_PCM_RATE_16000, 0x0200 }, /* 1/3 x 48 */
2559 { 22050, SNDRV_PCM_RATE_22050, 0x4100 }, /* 1/2 x 44 */
2560 { 32000, SNDRV_PCM_RATE_32000, 0x0a00 }, /* 2/3 x 48 */
2561 { 44100, SNDRV_PCM_RATE_44100, 0x4000 }, /* 44 */
2562 { 48000, SNDRV_PCM_RATE_48000, 0x0000 }, /* 48 */
2563 { 88200, SNDRV_PCM_RATE_88200, 0x4800 }, /* 2 x 44 */
2564 { 96000, SNDRV_PCM_RATE_96000, 0x0800 }, /* 2 x 48 */
2565 { 176400, SNDRV_PCM_RATE_176400, 0x5800 },/* 4 x 44 */
2566 { 192000, SNDRV_PCM_RATE_192000, 0x1800 }, /* 4 x 48 */
2567 #define AC_PAR_PCM_RATE_BITS 11
2568 /* up to bits 10, 384kHZ isn't supported properly */
2570 /* not autodetected value */
2571 { 9600, SNDRV_PCM_RATE_KNOT, 0x0400 }, /* 1/5 x 48 */
2573 { 0 } /* terminator */
2577 * snd_hda_calc_stream_format - calculate format bitset
2578 * @rate: the sample rate
2579 * @channels: the number of channels
2580 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
2581 * @maxbps: the max. bps
2583 * Calculate the format bitset from the given rate, channels and th PCM format.
2585 * Return zero if invalid.
2587 unsigned int snd_hda_calc_stream_format(unsigned int rate,
2588 unsigned int channels,
2589 unsigned int format,
2590 unsigned int maxbps)
2592 int i;
2593 unsigned int val = 0;
2595 for (i = 0; rate_bits[i].hz; i++)
2596 if (rate_bits[i].hz == rate) {
2597 val = rate_bits[i].hda_fmt;
2598 break;
2600 if (!rate_bits[i].hz) {
2601 snd_printdd("invalid rate %d\n", rate);
2602 return 0;
2605 if (channels == 0 || channels > 8) {
2606 snd_printdd("invalid channels %d\n", channels);
2607 return 0;
2609 val |= channels - 1;
2611 switch (snd_pcm_format_width(format)) {
2612 case 8: val |= 0x00; break;
2613 case 16: val |= 0x10; break;
2614 case 20:
2615 case 24:
2616 case 32:
2617 if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE)
2618 val |= 0x40;
2619 else if (maxbps >= 24)
2620 val |= 0x30;
2621 else
2622 val |= 0x20;
2623 break;
2624 default:
2625 snd_printdd("invalid format width %d\n",
2626 snd_pcm_format_width(format));
2627 return 0;
2630 return val;
2632 EXPORT_SYMBOL_HDA(snd_hda_calc_stream_format);
2634 static unsigned int get_pcm_param(struct hda_codec *codec, hda_nid_t nid)
2636 unsigned int val = 0;
2637 if (nid != codec->afg &&
2638 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD))
2639 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
2640 if (!val || val == -1)
2641 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
2642 if (!val || val == -1)
2643 return 0;
2644 return val;
2647 static unsigned int query_pcm_param(struct hda_codec *codec, hda_nid_t nid)
2649 return query_caps_hash(codec, nid, HDA_HASH_PARPCM_KEY(nid),
2650 get_pcm_param);
2653 static unsigned int get_stream_param(struct hda_codec *codec, hda_nid_t nid)
2655 unsigned int streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
2656 if (!streams || streams == -1)
2657 streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
2658 if (!streams || streams == -1)
2659 return 0;
2660 return streams;
2663 static unsigned int query_stream_param(struct hda_codec *codec, hda_nid_t nid)
2665 return query_caps_hash(codec, nid, HDA_HASH_PARSTR_KEY(nid),
2666 get_stream_param);
2670 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
2671 * @codec: the HDA codec
2672 * @nid: NID to query
2673 * @ratesp: the pointer to store the detected rate bitflags
2674 * @formatsp: the pointer to store the detected formats
2675 * @bpsp: the pointer to store the detected format widths
2677 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
2678 * or @bsps argument is ignored.
2680 * Returns 0 if successful, otherwise a negative error code.
2682 static int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
2683 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
2685 unsigned int i, val, wcaps;
2687 wcaps = get_wcaps(codec, nid);
2688 val = query_pcm_param(codec, nid);
2690 if (ratesp) {
2691 u32 rates = 0;
2692 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
2693 if (val & (1 << i))
2694 rates |= rate_bits[i].alsa_bits;
2696 if (rates == 0) {
2697 snd_printk(KERN_ERR "hda_codec: rates == 0 "
2698 "(nid=0x%x, val=0x%x, ovrd=%i)\n",
2699 nid, val,
2700 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0);
2701 return -EIO;
2703 *ratesp = rates;
2706 if (formatsp || bpsp) {
2707 u64 formats = 0;
2708 unsigned int streams, bps;
2710 streams = query_stream_param(codec, nid);
2711 if (!streams)
2712 return -EIO;
2714 bps = 0;
2715 if (streams & AC_SUPFMT_PCM) {
2716 if (val & AC_SUPPCM_BITS_8) {
2717 formats |= SNDRV_PCM_FMTBIT_U8;
2718 bps = 8;
2720 if (val & AC_SUPPCM_BITS_16) {
2721 formats |= SNDRV_PCM_FMTBIT_S16_LE;
2722 bps = 16;
2724 if (wcaps & AC_WCAP_DIGITAL) {
2725 if (val & AC_SUPPCM_BITS_32)
2726 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
2727 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
2728 formats |= SNDRV_PCM_FMTBIT_S32_LE;
2729 if (val & AC_SUPPCM_BITS_24)
2730 bps = 24;
2731 else if (val & AC_SUPPCM_BITS_20)
2732 bps = 20;
2733 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
2734 AC_SUPPCM_BITS_32)) {
2735 formats |= SNDRV_PCM_FMTBIT_S32_LE;
2736 if (val & AC_SUPPCM_BITS_32)
2737 bps = 32;
2738 else if (val & AC_SUPPCM_BITS_24)
2739 bps = 24;
2740 else if (val & AC_SUPPCM_BITS_20)
2741 bps = 20;
2744 if (streams & AC_SUPFMT_FLOAT32) {
2745 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
2746 if (!bps)
2747 bps = 32;
2749 if (streams == AC_SUPFMT_AC3) {
2750 /* should be exclusive */
2751 /* temporary hack: we have still no proper support
2752 * for the direct AC3 stream...
2754 formats |= SNDRV_PCM_FMTBIT_U8;
2755 bps = 8;
2757 if (formats == 0) {
2758 snd_printk(KERN_ERR "hda_codec: formats == 0 "
2759 "(nid=0x%x, val=0x%x, ovrd=%i, "
2760 "streams=0x%x)\n",
2761 nid, val,
2762 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0,
2763 streams);
2764 return -EIO;
2766 if (formatsp)
2767 *formatsp = formats;
2768 if (bpsp)
2769 *bpsp = bps;
2772 return 0;
2776 * snd_hda_is_supported_format - check whether the given node supports
2777 * the format val
2779 * Returns 1 if supported, 0 if not.
2781 int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
2782 unsigned int format)
2784 int i;
2785 unsigned int val = 0, rate, stream;
2787 val = query_pcm_param(codec, nid);
2788 if (!val)
2789 return 0;
2791 rate = format & 0xff00;
2792 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
2793 if (rate_bits[i].hda_fmt == rate) {
2794 if (val & (1 << i))
2795 break;
2796 return 0;
2798 if (i >= AC_PAR_PCM_RATE_BITS)
2799 return 0;
2801 stream = query_stream_param(codec, nid);
2802 if (!stream)
2803 return 0;
2805 if (stream & AC_SUPFMT_PCM) {
2806 switch (format & 0xf0) {
2807 case 0x00:
2808 if (!(val & AC_SUPPCM_BITS_8))
2809 return 0;
2810 break;
2811 case 0x10:
2812 if (!(val & AC_SUPPCM_BITS_16))
2813 return 0;
2814 break;
2815 case 0x20:
2816 if (!(val & AC_SUPPCM_BITS_20))
2817 return 0;
2818 break;
2819 case 0x30:
2820 if (!(val & AC_SUPPCM_BITS_24))
2821 return 0;
2822 break;
2823 case 0x40:
2824 if (!(val & AC_SUPPCM_BITS_32))
2825 return 0;
2826 break;
2827 default:
2828 return 0;
2830 } else {
2831 /* FIXME: check for float32 and AC3? */
2834 return 1;
2836 EXPORT_SYMBOL_HDA(snd_hda_is_supported_format);
2839 * PCM stuff
2841 static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
2842 struct hda_codec *codec,
2843 struct snd_pcm_substream *substream)
2845 return 0;
2848 static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
2849 struct hda_codec *codec,
2850 unsigned int stream_tag,
2851 unsigned int format,
2852 struct snd_pcm_substream *substream)
2854 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
2855 return 0;
2858 static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
2859 struct hda_codec *codec,
2860 struct snd_pcm_substream *substream)
2862 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
2863 return 0;
2866 static int set_pcm_default_values(struct hda_codec *codec,
2867 struct hda_pcm_stream *info)
2869 int err;
2871 /* query support PCM information from the given NID */
2872 if (info->nid && (!info->rates || !info->formats)) {
2873 err = snd_hda_query_supported_pcm(codec, info->nid,
2874 info->rates ? NULL : &info->rates,
2875 info->formats ? NULL : &info->formats,
2876 info->maxbps ? NULL : &info->maxbps);
2877 if (err < 0)
2878 return err;
2880 if (info->ops.open == NULL)
2881 info->ops.open = hda_pcm_default_open_close;
2882 if (info->ops.close == NULL)
2883 info->ops.close = hda_pcm_default_open_close;
2884 if (info->ops.prepare == NULL) {
2885 if (snd_BUG_ON(!info->nid))
2886 return -EINVAL;
2887 info->ops.prepare = hda_pcm_default_prepare;
2889 if (info->ops.cleanup == NULL) {
2890 if (snd_BUG_ON(!info->nid))
2891 return -EINVAL;
2892 info->ops.cleanup = hda_pcm_default_cleanup;
2894 return 0;
2897 const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = {
2898 "Audio", "SPDIF", "HDMI", "Modem"
2902 * get the empty PCM device number to assign
2904 static int get_empty_pcm_device(struct hda_bus *bus, int type)
2906 /* audio device indices; not linear to keep compatibility */
2907 static int audio_idx[HDA_PCM_NTYPES][5] = {
2908 [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 },
2909 [HDA_PCM_TYPE_SPDIF] = { 1, -1 },
2910 [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 },
2911 [HDA_PCM_TYPE_MODEM] = { 6, -1 },
2913 int i;
2915 if (type >= HDA_PCM_NTYPES) {
2916 snd_printk(KERN_WARNING "Invalid PCM type %d\n", type);
2917 return -EINVAL;
2920 for (i = 0; audio_idx[type][i] >= 0 ; i++)
2921 if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits))
2922 return audio_idx[type][i];
2924 snd_printk(KERN_WARNING "Too many %s devices\n", snd_hda_pcm_type_name[type]);
2925 return -EAGAIN;
2929 * attach a new PCM stream
2931 static int snd_hda_attach_pcm(struct hda_codec *codec, struct hda_pcm *pcm)
2933 struct hda_bus *bus = codec->bus;
2934 struct hda_pcm_stream *info;
2935 int stream, err;
2937 if (snd_BUG_ON(!pcm->name))
2938 return -EINVAL;
2939 for (stream = 0; stream < 2; stream++) {
2940 info = &pcm->stream[stream];
2941 if (info->substreams) {
2942 err = set_pcm_default_values(codec, info);
2943 if (err < 0)
2944 return err;
2947 return bus->ops.attach_pcm(bus, codec, pcm);
2950 /* assign all PCMs of the given codec */
2951 int snd_hda_codec_build_pcms(struct hda_codec *codec)
2953 unsigned int pcm;
2954 int err;
2956 if (!codec->num_pcms) {
2957 if (!codec->patch_ops.build_pcms)
2958 return 0;
2959 err = codec->patch_ops.build_pcms(codec);
2960 if (err < 0) {
2961 printk(KERN_ERR "hda_codec: cannot build PCMs"
2962 "for #%d (error %d)\n", codec->addr, err);
2963 err = snd_hda_codec_reset(codec);
2964 if (err < 0) {
2965 printk(KERN_ERR
2966 "hda_codec: cannot revert codec\n");
2967 return err;
2971 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
2972 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
2973 int dev;
2975 if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
2976 continue; /* no substreams assigned */
2978 if (!cpcm->pcm) {
2979 dev = get_empty_pcm_device(codec->bus, cpcm->pcm_type);
2980 if (dev < 0)
2981 continue; /* no fatal error */
2982 cpcm->device = dev;
2983 err = snd_hda_attach_pcm(codec, cpcm);
2984 if (err < 0) {
2985 printk(KERN_ERR "hda_codec: cannot attach "
2986 "PCM stream %d for codec #%d\n",
2987 dev, codec->addr);
2988 continue; /* no fatal error */
2992 return 0;
2996 * snd_hda_build_pcms - build PCM information
2997 * @bus: the BUS
2999 * Create PCM information for each codec included in the bus.
3001 * The build_pcms codec patch is requested to set up codec->num_pcms and
3002 * codec->pcm_info properly. The array is referred by the top-level driver
3003 * to create its PCM instances.
3004 * The allocated codec->pcm_info should be released in codec->patch_ops.free
3005 * callback.
3007 * At least, substreams, channels_min and channels_max must be filled for
3008 * each stream. substreams = 0 indicates that the stream doesn't exist.
3009 * When rates and/or formats are zero, the supported values are queried
3010 * from the given nid. The nid is used also by the default ops.prepare
3011 * and ops.cleanup callbacks.
3013 * The driver needs to call ops.open in its open callback. Similarly,
3014 * ops.close is supposed to be called in the close callback.
3015 * ops.prepare should be called in the prepare or hw_params callback
3016 * with the proper parameters for set up.
3017 * ops.cleanup should be called in hw_free for clean up of streams.
3019 * This function returns 0 if successfull, or a negative error code.
3021 int __devinit snd_hda_build_pcms(struct hda_bus *bus)
3023 struct hda_codec *codec;
3025 list_for_each_entry(codec, &bus->codec_list, list) {
3026 int err = snd_hda_codec_build_pcms(codec);
3027 if (err < 0)
3028 return err;
3030 return 0;
3032 EXPORT_SYMBOL_HDA(snd_hda_build_pcms);
3035 * snd_hda_check_board_config - compare the current codec with the config table
3036 * @codec: the HDA codec
3037 * @num_configs: number of config enums
3038 * @models: array of model name strings
3039 * @tbl: configuration table, terminated by null entries
3041 * Compares the modelname or PCI subsystem id of the current codec with the
3042 * given configuration table. If a matching entry is found, returns its
3043 * config value (supposed to be 0 or positive).
3045 * If no entries are matching, the function returns a negative value.
3047 int snd_hda_check_board_config(struct hda_codec *codec,
3048 int num_configs, const char **models,
3049 const struct snd_pci_quirk *tbl)
3051 if (codec->modelname && models) {
3052 int i;
3053 for (i = 0; i < num_configs; i++) {
3054 if (models[i] &&
3055 !strcmp(codec->modelname, models[i])) {
3056 snd_printd(KERN_INFO "hda_codec: model '%s' is "
3057 "selected\n", models[i]);
3058 return i;
3063 if (!codec->bus->pci || !tbl)
3064 return -1;
3066 tbl = snd_pci_quirk_lookup(codec->bus->pci, tbl);
3067 if (!tbl)
3068 return -1;
3069 if (tbl->value >= 0 && tbl->value < num_configs) {
3070 #ifdef CONFIG_SND_DEBUG_VERBOSE
3071 char tmp[10];
3072 const char *model = NULL;
3073 if (models)
3074 model = models[tbl->value];
3075 if (!model) {
3076 sprintf(tmp, "#%d", tbl->value);
3077 model = tmp;
3079 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
3080 "for config %x:%x (%s)\n",
3081 model, tbl->subvendor, tbl->subdevice,
3082 (tbl->name ? tbl->name : "Unknown device"));
3083 #endif
3084 return tbl->value;
3086 return -1;
3088 EXPORT_SYMBOL_HDA(snd_hda_check_board_config);
3091 * snd_hda_check_board_codec_sid_config - compare the current codec
3092 subsystem ID with the
3093 config table
3095 This is important for Gateway notebooks with SB450 HDA Audio
3096 where the vendor ID of the PCI device is:
3097 ATI Technologies Inc SB450 HDA Audio [1002:437b]
3098 and the vendor/subvendor are found only at the codec.
3100 * @codec: the HDA codec
3101 * @num_configs: number of config enums
3102 * @models: array of model name strings
3103 * @tbl: configuration table, terminated by null entries
3105 * Compares the modelname or PCI subsystem id of the current codec with the
3106 * given configuration table. If a matching entry is found, returns its
3107 * config value (supposed to be 0 or positive).
3109 * If no entries are matching, the function returns a negative value.
3111 int snd_hda_check_board_codec_sid_config(struct hda_codec *codec,
3112 int num_configs, const char **models,
3113 const struct snd_pci_quirk *tbl)
3115 const struct snd_pci_quirk *q;
3117 /* Search for codec ID */
3118 for (q = tbl; q->subvendor; q++) {
3119 unsigned long vendorid = (q->subdevice) | (q->subvendor << 16);
3121 if (vendorid == codec->subsystem_id)
3122 break;
3125 if (!q->subvendor)
3126 return -1;
3128 tbl = q;
3130 if (tbl->value >= 0 && tbl->value < num_configs) {
3131 #ifdef CONFIG_SND_DEBUG_VERBOSE
3132 char tmp[10];
3133 const char *model = NULL;
3134 if (models)
3135 model = models[tbl->value];
3136 if (!model) {
3137 sprintf(tmp, "#%d", tbl->value);
3138 model = tmp;
3140 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
3141 "for config %x:%x (%s)\n",
3142 model, tbl->subvendor, tbl->subdevice,
3143 (tbl->name ? tbl->name : "Unknown device"));
3144 #endif
3145 return tbl->value;
3147 return -1;
3149 EXPORT_SYMBOL_HDA(snd_hda_check_board_codec_sid_config);
3152 * snd_hda_add_new_ctls - create controls from the array
3153 * @codec: the HDA codec
3154 * @knew: the array of struct snd_kcontrol_new
3156 * This helper function creates and add new controls in the given array.
3157 * The array must be terminated with an empty entry as terminator.
3159 * Returns 0 if successful, or a negative error code.
3161 int snd_hda_add_new_ctls(struct hda_codec *codec, struct snd_kcontrol_new *knew)
3163 int err;
3165 for (; knew->name; knew++) {
3166 struct snd_kcontrol *kctl;
3167 kctl = snd_ctl_new1(knew, codec);
3168 if (!kctl)
3169 return -ENOMEM;
3170 err = snd_hda_ctl_add(codec, 0, kctl);
3171 if (err < 0) {
3172 if (!codec->addr)
3173 return err;
3174 kctl = snd_ctl_new1(knew, codec);
3175 if (!kctl)
3176 return -ENOMEM;
3177 kctl->id.device = codec->addr;
3178 err = snd_hda_ctl_add(codec, 0, kctl);
3179 if (err < 0)
3180 return err;
3183 return 0;
3185 EXPORT_SYMBOL_HDA(snd_hda_add_new_ctls);
3187 #ifdef CONFIG_SND_HDA_POWER_SAVE
3188 static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
3189 unsigned int power_state);
3191 static void hda_power_work(struct work_struct *work)
3193 struct hda_codec *codec =
3194 container_of(work, struct hda_codec, power_work.work);
3195 struct hda_bus *bus = codec->bus;
3197 if (!codec->power_on || codec->power_count) {
3198 codec->power_transition = 0;
3199 return;
3202 hda_call_codec_suspend(codec);
3203 if (bus->ops.pm_notify)
3204 bus->ops.pm_notify(bus);
3207 static void hda_keep_power_on(struct hda_codec *codec)
3209 codec->power_count++;
3210 codec->power_on = 1;
3211 codec->power_jiffies = jiffies;
3214 void snd_hda_update_power_acct(struct hda_codec *codec)
3216 unsigned long delta = jiffies - codec->power_jiffies;
3217 if (codec->power_on)
3218 codec->power_on_acct += delta;
3219 else
3220 codec->power_off_acct += delta;
3221 codec->power_jiffies += delta;
3224 void snd_hda_power_up(struct hda_codec *codec)
3226 struct hda_bus *bus = codec->bus;
3228 codec->power_count++;
3229 if (codec->power_on || codec->power_transition)
3230 return;
3232 snd_hda_update_power_acct(codec);
3233 codec->power_on = 1;
3234 codec->power_jiffies = jiffies;
3235 if (bus->ops.pm_notify)
3236 bus->ops.pm_notify(bus);
3237 hda_call_codec_resume(codec);
3238 cancel_delayed_work(&codec->power_work);
3239 codec->power_transition = 0;
3241 EXPORT_SYMBOL_HDA(snd_hda_power_up);
3243 #define power_save(codec) \
3244 ((codec)->bus->power_save ? *(codec)->bus->power_save : 0)
3246 #define power_save(codec) \
3247 ((codec)->bus->power_save ? *(codec)->bus->power_save : 0)
3249 void snd_hda_power_down(struct hda_codec *codec)
3251 --codec->power_count;
3252 if (!codec->power_on || codec->power_count || codec->power_transition)
3253 return;
3254 if (power_save(codec)) {
3255 codec->power_transition = 1; /* avoid reentrance */
3256 queue_delayed_work(codec->bus->workq, &codec->power_work,
3257 msecs_to_jiffies(power_save(codec) * 1000));
3260 EXPORT_SYMBOL_HDA(snd_hda_power_down);
3262 int snd_hda_check_amp_list_power(struct hda_codec *codec,
3263 struct hda_loopback_check *check,
3264 hda_nid_t nid)
3266 struct hda_amp_list *p;
3267 int ch, v;
3269 if (!check->amplist)
3270 return 0;
3271 for (p = check->amplist; p->nid; p++) {
3272 if (p->nid == nid)
3273 break;
3275 if (!p->nid)
3276 return 0; /* nothing changed */
3278 for (p = check->amplist; p->nid; p++) {
3279 for (ch = 0; ch < 2; ch++) {
3280 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
3281 p->idx);
3282 if (!(v & HDA_AMP_MUTE) && v > 0) {
3283 if (!check->power_on) {
3284 check->power_on = 1;
3285 snd_hda_power_up(codec);
3287 return 1;
3291 if (check->power_on) {
3292 check->power_on = 0;
3293 snd_hda_power_down(codec);
3295 return 0;
3297 EXPORT_SYMBOL_HDA(snd_hda_check_amp_list_power);
3298 #endif
3301 * Channel mode helper
3303 int snd_hda_ch_mode_info(struct hda_codec *codec,
3304 struct snd_ctl_elem_info *uinfo,
3305 const struct hda_channel_mode *chmode,
3306 int num_chmodes)
3308 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
3309 uinfo->count = 1;
3310 uinfo->value.enumerated.items = num_chmodes;
3311 if (uinfo->value.enumerated.item >= num_chmodes)
3312 uinfo->value.enumerated.item = num_chmodes - 1;
3313 sprintf(uinfo->value.enumerated.name, "%dch",
3314 chmode[uinfo->value.enumerated.item].channels);
3315 return 0;
3317 EXPORT_SYMBOL_HDA(snd_hda_ch_mode_info);
3319 int snd_hda_ch_mode_get(struct hda_codec *codec,
3320 struct snd_ctl_elem_value *ucontrol,
3321 const struct hda_channel_mode *chmode,
3322 int num_chmodes,
3323 int max_channels)
3325 int i;
3327 for (i = 0; i < num_chmodes; i++) {
3328 if (max_channels == chmode[i].channels) {
3329 ucontrol->value.enumerated.item[0] = i;
3330 break;
3333 return 0;
3335 EXPORT_SYMBOL_HDA(snd_hda_ch_mode_get);
3337 int snd_hda_ch_mode_put(struct hda_codec *codec,
3338 struct snd_ctl_elem_value *ucontrol,
3339 const struct hda_channel_mode *chmode,
3340 int num_chmodes,
3341 int *max_channelsp)
3343 unsigned int mode;
3345 mode = ucontrol->value.enumerated.item[0];
3346 if (mode >= num_chmodes)
3347 return -EINVAL;
3348 if (*max_channelsp == chmode[mode].channels)
3349 return 0;
3350 /* change the current channel setting */
3351 *max_channelsp = chmode[mode].channels;
3352 if (chmode[mode].sequence)
3353 snd_hda_sequence_write_cache(codec, chmode[mode].sequence);
3354 return 1;
3356 EXPORT_SYMBOL_HDA(snd_hda_ch_mode_put);
3359 * input MUX helper
3361 int snd_hda_input_mux_info(const struct hda_input_mux *imux,
3362 struct snd_ctl_elem_info *uinfo)
3364 unsigned int index;
3366 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
3367 uinfo->count = 1;
3368 uinfo->value.enumerated.items = imux->num_items;
3369 if (!imux->num_items)
3370 return 0;
3371 index = uinfo->value.enumerated.item;
3372 if (index >= imux->num_items)
3373 index = imux->num_items - 1;
3374 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
3375 return 0;
3377 EXPORT_SYMBOL_HDA(snd_hda_input_mux_info);
3379 int snd_hda_input_mux_put(struct hda_codec *codec,
3380 const struct hda_input_mux *imux,
3381 struct snd_ctl_elem_value *ucontrol,
3382 hda_nid_t nid,
3383 unsigned int *cur_val)
3385 unsigned int idx;
3387 if (!imux->num_items)
3388 return 0;
3389 idx = ucontrol->value.enumerated.item[0];
3390 if (idx >= imux->num_items)
3391 idx = imux->num_items - 1;
3392 if (*cur_val == idx)
3393 return 0;
3394 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
3395 imux->items[idx].index);
3396 *cur_val = idx;
3397 return 1;
3399 EXPORT_SYMBOL_HDA(snd_hda_input_mux_put);
3403 * Multi-channel / digital-out PCM helper functions
3406 /* setup SPDIF output stream */
3407 static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
3408 unsigned int stream_tag, unsigned int format)
3410 /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */
3411 if (codec->spdif_status_reset && (codec->spdif_ctls & AC_DIG1_ENABLE))
3412 set_dig_out_convert(codec, nid,
3413 codec->spdif_ctls & ~AC_DIG1_ENABLE & 0xff,
3414 -1);
3415 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
3416 if (codec->slave_dig_outs) {
3417 hda_nid_t *d;
3418 for (d = codec->slave_dig_outs; *d; d++)
3419 snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
3420 format);
3422 /* turn on again (if needed) */
3423 if (codec->spdif_status_reset && (codec->spdif_ctls & AC_DIG1_ENABLE))
3424 set_dig_out_convert(codec, nid,
3425 codec->spdif_ctls & 0xff, -1);
3428 static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
3430 snd_hda_codec_cleanup_stream(codec, nid);
3431 if (codec->slave_dig_outs) {
3432 hda_nid_t *d;
3433 for (d = codec->slave_dig_outs; *d; d++)
3434 snd_hda_codec_cleanup_stream(codec, *d);
3438 /* call each reboot notifier */
3439 void snd_hda_bus_reboot_notify(struct hda_bus *bus)
3441 struct hda_codec *codec;
3443 if (!bus)
3444 return;
3445 list_for_each_entry(codec, &bus->codec_list, list) {
3446 #ifdef CONFIG_SND_HDA_POWER_SAVE
3447 if (!codec->power_on)
3448 continue;
3449 #endif
3450 if (codec->patch_ops.reboot_notify)
3451 codec->patch_ops.reboot_notify(codec);
3454 EXPORT_SYMBOL_HDA(snd_hda_bus_reboot_notify);
3457 * open the digital out in the exclusive mode
3459 int snd_hda_multi_out_dig_open(struct hda_codec *codec,
3460 struct hda_multi_out *mout)
3462 mutex_lock(&codec->spdif_mutex);
3463 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
3464 /* already opened as analog dup; reset it once */
3465 cleanup_dig_out_stream(codec, mout->dig_out_nid);
3466 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
3467 mutex_unlock(&codec->spdif_mutex);
3468 return 0;
3470 EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_open);
3472 int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
3473 struct hda_multi_out *mout,
3474 unsigned int stream_tag,
3475 unsigned int format,
3476 struct snd_pcm_substream *substream)
3478 mutex_lock(&codec->spdif_mutex);
3479 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
3480 mutex_unlock(&codec->spdif_mutex);
3481 return 0;
3483 EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_prepare);
3485 int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec,
3486 struct hda_multi_out *mout)
3488 mutex_lock(&codec->spdif_mutex);
3489 cleanup_dig_out_stream(codec, mout->dig_out_nid);
3490 mutex_unlock(&codec->spdif_mutex);
3491 return 0;
3493 EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_cleanup);
3496 * release the digital out
3498 int snd_hda_multi_out_dig_close(struct hda_codec *codec,
3499 struct hda_multi_out *mout)
3501 mutex_lock(&codec->spdif_mutex);
3502 mout->dig_out_used = 0;
3503 mutex_unlock(&codec->spdif_mutex);
3504 return 0;
3506 EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_close);
3509 * set up more restrictions for analog out
3511 int snd_hda_multi_out_analog_open(struct hda_codec *codec,
3512 struct hda_multi_out *mout,
3513 struct snd_pcm_substream *substream,
3514 struct hda_pcm_stream *hinfo)
3516 struct snd_pcm_runtime *runtime = substream->runtime;
3517 runtime->hw.channels_max = mout->max_channels;
3518 if (mout->dig_out_nid) {
3519 if (!mout->analog_rates) {
3520 mout->analog_rates = hinfo->rates;
3521 mout->analog_formats = hinfo->formats;
3522 mout->analog_maxbps = hinfo->maxbps;
3523 } else {
3524 runtime->hw.rates = mout->analog_rates;
3525 runtime->hw.formats = mout->analog_formats;
3526 hinfo->maxbps = mout->analog_maxbps;
3528 if (!mout->spdif_rates) {
3529 snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
3530 &mout->spdif_rates,
3531 &mout->spdif_formats,
3532 &mout->spdif_maxbps);
3534 mutex_lock(&codec->spdif_mutex);
3535 if (mout->share_spdif) {
3536 if ((runtime->hw.rates & mout->spdif_rates) &&
3537 (runtime->hw.formats & mout->spdif_formats)) {
3538 runtime->hw.rates &= mout->spdif_rates;
3539 runtime->hw.formats &= mout->spdif_formats;
3540 if (mout->spdif_maxbps < hinfo->maxbps)
3541 hinfo->maxbps = mout->spdif_maxbps;
3542 } else {
3543 mout->share_spdif = 0;
3544 /* FIXME: need notify? */
3547 mutex_unlock(&codec->spdif_mutex);
3549 return snd_pcm_hw_constraint_step(substream->runtime, 0,
3550 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
3552 EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_open);
3555 * set up the i/o for analog out
3556 * when the digital out is available, copy the front out to digital out, too.
3558 int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
3559 struct hda_multi_out *mout,
3560 unsigned int stream_tag,
3561 unsigned int format,
3562 struct snd_pcm_substream *substream)
3564 hda_nid_t *nids = mout->dac_nids;
3565 int chs = substream->runtime->channels;
3566 int i;
3568 mutex_lock(&codec->spdif_mutex);
3569 if (mout->dig_out_nid && mout->share_spdif &&
3570 mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
3571 if (chs == 2 &&
3572 snd_hda_is_supported_format(codec, mout->dig_out_nid,
3573 format) &&
3574 !(codec->spdif_status & IEC958_AES0_NONAUDIO)) {
3575 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
3576 setup_dig_out_stream(codec, mout->dig_out_nid,
3577 stream_tag, format);
3578 } else {
3579 mout->dig_out_used = 0;
3580 cleanup_dig_out_stream(codec, mout->dig_out_nid);
3583 mutex_unlock(&codec->spdif_mutex);
3585 /* front */
3586 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
3587 0, format);
3588 if (!mout->no_share_stream &&
3589 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
3590 /* headphone out will just decode front left/right (stereo) */
3591 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
3592 0, format);
3593 /* extra outputs copied from front */
3594 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
3595 if (!mout->no_share_stream && mout->extra_out_nid[i])
3596 snd_hda_codec_setup_stream(codec,
3597 mout->extra_out_nid[i],
3598 stream_tag, 0, format);
3600 /* surrounds */
3601 for (i = 1; i < mout->num_dacs; i++) {
3602 if (chs >= (i + 1) * 2) /* independent out */
3603 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
3604 i * 2, format);
3605 else if (!mout->no_share_stream) /* copy front */
3606 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
3607 0, format);
3609 return 0;
3611 EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_prepare);
3614 * clean up the setting for analog out
3616 int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
3617 struct hda_multi_out *mout)
3619 hda_nid_t *nids = mout->dac_nids;
3620 int i;
3622 for (i = 0; i < mout->num_dacs; i++)
3623 snd_hda_codec_cleanup_stream(codec, nids[i]);
3624 if (mout->hp_nid)
3625 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
3626 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
3627 if (mout->extra_out_nid[i])
3628 snd_hda_codec_cleanup_stream(codec,
3629 mout->extra_out_nid[i]);
3630 mutex_lock(&codec->spdif_mutex);
3631 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
3632 cleanup_dig_out_stream(codec, mout->dig_out_nid);
3633 mout->dig_out_used = 0;
3635 mutex_unlock(&codec->spdif_mutex);
3636 return 0;
3638 EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_cleanup);
3641 * Helper for automatic pin configuration
3644 static int is_in_nid_list(hda_nid_t nid, hda_nid_t *list)
3646 for (; *list; list++)
3647 if (*list == nid)
3648 return 1;
3649 return 0;
3654 * Sort an associated group of pins according to their sequence numbers.
3656 static void sort_pins_by_sequence(hda_nid_t * pins, short * sequences,
3657 int num_pins)
3659 int i, j;
3660 short seq;
3661 hda_nid_t nid;
3663 for (i = 0; i < num_pins; i++) {
3664 for (j = i + 1; j < num_pins; j++) {
3665 if (sequences[i] > sequences[j]) {
3666 seq = sequences[i];
3667 sequences[i] = sequences[j];
3668 sequences[j] = seq;
3669 nid = pins[i];
3670 pins[i] = pins[j];
3671 pins[j] = nid;
3679 * Parse all pin widgets and store the useful pin nids to cfg
3681 * The number of line-outs or any primary output is stored in line_outs,
3682 * and the corresponding output pins are assigned to line_out_pins[],
3683 * in the order of front, rear, CLFE, side, ...
3685 * If more extra outputs (speaker and headphone) are found, the pins are
3686 * assisnged to hp_pins[] and speaker_pins[], respectively. If no line-out jack
3687 * is detected, one of speaker of HP pins is assigned as the primary
3688 * output, i.e. to line_out_pins[0]. So, line_outs is always positive
3689 * if any analog output exists.
3691 * The analog input pins are assigned to input_pins array.
3692 * The digital input/output pins are assigned to dig_in_pin and dig_out_pin,
3693 * respectively.
3695 int snd_hda_parse_pin_def_config(struct hda_codec *codec,
3696 struct auto_pin_cfg *cfg,
3697 hda_nid_t *ignore_nids)
3699 hda_nid_t nid, end_nid;
3700 short seq, assoc_line_out, assoc_speaker;
3701 short sequences_line_out[ARRAY_SIZE(cfg->line_out_pins)];
3702 short sequences_speaker[ARRAY_SIZE(cfg->speaker_pins)];
3703 short sequences_hp[ARRAY_SIZE(cfg->hp_pins)];
3705 memset(cfg, 0, sizeof(*cfg));
3707 memset(sequences_line_out, 0, sizeof(sequences_line_out));
3708 memset(sequences_speaker, 0, sizeof(sequences_speaker));
3709 memset(sequences_hp, 0, sizeof(sequences_hp));
3710 assoc_line_out = assoc_speaker = 0;
3712 end_nid = codec->start_nid + codec->num_nodes;
3713 for (nid = codec->start_nid; nid < end_nid; nid++) {
3714 unsigned int wid_caps = get_wcaps(codec, nid);
3715 unsigned int wid_type = get_wcaps_type(wid_caps);
3716 unsigned int def_conf;
3717 short assoc, loc;
3719 /* read all default configuration for pin complex */
3720 if (wid_type != AC_WID_PIN)
3721 continue;
3722 /* ignore the given nids (e.g. pc-beep returns error) */
3723 if (ignore_nids && is_in_nid_list(nid, ignore_nids))
3724 continue;
3726 def_conf = snd_hda_codec_get_pincfg(codec, nid);
3727 if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE)
3728 continue;
3729 loc = get_defcfg_location(def_conf);
3730 switch (get_defcfg_device(def_conf)) {
3731 case AC_JACK_LINE_OUT:
3732 seq = get_defcfg_sequence(def_conf);
3733 assoc = get_defcfg_association(def_conf);
3735 if (!(wid_caps & AC_WCAP_STEREO))
3736 if (!cfg->mono_out_pin)
3737 cfg->mono_out_pin = nid;
3738 if (!assoc)
3739 continue;
3740 if (!assoc_line_out)
3741 assoc_line_out = assoc;
3742 else if (assoc_line_out != assoc)
3743 continue;
3744 if (cfg->line_outs >= ARRAY_SIZE(cfg->line_out_pins))
3745 continue;
3746 cfg->line_out_pins[cfg->line_outs] = nid;
3747 sequences_line_out[cfg->line_outs] = seq;
3748 cfg->line_outs++;
3749 break;
3750 case AC_JACK_SPEAKER:
3751 seq = get_defcfg_sequence(def_conf);
3752 assoc = get_defcfg_association(def_conf);
3753 if (! assoc)
3754 continue;
3755 if (! assoc_speaker)
3756 assoc_speaker = assoc;
3757 else if (assoc_speaker != assoc)
3758 continue;
3759 if (cfg->speaker_outs >= ARRAY_SIZE(cfg->speaker_pins))
3760 continue;
3761 cfg->speaker_pins[cfg->speaker_outs] = nid;
3762 sequences_speaker[cfg->speaker_outs] = seq;
3763 cfg->speaker_outs++;
3764 break;
3765 case AC_JACK_HP_OUT:
3766 seq = get_defcfg_sequence(def_conf);
3767 assoc = get_defcfg_association(def_conf);
3768 if (cfg->hp_outs >= ARRAY_SIZE(cfg->hp_pins))
3769 continue;
3770 cfg->hp_pins[cfg->hp_outs] = nid;
3771 sequences_hp[cfg->hp_outs] = (assoc << 4) | seq;
3772 cfg->hp_outs++;
3773 break;
3774 case AC_JACK_MIC_IN: {
3775 int preferred, alt;
3776 if (loc == AC_JACK_LOC_FRONT) {
3777 preferred = AUTO_PIN_FRONT_MIC;
3778 alt = AUTO_PIN_MIC;
3779 } else {
3780 preferred = AUTO_PIN_MIC;
3781 alt = AUTO_PIN_FRONT_MIC;
3783 if (!cfg->input_pins[preferred])
3784 cfg->input_pins[preferred] = nid;
3785 else if (!cfg->input_pins[alt])
3786 cfg->input_pins[alt] = nid;
3787 break;
3789 case AC_JACK_LINE_IN:
3790 if (loc == AC_JACK_LOC_FRONT)
3791 cfg->input_pins[AUTO_PIN_FRONT_LINE] = nid;
3792 else
3793 cfg->input_pins[AUTO_PIN_LINE] = nid;
3794 break;
3795 case AC_JACK_CD:
3796 cfg->input_pins[AUTO_PIN_CD] = nid;
3797 break;
3798 case AC_JACK_AUX:
3799 cfg->input_pins[AUTO_PIN_AUX] = nid;
3800 break;
3801 case AC_JACK_SPDIF_OUT:
3802 case AC_JACK_DIG_OTHER_OUT:
3803 if (cfg->dig_outs >= ARRAY_SIZE(cfg->dig_out_pins))
3804 continue;
3805 cfg->dig_out_pins[cfg->dig_outs] = nid;
3806 cfg->dig_out_type[cfg->dig_outs] =
3807 (loc == AC_JACK_LOC_HDMI) ?
3808 HDA_PCM_TYPE_HDMI : HDA_PCM_TYPE_SPDIF;
3809 cfg->dig_outs++;
3810 break;
3811 case AC_JACK_SPDIF_IN:
3812 case AC_JACK_DIG_OTHER_IN:
3813 cfg->dig_in_pin = nid;
3814 if (loc == AC_JACK_LOC_HDMI)
3815 cfg->dig_in_type = HDA_PCM_TYPE_HDMI;
3816 else
3817 cfg->dig_in_type = HDA_PCM_TYPE_SPDIF;
3818 break;
3822 /* FIX-UP:
3823 * If no line-out is defined but multiple HPs are found,
3824 * some of them might be the real line-outs.
3826 if (!cfg->line_outs && cfg->hp_outs > 1) {
3827 int i = 0;
3828 while (i < cfg->hp_outs) {
3829 /* The real HPs should have the sequence 0x0f */
3830 if ((sequences_hp[i] & 0x0f) == 0x0f) {
3831 i++;
3832 continue;
3834 /* Move it to the line-out table */
3835 cfg->line_out_pins[cfg->line_outs] = cfg->hp_pins[i];
3836 sequences_line_out[cfg->line_outs] = sequences_hp[i];
3837 cfg->line_outs++;
3838 cfg->hp_outs--;
3839 memmove(cfg->hp_pins + i, cfg->hp_pins + i + 1,
3840 sizeof(cfg->hp_pins[0]) * (cfg->hp_outs - i));
3841 memmove(sequences_hp + i - 1, sequences_hp + i,
3842 sizeof(sequences_hp[0]) * (cfg->hp_outs - i));
3846 /* sort by sequence */
3847 sort_pins_by_sequence(cfg->line_out_pins, sequences_line_out,
3848 cfg->line_outs);
3849 sort_pins_by_sequence(cfg->speaker_pins, sequences_speaker,
3850 cfg->speaker_outs);
3851 sort_pins_by_sequence(cfg->hp_pins, sequences_hp,
3852 cfg->hp_outs);
3854 /* if we have only one mic, make it AUTO_PIN_MIC */
3855 if (!cfg->input_pins[AUTO_PIN_MIC] &&
3856 cfg->input_pins[AUTO_PIN_FRONT_MIC]) {
3857 cfg->input_pins[AUTO_PIN_MIC] =
3858 cfg->input_pins[AUTO_PIN_FRONT_MIC];
3859 cfg->input_pins[AUTO_PIN_FRONT_MIC] = 0;
3861 /* ditto for line-in */
3862 if (!cfg->input_pins[AUTO_PIN_LINE] &&
3863 cfg->input_pins[AUTO_PIN_FRONT_LINE]) {
3864 cfg->input_pins[AUTO_PIN_LINE] =
3865 cfg->input_pins[AUTO_PIN_FRONT_LINE];
3866 cfg->input_pins[AUTO_PIN_FRONT_LINE] = 0;
3870 * FIX-UP: if no line-outs are detected, try to use speaker or HP pin
3871 * as a primary output
3873 if (!cfg->line_outs) {
3874 if (cfg->speaker_outs) {
3875 cfg->line_outs = cfg->speaker_outs;
3876 memcpy(cfg->line_out_pins, cfg->speaker_pins,
3877 sizeof(cfg->speaker_pins));
3878 cfg->speaker_outs = 0;
3879 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
3880 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
3881 } else if (cfg->hp_outs) {
3882 cfg->line_outs = cfg->hp_outs;
3883 memcpy(cfg->line_out_pins, cfg->hp_pins,
3884 sizeof(cfg->hp_pins));
3885 cfg->hp_outs = 0;
3886 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
3887 cfg->line_out_type = AUTO_PIN_HP_OUT;
3891 /* Reorder the surround channels
3892 * ALSA sequence is front/surr/clfe/side
3893 * HDA sequence is:
3894 * 4-ch: front/surr => OK as it is
3895 * 6-ch: front/clfe/surr
3896 * 8-ch: front/clfe/rear/side|fc
3898 switch (cfg->line_outs) {
3899 case 3:
3900 case 4:
3901 nid = cfg->line_out_pins[1];
3902 cfg->line_out_pins[1] = cfg->line_out_pins[2];
3903 cfg->line_out_pins[2] = nid;
3904 break;
3908 * debug prints of the parsed results
3910 snd_printd("autoconfig: line_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
3911 cfg->line_outs, cfg->line_out_pins[0], cfg->line_out_pins[1],
3912 cfg->line_out_pins[2], cfg->line_out_pins[3],
3913 cfg->line_out_pins[4]);
3914 snd_printd(" speaker_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
3915 cfg->speaker_outs, cfg->speaker_pins[0],
3916 cfg->speaker_pins[1], cfg->speaker_pins[2],
3917 cfg->speaker_pins[3], cfg->speaker_pins[4]);
3918 snd_printd(" hp_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
3919 cfg->hp_outs, cfg->hp_pins[0],
3920 cfg->hp_pins[1], cfg->hp_pins[2],
3921 cfg->hp_pins[3], cfg->hp_pins[4]);
3922 snd_printd(" mono: mono_out=0x%x\n", cfg->mono_out_pin);
3923 if (cfg->dig_outs)
3924 snd_printd(" dig-out=0x%x/0x%x\n",
3925 cfg->dig_out_pins[0], cfg->dig_out_pins[1]);
3926 snd_printd(" inputs: mic=0x%x, fmic=0x%x, line=0x%x, fline=0x%x,"
3927 " cd=0x%x, aux=0x%x\n",
3928 cfg->input_pins[AUTO_PIN_MIC],
3929 cfg->input_pins[AUTO_PIN_FRONT_MIC],
3930 cfg->input_pins[AUTO_PIN_LINE],
3931 cfg->input_pins[AUTO_PIN_FRONT_LINE],
3932 cfg->input_pins[AUTO_PIN_CD],
3933 cfg->input_pins[AUTO_PIN_AUX]);
3934 if (cfg->dig_in_pin)
3935 snd_printd(" dig-in=0x%x\n", cfg->dig_in_pin);
3937 return 0;
3939 EXPORT_SYMBOL_HDA(snd_hda_parse_pin_def_config);
3941 /* labels for input pins */
3942 const char *auto_pin_cfg_labels[AUTO_PIN_LAST] = {
3943 "Mic", "Front Mic", "Line", "Front Line", "CD", "Aux"
3945 EXPORT_SYMBOL_HDA(auto_pin_cfg_labels);
3948 #ifdef CONFIG_PM
3950 * power management
3954 * snd_hda_suspend - suspend the codecs
3955 * @bus: the HDA bus
3957 * Returns 0 if successful.
3959 int snd_hda_suspend(struct hda_bus *bus)
3961 struct hda_codec *codec;
3963 list_for_each_entry(codec, &bus->codec_list, list) {
3964 #ifdef CONFIG_SND_HDA_POWER_SAVE
3965 if (!codec->power_on)
3966 continue;
3967 #endif
3968 hda_call_codec_suspend(codec);
3970 return 0;
3972 EXPORT_SYMBOL_HDA(snd_hda_suspend);
3975 * snd_hda_resume - resume the codecs
3976 * @bus: the HDA bus
3978 * Returns 0 if successful.
3980 * This fucntion is defined only when POWER_SAVE isn't set.
3981 * In the power-save mode, the codec is resumed dynamically.
3983 int snd_hda_resume(struct hda_bus *bus)
3985 struct hda_codec *codec;
3987 list_for_each_entry(codec, &bus->codec_list, list) {
3988 if (snd_hda_codec_needs_resume(codec))
3989 hda_call_codec_resume(codec);
3991 return 0;
3993 EXPORT_SYMBOL_HDA(snd_hda_resume);
3994 #endif /* CONFIG_PM */
3997 * generic arrays
4000 /* get a new element from the given array
4001 * if it exceeds the pre-allocated array size, re-allocate the array
4003 void *snd_array_new(struct snd_array *array)
4005 if (array->used >= array->alloced) {
4006 int num = array->alloced + array->alloc_align;
4007 void *nlist;
4008 if (snd_BUG_ON(num >= 4096))
4009 return NULL;
4010 nlist = kcalloc(num + 1, array->elem_size, GFP_KERNEL);
4011 if (!nlist)
4012 return NULL;
4013 if (array->list) {
4014 memcpy(nlist, array->list,
4015 array->elem_size * array->alloced);
4016 kfree(array->list);
4018 array->list = nlist;
4019 array->alloced = num;
4021 return snd_array_elem(array, array->used++);
4023 EXPORT_SYMBOL_HDA(snd_array_new);
4025 /* free the given array elements */
4026 void snd_array_free(struct snd_array *array)
4028 kfree(array->list);
4029 array->used = 0;
4030 array->alloced = 0;
4031 array->list = NULL;
4033 EXPORT_SYMBOL_HDA(snd_array_free);
4036 * used by hda_proc.c and hda_eld.c
4038 void snd_print_pcm_rates(int pcm, char *buf, int buflen)
4040 static unsigned int rates[] = {
4041 8000, 11025, 16000, 22050, 32000, 44100, 48000, 88200,
4042 96000, 176400, 192000, 384000
4044 int i, j;
4046 for (i = 0, j = 0; i < ARRAY_SIZE(rates); i++)
4047 if (pcm & (1 << i))
4048 j += snprintf(buf + j, buflen - j, " %d", rates[i]);
4050 buf[j] = '\0'; /* necessary when j == 0 */
4052 EXPORT_SYMBOL_HDA(snd_print_pcm_rates);
4054 void snd_print_pcm_bits(int pcm, char *buf, int buflen)
4056 static unsigned int bits[] = { 8, 16, 20, 24, 32 };
4057 int i, j;
4059 for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
4060 if (pcm & (AC_SUPPCM_BITS_8 << i))
4061 j += snprintf(buf + j, buflen - j, " %d", bits[i]);
4063 buf[j] = '\0'; /* necessary when j == 0 */
4065 EXPORT_SYMBOL_HDA(snd_print_pcm_bits);
4067 MODULE_DESCRIPTION("HDA codec core");
4068 MODULE_LICENSE("GPL");