Merge branch 'hwmon-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/groec...
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / sound / pci / hda / hda_codec.c
blob644e3f14f8ca5aea7af2e465450caab86d2f90cb
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 "hda_codec: "
400 "Too many connections %d for NID 0x%x\n",
401 conns, nid);
402 return -EINVAL;
404 conn_list[conns++] = n;
406 } else {
407 if (conns >= max_conns) {
408 snd_printk(KERN_ERR "hda_codec: "
409 "Too many connections %d for NID 0x%x\n",
410 conns, nid);
411 return -EINVAL;
413 conn_list[conns++] = val;
415 prev_nid = val;
417 return conns;
419 EXPORT_SYMBOL_HDA(snd_hda_get_connections);
423 * snd_hda_queue_unsol_event - add an unsolicited event to queue
424 * @bus: the BUS
425 * @res: unsolicited event (lower 32bit of RIRB entry)
426 * @res_ex: codec addr and flags (upper 32bit or RIRB entry)
428 * Adds the given event to the queue. The events are processed in
429 * the workqueue asynchronously. Call this function in the interrupt
430 * hanlder when RIRB receives an unsolicited event.
432 * Returns 0 if successful, or a negative error code.
434 int snd_hda_queue_unsol_event(struct hda_bus *bus, u32 res, u32 res_ex)
436 struct hda_bus_unsolicited *unsol;
437 unsigned int wp;
439 unsol = bus->unsol;
440 if (!unsol)
441 return 0;
443 wp = (unsol->wp + 1) % HDA_UNSOL_QUEUE_SIZE;
444 unsol->wp = wp;
446 wp <<= 1;
447 unsol->queue[wp] = res;
448 unsol->queue[wp + 1] = res_ex;
450 queue_work(bus->workq, &unsol->work);
452 return 0;
454 EXPORT_SYMBOL_HDA(snd_hda_queue_unsol_event);
457 * process queued unsolicited events
459 static void process_unsol_events(struct work_struct *work)
461 struct hda_bus_unsolicited *unsol =
462 container_of(work, struct hda_bus_unsolicited, work);
463 struct hda_bus *bus = unsol->bus;
464 struct hda_codec *codec;
465 unsigned int rp, caddr, res;
467 while (unsol->rp != unsol->wp) {
468 rp = (unsol->rp + 1) % HDA_UNSOL_QUEUE_SIZE;
469 unsol->rp = rp;
470 rp <<= 1;
471 res = unsol->queue[rp];
472 caddr = unsol->queue[rp + 1];
473 if (!(caddr & (1 << 4))) /* no unsolicited event? */
474 continue;
475 codec = bus->caddr_tbl[caddr & 0x0f];
476 if (codec && codec->patch_ops.unsol_event)
477 codec->patch_ops.unsol_event(codec, res);
482 * initialize unsolicited queue
484 static int init_unsol_queue(struct hda_bus *bus)
486 struct hda_bus_unsolicited *unsol;
488 if (bus->unsol) /* already initialized */
489 return 0;
491 unsol = kzalloc(sizeof(*unsol), GFP_KERNEL);
492 if (!unsol) {
493 snd_printk(KERN_ERR "hda_codec: "
494 "can't allocate unsolicited queue\n");
495 return -ENOMEM;
497 INIT_WORK(&unsol->work, process_unsol_events);
498 unsol->bus = bus;
499 bus->unsol = unsol;
500 return 0;
504 * destructor
506 static void snd_hda_codec_free(struct hda_codec *codec);
508 static int snd_hda_bus_free(struct hda_bus *bus)
510 struct hda_codec *codec, *n;
512 if (!bus)
513 return 0;
514 if (bus->workq)
515 flush_workqueue(bus->workq);
516 if (bus->unsol)
517 kfree(bus->unsol);
518 list_for_each_entry_safe(codec, n, &bus->codec_list, list) {
519 snd_hda_codec_free(codec);
521 if (bus->ops.private_free)
522 bus->ops.private_free(bus);
523 if (bus->workq)
524 destroy_workqueue(bus->workq);
525 kfree(bus);
526 return 0;
529 static int snd_hda_bus_dev_free(struct snd_device *device)
531 struct hda_bus *bus = device->device_data;
532 bus->shutdown = 1;
533 return snd_hda_bus_free(bus);
536 #ifdef CONFIG_SND_HDA_HWDEP
537 static int snd_hda_bus_dev_register(struct snd_device *device)
539 struct hda_bus *bus = device->device_data;
540 struct hda_codec *codec;
541 list_for_each_entry(codec, &bus->codec_list, list) {
542 snd_hda_hwdep_add_sysfs(codec);
543 snd_hda_hwdep_add_power_sysfs(codec);
545 return 0;
547 #else
548 #define snd_hda_bus_dev_register NULL
549 #endif
552 * snd_hda_bus_new - create a HDA bus
553 * @card: the card entry
554 * @temp: the template for hda_bus information
555 * @busp: the pointer to store the created bus instance
557 * Returns 0 if successful, or a negative error code.
559 int /*__devinit*/ snd_hda_bus_new(struct snd_card *card,
560 const struct hda_bus_template *temp,
561 struct hda_bus **busp)
563 struct hda_bus *bus;
564 int err;
565 static struct snd_device_ops dev_ops = {
566 .dev_register = snd_hda_bus_dev_register,
567 .dev_free = snd_hda_bus_dev_free,
570 if (snd_BUG_ON(!temp))
571 return -EINVAL;
572 if (snd_BUG_ON(!temp->ops.command || !temp->ops.get_response))
573 return -EINVAL;
575 if (busp)
576 *busp = NULL;
578 bus = kzalloc(sizeof(*bus), GFP_KERNEL);
579 if (bus == NULL) {
580 snd_printk(KERN_ERR "can't allocate struct hda_bus\n");
581 return -ENOMEM;
584 bus->card = card;
585 bus->private_data = temp->private_data;
586 bus->pci = temp->pci;
587 bus->modelname = temp->modelname;
588 bus->power_save = temp->power_save;
589 bus->ops = temp->ops;
591 mutex_init(&bus->cmd_mutex);
592 mutex_init(&bus->prepare_mutex);
593 INIT_LIST_HEAD(&bus->codec_list);
595 snprintf(bus->workq_name, sizeof(bus->workq_name),
596 "hd-audio%d", card->number);
597 bus->workq = create_singlethread_workqueue(bus->workq_name);
598 if (!bus->workq) {
599 snd_printk(KERN_ERR "cannot create workqueue %s\n",
600 bus->workq_name);
601 kfree(bus);
602 return -ENOMEM;
605 err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops);
606 if (err < 0) {
607 snd_hda_bus_free(bus);
608 return err;
610 if (busp)
611 *busp = bus;
612 return 0;
614 EXPORT_SYMBOL_HDA(snd_hda_bus_new);
616 #ifdef CONFIG_SND_HDA_GENERIC
617 #define is_generic_config(codec) \
618 (codec->modelname && !strcmp(codec->modelname, "generic"))
619 #else
620 #define is_generic_config(codec) 0
621 #endif
623 #ifdef MODULE
624 #define HDA_MODREQ_MAX_COUNT 2 /* two request_modules()'s */
625 #else
626 #define HDA_MODREQ_MAX_COUNT 0 /* all presets are statically linked */
627 #endif
630 * find a matching codec preset
632 static const struct hda_codec_preset *
633 find_codec_preset(struct hda_codec *codec)
635 struct hda_codec_preset_list *tbl;
636 const struct hda_codec_preset *preset;
637 int mod_requested = 0;
639 if (is_generic_config(codec))
640 return NULL; /* use the generic parser */
642 again:
643 mutex_lock(&preset_mutex);
644 list_for_each_entry(tbl, &hda_preset_tables, list) {
645 if (!try_module_get(tbl->owner)) {
646 snd_printk(KERN_ERR "hda_codec: cannot module_get\n");
647 continue;
649 for (preset = tbl->preset; preset->id; preset++) {
650 u32 mask = preset->mask;
651 if (preset->afg && preset->afg != codec->afg)
652 continue;
653 if (preset->mfg && preset->mfg != codec->mfg)
654 continue;
655 if (!mask)
656 mask = ~0;
657 if (preset->id == (codec->vendor_id & mask) &&
658 (!preset->rev ||
659 preset->rev == codec->revision_id)) {
660 mutex_unlock(&preset_mutex);
661 codec->owner = tbl->owner;
662 return preset;
665 module_put(tbl->owner);
667 mutex_unlock(&preset_mutex);
669 if (mod_requested < HDA_MODREQ_MAX_COUNT) {
670 char name[32];
671 if (!mod_requested)
672 snprintf(name, sizeof(name), "snd-hda-codec-id:%08x",
673 codec->vendor_id);
674 else
675 snprintf(name, sizeof(name), "snd-hda-codec-id:%04x*",
676 (codec->vendor_id >> 16) & 0xffff);
677 request_module(name);
678 mod_requested++;
679 goto again;
681 return NULL;
685 * get_codec_name - store the codec name
687 static int get_codec_name(struct hda_codec *codec)
689 const struct hda_vendor_id *c;
690 const char *vendor = NULL;
691 u16 vendor_id = codec->vendor_id >> 16;
692 char tmp[16];
694 if (codec->vendor_name)
695 goto get_chip_name;
697 for (c = hda_vendor_ids; c->id; c++) {
698 if (c->id == vendor_id) {
699 vendor = c->name;
700 break;
703 if (!vendor) {
704 sprintf(tmp, "Generic %04x", vendor_id);
705 vendor = tmp;
707 codec->vendor_name = kstrdup(vendor, GFP_KERNEL);
708 if (!codec->vendor_name)
709 return -ENOMEM;
711 get_chip_name:
712 if (codec->chip_name)
713 return 0;
715 if (codec->preset && codec->preset->name)
716 codec->chip_name = kstrdup(codec->preset->name, GFP_KERNEL);
717 else {
718 sprintf(tmp, "ID %x", codec->vendor_id & 0xffff);
719 codec->chip_name = kstrdup(tmp, GFP_KERNEL);
721 if (!codec->chip_name)
722 return -ENOMEM;
723 return 0;
727 * look for an AFG and MFG nodes
729 static void /*__devinit*/ setup_fg_nodes(struct hda_codec *codec)
731 int i, total_nodes, function_id;
732 hda_nid_t nid;
734 total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid);
735 for (i = 0; i < total_nodes; i++, nid++) {
736 function_id = snd_hda_param_read(codec, nid,
737 AC_PAR_FUNCTION_TYPE);
738 switch (function_id & 0xff) {
739 case AC_GRP_AUDIO_FUNCTION:
740 codec->afg = nid;
741 codec->afg_function_id = function_id & 0xff;
742 codec->afg_unsol = (function_id >> 8) & 1;
743 break;
744 case AC_GRP_MODEM_FUNCTION:
745 codec->mfg = nid;
746 codec->mfg_function_id = function_id & 0xff;
747 codec->mfg_unsol = (function_id >> 8) & 1;
748 break;
749 default:
750 break;
756 * read widget caps for each widget and store in cache
758 static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
760 int i;
761 hda_nid_t nid;
763 codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node,
764 &codec->start_nid);
765 codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL);
766 if (!codec->wcaps)
767 return -ENOMEM;
768 nid = codec->start_nid;
769 for (i = 0; i < codec->num_nodes; i++, nid++)
770 codec->wcaps[i] = snd_hda_param_read(codec, nid,
771 AC_PAR_AUDIO_WIDGET_CAP);
772 return 0;
775 /* read all pin default configurations and save codec->init_pins */
776 static int read_pin_defaults(struct hda_codec *codec)
778 int i;
779 hda_nid_t nid = codec->start_nid;
781 for (i = 0; i < codec->num_nodes; i++, nid++) {
782 struct hda_pincfg *pin;
783 unsigned int wcaps = get_wcaps(codec, nid);
784 unsigned int wid_type = get_wcaps_type(wcaps);
785 if (wid_type != AC_WID_PIN)
786 continue;
787 pin = snd_array_new(&codec->init_pins);
788 if (!pin)
789 return -ENOMEM;
790 pin->nid = nid;
791 pin->cfg = snd_hda_codec_read(codec, nid, 0,
792 AC_VERB_GET_CONFIG_DEFAULT, 0);
793 pin->ctrl = snd_hda_codec_read(codec, nid, 0,
794 AC_VERB_GET_PIN_WIDGET_CONTROL,
797 return 0;
800 /* look up the given pin config list and return the item matching with NID */
801 static struct hda_pincfg *look_up_pincfg(struct hda_codec *codec,
802 struct snd_array *array,
803 hda_nid_t nid)
805 int i;
806 for (i = 0; i < array->used; i++) {
807 struct hda_pincfg *pin = snd_array_elem(array, i);
808 if (pin->nid == nid)
809 return pin;
811 return NULL;
814 /* write a config value for the given NID */
815 static void set_pincfg(struct hda_codec *codec, hda_nid_t nid,
816 unsigned int cfg)
818 int i;
819 for (i = 0; i < 4; i++) {
820 snd_hda_codec_write(codec, nid, 0,
821 AC_VERB_SET_CONFIG_DEFAULT_BYTES_0 + i,
822 cfg & 0xff);
823 cfg >>= 8;
827 /* set the current pin config value for the given NID.
828 * the value is cached, and read via snd_hda_codec_get_pincfg()
830 int snd_hda_add_pincfg(struct hda_codec *codec, struct snd_array *list,
831 hda_nid_t nid, unsigned int cfg)
833 struct hda_pincfg *pin;
834 unsigned int oldcfg;
836 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN)
837 return -EINVAL;
839 oldcfg = snd_hda_codec_get_pincfg(codec, nid);
840 pin = look_up_pincfg(codec, list, nid);
841 if (!pin) {
842 pin = snd_array_new(list);
843 if (!pin)
844 return -ENOMEM;
845 pin->nid = nid;
847 pin->cfg = cfg;
849 /* change only when needed; e.g. if the pincfg is already present
850 * in user_pins[], don't write it
852 cfg = snd_hda_codec_get_pincfg(codec, nid);
853 if (oldcfg != cfg)
854 set_pincfg(codec, nid, cfg);
855 return 0;
859 * snd_hda_codec_set_pincfg - Override a pin default configuration
860 * @codec: the HDA codec
861 * @nid: NID to set the pin config
862 * @cfg: the pin default config value
864 * Override a pin default configuration value in the cache.
865 * This value can be read by snd_hda_codec_get_pincfg() in a higher
866 * priority than the real hardware value.
868 int snd_hda_codec_set_pincfg(struct hda_codec *codec,
869 hda_nid_t nid, unsigned int cfg)
871 return snd_hda_add_pincfg(codec, &codec->driver_pins, nid, cfg);
873 EXPORT_SYMBOL_HDA(snd_hda_codec_set_pincfg);
876 * snd_hda_codec_get_pincfg - Obtain a pin-default configuration
877 * @codec: the HDA codec
878 * @nid: NID to get the pin config
880 * Get the current pin config value of the given pin NID.
881 * If the pincfg value is cached or overridden via sysfs or driver,
882 * returns the cached value.
884 unsigned int snd_hda_codec_get_pincfg(struct hda_codec *codec, hda_nid_t nid)
886 struct hda_pincfg *pin;
888 #ifdef CONFIG_SND_HDA_HWDEP
889 pin = look_up_pincfg(codec, &codec->user_pins, nid);
890 if (pin)
891 return pin->cfg;
892 #endif
893 pin = look_up_pincfg(codec, &codec->driver_pins, nid);
894 if (pin)
895 return pin->cfg;
896 pin = look_up_pincfg(codec, &codec->init_pins, nid);
897 if (pin)
898 return pin->cfg;
899 return 0;
901 EXPORT_SYMBOL_HDA(snd_hda_codec_get_pincfg);
903 /* restore all current pin configs */
904 static void restore_pincfgs(struct hda_codec *codec)
906 int i;
907 for (i = 0; i < codec->init_pins.used; i++) {
908 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
909 set_pincfg(codec, pin->nid,
910 snd_hda_codec_get_pincfg(codec, pin->nid));
915 * snd_hda_shutup_pins - Shut up all pins
916 * @codec: the HDA codec
918 * Clear all pin controls to shup up before suspend for avoiding click noise.
919 * The controls aren't cached so that they can be resumed properly.
921 void snd_hda_shutup_pins(struct hda_codec *codec)
923 int i;
924 /* don't shut up pins when unloading the driver; otherwise it breaks
925 * the default pin setup at the next load of the driver
927 if (codec->bus->shutdown)
928 return;
929 for (i = 0; i < codec->init_pins.used; i++) {
930 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
931 /* use read here for syncing after issuing each verb */
932 snd_hda_codec_read(codec, pin->nid, 0,
933 AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
935 codec->pins_shutup = 1;
937 EXPORT_SYMBOL_HDA(snd_hda_shutup_pins);
939 /* Restore the pin controls cleared previously via snd_hda_shutup_pins() */
940 static void restore_shutup_pins(struct hda_codec *codec)
942 int i;
943 if (!codec->pins_shutup)
944 return;
945 if (codec->bus->shutdown)
946 return;
947 for (i = 0; i < codec->init_pins.used; i++) {
948 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
949 snd_hda_codec_write(codec, pin->nid, 0,
950 AC_VERB_SET_PIN_WIDGET_CONTROL,
951 pin->ctrl);
953 codec->pins_shutup = 0;
956 static void init_hda_cache(struct hda_cache_rec *cache,
957 unsigned int record_size);
958 static void free_hda_cache(struct hda_cache_rec *cache);
960 /* restore the initial pin cfgs and release all pincfg lists */
961 static void restore_init_pincfgs(struct hda_codec *codec)
963 /* first free driver_pins and user_pins, then call restore_pincfg
964 * so that only the values in init_pins are restored
966 snd_array_free(&codec->driver_pins);
967 #ifdef CONFIG_SND_HDA_HWDEP
968 snd_array_free(&codec->user_pins);
969 #endif
970 restore_pincfgs(codec);
971 snd_array_free(&codec->init_pins);
975 * audio-converter setup caches
977 struct hda_cvt_setup {
978 hda_nid_t nid;
979 u8 stream_tag;
980 u8 channel_id;
981 u16 format_id;
982 unsigned char active; /* cvt is currently used */
983 unsigned char dirty; /* setups should be cleared */
986 /* get or create a cache entry for the given audio converter NID */
987 static struct hda_cvt_setup *
988 get_hda_cvt_setup(struct hda_codec *codec, hda_nid_t nid)
990 struct hda_cvt_setup *p;
991 int i;
993 for (i = 0; i < codec->cvt_setups.used; i++) {
994 p = snd_array_elem(&codec->cvt_setups, i);
995 if (p->nid == nid)
996 return p;
998 p = snd_array_new(&codec->cvt_setups);
999 if (p)
1000 p->nid = nid;
1001 return p;
1005 * codec destructor
1007 static void snd_hda_codec_free(struct hda_codec *codec)
1009 if (!codec)
1010 return;
1011 restore_init_pincfgs(codec);
1012 #ifdef CONFIG_SND_HDA_POWER_SAVE
1013 cancel_delayed_work(&codec->power_work);
1014 flush_workqueue(codec->bus->workq);
1015 #endif
1016 list_del(&codec->list);
1017 snd_array_free(&codec->mixers);
1018 snd_array_free(&codec->nids);
1019 codec->bus->caddr_tbl[codec->addr] = NULL;
1020 if (codec->patch_ops.free)
1021 codec->patch_ops.free(codec);
1022 module_put(codec->owner);
1023 free_hda_cache(&codec->amp_cache);
1024 free_hda_cache(&codec->cmd_cache);
1025 kfree(codec->vendor_name);
1026 kfree(codec->chip_name);
1027 kfree(codec->modelname);
1028 kfree(codec->wcaps);
1029 kfree(codec);
1032 static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
1033 unsigned int power_state);
1036 * snd_hda_codec_new - create a HDA codec
1037 * @bus: the bus to assign
1038 * @codec_addr: the codec address
1039 * @codecp: the pointer to store the generated codec
1041 * Returns 0 if successful, or a negative error code.
1043 int /*__devinit*/ snd_hda_codec_new(struct hda_bus *bus,
1044 unsigned int codec_addr,
1045 struct hda_codec **codecp)
1047 struct hda_codec *codec;
1048 char component[31];
1049 int err;
1051 if (snd_BUG_ON(!bus))
1052 return -EINVAL;
1053 if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS))
1054 return -EINVAL;
1056 if (bus->caddr_tbl[codec_addr]) {
1057 snd_printk(KERN_ERR "hda_codec: "
1058 "address 0x%x is already occupied\n", codec_addr);
1059 return -EBUSY;
1062 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
1063 if (codec == NULL) {
1064 snd_printk(KERN_ERR "can't allocate struct hda_codec\n");
1065 return -ENOMEM;
1068 codec->bus = bus;
1069 codec->addr = codec_addr;
1070 mutex_init(&codec->spdif_mutex);
1071 mutex_init(&codec->control_mutex);
1072 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
1073 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
1074 snd_array_init(&codec->mixers, sizeof(struct hda_nid_item), 32);
1075 snd_array_init(&codec->nids, sizeof(struct hda_nid_item), 32);
1076 snd_array_init(&codec->init_pins, sizeof(struct hda_pincfg), 16);
1077 snd_array_init(&codec->driver_pins, sizeof(struct hda_pincfg), 16);
1078 snd_array_init(&codec->cvt_setups, sizeof(struct hda_cvt_setup), 8);
1079 if (codec->bus->modelname) {
1080 codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL);
1081 if (!codec->modelname) {
1082 snd_hda_codec_free(codec);
1083 return -ENODEV;
1087 #ifdef CONFIG_SND_HDA_POWER_SAVE
1088 INIT_DELAYED_WORK(&codec->power_work, hda_power_work);
1089 /* snd_hda_codec_new() marks the codec as power-up, and leave it as is.
1090 * the caller has to power down appropriatley after initialization
1091 * phase.
1093 hda_keep_power_on(codec);
1094 #endif
1096 list_add_tail(&codec->list, &bus->codec_list);
1097 bus->caddr_tbl[codec_addr] = codec;
1099 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1100 AC_PAR_VENDOR_ID);
1101 if (codec->vendor_id == -1)
1102 /* read again, hopefully the access method was corrected
1103 * in the last read...
1105 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1106 AC_PAR_VENDOR_ID);
1107 codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1108 AC_PAR_SUBSYSTEM_ID);
1109 codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1110 AC_PAR_REV_ID);
1112 setup_fg_nodes(codec);
1113 if (!codec->afg && !codec->mfg) {
1114 snd_printdd("hda_codec: no AFG or MFG node found\n");
1115 err = -ENODEV;
1116 goto error;
1119 err = read_widget_caps(codec, codec->afg ? codec->afg : codec->mfg);
1120 if (err < 0) {
1121 snd_printk(KERN_ERR "hda_codec: cannot malloc\n");
1122 goto error;
1124 err = read_pin_defaults(codec);
1125 if (err < 0)
1126 goto error;
1128 if (!codec->subsystem_id) {
1129 hda_nid_t nid = codec->afg ? codec->afg : codec->mfg;
1130 codec->subsystem_id =
1131 snd_hda_codec_read(codec, nid, 0,
1132 AC_VERB_GET_SUBSYSTEM_ID, 0);
1135 /* power-up all before initialization */
1136 hda_set_power_state(codec,
1137 codec->afg ? codec->afg : codec->mfg,
1138 AC_PWRST_D0);
1140 snd_hda_codec_proc_new(codec);
1142 snd_hda_create_hwdep(codec);
1144 sprintf(component, "HDA:%08x,%08x,%08x", codec->vendor_id,
1145 codec->subsystem_id, codec->revision_id);
1146 snd_component_add(codec->bus->card, component);
1148 if (codecp)
1149 *codecp = codec;
1150 return 0;
1152 error:
1153 snd_hda_codec_free(codec);
1154 return err;
1156 EXPORT_SYMBOL_HDA(snd_hda_codec_new);
1159 * snd_hda_codec_configure - (Re-)configure the HD-audio codec
1160 * @codec: the HDA codec
1162 * Start parsing of the given codec tree and (re-)initialize the whole
1163 * patch instance.
1165 * Returns 0 if successful or a negative error code.
1167 int snd_hda_codec_configure(struct hda_codec *codec)
1169 int err;
1171 codec->preset = find_codec_preset(codec);
1172 if (!codec->vendor_name || !codec->chip_name) {
1173 err = get_codec_name(codec);
1174 if (err < 0)
1175 return err;
1178 if (is_generic_config(codec)) {
1179 err = snd_hda_parse_generic_codec(codec);
1180 goto patched;
1182 if (codec->preset && codec->preset->patch) {
1183 err = codec->preset->patch(codec);
1184 goto patched;
1187 /* call the default parser */
1188 err = snd_hda_parse_generic_codec(codec);
1189 if (err < 0)
1190 printk(KERN_ERR "hda-codec: No codec parser is available\n");
1192 patched:
1193 if (!err && codec->patch_ops.unsol_event)
1194 err = init_unsol_queue(codec->bus);
1195 /* audio codec should override the mixer name */
1196 if (!err && (codec->afg || !*codec->bus->card->mixername))
1197 snprintf(codec->bus->card->mixername,
1198 sizeof(codec->bus->card->mixername),
1199 "%s %s", codec->vendor_name, codec->chip_name);
1200 return err;
1202 EXPORT_SYMBOL_HDA(snd_hda_codec_configure);
1205 * snd_hda_codec_setup_stream - set up the codec for streaming
1206 * @codec: the CODEC to set up
1207 * @nid: the NID to set up
1208 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
1209 * @channel_id: channel id to pass, zero based.
1210 * @format: stream format.
1212 void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
1213 u32 stream_tag,
1214 int channel_id, int format)
1216 struct hda_codec *c;
1217 struct hda_cvt_setup *p;
1218 unsigned int oldval, newval;
1219 int type;
1220 int i;
1222 if (!nid)
1223 return;
1225 snd_printdd("hda_codec_setup_stream: "
1226 "NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
1227 nid, stream_tag, channel_id, format);
1228 p = get_hda_cvt_setup(codec, nid);
1229 if (!p)
1230 return;
1231 /* update the stream-id if changed */
1232 if (p->stream_tag != stream_tag || p->channel_id != channel_id) {
1233 oldval = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0);
1234 newval = (stream_tag << 4) | channel_id;
1235 if (oldval != newval)
1236 snd_hda_codec_write(codec, nid, 0,
1237 AC_VERB_SET_CHANNEL_STREAMID,
1238 newval);
1239 p->stream_tag = stream_tag;
1240 p->channel_id = channel_id;
1242 /* update the format-id if changed */
1243 if (p->format_id != format) {
1244 oldval = snd_hda_codec_read(codec, nid, 0,
1245 AC_VERB_GET_STREAM_FORMAT, 0);
1246 if (oldval != format) {
1247 msleep(1);
1248 snd_hda_codec_write(codec, nid, 0,
1249 AC_VERB_SET_STREAM_FORMAT,
1250 format);
1252 p->format_id = format;
1254 p->active = 1;
1255 p->dirty = 0;
1257 /* make other inactive cvts with the same stream-tag dirty */
1258 type = get_wcaps_type(get_wcaps(codec, nid));
1259 list_for_each_entry(c, &codec->bus->codec_list, list) {
1260 for (i = 0; i < c->cvt_setups.used; i++) {
1261 p = snd_array_elem(&c->cvt_setups, i);
1262 if (!p->active && p->stream_tag == stream_tag &&
1263 get_wcaps_type(get_wcaps(codec, p->nid)) == type)
1264 p->dirty = 1;
1268 EXPORT_SYMBOL_HDA(snd_hda_codec_setup_stream);
1270 static void really_cleanup_stream(struct hda_codec *codec,
1271 struct hda_cvt_setup *q);
1274 * __snd_hda_codec_cleanup_stream - clean up the codec for closing
1275 * @codec: the CODEC to clean up
1276 * @nid: the NID to clean up
1277 * @do_now: really clean up the stream instead of clearing the active flag
1279 void __snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid,
1280 int do_now)
1282 struct hda_cvt_setup *p;
1284 if (!nid)
1285 return;
1287 if (codec->no_sticky_stream)
1288 do_now = 1;
1290 snd_printdd("hda_codec_cleanup_stream: NID=0x%x\n", nid);
1291 p = get_hda_cvt_setup(codec, nid);
1292 if (p) {
1293 /* here we just clear the active flag when do_now isn't set;
1294 * actual clean-ups will be done later in
1295 * purify_inactive_streams() called from snd_hda_codec_prpapre()
1297 if (do_now)
1298 really_cleanup_stream(codec, p);
1299 else
1300 p->active = 0;
1303 EXPORT_SYMBOL_HDA(__snd_hda_codec_cleanup_stream);
1305 static void really_cleanup_stream(struct hda_codec *codec,
1306 struct hda_cvt_setup *q)
1308 hda_nid_t nid = q->nid;
1309 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
1310 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0);
1311 memset(q, 0, sizeof(*q));
1312 q->nid = nid;
1315 /* clean up the all conflicting obsolete streams */
1316 static void purify_inactive_streams(struct hda_codec *codec)
1318 struct hda_codec *c;
1319 int i;
1321 list_for_each_entry(c, &codec->bus->codec_list, list) {
1322 for (i = 0; i < c->cvt_setups.used; i++) {
1323 struct hda_cvt_setup *p;
1324 p = snd_array_elem(&c->cvt_setups, i);
1325 if (p->dirty)
1326 really_cleanup_stream(c, p);
1331 /* clean up all streams; called from suspend */
1332 static void hda_cleanup_all_streams(struct hda_codec *codec)
1334 int i;
1336 for (i = 0; i < codec->cvt_setups.used; i++) {
1337 struct hda_cvt_setup *p = snd_array_elem(&codec->cvt_setups, i);
1338 if (p->stream_tag)
1339 really_cleanup_stream(codec, p);
1344 * amp access functions
1347 /* FIXME: more better hash key? */
1348 #define HDA_HASH_KEY(nid, dir, idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
1349 #define HDA_HASH_PINCAP_KEY(nid) (u32)((nid) + (0x02 << 24))
1350 #define HDA_HASH_PARPCM_KEY(nid) (u32)((nid) + (0x03 << 24))
1351 #define HDA_HASH_PARSTR_KEY(nid) (u32)((nid) + (0x04 << 24))
1352 #define INFO_AMP_CAPS (1<<0)
1353 #define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
1355 /* initialize the hash table */
1356 static void /*__devinit*/ init_hda_cache(struct hda_cache_rec *cache,
1357 unsigned int record_size)
1359 memset(cache, 0, sizeof(*cache));
1360 memset(cache->hash, 0xff, sizeof(cache->hash));
1361 snd_array_init(&cache->buf, record_size, 64);
1364 static void free_hda_cache(struct hda_cache_rec *cache)
1366 snd_array_free(&cache->buf);
1369 /* query the hash. allocate an entry if not found. */
1370 static struct hda_cache_head *get_hash(struct hda_cache_rec *cache, u32 key)
1372 u16 idx = key % (u16)ARRAY_SIZE(cache->hash);
1373 u16 cur = cache->hash[idx];
1374 struct hda_cache_head *info;
1376 while (cur != 0xffff) {
1377 info = snd_array_elem(&cache->buf, cur);
1378 if (info->key == key)
1379 return info;
1380 cur = info->next;
1382 return NULL;
1385 /* query the hash. allocate an entry if not found. */
1386 static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache,
1387 u32 key)
1389 struct hda_cache_head *info = get_hash(cache, key);
1390 if (!info) {
1391 u16 idx, cur;
1392 /* add a new hash entry */
1393 info = snd_array_new(&cache->buf);
1394 if (!info)
1395 return NULL;
1396 cur = snd_array_index(&cache->buf, info);
1397 info->key = key;
1398 info->val = 0;
1399 idx = key % (u16)ARRAY_SIZE(cache->hash);
1400 info->next = cache->hash[idx];
1401 cache->hash[idx] = cur;
1403 return info;
1406 /* query and allocate an amp hash entry */
1407 static inline struct hda_amp_info *
1408 get_alloc_amp_hash(struct hda_codec *codec, u32 key)
1410 return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key);
1414 * query_amp_caps - query AMP capabilities
1415 * @codec: the HD-auio codec
1416 * @nid: the NID to query
1417 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1419 * Query AMP capabilities for the given widget and direction.
1420 * Returns the obtained capability bits.
1422 * When cap bits have been already read, this doesn't read again but
1423 * returns the cached value.
1425 u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
1427 struct hda_amp_info *info;
1429 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, 0));
1430 if (!info)
1431 return 0;
1432 if (!(info->head.val & INFO_AMP_CAPS)) {
1433 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
1434 nid = codec->afg;
1435 info->amp_caps = snd_hda_param_read(codec, nid,
1436 direction == HDA_OUTPUT ?
1437 AC_PAR_AMP_OUT_CAP :
1438 AC_PAR_AMP_IN_CAP);
1439 if (info->amp_caps)
1440 info->head.val |= INFO_AMP_CAPS;
1442 return info->amp_caps;
1444 EXPORT_SYMBOL_HDA(query_amp_caps);
1447 * snd_hda_override_amp_caps - Override the AMP capabilities
1448 * @codec: the CODEC to clean up
1449 * @nid: the NID to clean up
1450 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1451 * @caps: the capability bits to set
1453 * Override the cached AMP caps bits value by the given one.
1454 * This function is useful if the driver needs to adjust the AMP ranges,
1455 * e.g. limit to 0dB, etc.
1457 * Returns zero if successful or a negative error code.
1459 int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
1460 unsigned int caps)
1462 struct hda_amp_info *info;
1464 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, dir, 0));
1465 if (!info)
1466 return -EINVAL;
1467 info->amp_caps = caps;
1468 info->head.val |= INFO_AMP_CAPS;
1469 return 0;
1471 EXPORT_SYMBOL_HDA(snd_hda_override_amp_caps);
1473 static unsigned int
1474 query_caps_hash(struct hda_codec *codec, hda_nid_t nid, u32 key,
1475 unsigned int (*func)(struct hda_codec *, hda_nid_t))
1477 struct hda_amp_info *info;
1479 info = get_alloc_amp_hash(codec, key);
1480 if (!info)
1481 return 0;
1482 if (!info->head.val) {
1483 info->head.val |= INFO_AMP_CAPS;
1484 info->amp_caps = func(codec, nid);
1486 return info->amp_caps;
1489 static unsigned int read_pin_cap(struct hda_codec *codec, hda_nid_t nid)
1491 return snd_hda_param_read(codec, nid, AC_PAR_PIN_CAP);
1495 * snd_hda_query_pin_caps - Query PIN capabilities
1496 * @codec: the HD-auio codec
1497 * @nid: the NID to query
1499 * Query PIN capabilities for the given widget.
1500 * Returns the obtained capability bits.
1502 * When cap bits have been already read, this doesn't read again but
1503 * returns the cached value.
1505 u32 snd_hda_query_pin_caps(struct hda_codec *codec, hda_nid_t nid)
1507 return query_caps_hash(codec, nid, HDA_HASH_PINCAP_KEY(nid),
1508 read_pin_cap);
1510 EXPORT_SYMBOL_HDA(snd_hda_query_pin_caps);
1513 * snd_hda_pin_sense - execute pin sense measurement
1514 * @codec: the CODEC to sense
1515 * @nid: the pin NID to sense
1517 * Execute necessary pin sense measurement and return its Presence Detect,
1518 * Impedance, ELD Valid etc. status bits.
1520 u32 snd_hda_pin_sense(struct hda_codec *codec, hda_nid_t nid)
1522 u32 pincap;
1524 if (!codec->no_trigger_sense) {
1525 pincap = snd_hda_query_pin_caps(codec, nid);
1526 if (pincap & AC_PINCAP_TRIG_REQ) /* need trigger? */
1527 snd_hda_codec_read(codec, nid, 0,
1528 AC_VERB_SET_PIN_SENSE, 0);
1530 return snd_hda_codec_read(codec, nid, 0,
1531 AC_VERB_GET_PIN_SENSE, 0);
1533 EXPORT_SYMBOL_HDA(snd_hda_pin_sense);
1536 * snd_hda_jack_detect - query pin Presence Detect status
1537 * @codec: the CODEC to sense
1538 * @nid: the pin NID to sense
1540 * Query and return the pin's Presence Detect status.
1542 int snd_hda_jack_detect(struct hda_codec *codec, hda_nid_t nid)
1544 u32 sense = snd_hda_pin_sense(codec, nid);
1545 return !!(sense & AC_PINSENSE_PRESENCE);
1547 EXPORT_SYMBOL_HDA(snd_hda_jack_detect);
1550 * read the current volume to info
1551 * if the cache exists, read the cache value.
1553 static unsigned int get_vol_mute(struct hda_codec *codec,
1554 struct hda_amp_info *info, hda_nid_t nid,
1555 int ch, int direction, int index)
1557 u32 val, parm;
1559 if (info->head.val & INFO_AMP_VOL(ch))
1560 return info->vol[ch];
1562 parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
1563 parm |= direction == HDA_OUTPUT ? AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
1564 parm |= index;
1565 val = snd_hda_codec_read(codec, nid, 0,
1566 AC_VERB_GET_AMP_GAIN_MUTE, parm);
1567 info->vol[ch] = val & 0xff;
1568 info->head.val |= INFO_AMP_VOL(ch);
1569 return info->vol[ch];
1573 * write the current volume in info to the h/w and update the cache
1575 static void put_vol_mute(struct hda_codec *codec, struct hda_amp_info *info,
1576 hda_nid_t nid, int ch, int direction, int index,
1577 int val)
1579 u32 parm;
1581 parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
1582 parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
1583 parm |= index << AC_AMP_SET_INDEX_SHIFT;
1584 parm |= val;
1585 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
1586 info->vol[ch] = val;
1590 * snd_hda_codec_amp_read - Read AMP value
1591 * @codec: HD-audio codec
1592 * @nid: NID to read the AMP value
1593 * @ch: channel (left=0 or right=1)
1594 * @direction: #HDA_INPUT or #HDA_OUTPUT
1595 * @index: the index value (only for input direction)
1597 * Read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
1599 int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
1600 int direction, int index)
1602 struct hda_amp_info *info;
1603 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
1604 if (!info)
1605 return 0;
1606 return get_vol_mute(codec, info, nid, ch, direction, index);
1608 EXPORT_SYMBOL_HDA(snd_hda_codec_amp_read);
1611 * snd_hda_codec_amp_update - update the AMP value
1612 * @codec: HD-audio codec
1613 * @nid: NID to read the AMP value
1614 * @ch: channel (left=0 or right=1)
1615 * @direction: #HDA_INPUT or #HDA_OUTPUT
1616 * @idx: the index value (only for input direction)
1617 * @mask: bit mask to set
1618 * @val: the bits value to set
1620 * Update the AMP value with a bit mask.
1621 * Returns 0 if the value is unchanged, 1 if changed.
1623 int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
1624 int direction, int idx, int mask, int val)
1626 struct hda_amp_info *info;
1628 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, idx));
1629 if (!info)
1630 return 0;
1631 if (snd_BUG_ON(mask & ~0xff))
1632 mask &= 0xff;
1633 val &= mask;
1634 val |= get_vol_mute(codec, info, nid, ch, direction, idx) & ~mask;
1635 if (info->vol[ch] == val)
1636 return 0;
1637 put_vol_mute(codec, info, nid, ch, direction, idx, val);
1638 return 1;
1640 EXPORT_SYMBOL_HDA(snd_hda_codec_amp_update);
1643 * snd_hda_codec_amp_stereo - update the AMP stereo values
1644 * @codec: HD-audio codec
1645 * @nid: NID to read the AMP value
1646 * @direction: #HDA_INPUT or #HDA_OUTPUT
1647 * @idx: the index value (only for input direction)
1648 * @mask: bit mask to set
1649 * @val: the bits value to set
1651 * Update the AMP values like snd_hda_codec_amp_update(), but for a
1652 * stereo widget with the same mask and value.
1654 int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
1655 int direction, int idx, int mask, int val)
1657 int ch, ret = 0;
1659 if (snd_BUG_ON(mask & ~0xff))
1660 mask &= 0xff;
1661 for (ch = 0; ch < 2; ch++)
1662 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
1663 idx, mask, val);
1664 return ret;
1666 EXPORT_SYMBOL_HDA(snd_hda_codec_amp_stereo);
1668 #ifdef SND_HDA_NEEDS_RESUME
1670 * snd_hda_codec_resume_amp - Resume all AMP commands from the cache
1671 * @codec: HD-audio codec
1673 * Resume the all amp commands from the cache.
1675 void snd_hda_codec_resume_amp(struct hda_codec *codec)
1677 struct hda_amp_info *buffer = codec->amp_cache.buf.list;
1678 int i;
1680 for (i = 0; i < codec->amp_cache.buf.used; i++, buffer++) {
1681 u32 key = buffer->head.key;
1682 hda_nid_t nid;
1683 unsigned int idx, dir, ch;
1684 if (!key)
1685 continue;
1686 nid = key & 0xff;
1687 idx = (key >> 16) & 0xff;
1688 dir = (key >> 24) & 0xff;
1689 for (ch = 0; ch < 2; ch++) {
1690 if (!(buffer->head.val & INFO_AMP_VOL(ch)))
1691 continue;
1692 put_vol_mute(codec, buffer, nid, ch, dir, idx,
1693 buffer->vol[ch]);
1697 EXPORT_SYMBOL_HDA(snd_hda_codec_resume_amp);
1698 #endif /* SND_HDA_NEEDS_RESUME */
1700 static u32 get_amp_max_value(struct hda_codec *codec, hda_nid_t nid, int dir,
1701 unsigned int ofs)
1703 u32 caps = query_amp_caps(codec, nid, dir);
1704 /* get num steps */
1705 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1706 if (ofs < caps)
1707 caps -= ofs;
1708 return caps;
1712 * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer
1714 * The control element is supposed to have the private_value field
1715 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1717 int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
1718 struct snd_ctl_elem_info *uinfo)
1720 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1721 u16 nid = get_amp_nid(kcontrol);
1722 u8 chs = get_amp_channels(kcontrol);
1723 int dir = get_amp_direction(kcontrol);
1724 unsigned int ofs = get_amp_offset(kcontrol);
1726 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1727 uinfo->count = chs == 3 ? 2 : 1;
1728 uinfo->value.integer.min = 0;
1729 uinfo->value.integer.max = get_amp_max_value(codec, nid, dir, ofs);
1730 if (!uinfo->value.integer.max) {
1731 printk(KERN_WARNING "hda_codec: "
1732 "num_steps = 0 for NID=0x%x (ctl = %s)\n", nid,
1733 kcontrol->id.name);
1734 return -EINVAL;
1736 return 0;
1738 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_info);
1741 static inline unsigned int
1742 read_amp_value(struct hda_codec *codec, hda_nid_t nid,
1743 int ch, int dir, int idx, unsigned int ofs)
1745 unsigned int val;
1746 val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
1747 val &= HDA_AMP_VOLMASK;
1748 if (val >= ofs)
1749 val -= ofs;
1750 else
1751 val = 0;
1752 return val;
1755 static inline int
1756 update_amp_value(struct hda_codec *codec, hda_nid_t nid,
1757 int ch, int dir, int idx, unsigned int ofs,
1758 unsigned int val)
1760 unsigned int maxval;
1762 if (val > 0)
1763 val += ofs;
1764 /* ofs = 0: raw max value */
1765 maxval = get_amp_max_value(codec, nid, dir, 0);
1766 if (val > maxval)
1767 val = maxval;
1768 return snd_hda_codec_amp_update(codec, nid, ch, dir, idx,
1769 HDA_AMP_VOLMASK, val);
1773 * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume
1775 * The control element is supposed to have the private_value field
1776 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1778 int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
1779 struct snd_ctl_elem_value *ucontrol)
1781 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1782 hda_nid_t nid = get_amp_nid(kcontrol);
1783 int chs = get_amp_channels(kcontrol);
1784 int dir = get_amp_direction(kcontrol);
1785 int idx = get_amp_index(kcontrol);
1786 unsigned int ofs = get_amp_offset(kcontrol);
1787 long *valp = ucontrol->value.integer.value;
1789 if (chs & 1)
1790 *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs);
1791 if (chs & 2)
1792 *valp = read_amp_value(codec, nid, 1, dir, idx, ofs);
1793 return 0;
1795 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_get);
1798 * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume
1800 * The control element is supposed to have the private_value field
1801 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1803 int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
1804 struct snd_ctl_elem_value *ucontrol)
1806 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1807 hda_nid_t nid = get_amp_nid(kcontrol);
1808 int chs = get_amp_channels(kcontrol);
1809 int dir = get_amp_direction(kcontrol);
1810 int idx = get_amp_index(kcontrol);
1811 unsigned int ofs = get_amp_offset(kcontrol);
1812 long *valp = ucontrol->value.integer.value;
1813 int change = 0;
1815 snd_hda_power_up(codec);
1816 if (chs & 1) {
1817 change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp);
1818 valp++;
1820 if (chs & 2)
1821 change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp);
1822 snd_hda_power_down(codec);
1823 return change;
1825 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_put);
1828 * snd_hda_mixer_amp_volume_put - TLV callback for a standard AMP mixer volume
1830 * The control element is supposed to have the private_value field
1831 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1833 int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
1834 unsigned int size, unsigned int __user *_tlv)
1836 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1837 hda_nid_t nid = get_amp_nid(kcontrol);
1838 int dir = get_amp_direction(kcontrol);
1839 unsigned int ofs = get_amp_offset(kcontrol);
1840 bool min_mute = get_amp_min_mute(kcontrol);
1841 u32 caps, val1, val2;
1843 if (size < 4 * sizeof(unsigned int))
1844 return -ENOMEM;
1845 caps = query_amp_caps(codec, nid, dir);
1846 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
1847 val2 = (val2 + 1) * 25;
1848 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
1849 val1 += ofs;
1850 val1 = ((int)val1) * ((int)val2);
1851 if (min_mute)
1852 val2 |= TLV_DB_SCALE_MUTE;
1853 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
1854 return -EFAULT;
1855 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
1856 return -EFAULT;
1857 if (put_user(val1, _tlv + 2))
1858 return -EFAULT;
1859 if (put_user(val2, _tlv + 3))
1860 return -EFAULT;
1861 return 0;
1863 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_tlv);
1866 * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control
1867 * @codec: HD-audio codec
1868 * @nid: NID of a reference widget
1869 * @dir: #HDA_INPUT or #HDA_OUTPUT
1870 * @tlv: TLV data to be stored, at least 4 elements
1872 * Set (static) TLV data for a virtual master volume using the AMP caps
1873 * obtained from the reference NID.
1874 * The volume range is recalculated as if the max volume is 0dB.
1876 void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
1877 unsigned int *tlv)
1879 u32 caps;
1880 int nums, step;
1882 caps = query_amp_caps(codec, nid, dir);
1883 nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1884 step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
1885 step = (step + 1) * 25;
1886 tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
1887 tlv[1] = 2 * sizeof(unsigned int);
1888 tlv[2] = -nums * step;
1889 tlv[3] = step;
1891 EXPORT_SYMBOL_HDA(snd_hda_set_vmaster_tlv);
1893 /* find a mixer control element with the given name */
1894 static struct snd_kcontrol *
1895 _snd_hda_find_mixer_ctl(struct hda_codec *codec,
1896 const char *name, int idx)
1898 struct snd_ctl_elem_id id;
1899 memset(&id, 0, sizeof(id));
1900 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1901 id.index = idx;
1902 if (snd_BUG_ON(strlen(name) >= sizeof(id.name)))
1903 return NULL;
1904 strcpy(id.name, name);
1905 return snd_ctl_find_id(codec->bus->card, &id);
1909 * snd_hda_find_mixer_ctl - Find a mixer control element with the given name
1910 * @codec: HD-audio codec
1911 * @name: ctl id name string
1913 * Get the control element with the given id string and IFACE_MIXER.
1915 struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
1916 const char *name)
1918 return _snd_hda_find_mixer_ctl(codec, name, 0);
1920 EXPORT_SYMBOL_HDA(snd_hda_find_mixer_ctl);
1923 * snd_hda_ctl_add - Add a control element and assign to the codec
1924 * @codec: HD-audio codec
1925 * @nid: corresponding NID (optional)
1926 * @kctl: the control element to assign
1928 * Add the given control element to an array inside the codec instance.
1929 * All control elements belonging to a codec are supposed to be added
1930 * by this function so that a proper clean-up works at the free or
1931 * reconfiguration time.
1933 * If non-zero @nid is passed, the NID is assigned to the control element.
1934 * The assignment is shown in the codec proc file.
1936 * snd_hda_ctl_add() checks the control subdev id field whether
1937 * #HDA_SUBDEV_NID_FLAG bit is set. If set (and @nid is zero), the lower
1938 * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit
1939 * specifies if kctl->private_value is a HDA amplifier value.
1941 int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid,
1942 struct snd_kcontrol *kctl)
1944 int err;
1945 unsigned short flags = 0;
1946 struct hda_nid_item *item;
1948 if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) {
1949 flags |= HDA_NID_ITEM_AMP;
1950 if (nid == 0)
1951 nid = get_amp_nid_(kctl->private_value);
1953 if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0)
1954 nid = kctl->id.subdevice & 0xffff;
1955 if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG))
1956 kctl->id.subdevice = 0;
1957 err = snd_ctl_add(codec->bus->card, kctl);
1958 if (err < 0)
1959 return err;
1960 item = snd_array_new(&codec->mixers);
1961 if (!item)
1962 return -ENOMEM;
1963 item->kctl = kctl;
1964 item->nid = nid;
1965 item->flags = flags;
1966 return 0;
1968 EXPORT_SYMBOL_HDA(snd_hda_ctl_add);
1971 * snd_hda_add_nid - Assign a NID to a control element
1972 * @codec: HD-audio codec
1973 * @nid: corresponding NID (optional)
1974 * @kctl: the control element to assign
1975 * @index: index to kctl
1977 * Add the given control element to an array inside the codec instance.
1978 * This function is used when #snd_hda_ctl_add cannot be used for 1:1
1979 * NID:KCTL mapping - for example "Capture Source" selector.
1981 int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl,
1982 unsigned int index, hda_nid_t nid)
1984 struct hda_nid_item *item;
1986 if (nid > 0) {
1987 item = snd_array_new(&codec->nids);
1988 if (!item)
1989 return -ENOMEM;
1990 item->kctl = kctl;
1991 item->index = index;
1992 item->nid = nid;
1993 return 0;
1995 printk(KERN_ERR "hda-codec: no NID for mapping control %s:%d:%d\n",
1996 kctl->id.name, kctl->id.index, index);
1997 return -EINVAL;
1999 EXPORT_SYMBOL_HDA(snd_hda_add_nid);
2002 * snd_hda_ctls_clear - Clear all controls assigned to the given codec
2003 * @codec: HD-audio codec
2005 void snd_hda_ctls_clear(struct hda_codec *codec)
2007 int i;
2008 struct hda_nid_item *items = codec->mixers.list;
2009 for (i = 0; i < codec->mixers.used; i++)
2010 snd_ctl_remove(codec->bus->card, items[i].kctl);
2011 snd_array_free(&codec->mixers);
2012 snd_array_free(&codec->nids);
2015 /* pseudo device locking
2016 * toggle card->shutdown to allow/disallow the device access (as a hack)
2018 static int hda_lock_devices(struct snd_card *card)
2020 spin_lock(&card->files_lock);
2021 if (card->shutdown) {
2022 spin_unlock(&card->files_lock);
2023 return -EINVAL;
2025 card->shutdown = 1;
2026 spin_unlock(&card->files_lock);
2027 return 0;
2030 static void hda_unlock_devices(struct snd_card *card)
2032 spin_lock(&card->files_lock);
2033 card->shutdown = 0;
2034 spin_unlock(&card->files_lock);
2038 * snd_hda_codec_reset - Clear all objects assigned to the codec
2039 * @codec: HD-audio codec
2041 * This frees the all PCM and control elements assigned to the codec, and
2042 * clears the caches and restores the pin default configurations.
2044 * When a device is being used, it returns -EBSY. If successfully freed,
2045 * returns zero.
2047 int snd_hda_codec_reset(struct hda_codec *codec)
2049 struct snd_card *card = codec->bus->card;
2050 int i, pcm;
2052 if (hda_lock_devices(card) < 0)
2053 return -EBUSY;
2054 /* check whether the codec isn't used by any mixer or PCM streams */
2055 if (!list_empty(&card->ctl_files)) {
2056 hda_unlock_devices(card);
2057 return -EBUSY;
2059 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
2060 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
2061 if (!cpcm->pcm)
2062 continue;
2063 if (cpcm->pcm->streams[0].substream_opened ||
2064 cpcm->pcm->streams[1].substream_opened) {
2065 hda_unlock_devices(card);
2066 return -EBUSY;
2070 /* OK, let it free */
2072 #ifdef CONFIG_SND_HDA_POWER_SAVE
2073 cancel_delayed_work(&codec->power_work);
2074 flush_workqueue(codec->bus->workq);
2075 #endif
2076 snd_hda_ctls_clear(codec);
2077 /* relase PCMs */
2078 for (i = 0; i < codec->num_pcms; i++) {
2079 if (codec->pcm_info[i].pcm) {
2080 snd_device_free(card, codec->pcm_info[i].pcm);
2081 clear_bit(codec->pcm_info[i].device,
2082 codec->bus->pcm_dev_bits);
2085 if (codec->patch_ops.free)
2086 codec->patch_ops.free(codec);
2087 codec->proc_widget_hook = NULL;
2088 codec->spec = NULL;
2089 free_hda_cache(&codec->amp_cache);
2090 free_hda_cache(&codec->cmd_cache);
2091 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
2092 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
2093 /* free only driver_pins so that init_pins + user_pins are restored */
2094 snd_array_free(&codec->driver_pins);
2095 restore_pincfgs(codec);
2096 codec->num_pcms = 0;
2097 codec->pcm_info = NULL;
2098 codec->preset = NULL;
2099 memset(&codec->patch_ops, 0, sizeof(codec->patch_ops));
2100 codec->slave_dig_outs = NULL;
2101 codec->spdif_status_reset = 0;
2102 module_put(codec->owner);
2103 codec->owner = NULL;
2105 /* allow device access again */
2106 hda_unlock_devices(card);
2107 return 0;
2111 * snd_hda_add_vmaster - create a virtual master control and add slaves
2112 * @codec: HD-audio codec
2113 * @name: vmaster control name
2114 * @tlv: TLV data (optional)
2115 * @slaves: slave control names (optional)
2117 * Create a virtual master control with the given name. The TLV data
2118 * must be either NULL or a valid data.
2120 * @slaves is a NULL-terminated array of strings, each of which is a
2121 * slave control name. All controls with these names are assigned to
2122 * the new virtual master control.
2124 * This function returns zero if successful or a negative error code.
2126 int snd_hda_add_vmaster(struct hda_codec *codec, char *name,
2127 unsigned int *tlv, const char **slaves)
2129 struct snd_kcontrol *kctl;
2130 const char **s;
2131 int err;
2133 for (s = slaves; *s && !snd_hda_find_mixer_ctl(codec, *s); s++)
2135 if (!*s) {
2136 snd_printdd("No slave found for %s\n", name);
2137 return 0;
2139 kctl = snd_ctl_make_virtual_master(name, tlv);
2140 if (!kctl)
2141 return -ENOMEM;
2142 err = snd_hda_ctl_add(codec, 0, kctl);
2143 if (err < 0)
2144 return err;
2146 for (s = slaves; *s; s++) {
2147 struct snd_kcontrol *sctl;
2148 int i = 0;
2149 for (;;) {
2150 sctl = _snd_hda_find_mixer_ctl(codec, *s, i);
2151 if (!sctl) {
2152 if (!i)
2153 snd_printdd("Cannot find slave %s, "
2154 "skipped\n", *s);
2155 break;
2157 err = snd_ctl_add_slave(kctl, sctl);
2158 if (err < 0)
2159 return err;
2160 i++;
2163 return 0;
2165 EXPORT_SYMBOL_HDA(snd_hda_add_vmaster);
2168 * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch
2170 * The control element is supposed to have the private_value field
2171 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2173 int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
2174 struct snd_ctl_elem_info *uinfo)
2176 int chs = get_amp_channels(kcontrol);
2178 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2179 uinfo->count = chs == 3 ? 2 : 1;
2180 uinfo->value.integer.min = 0;
2181 uinfo->value.integer.max = 1;
2182 return 0;
2184 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_info);
2187 * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch
2189 * The control element is supposed to have the private_value field
2190 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2192 int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
2193 struct snd_ctl_elem_value *ucontrol)
2195 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2196 hda_nid_t nid = get_amp_nid(kcontrol);
2197 int chs = get_amp_channels(kcontrol);
2198 int dir = get_amp_direction(kcontrol);
2199 int idx = get_amp_index(kcontrol);
2200 long *valp = ucontrol->value.integer.value;
2202 if (chs & 1)
2203 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
2204 HDA_AMP_MUTE) ? 0 : 1;
2205 if (chs & 2)
2206 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
2207 HDA_AMP_MUTE) ? 0 : 1;
2208 return 0;
2210 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_get);
2213 * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch
2215 * The control element is supposed to have the private_value field
2216 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2218 int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
2219 struct snd_ctl_elem_value *ucontrol)
2221 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2222 hda_nid_t nid = get_amp_nid(kcontrol);
2223 int chs = get_amp_channels(kcontrol);
2224 int dir = get_amp_direction(kcontrol);
2225 int idx = get_amp_index(kcontrol);
2226 long *valp = ucontrol->value.integer.value;
2227 int change = 0;
2229 snd_hda_power_up(codec);
2230 if (chs & 1) {
2231 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
2232 HDA_AMP_MUTE,
2233 *valp ? 0 : HDA_AMP_MUTE);
2234 valp++;
2236 if (chs & 2)
2237 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
2238 HDA_AMP_MUTE,
2239 *valp ? 0 : HDA_AMP_MUTE);
2240 hda_call_check_power_status(codec, nid);
2241 snd_hda_power_down(codec);
2242 return change;
2244 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put);
2246 #ifdef CONFIG_SND_HDA_INPUT_BEEP
2248 * snd_hda_mixer_amp_switch_put_beep - Put callback for a beep AMP switch
2250 * This function calls snd_hda_enable_beep_device(), which behaves differently
2251 * depending on beep_mode option.
2253 int snd_hda_mixer_amp_switch_put_beep(struct snd_kcontrol *kcontrol,
2254 struct snd_ctl_elem_value *ucontrol)
2256 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2257 long *valp = ucontrol->value.integer.value;
2259 snd_hda_enable_beep_device(codec, *valp);
2260 return snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2262 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put_beep);
2263 #endif /* CONFIG_SND_HDA_INPUT_BEEP */
2266 * bound volume controls
2268 * bind multiple volumes (# indices, from 0)
2271 #define AMP_VAL_IDX_SHIFT 19
2272 #define AMP_VAL_IDX_MASK (0x0f<<19)
2275 * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control
2277 * The control element is supposed to have the private_value field
2278 * set up via HDA_BIND_MUTE*() macros.
2280 int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
2281 struct snd_ctl_elem_value *ucontrol)
2283 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2284 unsigned long pval;
2285 int err;
2287 mutex_lock(&codec->control_mutex);
2288 pval = kcontrol->private_value;
2289 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
2290 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
2291 kcontrol->private_value = pval;
2292 mutex_unlock(&codec->control_mutex);
2293 return err;
2295 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_get);
2298 * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control
2300 * The control element is supposed to have the private_value field
2301 * set up via HDA_BIND_MUTE*() macros.
2303 int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
2304 struct snd_ctl_elem_value *ucontrol)
2306 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2307 unsigned long pval;
2308 int i, indices, err = 0, change = 0;
2310 mutex_lock(&codec->control_mutex);
2311 pval = kcontrol->private_value;
2312 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
2313 for (i = 0; i < indices; i++) {
2314 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
2315 (i << AMP_VAL_IDX_SHIFT);
2316 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2317 if (err < 0)
2318 break;
2319 change |= err;
2321 kcontrol->private_value = pval;
2322 mutex_unlock(&codec->control_mutex);
2323 return err < 0 ? err : change;
2325 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_put);
2328 * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control
2330 * The control element is supposed to have the private_value field
2331 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2333 int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
2334 struct snd_ctl_elem_info *uinfo)
2336 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2337 struct hda_bind_ctls *c;
2338 int err;
2340 mutex_lock(&codec->control_mutex);
2341 c = (struct hda_bind_ctls *)kcontrol->private_value;
2342 kcontrol->private_value = *c->values;
2343 err = c->ops->info(kcontrol, uinfo);
2344 kcontrol->private_value = (long)c;
2345 mutex_unlock(&codec->control_mutex);
2346 return err;
2348 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_info);
2351 * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control
2353 * The control element is supposed to have the private_value field
2354 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2356 int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
2357 struct snd_ctl_elem_value *ucontrol)
2359 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2360 struct hda_bind_ctls *c;
2361 int err;
2363 mutex_lock(&codec->control_mutex);
2364 c = (struct hda_bind_ctls *)kcontrol->private_value;
2365 kcontrol->private_value = *c->values;
2366 err = c->ops->get(kcontrol, ucontrol);
2367 kcontrol->private_value = (long)c;
2368 mutex_unlock(&codec->control_mutex);
2369 return err;
2371 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_get);
2374 * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control
2376 * The control element is supposed to have the private_value field
2377 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2379 int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
2380 struct snd_ctl_elem_value *ucontrol)
2382 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2383 struct hda_bind_ctls *c;
2384 unsigned long *vals;
2385 int err = 0, change = 0;
2387 mutex_lock(&codec->control_mutex);
2388 c = (struct hda_bind_ctls *)kcontrol->private_value;
2389 for (vals = c->values; *vals; vals++) {
2390 kcontrol->private_value = *vals;
2391 err = c->ops->put(kcontrol, ucontrol);
2392 if (err < 0)
2393 break;
2394 change |= err;
2396 kcontrol->private_value = (long)c;
2397 mutex_unlock(&codec->control_mutex);
2398 return err < 0 ? err : change;
2400 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_put);
2403 * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control
2405 * The control element is supposed to have the private_value field
2406 * set up via HDA_BIND_VOL() macro.
2408 int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2409 unsigned int size, unsigned int __user *tlv)
2411 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2412 struct hda_bind_ctls *c;
2413 int err;
2415 mutex_lock(&codec->control_mutex);
2416 c = (struct hda_bind_ctls *)kcontrol->private_value;
2417 kcontrol->private_value = *c->values;
2418 err = c->ops->tlv(kcontrol, op_flag, size, tlv);
2419 kcontrol->private_value = (long)c;
2420 mutex_unlock(&codec->control_mutex);
2421 return err;
2423 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_tlv);
2425 struct hda_ctl_ops snd_hda_bind_vol = {
2426 .info = snd_hda_mixer_amp_volume_info,
2427 .get = snd_hda_mixer_amp_volume_get,
2428 .put = snd_hda_mixer_amp_volume_put,
2429 .tlv = snd_hda_mixer_amp_tlv
2431 EXPORT_SYMBOL_HDA(snd_hda_bind_vol);
2433 struct hda_ctl_ops snd_hda_bind_sw = {
2434 .info = snd_hda_mixer_amp_switch_info,
2435 .get = snd_hda_mixer_amp_switch_get,
2436 .put = snd_hda_mixer_amp_switch_put,
2437 .tlv = snd_hda_mixer_amp_tlv
2439 EXPORT_SYMBOL_HDA(snd_hda_bind_sw);
2442 * SPDIF out controls
2445 static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
2446 struct snd_ctl_elem_info *uinfo)
2448 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
2449 uinfo->count = 1;
2450 return 0;
2453 static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
2454 struct snd_ctl_elem_value *ucontrol)
2456 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2457 IEC958_AES0_NONAUDIO |
2458 IEC958_AES0_CON_EMPHASIS_5015 |
2459 IEC958_AES0_CON_NOT_COPYRIGHT;
2460 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
2461 IEC958_AES1_CON_ORIGINAL;
2462 return 0;
2465 static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
2466 struct snd_ctl_elem_value *ucontrol)
2468 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2469 IEC958_AES0_NONAUDIO |
2470 IEC958_AES0_PRO_EMPHASIS_5015;
2471 return 0;
2474 static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
2475 struct snd_ctl_elem_value *ucontrol)
2477 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2479 ucontrol->value.iec958.status[0] = codec->spdif_status & 0xff;
2480 ucontrol->value.iec958.status[1] = (codec->spdif_status >> 8) & 0xff;
2481 ucontrol->value.iec958.status[2] = (codec->spdif_status >> 16) & 0xff;
2482 ucontrol->value.iec958.status[3] = (codec->spdif_status >> 24) & 0xff;
2484 return 0;
2487 /* convert from SPDIF status bits to HDA SPDIF bits
2488 * bit 0 (DigEn) is always set zero (to be filled later)
2490 static unsigned short convert_from_spdif_status(unsigned int sbits)
2492 unsigned short val = 0;
2494 if (sbits & IEC958_AES0_PROFESSIONAL)
2495 val |= AC_DIG1_PROFESSIONAL;
2496 if (sbits & IEC958_AES0_NONAUDIO)
2497 val |= AC_DIG1_NONAUDIO;
2498 if (sbits & IEC958_AES0_PROFESSIONAL) {
2499 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
2500 IEC958_AES0_PRO_EMPHASIS_5015)
2501 val |= AC_DIG1_EMPHASIS;
2502 } else {
2503 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
2504 IEC958_AES0_CON_EMPHASIS_5015)
2505 val |= AC_DIG1_EMPHASIS;
2506 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
2507 val |= AC_DIG1_COPYRIGHT;
2508 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
2509 val |= AC_DIG1_LEVEL;
2510 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
2512 return val;
2515 /* convert to SPDIF status bits from HDA SPDIF bits
2517 static unsigned int convert_to_spdif_status(unsigned short val)
2519 unsigned int sbits = 0;
2521 if (val & AC_DIG1_NONAUDIO)
2522 sbits |= IEC958_AES0_NONAUDIO;
2523 if (val & AC_DIG1_PROFESSIONAL)
2524 sbits |= IEC958_AES0_PROFESSIONAL;
2525 if (sbits & IEC958_AES0_PROFESSIONAL) {
2526 if (sbits & AC_DIG1_EMPHASIS)
2527 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
2528 } else {
2529 if (val & AC_DIG1_EMPHASIS)
2530 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
2531 if (!(val & AC_DIG1_COPYRIGHT))
2532 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
2533 if (val & AC_DIG1_LEVEL)
2534 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
2535 sbits |= val & (0x7f << 8);
2537 return sbits;
2540 /* set digital convert verbs both for the given NID and its slaves */
2541 static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
2542 int verb, int val)
2544 hda_nid_t *d;
2546 snd_hda_codec_write_cache(codec, nid, 0, verb, val);
2547 d = codec->slave_dig_outs;
2548 if (!d)
2549 return;
2550 for (; *d; d++)
2551 snd_hda_codec_write_cache(codec, *d, 0, verb, val);
2554 static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
2555 int dig1, int dig2)
2557 if (dig1 != -1)
2558 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1);
2559 if (dig2 != -1)
2560 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2);
2563 static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
2564 struct snd_ctl_elem_value *ucontrol)
2566 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2567 hda_nid_t nid = kcontrol->private_value;
2568 unsigned short val;
2569 int change;
2571 mutex_lock(&codec->spdif_mutex);
2572 codec->spdif_status = ucontrol->value.iec958.status[0] |
2573 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
2574 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
2575 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
2576 val = convert_from_spdif_status(codec->spdif_status);
2577 val |= codec->spdif_ctls & 1;
2578 change = codec->spdif_ctls != val;
2579 codec->spdif_ctls = val;
2581 if (change)
2582 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
2584 mutex_unlock(&codec->spdif_mutex);
2585 return change;
2588 #define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
2590 static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
2591 struct snd_ctl_elem_value *ucontrol)
2593 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2595 ucontrol->value.integer.value[0] = codec->spdif_ctls & AC_DIG1_ENABLE;
2596 return 0;
2599 static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
2600 struct snd_ctl_elem_value *ucontrol)
2602 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2603 hda_nid_t nid = kcontrol->private_value;
2604 unsigned short val;
2605 int change;
2607 mutex_lock(&codec->spdif_mutex);
2608 val = codec->spdif_ctls & ~AC_DIG1_ENABLE;
2609 if (ucontrol->value.integer.value[0])
2610 val |= AC_DIG1_ENABLE;
2611 change = codec->spdif_ctls != val;
2612 if (change) {
2613 codec->spdif_ctls = val;
2614 set_dig_out_convert(codec, nid, val & 0xff, -1);
2615 /* unmute amp switch (if any) */
2616 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
2617 (val & AC_DIG1_ENABLE))
2618 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
2619 HDA_AMP_MUTE, 0);
2621 mutex_unlock(&codec->spdif_mutex);
2622 return change;
2625 static struct snd_kcontrol_new dig_mixes[] = {
2627 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2628 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2629 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
2630 .info = snd_hda_spdif_mask_info,
2631 .get = snd_hda_spdif_cmask_get,
2634 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2635 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2636 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK),
2637 .info = snd_hda_spdif_mask_info,
2638 .get = snd_hda_spdif_pmask_get,
2641 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2642 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
2643 .info = snd_hda_spdif_mask_info,
2644 .get = snd_hda_spdif_default_get,
2645 .put = snd_hda_spdif_default_put,
2648 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2649 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
2650 .info = snd_hda_spdif_out_switch_info,
2651 .get = snd_hda_spdif_out_switch_get,
2652 .put = snd_hda_spdif_out_switch_put,
2654 { } /* end */
2657 #define SPDIF_MAX_IDX 4 /* 4 instances should be enough to probe */
2660 * snd_hda_create_spdif_out_ctls - create Output SPDIF-related controls
2661 * @codec: the HDA codec
2662 * @nid: audio out widget NID
2664 * Creates controls related with the SPDIF output.
2665 * Called from each patch supporting the SPDIF out.
2667 * Returns 0 if successful, or a negative error code.
2669 int snd_hda_create_spdif_out_ctls(struct hda_codec *codec, hda_nid_t nid)
2671 int err;
2672 struct snd_kcontrol *kctl;
2673 struct snd_kcontrol_new *dig_mix;
2674 int idx;
2676 for (idx = 0; idx < SPDIF_MAX_IDX; idx++) {
2677 if (!_snd_hda_find_mixer_ctl(codec, "IEC958 Playback Switch",
2678 idx))
2679 break;
2681 if (idx >= SPDIF_MAX_IDX) {
2682 printk(KERN_ERR "hda_codec: too many IEC958 outputs\n");
2683 return -EBUSY;
2685 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
2686 kctl = snd_ctl_new1(dig_mix, codec);
2687 if (!kctl)
2688 return -ENOMEM;
2689 kctl->id.index = idx;
2690 kctl->private_value = nid;
2691 err = snd_hda_ctl_add(codec, nid, kctl);
2692 if (err < 0)
2693 return err;
2695 codec->spdif_ctls =
2696 snd_hda_codec_read(codec, nid, 0,
2697 AC_VERB_GET_DIGI_CONVERT_1, 0);
2698 codec->spdif_status = convert_to_spdif_status(codec->spdif_ctls);
2699 return 0;
2701 EXPORT_SYMBOL_HDA(snd_hda_create_spdif_out_ctls);
2704 * SPDIF sharing with analog output
2706 static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
2707 struct snd_ctl_elem_value *ucontrol)
2709 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
2710 ucontrol->value.integer.value[0] = mout->share_spdif;
2711 return 0;
2714 static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
2715 struct snd_ctl_elem_value *ucontrol)
2717 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
2718 mout->share_spdif = !!ucontrol->value.integer.value[0];
2719 return 0;
2722 static struct snd_kcontrol_new spdif_share_sw = {
2723 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2724 .name = "IEC958 Default PCM Playback Switch",
2725 .info = snd_ctl_boolean_mono_info,
2726 .get = spdif_share_sw_get,
2727 .put = spdif_share_sw_put,
2731 * snd_hda_create_spdif_share_sw - create Default PCM switch
2732 * @codec: the HDA codec
2733 * @mout: multi-out instance
2735 int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
2736 struct hda_multi_out *mout)
2738 if (!mout->dig_out_nid)
2739 return 0;
2740 /* ATTENTION: here mout is passed as private_data, instead of codec */
2741 return snd_hda_ctl_add(codec, mout->dig_out_nid,
2742 snd_ctl_new1(&spdif_share_sw, mout));
2744 EXPORT_SYMBOL_HDA(snd_hda_create_spdif_share_sw);
2747 * SPDIF input
2750 #define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
2752 static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
2753 struct snd_ctl_elem_value *ucontrol)
2755 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2757 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
2758 return 0;
2761 static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
2762 struct snd_ctl_elem_value *ucontrol)
2764 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2765 hda_nid_t nid = kcontrol->private_value;
2766 unsigned int val = !!ucontrol->value.integer.value[0];
2767 int change;
2769 mutex_lock(&codec->spdif_mutex);
2770 change = codec->spdif_in_enable != val;
2771 if (change) {
2772 codec->spdif_in_enable = val;
2773 snd_hda_codec_write_cache(codec, nid, 0,
2774 AC_VERB_SET_DIGI_CONVERT_1, val);
2776 mutex_unlock(&codec->spdif_mutex);
2777 return change;
2780 static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
2781 struct snd_ctl_elem_value *ucontrol)
2783 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2784 hda_nid_t nid = kcontrol->private_value;
2785 unsigned short val;
2786 unsigned int sbits;
2788 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0);
2789 sbits = convert_to_spdif_status(val);
2790 ucontrol->value.iec958.status[0] = sbits;
2791 ucontrol->value.iec958.status[1] = sbits >> 8;
2792 ucontrol->value.iec958.status[2] = sbits >> 16;
2793 ucontrol->value.iec958.status[3] = sbits >> 24;
2794 return 0;
2797 static struct snd_kcontrol_new dig_in_ctls[] = {
2799 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2800 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH),
2801 .info = snd_hda_spdif_in_switch_info,
2802 .get = snd_hda_spdif_in_switch_get,
2803 .put = snd_hda_spdif_in_switch_put,
2806 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2807 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2808 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
2809 .info = snd_hda_spdif_mask_info,
2810 .get = snd_hda_spdif_in_status_get,
2812 { } /* end */
2816 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
2817 * @codec: the HDA codec
2818 * @nid: audio in widget NID
2820 * Creates controls related with the SPDIF input.
2821 * Called from each patch supporting the SPDIF in.
2823 * Returns 0 if successful, or a negative error code.
2825 int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
2827 int err;
2828 struct snd_kcontrol *kctl;
2829 struct snd_kcontrol_new *dig_mix;
2830 int idx;
2832 for (idx = 0; idx < SPDIF_MAX_IDX; idx++) {
2833 if (!_snd_hda_find_mixer_ctl(codec, "IEC958 Capture Switch",
2834 idx))
2835 break;
2837 if (idx >= SPDIF_MAX_IDX) {
2838 printk(KERN_ERR "hda_codec: too many IEC958 inputs\n");
2839 return -EBUSY;
2841 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
2842 kctl = snd_ctl_new1(dig_mix, codec);
2843 if (!kctl)
2844 return -ENOMEM;
2845 kctl->private_value = nid;
2846 err = snd_hda_ctl_add(codec, nid, kctl);
2847 if (err < 0)
2848 return err;
2850 codec->spdif_in_enable =
2851 snd_hda_codec_read(codec, nid, 0,
2852 AC_VERB_GET_DIGI_CONVERT_1, 0) &
2853 AC_DIG1_ENABLE;
2854 return 0;
2856 EXPORT_SYMBOL_HDA(snd_hda_create_spdif_in_ctls);
2858 #ifdef SND_HDA_NEEDS_RESUME
2860 * command cache
2863 /* build a 32bit cache key with the widget id and the command parameter */
2864 #define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
2865 #define get_cmd_cache_nid(key) ((key) & 0xff)
2866 #define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
2869 * snd_hda_codec_write_cache - send a single command with caching
2870 * @codec: the HDA codec
2871 * @nid: NID to send the command
2872 * @direct: direct flag
2873 * @verb: the verb to send
2874 * @parm: the parameter for the verb
2876 * Send a single command without waiting for response.
2878 * Returns 0 if successful, or a negative error code.
2880 int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
2881 int direct, unsigned int verb, unsigned int parm)
2883 int err = snd_hda_codec_write(codec, nid, direct, verb, parm);
2884 struct hda_cache_head *c;
2885 u32 key;
2887 if (err < 0)
2888 return err;
2889 /* parm may contain the verb stuff for get/set amp */
2890 verb = verb | (parm >> 8);
2891 parm &= 0xff;
2892 key = build_cmd_cache_key(nid, verb);
2893 mutex_lock(&codec->bus->cmd_mutex);
2894 c = get_alloc_hash(&codec->cmd_cache, key);
2895 if (c)
2896 c->val = parm;
2897 mutex_unlock(&codec->bus->cmd_mutex);
2898 return 0;
2900 EXPORT_SYMBOL_HDA(snd_hda_codec_write_cache);
2903 * snd_hda_codec_update_cache - check cache and write the cmd only when needed
2904 * @codec: the HDA codec
2905 * @nid: NID to send the command
2906 * @direct: direct flag
2907 * @verb: the verb to send
2908 * @parm: the parameter for the verb
2910 * This function works like snd_hda_codec_write_cache(), but it doesn't send
2911 * command if the parameter is already identical with the cached value.
2912 * If not, it sends the command and refreshes the cache.
2914 * Returns 0 if successful, or a negative error code.
2916 int snd_hda_codec_update_cache(struct hda_codec *codec, hda_nid_t nid,
2917 int direct, unsigned int verb, unsigned int parm)
2919 struct hda_cache_head *c;
2920 u32 key;
2922 /* parm may contain the verb stuff for get/set amp */
2923 verb = verb | (parm >> 8);
2924 parm &= 0xff;
2925 key = build_cmd_cache_key(nid, verb);
2926 mutex_lock(&codec->bus->cmd_mutex);
2927 c = get_hash(&codec->cmd_cache, key);
2928 if (c && c->val == parm) {
2929 mutex_unlock(&codec->bus->cmd_mutex);
2930 return 0;
2932 mutex_unlock(&codec->bus->cmd_mutex);
2933 return snd_hda_codec_write_cache(codec, nid, direct, verb, parm);
2935 EXPORT_SYMBOL_HDA(snd_hda_codec_update_cache);
2938 * snd_hda_codec_resume_cache - Resume the all commands from the cache
2939 * @codec: HD-audio codec
2941 * Execute all verbs recorded in the command caches to resume.
2943 void snd_hda_codec_resume_cache(struct hda_codec *codec)
2945 struct hda_cache_head *buffer = codec->cmd_cache.buf.list;
2946 int i;
2948 for (i = 0; i < codec->cmd_cache.buf.used; i++, buffer++) {
2949 u32 key = buffer->key;
2950 if (!key)
2951 continue;
2952 snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
2953 get_cmd_cache_cmd(key), buffer->val);
2956 EXPORT_SYMBOL_HDA(snd_hda_codec_resume_cache);
2959 * snd_hda_sequence_write_cache - sequence writes with caching
2960 * @codec: the HDA codec
2961 * @seq: VERB array to send
2963 * Send the commands sequentially from the given array.
2964 * Thte commands are recorded on cache for power-save and resume.
2965 * The array must be terminated with NID=0.
2967 void snd_hda_sequence_write_cache(struct hda_codec *codec,
2968 const struct hda_verb *seq)
2970 for (; seq->nid; seq++)
2971 snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
2972 seq->param);
2974 EXPORT_SYMBOL_HDA(snd_hda_sequence_write_cache);
2975 #endif /* SND_HDA_NEEDS_RESUME */
2978 * set power state of the codec
2980 static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
2981 unsigned int power_state)
2983 hda_nid_t nid;
2984 int i;
2986 /* this delay seems necessary to avoid click noise at power-down */
2987 if (power_state == AC_PWRST_D3)
2988 msleep(100);
2989 snd_hda_codec_read(codec, fg, 0, AC_VERB_SET_POWER_STATE,
2990 power_state);
2991 /* partial workaround for "azx_get_response timeout" */
2992 if (power_state == AC_PWRST_D0 &&
2993 (codec->vendor_id & 0xffff0000) == 0x14f10000)
2994 msleep(10);
2996 nid = codec->start_nid;
2997 for (i = 0; i < codec->num_nodes; i++, nid++) {
2998 unsigned int wcaps = get_wcaps(codec, nid);
2999 if (wcaps & AC_WCAP_POWER) {
3000 unsigned int wid_type = get_wcaps_type(wcaps);
3001 if (power_state == AC_PWRST_D3 &&
3002 wid_type == AC_WID_PIN) {
3003 unsigned int pincap;
3005 * don't power down the widget if it controls
3006 * eapd and EAPD_BTLENABLE is set.
3008 pincap = snd_hda_query_pin_caps(codec, nid);
3009 if (pincap & AC_PINCAP_EAPD) {
3010 int eapd = snd_hda_codec_read(codec,
3011 nid, 0,
3012 AC_VERB_GET_EAPD_BTLENABLE, 0);
3013 eapd &= 0x02;
3014 if (eapd)
3015 continue;
3018 snd_hda_codec_write(codec, nid, 0,
3019 AC_VERB_SET_POWER_STATE,
3020 power_state);
3024 if (power_state == AC_PWRST_D0) {
3025 unsigned long end_time;
3026 int state;
3027 /* wait until the codec reachs to D0 */
3028 end_time = jiffies + msecs_to_jiffies(500);
3029 do {
3030 state = snd_hda_codec_read(codec, fg, 0,
3031 AC_VERB_GET_POWER_STATE, 0);
3032 if (state == power_state)
3033 break;
3034 msleep(1);
3035 } while (time_after_eq(end_time, jiffies));
3039 #ifdef CONFIG_SND_HDA_HWDEP
3040 /* execute additional init verbs */
3041 static void hda_exec_init_verbs(struct hda_codec *codec)
3043 if (codec->init_verbs.list)
3044 snd_hda_sequence_write(codec, codec->init_verbs.list);
3046 #else
3047 static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
3048 #endif
3050 #ifdef SND_HDA_NEEDS_RESUME
3052 * call suspend and power-down; used both from PM and power-save
3054 static void hda_call_codec_suspend(struct hda_codec *codec)
3056 if (codec->patch_ops.suspend)
3057 codec->patch_ops.suspend(codec, PMSG_SUSPEND);
3058 hda_cleanup_all_streams(codec);
3059 hda_set_power_state(codec,
3060 codec->afg ? codec->afg : codec->mfg,
3061 AC_PWRST_D3);
3062 #ifdef CONFIG_SND_HDA_POWER_SAVE
3063 snd_hda_update_power_acct(codec);
3064 cancel_delayed_work(&codec->power_work);
3065 codec->power_on = 0;
3066 codec->power_transition = 0;
3067 codec->power_jiffies = jiffies;
3068 #endif
3072 * kick up codec; used both from PM and power-save
3074 static void hda_call_codec_resume(struct hda_codec *codec)
3076 hda_set_power_state(codec,
3077 codec->afg ? codec->afg : codec->mfg,
3078 AC_PWRST_D0);
3079 restore_pincfgs(codec); /* restore all current pin configs */
3080 restore_shutup_pins(codec);
3081 hda_exec_init_verbs(codec);
3082 if (codec->patch_ops.resume)
3083 codec->patch_ops.resume(codec);
3084 else {
3085 if (codec->patch_ops.init)
3086 codec->patch_ops.init(codec);
3087 snd_hda_codec_resume_amp(codec);
3088 snd_hda_codec_resume_cache(codec);
3091 #endif /* SND_HDA_NEEDS_RESUME */
3095 * snd_hda_build_controls - build mixer controls
3096 * @bus: the BUS
3098 * Creates mixer controls for each codec included in the bus.
3100 * Returns 0 if successful, otherwise a negative error code.
3102 int /*__devinit*/ snd_hda_build_controls(struct hda_bus *bus)
3104 struct hda_codec *codec;
3106 list_for_each_entry(codec, &bus->codec_list, list) {
3107 int err = snd_hda_codec_build_controls(codec);
3108 if (err < 0) {
3109 printk(KERN_ERR "hda_codec: cannot build controls "
3110 "for #%d (error %d)\n", codec->addr, err);
3111 err = snd_hda_codec_reset(codec);
3112 if (err < 0) {
3113 printk(KERN_ERR
3114 "hda_codec: cannot revert codec\n");
3115 return err;
3119 return 0;
3121 EXPORT_SYMBOL_HDA(snd_hda_build_controls);
3123 int snd_hda_codec_build_controls(struct hda_codec *codec)
3125 int err = 0;
3126 hda_exec_init_verbs(codec);
3127 /* continue to initialize... */
3128 if (codec->patch_ops.init)
3129 err = codec->patch_ops.init(codec);
3130 if (!err && codec->patch_ops.build_controls)
3131 err = codec->patch_ops.build_controls(codec);
3132 if (err < 0)
3133 return err;
3134 return 0;
3138 * stream formats
3140 struct hda_rate_tbl {
3141 unsigned int hz;
3142 unsigned int alsa_bits;
3143 unsigned int hda_fmt;
3146 /* rate = base * mult / div */
3147 #define HDA_RATE(base, mult, div) \
3148 (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \
3149 (((div) - 1) << AC_FMT_DIV_SHIFT))
3151 static struct hda_rate_tbl rate_bits[] = {
3152 /* rate in Hz, ALSA rate bitmask, HDA format value */
3154 /* autodetected value used in snd_hda_query_supported_pcm */
3155 { 8000, SNDRV_PCM_RATE_8000, HDA_RATE(48, 1, 6) },
3156 { 11025, SNDRV_PCM_RATE_11025, HDA_RATE(44, 1, 4) },
3157 { 16000, SNDRV_PCM_RATE_16000, HDA_RATE(48, 1, 3) },
3158 { 22050, SNDRV_PCM_RATE_22050, HDA_RATE(44, 1, 2) },
3159 { 32000, SNDRV_PCM_RATE_32000, HDA_RATE(48, 2, 3) },
3160 { 44100, SNDRV_PCM_RATE_44100, HDA_RATE(44, 1, 1) },
3161 { 48000, SNDRV_PCM_RATE_48000, HDA_RATE(48, 1, 1) },
3162 { 88200, SNDRV_PCM_RATE_88200, HDA_RATE(44, 2, 1) },
3163 { 96000, SNDRV_PCM_RATE_96000, HDA_RATE(48, 2, 1) },
3164 { 176400, SNDRV_PCM_RATE_176400, HDA_RATE(44, 4, 1) },
3165 { 192000, SNDRV_PCM_RATE_192000, HDA_RATE(48, 4, 1) },
3166 #define AC_PAR_PCM_RATE_BITS 11
3167 /* up to bits 10, 384kHZ isn't supported properly */
3169 /* not autodetected value */
3170 { 9600, SNDRV_PCM_RATE_KNOT, HDA_RATE(48, 1, 5) },
3172 { 0 } /* terminator */
3176 * snd_hda_calc_stream_format - calculate format bitset
3177 * @rate: the sample rate
3178 * @channels: the number of channels
3179 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
3180 * @maxbps: the max. bps
3182 * Calculate the format bitset from the given rate, channels and th PCM format.
3184 * Return zero if invalid.
3186 unsigned int snd_hda_calc_stream_format(unsigned int rate,
3187 unsigned int channels,
3188 unsigned int format,
3189 unsigned int maxbps,
3190 unsigned short spdif_ctls)
3192 int i;
3193 unsigned int val = 0;
3195 for (i = 0; rate_bits[i].hz; i++)
3196 if (rate_bits[i].hz == rate) {
3197 val = rate_bits[i].hda_fmt;
3198 break;
3200 if (!rate_bits[i].hz) {
3201 snd_printdd("invalid rate %d\n", rate);
3202 return 0;
3205 if (channels == 0 || channels > 8) {
3206 snd_printdd("invalid channels %d\n", channels);
3207 return 0;
3209 val |= channels - 1;
3211 switch (snd_pcm_format_width(format)) {
3212 case 8:
3213 val |= AC_FMT_BITS_8;
3214 break;
3215 case 16:
3216 val |= AC_FMT_BITS_16;
3217 break;
3218 case 20:
3219 case 24:
3220 case 32:
3221 if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE)
3222 val |= AC_FMT_BITS_32;
3223 else if (maxbps >= 24)
3224 val |= AC_FMT_BITS_24;
3225 else
3226 val |= AC_FMT_BITS_20;
3227 break;
3228 default:
3229 snd_printdd("invalid format width %d\n",
3230 snd_pcm_format_width(format));
3231 return 0;
3234 if (spdif_ctls & AC_DIG1_NONAUDIO)
3235 val |= AC_FMT_TYPE_NON_PCM;
3237 return val;
3239 EXPORT_SYMBOL_HDA(snd_hda_calc_stream_format);
3241 static unsigned int get_pcm_param(struct hda_codec *codec, hda_nid_t nid)
3243 unsigned int val = 0;
3244 if (nid != codec->afg &&
3245 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD))
3246 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
3247 if (!val || val == -1)
3248 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
3249 if (!val || val == -1)
3250 return 0;
3251 return val;
3254 static unsigned int query_pcm_param(struct hda_codec *codec, hda_nid_t nid)
3256 return query_caps_hash(codec, nid, HDA_HASH_PARPCM_KEY(nid),
3257 get_pcm_param);
3260 static unsigned int get_stream_param(struct hda_codec *codec, hda_nid_t nid)
3262 unsigned int streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
3263 if (!streams || streams == -1)
3264 streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
3265 if (!streams || streams == -1)
3266 return 0;
3267 return streams;
3270 static unsigned int query_stream_param(struct hda_codec *codec, hda_nid_t nid)
3272 return query_caps_hash(codec, nid, HDA_HASH_PARSTR_KEY(nid),
3273 get_stream_param);
3277 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
3278 * @codec: the HDA codec
3279 * @nid: NID to query
3280 * @ratesp: the pointer to store the detected rate bitflags
3281 * @formatsp: the pointer to store the detected formats
3282 * @bpsp: the pointer to store the detected format widths
3284 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
3285 * or @bsps argument is ignored.
3287 * Returns 0 if successful, otherwise a negative error code.
3289 static int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
3290 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
3292 unsigned int i, val, wcaps;
3294 wcaps = get_wcaps(codec, nid);
3295 val = query_pcm_param(codec, nid);
3297 if (ratesp) {
3298 u32 rates = 0;
3299 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
3300 if (val & (1 << i))
3301 rates |= rate_bits[i].alsa_bits;
3303 if (rates == 0) {
3304 snd_printk(KERN_ERR "hda_codec: rates == 0 "
3305 "(nid=0x%x, val=0x%x, ovrd=%i)\n",
3306 nid, val,
3307 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0);
3308 return -EIO;
3310 *ratesp = rates;
3313 if (formatsp || bpsp) {
3314 u64 formats = 0;
3315 unsigned int streams, bps;
3317 streams = query_stream_param(codec, nid);
3318 if (!streams)
3319 return -EIO;
3321 bps = 0;
3322 if (streams & AC_SUPFMT_PCM) {
3323 if (val & AC_SUPPCM_BITS_8) {
3324 formats |= SNDRV_PCM_FMTBIT_U8;
3325 bps = 8;
3327 if (val & AC_SUPPCM_BITS_16) {
3328 formats |= SNDRV_PCM_FMTBIT_S16_LE;
3329 bps = 16;
3331 if (wcaps & AC_WCAP_DIGITAL) {
3332 if (val & AC_SUPPCM_BITS_32)
3333 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
3334 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
3335 formats |= SNDRV_PCM_FMTBIT_S32_LE;
3336 if (val & AC_SUPPCM_BITS_24)
3337 bps = 24;
3338 else if (val & AC_SUPPCM_BITS_20)
3339 bps = 20;
3340 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
3341 AC_SUPPCM_BITS_32)) {
3342 formats |= SNDRV_PCM_FMTBIT_S32_LE;
3343 if (val & AC_SUPPCM_BITS_32)
3344 bps = 32;
3345 else if (val & AC_SUPPCM_BITS_24)
3346 bps = 24;
3347 else if (val & AC_SUPPCM_BITS_20)
3348 bps = 20;
3351 if (streams & AC_SUPFMT_FLOAT32) {
3352 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
3353 if (!bps)
3354 bps = 32;
3356 if (streams == AC_SUPFMT_AC3) {
3357 /* should be exclusive */
3358 /* temporary hack: we have still no proper support
3359 * for the direct AC3 stream...
3361 formats |= SNDRV_PCM_FMTBIT_U8;
3362 bps = 8;
3364 if (formats == 0) {
3365 snd_printk(KERN_ERR "hda_codec: formats == 0 "
3366 "(nid=0x%x, val=0x%x, ovrd=%i, "
3367 "streams=0x%x)\n",
3368 nid, val,
3369 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0,
3370 streams);
3371 return -EIO;
3373 if (formatsp)
3374 *formatsp = formats;
3375 if (bpsp)
3376 *bpsp = bps;
3379 return 0;
3383 * snd_hda_is_supported_format - Check the validity of the format
3384 * @codec: HD-audio codec
3385 * @nid: NID to check
3386 * @format: the HD-audio format value to check
3388 * Check whether the given node supports the format value.
3390 * Returns 1 if supported, 0 if not.
3392 int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
3393 unsigned int format)
3395 int i;
3396 unsigned int val = 0, rate, stream;
3398 val = query_pcm_param(codec, nid);
3399 if (!val)
3400 return 0;
3402 rate = format & 0xff00;
3403 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
3404 if (rate_bits[i].hda_fmt == rate) {
3405 if (val & (1 << i))
3406 break;
3407 return 0;
3409 if (i >= AC_PAR_PCM_RATE_BITS)
3410 return 0;
3412 stream = query_stream_param(codec, nid);
3413 if (!stream)
3414 return 0;
3416 if (stream & AC_SUPFMT_PCM) {
3417 switch (format & 0xf0) {
3418 case 0x00:
3419 if (!(val & AC_SUPPCM_BITS_8))
3420 return 0;
3421 break;
3422 case 0x10:
3423 if (!(val & AC_SUPPCM_BITS_16))
3424 return 0;
3425 break;
3426 case 0x20:
3427 if (!(val & AC_SUPPCM_BITS_20))
3428 return 0;
3429 break;
3430 case 0x30:
3431 if (!(val & AC_SUPPCM_BITS_24))
3432 return 0;
3433 break;
3434 case 0x40:
3435 if (!(val & AC_SUPPCM_BITS_32))
3436 return 0;
3437 break;
3438 default:
3439 return 0;
3441 } else {
3442 /* FIXME: check for float32 and AC3? */
3445 return 1;
3447 EXPORT_SYMBOL_HDA(snd_hda_is_supported_format);
3450 * PCM stuff
3452 static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
3453 struct hda_codec *codec,
3454 struct snd_pcm_substream *substream)
3456 return 0;
3459 static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
3460 struct hda_codec *codec,
3461 unsigned int stream_tag,
3462 unsigned int format,
3463 struct snd_pcm_substream *substream)
3465 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
3466 return 0;
3469 static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
3470 struct hda_codec *codec,
3471 struct snd_pcm_substream *substream)
3473 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
3474 return 0;
3477 static int set_pcm_default_values(struct hda_codec *codec,
3478 struct hda_pcm_stream *info)
3480 int err;
3482 /* query support PCM information from the given NID */
3483 if (info->nid && (!info->rates || !info->formats)) {
3484 err = snd_hda_query_supported_pcm(codec, info->nid,
3485 info->rates ? NULL : &info->rates,
3486 info->formats ? NULL : &info->formats,
3487 info->maxbps ? NULL : &info->maxbps);
3488 if (err < 0)
3489 return err;
3491 if (info->ops.open == NULL)
3492 info->ops.open = hda_pcm_default_open_close;
3493 if (info->ops.close == NULL)
3494 info->ops.close = hda_pcm_default_open_close;
3495 if (info->ops.prepare == NULL) {
3496 if (snd_BUG_ON(!info->nid))
3497 return -EINVAL;
3498 info->ops.prepare = hda_pcm_default_prepare;
3500 if (info->ops.cleanup == NULL) {
3501 if (snd_BUG_ON(!info->nid))
3502 return -EINVAL;
3503 info->ops.cleanup = hda_pcm_default_cleanup;
3505 return 0;
3509 * codec prepare/cleanup entries
3511 int snd_hda_codec_prepare(struct hda_codec *codec,
3512 struct hda_pcm_stream *hinfo,
3513 unsigned int stream,
3514 unsigned int format,
3515 struct snd_pcm_substream *substream)
3517 int ret;
3518 mutex_lock(&codec->bus->prepare_mutex);
3519 ret = hinfo->ops.prepare(hinfo, codec, stream, format, substream);
3520 if (ret >= 0)
3521 purify_inactive_streams(codec);
3522 mutex_unlock(&codec->bus->prepare_mutex);
3523 return ret;
3525 EXPORT_SYMBOL_HDA(snd_hda_codec_prepare);
3527 void snd_hda_codec_cleanup(struct hda_codec *codec,
3528 struct hda_pcm_stream *hinfo,
3529 struct snd_pcm_substream *substream)
3531 mutex_lock(&codec->bus->prepare_mutex);
3532 hinfo->ops.cleanup(hinfo, codec, substream);
3533 mutex_unlock(&codec->bus->prepare_mutex);
3535 EXPORT_SYMBOL_HDA(snd_hda_codec_cleanup);
3537 /* global */
3538 const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = {
3539 "Audio", "SPDIF", "HDMI", "Modem"
3543 * get the empty PCM device number to assign
3545 * note the max device number is limited by HDA_MAX_PCMS, currently 10
3547 static int get_empty_pcm_device(struct hda_bus *bus, int type)
3549 /* audio device indices; not linear to keep compatibility */
3550 static int audio_idx[HDA_PCM_NTYPES][5] = {
3551 [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 },
3552 [HDA_PCM_TYPE_SPDIF] = { 1, -1 },
3553 [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 },
3554 [HDA_PCM_TYPE_MODEM] = { 6, -1 },
3556 int i;
3558 if (type >= HDA_PCM_NTYPES) {
3559 snd_printk(KERN_WARNING "Invalid PCM type %d\n", type);
3560 return -EINVAL;
3563 for (i = 0; audio_idx[type][i] >= 0 ; i++)
3564 if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits))
3565 return audio_idx[type][i];
3567 snd_printk(KERN_WARNING "Too many %s devices\n",
3568 snd_hda_pcm_type_name[type]);
3569 return -EAGAIN;
3573 * attach a new PCM stream
3575 static int snd_hda_attach_pcm(struct hda_codec *codec, struct hda_pcm *pcm)
3577 struct hda_bus *bus = codec->bus;
3578 struct hda_pcm_stream *info;
3579 int stream, err;
3581 if (snd_BUG_ON(!pcm->name))
3582 return -EINVAL;
3583 for (stream = 0; stream < 2; stream++) {
3584 info = &pcm->stream[stream];
3585 if (info->substreams) {
3586 err = set_pcm_default_values(codec, info);
3587 if (err < 0)
3588 return err;
3591 return bus->ops.attach_pcm(bus, codec, pcm);
3594 /* assign all PCMs of the given codec */
3595 int snd_hda_codec_build_pcms(struct hda_codec *codec)
3597 unsigned int pcm;
3598 int err;
3600 if (!codec->num_pcms) {
3601 if (!codec->patch_ops.build_pcms)
3602 return 0;
3603 err = codec->patch_ops.build_pcms(codec);
3604 if (err < 0) {
3605 printk(KERN_ERR "hda_codec: cannot build PCMs"
3606 "for #%d (error %d)\n", codec->addr, err);
3607 err = snd_hda_codec_reset(codec);
3608 if (err < 0) {
3609 printk(KERN_ERR
3610 "hda_codec: cannot revert codec\n");
3611 return err;
3615 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
3616 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
3617 int dev;
3619 if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
3620 continue; /* no substreams assigned */
3622 if (!cpcm->pcm) {
3623 dev = get_empty_pcm_device(codec->bus, cpcm->pcm_type);
3624 if (dev < 0)
3625 continue; /* no fatal error */
3626 cpcm->device = dev;
3627 err = snd_hda_attach_pcm(codec, cpcm);
3628 if (err < 0) {
3629 printk(KERN_ERR "hda_codec: cannot attach "
3630 "PCM stream %d for codec #%d\n",
3631 dev, codec->addr);
3632 continue; /* no fatal error */
3636 return 0;
3640 * snd_hda_build_pcms - build PCM information
3641 * @bus: the BUS
3643 * Create PCM information for each codec included in the bus.
3645 * The build_pcms codec patch is requested to set up codec->num_pcms and
3646 * codec->pcm_info properly. The array is referred by the top-level driver
3647 * to create its PCM instances.
3648 * The allocated codec->pcm_info should be released in codec->patch_ops.free
3649 * callback.
3651 * At least, substreams, channels_min and channels_max must be filled for
3652 * each stream. substreams = 0 indicates that the stream doesn't exist.
3653 * When rates and/or formats are zero, the supported values are queried
3654 * from the given nid. The nid is used also by the default ops.prepare
3655 * and ops.cleanup callbacks.
3657 * The driver needs to call ops.open in its open callback. Similarly,
3658 * ops.close is supposed to be called in the close callback.
3659 * ops.prepare should be called in the prepare or hw_params callback
3660 * with the proper parameters for set up.
3661 * ops.cleanup should be called in hw_free for clean up of streams.
3663 * This function returns 0 if successfull, or a negative error code.
3665 int __devinit snd_hda_build_pcms(struct hda_bus *bus)
3667 struct hda_codec *codec;
3669 list_for_each_entry(codec, &bus->codec_list, list) {
3670 int err = snd_hda_codec_build_pcms(codec);
3671 if (err < 0)
3672 return err;
3674 return 0;
3676 EXPORT_SYMBOL_HDA(snd_hda_build_pcms);
3679 * snd_hda_check_board_config - compare the current codec with the config table
3680 * @codec: the HDA codec
3681 * @num_configs: number of config enums
3682 * @models: array of model name strings
3683 * @tbl: configuration table, terminated by null entries
3685 * Compares the modelname or PCI subsystem id of the current codec with the
3686 * given configuration table. If a matching entry is found, returns its
3687 * config value (supposed to be 0 or positive).
3689 * If no entries are matching, the function returns a negative value.
3691 int snd_hda_check_board_config(struct hda_codec *codec,
3692 int num_configs, const char **models,
3693 const struct snd_pci_quirk *tbl)
3695 if (codec->modelname && models) {
3696 int i;
3697 for (i = 0; i < num_configs; i++) {
3698 if (models[i] &&
3699 !strcmp(codec->modelname, models[i])) {
3700 snd_printd(KERN_INFO "hda_codec: model '%s' is "
3701 "selected\n", models[i]);
3702 return i;
3707 if (!codec->bus->pci || !tbl)
3708 return -1;
3710 tbl = snd_pci_quirk_lookup(codec->bus->pci, tbl);
3711 if (!tbl)
3712 return -1;
3713 if (tbl->value >= 0 && tbl->value < num_configs) {
3714 #ifdef CONFIG_SND_DEBUG_VERBOSE
3715 char tmp[10];
3716 const char *model = NULL;
3717 if (models)
3718 model = models[tbl->value];
3719 if (!model) {
3720 sprintf(tmp, "#%d", tbl->value);
3721 model = tmp;
3723 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
3724 "for config %x:%x (%s)\n",
3725 model, tbl->subvendor, tbl->subdevice,
3726 (tbl->name ? tbl->name : "Unknown device"));
3727 #endif
3728 return tbl->value;
3730 return -1;
3732 EXPORT_SYMBOL_HDA(snd_hda_check_board_config);
3735 * snd_hda_check_board_codec_sid_config - compare the current codec
3736 subsystem ID with the
3737 config table
3739 This is important for Gateway notebooks with SB450 HDA Audio
3740 where the vendor ID of the PCI device is:
3741 ATI Technologies Inc SB450 HDA Audio [1002:437b]
3742 and the vendor/subvendor are found only at the codec.
3744 * @codec: the HDA codec
3745 * @num_configs: number of config enums
3746 * @models: array of model name strings
3747 * @tbl: configuration table, terminated by null entries
3749 * Compares the modelname or PCI subsystem id of the current codec with the
3750 * given configuration table. If a matching entry is found, returns its
3751 * config value (supposed to be 0 or positive).
3753 * If no entries are matching, the function returns a negative value.
3755 int snd_hda_check_board_codec_sid_config(struct hda_codec *codec,
3756 int num_configs, const char **models,
3757 const struct snd_pci_quirk *tbl)
3759 const struct snd_pci_quirk *q;
3761 /* Search for codec ID */
3762 for (q = tbl; q->subvendor; q++) {
3763 unsigned long vendorid = (q->subdevice) | (q->subvendor << 16);
3765 if (vendorid == codec->subsystem_id)
3766 break;
3769 if (!q->subvendor)
3770 return -1;
3772 tbl = q;
3774 if (tbl->value >= 0 && tbl->value < num_configs) {
3775 #ifdef CONFIG_SND_DEBUG_VERBOSE
3776 char tmp[10];
3777 const char *model = NULL;
3778 if (models)
3779 model = models[tbl->value];
3780 if (!model) {
3781 sprintf(tmp, "#%d", tbl->value);
3782 model = tmp;
3784 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
3785 "for config %x:%x (%s)\n",
3786 model, tbl->subvendor, tbl->subdevice,
3787 (tbl->name ? tbl->name : "Unknown device"));
3788 #endif
3789 return tbl->value;
3791 return -1;
3793 EXPORT_SYMBOL_HDA(snd_hda_check_board_codec_sid_config);
3796 * snd_hda_add_new_ctls - create controls from the array
3797 * @codec: the HDA codec
3798 * @knew: the array of struct snd_kcontrol_new
3800 * This helper function creates and add new controls in the given array.
3801 * The array must be terminated with an empty entry as terminator.
3803 * Returns 0 if successful, or a negative error code.
3805 int snd_hda_add_new_ctls(struct hda_codec *codec, struct snd_kcontrol_new *knew)
3807 int err;
3809 for (; knew->name; knew++) {
3810 struct snd_kcontrol *kctl;
3811 if (knew->iface == -1) /* skip this codec private value */
3812 continue;
3813 kctl = snd_ctl_new1(knew, codec);
3814 if (!kctl)
3815 return -ENOMEM;
3816 err = snd_hda_ctl_add(codec, 0, kctl);
3817 if (err < 0) {
3818 if (!codec->addr)
3819 return err;
3820 kctl = snd_ctl_new1(knew, codec);
3821 if (!kctl)
3822 return -ENOMEM;
3823 kctl->id.device = codec->addr;
3824 err = snd_hda_ctl_add(codec, 0, kctl);
3825 if (err < 0)
3826 return err;
3829 return 0;
3831 EXPORT_SYMBOL_HDA(snd_hda_add_new_ctls);
3833 #ifdef CONFIG_SND_HDA_POWER_SAVE
3834 static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
3835 unsigned int power_state);
3837 static void hda_power_work(struct work_struct *work)
3839 struct hda_codec *codec =
3840 container_of(work, struct hda_codec, power_work.work);
3841 struct hda_bus *bus = codec->bus;
3843 if (!codec->power_on || codec->power_count) {
3844 codec->power_transition = 0;
3845 return;
3848 hda_call_codec_suspend(codec);
3849 if (bus->ops.pm_notify)
3850 bus->ops.pm_notify(bus);
3853 static void hda_keep_power_on(struct hda_codec *codec)
3855 codec->power_count++;
3856 codec->power_on = 1;
3857 codec->power_jiffies = jiffies;
3860 /* update the power on/off account with the current jiffies */
3861 void snd_hda_update_power_acct(struct hda_codec *codec)
3863 unsigned long delta = jiffies - codec->power_jiffies;
3864 if (codec->power_on)
3865 codec->power_on_acct += delta;
3866 else
3867 codec->power_off_acct += delta;
3868 codec->power_jiffies += delta;
3872 * snd_hda_power_up - Power-up the codec
3873 * @codec: HD-audio codec
3875 * Increment the power-up counter and power up the hardware really when
3876 * not turned on yet.
3878 void snd_hda_power_up(struct hda_codec *codec)
3880 struct hda_bus *bus = codec->bus;
3882 codec->power_count++;
3883 if (codec->power_on || codec->power_transition)
3884 return;
3886 snd_hda_update_power_acct(codec);
3887 codec->power_on = 1;
3888 codec->power_jiffies = jiffies;
3889 if (bus->ops.pm_notify)
3890 bus->ops.pm_notify(bus);
3891 hda_call_codec_resume(codec);
3892 cancel_delayed_work(&codec->power_work);
3893 codec->power_transition = 0;
3895 EXPORT_SYMBOL_HDA(snd_hda_power_up);
3897 #define power_save(codec) \
3898 ((codec)->bus->power_save ? *(codec)->bus->power_save : 0)
3901 * snd_hda_power_down - Power-down the codec
3902 * @codec: HD-audio codec
3904 * Decrement the power-up counter and schedules the power-off work if
3905 * the counter rearches to zero.
3907 void snd_hda_power_down(struct hda_codec *codec)
3909 --codec->power_count;
3910 if (!codec->power_on || codec->power_count || codec->power_transition)
3911 return;
3912 if (power_save(codec)) {
3913 codec->power_transition = 1; /* avoid reentrance */
3914 queue_delayed_work(codec->bus->workq, &codec->power_work,
3915 msecs_to_jiffies(power_save(codec) * 1000));
3918 EXPORT_SYMBOL_HDA(snd_hda_power_down);
3921 * snd_hda_check_amp_list_power - Check the amp list and update the power
3922 * @codec: HD-audio codec
3923 * @check: the object containing an AMP list and the status
3924 * @nid: NID to check / update
3926 * Check whether the given NID is in the amp list. If it's in the list,
3927 * check the current AMP status, and update the the power-status according
3928 * to the mute status.
3930 * This function is supposed to be set or called from the check_power_status
3931 * patch ops.
3933 int snd_hda_check_amp_list_power(struct hda_codec *codec,
3934 struct hda_loopback_check *check,
3935 hda_nid_t nid)
3937 struct hda_amp_list *p;
3938 int ch, v;
3940 if (!check->amplist)
3941 return 0;
3942 for (p = check->amplist; p->nid; p++) {
3943 if (p->nid == nid)
3944 break;
3946 if (!p->nid)
3947 return 0; /* nothing changed */
3949 for (p = check->amplist; p->nid; p++) {
3950 for (ch = 0; ch < 2; ch++) {
3951 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
3952 p->idx);
3953 if (!(v & HDA_AMP_MUTE) && v > 0) {
3954 if (!check->power_on) {
3955 check->power_on = 1;
3956 snd_hda_power_up(codec);
3958 return 1;
3962 if (check->power_on) {
3963 check->power_on = 0;
3964 snd_hda_power_down(codec);
3966 return 0;
3968 EXPORT_SYMBOL_HDA(snd_hda_check_amp_list_power);
3969 #endif
3972 * Channel mode helper
3976 * snd_hda_ch_mode_info - Info callback helper for the channel mode enum
3978 int snd_hda_ch_mode_info(struct hda_codec *codec,
3979 struct snd_ctl_elem_info *uinfo,
3980 const struct hda_channel_mode *chmode,
3981 int num_chmodes)
3983 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
3984 uinfo->count = 1;
3985 uinfo->value.enumerated.items = num_chmodes;
3986 if (uinfo->value.enumerated.item >= num_chmodes)
3987 uinfo->value.enumerated.item = num_chmodes - 1;
3988 sprintf(uinfo->value.enumerated.name, "%dch",
3989 chmode[uinfo->value.enumerated.item].channels);
3990 return 0;
3992 EXPORT_SYMBOL_HDA(snd_hda_ch_mode_info);
3995 * snd_hda_ch_mode_get - Get callback helper for the channel mode enum
3997 int snd_hda_ch_mode_get(struct hda_codec *codec,
3998 struct snd_ctl_elem_value *ucontrol,
3999 const struct hda_channel_mode *chmode,
4000 int num_chmodes,
4001 int max_channels)
4003 int i;
4005 for (i = 0; i < num_chmodes; i++) {
4006 if (max_channels == chmode[i].channels) {
4007 ucontrol->value.enumerated.item[0] = i;
4008 break;
4011 return 0;
4013 EXPORT_SYMBOL_HDA(snd_hda_ch_mode_get);
4016 * snd_hda_ch_mode_put - Put callback helper for the channel mode enum
4018 int snd_hda_ch_mode_put(struct hda_codec *codec,
4019 struct snd_ctl_elem_value *ucontrol,
4020 const struct hda_channel_mode *chmode,
4021 int num_chmodes,
4022 int *max_channelsp)
4024 unsigned int mode;
4026 mode = ucontrol->value.enumerated.item[0];
4027 if (mode >= num_chmodes)
4028 return -EINVAL;
4029 if (*max_channelsp == chmode[mode].channels)
4030 return 0;
4031 /* change the current channel setting */
4032 *max_channelsp = chmode[mode].channels;
4033 if (chmode[mode].sequence)
4034 snd_hda_sequence_write_cache(codec, chmode[mode].sequence);
4035 return 1;
4037 EXPORT_SYMBOL_HDA(snd_hda_ch_mode_put);
4040 * input MUX helper
4044 * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum
4046 int snd_hda_input_mux_info(const struct hda_input_mux *imux,
4047 struct snd_ctl_elem_info *uinfo)
4049 unsigned int index;
4051 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4052 uinfo->count = 1;
4053 uinfo->value.enumerated.items = imux->num_items;
4054 if (!imux->num_items)
4055 return 0;
4056 index = uinfo->value.enumerated.item;
4057 if (index >= imux->num_items)
4058 index = imux->num_items - 1;
4059 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
4060 return 0;
4062 EXPORT_SYMBOL_HDA(snd_hda_input_mux_info);
4065 * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum
4067 int snd_hda_input_mux_put(struct hda_codec *codec,
4068 const struct hda_input_mux *imux,
4069 struct snd_ctl_elem_value *ucontrol,
4070 hda_nid_t nid,
4071 unsigned int *cur_val)
4073 unsigned int idx;
4075 if (!imux->num_items)
4076 return 0;
4077 idx = ucontrol->value.enumerated.item[0];
4078 if (idx >= imux->num_items)
4079 idx = imux->num_items - 1;
4080 if (*cur_val == idx)
4081 return 0;
4082 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
4083 imux->items[idx].index);
4084 *cur_val = idx;
4085 return 1;
4087 EXPORT_SYMBOL_HDA(snd_hda_input_mux_put);
4091 * Multi-channel / digital-out PCM helper functions
4094 /* setup SPDIF output stream */
4095 static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
4096 unsigned int stream_tag, unsigned int format)
4098 /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */
4099 if (codec->spdif_status_reset && (codec->spdif_ctls & AC_DIG1_ENABLE))
4100 set_dig_out_convert(codec, nid,
4101 codec->spdif_ctls & ~AC_DIG1_ENABLE & 0xff,
4102 -1);
4103 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
4104 if (codec->slave_dig_outs) {
4105 hda_nid_t *d;
4106 for (d = codec->slave_dig_outs; *d; d++)
4107 snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
4108 format);
4110 /* turn on again (if needed) */
4111 if (codec->spdif_status_reset && (codec->spdif_ctls & AC_DIG1_ENABLE))
4112 set_dig_out_convert(codec, nid,
4113 codec->spdif_ctls & 0xff, -1);
4116 static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
4118 snd_hda_codec_cleanup_stream(codec, nid);
4119 if (codec->slave_dig_outs) {
4120 hda_nid_t *d;
4121 for (d = codec->slave_dig_outs; *d; d++)
4122 snd_hda_codec_cleanup_stream(codec, *d);
4127 * snd_hda_bus_reboot_notify - call the reboot notifier of each codec
4128 * @bus: HD-audio bus
4130 void snd_hda_bus_reboot_notify(struct hda_bus *bus)
4132 struct hda_codec *codec;
4134 if (!bus)
4135 return;
4136 list_for_each_entry(codec, &bus->codec_list, list) {
4137 #ifdef CONFIG_SND_HDA_POWER_SAVE
4138 if (!codec->power_on)
4139 continue;
4140 #endif
4141 if (codec->patch_ops.reboot_notify)
4142 codec->patch_ops.reboot_notify(codec);
4145 EXPORT_SYMBOL_HDA(snd_hda_bus_reboot_notify);
4148 * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode
4150 int snd_hda_multi_out_dig_open(struct hda_codec *codec,
4151 struct hda_multi_out *mout)
4153 mutex_lock(&codec->spdif_mutex);
4154 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
4155 /* already opened as analog dup; reset it once */
4156 cleanup_dig_out_stream(codec, mout->dig_out_nid);
4157 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
4158 mutex_unlock(&codec->spdif_mutex);
4159 return 0;
4161 EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_open);
4164 * snd_hda_multi_out_dig_prepare - prepare the digital out stream
4166 int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
4167 struct hda_multi_out *mout,
4168 unsigned int stream_tag,
4169 unsigned int format,
4170 struct snd_pcm_substream *substream)
4172 mutex_lock(&codec->spdif_mutex);
4173 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
4174 mutex_unlock(&codec->spdif_mutex);
4175 return 0;
4177 EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_prepare);
4180 * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream
4182 int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec,
4183 struct hda_multi_out *mout)
4185 mutex_lock(&codec->spdif_mutex);
4186 cleanup_dig_out_stream(codec, mout->dig_out_nid);
4187 mutex_unlock(&codec->spdif_mutex);
4188 return 0;
4190 EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_cleanup);
4193 * snd_hda_multi_out_dig_close - release the digital out stream
4195 int snd_hda_multi_out_dig_close(struct hda_codec *codec,
4196 struct hda_multi_out *mout)
4198 mutex_lock(&codec->spdif_mutex);
4199 mout->dig_out_used = 0;
4200 mutex_unlock(&codec->spdif_mutex);
4201 return 0;
4203 EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_close);
4206 * snd_hda_multi_out_analog_open - open analog outputs
4208 * Open analog outputs and set up the hw-constraints.
4209 * If the digital outputs can be opened as slave, open the digital
4210 * outputs, too.
4212 int snd_hda_multi_out_analog_open(struct hda_codec *codec,
4213 struct hda_multi_out *mout,
4214 struct snd_pcm_substream *substream,
4215 struct hda_pcm_stream *hinfo)
4217 struct snd_pcm_runtime *runtime = substream->runtime;
4218 runtime->hw.channels_max = mout->max_channels;
4219 if (mout->dig_out_nid) {
4220 if (!mout->analog_rates) {
4221 mout->analog_rates = hinfo->rates;
4222 mout->analog_formats = hinfo->formats;
4223 mout->analog_maxbps = hinfo->maxbps;
4224 } else {
4225 runtime->hw.rates = mout->analog_rates;
4226 runtime->hw.formats = mout->analog_formats;
4227 hinfo->maxbps = mout->analog_maxbps;
4229 if (!mout->spdif_rates) {
4230 snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
4231 &mout->spdif_rates,
4232 &mout->spdif_formats,
4233 &mout->spdif_maxbps);
4235 mutex_lock(&codec->spdif_mutex);
4236 if (mout->share_spdif) {
4237 if ((runtime->hw.rates & mout->spdif_rates) &&
4238 (runtime->hw.formats & mout->spdif_formats)) {
4239 runtime->hw.rates &= mout->spdif_rates;
4240 runtime->hw.formats &= mout->spdif_formats;
4241 if (mout->spdif_maxbps < hinfo->maxbps)
4242 hinfo->maxbps = mout->spdif_maxbps;
4243 } else {
4244 mout->share_spdif = 0;
4245 /* FIXME: need notify? */
4248 mutex_unlock(&codec->spdif_mutex);
4250 return snd_pcm_hw_constraint_step(substream->runtime, 0,
4251 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
4253 EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_open);
4256 * snd_hda_multi_out_analog_prepare - Preapre the analog outputs.
4258 * Set up the i/o for analog out.
4259 * When the digital out is available, copy the front out to digital out, too.
4261 int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
4262 struct hda_multi_out *mout,
4263 unsigned int stream_tag,
4264 unsigned int format,
4265 struct snd_pcm_substream *substream)
4267 hda_nid_t *nids = mout->dac_nids;
4268 int chs = substream->runtime->channels;
4269 int i;
4271 mutex_lock(&codec->spdif_mutex);
4272 if (mout->dig_out_nid && mout->share_spdif &&
4273 mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
4274 if (chs == 2 &&
4275 snd_hda_is_supported_format(codec, mout->dig_out_nid,
4276 format) &&
4277 !(codec->spdif_status & IEC958_AES0_NONAUDIO)) {
4278 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
4279 setup_dig_out_stream(codec, mout->dig_out_nid,
4280 stream_tag, format);
4281 } else {
4282 mout->dig_out_used = 0;
4283 cleanup_dig_out_stream(codec, mout->dig_out_nid);
4286 mutex_unlock(&codec->spdif_mutex);
4288 /* front */
4289 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
4290 0, format);
4291 if (!mout->no_share_stream &&
4292 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
4293 /* headphone out will just decode front left/right (stereo) */
4294 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
4295 0, format);
4296 /* extra outputs copied from front */
4297 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
4298 if (!mout->no_share_stream && mout->extra_out_nid[i])
4299 snd_hda_codec_setup_stream(codec,
4300 mout->extra_out_nid[i],
4301 stream_tag, 0, format);
4303 /* surrounds */
4304 for (i = 1; i < mout->num_dacs; i++) {
4305 if (chs >= (i + 1) * 2) /* independent out */
4306 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
4307 i * 2, format);
4308 else if (!mout->no_share_stream) /* copy front */
4309 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
4310 0, format);
4312 return 0;
4314 EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_prepare);
4317 * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out
4319 int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
4320 struct hda_multi_out *mout)
4322 hda_nid_t *nids = mout->dac_nids;
4323 int i;
4325 for (i = 0; i < mout->num_dacs; i++)
4326 snd_hda_codec_cleanup_stream(codec, nids[i]);
4327 if (mout->hp_nid)
4328 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
4329 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
4330 if (mout->extra_out_nid[i])
4331 snd_hda_codec_cleanup_stream(codec,
4332 mout->extra_out_nid[i]);
4333 mutex_lock(&codec->spdif_mutex);
4334 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
4335 cleanup_dig_out_stream(codec, mout->dig_out_nid);
4336 mout->dig_out_used = 0;
4338 mutex_unlock(&codec->spdif_mutex);
4339 return 0;
4341 EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_cleanup);
4344 * Helper for automatic pin configuration
4347 static int is_in_nid_list(hda_nid_t nid, hda_nid_t *list)
4349 for (; *list; list++)
4350 if (*list == nid)
4351 return 1;
4352 return 0;
4357 * Sort an associated group of pins according to their sequence numbers.
4359 static void sort_pins_by_sequence(hda_nid_t *pins, short *sequences,
4360 int num_pins)
4362 int i, j;
4363 short seq;
4364 hda_nid_t nid;
4366 for (i = 0; i < num_pins; i++) {
4367 for (j = i + 1; j < num_pins; j++) {
4368 if (sequences[i] > sequences[j]) {
4369 seq = sequences[i];
4370 sequences[i] = sequences[j];
4371 sequences[j] = seq;
4372 nid = pins[i];
4373 pins[i] = pins[j];
4374 pins[j] = nid;
4381 /* add the found input-pin to the cfg->inputs[] table */
4382 static void add_auto_cfg_input_pin(struct auto_pin_cfg *cfg, hda_nid_t nid,
4383 int type)
4385 if (cfg->num_inputs < AUTO_CFG_MAX_INS) {
4386 cfg->inputs[cfg->num_inputs].pin = nid;
4387 cfg->inputs[cfg->num_inputs].type = type;
4388 cfg->num_inputs++;
4392 /* sort inputs in the order of AUTO_PIN_* type */
4393 static void sort_autocfg_input_pins(struct auto_pin_cfg *cfg)
4395 int i, j;
4397 for (i = 0; i < cfg->num_inputs; i++) {
4398 for (j = i + 1; j < cfg->num_inputs; j++) {
4399 if (cfg->inputs[i].type > cfg->inputs[j].type) {
4400 struct auto_pin_cfg_item tmp;
4401 tmp = cfg->inputs[i];
4402 cfg->inputs[i] = cfg->inputs[j];
4403 cfg->inputs[j] = tmp;
4410 * Parse all pin widgets and store the useful pin nids to cfg
4412 * The number of line-outs or any primary output is stored in line_outs,
4413 * and the corresponding output pins are assigned to line_out_pins[],
4414 * in the order of front, rear, CLFE, side, ...
4416 * If more extra outputs (speaker and headphone) are found, the pins are
4417 * assisnged to hp_pins[] and speaker_pins[], respectively. If no line-out jack
4418 * is detected, one of speaker of HP pins is assigned as the primary
4419 * output, i.e. to line_out_pins[0]. So, line_outs is always positive
4420 * if any analog output exists.
4422 * The analog input pins are assigned to inputs array.
4423 * The digital input/output pins are assigned to dig_in_pin and dig_out_pin,
4424 * respectively.
4426 int snd_hda_parse_pin_def_config(struct hda_codec *codec,
4427 struct auto_pin_cfg *cfg,
4428 hda_nid_t *ignore_nids)
4430 hda_nid_t nid, end_nid;
4431 short seq, assoc_line_out, assoc_speaker;
4432 short sequences_line_out[ARRAY_SIZE(cfg->line_out_pins)];
4433 short sequences_speaker[ARRAY_SIZE(cfg->speaker_pins)];
4434 short sequences_hp[ARRAY_SIZE(cfg->hp_pins)];
4435 int i;
4437 memset(cfg, 0, sizeof(*cfg));
4439 memset(sequences_line_out, 0, sizeof(sequences_line_out));
4440 memset(sequences_speaker, 0, sizeof(sequences_speaker));
4441 memset(sequences_hp, 0, sizeof(sequences_hp));
4442 assoc_line_out = assoc_speaker = 0;
4444 end_nid = codec->start_nid + codec->num_nodes;
4445 for (nid = codec->start_nid; nid < end_nid; nid++) {
4446 unsigned int wid_caps = get_wcaps(codec, nid);
4447 unsigned int wid_type = get_wcaps_type(wid_caps);
4448 unsigned int def_conf;
4449 short assoc, loc;
4451 /* read all default configuration for pin complex */
4452 if (wid_type != AC_WID_PIN)
4453 continue;
4454 /* ignore the given nids (e.g. pc-beep returns error) */
4455 if (ignore_nids && is_in_nid_list(nid, ignore_nids))
4456 continue;
4458 def_conf = snd_hda_codec_get_pincfg(codec, nid);
4459 if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE)
4460 continue;
4461 loc = get_defcfg_location(def_conf);
4462 switch (get_defcfg_device(def_conf)) {
4463 case AC_JACK_LINE_OUT:
4464 seq = get_defcfg_sequence(def_conf);
4465 assoc = get_defcfg_association(def_conf);
4467 if (!(wid_caps & AC_WCAP_STEREO))
4468 if (!cfg->mono_out_pin)
4469 cfg->mono_out_pin = nid;
4470 if (!assoc)
4471 continue;
4472 if (!assoc_line_out)
4473 assoc_line_out = assoc;
4474 else if (assoc_line_out != assoc)
4475 continue;
4476 if (cfg->line_outs >= ARRAY_SIZE(cfg->line_out_pins))
4477 continue;
4478 cfg->line_out_pins[cfg->line_outs] = nid;
4479 sequences_line_out[cfg->line_outs] = seq;
4480 cfg->line_outs++;
4481 break;
4482 case AC_JACK_SPEAKER:
4483 seq = get_defcfg_sequence(def_conf);
4484 assoc = get_defcfg_association(def_conf);
4485 if (!assoc)
4486 continue;
4487 if (!assoc_speaker)
4488 assoc_speaker = assoc;
4489 else if (assoc_speaker != assoc)
4490 continue;
4491 if (cfg->speaker_outs >= ARRAY_SIZE(cfg->speaker_pins))
4492 continue;
4493 cfg->speaker_pins[cfg->speaker_outs] = nid;
4494 sequences_speaker[cfg->speaker_outs] = seq;
4495 cfg->speaker_outs++;
4496 break;
4497 case AC_JACK_HP_OUT:
4498 seq = get_defcfg_sequence(def_conf);
4499 assoc = get_defcfg_association(def_conf);
4500 if (cfg->hp_outs >= ARRAY_SIZE(cfg->hp_pins))
4501 continue;
4502 cfg->hp_pins[cfg->hp_outs] = nid;
4503 sequences_hp[cfg->hp_outs] = (assoc << 4) | seq;
4504 cfg->hp_outs++;
4505 break;
4506 case AC_JACK_MIC_IN:
4507 add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_MIC);
4508 break;
4509 case AC_JACK_LINE_IN:
4510 add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_LINE_IN);
4511 break;
4512 case AC_JACK_CD:
4513 add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_CD);
4514 break;
4515 case AC_JACK_AUX:
4516 add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_AUX);
4517 break;
4518 case AC_JACK_SPDIF_OUT:
4519 case AC_JACK_DIG_OTHER_OUT:
4520 if (cfg->dig_outs >= ARRAY_SIZE(cfg->dig_out_pins))
4521 continue;
4522 cfg->dig_out_pins[cfg->dig_outs] = nid;
4523 cfg->dig_out_type[cfg->dig_outs] =
4524 (loc == AC_JACK_LOC_HDMI) ?
4525 HDA_PCM_TYPE_HDMI : HDA_PCM_TYPE_SPDIF;
4526 cfg->dig_outs++;
4527 break;
4528 case AC_JACK_SPDIF_IN:
4529 case AC_JACK_DIG_OTHER_IN:
4530 cfg->dig_in_pin = nid;
4531 if (loc == AC_JACK_LOC_HDMI)
4532 cfg->dig_in_type = HDA_PCM_TYPE_HDMI;
4533 else
4534 cfg->dig_in_type = HDA_PCM_TYPE_SPDIF;
4535 break;
4539 /* FIX-UP:
4540 * If no line-out is defined but multiple HPs are found,
4541 * some of them might be the real line-outs.
4543 if (!cfg->line_outs && cfg->hp_outs > 1) {
4544 int i = 0;
4545 while (i < cfg->hp_outs) {
4546 /* The real HPs should have the sequence 0x0f */
4547 if ((sequences_hp[i] & 0x0f) == 0x0f) {
4548 i++;
4549 continue;
4551 /* Move it to the line-out table */
4552 cfg->line_out_pins[cfg->line_outs] = cfg->hp_pins[i];
4553 sequences_line_out[cfg->line_outs] = sequences_hp[i];
4554 cfg->line_outs++;
4555 cfg->hp_outs--;
4556 memmove(cfg->hp_pins + i, cfg->hp_pins + i + 1,
4557 sizeof(cfg->hp_pins[0]) * (cfg->hp_outs - i));
4558 memmove(sequences_hp + i, sequences_hp + i + 1,
4559 sizeof(sequences_hp[0]) * (cfg->hp_outs - i));
4561 memset(cfg->hp_pins + cfg->hp_outs, 0,
4562 sizeof(hda_nid_t) * (AUTO_CFG_MAX_OUTS - cfg->hp_outs));
4565 /* sort by sequence */
4566 sort_pins_by_sequence(cfg->line_out_pins, sequences_line_out,
4567 cfg->line_outs);
4568 sort_pins_by_sequence(cfg->speaker_pins, sequences_speaker,
4569 cfg->speaker_outs);
4570 sort_pins_by_sequence(cfg->hp_pins, sequences_hp,
4571 cfg->hp_outs);
4574 * FIX-UP: if no line-outs are detected, try to use speaker or HP pin
4575 * as a primary output
4577 if (!cfg->line_outs) {
4578 if (cfg->speaker_outs) {
4579 cfg->line_outs = cfg->speaker_outs;
4580 memcpy(cfg->line_out_pins, cfg->speaker_pins,
4581 sizeof(cfg->speaker_pins));
4582 cfg->speaker_outs = 0;
4583 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
4584 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
4585 } else if (cfg->hp_outs) {
4586 cfg->line_outs = cfg->hp_outs;
4587 memcpy(cfg->line_out_pins, cfg->hp_pins,
4588 sizeof(cfg->hp_pins));
4589 cfg->hp_outs = 0;
4590 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
4591 cfg->line_out_type = AUTO_PIN_HP_OUT;
4595 /* Reorder the surround channels
4596 * ALSA sequence is front/surr/clfe/side
4597 * HDA sequence is:
4598 * 4-ch: front/surr => OK as it is
4599 * 6-ch: front/clfe/surr
4600 * 8-ch: front/clfe/rear/side|fc
4602 switch (cfg->line_outs) {
4603 case 3:
4604 case 4:
4605 nid = cfg->line_out_pins[1];
4606 cfg->line_out_pins[1] = cfg->line_out_pins[2];
4607 cfg->line_out_pins[2] = nid;
4608 break;
4611 sort_autocfg_input_pins(cfg);
4614 * debug prints of the parsed results
4616 snd_printd("autoconfig: line_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
4617 cfg->line_outs, cfg->line_out_pins[0], cfg->line_out_pins[1],
4618 cfg->line_out_pins[2], cfg->line_out_pins[3],
4619 cfg->line_out_pins[4]);
4620 snd_printd(" speaker_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
4621 cfg->speaker_outs, cfg->speaker_pins[0],
4622 cfg->speaker_pins[1], cfg->speaker_pins[2],
4623 cfg->speaker_pins[3], cfg->speaker_pins[4]);
4624 snd_printd(" hp_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
4625 cfg->hp_outs, cfg->hp_pins[0],
4626 cfg->hp_pins[1], cfg->hp_pins[2],
4627 cfg->hp_pins[3], cfg->hp_pins[4]);
4628 snd_printd(" mono: mono_out=0x%x\n", cfg->mono_out_pin);
4629 if (cfg->dig_outs)
4630 snd_printd(" dig-out=0x%x/0x%x\n",
4631 cfg->dig_out_pins[0], cfg->dig_out_pins[1]);
4632 snd_printd(" inputs:");
4633 for (i = 0; i < cfg->num_inputs; i++) {
4634 snd_printdd(" %s=0x%x",
4635 hda_get_autocfg_input_label(codec, cfg, i),
4636 cfg->inputs[i].pin);
4638 snd_printd("\n");
4639 if (cfg->dig_in_pin)
4640 snd_printd(" dig-in=0x%x\n", cfg->dig_in_pin);
4642 return 0;
4644 EXPORT_SYMBOL_HDA(snd_hda_parse_pin_def_config);
4646 int snd_hda_get_input_pin_attr(unsigned int def_conf)
4648 unsigned int loc = get_defcfg_location(def_conf);
4649 unsigned int conn = get_defcfg_connect(def_conf);
4650 if (conn == AC_JACK_PORT_NONE)
4651 return INPUT_PIN_ATTR_UNUSED;
4652 /* Windows may claim the internal mic to be BOTH, too */
4653 if (conn == AC_JACK_PORT_FIXED || conn == AC_JACK_PORT_BOTH)
4654 return INPUT_PIN_ATTR_INT;
4655 if ((loc & 0x30) == AC_JACK_LOC_INTERNAL)
4656 return INPUT_PIN_ATTR_INT;
4657 if ((loc & 0x30) == AC_JACK_LOC_SEPARATE)
4658 return INPUT_PIN_ATTR_DOCK;
4659 if (loc == AC_JACK_LOC_REAR)
4660 return INPUT_PIN_ATTR_REAR;
4661 if (loc == AC_JACK_LOC_FRONT)
4662 return INPUT_PIN_ATTR_FRONT;
4663 return INPUT_PIN_ATTR_NORMAL;
4665 EXPORT_SYMBOL_HDA(snd_hda_get_input_pin_attr);
4668 * hda_get_input_pin_label - Give a label for the given input pin
4670 * When check_location is true, the function checks the pin location
4671 * for mic and line-in pins, and set an appropriate prefix like "Front",
4672 * "Rear", "Internal".
4675 const char *hda_get_input_pin_label(struct hda_codec *codec, hda_nid_t pin,
4676 int check_location)
4678 unsigned int def_conf;
4679 static const char *mic_names[] = {
4680 "Internal Mic", "Dock Mic", "Mic", "Front Mic", "Rear Mic",
4682 int attr;
4684 def_conf = snd_hda_codec_get_pincfg(codec, pin);
4686 switch (get_defcfg_device(def_conf)) {
4687 case AC_JACK_MIC_IN:
4688 if (!check_location)
4689 return "Mic";
4690 attr = snd_hda_get_input_pin_attr(def_conf);
4691 if (!attr)
4692 return "None";
4693 return mic_names[attr - 1];
4694 case AC_JACK_LINE_IN:
4695 if (!check_location)
4696 return "Line";
4697 attr = snd_hda_get_input_pin_attr(def_conf);
4698 if (!attr)
4699 return "None";
4700 if (attr == INPUT_PIN_ATTR_DOCK)
4701 return "Dock Line";
4702 return "Line";
4703 case AC_JACK_AUX:
4704 return "Aux";
4705 case AC_JACK_CD:
4706 return "CD";
4707 case AC_JACK_SPDIF_IN:
4708 return "SPDIF In";
4709 case AC_JACK_DIG_OTHER_IN:
4710 return "Digital In";
4711 default:
4712 return "Misc";
4715 EXPORT_SYMBOL_HDA(hda_get_input_pin_label);
4717 /* Check whether the location prefix needs to be added to the label.
4718 * If all mic-jacks are in the same location (e.g. rear panel), we don't
4719 * have to put "Front" prefix to each label. In such a case, returns false.
4721 static int check_mic_location_need(struct hda_codec *codec,
4722 const struct auto_pin_cfg *cfg,
4723 int input)
4725 unsigned int defc;
4726 int i, attr, attr2;
4728 defc = snd_hda_codec_get_pincfg(codec, cfg->inputs[input].pin);
4729 attr = snd_hda_get_input_pin_attr(defc);
4730 /* for internal or docking mics, we need locations */
4731 if (attr <= INPUT_PIN_ATTR_NORMAL)
4732 return 1;
4734 attr = 0;
4735 for (i = 0; i < cfg->num_inputs; i++) {
4736 defc = snd_hda_codec_get_pincfg(codec, cfg->inputs[i].pin);
4737 attr2 = snd_hda_get_input_pin_attr(defc);
4738 if (attr2 >= INPUT_PIN_ATTR_NORMAL) {
4739 if (attr && attr != attr2)
4740 return 1; /* different locations found */
4741 attr = attr2;
4744 return 0;
4748 * hda_get_autocfg_input_label - Get a label for the given input
4750 * Get a label for the given input pin defined by the autocfg item.
4751 * Unlike hda_get_input_pin_label(), this function checks all inputs
4752 * defined in autocfg and avoids the redundant mic/line prefix as much as
4753 * possible.
4755 const char *hda_get_autocfg_input_label(struct hda_codec *codec,
4756 const struct auto_pin_cfg *cfg,
4757 int input)
4759 int type = cfg->inputs[input].type;
4760 int has_multiple_pins = 0;
4762 if ((input > 0 && cfg->inputs[input - 1].type == type) ||
4763 (input < cfg->num_inputs - 1 && cfg->inputs[input + 1].type == type))
4764 has_multiple_pins = 1;
4765 if (has_multiple_pins && type == AUTO_PIN_MIC)
4766 has_multiple_pins &= check_mic_location_need(codec, cfg, input);
4767 return hda_get_input_pin_label(codec, cfg->inputs[input].pin,
4768 has_multiple_pins);
4770 EXPORT_SYMBOL_HDA(hda_get_autocfg_input_label);
4773 * snd_hda_add_imux_item - Add an item to input_mux
4775 * When the same label is used already in the existing items, the number
4776 * suffix is appended to the label. This label index number is stored
4777 * to type_idx when non-NULL pointer is given.
4779 int snd_hda_add_imux_item(struct hda_input_mux *imux, const char *label,
4780 int index, int *type_idx)
4782 int i, label_idx = 0;
4783 if (imux->num_items >= HDA_MAX_NUM_INPUTS) {
4784 snd_printd(KERN_ERR "hda_codec: Too many imux items!\n");
4785 return -EINVAL;
4787 for (i = 0; i < imux->num_items; i++) {
4788 if (!strncmp(label, imux->items[i].label, strlen(label)))
4789 label_idx++;
4791 if (type_idx)
4792 *type_idx = label_idx;
4793 if (label_idx > 0)
4794 snprintf(imux->items[imux->num_items].label,
4795 sizeof(imux->items[imux->num_items].label),
4796 "%s %d", label, label_idx);
4797 else
4798 strlcpy(imux->items[imux->num_items].label, label,
4799 sizeof(imux->items[imux->num_items].label));
4800 imux->items[imux->num_items].index = index;
4801 imux->num_items++;
4802 return 0;
4804 EXPORT_SYMBOL_HDA(snd_hda_add_imux_item);
4807 #ifdef CONFIG_PM
4809 * power management
4813 * snd_hda_suspend - suspend the codecs
4814 * @bus: the HDA bus
4816 * Returns 0 if successful.
4818 int snd_hda_suspend(struct hda_bus *bus)
4820 struct hda_codec *codec;
4822 list_for_each_entry(codec, &bus->codec_list, list) {
4823 #ifdef CONFIG_SND_HDA_POWER_SAVE
4824 if (!codec->power_on)
4825 continue;
4826 #endif
4827 hda_call_codec_suspend(codec);
4829 return 0;
4831 EXPORT_SYMBOL_HDA(snd_hda_suspend);
4834 * snd_hda_resume - resume the codecs
4835 * @bus: the HDA bus
4837 * Returns 0 if successful.
4839 * This fucntion is defined only when POWER_SAVE isn't set.
4840 * In the power-save mode, the codec is resumed dynamically.
4842 int snd_hda_resume(struct hda_bus *bus)
4844 struct hda_codec *codec;
4846 list_for_each_entry(codec, &bus->codec_list, list) {
4847 if (snd_hda_codec_needs_resume(codec))
4848 hda_call_codec_resume(codec);
4850 return 0;
4852 EXPORT_SYMBOL_HDA(snd_hda_resume);
4853 #endif /* CONFIG_PM */
4856 * generic arrays
4860 * snd_array_new - get a new element from the given array
4861 * @array: the array object
4863 * Get a new element from the given array. If it exceeds the
4864 * pre-allocated array size, re-allocate the array.
4866 * Returns NULL if allocation failed.
4868 void *snd_array_new(struct snd_array *array)
4870 if (array->used >= array->alloced) {
4871 int num = array->alloced + array->alloc_align;
4872 void *nlist;
4873 if (snd_BUG_ON(num >= 4096))
4874 return NULL;
4875 nlist = kcalloc(num + 1, array->elem_size, GFP_KERNEL);
4876 if (!nlist)
4877 return NULL;
4878 if (array->list) {
4879 memcpy(nlist, array->list,
4880 array->elem_size * array->alloced);
4881 kfree(array->list);
4883 array->list = nlist;
4884 array->alloced = num;
4886 return snd_array_elem(array, array->used++);
4888 EXPORT_SYMBOL_HDA(snd_array_new);
4891 * snd_array_free - free the given array elements
4892 * @array: the array object
4894 void snd_array_free(struct snd_array *array)
4896 kfree(array->list);
4897 array->used = 0;
4898 array->alloced = 0;
4899 array->list = NULL;
4901 EXPORT_SYMBOL_HDA(snd_array_free);
4904 * snd_print_pcm_rates - Print the supported PCM rates to the string buffer
4905 * @pcm: PCM caps bits
4906 * @buf: the string buffer to write
4907 * @buflen: the max buffer length
4909 * used by hda_proc.c and hda_eld.c
4911 void snd_print_pcm_rates(int pcm, char *buf, int buflen)
4913 static unsigned int rates[] = {
4914 8000, 11025, 16000, 22050, 32000, 44100, 48000, 88200,
4915 96000, 176400, 192000, 384000
4917 int i, j;
4919 for (i = 0, j = 0; i < ARRAY_SIZE(rates); i++)
4920 if (pcm & (1 << i))
4921 j += snprintf(buf + j, buflen - j, " %d", rates[i]);
4923 buf[j] = '\0'; /* necessary when j == 0 */
4925 EXPORT_SYMBOL_HDA(snd_print_pcm_rates);
4928 * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer
4929 * @pcm: PCM caps bits
4930 * @buf: the string buffer to write
4931 * @buflen: the max buffer length
4933 * used by hda_proc.c and hda_eld.c
4935 void snd_print_pcm_bits(int pcm, char *buf, int buflen)
4937 static unsigned int bits[] = { 8, 16, 20, 24, 32 };
4938 int i, j;
4940 for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
4941 if (pcm & (AC_SUPPCM_BITS_8 << i))
4942 j += snprintf(buf + j, buflen - j, " %d", bits[i]);
4944 buf[j] = '\0'; /* necessary when j == 0 */
4946 EXPORT_SYMBOL_HDA(snd_print_pcm_bits);
4948 MODULE_DESCRIPTION("HDA codec core");
4949 MODULE_LICENSE("GPL");