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 <sound/driver.h>
23 #include <linux/init.h>
24 #include <linux/delay.h>
25 #include <linux/slab.h>
26 #include <linux/pci.h>
27 #include <linux/moduleparam.h>
28 #include <linux/mutex.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 "hda_local.h"
37 MODULE_AUTHOR("Takashi Iwai <tiwai@suse.de>");
38 MODULE_DESCRIPTION("Universal interface for High Definition Audio Codec");
39 MODULE_LICENSE("GPL");
43 * vendor / preset table
46 struct hda_vendor_id
{
51 /* codec vendor labels */
52 static struct hda_vendor_id hda_vendor_ids
[] = {
53 { 0x10ec, "Realtek" },
54 { 0x1057, "Motorola" },
55 { 0x11d4, "Analog Devices" },
56 { 0x13f6, "C-Media" },
57 { 0x14f1, "Conexant" },
58 { 0x434d, "C-Media" },
59 { 0x8384, "SigmaTel" },
64 #include "hda_patch.h"
68 * snd_hda_codec_read - send a command and get the response
69 * @codec: the HDA codec
70 * @nid: NID to send the command
71 * @direct: direct flag
72 * @verb: the verb to send
73 * @parm: the parameter for the verb
75 * Send a single command and read the corresponding response.
77 * Returns the obtained response value, or -1 for an error.
79 unsigned int snd_hda_codec_read(struct hda_codec
*codec
, hda_nid_t nid
, int direct
,
80 unsigned int verb
, unsigned int parm
)
83 mutex_lock(&codec
->bus
->cmd_mutex
);
84 if (! codec
->bus
->ops
.command(codec
, nid
, direct
, verb
, parm
))
85 res
= codec
->bus
->ops
.get_response(codec
);
87 res
= (unsigned int)-1;
88 mutex_unlock(&codec
->bus
->cmd_mutex
);
92 EXPORT_SYMBOL(snd_hda_codec_read
);
95 * snd_hda_codec_write - send a single command without waiting for response
96 * @codec: the HDA codec
97 * @nid: NID to send the command
98 * @direct: direct flag
99 * @verb: the verb to send
100 * @parm: the parameter for the verb
102 * Send a single command without waiting for response.
104 * Returns 0 if successful, or a negative error code.
106 int snd_hda_codec_write(struct hda_codec
*codec
, hda_nid_t nid
, int direct
,
107 unsigned int verb
, unsigned int parm
)
110 mutex_lock(&codec
->bus
->cmd_mutex
);
111 err
= codec
->bus
->ops
.command(codec
, nid
, direct
, verb
, parm
);
112 mutex_unlock(&codec
->bus
->cmd_mutex
);
116 EXPORT_SYMBOL(snd_hda_codec_write
);
119 * snd_hda_sequence_write - sequence writes
120 * @codec: the HDA codec
121 * @seq: VERB array to send
123 * Send the commands sequentially from the given array.
124 * The array must be terminated with NID=0.
126 void snd_hda_sequence_write(struct hda_codec
*codec
, const struct hda_verb
*seq
)
128 for (; seq
->nid
; seq
++)
129 snd_hda_codec_write(codec
, seq
->nid
, 0, seq
->verb
, seq
->param
);
132 EXPORT_SYMBOL(snd_hda_sequence_write
);
135 * snd_hda_get_sub_nodes - get the range of sub nodes
136 * @codec: the HDA codec
138 * @start_id: the pointer to store the start NID
140 * Parse the NID and store the start NID of its sub-nodes.
141 * Returns the number of sub-nodes.
143 int snd_hda_get_sub_nodes(struct hda_codec
*codec
, hda_nid_t nid
, hda_nid_t
*start_id
)
147 parm
= snd_hda_param_read(codec
, nid
, AC_PAR_NODE_COUNT
);
148 *start_id
= (parm
>> 16) & 0x7fff;
149 return (int)(parm
& 0x7fff);
152 EXPORT_SYMBOL(snd_hda_get_sub_nodes
);
155 * snd_hda_get_connections - get connection list
156 * @codec: the HDA codec
158 * @conn_list: connection list array
159 * @max_conns: max. number of connections to store
161 * Parses the connection list of the given widget and stores the list
164 * Returns the number of connections, or a negative error code.
166 int snd_hda_get_connections(struct hda_codec
*codec
, hda_nid_t nid
,
167 hda_nid_t
*conn_list
, int max_conns
)
170 int i
, conn_len
, conns
;
171 unsigned int shift
, num_elems
, mask
;
174 snd_assert(conn_list
&& max_conns
> 0, return -EINVAL
);
176 parm
= snd_hda_param_read(codec
, nid
, AC_PAR_CONNLIST_LEN
);
177 if (parm
& AC_CLIST_LONG
) {
186 conn_len
= parm
& AC_CLIST_LENGTH
;
187 mask
= (1 << (shift
-1)) - 1;
190 return 0; /* no connection */
193 /* single connection */
194 parm
= snd_hda_codec_read(codec
, nid
, 0, AC_VERB_GET_CONNECT_LIST
, 0);
195 conn_list
[0] = parm
& mask
;
199 /* multi connection */
202 for (i
= 0; i
< conn_len
; i
++) {
206 if (i
% num_elems
== 0)
207 parm
= snd_hda_codec_read(codec
, nid
, 0,
208 AC_VERB_GET_CONNECT_LIST
, i
);
209 range_val
= !! (parm
& (1 << (shift
-1))); /* ranges */
213 /* ranges between the previous and this one */
214 if (! prev_nid
|| prev_nid
>= val
) {
215 snd_printk(KERN_WARNING
"hda_codec: invalid dep_range_val %x:%x\n", prev_nid
, val
);
218 for (n
= prev_nid
+ 1; n
<= val
; n
++) {
219 if (conns
>= max_conns
) {
220 snd_printk(KERN_ERR
"Too many connections\n");
223 conn_list
[conns
++] = n
;
226 if (conns
>= max_conns
) {
227 snd_printk(KERN_ERR
"Too many connections\n");
230 conn_list
[conns
++] = val
;
239 * snd_hda_queue_unsol_event - add an unsolicited event to queue
241 * @res: unsolicited event (lower 32bit of RIRB entry)
242 * @res_ex: codec addr and flags (upper 32bit or RIRB entry)
244 * Adds the given event to the queue. The events are processed in
245 * the workqueue asynchronously. Call this function in the interrupt
246 * hanlder when RIRB receives an unsolicited event.
248 * Returns 0 if successful, or a negative error code.
250 int snd_hda_queue_unsol_event(struct hda_bus
*bus
, u32 res
, u32 res_ex
)
252 struct hda_bus_unsolicited
*unsol
;
255 if ((unsol
= bus
->unsol
) == NULL
)
258 wp
= (unsol
->wp
+ 1) % HDA_UNSOL_QUEUE_SIZE
;
262 unsol
->queue
[wp
] = res
;
263 unsol
->queue
[wp
+ 1] = res_ex
;
265 queue_work(unsol
->workq
, &unsol
->work
);
270 EXPORT_SYMBOL(snd_hda_queue_unsol_event
);
273 * process queueud unsolicited events
275 static void process_unsol_events(struct work_struct
*work
)
277 struct hda_bus_unsolicited
*unsol
=
278 container_of(work
, struct hda_bus_unsolicited
, work
);
279 struct hda_bus
*bus
= unsol
->bus
;
280 struct hda_codec
*codec
;
281 unsigned int rp
, caddr
, res
;
283 while (unsol
->rp
!= unsol
->wp
) {
284 rp
= (unsol
->rp
+ 1) % HDA_UNSOL_QUEUE_SIZE
;
287 res
= unsol
->queue
[rp
];
288 caddr
= unsol
->queue
[rp
+ 1];
289 if (! (caddr
& (1 << 4))) /* no unsolicited event? */
291 codec
= bus
->caddr_tbl
[caddr
& 0x0f];
292 if (codec
&& codec
->patch_ops
.unsol_event
)
293 codec
->patch_ops
.unsol_event(codec
, res
);
298 * initialize unsolicited queue
300 static int init_unsol_queue(struct hda_bus
*bus
)
302 struct hda_bus_unsolicited
*unsol
;
304 if (bus
->unsol
) /* already initialized */
307 unsol
= kzalloc(sizeof(*unsol
), GFP_KERNEL
);
309 snd_printk(KERN_ERR
"hda_codec: can't allocate unsolicited queue\n");
312 unsol
->workq
= create_singlethread_workqueue("hda_codec");
313 if (! unsol
->workq
) {
314 snd_printk(KERN_ERR
"hda_codec: can't create workqueue\n");
318 INIT_WORK(&unsol
->work
, process_unsol_events
);
327 static void snd_hda_codec_free(struct hda_codec
*codec
);
329 static int snd_hda_bus_free(struct hda_bus
*bus
)
331 struct list_head
*p
, *n
;
336 destroy_workqueue(bus
->unsol
->workq
);
339 list_for_each_safe(p
, n
, &bus
->codec_list
) {
340 struct hda_codec
*codec
= list_entry(p
, struct hda_codec
, list
);
341 snd_hda_codec_free(codec
);
343 if (bus
->ops
.private_free
)
344 bus
->ops
.private_free(bus
);
349 static int snd_hda_bus_dev_free(struct snd_device
*device
)
351 struct hda_bus
*bus
= device
->device_data
;
352 return snd_hda_bus_free(bus
);
356 * snd_hda_bus_new - create a HDA bus
357 * @card: the card entry
358 * @temp: the template for hda_bus information
359 * @busp: the pointer to store the created bus instance
361 * Returns 0 if successful, or a negative error code.
363 int snd_hda_bus_new(struct snd_card
*card
, const struct hda_bus_template
*temp
,
364 struct hda_bus
**busp
)
368 static struct snd_device_ops dev_ops
= {
369 .dev_free
= snd_hda_bus_dev_free
,
372 snd_assert(temp
, return -EINVAL
);
373 snd_assert(temp
->ops
.command
&& temp
->ops
.get_response
, return -EINVAL
);
378 bus
= kzalloc(sizeof(*bus
), GFP_KERNEL
);
380 snd_printk(KERN_ERR
"can't allocate struct hda_bus\n");
385 bus
->private_data
= temp
->private_data
;
386 bus
->pci
= temp
->pci
;
387 bus
->modelname
= temp
->modelname
;
388 bus
->ops
= temp
->ops
;
390 mutex_init(&bus
->cmd_mutex
);
391 INIT_LIST_HEAD(&bus
->codec_list
);
393 if ((err
= snd_device_new(card
, SNDRV_DEV_BUS
, bus
, &dev_ops
)) < 0) {
394 snd_hda_bus_free(bus
);
402 EXPORT_SYMBOL(snd_hda_bus_new
);
405 * find a matching codec preset
407 static const struct hda_codec_preset
*find_codec_preset(struct hda_codec
*codec
)
409 const struct hda_codec_preset
**tbl
, *preset
;
411 for (tbl
= hda_preset_tables
; *tbl
; tbl
++) {
412 for (preset
= *tbl
; preset
->id
; preset
++) {
413 u32 mask
= preset
->mask
;
416 if (preset
->id
== (codec
->vendor_id
& mask
) &&
418 preset
->rev
== codec
->revision_id
))
426 * snd_hda_get_codec_name - store the codec name
428 void snd_hda_get_codec_name(struct hda_codec
*codec
,
429 char *name
, int namelen
)
431 const struct hda_vendor_id
*c
;
432 const char *vendor
= NULL
;
433 u16 vendor_id
= codec
->vendor_id
>> 16;
436 for (c
= hda_vendor_ids
; c
->id
; c
++) {
437 if (c
->id
== vendor_id
) {
443 sprintf(tmp
, "Generic %04x", vendor_id
);
446 if (codec
->preset
&& codec
->preset
->name
)
447 snprintf(name
, namelen
, "%s %s", vendor
, codec
->preset
->name
);
449 snprintf(name
, namelen
, "%s ID %x", vendor
, codec
->vendor_id
& 0xffff);
453 * look for an AFG and MFG nodes
455 static void setup_fg_nodes(struct hda_codec
*codec
)
460 total_nodes
= snd_hda_get_sub_nodes(codec
, AC_NODE_ROOT
, &nid
);
461 for (i
= 0; i
< total_nodes
; i
++, nid
++) {
462 switch((snd_hda_param_read(codec
, nid
, AC_PAR_FUNCTION_TYPE
) & 0xff)) {
463 case AC_GRP_AUDIO_FUNCTION
:
466 case AC_GRP_MODEM_FUNCTION
:
476 * read widget caps for each widget and store in cache
478 static int read_widget_caps(struct hda_codec
*codec
, hda_nid_t fg_node
)
483 codec
->num_nodes
= snd_hda_get_sub_nodes(codec
, fg_node
,
485 codec
->wcaps
= kmalloc(codec
->num_nodes
* 4, GFP_KERNEL
);
488 nid
= codec
->start_nid
;
489 for (i
= 0; i
< codec
->num_nodes
; i
++, nid
++)
490 codec
->wcaps
[i
] = snd_hda_param_read(codec
, nid
,
491 AC_PAR_AUDIO_WIDGET_CAP
);
499 static void snd_hda_codec_free(struct hda_codec
*codec
)
503 list_del(&codec
->list
);
504 codec
->bus
->caddr_tbl
[codec
->addr
] = NULL
;
505 if (codec
->patch_ops
.free
)
506 codec
->patch_ops
.free(codec
);
507 kfree(codec
->amp_info
);
512 static void init_amp_hash(struct hda_codec
*codec
);
515 * snd_hda_codec_new - create a HDA codec
516 * @bus: the bus to assign
517 * @codec_addr: the codec address
518 * @codecp: the pointer to store the generated codec
520 * Returns 0 if successful, or a negative error code.
522 int snd_hda_codec_new(struct hda_bus
*bus
, unsigned int codec_addr
,
523 struct hda_codec
**codecp
)
525 struct hda_codec
*codec
;
529 snd_assert(bus
, return -EINVAL
);
530 snd_assert(codec_addr
<= HDA_MAX_CODEC_ADDRESS
, return -EINVAL
);
532 if (bus
->caddr_tbl
[codec_addr
]) {
533 snd_printk(KERN_ERR
"hda_codec: address 0x%x is already occupied\n", codec_addr
);
537 codec
= kzalloc(sizeof(*codec
), GFP_KERNEL
);
539 snd_printk(KERN_ERR
"can't allocate struct hda_codec\n");
544 codec
->addr
= codec_addr
;
545 mutex_init(&codec
->spdif_mutex
);
546 init_amp_hash(codec
);
548 list_add_tail(&codec
->list
, &bus
->codec_list
);
549 bus
->caddr_tbl
[codec_addr
] = codec
;
551 codec
->vendor_id
= snd_hda_param_read(codec
, AC_NODE_ROOT
, AC_PAR_VENDOR_ID
);
552 if (codec
->vendor_id
== -1)
553 /* read again, hopefully the access method was corrected
554 * in the last read...
556 codec
->vendor_id
= snd_hda_param_read(codec
, AC_NODE_ROOT
,
558 codec
->subsystem_id
= snd_hda_param_read(codec
, AC_NODE_ROOT
, AC_PAR_SUBSYSTEM_ID
);
559 codec
->revision_id
= snd_hda_param_read(codec
, AC_NODE_ROOT
, AC_PAR_REV_ID
);
561 setup_fg_nodes(codec
);
562 if (! codec
->afg
&& ! codec
->mfg
) {
563 snd_printdd("hda_codec: no AFG or MFG node found\n");
564 snd_hda_codec_free(codec
);
568 if (read_widget_caps(codec
, codec
->afg
? codec
->afg
: codec
->mfg
) < 0) {
569 snd_printk(KERN_ERR
"hda_codec: cannot malloc\n");
570 snd_hda_codec_free(codec
);
574 if (! codec
->subsystem_id
) {
575 hda_nid_t nid
= codec
->afg
? codec
->afg
: codec
->mfg
;
576 codec
->subsystem_id
= snd_hda_codec_read(codec
, nid
, 0,
577 AC_VERB_GET_SUBSYSTEM_ID
,
581 codec
->preset
= find_codec_preset(codec
);
582 if (! *bus
->card
->mixername
)
583 snd_hda_get_codec_name(codec
, bus
->card
->mixername
,
584 sizeof(bus
->card
->mixername
));
586 if (codec
->preset
&& codec
->preset
->patch
)
587 err
= codec
->preset
->patch(codec
);
589 err
= snd_hda_parse_generic_codec(codec
);
591 snd_hda_codec_free(codec
);
595 if (codec
->patch_ops
.unsol_event
)
596 init_unsol_queue(bus
);
598 snd_hda_codec_proc_new(codec
);
600 sprintf(component
, "HDA:%08x", codec
->vendor_id
);
601 snd_component_add(codec
->bus
->card
, component
);
608 EXPORT_SYMBOL(snd_hda_codec_new
);
611 * snd_hda_codec_setup_stream - set up the codec for streaming
612 * @codec: the CODEC to set up
613 * @nid: the NID to set up
614 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
615 * @channel_id: channel id to pass, zero based.
616 * @format: stream format.
618 void snd_hda_codec_setup_stream(struct hda_codec
*codec
, hda_nid_t nid
, u32 stream_tag
,
619 int channel_id
, int format
)
624 snd_printdd("hda_codec_setup_stream: NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
625 nid
, stream_tag
, channel_id
, format
);
626 snd_hda_codec_write(codec
, nid
, 0, AC_VERB_SET_CHANNEL_STREAMID
,
627 (stream_tag
<< 4) | channel_id
);
629 snd_hda_codec_write(codec
, nid
, 0, AC_VERB_SET_STREAM_FORMAT
, format
);
632 EXPORT_SYMBOL(snd_hda_codec_setup_stream
);
635 * amp access functions
638 /* FIXME: more better hash key? */
639 #define HDA_HASH_KEY(nid,dir,idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
640 #define INFO_AMP_CAPS (1<<0)
641 #define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
643 /* initialize the hash table */
644 static void init_amp_hash(struct hda_codec
*codec
)
646 memset(codec
->amp_hash
, 0xff, sizeof(codec
->amp_hash
));
647 codec
->num_amp_entries
= 0;
648 codec
->amp_info_size
= 0;
649 codec
->amp_info
= NULL
;
652 /* query the hash. allocate an entry if not found. */
653 static struct hda_amp_info
*get_alloc_amp_hash(struct hda_codec
*codec
, u32 key
)
655 u16 idx
= key
% (u16
)ARRAY_SIZE(codec
->amp_hash
);
656 u16 cur
= codec
->amp_hash
[idx
];
657 struct hda_amp_info
*info
;
659 while (cur
!= 0xffff) {
660 info
= &codec
->amp_info
[cur
];
661 if (info
->key
== key
)
666 /* add a new hash entry */
667 if (codec
->num_amp_entries
>= codec
->amp_info_size
) {
668 /* reallocate the array */
669 int new_size
= codec
->amp_info_size
+ 64;
670 struct hda_amp_info
*new_info
= kcalloc(new_size
, sizeof(struct hda_amp_info
),
673 snd_printk(KERN_ERR
"hda_codec: can't malloc amp_info\n");
676 if (codec
->amp_info
) {
677 memcpy(new_info
, codec
->amp_info
,
678 codec
->amp_info_size
* sizeof(struct hda_amp_info
));
679 kfree(codec
->amp_info
);
681 codec
->amp_info_size
= new_size
;
682 codec
->amp_info
= new_info
;
684 cur
= codec
->num_amp_entries
++;
685 info
= &codec
->amp_info
[cur
];
687 info
->status
= 0; /* not initialized yet */
688 info
->next
= codec
->amp_hash
[idx
];
689 codec
->amp_hash
[idx
] = cur
;
695 * query AMP capabilities for the given widget and direction
697 static u32
query_amp_caps(struct hda_codec
*codec
, hda_nid_t nid
, int direction
)
699 struct hda_amp_info
*info
= get_alloc_amp_hash(codec
, HDA_HASH_KEY(nid
, direction
, 0));
703 if (! (info
->status
& INFO_AMP_CAPS
)) {
704 if (! (get_wcaps(codec
, nid
) & AC_WCAP_AMP_OVRD
))
706 info
->amp_caps
= snd_hda_param_read(codec
, nid
, direction
== HDA_OUTPUT
?
707 AC_PAR_AMP_OUT_CAP
: AC_PAR_AMP_IN_CAP
);
708 info
->status
|= INFO_AMP_CAPS
;
710 return info
->amp_caps
;
714 * read the current volume to info
715 * if the cache exists, read the cache value.
717 static unsigned int get_vol_mute(struct hda_codec
*codec
, struct hda_amp_info
*info
,
718 hda_nid_t nid
, int ch
, int direction
, int index
)
722 if (info
->status
& INFO_AMP_VOL(ch
))
723 return info
->vol
[ch
];
725 parm
= ch
? AC_AMP_GET_RIGHT
: AC_AMP_GET_LEFT
;
726 parm
|= direction
== HDA_OUTPUT
? AC_AMP_GET_OUTPUT
: AC_AMP_GET_INPUT
;
728 val
= snd_hda_codec_read(codec
, nid
, 0, AC_VERB_GET_AMP_GAIN_MUTE
, parm
);
729 info
->vol
[ch
] = val
& 0xff;
730 info
->status
|= INFO_AMP_VOL(ch
);
731 return info
->vol
[ch
];
735 * write the current volume in info to the h/w and update the cache
737 static void put_vol_mute(struct hda_codec
*codec
, struct hda_amp_info
*info
,
738 hda_nid_t nid
, int ch
, int direction
, int index
, int val
)
742 parm
= ch
? AC_AMP_SET_RIGHT
: AC_AMP_SET_LEFT
;
743 parm
|= direction
== HDA_OUTPUT
? AC_AMP_SET_OUTPUT
: AC_AMP_SET_INPUT
;
744 parm
|= index
<< AC_AMP_SET_INDEX_SHIFT
;
746 snd_hda_codec_write(codec
, nid
, 0, AC_VERB_SET_AMP_GAIN_MUTE
, parm
);
751 * read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
753 int snd_hda_codec_amp_read(struct hda_codec
*codec
, hda_nid_t nid
, int ch
,
754 int direction
, int index
)
756 struct hda_amp_info
*info
= get_alloc_amp_hash(codec
, HDA_HASH_KEY(nid
, direction
, index
));
759 return get_vol_mute(codec
, info
, nid
, ch
, direction
, index
);
763 * update the AMP value, mask = bit mask to set, val = the value
765 int snd_hda_codec_amp_update(struct hda_codec
*codec
, hda_nid_t nid
, int ch
,
766 int direction
, int idx
, int mask
, int val
)
768 struct hda_amp_info
*info
= get_alloc_amp_hash(codec
, HDA_HASH_KEY(nid
, direction
, idx
));
773 val
|= get_vol_mute(codec
, info
, nid
, ch
, direction
, idx
) & ~mask
;
774 if (info
->vol
[ch
] == val
&& ! codec
->in_resume
)
776 put_vol_mute(codec
, info
, nid
, ch
, direction
, idx
, val
);
782 * AMP control callbacks
784 /* retrieve parameters from private_value */
785 #define get_amp_nid(kc) ((kc)->private_value & 0xffff)
786 #define get_amp_channels(kc) (((kc)->private_value >> 16) & 0x3)
787 #define get_amp_direction(kc) (((kc)->private_value >> 18) & 0x1)
788 #define get_amp_index(kc) (((kc)->private_value >> 19) & 0xf)
791 int snd_hda_mixer_amp_volume_info(struct snd_kcontrol
*kcontrol
, struct snd_ctl_elem_info
*uinfo
)
793 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
794 u16 nid
= get_amp_nid(kcontrol
);
795 u8 chs
= get_amp_channels(kcontrol
);
796 int dir
= get_amp_direction(kcontrol
);
799 caps
= query_amp_caps(codec
, nid
, dir
);
800 caps
= (caps
& AC_AMPCAP_NUM_STEPS
) >> AC_AMPCAP_NUM_STEPS_SHIFT
; /* num steps */
802 printk(KERN_WARNING
"hda_codec: num_steps = 0 for NID=0x%x\n", nid
);
805 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
806 uinfo
->count
= chs
== 3 ? 2 : 1;
807 uinfo
->value
.integer
.min
= 0;
808 uinfo
->value
.integer
.max
= caps
;
812 int snd_hda_mixer_amp_volume_get(struct snd_kcontrol
*kcontrol
, struct snd_ctl_elem_value
*ucontrol
)
814 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
815 hda_nid_t nid
= get_amp_nid(kcontrol
);
816 int chs
= get_amp_channels(kcontrol
);
817 int dir
= get_amp_direction(kcontrol
);
818 int idx
= get_amp_index(kcontrol
);
819 long *valp
= ucontrol
->value
.integer
.value
;
822 *valp
++ = snd_hda_codec_amp_read(codec
, nid
, 0, dir
, idx
) & 0x7f;
824 *valp
= snd_hda_codec_amp_read(codec
, nid
, 1, dir
, idx
) & 0x7f;
828 int snd_hda_mixer_amp_volume_put(struct snd_kcontrol
*kcontrol
, struct snd_ctl_elem_value
*ucontrol
)
830 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
831 hda_nid_t nid
= get_amp_nid(kcontrol
);
832 int chs
= get_amp_channels(kcontrol
);
833 int dir
= get_amp_direction(kcontrol
);
834 int idx
= get_amp_index(kcontrol
);
835 long *valp
= ucontrol
->value
.integer
.value
;
839 change
= snd_hda_codec_amp_update(codec
, nid
, 0, dir
, idx
,
844 change
|= snd_hda_codec_amp_update(codec
, nid
, 1, dir
, idx
,
849 int snd_hda_mixer_amp_tlv(struct snd_kcontrol
*kcontrol
, int op_flag
,
850 unsigned int size
, unsigned int __user
*_tlv
)
852 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
853 hda_nid_t nid
= get_amp_nid(kcontrol
);
854 int dir
= get_amp_direction(kcontrol
);
855 u32 caps
, val1
, val2
;
857 if (size
< 4 * sizeof(unsigned int))
859 caps
= query_amp_caps(codec
, nid
, dir
);
860 val2
= (((caps
& AC_AMPCAP_STEP_SIZE
) >> AC_AMPCAP_STEP_SIZE_SHIFT
) + 1) * 25;
861 val1
= -((caps
& AC_AMPCAP_OFFSET
) >> AC_AMPCAP_OFFSET_SHIFT
);
862 val1
= ((int)val1
) * ((int)val2
);
863 if (put_user(SNDRV_CTL_TLVT_DB_SCALE
, _tlv
))
865 if (put_user(2 * sizeof(unsigned int), _tlv
+ 1))
867 if (put_user(val1
, _tlv
+ 2))
869 if (put_user(val2
, _tlv
+ 3))
875 int snd_hda_mixer_amp_switch_info(struct snd_kcontrol
*kcontrol
, struct snd_ctl_elem_info
*uinfo
)
877 int chs
= get_amp_channels(kcontrol
);
879 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_BOOLEAN
;
880 uinfo
->count
= chs
== 3 ? 2 : 1;
881 uinfo
->value
.integer
.min
= 0;
882 uinfo
->value
.integer
.max
= 1;
886 int snd_hda_mixer_amp_switch_get(struct snd_kcontrol
*kcontrol
, struct snd_ctl_elem_value
*ucontrol
)
888 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
889 hda_nid_t nid
= get_amp_nid(kcontrol
);
890 int chs
= get_amp_channels(kcontrol
);
891 int dir
= get_amp_direction(kcontrol
);
892 int idx
= get_amp_index(kcontrol
);
893 long *valp
= ucontrol
->value
.integer
.value
;
896 *valp
++ = (snd_hda_codec_amp_read(codec
, nid
, 0, dir
, idx
) & 0x80) ? 0 : 1;
898 *valp
= (snd_hda_codec_amp_read(codec
, nid
, 1, dir
, idx
) & 0x80) ? 0 : 1;
902 int snd_hda_mixer_amp_switch_put(struct snd_kcontrol
*kcontrol
, struct snd_ctl_elem_value
*ucontrol
)
904 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
905 hda_nid_t nid
= get_amp_nid(kcontrol
);
906 int chs
= get_amp_channels(kcontrol
);
907 int dir
= get_amp_direction(kcontrol
);
908 int idx
= get_amp_index(kcontrol
);
909 long *valp
= ucontrol
->value
.integer
.value
;
913 change
= snd_hda_codec_amp_update(codec
, nid
, 0, dir
, idx
,
914 0x80, *valp
? 0 : 0x80);
918 change
|= snd_hda_codec_amp_update(codec
, nid
, 1, dir
, idx
,
919 0x80, *valp
? 0 : 0x80);
925 * bound volume controls
927 * bind multiple volumes (# indices, from 0)
930 #define AMP_VAL_IDX_SHIFT 19
931 #define AMP_VAL_IDX_MASK (0x0f<<19)
933 int snd_hda_mixer_bind_switch_get(struct snd_kcontrol
*kcontrol
, struct snd_ctl_elem_value
*ucontrol
)
935 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
939 mutex_lock(&codec
->spdif_mutex
); /* reuse spdif_mutex */
940 pval
= kcontrol
->private_value
;
941 kcontrol
->private_value
= pval
& ~AMP_VAL_IDX_MASK
; /* index 0 */
942 err
= snd_hda_mixer_amp_switch_get(kcontrol
, ucontrol
);
943 kcontrol
->private_value
= pval
;
944 mutex_unlock(&codec
->spdif_mutex
);
948 int snd_hda_mixer_bind_switch_put(struct snd_kcontrol
*kcontrol
, struct snd_ctl_elem_value
*ucontrol
)
950 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
952 int i
, indices
, err
= 0, change
= 0;
954 mutex_lock(&codec
->spdif_mutex
); /* reuse spdif_mutex */
955 pval
= kcontrol
->private_value
;
956 indices
= (pval
& AMP_VAL_IDX_MASK
) >> AMP_VAL_IDX_SHIFT
;
957 for (i
= 0; i
< indices
; i
++) {
958 kcontrol
->private_value
= (pval
& ~AMP_VAL_IDX_MASK
) | (i
<< AMP_VAL_IDX_SHIFT
);
959 err
= snd_hda_mixer_amp_switch_put(kcontrol
, ucontrol
);
964 kcontrol
->private_value
= pval
;
965 mutex_unlock(&codec
->spdif_mutex
);
966 return err
< 0 ? err
: change
;
973 static int snd_hda_spdif_mask_info(struct snd_kcontrol
*kcontrol
, struct snd_ctl_elem_info
*uinfo
)
975 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_IEC958
;
980 static int snd_hda_spdif_cmask_get(struct snd_kcontrol
*kcontrol
, struct snd_ctl_elem_value
*ucontrol
)
982 ucontrol
->value
.iec958
.status
[0] = IEC958_AES0_PROFESSIONAL
|
983 IEC958_AES0_NONAUDIO
|
984 IEC958_AES0_CON_EMPHASIS_5015
|
985 IEC958_AES0_CON_NOT_COPYRIGHT
;
986 ucontrol
->value
.iec958
.status
[1] = IEC958_AES1_CON_CATEGORY
|
987 IEC958_AES1_CON_ORIGINAL
;
991 static int snd_hda_spdif_pmask_get(struct snd_kcontrol
*kcontrol
, struct snd_ctl_elem_value
*ucontrol
)
993 ucontrol
->value
.iec958
.status
[0] = IEC958_AES0_PROFESSIONAL
|
994 IEC958_AES0_NONAUDIO
|
995 IEC958_AES0_PRO_EMPHASIS_5015
;
999 static int snd_hda_spdif_default_get(struct snd_kcontrol
*kcontrol
, struct snd_ctl_elem_value
*ucontrol
)
1001 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
1003 ucontrol
->value
.iec958
.status
[0] = codec
->spdif_status
& 0xff;
1004 ucontrol
->value
.iec958
.status
[1] = (codec
->spdif_status
>> 8) & 0xff;
1005 ucontrol
->value
.iec958
.status
[2] = (codec
->spdif_status
>> 16) & 0xff;
1006 ucontrol
->value
.iec958
.status
[3] = (codec
->spdif_status
>> 24) & 0xff;
1011 /* convert from SPDIF status bits to HDA SPDIF bits
1012 * bit 0 (DigEn) is always set zero (to be filled later)
1014 static unsigned short convert_from_spdif_status(unsigned int sbits
)
1016 unsigned short val
= 0;
1018 if (sbits
& IEC958_AES0_PROFESSIONAL
)
1020 if (sbits
& IEC958_AES0_NONAUDIO
)
1022 if (sbits
& IEC958_AES0_PROFESSIONAL
) {
1023 if ((sbits
& IEC958_AES0_PRO_EMPHASIS
) == IEC958_AES0_PRO_EMPHASIS_5015
)
1026 if ((sbits
& IEC958_AES0_CON_EMPHASIS
) == IEC958_AES0_CON_EMPHASIS_5015
)
1028 if (! (sbits
& IEC958_AES0_CON_NOT_COPYRIGHT
))
1030 if (sbits
& (IEC958_AES1_CON_ORIGINAL
<< 8))
1032 val
|= sbits
& (IEC958_AES1_CON_CATEGORY
<< 8);
1037 /* convert to SPDIF status bits from HDA SPDIF bits
1039 static unsigned int convert_to_spdif_status(unsigned short val
)
1041 unsigned int sbits
= 0;
1044 sbits
|= IEC958_AES0_NONAUDIO
;
1046 sbits
|= IEC958_AES0_PROFESSIONAL
;
1047 if (sbits
& IEC958_AES0_PROFESSIONAL
) {
1048 if (sbits
& (1 << 3))
1049 sbits
|= IEC958_AES0_PRO_EMPHASIS_5015
;
1052 sbits
|= IEC958_AES0_CON_EMPHASIS_5015
;
1053 if (! (val
& (1 << 4)))
1054 sbits
|= IEC958_AES0_CON_NOT_COPYRIGHT
;
1056 sbits
|= (IEC958_AES1_CON_ORIGINAL
<< 8);
1057 sbits
|= val
& (0x7f << 8);
1062 static int snd_hda_spdif_default_put(struct snd_kcontrol
*kcontrol
, struct snd_ctl_elem_value
*ucontrol
)
1064 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
1065 hda_nid_t nid
= kcontrol
->private_value
;
1069 mutex_lock(&codec
->spdif_mutex
);
1070 codec
->spdif_status
= ucontrol
->value
.iec958
.status
[0] |
1071 ((unsigned int)ucontrol
->value
.iec958
.status
[1] << 8) |
1072 ((unsigned int)ucontrol
->value
.iec958
.status
[2] << 16) |
1073 ((unsigned int)ucontrol
->value
.iec958
.status
[3] << 24);
1074 val
= convert_from_spdif_status(codec
->spdif_status
);
1075 val
|= codec
->spdif_ctls
& 1;
1076 change
= codec
->spdif_ctls
!= val
;
1077 codec
->spdif_ctls
= val
;
1079 if (change
|| codec
->in_resume
) {
1080 snd_hda_codec_write(codec
, nid
, 0, AC_VERB_SET_DIGI_CONVERT_1
, val
& 0xff);
1081 snd_hda_codec_write(codec
, nid
, 0, AC_VERB_SET_DIGI_CONVERT_2
, val
>> 8);
1084 mutex_unlock(&codec
->spdif_mutex
);
1088 static int snd_hda_spdif_out_switch_info(struct snd_kcontrol
*kcontrol
, struct snd_ctl_elem_info
*uinfo
)
1090 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_BOOLEAN
;
1092 uinfo
->value
.integer
.min
= 0;
1093 uinfo
->value
.integer
.max
= 1;
1097 static int snd_hda_spdif_out_switch_get(struct snd_kcontrol
*kcontrol
, struct snd_ctl_elem_value
*ucontrol
)
1099 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
1101 ucontrol
->value
.integer
.value
[0] = codec
->spdif_ctls
& 1;
1105 static int snd_hda_spdif_out_switch_put(struct snd_kcontrol
*kcontrol
, struct snd_ctl_elem_value
*ucontrol
)
1107 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
1108 hda_nid_t nid
= kcontrol
->private_value
;
1112 mutex_lock(&codec
->spdif_mutex
);
1113 val
= codec
->spdif_ctls
& ~1;
1114 if (ucontrol
->value
.integer
.value
[0])
1116 change
= codec
->spdif_ctls
!= val
;
1117 if (change
|| codec
->in_resume
) {
1118 codec
->spdif_ctls
= val
;
1119 snd_hda_codec_write(codec
, nid
, 0, AC_VERB_SET_DIGI_CONVERT_1
, val
& 0xff);
1120 snd_hda_codec_write(codec
, nid
, 0, AC_VERB_SET_AMP_GAIN_MUTE
,
1121 AC_AMP_SET_RIGHT
| AC_AMP_SET_LEFT
|
1122 AC_AMP_SET_OUTPUT
| ((val
& 1) ? 0 : 0x80));
1124 mutex_unlock(&codec
->spdif_mutex
);
1128 static struct snd_kcontrol_new dig_mixes
[] = {
1130 .access
= SNDRV_CTL_ELEM_ACCESS_READ
,
1131 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
1132 .name
= SNDRV_CTL_NAME_IEC958("",PLAYBACK
,CON_MASK
),
1133 .info
= snd_hda_spdif_mask_info
,
1134 .get
= snd_hda_spdif_cmask_get
,
1137 .access
= SNDRV_CTL_ELEM_ACCESS_READ
,
1138 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
1139 .name
= SNDRV_CTL_NAME_IEC958("",PLAYBACK
,PRO_MASK
),
1140 .info
= snd_hda_spdif_mask_info
,
1141 .get
= snd_hda_spdif_pmask_get
,
1144 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
1145 .name
= SNDRV_CTL_NAME_IEC958("",PLAYBACK
,DEFAULT
),
1146 .info
= snd_hda_spdif_mask_info
,
1147 .get
= snd_hda_spdif_default_get
,
1148 .put
= snd_hda_spdif_default_put
,
1151 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
1152 .name
= SNDRV_CTL_NAME_IEC958("",PLAYBACK
,SWITCH
),
1153 .info
= snd_hda_spdif_out_switch_info
,
1154 .get
= snd_hda_spdif_out_switch_get
,
1155 .put
= snd_hda_spdif_out_switch_put
,
1161 * snd_hda_create_spdif_out_ctls - create Output SPDIF-related controls
1162 * @codec: the HDA codec
1163 * @nid: audio out widget NID
1165 * Creates controls related with the SPDIF output.
1166 * Called from each patch supporting the SPDIF out.
1168 * Returns 0 if successful, or a negative error code.
1170 int snd_hda_create_spdif_out_ctls(struct hda_codec
*codec
, hda_nid_t nid
)
1173 struct snd_kcontrol
*kctl
;
1174 struct snd_kcontrol_new
*dig_mix
;
1176 for (dig_mix
= dig_mixes
; dig_mix
->name
; dig_mix
++) {
1177 kctl
= snd_ctl_new1(dig_mix
, codec
);
1178 kctl
->private_value
= nid
;
1179 if ((err
= snd_ctl_add(codec
->bus
->card
, kctl
)) < 0)
1182 codec
->spdif_ctls
= snd_hda_codec_read(codec
, nid
, 0, AC_VERB_GET_DIGI_CONVERT
, 0);
1183 codec
->spdif_status
= convert_to_spdif_status(codec
->spdif_ctls
);
1191 #define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
1193 static int snd_hda_spdif_in_switch_get(struct snd_kcontrol
*kcontrol
, struct snd_ctl_elem_value
*ucontrol
)
1195 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
1197 ucontrol
->value
.integer
.value
[0] = codec
->spdif_in_enable
;
1201 static int snd_hda_spdif_in_switch_put(struct snd_kcontrol
*kcontrol
, struct snd_ctl_elem_value
*ucontrol
)
1203 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
1204 hda_nid_t nid
= kcontrol
->private_value
;
1205 unsigned int val
= !!ucontrol
->value
.integer
.value
[0];
1208 mutex_lock(&codec
->spdif_mutex
);
1209 change
= codec
->spdif_in_enable
!= val
;
1210 if (change
|| codec
->in_resume
) {
1211 codec
->spdif_in_enable
= val
;
1212 snd_hda_codec_write(codec
, nid
, 0, AC_VERB_SET_DIGI_CONVERT_1
, val
);
1214 mutex_unlock(&codec
->spdif_mutex
);
1218 static int snd_hda_spdif_in_status_get(struct snd_kcontrol
*kcontrol
, struct snd_ctl_elem_value
*ucontrol
)
1220 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
1221 hda_nid_t nid
= kcontrol
->private_value
;
1225 val
= snd_hda_codec_read(codec
, nid
, 0, AC_VERB_GET_DIGI_CONVERT
, 0);
1226 sbits
= convert_to_spdif_status(val
);
1227 ucontrol
->value
.iec958
.status
[0] = sbits
;
1228 ucontrol
->value
.iec958
.status
[1] = sbits
>> 8;
1229 ucontrol
->value
.iec958
.status
[2] = sbits
>> 16;
1230 ucontrol
->value
.iec958
.status
[3] = sbits
>> 24;
1234 static struct snd_kcontrol_new dig_in_ctls
[] = {
1236 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
1237 .name
= SNDRV_CTL_NAME_IEC958("",CAPTURE
,SWITCH
),
1238 .info
= snd_hda_spdif_in_switch_info
,
1239 .get
= snd_hda_spdif_in_switch_get
,
1240 .put
= snd_hda_spdif_in_switch_put
,
1243 .access
= SNDRV_CTL_ELEM_ACCESS_READ
,
1244 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
1245 .name
= SNDRV_CTL_NAME_IEC958("",CAPTURE
,DEFAULT
),
1246 .info
= snd_hda_spdif_mask_info
,
1247 .get
= snd_hda_spdif_in_status_get
,
1253 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
1254 * @codec: the HDA codec
1255 * @nid: audio in widget NID
1257 * Creates controls related with the SPDIF input.
1258 * Called from each patch supporting the SPDIF in.
1260 * Returns 0 if successful, or a negative error code.
1262 int snd_hda_create_spdif_in_ctls(struct hda_codec
*codec
, hda_nid_t nid
)
1265 struct snd_kcontrol
*kctl
;
1266 struct snd_kcontrol_new
*dig_mix
;
1268 for (dig_mix
= dig_in_ctls
; dig_mix
->name
; dig_mix
++) {
1269 kctl
= snd_ctl_new1(dig_mix
, codec
);
1270 kctl
->private_value
= nid
;
1271 if ((err
= snd_ctl_add(codec
->bus
->card
, kctl
)) < 0)
1274 codec
->spdif_in_enable
= snd_hda_codec_read(codec
, nid
, 0, AC_VERB_GET_DIGI_CONVERT
, 0) & 1;
1280 * set power state of the codec
1282 static void hda_set_power_state(struct hda_codec
*codec
, hda_nid_t fg
,
1283 unsigned int power_state
)
1285 hda_nid_t nid
, nid_start
;
1288 snd_hda_codec_write(codec
, fg
, 0, AC_VERB_SET_POWER_STATE
,
1291 nodes
= snd_hda_get_sub_nodes(codec
, fg
, &nid_start
);
1292 for (nid
= nid_start
; nid
< nodes
+ nid_start
; nid
++) {
1293 if (get_wcaps(codec
, nid
) & AC_WCAP_POWER
)
1294 snd_hda_codec_write(codec
, nid
, 0,
1295 AC_VERB_SET_POWER_STATE
,
1299 if (power_state
== AC_PWRST_D0
)
1305 * snd_hda_build_controls - build mixer controls
1308 * Creates mixer controls for each codec included in the bus.
1310 * Returns 0 if successful, otherwise a negative error code.
1312 int snd_hda_build_controls(struct hda_bus
*bus
)
1314 struct list_head
*p
;
1316 /* build controls */
1317 list_for_each(p
, &bus
->codec_list
) {
1318 struct hda_codec
*codec
= list_entry(p
, struct hda_codec
, list
);
1320 if (! codec
->patch_ops
.build_controls
)
1322 err
= codec
->patch_ops
.build_controls(codec
);
1328 list_for_each(p
, &bus
->codec_list
) {
1329 struct hda_codec
*codec
= list_entry(p
, struct hda_codec
, list
);
1331 hda_set_power_state(codec
,
1332 codec
->afg
? codec
->afg
: codec
->mfg
,
1334 if (! codec
->patch_ops
.init
)
1336 err
= codec
->patch_ops
.init(codec
);
1343 EXPORT_SYMBOL(snd_hda_build_controls
);
1348 struct hda_rate_tbl
{
1350 unsigned int alsa_bits
;
1351 unsigned int hda_fmt
;
1354 static struct hda_rate_tbl rate_bits
[] = {
1355 /* rate in Hz, ALSA rate bitmask, HDA format value */
1357 /* autodetected value used in snd_hda_query_supported_pcm */
1358 { 8000, SNDRV_PCM_RATE_8000
, 0x0500 }, /* 1/6 x 48 */
1359 { 11025, SNDRV_PCM_RATE_11025
, 0x4300 }, /* 1/4 x 44 */
1360 { 16000, SNDRV_PCM_RATE_16000
, 0x0200 }, /* 1/3 x 48 */
1361 { 22050, SNDRV_PCM_RATE_22050
, 0x4100 }, /* 1/2 x 44 */
1362 { 32000, SNDRV_PCM_RATE_32000
, 0x0a00 }, /* 2/3 x 48 */
1363 { 44100, SNDRV_PCM_RATE_44100
, 0x4000 }, /* 44 */
1364 { 48000, SNDRV_PCM_RATE_48000
, 0x0000 }, /* 48 */
1365 { 88200, SNDRV_PCM_RATE_88200
, 0x4800 }, /* 2 x 44 */
1366 { 96000, SNDRV_PCM_RATE_96000
, 0x0800 }, /* 2 x 48 */
1367 { 176400, SNDRV_PCM_RATE_176400
, 0x5800 },/* 4 x 44 */
1368 { 192000, SNDRV_PCM_RATE_192000
, 0x1800 }, /* 4 x 48 */
1370 /* not autodetected value */
1371 { 9600, SNDRV_PCM_RATE_KNOT
, 0x0400 }, /* 1/5 x 48 */
1373 { 0 } /* terminator */
1377 * snd_hda_calc_stream_format - calculate format bitset
1378 * @rate: the sample rate
1379 * @channels: the number of channels
1380 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
1381 * @maxbps: the max. bps
1383 * Calculate the format bitset from the given rate, channels and th PCM format.
1385 * Return zero if invalid.
1387 unsigned int snd_hda_calc_stream_format(unsigned int rate
,
1388 unsigned int channels
,
1389 unsigned int format
,
1390 unsigned int maxbps
)
1393 unsigned int val
= 0;
1395 for (i
= 0; rate_bits
[i
].hz
; i
++)
1396 if (rate_bits
[i
].hz
== rate
) {
1397 val
= rate_bits
[i
].hda_fmt
;
1400 if (! rate_bits
[i
].hz
) {
1401 snd_printdd("invalid rate %d\n", rate
);
1405 if (channels
== 0 || channels
> 8) {
1406 snd_printdd("invalid channels %d\n", channels
);
1409 val
|= channels
- 1;
1411 switch (snd_pcm_format_width(format
)) {
1412 case 8: val
|= 0x00; break;
1413 case 16: val
|= 0x10; break;
1419 else if (maxbps
>= 24)
1425 snd_printdd("invalid format width %d\n", snd_pcm_format_width(format
));
1432 EXPORT_SYMBOL(snd_hda_calc_stream_format
);
1435 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
1436 * @codec: the HDA codec
1437 * @nid: NID to query
1438 * @ratesp: the pointer to store the detected rate bitflags
1439 * @formatsp: the pointer to store the detected formats
1440 * @bpsp: the pointer to store the detected format widths
1442 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
1443 * or @bsps argument is ignored.
1445 * Returns 0 if successful, otherwise a negative error code.
1447 int snd_hda_query_supported_pcm(struct hda_codec
*codec
, hda_nid_t nid
,
1448 u32
*ratesp
, u64
*formatsp
, unsigned int *bpsp
)
1451 unsigned int val
, streams
;
1454 if (nid
!= codec
->afg
&&
1455 (get_wcaps(codec
, nid
) & AC_WCAP_FORMAT_OVRD
)) {
1456 val
= snd_hda_param_read(codec
, nid
, AC_PAR_PCM
);
1461 val
= snd_hda_param_read(codec
, codec
->afg
, AC_PAR_PCM
);
1465 for (i
= 0; rate_bits
[i
].hz
; i
++) {
1467 rates
|= rate_bits
[i
].alsa_bits
;
1472 if (formatsp
|| bpsp
) {
1477 wcaps
= get_wcaps(codec
, nid
);
1478 streams
= snd_hda_param_read(codec
, nid
, AC_PAR_STREAM
);
1482 streams
= snd_hda_param_read(codec
, codec
->afg
, AC_PAR_STREAM
);
1488 if (streams
& AC_SUPFMT_PCM
) {
1489 if (val
& AC_SUPPCM_BITS_8
) {
1490 formats
|= SNDRV_PCM_FMTBIT_U8
;
1493 if (val
& AC_SUPPCM_BITS_16
) {
1494 formats
|= SNDRV_PCM_FMTBIT_S16_LE
;
1497 if (wcaps
& AC_WCAP_DIGITAL
) {
1498 if (val
& AC_SUPPCM_BITS_32
)
1499 formats
|= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
;
1500 if (val
& (AC_SUPPCM_BITS_20
|AC_SUPPCM_BITS_24
))
1501 formats
|= SNDRV_PCM_FMTBIT_S32_LE
;
1502 if (val
& AC_SUPPCM_BITS_24
)
1504 else if (val
& AC_SUPPCM_BITS_20
)
1506 } else if (val
& (AC_SUPPCM_BITS_20
|AC_SUPPCM_BITS_24
|AC_SUPPCM_BITS_32
)) {
1507 formats
|= SNDRV_PCM_FMTBIT_S32_LE
;
1508 if (val
& AC_SUPPCM_BITS_32
)
1510 else if (val
& AC_SUPPCM_BITS_24
)
1512 else if (val
& AC_SUPPCM_BITS_20
)
1516 else if (streams
== AC_SUPFMT_FLOAT32
) { /* should be exclusive */
1517 formats
|= SNDRV_PCM_FMTBIT_FLOAT_LE
;
1519 } else if (streams
== AC_SUPFMT_AC3
) { /* should be exclusive */
1520 /* temporary hack: we have still no proper support
1521 * for the direct AC3 stream...
1523 formats
|= SNDRV_PCM_FMTBIT_U8
;
1527 *formatsp
= formats
;
1536 * snd_hda_is_supported_format - check whether the given node supports the format val
1538 * Returns 1 if supported, 0 if not.
1540 int snd_hda_is_supported_format(struct hda_codec
*codec
, hda_nid_t nid
,
1541 unsigned int format
)
1544 unsigned int val
= 0, rate
, stream
;
1546 if (nid
!= codec
->afg
&&
1547 (get_wcaps(codec
, nid
) & AC_WCAP_FORMAT_OVRD
)) {
1548 val
= snd_hda_param_read(codec
, nid
, AC_PAR_PCM
);
1553 val
= snd_hda_param_read(codec
, codec
->afg
, AC_PAR_PCM
);
1558 rate
= format
& 0xff00;
1559 for (i
= 0; rate_bits
[i
].hz
; i
++)
1560 if (rate_bits
[i
].hda_fmt
== rate
) {
1565 if (! rate_bits
[i
].hz
)
1568 stream
= snd_hda_param_read(codec
, nid
, AC_PAR_STREAM
);
1571 if (! stream
&& nid
!= codec
->afg
)
1572 stream
= snd_hda_param_read(codec
, codec
->afg
, AC_PAR_STREAM
);
1573 if (! stream
|| stream
== -1)
1576 if (stream
& AC_SUPFMT_PCM
) {
1577 switch (format
& 0xf0) {
1579 if (! (val
& AC_SUPPCM_BITS_8
))
1583 if (! (val
& AC_SUPPCM_BITS_16
))
1587 if (! (val
& AC_SUPPCM_BITS_20
))
1591 if (! (val
& AC_SUPPCM_BITS_24
))
1595 if (! (val
& AC_SUPPCM_BITS_32
))
1602 /* FIXME: check for float32 and AC3? */
1611 static int hda_pcm_default_open_close(struct hda_pcm_stream
*hinfo
,
1612 struct hda_codec
*codec
,
1613 struct snd_pcm_substream
*substream
)
1618 static int hda_pcm_default_prepare(struct hda_pcm_stream
*hinfo
,
1619 struct hda_codec
*codec
,
1620 unsigned int stream_tag
,
1621 unsigned int format
,
1622 struct snd_pcm_substream
*substream
)
1624 snd_hda_codec_setup_stream(codec
, hinfo
->nid
, stream_tag
, 0, format
);
1628 static int hda_pcm_default_cleanup(struct hda_pcm_stream
*hinfo
,
1629 struct hda_codec
*codec
,
1630 struct snd_pcm_substream
*substream
)
1632 snd_hda_codec_setup_stream(codec
, hinfo
->nid
, 0, 0, 0);
1636 static int set_pcm_default_values(struct hda_codec
*codec
, struct hda_pcm_stream
*info
)
1639 /* query support PCM information from the given NID */
1640 if (! info
->rates
|| ! info
->formats
)
1641 snd_hda_query_supported_pcm(codec
, info
->nid
,
1642 info
->rates
? NULL
: &info
->rates
,
1643 info
->formats
? NULL
: &info
->formats
,
1644 info
->maxbps
? NULL
: &info
->maxbps
);
1646 if (info
->ops
.open
== NULL
)
1647 info
->ops
.open
= hda_pcm_default_open_close
;
1648 if (info
->ops
.close
== NULL
)
1649 info
->ops
.close
= hda_pcm_default_open_close
;
1650 if (info
->ops
.prepare
== NULL
) {
1651 snd_assert(info
->nid
, return -EINVAL
);
1652 info
->ops
.prepare
= hda_pcm_default_prepare
;
1654 if (info
->ops
.cleanup
== NULL
) {
1655 snd_assert(info
->nid
, return -EINVAL
);
1656 info
->ops
.cleanup
= hda_pcm_default_cleanup
;
1662 * snd_hda_build_pcms - build PCM information
1665 * Create PCM information for each codec included in the bus.
1667 * The build_pcms codec patch is requested to set up codec->num_pcms and
1668 * codec->pcm_info properly. The array is referred by the top-level driver
1669 * to create its PCM instances.
1670 * The allocated codec->pcm_info should be released in codec->patch_ops.free
1673 * At least, substreams, channels_min and channels_max must be filled for
1674 * each stream. substreams = 0 indicates that the stream doesn't exist.
1675 * When rates and/or formats are zero, the supported values are queried
1676 * from the given nid. The nid is used also by the default ops.prepare
1677 * and ops.cleanup callbacks.
1679 * The driver needs to call ops.open in its open callback. Similarly,
1680 * ops.close is supposed to be called in the close callback.
1681 * ops.prepare should be called in the prepare or hw_params callback
1682 * with the proper parameters for set up.
1683 * ops.cleanup should be called in hw_free for clean up of streams.
1685 * This function returns 0 if successfull, or a negative error code.
1687 int snd_hda_build_pcms(struct hda_bus
*bus
)
1689 struct list_head
*p
;
1691 list_for_each(p
, &bus
->codec_list
) {
1692 struct hda_codec
*codec
= list_entry(p
, struct hda_codec
, list
);
1693 unsigned int pcm
, s
;
1695 if (! codec
->patch_ops
.build_pcms
)
1697 err
= codec
->patch_ops
.build_pcms(codec
);
1700 for (pcm
= 0; pcm
< codec
->num_pcms
; pcm
++) {
1701 for (s
= 0; s
< 2; s
++) {
1702 struct hda_pcm_stream
*info
;
1703 info
= &codec
->pcm_info
[pcm
].stream
[s
];
1704 if (! info
->substreams
)
1706 err
= set_pcm_default_values(codec
, info
);
1715 EXPORT_SYMBOL(snd_hda_build_pcms
);
1718 * snd_hda_check_board_config - compare the current codec with the config table
1719 * @codec: the HDA codec
1720 * @tbl: configuration table, terminated by null entries
1722 * Compares the modelname or PCI subsystem id of the current codec with the
1723 * given configuration table. If a matching entry is found, returns its
1724 * config value (supposed to be 0 or positive).
1726 * If no entries are matching, the function returns a negative value.
1728 int snd_hda_check_board_config(struct hda_codec
*codec
, const struct hda_board_config
*tbl
)
1730 const struct hda_board_config
*c
;
1732 if (codec
->bus
->modelname
) {
1733 for (c
= tbl
; c
->modelname
|| c
->pci_subvendor
; c
++) {
1735 ! strcmp(codec
->bus
->modelname
, c
->modelname
)) {
1736 snd_printd(KERN_INFO
"hda_codec: model '%s' is selected\n", c
->modelname
);
1742 if (codec
->bus
->pci
) {
1743 u16 subsystem_vendor
, subsystem_device
;
1744 pci_read_config_word(codec
->bus
->pci
, PCI_SUBSYSTEM_VENDOR_ID
, &subsystem_vendor
);
1745 pci_read_config_word(codec
->bus
->pci
, PCI_SUBSYSTEM_ID
, &subsystem_device
);
1746 for (c
= tbl
; c
->modelname
|| c
->pci_subvendor
; c
++) {
1747 if (c
->pci_subvendor
== subsystem_vendor
&&
1748 (! c
->pci_subdevice
/* all match */||
1749 (c
->pci_subdevice
== subsystem_device
))) {
1750 snd_printdd(KERN_INFO
"hda_codec: PCI %x:%x, codec config %d is selected\n",
1751 subsystem_vendor
, subsystem_device
, c
->config
);
1760 * snd_hda_add_new_ctls - create controls from the array
1761 * @codec: the HDA codec
1762 * @knew: the array of struct snd_kcontrol_new
1764 * This helper function creates and add new controls in the given array.
1765 * The array must be terminated with an empty entry as terminator.
1767 * Returns 0 if successful, or a negative error code.
1769 int snd_hda_add_new_ctls(struct hda_codec
*codec
, struct snd_kcontrol_new
*knew
)
1773 for (; knew
->name
; knew
++) {
1774 struct snd_kcontrol
*kctl
;
1775 kctl
= snd_ctl_new1(knew
, codec
);
1778 err
= snd_ctl_add(codec
->bus
->card
, kctl
);
1782 kctl
= snd_ctl_new1(knew
, codec
);
1785 kctl
->id
.device
= codec
->addr
;
1786 if ((err
= snd_ctl_add(codec
->bus
->card
, kctl
)) < 0)
1795 * Channel mode helper
1797 int snd_hda_ch_mode_info(struct hda_codec
*codec
, struct snd_ctl_elem_info
*uinfo
,
1798 const struct hda_channel_mode
*chmode
, int num_chmodes
)
1800 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_ENUMERATED
;
1802 uinfo
->value
.enumerated
.items
= num_chmodes
;
1803 if (uinfo
->value
.enumerated
.item
>= num_chmodes
)
1804 uinfo
->value
.enumerated
.item
= num_chmodes
- 1;
1805 sprintf(uinfo
->value
.enumerated
.name
, "%dch",
1806 chmode
[uinfo
->value
.enumerated
.item
].channels
);
1810 int snd_hda_ch_mode_get(struct hda_codec
*codec
, struct snd_ctl_elem_value
*ucontrol
,
1811 const struct hda_channel_mode
*chmode
, int num_chmodes
,
1816 for (i
= 0; i
< num_chmodes
; i
++) {
1817 if (max_channels
== chmode
[i
].channels
) {
1818 ucontrol
->value
.enumerated
.item
[0] = i
;
1825 int snd_hda_ch_mode_put(struct hda_codec
*codec
, struct snd_ctl_elem_value
*ucontrol
,
1826 const struct hda_channel_mode
*chmode
, int num_chmodes
,
1831 mode
= ucontrol
->value
.enumerated
.item
[0];
1832 snd_assert(mode
< num_chmodes
, return -EINVAL
);
1833 if (*max_channelsp
== chmode
[mode
].channels
&& ! codec
->in_resume
)
1835 /* change the current channel setting */
1836 *max_channelsp
= chmode
[mode
].channels
;
1837 if (chmode
[mode
].sequence
)
1838 snd_hda_sequence_write(codec
, chmode
[mode
].sequence
);
1845 int snd_hda_input_mux_info(const struct hda_input_mux
*imux
, struct snd_ctl_elem_info
*uinfo
)
1849 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_ENUMERATED
;
1851 uinfo
->value
.enumerated
.items
= imux
->num_items
;
1852 index
= uinfo
->value
.enumerated
.item
;
1853 if (index
>= imux
->num_items
)
1854 index
= imux
->num_items
- 1;
1855 strcpy(uinfo
->value
.enumerated
.name
, imux
->items
[index
].label
);
1859 int snd_hda_input_mux_put(struct hda_codec
*codec
, const struct hda_input_mux
*imux
,
1860 struct snd_ctl_elem_value
*ucontrol
, hda_nid_t nid
,
1861 unsigned int *cur_val
)
1865 idx
= ucontrol
->value
.enumerated
.item
[0];
1866 if (idx
>= imux
->num_items
)
1867 idx
= imux
->num_items
- 1;
1868 if (*cur_val
== idx
&& ! codec
->in_resume
)
1870 snd_hda_codec_write(codec
, nid
, 0, AC_VERB_SET_CONNECT_SEL
,
1871 imux
->items
[idx
].index
);
1878 * Multi-channel / digital-out PCM helper functions
1882 * open the digital out in the exclusive mode
1884 int snd_hda_multi_out_dig_open(struct hda_codec
*codec
, struct hda_multi_out
*mout
)
1886 mutex_lock(&codec
->spdif_mutex
);
1887 if (mout
->dig_out_used
) {
1888 mutex_unlock(&codec
->spdif_mutex
);
1889 return -EBUSY
; /* already being used */
1891 mout
->dig_out_used
= HDA_DIG_EXCLUSIVE
;
1892 mutex_unlock(&codec
->spdif_mutex
);
1897 * release the digital out
1899 int snd_hda_multi_out_dig_close(struct hda_codec
*codec
, struct hda_multi_out
*mout
)
1901 mutex_lock(&codec
->spdif_mutex
);
1902 mout
->dig_out_used
= 0;
1903 mutex_unlock(&codec
->spdif_mutex
);
1908 * set up more restrictions for analog out
1910 int snd_hda_multi_out_analog_open(struct hda_codec
*codec
, struct hda_multi_out
*mout
,
1911 struct snd_pcm_substream
*substream
)
1913 substream
->runtime
->hw
.channels_max
= mout
->max_channels
;
1914 return snd_pcm_hw_constraint_step(substream
->runtime
, 0,
1915 SNDRV_PCM_HW_PARAM_CHANNELS
, 2);
1919 * set up the i/o for analog out
1920 * when the digital out is available, copy the front out to digital out, too.
1922 int snd_hda_multi_out_analog_prepare(struct hda_codec
*codec
, struct hda_multi_out
*mout
,
1923 unsigned int stream_tag
,
1924 unsigned int format
,
1925 struct snd_pcm_substream
*substream
)
1927 hda_nid_t
*nids
= mout
->dac_nids
;
1928 int chs
= substream
->runtime
->channels
;
1931 mutex_lock(&codec
->spdif_mutex
);
1932 if (mout
->dig_out_nid
&& mout
->dig_out_used
!= HDA_DIG_EXCLUSIVE
) {
1934 snd_hda_is_supported_format(codec
, mout
->dig_out_nid
, format
) &&
1935 ! (codec
->spdif_status
& IEC958_AES0_NONAUDIO
)) {
1936 mout
->dig_out_used
= HDA_DIG_ANALOG_DUP
;
1937 /* setup digital receiver */
1938 snd_hda_codec_setup_stream(codec
, mout
->dig_out_nid
,
1939 stream_tag
, 0, format
);
1941 mout
->dig_out_used
= 0;
1942 snd_hda_codec_setup_stream(codec
, mout
->dig_out_nid
, 0, 0, 0);
1945 mutex_unlock(&codec
->spdif_mutex
);
1948 snd_hda_codec_setup_stream(codec
, nids
[HDA_FRONT
], stream_tag
, 0, format
);
1949 if (mout
->hp_nid
&& mout
->hp_nid
!= nids
[HDA_FRONT
])
1950 /* headphone out will just decode front left/right (stereo) */
1951 snd_hda_codec_setup_stream(codec
, mout
->hp_nid
, stream_tag
, 0, format
);
1952 /* extra outputs copied from front */
1953 for (i
= 0; i
< ARRAY_SIZE(mout
->extra_out_nid
); i
++)
1954 if (mout
->extra_out_nid
[i
])
1955 snd_hda_codec_setup_stream(codec
,
1956 mout
->extra_out_nid
[i
],
1957 stream_tag
, 0, format
);
1960 for (i
= 1; i
< mout
->num_dacs
; i
++) {
1961 if (chs
>= (i
+ 1) * 2) /* independent out */
1962 snd_hda_codec_setup_stream(codec
, nids
[i
], stream_tag
, i
* 2,
1964 else /* copy front */
1965 snd_hda_codec_setup_stream(codec
, nids
[i
], stream_tag
, 0,
1972 * clean up the setting for analog out
1974 int snd_hda_multi_out_analog_cleanup(struct hda_codec
*codec
, struct hda_multi_out
*mout
)
1976 hda_nid_t
*nids
= mout
->dac_nids
;
1979 for (i
= 0; i
< mout
->num_dacs
; i
++)
1980 snd_hda_codec_setup_stream(codec
, nids
[i
], 0, 0, 0);
1982 snd_hda_codec_setup_stream(codec
, mout
->hp_nid
, 0, 0, 0);
1983 for (i
= 0; i
< ARRAY_SIZE(mout
->extra_out_nid
); i
++)
1984 if (mout
->extra_out_nid
[i
])
1985 snd_hda_codec_setup_stream(codec
,
1986 mout
->extra_out_nid
[i
],
1988 mutex_lock(&codec
->spdif_mutex
);
1989 if (mout
->dig_out_nid
&& mout
->dig_out_used
== HDA_DIG_ANALOG_DUP
) {
1990 snd_hda_codec_setup_stream(codec
, mout
->dig_out_nid
, 0, 0, 0);
1991 mout
->dig_out_used
= 0;
1993 mutex_unlock(&codec
->spdif_mutex
);
1998 * Helper for automatic ping configuration
2001 static int is_in_nid_list(hda_nid_t nid
, hda_nid_t
*list
)
2003 for (; *list
; list
++)
2010 * Parse all pin widgets and store the useful pin nids to cfg
2012 * The number of line-outs or any primary output is stored in line_outs,
2013 * and the corresponding output pins are assigned to line_out_pins[],
2014 * in the order of front, rear, CLFE, side, ...
2016 * If more extra outputs (speaker and headphone) are found, the pins are
2017 * assisnged to hp_pins[] and speaker_pins[], respectively. If no line-out jack
2018 * is detected, one of speaker of HP pins is assigned as the primary
2019 * output, i.e. to line_out_pins[0]. So, line_outs is always positive
2020 * if any analog output exists.
2022 * The analog input pins are assigned to input_pins array.
2023 * The digital input/output pins are assigned to dig_in_pin and dig_out_pin,
2026 int snd_hda_parse_pin_def_config(struct hda_codec
*codec
, struct auto_pin_cfg
*cfg
,
2027 hda_nid_t
*ignore_nids
)
2029 hda_nid_t nid
, nid_start
;
2031 short seq
, assoc_line_out
, sequences
[ARRAY_SIZE(cfg
->line_out_pins
)];
2033 memset(cfg
, 0, sizeof(*cfg
));
2035 memset(sequences
, 0, sizeof(sequences
));
2038 nodes
= snd_hda_get_sub_nodes(codec
, codec
->afg
, &nid_start
);
2039 for (nid
= nid_start
; nid
< nodes
+ nid_start
; nid
++) {
2040 unsigned int wid_caps
= get_wcaps(codec
, nid
);
2041 unsigned int wid_type
= (wid_caps
& AC_WCAP_TYPE
) >> AC_WCAP_TYPE_SHIFT
;
2042 unsigned int def_conf
;
2045 /* read all default configuration for pin complex */
2046 if (wid_type
!= AC_WID_PIN
)
2048 /* ignore the given nids (e.g. pc-beep returns error) */
2049 if (ignore_nids
&& is_in_nid_list(nid
, ignore_nids
))
2052 def_conf
= snd_hda_codec_read(codec
, nid
, 0, AC_VERB_GET_CONFIG_DEFAULT
, 0);
2053 if (get_defcfg_connect(def_conf
) == AC_JACK_PORT_NONE
)
2055 loc
= get_defcfg_location(def_conf
);
2056 switch (get_defcfg_device(def_conf
)) {
2057 case AC_JACK_LINE_OUT
:
2058 seq
= get_defcfg_sequence(def_conf
);
2059 assoc
= get_defcfg_association(def_conf
);
2062 if (! assoc_line_out
)
2063 assoc_line_out
= assoc
;
2064 else if (assoc_line_out
!= assoc
)
2066 if (cfg
->line_outs
>= ARRAY_SIZE(cfg
->line_out_pins
))
2068 cfg
->line_out_pins
[cfg
->line_outs
] = nid
;
2069 sequences
[cfg
->line_outs
] = seq
;
2072 case AC_JACK_SPEAKER
:
2073 if (cfg
->speaker_outs
>= ARRAY_SIZE(cfg
->speaker_pins
))
2075 cfg
->speaker_pins
[cfg
->speaker_outs
] = nid
;
2076 cfg
->speaker_outs
++;
2078 case AC_JACK_HP_OUT
:
2079 if (cfg
->hp_outs
>= ARRAY_SIZE(cfg
->hp_pins
))
2081 cfg
->hp_pins
[cfg
->hp_outs
] = nid
;
2084 case AC_JACK_MIC_IN
: {
2086 if (loc
== AC_JACK_LOC_FRONT
) {
2087 preferred
= AUTO_PIN_FRONT_MIC
;
2090 preferred
= AUTO_PIN_MIC
;
2091 alt
= AUTO_PIN_FRONT_MIC
;
2093 if (!cfg
->input_pins
[preferred
])
2094 cfg
->input_pins
[preferred
] = nid
;
2095 else if (!cfg
->input_pins
[alt
])
2096 cfg
->input_pins
[alt
] = nid
;
2099 case AC_JACK_LINE_IN
:
2100 if (loc
== AC_JACK_LOC_FRONT
)
2101 cfg
->input_pins
[AUTO_PIN_FRONT_LINE
] = nid
;
2103 cfg
->input_pins
[AUTO_PIN_LINE
] = nid
;
2106 cfg
->input_pins
[AUTO_PIN_CD
] = nid
;
2109 cfg
->input_pins
[AUTO_PIN_AUX
] = nid
;
2111 case AC_JACK_SPDIF_OUT
:
2112 cfg
->dig_out_pin
= nid
;
2114 case AC_JACK_SPDIF_IN
:
2115 cfg
->dig_in_pin
= nid
;
2120 /* sort by sequence */
2121 for (i
= 0; i
< cfg
->line_outs
; i
++)
2122 for (j
= i
+ 1; j
< cfg
->line_outs
; j
++)
2123 if (sequences
[i
] > sequences
[j
]) {
2125 sequences
[i
] = sequences
[j
];
2127 nid
= cfg
->line_out_pins
[i
];
2128 cfg
->line_out_pins
[i
] = cfg
->line_out_pins
[j
];
2129 cfg
->line_out_pins
[j
] = nid
;
2132 /* Reorder the surround channels
2133 * ALSA sequence is front/surr/clfe/side
2135 * 4-ch: front/surr => OK as it is
2136 * 6-ch: front/clfe/surr
2137 * 8-ch: front/clfe/side/surr
2139 switch (cfg
->line_outs
) {
2141 nid
= cfg
->line_out_pins
[1];
2142 cfg
->line_out_pins
[1] = cfg
->line_out_pins
[2];
2143 cfg
->line_out_pins
[2] = nid
;
2146 nid
= cfg
->line_out_pins
[1];
2147 cfg
->line_out_pins
[1] = cfg
->line_out_pins
[3];
2148 cfg
->line_out_pins
[3] = cfg
->line_out_pins
[2];
2149 cfg
->line_out_pins
[2] = nid
;
2154 * debug prints of the parsed results
2156 snd_printd("autoconfig: line_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
2157 cfg
->line_outs
, cfg
->line_out_pins
[0], cfg
->line_out_pins
[1],
2158 cfg
->line_out_pins
[2], cfg
->line_out_pins
[3],
2159 cfg
->line_out_pins
[4]);
2160 snd_printd(" speaker_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
2161 cfg
->speaker_outs
, cfg
->speaker_pins
[0],
2162 cfg
->speaker_pins
[1], cfg
->speaker_pins
[2],
2163 cfg
->speaker_pins
[3], cfg
->speaker_pins
[4]);
2164 snd_printd(" hp_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
2165 cfg
->hp_outs
, cfg
->hp_pins
[0],
2166 cfg
->hp_pins
[1], cfg
->hp_pins
[2],
2167 cfg
->hp_pins
[3], cfg
->hp_pins
[4]);
2168 snd_printd(" inputs: mic=0x%x, fmic=0x%x, line=0x%x, fline=0x%x,"
2169 " cd=0x%x, aux=0x%x\n",
2170 cfg
->input_pins
[AUTO_PIN_MIC
],
2171 cfg
->input_pins
[AUTO_PIN_FRONT_MIC
],
2172 cfg
->input_pins
[AUTO_PIN_LINE
],
2173 cfg
->input_pins
[AUTO_PIN_FRONT_LINE
],
2174 cfg
->input_pins
[AUTO_PIN_CD
],
2175 cfg
->input_pins
[AUTO_PIN_AUX
]);
2178 * FIX-UP: if no line-outs are detected, try to use speaker or HP pin
2179 * as a primary output
2181 if (! cfg
->line_outs
) {
2182 if (cfg
->speaker_outs
) {
2183 cfg
->line_outs
= cfg
->speaker_outs
;
2184 memcpy(cfg
->line_out_pins
, cfg
->speaker_pins
,
2185 sizeof(cfg
->speaker_pins
));
2186 cfg
->speaker_outs
= 0;
2187 memset(cfg
->speaker_pins
, 0, sizeof(cfg
->speaker_pins
));
2188 } else if (cfg
->hp_outs
) {
2189 cfg
->line_outs
= cfg
->hp_outs
;
2190 memcpy(cfg
->line_out_pins
, cfg
->hp_pins
,
2191 sizeof(cfg
->hp_pins
));
2193 memset(cfg
->hp_pins
, 0, sizeof(cfg
->hp_pins
));
2200 /* labels for input pins */
2201 const char *auto_pin_cfg_labels
[AUTO_PIN_LAST
] = {
2202 "Mic", "Front Mic", "Line", "Front Line", "CD", "Aux"
2212 * snd_hda_suspend - suspend the codecs
2214 * @state: suspsend state
2216 * Returns 0 if successful.
2218 int snd_hda_suspend(struct hda_bus
*bus
, pm_message_t state
)
2220 struct list_head
*p
;
2222 /* FIXME: should handle power widget capabilities */
2223 list_for_each(p
, &bus
->codec_list
) {
2224 struct hda_codec
*codec
= list_entry(p
, struct hda_codec
, list
);
2225 if (codec
->patch_ops
.suspend
)
2226 codec
->patch_ops
.suspend(codec
, state
);
2227 hda_set_power_state(codec
,
2228 codec
->afg
? codec
->afg
: codec
->mfg
,
2234 EXPORT_SYMBOL(snd_hda_suspend
);
2237 * snd_hda_resume - resume the codecs
2239 * @state: resume state
2241 * Returns 0 if successful.
2243 int snd_hda_resume(struct hda_bus
*bus
)
2245 struct list_head
*p
;
2247 list_for_each(p
, &bus
->codec_list
) {
2248 struct hda_codec
*codec
= list_entry(p
, struct hda_codec
, list
);
2249 hda_set_power_state(codec
,
2250 codec
->afg
? codec
->afg
: codec
->mfg
,
2252 if (codec
->patch_ops
.resume
)
2253 codec
->patch_ops
.resume(codec
);
2258 EXPORT_SYMBOL(snd_hda_resume
);
2261 * snd_hda_resume_ctls - resume controls in the new control list
2262 * @codec: the HDA codec
2263 * @knew: the array of struct snd_kcontrol_new
2265 * This function resumes the mixer controls in the struct snd_kcontrol_new array,
2266 * originally for snd_hda_add_new_ctls().
2267 * The array must be terminated with an empty entry as terminator.
2269 int snd_hda_resume_ctls(struct hda_codec
*codec
, struct snd_kcontrol_new
*knew
)
2271 struct snd_ctl_elem_value
*val
;
2273 val
= kmalloc(sizeof(*val
), GFP_KERNEL
);
2276 codec
->in_resume
= 1;
2277 for (; knew
->name
; knew
++) {
2279 count
= knew
->count
? knew
->count
: 1;
2280 for (i
= 0; i
< count
; i
++) {
2281 memset(val
, 0, sizeof(*val
));
2282 val
->id
.iface
= knew
->iface
;
2283 val
->id
.device
= knew
->device
;
2284 val
->id
.subdevice
= knew
->subdevice
;
2285 strcpy(val
->id
.name
, knew
->name
);
2286 val
->id
.index
= knew
->index
? knew
->index
: i
;
2287 /* Assume that get callback reads only from cache,
2288 * not accessing to the real hardware
2290 if (snd_ctl_elem_read(codec
->bus
->card
, val
) < 0)
2292 snd_ctl_elem_write(codec
->bus
->card
, NULL
, val
);
2295 codec
->in_resume
= 0;
2301 * snd_hda_resume_spdif_out - resume the digital out
2302 * @codec: the HDA codec
2304 int snd_hda_resume_spdif_out(struct hda_codec
*codec
)
2306 return snd_hda_resume_ctls(codec
, dig_mixes
);
2310 * snd_hda_resume_spdif_in - resume the digital in
2311 * @codec: the HDA codec
2313 int snd_hda_resume_spdif_in(struct hda_codec
*codec
)
2315 return snd_hda_resume_ctls(codec
, dig_in_ctls
);
2323 static int __init
alsa_hda_init(void)
2328 static void __exit
alsa_hda_exit(void)
2332 module_init(alsa_hda_init
)
2333 module_exit(alsa_hda_exit
)