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
23 #include <linux/init.h>
24 #include <linux/delay.h>
25 #include <linux/slab.h>
26 #include <linux/pci.h>
27 #include <linux/mutex.h>
28 #include <linux/module.h>
29 #include <sound/core.h>
30 #include "hda_codec.h"
31 #include <sound/asoundef.h>
32 #include <sound/tlv.h>
33 #include <sound/initval.h>
34 #include <sound/jack.h>
35 #include "hda_local.h"
38 #include <sound/hda_hwdep.h>
40 #define CREATE_TRACE_POINTS
41 #include "hda_trace.h"
44 * vendor / preset table
47 struct hda_vendor_id
{
52 /* codec vendor labels */
53 static struct hda_vendor_id hda_vendor_ids
[] = {
55 { 0x1013, "Cirrus Logic" },
56 { 0x1057, "Motorola" },
57 { 0x1095, "Silicon Image" },
59 { 0x10ec, "Realtek" },
60 { 0x1102, "Creative" },
64 { 0x11d4, "Analog Devices" },
65 { 0x13f6, "C-Media" },
66 { 0x14f1, "Conexant" },
67 { 0x17e8, "Chrontel" },
69 { 0x1aec, "Wolfson Microelectronics" },
70 { 0x434d, "C-Media" },
72 { 0x8384, "SigmaTel" },
76 static DEFINE_MUTEX(preset_mutex
);
77 static LIST_HEAD(hda_preset_tables
);
79 int snd_hda_add_codec_preset(struct hda_codec_preset_list
*preset
)
81 mutex_lock(&preset_mutex
);
82 list_add_tail(&preset
->list
, &hda_preset_tables
);
83 mutex_unlock(&preset_mutex
);
86 EXPORT_SYMBOL_HDA(snd_hda_add_codec_preset
);
88 int snd_hda_delete_codec_preset(struct hda_codec_preset_list
*preset
)
90 mutex_lock(&preset_mutex
);
91 list_del(&preset
->list
);
92 mutex_unlock(&preset_mutex
);
95 EXPORT_SYMBOL_HDA(snd_hda_delete_codec_preset
);
97 #ifdef CONFIG_SND_HDA_POWER_SAVE
98 static void hda_power_work(struct work_struct
*work
);
99 static void hda_keep_power_on(struct hda_codec
*codec
);
100 #define hda_codec_is_power_on(codec) ((codec)->power_on)
102 static inline void hda_keep_power_on(struct hda_codec
*codec
) {}
103 #define hda_codec_is_power_on(codec) 1
107 * snd_hda_get_jack_location - Give a location string of the jack
108 * @cfg: pin default config value
110 * Parse the pin default config value and returns the string of the
111 * jack location, e.g. "Rear", "Front", etc.
113 const char *snd_hda_get_jack_location(u32 cfg
)
115 static char *bases
[7] = {
116 "N/A", "Rear", "Front", "Left", "Right", "Top", "Bottom",
118 static unsigned char specials_idx
[] = {
123 static char *specials
[] = {
124 "Rear Panel", "Drive Bar",
125 "Riser", "HDMI", "ATAPI",
126 "Mobile-In", "Mobile-Out"
129 cfg
= (cfg
& AC_DEFCFG_LOCATION
) >> AC_DEFCFG_LOCATION_SHIFT
;
130 if ((cfg
& 0x0f) < 7)
131 return bases
[cfg
& 0x0f];
132 for (i
= 0; i
< ARRAY_SIZE(specials_idx
); i
++) {
133 if (cfg
== specials_idx
[i
])
138 EXPORT_SYMBOL_HDA(snd_hda_get_jack_location
);
141 * snd_hda_get_jack_connectivity - Give a connectivity string of the jack
142 * @cfg: pin default config value
144 * Parse the pin default config value and returns the string of the
145 * jack connectivity, i.e. external or internal connection.
147 const char *snd_hda_get_jack_connectivity(u32 cfg
)
149 static char *jack_locations
[4] = { "Ext", "Int", "Sep", "Oth" };
151 return jack_locations
[(cfg
>> (AC_DEFCFG_LOCATION_SHIFT
+ 4)) & 3];
153 EXPORT_SYMBOL_HDA(snd_hda_get_jack_connectivity
);
156 * snd_hda_get_jack_type - Give a type string of the jack
157 * @cfg: pin default config value
159 * Parse the pin default config value and returns the string of the
160 * jack type, i.e. the purpose of the jack, such as Line-Out or CD.
162 const char *snd_hda_get_jack_type(u32 cfg
)
164 static char *jack_types
[16] = {
165 "Line Out", "Speaker", "HP Out", "CD",
166 "SPDIF Out", "Digital Out", "Modem Line", "Modem Hand",
167 "Line In", "Aux", "Mic", "Telephony",
168 "SPDIF In", "Digitial In", "Reserved", "Other"
171 return jack_types
[(cfg
& AC_DEFCFG_DEVICE
)
172 >> AC_DEFCFG_DEVICE_SHIFT
];
174 EXPORT_SYMBOL_HDA(snd_hda_get_jack_type
);
177 * Compose a 32bit command word to be sent to the HD-audio controller
179 static inline unsigned int
180 make_codec_cmd(struct hda_codec
*codec
, hda_nid_t nid
, int direct
,
181 unsigned int verb
, unsigned int parm
)
185 if ((codec
->addr
& ~0xf) || (direct
& ~1) || (nid
& ~0x7f) ||
186 (verb
& ~0xfff) || (parm
& ~0xffff)) {
187 printk(KERN_ERR
"hda-codec: out of range cmd %x:%x:%x:%x:%x\n",
188 codec
->addr
, direct
, nid
, verb
, parm
);
192 val
= (u32
)codec
->addr
<< 28;
193 val
|= (u32
)direct
<< 27;
194 val
|= (u32
)nid
<< 20;
201 * Send and receive a verb
203 static int codec_exec_verb(struct hda_codec
*codec
, unsigned int cmd
,
206 struct hda_bus
*bus
= codec
->bus
;
215 snd_hda_power_up(codec
);
216 mutex_lock(&bus
->cmd_mutex
);
217 trace_hda_send_cmd(codec
, cmd
);
218 err
= bus
->ops
.command(bus
, cmd
);
220 *res
= bus
->ops
.get_response(bus
, codec
->addr
);
221 trace_hda_get_response(codec
, *res
);
223 mutex_unlock(&bus
->cmd_mutex
);
224 snd_hda_power_down(codec
);
225 if (res
&& *res
== -1 && bus
->rirb_error
) {
226 if (bus
->response_reset
) {
227 snd_printd("hda_codec: resetting BUS due to "
228 "fatal communication error\n");
229 trace_hda_bus_reset(bus
);
230 bus
->ops
.bus_reset(bus
);
234 /* clear reset-flag when the communication gets recovered */
236 bus
->response_reset
= 0;
241 * snd_hda_codec_read - send a command and get the response
242 * @codec: the HDA codec
243 * @nid: NID to send the command
244 * @direct: direct flag
245 * @verb: the verb to send
246 * @parm: the parameter for the verb
248 * Send a single command and read the corresponding response.
250 * Returns the obtained response value, or -1 for an error.
252 unsigned int snd_hda_codec_read(struct hda_codec
*codec
, hda_nid_t nid
,
254 unsigned int verb
, unsigned int parm
)
256 unsigned cmd
= make_codec_cmd(codec
, nid
, direct
, verb
, parm
);
258 if (codec_exec_verb(codec
, cmd
, &res
))
262 EXPORT_SYMBOL_HDA(snd_hda_codec_read
);
265 * snd_hda_codec_write - send a single command without waiting for response
266 * @codec: the HDA codec
267 * @nid: NID to send the command
268 * @direct: direct flag
269 * @verb: the verb to send
270 * @parm: the parameter for the verb
272 * Send a single command without waiting for response.
274 * Returns 0 if successful, or a negative error code.
276 int snd_hda_codec_write(struct hda_codec
*codec
, hda_nid_t nid
, int direct
,
277 unsigned int verb
, unsigned int parm
)
279 unsigned int cmd
= make_codec_cmd(codec
, nid
, direct
, verb
, parm
);
281 return codec_exec_verb(codec
, cmd
,
282 codec
->bus
->sync_write
? &res
: NULL
);
284 EXPORT_SYMBOL_HDA(snd_hda_codec_write
);
287 * snd_hda_sequence_write - sequence writes
288 * @codec: the HDA codec
289 * @seq: VERB array to send
291 * Send the commands sequentially from the given array.
292 * The array must be terminated with NID=0.
294 void snd_hda_sequence_write(struct hda_codec
*codec
, const struct hda_verb
*seq
)
296 for (; seq
->nid
; seq
++)
297 snd_hda_codec_write(codec
, seq
->nid
, 0, seq
->verb
, seq
->param
);
299 EXPORT_SYMBOL_HDA(snd_hda_sequence_write
);
302 * snd_hda_get_sub_nodes - get the range of sub nodes
303 * @codec: the HDA codec
305 * @start_id: the pointer to store the start NID
307 * Parse the NID and store the start NID of its sub-nodes.
308 * Returns the number of sub-nodes.
310 int snd_hda_get_sub_nodes(struct hda_codec
*codec
, hda_nid_t nid
,
315 parm
= snd_hda_param_read(codec
, nid
, AC_PAR_NODE_COUNT
);
318 *start_id
= (parm
>> 16) & 0x7fff;
319 return (int)(parm
& 0x7fff);
321 EXPORT_SYMBOL_HDA(snd_hda_get_sub_nodes
);
323 /* look up the cached results */
324 static hda_nid_t
*lookup_conn_list(struct snd_array
*array
, hda_nid_t nid
)
327 for (i
= 0; i
< array
->used
; ) {
328 hda_nid_t
*p
= snd_array_elem(array
, i
);
337 /* read the connection and add to the cache */
338 static int read_and_add_raw_conns(struct hda_codec
*codec
, hda_nid_t nid
)
340 hda_nid_t list
[HDA_MAX_CONNECTIONS
];
343 len
= snd_hda_get_raw_connections(codec
, nid
, list
, ARRAY_SIZE(list
));
346 return snd_hda_override_conn_list(codec
, nid
, len
, list
);
350 * snd_hda_get_connections - copy connection list
351 * @codec: the HDA codec
353 * @conn_list: connection list array; when NULL, checks only the size
354 * @max_conns: max. number of connections to store
356 * Parses the connection list of the given widget and stores the list
359 * Returns the number of connections, or a negative error code.
361 int snd_hda_get_connections(struct hda_codec
*codec
, hda_nid_t nid
,
362 hda_nid_t
*conn_list
, int max_conns
)
364 struct snd_array
*array
= &codec
->conn_lists
;
370 mutex_lock(&codec
->hash_mutex
);
372 /* if the connection-list is already cached, read it */
373 p
= lookup_conn_list(array
, nid
);
376 if (conn_list
&& len
> max_conns
) {
377 snd_printk(KERN_ERR
"hda_codec: "
378 "Too many connections %d for NID 0x%x\n",
380 mutex_unlock(&codec
->hash_mutex
);
383 if (conn_list
&& len
)
384 memcpy(conn_list
, p
+ 2, len
* sizeof(hda_nid_t
));
386 mutex_unlock(&codec
->hash_mutex
);
389 if (snd_BUG_ON(added
))
392 len
= read_and_add_raw_conns(codec
, nid
);
398 EXPORT_SYMBOL_HDA(snd_hda_get_connections
);
401 * snd_hda_get_raw_connections - copy connection list without cache
402 * @codec: the HDA codec
404 * @conn_list: connection list array
405 * @max_conns: max. number of connections to store
407 * Like snd_hda_get_connections(), copy the connection list but without
408 * checking through the connection-list cache.
409 * Currently called only from hda_proc.c, so not exported.
411 int snd_hda_get_raw_connections(struct hda_codec
*codec
, hda_nid_t nid
,
412 hda_nid_t
*conn_list
, int max_conns
)
415 int i
, conn_len
, conns
;
416 unsigned int shift
, num_elems
, mask
;
420 if (snd_BUG_ON(!conn_list
|| max_conns
<= 0))
423 wcaps
= get_wcaps(codec
, nid
);
424 if (!(wcaps
& AC_WCAP_CONN_LIST
) &&
425 get_wcaps_type(wcaps
) != AC_WID_VOL_KNB
)
428 parm
= snd_hda_param_read(codec
, nid
, AC_PAR_CONNLIST_LEN
);
429 if (parm
& AC_CLIST_LONG
) {
438 conn_len
= parm
& AC_CLIST_LENGTH
;
439 mask
= (1 << (shift
-1)) - 1;
442 return 0; /* no connection */
445 /* single connection */
446 parm
= snd_hda_codec_read(codec
, nid
, 0,
447 AC_VERB_GET_CONNECT_LIST
, 0);
448 if (parm
== -1 && codec
->bus
->rirb_error
)
450 conn_list
[0] = parm
& mask
;
454 /* multi connection */
457 for (i
= 0; i
< conn_len
; i
++) {
461 if (i
% num_elems
== 0) {
462 parm
= snd_hda_codec_read(codec
, nid
, 0,
463 AC_VERB_GET_CONNECT_LIST
, i
);
464 if (parm
== -1 && codec
->bus
->rirb_error
)
467 range_val
= !!(parm
& (1 << (shift
-1))); /* ranges */
470 snd_printk(KERN_WARNING
"hda_codec: "
471 "invalid CONNECT_LIST verb %x[%i]:%x\n",
477 /* ranges between the previous and this one */
478 if (!prev_nid
|| prev_nid
>= val
) {
479 snd_printk(KERN_WARNING
"hda_codec: "
480 "invalid dep_range_val %x:%x\n",
484 for (n
= prev_nid
+ 1; n
<= val
; n
++) {
485 if (conns
>= max_conns
) {
486 snd_printk(KERN_ERR
"hda_codec: "
487 "Too many connections %d for NID 0x%x\n",
491 conn_list
[conns
++] = n
;
494 if (conns
>= max_conns
) {
495 snd_printk(KERN_ERR
"hda_codec: "
496 "Too many connections %d for NID 0x%x\n",
500 conn_list
[conns
++] = val
;
507 static bool add_conn_list(struct snd_array
*array
, hda_nid_t nid
)
509 hda_nid_t
*p
= snd_array_new(array
);
517 * snd_hda_override_conn_list - add/modify the connection-list to cache
518 * @codec: the HDA codec
520 * @len: number of connection list entries
521 * @list: the list of connection entries
523 * Add or modify the given connection-list to the cache. If the corresponding
524 * cache already exists, invalidate it and append a new one.
526 * Returns zero or a negative error code.
528 int snd_hda_override_conn_list(struct hda_codec
*codec
, hda_nid_t nid
, int len
,
529 const hda_nid_t
*list
)
531 struct snd_array
*array
= &codec
->conn_lists
;
535 mutex_lock(&codec
->hash_mutex
);
536 p
= lookup_conn_list(array
, nid
);
538 *p
= -1; /* invalidate the old entry */
540 old_used
= array
->used
;
541 if (!add_conn_list(array
, nid
) || !add_conn_list(array
, len
))
543 for (i
= 0; i
< len
; i
++)
544 if (!add_conn_list(array
, list
[i
]))
546 mutex_unlock(&codec
->hash_mutex
);
550 array
->used
= old_used
;
551 mutex_unlock(&codec
->hash_mutex
);
554 EXPORT_SYMBOL_HDA(snd_hda_override_conn_list
);
557 * snd_hda_get_conn_index - get the connection index of the given NID
558 * @codec: the HDA codec
559 * @mux: NID containing the list
560 * @nid: NID to select
561 * @recursive: 1 when searching NID recursively, otherwise 0
563 * Parses the connection list of the widget @mux and checks whether the
564 * widget @nid is present. If it is, return the connection index.
565 * Otherwise it returns -1.
567 int snd_hda_get_conn_index(struct hda_codec
*codec
, hda_nid_t mux
,
568 hda_nid_t nid
, int recursive
)
570 hda_nid_t conn
[HDA_MAX_NUM_INPUTS
];
573 nums
= snd_hda_get_connections(codec
, mux
, conn
, ARRAY_SIZE(conn
));
574 for (i
= 0; i
< nums
; i
++)
580 snd_printd("hda_codec: too deep connection for 0x%x\n", nid
);
584 for (i
= 0; i
< nums
; i
++) {
585 unsigned int type
= get_wcaps_type(get_wcaps(codec
, conn
[i
]));
586 if (type
== AC_WID_PIN
|| type
== AC_WID_AUD_OUT
)
588 if (snd_hda_get_conn_index(codec
, conn
[i
], nid
, recursive
) >= 0)
593 EXPORT_SYMBOL_HDA(snd_hda_get_conn_index
);
596 * snd_hda_queue_unsol_event - add an unsolicited event to queue
598 * @res: unsolicited event (lower 32bit of RIRB entry)
599 * @res_ex: codec addr and flags (upper 32bit or RIRB entry)
601 * Adds the given event to the queue. The events are processed in
602 * the workqueue asynchronously. Call this function in the interrupt
603 * hanlder when RIRB receives an unsolicited event.
605 * Returns 0 if successful, or a negative error code.
607 int snd_hda_queue_unsol_event(struct hda_bus
*bus
, u32 res
, u32 res_ex
)
609 struct hda_bus_unsolicited
*unsol
;
612 trace_hda_unsol_event(bus
, res
, res_ex
);
617 wp
= (unsol
->wp
+ 1) % HDA_UNSOL_QUEUE_SIZE
;
621 unsol
->queue
[wp
] = res
;
622 unsol
->queue
[wp
+ 1] = res_ex
;
624 queue_work(bus
->workq
, &unsol
->work
);
628 EXPORT_SYMBOL_HDA(snd_hda_queue_unsol_event
);
631 * process queued unsolicited events
633 static void process_unsol_events(struct work_struct
*work
)
635 struct hda_bus_unsolicited
*unsol
=
636 container_of(work
, struct hda_bus_unsolicited
, work
);
637 struct hda_bus
*bus
= unsol
->bus
;
638 struct hda_codec
*codec
;
639 unsigned int rp
, caddr
, res
;
641 while (unsol
->rp
!= unsol
->wp
) {
642 rp
= (unsol
->rp
+ 1) % HDA_UNSOL_QUEUE_SIZE
;
645 res
= unsol
->queue
[rp
];
646 caddr
= unsol
->queue
[rp
+ 1];
647 if (!(caddr
& (1 << 4))) /* no unsolicited event? */
649 codec
= bus
->caddr_tbl
[caddr
& 0x0f];
650 if (codec
&& codec
->patch_ops
.unsol_event
)
651 codec
->patch_ops
.unsol_event(codec
, res
);
656 * initialize unsolicited queue
658 static int init_unsol_queue(struct hda_bus
*bus
)
660 struct hda_bus_unsolicited
*unsol
;
662 if (bus
->unsol
) /* already initialized */
665 unsol
= kzalloc(sizeof(*unsol
), GFP_KERNEL
);
667 snd_printk(KERN_ERR
"hda_codec: "
668 "can't allocate unsolicited queue\n");
671 INIT_WORK(&unsol
->work
, process_unsol_events
);
680 static void snd_hda_codec_free(struct hda_codec
*codec
);
682 static int snd_hda_bus_free(struct hda_bus
*bus
)
684 struct hda_codec
*codec
, *n
;
689 flush_workqueue(bus
->workq
);
692 list_for_each_entry_safe(codec
, n
, &bus
->codec_list
, list
) {
693 snd_hda_codec_free(codec
);
695 if (bus
->ops
.private_free
)
696 bus
->ops
.private_free(bus
);
698 destroy_workqueue(bus
->workq
);
703 static int snd_hda_bus_dev_free(struct snd_device
*device
)
705 struct hda_bus
*bus
= device
->device_data
;
707 return snd_hda_bus_free(bus
);
710 #ifdef CONFIG_SND_HDA_HWDEP
711 static int snd_hda_bus_dev_register(struct snd_device
*device
)
713 struct hda_bus
*bus
= device
->device_data
;
714 struct hda_codec
*codec
;
715 list_for_each_entry(codec
, &bus
->codec_list
, list
) {
716 snd_hda_hwdep_add_sysfs(codec
);
717 snd_hda_hwdep_add_power_sysfs(codec
);
722 #define snd_hda_bus_dev_register NULL
726 * snd_hda_bus_new - create a HDA bus
727 * @card: the card entry
728 * @temp: the template for hda_bus information
729 * @busp: the pointer to store the created bus instance
731 * Returns 0 if successful, or a negative error code.
733 int /*__devinit*/ snd_hda_bus_new(struct snd_card
*card
,
734 const struct hda_bus_template
*temp
,
735 struct hda_bus
**busp
)
739 static struct snd_device_ops dev_ops
= {
740 .dev_register
= snd_hda_bus_dev_register
,
741 .dev_free
= snd_hda_bus_dev_free
,
744 if (snd_BUG_ON(!temp
))
746 if (snd_BUG_ON(!temp
->ops
.command
|| !temp
->ops
.get_response
))
752 bus
= kzalloc(sizeof(*bus
), GFP_KERNEL
);
754 snd_printk(KERN_ERR
"can't allocate struct hda_bus\n");
759 bus
->private_data
= temp
->private_data
;
760 bus
->pci
= temp
->pci
;
761 bus
->modelname
= temp
->modelname
;
762 bus
->power_save
= temp
->power_save
;
763 bus
->ops
= temp
->ops
;
765 mutex_init(&bus
->cmd_mutex
);
766 mutex_init(&bus
->prepare_mutex
);
767 INIT_LIST_HEAD(&bus
->codec_list
);
769 snprintf(bus
->workq_name
, sizeof(bus
->workq_name
),
770 "hd-audio%d", card
->number
);
771 bus
->workq
= create_singlethread_workqueue(bus
->workq_name
);
773 snd_printk(KERN_ERR
"cannot create workqueue %s\n",
779 err
= snd_device_new(card
, SNDRV_DEV_BUS
, bus
, &dev_ops
);
781 snd_hda_bus_free(bus
);
788 EXPORT_SYMBOL_HDA(snd_hda_bus_new
);
790 #ifdef CONFIG_SND_HDA_GENERIC
791 #define is_generic_config(codec) \
792 (codec->modelname && !strcmp(codec->modelname, "generic"))
794 #define is_generic_config(codec) 0
798 #define HDA_MODREQ_MAX_COUNT 2 /* two request_modules()'s */
800 #define HDA_MODREQ_MAX_COUNT 0 /* all presets are statically linked */
804 * find a matching codec preset
806 static const struct hda_codec_preset
*
807 find_codec_preset(struct hda_codec
*codec
)
809 struct hda_codec_preset_list
*tbl
;
810 const struct hda_codec_preset
*preset
;
811 int mod_requested
= 0;
813 if (is_generic_config(codec
))
814 return NULL
; /* use the generic parser */
817 mutex_lock(&preset_mutex
);
818 list_for_each_entry(tbl
, &hda_preset_tables
, list
) {
819 if (!try_module_get(tbl
->owner
)) {
820 snd_printk(KERN_ERR
"hda_codec: cannot module_get\n");
823 for (preset
= tbl
->preset
; preset
->id
; preset
++) {
824 u32 mask
= preset
->mask
;
825 if (preset
->afg
&& preset
->afg
!= codec
->afg
)
827 if (preset
->mfg
&& preset
->mfg
!= codec
->mfg
)
831 if (preset
->id
== (codec
->vendor_id
& mask
) &&
833 preset
->rev
== codec
->revision_id
)) {
834 mutex_unlock(&preset_mutex
);
835 codec
->owner
= tbl
->owner
;
839 module_put(tbl
->owner
);
841 mutex_unlock(&preset_mutex
);
843 if (mod_requested
< HDA_MODREQ_MAX_COUNT
) {
846 snprintf(name
, sizeof(name
), "snd-hda-codec-id:%08x",
849 snprintf(name
, sizeof(name
), "snd-hda-codec-id:%04x*",
850 (codec
->vendor_id
>> 16) & 0xffff);
851 request_module(name
);
859 * get_codec_name - store the codec name
861 static int get_codec_name(struct hda_codec
*codec
)
863 const struct hda_vendor_id
*c
;
864 const char *vendor
= NULL
;
865 u16 vendor_id
= codec
->vendor_id
>> 16;
868 if (codec
->vendor_name
)
871 for (c
= hda_vendor_ids
; c
->id
; c
++) {
872 if (c
->id
== vendor_id
) {
878 sprintf(tmp
, "Generic %04x", vendor_id
);
881 codec
->vendor_name
= kstrdup(vendor
, GFP_KERNEL
);
882 if (!codec
->vendor_name
)
886 if (codec
->chip_name
)
889 if (codec
->preset
&& codec
->preset
->name
)
890 codec
->chip_name
= kstrdup(codec
->preset
->name
, GFP_KERNEL
);
892 sprintf(tmp
, "ID %x", codec
->vendor_id
& 0xffff);
893 codec
->chip_name
= kstrdup(tmp
, GFP_KERNEL
);
895 if (!codec
->chip_name
)
901 * look for an AFG and MFG nodes
903 static void /*__devinit*/ setup_fg_nodes(struct hda_codec
*codec
)
905 int i
, total_nodes
, function_id
;
908 total_nodes
= snd_hda_get_sub_nodes(codec
, AC_NODE_ROOT
, &nid
);
909 for (i
= 0; i
< total_nodes
; i
++, nid
++) {
910 function_id
= snd_hda_param_read(codec
, nid
,
911 AC_PAR_FUNCTION_TYPE
);
912 switch (function_id
& 0xff) {
913 case AC_GRP_AUDIO_FUNCTION
:
915 codec
->afg_function_id
= function_id
& 0xff;
916 codec
->afg_unsol
= (function_id
>> 8) & 1;
918 case AC_GRP_MODEM_FUNCTION
:
920 codec
->mfg_function_id
= function_id
& 0xff;
921 codec
->mfg_unsol
= (function_id
>> 8) & 1;
930 * read widget caps for each widget and store in cache
932 static int read_widget_caps(struct hda_codec
*codec
, hda_nid_t fg_node
)
937 codec
->num_nodes
= snd_hda_get_sub_nodes(codec
, fg_node
,
939 codec
->wcaps
= kmalloc(codec
->num_nodes
* 4, GFP_KERNEL
);
942 nid
= codec
->start_nid
;
943 for (i
= 0; i
< codec
->num_nodes
; i
++, nid
++)
944 codec
->wcaps
[i
] = snd_hda_param_read(codec
, nid
,
945 AC_PAR_AUDIO_WIDGET_CAP
);
949 /* read all pin default configurations and save codec->init_pins */
950 static int read_pin_defaults(struct hda_codec
*codec
)
953 hda_nid_t nid
= codec
->start_nid
;
955 for (i
= 0; i
< codec
->num_nodes
; i
++, nid
++) {
956 struct hda_pincfg
*pin
;
957 unsigned int wcaps
= get_wcaps(codec
, nid
);
958 unsigned int wid_type
= get_wcaps_type(wcaps
);
959 if (wid_type
!= AC_WID_PIN
)
961 pin
= snd_array_new(&codec
->init_pins
);
965 pin
->cfg
= snd_hda_codec_read(codec
, nid
, 0,
966 AC_VERB_GET_CONFIG_DEFAULT
, 0);
967 pin
->ctrl
= snd_hda_codec_read(codec
, nid
, 0,
968 AC_VERB_GET_PIN_WIDGET_CONTROL
,
974 /* look up the given pin config list and return the item matching with NID */
975 static struct hda_pincfg
*look_up_pincfg(struct hda_codec
*codec
,
976 struct snd_array
*array
,
980 for (i
= 0; i
< array
->used
; i
++) {
981 struct hda_pincfg
*pin
= snd_array_elem(array
, i
);
988 /* write a config value for the given NID */
989 static void set_pincfg(struct hda_codec
*codec
, hda_nid_t nid
,
993 for (i
= 0; i
< 4; i
++) {
994 snd_hda_codec_write(codec
, nid
, 0,
995 AC_VERB_SET_CONFIG_DEFAULT_BYTES_0
+ i
,
1001 /* set the current pin config value for the given NID.
1002 * the value is cached, and read via snd_hda_codec_get_pincfg()
1004 int snd_hda_add_pincfg(struct hda_codec
*codec
, struct snd_array
*list
,
1005 hda_nid_t nid
, unsigned int cfg
)
1007 struct hda_pincfg
*pin
;
1008 unsigned int oldcfg
;
1010 if (get_wcaps_type(get_wcaps(codec
, nid
)) != AC_WID_PIN
)
1013 oldcfg
= snd_hda_codec_get_pincfg(codec
, nid
);
1014 pin
= look_up_pincfg(codec
, list
, nid
);
1016 pin
= snd_array_new(list
);
1023 /* change only when needed; e.g. if the pincfg is already present
1024 * in user_pins[], don't write it
1026 cfg
= snd_hda_codec_get_pincfg(codec
, nid
);
1028 set_pincfg(codec
, nid
, cfg
);
1033 * snd_hda_codec_set_pincfg - Override a pin default configuration
1034 * @codec: the HDA codec
1035 * @nid: NID to set the pin config
1036 * @cfg: the pin default config value
1038 * Override a pin default configuration value in the cache.
1039 * This value can be read by snd_hda_codec_get_pincfg() in a higher
1040 * priority than the real hardware value.
1042 int snd_hda_codec_set_pincfg(struct hda_codec
*codec
,
1043 hda_nid_t nid
, unsigned int cfg
)
1045 return snd_hda_add_pincfg(codec
, &codec
->driver_pins
, nid
, cfg
);
1047 EXPORT_SYMBOL_HDA(snd_hda_codec_set_pincfg
);
1050 * snd_hda_codec_get_pincfg - Obtain a pin-default configuration
1051 * @codec: the HDA codec
1052 * @nid: NID to get the pin config
1054 * Get the current pin config value of the given pin NID.
1055 * If the pincfg value is cached or overridden via sysfs or driver,
1056 * returns the cached value.
1058 unsigned int snd_hda_codec_get_pincfg(struct hda_codec
*codec
, hda_nid_t nid
)
1060 struct hda_pincfg
*pin
;
1062 #ifdef CONFIG_SND_HDA_HWDEP
1063 pin
= look_up_pincfg(codec
, &codec
->user_pins
, nid
);
1067 pin
= look_up_pincfg(codec
, &codec
->driver_pins
, nid
);
1070 pin
= look_up_pincfg(codec
, &codec
->init_pins
, nid
);
1075 EXPORT_SYMBOL_HDA(snd_hda_codec_get_pincfg
);
1077 /* restore all current pin configs */
1078 static void restore_pincfgs(struct hda_codec
*codec
)
1081 for (i
= 0; i
< codec
->init_pins
.used
; i
++) {
1082 struct hda_pincfg
*pin
= snd_array_elem(&codec
->init_pins
, i
);
1083 set_pincfg(codec
, pin
->nid
,
1084 snd_hda_codec_get_pincfg(codec
, pin
->nid
));
1089 * snd_hda_shutup_pins - Shut up all pins
1090 * @codec: the HDA codec
1092 * Clear all pin controls to shup up before suspend for avoiding click noise.
1093 * The controls aren't cached so that they can be resumed properly.
1095 void snd_hda_shutup_pins(struct hda_codec
*codec
)
1098 /* don't shut up pins when unloading the driver; otherwise it breaks
1099 * the default pin setup at the next load of the driver
1101 if (codec
->bus
->shutdown
)
1103 for (i
= 0; i
< codec
->init_pins
.used
; i
++) {
1104 struct hda_pincfg
*pin
= snd_array_elem(&codec
->init_pins
, i
);
1105 /* use read here for syncing after issuing each verb */
1106 snd_hda_codec_read(codec
, pin
->nid
, 0,
1107 AC_VERB_SET_PIN_WIDGET_CONTROL
, 0);
1109 codec
->pins_shutup
= 1;
1111 EXPORT_SYMBOL_HDA(snd_hda_shutup_pins
);
1114 /* Restore the pin controls cleared previously via snd_hda_shutup_pins() */
1115 static void restore_shutup_pins(struct hda_codec
*codec
)
1118 if (!codec
->pins_shutup
)
1120 if (codec
->bus
->shutdown
)
1122 for (i
= 0; i
< codec
->init_pins
.used
; i
++) {
1123 struct hda_pincfg
*pin
= snd_array_elem(&codec
->init_pins
, i
);
1124 snd_hda_codec_write(codec
, pin
->nid
, 0,
1125 AC_VERB_SET_PIN_WIDGET_CONTROL
,
1128 codec
->pins_shutup
= 0;
1132 static void init_hda_cache(struct hda_cache_rec
*cache
,
1133 unsigned int record_size
);
1134 static void free_hda_cache(struct hda_cache_rec
*cache
);
1136 /* restore the initial pin cfgs and release all pincfg lists */
1137 static void restore_init_pincfgs(struct hda_codec
*codec
)
1139 /* first free driver_pins and user_pins, then call restore_pincfg
1140 * so that only the values in init_pins are restored
1142 snd_array_free(&codec
->driver_pins
);
1143 #ifdef CONFIG_SND_HDA_HWDEP
1144 snd_array_free(&codec
->user_pins
);
1146 restore_pincfgs(codec
);
1147 snd_array_free(&codec
->init_pins
);
1151 * audio-converter setup caches
1153 struct hda_cvt_setup
{
1158 unsigned char active
; /* cvt is currently used */
1159 unsigned char dirty
; /* setups should be cleared */
1162 /* get or create a cache entry for the given audio converter NID */
1163 static struct hda_cvt_setup
*
1164 get_hda_cvt_setup(struct hda_codec
*codec
, hda_nid_t nid
)
1166 struct hda_cvt_setup
*p
;
1169 for (i
= 0; i
< codec
->cvt_setups
.used
; i
++) {
1170 p
= snd_array_elem(&codec
->cvt_setups
, i
);
1174 p
= snd_array_new(&codec
->cvt_setups
);
1183 static void snd_hda_codec_free(struct hda_codec
*codec
)
1187 restore_init_pincfgs(codec
);
1188 #ifdef CONFIG_SND_HDA_POWER_SAVE
1189 cancel_delayed_work(&codec
->power_work
);
1190 flush_workqueue(codec
->bus
->workq
);
1192 list_del(&codec
->list
);
1193 snd_array_free(&codec
->mixers
);
1194 snd_array_free(&codec
->nids
);
1195 snd_array_free(&codec
->conn_lists
);
1196 snd_array_free(&codec
->spdif_out
);
1197 codec
->bus
->caddr_tbl
[codec
->addr
] = NULL
;
1198 if (codec
->patch_ops
.free
)
1199 codec
->patch_ops
.free(codec
);
1200 module_put(codec
->owner
);
1201 free_hda_cache(&codec
->amp_cache
);
1202 free_hda_cache(&codec
->cmd_cache
);
1203 kfree(codec
->vendor_name
);
1204 kfree(codec
->chip_name
);
1205 kfree(codec
->modelname
);
1206 kfree(codec
->wcaps
);
1210 static void hda_set_power_state(struct hda_codec
*codec
, hda_nid_t fg
,
1211 unsigned int power_state
);
1214 * snd_hda_codec_new - create a HDA codec
1215 * @bus: the bus to assign
1216 * @codec_addr: the codec address
1217 * @codecp: the pointer to store the generated codec
1219 * Returns 0 if successful, or a negative error code.
1221 int /*__devinit*/ snd_hda_codec_new(struct hda_bus
*bus
,
1222 unsigned int codec_addr
,
1223 struct hda_codec
**codecp
)
1225 struct hda_codec
*codec
;
1229 if (snd_BUG_ON(!bus
))
1231 if (snd_BUG_ON(codec_addr
> HDA_MAX_CODEC_ADDRESS
))
1234 if (bus
->caddr_tbl
[codec_addr
]) {
1235 snd_printk(KERN_ERR
"hda_codec: "
1236 "address 0x%x is already occupied\n", codec_addr
);
1240 codec
= kzalloc(sizeof(*codec
), GFP_KERNEL
);
1241 if (codec
== NULL
) {
1242 snd_printk(KERN_ERR
"can't allocate struct hda_codec\n");
1247 codec
->addr
= codec_addr
;
1248 mutex_init(&codec
->spdif_mutex
);
1249 mutex_init(&codec
->control_mutex
);
1250 mutex_init(&codec
->hash_mutex
);
1251 init_hda_cache(&codec
->amp_cache
, sizeof(struct hda_amp_info
));
1252 init_hda_cache(&codec
->cmd_cache
, sizeof(struct hda_cache_head
));
1253 snd_array_init(&codec
->mixers
, sizeof(struct hda_nid_item
), 32);
1254 snd_array_init(&codec
->nids
, sizeof(struct hda_nid_item
), 32);
1255 snd_array_init(&codec
->init_pins
, sizeof(struct hda_pincfg
), 16);
1256 snd_array_init(&codec
->driver_pins
, sizeof(struct hda_pincfg
), 16);
1257 snd_array_init(&codec
->cvt_setups
, sizeof(struct hda_cvt_setup
), 8);
1258 snd_array_init(&codec
->conn_lists
, sizeof(hda_nid_t
), 64);
1259 snd_array_init(&codec
->spdif_out
, sizeof(struct hda_spdif_out
), 16);
1261 #ifdef CONFIG_SND_HDA_POWER_SAVE
1262 spin_lock_init(&codec
->power_lock
);
1263 INIT_DELAYED_WORK(&codec
->power_work
, hda_power_work
);
1264 /* snd_hda_codec_new() marks the codec as power-up, and leave it as is.
1265 * the caller has to power down appropriatley after initialization
1268 hda_keep_power_on(codec
);
1271 if (codec
->bus
->modelname
) {
1272 codec
->modelname
= kstrdup(codec
->bus
->modelname
, GFP_KERNEL
);
1273 if (!codec
->modelname
) {
1274 snd_hda_codec_free(codec
);
1279 list_add_tail(&codec
->list
, &bus
->codec_list
);
1280 bus
->caddr_tbl
[codec_addr
] = codec
;
1282 codec
->vendor_id
= snd_hda_param_read(codec
, AC_NODE_ROOT
,
1284 if (codec
->vendor_id
== -1)
1285 /* read again, hopefully the access method was corrected
1286 * in the last read...
1288 codec
->vendor_id
= snd_hda_param_read(codec
, AC_NODE_ROOT
,
1290 codec
->subsystem_id
= snd_hda_param_read(codec
, AC_NODE_ROOT
,
1291 AC_PAR_SUBSYSTEM_ID
);
1292 codec
->revision_id
= snd_hda_param_read(codec
, AC_NODE_ROOT
,
1295 setup_fg_nodes(codec
);
1296 if (!codec
->afg
&& !codec
->mfg
) {
1297 snd_printdd("hda_codec: no AFG or MFG node found\n");
1302 err
= read_widget_caps(codec
, codec
->afg
? codec
->afg
: codec
->mfg
);
1304 snd_printk(KERN_ERR
"hda_codec: cannot malloc\n");
1307 err
= read_pin_defaults(codec
);
1311 if (!codec
->subsystem_id
) {
1312 hda_nid_t nid
= codec
->afg
? codec
->afg
: codec
->mfg
;
1313 codec
->subsystem_id
=
1314 snd_hda_codec_read(codec
, nid
, 0,
1315 AC_VERB_GET_SUBSYSTEM_ID
, 0);
1318 /* power-up all before initialization */
1319 hda_set_power_state(codec
,
1320 codec
->afg
? codec
->afg
: codec
->mfg
,
1323 snd_hda_codec_proc_new(codec
);
1325 snd_hda_create_hwdep(codec
);
1327 sprintf(component
, "HDA:%08x,%08x,%08x", codec
->vendor_id
,
1328 codec
->subsystem_id
, codec
->revision_id
);
1329 snd_component_add(codec
->bus
->card
, component
);
1336 snd_hda_codec_free(codec
);
1339 EXPORT_SYMBOL_HDA(snd_hda_codec_new
);
1342 * snd_hda_codec_configure - (Re-)configure the HD-audio codec
1343 * @codec: the HDA codec
1345 * Start parsing of the given codec tree and (re-)initialize the whole
1348 * Returns 0 if successful or a negative error code.
1350 int snd_hda_codec_configure(struct hda_codec
*codec
)
1354 codec
->preset
= find_codec_preset(codec
);
1355 if (!codec
->vendor_name
|| !codec
->chip_name
) {
1356 err
= get_codec_name(codec
);
1361 if (is_generic_config(codec
)) {
1362 err
= snd_hda_parse_generic_codec(codec
);
1365 if (codec
->preset
&& codec
->preset
->patch
) {
1366 err
= codec
->preset
->patch(codec
);
1370 /* call the default parser */
1371 err
= snd_hda_parse_generic_codec(codec
);
1373 printk(KERN_ERR
"hda-codec: No codec parser is available\n");
1376 if (!err
&& codec
->patch_ops
.unsol_event
)
1377 err
= init_unsol_queue(codec
->bus
);
1378 /* audio codec should override the mixer name */
1379 if (!err
&& (codec
->afg
|| !*codec
->bus
->card
->mixername
))
1380 snprintf(codec
->bus
->card
->mixername
,
1381 sizeof(codec
->bus
->card
->mixername
),
1382 "%s %s", codec
->vendor_name
, codec
->chip_name
);
1385 EXPORT_SYMBOL_HDA(snd_hda_codec_configure
);
1388 * snd_hda_codec_setup_stream - set up the codec for streaming
1389 * @codec: the CODEC to set up
1390 * @nid: the NID to set up
1391 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
1392 * @channel_id: channel id to pass, zero based.
1393 * @format: stream format.
1395 void snd_hda_codec_setup_stream(struct hda_codec
*codec
, hda_nid_t nid
,
1397 int channel_id
, int format
)
1399 struct hda_codec
*c
;
1400 struct hda_cvt_setup
*p
;
1401 unsigned int oldval
, newval
;
1408 snd_printdd("hda_codec_setup_stream: "
1409 "NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
1410 nid
, stream_tag
, channel_id
, format
);
1411 p
= get_hda_cvt_setup(codec
, nid
);
1414 /* update the stream-id if changed */
1415 if (p
->stream_tag
!= stream_tag
|| p
->channel_id
!= channel_id
) {
1416 oldval
= snd_hda_codec_read(codec
, nid
, 0, AC_VERB_GET_CONV
, 0);
1417 newval
= (stream_tag
<< 4) | channel_id
;
1418 if (oldval
!= newval
)
1419 snd_hda_codec_write(codec
, nid
, 0,
1420 AC_VERB_SET_CHANNEL_STREAMID
,
1422 p
->stream_tag
= stream_tag
;
1423 p
->channel_id
= channel_id
;
1425 /* update the format-id if changed */
1426 if (p
->format_id
!= format
) {
1427 oldval
= snd_hda_codec_read(codec
, nid
, 0,
1428 AC_VERB_GET_STREAM_FORMAT
, 0);
1429 if (oldval
!= format
) {
1431 snd_hda_codec_write(codec
, nid
, 0,
1432 AC_VERB_SET_STREAM_FORMAT
,
1435 p
->format_id
= format
;
1440 /* make other inactive cvts with the same stream-tag dirty */
1441 type
= get_wcaps_type(get_wcaps(codec
, nid
));
1442 list_for_each_entry(c
, &codec
->bus
->codec_list
, list
) {
1443 for (i
= 0; i
< c
->cvt_setups
.used
; i
++) {
1444 p
= snd_array_elem(&c
->cvt_setups
, i
);
1445 if (!p
->active
&& p
->stream_tag
== stream_tag
&&
1446 get_wcaps_type(get_wcaps(c
, p
->nid
)) == type
)
1451 EXPORT_SYMBOL_HDA(snd_hda_codec_setup_stream
);
1453 static void really_cleanup_stream(struct hda_codec
*codec
,
1454 struct hda_cvt_setup
*q
);
1457 * __snd_hda_codec_cleanup_stream - clean up the codec for closing
1458 * @codec: the CODEC to clean up
1459 * @nid: the NID to clean up
1460 * @do_now: really clean up the stream instead of clearing the active flag
1462 void __snd_hda_codec_cleanup_stream(struct hda_codec
*codec
, hda_nid_t nid
,
1465 struct hda_cvt_setup
*p
;
1470 if (codec
->no_sticky_stream
)
1473 snd_printdd("hda_codec_cleanup_stream: NID=0x%x\n", nid
);
1474 p
= get_hda_cvt_setup(codec
, nid
);
1476 /* here we just clear the active flag when do_now isn't set;
1477 * actual clean-ups will be done later in
1478 * purify_inactive_streams() called from snd_hda_codec_prpapre()
1481 really_cleanup_stream(codec
, p
);
1486 EXPORT_SYMBOL_HDA(__snd_hda_codec_cleanup_stream
);
1488 static void really_cleanup_stream(struct hda_codec
*codec
,
1489 struct hda_cvt_setup
*q
)
1491 hda_nid_t nid
= q
->nid
;
1492 if (q
->stream_tag
|| q
->channel_id
)
1493 snd_hda_codec_write(codec
, nid
, 0, AC_VERB_SET_CHANNEL_STREAMID
, 0);
1495 snd_hda_codec_write(codec
, nid
, 0, AC_VERB_SET_STREAM_FORMAT
, 0
1497 memset(q
, 0, sizeof(*q
));
1501 /* clean up the all conflicting obsolete streams */
1502 static void purify_inactive_streams(struct hda_codec
*codec
)
1504 struct hda_codec
*c
;
1507 list_for_each_entry(c
, &codec
->bus
->codec_list
, list
) {
1508 for (i
= 0; i
< c
->cvt_setups
.used
; i
++) {
1509 struct hda_cvt_setup
*p
;
1510 p
= snd_array_elem(&c
->cvt_setups
, i
);
1512 really_cleanup_stream(c
, p
);
1518 /* clean up all streams; called from suspend */
1519 static void hda_cleanup_all_streams(struct hda_codec
*codec
)
1523 for (i
= 0; i
< codec
->cvt_setups
.used
; i
++) {
1524 struct hda_cvt_setup
*p
= snd_array_elem(&codec
->cvt_setups
, i
);
1526 really_cleanup_stream(codec
, p
);
1532 * amp access functions
1535 /* FIXME: more better hash key? */
1536 #define HDA_HASH_KEY(nid, dir, idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
1537 #define HDA_HASH_PINCAP_KEY(nid) (u32)((nid) + (0x02 << 24))
1538 #define HDA_HASH_PARPCM_KEY(nid) (u32)((nid) + (0x03 << 24))
1539 #define HDA_HASH_PARSTR_KEY(nid) (u32)((nid) + (0x04 << 24))
1540 #define INFO_AMP_CAPS (1<<0)
1541 #define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
1543 /* initialize the hash table */
1544 static void /*__devinit*/ init_hda_cache(struct hda_cache_rec
*cache
,
1545 unsigned int record_size
)
1547 memset(cache
, 0, sizeof(*cache
));
1548 memset(cache
->hash
, 0xff, sizeof(cache
->hash
));
1549 snd_array_init(&cache
->buf
, record_size
, 64);
1552 static void free_hda_cache(struct hda_cache_rec
*cache
)
1554 snd_array_free(&cache
->buf
);
1557 /* query the hash. allocate an entry if not found. */
1558 static struct hda_cache_head
*get_hash(struct hda_cache_rec
*cache
, u32 key
)
1560 u16 idx
= key
% (u16
)ARRAY_SIZE(cache
->hash
);
1561 u16 cur
= cache
->hash
[idx
];
1562 struct hda_cache_head
*info
;
1564 while (cur
!= 0xffff) {
1565 info
= snd_array_elem(&cache
->buf
, cur
);
1566 if (info
->key
== key
)
1573 /* query the hash. allocate an entry if not found. */
1574 static struct hda_cache_head
*get_alloc_hash(struct hda_cache_rec
*cache
,
1577 struct hda_cache_head
*info
= get_hash(cache
, key
);
1580 /* add a new hash entry */
1581 info
= snd_array_new(&cache
->buf
);
1584 cur
= snd_array_index(&cache
->buf
, info
);
1587 idx
= key
% (u16
)ARRAY_SIZE(cache
->hash
);
1588 info
->next
= cache
->hash
[idx
];
1589 cache
->hash
[idx
] = cur
;
1594 /* query and allocate an amp hash entry */
1595 static inline struct hda_amp_info
*
1596 get_alloc_amp_hash(struct hda_codec
*codec
, u32 key
)
1598 return (struct hda_amp_info
*)get_alloc_hash(&codec
->amp_cache
, key
);
1601 /* overwrite the value with the key in the caps hash */
1602 static int write_caps_hash(struct hda_codec
*codec
, u32 key
, unsigned int val
)
1604 struct hda_amp_info
*info
;
1606 mutex_lock(&codec
->hash_mutex
);
1607 info
= get_alloc_amp_hash(codec
, key
);
1609 mutex_unlock(&codec
->hash_mutex
);
1612 info
->amp_caps
= val
;
1613 info
->head
.val
|= INFO_AMP_CAPS
;
1614 mutex_unlock(&codec
->hash_mutex
);
1618 /* query the value from the caps hash; if not found, fetch the current
1619 * value from the given function and store in the hash
1622 query_caps_hash(struct hda_codec
*codec
, hda_nid_t nid
, int dir
, u32 key
,
1623 unsigned int (*func
)(struct hda_codec
*, hda_nid_t
, int))
1625 struct hda_amp_info
*info
;
1628 mutex_lock(&codec
->hash_mutex
);
1629 info
= get_alloc_amp_hash(codec
, key
);
1631 mutex_unlock(&codec
->hash_mutex
);
1634 if (!(info
->head
.val
& INFO_AMP_CAPS
)) {
1635 mutex_unlock(&codec
->hash_mutex
); /* for reentrance */
1636 val
= func(codec
, nid
, dir
);
1637 write_caps_hash(codec
, key
, val
);
1639 val
= info
->amp_caps
;
1640 mutex_unlock(&codec
->hash_mutex
);
1645 static unsigned int read_amp_cap(struct hda_codec
*codec
, hda_nid_t nid
,
1648 if (!(get_wcaps(codec
, nid
) & AC_WCAP_AMP_OVRD
))
1650 return snd_hda_param_read(codec
, nid
,
1651 direction
== HDA_OUTPUT
?
1652 AC_PAR_AMP_OUT_CAP
: AC_PAR_AMP_IN_CAP
);
1656 * query_amp_caps - query AMP capabilities
1657 * @codec: the HD-auio codec
1658 * @nid: the NID to query
1659 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1661 * Query AMP capabilities for the given widget and direction.
1662 * Returns the obtained capability bits.
1664 * When cap bits have been already read, this doesn't read again but
1665 * returns the cached value.
1667 u32
query_amp_caps(struct hda_codec
*codec
, hda_nid_t nid
, int direction
)
1669 return query_caps_hash(codec
, nid
, direction
,
1670 HDA_HASH_KEY(nid
, direction
, 0),
1673 EXPORT_SYMBOL_HDA(query_amp_caps
);
1676 * snd_hda_override_amp_caps - Override the AMP capabilities
1677 * @codec: the CODEC to clean up
1678 * @nid: the NID to clean up
1679 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1680 * @caps: the capability bits to set
1682 * Override the cached AMP caps bits value by the given one.
1683 * This function is useful if the driver needs to adjust the AMP ranges,
1684 * e.g. limit to 0dB, etc.
1686 * Returns zero if successful or a negative error code.
1688 int snd_hda_override_amp_caps(struct hda_codec
*codec
, hda_nid_t nid
, int dir
,
1691 return write_caps_hash(codec
, HDA_HASH_KEY(nid
, dir
, 0), caps
);
1693 EXPORT_SYMBOL_HDA(snd_hda_override_amp_caps
);
1695 static unsigned int read_pin_cap(struct hda_codec
*codec
, hda_nid_t nid
,
1698 return snd_hda_param_read(codec
, nid
, AC_PAR_PIN_CAP
);
1702 * snd_hda_query_pin_caps - Query PIN capabilities
1703 * @codec: the HD-auio codec
1704 * @nid: the NID to query
1706 * Query PIN capabilities for the given widget.
1707 * Returns the obtained capability bits.
1709 * When cap bits have been already read, this doesn't read again but
1710 * returns the cached value.
1712 u32
snd_hda_query_pin_caps(struct hda_codec
*codec
, hda_nid_t nid
)
1714 return query_caps_hash(codec
, nid
, 0, HDA_HASH_PINCAP_KEY(nid
),
1717 EXPORT_SYMBOL_HDA(snd_hda_query_pin_caps
);
1720 * snd_hda_override_pin_caps - Override the pin capabilities
1722 * @nid: the NID to override
1723 * @caps: the capability bits to set
1725 * Override the cached PIN capabilitiy bits value by the given one.
1727 * Returns zero if successful or a negative error code.
1729 int snd_hda_override_pin_caps(struct hda_codec
*codec
, hda_nid_t nid
,
1732 return write_caps_hash(codec
, HDA_HASH_PINCAP_KEY(nid
), caps
);
1734 EXPORT_SYMBOL_HDA(snd_hda_override_pin_caps
);
1736 /* read or sync the hash value with the current value;
1737 * call within hash_mutex
1739 static struct hda_amp_info
*
1740 update_amp_hash(struct hda_codec
*codec
, hda_nid_t nid
, int ch
,
1741 int direction
, int index
)
1743 struct hda_amp_info
*info
;
1744 unsigned int parm
, val
= 0;
1745 bool val_read
= false;
1748 info
= get_alloc_amp_hash(codec
, HDA_HASH_KEY(nid
, direction
, index
));
1751 if (!(info
->head
.val
& INFO_AMP_VOL(ch
))) {
1753 mutex_unlock(&codec
->hash_mutex
);
1754 parm
= ch
? AC_AMP_GET_RIGHT
: AC_AMP_GET_LEFT
;
1755 parm
|= direction
== HDA_OUTPUT
?
1756 AC_AMP_GET_OUTPUT
: AC_AMP_GET_INPUT
;
1758 val
= snd_hda_codec_read(codec
, nid
, 0,
1759 AC_VERB_GET_AMP_GAIN_MUTE
, parm
);
1762 mutex_lock(&codec
->hash_mutex
);
1765 info
->vol
[ch
] = val
;
1766 info
->head
.val
|= INFO_AMP_VOL(ch
);
1772 * write the current volume in info to the h/w
1774 static void put_vol_mute(struct hda_codec
*codec
, struct hda_amp_info
*info
,
1775 hda_nid_t nid
, int ch
, int direction
, int index
,
1780 parm
= ch
? AC_AMP_SET_RIGHT
: AC_AMP_SET_LEFT
;
1781 parm
|= direction
== HDA_OUTPUT
? AC_AMP_SET_OUTPUT
: AC_AMP_SET_INPUT
;
1782 parm
|= index
<< AC_AMP_SET_INDEX_SHIFT
;
1783 if ((val
& HDA_AMP_MUTE
) && !(info
->amp_caps
& AC_AMPCAP_MUTE
) &&
1784 (info
->amp_caps
& AC_AMPCAP_MIN_MUTE
))
1785 ; /* set the zero value as a fake mute */
1788 snd_hda_codec_write(codec
, nid
, 0, AC_VERB_SET_AMP_GAIN_MUTE
, parm
);
1792 * snd_hda_codec_amp_read - Read AMP value
1793 * @codec: HD-audio codec
1794 * @nid: NID to read the AMP value
1795 * @ch: channel (left=0 or right=1)
1796 * @direction: #HDA_INPUT or #HDA_OUTPUT
1797 * @index: the index value (only for input direction)
1799 * Read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
1801 int snd_hda_codec_amp_read(struct hda_codec
*codec
, hda_nid_t nid
, int ch
,
1802 int direction
, int index
)
1804 struct hda_amp_info
*info
;
1805 unsigned int val
= 0;
1807 mutex_lock(&codec
->hash_mutex
);
1808 info
= update_amp_hash(codec
, nid
, ch
, direction
, index
);
1810 val
= info
->vol
[ch
];
1811 mutex_unlock(&codec
->hash_mutex
);
1814 EXPORT_SYMBOL_HDA(snd_hda_codec_amp_read
);
1817 * snd_hda_codec_amp_update - update the AMP value
1818 * @codec: HD-audio codec
1819 * @nid: NID to read the AMP value
1820 * @ch: channel (left=0 or right=1)
1821 * @direction: #HDA_INPUT or #HDA_OUTPUT
1822 * @idx: the index value (only for input direction)
1823 * @mask: bit mask to set
1824 * @val: the bits value to set
1826 * Update the AMP value with a bit mask.
1827 * Returns 0 if the value is unchanged, 1 if changed.
1829 int snd_hda_codec_amp_update(struct hda_codec
*codec
, hda_nid_t nid
, int ch
,
1830 int direction
, int idx
, int mask
, int val
)
1832 struct hda_amp_info
*info
;
1834 if (snd_BUG_ON(mask
& ~0xff))
1838 mutex_lock(&codec
->hash_mutex
);
1839 info
= update_amp_hash(codec
, nid
, ch
, direction
, idx
);
1841 mutex_unlock(&codec
->hash_mutex
);
1844 val
|= info
->vol
[ch
] & ~mask
;
1845 if (info
->vol
[ch
] == val
) {
1846 mutex_unlock(&codec
->hash_mutex
);
1849 info
->vol
[ch
] = val
;
1850 mutex_unlock(&codec
->hash_mutex
);
1851 put_vol_mute(codec
, info
, nid
, ch
, direction
, idx
, val
);
1854 EXPORT_SYMBOL_HDA(snd_hda_codec_amp_update
);
1857 * snd_hda_codec_amp_stereo - update the AMP stereo values
1858 * @codec: HD-audio codec
1859 * @nid: NID to read the AMP value
1860 * @direction: #HDA_INPUT or #HDA_OUTPUT
1861 * @idx: the index value (only for input direction)
1862 * @mask: bit mask to set
1863 * @val: the bits value to set
1865 * Update the AMP values like snd_hda_codec_amp_update(), but for a
1866 * stereo widget with the same mask and value.
1868 int snd_hda_codec_amp_stereo(struct hda_codec
*codec
, hda_nid_t nid
,
1869 int direction
, int idx
, int mask
, int val
)
1873 if (snd_BUG_ON(mask
& ~0xff))
1875 for (ch
= 0; ch
< 2; ch
++)
1876 ret
|= snd_hda_codec_amp_update(codec
, nid
, ch
, direction
,
1880 EXPORT_SYMBOL_HDA(snd_hda_codec_amp_stereo
);
1884 * snd_hda_codec_resume_amp - Resume all AMP commands from the cache
1885 * @codec: HD-audio codec
1887 * Resume the all amp commands from the cache.
1889 void snd_hda_codec_resume_amp(struct hda_codec
*codec
)
1891 struct hda_amp_info
*buffer
= codec
->amp_cache
.buf
.list
;
1894 for (i
= 0; i
< codec
->amp_cache
.buf
.used
; i
++, buffer
++) {
1895 u32 key
= buffer
->head
.key
;
1897 unsigned int idx
, dir
, ch
;
1901 idx
= (key
>> 16) & 0xff;
1902 dir
= (key
>> 24) & 0xff;
1903 for (ch
= 0; ch
< 2; ch
++) {
1904 if (!(buffer
->head
.val
& INFO_AMP_VOL(ch
)))
1906 put_vol_mute(codec
, buffer
, nid
, ch
, dir
, idx
,
1911 EXPORT_SYMBOL_HDA(snd_hda_codec_resume_amp
);
1912 #endif /* CONFIG_PM */
1914 static u32
get_amp_max_value(struct hda_codec
*codec
, hda_nid_t nid
, int dir
,
1917 u32 caps
= query_amp_caps(codec
, nid
, dir
);
1919 caps
= (caps
& AC_AMPCAP_NUM_STEPS
) >> AC_AMPCAP_NUM_STEPS_SHIFT
;
1926 * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer
1928 * The control element is supposed to have the private_value field
1929 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1931 int snd_hda_mixer_amp_volume_info(struct snd_kcontrol
*kcontrol
,
1932 struct snd_ctl_elem_info
*uinfo
)
1934 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
1935 u16 nid
= get_amp_nid(kcontrol
);
1936 u8 chs
= get_amp_channels(kcontrol
);
1937 int dir
= get_amp_direction(kcontrol
);
1938 unsigned int ofs
= get_amp_offset(kcontrol
);
1940 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
1941 uinfo
->count
= chs
== 3 ? 2 : 1;
1942 uinfo
->value
.integer
.min
= 0;
1943 uinfo
->value
.integer
.max
= get_amp_max_value(codec
, nid
, dir
, ofs
);
1944 if (!uinfo
->value
.integer
.max
) {
1945 printk(KERN_WARNING
"hda_codec: "
1946 "num_steps = 0 for NID=0x%x (ctl = %s)\n", nid
,
1952 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_info
);
1955 static inline unsigned int
1956 read_amp_value(struct hda_codec
*codec
, hda_nid_t nid
,
1957 int ch
, int dir
, int idx
, unsigned int ofs
)
1960 val
= snd_hda_codec_amp_read(codec
, nid
, ch
, dir
, idx
);
1961 val
&= HDA_AMP_VOLMASK
;
1970 update_amp_value(struct hda_codec
*codec
, hda_nid_t nid
,
1971 int ch
, int dir
, int idx
, unsigned int ofs
,
1974 unsigned int maxval
;
1978 /* ofs = 0: raw max value */
1979 maxval
= get_amp_max_value(codec
, nid
, dir
, 0);
1982 return snd_hda_codec_amp_update(codec
, nid
, ch
, dir
, idx
,
1983 HDA_AMP_VOLMASK
, val
);
1987 * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume
1989 * The control element is supposed to have the private_value field
1990 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1992 int snd_hda_mixer_amp_volume_get(struct snd_kcontrol
*kcontrol
,
1993 struct snd_ctl_elem_value
*ucontrol
)
1995 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
1996 hda_nid_t nid
= get_amp_nid(kcontrol
);
1997 int chs
= get_amp_channels(kcontrol
);
1998 int dir
= get_amp_direction(kcontrol
);
1999 int idx
= get_amp_index(kcontrol
);
2000 unsigned int ofs
= get_amp_offset(kcontrol
);
2001 long *valp
= ucontrol
->value
.integer
.value
;
2004 *valp
++ = read_amp_value(codec
, nid
, 0, dir
, idx
, ofs
);
2006 *valp
= read_amp_value(codec
, nid
, 1, dir
, idx
, ofs
);
2009 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_get
);
2012 * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume
2014 * The control element is supposed to have the private_value field
2015 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2017 int snd_hda_mixer_amp_volume_put(struct snd_kcontrol
*kcontrol
,
2018 struct snd_ctl_elem_value
*ucontrol
)
2020 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2021 hda_nid_t nid
= get_amp_nid(kcontrol
);
2022 int chs
= get_amp_channels(kcontrol
);
2023 int dir
= get_amp_direction(kcontrol
);
2024 int idx
= get_amp_index(kcontrol
);
2025 unsigned int ofs
= get_amp_offset(kcontrol
);
2026 long *valp
= ucontrol
->value
.integer
.value
;
2029 snd_hda_power_up(codec
);
2031 change
= update_amp_value(codec
, nid
, 0, dir
, idx
, ofs
, *valp
);
2035 change
|= update_amp_value(codec
, nid
, 1, dir
, idx
, ofs
, *valp
);
2036 snd_hda_power_down(codec
);
2039 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_put
);
2042 * snd_hda_mixer_amp_volume_put - TLV callback for a standard AMP mixer volume
2044 * The control element is supposed to have the private_value field
2045 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2047 int snd_hda_mixer_amp_tlv(struct snd_kcontrol
*kcontrol
, int op_flag
,
2048 unsigned int size
, unsigned int __user
*_tlv
)
2050 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2051 hda_nid_t nid
= get_amp_nid(kcontrol
);
2052 int dir
= get_amp_direction(kcontrol
);
2053 unsigned int ofs
= get_amp_offset(kcontrol
);
2054 bool min_mute
= get_amp_min_mute(kcontrol
);
2055 u32 caps
, val1
, val2
;
2057 if (size
< 4 * sizeof(unsigned int))
2059 caps
= query_amp_caps(codec
, nid
, dir
);
2060 val2
= (caps
& AC_AMPCAP_STEP_SIZE
) >> AC_AMPCAP_STEP_SIZE_SHIFT
;
2061 val2
= (val2
+ 1) * 25;
2062 val1
= -((caps
& AC_AMPCAP_OFFSET
) >> AC_AMPCAP_OFFSET_SHIFT
);
2064 val1
= ((int)val1
) * ((int)val2
);
2065 if (min_mute
|| (caps
& AC_AMPCAP_MIN_MUTE
))
2066 val2
|= TLV_DB_SCALE_MUTE
;
2067 if (put_user(SNDRV_CTL_TLVT_DB_SCALE
, _tlv
))
2069 if (put_user(2 * sizeof(unsigned int), _tlv
+ 1))
2071 if (put_user(val1
, _tlv
+ 2))
2073 if (put_user(val2
, _tlv
+ 3))
2077 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_tlv
);
2080 * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control
2081 * @codec: HD-audio codec
2082 * @nid: NID of a reference widget
2083 * @dir: #HDA_INPUT or #HDA_OUTPUT
2084 * @tlv: TLV data to be stored, at least 4 elements
2086 * Set (static) TLV data for a virtual master volume using the AMP caps
2087 * obtained from the reference NID.
2088 * The volume range is recalculated as if the max volume is 0dB.
2090 void snd_hda_set_vmaster_tlv(struct hda_codec
*codec
, hda_nid_t nid
, int dir
,
2096 caps
= query_amp_caps(codec
, nid
, dir
);
2097 nums
= (caps
& AC_AMPCAP_NUM_STEPS
) >> AC_AMPCAP_NUM_STEPS_SHIFT
;
2098 step
= (caps
& AC_AMPCAP_STEP_SIZE
) >> AC_AMPCAP_STEP_SIZE_SHIFT
;
2099 step
= (step
+ 1) * 25;
2100 tlv
[0] = SNDRV_CTL_TLVT_DB_SCALE
;
2101 tlv
[1] = 2 * sizeof(unsigned int);
2102 tlv
[2] = -nums
* step
;
2105 EXPORT_SYMBOL_HDA(snd_hda_set_vmaster_tlv
);
2107 /* find a mixer control element with the given name */
2108 static struct snd_kcontrol
*
2109 _snd_hda_find_mixer_ctl(struct hda_codec
*codec
,
2110 const char *name
, int idx
)
2112 struct snd_ctl_elem_id id
;
2113 memset(&id
, 0, sizeof(id
));
2114 id
.iface
= SNDRV_CTL_ELEM_IFACE_MIXER
;
2116 if (snd_BUG_ON(strlen(name
) >= sizeof(id
.name
)))
2118 strcpy(id
.name
, name
);
2119 return snd_ctl_find_id(codec
->bus
->card
, &id
);
2123 * snd_hda_find_mixer_ctl - Find a mixer control element with the given name
2124 * @codec: HD-audio codec
2125 * @name: ctl id name string
2127 * Get the control element with the given id string and IFACE_MIXER.
2129 struct snd_kcontrol
*snd_hda_find_mixer_ctl(struct hda_codec
*codec
,
2132 return _snd_hda_find_mixer_ctl(codec
, name
, 0);
2134 EXPORT_SYMBOL_HDA(snd_hda_find_mixer_ctl
);
2136 static int find_empty_mixer_ctl_idx(struct hda_codec
*codec
, const char *name
)
2139 for (idx
= 0; idx
< 16; idx
++) { /* 16 ctlrs should be large enough */
2140 if (!_snd_hda_find_mixer_ctl(codec
, name
, idx
))
2147 * snd_hda_ctl_add - Add a control element and assign to the codec
2148 * @codec: HD-audio codec
2149 * @nid: corresponding NID (optional)
2150 * @kctl: the control element to assign
2152 * Add the given control element to an array inside the codec instance.
2153 * All control elements belonging to a codec are supposed to be added
2154 * by this function so that a proper clean-up works at the free or
2155 * reconfiguration time.
2157 * If non-zero @nid is passed, the NID is assigned to the control element.
2158 * The assignment is shown in the codec proc file.
2160 * snd_hda_ctl_add() checks the control subdev id field whether
2161 * #HDA_SUBDEV_NID_FLAG bit is set. If set (and @nid is zero), the lower
2162 * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit
2163 * specifies if kctl->private_value is a HDA amplifier value.
2165 int snd_hda_ctl_add(struct hda_codec
*codec
, hda_nid_t nid
,
2166 struct snd_kcontrol
*kctl
)
2169 unsigned short flags
= 0;
2170 struct hda_nid_item
*item
;
2172 if (kctl
->id
.subdevice
& HDA_SUBDEV_AMP_FLAG
) {
2173 flags
|= HDA_NID_ITEM_AMP
;
2175 nid
= get_amp_nid_(kctl
->private_value
);
2177 if ((kctl
->id
.subdevice
& HDA_SUBDEV_NID_FLAG
) != 0 && nid
== 0)
2178 nid
= kctl
->id
.subdevice
& 0xffff;
2179 if (kctl
->id
.subdevice
& (HDA_SUBDEV_NID_FLAG
|HDA_SUBDEV_AMP_FLAG
))
2180 kctl
->id
.subdevice
= 0;
2181 err
= snd_ctl_add(codec
->bus
->card
, kctl
);
2184 item
= snd_array_new(&codec
->mixers
);
2189 item
->flags
= flags
;
2192 EXPORT_SYMBOL_HDA(snd_hda_ctl_add
);
2195 * snd_hda_add_nid - Assign a NID to a control element
2196 * @codec: HD-audio codec
2197 * @nid: corresponding NID (optional)
2198 * @kctl: the control element to assign
2199 * @index: index to kctl
2201 * Add the given control element to an array inside the codec instance.
2202 * This function is used when #snd_hda_ctl_add cannot be used for 1:1
2203 * NID:KCTL mapping - for example "Capture Source" selector.
2205 int snd_hda_add_nid(struct hda_codec
*codec
, struct snd_kcontrol
*kctl
,
2206 unsigned int index
, hda_nid_t nid
)
2208 struct hda_nid_item
*item
;
2211 item
= snd_array_new(&codec
->nids
);
2215 item
->index
= index
;
2219 printk(KERN_ERR
"hda-codec: no NID for mapping control %s:%d:%d\n",
2220 kctl
->id
.name
, kctl
->id
.index
, index
);
2223 EXPORT_SYMBOL_HDA(snd_hda_add_nid
);
2226 * snd_hda_ctls_clear - Clear all controls assigned to the given codec
2227 * @codec: HD-audio codec
2229 void snd_hda_ctls_clear(struct hda_codec
*codec
)
2232 struct hda_nid_item
*items
= codec
->mixers
.list
;
2233 for (i
= 0; i
< codec
->mixers
.used
; i
++)
2234 snd_ctl_remove(codec
->bus
->card
, items
[i
].kctl
);
2235 snd_array_free(&codec
->mixers
);
2236 snd_array_free(&codec
->nids
);
2239 /* pseudo device locking
2240 * toggle card->shutdown to allow/disallow the device access (as a hack)
2242 int snd_hda_lock_devices(struct hda_bus
*bus
)
2244 struct snd_card
*card
= bus
->card
;
2245 struct hda_codec
*codec
;
2247 spin_lock(&card
->files_lock
);
2251 if (!list_empty(&card
->ctl_files
))
2254 list_for_each_entry(codec
, &bus
->codec_list
, list
) {
2256 for (pcm
= 0; pcm
< codec
->num_pcms
; pcm
++) {
2257 struct hda_pcm
*cpcm
= &codec
->pcm_info
[pcm
];
2260 if (cpcm
->pcm
->streams
[0].substream_opened
||
2261 cpcm
->pcm
->streams
[1].substream_opened
)
2265 spin_unlock(&card
->files_lock
);
2271 spin_unlock(&card
->files_lock
);
2274 EXPORT_SYMBOL_HDA(snd_hda_lock_devices
);
2276 void snd_hda_unlock_devices(struct hda_bus
*bus
)
2278 struct snd_card
*card
= bus
->card
;
2281 spin_lock(&card
->files_lock
);
2283 spin_unlock(&card
->files_lock
);
2285 EXPORT_SYMBOL_HDA(snd_hda_unlock_devices
);
2288 * snd_hda_codec_reset - Clear all objects assigned to the codec
2289 * @codec: HD-audio codec
2291 * This frees the all PCM and control elements assigned to the codec, and
2292 * clears the caches and restores the pin default configurations.
2294 * When a device is being used, it returns -EBSY. If successfully freed,
2297 int snd_hda_codec_reset(struct hda_codec
*codec
)
2299 struct hda_bus
*bus
= codec
->bus
;
2300 struct snd_card
*card
= bus
->card
;
2303 if (snd_hda_lock_devices(bus
) < 0)
2306 /* OK, let it free */
2308 #ifdef CONFIG_SND_HDA_POWER_SAVE
2309 cancel_delayed_work_sync(&codec
->power_work
);
2310 codec
->power_on
= 0;
2311 codec
->power_transition
= 0;
2312 codec
->power_jiffies
= jiffies
;
2313 flush_workqueue(bus
->workq
);
2315 snd_hda_ctls_clear(codec
);
2317 for (i
= 0; i
< codec
->num_pcms
; i
++) {
2318 if (codec
->pcm_info
[i
].pcm
) {
2319 snd_device_free(card
, codec
->pcm_info
[i
].pcm
);
2320 clear_bit(codec
->pcm_info
[i
].device
,
2324 if (codec
->patch_ops
.free
)
2325 codec
->patch_ops
.free(codec
);
2326 snd_hda_jack_tbl_clear(codec
);
2327 codec
->proc_widget_hook
= NULL
;
2329 free_hda_cache(&codec
->amp_cache
);
2330 free_hda_cache(&codec
->cmd_cache
);
2331 init_hda_cache(&codec
->amp_cache
, sizeof(struct hda_amp_info
));
2332 init_hda_cache(&codec
->cmd_cache
, sizeof(struct hda_cache_head
));
2333 /* free only driver_pins so that init_pins + user_pins are restored */
2334 snd_array_free(&codec
->driver_pins
);
2335 restore_pincfgs(codec
);
2336 codec
->num_pcms
= 0;
2337 codec
->pcm_info
= NULL
;
2338 codec
->preset
= NULL
;
2339 memset(&codec
->patch_ops
, 0, sizeof(codec
->patch_ops
));
2340 codec
->slave_dig_outs
= NULL
;
2341 codec
->spdif_status_reset
= 0;
2342 module_put(codec
->owner
);
2343 codec
->owner
= NULL
;
2345 /* allow device access again */
2346 snd_hda_unlock_devices(bus
);
2350 typedef int (*map_slave_func_t
)(void *, struct snd_kcontrol
*);
2352 /* apply the function to all matching slave ctls in the mixer list */
2353 static int map_slaves(struct hda_codec
*codec
, const char * const *slaves
,
2354 const char *suffix
, map_slave_func_t func
, void *data
)
2356 struct hda_nid_item
*items
;
2357 const char * const *s
;
2360 items
= codec
->mixers
.list
;
2361 for (i
= 0; i
< codec
->mixers
.used
; i
++) {
2362 struct snd_kcontrol
*sctl
= items
[i
].kctl
;
2363 if (!sctl
|| !sctl
->id
.name
||
2364 sctl
->id
.iface
!= SNDRV_CTL_ELEM_IFACE_MIXER
)
2366 for (s
= slaves
; *s
; s
++) {
2367 char tmpname
[sizeof(sctl
->id
.name
)];
2368 const char *name
= *s
;
2370 snprintf(tmpname
, sizeof(tmpname
), "%s %s",
2374 if (!strcmp(sctl
->id
.name
, name
)) {
2375 err
= func(data
, sctl
);
2385 static int check_slave_present(void *data
, struct snd_kcontrol
*sctl
)
2390 /* guess the value corresponding to 0dB */
2391 static int get_kctl_0dB_offset(struct snd_kcontrol
*kctl
)
2394 const int *tlv
= NULL
;
2397 if (kctl
->vd
[0].access
& SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK
) {
2398 /* FIXME: set_fs() hack for obtaining user-space TLV data */
2399 mm_segment_t fs
= get_fs();
2401 if (!kctl
->tlv
.c(kctl
, 0, sizeof(_tlv
), _tlv
))
2404 } else if (kctl
->vd
[0].access
& SNDRV_CTL_ELEM_ACCESS_TLV_READ
)
2406 if (tlv
&& tlv
[0] == SNDRV_CTL_TLVT_DB_SCALE
)
2407 val
= -tlv
[2] / tlv
[3];
2411 /* call kctl->put with the given value(s) */
2412 static int put_kctl_with_value(struct snd_kcontrol
*kctl
, int val
)
2414 struct snd_ctl_elem_value
*ucontrol
;
2415 ucontrol
= kzalloc(sizeof(*ucontrol
), GFP_KERNEL
);
2418 ucontrol
->value
.integer
.value
[0] = val
;
2419 ucontrol
->value
.integer
.value
[1] = val
;
2420 kctl
->put(kctl
, ucontrol
);
2425 /* initialize the slave volume with 0dB */
2426 static int init_slave_0dB(void *data
, struct snd_kcontrol
*slave
)
2428 int offset
= get_kctl_0dB_offset(slave
);
2430 put_kctl_with_value(slave
, offset
);
2434 /* unmute the slave */
2435 static int init_slave_unmute(void *data
, struct snd_kcontrol
*slave
)
2437 return put_kctl_with_value(slave
, 1);
2441 * snd_hda_add_vmaster - create a virtual master control and add slaves
2442 * @codec: HD-audio codec
2443 * @name: vmaster control name
2444 * @tlv: TLV data (optional)
2445 * @slaves: slave control names (optional)
2446 * @suffix: suffix string to each slave name (optional)
2447 * @init_slave_vol: initialize slaves to unmute/0dB
2448 * @ctl_ret: store the vmaster kcontrol in return
2450 * Create a virtual master control with the given name. The TLV data
2451 * must be either NULL or a valid data.
2453 * @slaves is a NULL-terminated array of strings, each of which is a
2454 * slave control name. All controls with these names are assigned to
2455 * the new virtual master control.
2457 * This function returns zero if successful or a negative error code.
2459 int __snd_hda_add_vmaster(struct hda_codec
*codec
, char *name
,
2460 unsigned int *tlv
, const char * const *slaves
,
2461 const char *suffix
, bool init_slave_vol
,
2462 struct snd_kcontrol
**ctl_ret
)
2464 struct snd_kcontrol
*kctl
;
2470 err
= map_slaves(codec
, slaves
, suffix
, check_slave_present
, NULL
);
2472 snd_printdd("No slave found for %s\n", name
);
2475 kctl
= snd_ctl_make_virtual_master(name
, tlv
);
2478 err
= snd_hda_ctl_add(codec
, 0, kctl
);
2482 err
= map_slaves(codec
, slaves
, suffix
,
2483 (map_slave_func_t
)snd_ctl_add_slave
, kctl
);
2487 /* init with master mute & zero volume */
2488 put_kctl_with_value(kctl
, 0);
2490 map_slaves(codec
, slaves
, suffix
,
2491 tlv
? init_slave_0dB
: init_slave_unmute
, kctl
);
2497 EXPORT_SYMBOL_HDA(__snd_hda_add_vmaster
);
2500 * mute-LED control using vmaster
2502 static int vmaster_mute_mode_info(struct snd_kcontrol
*kcontrol
,
2503 struct snd_ctl_elem_info
*uinfo
)
2505 static const char * const texts
[] = {
2506 "Off", "On", "Follow Master"
2510 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_ENUMERATED
;
2512 uinfo
->value
.enumerated
.items
= 3;
2513 index
= uinfo
->value
.enumerated
.item
;
2516 strcpy(uinfo
->value
.enumerated
.name
, texts
[index
]);
2520 static int vmaster_mute_mode_get(struct snd_kcontrol
*kcontrol
,
2521 struct snd_ctl_elem_value
*ucontrol
)
2523 struct hda_vmaster_mute_hook
*hook
= snd_kcontrol_chip(kcontrol
);
2524 ucontrol
->value
.enumerated
.item
[0] = hook
->mute_mode
;
2528 static int vmaster_mute_mode_put(struct snd_kcontrol
*kcontrol
,
2529 struct snd_ctl_elem_value
*ucontrol
)
2531 struct hda_vmaster_mute_hook
*hook
= snd_kcontrol_chip(kcontrol
);
2532 unsigned int old_mode
= hook
->mute_mode
;
2534 hook
->mute_mode
= ucontrol
->value
.enumerated
.item
[0];
2535 if (hook
->mute_mode
> HDA_VMUTE_FOLLOW_MASTER
)
2536 hook
->mute_mode
= HDA_VMUTE_FOLLOW_MASTER
;
2537 if (old_mode
== hook
->mute_mode
)
2539 snd_hda_sync_vmaster_hook(hook
);
2543 static struct snd_kcontrol_new vmaster_mute_mode
= {
2544 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
2545 .name
= "Mute-LED Mode",
2546 .info
= vmaster_mute_mode_info
,
2547 .get
= vmaster_mute_mode_get
,
2548 .put
= vmaster_mute_mode_put
,
2552 * Add a mute-LED hook with the given vmaster switch kctl
2553 * "Mute-LED Mode" control is automatically created and associated with
2556 int snd_hda_add_vmaster_hook(struct hda_codec
*codec
,
2557 struct hda_vmaster_mute_hook
*hook
,
2558 bool expose_enum_ctl
)
2560 struct snd_kcontrol
*kctl
;
2562 if (!hook
->hook
|| !hook
->sw_kctl
)
2564 snd_ctl_add_vmaster_hook(hook
->sw_kctl
, hook
->hook
, codec
);
2565 hook
->codec
= codec
;
2566 hook
->mute_mode
= HDA_VMUTE_FOLLOW_MASTER
;
2567 if (!expose_enum_ctl
)
2569 kctl
= snd_ctl_new1(&vmaster_mute_mode
, hook
);
2572 return snd_hda_ctl_add(codec
, 0, kctl
);
2574 EXPORT_SYMBOL_HDA(snd_hda_add_vmaster_hook
);
2577 * Call the hook with the current value for synchronization
2578 * Should be called in init callback
2580 void snd_hda_sync_vmaster_hook(struct hda_vmaster_mute_hook
*hook
)
2582 if (!hook
->hook
|| !hook
->codec
)
2584 switch (hook
->mute_mode
) {
2585 case HDA_VMUTE_FOLLOW_MASTER
:
2586 snd_ctl_sync_vmaster_hook(hook
->sw_kctl
);
2589 hook
->hook(hook
->codec
, hook
->mute_mode
);
2593 EXPORT_SYMBOL_HDA(snd_hda_sync_vmaster_hook
);
2597 * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch
2599 * The control element is supposed to have the private_value field
2600 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2602 int snd_hda_mixer_amp_switch_info(struct snd_kcontrol
*kcontrol
,
2603 struct snd_ctl_elem_info
*uinfo
)
2605 int chs
= get_amp_channels(kcontrol
);
2607 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_BOOLEAN
;
2608 uinfo
->count
= chs
== 3 ? 2 : 1;
2609 uinfo
->value
.integer
.min
= 0;
2610 uinfo
->value
.integer
.max
= 1;
2613 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_info
);
2616 * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch
2618 * The control element is supposed to have the private_value field
2619 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2621 int snd_hda_mixer_amp_switch_get(struct snd_kcontrol
*kcontrol
,
2622 struct snd_ctl_elem_value
*ucontrol
)
2624 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2625 hda_nid_t nid
= get_amp_nid(kcontrol
);
2626 int chs
= get_amp_channels(kcontrol
);
2627 int dir
= get_amp_direction(kcontrol
);
2628 int idx
= get_amp_index(kcontrol
);
2629 long *valp
= ucontrol
->value
.integer
.value
;
2632 *valp
++ = (snd_hda_codec_amp_read(codec
, nid
, 0, dir
, idx
) &
2633 HDA_AMP_MUTE
) ? 0 : 1;
2635 *valp
= (snd_hda_codec_amp_read(codec
, nid
, 1, dir
, idx
) &
2636 HDA_AMP_MUTE
) ? 0 : 1;
2639 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_get
);
2642 * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch
2644 * The control element is supposed to have the private_value field
2645 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2647 int snd_hda_mixer_amp_switch_put(struct snd_kcontrol
*kcontrol
,
2648 struct snd_ctl_elem_value
*ucontrol
)
2650 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2651 hda_nid_t nid
= get_amp_nid(kcontrol
);
2652 int chs
= get_amp_channels(kcontrol
);
2653 int dir
= get_amp_direction(kcontrol
);
2654 int idx
= get_amp_index(kcontrol
);
2655 long *valp
= ucontrol
->value
.integer
.value
;
2658 snd_hda_power_up(codec
);
2660 change
= snd_hda_codec_amp_update(codec
, nid
, 0, dir
, idx
,
2662 *valp
? 0 : HDA_AMP_MUTE
);
2666 change
|= snd_hda_codec_amp_update(codec
, nid
, 1, dir
, idx
,
2668 *valp
? 0 : HDA_AMP_MUTE
);
2669 hda_call_check_power_status(codec
, nid
);
2670 snd_hda_power_down(codec
);
2673 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put
);
2675 #ifdef CONFIG_SND_HDA_INPUT_BEEP
2677 * snd_hda_mixer_amp_switch_put_beep - Put callback for a beep AMP switch
2679 * This function calls snd_hda_enable_beep_device(), which behaves differently
2680 * depending on beep_mode option.
2682 int snd_hda_mixer_amp_switch_put_beep(struct snd_kcontrol
*kcontrol
,
2683 struct snd_ctl_elem_value
*ucontrol
)
2685 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2686 long *valp
= ucontrol
->value
.integer
.value
;
2688 snd_hda_enable_beep_device(codec
, *valp
);
2689 return snd_hda_mixer_amp_switch_put(kcontrol
, ucontrol
);
2691 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put_beep
);
2692 #endif /* CONFIG_SND_HDA_INPUT_BEEP */
2695 * bound volume controls
2697 * bind multiple volumes (# indices, from 0)
2700 #define AMP_VAL_IDX_SHIFT 19
2701 #define AMP_VAL_IDX_MASK (0x0f<<19)
2704 * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control
2706 * The control element is supposed to have the private_value field
2707 * set up via HDA_BIND_MUTE*() macros.
2709 int snd_hda_mixer_bind_switch_get(struct snd_kcontrol
*kcontrol
,
2710 struct snd_ctl_elem_value
*ucontrol
)
2712 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2716 mutex_lock(&codec
->control_mutex
);
2717 pval
= kcontrol
->private_value
;
2718 kcontrol
->private_value
= pval
& ~AMP_VAL_IDX_MASK
; /* index 0 */
2719 err
= snd_hda_mixer_amp_switch_get(kcontrol
, ucontrol
);
2720 kcontrol
->private_value
= pval
;
2721 mutex_unlock(&codec
->control_mutex
);
2724 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_get
);
2727 * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control
2729 * The control element is supposed to have the private_value field
2730 * set up via HDA_BIND_MUTE*() macros.
2732 int snd_hda_mixer_bind_switch_put(struct snd_kcontrol
*kcontrol
,
2733 struct snd_ctl_elem_value
*ucontrol
)
2735 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2737 int i
, indices
, err
= 0, change
= 0;
2739 mutex_lock(&codec
->control_mutex
);
2740 pval
= kcontrol
->private_value
;
2741 indices
= (pval
& AMP_VAL_IDX_MASK
) >> AMP_VAL_IDX_SHIFT
;
2742 for (i
= 0; i
< indices
; i
++) {
2743 kcontrol
->private_value
= (pval
& ~AMP_VAL_IDX_MASK
) |
2744 (i
<< AMP_VAL_IDX_SHIFT
);
2745 err
= snd_hda_mixer_amp_switch_put(kcontrol
, ucontrol
);
2750 kcontrol
->private_value
= pval
;
2751 mutex_unlock(&codec
->control_mutex
);
2752 return err
< 0 ? err
: change
;
2754 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_put
);
2757 * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control
2759 * The control element is supposed to have the private_value field
2760 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2762 int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol
*kcontrol
,
2763 struct snd_ctl_elem_info
*uinfo
)
2765 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2766 struct hda_bind_ctls
*c
;
2769 mutex_lock(&codec
->control_mutex
);
2770 c
= (struct hda_bind_ctls
*)kcontrol
->private_value
;
2771 kcontrol
->private_value
= *c
->values
;
2772 err
= c
->ops
->info(kcontrol
, uinfo
);
2773 kcontrol
->private_value
= (long)c
;
2774 mutex_unlock(&codec
->control_mutex
);
2777 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_info
);
2780 * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control
2782 * The control element is supposed to have the private_value field
2783 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2785 int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol
*kcontrol
,
2786 struct snd_ctl_elem_value
*ucontrol
)
2788 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2789 struct hda_bind_ctls
*c
;
2792 mutex_lock(&codec
->control_mutex
);
2793 c
= (struct hda_bind_ctls
*)kcontrol
->private_value
;
2794 kcontrol
->private_value
= *c
->values
;
2795 err
= c
->ops
->get(kcontrol
, ucontrol
);
2796 kcontrol
->private_value
= (long)c
;
2797 mutex_unlock(&codec
->control_mutex
);
2800 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_get
);
2803 * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control
2805 * The control element is supposed to have the private_value field
2806 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2808 int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol
*kcontrol
,
2809 struct snd_ctl_elem_value
*ucontrol
)
2811 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2812 struct hda_bind_ctls
*c
;
2813 unsigned long *vals
;
2814 int err
= 0, change
= 0;
2816 mutex_lock(&codec
->control_mutex
);
2817 c
= (struct hda_bind_ctls
*)kcontrol
->private_value
;
2818 for (vals
= c
->values
; *vals
; vals
++) {
2819 kcontrol
->private_value
= *vals
;
2820 err
= c
->ops
->put(kcontrol
, ucontrol
);
2825 kcontrol
->private_value
= (long)c
;
2826 mutex_unlock(&codec
->control_mutex
);
2827 return err
< 0 ? err
: change
;
2829 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_put
);
2832 * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control
2834 * The control element is supposed to have the private_value field
2835 * set up via HDA_BIND_VOL() macro.
2837 int snd_hda_mixer_bind_tlv(struct snd_kcontrol
*kcontrol
, int op_flag
,
2838 unsigned int size
, unsigned int __user
*tlv
)
2840 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2841 struct hda_bind_ctls
*c
;
2844 mutex_lock(&codec
->control_mutex
);
2845 c
= (struct hda_bind_ctls
*)kcontrol
->private_value
;
2846 kcontrol
->private_value
= *c
->values
;
2847 err
= c
->ops
->tlv(kcontrol
, op_flag
, size
, tlv
);
2848 kcontrol
->private_value
= (long)c
;
2849 mutex_unlock(&codec
->control_mutex
);
2852 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_tlv
);
2854 struct hda_ctl_ops snd_hda_bind_vol
= {
2855 .info
= snd_hda_mixer_amp_volume_info
,
2856 .get
= snd_hda_mixer_amp_volume_get
,
2857 .put
= snd_hda_mixer_amp_volume_put
,
2858 .tlv
= snd_hda_mixer_amp_tlv
2860 EXPORT_SYMBOL_HDA(snd_hda_bind_vol
);
2862 struct hda_ctl_ops snd_hda_bind_sw
= {
2863 .info
= snd_hda_mixer_amp_switch_info
,
2864 .get
= snd_hda_mixer_amp_switch_get
,
2865 .put
= snd_hda_mixer_amp_switch_put
,
2866 .tlv
= snd_hda_mixer_amp_tlv
2868 EXPORT_SYMBOL_HDA(snd_hda_bind_sw
);
2871 * SPDIF out controls
2874 static int snd_hda_spdif_mask_info(struct snd_kcontrol
*kcontrol
,
2875 struct snd_ctl_elem_info
*uinfo
)
2877 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_IEC958
;
2882 static int snd_hda_spdif_cmask_get(struct snd_kcontrol
*kcontrol
,
2883 struct snd_ctl_elem_value
*ucontrol
)
2885 ucontrol
->value
.iec958
.status
[0] = IEC958_AES0_PROFESSIONAL
|
2886 IEC958_AES0_NONAUDIO
|
2887 IEC958_AES0_CON_EMPHASIS_5015
|
2888 IEC958_AES0_CON_NOT_COPYRIGHT
;
2889 ucontrol
->value
.iec958
.status
[1] = IEC958_AES1_CON_CATEGORY
|
2890 IEC958_AES1_CON_ORIGINAL
;
2894 static int snd_hda_spdif_pmask_get(struct snd_kcontrol
*kcontrol
,
2895 struct snd_ctl_elem_value
*ucontrol
)
2897 ucontrol
->value
.iec958
.status
[0] = IEC958_AES0_PROFESSIONAL
|
2898 IEC958_AES0_NONAUDIO
|
2899 IEC958_AES0_PRO_EMPHASIS_5015
;
2903 static int snd_hda_spdif_default_get(struct snd_kcontrol
*kcontrol
,
2904 struct snd_ctl_elem_value
*ucontrol
)
2906 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2907 int idx
= kcontrol
->private_value
;
2908 struct hda_spdif_out
*spdif
;
2910 mutex_lock(&codec
->spdif_mutex
);
2911 spdif
= snd_array_elem(&codec
->spdif_out
, idx
);
2912 ucontrol
->value
.iec958
.status
[0] = spdif
->status
& 0xff;
2913 ucontrol
->value
.iec958
.status
[1] = (spdif
->status
>> 8) & 0xff;
2914 ucontrol
->value
.iec958
.status
[2] = (spdif
->status
>> 16) & 0xff;
2915 ucontrol
->value
.iec958
.status
[3] = (spdif
->status
>> 24) & 0xff;
2916 mutex_unlock(&codec
->spdif_mutex
);
2921 /* convert from SPDIF status bits to HDA SPDIF bits
2922 * bit 0 (DigEn) is always set zero (to be filled later)
2924 static unsigned short convert_from_spdif_status(unsigned int sbits
)
2926 unsigned short val
= 0;
2928 if (sbits
& IEC958_AES0_PROFESSIONAL
)
2929 val
|= AC_DIG1_PROFESSIONAL
;
2930 if (sbits
& IEC958_AES0_NONAUDIO
)
2931 val
|= AC_DIG1_NONAUDIO
;
2932 if (sbits
& IEC958_AES0_PROFESSIONAL
) {
2933 if ((sbits
& IEC958_AES0_PRO_EMPHASIS
) ==
2934 IEC958_AES0_PRO_EMPHASIS_5015
)
2935 val
|= AC_DIG1_EMPHASIS
;
2937 if ((sbits
& IEC958_AES0_CON_EMPHASIS
) ==
2938 IEC958_AES0_CON_EMPHASIS_5015
)
2939 val
|= AC_DIG1_EMPHASIS
;
2940 if (!(sbits
& IEC958_AES0_CON_NOT_COPYRIGHT
))
2941 val
|= AC_DIG1_COPYRIGHT
;
2942 if (sbits
& (IEC958_AES1_CON_ORIGINAL
<< 8))
2943 val
|= AC_DIG1_LEVEL
;
2944 val
|= sbits
& (IEC958_AES1_CON_CATEGORY
<< 8);
2949 /* convert to SPDIF status bits from HDA SPDIF bits
2951 static unsigned int convert_to_spdif_status(unsigned short val
)
2953 unsigned int sbits
= 0;
2955 if (val
& AC_DIG1_NONAUDIO
)
2956 sbits
|= IEC958_AES0_NONAUDIO
;
2957 if (val
& AC_DIG1_PROFESSIONAL
)
2958 sbits
|= IEC958_AES0_PROFESSIONAL
;
2959 if (sbits
& IEC958_AES0_PROFESSIONAL
) {
2960 if (sbits
& AC_DIG1_EMPHASIS
)
2961 sbits
|= IEC958_AES0_PRO_EMPHASIS_5015
;
2963 if (val
& AC_DIG1_EMPHASIS
)
2964 sbits
|= IEC958_AES0_CON_EMPHASIS_5015
;
2965 if (!(val
& AC_DIG1_COPYRIGHT
))
2966 sbits
|= IEC958_AES0_CON_NOT_COPYRIGHT
;
2967 if (val
& AC_DIG1_LEVEL
)
2968 sbits
|= (IEC958_AES1_CON_ORIGINAL
<< 8);
2969 sbits
|= val
& (0x7f << 8);
2974 /* set digital convert verbs both for the given NID and its slaves */
2975 static void set_dig_out(struct hda_codec
*codec
, hda_nid_t nid
,
2980 snd_hda_codec_write_cache(codec
, nid
, 0, verb
, val
);
2981 d
= codec
->slave_dig_outs
;
2985 snd_hda_codec_write_cache(codec
, *d
, 0, verb
, val
);
2988 static inline void set_dig_out_convert(struct hda_codec
*codec
, hda_nid_t nid
,
2992 set_dig_out(codec
, nid
, AC_VERB_SET_DIGI_CONVERT_1
, dig1
);
2994 set_dig_out(codec
, nid
, AC_VERB_SET_DIGI_CONVERT_2
, dig2
);
2997 static int snd_hda_spdif_default_put(struct snd_kcontrol
*kcontrol
,
2998 struct snd_ctl_elem_value
*ucontrol
)
3000 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
3001 int idx
= kcontrol
->private_value
;
3002 struct hda_spdif_out
*spdif
;
3007 mutex_lock(&codec
->spdif_mutex
);
3008 spdif
= snd_array_elem(&codec
->spdif_out
, idx
);
3010 spdif
->status
= ucontrol
->value
.iec958
.status
[0] |
3011 ((unsigned int)ucontrol
->value
.iec958
.status
[1] << 8) |
3012 ((unsigned int)ucontrol
->value
.iec958
.status
[2] << 16) |
3013 ((unsigned int)ucontrol
->value
.iec958
.status
[3] << 24);
3014 val
= convert_from_spdif_status(spdif
->status
);
3015 val
|= spdif
->ctls
& 1;
3016 change
= spdif
->ctls
!= val
;
3018 if (change
&& nid
!= (u16
)-1)
3019 set_dig_out_convert(codec
, nid
, val
& 0xff, (val
>> 8) & 0xff);
3020 mutex_unlock(&codec
->spdif_mutex
);
3024 #define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
3026 static int snd_hda_spdif_out_switch_get(struct snd_kcontrol
*kcontrol
,
3027 struct snd_ctl_elem_value
*ucontrol
)
3029 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
3030 int idx
= kcontrol
->private_value
;
3031 struct hda_spdif_out
*spdif
;
3033 mutex_lock(&codec
->spdif_mutex
);
3034 spdif
= snd_array_elem(&codec
->spdif_out
, idx
);
3035 ucontrol
->value
.integer
.value
[0] = spdif
->ctls
& AC_DIG1_ENABLE
;
3036 mutex_unlock(&codec
->spdif_mutex
);
3040 static inline void set_spdif_ctls(struct hda_codec
*codec
, hda_nid_t nid
,
3043 set_dig_out_convert(codec
, nid
, dig1
, dig2
);
3044 /* unmute amp switch (if any) */
3045 if ((get_wcaps(codec
, nid
) & AC_WCAP_OUT_AMP
) &&
3046 (dig1
& AC_DIG1_ENABLE
))
3047 snd_hda_codec_amp_stereo(codec
, nid
, HDA_OUTPUT
, 0,
3051 static int snd_hda_spdif_out_switch_put(struct snd_kcontrol
*kcontrol
,
3052 struct snd_ctl_elem_value
*ucontrol
)
3054 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
3055 int idx
= kcontrol
->private_value
;
3056 struct hda_spdif_out
*spdif
;
3061 mutex_lock(&codec
->spdif_mutex
);
3062 spdif
= snd_array_elem(&codec
->spdif_out
, idx
);
3064 val
= spdif
->ctls
& ~AC_DIG1_ENABLE
;
3065 if (ucontrol
->value
.integer
.value
[0])
3066 val
|= AC_DIG1_ENABLE
;
3067 change
= spdif
->ctls
!= val
;
3069 if (change
&& nid
!= (u16
)-1)
3070 set_spdif_ctls(codec
, nid
, val
& 0xff, -1);
3071 mutex_unlock(&codec
->spdif_mutex
);
3075 static struct snd_kcontrol_new dig_mixes
[] = {
3077 .access
= SNDRV_CTL_ELEM_ACCESS_READ
,
3078 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
3079 .name
= SNDRV_CTL_NAME_IEC958("", PLAYBACK
, CON_MASK
),
3080 .info
= snd_hda_spdif_mask_info
,
3081 .get
= snd_hda_spdif_cmask_get
,
3084 .access
= SNDRV_CTL_ELEM_ACCESS_READ
,
3085 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
3086 .name
= SNDRV_CTL_NAME_IEC958("", PLAYBACK
, PRO_MASK
),
3087 .info
= snd_hda_spdif_mask_info
,
3088 .get
= snd_hda_spdif_pmask_get
,
3091 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
3092 .name
= SNDRV_CTL_NAME_IEC958("", PLAYBACK
, DEFAULT
),
3093 .info
= snd_hda_spdif_mask_info
,
3094 .get
= snd_hda_spdif_default_get
,
3095 .put
= snd_hda_spdif_default_put
,
3098 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
3099 .name
= SNDRV_CTL_NAME_IEC958("", PLAYBACK
, SWITCH
),
3100 .info
= snd_hda_spdif_out_switch_info
,
3101 .get
= snd_hda_spdif_out_switch_get
,
3102 .put
= snd_hda_spdif_out_switch_put
,
3108 * snd_hda_create_spdif_out_ctls - create Output SPDIF-related controls
3109 * @codec: the HDA codec
3110 * @nid: audio out widget NID
3112 * Creates controls related with the SPDIF output.
3113 * Called from each patch supporting the SPDIF out.
3115 * Returns 0 if successful, or a negative error code.
3117 int snd_hda_create_spdif_out_ctls(struct hda_codec
*codec
,
3118 hda_nid_t associated_nid
,
3122 struct snd_kcontrol
*kctl
;
3123 struct snd_kcontrol_new
*dig_mix
;
3125 struct hda_spdif_out
*spdif
;
3127 idx
= find_empty_mixer_ctl_idx(codec
, "IEC958 Playback Switch");
3129 printk(KERN_ERR
"hda_codec: too many IEC958 outputs\n");
3132 spdif
= snd_array_new(&codec
->spdif_out
);
3133 for (dig_mix
= dig_mixes
; dig_mix
->name
; dig_mix
++) {
3134 kctl
= snd_ctl_new1(dig_mix
, codec
);
3137 kctl
->id
.index
= idx
;
3138 kctl
->private_value
= codec
->spdif_out
.used
- 1;
3139 err
= snd_hda_ctl_add(codec
, associated_nid
, kctl
);
3143 spdif
->nid
= cvt_nid
;
3144 spdif
->ctls
= snd_hda_codec_read(codec
, cvt_nid
, 0,
3145 AC_VERB_GET_DIGI_CONVERT_1
, 0);
3146 spdif
->status
= convert_to_spdif_status(spdif
->ctls
);
3149 EXPORT_SYMBOL_HDA(snd_hda_create_spdif_out_ctls
);
3151 /* get the hda_spdif_out entry from the given NID
3152 * call within spdif_mutex lock
3154 struct hda_spdif_out
*snd_hda_spdif_out_of_nid(struct hda_codec
*codec
,
3158 for (i
= 0; i
< codec
->spdif_out
.used
; i
++) {
3159 struct hda_spdif_out
*spdif
=
3160 snd_array_elem(&codec
->spdif_out
, i
);
3161 if (spdif
->nid
== nid
)
3166 EXPORT_SYMBOL_HDA(snd_hda_spdif_out_of_nid
);
3168 void snd_hda_spdif_ctls_unassign(struct hda_codec
*codec
, int idx
)
3170 struct hda_spdif_out
*spdif
;
3172 mutex_lock(&codec
->spdif_mutex
);
3173 spdif
= snd_array_elem(&codec
->spdif_out
, idx
);
3174 spdif
->nid
= (u16
)-1;
3175 mutex_unlock(&codec
->spdif_mutex
);
3177 EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_unassign
);
3179 void snd_hda_spdif_ctls_assign(struct hda_codec
*codec
, int idx
, hda_nid_t nid
)
3181 struct hda_spdif_out
*spdif
;
3184 mutex_lock(&codec
->spdif_mutex
);
3185 spdif
= snd_array_elem(&codec
->spdif_out
, idx
);
3186 if (spdif
->nid
!= nid
) {
3189 set_spdif_ctls(codec
, nid
, val
& 0xff, (val
>> 8) & 0xff);
3191 mutex_unlock(&codec
->spdif_mutex
);
3193 EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_assign
);
3196 * SPDIF sharing with analog output
3198 static int spdif_share_sw_get(struct snd_kcontrol
*kcontrol
,
3199 struct snd_ctl_elem_value
*ucontrol
)
3201 struct hda_multi_out
*mout
= snd_kcontrol_chip(kcontrol
);
3202 ucontrol
->value
.integer
.value
[0] = mout
->share_spdif
;
3206 static int spdif_share_sw_put(struct snd_kcontrol
*kcontrol
,
3207 struct snd_ctl_elem_value
*ucontrol
)
3209 struct hda_multi_out
*mout
= snd_kcontrol_chip(kcontrol
);
3210 mout
->share_spdif
= !!ucontrol
->value
.integer
.value
[0];
3214 static struct snd_kcontrol_new spdif_share_sw
= {
3215 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
3216 .name
= "IEC958 Default PCM Playback Switch",
3217 .info
= snd_ctl_boolean_mono_info
,
3218 .get
= spdif_share_sw_get
,
3219 .put
= spdif_share_sw_put
,
3223 * snd_hda_create_spdif_share_sw - create Default PCM switch
3224 * @codec: the HDA codec
3225 * @mout: multi-out instance
3227 int snd_hda_create_spdif_share_sw(struct hda_codec
*codec
,
3228 struct hda_multi_out
*mout
)
3230 if (!mout
->dig_out_nid
)
3232 /* ATTENTION: here mout is passed as private_data, instead of codec */
3233 return snd_hda_ctl_add(codec
, mout
->dig_out_nid
,
3234 snd_ctl_new1(&spdif_share_sw
, mout
));
3236 EXPORT_SYMBOL_HDA(snd_hda_create_spdif_share_sw
);
3242 #define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
3244 static int snd_hda_spdif_in_switch_get(struct snd_kcontrol
*kcontrol
,
3245 struct snd_ctl_elem_value
*ucontrol
)
3247 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
3249 ucontrol
->value
.integer
.value
[0] = codec
->spdif_in_enable
;
3253 static int snd_hda_spdif_in_switch_put(struct snd_kcontrol
*kcontrol
,
3254 struct snd_ctl_elem_value
*ucontrol
)
3256 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
3257 hda_nid_t nid
= kcontrol
->private_value
;
3258 unsigned int val
= !!ucontrol
->value
.integer
.value
[0];
3261 mutex_lock(&codec
->spdif_mutex
);
3262 change
= codec
->spdif_in_enable
!= val
;
3264 codec
->spdif_in_enable
= val
;
3265 snd_hda_codec_write_cache(codec
, nid
, 0,
3266 AC_VERB_SET_DIGI_CONVERT_1
, val
);
3268 mutex_unlock(&codec
->spdif_mutex
);
3272 static int snd_hda_spdif_in_status_get(struct snd_kcontrol
*kcontrol
,
3273 struct snd_ctl_elem_value
*ucontrol
)
3275 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
3276 hda_nid_t nid
= kcontrol
->private_value
;
3280 val
= snd_hda_codec_read(codec
, nid
, 0, AC_VERB_GET_DIGI_CONVERT_1
, 0);
3281 sbits
= convert_to_spdif_status(val
);
3282 ucontrol
->value
.iec958
.status
[0] = sbits
;
3283 ucontrol
->value
.iec958
.status
[1] = sbits
>> 8;
3284 ucontrol
->value
.iec958
.status
[2] = sbits
>> 16;
3285 ucontrol
->value
.iec958
.status
[3] = sbits
>> 24;
3289 static struct snd_kcontrol_new dig_in_ctls
[] = {
3291 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
3292 .name
= SNDRV_CTL_NAME_IEC958("", CAPTURE
, SWITCH
),
3293 .info
= snd_hda_spdif_in_switch_info
,
3294 .get
= snd_hda_spdif_in_switch_get
,
3295 .put
= snd_hda_spdif_in_switch_put
,
3298 .access
= SNDRV_CTL_ELEM_ACCESS_READ
,
3299 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
3300 .name
= SNDRV_CTL_NAME_IEC958("", CAPTURE
, DEFAULT
),
3301 .info
= snd_hda_spdif_mask_info
,
3302 .get
= snd_hda_spdif_in_status_get
,
3308 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
3309 * @codec: the HDA codec
3310 * @nid: audio in widget NID
3312 * Creates controls related with the SPDIF input.
3313 * Called from each patch supporting the SPDIF in.
3315 * Returns 0 if successful, or a negative error code.
3317 int snd_hda_create_spdif_in_ctls(struct hda_codec
*codec
, hda_nid_t nid
)
3320 struct snd_kcontrol
*kctl
;
3321 struct snd_kcontrol_new
*dig_mix
;
3324 idx
= find_empty_mixer_ctl_idx(codec
, "IEC958 Capture Switch");
3326 printk(KERN_ERR
"hda_codec: too many IEC958 inputs\n");
3329 for (dig_mix
= dig_in_ctls
; dig_mix
->name
; dig_mix
++) {
3330 kctl
= snd_ctl_new1(dig_mix
, codec
);
3333 kctl
->private_value
= nid
;
3334 err
= snd_hda_ctl_add(codec
, nid
, kctl
);
3338 codec
->spdif_in_enable
=
3339 snd_hda_codec_read(codec
, nid
, 0,
3340 AC_VERB_GET_DIGI_CONVERT_1
, 0) &
3344 EXPORT_SYMBOL_HDA(snd_hda_create_spdif_in_ctls
);
3351 /* build a 32bit cache key with the widget id and the command parameter */
3352 #define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
3353 #define get_cmd_cache_nid(key) ((key) & 0xff)
3354 #define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
3357 * snd_hda_codec_write_cache - send a single command with caching
3358 * @codec: the HDA codec
3359 * @nid: NID to send the command
3360 * @direct: direct flag
3361 * @verb: the verb to send
3362 * @parm: the parameter for the verb
3364 * Send a single command without waiting for response.
3366 * Returns 0 if successful, or a negative error code.
3368 int snd_hda_codec_write_cache(struct hda_codec
*codec
, hda_nid_t nid
,
3369 int direct
, unsigned int verb
, unsigned int parm
)
3371 int err
= snd_hda_codec_write(codec
, nid
, direct
, verb
, parm
);
3372 struct hda_cache_head
*c
;
3377 /* parm may contain the verb stuff for get/set amp */
3378 verb
= verb
| (parm
>> 8);
3380 key
= build_cmd_cache_key(nid
, verb
);
3381 mutex_lock(&codec
->bus
->cmd_mutex
);
3382 c
= get_alloc_hash(&codec
->cmd_cache
, key
);
3385 mutex_unlock(&codec
->bus
->cmd_mutex
);
3388 EXPORT_SYMBOL_HDA(snd_hda_codec_write_cache
);
3391 * snd_hda_codec_update_cache - check cache and write the cmd only when needed
3392 * @codec: the HDA codec
3393 * @nid: NID to send the command
3394 * @direct: direct flag
3395 * @verb: the verb to send
3396 * @parm: the parameter for the verb
3398 * This function works like snd_hda_codec_write_cache(), but it doesn't send
3399 * command if the parameter is already identical with the cached value.
3400 * If not, it sends the command and refreshes the cache.
3402 * Returns 0 if successful, or a negative error code.
3404 int snd_hda_codec_update_cache(struct hda_codec
*codec
, hda_nid_t nid
,
3405 int direct
, unsigned int verb
, unsigned int parm
)
3407 struct hda_cache_head
*c
;
3410 /* parm may contain the verb stuff for get/set amp */
3411 verb
= verb
| (parm
>> 8);
3413 key
= build_cmd_cache_key(nid
, verb
);
3414 mutex_lock(&codec
->bus
->cmd_mutex
);
3415 c
= get_hash(&codec
->cmd_cache
, key
);
3416 if (c
&& c
->val
== parm
) {
3417 mutex_unlock(&codec
->bus
->cmd_mutex
);
3420 mutex_unlock(&codec
->bus
->cmd_mutex
);
3421 return snd_hda_codec_write_cache(codec
, nid
, direct
, verb
, parm
);
3423 EXPORT_SYMBOL_HDA(snd_hda_codec_update_cache
);
3426 * snd_hda_codec_resume_cache - Resume the all commands from the cache
3427 * @codec: HD-audio codec
3429 * Execute all verbs recorded in the command caches to resume.
3431 void snd_hda_codec_resume_cache(struct hda_codec
*codec
)
3433 struct hda_cache_head
*buffer
= codec
->cmd_cache
.buf
.list
;
3436 for (i
= 0; i
< codec
->cmd_cache
.buf
.used
; i
++, buffer
++) {
3437 u32 key
= buffer
->key
;
3440 snd_hda_codec_write(codec
, get_cmd_cache_nid(key
), 0,
3441 get_cmd_cache_cmd(key
), buffer
->val
);
3444 EXPORT_SYMBOL_HDA(snd_hda_codec_resume_cache
);
3447 * snd_hda_sequence_write_cache - sequence writes with caching
3448 * @codec: the HDA codec
3449 * @seq: VERB array to send
3451 * Send the commands sequentially from the given array.
3452 * Thte commands are recorded on cache for power-save and resume.
3453 * The array must be terminated with NID=0.
3455 void snd_hda_sequence_write_cache(struct hda_codec
*codec
,
3456 const struct hda_verb
*seq
)
3458 for (; seq
->nid
; seq
++)
3459 snd_hda_codec_write_cache(codec
, seq
->nid
, 0, seq
->verb
,
3462 EXPORT_SYMBOL_HDA(snd_hda_sequence_write_cache
);
3463 #endif /* CONFIG_PM */
3465 void snd_hda_codec_set_power_to_all(struct hda_codec
*codec
, hda_nid_t fg
,
3466 unsigned int power_state
,
3467 bool eapd_workaround
)
3469 hda_nid_t nid
= codec
->start_nid
;
3472 for (i
= 0; i
< codec
->num_nodes
; i
++, nid
++) {
3473 unsigned int wcaps
= get_wcaps(codec
, nid
);
3474 if (!(wcaps
& AC_WCAP_POWER
))
3476 /* don't power down the widget if it controls eapd and
3477 * EAPD_BTLENABLE is set.
3479 if (eapd_workaround
&& power_state
== AC_PWRST_D3
&&
3480 get_wcaps_type(wcaps
) == AC_WID_PIN
&&
3481 (snd_hda_query_pin_caps(codec
, nid
) & AC_PINCAP_EAPD
)) {
3482 int eapd
= snd_hda_codec_read(codec
, nid
, 0,
3483 AC_VERB_GET_EAPD_BTLENABLE
, 0);
3487 snd_hda_codec_write(codec
, nid
, 0, AC_VERB_SET_POWER_STATE
,
3491 if (power_state
== AC_PWRST_D0
) {
3492 unsigned long end_time
;
3494 /* wait until the codec reachs to D0 */
3495 end_time
= jiffies
+ msecs_to_jiffies(500);
3497 state
= snd_hda_codec_read(codec
, fg
, 0,
3498 AC_VERB_GET_POWER_STATE
, 0);
3499 if (state
== power_state
)
3502 } while (time_after_eq(end_time
, jiffies
));
3505 EXPORT_SYMBOL_HDA(snd_hda_codec_set_power_to_all
);
3508 * set power state of the codec
3510 static void hda_set_power_state(struct hda_codec
*codec
, hda_nid_t fg
,
3511 unsigned int power_state
)
3513 if (codec
->patch_ops
.set_power_state
) {
3514 codec
->patch_ops
.set_power_state(codec
, fg
, power_state
);
3518 /* this delay seems necessary to avoid click noise at power-down */
3519 if (power_state
== AC_PWRST_D3
)
3521 snd_hda_codec_read(codec
, fg
, 0, AC_VERB_SET_POWER_STATE
,
3523 snd_hda_codec_set_power_to_all(codec
, fg
, power_state
, true);
3526 #ifdef CONFIG_SND_HDA_HWDEP
3527 /* execute additional init verbs */
3528 static void hda_exec_init_verbs(struct hda_codec
*codec
)
3530 if (codec
->init_verbs
.list
)
3531 snd_hda_sequence_write(codec
, codec
->init_verbs
.list
);
3534 static inline void hda_exec_init_verbs(struct hda_codec
*codec
) {}
3539 * call suspend and power-down; used both from PM and power-save
3541 static void hda_call_codec_suspend(struct hda_codec
*codec
)
3543 if (codec
->patch_ops
.suspend
)
3544 codec
->patch_ops
.suspend(codec
, PMSG_SUSPEND
);
3545 hda_cleanup_all_streams(codec
);
3546 hda_set_power_state(codec
,
3547 codec
->afg
? codec
->afg
: codec
->mfg
,
3549 #ifdef CONFIG_SND_HDA_POWER_SAVE
3550 cancel_delayed_work(&codec
->power_work
);
3551 spin_lock(&codec
->power_lock
);
3552 snd_hda_update_power_acct(codec
);
3553 trace_hda_power_down(codec
);
3554 codec
->power_on
= 0;
3555 codec
->power_transition
= 0;
3556 codec
->power_jiffies
= jiffies
;
3557 spin_unlock(&codec
->power_lock
);
3562 * kick up codec; used both from PM and power-save
3564 static void hda_call_codec_resume(struct hda_codec
*codec
)
3566 /* set as if powered on for avoiding re-entering the resume
3567 * in the resume / power-save sequence
3569 hda_keep_power_on(codec
);
3570 hda_set_power_state(codec
,
3571 codec
->afg
? codec
->afg
: codec
->mfg
,
3573 restore_pincfgs(codec
); /* restore all current pin configs */
3574 restore_shutup_pins(codec
);
3575 hda_exec_init_verbs(codec
);
3576 snd_hda_jack_set_dirty_all(codec
);
3577 if (codec
->patch_ops
.resume
)
3578 codec
->patch_ops
.resume(codec
);
3580 if (codec
->patch_ops
.init
)
3581 codec
->patch_ops
.init(codec
);
3582 snd_hda_codec_resume_amp(codec
);
3583 snd_hda_codec_resume_cache(codec
);
3585 snd_hda_power_down(codec
); /* flag down before returning */
3587 #endif /* CONFIG_PM */
3591 * snd_hda_build_controls - build mixer controls
3594 * Creates mixer controls for each codec included in the bus.
3596 * Returns 0 if successful, otherwise a negative error code.
3598 int /*__devinit*/ snd_hda_build_controls(struct hda_bus
*bus
)
3600 struct hda_codec
*codec
;
3602 list_for_each_entry(codec
, &bus
->codec_list
, list
) {
3603 int err
= snd_hda_codec_build_controls(codec
);
3605 printk(KERN_ERR
"hda_codec: cannot build controls "
3606 "for #%d (error %d)\n", codec
->addr
, err
);
3607 err
= snd_hda_codec_reset(codec
);
3610 "hda_codec: cannot revert codec\n");
3617 EXPORT_SYMBOL_HDA(snd_hda_build_controls
);
3619 int snd_hda_codec_build_controls(struct hda_codec
*codec
)
3622 hda_exec_init_verbs(codec
);
3623 /* continue to initialize... */
3624 if (codec
->patch_ops
.init
)
3625 err
= codec
->patch_ops
.init(codec
);
3626 if (!err
&& codec
->patch_ops
.build_controls
)
3627 err
= codec
->patch_ops
.build_controls(codec
);
3636 struct hda_rate_tbl
{
3638 unsigned int alsa_bits
;
3639 unsigned int hda_fmt
;
3642 /* rate = base * mult / div */
3643 #define HDA_RATE(base, mult, div) \
3644 (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \
3645 (((div) - 1) << AC_FMT_DIV_SHIFT))
3647 static struct hda_rate_tbl rate_bits
[] = {
3648 /* rate in Hz, ALSA rate bitmask, HDA format value */
3650 /* autodetected value used in snd_hda_query_supported_pcm */
3651 { 8000, SNDRV_PCM_RATE_8000
, HDA_RATE(48, 1, 6) },
3652 { 11025, SNDRV_PCM_RATE_11025
, HDA_RATE(44, 1, 4) },
3653 { 16000, SNDRV_PCM_RATE_16000
, HDA_RATE(48, 1, 3) },
3654 { 22050, SNDRV_PCM_RATE_22050
, HDA_RATE(44, 1, 2) },
3655 { 32000, SNDRV_PCM_RATE_32000
, HDA_RATE(48, 2, 3) },
3656 { 44100, SNDRV_PCM_RATE_44100
, HDA_RATE(44, 1, 1) },
3657 { 48000, SNDRV_PCM_RATE_48000
, HDA_RATE(48, 1, 1) },
3658 { 88200, SNDRV_PCM_RATE_88200
, HDA_RATE(44, 2, 1) },
3659 { 96000, SNDRV_PCM_RATE_96000
, HDA_RATE(48, 2, 1) },
3660 { 176400, SNDRV_PCM_RATE_176400
, HDA_RATE(44, 4, 1) },
3661 { 192000, SNDRV_PCM_RATE_192000
, HDA_RATE(48, 4, 1) },
3662 #define AC_PAR_PCM_RATE_BITS 11
3663 /* up to bits 10, 384kHZ isn't supported properly */
3665 /* not autodetected value */
3666 { 9600, SNDRV_PCM_RATE_KNOT
, HDA_RATE(48, 1, 5) },
3668 { 0 } /* terminator */
3672 * snd_hda_calc_stream_format - calculate format bitset
3673 * @rate: the sample rate
3674 * @channels: the number of channels
3675 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
3676 * @maxbps: the max. bps
3678 * Calculate the format bitset from the given rate, channels and th PCM format.
3680 * Return zero if invalid.
3682 unsigned int snd_hda_calc_stream_format(unsigned int rate
,
3683 unsigned int channels
,
3684 unsigned int format
,
3685 unsigned int maxbps
,
3686 unsigned short spdif_ctls
)
3689 unsigned int val
= 0;
3691 for (i
= 0; rate_bits
[i
].hz
; i
++)
3692 if (rate_bits
[i
].hz
== rate
) {
3693 val
= rate_bits
[i
].hda_fmt
;
3696 if (!rate_bits
[i
].hz
) {
3697 snd_printdd("invalid rate %d\n", rate
);
3701 if (channels
== 0 || channels
> 8) {
3702 snd_printdd("invalid channels %d\n", channels
);
3705 val
|= channels
- 1;
3707 switch (snd_pcm_format_width(format
)) {
3709 val
|= AC_FMT_BITS_8
;
3712 val
|= AC_FMT_BITS_16
;
3717 if (maxbps
>= 32 || format
== SNDRV_PCM_FORMAT_FLOAT_LE
)
3718 val
|= AC_FMT_BITS_32
;
3719 else if (maxbps
>= 24)
3720 val
|= AC_FMT_BITS_24
;
3722 val
|= AC_FMT_BITS_20
;
3725 snd_printdd("invalid format width %d\n",
3726 snd_pcm_format_width(format
));
3730 if (spdif_ctls
& AC_DIG1_NONAUDIO
)
3731 val
|= AC_FMT_TYPE_NON_PCM
;
3735 EXPORT_SYMBOL_HDA(snd_hda_calc_stream_format
);
3737 static unsigned int get_pcm_param(struct hda_codec
*codec
, hda_nid_t nid
,
3740 unsigned int val
= 0;
3741 if (nid
!= codec
->afg
&&
3742 (get_wcaps(codec
, nid
) & AC_WCAP_FORMAT_OVRD
))
3743 val
= snd_hda_param_read(codec
, nid
, AC_PAR_PCM
);
3744 if (!val
|| val
== -1)
3745 val
= snd_hda_param_read(codec
, codec
->afg
, AC_PAR_PCM
);
3746 if (!val
|| val
== -1)
3751 static unsigned int query_pcm_param(struct hda_codec
*codec
, hda_nid_t nid
)
3753 return query_caps_hash(codec
, nid
, 0, HDA_HASH_PARPCM_KEY(nid
),
3757 static unsigned int get_stream_param(struct hda_codec
*codec
, hda_nid_t nid
,
3760 unsigned int streams
= snd_hda_param_read(codec
, nid
, AC_PAR_STREAM
);
3761 if (!streams
|| streams
== -1)
3762 streams
= snd_hda_param_read(codec
, codec
->afg
, AC_PAR_STREAM
);
3763 if (!streams
|| streams
== -1)
3768 static unsigned int query_stream_param(struct hda_codec
*codec
, hda_nid_t nid
)
3770 return query_caps_hash(codec
, nid
, 0, HDA_HASH_PARSTR_KEY(nid
),
3775 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
3776 * @codec: the HDA codec
3777 * @nid: NID to query
3778 * @ratesp: the pointer to store the detected rate bitflags
3779 * @formatsp: the pointer to store the detected formats
3780 * @bpsp: the pointer to store the detected format widths
3782 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
3783 * or @bsps argument is ignored.
3785 * Returns 0 if successful, otherwise a negative error code.
3787 int snd_hda_query_supported_pcm(struct hda_codec
*codec
, hda_nid_t nid
,
3788 u32
*ratesp
, u64
*formatsp
, unsigned int *bpsp
)
3790 unsigned int i
, val
, wcaps
;
3792 wcaps
= get_wcaps(codec
, nid
);
3793 val
= query_pcm_param(codec
, nid
);
3797 for (i
= 0; i
< AC_PAR_PCM_RATE_BITS
; i
++) {
3799 rates
|= rate_bits
[i
].alsa_bits
;
3802 snd_printk(KERN_ERR
"hda_codec: rates == 0 "
3803 "(nid=0x%x, val=0x%x, ovrd=%i)\n",
3805 (wcaps
& AC_WCAP_FORMAT_OVRD
) ? 1 : 0);
3811 if (formatsp
|| bpsp
) {
3813 unsigned int streams
, bps
;
3815 streams
= query_stream_param(codec
, nid
);
3820 if (streams
& AC_SUPFMT_PCM
) {
3821 if (val
& AC_SUPPCM_BITS_8
) {
3822 formats
|= SNDRV_PCM_FMTBIT_U8
;
3825 if (val
& AC_SUPPCM_BITS_16
) {
3826 formats
|= SNDRV_PCM_FMTBIT_S16_LE
;
3829 if (wcaps
& AC_WCAP_DIGITAL
) {
3830 if (val
& AC_SUPPCM_BITS_32
)
3831 formats
|= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
;
3832 if (val
& (AC_SUPPCM_BITS_20
|AC_SUPPCM_BITS_24
))
3833 formats
|= SNDRV_PCM_FMTBIT_S32_LE
;
3834 if (val
& AC_SUPPCM_BITS_24
)
3836 else if (val
& AC_SUPPCM_BITS_20
)
3838 } else if (val
& (AC_SUPPCM_BITS_20
|AC_SUPPCM_BITS_24
|
3839 AC_SUPPCM_BITS_32
)) {
3840 formats
|= SNDRV_PCM_FMTBIT_S32_LE
;
3841 if (val
& AC_SUPPCM_BITS_32
)
3843 else if (val
& AC_SUPPCM_BITS_24
)
3845 else if (val
& AC_SUPPCM_BITS_20
)
3849 #if 0 /* FIXME: CS4206 doesn't work, which is the only codec supporting float */
3850 if (streams
& AC_SUPFMT_FLOAT32
) {
3851 formats
|= SNDRV_PCM_FMTBIT_FLOAT_LE
;
3856 if (streams
== AC_SUPFMT_AC3
) {
3857 /* should be exclusive */
3858 /* temporary hack: we have still no proper support
3859 * for the direct AC3 stream...
3861 formats
|= SNDRV_PCM_FMTBIT_U8
;
3865 snd_printk(KERN_ERR
"hda_codec: formats == 0 "
3866 "(nid=0x%x, val=0x%x, ovrd=%i, "
3869 (wcaps
& AC_WCAP_FORMAT_OVRD
) ? 1 : 0,
3874 *formatsp
= formats
;
3881 EXPORT_SYMBOL_HDA(snd_hda_query_supported_pcm
);
3884 * snd_hda_is_supported_format - Check the validity of the format
3885 * @codec: HD-audio codec
3886 * @nid: NID to check
3887 * @format: the HD-audio format value to check
3889 * Check whether the given node supports the format value.
3891 * Returns 1 if supported, 0 if not.
3893 int snd_hda_is_supported_format(struct hda_codec
*codec
, hda_nid_t nid
,
3894 unsigned int format
)
3897 unsigned int val
= 0, rate
, stream
;
3899 val
= query_pcm_param(codec
, nid
);
3903 rate
= format
& 0xff00;
3904 for (i
= 0; i
< AC_PAR_PCM_RATE_BITS
; i
++)
3905 if (rate_bits
[i
].hda_fmt
== rate
) {
3910 if (i
>= AC_PAR_PCM_RATE_BITS
)
3913 stream
= query_stream_param(codec
, nid
);
3917 if (stream
& AC_SUPFMT_PCM
) {
3918 switch (format
& 0xf0) {
3920 if (!(val
& AC_SUPPCM_BITS_8
))
3924 if (!(val
& AC_SUPPCM_BITS_16
))
3928 if (!(val
& AC_SUPPCM_BITS_20
))
3932 if (!(val
& AC_SUPPCM_BITS_24
))
3936 if (!(val
& AC_SUPPCM_BITS_32
))
3943 /* FIXME: check for float32 and AC3? */
3948 EXPORT_SYMBOL_HDA(snd_hda_is_supported_format
);
3953 static int hda_pcm_default_open_close(struct hda_pcm_stream
*hinfo
,
3954 struct hda_codec
*codec
,
3955 struct snd_pcm_substream
*substream
)
3960 static int hda_pcm_default_prepare(struct hda_pcm_stream
*hinfo
,
3961 struct hda_codec
*codec
,
3962 unsigned int stream_tag
,
3963 unsigned int format
,
3964 struct snd_pcm_substream
*substream
)
3966 snd_hda_codec_setup_stream(codec
, hinfo
->nid
, stream_tag
, 0, format
);
3970 static int hda_pcm_default_cleanup(struct hda_pcm_stream
*hinfo
,
3971 struct hda_codec
*codec
,
3972 struct snd_pcm_substream
*substream
)
3974 snd_hda_codec_cleanup_stream(codec
, hinfo
->nid
);
3978 static int set_pcm_default_values(struct hda_codec
*codec
,
3979 struct hda_pcm_stream
*info
)
3983 /* query support PCM information from the given NID */
3984 if (info
->nid
&& (!info
->rates
|| !info
->formats
)) {
3985 err
= snd_hda_query_supported_pcm(codec
, info
->nid
,
3986 info
->rates
? NULL
: &info
->rates
,
3987 info
->formats
? NULL
: &info
->formats
,
3988 info
->maxbps
? NULL
: &info
->maxbps
);
3992 if (info
->ops
.open
== NULL
)
3993 info
->ops
.open
= hda_pcm_default_open_close
;
3994 if (info
->ops
.close
== NULL
)
3995 info
->ops
.close
= hda_pcm_default_open_close
;
3996 if (info
->ops
.prepare
== NULL
) {
3997 if (snd_BUG_ON(!info
->nid
))
3999 info
->ops
.prepare
= hda_pcm_default_prepare
;
4001 if (info
->ops
.cleanup
== NULL
) {
4002 if (snd_BUG_ON(!info
->nid
))
4004 info
->ops
.cleanup
= hda_pcm_default_cleanup
;
4010 * codec prepare/cleanup entries
4012 int snd_hda_codec_prepare(struct hda_codec
*codec
,
4013 struct hda_pcm_stream
*hinfo
,
4014 unsigned int stream
,
4015 unsigned int format
,
4016 struct snd_pcm_substream
*substream
)
4019 mutex_lock(&codec
->bus
->prepare_mutex
);
4020 ret
= hinfo
->ops
.prepare(hinfo
, codec
, stream
, format
, substream
);
4022 purify_inactive_streams(codec
);
4023 mutex_unlock(&codec
->bus
->prepare_mutex
);
4026 EXPORT_SYMBOL_HDA(snd_hda_codec_prepare
);
4028 void snd_hda_codec_cleanup(struct hda_codec
*codec
,
4029 struct hda_pcm_stream
*hinfo
,
4030 struct snd_pcm_substream
*substream
)
4032 mutex_lock(&codec
->bus
->prepare_mutex
);
4033 hinfo
->ops
.cleanup(hinfo
, codec
, substream
);
4034 mutex_unlock(&codec
->bus
->prepare_mutex
);
4036 EXPORT_SYMBOL_HDA(snd_hda_codec_cleanup
);
4039 const char *snd_hda_pcm_type_name
[HDA_PCM_NTYPES
] = {
4040 "Audio", "SPDIF", "HDMI", "Modem"
4044 * get the empty PCM device number to assign
4046 * note the max device number is limited by HDA_MAX_PCMS, currently 10
4048 static int get_empty_pcm_device(struct hda_bus
*bus
, int type
)
4050 /* audio device indices; not linear to keep compatibility */
4051 static int audio_idx
[HDA_PCM_NTYPES
][5] = {
4052 [HDA_PCM_TYPE_AUDIO
] = { 0, 2, 4, 5, -1 },
4053 [HDA_PCM_TYPE_SPDIF
] = { 1, -1 },
4054 [HDA_PCM_TYPE_HDMI
] = { 3, 7, 8, 9, -1 },
4055 [HDA_PCM_TYPE_MODEM
] = { 6, -1 },
4059 if (type
>= HDA_PCM_NTYPES
) {
4060 snd_printk(KERN_WARNING
"Invalid PCM type %d\n", type
);
4064 for (i
= 0; audio_idx
[type
][i
] >= 0 ; i
++)
4065 if (!test_and_set_bit(audio_idx
[type
][i
], bus
->pcm_dev_bits
))
4066 return audio_idx
[type
][i
];
4068 /* non-fixed slots starting from 10 */
4069 for (i
= 10; i
< 32; i
++) {
4070 if (!test_and_set_bit(i
, bus
->pcm_dev_bits
))
4074 snd_printk(KERN_WARNING
"Too many %s devices\n",
4075 snd_hda_pcm_type_name
[type
]);
4080 * attach a new PCM stream
4082 static int snd_hda_attach_pcm(struct hda_codec
*codec
, struct hda_pcm
*pcm
)
4084 struct hda_bus
*bus
= codec
->bus
;
4085 struct hda_pcm_stream
*info
;
4088 if (snd_BUG_ON(!pcm
->name
))
4090 for (stream
= 0; stream
< 2; stream
++) {
4091 info
= &pcm
->stream
[stream
];
4092 if (info
->substreams
) {
4093 err
= set_pcm_default_values(codec
, info
);
4098 return bus
->ops
.attach_pcm(bus
, codec
, pcm
);
4101 /* assign all PCMs of the given codec */
4102 int snd_hda_codec_build_pcms(struct hda_codec
*codec
)
4107 if (!codec
->num_pcms
) {
4108 if (!codec
->patch_ops
.build_pcms
)
4110 err
= codec
->patch_ops
.build_pcms(codec
);
4112 printk(KERN_ERR
"hda_codec: cannot build PCMs"
4113 "for #%d (error %d)\n", codec
->addr
, err
);
4114 err
= snd_hda_codec_reset(codec
);
4117 "hda_codec: cannot revert codec\n");
4122 for (pcm
= 0; pcm
< codec
->num_pcms
; pcm
++) {
4123 struct hda_pcm
*cpcm
= &codec
->pcm_info
[pcm
];
4126 if (!cpcm
->stream
[0].substreams
&& !cpcm
->stream
[1].substreams
)
4127 continue; /* no substreams assigned */
4130 dev
= get_empty_pcm_device(codec
->bus
, cpcm
->pcm_type
);
4132 continue; /* no fatal error */
4134 err
= snd_hda_attach_pcm(codec
, cpcm
);
4136 printk(KERN_ERR
"hda_codec: cannot attach "
4137 "PCM stream %d for codec #%d\n",
4139 continue; /* no fatal error */
4147 * snd_hda_build_pcms - build PCM information
4150 * Create PCM information for each codec included in the bus.
4152 * The build_pcms codec patch is requested to set up codec->num_pcms and
4153 * codec->pcm_info properly. The array is referred by the top-level driver
4154 * to create its PCM instances.
4155 * The allocated codec->pcm_info should be released in codec->patch_ops.free
4158 * At least, substreams, channels_min and channels_max must be filled for
4159 * each stream. substreams = 0 indicates that the stream doesn't exist.
4160 * When rates and/or formats are zero, the supported values are queried
4161 * from the given nid. The nid is used also by the default ops.prepare
4162 * and ops.cleanup callbacks.
4164 * The driver needs to call ops.open in its open callback. Similarly,
4165 * ops.close is supposed to be called in the close callback.
4166 * ops.prepare should be called in the prepare or hw_params callback
4167 * with the proper parameters for set up.
4168 * ops.cleanup should be called in hw_free for clean up of streams.
4170 * This function returns 0 if successful, or a negative error code.
4172 int __devinit
snd_hda_build_pcms(struct hda_bus
*bus
)
4174 struct hda_codec
*codec
;
4176 list_for_each_entry(codec
, &bus
->codec_list
, list
) {
4177 int err
= snd_hda_codec_build_pcms(codec
);
4183 EXPORT_SYMBOL_HDA(snd_hda_build_pcms
);
4186 * snd_hda_check_board_config - compare the current codec with the config table
4187 * @codec: the HDA codec
4188 * @num_configs: number of config enums
4189 * @models: array of model name strings
4190 * @tbl: configuration table, terminated by null entries
4192 * Compares the modelname or PCI subsystem id of the current codec with the
4193 * given configuration table. If a matching entry is found, returns its
4194 * config value (supposed to be 0 or positive).
4196 * If no entries are matching, the function returns a negative value.
4198 int snd_hda_check_board_config(struct hda_codec
*codec
,
4199 int num_configs
, const char * const *models
,
4200 const struct snd_pci_quirk
*tbl
)
4202 if (codec
->modelname
&& models
) {
4204 for (i
= 0; i
< num_configs
; i
++) {
4206 !strcmp(codec
->modelname
, models
[i
])) {
4207 snd_printd(KERN_INFO
"hda_codec: model '%s' is "
4208 "selected\n", models
[i
]);
4214 if (!codec
->bus
->pci
|| !tbl
)
4217 tbl
= snd_pci_quirk_lookup(codec
->bus
->pci
, tbl
);
4220 if (tbl
->value
>= 0 && tbl
->value
< num_configs
) {
4221 #ifdef CONFIG_SND_DEBUG_VERBOSE
4223 const char *model
= NULL
;
4225 model
= models
[tbl
->value
];
4227 sprintf(tmp
, "#%d", tbl
->value
);
4230 snd_printdd(KERN_INFO
"hda_codec: model '%s' is selected "
4231 "for config %x:%x (%s)\n",
4232 model
, tbl
->subvendor
, tbl
->subdevice
,
4233 (tbl
->name
? tbl
->name
: "Unknown device"));
4239 EXPORT_SYMBOL_HDA(snd_hda_check_board_config
);
4242 * snd_hda_check_board_codec_sid_config - compare the current codec
4243 subsystem ID with the
4246 This is important for Gateway notebooks with SB450 HDA Audio
4247 where the vendor ID of the PCI device is:
4248 ATI Technologies Inc SB450 HDA Audio [1002:437b]
4249 and the vendor/subvendor are found only at the codec.
4251 * @codec: the HDA codec
4252 * @num_configs: number of config enums
4253 * @models: array of model name strings
4254 * @tbl: configuration table, terminated by null entries
4256 * Compares the modelname or PCI subsystem id of the current codec with the
4257 * given configuration table. If a matching entry is found, returns its
4258 * config value (supposed to be 0 or positive).
4260 * If no entries are matching, the function returns a negative value.
4262 int snd_hda_check_board_codec_sid_config(struct hda_codec
*codec
,
4263 int num_configs
, const char * const *models
,
4264 const struct snd_pci_quirk
*tbl
)
4266 const struct snd_pci_quirk
*q
;
4268 /* Search for codec ID */
4269 for (q
= tbl
; q
->subvendor
; q
++) {
4270 unsigned int mask
= 0xffff0000 | q
->subdevice_mask
;
4271 unsigned int id
= (q
->subdevice
| (q
->subvendor
<< 16)) & mask
;
4272 if ((codec
->subsystem_id
& mask
) == id
)
4281 if (tbl
->value
>= 0 && tbl
->value
< num_configs
) {
4282 #ifdef CONFIG_SND_DEBUG_VERBOSE
4284 const char *model
= NULL
;
4286 model
= models
[tbl
->value
];
4288 sprintf(tmp
, "#%d", tbl
->value
);
4291 snd_printdd(KERN_INFO
"hda_codec: model '%s' is selected "
4292 "for config %x:%x (%s)\n",
4293 model
, tbl
->subvendor
, tbl
->subdevice
,
4294 (tbl
->name
? tbl
->name
: "Unknown device"));
4300 EXPORT_SYMBOL_HDA(snd_hda_check_board_codec_sid_config
);
4303 * snd_hda_add_new_ctls - create controls from the array
4304 * @codec: the HDA codec
4305 * @knew: the array of struct snd_kcontrol_new
4307 * This helper function creates and add new controls in the given array.
4308 * The array must be terminated with an empty entry as terminator.
4310 * Returns 0 if successful, or a negative error code.
4312 int snd_hda_add_new_ctls(struct hda_codec
*codec
,
4313 const struct snd_kcontrol_new
*knew
)
4317 for (; knew
->name
; knew
++) {
4318 struct snd_kcontrol
*kctl
;
4319 int addr
= 0, idx
= 0;
4320 if (knew
->iface
== -1) /* skip this codec private value */
4323 kctl
= snd_ctl_new1(knew
, codec
);
4327 kctl
->id
.device
= addr
;
4329 kctl
->id
.index
= idx
;
4330 err
= snd_hda_ctl_add(codec
, 0, kctl
);
4333 /* try first with another device index corresponding to
4334 * the codec addr; if it still fails (or it's the
4335 * primary codec), then try another control index
4337 if (!addr
&& codec
->addr
)
4339 else if (!idx
&& !knew
->index
) {
4340 idx
= find_empty_mixer_ctl_idx(codec
,
4350 EXPORT_SYMBOL_HDA(snd_hda_add_new_ctls
);
4352 #ifdef CONFIG_SND_HDA_POWER_SAVE
4353 static void hda_power_work(struct work_struct
*work
)
4355 struct hda_codec
*codec
=
4356 container_of(work
, struct hda_codec
, power_work
.work
);
4357 struct hda_bus
*bus
= codec
->bus
;
4359 spin_lock(&codec
->power_lock
);
4360 if (codec
->power_transition
> 0) { /* during power-up sequence? */
4361 spin_unlock(&codec
->power_lock
);
4364 if (!codec
->power_on
|| codec
->power_count
) {
4365 codec
->power_transition
= 0;
4366 spin_unlock(&codec
->power_lock
);
4369 spin_unlock(&codec
->power_lock
);
4371 hda_call_codec_suspend(codec
);
4372 if (bus
->ops
.pm_notify
)
4373 bus
->ops
.pm_notify(bus
);
4376 static void hda_keep_power_on(struct hda_codec
*codec
)
4378 spin_lock(&codec
->power_lock
);
4379 codec
->power_count
++;
4380 codec
->power_on
= 1;
4381 codec
->power_jiffies
= jiffies
;
4382 spin_unlock(&codec
->power_lock
);
4385 /* update the power on/off account with the current jiffies */
4386 void snd_hda_update_power_acct(struct hda_codec
*codec
)
4388 unsigned long delta
= jiffies
- codec
->power_jiffies
;
4389 if (codec
->power_on
)
4390 codec
->power_on_acct
+= delta
;
4392 codec
->power_off_acct
+= delta
;
4393 codec
->power_jiffies
+= delta
;
4396 /* Transition to powered up, if wait_power_down then wait for a pending
4397 * transition to D3 to complete. A pending D3 transition is indicated
4398 * with power_transition == -1. */
4399 static void __snd_hda_power_up(struct hda_codec
*codec
, bool wait_power_down
)
4401 struct hda_bus
*bus
= codec
->bus
;
4403 spin_lock(&codec
->power_lock
);
4404 codec
->power_count
++;
4405 /* Return if power_on or transitioning to power_on, unless currently
4407 if ((codec
->power_on
|| codec
->power_transition
> 0) &&
4408 !(wait_power_down
&& codec
->power_transition
< 0)) {
4409 spin_unlock(&codec
->power_lock
);
4412 spin_unlock(&codec
->power_lock
);
4414 cancel_delayed_work_sync(&codec
->power_work
);
4416 spin_lock(&codec
->power_lock
);
4417 trace_hda_power_up(codec
);
4418 snd_hda_update_power_acct(codec
);
4419 codec
->power_on
= 1;
4420 codec
->power_jiffies
= jiffies
;
4421 codec
->power_transition
= 1; /* avoid reentrance */
4422 spin_unlock(&codec
->power_lock
);
4424 if (bus
->ops
.pm_notify
)
4425 bus
->ops
.pm_notify(bus
);
4426 hda_call_codec_resume(codec
);
4428 spin_lock(&codec
->power_lock
);
4429 codec
->power_transition
= 0;
4430 spin_unlock(&codec
->power_lock
);
4434 * snd_hda_power_up - Power-up the codec
4435 * @codec: HD-audio codec
4437 * Increment the power-up counter and power up the hardware really when
4438 * not turned on yet.
4440 void snd_hda_power_up(struct hda_codec
*codec
)
4442 __snd_hda_power_up(codec
, false);
4444 EXPORT_SYMBOL_HDA(snd_hda_power_up
);
4447 * snd_hda_power_up_d3wait - Power-up the codec after waiting for any pending
4448 * D3 transition to complete. This differs from snd_hda_power_up() when
4449 * power_transition == -1. snd_hda_power_up sees this case as a nop,
4450 * snd_hda_power_up_d3wait waits for the D3 transition to complete then powers
4452 * @codec: HD-audio codec
4454 * Cancel any power down operation hapenning on the work queue, then power up.
4456 void snd_hda_power_up_d3wait(struct hda_codec
*codec
)
4458 /* This will cancel and wait for pending power_work to complete. */
4459 __snd_hda_power_up(codec
, true);
4461 EXPORT_SYMBOL_HDA(snd_hda_power_up_d3wait
);
4463 #define power_save(codec) \
4464 ((codec)->bus->power_save ? *(codec)->bus->power_save : 0)
4467 * snd_hda_power_down - Power-down the codec
4468 * @codec: HD-audio codec
4470 * Decrement the power-up counter and schedules the power-off work if
4471 * the counter rearches to zero.
4473 void snd_hda_power_down(struct hda_codec
*codec
)
4475 spin_lock(&codec
->power_lock
);
4476 --codec
->power_count
;
4477 if (!codec
->power_on
|| codec
->power_count
|| codec
->power_transition
) {
4478 spin_unlock(&codec
->power_lock
);
4481 if (power_save(codec
)) {
4482 codec
->power_transition
= -1; /* avoid reentrance */
4483 queue_delayed_work(codec
->bus
->workq
, &codec
->power_work
,
4484 msecs_to_jiffies(power_save(codec
) * 1000));
4486 spin_unlock(&codec
->power_lock
);
4488 EXPORT_SYMBOL_HDA(snd_hda_power_down
);
4491 * snd_hda_check_amp_list_power - Check the amp list and update the power
4492 * @codec: HD-audio codec
4493 * @check: the object containing an AMP list and the status
4494 * @nid: NID to check / update
4496 * Check whether the given NID is in the amp list. If it's in the list,
4497 * check the current AMP status, and update the the power-status according
4498 * to the mute status.
4500 * This function is supposed to be set or called from the check_power_status
4503 int snd_hda_check_amp_list_power(struct hda_codec
*codec
,
4504 struct hda_loopback_check
*check
,
4507 const struct hda_amp_list
*p
;
4510 if (!check
->amplist
)
4512 for (p
= check
->amplist
; p
->nid
; p
++) {
4517 return 0; /* nothing changed */
4519 for (p
= check
->amplist
; p
->nid
; p
++) {
4520 for (ch
= 0; ch
< 2; ch
++) {
4521 v
= snd_hda_codec_amp_read(codec
, p
->nid
, ch
, p
->dir
,
4523 if (!(v
& HDA_AMP_MUTE
) && v
> 0) {
4524 if (!check
->power_on
) {
4525 check
->power_on
= 1;
4526 snd_hda_power_up(codec
);
4532 if (check
->power_on
) {
4533 check
->power_on
= 0;
4534 snd_hda_power_down(codec
);
4538 EXPORT_SYMBOL_HDA(snd_hda_check_amp_list_power
);
4542 * Channel mode helper
4546 * snd_hda_ch_mode_info - Info callback helper for the channel mode enum
4548 int snd_hda_ch_mode_info(struct hda_codec
*codec
,
4549 struct snd_ctl_elem_info
*uinfo
,
4550 const struct hda_channel_mode
*chmode
,
4553 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_ENUMERATED
;
4555 uinfo
->value
.enumerated
.items
= num_chmodes
;
4556 if (uinfo
->value
.enumerated
.item
>= num_chmodes
)
4557 uinfo
->value
.enumerated
.item
= num_chmodes
- 1;
4558 sprintf(uinfo
->value
.enumerated
.name
, "%dch",
4559 chmode
[uinfo
->value
.enumerated
.item
].channels
);
4562 EXPORT_SYMBOL_HDA(snd_hda_ch_mode_info
);
4565 * snd_hda_ch_mode_get - Get callback helper for the channel mode enum
4567 int snd_hda_ch_mode_get(struct hda_codec
*codec
,
4568 struct snd_ctl_elem_value
*ucontrol
,
4569 const struct hda_channel_mode
*chmode
,
4575 for (i
= 0; i
< num_chmodes
; i
++) {
4576 if (max_channels
== chmode
[i
].channels
) {
4577 ucontrol
->value
.enumerated
.item
[0] = i
;
4583 EXPORT_SYMBOL_HDA(snd_hda_ch_mode_get
);
4586 * snd_hda_ch_mode_put - Put callback helper for the channel mode enum
4588 int snd_hda_ch_mode_put(struct hda_codec
*codec
,
4589 struct snd_ctl_elem_value
*ucontrol
,
4590 const struct hda_channel_mode
*chmode
,
4596 mode
= ucontrol
->value
.enumerated
.item
[0];
4597 if (mode
>= num_chmodes
)
4599 if (*max_channelsp
== chmode
[mode
].channels
)
4601 /* change the current channel setting */
4602 *max_channelsp
= chmode
[mode
].channels
;
4603 if (chmode
[mode
].sequence
)
4604 snd_hda_sequence_write_cache(codec
, chmode
[mode
].sequence
);
4607 EXPORT_SYMBOL_HDA(snd_hda_ch_mode_put
);
4614 * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum
4616 int snd_hda_input_mux_info(const struct hda_input_mux
*imux
,
4617 struct snd_ctl_elem_info
*uinfo
)
4621 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_ENUMERATED
;
4623 uinfo
->value
.enumerated
.items
= imux
->num_items
;
4624 if (!imux
->num_items
)
4626 index
= uinfo
->value
.enumerated
.item
;
4627 if (index
>= imux
->num_items
)
4628 index
= imux
->num_items
- 1;
4629 strcpy(uinfo
->value
.enumerated
.name
, imux
->items
[index
].label
);
4632 EXPORT_SYMBOL_HDA(snd_hda_input_mux_info
);
4635 * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum
4637 int snd_hda_input_mux_put(struct hda_codec
*codec
,
4638 const struct hda_input_mux
*imux
,
4639 struct snd_ctl_elem_value
*ucontrol
,
4641 unsigned int *cur_val
)
4645 if (!imux
->num_items
)
4647 idx
= ucontrol
->value
.enumerated
.item
[0];
4648 if (idx
>= imux
->num_items
)
4649 idx
= imux
->num_items
- 1;
4650 if (*cur_val
== idx
)
4652 snd_hda_codec_write_cache(codec
, nid
, 0, AC_VERB_SET_CONNECT_SEL
,
4653 imux
->items
[idx
].index
);
4657 EXPORT_SYMBOL_HDA(snd_hda_input_mux_put
);
4661 * Multi-channel / digital-out PCM helper functions
4664 /* setup SPDIF output stream */
4665 static void setup_dig_out_stream(struct hda_codec
*codec
, hda_nid_t nid
,
4666 unsigned int stream_tag
, unsigned int format
)
4668 struct hda_spdif_out
*spdif
= snd_hda_spdif_out_of_nid(codec
, nid
);
4670 /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */
4671 if (codec
->spdif_status_reset
&& (spdif
->ctls
& AC_DIG1_ENABLE
))
4672 set_dig_out_convert(codec
, nid
,
4673 spdif
->ctls
& ~AC_DIG1_ENABLE
& 0xff,
4675 snd_hda_codec_setup_stream(codec
, nid
, stream_tag
, 0, format
);
4676 if (codec
->slave_dig_outs
) {
4678 for (d
= codec
->slave_dig_outs
; *d
; d
++)
4679 snd_hda_codec_setup_stream(codec
, *d
, stream_tag
, 0,
4682 /* turn on again (if needed) */
4683 if (codec
->spdif_status_reset
&& (spdif
->ctls
& AC_DIG1_ENABLE
))
4684 set_dig_out_convert(codec
, nid
,
4685 spdif
->ctls
& 0xff, -1);
4688 static void cleanup_dig_out_stream(struct hda_codec
*codec
, hda_nid_t nid
)
4690 snd_hda_codec_cleanup_stream(codec
, nid
);
4691 if (codec
->slave_dig_outs
) {
4693 for (d
= codec
->slave_dig_outs
; *d
; d
++)
4694 snd_hda_codec_cleanup_stream(codec
, *d
);
4699 * snd_hda_bus_reboot_notify - call the reboot notifier of each codec
4700 * @bus: HD-audio bus
4702 void snd_hda_bus_reboot_notify(struct hda_bus
*bus
)
4704 struct hda_codec
*codec
;
4708 list_for_each_entry(codec
, &bus
->codec_list
, list
) {
4709 if (hda_codec_is_power_on(codec
) &&
4710 codec
->patch_ops
.reboot_notify
)
4711 codec
->patch_ops
.reboot_notify(codec
);
4714 EXPORT_SYMBOL_HDA(snd_hda_bus_reboot_notify
);
4717 * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode
4719 int snd_hda_multi_out_dig_open(struct hda_codec
*codec
,
4720 struct hda_multi_out
*mout
)
4722 mutex_lock(&codec
->spdif_mutex
);
4723 if (mout
->dig_out_used
== HDA_DIG_ANALOG_DUP
)
4724 /* already opened as analog dup; reset it once */
4725 cleanup_dig_out_stream(codec
, mout
->dig_out_nid
);
4726 mout
->dig_out_used
= HDA_DIG_EXCLUSIVE
;
4727 mutex_unlock(&codec
->spdif_mutex
);
4730 EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_open
);
4733 * snd_hda_multi_out_dig_prepare - prepare the digital out stream
4735 int snd_hda_multi_out_dig_prepare(struct hda_codec
*codec
,
4736 struct hda_multi_out
*mout
,
4737 unsigned int stream_tag
,
4738 unsigned int format
,
4739 struct snd_pcm_substream
*substream
)
4741 mutex_lock(&codec
->spdif_mutex
);
4742 setup_dig_out_stream(codec
, mout
->dig_out_nid
, stream_tag
, format
);
4743 mutex_unlock(&codec
->spdif_mutex
);
4746 EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_prepare
);
4749 * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream
4751 int snd_hda_multi_out_dig_cleanup(struct hda_codec
*codec
,
4752 struct hda_multi_out
*mout
)
4754 mutex_lock(&codec
->spdif_mutex
);
4755 cleanup_dig_out_stream(codec
, mout
->dig_out_nid
);
4756 mutex_unlock(&codec
->spdif_mutex
);
4759 EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_cleanup
);
4762 * snd_hda_multi_out_dig_close - release the digital out stream
4764 int snd_hda_multi_out_dig_close(struct hda_codec
*codec
,
4765 struct hda_multi_out
*mout
)
4767 mutex_lock(&codec
->spdif_mutex
);
4768 mout
->dig_out_used
= 0;
4769 mutex_unlock(&codec
->spdif_mutex
);
4772 EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_close
);
4775 * snd_hda_multi_out_analog_open - open analog outputs
4777 * Open analog outputs and set up the hw-constraints.
4778 * If the digital outputs can be opened as slave, open the digital
4781 int snd_hda_multi_out_analog_open(struct hda_codec
*codec
,
4782 struct hda_multi_out
*mout
,
4783 struct snd_pcm_substream
*substream
,
4784 struct hda_pcm_stream
*hinfo
)
4786 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
4787 runtime
->hw
.channels_max
= mout
->max_channels
;
4788 if (mout
->dig_out_nid
) {
4789 if (!mout
->analog_rates
) {
4790 mout
->analog_rates
= hinfo
->rates
;
4791 mout
->analog_formats
= hinfo
->formats
;
4792 mout
->analog_maxbps
= hinfo
->maxbps
;
4794 runtime
->hw
.rates
= mout
->analog_rates
;
4795 runtime
->hw
.formats
= mout
->analog_formats
;
4796 hinfo
->maxbps
= mout
->analog_maxbps
;
4798 if (!mout
->spdif_rates
) {
4799 snd_hda_query_supported_pcm(codec
, mout
->dig_out_nid
,
4801 &mout
->spdif_formats
,
4802 &mout
->spdif_maxbps
);
4804 mutex_lock(&codec
->spdif_mutex
);
4805 if (mout
->share_spdif
) {
4806 if ((runtime
->hw
.rates
& mout
->spdif_rates
) &&
4807 (runtime
->hw
.formats
& mout
->spdif_formats
)) {
4808 runtime
->hw
.rates
&= mout
->spdif_rates
;
4809 runtime
->hw
.formats
&= mout
->spdif_formats
;
4810 if (mout
->spdif_maxbps
< hinfo
->maxbps
)
4811 hinfo
->maxbps
= mout
->spdif_maxbps
;
4813 mout
->share_spdif
= 0;
4814 /* FIXME: need notify? */
4817 mutex_unlock(&codec
->spdif_mutex
);
4819 return snd_pcm_hw_constraint_step(substream
->runtime
, 0,
4820 SNDRV_PCM_HW_PARAM_CHANNELS
, 2);
4822 EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_open
);
4825 * snd_hda_multi_out_analog_prepare - Preapre the analog outputs.
4827 * Set up the i/o for analog out.
4828 * When the digital out is available, copy the front out to digital out, too.
4830 int snd_hda_multi_out_analog_prepare(struct hda_codec
*codec
,
4831 struct hda_multi_out
*mout
,
4832 unsigned int stream_tag
,
4833 unsigned int format
,
4834 struct snd_pcm_substream
*substream
)
4836 const hda_nid_t
*nids
= mout
->dac_nids
;
4837 int chs
= substream
->runtime
->channels
;
4838 struct hda_spdif_out
*spdif
;
4841 mutex_lock(&codec
->spdif_mutex
);
4842 spdif
= snd_hda_spdif_out_of_nid(codec
, mout
->dig_out_nid
);
4843 if (mout
->dig_out_nid
&& mout
->share_spdif
&&
4844 mout
->dig_out_used
!= HDA_DIG_EXCLUSIVE
) {
4846 snd_hda_is_supported_format(codec
, mout
->dig_out_nid
,
4848 !(spdif
->status
& IEC958_AES0_NONAUDIO
)) {
4849 mout
->dig_out_used
= HDA_DIG_ANALOG_DUP
;
4850 setup_dig_out_stream(codec
, mout
->dig_out_nid
,
4851 stream_tag
, format
);
4853 mout
->dig_out_used
= 0;
4854 cleanup_dig_out_stream(codec
, mout
->dig_out_nid
);
4857 mutex_unlock(&codec
->spdif_mutex
);
4860 snd_hda_codec_setup_stream(codec
, nids
[HDA_FRONT
], stream_tag
,
4862 if (!mout
->no_share_stream
&&
4863 mout
->hp_nid
&& mout
->hp_nid
!= nids
[HDA_FRONT
])
4864 /* headphone out will just decode front left/right (stereo) */
4865 snd_hda_codec_setup_stream(codec
, mout
->hp_nid
, stream_tag
,
4867 /* extra outputs copied from front */
4868 for (i
= 0; i
< ARRAY_SIZE(mout
->hp_out_nid
); i
++)
4869 if (!mout
->no_share_stream
&& mout
->hp_out_nid
[i
])
4870 snd_hda_codec_setup_stream(codec
,
4871 mout
->hp_out_nid
[i
],
4872 stream_tag
, 0, format
);
4873 for (i
= 0; i
< ARRAY_SIZE(mout
->extra_out_nid
); i
++)
4874 if (!mout
->no_share_stream
&& mout
->extra_out_nid
[i
])
4875 snd_hda_codec_setup_stream(codec
,
4876 mout
->extra_out_nid
[i
],
4877 stream_tag
, 0, format
);
4880 for (i
= 1; i
< mout
->num_dacs
; i
++) {
4881 if (chs
>= (i
+ 1) * 2) /* independent out */
4882 snd_hda_codec_setup_stream(codec
, nids
[i
], stream_tag
,
4884 else if (!mout
->no_share_stream
) /* copy front */
4885 snd_hda_codec_setup_stream(codec
, nids
[i
], stream_tag
,
4890 EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_prepare
);
4893 * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out
4895 int snd_hda_multi_out_analog_cleanup(struct hda_codec
*codec
,
4896 struct hda_multi_out
*mout
)
4898 const hda_nid_t
*nids
= mout
->dac_nids
;
4901 for (i
= 0; i
< mout
->num_dacs
; i
++)
4902 snd_hda_codec_cleanup_stream(codec
, nids
[i
]);
4904 snd_hda_codec_cleanup_stream(codec
, mout
->hp_nid
);
4905 for (i
= 0; i
< ARRAY_SIZE(mout
->hp_out_nid
); i
++)
4906 if (mout
->hp_out_nid
[i
])
4907 snd_hda_codec_cleanup_stream(codec
,
4908 mout
->hp_out_nid
[i
]);
4909 for (i
= 0; i
< ARRAY_SIZE(mout
->extra_out_nid
); i
++)
4910 if (mout
->extra_out_nid
[i
])
4911 snd_hda_codec_cleanup_stream(codec
,
4912 mout
->extra_out_nid
[i
]);
4913 mutex_lock(&codec
->spdif_mutex
);
4914 if (mout
->dig_out_nid
&& mout
->dig_out_used
== HDA_DIG_ANALOG_DUP
) {
4915 cleanup_dig_out_stream(codec
, mout
->dig_out_nid
);
4916 mout
->dig_out_used
= 0;
4918 mutex_unlock(&codec
->spdif_mutex
);
4921 EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_cleanup
);
4924 * snd_hda_get_default_vref - Get the default (mic) VREF pin bits
4926 * Guess the suitable VREF pin bits to be set as the pin-control value.
4927 * Note: the function doesn't set the AC_PINCTL_IN_EN bit.
4929 unsigned int snd_hda_get_default_vref(struct hda_codec
*codec
, hda_nid_t pin
)
4931 unsigned int pincap
;
4932 unsigned int oldval
;
4933 oldval
= snd_hda_codec_read(codec
, pin
, 0,
4934 AC_VERB_GET_PIN_WIDGET_CONTROL
, 0);
4935 pincap
= snd_hda_query_pin_caps(codec
, pin
);
4936 pincap
= (pincap
& AC_PINCAP_VREF
) >> AC_PINCAP_VREF_SHIFT
;
4937 /* Exception: if the default pin setup is vref50, we give it priority */
4938 if ((pincap
& AC_PINCAP_VREF_80
) && oldval
!= PIN_VREF50
)
4939 return AC_PINCTL_VREF_80
;
4940 else if (pincap
& AC_PINCAP_VREF_50
)
4941 return AC_PINCTL_VREF_50
;
4942 else if (pincap
& AC_PINCAP_VREF_100
)
4943 return AC_PINCTL_VREF_100
;
4944 else if (pincap
& AC_PINCAP_VREF_GRD
)
4945 return AC_PINCTL_VREF_GRD
;
4946 return AC_PINCTL_VREF_HIZ
;
4948 EXPORT_SYMBOL_HDA(snd_hda_get_default_vref
);
4950 int _snd_hda_set_pin_ctl(struct hda_codec
*codec
, hda_nid_t pin
,
4951 unsigned int val
, bool cached
)
4954 unsigned int cap
= snd_hda_query_pin_caps(codec
, pin
);
4955 if (cap
&& (val
& AC_PINCTL_OUT_EN
)) {
4956 if (!(cap
& AC_PINCAP_OUT
))
4957 val
&= ~(AC_PINCTL_OUT_EN
| AC_PINCTL_HP_EN
);
4958 else if ((val
& AC_PINCTL_HP_EN
) &&
4959 !(cap
& AC_PINCAP_HP_DRV
))
4960 val
&= ~AC_PINCTL_HP_EN
;
4962 if (cap
&& (val
& AC_PINCTL_IN_EN
)) {
4963 if (!(cap
& AC_PINCAP_IN
))
4964 val
&= ~(AC_PINCTL_IN_EN
| AC_PINCTL_VREFEN
);
4968 return snd_hda_codec_update_cache(codec
, pin
, 0,
4969 AC_VERB_SET_PIN_WIDGET_CONTROL
, val
);
4971 return snd_hda_codec_write(codec
, pin
, 0,
4972 AC_VERB_SET_PIN_WIDGET_CONTROL
, val
);
4974 EXPORT_SYMBOL_HDA(_snd_hda_set_pin_ctl
);
4977 * snd_hda_add_imux_item - Add an item to input_mux
4979 * When the same label is used already in the existing items, the number
4980 * suffix is appended to the label. This label index number is stored
4981 * to type_idx when non-NULL pointer is given.
4983 int snd_hda_add_imux_item(struct hda_input_mux
*imux
, const char *label
,
4984 int index
, int *type_idx
)
4986 int i
, label_idx
= 0;
4987 if (imux
->num_items
>= HDA_MAX_NUM_INPUTS
) {
4988 snd_printd(KERN_ERR
"hda_codec: Too many imux items!\n");
4991 for (i
= 0; i
< imux
->num_items
; i
++) {
4992 if (!strncmp(label
, imux
->items
[i
].label
, strlen(label
)))
4996 *type_idx
= label_idx
;
4998 snprintf(imux
->items
[imux
->num_items
].label
,
4999 sizeof(imux
->items
[imux
->num_items
].label
),
5000 "%s %d", label
, label_idx
);
5002 strlcpy(imux
->items
[imux
->num_items
].label
, label
,
5003 sizeof(imux
->items
[imux
->num_items
].label
));
5004 imux
->items
[imux
->num_items
].index
= index
;
5008 EXPORT_SYMBOL_HDA(snd_hda_add_imux_item
);
5017 * snd_hda_suspend - suspend the codecs
5020 * Returns 0 if successful.
5022 int snd_hda_suspend(struct hda_bus
*bus
)
5024 struct hda_codec
*codec
;
5026 list_for_each_entry(codec
, &bus
->codec_list
, list
) {
5027 if (hda_codec_is_power_on(codec
))
5028 hda_call_codec_suspend(codec
);
5032 EXPORT_SYMBOL_HDA(snd_hda_suspend
);
5035 * snd_hda_resume - resume the codecs
5038 * Returns 0 if successful.
5040 * This function is defined only when POWER_SAVE isn't set.
5041 * In the power-save mode, the codec is resumed dynamically.
5043 int snd_hda_resume(struct hda_bus
*bus
)
5045 struct hda_codec
*codec
;
5047 list_for_each_entry(codec
, &bus
->codec_list
, list
) {
5048 hda_call_codec_resume(codec
);
5052 EXPORT_SYMBOL_HDA(snd_hda_resume
);
5053 #endif /* CONFIG_PM */
5060 * snd_array_new - get a new element from the given array
5061 * @array: the array object
5063 * Get a new element from the given array. If it exceeds the
5064 * pre-allocated array size, re-allocate the array.
5066 * Returns NULL if allocation failed.
5068 void *snd_array_new(struct snd_array
*array
)
5070 if (array
->used
>= array
->alloced
) {
5071 int num
= array
->alloced
+ array
->alloc_align
;
5072 int size
= (num
+ 1) * array
->elem_size
;
5073 int oldsize
= array
->alloced
* array
->elem_size
;
5075 if (snd_BUG_ON(num
>= 4096))
5077 nlist
= krealloc(array
->list
, size
, GFP_KERNEL
);
5080 memset(nlist
+ oldsize
, 0, size
- oldsize
);
5081 array
->list
= nlist
;
5082 array
->alloced
= num
;
5084 return snd_array_elem(array
, array
->used
++);
5086 EXPORT_SYMBOL_HDA(snd_array_new
);
5089 * snd_array_free - free the given array elements
5090 * @array: the array object
5092 void snd_array_free(struct snd_array
*array
)
5099 EXPORT_SYMBOL_HDA(snd_array_free
);
5102 * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer
5103 * @pcm: PCM caps bits
5104 * @buf: the string buffer to write
5105 * @buflen: the max buffer length
5107 * used by hda_proc.c and hda_eld.c
5109 void snd_print_pcm_bits(int pcm
, char *buf
, int buflen
)
5111 static unsigned int bits
[] = { 8, 16, 20, 24, 32 };
5114 for (i
= 0, j
= 0; i
< ARRAY_SIZE(bits
); i
++)
5115 if (pcm
& (AC_SUPPCM_BITS_8
<< i
))
5116 j
+= snprintf(buf
+ j
, buflen
- j
, " %d", bits
[i
]);
5118 buf
[j
] = '\0'; /* necessary when j == 0 */
5120 EXPORT_SYMBOL_HDA(snd_print_pcm_bits
);
5122 MODULE_DESCRIPTION("HDA codec core");
5123 MODULE_LICENSE("GPL");