Linux 3.11-rc3
[cris-mirror.git] / sound / pci / hda / hda_codec.c
blob8a005f0e5ca484c842e2fe5f684275abafbde248
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/mm.h>
23 #include <linux/init.h>
24 #include <linux/delay.h>
25 #include <linux/slab.h>
26 #include <linux/pci.h>
27 #include <linux/mutex.h>
28 #include <linux/module.h>
29 #include <sound/core.h>
30 #include "hda_codec.h"
31 #include <sound/asoundef.h>
32 #include <sound/tlv.h>
33 #include <sound/initval.h>
34 #include <sound/jack.h>
35 #include "hda_local.h"
36 #include "hda_beep.h"
37 #include "hda_jack.h"
38 #include <sound/hda_hwdep.h>
40 #define CREATE_TRACE_POINTS
41 #include "hda_trace.h"
44 * vendor / preset table
47 struct hda_vendor_id {
48 unsigned int id;
49 const char *name;
52 /* codec vendor labels */
53 static struct hda_vendor_id hda_vendor_ids[] = {
54 { 0x1002, "ATI" },
55 { 0x1013, "Cirrus Logic" },
56 { 0x1057, "Motorola" },
57 { 0x1095, "Silicon Image" },
58 { 0x10de, "Nvidia" },
59 { 0x10ec, "Realtek" },
60 { 0x1102, "Creative" },
61 { 0x1106, "VIA" },
62 { 0x111d, "IDT" },
63 { 0x11c1, "LSI" },
64 { 0x11d4, "Analog Devices" },
65 { 0x13f6, "C-Media" },
66 { 0x14f1, "Conexant" },
67 { 0x17e8, "Chrontel" },
68 { 0x1854, "LG" },
69 { 0x1aec, "Wolfson Microelectronics" },
70 { 0x434d, "C-Media" },
71 { 0x8086, "Intel" },
72 { 0x8384, "SigmaTel" },
73 {} /* terminator */
76 static DEFINE_MUTEX(preset_mutex);
77 static LIST_HEAD(hda_preset_tables);
79 int snd_hda_add_codec_preset(struct hda_codec_preset_list *preset)
81 mutex_lock(&preset_mutex);
82 list_add_tail(&preset->list, &hda_preset_tables);
83 mutex_unlock(&preset_mutex);
84 return 0;
86 EXPORT_SYMBOL_HDA(snd_hda_add_codec_preset);
88 int snd_hda_delete_codec_preset(struct hda_codec_preset_list *preset)
90 mutex_lock(&preset_mutex);
91 list_del(&preset->list);
92 mutex_unlock(&preset_mutex);
93 return 0;
95 EXPORT_SYMBOL_HDA(snd_hda_delete_codec_preset);
97 #ifdef CONFIG_PM
98 #define codec_in_pm(codec) ((codec)->in_pm)
99 static void hda_power_work(struct work_struct *work);
100 static void hda_keep_power_on(struct hda_codec *codec);
101 #define hda_codec_is_power_on(codec) ((codec)->power_on)
102 static inline void hda_call_pm_notify(struct hda_bus *bus, bool power_up)
104 if (bus->ops.pm_notify)
105 bus->ops.pm_notify(bus, power_up);
107 #else
108 #define codec_in_pm(codec) 0
109 static inline void hda_keep_power_on(struct hda_codec *codec) {}
110 #define hda_codec_is_power_on(codec) 1
111 #define hda_call_pm_notify(bus, state) {}
112 #endif
115 * snd_hda_get_jack_location - Give a location string of the jack
116 * @cfg: pin default config value
118 * Parse the pin default config value and returns the string of the
119 * jack location, e.g. "Rear", "Front", etc.
121 const char *snd_hda_get_jack_location(u32 cfg)
123 static char *bases[7] = {
124 "N/A", "Rear", "Front", "Left", "Right", "Top", "Bottom",
126 static unsigned char specials_idx[] = {
127 0x07, 0x08,
128 0x17, 0x18, 0x19,
129 0x37, 0x38
131 static char *specials[] = {
132 "Rear Panel", "Drive Bar",
133 "Riser", "HDMI", "ATAPI",
134 "Mobile-In", "Mobile-Out"
136 int i;
137 cfg = (cfg & AC_DEFCFG_LOCATION) >> AC_DEFCFG_LOCATION_SHIFT;
138 if ((cfg & 0x0f) < 7)
139 return bases[cfg & 0x0f];
140 for (i = 0; i < ARRAY_SIZE(specials_idx); i++) {
141 if (cfg == specials_idx[i])
142 return specials[i];
144 return "UNKNOWN";
146 EXPORT_SYMBOL_HDA(snd_hda_get_jack_location);
149 * snd_hda_get_jack_connectivity - Give a connectivity string of the jack
150 * @cfg: pin default config value
152 * Parse the pin default config value and returns the string of the
153 * jack connectivity, i.e. external or internal connection.
155 const char *snd_hda_get_jack_connectivity(u32 cfg)
157 static char *jack_locations[4] = { "Ext", "Int", "Sep", "Oth" };
159 return jack_locations[(cfg >> (AC_DEFCFG_LOCATION_SHIFT + 4)) & 3];
161 EXPORT_SYMBOL_HDA(snd_hda_get_jack_connectivity);
164 * snd_hda_get_jack_type - Give a type string of the jack
165 * @cfg: pin default config value
167 * Parse the pin default config value and returns the string of the
168 * jack type, i.e. the purpose of the jack, such as Line-Out or CD.
170 const char *snd_hda_get_jack_type(u32 cfg)
172 static char *jack_types[16] = {
173 "Line Out", "Speaker", "HP Out", "CD",
174 "SPDIF Out", "Digital Out", "Modem Line", "Modem Hand",
175 "Line In", "Aux", "Mic", "Telephony",
176 "SPDIF In", "Digital In", "Reserved", "Other"
179 return jack_types[(cfg & AC_DEFCFG_DEVICE)
180 >> AC_DEFCFG_DEVICE_SHIFT];
182 EXPORT_SYMBOL_HDA(snd_hda_get_jack_type);
185 * Compose a 32bit command word to be sent to the HD-audio controller
187 static inline unsigned int
188 make_codec_cmd(struct hda_codec *codec, hda_nid_t nid, int flags,
189 unsigned int verb, unsigned int parm)
191 u32 val;
193 if ((codec->addr & ~0xf) || (nid & ~0x7f) ||
194 (verb & ~0xfff) || (parm & ~0xffff)) {
195 printk(KERN_ERR "hda-codec: out of range cmd %x:%x:%x:%x\n",
196 codec->addr, nid, verb, parm);
197 return ~0;
200 val = (u32)codec->addr << 28;
201 val |= (u32)nid << 20;
202 val |= verb << 8;
203 val |= parm;
204 return val;
208 * Send and receive a verb
210 static int codec_exec_verb(struct hda_codec *codec, unsigned int cmd,
211 int flags, unsigned int *res)
213 struct hda_bus *bus = codec->bus;
214 int err;
216 if (cmd == ~0)
217 return -1;
219 if (res)
220 *res = -1;
221 again:
222 snd_hda_power_up(codec);
223 mutex_lock(&bus->cmd_mutex);
224 if (flags & HDA_RW_NO_RESPONSE_FALLBACK)
225 bus->no_response_fallback = 1;
226 for (;;) {
227 trace_hda_send_cmd(codec, cmd);
228 err = bus->ops.command(bus, cmd);
229 if (err != -EAGAIN)
230 break;
231 /* process pending verbs */
232 bus->ops.get_response(bus, codec->addr);
234 if (!err && res) {
235 *res = bus->ops.get_response(bus, codec->addr);
236 trace_hda_get_response(codec, *res);
238 bus->no_response_fallback = 0;
239 mutex_unlock(&bus->cmd_mutex);
240 snd_hda_power_down(codec);
241 if (!codec_in_pm(codec) && res && *res == -1 && bus->rirb_error) {
242 if (bus->response_reset) {
243 snd_printd("hda_codec: resetting BUS due to "
244 "fatal communication error\n");
245 trace_hda_bus_reset(bus);
246 bus->ops.bus_reset(bus);
248 goto again;
250 /* clear reset-flag when the communication gets recovered */
251 if (!err || codec_in_pm(codec))
252 bus->response_reset = 0;
253 return err;
257 * snd_hda_codec_read - send a command and get the response
258 * @codec: the HDA codec
259 * @nid: NID to send the command
260 * @flags: optional bit flags
261 * @verb: the verb to send
262 * @parm: the parameter for the verb
264 * Send a single command and read the corresponding response.
266 * Returns the obtained response value, or -1 for an error.
268 unsigned int snd_hda_codec_read(struct hda_codec *codec, hda_nid_t nid,
269 int flags,
270 unsigned int verb, unsigned int parm)
272 unsigned cmd = make_codec_cmd(codec, nid, flags, verb, parm);
273 unsigned int res;
274 if (codec_exec_verb(codec, cmd, flags, &res))
275 return -1;
276 return res;
278 EXPORT_SYMBOL_HDA(snd_hda_codec_read);
281 * snd_hda_codec_write - send a single command without waiting for response
282 * @codec: the HDA codec
283 * @nid: NID to send the command
284 * @flags: optional bit flags
285 * @verb: the verb to send
286 * @parm: the parameter for the verb
288 * Send a single command without waiting for response.
290 * Returns 0 if successful, or a negative error code.
292 int snd_hda_codec_write(struct hda_codec *codec, hda_nid_t nid, int flags,
293 unsigned int verb, unsigned int parm)
295 unsigned int cmd = make_codec_cmd(codec, nid, flags, verb, parm);
296 unsigned int res;
297 return codec_exec_verb(codec, cmd, flags,
298 codec->bus->sync_write ? &res : NULL);
300 EXPORT_SYMBOL_HDA(snd_hda_codec_write);
303 * snd_hda_sequence_write - sequence writes
304 * @codec: the HDA codec
305 * @seq: VERB array to send
307 * Send the commands sequentially from the given array.
308 * The array must be terminated with NID=0.
310 void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq)
312 for (; seq->nid; seq++)
313 snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param);
315 EXPORT_SYMBOL_HDA(snd_hda_sequence_write);
318 * snd_hda_get_sub_nodes - get the range of sub nodes
319 * @codec: the HDA codec
320 * @nid: NID to parse
321 * @start_id: the pointer to store the start NID
323 * Parse the NID and store the start NID of its sub-nodes.
324 * Returns the number of sub-nodes.
326 int snd_hda_get_sub_nodes(struct hda_codec *codec, hda_nid_t nid,
327 hda_nid_t *start_id)
329 unsigned int parm;
331 parm = snd_hda_param_read(codec, nid, AC_PAR_NODE_COUNT);
332 if (parm == -1)
333 return 0;
334 *start_id = (parm >> 16) & 0x7fff;
335 return (int)(parm & 0x7fff);
337 EXPORT_SYMBOL_HDA(snd_hda_get_sub_nodes);
339 /* connection list element */
340 struct hda_conn_list {
341 struct list_head list;
342 int len;
343 hda_nid_t nid;
344 hda_nid_t conns[0];
347 /* look up the cached results */
348 static struct hda_conn_list *
349 lookup_conn_list(struct hda_codec *codec, hda_nid_t nid)
351 struct hda_conn_list *p;
352 list_for_each_entry(p, &codec->conn_list, list) {
353 if (p->nid == nid)
354 return p;
356 return NULL;
359 static int add_conn_list(struct hda_codec *codec, hda_nid_t nid, int len,
360 const hda_nid_t *list)
362 struct hda_conn_list *p;
364 p = kmalloc(sizeof(*p) + len * sizeof(hda_nid_t), GFP_KERNEL);
365 if (!p)
366 return -ENOMEM;
367 p->len = len;
368 p->nid = nid;
369 memcpy(p->conns, list, len * sizeof(hda_nid_t));
370 list_add(&p->list, &codec->conn_list);
371 return 0;
374 static void remove_conn_list(struct hda_codec *codec)
376 while (!list_empty(&codec->conn_list)) {
377 struct hda_conn_list *p;
378 p = list_first_entry(&codec->conn_list, typeof(*p), list);
379 list_del(&p->list);
380 kfree(p);
384 /* read the connection and add to the cache */
385 static int read_and_add_raw_conns(struct hda_codec *codec, hda_nid_t nid)
387 hda_nid_t list[32];
388 hda_nid_t *result = list;
389 int len;
391 len = snd_hda_get_raw_connections(codec, nid, list, ARRAY_SIZE(list));
392 if (len == -ENOSPC) {
393 len = snd_hda_get_num_raw_conns(codec, nid);
394 result = kmalloc(sizeof(hda_nid_t) * len, GFP_KERNEL);
395 if (!result)
396 return -ENOMEM;
397 len = snd_hda_get_raw_connections(codec, nid, result, len);
399 if (len >= 0)
400 len = snd_hda_override_conn_list(codec, nid, len, result);
401 if (result != list)
402 kfree(result);
403 return len;
407 * snd_hda_get_conn_list - get connection list
408 * @codec: the HDA codec
409 * @nid: NID to parse
410 * @len: number of connection list entries
411 * @listp: the pointer to store NID list
413 * Parses the connection list of the given widget and stores the pointer
414 * to the list of NIDs.
416 * Returns the number of connections, or a negative error code.
418 * Note that the returned pointer isn't protected against the list
419 * modification. If snd_hda_override_conn_list() might be called
420 * concurrently, protect with a mutex appropriately.
422 int snd_hda_get_conn_list(struct hda_codec *codec, hda_nid_t nid,
423 const hda_nid_t **listp)
425 bool added = false;
427 for (;;) {
428 int err;
429 const struct hda_conn_list *p;
431 /* if the connection-list is already cached, read it */
432 p = lookup_conn_list(codec, nid);
433 if (p) {
434 if (listp)
435 *listp = p->conns;
436 return p->len;
438 if (snd_BUG_ON(added))
439 return -EINVAL;
441 err = read_and_add_raw_conns(codec, nid);
442 if (err < 0)
443 return err;
444 added = true;
447 EXPORT_SYMBOL_HDA(snd_hda_get_conn_list);
450 * snd_hda_get_connections - copy connection list
451 * @codec: the HDA codec
452 * @nid: NID to parse
453 * @conn_list: connection list array; when NULL, checks only the size
454 * @max_conns: max. number of connections to store
456 * Parses the connection list of the given widget and stores the list
457 * of NIDs.
459 * Returns the number of connections, or a negative error code.
461 int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
462 hda_nid_t *conn_list, int max_conns)
464 const hda_nid_t *list;
465 int len = snd_hda_get_conn_list(codec, nid, &list);
467 if (len > 0 && conn_list) {
468 if (len > max_conns) {
469 snd_printk(KERN_ERR "hda_codec: "
470 "Too many connections %d for NID 0x%x\n",
471 len, nid);
472 return -EINVAL;
474 memcpy(conn_list, list, len * sizeof(hda_nid_t));
477 return len;
479 EXPORT_SYMBOL_HDA(snd_hda_get_connections);
481 /* return CONNLIST_LEN parameter of the given widget */
482 static unsigned int get_num_conns(struct hda_codec *codec, hda_nid_t nid)
484 unsigned int wcaps = get_wcaps(codec, nid);
485 unsigned int parm;
487 if (!(wcaps & AC_WCAP_CONN_LIST) &&
488 get_wcaps_type(wcaps) != AC_WID_VOL_KNB)
489 return 0;
491 parm = snd_hda_param_read(codec, nid, AC_PAR_CONNLIST_LEN);
492 if (parm == -1)
493 parm = 0;
494 return parm;
497 int snd_hda_get_num_raw_conns(struct hda_codec *codec, hda_nid_t nid)
499 return snd_hda_get_raw_connections(codec, nid, NULL, 0);
503 * snd_hda_get_raw_connections - copy connection list without cache
504 * @codec: the HDA codec
505 * @nid: NID to parse
506 * @conn_list: connection list array
507 * @max_conns: max. number of connections to store
509 * Like snd_hda_get_connections(), copy the connection list but without
510 * checking through the connection-list cache.
511 * Currently called only from hda_proc.c, so not exported.
513 int snd_hda_get_raw_connections(struct hda_codec *codec, hda_nid_t nid,
514 hda_nid_t *conn_list, int max_conns)
516 unsigned int parm;
517 int i, conn_len, conns;
518 unsigned int shift, num_elems, mask;
519 hda_nid_t prev_nid;
520 int null_count = 0;
522 parm = get_num_conns(codec, nid);
523 if (!parm)
524 return 0;
526 if (parm & AC_CLIST_LONG) {
527 /* long form */
528 shift = 16;
529 num_elems = 2;
530 } else {
531 /* short form */
532 shift = 8;
533 num_elems = 4;
535 conn_len = parm & AC_CLIST_LENGTH;
536 mask = (1 << (shift-1)) - 1;
538 if (!conn_len)
539 return 0; /* no connection */
541 if (conn_len == 1) {
542 /* single connection */
543 parm = snd_hda_codec_read(codec, nid, 0,
544 AC_VERB_GET_CONNECT_LIST, 0);
545 if (parm == -1 && codec->bus->rirb_error)
546 return -EIO;
547 if (conn_list)
548 conn_list[0] = parm & mask;
549 return 1;
552 /* multi connection */
553 conns = 0;
554 prev_nid = 0;
555 for (i = 0; i < conn_len; i++) {
556 int range_val;
557 hda_nid_t val, n;
559 if (i % num_elems == 0) {
560 parm = snd_hda_codec_read(codec, nid, 0,
561 AC_VERB_GET_CONNECT_LIST, i);
562 if (parm == -1 && codec->bus->rirb_error)
563 return -EIO;
565 range_val = !!(parm & (1 << (shift-1))); /* ranges */
566 val = parm & mask;
567 if (val == 0 && null_count++) { /* no second chance */
568 snd_printk(KERN_WARNING "hda_codec: "
569 "invalid CONNECT_LIST verb %x[%i]:%x\n",
570 nid, i, parm);
571 return 0;
573 parm >>= shift;
574 if (range_val) {
575 /* ranges between the previous and this one */
576 if (!prev_nid || prev_nid >= val) {
577 snd_printk(KERN_WARNING "hda_codec: "
578 "invalid dep_range_val %x:%x\n",
579 prev_nid, val);
580 continue;
582 for (n = prev_nid + 1; n <= val; n++) {
583 if (conn_list) {
584 if (conns >= max_conns)
585 return -ENOSPC;
586 conn_list[conns] = n;
588 conns++;
590 } else {
591 if (conn_list) {
592 if (conns >= max_conns)
593 return -ENOSPC;
594 conn_list[conns] = val;
596 conns++;
598 prev_nid = val;
600 return conns;
604 * snd_hda_override_conn_list - add/modify the connection-list to cache
605 * @codec: the HDA codec
606 * @nid: NID to parse
607 * @len: number of connection list entries
608 * @list: the list of connection entries
610 * Add or modify the given connection-list to the cache. If the corresponding
611 * cache already exists, invalidate it and append a new one.
613 * Returns zero or a negative error code.
615 int snd_hda_override_conn_list(struct hda_codec *codec, hda_nid_t nid, int len,
616 const hda_nid_t *list)
618 struct hda_conn_list *p;
620 p = lookup_conn_list(codec, nid);
621 if (p) {
622 list_del(&p->list);
623 kfree(p);
626 return add_conn_list(codec, nid, len, list);
628 EXPORT_SYMBOL_HDA(snd_hda_override_conn_list);
631 * snd_hda_get_conn_index - get the connection index of the given NID
632 * @codec: the HDA codec
633 * @mux: NID containing the list
634 * @nid: NID to select
635 * @recursive: 1 when searching NID recursively, otherwise 0
637 * Parses the connection list of the widget @mux and checks whether the
638 * widget @nid is present. If it is, return the connection index.
639 * Otherwise it returns -1.
641 int snd_hda_get_conn_index(struct hda_codec *codec, hda_nid_t mux,
642 hda_nid_t nid, int recursive)
644 const hda_nid_t *conn;
645 int i, nums;
647 nums = snd_hda_get_conn_list(codec, mux, &conn);
648 for (i = 0; i < nums; i++)
649 if (conn[i] == nid)
650 return i;
651 if (!recursive)
652 return -1;
653 if (recursive > 10) {
654 snd_printd("hda_codec: too deep connection for 0x%x\n", nid);
655 return -1;
657 recursive++;
658 for (i = 0; i < nums; i++) {
659 unsigned int type = get_wcaps_type(get_wcaps(codec, conn[i]));
660 if (type == AC_WID_PIN || type == AC_WID_AUD_OUT)
661 continue;
662 if (snd_hda_get_conn_index(codec, conn[i], nid, recursive) >= 0)
663 return i;
665 return -1;
667 EXPORT_SYMBOL_HDA(snd_hda_get_conn_index);
670 * snd_hda_queue_unsol_event - add an unsolicited event to queue
671 * @bus: the BUS
672 * @res: unsolicited event (lower 32bit of RIRB entry)
673 * @res_ex: codec addr and flags (upper 32bit or RIRB entry)
675 * Adds the given event to the queue. The events are processed in
676 * the workqueue asynchronously. Call this function in the interrupt
677 * hanlder when RIRB receives an unsolicited event.
679 * Returns 0 if successful, or a negative error code.
681 int snd_hda_queue_unsol_event(struct hda_bus *bus, u32 res, u32 res_ex)
683 struct hda_bus_unsolicited *unsol;
684 unsigned int wp;
686 if (!bus || !bus->workq)
687 return 0;
689 trace_hda_unsol_event(bus, res, res_ex);
690 unsol = bus->unsol;
691 if (!unsol)
692 return 0;
694 wp = (unsol->wp + 1) % HDA_UNSOL_QUEUE_SIZE;
695 unsol->wp = wp;
697 wp <<= 1;
698 unsol->queue[wp] = res;
699 unsol->queue[wp + 1] = res_ex;
701 queue_work(bus->workq, &unsol->work);
703 return 0;
705 EXPORT_SYMBOL_HDA(snd_hda_queue_unsol_event);
708 * process queued unsolicited events
710 static void process_unsol_events(struct work_struct *work)
712 struct hda_bus_unsolicited *unsol =
713 container_of(work, struct hda_bus_unsolicited, work);
714 struct hda_bus *bus = unsol->bus;
715 struct hda_codec *codec;
716 unsigned int rp, caddr, res;
718 while (unsol->rp != unsol->wp) {
719 rp = (unsol->rp + 1) % HDA_UNSOL_QUEUE_SIZE;
720 unsol->rp = rp;
721 rp <<= 1;
722 res = unsol->queue[rp];
723 caddr = unsol->queue[rp + 1];
724 if (!(caddr & (1 << 4))) /* no unsolicited event? */
725 continue;
726 codec = bus->caddr_tbl[caddr & 0x0f];
727 if (codec && codec->patch_ops.unsol_event)
728 codec->patch_ops.unsol_event(codec, res);
733 * initialize unsolicited queue
735 static int init_unsol_queue(struct hda_bus *bus)
737 struct hda_bus_unsolicited *unsol;
739 if (bus->unsol) /* already initialized */
740 return 0;
742 unsol = kzalloc(sizeof(*unsol), GFP_KERNEL);
743 if (!unsol) {
744 snd_printk(KERN_ERR "hda_codec: "
745 "can't allocate unsolicited queue\n");
746 return -ENOMEM;
748 INIT_WORK(&unsol->work, process_unsol_events);
749 unsol->bus = bus;
750 bus->unsol = unsol;
751 return 0;
755 * destructor
757 static void snd_hda_codec_free(struct hda_codec *codec);
759 static int snd_hda_bus_free(struct hda_bus *bus)
761 struct hda_codec *codec, *n;
763 if (!bus)
764 return 0;
765 if (bus->workq)
766 flush_workqueue(bus->workq);
767 if (bus->unsol)
768 kfree(bus->unsol);
769 list_for_each_entry_safe(codec, n, &bus->codec_list, list) {
770 snd_hda_codec_free(codec);
772 if (bus->ops.private_free)
773 bus->ops.private_free(bus);
774 if (bus->workq)
775 destroy_workqueue(bus->workq);
776 kfree(bus);
777 return 0;
780 static int snd_hda_bus_dev_free(struct snd_device *device)
782 struct hda_bus *bus = device->device_data;
783 bus->shutdown = 1;
784 return snd_hda_bus_free(bus);
787 #ifdef CONFIG_SND_HDA_HWDEP
788 static int snd_hda_bus_dev_register(struct snd_device *device)
790 struct hda_bus *bus = device->device_data;
791 struct hda_codec *codec;
792 list_for_each_entry(codec, &bus->codec_list, list) {
793 snd_hda_hwdep_add_sysfs(codec);
794 snd_hda_hwdep_add_power_sysfs(codec);
796 return 0;
798 #else
799 #define snd_hda_bus_dev_register NULL
800 #endif
803 * snd_hda_bus_new - create a HDA bus
804 * @card: the card entry
805 * @temp: the template for hda_bus information
806 * @busp: the pointer to store the created bus instance
808 * Returns 0 if successful, or a negative error code.
810 int snd_hda_bus_new(struct snd_card *card,
811 const struct hda_bus_template *temp,
812 struct hda_bus **busp)
814 struct hda_bus *bus;
815 int err;
816 static struct snd_device_ops dev_ops = {
817 .dev_register = snd_hda_bus_dev_register,
818 .dev_free = snd_hda_bus_dev_free,
821 if (snd_BUG_ON(!temp))
822 return -EINVAL;
823 if (snd_BUG_ON(!temp->ops.command || !temp->ops.get_response))
824 return -EINVAL;
826 if (busp)
827 *busp = NULL;
829 bus = kzalloc(sizeof(*bus), GFP_KERNEL);
830 if (bus == NULL) {
831 snd_printk(KERN_ERR "can't allocate struct hda_bus\n");
832 return -ENOMEM;
835 bus->card = card;
836 bus->private_data = temp->private_data;
837 bus->pci = temp->pci;
838 bus->modelname = temp->modelname;
839 bus->power_save = temp->power_save;
840 bus->ops = temp->ops;
842 mutex_init(&bus->cmd_mutex);
843 mutex_init(&bus->prepare_mutex);
844 INIT_LIST_HEAD(&bus->codec_list);
846 snprintf(bus->workq_name, sizeof(bus->workq_name),
847 "hd-audio%d", card->number);
848 bus->workq = create_singlethread_workqueue(bus->workq_name);
849 if (!bus->workq) {
850 snd_printk(KERN_ERR "cannot create workqueue %s\n",
851 bus->workq_name);
852 kfree(bus);
853 return -ENOMEM;
856 err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops);
857 if (err < 0) {
858 snd_hda_bus_free(bus);
859 return err;
861 if (busp)
862 *busp = bus;
863 return 0;
865 EXPORT_SYMBOL_HDA(snd_hda_bus_new);
867 #ifdef CONFIG_SND_HDA_GENERIC
868 #define is_generic_config(codec) \
869 (codec->modelname && !strcmp(codec->modelname, "generic"))
870 #else
871 #define is_generic_config(codec) 0
872 #endif
874 #ifdef MODULE
875 #define HDA_MODREQ_MAX_COUNT 2 /* two request_modules()'s */
876 #else
877 #define HDA_MODREQ_MAX_COUNT 0 /* all presets are statically linked */
878 #endif
881 * find a matching codec preset
883 static const struct hda_codec_preset *
884 find_codec_preset(struct hda_codec *codec)
886 struct hda_codec_preset_list *tbl;
887 const struct hda_codec_preset *preset;
888 unsigned int mod_requested = 0;
890 if (is_generic_config(codec))
891 return NULL; /* use the generic parser */
893 again:
894 mutex_lock(&preset_mutex);
895 list_for_each_entry(tbl, &hda_preset_tables, list) {
896 if (!try_module_get(tbl->owner)) {
897 snd_printk(KERN_ERR "hda_codec: cannot module_get\n");
898 continue;
900 for (preset = tbl->preset; preset->id; preset++) {
901 u32 mask = preset->mask;
902 if (preset->afg && preset->afg != codec->afg)
903 continue;
904 if (preset->mfg && preset->mfg != codec->mfg)
905 continue;
906 if (!mask)
907 mask = ~0;
908 if (preset->id == (codec->vendor_id & mask) &&
909 (!preset->rev ||
910 preset->rev == codec->revision_id)) {
911 mutex_unlock(&preset_mutex);
912 codec->owner = tbl->owner;
913 return preset;
916 module_put(tbl->owner);
918 mutex_unlock(&preset_mutex);
920 if (mod_requested < HDA_MODREQ_MAX_COUNT) {
921 char name[32];
922 if (!mod_requested)
923 snprintf(name, sizeof(name), "snd-hda-codec-id:%08x",
924 codec->vendor_id);
925 else
926 snprintf(name, sizeof(name), "snd-hda-codec-id:%04x*",
927 (codec->vendor_id >> 16) & 0xffff);
928 request_module(name);
929 mod_requested++;
930 goto again;
932 return NULL;
936 * get_codec_name - store the codec name
938 static int get_codec_name(struct hda_codec *codec)
940 const struct hda_vendor_id *c;
941 const char *vendor = NULL;
942 u16 vendor_id = codec->vendor_id >> 16;
943 char tmp[16];
945 if (codec->vendor_name)
946 goto get_chip_name;
948 for (c = hda_vendor_ids; c->id; c++) {
949 if (c->id == vendor_id) {
950 vendor = c->name;
951 break;
954 if (!vendor) {
955 sprintf(tmp, "Generic %04x", vendor_id);
956 vendor = tmp;
958 codec->vendor_name = kstrdup(vendor, GFP_KERNEL);
959 if (!codec->vendor_name)
960 return -ENOMEM;
962 get_chip_name:
963 if (codec->chip_name)
964 return 0;
966 if (codec->preset && codec->preset->name)
967 codec->chip_name = kstrdup(codec->preset->name, GFP_KERNEL);
968 else {
969 sprintf(tmp, "ID %x", codec->vendor_id & 0xffff);
970 codec->chip_name = kstrdup(tmp, GFP_KERNEL);
972 if (!codec->chip_name)
973 return -ENOMEM;
974 return 0;
978 * look for an AFG and MFG nodes
980 static void setup_fg_nodes(struct hda_codec *codec)
982 int i, total_nodes, function_id;
983 hda_nid_t nid;
985 total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid);
986 for (i = 0; i < total_nodes; i++, nid++) {
987 function_id = snd_hda_param_read(codec, nid,
988 AC_PAR_FUNCTION_TYPE);
989 switch (function_id & 0xff) {
990 case AC_GRP_AUDIO_FUNCTION:
991 codec->afg = nid;
992 codec->afg_function_id = function_id & 0xff;
993 codec->afg_unsol = (function_id >> 8) & 1;
994 break;
995 case AC_GRP_MODEM_FUNCTION:
996 codec->mfg = nid;
997 codec->mfg_function_id = function_id & 0xff;
998 codec->mfg_unsol = (function_id >> 8) & 1;
999 break;
1000 default:
1001 break;
1007 * read widget caps for each widget and store in cache
1009 static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
1011 int i;
1012 hda_nid_t nid;
1014 codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node,
1015 &codec->start_nid);
1016 codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL);
1017 if (!codec->wcaps)
1018 return -ENOMEM;
1019 nid = codec->start_nid;
1020 for (i = 0; i < codec->num_nodes; i++, nid++)
1021 codec->wcaps[i] = snd_hda_param_read(codec, nid,
1022 AC_PAR_AUDIO_WIDGET_CAP);
1023 return 0;
1026 /* read all pin default configurations and save codec->init_pins */
1027 static int read_pin_defaults(struct hda_codec *codec)
1029 int i;
1030 hda_nid_t nid = codec->start_nid;
1032 for (i = 0; i < codec->num_nodes; i++, nid++) {
1033 struct hda_pincfg *pin;
1034 unsigned int wcaps = get_wcaps(codec, nid);
1035 unsigned int wid_type = get_wcaps_type(wcaps);
1036 if (wid_type != AC_WID_PIN)
1037 continue;
1038 pin = snd_array_new(&codec->init_pins);
1039 if (!pin)
1040 return -ENOMEM;
1041 pin->nid = nid;
1042 pin->cfg = snd_hda_codec_read(codec, nid, 0,
1043 AC_VERB_GET_CONFIG_DEFAULT, 0);
1044 pin->ctrl = snd_hda_codec_read(codec, nid, 0,
1045 AC_VERB_GET_PIN_WIDGET_CONTROL,
1048 return 0;
1051 /* look up the given pin config list and return the item matching with NID */
1052 static struct hda_pincfg *look_up_pincfg(struct hda_codec *codec,
1053 struct snd_array *array,
1054 hda_nid_t nid)
1056 int i;
1057 for (i = 0; i < array->used; i++) {
1058 struct hda_pincfg *pin = snd_array_elem(array, i);
1059 if (pin->nid == nid)
1060 return pin;
1062 return NULL;
1065 /* set the current pin config value for the given NID.
1066 * the value is cached, and read via snd_hda_codec_get_pincfg()
1068 int snd_hda_add_pincfg(struct hda_codec *codec, struct snd_array *list,
1069 hda_nid_t nid, unsigned int cfg)
1071 struct hda_pincfg *pin;
1073 /* the check below may be invalid when pins are added by a fixup
1074 * dynamically (e.g. via snd_hda_codec_update_widgets()), so disabled
1075 * for now
1078 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN)
1079 return -EINVAL;
1082 pin = look_up_pincfg(codec, list, nid);
1083 if (!pin) {
1084 pin = snd_array_new(list);
1085 if (!pin)
1086 return -ENOMEM;
1087 pin->nid = nid;
1089 pin->cfg = cfg;
1090 return 0;
1094 * snd_hda_codec_set_pincfg - Override a pin default configuration
1095 * @codec: the HDA codec
1096 * @nid: NID to set the pin config
1097 * @cfg: the pin default config value
1099 * Override a pin default configuration value in the cache.
1100 * This value can be read by snd_hda_codec_get_pincfg() in a higher
1101 * priority than the real hardware value.
1103 int snd_hda_codec_set_pincfg(struct hda_codec *codec,
1104 hda_nid_t nid, unsigned int cfg)
1106 return snd_hda_add_pincfg(codec, &codec->driver_pins, nid, cfg);
1108 EXPORT_SYMBOL_HDA(snd_hda_codec_set_pincfg);
1111 * snd_hda_codec_get_pincfg - Obtain a pin-default configuration
1112 * @codec: the HDA codec
1113 * @nid: NID to get the pin config
1115 * Get the current pin config value of the given pin NID.
1116 * If the pincfg value is cached or overridden via sysfs or driver,
1117 * returns the cached value.
1119 unsigned int snd_hda_codec_get_pincfg(struct hda_codec *codec, hda_nid_t nid)
1121 struct hda_pincfg *pin;
1123 #ifdef CONFIG_SND_HDA_HWDEP
1125 unsigned int cfg = 0;
1126 mutex_lock(&codec->user_mutex);
1127 pin = look_up_pincfg(codec, &codec->user_pins, nid);
1128 if (pin)
1129 cfg = pin->cfg;
1130 mutex_unlock(&codec->user_mutex);
1131 if (cfg)
1132 return cfg;
1134 #endif
1135 pin = look_up_pincfg(codec, &codec->driver_pins, nid);
1136 if (pin)
1137 return pin->cfg;
1138 pin = look_up_pincfg(codec, &codec->init_pins, nid);
1139 if (pin)
1140 return pin->cfg;
1141 return 0;
1143 EXPORT_SYMBOL_HDA(snd_hda_codec_get_pincfg);
1145 /* remember the current pinctl target value */
1146 int snd_hda_codec_set_pin_target(struct hda_codec *codec, hda_nid_t nid,
1147 unsigned int val)
1149 struct hda_pincfg *pin;
1151 pin = look_up_pincfg(codec, &codec->init_pins, nid);
1152 if (!pin)
1153 return -EINVAL;
1154 pin->target = val;
1155 return 0;
1157 EXPORT_SYMBOL_HDA(snd_hda_codec_set_pin_target);
1159 /* return the current pinctl target value */
1160 int snd_hda_codec_get_pin_target(struct hda_codec *codec, hda_nid_t nid)
1162 struct hda_pincfg *pin;
1164 pin = look_up_pincfg(codec, &codec->init_pins, nid);
1165 if (!pin)
1166 return 0;
1167 return pin->target;
1169 EXPORT_SYMBOL_HDA(snd_hda_codec_get_pin_target);
1172 * snd_hda_shutup_pins - Shut up all pins
1173 * @codec: the HDA codec
1175 * Clear all pin controls to shup up before suspend for avoiding click noise.
1176 * The controls aren't cached so that they can be resumed properly.
1178 void snd_hda_shutup_pins(struct hda_codec *codec)
1180 int i;
1181 /* don't shut up pins when unloading the driver; otherwise it breaks
1182 * the default pin setup at the next load of the driver
1184 if (codec->bus->shutdown)
1185 return;
1186 for (i = 0; i < codec->init_pins.used; i++) {
1187 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1188 /* use read here for syncing after issuing each verb */
1189 snd_hda_codec_read(codec, pin->nid, 0,
1190 AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
1192 codec->pins_shutup = 1;
1194 EXPORT_SYMBOL_HDA(snd_hda_shutup_pins);
1196 #ifdef CONFIG_PM
1197 /* Restore the pin controls cleared previously via snd_hda_shutup_pins() */
1198 static void restore_shutup_pins(struct hda_codec *codec)
1200 int i;
1201 if (!codec->pins_shutup)
1202 return;
1203 if (codec->bus->shutdown)
1204 return;
1205 for (i = 0; i < codec->init_pins.used; i++) {
1206 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1207 snd_hda_codec_write(codec, pin->nid, 0,
1208 AC_VERB_SET_PIN_WIDGET_CONTROL,
1209 pin->ctrl);
1211 codec->pins_shutup = 0;
1213 #endif
1215 static void hda_jackpoll_work(struct work_struct *work)
1217 struct hda_codec *codec =
1218 container_of(work, struct hda_codec, jackpoll_work.work);
1219 if (!codec->jackpoll_interval)
1220 return;
1222 snd_hda_jack_set_dirty_all(codec);
1223 snd_hda_jack_poll_all(codec);
1224 queue_delayed_work(codec->bus->workq, &codec->jackpoll_work,
1225 codec->jackpoll_interval);
1228 static void init_hda_cache(struct hda_cache_rec *cache,
1229 unsigned int record_size);
1230 static void free_hda_cache(struct hda_cache_rec *cache);
1232 /* release all pincfg lists */
1233 static void free_init_pincfgs(struct hda_codec *codec)
1235 snd_array_free(&codec->driver_pins);
1236 #ifdef CONFIG_SND_HDA_HWDEP
1237 snd_array_free(&codec->user_pins);
1238 #endif
1239 snd_array_free(&codec->init_pins);
1243 * audio-converter setup caches
1245 struct hda_cvt_setup {
1246 hda_nid_t nid;
1247 u8 stream_tag;
1248 u8 channel_id;
1249 u16 format_id;
1250 unsigned char active; /* cvt is currently used */
1251 unsigned char dirty; /* setups should be cleared */
1254 /* get or create a cache entry for the given audio converter NID */
1255 static struct hda_cvt_setup *
1256 get_hda_cvt_setup(struct hda_codec *codec, hda_nid_t nid)
1258 struct hda_cvt_setup *p;
1259 int i;
1261 for (i = 0; i < codec->cvt_setups.used; i++) {
1262 p = snd_array_elem(&codec->cvt_setups, i);
1263 if (p->nid == nid)
1264 return p;
1266 p = snd_array_new(&codec->cvt_setups);
1267 if (p)
1268 p->nid = nid;
1269 return p;
1273 * codec destructor
1275 static void snd_hda_codec_free(struct hda_codec *codec)
1277 if (!codec)
1278 return;
1279 cancel_delayed_work_sync(&codec->jackpoll_work);
1280 snd_hda_jack_tbl_clear(codec);
1281 free_init_pincfgs(codec);
1282 #ifdef CONFIG_PM
1283 cancel_delayed_work(&codec->power_work);
1284 flush_workqueue(codec->bus->workq);
1285 #endif
1286 list_del(&codec->list);
1287 snd_array_free(&codec->mixers);
1288 snd_array_free(&codec->nids);
1289 snd_array_free(&codec->cvt_setups);
1290 snd_array_free(&codec->spdif_out);
1291 remove_conn_list(codec);
1292 codec->bus->caddr_tbl[codec->addr] = NULL;
1293 if (codec->patch_ops.free)
1294 codec->patch_ops.free(codec);
1295 #ifdef CONFIG_PM
1296 if (!codec->pm_down_notified) /* cancel leftover refcounts */
1297 hda_call_pm_notify(codec->bus, false);
1298 #endif
1299 module_put(codec->owner);
1300 free_hda_cache(&codec->amp_cache);
1301 free_hda_cache(&codec->cmd_cache);
1302 kfree(codec->vendor_name);
1303 kfree(codec->chip_name);
1304 kfree(codec->modelname);
1305 kfree(codec->wcaps);
1306 kfree(codec);
1309 static bool snd_hda_codec_get_supported_ps(struct hda_codec *codec,
1310 hda_nid_t fg, unsigned int power_state);
1312 static unsigned int hda_set_power_state(struct hda_codec *codec,
1313 unsigned int power_state);
1316 * snd_hda_codec_new - create a HDA codec
1317 * @bus: the bus to assign
1318 * @codec_addr: the codec address
1319 * @codecp: the pointer to store the generated codec
1321 * Returns 0 if successful, or a negative error code.
1323 int snd_hda_codec_new(struct hda_bus *bus,
1324 unsigned int codec_addr,
1325 struct hda_codec **codecp)
1327 struct hda_codec *codec;
1328 char component[31];
1329 hda_nid_t fg;
1330 int err;
1332 if (snd_BUG_ON(!bus))
1333 return -EINVAL;
1334 if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS))
1335 return -EINVAL;
1337 if (bus->caddr_tbl[codec_addr]) {
1338 snd_printk(KERN_ERR "hda_codec: "
1339 "address 0x%x is already occupied\n", codec_addr);
1340 return -EBUSY;
1343 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
1344 if (codec == NULL) {
1345 snd_printk(KERN_ERR "can't allocate struct hda_codec\n");
1346 return -ENOMEM;
1349 codec->bus = bus;
1350 codec->addr = codec_addr;
1351 mutex_init(&codec->spdif_mutex);
1352 mutex_init(&codec->control_mutex);
1353 mutex_init(&codec->hash_mutex);
1354 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
1355 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
1356 snd_array_init(&codec->mixers, sizeof(struct hda_nid_item), 32);
1357 snd_array_init(&codec->nids, sizeof(struct hda_nid_item), 32);
1358 snd_array_init(&codec->init_pins, sizeof(struct hda_pincfg), 16);
1359 snd_array_init(&codec->driver_pins, sizeof(struct hda_pincfg), 16);
1360 snd_array_init(&codec->cvt_setups, sizeof(struct hda_cvt_setup), 8);
1361 snd_array_init(&codec->spdif_out, sizeof(struct hda_spdif_out), 16);
1362 snd_array_init(&codec->jacktbl, sizeof(struct hda_jack_tbl), 16);
1363 snd_array_init(&codec->verbs, sizeof(struct hda_verb *), 8);
1364 INIT_LIST_HEAD(&codec->conn_list);
1366 INIT_DELAYED_WORK(&codec->jackpoll_work, hda_jackpoll_work);
1368 #ifdef CONFIG_PM
1369 spin_lock_init(&codec->power_lock);
1370 INIT_DELAYED_WORK(&codec->power_work, hda_power_work);
1371 /* snd_hda_codec_new() marks the codec as power-up, and leave it as is.
1372 * the caller has to power down appropriatley after initialization
1373 * phase.
1375 hda_keep_power_on(codec);
1376 hda_call_pm_notify(bus, true);
1377 #endif
1379 if (codec->bus->modelname) {
1380 codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL);
1381 if (!codec->modelname) {
1382 snd_hda_codec_free(codec);
1383 return -ENODEV;
1387 list_add_tail(&codec->list, &bus->codec_list);
1388 bus->caddr_tbl[codec_addr] = codec;
1390 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1391 AC_PAR_VENDOR_ID);
1392 if (codec->vendor_id == -1)
1393 /* read again, hopefully the access method was corrected
1394 * in the last read...
1396 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1397 AC_PAR_VENDOR_ID);
1398 codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1399 AC_PAR_SUBSYSTEM_ID);
1400 codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1401 AC_PAR_REV_ID);
1403 setup_fg_nodes(codec);
1404 if (!codec->afg && !codec->mfg) {
1405 snd_printdd("hda_codec: no AFG or MFG node found\n");
1406 err = -ENODEV;
1407 goto error;
1410 fg = codec->afg ? codec->afg : codec->mfg;
1411 err = read_widget_caps(codec, fg);
1412 if (err < 0) {
1413 snd_printk(KERN_ERR "hda_codec: cannot malloc\n");
1414 goto error;
1416 err = read_pin_defaults(codec);
1417 if (err < 0)
1418 goto error;
1420 if (!codec->subsystem_id) {
1421 codec->subsystem_id =
1422 snd_hda_codec_read(codec, fg, 0,
1423 AC_VERB_GET_SUBSYSTEM_ID, 0);
1426 #ifdef CONFIG_PM
1427 codec->d3_stop_clk = snd_hda_codec_get_supported_ps(codec, fg,
1428 AC_PWRST_CLKSTOP);
1429 if (!codec->d3_stop_clk)
1430 bus->power_keep_link_on = 1;
1431 #endif
1432 codec->epss = snd_hda_codec_get_supported_ps(codec, fg,
1433 AC_PWRST_EPSS);
1435 /* power-up all before initialization */
1436 hda_set_power_state(codec, AC_PWRST_D0);
1438 snd_hda_codec_proc_new(codec);
1440 snd_hda_create_hwdep(codec);
1442 sprintf(component, "HDA:%08x,%08x,%08x", codec->vendor_id,
1443 codec->subsystem_id, codec->revision_id);
1444 snd_component_add(codec->bus->card, component);
1446 if (codecp)
1447 *codecp = codec;
1448 return 0;
1450 error:
1451 snd_hda_codec_free(codec);
1452 return err;
1454 EXPORT_SYMBOL_HDA(snd_hda_codec_new);
1456 int snd_hda_codec_update_widgets(struct hda_codec *codec)
1458 hda_nid_t fg;
1459 int err;
1461 /* Assume the function group node does not change,
1462 * only the widget nodes may change.
1464 kfree(codec->wcaps);
1465 fg = codec->afg ? codec->afg : codec->mfg;
1466 err = read_widget_caps(codec, fg);
1467 if (err < 0) {
1468 snd_printk(KERN_ERR "hda_codec: cannot malloc\n");
1469 return err;
1472 snd_array_free(&codec->init_pins);
1473 err = read_pin_defaults(codec);
1475 return err;
1477 EXPORT_SYMBOL_HDA(snd_hda_codec_update_widgets);
1481 * snd_hda_codec_configure - (Re-)configure the HD-audio codec
1482 * @codec: the HDA codec
1484 * Start parsing of the given codec tree and (re-)initialize the whole
1485 * patch instance.
1487 * Returns 0 if successful or a negative error code.
1489 int snd_hda_codec_configure(struct hda_codec *codec)
1491 int err;
1493 codec->preset = find_codec_preset(codec);
1494 if (!codec->vendor_name || !codec->chip_name) {
1495 err = get_codec_name(codec);
1496 if (err < 0)
1497 return err;
1500 if (is_generic_config(codec)) {
1501 err = snd_hda_parse_generic_codec(codec);
1502 goto patched;
1504 if (codec->preset && codec->preset->patch) {
1505 err = codec->preset->patch(codec);
1506 goto patched;
1509 /* call the default parser */
1510 err = snd_hda_parse_generic_codec(codec);
1511 if (err < 0)
1512 printk(KERN_ERR "hda-codec: No codec parser is available\n");
1514 patched:
1515 if (!err && codec->patch_ops.unsol_event)
1516 err = init_unsol_queue(codec->bus);
1517 /* audio codec should override the mixer name */
1518 if (!err && (codec->afg || !*codec->bus->card->mixername))
1519 snprintf(codec->bus->card->mixername,
1520 sizeof(codec->bus->card->mixername),
1521 "%s %s", codec->vendor_name, codec->chip_name);
1522 return err;
1524 EXPORT_SYMBOL_HDA(snd_hda_codec_configure);
1526 /* update the stream-id if changed */
1527 static void update_pcm_stream_id(struct hda_codec *codec,
1528 struct hda_cvt_setup *p, hda_nid_t nid,
1529 u32 stream_tag, int channel_id)
1531 unsigned int oldval, newval;
1533 if (p->stream_tag != stream_tag || p->channel_id != channel_id) {
1534 oldval = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0);
1535 newval = (stream_tag << 4) | channel_id;
1536 if (oldval != newval)
1537 snd_hda_codec_write(codec, nid, 0,
1538 AC_VERB_SET_CHANNEL_STREAMID,
1539 newval);
1540 p->stream_tag = stream_tag;
1541 p->channel_id = channel_id;
1545 /* update the format-id if changed */
1546 static void update_pcm_format(struct hda_codec *codec, struct hda_cvt_setup *p,
1547 hda_nid_t nid, int format)
1549 unsigned int oldval;
1551 if (p->format_id != format) {
1552 oldval = snd_hda_codec_read(codec, nid, 0,
1553 AC_VERB_GET_STREAM_FORMAT, 0);
1554 if (oldval != format) {
1555 msleep(1);
1556 snd_hda_codec_write(codec, nid, 0,
1557 AC_VERB_SET_STREAM_FORMAT,
1558 format);
1560 p->format_id = format;
1565 * snd_hda_codec_setup_stream - set up the codec for streaming
1566 * @codec: the CODEC to set up
1567 * @nid: the NID to set up
1568 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
1569 * @channel_id: channel id to pass, zero based.
1570 * @format: stream format.
1572 void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
1573 u32 stream_tag,
1574 int channel_id, int format)
1576 struct hda_codec *c;
1577 struct hda_cvt_setup *p;
1578 int type;
1579 int i;
1581 if (!nid)
1582 return;
1584 snd_printdd("hda_codec_setup_stream: "
1585 "NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
1586 nid, stream_tag, channel_id, format);
1587 p = get_hda_cvt_setup(codec, nid);
1588 if (!p)
1589 return;
1591 if (codec->pcm_format_first)
1592 update_pcm_format(codec, p, nid, format);
1593 update_pcm_stream_id(codec, p, nid, stream_tag, channel_id);
1594 if (!codec->pcm_format_first)
1595 update_pcm_format(codec, p, nid, format);
1597 p->active = 1;
1598 p->dirty = 0;
1600 /* make other inactive cvts with the same stream-tag dirty */
1601 type = get_wcaps_type(get_wcaps(codec, nid));
1602 list_for_each_entry(c, &codec->bus->codec_list, list) {
1603 for (i = 0; i < c->cvt_setups.used; i++) {
1604 p = snd_array_elem(&c->cvt_setups, i);
1605 if (!p->active && p->stream_tag == stream_tag &&
1606 get_wcaps_type(get_wcaps(c, p->nid)) == type)
1607 p->dirty = 1;
1611 EXPORT_SYMBOL_HDA(snd_hda_codec_setup_stream);
1613 static void really_cleanup_stream(struct hda_codec *codec,
1614 struct hda_cvt_setup *q);
1617 * __snd_hda_codec_cleanup_stream - clean up the codec for closing
1618 * @codec: the CODEC to clean up
1619 * @nid: the NID to clean up
1620 * @do_now: really clean up the stream instead of clearing the active flag
1622 void __snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid,
1623 int do_now)
1625 struct hda_cvt_setup *p;
1627 if (!nid)
1628 return;
1630 if (codec->no_sticky_stream)
1631 do_now = 1;
1633 snd_printdd("hda_codec_cleanup_stream: NID=0x%x\n", nid);
1634 p = get_hda_cvt_setup(codec, nid);
1635 if (p) {
1636 /* here we just clear the active flag when do_now isn't set;
1637 * actual clean-ups will be done later in
1638 * purify_inactive_streams() called from snd_hda_codec_prpapre()
1640 if (do_now)
1641 really_cleanup_stream(codec, p);
1642 else
1643 p->active = 0;
1646 EXPORT_SYMBOL_HDA(__snd_hda_codec_cleanup_stream);
1648 static void really_cleanup_stream(struct hda_codec *codec,
1649 struct hda_cvt_setup *q)
1651 hda_nid_t nid = q->nid;
1652 if (q->stream_tag || q->channel_id)
1653 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
1654 if (q->format_id)
1655 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0
1657 memset(q, 0, sizeof(*q));
1658 q->nid = nid;
1661 /* clean up the all conflicting obsolete streams */
1662 static void purify_inactive_streams(struct hda_codec *codec)
1664 struct hda_codec *c;
1665 int i;
1667 list_for_each_entry(c, &codec->bus->codec_list, list) {
1668 for (i = 0; i < c->cvt_setups.used; i++) {
1669 struct hda_cvt_setup *p;
1670 p = snd_array_elem(&c->cvt_setups, i);
1671 if (p->dirty)
1672 really_cleanup_stream(c, p);
1677 #ifdef CONFIG_PM
1678 /* clean up all streams; called from suspend */
1679 static void hda_cleanup_all_streams(struct hda_codec *codec)
1681 int i;
1683 for (i = 0; i < codec->cvt_setups.used; i++) {
1684 struct hda_cvt_setup *p = snd_array_elem(&codec->cvt_setups, i);
1685 if (p->stream_tag)
1686 really_cleanup_stream(codec, p);
1689 #endif
1692 * amp access functions
1695 /* FIXME: more better hash key? */
1696 #define HDA_HASH_KEY(nid, dir, idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
1697 #define HDA_HASH_PINCAP_KEY(nid) (u32)((nid) + (0x02 << 24))
1698 #define HDA_HASH_PARPCM_KEY(nid) (u32)((nid) + (0x03 << 24))
1699 #define HDA_HASH_PARSTR_KEY(nid) (u32)((nid) + (0x04 << 24))
1700 #define INFO_AMP_CAPS (1<<0)
1701 #define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
1703 /* initialize the hash table */
1704 static void init_hda_cache(struct hda_cache_rec *cache,
1705 unsigned int record_size)
1707 memset(cache, 0, sizeof(*cache));
1708 memset(cache->hash, 0xff, sizeof(cache->hash));
1709 snd_array_init(&cache->buf, record_size, 64);
1712 static void free_hda_cache(struct hda_cache_rec *cache)
1714 snd_array_free(&cache->buf);
1717 /* query the hash. allocate an entry if not found. */
1718 static struct hda_cache_head *get_hash(struct hda_cache_rec *cache, u32 key)
1720 u16 idx = key % (u16)ARRAY_SIZE(cache->hash);
1721 u16 cur = cache->hash[idx];
1722 struct hda_cache_head *info;
1724 while (cur != 0xffff) {
1725 info = snd_array_elem(&cache->buf, cur);
1726 if (info->key == key)
1727 return info;
1728 cur = info->next;
1730 return NULL;
1733 /* query the hash. allocate an entry if not found. */
1734 static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache,
1735 u32 key)
1737 struct hda_cache_head *info = get_hash(cache, key);
1738 if (!info) {
1739 u16 idx, cur;
1740 /* add a new hash entry */
1741 info = snd_array_new(&cache->buf);
1742 if (!info)
1743 return NULL;
1744 cur = snd_array_index(&cache->buf, info);
1745 info->key = key;
1746 info->val = 0;
1747 info->dirty = 0;
1748 idx = key % (u16)ARRAY_SIZE(cache->hash);
1749 info->next = cache->hash[idx];
1750 cache->hash[idx] = cur;
1752 return info;
1755 /* query and allocate an amp hash entry */
1756 static inline struct hda_amp_info *
1757 get_alloc_amp_hash(struct hda_codec *codec, u32 key)
1759 return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key);
1762 /* overwrite the value with the key in the caps hash */
1763 static int write_caps_hash(struct hda_codec *codec, u32 key, unsigned int val)
1765 struct hda_amp_info *info;
1767 mutex_lock(&codec->hash_mutex);
1768 info = get_alloc_amp_hash(codec, key);
1769 if (!info) {
1770 mutex_unlock(&codec->hash_mutex);
1771 return -EINVAL;
1773 info->amp_caps = val;
1774 info->head.val |= INFO_AMP_CAPS;
1775 mutex_unlock(&codec->hash_mutex);
1776 return 0;
1779 /* query the value from the caps hash; if not found, fetch the current
1780 * value from the given function and store in the hash
1782 static unsigned int
1783 query_caps_hash(struct hda_codec *codec, hda_nid_t nid, int dir, u32 key,
1784 unsigned int (*func)(struct hda_codec *, hda_nid_t, int))
1786 struct hda_amp_info *info;
1787 unsigned int val;
1789 mutex_lock(&codec->hash_mutex);
1790 info = get_alloc_amp_hash(codec, key);
1791 if (!info) {
1792 mutex_unlock(&codec->hash_mutex);
1793 return 0;
1795 if (!(info->head.val & INFO_AMP_CAPS)) {
1796 mutex_unlock(&codec->hash_mutex); /* for reentrance */
1797 val = func(codec, nid, dir);
1798 write_caps_hash(codec, key, val);
1799 } else {
1800 val = info->amp_caps;
1801 mutex_unlock(&codec->hash_mutex);
1803 return val;
1806 static unsigned int read_amp_cap(struct hda_codec *codec, hda_nid_t nid,
1807 int direction)
1809 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
1810 nid = codec->afg;
1811 return snd_hda_param_read(codec, nid,
1812 direction == HDA_OUTPUT ?
1813 AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP);
1817 * query_amp_caps - query AMP capabilities
1818 * @codec: the HD-auio codec
1819 * @nid: the NID to query
1820 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1822 * Query AMP capabilities for the given widget and direction.
1823 * Returns the obtained capability bits.
1825 * When cap bits have been already read, this doesn't read again but
1826 * returns the cached value.
1828 u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
1830 return query_caps_hash(codec, nid, direction,
1831 HDA_HASH_KEY(nid, direction, 0),
1832 read_amp_cap);
1834 EXPORT_SYMBOL_HDA(query_amp_caps);
1837 * snd_hda_override_amp_caps - Override the AMP capabilities
1838 * @codec: the CODEC to clean up
1839 * @nid: the NID to clean up
1840 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1841 * @caps: the capability bits to set
1843 * Override the cached AMP caps bits value by the given one.
1844 * This function is useful if the driver needs to adjust the AMP ranges,
1845 * e.g. limit to 0dB, etc.
1847 * Returns zero if successful or a negative error code.
1849 int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
1850 unsigned int caps)
1852 return write_caps_hash(codec, HDA_HASH_KEY(nid, dir, 0), caps);
1854 EXPORT_SYMBOL_HDA(snd_hda_override_amp_caps);
1856 static unsigned int read_pin_cap(struct hda_codec *codec, hda_nid_t nid,
1857 int dir)
1859 return snd_hda_param_read(codec, nid, AC_PAR_PIN_CAP);
1863 * snd_hda_query_pin_caps - Query PIN capabilities
1864 * @codec: the HD-auio codec
1865 * @nid: the NID to query
1867 * Query PIN capabilities for the given widget.
1868 * Returns the obtained capability bits.
1870 * When cap bits have been already read, this doesn't read again but
1871 * returns the cached value.
1873 u32 snd_hda_query_pin_caps(struct hda_codec *codec, hda_nid_t nid)
1875 return query_caps_hash(codec, nid, 0, HDA_HASH_PINCAP_KEY(nid),
1876 read_pin_cap);
1878 EXPORT_SYMBOL_HDA(snd_hda_query_pin_caps);
1881 * snd_hda_override_pin_caps - Override the pin capabilities
1882 * @codec: the CODEC
1883 * @nid: the NID to override
1884 * @caps: the capability bits to set
1886 * Override the cached PIN capabilitiy bits value by the given one.
1888 * Returns zero if successful or a negative error code.
1890 int snd_hda_override_pin_caps(struct hda_codec *codec, hda_nid_t nid,
1891 unsigned int caps)
1893 return write_caps_hash(codec, HDA_HASH_PINCAP_KEY(nid), caps);
1895 EXPORT_SYMBOL_HDA(snd_hda_override_pin_caps);
1897 /* read or sync the hash value with the current value;
1898 * call within hash_mutex
1900 static struct hda_amp_info *
1901 update_amp_hash(struct hda_codec *codec, hda_nid_t nid, int ch,
1902 int direction, int index, bool init_only)
1904 struct hda_amp_info *info;
1905 unsigned int parm, val = 0;
1906 bool val_read = false;
1908 retry:
1909 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
1910 if (!info)
1911 return NULL;
1912 if (!(info->head.val & INFO_AMP_VOL(ch))) {
1913 if (!val_read) {
1914 mutex_unlock(&codec->hash_mutex);
1915 parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
1916 parm |= direction == HDA_OUTPUT ?
1917 AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
1918 parm |= index;
1919 val = snd_hda_codec_read(codec, nid, 0,
1920 AC_VERB_GET_AMP_GAIN_MUTE, parm);
1921 val &= 0xff;
1922 val_read = true;
1923 mutex_lock(&codec->hash_mutex);
1924 goto retry;
1926 info->vol[ch] = val;
1927 info->head.val |= INFO_AMP_VOL(ch);
1928 } else if (init_only)
1929 return NULL;
1930 return info;
1934 * write the current volume in info to the h/w
1936 static void put_vol_mute(struct hda_codec *codec, unsigned int amp_caps,
1937 hda_nid_t nid, int ch, int direction, int index,
1938 int val)
1940 u32 parm;
1942 parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
1943 parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
1944 parm |= index << AC_AMP_SET_INDEX_SHIFT;
1945 if ((val & HDA_AMP_MUTE) && !(amp_caps & AC_AMPCAP_MUTE) &&
1946 (amp_caps & AC_AMPCAP_MIN_MUTE))
1947 ; /* set the zero value as a fake mute */
1948 else
1949 parm |= val;
1950 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
1954 * snd_hda_codec_amp_read - Read AMP value
1955 * @codec: HD-audio codec
1956 * @nid: NID to read the AMP value
1957 * @ch: channel (left=0 or right=1)
1958 * @direction: #HDA_INPUT or #HDA_OUTPUT
1959 * @index: the index value (only for input direction)
1961 * Read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
1963 int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
1964 int direction, int index)
1966 struct hda_amp_info *info;
1967 unsigned int val = 0;
1969 mutex_lock(&codec->hash_mutex);
1970 info = update_amp_hash(codec, nid, ch, direction, index, false);
1971 if (info)
1972 val = info->vol[ch];
1973 mutex_unlock(&codec->hash_mutex);
1974 return val;
1976 EXPORT_SYMBOL_HDA(snd_hda_codec_amp_read);
1978 static int codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
1979 int direction, int idx, int mask, int val,
1980 bool init_only)
1982 struct hda_amp_info *info;
1983 unsigned int caps;
1984 unsigned int cache_only;
1986 if (snd_BUG_ON(mask & ~0xff))
1987 mask &= 0xff;
1988 val &= mask;
1990 mutex_lock(&codec->hash_mutex);
1991 info = update_amp_hash(codec, nid, ch, direction, idx, init_only);
1992 if (!info) {
1993 mutex_unlock(&codec->hash_mutex);
1994 return 0;
1996 val |= info->vol[ch] & ~mask;
1997 if (info->vol[ch] == val) {
1998 mutex_unlock(&codec->hash_mutex);
1999 return 0;
2001 info->vol[ch] = val;
2002 cache_only = info->head.dirty = codec->cached_write;
2003 caps = info->amp_caps;
2004 mutex_unlock(&codec->hash_mutex);
2005 if (!cache_only)
2006 put_vol_mute(codec, caps, nid, ch, direction, idx, val);
2007 return 1;
2011 * snd_hda_codec_amp_update - update the AMP value
2012 * @codec: HD-audio codec
2013 * @nid: NID to read the AMP value
2014 * @ch: channel (left=0 or right=1)
2015 * @direction: #HDA_INPUT or #HDA_OUTPUT
2016 * @idx: the index value (only for input direction)
2017 * @mask: bit mask to set
2018 * @val: the bits value to set
2020 * Update the AMP value with a bit mask.
2021 * Returns 0 if the value is unchanged, 1 if changed.
2023 int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
2024 int direction, int idx, int mask, int val)
2026 return codec_amp_update(codec, nid, ch, direction, idx, mask, val, false);
2028 EXPORT_SYMBOL_HDA(snd_hda_codec_amp_update);
2031 * snd_hda_codec_amp_stereo - update the AMP stereo values
2032 * @codec: HD-audio codec
2033 * @nid: NID to read the AMP value
2034 * @direction: #HDA_INPUT or #HDA_OUTPUT
2035 * @idx: the index value (only for input direction)
2036 * @mask: bit mask to set
2037 * @val: the bits value to set
2039 * Update the AMP values like snd_hda_codec_amp_update(), but for a
2040 * stereo widget with the same mask and value.
2042 int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
2043 int direction, int idx, int mask, int val)
2045 int ch, ret = 0;
2047 if (snd_BUG_ON(mask & ~0xff))
2048 mask &= 0xff;
2049 for (ch = 0; ch < 2; ch++)
2050 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
2051 idx, mask, val);
2052 return ret;
2054 EXPORT_SYMBOL_HDA(snd_hda_codec_amp_stereo);
2056 /* Works like snd_hda_codec_amp_update() but it writes the value only at
2057 * the first access. If the amp was already initialized / updated beforehand,
2058 * this does nothing.
2060 int snd_hda_codec_amp_init(struct hda_codec *codec, hda_nid_t nid, int ch,
2061 int dir, int idx, int mask, int val)
2063 return codec_amp_update(codec, nid, ch, dir, idx, mask, val, true);
2065 EXPORT_SYMBOL_HDA(snd_hda_codec_amp_init);
2067 int snd_hda_codec_amp_init_stereo(struct hda_codec *codec, hda_nid_t nid,
2068 int dir, int idx, int mask, int val)
2070 int ch, ret = 0;
2072 if (snd_BUG_ON(mask & ~0xff))
2073 mask &= 0xff;
2074 for (ch = 0; ch < 2; ch++)
2075 ret |= snd_hda_codec_amp_init(codec, nid, ch, dir,
2076 idx, mask, val);
2077 return ret;
2079 EXPORT_SYMBOL_HDA(snd_hda_codec_amp_init_stereo);
2082 * snd_hda_codec_resume_amp - Resume all AMP commands from the cache
2083 * @codec: HD-audio codec
2085 * Resume the all amp commands from the cache.
2087 void snd_hda_codec_resume_amp(struct hda_codec *codec)
2089 int i;
2091 mutex_lock(&codec->hash_mutex);
2092 codec->cached_write = 0;
2093 for (i = 0; i < codec->amp_cache.buf.used; i++) {
2094 struct hda_amp_info *buffer;
2095 u32 key;
2096 hda_nid_t nid;
2097 unsigned int idx, dir, ch;
2098 struct hda_amp_info info;
2100 buffer = snd_array_elem(&codec->amp_cache.buf, i);
2101 if (!buffer->head.dirty)
2102 continue;
2103 buffer->head.dirty = 0;
2104 info = *buffer;
2105 key = info.head.key;
2106 if (!key)
2107 continue;
2108 nid = key & 0xff;
2109 idx = (key >> 16) & 0xff;
2110 dir = (key >> 24) & 0xff;
2111 for (ch = 0; ch < 2; ch++) {
2112 if (!(info.head.val & INFO_AMP_VOL(ch)))
2113 continue;
2114 mutex_unlock(&codec->hash_mutex);
2115 put_vol_mute(codec, info.amp_caps, nid, ch, dir, idx,
2116 info.vol[ch]);
2117 mutex_lock(&codec->hash_mutex);
2120 mutex_unlock(&codec->hash_mutex);
2122 EXPORT_SYMBOL_HDA(snd_hda_codec_resume_amp);
2124 static u32 get_amp_max_value(struct hda_codec *codec, hda_nid_t nid, int dir,
2125 unsigned int ofs)
2127 u32 caps = query_amp_caps(codec, nid, dir);
2128 /* get num steps */
2129 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
2130 if (ofs < caps)
2131 caps -= ofs;
2132 return caps;
2136 * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer
2138 * The control element is supposed to have the private_value field
2139 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2141 int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
2142 struct snd_ctl_elem_info *uinfo)
2144 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2145 u16 nid = get_amp_nid(kcontrol);
2146 u8 chs = get_amp_channels(kcontrol);
2147 int dir = get_amp_direction(kcontrol);
2148 unsigned int ofs = get_amp_offset(kcontrol);
2150 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2151 uinfo->count = chs == 3 ? 2 : 1;
2152 uinfo->value.integer.min = 0;
2153 uinfo->value.integer.max = get_amp_max_value(codec, nid, dir, ofs);
2154 if (!uinfo->value.integer.max) {
2155 printk(KERN_WARNING "hda_codec: "
2156 "num_steps = 0 for NID=0x%x (ctl = %s)\n", nid,
2157 kcontrol->id.name);
2158 return -EINVAL;
2160 return 0;
2162 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_info);
2165 static inline unsigned int
2166 read_amp_value(struct hda_codec *codec, hda_nid_t nid,
2167 int ch, int dir, int idx, unsigned int ofs)
2169 unsigned int val;
2170 val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
2171 val &= HDA_AMP_VOLMASK;
2172 if (val >= ofs)
2173 val -= ofs;
2174 else
2175 val = 0;
2176 return val;
2179 static inline int
2180 update_amp_value(struct hda_codec *codec, hda_nid_t nid,
2181 int ch, int dir, int idx, unsigned int ofs,
2182 unsigned int val)
2184 unsigned int maxval;
2186 if (val > 0)
2187 val += ofs;
2188 /* ofs = 0: raw max value */
2189 maxval = get_amp_max_value(codec, nid, dir, 0);
2190 if (val > maxval)
2191 val = maxval;
2192 return snd_hda_codec_amp_update(codec, nid, ch, dir, idx,
2193 HDA_AMP_VOLMASK, val);
2197 * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume
2199 * The control element is supposed to have the private_value field
2200 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2202 int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
2203 struct snd_ctl_elem_value *ucontrol)
2205 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2206 hda_nid_t nid = get_amp_nid(kcontrol);
2207 int chs = get_amp_channels(kcontrol);
2208 int dir = get_amp_direction(kcontrol);
2209 int idx = get_amp_index(kcontrol);
2210 unsigned int ofs = get_amp_offset(kcontrol);
2211 long *valp = ucontrol->value.integer.value;
2213 if (chs & 1)
2214 *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs);
2215 if (chs & 2)
2216 *valp = read_amp_value(codec, nid, 1, dir, idx, ofs);
2217 return 0;
2219 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_get);
2222 * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume
2224 * The control element is supposed to have the private_value field
2225 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2227 int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
2228 struct snd_ctl_elem_value *ucontrol)
2230 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2231 hda_nid_t nid = get_amp_nid(kcontrol);
2232 int chs = get_amp_channels(kcontrol);
2233 int dir = get_amp_direction(kcontrol);
2234 int idx = get_amp_index(kcontrol);
2235 unsigned int ofs = get_amp_offset(kcontrol);
2236 long *valp = ucontrol->value.integer.value;
2237 int change = 0;
2239 snd_hda_power_up(codec);
2240 if (chs & 1) {
2241 change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp);
2242 valp++;
2244 if (chs & 2)
2245 change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp);
2246 snd_hda_power_down(codec);
2247 return change;
2249 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_put);
2252 * snd_hda_mixer_amp_volume_put - TLV callback for a standard AMP mixer volume
2254 * The control element is supposed to have the private_value field
2255 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2257 int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2258 unsigned int size, unsigned int __user *_tlv)
2260 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2261 hda_nid_t nid = get_amp_nid(kcontrol);
2262 int dir = get_amp_direction(kcontrol);
2263 unsigned int ofs = get_amp_offset(kcontrol);
2264 bool min_mute = get_amp_min_mute(kcontrol);
2265 u32 caps, val1, val2;
2267 if (size < 4 * sizeof(unsigned int))
2268 return -ENOMEM;
2269 caps = query_amp_caps(codec, nid, dir);
2270 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2271 val2 = (val2 + 1) * 25;
2272 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
2273 val1 += ofs;
2274 val1 = ((int)val1) * ((int)val2);
2275 if (min_mute || (caps & AC_AMPCAP_MIN_MUTE))
2276 val2 |= TLV_DB_SCALE_MUTE;
2277 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
2278 return -EFAULT;
2279 if (put_user(2 * sizeof(unsigned int), _tlv + 1))
2280 return -EFAULT;
2281 if (put_user(val1, _tlv + 2))
2282 return -EFAULT;
2283 if (put_user(val2, _tlv + 3))
2284 return -EFAULT;
2285 return 0;
2287 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_tlv);
2290 * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control
2291 * @codec: HD-audio codec
2292 * @nid: NID of a reference widget
2293 * @dir: #HDA_INPUT or #HDA_OUTPUT
2294 * @tlv: TLV data to be stored, at least 4 elements
2296 * Set (static) TLV data for a virtual master volume using the AMP caps
2297 * obtained from the reference NID.
2298 * The volume range is recalculated as if the max volume is 0dB.
2300 void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
2301 unsigned int *tlv)
2303 u32 caps;
2304 int nums, step;
2306 caps = query_amp_caps(codec, nid, dir);
2307 nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
2308 step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2309 step = (step + 1) * 25;
2310 tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
2311 tlv[1] = 2 * sizeof(unsigned int);
2312 tlv[2] = -nums * step;
2313 tlv[3] = step;
2315 EXPORT_SYMBOL_HDA(snd_hda_set_vmaster_tlv);
2317 /* find a mixer control element with the given name */
2318 static struct snd_kcontrol *
2319 find_mixer_ctl(struct hda_codec *codec, const char *name, int dev, int idx)
2321 struct snd_ctl_elem_id id;
2322 memset(&id, 0, sizeof(id));
2323 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
2324 id.device = dev;
2325 id.index = idx;
2326 if (snd_BUG_ON(strlen(name) >= sizeof(id.name)))
2327 return NULL;
2328 strcpy(id.name, name);
2329 return snd_ctl_find_id(codec->bus->card, &id);
2333 * snd_hda_find_mixer_ctl - Find a mixer control element with the given name
2334 * @codec: HD-audio codec
2335 * @name: ctl id name string
2337 * Get the control element with the given id string and IFACE_MIXER.
2339 struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
2340 const char *name)
2342 return find_mixer_ctl(codec, name, 0, 0);
2344 EXPORT_SYMBOL_HDA(snd_hda_find_mixer_ctl);
2346 static int find_empty_mixer_ctl_idx(struct hda_codec *codec, const char *name,
2347 int start_idx)
2349 int i, idx;
2350 /* 16 ctlrs should be large enough */
2351 for (i = 0, idx = start_idx; i < 16; i++, idx++) {
2352 if (!find_mixer_ctl(codec, name, 0, idx))
2353 return idx;
2355 return -EBUSY;
2359 * snd_hda_ctl_add - Add a control element and assign to the codec
2360 * @codec: HD-audio codec
2361 * @nid: corresponding NID (optional)
2362 * @kctl: the control element to assign
2364 * Add the given control element to an array inside the codec instance.
2365 * All control elements belonging to a codec are supposed to be added
2366 * by this function so that a proper clean-up works at the free or
2367 * reconfiguration time.
2369 * If non-zero @nid is passed, the NID is assigned to the control element.
2370 * The assignment is shown in the codec proc file.
2372 * snd_hda_ctl_add() checks the control subdev id field whether
2373 * #HDA_SUBDEV_NID_FLAG bit is set. If set (and @nid is zero), the lower
2374 * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit
2375 * specifies if kctl->private_value is a HDA amplifier value.
2377 int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid,
2378 struct snd_kcontrol *kctl)
2380 int err;
2381 unsigned short flags = 0;
2382 struct hda_nid_item *item;
2384 if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) {
2385 flags |= HDA_NID_ITEM_AMP;
2386 if (nid == 0)
2387 nid = get_amp_nid_(kctl->private_value);
2389 if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0)
2390 nid = kctl->id.subdevice & 0xffff;
2391 if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG))
2392 kctl->id.subdevice = 0;
2393 err = snd_ctl_add(codec->bus->card, kctl);
2394 if (err < 0)
2395 return err;
2396 item = snd_array_new(&codec->mixers);
2397 if (!item)
2398 return -ENOMEM;
2399 item->kctl = kctl;
2400 item->nid = nid;
2401 item->flags = flags;
2402 return 0;
2404 EXPORT_SYMBOL_HDA(snd_hda_ctl_add);
2407 * snd_hda_add_nid - Assign a NID to a control element
2408 * @codec: HD-audio codec
2409 * @nid: corresponding NID (optional)
2410 * @kctl: the control element to assign
2411 * @index: index to kctl
2413 * Add the given control element to an array inside the codec instance.
2414 * This function is used when #snd_hda_ctl_add cannot be used for 1:1
2415 * NID:KCTL mapping - for example "Capture Source" selector.
2417 int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl,
2418 unsigned int index, hda_nid_t nid)
2420 struct hda_nid_item *item;
2422 if (nid > 0) {
2423 item = snd_array_new(&codec->nids);
2424 if (!item)
2425 return -ENOMEM;
2426 item->kctl = kctl;
2427 item->index = index;
2428 item->nid = nid;
2429 return 0;
2431 printk(KERN_ERR "hda-codec: no NID for mapping control %s:%d:%d\n",
2432 kctl->id.name, kctl->id.index, index);
2433 return -EINVAL;
2435 EXPORT_SYMBOL_HDA(snd_hda_add_nid);
2438 * snd_hda_ctls_clear - Clear all controls assigned to the given codec
2439 * @codec: HD-audio codec
2441 void snd_hda_ctls_clear(struct hda_codec *codec)
2443 int i;
2444 struct hda_nid_item *items = codec->mixers.list;
2445 for (i = 0; i < codec->mixers.used; i++)
2446 snd_ctl_remove(codec->bus->card, items[i].kctl);
2447 snd_array_free(&codec->mixers);
2448 snd_array_free(&codec->nids);
2451 /* pseudo device locking
2452 * toggle card->shutdown to allow/disallow the device access (as a hack)
2454 int snd_hda_lock_devices(struct hda_bus *bus)
2456 struct snd_card *card = bus->card;
2457 struct hda_codec *codec;
2459 spin_lock(&card->files_lock);
2460 if (card->shutdown)
2461 goto err_unlock;
2462 card->shutdown = 1;
2463 if (!list_empty(&card->ctl_files))
2464 goto err_clear;
2466 list_for_each_entry(codec, &bus->codec_list, list) {
2467 int pcm;
2468 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
2469 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
2470 if (!cpcm->pcm)
2471 continue;
2472 if (cpcm->pcm->streams[0].substream_opened ||
2473 cpcm->pcm->streams[1].substream_opened)
2474 goto err_clear;
2477 spin_unlock(&card->files_lock);
2478 return 0;
2480 err_clear:
2481 card->shutdown = 0;
2482 err_unlock:
2483 spin_unlock(&card->files_lock);
2484 return -EINVAL;
2486 EXPORT_SYMBOL_HDA(snd_hda_lock_devices);
2488 void snd_hda_unlock_devices(struct hda_bus *bus)
2490 struct snd_card *card = bus->card;
2492 card = bus->card;
2493 spin_lock(&card->files_lock);
2494 card->shutdown = 0;
2495 spin_unlock(&card->files_lock);
2497 EXPORT_SYMBOL_HDA(snd_hda_unlock_devices);
2500 * snd_hda_codec_reset - Clear all objects assigned to the codec
2501 * @codec: HD-audio codec
2503 * This frees the all PCM and control elements assigned to the codec, and
2504 * clears the caches and restores the pin default configurations.
2506 * When a device is being used, it returns -EBSY. If successfully freed,
2507 * returns zero.
2509 int snd_hda_codec_reset(struct hda_codec *codec)
2511 struct hda_bus *bus = codec->bus;
2512 struct snd_card *card = bus->card;
2513 int i;
2515 if (snd_hda_lock_devices(bus) < 0)
2516 return -EBUSY;
2518 /* OK, let it free */
2519 cancel_delayed_work_sync(&codec->jackpoll_work);
2520 #ifdef CONFIG_PM
2521 cancel_delayed_work_sync(&codec->power_work);
2522 codec->power_on = 0;
2523 codec->power_transition = 0;
2524 codec->power_jiffies = jiffies;
2525 flush_workqueue(bus->workq);
2526 #endif
2527 snd_hda_ctls_clear(codec);
2528 /* release PCMs */
2529 for (i = 0; i < codec->num_pcms; i++) {
2530 if (codec->pcm_info[i].pcm) {
2531 snd_device_free(card, codec->pcm_info[i].pcm);
2532 clear_bit(codec->pcm_info[i].device,
2533 bus->pcm_dev_bits);
2536 if (codec->patch_ops.free)
2537 codec->patch_ops.free(codec);
2538 memset(&codec->patch_ops, 0, sizeof(codec->patch_ops));
2539 snd_hda_jack_tbl_clear(codec);
2540 codec->proc_widget_hook = NULL;
2541 codec->spec = NULL;
2542 free_hda_cache(&codec->amp_cache);
2543 free_hda_cache(&codec->cmd_cache);
2544 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
2545 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
2546 /* free only driver_pins so that init_pins + user_pins are restored */
2547 snd_array_free(&codec->driver_pins);
2548 snd_array_free(&codec->cvt_setups);
2549 snd_array_free(&codec->spdif_out);
2550 snd_array_free(&codec->verbs);
2551 codec->num_pcms = 0;
2552 codec->pcm_info = NULL;
2553 codec->preset = NULL;
2554 codec->slave_dig_outs = NULL;
2555 codec->spdif_status_reset = 0;
2556 module_put(codec->owner);
2557 codec->owner = NULL;
2559 /* allow device access again */
2560 snd_hda_unlock_devices(bus);
2561 return 0;
2564 typedef int (*map_slave_func_t)(void *, struct snd_kcontrol *);
2566 /* apply the function to all matching slave ctls in the mixer list */
2567 static int map_slaves(struct hda_codec *codec, const char * const *slaves,
2568 const char *suffix, map_slave_func_t func, void *data)
2570 struct hda_nid_item *items;
2571 const char * const *s;
2572 int i, err;
2574 items = codec->mixers.list;
2575 for (i = 0; i < codec->mixers.used; i++) {
2576 struct snd_kcontrol *sctl = items[i].kctl;
2577 if (!sctl || !sctl->id.name ||
2578 sctl->id.iface != SNDRV_CTL_ELEM_IFACE_MIXER)
2579 continue;
2580 for (s = slaves; *s; s++) {
2581 char tmpname[sizeof(sctl->id.name)];
2582 const char *name = *s;
2583 if (suffix) {
2584 snprintf(tmpname, sizeof(tmpname), "%s %s",
2585 name, suffix);
2586 name = tmpname;
2588 if (!strcmp(sctl->id.name, name)) {
2589 err = func(data, sctl);
2590 if (err)
2591 return err;
2592 break;
2596 return 0;
2599 static int check_slave_present(void *data, struct snd_kcontrol *sctl)
2601 return 1;
2604 /* guess the value corresponding to 0dB */
2605 static int get_kctl_0dB_offset(struct snd_kcontrol *kctl)
2607 int _tlv[4];
2608 const int *tlv = NULL;
2609 int val = -1;
2611 if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
2612 /* FIXME: set_fs() hack for obtaining user-space TLV data */
2613 mm_segment_t fs = get_fs();
2614 set_fs(get_ds());
2615 if (!kctl->tlv.c(kctl, 0, sizeof(_tlv), _tlv))
2616 tlv = _tlv;
2617 set_fs(fs);
2618 } else if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_READ)
2619 tlv = kctl->tlv.p;
2620 if (tlv && tlv[0] == SNDRV_CTL_TLVT_DB_SCALE)
2621 val = -tlv[2] / tlv[3];
2622 return val;
2625 /* call kctl->put with the given value(s) */
2626 static int put_kctl_with_value(struct snd_kcontrol *kctl, int val)
2628 struct snd_ctl_elem_value *ucontrol;
2629 ucontrol = kzalloc(sizeof(*ucontrol), GFP_KERNEL);
2630 if (!ucontrol)
2631 return -ENOMEM;
2632 ucontrol->value.integer.value[0] = val;
2633 ucontrol->value.integer.value[1] = val;
2634 kctl->put(kctl, ucontrol);
2635 kfree(ucontrol);
2636 return 0;
2639 /* initialize the slave volume with 0dB */
2640 static int init_slave_0dB(void *data, struct snd_kcontrol *slave)
2642 int offset = get_kctl_0dB_offset(slave);
2643 if (offset > 0)
2644 put_kctl_with_value(slave, offset);
2645 return 0;
2648 /* unmute the slave */
2649 static int init_slave_unmute(void *data, struct snd_kcontrol *slave)
2651 return put_kctl_with_value(slave, 1);
2655 * snd_hda_add_vmaster - create a virtual master control and add slaves
2656 * @codec: HD-audio codec
2657 * @name: vmaster control name
2658 * @tlv: TLV data (optional)
2659 * @slaves: slave control names (optional)
2660 * @suffix: suffix string to each slave name (optional)
2661 * @init_slave_vol: initialize slaves to unmute/0dB
2662 * @ctl_ret: store the vmaster kcontrol in return
2664 * Create a virtual master control with the given name. The TLV data
2665 * must be either NULL or a valid data.
2667 * @slaves is a NULL-terminated array of strings, each of which is a
2668 * slave control name. All controls with these names are assigned to
2669 * the new virtual master control.
2671 * This function returns zero if successful or a negative error code.
2673 int __snd_hda_add_vmaster(struct hda_codec *codec, char *name,
2674 unsigned int *tlv, const char * const *slaves,
2675 const char *suffix, bool init_slave_vol,
2676 struct snd_kcontrol **ctl_ret)
2678 struct snd_kcontrol *kctl;
2679 int err;
2681 if (ctl_ret)
2682 *ctl_ret = NULL;
2684 err = map_slaves(codec, slaves, suffix, check_slave_present, NULL);
2685 if (err != 1) {
2686 snd_printdd("No slave found for %s\n", name);
2687 return 0;
2689 kctl = snd_ctl_make_virtual_master(name, tlv);
2690 if (!kctl)
2691 return -ENOMEM;
2692 err = snd_hda_ctl_add(codec, 0, kctl);
2693 if (err < 0)
2694 return err;
2696 err = map_slaves(codec, slaves, suffix,
2697 (map_slave_func_t)snd_ctl_add_slave, kctl);
2698 if (err < 0)
2699 return err;
2701 /* init with master mute & zero volume */
2702 put_kctl_with_value(kctl, 0);
2703 if (init_slave_vol)
2704 map_slaves(codec, slaves, suffix,
2705 tlv ? init_slave_0dB : init_slave_unmute, kctl);
2707 if (ctl_ret)
2708 *ctl_ret = kctl;
2709 return 0;
2711 EXPORT_SYMBOL_HDA(__snd_hda_add_vmaster);
2714 * mute-LED control using vmaster
2716 static int vmaster_mute_mode_info(struct snd_kcontrol *kcontrol,
2717 struct snd_ctl_elem_info *uinfo)
2719 static const char * const texts[] = {
2720 "On", "Off", "Follow Master"
2722 unsigned int index;
2724 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2725 uinfo->count = 1;
2726 uinfo->value.enumerated.items = 3;
2727 index = uinfo->value.enumerated.item;
2728 if (index >= 3)
2729 index = 2;
2730 strcpy(uinfo->value.enumerated.name, texts[index]);
2731 return 0;
2734 static int vmaster_mute_mode_get(struct snd_kcontrol *kcontrol,
2735 struct snd_ctl_elem_value *ucontrol)
2737 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2738 ucontrol->value.enumerated.item[0] = hook->mute_mode;
2739 return 0;
2742 static int vmaster_mute_mode_put(struct snd_kcontrol *kcontrol,
2743 struct snd_ctl_elem_value *ucontrol)
2745 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2746 unsigned int old_mode = hook->mute_mode;
2748 hook->mute_mode = ucontrol->value.enumerated.item[0];
2749 if (hook->mute_mode > HDA_VMUTE_FOLLOW_MASTER)
2750 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
2751 if (old_mode == hook->mute_mode)
2752 return 0;
2753 snd_hda_sync_vmaster_hook(hook);
2754 return 1;
2757 static struct snd_kcontrol_new vmaster_mute_mode = {
2758 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2759 .name = "Mute-LED Mode",
2760 .info = vmaster_mute_mode_info,
2761 .get = vmaster_mute_mode_get,
2762 .put = vmaster_mute_mode_put,
2766 * Add a mute-LED hook with the given vmaster switch kctl
2767 * "Mute-LED Mode" control is automatically created and associated with
2768 * the given hook.
2770 int snd_hda_add_vmaster_hook(struct hda_codec *codec,
2771 struct hda_vmaster_mute_hook *hook,
2772 bool expose_enum_ctl)
2774 struct snd_kcontrol *kctl;
2776 if (!hook->hook || !hook->sw_kctl)
2777 return 0;
2778 snd_ctl_add_vmaster_hook(hook->sw_kctl, hook->hook, codec);
2779 hook->codec = codec;
2780 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
2781 if (!expose_enum_ctl)
2782 return 0;
2783 kctl = snd_ctl_new1(&vmaster_mute_mode, hook);
2784 if (!kctl)
2785 return -ENOMEM;
2786 return snd_hda_ctl_add(codec, 0, kctl);
2788 EXPORT_SYMBOL_HDA(snd_hda_add_vmaster_hook);
2791 * Call the hook with the current value for synchronization
2792 * Should be called in init callback
2794 void snd_hda_sync_vmaster_hook(struct hda_vmaster_mute_hook *hook)
2796 if (!hook->hook || !hook->codec)
2797 return;
2798 /* don't call vmaster hook in the destructor since it might have
2799 * been already destroyed
2801 if (hook->codec->bus->shutdown)
2802 return;
2803 switch (hook->mute_mode) {
2804 case HDA_VMUTE_FOLLOW_MASTER:
2805 snd_ctl_sync_vmaster_hook(hook->sw_kctl);
2806 break;
2807 default:
2808 hook->hook(hook->codec, hook->mute_mode);
2809 break;
2812 EXPORT_SYMBOL_HDA(snd_hda_sync_vmaster_hook);
2816 * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch
2818 * The control element is supposed to have the private_value field
2819 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2821 int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
2822 struct snd_ctl_elem_info *uinfo)
2824 int chs = get_amp_channels(kcontrol);
2826 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2827 uinfo->count = chs == 3 ? 2 : 1;
2828 uinfo->value.integer.min = 0;
2829 uinfo->value.integer.max = 1;
2830 return 0;
2832 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_info);
2835 * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch
2837 * The control element is supposed to have the private_value field
2838 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2840 int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
2841 struct snd_ctl_elem_value *ucontrol)
2843 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2844 hda_nid_t nid = get_amp_nid(kcontrol);
2845 int chs = get_amp_channels(kcontrol);
2846 int dir = get_amp_direction(kcontrol);
2847 int idx = get_amp_index(kcontrol);
2848 long *valp = ucontrol->value.integer.value;
2850 if (chs & 1)
2851 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
2852 HDA_AMP_MUTE) ? 0 : 1;
2853 if (chs & 2)
2854 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
2855 HDA_AMP_MUTE) ? 0 : 1;
2856 return 0;
2858 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_get);
2861 * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch
2863 * The control element is supposed to have the private_value field
2864 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2866 int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
2867 struct snd_ctl_elem_value *ucontrol)
2869 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2870 hda_nid_t nid = get_amp_nid(kcontrol);
2871 int chs = get_amp_channels(kcontrol);
2872 int dir = get_amp_direction(kcontrol);
2873 int idx = get_amp_index(kcontrol);
2874 long *valp = ucontrol->value.integer.value;
2875 int change = 0;
2877 snd_hda_power_up(codec);
2878 if (chs & 1) {
2879 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
2880 HDA_AMP_MUTE,
2881 *valp ? 0 : HDA_AMP_MUTE);
2882 valp++;
2884 if (chs & 2)
2885 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
2886 HDA_AMP_MUTE,
2887 *valp ? 0 : HDA_AMP_MUTE);
2888 hda_call_check_power_status(codec, nid);
2889 snd_hda_power_down(codec);
2890 return change;
2892 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put);
2895 * bound volume controls
2897 * bind multiple volumes (# indices, from 0)
2900 #define AMP_VAL_IDX_SHIFT 19
2901 #define AMP_VAL_IDX_MASK (0x0f<<19)
2904 * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control
2906 * The control element is supposed to have the private_value field
2907 * set up via HDA_BIND_MUTE*() macros.
2909 int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
2910 struct snd_ctl_elem_value *ucontrol)
2912 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2913 unsigned long pval;
2914 int err;
2916 mutex_lock(&codec->control_mutex);
2917 pval = kcontrol->private_value;
2918 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
2919 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
2920 kcontrol->private_value = pval;
2921 mutex_unlock(&codec->control_mutex);
2922 return err;
2924 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_get);
2927 * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control
2929 * The control element is supposed to have the private_value field
2930 * set up via HDA_BIND_MUTE*() macros.
2932 int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
2933 struct snd_ctl_elem_value *ucontrol)
2935 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2936 unsigned long pval;
2937 int i, indices, err = 0, change = 0;
2939 mutex_lock(&codec->control_mutex);
2940 pval = kcontrol->private_value;
2941 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
2942 for (i = 0; i < indices; i++) {
2943 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
2944 (i << AMP_VAL_IDX_SHIFT);
2945 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2946 if (err < 0)
2947 break;
2948 change |= err;
2950 kcontrol->private_value = pval;
2951 mutex_unlock(&codec->control_mutex);
2952 return err < 0 ? err : change;
2954 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_put);
2957 * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control
2959 * The control element is supposed to have the private_value field
2960 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2962 int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
2963 struct snd_ctl_elem_info *uinfo)
2965 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2966 struct hda_bind_ctls *c;
2967 int err;
2969 mutex_lock(&codec->control_mutex);
2970 c = (struct hda_bind_ctls *)kcontrol->private_value;
2971 kcontrol->private_value = *c->values;
2972 err = c->ops->info(kcontrol, uinfo);
2973 kcontrol->private_value = (long)c;
2974 mutex_unlock(&codec->control_mutex);
2975 return err;
2977 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_info);
2980 * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control
2982 * The control element is supposed to have the private_value field
2983 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2985 int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
2986 struct snd_ctl_elem_value *ucontrol)
2988 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2989 struct hda_bind_ctls *c;
2990 int err;
2992 mutex_lock(&codec->control_mutex);
2993 c = (struct hda_bind_ctls *)kcontrol->private_value;
2994 kcontrol->private_value = *c->values;
2995 err = c->ops->get(kcontrol, ucontrol);
2996 kcontrol->private_value = (long)c;
2997 mutex_unlock(&codec->control_mutex);
2998 return err;
3000 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_get);
3003 * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control
3005 * The control element is supposed to have the private_value field
3006 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
3008 int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
3009 struct snd_ctl_elem_value *ucontrol)
3011 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3012 struct hda_bind_ctls *c;
3013 unsigned long *vals;
3014 int err = 0, change = 0;
3016 mutex_lock(&codec->control_mutex);
3017 c = (struct hda_bind_ctls *)kcontrol->private_value;
3018 for (vals = c->values; *vals; vals++) {
3019 kcontrol->private_value = *vals;
3020 err = c->ops->put(kcontrol, ucontrol);
3021 if (err < 0)
3022 break;
3023 change |= err;
3025 kcontrol->private_value = (long)c;
3026 mutex_unlock(&codec->control_mutex);
3027 return err < 0 ? err : change;
3029 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_put);
3032 * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control
3034 * The control element is supposed to have the private_value field
3035 * set up via HDA_BIND_VOL() macro.
3037 int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
3038 unsigned int size, unsigned int __user *tlv)
3040 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3041 struct hda_bind_ctls *c;
3042 int err;
3044 mutex_lock(&codec->control_mutex);
3045 c = (struct hda_bind_ctls *)kcontrol->private_value;
3046 kcontrol->private_value = *c->values;
3047 err = c->ops->tlv(kcontrol, op_flag, size, tlv);
3048 kcontrol->private_value = (long)c;
3049 mutex_unlock(&codec->control_mutex);
3050 return err;
3052 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_tlv);
3054 struct hda_ctl_ops snd_hda_bind_vol = {
3055 .info = snd_hda_mixer_amp_volume_info,
3056 .get = snd_hda_mixer_amp_volume_get,
3057 .put = snd_hda_mixer_amp_volume_put,
3058 .tlv = snd_hda_mixer_amp_tlv
3060 EXPORT_SYMBOL_HDA(snd_hda_bind_vol);
3062 struct hda_ctl_ops snd_hda_bind_sw = {
3063 .info = snd_hda_mixer_amp_switch_info,
3064 .get = snd_hda_mixer_amp_switch_get,
3065 .put = snd_hda_mixer_amp_switch_put,
3066 .tlv = snd_hda_mixer_amp_tlv
3068 EXPORT_SYMBOL_HDA(snd_hda_bind_sw);
3071 * SPDIF out controls
3074 static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
3075 struct snd_ctl_elem_info *uinfo)
3077 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
3078 uinfo->count = 1;
3079 return 0;
3082 static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
3083 struct snd_ctl_elem_value *ucontrol)
3085 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
3086 IEC958_AES0_NONAUDIO |
3087 IEC958_AES0_CON_EMPHASIS_5015 |
3088 IEC958_AES0_CON_NOT_COPYRIGHT;
3089 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
3090 IEC958_AES1_CON_ORIGINAL;
3091 return 0;
3094 static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
3095 struct snd_ctl_elem_value *ucontrol)
3097 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
3098 IEC958_AES0_NONAUDIO |
3099 IEC958_AES0_PRO_EMPHASIS_5015;
3100 return 0;
3103 static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
3104 struct snd_ctl_elem_value *ucontrol)
3106 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3107 int idx = kcontrol->private_value;
3108 struct hda_spdif_out *spdif;
3110 mutex_lock(&codec->spdif_mutex);
3111 spdif = snd_array_elem(&codec->spdif_out, idx);
3112 ucontrol->value.iec958.status[0] = spdif->status & 0xff;
3113 ucontrol->value.iec958.status[1] = (spdif->status >> 8) & 0xff;
3114 ucontrol->value.iec958.status[2] = (spdif->status >> 16) & 0xff;
3115 ucontrol->value.iec958.status[3] = (spdif->status >> 24) & 0xff;
3116 mutex_unlock(&codec->spdif_mutex);
3118 return 0;
3121 /* convert from SPDIF status bits to HDA SPDIF bits
3122 * bit 0 (DigEn) is always set zero (to be filled later)
3124 static unsigned short convert_from_spdif_status(unsigned int sbits)
3126 unsigned short val = 0;
3128 if (sbits & IEC958_AES0_PROFESSIONAL)
3129 val |= AC_DIG1_PROFESSIONAL;
3130 if (sbits & IEC958_AES0_NONAUDIO)
3131 val |= AC_DIG1_NONAUDIO;
3132 if (sbits & IEC958_AES0_PROFESSIONAL) {
3133 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
3134 IEC958_AES0_PRO_EMPHASIS_5015)
3135 val |= AC_DIG1_EMPHASIS;
3136 } else {
3137 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
3138 IEC958_AES0_CON_EMPHASIS_5015)
3139 val |= AC_DIG1_EMPHASIS;
3140 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
3141 val |= AC_DIG1_COPYRIGHT;
3142 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
3143 val |= AC_DIG1_LEVEL;
3144 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
3146 return val;
3149 /* convert to SPDIF status bits from HDA SPDIF bits
3151 static unsigned int convert_to_spdif_status(unsigned short val)
3153 unsigned int sbits = 0;
3155 if (val & AC_DIG1_NONAUDIO)
3156 sbits |= IEC958_AES0_NONAUDIO;
3157 if (val & AC_DIG1_PROFESSIONAL)
3158 sbits |= IEC958_AES0_PROFESSIONAL;
3159 if (sbits & IEC958_AES0_PROFESSIONAL) {
3160 if (val & AC_DIG1_EMPHASIS)
3161 sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
3162 } else {
3163 if (val & AC_DIG1_EMPHASIS)
3164 sbits |= IEC958_AES0_CON_EMPHASIS_5015;
3165 if (!(val & AC_DIG1_COPYRIGHT))
3166 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
3167 if (val & AC_DIG1_LEVEL)
3168 sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
3169 sbits |= val & (0x7f << 8);
3171 return sbits;
3174 /* set digital convert verbs both for the given NID and its slaves */
3175 static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
3176 int verb, int val)
3178 const hda_nid_t *d;
3180 snd_hda_codec_write_cache(codec, nid, 0, verb, val);
3181 d = codec->slave_dig_outs;
3182 if (!d)
3183 return;
3184 for (; *d; d++)
3185 snd_hda_codec_write_cache(codec, *d, 0, verb, val);
3188 static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
3189 int dig1, int dig2)
3191 if (dig1 != -1)
3192 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1);
3193 if (dig2 != -1)
3194 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2);
3197 static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
3198 struct snd_ctl_elem_value *ucontrol)
3200 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3201 int idx = kcontrol->private_value;
3202 struct hda_spdif_out *spdif;
3203 hda_nid_t nid;
3204 unsigned short val;
3205 int change;
3207 mutex_lock(&codec->spdif_mutex);
3208 spdif = snd_array_elem(&codec->spdif_out, idx);
3209 nid = spdif->nid;
3210 spdif->status = ucontrol->value.iec958.status[0] |
3211 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
3212 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
3213 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
3214 val = convert_from_spdif_status(spdif->status);
3215 val |= spdif->ctls & 1;
3216 change = spdif->ctls != val;
3217 spdif->ctls = val;
3218 if (change && nid != (u16)-1)
3219 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
3220 mutex_unlock(&codec->spdif_mutex);
3221 return change;
3224 #define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
3226 static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
3227 struct snd_ctl_elem_value *ucontrol)
3229 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3230 int idx = kcontrol->private_value;
3231 struct hda_spdif_out *spdif;
3233 mutex_lock(&codec->spdif_mutex);
3234 spdif = snd_array_elem(&codec->spdif_out, idx);
3235 ucontrol->value.integer.value[0] = spdif->ctls & AC_DIG1_ENABLE;
3236 mutex_unlock(&codec->spdif_mutex);
3237 return 0;
3240 static inline void set_spdif_ctls(struct hda_codec *codec, hda_nid_t nid,
3241 int dig1, int dig2)
3243 set_dig_out_convert(codec, nid, dig1, dig2);
3244 /* unmute amp switch (if any) */
3245 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
3246 (dig1 & AC_DIG1_ENABLE))
3247 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
3248 HDA_AMP_MUTE, 0);
3251 static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
3252 struct snd_ctl_elem_value *ucontrol)
3254 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3255 int idx = kcontrol->private_value;
3256 struct hda_spdif_out *spdif;
3257 hda_nid_t nid;
3258 unsigned short val;
3259 int change;
3261 mutex_lock(&codec->spdif_mutex);
3262 spdif = snd_array_elem(&codec->spdif_out, idx);
3263 nid = spdif->nid;
3264 val = spdif->ctls & ~AC_DIG1_ENABLE;
3265 if (ucontrol->value.integer.value[0])
3266 val |= AC_DIG1_ENABLE;
3267 change = spdif->ctls != val;
3268 spdif->ctls = val;
3269 if (change && nid != (u16)-1)
3270 set_spdif_ctls(codec, nid, val & 0xff, -1);
3271 mutex_unlock(&codec->spdif_mutex);
3272 return change;
3275 static struct snd_kcontrol_new dig_mixes[] = {
3277 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3278 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3279 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
3280 .info = snd_hda_spdif_mask_info,
3281 .get = snd_hda_spdif_cmask_get,
3284 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3285 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3286 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK),
3287 .info = snd_hda_spdif_mask_info,
3288 .get = snd_hda_spdif_pmask_get,
3291 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3292 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
3293 .info = snd_hda_spdif_mask_info,
3294 .get = snd_hda_spdif_default_get,
3295 .put = snd_hda_spdif_default_put,
3298 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3299 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
3300 .info = snd_hda_spdif_out_switch_info,
3301 .get = snd_hda_spdif_out_switch_get,
3302 .put = snd_hda_spdif_out_switch_put,
3304 { } /* end */
3308 * snd_hda_create_dig_out_ctls - create Output SPDIF-related controls
3309 * @codec: the HDA codec
3310 * @associated_nid: NID that new ctls associated with
3311 * @cvt_nid: converter NID
3312 * @type: HDA_PCM_TYPE_*
3313 * Creates controls related with the digital output.
3314 * Called from each patch supporting the digital out.
3316 * Returns 0 if successful, or a negative error code.
3318 int snd_hda_create_dig_out_ctls(struct hda_codec *codec,
3319 hda_nid_t associated_nid,
3320 hda_nid_t cvt_nid,
3321 int type)
3323 int err;
3324 struct snd_kcontrol *kctl;
3325 struct snd_kcontrol_new *dig_mix;
3326 int idx = 0;
3327 const int spdif_index = 16;
3328 struct hda_spdif_out *spdif;
3329 struct hda_bus *bus = codec->bus;
3331 if (bus->primary_dig_out_type == HDA_PCM_TYPE_HDMI &&
3332 type == HDA_PCM_TYPE_SPDIF) {
3333 idx = spdif_index;
3334 } else if (bus->primary_dig_out_type == HDA_PCM_TYPE_SPDIF &&
3335 type == HDA_PCM_TYPE_HDMI) {
3336 /* suppose a single SPDIF device */
3337 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
3338 kctl = find_mixer_ctl(codec, dig_mix->name, 0, 0);
3339 if (!kctl)
3340 break;
3341 kctl->id.index = spdif_index;
3343 bus->primary_dig_out_type = HDA_PCM_TYPE_HDMI;
3345 if (!bus->primary_dig_out_type)
3346 bus->primary_dig_out_type = type;
3348 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Playback Switch", idx);
3349 if (idx < 0) {
3350 printk(KERN_ERR "hda_codec: too many IEC958 outputs\n");
3351 return -EBUSY;
3353 spdif = snd_array_new(&codec->spdif_out);
3354 if (!spdif)
3355 return -ENOMEM;
3356 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
3357 kctl = snd_ctl_new1(dig_mix, codec);
3358 if (!kctl)
3359 return -ENOMEM;
3360 kctl->id.index = idx;
3361 kctl->private_value = codec->spdif_out.used - 1;
3362 err = snd_hda_ctl_add(codec, associated_nid, kctl);
3363 if (err < 0)
3364 return err;
3366 spdif->nid = cvt_nid;
3367 spdif->ctls = snd_hda_codec_read(codec, cvt_nid, 0,
3368 AC_VERB_GET_DIGI_CONVERT_1, 0);
3369 spdif->status = convert_to_spdif_status(spdif->ctls);
3370 return 0;
3372 EXPORT_SYMBOL_HDA(snd_hda_create_dig_out_ctls);
3374 /* get the hda_spdif_out entry from the given NID
3375 * call within spdif_mutex lock
3377 struct hda_spdif_out *snd_hda_spdif_out_of_nid(struct hda_codec *codec,
3378 hda_nid_t nid)
3380 int i;
3381 for (i = 0; i < codec->spdif_out.used; i++) {
3382 struct hda_spdif_out *spdif =
3383 snd_array_elem(&codec->spdif_out, i);
3384 if (spdif->nid == nid)
3385 return spdif;
3387 return NULL;
3389 EXPORT_SYMBOL_HDA(snd_hda_spdif_out_of_nid);
3391 void snd_hda_spdif_ctls_unassign(struct hda_codec *codec, int idx)
3393 struct hda_spdif_out *spdif;
3395 mutex_lock(&codec->spdif_mutex);
3396 spdif = snd_array_elem(&codec->spdif_out, idx);
3397 spdif->nid = (u16)-1;
3398 mutex_unlock(&codec->spdif_mutex);
3400 EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_unassign);
3402 void snd_hda_spdif_ctls_assign(struct hda_codec *codec, int idx, hda_nid_t nid)
3404 struct hda_spdif_out *spdif;
3405 unsigned short val;
3407 mutex_lock(&codec->spdif_mutex);
3408 spdif = snd_array_elem(&codec->spdif_out, idx);
3409 if (spdif->nid != nid) {
3410 spdif->nid = nid;
3411 val = spdif->ctls;
3412 set_spdif_ctls(codec, nid, val & 0xff, (val >> 8) & 0xff);
3414 mutex_unlock(&codec->spdif_mutex);
3416 EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_assign);
3419 * SPDIF sharing with analog output
3421 static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
3422 struct snd_ctl_elem_value *ucontrol)
3424 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3425 ucontrol->value.integer.value[0] = mout->share_spdif;
3426 return 0;
3429 static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
3430 struct snd_ctl_elem_value *ucontrol)
3432 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3433 mout->share_spdif = !!ucontrol->value.integer.value[0];
3434 return 0;
3437 static struct snd_kcontrol_new spdif_share_sw = {
3438 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3439 .name = "IEC958 Default PCM Playback Switch",
3440 .info = snd_ctl_boolean_mono_info,
3441 .get = spdif_share_sw_get,
3442 .put = spdif_share_sw_put,
3446 * snd_hda_create_spdif_share_sw - create Default PCM switch
3447 * @codec: the HDA codec
3448 * @mout: multi-out instance
3450 int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
3451 struct hda_multi_out *mout)
3453 struct snd_kcontrol *kctl;
3455 if (!mout->dig_out_nid)
3456 return 0;
3458 kctl = snd_ctl_new1(&spdif_share_sw, mout);
3459 if (!kctl)
3460 return -ENOMEM;
3461 /* ATTENTION: here mout is passed as private_data, instead of codec */
3462 return snd_hda_ctl_add(codec, mout->dig_out_nid, kctl);
3464 EXPORT_SYMBOL_HDA(snd_hda_create_spdif_share_sw);
3467 * SPDIF input
3470 #define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
3472 static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
3473 struct snd_ctl_elem_value *ucontrol)
3475 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3477 ucontrol->value.integer.value[0] = codec->spdif_in_enable;
3478 return 0;
3481 static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
3482 struct snd_ctl_elem_value *ucontrol)
3484 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3485 hda_nid_t nid = kcontrol->private_value;
3486 unsigned int val = !!ucontrol->value.integer.value[0];
3487 int change;
3489 mutex_lock(&codec->spdif_mutex);
3490 change = codec->spdif_in_enable != val;
3491 if (change) {
3492 codec->spdif_in_enable = val;
3493 snd_hda_codec_write_cache(codec, nid, 0,
3494 AC_VERB_SET_DIGI_CONVERT_1, val);
3496 mutex_unlock(&codec->spdif_mutex);
3497 return change;
3500 static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
3501 struct snd_ctl_elem_value *ucontrol)
3503 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3504 hda_nid_t nid = kcontrol->private_value;
3505 unsigned short val;
3506 unsigned int sbits;
3508 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0);
3509 sbits = convert_to_spdif_status(val);
3510 ucontrol->value.iec958.status[0] = sbits;
3511 ucontrol->value.iec958.status[1] = sbits >> 8;
3512 ucontrol->value.iec958.status[2] = sbits >> 16;
3513 ucontrol->value.iec958.status[3] = sbits >> 24;
3514 return 0;
3517 static struct snd_kcontrol_new dig_in_ctls[] = {
3519 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3520 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH),
3521 .info = snd_hda_spdif_in_switch_info,
3522 .get = snd_hda_spdif_in_switch_get,
3523 .put = snd_hda_spdif_in_switch_put,
3526 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3527 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3528 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
3529 .info = snd_hda_spdif_mask_info,
3530 .get = snd_hda_spdif_in_status_get,
3532 { } /* end */
3536 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
3537 * @codec: the HDA codec
3538 * @nid: audio in widget NID
3540 * Creates controls related with the SPDIF input.
3541 * Called from each patch supporting the SPDIF in.
3543 * Returns 0 if successful, or a negative error code.
3545 int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
3547 int err;
3548 struct snd_kcontrol *kctl;
3549 struct snd_kcontrol_new *dig_mix;
3550 int idx;
3552 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Capture Switch", 0);
3553 if (idx < 0) {
3554 printk(KERN_ERR "hda_codec: too many IEC958 inputs\n");
3555 return -EBUSY;
3557 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
3558 kctl = snd_ctl_new1(dig_mix, codec);
3559 if (!kctl)
3560 return -ENOMEM;
3561 kctl->private_value = nid;
3562 err = snd_hda_ctl_add(codec, nid, kctl);
3563 if (err < 0)
3564 return err;
3566 codec->spdif_in_enable =
3567 snd_hda_codec_read(codec, nid, 0,
3568 AC_VERB_GET_DIGI_CONVERT_1, 0) &
3569 AC_DIG1_ENABLE;
3570 return 0;
3572 EXPORT_SYMBOL_HDA(snd_hda_create_spdif_in_ctls);
3575 * command cache
3578 /* build a 31bit cache key with the widget id and the command parameter */
3579 #define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
3580 #define get_cmd_cache_nid(key) ((key) & 0xff)
3581 #define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
3584 * snd_hda_codec_write_cache - send a single command with caching
3585 * @codec: the HDA codec
3586 * @nid: NID to send the command
3587 * @flags: optional bit flags
3588 * @verb: the verb to send
3589 * @parm: the parameter for the verb
3591 * Send a single command without waiting for response.
3593 * Returns 0 if successful, or a negative error code.
3595 int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
3596 int flags, unsigned int verb, unsigned int parm)
3598 int err;
3599 struct hda_cache_head *c;
3600 u32 key;
3601 unsigned int cache_only;
3603 cache_only = codec->cached_write;
3604 if (!cache_only) {
3605 err = snd_hda_codec_write(codec, nid, flags, verb, parm);
3606 if (err < 0)
3607 return err;
3610 /* parm may contain the verb stuff for get/set amp */
3611 verb = verb | (parm >> 8);
3612 parm &= 0xff;
3613 key = build_cmd_cache_key(nid, verb);
3614 mutex_lock(&codec->bus->cmd_mutex);
3615 c = get_alloc_hash(&codec->cmd_cache, key);
3616 if (c) {
3617 c->val = parm;
3618 c->dirty = cache_only;
3620 mutex_unlock(&codec->bus->cmd_mutex);
3621 return 0;
3623 EXPORT_SYMBOL_HDA(snd_hda_codec_write_cache);
3626 * snd_hda_codec_update_cache - check cache and write the cmd only when needed
3627 * @codec: the HDA codec
3628 * @nid: NID to send the command
3629 * @flags: optional bit flags
3630 * @verb: the verb to send
3631 * @parm: the parameter for the verb
3633 * This function works like snd_hda_codec_write_cache(), but it doesn't send
3634 * command if the parameter is already identical with the cached value.
3635 * If not, it sends the command and refreshes the cache.
3637 * Returns 0 if successful, or a negative error code.
3639 int snd_hda_codec_update_cache(struct hda_codec *codec, hda_nid_t nid,
3640 int flags, unsigned int verb, unsigned int parm)
3642 struct hda_cache_head *c;
3643 u32 key;
3645 /* parm may contain the verb stuff for get/set amp */
3646 verb = verb | (parm >> 8);
3647 parm &= 0xff;
3648 key = build_cmd_cache_key(nid, verb);
3649 mutex_lock(&codec->bus->cmd_mutex);
3650 c = get_hash(&codec->cmd_cache, key);
3651 if (c && c->val == parm) {
3652 mutex_unlock(&codec->bus->cmd_mutex);
3653 return 0;
3655 mutex_unlock(&codec->bus->cmd_mutex);
3656 return snd_hda_codec_write_cache(codec, nid, flags, verb, parm);
3658 EXPORT_SYMBOL_HDA(snd_hda_codec_update_cache);
3661 * snd_hda_codec_resume_cache - Resume the all commands from the cache
3662 * @codec: HD-audio codec
3664 * Execute all verbs recorded in the command caches to resume.
3666 void snd_hda_codec_resume_cache(struct hda_codec *codec)
3668 int i;
3670 mutex_lock(&codec->hash_mutex);
3671 codec->cached_write = 0;
3672 for (i = 0; i < codec->cmd_cache.buf.used; i++) {
3673 struct hda_cache_head *buffer;
3674 u32 key;
3676 buffer = snd_array_elem(&codec->cmd_cache.buf, i);
3677 key = buffer->key;
3678 if (!key)
3679 continue;
3680 if (!buffer->dirty)
3681 continue;
3682 buffer->dirty = 0;
3683 mutex_unlock(&codec->hash_mutex);
3684 snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
3685 get_cmd_cache_cmd(key), buffer->val);
3686 mutex_lock(&codec->hash_mutex);
3688 mutex_unlock(&codec->hash_mutex);
3690 EXPORT_SYMBOL_HDA(snd_hda_codec_resume_cache);
3693 * snd_hda_sequence_write_cache - sequence writes with caching
3694 * @codec: the HDA codec
3695 * @seq: VERB array to send
3697 * Send the commands sequentially from the given array.
3698 * Thte commands are recorded on cache for power-save and resume.
3699 * The array must be terminated with NID=0.
3701 void snd_hda_sequence_write_cache(struct hda_codec *codec,
3702 const struct hda_verb *seq)
3704 for (; seq->nid; seq++)
3705 snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
3706 seq->param);
3708 EXPORT_SYMBOL_HDA(snd_hda_sequence_write_cache);
3711 * snd_hda_codec_flush_cache - Execute all pending (cached) amps / verbs
3712 * @codec: HD-audio codec
3714 void snd_hda_codec_flush_cache(struct hda_codec *codec)
3716 snd_hda_codec_resume_amp(codec);
3717 snd_hda_codec_resume_cache(codec);
3719 EXPORT_SYMBOL_HDA(snd_hda_codec_flush_cache);
3721 void snd_hda_codec_set_power_to_all(struct hda_codec *codec, hda_nid_t fg,
3722 unsigned int power_state)
3724 hda_nid_t nid = codec->start_nid;
3725 int i;
3727 for (i = 0; i < codec->num_nodes; i++, nid++) {
3728 unsigned int wcaps = get_wcaps(codec, nid);
3729 unsigned int state = power_state;
3730 if (!(wcaps & AC_WCAP_POWER))
3731 continue;
3732 if (codec->power_filter) {
3733 state = codec->power_filter(codec, nid, power_state);
3734 if (state != power_state && power_state == AC_PWRST_D3)
3735 continue;
3737 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE,
3738 state);
3741 EXPORT_SYMBOL_HDA(snd_hda_codec_set_power_to_all);
3744 * supported power states check
3746 static bool snd_hda_codec_get_supported_ps(struct hda_codec *codec, hda_nid_t fg,
3747 unsigned int power_state)
3749 int sup = snd_hda_param_read(codec, fg, AC_PAR_POWER_STATE);
3751 if (sup == -1)
3752 return false;
3753 if (sup & power_state)
3754 return true;
3755 else
3756 return false;
3760 * wait until the state is reached, returns the current state
3762 static unsigned int hda_sync_power_state(struct hda_codec *codec,
3763 hda_nid_t fg,
3764 unsigned int power_state)
3766 unsigned long end_time = jiffies + msecs_to_jiffies(500);
3767 unsigned int state, actual_state;
3769 for (;;) {
3770 state = snd_hda_codec_read(codec, fg, 0,
3771 AC_VERB_GET_POWER_STATE, 0);
3772 if (state & AC_PWRST_ERROR)
3773 break;
3774 actual_state = (state >> 4) & 0x0f;
3775 if (actual_state == power_state)
3776 break;
3777 if (time_after_eq(jiffies, end_time))
3778 break;
3779 /* wait until the codec reachs to the target state */
3780 msleep(1);
3782 return state;
3785 /* don't power down the widget if it controls eapd and EAPD_BTLENABLE is set */
3786 unsigned int snd_hda_codec_eapd_power_filter(struct hda_codec *codec,
3787 hda_nid_t nid,
3788 unsigned int power_state)
3790 if (power_state == AC_PWRST_D3 &&
3791 get_wcaps_type(get_wcaps(codec, nid)) == AC_WID_PIN &&
3792 (snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD)) {
3793 int eapd = snd_hda_codec_read(codec, nid, 0,
3794 AC_VERB_GET_EAPD_BTLENABLE, 0);
3795 if (eapd & 0x02)
3796 return AC_PWRST_D0;
3798 return power_state;
3800 EXPORT_SYMBOL_HDA(snd_hda_codec_eapd_power_filter);
3803 * set power state of the codec, and return the power state
3805 static unsigned int hda_set_power_state(struct hda_codec *codec,
3806 unsigned int power_state)
3808 hda_nid_t fg = codec->afg ? codec->afg : codec->mfg;
3809 int count;
3810 unsigned int state;
3811 int flags = 0;
3813 /* this delay seems necessary to avoid click noise at power-down */
3814 if (power_state == AC_PWRST_D3) {
3815 /* transition time less than 10ms for power down */
3816 msleep(codec->epss ? 10 : 100);
3817 flags = HDA_RW_NO_RESPONSE_FALLBACK;
3820 /* repeat power states setting at most 10 times*/
3821 for (count = 0; count < 10; count++) {
3822 if (codec->patch_ops.set_power_state)
3823 codec->patch_ops.set_power_state(codec, fg,
3824 power_state);
3825 else {
3826 snd_hda_codec_read(codec, fg, flags,
3827 AC_VERB_SET_POWER_STATE,
3828 power_state);
3829 snd_hda_codec_set_power_to_all(codec, fg, power_state);
3831 state = hda_sync_power_state(codec, fg, power_state);
3832 if (!(state & AC_PWRST_ERROR))
3833 break;
3836 return state;
3839 /* sync power states of all widgets;
3840 * this is called at the end of codec parsing
3842 static void sync_power_up_states(struct hda_codec *codec)
3844 hda_nid_t nid = codec->start_nid;
3845 int i;
3847 /* don't care if no filter is used */
3848 if (!codec->power_filter)
3849 return;
3851 for (i = 0; i < codec->num_nodes; i++, nid++) {
3852 unsigned int wcaps = get_wcaps(codec, nid);
3853 unsigned int target;
3854 if (!(wcaps & AC_WCAP_POWER))
3855 continue;
3856 target = codec->power_filter(codec, nid, AC_PWRST_D0);
3857 if (target == AC_PWRST_D0)
3858 continue;
3859 if (!snd_hda_check_power_state(codec, nid, target))
3860 snd_hda_codec_write(codec, nid, 0,
3861 AC_VERB_SET_POWER_STATE, target);
3865 #ifdef CONFIG_SND_HDA_HWDEP
3866 /* execute additional init verbs */
3867 static void hda_exec_init_verbs(struct hda_codec *codec)
3869 if (codec->init_verbs.list)
3870 snd_hda_sequence_write(codec, codec->init_verbs.list);
3872 #else
3873 static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
3874 #endif
3876 #ifdef CONFIG_PM
3878 * call suspend and power-down; used both from PM and power-save
3879 * this function returns the power state in the end
3881 static unsigned int hda_call_codec_suspend(struct hda_codec *codec, bool in_wq)
3883 unsigned int state;
3885 codec->in_pm = 1;
3887 if (codec->patch_ops.suspend)
3888 codec->patch_ops.suspend(codec);
3889 hda_cleanup_all_streams(codec);
3890 state = hda_set_power_state(codec, AC_PWRST_D3);
3891 /* Cancel delayed work if we aren't currently running from it. */
3892 if (!in_wq)
3893 cancel_delayed_work_sync(&codec->power_work);
3894 spin_lock(&codec->power_lock);
3895 snd_hda_update_power_acct(codec);
3896 trace_hda_power_down(codec);
3897 codec->power_on = 0;
3898 codec->power_transition = 0;
3899 codec->power_jiffies = jiffies;
3900 spin_unlock(&codec->power_lock);
3901 codec->in_pm = 0;
3902 return state;
3905 /* mark all entries of cmd and amp caches dirty */
3906 static void hda_mark_cmd_cache_dirty(struct hda_codec *codec)
3908 int i;
3909 for (i = 0; i < codec->cmd_cache.buf.used; i++) {
3910 struct hda_cache_head *cmd;
3911 cmd = snd_array_elem(&codec->cmd_cache.buf, i);
3912 cmd->dirty = 1;
3914 for (i = 0; i < codec->amp_cache.buf.used; i++) {
3915 struct hda_amp_info *amp;
3916 amp = snd_array_elem(&codec->amp_cache.buf, i);
3917 amp->head.dirty = 1;
3922 * kick up codec; used both from PM and power-save
3924 static void hda_call_codec_resume(struct hda_codec *codec)
3926 codec->in_pm = 1;
3928 hda_mark_cmd_cache_dirty(codec);
3930 /* set as if powered on for avoiding re-entering the resume
3931 * in the resume / power-save sequence
3933 hda_keep_power_on(codec);
3934 hda_set_power_state(codec, AC_PWRST_D0);
3935 restore_shutup_pins(codec);
3936 hda_exec_init_verbs(codec);
3937 snd_hda_jack_set_dirty_all(codec);
3938 if (codec->patch_ops.resume)
3939 codec->patch_ops.resume(codec);
3940 else {
3941 if (codec->patch_ops.init)
3942 codec->patch_ops.init(codec);
3943 snd_hda_codec_resume_amp(codec);
3944 snd_hda_codec_resume_cache(codec);
3947 if (codec->jackpoll_interval)
3948 hda_jackpoll_work(&codec->jackpoll_work.work);
3949 else
3950 snd_hda_jack_report_sync(codec);
3952 codec->in_pm = 0;
3953 snd_hda_power_down(codec); /* flag down before returning */
3955 #endif /* CONFIG_PM */
3959 * snd_hda_build_controls - build mixer controls
3960 * @bus: the BUS
3962 * Creates mixer controls for each codec included in the bus.
3964 * Returns 0 if successful, otherwise a negative error code.
3966 int snd_hda_build_controls(struct hda_bus *bus)
3968 struct hda_codec *codec;
3970 list_for_each_entry(codec, &bus->codec_list, list) {
3971 int err = snd_hda_codec_build_controls(codec);
3972 if (err < 0) {
3973 printk(KERN_ERR "hda_codec: cannot build controls "
3974 "for #%d (error %d)\n", codec->addr, err);
3975 err = snd_hda_codec_reset(codec);
3976 if (err < 0) {
3977 printk(KERN_ERR
3978 "hda_codec: cannot revert codec\n");
3979 return err;
3983 return 0;
3985 EXPORT_SYMBOL_HDA(snd_hda_build_controls);
3988 * add standard channel maps if not specified
3990 static int add_std_chmaps(struct hda_codec *codec)
3992 int i, str, err;
3994 for (i = 0; i < codec->num_pcms; i++) {
3995 for (str = 0; str < 2; str++) {
3996 struct snd_pcm *pcm = codec->pcm_info[i].pcm;
3997 struct hda_pcm_stream *hinfo =
3998 &codec->pcm_info[i].stream[str];
3999 struct snd_pcm_chmap *chmap;
4000 const struct snd_pcm_chmap_elem *elem;
4002 if (codec->pcm_info[i].own_chmap)
4003 continue;
4004 if (!pcm || !hinfo->substreams)
4005 continue;
4006 elem = hinfo->chmap ? hinfo->chmap : snd_pcm_std_chmaps;
4007 err = snd_pcm_add_chmap_ctls(pcm, str, elem,
4008 hinfo->channels_max,
4009 0, &chmap);
4010 if (err < 0)
4011 return err;
4012 chmap->channel_mask = SND_PCM_CHMAP_MASK_2468;
4015 return 0;
4018 /* default channel maps for 2.1 speakers;
4019 * since HD-audio supports only stereo, odd number channels are omitted
4021 const struct snd_pcm_chmap_elem snd_pcm_2_1_chmaps[] = {
4022 { .channels = 2,
4023 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } },
4024 { .channels = 4,
4025 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
4026 SNDRV_CHMAP_LFE, SNDRV_CHMAP_LFE } },
4029 EXPORT_SYMBOL_GPL(snd_pcm_2_1_chmaps);
4031 int snd_hda_codec_build_controls(struct hda_codec *codec)
4033 int err = 0;
4034 hda_exec_init_verbs(codec);
4035 /* continue to initialize... */
4036 if (codec->patch_ops.init)
4037 err = codec->patch_ops.init(codec);
4038 if (!err && codec->patch_ops.build_controls)
4039 err = codec->patch_ops.build_controls(codec);
4040 if (err < 0)
4041 return err;
4043 /* we create chmaps here instead of build_pcms */
4044 err = add_std_chmaps(codec);
4045 if (err < 0)
4046 return err;
4048 if (codec->jackpoll_interval)
4049 hda_jackpoll_work(&codec->jackpoll_work.work);
4050 else
4051 snd_hda_jack_report_sync(codec); /* call at the last init point */
4052 sync_power_up_states(codec);
4053 return 0;
4057 * stream formats
4059 struct hda_rate_tbl {
4060 unsigned int hz;
4061 unsigned int alsa_bits;
4062 unsigned int hda_fmt;
4065 /* rate = base * mult / div */
4066 #define HDA_RATE(base, mult, div) \
4067 (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \
4068 (((div) - 1) << AC_FMT_DIV_SHIFT))
4070 static struct hda_rate_tbl rate_bits[] = {
4071 /* rate in Hz, ALSA rate bitmask, HDA format value */
4073 /* autodetected value used in snd_hda_query_supported_pcm */
4074 { 8000, SNDRV_PCM_RATE_8000, HDA_RATE(48, 1, 6) },
4075 { 11025, SNDRV_PCM_RATE_11025, HDA_RATE(44, 1, 4) },
4076 { 16000, SNDRV_PCM_RATE_16000, HDA_RATE(48, 1, 3) },
4077 { 22050, SNDRV_PCM_RATE_22050, HDA_RATE(44, 1, 2) },
4078 { 32000, SNDRV_PCM_RATE_32000, HDA_RATE(48, 2, 3) },
4079 { 44100, SNDRV_PCM_RATE_44100, HDA_RATE(44, 1, 1) },
4080 { 48000, SNDRV_PCM_RATE_48000, HDA_RATE(48, 1, 1) },
4081 { 88200, SNDRV_PCM_RATE_88200, HDA_RATE(44, 2, 1) },
4082 { 96000, SNDRV_PCM_RATE_96000, HDA_RATE(48, 2, 1) },
4083 { 176400, SNDRV_PCM_RATE_176400, HDA_RATE(44, 4, 1) },
4084 { 192000, SNDRV_PCM_RATE_192000, HDA_RATE(48, 4, 1) },
4085 #define AC_PAR_PCM_RATE_BITS 11
4086 /* up to bits 10, 384kHZ isn't supported properly */
4088 /* not autodetected value */
4089 { 9600, SNDRV_PCM_RATE_KNOT, HDA_RATE(48, 1, 5) },
4091 { 0 } /* terminator */
4095 * snd_hda_calc_stream_format - calculate format bitset
4096 * @rate: the sample rate
4097 * @channels: the number of channels
4098 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
4099 * @maxbps: the max. bps
4101 * Calculate the format bitset from the given rate, channels and th PCM format.
4103 * Return zero if invalid.
4105 unsigned int snd_hda_calc_stream_format(unsigned int rate,
4106 unsigned int channels,
4107 unsigned int format,
4108 unsigned int maxbps,
4109 unsigned short spdif_ctls)
4111 int i;
4112 unsigned int val = 0;
4114 for (i = 0; rate_bits[i].hz; i++)
4115 if (rate_bits[i].hz == rate) {
4116 val = rate_bits[i].hda_fmt;
4117 break;
4119 if (!rate_bits[i].hz) {
4120 snd_printdd("invalid rate %d\n", rate);
4121 return 0;
4124 if (channels == 0 || channels > 8) {
4125 snd_printdd("invalid channels %d\n", channels);
4126 return 0;
4128 val |= channels - 1;
4130 switch (snd_pcm_format_width(format)) {
4131 case 8:
4132 val |= AC_FMT_BITS_8;
4133 break;
4134 case 16:
4135 val |= AC_FMT_BITS_16;
4136 break;
4137 case 20:
4138 case 24:
4139 case 32:
4140 if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE)
4141 val |= AC_FMT_BITS_32;
4142 else if (maxbps >= 24)
4143 val |= AC_FMT_BITS_24;
4144 else
4145 val |= AC_FMT_BITS_20;
4146 break;
4147 default:
4148 snd_printdd("invalid format width %d\n",
4149 snd_pcm_format_width(format));
4150 return 0;
4153 if (spdif_ctls & AC_DIG1_NONAUDIO)
4154 val |= AC_FMT_TYPE_NON_PCM;
4156 return val;
4158 EXPORT_SYMBOL_HDA(snd_hda_calc_stream_format);
4160 static unsigned int get_pcm_param(struct hda_codec *codec, hda_nid_t nid,
4161 int dir)
4163 unsigned int val = 0;
4164 if (nid != codec->afg &&
4165 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD))
4166 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
4167 if (!val || val == -1)
4168 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
4169 if (!val || val == -1)
4170 return 0;
4171 return val;
4174 static unsigned int query_pcm_param(struct hda_codec *codec, hda_nid_t nid)
4176 return query_caps_hash(codec, nid, 0, HDA_HASH_PARPCM_KEY(nid),
4177 get_pcm_param);
4180 static unsigned int get_stream_param(struct hda_codec *codec, hda_nid_t nid,
4181 int dir)
4183 unsigned int streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
4184 if (!streams || streams == -1)
4185 streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
4186 if (!streams || streams == -1)
4187 return 0;
4188 return streams;
4191 static unsigned int query_stream_param(struct hda_codec *codec, hda_nid_t nid)
4193 return query_caps_hash(codec, nid, 0, HDA_HASH_PARSTR_KEY(nid),
4194 get_stream_param);
4198 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
4199 * @codec: the HDA codec
4200 * @nid: NID to query
4201 * @ratesp: the pointer to store the detected rate bitflags
4202 * @formatsp: the pointer to store the detected formats
4203 * @bpsp: the pointer to store the detected format widths
4205 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
4206 * or @bsps argument is ignored.
4208 * Returns 0 if successful, otherwise a negative error code.
4210 int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
4211 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
4213 unsigned int i, val, wcaps;
4215 wcaps = get_wcaps(codec, nid);
4216 val = query_pcm_param(codec, nid);
4218 if (ratesp) {
4219 u32 rates = 0;
4220 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
4221 if (val & (1 << i))
4222 rates |= rate_bits[i].alsa_bits;
4224 if (rates == 0) {
4225 snd_printk(KERN_ERR "hda_codec: rates == 0 "
4226 "(nid=0x%x, val=0x%x, ovrd=%i)\n",
4227 nid, val,
4228 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0);
4229 return -EIO;
4231 *ratesp = rates;
4234 if (formatsp || bpsp) {
4235 u64 formats = 0;
4236 unsigned int streams, bps;
4238 streams = query_stream_param(codec, nid);
4239 if (!streams)
4240 return -EIO;
4242 bps = 0;
4243 if (streams & AC_SUPFMT_PCM) {
4244 if (val & AC_SUPPCM_BITS_8) {
4245 formats |= SNDRV_PCM_FMTBIT_U8;
4246 bps = 8;
4248 if (val & AC_SUPPCM_BITS_16) {
4249 formats |= SNDRV_PCM_FMTBIT_S16_LE;
4250 bps = 16;
4252 if (wcaps & AC_WCAP_DIGITAL) {
4253 if (val & AC_SUPPCM_BITS_32)
4254 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
4255 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
4256 formats |= SNDRV_PCM_FMTBIT_S32_LE;
4257 if (val & AC_SUPPCM_BITS_24)
4258 bps = 24;
4259 else if (val & AC_SUPPCM_BITS_20)
4260 bps = 20;
4261 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
4262 AC_SUPPCM_BITS_32)) {
4263 formats |= SNDRV_PCM_FMTBIT_S32_LE;
4264 if (val & AC_SUPPCM_BITS_32)
4265 bps = 32;
4266 else if (val & AC_SUPPCM_BITS_24)
4267 bps = 24;
4268 else if (val & AC_SUPPCM_BITS_20)
4269 bps = 20;
4272 #if 0 /* FIXME: CS4206 doesn't work, which is the only codec supporting float */
4273 if (streams & AC_SUPFMT_FLOAT32) {
4274 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
4275 if (!bps)
4276 bps = 32;
4278 #endif
4279 if (streams == AC_SUPFMT_AC3) {
4280 /* should be exclusive */
4281 /* temporary hack: we have still no proper support
4282 * for the direct AC3 stream...
4284 formats |= SNDRV_PCM_FMTBIT_U8;
4285 bps = 8;
4287 if (formats == 0) {
4288 snd_printk(KERN_ERR "hda_codec: formats == 0 "
4289 "(nid=0x%x, val=0x%x, ovrd=%i, "
4290 "streams=0x%x)\n",
4291 nid, val,
4292 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0,
4293 streams);
4294 return -EIO;
4296 if (formatsp)
4297 *formatsp = formats;
4298 if (bpsp)
4299 *bpsp = bps;
4302 return 0;
4304 EXPORT_SYMBOL_HDA(snd_hda_query_supported_pcm);
4307 * snd_hda_is_supported_format - Check the validity of the format
4308 * @codec: HD-audio codec
4309 * @nid: NID to check
4310 * @format: the HD-audio format value to check
4312 * Check whether the given node supports the format value.
4314 * Returns 1 if supported, 0 if not.
4316 int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
4317 unsigned int format)
4319 int i;
4320 unsigned int val = 0, rate, stream;
4322 val = query_pcm_param(codec, nid);
4323 if (!val)
4324 return 0;
4326 rate = format & 0xff00;
4327 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
4328 if (rate_bits[i].hda_fmt == rate) {
4329 if (val & (1 << i))
4330 break;
4331 return 0;
4333 if (i >= AC_PAR_PCM_RATE_BITS)
4334 return 0;
4336 stream = query_stream_param(codec, nid);
4337 if (!stream)
4338 return 0;
4340 if (stream & AC_SUPFMT_PCM) {
4341 switch (format & 0xf0) {
4342 case 0x00:
4343 if (!(val & AC_SUPPCM_BITS_8))
4344 return 0;
4345 break;
4346 case 0x10:
4347 if (!(val & AC_SUPPCM_BITS_16))
4348 return 0;
4349 break;
4350 case 0x20:
4351 if (!(val & AC_SUPPCM_BITS_20))
4352 return 0;
4353 break;
4354 case 0x30:
4355 if (!(val & AC_SUPPCM_BITS_24))
4356 return 0;
4357 break;
4358 case 0x40:
4359 if (!(val & AC_SUPPCM_BITS_32))
4360 return 0;
4361 break;
4362 default:
4363 return 0;
4365 } else {
4366 /* FIXME: check for float32 and AC3? */
4369 return 1;
4371 EXPORT_SYMBOL_HDA(snd_hda_is_supported_format);
4374 * PCM stuff
4376 static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
4377 struct hda_codec *codec,
4378 struct snd_pcm_substream *substream)
4380 return 0;
4383 static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
4384 struct hda_codec *codec,
4385 unsigned int stream_tag,
4386 unsigned int format,
4387 struct snd_pcm_substream *substream)
4389 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4390 return 0;
4393 static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
4394 struct hda_codec *codec,
4395 struct snd_pcm_substream *substream)
4397 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
4398 return 0;
4401 static int set_pcm_default_values(struct hda_codec *codec,
4402 struct hda_pcm_stream *info)
4404 int err;
4406 /* query support PCM information from the given NID */
4407 if (info->nid && (!info->rates || !info->formats)) {
4408 err = snd_hda_query_supported_pcm(codec, info->nid,
4409 info->rates ? NULL : &info->rates,
4410 info->formats ? NULL : &info->formats,
4411 info->maxbps ? NULL : &info->maxbps);
4412 if (err < 0)
4413 return err;
4415 if (info->ops.open == NULL)
4416 info->ops.open = hda_pcm_default_open_close;
4417 if (info->ops.close == NULL)
4418 info->ops.close = hda_pcm_default_open_close;
4419 if (info->ops.prepare == NULL) {
4420 if (snd_BUG_ON(!info->nid))
4421 return -EINVAL;
4422 info->ops.prepare = hda_pcm_default_prepare;
4424 if (info->ops.cleanup == NULL) {
4425 if (snd_BUG_ON(!info->nid))
4426 return -EINVAL;
4427 info->ops.cleanup = hda_pcm_default_cleanup;
4429 return 0;
4433 * codec prepare/cleanup entries
4435 int snd_hda_codec_prepare(struct hda_codec *codec,
4436 struct hda_pcm_stream *hinfo,
4437 unsigned int stream,
4438 unsigned int format,
4439 struct snd_pcm_substream *substream)
4441 int ret;
4442 mutex_lock(&codec->bus->prepare_mutex);
4443 ret = hinfo->ops.prepare(hinfo, codec, stream, format, substream);
4444 if (ret >= 0)
4445 purify_inactive_streams(codec);
4446 mutex_unlock(&codec->bus->prepare_mutex);
4447 return ret;
4449 EXPORT_SYMBOL_HDA(snd_hda_codec_prepare);
4451 void snd_hda_codec_cleanup(struct hda_codec *codec,
4452 struct hda_pcm_stream *hinfo,
4453 struct snd_pcm_substream *substream)
4455 mutex_lock(&codec->bus->prepare_mutex);
4456 hinfo->ops.cleanup(hinfo, codec, substream);
4457 mutex_unlock(&codec->bus->prepare_mutex);
4459 EXPORT_SYMBOL_HDA(snd_hda_codec_cleanup);
4461 /* global */
4462 const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = {
4463 "Audio", "SPDIF", "HDMI", "Modem"
4467 * get the empty PCM device number to assign
4469 static int get_empty_pcm_device(struct hda_bus *bus, unsigned int type)
4471 /* audio device indices; not linear to keep compatibility */
4472 /* assigned to static slots up to dev#10; if more needed, assign
4473 * the later slot dynamically (when CONFIG_SND_DYNAMIC_MINORS=y)
4475 static int audio_idx[HDA_PCM_NTYPES][5] = {
4476 [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 },
4477 [HDA_PCM_TYPE_SPDIF] = { 1, -1 },
4478 [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 },
4479 [HDA_PCM_TYPE_MODEM] = { 6, -1 },
4481 int i;
4483 if (type >= HDA_PCM_NTYPES) {
4484 snd_printk(KERN_WARNING "Invalid PCM type %d\n", type);
4485 return -EINVAL;
4488 for (i = 0; audio_idx[type][i] >= 0; i++) {
4489 #ifndef CONFIG_SND_DYNAMIC_MINORS
4490 if (audio_idx[type][i] >= 8)
4491 break;
4492 #endif
4493 if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits))
4494 return audio_idx[type][i];
4497 #ifdef CONFIG_SND_DYNAMIC_MINORS
4498 /* non-fixed slots starting from 10 */
4499 for (i = 10; i < 32; i++) {
4500 if (!test_and_set_bit(i, bus->pcm_dev_bits))
4501 return i;
4503 #endif
4505 snd_printk(KERN_WARNING "Too many %s devices\n",
4506 snd_hda_pcm_type_name[type]);
4507 #ifndef CONFIG_SND_DYNAMIC_MINORS
4508 snd_printk(KERN_WARNING "Consider building the kernel with CONFIG_SND_DYNAMIC_MINORS=y\n");
4509 #endif
4510 return -EAGAIN;
4514 * attach a new PCM stream
4516 static int snd_hda_attach_pcm(struct hda_codec *codec, struct hda_pcm *pcm)
4518 struct hda_bus *bus = codec->bus;
4519 struct hda_pcm_stream *info;
4520 int stream, err;
4522 if (snd_BUG_ON(!pcm->name))
4523 return -EINVAL;
4524 for (stream = 0; stream < 2; stream++) {
4525 info = &pcm->stream[stream];
4526 if (info->substreams) {
4527 err = set_pcm_default_values(codec, info);
4528 if (err < 0)
4529 return err;
4532 return bus->ops.attach_pcm(bus, codec, pcm);
4535 /* assign all PCMs of the given codec */
4536 int snd_hda_codec_build_pcms(struct hda_codec *codec)
4538 unsigned int pcm;
4539 int err;
4541 if (!codec->num_pcms) {
4542 if (!codec->patch_ops.build_pcms)
4543 return 0;
4544 err = codec->patch_ops.build_pcms(codec);
4545 if (err < 0) {
4546 printk(KERN_ERR "hda_codec: cannot build PCMs"
4547 "for #%d (error %d)\n", codec->addr, err);
4548 err = snd_hda_codec_reset(codec);
4549 if (err < 0) {
4550 printk(KERN_ERR
4551 "hda_codec: cannot revert codec\n");
4552 return err;
4556 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
4557 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
4558 int dev;
4560 if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
4561 continue; /* no substreams assigned */
4563 if (!cpcm->pcm) {
4564 dev = get_empty_pcm_device(codec->bus, cpcm->pcm_type);
4565 if (dev < 0)
4566 continue; /* no fatal error */
4567 cpcm->device = dev;
4568 err = snd_hda_attach_pcm(codec, cpcm);
4569 if (err < 0) {
4570 printk(KERN_ERR "hda_codec: cannot attach "
4571 "PCM stream %d for codec #%d\n",
4572 dev, codec->addr);
4573 continue; /* no fatal error */
4577 return 0;
4581 * snd_hda_build_pcms - build PCM information
4582 * @bus: the BUS
4584 * Create PCM information for each codec included in the bus.
4586 * The build_pcms codec patch is requested to set up codec->num_pcms and
4587 * codec->pcm_info properly. The array is referred by the top-level driver
4588 * to create its PCM instances.
4589 * The allocated codec->pcm_info should be released in codec->patch_ops.free
4590 * callback.
4592 * At least, substreams, channels_min and channels_max must be filled for
4593 * each stream. substreams = 0 indicates that the stream doesn't exist.
4594 * When rates and/or formats are zero, the supported values are queried
4595 * from the given nid. The nid is used also by the default ops.prepare
4596 * and ops.cleanup callbacks.
4598 * The driver needs to call ops.open in its open callback. Similarly,
4599 * ops.close is supposed to be called in the close callback.
4600 * ops.prepare should be called in the prepare or hw_params callback
4601 * with the proper parameters for set up.
4602 * ops.cleanup should be called in hw_free for clean up of streams.
4604 * This function returns 0 if successful, or a negative error code.
4606 int snd_hda_build_pcms(struct hda_bus *bus)
4608 struct hda_codec *codec;
4610 list_for_each_entry(codec, &bus->codec_list, list) {
4611 int err = snd_hda_codec_build_pcms(codec);
4612 if (err < 0)
4613 return err;
4615 return 0;
4617 EXPORT_SYMBOL_HDA(snd_hda_build_pcms);
4620 * snd_hda_check_board_config - compare the current codec with the config table
4621 * @codec: the HDA codec
4622 * @num_configs: number of config enums
4623 * @models: array of model name strings
4624 * @tbl: configuration table, terminated by null entries
4626 * Compares the modelname or PCI subsystem id of the current codec with the
4627 * given configuration table. If a matching entry is found, returns its
4628 * config value (supposed to be 0 or positive).
4630 * If no entries are matching, the function returns a negative value.
4632 int snd_hda_check_board_config(struct hda_codec *codec,
4633 int num_configs, const char * const *models,
4634 const struct snd_pci_quirk *tbl)
4636 if (codec->modelname && models) {
4637 int i;
4638 for (i = 0; i < num_configs; i++) {
4639 if (models[i] &&
4640 !strcmp(codec->modelname, models[i])) {
4641 snd_printd(KERN_INFO "hda_codec: model '%s' is "
4642 "selected\n", models[i]);
4643 return i;
4648 if (!codec->bus->pci || !tbl)
4649 return -1;
4651 tbl = snd_pci_quirk_lookup(codec->bus->pci, tbl);
4652 if (!tbl)
4653 return -1;
4654 if (tbl->value >= 0 && tbl->value < num_configs) {
4655 #ifdef CONFIG_SND_DEBUG_VERBOSE
4656 char tmp[10];
4657 const char *model = NULL;
4658 if (models)
4659 model = models[tbl->value];
4660 if (!model) {
4661 sprintf(tmp, "#%d", tbl->value);
4662 model = tmp;
4664 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
4665 "for config %x:%x (%s)\n",
4666 model, tbl->subvendor, tbl->subdevice,
4667 (tbl->name ? tbl->name : "Unknown device"));
4668 #endif
4669 return tbl->value;
4671 return -1;
4673 EXPORT_SYMBOL_HDA(snd_hda_check_board_config);
4676 * snd_hda_check_board_codec_sid_config - compare the current codec
4677 subsystem ID with the
4678 config table
4680 This is important for Gateway notebooks with SB450 HDA Audio
4681 where the vendor ID of the PCI device is:
4682 ATI Technologies Inc SB450 HDA Audio [1002:437b]
4683 and the vendor/subvendor are found only at the codec.
4685 * @codec: the HDA codec
4686 * @num_configs: number of config enums
4687 * @models: array of model name strings
4688 * @tbl: configuration table, terminated by null entries
4690 * Compares the modelname or PCI subsystem id of the current codec with the
4691 * given configuration table. If a matching entry is found, returns its
4692 * config value (supposed to be 0 or positive).
4694 * If no entries are matching, the function returns a negative value.
4696 int snd_hda_check_board_codec_sid_config(struct hda_codec *codec,
4697 int num_configs, const char * const *models,
4698 const struct snd_pci_quirk *tbl)
4700 const struct snd_pci_quirk *q;
4702 /* Search for codec ID */
4703 for (q = tbl; q->subvendor; q++) {
4704 unsigned int mask = 0xffff0000 | q->subdevice_mask;
4705 unsigned int id = (q->subdevice | (q->subvendor << 16)) & mask;
4706 if ((codec->subsystem_id & mask) == id)
4707 break;
4710 if (!q->subvendor)
4711 return -1;
4713 tbl = q;
4715 if (tbl->value >= 0 && tbl->value < num_configs) {
4716 #ifdef CONFIG_SND_DEBUG_VERBOSE
4717 char tmp[10];
4718 const char *model = NULL;
4719 if (models)
4720 model = models[tbl->value];
4721 if (!model) {
4722 sprintf(tmp, "#%d", tbl->value);
4723 model = tmp;
4725 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
4726 "for config %x:%x (%s)\n",
4727 model, tbl->subvendor, tbl->subdevice,
4728 (tbl->name ? tbl->name : "Unknown device"));
4729 #endif
4730 return tbl->value;
4732 return -1;
4734 EXPORT_SYMBOL_HDA(snd_hda_check_board_codec_sid_config);
4737 * snd_hda_add_new_ctls - create controls from the array
4738 * @codec: the HDA codec
4739 * @knew: the array of struct snd_kcontrol_new
4741 * This helper function creates and add new controls in the given array.
4742 * The array must be terminated with an empty entry as terminator.
4744 * Returns 0 if successful, or a negative error code.
4746 int snd_hda_add_new_ctls(struct hda_codec *codec,
4747 const struct snd_kcontrol_new *knew)
4749 int err;
4751 for (; knew->name; knew++) {
4752 struct snd_kcontrol *kctl;
4753 int addr = 0, idx = 0;
4754 if (knew->iface == -1) /* skip this codec private value */
4755 continue;
4756 for (;;) {
4757 kctl = snd_ctl_new1(knew, codec);
4758 if (!kctl)
4759 return -ENOMEM;
4760 if (addr > 0)
4761 kctl->id.device = addr;
4762 if (idx > 0)
4763 kctl->id.index = idx;
4764 err = snd_hda_ctl_add(codec, 0, kctl);
4765 if (!err)
4766 break;
4767 /* try first with another device index corresponding to
4768 * the codec addr; if it still fails (or it's the
4769 * primary codec), then try another control index
4771 if (!addr && codec->addr)
4772 addr = codec->addr;
4773 else if (!idx && !knew->index) {
4774 idx = find_empty_mixer_ctl_idx(codec,
4775 knew->name, 0);
4776 if (idx <= 0)
4777 return err;
4778 } else
4779 return err;
4782 return 0;
4784 EXPORT_SYMBOL_HDA(snd_hda_add_new_ctls);
4786 #ifdef CONFIG_PM
4787 static void hda_power_work(struct work_struct *work)
4789 struct hda_codec *codec =
4790 container_of(work, struct hda_codec, power_work.work);
4791 struct hda_bus *bus = codec->bus;
4792 unsigned int state;
4794 spin_lock(&codec->power_lock);
4795 if (codec->power_transition > 0) { /* during power-up sequence? */
4796 spin_unlock(&codec->power_lock);
4797 return;
4799 if (!codec->power_on || codec->power_count) {
4800 codec->power_transition = 0;
4801 spin_unlock(&codec->power_lock);
4802 return;
4804 spin_unlock(&codec->power_lock);
4806 state = hda_call_codec_suspend(codec, true);
4807 codec->pm_down_notified = 0;
4808 if (!bus->power_keep_link_on && (state & AC_PWRST_CLK_STOP_OK)) {
4809 codec->pm_down_notified = 1;
4810 hda_call_pm_notify(bus, false);
4814 static void hda_keep_power_on(struct hda_codec *codec)
4816 spin_lock(&codec->power_lock);
4817 codec->power_count++;
4818 codec->power_on = 1;
4819 codec->power_jiffies = jiffies;
4820 spin_unlock(&codec->power_lock);
4823 /* update the power on/off account with the current jiffies */
4824 void snd_hda_update_power_acct(struct hda_codec *codec)
4826 unsigned long delta = jiffies - codec->power_jiffies;
4827 if (codec->power_on)
4828 codec->power_on_acct += delta;
4829 else
4830 codec->power_off_acct += delta;
4831 codec->power_jiffies += delta;
4834 /* Transition to powered up, if wait_power_down then wait for a pending
4835 * transition to D3 to complete. A pending D3 transition is indicated
4836 * with power_transition == -1. */
4837 /* call this with codec->power_lock held! */
4838 static void __snd_hda_power_up(struct hda_codec *codec, bool wait_power_down)
4840 struct hda_bus *bus = codec->bus;
4842 /* Return if power_on or transitioning to power_on, unless currently
4843 * powering down. */
4844 if ((codec->power_on || codec->power_transition > 0) &&
4845 !(wait_power_down && codec->power_transition < 0))
4846 return;
4847 spin_unlock(&codec->power_lock);
4849 cancel_delayed_work_sync(&codec->power_work);
4851 spin_lock(&codec->power_lock);
4852 /* If the power down delayed work was cancelled above before starting,
4853 * then there is no need to go through power up here.
4855 if (codec->power_on) {
4856 if (codec->power_transition < 0)
4857 codec->power_transition = 0;
4858 return;
4861 trace_hda_power_up(codec);
4862 snd_hda_update_power_acct(codec);
4863 codec->power_on = 1;
4864 codec->power_jiffies = jiffies;
4865 codec->power_transition = 1; /* avoid reentrance */
4866 spin_unlock(&codec->power_lock);
4868 if (codec->pm_down_notified) {
4869 codec->pm_down_notified = 0;
4870 hda_call_pm_notify(bus, true);
4873 hda_call_codec_resume(codec);
4875 spin_lock(&codec->power_lock);
4876 codec->power_transition = 0;
4879 #define power_save(codec) \
4880 ((codec)->bus->power_save ? *(codec)->bus->power_save : 0)
4882 /* Transition to powered down */
4883 static void __snd_hda_power_down(struct hda_codec *codec)
4885 if (!codec->power_on || codec->power_count || codec->power_transition)
4886 return;
4888 if (power_save(codec)) {
4889 codec->power_transition = -1; /* avoid reentrance */
4890 queue_delayed_work(codec->bus->workq, &codec->power_work,
4891 msecs_to_jiffies(power_save(codec) * 1000));
4896 * snd_hda_power_save - Power-up/down/sync the codec
4897 * @codec: HD-audio codec
4898 * @delta: the counter delta to change
4900 * Change the power-up counter via @delta, and power up or down the hardware
4901 * appropriately. For the power-down, queue to the delayed action.
4902 * Passing zero to @delta means to synchronize the power state.
4904 void snd_hda_power_save(struct hda_codec *codec, int delta, bool d3wait)
4906 spin_lock(&codec->power_lock);
4907 codec->power_count += delta;
4908 trace_hda_power_count(codec);
4909 if (delta > 0)
4910 __snd_hda_power_up(codec, d3wait);
4911 else
4912 __snd_hda_power_down(codec);
4913 spin_unlock(&codec->power_lock);
4915 EXPORT_SYMBOL_HDA(snd_hda_power_save);
4918 * snd_hda_check_amp_list_power - Check the amp list and update the power
4919 * @codec: HD-audio codec
4920 * @check: the object containing an AMP list and the status
4921 * @nid: NID to check / update
4923 * Check whether the given NID is in the amp list. If it's in the list,
4924 * check the current AMP status, and update the the power-status according
4925 * to the mute status.
4927 * This function is supposed to be set or called from the check_power_status
4928 * patch ops.
4930 int snd_hda_check_amp_list_power(struct hda_codec *codec,
4931 struct hda_loopback_check *check,
4932 hda_nid_t nid)
4934 const struct hda_amp_list *p;
4935 int ch, v;
4937 if (!check->amplist)
4938 return 0;
4939 for (p = check->amplist; p->nid; p++) {
4940 if (p->nid == nid)
4941 break;
4943 if (!p->nid)
4944 return 0; /* nothing changed */
4946 for (p = check->amplist; p->nid; p++) {
4947 for (ch = 0; ch < 2; ch++) {
4948 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
4949 p->idx);
4950 if (!(v & HDA_AMP_MUTE) && v > 0) {
4951 if (!check->power_on) {
4952 check->power_on = 1;
4953 snd_hda_power_up(codec);
4955 return 1;
4959 if (check->power_on) {
4960 check->power_on = 0;
4961 snd_hda_power_down(codec);
4963 return 0;
4965 EXPORT_SYMBOL_HDA(snd_hda_check_amp_list_power);
4966 #endif
4969 * Channel mode helper
4973 * snd_hda_ch_mode_info - Info callback helper for the channel mode enum
4975 int snd_hda_ch_mode_info(struct hda_codec *codec,
4976 struct snd_ctl_elem_info *uinfo,
4977 const struct hda_channel_mode *chmode,
4978 int num_chmodes)
4980 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4981 uinfo->count = 1;
4982 uinfo->value.enumerated.items = num_chmodes;
4983 if (uinfo->value.enumerated.item >= num_chmodes)
4984 uinfo->value.enumerated.item = num_chmodes - 1;
4985 sprintf(uinfo->value.enumerated.name, "%dch",
4986 chmode[uinfo->value.enumerated.item].channels);
4987 return 0;
4989 EXPORT_SYMBOL_HDA(snd_hda_ch_mode_info);
4992 * snd_hda_ch_mode_get - Get callback helper for the channel mode enum
4994 int snd_hda_ch_mode_get(struct hda_codec *codec,
4995 struct snd_ctl_elem_value *ucontrol,
4996 const struct hda_channel_mode *chmode,
4997 int num_chmodes,
4998 int max_channels)
5000 int i;
5002 for (i = 0; i < num_chmodes; i++) {
5003 if (max_channels == chmode[i].channels) {
5004 ucontrol->value.enumerated.item[0] = i;
5005 break;
5008 return 0;
5010 EXPORT_SYMBOL_HDA(snd_hda_ch_mode_get);
5013 * snd_hda_ch_mode_put - Put callback helper for the channel mode enum
5015 int snd_hda_ch_mode_put(struct hda_codec *codec,
5016 struct snd_ctl_elem_value *ucontrol,
5017 const struct hda_channel_mode *chmode,
5018 int num_chmodes,
5019 int *max_channelsp)
5021 unsigned int mode;
5023 mode = ucontrol->value.enumerated.item[0];
5024 if (mode >= num_chmodes)
5025 return -EINVAL;
5026 if (*max_channelsp == chmode[mode].channels)
5027 return 0;
5028 /* change the current channel setting */
5029 *max_channelsp = chmode[mode].channels;
5030 if (chmode[mode].sequence)
5031 snd_hda_sequence_write_cache(codec, chmode[mode].sequence);
5032 return 1;
5034 EXPORT_SYMBOL_HDA(snd_hda_ch_mode_put);
5037 * input MUX helper
5041 * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum
5043 int snd_hda_input_mux_info(const struct hda_input_mux *imux,
5044 struct snd_ctl_elem_info *uinfo)
5046 unsigned int index;
5048 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
5049 uinfo->count = 1;
5050 uinfo->value.enumerated.items = imux->num_items;
5051 if (!imux->num_items)
5052 return 0;
5053 index = uinfo->value.enumerated.item;
5054 if (index >= imux->num_items)
5055 index = imux->num_items - 1;
5056 strcpy(uinfo->value.enumerated.name, imux->items[index].label);
5057 return 0;
5059 EXPORT_SYMBOL_HDA(snd_hda_input_mux_info);
5062 * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum
5064 int snd_hda_input_mux_put(struct hda_codec *codec,
5065 const struct hda_input_mux *imux,
5066 struct snd_ctl_elem_value *ucontrol,
5067 hda_nid_t nid,
5068 unsigned int *cur_val)
5070 unsigned int idx;
5072 if (!imux->num_items)
5073 return 0;
5074 idx = ucontrol->value.enumerated.item[0];
5075 if (idx >= imux->num_items)
5076 idx = imux->num_items - 1;
5077 if (*cur_val == idx)
5078 return 0;
5079 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
5080 imux->items[idx].index);
5081 *cur_val = idx;
5082 return 1;
5084 EXPORT_SYMBOL_HDA(snd_hda_input_mux_put);
5088 * process kcontrol info callback of a simple string enum array
5089 * when @num_items is 0 or @texts is NULL, assume a boolean enum array
5091 int snd_hda_enum_helper_info(struct snd_kcontrol *kcontrol,
5092 struct snd_ctl_elem_info *uinfo,
5093 int num_items, const char * const *texts)
5095 static const char * const texts_default[] = {
5096 "Disabled", "Enabled"
5099 if (!texts || !num_items) {
5100 num_items = 2;
5101 texts = texts_default;
5104 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
5105 uinfo->count = 1;
5106 uinfo->value.enumerated.items = num_items;
5107 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
5108 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
5109 strcpy(uinfo->value.enumerated.name,
5110 texts[uinfo->value.enumerated.item]);
5111 return 0;
5113 EXPORT_SYMBOL_HDA(snd_hda_enum_helper_info);
5116 * Multi-channel / digital-out PCM helper functions
5119 /* setup SPDIF output stream */
5120 static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
5121 unsigned int stream_tag, unsigned int format)
5123 struct hda_spdif_out *spdif;
5124 unsigned int curr_fmt;
5125 bool reset;
5127 spdif = snd_hda_spdif_out_of_nid(codec, nid);
5128 curr_fmt = snd_hda_codec_read(codec, nid, 0,
5129 AC_VERB_GET_STREAM_FORMAT, 0);
5130 reset = codec->spdif_status_reset &&
5131 (spdif->ctls & AC_DIG1_ENABLE) &&
5132 curr_fmt != format;
5134 /* turn off SPDIF if needed; otherwise the IEC958 bits won't be
5135 updated */
5136 if (reset)
5137 set_dig_out_convert(codec, nid,
5138 spdif->ctls & ~AC_DIG1_ENABLE & 0xff,
5139 -1);
5140 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
5141 if (codec->slave_dig_outs) {
5142 const hda_nid_t *d;
5143 for (d = codec->slave_dig_outs; *d; d++)
5144 snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
5145 format);
5147 /* turn on again (if needed) */
5148 if (reset)
5149 set_dig_out_convert(codec, nid,
5150 spdif->ctls & 0xff, -1);
5153 static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
5155 snd_hda_codec_cleanup_stream(codec, nid);
5156 if (codec->slave_dig_outs) {
5157 const hda_nid_t *d;
5158 for (d = codec->slave_dig_outs; *d; d++)
5159 snd_hda_codec_cleanup_stream(codec, *d);
5164 * snd_hda_bus_reboot_notify - call the reboot notifier of each codec
5165 * @bus: HD-audio bus
5167 void snd_hda_bus_reboot_notify(struct hda_bus *bus)
5169 struct hda_codec *codec;
5171 if (!bus)
5172 return;
5173 list_for_each_entry(codec, &bus->codec_list, list) {
5174 if (hda_codec_is_power_on(codec) &&
5175 codec->patch_ops.reboot_notify)
5176 codec->patch_ops.reboot_notify(codec);
5179 EXPORT_SYMBOL_HDA(snd_hda_bus_reboot_notify);
5182 * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode
5184 int snd_hda_multi_out_dig_open(struct hda_codec *codec,
5185 struct hda_multi_out *mout)
5187 mutex_lock(&codec->spdif_mutex);
5188 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
5189 /* already opened as analog dup; reset it once */
5190 cleanup_dig_out_stream(codec, mout->dig_out_nid);
5191 mout->dig_out_used = HDA_DIG_EXCLUSIVE;
5192 mutex_unlock(&codec->spdif_mutex);
5193 return 0;
5195 EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_open);
5198 * snd_hda_multi_out_dig_prepare - prepare the digital out stream
5200 int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
5201 struct hda_multi_out *mout,
5202 unsigned int stream_tag,
5203 unsigned int format,
5204 struct snd_pcm_substream *substream)
5206 mutex_lock(&codec->spdif_mutex);
5207 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
5208 mutex_unlock(&codec->spdif_mutex);
5209 return 0;
5211 EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_prepare);
5214 * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream
5216 int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec,
5217 struct hda_multi_out *mout)
5219 mutex_lock(&codec->spdif_mutex);
5220 cleanup_dig_out_stream(codec, mout->dig_out_nid);
5221 mutex_unlock(&codec->spdif_mutex);
5222 return 0;
5224 EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_cleanup);
5227 * snd_hda_multi_out_dig_close - release the digital out stream
5229 int snd_hda_multi_out_dig_close(struct hda_codec *codec,
5230 struct hda_multi_out *mout)
5232 mutex_lock(&codec->spdif_mutex);
5233 mout->dig_out_used = 0;
5234 mutex_unlock(&codec->spdif_mutex);
5235 return 0;
5237 EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_close);
5240 * snd_hda_multi_out_analog_open - open analog outputs
5242 * Open analog outputs and set up the hw-constraints.
5243 * If the digital outputs can be opened as slave, open the digital
5244 * outputs, too.
5246 int snd_hda_multi_out_analog_open(struct hda_codec *codec,
5247 struct hda_multi_out *mout,
5248 struct snd_pcm_substream *substream,
5249 struct hda_pcm_stream *hinfo)
5251 struct snd_pcm_runtime *runtime = substream->runtime;
5252 runtime->hw.channels_max = mout->max_channels;
5253 if (mout->dig_out_nid) {
5254 if (!mout->analog_rates) {
5255 mout->analog_rates = hinfo->rates;
5256 mout->analog_formats = hinfo->formats;
5257 mout->analog_maxbps = hinfo->maxbps;
5258 } else {
5259 runtime->hw.rates = mout->analog_rates;
5260 runtime->hw.formats = mout->analog_formats;
5261 hinfo->maxbps = mout->analog_maxbps;
5263 if (!mout->spdif_rates) {
5264 snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
5265 &mout->spdif_rates,
5266 &mout->spdif_formats,
5267 &mout->spdif_maxbps);
5269 mutex_lock(&codec->spdif_mutex);
5270 if (mout->share_spdif) {
5271 if ((runtime->hw.rates & mout->spdif_rates) &&
5272 (runtime->hw.formats & mout->spdif_formats)) {
5273 runtime->hw.rates &= mout->spdif_rates;
5274 runtime->hw.formats &= mout->spdif_formats;
5275 if (mout->spdif_maxbps < hinfo->maxbps)
5276 hinfo->maxbps = mout->spdif_maxbps;
5277 } else {
5278 mout->share_spdif = 0;
5279 /* FIXME: need notify? */
5282 mutex_unlock(&codec->spdif_mutex);
5284 return snd_pcm_hw_constraint_step(substream->runtime, 0,
5285 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
5287 EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_open);
5290 * snd_hda_multi_out_analog_prepare - Preapre the analog outputs.
5292 * Set up the i/o for analog out.
5293 * When the digital out is available, copy the front out to digital out, too.
5295 int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
5296 struct hda_multi_out *mout,
5297 unsigned int stream_tag,
5298 unsigned int format,
5299 struct snd_pcm_substream *substream)
5301 const hda_nid_t *nids = mout->dac_nids;
5302 int chs = substream->runtime->channels;
5303 struct hda_spdif_out *spdif;
5304 int i;
5306 mutex_lock(&codec->spdif_mutex);
5307 spdif = snd_hda_spdif_out_of_nid(codec, mout->dig_out_nid);
5308 if (mout->dig_out_nid && mout->share_spdif &&
5309 mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
5310 if (chs == 2 &&
5311 snd_hda_is_supported_format(codec, mout->dig_out_nid,
5312 format) &&
5313 !(spdif->status & IEC958_AES0_NONAUDIO)) {
5314 mout->dig_out_used = HDA_DIG_ANALOG_DUP;
5315 setup_dig_out_stream(codec, mout->dig_out_nid,
5316 stream_tag, format);
5317 } else {
5318 mout->dig_out_used = 0;
5319 cleanup_dig_out_stream(codec, mout->dig_out_nid);
5322 mutex_unlock(&codec->spdif_mutex);
5324 /* front */
5325 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
5326 0, format);
5327 if (!mout->no_share_stream &&
5328 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
5329 /* headphone out will just decode front left/right (stereo) */
5330 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
5331 0, format);
5332 /* extra outputs copied from front */
5333 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
5334 if (!mout->no_share_stream && mout->hp_out_nid[i])
5335 snd_hda_codec_setup_stream(codec,
5336 mout->hp_out_nid[i],
5337 stream_tag, 0, format);
5338 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
5339 if (!mout->no_share_stream && mout->extra_out_nid[i])
5340 snd_hda_codec_setup_stream(codec,
5341 mout->extra_out_nid[i],
5342 stream_tag, 0, format);
5344 /* surrounds */
5345 for (i = 1; i < mout->num_dacs; i++) {
5346 if (chs >= (i + 1) * 2) /* independent out */
5347 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
5348 i * 2, format);
5349 else if (!mout->no_share_stream) /* copy front */
5350 snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
5351 0, format);
5353 return 0;
5355 EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_prepare);
5358 * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out
5360 int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
5361 struct hda_multi_out *mout)
5363 const hda_nid_t *nids = mout->dac_nids;
5364 int i;
5366 for (i = 0; i < mout->num_dacs; i++)
5367 snd_hda_codec_cleanup_stream(codec, nids[i]);
5368 if (mout->hp_nid)
5369 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
5370 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
5371 if (mout->hp_out_nid[i])
5372 snd_hda_codec_cleanup_stream(codec,
5373 mout->hp_out_nid[i]);
5374 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
5375 if (mout->extra_out_nid[i])
5376 snd_hda_codec_cleanup_stream(codec,
5377 mout->extra_out_nid[i]);
5378 mutex_lock(&codec->spdif_mutex);
5379 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
5380 cleanup_dig_out_stream(codec, mout->dig_out_nid);
5381 mout->dig_out_used = 0;
5383 mutex_unlock(&codec->spdif_mutex);
5384 return 0;
5386 EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_cleanup);
5389 * snd_hda_get_default_vref - Get the default (mic) VREF pin bits
5391 * Guess the suitable VREF pin bits to be set as the pin-control value.
5392 * Note: the function doesn't set the AC_PINCTL_IN_EN bit.
5394 unsigned int snd_hda_get_default_vref(struct hda_codec *codec, hda_nid_t pin)
5396 unsigned int pincap;
5397 unsigned int oldval;
5398 oldval = snd_hda_codec_read(codec, pin, 0,
5399 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
5400 pincap = snd_hda_query_pin_caps(codec, pin);
5401 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
5402 /* Exception: if the default pin setup is vref50, we give it priority */
5403 if ((pincap & AC_PINCAP_VREF_80) && oldval != PIN_VREF50)
5404 return AC_PINCTL_VREF_80;
5405 else if (pincap & AC_PINCAP_VREF_50)
5406 return AC_PINCTL_VREF_50;
5407 else if (pincap & AC_PINCAP_VREF_100)
5408 return AC_PINCTL_VREF_100;
5409 else if (pincap & AC_PINCAP_VREF_GRD)
5410 return AC_PINCTL_VREF_GRD;
5411 return AC_PINCTL_VREF_HIZ;
5413 EXPORT_SYMBOL_HDA(snd_hda_get_default_vref);
5415 /* correct the pin ctl value for matching with the pin cap */
5416 unsigned int snd_hda_correct_pin_ctl(struct hda_codec *codec,
5417 hda_nid_t pin, unsigned int val)
5419 static unsigned int cap_lists[][2] = {
5420 { AC_PINCTL_VREF_100, AC_PINCAP_VREF_100 },
5421 { AC_PINCTL_VREF_80, AC_PINCAP_VREF_80 },
5422 { AC_PINCTL_VREF_50, AC_PINCAP_VREF_50 },
5423 { AC_PINCTL_VREF_GRD, AC_PINCAP_VREF_GRD },
5425 unsigned int cap;
5427 if (!val)
5428 return 0;
5429 cap = snd_hda_query_pin_caps(codec, pin);
5430 if (!cap)
5431 return val; /* don't know what to do... */
5433 if (val & AC_PINCTL_OUT_EN) {
5434 if (!(cap & AC_PINCAP_OUT))
5435 val &= ~(AC_PINCTL_OUT_EN | AC_PINCTL_HP_EN);
5436 else if ((val & AC_PINCTL_HP_EN) && !(cap & AC_PINCAP_HP_DRV))
5437 val &= ~AC_PINCTL_HP_EN;
5440 if (val & AC_PINCTL_IN_EN) {
5441 if (!(cap & AC_PINCAP_IN))
5442 val &= ~(AC_PINCTL_IN_EN | AC_PINCTL_VREFEN);
5443 else {
5444 unsigned int vcap, vref;
5445 int i;
5446 vcap = (cap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
5447 vref = val & AC_PINCTL_VREFEN;
5448 for (i = 0; i < ARRAY_SIZE(cap_lists); i++) {
5449 if (vref == cap_lists[i][0] &&
5450 !(vcap & cap_lists[i][1])) {
5451 if (i == ARRAY_SIZE(cap_lists) - 1)
5452 vref = AC_PINCTL_VREF_HIZ;
5453 else
5454 vref = cap_lists[i + 1][0];
5457 val &= ~AC_PINCTL_VREFEN;
5458 val |= vref;
5462 return val;
5464 EXPORT_SYMBOL_HDA(snd_hda_correct_pin_ctl);
5466 int _snd_hda_set_pin_ctl(struct hda_codec *codec, hda_nid_t pin,
5467 unsigned int val, bool cached)
5469 val = snd_hda_correct_pin_ctl(codec, pin, val);
5470 snd_hda_codec_set_pin_target(codec, pin, val);
5471 if (cached)
5472 return snd_hda_codec_update_cache(codec, pin, 0,
5473 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
5474 else
5475 return snd_hda_codec_write(codec, pin, 0,
5476 AC_VERB_SET_PIN_WIDGET_CONTROL, val);
5478 EXPORT_SYMBOL_HDA(_snd_hda_set_pin_ctl);
5481 * snd_hda_add_imux_item - Add an item to input_mux
5483 * When the same label is used already in the existing items, the number
5484 * suffix is appended to the label. This label index number is stored
5485 * to type_idx when non-NULL pointer is given.
5487 int snd_hda_add_imux_item(struct hda_input_mux *imux, const char *label,
5488 int index, int *type_idx)
5490 int i, label_idx = 0;
5491 if (imux->num_items >= HDA_MAX_NUM_INPUTS) {
5492 snd_printd(KERN_ERR "hda_codec: Too many imux items!\n");
5493 return -EINVAL;
5495 for (i = 0; i < imux->num_items; i++) {
5496 if (!strncmp(label, imux->items[i].label, strlen(label)))
5497 label_idx++;
5499 if (type_idx)
5500 *type_idx = label_idx;
5501 if (label_idx > 0)
5502 snprintf(imux->items[imux->num_items].label,
5503 sizeof(imux->items[imux->num_items].label),
5504 "%s %d", label, label_idx);
5505 else
5506 strlcpy(imux->items[imux->num_items].label, label,
5507 sizeof(imux->items[imux->num_items].label));
5508 imux->items[imux->num_items].index = index;
5509 imux->num_items++;
5510 return 0;
5512 EXPORT_SYMBOL_HDA(snd_hda_add_imux_item);
5515 #ifdef CONFIG_PM
5517 * power management
5521 * snd_hda_suspend - suspend the codecs
5522 * @bus: the HDA bus
5524 * Returns 0 if successful.
5526 int snd_hda_suspend(struct hda_bus *bus)
5528 struct hda_codec *codec;
5530 list_for_each_entry(codec, &bus->codec_list, list) {
5531 cancel_delayed_work_sync(&codec->jackpoll_work);
5532 if (hda_codec_is_power_on(codec))
5533 hda_call_codec_suspend(codec, false);
5535 return 0;
5537 EXPORT_SYMBOL_HDA(snd_hda_suspend);
5540 * snd_hda_resume - resume the codecs
5541 * @bus: the HDA bus
5543 * Returns 0 if successful.
5545 int snd_hda_resume(struct hda_bus *bus)
5547 struct hda_codec *codec;
5549 list_for_each_entry(codec, &bus->codec_list, list) {
5550 hda_call_codec_resume(codec);
5552 return 0;
5554 EXPORT_SYMBOL_HDA(snd_hda_resume);
5555 #endif /* CONFIG_PM */
5558 * generic arrays
5562 * snd_array_new - get a new element from the given array
5563 * @array: the array object
5565 * Get a new element from the given array. If it exceeds the
5566 * pre-allocated array size, re-allocate the array.
5568 * Returns NULL if allocation failed.
5570 void *snd_array_new(struct snd_array *array)
5572 if (snd_BUG_ON(!array->elem_size))
5573 return NULL;
5574 if (array->used >= array->alloced) {
5575 int num = array->alloced + array->alloc_align;
5576 int size = (num + 1) * array->elem_size;
5577 void *nlist;
5578 if (snd_BUG_ON(num >= 4096))
5579 return NULL;
5580 nlist = krealloc(array->list, size, GFP_KERNEL | __GFP_ZERO);
5581 if (!nlist)
5582 return NULL;
5583 array->list = nlist;
5584 array->alloced = num;
5586 return snd_array_elem(array, array->used++);
5588 EXPORT_SYMBOL_HDA(snd_array_new);
5591 * snd_array_free - free the given array elements
5592 * @array: the array object
5594 void snd_array_free(struct snd_array *array)
5596 kfree(array->list);
5597 array->used = 0;
5598 array->alloced = 0;
5599 array->list = NULL;
5601 EXPORT_SYMBOL_HDA(snd_array_free);
5604 * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer
5605 * @pcm: PCM caps bits
5606 * @buf: the string buffer to write
5607 * @buflen: the max buffer length
5609 * used by hda_proc.c and hda_eld.c
5611 void snd_print_pcm_bits(int pcm, char *buf, int buflen)
5613 static unsigned int bits[] = { 8, 16, 20, 24, 32 };
5614 int i, j;
5616 for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
5617 if (pcm & (AC_SUPPCM_BITS_8 << i))
5618 j += snprintf(buf + j, buflen - j, " %d", bits[i]);
5620 buf[j] = '\0'; /* necessary when j == 0 */
5622 EXPORT_SYMBOL_HDA(snd_print_pcm_bits);
5624 MODULE_DESCRIPTION("HDA codec core");
5625 MODULE_LICENSE("GPL");