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
);
336 *start_id
= (parm
>> 16) & 0x7fff;
337 return (int)(parm
& 0x7fff);
339 EXPORT_SYMBOL_HDA(snd_hda_get_sub_nodes
);
341 /* connection list element */
342 struct hda_conn_list
{
343 struct list_head list
;
349 /* look up the cached results */
350 static struct hda_conn_list
*
351 lookup_conn_list(struct hda_codec
*codec
, hda_nid_t nid
)
353 struct hda_conn_list
*p
;
354 list_for_each_entry(p
, &codec
->conn_list
, list
) {
361 static int add_conn_list(struct hda_codec
*codec
, hda_nid_t nid
, int len
,
362 const hda_nid_t
*list
)
364 struct hda_conn_list
*p
;
366 p
= kmalloc(sizeof(*p
) + len
* sizeof(hda_nid_t
), GFP_KERNEL
);
371 memcpy(p
->conns
, list
, len
* sizeof(hda_nid_t
));
372 list_add(&p
->list
, &codec
->conn_list
);
376 static void remove_conn_list(struct hda_codec
*codec
)
378 while (!list_empty(&codec
->conn_list
)) {
379 struct hda_conn_list
*p
;
380 p
= list_first_entry(&codec
->conn_list
, typeof(*p
), list
);
386 /* read the connection and add to the cache */
387 static int read_and_add_raw_conns(struct hda_codec
*codec
, hda_nid_t nid
)
390 hda_nid_t
*result
= list
;
393 len
= snd_hda_get_raw_connections(codec
, nid
, list
, ARRAY_SIZE(list
));
394 if (len
== -ENOSPC
) {
395 len
= snd_hda_get_num_raw_conns(codec
, nid
);
396 result
= kmalloc(sizeof(hda_nid_t
) * len
, GFP_KERNEL
);
399 len
= snd_hda_get_raw_connections(codec
, nid
, result
, len
);
402 len
= snd_hda_override_conn_list(codec
, nid
, len
, result
);
409 * snd_hda_get_conn_list - get connection list
410 * @codec: the HDA codec
412 * @len: number of connection list entries
413 * @listp: the pointer to store NID list
415 * Parses the connection list of the given widget and stores the pointer
416 * to the list of NIDs.
418 * Returns the number of connections, or a negative error code.
420 * Note that the returned pointer isn't protected against the list
421 * modification. If snd_hda_override_conn_list() might be called
422 * concurrently, protect with a mutex appropriately.
424 int snd_hda_get_conn_list(struct hda_codec
*codec
, hda_nid_t nid
,
425 const hda_nid_t
**listp
)
431 const struct hda_conn_list
*p
;
433 /* if the connection-list is already cached, read it */
434 p
= lookup_conn_list(codec
, nid
);
440 if (snd_BUG_ON(added
))
443 err
= read_and_add_raw_conns(codec
, nid
);
449 EXPORT_SYMBOL_HDA(snd_hda_get_conn_list
);
452 * snd_hda_get_connections - copy connection list
453 * @codec: the HDA codec
455 * @conn_list: connection list array; when NULL, checks only the size
456 * @max_conns: max. number of connections to store
458 * Parses the connection list of the given widget and stores the list
461 * Returns the number of connections, or a negative error code.
463 int snd_hda_get_connections(struct hda_codec
*codec
, hda_nid_t nid
,
464 hda_nid_t
*conn_list
, int max_conns
)
466 const hda_nid_t
*list
;
467 int len
= snd_hda_get_conn_list(codec
, nid
, &list
);
469 if (len
> 0 && conn_list
) {
470 if (len
> max_conns
) {
471 snd_printk(KERN_ERR
"hda_codec: "
472 "Too many connections %d for NID 0x%x\n",
476 memcpy(conn_list
, list
, len
* sizeof(hda_nid_t
));
481 EXPORT_SYMBOL_HDA(snd_hda_get_connections
);
483 /* return CONNLIST_LEN parameter of the given widget */
484 static unsigned int get_num_conns(struct hda_codec
*codec
, hda_nid_t nid
)
486 unsigned int wcaps
= get_wcaps(codec
, nid
);
489 if (!(wcaps
& AC_WCAP_CONN_LIST
) &&
490 get_wcaps_type(wcaps
) != AC_WID_VOL_KNB
)
493 parm
= snd_hda_param_read(codec
, nid
, AC_PAR_CONNLIST_LEN
);
499 int snd_hda_get_num_raw_conns(struct hda_codec
*codec
, hda_nid_t nid
)
501 return snd_hda_get_raw_connections(codec
, nid
, NULL
, 0);
505 * snd_hda_get_raw_connections - copy connection list without cache
506 * @codec: the HDA codec
508 * @conn_list: connection list array
509 * @max_conns: max. number of connections to store
511 * Like snd_hda_get_connections(), copy the connection list but without
512 * checking through the connection-list cache.
513 * Currently called only from hda_proc.c, so not exported.
515 int snd_hda_get_raw_connections(struct hda_codec
*codec
, hda_nid_t nid
,
516 hda_nid_t
*conn_list
, int max_conns
)
519 int i
, conn_len
, conns
;
520 unsigned int shift
, num_elems
, mask
;
524 parm
= get_num_conns(codec
, nid
);
528 if (parm
& AC_CLIST_LONG
) {
537 conn_len
= parm
& AC_CLIST_LENGTH
;
538 mask
= (1 << (shift
-1)) - 1;
541 return 0; /* no connection */
544 /* single connection */
545 parm
= snd_hda_codec_read(codec
, nid
, 0,
546 AC_VERB_GET_CONNECT_LIST
, 0);
547 if (parm
== -1 && codec
->bus
->rirb_error
)
550 conn_list
[0] = parm
& mask
;
554 /* multi connection */
557 for (i
= 0; i
< conn_len
; i
++) {
561 if (i
% num_elems
== 0) {
562 parm
= snd_hda_codec_read(codec
, nid
, 0,
563 AC_VERB_GET_CONNECT_LIST
, i
);
564 if (parm
== -1 && codec
->bus
->rirb_error
)
567 range_val
= !!(parm
& (1 << (shift
-1))); /* ranges */
569 if (val
== 0 && null_count
++) { /* no second chance */
570 snd_printk(KERN_WARNING
"hda_codec: "
571 "invalid CONNECT_LIST verb %x[%i]:%x\n",
577 /* ranges between the previous and this one */
578 if (!prev_nid
|| prev_nid
>= val
) {
579 snd_printk(KERN_WARNING
"hda_codec: "
580 "invalid dep_range_val %x:%x\n",
584 for (n
= prev_nid
+ 1; n
<= val
; n
++) {
586 if (conns
>= max_conns
)
588 conn_list
[conns
] = n
;
594 if (conns
>= max_conns
)
596 conn_list
[conns
] = val
;
606 * snd_hda_override_conn_list - add/modify the connection-list to cache
607 * @codec: the HDA codec
609 * @len: number of connection list entries
610 * @list: the list of connection entries
612 * Add or modify the given connection-list to the cache. If the corresponding
613 * cache already exists, invalidate it and append a new one.
615 * Returns zero or a negative error code.
617 int snd_hda_override_conn_list(struct hda_codec
*codec
, hda_nid_t nid
, int len
,
618 const hda_nid_t
*list
)
620 struct hda_conn_list
*p
;
622 p
= lookup_conn_list(codec
, nid
);
628 return add_conn_list(codec
, nid
, len
, list
);
630 EXPORT_SYMBOL_HDA(snd_hda_override_conn_list
);
633 * snd_hda_get_conn_index - get the connection index of the given NID
634 * @codec: the HDA codec
635 * @mux: NID containing the list
636 * @nid: NID to select
637 * @recursive: 1 when searching NID recursively, otherwise 0
639 * Parses the connection list of the widget @mux and checks whether the
640 * widget @nid is present. If it is, return the connection index.
641 * Otherwise it returns -1.
643 int snd_hda_get_conn_index(struct hda_codec
*codec
, hda_nid_t mux
,
644 hda_nid_t nid
, int recursive
)
646 const hda_nid_t
*conn
;
649 nums
= snd_hda_get_conn_list(codec
, mux
, &conn
);
650 for (i
= 0; i
< nums
; i
++)
655 if (recursive
> 10) {
656 snd_printd("hda_codec: too deep connection for 0x%x\n", nid
);
660 for (i
= 0; i
< nums
; i
++) {
661 unsigned int type
= get_wcaps_type(get_wcaps(codec
, conn
[i
]));
662 if (type
== AC_WID_PIN
|| type
== AC_WID_AUD_OUT
)
664 if (snd_hda_get_conn_index(codec
, conn
[i
], nid
, recursive
) >= 0)
669 EXPORT_SYMBOL_HDA(snd_hda_get_conn_index
);
672 /* return DEVLIST_LEN parameter of the given widget */
673 static unsigned int get_num_devices(struct hda_codec
*codec
, hda_nid_t nid
)
675 unsigned int wcaps
= get_wcaps(codec
, nid
);
678 if (!codec
->dp_mst
|| !(wcaps
& AC_WCAP_DIGITAL
) ||
679 get_wcaps_type(wcaps
) != AC_WID_PIN
)
682 parm
= snd_hda_param_read(codec
, nid
, AC_PAR_DEVLIST_LEN
);
683 if (parm
== -1 && codec
->bus
->rirb_error
)
685 return parm
& AC_DEV_LIST_LEN_MASK
;
689 * snd_hda_get_devices - copy device list without cache
690 * @codec: the HDA codec
691 * @nid: NID of the pin to parse
692 * @dev_list: device list array
693 * @max_devices: max. number of devices to store
695 * Copy the device list. This info is dynamic and so not cached.
696 * Currently called only from hda_proc.c, so not exported.
698 int snd_hda_get_devices(struct hda_codec
*codec
, hda_nid_t nid
,
699 u8
*dev_list
, int max_devices
)
702 int i
, dev_len
, devices
;
704 parm
= get_num_devices(codec
, nid
);
705 if (!parm
) /* not multi-stream capable */
709 dev_len
= dev_len
< max_devices
? dev_len
: max_devices
;
712 while (devices
< dev_len
) {
713 parm
= snd_hda_codec_read(codec
, nid
, 0,
714 AC_VERB_GET_DEVICE_LIST
, devices
);
715 if (parm
== -1 && codec
->bus
->rirb_error
)
718 for (i
= 0; i
< 8; i
++) {
719 dev_list
[devices
] = (u8
)parm
;
722 if (devices
>= dev_len
)
730 * snd_hda_queue_unsol_event - add an unsolicited event to queue
732 * @res: unsolicited event (lower 32bit of RIRB entry)
733 * @res_ex: codec addr and flags (upper 32bit or RIRB entry)
735 * Adds the given event to the queue. The events are processed in
736 * the workqueue asynchronously. Call this function in the interrupt
737 * hanlder when RIRB receives an unsolicited event.
739 * Returns 0 if successful, or a negative error code.
741 int snd_hda_queue_unsol_event(struct hda_bus
*bus
, u32 res
, u32 res_ex
)
743 struct hda_bus_unsolicited
*unsol
;
746 if (!bus
|| !bus
->workq
)
749 trace_hda_unsol_event(bus
, res
, res_ex
);
754 wp
= (unsol
->wp
+ 1) % HDA_UNSOL_QUEUE_SIZE
;
758 unsol
->queue
[wp
] = res
;
759 unsol
->queue
[wp
+ 1] = res_ex
;
761 queue_work(bus
->workq
, &unsol
->work
);
765 EXPORT_SYMBOL_HDA(snd_hda_queue_unsol_event
);
768 * process queued unsolicited events
770 static void process_unsol_events(struct work_struct
*work
)
772 struct hda_bus_unsolicited
*unsol
=
773 container_of(work
, struct hda_bus_unsolicited
, work
);
774 struct hda_bus
*bus
= unsol
->bus
;
775 struct hda_codec
*codec
;
776 unsigned int rp
, caddr
, res
;
778 while (unsol
->rp
!= unsol
->wp
) {
779 rp
= (unsol
->rp
+ 1) % HDA_UNSOL_QUEUE_SIZE
;
782 res
= unsol
->queue
[rp
];
783 caddr
= unsol
->queue
[rp
+ 1];
784 if (!(caddr
& (1 << 4))) /* no unsolicited event? */
786 codec
= bus
->caddr_tbl
[caddr
& 0x0f];
787 if (codec
&& codec
->patch_ops
.unsol_event
)
788 codec
->patch_ops
.unsol_event(codec
, res
);
793 * initialize unsolicited queue
795 static int init_unsol_queue(struct hda_bus
*bus
)
797 struct hda_bus_unsolicited
*unsol
;
799 if (bus
->unsol
) /* already initialized */
802 unsol
= kzalloc(sizeof(*unsol
), GFP_KERNEL
);
804 snd_printk(KERN_ERR
"hda_codec: "
805 "can't allocate unsolicited queue\n");
808 INIT_WORK(&unsol
->work
, process_unsol_events
);
817 static void snd_hda_codec_free(struct hda_codec
*codec
);
819 static int snd_hda_bus_free(struct hda_bus
*bus
)
821 struct hda_codec
*codec
, *n
;
826 flush_workqueue(bus
->workq
);
829 list_for_each_entry_safe(codec
, n
, &bus
->codec_list
, list
) {
830 snd_hda_codec_free(codec
);
832 if (bus
->ops
.private_free
)
833 bus
->ops
.private_free(bus
);
835 destroy_workqueue(bus
->workq
);
840 static int snd_hda_bus_dev_free(struct snd_device
*device
)
842 struct hda_bus
*bus
= device
->device_data
;
844 return snd_hda_bus_free(bus
);
847 #ifdef CONFIG_SND_HDA_HWDEP
848 static int snd_hda_bus_dev_register(struct snd_device
*device
)
850 struct hda_bus
*bus
= device
->device_data
;
851 struct hda_codec
*codec
;
852 list_for_each_entry(codec
, &bus
->codec_list
, list
) {
853 snd_hda_hwdep_add_sysfs(codec
);
854 snd_hda_hwdep_add_power_sysfs(codec
);
859 #define snd_hda_bus_dev_register NULL
863 * snd_hda_bus_new - create a HDA bus
864 * @card: the card entry
865 * @temp: the template for hda_bus information
866 * @busp: the pointer to store the created bus instance
868 * Returns 0 if successful, or a negative error code.
870 int snd_hda_bus_new(struct snd_card
*card
,
871 const struct hda_bus_template
*temp
,
872 struct hda_bus
**busp
)
876 static struct snd_device_ops dev_ops
= {
877 .dev_register
= snd_hda_bus_dev_register
,
878 .dev_free
= snd_hda_bus_dev_free
,
881 if (snd_BUG_ON(!temp
))
883 if (snd_BUG_ON(!temp
->ops
.command
|| !temp
->ops
.get_response
))
889 bus
= kzalloc(sizeof(*bus
), GFP_KERNEL
);
891 snd_printk(KERN_ERR
"can't allocate struct hda_bus\n");
896 bus
->private_data
= temp
->private_data
;
897 bus
->pci
= temp
->pci
;
898 bus
->modelname
= temp
->modelname
;
899 bus
->power_save
= temp
->power_save
;
900 bus
->ops
= temp
->ops
;
902 mutex_init(&bus
->cmd_mutex
);
903 mutex_init(&bus
->prepare_mutex
);
904 INIT_LIST_HEAD(&bus
->codec_list
);
906 snprintf(bus
->workq_name
, sizeof(bus
->workq_name
),
907 "hd-audio%d", card
->number
);
908 bus
->workq
= create_singlethread_workqueue(bus
->workq_name
);
910 snd_printk(KERN_ERR
"cannot create workqueue %s\n",
916 err
= snd_device_new(card
, SNDRV_DEV_BUS
, bus
, &dev_ops
);
918 snd_hda_bus_free(bus
);
925 EXPORT_SYMBOL_HDA(snd_hda_bus_new
);
927 #ifdef CONFIG_SND_HDA_GENERIC
928 #define is_generic_config(codec) \
929 (codec->modelname && !strcmp(codec->modelname, "generic"))
931 #define is_generic_config(codec) 0
935 #define HDA_MODREQ_MAX_COUNT 2 /* two request_modules()'s */
937 #define HDA_MODREQ_MAX_COUNT 0 /* all presets are statically linked */
941 * find a matching codec preset
943 static const struct hda_codec_preset
*
944 find_codec_preset(struct hda_codec
*codec
)
946 struct hda_codec_preset_list
*tbl
;
947 const struct hda_codec_preset
*preset
;
948 unsigned int mod_requested
= 0;
950 if (is_generic_config(codec
))
951 return NULL
; /* use the generic parser */
954 mutex_lock(&preset_mutex
);
955 list_for_each_entry(tbl
, &hda_preset_tables
, list
) {
956 if (!try_module_get(tbl
->owner
)) {
957 snd_printk(KERN_ERR
"hda_codec: cannot module_get\n");
960 for (preset
= tbl
->preset
; preset
->id
; preset
++) {
961 u32 mask
= preset
->mask
;
962 if (preset
->afg
&& preset
->afg
!= codec
->afg
)
964 if (preset
->mfg
&& preset
->mfg
!= codec
->mfg
)
968 if (preset
->id
== (codec
->vendor_id
& mask
) &&
970 preset
->rev
== codec
->revision_id
)) {
971 mutex_unlock(&preset_mutex
);
972 codec
->owner
= tbl
->owner
;
976 module_put(tbl
->owner
);
978 mutex_unlock(&preset_mutex
);
980 if (mod_requested
< HDA_MODREQ_MAX_COUNT
) {
983 snprintf(name
, sizeof(name
), "snd-hda-codec-id:%08x",
986 snprintf(name
, sizeof(name
), "snd-hda-codec-id:%04x*",
987 (codec
->vendor_id
>> 16) & 0xffff);
988 request_module(name
);
996 * get_codec_name - store the codec name
998 static int get_codec_name(struct hda_codec
*codec
)
1000 const struct hda_vendor_id
*c
;
1001 const char *vendor
= NULL
;
1002 u16 vendor_id
= codec
->vendor_id
>> 16;
1005 if (codec
->vendor_name
)
1008 for (c
= hda_vendor_ids
; c
->id
; c
++) {
1009 if (c
->id
== vendor_id
) {
1015 sprintf(tmp
, "Generic %04x", vendor_id
);
1018 codec
->vendor_name
= kstrdup(vendor
, GFP_KERNEL
);
1019 if (!codec
->vendor_name
)
1023 if (codec
->chip_name
)
1026 if (codec
->preset
&& codec
->preset
->name
)
1027 codec
->chip_name
= kstrdup(codec
->preset
->name
, GFP_KERNEL
);
1029 sprintf(tmp
, "ID %x", codec
->vendor_id
& 0xffff);
1030 codec
->chip_name
= kstrdup(tmp
, GFP_KERNEL
);
1032 if (!codec
->chip_name
)
1038 * look for an AFG and MFG nodes
1040 static void setup_fg_nodes(struct hda_codec
*codec
)
1042 int i
, total_nodes
, function_id
;
1045 total_nodes
= snd_hda_get_sub_nodes(codec
, AC_NODE_ROOT
, &nid
);
1046 for (i
= 0; i
< total_nodes
; i
++, nid
++) {
1047 function_id
= snd_hda_param_read(codec
, nid
,
1048 AC_PAR_FUNCTION_TYPE
);
1049 switch (function_id
& 0xff) {
1050 case AC_GRP_AUDIO_FUNCTION
:
1052 codec
->afg_function_id
= function_id
& 0xff;
1053 codec
->afg_unsol
= (function_id
>> 8) & 1;
1055 case AC_GRP_MODEM_FUNCTION
:
1057 codec
->mfg_function_id
= function_id
& 0xff;
1058 codec
->mfg_unsol
= (function_id
>> 8) & 1;
1067 * read widget caps for each widget and store in cache
1069 static int read_widget_caps(struct hda_codec
*codec
, hda_nid_t fg_node
)
1074 codec
->num_nodes
= snd_hda_get_sub_nodes(codec
, fg_node
,
1076 codec
->wcaps
= kmalloc(codec
->num_nodes
* 4, GFP_KERNEL
);
1079 nid
= codec
->start_nid
;
1080 for (i
= 0; i
< codec
->num_nodes
; i
++, nid
++)
1081 codec
->wcaps
[i
] = snd_hda_param_read(codec
, nid
,
1082 AC_PAR_AUDIO_WIDGET_CAP
);
1086 /* read all pin default configurations and save codec->init_pins */
1087 static int read_pin_defaults(struct hda_codec
*codec
)
1090 hda_nid_t nid
= codec
->start_nid
;
1092 for (i
= 0; i
< codec
->num_nodes
; i
++, nid
++) {
1093 struct hda_pincfg
*pin
;
1094 unsigned int wcaps
= get_wcaps(codec
, nid
);
1095 unsigned int wid_type
= get_wcaps_type(wcaps
);
1096 if (wid_type
!= AC_WID_PIN
)
1098 pin
= snd_array_new(&codec
->init_pins
);
1102 pin
->cfg
= snd_hda_codec_read(codec
, nid
, 0,
1103 AC_VERB_GET_CONFIG_DEFAULT
, 0);
1104 pin
->ctrl
= snd_hda_codec_read(codec
, nid
, 0,
1105 AC_VERB_GET_PIN_WIDGET_CONTROL
,
1111 /* look up the given pin config list and return the item matching with NID */
1112 static struct hda_pincfg
*look_up_pincfg(struct hda_codec
*codec
,
1113 struct snd_array
*array
,
1117 for (i
= 0; i
< array
->used
; i
++) {
1118 struct hda_pincfg
*pin
= snd_array_elem(array
, i
);
1119 if (pin
->nid
== nid
)
1125 /* set the current pin config value for the given NID.
1126 * the value is cached, and read via snd_hda_codec_get_pincfg()
1128 int snd_hda_add_pincfg(struct hda_codec
*codec
, struct snd_array
*list
,
1129 hda_nid_t nid
, unsigned int cfg
)
1131 struct hda_pincfg
*pin
;
1133 /* the check below may be invalid when pins are added by a fixup
1134 * dynamically (e.g. via snd_hda_codec_update_widgets()), so disabled
1138 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN)
1142 pin
= look_up_pincfg(codec
, list
, nid
);
1144 pin
= snd_array_new(list
);
1154 * snd_hda_codec_set_pincfg - Override a pin default configuration
1155 * @codec: the HDA codec
1156 * @nid: NID to set the pin config
1157 * @cfg: the pin default config value
1159 * Override a pin default configuration value in the cache.
1160 * This value can be read by snd_hda_codec_get_pincfg() in a higher
1161 * priority than the real hardware value.
1163 int snd_hda_codec_set_pincfg(struct hda_codec
*codec
,
1164 hda_nid_t nid
, unsigned int cfg
)
1166 return snd_hda_add_pincfg(codec
, &codec
->driver_pins
, nid
, cfg
);
1168 EXPORT_SYMBOL_HDA(snd_hda_codec_set_pincfg
);
1171 * snd_hda_codec_get_pincfg - Obtain a pin-default configuration
1172 * @codec: the HDA codec
1173 * @nid: NID to get the pin config
1175 * Get the current pin config value of the given pin NID.
1176 * If the pincfg value is cached or overridden via sysfs or driver,
1177 * returns the cached value.
1179 unsigned int snd_hda_codec_get_pincfg(struct hda_codec
*codec
, hda_nid_t nid
)
1181 struct hda_pincfg
*pin
;
1183 #ifdef CONFIG_SND_HDA_HWDEP
1185 unsigned int cfg
= 0;
1186 mutex_lock(&codec
->user_mutex
);
1187 pin
= look_up_pincfg(codec
, &codec
->user_pins
, nid
);
1190 mutex_unlock(&codec
->user_mutex
);
1195 pin
= look_up_pincfg(codec
, &codec
->driver_pins
, nid
);
1198 pin
= look_up_pincfg(codec
, &codec
->init_pins
, nid
);
1203 EXPORT_SYMBOL_HDA(snd_hda_codec_get_pincfg
);
1205 /* remember the current pinctl target value */
1206 int snd_hda_codec_set_pin_target(struct hda_codec
*codec
, hda_nid_t nid
,
1209 struct hda_pincfg
*pin
;
1211 pin
= look_up_pincfg(codec
, &codec
->init_pins
, nid
);
1217 EXPORT_SYMBOL_HDA(snd_hda_codec_set_pin_target
);
1219 /* return the current pinctl target value */
1220 int snd_hda_codec_get_pin_target(struct hda_codec
*codec
, hda_nid_t nid
)
1222 struct hda_pincfg
*pin
;
1224 pin
= look_up_pincfg(codec
, &codec
->init_pins
, nid
);
1229 EXPORT_SYMBOL_HDA(snd_hda_codec_get_pin_target
);
1232 * snd_hda_shutup_pins - Shut up all pins
1233 * @codec: the HDA codec
1235 * Clear all pin controls to shup up before suspend for avoiding click noise.
1236 * The controls aren't cached so that they can be resumed properly.
1238 void snd_hda_shutup_pins(struct hda_codec
*codec
)
1241 /* don't shut up pins when unloading the driver; otherwise it breaks
1242 * the default pin setup at the next load of the driver
1244 if (codec
->bus
->shutdown
)
1246 for (i
= 0; i
< codec
->init_pins
.used
; i
++) {
1247 struct hda_pincfg
*pin
= snd_array_elem(&codec
->init_pins
, i
);
1248 /* use read here for syncing after issuing each verb */
1249 snd_hda_codec_read(codec
, pin
->nid
, 0,
1250 AC_VERB_SET_PIN_WIDGET_CONTROL
, 0);
1252 codec
->pins_shutup
= 1;
1254 EXPORT_SYMBOL_HDA(snd_hda_shutup_pins
);
1257 /* Restore the pin controls cleared previously via snd_hda_shutup_pins() */
1258 static void restore_shutup_pins(struct hda_codec
*codec
)
1261 if (!codec
->pins_shutup
)
1263 if (codec
->bus
->shutdown
)
1265 for (i
= 0; i
< codec
->init_pins
.used
; i
++) {
1266 struct hda_pincfg
*pin
= snd_array_elem(&codec
->init_pins
, i
);
1267 snd_hda_codec_write(codec
, pin
->nid
, 0,
1268 AC_VERB_SET_PIN_WIDGET_CONTROL
,
1271 codec
->pins_shutup
= 0;
1275 static void hda_jackpoll_work(struct work_struct
*work
)
1277 struct hda_codec
*codec
=
1278 container_of(work
, struct hda_codec
, jackpoll_work
.work
);
1280 snd_hda_jack_set_dirty_all(codec
);
1281 snd_hda_jack_poll_all(codec
);
1283 if (!codec
->jackpoll_interval
)
1286 queue_delayed_work(codec
->bus
->workq
, &codec
->jackpoll_work
,
1287 codec
->jackpoll_interval
);
1290 static void init_hda_cache(struct hda_cache_rec
*cache
,
1291 unsigned int record_size
);
1292 static void free_hda_cache(struct hda_cache_rec
*cache
);
1294 /* release all pincfg lists */
1295 static void free_init_pincfgs(struct hda_codec
*codec
)
1297 snd_array_free(&codec
->driver_pins
);
1298 #ifdef CONFIG_SND_HDA_HWDEP
1299 snd_array_free(&codec
->user_pins
);
1301 snd_array_free(&codec
->init_pins
);
1305 * audio-converter setup caches
1307 struct hda_cvt_setup
{
1312 unsigned char active
; /* cvt is currently used */
1313 unsigned char dirty
; /* setups should be cleared */
1316 /* get or create a cache entry for the given audio converter NID */
1317 static struct hda_cvt_setup
*
1318 get_hda_cvt_setup(struct hda_codec
*codec
, hda_nid_t nid
)
1320 struct hda_cvt_setup
*p
;
1323 for (i
= 0; i
< codec
->cvt_setups
.used
; i
++) {
1324 p
= snd_array_elem(&codec
->cvt_setups
, i
);
1328 p
= snd_array_new(&codec
->cvt_setups
);
1337 static void snd_hda_codec_free(struct hda_codec
*codec
)
1341 cancel_delayed_work_sync(&codec
->jackpoll_work
);
1342 snd_hda_jack_tbl_clear(codec
);
1343 free_init_pincfgs(codec
);
1345 cancel_delayed_work(&codec
->power_work
);
1346 flush_workqueue(codec
->bus
->workq
);
1348 list_del(&codec
->list
);
1349 snd_array_free(&codec
->mixers
);
1350 snd_array_free(&codec
->nids
);
1351 snd_array_free(&codec
->cvt_setups
);
1352 snd_array_free(&codec
->spdif_out
);
1353 remove_conn_list(codec
);
1354 codec
->bus
->caddr_tbl
[codec
->addr
] = NULL
;
1355 if (codec
->patch_ops
.free
)
1356 codec
->patch_ops
.free(codec
);
1358 if (!codec
->pm_down_notified
) /* cancel leftover refcounts */
1359 hda_call_pm_notify(codec
->bus
, false);
1361 module_put(codec
->owner
);
1362 free_hda_cache(&codec
->amp_cache
);
1363 free_hda_cache(&codec
->cmd_cache
);
1364 kfree(codec
->vendor_name
);
1365 kfree(codec
->chip_name
);
1366 kfree(codec
->modelname
);
1367 kfree(codec
->wcaps
);
1371 static bool snd_hda_codec_get_supported_ps(struct hda_codec
*codec
,
1372 hda_nid_t fg
, unsigned int power_state
);
1374 static unsigned int hda_set_power_state(struct hda_codec
*codec
,
1375 unsigned int power_state
);
1378 * snd_hda_codec_new - create a HDA codec
1379 * @bus: the bus to assign
1380 * @codec_addr: the codec address
1381 * @codecp: the pointer to store the generated codec
1383 * Returns 0 if successful, or a negative error code.
1385 int snd_hda_codec_new(struct hda_bus
*bus
,
1386 unsigned int codec_addr
,
1387 struct hda_codec
**codecp
)
1389 struct hda_codec
*codec
;
1394 if (snd_BUG_ON(!bus
))
1396 if (snd_BUG_ON(codec_addr
> HDA_MAX_CODEC_ADDRESS
))
1399 if (bus
->caddr_tbl
[codec_addr
]) {
1400 snd_printk(KERN_ERR
"hda_codec: "
1401 "address 0x%x is already occupied\n", codec_addr
);
1405 codec
= kzalloc(sizeof(*codec
), GFP_KERNEL
);
1406 if (codec
== NULL
) {
1407 snd_printk(KERN_ERR
"can't allocate struct hda_codec\n");
1412 codec
->addr
= codec_addr
;
1413 mutex_init(&codec
->spdif_mutex
);
1414 mutex_init(&codec
->control_mutex
);
1415 mutex_init(&codec
->hash_mutex
);
1416 init_hda_cache(&codec
->amp_cache
, sizeof(struct hda_amp_info
));
1417 init_hda_cache(&codec
->cmd_cache
, sizeof(struct hda_cache_head
));
1418 snd_array_init(&codec
->mixers
, sizeof(struct hda_nid_item
), 32);
1419 snd_array_init(&codec
->nids
, sizeof(struct hda_nid_item
), 32);
1420 snd_array_init(&codec
->init_pins
, sizeof(struct hda_pincfg
), 16);
1421 snd_array_init(&codec
->driver_pins
, sizeof(struct hda_pincfg
), 16);
1422 snd_array_init(&codec
->cvt_setups
, sizeof(struct hda_cvt_setup
), 8);
1423 snd_array_init(&codec
->spdif_out
, sizeof(struct hda_spdif_out
), 16);
1424 snd_array_init(&codec
->jacktbl
, sizeof(struct hda_jack_tbl
), 16);
1425 snd_array_init(&codec
->verbs
, sizeof(struct hda_verb
*), 8);
1426 INIT_LIST_HEAD(&codec
->conn_list
);
1428 INIT_DELAYED_WORK(&codec
->jackpoll_work
, hda_jackpoll_work
);
1431 spin_lock_init(&codec
->power_lock
);
1432 INIT_DELAYED_WORK(&codec
->power_work
, hda_power_work
);
1433 /* snd_hda_codec_new() marks the codec as power-up, and leave it as is.
1434 * the caller has to power down appropriatley after initialization
1437 hda_keep_power_on(codec
);
1438 hda_call_pm_notify(bus
, true);
1441 if (codec
->bus
->modelname
) {
1442 codec
->modelname
= kstrdup(codec
->bus
->modelname
, GFP_KERNEL
);
1443 if (!codec
->modelname
) {
1444 snd_hda_codec_free(codec
);
1449 list_add_tail(&codec
->list
, &bus
->codec_list
);
1450 bus
->caddr_tbl
[codec_addr
] = codec
;
1452 codec
->vendor_id
= snd_hda_param_read(codec
, AC_NODE_ROOT
,
1454 if (codec
->vendor_id
== -1)
1455 /* read again, hopefully the access method was corrected
1456 * in the last read...
1458 codec
->vendor_id
= snd_hda_param_read(codec
, AC_NODE_ROOT
,
1460 codec
->subsystem_id
= snd_hda_param_read(codec
, AC_NODE_ROOT
,
1461 AC_PAR_SUBSYSTEM_ID
);
1462 codec
->revision_id
= snd_hda_param_read(codec
, AC_NODE_ROOT
,
1465 setup_fg_nodes(codec
);
1466 if (!codec
->afg
&& !codec
->mfg
) {
1467 snd_printdd("hda_codec: no AFG or MFG node found\n");
1472 fg
= codec
->afg
? codec
->afg
: codec
->mfg
;
1473 err
= read_widget_caps(codec
, fg
);
1475 snd_printk(KERN_ERR
"hda_codec: cannot malloc\n");
1478 err
= read_pin_defaults(codec
);
1482 if (!codec
->subsystem_id
) {
1483 codec
->subsystem_id
=
1484 snd_hda_codec_read(codec
, fg
, 0,
1485 AC_VERB_GET_SUBSYSTEM_ID
, 0);
1489 codec
->d3_stop_clk
= snd_hda_codec_get_supported_ps(codec
, fg
,
1491 if (!codec
->d3_stop_clk
)
1492 bus
->power_keep_link_on
= 1;
1494 codec
->epss
= snd_hda_codec_get_supported_ps(codec
, fg
,
1497 /* power-up all before initialization */
1498 hda_set_power_state(codec
, AC_PWRST_D0
);
1500 snd_hda_codec_proc_new(codec
);
1502 snd_hda_create_hwdep(codec
);
1504 sprintf(component
, "HDA:%08x,%08x,%08x", codec
->vendor_id
,
1505 codec
->subsystem_id
, codec
->revision_id
);
1506 snd_component_add(codec
->bus
->card
, component
);
1513 snd_hda_codec_free(codec
);
1516 EXPORT_SYMBOL_HDA(snd_hda_codec_new
);
1518 int snd_hda_codec_update_widgets(struct hda_codec
*codec
)
1523 /* Assume the function group node does not change,
1524 * only the widget nodes may change.
1526 kfree(codec
->wcaps
);
1527 fg
= codec
->afg
? codec
->afg
: codec
->mfg
;
1528 err
= read_widget_caps(codec
, fg
);
1530 snd_printk(KERN_ERR
"hda_codec: cannot malloc\n");
1534 snd_array_free(&codec
->init_pins
);
1535 err
= read_pin_defaults(codec
);
1539 EXPORT_SYMBOL_HDA(snd_hda_codec_update_widgets
);
1543 * snd_hda_codec_configure - (Re-)configure the HD-audio codec
1544 * @codec: the HDA codec
1546 * Start parsing of the given codec tree and (re-)initialize the whole
1549 * Returns 0 if successful or a negative error code.
1551 int snd_hda_codec_configure(struct hda_codec
*codec
)
1555 codec
->preset
= find_codec_preset(codec
);
1556 if (!codec
->vendor_name
|| !codec
->chip_name
) {
1557 err
= get_codec_name(codec
);
1562 if (is_generic_config(codec
)) {
1563 err
= snd_hda_parse_generic_codec(codec
);
1566 if (codec
->preset
&& codec
->preset
->patch
) {
1567 err
= codec
->preset
->patch(codec
);
1571 /* call the default parser */
1572 err
= snd_hda_parse_generic_codec(codec
);
1574 printk(KERN_ERR
"hda-codec: No codec parser is available\n");
1577 if (!err
&& codec
->patch_ops
.unsol_event
)
1578 err
= init_unsol_queue(codec
->bus
);
1579 /* audio codec should override the mixer name */
1580 if (!err
&& (codec
->afg
|| !*codec
->bus
->card
->mixername
))
1581 snprintf(codec
->bus
->card
->mixername
,
1582 sizeof(codec
->bus
->card
->mixername
),
1583 "%s %s", codec
->vendor_name
, codec
->chip_name
);
1586 EXPORT_SYMBOL_HDA(snd_hda_codec_configure
);
1588 /* update the stream-id if changed */
1589 static void update_pcm_stream_id(struct hda_codec
*codec
,
1590 struct hda_cvt_setup
*p
, hda_nid_t nid
,
1591 u32 stream_tag
, int channel_id
)
1593 unsigned int oldval
, newval
;
1595 if (p
->stream_tag
!= stream_tag
|| p
->channel_id
!= channel_id
) {
1596 oldval
= snd_hda_codec_read(codec
, nid
, 0, AC_VERB_GET_CONV
, 0);
1597 newval
= (stream_tag
<< 4) | channel_id
;
1598 if (oldval
!= newval
)
1599 snd_hda_codec_write(codec
, nid
, 0,
1600 AC_VERB_SET_CHANNEL_STREAMID
,
1602 p
->stream_tag
= stream_tag
;
1603 p
->channel_id
= channel_id
;
1607 /* update the format-id if changed */
1608 static void update_pcm_format(struct hda_codec
*codec
, struct hda_cvt_setup
*p
,
1609 hda_nid_t nid
, int format
)
1611 unsigned int oldval
;
1613 if (p
->format_id
!= format
) {
1614 oldval
= snd_hda_codec_read(codec
, nid
, 0,
1615 AC_VERB_GET_STREAM_FORMAT
, 0);
1616 if (oldval
!= format
) {
1618 snd_hda_codec_write(codec
, nid
, 0,
1619 AC_VERB_SET_STREAM_FORMAT
,
1622 p
->format_id
= format
;
1627 * snd_hda_codec_setup_stream - set up the codec for streaming
1628 * @codec: the CODEC to set up
1629 * @nid: the NID to set up
1630 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
1631 * @channel_id: channel id to pass, zero based.
1632 * @format: stream format.
1634 void snd_hda_codec_setup_stream(struct hda_codec
*codec
, hda_nid_t nid
,
1636 int channel_id
, int format
)
1638 struct hda_codec
*c
;
1639 struct hda_cvt_setup
*p
;
1646 snd_printdd("hda_codec_setup_stream: "
1647 "NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
1648 nid
, stream_tag
, channel_id
, format
);
1649 p
= get_hda_cvt_setup(codec
, nid
);
1653 if (codec
->pcm_format_first
)
1654 update_pcm_format(codec
, p
, nid
, format
);
1655 update_pcm_stream_id(codec
, p
, nid
, stream_tag
, channel_id
);
1656 if (!codec
->pcm_format_first
)
1657 update_pcm_format(codec
, p
, nid
, format
);
1662 /* make other inactive cvts with the same stream-tag dirty */
1663 type
= get_wcaps_type(get_wcaps(codec
, nid
));
1664 list_for_each_entry(c
, &codec
->bus
->codec_list
, list
) {
1665 for (i
= 0; i
< c
->cvt_setups
.used
; i
++) {
1666 p
= snd_array_elem(&c
->cvt_setups
, i
);
1667 if (!p
->active
&& p
->stream_tag
== stream_tag
&&
1668 get_wcaps_type(get_wcaps(c
, p
->nid
)) == type
)
1673 EXPORT_SYMBOL_HDA(snd_hda_codec_setup_stream
);
1675 static void really_cleanup_stream(struct hda_codec
*codec
,
1676 struct hda_cvt_setup
*q
);
1679 * __snd_hda_codec_cleanup_stream - clean up the codec for closing
1680 * @codec: the CODEC to clean up
1681 * @nid: the NID to clean up
1682 * @do_now: really clean up the stream instead of clearing the active flag
1684 void __snd_hda_codec_cleanup_stream(struct hda_codec
*codec
, hda_nid_t nid
,
1687 struct hda_cvt_setup
*p
;
1692 if (codec
->no_sticky_stream
)
1695 snd_printdd("hda_codec_cleanup_stream: NID=0x%x\n", nid
);
1696 p
= get_hda_cvt_setup(codec
, nid
);
1698 /* here we just clear the active flag when do_now isn't set;
1699 * actual clean-ups will be done later in
1700 * purify_inactive_streams() called from snd_hda_codec_prpapre()
1703 really_cleanup_stream(codec
, p
);
1708 EXPORT_SYMBOL_HDA(__snd_hda_codec_cleanup_stream
);
1710 static void really_cleanup_stream(struct hda_codec
*codec
,
1711 struct hda_cvt_setup
*q
)
1713 hda_nid_t nid
= q
->nid
;
1714 if (q
->stream_tag
|| q
->channel_id
)
1715 snd_hda_codec_write(codec
, nid
, 0, AC_VERB_SET_CHANNEL_STREAMID
, 0);
1717 snd_hda_codec_write(codec
, nid
, 0, AC_VERB_SET_STREAM_FORMAT
, 0
1719 memset(q
, 0, sizeof(*q
));
1723 /* clean up the all conflicting obsolete streams */
1724 static void purify_inactive_streams(struct hda_codec
*codec
)
1726 struct hda_codec
*c
;
1729 list_for_each_entry(c
, &codec
->bus
->codec_list
, list
) {
1730 for (i
= 0; i
< c
->cvt_setups
.used
; i
++) {
1731 struct hda_cvt_setup
*p
;
1732 p
= snd_array_elem(&c
->cvt_setups
, i
);
1734 really_cleanup_stream(c
, p
);
1740 /* clean up all streams; called from suspend */
1741 static void hda_cleanup_all_streams(struct hda_codec
*codec
)
1745 for (i
= 0; i
< codec
->cvt_setups
.used
; i
++) {
1746 struct hda_cvt_setup
*p
= snd_array_elem(&codec
->cvt_setups
, i
);
1748 really_cleanup_stream(codec
, p
);
1754 * amp access functions
1757 /* FIXME: more better hash key? */
1758 #define HDA_HASH_KEY(nid, dir, idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
1759 #define HDA_HASH_PINCAP_KEY(nid) (u32)((nid) + (0x02 << 24))
1760 #define HDA_HASH_PARPCM_KEY(nid) (u32)((nid) + (0x03 << 24))
1761 #define HDA_HASH_PARSTR_KEY(nid) (u32)((nid) + (0x04 << 24))
1762 #define INFO_AMP_CAPS (1<<0)
1763 #define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
1765 /* initialize the hash table */
1766 static void init_hda_cache(struct hda_cache_rec
*cache
,
1767 unsigned int record_size
)
1769 memset(cache
, 0, sizeof(*cache
));
1770 memset(cache
->hash
, 0xff, sizeof(cache
->hash
));
1771 snd_array_init(&cache
->buf
, record_size
, 64);
1774 static void free_hda_cache(struct hda_cache_rec
*cache
)
1776 snd_array_free(&cache
->buf
);
1779 /* query the hash. allocate an entry if not found. */
1780 static struct hda_cache_head
*get_hash(struct hda_cache_rec
*cache
, u32 key
)
1782 u16 idx
= key
% (u16
)ARRAY_SIZE(cache
->hash
);
1783 u16 cur
= cache
->hash
[idx
];
1784 struct hda_cache_head
*info
;
1786 while (cur
!= 0xffff) {
1787 info
= snd_array_elem(&cache
->buf
, cur
);
1788 if (info
->key
== key
)
1795 /* query the hash. allocate an entry if not found. */
1796 static struct hda_cache_head
*get_alloc_hash(struct hda_cache_rec
*cache
,
1799 struct hda_cache_head
*info
= get_hash(cache
, key
);
1802 /* add a new hash entry */
1803 info
= snd_array_new(&cache
->buf
);
1806 cur
= snd_array_index(&cache
->buf
, info
);
1810 idx
= key
% (u16
)ARRAY_SIZE(cache
->hash
);
1811 info
->next
= cache
->hash
[idx
];
1812 cache
->hash
[idx
] = cur
;
1817 /* query and allocate an amp hash entry */
1818 static inline struct hda_amp_info
*
1819 get_alloc_amp_hash(struct hda_codec
*codec
, u32 key
)
1821 return (struct hda_amp_info
*)get_alloc_hash(&codec
->amp_cache
, key
);
1824 /* overwrite the value with the key in the caps hash */
1825 static int write_caps_hash(struct hda_codec
*codec
, u32 key
, unsigned int val
)
1827 struct hda_amp_info
*info
;
1829 mutex_lock(&codec
->hash_mutex
);
1830 info
= get_alloc_amp_hash(codec
, key
);
1832 mutex_unlock(&codec
->hash_mutex
);
1835 info
->amp_caps
= val
;
1836 info
->head
.val
|= INFO_AMP_CAPS
;
1837 mutex_unlock(&codec
->hash_mutex
);
1841 /* query the value from the caps hash; if not found, fetch the current
1842 * value from the given function and store in the hash
1845 query_caps_hash(struct hda_codec
*codec
, hda_nid_t nid
, int dir
, u32 key
,
1846 unsigned int (*func
)(struct hda_codec
*, hda_nid_t
, int))
1848 struct hda_amp_info
*info
;
1851 mutex_lock(&codec
->hash_mutex
);
1852 info
= get_alloc_amp_hash(codec
, key
);
1854 mutex_unlock(&codec
->hash_mutex
);
1857 if (!(info
->head
.val
& INFO_AMP_CAPS
)) {
1858 mutex_unlock(&codec
->hash_mutex
); /* for reentrance */
1859 val
= func(codec
, nid
, dir
);
1860 write_caps_hash(codec
, key
, val
);
1862 val
= info
->amp_caps
;
1863 mutex_unlock(&codec
->hash_mutex
);
1868 static unsigned int read_amp_cap(struct hda_codec
*codec
, hda_nid_t nid
,
1871 if (!(get_wcaps(codec
, nid
) & AC_WCAP_AMP_OVRD
))
1873 return snd_hda_param_read(codec
, nid
,
1874 direction
== HDA_OUTPUT
?
1875 AC_PAR_AMP_OUT_CAP
: AC_PAR_AMP_IN_CAP
);
1879 * query_amp_caps - query AMP capabilities
1880 * @codec: the HD-auio codec
1881 * @nid: the NID to query
1882 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1884 * Query AMP capabilities for the given widget and direction.
1885 * Returns the obtained capability bits.
1887 * When cap bits have been already read, this doesn't read again but
1888 * returns the cached value.
1890 u32
query_amp_caps(struct hda_codec
*codec
, hda_nid_t nid
, int direction
)
1892 return query_caps_hash(codec
, nid
, direction
,
1893 HDA_HASH_KEY(nid
, direction
, 0),
1896 EXPORT_SYMBOL_HDA(query_amp_caps
);
1899 * snd_hda_override_amp_caps - Override the AMP capabilities
1900 * @codec: the CODEC to clean up
1901 * @nid: the NID to clean up
1902 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1903 * @caps: the capability bits to set
1905 * Override the cached AMP caps bits value by the given one.
1906 * This function is useful if the driver needs to adjust the AMP ranges,
1907 * e.g. limit to 0dB, etc.
1909 * Returns zero if successful or a negative error code.
1911 int snd_hda_override_amp_caps(struct hda_codec
*codec
, hda_nid_t nid
, int dir
,
1914 return write_caps_hash(codec
, HDA_HASH_KEY(nid
, dir
, 0), caps
);
1916 EXPORT_SYMBOL_HDA(snd_hda_override_amp_caps
);
1918 static unsigned int read_pin_cap(struct hda_codec
*codec
, hda_nid_t nid
,
1921 return snd_hda_param_read(codec
, nid
, AC_PAR_PIN_CAP
);
1925 * snd_hda_query_pin_caps - Query PIN capabilities
1926 * @codec: the HD-auio codec
1927 * @nid: the NID to query
1929 * Query PIN capabilities for the given widget.
1930 * Returns the obtained capability bits.
1932 * When cap bits have been already read, this doesn't read again but
1933 * returns the cached value.
1935 u32
snd_hda_query_pin_caps(struct hda_codec
*codec
, hda_nid_t nid
)
1937 return query_caps_hash(codec
, nid
, 0, HDA_HASH_PINCAP_KEY(nid
),
1940 EXPORT_SYMBOL_HDA(snd_hda_query_pin_caps
);
1943 * snd_hda_override_pin_caps - Override the pin capabilities
1945 * @nid: the NID to override
1946 * @caps: the capability bits to set
1948 * Override the cached PIN capabilitiy bits value by the given one.
1950 * Returns zero if successful or a negative error code.
1952 int snd_hda_override_pin_caps(struct hda_codec
*codec
, hda_nid_t nid
,
1955 return write_caps_hash(codec
, HDA_HASH_PINCAP_KEY(nid
), caps
);
1957 EXPORT_SYMBOL_HDA(snd_hda_override_pin_caps
);
1959 /* read or sync the hash value with the current value;
1960 * call within hash_mutex
1962 static struct hda_amp_info
*
1963 update_amp_hash(struct hda_codec
*codec
, hda_nid_t nid
, int ch
,
1964 int direction
, int index
, bool init_only
)
1966 struct hda_amp_info
*info
;
1967 unsigned int parm
, val
= 0;
1968 bool val_read
= false;
1971 info
= get_alloc_amp_hash(codec
, HDA_HASH_KEY(nid
, direction
, index
));
1974 if (!(info
->head
.val
& INFO_AMP_VOL(ch
))) {
1976 mutex_unlock(&codec
->hash_mutex
);
1977 parm
= ch
? AC_AMP_GET_RIGHT
: AC_AMP_GET_LEFT
;
1978 parm
|= direction
== HDA_OUTPUT
?
1979 AC_AMP_GET_OUTPUT
: AC_AMP_GET_INPUT
;
1981 val
= snd_hda_codec_read(codec
, nid
, 0,
1982 AC_VERB_GET_AMP_GAIN_MUTE
, parm
);
1985 mutex_lock(&codec
->hash_mutex
);
1988 info
->vol
[ch
] = val
;
1989 info
->head
.val
|= INFO_AMP_VOL(ch
);
1990 } else if (init_only
)
1996 * write the current volume in info to the h/w
1998 static void put_vol_mute(struct hda_codec
*codec
, unsigned int amp_caps
,
1999 hda_nid_t nid
, int ch
, int direction
, int index
,
2004 parm
= ch
? AC_AMP_SET_RIGHT
: AC_AMP_SET_LEFT
;
2005 parm
|= direction
== HDA_OUTPUT
? AC_AMP_SET_OUTPUT
: AC_AMP_SET_INPUT
;
2006 parm
|= index
<< AC_AMP_SET_INDEX_SHIFT
;
2007 if ((val
& HDA_AMP_MUTE
) && !(amp_caps
& AC_AMPCAP_MUTE
) &&
2008 (amp_caps
& AC_AMPCAP_MIN_MUTE
))
2009 ; /* set the zero value as a fake mute */
2012 snd_hda_codec_write(codec
, nid
, 0, AC_VERB_SET_AMP_GAIN_MUTE
, parm
);
2016 * snd_hda_codec_amp_read - Read AMP value
2017 * @codec: HD-audio codec
2018 * @nid: NID to read the AMP value
2019 * @ch: channel (left=0 or right=1)
2020 * @direction: #HDA_INPUT or #HDA_OUTPUT
2021 * @index: the index value (only for input direction)
2023 * Read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
2025 int snd_hda_codec_amp_read(struct hda_codec
*codec
, hda_nid_t nid
, int ch
,
2026 int direction
, int index
)
2028 struct hda_amp_info
*info
;
2029 unsigned int val
= 0;
2031 mutex_lock(&codec
->hash_mutex
);
2032 info
= update_amp_hash(codec
, nid
, ch
, direction
, index
, false);
2034 val
= info
->vol
[ch
];
2035 mutex_unlock(&codec
->hash_mutex
);
2038 EXPORT_SYMBOL_HDA(snd_hda_codec_amp_read
);
2040 static int codec_amp_update(struct hda_codec
*codec
, hda_nid_t nid
, int ch
,
2041 int direction
, int idx
, int mask
, int val
,
2044 struct hda_amp_info
*info
;
2046 unsigned int cache_only
;
2048 if (snd_BUG_ON(mask
& ~0xff))
2052 mutex_lock(&codec
->hash_mutex
);
2053 info
= update_amp_hash(codec
, nid
, ch
, direction
, idx
, init_only
);
2055 mutex_unlock(&codec
->hash_mutex
);
2058 val
|= info
->vol
[ch
] & ~mask
;
2059 if (info
->vol
[ch
] == val
) {
2060 mutex_unlock(&codec
->hash_mutex
);
2063 info
->vol
[ch
] = val
;
2064 cache_only
= info
->head
.dirty
= codec
->cached_write
;
2065 caps
= info
->amp_caps
;
2066 mutex_unlock(&codec
->hash_mutex
);
2068 put_vol_mute(codec
, caps
, nid
, ch
, direction
, idx
, val
);
2073 * snd_hda_codec_amp_update - update the AMP value
2074 * @codec: HD-audio codec
2075 * @nid: NID to read the AMP value
2076 * @ch: channel (left=0 or right=1)
2077 * @direction: #HDA_INPUT or #HDA_OUTPUT
2078 * @idx: the index value (only for input direction)
2079 * @mask: bit mask to set
2080 * @val: the bits value to set
2082 * Update the AMP value with a bit mask.
2083 * Returns 0 if the value is unchanged, 1 if changed.
2085 int snd_hda_codec_amp_update(struct hda_codec
*codec
, hda_nid_t nid
, int ch
,
2086 int direction
, int idx
, int mask
, int val
)
2088 return codec_amp_update(codec
, nid
, ch
, direction
, idx
, mask
, val
, false);
2090 EXPORT_SYMBOL_HDA(snd_hda_codec_amp_update
);
2093 * snd_hda_codec_amp_stereo - update the AMP stereo values
2094 * @codec: HD-audio codec
2095 * @nid: NID to read the AMP value
2096 * @direction: #HDA_INPUT or #HDA_OUTPUT
2097 * @idx: the index value (only for input direction)
2098 * @mask: bit mask to set
2099 * @val: the bits value to set
2101 * Update the AMP values like snd_hda_codec_amp_update(), but for a
2102 * stereo widget with the same mask and value.
2104 int snd_hda_codec_amp_stereo(struct hda_codec
*codec
, hda_nid_t nid
,
2105 int direction
, int idx
, int mask
, int val
)
2109 if (snd_BUG_ON(mask
& ~0xff))
2111 for (ch
= 0; ch
< 2; ch
++)
2112 ret
|= snd_hda_codec_amp_update(codec
, nid
, ch
, direction
,
2116 EXPORT_SYMBOL_HDA(snd_hda_codec_amp_stereo
);
2118 /* Works like snd_hda_codec_amp_update() but it writes the value only at
2119 * the first access. If the amp was already initialized / updated beforehand,
2120 * this does nothing.
2122 int snd_hda_codec_amp_init(struct hda_codec
*codec
, hda_nid_t nid
, int ch
,
2123 int dir
, int idx
, int mask
, int val
)
2125 return codec_amp_update(codec
, nid
, ch
, dir
, idx
, mask
, val
, true);
2127 EXPORT_SYMBOL_HDA(snd_hda_codec_amp_init
);
2129 int snd_hda_codec_amp_init_stereo(struct hda_codec
*codec
, hda_nid_t nid
,
2130 int dir
, int idx
, int mask
, int val
)
2134 if (snd_BUG_ON(mask
& ~0xff))
2136 for (ch
= 0; ch
< 2; ch
++)
2137 ret
|= snd_hda_codec_amp_init(codec
, nid
, ch
, dir
,
2141 EXPORT_SYMBOL_HDA(snd_hda_codec_amp_init_stereo
);
2144 * snd_hda_codec_resume_amp - Resume all AMP commands from the cache
2145 * @codec: HD-audio codec
2147 * Resume the all amp commands from the cache.
2149 void snd_hda_codec_resume_amp(struct hda_codec
*codec
)
2153 mutex_lock(&codec
->hash_mutex
);
2154 codec
->cached_write
= 0;
2155 for (i
= 0; i
< codec
->amp_cache
.buf
.used
; i
++) {
2156 struct hda_amp_info
*buffer
;
2159 unsigned int idx
, dir
, ch
;
2160 struct hda_amp_info info
;
2162 buffer
= snd_array_elem(&codec
->amp_cache
.buf
, i
);
2163 if (!buffer
->head
.dirty
)
2165 buffer
->head
.dirty
= 0;
2167 key
= info
.head
.key
;
2171 idx
= (key
>> 16) & 0xff;
2172 dir
= (key
>> 24) & 0xff;
2173 for (ch
= 0; ch
< 2; ch
++) {
2174 if (!(info
.head
.val
& INFO_AMP_VOL(ch
)))
2176 mutex_unlock(&codec
->hash_mutex
);
2177 put_vol_mute(codec
, info
.amp_caps
, nid
, ch
, dir
, idx
,
2179 mutex_lock(&codec
->hash_mutex
);
2182 mutex_unlock(&codec
->hash_mutex
);
2184 EXPORT_SYMBOL_HDA(snd_hda_codec_resume_amp
);
2186 static u32
get_amp_max_value(struct hda_codec
*codec
, hda_nid_t nid
, int dir
,
2189 u32 caps
= query_amp_caps(codec
, nid
, dir
);
2191 caps
= (caps
& AC_AMPCAP_NUM_STEPS
) >> AC_AMPCAP_NUM_STEPS_SHIFT
;
2198 * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer
2200 * The control element is supposed to have the private_value field
2201 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2203 int snd_hda_mixer_amp_volume_info(struct snd_kcontrol
*kcontrol
,
2204 struct snd_ctl_elem_info
*uinfo
)
2206 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2207 u16 nid
= get_amp_nid(kcontrol
);
2208 u8 chs
= get_amp_channels(kcontrol
);
2209 int dir
= get_amp_direction(kcontrol
);
2210 unsigned int ofs
= get_amp_offset(kcontrol
);
2212 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
2213 uinfo
->count
= chs
== 3 ? 2 : 1;
2214 uinfo
->value
.integer
.min
= 0;
2215 uinfo
->value
.integer
.max
= get_amp_max_value(codec
, nid
, dir
, ofs
);
2216 if (!uinfo
->value
.integer
.max
) {
2217 printk(KERN_WARNING
"hda_codec: "
2218 "num_steps = 0 for NID=0x%x (ctl = %s)\n", nid
,
2224 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_info
);
2227 static inline unsigned int
2228 read_amp_value(struct hda_codec
*codec
, hda_nid_t nid
,
2229 int ch
, int dir
, int idx
, unsigned int ofs
)
2232 val
= snd_hda_codec_amp_read(codec
, nid
, ch
, dir
, idx
);
2233 val
&= HDA_AMP_VOLMASK
;
2242 update_amp_value(struct hda_codec
*codec
, hda_nid_t nid
,
2243 int ch
, int dir
, int idx
, unsigned int ofs
,
2246 unsigned int maxval
;
2250 /* ofs = 0: raw max value */
2251 maxval
= get_amp_max_value(codec
, nid
, dir
, 0);
2254 return snd_hda_codec_amp_update(codec
, nid
, ch
, dir
, idx
,
2255 HDA_AMP_VOLMASK
, val
);
2259 * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume
2261 * The control element is supposed to have the private_value field
2262 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2264 int snd_hda_mixer_amp_volume_get(struct snd_kcontrol
*kcontrol
,
2265 struct snd_ctl_elem_value
*ucontrol
)
2267 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2268 hda_nid_t nid
= get_amp_nid(kcontrol
);
2269 int chs
= get_amp_channels(kcontrol
);
2270 int dir
= get_amp_direction(kcontrol
);
2271 int idx
= get_amp_index(kcontrol
);
2272 unsigned int ofs
= get_amp_offset(kcontrol
);
2273 long *valp
= ucontrol
->value
.integer
.value
;
2276 *valp
++ = read_amp_value(codec
, nid
, 0, dir
, idx
, ofs
);
2278 *valp
= read_amp_value(codec
, nid
, 1, dir
, idx
, ofs
);
2281 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_get
);
2284 * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume
2286 * The control element is supposed to have the private_value field
2287 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2289 int snd_hda_mixer_amp_volume_put(struct snd_kcontrol
*kcontrol
,
2290 struct snd_ctl_elem_value
*ucontrol
)
2292 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2293 hda_nid_t nid
= get_amp_nid(kcontrol
);
2294 int chs
= get_amp_channels(kcontrol
);
2295 int dir
= get_amp_direction(kcontrol
);
2296 int idx
= get_amp_index(kcontrol
);
2297 unsigned int ofs
= get_amp_offset(kcontrol
);
2298 long *valp
= ucontrol
->value
.integer
.value
;
2301 snd_hda_power_up(codec
);
2303 change
= update_amp_value(codec
, nid
, 0, dir
, idx
, ofs
, *valp
);
2307 change
|= update_amp_value(codec
, nid
, 1, dir
, idx
, ofs
, *valp
);
2308 snd_hda_power_down(codec
);
2311 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_put
);
2314 * snd_hda_mixer_amp_volume_put - TLV callback for a standard AMP mixer volume
2316 * The control element is supposed to have the private_value field
2317 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2319 int snd_hda_mixer_amp_tlv(struct snd_kcontrol
*kcontrol
, int op_flag
,
2320 unsigned int size
, unsigned int __user
*_tlv
)
2322 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2323 hda_nid_t nid
= get_amp_nid(kcontrol
);
2324 int dir
= get_amp_direction(kcontrol
);
2325 unsigned int ofs
= get_amp_offset(kcontrol
);
2326 bool min_mute
= get_amp_min_mute(kcontrol
);
2327 u32 caps
, val1
, val2
;
2329 if (size
< 4 * sizeof(unsigned int))
2331 caps
= query_amp_caps(codec
, nid
, dir
);
2332 val2
= (caps
& AC_AMPCAP_STEP_SIZE
) >> AC_AMPCAP_STEP_SIZE_SHIFT
;
2333 val2
= (val2
+ 1) * 25;
2334 val1
= -((caps
& AC_AMPCAP_OFFSET
) >> AC_AMPCAP_OFFSET_SHIFT
);
2336 val1
= ((int)val1
) * ((int)val2
);
2337 if (min_mute
|| (caps
& AC_AMPCAP_MIN_MUTE
))
2338 val2
|= TLV_DB_SCALE_MUTE
;
2339 if (put_user(SNDRV_CTL_TLVT_DB_SCALE
, _tlv
))
2341 if (put_user(2 * sizeof(unsigned int), _tlv
+ 1))
2343 if (put_user(val1
, _tlv
+ 2))
2345 if (put_user(val2
, _tlv
+ 3))
2349 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_tlv
);
2352 * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control
2353 * @codec: HD-audio codec
2354 * @nid: NID of a reference widget
2355 * @dir: #HDA_INPUT or #HDA_OUTPUT
2356 * @tlv: TLV data to be stored, at least 4 elements
2358 * Set (static) TLV data for a virtual master volume using the AMP caps
2359 * obtained from the reference NID.
2360 * The volume range is recalculated as if the max volume is 0dB.
2362 void snd_hda_set_vmaster_tlv(struct hda_codec
*codec
, hda_nid_t nid
, int dir
,
2368 caps
= query_amp_caps(codec
, nid
, dir
);
2369 nums
= (caps
& AC_AMPCAP_NUM_STEPS
) >> AC_AMPCAP_NUM_STEPS_SHIFT
;
2370 step
= (caps
& AC_AMPCAP_STEP_SIZE
) >> AC_AMPCAP_STEP_SIZE_SHIFT
;
2371 step
= (step
+ 1) * 25;
2372 tlv
[0] = SNDRV_CTL_TLVT_DB_SCALE
;
2373 tlv
[1] = 2 * sizeof(unsigned int);
2374 tlv
[2] = -nums
* step
;
2377 EXPORT_SYMBOL_HDA(snd_hda_set_vmaster_tlv
);
2379 /* find a mixer control element with the given name */
2380 static struct snd_kcontrol
*
2381 find_mixer_ctl(struct hda_codec
*codec
, const char *name
, int dev
, int idx
)
2383 struct snd_ctl_elem_id id
;
2384 memset(&id
, 0, sizeof(id
));
2385 id
.iface
= SNDRV_CTL_ELEM_IFACE_MIXER
;
2388 if (snd_BUG_ON(strlen(name
) >= sizeof(id
.name
)))
2390 strcpy(id
.name
, name
);
2391 return snd_ctl_find_id(codec
->bus
->card
, &id
);
2395 * snd_hda_find_mixer_ctl - Find a mixer control element with the given name
2396 * @codec: HD-audio codec
2397 * @name: ctl id name string
2399 * Get the control element with the given id string and IFACE_MIXER.
2401 struct snd_kcontrol
*snd_hda_find_mixer_ctl(struct hda_codec
*codec
,
2404 return find_mixer_ctl(codec
, name
, 0, 0);
2406 EXPORT_SYMBOL_HDA(snd_hda_find_mixer_ctl
);
2408 static int find_empty_mixer_ctl_idx(struct hda_codec
*codec
, const char *name
,
2412 /* 16 ctlrs should be large enough */
2413 for (i
= 0, idx
= start_idx
; i
< 16; i
++, idx
++) {
2414 if (!find_mixer_ctl(codec
, name
, 0, idx
))
2421 * snd_hda_ctl_add - Add a control element and assign to the codec
2422 * @codec: HD-audio codec
2423 * @nid: corresponding NID (optional)
2424 * @kctl: the control element to assign
2426 * Add the given control element to an array inside the codec instance.
2427 * All control elements belonging to a codec are supposed to be added
2428 * by this function so that a proper clean-up works at the free or
2429 * reconfiguration time.
2431 * If non-zero @nid is passed, the NID is assigned to the control element.
2432 * The assignment is shown in the codec proc file.
2434 * snd_hda_ctl_add() checks the control subdev id field whether
2435 * #HDA_SUBDEV_NID_FLAG bit is set. If set (and @nid is zero), the lower
2436 * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit
2437 * specifies if kctl->private_value is a HDA amplifier value.
2439 int snd_hda_ctl_add(struct hda_codec
*codec
, hda_nid_t nid
,
2440 struct snd_kcontrol
*kctl
)
2443 unsigned short flags
= 0;
2444 struct hda_nid_item
*item
;
2446 if (kctl
->id
.subdevice
& HDA_SUBDEV_AMP_FLAG
) {
2447 flags
|= HDA_NID_ITEM_AMP
;
2449 nid
= get_amp_nid_(kctl
->private_value
);
2451 if ((kctl
->id
.subdevice
& HDA_SUBDEV_NID_FLAG
) != 0 && nid
== 0)
2452 nid
= kctl
->id
.subdevice
& 0xffff;
2453 if (kctl
->id
.subdevice
& (HDA_SUBDEV_NID_FLAG
|HDA_SUBDEV_AMP_FLAG
))
2454 kctl
->id
.subdevice
= 0;
2455 err
= snd_ctl_add(codec
->bus
->card
, kctl
);
2458 item
= snd_array_new(&codec
->mixers
);
2463 item
->flags
= flags
;
2466 EXPORT_SYMBOL_HDA(snd_hda_ctl_add
);
2469 * snd_hda_add_nid - Assign a NID to a control element
2470 * @codec: HD-audio codec
2471 * @nid: corresponding NID (optional)
2472 * @kctl: the control element to assign
2473 * @index: index to kctl
2475 * Add the given control element to an array inside the codec instance.
2476 * This function is used when #snd_hda_ctl_add cannot be used for 1:1
2477 * NID:KCTL mapping - for example "Capture Source" selector.
2479 int snd_hda_add_nid(struct hda_codec
*codec
, struct snd_kcontrol
*kctl
,
2480 unsigned int index
, hda_nid_t nid
)
2482 struct hda_nid_item
*item
;
2485 item
= snd_array_new(&codec
->nids
);
2489 item
->index
= index
;
2493 printk(KERN_ERR
"hda-codec: no NID for mapping control %s:%d:%d\n",
2494 kctl
->id
.name
, kctl
->id
.index
, index
);
2497 EXPORT_SYMBOL_HDA(snd_hda_add_nid
);
2500 * snd_hda_ctls_clear - Clear all controls assigned to the given codec
2501 * @codec: HD-audio codec
2503 void snd_hda_ctls_clear(struct hda_codec
*codec
)
2506 struct hda_nid_item
*items
= codec
->mixers
.list
;
2507 for (i
= 0; i
< codec
->mixers
.used
; i
++)
2508 snd_ctl_remove(codec
->bus
->card
, items
[i
].kctl
);
2509 snd_array_free(&codec
->mixers
);
2510 snd_array_free(&codec
->nids
);
2513 /* pseudo device locking
2514 * toggle card->shutdown to allow/disallow the device access (as a hack)
2516 int snd_hda_lock_devices(struct hda_bus
*bus
)
2518 struct snd_card
*card
= bus
->card
;
2519 struct hda_codec
*codec
;
2521 spin_lock(&card
->files_lock
);
2525 if (!list_empty(&card
->ctl_files
))
2528 list_for_each_entry(codec
, &bus
->codec_list
, list
) {
2530 for (pcm
= 0; pcm
< codec
->num_pcms
; pcm
++) {
2531 struct hda_pcm
*cpcm
= &codec
->pcm_info
[pcm
];
2534 if (cpcm
->pcm
->streams
[0].substream_opened
||
2535 cpcm
->pcm
->streams
[1].substream_opened
)
2539 spin_unlock(&card
->files_lock
);
2545 spin_unlock(&card
->files_lock
);
2548 EXPORT_SYMBOL_HDA(snd_hda_lock_devices
);
2550 void snd_hda_unlock_devices(struct hda_bus
*bus
)
2552 struct snd_card
*card
= bus
->card
;
2555 spin_lock(&card
->files_lock
);
2557 spin_unlock(&card
->files_lock
);
2559 EXPORT_SYMBOL_HDA(snd_hda_unlock_devices
);
2562 * snd_hda_codec_reset - Clear all objects assigned to the codec
2563 * @codec: HD-audio codec
2565 * This frees the all PCM and control elements assigned to the codec, and
2566 * clears the caches and restores the pin default configurations.
2568 * When a device is being used, it returns -EBSY. If successfully freed,
2571 int snd_hda_codec_reset(struct hda_codec
*codec
)
2573 struct hda_bus
*bus
= codec
->bus
;
2574 struct snd_card
*card
= bus
->card
;
2577 if (snd_hda_lock_devices(bus
) < 0)
2580 /* OK, let it free */
2581 cancel_delayed_work_sync(&codec
->jackpoll_work
);
2583 cancel_delayed_work_sync(&codec
->power_work
);
2584 flush_workqueue(bus
->workq
);
2586 snd_hda_ctls_clear(codec
);
2588 for (i
= 0; i
< codec
->num_pcms
; i
++) {
2589 if (codec
->pcm_info
[i
].pcm
) {
2590 snd_device_free(card
, codec
->pcm_info
[i
].pcm
);
2591 clear_bit(codec
->pcm_info
[i
].device
,
2595 if (codec
->patch_ops
.free
)
2596 codec
->patch_ops
.free(codec
);
2597 memset(&codec
->patch_ops
, 0, sizeof(codec
->patch_ops
));
2598 snd_hda_jack_tbl_clear(codec
);
2599 codec
->proc_widget_hook
= NULL
;
2601 free_hda_cache(&codec
->amp_cache
);
2602 free_hda_cache(&codec
->cmd_cache
);
2603 init_hda_cache(&codec
->amp_cache
, sizeof(struct hda_amp_info
));
2604 init_hda_cache(&codec
->cmd_cache
, sizeof(struct hda_cache_head
));
2605 /* free only driver_pins so that init_pins + user_pins are restored */
2606 snd_array_free(&codec
->driver_pins
);
2607 snd_array_free(&codec
->cvt_setups
);
2608 snd_array_free(&codec
->spdif_out
);
2609 snd_array_free(&codec
->verbs
);
2610 codec
->num_pcms
= 0;
2611 codec
->pcm_info
= NULL
;
2612 codec
->preset
= NULL
;
2613 codec
->slave_dig_outs
= NULL
;
2614 codec
->spdif_status_reset
= 0;
2615 module_put(codec
->owner
);
2616 codec
->owner
= NULL
;
2618 /* allow device access again */
2619 snd_hda_unlock_devices(bus
);
2623 typedef int (*map_slave_func_t
)(void *, struct snd_kcontrol
*);
2625 /* apply the function to all matching slave ctls in the mixer list */
2626 static int map_slaves(struct hda_codec
*codec
, const char * const *slaves
,
2627 const char *suffix
, map_slave_func_t func
, void *data
)
2629 struct hda_nid_item
*items
;
2630 const char * const *s
;
2633 items
= codec
->mixers
.list
;
2634 for (i
= 0; i
< codec
->mixers
.used
; i
++) {
2635 struct snd_kcontrol
*sctl
= items
[i
].kctl
;
2636 if (!sctl
|| !sctl
->id
.name
||
2637 sctl
->id
.iface
!= SNDRV_CTL_ELEM_IFACE_MIXER
)
2639 for (s
= slaves
; *s
; s
++) {
2640 char tmpname
[sizeof(sctl
->id
.name
)];
2641 const char *name
= *s
;
2643 snprintf(tmpname
, sizeof(tmpname
), "%s %s",
2647 if (!strcmp(sctl
->id
.name
, name
)) {
2648 err
= func(data
, sctl
);
2658 static int check_slave_present(void *data
, struct snd_kcontrol
*sctl
)
2663 /* guess the value corresponding to 0dB */
2664 static int get_kctl_0dB_offset(struct snd_kcontrol
*kctl
)
2667 const int *tlv
= NULL
;
2670 if (kctl
->vd
[0].access
& SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK
) {
2671 /* FIXME: set_fs() hack for obtaining user-space TLV data */
2672 mm_segment_t fs
= get_fs();
2674 if (!kctl
->tlv
.c(kctl
, 0, sizeof(_tlv
), _tlv
))
2677 } else if (kctl
->vd
[0].access
& SNDRV_CTL_ELEM_ACCESS_TLV_READ
)
2679 if (tlv
&& tlv
[0] == SNDRV_CTL_TLVT_DB_SCALE
)
2680 val
= -tlv
[2] / tlv
[3];
2684 /* call kctl->put with the given value(s) */
2685 static int put_kctl_with_value(struct snd_kcontrol
*kctl
, int val
)
2687 struct snd_ctl_elem_value
*ucontrol
;
2688 ucontrol
= kzalloc(sizeof(*ucontrol
), GFP_KERNEL
);
2691 ucontrol
->value
.integer
.value
[0] = val
;
2692 ucontrol
->value
.integer
.value
[1] = val
;
2693 kctl
->put(kctl
, ucontrol
);
2698 /* initialize the slave volume with 0dB */
2699 static int init_slave_0dB(void *data
, struct snd_kcontrol
*slave
)
2701 int offset
= get_kctl_0dB_offset(slave
);
2703 put_kctl_with_value(slave
, offset
);
2707 /* unmute the slave */
2708 static int init_slave_unmute(void *data
, struct snd_kcontrol
*slave
)
2710 return put_kctl_with_value(slave
, 1);
2714 * snd_hda_add_vmaster - create a virtual master control and add slaves
2715 * @codec: HD-audio codec
2716 * @name: vmaster control name
2717 * @tlv: TLV data (optional)
2718 * @slaves: slave control names (optional)
2719 * @suffix: suffix string to each slave name (optional)
2720 * @init_slave_vol: initialize slaves to unmute/0dB
2721 * @ctl_ret: store the vmaster kcontrol in return
2723 * Create a virtual master control with the given name. The TLV data
2724 * must be either NULL or a valid data.
2726 * @slaves is a NULL-terminated array of strings, each of which is a
2727 * slave control name. All controls with these names are assigned to
2728 * the new virtual master control.
2730 * This function returns zero if successful or a negative error code.
2732 int __snd_hda_add_vmaster(struct hda_codec
*codec
, char *name
,
2733 unsigned int *tlv
, const char * const *slaves
,
2734 const char *suffix
, bool init_slave_vol
,
2735 struct snd_kcontrol
**ctl_ret
)
2737 struct snd_kcontrol
*kctl
;
2743 err
= map_slaves(codec
, slaves
, suffix
, check_slave_present
, NULL
);
2745 snd_printdd("No slave found for %s\n", name
);
2748 kctl
= snd_ctl_make_virtual_master(name
, tlv
);
2751 err
= snd_hda_ctl_add(codec
, 0, kctl
);
2755 err
= map_slaves(codec
, slaves
, suffix
,
2756 (map_slave_func_t
)snd_ctl_add_slave
, kctl
);
2760 /* init with master mute & zero volume */
2761 put_kctl_with_value(kctl
, 0);
2763 map_slaves(codec
, slaves
, suffix
,
2764 tlv
? init_slave_0dB
: init_slave_unmute
, kctl
);
2770 EXPORT_SYMBOL_HDA(__snd_hda_add_vmaster
);
2773 * mute-LED control using vmaster
2775 static int vmaster_mute_mode_info(struct snd_kcontrol
*kcontrol
,
2776 struct snd_ctl_elem_info
*uinfo
)
2778 static const char * const texts
[] = {
2779 "On", "Off", "Follow Master"
2783 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_ENUMERATED
;
2785 uinfo
->value
.enumerated
.items
= 3;
2786 index
= uinfo
->value
.enumerated
.item
;
2789 strcpy(uinfo
->value
.enumerated
.name
, texts
[index
]);
2793 static int vmaster_mute_mode_get(struct snd_kcontrol
*kcontrol
,
2794 struct snd_ctl_elem_value
*ucontrol
)
2796 struct hda_vmaster_mute_hook
*hook
= snd_kcontrol_chip(kcontrol
);
2797 ucontrol
->value
.enumerated
.item
[0] = hook
->mute_mode
;
2801 static int vmaster_mute_mode_put(struct snd_kcontrol
*kcontrol
,
2802 struct snd_ctl_elem_value
*ucontrol
)
2804 struct hda_vmaster_mute_hook
*hook
= snd_kcontrol_chip(kcontrol
);
2805 unsigned int old_mode
= hook
->mute_mode
;
2807 hook
->mute_mode
= ucontrol
->value
.enumerated
.item
[0];
2808 if (hook
->mute_mode
> HDA_VMUTE_FOLLOW_MASTER
)
2809 hook
->mute_mode
= HDA_VMUTE_FOLLOW_MASTER
;
2810 if (old_mode
== hook
->mute_mode
)
2812 snd_hda_sync_vmaster_hook(hook
);
2816 static struct snd_kcontrol_new vmaster_mute_mode
= {
2817 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
2818 .name
= "Mute-LED Mode",
2819 .info
= vmaster_mute_mode_info
,
2820 .get
= vmaster_mute_mode_get
,
2821 .put
= vmaster_mute_mode_put
,
2825 * Add a mute-LED hook with the given vmaster switch kctl
2826 * "Mute-LED Mode" control is automatically created and associated with
2829 int snd_hda_add_vmaster_hook(struct hda_codec
*codec
,
2830 struct hda_vmaster_mute_hook
*hook
,
2831 bool expose_enum_ctl
)
2833 struct snd_kcontrol
*kctl
;
2835 if (!hook
->hook
|| !hook
->sw_kctl
)
2837 snd_ctl_add_vmaster_hook(hook
->sw_kctl
, hook
->hook
, codec
);
2838 hook
->codec
= codec
;
2839 hook
->mute_mode
= HDA_VMUTE_FOLLOW_MASTER
;
2840 if (!expose_enum_ctl
)
2842 kctl
= snd_ctl_new1(&vmaster_mute_mode
, hook
);
2845 return snd_hda_ctl_add(codec
, 0, kctl
);
2847 EXPORT_SYMBOL_HDA(snd_hda_add_vmaster_hook
);
2850 * Call the hook with the current value for synchronization
2851 * Should be called in init callback
2853 void snd_hda_sync_vmaster_hook(struct hda_vmaster_mute_hook
*hook
)
2855 if (!hook
->hook
|| !hook
->codec
)
2857 /* don't call vmaster hook in the destructor since it might have
2858 * been already destroyed
2860 if (hook
->codec
->bus
->shutdown
)
2862 switch (hook
->mute_mode
) {
2863 case HDA_VMUTE_FOLLOW_MASTER
:
2864 snd_ctl_sync_vmaster_hook(hook
->sw_kctl
);
2867 hook
->hook(hook
->codec
, hook
->mute_mode
);
2871 EXPORT_SYMBOL_HDA(snd_hda_sync_vmaster_hook
);
2875 * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch
2877 * The control element is supposed to have the private_value field
2878 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2880 int snd_hda_mixer_amp_switch_info(struct snd_kcontrol
*kcontrol
,
2881 struct snd_ctl_elem_info
*uinfo
)
2883 int chs
= get_amp_channels(kcontrol
);
2885 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_BOOLEAN
;
2886 uinfo
->count
= chs
== 3 ? 2 : 1;
2887 uinfo
->value
.integer
.min
= 0;
2888 uinfo
->value
.integer
.max
= 1;
2891 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_info
);
2894 * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch
2896 * The control element is supposed to have the private_value field
2897 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2899 int snd_hda_mixer_amp_switch_get(struct snd_kcontrol
*kcontrol
,
2900 struct snd_ctl_elem_value
*ucontrol
)
2902 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2903 hda_nid_t nid
= get_amp_nid(kcontrol
);
2904 int chs
= get_amp_channels(kcontrol
);
2905 int dir
= get_amp_direction(kcontrol
);
2906 int idx
= get_amp_index(kcontrol
);
2907 long *valp
= ucontrol
->value
.integer
.value
;
2910 *valp
++ = (snd_hda_codec_amp_read(codec
, nid
, 0, dir
, idx
) &
2911 HDA_AMP_MUTE
) ? 0 : 1;
2913 *valp
= (snd_hda_codec_amp_read(codec
, nid
, 1, dir
, idx
) &
2914 HDA_AMP_MUTE
) ? 0 : 1;
2917 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_get
);
2920 * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch
2922 * The control element is supposed to have the private_value field
2923 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2925 int snd_hda_mixer_amp_switch_put(struct snd_kcontrol
*kcontrol
,
2926 struct snd_ctl_elem_value
*ucontrol
)
2928 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2929 hda_nid_t nid
= get_amp_nid(kcontrol
);
2930 int chs
= get_amp_channels(kcontrol
);
2931 int dir
= get_amp_direction(kcontrol
);
2932 int idx
= get_amp_index(kcontrol
);
2933 long *valp
= ucontrol
->value
.integer
.value
;
2936 snd_hda_power_up(codec
);
2938 change
= snd_hda_codec_amp_update(codec
, nid
, 0, dir
, idx
,
2940 *valp
? 0 : HDA_AMP_MUTE
);
2944 change
|= snd_hda_codec_amp_update(codec
, nid
, 1, dir
, idx
,
2946 *valp
? 0 : HDA_AMP_MUTE
);
2947 hda_call_check_power_status(codec
, nid
);
2948 snd_hda_power_down(codec
);
2951 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put
);
2954 * bound volume controls
2956 * bind multiple volumes (# indices, from 0)
2959 #define AMP_VAL_IDX_SHIFT 19
2960 #define AMP_VAL_IDX_MASK (0x0f<<19)
2963 * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control
2965 * The control element is supposed to have the private_value field
2966 * set up via HDA_BIND_MUTE*() macros.
2968 int snd_hda_mixer_bind_switch_get(struct snd_kcontrol
*kcontrol
,
2969 struct snd_ctl_elem_value
*ucontrol
)
2971 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2975 mutex_lock(&codec
->control_mutex
);
2976 pval
= kcontrol
->private_value
;
2977 kcontrol
->private_value
= pval
& ~AMP_VAL_IDX_MASK
; /* index 0 */
2978 err
= snd_hda_mixer_amp_switch_get(kcontrol
, ucontrol
);
2979 kcontrol
->private_value
= pval
;
2980 mutex_unlock(&codec
->control_mutex
);
2983 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_get
);
2986 * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control
2988 * The control element is supposed to have the private_value field
2989 * set up via HDA_BIND_MUTE*() macros.
2991 int snd_hda_mixer_bind_switch_put(struct snd_kcontrol
*kcontrol
,
2992 struct snd_ctl_elem_value
*ucontrol
)
2994 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2996 int i
, indices
, err
= 0, change
= 0;
2998 mutex_lock(&codec
->control_mutex
);
2999 pval
= kcontrol
->private_value
;
3000 indices
= (pval
& AMP_VAL_IDX_MASK
) >> AMP_VAL_IDX_SHIFT
;
3001 for (i
= 0; i
< indices
; i
++) {
3002 kcontrol
->private_value
= (pval
& ~AMP_VAL_IDX_MASK
) |
3003 (i
<< AMP_VAL_IDX_SHIFT
);
3004 err
= snd_hda_mixer_amp_switch_put(kcontrol
, ucontrol
);
3009 kcontrol
->private_value
= pval
;
3010 mutex_unlock(&codec
->control_mutex
);
3011 return err
< 0 ? err
: change
;
3013 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_put
);
3016 * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control
3018 * The control element is supposed to have the private_value field
3019 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
3021 int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol
*kcontrol
,
3022 struct snd_ctl_elem_info
*uinfo
)
3024 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
3025 struct hda_bind_ctls
*c
;
3028 mutex_lock(&codec
->control_mutex
);
3029 c
= (struct hda_bind_ctls
*)kcontrol
->private_value
;
3030 kcontrol
->private_value
= *c
->values
;
3031 err
= c
->ops
->info(kcontrol
, uinfo
);
3032 kcontrol
->private_value
= (long)c
;
3033 mutex_unlock(&codec
->control_mutex
);
3036 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_info
);
3039 * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control
3041 * The control element is supposed to have the private_value field
3042 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
3044 int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol
*kcontrol
,
3045 struct snd_ctl_elem_value
*ucontrol
)
3047 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
3048 struct hda_bind_ctls
*c
;
3051 mutex_lock(&codec
->control_mutex
);
3052 c
= (struct hda_bind_ctls
*)kcontrol
->private_value
;
3053 kcontrol
->private_value
= *c
->values
;
3054 err
= c
->ops
->get(kcontrol
, ucontrol
);
3055 kcontrol
->private_value
= (long)c
;
3056 mutex_unlock(&codec
->control_mutex
);
3059 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_get
);
3062 * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control
3064 * The control element is supposed to have the private_value field
3065 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
3067 int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol
*kcontrol
,
3068 struct snd_ctl_elem_value
*ucontrol
)
3070 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
3071 struct hda_bind_ctls
*c
;
3072 unsigned long *vals
;
3073 int err
= 0, change
= 0;
3075 mutex_lock(&codec
->control_mutex
);
3076 c
= (struct hda_bind_ctls
*)kcontrol
->private_value
;
3077 for (vals
= c
->values
; *vals
; vals
++) {
3078 kcontrol
->private_value
= *vals
;
3079 err
= c
->ops
->put(kcontrol
, ucontrol
);
3084 kcontrol
->private_value
= (long)c
;
3085 mutex_unlock(&codec
->control_mutex
);
3086 return err
< 0 ? err
: change
;
3088 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_put
);
3091 * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control
3093 * The control element is supposed to have the private_value field
3094 * set up via HDA_BIND_VOL() macro.
3096 int snd_hda_mixer_bind_tlv(struct snd_kcontrol
*kcontrol
, int op_flag
,
3097 unsigned int size
, unsigned int __user
*tlv
)
3099 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
3100 struct hda_bind_ctls
*c
;
3103 mutex_lock(&codec
->control_mutex
);
3104 c
= (struct hda_bind_ctls
*)kcontrol
->private_value
;
3105 kcontrol
->private_value
= *c
->values
;
3106 err
= c
->ops
->tlv(kcontrol
, op_flag
, size
, tlv
);
3107 kcontrol
->private_value
= (long)c
;
3108 mutex_unlock(&codec
->control_mutex
);
3111 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_tlv
);
3113 struct hda_ctl_ops snd_hda_bind_vol
= {
3114 .info
= snd_hda_mixer_amp_volume_info
,
3115 .get
= snd_hda_mixer_amp_volume_get
,
3116 .put
= snd_hda_mixer_amp_volume_put
,
3117 .tlv
= snd_hda_mixer_amp_tlv
3119 EXPORT_SYMBOL_HDA(snd_hda_bind_vol
);
3121 struct hda_ctl_ops snd_hda_bind_sw
= {
3122 .info
= snd_hda_mixer_amp_switch_info
,
3123 .get
= snd_hda_mixer_amp_switch_get
,
3124 .put
= snd_hda_mixer_amp_switch_put
,
3125 .tlv
= snd_hda_mixer_amp_tlv
3127 EXPORT_SYMBOL_HDA(snd_hda_bind_sw
);
3130 * SPDIF out controls
3133 static int snd_hda_spdif_mask_info(struct snd_kcontrol
*kcontrol
,
3134 struct snd_ctl_elem_info
*uinfo
)
3136 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_IEC958
;
3141 static int snd_hda_spdif_cmask_get(struct snd_kcontrol
*kcontrol
,
3142 struct snd_ctl_elem_value
*ucontrol
)
3144 ucontrol
->value
.iec958
.status
[0] = IEC958_AES0_PROFESSIONAL
|
3145 IEC958_AES0_NONAUDIO
|
3146 IEC958_AES0_CON_EMPHASIS_5015
|
3147 IEC958_AES0_CON_NOT_COPYRIGHT
;
3148 ucontrol
->value
.iec958
.status
[1] = IEC958_AES1_CON_CATEGORY
|
3149 IEC958_AES1_CON_ORIGINAL
;
3153 static int snd_hda_spdif_pmask_get(struct snd_kcontrol
*kcontrol
,
3154 struct snd_ctl_elem_value
*ucontrol
)
3156 ucontrol
->value
.iec958
.status
[0] = IEC958_AES0_PROFESSIONAL
|
3157 IEC958_AES0_NONAUDIO
|
3158 IEC958_AES0_PRO_EMPHASIS_5015
;
3162 static int snd_hda_spdif_default_get(struct snd_kcontrol
*kcontrol
,
3163 struct snd_ctl_elem_value
*ucontrol
)
3165 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
3166 int idx
= kcontrol
->private_value
;
3167 struct hda_spdif_out
*spdif
;
3169 mutex_lock(&codec
->spdif_mutex
);
3170 spdif
= snd_array_elem(&codec
->spdif_out
, idx
);
3171 ucontrol
->value
.iec958
.status
[0] = spdif
->status
& 0xff;
3172 ucontrol
->value
.iec958
.status
[1] = (spdif
->status
>> 8) & 0xff;
3173 ucontrol
->value
.iec958
.status
[2] = (spdif
->status
>> 16) & 0xff;
3174 ucontrol
->value
.iec958
.status
[3] = (spdif
->status
>> 24) & 0xff;
3175 mutex_unlock(&codec
->spdif_mutex
);
3180 /* convert from SPDIF status bits to HDA SPDIF bits
3181 * bit 0 (DigEn) is always set zero (to be filled later)
3183 static unsigned short convert_from_spdif_status(unsigned int sbits
)
3185 unsigned short val
= 0;
3187 if (sbits
& IEC958_AES0_PROFESSIONAL
)
3188 val
|= AC_DIG1_PROFESSIONAL
;
3189 if (sbits
& IEC958_AES0_NONAUDIO
)
3190 val
|= AC_DIG1_NONAUDIO
;
3191 if (sbits
& IEC958_AES0_PROFESSIONAL
) {
3192 if ((sbits
& IEC958_AES0_PRO_EMPHASIS
) ==
3193 IEC958_AES0_PRO_EMPHASIS_5015
)
3194 val
|= AC_DIG1_EMPHASIS
;
3196 if ((sbits
& IEC958_AES0_CON_EMPHASIS
) ==
3197 IEC958_AES0_CON_EMPHASIS_5015
)
3198 val
|= AC_DIG1_EMPHASIS
;
3199 if (!(sbits
& IEC958_AES0_CON_NOT_COPYRIGHT
))
3200 val
|= AC_DIG1_COPYRIGHT
;
3201 if (sbits
& (IEC958_AES1_CON_ORIGINAL
<< 8))
3202 val
|= AC_DIG1_LEVEL
;
3203 val
|= sbits
& (IEC958_AES1_CON_CATEGORY
<< 8);
3208 /* convert to SPDIF status bits from HDA SPDIF bits
3210 static unsigned int convert_to_spdif_status(unsigned short val
)
3212 unsigned int sbits
= 0;
3214 if (val
& AC_DIG1_NONAUDIO
)
3215 sbits
|= IEC958_AES0_NONAUDIO
;
3216 if (val
& AC_DIG1_PROFESSIONAL
)
3217 sbits
|= IEC958_AES0_PROFESSIONAL
;
3218 if (sbits
& IEC958_AES0_PROFESSIONAL
) {
3219 if (val
& AC_DIG1_EMPHASIS
)
3220 sbits
|= IEC958_AES0_PRO_EMPHASIS_5015
;
3222 if (val
& AC_DIG1_EMPHASIS
)
3223 sbits
|= IEC958_AES0_CON_EMPHASIS_5015
;
3224 if (!(val
& AC_DIG1_COPYRIGHT
))
3225 sbits
|= IEC958_AES0_CON_NOT_COPYRIGHT
;
3226 if (val
& AC_DIG1_LEVEL
)
3227 sbits
|= (IEC958_AES1_CON_ORIGINAL
<< 8);
3228 sbits
|= val
& (0x7f << 8);
3233 /* set digital convert verbs both for the given NID and its slaves */
3234 static void set_dig_out(struct hda_codec
*codec
, hda_nid_t nid
,
3239 snd_hda_codec_write_cache(codec
, nid
, 0, verb
, val
);
3240 d
= codec
->slave_dig_outs
;
3244 snd_hda_codec_write_cache(codec
, *d
, 0, verb
, val
);
3247 static inline void set_dig_out_convert(struct hda_codec
*codec
, hda_nid_t nid
,
3251 set_dig_out(codec
, nid
, AC_VERB_SET_DIGI_CONVERT_1
, dig1
);
3253 set_dig_out(codec
, nid
, AC_VERB_SET_DIGI_CONVERT_2
, dig2
);
3256 static int snd_hda_spdif_default_put(struct snd_kcontrol
*kcontrol
,
3257 struct snd_ctl_elem_value
*ucontrol
)
3259 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
3260 int idx
= kcontrol
->private_value
;
3261 struct hda_spdif_out
*spdif
;
3266 mutex_lock(&codec
->spdif_mutex
);
3267 spdif
= snd_array_elem(&codec
->spdif_out
, idx
);
3269 spdif
->status
= ucontrol
->value
.iec958
.status
[0] |
3270 ((unsigned int)ucontrol
->value
.iec958
.status
[1] << 8) |
3271 ((unsigned int)ucontrol
->value
.iec958
.status
[2] << 16) |
3272 ((unsigned int)ucontrol
->value
.iec958
.status
[3] << 24);
3273 val
= convert_from_spdif_status(spdif
->status
);
3274 val
|= spdif
->ctls
& 1;
3275 change
= spdif
->ctls
!= val
;
3277 if (change
&& nid
!= (u16
)-1)
3278 set_dig_out_convert(codec
, nid
, val
& 0xff, (val
>> 8) & 0xff);
3279 mutex_unlock(&codec
->spdif_mutex
);
3283 #define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
3285 static int snd_hda_spdif_out_switch_get(struct snd_kcontrol
*kcontrol
,
3286 struct snd_ctl_elem_value
*ucontrol
)
3288 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
3289 int idx
= kcontrol
->private_value
;
3290 struct hda_spdif_out
*spdif
;
3292 mutex_lock(&codec
->spdif_mutex
);
3293 spdif
= snd_array_elem(&codec
->spdif_out
, idx
);
3294 ucontrol
->value
.integer
.value
[0] = spdif
->ctls
& AC_DIG1_ENABLE
;
3295 mutex_unlock(&codec
->spdif_mutex
);
3299 static inline void set_spdif_ctls(struct hda_codec
*codec
, hda_nid_t nid
,
3302 set_dig_out_convert(codec
, nid
, dig1
, dig2
);
3303 /* unmute amp switch (if any) */
3304 if ((get_wcaps(codec
, nid
) & AC_WCAP_OUT_AMP
) &&
3305 (dig1
& AC_DIG1_ENABLE
))
3306 snd_hda_codec_amp_stereo(codec
, nid
, HDA_OUTPUT
, 0,
3310 static int snd_hda_spdif_out_switch_put(struct snd_kcontrol
*kcontrol
,
3311 struct snd_ctl_elem_value
*ucontrol
)
3313 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
3314 int idx
= kcontrol
->private_value
;
3315 struct hda_spdif_out
*spdif
;
3320 mutex_lock(&codec
->spdif_mutex
);
3321 spdif
= snd_array_elem(&codec
->spdif_out
, idx
);
3323 val
= spdif
->ctls
& ~AC_DIG1_ENABLE
;
3324 if (ucontrol
->value
.integer
.value
[0])
3325 val
|= AC_DIG1_ENABLE
;
3326 change
= spdif
->ctls
!= val
;
3328 if (change
&& nid
!= (u16
)-1)
3329 set_spdif_ctls(codec
, nid
, val
& 0xff, -1);
3330 mutex_unlock(&codec
->spdif_mutex
);
3334 static struct snd_kcontrol_new dig_mixes
[] = {
3336 .access
= SNDRV_CTL_ELEM_ACCESS_READ
,
3337 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
3338 .name
= SNDRV_CTL_NAME_IEC958("", PLAYBACK
, CON_MASK
),
3339 .info
= snd_hda_spdif_mask_info
,
3340 .get
= snd_hda_spdif_cmask_get
,
3343 .access
= SNDRV_CTL_ELEM_ACCESS_READ
,
3344 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
3345 .name
= SNDRV_CTL_NAME_IEC958("", PLAYBACK
, PRO_MASK
),
3346 .info
= snd_hda_spdif_mask_info
,
3347 .get
= snd_hda_spdif_pmask_get
,
3350 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
3351 .name
= SNDRV_CTL_NAME_IEC958("", PLAYBACK
, DEFAULT
),
3352 .info
= snd_hda_spdif_mask_info
,
3353 .get
= snd_hda_spdif_default_get
,
3354 .put
= snd_hda_spdif_default_put
,
3357 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
3358 .name
= SNDRV_CTL_NAME_IEC958("", PLAYBACK
, SWITCH
),
3359 .info
= snd_hda_spdif_out_switch_info
,
3360 .get
= snd_hda_spdif_out_switch_get
,
3361 .put
= snd_hda_spdif_out_switch_put
,
3367 * snd_hda_create_dig_out_ctls - create Output SPDIF-related controls
3368 * @codec: the HDA codec
3369 * @associated_nid: NID that new ctls associated with
3370 * @cvt_nid: converter NID
3371 * @type: HDA_PCM_TYPE_*
3372 * Creates controls related with the digital output.
3373 * Called from each patch supporting the digital out.
3375 * Returns 0 if successful, or a negative error code.
3377 int snd_hda_create_dig_out_ctls(struct hda_codec
*codec
,
3378 hda_nid_t associated_nid
,
3383 struct snd_kcontrol
*kctl
;
3384 struct snd_kcontrol_new
*dig_mix
;
3386 const int spdif_index
= 16;
3387 struct hda_spdif_out
*spdif
;
3388 struct hda_bus
*bus
= codec
->bus
;
3390 if (bus
->primary_dig_out_type
== HDA_PCM_TYPE_HDMI
&&
3391 type
== HDA_PCM_TYPE_SPDIF
) {
3393 } else if (bus
->primary_dig_out_type
== HDA_PCM_TYPE_SPDIF
&&
3394 type
== HDA_PCM_TYPE_HDMI
) {
3395 /* suppose a single SPDIF device */
3396 for (dig_mix
= dig_mixes
; dig_mix
->name
; dig_mix
++) {
3397 kctl
= find_mixer_ctl(codec
, dig_mix
->name
, 0, 0);
3400 kctl
->id
.index
= spdif_index
;
3402 bus
->primary_dig_out_type
= HDA_PCM_TYPE_HDMI
;
3404 if (!bus
->primary_dig_out_type
)
3405 bus
->primary_dig_out_type
= type
;
3407 idx
= find_empty_mixer_ctl_idx(codec
, "IEC958 Playback Switch", idx
);
3409 printk(KERN_ERR
"hda_codec: too many IEC958 outputs\n");
3412 spdif
= snd_array_new(&codec
->spdif_out
);
3415 for (dig_mix
= dig_mixes
; dig_mix
->name
; dig_mix
++) {
3416 kctl
= snd_ctl_new1(dig_mix
, codec
);
3419 kctl
->id
.index
= idx
;
3420 kctl
->private_value
= codec
->spdif_out
.used
- 1;
3421 err
= snd_hda_ctl_add(codec
, associated_nid
, kctl
);
3425 spdif
->nid
= cvt_nid
;
3426 spdif
->ctls
= snd_hda_codec_read(codec
, cvt_nid
, 0,
3427 AC_VERB_GET_DIGI_CONVERT_1
, 0);
3428 spdif
->status
= convert_to_spdif_status(spdif
->ctls
);
3431 EXPORT_SYMBOL_HDA(snd_hda_create_dig_out_ctls
);
3433 /* get the hda_spdif_out entry from the given NID
3434 * call within spdif_mutex lock
3436 struct hda_spdif_out
*snd_hda_spdif_out_of_nid(struct hda_codec
*codec
,
3440 for (i
= 0; i
< codec
->spdif_out
.used
; i
++) {
3441 struct hda_spdif_out
*spdif
=
3442 snd_array_elem(&codec
->spdif_out
, i
);
3443 if (spdif
->nid
== nid
)
3448 EXPORT_SYMBOL_HDA(snd_hda_spdif_out_of_nid
);
3450 void snd_hda_spdif_ctls_unassign(struct hda_codec
*codec
, int idx
)
3452 struct hda_spdif_out
*spdif
;
3454 mutex_lock(&codec
->spdif_mutex
);
3455 spdif
= snd_array_elem(&codec
->spdif_out
, idx
);
3456 spdif
->nid
= (u16
)-1;
3457 mutex_unlock(&codec
->spdif_mutex
);
3459 EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_unassign
);
3461 void snd_hda_spdif_ctls_assign(struct hda_codec
*codec
, int idx
, hda_nid_t nid
)
3463 struct hda_spdif_out
*spdif
;
3466 mutex_lock(&codec
->spdif_mutex
);
3467 spdif
= snd_array_elem(&codec
->spdif_out
, idx
);
3468 if (spdif
->nid
!= nid
) {
3471 set_spdif_ctls(codec
, nid
, val
& 0xff, (val
>> 8) & 0xff);
3473 mutex_unlock(&codec
->spdif_mutex
);
3475 EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_assign
);
3478 * SPDIF sharing with analog output
3480 static int spdif_share_sw_get(struct snd_kcontrol
*kcontrol
,
3481 struct snd_ctl_elem_value
*ucontrol
)
3483 struct hda_multi_out
*mout
= snd_kcontrol_chip(kcontrol
);
3484 ucontrol
->value
.integer
.value
[0] = mout
->share_spdif
;
3488 static int spdif_share_sw_put(struct snd_kcontrol
*kcontrol
,
3489 struct snd_ctl_elem_value
*ucontrol
)
3491 struct hda_multi_out
*mout
= snd_kcontrol_chip(kcontrol
);
3492 mout
->share_spdif
= !!ucontrol
->value
.integer
.value
[0];
3496 static struct snd_kcontrol_new spdif_share_sw
= {
3497 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
3498 .name
= "IEC958 Default PCM Playback Switch",
3499 .info
= snd_ctl_boolean_mono_info
,
3500 .get
= spdif_share_sw_get
,
3501 .put
= spdif_share_sw_put
,
3505 * snd_hda_create_spdif_share_sw - create Default PCM switch
3506 * @codec: the HDA codec
3507 * @mout: multi-out instance
3509 int snd_hda_create_spdif_share_sw(struct hda_codec
*codec
,
3510 struct hda_multi_out
*mout
)
3512 struct snd_kcontrol
*kctl
;
3514 if (!mout
->dig_out_nid
)
3517 kctl
= snd_ctl_new1(&spdif_share_sw
, mout
);
3520 /* ATTENTION: here mout is passed as private_data, instead of codec */
3521 return snd_hda_ctl_add(codec
, mout
->dig_out_nid
, kctl
);
3523 EXPORT_SYMBOL_HDA(snd_hda_create_spdif_share_sw
);
3529 #define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
3531 static int snd_hda_spdif_in_switch_get(struct snd_kcontrol
*kcontrol
,
3532 struct snd_ctl_elem_value
*ucontrol
)
3534 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
3536 ucontrol
->value
.integer
.value
[0] = codec
->spdif_in_enable
;
3540 static int snd_hda_spdif_in_switch_put(struct snd_kcontrol
*kcontrol
,
3541 struct snd_ctl_elem_value
*ucontrol
)
3543 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
3544 hda_nid_t nid
= kcontrol
->private_value
;
3545 unsigned int val
= !!ucontrol
->value
.integer
.value
[0];
3548 mutex_lock(&codec
->spdif_mutex
);
3549 change
= codec
->spdif_in_enable
!= val
;
3551 codec
->spdif_in_enable
= val
;
3552 snd_hda_codec_write_cache(codec
, nid
, 0,
3553 AC_VERB_SET_DIGI_CONVERT_1
, val
);
3555 mutex_unlock(&codec
->spdif_mutex
);
3559 static int snd_hda_spdif_in_status_get(struct snd_kcontrol
*kcontrol
,
3560 struct snd_ctl_elem_value
*ucontrol
)
3562 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
3563 hda_nid_t nid
= kcontrol
->private_value
;
3567 val
= snd_hda_codec_read(codec
, nid
, 0, AC_VERB_GET_DIGI_CONVERT_1
, 0);
3568 sbits
= convert_to_spdif_status(val
);
3569 ucontrol
->value
.iec958
.status
[0] = sbits
;
3570 ucontrol
->value
.iec958
.status
[1] = sbits
>> 8;
3571 ucontrol
->value
.iec958
.status
[2] = sbits
>> 16;
3572 ucontrol
->value
.iec958
.status
[3] = sbits
>> 24;
3576 static struct snd_kcontrol_new dig_in_ctls
[] = {
3578 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
3579 .name
= SNDRV_CTL_NAME_IEC958("", CAPTURE
, SWITCH
),
3580 .info
= snd_hda_spdif_in_switch_info
,
3581 .get
= snd_hda_spdif_in_switch_get
,
3582 .put
= snd_hda_spdif_in_switch_put
,
3585 .access
= SNDRV_CTL_ELEM_ACCESS_READ
,
3586 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
3587 .name
= SNDRV_CTL_NAME_IEC958("", CAPTURE
, DEFAULT
),
3588 .info
= snd_hda_spdif_mask_info
,
3589 .get
= snd_hda_spdif_in_status_get
,
3595 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
3596 * @codec: the HDA codec
3597 * @nid: audio in widget NID
3599 * Creates controls related with the SPDIF input.
3600 * Called from each patch supporting the SPDIF in.
3602 * Returns 0 if successful, or a negative error code.
3604 int snd_hda_create_spdif_in_ctls(struct hda_codec
*codec
, hda_nid_t nid
)
3607 struct snd_kcontrol
*kctl
;
3608 struct snd_kcontrol_new
*dig_mix
;
3611 idx
= find_empty_mixer_ctl_idx(codec
, "IEC958 Capture Switch", 0);
3613 printk(KERN_ERR
"hda_codec: too many IEC958 inputs\n");
3616 for (dig_mix
= dig_in_ctls
; dig_mix
->name
; dig_mix
++) {
3617 kctl
= snd_ctl_new1(dig_mix
, codec
);
3620 kctl
->private_value
= nid
;
3621 err
= snd_hda_ctl_add(codec
, nid
, kctl
);
3625 codec
->spdif_in_enable
=
3626 snd_hda_codec_read(codec
, nid
, 0,
3627 AC_VERB_GET_DIGI_CONVERT_1
, 0) &
3631 EXPORT_SYMBOL_HDA(snd_hda_create_spdif_in_ctls
);
3637 /* build a 31bit cache key with the widget id and the command parameter */
3638 #define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
3639 #define get_cmd_cache_nid(key) ((key) & 0xff)
3640 #define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
3643 * snd_hda_codec_write_cache - send a single command with caching
3644 * @codec: the HDA codec
3645 * @nid: NID to send the command
3646 * @flags: optional bit flags
3647 * @verb: the verb to send
3648 * @parm: the parameter for the verb
3650 * Send a single command without waiting for response.
3652 * Returns 0 if successful, or a negative error code.
3654 int snd_hda_codec_write_cache(struct hda_codec
*codec
, hda_nid_t nid
,
3655 int flags
, unsigned int verb
, unsigned int parm
)
3658 struct hda_cache_head
*c
;
3660 unsigned int cache_only
;
3662 cache_only
= codec
->cached_write
;
3664 err
= snd_hda_codec_write(codec
, nid
, flags
, verb
, parm
);
3669 /* parm may contain the verb stuff for get/set amp */
3670 verb
= verb
| (parm
>> 8);
3672 key
= build_cmd_cache_key(nid
, verb
);
3673 mutex_lock(&codec
->bus
->cmd_mutex
);
3674 c
= get_alloc_hash(&codec
->cmd_cache
, key
);
3677 c
->dirty
= cache_only
;
3679 mutex_unlock(&codec
->bus
->cmd_mutex
);
3682 EXPORT_SYMBOL_HDA(snd_hda_codec_write_cache
);
3685 * snd_hda_codec_update_cache - check cache and write the cmd only when needed
3686 * @codec: the HDA codec
3687 * @nid: NID to send the command
3688 * @flags: optional bit flags
3689 * @verb: the verb to send
3690 * @parm: the parameter for the verb
3692 * This function works like snd_hda_codec_write_cache(), but it doesn't send
3693 * command if the parameter is already identical with the cached value.
3694 * If not, it sends the command and refreshes the cache.
3696 * Returns 0 if successful, or a negative error code.
3698 int snd_hda_codec_update_cache(struct hda_codec
*codec
, hda_nid_t nid
,
3699 int flags
, unsigned int verb
, unsigned int parm
)
3701 struct hda_cache_head
*c
;
3704 /* parm may contain the verb stuff for get/set amp */
3705 verb
= verb
| (parm
>> 8);
3707 key
= build_cmd_cache_key(nid
, verb
);
3708 mutex_lock(&codec
->bus
->cmd_mutex
);
3709 c
= get_hash(&codec
->cmd_cache
, key
);
3710 if (c
&& c
->val
== parm
) {
3711 mutex_unlock(&codec
->bus
->cmd_mutex
);
3714 mutex_unlock(&codec
->bus
->cmd_mutex
);
3715 return snd_hda_codec_write_cache(codec
, nid
, flags
, verb
, parm
);
3717 EXPORT_SYMBOL_HDA(snd_hda_codec_update_cache
);
3720 * snd_hda_codec_resume_cache - Resume the all commands from the cache
3721 * @codec: HD-audio codec
3723 * Execute all verbs recorded in the command caches to resume.
3725 void snd_hda_codec_resume_cache(struct hda_codec
*codec
)
3729 mutex_lock(&codec
->hash_mutex
);
3730 codec
->cached_write
= 0;
3731 for (i
= 0; i
< codec
->cmd_cache
.buf
.used
; i
++) {
3732 struct hda_cache_head
*buffer
;
3735 buffer
= snd_array_elem(&codec
->cmd_cache
.buf
, i
);
3742 mutex_unlock(&codec
->hash_mutex
);
3743 snd_hda_codec_write(codec
, get_cmd_cache_nid(key
), 0,
3744 get_cmd_cache_cmd(key
), buffer
->val
);
3745 mutex_lock(&codec
->hash_mutex
);
3747 mutex_unlock(&codec
->hash_mutex
);
3749 EXPORT_SYMBOL_HDA(snd_hda_codec_resume_cache
);
3752 * snd_hda_sequence_write_cache - sequence writes with caching
3753 * @codec: the HDA codec
3754 * @seq: VERB array to send
3756 * Send the commands sequentially from the given array.
3757 * Thte commands are recorded on cache for power-save and resume.
3758 * The array must be terminated with NID=0.
3760 void snd_hda_sequence_write_cache(struct hda_codec
*codec
,
3761 const struct hda_verb
*seq
)
3763 for (; seq
->nid
; seq
++)
3764 snd_hda_codec_write_cache(codec
, seq
->nid
, 0, seq
->verb
,
3767 EXPORT_SYMBOL_HDA(snd_hda_sequence_write_cache
);
3770 * snd_hda_codec_flush_cache - Execute all pending (cached) amps / verbs
3771 * @codec: HD-audio codec
3773 void snd_hda_codec_flush_cache(struct hda_codec
*codec
)
3775 snd_hda_codec_resume_amp(codec
);
3776 snd_hda_codec_resume_cache(codec
);
3778 EXPORT_SYMBOL_HDA(snd_hda_codec_flush_cache
);
3780 void snd_hda_codec_set_power_to_all(struct hda_codec
*codec
, hda_nid_t fg
,
3781 unsigned int power_state
)
3783 hda_nid_t nid
= codec
->start_nid
;
3786 for (i
= 0; i
< codec
->num_nodes
; i
++, nid
++) {
3787 unsigned int wcaps
= get_wcaps(codec
, nid
);
3788 unsigned int state
= power_state
;
3789 if (!(wcaps
& AC_WCAP_POWER
))
3791 if (codec
->power_filter
) {
3792 state
= codec
->power_filter(codec
, nid
, power_state
);
3793 if (state
!= power_state
&& power_state
== AC_PWRST_D3
)
3796 snd_hda_codec_write(codec
, nid
, 0, AC_VERB_SET_POWER_STATE
,
3800 EXPORT_SYMBOL_HDA(snd_hda_codec_set_power_to_all
);
3803 * supported power states check
3805 static bool snd_hda_codec_get_supported_ps(struct hda_codec
*codec
, hda_nid_t fg
,
3806 unsigned int power_state
)
3808 int sup
= snd_hda_param_read(codec
, fg
, AC_PAR_POWER_STATE
);
3812 if (sup
& power_state
)
3819 * wait until the state is reached, returns the current state
3821 static unsigned int hda_sync_power_state(struct hda_codec
*codec
,
3823 unsigned int power_state
)
3825 unsigned long end_time
= jiffies
+ msecs_to_jiffies(500);
3826 unsigned int state
, actual_state
;
3829 state
= snd_hda_codec_read(codec
, fg
, 0,
3830 AC_VERB_GET_POWER_STATE
, 0);
3831 if (state
& AC_PWRST_ERROR
)
3833 actual_state
= (state
>> 4) & 0x0f;
3834 if (actual_state
== power_state
)
3836 if (time_after_eq(jiffies
, end_time
))
3838 /* wait until the codec reachs to the target state */
3844 /* don't power down the widget if it controls eapd and EAPD_BTLENABLE is set */
3845 unsigned int snd_hda_codec_eapd_power_filter(struct hda_codec
*codec
,
3847 unsigned int power_state
)
3849 if (power_state
== AC_PWRST_D3
&&
3850 get_wcaps_type(get_wcaps(codec
, nid
)) == AC_WID_PIN
&&
3851 (snd_hda_query_pin_caps(codec
, nid
) & AC_PINCAP_EAPD
)) {
3852 int eapd
= snd_hda_codec_read(codec
, nid
, 0,
3853 AC_VERB_GET_EAPD_BTLENABLE
, 0);
3859 EXPORT_SYMBOL_HDA(snd_hda_codec_eapd_power_filter
);
3862 * set power state of the codec, and return the power state
3864 static unsigned int hda_set_power_state(struct hda_codec
*codec
,
3865 unsigned int power_state
)
3867 hda_nid_t fg
= codec
->afg
? codec
->afg
: codec
->mfg
;
3872 /* this delay seems necessary to avoid click noise at power-down */
3873 if (power_state
== AC_PWRST_D3
) {
3874 /* transition time less than 10ms for power down */
3875 msleep(codec
->epss
? 10 : 100);
3876 flags
= HDA_RW_NO_RESPONSE_FALLBACK
;
3879 /* repeat power states setting at most 10 times*/
3880 for (count
= 0; count
< 10; count
++) {
3881 if (codec
->patch_ops
.set_power_state
)
3882 codec
->patch_ops
.set_power_state(codec
, fg
,
3885 snd_hda_codec_read(codec
, fg
, flags
,
3886 AC_VERB_SET_POWER_STATE
,
3888 snd_hda_codec_set_power_to_all(codec
, fg
, power_state
);
3890 state
= hda_sync_power_state(codec
, fg
, power_state
);
3891 if (!(state
& AC_PWRST_ERROR
))
3898 /* sync power states of all widgets;
3899 * this is called at the end of codec parsing
3901 static void sync_power_up_states(struct hda_codec
*codec
)
3903 hda_nid_t nid
= codec
->start_nid
;
3906 /* don't care if no filter is used */
3907 if (!codec
->power_filter
)
3910 for (i
= 0; i
< codec
->num_nodes
; i
++, nid
++) {
3911 unsigned int wcaps
= get_wcaps(codec
, nid
);
3912 unsigned int target
;
3913 if (!(wcaps
& AC_WCAP_POWER
))
3915 target
= codec
->power_filter(codec
, nid
, AC_PWRST_D0
);
3916 if (target
== AC_PWRST_D0
)
3918 if (!snd_hda_check_power_state(codec
, nid
, target
))
3919 snd_hda_codec_write(codec
, nid
, 0,
3920 AC_VERB_SET_POWER_STATE
, target
);
3924 #ifdef CONFIG_SND_HDA_HWDEP
3925 /* execute additional init verbs */
3926 static void hda_exec_init_verbs(struct hda_codec
*codec
)
3928 if (codec
->init_verbs
.list
)
3929 snd_hda_sequence_write(codec
, codec
->init_verbs
.list
);
3932 static inline void hda_exec_init_verbs(struct hda_codec
*codec
) {}
3937 * call suspend and power-down; used both from PM and power-save
3938 * this function returns the power state in the end
3940 static unsigned int hda_call_codec_suspend(struct hda_codec
*codec
, bool in_wq
)
3946 if (codec
->patch_ops
.suspend
)
3947 codec
->patch_ops
.suspend(codec
);
3948 hda_cleanup_all_streams(codec
);
3949 state
= hda_set_power_state(codec
, AC_PWRST_D3
);
3950 /* Cancel delayed work if we aren't currently running from it. */
3952 cancel_delayed_work_sync(&codec
->power_work
);
3953 spin_lock(&codec
->power_lock
);
3954 snd_hda_update_power_acct(codec
);
3955 trace_hda_power_down(codec
);
3956 codec
->power_on
= 0;
3957 codec
->power_transition
= 0;
3958 codec
->power_jiffies
= jiffies
;
3959 spin_unlock(&codec
->power_lock
);
3964 /* mark all entries of cmd and amp caches dirty */
3965 static void hda_mark_cmd_cache_dirty(struct hda_codec
*codec
)
3968 for (i
= 0; i
< codec
->cmd_cache
.buf
.used
; i
++) {
3969 struct hda_cache_head
*cmd
;
3970 cmd
= snd_array_elem(&codec
->cmd_cache
.buf
, i
);
3973 for (i
= 0; i
< codec
->amp_cache
.buf
.used
; i
++) {
3974 struct hda_amp_info
*amp
;
3975 amp
= snd_array_elem(&codec
->amp_cache
.buf
, i
);
3976 amp
->head
.dirty
= 1;
3981 * kick up codec; used both from PM and power-save
3983 static void hda_call_codec_resume(struct hda_codec
*codec
)
3987 hda_mark_cmd_cache_dirty(codec
);
3989 /* set as if powered on for avoiding re-entering the resume
3990 * in the resume / power-save sequence
3992 hda_keep_power_on(codec
);
3993 if (codec
->pm_down_notified
) {
3994 codec
->pm_down_notified
= 0;
3995 hda_call_pm_notify(codec
->bus
, true);
3997 hda_set_power_state(codec
, AC_PWRST_D0
);
3998 restore_shutup_pins(codec
);
3999 hda_exec_init_verbs(codec
);
4000 snd_hda_jack_set_dirty_all(codec
);
4001 if (codec
->patch_ops
.resume
)
4002 codec
->patch_ops
.resume(codec
);
4004 if (codec
->patch_ops
.init
)
4005 codec
->patch_ops
.init(codec
);
4006 snd_hda_codec_resume_amp(codec
);
4007 snd_hda_codec_resume_cache(codec
);
4010 if (codec
->jackpoll_interval
)
4011 hda_jackpoll_work(&codec
->jackpoll_work
.work
);
4013 snd_hda_jack_report_sync(codec
);
4016 snd_hda_power_down(codec
); /* flag down before returning */
4018 #endif /* CONFIG_PM */
4022 * snd_hda_build_controls - build mixer controls
4025 * Creates mixer controls for each codec included in the bus.
4027 * Returns 0 if successful, otherwise a negative error code.
4029 int snd_hda_build_controls(struct hda_bus
*bus
)
4031 struct hda_codec
*codec
;
4033 list_for_each_entry(codec
, &bus
->codec_list
, list
) {
4034 int err
= snd_hda_codec_build_controls(codec
);
4036 printk(KERN_ERR
"hda_codec: cannot build controls "
4037 "for #%d (error %d)\n", codec
->addr
, err
);
4038 err
= snd_hda_codec_reset(codec
);
4041 "hda_codec: cannot revert codec\n");
4048 EXPORT_SYMBOL_HDA(snd_hda_build_controls
);
4051 * add standard channel maps if not specified
4053 static int add_std_chmaps(struct hda_codec
*codec
)
4057 for (i
= 0; i
< codec
->num_pcms
; i
++) {
4058 for (str
= 0; str
< 2; str
++) {
4059 struct snd_pcm
*pcm
= codec
->pcm_info
[i
].pcm
;
4060 struct hda_pcm_stream
*hinfo
=
4061 &codec
->pcm_info
[i
].stream
[str
];
4062 struct snd_pcm_chmap
*chmap
;
4063 const struct snd_pcm_chmap_elem
*elem
;
4065 if (codec
->pcm_info
[i
].own_chmap
)
4067 if (!pcm
|| !hinfo
->substreams
)
4069 elem
= hinfo
->chmap
? hinfo
->chmap
: snd_pcm_std_chmaps
;
4070 err
= snd_pcm_add_chmap_ctls(pcm
, str
, elem
,
4071 hinfo
->channels_max
,
4075 chmap
->channel_mask
= SND_PCM_CHMAP_MASK_2468
;
4081 /* default channel maps for 2.1 speakers;
4082 * since HD-audio supports only stereo, odd number channels are omitted
4084 const struct snd_pcm_chmap_elem snd_pcm_2_1_chmaps
[] = {
4086 .map
= { SNDRV_CHMAP_FL
, SNDRV_CHMAP_FR
} },
4088 .map
= { SNDRV_CHMAP_FL
, SNDRV_CHMAP_FR
,
4089 SNDRV_CHMAP_LFE
, SNDRV_CHMAP_LFE
} },
4092 EXPORT_SYMBOL_GPL(snd_pcm_2_1_chmaps
);
4094 int snd_hda_codec_build_controls(struct hda_codec
*codec
)
4097 hda_exec_init_verbs(codec
);
4098 /* continue to initialize... */
4099 if (codec
->patch_ops
.init
)
4100 err
= codec
->patch_ops
.init(codec
);
4101 if (!err
&& codec
->patch_ops
.build_controls
)
4102 err
= codec
->patch_ops
.build_controls(codec
);
4106 /* we create chmaps here instead of build_pcms */
4107 err
= add_std_chmaps(codec
);
4111 if (codec
->jackpoll_interval
)
4112 hda_jackpoll_work(&codec
->jackpoll_work
.work
);
4114 snd_hda_jack_report_sync(codec
); /* call at the last init point */
4115 sync_power_up_states(codec
);
4122 struct hda_rate_tbl
{
4124 unsigned int alsa_bits
;
4125 unsigned int hda_fmt
;
4128 /* rate = base * mult / div */
4129 #define HDA_RATE(base, mult, div) \
4130 (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \
4131 (((div) - 1) << AC_FMT_DIV_SHIFT))
4133 static struct hda_rate_tbl rate_bits
[] = {
4134 /* rate in Hz, ALSA rate bitmask, HDA format value */
4136 /* autodetected value used in snd_hda_query_supported_pcm */
4137 { 8000, SNDRV_PCM_RATE_8000
, HDA_RATE(48, 1, 6) },
4138 { 11025, SNDRV_PCM_RATE_11025
, HDA_RATE(44, 1, 4) },
4139 { 16000, SNDRV_PCM_RATE_16000
, HDA_RATE(48, 1, 3) },
4140 { 22050, SNDRV_PCM_RATE_22050
, HDA_RATE(44, 1, 2) },
4141 { 32000, SNDRV_PCM_RATE_32000
, HDA_RATE(48, 2, 3) },
4142 { 44100, SNDRV_PCM_RATE_44100
, HDA_RATE(44, 1, 1) },
4143 { 48000, SNDRV_PCM_RATE_48000
, HDA_RATE(48, 1, 1) },
4144 { 88200, SNDRV_PCM_RATE_88200
, HDA_RATE(44, 2, 1) },
4145 { 96000, SNDRV_PCM_RATE_96000
, HDA_RATE(48, 2, 1) },
4146 { 176400, SNDRV_PCM_RATE_176400
, HDA_RATE(44, 4, 1) },
4147 { 192000, SNDRV_PCM_RATE_192000
, HDA_RATE(48, 4, 1) },
4148 #define AC_PAR_PCM_RATE_BITS 11
4149 /* up to bits 10, 384kHZ isn't supported properly */
4151 /* not autodetected value */
4152 { 9600, SNDRV_PCM_RATE_KNOT
, HDA_RATE(48, 1, 5) },
4154 { 0 } /* terminator */
4158 * snd_hda_calc_stream_format - calculate format bitset
4159 * @rate: the sample rate
4160 * @channels: the number of channels
4161 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
4162 * @maxbps: the max. bps
4164 * Calculate the format bitset from the given rate, channels and th PCM format.
4166 * Return zero if invalid.
4168 unsigned int snd_hda_calc_stream_format(unsigned int rate
,
4169 unsigned int channels
,
4170 unsigned int format
,
4171 unsigned int maxbps
,
4172 unsigned short spdif_ctls
)
4175 unsigned int val
= 0;
4177 for (i
= 0; rate_bits
[i
].hz
; i
++)
4178 if (rate_bits
[i
].hz
== rate
) {
4179 val
= rate_bits
[i
].hda_fmt
;
4182 if (!rate_bits
[i
].hz
) {
4183 snd_printdd("invalid rate %d\n", rate
);
4187 if (channels
== 0 || channels
> 8) {
4188 snd_printdd("invalid channels %d\n", channels
);
4191 val
|= channels
- 1;
4193 switch (snd_pcm_format_width(format
)) {
4195 val
|= AC_FMT_BITS_8
;
4198 val
|= AC_FMT_BITS_16
;
4203 if (maxbps
>= 32 || format
== SNDRV_PCM_FORMAT_FLOAT_LE
)
4204 val
|= AC_FMT_BITS_32
;
4205 else if (maxbps
>= 24)
4206 val
|= AC_FMT_BITS_24
;
4208 val
|= AC_FMT_BITS_20
;
4211 snd_printdd("invalid format width %d\n",
4212 snd_pcm_format_width(format
));
4216 if (spdif_ctls
& AC_DIG1_NONAUDIO
)
4217 val
|= AC_FMT_TYPE_NON_PCM
;
4221 EXPORT_SYMBOL_HDA(snd_hda_calc_stream_format
);
4223 static unsigned int get_pcm_param(struct hda_codec
*codec
, hda_nid_t nid
,
4226 unsigned int val
= 0;
4227 if (nid
!= codec
->afg
&&
4228 (get_wcaps(codec
, nid
) & AC_WCAP_FORMAT_OVRD
))
4229 val
= snd_hda_param_read(codec
, nid
, AC_PAR_PCM
);
4230 if (!val
|| val
== -1)
4231 val
= snd_hda_param_read(codec
, codec
->afg
, AC_PAR_PCM
);
4232 if (!val
|| val
== -1)
4237 static unsigned int query_pcm_param(struct hda_codec
*codec
, hda_nid_t nid
)
4239 return query_caps_hash(codec
, nid
, 0, HDA_HASH_PARPCM_KEY(nid
),
4243 static unsigned int get_stream_param(struct hda_codec
*codec
, hda_nid_t nid
,
4246 unsigned int streams
= snd_hda_param_read(codec
, nid
, AC_PAR_STREAM
);
4247 if (!streams
|| streams
== -1)
4248 streams
= snd_hda_param_read(codec
, codec
->afg
, AC_PAR_STREAM
);
4249 if (!streams
|| streams
== -1)
4254 static unsigned int query_stream_param(struct hda_codec
*codec
, hda_nid_t nid
)
4256 return query_caps_hash(codec
, nid
, 0, HDA_HASH_PARSTR_KEY(nid
),
4261 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
4262 * @codec: the HDA codec
4263 * @nid: NID to query
4264 * @ratesp: the pointer to store the detected rate bitflags
4265 * @formatsp: the pointer to store the detected formats
4266 * @bpsp: the pointer to store the detected format widths
4268 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
4269 * or @bsps argument is ignored.
4271 * Returns 0 if successful, otherwise a negative error code.
4273 int snd_hda_query_supported_pcm(struct hda_codec
*codec
, hda_nid_t nid
,
4274 u32
*ratesp
, u64
*formatsp
, unsigned int *bpsp
)
4276 unsigned int i
, val
, wcaps
;
4278 wcaps
= get_wcaps(codec
, nid
);
4279 val
= query_pcm_param(codec
, nid
);
4283 for (i
= 0; i
< AC_PAR_PCM_RATE_BITS
; i
++) {
4285 rates
|= rate_bits
[i
].alsa_bits
;
4288 snd_printk(KERN_ERR
"hda_codec: rates == 0 "
4289 "(nid=0x%x, val=0x%x, ovrd=%i)\n",
4291 (wcaps
& AC_WCAP_FORMAT_OVRD
) ? 1 : 0);
4297 if (formatsp
|| bpsp
) {
4299 unsigned int streams
, bps
;
4301 streams
= query_stream_param(codec
, nid
);
4306 if (streams
& AC_SUPFMT_PCM
) {
4307 if (val
& AC_SUPPCM_BITS_8
) {
4308 formats
|= SNDRV_PCM_FMTBIT_U8
;
4311 if (val
& AC_SUPPCM_BITS_16
) {
4312 formats
|= SNDRV_PCM_FMTBIT_S16_LE
;
4315 if (wcaps
& AC_WCAP_DIGITAL
) {
4316 if (val
& AC_SUPPCM_BITS_32
)
4317 formats
|= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
;
4318 if (val
& (AC_SUPPCM_BITS_20
|AC_SUPPCM_BITS_24
))
4319 formats
|= SNDRV_PCM_FMTBIT_S32_LE
;
4320 if (val
& AC_SUPPCM_BITS_24
)
4322 else if (val
& AC_SUPPCM_BITS_20
)
4324 } else if (val
& (AC_SUPPCM_BITS_20
|AC_SUPPCM_BITS_24
|
4325 AC_SUPPCM_BITS_32
)) {
4326 formats
|= SNDRV_PCM_FMTBIT_S32_LE
;
4327 if (val
& AC_SUPPCM_BITS_32
)
4329 else if (val
& AC_SUPPCM_BITS_24
)
4331 else if (val
& AC_SUPPCM_BITS_20
)
4335 #if 0 /* FIXME: CS4206 doesn't work, which is the only codec supporting float */
4336 if (streams
& AC_SUPFMT_FLOAT32
) {
4337 formats
|= SNDRV_PCM_FMTBIT_FLOAT_LE
;
4342 if (streams
== AC_SUPFMT_AC3
) {
4343 /* should be exclusive */
4344 /* temporary hack: we have still no proper support
4345 * for the direct AC3 stream...
4347 formats
|= SNDRV_PCM_FMTBIT_U8
;
4351 snd_printk(KERN_ERR
"hda_codec: formats == 0 "
4352 "(nid=0x%x, val=0x%x, ovrd=%i, "
4355 (wcaps
& AC_WCAP_FORMAT_OVRD
) ? 1 : 0,
4360 *formatsp
= formats
;
4367 EXPORT_SYMBOL_HDA(snd_hda_query_supported_pcm
);
4370 * snd_hda_is_supported_format - Check the validity of the format
4371 * @codec: HD-audio codec
4372 * @nid: NID to check
4373 * @format: the HD-audio format value to check
4375 * Check whether the given node supports the format value.
4377 * Returns 1 if supported, 0 if not.
4379 int snd_hda_is_supported_format(struct hda_codec
*codec
, hda_nid_t nid
,
4380 unsigned int format
)
4383 unsigned int val
= 0, rate
, stream
;
4385 val
= query_pcm_param(codec
, nid
);
4389 rate
= format
& 0xff00;
4390 for (i
= 0; i
< AC_PAR_PCM_RATE_BITS
; i
++)
4391 if (rate_bits
[i
].hda_fmt
== rate
) {
4396 if (i
>= AC_PAR_PCM_RATE_BITS
)
4399 stream
= query_stream_param(codec
, nid
);
4403 if (stream
& AC_SUPFMT_PCM
) {
4404 switch (format
& 0xf0) {
4406 if (!(val
& AC_SUPPCM_BITS_8
))
4410 if (!(val
& AC_SUPPCM_BITS_16
))
4414 if (!(val
& AC_SUPPCM_BITS_20
))
4418 if (!(val
& AC_SUPPCM_BITS_24
))
4422 if (!(val
& AC_SUPPCM_BITS_32
))
4429 /* FIXME: check for float32 and AC3? */
4434 EXPORT_SYMBOL_HDA(snd_hda_is_supported_format
);
4439 static int hda_pcm_default_open_close(struct hda_pcm_stream
*hinfo
,
4440 struct hda_codec
*codec
,
4441 struct snd_pcm_substream
*substream
)
4446 static int hda_pcm_default_prepare(struct hda_pcm_stream
*hinfo
,
4447 struct hda_codec
*codec
,
4448 unsigned int stream_tag
,
4449 unsigned int format
,
4450 struct snd_pcm_substream
*substream
)
4452 snd_hda_codec_setup_stream(codec
, hinfo
->nid
, stream_tag
, 0, format
);
4456 static int hda_pcm_default_cleanup(struct hda_pcm_stream
*hinfo
,
4457 struct hda_codec
*codec
,
4458 struct snd_pcm_substream
*substream
)
4460 snd_hda_codec_cleanup_stream(codec
, hinfo
->nid
);
4464 static int set_pcm_default_values(struct hda_codec
*codec
,
4465 struct hda_pcm_stream
*info
)
4469 /* query support PCM information from the given NID */
4470 if (info
->nid
&& (!info
->rates
|| !info
->formats
)) {
4471 err
= snd_hda_query_supported_pcm(codec
, info
->nid
,
4472 info
->rates
? NULL
: &info
->rates
,
4473 info
->formats
? NULL
: &info
->formats
,
4474 info
->maxbps
? NULL
: &info
->maxbps
);
4478 if (info
->ops
.open
== NULL
)
4479 info
->ops
.open
= hda_pcm_default_open_close
;
4480 if (info
->ops
.close
== NULL
)
4481 info
->ops
.close
= hda_pcm_default_open_close
;
4482 if (info
->ops
.prepare
== NULL
) {
4483 if (snd_BUG_ON(!info
->nid
))
4485 info
->ops
.prepare
= hda_pcm_default_prepare
;
4487 if (info
->ops
.cleanup
== NULL
) {
4488 if (snd_BUG_ON(!info
->nid
))
4490 info
->ops
.cleanup
= hda_pcm_default_cleanup
;
4496 * codec prepare/cleanup entries
4498 int snd_hda_codec_prepare(struct hda_codec
*codec
,
4499 struct hda_pcm_stream
*hinfo
,
4500 unsigned int stream
,
4501 unsigned int format
,
4502 struct snd_pcm_substream
*substream
)
4505 mutex_lock(&codec
->bus
->prepare_mutex
);
4506 ret
= hinfo
->ops
.prepare(hinfo
, codec
, stream
, format
, substream
);
4508 purify_inactive_streams(codec
);
4509 mutex_unlock(&codec
->bus
->prepare_mutex
);
4512 EXPORT_SYMBOL_HDA(snd_hda_codec_prepare
);
4514 void snd_hda_codec_cleanup(struct hda_codec
*codec
,
4515 struct hda_pcm_stream
*hinfo
,
4516 struct snd_pcm_substream
*substream
)
4518 mutex_lock(&codec
->bus
->prepare_mutex
);
4519 hinfo
->ops
.cleanup(hinfo
, codec
, substream
);
4520 mutex_unlock(&codec
->bus
->prepare_mutex
);
4522 EXPORT_SYMBOL_HDA(snd_hda_codec_cleanup
);
4525 const char *snd_hda_pcm_type_name
[HDA_PCM_NTYPES
] = {
4526 "Audio", "SPDIF", "HDMI", "Modem"
4530 * get the empty PCM device number to assign
4532 static int get_empty_pcm_device(struct hda_bus
*bus
, unsigned int type
)
4534 /* audio device indices; not linear to keep compatibility */
4535 /* assigned to static slots up to dev#10; if more needed, assign
4536 * the later slot dynamically (when CONFIG_SND_DYNAMIC_MINORS=y)
4538 static int audio_idx
[HDA_PCM_NTYPES
][5] = {
4539 [HDA_PCM_TYPE_AUDIO
] = { 0, 2, 4, 5, -1 },
4540 [HDA_PCM_TYPE_SPDIF
] = { 1, -1 },
4541 [HDA_PCM_TYPE_HDMI
] = { 3, 7, 8, 9, -1 },
4542 [HDA_PCM_TYPE_MODEM
] = { 6, -1 },
4546 if (type
>= HDA_PCM_NTYPES
) {
4547 snd_printk(KERN_WARNING
"Invalid PCM type %d\n", type
);
4551 for (i
= 0; audio_idx
[type
][i
] >= 0; i
++) {
4552 #ifndef CONFIG_SND_DYNAMIC_MINORS
4553 if (audio_idx
[type
][i
] >= 8)
4556 if (!test_and_set_bit(audio_idx
[type
][i
], bus
->pcm_dev_bits
))
4557 return audio_idx
[type
][i
];
4560 #ifdef CONFIG_SND_DYNAMIC_MINORS
4561 /* non-fixed slots starting from 10 */
4562 for (i
= 10; i
< 32; i
++) {
4563 if (!test_and_set_bit(i
, bus
->pcm_dev_bits
))
4568 snd_printk(KERN_WARNING
"Too many %s devices\n",
4569 snd_hda_pcm_type_name
[type
]);
4570 #ifndef CONFIG_SND_DYNAMIC_MINORS
4571 snd_printk(KERN_WARNING
"Consider building the kernel with CONFIG_SND_DYNAMIC_MINORS=y\n");
4577 * attach a new PCM stream
4579 static int snd_hda_attach_pcm(struct hda_codec
*codec
, struct hda_pcm
*pcm
)
4581 struct hda_bus
*bus
= codec
->bus
;
4582 struct hda_pcm_stream
*info
;
4585 if (snd_BUG_ON(!pcm
->name
))
4587 for (stream
= 0; stream
< 2; stream
++) {
4588 info
= &pcm
->stream
[stream
];
4589 if (info
->substreams
) {
4590 err
= set_pcm_default_values(codec
, info
);
4595 return bus
->ops
.attach_pcm(bus
, codec
, pcm
);
4598 /* assign all PCMs of the given codec */
4599 int snd_hda_codec_build_pcms(struct hda_codec
*codec
)
4604 if (!codec
->num_pcms
) {
4605 if (!codec
->patch_ops
.build_pcms
)
4607 err
= codec
->patch_ops
.build_pcms(codec
);
4609 printk(KERN_ERR
"hda_codec: cannot build PCMs"
4610 "for #%d (error %d)\n", codec
->addr
, err
);
4611 err
= snd_hda_codec_reset(codec
);
4614 "hda_codec: cannot revert codec\n");
4619 for (pcm
= 0; pcm
< codec
->num_pcms
; pcm
++) {
4620 struct hda_pcm
*cpcm
= &codec
->pcm_info
[pcm
];
4623 if (!cpcm
->stream
[0].substreams
&& !cpcm
->stream
[1].substreams
)
4624 continue; /* no substreams assigned */
4627 dev
= get_empty_pcm_device(codec
->bus
, cpcm
->pcm_type
);
4629 continue; /* no fatal error */
4631 err
= snd_hda_attach_pcm(codec
, cpcm
);
4633 printk(KERN_ERR
"hda_codec: cannot attach "
4634 "PCM stream %d for codec #%d\n",
4636 continue; /* no fatal error */
4644 * snd_hda_build_pcms - build PCM information
4647 * Create PCM information for each codec included in the bus.
4649 * The build_pcms codec patch is requested to set up codec->num_pcms and
4650 * codec->pcm_info properly. The array is referred by the top-level driver
4651 * to create its PCM instances.
4652 * The allocated codec->pcm_info should be released in codec->patch_ops.free
4655 * At least, substreams, channels_min and channels_max must be filled for
4656 * each stream. substreams = 0 indicates that the stream doesn't exist.
4657 * When rates and/or formats are zero, the supported values are queried
4658 * from the given nid. The nid is used also by the default ops.prepare
4659 * and ops.cleanup callbacks.
4661 * The driver needs to call ops.open in its open callback. Similarly,
4662 * ops.close is supposed to be called in the close callback.
4663 * ops.prepare should be called in the prepare or hw_params callback
4664 * with the proper parameters for set up.
4665 * ops.cleanup should be called in hw_free for clean up of streams.
4667 * This function returns 0 if successful, or a negative error code.
4669 int snd_hda_build_pcms(struct hda_bus
*bus
)
4671 struct hda_codec
*codec
;
4673 list_for_each_entry(codec
, &bus
->codec_list
, list
) {
4674 int err
= snd_hda_codec_build_pcms(codec
);
4680 EXPORT_SYMBOL_HDA(snd_hda_build_pcms
);
4683 * snd_hda_check_board_config - compare the current codec with the config table
4684 * @codec: the HDA codec
4685 * @num_configs: number of config enums
4686 * @models: array of model name strings
4687 * @tbl: configuration table, terminated by null entries
4689 * Compares the modelname or PCI subsystem id of the current codec with the
4690 * given configuration table. If a matching entry is found, returns its
4691 * config value (supposed to be 0 or positive).
4693 * If no entries are matching, the function returns a negative value.
4695 int snd_hda_check_board_config(struct hda_codec
*codec
,
4696 int num_configs
, const char * const *models
,
4697 const struct snd_pci_quirk
*tbl
)
4699 if (codec
->modelname
&& models
) {
4701 for (i
= 0; i
< num_configs
; i
++) {
4703 !strcmp(codec
->modelname
, models
[i
])) {
4704 snd_printd(KERN_INFO
"hda_codec: model '%s' is "
4705 "selected\n", models
[i
]);
4711 if (!codec
->bus
->pci
|| !tbl
)
4714 tbl
= snd_pci_quirk_lookup(codec
->bus
->pci
, tbl
);
4717 if (tbl
->value
>= 0 && tbl
->value
< num_configs
) {
4718 #ifdef CONFIG_SND_DEBUG_VERBOSE
4720 const char *model
= NULL
;
4722 model
= models
[tbl
->value
];
4724 sprintf(tmp
, "#%d", tbl
->value
);
4727 snd_printdd(KERN_INFO
"hda_codec: model '%s' is selected "
4728 "for config %x:%x (%s)\n",
4729 model
, tbl
->subvendor
, tbl
->subdevice
,
4730 (tbl
->name
? tbl
->name
: "Unknown device"));
4736 EXPORT_SYMBOL_HDA(snd_hda_check_board_config
);
4739 * snd_hda_check_board_codec_sid_config - compare the current codec
4740 subsystem ID with the
4743 This is important for Gateway notebooks with SB450 HDA Audio
4744 where the vendor ID of the PCI device is:
4745 ATI Technologies Inc SB450 HDA Audio [1002:437b]
4746 and the vendor/subvendor are found only at the codec.
4748 * @codec: the HDA codec
4749 * @num_configs: number of config enums
4750 * @models: array of model name strings
4751 * @tbl: configuration table, terminated by null entries
4753 * Compares the modelname or PCI subsystem id of the current codec with the
4754 * given configuration table. If a matching entry is found, returns its
4755 * config value (supposed to be 0 or positive).
4757 * If no entries are matching, the function returns a negative value.
4759 int snd_hda_check_board_codec_sid_config(struct hda_codec
*codec
,
4760 int num_configs
, const char * const *models
,
4761 const struct snd_pci_quirk
*tbl
)
4763 const struct snd_pci_quirk
*q
;
4765 /* Search for codec ID */
4766 for (q
= tbl
; q
->subvendor
; q
++) {
4767 unsigned int mask
= 0xffff0000 | q
->subdevice_mask
;
4768 unsigned int id
= (q
->subdevice
| (q
->subvendor
<< 16)) & mask
;
4769 if ((codec
->subsystem_id
& mask
) == id
)
4778 if (tbl
->value
>= 0 && tbl
->value
< num_configs
) {
4779 #ifdef CONFIG_SND_DEBUG_VERBOSE
4781 const char *model
= NULL
;
4783 model
= models
[tbl
->value
];
4785 sprintf(tmp
, "#%d", tbl
->value
);
4788 snd_printdd(KERN_INFO
"hda_codec: model '%s' is selected "
4789 "for config %x:%x (%s)\n",
4790 model
, tbl
->subvendor
, tbl
->subdevice
,
4791 (tbl
->name
? tbl
->name
: "Unknown device"));
4797 EXPORT_SYMBOL_HDA(snd_hda_check_board_codec_sid_config
);
4800 * snd_hda_add_new_ctls - create controls from the array
4801 * @codec: the HDA codec
4802 * @knew: the array of struct snd_kcontrol_new
4804 * This helper function creates and add new controls in the given array.
4805 * The array must be terminated with an empty entry as terminator.
4807 * Returns 0 if successful, or a negative error code.
4809 int snd_hda_add_new_ctls(struct hda_codec
*codec
,
4810 const struct snd_kcontrol_new
*knew
)
4814 for (; knew
->name
; knew
++) {
4815 struct snd_kcontrol
*kctl
;
4816 int addr
= 0, idx
= 0;
4817 if (knew
->iface
== -1) /* skip this codec private value */
4820 kctl
= snd_ctl_new1(knew
, codec
);
4824 kctl
->id
.device
= addr
;
4826 kctl
->id
.index
= idx
;
4827 err
= snd_hda_ctl_add(codec
, 0, kctl
);
4830 /* try first with another device index corresponding to
4831 * the codec addr; if it still fails (or it's the
4832 * primary codec), then try another control index
4834 if (!addr
&& codec
->addr
)
4836 else if (!idx
&& !knew
->index
) {
4837 idx
= find_empty_mixer_ctl_idx(codec
,
4847 EXPORT_SYMBOL_HDA(snd_hda_add_new_ctls
);
4850 static void hda_power_work(struct work_struct
*work
)
4852 struct hda_codec
*codec
=
4853 container_of(work
, struct hda_codec
, power_work
.work
);
4854 struct hda_bus
*bus
= codec
->bus
;
4857 spin_lock(&codec
->power_lock
);
4858 if (codec
->power_transition
> 0) { /* during power-up sequence? */
4859 spin_unlock(&codec
->power_lock
);
4862 if (!codec
->power_on
|| codec
->power_count
) {
4863 codec
->power_transition
= 0;
4864 spin_unlock(&codec
->power_lock
);
4867 spin_unlock(&codec
->power_lock
);
4869 state
= hda_call_codec_suspend(codec
, true);
4870 if (!codec
->pm_down_notified
&&
4871 !bus
->power_keep_link_on
&& (state
& AC_PWRST_CLK_STOP_OK
)) {
4872 codec
->pm_down_notified
= 1;
4873 hda_call_pm_notify(bus
, false);
4877 static void hda_keep_power_on(struct hda_codec
*codec
)
4879 spin_lock(&codec
->power_lock
);
4880 codec
->power_count
++;
4881 codec
->power_on
= 1;
4882 codec
->power_jiffies
= jiffies
;
4883 spin_unlock(&codec
->power_lock
);
4886 /* update the power on/off account with the current jiffies */
4887 void snd_hda_update_power_acct(struct hda_codec
*codec
)
4889 unsigned long delta
= jiffies
- codec
->power_jiffies
;
4890 if (codec
->power_on
)
4891 codec
->power_on_acct
+= delta
;
4893 codec
->power_off_acct
+= delta
;
4894 codec
->power_jiffies
+= delta
;
4897 /* Transition to powered up, if wait_power_down then wait for a pending
4898 * transition to D3 to complete. A pending D3 transition is indicated
4899 * with power_transition == -1. */
4900 /* call this with codec->power_lock held! */
4901 static void __snd_hda_power_up(struct hda_codec
*codec
, bool wait_power_down
)
4903 struct hda_bus
*bus
= codec
->bus
;
4905 /* Return if power_on or transitioning to power_on, unless currently
4907 if ((codec
->power_on
|| codec
->power_transition
> 0) &&
4908 !(wait_power_down
&& codec
->power_transition
< 0))
4910 spin_unlock(&codec
->power_lock
);
4912 cancel_delayed_work_sync(&codec
->power_work
);
4914 spin_lock(&codec
->power_lock
);
4915 /* If the power down delayed work was cancelled above before starting,
4916 * then there is no need to go through power up here.
4918 if (codec
->power_on
) {
4919 if (codec
->power_transition
< 0)
4920 codec
->power_transition
= 0;
4924 trace_hda_power_up(codec
);
4925 snd_hda_update_power_acct(codec
);
4926 codec
->power_on
= 1;
4927 codec
->power_jiffies
= jiffies
;
4928 codec
->power_transition
= 1; /* avoid reentrance */
4929 spin_unlock(&codec
->power_lock
);
4931 if (codec
->pm_down_notified
) {
4932 codec
->pm_down_notified
= 0;
4933 hda_call_pm_notify(bus
, true);
4936 hda_call_codec_resume(codec
);
4938 spin_lock(&codec
->power_lock
);
4939 codec
->power_transition
= 0;
4942 #define power_save(codec) \
4943 ((codec)->bus->power_save ? *(codec)->bus->power_save : 0)
4945 /* Transition to powered down */
4946 static void __snd_hda_power_down(struct hda_codec
*codec
)
4948 if (!codec
->power_on
|| codec
->power_count
|| codec
->power_transition
)
4951 if (power_save(codec
)) {
4952 codec
->power_transition
= -1; /* avoid reentrance */
4953 queue_delayed_work(codec
->bus
->workq
, &codec
->power_work
,
4954 msecs_to_jiffies(power_save(codec
) * 1000));
4959 * snd_hda_power_save - Power-up/down/sync the codec
4960 * @codec: HD-audio codec
4961 * @delta: the counter delta to change
4963 * Change the power-up counter via @delta, and power up or down the hardware
4964 * appropriately. For the power-down, queue to the delayed action.
4965 * Passing zero to @delta means to synchronize the power state.
4967 void snd_hda_power_save(struct hda_codec
*codec
, int delta
, bool d3wait
)
4969 spin_lock(&codec
->power_lock
);
4970 codec
->power_count
+= delta
;
4971 trace_hda_power_count(codec
);
4973 __snd_hda_power_up(codec
, d3wait
);
4975 __snd_hda_power_down(codec
);
4976 spin_unlock(&codec
->power_lock
);
4978 EXPORT_SYMBOL_HDA(snd_hda_power_save
);
4981 * snd_hda_check_amp_list_power - Check the amp list and update the power
4982 * @codec: HD-audio codec
4983 * @check: the object containing an AMP list and the status
4984 * @nid: NID to check / update
4986 * Check whether the given NID is in the amp list. If it's in the list,
4987 * check the current AMP status, and update the the power-status according
4988 * to the mute status.
4990 * This function is supposed to be set or called from the check_power_status
4993 int snd_hda_check_amp_list_power(struct hda_codec
*codec
,
4994 struct hda_loopback_check
*check
,
4997 const struct hda_amp_list
*p
;
5000 if (!check
->amplist
)
5002 for (p
= check
->amplist
; p
->nid
; p
++) {
5007 return 0; /* nothing changed */
5009 for (p
= check
->amplist
; p
->nid
; p
++) {
5010 for (ch
= 0; ch
< 2; ch
++) {
5011 v
= snd_hda_codec_amp_read(codec
, p
->nid
, ch
, p
->dir
,
5013 if (!(v
& HDA_AMP_MUTE
) && v
> 0) {
5014 if (!check
->power_on
) {
5015 check
->power_on
= 1;
5016 snd_hda_power_up(codec
);
5022 if (check
->power_on
) {
5023 check
->power_on
= 0;
5024 snd_hda_power_down(codec
);
5028 EXPORT_SYMBOL_HDA(snd_hda_check_amp_list_power
);
5032 * Channel mode helper
5036 * snd_hda_ch_mode_info - Info callback helper for the channel mode enum
5038 int snd_hda_ch_mode_info(struct hda_codec
*codec
,
5039 struct snd_ctl_elem_info
*uinfo
,
5040 const struct hda_channel_mode
*chmode
,
5043 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_ENUMERATED
;
5045 uinfo
->value
.enumerated
.items
= num_chmodes
;
5046 if (uinfo
->value
.enumerated
.item
>= num_chmodes
)
5047 uinfo
->value
.enumerated
.item
= num_chmodes
- 1;
5048 sprintf(uinfo
->value
.enumerated
.name
, "%dch",
5049 chmode
[uinfo
->value
.enumerated
.item
].channels
);
5052 EXPORT_SYMBOL_HDA(snd_hda_ch_mode_info
);
5055 * snd_hda_ch_mode_get - Get callback helper for the channel mode enum
5057 int snd_hda_ch_mode_get(struct hda_codec
*codec
,
5058 struct snd_ctl_elem_value
*ucontrol
,
5059 const struct hda_channel_mode
*chmode
,
5065 for (i
= 0; i
< num_chmodes
; i
++) {
5066 if (max_channels
== chmode
[i
].channels
) {
5067 ucontrol
->value
.enumerated
.item
[0] = i
;
5073 EXPORT_SYMBOL_HDA(snd_hda_ch_mode_get
);
5076 * snd_hda_ch_mode_put - Put callback helper for the channel mode enum
5078 int snd_hda_ch_mode_put(struct hda_codec
*codec
,
5079 struct snd_ctl_elem_value
*ucontrol
,
5080 const struct hda_channel_mode
*chmode
,
5086 mode
= ucontrol
->value
.enumerated
.item
[0];
5087 if (mode
>= num_chmodes
)
5089 if (*max_channelsp
== chmode
[mode
].channels
)
5091 /* change the current channel setting */
5092 *max_channelsp
= chmode
[mode
].channels
;
5093 if (chmode
[mode
].sequence
)
5094 snd_hda_sequence_write_cache(codec
, chmode
[mode
].sequence
);
5097 EXPORT_SYMBOL_HDA(snd_hda_ch_mode_put
);
5104 * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum
5106 int snd_hda_input_mux_info(const struct hda_input_mux
*imux
,
5107 struct snd_ctl_elem_info
*uinfo
)
5111 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_ENUMERATED
;
5113 uinfo
->value
.enumerated
.items
= imux
->num_items
;
5114 if (!imux
->num_items
)
5116 index
= uinfo
->value
.enumerated
.item
;
5117 if (index
>= imux
->num_items
)
5118 index
= imux
->num_items
- 1;
5119 strcpy(uinfo
->value
.enumerated
.name
, imux
->items
[index
].label
);
5122 EXPORT_SYMBOL_HDA(snd_hda_input_mux_info
);
5125 * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum
5127 int snd_hda_input_mux_put(struct hda_codec
*codec
,
5128 const struct hda_input_mux
*imux
,
5129 struct snd_ctl_elem_value
*ucontrol
,
5131 unsigned int *cur_val
)
5135 if (!imux
->num_items
)
5137 idx
= ucontrol
->value
.enumerated
.item
[0];
5138 if (idx
>= imux
->num_items
)
5139 idx
= imux
->num_items
- 1;
5140 if (*cur_val
== idx
)
5142 snd_hda_codec_write_cache(codec
, nid
, 0, AC_VERB_SET_CONNECT_SEL
,
5143 imux
->items
[idx
].index
);
5147 EXPORT_SYMBOL_HDA(snd_hda_input_mux_put
);
5151 * process kcontrol info callback of a simple string enum array
5152 * when @num_items is 0 or @texts is NULL, assume a boolean enum array
5154 int snd_hda_enum_helper_info(struct snd_kcontrol
*kcontrol
,
5155 struct snd_ctl_elem_info
*uinfo
,
5156 int num_items
, const char * const *texts
)
5158 static const char * const texts_default
[] = {
5159 "Disabled", "Enabled"
5162 if (!texts
|| !num_items
) {
5164 texts
= texts_default
;
5167 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_ENUMERATED
;
5169 uinfo
->value
.enumerated
.items
= num_items
;
5170 if (uinfo
->value
.enumerated
.item
>= uinfo
->value
.enumerated
.items
)
5171 uinfo
->value
.enumerated
.item
= uinfo
->value
.enumerated
.items
- 1;
5172 strcpy(uinfo
->value
.enumerated
.name
,
5173 texts
[uinfo
->value
.enumerated
.item
]);
5176 EXPORT_SYMBOL_HDA(snd_hda_enum_helper_info
);
5179 * Multi-channel / digital-out PCM helper functions
5182 /* setup SPDIF output stream */
5183 static void setup_dig_out_stream(struct hda_codec
*codec
, hda_nid_t nid
,
5184 unsigned int stream_tag
, unsigned int format
)
5186 struct hda_spdif_out
*spdif
;
5187 unsigned int curr_fmt
;
5190 spdif
= snd_hda_spdif_out_of_nid(codec
, nid
);
5191 curr_fmt
= snd_hda_codec_read(codec
, nid
, 0,
5192 AC_VERB_GET_STREAM_FORMAT
, 0);
5193 reset
= codec
->spdif_status_reset
&&
5194 (spdif
->ctls
& AC_DIG1_ENABLE
) &&
5197 /* turn off SPDIF if needed; otherwise the IEC958 bits won't be
5200 set_dig_out_convert(codec
, nid
,
5201 spdif
->ctls
& ~AC_DIG1_ENABLE
& 0xff,
5203 snd_hda_codec_setup_stream(codec
, nid
, stream_tag
, 0, format
);
5204 if (codec
->slave_dig_outs
) {
5206 for (d
= codec
->slave_dig_outs
; *d
; d
++)
5207 snd_hda_codec_setup_stream(codec
, *d
, stream_tag
, 0,
5210 /* turn on again (if needed) */
5212 set_dig_out_convert(codec
, nid
,
5213 spdif
->ctls
& 0xff, -1);
5216 static void cleanup_dig_out_stream(struct hda_codec
*codec
, hda_nid_t nid
)
5218 snd_hda_codec_cleanup_stream(codec
, nid
);
5219 if (codec
->slave_dig_outs
) {
5221 for (d
= codec
->slave_dig_outs
; *d
; d
++)
5222 snd_hda_codec_cleanup_stream(codec
, *d
);
5227 * snd_hda_bus_reboot_notify - call the reboot notifier of each codec
5228 * @bus: HD-audio bus
5230 void snd_hda_bus_reboot_notify(struct hda_bus
*bus
)
5232 struct hda_codec
*codec
;
5236 list_for_each_entry(codec
, &bus
->codec_list
, list
) {
5237 if (hda_codec_is_power_on(codec
) &&
5238 codec
->patch_ops
.reboot_notify
)
5239 codec
->patch_ops
.reboot_notify(codec
);
5242 EXPORT_SYMBOL_HDA(snd_hda_bus_reboot_notify
);
5245 * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode
5247 int snd_hda_multi_out_dig_open(struct hda_codec
*codec
,
5248 struct hda_multi_out
*mout
)
5250 mutex_lock(&codec
->spdif_mutex
);
5251 if (mout
->dig_out_used
== HDA_DIG_ANALOG_DUP
)
5252 /* already opened as analog dup; reset it once */
5253 cleanup_dig_out_stream(codec
, mout
->dig_out_nid
);
5254 mout
->dig_out_used
= HDA_DIG_EXCLUSIVE
;
5255 mutex_unlock(&codec
->spdif_mutex
);
5258 EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_open
);
5261 * snd_hda_multi_out_dig_prepare - prepare the digital out stream
5263 int snd_hda_multi_out_dig_prepare(struct hda_codec
*codec
,
5264 struct hda_multi_out
*mout
,
5265 unsigned int stream_tag
,
5266 unsigned int format
,
5267 struct snd_pcm_substream
*substream
)
5269 mutex_lock(&codec
->spdif_mutex
);
5270 setup_dig_out_stream(codec
, mout
->dig_out_nid
, stream_tag
, format
);
5271 mutex_unlock(&codec
->spdif_mutex
);
5274 EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_prepare
);
5277 * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream
5279 int snd_hda_multi_out_dig_cleanup(struct hda_codec
*codec
,
5280 struct hda_multi_out
*mout
)
5282 mutex_lock(&codec
->spdif_mutex
);
5283 cleanup_dig_out_stream(codec
, mout
->dig_out_nid
);
5284 mutex_unlock(&codec
->spdif_mutex
);
5287 EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_cleanup
);
5290 * snd_hda_multi_out_dig_close - release the digital out stream
5292 int snd_hda_multi_out_dig_close(struct hda_codec
*codec
,
5293 struct hda_multi_out
*mout
)
5295 mutex_lock(&codec
->spdif_mutex
);
5296 mout
->dig_out_used
= 0;
5297 mutex_unlock(&codec
->spdif_mutex
);
5300 EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_close
);
5303 * snd_hda_multi_out_analog_open - open analog outputs
5305 * Open analog outputs and set up the hw-constraints.
5306 * If the digital outputs can be opened as slave, open the digital
5309 int snd_hda_multi_out_analog_open(struct hda_codec
*codec
,
5310 struct hda_multi_out
*mout
,
5311 struct snd_pcm_substream
*substream
,
5312 struct hda_pcm_stream
*hinfo
)
5314 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
5315 runtime
->hw
.channels_max
= mout
->max_channels
;
5316 if (mout
->dig_out_nid
) {
5317 if (!mout
->analog_rates
) {
5318 mout
->analog_rates
= hinfo
->rates
;
5319 mout
->analog_formats
= hinfo
->formats
;
5320 mout
->analog_maxbps
= hinfo
->maxbps
;
5322 runtime
->hw
.rates
= mout
->analog_rates
;
5323 runtime
->hw
.formats
= mout
->analog_formats
;
5324 hinfo
->maxbps
= mout
->analog_maxbps
;
5326 if (!mout
->spdif_rates
) {
5327 snd_hda_query_supported_pcm(codec
, mout
->dig_out_nid
,
5329 &mout
->spdif_formats
,
5330 &mout
->spdif_maxbps
);
5332 mutex_lock(&codec
->spdif_mutex
);
5333 if (mout
->share_spdif
) {
5334 if ((runtime
->hw
.rates
& mout
->spdif_rates
) &&
5335 (runtime
->hw
.formats
& mout
->spdif_formats
)) {
5336 runtime
->hw
.rates
&= mout
->spdif_rates
;
5337 runtime
->hw
.formats
&= mout
->spdif_formats
;
5338 if (mout
->spdif_maxbps
< hinfo
->maxbps
)
5339 hinfo
->maxbps
= mout
->spdif_maxbps
;
5341 mout
->share_spdif
= 0;
5342 /* FIXME: need notify? */
5345 mutex_unlock(&codec
->spdif_mutex
);
5347 return snd_pcm_hw_constraint_step(substream
->runtime
, 0,
5348 SNDRV_PCM_HW_PARAM_CHANNELS
, 2);
5350 EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_open
);
5353 * snd_hda_multi_out_analog_prepare - Preapre the analog outputs.
5355 * Set up the i/o for analog out.
5356 * When the digital out is available, copy the front out to digital out, too.
5358 int snd_hda_multi_out_analog_prepare(struct hda_codec
*codec
,
5359 struct hda_multi_out
*mout
,
5360 unsigned int stream_tag
,
5361 unsigned int format
,
5362 struct snd_pcm_substream
*substream
)
5364 const hda_nid_t
*nids
= mout
->dac_nids
;
5365 int chs
= substream
->runtime
->channels
;
5366 struct hda_spdif_out
*spdif
;
5369 mutex_lock(&codec
->spdif_mutex
);
5370 spdif
= snd_hda_spdif_out_of_nid(codec
, mout
->dig_out_nid
);
5371 if (mout
->dig_out_nid
&& mout
->share_spdif
&&
5372 mout
->dig_out_used
!= HDA_DIG_EXCLUSIVE
) {
5374 snd_hda_is_supported_format(codec
, mout
->dig_out_nid
,
5376 !(spdif
->status
& IEC958_AES0_NONAUDIO
)) {
5377 mout
->dig_out_used
= HDA_DIG_ANALOG_DUP
;
5378 setup_dig_out_stream(codec
, mout
->dig_out_nid
,
5379 stream_tag
, format
);
5381 mout
->dig_out_used
= 0;
5382 cleanup_dig_out_stream(codec
, mout
->dig_out_nid
);
5385 mutex_unlock(&codec
->spdif_mutex
);
5388 snd_hda_codec_setup_stream(codec
, nids
[HDA_FRONT
], stream_tag
,
5390 if (!mout
->no_share_stream
&&
5391 mout
->hp_nid
&& mout
->hp_nid
!= nids
[HDA_FRONT
])
5392 /* headphone out will just decode front left/right (stereo) */
5393 snd_hda_codec_setup_stream(codec
, mout
->hp_nid
, stream_tag
,
5395 /* extra outputs copied from front */
5396 for (i
= 0; i
< ARRAY_SIZE(mout
->hp_out_nid
); i
++)
5397 if (!mout
->no_share_stream
&& mout
->hp_out_nid
[i
])
5398 snd_hda_codec_setup_stream(codec
,
5399 mout
->hp_out_nid
[i
],
5400 stream_tag
, 0, format
);
5401 for (i
= 0; i
< ARRAY_SIZE(mout
->extra_out_nid
); i
++)
5402 if (!mout
->no_share_stream
&& mout
->extra_out_nid
[i
])
5403 snd_hda_codec_setup_stream(codec
,
5404 mout
->extra_out_nid
[i
],
5405 stream_tag
, 0, format
);
5408 for (i
= 1; i
< mout
->num_dacs
; i
++) {
5409 if (chs
>= (i
+ 1) * 2) /* independent out */
5410 snd_hda_codec_setup_stream(codec
, nids
[i
], stream_tag
,
5412 else if (!mout
->no_share_stream
) /* copy front */
5413 snd_hda_codec_setup_stream(codec
, nids
[i
], stream_tag
,
5418 EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_prepare
);
5421 * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out
5423 int snd_hda_multi_out_analog_cleanup(struct hda_codec
*codec
,
5424 struct hda_multi_out
*mout
)
5426 const hda_nid_t
*nids
= mout
->dac_nids
;
5429 for (i
= 0; i
< mout
->num_dacs
; i
++)
5430 snd_hda_codec_cleanup_stream(codec
, nids
[i
]);
5432 snd_hda_codec_cleanup_stream(codec
, mout
->hp_nid
);
5433 for (i
= 0; i
< ARRAY_SIZE(mout
->hp_out_nid
); i
++)
5434 if (mout
->hp_out_nid
[i
])
5435 snd_hda_codec_cleanup_stream(codec
,
5436 mout
->hp_out_nid
[i
]);
5437 for (i
= 0; i
< ARRAY_SIZE(mout
->extra_out_nid
); i
++)
5438 if (mout
->extra_out_nid
[i
])
5439 snd_hda_codec_cleanup_stream(codec
,
5440 mout
->extra_out_nid
[i
]);
5441 mutex_lock(&codec
->spdif_mutex
);
5442 if (mout
->dig_out_nid
&& mout
->dig_out_used
== HDA_DIG_ANALOG_DUP
) {
5443 cleanup_dig_out_stream(codec
, mout
->dig_out_nid
);
5444 mout
->dig_out_used
= 0;
5446 mutex_unlock(&codec
->spdif_mutex
);
5449 EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_cleanup
);
5452 * snd_hda_get_default_vref - Get the default (mic) VREF pin bits
5454 * Guess the suitable VREF pin bits to be set as the pin-control value.
5455 * Note: the function doesn't set the AC_PINCTL_IN_EN bit.
5457 unsigned int snd_hda_get_default_vref(struct hda_codec
*codec
, hda_nid_t pin
)
5459 unsigned int pincap
;
5460 unsigned int oldval
;
5461 oldval
= snd_hda_codec_read(codec
, pin
, 0,
5462 AC_VERB_GET_PIN_WIDGET_CONTROL
, 0);
5463 pincap
= snd_hda_query_pin_caps(codec
, pin
);
5464 pincap
= (pincap
& AC_PINCAP_VREF
) >> AC_PINCAP_VREF_SHIFT
;
5465 /* Exception: if the default pin setup is vref50, we give it priority */
5466 if ((pincap
& AC_PINCAP_VREF_80
) && oldval
!= PIN_VREF50
)
5467 return AC_PINCTL_VREF_80
;
5468 else if (pincap
& AC_PINCAP_VREF_50
)
5469 return AC_PINCTL_VREF_50
;
5470 else if (pincap
& AC_PINCAP_VREF_100
)
5471 return AC_PINCTL_VREF_100
;
5472 else if (pincap
& AC_PINCAP_VREF_GRD
)
5473 return AC_PINCTL_VREF_GRD
;
5474 return AC_PINCTL_VREF_HIZ
;
5476 EXPORT_SYMBOL_HDA(snd_hda_get_default_vref
);
5478 /* correct the pin ctl value for matching with the pin cap */
5479 unsigned int snd_hda_correct_pin_ctl(struct hda_codec
*codec
,
5480 hda_nid_t pin
, unsigned int val
)
5482 static unsigned int cap_lists
[][2] = {
5483 { AC_PINCTL_VREF_100
, AC_PINCAP_VREF_100
},
5484 { AC_PINCTL_VREF_80
, AC_PINCAP_VREF_80
},
5485 { AC_PINCTL_VREF_50
, AC_PINCAP_VREF_50
},
5486 { AC_PINCTL_VREF_GRD
, AC_PINCAP_VREF_GRD
},
5492 cap
= snd_hda_query_pin_caps(codec
, pin
);
5494 return val
; /* don't know what to do... */
5496 if (val
& AC_PINCTL_OUT_EN
) {
5497 if (!(cap
& AC_PINCAP_OUT
))
5498 val
&= ~(AC_PINCTL_OUT_EN
| AC_PINCTL_HP_EN
);
5499 else if ((val
& AC_PINCTL_HP_EN
) && !(cap
& AC_PINCAP_HP_DRV
))
5500 val
&= ~AC_PINCTL_HP_EN
;
5503 if (val
& AC_PINCTL_IN_EN
) {
5504 if (!(cap
& AC_PINCAP_IN
))
5505 val
&= ~(AC_PINCTL_IN_EN
| AC_PINCTL_VREFEN
);
5507 unsigned int vcap
, vref
;
5509 vcap
= (cap
& AC_PINCAP_VREF
) >> AC_PINCAP_VREF_SHIFT
;
5510 vref
= val
& AC_PINCTL_VREFEN
;
5511 for (i
= 0; i
< ARRAY_SIZE(cap_lists
); i
++) {
5512 if (vref
== cap_lists
[i
][0] &&
5513 !(vcap
& cap_lists
[i
][1])) {
5514 if (i
== ARRAY_SIZE(cap_lists
) - 1)
5515 vref
= AC_PINCTL_VREF_HIZ
;
5517 vref
= cap_lists
[i
+ 1][0];
5520 val
&= ~AC_PINCTL_VREFEN
;
5527 EXPORT_SYMBOL_HDA(snd_hda_correct_pin_ctl
);
5529 int _snd_hda_set_pin_ctl(struct hda_codec
*codec
, hda_nid_t pin
,
5530 unsigned int val
, bool cached
)
5532 val
= snd_hda_correct_pin_ctl(codec
, pin
, val
);
5533 snd_hda_codec_set_pin_target(codec
, pin
, val
);
5535 return snd_hda_codec_update_cache(codec
, pin
, 0,
5536 AC_VERB_SET_PIN_WIDGET_CONTROL
, val
);
5538 return snd_hda_codec_write(codec
, pin
, 0,
5539 AC_VERB_SET_PIN_WIDGET_CONTROL
, val
);
5541 EXPORT_SYMBOL_HDA(_snd_hda_set_pin_ctl
);
5544 * snd_hda_add_imux_item - Add an item to input_mux
5546 * When the same label is used already in the existing items, the number
5547 * suffix is appended to the label. This label index number is stored
5548 * to type_idx when non-NULL pointer is given.
5550 int snd_hda_add_imux_item(struct hda_input_mux
*imux
, const char *label
,
5551 int index
, int *type_idx
)
5553 int i
, label_idx
= 0;
5554 if (imux
->num_items
>= HDA_MAX_NUM_INPUTS
) {
5555 snd_printd(KERN_ERR
"hda_codec: Too many imux items!\n");
5558 for (i
= 0; i
< imux
->num_items
; i
++) {
5559 if (!strncmp(label
, imux
->items
[i
].label
, strlen(label
)))
5563 *type_idx
= label_idx
;
5565 snprintf(imux
->items
[imux
->num_items
].label
,
5566 sizeof(imux
->items
[imux
->num_items
].label
),
5567 "%s %d", label
, label_idx
);
5569 strlcpy(imux
->items
[imux
->num_items
].label
, label
,
5570 sizeof(imux
->items
[imux
->num_items
].label
));
5571 imux
->items
[imux
->num_items
].index
= index
;
5575 EXPORT_SYMBOL_HDA(snd_hda_add_imux_item
);
5584 * snd_hda_suspend - suspend the codecs
5587 * Returns 0 if successful.
5589 int snd_hda_suspend(struct hda_bus
*bus
)
5591 struct hda_codec
*codec
;
5593 list_for_each_entry(codec
, &bus
->codec_list
, list
) {
5594 cancel_delayed_work_sync(&codec
->jackpoll_work
);
5595 if (hda_codec_is_power_on(codec
))
5596 hda_call_codec_suspend(codec
, false);
5600 EXPORT_SYMBOL_HDA(snd_hda_suspend
);
5603 * snd_hda_resume - resume the codecs
5606 * Returns 0 if successful.
5608 int snd_hda_resume(struct hda_bus
*bus
)
5610 struct hda_codec
*codec
;
5612 list_for_each_entry(codec
, &bus
->codec_list
, list
) {
5613 hda_call_codec_resume(codec
);
5617 EXPORT_SYMBOL_HDA(snd_hda_resume
);
5618 #endif /* CONFIG_PM */
5625 * snd_array_new - get a new element from the given array
5626 * @array: the array object
5628 * Get a new element from the given array. If it exceeds the
5629 * pre-allocated array size, re-allocate the array.
5631 * Returns NULL if allocation failed.
5633 void *snd_array_new(struct snd_array
*array
)
5635 if (snd_BUG_ON(!array
->elem_size
))
5637 if (array
->used
>= array
->alloced
) {
5638 int num
= array
->alloced
+ array
->alloc_align
;
5639 int size
= (num
+ 1) * array
->elem_size
;
5641 if (snd_BUG_ON(num
>= 4096))
5643 nlist
= krealloc(array
->list
, size
, GFP_KERNEL
| __GFP_ZERO
);
5646 array
->list
= nlist
;
5647 array
->alloced
= num
;
5649 return snd_array_elem(array
, array
->used
++);
5651 EXPORT_SYMBOL_HDA(snd_array_new
);
5654 * snd_array_free - free the given array elements
5655 * @array: the array object
5657 void snd_array_free(struct snd_array
*array
)
5664 EXPORT_SYMBOL_HDA(snd_array_free
);
5667 * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer
5668 * @pcm: PCM caps bits
5669 * @buf: the string buffer to write
5670 * @buflen: the max buffer length
5672 * used by hda_proc.c and hda_eld.c
5674 void snd_print_pcm_bits(int pcm
, char *buf
, int buflen
)
5676 static unsigned int bits
[] = { 8, 16, 20, 24, 32 };
5679 for (i
= 0, j
= 0; i
< ARRAY_SIZE(bits
); i
++)
5680 if (pcm
& (AC_SUPPCM_BITS_8
<< i
))
5681 j
+= snprintf(buf
+ j
, buflen
- j
, " %d", bits
[i
]);
5683 buf
[j
] = '\0'; /* necessary when j == 0 */
5685 EXPORT_SYMBOL_HDA(snd_print_pcm_bits
);
5687 MODULE_DESCRIPTION("HDA codec core");
5688 MODULE_LICENSE("GPL");