2 * Universal Interface for Intel High Definition Audio Codec
4 * Copyright (c) 2004 Takashi Iwai <tiwai@suse.de>
7 * This driver is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This driver is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include <linux/init.h>
23 #include <linux/delay.h>
24 #include <linux/slab.h>
25 #include <linux/pci.h>
26 #include <linux/mutex.h>
27 #include <sound/core.h>
28 #include "hda_codec.h"
29 #include <sound/asoundef.h>
30 #include <sound/tlv.h>
31 #include <sound/initval.h>
32 #include "hda_local.h"
33 #include <sound/hda_hwdep.h>
36 * vendor / preset table
39 struct hda_vendor_id
{
44 /* codec vendor labels */
45 static struct hda_vendor_id hda_vendor_ids
[] = {
47 { 0x1057, "Motorola" },
48 { 0x1095, "Silicon Image" },
50 { 0x10ec, "Realtek" },
54 { 0x11d4, "Analog Devices" },
55 { 0x13f6, "C-Media" },
56 { 0x14f1, "Conexant" },
57 { 0x17e8, "Chrontel" },
59 { 0x1aec, "Wolfson Microelectronics" },
60 { 0x434d, "C-Media" },
62 { 0x8384, "SigmaTel" },
66 static DEFINE_MUTEX(preset_mutex
);
67 static LIST_HEAD(hda_preset_tables
);
69 int snd_hda_add_codec_preset(struct hda_codec_preset_list
*preset
)
71 mutex_lock(&preset_mutex
);
72 list_add_tail(&preset
->list
, &hda_preset_tables
);
73 mutex_unlock(&preset_mutex
);
76 EXPORT_SYMBOL_HDA(snd_hda_add_codec_preset
);
78 int snd_hda_delete_codec_preset(struct hda_codec_preset_list
*preset
)
80 mutex_lock(&preset_mutex
);
81 list_del(&preset
->list
);
82 mutex_unlock(&preset_mutex
);
85 EXPORT_SYMBOL_HDA(snd_hda_delete_codec_preset
);
87 #ifdef CONFIG_SND_HDA_POWER_SAVE
88 static void hda_power_work(struct work_struct
*work
);
89 static void hda_keep_power_on(struct hda_codec
*codec
);
91 static inline void hda_keep_power_on(struct hda_codec
*codec
) {}
94 const char *snd_hda_get_jack_location(u32 cfg
)
96 static char *bases
[7] = {
97 "N/A", "Rear", "Front", "Left", "Right", "Top", "Bottom",
99 static unsigned char specials_idx
[] = {
104 static char *specials
[] = {
105 "Rear Panel", "Drive Bar",
106 "Riser", "HDMI", "ATAPI",
107 "Mobile-In", "Mobile-Out"
110 cfg
= (cfg
& AC_DEFCFG_LOCATION
) >> AC_DEFCFG_LOCATION_SHIFT
;
111 if ((cfg
& 0x0f) < 7)
112 return bases
[cfg
& 0x0f];
113 for (i
= 0; i
< ARRAY_SIZE(specials_idx
); i
++) {
114 if (cfg
== specials_idx
[i
])
119 EXPORT_SYMBOL_HDA(snd_hda_get_jack_location
);
121 const char *snd_hda_get_jack_connectivity(u32 cfg
)
123 static char *jack_locations
[4] = { "Ext", "Int", "Sep", "Oth" };
125 return jack_locations
[(cfg
>> (AC_DEFCFG_LOCATION_SHIFT
+ 4)) & 3];
127 EXPORT_SYMBOL_HDA(snd_hda_get_jack_connectivity
);
129 const char *snd_hda_get_jack_type(u32 cfg
)
131 static char *jack_types
[16] = {
132 "Line Out", "Speaker", "HP Out", "CD",
133 "SPDIF Out", "Digital Out", "Modem Line", "Modem Hand",
134 "Line In", "Aux", "Mic", "Telephony",
135 "SPDIF In", "Digitial In", "Reserved", "Other"
138 return jack_types
[(cfg
& AC_DEFCFG_DEVICE
)
139 >> AC_DEFCFG_DEVICE_SHIFT
];
141 EXPORT_SYMBOL_HDA(snd_hda_get_jack_type
);
144 * Compose a 32bit command word to be sent to the HD-audio controller
146 static inline unsigned int
147 make_codec_cmd(struct hda_codec
*codec
, hda_nid_t nid
, int direct
,
148 unsigned int verb
, unsigned int parm
)
152 val
= (u32
)(codec
->addr
& 0x0f) << 28;
153 val
|= (u32
)direct
<< 27;
154 val
|= (u32
)nid
<< 20;
161 * snd_hda_codec_read - send a command and get the response
162 * @codec: the HDA codec
163 * @nid: NID to send the command
164 * @direct: direct flag
165 * @verb: the verb to send
166 * @parm: the parameter for the verb
168 * Send a single command and read the corresponding response.
170 * Returns the obtained response value, or -1 for an error.
172 unsigned int snd_hda_codec_read(struct hda_codec
*codec
, hda_nid_t nid
,
174 unsigned int verb
, unsigned int parm
)
176 struct hda_bus
*bus
= codec
->bus
;
177 unsigned int cmd
, res
;
180 cmd
= make_codec_cmd(codec
, nid
, direct
, verb
, parm
);
181 snd_hda_power_up(codec
);
182 mutex_lock(&bus
->cmd_mutex
);
184 if (!bus
->ops
.command(bus
, cmd
)) {
185 res
= bus
->ops
.get_response(bus
);
186 if (res
== -1 && bus
->rirb_error
) {
187 if (repeated
++ < 1) {
188 snd_printd(KERN_WARNING
"hda_codec: "
189 "Trying verb 0x%08x again\n", cmd
);
194 res
= (unsigned int)-1;
195 mutex_unlock(&bus
->cmd_mutex
);
196 snd_hda_power_down(codec
);
199 EXPORT_SYMBOL_HDA(snd_hda_codec_read
);
202 * snd_hda_codec_write - send a single command without waiting for response
203 * @codec: the HDA codec
204 * @nid: NID to send the command
205 * @direct: direct flag
206 * @verb: the verb to send
207 * @parm: the parameter for the verb
209 * Send a single command without waiting for response.
211 * Returns 0 if successful, or a negative error code.
213 int snd_hda_codec_write(struct hda_codec
*codec
, hda_nid_t nid
, int direct
,
214 unsigned int verb
, unsigned int parm
)
216 struct hda_bus
*bus
= codec
->bus
;
220 res
= make_codec_cmd(codec
, nid
, direct
, verb
, parm
);
221 snd_hda_power_up(codec
);
222 mutex_lock(&bus
->cmd_mutex
);
223 err
= bus
->ops
.command(bus
, res
);
224 mutex_unlock(&bus
->cmd_mutex
);
225 snd_hda_power_down(codec
);
228 EXPORT_SYMBOL_HDA(snd_hda_codec_write
);
231 * snd_hda_sequence_write - sequence writes
232 * @codec: the HDA codec
233 * @seq: VERB array to send
235 * Send the commands sequentially from the given array.
236 * The array must be terminated with NID=0.
238 void snd_hda_sequence_write(struct hda_codec
*codec
, const struct hda_verb
*seq
)
240 for (; seq
->nid
; seq
++)
241 snd_hda_codec_write(codec
, seq
->nid
, 0, seq
->verb
, seq
->param
);
243 EXPORT_SYMBOL_HDA(snd_hda_sequence_write
);
246 * snd_hda_get_sub_nodes - get the range of sub nodes
247 * @codec: the HDA codec
249 * @start_id: the pointer to store the start NID
251 * Parse the NID and store the start NID of its sub-nodes.
252 * Returns the number of sub-nodes.
254 int snd_hda_get_sub_nodes(struct hda_codec
*codec
, hda_nid_t nid
,
259 parm
= snd_hda_param_read(codec
, nid
, AC_PAR_NODE_COUNT
);
262 *start_id
= (parm
>> 16) & 0x7fff;
263 return (int)(parm
& 0x7fff);
265 EXPORT_SYMBOL_HDA(snd_hda_get_sub_nodes
);
268 * snd_hda_get_connections - get connection list
269 * @codec: the HDA codec
271 * @conn_list: connection list array
272 * @max_conns: max. number of connections to store
274 * Parses the connection list of the given widget and stores the list
277 * Returns the number of connections, or a negative error code.
279 int snd_hda_get_connections(struct hda_codec
*codec
, hda_nid_t nid
,
280 hda_nid_t
*conn_list
, int max_conns
)
283 int i
, conn_len
, conns
;
284 unsigned int shift
, num_elems
, mask
;
287 if (snd_BUG_ON(!conn_list
|| max_conns
<= 0))
290 parm
= snd_hda_param_read(codec
, nid
, AC_PAR_CONNLIST_LEN
);
291 if (parm
& AC_CLIST_LONG
) {
300 conn_len
= parm
& AC_CLIST_LENGTH
;
301 mask
= (1 << (shift
-1)) - 1;
304 return 0; /* no connection */
307 /* single connection */
308 parm
= snd_hda_codec_read(codec
, nid
, 0,
309 AC_VERB_GET_CONNECT_LIST
, 0);
310 conn_list
[0] = parm
& mask
;
314 /* multi connection */
317 for (i
= 0; i
< conn_len
; i
++) {
321 if (i
% num_elems
== 0)
322 parm
= snd_hda_codec_read(codec
, nid
, 0,
323 AC_VERB_GET_CONNECT_LIST
, i
);
324 range_val
= !!(parm
& (1 << (shift
-1))); /* ranges */
328 /* ranges between the previous and this one */
329 if (!prev_nid
|| prev_nid
>= val
) {
330 snd_printk(KERN_WARNING
"hda_codec: "
331 "invalid dep_range_val %x:%x\n",
335 for (n
= prev_nid
+ 1; n
<= val
; n
++) {
336 if (conns
>= max_conns
) {
338 "Too many connections\n");
341 conn_list
[conns
++] = n
;
344 if (conns
>= max_conns
) {
345 snd_printk(KERN_ERR
"Too many connections\n");
348 conn_list
[conns
++] = val
;
354 EXPORT_SYMBOL_HDA(snd_hda_get_connections
);
358 * snd_hda_queue_unsol_event - add an unsolicited event to queue
360 * @res: unsolicited event (lower 32bit of RIRB entry)
361 * @res_ex: codec addr and flags (upper 32bit or RIRB entry)
363 * Adds the given event to the queue. The events are processed in
364 * the workqueue asynchronously. Call this function in the interrupt
365 * hanlder when RIRB receives an unsolicited event.
367 * Returns 0 if successful, or a negative error code.
369 int snd_hda_queue_unsol_event(struct hda_bus
*bus
, u32 res
, u32 res_ex
)
371 struct hda_bus_unsolicited
*unsol
;
378 wp
= (unsol
->wp
+ 1) % HDA_UNSOL_QUEUE_SIZE
;
382 unsol
->queue
[wp
] = res
;
383 unsol
->queue
[wp
+ 1] = res_ex
;
385 queue_work(bus
->workq
, &unsol
->work
);
389 EXPORT_SYMBOL_HDA(snd_hda_queue_unsol_event
);
392 * process queued unsolicited events
394 static void process_unsol_events(struct work_struct
*work
)
396 struct hda_bus_unsolicited
*unsol
=
397 container_of(work
, struct hda_bus_unsolicited
, work
);
398 struct hda_bus
*bus
= unsol
->bus
;
399 struct hda_codec
*codec
;
400 unsigned int rp
, caddr
, res
;
402 while (unsol
->rp
!= unsol
->wp
) {
403 rp
= (unsol
->rp
+ 1) % HDA_UNSOL_QUEUE_SIZE
;
406 res
= unsol
->queue
[rp
];
407 caddr
= unsol
->queue
[rp
+ 1];
408 if (!(caddr
& (1 << 4))) /* no unsolicited event? */
410 codec
= bus
->caddr_tbl
[caddr
& 0x0f];
411 if (codec
&& codec
->patch_ops
.unsol_event
)
412 codec
->patch_ops
.unsol_event(codec
, res
);
417 * initialize unsolicited queue
419 static int init_unsol_queue(struct hda_bus
*bus
)
421 struct hda_bus_unsolicited
*unsol
;
423 if (bus
->unsol
) /* already initialized */
426 unsol
= kzalloc(sizeof(*unsol
), GFP_KERNEL
);
428 snd_printk(KERN_ERR
"hda_codec: "
429 "can't allocate unsolicited queue\n");
432 INIT_WORK(&unsol
->work
, process_unsol_events
);
441 static void snd_hda_codec_free(struct hda_codec
*codec
);
443 static int snd_hda_bus_free(struct hda_bus
*bus
)
445 struct hda_codec
*codec
, *n
;
450 flush_workqueue(bus
->workq
);
453 list_for_each_entry_safe(codec
, n
, &bus
->codec_list
, list
) {
454 snd_hda_codec_free(codec
);
456 if (bus
->ops
.private_free
)
457 bus
->ops
.private_free(bus
);
459 destroy_workqueue(bus
->workq
);
464 static int snd_hda_bus_dev_free(struct snd_device
*device
)
466 struct hda_bus
*bus
= device
->device_data
;
468 return snd_hda_bus_free(bus
);
471 #ifdef CONFIG_SND_HDA_HWDEP
472 static int snd_hda_bus_dev_register(struct snd_device
*device
)
474 struct hda_bus
*bus
= device
->device_data
;
475 struct hda_codec
*codec
;
476 list_for_each_entry(codec
, &bus
->codec_list
, list
) {
477 snd_hda_hwdep_add_sysfs(codec
);
482 #define snd_hda_bus_dev_register NULL
486 * snd_hda_bus_new - create a HDA bus
487 * @card: the card entry
488 * @temp: the template for hda_bus information
489 * @busp: the pointer to store the created bus instance
491 * Returns 0 if successful, or a negative error code.
493 int /*__devinit*/ snd_hda_bus_new(struct snd_card
*card
,
494 const struct hda_bus_template
*temp
,
495 struct hda_bus
**busp
)
499 static struct snd_device_ops dev_ops
= {
500 .dev_register
= snd_hda_bus_dev_register
,
501 .dev_free
= snd_hda_bus_dev_free
,
504 if (snd_BUG_ON(!temp
))
506 if (snd_BUG_ON(!temp
->ops
.command
|| !temp
->ops
.get_response
))
512 bus
= kzalloc(sizeof(*bus
), GFP_KERNEL
);
514 snd_printk(KERN_ERR
"can't allocate struct hda_bus\n");
519 bus
->private_data
= temp
->private_data
;
520 bus
->pci
= temp
->pci
;
521 bus
->modelname
= temp
->modelname
;
522 bus
->power_save
= temp
->power_save
;
523 bus
->ops
= temp
->ops
;
525 mutex_init(&bus
->cmd_mutex
);
526 INIT_LIST_HEAD(&bus
->codec_list
);
528 snprintf(bus
->workq_name
, sizeof(bus
->workq_name
),
529 "hd-audio%d", card
->number
);
530 bus
->workq
= create_singlethread_workqueue(bus
->workq_name
);
532 snd_printk(KERN_ERR
"cannot create workqueue %s\n",
538 err
= snd_device_new(card
, SNDRV_DEV_BUS
, bus
, &dev_ops
);
540 snd_hda_bus_free(bus
);
547 EXPORT_SYMBOL_HDA(snd_hda_bus_new
);
549 #ifdef CONFIG_SND_HDA_GENERIC
550 #define is_generic_config(codec) \
551 (codec->modelname && !strcmp(codec->modelname, "generic"))
553 #define is_generic_config(codec) 0
557 #define HDA_MODREQ_MAX_COUNT 2 /* two request_modules()'s */
559 #define HDA_MODREQ_MAX_COUNT 0 /* all presets are statically linked */
563 * find a matching codec preset
565 static const struct hda_codec_preset
*
566 find_codec_preset(struct hda_codec
*codec
)
568 struct hda_codec_preset_list
*tbl
;
569 const struct hda_codec_preset
*preset
;
570 int mod_requested
= 0;
572 if (is_generic_config(codec
))
573 return NULL
; /* use the generic parser */
576 mutex_lock(&preset_mutex
);
577 list_for_each_entry(tbl
, &hda_preset_tables
, list
) {
578 if (!try_module_get(tbl
->owner
)) {
579 snd_printk(KERN_ERR
"hda_codec: cannot module_get\n");
582 for (preset
= tbl
->preset
; preset
->id
; preset
++) {
583 u32 mask
= preset
->mask
;
584 if (preset
->afg
&& preset
->afg
!= codec
->afg
)
586 if (preset
->mfg
&& preset
->mfg
!= codec
->mfg
)
590 if (preset
->id
== (codec
->vendor_id
& mask
) &&
592 preset
->rev
== codec
->revision_id
)) {
593 mutex_unlock(&preset_mutex
);
594 codec
->owner
= tbl
->owner
;
598 module_put(tbl
->owner
);
600 mutex_unlock(&preset_mutex
);
602 if (mod_requested
< HDA_MODREQ_MAX_COUNT
) {
605 snprintf(name
, sizeof(name
), "snd-hda-codec-id:%08x",
608 snprintf(name
, sizeof(name
), "snd-hda-codec-id:%04x*",
609 (codec
->vendor_id
>> 16) & 0xffff);
610 request_module(name
);
618 * get_codec_name - store the codec name
620 static int get_codec_name(struct hda_codec
*codec
)
622 const struct hda_vendor_id
*c
;
623 const char *vendor
= NULL
;
624 u16 vendor_id
= codec
->vendor_id
>> 16;
625 char tmp
[16], name
[32];
627 for (c
= hda_vendor_ids
; c
->id
; c
++) {
628 if (c
->id
== vendor_id
) {
634 sprintf(tmp
, "Generic %04x", vendor_id
);
637 if (codec
->preset
&& codec
->preset
->name
)
638 snprintf(name
, sizeof(name
), "%s %s", vendor
,
639 codec
->preset
->name
);
641 snprintf(name
, sizeof(name
), "%s ID %x", vendor
,
642 codec
->vendor_id
& 0xffff);
643 codec
->name
= kstrdup(name
, GFP_KERNEL
);
650 * look for an AFG and MFG nodes
652 static void /*__devinit*/ setup_fg_nodes(struct hda_codec
*codec
)
657 total_nodes
= snd_hda_get_sub_nodes(codec
, AC_NODE_ROOT
, &nid
);
658 for (i
= 0; i
< total_nodes
; i
++, nid
++) {
659 codec
->function_id
= snd_hda_param_read(codec
, nid
,
660 AC_PAR_FUNCTION_TYPE
) & 0xff;
661 switch (codec
->function_id
) {
662 case AC_GRP_AUDIO_FUNCTION
:
665 case AC_GRP_MODEM_FUNCTION
:
675 * read widget caps for each widget and store in cache
677 static int read_widget_caps(struct hda_codec
*codec
, hda_nid_t fg_node
)
682 codec
->num_nodes
= snd_hda_get_sub_nodes(codec
, fg_node
,
684 codec
->wcaps
= kmalloc(codec
->num_nodes
* 4, GFP_KERNEL
);
687 nid
= codec
->start_nid
;
688 for (i
= 0; i
< codec
->num_nodes
; i
++, nid
++)
689 codec
->wcaps
[i
] = snd_hda_param_read(codec
, nid
,
690 AC_PAR_AUDIO_WIDGET_CAP
);
694 /* read all pin default configurations and save codec->init_pins */
695 static int read_pin_defaults(struct hda_codec
*codec
)
698 hda_nid_t nid
= codec
->start_nid
;
700 for (i
= 0; i
< codec
->num_nodes
; i
++, nid
++) {
701 struct hda_pincfg
*pin
;
702 unsigned int wcaps
= get_wcaps(codec
, nid
);
703 unsigned int wid_type
= (wcaps
& AC_WCAP_TYPE
) >>
705 if (wid_type
!= AC_WID_PIN
)
707 pin
= snd_array_new(&codec
->init_pins
);
711 pin
->cfg
= snd_hda_codec_read(codec
, nid
, 0,
712 AC_VERB_GET_CONFIG_DEFAULT
, 0);
717 /* look up the given pin config list and return the item matching with NID */
718 static struct hda_pincfg
*look_up_pincfg(struct hda_codec
*codec
,
719 struct snd_array
*array
,
723 for (i
= 0; i
< array
->used
; i
++) {
724 struct hda_pincfg
*pin
= snd_array_elem(array
, i
);
731 /* write a config value for the given NID */
732 static void set_pincfg(struct hda_codec
*codec
, hda_nid_t nid
,
736 for (i
= 0; i
< 4; i
++) {
737 snd_hda_codec_write(codec
, nid
, 0,
738 AC_VERB_SET_CONFIG_DEFAULT_BYTES_0
+ i
,
744 /* set the current pin config value for the given NID.
745 * the value is cached, and read via snd_hda_codec_get_pincfg()
747 int snd_hda_add_pincfg(struct hda_codec
*codec
, struct snd_array
*list
,
748 hda_nid_t nid
, unsigned int cfg
)
750 struct hda_pincfg
*pin
;
753 oldcfg
= snd_hda_codec_get_pincfg(codec
, nid
);
754 pin
= look_up_pincfg(codec
, list
, nid
);
756 pin
= snd_array_new(list
);
763 /* change only when needed; e.g. if the pincfg is already present
764 * in user_pins[], don't write it
766 cfg
= snd_hda_codec_get_pincfg(codec
, nid
);
768 set_pincfg(codec
, nid
, cfg
);
772 int snd_hda_codec_set_pincfg(struct hda_codec
*codec
,
773 hda_nid_t nid
, unsigned int cfg
)
775 return snd_hda_add_pincfg(codec
, &codec
->driver_pins
, nid
, cfg
);
777 EXPORT_SYMBOL_HDA(snd_hda_codec_set_pincfg
);
779 /* get the current pin config value of the given pin NID */
780 unsigned int snd_hda_codec_get_pincfg(struct hda_codec
*codec
, hda_nid_t nid
)
782 struct hda_pincfg
*pin
;
784 #ifdef CONFIG_SND_HDA_HWDEP
785 pin
= look_up_pincfg(codec
, &codec
->user_pins
, nid
);
789 pin
= look_up_pincfg(codec
, &codec
->driver_pins
, nid
);
792 pin
= look_up_pincfg(codec
, &codec
->init_pins
, nid
);
797 EXPORT_SYMBOL_HDA(snd_hda_codec_get_pincfg
);
799 /* restore all current pin configs */
800 static void restore_pincfgs(struct hda_codec
*codec
)
803 for (i
= 0; i
< codec
->init_pins
.used
; i
++) {
804 struct hda_pincfg
*pin
= snd_array_elem(&codec
->init_pins
, i
);
805 set_pincfg(codec
, pin
->nid
,
806 snd_hda_codec_get_pincfg(codec
, pin
->nid
));
810 static void init_hda_cache(struct hda_cache_rec
*cache
,
811 unsigned int record_size
);
812 static void free_hda_cache(struct hda_cache_rec
*cache
);
814 /* restore the initial pin cfgs and release all pincfg lists */
815 static void restore_init_pincfgs(struct hda_codec
*codec
)
817 /* first free driver_pins and user_pins, then call restore_pincfg
818 * so that only the values in init_pins are restored
820 snd_array_free(&codec
->driver_pins
);
821 #ifdef CONFIG_SND_HDA_HWDEP
822 snd_array_free(&codec
->user_pins
);
824 restore_pincfgs(codec
);
825 snd_array_free(&codec
->init_pins
);
831 static void snd_hda_codec_free(struct hda_codec
*codec
)
835 restore_init_pincfgs(codec
);
836 #ifdef CONFIG_SND_HDA_POWER_SAVE
837 cancel_delayed_work(&codec
->power_work
);
838 flush_workqueue(codec
->bus
->workq
);
840 list_del(&codec
->list
);
841 snd_array_free(&codec
->mixers
);
842 codec
->bus
->caddr_tbl
[codec
->addr
] = NULL
;
843 if (codec
->patch_ops
.free
)
844 codec
->patch_ops
.free(codec
);
845 module_put(codec
->owner
);
846 free_hda_cache(&codec
->amp_cache
);
847 free_hda_cache(&codec
->cmd_cache
);
849 kfree(codec
->modelname
);
854 static void hda_set_power_state(struct hda_codec
*codec
, hda_nid_t fg
,
855 unsigned int power_state
);
858 * snd_hda_codec_new - create a HDA codec
859 * @bus: the bus to assign
860 * @codec_addr: the codec address
861 * @codecp: the pointer to store the generated codec
863 * Returns 0 if successful, or a negative error code.
865 int /*__devinit*/ snd_hda_codec_new(struct hda_bus
*bus
, unsigned int codec_addr
,
866 int do_init
, struct hda_codec
**codecp
)
868 struct hda_codec
*codec
;
872 if (snd_BUG_ON(!bus
))
874 if (snd_BUG_ON(codec_addr
> HDA_MAX_CODEC_ADDRESS
))
877 if (bus
->caddr_tbl
[codec_addr
]) {
878 snd_printk(KERN_ERR
"hda_codec: "
879 "address 0x%x is already occupied\n", codec_addr
);
883 codec
= kzalloc(sizeof(*codec
), GFP_KERNEL
);
885 snd_printk(KERN_ERR
"can't allocate struct hda_codec\n");
890 codec
->addr
= codec_addr
;
891 mutex_init(&codec
->spdif_mutex
);
892 mutex_init(&codec
->control_mutex
);
893 init_hda_cache(&codec
->amp_cache
, sizeof(struct hda_amp_info
));
894 init_hda_cache(&codec
->cmd_cache
, sizeof(struct hda_cache_head
));
895 snd_array_init(&codec
->mixers
, sizeof(struct snd_kcontrol
*), 32);
896 snd_array_init(&codec
->init_pins
, sizeof(struct hda_pincfg
), 16);
897 snd_array_init(&codec
->driver_pins
, sizeof(struct hda_pincfg
), 16);
898 if (codec
->bus
->modelname
) {
899 codec
->modelname
= kstrdup(codec
->bus
->modelname
, GFP_KERNEL
);
900 if (!codec
->modelname
) {
901 snd_hda_codec_free(codec
);
906 #ifdef CONFIG_SND_HDA_POWER_SAVE
907 INIT_DELAYED_WORK(&codec
->power_work
, hda_power_work
);
908 /* snd_hda_codec_new() marks the codec as power-up, and leave it as is.
909 * the caller has to power down appropriatley after initialization
912 hda_keep_power_on(codec
);
915 list_add_tail(&codec
->list
, &bus
->codec_list
);
916 bus
->caddr_tbl
[codec_addr
] = codec
;
918 codec
->vendor_id
= snd_hda_param_read(codec
, AC_NODE_ROOT
,
920 if (codec
->vendor_id
== -1)
921 /* read again, hopefully the access method was corrected
922 * in the last read...
924 codec
->vendor_id
= snd_hda_param_read(codec
, AC_NODE_ROOT
,
926 codec
->subsystem_id
= snd_hda_param_read(codec
, AC_NODE_ROOT
,
927 AC_PAR_SUBSYSTEM_ID
);
928 codec
->revision_id
= snd_hda_param_read(codec
, AC_NODE_ROOT
,
931 setup_fg_nodes(codec
);
932 if (!codec
->afg
&& !codec
->mfg
) {
933 snd_printdd("hda_codec: no AFG or MFG node found\n");
938 err
= read_widget_caps(codec
, codec
->afg
? codec
->afg
: codec
->mfg
);
940 snd_printk(KERN_ERR
"hda_codec: cannot malloc\n");
943 err
= read_pin_defaults(codec
);
947 if (!codec
->subsystem_id
) {
948 hda_nid_t nid
= codec
->afg
? codec
->afg
: codec
->mfg
;
949 codec
->subsystem_id
=
950 snd_hda_codec_read(codec
, nid
, 0,
951 AC_VERB_GET_SUBSYSTEM_ID
, 0);
954 codec
->modelname
= kstrdup(bus
->modelname
, GFP_KERNEL
);
956 /* power-up all before initialization */
957 hda_set_power_state(codec
,
958 codec
->afg
? codec
->afg
: codec
->mfg
,
962 err
= snd_hda_codec_configure(codec
);
966 snd_hda_codec_proc_new(codec
);
968 snd_hda_create_hwdep(codec
);
970 sprintf(component
, "HDA:%08x,%08x,%08x", codec
->vendor_id
,
971 codec
->subsystem_id
, codec
->revision_id
);
972 snd_component_add(codec
->bus
->card
, component
);
979 snd_hda_codec_free(codec
);
982 EXPORT_SYMBOL_HDA(snd_hda_codec_new
);
984 int snd_hda_codec_configure(struct hda_codec
*codec
)
988 codec
->preset
= find_codec_preset(codec
);
990 err
= get_codec_name(codec
);
994 /* audio codec should override the mixer name */
995 if (codec
->afg
|| !*codec
->bus
->card
->mixername
)
996 strlcpy(codec
->bus
->card
->mixername
, codec
->name
,
997 sizeof(codec
->bus
->card
->mixername
));
999 if (is_generic_config(codec
)) {
1000 err
= snd_hda_parse_generic_codec(codec
);
1003 if (codec
->preset
&& codec
->preset
->patch
) {
1004 err
= codec
->preset
->patch(codec
);
1008 /* call the default parser */
1009 err
= snd_hda_parse_generic_codec(codec
);
1011 printk(KERN_ERR
"hda-codec: No codec parser is available\n");
1014 if (!err
&& codec
->patch_ops
.unsol_event
)
1015 err
= init_unsol_queue(codec
->bus
);
1020 * snd_hda_codec_setup_stream - set up the codec for streaming
1021 * @codec: the CODEC to set up
1022 * @nid: the NID to set up
1023 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
1024 * @channel_id: channel id to pass, zero based.
1025 * @format: stream format.
1027 void snd_hda_codec_setup_stream(struct hda_codec
*codec
, hda_nid_t nid
,
1029 int channel_id
, int format
)
1034 snd_printdd("hda_codec_setup_stream: "
1035 "NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
1036 nid
, stream_tag
, channel_id
, format
);
1037 snd_hda_codec_write(codec
, nid
, 0, AC_VERB_SET_CHANNEL_STREAMID
,
1038 (stream_tag
<< 4) | channel_id
);
1040 snd_hda_codec_write(codec
, nid
, 0, AC_VERB_SET_STREAM_FORMAT
, format
);
1042 EXPORT_SYMBOL_HDA(snd_hda_codec_setup_stream
);
1044 void snd_hda_codec_cleanup_stream(struct hda_codec
*codec
, hda_nid_t nid
)
1049 snd_printdd("hda_codec_cleanup_stream: NID=0x%x\n", nid
);
1050 snd_hda_codec_write(codec
, nid
, 0, AC_VERB_SET_CHANNEL_STREAMID
, 0);
1051 #if 0 /* keep the format */
1053 snd_hda_codec_write(codec
, nid
, 0, AC_VERB_SET_STREAM_FORMAT
, 0);
1056 EXPORT_SYMBOL_HDA(snd_hda_codec_cleanup_stream
);
1059 * amp access functions
1062 /* FIXME: more better hash key? */
1063 #define HDA_HASH_KEY(nid,dir,idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
1064 #define HDA_HASH_PINCAP_KEY(nid) (u32)((nid) + (0x02 << 24))
1065 #define HDA_HASH_PARPCM_KEY(nid) (u32)((nid) + (0x03 << 24))
1066 #define HDA_HASH_PARSTR_KEY(nid) (u32)((nid) + (0x04 << 24))
1067 #define INFO_AMP_CAPS (1<<0)
1068 #define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
1070 /* initialize the hash table */
1071 static void /*__devinit*/ init_hda_cache(struct hda_cache_rec
*cache
,
1072 unsigned int record_size
)
1074 memset(cache
, 0, sizeof(*cache
));
1075 memset(cache
->hash
, 0xff, sizeof(cache
->hash
));
1076 snd_array_init(&cache
->buf
, record_size
, 64);
1079 static void free_hda_cache(struct hda_cache_rec
*cache
)
1081 snd_array_free(&cache
->buf
);
1084 /* query the hash. allocate an entry if not found. */
1085 static struct hda_cache_head
*get_alloc_hash(struct hda_cache_rec
*cache
,
1088 u16 idx
= key
% (u16
)ARRAY_SIZE(cache
->hash
);
1089 u16 cur
= cache
->hash
[idx
];
1090 struct hda_cache_head
*info
;
1092 while (cur
!= 0xffff) {
1093 info
= snd_array_elem(&cache
->buf
, cur
);
1094 if (info
->key
== key
)
1099 /* add a new hash entry */
1100 info
= snd_array_new(&cache
->buf
);
1103 cur
= snd_array_index(&cache
->buf
, info
);
1106 info
->next
= cache
->hash
[idx
];
1107 cache
->hash
[idx
] = cur
;
1112 /* query and allocate an amp hash entry */
1113 static inline struct hda_amp_info
*
1114 get_alloc_amp_hash(struct hda_codec
*codec
, u32 key
)
1116 return (struct hda_amp_info
*)get_alloc_hash(&codec
->amp_cache
, key
);
1120 * query AMP capabilities for the given widget and direction
1122 u32
query_amp_caps(struct hda_codec
*codec
, hda_nid_t nid
, int direction
)
1124 struct hda_amp_info
*info
;
1126 info
= get_alloc_amp_hash(codec
, HDA_HASH_KEY(nid
, direction
, 0));
1129 if (!(info
->head
.val
& INFO_AMP_CAPS
)) {
1130 if (!(get_wcaps(codec
, nid
) & AC_WCAP_AMP_OVRD
))
1132 info
->amp_caps
= snd_hda_param_read(codec
, nid
,
1133 direction
== HDA_OUTPUT
?
1134 AC_PAR_AMP_OUT_CAP
:
1137 info
->head
.val
|= INFO_AMP_CAPS
;
1139 return info
->amp_caps
;
1141 EXPORT_SYMBOL_HDA(query_amp_caps
);
1143 int snd_hda_override_amp_caps(struct hda_codec
*codec
, hda_nid_t nid
, int dir
,
1146 struct hda_amp_info
*info
;
1148 info
= get_alloc_amp_hash(codec
, HDA_HASH_KEY(nid
, dir
, 0));
1151 info
->amp_caps
= caps
;
1152 info
->head
.val
|= INFO_AMP_CAPS
;
1155 EXPORT_SYMBOL_HDA(snd_hda_override_amp_caps
);
1158 query_caps_hash(struct hda_codec
*codec
, hda_nid_t nid
, u32 key
,
1159 unsigned int (*func
)(struct hda_codec
*, hda_nid_t
))
1161 struct hda_amp_info
*info
;
1163 info
= get_alloc_amp_hash(codec
, key
);
1166 if (!info
->head
.val
) {
1167 info
->head
.val
|= INFO_AMP_CAPS
;
1168 info
->amp_caps
= func(codec
, nid
);
1170 return info
->amp_caps
;
1173 static unsigned int read_pin_cap(struct hda_codec
*codec
, hda_nid_t nid
)
1175 return snd_hda_param_read(codec
, nid
, AC_PAR_PIN_CAP
);
1178 u32
snd_hda_query_pin_caps(struct hda_codec
*codec
, hda_nid_t nid
)
1180 return query_caps_hash(codec
, nid
, HDA_HASH_PINCAP_KEY(nid
),
1183 EXPORT_SYMBOL_HDA(snd_hda_query_pin_caps
);
1186 * read the current volume to info
1187 * if the cache exists, read the cache value.
1189 static unsigned int get_vol_mute(struct hda_codec
*codec
,
1190 struct hda_amp_info
*info
, hda_nid_t nid
,
1191 int ch
, int direction
, int index
)
1195 if (info
->head
.val
& INFO_AMP_VOL(ch
))
1196 return info
->vol
[ch
];
1198 parm
= ch
? AC_AMP_GET_RIGHT
: AC_AMP_GET_LEFT
;
1199 parm
|= direction
== HDA_OUTPUT
? AC_AMP_GET_OUTPUT
: AC_AMP_GET_INPUT
;
1201 val
= snd_hda_codec_read(codec
, nid
, 0,
1202 AC_VERB_GET_AMP_GAIN_MUTE
, parm
);
1203 info
->vol
[ch
] = val
& 0xff;
1204 info
->head
.val
|= INFO_AMP_VOL(ch
);
1205 return info
->vol
[ch
];
1209 * write the current volume in info to the h/w and update the cache
1211 static void put_vol_mute(struct hda_codec
*codec
, struct hda_amp_info
*info
,
1212 hda_nid_t nid
, int ch
, int direction
, int index
,
1217 parm
= ch
? AC_AMP_SET_RIGHT
: AC_AMP_SET_LEFT
;
1218 parm
|= direction
== HDA_OUTPUT
? AC_AMP_SET_OUTPUT
: AC_AMP_SET_INPUT
;
1219 parm
|= index
<< AC_AMP_SET_INDEX_SHIFT
;
1221 snd_hda_codec_write(codec
, nid
, 0, AC_VERB_SET_AMP_GAIN_MUTE
, parm
);
1222 info
->vol
[ch
] = val
;
1226 * read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
1228 int snd_hda_codec_amp_read(struct hda_codec
*codec
, hda_nid_t nid
, int ch
,
1229 int direction
, int index
)
1231 struct hda_amp_info
*info
;
1232 info
= get_alloc_amp_hash(codec
, HDA_HASH_KEY(nid
, direction
, index
));
1235 return get_vol_mute(codec
, info
, nid
, ch
, direction
, index
);
1237 EXPORT_SYMBOL_HDA(snd_hda_codec_amp_read
);
1240 * update the AMP value, mask = bit mask to set, val = the value
1242 int snd_hda_codec_amp_update(struct hda_codec
*codec
, hda_nid_t nid
, int ch
,
1243 int direction
, int idx
, int mask
, int val
)
1245 struct hda_amp_info
*info
;
1247 info
= get_alloc_amp_hash(codec
, HDA_HASH_KEY(nid
, direction
, idx
));
1251 val
|= get_vol_mute(codec
, info
, nid
, ch
, direction
, idx
) & ~mask
;
1252 if (info
->vol
[ch
] == val
)
1254 put_vol_mute(codec
, info
, nid
, ch
, direction
, idx
, val
);
1257 EXPORT_SYMBOL_HDA(snd_hda_codec_amp_update
);
1260 * update the AMP stereo with the same mask and value
1262 int snd_hda_codec_amp_stereo(struct hda_codec
*codec
, hda_nid_t nid
,
1263 int direction
, int idx
, int mask
, int val
)
1266 for (ch
= 0; ch
< 2; ch
++)
1267 ret
|= snd_hda_codec_amp_update(codec
, nid
, ch
, direction
,
1271 EXPORT_SYMBOL_HDA(snd_hda_codec_amp_stereo
);
1273 #ifdef SND_HDA_NEEDS_RESUME
1274 /* resume the all amp commands from the cache */
1275 void snd_hda_codec_resume_amp(struct hda_codec
*codec
)
1277 struct hda_amp_info
*buffer
= codec
->amp_cache
.buf
.list
;
1280 for (i
= 0; i
< codec
->amp_cache
.buf
.used
; i
++, buffer
++) {
1281 u32 key
= buffer
->head
.key
;
1283 unsigned int idx
, dir
, ch
;
1287 idx
= (key
>> 16) & 0xff;
1288 dir
= (key
>> 24) & 0xff;
1289 for (ch
= 0; ch
< 2; ch
++) {
1290 if (!(buffer
->head
.val
& INFO_AMP_VOL(ch
)))
1292 put_vol_mute(codec
, buffer
, nid
, ch
, dir
, idx
,
1297 EXPORT_SYMBOL_HDA(snd_hda_codec_resume_amp
);
1298 #endif /* SND_HDA_NEEDS_RESUME */
1301 int snd_hda_mixer_amp_volume_info(struct snd_kcontrol
*kcontrol
,
1302 struct snd_ctl_elem_info
*uinfo
)
1304 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
1305 u16 nid
= get_amp_nid(kcontrol
);
1306 u8 chs
= get_amp_channels(kcontrol
);
1307 int dir
= get_amp_direction(kcontrol
);
1308 unsigned int ofs
= get_amp_offset(kcontrol
);
1311 caps
= query_amp_caps(codec
, nid
, dir
);
1313 caps
= (caps
& AC_AMPCAP_NUM_STEPS
) >> AC_AMPCAP_NUM_STEPS_SHIFT
;
1315 printk(KERN_WARNING
"hda_codec: "
1316 "num_steps = 0 for NID=0x%x (ctl = %s)\n", nid
,
1322 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
1323 uinfo
->count
= chs
== 3 ? 2 : 1;
1324 uinfo
->value
.integer
.min
= 0;
1325 uinfo
->value
.integer
.max
= caps
;
1328 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_info
);
1331 static inline unsigned int
1332 read_amp_value(struct hda_codec
*codec
, hda_nid_t nid
,
1333 int ch
, int dir
, int idx
, unsigned int ofs
)
1336 val
= snd_hda_codec_amp_read(codec
, nid
, ch
, dir
, idx
);
1337 val
&= HDA_AMP_VOLMASK
;
1346 update_amp_value(struct hda_codec
*codec
, hda_nid_t nid
,
1347 int ch
, int dir
, int idx
, unsigned int ofs
,
1352 return snd_hda_codec_amp_update(codec
, nid
, ch
, dir
, idx
,
1353 HDA_AMP_VOLMASK
, val
);
1356 int snd_hda_mixer_amp_volume_get(struct snd_kcontrol
*kcontrol
,
1357 struct snd_ctl_elem_value
*ucontrol
)
1359 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
1360 hda_nid_t nid
= get_amp_nid(kcontrol
);
1361 int chs
= get_amp_channels(kcontrol
);
1362 int dir
= get_amp_direction(kcontrol
);
1363 int idx
= get_amp_index(kcontrol
);
1364 unsigned int ofs
= get_amp_offset(kcontrol
);
1365 long *valp
= ucontrol
->value
.integer
.value
;
1368 *valp
++ = read_amp_value(codec
, nid
, 0, dir
, idx
, ofs
);
1370 *valp
= read_amp_value(codec
, nid
, 1, dir
, idx
, ofs
);
1373 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_get
);
1375 int snd_hda_mixer_amp_volume_put(struct snd_kcontrol
*kcontrol
,
1376 struct snd_ctl_elem_value
*ucontrol
)
1378 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
1379 hda_nid_t nid
= get_amp_nid(kcontrol
);
1380 int chs
= get_amp_channels(kcontrol
);
1381 int dir
= get_amp_direction(kcontrol
);
1382 int idx
= get_amp_index(kcontrol
);
1383 unsigned int ofs
= get_amp_offset(kcontrol
);
1384 long *valp
= ucontrol
->value
.integer
.value
;
1387 snd_hda_power_up(codec
);
1389 change
= update_amp_value(codec
, nid
, 0, dir
, idx
, ofs
, *valp
);
1393 change
|= update_amp_value(codec
, nid
, 1, dir
, idx
, ofs
, *valp
);
1394 snd_hda_power_down(codec
);
1397 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_put
);
1399 int snd_hda_mixer_amp_tlv(struct snd_kcontrol
*kcontrol
, int op_flag
,
1400 unsigned int size
, unsigned int __user
*_tlv
)
1402 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
1403 hda_nid_t nid
= get_amp_nid(kcontrol
);
1404 int dir
= get_amp_direction(kcontrol
);
1405 unsigned int ofs
= get_amp_offset(kcontrol
);
1406 u32 caps
, val1
, val2
;
1408 if (size
< 4 * sizeof(unsigned int))
1410 caps
= query_amp_caps(codec
, nid
, dir
);
1411 val2
= (caps
& AC_AMPCAP_STEP_SIZE
) >> AC_AMPCAP_STEP_SIZE_SHIFT
;
1412 val2
= (val2
+ 1) * 25;
1413 val1
= -((caps
& AC_AMPCAP_OFFSET
) >> AC_AMPCAP_OFFSET_SHIFT
);
1415 val1
= ((int)val1
) * ((int)val2
);
1416 if (put_user(SNDRV_CTL_TLVT_DB_SCALE
, _tlv
))
1418 if (put_user(2 * sizeof(unsigned int), _tlv
+ 1))
1420 if (put_user(val1
, _tlv
+ 2))
1422 if (put_user(val2
, _tlv
+ 3))
1426 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_tlv
);
1429 * set (static) TLV for virtual master volume; recalculated as max 0dB
1431 void snd_hda_set_vmaster_tlv(struct hda_codec
*codec
, hda_nid_t nid
, int dir
,
1437 caps
= query_amp_caps(codec
, nid
, dir
);
1438 nums
= (caps
& AC_AMPCAP_NUM_STEPS
) >> AC_AMPCAP_NUM_STEPS_SHIFT
;
1439 step
= (caps
& AC_AMPCAP_STEP_SIZE
) >> AC_AMPCAP_STEP_SIZE_SHIFT
;
1440 step
= (step
+ 1) * 25;
1441 tlv
[0] = SNDRV_CTL_TLVT_DB_SCALE
;
1442 tlv
[1] = 2 * sizeof(unsigned int);
1443 tlv
[2] = -nums
* step
;
1446 EXPORT_SYMBOL_HDA(snd_hda_set_vmaster_tlv
);
1448 /* find a mixer control element with the given name */
1449 static struct snd_kcontrol
*
1450 _snd_hda_find_mixer_ctl(struct hda_codec
*codec
,
1451 const char *name
, int idx
)
1453 struct snd_ctl_elem_id id
;
1454 memset(&id
, 0, sizeof(id
));
1455 id
.iface
= SNDRV_CTL_ELEM_IFACE_MIXER
;
1457 strcpy(id
.name
, name
);
1458 return snd_ctl_find_id(codec
->bus
->card
, &id
);
1461 struct snd_kcontrol
*snd_hda_find_mixer_ctl(struct hda_codec
*codec
,
1464 return _snd_hda_find_mixer_ctl(codec
, name
, 0);
1466 EXPORT_SYMBOL_HDA(snd_hda_find_mixer_ctl
);
1468 /* Add a control element and assign to the codec */
1469 int snd_hda_ctl_add(struct hda_codec
*codec
, struct snd_kcontrol
*kctl
)
1472 struct snd_kcontrol
**knewp
;
1474 err
= snd_ctl_add(codec
->bus
->card
, kctl
);
1477 knewp
= snd_array_new(&codec
->mixers
);
1483 EXPORT_SYMBOL_HDA(snd_hda_ctl_add
);
1485 /* Clear all controls assigned to the given codec */
1486 void snd_hda_ctls_clear(struct hda_codec
*codec
)
1489 struct snd_kcontrol
**kctls
= codec
->mixers
.list
;
1490 for (i
= 0; i
< codec
->mixers
.used
; i
++)
1491 snd_ctl_remove(codec
->bus
->card
, kctls
[i
]);
1492 snd_array_free(&codec
->mixers
);
1495 /* pseudo device locking
1496 * toggle card->shutdown to allow/disallow the device access (as a hack)
1498 static int hda_lock_devices(struct snd_card
*card
)
1500 spin_lock(&card
->files_lock
);
1501 if (card
->shutdown
) {
1502 spin_unlock(&card
->files_lock
);
1506 spin_unlock(&card
->files_lock
);
1510 static void hda_unlock_devices(struct snd_card
*card
)
1512 spin_lock(&card
->files_lock
);
1514 spin_unlock(&card
->files_lock
);
1517 int snd_hda_codec_reset(struct hda_codec
*codec
)
1519 struct snd_card
*card
= codec
->bus
->card
;
1522 if (hda_lock_devices(card
) < 0)
1524 /* check whether the codec isn't used by any mixer or PCM streams */
1525 if (!list_empty(&card
->ctl_files
)) {
1526 hda_unlock_devices(card
);
1529 for (pcm
= 0; pcm
< codec
->num_pcms
; pcm
++) {
1530 struct hda_pcm
*cpcm
= &codec
->pcm_info
[pcm
];
1533 if (cpcm
->pcm
->streams
[0].substream_opened
||
1534 cpcm
->pcm
->streams
[1].substream_opened
) {
1535 hda_unlock_devices(card
);
1540 /* OK, let it free */
1542 #ifdef CONFIG_SND_HDA_POWER_SAVE
1543 cancel_delayed_work(&codec
->power_work
);
1544 flush_workqueue(codec
->bus
->workq
);
1546 snd_hda_ctls_clear(codec
);
1548 for (i
= 0; i
< codec
->num_pcms
; i
++) {
1549 if (codec
->pcm_info
[i
].pcm
) {
1550 snd_device_free(card
, codec
->pcm_info
[i
].pcm
);
1551 clear_bit(codec
->pcm_info
[i
].device
,
1552 codec
->bus
->pcm_dev_bits
);
1555 if (codec
->patch_ops
.free
)
1556 codec
->patch_ops
.free(codec
);
1557 codec
->proc_widget_hook
= NULL
;
1559 free_hda_cache(&codec
->amp_cache
);
1560 free_hda_cache(&codec
->cmd_cache
);
1561 init_hda_cache(&codec
->amp_cache
, sizeof(struct hda_amp_info
));
1562 init_hda_cache(&codec
->cmd_cache
, sizeof(struct hda_cache_head
));
1563 /* free only driver_pins so that init_pins + user_pins are restored */
1564 snd_array_free(&codec
->driver_pins
);
1565 restore_pincfgs(codec
);
1566 codec
->num_pcms
= 0;
1567 codec
->pcm_info
= NULL
;
1568 codec
->preset
= NULL
;
1569 memset(&codec
->patch_ops
, 0, sizeof(codec
->patch_ops
));
1570 codec
->slave_dig_outs
= NULL
;
1571 codec
->spdif_status_reset
= 0;
1572 module_put(codec
->owner
);
1573 codec
->owner
= NULL
;
1575 /* allow device access again */
1576 hda_unlock_devices(card
);
1580 /* create a virtual master control and add slaves */
1581 int snd_hda_add_vmaster(struct hda_codec
*codec
, char *name
,
1582 unsigned int *tlv
, const char **slaves
)
1584 struct snd_kcontrol
*kctl
;
1588 for (s
= slaves
; *s
&& !snd_hda_find_mixer_ctl(codec
, *s
); s
++)
1591 snd_printdd("No slave found for %s\n", name
);
1594 kctl
= snd_ctl_make_virtual_master(name
, tlv
);
1597 err
= snd_hda_ctl_add(codec
, kctl
);
1601 for (s
= slaves
; *s
; s
++) {
1602 struct snd_kcontrol
*sctl
;
1605 sctl
= _snd_hda_find_mixer_ctl(codec
, *s
, i
);
1608 snd_printdd("Cannot find slave %s, "
1612 err
= snd_ctl_add_slave(kctl
, sctl
);
1620 EXPORT_SYMBOL_HDA(snd_hda_add_vmaster
);
1623 int snd_hda_mixer_amp_switch_info(struct snd_kcontrol
*kcontrol
,
1624 struct snd_ctl_elem_info
*uinfo
)
1626 int chs
= get_amp_channels(kcontrol
);
1628 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_BOOLEAN
;
1629 uinfo
->count
= chs
== 3 ? 2 : 1;
1630 uinfo
->value
.integer
.min
= 0;
1631 uinfo
->value
.integer
.max
= 1;
1634 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_info
);
1636 int snd_hda_mixer_amp_switch_get(struct snd_kcontrol
*kcontrol
,
1637 struct snd_ctl_elem_value
*ucontrol
)
1639 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
1640 hda_nid_t nid
= get_amp_nid(kcontrol
);
1641 int chs
= get_amp_channels(kcontrol
);
1642 int dir
= get_amp_direction(kcontrol
);
1643 int idx
= get_amp_index(kcontrol
);
1644 long *valp
= ucontrol
->value
.integer
.value
;
1647 *valp
++ = (snd_hda_codec_amp_read(codec
, nid
, 0, dir
, idx
) &
1648 HDA_AMP_MUTE
) ? 0 : 1;
1650 *valp
= (snd_hda_codec_amp_read(codec
, nid
, 1, dir
, idx
) &
1651 HDA_AMP_MUTE
) ? 0 : 1;
1654 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_get
);
1656 int snd_hda_mixer_amp_switch_put(struct snd_kcontrol
*kcontrol
,
1657 struct snd_ctl_elem_value
*ucontrol
)
1659 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
1660 hda_nid_t nid
= get_amp_nid(kcontrol
);
1661 int chs
= get_amp_channels(kcontrol
);
1662 int dir
= get_amp_direction(kcontrol
);
1663 int idx
= get_amp_index(kcontrol
);
1664 long *valp
= ucontrol
->value
.integer
.value
;
1667 snd_hda_power_up(codec
);
1669 change
= snd_hda_codec_amp_update(codec
, nid
, 0, dir
, idx
,
1671 *valp
? 0 : HDA_AMP_MUTE
);
1675 change
|= snd_hda_codec_amp_update(codec
, nid
, 1, dir
, idx
,
1677 *valp
? 0 : HDA_AMP_MUTE
);
1678 #ifdef CONFIG_SND_HDA_POWER_SAVE
1679 if (codec
->patch_ops
.check_power_status
)
1680 codec
->patch_ops
.check_power_status(codec
, nid
);
1682 snd_hda_power_down(codec
);
1685 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put
);
1688 * bound volume controls
1690 * bind multiple volumes (# indices, from 0)
1693 #define AMP_VAL_IDX_SHIFT 19
1694 #define AMP_VAL_IDX_MASK (0x0f<<19)
1696 int snd_hda_mixer_bind_switch_get(struct snd_kcontrol
*kcontrol
,
1697 struct snd_ctl_elem_value
*ucontrol
)
1699 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
1703 mutex_lock(&codec
->control_mutex
);
1704 pval
= kcontrol
->private_value
;
1705 kcontrol
->private_value
= pval
& ~AMP_VAL_IDX_MASK
; /* index 0 */
1706 err
= snd_hda_mixer_amp_switch_get(kcontrol
, ucontrol
);
1707 kcontrol
->private_value
= pval
;
1708 mutex_unlock(&codec
->control_mutex
);
1711 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_get
);
1713 int snd_hda_mixer_bind_switch_put(struct snd_kcontrol
*kcontrol
,
1714 struct snd_ctl_elem_value
*ucontrol
)
1716 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
1718 int i
, indices
, err
= 0, change
= 0;
1720 mutex_lock(&codec
->control_mutex
);
1721 pval
= kcontrol
->private_value
;
1722 indices
= (pval
& AMP_VAL_IDX_MASK
) >> AMP_VAL_IDX_SHIFT
;
1723 for (i
= 0; i
< indices
; i
++) {
1724 kcontrol
->private_value
= (pval
& ~AMP_VAL_IDX_MASK
) |
1725 (i
<< AMP_VAL_IDX_SHIFT
);
1726 err
= snd_hda_mixer_amp_switch_put(kcontrol
, ucontrol
);
1731 kcontrol
->private_value
= pval
;
1732 mutex_unlock(&codec
->control_mutex
);
1733 return err
< 0 ? err
: change
;
1735 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_put
);
1738 * generic bound volume/swtich controls
1740 int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol
*kcontrol
,
1741 struct snd_ctl_elem_info
*uinfo
)
1743 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
1744 struct hda_bind_ctls
*c
;
1747 mutex_lock(&codec
->control_mutex
);
1748 c
= (struct hda_bind_ctls
*)kcontrol
->private_value
;
1749 kcontrol
->private_value
= *c
->values
;
1750 err
= c
->ops
->info(kcontrol
, uinfo
);
1751 kcontrol
->private_value
= (long)c
;
1752 mutex_unlock(&codec
->control_mutex
);
1755 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_info
);
1757 int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol
*kcontrol
,
1758 struct snd_ctl_elem_value
*ucontrol
)
1760 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
1761 struct hda_bind_ctls
*c
;
1764 mutex_lock(&codec
->control_mutex
);
1765 c
= (struct hda_bind_ctls
*)kcontrol
->private_value
;
1766 kcontrol
->private_value
= *c
->values
;
1767 err
= c
->ops
->get(kcontrol
, ucontrol
);
1768 kcontrol
->private_value
= (long)c
;
1769 mutex_unlock(&codec
->control_mutex
);
1772 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_get
);
1774 int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol
*kcontrol
,
1775 struct snd_ctl_elem_value
*ucontrol
)
1777 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
1778 struct hda_bind_ctls
*c
;
1779 unsigned long *vals
;
1780 int err
= 0, change
= 0;
1782 mutex_lock(&codec
->control_mutex
);
1783 c
= (struct hda_bind_ctls
*)kcontrol
->private_value
;
1784 for (vals
= c
->values
; *vals
; vals
++) {
1785 kcontrol
->private_value
= *vals
;
1786 err
= c
->ops
->put(kcontrol
, ucontrol
);
1791 kcontrol
->private_value
= (long)c
;
1792 mutex_unlock(&codec
->control_mutex
);
1793 return err
< 0 ? err
: change
;
1795 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_put
);
1797 int snd_hda_mixer_bind_tlv(struct snd_kcontrol
*kcontrol
, int op_flag
,
1798 unsigned int size
, unsigned int __user
*tlv
)
1800 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
1801 struct hda_bind_ctls
*c
;
1804 mutex_lock(&codec
->control_mutex
);
1805 c
= (struct hda_bind_ctls
*)kcontrol
->private_value
;
1806 kcontrol
->private_value
= *c
->values
;
1807 err
= c
->ops
->tlv(kcontrol
, op_flag
, size
, tlv
);
1808 kcontrol
->private_value
= (long)c
;
1809 mutex_unlock(&codec
->control_mutex
);
1812 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_tlv
);
1814 struct hda_ctl_ops snd_hda_bind_vol
= {
1815 .info
= snd_hda_mixer_amp_volume_info
,
1816 .get
= snd_hda_mixer_amp_volume_get
,
1817 .put
= snd_hda_mixer_amp_volume_put
,
1818 .tlv
= snd_hda_mixer_amp_tlv
1820 EXPORT_SYMBOL_HDA(snd_hda_bind_vol
);
1822 struct hda_ctl_ops snd_hda_bind_sw
= {
1823 .info
= snd_hda_mixer_amp_switch_info
,
1824 .get
= snd_hda_mixer_amp_switch_get
,
1825 .put
= snd_hda_mixer_amp_switch_put
,
1826 .tlv
= snd_hda_mixer_amp_tlv
1828 EXPORT_SYMBOL_HDA(snd_hda_bind_sw
);
1831 * SPDIF out controls
1834 static int snd_hda_spdif_mask_info(struct snd_kcontrol
*kcontrol
,
1835 struct snd_ctl_elem_info
*uinfo
)
1837 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_IEC958
;
1842 static int snd_hda_spdif_cmask_get(struct snd_kcontrol
*kcontrol
,
1843 struct snd_ctl_elem_value
*ucontrol
)
1845 ucontrol
->value
.iec958
.status
[0] = IEC958_AES0_PROFESSIONAL
|
1846 IEC958_AES0_NONAUDIO
|
1847 IEC958_AES0_CON_EMPHASIS_5015
|
1848 IEC958_AES0_CON_NOT_COPYRIGHT
;
1849 ucontrol
->value
.iec958
.status
[1] = IEC958_AES1_CON_CATEGORY
|
1850 IEC958_AES1_CON_ORIGINAL
;
1854 static int snd_hda_spdif_pmask_get(struct snd_kcontrol
*kcontrol
,
1855 struct snd_ctl_elem_value
*ucontrol
)
1857 ucontrol
->value
.iec958
.status
[0] = IEC958_AES0_PROFESSIONAL
|
1858 IEC958_AES0_NONAUDIO
|
1859 IEC958_AES0_PRO_EMPHASIS_5015
;
1863 static int snd_hda_spdif_default_get(struct snd_kcontrol
*kcontrol
,
1864 struct snd_ctl_elem_value
*ucontrol
)
1866 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
1868 ucontrol
->value
.iec958
.status
[0] = codec
->spdif_status
& 0xff;
1869 ucontrol
->value
.iec958
.status
[1] = (codec
->spdif_status
>> 8) & 0xff;
1870 ucontrol
->value
.iec958
.status
[2] = (codec
->spdif_status
>> 16) & 0xff;
1871 ucontrol
->value
.iec958
.status
[3] = (codec
->spdif_status
>> 24) & 0xff;
1876 /* convert from SPDIF status bits to HDA SPDIF bits
1877 * bit 0 (DigEn) is always set zero (to be filled later)
1879 static unsigned short convert_from_spdif_status(unsigned int sbits
)
1881 unsigned short val
= 0;
1883 if (sbits
& IEC958_AES0_PROFESSIONAL
)
1884 val
|= AC_DIG1_PROFESSIONAL
;
1885 if (sbits
& IEC958_AES0_NONAUDIO
)
1886 val
|= AC_DIG1_NONAUDIO
;
1887 if (sbits
& IEC958_AES0_PROFESSIONAL
) {
1888 if ((sbits
& IEC958_AES0_PRO_EMPHASIS
) ==
1889 IEC958_AES0_PRO_EMPHASIS_5015
)
1890 val
|= AC_DIG1_EMPHASIS
;
1892 if ((sbits
& IEC958_AES0_CON_EMPHASIS
) ==
1893 IEC958_AES0_CON_EMPHASIS_5015
)
1894 val
|= AC_DIG1_EMPHASIS
;
1895 if (!(sbits
& IEC958_AES0_CON_NOT_COPYRIGHT
))
1896 val
|= AC_DIG1_COPYRIGHT
;
1897 if (sbits
& (IEC958_AES1_CON_ORIGINAL
<< 8))
1898 val
|= AC_DIG1_LEVEL
;
1899 val
|= sbits
& (IEC958_AES1_CON_CATEGORY
<< 8);
1904 /* convert to SPDIF status bits from HDA SPDIF bits
1906 static unsigned int convert_to_spdif_status(unsigned short val
)
1908 unsigned int sbits
= 0;
1910 if (val
& AC_DIG1_NONAUDIO
)
1911 sbits
|= IEC958_AES0_NONAUDIO
;
1912 if (val
& AC_DIG1_PROFESSIONAL
)
1913 sbits
|= IEC958_AES0_PROFESSIONAL
;
1914 if (sbits
& IEC958_AES0_PROFESSIONAL
) {
1915 if (sbits
& AC_DIG1_EMPHASIS
)
1916 sbits
|= IEC958_AES0_PRO_EMPHASIS_5015
;
1918 if (val
& AC_DIG1_EMPHASIS
)
1919 sbits
|= IEC958_AES0_CON_EMPHASIS_5015
;
1920 if (!(val
& AC_DIG1_COPYRIGHT
))
1921 sbits
|= IEC958_AES0_CON_NOT_COPYRIGHT
;
1922 if (val
& AC_DIG1_LEVEL
)
1923 sbits
|= (IEC958_AES1_CON_ORIGINAL
<< 8);
1924 sbits
|= val
& (0x7f << 8);
1929 /* set digital convert verbs both for the given NID and its slaves */
1930 static void set_dig_out(struct hda_codec
*codec
, hda_nid_t nid
,
1935 snd_hda_codec_write_cache(codec
, nid
, 0, verb
, val
);
1936 d
= codec
->slave_dig_outs
;
1940 snd_hda_codec_write_cache(codec
, *d
, 0, verb
, val
);
1943 static inline void set_dig_out_convert(struct hda_codec
*codec
, hda_nid_t nid
,
1947 set_dig_out(codec
, nid
, AC_VERB_SET_DIGI_CONVERT_1
, dig1
);
1949 set_dig_out(codec
, nid
, AC_VERB_SET_DIGI_CONVERT_2
, dig2
);
1952 static int snd_hda_spdif_default_put(struct snd_kcontrol
*kcontrol
,
1953 struct snd_ctl_elem_value
*ucontrol
)
1955 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
1956 hda_nid_t nid
= kcontrol
->private_value
;
1960 mutex_lock(&codec
->spdif_mutex
);
1961 codec
->spdif_status
= ucontrol
->value
.iec958
.status
[0] |
1962 ((unsigned int)ucontrol
->value
.iec958
.status
[1] << 8) |
1963 ((unsigned int)ucontrol
->value
.iec958
.status
[2] << 16) |
1964 ((unsigned int)ucontrol
->value
.iec958
.status
[3] << 24);
1965 val
= convert_from_spdif_status(codec
->spdif_status
);
1966 val
|= codec
->spdif_ctls
& 1;
1967 change
= codec
->spdif_ctls
!= val
;
1968 codec
->spdif_ctls
= val
;
1971 set_dig_out_convert(codec
, nid
, val
& 0xff, (val
>> 8) & 0xff);
1973 mutex_unlock(&codec
->spdif_mutex
);
1977 #define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
1979 static int snd_hda_spdif_out_switch_get(struct snd_kcontrol
*kcontrol
,
1980 struct snd_ctl_elem_value
*ucontrol
)
1982 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
1984 ucontrol
->value
.integer
.value
[0] = codec
->spdif_ctls
& AC_DIG1_ENABLE
;
1988 static int snd_hda_spdif_out_switch_put(struct snd_kcontrol
*kcontrol
,
1989 struct snd_ctl_elem_value
*ucontrol
)
1991 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
1992 hda_nid_t nid
= kcontrol
->private_value
;
1996 mutex_lock(&codec
->spdif_mutex
);
1997 val
= codec
->spdif_ctls
& ~AC_DIG1_ENABLE
;
1998 if (ucontrol
->value
.integer
.value
[0])
1999 val
|= AC_DIG1_ENABLE
;
2000 change
= codec
->spdif_ctls
!= val
;
2002 codec
->spdif_ctls
= val
;
2003 set_dig_out_convert(codec
, nid
, val
& 0xff, -1);
2004 /* unmute amp switch (if any) */
2005 if ((get_wcaps(codec
, nid
) & AC_WCAP_OUT_AMP
) &&
2006 (val
& AC_DIG1_ENABLE
))
2007 snd_hda_codec_amp_stereo(codec
, nid
, HDA_OUTPUT
, 0,
2010 mutex_unlock(&codec
->spdif_mutex
);
2014 static struct snd_kcontrol_new dig_mixes
[] = {
2016 .access
= SNDRV_CTL_ELEM_ACCESS_READ
,
2017 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
2018 .name
= SNDRV_CTL_NAME_IEC958("",PLAYBACK
,CON_MASK
),
2019 .info
= snd_hda_spdif_mask_info
,
2020 .get
= snd_hda_spdif_cmask_get
,
2023 .access
= SNDRV_CTL_ELEM_ACCESS_READ
,
2024 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
2025 .name
= SNDRV_CTL_NAME_IEC958("",PLAYBACK
,PRO_MASK
),
2026 .info
= snd_hda_spdif_mask_info
,
2027 .get
= snd_hda_spdif_pmask_get
,
2030 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
2031 .name
= SNDRV_CTL_NAME_IEC958("",PLAYBACK
,DEFAULT
),
2032 .info
= snd_hda_spdif_mask_info
,
2033 .get
= snd_hda_spdif_default_get
,
2034 .put
= snd_hda_spdif_default_put
,
2037 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
2038 .name
= SNDRV_CTL_NAME_IEC958("",PLAYBACK
,SWITCH
),
2039 .info
= snd_hda_spdif_out_switch_info
,
2040 .get
= snd_hda_spdif_out_switch_get
,
2041 .put
= snd_hda_spdif_out_switch_put
,
2046 #define SPDIF_MAX_IDX 4 /* 4 instances should be enough to probe */
2049 * snd_hda_create_spdif_out_ctls - create Output SPDIF-related controls
2050 * @codec: the HDA codec
2051 * @nid: audio out widget NID
2053 * Creates controls related with the SPDIF output.
2054 * Called from each patch supporting the SPDIF out.
2056 * Returns 0 if successful, or a negative error code.
2058 int snd_hda_create_spdif_out_ctls(struct hda_codec
*codec
, hda_nid_t nid
)
2061 struct snd_kcontrol
*kctl
;
2062 struct snd_kcontrol_new
*dig_mix
;
2065 for (idx
= 0; idx
< SPDIF_MAX_IDX
; idx
++) {
2066 if (!_snd_hda_find_mixer_ctl(codec
, "IEC958 Playback Switch",
2070 if (idx
>= SPDIF_MAX_IDX
) {
2071 printk(KERN_ERR
"hda_codec: too many IEC958 outputs\n");
2074 for (dig_mix
= dig_mixes
; dig_mix
->name
; dig_mix
++) {
2075 kctl
= snd_ctl_new1(dig_mix
, codec
);
2078 kctl
->id
.index
= idx
;
2079 kctl
->private_value
= nid
;
2080 err
= snd_hda_ctl_add(codec
, kctl
);
2085 snd_hda_codec_read(codec
, nid
, 0,
2086 AC_VERB_GET_DIGI_CONVERT_1
, 0);
2087 codec
->spdif_status
= convert_to_spdif_status(codec
->spdif_ctls
);
2090 EXPORT_SYMBOL_HDA(snd_hda_create_spdif_out_ctls
);
2093 * SPDIF sharing with analog output
2095 static int spdif_share_sw_get(struct snd_kcontrol
*kcontrol
,
2096 struct snd_ctl_elem_value
*ucontrol
)
2098 struct hda_multi_out
*mout
= snd_kcontrol_chip(kcontrol
);
2099 ucontrol
->value
.integer
.value
[0] = mout
->share_spdif
;
2103 static int spdif_share_sw_put(struct snd_kcontrol
*kcontrol
,
2104 struct snd_ctl_elem_value
*ucontrol
)
2106 struct hda_multi_out
*mout
= snd_kcontrol_chip(kcontrol
);
2107 mout
->share_spdif
= !!ucontrol
->value
.integer
.value
[0];
2111 static struct snd_kcontrol_new spdif_share_sw
= {
2112 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
2113 .name
= "IEC958 Default PCM Playback Switch",
2114 .info
= snd_ctl_boolean_mono_info
,
2115 .get
= spdif_share_sw_get
,
2116 .put
= spdif_share_sw_put
,
2119 int snd_hda_create_spdif_share_sw(struct hda_codec
*codec
,
2120 struct hda_multi_out
*mout
)
2122 if (!mout
->dig_out_nid
)
2124 /* ATTENTION: here mout is passed as private_data, instead of codec */
2125 return snd_hda_ctl_add(codec
,
2126 snd_ctl_new1(&spdif_share_sw
, mout
));
2128 EXPORT_SYMBOL_HDA(snd_hda_create_spdif_share_sw
);
2134 #define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
2136 static int snd_hda_spdif_in_switch_get(struct snd_kcontrol
*kcontrol
,
2137 struct snd_ctl_elem_value
*ucontrol
)
2139 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2141 ucontrol
->value
.integer
.value
[0] = codec
->spdif_in_enable
;
2145 static int snd_hda_spdif_in_switch_put(struct snd_kcontrol
*kcontrol
,
2146 struct snd_ctl_elem_value
*ucontrol
)
2148 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2149 hda_nid_t nid
= kcontrol
->private_value
;
2150 unsigned int val
= !!ucontrol
->value
.integer
.value
[0];
2153 mutex_lock(&codec
->spdif_mutex
);
2154 change
= codec
->spdif_in_enable
!= val
;
2156 codec
->spdif_in_enable
= val
;
2157 snd_hda_codec_write_cache(codec
, nid
, 0,
2158 AC_VERB_SET_DIGI_CONVERT_1
, val
);
2160 mutex_unlock(&codec
->spdif_mutex
);
2164 static int snd_hda_spdif_in_status_get(struct snd_kcontrol
*kcontrol
,
2165 struct snd_ctl_elem_value
*ucontrol
)
2167 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2168 hda_nid_t nid
= kcontrol
->private_value
;
2172 val
= snd_hda_codec_read(codec
, nid
, 0, AC_VERB_GET_DIGI_CONVERT_1
, 0);
2173 sbits
= convert_to_spdif_status(val
);
2174 ucontrol
->value
.iec958
.status
[0] = sbits
;
2175 ucontrol
->value
.iec958
.status
[1] = sbits
>> 8;
2176 ucontrol
->value
.iec958
.status
[2] = sbits
>> 16;
2177 ucontrol
->value
.iec958
.status
[3] = sbits
>> 24;
2181 static struct snd_kcontrol_new dig_in_ctls
[] = {
2183 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
2184 .name
= SNDRV_CTL_NAME_IEC958("",CAPTURE
,SWITCH
),
2185 .info
= snd_hda_spdif_in_switch_info
,
2186 .get
= snd_hda_spdif_in_switch_get
,
2187 .put
= snd_hda_spdif_in_switch_put
,
2190 .access
= SNDRV_CTL_ELEM_ACCESS_READ
,
2191 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
2192 .name
= SNDRV_CTL_NAME_IEC958("",CAPTURE
,DEFAULT
),
2193 .info
= snd_hda_spdif_mask_info
,
2194 .get
= snd_hda_spdif_in_status_get
,
2200 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
2201 * @codec: the HDA codec
2202 * @nid: audio in widget NID
2204 * Creates controls related with the SPDIF input.
2205 * Called from each patch supporting the SPDIF in.
2207 * Returns 0 if successful, or a negative error code.
2209 int snd_hda_create_spdif_in_ctls(struct hda_codec
*codec
, hda_nid_t nid
)
2212 struct snd_kcontrol
*kctl
;
2213 struct snd_kcontrol_new
*dig_mix
;
2216 for (idx
= 0; idx
< SPDIF_MAX_IDX
; idx
++) {
2217 if (!_snd_hda_find_mixer_ctl(codec
, "IEC958 Capture Switch",
2221 if (idx
>= SPDIF_MAX_IDX
) {
2222 printk(KERN_ERR
"hda_codec: too many IEC958 inputs\n");
2225 for (dig_mix
= dig_in_ctls
; dig_mix
->name
; dig_mix
++) {
2226 kctl
= snd_ctl_new1(dig_mix
, codec
);
2229 kctl
->private_value
= nid
;
2230 err
= snd_hda_ctl_add(codec
, kctl
);
2234 codec
->spdif_in_enable
=
2235 snd_hda_codec_read(codec
, nid
, 0,
2236 AC_VERB_GET_DIGI_CONVERT_1
, 0) &
2240 EXPORT_SYMBOL_HDA(snd_hda_create_spdif_in_ctls
);
2242 #ifdef SND_HDA_NEEDS_RESUME
2247 /* build a 32bit cache key with the widget id and the command parameter */
2248 #define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
2249 #define get_cmd_cache_nid(key) ((key) & 0xff)
2250 #define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
2253 * snd_hda_codec_write_cache - send a single command with caching
2254 * @codec: the HDA codec
2255 * @nid: NID to send the command
2256 * @direct: direct flag
2257 * @verb: the verb to send
2258 * @parm: the parameter for the verb
2260 * Send a single command without waiting for response.
2262 * Returns 0 if successful, or a negative error code.
2264 int snd_hda_codec_write_cache(struct hda_codec
*codec
, hda_nid_t nid
,
2265 int direct
, unsigned int verb
, unsigned int parm
)
2267 struct hda_bus
*bus
= codec
->bus
;
2271 res
= make_codec_cmd(codec
, nid
, direct
, verb
, parm
);
2272 snd_hda_power_up(codec
);
2273 mutex_lock(&bus
->cmd_mutex
);
2274 err
= bus
->ops
.command(bus
, res
);
2276 struct hda_cache_head
*c
;
2277 u32 key
= build_cmd_cache_key(nid
, verb
);
2278 c
= get_alloc_hash(&codec
->cmd_cache
, key
);
2282 mutex_unlock(&bus
->cmd_mutex
);
2283 snd_hda_power_down(codec
);
2286 EXPORT_SYMBOL_HDA(snd_hda_codec_write_cache
);
2288 /* resume the all commands from the cache */
2289 void snd_hda_codec_resume_cache(struct hda_codec
*codec
)
2291 struct hda_cache_head
*buffer
= codec
->cmd_cache
.buf
.list
;
2294 for (i
= 0; i
< codec
->cmd_cache
.buf
.used
; i
++, buffer
++) {
2295 u32 key
= buffer
->key
;
2298 snd_hda_codec_write(codec
, get_cmd_cache_nid(key
), 0,
2299 get_cmd_cache_cmd(key
), buffer
->val
);
2302 EXPORT_SYMBOL_HDA(snd_hda_codec_resume_cache
);
2305 * snd_hda_sequence_write_cache - sequence writes with caching
2306 * @codec: the HDA codec
2307 * @seq: VERB array to send
2309 * Send the commands sequentially from the given array.
2310 * Thte commands are recorded on cache for power-save and resume.
2311 * The array must be terminated with NID=0.
2313 void snd_hda_sequence_write_cache(struct hda_codec
*codec
,
2314 const struct hda_verb
*seq
)
2316 for (; seq
->nid
; seq
++)
2317 snd_hda_codec_write_cache(codec
, seq
->nid
, 0, seq
->verb
,
2320 EXPORT_SYMBOL_HDA(snd_hda_sequence_write_cache
);
2321 #endif /* SND_HDA_NEEDS_RESUME */
2324 * set power state of the codec
2326 static void hda_set_power_state(struct hda_codec
*codec
, hda_nid_t fg
,
2327 unsigned int power_state
)
2332 snd_hda_codec_write(codec
, fg
, 0, AC_VERB_SET_POWER_STATE
,
2334 msleep(10); /* partial workaround for "azx_get_response timeout" */
2336 nid
= codec
->start_nid
;
2337 for (i
= 0; i
< codec
->num_nodes
; i
++, nid
++) {
2338 unsigned int wcaps
= get_wcaps(codec
, nid
);
2339 if (wcaps
& AC_WCAP_POWER
) {
2340 unsigned int wid_type
= (wcaps
& AC_WCAP_TYPE
) >>
2342 if (wid_type
== AC_WID_PIN
) {
2343 unsigned int pincap
;
2345 * don't power down the widget if it controls
2346 * eapd and EAPD_BTLENABLE is set.
2348 pincap
= snd_hda_query_pin_caps(codec
, nid
);
2349 if (pincap
& AC_PINCAP_EAPD
) {
2350 int eapd
= snd_hda_codec_read(codec
,
2352 AC_VERB_GET_EAPD_BTLENABLE
, 0);
2354 if (power_state
== AC_PWRST_D3
&& eapd
)
2358 snd_hda_codec_write(codec
, nid
, 0,
2359 AC_VERB_SET_POWER_STATE
,
2364 if (power_state
== AC_PWRST_D0
) {
2365 unsigned long end_time
;
2368 /* wait until the codec reachs to D0 */
2369 end_time
= jiffies
+ msecs_to_jiffies(500);
2371 state
= snd_hda_codec_read(codec
, fg
, 0,
2372 AC_VERB_GET_POWER_STATE
, 0);
2373 if (state
== power_state
)
2376 } while (time_after_eq(end_time
, jiffies
));
2380 #ifdef CONFIG_SND_HDA_HWDEP
2381 /* execute additional init verbs */
2382 static void hda_exec_init_verbs(struct hda_codec
*codec
)
2384 if (codec
->init_verbs
.list
)
2385 snd_hda_sequence_write(codec
, codec
->init_verbs
.list
);
2388 static inline void hda_exec_init_verbs(struct hda_codec
*codec
) {}
2391 #ifdef SND_HDA_NEEDS_RESUME
2393 * call suspend and power-down; used both from PM and power-save
2395 static void hda_call_codec_suspend(struct hda_codec
*codec
)
2397 if (codec
->patch_ops
.suspend
)
2398 codec
->patch_ops
.suspend(codec
, PMSG_SUSPEND
);
2399 hda_set_power_state(codec
,
2400 codec
->afg
? codec
->afg
: codec
->mfg
,
2402 #ifdef CONFIG_SND_HDA_POWER_SAVE
2403 cancel_delayed_work(&codec
->power_work
);
2404 codec
->power_on
= 0;
2405 codec
->power_transition
= 0;
2410 * kick up codec; used both from PM and power-save
2412 static void hda_call_codec_resume(struct hda_codec
*codec
)
2414 hda_set_power_state(codec
,
2415 codec
->afg
? codec
->afg
: codec
->mfg
,
2417 restore_pincfgs(codec
); /* restore all current pin configs */
2418 hda_exec_init_verbs(codec
);
2419 if (codec
->patch_ops
.resume
)
2420 codec
->patch_ops
.resume(codec
);
2422 if (codec
->patch_ops
.init
)
2423 codec
->patch_ops
.init(codec
);
2424 snd_hda_codec_resume_amp(codec
);
2425 snd_hda_codec_resume_cache(codec
);
2428 #endif /* SND_HDA_NEEDS_RESUME */
2432 * snd_hda_build_controls - build mixer controls
2435 * Creates mixer controls for each codec included in the bus.
2437 * Returns 0 if successful, otherwise a negative error code.
2439 int /*__devinit*/ snd_hda_build_controls(struct hda_bus
*bus
)
2441 struct hda_codec
*codec
;
2443 list_for_each_entry(codec
, &bus
->codec_list
, list
) {
2444 int err
= snd_hda_codec_build_controls(codec
);
2446 printk(KERN_ERR
"hda_codec: cannot build controls"
2447 "for #%d (error %d)\n", codec
->addr
, err
);
2448 err
= snd_hda_codec_reset(codec
);
2451 "hda_codec: cannot revert codec\n");
2458 EXPORT_SYMBOL_HDA(snd_hda_build_controls
);
2460 int snd_hda_codec_build_controls(struct hda_codec
*codec
)
2463 hda_exec_init_verbs(codec
);
2464 /* continue to initialize... */
2465 if (codec
->patch_ops
.init
)
2466 err
= codec
->patch_ops
.init(codec
);
2467 if (!err
&& codec
->patch_ops
.build_controls
)
2468 err
= codec
->patch_ops
.build_controls(codec
);
2477 struct hda_rate_tbl
{
2479 unsigned int alsa_bits
;
2480 unsigned int hda_fmt
;
2483 static struct hda_rate_tbl rate_bits
[] = {
2484 /* rate in Hz, ALSA rate bitmask, HDA format value */
2486 /* autodetected value used in snd_hda_query_supported_pcm */
2487 { 8000, SNDRV_PCM_RATE_8000
, 0x0500 }, /* 1/6 x 48 */
2488 { 11025, SNDRV_PCM_RATE_11025
, 0x4300 }, /* 1/4 x 44 */
2489 { 16000, SNDRV_PCM_RATE_16000
, 0x0200 }, /* 1/3 x 48 */
2490 { 22050, SNDRV_PCM_RATE_22050
, 0x4100 }, /* 1/2 x 44 */
2491 { 32000, SNDRV_PCM_RATE_32000
, 0x0a00 }, /* 2/3 x 48 */
2492 { 44100, SNDRV_PCM_RATE_44100
, 0x4000 }, /* 44 */
2493 { 48000, SNDRV_PCM_RATE_48000
, 0x0000 }, /* 48 */
2494 { 88200, SNDRV_PCM_RATE_88200
, 0x4800 }, /* 2 x 44 */
2495 { 96000, SNDRV_PCM_RATE_96000
, 0x0800 }, /* 2 x 48 */
2496 { 176400, SNDRV_PCM_RATE_176400
, 0x5800 },/* 4 x 44 */
2497 { 192000, SNDRV_PCM_RATE_192000
, 0x1800 }, /* 4 x 48 */
2498 #define AC_PAR_PCM_RATE_BITS 11
2499 /* up to bits 10, 384kHZ isn't supported properly */
2501 /* not autodetected value */
2502 { 9600, SNDRV_PCM_RATE_KNOT
, 0x0400 }, /* 1/5 x 48 */
2504 { 0 } /* terminator */
2508 * snd_hda_calc_stream_format - calculate format bitset
2509 * @rate: the sample rate
2510 * @channels: the number of channels
2511 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
2512 * @maxbps: the max. bps
2514 * Calculate the format bitset from the given rate, channels and th PCM format.
2516 * Return zero if invalid.
2518 unsigned int snd_hda_calc_stream_format(unsigned int rate
,
2519 unsigned int channels
,
2520 unsigned int format
,
2521 unsigned int maxbps
)
2524 unsigned int val
= 0;
2526 for (i
= 0; rate_bits
[i
].hz
; i
++)
2527 if (rate_bits
[i
].hz
== rate
) {
2528 val
= rate_bits
[i
].hda_fmt
;
2531 if (!rate_bits
[i
].hz
) {
2532 snd_printdd("invalid rate %d\n", rate
);
2536 if (channels
== 0 || channels
> 8) {
2537 snd_printdd("invalid channels %d\n", channels
);
2540 val
|= channels
- 1;
2542 switch (snd_pcm_format_width(format
)) {
2543 case 8: val
|= 0x00; break;
2544 case 16: val
|= 0x10; break;
2550 else if (maxbps
>= 24)
2556 snd_printdd("invalid format width %d\n",
2557 snd_pcm_format_width(format
));
2563 EXPORT_SYMBOL_HDA(snd_hda_calc_stream_format
);
2565 static unsigned int get_pcm_param(struct hda_codec
*codec
, hda_nid_t nid
)
2567 unsigned int val
= 0;
2568 if (nid
!= codec
->afg
&&
2569 (get_wcaps(codec
, nid
) & AC_WCAP_FORMAT_OVRD
))
2570 val
= snd_hda_param_read(codec
, nid
, AC_PAR_PCM
);
2571 if (!val
|| val
== -1)
2572 val
= snd_hda_param_read(codec
, codec
->afg
, AC_PAR_PCM
);
2573 if (!val
|| val
== -1)
2578 static unsigned int query_pcm_param(struct hda_codec
*codec
, hda_nid_t nid
)
2580 return query_caps_hash(codec
, nid
, HDA_HASH_PARPCM_KEY(nid
),
2584 static unsigned int get_stream_param(struct hda_codec
*codec
, hda_nid_t nid
)
2586 unsigned int streams
= snd_hda_param_read(codec
, nid
, AC_PAR_STREAM
);
2587 if (!streams
|| streams
== -1)
2588 streams
= snd_hda_param_read(codec
, codec
->afg
, AC_PAR_STREAM
);
2589 if (!streams
|| streams
== -1)
2594 static unsigned int query_stream_param(struct hda_codec
*codec
, hda_nid_t nid
)
2596 return query_caps_hash(codec
, nid
, HDA_HASH_PARSTR_KEY(nid
),
2601 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
2602 * @codec: the HDA codec
2603 * @nid: NID to query
2604 * @ratesp: the pointer to store the detected rate bitflags
2605 * @formatsp: the pointer to store the detected formats
2606 * @bpsp: the pointer to store the detected format widths
2608 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
2609 * or @bsps argument is ignored.
2611 * Returns 0 if successful, otherwise a negative error code.
2613 static int snd_hda_query_supported_pcm(struct hda_codec
*codec
, hda_nid_t nid
,
2614 u32
*ratesp
, u64
*formatsp
, unsigned int *bpsp
)
2616 unsigned int i
, val
, wcaps
;
2618 wcaps
= get_wcaps(codec
, nid
);
2619 val
= query_pcm_param(codec
, nid
);
2623 for (i
= 0; i
< AC_PAR_PCM_RATE_BITS
; i
++) {
2625 rates
|= rate_bits
[i
].alsa_bits
;
2628 snd_printk(KERN_ERR
"hda_codec: rates == 0 "
2629 "(nid=0x%x, val=0x%x, ovrd=%i)\n",
2631 (wcaps
& AC_WCAP_FORMAT_OVRD
) ? 1 : 0);
2637 if (formatsp
|| bpsp
) {
2639 unsigned int streams
, bps
;
2641 streams
= query_stream_param(codec
, nid
);
2646 if (streams
& AC_SUPFMT_PCM
) {
2647 if (val
& AC_SUPPCM_BITS_8
) {
2648 formats
|= SNDRV_PCM_FMTBIT_U8
;
2651 if (val
& AC_SUPPCM_BITS_16
) {
2652 formats
|= SNDRV_PCM_FMTBIT_S16_LE
;
2655 if (wcaps
& AC_WCAP_DIGITAL
) {
2656 if (val
& AC_SUPPCM_BITS_32
)
2657 formats
|= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
;
2658 if (val
& (AC_SUPPCM_BITS_20
|AC_SUPPCM_BITS_24
))
2659 formats
|= SNDRV_PCM_FMTBIT_S32_LE
;
2660 if (val
& AC_SUPPCM_BITS_24
)
2662 else if (val
& AC_SUPPCM_BITS_20
)
2664 } else if (val
& (AC_SUPPCM_BITS_20
|AC_SUPPCM_BITS_24
|
2665 AC_SUPPCM_BITS_32
)) {
2666 formats
|= SNDRV_PCM_FMTBIT_S32_LE
;
2667 if (val
& AC_SUPPCM_BITS_32
)
2669 else if (val
& AC_SUPPCM_BITS_24
)
2671 else if (val
& AC_SUPPCM_BITS_20
)
2675 else if (streams
== AC_SUPFMT_FLOAT32
) {
2676 /* should be exclusive */
2677 formats
|= SNDRV_PCM_FMTBIT_FLOAT_LE
;
2679 } else if (streams
== AC_SUPFMT_AC3
) {
2680 /* should be exclusive */
2681 /* temporary hack: we have still no proper support
2682 * for the direct AC3 stream...
2684 formats
|= SNDRV_PCM_FMTBIT_U8
;
2688 snd_printk(KERN_ERR
"hda_codec: formats == 0 "
2689 "(nid=0x%x, val=0x%x, ovrd=%i, "
2692 (wcaps
& AC_WCAP_FORMAT_OVRD
) ? 1 : 0,
2697 *formatsp
= formats
;
2706 * snd_hda_is_supported_format - check whether the given node supports
2709 * Returns 1 if supported, 0 if not.
2711 int snd_hda_is_supported_format(struct hda_codec
*codec
, hda_nid_t nid
,
2712 unsigned int format
)
2715 unsigned int val
= 0, rate
, stream
;
2717 val
= query_pcm_param(codec
, nid
);
2721 rate
= format
& 0xff00;
2722 for (i
= 0; i
< AC_PAR_PCM_RATE_BITS
; i
++)
2723 if (rate_bits
[i
].hda_fmt
== rate
) {
2728 if (i
>= AC_PAR_PCM_RATE_BITS
)
2731 stream
= query_stream_param(codec
, nid
);
2735 if (stream
& AC_SUPFMT_PCM
) {
2736 switch (format
& 0xf0) {
2738 if (!(val
& AC_SUPPCM_BITS_8
))
2742 if (!(val
& AC_SUPPCM_BITS_16
))
2746 if (!(val
& AC_SUPPCM_BITS_20
))
2750 if (!(val
& AC_SUPPCM_BITS_24
))
2754 if (!(val
& AC_SUPPCM_BITS_32
))
2761 /* FIXME: check for float32 and AC3? */
2766 EXPORT_SYMBOL_HDA(snd_hda_is_supported_format
);
2771 static int hda_pcm_default_open_close(struct hda_pcm_stream
*hinfo
,
2772 struct hda_codec
*codec
,
2773 struct snd_pcm_substream
*substream
)
2778 static int hda_pcm_default_prepare(struct hda_pcm_stream
*hinfo
,
2779 struct hda_codec
*codec
,
2780 unsigned int stream_tag
,
2781 unsigned int format
,
2782 struct snd_pcm_substream
*substream
)
2784 snd_hda_codec_setup_stream(codec
, hinfo
->nid
, stream_tag
, 0, format
);
2788 static int hda_pcm_default_cleanup(struct hda_pcm_stream
*hinfo
,
2789 struct hda_codec
*codec
,
2790 struct snd_pcm_substream
*substream
)
2792 snd_hda_codec_cleanup_stream(codec
, hinfo
->nid
);
2796 static int set_pcm_default_values(struct hda_codec
*codec
,
2797 struct hda_pcm_stream
*info
)
2801 /* query support PCM information from the given NID */
2802 if (info
->nid
&& (!info
->rates
|| !info
->formats
)) {
2803 err
= snd_hda_query_supported_pcm(codec
, info
->nid
,
2804 info
->rates
? NULL
: &info
->rates
,
2805 info
->formats
? NULL
: &info
->formats
,
2806 info
->maxbps
? NULL
: &info
->maxbps
);
2810 if (info
->ops
.open
== NULL
)
2811 info
->ops
.open
= hda_pcm_default_open_close
;
2812 if (info
->ops
.close
== NULL
)
2813 info
->ops
.close
= hda_pcm_default_open_close
;
2814 if (info
->ops
.prepare
== NULL
) {
2815 if (snd_BUG_ON(!info
->nid
))
2817 info
->ops
.prepare
= hda_pcm_default_prepare
;
2819 if (info
->ops
.cleanup
== NULL
) {
2820 if (snd_BUG_ON(!info
->nid
))
2822 info
->ops
.cleanup
= hda_pcm_default_cleanup
;
2828 * get the empty PCM device number to assign
2830 static int get_empty_pcm_device(struct hda_bus
*bus
, int type
)
2832 static const char *dev_name
[HDA_PCM_NTYPES
] = {
2833 "Audio", "SPDIF", "HDMI", "Modem"
2835 /* starting device index for each PCM type */
2836 static int dev_idx
[HDA_PCM_NTYPES
] = {
2837 [HDA_PCM_TYPE_AUDIO
] = 0,
2838 [HDA_PCM_TYPE_SPDIF
] = 1,
2839 [HDA_PCM_TYPE_HDMI
] = 3,
2840 [HDA_PCM_TYPE_MODEM
] = 6
2842 /* normal audio device indices; not linear to keep compatibility */
2843 static int audio_idx
[4] = { 0, 2, 4, 5 };
2847 case HDA_PCM_TYPE_AUDIO
:
2848 for (i
= 0; i
< ARRAY_SIZE(audio_idx
); i
++) {
2850 if (!test_bit(dev
, bus
->pcm_dev_bits
))
2853 snd_printk(KERN_WARNING
"Too many audio devices\n");
2855 case HDA_PCM_TYPE_SPDIF
:
2856 case HDA_PCM_TYPE_HDMI
:
2857 case HDA_PCM_TYPE_MODEM
:
2858 dev
= dev_idx
[type
];
2859 if (test_bit(dev
, bus
->pcm_dev_bits
)) {
2860 snd_printk(KERN_WARNING
"%s already defined\n",
2866 snd_printk(KERN_WARNING
"Invalid PCM type %d\n", type
);
2870 set_bit(dev
, bus
->pcm_dev_bits
);
2875 * attach a new PCM stream
2877 static int snd_hda_attach_pcm(struct hda_codec
*codec
, struct hda_pcm
*pcm
)
2879 struct hda_bus
*bus
= codec
->bus
;
2880 struct hda_pcm_stream
*info
;
2883 if (snd_BUG_ON(!pcm
->name
))
2885 for (stream
= 0; stream
< 2; stream
++) {
2886 info
= &pcm
->stream
[stream
];
2887 if (info
->substreams
) {
2888 err
= set_pcm_default_values(codec
, info
);
2893 return bus
->ops
.attach_pcm(bus
, codec
, pcm
);
2896 /* assign all PCMs of the given codec */
2897 int snd_hda_codec_build_pcms(struct hda_codec
*codec
)
2902 if (!codec
->num_pcms
) {
2903 if (!codec
->patch_ops
.build_pcms
)
2905 err
= codec
->patch_ops
.build_pcms(codec
);
2907 printk(KERN_ERR
"hda_codec: cannot build PCMs"
2908 "for #%d (error %d)\n", codec
->addr
, err
);
2909 err
= snd_hda_codec_reset(codec
);
2912 "hda_codec: cannot revert codec\n");
2917 for (pcm
= 0; pcm
< codec
->num_pcms
; pcm
++) {
2918 struct hda_pcm
*cpcm
= &codec
->pcm_info
[pcm
];
2921 if (!cpcm
->stream
[0].substreams
&& !cpcm
->stream
[1].substreams
)
2922 continue; /* no substreams assigned */
2925 dev
= get_empty_pcm_device(codec
->bus
, cpcm
->pcm_type
);
2927 continue; /* no fatal error */
2929 err
= snd_hda_attach_pcm(codec
, cpcm
);
2931 printk(KERN_ERR
"hda_codec: cannot attach "
2932 "PCM stream %d for codec #%d\n",
2934 continue; /* no fatal error */
2942 * snd_hda_build_pcms - build PCM information
2945 * Create PCM information for each codec included in the bus.
2947 * The build_pcms codec patch is requested to set up codec->num_pcms and
2948 * codec->pcm_info properly. The array is referred by the top-level driver
2949 * to create its PCM instances.
2950 * The allocated codec->pcm_info should be released in codec->patch_ops.free
2953 * At least, substreams, channels_min and channels_max must be filled for
2954 * each stream. substreams = 0 indicates that the stream doesn't exist.
2955 * When rates and/or formats are zero, the supported values are queried
2956 * from the given nid. The nid is used also by the default ops.prepare
2957 * and ops.cleanup callbacks.
2959 * The driver needs to call ops.open in its open callback. Similarly,
2960 * ops.close is supposed to be called in the close callback.
2961 * ops.prepare should be called in the prepare or hw_params callback
2962 * with the proper parameters for set up.
2963 * ops.cleanup should be called in hw_free for clean up of streams.
2965 * This function returns 0 if successfull, or a negative error code.
2967 int __devinit
snd_hda_build_pcms(struct hda_bus
*bus
)
2969 struct hda_codec
*codec
;
2971 list_for_each_entry(codec
, &bus
->codec_list
, list
) {
2972 int err
= snd_hda_codec_build_pcms(codec
);
2978 EXPORT_SYMBOL_HDA(snd_hda_build_pcms
);
2981 * snd_hda_check_board_config - compare the current codec with the config table
2982 * @codec: the HDA codec
2983 * @num_configs: number of config enums
2984 * @models: array of model name strings
2985 * @tbl: configuration table, terminated by null entries
2987 * Compares the modelname or PCI subsystem id of the current codec with the
2988 * given configuration table. If a matching entry is found, returns its
2989 * config value (supposed to be 0 or positive).
2991 * If no entries are matching, the function returns a negative value.
2993 int snd_hda_check_board_config(struct hda_codec
*codec
,
2994 int num_configs
, const char **models
,
2995 const struct snd_pci_quirk
*tbl
)
2997 if (codec
->modelname
&& models
) {
2999 for (i
= 0; i
< num_configs
; i
++) {
3001 !strcmp(codec
->modelname
, models
[i
])) {
3002 snd_printd(KERN_INFO
"hda_codec: model '%s' is "
3003 "selected\n", models
[i
]);
3009 if (!codec
->bus
->pci
|| !tbl
)
3012 tbl
= snd_pci_quirk_lookup(codec
->bus
->pci
, tbl
);
3015 if (tbl
->value
>= 0 && tbl
->value
< num_configs
) {
3016 #ifdef CONFIG_SND_DEBUG_VERBOSE
3018 const char *model
= NULL
;
3020 model
= models
[tbl
->value
];
3022 sprintf(tmp
, "#%d", tbl
->value
);
3025 snd_printdd(KERN_INFO
"hda_codec: model '%s' is selected "
3026 "for config %x:%x (%s)\n",
3027 model
, tbl
->subvendor
, tbl
->subdevice
,
3028 (tbl
->name
? tbl
->name
: "Unknown device"));
3034 EXPORT_SYMBOL_HDA(snd_hda_check_board_config
);
3037 * snd_hda_check_board_codec_sid_config - compare the current codec
3038 subsystem ID with the
3041 This is important for Gateway notebooks with SB450 HDA Audio
3042 where the vendor ID of the PCI device is:
3043 ATI Technologies Inc SB450 HDA Audio [1002:437b]
3044 and the vendor/subvendor are found only at the codec.
3046 * @codec: the HDA codec
3047 * @num_configs: number of config enums
3048 * @models: array of model name strings
3049 * @tbl: configuration table, terminated by null entries
3051 * Compares the modelname or PCI subsystem id of the current codec with the
3052 * given configuration table. If a matching entry is found, returns its
3053 * config value (supposed to be 0 or positive).
3055 * If no entries are matching, the function returns a negative value.
3057 int snd_hda_check_board_codec_sid_config(struct hda_codec
*codec
,
3058 int num_configs
, const char **models
,
3059 const struct snd_pci_quirk
*tbl
)
3061 const struct snd_pci_quirk
*q
;
3063 /* Search for codec ID */
3064 for (q
= tbl
; q
->subvendor
; q
++) {
3065 unsigned long vendorid
= (q
->subdevice
) | (q
->subvendor
<< 16);
3067 if (vendorid
== codec
->subsystem_id
)
3076 if (tbl
->value
>= 0 && tbl
->value
< num_configs
) {
3077 #ifdef CONFIG_SND_DEBUG_DETECT
3079 const char *model
= NULL
;
3081 model
= models
[tbl
->value
];
3083 sprintf(tmp
, "#%d", tbl
->value
);
3086 snd_printdd(KERN_INFO
"hda_codec: model '%s' is selected "
3087 "for config %x:%x (%s)\n",
3088 model
, tbl
->subvendor
, tbl
->subdevice
,
3089 (tbl
->name
? tbl
->name
: "Unknown device"));
3095 EXPORT_SYMBOL_HDA(snd_hda_check_board_codec_sid_config
);
3098 * snd_hda_add_new_ctls - create controls from the array
3099 * @codec: the HDA codec
3100 * @knew: the array of struct snd_kcontrol_new
3102 * This helper function creates and add new controls in the given array.
3103 * The array must be terminated with an empty entry as terminator.
3105 * Returns 0 if successful, or a negative error code.
3107 int snd_hda_add_new_ctls(struct hda_codec
*codec
, struct snd_kcontrol_new
*knew
)
3111 for (; knew
->name
; knew
++) {
3112 struct snd_kcontrol
*kctl
;
3113 kctl
= snd_ctl_new1(knew
, codec
);
3116 err
= snd_hda_ctl_add(codec
, kctl
);
3120 kctl
= snd_ctl_new1(knew
, codec
);
3123 kctl
->id
.device
= codec
->addr
;
3124 err
= snd_hda_ctl_add(codec
, kctl
);
3131 EXPORT_SYMBOL_HDA(snd_hda_add_new_ctls
);
3133 #ifdef CONFIG_SND_HDA_POWER_SAVE
3134 static void hda_set_power_state(struct hda_codec
*codec
, hda_nid_t fg
,
3135 unsigned int power_state
);
3137 static void hda_power_work(struct work_struct
*work
)
3139 struct hda_codec
*codec
=
3140 container_of(work
, struct hda_codec
, power_work
.work
);
3141 struct hda_bus
*bus
= codec
->bus
;
3143 if (!codec
->power_on
|| codec
->power_count
) {
3144 codec
->power_transition
= 0;
3148 hda_call_codec_suspend(codec
);
3149 if (bus
->ops
.pm_notify
)
3150 bus
->ops
.pm_notify(bus
);
3153 static void hda_keep_power_on(struct hda_codec
*codec
)
3155 codec
->power_count
++;
3156 codec
->power_on
= 1;
3159 void snd_hda_power_up(struct hda_codec
*codec
)
3161 struct hda_bus
*bus
= codec
->bus
;
3163 codec
->power_count
++;
3164 if (codec
->power_on
|| codec
->power_transition
)
3167 codec
->power_on
= 1;
3168 if (bus
->ops
.pm_notify
)
3169 bus
->ops
.pm_notify(bus
);
3170 hda_call_codec_resume(codec
);
3171 cancel_delayed_work(&codec
->power_work
);
3172 codec
->power_transition
= 0;
3174 EXPORT_SYMBOL_HDA(snd_hda_power_up
);
3176 #define power_save(codec) \
3177 ((codec)->bus->power_save ? *(codec)->bus->power_save : 0)
3179 #define power_save(codec) \
3180 ((codec)->bus->power_save ? *(codec)->bus->power_save : 0)
3182 void snd_hda_power_down(struct hda_codec
*codec
)
3184 --codec
->power_count
;
3185 if (!codec
->power_on
|| codec
->power_count
|| codec
->power_transition
)
3187 if (power_save(codec
)) {
3188 codec
->power_transition
= 1; /* avoid reentrance */
3189 queue_delayed_work(codec
->bus
->workq
, &codec
->power_work
,
3190 msecs_to_jiffies(power_save(codec
) * 1000));
3193 EXPORT_SYMBOL_HDA(snd_hda_power_down
);
3195 int snd_hda_check_amp_list_power(struct hda_codec
*codec
,
3196 struct hda_loopback_check
*check
,
3199 struct hda_amp_list
*p
;
3202 if (!check
->amplist
)
3204 for (p
= check
->amplist
; p
->nid
; p
++) {
3209 return 0; /* nothing changed */
3211 for (p
= check
->amplist
; p
->nid
; p
++) {
3212 for (ch
= 0; ch
< 2; ch
++) {
3213 v
= snd_hda_codec_amp_read(codec
, p
->nid
, ch
, p
->dir
,
3215 if (!(v
& HDA_AMP_MUTE
) && v
> 0) {
3216 if (!check
->power_on
) {
3217 check
->power_on
= 1;
3218 snd_hda_power_up(codec
);
3224 if (check
->power_on
) {
3225 check
->power_on
= 0;
3226 snd_hda_power_down(codec
);
3230 EXPORT_SYMBOL_HDA(snd_hda_check_amp_list_power
);
3234 * Channel mode helper
3236 int snd_hda_ch_mode_info(struct hda_codec
*codec
,
3237 struct snd_ctl_elem_info
*uinfo
,
3238 const struct hda_channel_mode
*chmode
,
3241 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_ENUMERATED
;
3243 uinfo
->value
.enumerated
.items
= num_chmodes
;
3244 if (uinfo
->value
.enumerated
.item
>= num_chmodes
)
3245 uinfo
->value
.enumerated
.item
= num_chmodes
- 1;
3246 sprintf(uinfo
->value
.enumerated
.name
, "%dch",
3247 chmode
[uinfo
->value
.enumerated
.item
].channels
);
3250 EXPORT_SYMBOL_HDA(snd_hda_ch_mode_info
);
3252 int snd_hda_ch_mode_get(struct hda_codec
*codec
,
3253 struct snd_ctl_elem_value
*ucontrol
,
3254 const struct hda_channel_mode
*chmode
,
3260 for (i
= 0; i
< num_chmodes
; i
++) {
3261 if (max_channels
== chmode
[i
].channels
) {
3262 ucontrol
->value
.enumerated
.item
[0] = i
;
3268 EXPORT_SYMBOL_HDA(snd_hda_ch_mode_get
);
3270 int snd_hda_ch_mode_put(struct hda_codec
*codec
,
3271 struct snd_ctl_elem_value
*ucontrol
,
3272 const struct hda_channel_mode
*chmode
,
3278 mode
= ucontrol
->value
.enumerated
.item
[0];
3279 if (mode
>= num_chmodes
)
3281 if (*max_channelsp
== chmode
[mode
].channels
)
3283 /* change the current channel setting */
3284 *max_channelsp
= chmode
[mode
].channels
;
3285 if (chmode
[mode
].sequence
)
3286 snd_hda_sequence_write_cache(codec
, chmode
[mode
].sequence
);
3289 EXPORT_SYMBOL_HDA(snd_hda_ch_mode_put
);
3294 int snd_hda_input_mux_info(const struct hda_input_mux
*imux
,
3295 struct snd_ctl_elem_info
*uinfo
)
3299 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_ENUMERATED
;
3301 uinfo
->value
.enumerated
.items
= imux
->num_items
;
3302 if (!imux
->num_items
)
3304 index
= uinfo
->value
.enumerated
.item
;
3305 if (index
>= imux
->num_items
)
3306 index
= imux
->num_items
- 1;
3307 strcpy(uinfo
->value
.enumerated
.name
, imux
->items
[index
].label
);
3310 EXPORT_SYMBOL_HDA(snd_hda_input_mux_info
);
3312 int snd_hda_input_mux_put(struct hda_codec
*codec
,
3313 const struct hda_input_mux
*imux
,
3314 struct snd_ctl_elem_value
*ucontrol
,
3316 unsigned int *cur_val
)
3320 if (!imux
->num_items
)
3322 idx
= ucontrol
->value
.enumerated
.item
[0];
3323 if (idx
>= imux
->num_items
)
3324 idx
= imux
->num_items
- 1;
3325 if (*cur_val
== idx
)
3327 snd_hda_codec_write_cache(codec
, nid
, 0, AC_VERB_SET_CONNECT_SEL
,
3328 imux
->items
[idx
].index
);
3332 EXPORT_SYMBOL_HDA(snd_hda_input_mux_put
);
3336 * Multi-channel / digital-out PCM helper functions
3339 /* setup SPDIF output stream */
3340 static void setup_dig_out_stream(struct hda_codec
*codec
, hda_nid_t nid
,
3341 unsigned int stream_tag
, unsigned int format
)
3343 /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */
3344 if (codec
->spdif_status_reset
&& (codec
->spdif_ctls
& AC_DIG1_ENABLE
))
3345 set_dig_out_convert(codec
, nid
,
3346 codec
->spdif_ctls
& ~AC_DIG1_ENABLE
& 0xff,
3348 snd_hda_codec_setup_stream(codec
, nid
, stream_tag
, 0, format
);
3349 if (codec
->slave_dig_outs
) {
3351 for (d
= codec
->slave_dig_outs
; *d
; d
++)
3352 snd_hda_codec_setup_stream(codec
, *d
, stream_tag
, 0,
3355 /* turn on again (if needed) */
3356 if (codec
->spdif_status_reset
&& (codec
->spdif_ctls
& AC_DIG1_ENABLE
))
3357 set_dig_out_convert(codec
, nid
,
3358 codec
->spdif_ctls
& 0xff, -1);
3361 static void cleanup_dig_out_stream(struct hda_codec
*codec
, hda_nid_t nid
)
3363 snd_hda_codec_cleanup_stream(codec
, nid
);
3364 if (codec
->slave_dig_outs
) {
3366 for (d
= codec
->slave_dig_outs
; *d
; d
++)
3367 snd_hda_codec_cleanup_stream(codec
, *d
);
3372 * open the digital out in the exclusive mode
3374 int snd_hda_multi_out_dig_open(struct hda_codec
*codec
,
3375 struct hda_multi_out
*mout
)
3377 mutex_lock(&codec
->spdif_mutex
);
3378 if (mout
->dig_out_used
== HDA_DIG_ANALOG_DUP
)
3379 /* already opened as analog dup; reset it once */
3380 cleanup_dig_out_stream(codec
, mout
->dig_out_nid
);
3381 mout
->dig_out_used
= HDA_DIG_EXCLUSIVE
;
3382 mutex_unlock(&codec
->spdif_mutex
);
3385 EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_open
);
3387 int snd_hda_multi_out_dig_prepare(struct hda_codec
*codec
,
3388 struct hda_multi_out
*mout
,
3389 unsigned int stream_tag
,
3390 unsigned int format
,
3391 struct snd_pcm_substream
*substream
)
3393 mutex_lock(&codec
->spdif_mutex
);
3394 setup_dig_out_stream(codec
, mout
->dig_out_nid
, stream_tag
, format
);
3395 mutex_unlock(&codec
->spdif_mutex
);
3398 EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_prepare
);
3400 int snd_hda_multi_out_dig_cleanup(struct hda_codec
*codec
,
3401 struct hda_multi_out
*mout
)
3403 mutex_lock(&codec
->spdif_mutex
);
3404 cleanup_dig_out_stream(codec
, mout
->dig_out_nid
);
3405 mutex_unlock(&codec
->spdif_mutex
);
3408 EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_cleanup
);
3411 * release the digital out
3413 int snd_hda_multi_out_dig_close(struct hda_codec
*codec
,
3414 struct hda_multi_out
*mout
)
3416 mutex_lock(&codec
->spdif_mutex
);
3417 mout
->dig_out_used
= 0;
3418 mutex_unlock(&codec
->spdif_mutex
);
3421 EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_close
);
3424 * set up more restrictions for analog out
3426 int snd_hda_multi_out_analog_open(struct hda_codec
*codec
,
3427 struct hda_multi_out
*mout
,
3428 struct snd_pcm_substream
*substream
,
3429 struct hda_pcm_stream
*hinfo
)
3431 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
3432 runtime
->hw
.channels_max
= mout
->max_channels
;
3433 if (mout
->dig_out_nid
) {
3434 if (!mout
->analog_rates
) {
3435 mout
->analog_rates
= hinfo
->rates
;
3436 mout
->analog_formats
= hinfo
->formats
;
3437 mout
->analog_maxbps
= hinfo
->maxbps
;
3439 runtime
->hw
.rates
= mout
->analog_rates
;
3440 runtime
->hw
.formats
= mout
->analog_formats
;
3441 hinfo
->maxbps
= mout
->analog_maxbps
;
3443 if (!mout
->spdif_rates
) {
3444 snd_hda_query_supported_pcm(codec
, mout
->dig_out_nid
,
3446 &mout
->spdif_formats
,
3447 &mout
->spdif_maxbps
);
3449 mutex_lock(&codec
->spdif_mutex
);
3450 if (mout
->share_spdif
) {
3451 runtime
->hw
.rates
&= mout
->spdif_rates
;
3452 runtime
->hw
.formats
&= mout
->spdif_formats
;
3453 if (mout
->spdif_maxbps
< hinfo
->maxbps
)
3454 hinfo
->maxbps
= mout
->spdif_maxbps
;
3456 mutex_unlock(&codec
->spdif_mutex
);
3458 return snd_pcm_hw_constraint_step(substream
->runtime
, 0,
3459 SNDRV_PCM_HW_PARAM_CHANNELS
, 2);
3461 EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_open
);
3464 * set up the i/o for analog out
3465 * when the digital out is available, copy the front out to digital out, too.
3467 int snd_hda_multi_out_analog_prepare(struct hda_codec
*codec
,
3468 struct hda_multi_out
*mout
,
3469 unsigned int stream_tag
,
3470 unsigned int format
,
3471 struct snd_pcm_substream
*substream
)
3473 hda_nid_t
*nids
= mout
->dac_nids
;
3474 int chs
= substream
->runtime
->channels
;
3477 mutex_lock(&codec
->spdif_mutex
);
3478 if (mout
->dig_out_nid
&& mout
->share_spdif
&&
3479 mout
->dig_out_used
!= HDA_DIG_EXCLUSIVE
) {
3481 snd_hda_is_supported_format(codec
, mout
->dig_out_nid
,
3483 !(codec
->spdif_status
& IEC958_AES0_NONAUDIO
)) {
3484 mout
->dig_out_used
= HDA_DIG_ANALOG_DUP
;
3485 setup_dig_out_stream(codec
, mout
->dig_out_nid
,
3486 stream_tag
, format
);
3488 mout
->dig_out_used
= 0;
3489 cleanup_dig_out_stream(codec
, mout
->dig_out_nid
);
3492 mutex_unlock(&codec
->spdif_mutex
);
3495 snd_hda_codec_setup_stream(codec
, nids
[HDA_FRONT
], stream_tag
,
3497 if (!mout
->no_share_stream
&&
3498 mout
->hp_nid
&& mout
->hp_nid
!= nids
[HDA_FRONT
])
3499 /* headphone out will just decode front left/right (stereo) */
3500 snd_hda_codec_setup_stream(codec
, mout
->hp_nid
, stream_tag
,
3502 /* extra outputs copied from front */
3503 for (i
= 0; i
< ARRAY_SIZE(mout
->extra_out_nid
); i
++)
3504 if (!mout
->no_share_stream
&& mout
->extra_out_nid
[i
])
3505 snd_hda_codec_setup_stream(codec
,
3506 mout
->extra_out_nid
[i
],
3507 stream_tag
, 0, format
);
3510 for (i
= 1; i
< mout
->num_dacs
; i
++) {
3511 if (chs
>= (i
+ 1) * 2) /* independent out */
3512 snd_hda_codec_setup_stream(codec
, nids
[i
], stream_tag
,
3514 else if (!mout
->no_share_stream
) /* copy front */
3515 snd_hda_codec_setup_stream(codec
, nids
[i
], stream_tag
,
3520 EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_prepare
);
3523 * clean up the setting for analog out
3525 int snd_hda_multi_out_analog_cleanup(struct hda_codec
*codec
,
3526 struct hda_multi_out
*mout
)
3528 hda_nid_t
*nids
= mout
->dac_nids
;
3531 for (i
= 0; i
< mout
->num_dacs
; i
++)
3532 snd_hda_codec_cleanup_stream(codec
, nids
[i
]);
3534 snd_hda_codec_cleanup_stream(codec
, mout
->hp_nid
);
3535 for (i
= 0; i
< ARRAY_SIZE(mout
->extra_out_nid
); i
++)
3536 if (mout
->extra_out_nid
[i
])
3537 snd_hda_codec_cleanup_stream(codec
,
3538 mout
->extra_out_nid
[i
]);
3539 mutex_lock(&codec
->spdif_mutex
);
3540 if (mout
->dig_out_nid
&& mout
->dig_out_used
== HDA_DIG_ANALOG_DUP
) {
3541 cleanup_dig_out_stream(codec
, mout
->dig_out_nid
);
3542 mout
->dig_out_used
= 0;
3544 mutex_unlock(&codec
->spdif_mutex
);
3547 EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_cleanup
);
3550 * Helper for automatic pin configuration
3553 static int is_in_nid_list(hda_nid_t nid
, hda_nid_t
*list
)
3555 for (; *list
; list
++)
3563 * Sort an associated group of pins according to their sequence numbers.
3565 static void sort_pins_by_sequence(hda_nid_t
* pins
, short * sequences
,
3572 for (i
= 0; i
< num_pins
; i
++) {
3573 for (j
= i
+ 1; j
< num_pins
; j
++) {
3574 if (sequences
[i
] > sequences
[j
]) {
3576 sequences
[i
] = sequences
[j
];
3588 * Parse all pin widgets and store the useful pin nids to cfg
3590 * The number of line-outs or any primary output is stored in line_outs,
3591 * and the corresponding output pins are assigned to line_out_pins[],
3592 * in the order of front, rear, CLFE, side, ...
3594 * If more extra outputs (speaker and headphone) are found, the pins are
3595 * assisnged to hp_pins[] and speaker_pins[], respectively. If no line-out jack
3596 * is detected, one of speaker of HP pins is assigned as the primary
3597 * output, i.e. to line_out_pins[0]. So, line_outs is always positive
3598 * if any analog output exists.
3600 * The analog input pins are assigned to input_pins array.
3601 * The digital input/output pins are assigned to dig_in_pin and dig_out_pin,
3604 int snd_hda_parse_pin_def_config(struct hda_codec
*codec
,
3605 struct auto_pin_cfg
*cfg
,
3606 hda_nid_t
*ignore_nids
)
3608 hda_nid_t nid
, end_nid
;
3609 short seq
, assoc_line_out
, assoc_speaker
;
3610 short sequences_line_out
[ARRAY_SIZE(cfg
->line_out_pins
)];
3611 short sequences_speaker
[ARRAY_SIZE(cfg
->speaker_pins
)];
3612 short sequences_hp
[ARRAY_SIZE(cfg
->hp_pins
)];
3614 memset(cfg
, 0, sizeof(*cfg
));
3616 memset(sequences_line_out
, 0, sizeof(sequences_line_out
));
3617 memset(sequences_speaker
, 0, sizeof(sequences_speaker
));
3618 memset(sequences_hp
, 0, sizeof(sequences_hp
));
3619 assoc_line_out
= assoc_speaker
= 0;
3621 end_nid
= codec
->start_nid
+ codec
->num_nodes
;
3622 for (nid
= codec
->start_nid
; nid
< end_nid
; nid
++) {
3623 unsigned int wid_caps
= get_wcaps(codec
, nid
);
3624 unsigned int wid_type
=
3625 (wid_caps
& AC_WCAP_TYPE
) >> AC_WCAP_TYPE_SHIFT
;
3626 unsigned int def_conf
;
3629 /* read all default configuration for pin complex */
3630 if (wid_type
!= AC_WID_PIN
)
3632 /* ignore the given nids (e.g. pc-beep returns error) */
3633 if (ignore_nids
&& is_in_nid_list(nid
, ignore_nids
))
3636 def_conf
= snd_hda_codec_get_pincfg(codec
, nid
);
3637 if (get_defcfg_connect(def_conf
) == AC_JACK_PORT_NONE
)
3639 loc
= get_defcfg_location(def_conf
);
3640 switch (get_defcfg_device(def_conf
)) {
3641 case AC_JACK_LINE_OUT
:
3642 seq
= get_defcfg_sequence(def_conf
);
3643 assoc
= get_defcfg_association(def_conf
);
3645 if (!(wid_caps
& AC_WCAP_STEREO
))
3646 if (!cfg
->mono_out_pin
)
3647 cfg
->mono_out_pin
= nid
;
3650 if (!assoc_line_out
)
3651 assoc_line_out
= assoc
;
3652 else if (assoc_line_out
!= assoc
)
3654 if (cfg
->line_outs
>= ARRAY_SIZE(cfg
->line_out_pins
))
3656 cfg
->line_out_pins
[cfg
->line_outs
] = nid
;
3657 sequences_line_out
[cfg
->line_outs
] = seq
;
3660 case AC_JACK_SPEAKER
:
3661 seq
= get_defcfg_sequence(def_conf
);
3662 assoc
= get_defcfg_association(def_conf
);
3665 if (! assoc_speaker
)
3666 assoc_speaker
= assoc
;
3667 else if (assoc_speaker
!= assoc
)
3669 if (cfg
->speaker_outs
>= ARRAY_SIZE(cfg
->speaker_pins
))
3671 cfg
->speaker_pins
[cfg
->speaker_outs
] = nid
;
3672 sequences_speaker
[cfg
->speaker_outs
] = seq
;
3673 cfg
->speaker_outs
++;
3675 case AC_JACK_HP_OUT
:
3676 seq
= get_defcfg_sequence(def_conf
);
3677 assoc
= get_defcfg_association(def_conf
);
3678 if (cfg
->hp_outs
>= ARRAY_SIZE(cfg
->hp_pins
))
3680 cfg
->hp_pins
[cfg
->hp_outs
] = nid
;
3681 sequences_hp
[cfg
->hp_outs
] = (assoc
<< 4) | seq
;
3684 case AC_JACK_MIC_IN
: {
3686 if (loc
== AC_JACK_LOC_FRONT
) {
3687 preferred
= AUTO_PIN_FRONT_MIC
;
3690 preferred
= AUTO_PIN_MIC
;
3691 alt
= AUTO_PIN_FRONT_MIC
;
3693 if (!cfg
->input_pins
[preferred
])
3694 cfg
->input_pins
[preferred
] = nid
;
3695 else if (!cfg
->input_pins
[alt
])
3696 cfg
->input_pins
[alt
] = nid
;
3699 case AC_JACK_LINE_IN
:
3700 if (loc
== AC_JACK_LOC_FRONT
)
3701 cfg
->input_pins
[AUTO_PIN_FRONT_LINE
] = nid
;
3703 cfg
->input_pins
[AUTO_PIN_LINE
] = nid
;
3706 cfg
->input_pins
[AUTO_PIN_CD
] = nid
;
3709 cfg
->input_pins
[AUTO_PIN_AUX
] = nid
;
3711 case AC_JACK_SPDIF_OUT
:
3712 case AC_JACK_DIG_OTHER_OUT
:
3713 if (cfg
->dig_outs
>= ARRAY_SIZE(cfg
->dig_out_pins
))
3715 cfg
->dig_out_pins
[cfg
->dig_outs
] = nid
;
3716 cfg
->dig_out_type
[cfg
->dig_outs
] =
3717 (loc
== AC_JACK_LOC_HDMI
) ?
3718 HDA_PCM_TYPE_HDMI
: HDA_PCM_TYPE_SPDIF
;
3721 case AC_JACK_SPDIF_IN
:
3722 case AC_JACK_DIG_OTHER_IN
:
3723 cfg
->dig_in_pin
= nid
;
3724 if (loc
== AC_JACK_LOC_HDMI
)
3725 cfg
->dig_in_type
= HDA_PCM_TYPE_HDMI
;
3727 cfg
->dig_in_type
= HDA_PCM_TYPE_SPDIF
;
3733 * If no line-out is defined but multiple HPs are found,
3734 * some of them might be the real line-outs.
3736 if (!cfg
->line_outs
&& cfg
->hp_outs
> 1) {
3738 while (i
< cfg
->hp_outs
) {
3739 /* The real HPs should have the sequence 0x0f */
3740 if ((sequences_hp
[i
] & 0x0f) == 0x0f) {
3744 /* Move it to the line-out table */
3745 cfg
->line_out_pins
[cfg
->line_outs
] = cfg
->hp_pins
[i
];
3746 sequences_line_out
[cfg
->line_outs
] = sequences_hp
[i
];
3749 memmove(cfg
->hp_pins
+ i
, cfg
->hp_pins
+ i
+ 1,
3750 sizeof(cfg
->hp_pins
[0]) * (cfg
->hp_outs
- i
));
3751 memmove(sequences_hp
+ i
- 1, sequences_hp
+ i
,
3752 sizeof(sequences_hp
[0]) * (cfg
->hp_outs
- i
));
3756 /* sort by sequence */
3757 sort_pins_by_sequence(cfg
->line_out_pins
, sequences_line_out
,
3759 sort_pins_by_sequence(cfg
->speaker_pins
, sequences_speaker
,
3761 sort_pins_by_sequence(cfg
->hp_pins
, sequences_hp
,
3764 /* if we have only one mic, make it AUTO_PIN_MIC */
3765 if (!cfg
->input_pins
[AUTO_PIN_MIC
] &&
3766 cfg
->input_pins
[AUTO_PIN_FRONT_MIC
]) {
3767 cfg
->input_pins
[AUTO_PIN_MIC
] =
3768 cfg
->input_pins
[AUTO_PIN_FRONT_MIC
];
3769 cfg
->input_pins
[AUTO_PIN_FRONT_MIC
] = 0;
3771 /* ditto for line-in */
3772 if (!cfg
->input_pins
[AUTO_PIN_LINE
] &&
3773 cfg
->input_pins
[AUTO_PIN_FRONT_LINE
]) {
3774 cfg
->input_pins
[AUTO_PIN_LINE
] =
3775 cfg
->input_pins
[AUTO_PIN_FRONT_LINE
];
3776 cfg
->input_pins
[AUTO_PIN_FRONT_LINE
] = 0;
3780 * FIX-UP: if no line-outs are detected, try to use speaker or HP pin
3781 * as a primary output
3783 if (!cfg
->line_outs
) {
3784 if (cfg
->speaker_outs
) {
3785 cfg
->line_outs
= cfg
->speaker_outs
;
3786 memcpy(cfg
->line_out_pins
, cfg
->speaker_pins
,
3787 sizeof(cfg
->speaker_pins
));
3788 cfg
->speaker_outs
= 0;
3789 memset(cfg
->speaker_pins
, 0, sizeof(cfg
->speaker_pins
));
3790 cfg
->line_out_type
= AUTO_PIN_SPEAKER_OUT
;
3791 } else if (cfg
->hp_outs
) {
3792 cfg
->line_outs
= cfg
->hp_outs
;
3793 memcpy(cfg
->line_out_pins
, cfg
->hp_pins
,
3794 sizeof(cfg
->hp_pins
));
3796 memset(cfg
->hp_pins
, 0, sizeof(cfg
->hp_pins
));
3797 cfg
->line_out_type
= AUTO_PIN_HP_OUT
;
3801 /* Reorder the surround channels
3802 * ALSA sequence is front/surr/clfe/side
3804 * 4-ch: front/surr => OK as it is
3805 * 6-ch: front/clfe/surr
3806 * 8-ch: front/clfe/rear/side|fc
3808 switch (cfg
->line_outs
) {
3811 nid
= cfg
->line_out_pins
[1];
3812 cfg
->line_out_pins
[1] = cfg
->line_out_pins
[2];
3813 cfg
->line_out_pins
[2] = nid
;
3818 * debug prints of the parsed results
3820 snd_printd("autoconfig: line_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
3821 cfg
->line_outs
, cfg
->line_out_pins
[0], cfg
->line_out_pins
[1],
3822 cfg
->line_out_pins
[2], cfg
->line_out_pins
[3],
3823 cfg
->line_out_pins
[4]);
3824 snd_printd(" speaker_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
3825 cfg
->speaker_outs
, cfg
->speaker_pins
[0],
3826 cfg
->speaker_pins
[1], cfg
->speaker_pins
[2],
3827 cfg
->speaker_pins
[3], cfg
->speaker_pins
[4]);
3828 snd_printd(" hp_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
3829 cfg
->hp_outs
, cfg
->hp_pins
[0],
3830 cfg
->hp_pins
[1], cfg
->hp_pins
[2],
3831 cfg
->hp_pins
[3], cfg
->hp_pins
[4]);
3832 snd_printd(" mono: mono_out=0x%x\n", cfg
->mono_out_pin
);
3834 snd_printd(" dig-out=0x%x/0x%x\n",
3835 cfg
->dig_out_pins
[0], cfg
->dig_out_pins
[1]);
3836 snd_printd(" inputs: mic=0x%x, fmic=0x%x, line=0x%x, fline=0x%x,"
3837 " cd=0x%x, aux=0x%x\n",
3838 cfg
->input_pins
[AUTO_PIN_MIC
],
3839 cfg
->input_pins
[AUTO_PIN_FRONT_MIC
],
3840 cfg
->input_pins
[AUTO_PIN_LINE
],
3841 cfg
->input_pins
[AUTO_PIN_FRONT_LINE
],
3842 cfg
->input_pins
[AUTO_PIN_CD
],
3843 cfg
->input_pins
[AUTO_PIN_AUX
]);
3844 if (cfg
->dig_in_pin
)
3845 snd_printd(" dig-in=0x%x\n", cfg
->dig_in_pin
);
3849 EXPORT_SYMBOL_HDA(snd_hda_parse_pin_def_config
);
3851 /* labels for input pins */
3852 const char *auto_pin_cfg_labels
[AUTO_PIN_LAST
] = {
3853 "Mic", "Front Mic", "Line", "Front Line", "CD", "Aux"
3855 EXPORT_SYMBOL_HDA(auto_pin_cfg_labels
);
3864 * snd_hda_suspend - suspend the codecs
3866 * @state: suspsend state
3868 * Returns 0 if successful.
3870 int snd_hda_suspend(struct hda_bus
*bus
, pm_message_t state
)
3872 struct hda_codec
*codec
;
3874 list_for_each_entry(codec
, &bus
->codec_list
, list
) {
3875 #ifdef CONFIG_SND_HDA_POWER_SAVE
3876 if (!codec
->power_on
)
3879 hda_call_codec_suspend(codec
);
3883 EXPORT_SYMBOL_HDA(snd_hda_suspend
);
3886 * snd_hda_resume - resume the codecs
3889 * Returns 0 if successful.
3891 * This fucntion is defined only when POWER_SAVE isn't set.
3892 * In the power-save mode, the codec is resumed dynamically.
3894 int snd_hda_resume(struct hda_bus
*bus
)
3896 struct hda_codec
*codec
;
3898 list_for_each_entry(codec
, &bus
->codec_list
, list
) {
3899 if (snd_hda_codec_needs_resume(codec
))
3900 hda_call_codec_resume(codec
);
3904 EXPORT_SYMBOL_HDA(snd_hda_resume
);
3905 #endif /* CONFIG_PM */
3911 /* get a new element from the given array
3912 * if it exceeds the pre-allocated array size, re-allocate the array
3914 void *snd_array_new(struct snd_array
*array
)
3916 if (array
->used
>= array
->alloced
) {
3917 int num
= array
->alloced
+ array
->alloc_align
;
3919 if (snd_BUG_ON(num
>= 4096))
3921 nlist
= kcalloc(num
+ 1, array
->elem_size
, GFP_KERNEL
);
3925 memcpy(nlist
, array
->list
,
3926 array
->elem_size
* array
->alloced
);
3929 array
->list
= nlist
;
3930 array
->alloced
= num
;
3932 return snd_array_elem(array
, array
->used
++);
3934 EXPORT_SYMBOL_HDA(snd_array_new
);
3936 /* free the given array elements */
3937 void snd_array_free(struct snd_array
*array
)
3944 EXPORT_SYMBOL_HDA(snd_array_free
);
3947 * used by hda_proc.c and hda_eld.c
3949 void snd_print_pcm_rates(int pcm
, char *buf
, int buflen
)
3951 static unsigned int rates
[] = {
3952 8000, 11025, 16000, 22050, 32000, 44100, 48000, 88200,
3953 96000, 176400, 192000, 384000
3957 for (i
= 0, j
= 0; i
< ARRAY_SIZE(rates
); i
++)
3959 j
+= snprintf(buf
+ j
, buflen
- j
, " %d", rates
[i
]);
3961 buf
[j
] = '\0'; /* necessary when j == 0 */
3963 EXPORT_SYMBOL_HDA(snd_print_pcm_rates
);
3965 void snd_print_pcm_bits(int pcm
, char *buf
, int buflen
)
3967 static unsigned int bits
[] = { 8, 16, 20, 24, 32 };
3970 for (i
= 0, j
= 0; i
< ARRAY_SIZE(bits
); i
++)
3971 if (pcm
& (AC_SUPPCM_BITS_8
<< i
))
3972 j
+= snprintf(buf
+ j
, buflen
- j
, " %d", bits
[i
]);
3974 buf
[j
] = '\0'; /* necessary when j == 0 */
3976 EXPORT_SYMBOL_HDA(snd_print_pcm_bits
);
3978 MODULE_DESCRIPTION("HDA codec core");
3979 MODULE_LICENSE("GPL");