code style scripts/checkpatch.pl (linux-3.9-rc1) formatting
[linux-2.6.34.14-moxart.git] / sound / pci / hda / hda_codec.c
blobb2ec16a69b1a6185721d66708d4bd2a4a2adafe7
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 /**
98 * snd_hda_get_jack_location - Give a location string of the jack
99 * @cfg: pin default config value
101 * Parse the pin default config value and returns the string of the
102 * jack location, e.g. "Rear", "Front", etc.
104 const char *snd_hda_get_jack_location(u32 cfg)
106 static char *bases[7] = {
107 "N/A", "Rear", "Front", "Left", "Right", "Top", "Bottom",
109 static unsigned char specials_idx[] = {
110 0x07, 0x08,
111 0x17, 0x18, 0x19,
112 0x37, 0x38
114 static char *specials[] = {
115 "Rear Panel", "Drive Bar",
116 "Riser", "HDMI", "ATAPI",
117 "Mobile-In", "Mobile-Out"
119 int i;
120 cfg = (cfg & AC_DEFCFG_LOCATION) >> AC_DEFCFG_LOCATION_SHIFT;
121 if ((cfg & 0x0f) < 7)
122 return bases[cfg & 0x0f];
123 for (i = 0; i < ARRAY_SIZE(specials_idx); i++) {
124 if (cfg == specials_idx[i])
125 return specials[i];
127 return "UNKNOWN";
129 EXPORT_SYMBOL_HDA(snd_hda_get_jack_location);
132 * snd_hda_get_jack_connectivity - Give a connectivity string of the jack
133 * @cfg: pin default config value
135 * Parse the pin default config value and returns the string of the
136 * jack connectivity, i.e. external or internal connection.
138 const char *snd_hda_get_jack_connectivity(u32 cfg)
140 static char *jack_locations[4] = { "Ext", "Int", "Sep", "Oth" };
142 return jack_locations[(cfg >> (AC_DEFCFG_LOCATION_SHIFT + 4)) & 3];
144 EXPORT_SYMBOL_HDA(snd_hda_get_jack_connectivity);
147 * snd_hda_get_jack_type - Give a type string of the jack
148 * @cfg: pin default config value
150 * Parse the pin default config value and returns the string of the
151 * jack type, i.e. the purpose of the jack, such as Line-Out or CD.
153 const char *snd_hda_get_jack_type(u32 cfg)
155 static char *jack_types[16] = {
156 "Line Out", "Speaker", "HP Out", "CD",
157 "SPDIF Out", "Digital Out", "Modem Line", "Modem Hand",
158 "Line In", "Aux", "Mic", "Telephony",
159 "SPDIF In", "Digitial In", "Reserved", "Other"
162 return jack_types[(cfg & AC_DEFCFG_DEVICE)
163 >> AC_DEFCFG_DEVICE_SHIFT];
165 EXPORT_SYMBOL_HDA(snd_hda_get_jack_type);
168 * Compose a 32bit command word to be sent to the HD-audio controller
170 static inline unsigned int
171 make_codec_cmd(struct hda_codec *codec, hda_nid_t nid, int direct,
172 unsigned int verb, unsigned int parm)
174 u32 val;
176 if ((codec->addr & ~0xf) || (direct & ~1) || (nid & ~0x7f) ||
177 (verb & ~0xfff) || (parm & ~0xffff)) {
178 printk(KERN_ERR "hda-codec: out of range cmd %x:%x:%x:%x:%x\n",
179 codec->addr, direct, nid, verb, parm);
180 return ~0;
183 val = (u32)codec->addr << 28;
184 val |= (u32)direct << 27;
185 val |= (u32)nid << 20;
186 val |= verb << 8;
187 val |= parm;
188 return val;
192 * Send and receive a verb
194 static int codec_exec_verb(struct hda_codec *codec, unsigned int cmd,
195 unsigned int *res)
197 struct hda_bus *bus = codec->bus;
198 int err;
200 if (cmd == ~0)
201 return -1;
203 if (res)
204 *res = -1;
205 again:
206 snd_hda_power_up(codec);
207 mutex_lock(&bus->cmd_mutex);
208 err = bus->ops.command(bus, cmd);
209 if (!err && res)
210 *res = bus->ops.get_response(bus, codec->addr);
211 mutex_unlock(&bus->cmd_mutex);
212 snd_hda_power_down(codec);
213 if (res && *res == -1 && bus->rirb_error) {
214 if (bus->response_reset) {
215 snd_printd("hda_codec: resetting BUS due to "
216 "fatal communication error\n");
217 bus->ops.bus_reset(bus);
219 goto again;
221 /* clear reset-flag when the communication gets recovered */
222 if (!err)
223 bus->response_reset = 0;
224 return err;
228 * snd_hda_codec_read - send a command and get the response
229 * @codec: the HDA codec
230 * @nid: NID to send the command
231 * @direct: direct flag
232 * @verb: the verb to send
233 * @parm: the parameter for the verb
235 * Send a single command and read the corresponding response.
237 * Returns the obtained response value, or -1 for an error.
239 unsigned int snd_hda_codec_read(struct hda_codec *codec, hda_nid_t nid,
240 int direct,
241 unsigned int verb, unsigned int parm)
243 unsigned cmd = make_codec_cmd(codec, nid, direct, verb, parm);
244 unsigned int res;
245 codec_exec_verb(codec, cmd, &res);
246 return res;
248 EXPORT_SYMBOL_HDA(snd_hda_codec_read);
251 * snd_hda_codec_write - send a single command without waiting for response
252 * @codec: the HDA codec
253 * @nid: NID to send the command
254 * @direct: direct flag
255 * @verb: the verb to send
256 * @parm: the parameter for the verb
258 * Send a single command without waiting for response.
260 * Returns 0 if successful, or a negative error code.
262 int snd_hda_codec_write(struct hda_codec *codec, hda_nid_t nid, int direct,
263 unsigned int verb, unsigned int parm)
265 unsigned int cmd = make_codec_cmd(codec, nid, direct, verb, parm);
266 unsigned int res;
267 return codec_exec_verb(codec, cmd,
268 codec->bus->sync_write ? &res : NULL);
270 EXPORT_SYMBOL_HDA(snd_hda_codec_write);
273 * snd_hda_sequence_write - sequence writes
274 * @codec: the HDA codec
275 * @seq: VERB array to send
277 * Send the commands sequentially from the given array.
278 * The array must be terminated with NID=0.
280 void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq)
282 for (; seq->nid; seq++)
283 snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param);
285 EXPORT_SYMBOL_HDA(snd_hda_sequence_write);
288 * snd_hda_get_sub_nodes - get the range of sub nodes
289 * @codec: the HDA codec
290 * @nid: NID to parse
291 * @start_id: the pointer to store the start NID
293 * Parse the NID and store the start NID of its sub-nodes.
294 * Returns the number of sub-nodes.
296 int snd_hda_get_sub_nodes(struct hda_codec *codec, hda_nid_t nid,
297 hda_nid_t *start_id)
299 unsigned int parm;
301 parm = snd_hda_param_read(codec, nid, AC_PAR_NODE_COUNT);
302 if (parm == -1)
303 return 0;
304 *start_id = (parm >> 16) & 0x7fff;
305 return (int)(parm & 0x7fff);
307 EXPORT_SYMBOL_HDA(snd_hda_get_sub_nodes);
310 * snd_hda_get_connections - get connection list
311 * @codec: the HDA codec
312 * @nid: NID to parse
313 * @conn_list: connection list array
314 * @max_conns: max. number of connections to store
316 * Parses the connection list of the given widget and stores the list
317 * of NIDs.
319 * Returns the number of connections, or a negative error code.
321 int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
322 hda_nid_t *conn_list, int max_conns)
324 unsigned int parm;
325 int i, conn_len, conns;
326 unsigned int shift, num_elems, mask;
327 unsigned int wcaps;
328 hda_nid_t prev_nid;
330 if (snd_BUG_ON(!conn_list || max_conns <= 0))
331 return -EINVAL;
333 wcaps = get_wcaps(codec, nid);
334 if (!(wcaps & AC_WCAP_CONN_LIST) &&
335 get_wcaps_type(wcaps) != AC_WID_VOL_KNB) {
336 snd_printk(KERN_WARNING "hda_codec: "
337 "connection list not available for 0x%x\n", nid);
338 return -EINVAL;
341 parm = snd_hda_param_read(codec, nid, AC_PAR_CONNLIST_LEN);
342 if (parm & AC_CLIST_LONG) {
343 /* long form */
344 shift = 16;
345 num_elems = 2;
346 } else {
347 /* short form */
348 shift = 8;
349 num_elems = 4;
351 conn_len = parm & AC_CLIST_LENGTH;
352 mask = (1 << (shift-1)) - 1;
354 if (!conn_len)
355 return 0; /* no connection */
357 if (conn_len == 1) {
358 /* single connection */
359 parm = snd_hda_codec_read(codec, nid, 0,
360 AC_VERB_GET_CONNECT_LIST, 0);
361 if (parm == -1 && codec->bus->rirb_error)
362 return -EIO;
363 conn_list[0] = parm & mask;
364 return 1;
367 /* multi connection */
368 conns = 0;
369 prev_nid = 0;
370 for (i = 0; i < conn_len; i++) {
371 int range_val;
372 hda_nid_t val, n;
374 if (i % num_elems == 0) {
375 parm = snd_hda_codec_read(codec, nid, 0,
376 AC_VERB_GET_CONNECT_LIST, i);
377 if (parm == -1 && codec->bus->rirb_error)
378 return -EIO;
380 range_val = !!(parm & (1 << (shift-1))); /* ranges */
381 val = parm & mask;
382 if (val == 0) {
383 snd_printk(KERN_WARNING "hda_codec: "
384 "invalid CONNECT_LIST verb %x[%i]:%x\n",
385 nid, i, parm);
386 return 0;
388 parm >>= shift;
389 if (range_val) {
390 /* ranges between the previous and this one */
391 if (!prev_nid || prev_nid >= val) {
392 snd_printk(KERN_WARNING "hda_codec: "
393 "invalid dep_range_val %x:%x\n",
394 prev_nid, val);
395 continue;
397 for (n = prev_nid + 1; n <= val; n++) {
398 if (conns >= max_conns) {
399 snd_printk(KERN_ERR
400 "Too many connections\n");
401 return -EINVAL;
403 conn_list[conns++] = n;
405 } else {
406 if (conns >= max_conns) {
407 snd_printk(KERN_ERR "Too many connections\n");
408 return -EINVAL;
410 conn_list[conns++] = val;
412 prev_nid = val;
414 return conns;
416 EXPORT_SYMBOL_HDA(snd_hda_get_connections);
420 * snd_hda_queue_unsol_event - add an unsolicited event to queue
421 * @bus: the BUS
422 * @res: unsolicited event (lower 32bit of RIRB entry)
423 * @res_ex: codec addr and flags (upper 32bit or RIRB entry)
425 * Adds the given event to the queue. The events are processed in
426 * the workqueue asynchronously. Call this function in the interrupt
427 * hanlder when RIRB receives an unsolicited event.
429 * Returns 0 if successful, or a negative error code.
431 int snd_hda_queue_unsol_event(struct hda_bus *bus, u32 res, u32 res_ex)
433 struct hda_bus_unsolicited *unsol;
434 unsigned int wp;
436 unsol = bus->unsol;
437 if (!unsol)
438 return 0;
440 wp = (unsol->wp + 1) % HDA_UNSOL_QUEUE_SIZE;
441 unsol->wp = wp;
443 wp <<= 1;
444 unsol->queue[wp] = res;
445 unsol->queue[wp + 1] = res_ex;
447 queue_work(bus->workq, &unsol->work);
449 return 0;
451 EXPORT_SYMBOL_HDA(snd_hda_queue_unsol_event);
454 * process queued unsolicited events
456 static void process_unsol_events(struct work_struct *work)
458 struct hda_bus_unsolicited *unsol =
459 container_of(work, struct hda_bus_unsolicited, work);
460 struct hda_bus *bus = unsol->bus;
461 struct hda_codec *codec;
462 unsigned int rp, caddr, res;
464 while (unsol->rp != unsol->wp) {
465 rp = (unsol->rp + 1) % HDA_UNSOL_QUEUE_SIZE;
466 unsol->rp = rp;
467 rp <<= 1;
468 res = unsol->queue[rp];
469 caddr = unsol->queue[rp + 1];
470 if (!(caddr & (1 << 4))) /* no unsolicited event? */
471 continue;
472 codec = bus->caddr_tbl[caddr & 0x0f];
473 if (codec && codec->patch_ops.unsol_event)
474 codec->patch_ops.unsol_event(codec, res);
479 * initialize unsolicited queue
481 static int init_unsol_queue(struct hda_bus *bus)
483 struct hda_bus_unsolicited *unsol;
485 if (bus->unsol) /* already initialized */
486 return 0;
488 unsol = kzalloc(sizeof(*unsol), GFP_KERNEL);
489 if (!unsol) {
490 snd_printk(KERN_ERR "hda_codec: "
491 "can't allocate unsolicited queue\n");
492 return -ENOMEM;
494 INIT_WORK(&unsol->work, process_unsol_events);
495 unsol->bus = bus;
496 bus->unsol = unsol;
497 return 0;
501 * destructor
503 static void snd_hda_codec_free(struct hda_codec *codec);
505 static int snd_hda_bus_free(struct hda_bus *bus)
507 struct hda_codec *codec, *n;
509 if (!bus)
510 return 0;
511 if (bus->workq)
512 flush_workqueue(bus->workq);
513 if (bus->unsol)
514 kfree(bus->unsol);
515 list_for_each_entry_safe(codec, n, &bus->codec_list, list) {
516 snd_hda_codec_free(codec);
518 if (bus->ops.private_free)
519 bus->ops.private_free(bus);
520 if (bus->workq)
521 destroy_workqueue(bus->workq);
522 kfree(bus);
523 return 0;
526 static int snd_hda_bus_dev_free(struct snd_device *device)
528 struct hda_bus *bus = device->device_data;
529 bus->shutdown = 1;
530 return snd_hda_bus_free(bus);
533 #ifdef CONFIG_SND_HDA_HWDEP
534 static int snd_hda_bus_dev_register(struct snd_device *device)
536 struct hda_bus *bus = device->device_data;
537 struct hda_codec *codec;
538 list_for_each_entry(codec, &bus->codec_list, list) {
539 snd_hda_hwdep_add_sysfs(codec);
540 snd_hda_hwdep_add_power_sysfs(codec);
542 return 0;
544 #else
545 #define snd_hda_bus_dev_register NULL
546 #endif
549 * snd_hda_bus_new - create a HDA bus
550 * @card: the card entry
551 * @temp: the template for hda_bus information
552 * @busp: the pointer to store the created bus instance
554 * Returns 0 if successful, or a negative error code.
556 int /*__devinit*/ snd_hda_bus_new(struct snd_card *card,
557 const struct hda_bus_template *temp,
558 struct hda_bus **busp)
560 struct hda_bus *bus;
561 int err;
562 static struct snd_device_ops dev_ops = {
563 .dev_register = snd_hda_bus_dev_register,
564 .dev_free = snd_hda_bus_dev_free,
567 if (snd_BUG_ON(!temp))
568 return -EINVAL;
569 if (snd_BUG_ON(!temp->ops.command || !temp->ops.get_response))
570 return -EINVAL;
572 if (busp)
573 *busp = NULL;
575 bus = kzalloc(sizeof(*bus), GFP_KERNEL);
576 if (bus == NULL) {
577 snd_printk(KERN_ERR "can't allocate struct hda_bus\n");
578 return -ENOMEM;
581 bus->card = card;
582 bus->private_data = temp->private_data;
583 bus->pci = temp->pci;
584 bus->modelname = temp->modelname;
585 bus->power_save = temp->power_save;
586 bus->ops = temp->ops;
588 mutex_init(&bus->cmd_mutex);
589 INIT_LIST_HEAD(&bus->codec_list);
591 snprintf(bus->workq_name, sizeof(bus->workq_name),
592 "hd-audio%d", card->number);
593 bus->workq = create_singlethread_workqueue(bus->workq_name);
594 if (!bus->workq) {
595 snd_printk(KERN_ERR "cannot create workqueue %s\n",
596 bus->workq_name);
597 kfree(bus);
598 return -ENOMEM;
601 err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops);
602 if (err < 0) {
603 snd_hda_bus_free(bus);
604 return err;
606 if (busp)
607 *busp = bus;
608 return 0;
610 EXPORT_SYMBOL_HDA(snd_hda_bus_new);
612 #ifdef CONFIG_SND_HDA_GENERIC
613 #define is_generic_config(codec) \
614 (codec->modelname && !strcmp(codec->modelname, "generic"))
615 #else
616 #define is_generic_config(codec) 0
617 #endif
619 #ifdef MODULE
620 #define HDA_MODREQ_MAX_COUNT 2 /* two request_modules()'s */
621 #else
622 #define HDA_MODREQ_MAX_COUNT 0 /* all presets are statically linked */
623 #endif
626 * find a matching codec preset
628 static const struct hda_codec_preset *
629 find_codec_preset(struct hda_codec *codec)
631 struct hda_codec_preset_list *tbl;
632 const struct hda_codec_preset *preset;
633 int mod_requested = 0;
635 if (is_generic_config(codec))
636 return NULL; /* use the generic parser */
638 again:
639 mutex_lock(&preset_mutex);
640 list_for_each_entry(tbl, &hda_preset_tables, list) {
641 if (!try_module_get(tbl->owner)) {
642 snd_printk(KERN_ERR "hda_codec: cannot module_get\n");
643 continue;
645 for (preset = tbl->preset; preset->id; preset++) {
646 u32 mask = preset->mask;
647 if (preset->afg && preset->afg != codec->afg)
648 continue;
649 if (preset->mfg && preset->mfg != codec->mfg)
650 continue;
651 if (!mask)
652 mask = ~0;
653 if (preset->id == (codec->vendor_id & mask) &&
654 (!preset->rev ||
655 preset->rev == codec->revision_id)) {
656 mutex_unlock(&preset_mutex);
657 codec->owner = tbl->owner;
658 return preset;
661 module_put(tbl->owner);
663 mutex_unlock(&preset_mutex);
665 if (mod_requested < HDA_MODREQ_MAX_COUNT) {
666 char name[32];
667 if (!mod_requested)
668 snprintf(name, sizeof(name), "snd-hda-codec-id:%08x",
669 codec->vendor_id);
670 else
671 snprintf(name, sizeof(name), "snd-hda-codec-id:%04x*",
672 (codec->vendor_id >> 16) & 0xffff);
673 request_module(name);
674 mod_requested++;
675 goto again;
677 return NULL;
681 * get_codec_name - store the codec name
683 static int get_codec_name(struct hda_codec *codec)
685 const struct hda_vendor_id *c;
686 const char *vendor = NULL;
687 u16 vendor_id = codec->vendor_id >> 16;
688 char tmp[16];
690 if (codec->vendor_name)
691 goto get_chip_name;
693 for (c = hda_vendor_ids; c->id; c++) {
694 if (c->id == vendor_id) {
695 vendor = c->name;
696 break;
699 if (!vendor) {
700 sprintf(tmp, "Generic %04x", vendor_id);
701 vendor = tmp;
703 codec->vendor_name = kstrdup(vendor, GFP_KERNEL);
704 if (!codec->vendor_name)
705 return -ENOMEM;
707 get_chip_name:
708 if (codec->chip_name)
709 return 0;
711 if (codec->preset && codec->preset->name)
712 codec->chip_name = kstrdup(codec->preset->name, GFP_KERNEL);
713 else {
714 sprintf(tmp, "ID %x", codec->vendor_id & 0xffff);
715 codec->chip_name = kstrdup(tmp, GFP_KERNEL);
717 if (!codec->chip_name)
718 return -ENOMEM;
719 return 0;
723 * look for an AFG and MFG nodes
725 static void /*__devinit*/ setup_fg_nodes(struct hda_codec *codec)
727 int i, total_nodes, function_id;
728 hda_nid_t nid;
730 total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid);
731 for (i = 0; i < total_nodes; i++, nid++) {
732 function_id = snd_hda_param_read(codec, nid,
733 AC_PAR_FUNCTION_TYPE) & 0xff;
734 switch (function_id) {
735 case AC_GRP_AUDIO_FUNCTION:
736 codec->afg = nid;
737 codec->function_id = function_id;
738 break;
739 case AC_GRP_MODEM_FUNCTION:
740 codec->mfg = nid;
741 codec->function_id = function_id;
742 break;
743 default:
744 break;
750 * read widget caps for each widget and store in cache
752 static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
754 int i;
755 hda_nid_t nid;
757 codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node,
758 &codec->start_nid);
759 codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL);
760 if (!codec->wcaps)
761 return -ENOMEM;
762 nid = codec->start_nid;
763 for (i = 0; i < codec->num_nodes; i++, nid++)
764 codec->wcaps[i] = snd_hda_param_read(codec, nid,
765 AC_PAR_AUDIO_WIDGET_CAP);
766 return 0;
769 /* read all pin default configurations and save codec->init_pins */
770 static int read_pin_defaults(struct hda_codec *codec)
772 int i;
773 hda_nid_t nid = codec->start_nid;
775 for (i = 0; i < codec->num_nodes; i++, nid++) {
776 struct hda_pincfg *pin;
777 unsigned int wcaps = get_wcaps(codec, nid);
778 unsigned int wid_type = get_wcaps_type(wcaps);
779 if (wid_type != AC_WID_PIN)
780 continue;
781 pin = snd_array_new(&codec->init_pins);
782 if (!pin)
783 return -ENOMEM;
784 pin->nid = nid;
785 pin->cfg = snd_hda_codec_read(codec, nid, 0,
786 AC_VERB_GET_CONFIG_DEFAULT, 0);
787 pin->ctrl = snd_hda_codec_read(codec, nid, 0,
788 AC_VERB_GET_PIN_WIDGET_CONTROL,
791 return 0;
794 /* look up the given pin config list and return the item matching with NID */
795 static struct hda_pincfg *look_up_pincfg(struct hda_codec *codec,
796 struct snd_array *array,
797 hda_nid_t nid)
799 int i;
800 for (i = 0; i < array->used; i++) {
801 struct hda_pincfg *pin = snd_array_elem(array, i);
802 if (pin->nid == nid)
803 return pin;
805 return NULL;
808 /* write a config value for the given NID */
809 static void set_pincfg(struct hda_codec *codec, hda_nid_t nid,
810 unsigned int cfg)
812 int i;
813 for (i = 0; i < 4; i++) {
814 snd_hda_codec_write(codec, nid, 0,
815 AC_VERB_SET_CONFIG_DEFAULT_BYTES_0 + i,
816 cfg & 0xff);
817 cfg >>= 8;
821 /* set the current pin config value for the given NID.
822 * the value is cached, and read via snd_hda_codec_get_pincfg()
824 int snd_hda_add_pincfg(struct hda_codec *codec, struct snd_array *list,
825 hda_nid_t nid, unsigned int cfg)
827 struct hda_pincfg *pin;
828 unsigned int oldcfg;
830 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN)
831 return -EINVAL;
833 oldcfg = snd_hda_codec_get_pincfg(codec, nid);
834 pin = look_up_pincfg(codec, list, nid);
835 if (!pin) {
836 pin = snd_array_new(list);
837 if (!pin)
838 return -ENOMEM;
839 pin->nid = nid;
841 pin->cfg = cfg;
843 /* change only when needed; e.g. if the pincfg is already present
844 * in user_pins[], don't write it
846 cfg = snd_hda_codec_get_pincfg(codec, nid);
847 if (oldcfg != cfg)
848 set_pincfg(codec, nid, cfg);
849 return 0;
853 * snd_hda_codec_set_pincfg - Override a pin default configuration
854 * @codec: the HDA codec
855 * @nid: NID to set the pin config
856 * @cfg: the pin default config value
858 * Override a pin default configuration value in the cache.
859 * This value can be read by snd_hda_codec_get_pincfg() in a higher
860 * priority than the real hardware value.
862 int snd_hda_codec_set_pincfg(struct hda_codec *codec,
863 hda_nid_t nid, unsigned int cfg)
865 return snd_hda_add_pincfg(codec, &codec->driver_pins, nid, cfg);
867 EXPORT_SYMBOL_HDA(snd_hda_codec_set_pincfg);
870 * snd_hda_codec_get_pincfg - Obtain a pin-default configuration
871 * @codec: the HDA codec
872 * @nid: NID to get the pin config
874 * Get the current pin config value of the given pin NID.
875 * If the pincfg value is cached or overridden via sysfs or driver,
876 * returns the cached value.
878 unsigned int snd_hda_codec_get_pincfg(struct hda_codec *codec, hda_nid_t nid)
880 struct hda_pincfg *pin;
882 #ifdef CONFIG_SND_HDA_HWDEP
883 pin = look_up_pincfg(codec, &codec->user_pins, nid);
884 if (pin)
885 return pin->cfg;
886 #endif
887 pin = look_up_pincfg(codec, &codec->driver_pins, nid);
888 if (pin)
889 return pin->cfg;
890 pin = look_up_pincfg(codec, &codec->init_pins, nid);
891 if (pin)
892 return pin->cfg;
893 return 0;
895 EXPORT_SYMBOL_HDA(snd_hda_codec_get_pincfg);
897 /* restore all current pin configs */
898 static void restore_pincfgs(struct hda_codec *codec)
900 int i;
901 for (i = 0; i < codec->init_pins.used; i++) {
902 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
903 set_pincfg(codec, pin->nid,
904 snd_hda_codec_get_pincfg(codec, pin->nid));
909 * snd_hda_shutup_pins - Shut up all pins
910 * @codec: the HDA codec
912 * Clear all pin controls to shup up before suspend for avoiding click noise.
913 * The controls aren't cached so that they can be resumed properly.
915 void snd_hda_shutup_pins(struct hda_codec *codec)
917 int i;
918 /* don't shut up pins when unloading the driver; otherwise it breaks
919 * the default pin setup at the next load of the driver
921 if (codec->bus->shutdown)
922 return;
923 for (i = 0; i < codec->init_pins.used; i++) {
924 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
925 /* use read here for syncing after issuing each verb */
926 snd_hda_codec_read(codec, pin->nid, 0,
927 AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
929 codec->pins_shutup = 1;
931 EXPORT_SYMBOL_HDA(snd_hda_shutup_pins);
933 /* Restore the pin controls cleared previously via snd_hda_shutup_pins() */
934 static void restore_shutup_pins(struct hda_codec *codec)
936 int i;
937 if (!codec->pins_shutup)
938 return;
939 if (codec->bus->shutdown)
940 return;
941 for (i = 0; i < codec->init_pins.used; i++) {
942 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
943 snd_hda_codec_write(codec, pin->nid, 0,
944 AC_VERB_SET_PIN_WIDGET_CONTROL,
945 pin->ctrl);
947 codec->pins_shutup = 0;
950 static void init_hda_cache(struct hda_cache_rec *cache,
951 unsigned int record_size);
952 static void free_hda_cache(struct hda_cache_rec *cache);
954 /* restore the initial pin cfgs and release all pincfg lists */
955 static void restore_init_pincfgs(struct hda_codec *codec)
957 /* first free driver_pins and user_pins, then call restore_pincfg
958 * so that only the values in init_pins are restored
960 snd_array_free(&codec->driver_pins);
961 #ifdef CONFIG_SND_HDA_HWDEP
962 snd_array_free(&codec->user_pins);
963 #endif
964 restore_pincfgs(codec);
965 snd_array_free(&codec->init_pins);
969 * codec destructor
971 static void snd_hda_codec_free(struct hda_codec *codec)
973 if (!codec)
974 return;
975 restore_init_pincfgs(codec);
976 #ifdef CONFIG_SND_HDA_POWER_SAVE
977 cancel_delayed_work(&codec->power_work);
978 flush_workqueue(codec->bus->workq);
979 #endif
980 list_del(&codec->list);
981 snd_array_free(&codec->mixers);
982 snd_array_free(&codec->nids);
983 codec->bus->caddr_tbl[codec->addr] = NULL;
984 if (codec->patch_ops.free)
985 codec->patch_ops.free(codec);
986 module_put(codec->owner);
987 free_hda_cache(&codec->amp_cache);
988 free_hda_cache(&codec->cmd_cache);
989 kfree(codec->vendor_name);
990 kfree(codec->chip_name);
991 kfree(codec->modelname);
992 kfree(codec->wcaps);
993 kfree(codec);
996 static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
997 unsigned int power_state);
1000 * snd_hda_codec_new - create a HDA codec
1001 * @bus: the bus to assign
1002 * @codec_addr: the codec address
1003 * @codecp: the pointer to store the generated codec
1005 * Returns 0 if successful, or a negative error code.
1007 int /*__devinit*/ snd_hda_codec_new(struct hda_bus *bus,
1008 unsigned int codec_addr,
1009 struct hda_codec **codecp)
1011 struct hda_codec *codec;
1012 char component[31];
1013 int err;
1015 if (snd_BUG_ON(!bus))
1016 return -EINVAL;
1017 if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS))
1018 return -EINVAL;
1020 if (bus->caddr_tbl[codec_addr]) {
1021 snd_printk(KERN_ERR "hda_codec: "
1022 "address 0x%x is already occupied\n", codec_addr);
1023 return -EBUSY;
1026 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
1027 if (codec == NULL) {
1028 snd_printk(KERN_ERR "can't allocate struct hda_codec\n");
1029 return -ENOMEM;
1032 codec->bus = bus;
1033 codec->addr = codec_addr;
1034 mutex_init(&codec->spdif_mutex);
1035 mutex_init(&codec->control_mutex);
1036 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
1037 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
1038 snd_array_init(&codec->mixers, sizeof(struct hda_nid_item), 32);
1039 snd_array_init(&codec->nids, sizeof(struct hda_nid_item), 32);
1040 snd_array_init(&codec->init_pins, sizeof(struct hda_pincfg), 16);
1041 snd_array_init(&codec->driver_pins, sizeof(struct hda_pincfg), 16);
1042 if (codec->bus->modelname) {
1043 codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL);
1044 if (!codec->modelname) {
1045 snd_hda_codec_free(codec);
1046 return -ENODEV;
1050 #ifdef CONFIG_SND_HDA_POWER_SAVE
1051 INIT_DELAYED_WORK(&codec->power_work, hda_power_work);
1052 /* snd_hda_codec_new() marks the codec as power-up, and leave it as is.
1053 * the caller has to power down appropriatley after initialization
1054 * phase.
1056 hda_keep_power_on(codec);
1057 #endif
1059 list_add_tail(&codec->list, &bus->codec_list);
1060 bus->caddr_tbl[codec_addr] = codec;
1062 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1063 AC_PAR_VENDOR_ID);
1064 if (codec->vendor_id == -1)
1065 /* read again, hopefully the access method was corrected
1066 * in the last read...
1068 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1069 AC_PAR_VENDOR_ID);
1070 codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1071 AC_PAR_SUBSYSTEM_ID);
1072 codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1073 AC_PAR_REV_ID);
1075 setup_fg_nodes(codec);
1076 if (!codec->afg && !codec->mfg) {
1077 snd_printdd("hda_codec: no AFG or MFG node found\n");
1078 err = -ENODEV;
1079 goto error;
1082 err = read_widget_caps(codec, codec->afg ? codec->afg : codec->mfg);
1083 if (err < 0) {
1084 snd_printk(KERN_ERR "hda_codec: cannot malloc\n");
1085 goto error;
1087 err = read_pin_defaults(codec);
1088 if (err < 0)
1089 goto error;
1091 if (!codec->subsystem_id) {
1092 hda_nid_t nid = codec->afg ? codec->afg : codec->mfg;
1093 codec->subsystem_id =
1094 snd_hda_codec_read(codec, nid, 0,
1095 AC_VERB_GET_SUBSYSTEM_ID, 0);
1098 /* power-up all before initialization */
1099 hda_set_power_state(codec,
1100 codec->afg ? codec->afg : codec->mfg,
1101 AC_PWRST_D0);
1103 snd_hda_codec_proc_new(codec);
1105 snd_hda_create_hwdep(codec);
1107 sprintf(component, "HDA:%08x,%08x,%08x", codec->vendor_id,
1108 codec->subsystem_id, codec->revision_id);
1109 snd_component_add(codec->bus->card, component);
1111 if (codecp)
1112 *codecp = codec;
1113 return 0;
1115 error:
1116 snd_hda_codec_free(codec);
1117 return err;
1119 EXPORT_SYMBOL_HDA(snd_hda_codec_new);
1122 * snd_hda_codec_configure - (Re-)configure the HD-audio codec
1123 * @codec: the HDA codec
1125 * Start parsing of the given codec tree and (re-)initialize the whole
1126 * patch instance.
1128 * Returns 0 if successful or a negative error code.
1130 int snd_hda_codec_configure(struct hda_codec *codec)
1132 int err;
1134 codec->preset = find_codec_preset(codec);
1135 if (!codec->vendor_name || !codec->chip_name) {
1136 err = get_codec_name(codec);
1137 if (err < 0)
1138 return err;
1141 if (is_generic_config(codec)) {
1142 err = snd_hda_parse_generic_codec(codec);
1143 goto patched;
1145 if (codec->preset && codec->preset->patch) {
1146 err = codec->preset->patch(codec);
1147 goto patched;
1150 /* call the default parser */
1151 err = snd_hda_parse_generic_codec(codec);
1152 if (err < 0)
1153 printk(KERN_ERR "hda-codec: No codec parser is available\n");
1155 patched:
1156 if (!err && codec->patch_ops.unsol_event)
1157 err = init_unsol_queue(codec->bus);
1158 /* audio codec should override the mixer name */
1159 if (!err && (codec->afg || !*codec->bus->card->mixername))
1160 snprintf(codec->bus->card->mixername,
1161 sizeof(codec->bus->card->mixername),
1162 "%s %s", codec->vendor_name, codec->chip_name);
1163 return err;
1165 EXPORT_SYMBOL_HDA(snd_hda_codec_configure);
1168 * snd_hda_codec_setup_stream - set up the codec for streaming
1169 * @codec: the CODEC to set up
1170 * @nid: the NID to set up
1171 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
1172 * @channel_id: channel id to pass, zero based.
1173 * @format: stream format.
1175 void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
1176 u32 stream_tag,
1177 int channel_id, int format)
1179 if (!nid)
1180 return;
1182 snd_printdd("hda_codec_setup_stream: "
1183 "NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
1184 nid, stream_tag, channel_id, format);
1185 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID,
1186 (stream_tag << 4) | channel_id);
1187 msleep(1);
1188 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, format);
1190 EXPORT_SYMBOL_HDA(snd_hda_codec_setup_stream);
1193 * snd_hda_codec_cleanup_stream - clean up the codec for closing
1194 * @codec: the CODEC to clean up
1195 * @nid: the NID to clean up
1197 void snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid)
1199 if (!nid)
1200 return;
1202 snd_printdd("hda_codec_cleanup_stream: NID=0x%x\n", nid);
1203 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
1204 #if 0 /* keep the format */
1205 msleep(1);
1206 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0);
1207 #endif
1209 EXPORT_SYMBOL_HDA(snd_hda_codec_cleanup_stream);
1212 * amp access functions
1215 /* FIXME: more better hash key? */
1216 #define HDA_HASH_KEY(nid, dir, idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
1217 #define HDA_HASH_PINCAP_KEY(nid) (u32)((nid) + (0x02 << 24))
1218 #define HDA_HASH_PARPCM_KEY(nid) (u32)((nid) + (0x03 << 24))
1219 #define HDA_HASH_PARSTR_KEY(nid) (u32)((nid) + (0x04 << 24))
1220 #define INFO_AMP_CAPS (1<<0)
1221 #define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
1223 /* initialize the hash table */
1224 static void /*__devinit*/ init_hda_cache(struct hda_cache_rec *cache,
1225 unsigned int record_size)
1227 memset(cache, 0, sizeof(*cache));
1228 memset(cache->hash, 0xff, sizeof(cache->hash));
1229 snd_array_init(&cache->buf, record_size, 64);
1232 static void free_hda_cache(struct hda_cache_rec *cache)
1234 snd_array_free(&cache->buf);
1237 /* query the hash. allocate an entry if not found. */
1238 static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache,
1239 u32 key)
1241 u16 idx = key % (u16)ARRAY_SIZE(cache->hash);
1242 u16 cur = cache->hash[idx];
1243 struct hda_cache_head *info;
1245 while (cur != 0xffff) {
1246 info = snd_array_elem(&cache->buf, cur);
1247 if (info->key == key)
1248 return info;
1249 cur = info->next;
1252 /* add a new hash entry */
1253 info = snd_array_new(&cache->buf);
1254 if (!info)
1255 return NULL;
1256 cur = snd_array_index(&cache->buf, info);
1257 info->key = key;
1258 info->val = 0;
1259 info->next = cache->hash[idx];
1260 cache->hash[idx] = cur;
1262 return info;
1265 /* query and allocate an amp hash entry */
1266 static inline struct hda_amp_info *
1267 get_alloc_amp_hash(struct hda_codec *codec, u32 key)
1269 return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key);
1273 * query_amp_caps - query AMP capabilities
1274 * @codec: the HD-auio codec
1275 * @nid: the NID to query
1276 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1278 * Query AMP capabilities for the given widget and direction.
1279 * Returns the obtained capability bits.
1281 * When cap bits have been already read, this doesn't read again but
1282 * returns the cached value.
1284 u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
1286 struct hda_amp_info *info;
1288 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, 0));
1289 if (!info)
1290 return 0;
1291 if (!(info->head.val & INFO_AMP_CAPS)) {
1292 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
1293 nid = codec->afg;
1294 info->amp_caps = snd_hda_param_read(codec, nid,
1295 direction == HDA_OUTPUT ?
1296 AC_PAR_AMP_OUT_CAP :
1297 AC_PAR_AMP_IN_CAP);
1298 if (info->amp_caps)
1299 info->head.val |= INFO_AMP_CAPS;
1301 return info->amp_caps;
1303 EXPORT_SYMBOL_HDA(query_amp_caps);
1306 * snd_hda_override_amp_caps - Override the AMP capabilities
1307 * @codec: the CODEC to clean up
1308 * @nid: the NID to clean up
1309 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1310 * @caps: the capability bits to set
1312 * Override the cached AMP caps bits value by the given one.
1313 * This function is useful if the driver needs to adjust the AMP ranges,
1314 * e.g. limit to 0dB, etc.
1316 * Returns zero if successful or a negative error code.
1318 int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
1319 unsigned int caps)
1321 struct hda_amp_info *info;
1323 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, dir, 0));
1324 if (!info)
1325 return -EINVAL;
1326 info->amp_caps = caps;
1327 info->head.val |= INFO_AMP_CAPS;
1328 return 0;
1330 EXPORT_SYMBOL_HDA(snd_hda_override_amp_caps);
1332 static unsigned int
1333 query_caps_hash(struct hda_codec *codec, hda_nid_t nid, u32 key,
1334 unsigned int (*func)(struct hda_codec *, hda_nid_t))
1336 struct hda_amp_info *info;
1338 info = get_alloc_amp_hash(codec, key);
1339 if (!info)
1340 return 0;
1341 if (!info->head.val) {
1342 info->head.val |= INFO_AMP_CAPS;
1343 info->amp_caps = func(codec, nid);
1345 return info->amp_caps;
1348 static unsigned int read_pin_cap(struct hda_codec *codec, hda_nid_t nid)
1350 return snd_hda_param_read(codec, nid, AC_PAR_PIN_CAP);
1354 * snd_hda_query_pin_caps - Query PIN capabilities
1355 * @codec: the HD-auio codec
1356 * @nid: the NID to query
1358 * Query PIN capabilities for the given widget.
1359 * Returns the obtained capability bits.
1361 * When cap bits have been already read, this doesn't read again but
1362 * returns the cached value.
1364 u32 snd_hda_query_pin_caps(struct hda_codec *codec, hda_nid_t nid)
1366 return query_caps_hash(codec, nid, HDA_HASH_PINCAP_KEY(nid),
1367 read_pin_cap);
1369 EXPORT_SYMBOL_HDA(snd_hda_query_pin_caps);
1372 * snd_hda_pin_sense - execute pin sense measurement
1373 * @codec: the CODEC to sense
1374 * @nid: the pin NID to sense
1376 * Execute necessary pin sense measurement and return its Presence Detect,
1377 * Impedance, ELD Valid etc. status bits.
1379 u32 snd_hda_pin_sense(struct hda_codec *codec, hda_nid_t nid)
1381 u32 pincap;
1383 if (!codec->no_trigger_sense) {
1384 pincap = snd_hda_query_pin_caps(codec, nid);
1385 if (pincap & AC_PINCAP_TRIG_REQ) /* need trigger? */
1386 snd_hda_codec_read(codec, nid, 0,
1387 AC_VERB_SET_PIN_SENSE, 0);
1389 return snd_hda_codec_read(codec, nid, 0,
1390 AC_VERB_GET_PIN_SENSE, 0);
1392 EXPORT_SYMBOL_HDA(snd_hda_pin_sense);
1395 * snd_hda_jack_detect - query pin Presence Detect status
1396 * @codec: the CODEC to sense
1397 * @nid: the pin NID to sense
1399 * Query and return the pin's Presence Detect status.
1401 int snd_hda_jack_detect(struct hda_codec *codec, hda_nid_t nid)
1403 u32 sense = snd_hda_pin_sense(codec, nid);
1404 return !!(sense & AC_PINSENSE_PRESENCE);
1406 EXPORT_SYMBOL_HDA(snd_hda_jack_detect);
1409 * read the current volume to info
1410 * if the cache exists, read the cache value.
1412 static unsigned int get_vol_mute(struct hda_codec *codec,
1413 struct hda_amp_info *info, hda_nid_t nid,
1414 int ch, int direction, int index)
1416 u32 val, parm;
1418 if (info->head.val & INFO_AMP_VOL(ch))
1419 return info->vol[ch];
1421 parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
1422 parm |= direction == HDA_OUTPUT ? AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
1423 parm |= index;
1424 val = snd_hda_codec_read(codec, nid, 0,
1425 AC_VERB_GET_AMP_GAIN_MUTE, parm);
1426 info->vol[ch] = val & 0xff;
1427 info->head.val |= INFO_AMP_VOL(ch);
1428 return info->vol[ch];
1432 * write the current volume in info to the h/w and update the cache
1434 static void put_vol_mute(struct hda_codec *codec, struct hda_amp_info *info,
1435 hda_nid_t nid, int ch, int direction, int index,
1436 int val)
1438 u32 parm;
1440 parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
1441 parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
1442 parm |= index << AC_AMP_SET_INDEX_SHIFT;
1443 parm |= val;
1444 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
1445 info->vol[ch] = val;
1449 * snd_hda_codec_amp_read - Read AMP value
1450 * @codec: HD-audio codec
1451 * @nid: NID to read the AMP value
1452 * @ch: channel (left=0 or right=1)
1453 * @direction: #HDA_INPUT or #HDA_OUTPUT
1454 * @index: the index value (only for input direction)
1456 * Read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
1458 int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
1459 int direction, int index)
1461 struct hda_amp_info *info;
1462 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
1463 if (!info)
1464 return 0;
1465 return get_vol_mute(codec, info, nid, ch, direction, index);
1467 EXPORT_SYMBOL_HDA(snd_hda_codec_amp_read);
1470 * snd_hda_codec_amp_update - update the AMP value
1471 * @codec: HD-audio codec
1472 * @nid: NID to read the AMP value
1473 * @ch: channel (left=0 or right=1)
1474 * @direction: #HDA_INPUT or #HDA_OUTPUT
1475 * @idx: the index value (only for input direction)
1476 * @mask: bit mask to set
1477 * @val: the bits value to set
1479 * Update the AMP value with a bit mask.
1480 * Returns 0 if the value is unchanged, 1 if changed.
1482 int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
1483 int direction, int idx, int mask, int val)
1485 struct hda_amp_info *info;
1487 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, idx));
1488 if (!info)
1489 return 0;
1490 val &= mask;
1491 val |= get_vol_mute(codec, info, nid, ch, direction, idx) & ~mask;
1492 if (info->vol[ch] == val)
1493 return 0;
1494 put_vol_mute(codec, info, nid, ch, direction, idx, val);
1495 return 1;
1497 EXPORT_SYMBOL_HDA(snd_hda_codec_amp_update);
1500 * snd_hda_codec_amp_stereo - update the AMP stereo values
1501 * @codec: HD-audio codec
1502 * @nid: NID to read the AMP value
1503 * @direction: #HDA_INPUT or #HDA_OUTPUT
1504 * @idx: the index value (only for input direction)
1505 * @mask: bit mask to set
1506 * @val: the bits value to set
1508 * Update the AMP values like snd_hda_codec_amp_update(), but for a
1509 * stereo widget with the same mask and value.
1511 int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
1512 int direction, int idx, int mask, int val)
1514 int ch, ret = 0;
1515 for (ch = 0; ch < 2; ch++)
1516 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
1517 idx, mask, val);
1518 return ret;
1520 EXPORT_SYMBOL_HDA(snd_hda_codec_amp_stereo);
1522 #ifdef SND_HDA_NEEDS_RESUME
1524 * snd_hda_codec_resume_amp - Resume all AMP commands from the cache
1525 * @codec: HD-audio codec
1527 * Resume the all amp commands from the cache.
1529 void snd_hda_codec_resume_amp(struct hda_codec *codec)
1531 struct hda_amp_info *buffer = codec->amp_cache.buf.list;
1532 int i;
1534 for (i = 0; i < codec->amp_cache.buf.used; i++, buffer++) {
1535 u32 key = buffer->head.key;
1536 hda_nid_t nid;
1537 unsigned int idx, dir, ch;
1538 if (!key)
1539 continue;
1540 nid = key & 0xff;
1541 idx = (key >> 16) & 0xff;
1542 dir = (key >> 24) & 0xff;
1543 for (ch = 0; ch < 2; ch++) {
1544 if (!(buffer->head.val & INFO_AMP_VOL(ch)))
1545 continue;
1546 put_vol_mute(codec, buffer, nid, ch, dir, idx,
1547 buffer->vol[ch]);
1551 EXPORT_SYMBOL_HDA(snd_hda_codec_resume_amp);
1552 #endif /* SND_HDA_NEEDS_RESUME */
1555 * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer
1557 * The control element is supposed to have the private_value field
1558 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1560 int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
1561 struct snd_ctl_elem_info *uinfo)
1563 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1564 u16 nid = get_amp_nid(kcontrol);
1565 u8 chs = get_amp_channels(kcontrol);
1566 int dir = get_amp_direction(kcontrol);
1567 unsigned int ofs = get_amp_offset(kcontrol);
1568 u32 caps;
1570 caps = query_amp_caps(codec, nid, dir);
1571 /* num steps */
1572 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1573 if (!caps) {
1574 printk(KERN_WARNING "hda_codec: "
1575 "num_steps = 0 for NID=0x%x (ctl = %s)\n", nid,
1576 kcontrol->id.name);
1577 return -EINVAL;
1579 if (ofs < caps)
1580 caps -= ofs;
1581 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1582 uinfo->count = chs == 3 ? 2 : 1;
1583 uinfo->value.integer.min = 0;
1584 uinfo->value.integer.max = caps;
1585 return 0;
1587 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_info);
1590 static inline unsigned int
1591 read_amp_value(struct hda_codec *codec, hda_nid_t nid,
1592 int ch, int dir, int idx, unsigned int ofs)
1594 unsigned int val;
1595 val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
1596 val &= HDA_AMP_VOLMASK;
1597 if (val >= ofs)
1598 val -= ofs;
1599 else
1600 val = 0;
1601 return val;
1604 static inline int
1605 update_amp_value(struct hda_codec *codec, hda_nid_t nid,
1606 int ch, int dir, int idx, unsigned int ofs,
1607 unsigned int val)
1609 if (val > 0)
1610 val += ofs;
1611 return snd_hda_codec_amp_update(codec, nid, ch, dir, idx,
1612 HDA_AMP_VOLMASK, val);
1616 * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume
1618 * The control element is supposed to have the private_value field
1619 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1621 int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
1622 struct snd_ctl_elem_value *ucontrol)
1624 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1625 hda_nid_t nid = get_amp_nid(kcontrol);
1626 int chs = get_amp_channels(kcontrol);
1627 int dir = get_amp_direction(kcontrol);
1628 int idx = get_amp_index(kcontrol);
1629 unsigned int ofs = get_amp_offset(kcontrol);
1630 long *valp = ucontrol->value.integer.value;
1632 if (chs & 1)
1633 *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs);
1634 if (chs & 2)
1635 *valp = read_amp_value(codec, nid, 1, dir, idx, ofs);
1636 return 0;
1638 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_get);
1641 * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume
1643 * The control element is supposed to have the private_value field
1644 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1646 int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
1647 struct snd_ctl_elem_value *ucontrol)
1649 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1650 hda_nid_t nid = get_amp_nid(kcontrol);
1651 int chs = get_amp_channels(kcontrol);
1652 int dir = get_amp_direction(kcontrol);
1653 int idx = get_amp_index(kcontrol);
1654 unsigned int ofs = get_amp_offset(kcontrol);
1655 long *valp = ucontrol->value.integer.value;
1656 int change = 0;
1658 snd_hda_power_up(codec);
1659 if (chs & 1) {
1660 change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp);
1661 valp++;
1663 if (chs & 2)
1664 change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp);
1665 snd_hda_power_down(codec);
1666 return change;
1668 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_put);
1671 * snd_hda_mixer_amp_volume_put - TLV callback for a standard AMP mixer volume
1673 * The control element is supposed to have the private_value field
1674 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1676 int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
1677 unsigned int size, unsigned int __user *_tlv)
1679 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1680 hda_nid_t nid = get_amp_nid(kcontrol);
1681 int dir = get_amp_direction(kcontrol);
1682 unsigned int ofs = get_amp_offset(kcontrol);
1683 u32 caps, val1, val2;
1685 if (size < 4 * sizeof(unsigned int))
1686 return -ENOMEM;
1687 caps = query_amp_caps(codec, nid, dir);
1688 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
1689 val2 = (val2 + 1) * 25;
1690 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
1691 val1 += ofs;
1692 val1 = ((int)val1) * ((int)val2);
1693 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
1694 return -EFAULT;
1695 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
1696 return -EFAULT;
1697 if (put_user(val1, _tlv + 2))
1698 return -EFAULT;
1699 if (put_user(val2, _tlv + 3))
1700 return -EFAULT;
1701 return 0;
1703 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_tlv);
1706 * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control
1707 * @codec: HD-audio codec
1708 * @nid: NID of a reference widget
1709 * @dir: #HDA_INPUT or #HDA_OUTPUT
1710 * @tlv: TLV data to be stored, at least 4 elements
1712 * Set (static) TLV data for a virtual master volume using the AMP caps
1713 * obtained from the reference NID.
1714 * The volume range is recalculated as if the max volume is 0dB.
1716 void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
1717 unsigned int *tlv)
1719 u32 caps;
1720 int nums, step;
1722 caps = query_amp_caps(codec, nid, dir);
1723 nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1724 step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
1725 step = (step + 1) * 25;
1726 tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
1727 tlv[1] = 2 * sizeof(unsigned int);
1728 tlv[2] = -nums * step;
1729 tlv[3] = step;
1731 EXPORT_SYMBOL_HDA(snd_hda_set_vmaster_tlv);
1733 /* find a mixer control element with the given name */
1734 static struct snd_kcontrol *
1735 _snd_hda_find_mixer_ctl(struct hda_codec *codec,
1736 const char *name, int idx)
1738 struct snd_ctl_elem_id id;
1739 memset(&id, 0, sizeof(id));
1740 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1741 id.index = idx;
1742 if (snd_BUG_ON(strlen(name) >= sizeof(id.name)))
1743 return NULL;
1744 strcpy(id.name, name);
1745 return snd_ctl_find_id(codec->bus->card, &id);
1749 * snd_hda_find_mixer_ctl - Find a mixer control element with the given name
1750 * @codec: HD-audio codec
1751 * @name: ctl id name string
1753 * Get the control element with the given id string and IFACE_MIXER.
1755 struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
1756 const char *name)
1758 return _snd_hda_find_mixer_ctl(codec, name, 0);
1760 EXPORT_SYMBOL_HDA(snd_hda_find_mixer_ctl);
1763 * snd_hda_ctl_add - Add a control element and assign to the codec
1764 * @codec: HD-audio codec
1765 * @nid: corresponding NID (optional)
1766 * @kctl: the control element to assign
1768 * Add the given control element to an array inside the codec instance.
1769 * All control elements belonging to a codec are supposed to be added
1770 * by this function so that a proper clean-up works at the free or
1771 * reconfiguration time.
1773 * If non-zero @nid is passed, the NID is assigned to the control element.
1774 * The assignment is shown in the codec proc file.
1776 * snd_hda_ctl_add() checks the control subdev id field whether
1777 * #HDA_SUBDEV_NID_FLAG bit is set. If set (and @nid is zero), the lower
1778 * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit
1779 * specifies if kctl->private_value is a HDA amplifier value.
1781 int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid,
1782 struct snd_kcontrol *kctl)
1784 int err;
1785 unsigned short flags = 0;
1786 struct hda_nid_item *item;
1788 if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) {
1789 flags |= HDA_NID_ITEM_AMP;
1790 if (nid == 0)
1791 nid = get_amp_nid_(kctl->private_value);
1793 if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0)
1794 nid = kctl->id.subdevice & 0xffff;
1795 if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG))
1796 kctl->id.subdevice = 0;
1797 err = snd_ctl_add(codec->bus->card, kctl);
1798 if (err < 0)
1799 return err;
1800 item = snd_array_new(&codec->mixers);
1801 if (!item)
1802 return -ENOMEM;
1803 item->kctl = kctl;
1804 item->nid = nid;
1805 item->flags = flags;
1806 return 0;
1808 EXPORT_SYMBOL_HDA(snd_hda_ctl_add);
1811 * snd_hda_add_nid - Assign a NID to a control element
1812 * @codec: HD-audio codec
1813 * @nid: corresponding NID (optional)
1814 * @kctl: the control element to assign
1815 * @index: index to kctl
1817 * Add the given control element to an array inside the codec instance.
1818 * This function is used when #snd_hda_ctl_add cannot be used for 1:1
1819 * NID:KCTL mapping - for example "Capture Source" selector.
1821 int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl,
1822 unsigned int index, hda_nid_t nid)
1824 struct hda_nid_item *item;
1826 if (nid > 0) {
1827 item = snd_array_new(&codec->nids);
1828 if (!item)
1829 return -ENOMEM;
1830 item->kctl = kctl;
1831 item->index = index;
1832 item->nid = nid;
1833 return 0;
1835 printk(KERN_ERR "hda-codec: no NID for mapping control %s:%d:%d\n",
1836 kctl->id.name, kctl->id.index, index);
1837 return -EINVAL;
1839 EXPORT_SYMBOL_HDA(snd_hda_add_nid);
1842 * snd_hda_ctls_clear - Clear all controls assigned to the given codec
1843 * @codec: HD-audio codec
1845 void snd_hda_ctls_clear(struct hda_codec *codec)
1847 int i;
1848 struct hda_nid_item *items = codec->mixers.list;
1849 for (i = 0; i < codec->mixers.used; i++)
1850 snd_ctl_remove(codec->bus->card, items[i].kctl);
1851 snd_array_free(&codec->mixers);
1852 snd_array_free(&codec->nids);
1855 /* pseudo device locking
1856 * toggle card->shutdown to allow/disallow the device access (as a hack)
1858 static int hda_lock_devices(struct snd_card *card)
1860 spin_lock(&card->files_lock);
1861 if (card->shutdown) {
1862 spin_unlock(&card->files_lock);
1863 return -EINVAL;
1865 card->shutdown = 1;
1866 spin_unlock(&card->files_lock);
1867 return 0;
1870 static void hda_unlock_devices(struct snd_card *card)
1872 spin_lock(&card->files_lock);
1873 card->shutdown = 0;
1874 spin_unlock(&card->files_lock);
1878 * snd_hda_codec_reset - Clear all objects assigned to the codec
1879 * @codec: HD-audio codec
1881 * This frees the all PCM and control elements assigned to the codec, and
1882 * clears the caches and restores the pin default configurations.
1884 * When a device is being used, it returns -EBSY. If successfully freed,
1885 * returns zero.
1887 int snd_hda_codec_reset(struct hda_codec *codec)
1889 struct snd_card *card = codec->bus->card;
1890 int i, pcm;
1892 if (hda_lock_devices(card) < 0)
1893 return -EBUSY;
1894 /* check whether the codec isn't used by any mixer or PCM streams */
1895 if (!list_empty(&card->ctl_files)) {
1896 hda_unlock_devices(card);
1897 return -EBUSY;
1899 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
1900 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
1901 if (!cpcm->pcm)
1902 continue;
1903 if (cpcm->pcm->streams[0].substream_opened ||
1904 cpcm->pcm->streams[1].substream_opened) {
1905 hda_unlock_devices(card);
1906 return -EBUSY;
1910 /* OK, let it free */
1912 #ifdef CONFIG_SND_HDA_POWER_SAVE
1913 cancel_delayed_work(&codec->power_work);
1914 flush_workqueue(codec->bus->workq);
1915 #endif
1916 snd_hda_ctls_clear(codec);
1917 /* relase PCMs */
1918 for (i = 0; i < codec->num_pcms; i++) {
1919 if (codec->pcm_info[i].pcm) {
1920 snd_device_free(card, codec->pcm_info[i].pcm);
1921 clear_bit(codec->pcm_info[i].device,
1922 codec->bus->pcm_dev_bits);
1925 if (codec->patch_ops.free)
1926 codec->patch_ops.free(codec);
1927 codec->proc_widget_hook = NULL;
1928 codec->spec = NULL;
1929 free_hda_cache(&codec->amp_cache);
1930 free_hda_cache(&codec->cmd_cache);
1931 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
1932 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
1933 /* free only driver_pins so that init_pins + user_pins are restored */
1934 snd_array_free(&codec->driver_pins);
1935 restore_pincfgs(codec);
1936 codec->num_pcms = 0;
1937 codec->pcm_info = NULL;
1938 codec->preset = NULL;
1939 memset(&codec->patch_ops, 0, sizeof(codec->patch_ops));
1940 codec->slave_dig_outs = NULL;
1941 codec->spdif_status_reset = 0;
1942 module_put(codec->owner);
1943 codec->owner = NULL;
1945 /* allow device access again */
1946 hda_unlock_devices(card);
1947 return 0;
1951 * snd_hda_add_vmaster - create a virtual master control and add slaves
1952 * @codec: HD-audio codec
1953 * @name: vmaster control name
1954 * @tlv: TLV data (optional)
1955 * @slaves: slave control names (optional)
1957 * Create a virtual master control with the given name. The TLV data
1958 * must be either NULL or a valid data.
1960 * @slaves is a NULL-terminated array of strings, each of which is a
1961 * slave control name. All controls with these names are assigned to
1962 * the new virtual master control.
1964 * This function returns zero if successful or a negative error code.
1966 int snd_hda_add_vmaster(struct hda_codec *codec, char *name,
1967 unsigned int *tlv, const char **slaves)
1969 struct snd_kcontrol *kctl;
1970 const char **s;
1971 int err;
1973 for (s = slaves; *s && !snd_hda_find_mixer_ctl(codec, *s); s++)
1975 if (!*s) {
1976 snd_printdd("No slave found for %s\n", name);
1977 return 0;
1979 kctl = snd_ctl_make_virtual_master(name, tlv);
1980 if (!kctl)
1981 return -ENOMEM;
1982 err = snd_hda_ctl_add(codec, 0, kctl);
1983 if (err < 0)
1984 return err;
1986 for (s = slaves; *s; s++) {
1987 struct snd_kcontrol *sctl;
1988 int i = 0;
1989 for (;;) {
1990 sctl = _snd_hda_find_mixer_ctl(codec, *s, i);
1991 if (!sctl) {
1992 if (!i)
1993 snd_printdd("Cannot find slave %s, "
1994 "skipped\n", *s);
1995 break;
1997 err = snd_ctl_add_slave(kctl, sctl);
1998 if (err < 0)
1999 return err;
2000 i++;
2003 return 0;
2005 EXPORT_SYMBOL_HDA(snd_hda_add_vmaster);
2008 * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch
2010 * The control element is supposed to have the private_value field
2011 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2013 int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
2014 struct snd_ctl_elem_info *uinfo)
2016 int chs = get_amp_channels(kcontrol);
2018 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2019 uinfo->count = chs == 3 ? 2 : 1;
2020 uinfo->value.integer.min = 0;
2021 uinfo->value.integer.max = 1;
2022 return 0;
2024 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_info);
2027 * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch
2029 * The control element is supposed to have the private_value field
2030 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2032 int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
2033 struct snd_ctl_elem_value *ucontrol)
2035 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2036 hda_nid_t nid = get_amp_nid(kcontrol);
2037 int chs = get_amp_channels(kcontrol);
2038 int dir = get_amp_direction(kcontrol);
2039 int idx = get_amp_index(kcontrol);
2040 long *valp = ucontrol->value.integer.value;
2042 if (chs & 1)
2043 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
2044 HDA_AMP_MUTE) ? 0 : 1;
2045 if (chs & 2)
2046 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
2047 HDA_AMP_MUTE) ? 0 : 1;
2048 return 0;
2050 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_get);
2053 * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch
2055 * The control element is supposed to have the private_value field
2056 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2058 int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
2059 struct snd_ctl_elem_value *ucontrol)
2061 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2062 hda_nid_t nid = get_amp_nid(kcontrol);
2063 int chs = get_amp_channels(kcontrol);
2064 int dir = get_amp_direction(kcontrol);
2065 int idx = get_amp_index(kcontrol);
2066 long *valp = ucontrol->value.integer.value;
2067 int change = 0;
2069 snd_hda_power_up(codec);
2070 if (chs & 1) {
2071 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
2072 HDA_AMP_MUTE,
2073 *valp ? 0 : HDA_AMP_MUTE);
2074 valp++;
2076 if (chs & 2)
2077 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
2078 HDA_AMP_MUTE,
2079 *valp ? 0 : HDA_AMP_MUTE);
2080 #ifdef CONFIG_SND_HDA_POWER_SAVE
2081 if (codec->patch_ops.check_power_status)
2082 codec->patch_ops.check_power_status(codec, nid);
2083 #endif
2084 snd_hda_power_down(codec);
2085 return change;
2087 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put);
2089 #ifdef CONFIG_SND_HDA_INPUT_BEEP
2091 * snd_hda_mixer_amp_switch_put_beep - Put callback for a beep AMP switch
2093 * This function calls snd_hda_enable_beep_device(), which behaves differently
2094 * depending on beep_mode option.
2096 int snd_hda_mixer_amp_switch_put_beep(struct snd_kcontrol *kcontrol,
2097 struct snd_ctl_elem_value *ucontrol)
2099 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2100 long *valp = ucontrol->value.integer.value;
2102 snd_hda_enable_beep_device(codec, *valp);
2103 return snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2105 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put_beep);
2106 #endif /* CONFIG_SND_HDA_INPUT_BEEP */
2109 * bound volume controls
2111 * bind multiple volumes (# indices, from 0)
2114 #define AMP_VAL_IDX_SHIFT 19
2115 #define AMP_VAL_IDX_MASK (0x0f<<19)
2118 * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control
2120 * The control element is supposed to have the private_value field
2121 * set up via HDA_BIND_MUTE*() macros.
2123 int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
2124 struct snd_ctl_elem_value *ucontrol)
2126 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2127 unsigned long pval;
2128 int err;
2130 mutex_lock(&codec->control_mutex);
2131 pval = kcontrol->private_value;
2132 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
2133 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
2134 kcontrol->private_value = pval;
2135 mutex_unlock(&codec->control_mutex);
2136 return err;
2138 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_get);
2141 * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control
2143 * The control element is supposed to have the private_value field
2144 * set up via HDA_BIND_MUTE*() macros.
2146 int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
2147 struct snd_ctl_elem_value *ucontrol)
2149 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2150 unsigned long pval;
2151 int i, indices, err = 0, change = 0;
2153 mutex_lock(&codec->control_mutex);
2154 pval = kcontrol->private_value;
2155 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
2156 for (i = 0; i < indices; i++) {
2157 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
2158 (i << AMP_VAL_IDX_SHIFT);
2159 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2160 if (err < 0)
2161 break;
2162 change |= err;
2164 kcontrol->private_value = pval;
2165 mutex_unlock(&codec->control_mutex);
2166 return err < 0 ? err : change;
2168 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_put);
2171 * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control
2173 * The control element is supposed to have the private_value field
2174 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2176 int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
2177 struct snd_ctl_elem_info *uinfo)
2179 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2180 struct hda_bind_ctls *c;
2181 int err;
2183 mutex_lock(&codec->control_mutex);
2184 c = (struct hda_bind_ctls *)kcontrol->private_value;
2185 kcontrol->private_value = *c->values;
2186 err = c->ops->info(kcontrol, uinfo);
2187 kcontrol->private_value = (long)c;
2188 mutex_unlock(&codec->control_mutex);
2189 return err;
2191 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_info);
2194 * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control
2196 * The control element is supposed to have the private_value field
2197 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2199 int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
2200 struct snd_ctl_elem_value *ucontrol)
2202 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2203 struct hda_bind_ctls *c;
2204 int err;
2206 mutex_lock(&codec->control_mutex);
2207 c = (struct hda_bind_ctls *)kcontrol->private_value;
2208 kcontrol->private_value = *c->values;
2209 err = c->ops->get(kcontrol, ucontrol);
2210 kcontrol->private_value = (long)c;
2211 mutex_unlock(&codec->control_mutex);
2212 return err;
2214 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_get);
2217 * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control
2219 * The control element is supposed to have the private_value field
2220 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2222 int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
2223 struct snd_ctl_elem_value *ucontrol)
2225 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2226 struct hda_bind_ctls *c;
2227 unsigned long *vals;
2228 int err = 0, change = 0;
2230 mutex_lock(&codec->control_mutex);
2231 c = (struct hda_bind_ctls *)kcontrol->private_value;
2232 for (vals = c->values; *vals; vals++) {
2233 kcontrol->private_value = *vals;
2234 err = c->ops->put(kcontrol, ucontrol);
2235 if (err < 0)
2236 break;
2237 change |= err;
2239 kcontrol->private_value = (long)c;
2240 mutex_unlock(&codec->control_mutex);
2241 return err < 0 ? err : change;
2243 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_put);
2246 * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control
2248 * The control element is supposed to have the private_value field
2249 * set up via HDA_BIND_VOL() macro.
2251 int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2252 unsigned int size, unsigned int __user *tlv)
2254 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2255 struct hda_bind_ctls *c;
2256 int err;
2258 mutex_lock(&codec->control_mutex);
2259 c = (struct hda_bind_ctls *)kcontrol->private_value;
2260 kcontrol->private_value = *c->values;
2261 err = c->ops->tlv(kcontrol, op_flag, size, tlv);
2262 kcontrol->private_value = (long)c;
2263 mutex_unlock(&codec->control_mutex);
2264 return err;
2266 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_tlv);
2268 struct hda_ctl_ops snd_hda_bind_vol = {
2269 .info = snd_hda_mixer_amp_volume_info,
2270 .get = snd_hda_mixer_amp_volume_get,
2271 .put = snd_hda_mixer_amp_volume_put,
2272 .tlv = snd_hda_mixer_amp_tlv
2274 EXPORT_SYMBOL_HDA(snd_hda_bind_vol);
2276 struct hda_ctl_ops snd_hda_bind_sw = {
2277 .info = snd_hda_mixer_amp_switch_info,
2278 .get = snd_hda_mixer_amp_switch_get,
2279 .put = snd_hda_mixer_amp_switch_put,
2280 .tlv = snd_hda_mixer_amp_tlv
2282 EXPORT_SYMBOL_HDA(snd_hda_bind_sw);
2285 * SPDIF out controls
2288 static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
2289 struct snd_ctl_elem_info *uinfo)
2291 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
2292 uinfo->count = 1;
2293 return 0;
2296 static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
2297 struct snd_ctl_elem_value *ucontrol)
2299 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2300 IEC958_AES0_NONAUDIO |
2301 IEC958_AES0_CON_EMPHASIS_5015 |
2302 IEC958_AES0_CON_NOT_COPYRIGHT;
2303 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
2304 IEC958_AES1_CON_ORIGINAL;
2305 return 0;
2308 static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
2309 struct snd_ctl_elem_value *ucontrol)
2311 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2312 IEC958_AES0_NONAUDIO |
2313 IEC958_AES0_PRO_EMPHASIS_5015;
2314 return 0;
2317 static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
2318 struct snd_ctl_elem_value *ucontrol)
2320 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2322 ucontrol->value.iec958.status[0] = codec->spdif_status & 0xff;
2323 ucontrol->value.iec958.status[1] = (codec->spdif_status >> 8) & 0xff;
2324 ucontrol->value.iec958.status[2] = (codec->spdif_status >> 16) & 0xff;
2325 ucontrol->value.iec958.status[3] = (codec->spdif_status >> 24) & 0xff;
2327 return 0;
2330 /* convert from SPDIF status bits to HDA SPDIF bits
2331 * bit 0 (DigEn) is always set zero (to be filled later)
2333 static unsigned short convert_from_spdif_status(unsigned int sbits)
2335 unsigned short val = 0;
2337 if (sbits & IEC958_AES0_PROFESSIONAL)
2338 val |= AC_DIG1_PROFESSIONAL;
2339 if (sbits & IEC958_AES0_NONAUDIO)
2340 val |= AC_DIG1_NONAUDIO;
2341 if (sbits & IEC958_AES0_PROFESSIONAL) {
2342 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
2343 IEC958_AES0_PRO_EMPHASIS_5015)
2344 val |= AC_DIG1_EMPHASIS;
2345 } else {
2346 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
2347 IEC958_AES0_CON_EMPHASIS_5015)
2348 val |= AC_DIG1_EMPHASIS;
2349 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
2350 val |= AC_DIG1_COPYRIGHT;
2351 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
2352 val |= AC_DIG1_LEVEL;
2353 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
2355 return val;
2358 /* convert to SPDIF status bits from HDA SPDIF bits
2360 static unsigned int convert_to_spdif_status(unsigned short val)
2362 unsigned int sbits = 0;
2364 if (val & AC_DIG1_NONAUDIO)
2365 sbits |= IEC958_AES0_NONAUDIO;
2366 if (val & AC_DIG1_PROFESSIONAL)
2367 sbits |= IEC958_AES0_PROFESSIONAL;
2368 if (sbits & IEC958_AES0_PROFESSIONAL) {
2369 if (sbits & AC_DIG1_EMPHASIS)
2370 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
2371 } else {
2372 if (val & AC_DIG1_EMPHASIS)
2373 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
2374 if (!(val & AC_DIG1_COPYRIGHT))
2375 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
2376 if (val & AC_DIG1_LEVEL)
2377 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
2378 sbits |= val & (0x7f << 8);
2380 return sbits;
2383 /* set digital convert verbs both for the given NID and its slaves */
2384 static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
2385 int verb, int val)
2387 hda_nid_t *d;
2389 snd_hda_codec_write_cache(codec, nid, 0, verb, val);
2390 d = codec->slave_dig_outs;
2391 if (!d)
2392 return;
2393 for (; *d; d++)
2394 snd_hda_codec_write_cache(codec, *d, 0, verb, val);
2397 static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
2398 int dig1, int dig2)
2400 if (dig1 != -1)
2401 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1);
2402 if (dig2 != -1)
2403 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2);
2406 static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
2407 struct snd_ctl_elem_value *ucontrol)
2409 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2410 hda_nid_t nid = kcontrol->private_value;
2411 unsigned short val;
2412 int change;
2414 mutex_lock(&codec->spdif_mutex);
2415 codec->spdif_status = ucontrol->value.iec958.status[0] |
2416 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
2417 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
2418 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
2419 val = convert_from_spdif_status(codec->spdif_status);
2420 val |= codec->spdif_ctls & 1;
2421 change = codec->spdif_ctls != val;
2422 codec->spdif_ctls = val;
2424 if (change)
2425 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
2427 mutex_unlock(&codec->spdif_mutex);
2428 return change;
2431 #define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
2433 static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
2434 struct snd_ctl_elem_value *ucontrol)
2436 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2438 ucontrol->value.integer.value[0] = codec->spdif_ctls & AC_DIG1_ENABLE;
2439 return 0;
2442 static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
2443 struct snd_ctl_elem_value *ucontrol)
2445 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2446 hda_nid_t nid = kcontrol->private_value;
2447 unsigned short val;
2448 int change;
2450 mutex_lock(&codec->spdif_mutex);
2451 val = codec->spdif_ctls & ~AC_DIG1_ENABLE;
2452 if (ucontrol->value.integer.value[0])
2453 val |= AC_DIG1_ENABLE;
2454 change = codec->spdif_ctls != val;
2455 if (change) {
2456 codec->spdif_ctls = val;
2457 set_dig_out_convert(codec, nid, val & 0xff, -1);
2458 /* unmute amp switch (if any) */
2459 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
2460 (val & AC_DIG1_ENABLE))
2461 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
2462 HDA_AMP_MUTE, 0);
2464 mutex_unlock(&codec->spdif_mutex);
2465 return change;
2468 static struct snd_kcontrol_new dig_mixes[] = {
2470 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2471 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2472 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
2473 .info = snd_hda_spdif_mask_info,
2474 .get = snd_hda_spdif_cmask_get,
2477 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2478 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2479 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK),
2480 .info = snd_hda_spdif_mask_info,
2481 .get = snd_hda_spdif_pmask_get,
2484 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2485 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
2486 .info = snd_hda_spdif_mask_info,
2487 .get = snd_hda_spdif_default_get,
2488 .put = snd_hda_spdif_default_put,
2491 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2492 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
2493 .info = snd_hda_spdif_out_switch_info,
2494 .get = snd_hda_spdif_out_switch_get,
2495 .put = snd_hda_spdif_out_switch_put,
2497 { } /* end */
2500 #define SPDIF_MAX_IDX 4 /* 4 instances should be enough to probe */
2503 * snd_hda_create_spdif_out_ctls - create Output SPDIF-related controls
2504 * @codec: the HDA codec
2505 * @nid: audio out widget NID
2507 * Creates controls related with the SPDIF output.
2508 * Called from each patch supporting the SPDIF out.
2510 * Returns 0 if successful, or a negative error code.
2512 int snd_hda_create_spdif_out_ctls(struct hda_codec *codec, hda_nid_t nid)
2514 int err;
2515 struct snd_kcontrol *kctl;
2516 struct snd_kcontrol_new *dig_mix;
2517 int idx;
2519 for (idx = 0; idx < SPDIF_MAX_IDX; idx++) {
2520 if (!_snd_hda_find_mixer_ctl(codec, "IEC958 Playback Switch",
2521 idx))
2522 break;
2524 if (idx >= SPDIF_MAX_IDX) {
2525 printk(KERN_ERR "hda_codec: too many IEC958 outputs\n");
2526 return -EBUSY;
2528 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
2529 kctl = snd_ctl_new1(dig_mix, codec);
2530 if (!kctl)
2531 return -ENOMEM;
2532 kctl->id.index = idx;
2533 kctl->private_value = nid;
2534 err = snd_hda_ctl_add(codec, nid, kctl);
2535 if (err < 0)
2536 return err;
2538 codec->spdif_ctls =
2539 snd_hda_codec_read(codec, nid, 0,
2540 AC_VERB_GET_DIGI_CONVERT_1, 0);
2541 codec->spdif_status = convert_to_spdif_status(codec->spdif_ctls);
2542 return 0;
2544 EXPORT_SYMBOL_HDA(snd_hda_create_spdif_out_ctls);
2547 * SPDIF sharing with analog output
2549 static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
2550 struct snd_ctl_elem_value *ucontrol)
2552 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
2553 ucontrol->value.integer.value[0] = mout->share_spdif;
2554 return 0;
2557 static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
2558 struct snd_ctl_elem_value *ucontrol)
2560 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
2561 mout->share_spdif = !!ucontrol->value.integer.value[0];
2562 return 0;
2565 static struct snd_kcontrol_new spdif_share_sw = {
2566 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2567 .name = "IEC958 Default PCM Playback Switch",
2568 .info = snd_ctl_boolean_mono_info,
2569 .get = spdif_share_sw_get,
2570 .put = spdif_share_sw_put,
2574 * snd_hda_create_spdif_share_sw - create Default PCM switch
2575 * @codec: the HDA codec
2576 * @mout: multi-out instance
2578 int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
2579 struct hda_multi_out *mout)
2581 if (!mout->dig_out_nid)
2582 return 0;
2583 /* ATTENTION: here mout is passed as private_data, instead of codec */
2584 return snd_hda_ctl_add(codec, mout->dig_out_nid,
2585 snd_ctl_new1(&spdif_share_sw, mout));
2587 EXPORT_SYMBOL_HDA(snd_hda_create_spdif_share_sw);
2590 * SPDIF input
2593 #define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
2595 static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
2596 struct snd_ctl_elem_value *ucontrol)
2598 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2600 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
2601 return 0;
2604 static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
2605 struct snd_ctl_elem_value *ucontrol)
2607 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2608 hda_nid_t nid = kcontrol->private_value;
2609 unsigned int val = !!ucontrol->value.integer.value[0];
2610 int change;
2612 mutex_lock(&codec->spdif_mutex);
2613 change = codec->spdif_in_enable != val;
2614 if (change) {
2615 codec->spdif_in_enable = val;
2616 snd_hda_codec_write_cache(codec, nid, 0,
2617 AC_VERB_SET_DIGI_CONVERT_1, val);
2619 mutex_unlock(&codec->spdif_mutex);
2620 return change;
2623 static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
2624 struct snd_ctl_elem_value *ucontrol)
2626 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2627 hda_nid_t nid = kcontrol->private_value;
2628 unsigned short val;
2629 unsigned int sbits;
2631 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0);
2632 sbits = convert_to_spdif_status(val);
2633 ucontrol->value.iec958.status[0] = sbits;
2634 ucontrol->value.iec958.status[1] = sbits >> 8;
2635 ucontrol->value.iec958.status[2] = sbits >> 16;
2636 ucontrol->value.iec958.status[3] = sbits >> 24;
2637 return 0;
2640 static struct snd_kcontrol_new dig_in_ctls[] = {
2642 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2643 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH),
2644 .info = snd_hda_spdif_in_switch_info,
2645 .get = snd_hda_spdif_in_switch_get,
2646 .put = snd_hda_spdif_in_switch_put,
2649 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2650 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2651 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
2652 .info = snd_hda_spdif_mask_info,
2653 .get = snd_hda_spdif_in_status_get,
2655 { } /* end */
2659 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
2660 * @codec: the HDA codec
2661 * @nid: audio in widget NID
2663 * Creates controls related with the SPDIF input.
2664 * Called from each patch supporting the SPDIF in.
2666 * Returns 0 if successful, or a negative error code.
2668 int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
2670 int err;
2671 struct snd_kcontrol *kctl;
2672 struct snd_kcontrol_new *dig_mix;
2673 int idx;
2675 for (idx = 0; idx < SPDIF_MAX_IDX; idx++) {
2676 if (!_snd_hda_find_mixer_ctl(codec, "IEC958 Capture Switch",
2677 idx))
2678 break;
2680 if (idx >= SPDIF_MAX_IDX) {
2681 printk(KERN_ERR "hda_codec: too many IEC958 inputs\n");
2682 return -EBUSY;
2684 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
2685 kctl = snd_ctl_new1(dig_mix, codec);
2686 if (!kctl)
2687 return -ENOMEM;
2688 kctl->private_value = nid;
2689 err = snd_hda_ctl_add(codec, nid, kctl);
2690 if (err < 0)
2691 return err;
2693 codec->spdif_in_enable =
2694 snd_hda_codec_read(codec, nid, 0,
2695 AC_VERB_GET_DIGI_CONVERT_1, 0) &
2696 AC_DIG1_ENABLE;
2697 return 0;
2699 EXPORT_SYMBOL_HDA(snd_hda_create_spdif_in_ctls);
2701 #ifdef SND_HDA_NEEDS_RESUME
2703 * command cache
2706 /* build a 32bit cache key with the widget id and the command parameter */
2707 #define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
2708 #define get_cmd_cache_nid(key) ((key) & 0xff)
2709 #define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
2712 * snd_hda_codec_write_cache - send a single command with caching
2713 * @codec: the HDA codec
2714 * @nid: NID to send the command
2715 * @direct: direct flag
2716 * @verb: the verb to send
2717 * @parm: the parameter for the verb
2719 * Send a single command without waiting for response.
2721 * Returns 0 if successful, or a negative error code.
2723 int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
2724 int direct, unsigned int verb, unsigned int parm)
2726 int err = snd_hda_codec_write(codec, nid, direct, verb, parm);
2727 struct hda_cache_head *c;
2728 u32 key;
2730 if (err < 0)
2731 return err;
2732 /* parm may contain the verb stuff for get/set amp */
2733 verb = verb | (parm >> 8);
2734 parm &= 0xff;
2735 key = build_cmd_cache_key(nid, verb);
2736 mutex_lock(&codec->bus->cmd_mutex);
2737 c = get_alloc_hash(&codec->cmd_cache, key);
2738 if (c)
2739 c->val = parm;
2740 mutex_unlock(&codec->bus->cmd_mutex);
2741 return 0;
2743 EXPORT_SYMBOL_HDA(snd_hda_codec_write_cache);
2746 * snd_hda_codec_resume_cache - Resume the all commands from the cache
2747 * @codec: HD-audio codec
2749 * Execute all verbs recorded in the command caches to resume.
2751 void snd_hda_codec_resume_cache(struct hda_codec *codec)
2753 struct hda_cache_head *buffer = codec->cmd_cache.buf.list;
2754 int i;
2756 for (i = 0; i < codec->cmd_cache.buf.used; i++, buffer++) {
2757 u32 key = buffer->key;
2758 if (!key)
2759 continue;
2760 snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
2761 get_cmd_cache_cmd(key), buffer->val);
2764 EXPORT_SYMBOL_HDA(snd_hda_codec_resume_cache);
2767 * snd_hda_sequence_write_cache - sequence writes with caching
2768 * @codec: the HDA codec
2769 * @seq: VERB array to send
2771 * Send the commands sequentially from the given array.
2772 * Thte commands are recorded on cache for power-save and resume.
2773 * The array must be terminated with NID=0.
2775 void snd_hda_sequence_write_cache(struct hda_codec *codec,
2776 const struct hda_verb *seq)
2778 for (; seq->nid; seq++)
2779 snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
2780 seq->param);
2782 EXPORT_SYMBOL_HDA(snd_hda_sequence_write_cache);
2783 #endif /* SND_HDA_NEEDS_RESUME */
2786 * set power state of the codec
2788 static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
2789 unsigned int power_state)
2791 hda_nid_t nid;
2792 int i;
2794 /* this delay seems necessary to avoid click noise at power-down */
2795 if (power_state == AC_PWRST_D3)
2796 msleep(100);
2797 snd_hda_codec_read(codec, fg, 0, AC_VERB_SET_POWER_STATE,
2798 power_state);
2799 /* partial workaround for "azx_get_response timeout" */
2800 if (power_state == AC_PWRST_D0 &&
2801 (codec->vendor_id & 0xffff0000) == 0x14f10000)
2802 msleep(10);
2804 nid = codec->start_nid;
2805 for (i = 0; i < codec->num_nodes; i++, nid++) {
2806 unsigned int wcaps = get_wcaps(codec, nid);
2807 if (wcaps & AC_WCAP_POWER) {
2808 unsigned int wid_type = get_wcaps_type(wcaps);
2809 if (power_state == AC_PWRST_D3 &&
2810 wid_type == AC_WID_PIN) {
2811 unsigned int pincap;
2813 * don't power down the widget if it controls
2814 * eapd and EAPD_BTLENABLE is set.
2816 pincap = snd_hda_query_pin_caps(codec, nid);
2817 if (pincap & AC_PINCAP_EAPD) {
2818 int eapd = snd_hda_codec_read(codec,
2819 nid, 0,
2820 AC_VERB_GET_EAPD_BTLENABLE, 0);
2821 eapd &= 0x02;
2822 if (eapd)
2823 continue;
2826 snd_hda_codec_write(codec, nid, 0,
2827 AC_VERB_SET_POWER_STATE,
2828 power_state);
2832 if (power_state == AC_PWRST_D0) {
2833 unsigned long end_time;
2834 int state;
2835 /* wait until the codec reachs to D0 */
2836 end_time = jiffies + msecs_to_jiffies(500);
2837 do {
2838 state = snd_hda_codec_read(codec, fg, 0,
2839 AC_VERB_GET_POWER_STATE, 0);
2840 if (state == power_state)
2841 break;
2842 msleep(1);
2843 } while (time_after_eq(end_time, jiffies));
2847 #ifdef CONFIG_SND_HDA_HWDEP
2848 /* execute additional init verbs */
2849 static void hda_exec_init_verbs(struct hda_codec *codec)
2851 if (codec->init_verbs.list)
2852 snd_hda_sequence_write(codec, codec->init_verbs.list);
2854 #else
2855 static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
2856 #endif
2858 #ifdef SND_HDA_NEEDS_RESUME
2860 * call suspend and power-down; used both from PM and power-save
2862 static void hda_call_codec_suspend(struct hda_codec *codec)
2864 if (codec->patch_ops.suspend)
2865 codec->patch_ops.suspend(codec, PMSG_SUSPEND);
2866 hda_set_power_state(codec,
2867 codec->afg ? codec->afg : codec->mfg,
2868 AC_PWRST_D3);
2869 #ifdef CONFIG_SND_HDA_POWER_SAVE
2870 snd_hda_update_power_acct(codec);
2871 cancel_delayed_work(&codec->power_work);
2872 codec->power_on = 0;
2873 codec->power_transition = 0;
2874 codec->power_jiffies = jiffies;
2875 #endif
2879 * kick up codec; used both from PM and power-save
2881 static void hda_call_codec_resume(struct hda_codec *codec)
2883 hda_set_power_state(codec,
2884 codec->afg ? codec->afg : codec->mfg,
2885 AC_PWRST_D0);
2886 restore_pincfgs(codec); /* restore all current pin configs */
2887 restore_shutup_pins(codec);
2888 hda_exec_init_verbs(codec);
2889 if (codec->patch_ops.resume)
2890 codec->patch_ops.resume(codec);
2891 else {
2892 if (codec->patch_ops.init)
2893 codec->patch_ops.init(codec);
2894 snd_hda_codec_resume_amp(codec);
2895 snd_hda_codec_resume_cache(codec);
2898 #endif /* SND_HDA_NEEDS_RESUME */
2902 * snd_hda_build_controls - build mixer controls
2903 * @bus: the BUS
2905 * Creates mixer controls for each codec included in the bus.
2907 * Returns 0 if successful, otherwise a negative error code.
2909 int /*__devinit*/ snd_hda_build_controls(struct hda_bus *bus)
2911 struct hda_codec *codec;
2913 list_for_each_entry(codec, &bus->codec_list, list) {
2914 int err = snd_hda_codec_build_controls(codec);
2915 if (err < 0) {
2916 printk(KERN_ERR "hda_codec: cannot build controls "
2917 "for #%d (error %d)\n", codec->addr, err);
2918 err = snd_hda_codec_reset(codec);
2919 if (err < 0) {
2920 printk(KERN_ERR
2921 "hda_codec: cannot revert codec\n");
2922 return err;
2926 return 0;
2928 EXPORT_SYMBOL_HDA(snd_hda_build_controls);
2930 int snd_hda_codec_build_controls(struct hda_codec *codec)
2932 int err = 0;
2933 hda_exec_init_verbs(codec);
2934 /* continue to initialize... */
2935 if (codec->patch_ops.init)
2936 err = codec->patch_ops.init(codec);
2937 if (!err && codec->patch_ops.build_controls)
2938 err = codec->patch_ops.build_controls(codec);
2939 if (err < 0)
2940 return err;
2941 return 0;
2945 * stream formats
2947 struct hda_rate_tbl {
2948 unsigned int hz;
2949 unsigned int alsa_bits;
2950 unsigned int hda_fmt;
2953 static struct hda_rate_tbl rate_bits[] = {
2954 /* rate in Hz, ALSA rate bitmask, HDA format value */
2956 /* autodetected value used in snd_hda_query_supported_pcm */
2957 { 8000, SNDRV_PCM_RATE_8000, 0x0500 }, /* 1/6 x 48 */
2958 { 11025, SNDRV_PCM_RATE_11025, 0x4300 }, /* 1/4 x 44 */
2959 { 16000, SNDRV_PCM_RATE_16000, 0x0200 }, /* 1/3 x 48 */
2960 { 22050, SNDRV_PCM_RATE_22050, 0x4100 }, /* 1/2 x 44 */
2961 { 32000, SNDRV_PCM_RATE_32000, 0x0a00 }, /* 2/3 x 48 */
2962 { 44100, SNDRV_PCM_RATE_44100, 0x4000 }, /* 44 */
2963 { 48000, SNDRV_PCM_RATE_48000, 0x0000 }, /* 48 */
2964 { 88200, SNDRV_PCM_RATE_88200, 0x4800 }, /* 2 x 44 */
2965 { 96000, SNDRV_PCM_RATE_96000, 0x0800 }, /* 2 x 48 */
2966 { 176400, SNDRV_PCM_RATE_176400, 0x5800 },/* 4 x 44 */
2967 { 192000, SNDRV_PCM_RATE_192000, 0x1800 }, /* 4 x 48 */
2968 #define AC_PAR_PCM_RATE_BITS 11
2969 /* up to bits 10, 384kHZ isn't supported properly */
2971 /* not autodetected value */
2972 { 9600, SNDRV_PCM_RATE_KNOT, 0x0400 }, /* 1/5 x 48 */
2974 { 0 } /* terminator */
2978 * snd_hda_calc_stream_format - calculate format bitset
2979 * @rate: the sample rate
2980 * @channels: the number of channels
2981 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
2982 * @maxbps: the max. bps
2984 * Calculate the format bitset from the given rate, channels and th PCM format.
2986 * Return zero if invalid.
2988 unsigned int snd_hda_calc_stream_format(unsigned int rate,
2989 unsigned int channels,
2990 unsigned int format,
2991 unsigned int maxbps)
2993 int i;
2994 unsigned int val = 0;
2996 for (i = 0; rate_bits[i].hz; i++)
2997 if (rate_bits[i].hz == rate) {
2998 val = rate_bits[i].hda_fmt;
2999 break;
3001 if (!rate_bits[i].hz) {
3002 snd_printdd("invalid rate %d\n", rate);
3003 return 0;
3006 if (channels == 0 || channels > 8) {
3007 snd_printdd("invalid channels %d\n", channels);
3008 return 0;
3010 val |= channels - 1;
3012 switch (snd_pcm_format_width(format)) {
3013 case 8:
3014 val |= 0x00;
3015 break;
3016 case 16:
3017 val |= 0x10;
3018 break;
3019 case 20:
3020 case 24:
3021 case 32:
3022 if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE)
3023 val |= 0x40;
3024 else if (maxbps >= 24)
3025 val |= 0x30;
3026 else
3027 val |= 0x20;
3028 break;
3029 default:
3030 snd_printdd("invalid format width %d\n",
3031 snd_pcm_format_width(format));
3032 return 0;
3035 return val;
3037 EXPORT_SYMBOL_HDA(snd_hda_calc_stream_format);
3039 static unsigned int get_pcm_param(struct hda_codec *codec, hda_nid_t nid)
3041 unsigned int val = 0;
3042 if (nid != codec->afg &&
3043 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD))
3044 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
3045 if (!val || val == -1)
3046 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
3047 if (!val || val == -1)
3048 return 0;
3049 return val;
3052 static unsigned int query_pcm_param(struct hda_codec *codec, hda_nid_t nid)
3054 return query_caps_hash(codec, nid, HDA_HASH_PARPCM_KEY(nid),
3055 get_pcm_param);
3058 static unsigned int get_stream_param(struct hda_codec *codec, hda_nid_t nid)
3060 unsigned int streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
3061 if (!streams || streams == -1)
3062 streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
3063 if (!streams || streams == -1)
3064 return 0;
3065 return streams;
3068 static unsigned int query_stream_param(struct hda_codec *codec, hda_nid_t nid)
3070 return query_caps_hash(codec, nid, HDA_HASH_PARSTR_KEY(nid),
3071 get_stream_param);
3075 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
3076 * @codec: the HDA codec
3077 * @nid: NID to query
3078 * @ratesp: the pointer to store the detected rate bitflags
3079 * @formatsp: the pointer to store the detected formats
3080 * @bpsp: the pointer to store the detected format widths
3082 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
3083 * or @bsps argument is ignored.
3085 * Returns 0 if successful, otherwise a negative error code.
3087 static int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
3088 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
3090 unsigned int i, val, wcaps;
3092 wcaps = get_wcaps(codec, nid);
3093 val = query_pcm_param(codec, nid);
3095 if (ratesp) {
3096 u32 rates = 0;
3097 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
3098 if (val & (1 << i))
3099 rates |= rate_bits[i].alsa_bits;
3101 if (rates == 0) {
3102 snd_printk(KERN_ERR "hda_codec: rates == 0 "
3103 "(nid=0x%x, val=0x%x, ovrd=%i)\n",
3104 nid, val,
3105 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0);
3106 return -EIO;
3108 *ratesp = rates;
3111 if (formatsp || bpsp) {
3112 u64 formats = 0;
3113 unsigned int streams, bps;
3115 streams = query_stream_param(codec, nid);
3116 if (!streams)
3117 return -EIO;
3119 bps = 0;
3120 if (streams & AC_SUPFMT_PCM) {
3121 if (val & AC_SUPPCM_BITS_8) {
3122 formats |= SNDRV_PCM_FMTBIT_U8;
3123 bps = 8;
3125 if (val & AC_SUPPCM_BITS_16) {
3126 formats |= SNDRV_PCM_FMTBIT_S16_LE;
3127 bps = 16;
3129 if (wcaps & AC_WCAP_DIGITAL) {
3130 if (val & AC_SUPPCM_BITS_32)
3131 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
3132 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
3133 formats |= SNDRV_PCM_FMTBIT_S32_LE;
3134 if (val & AC_SUPPCM_BITS_24)
3135 bps = 24;
3136 else if (val & AC_SUPPCM_BITS_20)
3137 bps = 20;
3138 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
3139 AC_SUPPCM_BITS_32)) {
3140 formats |= SNDRV_PCM_FMTBIT_S32_LE;
3141 if (val & AC_SUPPCM_BITS_32)
3142 bps = 32;
3143 else if (val & AC_SUPPCM_BITS_24)
3144 bps = 24;
3145 else if (val & AC_SUPPCM_BITS_20)
3146 bps = 20;
3149 if (streams & AC_SUPFMT_FLOAT32) {
3150 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
3151 if (!bps)
3152 bps = 32;
3154 if (streams == AC_SUPFMT_AC3) {
3155 /* should be exclusive */
3156 /* temporary hack: we have still no proper support
3157 * for the direct AC3 stream...
3159 formats |= SNDRV_PCM_FMTBIT_U8;
3160 bps = 8;
3162 if (formats == 0) {
3163 snd_printk(KERN_ERR "hda_codec: formats == 0 "
3164 "(nid=0x%x, val=0x%x, ovrd=%i, "
3165 "streams=0x%x)\n",
3166 nid, val,
3167 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0,
3168 streams);
3169 return -EIO;
3171 if (formatsp)
3172 *formatsp = formats;
3173 if (bpsp)
3174 *bpsp = bps;
3177 return 0;
3181 * snd_hda_is_supported_format - Check the validity of the format
3182 * @codec: HD-audio codec
3183 * @nid: NID to check
3184 * @format: the HD-audio format value to check
3186 * Check whether the given node supports the format value.
3188 * Returns 1 if supported, 0 if not.
3190 int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
3191 unsigned int format)
3193 int i;
3194 unsigned int val = 0, rate, stream;
3196 val = query_pcm_param(codec, nid);
3197 if (!val)
3198 return 0;
3200 rate = format & 0xff00;
3201 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
3202 if (rate_bits[i].hda_fmt == rate) {
3203 if (val & (1 << i))
3204 break;
3205 return 0;
3207 if (i >= AC_PAR_PCM_RATE_BITS)
3208 return 0;
3210 stream = query_stream_param(codec, nid);
3211 if (!stream)
3212 return 0;
3214 if (stream & AC_SUPFMT_PCM) {
3215 switch (format & 0xf0) {
3216 case 0x00:
3217 if (!(val & AC_SUPPCM_BITS_8))
3218 return 0;
3219 break;
3220 case 0x10:
3221 if (!(val & AC_SUPPCM_BITS_16))
3222 return 0;
3223 break;
3224 case 0x20:
3225 if (!(val & AC_SUPPCM_BITS_20))
3226 return 0;
3227 break;
3228 case 0x30:
3229 if (!(val & AC_SUPPCM_BITS_24))
3230 return 0;
3231 break;
3232 case 0x40:
3233 if (!(val & AC_SUPPCM_BITS_32))
3234 return 0;
3235 break;
3236 default:
3237 return 0;
3239 } else {
3240 /* FIXME: check for float32 and AC3? */
3243 return 1;
3245 EXPORT_SYMBOL_HDA(snd_hda_is_supported_format);
3248 * PCM stuff
3250 static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
3251 struct hda_codec *codec,
3252 struct snd_pcm_substream *substream)
3254 return 0;
3257 static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
3258 struct hda_codec *codec,
3259 unsigned int stream_tag,
3260 unsigned int format,
3261 struct snd_pcm_substream *substream)
3263 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
3264 return 0;
3267 static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
3268 struct hda_codec *codec,
3269 struct snd_pcm_substream *substream)
3271 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
3272 return 0;
3275 static int set_pcm_default_values(struct hda_codec *codec,
3276 struct hda_pcm_stream *info)
3278 int err;
3280 /* query support PCM information from the given NID */
3281 if (info->nid && (!info->rates || !info->formats)) {
3282 err = snd_hda_query_supported_pcm(codec, info->nid,
3283 info->rates ? NULL : &info->rates,
3284 info->formats ? NULL : &info->formats,
3285 info->maxbps ? NULL : &info->maxbps);
3286 if (err < 0)
3287 return err;
3289 if (info->ops.open == NULL)
3290 info->ops.open = hda_pcm_default_open_close;
3291 if (info->ops.close == NULL)
3292 info->ops.close = hda_pcm_default_open_close;
3293 if (info->ops.prepare == NULL) {
3294 if (snd_BUG_ON(!info->nid))
3295 return -EINVAL;
3296 info->ops.prepare = hda_pcm_default_prepare;
3298 if (info->ops.cleanup == NULL) {
3299 if (snd_BUG_ON(!info->nid))
3300 return -EINVAL;
3301 info->ops.cleanup = hda_pcm_default_cleanup;
3303 return 0;
3306 /* global */
3307 const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = {
3308 "Audio", "SPDIF", "HDMI", "Modem"
3312 * get the empty PCM device number to assign
3314 * note the max device number is limited by HDA_MAX_PCMS, currently 10
3316 static int get_empty_pcm_device(struct hda_bus *bus, int type)
3318 /* audio device indices; not linear to keep compatibility */
3319 static int audio_idx[HDA_PCM_NTYPES][5] = {
3320 [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 },
3321 [HDA_PCM_TYPE_SPDIF] = { 1, -1 },
3322 [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 },
3323 [HDA_PCM_TYPE_MODEM] = { 6, -1 },
3325 int i;
3327 if (type >= HDA_PCM_NTYPES) {
3328 snd_printk(KERN_WARNING "Invalid PCM type %d\n", type);
3329 return -EINVAL;
3332 for (i = 0; audio_idx[type][i] >= 0 ; i++)
3333 if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits))
3334 return audio_idx[type][i];
3336 snd_printk(KERN_WARNING "Too many %s devices\n",
3337 snd_hda_pcm_type_name[type]);
3338 return -EAGAIN;
3342 * attach a new PCM stream
3344 static int snd_hda_attach_pcm(struct hda_codec *codec, struct hda_pcm *pcm)
3346 struct hda_bus *bus = codec->bus;
3347 struct hda_pcm_stream *info;
3348 int stream, err;
3350 if (snd_BUG_ON(!pcm->name))
3351 return -EINVAL;
3352 for (stream = 0; stream < 2; stream++) {
3353 info = &pcm->stream[stream];
3354 if (info->substreams) {
3355 err = set_pcm_default_values(codec, info);
3356 if (err < 0)
3357 return err;
3360 return bus->ops.attach_pcm(bus, codec, pcm);
3363 /* assign all PCMs of the given codec */
3364 int snd_hda_codec_build_pcms(struct hda_codec *codec)
3366 unsigned int pcm;
3367 int err;
3369 if (!codec->num_pcms) {
3370 if (!codec->patch_ops.build_pcms)
3371 return 0;
3372 err = codec->patch_ops.build_pcms(codec);
3373 if (err < 0) {
3374 printk(KERN_ERR "hda_codec: cannot build PCMs"
3375 "for #%d (error %d)\n", codec->addr, err);
3376 err = snd_hda_codec_reset(codec);
3377 if (err < 0) {
3378 printk(KERN_ERR
3379 "hda_codec: cannot revert codec\n");
3380 return err;
3384 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
3385 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
3386 int dev;
3388 if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
3389 continue; /* no substreams assigned */
3391 if (!cpcm->pcm) {
3392 dev = get_empty_pcm_device(codec->bus, cpcm->pcm_type);
3393 if (dev < 0)
3394 continue; /* no fatal error */
3395 cpcm->device = dev;
3396 err = snd_hda_attach_pcm(codec, cpcm);
3397 if (err < 0) {
3398 printk(KERN_ERR "hda_codec: cannot attach "
3399 "PCM stream %d for codec #%d\n",
3400 dev, codec->addr);
3401 continue; /* no fatal error */
3405 return 0;
3409 * snd_hda_build_pcms - build PCM information
3410 * @bus: the BUS
3412 * Create PCM information for each codec included in the bus.
3414 * The build_pcms codec patch is requested to set up codec->num_pcms and
3415 * codec->pcm_info properly. The array is referred by the top-level driver
3416 * to create its PCM instances.
3417 * The allocated codec->pcm_info should be released in codec->patch_ops.free
3418 * callback.
3420 * At least, substreams, channels_min and channels_max must be filled for
3421 * each stream. substreams = 0 indicates that the stream doesn't exist.
3422 * When rates and/or formats are zero, the supported values are queried
3423 * from the given nid. The nid is used also by the default ops.prepare
3424 * and ops.cleanup callbacks.
3426 * The driver needs to call ops.open in its open callback. Similarly,
3427 * ops.close is supposed to be called in the close callback.
3428 * ops.prepare should be called in the prepare or hw_params callback
3429 * with the proper parameters for set up.
3430 * ops.cleanup should be called in hw_free for clean up of streams.
3432 * This function returns 0 if successfull, or a negative error code.
3434 int __devinit snd_hda_build_pcms(struct hda_bus *bus)
3436 struct hda_codec *codec;
3438 list_for_each_entry(codec, &bus->codec_list, list) {
3439 int err = snd_hda_codec_build_pcms(codec);
3440 if (err < 0)
3441 return err;
3443 return 0;
3445 EXPORT_SYMBOL_HDA(snd_hda_build_pcms);
3448 * snd_hda_check_board_config - compare the current codec with the config table
3449 * @codec: the HDA codec
3450 * @num_configs: number of config enums
3451 * @models: array of model name strings
3452 * @tbl: configuration table, terminated by null entries
3454 * Compares the modelname or PCI subsystem id of the current codec with the
3455 * given configuration table. If a matching entry is found, returns its
3456 * config value (supposed to be 0 or positive).
3458 * If no entries are matching, the function returns a negative value.
3460 int snd_hda_check_board_config(struct hda_codec *codec,
3461 int num_configs, const char **models,
3462 const struct snd_pci_quirk *tbl)
3464 if (codec->modelname && models) {
3465 int i;
3466 for (i = 0; i < num_configs; i++) {
3467 if (models[i] &&
3468 !strcmp(codec->modelname, models[i])) {
3469 snd_printd(KERN_INFO "hda_codec: model '%s' is "
3470 "selected\n", models[i]);
3471 return i;
3476 if (!codec->bus->pci || !tbl)
3477 return -1;
3479 tbl = snd_pci_quirk_lookup(codec->bus->pci, tbl);
3480 if (!tbl)
3481 return -1;
3482 if (tbl->value >= 0 && tbl->value < num_configs) {
3483 #ifdef CONFIG_SND_DEBUG_VERBOSE
3484 char tmp[10];
3485 const char *model = NULL;
3486 if (models)
3487 model = models[tbl->value];
3488 if (!model) {
3489 sprintf(tmp, "#%d", tbl->value);
3490 model = tmp;
3492 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
3493 "for config %x:%x (%s)\n",
3494 model, tbl->subvendor, tbl->subdevice,
3495 (tbl->name ? tbl->name : "Unknown device"));
3496 #endif
3497 return tbl->value;
3499 return -1;
3501 EXPORT_SYMBOL_HDA(snd_hda_check_board_config);
3504 * snd_hda_check_board_codec_sid_config - compare the current codec
3505 subsystem ID with the
3506 config table
3508 This is important for Gateway notebooks with SB450 HDA Audio
3509 where the vendor ID of the PCI device is:
3510 ATI Technologies Inc SB450 HDA Audio [1002:437b]
3511 and the vendor/subvendor are found only at the codec.
3513 * @codec: the HDA codec
3514 * @num_configs: number of config enums
3515 * @models: array of model name strings
3516 * @tbl: configuration table, terminated by null entries
3518 * Compares the modelname or PCI subsystem id of the current codec with the
3519 * given configuration table. If a matching entry is found, returns its
3520 * config value (supposed to be 0 or positive).
3522 * If no entries are matching, the function returns a negative value.
3524 int snd_hda_check_board_codec_sid_config(struct hda_codec *codec,
3525 int num_configs, const char **models,
3526 const struct snd_pci_quirk *tbl)
3528 const struct snd_pci_quirk *q;
3530 /* Search for codec ID */
3531 for (q = tbl; q->subvendor; q++) {
3532 unsigned long vendorid = (q->subdevice) | (q->subvendor << 16);
3534 if (vendorid == codec->subsystem_id)
3535 break;
3538 if (!q->subvendor)
3539 return -1;
3541 tbl = q;
3543 if (tbl->value >= 0 && tbl->value < num_configs) {
3544 #ifdef CONFIG_SND_DEBUG_VERBOSE
3545 char tmp[10];
3546 const char *model = NULL;
3547 if (models)
3548 model = models[tbl->value];
3549 if (!model) {
3550 sprintf(tmp, "#%d", tbl->value);
3551 model = tmp;
3553 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
3554 "for config %x:%x (%s)\n",
3555 model, tbl->subvendor, tbl->subdevice,
3556 (tbl->name ? tbl->name : "Unknown device"));
3557 #endif
3558 return tbl->value;
3560 return -1;
3562 EXPORT_SYMBOL_HDA(snd_hda_check_board_codec_sid_config);
3565 * snd_hda_add_new_ctls - create controls from the array
3566 * @codec: the HDA codec
3567 * @knew: the array of struct snd_kcontrol_new
3569 * This helper function creates and add new controls in the given array.
3570 * The array must be terminated with an empty entry as terminator.
3572 * Returns 0 if successful, or a negative error code.
3574 int snd_hda_add_new_ctls(struct hda_codec *codec, struct snd_kcontrol_new *knew)
3576 int err;
3578 for (; knew->name; knew++) {
3579 struct snd_kcontrol *kctl;
3580 if (knew->iface == -1) /* skip this codec private value */
3581 continue;
3582 kctl = snd_ctl_new1(knew, codec);
3583 if (!kctl)
3584 return -ENOMEM;
3585 err = snd_hda_ctl_add(codec, 0, kctl);
3586 if (err < 0) {
3587 if (!codec->addr)
3588 return err;
3589 kctl = snd_ctl_new1(knew, codec);
3590 if (!kctl)
3591 return -ENOMEM;
3592 kctl->id.device = codec->addr;
3593 err = snd_hda_ctl_add(codec, 0, kctl);
3594 if (err < 0)
3595 return err;
3598 return 0;
3600 EXPORT_SYMBOL_HDA(snd_hda_add_new_ctls);
3602 #ifdef CONFIG_SND_HDA_POWER_SAVE
3603 static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
3604 unsigned int power_state);
3606 static void hda_power_work(struct work_struct *work)
3608 struct hda_codec *codec =
3609 container_of(work, struct hda_codec, power_work.work);
3610 struct hda_bus *bus = codec->bus;
3612 if (!codec->power_on || codec->power_count) {
3613 codec->power_transition = 0;
3614 return;
3617 hda_call_codec_suspend(codec);
3618 if (bus->ops.pm_notify)
3619 bus->ops.pm_notify(bus);
3622 static void hda_keep_power_on(struct hda_codec *codec)
3624 codec->power_count++;
3625 codec->power_on = 1;
3626 codec->power_jiffies = jiffies;
3629 /* update the power on/off account with the current jiffies */
3630 void snd_hda_update_power_acct(struct hda_codec *codec)
3632 unsigned long delta = jiffies - codec->power_jiffies;
3633 if (codec->power_on)
3634 codec->power_on_acct += delta;
3635 else
3636 codec->power_off_acct += delta;
3637 codec->power_jiffies += delta;
3641 * snd_hda_power_up - Power-up the codec
3642 * @codec: HD-audio codec
3644 * Increment the power-up counter and power up the hardware really when
3645 * not turned on yet.
3647 void snd_hda_power_up(struct hda_codec *codec)
3649 struct hda_bus *bus = codec->bus;
3651 codec->power_count++;
3652 if (codec->power_on || codec->power_transition)
3653 return;
3655 snd_hda_update_power_acct(codec);
3656 codec->power_on = 1;
3657 codec->power_jiffies = jiffies;
3658 if (bus->ops.pm_notify)
3659 bus->ops.pm_notify(bus);
3660 hda_call_codec_resume(codec);
3661 cancel_delayed_work(&codec->power_work);
3662 codec->power_transition = 0;
3664 EXPORT_SYMBOL_HDA(snd_hda_power_up);
3666 #define power_save(codec) \
3667 ((codec)->bus->power_save ? *(codec)->bus->power_save : 0)
3670 * snd_hda_power_down - Power-down the codec
3671 * @codec: HD-audio codec
3673 * Decrement the power-up counter and schedules the power-off work if
3674 * the counter rearches to zero.
3676 void snd_hda_power_down(struct hda_codec *codec)
3678 --codec->power_count;
3679 if (!codec->power_on || codec->power_count || codec->power_transition)
3680 return;
3681 if (power_save(codec)) {
3682 codec->power_transition = 1; /* avoid reentrance */
3683 queue_delayed_work(codec->bus->workq, &codec->power_work,
3684 msecs_to_jiffies(power_save(codec) * 1000));
3687 EXPORT_SYMBOL_HDA(snd_hda_power_down);
3690 * snd_hda_check_amp_list_power - Check the amp list and update the power
3691 * @codec: HD-audio codec
3692 * @check: the object containing an AMP list and the status
3693 * @nid: NID to check / update
3695 * Check whether the given NID is in the amp list. If it's in the list,
3696 * check the current AMP status, and update the the power-status according
3697 * to the mute status.
3699 * This function is supposed to be set or called from the check_power_status
3700 * patch ops.
3702 int snd_hda_check_amp_list_power(struct hda_codec *codec,
3703 struct hda_loopback_check *check,
3704 hda_nid_t nid)
3706 struct hda_amp_list *p;
3707 int ch, v;
3709 if (!check->amplist)
3710 return 0;
3711 for (p = check->amplist; p->nid; p++) {
3712 if (p->nid == nid)
3713 break;
3715 if (!p->nid)
3716 return 0; /* nothing changed */
3718 for (p = check->amplist; p->nid; p++) {
3719 for (ch = 0; ch < 2; ch++) {
3720 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
3721 p->idx);
3722 if (!(v & HDA_AMP_MUTE) && v > 0) {
3723 if (!check->power_on) {
3724 check->power_on = 1;
3725 snd_hda_power_up(codec);
3727 return 1;
3731 if (check->power_on) {
3732 check->power_on = 0;
3733 snd_hda_power_down(codec);
3735 return 0;
3737 EXPORT_SYMBOL_HDA(snd_hda_check_amp_list_power);
3738 #endif
3741 * Channel mode helper
3745 * snd_hda_ch_mode_info - Info callback helper for the channel mode enum
3747 int snd_hda_ch_mode_info(struct hda_codec *codec,
3748 struct snd_ctl_elem_info *uinfo,
3749 const struct hda_channel_mode *chmode,
3750 int num_chmodes)
3752 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
3753 uinfo->count = 1;
3754 uinfo->value.enumerated.items = num_chmodes;
3755 if (uinfo->value.enumerated.item >= num_chmodes)
3756 uinfo->value.enumerated.item = num_chmodes - 1;
3757 sprintf(uinfo->value.enumerated.name, "%dch",
3758 chmode[uinfo->value.enumerated.item].channels);
3759 return 0;
3761 EXPORT_SYMBOL_HDA(snd_hda_ch_mode_info);
3764 * snd_hda_ch_mode_get - Get callback helper for the channel mode enum
3766 int snd_hda_ch_mode_get(struct hda_codec *codec,
3767 struct snd_ctl_elem_value *ucontrol,
3768 const struct hda_channel_mode *chmode,
3769 int num_chmodes,
3770 int max_channels)
3772 int i;
3774 for (i = 0; i < num_chmodes; i++) {
3775 if (max_channels == chmode[i].channels) {
3776 ucontrol->value.enumerated.item[0] = i;
3777 break;
3780 return 0;
3782 EXPORT_SYMBOL_HDA(snd_hda_ch_mode_get);
3785 * snd_hda_ch_mode_put - Put callback helper for the channel mode enum
3787 int snd_hda_ch_mode_put(struct hda_codec *codec,
3788 struct snd_ctl_elem_value *ucontrol,
3789 const struct hda_channel_mode *chmode,
3790 int num_chmodes,
3791 int *max_channelsp)
3793 unsigned int mode;
3795 mode = ucontrol->value.enumerated.item[0];
3796 if (mode >= num_chmodes)
3797 return -EINVAL;
3798 if (*max_channelsp == chmode[mode].channels)
3799 return 0;
3800 /* change the current channel setting */
3801 *max_channelsp = chmode[mode].channels;
3802 if (chmode[mode].sequence)
3803 snd_hda_sequence_write_cache(codec, chmode[mode].sequence);
3804 return 1;
3806 EXPORT_SYMBOL_HDA(snd_hda_ch_mode_put);
3809 * input MUX helper
3813 * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum
3815 int snd_hda_input_mux_info(const struct hda_input_mux *imux,
3816 struct snd_ctl_elem_info *uinfo)
3818 unsigned int index;
3820 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
3821 uinfo->count = 1;
3822 uinfo->value.enumerated.items = imux->num_items;
3823 if (!imux->num_items)
3824 return 0;
3825 index = uinfo->value.enumerated.item;
3826 if (index >= imux->num_items)
3827 index = imux->num_items - 1;
3828 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
3829 return 0;
3831 EXPORT_SYMBOL_HDA(snd_hda_input_mux_info);
3834 * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum
3836 int snd_hda_input_mux_put(struct hda_codec *codec,
3837 const struct hda_input_mux *imux,
3838 struct snd_ctl_elem_value *ucontrol,
3839 hda_nid_t nid,
3840 unsigned int *cur_val)
3842 unsigned int idx;
3844 if (!imux->num_items)
3845 return 0;
3846 idx = ucontrol->value.enumerated.item[0];
3847 if (idx >= imux->num_items)
3848 idx = imux->num_items - 1;
3849 if (*cur_val == idx)
3850 return 0;
3851 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
3852 imux->items[idx].index);
3853 *cur_val = idx;
3854 return 1;
3856 EXPORT_SYMBOL_HDA(snd_hda_input_mux_put);
3860 * Multi-channel / digital-out PCM helper functions
3863 /* setup SPDIF output stream */
3864 static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
3865 unsigned int stream_tag, unsigned int format)
3867 /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */
3868 if (codec->spdif_status_reset && (codec->spdif_ctls & AC_DIG1_ENABLE))
3869 set_dig_out_convert(codec, nid,
3870 codec->spdif_ctls & ~AC_DIG1_ENABLE & 0xff,
3871 -1);
3872 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
3873 if (codec->slave_dig_outs) {
3874 hda_nid_t *d;
3875 for (d = codec->slave_dig_outs; *d; d++)
3876 snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
3877 format);
3879 /* turn on again (if needed) */
3880 if (codec->spdif_status_reset && (codec->spdif_ctls & AC_DIG1_ENABLE))
3881 set_dig_out_convert(codec, nid,
3882 codec->spdif_ctls & 0xff, -1);
3885 static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
3887 snd_hda_codec_cleanup_stream(codec, nid);
3888 if (codec->slave_dig_outs) {
3889 hda_nid_t *d;
3890 for (d = codec->slave_dig_outs; *d; d++)
3891 snd_hda_codec_cleanup_stream(codec, *d);
3896 * snd_hda_bus_reboot_notify - call the reboot notifier of each codec
3897 * @bus: HD-audio bus
3899 void snd_hda_bus_reboot_notify(struct hda_bus *bus)
3901 struct hda_codec *codec;
3903 if (!bus)
3904 return;
3905 list_for_each_entry(codec, &bus->codec_list, list) {
3906 #ifdef CONFIG_SND_HDA_POWER_SAVE
3907 if (!codec->power_on)
3908 continue;
3909 #endif
3910 if (codec->patch_ops.reboot_notify)
3911 codec->patch_ops.reboot_notify(codec);
3914 EXPORT_SYMBOL_HDA(snd_hda_bus_reboot_notify);
3917 * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode
3919 int snd_hda_multi_out_dig_open(struct hda_codec *codec,
3920 struct hda_multi_out *mout)
3922 mutex_lock(&codec->spdif_mutex);
3923 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
3924 /* already opened as analog dup; reset it once */
3925 cleanup_dig_out_stream(codec, mout->dig_out_nid);
3926 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
3927 mutex_unlock(&codec->spdif_mutex);
3928 return 0;
3930 EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_open);
3933 * snd_hda_multi_out_dig_prepare - prepare the digital out stream
3935 int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
3936 struct hda_multi_out *mout,
3937 unsigned int stream_tag,
3938 unsigned int format,
3939 struct snd_pcm_substream *substream)
3941 mutex_lock(&codec->spdif_mutex);
3942 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
3943 mutex_unlock(&codec->spdif_mutex);
3944 return 0;
3946 EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_prepare);
3949 * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream
3951 int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec,
3952 struct hda_multi_out *mout)
3954 mutex_lock(&codec->spdif_mutex);
3955 cleanup_dig_out_stream(codec, mout->dig_out_nid);
3956 mutex_unlock(&codec->spdif_mutex);
3957 return 0;
3959 EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_cleanup);
3962 * snd_hda_multi_out_dig_close - release the digital out stream
3964 int snd_hda_multi_out_dig_close(struct hda_codec *codec,
3965 struct hda_multi_out *mout)
3967 mutex_lock(&codec->spdif_mutex);
3968 mout->dig_out_used = 0;
3969 mutex_unlock(&codec->spdif_mutex);
3970 return 0;
3972 EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_close);
3975 * snd_hda_multi_out_analog_open - open analog outputs
3977 * Open analog outputs and set up the hw-constraints.
3978 * If the digital outputs can be opened as slave, open the digital
3979 * outputs, too.
3981 int snd_hda_multi_out_analog_open(struct hda_codec *codec,
3982 struct hda_multi_out *mout,
3983 struct snd_pcm_substream *substream,
3984 struct hda_pcm_stream *hinfo)
3986 struct snd_pcm_runtime *runtime = substream->runtime;
3987 runtime->hw.channels_max = mout->max_channels;
3988 if (mout->dig_out_nid) {
3989 if (!mout->analog_rates) {
3990 mout->analog_rates = hinfo->rates;
3991 mout->analog_formats = hinfo->formats;
3992 mout->analog_maxbps = hinfo->maxbps;
3993 } else {
3994 runtime->hw.rates = mout->analog_rates;
3995 runtime->hw.formats = mout->analog_formats;
3996 hinfo->maxbps = mout->analog_maxbps;
3998 if (!mout->spdif_rates) {
3999 snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
4000 &mout->spdif_rates,
4001 &mout->spdif_formats,
4002 &mout->spdif_maxbps);
4004 mutex_lock(&codec->spdif_mutex);
4005 if (mout->share_spdif) {
4006 if ((runtime->hw.rates & mout->spdif_rates) &&
4007 (runtime->hw.formats & mout->spdif_formats)) {
4008 runtime->hw.rates &= mout->spdif_rates;
4009 runtime->hw.formats &= mout->spdif_formats;
4010 if (mout->spdif_maxbps < hinfo->maxbps)
4011 hinfo->maxbps = mout->spdif_maxbps;
4012 } else {
4013 mout->share_spdif = 0;
4014 /* FIXME: need notify? */
4017 mutex_unlock(&codec->spdif_mutex);
4019 return snd_pcm_hw_constraint_step(substream->runtime, 0,
4020 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
4022 EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_open);
4025 * snd_hda_multi_out_analog_prepare - Preapre the analog outputs.
4027 * Set up the i/o for analog out.
4028 * When the digital out is available, copy the front out to digital out, too.
4030 int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
4031 struct hda_multi_out *mout,
4032 unsigned int stream_tag,
4033 unsigned int format,
4034 struct snd_pcm_substream *substream)
4036 hda_nid_t *nids = mout->dac_nids;
4037 int chs = substream->runtime->channels;
4038 int i;
4040 mutex_lock(&codec->spdif_mutex);
4041 if (mout->dig_out_nid && mout->share_spdif &&
4042 mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
4043 if (chs == 2 &&
4044 snd_hda_is_supported_format(codec, mout->dig_out_nid,
4045 format) &&
4046 !(codec->spdif_status & IEC958_AES0_NONAUDIO)) {
4047 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
4048 setup_dig_out_stream(codec, mout->dig_out_nid,
4049 stream_tag, format);
4050 } else {
4051 mout->dig_out_used = 0;
4052 cleanup_dig_out_stream(codec, mout->dig_out_nid);
4055 mutex_unlock(&codec->spdif_mutex);
4057 /* front */
4058 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
4059 0, format);
4060 if (!mout->no_share_stream &&
4061 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
4062 /* headphone out will just decode front left/right (stereo) */
4063 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
4064 0, format);
4065 /* extra outputs copied from front */
4066 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
4067 if (!mout->no_share_stream && mout->extra_out_nid[i])
4068 snd_hda_codec_setup_stream(codec,
4069 mout->extra_out_nid[i],
4070 stream_tag, 0, format);
4072 /* surrounds */
4073 for (i = 1; i < mout->num_dacs; i++) {
4074 if (chs >= (i + 1) * 2) /* independent out */
4075 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
4076 i * 2, format);
4077 else if (!mout->no_share_stream) /* copy front */
4078 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
4079 0, format);
4081 return 0;
4083 EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_prepare);
4086 * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out
4088 int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
4089 struct hda_multi_out *mout)
4091 hda_nid_t *nids = mout->dac_nids;
4092 int i;
4094 for (i = 0; i < mout->num_dacs; i++)
4095 snd_hda_codec_cleanup_stream(codec, nids[i]);
4096 if (mout->hp_nid)
4097 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
4098 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
4099 if (mout->extra_out_nid[i])
4100 snd_hda_codec_cleanup_stream(codec,
4101 mout->extra_out_nid[i]);
4102 mutex_lock(&codec->spdif_mutex);
4103 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
4104 cleanup_dig_out_stream(codec, mout->dig_out_nid);
4105 mout->dig_out_used = 0;
4107 mutex_unlock(&codec->spdif_mutex);
4108 return 0;
4110 EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_cleanup);
4113 * Helper for automatic pin configuration
4116 static int is_in_nid_list(hda_nid_t nid, hda_nid_t *list)
4118 for (; *list; list++)
4119 if (*list == nid)
4120 return 1;
4121 return 0;
4126 * Sort an associated group of pins according to their sequence numbers.
4128 static void sort_pins_by_sequence(hda_nid_t *pins, short *sequences,
4129 int num_pins)
4131 int i, j;
4132 short seq;
4133 hda_nid_t nid;
4135 for (i = 0; i < num_pins; i++) {
4136 for (j = i + 1; j < num_pins; j++) {
4137 if (sequences[i] > sequences[j]) {
4138 seq = sequences[i];
4139 sequences[i] = sequences[j];
4140 sequences[j] = seq;
4141 nid = pins[i];
4142 pins[i] = pins[j];
4143 pins[j] = nid;
4151 * Parse all pin widgets and store the useful pin nids to cfg
4153 * The number of line-outs or any primary output is stored in line_outs,
4154 * and the corresponding output pins are assigned to line_out_pins[],
4155 * in the order of front, rear, CLFE, side, ...
4157 * If more extra outputs (speaker and headphone) are found, the pins are
4158 * assisnged to hp_pins[] and speaker_pins[], respectively. If no line-out jack
4159 * is detected, one of speaker of HP pins is assigned as the primary
4160 * output, i.e. to line_out_pins[0]. So, line_outs is always positive
4161 * if any analog output exists.
4163 * The analog input pins are assigned to input_pins array.
4164 * The digital input/output pins are assigned to dig_in_pin and dig_out_pin,
4165 * respectively.
4167 int snd_hda_parse_pin_def_config(struct hda_codec *codec,
4168 struct auto_pin_cfg *cfg,
4169 hda_nid_t *ignore_nids)
4171 hda_nid_t nid, end_nid;
4172 short seq, assoc_line_out, assoc_speaker;
4173 short sequences_line_out[ARRAY_SIZE(cfg->line_out_pins)];
4174 short sequences_speaker[ARRAY_SIZE(cfg->speaker_pins)];
4175 short sequences_hp[ARRAY_SIZE(cfg->hp_pins)];
4177 memset(cfg, 0, sizeof(*cfg));
4179 memset(sequences_line_out, 0, sizeof(sequences_line_out));
4180 memset(sequences_speaker, 0, sizeof(sequences_speaker));
4181 memset(sequences_hp, 0, sizeof(sequences_hp));
4182 assoc_line_out = assoc_speaker = 0;
4184 end_nid = codec->start_nid + codec->num_nodes;
4185 for (nid = codec->start_nid; nid < end_nid; nid++) {
4186 unsigned int wid_caps = get_wcaps(codec, nid);
4187 unsigned int wid_type = get_wcaps_type(wid_caps);
4188 unsigned int def_conf;
4189 short assoc, loc;
4191 /* read all default configuration for pin complex */
4192 if (wid_type != AC_WID_PIN)
4193 continue;
4194 /* ignore the given nids (e.g. pc-beep returns error) */
4195 if (ignore_nids && is_in_nid_list(nid, ignore_nids))
4196 continue;
4198 def_conf = snd_hda_codec_get_pincfg(codec, nid);
4199 if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE)
4200 continue;
4201 loc = get_defcfg_location(def_conf);
4202 switch (get_defcfg_device(def_conf)) {
4203 case AC_JACK_LINE_OUT:
4204 seq = get_defcfg_sequence(def_conf);
4205 assoc = get_defcfg_association(def_conf);
4207 if (!(wid_caps & AC_WCAP_STEREO))
4208 if (!cfg->mono_out_pin)
4209 cfg->mono_out_pin = nid;
4210 if (!assoc)
4211 continue;
4212 if (!assoc_line_out)
4213 assoc_line_out = assoc;
4214 else if (assoc_line_out != assoc)
4215 continue;
4216 if (cfg->line_outs >= ARRAY_SIZE(cfg->line_out_pins))
4217 continue;
4218 cfg->line_out_pins[cfg->line_outs] = nid;
4219 sequences_line_out[cfg->line_outs] = seq;
4220 cfg->line_outs++;
4221 break;
4222 case AC_JACK_SPEAKER:
4223 seq = get_defcfg_sequence(def_conf);
4224 assoc = get_defcfg_association(def_conf);
4225 if (!assoc)
4226 continue;
4227 if (!assoc_speaker)
4228 assoc_speaker = assoc;
4229 else if (assoc_speaker != assoc)
4230 continue;
4231 if (cfg->speaker_outs >= ARRAY_SIZE(cfg->speaker_pins))
4232 continue;
4233 cfg->speaker_pins[cfg->speaker_outs] = nid;
4234 sequences_speaker[cfg->speaker_outs] = seq;
4235 cfg->speaker_outs++;
4236 break;
4237 case AC_JACK_HP_OUT:
4238 seq = get_defcfg_sequence(def_conf);
4239 assoc = get_defcfg_association(def_conf);
4240 if (cfg->hp_outs >= ARRAY_SIZE(cfg->hp_pins))
4241 continue;
4242 cfg->hp_pins[cfg->hp_outs] = nid;
4243 sequences_hp[cfg->hp_outs] = (assoc << 4) | seq;
4244 cfg->hp_outs++;
4245 break;
4246 case AC_JACK_MIC_IN: {
4247 int preferred, alt;
4248 if (loc == AC_JACK_LOC_FRONT) {
4249 preferred = AUTO_PIN_FRONT_MIC;
4250 alt = AUTO_PIN_MIC;
4251 } else {
4252 preferred = AUTO_PIN_MIC;
4253 alt = AUTO_PIN_FRONT_MIC;
4255 if (!cfg->input_pins[preferred])
4256 cfg->input_pins[preferred] = nid;
4257 else if (!cfg->input_pins[alt])
4258 cfg->input_pins[alt] = nid;
4259 break;
4261 case AC_JACK_LINE_IN:
4262 if (loc == AC_JACK_LOC_FRONT)
4263 cfg->input_pins[AUTO_PIN_FRONT_LINE] = nid;
4264 else
4265 cfg->input_pins[AUTO_PIN_LINE] = nid;
4266 break;
4267 case AC_JACK_CD:
4268 cfg->input_pins[AUTO_PIN_CD] = nid;
4269 break;
4270 case AC_JACK_AUX:
4271 cfg->input_pins[AUTO_PIN_AUX] = nid;
4272 break;
4273 case AC_JACK_SPDIF_OUT:
4274 case AC_JACK_DIG_OTHER_OUT:
4275 if (cfg->dig_outs >= ARRAY_SIZE(cfg->dig_out_pins))
4276 continue;
4277 cfg->dig_out_pins[cfg->dig_outs] = nid;
4278 cfg->dig_out_type[cfg->dig_outs] =
4279 (loc == AC_JACK_LOC_HDMI) ?
4280 HDA_PCM_TYPE_HDMI : HDA_PCM_TYPE_SPDIF;
4281 cfg->dig_outs++;
4282 break;
4283 case AC_JACK_SPDIF_IN:
4284 case AC_JACK_DIG_OTHER_IN:
4285 cfg->dig_in_pin = nid;
4286 if (loc == AC_JACK_LOC_HDMI)
4287 cfg->dig_in_type = HDA_PCM_TYPE_HDMI;
4288 else
4289 cfg->dig_in_type = HDA_PCM_TYPE_SPDIF;
4290 break;
4294 /* FIX-UP:
4295 * If no line-out is defined but multiple HPs are found,
4296 * some of them might be the real line-outs.
4298 if (!cfg->line_outs && cfg->hp_outs > 1) {
4299 int i = 0;
4300 while (i < cfg->hp_outs) {
4301 /* The real HPs should have the sequence 0x0f */
4302 if ((sequences_hp[i] & 0x0f) == 0x0f) {
4303 i++;
4304 continue;
4306 /* Move it to the line-out table */
4307 cfg->line_out_pins[cfg->line_outs] = cfg->hp_pins[i];
4308 sequences_line_out[cfg->line_outs] = sequences_hp[i];
4309 cfg->line_outs++;
4310 cfg->hp_outs--;
4311 memmove(cfg->hp_pins + i, cfg->hp_pins + i + 1,
4312 sizeof(cfg->hp_pins[0]) * (cfg->hp_outs - i));
4313 memmove(sequences_hp + i - 1, sequences_hp + i,
4314 sizeof(sequences_hp[0]) * (cfg->hp_outs - i));
4318 /* sort by sequence */
4319 sort_pins_by_sequence(cfg->line_out_pins, sequences_line_out,
4320 cfg->line_outs);
4321 sort_pins_by_sequence(cfg->speaker_pins, sequences_speaker,
4322 cfg->speaker_outs);
4323 sort_pins_by_sequence(cfg->hp_pins, sequences_hp,
4324 cfg->hp_outs);
4326 /* if we have only one mic, make it AUTO_PIN_MIC */
4327 if (!cfg->input_pins[AUTO_PIN_MIC] &&
4328 cfg->input_pins[AUTO_PIN_FRONT_MIC]) {
4329 cfg->input_pins[AUTO_PIN_MIC] =
4330 cfg->input_pins[AUTO_PIN_FRONT_MIC];
4331 cfg->input_pins[AUTO_PIN_FRONT_MIC] = 0;
4333 /* ditto for line-in */
4334 if (!cfg->input_pins[AUTO_PIN_LINE] &&
4335 cfg->input_pins[AUTO_PIN_FRONT_LINE]) {
4336 cfg->input_pins[AUTO_PIN_LINE] =
4337 cfg->input_pins[AUTO_PIN_FRONT_LINE];
4338 cfg->input_pins[AUTO_PIN_FRONT_LINE] = 0;
4342 * FIX-UP: if no line-outs are detected, try to use speaker or HP pin
4343 * as a primary output
4345 if (!cfg->line_outs) {
4346 if (cfg->speaker_outs) {
4347 cfg->line_outs = cfg->speaker_outs;
4348 memcpy(cfg->line_out_pins, cfg->speaker_pins,
4349 sizeof(cfg->speaker_pins));
4350 cfg->speaker_outs = 0;
4351 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
4352 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
4353 } else if (cfg->hp_outs) {
4354 cfg->line_outs = cfg->hp_outs;
4355 memcpy(cfg->line_out_pins, cfg->hp_pins,
4356 sizeof(cfg->hp_pins));
4357 cfg->hp_outs = 0;
4358 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
4359 cfg->line_out_type = AUTO_PIN_HP_OUT;
4363 /* Reorder the surround channels
4364 * ALSA sequence is front/surr/clfe/side
4365 * HDA sequence is:
4366 * 4-ch: front/surr => OK as it is
4367 * 6-ch: front/clfe/surr
4368 * 8-ch: front/clfe/rear/side|fc
4370 switch (cfg->line_outs) {
4371 case 3:
4372 case 4:
4373 nid = cfg->line_out_pins[1];
4374 cfg->line_out_pins[1] = cfg->line_out_pins[2];
4375 cfg->line_out_pins[2] = nid;
4376 break;
4380 * debug prints of the parsed results
4382 snd_printd("autoconfig: line_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
4383 cfg->line_outs, cfg->line_out_pins[0], cfg->line_out_pins[1],
4384 cfg->line_out_pins[2], cfg->line_out_pins[3],
4385 cfg->line_out_pins[4]);
4386 snd_printd(" speaker_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
4387 cfg->speaker_outs, cfg->speaker_pins[0],
4388 cfg->speaker_pins[1], cfg->speaker_pins[2],
4389 cfg->speaker_pins[3], cfg->speaker_pins[4]);
4390 snd_printd(" hp_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
4391 cfg->hp_outs, cfg->hp_pins[0],
4392 cfg->hp_pins[1], cfg->hp_pins[2],
4393 cfg->hp_pins[3], cfg->hp_pins[4]);
4394 snd_printd(" mono: mono_out=0x%x\n", cfg->mono_out_pin);
4395 if (cfg->dig_outs)
4396 snd_printd(" dig-out=0x%x/0x%x\n",
4397 cfg->dig_out_pins[0], cfg->dig_out_pins[1]);
4398 snd_printd(" inputs: mic=0x%x, fmic=0x%x, line=0x%x, fline=0x%x,"
4399 " cd=0x%x, aux=0x%x\n",
4400 cfg->input_pins[AUTO_PIN_MIC],
4401 cfg->input_pins[AUTO_PIN_FRONT_MIC],
4402 cfg->input_pins[AUTO_PIN_LINE],
4403 cfg->input_pins[AUTO_PIN_FRONT_LINE],
4404 cfg->input_pins[AUTO_PIN_CD],
4405 cfg->input_pins[AUTO_PIN_AUX]);
4406 if (cfg->dig_in_pin)
4407 snd_printd(" dig-in=0x%x\n", cfg->dig_in_pin);
4409 return 0;
4411 EXPORT_SYMBOL_HDA(snd_hda_parse_pin_def_config);
4413 /* labels for input pins */
4414 const char *auto_pin_cfg_labels[AUTO_PIN_LAST] = {
4415 "Mic", "Front Mic", "Line", "Front Line", "CD", "Aux"
4417 EXPORT_SYMBOL_HDA(auto_pin_cfg_labels);
4420 #ifdef CONFIG_PM
4422 * power management
4426 * snd_hda_suspend - suspend the codecs
4427 * @bus: the HDA bus
4429 * Returns 0 if successful.
4431 int snd_hda_suspend(struct hda_bus *bus)
4433 struct hda_codec *codec;
4435 list_for_each_entry(codec, &bus->codec_list, list) {
4436 #ifdef CONFIG_SND_HDA_POWER_SAVE
4437 if (!codec->power_on)
4438 continue;
4439 #endif
4440 hda_call_codec_suspend(codec);
4442 return 0;
4444 EXPORT_SYMBOL_HDA(snd_hda_suspend);
4447 * snd_hda_resume - resume the codecs
4448 * @bus: the HDA bus
4450 * Returns 0 if successful.
4452 * This fucntion is defined only when POWER_SAVE isn't set.
4453 * In the power-save mode, the codec is resumed dynamically.
4455 int snd_hda_resume(struct hda_bus *bus)
4457 struct hda_codec *codec;
4459 list_for_each_entry(codec, &bus->codec_list, list) {
4460 if (snd_hda_codec_needs_resume(codec))
4461 hda_call_codec_resume(codec);
4463 return 0;
4465 EXPORT_SYMBOL_HDA(snd_hda_resume);
4466 #endif /* CONFIG_PM */
4469 * generic arrays
4473 * snd_array_new - get a new element from the given array
4474 * @array: the array object
4476 * Get a new element from the given array. If it exceeds the
4477 * pre-allocated array size, re-allocate the array.
4479 * Returns NULL if allocation failed.
4481 void *snd_array_new(struct snd_array *array)
4483 if (array->used >= array->alloced) {
4484 int num = array->alloced + array->alloc_align;
4485 void *nlist;
4486 if (snd_BUG_ON(num >= 4096))
4487 return NULL;
4488 nlist = kcalloc(num + 1, array->elem_size, GFP_KERNEL);
4489 if (!nlist)
4490 return NULL;
4491 if (array->list) {
4492 memcpy(nlist, array->list,
4493 array->elem_size * array->alloced);
4494 kfree(array->list);
4496 array->list = nlist;
4497 array->alloced = num;
4499 return snd_array_elem(array, array->used++);
4501 EXPORT_SYMBOL_HDA(snd_array_new);
4504 * snd_array_free - free the given array elements
4505 * @array: the array object
4507 void snd_array_free(struct snd_array *array)
4509 kfree(array->list);
4510 array->used = 0;
4511 array->alloced = 0;
4512 array->list = NULL;
4514 EXPORT_SYMBOL_HDA(snd_array_free);
4517 * snd_print_pcm_rates - Print the supported PCM rates to the string buffer
4518 * @pcm: PCM caps bits
4519 * @buf: the string buffer to write
4520 * @buflen: the max buffer length
4522 * used by hda_proc.c and hda_eld.c
4524 void snd_print_pcm_rates(int pcm, char *buf, int buflen)
4526 static unsigned int rates[] = {
4527 8000, 11025, 16000, 22050, 32000, 44100, 48000, 88200,
4528 96000, 176400, 192000, 384000
4530 int i, j;
4532 for (i = 0, j = 0; i < ARRAY_SIZE(rates); i++)
4533 if (pcm & (1 << i))
4534 j += snprintf(buf + j, buflen - j, " %d", rates[i]);
4536 buf[j] = '\0'; /* necessary when j == 0 */
4538 EXPORT_SYMBOL_HDA(snd_print_pcm_rates);
4541 * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer
4542 * @pcm: PCM caps bits
4543 * @buf: the string buffer to write
4544 * @buflen: the max buffer length
4546 * used by hda_proc.c and hda_eld.c
4548 void snd_print_pcm_bits(int pcm, char *buf, int buflen)
4550 static unsigned int bits[] = { 8, 16, 20, 24, 32 };
4551 int i, j;
4553 for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
4554 if (pcm & (AC_SUPPCM_BITS_8 << i))
4555 j += snprintf(buf + j, buflen - j, " %d", bits[i]);
4557 buf[j] = '\0'; /* necessary when j == 0 */
4559 EXPORT_SYMBOL_HDA(snd_print_pcm_bits);
4561 MODULE_DESCRIPTION("HDA codec core");
4562 MODULE_LICENSE("GPL");