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
);
98 #define codec_in_pm(codec) ((codec)->in_pm)
99 static void hda_power_work(struct work_struct
*work
);
100 static void hda_keep_power_on(struct hda_codec
*codec
);
101 #define hda_codec_is_power_on(codec) ((codec)->power_on)
102 static inline void hda_call_pm_notify(struct hda_bus
*bus
, bool power_up
)
104 if (bus
->ops
.pm_notify
)
105 bus
->ops
.pm_notify(bus
, power_up
);
108 #define codec_in_pm(codec) 0
109 static inline void hda_keep_power_on(struct hda_codec
*codec
) {}
110 #define hda_codec_is_power_on(codec) 1
111 #define hda_call_pm_notify(bus, state) {}
115 * snd_hda_get_jack_location - Give a location string of the jack
116 * @cfg: pin default config value
118 * Parse the pin default config value and returns the string of the
119 * jack location, e.g. "Rear", "Front", etc.
121 const char *snd_hda_get_jack_location(u32 cfg
)
123 static char *bases
[7] = {
124 "N/A", "Rear", "Front", "Left", "Right", "Top", "Bottom",
126 static unsigned char specials_idx
[] = {
131 static char *specials
[] = {
132 "Rear Panel", "Drive Bar",
133 "Riser", "HDMI", "ATAPI",
134 "Mobile-In", "Mobile-Out"
137 cfg
= (cfg
& AC_DEFCFG_LOCATION
) >> AC_DEFCFG_LOCATION_SHIFT
;
138 if ((cfg
& 0x0f) < 7)
139 return bases
[cfg
& 0x0f];
140 for (i
= 0; i
< ARRAY_SIZE(specials_idx
); i
++) {
141 if (cfg
== specials_idx
[i
])
146 EXPORT_SYMBOL_HDA(snd_hda_get_jack_location
);
149 * snd_hda_get_jack_connectivity - Give a connectivity string of the jack
150 * @cfg: pin default config value
152 * Parse the pin default config value and returns the string of the
153 * jack connectivity, i.e. external or internal connection.
155 const char *snd_hda_get_jack_connectivity(u32 cfg
)
157 static char *jack_locations
[4] = { "Ext", "Int", "Sep", "Oth" };
159 return jack_locations
[(cfg
>> (AC_DEFCFG_LOCATION_SHIFT
+ 4)) & 3];
161 EXPORT_SYMBOL_HDA(snd_hda_get_jack_connectivity
);
164 * snd_hda_get_jack_type - Give a type string of the jack
165 * @cfg: pin default config value
167 * Parse the pin default config value and returns the string of the
168 * jack type, i.e. the purpose of the jack, such as Line-Out or CD.
170 const char *snd_hda_get_jack_type(u32 cfg
)
172 static char *jack_types
[16] = {
173 "Line Out", "Speaker", "HP Out", "CD",
174 "SPDIF Out", "Digital Out", "Modem Line", "Modem Hand",
175 "Line In", "Aux", "Mic", "Telephony",
176 "SPDIF In", "Digital In", "Reserved", "Other"
179 return jack_types
[(cfg
& AC_DEFCFG_DEVICE
)
180 >> AC_DEFCFG_DEVICE_SHIFT
];
182 EXPORT_SYMBOL_HDA(snd_hda_get_jack_type
);
185 * Compose a 32bit command word to be sent to the HD-audio controller
187 static inline unsigned int
188 make_codec_cmd(struct hda_codec
*codec
, hda_nid_t nid
, int flags
,
189 unsigned int verb
, unsigned int parm
)
193 if ((codec
->addr
& ~0xf) || (nid
& ~0x7f) ||
194 (verb
& ~0xfff) || (parm
& ~0xffff)) {
195 printk(KERN_ERR
"hda-codec: out of range cmd %x:%x:%x:%x\n",
196 codec
->addr
, nid
, verb
, parm
);
200 val
= (u32
)codec
->addr
<< 28;
201 val
|= (u32
)nid
<< 20;
208 * Send and receive a verb
210 static int codec_exec_verb(struct hda_codec
*codec
, unsigned int cmd
,
211 int flags
, unsigned int *res
)
213 struct hda_bus
*bus
= codec
->bus
;
222 snd_hda_power_up(codec
);
223 mutex_lock(&bus
->cmd_mutex
);
224 if (flags
& HDA_RW_NO_RESPONSE_FALLBACK
)
225 bus
->no_response_fallback
= 1;
227 trace_hda_send_cmd(codec
, cmd
);
228 err
= bus
->ops
.command(bus
, cmd
);
231 /* process pending verbs */
232 bus
->ops
.get_response(bus
, codec
->addr
);
235 *res
= bus
->ops
.get_response(bus
, codec
->addr
);
236 trace_hda_get_response(codec
, *res
);
238 bus
->no_response_fallback
= 0;
239 mutex_unlock(&bus
->cmd_mutex
);
240 snd_hda_power_down(codec
);
241 if (!codec_in_pm(codec
) && res
&& *res
== -1 && bus
->rirb_error
) {
242 if (bus
->response_reset
) {
243 snd_printd("hda_codec: resetting BUS due to "
244 "fatal communication error\n");
245 trace_hda_bus_reset(bus
);
246 bus
->ops
.bus_reset(bus
);
250 /* clear reset-flag when the communication gets recovered */
251 if (!err
|| codec_in_pm(codec
))
252 bus
->response_reset
= 0;
257 * snd_hda_codec_read - send a command and get the response
258 * @codec: the HDA codec
259 * @nid: NID to send the command
260 * @flags: optional bit flags
261 * @verb: the verb to send
262 * @parm: the parameter for the verb
264 * Send a single command and read the corresponding response.
266 * Returns the obtained response value, or -1 for an error.
268 unsigned int snd_hda_codec_read(struct hda_codec
*codec
, hda_nid_t nid
,
270 unsigned int verb
, unsigned int parm
)
272 unsigned cmd
= make_codec_cmd(codec
, nid
, flags
, verb
, parm
);
274 if (codec_exec_verb(codec
, cmd
, flags
, &res
))
278 EXPORT_SYMBOL_HDA(snd_hda_codec_read
);
281 * snd_hda_codec_write - send a single command without waiting for response
282 * @codec: the HDA codec
283 * @nid: NID to send the command
284 * @flags: optional bit flags
285 * @verb: the verb to send
286 * @parm: the parameter for the verb
288 * Send a single command without waiting for response.
290 * Returns 0 if successful, or a negative error code.
292 int snd_hda_codec_write(struct hda_codec
*codec
, hda_nid_t nid
, int flags
,
293 unsigned int verb
, unsigned int parm
)
295 unsigned int cmd
= make_codec_cmd(codec
, nid
, flags
, verb
, parm
);
297 return codec_exec_verb(codec
, cmd
, flags
,
298 codec
->bus
->sync_write
? &res
: NULL
);
300 EXPORT_SYMBOL_HDA(snd_hda_codec_write
);
303 * snd_hda_sequence_write - sequence writes
304 * @codec: the HDA codec
305 * @seq: VERB array to send
307 * Send the commands sequentially from the given array.
308 * The array must be terminated with NID=0.
310 void snd_hda_sequence_write(struct hda_codec
*codec
, const struct hda_verb
*seq
)
312 for (; seq
->nid
; seq
++)
313 snd_hda_codec_write(codec
, seq
->nid
, 0, seq
->verb
, seq
->param
);
315 EXPORT_SYMBOL_HDA(snd_hda_sequence_write
);
318 * snd_hda_get_sub_nodes - get the range of sub nodes
319 * @codec: the HDA codec
321 * @start_id: the pointer to store the start NID
323 * Parse the NID and store the start NID of its sub-nodes.
324 * Returns the number of sub-nodes.
326 int snd_hda_get_sub_nodes(struct hda_codec
*codec
, hda_nid_t nid
,
331 parm
= snd_hda_param_read(codec
, nid
, AC_PAR_NODE_COUNT
);
334 *start_id
= (parm
>> 16) & 0x7fff;
335 return (int)(parm
& 0x7fff);
337 EXPORT_SYMBOL_HDA(snd_hda_get_sub_nodes
);
339 /* connection list element */
340 struct hda_conn_list
{
341 struct list_head list
;
347 /* look up the cached results */
348 static struct hda_conn_list
*
349 lookup_conn_list(struct hda_codec
*codec
, hda_nid_t nid
)
351 struct hda_conn_list
*p
;
352 list_for_each_entry(p
, &codec
->conn_list
, list
) {
359 static int add_conn_list(struct hda_codec
*codec
, hda_nid_t nid
, int len
,
360 const hda_nid_t
*list
)
362 struct hda_conn_list
*p
;
364 p
= kmalloc(sizeof(*p
) + len
* sizeof(hda_nid_t
), GFP_KERNEL
);
369 memcpy(p
->conns
, list
, len
* sizeof(hda_nid_t
));
370 list_add(&p
->list
, &codec
->conn_list
);
374 static void remove_conn_list(struct hda_codec
*codec
)
376 while (!list_empty(&codec
->conn_list
)) {
377 struct hda_conn_list
*p
;
378 p
= list_first_entry(&codec
->conn_list
, typeof(*p
), list
);
384 /* read the connection and add to the cache */
385 static int read_and_add_raw_conns(struct hda_codec
*codec
, hda_nid_t nid
)
388 hda_nid_t
*result
= list
;
391 len
= snd_hda_get_raw_connections(codec
, nid
, list
, ARRAY_SIZE(list
));
392 if (len
== -ENOSPC
) {
393 len
= snd_hda_get_num_raw_conns(codec
, nid
);
394 result
= kmalloc(sizeof(hda_nid_t
) * len
, GFP_KERNEL
);
397 len
= snd_hda_get_raw_connections(codec
, nid
, result
, len
);
400 len
= snd_hda_override_conn_list(codec
, nid
, len
, result
);
407 * snd_hda_get_conn_list - get connection list
408 * @codec: the HDA codec
410 * @len: number of connection list entries
411 * @listp: the pointer to store NID list
413 * Parses the connection list of the given widget and stores the pointer
414 * to the list of NIDs.
416 * Returns the number of connections, or a negative error code.
418 * Note that the returned pointer isn't protected against the list
419 * modification. If snd_hda_override_conn_list() might be called
420 * concurrently, protect with a mutex appropriately.
422 int snd_hda_get_conn_list(struct hda_codec
*codec
, hda_nid_t nid
,
423 const hda_nid_t
**listp
)
429 const struct hda_conn_list
*p
;
431 /* if the connection-list is already cached, read it */
432 p
= lookup_conn_list(codec
, nid
);
438 if (snd_BUG_ON(added
))
441 err
= read_and_add_raw_conns(codec
, nid
);
447 EXPORT_SYMBOL_HDA(snd_hda_get_conn_list
);
450 * snd_hda_get_connections - copy connection list
451 * @codec: the HDA codec
453 * @conn_list: connection list array; when NULL, checks only the size
454 * @max_conns: max. number of connections to store
456 * Parses the connection list of the given widget and stores the list
459 * Returns the number of connections, or a negative error code.
461 int snd_hda_get_connections(struct hda_codec
*codec
, hda_nid_t nid
,
462 hda_nid_t
*conn_list
, int max_conns
)
464 const hda_nid_t
*list
;
465 int len
= snd_hda_get_conn_list(codec
, nid
, &list
);
467 if (len
> 0 && conn_list
) {
468 if (len
> max_conns
) {
469 snd_printk(KERN_ERR
"hda_codec: "
470 "Too many connections %d for NID 0x%x\n",
474 memcpy(conn_list
, list
, len
* sizeof(hda_nid_t
));
479 EXPORT_SYMBOL_HDA(snd_hda_get_connections
);
481 /* return CONNLIST_LEN parameter of the given widget */
482 static unsigned int get_num_conns(struct hda_codec
*codec
, hda_nid_t nid
)
484 unsigned int wcaps
= get_wcaps(codec
, nid
);
487 if (!(wcaps
& AC_WCAP_CONN_LIST
) &&
488 get_wcaps_type(wcaps
) != AC_WID_VOL_KNB
)
491 parm
= snd_hda_param_read(codec
, nid
, AC_PAR_CONNLIST_LEN
);
497 int snd_hda_get_num_raw_conns(struct hda_codec
*codec
, hda_nid_t nid
)
499 return snd_hda_get_raw_connections(codec
, nid
, NULL
, 0);
503 * snd_hda_get_raw_connections - copy connection list without cache
504 * @codec: the HDA codec
506 * @conn_list: connection list array
507 * @max_conns: max. number of connections to store
509 * Like snd_hda_get_connections(), copy the connection list but without
510 * checking through the connection-list cache.
511 * Currently called only from hda_proc.c, so not exported.
513 int snd_hda_get_raw_connections(struct hda_codec
*codec
, hda_nid_t nid
,
514 hda_nid_t
*conn_list
, int max_conns
)
517 int i
, conn_len
, conns
;
518 unsigned int shift
, num_elems
, mask
;
522 parm
= get_num_conns(codec
, nid
);
526 if (parm
& AC_CLIST_LONG
) {
535 conn_len
= parm
& AC_CLIST_LENGTH
;
536 mask
= (1 << (shift
-1)) - 1;
539 return 0; /* no connection */
542 /* single connection */
543 parm
= snd_hda_codec_read(codec
, nid
, 0,
544 AC_VERB_GET_CONNECT_LIST
, 0);
545 if (parm
== -1 && codec
->bus
->rirb_error
)
548 conn_list
[0] = parm
& mask
;
552 /* multi connection */
555 for (i
= 0; i
< conn_len
; i
++) {
559 if (i
% num_elems
== 0) {
560 parm
= snd_hda_codec_read(codec
, nid
, 0,
561 AC_VERB_GET_CONNECT_LIST
, i
);
562 if (parm
== -1 && codec
->bus
->rirb_error
)
565 range_val
= !!(parm
& (1 << (shift
-1))); /* ranges */
567 if (val
== 0 && null_count
++) { /* no second chance */
568 snd_printk(KERN_WARNING
"hda_codec: "
569 "invalid CONNECT_LIST verb %x[%i]:%x\n",
575 /* ranges between the previous and this one */
576 if (!prev_nid
|| prev_nid
>= val
) {
577 snd_printk(KERN_WARNING
"hda_codec: "
578 "invalid dep_range_val %x:%x\n",
582 for (n
= prev_nid
+ 1; n
<= val
; n
++) {
584 if (conns
>= max_conns
)
586 conn_list
[conns
] = n
;
592 if (conns
>= max_conns
)
594 conn_list
[conns
] = val
;
604 * snd_hda_override_conn_list - add/modify the connection-list to cache
605 * @codec: the HDA codec
607 * @len: number of connection list entries
608 * @list: the list of connection entries
610 * Add or modify the given connection-list to the cache. If the corresponding
611 * cache already exists, invalidate it and append a new one.
613 * Returns zero or a negative error code.
615 int snd_hda_override_conn_list(struct hda_codec
*codec
, hda_nid_t nid
, int len
,
616 const hda_nid_t
*list
)
618 struct hda_conn_list
*p
;
620 p
= lookup_conn_list(codec
, nid
);
626 return add_conn_list(codec
, nid
, len
, list
);
628 EXPORT_SYMBOL_HDA(snd_hda_override_conn_list
);
631 * snd_hda_get_conn_index - get the connection index of the given NID
632 * @codec: the HDA codec
633 * @mux: NID containing the list
634 * @nid: NID to select
635 * @recursive: 1 when searching NID recursively, otherwise 0
637 * Parses the connection list of the widget @mux and checks whether the
638 * widget @nid is present. If it is, return the connection index.
639 * Otherwise it returns -1.
641 int snd_hda_get_conn_index(struct hda_codec
*codec
, hda_nid_t mux
,
642 hda_nid_t nid
, int recursive
)
644 const hda_nid_t
*conn
;
647 nums
= snd_hda_get_conn_list(codec
, mux
, &conn
);
648 for (i
= 0; i
< nums
; i
++)
653 if (recursive
> 10) {
654 snd_printd("hda_codec: too deep connection for 0x%x\n", nid
);
658 for (i
= 0; i
< nums
; i
++) {
659 unsigned int type
= get_wcaps_type(get_wcaps(codec
, conn
[i
]));
660 if (type
== AC_WID_PIN
|| type
== AC_WID_AUD_OUT
)
662 if (snd_hda_get_conn_index(codec
, conn
[i
], nid
, recursive
) >= 0)
667 EXPORT_SYMBOL_HDA(snd_hda_get_conn_index
);
670 * snd_hda_queue_unsol_event - add an unsolicited event to queue
672 * @res: unsolicited event (lower 32bit of RIRB entry)
673 * @res_ex: codec addr and flags (upper 32bit or RIRB entry)
675 * Adds the given event to the queue. The events are processed in
676 * the workqueue asynchronously. Call this function in the interrupt
677 * hanlder when RIRB receives an unsolicited event.
679 * Returns 0 if successful, or a negative error code.
681 int snd_hda_queue_unsol_event(struct hda_bus
*bus
, u32 res
, u32 res_ex
)
683 struct hda_bus_unsolicited
*unsol
;
686 if (!bus
|| !bus
->workq
)
689 trace_hda_unsol_event(bus
, res
, res_ex
);
694 wp
= (unsol
->wp
+ 1) % HDA_UNSOL_QUEUE_SIZE
;
698 unsol
->queue
[wp
] = res
;
699 unsol
->queue
[wp
+ 1] = res_ex
;
701 queue_work(bus
->workq
, &unsol
->work
);
705 EXPORT_SYMBOL_HDA(snd_hda_queue_unsol_event
);
708 * process queued unsolicited events
710 static void process_unsol_events(struct work_struct
*work
)
712 struct hda_bus_unsolicited
*unsol
=
713 container_of(work
, struct hda_bus_unsolicited
, work
);
714 struct hda_bus
*bus
= unsol
->bus
;
715 struct hda_codec
*codec
;
716 unsigned int rp
, caddr
, res
;
718 while (unsol
->rp
!= unsol
->wp
) {
719 rp
= (unsol
->rp
+ 1) % HDA_UNSOL_QUEUE_SIZE
;
722 res
= unsol
->queue
[rp
];
723 caddr
= unsol
->queue
[rp
+ 1];
724 if (!(caddr
& (1 << 4))) /* no unsolicited event? */
726 codec
= bus
->caddr_tbl
[caddr
& 0x0f];
727 if (codec
&& codec
->patch_ops
.unsol_event
)
728 codec
->patch_ops
.unsol_event(codec
, res
);
733 * initialize unsolicited queue
735 static int init_unsol_queue(struct hda_bus
*bus
)
737 struct hda_bus_unsolicited
*unsol
;
739 if (bus
->unsol
) /* already initialized */
742 unsol
= kzalloc(sizeof(*unsol
), GFP_KERNEL
);
744 snd_printk(KERN_ERR
"hda_codec: "
745 "can't allocate unsolicited queue\n");
748 INIT_WORK(&unsol
->work
, process_unsol_events
);
757 static void snd_hda_codec_free(struct hda_codec
*codec
);
759 static int snd_hda_bus_free(struct hda_bus
*bus
)
761 struct hda_codec
*codec
, *n
;
766 flush_workqueue(bus
->workq
);
769 list_for_each_entry_safe(codec
, n
, &bus
->codec_list
, list
) {
770 snd_hda_codec_free(codec
);
772 if (bus
->ops
.private_free
)
773 bus
->ops
.private_free(bus
);
775 destroy_workqueue(bus
->workq
);
780 static int snd_hda_bus_dev_free(struct snd_device
*device
)
782 struct hda_bus
*bus
= device
->device_data
;
784 return snd_hda_bus_free(bus
);
787 #ifdef CONFIG_SND_HDA_HWDEP
788 static int snd_hda_bus_dev_register(struct snd_device
*device
)
790 struct hda_bus
*bus
= device
->device_data
;
791 struct hda_codec
*codec
;
792 list_for_each_entry(codec
, &bus
->codec_list
, list
) {
793 snd_hda_hwdep_add_sysfs(codec
);
794 snd_hda_hwdep_add_power_sysfs(codec
);
799 #define snd_hda_bus_dev_register NULL
803 * snd_hda_bus_new - create a HDA bus
804 * @card: the card entry
805 * @temp: the template for hda_bus information
806 * @busp: the pointer to store the created bus instance
808 * Returns 0 if successful, or a negative error code.
810 int snd_hda_bus_new(struct snd_card
*card
,
811 const struct hda_bus_template
*temp
,
812 struct hda_bus
**busp
)
816 static struct snd_device_ops dev_ops
= {
817 .dev_register
= snd_hda_bus_dev_register
,
818 .dev_free
= snd_hda_bus_dev_free
,
821 if (snd_BUG_ON(!temp
))
823 if (snd_BUG_ON(!temp
->ops
.command
|| !temp
->ops
.get_response
))
829 bus
= kzalloc(sizeof(*bus
), GFP_KERNEL
);
831 snd_printk(KERN_ERR
"can't allocate struct hda_bus\n");
836 bus
->private_data
= temp
->private_data
;
837 bus
->pci
= temp
->pci
;
838 bus
->modelname
= temp
->modelname
;
839 bus
->power_save
= temp
->power_save
;
840 bus
->ops
= temp
->ops
;
842 mutex_init(&bus
->cmd_mutex
);
843 mutex_init(&bus
->prepare_mutex
);
844 INIT_LIST_HEAD(&bus
->codec_list
);
846 snprintf(bus
->workq_name
, sizeof(bus
->workq_name
),
847 "hd-audio%d", card
->number
);
848 bus
->workq
= create_singlethread_workqueue(bus
->workq_name
);
850 snd_printk(KERN_ERR
"cannot create workqueue %s\n",
856 err
= snd_device_new(card
, SNDRV_DEV_BUS
, bus
, &dev_ops
);
858 snd_hda_bus_free(bus
);
865 EXPORT_SYMBOL_HDA(snd_hda_bus_new
);
867 #ifdef CONFIG_SND_HDA_GENERIC
868 #define is_generic_config(codec) \
869 (codec->modelname && !strcmp(codec->modelname, "generic"))
871 #define is_generic_config(codec) 0
875 #define HDA_MODREQ_MAX_COUNT 2 /* two request_modules()'s */
877 #define HDA_MODREQ_MAX_COUNT 0 /* all presets are statically linked */
881 * find a matching codec preset
883 static const struct hda_codec_preset
*
884 find_codec_preset(struct hda_codec
*codec
)
886 struct hda_codec_preset_list
*tbl
;
887 const struct hda_codec_preset
*preset
;
888 unsigned int mod_requested
= 0;
890 if (is_generic_config(codec
))
891 return NULL
; /* use the generic parser */
894 mutex_lock(&preset_mutex
);
895 list_for_each_entry(tbl
, &hda_preset_tables
, list
) {
896 if (!try_module_get(tbl
->owner
)) {
897 snd_printk(KERN_ERR
"hda_codec: cannot module_get\n");
900 for (preset
= tbl
->preset
; preset
->id
; preset
++) {
901 u32 mask
= preset
->mask
;
902 if (preset
->afg
&& preset
->afg
!= codec
->afg
)
904 if (preset
->mfg
&& preset
->mfg
!= codec
->mfg
)
908 if (preset
->id
== (codec
->vendor_id
& mask
) &&
910 preset
->rev
== codec
->revision_id
)) {
911 mutex_unlock(&preset_mutex
);
912 codec
->owner
= tbl
->owner
;
916 module_put(tbl
->owner
);
918 mutex_unlock(&preset_mutex
);
920 if (mod_requested
< HDA_MODREQ_MAX_COUNT
) {
923 snprintf(name
, sizeof(name
), "snd-hda-codec-id:%08x",
926 snprintf(name
, sizeof(name
), "snd-hda-codec-id:%04x*",
927 (codec
->vendor_id
>> 16) & 0xffff);
928 request_module(name
);
936 * get_codec_name - store the codec name
938 static int get_codec_name(struct hda_codec
*codec
)
940 const struct hda_vendor_id
*c
;
941 const char *vendor
= NULL
;
942 u16 vendor_id
= codec
->vendor_id
>> 16;
945 if (codec
->vendor_name
)
948 for (c
= hda_vendor_ids
; c
->id
; c
++) {
949 if (c
->id
== vendor_id
) {
955 sprintf(tmp
, "Generic %04x", vendor_id
);
958 codec
->vendor_name
= kstrdup(vendor
, GFP_KERNEL
);
959 if (!codec
->vendor_name
)
963 if (codec
->chip_name
)
966 if (codec
->preset
&& codec
->preset
->name
)
967 codec
->chip_name
= kstrdup(codec
->preset
->name
, GFP_KERNEL
);
969 sprintf(tmp
, "ID %x", codec
->vendor_id
& 0xffff);
970 codec
->chip_name
= kstrdup(tmp
, GFP_KERNEL
);
972 if (!codec
->chip_name
)
978 * look for an AFG and MFG nodes
980 static void setup_fg_nodes(struct hda_codec
*codec
)
982 int i
, total_nodes
, function_id
;
985 total_nodes
= snd_hda_get_sub_nodes(codec
, AC_NODE_ROOT
, &nid
);
986 for (i
= 0; i
< total_nodes
; i
++, nid
++) {
987 function_id
= snd_hda_param_read(codec
, nid
,
988 AC_PAR_FUNCTION_TYPE
);
989 switch (function_id
& 0xff) {
990 case AC_GRP_AUDIO_FUNCTION
:
992 codec
->afg_function_id
= function_id
& 0xff;
993 codec
->afg_unsol
= (function_id
>> 8) & 1;
995 case AC_GRP_MODEM_FUNCTION
:
997 codec
->mfg_function_id
= function_id
& 0xff;
998 codec
->mfg_unsol
= (function_id
>> 8) & 1;
1007 * read widget caps for each widget and store in cache
1009 static int read_widget_caps(struct hda_codec
*codec
, hda_nid_t fg_node
)
1014 codec
->num_nodes
= snd_hda_get_sub_nodes(codec
, fg_node
,
1016 codec
->wcaps
= kmalloc(codec
->num_nodes
* 4, GFP_KERNEL
);
1019 nid
= codec
->start_nid
;
1020 for (i
= 0; i
< codec
->num_nodes
; i
++, nid
++)
1021 codec
->wcaps
[i
] = snd_hda_param_read(codec
, nid
,
1022 AC_PAR_AUDIO_WIDGET_CAP
);
1026 /* read all pin default configurations and save codec->init_pins */
1027 static int read_pin_defaults(struct hda_codec
*codec
)
1030 hda_nid_t nid
= codec
->start_nid
;
1032 for (i
= 0; i
< codec
->num_nodes
; i
++, nid
++) {
1033 struct hda_pincfg
*pin
;
1034 unsigned int wcaps
= get_wcaps(codec
, nid
);
1035 unsigned int wid_type
= get_wcaps_type(wcaps
);
1036 if (wid_type
!= AC_WID_PIN
)
1038 pin
= snd_array_new(&codec
->init_pins
);
1042 pin
->cfg
= snd_hda_codec_read(codec
, nid
, 0,
1043 AC_VERB_GET_CONFIG_DEFAULT
, 0);
1044 pin
->ctrl
= snd_hda_codec_read(codec
, nid
, 0,
1045 AC_VERB_GET_PIN_WIDGET_CONTROL
,
1051 /* look up the given pin config list and return the item matching with NID */
1052 static struct hda_pincfg
*look_up_pincfg(struct hda_codec
*codec
,
1053 struct snd_array
*array
,
1057 for (i
= 0; i
< array
->used
; i
++) {
1058 struct hda_pincfg
*pin
= snd_array_elem(array
, i
);
1059 if (pin
->nid
== nid
)
1065 /* set the current pin config value for the given NID.
1066 * the value is cached, and read via snd_hda_codec_get_pincfg()
1068 int snd_hda_add_pincfg(struct hda_codec
*codec
, struct snd_array
*list
,
1069 hda_nid_t nid
, unsigned int cfg
)
1071 struct hda_pincfg
*pin
;
1073 /* the check below may be invalid when pins are added by a fixup
1074 * dynamically (e.g. via snd_hda_codec_update_widgets()), so disabled
1078 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN)
1082 pin
= look_up_pincfg(codec
, list
, nid
);
1084 pin
= snd_array_new(list
);
1094 * snd_hda_codec_set_pincfg - Override a pin default configuration
1095 * @codec: the HDA codec
1096 * @nid: NID to set the pin config
1097 * @cfg: the pin default config value
1099 * Override a pin default configuration value in the cache.
1100 * This value can be read by snd_hda_codec_get_pincfg() in a higher
1101 * priority than the real hardware value.
1103 int snd_hda_codec_set_pincfg(struct hda_codec
*codec
,
1104 hda_nid_t nid
, unsigned int cfg
)
1106 return snd_hda_add_pincfg(codec
, &codec
->driver_pins
, nid
, cfg
);
1108 EXPORT_SYMBOL_HDA(snd_hda_codec_set_pincfg
);
1111 * snd_hda_codec_get_pincfg - Obtain a pin-default configuration
1112 * @codec: the HDA codec
1113 * @nid: NID to get the pin config
1115 * Get the current pin config value of the given pin NID.
1116 * If the pincfg value is cached or overridden via sysfs or driver,
1117 * returns the cached value.
1119 unsigned int snd_hda_codec_get_pincfg(struct hda_codec
*codec
, hda_nid_t nid
)
1121 struct hda_pincfg
*pin
;
1123 #ifdef CONFIG_SND_HDA_HWDEP
1125 unsigned int cfg
= 0;
1126 mutex_lock(&codec
->user_mutex
);
1127 pin
= look_up_pincfg(codec
, &codec
->user_pins
, nid
);
1130 mutex_unlock(&codec
->user_mutex
);
1135 pin
= look_up_pincfg(codec
, &codec
->driver_pins
, nid
);
1138 pin
= look_up_pincfg(codec
, &codec
->init_pins
, nid
);
1143 EXPORT_SYMBOL_HDA(snd_hda_codec_get_pincfg
);
1145 /* remember the current pinctl target value */
1146 int snd_hda_codec_set_pin_target(struct hda_codec
*codec
, hda_nid_t nid
,
1149 struct hda_pincfg
*pin
;
1151 pin
= look_up_pincfg(codec
, &codec
->init_pins
, nid
);
1157 EXPORT_SYMBOL_HDA(snd_hda_codec_set_pin_target
);
1159 /* return the current pinctl target value */
1160 int snd_hda_codec_get_pin_target(struct hda_codec
*codec
, hda_nid_t nid
)
1162 struct hda_pincfg
*pin
;
1164 pin
= look_up_pincfg(codec
, &codec
->init_pins
, nid
);
1169 EXPORT_SYMBOL_HDA(snd_hda_codec_get_pin_target
);
1172 * snd_hda_shutup_pins - Shut up all pins
1173 * @codec: the HDA codec
1175 * Clear all pin controls to shup up before suspend for avoiding click noise.
1176 * The controls aren't cached so that they can be resumed properly.
1178 void snd_hda_shutup_pins(struct hda_codec
*codec
)
1181 /* don't shut up pins when unloading the driver; otherwise it breaks
1182 * the default pin setup at the next load of the driver
1184 if (codec
->bus
->shutdown
)
1186 for (i
= 0; i
< codec
->init_pins
.used
; i
++) {
1187 struct hda_pincfg
*pin
= snd_array_elem(&codec
->init_pins
, i
);
1188 /* use read here for syncing after issuing each verb */
1189 snd_hda_codec_read(codec
, pin
->nid
, 0,
1190 AC_VERB_SET_PIN_WIDGET_CONTROL
, 0);
1192 codec
->pins_shutup
= 1;
1194 EXPORT_SYMBOL_HDA(snd_hda_shutup_pins
);
1197 /* Restore the pin controls cleared previously via snd_hda_shutup_pins() */
1198 static void restore_shutup_pins(struct hda_codec
*codec
)
1201 if (!codec
->pins_shutup
)
1203 if (codec
->bus
->shutdown
)
1205 for (i
= 0; i
< codec
->init_pins
.used
; i
++) {
1206 struct hda_pincfg
*pin
= snd_array_elem(&codec
->init_pins
, i
);
1207 snd_hda_codec_write(codec
, pin
->nid
, 0,
1208 AC_VERB_SET_PIN_WIDGET_CONTROL
,
1211 codec
->pins_shutup
= 0;
1215 static void hda_jackpoll_work(struct work_struct
*work
)
1217 struct hda_codec
*codec
=
1218 container_of(work
, struct hda_codec
, jackpoll_work
.work
);
1219 if (!codec
->jackpoll_interval
)
1222 snd_hda_jack_set_dirty_all(codec
);
1223 snd_hda_jack_poll_all(codec
);
1224 queue_delayed_work(codec
->bus
->workq
, &codec
->jackpoll_work
,
1225 codec
->jackpoll_interval
);
1228 static void init_hda_cache(struct hda_cache_rec
*cache
,
1229 unsigned int record_size
);
1230 static void free_hda_cache(struct hda_cache_rec
*cache
);
1232 /* release all pincfg lists */
1233 static void free_init_pincfgs(struct hda_codec
*codec
)
1235 snd_array_free(&codec
->driver_pins
);
1236 #ifdef CONFIG_SND_HDA_HWDEP
1237 snd_array_free(&codec
->user_pins
);
1239 snd_array_free(&codec
->init_pins
);
1243 * audio-converter setup caches
1245 struct hda_cvt_setup
{
1250 unsigned char active
; /* cvt is currently used */
1251 unsigned char dirty
; /* setups should be cleared */
1254 /* get or create a cache entry for the given audio converter NID */
1255 static struct hda_cvt_setup
*
1256 get_hda_cvt_setup(struct hda_codec
*codec
, hda_nid_t nid
)
1258 struct hda_cvt_setup
*p
;
1261 for (i
= 0; i
< codec
->cvt_setups
.used
; i
++) {
1262 p
= snd_array_elem(&codec
->cvt_setups
, i
);
1266 p
= snd_array_new(&codec
->cvt_setups
);
1275 static void snd_hda_codec_free(struct hda_codec
*codec
)
1279 cancel_delayed_work_sync(&codec
->jackpoll_work
);
1280 snd_hda_jack_tbl_clear(codec
);
1281 free_init_pincfgs(codec
);
1283 cancel_delayed_work(&codec
->power_work
);
1284 flush_workqueue(codec
->bus
->workq
);
1286 list_del(&codec
->list
);
1287 snd_array_free(&codec
->mixers
);
1288 snd_array_free(&codec
->nids
);
1289 snd_array_free(&codec
->cvt_setups
);
1290 snd_array_free(&codec
->spdif_out
);
1291 remove_conn_list(codec
);
1292 codec
->bus
->caddr_tbl
[codec
->addr
] = NULL
;
1293 if (codec
->patch_ops
.free
)
1294 codec
->patch_ops
.free(codec
);
1296 if (!codec
->pm_down_notified
) /* cancel leftover refcounts */
1297 hda_call_pm_notify(codec
->bus
, false);
1299 module_put(codec
->owner
);
1300 free_hda_cache(&codec
->amp_cache
);
1301 free_hda_cache(&codec
->cmd_cache
);
1302 kfree(codec
->vendor_name
);
1303 kfree(codec
->chip_name
);
1304 kfree(codec
->modelname
);
1305 kfree(codec
->wcaps
);
1309 static bool snd_hda_codec_get_supported_ps(struct hda_codec
*codec
,
1310 hda_nid_t fg
, unsigned int power_state
);
1312 static unsigned int hda_set_power_state(struct hda_codec
*codec
,
1313 unsigned int power_state
);
1316 * snd_hda_codec_new - create a HDA codec
1317 * @bus: the bus to assign
1318 * @codec_addr: the codec address
1319 * @codecp: the pointer to store the generated codec
1321 * Returns 0 if successful, or a negative error code.
1323 int snd_hda_codec_new(struct hda_bus
*bus
,
1324 unsigned int codec_addr
,
1325 struct hda_codec
**codecp
)
1327 struct hda_codec
*codec
;
1332 if (snd_BUG_ON(!bus
))
1334 if (snd_BUG_ON(codec_addr
> HDA_MAX_CODEC_ADDRESS
))
1337 if (bus
->caddr_tbl
[codec_addr
]) {
1338 snd_printk(KERN_ERR
"hda_codec: "
1339 "address 0x%x is already occupied\n", codec_addr
);
1343 codec
= kzalloc(sizeof(*codec
), GFP_KERNEL
);
1344 if (codec
== NULL
) {
1345 snd_printk(KERN_ERR
"can't allocate struct hda_codec\n");
1350 codec
->addr
= codec_addr
;
1351 mutex_init(&codec
->spdif_mutex
);
1352 mutex_init(&codec
->control_mutex
);
1353 mutex_init(&codec
->hash_mutex
);
1354 init_hda_cache(&codec
->amp_cache
, sizeof(struct hda_amp_info
));
1355 init_hda_cache(&codec
->cmd_cache
, sizeof(struct hda_cache_head
));
1356 snd_array_init(&codec
->mixers
, sizeof(struct hda_nid_item
), 32);
1357 snd_array_init(&codec
->nids
, sizeof(struct hda_nid_item
), 32);
1358 snd_array_init(&codec
->init_pins
, sizeof(struct hda_pincfg
), 16);
1359 snd_array_init(&codec
->driver_pins
, sizeof(struct hda_pincfg
), 16);
1360 snd_array_init(&codec
->cvt_setups
, sizeof(struct hda_cvt_setup
), 8);
1361 snd_array_init(&codec
->spdif_out
, sizeof(struct hda_spdif_out
), 16);
1362 snd_array_init(&codec
->jacktbl
, sizeof(struct hda_jack_tbl
), 16);
1363 snd_array_init(&codec
->verbs
, sizeof(struct hda_verb
*), 8);
1364 INIT_LIST_HEAD(&codec
->conn_list
);
1366 INIT_DELAYED_WORK(&codec
->jackpoll_work
, hda_jackpoll_work
);
1369 spin_lock_init(&codec
->power_lock
);
1370 INIT_DELAYED_WORK(&codec
->power_work
, hda_power_work
);
1371 /* snd_hda_codec_new() marks the codec as power-up, and leave it as is.
1372 * the caller has to power down appropriatley after initialization
1375 hda_keep_power_on(codec
);
1376 hda_call_pm_notify(bus
, true);
1379 if (codec
->bus
->modelname
) {
1380 codec
->modelname
= kstrdup(codec
->bus
->modelname
, GFP_KERNEL
);
1381 if (!codec
->modelname
) {
1382 snd_hda_codec_free(codec
);
1387 list_add_tail(&codec
->list
, &bus
->codec_list
);
1388 bus
->caddr_tbl
[codec_addr
] = codec
;
1390 codec
->vendor_id
= snd_hda_param_read(codec
, AC_NODE_ROOT
,
1392 if (codec
->vendor_id
== -1)
1393 /* read again, hopefully the access method was corrected
1394 * in the last read...
1396 codec
->vendor_id
= snd_hda_param_read(codec
, AC_NODE_ROOT
,
1398 codec
->subsystem_id
= snd_hda_param_read(codec
, AC_NODE_ROOT
,
1399 AC_PAR_SUBSYSTEM_ID
);
1400 codec
->revision_id
= snd_hda_param_read(codec
, AC_NODE_ROOT
,
1403 setup_fg_nodes(codec
);
1404 if (!codec
->afg
&& !codec
->mfg
) {
1405 snd_printdd("hda_codec: no AFG or MFG node found\n");
1410 fg
= codec
->afg
? codec
->afg
: codec
->mfg
;
1411 err
= read_widget_caps(codec
, fg
);
1413 snd_printk(KERN_ERR
"hda_codec: cannot malloc\n");
1416 err
= read_pin_defaults(codec
);
1420 if (!codec
->subsystem_id
) {
1421 codec
->subsystem_id
=
1422 snd_hda_codec_read(codec
, fg
, 0,
1423 AC_VERB_GET_SUBSYSTEM_ID
, 0);
1427 codec
->d3_stop_clk
= snd_hda_codec_get_supported_ps(codec
, fg
,
1429 if (!codec
->d3_stop_clk
)
1430 bus
->power_keep_link_on
= 1;
1432 codec
->epss
= snd_hda_codec_get_supported_ps(codec
, fg
,
1435 /* power-up all before initialization */
1436 hda_set_power_state(codec
, AC_PWRST_D0
);
1438 snd_hda_codec_proc_new(codec
);
1440 snd_hda_create_hwdep(codec
);
1442 sprintf(component
, "HDA:%08x,%08x,%08x", codec
->vendor_id
,
1443 codec
->subsystem_id
, codec
->revision_id
);
1444 snd_component_add(codec
->bus
->card
, component
);
1451 snd_hda_codec_free(codec
);
1454 EXPORT_SYMBOL_HDA(snd_hda_codec_new
);
1456 int snd_hda_codec_update_widgets(struct hda_codec
*codec
)
1461 /* Assume the function group node does not change,
1462 * only the widget nodes may change.
1464 kfree(codec
->wcaps
);
1465 fg
= codec
->afg
? codec
->afg
: codec
->mfg
;
1466 err
= read_widget_caps(codec
, fg
);
1468 snd_printk(KERN_ERR
"hda_codec: cannot malloc\n");
1472 snd_array_free(&codec
->init_pins
);
1473 err
= read_pin_defaults(codec
);
1477 EXPORT_SYMBOL_HDA(snd_hda_codec_update_widgets
);
1481 * snd_hda_codec_configure - (Re-)configure the HD-audio codec
1482 * @codec: the HDA codec
1484 * Start parsing of the given codec tree and (re-)initialize the whole
1487 * Returns 0 if successful or a negative error code.
1489 int snd_hda_codec_configure(struct hda_codec
*codec
)
1493 codec
->preset
= find_codec_preset(codec
);
1494 if (!codec
->vendor_name
|| !codec
->chip_name
) {
1495 err
= get_codec_name(codec
);
1500 if (is_generic_config(codec
)) {
1501 err
= snd_hda_parse_generic_codec(codec
);
1504 if (codec
->preset
&& codec
->preset
->patch
) {
1505 err
= codec
->preset
->patch(codec
);
1509 /* call the default parser */
1510 err
= snd_hda_parse_generic_codec(codec
);
1512 printk(KERN_ERR
"hda-codec: No codec parser is available\n");
1515 if (!err
&& codec
->patch_ops
.unsol_event
)
1516 err
= init_unsol_queue(codec
->bus
);
1517 /* audio codec should override the mixer name */
1518 if (!err
&& (codec
->afg
|| !*codec
->bus
->card
->mixername
))
1519 snprintf(codec
->bus
->card
->mixername
,
1520 sizeof(codec
->bus
->card
->mixername
),
1521 "%s %s", codec
->vendor_name
, codec
->chip_name
);
1524 EXPORT_SYMBOL_HDA(snd_hda_codec_configure
);
1526 /* update the stream-id if changed */
1527 static void update_pcm_stream_id(struct hda_codec
*codec
,
1528 struct hda_cvt_setup
*p
, hda_nid_t nid
,
1529 u32 stream_tag
, int channel_id
)
1531 unsigned int oldval
, newval
;
1533 if (p
->stream_tag
!= stream_tag
|| p
->channel_id
!= channel_id
) {
1534 oldval
= snd_hda_codec_read(codec
, nid
, 0, AC_VERB_GET_CONV
, 0);
1535 newval
= (stream_tag
<< 4) | channel_id
;
1536 if (oldval
!= newval
)
1537 snd_hda_codec_write(codec
, nid
, 0,
1538 AC_VERB_SET_CHANNEL_STREAMID
,
1540 p
->stream_tag
= stream_tag
;
1541 p
->channel_id
= channel_id
;
1545 /* update the format-id if changed */
1546 static void update_pcm_format(struct hda_codec
*codec
, struct hda_cvt_setup
*p
,
1547 hda_nid_t nid
, int format
)
1549 unsigned int oldval
;
1551 if (p
->format_id
!= format
) {
1552 oldval
= snd_hda_codec_read(codec
, nid
, 0,
1553 AC_VERB_GET_STREAM_FORMAT
, 0);
1554 if (oldval
!= format
) {
1556 snd_hda_codec_write(codec
, nid
, 0,
1557 AC_VERB_SET_STREAM_FORMAT
,
1560 p
->format_id
= format
;
1565 * snd_hda_codec_setup_stream - set up the codec for streaming
1566 * @codec: the CODEC to set up
1567 * @nid: the NID to set up
1568 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
1569 * @channel_id: channel id to pass, zero based.
1570 * @format: stream format.
1572 void snd_hda_codec_setup_stream(struct hda_codec
*codec
, hda_nid_t nid
,
1574 int channel_id
, int format
)
1576 struct hda_codec
*c
;
1577 struct hda_cvt_setup
*p
;
1584 snd_printdd("hda_codec_setup_stream: "
1585 "NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
1586 nid
, stream_tag
, channel_id
, format
);
1587 p
= get_hda_cvt_setup(codec
, nid
);
1591 if (codec
->pcm_format_first
)
1592 update_pcm_format(codec
, p
, nid
, format
);
1593 update_pcm_stream_id(codec
, p
, nid
, stream_tag
, channel_id
);
1594 if (!codec
->pcm_format_first
)
1595 update_pcm_format(codec
, p
, nid
, format
);
1600 /* make other inactive cvts with the same stream-tag dirty */
1601 type
= get_wcaps_type(get_wcaps(codec
, nid
));
1602 list_for_each_entry(c
, &codec
->bus
->codec_list
, list
) {
1603 for (i
= 0; i
< c
->cvt_setups
.used
; i
++) {
1604 p
= snd_array_elem(&c
->cvt_setups
, i
);
1605 if (!p
->active
&& p
->stream_tag
== stream_tag
&&
1606 get_wcaps_type(get_wcaps(c
, p
->nid
)) == type
)
1611 EXPORT_SYMBOL_HDA(snd_hda_codec_setup_stream
);
1613 static void really_cleanup_stream(struct hda_codec
*codec
,
1614 struct hda_cvt_setup
*q
);
1617 * __snd_hda_codec_cleanup_stream - clean up the codec for closing
1618 * @codec: the CODEC to clean up
1619 * @nid: the NID to clean up
1620 * @do_now: really clean up the stream instead of clearing the active flag
1622 void __snd_hda_codec_cleanup_stream(struct hda_codec
*codec
, hda_nid_t nid
,
1625 struct hda_cvt_setup
*p
;
1630 if (codec
->no_sticky_stream
)
1633 snd_printdd("hda_codec_cleanup_stream: NID=0x%x\n", nid
);
1634 p
= get_hda_cvt_setup(codec
, nid
);
1636 /* here we just clear the active flag when do_now isn't set;
1637 * actual clean-ups will be done later in
1638 * purify_inactive_streams() called from snd_hda_codec_prpapre()
1641 really_cleanup_stream(codec
, p
);
1646 EXPORT_SYMBOL_HDA(__snd_hda_codec_cleanup_stream
);
1648 static void really_cleanup_stream(struct hda_codec
*codec
,
1649 struct hda_cvt_setup
*q
)
1651 hda_nid_t nid
= q
->nid
;
1652 if (q
->stream_tag
|| q
->channel_id
)
1653 snd_hda_codec_write(codec
, nid
, 0, AC_VERB_SET_CHANNEL_STREAMID
, 0);
1655 snd_hda_codec_write(codec
, nid
, 0, AC_VERB_SET_STREAM_FORMAT
, 0
1657 memset(q
, 0, sizeof(*q
));
1661 /* clean up the all conflicting obsolete streams */
1662 static void purify_inactive_streams(struct hda_codec
*codec
)
1664 struct hda_codec
*c
;
1667 list_for_each_entry(c
, &codec
->bus
->codec_list
, list
) {
1668 for (i
= 0; i
< c
->cvt_setups
.used
; i
++) {
1669 struct hda_cvt_setup
*p
;
1670 p
= snd_array_elem(&c
->cvt_setups
, i
);
1672 really_cleanup_stream(c
, p
);
1678 /* clean up all streams; called from suspend */
1679 static void hda_cleanup_all_streams(struct hda_codec
*codec
)
1683 for (i
= 0; i
< codec
->cvt_setups
.used
; i
++) {
1684 struct hda_cvt_setup
*p
= snd_array_elem(&codec
->cvt_setups
, i
);
1686 really_cleanup_stream(codec
, p
);
1692 * amp access functions
1695 /* FIXME: more better hash key? */
1696 #define HDA_HASH_KEY(nid, dir, idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
1697 #define HDA_HASH_PINCAP_KEY(nid) (u32)((nid) + (0x02 << 24))
1698 #define HDA_HASH_PARPCM_KEY(nid) (u32)((nid) + (0x03 << 24))
1699 #define HDA_HASH_PARSTR_KEY(nid) (u32)((nid) + (0x04 << 24))
1700 #define INFO_AMP_CAPS (1<<0)
1701 #define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
1703 /* initialize the hash table */
1704 static void init_hda_cache(struct hda_cache_rec
*cache
,
1705 unsigned int record_size
)
1707 memset(cache
, 0, sizeof(*cache
));
1708 memset(cache
->hash
, 0xff, sizeof(cache
->hash
));
1709 snd_array_init(&cache
->buf
, record_size
, 64);
1712 static void free_hda_cache(struct hda_cache_rec
*cache
)
1714 snd_array_free(&cache
->buf
);
1717 /* query the hash. allocate an entry if not found. */
1718 static struct hda_cache_head
*get_hash(struct hda_cache_rec
*cache
, u32 key
)
1720 u16 idx
= key
% (u16
)ARRAY_SIZE(cache
->hash
);
1721 u16 cur
= cache
->hash
[idx
];
1722 struct hda_cache_head
*info
;
1724 while (cur
!= 0xffff) {
1725 info
= snd_array_elem(&cache
->buf
, cur
);
1726 if (info
->key
== key
)
1733 /* query the hash. allocate an entry if not found. */
1734 static struct hda_cache_head
*get_alloc_hash(struct hda_cache_rec
*cache
,
1737 struct hda_cache_head
*info
= get_hash(cache
, key
);
1740 /* add a new hash entry */
1741 info
= snd_array_new(&cache
->buf
);
1744 cur
= snd_array_index(&cache
->buf
, info
);
1748 idx
= key
% (u16
)ARRAY_SIZE(cache
->hash
);
1749 info
->next
= cache
->hash
[idx
];
1750 cache
->hash
[idx
] = cur
;
1755 /* query and allocate an amp hash entry */
1756 static inline struct hda_amp_info
*
1757 get_alloc_amp_hash(struct hda_codec
*codec
, u32 key
)
1759 return (struct hda_amp_info
*)get_alloc_hash(&codec
->amp_cache
, key
);
1762 /* overwrite the value with the key in the caps hash */
1763 static int write_caps_hash(struct hda_codec
*codec
, u32 key
, unsigned int val
)
1765 struct hda_amp_info
*info
;
1767 mutex_lock(&codec
->hash_mutex
);
1768 info
= get_alloc_amp_hash(codec
, key
);
1770 mutex_unlock(&codec
->hash_mutex
);
1773 info
->amp_caps
= val
;
1774 info
->head
.val
|= INFO_AMP_CAPS
;
1775 mutex_unlock(&codec
->hash_mutex
);
1779 /* query the value from the caps hash; if not found, fetch the current
1780 * value from the given function and store in the hash
1783 query_caps_hash(struct hda_codec
*codec
, hda_nid_t nid
, int dir
, u32 key
,
1784 unsigned int (*func
)(struct hda_codec
*, hda_nid_t
, int))
1786 struct hda_amp_info
*info
;
1789 mutex_lock(&codec
->hash_mutex
);
1790 info
= get_alloc_amp_hash(codec
, key
);
1792 mutex_unlock(&codec
->hash_mutex
);
1795 if (!(info
->head
.val
& INFO_AMP_CAPS
)) {
1796 mutex_unlock(&codec
->hash_mutex
); /* for reentrance */
1797 val
= func(codec
, nid
, dir
);
1798 write_caps_hash(codec
, key
, val
);
1800 val
= info
->amp_caps
;
1801 mutex_unlock(&codec
->hash_mutex
);
1806 static unsigned int read_amp_cap(struct hda_codec
*codec
, hda_nid_t nid
,
1809 if (!(get_wcaps(codec
, nid
) & AC_WCAP_AMP_OVRD
))
1811 return snd_hda_param_read(codec
, nid
,
1812 direction
== HDA_OUTPUT
?
1813 AC_PAR_AMP_OUT_CAP
: AC_PAR_AMP_IN_CAP
);
1817 * query_amp_caps - query AMP capabilities
1818 * @codec: the HD-auio codec
1819 * @nid: the NID to query
1820 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1822 * Query AMP capabilities for the given widget and direction.
1823 * Returns the obtained capability bits.
1825 * When cap bits have been already read, this doesn't read again but
1826 * returns the cached value.
1828 u32
query_amp_caps(struct hda_codec
*codec
, hda_nid_t nid
, int direction
)
1830 return query_caps_hash(codec
, nid
, direction
,
1831 HDA_HASH_KEY(nid
, direction
, 0),
1834 EXPORT_SYMBOL_HDA(query_amp_caps
);
1837 * snd_hda_override_amp_caps - Override the AMP capabilities
1838 * @codec: the CODEC to clean up
1839 * @nid: the NID to clean up
1840 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1841 * @caps: the capability bits to set
1843 * Override the cached AMP caps bits value by the given one.
1844 * This function is useful if the driver needs to adjust the AMP ranges,
1845 * e.g. limit to 0dB, etc.
1847 * Returns zero if successful or a negative error code.
1849 int snd_hda_override_amp_caps(struct hda_codec
*codec
, hda_nid_t nid
, int dir
,
1852 return write_caps_hash(codec
, HDA_HASH_KEY(nid
, dir
, 0), caps
);
1854 EXPORT_SYMBOL_HDA(snd_hda_override_amp_caps
);
1856 static unsigned int read_pin_cap(struct hda_codec
*codec
, hda_nid_t nid
,
1859 return snd_hda_param_read(codec
, nid
, AC_PAR_PIN_CAP
);
1863 * snd_hda_query_pin_caps - Query PIN capabilities
1864 * @codec: the HD-auio codec
1865 * @nid: the NID to query
1867 * Query PIN capabilities for the given widget.
1868 * Returns the obtained capability bits.
1870 * When cap bits have been already read, this doesn't read again but
1871 * returns the cached value.
1873 u32
snd_hda_query_pin_caps(struct hda_codec
*codec
, hda_nid_t nid
)
1875 return query_caps_hash(codec
, nid
, 0, HDA_HASH_PINCAP_KEY(nid
),
1878 EXPORT_SYMBOL_HDA(snd_hda_query_pin_caps
);
1881 * snd_hda_override_pin_caps - Override the pin capabilities
1883 * @nid: the NID to override
1884 * @caps: the capability bits to set
1886 * Override the cached PIN capabilitiy bits value by the given one.
1888 * Returns zero if successful or a negative error code.
1890 int snd_hda_override_pin_caps(struct hda_codec
*codec
, hda_nid_t nid
,
1893 return write_caps_hash(codec
, HDA_HASH_PINCAP_KEY(nid
), caps
);
1895 EXPORT_SYMBOL_HDA(snd_hda_override_pin_caps
);
1897 /* read or sync the hash value with the current value;
1898 * call within hash_mutex
1900 static struct hda_amp_info
*
1901 update_amp_hash(struct hda_codec
*codec
, hda_nid_t nid
, int ch
,
1902 int direction
, int index
, bool init_only
)
1904 struct hda_amp_info
*info
;
1905 unsigned int parm
, val
= 0;
1906 bool val_read
= false;
1909 info
= get_alloc_amp_hash(codec
, HDA_HASH_KEY(nid
, direction
, index
));
1912 if (!(info
->head
.val
& INFO_AMP_VOL(ch
))) {
1914 mutex_unlock(&codec
->hash_mutex
);
1915 parm
= ch
? AC_AMP_GET_RIGHT
: AC_AMP_GET_LEFT
;
1916 parm
|= direction
== HDA_OUTPUT
?
1917 AC_AMP_GET_OUTPUT
: AC_AMP_GET_INPUT
;
1919 val
= snd_hda_codec_read(codec
, nid
, 0,
1920 AC_VERB_GET_AMP_GAIN_MUTE
, parm
);
1923 mutex_lock(&codec
->hash_mutex
);
1926 info
->vol
[ch
] = val
;
1927 info
->head
.val
|= INFO_AMP_VOL(ch
);
1928 } else if (init_only
)
1934 * write the current volume in info to the h/w
1936 static void put_vol_mute(struct hda_codec
*codec
, unsigned int amp_caps
,
1937 hda_nid_t nid
, int ch
, int direction
, int index
,
1942 parm
= ch
? AC_AMP_SET_RIGHT
: AC_AMP_SET_LEFT
;
1943 parm
|= direction
== HDA_OUTPUT
? AC_AMP_SET_OUTPUT
: AC_AMP_SET_INPUT
;
1944 parm
|= index
<< AC_AMP_SET_INDEX_SHIFT
;
1945 if ((val
& HDA_AMP_MUTE
) && !(amp_caps
& AC_AMPCAP_MUTE
) &&
1946 (amp_caps
& AC_AMPCAP_MIN_MUTE
))
1947 ; /* set the zero value as a fake mute */
1950 snd_hda_codec_write(codec
, nid
, 0, AC_VERB_SET_AMP_GAIN_MUTE
, parm
);
1954 * snd_hda_codec_amp_read - Read AMP value
1955 * @codec: HD-audio codec
1956 * @nid: NID to read the AMP value
1957 * @ch: channel (left=0 or right=1)
1958 * @direction: #HDA_INPUT or #HDA_OUTPUT
1959 * @index: the index value (only for input direction)
1961 * Read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
1963 int snd_hda_codec_amp_read(struct hda_codec
*codec
, hda_nid_t nid
, int ch
,
1964 int direction
, int index
)
1966 struct hda_amp_info
*info
;
1967 unsigned int val
= 0;
1969 mutex_lock(&codec
->hash_mutex
);
1970 info
= update_amp_hash(codec
, nid
, ch
, direction
, index
, false);
1972 val
= info
->vol
[ch
];
1973 mutex_unlock(&codec
->hash_mutex
);
1976 EXPORT_SYMBOL_HDA(snd_hda_codec_amp_read
);
1978 static int codec_amp_update(struct hda_codec
*codec
, hda_nid_t nid
, int ch
,
1979 int direction
, int idx
, int mask
, int val
,
1982 struct hda_amp_info
*info
;
1984 unsigned int cache_only
;
1986 if (snd_BUG_ON(mask
& ~0xff))
1990 mutex_lock(&codec
->hash_mutex
);
1991 info
= update_amp_hash(codec
, nid
, ch
, direction
, idx
, init_only
);
1993 mutex_unlock(&codec
->hash_mutex
);
1996 val
|= info
->vol
[ch
] & ~mask
;
1997 if (info
->vol
[ch
] == val
) {
1998 mutex_unlock(&codec
->hash_mutex
);
2001 info
->vol
[ch
] = val
;
2002 cache_only
= info
->head
.dirty
= codec
->cached_write
;
2003 caps
= info
->amp_caps
;
2004 mutex_unlock(&codec
->hash_mutex
);
2006 put_vol_mute(codec
, caps
, nid
, ch
, direction
, idx
, val
);
2011 * snd_hda_codec_amp_update - update the AMP value
2012 * @codec: HD-audio codec
2013 * @nid: NID to read the AMP value
2014 * @ch: channel (left=0 or right=1)
2015 * @direction: #HDA_INPUT or #HDA_OUTPUT
2016 * @idx: the index value (only for input direction)
2017 * @mask: bit mask to set
2018 * @val: the bits value to set
2020 * Update the AMP value with a bit mask.
2021 * Returns 0 if the value is unchanged, 1 if changed.
2023 int snd_hda_codec_amp_update(struct hda_codec
*codec
, hda_nid_t nid
, int ch
,
2024 int direction
, int idx
, int mask
, int val
)
2026 return codec_amp_update(codec
, nid
, ch
, direction
, idx
, mask
, val
, false);
2028 EXPORT_SYMBOL_HDA(snd_hda_codec_amp_update
);
2031 * snd_hda_codec_amp_stereo - update the AMP stereo values
2032 * @codec: HD-audio codec
2033 * @nid: NID to read the AMP value
2034 * @direction: #HDA_INPUT or #HDA_OUTPUT
2035 * @idx: the index value (only for input direction)
2036 * @mask: bit mask to set
2037 * @val: the bits value to set
2039 * Update the AMP values like snd_hda_codec_amp_update(), but for a
2040 * stereo widget with the same mask and value.
2042 int snd_hda_codec_amp_stereo(struct hda_codec
*codec
, hda_nid_t nid
,
2043 int direction
, int idx
, int mask
, int val
)
2047 if (snd_BUG_ON(mask
& ~0xff))
2049 for (ch
= 0; ch
< 2; ch
++)
2050 ret
|= snd_hda_codec_amp_update(codec
, nid
, ch
, direction
,
2054 EXPORT_SYMBOL_HDA(snd_hda_codec_amp_stereo
);
2056 /* Works like snd_hda_codec_amp_update() but it writes the value only at
2057 * the first access. If the amp was already initialized / updated beforehand,
2058 * this does nothing.
2060 int snd_hda_codec_amp_init(struct hda_codec
*codec
, hda_nid_t nid
, int ch
,
2061 int dir
, int idx
, int mask
, int val
)
2063 return codec_amp_update(codec
, nid
, ch
, dir
, idx
, mask
, val
, true);
2065 EXPORT_SYMBOL_HDA(snd_hda_codec_amp_init
);
2067 int snd_hda_codec_amp_init_stereo(struct hda_codec
*codec
, hda_nid_t nid
,
2068 int dir
, int idx
, int mask
, int val
)
2072 if (snd_BUG_ON(mask
& ~0xff))
2074 for (ch
= 0; ch
< 2; ch
++)
2075 ret
|= snd_hda_codec_amp_init(codec
, nid
, ch
, dir
,
2079 EXPORT_SYMBOL_HDA(snd_hda_codec_amp_init_stereo
);
2082 * snd_hda_codec_resume_amp - Resume all AMP commands from the cache
2083 * @codec: HD-audio codec
2085 * Resume the all amp commands from the cache.
2087 void snd_hda_codec_resume_amp(struct hda_codec
*codec
)
2091 mutex_lock(&codec
->hash_mutex
);
2092 codec
->cached_write
= 0;
2093 for (i
= 0; i
< codec
->amp_cache
.buf
.used
; i
++) {
2094 struct hda_amp_info
*buffer
;
2097 unsigned int idx
, dir
, ch
;
2098 struct hda_amp_info info
;
2100 buffer
= snd_array_elem(&codec
->amp_cache
.buf
, i
);
2101 if (!buffer
->head
.dirty
)
2103 buffer
->head
.dirty
= 0;
2105 key
= info
.head
.key
;
2109 idx
= (key
>> 16) & 0xff;
2110 dir
= (key
>> 24) & 0xff;
2111 for (ch
= 0; ch
< 2; ch
++) {
2112 if (!(info
.head
.val
& INFO_AMP_VOL(ch
)))
2114 mutex_unlock(&codec
->hash_mutex
);
2115 put_vol_mute(codec
, info
.amp_caps
, nid
, ch
, dir
, idx
,
2117 mutex_lock(&codec
->hash_mutex
);
2120 mutex_unlock(&codec
->hash_mutex
);
2122 EXPORT_SYMBOL_HDA(snd_hda_codec_resume_amp
);
2124 static u32
get_amp_max_value(struct hda_codec
*codec
, hda_nid_t nid
, int dir
,
2127 u32 caps
= query_amp_caps(codec
, nid
, dir
);
2129 caps
= (caps
& AC_AMPCAP_NUM_STEPS
) >> AC_AMPCAP_NUM_STEPS_SHIFT
;
2136 * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer
2138 * The control element is supposed to have the private_value field
2139 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2141 int snd_hda_mixer_amp_volume_info(struct snd_kcontrol
*kcontrol
,
2142 struct snd_ctl_elem_info
*uinfo
)
2144 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2145 u16 nid
= get_amp_nid(kcontrol
);
2146 u8 chs
= get_amp_channels(kcontrol
);
2147 int dir
= get_amp_direction(kcontrol
);
2148 unsigned int ofs
= get_amp_offset(kcontrol
);
2150 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
2151 uinfo
->count
= chs
== 3 ? 2 : 1;
2152 uinfo
->value
.integer
.min
= 0;
2153 uinfo
->value
.integer
.max
= get_amp_max_value(codec
, nid
, dir
, ofs
);
2154 if (!uinfo
->value
.integer
.max
) {
2155 printk(KERN_WARNING
"hda_codec: "
2156 "num_steps = 0 for NID=0x%x (ctl = %s)\n", nid
,
2162 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_info
);
2165 static inline unsigned int
2166 read_amp_value(struct hda_codec
*codec
, hda_nid_t nid
,
2167 int ch
, int dir
, int idx
, unsigned int ofs
)
2170 val
= snd_hda_codec_amp_read(codec
, nid
, ch
, dir
, idx
);
2171 val
&= HDA_AMP_VOLMASK
;
2180 update_amp_value(struct hda_codec
*codec
, hda_nid_t nid
,
2181 int ch
, int dir
, int idx
, unsigned int ofs
,
2184 unsigned int maxval
;
2188 /* ofs = 0: raw max value */
2189 maxval
= get_amp_max_value(codec
, nid
, dir
, 0);
2192 return snd_hda_codec_amp_update(codec
, nid
, ch
, dir
, idx
,
2193 HDA_AMP_VOLMASK
, val
);
2197 * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume
2199 * The control element is supposed to have the private_value field
2200 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2202 int snd_hda_mixer_amp_volume_get(struct snd_kcontrol
*kcontrol
,
2203 struct snd_ctl_elem_value
*ucontrol
)
2205 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2206 hda_nid_t nid
= get_amp_nid(kcontrol
);
2207 int chs
= get_amp_channels(kcontrol
);
2208 int dir
= get_amp_direction(kcontrol
);
2209 int idx
= get_amp_index(kcontrol
);
2210 unsigned int ofs
= get_amp_offset(kcontrol
);
2211 long *valp
= ucontrol
->value
.integer
.value
;
2214 *valp
++ = read_amp_value(codec
, nid
, 0, dir
, idx
, ofs
);
2216 *valp
= read_amp_value(codec
, nid
, 1, dir
, idx
, ofs
);
2219 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_get
);
2222 * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume
2224 * The control element is supposed to have the private_value field
2225 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2227 int snd_hda_mixer_amp_volume_put(struct snd_kcontrol
*kcontrol
,
2228 struct snd_ctl_elem_value
*ucontrol
)
2230 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2231 hda_nid_t nid
= get_amp_nid(kcontrol
);
2232 int chs
= get_amp_channels(kcontrol
);
2233 int dir
= get_amp_direction(kcontrol
);
2234 int idx
= get_amp_index(kcontrol
);
2235 unsigned int ofs
= get_amp_offset(kcontrol
);
2236 long *valp
= ucontrol
->value
.integer
.value
;
2239 snd_hda_power_up(codec
);
2241 change
= update_amp_value(codec
, nid
, 0, dir
, idx
, ofs
, *valp
);
2245 change
|= update_amp_value(codec
, nid
, 1, dir
, idx
, ofs
, *valp
);
2246 snd_hda_power_down(codec
);
2249 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_put
);
2252 * snd_hda_mixer_amp_volume_put - TLV callback for a standard AMP mixer volume
2254 * The control element is supposed to have the private_value field
2255 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2257 int snd_hda_mixer_amp_tlv(struct snd_kcontrol
*kcontrol
, int op_flag
,
2258 unsigned int size
, unsigned int __user
*_tlv
)
2260 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2261 hda_nid_t nid
= get_amp_nid(kcontrol
);
2262 int dir
= get_amp_direction(kcontrol
);
2263 unsigned int ofs
= get_amp_offset(kcontrol
);
2264 bool min_mute
= get_amp_min_mute(kcontrol
);
2265 u32 caps
, val1
, val2
;
2267 if (size
< 4 * sizeof(unsigned int))
2269 caps
= query_amp_caps(codec
, nid
, dir
);
2270 val2
= (caps
& AC_AMPCAP_STEP_SIZE
) >> AC_AMPCAP_STEP_SIZE_SHIFT
;
2271 val2
= (val2
+ 1) * 25;
2272 val1
= -((caps
& AC_AMPCAP_OFFSET
) >> AC_AMPCAP_OFFSET_SHIFT
);
2274 val1
= ((int)val1
) * ((int)val2
);
2275 if (min_mute
|| (caps
& AC_AMPCAP_MIN_MUTE
))
2276 val2
|= TLV_DB_SCALE_MUTE
;
2277 if (put_user(SNDRV_CTL_TLVT_DB_SCALE
, _tlv
))
2279 if (put_user(2 * sizeof(unsigned int), _tlv
+ 1))
2281 if (put_user(val1
, _tlv
+ 2))
2283 if (put_user(val2
, _tlv
+ 3))
2287 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_tlv
);
2290 * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control
2291 * @codec: HD-audio codec
2292 * @nid: NID of a reference widget
2293 * @dir: #HDA_INPUT or #HDA_OUTPUT
2294 * @tlv: TLV data to be stored, at least 4 elements
2296 * Set (static) TLV data for a virtual master volume using the AMP caps
2297 * obtained from the reference NID.
2298 * The volume range is recalculated as if the max volume is 0dB.
2300 void snd_hda_set_vmaster_tlv(struct hda_codec
*codec
, hda_nid_t nid
, int dir
,
2306 caps
= query_amp_caps(codec
, nid
, dir
);
2307 nums
= (caps
& AC_AMPCAP_NUM_STEPS
) >> AC_AMPCAP_NUM_STEPS_SHIFT
;
2308 step
= (caps
& AC_AMPCAP_STEP_SIZE
) >> AC_AMPCAP_STEP_SIZE_SHIFT
;
2309 step
= (step
+ 1) * 25;
2310 tlv
[0] = SNDRV_CTL_TLVT_DB_SCALE
;
2311 tlv
[1] = 2 * sizeof(unsigned int);
2312 tlv
[2] = -nums
* step
;
2315 EXPORT_SYMBOL_HDA(snd_hda_set_vmaster_tlv
);
2317 /* find a mixer control element with the given name */
2318 static struct snd_kcontrol
*
2319 find_mixer_ctl(struct hda_codec
*codec
, const char *name
, int dev
, int idx
)
2321 struct snd_ctl_elem_id id
;
2322 memset(&id
, 0, sizeof(id
));
2323 id
.iface
= SNDRV_CTL_ELEM_IFACE_MIXER
;
2326 if (snd_BUG_ON(strlen(name
) >= sizeof(id
.name
)))
2328 strcpy(id
.name
, name
);
2329 return snd_ctl_find_id(codec
->bus
->card
, &id
);
2333 * snd_hda_find_mixer_ctl - Find a mixer control element with the given name
2334 * @codec: HD-audio codec
2335 * @name: ctl id name string
2337 * Get the control element with the given id string and IFACE_MIXER.
2339 struct snd_kcontrol
*snd_hda_find_mixer_ctl(struct hda_codec
*codec
,
2342 return find_mixer_ctl(codec
, name
, 0, 0);
2344 EXPORT_SYMBOL_HDA(snd_hda_find_mixer_ctl
);
2346 static int find_empty_mixer_ctl_idx(struct hda_codec
*codec
, const char *name
,
2350 /* 16 ctlrs should be large enough */
2351 for (i
= 0, idx
= start_idx
; i
< 16; i
++, idx
++) {
2352 if (!find_mixer_ctl(codec
, name
, 0, idx
))
2359 * snd_hda_ctl_add - Add a control element and assign to the codec
2360 * @codec: HD-audio codec
2361 * @nid: corresponding NID (optional)
2362 * @kctl: the control element to assign
2364 * Add the given control element to an array inside the codec instance.
2365 * All control elements belonging to a codec are supposed to be added
2366 * by this function so that a proper clean-up works at the free or
2367 * reconfiguration time.
2369 * If non-zero @nid is passed, the NID is assigned to the control element.
2370 * The assignment is shown in the codec proc file.
2372 * snd_hda_ctl_add() checks the control subdev id field whether
2373 * #HDA_SUBDEV_NID_FLAG bit is set. If set (and @nid is zero), the lower
2374 * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit
2375 * specifies if kctl->private_value is a HDA amplifier value.
2377 int snd_hda_ctl_add(struct hda_codec
*codec
, hda_nid_t nid
,
2378 struct snd_kcontrol
*kctl
)
2381 unsigned short flags
= 0;
2382 struct hda_nid_item
*item
;
2384 if (kctl
->id
.subdevice
& HDA_SUBDEV_AMP_FLAG
) {
2385 flags
|= HDA_NID_ITEM_AMP
;
2387 nid
= get_amp_nid_(kctl
->private_value
);
2389 if ((kctl
->id
.subdevice
& HDA_SUBDEV_NID_FLAG
) != 0 && nid
== 0)
2390 nid
= kctl
->id
.subdevice
& 0xffff;
2391 if (kctl
->id
.subdevice
& (HDA_SUBDEV_NID_FLAG
|HDA_SUBDEV_AMP_FLAG
))
2392 kctl
->id
.subdevice
= 0;
2393 err
= snd_ctl_add(codec
->bus
->card
, kctl
);
2396 item
= snd_array_new(&codec
->mixers
);
2401 item
->flags
= flags
;
2404 EXPORT_SYMBOL_HDA(snd_hda_ctl_add
);
2407 * snd_hda_add_nid - Assign a NID to a control element
2408 * @codec: HD-audio codec
2409 * @nid: corresponding NID (optional)
2410 * @kctl: the control element to assign
2411 * @index: index to kctl
2413 * Add the given control element to an array inside the codec instance.
2414 * This function is used when #snd_hda_ctl_add cannot be used for 1:1
2415 * NID:KCTL mapping - for example "Capture Source" selector.
2417 int snd_hda_add_nid(struct hda_codec
*codec
, struct snd_kcontrol
*kctl
,
2418 unsigned int index
, hda_nid_t nid
)
2420 struct hda_nid_item
*item
;
2423 item
= snd_array_new(&codec
->nids
);
2427 item
->index
= index
;
2431 printk(KERN_ERR
"hda-codec: no NID for mapping control %s:%d:%d\n",
2432 kctl
->id
.name
, kctl
->id
.index
, index
);
2435 EXPORT_SYMBOL_HDA(snd_hda_add_nid
);
2438 * snd_hda_ctls_clear - Clear all controls assigned to the given codec
2439 * @codec: HD-audio codec
2441 void snd_hda_ctls_clear(struct hda_codec
*codec
)
2444 struct hda_nid_item
*items
= codec
->mixers
.list
;
2445 for (i
= 0; i
< codec
->mixers
.used
; i
++)
2446 snd_ctl_remove(codec
->bus
->card
, items
[i
].kctl
);
2447 snd_array_free(&codec
->mixers
);
2448 snd_array_free(&codec
->nids
);
2451 /* pseudo device locking
2452 * toggle card->shutdown to allow/disallow the device access (as a hack)
2454 int snd_hda_lock_devices(struct hda_bus
*bus
)
2456 struct snd_card
*card
= bus
->card
;
2457 struct hda_codec
*codec
;
2459 spin_lock(&card
->files_lock
);
2463 if (!list_empty(&card
->ctl_files
))
2466 list_for_each_entry(codec
, &bus
->codec_list
, list
) {
2468 for (pcm
= 0; pcm
< codec
->num_pcms
; pcm
++) {
2469 struct hda_pcm
*cpcm
= &codec
->pcm_info
[pcm
];
2472 if (cpcm
->pcm
->streams
[0].substream_opened
||
2473 cpcm
->pcm
->streams
[1].substream_opened
)
2477 spin_unlock(&card
->files_lock
);
2483 spin_unlock(&card
->files_lock
);
2486 EXPORT_SYMBOL_HDA(snd_hda_lock_devices
);
2488 void snd_hda_unlock_devices(struct hda_bus
*bus
)
2490 struct snd_card
*card
= bus
->card
;
2493 spin_lock(&card
->files_lock
);
2495 spin_unlock(&card
->files_lock
);
2497 EXPORT_SYMBOL_HDA(snd_hda_unlock_devices
);
2500 * snd_hda_codec_reset - Clear all objects assigned to the codec
2501 * @codec: HD-audio codec
2503 * This frees the all PCM and control elements assigned to the codec, and
2504 * clears the caches and restores the pin default configurations.
2506 * When a device is being used, it returns -EBSY. If successfully freed,
2509 int snd_hda_codec_reset(struct hda_codec
*codec
)
2511 struct hda_bus
*bus
= codec
->bus
;
2512 struct snd_card
*card
= bus
->card
;
2515 if (snd_hda_lock_devices(bus
) < 0)
2518 /* OK, let it free */
2519 cancel_delayed_work_sync(&codec
->jackpoll_work
);
2521 cancel_delayed_work_sync(&codec
->power_work
);
2522 codec
->power_on
= 0;
2523 codec
->power_transition
= 0;
2524 codec
->power_jiffies
= jiffies
;
2525 flush_workqueue(bus
->workq
);
2527 snd_hda_ctls_clear(codec
);
2529 for (i
= 0; i
< codec
->num_pcms
; i
++) {
2530 if (codec
->pcm_info
[i
].pcm
) {
2531 snd_device_free(card
, codec
->pcm_info
[i
].pcm
);
2532 clear_bit(codec
->pcm_info
[i
].device
,
2536 if (codec
->patch_ops
.free
)
2537 codec
->patch_ops
.free(codec
);
2538 memset(&codec
->patch_ops
, 0, sizeof(codec
->patch_ops
));
2539 snd_hda_jack_tbl_clear(codec
);
2540 codec
->proc_widget_hook
= NULL
;
2542 free_hda_cache(&codec
->amp_cache
);
2543 free_hda_cache(&codec
->cmd_cache
);
2544 init_hda_cache(&codec
->amp_cache
, sizeof(struct hda_amp_info
));
2545 init_hda_cache(&codec
->cmd_cache
, sizeof(struct hda_cache_head
));
2546 /* free only driver_pins so that init_pins + user_pins are restored */
2547 snd_array_free(&codec
->driver_pins
);
2548 snd_array_free(&codec
->cvt_setups
);
2549 snd_array_free(&codec
->spdif_out
);
2550 snd_array_free(&codec
->verbs
);
2551 codec
->num_pcms
= 0;
2552 codec
->pcm_info
= NULL
;
2553 codec
->preset
= NULL
;
2554 codec
->slave_dig_outs
= NULL
;
2555 codec
->spdif_status_reset
= 0;
2556 module_put(codec
->owner
);
2557 codec
->owner
= NULL
;
2559 /* allow device access again */
2560 snd_hda_unlock_devices(bus
);
2564 typedef int (*map_slave_func_t
)(void *, struct snd_kcontrol
*);
2566 /* apply the function to all matching slave ctls in the mixer list */
2567 static int map_slaves(struct hda_codec
*codec
, const char * const *slaves
,
2568 const char *suffix
, map_slave_func_t func
, void *data
)
2570 struct hda_nid_item
*items
;
2571 const char * const *s
;
2574 items
= codec
->mixers
.list
;
2575 for (i
= 0; i
< codec
->mixers
.used
; i
++) {
2576 struct snd_kcontrol
*sctl
= items
[i
].kctl
;
2577 if (!sctl
|| !sctl
->id
.name
||
2578 sctl
->id
.iface
!= SNDRV_CTL_ELEM_IFACE_MIXER
)
2580 for (s
= slaves
; *s
; s
++) {
2581 char tmpname
[sizeof(sctl
->id
.name
)];
2582 const char *name
= *s
;
2584 snprintf(tmpname
, sizeof(tmpname
), "%s %s",
2588 if (!strcmp(sctl
->id
.name
, name
)) {
2589 err
= func(data
, sctl
);
2599 static int check_slave_present(void *data
, struct snd_kcontrol
*sctl
)
2604 /* guess the value corresponding to 0dB */
2605 static int get_kctl_0dB_offset(struct snd_kcontrol
*kctl
)
2608 const int *tlv
= NULL
;
2611 if (kctl
->vd
[0].access
& SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK
) {
2612 /* FIXME: set_fs() hack for obtaining user-space TLV data */
2613 mm_segment_t fs
= get_fs();
2615 if (!kctl
->tlv
.c(kctl
, 0, sizeof(_tlv
), _tlv
))
2618 } else if (kctl
->vd
[0].access
& SNDRV_CTL_ELEM_ACCESS_TLV_READ
)
2620 if (tlv
&& tlv
[0] == SNDRV_CTL_TLVT_DB_SCALE
)
2621 val
= -tlv
[2] / tlv
[3];
2625 /* call kctl->put with the given value(s) */
2626 static int put_kctl_with_value(struct snd_kcontrol
*kctl
, int val
)
2628 struct snd_ctl_elem_value
*ucontrol
;
2629 ucontrol
= kzalloc(sizeof(*ucontrol
), GFP_KERNEL
);
2632 ucontrol
->value
.integer
.value
[0] = val
;
2633 ucontrol
->value
.integer
.value
[1] = val
;
2634 kctl
->put(kctl
, ucontrol
);
2639 /* initialize the slave volume with 0dB */
2640 static int init_slave_0dB(void *data
, struct snd_kcontrol
*slave
)
2642 int offset
= get_kctl_0dB_offset(slave
);
2644 put_kctl_with_value(slave
, offset
);
2648 /* unmute the slave */
2649 static int init_slave_unmute(void *data
, struct snd_kcontrol
*slave
)
2651 return put_kctl_with_value(slave
, 1);
2655 * snd_hda_add_vmaster - create a virtual master control and add slaves
2656 * @codec: HD-audio codec
2657 * @name: vmaster control name
2658 * @tlv: TLV data (optional)
2659 * @slaves: slave control names (optional)
2660 * @suffix: suffix string to each slave name (optional)
2661 * @init_slave_vol: initialize slaves to unmute/0dB
2662 * @ctl_ret: store the vmaster kcontrol in return
2664 * Create a virtual master control with the given name. The TLV data
2665 * must be either NULL or a valid data.
2667 * @slaves is a NULL-terminated array of strings, each of which is a
2668 * slave control name. All controls with these names are assigned to
2669 * the new virtual master control.
2671 * This function returns zero if successful or a negative error code.
2673 int __snd_hda_add_vmaster(struct hda_codec
*codec
, char *name
,
2674 unsigned int *tlv
, const char * const *slaves
,
2675 const char *suffix
, bool init_slave_vol
,
2676 struct snd_kcontrol
**ctl_ret
)
2678 struct snd_kcontrol
*kctl
;
2684 err
= map_slaves(codec
, slaves
, suffix
, check_slave_present
, NULL
);
2686 snd_printdd("No slave found for %s\n", name
);
2689 kctl
= snd_ctl_make_virtual_master(name
, tlv
);
2692 err
= snd_hda_ctl_add(codec
, 0, kctl
);
2696 err
= map_slaves(codec
, slaves
, suffix
,
2697 (map_slave_func_t
)snd_ctl_add_slave
, kctl
);
2701 /* init with master mute & zero volume */
2702 put_kctl_with_value(kctl
, 0);
2704 map_slaves(codec
, slaves
, suffix
,
2705 tlv
? init_slave_0dB
: init_slave_unmute
, kctl
);
2711 EXPORT_SYMBOL_HDA(__snd_hda_add_vmaster
);
2714 * mute-LED control using vmaster
2716 static int vmaster_mute_mode_info(struct snd_kcontrol
*kcontrol
,
2717 struct snd_ctl_elem_info
*uinfo
)
2719 static const char * const texts
[] = {
2720 "On", "Off", "Follow Master"
2724 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_ENUMERATED
;
2726 uinfo
->value
.enumerated
.items
= 3;
2727 index
= uinfo
->value
.enumerated
.item
;
2730 strcpy(uinfo
->value
.enumerated
.name
, texts
[index
]);
2734 static int vmaster_mute_mode_get(struct snd_kcontrol
*kcontrol
,
2735 struct snd_ctl_elem_value
*ucontrol
)
2737 struct hda_vmaster_mute_hook
*hook
= snd_kcontrol_chip(kcontrol
);
2738 ucontrol
->value
.enumerated
.item
[0] = hook
->mute_mode
;
2742 static int vmaster_mute_mode_put(struct snd_kcontrol
*kcontrol
,
2743 struct snd_ctl_elem_value
*ucontrol
)
2745 struct hda_vmaster_mute_hook
*hook
= snd_kcontrol_chip(kcontrol
);
2746 unsigned int old_mode
= hook
->mute_mode
;
2748 hook
->mute_mode
= ucontrol
->value
.enumerated
.item
[0];
2749 if (hook
->mute_mode
> HDA_VMUTE_FOLLOW_MASTER
)
2750 hook
->mute_mode
= HDA_VMUTE_FOLLOW_MASTER
;
2751 if (old_mode
== hook
->mute_mode
)
2753 snd_hda_sync_vmaster_hook(hook
);
2757 static struct snd_kcontrol_new vmaster_mute_mode
= {
2758 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
2759 .name
= "Mute-LED Mode",
2760 .info
= vmaster_mute_mode_info
,
2761 .get
= vmaster_mute_mode_get
,
2762 .put
= vmaster_mute_mode_put
,
2766 * Add a mute-LED hook with the given vmaster switch kctl
2767 * "Mute-LED Mode" control is automatically created and associated with
2770 int snd_hda_add_vmaster_hook(struct hda_codec
*codec
,
2771 struct hda_vmaster_mute_hook
*hook
,
2772 bool expose_enum_ctl
)
2774 struct snd_kcontrol
*kctl
;
2776 if (!hook
->hook
|| !hook
->sw_kctl
)
2778 snd_ctl_add_vmaster_hook(hook
->sw_kctl
, hook
->hook
, codec
);
2779 hook
->codec
= codec
;
2780 hook
->mute_mode
= HDA_VMUTE_FOLLOW_MASTER
;
2781 if (!expose_enum_ctl
)
2783 kctl
= snd_ctl_new1(&vmaster_mute_mode
, hook
);
2786 return snd_hda_ctl_add(codec
, 0, kctl
);
2788 EXPORT_SYMBOL_HDA(snd_hda_add_vmaster_hook
);
2791 * Call the hook with the current value for synchronization
2792 * Should be called in init callback
2794 void snd_hda_sync_vmaster_hook(struct hda_vmaster_mute_hook
*hook
)
2796 if (!hook
->hook
|| !hook
->codec
)
2798 /* don't call vmaster hook in the destructor since it might have
2799 * been already destroyed
2801 if (hook
->codec
->bus
->shutdown
)
2803 switch (hook
->mute_mode
) {
2804 case HDA_VMUTE_FOLLOW_MASTER
:
2805 snd_ctl_sync_vmaster_hook(hook
->sw_kctl
);
2808 hook
->hook(hook
->codec
, hook
->mute_mode
);
2812 EXPORT_SYMBOL_HDA(snd_hda_sync_vmaster_hook
);
2816 * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch
2818 * The control element is supposed to have the private_value field
2819 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2821 int snd_hda_mixer_amp_switch_info(struct snd_kcontrol
*kcontrol
,
2822 struct snd_ctl_elem_info
*uinfo
)
2824 int chs
= get_amp_channels(kcontrol
);
2826 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_BOOLEAN
;
2827 uinfo
->count
= chs
== 3 ? 2 : 1;
2828 uinfo
->value
.integer
.min
= 0;
2829 uinfo
->value
.integer
.max
= 1;
2832 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_info
);
2835 * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch
2837 * The control element is supposed to have the private_value field
2838 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2840 int snd_hda_mixer_amp_switch_get(struct snd_kcontrol
*kcontrol
,
2841 struct snd_ctl_elem_value
*ucontrol
)
2843 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2844 hda_nid_t nid
= get_amp_nid(kcontrol
);
2845 int chs
= get_amp_channels(kcontrol
);
2846 int dir
= get_amp_direction(kcontrol
);
2847 int idx
= get_amp_index(kcontrol
);
2848 long *valp
= ucontrol
->value
.integer
.value
;
2851 *valp
++ = (snd_hda_codec_amp_read(codec
, nid
, 0, dir
, idx
) &
2852 HDA_AMP_MUTE
) ? 0 : 1;
2854 *valp
= (snd_hda_codec_amp_read(codec
, nid
, 1, dir
, idx
) &
2855 HDA_AMP_MUTE
) ? 0 : 1;
2858 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_get
);
2861 * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch
2863 * The control element is supposed to have the private_value field
2864 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2866 int snd_hda_mixer_amp_switch_put(struct snd_kcontrol
*kcontrol
,
2867 struct snd_ctl_elem_value
*ucontrol
)
2869 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2870 hda_nid_t nid
= get_amp_nid(kcontrol
);
2871 int chs
= get_amp_channels(kcontrol
);
2872 int dir
= get_amp_direction(kcontrol
);
2873 int idx
= get_amp_index(kcontrol
);
2874 long *valp
= ucontrol
->value
.integer
.value
;
2877 snd_hda_power_up(codec
);
2879 change
= snd_hda_codec_amp_update(codec
, nid
, 0, dir
, idx
,
2881 *valp
? 0 : HDA_AMP_MUTE
);
2885 change
|= snd_hda_codec_amp_update(codec
, nid
, 1, dir
, idx
,
2887 *valp
? 0 : HDA_AMP_MUTE
);
2888 hda_call_check_power_status(codec
, nid
);
2889 snd_hda_power_down(codec
);
2892 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put
);
2895 * bound volume controls
2897 * bind multiple volumes (# indices, from 0)
2900 #define AMP_VAL_IDX_SHIFT 19
2901 #define AMP_VAL_IDX_MASK (0x0f<<19)
2904 * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control
2906 * The control element is supposed to have the private_value field
2907 * set up via HDA_BIND_MUTE*() macros.
2909 int snd_hda_mixer_bind_switch_get(struct snd_kcontrol
*kcontrol
,
2910 struct snd_ctl_elem_value
*ucontrol
)
2912 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2916 mutex_lock(&codec
->control_mutex
);
2917 pval
= kcontrol
->private_value
;
2918 kcontrol
->private_value
= pval
& ~AMP_VAL_IDX_MASK
; /* index 0 */
2919 err
= snd_hda_mixer_amp_switch_get(kcontrol
, ucontrol
);
2920 kcontrol
->private_value
= pval
;
2921 mutex_unlock(&codec
->control_mutex
);
2924 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_get
);
2927 * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control
2929 * The control element is supposed to have the private_value field
2930 * set up via HDA_BIND_MUTE*() macros.
2932 int snd_hda_mixer_bind_switch_put(struct snd_kcontrol
*kcontrol
,
2933 struct snd_ctl_elem_value
*ucontrol
)
2935 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2937 int i
, indices
, err
= 0, change
= 0;
2939 mutex_lock(&codec
->control_mutex
);
2940 pval
= kcontrol
->private_value
;
2941 indices
= (pval
& AMP_VAL_IDX_MASK
) >> AMP_VAL_IDX_SHIFT
;
2942 for (i
= 0; i
< indices
; i
++) {
2943 kcontrol
->private_value
= (pval
& ~AMP_VAL_IDX_MASK
) |
2944 (i
<< AMP_VAL_IDX_SHIFT
);
2945 err
= snd_hda_mixer_amp_switch_put(kcontrol
, ucontrol
);
2950 kcontrol
->private_value
= pval
;
2951 mutex_unlock(&codec
->control_mutex
);
2952 return err
< 0 ? err
: change
;
2954 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_put
);
2957 * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control
2959 * The control element is supposed to have the private_value field
2960 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2962 int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol
*kcontrol
,
2963 struct snd_ctl_elem_info
*uinfo
)
2965 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2966 struct hda_bind_ctls
*c
;
2969 mutex_lock(&codec
->control_mutex
);
2970 c
= (struct hda_bind_ctls
*)kcontrol
->private_value
;
2971 kcontrol
->private_value
= *c
->values
;
2972 err
= c
->ops
->info(kcontrol
, uinfo
);
2973 kcontrol
->private_value
= (long)c
;
2974 mutex_unlock(&codec
->control_mutex
);
2977 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_info
);
2980 * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control
2982 * The control element is supposed to have the private_value field
2983 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2985 int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol
*kcontrol
,
2986 struct snd_ctl_elem_value
*ucontrol
)
2988 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2989 struct hda_bind_ctls
*c
;
2992 mutex_lock(&codec
->control_mutex
);
2993 c
= (struct hda_bind_ctls
*)kcontrol
->private_value
;
2994 kcontrol
->private_value
= *c
->values
;
2995 err
= c
->ops
->get(kcontrol
, ucontrol
);
2996 kcontrol
->private_value
= (long)c
;
2997 mutex_unlock(&codec
->control_mutex
);
3000 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_get
);
3003 * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control
3005 * The control element is supposed to have the private_value field
3006 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
3008 int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol
*kcontrol
,
3009 struct snd_ctl_elem_value
*ucontrol
)
3011 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
3012 struct hda_bind_ctls
*c
;
3013 unsigned long *vals
;
3014 int err
= 0, change
= 0;
3016 mutex_lock(&codec
->control_mutex
);
3017 c
= (struct hda_bind_ctls
*)kcontrol
->private_value
;
3018 for (vals
= c
->values
; *vals
; vals
++) {
3019 kcontrol
->private_value
= *vals
;
3020 err
= c
->ops
->put(kcontrol
, ucontrol
);
3025 kcontrol
->private_value
= (long)c
;
3026 mutex_unlock(&codec
->control_mutex
);
3027 return err
< 0 ? err
: change
;
3029 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_put
);
3032 * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control
3034 * The control element is supposed to have the private_value field
3035 * set up via HDA_BIND_VOL() macro.
3037 int snd_hda_mixer_bind_tlv(struct snd_kcontrol
*kcontrol
, int op_flag
,
3038 unsigned int size
, unsigned int __user
*tlv
)
3040 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
3041 struct hda_bind_ctls
*c
;
3044 mutex_lock(&codec
->control_mutex
);
3045 c
= (struct hda_bind_ctls
*)kcontrol
->private_value
;
3046 kcontrol
->private_value
= *c
->values
;
3047 err
= c
->ops
->tlv(kcontrol
, op_flag
, size
, tlv
);
3048 kcontrol
->private_value
= (long)c
;
3049 mutex_unlock(&codec
->control_mutex
);
3052 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_tlv
);
3054 struct hda_ctl_ops snd_hda_bind_vol
= {
3055 .info
= snd_hda_mixer_amp_volume_info
,
3056 .get
= snd_hda_mixer_amp_volume_get
,
3057 .put
= snd_hda_mixer_amp_volume_put
,
3058 .tlv
= snd_hda_mixer_amp_tlv
3060 EXPORT_SYMBOL_HDA(snd_hda_bind_vol
);
3062 struct hda_ctl_ops snd_hda_bind_sw
= {
3063 .info
= snd_hda_mixer_amp_switch_info
,
3064 .get
= snd_hda_mixer_amp_switch_get
,
3065 .put
= snd_hda_mixer_amp_switch_put
,
3066 .tlv
= snd_hda_mixer_amp_tlv
3068 EXPORT_SYMBOL_HDA(snd_hda_bind_sw
);
3071 * SPDIF out controls
3074 static int snd_hda_spdif_mask_info(struct snd_kcontrol
*kcontrol
,
3075 struct snd_ctl_elem_info
*uinfo
)
3077 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_IEC958
;
3082 static int snd_hda_spdif_cmask_get(struct snd_kcontrol
*kcontrol
,
3083 struct snd_ctl_elem_value
*ucontrol
)
3085 ucontrol
->value
.iec958
.status
[0] = IEC958_AES0_PROFESSIONAL
|
3086 IEC958_AES0_NONAUDIO
|
3087 IEC958_AES0_CON_EMPHASIS_5015
|
3088 IEC958_AES0_CON_NOT_COPYRIGHT
;
3089 ucontrol
->value
.iec958
.status
[1] = IEC958_AES1_CON_CATEGORY
|
3090 IEC958_AES1_CON_ORIGINAL
;
3094 static int snd_hda_spdif_pmask_get(struct snd_kcontrol
*kcontrol
,
3095 struct snd_ctl_elem_value
*ucontrol
)
3097 ucontrol
->value
.iec958
.status
[0] = IEC958_AES0_PROFESSIONAL
|
3098 IEC958_AES0_NONAUDIO
|
3099 IEC958_AES0_PRO_EMPHASIS_5015
;
3103 static int snd_hda_spdif_default_get(struct snd_kcontrol
*kcontrol
,
3104 struct snd_ctl_elem_value
*ucontrol
)
3106 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
3107 int idx
= kcontrol
->private_value
;
3108 struct hda_spdif_out
*spdif
;
3110 mutex_lock(&codec
->spdif_mutex
);
3111 spdif
= snd_array_elem(&codec
->spdif_out
, idx
);
3112 ucontrol
->value
.iec958
.status
[0] = spdif
->status
& 0xff;
3113 ucontrol
->value
.iec958
.status
[1] = (spdif
->status
>> 8) & 0xff;
3114 ucontrol
->value
.iec958
.status
[2] = (spdif
->status
>> 16) & 0xff;
3115 ucontrol
->value
.iec958
.status
[3] = (spdif
->status
>> 24) & 0xff;
3116 mutex_unlock(&codec
->spdif_mutex
);
3121 /* convert from SPDIF status bits to HDA SPDIF bits
3122 * bit 0 (DigEn) is always set zero (to be filled later)
3124 static unsigned short convert_from_spdif_status(unsigned int sbits
)
3126 unsigned short val
= 0;
3128 if (sbits
& IEC958_AES0_PROFESSIONAL
)
3129 val
|= AC_DIG1_PROFESSIONAL
;
3130 if (sbits
& IEC958_AES0_NONAUDIO
)
3131 val
|= AC_DIG1_NONAUDIO
;
3132 if (sbits
& IEC958_AES0_PROFESSIONAL
) {
3133 if ((sbits
& IEC958_AES0_PRO_EMPHASIS
) ==
3134 IEC958_AES0_PRO_EMPHASIS_5015
)
3135 val
|= AC_DIG1_EMPHASIS
;
3137 if ((sbits
& IEC958_AES0_CON_EMPHASIS
) ==
3138 IEC958_AES0_CON_EMPHASIS_5015
)
3139 val
|= AC_DIG1_EMPHASIS
;
3140 if (!(sbits
& IEC958_AES0_CON_NOT_COPYRIGHT
))
3141 val
|= AC_DIG1_COPYRIGHT
;
3142 if (sbits
& (IEC958_AES1_CON_ORIGINAL
<< 8))
3143 val
|= AC_DIG1_LEVEL
;
3144 val
|= sbits
& (IEC958_AES1_CON_CATEGORY
<< 8);
3149 /* convert to SPDIF status bits from HDA SPDIF bits
3151 static unsigned int convert_to_spdif_status(unsigned short val
)
3153 unsigned int sbits
= 0;
3155 if (val
& AC_DIG1_NONAUDIO
)
3156 sbits
|= IEC958_AES0_NONAUDIO
;
3157 if (val
& AC_DIG1_PROFESSIONAL
)
3158 sbits
|= IEC958_AES0_PROFESSIONAL
;
3159 if (sbits
& IEC958_AES0_PROFESSIONAL
) {
3160 if (val
& AC_DIG1_EMPHASIS
)
3161 sbits
|= IEC958_AES0_PRO_EMPHASIS_5015
;
3163 if (val
& AC_DIG1_EMPHASIS
)
3164 sbits
|= IEC958_AES0_CON_EMPHASIS_5015
;
3165 if (!(val
& AC_DIG1_COPYRIGHT
))
3166 sbits
|= IEC958_AES0_CON_NOT_COPYRIGHT
;
3167 if (val
& AC_DIG1_LEVEL
)
3168 sbits
|= (IEC958_AES1_CON_ORIGINAL
<< 8);
3169 sbits
|= val
& (0x7f << 8);
3174 /* set digital convert verbs both for the given NID and its slaves */
3175 static void set_dig_out(struct hda_codec
*codec
, hda_nid_t nid
,
3180 snd_hda_codec_write_cache(codec
, nid
, 0, verb
, val
);
3181 d
= codec
->slave_dig_outs
;
3185 snd_hda_codec_write_cache(codec
, *d
, 0, verb
, val
);
3188 static inline void set_dig_out_convert(struct hda_codec
*codec
, hda_nid_t nid
,
3192 set_dig_out(codec
, nid
, AC_VERB_SET_DIGI_CONVERT_1
, dig1
);
3194 set_dig_out(codec
, nid
, AC_VERB_SET_DIGI_CONVERT_2
, dig2
);
3197 static int snd_hda_spdif_default_put(struct snd_kcontrol
*kcontrol
,
3198 struct snd_ctl_elem_value
*ucontrol
)
3200 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
3201 int idx
= kcontrol
->private_value
;
3202 struct hda_spdif_out
*spdif
;
3207 mutex_lock(&codec
->spdif_mutex
);
3208 spdif
= snd_array_elem(&codec
->spdif_out
, idx
);
3210 spdif
->status
= ucontrol
->value
.iec958
.status
[0] |
3211 ((unsigned int)ucontrol
->value
.iec958
.status
[1] << 8) |
3212 ((unsigned int)ucontrol
->value
.iec958
.status
[2] << 16) |
3213 ((unsigned int)ucontrol
->value
.iec958
.status
[3] << 24);
3214 val
= convert_from_spdif_status(spdif
->status
);
3215 val
|= spdif
->ctls
& 1;
3216 change
= spdif
->ctls
!= val
;
3218 if (change
&& nid
!= (u16
)-1)
3219 set_dig_out_convert(codec
, nid
, val
& 0xff, (val
>> 8) & 0xff);
3220 mutex_unlock(&codec
->spdif_mutex
);
3224 #define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
3226 static int snd_hda_spdif_out_switch_get(struct snd_kcontrol
*kcontrol
,
3227 struct snd_ctl_elem_value
*ucontrol
)
3229 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
3230 int idx
= kcontrol
->private_value
;
3231 struct hda_spdif_out
*spdif
;
3233 mutex_lock(&codec
->spdif_mutex
);
3234 spdif
= snd_array_elem(&codec
->spdif_out
, idx
);
3235 ucontrol
->value
.integer
.value
[0] = spdif
->ctls
& AC_DIG1_ENABLE
;
3236 mutex_unlock(&codec
->spdif_mutex
);
3240 static inline void set_spdif_ctls(struct hda_codec
*codec
, hda_nid_t nid
,
3243 set_dig_out_convert(codec
, nid
, dig1
, dig2
);
3244 /* unmute amp switch (if any) */
3245 if ((get_wcaps(codec
, nid
) & AC_WCAP_OUT_AMP
) &&
3246 (dig1
& AC_DIG1_ENABLE
))
3247 snd_hda_codec_amp_stereo(codec
, nid
, HDA_OUTPUT
, 0,
3251 static int snd_hda_spdif_out_switch_put(struct snd_kcontrol
*kcontrol
,
3252 struct snd_ctl_elem_value
*ucontrol
)
3254 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
3255 int idx
= kcontrol
->private_value
;
3256 struct hda_spdif_out
*spdif
;
3261 mutex_lock(&codec
->spdif_mutex
);
3262 spdif
= snd_array_elem(&codec
->spdif_out
, idx
);
3264 val
= spdif
->ctls
& ~AC_DIG1_ENABLE
;
3265 if (ucontrol
->value
.integer
.value
[0])
3266 val
|= AC_DIG1_ENABLE
;
3267 change
= spdif
->ctls
!= val
;
3269 if (change
&& nid
!= (u16
)-1)
3270 set_spdif_ctls(codec
, nid
, val
& 0xff, -1);
3271 mutex_unlock(&codec
->spdif_mutex
);
3275 static struct snd_kcontrol_new dig_mixes
[] = {
3277 .access
= SNDRV_CTL_ELEM_ACCESS_READ
,
3278 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
3279 .name
= SNDRV_CTL_NAME_IEC958("", PLAYBACK
, CON_MASK
),
3280 .info
= snd_hda_spdif_mask_info
,
3281 .get
= snd_hda_spdif_cmask_get
,
3284 .access
= SNDRV_CTL_ELEM_ACCESS_READ
,
3285 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
3286 .name
= SNDRV_CTL_NAME_IEC958("", PLAYBACK
, PRO_MASK
),
3287 .info
= snd_hda_spdif_mask_info
,
3288 .get
= snd_hda_spdif_pmask_get
,
3291 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
3292 .name
= SNDRV_CTL_NAME_IEC958("", PLAYBACK
, DEFAULT
),
3293 .info
= snd_hda_spdif_mask_info
,
3294 .get
= snd_hda_spdif_default_get
,
3295 .put
= snd_hda_spdif_default_put
,
3298 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
3299 .name
= SNDRV_CTL_NAME_IEC958("", PLAYBACK
, SWITCH
),
3300 .info
= snd_hda_spdif_out_switch_info
,
3301 .get
= snd_hda_spdif_out_switch_get
,
3302 .put
= snd_hda_spdif_out_switch_put
,
3308 * snd_hda_create_dig_out_ctls - create Output SPDIF-related controls
3309 * @codec: the HDA codec
3310 * @associated_nid: NID that new ctls associated with
3311 * @cvt_nid: converter NID
3312 * @type: HDA_PCM_TYPE_*
3313 * Creates controls related with the digital output.
3314 * Called from each patch supporting the digital out.
3316 * Returns 0 if successful, or a negative error code.
3318 int snd_hda_create_dig_out_ctls(struct hda_codec
*codec
,
3319 hda_nid_t associated_nid
,
3324 struct snd_kcontrol
*kctl
;
3325 struct snd_kcontrol_new
*dig_mix
;
3327 const int spdif_index
= 16;
3328 struct hda_spdif_out
*spdif
;
3329 struct hda_bus
*bus
= codec
->bus
;
3331 if (bus
->primary_dig_out_type
== HDA_PCM_TYPE_HDMI
&&
3332 type
== HDA_PCM_TYPE_SPDIF
) {
3334 } else if (bus
->primary_dig_out_type
== HDA_PCM_TYPE_SPDIF
&&
3335 type
== HDA_PCM_TYPE_HDMI
) {
3336 /* suppose a single SPDIF device */
3337 for (dig_mix
= dig_mixes
; dig_mix
->name
; dig_mix
++) {
3338 kctl
= find_mixer_ctl(codec
, dig_mix
->name
, 0, 0);
3341 kctl
->id
.index
= spdif_index
;
3343 bus
->primary_dig_out_type
= HDA_PCM_TYPE_HDMI
;
3345 if (!bus
->primary_dig_out_type
)
3346 bus
->primary_dig_out_type
= type
;
3348 idx
= find_empty_mixer_ctl_idx(codec
, "IEC958 Playback Switch", idx
);
3350 printk(KERN_ERR
"hda_codec: too many IEC958 outputs\n");
3353 spdif
= snd_array_new(&codec
->spdif_out
);
3356 for (dig_mix
= dig_mixes
; dig_mix
->name
; dig_mix
++) {
3357 kctl
= snd_ctl_new1(dig_mix
, codec
);
3360 kctl
->id
.index
= idx
;
3361 kctl
->private_value
= codec
->spdif_out
.used
- 1;
3362 err
= snd_hda_ctl_add(codec
, associated_nid
, kctl
);
3366 spdif
->nid
= cvt_nid
;
3367 spdif
->ctls
= snd_hda_codec_read(codec
, cvt_nid
, 0,
3368 AC_VERB_GET_DIGI_CONVERT_1
, 0);
3369 spdif
->status
= convert_to_spdif_status(spdif
->ctls
);
3372 EXPORT_SYMBOL_HDA(snd_hda_create_dig_out_ctls
);
3374 /* get the hda_spdif_out entry from the given NID
3375 * call within spdif_mutex lock
3377 struct hda_spdif_out
*snd_hda_spdif_out_of_nid(struct hda_codec
*codec
,
3381 for (i
= 0; i
< codec
->spdif_out
.used
; i
++) {
3382 struct hda_spdif_out
*spdif
=
3383 snd_array_elem(&codec
->spdif_out
, i
);
3384 if (spdif
->nid
== nid
)
3389 EXPORT_SYMBOL_HDA(snd_hda_spdif_out_of_nid
);
3391 void snd_hda_spdif_ctls_unassign(struct hda_codec
*codec
, int idx
)
3393 struct hda_spdif_out
*spdif
;
3395 mutex_lock(&codec
->spdif_mutex
);
3396 spdif
= snd_array_elem(&codec
->spdif_out
, idx
);
3397 spdif
->nid
= (u16
)-1;
3398 mutex_unlock(&codec
->spdif_mutex
);
3400 EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_unassign
);
3402 void snd_hda_spdif_ctls_assign(struct hda_codec
*codec
, int idx
, hda_nid_t nid
)
3404 struct hda_spdif_out
*spdif
;
3407 mutex_lock(&codec
->spdif_mutex
);
3408 spdif
= snd_array_elem(&codec
->spdif_out
, idx
);
3409 if (spdif
->nid
!= nid
) {
3412 set_spdif_ctls(codec
, nid
, val
& 0xff, (val
>> 8) & 0xff);
3414 mutex_unlock(&codec
->spdif_mutex
);
3416 EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_assign
);
3419 * SPDIF sharing with analog output
3421 static int spdif_share_sw_get(struct snd_kcontrol
*kcontrol
,
3422 struct snd_ctl_elem_value
*ucontrol
)
3424 struct hda_multi_out
*mout
= snd_kcontrol_chip(kcontrol
);
3425 ucontrol
->value
.integer
.value
[0] = mout
->share_spdif
;
3429 static int spdif_share_sw_put(struct snd_kcontrol
*kcontrol
,
3430 struct snd_ctl_elem_value
*ucontrol
)
3432 struct hda_multi_out
*mout
= snd_kcontrol_chip(kcontrol
);
3433 mout
->share_spdif
= !!ucontrol
->value
.integer
.value
[0];
3437 static struct snd_kcontrol_new spdif_share_sw
= {
3438 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
3439 .name
= "IEC958 Default PCM Playback Switch",
3440 .info
= snd_ctl_boolean_mono_info
,
3441 .get
= spdif_share_sw_get
,
3442 .put
= spdif_share_sw_put
,
3446 * snd_hda_create_spdif_share_sw - create Default PCM switch
3447 * @codec: the HDA codec
3448 * @mout: multi-out instance
3450 int snd_hda_create_spdif_share_sw(struct hda_codec
*codec
,
3451 struct hda_multi_out
*mout
)
3453 struct snd_kcontrol
*kctl
;
3455 if (!mout
->dig_out_nid
)
3458 kctl
= snd_ctl_new1(&spdif_share_sw
, mout
);
3461 /* ATTENTION: here mout is passed as private_data, instead of codec */
3462 return snd_hda_ctl_add(codec
, mout
->dig_out_nid
, kctl
);
3464 EXPORT_SYMBOL_HDA(snd_hda_create_spdif_share_sw
);
3470 #define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
3472 static int snd_hda_spdif_in_switch_get(struct snd_kcontrol
*kcontrol
,
3473 struct snd_ctl_elem_value
*ucontrol
)
3475 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
3477 ucontrol
->value
.integer
.value
[0] = codec
->spdif_in_enable
;
3481 static int snd_hda_spdif_in_switch_put(struct snd_kcontrol
*kcontrol
,
3482 struct snd_ctl_elem_value
*ucontrol
)
3484 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
3485 hda_nid_t nid
= kcontrol
->private_value
;
3486 unsigned int val
= !!ucontrol
->value
.integer
.value
[0];
3489 mutex_lock(&codec
->spdif_mutex
);
3490 change
= codec
->spdif_in_enable
!= val
;
3492 codec
->spdif_in_enable
= val
;
3493 snd_hda_codec_write_cache(codec
, nid
, 0,
3494 AC_VERB_SET_DIGI_CONVERT_1
, val
);
3496 mutex_unlock(&codec
->spdif_mutex
);
3500 static int snd_hda_spdif_in_status_get(struct snd_kcontrol
*kcontrol
,
3501 struct snd_ctl_elem_value
*ucontrol
)
3503 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
3504 hda_nid_t nid
= kcontrol
->private_value
;
3508 val
= snd_hda_codec_read(codec
, nid
, 0, AC_VERB_GET_DIGI_CONVERT_1
, 0);
3509 sbits
= convert_to_spdif_status(val
);
3510 ucontrol
->value
.iec958
.status
[0] = sbits
;
3511 ucontrol
->value
.iec958
.status
[1] = sbits
>> 8;
3512 ucontrol
->value
.iec958
.status
[2] = sbits
>> 16;
3513 ucontrol
->value
.iec958
.status
[3] = sbits
>> 24;
3517 static struct snd_kcontrol_new dig_in_ctls
[] = {
3519 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
3520 .name
= SNDRV_CTL_NAME_IEC958("", CAPTURE
, SWITCH
),
3521 .info
= snd_hda_spdif_in_switch_info
,
3522 .get
= snd_hda_spdif_in_switch_get
,
3523 .put
= snd_hda_spdif_in_switch_put
,
3526 .access
= SNDRV_CTL_ELEM_ACCESS_READ
,
3527 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
3528 .name
= SNDRV_CTL_NAME_IEC958("", CAPTURE
, DEFAULT
),
3529 .info
= snd_hda_spdif_mask_info
,
3530 .get
= snd_hda_spdif_in_status_get
,
3536 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
3537 * @codec: the HDA codec
3538 * @nid: audio in widget NID
3540 * Creates controls related with the SPDIF input.
3541 * Called from each patch supporting the SPDIF in.
3543 * Returns 0 if successful, or a negative error code.
3545 int snd_hda_create_spdif_in_ctls(struct hda_codec
*codec
, hda_nid_t nid
)
3548 struct snd_kcontrol
*kctl
;
3549 struct snd_kcontrol_new
*dig_mix
;
3552 idx
= find_empty_mixer_ctl_idx(codec
, "IEC958 Capture Switch", 0);
3554 printk(KERN_ERR
"hda_codec: too many IEC958 inputs\n");
3557 for (dig_mix
= dig_in_ctls
; dig_mix
->name
; dig_mix
++) {
3558 kctl
= snd_ctl_new1(dig_mix
, codec
);
3561 kctl
->private_value
= nid
;
3562 err
= snd_hda_ctl_add(codec
, nid
, kctl
);
3566 codec
->spdif_in_enable
=
3567 snd_hda_codec_read(codec
, nid
, 0,
3568 AC_VERB_GET_DIGI_CONVERT_1
, 0) &
3572 EXPORT_SYMBOL_HDA(snd_hda_create_spdif_in_ctls
);
3578 /* build a 31bit cache key with the widget id and the command parameter */
3579 #define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
3580 #define get_cmd_cache_nid(key) ((key) & 0xff)
3581 #define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
3584 * snd_hda_codec_write_cache - send a single command with caching
3585 * @codec: the HDA codec
3586 * @nid: NID to send the command
3587 * @flags: optional bit flags
3588 * @verb: the verb to send
3589 * @parm: the parameter for the verb
3591 * Send a single command without waiting for response.
3593 * Returns 0 if successful, or a negative error code.
3595 int snd_hda_codec_write_cache(struct hda_codec
*codec
, hda_nid_t nid
,
3596 int flags
, unsigned int verb
, unsigned int parm
)
3599 struct hda_cache_head
*c
;
3601 unsigned int cache_only
;
3603 cache_only
= codec
->cached_write
;
3605 err
= snd_hda_codec_write(codec
, nid
, flags
, verb
, parm
);
3610 /* parm may contain the verb stuff for get/set amp */
3611 verb
= verb
| (parm
>> 8);
3613 key
= build_cmd_cache_key(nid
, verb
);
3614 mutex_lock(&codec
->bus
->cmd_mutex
);
3615 c
= get_alloc_hash(&codec
->cmd_cache
, key
);
3618 c
->dirty
= cache_only
;
3620 mutex_unlock(&codec
->bus
->cmd_mutex
);
3623 EXPORT_SYMBOL_HDA(snd_hda_codec_write_cache
);
3626 * snd_hda_codec_update_cache - check cache and write the cmd only when needed
3627 * @codec: the HDA codec
3628 * @nid: NID to send the command
3629 * @flags: optional bit flags
3630 * @verb: the verb to send
3631 * @parm: the parameter for the verb
3633 * This function works like snd_hda_codec_write_cache(), but it doesn't send
3634 * command if the parameter is already identical with the cached value.
3635 * If not, it sends the command and refreshes the cache.
3637 * Returns 0 if successful, or a negative error code.
3639 int snd_hda_codec_update_cache(struct hda_codec
*codec
, hda_nid_t nid
,
3640 int flags
, unsigned int verb
, unsigned int parm
)
3642 struct hda_cache_head
*c
;
3645 /* parm may contain the verb stuff for get/set amp */
3646 verb
= verb
| (parm
>> 8);
3648 key
= build_cmd_cache_key(nid
, verb
);
3649 mutex_lock(&codec
->bus
->cmd_mutex
);
3650 c
= get_hash(&codec
->cmd_cache
, key
);
3651 if (c
&& c
->val
== parm
) {
3652 mutex_unlock(&codec
->bus
->cmd_mutex
);
3655 mutex_unlock(&codec
->bus
->cmd_mutex
);
3656 return snd_hda_codec_write_cache(codec
, nid
, flags
, verb
, parm
);
3658 EXPORT_SYMBOL_HDA(snd_hda_codec_update_cache
);
3661 * snd_hda_codec_resume_cache - Resume the all commands from the cache
3662 * @codec: HD-audio codec
3664 * Execute all verbs recorded in the command caches to resume.
3666 void snd_hda_codec_resume_cache(struct hda_codec
*codec
)
3670 mutex_lock(&codec
->hash_mutex
);
3671 codec
->cached_write
= 0;
3672 for (i
= 0; i
< codec
->cmd_cache
.buf
.used
; i
++) {
3673 struct hda_cache_head
*buffer
;
3676 buffer
= snd_array_elem(&codec
->cmd_cache
.buf
, i
);
3683 mutex_unlock(&codec
->hash_mutex
);
3684 snd_hda_codec_write(codec
, get_cmd_cache_nid(key
), 0,
3685 get_cmd_cache_cmd(key
), buffer
->val
);
3686 mutex_lock(&codec
->hash_mutex
);
3688 mutex_unlock(&codec
->hash_mutex
);
3690 EXPORT_SYMBOL_HDA(snd_hda_codec_resume_cache
);
3693 * snd_hda_sequence_write_cache - sequence writes with caching
3694 * @codec: the HDA codec
3695 * @seq: VERB array to send
3697 * Send the commands sequentially from the given array.
3698 * Thte commands are recorded on cache for power-save and resume.
3699 * The array must be terminated with NID=0.
3701 void snd_hda_sequence_write_cache(struct hda_codec
*codec
,
3702 const struct hda_verb
*seq
)
3704 for (; seq
->nid
; seq
++)
3705 snd_hda_codec_write_cache(codec
, seq
->nid
, 0, seq
->verb
,
3708 EXPORT_SYMBOL_HDA(snd_hda_sequence_write_cache
);
3711 * snd_hda_codec_flush_cache - Execute all pending (cached) amps / verbs
3712 * @codec: HD-audio codec
3714 void snd_hda_codec_flush_cache(struct hda_codec
*codec
)
3716 snd_hda_codec_resume_amp(codec
);
3717 snd_hda_codec_resume_cache(codec
);
3719 EXPORT_SYMBOL_HDA(snd_hda_codec_flush_cache
);
3721 void snd_hda_codec_set_power_to_all(struct hda_codec
*codec
, hda_nid_t fg
,
3722 unsigned int power_state
)
3724 hda_nid_t nid
= codec
->start_nid
;
3727 for (i
= 0; i
< codec
->num_nodes
; i
++, nid
++) {
3728 unsigned int wcaps
= get_wcaps(codec
, nid
);
3729 unsigned int state
= power_state
;
3730 if (!(wcaps
& AC_WCAP_POWER
))
3732 if (codec
->power_filter
) {
3733 state
= codec
->power_filter(codec
, nid
, power_state
);
3734 if (state
!= power_state
&& power_state
== AC_PWRST_D3
)
3737 snd_hda_codec_write(codec
, nid
, 0, AC_VERB_SET_POWER_STATE
,
3741 EXPORT_SYMBOL_HDA(snd_hda_codec_set_power_to_all
);
3744 * supported power states check
3746 static bool snd_hda_codec_get_supported_ps(struct hda_codec
*codec
, hda_nid_t fg
,
3747 unsigned int power_state
)
3749 int sup
= snd_hda_param_read(codec
, fg
, AC_PAR_POWER_STATE
);
3753 if (sup
& power_state
)
3760 * wait until the state is reached, returns the current state
3762 static unsigned int hda_sync_power_state(struct hda_codec
*codec
,
3764 unsigned int power_state
)
3766 unsigned long end_time
= jiffies
+ msecs_to_jiffies(500);
3767 unsigned int state
, actual_state
;
3770 state
= snd_hda_codec_read(codec
, fg
, 0,
3771 AC_VERB_GET_POWER_STATE
, 0);
3772 if (state
& AC_PWRST_ERROR
)
3774 actual_state
= (state
>> 4) & 0x0f;
3775 if (actual_state
== power_state
)
3777 if (time_after_eq(jiffies
, end_time
))
3779 /* wait until the codec reachs to the target state */
3785 /* don't power down the widget if it controls eapd and EAPD_BTLENABLE is set */
3786 unsigned int snd_hda_codec_eapd_power_filter(struct hda_codec
*codec
,
3788 unsigned int power_state
)
3790 if (power_state
== AC_PWRST_D3
&&
3791 get_wcaps_type(get_wcaps(codec
, nid
)) == AC_WID_PIN
&&
3792 (snd_hda_query_pin_caps(codec
, nid
) & AC_PINCAP_EAPD
)) {
3793 int eapd
= snd_hda_codec_read(codec
, nid
, 0,
3794 AC_VERB_GET_EAPD_BTLENABLE
, 0);
3800 EXPORT_SYMBOL_HDA(snd_hda_codec_eapd_power_filter
);
3803 * set power state of the codec, and return the power state
3805 static unsigned int hda_set_power_state(struct hda_codec
*codec
,
3806 unsigned int power_state
)
3808 hda_nid_t fg
= codec
->afg
? codec
->afg
: codec
->mfg
;
3813 /* this delay seems necessary to avoid click noise at power-down */
3814 if (power_state
== AC_PWRST_D3
) {
3815 /* transition time less than 10ms for power down */
3816 msleep(codec
->epss
? 10 : 100);
3817 flags
= HDA_RW_NO_RESPONSE_FALLBACK
;
3820 /* repeat power states setting at most 10 times*/
3821 for (count
= 0; count
< 10; count
++) {
3822 if (codec
->patch_ops
.set_power_state
)
3823 codec
->patch_ops
.set_power_state(codec
, fg
,
3826 snd_hda_codec_read(codec
, fg
, flags
,
3827 AC_VERB_SET_POWER_STATE
,
3829 snd_hda_codec_set_power_to_all(codec
, fg
, power_state
);
3831 state
= hda_sync_power_state(codec
, fg
, power_state
);
3832 if (!(state
& AC_PWRST_ERROR
))
3839 /* sync power states of all widgets;
3840 * this is called at the end of codec parsing
3842 static void sync_power_up_states(struct hda_codec
*codec
)
3844 hda_nid_t nid
= codec
->start_nid
;
3847 /* don't care if no filter is used */
3848 if (!codec
->power_filter
)
3851 for (i
= 0; i
< codec
->num_nodes
; i
++, nid
++) {
3852 unsigned int wcaps
= get_wcaps(codec
, nid
);
3853 unsigned int target
;
3854 if (!(wcaps
& AC_WCAP_POWER
))
3856 target
= codec
->power_filter(codec
, nid
, AC_PWRST_D0
);
3857 if (target
== AC_PWRST_D0
)
3859 if (!snd_hda_check_power_state(codec
, nid
, target
))
3860 snd_hda_codec_write(codec
, nid
, 0,
3861 AC_VERB_SET_POWER_STATE
, target
);
3865 #ifdef CONFIG_SND_HDA_HWDEP
3866 /* execute additional init verbs */
3867 static void hda_exec_init_verbs(struct hda_codec
*codec
)
3869 if (codec
->init_verbs
.list
)
3870 snd_hda_sequence_write(codec
, codec
->init_verbs
.list
);
3873 static inline void hda_exec_init_verbs(struct hda_codec
*codec
) {}
3878 * call suspend and power-down; used both from PM and power-save
3879 * this function returns the power state in the end
3881 static unsigned int hda_call_codec_suspend(struct hda_codec
*codec
, bool in_wq
)
3887 if (codec
->patch_ops
.suspend
)
3888 codec
->patch_ops
.suspend(codec
);
3889 hda_cleanup_all_streams(codec
);
3890 state
= hda_set_power_state(codec
, AC_PWRST_D3
);
3891 /* Cancel delayed work if we aren't currently running from it. */
3893 cancel_delayed_work_sync(&codec
->power_work
);
3894 spin_lock(&codec
->power_lock
);
3895 snd_hda_update_power_acct(codec
);
3896 trace_hda_power_down(codec
);
3897 codec
->power_on
= 0;
3898 codec
->power_transition
= 0;
3899 codec
->power_jiffies
= jiffies
;
3900 spin_unlock(&codec
->power_lock
);
3905 /* mark all entries of cmd and amp caches dirty */
3906 static void hda_mark_cmd_cache_dirty(struct hda_codec
*codec
)
3909 for (i
= 0; i
< codec
->cmd_cache
.buf
.used
; i
++) {
3910 struct hda_cache_head
*cmd
;
3911 cmd
= snd_array_elem(&codec
->cmd_cache
.buf
, i
);
3914 for (i
= 0; i
< codec
->amp_cache
.buf
.used
; i
++) {
3915 struct hda_amp_info
*amp
;
3916 amp
= snd_array_elem(&codec
->amp_cache
.buf
, i
);
3917 amp
->head
.dirty
= 1;
3922 * kick up codec; used both from PM and power-save
3924 static void hda_call_codec_resume(struct hda_codec
*codec
)
3928 hda_mark_cmd_cache_dirty(codec
);
3930 /* set as if powered on for avoiding re-entering the resume
3931 * in the resume / power-save sequence
3933 hda_keep_power_on(codec
);
3934 hda_set_power_state(codec
, AC_PWRST_D0
);
3935 restore_shutup_pins(codec
);
3936 hda_exec_init_verbs(codec
);
3937 snd_hda_jack_set_dirty_all(codec
);
3938 if (codec
->patch_ops
.resume
)
3939 codec
->patch_ops
.resume(codec
);
3941 if (codec
->patch_ops
.init
)
3942 codec
->patch_ops
.init(codec
);
3943 snd_hda_codec_resume_amp(codec
);
3944 snd_hda_codec_resume_cache(codec
);
3947 if (codec
->jackpoll_interval
)
3948 hda_jackpoll_work(&codec
->jackpoll_work
.work
);
3950 snd_hda_jack_report_sync(codec
);
3953 snd_hda_power_down(codec
); /* flag down before returning */
3955 #endif /* CONFIG_PM */
3959 * snd_hda_build_controls - build mixer controls
3962 * Creates mixer controls for each codec included in the bus.
3964 * Returns 0 if successful, otherwise a negative error code.
3966 int snd_hda_build_controls(struct hda_bus
*bus
)
3968 struct hda_codec
*codec
;
3970 list_for_each_entry(codec
, &bus
->codec_list
, list
) {
3971 int err
= snd_hda_codec_build_controls(codec
);
3973 printk(KERN_ERR
"hda_codec: cannot build controls "
3974 "for #%d (error %d)\n", codec
->addr
, err
);
3975 err
= snd_hda_codec_reset(codec
);
3978 "hda_codec: cannot revert codec\n");
3985 EXPORT_SYMBOL_HDA(snd_hda_build_controls
);
3988 * add standard channel maps if not specified
3990 static int add_std_chmaps(struct hda_codec
*codec
)
3994 for (i
= 0; i
< codec
->num_pcms
; i
++) {
3995 for (str
= 0; str
< 2; str
++) {
3996 struct snd_pcm
*pcm
= codec
->pcm_info
[i
].pcm
;
3997 struct hda_pcm_stream
*hinfo
=
3998 &codec
->pcm_info
[i
].stream
[str
];
3999 struct snd_pcm_chmap
*chmap
;
4000 const struct snd_pcm_chmap_elem
*elem
;
4002 if (codec
->pcm_info
[i
].own_chmap
)
4004 if (!pcm
|| !hinfo
->substreams
)
4006 elem
= hinfo
->chmap
? hinfo
->chmap
: snd_pcm_std_chmaps
;
4007 err
= snd_pcm_add_chmap_ctls(pcm
, str
, elem
,
4008 hinfo
->channels_max
,
4012 chmap
->channel_mask
= SND_PCM_CHMAP_MASK_2468
;
4018 /* default channel maps for 2.1 speakers;
4019 * since HD-audio supports only stereo, odd number channels are omitted
4021 const struct snd_pcm_chmap_elem snd_pcm_2_1_chmaps
[] = {
4023 .map
= { SNDRV_CHMAP_FL
, SNDRV_CHMAP_FR
} },
4025 .map
= { SNDRV_CHMAP_FL
, SNDRV_CHMAP_FR
,
4026 SNDRV_CHMAP_LFE
, SNDRV_CHMAP_LFE
} },
4029 EXPORT_SYMBOL_GPL(snd_pcm_2_1_chmaps
);
4031 int snd_hda_codec_build_controls(struct hda_codec
*codec
)
4034 hda_exec_init_verbs(codec
);
4035 /* continue to initialize... */
4036 if (codec
->patch_ops
.init
)
4037 err
= codec
->patch_ops
.init(codec
);
4038 if (!err
&& codec
->patch_ops
.build_controls
)
4039 err
= codec
->patch_ops
.build_controls(codec
);
4043 /* we create chmaps here instead of build_pcms */
4044 err
= add_std_chmaps(codec
);
4048 if (codec
->jackpoll_interval
)
4049 hda_jackpoll_work(&codec
->jackpoll_work
.work
);
4051 snd_hda_jack_report_sync(codec
); /* call at the last init point */
4052 sync_power_up_states(codec
);
4059 struct hda_rate_tbl
{
4061 unsigned int alsa_bits
;
4062 unsigned int hda_fmt
;
4065 /* rate = base * mult / div */
4066 #define HDA_RATE(base, mult, div) \
4067 (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \
4068 (((div) - 1) << AC_FMT_DIV_SHIFT))
4070 static struct hda_rate_tbl rate_bits
[] = {
4071 /* rate in Hz, ALSA rate bitmask, HDA format value */
4073 /* autodetected value used in snd_hda_query_supported_pcm */
4074 { 8000, SNDRV_PCM_RATE_8000
, HDA_RATE(48, 1, 6) },
4075 { 11025, SNDRV_PCM_RATE_11025
, HDA_RATE(44, 1, 4) },
4076 { 16000, SNDRV_PCM_RATE_16000
, HDA_RATE(48, 1, 3) },
4077 { 22050, SNDRV_PCM_RATE_22050
, HDA_RATE(44, 1, 2) },
4078 { 32000, SNDRV_PCM_RATE_32000
, HDA_RATE(48, 2, 3) },
4079 { 44100, SNDRV_PCM_RATE_44100
, HDA_RATE(44, 1, 1) },
4080 { 48000, SNDRV_PCM_RATE_48000
, HDA_RATE(48, 1, 1) },
4081 { 88200, SNDRV_PCM_RATE_88200
, HDA_RATE(44, 2, 1) },
4082 { 96000, SNDRV_PCM_RATE_96000
, HDA_RATE(48, 2, 1) },
4083 { 176400, SNDRV_PCM_RATE_176400
, HDA_RATE(44, 4, 1) },
4084 { 192000, SNDRV_PCM_RATE_192000
, HDA_RATE(48, 4, 1) },
4085 #define AC_PAR_PCM_RATE_BITS 11
4086 /* up to bits 10, 384kHZ isn't supported properly */
4088 /* not autodetected value */
4089 { 9600, SNDRV_PCM_RATE_KNOT
, HDA_RATE(48, 1, 5) },
4091 { 0 } /* terminator */
4095 * snd_hda_calc_stream_format - calculate format bitset
4096 * @rate: the sample rate
4097 * @channels: the number of channels
4098 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
4099 * @maxbps: the max. bps
4101 * Calculate the format bitset from the given rate, channels and th PCM format.
4103 * Return zero if invalid.
4105 unsigned int snd_hda_calc_stream_format(unsigned int rate
,
4106 unsigned int channels
,
4107 unsigned int format
,
4108 unsigned int maxbps
,
4109 unsigned short spdif_ctls
)
4112 unsigned int val
= 0;
4114 for (i
= 0; rate_bits
[i
].hz
; i
++)
4115 if (rate_bits
[i
].hz
== rate
) {
4116 val
= rate_bits
[i
].hda_fmt
;
4119 if (!rate_bits
[i
].hz
) {
4120 snd_printdd("invalid rate %d\n", rate
);
4124 if (channels
== 0 || channels
> 8) {
4125 snd_printdd("invalid channels %d\n", channels
);
4128 val
|= channels
- 1;
4130 switch (snd_pcm_format_width(format
)) {
4132 val
|= AC_FMT_BITS_8
;
4135 val
|= AC_FMT_BITS_16
;
4140 if (maxbps
>= 32 || format
== SNDRV_PCM_FORMAT_FLOAT_LE
)
4141 val
|= AC_FMT_BITS_32
;
4142 else if (maxbps
>= 24)
4143 val
|= AC_FMT_BITS_24
;
4145 val
|= AC_FMT_BITS_20
;
4148 snd_printdd("invalid format width %d\n",
4149 snd_pcm_format_width(format
));
4153 if (spdif_ctls
& AC_DIG1_NONAUDIO
)
4154 val
|= AC_FMT_TYPE_NON_PCM
;
4158 EXPORT_SYMBOL_HDA(snd_hda_calc_stream_format
);
4160 static unsigned int get_pcm_param(struct hda_codec
*codec
, hda_nid_t nid
,
4163 unsigned int val
= 0;
4164 if (nid
!= codec
->afg
&&
4165 (get_wcaps(codec
, nid
) & AC_WCAP_FORMAT_OVRD
))
4166 val
= snd_hda_param_read(codec
, nid
, AC_PAR_PCM
);
4167 if (!val
|| val
== -1)
4168 val
= snd_hda_param_read(codec
, codec
->afg
, AC_PAR_PCM
);
4169 if (!val
|| val
== -1)
4174 static unsigned int query_pcm_param(struct hda_codec
*codec
, hda_nid_t nid
)
4176 return query_caps_hash(codec
, nid
, 0, HDA_HASH_PARPCM_KEY(nid
),
4180 static unsigned int get_stream_param(struct hda_codec
*codec
, hda_nid_t nid
,
4183 unsigned int streams
= snd_hda_param_read(codec
, nid
, AC_PAR_STREAM
);
4184 if (!streams
|| streams
== -1)
4185 streams
= snd_hda_param_read(codec
, codec
->afg
, AC_PAR_STREAM
);
4186 if (!streams
|| streams
== -1)
4191 static unsigned int query_stream_param(struct hda_codec
*codec
, hda_nid_t nid
)
4193 return query_caps_hash(codec
, nid
, 0, HDA_HASH_PARSTR_KEY(nid
),
4198 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
4199 * @codec: the HDA codec
4200 * @nid: NID to query
4201 * @ratesp: the pointer to store the detected rate bitflags
4202 * @formatsp: the pointer to store the detected formats
4203 * @bpsp: the pointer to store the detected format widths
4205 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
4206 * or @bsps argument is ignored.
4208 * Returns 0 if successful, otherwise a negative error code.
4210 int snd_hda_query_supported_pcm(struct hda_codec
*codec
, hda_nid_t nid
,
4211 u32
*ratesp
, u64
*formatsp
, unsigned int *bpsp
)
4213 unsigned int i
, val
, wcaps
;
4215 wcaps
= get_wcaps(codec
, nid
);
4216 val
= query_pcm_param(codec
, nid
);
4220 for (i
= 0; i
< AC_PAR_PCM_RATE_BITS
; i
++) {
4222 rates
|= rate_bits
[i
].alsa_bits
;
4225 snd_printk(KERN_ERR
"hda_codec: rates == 0 "
4226 "(nid=0x%x, val=0x%x, ovrd=%i)\n",
4228 (wcaps
& AC_WCAP_FORMAT_OVRD
) ? 1 : 0);
4234 if (formatsp
|| bpsp
) {
4236 unsigned int streams
, bps
;
4238 streams
= query_stream_param(codec
, nid
);
4243 if (streams
& AC_SUPFMT_PCM
) {
4244 if (val
& AC_SUPPCM_BITS_8
) {
4245 formats
|= SNDRV_PCM_FMTBIT_U8
;
4248 if (val
& AC_SUPPCM_BITS_16
) {
4249 formats
|= SNDRV_PCM_FMTBIT_S16_LE
;
4252 if (wcaps
& AC_WCAP_DIGITAL
) {
4253 if (val
& AC_SUPPCM_BITS_32
)
4254 formats
|= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
;
4255 if (val
& (AC_SUPPCM_BITS_20
|AC_SUPPCM_BITS_24
))
4256 formats
|= SNDRV_PCM_FMTBIT_S32_LE
;
4257 if (val
& AC_SUPPCM_BITS_24
)
4259 else if (val
& AC_SUPPCM_BITS_20
)
4261 } else if (val
& (AC_SUPPCM_BITS_20
|AC_SUPPCM_BITS_24
|
4262 AC_SUPPCM_BITS_32
)) {
4263 formats
|= SNDRV_PCM_FMTBIT_S32_LE
;
4264 if (val
& AC_SUPPCM_BITS_32
)
4266 else if (val
& AC_SUPPCM_BITS_24
)
4268 else if (val
& AC_SUPPCM_BITS_20
)
4272 #if 0 /* FIXME: CS4206 doesn't work, which is the only codec supporting float */
4273 if (streams
& AC_SUPFMT_FLOAT32
) {
4274 formats
|= SNDRV_PCM_FMTBIT_FLOAT_LE
;
4279 if (streams
== AC_SUPFMT_AC3
) {
4280 /* should be exclusive */
4281 /* temporary hack: we have still no proper support
4282 * for the direct AC3 stream...
4284 formats
|= SNDRV_PCM_FMTBIT_U8
;
4288 snd_printk(KERN_ERR
"hda_codec: formats == 0 "
4289 "(nid=0x%x, val=0x%x, ovrd=%i, "
4292 (wcaps
& AC_WCAP_FORMAT_OVRD
) ? 1 : 0,
4297 *formatsp
= formats
;
4304 EXPORT_SYMBOL_HDA(snd_hda_query_supported_pcm
);
4307 * snd_hda_is_supported_format - Check the validity of the format
4308 * @codec: HD-audio codec
4309 * @nid: NID to check
4310 * @format: the HD-audio format value to check
4312 * Check whether the given node supports the format value.
4314 * Returns 1 if supported, 0 if not.
4316 int snd_hda_is_supported_format(struct hda_codec
*codec
, hda_nid_t nid
,
4317 unsigned int format
)
4320 unsigned int val
= 0, rate
, stream
;
4322 val
= query_pcm_param(codec
, nid
);
4326 rate
= format
& 0xff00;
4327 for (i
= 0; i
< AC_PAR_PCM_RATE_BITS
; i
++)
4328 if (rate_bits
[i
].hda_fmt
== rate
) {
4333 if (i
>= AC_PAR_PCM_RATE_BITS
)
4336 stream
= query_stream_param(codec
, nid
);
4340 if (stream
& AC_SUPFMT_PCM
) {
4341 switch (format
& 0xf0) {
4343 if (!(val
& AC_SUPPCM_BITS_8
))
4347 if (!(val
& AC_SUPPCM_BITS_16
))
4351 if (!(val
& AC_SUPPCM_BITS_20
))
4355 if (!(val
& AC_SUPPCM_BITS_24
))
4359 if (!(val
& AC_SUPPCM_BITS_32
))
4366 /* FIXME: check for float32 and AC3? */
4371 EXPORT_SYMBOL_HDA(snd_hda_is_supported_format
);
4376 static int hda_pcm_default_open_close(struct hda_pcm_stream
*hinfo
,
4377 struct hda_codec
*codec
,
4378 struct snd_pcm_substream
*substream
)
4383 static int hda_pcm_default_prepare(struct hda_pcm_stream
*hinfo
,
4384 struct hda_codec
*codec
,
4385 unsigned int stream_tag
,
4386 unsigned int format
,
4387 struct snd_pcm_substream
*substream
)
4389 snd_hda_codec_setup_stream(codec
, hinfo
->nid
, stream_tag
, 0, format
);
4393 static int hda_pcm_default_cleanup(struct hda_pcm_stream
*hinfo
,
4394 struct hda_codec
*codec
,
4395 struct snd_pcm_substream
*substream
)
4397 snd_hda_codec_cleanup_stream(codec
, hinfo
->nid
);
4401 static int set_pcm_default_values(struct hda_codec
*codec
,
4402 struct hda_pcm_stream
*info
)
4406 /* query support PCM information from the given NID */
4407 if (info
->nid
&& (!info
->rates
|| !info
->formats
)) {
4408 err
= snd_hda_query_supported_pcm(codec
, info
->nid
,
4409 info
->rates
? NULL
: &info
->rates
,
4410 info
->formats
? NULL
: &info
->formats
,
4411 info
->maxbps
? NULL
: &info
->maxbps
);
4415 if (info
->ops
.open
== NULL
)
4416 info
->ops
.open
= hda_pcm_default_open_close
;
4417 if (info
->ops
.close
== NULL
)
4418 info
->ops
.close
= hda_pcm_default_open_close
;
4419 if (info
->ops
.prepare
== NULL
) {
4420 if (snd_BUG_ON(!info
->nid
))
4422 info
->ops
.prepare
= hda_pcm_default_prepare
;
4424 if (info
->ops
.cleanup
== NULL
) {
4425 if (snd_BUG_ON(!info
->nid
))
4427 info
->ops
.cleanup
= hda_pcm_default_cleanup
;
4433 * codec prepare/cleanup entries
4435 int snd_hda_codec_prepare(struct hda_codec
*codec
,
4436 struct hda_pcm_stream
*hinfo
,
4437 unsigned int stream
,
4438 unsigned int format
,
4439 struct snd_pcm_substream
*substream
)
4442 mutex_lock(&codec
->bus
->prepare_mutex
);
4443 ret
= hinfo
->ops
.prepare(hinfo
, codec
, stream
, format
, substream
);
4445 purify_inactive_streams(codec
);
4446 mutex_unlock(&codec
->bus
->prepare_mutex
);
4449 EXPORT_SYMBOL_HDA(snd_hda_codec_prepare
);
4451 void snd_hda_codec_cleanup(struct hda_codec
*codec
,
4452 struct hda_pcm_stream
*hinfo
,
4453 struct snd_pcm_substream
*substream
)
4455 mutex_lock(&codec
->bus
->prepare_mutex
);
4456 hinfo
->ops
.cleanup(hinfo
, codec
, substream
);
4457 mutex_unlock(&codec
->bus
->prepare_mutex
);
4459 EXPORT_SYMBOL_HDA(snd_hda_codec_cleanup
);
4462 const char *snd_hda_pcm_type_name
[HDA_PCM_NTYPES
] = {
4463 "Audio", "SPDIF", "HDMI", "Modem"
4467 * get the empty PCM device number to assign
4469 static int get_empty_pcm_device(struct hda_bus
*bus
, unsigned int type
)
4471 /* audio device indices; not linear to keep compatibility */
4472 /* assigned to static slots up to dev#10; if more needed, assign
4473 * the later slot dynamically (when CONFIG_SND_DYNAMIC_MINORS=y)
4475 static int audio_idx
[HDA_PCM_NTYPES
][5] = {
4476 [HDA_PCM_TYPE_AUDIO
] = { 0, 2, 4, 5, -1 },
4477 [HDA_PCM_TYPE_SPDIF
] = { 1, -1 },
4478 [HDA_PCM_TYPE_HDMI
] = { 3, 7, 8, 9, -1 },
4479 [HDA_PCM_TYPE_MODEM
] = { 6, -1 },
4483 if (type
>= HDA_PCM_NTYPES
) {
4484 snd_printk(KERN_WARNING
"Invalid PCM type %d\n", type
);
4488 for (i
= 0; audio_idx
[type
][i
] >= 0; i
++) {
4489 #ifndef CONFIG_SND_DYNAMIC_MINORS
4490 if (audio_idx
[type
][i
] >= 8)
4493 if (!test_and_set_bit(audio_idx
[type
][i
], bus
->pcm_dev_bits
))
4494 return audio_idx
[type
][i
];
4497 #ifdef CONFIG_SND_DYNAMIC_MINORS
4498 /* non-fixed slots starting from 10 */
4499 for (i
= 10; i
< 32; i
++) {
4500 if (!test_and_set_bit(i
, bus
->pcm_dev_bits
))
4505 snd_printk(KERN_WARNING
"Too many %s devices\n",
4506 snd_hda_pcm_type_name
[type
]);
4507 #ifndef CONFIG_SND_DYNAMIC_MINORS
4508 snd_printk(KERN_WARNING
"Consider building the kernel with CONFIG_SND_DYNAMIC_MINORS=y\n");
4514 * attach a new PCM stream
4516 static int snd_hda_attach_pcm(struct hda_codec
*codec
, struct hda_pcm
*pcm
)
4518 struct hda_bus
*bus
= codec
->bus
;
4519 struct hda_pcm_stream
*info
;
4522 if (snd_BUG_ON(!pcm
->name
))
4524 for (stream
= 0; stream
< 2; stream
++) {
4525 info
= &pcm
->stream
[stream
];
4526 if (info
->substreams
) {
4527 err
= set_pcm_default_values(codec
, info
);
4532 return bus
->ops
.attach_pcm(bus
, codec
, pcm
);
4535 /* assign all PCMs of the given codec */
4536 int snd_hda_codec_build_pcms(struct hda_codec
*codec
)
4541 if (!codec
->num_pcms
) {
4542 if (!codec
->patch_ops
.build_pcms
)
4544 err
= codec
->patch_ops
.build_pcms(codec
);
4546 printk(KERN_ERR
"hda_codec: cannot build PCMs"
4547 "for #%d (error %d)\n", codec
->addr
, err
);
4548 err
= snd_hda_codec_reset(codec
);
4551 "hda_codec: cannot revert codec\n");
4556 for (pcm
= 0; pcm
< codec
->num_pcms
; pcm
++) {
4557 struct hda_pcm
*cpcm
= &codec
->pcm_info
[pcm
];
4560 if (!cpcm
->stream
[0].substreams
&& !cpcm
->stream
[1].substreams
)
4561 continue; /* no substreams assigned */
4564 dev
= get_empty_pcm_device(codec
->bus
, cpcm
->pcm_type
);
4566 continue; /* no fatal error */
4568 err
= snd_hda_attach_pcm(codec
, cpcm
);
4570 printk(KERN_ERR
"hda_codec: cannot attach "
4571 "PCM stream %d for codec #%d\n",
4573 continue; /* no fatal error */
4581 * snd_hda_build_pcms - build PCM information
4584 * Create PCM information for each codec included in the bus.
4586 * The build_pcms codec patch is requested to set up codec->num_pcms and
4587 * codec->pcm_info properly. The array is referred by the top-level driver
4588 * to create its PCM instances.
4589 * The allocated codec->pcm_info should be released in codec->patch_ops.free
4592 * At least, substreams, channels_min and channels_max must be filled for
4593 * each stream. substreams = 0 indicates that the stream doesn't exist.
4594 * When rates and/or formats are zero, the supported values are queried
4595 * from the given nid. The nid is used also by the default ops.prepare
4596 * and ops.cleanup callbacks.
4598 * The driver needs to call ops.open in its open callback. Similarly,
4599 * ops.close is supposed to be called in the close callback.
4600 * ops.prepare should be called in the prepare or hw_params callback
4601 * with the proper parameters for set up.
4602 * ops.cleanup should be called in hw_free for clean up of streams.
4604 * This function returns 0 if successful, or a negative error code.
4606 int snd_hda_build_pcms(struct hda_bus
*bus
)
4608 struct hda_codec
*codec
;
4610 list_for_each_entry(codec
, &bus
->codec_list
, list
) {
4611 int err
= snd_hda_codec_build_pcms(codec
);
4617 EXPORT_SYMBOL_HDA(snd_hda_build_pcms
);
4620 * snd_hda_check_board_config - compare the current codec with the config table
4621 * @codec: the HDA codec
4622 * @num_configs: number of config enums
4623 * @models: array of model name strings
4624 * @tbl: configuration table, terminated by null entries
4626 * Compares the modelname or PCI subsystem id of the current codec with the
4627 * given configuration table. If a matching entry is found, returns its
4628 * config value (supposed to be 0 or positive).
4630 * If no entries are matching, the function returns a negative value.
4632 int snd_hda_check_board_config(struct hda_codec
*codec
,
4633 int num_configs
, const char * const *models
,
4634 const struct snd_pci_quirk
*tbl
)
4636 if (codec
->modelname
&& models
) {
4638 for (i
= 0; i
< num_configs
; i
++) {
4640 !strcmp(codec
->modelname
, models
[i
])) {
4641 snd_printd(KERN_INFO
"hda_codec: model '%s' is "
4642 "selected\n", models
[i
]);
4648 if (!codec
->bus
->pci
|| !tbl
)
4651 tbl
= snd_pci_quirk_lookup(codec
->bus
->pci
, tbl
);
4654 if (tbl
->value
>= 0 && tbl
->value
< num_configs
) {
4655 #ifdef CONFIG_SND_DEBUG_VERBOSE
4657 const char *model
= NULL
;
4659 model
= models
[tbl
->value
];
4661 sprintf(tmp
, "#%d", tbl
->value
);
4664 snd_printdd(KERN_INFO
"hda_codec: model '%s' is selected "
4665 "for config %x:%x (%s)\n",
4666 model
, tbl
->subvendor
, tbl
->subdevice
,
4667 (tbl
->name
? tbl
->name
: "Unknown device"));
4673 EXPORT_SYMBOL_HDA(snd_hda_check_board_config
);
4676 * snd_hda_check_board_codec_sid_config - compare the current codec
4677 subsystem ID with the
4680 This is important for Gateway notebooks with SB450 HDA Audio
4681 where the vendor ID of the PCI device is:
4682 ATI Technologies Inc SB450 HDA Audio [1002:437b]
4683 and the vendor/subvendor are found only at the codec.
4685 * @codec: the HDA codec
4686 * @num_configs: number of config enums
4687 * @models: array of model name strings
4688 * @tbl: configuration table, terminated by null entries
4690 * Compares the modelname or PCI subsystem id of the current codec with the
4691 * given configuration table. If a matching entry is found, returns its
4692 * config value (supposed to be 0 or positive).
4694 * If no entries are matching, the function returns a negative value.
4696 int snd_hda_check_board_codec_sid_config(struct hda_codec
*codec
,
4697 int num_configs
, const char * const *models
,
4698 const struct snd_pci_quirk
*tbl
)
4700 const struct snd_pci_quirk
*q
;
4702 /* Search for codec ID */
4703 for (q
= tbl
; q
->subvendor
; q
++) {
4704 unsigned int mask
= 0xffff0000 | q
->subdevice_mask
;
4705 unsigned int id
= (q
->subdevice
| (q
->subvendor
<< 16)) & mask
;
4706 if ((codec
->subsystem_id
& mask
) == id
)
4715 if (tbl
->value
>= 0 && tbl
->value
< num_configs
) {
4716 #ifdef CONFIG_SND_DEBUG_VERBOSE
4718 const char *model
= NULL
;
4720 model
= models
[tbl
->value
];
4722 sprintf(tmp
, "#%d", tbl
->value
);
4725 snd_printdd(KERN_INFO
"hda_codec: model '%s' is selected "
4726 "for config %x:%x (%s)\n",
4727 model
, tbl
->subvendor
, tbl
->subdevice
,
4728 (tbl
->name
? tbl
->name
: "Unknown device"));
4734 EXPORT_SYMBOL_HDA(snd_hda_check_board_codec_sid_config
);
4737 * snd_hda_add_new_ctls - create controls from the array
4738 * @codec: the HDA codec
4739 * @knew: the array of struct snd_kcontrol_new
4741 * This helper function creates and add new controls in the given array.
4742 * The array must be terminated with an empty entry as terminator.
4744 * Returns 0 if successful, or a negative error code.
4746 int snd_hda_add_new_ctls(struct hda_codec
*codec
,
4747 const struct snd_kcontrol_new
*knew
)
4751 for (; knew
->name
; knew
++) {
4752 struct snd_kcontrol
*kctl
;
4753 int addr
= 0, idx
= 0;
4754 if (knew
->iface
== -1) /* skip this codec private value */
4757 kctl
= snd_ctl_new1(knew
, codec
);
4761 kctl
->id
.device
= addr
;
4763 kctl
->id
.index
= idx
;
4764 err
= snd_hda_ctl_add(codec
, 0, kctl
);
4767 /* try first with another device index corresponding to
4768 * the codec addr; if it still fails (or it's the
4769 * primary codec), then try another control index
4771 if (!addr
&& codec
->addr
)
4773 else if (!idx
&& !knew
->index
) {
4774 idx
= find_empty_mixer_ctl_idx(codec
,
4784 EXPORT_SYMBOL_HDA(snd_hda_add_new_ctls
);
4787 static void hda_power_work(struct work_struct
*work
)
4789 struct hda_codec
*codec
=
4790 container_of(work
, struct hda_codec
, power_work
.work
);
4791 struct hda_bus
*bus
= codec
->bus
;
4794 spin_lock(&codec
->power_lock
);
4795 if (codec
->power_transition
> 0) { /* during power-up sequence? */
4796 spin_unlock(&codec
->power_lock
);
4799 if (!codec
->power_on
|| codec
->power_count
) {
4800 codec
->power_transition
= 0;
4801 spin_unlock(&codec
->power_lock
);
4804 spin_unlock(&codec
->power_lock
);
4806 state
= hda_call_codec_suspend(codec
, true);
4807 codec
->pm_down_notified
= 0;
4808 if (!bus
->power_keep_link_on
&& (state
& AC_PWRST_CLK_STOP_OK
)) {
4809 codec
->pm_down_notified
= 1;
4810 hda_call_pm_notify(bus
, false);
4814 static void hda_keep_power_on(struct hda_codec
*codec
)
4816 spin_lock(&codec
->power_lock
);
4817 codec
->power_count
++;
4818 codec
->power_on
= 1;
4819 codec
->power_jiffies
= jiffies
;
4820 spin_unlock(&codec
->power_lock
);
4823 /* update the power on/off account with the current jiffies */
4824 void snd_hda_update_power_acct(struct hda_codec
*codec
)
4826 unsigned long delta
= jiffies
- codec
->power_jiffies
;
4827 if (codec
->power_on
)
4828 codec
->power_on_acct
+= delta
;
4830 codec
->power_off_acct
+= delta
;
4831 codec
->power_jiffies
+= delta
;
4834 /* Transition to powered up, if wait_power_down then wait for a pending
4835 * transition to D3 to complete. A pending D3 transition is indicated
4836 * with power_transition == -1. */
4837 /* call this with codec->power_lock held! */
4838 static void __snd_hda_power_up(struct hda_codec
*codec
, bool wait_power_down
)
4840 struct hda_bus
*bus
= codec
->bus
;
4842 /* Return if power_on or transitioning to power_on, unless currently
4844 if ((codec
->power_on
|| codec
->power_transition
> 0) &&
4845 !(wait_power_down
&& codec
->power_transition
< 0))
4847 spin_unlock(&codec
->power_lock
);
4849 cancel_delayed_work_sync(&codec
->power_work
);
4851 spin_lock(&codec
->power_lock
);
4852 /* If the power down delayed work was cancelled above before starting,
4853 * then there is no need to go through power up here.
4855 if (codec
->power_on
) {
4856 if (codec
->power_transition
< 0)
4857 codec
->power_transition
= 0;
4861 trace_hda_power_up(codec
);
4862 snd_hda_update_power_acct(codec
);
4863 codec
->power_on
= 1;
4864 codec
->power_jiffies
= jiffies
;
4865 codec
->power_transition
= 1; /* avoid reentrance */
4866 spin_unlock(&codec
->power_lock
);
4868 if (codec
->pm_down_notified
) {
4869 codec
->pm_down_notified
= 0;
4870 hda_call_pm_notify(bus
, true);
4873 hda_call_codec_resume(codec
);
4875 spin_lock(&codec
->power_lock
);
4876 codec
->power_transition
= 0;
4879 #define power_save(codec) \
4880 ((codec)->bus->power_save ? *(codec)->bus->power_save : 0)
4882 /* Transition to powered down */
4883 static void __snd_hda_power_down(struct hda_codec
*codec
)
4885 if (!codec
->power_on
|| codec
->power_count
|| codec
->power_transition
)
4888 if (power_save(codec
)) {
4889 codec
->power_transition
= -1; /* avoid reentrance */
4890 queue_delayed_work(codec
->bus
->workq
, &codec
->power_work
,
4891 msecs_to_jiffies(power_save(codec
) * 1000));
4896 * snd_hda_power_save - Power-up/down/sync the codec
4897 * @codec: HD-audio codec
4898 * @delta: the counter delta to change
4900 * Change the power-up counter via @delta, and power up or down the hardware
4901 * appropriately. For the power-down, queue to the delayed action.
4902 * Passing zero to @delta means to synchronize the power state.
4904 void snd_hda_power_save(struct hda_codec
*codec
, int delta
, bool d3wait
)
4906 spin_lock(&codec
->power_lock
);
4907 codec
->power_count
+= delta
;
4908 trace_hda_power_count(codec
);
4910 __snd_hda_power_up(codec
, d3wait
);
4912 __snd_hda_power_down(codec
);
4913 spin_unlock(&codec
->power_lock
);
4915 EXPORT_SYMBOL_HDA(snd_hda_power_save
);
4918 * snd_hda_check_amp_list_power - Check the amp list and update the power
4919 * @codec: HD-audio codec
4920 * @check: the object containing an AMP list and the status
4921 * @nid: NID to check / update
4923 * Check whether the given NID is in the amp list. If it's in the list,
4924 * check the current AMP status, and update the the power-status according
4925 * to the mute status.
4927 * This function is supposed to be set or called from the check_power_status
4930 int snd_hda_check_amp_list_power(struct hda_codec
*codec
,
4931 struct hda_loopback_check
*check
,
4934 const struct hda_amp_list
*p
;
4937 if (!check
->amplist
)
4939 for (p
= check
->amplist
; p
->nid
; p
++) {
4944 return 0; /* nothing changed */
4946 for (p
= check
->amplist
; p
->nid
; p
++) {
4947 for (ch
= 0; ch
< 2; ch
++) {
4948 v
= snd_hda_codec_amp_read(codec
, p
->nid
, ch
, p
->dir
,
4950 if (!(v
& HDA_AMP_MUTE
) && v
> 0) {
4951 if (!check
->power_on
) {
4952 check
->power_on
= 1;
4953 snd_hda_power_up(codec
);
4959 if (check
->power_on
) {
4960 check
->power_on
= 0;
4961 snd_hda_power_down(codec
);
4965 EXPORT_SYMBOL_HDA(snd_hda_check_amp_list_power
);
4969 * Channel mode helper
4973 * snd_hda_ch_mode_info - Info callback helper for the channel mode enum
4975 int snd_hda_ch_mode_info(struct hda_codec
*codec
,
4976 struct snd_ctl_elem_info
*uinfo
,
4977 const struct hda_channel_mode
*chmode
,
4980 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_ENUMERATED
;
4982 uinfo
->value
.enumerated
.items
= num_chmodes
;
4983 if (uinfo
->value
.enumerated
.item
>= num_chmodes
)
4984 uinfo
->value
.enumerated
.item
= num_chmodes
- 1;
4985 sprintf(uinfo
->value
.enumerated
.name
, "%dch",
4986 chmode
[uinfo
->value
.enumerated
.item
].channels
);
4989 EXPORT_SYMBOL_HDA(snd_hda_ch_mode_info
);
4992 * snd_hda_ch_mode_get - Get callback helper for the channel mode enum
4994 int snd_hda_ch_mode_get(struct hda_codec
*codec
,
4995 struct snd_ctl_elem_value
*ucontrol
,
4996 const struct hda_channel_mode
*chmode
,
5002 for (i
= 0; i
< num_chmodes
; i
++) {
5003 if (max_channels
== chmode
[i
].channels
) {
5004 ucontrol
->value
.enumerated
.item
[0] = i
;
5010 EXPORT_SYMBOL_HDA(snd_hda_ch_mode_get
);
5013 * snd_hda_ch_mode_put - Put callback helper for the channel mode enum
5015 int snd_hda_ch_mode_put(struct hda_codec
*codec
,
5016 struct snd_ctl_elem_value
*ucontrol
,
5017 const struct hda_channel_mode
*chmode
,
5023 mode
= ucontrol
->value
.enumerated
.item
[0];
5024 if (mode
>= num_chmodes
)
5026 if (*max_channelsp
== chmode
[mode
].channels
)
5028 /* change the current channel setting */
5029 *max_channelsp
= chmode
[mode
].channels
;
5030 if (chmode
[mode
].sequence
)
5031 snd_hda_sequence_write_cache(codec
, chmode
[mode
].sequence
);
5034 EXPORT_SYMBOL_HDA(snd_hda_ch_mode_put
);
5041 * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum
5043 int snd_hda_input_mux_info(const struct hda_input_mux
*imux
,
5044 struct snd_ctl_elem_info
*uinfo
)
5048 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_ENUMERATED
;
5050 uinfo
->value
.enumerated
.items
= imux
->num_items
;
5051 if (!imux
->num_items
)
5053 index
= uinfo
->value
.enumerated
.item
;
5054 if (index
>= imux
->num_items
)
5055 index
= imux
->num_items
- 1;
5056 strcpy(uinfo
->value
.enumerated
.name
, imux
->items
[index
].label
);
5059 EXPORT_SYMBOL_HDA(snd_hda_input_mux_info
);
5062 * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum
5064 int snd_hda_input_mux_put(struct hda_codec
*codec
,
5065 const struct hda_input_mux
*imux
,
5066 struct snd_ctl_elem_value
*ucontrol
,
5068 unsigned int *cur_val
)
5072 if (!imux
->num_items
)
5074 idx
= ucontrol
->value
.enumerated
.item
[0];
5075 if (idx
>= imux
->num_items
)
5076 idx
= imux
->num_items
- 1;
5077 if (*cur_val
== idx
)
5079 snd_hda_codec_write_cache(codec
, nid
, 0, AC_VERB_SET_CONNECT_SEL
,
5080 imux
->items
[idx
].index
);
5084 EXPORT_SYMBOL_HDA(snd_hda_input_mux_put
);
5088 * process kcontrol info callback of a simple string enum array
5089 * when @num_items is 0 or @texts is NULL, assume a boolean enum array
5091 int snd_hda_enum_helper_info(struct snd_kcontrol
*kcontrol
,
5092 struct snd_ctl_elem_info
*uinfo
,
5093 int num_items
, const char * const *texts
)
5095 static const char * const texts_default
[] = {
5096 "Disabled", "Enabled"
5099 if (!texts
|| !num_items
) {
5101 texts
= texts_default
;
5104 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_ENUMERATED
;
5106 uinfo
->value
.enumerated
.items
= num_items
;
5107 if (uinfo
->value
.enumerated
.item
>= uinfo
->value
.enumerated
.items
)
5108 uinfo
->value
.enumerated
.item
= uinfo
->value
.enumerated
.items
- 1;
5109 strcpy(uinfo
->value
.enumerated
.name
,
5110 texts
[uinfo
->value
.enumerated
.item
]);
5113 EXPORT_SYMBOL_HDA(snd_hda_enum_helper_info
);
5116 * Multi-channel / digital-out PCM helper functions
5119 /* setup SPDIF output stream */
5120 static void setup_dig_out_stream(struct hda_codec
*codec
, hda_nid_t nid
,
5121 unsigned int stream_tag
, unsigned int format
)
5123 struct hda_spdif_out
*spdif
;
5124 unsigned int curr_fmt
;
5127 spdif
= snd_hda_spdif_out_of_nid(codec
, nid
);
5128 curr_fmt
= snd_hda_codec_read(codec
, nid
, 0,
5129 AC_VERB_GET_STREAM_FORMAT
, 0);
5130 reset
= codec
->spdif_status_reset
&&
5131 (spdif
->ctls
& AC_DIG1_ENABLE
) &&
5134 /* turn off SPDIF if needed; otherwise the IEC958 bits won't be
5137 set_dig_out_convert(codec
, nid
,
5138 spdif
->ctls
& ~AC_DIG1_ENABLE
& 0xff,
5140 snd_hda_codec_setup_stream(codec
, nid
, stream_tag
, 0, format
);
5141 if (codec
->slave_dig_outs
) {
5143 for (d
= codec
->slave_dig_outs
; *d
; d
++)
5144 snd_hda_codec_setup_stream(codec
, *d
, stream_tag
, 0,
5147 /* turn on again (if needed) */
5149 set_dig_out_convert(codec
, nid
,
5150 spdif
->ctls
& 0xff, -1);
5153 static void cleanup_dig_out_stream(struct hda_codec
*codec
, hda_nid_t nid
)
5155 snd_hda_codec_cleanup_stream(codec
, nid
);
5156 if (codec
->slave_dig_outs
) {
5158 for (d
= codec
->slave_dig_outs
; *d
; d
++)
5159 snd_hda_codec_cleanup_stream(codec
, *d
);
5164 * snd_hda_bus_reboot_notify - call the reboot notifier of each codec
5165 * @bus: HD-audio bus
5167 void snd_hda_bus_reboot_notify(struct hda_bus
*bus
)
5169 struct hda_codec
*codec
;
5173 list_for_each_entry(codec
, &bus
->codec_list
, list
) {
5174 if (hda_codec_is_power_on(codec
) &&
5175 codec
->patch_ops
.reboot_notify
)
5176 codec
->patch_ops
.reboot_notify(codec
);
5179 EXPORT_SYMBOL_HDA(snd_hda_bus_reboot_notify
);
5182 * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode
5184 int snd_hda_multi_out_dig_open(struct hda_codec
*codec
,
5185 struct hda_multi_out
*mout
)
5187 mutex_lock(&codec
->spdif_mutex
);
5188 if (mout
->dig_out_used
== HDA_DIG_ANALOG_DUP
)
5189 /* already opened as analog dup; reset it once */
5190 cleanup_dig_out_stream(codec
, mout
->dig_out_nid
);
5191 mout
->dig_out_used
= HDA_DIG_EXCLUSIVE
;
5192 mutex_unlock(&codec
->spdif_mutex
);
5195 EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_open
);
5198 * snd_hda_multi_out_dig_prepare - prepare the digital out stream
5200 int snd_hda_multi_out_dig_prepare(struct hda_codec
*codec
,
5201 struct hda_multi_out
*mout
,
5202 unsigned int stream_tag
,
5203 unsigned int format
,
5204 struct snd_pcm_substream
*substream
)
5206 mutex_lock(&codec
->spdif_mutex
);
5207 setup_dig_out_stream(codec
, mout
->dig_out_nid
, stream_tag
, format
);
5208 mutex_unlock(&codec
->spdif_mutex
);
5211 EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_prepare
);
5214 * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream
5216 int snd_hda_multi_out_dig_cleanup(struct hda_codec
*codec
,
5217 struct hda_multi_out
*mout
)
5219 mutex_lock(&codec
->spdif_mutex
);
5220 cleanup_dig_out_stream(codec
, mout
->dig_out_nid
);
5221 mutex_unlock(&codec
->spdif_mutex
);
5224 EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_cleanup
);
5227 * snd_hda_multi_out_dig_close - release the digital out stream
5229 int snd_hda_multi_out_dig_close(struct hda_codec
*codec
,
5230 struct hda_multi_out
*mout
)
5232 mutex_lock(&codec
->spdif_mutex
);
5233 mout
->dig_out_used
= 0;
5234 mutex_unlock(&codec
->spdif_mutex
);
5237 EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_close
);
5240 * snd_hda_multi_out_analog_open - open analog outputs
5242 * Open analog outputs and set up the hw-constraints.
5243 * If the digital outputs can be opened as slave, open the digital
5246 int snd_hda_multi_out_analog_open(struct hda_codec
*codec
,
5247 struct hda_multi_out
*mout
,
5248 struct snd_pcm_substream
*substream
,
5249 struct hda_pcm_stream
*hinfo
)
5251 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
5252 runtime
->hw
.channels_max
= mout
->max_channels
;
5253 if (mout
->dig_out_nid
) {
5254 if (!mout
->analog_rates
) {
5255 mout
->analog_rates
= hinfo
->rates
;
5256 mout
->analog_formats
= hinfo
->formats
;
5257 mout
->analog_maxbps
= hinfo
->maxbps
;
5259 runtime
->hw
.rates
= mout
->analog_rates
;
5260 runtime
->hw
.formats
= mout
->analog_formats
;
5261 hinfo
->maxbps
= mout
->analog_maxbps
;
5263 if (!mout
->spdif_rates
) {
5264 snd_hda_query_supported_pcm(codec
, mout
->dig_out_nid
,
5266 &mout
->spdif_formats
,
5267 &mout
->spdif_maxbps
);
5269 mutex_lock(&codec
->spdif_mutex
);
5270 if (mout
->share_spdif
) {
5271 if ((runtime
->hw
.rates
& mout
->spdif_rates
) &&
5272 (runtime
->hw
.formats
& mout
->spdif_formats
)) {
5273 runtime
->hw
.rates
&= mout
->spdif_rates
;
5274 runtime
->hw
.formats
&= mout
->spdif_formats
;
5275 if (mout
->spdif_maxbps
< hinfo
->maxbps
)
5276 hinfo
->maxbps
= mout
->spdif_maxbps
;
5278 mout
->share_spdif
= 0;
5279 /* FIXME: need notify? */
5282 mutex_unlock(&codec
->spdif_mutex
);
5284 return snd_pcm_hw_constraint_step(substream
->runtime
, 0,
5285 SNDRV_PCM_HW_PARAM_CHANNELS
, 2);
5287 EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_open
);
5290 * snd_hda_multi_out_analog_prepare - Preapre the analog outputs.
5292 * Set up the i/o for analog out.
5293 * When the digital out is available, copy the front out to digital out, too.
5295 int snd_hda_multi_out_analog_prepare(struct hda_codec
*codec
,
5296 struct hda_multi_out
*mout
,
5297 unsigned int stream_tag
,
5298 unsigned int format
,
5299 struct snd_pcm_substream
*substream
)
5301 const hda_nid_t
*nids
= mout
->dac_nids
;
5302 int chs
= substream
->runtime
->channels
;
5303 struct hda_spdif_out
*spdif
;
5306 mutex_lock(&codec
->spdif_mutex
);
5307 spdif
= snd_hda_spdif_out_of_nid(codec
, mout
->dig_out_nid
);
5308 if (mout
->dig_out_nid
&& mout
->share_spdif
&&
5309 mout
->dig_out_used
!= HDA_DIG_EXCLUSIVE
) {
5311 snd_hda_is_supported_format(codec
, mout
->dig_out_nid
,
5313 !(spdif
->status
& IEC958_AES0_NONAUDIO
)) {
5314 mout
->dig_out_used
= HDA_DIG_ANALOG_DUP
;
5315 setup_dig_out_stream(codec
, mout
->dig_out_nid
,
5316 stream_tag
, format
);
5318 mout
->dig_out_used
= 0;
5319 cleanup_dig_out_stream(codec
, mout
->dig_out_nid
);
5322 mutex_unlock(&codec
->spdif_mutex
);
5325 snd_hda_codec_setup_stream(codec
, nids
[HDA_FRONT
], stream_tag
,
5327 if (!mout
->no_share_stream
&&
5328 mout
->hp_nid
&& mout
->hp_nid
!= nids
[HDA_FRONT
])
5329 /* headphone out will just decode front left/right (stereo) */
5330 snd_hda_codec_setup_stream(codec
, mout
->hp_nid
, stream_tag
,
5332 /* extra outputs copied from front */
5333 for (i
= 0; i
< ARRAY_SIZE(mout
->hp_out_nid
); i
++)
5334 if (!mout
->no_share_stream
&& mout
->hp_out_nid
[i
])
5335 snd_hda_codec_setup_stream(codec
,
5336 mout
->hp_out_nid
[i
],
5337 stream_tag
, 0, format
);
5338 for (i
= 0; i
< ARRAY_SIZE(mout
->extra_out_nid
); i
++)
5339 if (!mout
->no_share_stream
&& mout
->extra_out_nid
[i
])
5340 snd_hda_codec_setup_stream(codec
,
5341 mout
->extra_out_nid
[i
],
5342 stream_tag
, 0, format
);
5345 for (i
= 1; i
< mout
->num_dacs
; i
++) {
5346 if (chs
>= (i
+ 1) * 2) /* independent out */
5347 snd_hda_codec_setup_stream(codec
, nids
[i
], stream_tag
,
5349 else if (!mout
->no_share_stream
) /* copy front */
5350 snd_hda_codec_setup_stream(codec
, nids
[i
], stream_tag
,
5355 EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_prepare
);
5358 * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out
5360 int snd_hda_multi_out_analog_cleanup(struct hda_codec
*codec
,
5361 struct hda_multi_out
*mout
)
5363 const hda_nid_t
*nids
= mout
->dac_nids
;
5366 for (i
= 0; i
< mout
->num_dacs
; i
++)
5367 snd_hda_codec_cleanup_stream(codec
, nids
[i
]);
5369 snd_hda_codec_cleanup_stream(codec
, mout
->hp_nid
);
5370 for (i
= 0; i
< ARRAY_SIZE(mout
->hp_out_nid
); i
++)
5371 if (mout
->hp_out_nid
[i
])
5372 snd_hda_codec_cleanup_stream(codec
,
5373 mout
->hp_out_nid
[i
]);
5374 for (i
= 0; i
< ARRAY_SIZE(mout
->extra_out_nid
); i
++)
5375 if (mout
->extra_out_nid
[i
])
5376 snd_hda_codec_cleanup_stream(codec
,
5377 mout
->extra_out_nid
[i
]);
5378 mutex_lock(&codec
->spdif_mutex
);
5379 if (mout
->dig_out_nid
&& mout
->dig_out_used
== HDA_DIG_ANALOG_DUP
) {
5380 cleanup_dig_out_stream(codec
, mout
->dig_out_nid
);
5381 mout
->dig_out_used
= 0;
5383 mutex_unlock(&codec
->spdif_mutex
);
5386 EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_cleanup
);
5389 * snd_hda_get_default_vref - Get the default (mic) VREF pin bits
5391 * Guess the suitable VREF pin bits to be set as the pin-control value.
5392 * Note: the function doesn't set the AC_PINCTL_IN_EN bit.
5394 unsigned int snd_hda_get_default_vref(struct hda_codec
*codec
, hda_nid_t pin
)
5396 unsigned int pincap
;
5397 unsigned int oldval
;
5398 oldval
= snd_hda_codec_read(codec
, pin
, 0,
5399 AC_VERB_GET_PIN_WIDGET_CONTROL
, 0);
5400 pincap
= snd_hda_query_pin_caps(codec
, pin
);
5401 pincap
= (pincap
& AC_PINCAP_VREF
) >> AC_PINCAP_VREF_SHIFT
;
5402 /* Exception: if the default pin setup is vref50, we give it priority */
5403 if ((pincap
& AC_PINCAP_VREF_80
) && oldval
!= PIN_VREF50
)
5404 return AC_PINCTL_VREF_80
;
5405 else if (pincap
& AC_PINCAP_VREF_50
)
5406 return AC_PINCTL_VREF_50
;
5407 else if (pincap
& AC_PINCAP_VREF_100
)
5408 return AC_PINCTL_VREF_100
;
5409 else if (pincap
& AC_PINCAP_VREF_GRD
)
5410 return AC_PINCTL_VREF_GRD
;
5411 return AC_PINCTL_VREF_HIZ
;
5413 EXPORT_SYMBOL_HDA(snd_hda_get_default_vref
);
5415 /* correct the pin ctl value for matching with the pin cap */
5416 unsigned int snd_hda_correct_pin_ctl(struct hda_codec
*codec
,
5417 hda_nid_t pin
, unsigned int val
)
5419 static unsigned int cap_lists
[][2] = {
5420 { AC_PINCTL_VREF_100
, AC_PINCAP_VREF_100
},
5421 { AC_PINCTL_VREF_80
, AC_PINCAP_VREF_80
},
5422 { AC_PINCTL_VREF_50
, AC_PINCAP_VREF_50
},
5423 { AC_PINCTL_VREF_GRD
, AC_PINCAP_VREF_GRD
},
5429 cap
= snd_hda_query_pin_caps(codec
, pin
);
5431 return val
; /* don't know what to do... */
5433 if (val
& AC_PINCTL_OUT_EN
) {
5434 if (!(cap
& AC_PINCAP_OUT
))
5435 val
&= ~(AC_PINCTL_OUT_EN
| AC_PINCTL_HP_EN
);
5436 else if ((val
& AC_PINCTL_HP_EN
) && !(cap
& AC_PINCAP_HP_DRV
))
5437 val
&= ~AC_PINCTL_HP_EN
;
5440 if (val
& AC_PINCTL_IN_EN
) {
5441 if (!(cap
& AC_PINCAP_IN
))
5442 val
&= ~(AC_PINCTL_IN_EN
| AC_PINCTL_VREFEN
);
5444 unsigned int vcap
, vref
;
5446 vcap
= (cap
& AC_PINCAP_VREF
) >> AC_PINCAP_VREF_SHIFT
;
5447 vref
= val
& AC_PINCTL_VREFEN
;
5448 for (i
= 0; i
< ARRAY_SIZE(cap_lists
); i
++) {
5449 if (vref
== cap_lists
[i
][0] &&
5450 !(vcap
& cap_lists
[i
][1])) {
5451 if (i
== ARRAY_SIZE(cap_lists
) - 1)
5452 vref
= AC_PINCTL_VREF_HIZ
;
5454 vref
= cap_lists
[i
+ 1][0];
5457 val
&= ~AC_PINCTL_VREFEN
;
5464 EXPORT_SYMBOL_HDA(snd_hda_correct_pin_ctl
);
5466 int _snd_hda_set_pin_ctl(struct hda_codec
*codec
, hda_nid_t pin
,
5467 unsigned int val
, bool cached
)
5469 val
= snd_hda_correct_pin_ctl(codec
, pin
, val
);
5470 snd_hda_codec_set_pin_target(codec
, pin
, val
);
5472 return snd_hda_codec_update_cache(codec
, pin
, 0,
5473 AC_VERB_SET_PIN_WIDGET_CONTROL
, val
);
5475 return snd_hda_codec_write(codec
, pin
, 0,
5476 AC_VERB_SET_PIN_WIDGET_CONTROL
, val
);
5478 EXPORT_SYMBOL_HDA(_snd_hda_set_pin_ctl
);
5481 * snd_hda_add_imux_item - Add an item to input_mux
5483 * When the same label is used already in the existing items, the number
5484 * suffix is appended to the label. This label index number is stored
5485 * to type_idx when non-NULL pointer is given.
5487 int snd_hda_add_imux_item(struct hda_input_mux
*imux
, const char *label
,
5488 int index
, int *type_idx
)
5490 int i
, label_idx
= 0;
5491 if (imux
->num_items
>= HDA_MAX_NUM_INPUTS
) {
5492 snd_printd(KERN_ERR
"hda_codec: Too many imux items!\n");
5495 for (i
= 0; i
< imux
->num_items
; i
++) {
5496 if (!strncmp(label
, imux
->items
[i
].label
, strlen(label
)))
5500 *type_idx
= label_idx
;
5502 snprintf(imux
->items
[imux
->num_items
].label
,
5503 sizeof(imux
->items
[imux
->num_items
].label
),
5504 "%s %d", label
, label_idx
);
5506 strlcpy(imux
->items
[imux
->num_items
].label
, label
,
5507 sizeof(imux
->items
[imux
->num_items
].label
));
5508 imux
->items
[imux
->num_items
].index
= index
;
5512 EXPORT_SYMBOL_HDA(snd_hda_add_imux_item
);
5521 * snd_hda_suspend - suspend the codecs
5524 * Returns 0 if successful.
5526 int snd_hda_suspend(struct hda_bus
*bus
)
5528 struct hda_codec
*codec
;
5530 list_for_each_entry(codec
, &bus
->codec_list
, list
) {
5531 cancel_delayed_work_sync(&codec
->jackpoll_work
);
5532 if (hda_codec_is_power_on(codec
))
5533 hda_call_codec_suspend(codec
, false);
5537 EXPORT_SYMBOL_HDA(snd_hda_suspend
);
5540 * snd_hda_resume - resume the codecs
5543 * Returns 0 if successful.
5545 int snd_hda_resume(struct hda_bus
*bus
)
5547 struct hda_codec
*codec
;
5549 list_for_each_entry(codec
, &bus
->codec_list
, list
) {
5550 hda_call_codec_resume(codec
);
5554 EXPORT_SYMBOL_HDA(snd_hda_resume
);
5555 #endif /* CONFIG_PM */
5562 * snd_array_new - get a new element from the given array
5563 * @array: the array object
5565 * Get a new element from the given array. If it exceeds the
5566 * pre-allocated array size, re-allocate the array.
5568 * Returns NULL if allocation failed.
5570 void *snd_array_new(struct snd_array
*array
)
5572 if (snd_BUG_ON(!array
->elem_size
))
5574 if (array
->used
>= array
->alloced
) {
5575 int num
= array
->alloced
+ array
->alloc_align
;
5576 int size
= (num
+ 1) * array
->elem_size
;
5578 if (snd_BUG_ON(num
>= 4096))
5580 nlist
= krealloc(array
->list
, size
, GFP_KERNEL
| __GFP_ZERO
);
5583 array
->list
= nlist
;
5584 array
->alloced
= num
;
5586 return snd_array_elem(array
, array
->used
++);
5588 EXPORT_SYMBOL_HDA(snd_array_new
);
5591 * snd_array_free - free the given array elements
5592 * @array: the array object
5594 void snd_array_free(struct snd_array
*array
)
5601 EXPORT_SYMBOL_HDA(snd_array_free
);
5604 * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer
5605 * @pcm: PCM caps bits
5606 * @buf: the string buffer to write
5607 * @buflen: the max buffer length
5609 * used by hda_proc.c and hda_eld.c
5611 void snd_print_pcm_bits(int pcm
, char *buf
, int buflen
)
5613 static unsigned int bits
[] = { 8, 16, 20, 24, 32 };
5616 for (i
= 0, j
= 0; i
< ARRAY_SIZE(bits
); i
++)
5617 if (pcm
& (AC_SUPPCM_BITS_8
<< i
))
5618 j
+= snprintf(buf
+ j
, buflen
- j
, " %d", bits
[i
]);
5620 buf
[j
] = '\0'; /* necessary when j == 0 */
5622 EXPORT_SYMBOL_HDA(snd_print_pcm_bits
);
5624 MODULE_DESCRIPTION("HDA codec core");
5625 MODULE_LICENSE("GPL");