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/mutex.h>
27 #include <linux/module.h>
28 #include <linux/async.h>
30 #include <linux/pm_runtime.h>
31 #include <sound/core.h>
32 #include "hda_codec.h"
33 #include <sound/asoundef.h>
34 #include <sound/tlv.h>
35 #include <sound/initval.h>
36 #include <sound/jack.h>
37 #include "hda_local.h"
40 #include <sound/hda_hwdep.h>
42 #define CREATE_TRACE_POINTS
43 #include "hda_trace.h"
46 #define codec_in_pm(codec) atomic_read(&(codec)->in_pm)
47 #define hda_codec_is_power_on(codec) \
48 (!pm_runtime_suspended(hda_codec_dev(codec)))
50 #define codec_in_pm(codec) 0
51 #define hda_codec_is_power_on(codec) 1
55 * snd_hda_get_jack_location - Give a location string of the jack
56 * @cfg: pin default config value
58 * Parse the pin default config value and returns the string of the
59 * jack location, e.g. "Rear", "Front", etc.
61 const char *snd_hda_get_jack_location(u32 cfg
)
63 static char *bases
[7] = {
64 "N/A", "Rear", "Front", "Left", "Right", "Top", "Bottom",
66 static unsigned char specials_idx
[] = {
71 static char *specials
[] = {
72 "Rear Panel", "Drive Bar",
73 "Riser", "HDMI", "ATAPI",
74 "Mobile-In", "Mobile-Out"
77 cfg
= (cfg
& AC_DEFCFG_LOCATION
) >> AC_DEFCFG_LOCATION_SHIFT
;
79 return bases
[cfg
& 0x0f];
80 for (i
= 0; i
< ARRAY_SIZE(specials_idx
); i
++) {
81 if (cfg
== specials_idx
[i
])
86 EXPORT_SYMBOL_GPL(snd_hda_get_jack_location
);
89 * snd_hda_get_jack_connectivity - Give a connectivity string of the jack
90 * @cfg: pin default config value
92 * Parse the pin default config value and returns the string of the
93 * jack connectivity, i.e. external or internal connection.
95 const char *snd_hda_get_jack_connectivity(u32 cfg
)
97 static char *jack_locations
[4] = { "Ext", "Int", "Sep", "Oth" };
99 return jack_locations
[(cfg
>> (AC_DEFCFG_LOCATION_SHIFT
+ 4)) & 3];
101 EXPORT_SYMBOL_GPL(snd_hda_get_jack_connectivity
);
104 * snd_hda_get_jack_type - Give a type string of the jack
105 * @cfg: pin default config value
107 * Parse the pin default config value and returns the string of the
108 * jack type, i.e. the purpose of the jack, such as Line-Out or CD.
110 const char *snd_hda_get_jack_type(u32 cfg
)
112 static char *jack_types
[16] = {
113 "Line Out", "Speaker", "HP Out", "CD",
114 "SPDIF Out", "Digital Out", "Modem Line", "Modem Hand",
115 "Line In", "Aux", "Mic", "Telephony",
116 "SPDIF In", "Digital In", "Reserved", "Other"
119 return jack_types
[(cfg
& AC_DEFCFG_DEVICE
)
120 >> AC_DEFCFG_DEVICE_SHIFT
];
122 EXPORT_SYMBOL_GPL(snd_hda_get_jack_type
);
125 * Compose a 32bit command word to be sent to the HD-audio controller
127 static inline unsigned int
128 make_codec_cmd(struct hda_codec
*codec
, hda_nid_t nid
, int flags
,
129 unsigned int verb
, unsigned int parm
)
133 if ((codec
->addr
& ~0xf) || (nid
& ~0x7f) ||
134 (verb
& ~0xfff) || (parm
& ~0xffff)) {
135 codec_err(codec
, "hda-codec: out of range cmd %x:%x:%x:%x\n",
136 codec
->addr
, nid
, verb
, parm
);
140 val
= (u32
)codec
->addr
<< 28;
141 val
|= (u32
)nid
<< 20;
148 * Send and receive a verb
150 static int codec_exec_verb(struct hda_codec
*codec
, unsigned int cmd
,
151 int flags
, unsigned int *res
)
153 struct hda_bus
*bus
= codec
->bus
;
162 snd_hda_power_up(codec
);
163 mutex_lock(&bus
->cmd_mutex
);
164 if (flags
& HDA_RW_NO_RESPONSE_FALLBACK
)
165 bus
->no_response_fallback
= 1;
167 trace_hda_send_cmd(codec
, cmd
);
168 err
= bus
->ops
.command(bus
, cmd
);
171 /* process pending verbs */
172 bus
->ops
.get_response(bus
, codec
->addr
);
175 *res
= bus
->ops
.get_response(bus
, codec
->addr
);
176 trace_hda_get_response(codec
, *res
);
178 bus
->no_response_fallback
= 0;
179 mutex_unlock(&bus
->cmd_mutex
);
180 snd_hda_power_down(codec
);
181 if (!codec_in_pm(codec
) && res
&& *res
== -1 && bus
->rirb_error
) {
182 if (bus
->response_reset
) {
184 "resetting BUS due to fatal communication error\n");
185 trace_hda_bus_reset(bus
);
186 bus
->ops
.bus_reset(bus
);
190 /* clear reset-flag when the communication gets recovered */
191 if (!err
|| codec_in_pm(codec
))
192 bus
->response_reset
= 0;
197 * snd_hda_codec_read - send a command and get the response
198 * @codec: the HDA codec
199 * @nid: NID to send the command
200 * @flags: optional bit flags
201 * @verb: the verb to send
202 * @parm: the parameter for the verb
204 * Send a single command and read the corresponding response.
206 * Returns the obtained response value, or -1 for an error.
208 unsigned int snd_hda_codec_read(struct hda_codec
*codec
, hda_nid_t nid
,
210 unsigned int verb
, unsigned int parm
)
212 unsigned cmd
= make_codec_cmd(codec
, nid
, flags
, verb
, parm
);
214 if (codec_exec_verb(codec
, cmd
, flags
, &res
))
218 EXPORT_SYMBOL_GPL(snd_hda_codec_read
);
221 * snd_hda_codec_write - send a single command without waiting for response
222 * @codec: the HDA codec
223 * @nid: NID to send the command
224 * @flags: optional bit flags
225 * @verb: the verb to send
226 * @parm: the parameter for the verb
228 * Send a single command without waiting for response.
230 * Returns 0 if successful, or a negative error code.
232 int snd_hda_codec_write(struct hda_codec
*codec
, hda_nid_t nid
, int flags
,
233 unsigned int verb
, unsigned int parm
)
235 unsigned int cmd
= make_codec_cmd(codec
, nid
, flags
, verb
, parm
);
237 return codec_exec_verb(codec
, cmd
, flags
,
238 codec
->bus
->sync_write
? &res
: NULL
);
240 EXPORT_SYMBOL_GPL(snd_hda_codec_write
);
243 * snd_hda_sequence_write - sequence writes
244 * @codec: the HDA codec
245 * @seq: VERB array to send
247 * Send the commands sequentially from the given array.
248 * The array must be terminated with NID=0.
250 void snd_hda_sequence_write(struct hda_codec
*codec
, const struct hda_verb
*seq
)
252 for (; seq
->nid
; seq
++)
253 snd_hda_codec_write(codec
, seq
->nid
, 0, seq
->verb
, seq
->param
);
255 EXPORT_SYMBOL_GPL(snd_hda_sequence_write
);
258 * snd_hda_get_sub_nodes - get the range of sub nodes
259 * @codec: the HDA codec
261 * @start_id: the pointer to store the start NID
263 * Parse the NID and store the start NID of its sub-nodes.
264 * Returns the number of sub-nodes.
266 int snd_hda_get_sub_nodes(struct hda_codec
*codec
, hda_nid_t nid
,
271 parm
= snd_hda_param_read(codec
, nid
, AC_PAR_NODE_COUNT
);
276 *start_id
= (parm
>> 16) & 0x7fff;
277 return (int)(parm
& 0x7fff);
279 EXPORT_SYMBOL_GPL(snd_hda_get_sub_nodes
);
281 /* connection list element */
282 struct hda_conn_list
{
283 struct list_head list
;
289 /* look up the cached results */
290 static struct hda_conn_list
*
291 lookup_conn_list(struct hda_codec
*codec
, hda_nid_t nid
)
293 struct hda_conn_list
*p
;
294 list_for_each_entry(p
, &codec
->conn_list
, list
) {
301 static int add_conn_list(struct hda_codec
*codec
, hda_nid_t nid
, int len
,
302 const hda_nid_t
*list
)
304 struct hda_conn_list
*p
;
306 p
= kmalloc(sizeof(*p
) + len
* sizeof(hda_nid_t
), GFP_KERNEL
);
311 memcpy(p
->conns
, list
, len
* sizeof(hda_nid_t
));
312 list_add(&p
->list
, &codec
->conn_list
);
316 static void remove_conn_list(struct hda_codec
*codec
)
318 while (!list_empty(&codec
->conn_list
)) {
319 struct hda_conn_list
*p
;
320 p
= list_first_entry(&codec
->conn_list
, typeof(*p
), list
);
326 /* read the connection and add to the cache */
327 static int read_and_add_raw_conns(struct hda_codec
*codec
, hda_nid_t nid
)
330 hda_nid_t
*result
= list
;
333 len
= snd_hda_get_raw_connections(codec
, nid
, list
, ARRAY_SIZE(list
));
334 if (len
== -ENOSPC
) {
335 len
= snd_hda_get_num_raw_conns(codec
, nid
);
336 result
= kmalloc(sizeof(hda_nid_t
) * len
, GFP_KERNEL
);
339 len
= snd_hda_get_raw_connections(codec
, nid
, result
, len
);
342 len
= snd_hda_override_conn_list(codec
, nid
, len
, result
);
349 * snd_hda_get_conn_list - get connection list
350 * @codec: the HDA codec
352 * @listp: the pointer to store NID list
354 * Parses the connection list of the given widget and stores the pointer
355 * to the list of NIDs.
357 * Returns the number of connections, or a negative error code.
359 * Note that the returned pointer isn't protected against the list
360 * modification. If snd_hda_override_conn_list() might be called
361 * concurrently, protect with a mutex appropriately.
363 int snd_hda_get_conn_list(struct hda_codec
*codec
, hda_nid_t nid
,
364 const hda_nid_t
**listp
)
370 const struct hda_conn_list
*p
;
372 /* if the connection-list is already cached, read it */
373 p
= lookup_conn_list(codec
, nid
);
379 if (snd_BUG_ON(added
))
382 err
= read_and_add_raw_conns(codec
, nid
);
388 EXPORT_SYMBOL_GPL(snd_hda_get_conn_list
);
391 * snd_hda_get_connections - copy connection list
392 * @codec: the HDA codec
394 * @conn_list: connection list array; when NULL, checks only the size
395 * @max_conns: max. number of connections to store
397 * Parses the connection list of the given widget and stores the list
400 * Returns the number of connections, or a negative error code.
402 int snd_hda_get_connections(struct hda_codec
*codec
, hda_nid_t nid
,
403 hda_nid_t
*conn_list
, int max_conns
)
405 const hda_nid_t
*list
;
406 int len
= snd_hda_get_conn_list(codec
, nid
, &list
);
408 if (len
> 0 && conn_list
) {
409 if (len
> max_conns
) {
410 codec_err(codec
, "Too many connections %d for NID 0x%x\n",
414 memcpy(conn_list
, list
, len
* sizeof(hda_nid_t
));
419 EXPORT_SYMBOL_GPL(snd_hda_get_connections
);
421 /* return CONNLIST_LEN parameter of the given widget */
422 static unsigned int get_num_conns(struct hda_codec
*codec
, hda_nid_t nid
)
424 unsigned int wcaps
= get_wcaps(codec
, nid
);
427 if (!(wcaps
& AC_WCAP_CONN_LIST
) &&
428 get_wcaps_type(wcaps
) != AC_WID_VOL_KNB
)
431 parm
= snd_hda_param_read(codec
, nid
, AC_PAR_CONNLIST_LEN
);
437 int snd_hda_get_num_raw_conns(struct hda_codec
*codec
, hda_nid_t nid
)
439 return snd_hda_get_raw_connections(codec
, nid
, NULL
, 0);
443 * snd_hda_get_raw_connections - copy connection list without cache
444 * @codec: the HDA codec
446 * @conn_list: connection list array
447 * @max_conns: max. number of connections to store
449 * Like snd_hda_get_connections(), copy the connection list but without
450 * checking through the connection-list cache.
451 * Currently called only from hda_proc.c, so not exported.
453 int snd_hda_get_raw_connections(struct hda_codec
*codec
, hda_nid_t nid
,
454 hda_nid_t
*conn_list
, int max_conns
)
457 int i
, conn_len
, conns
;
458 unsigned int shift
, num_elems
, mask
;
462 parm
= get_num_conns(codec
, nid
);
466 if (parm
& AC_CLIST_LONG
) {
475 conn_len
= parm
& AC_CLIST_LENGTH
;
476 mask
= (1 << (shift
-1)) - 1;
479 return 0; /* no connection */
482 /* single connection */
483 parm
= snd_hda_codec_read(codec
, nid
, 0,
484 AC_VERB_GET_CONNECT_LIST
, 0);
485 if (parm
== -1 && codec
->bus
->rirb_error
)
488 conn_list
[0] = parm
& mask
;
492 /* multi connection */
495 for (i
= 0; i
< conn_len
; i
++) {
499 if (i
% num_elems
== 0) {
500 parm
= snd_hda_codec_read(codec
, nid
, 0,
501 AC_VERB_GET_CONNECT_LIST
, i
);
502 if (parm
== -1 && codec
->bus
->rirb_error
)
505 range_val
= !!(parm
& (1 << (shift
-1))); /* ranges */
507 if (val
== 0 && null_count
++) { /* no second chance */
509 "invalid CONNECT_LIST verb %x[%i]:%x\n",
515 /* ranges between the previous and this one */
516 if (!prev_nid
|| prev_nid
>= val
) {
518 "invalid dep_range_val %x:%x\n",
522 for (n
= prev_nid
+ 1; n
<= val
; n
++) {
524 if (conns
>= max_conns
)
526 conn_list
[conns
] = n
;
532 if (conns
>= max_conns
)
534 conn_list
[conns
] = val
;
544 * snd_hda_override_conn_list - add/modify the connection-list to cache
545 * @codec: the HDA codec
547 * @len: number of connection list entries
548 * @list: the list of connection entries
550 * Add or modify the given connection-list to the cache. If the corresponding
551 * cache already exists, invalidate it and append a new one.
553 * Returns zero or a negative error code.
555 int snd_hda_override_conn_list(struct hda_codec
*codec
, hda_nid_t nid
, int len
,
556 const hda_nid_t
*list
)
558 struct hda_conn_list
*p
;
560 p
= lookup_conn_list(codec
, nid
);
566 return add_conn_list(codec
, nid
, len
, list
);
568 EXPORT_SYMBOL_GPL(snd_hda_override_conn_list
);
571 * snd_hda_get_conn_index - get the connection index of the given NID
572 * @codec: the HDA codec
573 * @mux: NID containing the list
574 * @nid: NID to select
575 * @recursive: 1 when searching NID recursively, otherwise 0
577 * Parses the connection list of the widget @mux and checks whether the
578 * widget @nid is present. If it is, return the connection index.
579 * Otherwise it returns -1.
581 int snd_hda_get_conn_index(struct hda_codec
*codec
, hda_nid_t mux
,
582 hda_nid_t nid
, int recursive
)
584 const hda_nid_t
*conn
;
587 nums
= snd_hda_get_conn_list(codec
, mux
, &conn
);
588 for (i
= 0; i
< nums
; i
++)
593 if (recursive
> 10) {
594 codec_dbg(codec
, "too deep connection for 0x%x\n", nid
);
598 for (i
= 0; i
< nums
; i
++) {
599 unsigned int type
= get_wcaps_type(get_wcaps(codec
, conn
[i
]));
600 if (type
== AC_WID_PIN
|| type
== AC_WID_AUD_OUT
)
602 if (snd_hda_get_conn_index(codec
, conn
[i
], nid
, recursive
) >= 0)
607 EXPORT_SYMBOL_GPL(snd_hda_get_conn_index
);
610 /* return DEVLIST_LEN parameter of the given widget */
611 static unsigned int get_num_devices(struct hda_codec
*codec
, hda_nid_t nid
)
613 unsigned int wcaps
= get_wcaps(codec
, nid
);
616 if (!codec
->dp_mst
|| !(wcaps
& AC_WCAP_DIGITAL
) ||
617 get_wcaps_type(wcaps
) != AC_WID_PIN
)
620 parm
= snd_hda_param_read(codec
, nid
, AC_PAR_DEVLIST_LEN
);
621 if (parm
== -1 && codec
->bus
->rirb_error
)
623 return parm
& AC_DEV_LIST_LEN_MASK
;
627 * snd_hda_get_devices - copy device list without cache
628 * @codec: the HDA codec
629 * @nid: NID of the pin to parse
630 * @dev_list: device list array
631 * @max_devices: max. number of devices to store
633 * Copy the device list. This info is dynamic and so not cached.
634 * Currently called only from hda_proc.c, so not exported.
636 int snd_hda_get_devices(struct hda_codec
*codec
, hda_nid_t nid
,
637 u8
*dev_list
, int max_devices
)
640 int i
, dev_len
, devices
;
642 parm
= get_num_devices(codec
, nid
);
643 if (!parm
) /* not multi-stream capable */
647 dev_len
= dev_len
< max_devices
? dev_len
: max_devices
;
650 while (devices
< dev_len
) {
651 parm
= snd_hda_codec_read(codec
, nid
, 0,
652 AC_VERB_GET_DEVICE_LIST
, devices
);
653 if (parm
== -1 && codec
->bus
->rirb_error
)
656 for (i
= 0; i
< 8; i
++) {
657 dev_list
[devices
] = (u8
)parm
;
660 if (devices
>= dev_len
)
668 * snd_hda_queue_unsol_event - add an unsolicited event to queue
670 * @res: unsolicited event (lower 32bit of RIRB entry)
671 * @res_ex: codec addr and flags (upper 32bit or RIRB entry)
673 * Adds the given event to the queue. The events are processed in
674 * the workqueue asynchronously. Call this function in the interrupt
675 * hanlder when RIRB receives an unsolicited event.
677 * Returns 0 if successful, or a negative error code.
679 int snd_hda_queue_unsol_event(struct hda_bus
*bus
, u32 res
, u32 res_ex
)
681 struct hda_bus_unsolicited
*unsol
;
687 trace_hda_unsol_event(bus
, res
, res_ex
);
689 wp
= (unsol
->wp
+ 1) % HDA_UNSOL_QUEUE_SIZE
;
693 unsol
->queue
[wp
] = res
;
694 unsol
->queue
[wp
+ 1] = res_ex
;
696 schedule_work(&unsol
->work
);
700 EXPORT_SYMBOL_GPL(snd_hda_queue_unsol_event
);
703 * process queued unsolicited events
705 static void process_unsol_events(struct work_struct
*work
)
707 struct hda_bus
*bus
= container_of(work
, struct hda_bus
, unsol
.work
);
708 struct hda_bus_unsolicited
*unsol
= &bus
->unsol
;
709 struct hda_codec
*codec
;
710 unsigned int rp
, caddr
, res
;
712 while (unsol
->rp
!= unsol
->wp
) {
713 rp
= (unsol
->rp
+ 1) % HDA_UNSOL_QUEUE_SIZE
;
716 res
= unsol
->queue
[rp
];
717 caddr
= unsol
->queue
[rp
+ 1];
718 if (!(caddr
& (1 << 4))) /* no unsolicited event? */
720 codec
= bus
->caddr_tbl
[caddr
& 0x0f];
721 if (codec
&& codec
->patch_ops
.unsol_event
)
722 codec
->patch_ops
.unsol_event(codec
, res
);
729 static void snd_hda_bus_free(struct hda_bus
*bus
)
734 WARN_ON(!list_empty(&bus
->codec_list
));
735 cancel_work_sync(&bus
->unsol
.work
);
736 if (bus
->ops
.private_free
)
737 bus
->ops
.private_free(bus
);
741 static int snd_hda_bus_dev_free(struct snd_device
*device
)
743 snd_hda_bus_free(device
->device_data
);
747 static int snd_hda_bus_dev_disconnect(struct snd_device
*device
)
749 struct hda_bus
*bus
= device
->device_data
;
755 * snd_hda_bus_new - create a HDA bus
756 * @card: the card entry
757 * @busp: the pointer to store the created bus instance
759 * Returns 0 if successful, or a negative error code.
761 int snd_hda_bus_new(struct snd_card
*card
,
762 struct hda_bus
**busp
)
766 static struct snd_device_ops dev_ops
= {
767 .dev_disconnect
= snd_hda_bus_dev_disconnect
,
768 .dev_free
= snd_hda_bus_dev_free
,
774 bus
= kzalloc(sizeof(*bus
), GFP_KERNEL
);
779 mutex_init(&bus
->cmd_mutex
);
780 mutex_init(&bus
->prepare_mutex
);
781 INIT_LIST_HEAD(&bus
->codec_list
);
782 INIT_WORK(&bus
->unsol
.work
, process_unsol_events
);
784 err
= snd_device_new(card
, SNDRV_DEV_BUS
, bus
, &dev_ops
);
786 snd_hda_bus_free(bus
);
793 EXPORT_SYMBOL_GPL(snd_hda_bus_new
);
796 * look for an AFG and MFG nodes
798 static void setup_fg_nodes(struct hda_codec
*codec
)
800 int i
, total_nodes
, function_id
;
803 total_nodes
= snd_hda_get_sub_nodes(codec
, AC_NODE_ROOT
, &nid
);
804 for (i
= 0; i
< total_nodes
; i
++, nid
++) {
805 function_id
= snd_hda_param_read(codec
, nid
,
806 AC_PAR_FUNCTION_TYPE
);
807 switch (function_id
& 0xff) {
808 case AC_GRP_AUDIO_FUNCTION
:
810 codec
->afg_function_id
= function_id
& 0xff;
811 codec
->afg_unsol
= (function_id
>> 8) & 1;
813 case AC_GRP_MODEM_FUNCTION
:
815 codec
->mfg_function_id
= function_id
& 0xff;
816 codec
->mfg_unsol
= (function_id
>> 8) & 1;
825 * read widget caps for each widget and store in cache
827 static int read_widget_caps(struct hda_codec
*codec
, hda_nid_t fg_node
)
832 codec
->num_nodes
= snd_hda_get_sub_nodes(codec
, fg_node
,
834 codec
->wcaps
= kmalloc(codec
->num_nodes
* 4, GFP_KERNEL
);
837 nid
= codec
->start_nid
;
838 for (i
= 0; i
< codec
->num_nodes
; i
++, nid
++)
839 codec
->wcaps
[i
] = snd_hda_param_read(codec
, nid
,
840 AC_PAR_AUDIO_WIDGET_CAP
);
844 /* read all pin default configurations and save codec->init_pins */
845 static int read_pin_defaults(struct hda_codec
*codec
)
848 hda_nid_t nid
= codec
->start_nid
;
850 for (i
= 0; i
< codec
->num_nodes
; i
++, nid
++) {
851 struct hda_pincfg
*pin
;
852 unsigned int wcaps
= get_wcaps(codec
, nid
);
853 unsigned int wid_type
= get_wcaps_type(wcaps
);
854 if (wid_type
!= AC_WID_PIN
)
856 pin
= snd_array_new(&codec
->init_pins
);
860 pin
->cfg
= snd_hda_codec_read(codec
, nid
, 0,
861 AC_VERB_GET_CONFIG_DEFAULT
, 0);
862 pin
->ctrl
= snd_hda_codec_read(codec
, nid
, 0,
863 AC_VERB_GET_PIN_WIDGET_CONTROL
,
869 /* look up the given pin config list and return the item matching with NID */
870 static struct hda_pincfg
*look_up_pincfg(struct hda_codec
*codec
,
871 struct snd_array
*array
,
875 for (i
= 0; i
< array
->used
; i
++) {
876 struct hda_pincfg
*pin
= snd_array_elem(array
, i
);
883 /* set the current pin config value for the given NID.
884 * the value is cached, and read via snd_hda_codec_get_pincfg()
886 int snd_hda_add_pincfg(struct hda_codec
*codec
, struct snd_array
*list
,
887 hda_nid_t nid
, unsigned int cfg
)
889 struct hda_pincfg
*pin
;
891 /* the check below may be invalid when pins are added by a fixup
892 * dynamically (e.g. via snd_hda_codec_update_widgets()), so disabled
896 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN)
900 pin
= look_up_pincfg(codec
, list
, nid
);
902 pin
= snd_array_new(list
);
912 * snd_hda_codec_set_pincfg - Override a pin default configuration
913 * @codec: the HDA codec
914 * @nid: NID to set the pin config
915 * @cfg: the pin default config value
917 * Override a pin default configuration value in the cache.
918 * This value can be read by snd_hda_codec_get_pincfg() in a higher
919 * priority than the real hardware value.
921 int snd_hda_codec_set_pincfg(struct hda_codec
*codec
,
922 hda_nid_t nid
, unsigned int cfg
)
924 return snd_hda_add_pincfg(codec
, &codec
->driver_pins
, nid
, cfg
);
926 EXPORT_SYMBOL_GPL(snd_hda_codec_set_pincfg
);
929 * snd_hda_codec_get_pincfg - Obtain a pin-default configuration
930 * @codec: the HDA codec
931 * @nid: NID to get the pin config
933 * Get the current pin config value of the given pin NID.
934 * If the pincfg value is cached or overridden via sysfs or driver,
935 * returns the cached value.
937 unsigned int snd_hda_codec_get_pincfg(struct hda_codec
*codec
, hda_nid_t nid
)
939 struct hda_pincfg
*pin
;
941 #ifdef CONFIG_SND_HDA_RECONFIG
943 unsigned int cfg
= 0;
944 mutex_lock(&codec
->user_mutex
);
945 pin
= look_up_pincfg(codec
, &codec
->user_pins
, nid
);
948 mutex_unlock(&codec
->user_mutex
);
953 pin
= look_up_pincfg(codec
, &codec
->driver_pins
, nid
);
956 pin
= look_up_pincfg(codec
, &codec
->init_pins
, nid
);
961 EXPORT_SYMBOL_GPL(snd_hda_codec_get_pincfg
);
964 * snd_hda_codec_set_pin_target - remember the current pinctl target value
965 * @codec: the HDA codec
967 * @val: assigned pinctl value
969 * This function stores the given value to a pinctl target value in the
970 * pincfg table. This isn't always as same as the actually written value
971 * but can be referred at any time via snd_hda_codec_get_pin_target().
973 int snd_hda_codec_set_pin_target(struct hda_codec
*codec
, hda_nid_t nid
,
976 struct hda_pincfg
*pin
;
978 pin
= look_up_pincfg(codec
, &codec
->init_pins
, nid
);
984 EXPORT_SYMBOL_GPL(snd_hda_codec_set_pin_target
);
987 * snd_hda_codec_get_pin_target - return the current pinctl target value
988 * @codec: the HDA codec
991 int snd_hda_codec_get_pin_target(struct hda_codec
*codec
, hda_nid_t nid
)
993 struct hda_pincfg
*pin
;
995 pin
= look_up_pincfg(codec
, &codec
->init_pins
, nid
);
1000 EXPORT_SYMBOL_GPL(snd_hda_codec_get_pin_target
);
1003 * snd_hda_shutup_pins - Shut up all pins
1004 * @codec: the HDA codec
1006 * Clear all pin controls to shup up before suspend for avoiding click noise.
1007 * The controls aren't cached so that they can be resumed properly.
1009 void snd_hda_shutup_pins(struct hda_codec
*codec
)
1012 /* don't shut up pins when unloading the driver; otherwise it breaks
1013 * the default pin setup at the next load of the driver
1015 if (codec
->bus
->shutdown
)
1017 for (i
= 0; i
< codec
->init_pins
.used
; i
++) {
1018 struct hda_pincfg
*pin
= snd_array_elem(&codec
->init_pins
, i
);
1019 /* use read here for syncing after issuing each verb */
1020 snd_hda_codec_read(codec
, pin
->nid
, 0,
1021 AC_VERB_SET_PIN_WIDGET_CONTROL
, 0);
1023 codec
->pins_shutup
= 1;
1025 EXPORT_SYMBOL_GPL(snd_hda_shutup_pins
);
1028 /* Restore the pin controls cleared previously via snd_hda_shutup_pins() */
1029 static void restore_shutup_pins(struct hda_codec
*codec
)
1032 if (!codec
->pins_shutup
)
1034 if (codec
->bus
->shutdown
)
1036 for (i
= 0; i
< codec
->init_pins
.used
; i
++) {
1037 struct hda_pincfg
*pin
= snd_array_elem(&codec
->init_pins
, i
);
1038 snd_hda_codec_write(codec
, pin
->nid
, 0,
1039 AC_VERB_SET_PIN_WIDGET_CONTROL
,
1042 codec
->pins_shutup
= 0;
1046 static void hda_jackpoll_work(struct work_struct
*work
)
1048 struct hda_codec
*codec
=
1049 container_of(work
, struct hda_codec
, jackpoll_work
.work
);
1051 snd_hda_jack_set_dirty_all(codec
);
1052 snd_hda_jack_poll_all(codec
);
1054 if (!codec
->jackpoll_interval
)
1057 schedule_delayed_work(&codec
->jackpoll_work
,
1058 codec
->jackpoll_interval
);
1061 static void init_hda_cache(struct hda_cache_rec
*cache
,
1062 unsigned int record_size
);
1063 static void free_hda_cache(struct hda_cache_rec
*cache
);
1065 /* release all pincfg lists */
1066 static void free_init_pincfgs(struct hda_codec
*codec
)
1068 snd_array_free(&codec
->driver_pins
);
1069 #ifdef CONFIG_SND_HDA_RECONFIG
1070 snd_array_free(&codec
->user_pins
);
1072 snd_array_free(&codec
->init_pins
);
1076 * audio-converter setup caches
1078 struct hda_cvt_setup
{
1083 unsigned char active
; /* cvt is currently used */
1084 unsigned char dirty
; /* setups should be cleared */
1087 /* get or create a cache entry for the given audio converter NID */
1088 static struct hda_cvt_setup
*
1089 get_hda_cvt_setup(struct hda_codec
*codec
, hda_nid_t nid
)
1091 struct hda_cvt_setup
*p
;
1094 for (i
= 0; i
< codec
->cvt_setups
.used
; i
++) {
1095 p
= snd_array_elem(&codec
->cvt_setups
, i
);
1099 p
= snd_array_new(&codec
->cvt_setups
);
1108 static void release_pcm(struct kref
*kref
)
1110 struct hda_pcm
*pcm
= container_of(kref
, struct hda_pcm
, kref
);
1113 snd_device_free(pcm
->codec
->card
, pcm
->pcm
);
1114 clear_bit(pcm
->device
, pcm
->codec
->bus
->pcm_dev_bits
);
1119 void snd_hda_codec_pcm_put(struct hda_pcm
*pcm
)
1121 kref_put(&pcm
->kref
, release_pcm
);
1123 EXPORT_SYMBOL_GPL(snd_hda_codec_pcm_put
);
1125 struct hda_pcm
*snd_hda_codec_pcm_new(struct hda_codec
*codec
,
1126 const char *fmt
, ...)
1128 struct hda_pcm
*pcm
;
1131 va_start(args
, fmt
);
1132 pcm
= kzalloc(sizeof(*pcm
), GFP_KERNEL
);
1137 kref_init(&pcm
->kref
);
1138 pcm
->name
= kvasprintf(GFP_KERNEL
, fmt
, args
);
1144 list_add_tail(&pcm
->list
, &codec
->pcm_list_head
);
1147 EXPORT_SYMBOL_GPL(snd_hda_codec_pcm_new
);
1152 static void codec_release_pcms(struct hda_codec
*codec
)
1154 struct hda_pcm
*pcm
, *n
;
1156 list_for_each_entry_safe(pcm
, n
, &codec
->pcm_list_head
, list
) {
1157 list_del_init(&pcm
->list
);
1159 snd_device_disconnect(codec
->card
, pcm
->pcm
);
1160 snd_hda_codec_pcm_put(pcm
);
1164 void snd_hda_codec_cleanup_for_unbind(struct hda_codec
*codec
)
1166 cancel_delayed_work_sync(&codec
->jackpoll_work
);
1167 if (!codec
->in_freeing
)
1168 snd_hda_ctls_clear(codec
);
1169 codec_release_pcms(codec
);
1170 snd_hda_detach_beep_device(codec
);
1171 memset(&codec
->patch_ops
, 0, sizeof(codec
->patch_ops
));
1172 snd_hda_jack_tbl_clear(codec
);
1173 codec
->proc_widget_hook
= NULL
;
1176 free_hda_cache(&codec
->amp_cache
);
1177 free_hda_cache(&codec
->cmd_cache
);
1178 init_hda_cache(&codec
->amp_cache
, sizeof(struct hda_amp_info
));
1179 init_hda_cache(&codec
->cmd_cache
, sizeof(struct hda_cache_head
));
1181 /* free only driver_pins so that init_pins + user_pins are restored */
1182 snd_array_free(&codec
->driver_pins
);
1183 snd_array_free(&codec
->cvt_setups
);
1184 snd_array_free(&codec
->spdif_out
);
1185 snd_array_free(&codec
->verbs
);
1186 codec
->preset
= NULL
;
1187 codec
->slave_dig_outs
= NULL
;
1188 codec
->spdif_status_reset
= 0;
1189 snd_array_free(&codec
->mixers
);
1190 snd_array_free(&codec
->nids
);
1191 remove_conn_list(codec
);
1194 static bool snd_hda_codec_get_supported_ps(struct hda_codec
*codec
,
1195 hda_nid_t fg
, unsigned int power_state
);
1197 static unsigned int hda_set_power_state(struct hda_codec
*codec
,
1198 unsigned int power_state
);
1200 static int snd_hda_codec_dev_register(struct snd_device
*device
)
1202 struct hda_codec
*codec
= device
->device_data
;
1204 snd_hda_register_beep_device(codec
);
1205 if (device_is_registered(hda_codec_dev(codec
)))
1206 pm_runtime_enable(hda_codec_dev(codec
));
1207 /* it was powered up in snd_hda_codec_new(), now all done */
1208 snd_hda_power_down(codec
);
1212 static int snd_hda_codec_dev_disconnect(struct snd_device
*device
)
1214 struct hda_codec
*codec
= device
->device_data
;
1216 snd_hda_detach_beep_device(codec
);
1220 static int snd_hda_codec_dev_free(struct snd_device
*device
)
1222 struct hda_codec
*codec
= device
->device_data
;
1224 codec
->in_freeing
= 1;
1225 if (device_is_registered(hda_codec_dev(codec
)))
1226 device_del(hda_codec_dev(codec
));
1227 put_device(hda_codec_dev(codec
));
1231 static void snd_hda_codec_dev_release(struct device
*dev
)
1233 struct hda_codec
*codec
= dev_to_hda_codec(dev
);
1235 free_init_pincfgs(codec
);
1236 list_del(&codec
->list
);
1237 codec
->bus
->caddr_tbl
[codec
->addr
] = NULL
;
1238 clear_bit(codec
->addr
, &codec
->bus
->codec_powered
);
1239 snd_hda_sysfs_clear(codec
);
1240 free_hda_cache(&codec
->amp_cache
);
1241 free_hda_cache(&codec
->cmd_cache
);
1242 kfree(codec
->vendor_name
);
1243 kfree(codec
->chip_name
);
1244 kfree(codec
->modelname
);
1245 kfree(codec
->wcaps
);
1246 codec
->bus
->num_codecs
--;
1251 * snd_hda_codec_new - create a HDA codec
1252 * @bus: the bus to assign
1253 * @codec_addr: the codec address
1254 * @codecp: the pointer to store the generated codec
1256 * Returns 0 if successful, or a negative error code.
1258 int snd_hda_codec_new(struct hda_bus
*bus
, struct snd_card
*card
,
1259 unsigned int codec_addr
, struct hda_codec
**codecp
)
1261 struct hda_codec
*codec
;
1266 static struct snd_device_ops dev_ops
= {
1267 .dev_register
= snd_hda_codec_dev_register
,
1268 .dev_disconnect
= snd_hda_codec_dev_disconnect
,
1269 .dev_free
= snd_hda_codec_dev_free
,
1272 if (snd_BUG_ON(!bus
))
1274 if (snd_BUG_ON(codec_addr
> HDA_MAX_CODEC_ADDRESS
))
1277 if (bus
->caddr_tbl
[codec_addr
]) {
1279 "address 0x%x is already occupied\n",
1284 codec
= kzalloc(sizeof(*codec
), GFP_KERNEL
);
1288 dev
= hda_codec_dev(codec
);
1289 device_initialize(dev
);
1290 dev
->parent
= card
->dev
;
1291 dev
->bus
= &snd_hda_bus_type
;
1292 dev
->release
= snd_hda_codec_dev_release
;
1293 dev
->groups
= snd_hda_dev_attr_groups
;
1294 dev_set_name(dev
, "hdaudioC%dD%d", card
->number
, codec_addr
);
1295 dev_set_drvdata(dev
, codec
); /* for sysfs */
1296 device_enable_async_suspend(dev
);
1297 codec
->core
.type
= HDA_DEV_LEGACY
;
1301 codec
->addr
= codec_addr
;
1302 mutex_init(&codec
->spdif_mutex
);
1303 mutex_init(&codec
->control_mutex
);
1304 mutex_init(&codec
->hash_mutex
);
1305 init_hda_cache(&codec
->amp_cache
, sizeof(struct hda_amp_info
));
1306 init_hda_cache(&codec
->cmd_cache
, sizeof(struct hda_cache_head
));
1307 snd_array_init(&codec
->mixers
, sizeof(struct hda_nid_item
), 32);
1308 snd_array_init(&codec
->nids
, sizeof(struct hda_nid_item
), 32);
1309 snd_array_init(&codec
->init_pins
, sizeof(struct hda_pincfg
), 16);
1310 snd_array_init(&codec
->driver_pins
, sizeof(struct hda_pincfg
), 16);
1311 snd_array_init(&codec
->cvt_setups
, sizeof(struct hda_cvt_setup
), 8);
1312 snd_array_init(&codec
->spdif_out
, sizeof(struct hda_spdif_out
), 16);
1313 snd_array_init(&codec
->jacktbl
, sizeof(struct hda_jack_tbl
), 16);
1314 snd_array_init(&codec
->verbs
, sizeof(struct hda_verb
*), 8);
1315 INIT_LIST_HEAD(&codec
->conn_list
);
1316 INIT_LIST_HEAD(&codec
->pcm_list_head
);
1318 INIT_DELAYED_WORK(&codec
->jackpoll_work
, hda_jackpoll_work
);
1319 codec
->depop_delay
= -1;
1320 codec
->fixup_id
= HDA_FIXUP_ID_NOT_SET
;
1323 /* snd_hda_codec_new() marks the codec as power-up, and leave it as is.
1324 * it's powered down later in snd_hda_codec_dev_register().
1326 set_bit(codec
->addr
, &bus
->codec_powered
);
1327 pm_runtime_set_active(hda_codec_dev(codec
));
1328 pm_runtime_get_noresume(hda_codec_dev(codec
));
1329 codec
->power_jiffies
= jiffies
;
1332 snd_hda_sysfs_init(codec
);
1334 if (codec
->bus
->modelname
) {
1335 codec
->modelname
= kstrdup(codec
->bus
->modelname
, GFP_KERNEL
);
1336 if (!codec
->modelname
) {
1342 list_add_tail(&codec
->list
, &bus
->codec_list
);
1345 bus
->caddr_tbl
[codec_addr
] = codec
;
1347 codec
->vendor_id
= snd_hda_param_read(codec
, AC_NODE_ROOT
,
1349 if (codec
->vendor_id
== -1)
1350 /* read again, hopefully the access method was corrected
1351 * in the last read...
1353 codec
->vendor_id
= snd_hda_param_read(codec
, AC_NODE_ROOT
,
1355 codec
->subsystem_id
= snd_hda_param_read(codec
, AC_NODE_ROOT
,
1356 AC_PAR_SUBSYSTEM_ID
);
1357 codec
->revision_id
= snd_hda_param_read(codec
, AC_NODE_ROOT
,
1360 setup_fg_nodes(codec
);
1361 if (!codec
->afg
&& !codec
->mfg
) {
1362 codec_err(codec
, "no AFG or MFG node found\n");
1367 fg
= codec
->afg
? codec
->afg
: codec
->mfg
;
1368 err
= read_widget_caps(codec
, fg
);
1371 err
= read_pin_defaults(codec
);
1375 if (!codec
->subsystem_id
) {
1376 codec
->subsystem_id
=
1377 snd_hda_codec_read(codec
, fg
, 0,
1378 AC_VERB_GET_SUBSYSTEM_ID
, 0);
1382 codec
->d3_stop_clk
= snd_hda_codec_get_supported_ps(codec
, fg
,
1385 codec
->epss
= snd_hda_codec_get_supported_ps(codec
, fg
,
1388 /* power-up all before initialization */
1389 hda_set_power_state(codec
, AC_PWRST_D0
);
1391 snd_hda_codec_proc_new(codec
);
1393 snd_hda_create_hwdep(codec
);
1395 sprintf(component
, "HDA:%08x,%08x,%08x", codec
->vendor_id
,
1396 codec
->subsystem_id
, codec
->revision_id
);
1397 snd_component_add(card
, component
);
1399 err
= snd_device_new(card
, SNDRV_DEV_CODEC
, codec
, &dev_ops
);
1408 put_device(hda_codec_dev(codec
));
1411 EXPORT_SYMBOL_GPL(snd_hda_codec_new
);
1414 * snd_hda_codec_update_widgets - Refresh widget caps and pin defaults
1415 * @codec: the HDA codec
1417 * Forcibly refresh the all widget caps and the init pin configurations of
1420 int snd_hda_codec_update_widgets(struct hda_codec
*codec
)
1425 /* Assume the function group node does not change,
1426 * only the widget nodes may change.
1428 kfree(codec
->wcaps
);
1429 fg
= codec
->afg
? codec
->afg
: codec
->mfg
;
1430 err
= read_widget_caps(codec
, fg
);
1434 snd_array_free(&codec
->init_pins
);
1435 err
= read_pin_defaults(codec
);
1439 EXPORT_SYMBOL_GPL(snd_hda_codec_update_widgets
);
1441 /* update the stream-id if changed */
1442 static void update_pcm_stream_id(struct hda_codec
*codec
,
1443 struct hda_cvt_setup
*p
, hda_nid_t nid
,
1444 u32 stream_tag
, int channel_id
)
1446 unsigned int oldval
, newval
;
1448 if (p
->stream_tag
!= stream_tag
|| p
->channel_id
!= channel_id
) {
1449 oldval
= snd_hda_codec_read(codec
, nid
, 0, AC_VERB_GET_CONV
, 0);
1450 newval
= (stream_tag
<< 4) | channel_id
;
1451 if (oldval
!= newval
)
1452 snd_hda_codec_write(codec
, nid
, 0,
1453 AC_VERB_SET_CHANNEL_STREAMID
,
1455 p
->stream_tag
= stream_tag
;
1456 p
->channel_id
= channel_id
;
1460 /* update the format-id if changed */
1461 static void update_pcm_format(struct hda_codec
*codec
, struct hda_cvt_setup
*p
,
1462 hda_nid_t nid
, int format
)
1464 unsigned int oldval
;
1466 if (p
->format_id
!= format
) {
1467 oldval
= snd_hda_codec_read(codec
, nid
, 0,
1468 AC_VERB_GET_STREAM_FORMAT
, 0);
1469 if (oldval
!= format
) {
1471 snd_hda_codec_write(codec
, nid
, 0,
1472 AC_VERB_SET_STREAM_FORMAT
,
1475 p
->format_id
= format
;
1480 * snd_hda_codec_setup_stream - set up the codec for streaming
1481 * @codec: the CODEC to set up
1482 * @nid: the NID to set up
1483 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
1484 * @channel_id: channel id to pass, zero based.
1485 * @format: stream format.
1487 void snd_hda_codec_setup_stream(struct hda_codec
*codec
, hda_nid_t nid
,
1489 int channel_id
, int format
)
1491 struct hda_codec
*c
;
1492 struct hda_cvt_setup
*p
;
1500 "hda_codec_setup_stream: NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
1501 nid
, stream_tag
, channel_id
, format
);
1502 p
= get_hda_cvt_setup(codec
, nid
);
1506 if (codec
->patch_ops
.stream_pm
)
1507 codec
->patch_ops
.stream_pm(codec
, nid
, true);
1508 if (codec
->pcm_format_first
)
1509 update_pcm_format(codec
, p
, nid
, format
);
1510 update_pcm_stream_id(codec
, p
, nid
, stream_tag
, channel_id
);
1511 if (!codec
->pcm_format_first
)
1512 update_pcm_format(codec
, p
, nid
, format
);
1517 /* make other inactive cvts with the same stream-tag dirty */
1518 type
= get_wcaps_type(get_wcaps(codec
, nid
));
1519 list_for_each_entry(c
, &codec
->bus
->codec_list
, list
) {
1520 for (i
= 0; i
< c
->cvt_setups
.used
; i
++) {
1521 p
= snd_array_elem(&c
->cvt_setups
, i
);
1522 if (!p
->active
&& p
->stream_tag
== stream_tag
&&
1523 get_wcaps_type(get_wcaps(c
, p
->nid
)) == type
)
1528 EXPORT_SYMBOL_GPL(snd_hda_codec_setup_stream
);
1530 static void really_cleanup_stream(struct hda_codec
*codec
,
1531 struct hda_cvt_setup
*q
);
1534 * __snd_hda_codec_cleanup_stream - clean up the codec for closing
1535 * @codec: the CODEC to clean up
1536 * @nid: the NID to clean up
1537 * @do_now: really clean up the stream instead of clearing the active flag
1539 void __snd_hda_codec_cleanup_stream(struct hda_codec
*codec
, hda_nid_t nid
,
1542 struct hda_cvt_setup
*p
;
1547 if (codec
->no_sticky_stream
)
1550 codec_dbg(codec
, "hda_codec_cleanup_stream: NID=0x%x\n", nid
);
1551 p
= get_hda_cvt_setup(codec
, nid
);
1553 /* here we just clear the active flag when do_now isn't set;
1554 * actual clean-ups will be done later in
1555 * purify_inactive_streams() called from snd_hda_codec_prpapre()
1558 really_cleanup_stream(codec
, p
);
1563 EXPORT_SYMBOL_GPL(__snd_hda_codec_cleanup_stream
);
1565 static void really_cleanup_stream(struct hda_codec
*codec
,
1566 struct hda_cvt_setup
*q
)
1568 hda_nid_t nid
= q
->nid
;
1569 if (q
->stream_tag
|| q
->channel_id
)
1570 snd_hda_codec_write(codec
, nid
, 0, AC_VERB_SET_CHANNEL_STREAMID
, 0);
1572 snd_hda_codec_write(codec
, nid
, 0, AC_VERB_SET_STREAM_FORMAT
, 0
1574 memset(q
, 0, sizeof(*q
));
1576 if (codec
->patch_ops
.stream_pm
)
1577 codec
->patch_ops
.stream_pm(codec
, nid
, false);
1580 /* clean up the all conflicting obsolete streams */
1581 static void purify_inactive_streams(struct hda_codec
*codec
)
1583 struct hda_codec
*c
;
1586 list_for_each_entry(c
, &codec
->bus
->codec_list
, list
) {
1587 for (i
= 0; i
< c
->cvt_setups
.used
; i
++) {
1588 struct hda_cvt_setup
*p
;
1589 p
= snd_array_elem(&c
->cvt_setups
, i
);
1591 really_cleanup_stream(c
, p
);
1597 /* clean up all streams; called from suspend */
1598 static void hda_cleanup_all_streams(struct hda_codec
*codec
)
1602 for (i
= 0; i
< codec
->cvt_setups
.used
; i
++) {
1603 struct hda_cvt_setup
*p
= snd_array_elem(&codec
->cvt_setups
, i
);
1605 really_cleanup_stream(codec
, p
);
1611 * amp access functions
1614 /* FIXME: more better hash key? */
1615 #define HDA_HASH_KEY(nid, dir, idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
1616 #define HDA_HASH_PINCAP_KEY(nid) (u32)((nid) + (0x02 << 24))
1617 #define HDA_HASH_PARPCM_KEY(nid) (u32)((nid) + (0x03 << 24))
1618 #define HDA_HASH_PARSTR_KEY(nid) (u32)((nid) + (0x04 << 24))
1619 #define INFO_AMP_CAPS (1<<0)
1620 #define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
1622 /* initialize the hash table */
1623 static void init_hda_cache(struct hda_cache_rec
*cache
,
1624 unsigned int record_size
)
1626 memset(cache
, 0, sizeof(*cache
));
1627 memset(cache
->hash
, 0xff, sizeof(cache
->hash
));
1628 snd_array_init(&cache
->buf
, record_size
, 64);
1631 static void free_hda_cache(struct hda_cache_rec
*cache
)
1633 snd_array_free(&cache
->buf
);
1636 /* query the hash. allocate an entry if not found. */
1637 static struct hda_cache_head
*get_hash(struct hda_cache_rec
*cache
, u32 key
)
1639 u16 idx
= key
% (u16
)ARRAY_SIZE(cache
->hash
);
1640 u16 cur
= cache
->hash
[idx
];
1641 struct hda_cache_head
*info
;
1643 while (cur
!= 0xffff) {
1644 info
= snd_array_elem(&cache
->buf
, cur
);
1645 if (info
->key
== key
)
1652 /* query the hash. allocate an entry if not found. */
1653 static struct hda_cache_head
*get_alloc_hash(struct hda_cache_rec
*cache
,
1656 struct hda_cache_head
*info
= get_hash(cache
, key
);
1659 /* add a new hash entry */
1660 info
= snd_array_new(&cache
->buf
);
1663 cur
= snd_array_index(&cache
->buf
, info
);
1667 idx
= key
% (u16
)ARRAY_SIZE(cache
->hash
);
1668 info
->next
= cache
->hash
[idx
];
1669 cache
->hash
[idx
] = cur
;
1674 /* query and allocate an amp hash entry */
1675 static inline struct hda_amp_info
*
1676 get_alloc_amp_hash(struct hda_codec
*codec
, u32 key
)
1678 return (struct hda_amp_info
*)get_alloc_hash(&codec
->amp_cache
, key
);
1681 /* overwrite the value with the key in the caps hash */
1682 static int write_caps_hash(struct hda_codec
*codec
, u32 key
, unsigned int val
)
1684 struct hda_amp_info
*info
;
1686 mutex_lock(&codec
->hash_mutex
);
1687 info
= get_alloc_amp_hash(codec
, key
);
1689 mutex_unlock(&codec
->hash_mutex
);
1692 info
->amp_caps
= val
;
1693 info
->head
.val
|= INFO_AMP_CAPS
;
1694 mutex_unlock(&codec
->hash_mutex
);
1698 /* query the value from the caps hash; if not found, fetch the current
1699 * value from the given function and store in the hash
1702 query_caps_hash(struct hda_codec
*codec
, hda_nid_t nid
, int dir
, u32 key
,
1703 unsigned int (*func
)(struct hda_codec
*, hda_nid_t
, int))
1705 struct hda_amp_info
*info
;
1708 mutex_lock(&codec
->hash_mutex
);
1709 info
= get_alloc_amp_hash(codec
, key
);
1711 mutex_unlock(&codec
->hash_mutex
);
1714 if (!(info
->head
.val
& INFO_AMP_CAPS
)) {
1715 mutex_unlock(&codec
->hash_mutex
); /* for reentrance */
1716 val
= func(codec
, nid
, dir
);
1717 write_caps_hash(codec
, key
, val
);
1719 val
= info
->amp_caps
;
1720 mutex_unlock(&codec
->hash_mutex
);
1725 static unsigned int read_amp_cap(struct hda_codec
*codec
, hda_nid_t nid
,
1728 if (!(get_wcaps(codec
, nid
) & AC_WCAP_AMP_OVRD
))
1730 return snd_hda_param_read(codec
, nid
,
1731 direction
== HDA_OUTPUT
?
1732 AC_PAR_AMP_OUT_CAP
: AC_PAR_AMP_IN_CAP
);
1736 * query_amp_caps - query AMP capabilities
1737 * @codec: the HD-auio codec
1738 * @nid: the NID to query
1739 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1741 * Query AMP capabilities for the given widget and direction.
1742 * Returns the obtained capability bits.
1744 * When cap bits have been already read, this doesn't read again but
1745 * returns the cached value.
1747 u32
query_amp_caps(struct hda_codec
*codec
, hda_nid_t nid
, int direction
)
1749 return query_caps_hash(codec
, nid
, direction
,
1750 HDA_HASH_KEY(nid
, direction
, 0),
1753 EXPORT_SYMBOL_GPL(query_amp_caps
);
1756 * snd_hda_check_amp_caps - query AMP capabilities
1757 * @codec: the HD-audio codec
1758 * @nid: the NID to query
1759 * @dir: either #HDA_INPUT or #HDA_OUTPUT
1760 * @bits: bit mask to check the result
1762 * Check whether the widget has the given amp capability for the direction.
1764 bool snd_hda_check_amp_caps(struct hda_codec
*codec
, hda_nid_t nid
,
1765 int dir
, unsigned int bits
)
1769 if (get_wcaps(codec
, nid
) & (1 << (dir
+ 1)))
1770 if (query_amp_caps(codec
, nid
, dir
) & bits
)
1774 EXPORT_SYMBOL_GPL(snd_hda_check_amp_caps
);
1777 * snd_hda_override_amp_caps - Override the AMP capabilities
1778 * @codec: the CODEC to clean up
1779 * @nid: the NID to clean up
1780 * @dir: either #HDA_INPUT or #HDA_OUTPUT
1781 * @caps: the capability bits to set
1783 * Override the cached AMP caps bits value by the given one.
1784 * This function is useful if the driver needs to adjust the AMP ranges,
1785 * e.g. limit to 0dB, etc.
1787 * Returns zero if successful or a negative error code.
1789 int snd_hda_override_amp_caps(struct hda_codec
*codec
, hda_nid_t nid
, int dir
,
1792 return write_caps_hash(codec
, HDA_HASH_KEY(nid
, dir
, 0), caps
);
1794 EXPORT_SYMBOL_GPL(snd_hda_override_amp_caps
);
1796 static unsigned int read_pin_cap(struct hda_codec
*codec
, hda_nid_t nid
,
1799 return snd_hda_param_read(codec
, nid
, AC_PAR_PIN_CAP
);
1803 * snd_hda_query_pin_caps - Query PIN capabilities
1804 * @codec: the HD-auio codec
1805 * @nid: the NID to query
1807 * Query PIN capabilities for the given widget.
1808 * Returns the obtained capability bits.
1810 * When cap bits have been already read, this doesn't read again but
1811 * returns the cached value.
1813 u32
snd_hda_query_pin_caps(struct hda_codec
*codec
, hda_nid_t nid
)
1815 return query_caps_hash(codec
, nid
, 0, HDA_HASH_PINCAP_KEY(nid
),
1818 EXPORT_SYMBOL_GPL(snd_hda_query_pin_caps
);
1821 * snd_hda_override_pin_caps - Override the pin capabilities
1823 * @nid: the NID to override
1824 * @caps: the capability bits to set
1826 * Override the cached PIN capabilitiy bits value by the given one.
1828 * Returns zero if successful or a negative error code.
1830 int snd_hda_override_pin_caps(struct hda_codec
*codec
, hda_nid_t nid
,
1833 return write_caps_hash(codec
, HDA_HASH_PINCAP_KEY(nid
), caps
);
1835 EXPORT_SYMBOL_GPL(snd_hda_override_pin_caps
);
1837 /* read or sync the hash value with the current value;
1838 * call within hash_mutex
1840 static struct hda_amp_info
*
1841 update_amp_hash(struct hda_codec
*codec
, hda_nid_t nid
, int ch
,
1842 int direction
, int index
, bool init_only
)
1844 struct hda_amp_info
*info
;
1845 unsigned int parm
, val
= 0;
1846 bool val_read
= false;
1849 info
= get_alloc_amp_hash(codec
, HDA_HASH_KEY(nid
, direction
, index
));
1852 if (!(info
->head
.val
& INFO_AMP_VOL(ch
))) {
1854 mutex_unlock(&codec
->hash_mutex
);
1855 parm
= ch
? AC_AMP_GET_RIGHT
: AC_AMP_GET_LEFT
;
1856 parm
|= direction
== HDA_OUTPUT
?
1857 AC_AMP_GET_OUTPUT
: AC_AMP_GET_INPUT
;
1859 val
= snd_hda_codec_read(codec
, nid
, 0,
1860 AC_VERB_GET_AMP_GAIN_MUTE
, parm
);
1863 mutex_lock(&codec
->hash_mutex
);
1866 info
->vol
[ch
] = val
;
1867 info
->head
.val
|= INFO_AMP_VOL(ch
);
1868 } else if (init_only
)
1874 * write the current volume in info to the h/w
1876 static void put_vol_mute(struct hda_codec
*codec
, unsigned int amp_caps
,
1877 hda_nid_t nid
, int ch
, int direction
, int index
,
1882 parm
= ch
? AC_AMP_SET_RIGHT
: AC_AMP_SET_LEFT
;
1883 parm
|= direction
== HDA_OUTPUT
? AC_AMP_SET_OUTPUT
: AC_AMP_SET_INPUT
;
1884 parm
|= index
<< AC_AMP_SET_INDEX_SHIFT
;
1885 if ((val
& HDA_AMP_MUTE
) && !(amp_caps
& AC_AMPCAP_MUTE
) &&
1886 (amp_caps
& AC_AMPCAP_MIN_MUTE
))
1887 ; /* set the zero value as a fake mute */
1890 snd_hda_codec_write(codec
, nid
, 0, AC_VERB_SET_AMP_GAIN_MUTE
, parm
);
1894 * snd_hda_codec_amp_read - Read AMP value
1895 * @codec: HD-audio codec
1896 * @nid: NID to read the AMP value
1897 * @ch: channel (left=0 or right=1)
1898 * @direction: #HDA_INPUT or #HDA_OUTPUT
1899 * @index: the index value (only for input direction)
1901 * Read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
1903 int snd_hda_codec_amp_read(struct hda_codec
*codec
, hda_nid_t nid
, int ch
,
1904 int direction
, int index
)
1906 struct hda_amp_info
*info
;
1907 unsigned int val
= 0;
1909 mutex_lock(&codec
->hash_mutex
);
1910 info
= update_amp_hash(codec
, nid
, ch
, direction
, index
, false);
1912 val
= info
->vol
[ch
];
1913 mutex_unlock(&codec
->hash_mutex
);
1916 EXPORT_SYMBOL_GPL(snd_hda_codec_amp_read
);
1918 static int codec_amp_update(struct hda_codec
*codec
, hda_nid_t nid
, int ch
,
1919 int direction
, int idx
, int mask
, int val
,
1920 bool init_only
, bool cache_only
)
1922 struct hda_amp_info
*info
;
1925 if (snd_BUG_ON(mask
& ~0xff))
1929 mutex_lock(&codec
->hash_mutex
);
1930 info
= update_amp_hash(codec
, nid
, ch
, direction
, idx
, init_only
);
1932 mutex_unlock(&codec
->hash_mutex
);
1935 val
|= info
->vol
[ch
] & ~mask
;
1936 if (info
->vol
[ch
] == val
) {
1937 mutex_unlock(&codec
->hash_mutex
);
1940 info
->vol
[ch
] = val
;
1941 info
->head
.dirty
|= cache_only
;
1942 caps
= info
->amp_caps
;
1943 mutex_unlock(&codec
->hash_mutex
);
1945 put_vol_mute(codec
, caps
, nid
, ch
, direction
, idx
, val
);
1950 * snd_hda_codec_amp_update - update the AMP value
1951 * @codec: HD-audio codec
1952 * @nid: NID to read the AMP value
1953 * @ch: channel (left=0 or right=1)
1954 * @direction: #HDA_INPUT or #HDA_OUTPUT
1955 * @idx: the index value (only for input direction)
1956 * @mask: bit mask to set
1957 * @val: the bits value to set
1959 * Update the AMP value with a bit mask.
1960 * Returns 0 if the value is unchanged, 1 if changed.
1962 int snd_hda_codec_amp_update(struct hda_codec
*codec
, hda_nid_t nid
, int ch
,
1963 int direction
, int idx
, int mask
, int val
)
1965 return codec_amp_update(codec
, nid
, ch
, direction
, idx
, mask
, val
,
1966 false, codec
->cached_write
);
1968 EXPORT_SYMBOL_GPL(snd_hda_codec_amp_update
);
1971 * snd_hda_codec_amp_stereo - update the AMP stereo values
1972 * @codec: HD-audio codec
1973 * @nid: NID to read the AMP value
1974 * @direction: #HDA_INPUT or #HDA_OUTPUT
1975 * @idx: the index value (only for input direction)
1976 * @mask: bit mask to set
1977 * @val: the bits value to set
1979 * Update the AMP values like snd_hda_codec_amp_update(), but for a
1980 * stereo widget with the same mask and value.
1982 int snd_hda_codec_amp_stereo(struct hda_codec
*codec
, hda_nid_t nid
,
1983 int direction
, int idx
, int mask
, int val
)
1987 if (snd_BUG_ON(mask
& ~0xff))
1989 for (ch
= 0; ch
< 2; ch
++)
1990 ret
|= snd_hda_codec_amp_update(codec
, nid
, ch
, direction
,
1994 EXPORT_SYMBOL_GPL(snd_hda_codec_amp_stereo
);
1997 * snd_hda_codec_amp_init - initialize the AMP value
1998 * @codec: the HDA codec
1999 * @nid: NID to read the AMP value
2000 * @ch: channel (left=0 or right=1)
2001 * @dir: #HDA_INPUT or #HDA_OUTPUT
2002 * @idx: the index value (only for input direction)
2003 * @mask: bit mask to set
2004 * @val: the bits value to set
2006 * Works like snd_hda_codec_amp_update() but it writes the value only at
2007 * the first access. If the amp was already initialized / updated beforehand,
2008 * this does nothing.
2010 int snd_hda_codec_amp_init(struct hda_codec
*codec
, hda_nid_t nid
, int ch
,
2011 int dir
, int idx
, int mask
, int val
)
2013 return codec_amp_update(codec
, nid
, ch
, dir
, idx
, mask
, val
, true,
2014 codec
->cached_write
);
2016 EXPORT_SYMBOL_GPL(snd_hda_codec_amp_init
);
2019 * snd_hda_codec_amp_init_stereo - initialize the stereo AMP value
2020 * @codec: the HDA codec
2021 * @nid: NID to read the AMP value
2022 * @dir: #HDA_INPUT or #HDA_OUTPUT
2023 * @idx: the index value (only for input direction)
2024 * @mask: bit mask to set
2025 * @val: the bits value to set
2027 * Call snd_hda_codec_amp_init() for both stereo channels.
2029 int snd_hda_codec_amp_init_stereo(struct hda_codec
*codec
, hda_nid_t nid
,
2030 int dir
, int idx
, int mask
, int val
)
2034 if (snd_BUG_ON(mask
& ~0xff))
2036 for (ch
= 0; ch
< 2; ch
++)
2037 ret
|= snd_hda_codec_amp_init(codec
, nid
, ch
, dir
,
2041 EXPORT_SYMBOL_GPL(snd_hda_codec_amp_init_stereo
);
2044 * snd_hda_codec_resume_amp - Resume all AMP commands from the cache
2045 * @codec: HD-audio codec
2047 * Resume the all amp commands from the cache.
2049 void snd_hda_codec_resume_amp(struct hda_codec
*codec
)
2053 mutex_lock(&codec
->hash_mutex
);
2054 codec
->cached_write
= 0;
2055 for (i
= 0; i
< codec
->amp_cache
.buf
.used
; i
++) {
2056 struct hda_amp_info
*buffer
;
2059 unsigned int idx
, dir
, ch
;
2060 struct hda_amp_info info
;
2062 buffer
= snd_array_elem(&codec
->amp_cache
.buf
, i
);
2063 if (!buffer
->head
.dirty
)
2065 buffer
->head
.dirty
= 0;
2067 key
= info
.head
.key
;
2071 idx
= (key
>> 16) & 0xff;
2072 dir
= (key
>> 24) & 0xff;
2073 for (ch
= 0; ch
< 2; ch
++) {
2074 if (!(info
.head
.val
& INFO_AMP_VOL(ch
)))
2076 mutex_unlock(&codec
->hash_mutex
);
2077 put_vol_mute(codec
, info
.amp_caps
, nid
, ch
, dir
, idx
,
2079 mutex_lock(&codec
->hash_mutex
);
2082 mutex_unlock(&codec
->hash_mutex
);
2084 EXPORT_SYMBOL_GPL(snd_hda_codec_resume_amp
);
2086 static u32
get_amp_max_value(struct hda_codec
*codec
, hda_nid_t nid
, int dir
,
2089 u32 caps
= query_amp_caps(codec
, nid
, dir
);
2091 caps
= (caps
& AC_AMPCAP_NUM_STEPS
) >> AC_AMPCAP_NUM_STEPS_SHIFT
;
2098 * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer
2099 * @kcontrol: referred ctl element
2100 * @uinfo: pointer to get/store the data
2102 * The control element is supposed to have the private_value field
2103 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2105 int snd_hda_mixer_amp_volume_info(struct snd_kcontrol
*kcontrol
,
2106 struct snd_ctl_elem_info
*uinfo
)
2108 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2109 u16 nid
= get_amp_nid(kcontrol
);
2110 u8 chs
= get_amp_channels(kcontrol
);
2111 int dir
= get_amp_direction(kcontrol
);
2112 unsigned int ofs
= get_amp_offset(kcontrol
);
2114 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
2115 uinfo
->count
= chs
== 3 ? 2 : 1;
2116 uinfo
->value
.integer
.min
= 0;
2117 uinfo
->value
.integer
.max
= get_amp_max_value(codec
, nid
, dir
, ofs
);
2118 if (!uinfo
->value
.integer
.max
) {
2120 "num_steps = 0 for NID=0x%x (ctl = %s)\n",
2121 nid
, kcontrol
->id
.name
);
2126 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_info
);
2129 static inline unsigned int
2130 read_amp_value(struct hda_codec
*codec
, hda_nid_t nid
,
2131 int ch
, int dir
, int idx
, unsigned int ofs
)
2134 val
= snd_hda_codec_amp_read(codec
, nid
, ch
, dir
, idx
);
2135 val
&= HDA_AMP_VOLMASK
;
2144 update_amp_value(struct hda_codec
*codec
, hda_nid_t nid
,
2145 int ch
, int dir
, int idx
, unsigned int ofs
,
2148 unsigned int maxval
;
2152 /* ofs = 0: raw max value */
2153 maxval
= get_amp_max_value(codec
, nid
, dir
, 0);
2156 return codec_amp_update(codec
, nid
, ch
, dir
, idx
, HDA_AMP_VOLMASK
, val
,
2157 false, !hda_codec_is_power_on(codec
));
2161 * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume
2162 * @kcontrol: ctl element
2163 * @ucontrol: pointer to get/store the data
2165 * The control element is supposed to have the private_value field
2166 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2168 int snd_hda_mixer_amp_volume_get(struct snd_kcontrol
*kcontrol
,
2169 struct snd_ctl_elem_value
*ucontrol
)
2171 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2172 hda_nid_t nid
= get_amp_nid(kcontrol
);
2173 int chs
= get_amp_channels(kcontrol
);
2174 int dir
= get_amp_direction(kcontrol
);
2175 int idx
= get_amp_index(kcontrol
);
2176 unsigned int ofs
= get_amp_offset(kcontrol
);
2177 long *valp
= ucontrol
->value
.integer
.value
;
2180 *valp
++ = read_amp_value(codec
, nid
, 0, dir
, idx
, ofs
);
2182 *valp
= read_amp_value(codec
, nid
, 1, dir
, idx
, ofs
);
2185 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_get
);
2188 * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume
2189 * @kcontrol: ctl element
2190 * @ucontrol: pointer to get/store the data
2192 * The control element is supposed to have the private_value field
2193 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2195 int snd_hda_mixer_amp_volume_put(struct snd_kcontrol
*kcontrol
,
2196 struct snd_ctl_elem_value
*ucontrol
)
2198 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2199 hda_nid_t nid
= get_amp_nid(kcontrol
);
2200 int chs
= get_amp_channels(kcontrol
);
2201 int dir
= get_amp_direction(kcontrol
);
2202 int idx
= get_amp_index(kcontrol
);
2203 unsigned int ofs
= get_amp_offset(kcontrol
);
2204 long *valp
= ucontrol
->value
.integer
.value
;
2208 change
= update_amp_value(codec
, nid
, 0, dir
, idx
, ofs
, *valp
);
2212 change
|= update_amp_value(codec
, nid
, 1, dir
, idx
, ofs
, *valp
);
2215 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_put
);
2218 * snd_hda_mixer_amp_volume_put - TLV callback for a standard AMP mixer volume
2219 * @kcontrol: ctl element
2220 * @op_flag: operation flag
2221 * @size: byte size of input TLV
2224 * The control element is supposed to have the private_value field
2225 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2227 int snd_hda_mixer_amp_tlv(struct snd_kcontrol
*kcontrol
, int op_flag
,
2228 unsigned int size
, unsigned int __user
*_tlv
)
2230 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2231 hda_nid_t nid
= get_amp_nid(kcontrol
);
2232 int dir
= get_amp_direction(kcontrol
);
2233 unsigned int ofs
= get_amp_offset(kcontrol
);
2234 bool min_mute
= get_amp_min_mute(kcontrol
);
2235 u32 caps
, val1
, val2
;
2237 if (size
< 4 * sizeof(unsigned int))
2239 caps
= query_amp_caps(codec
, nid
, dir
);
2240 val2
= (caps
& AC_AMPCAP_STEP_SIZE
) >> AC_AMPCAP_STEP_SIZE_SHIFT
;
2241 val2
= (val2
+ 1) * 25;
2242 val1
= -((caps
& AC_AMPCAP_OFFSET
) >> AC_AMPCAP_OFFSET_SHIFT
);
2244 val1
= ((int)val1
) * ((int)val2
);
2245 if (min_mute
|| (caps
& AC_AMPCAP_MIN_MUTE
))
2246 val2
|= TLV_DB_SCALE_MUTE
;
2247 if (put_user(SNDRV_CTL_TLVT_DB_SCALE
, _tlv
))
2249 if (put_user(2 * sizeof(unsigned int), _tlv
+ 1))
2251 if (put_user(val1
, _tlv
+ 2))
2253 if (put_user(val2
, _tlv
+ 3))
2257 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_tlv
);
2260 * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control
2261 * @codec: HD-audio codec
2262 * @nid: NID of a reference widget
2263 * @dir: #HDA_INPUT or #HDA_OUTPUT
2264 * @tlv: TLV data to be stored, at least 4 elements
2266 * Set (static) TLV data for a virtual master volume using the AMP caps
2267 * obtained from the reference NID.
2268 * The volume range is recalculated as if the max volume is 0dB.
2270 void snd_hda_set_vmaster_tlv(struct hda_codec
*codec
, hda_nid_t nid
, int dir
,
2276 caps
= query_amp_caps(codec
, nid
, dir
);
2277 nums
= (caps
& AC_AMPCAP_NUM_STEPS
) >> AC_AMPCAP_NUM_STEPS_SHIFT
;
2278 step
= (caps
& AC_AMPCAP_STEP_SIZE
) >> AC_AMPCAP_STEP_SIZE_SHIFT
;
2279 step
= (step
+ 1) * 25;
2280 tlv
[0] = SNDRV_CTL_TLVT_DB_SCALE
;
2281 tlv
[1] = 2 * sizeof(unsigned int);
2282 tlv
[2] = -nums
* step
;
2285 EXPORT_SYMBOL_GPL(snd_hda_set_vmaster_tlv
);
2287 /* find a mixer control element with the given name */
2288 static struct snd_kcontrol
*
2289 find_mixer_ctl(struct hda_codec
*codec
, const char *name
, int dev
, int idx
)
2291 struct snd_ctl_elem_id id
;
2292 memset(&id
, 0, sizeof(id
));
2293 id
.iface
= SNDRV_CTL_ELEM_IFACE_MIXER
;
2296 if (snd_BUG_ON(strlen(name
) >= sizeof(id
.name
)))
2298 strcpy(id
.name
, name
);
2299 return snd_ctl_find_id(codec
->card
, &id
);
2303 * snd_hda_find_mixer_ctl - Find a mixer control element with the given name
2304 * @codec: HD-audio codec
2305 * @name: ctl id name string
2307 * Get the control element with the given id string and IFACE_MIXER.
2309 struct snd_kcontrol
*snd_hda_find_mixer_ctl(struct hda_codec
*codec
,
2312 return find_mixer_ctl(codec
, name
, 0, 0);
2314 EXPORT_SYMBOL_GPL(snd_hda_find_mixer_ctl
);
2316 static int find_empty_mixer_ctl_idx(struct hda_codec
*codec
, const char *name
,
2320 /* 16 ctlrs should be large enough */
2321 for (i
= 0, idx
= start_idx
; i
< 16; i
++, idx
++) {
2322 if (!find_mixer_ctl(codec
, name
, 0, idx
))
2329 * snd_hda_ctl_add - Add a control element and assign to the codec
2330 * @codec: HD-audio codec
2331 * @nid: corresponding NID (optional)
2332 * @kctl: the control element to assign
2334 * Add the given control element to an array inside the codec instance.
2335 * All control elements belonging to a codec are supposed to be added
2336 * by this function so that a proper clean-up works at the free or
2337 * reconfiguration time.
2339 * If non-zero @nid is passed, the NID is assigned to the control element.
2340 * The assignment is shown in the codec proc file.
2342 * snd_hda_ctl_add() checks the control subdev id field whether
2343 * #HDA_SUBDEV_NID_FLAG bit is set. If set (and @nid is zero), the lower
2344 * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit
2345 * specifies if kctl->private_value is a HDA amplifier value.
2347 int snd_hda_ctl_add(struct hda_codec
*codec
, hda_nid_t nid
,
2348 struct snd_kcontrol
*kctl
)
2351 unsigned short flags
= 0;
2352 struct hda_nid_item
*item
;
2354 if (kctl
->id
.subdevice
& HDA_SUBDEV_AMP_FLAG
) {
2355 flags
|= HDA_NID_ITEM_AMP
;
2357 nid
= get_amp_nid_(kctl
->private_value
);
2359 if ((kctl
->id
.subdevice
& HDA_SUBDEV_NID_FLAG
) != 0 && nid
== 0)
2360 nid
= kctl
->id
.subdevice
& 0xffff;
2361 if (kctl
->id
.subdevice
& (HDA_SUBDEV_NID_FLAG
|HDA_SUBDEV_AMP_FLAG
))
2362 kctl
->id
.subdevice
= 0;
2363 err
= snd_ctl_add(codec
->card
, kctl
);
2366 item
= snd_array_new(&codec
->mixers
);
2371 item
->flags
= flags
;
2374 EXPORT_SYMBOL_GPL(snd_hda_ctl_add
);
2377 * snd_hda_add_nid - Assign a NID to a control element
2378 * @codec: HD-audio codec
2379 * @nid: corresponding NID (optional)
2380 * @kctl: the control element to assign
2381 * @index: index to kctl
2383 * Add the given control element to an array inside the codec instance.
2384 * This function is used when #snd_hda_ctl_add cannot be used for 1:1
2385 * NID:KCTL mapping - for example "Capture Source" selector.
2387 int snd_hda_add_nid(struct hda_codec
*codec
, struct snd_kcontrol
*kctl
,
2388 unsigned int index
, hda_nid_t nid
)
2390 struct hda_nid_item
*item
;
2393 item
= snd_array_new(&codec
->nids
);
2397 item
->index
= index
;
2401 codec_err(codec
, "no NID for mapping control %s:%d:%d\n",
2402 kctl
->id
.name
, kctl
->id
.index
, index
);
2405 EXPORT_SYMBOL_GPL(snd_hda_add_nid
);
2408 * snd_hda_ctls_clear - Clear all controls assigned to the given codec
2409 * @codec: HD-audio codec
2411 void snd_hda_ctls_clear(struct hda_codec
*codec
)
2414 struct hda_nid_item
*items
= codec
->mixers
.list
;
2415 for (i
= 0; i
< codec
->mixers
.used
; i
++)
2416 snd_ctl_remove(codec
->card
, items
[i
].kctl
);
2417 snd_array_free(&codec
->mixers
);
2418 snd_array_free(&codec
->nids
);
2422 * snd_hda_lock_devices - pseudo device locking
2425 * toggle card->shutdown to allow/disallow the device access (as a hack)
2427 int snd_hda_lock_devices(struct hda_bus
*bus
)
2429 struct snd_card
*card
= bus
->card
;
2430 struct hda_codec
*codec
;
2432 spin_lock(&card
->files_lock
);
2436 if (!list_empty(&card
->ctl_files
))
2439 list_for_each_entry(codec
, &bus
->codec_list
, list
) {
2440 struct hda_pcm
*cpcm
;
2441 list_for_each_entry(cpcm
, &codec
->pcm_list_head
, list
) {
2444 if (cpcm
->pcm
->streams
[0].substream_opened
||
2445 cpcm
->pcm
->streams
[1].substream_opened
)
2449 spin_unlock(&card
->files_lock
);
2455 spin_unlock(&card
->files_lock
);
2458 EXPORT_SYMBOL_GPL(snd_hda_lock_devices
);
2461 * snd_hda_unlock_devices - pseudo device unlocking
2464 void snd_hda_unlock_devices(struct hda_bus
*bus
)
2466 struct snd_card
*card
= bus
->card
;
2468 spin_lock(&card
->files_lock
);
2470 spin_unlock(&card
->files_lock
);
2472 EXPORT_SYMBOL_GPL(snd_hda_unlock_devices
);
2475 * snd_hda_codec_reset - Clear all objects assigned to the codec
2476 * @codec: HD-audio codec
2478 * This frees the all PCM and control elements assigned to the codec, and
2479 * clears the caches and restores the pin default configurations.
2481 * When a device is being used, it returns -EBSY. If successfully freed,
2484 int snd_hda_codec_reset(struct hda_codec
*codec
)
2486 struct hda_bus
*bus
= codec
->bus
;
2488 if (snd_hda_lock_devices(bus
) < 0)
2491 /* OK, let it free */
2492 if (device_is_registered(hda_codec_dev(codec
)))
2493 device_del(hda_codec_dev(codec
));
2495 /* allow device access again */
2496 snd_hda_unlock_devices(bus
);
2500 typedef int (*map_slave_func_t
)(struct hda_codec
*, void *, struct snd_kcontrol
*);
2502 /* apply the function to all matching slave ctls in the mixer list */
2503 static int map_slaves(struct hda_codec
*codec
, const char * const *slaves
,
2504 const char *suffix
, map_slave_func_t func
, void *data
)
2506 struct hda_nid_item
*items
;
2507 const char * const *s
;
2510 items
= codec
->mixers
.list
;
2511 for (i
= 0; i
< codec
->mixers
.used
; i
++) {
2512 struct snd_kcontrol
*sctl
= items
[i
].kctl
;
2513 if (!sctl
|| sctl
->id
.iface
!= SNDRV_CTL_ELEM_IFACE_MIXER
)
2515 for (s
= slaves
; *s
; s
++) {
2516 char tmpname
[sizeof(sctl
->id
.name
)];
2517 const char *name
= *s
;
2519 snprintf(tmpname
, sizeof(tmpname
), "%s %s",
2523 if (!strcmp(sctl
->id
.name
, name
)) {
2524 err
= func(codec
, data
, sctl
);
2534 static int check_slave_present(struct hda_codec
*codec
,
2535 void *data
, struct snd_kcontrol
*sctl
)
2540 /* guess the value corresponding to 0dB */
2541 static int get_kctl_0dB_offset(struct hda_codec
*codec
,
2542 struct snd_kcontrol
*kctl
, int *step_to_check
)
2545 const int *tlv
= NULL
;
2548 if (kctl
->vd
[0].access
& SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK
) {
2549 /* FIXME: set_fs() hack for obtaining user-space TLV data */
2550 mm_segment_t fs
= get_fs();
2552 if (!kctl
->tlv
.c(kctl
, 0, sizeof(_tlv
), _tlv
))
2555 } else if (kctl
->vd
[0].access
& SNDRV_CTL_ELEM_ACCESS_TLV_READ
)
2557 if (tlv
&& tlv
[0] == SNDRV_CTL_TLVT_DB_SCALE
) {
2559 step
&= ~TLV_DB_SCALE_MUTE
;
2562 if (*step_to_check
&& *step_to_check
!= step
) {
2563 codec_err(codec
, "Mismatching dB step for vmaster slave (%d!=%d)\n",
2564 - *step_to_check
, step
);
2567 *step_to_check
= step
;
2568 val
= -tlv
[2] / step
;
2573 /* call kctl->put with the given value(s) */
2574 static int put_kctl_with_value(struct snd_kcontrol
*kctl
, int val
)
2576 struct snd_ctl_elem_value
*ucontrol
;
2577 ucontrol
= kzalloc(sizeof(*ucontrol
), GFP_KERNEL
);
2580 ucontrol
->value
.integer
.value
[0] = val
;
2581 ucontrol
->value
.integer
.value
[1] = val
;
2582 kctl
->put(kctl
, ucontrol
);
2587 /* initialize the slave volume with 0dB */
2588 static int init_slave_0dB(struct hda_codec
*codec
,
2589 void *data
, struct snd_kcontrol
*slave
)
2591 int offset
= get_kctl_0dB_offset(codec
, slave
, data
);
2593 put_kctl_with_value(slave
, offset
);
2597 /* unmute the slave */
2598 static int init_slave_unmute(struct hda_codec
*codec
,
2599 void *data
, struct snd_kcontrol
*slave
)
2601 return put_kctl_with_value(slave
, 1);
2604 static int add_slave(struct hda_codec
*codec
,
2605 void *data
, struct snd_kcontrol
*slave
)
2607 return snd_ctl_add_slave(data
, slave
);
2611 * __snd_hda_add_vmaster - create a virtual master control and add slaves
2612 * @codec: HD-audio codec
2613 * @name: vmaster control name
2614 * @tlv: TLV data (optional)
2615 * @slaves: slave control names (optional)
2616 * @suffix: suffix string to each slave name (optional)
2617 * @init_slave_vol: initialize slaves to unmute/0dB
2618 * @ctl_ret: store the vmaster kcontrol in return
2620 * Create a virtual master control with the given name. The TLV data
2621 * must be either NULL or a valid data.
2623 * @slaves is a NULL-terminated array of strings, each of which is a
2624 * slave control name. All controls with these names are assigned to
2625 * the new virtual master control.
2627 * This function returns zero if successful or a negative error code.
2629 int __snd_hda_add_vmaster(struct hda_codec
*codec
, char *name
,
2630 unsigned int *tlv
, const char * const *slaves
,
2631 const char *suffix
, bool init_slave_vol
,
2632 struct snd_kcontrol
**ctl_ret
)
2634 struct snd_kcontrol
*kctl
;
2640 err
= map_slaves(codec
, slaves
, suffix
, check_slave_present
, NULL
);
2642 codec_dbg(codec
, "No slave found for %s\n", name
);
2645 kctl
= snd_ctl_make_virtual_master(name
, tlv
);
2648 err
= snd_hda_ctl_add(codec
, 0, kctl
);
2652 err
= map_slaves(codec
, slaves
, suffix
, add_slave
, kctl
);
2656 /* init with master mute & zero volume */
2657 put_kctl_with_value(kctl
, 0);
2658 if (init_slave_vol
) {
2660 map_slaves(codec
, slaves
, suffix
,
2661 tlv
? init_slave_0dB
: init_slave_unmute
, &step
);
2668 EXPORT_SYMBOL_GPL(__snd_hda_add_vmaster
);
2671 * mute-LED control using vmaster
2673 static int vmaster_mute_mode_info(struct snd_kcontrol
*kcontrol
,
2674 struct snd_ctl_elem_info
*uinfo
)
2676 static const char * const texts
[] = {
2677 "On", "Off", "Follow Master"
2680 return snd_ctl_enum_info(uinfo
, 1, 3, texts
);
2683 static int vmaster_mute_mode_get(struct snd_kcontrol
*kcontrol
,
2684 struct snd_ctl_elem_value
*ucontrol
)
2686 struct hda_vmaster_mute_hook
*hook
= snd_kcontrol_chip(kcontrol
);
2687 ucontrol
->value
.enumerated
.item
[0] = hook
->mute_mode
;
2691 static int vmaster_mute_mode_put(struct snd_kcontrol
*kcontrol
,
2692 struct snd_ctl_elem_value
*ucontrol
)
2694 struct hda_vmaster_mute_hook
*hook
= snd_kcontrol_chip(kcontrol
);
2695 unsigned int old_mode
= hook
->mute_mode
;
2697 hook
->mute_mode
= ucontrol
->value
.enumerated
.item
[0];
2698 if (hook
->mute_mode
> HDA_VMUTE_FOLLOW_MASTER
)
2699 hook
->mute_mode
= HDA_VMUTE_FOLLOW_MASTER
;
2700 if (old_mode
== hook
->mute_mode
)
2702 snd_hda_sync_vmaster_hook(hook
);
2706 static struct snd_kcontrol_new vmaster_mute_mode
= {
2707 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
2708 .name
= "Mute-LED Mode",
2709 .info
= vmaster_mute_mode_info
,
2710 .get
= vmaster_mute_mode_get
,
2711 .put
= vmaster_mute_mode_put
,
2715 * snd_hda_add_vmaster_hook - Add a vmaster hook for mute-LED
2716 * @codec: the HDA codec
2717 * @hook: the vmaster hook object
2718 * @expose_enum_ctl: flag to create an enum ctl
2720 * Add a mute-LED hook with the given vmaster switch kctl.
2721 * When @expose_enum_ctl is set, "Mute-LED Mode" control is automatically
2722 * created and associated with the given hook.
2724 int snd_hda_add_vmaster_hook(struct hda_codec
*codec
,
2725 struct hda_vmaster_mute_hook
*hook
,
2726 bool expose_enum_ctl
)
2728 struct snd_kcontrol
*kctl
;
2730 if (!hook
->hook
|| !hook
->sw_kctl
)
2732 snd_ctl_add_vmaster_hook(hook
->sw_kctl
, hook
->hook
, codec
);
2733 hook
->codec
= codec
;
2734 hook
->mute_mode
= HDA_VMUTE_FOLLOW_MASTER
;
2735 if (!expose_enum_ctl
)
2737 kctl
= snd_ctl_new1(&vmaster_mute_mode
, hook
);
2740 return snd_hda_ctl_add(codec
, 0, kctl
);
2742 EXPORT_SYMBOL_GPL(snd_hda_add_vmaster_hook
);
2745 * snd_hda_sync_vmaster_hook - Sync vmaster hook
2746 * @hook: the vmaster hook
2748 * Call the hook with the current value for synchronization.
2749 * Should be called in init callback.
2751 void snd_hda_sync_vmaster_hook(struct hda_vmaster_mute_hook
*hook
)
2753 if (!hook
->hook
|| !hook
->codec
)
2755 /* don't call vmaster hook in the destructor since it might have
2756 * been already destroyed
2758 if (hook
->codec
->bus
->shutdown
)
2760 switch (hook
->mute_mode
) {
2761 case HDA_VMUTE_FOLLOW_MASTER
:
2762 snd_ctl_sync_vmaster_hook(hook
->sw_kctl
);
2765 hook
->hook(hook
->codec
, hook
->mute_mode
);
2769 EXPORT_SYMBOL_GPL(snd_hda_sync_vmaster_hook
);
2773 * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch
2774 * @kcontrol: referred ctl element
2775 * @uinfo: pointer to get/store the data
2777 * The control element is supposed to have the private_value field
2778 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2780 int snd_hda_mixer_amp_switch_info(struct snd_kcontrol
*kcontrol
,
2781 struct snd_ctl_elem_info
*uinfo
)
2783 int chs
= get_amp_channels(kcontrol
);
2785 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_BOOLEAN
;
2786 uinfo
->count
= chs
== 3 ? 2 : 1;
2787 uinfo
->value
.integer
.min
= 0;
2788 uinfo
->value
.integer
.max
= 1;
2791 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_info
);
2794 * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch
2795 * @kcontrol: ctl element
2796 * @ucontrol: pointer to get/store the data
2798 * The control element is supposed to have the private_value field
2799 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2801 int snd_hda_mixer_amp_switch_get(struct snd_kcontrol
*kcontrol
,
2802 struct snd_ctl_elem_value
*ucontrol
)
2804 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2805 hda_nid_t nid
= get_amp_nid(kcontrol
);
2806 int chs
= get_amp_channels(kcontrol
);
2807 int dir
= get_amp_direction(kcontrol
);
2808 int idx
= get_amp_index(kcontrol
);
2809 long *valp
= ucontrol
->value
.integer
.value
;
2812 *valp
++ = (snd_hda_codec_amp_read(codec
, nid
, 0, dir
, idx
) &
2813 HDA_AMP_MUTE
) ? 0 : 1;
2815 *valp
= (snd_hda_codec_amp_read(codec
, nid
, 1, dir
, idx
) &
2816 HDA_AMP_MUTE
) ? 0 : 1;
2819 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_get
);
2822 * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch
2823 * @kcontrol: ctl element
2824 * @ucontrol: pointer to get/store the data
2826 * The control element is supposed to have the private_value field
2827 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2829 int snd_hda_mixer_amp_switch_put(struct snd_kcontrol
*kcontrol
,
2830 struct snd_ctl_elem_value
*ucontrol
)
2832 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2833 hda_nid_t nid
= get_amp_nid(kcontrol
);
2834 int chs
= get_amp_channels(kcontrol
);
2835 int dir
= get_amp_direction(kcontrol
);
2836 int idx
= get_amp_index(kcontrol
);
2837 long *valp
= ucontrol
->value
.integer
.value
;
2841 change
= codec_amp_update(codec
, nid
, 0, dir
, idx
,
2843 *valp
? 0 : HDA_AMP_MUTE
, false,
2844 !hda_codec_is_power_on(codec
));
2848 change
|= codec_amp_update(codec
, nid
, 1, dir
, idx
,
2850 *valp
? 0 : HDA_AMP_MUTE
, false,
2851 !hda_codec_is_power_on(codec
));
2852 hda_call_check_power_status(codec
, nid
);
2855 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_put
);
2858 * bound volume controls
2860 * bind multiple volumes (# indices, from 0)
2863 #define AMP_VAL_IDX_SHIFT 19
2864 #define AMP_VAL_IDX_MASK (0x0f<<19)
2867 * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control
2868 * @kcontrol: ctl element
2869 * @ucontrol: pointer to get/store the data
2871 * The control element is supposed to have the private_value field
2872 * set up via HDA_BIND_MUTE*() macros.
2874 int snd_hda_mixer_bind_switch_get(struct snd_kcontrol
*kcontrol
,
2875 struct snd_ctl_elem_value
*ucontrol
)
2877 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2881 mutex_lock(&codec
->control_mutex
);
2882 pval
= kcontrol
->private_value
;
2883 kcontrol
->private_value
= pval
& ~AMP_VAL_IDX_MASK
; /* index 0 */
2884 err
= snd_hda_mixer_amp_switch_get(kcontrol
, ucontrol
);
2885 kcontrol
->private_value
= pval
;
2886 mutex_unlock(&codec
->control_mutex
);
2889 EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_switch_get
);
2892 * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control
2893 * @kcontrol: ctl element
2894 * @ucontrol: pointer to get/store the data
2896 * The control element is supposed to have the private_value field
2897 * set up via HDA_BIND_MUTE*() macros.
2899 int snd_hda_mixer_bind_switch_put(struct snd_kcontrol
*kcontrol
,
2900 struct snd_ctl_elem_value
*ucontrol
)
2902 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2904 int i
, indices
, err
= 0, change
= 0;
2906 mutex_lock(&codec
->control_mutex
);
2907 pval
= kcontrol
->private_value
;
2908 indices
= (pval
& AMP_VAL_IDX_MASK
) >> AMP_VAL_IDX_SHIFT
;
2909 for (i
= 0; i
< indices
; i
++) {
2910 kcontrol
->private_value
= (pval
& ~AMP_VAL_IDX_MASK
) |
2911 (i
<< AMP_VAL_IDX_SHIFT
);
2912 err
= snd_hda_mixer_amp_switch_put(kcontrol
, ucontrol
);
2917 kcontrol
->private_value
= pval
;
2918 mutex_unlock(&codec
->control_mutex
);
2919 return err
< 0 ? err
: change
;
2921 EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_switch_put
);
2924 * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control
2925 * @kcontrol: referred ctl element
2926 * @uinfo: pointer to get/store the data
2928 * The control element is supposed to have the private_value field
2929 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2931 int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol
*kcontrol
,
2932 struct snd_ctl_elem_info
*uinfo
)
2934 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2935 struct hda_bind_ctls
*c
;
2938 mutex_lock(&codec
->control_mutex
);
2939 c
= (struct hda_bind_ctls
*)kcontrol
->private_value
;
2940 kcontrol
->private_value
= *c
->values
;
2941 err
= c
->ops
->info(kcontrol
, uinfo
);
2942 kcontrol
->private_value
= (long)c
;
2943 mutex_unlock(&codec
->control_mutex
);
2946 EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_info
);
2949 * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control
2950 * @kcontrol: ctl element
2951 * @ucontrol: pointer to get/store the data
2953 * The control element is supposed to have the private_value field
2954 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2956 int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol
*kcontrol
,
2957 struct snd_ctl_elem_value
*ucontrol
)
2959 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2960 struct hda_bind_ctls
*c
;
2963 mutex_lock(&codec
->control_mutex
);
2964 c
= (struct hda_bind_ctls
*)kcontrol
->private_value
;
2965 kcontrol
->private_value
= *c
->values
;
2966 err
= c
->ops
->get(kcontrol
, ucontrol
);
2967 kcontrol
->private_value
= (long)c
;
2968 mutex_unlock(&codec
->control_mutex
);
2971 EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_get
);
2974 * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control
2975 * @kcontrol: ctl element
2976 * @ucontrol: pointer to get/store the data
2978 * The control element is supposed to have the private_value field
2979 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2981 int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol
*kcontrol
,
2982 struct snd_ctl_elem_value
*ucontrol
)
2984 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2985 struct hda_bind_ctls
*c
;
2986 unsigned long *vals
;
2987 int err
= 0, change
= 0;
2989 mutex_lock(&codec
->control_mutex
);
2990 c
= (struct hda_bind_ctls
*)kcontrol
->private_value
;
2991 for (vals
= c
->values
; *vals
; vals
++) {
2992 kcontrol
->private_value
= *vals
;
2993 err
= c
->ops
->put(kcontrol
, ucontrol
);
2998 kcontrol
->private_value
= (long)c
;
2999 mutex_unlock(&codec
->control_mutex
);
3000 return err
< 0 ? err
: change
;
3002 EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_put
);
3005 * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control
3006 * @kcontrol: ctl element
3007 * @op_flag: operation flag
3008 * @size: byte size of input TLV
3011 * The control element is supposed to have the private_value field
3012 * set up via HDA_BIND_VOL() macro.
3014 int snd_hda_mixer_bind_tlv(struct snd_kcontrol
*kcontrol
, int op_flag
,
3015 unsigned int size
, unsigned int __user
*tlv
)
3017 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
3018 struct hda_bind_ctls
*c
;
3021 mutex_lock(&codec
->control_mutex
);
3022 c
= (struct hda_bind_ctls
*)kcontrol
->private_value
;
3023 kcontrol
->private_value
= *c
->values
;
3024 err
= c
->ops
->tlv(kcontrol
, op_flag
, size
, tlv
);
3025 kcontrol
->private_value
= (long)c
;
3026 mutex_unlock(&codec
->control_mutex
);
3029 EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_tlv
);
3031 struct hda_ctl_ops snd_hda_bind_vol
= {
3032 .info
= snd_hda_mixer_amp_volume_info
,
3033 .get
= snd_hda_mixer_amp_volume_get
,
3034 .put
= snd_hda_mixer_amp_volume_put
,
3035 .tlv
= snd_hda_mixer_amp_tlv
3037 EXPORT_SYMBOL_GPL(snd_hda_bind_vol
);
3039 struct hda_ctl_ops snd_hda_bind_sw
= {
3040 .info
= snd_hda_mixer_amp_switch_info
,
3041 .get
= snd_hda_mixer_amp_switch_get
,
3042 .put
= snd_hda_mixer_amp_switch_put
,
3043 .tlv
= snd_hda_mixer_amp_tlv
3045 EXPORT_SYMBOL_GPL(snd_hda_bind_sw
);
3048 * SPDIF out controls
3051 static int snd_hda_spdif_mask_info(struct snd_kcontrol
*kcontrol
,
3052 struct snd_ctl_elem_info
*uinfo
)
3054 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_IEC958
;
3059 static int snd_hda_spdif_cmask_get(struct snd_kcontrol
*kcontrol
,
3060 struct snd_ctl_elem_value
*ucontrol
)
3062 ucontrol
->value
.iec958
.status
[0] = IEC958_AES0_PROFESSIONAL
|
3063 IEC958_AES0_NONAUDIO
|
3064 IEC958_AES0_CON_EMPHASIS_5015
|
3065 IEC958_AES0_CON_NOT_COPYRIGHT
;
3066 ucontrol
->value
.iec958
.status
[1] = IEC958_AES1_CON_CATEGORY
|
3067 IEC958_AES1_CON_ORIGINAL
;
3071 static int snd_hda_spdif_pmask_get(struct snd_kcontrol
*kcontrol
,
3072 struct snd_ctl_elem_value
*ucontrol
)
3074 ucontrol
->value
.iec958
.status
[0] = IEC958_AES0_PROFESSIONAL
|
3075 IEC958_AES0_NONAUDIO
|
3076 IEC958_AES0_PRO_EMPHASIS_5015
;
3080 static int snd_hda_spdif_default_get(struct snd_kcontrol
*kcontrol
,
3081 struct snd_ctl_elem_value
*ucontrol
)
3083 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
3084 int idx
= kcontrol
->private_value
;
3085 struct hda_spdif_out
*spdif
;
3087 mutex_lock(&codec
->spdif_mutex
);
3088 spdif
= snd_array_elem(&codec
->spdif_out
, idx
);
3089 ucontrol
->value
.iec958
.status
[0] = spdif
->status
& 0xff;
3090 ucontrol
->value
.iec958
.status
[1] = (spdif
->status
>> 8) & 0xff;
3091 ucontrol
->value
.iec958
.status
[2] = (spdif
->status
>> 16) & 0xff;
3092 ucontrol
->value
.iec958
.status
[3] = (spdif
->status
>> 24) & 0xff;
3093 mutex_unlock(&codec
->spdif_mutex
);
3098 /* convert from SPDIF status bits to HDA SPDIF bits
3099 * bit 0 (DigEn) is always set zero (to be filled later)
3101 static unsigned short convert_from_spdif_status(unsigned int sbits
)
3103 unsigned short val
= 0;
3105 if (sbits
& IEC958_AES0_PROFESSIONAL
)
3106 val
|= AC_DIG1_PROFESSIONAL
;
3107 if (sbits
& IEC958_AES0_NONAUDIO
)
3108 val
|= AC_DIG1_NONAUDIO
;
3109 if (sbits
& IEC958_AES0_PROFESSIONAL
) {
3110 if ((sbits
& IEC958_AES0_PRO_EMPHASIS
) ==
3111 IEC958_AES0_PRO_EMPHASIS_5015
)
3112 val
|= AC_DIG1_EMPHASIS
;
3114 if ((sbits
& IEC958_AES0_CON_EMPHASIS
) ==
3115 IEC958_AES0_CON_EMPHASIS_5015
)
3116 val
|= AC_DIG1_EMPHASIS
;
3117 if (!(sbits
& IEC958_AES0_CON_NOT_COPYRIGHT
))
3118 val
|= AC_DIG1_COPYRIGHT
;
3119 if (sbits
& (IEC958_AES1_CON_ORIGINAL
<< 8))
3120 val
|= AC_DIG1_LEVEL
;
3121 val
|= sbits
& (IEC958_AES1_CON_CATEGORY
<< 8);
3126 /* convert to SPDIF status bits from HDA SPDIF bits
3128 static unsigned int convert_to_spdif_status(unsigned short val
)
3130 unsigned int sbits
= 0;
3132 if (val
& AC_DIG1_NONAUDIO
)
3133 sbits
|= IEC958_AES0_NONAUDIO
;
3134 if (val
& AC_DIG1_PROFESSIONAL
)
3135 sbits
|= IEC958_AES0_PROFESSIONAL
;
3136 if (sbits
& IEC958_AES0_PROFESSIONAL
) {
3137 if (val
& AC_DIG1_EMPHASIS
)
3138 sbits
|= IEC958_AES0_PRO_EMPHASIS_5015
;
3140 if (val
& AC_DIG1_EMPHASIS
)
3141 sbits
|= IEC958_AES0_CON_EMPHASIS_5015
;
3142 if (!(val
& AC_DIG1_COPYRIGHT
))
3143 sbits
|= IEC958_AES0_CON_NOT_COPYRIGHT
;
3144 if (val
& AC_DIG1_LEVEL
)
3145 sbits
|= (IEC958_AES1_CON_ORIGINAL
<< 8);
3146 sbits
|= val
& (0x7f << 8);
3151 /* set digital convert verbs both for the given NID and its slaves */
3152 static void set_dig_out(struct hda_codec
*codec
, hda_nid_t nid
,
3157 snd_hda_codec_write_cache(codec
, nid
, 0, verb
, val
);
3158 d
= codec
->slave_dig_outs
;
3162 snd_hda_codec_write_cache(codec
, *d
, 0, verb
, val
);
3165 static inline void set_dig_out_convert(struct hda_codec
*codec
, hda_nid_t nid
,
3169 set_dig_out(codec
, nid
, AC_VERB_SET_DIGI_CONVERT_1
, dig1
);
3171 set_dig_out(codec
, nid
, AC_VERB_SET_DIGI_CONVERT_2
, dig2
);
3174 static int snd_hda_spdif_default_put(struct snd_kcontrol
*kcontrol
,
3175 struct snd_ctl_elem_value
*ucontrol
)
3177 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
3178 int idx
= kcontrol
->private_value
;
3179 struct hda_spdif_out
*spdif
;
3184 mutex_lock(&codec
->spdif_mutex
);
3185 spdif
= snd_array_elem(&codec
->spdif_out
, idx
);
3187 spdif
->status
= ucontrol
->value
.iec958
.status
[0] |
3188 ((unsigned int)ucontrol
->value
.iec958
.status
[1] << 8) |
3189 ((unsigned int)ucontrol
->value
.iec958
.status
[2] << 16) |
3190 ((unsigned int)ucontrol
->value
.iec958
.status
[3] << 24);
3191 val
= convert_from_spdif_status(spdif
->status
);
3192 val
|= spdif
->ctls
& 1;
3193 change
= spdif
->ctls
!= val
;
3195 if (change
&& nid
!= (u16
)-1)
3196 set_dig_out_convert(codec
, nid
, val
& 0xff, (val
>> 8) & 0xff);
3197 mutex_unlock(&codec
->spdif_mutex
);
3201 #define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
3203 static int snd_hda_spdif_out_switch_get(struct snd_kcontrol
*kcontrol
,
3204 struct snd_ctl_elem_value
*ucontrol
)
3206 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
3207 int idx
= kcontrol
->private_value
;
3208 struct hda_spdif_out
*spdif
;
3210 mutex_lock(&codec
->spdif_mutex
);
3211 spdif
= snd_array_elem(&codec
->spdif_out
, idx
);
3212 ucontrol
->value
.integer
.value
[0] = spdif
->ctls
& AC_DIG1_ENABLE
;
3213 mutex_unlock(&codec
->spdif_mutex
);
3217 static inline void set_spdif_ctls(struct hda_codec
*codec
, hda_nid_t nid
,
3220 set_dig_out_convert(codec
, nid
, dig1
, dig2
);
3221 /* unmute amp switch (if any) */
3222 if ((get_wcaps(codec
, nid
) & AC_WCAP_OUT_AMP
) &&
3223 (dig1
& AC_DIG1_ENABLE
))
3224 snd_hda_codec_amp_stereo(codec
, nid
, HDA_OUTPUT
, 0,
3228 static int snd_hda_spdif_out_switch_put(struct snd_kcontrol
*kcontrol
,
3229 struct snd_ctl_elem_value
*ucontrol
)
3231 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
3232 int idx
= kcontrol
->private_value
;
3233 struct hda_spdif_out
*spdif
;
3238 mutex_lock(&codec
->spdif_mutex
);
3239 spdif
= snd_array_elem(&codec
->spdif_out
, idx
);
3241 val
= spdif
->ctls
& ~AC_DIG1_ENABLE
;
3242 if (ucontrol
->value
.integer
.value
[0])
3243 val
|= AC_DIG1_ENABLE
;
3244 change
= spdif
->ctls
!= val
;
3246 if (change
&& nid
!= (u16
)-1)
3247 set_spdif_ctls(codec
, nid
, val
& 0xff, -1);
3248 mutex_unlock(&codec
->spdif_mutex
);
3252 static struct snd_kcontrol_new dig_mixes
[] = {
3254 .access
= SNDRV_CTL_ELEM_ACCESS_READ
,
3255 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
3256 .name
= SNDRV_CTL_NAME_IEC958("", PLAYBACK
, CON_MASK
),
3257 .info
= snd_hda_spdif_mask_info
,
3258 .get
= snd_hda_spdif_cmask_get
,
3261 .access
= SNDRV_CTL_ELEM_ACCESS_READ
,
3262 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
3263 .name
= SNDRV_CTL_NAME_IEC958("", PLAYBACK
, PRO_MASK
),
3264 .info
= snd_hda_spdif_mask_info
,
3265 .get
= snd_hda_spdif_pmask_get
,
3268 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
3269 .name
= SNDRV_CTL_NAME_IEC958("", PLAYBACK
, DEFAULT
),
3270 .info
= snd_hda_spdif_mask_info
,
3271 .get
= snd_hda_spdif_default_get
,
3272 .put
= snd_hda_spdif_default_put
,
3275 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
3276 .name
= SNDRV_CTL_NAME_IEC958("", PLAYBACK
, SWITCH
),
3277 .info
= snd_hda_spdif_out_switch_info
,
3278 .get
= snd_hda_spdif_out_switch_get
,
3279 .put
= snd_hda_spdif_out_switch_put
,
3285 * snd_hda_create_dig_out_ctls - create Output SPDIF-related controls
3286 * @codec: the HDA codec
3287 * @associated_nid: NID that new ctls associated with
3288 * @cvt_nid: converter NID
3289 * @type: HDA_PCM_TYPE_*
3290 * Creates controls related with the digital output.
3291 * Called from each patch supporting the digital out.
3293 * Returns 0 if successful, or a negative error code.
3295 int snd_hda_create_dig_out_ctls(struct hda_codec
*codec
,
3296 hda_nid_t associated_nid
,
3301 struct snd_kcontrol
*kctl
;
3302 struct snd_kcontrol_new
*dig_mix
;
3304 const int spdif_index
= 16;
3305 struct hda_spdif_out
*spdif
;
3306 struct hda_bus
*bus
= codec
->bus
;
3308 if (bus
->primary_dig_out_type
== HDA_PCM_TYPE_HDMI
&&
3309 type
== HDA_PCM_TYPE_SPDIF
) {
3311 } else if (bus
->primary_dig_out_type
== HDA_PCM_TYPE_SPDIF
&&
3312 type
== HDA_PCM_TYPE_HDMI
) {
3313 /* suppose a single SPDIF device */
3314 for (dig_mix
= dig_mixes
; dig_mix
->name
; dig_mix
++) {
3315 kctl
= find_mixer_ctl(codec
, dig_mix
->name
, 0, 0);
3318 kctl
->id
.index
= spdif_index
;
3320 bus
->primary_dig_out_type
= HDA_PCM_TYPE_HDMI
;
3322 if (!bus
->primary_dig_out_type
)
3323 bus
->primary_dig_out_type
= type
;
3325 idx
= find_empty_mixer_ctl_idx(codec
, "IEC958 Playback Switch", idx
);
3327 codec_err(codec
, "too many IEC958 outputs\n");
3330 spdif
= snd_array_new(&codec
->spdif_out
);
3333 for (dig_mix
= dig_mixes
; dig_mix
->name
; dig_mix
++) {
3334 kctl
= snd_ctl_new1(dig_mix
, codec
);
3337 kctl
->id
.index
= idx
;
3338 kctl
->private_value
= codec
->spdif_out
.used
- 1;
3339 err
= snd_hda_ctl_add(codec
, associated_nid
, kctl
);
3343 spdif
->nid
= cvt_nid
;
3344 spdif
->ctls
= snd_hda_codec_read(codec
, cvt_nid
, 0,
3345 AC_VERB_GET_DIGI_CONVERT_1
, 0);
3346 spdif
->status
= convert_to_spdif_status(spdif
->ctls
);
3349 EXPORT_SYMBOL_GPL(snd_hda_create_dig_out_ctls
);
3352 * snd_hda_spdif_out_of_nid - get the hda_spdif_out entry from the given NID
3353 * @codec: the HDA codec
3356 * call within spdif_mutex lock
3358 struct hda_spdif_out
*snd_hda_spdif_out_of_nid(struct hda_codec
*codec
,
3362 for (i
= 0; i
< codec
->spdif_out
.used
; i
++) {
3363 struct hda_spdif_out
*spdif
=
3364 snd_array_elem(&codec
->spdif_out
, i
);
3365 if (spdif
->nid
== nid
)
3370 EXPORT_SYMBOL_GPL(snd_hda_spdif_out_of_nid
);
3373 * snd_hda_spdif_ctls_unassign - Unassign the given SPDIF ctl
3374 * @codec: the HDA codec
3375 * @idx: the SPDIF ctl index
3377 * Unassign the widget from the given SPDIF control.
3379 void snd_hda_spdif_ctls_unassign(struct hda_codec
*codec
, int idx
)
3381 struct hda_spdif_out
*spdif
;
3383 mutex_lock(&codec
->spdif_mutex
);
3384 spdif
= snd_array_elem(&codec
->spdif_out
, idx
);
3385 spdif
->nid
= (u16
)-1;
3386 mutex_unlock(&codec
->spdif_mutex
);
3388 EXPORT_SYMBOL_GPL(snd_hda_spdif_ctls_unassign
);
3391 * snd_hda_spdif_ctls_assign - Assign the SPDIF controls to the given NID
3392 * @codec: the HDA codec
3393 * @idx: the SPDIF ctl idx
3396 * Assign the widget to the SPDIF control with the given index.
3398 void snd_hda_spdif_ctls_assign(struct hda_codec
*codec
, int idx
, hda_nid_t nid
)
3400 struct hda_spdif_out
*spdif
;
3403 mutex_lock(&codec
->spdif_mutex
);
3404 spdif
= snd_array_elem(&codec
->spdif_out
, idx
);
3405 if (spdif
->nid
!= nid
) {
3408 set_spdif_ctls(codec
, nid
, val
& 0xff, (val
>> 8) & 0xff);
3410 mutex_unlock(&codec
->spdif_mutex
);
3412 EXPORT_SYMBOL_GPL(snd_hda_spdif_ctls_assign
);
3415 * SPDIF sharing with analog output
3417 static int spdif_share_sw_get(struct snd_kcontrol
*kcontrol
,
3418 struct snd_ctl_elem_value
*ucontrol
)
3420 struct hda_multi_out
*mout
= snd_kcontrol_chip(kcontrol
);
3421 ucontrol
->value
.integer
.value
[0] = mout
->share_spdif
;
3425 static int spdif_share_sw_put(struct snd_kcontrol
*kcontrol
,
3426 struct snd_ctl_elem_value
*ucontrol
)
3428 struct hda_multi_out
*mout
= snd_kcontrol_chip(kcontrol
);
3429 mout
->share_spdif
= !!ucontrol
->value
.integer
.value
[0];
3433 static struct snd_kcontrol_new spdif_share_sw
= {
3434 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
3435 .name
= "IEC958 Default PCM Playback Switch",
3436 .info
= snd_ctl_boolean_mono_info
,
3437 .get
= spdif_share_sw_get
,
3438 .put
= spdif_share_sw_put
,
3442 * snd_hda_create_spdif_share_sw - create Default PCM switch
3443 * @codec: the HDA codec
3444 * @mout: multi-out instance
3446 int snd_hda_create_spdif_share_sw(struct hda_codec
*codec
,
3447 struct hda_multi_out
*mout
)
3449 struct snd_kcontrol
*kctl
;
3451 if (!mout
->dig_out_nid
)
3454 kctl
= snd_ctl_new1(&spdif_share_sw
, mout
);
3457 /* ATTENTION: here mout is passed as private_data, instead of codec */
3458 return snd_hda_ctl_add(codec
, mout
->dig_out_nid
, kctl
);
3460 EXPORT_SYMBOL_GPL(snd_hda_create_spdif_share_sw
);
3466 #define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
3468 static int snd_hda_spdif_in_switch_get(struct snd_kcontrol
*kcontrol
,
3469 struct snd_ctl_elem_value
*ucontrol
)
3471 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
3473 ucontrol
->value
.integer
.value
[0] = codec
->spdif_in_enable
;
3477 static int snd_hda_spdif_in_switch_put(struct snd_kcontrol
*kcontrol
,
3478 struct snd_ctl_elem_value
*ucontrol
)
3480 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
3481 hda_nid_t nid
= kcontrol
->private_value
;
3482 unsigned int val
= !!ucontrol
->value
.integer
.value
[0];
3485 mutex_lock(&codec
->spdif_mutex
);
3486 change
= codec
->spdif_in_enable
!= val
;
3488 codec
->spdif_in_enable
= val
;
3489 snd_hda_codec_write_cache(codec
, nid
, 0,
3490 AC_VERB_SET_DIGI_CONVERT_1
, val
);
3492 mutex_unlock(&codec
->spdif_mutex
);
3496 static int snd_hda_spdif_in_status_get(struct snd_kcontrol
*kcontrol
,
3497 struct snd_ctl_elem_value
*ucontrol
)
3499 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
3500 hda_nid_t nid
= kcontrol
->private_value
;
3504 val
= snd_hda_codec_read(codec
, nid
, 0, AC_VERB_GET_DIGI_CONVERT_1
, 0);
3505 sbits
= convert_to_spdif_status(val
);
3506 ucontrol
->value
.iec958
.status
[0] = sbits
;
3507 ucontrol
->value
.iec958
.status
[1] = sbits
>> 8;
3508 ucontrol
->value
.iec958
.status
[2] = sbits
>> 16;
3509 ucontrol
->value
.iec958
.status
[3] = sbits
>> 24;
3513 static struct snd_kcontrol_new dig_in_ctls
[] = {
3515 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
3516 .name
= SNDRV_CTL_NAME_IEC958("", CAPTURE
, SWITCH
),
3517 .info
= snd_hda_spdif_in_switch_info
,
3518 .get
= snd_hda_spdif_in_switch_get
,
3519 .put
= snd_hda_spdif_in_switch_put
,
3522 .access
= SNDRV_CTL_ELEM_ACCESS_READ
,
3523 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
3524 .name
= SNDRV_CTL_NAME_IEC958("", CAPTURE
, DEFAULT
),
3525 .info
= snd_hda_spdif_mask_info
,
3526 .get
= snd_hda_spdif_in_status_get
,
3532 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
3533 * @codec: the HDA codec
3534 * @nid: audio in widget NID
3536 * Creates controls related with the SPDIF input.
3537 * Called from each patch supporting the SPDIF in.
3539 * Returns 0 if successful, or a negative error code.
3541 int snd_hda_create_spdif_in_ctls(struct hda_codec
*codec
, hda_nid_t nid
)
3544 struct snd_kcontrol
*kctl
;
3545 struct snd_kcontrol_new
*dig_mix
;
3548 idx
= find_empty_mixer_ctl_idx(codec
, "IEC958 Capture Switch", 0);
3550 codec_err(codec
, "too many IEC958 inputs\n");
3553 for (dig_mix
= dig_in_ctls
; dig_mix
->name
; dig_mix
++) {
3554 kctl
= snd_ctl_new1(dig_mix
, codec
);
3557 kctl
->private_value
= nid
;
3558 err
= snd_hda_ctl_add(codec
, nid
, kctl
);
3562 codec
->spdif_in_enable
=
3563 snd_hda_codec_read(codec
, nid
, 0,
3564 AC_VERB_GET_DIGI_CONVERT_1
, 0) &
3568 EXPORT_SYMBOL_GPL(snd_hda_create_spdif_in_ctls
);
3574 /* build a 31bit cache key with the widget id and the command parameter */
3575 #define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
3576 #define get_cmd_cache_nid(key) ((key) & 0xff)
3577 #define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
3580 * snd_hda_codec_write_cache - send a single command with caching
3581 * @codec: the HDA codec
3582 * @nid: NID to send the command
3583 * @flags: optional bit flags
3584 * @verb: the verb to send
3585 * @parm: the parameter for the verb
3587 * Send a single command without waiting for response.
3589 * Returns 0 if successful, or a negative error code.
3591 int snd_hda_codec_write_cache(struct hda_codec
*codec
, hda_nid_t nid
,
3592 int flags
, unsigned int verb
, unsigned int parm
)
3595 struct hda_cache_head
*c
;
3597 unsigned int cache_only
;
3599 cache_only
= codec
->cached_write
;
3601 err
= snd_hda_codec_write(codec
, nid
, flags
, verb
, parm
);
3606 /* parm may contain the verb stuff for get/set amp */
3607 verb
= verb
| (parm
>> 8);
3609 key
= build_cmd_cache_key(nid
, verb
);
3610 mutex_lock(&codec
->bus
->cmd_mutex
);
3611 c
= get_alloc_hash(&codec
->cmd_cache
, key
);
3614 c
->dirty
= cache_only
;
3616 mutex_unlock(&codec
->bus
->cmd_mutex
);
3619 EXPORT_SYMBOL_GPL(snd_hda_codec_write_cache
);
3622 * snd_hda_codec_update_cache - check cache and write the cmd only when needed
3623 * @codec: the HDA codec
3624 * @nid: NID to send the command
3625 * @flags: optional bit flags
3626 * @verb: the verb to send
3627 * @parm: the parameter for the verb
3629 * This function works like snd_hda_codec_write_cache(), but it doesn't send
3630 * command if the parameter is already identical with the cached value.
3631 * If not, it sends the command and refreshes the cache.
3633 * Returns 0 if successful, or a negative error code.
3635 int snd_hda_codec_update_cache(struct hda_codec
*codec
, hda_nid_t nid
,
3636 int flags
, unsigned int verb
, unsigned int parm
)
3638 struct hda_cache_head
*c
;
3641 /* parm may contain the verb stuff for get/set amp */
3642 verb
= verb
| (parm
>> 8);
3644 key
= build_cmd_cache_key(nid
, verb
);
3645 mutex_lock(&codec
->bus
->cmd_mutex
);
3646 c
= get_hash(&codec
->cmd_cache
, key
);
3647 if (c
&& c
->val
== parm
) {
3648 mutex_unlock(&codec
->bus
->cmd_mutex
);
3651 mutex_unlock(&codec
->bus
->cmd_mutex
);
3652 return snd_hda_codec_write_cache(codec
, nid
, flags
, verb
, parm
);
3654 EXPORT_SYMBOL_GPL(snd_hda_codec_update_cache
);
3657 * snd_hda_codec_resume_cache - Resume the all commands from the cache
3658 * @codec: HD-audio codec
3660 * Execute all verbs recorded in the command caches to resume.
3662 void snd_hda_codec_resume_cache(struct hda_codec
*codec
)
3666 mutex_lock(&codec
->hash_mutex
);
3667 codec
->cached_write
= 0;
3668 for (i
= 0; i
< codec
->cmd_cache
.buf
.used
; i
++) {
3669 struct hda_cache_head
*buffer
;
3672 buffer
= snd_array_elem(&codec
->cmd_cache
.buf
, i
);
3679 mutex_unlock(&codec
->hash_mutex
);
3680 snd_hda_codec_write(codec
, get_cmd_cache_nid(key
), 0,
3681 get_cmd_cache_cmd(key
), buffer
->val
);
3682 mutex_lock(&codec
->hash_mutex
);
3684 mutex_unlock(&codec
->hash_mutex
);
3686 EXPORT_SYMBOL_GPL(snd_hda_codec_resume_cache
);
3689 * snd_hda_sequence_write_cache - sequence writes with caching
3690 * @codec: the HDA codec
3691 * @seq: VERB array to send
3693 * Send the commands sequentially from the given array.
3694 * Thte commands are recorded on cache for power-save and resume.
3695 * The array must be terminated with NID=0.
3697 void snd_hda_sequence_write_cache(struct hda_codec
*codec
,
3698 const struct hda_verb
*seq
)
3700 for (; seq
->nid
; seq
++)
3701 snd_hda_codec_write_cache(codec
, seq
->nid
, 0, seq
->verb
,
3704 EXPORT_SYMBOL_GPL(snd_hda_sequence_write_cache
);
3707 * snd_hda_codec_flush_cache - Execute all pending (cached) amps / verbs
3708 * @codec: HD-audio codec
3710 void snd_hda_codec_flush_cache(struct hda_codec
*codec
)
3712 snd_hda_codec_resume_amp(codec
);
3713 snd_hda_codec_resume_cache(codec
);
3715 EXPORT_SYMBOL_GPL(snd_hda_codec_flush_cache
);
3718 * snd_hda_codec_set_power_to_all - Set the power state to all widgets
3719 * @codec: the HDA codec
3720 * @fg: function group (not used now)
3721 * @power_state: the power state to set (AC_PWRST_*)
3723 * Set the given power state to all widgets that have the power control.
3724 * If the codec has power_filter set, it evaluates the power state and
3725 * filter out if it's unchanged as D3.
3727 void snd_hda_codec_set_power_to_all(struct hda_codec
*codec
, hda_nid_t fg
,
3728 unsigned int power_state
)
3730 hda_nid_t nid
= codec
->start_nid
;
3733 for (i
= 0; i
< codec
->num_nodes
; i
++, nid
++) {
3734 unsigned int wcaps
= get_wcaps(codec
, nid
);
3735 unsigned int state
= power_state
;
3736 if (!(wcaps
& AC_WCAP_POWER
))
3738 if (codec
->power_filter
) {
3739 state
= codec
->power_filter(codec
, nid
, power_state
);
3740 if (state
!= power_state
&& power_state
== AC_PWRST_D3
)
3743 snd_hda_codec_write(codec
, nid
, 0, AC_VERB_SET_POWER_STATE
,
3747 EXPORT_SYMBOL_GPL(snd_hda_codec_set_power_to_all
);
3750 * supported power states check
3752 static bool snd_hda_codec_get_supported_ps(struct hda_codec
*codec
, hda_nid_t fg
,
3753 unsigned int power_state
)
3755 int sup
= snd_hda_param_read(codec
, fg
, AC_PAR_POWER_STATE
);
3759 if (sup
& power_state
)
3766 * wait until the state is reached, returns the current state
3768 static unsigned int hda_sync_power_state(struct hda_codec
*codec
,
3770 unsigned int power_state
)
3772 unsigned long end_time
= jiffies
+ msecs_to_jiffies(500);
3773 unsigned int state
, actual_state
;
3776 state
= snd_hda_codec_read(codec
, fg
, 0,
3777 AC_VERB_GET_POWER_STATE
, 0);
3778 if (state
& AC_PWRST_ERROR
)
3780 actual_state
= (state
>> 4) & 0x0f;
3781 if (actual_state
== power_state
)
3783 if (time_after_eq(jiffies
, end_time
))
3785 /* wait until the codec reachs to the target state */
3792 * snd_hda_codec_eapd_power_filter - A power filter callback for EAPD
3793 * @codec: the HDA codec
3795 * @power_state: power state to evalue
3797 * Don't power down the widget if it controls eapd and EAPD_BTLENABLE is set.
3798 * This can be used a codec power_filter callback.
3800 unsigned int snd_hda_codec_eapd_power_filter(struct hda_codec
*codec
,
3802 unsigned int power_state
)
3804 if (nid
== codec
->afg
|| nid
== codec
->mfg
)
3806 if (power_state
== AC_PWRST_D3
&&
3807 get_wcaps_type(get_wcaps(codec
, nid
)) == AC_WID_PIN
&&
3808 (snd_hda_query_pin_caps(codec
, nid
) & AC_PINCAP_EAPD
)) {
3809 int eapd
= snd_hda_codec_read(codec
, nid
, 0,
3810 AC_VERB_GET_EAPD_BTLENABLE
, 0);
3816 EXPORT_SYMBOL_GPL(snd_hda_codec_eapd_power_filter
);
3819 * set power state of the codec, and return the power state
3821 static unsigned int hda_set_power_state(struct hda_codec
*codec
,
3822 unsigned int power_state
)
3824 hda_nid_t fg
= codec
->afg
? codec
->afg
: codec
->mfg
;
3829 /* this delay seems necessary to avoid click noise at power-down */
3830 if (power_state
== AC_PWRST_D3
) {
3831 if (codec
->depop_delay
< 0)
3832 msleep(codec
->epss
? 10 : 100);
3833 else if (codec
->depop_delay
> 0)
3834 msleep(codec
->depop_delay
);
3835 flags
= HDA_RW_NO_RESPONSE_FALLBACK
;
3838 /* repeat power states setting at most 10 times*/
3839 for (count
= 0; count
< 10; count
++) {
3840 if (codec
->patch_ops
.set_power_state
)
3841 codec
->patch_ops
.set_power_state(codec
, fg
,
3844 state
= power_state
;
3845 if (codec
->power_filter
)
3846 state
= codec
->power_filter(codec
, fg
, state
);
3847 if (state
== power_state
|| power_state
!= AC_PWRST_D3
)
3848 snd_hda_codec_read(codec
, fg
, flags
,
3849 AC_VERB_SET_POWER_STATE
,
3851 snd_hda_codec_set_power_to_all(codec
, fg
, power_state
);
3853 state
= hda_sync_power_state(codec
, fg
, power_state
);
3854 if (!(state
& AC_PWRST_ERROR
))
3861 /* sync power states of all widgets;
3862 * this is called at the end of codec parsing
3864 static void sync_power_up_states(struct hda_codec
*codec
)
3866 hda_nid_t nid
= codec
->start_nid
;
3869 /* don't care if no filter is used */
3870 if (!codec
->power_filter
)
3873 for (i
= 0; i
< codec
->num_nodes
; i
++, nid
++) {
3874 unsigned int wcaps
= get_wcaps(codec
, nid
);
3875 unsigned int target
;
3876 if (!(wcaps
& AC_WCAP_POWER
))
3878 target
= codec
->power_filter(codec
, nid
, AC_PWRST_D0
);
3879 if (target
== AC_PWRST_D0
)
3881 if (!snd_hda_check_power_state(codec
, nid
, target
))
3882 snd_hda_codec_write(codec
, nid
, 0,
3883 AC_VERB_SET_POWER_STATE
, target
);
3887 #ifdef CONFIG_SND_HDA_RECONFIG
3888 /* execute additional init verbs */
3889 static void hda_exec_init_verbs(struct hda_codec
*codec
)
3891 if (codec
->init_verbs
.list
)
3892 snd_hda_sequence_write(codec
, codec
->init_verbs
.list
);
3895 static inline void hda_exec_init_verbs(struct hda_codec
*codec
) {}
3899 /* update the power on/off account with the current jiffies */
3900 static void update_power_acct(struct hda_codec
*codec
, bool on
)
3902 unsigned long delta
= jiffies
- codec
->power_jiffies
;
3905 codec
->power_on_acct
+= delta
;
3907 codec
->power_off_acct
+= delta
;
3908 codec
->power_jiffies
+= delta
;
3911 void snd_hda_update_power_acct(struct hda_codec
*codec
)
3913 update_power_acct(codec
, hda_codec_is_power_on(codec
));
3917 * call suspend and power-down; used both from PM and power-save
3918 * this function returns the power state in the end
3920 static unsigned int hda_call_codec_suspend(struct hda_codec
*codec
)
3924 atomic_inc(&codec
->in_pm
);
3926 if (codec
->patch_ops
.suspend
)
3927 codec
->patch_ops
.suspend(codec
);
3928 hda_cleanup_all_streams(codec
);
3929 state
= hda_set_power_state(codec
, AC_PWRST_D3
);
3930 trace_hda_power_down(codec
);
3931 update_power_acct(codec
, true);
3932 atomic_dec(&codec
->in_pm
);
3936 /* mark all entries of cmd and amp caches dirty */
3937 static void hda_mark_cmd_cache_dirty(struct hda_codec
*codec
)
3940 for (i
= 0; i
< codec
->cmd_cache
.buf
.used
; i
++) {
3941 struct hda_cache_head
*cmd
;
3942 cmd
= snd_array_elem(&codec
->cmd_cache
.buf
, i
);
3945 for (i
= 0; i
< codec
->amp_cache
.buf
.used
; i
++) {
3946 struct hda_amp_info
*amp
;
3947 amp
= snd_array_elem(&codec
->amp_cache
.buf
, i
);
3948 amp
->head
.dirty
= 1;
3953 * kick up codec; used both from PM and power-save
3955 static void hda_call_codec_resume(struct hda_codec
*codec
)
3957 atomic_inc(&codec
->in_pm
);
3959 trace_hda_power_up(codec
);
3960 hda_mark_cmd_cache_dirty(codec
);
3962 codec
->power_jiffies
= jiffies
;
3964 hda_set_power_state(codec
, AC_PWRST_D0
);
3965 restore_shutup_pins(codec
);
3966 hda_exec_init_verbs(codec
);
3967 snd_hda_jack_set_dirty_all(codec
);
3968 if (codec
->patch_ops
.resume
)
3969 codec
->patch_ops
.resume(codec
);
3971 if (codec
->patch_ops
.init
)
3972 codec
->patch_ops
.init(codec
);
3973 snd_hda_codec_resume_amp(codec
);
3974 snd_hda_codec_resume_cache(codec
);
3977 if (codec
->jackpoll_interval
)
3978 hda_jackpoll_work(&codec
->jackpoll_work
.work
);
3980 snd_hda_jack_report_sync(codec
);
3981 atomic_dec(&codec
->in_pm
);
3984 static int hda_codec_runtime_suspend(struct device
*dev
)
3986 struct hda_codec
*codec
= dev_to_hda_codec(dev
);
3987 struct hda_pcm
*pcm
;
3990 cancel_delayed_work_sync(&codec
->jackpoll_work
);
3991 list_for_each_entry(pcm
, &codec
->pcm_list_head
, list
)
3992 snd_pcm_suspend_all(pcm
->pcm
);
3993 state
= hda_call_codec_suspend(codec
);
3994 if (codec
->d3_stop_clk
&& codec
->epss
&& (state
& AC_PWRST_CLK_STOP_OK
))
3995 clear_bit(codec
->addr
, &codec
->bus
->codec_powered
);
3999 static int hda_codec_runtime_resume(struct device
*dev
)
4001 struct hda_codec
*codec
= dev_to_hda_codec(dev
);
4003 set_bit(codec
->addr
, &codec
->bus
->codec_powered
);
4004 hda_call_codec_resume(codec
);
4005 pm_runtime_mark_last_busy(dev
);
4008 #endif /* CONFIG_PM */
4010 /* referred in hda_bind.c */
4011 const struct dev_pm_ops hda_codec_driver_pm
= {
4012 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend
,
4013 pm_runtime_force_resume
)
4014 SET_RUNTIME_PM_OPS(hda_codec_runtime_suspend
, hda_codec_runtime_resume
,
4019 * add standard channel maps if not specified
4021 static int add_std_chmaps(struct hda_codec
*codec
)
4023 struct hda_pcm
*pcm
;
4026 list_for_each_entry(pcm
, &codec
->pcm_list_head
, list
) {
4027 for (str
= 0; str
< 2; str
++) {
4028 struct hda_pcm_stream
*hinfo
= &pcm
->stream
[str
];
4029 struct snd_pcm_chmap
*chmap
;
4030 const struct snd_pcm_chmap_elem
*elem
;
4034 if (!pcm
|| !hinfo
->substreams
)
4036 elem
= hinfo
->chmap
? hinfo
->chmap
: snd_pcm_std_chmaps
;
4037 err
= snd_pcm_add_chmap_ctls(pcm
->pcm
, str
, elem
,
4038 hinfo
->channels_max
,
4042 chmap
->channel_mask
= SND_PCM_CHMAP_MASK_2468
;
4048 /* default channel maps for 2.1 speakers;
4049 * since HD-audio supports only stereo, odd number channels are omitted
4051 const struct snd_pcm_chmap_elem snd_pcm_2_1_chmaps
[] = {
4053 .map
= { SNDRV_CHMAP_FL
, SNDRV_CHMAP_FR
} },
4055 .map
= { SNDRV_CHMAP_FL
, SNDRV_CHMAP_FR
,
4056 SNDRV_CHMAP_LFE
, SNDRV_CHMAP_LFE
} },
4059 EXPORT_SYMBOL_GPL(snd_pcm_2_1_chmaps
);
4061 int snd_hda_codec_build_controls(struct hda_codec
*codec
)
4064 hda_exec_init_verbs(codec
);
4065 /* continue to initialize... */
4066 if (codec
->patch_ops
.init
)
4067 err
= codec
->patch_ops
.init(codec
);
4068 if (!err
&& codec
->patch_ops
.build_controls
)
4069 err
= codec
->patch_ops
.build_controls(codec
);
4073 /* we create chmaps here instead of build_pcms */
4074 err
= add_std_chmaps(codec
);
4078 if (codec
->jackpoll_interval
)
4079 hda_jackpoll_work(&codec
->jackpoll_work
.work
);
4081 snd_hda_jack_report_sync(codec
); /* call at the last init point */
4082 sync_power_up_states(codec
);
4089 struct hda_rate_tbl
{
4091 unsigned int alsa_bits
;
4092 unsigned int hda_fmt
;
4095 /* rate = base * mult / div */
4096 #define HDA_RATE(base, mult, div) \
4097 (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \
4098 (((div) - 1) << AC_FMT_DIV_SHIFT))
4100 static struct hda_rate_tbl rate_bits
[] = {
4101 /* rate in Hz, ALSA rate bitmask, HDA format value */
4103 /* autodetected value used in snd_hda_query_supported_pcm */
4104 { 8000, SNDRV_PCM_RATE_8000
, HDA_RATE(48, 1, 6) },
4105 { 11025, SNDRV_PCM_RATE_11025
, HDA_RATE(44, 1, 4) },
4106 { 16000, SNDRV_PCM_RATE_16000
, HDA_RATE(48, 1, 3) },
4107 { 22050, SNDRV_PCM_RATE_22050
, HDA_RATE(44, 1, 2) },
4108 { 32000, SNDRV_PCM_RATE_32000
, HDA_RATE(48, 2, 3) },
4109 { 44100, SNDRV_PCM_RATE_44100
, HDA_RATE(44, 1, 1) },
4110 { 48000, SNDRV_PCM_RATE_48000
, HDA_RATE(48, 1, 1) },
4111 { 88200, SNDRV_PCM_RATE_88200
, HDA_RATE(44, 2, 1) },
4112 { 96000, SNDRV_PCM_RATE_96000
, HDA_RATE(48, 2, 1) },
4113 { 176400, SNDRV_PCM_RATE_176400
, HDA_RATE(44, 4, 1) },
4114 { 192000, SNDRV_PCM_RATE_192000
, HDA_RATE(48, 4, 1) },
4115 #define AC_PAR_PCM_RATE_BITS 11
4116 /* up to bits 10, 384kHZ isn't supported properly */
4118 /* not autodetected value */
4119 { 9600, SNDRV_PCM_RATE_KNOT
, HDA_RATE(48, 1, 5) },
4121 { 0 } /* terminator */
4125 * snd_hda_calc_stream_format - calculate format bitset
4126 * @codec: HD-audio codec
4127 * @rate: the sample rate
4128 * @channels: the number of channels
4129 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
4130 * @maxbps: the max. bps
4131 * @spdif_ctls: HD-audio SPDIF status bits (0 if irrelevant)
4133 * Calculate the format bitset from the given rate, channels and th PCM format.
4135 * Return zero if invalid.
4137 unsigned int snd_hda_calc_stream_format(struct hda_codec
*codec
,
4139 unsigned int channels
,
4140 unsigned int format
,
4141 unsigned int maxbps
,
4142 unsigned short spdif_ctls
)
4145 unsigned int val
= 0;
4147 for (i
= 0; rate_bits
[i
].hz
; i
++)
4148 if (rate_bits
[i
].hz
== rate
) {
4149 val
= rate_bits
[i
].hda_fmt
;
4152 if (!rate_bits
[i
].hz
) {
4153 codec_dbg(codec
, "invalid rate %d\n", rate
);
4157 if (channels
== 0 || channels
> 8) {
4158 codec_dbg(codec
, "invalid channels %d\n", channels
);
4161 val
|= channels
- 1;
4163 switch (snd_pcm_format_width(format
)) {
4165 val
|= AC_FMT_BITS_8
;
4168 val
|= AC_FMT_BITS_16
;
4173 if (maxbps
>= 32 || format
== SNDRV_PCM_FORMAT_FLOAT_LE
)
4174 val
|= AC_FMT_BITS_32
;
4175 else if (maxbps
>= 24)
4176 val
|= AC_FMT_BITS_24
;
4178 val
|= AC_FMT_BITS_20
;
4181 codec_dbg(codec
, "invalid format width %d\n",
4182 snd_pcm_format_width(format
));
4186 if (spdif_ctls
& AC_DIG1_NONAUDIO
)
4187 val
|= AC_FMT_TYPE_NON_PCM
;
4191 EXPORT_SYMBOL_GPL(snd_hda_calc_stream_format
);
4193 static unsigned int get_pcm_param(struct hda_codec
*codec
, hda_nid_t nid
,
4196 unsigned int val
= 0;
4197 if (nid
!= codec
->afg
&&
4198 (get_wcaps(codec
, nid
) & AC_WCAP_FORMAT_OVRD
))
4199 val
= snd_hda_param_read(codec
, nid
, AC_PAR_PCM
);
4200 if (!val
|| val
== -1)
4201 val
= snd_hda_param_read(codec
, codec
->afg
, AC_PAR_PCM
);
4202 if (!val
|| val
== -1)
4207 static unsigned int query_pcm_param(struct hda_codec
*codec
, hda_nid_t nid
)
4209 return query_caps_hash(codec
, nid
, 0, HDA_HASH_PARPCM_KEY(nid
),
4213 static unsigned int get_stream_param(struct hda_codec
*codec
, hda_nid_t nid
,
4216 unsigned int streams
= snd_hda_param_read(codec
, nid
, AC_PAR_STREAM
);
4217 if (!streams
|| streams
== -1)
4218 streams
= snd_hda_param_read(codec
, codec
->afg
, AC_PAR_STREAM
);
4219 if (!streams
|| streams
== -1)
4224 static unsigned int query_stream_param(struct hda_codec
*codec
, hda_nid_t nid
)
4226 return query_caps_hash(codec
, nid
, 0, HDA_HASH_PARSTR_KEY(nid
),
4231 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
4232 * @codec: the HDA codec
4233 * @nid: NID to query
4234 * @ratesp: the pointer to store the detected rate bitflags
4235 * @formatsp: the pointer to store the detected formats
4236 * @bpsp: the pointer to store the detected format widths
4238 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
4239 * or @bsps argument is ignored.
4241 * Returns 0 if successful, otherwise a negative error code.
4243 int snd_hda_query_supported_pcm(struct hda_codec
*codec
, hda_nid_t nid
,
4244 u32
*ratesp
, u64
*formatsp
, unsigned int *bpsp
)
4246 unsigned int i
, val
, wcaps
;
4248 wcaps
= get_wcaps(codec
, nid
);
4249 val
= query_pcm_param(codec
, nid
);
4253 for (i
= 0; i
< AC_PAR_PCM_RATE_BITS
; i
++) {
4255 rates
|= rate_bits
[i
].alsa_bits
;
4259 "rates == 0 (nid=0x%x, val=0x%x, ovrd=%i)\n",
4261 (wcaps
& AC_WCAP_FORMAT_OVRD
) ? 1 : 0);
4267 if (formatsp
|| bpsp
) {
4269 unsigned int streams
, bps
;
4271 streams
= query_stream_param(codec
, nid
);
4276 if (streams
& AC_SUPFMT_PCM
) {
4277 if (val
& AC_SUPPCM_BITS_8
) {
4278 formats
|= SNDRV_PCM_FMTBIT_U8
;
4281 if (val
& AC_SUPPCM_BITS_16
) {
4282 formats
|= SNDRV_PCM_FMTBIT_S16_LE
;
4285 if (wcaps
& AC_WCAP_DIGITAL
) {
4286 if (val
& AC_SUPPCM_BITS_32
)
4287 formats
|= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
;
4288 if (val
& (AC_SUPPCM_BITS_20
|AC_SUPPCM_BITS_24
))
4289 formats
|= SNDRV_PCM_FMTBIT_S32_LE
;
4290 if (val
& AC_SUPPCM_BITS_24
)
4292 else if (val
& AC_SUPPCM_BITS_20
)
4294 } else if (val
& (AC_SUPPCM_BITS_20
|AC_SUPPCM_BITS_24
|
4295 AC_SUPPCM_BITS_32
)) {
4296 formats
|= SNDRV_PCM_FMTBIT_S32_LE
;
4297 if (val
& AC_SUPPCM_BITS_32
)
4299 else if (val
& AC_SUPPCM_BITS_24
)
4301 else if (val
& AC_SUPPCM_BITS_20
)
4305 #if 0 /* FIXME: CS4206 doesn't work, which is the only codec supporting float */
4306 if (streams
& AC_SUPFMT_FLOAT32
) {
4307 formats
|= SNDRV_PCM_FMTBIT_FLOAT_LE
;
4312 if (streams
== AC_SUPFMT_AC3
) {
4313 /* should be exclusive */
4314 /* temporary hack: we have still no proper support
4315 * for the direct AC3 stream...
4317 formats
|= SNDRV_PCM_FMTBIT_U8
;
4322 "formats == 0 (nid=0x%x, val=0x%x, ovrd=%i, streams=0x%x)\n",
4324 (wcaps
& AC_WCAP_FORMAT_OVRD
) ? 1 : 0,
4329 *formatsp
= formats
;
4336 EXPORT_SYMBOL_GPL(snd_hda_query_supported_pcm
);
4339 * snd_hda_is_supported_format - Check the validity of the format
4340 * @codec: HD-audio codec
4341 * @nid: NID to check
4342 * @format: the HD-audio format value to check
4344 * Check whether the given node supports the format value.
4346 * Returns 1 if supported, 0 if not.
4348 int snd_hda_is_supported_format(struct hda_codec
*codec
, hda_nid_t nid
,
4349 unsigned int format
)
4352 unsigned int val
= 0, rate
, stream
;
4354 val
= query_pcm_param(codec
, nid
);
4358 rate
= format
& 0xff00;
4359 for (i
= 0; i
< AC_PAR_PCM_RATE_BITS
; i
++)
4360 if (rate_bits
[i
].hda_fmt
== rate
) {
4365 if (i
>= AC_PAR_PCM_RATE_BITS
)
4368 stream
= query_stream_param(codec
, nid
);
4372 if (stream
& AC_SUPFMT_PCM
) {
4373 switch (format
& 0xf0) {
4375 if (!(val
& AC_SUPPCM_BITS_8
))
4379 if (!(val
& AC_SUPPCM_BITS_16
))
4383 if (!(val
& AC_SUPPCM_BITS_20
))
4387 if (!(val
& AC_SUPPCM_BITS_24
))
4391 if (!(val
& AC_SUPPCM_BITS_32
))
4398 /* FIXME: check for float32 and AC3? */
4403 EXPORT_SYMBOL_GPL(snd_hda_is_supported_format
);
4408 static int hda_pcm_default_open_close(struct hda_pcm_stream
*hinfo
,
4409 struct hda_codec
*codec
,
4410 struct snd_pcm_substream
*substream
)
4415 static int hda_pcm_default_prepare(struct hda_pcm_stream
*hinfo
,
4416 struct hda_codec
*codec
,
4417 unsigned int stream_tag
,
4418 unsigned int format
,
4419 struct snd_pcm_substream
*substream
)
4421 snd_hda_codec_setup_stream(codec
, hinfo
->nid
, stream_tag
, 0, format
);
4425 static int hda_pcm_default_cleanup(struct hda_pcm_stream
*hinfo
,
4426 struct hda_codec
*codec
,
4427 struct snd_pcm_substream
*substream
)
4429 snd_hda_codec_cleanup_stream(codec
, hinfo
->nid
);
4433 static int set_pcm_default_values(struct hda_codec
*codec
,
4434 struct hda_pcm_stream
*info
)
4438 /* query support PCM information from the given NID */
4439 if (info
->nid
&& (!info
->rates
|| !info
->formats
)) {
4440 err
= snd_hda_query_supported_pcm(codec
, info
->nid
,
4441 info
->rates
? NULL
: &info
->rates
,
4442 info
->formats
? NULL
: &info
->formats
,
4443 info
->maxbps
? NULL
: &info
->maxbps
);
4447 if (info
->ops
.open
== NULL
)
4448 info
->ops
.open
= hda_pcm_default_open_close
;
4449 if (info
->ops
.close
== NULL
)
4450 info
->ops
.close
= hda_pcm_default_open_close
;
4451 if (info
->ops
.prepare
== NULL
) {
4452 if (snd_BUG_ON(!info
->nid
))
4454 info
->ops
.prepare
= hda_pcm_default_prepare
;
4456 if (info
->ops
.cleanup
== NULL
) {
4457 if (snd_BUG_ON(!info
->nid
))
4459 info
->ops
.cleanup
= hda_pcm_default_cleanup
;
4465 * codec prepare/cleanup entries
4468 * snd_hda_codec_prepare - Prepare a stream
4469 * @codec: the HDA codec
4470 * @hinfo: PCM information
4471 * @stream: stream tag to assign
4472 * @format: format id to assign
4473 * @substream: PCM substream to assign
4475 * Calls the prepare callback set by the codec with the given arguments.
4476 * Clean up the inactive streams when successful.
4478 int snd_hda_codec_prepare(struct hda_codec
*codec
,
4479 struct hda_pcm_stream
*hinfo
,
4480 unsigned int stream
,
4481 unsigned int format
,
4482 struct snd_pcm_substream
*substream
)
4485 mutex_lock(&codec
->bus
->prepare_mutex
);
4486 if (hinfo
->ops
.prepare
)
4487 ret
= hinfo
->ops
.prepare(hinfo
, codec
, stream
, format
,
4492 purify_inactive_streams(codec
);
4493 mutex_unlock(&codec
->bus
->prepare_mutex
);
4496 EXPORT_SYMBOL_GPL(snd_hda_codec_prepare
);
4499 * snd_hda_codec_cleanup - Prepare a stream
4500 * @codec: the HDA codec
4501 * @hinfo: PCM information
4502 * @substream: PCM substream
4504 * Calls the cleanup callback set by the codec with the given arguments.
4506 void snd_hda_codec_cleanup(struct hda_codec
*codec
,
4507 struct hda_pcm_stream
*hinfo
,
4508 struct snd_pcm_substream
*substream
)
4510 mutex_lock(&codec
->bus
->prepare_mutex
);
4511 if (hinfo
->ops
.cleanup
)
4512 hinfo
->ops
.cleanup(hinfo
, codec
, substream
);
4513 mutex_unlock(&codec
->bus
->prepare_mutex
);
4515 EXPORT_SYMBOL_GPL(snd_hda_codec_cleanup
);
4518 const char *snd_hda_pcm_type_name
[HDA_PCM_NTYPES
] = {
4519 "Audio", "SPDIF", "HDMI", "Modem"
4523 * get the empty PCM device number to assign
4525 static int get_empty_pcm_device(struct hda_bus
*bus
, unsigned int type
)
4527 /* audio device indices; not linear to keep compatibility */
4528 /* assigned to static slots up to dev#10; if more needed, assign
4529 * the later slot dynamically (when CONFIG_SND_DYNAMIC_MINORS=y)
4531 static int audio_idx
[HDA_PCM_NTYPES
][5] = {
4532 [HDA_PCM_TYPE_AUDIO
] = { 0, 2, 4, 5, -1 },
4533 [HDA_PCM_TYPE_SPDIF
] = { 1, -1 },
4534 [HDA_PCM_TYPE_HDMI
] = { 3, 7, 8, 9, -1 },
4535 [HDA_PCM_TYPE_MODEM
] = { 6, -1 },
4539 if (type
>= HDA_PCM_NTYPES
) {
4540 dev_err(bus
->card
->dev
, "Invalid PCM type %d\n", type
);
4544 for (i
= 0; audio_idx
[type
][i
] >= 0; i
++) {
4545 #ifndef CONFIG_SND_DYNAMIC_MINORS
4546 if (audio_idx
[type
][i
] >= 8)
4549 if (!test_and_set_bit(audio_idx
[type
][i
], bus
->pcm_dev_bits
))
4550 return audio_idx
[type
][i
];
4553 #ifdef CONFIG_SND_DYNAMIC_MINORS
4554 /* non-fixed slots starting from 10 */
4555 for (i
= 10; i
< 32; i
++) {
4556 if (!test_and_set_bit(i
, bus
->pcm_dev_bits
))
4561 dev_warn(bus
->card
->dev
, "Too many %s devices\n",
4562 snd_hda_pcm_type_name
[type
]);
4563 #ifndef CONFIG_SND_DYNAMIC_MINORS
4564 dev_warn(bus
->card
->dev
,
4565 "Consider building the kernel with CONFIG_SND_DYNAMIC_MINORS=y\n");
4570 /* call build_pcms ops of the given codec and set up the default parameters */
4571 int snd_hda_codec_parse_pcms(struct hda_codec
*codec
)
4573 struct hda_pcm
*cpcm
;
4576 if (!list_empty(&codec
->pcm_list_head
))
4577 return 0; /* already parsed */
4579 if (!codec
->patch_ops
.build_pcms
)
4582 err
= codec
->patch_ops
.build_pcms(codec
);
4584 codec_err(codec
, "cannot build PCMs for #%d (error %d)\n",
4589 list_for_each_entry(cpcm
, &codec
->pcm_list_head
, list
) {
4592 for (stream
= 0; stream
< 2; stream
++) {
4593 struct hda_pcm_stream
*info
= &cpcm
->stream
[stream
];
4595 if (!info
->substreams
)
4597 err
= set_pcm_default_values(codec
, info
);
4600 "fail to setup default for PCM %s\n",
4610 /* assign all PCMs of the given codec */
4611 int snd_hda_codec_build_pcms(struct hda_codec
*codec
)
4613 struct hda_bus
*bus
= codec
->bus
;
4614 struct hda_pcm
*cpcm
;
4617 if (snd_BUG_ON(!bus
->ops
.attach_pcm
))
4620 err
= snd_hda_codec_parse_pcms(codec
);
4622 snd_hda_codec_reset(codec
);
4626 /* attach a new PCM streams */
4627 list_for_each_entry(cpcm
, &codec
->pcm_list_head
, list
) {
4629 continue; /* already attached */
4630 if (!cpcm
->stream
[0].substreams
&& !cpcm
->stream
[1].substreams
)
4631 continue; /* no substreams assigned */
4633 dev
= get_empty_pcm_device(bus
, cpcm
->pcm_type
);
4635 continue; /* no fatal error */
4637 err
= bus
->ops
.attach_pcm(bus
, codec
, cpcm
);
4640 "cannot attach PCM stream %d for codec #%d\n",
4642 continue; /* no fatal error */
4650 * snd_hda_add_new_ctls - create controls from the array
4651 * @codec: the HDA codec
4652 * @knew: the array of struct snd_kcontrol_new
4654 * This helper function creates and add new controls in the given array.
4655 * The array must be terminated with an empty entry as terminator.
4657 * Returns 0 if successful, or a negative error code.
4659 int snd_hda_add_new_ctls(struct hda_codec
*codec
,
4660 const struct snd_kcontrol_new
*knew
)
4664 for (; knew
->name
; knew
++) {
4665 struct snd_kcontrol
*kctl
;
4666 int addr
= 0, idx
= 0;
4667 if (knew
->iface
== -1) /* skip this codec private value */
4670 kctl
= snd_ctl_new1(knew
, codec
);
4674 kctl
->id
.device
= addr
;
4676 kctl
->id
.index
= idx
;
4677 err
= snd_hda_ctl_add(codec
, 0, kctl
);
4680 /* try first with another device index corresponding to
4681 * the codec addr; if it still fails (or it's the
4682 * primary codec), then try another control index
4684 if (!addr
&& codec
->addr
)
4686 else if (!idx
&& !knew
->index
) {
4687 idx
= find_empty_mixer_ctl_idx(codec
,
4697 EXPORT_SYMBOL_GPL(snd_hda_add_new_ctls
);
4701 * snd_hda_power_up - Power-up the codec
4702 * @codec: HD-audio codec
4704 * Increment the usage counter and resume the device if not done yet.
4706 void snd_hda_power_up(struct hda_codec
*codec
)
4708 struct device
*dev
= hda_codec_dev(codec
);
4710 if (codec_in_pm(codec
))
4712 pm_runtime_get_sync(dev
);
4714 EXPORT_SYMBOL_GPL(snd_hda_power_up
);
4717 * snd_hda_power_down - Power-down the codec
4718 * @codec: HD-audio codec
4720 * Decrement the usage counter and schedules the autosuspend if none used.
4722 void snd_hda_power_down(struct hda_codec
*codec
)
4724 struct device
*dev
= hda_codec_dev(codec
);
4726 if (codec_in_pm(codec
))
4728 pm_runtime_mark_last_busy(dev
);
4729 pm_runtime_put_autosuspend(dev
);
4731 EXPORT_SYMBOL_GPL(snd_hda_power_down
);
4733 static void codec_set_power_save(struct hda_codec
*codec
, int delay
)
4735 struct device
*dev
= hda_codec_dev(codec
);
4738 pm_runtime_set_autosuspend_delay(dev
, delay
);
4739 pm_runtime_use_autosuspend(dev
);
4740 pm_runtime_allow(dev
);
4741 if (!pm_runtime_suspended(dev
))
4742 pm_runtime_mark_last_busy(dev
);
4744 pm_runtime_dont_use_autosuspend(dev
);
4745 pm_runtime_forbid(dev
);
4750 * snd_hda_set_power_save - reprogram autosuspend for the given delay
4751 * @bus: HD-audio bus
4752 * @delay: autosuspend delay in msec, 0 = off
4754 * Synchronize the runtime PM autosuspend state from the power_save option.
4756 void snd_hda_set_power_save(struct hda_bus
*bus
, int delay
)
4758 struct hda_codec
*c
;
4760 list_for_each_entry(c
, &bus
->codec_list
, list
)
4761 codec_set_power_save(c
, delay
);
4763 EXPORT_SYMBOL_GPL(snd_hda_set_power_save
);
4766 * snd_hda_check_amp_list_power - Check the amp list and update the power
4767 * @codec: HD-audio codec
4768 * @check: the object containing an AMP list and the status
4769 * @nid: NID to check / update
4771 * Check whether the given NID is in the amp list. If it's in the list,
4772 * check the current AMP status, and update the the power-status according
4773 * to the mute status.
4775 * This function is supposed to be set or called from the check_power_status
4778 int snd_hda_check_amp_list_power(struct hda_codec
*codec
,
4779 struct hda_loopback_check
*check
,
4782 const struct hda_amp_list
*p
;
4785 if (!check
->amplist
)
4787 for (p
= check
->amplist
; p
->nid
; p
++) {
4792 return 0; /* nothing changed */
4794 for (p
= check
->amplist
; p
->nid
; p
++) {
4795 for (ch
= 0; ch
< 2; ch
++) {
4796 v
= snd_hda_codec_amp_read(codec
, p
->nid
, ch
, p
->dir
,
4798 if (!(v
& HDA_AMP_MUTE
) && v
> 0) {
4799 if (!check
->power_on
) {
4800 check
->power_on
= 1;
4801 snd_hda_power_up(codec
);
4807 if (check
->power_on
) {
4808 check
->power_on
= 0;
4809 snd_hda_power_down(codec
);
4813 EXPORT_SYMBOL_GPL(snd_hda_check_amp_list_power
);
4821 * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum
4822 * @imux: imux helper object
4823 * @uinfo: pointer to get/store the data
4825 int snd_hda_input_mux_info(const struct hda_input_mux
*imux
,
4826 struct snd_ctl_elem_info
*uinfo
)
4830 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_ENUMERATED
;
4832 uinfo
->value
.enumerated
.items
= imux
->num_items
;
4833 if (!imux
->num_items
)
4835 index
= uinfo
->value
.enumerated
.item
;
4836 if (index
>= imux
->num_items
)
4837 index
= imux
->num_items
- 1;
4838 strcpy(uinfo
->value
.enumerated
.name
, imux
->items
[index
].label
);
4841 EXPORT_SYMBOL_GPL(snd_hda_input_mux_info
);
4844 * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum
4845 * @codec: the HDA codec
4846 * @imux: imux helper object
4847 * @ucontrol: pointer to get/store the data
4848 * @nid: input mux NID
4849 * @cur_val: pointer to get/store the current imux value
4851 int snd_hda_input_mux_put(struct hda_codec
*codec
,
4852 const struct hda_input_mux
*imux
,
4853 struct snd_ctl_elem_value
*ucontrol
,
4855 unsigned int *cur_val
)
4859 if (!imux
->num_items
)
4861 idx
= ucontrol
->value
.enumerated
.item
[0];
4862 if (idx
>= imux
->num_items
)
4863 idx
= imux
->num_items
- 1;
4864 if (*cur_val
== idx
)
4866 snd_hda_codec_write_cache(codec
, nid
, 0, AC_VERB_SET_CONNECT_SEL
,
4867 imux
->items
[idx
].index
);
4871 EXPORT_SYMBOL_GPL(snd_hda_input_mux_put
);
4875 * snd_hda_enum_helper_info - Helper for simple enum ctls
4876 * @kcontrol: ctl element
4877 * @uinfo: pointer to get/store the data
4878 * @num_items: number of enum items
4879 * @texts: enum item string array
4881 * process kcontrol info callback of a simple string enum array
4882 * when @num_items is 0 or @texts is NULL, assume a boolean enum array
4884 int snd_hda_enum_helper_info(struct snd_kcontrol
*kcontrol
,
4885 struct snd_ctl_elem_info
*uinfo
,
4886 int num_items
, const char * const *texts
)
4888 static const char * const texts_default
[] = {
4889 "Disabled", "Enabled"
4892 if (!texts
|| !num_items
) {
4894 texts
= texts_default
;
4897 return snd_ctl_enum_info(uinfo
, 1, num_items
, texts
);
4899 EXPORT_SYMBOL_GPL(snd_hda_enum_helper_info
);
4902 * Multi-channel / digital-out PCM helper functions
4905 /* setup SPDIF output stream */
4906 static void setup_dig_out_stream(struct hda_codec
*codec
, hda_nid_t nid
,
4907 unsigned int stream_tag
, unsigned int format
)
4909 struct hda_spdif_out
*spdif
;
4910 unsigned int curr_fmt
;
4913 spdif
= snd_hda_spdif_out_of_nid(codec
, nid
);
4914 curr_fmt
= snd_hda_codec_read(codec
, nid
, 0,
4915 AC_VERB_GET_STREAM_FORMAT
, 0);
4916 reset
= codec
->spdif_status_reset
&&
4917 (spdif
->ctls
& AC_DIG1_ENABLE
) &&
4920 /* turn off SPDIF if needed; otherwise the IEC958 bits won't be
4923 set_dig_out_convert(codec
, nid
,
4924 spdif
->ctls
& ~AC_DIG1_ENABLE
& 0xff,
4926 snd_hda_codec_setup_stream(codec
, nid
, stream_tag
, 0, format
);
4927 if (codec
->slave_dig_outs
) {
4929 for (d
= codec
->slave_dig_outs
; *d
; d
++)
4930 snd_hda_codec_setup_stream(codec
, *d
, stream_tag
, 0,
4933 /* turn on again (if needed) */
4935 set_dig_out_convert(codec
, nid
,
4936 spdif
->ctls
& 0xff, -1);
4939 static void cleanup_dig_out_stream(struct hda_codec
*codec
, hda_nid_t nid
)
4941 snd_hda_codec_cleanup_stream(codec
, nid
);
4942 if (codec
->slave_dig_outs
) {
4944 for (d
= codec
->slave_dig_outs
; *d
; d
++)
4945 snd_hda_codec_cleanup_stream(codec
, *d
);
4950 * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode
4951 * @codec: the HDA codec
4952 * @mout: hda_multi_out object
4954 int snd_hda_multi_out_dig_open(struct hda_codec
*codec
,
4955 struct hda_multi_out
*mout
)
4957 mutex_lock(&codec
->spdif_mutex
);
4958 if (mout
->dig_out_used
== HDA_DIG_ANALOG_DUP
)
4959 /* already opened as analog dup; reset it once */
4960 cleanup_dig_out_stream(codec
, mout
->dig_out_nid
);
4961 mout
->dig_out_used
= HDA_DIG_EXCLUSIVE
;
4962 mutex_unlock(&codec
->spdif_mutex
);
4965 EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_open
);
4968 * snd_hda_multi_out_dig_prepare - prepare the digital out stream
4969 * @codec: the HDA codec
4970 * @mout: hda_multi_out object
4971 * @stream_tag: stream tag to assign
4972 * @format: format id to assign
4973 * @substream: PCM substream to assign
4975 int snd_hda_multi_out_dig_prepare(struct hda_codec
*codec
,
4976 struct hda_multi_out
*mout
,
4977 unsigned int stream_tag
,
4978 unsigned int format
,
4979 struct snd_pcm_substream
*substream
)
4981 mutex_lock(&codec
->spdif_mutex
);
4982 setup_dig_out_stream(codec
, mout
->dig_out_nid
, stream_tag
, format
);
4983 mutex_unlock(&codec
->spdif_mutex
);
4986 EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_prepare
);
4989 * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream
4990 * @codec: the HDA codec
4991 * @mout: hda_multi_out object
4993 int snd_hda_multi_out_dig_cleanup(struct hda_codec
*codec
,
4994 struct hda_multi_out
*mout
)
4996 mutex_lock(&codec
->spdif_mutex
);
4997 cleanup_dig_out_stream(codec
, mout
->dig_out_nid
);
4998 mutex_unlock(&codec
->spdif_mutex
);
5001 EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_cleanup
);
5004 * snd_hda_multi_out_dig_close - release the digital out stream
5005 * @codec: the HDA codec
5006 * @mout: hda_multi_out object
5008 int snd_hda_multi_out_dig_close(struct hda_codec
*codec
,
5009 struct hda_multi_out
*mout
)
5011 mutex_lock(&codec
->spdif_mutex
);
5012 mout
->dig_out_used
= 0;
5013 mutex_unlock(&codec
->spdif_mutex
);
5016 EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_close
);
5019 * snd_hda_multi_out_analog_open - open analog outputs
5020 * @codec: the HDA codec
5021 * @mout: hda_multi_out object
5022 * @substream: PCM substream to assign
5023 * @hinfo: PCM information to assign
5025 * Open analog outputs and set up the hw-constraints.
5026 * If the digital outputs can be opened as slave, open the digital
5029 int snd_hda_multi_out_analog_open(struct hda_codec
*codec
,
5030 struct hda_multi_out
*mout
,
5031 struct snd_pcm_substream
*substream
,
5032 struct hda_pcm_stream
*hinfo
)
5034 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
5035 runtime
->hw
.channels_max
= mout
->max_channels
;
5036 if (mout
->dig_out_nid
) {
5037 if (!mout
->analog_rates
) {
5038 mout
->analog_rates
= hinfo
->rates
;
5039 mout
->analog_formats
= hinfo
->formats
;
5040 mout
->analog_maxbps
= hinfo
->maxbps
;
5042 runtime
->hw
.rates
= mout
->analog_rates
;
5043 runtime
->hw
.formats
= mout
->analog_formats
;
5044 hinfo
->maxbps
= mout
->analog_maxbps
;
5046 if (!mout
->spdif_rates
) {
5047 snd_hda_query_supported_pcm(codec
, mout
->dig_out_nid
,
5049 &mout
->spdif_formats
,
5050 &mout
->spdif_maxbps
);
5052 mutex_lock(&codec
->spdif_mutex
);
5053 if (mout
->share_spdif
) {
5054 if ((runtime
->hw
.rates
& mout
->spdif_rates
) &&
5055 (runtime
->hw
.formats
& mout
->spdif_formats
)) {
5056 runtime
->hw
.rates
&= mout
->spdif_rates
;
5057 runtime
->hw
.formats
&= mout
->spdif_formats
;
5058 if (mout
->spdif_maxbps
< hinfo
->maxbps
)
5059 hinfo
->maxbps
= mout
->spdif_maxbps
;
5061 mout
->share_spdif
= 0;
5062 /* FIXME: need notify? */
5065 mutex_unlock(&codec
->spdif_mutex
);
5067 return snd_pcm_hw_constraint_step(substream
->runtime
, 0,
5068 SNDRV_PCM_HW_PARAM_CHANNELS
, 2);
5070 EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_open
);
5073 * snd_hda_multi_out_analog_prepare - Preapre the analog outputs.
5074 * @codec: the HDA codec
5075 * @mout: hda_multi_out object
5076 * @stream_tag: stream tag to assign
5077 * @format: format id to assign
5078 * @substream: PCM substream to assign
5080 * Set up the i/o for analog out.
5081 * When the digital out is available, copy the front out to digital out, too.
5083 int snd_hda_multi_out_analog_prepare(struct hda_codec
*codec
,
5084 struct hda_multi_out
*mout
,
5085 unsigned int stream_tag
,
5086 unsigned int format
,
5087 struct snd_pcm_substream
*substream
)
5089 const hda_nid_t
*nids
= mout
->dac_nids
;
5090 int chs
= substream
->runtime
->channels
;
5091 struct hda_spdif_out
*spdif
;
5094 mutex_lock(&codec
->spdif_mutex
);
5095 spdif
= snd_hda_spdif_out_of_nid(codec
, mout
->dig_out_nid
);
5096 if (mout
->dig_out_nid
&& mout
->share_spdif
&&
5097 mout
->dig_out_used
!= HDA_DIG_EXCLUSIVE
) {
5099 snd_hda_is_supported_format(codec
, mout
->dig_out_nid
,
5101 !(spdif
->status
& IEC958_AES0_NONAUDIO
)) {
5102 mout
->dig_out_used
= HDA_DIG_ANALOG_DUP
;
5103 setup_dig_out_stream(codec
, mout
->dig_out_nid
,
5104 stream_tag
, format
);
5106 mout
->dig_out_used
= 0;
5107 cleanup_dig_out_stream(codec
, mout
->dig_out_nid
);
5110 mutex_unlock(&codec
->spdif_mutex
);
5113 snd_hda_codec_setup_stream(codec
, nids
[HDA_FRONT
], stream_tag
,
5115 if (!mout
->no_share_stream
&&
5116 mout
->hp_nid
&& mout
->hp_nid
!= nids
[HDA_FRONT
])
5117 /* headphone out will just decode front left/right (stereo) */
5118 snd_hda_codec_setup_stream(codec
, mout
->hp_nid
, stream_tag
,
5120 /* extra outputs copied from front */
5121 for (i
= 0; i
< ARRAY_SIZE(mout
->hp_out_nid
); i
++)
5122 if (!mout
->no_share_stream
&& mout
->hp_out_nid
[i
])
5123 snd_hda_codec_setup_stream(codec
,
5124 mout
->hp_out_nid
[i
],
5125 stream_tag
, 0, format
);
5128 for (i
= 1; i
< mout
->num_dacs
; i
++) {
5129 if (chs
>= (i
+ 1) * 2) /* independent out */
5130 snd_hda_codec_setup_stream(codec
, nids
[i
], stream_tag
,
5132 else if (!mout
->no_share_stream
) /* copy front */
5133 snd_hda_codec_setup_stream(codec
, nids
[i
], stream_tag
,
5137 /* extra surrounds */
5138 for (i
= 0; i
< ARRAY_SIZE(mout
->extra_out_nid
); i
++) {
5140 if (!mout
->extra_out_nid
[i
])
5142 if (chs
>= (i
+ 1) * 2)
5144 else if (!mout
->no_share_stream
)
5146 snd_hda_codec_setup_stream(codec
, mout
->extra_out_nid
[i
],
5147 stream_tag
, ch
, format
);
5152 EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_prepare
);
5155 * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out
5156 * @codec: the HDA codec
5157 * @mout: hda_multi_out object
5159 int snd_hda_multi_out_analog_cleanup(struct hda_codec
*codec
,
5160 struct hda_multi_out
*mout
)
5162 const hda_nid_t
*nids
= mout
->dac_nids
;
5165 for (i
= 0; i
< mout
->num_dacs
; i
++)
5166 snd_hda_codec_cleanup_stream(codec
, nids
[i
]);
5168 snd_hda_codec_cleanup_stream(codec
, mout
->hp_nid
);
5169 for (i
= 0; i
< ARRAY_SIZE(mout
->hp_out_nid
); i
++)
5170 if (mout
->hp_out_nid
[i
])
5171 snd_hda_codec_cleanup_stream(codec
,
5172 mout
->hp_out_nid
[i
]);
5173 for (i
= 0; i
< ARRAY_SIZE(mout
->extra_out_nid
); i
++)
5174 if (mout
->extra_out_nid
[i
])
5175 snd_hda_codec_cleanup_stream(codec
,
5176 mout
->extra_out_nid
[i
]);
5177 mutex_lock(&codec
->spdif_mutex
);
5178 if (mout
->dig_out_nid
&& mout
->dig_out_used
== HDA_DIG_ANALOG_DUP
) {
5179 cleanup_dig_out_stream(codec
, mout
->dig_out_nid
);
5180 mout
->dig_out_used
= 0;
5182 mutex_unlock(&codec
->spdif_mutex
);
5185 EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_cleanup
);
5188 * snd_hda_get_default_vref - Get the default (mic) VREF pin bits
5189 * @codec: the HDA codec
5190 * @pin: referred pin NID
5192 * Guess the suitable VREF pin bits to be set as the pin-control value.
5193 * Note: the function doesn't set the AC_PINCTL_IN_EN bit.
5195 unsigned int snd_hda_get_default_vref(struct hda_codec
*codec
, hda_nid_t pin
)
5197 unsigned int pincap
;
5198 unsigned int oldval
;
5199 oldval
= snd_hda_codec_read(codec
, pin
, 0,
5200 AC_VERB_GET_PIN_WIDGET_CONTROL
, 0);
5201 pincap
= snd_hda_query_pin_caps(codec
, pin
);
5202 pincap
= (pincap
& AC_PINCAP_VREF
) >> AC_PINCAP_VREF_SHIFT
;
5203 /* Exception: if the default pin setup is vref50, we give it priority */
5204 if ((pincap
& AC_PINCAP_VREF_80
) && oldval
!= PIN_VREF50
)
5205 return AC_PINCTL_VREF_80
;
5206 else if (pincap
& AC_PINCAP_VREF_50
)
5207 return AC_PINCTL_VREF_50
;
5208 else if (pincap
& AC_PINCAP_VREF_100
)
5209 return AC_PINCTL_VREF_100
;
5210 else if (pincap
& AC_PINCAP_VREF_GRD
)
5211 return AC_PINCTL_VREF_GRD
;
5212 return AC_PINCTL_VREF_HIZ
;
5214 EXPORT_SYMBOL_GPL(snd_hda_get_default_vref
);
5217 * snd_hda_correct_pin_ctl - correct the pin ctl value for matching with the pin cap
5218 * @codec: the HDA codec
5219 * @pin: referred pin NID
5220 * @val: pin ctl value to audit
5222 unsigned int snd_hda_correct_pin_ctl(struct hda_codec
*codec
,
5223 hda_nid_t pin
, unsigned int val
)
5225 static unsigned int cap_lists
[][2] = {
5226 { AC_PINCTL_VREF_100
, AC_PINCAP_VREF_100
},
5227 { AC_PINCTL_VREF_80
, AC_PINCAP_VREF_80
},
5228 { AC_PINCTL_VREF_50
, AC_PINCAP_VREF_50
},
5229 { AC_PINCTL_VREF_GRD
, AC_PINCAP_VREF_GRD
},
5235 cap
= snd_hda_query_pin_caps(codec
, pin
);
5237 return val
; /* don't know what to do... */
5239 if (val
& AC_PINCTL_OUT_EN
) {
5240 if (!(cap
& AC_PINCAP_OUT
))
5241 val
&= ~(AC_PINCTL_OUT_EN
| AC_PINCTL_HP_EN
);
5242 else if ((val
& AC_PINCTL_HP_EN
) && !(cap
& AC_PINCAP_HP_DRV
))
5243 val
&= ~AC_PINCTL_HP_EN
;
5246 if (val
& AC_PINCTL_IN_EN
) {
5247 if (!(cap
& AC_PINCAP_IN
))
5248 val
&= ~(AC_PINCTL_IN_EN
| AC_PINCTL_VREFEN
);
5250 unsigned int vcap
, vref
;
5252 vcap
= (cap
& AC_PINCAP_VREF
) >> AC_PINCAP_VREF_SHIFT
;
5253 vref
= val
& AC_PINCTL_VREFEN
;
5254 for (i
= 0; i
< ARRAY_SIZE(cap_lists
); i
++) {
5255 if (vref
== cap_lists
[i
][0] &&
5256 !(vcap
& cap_lists
[i
][1])) {
5257 if (i
== ARRAY_SIZE(cap_lists
) - 1)
5258 vref
= AC_PINCTL_VREF_HIZ
;
5260 vref
= cap_lists
[i
+ 1][0];
5263 val
&= ~AC_PINCTL_VREFEN
;
5270 EXPORT_SYMBOL_GPL(snd_hda_correct_pin_ctl
);
5273 * _snd_hda_pin_ctl - Helper to set pin ctl value
5274 * @codec: the HDA codec
5275 * @pin: referred pin NID
5276 * @val: pin control value to set
5277 * @cached: access over codec pinctl cache or direct write
5279 * This function is a helper to set a pin ctl value more safely.
5280 * It corrects the pin ctl value via snd_hda_correct_pin_ctl(), stores the
5281 * value in pin target array via snd_hda_codec_set_pin_target(), then
5282 * actually writes the value via either snd_hda_codec_update_cache() or
5283 * snd_hda_codec_write() depending on @cached flag.
5285 int _snd_hda_set_pin_ctl(struct hda_codec
*codec
, hda_nid_t pin
,
5286 unsigned int val
, bool cached
)
5288 val
= snd_hda_correct_pin_ctl(codec
, pin
, val
);
5289 snd_hda_codec_set_pin_target(codec
, pin
, val
);
5291 return snd_hda_codec_update_cache(codec
, pin
, 0,
5292 AC_VERB_SET_PIN_WIDGET_CONTROL
, val
);
5294 return snd_hda_codec_write(codec
, pin
, 0,
5295 AC_VERB_SET_PIN_WIDGET_CONTROL
, val
);
5297 EXPORT_SYMBOL_GPL(_snd_hda_set_pin_ctl
);
5300 * snd_hda_add_imux_item - Add an item to input_mux
5301 * @codec: the HDA codec
5302 * @imux: imux helper object
5303 * @label: the name of imux item to assign
5304 * @index: index number of imux item to assign
5305 * @type_idx: pointer to store the resultant label index
5307 * When the same label is used already in the existing items, the number
5308 * suffix is appended to the label. This label index number is stored
5309 * to type_idx when non-NULL pointer is given.
5311 int snd_hda_add_imux_item(struct hda_codec
*codec
,
5312 struct hda_input_mux
*imux
, const char *label
,
5313 int index
, int *type_idx
)
5315 int i
, label_idx
= 0;
5316 if (imux
->num_items
>= HDA_MAX_NUM_INPUTS
) {
5317 codec_err(codec
, "hda_codec: Too many imux items!\n");
5320 for (i
= 0; i
< imux
->num_items
; i
++) {
5321 if (!strncmp(label
, imux
->items
[i
].label
, strlen(label
)))
5325 *type_idx
= label_idx
;
5327 snprintf(imux
->items
[imux
->num_items
].label
,
5328 sizeof(imux
->items
[imux
->num_items
].label
),
5329 "%s %d", label
, label_idx
);
5331 strlcpy(imux
->items
[imux
->num_items
].label
, label
,
5332 sizeof(imux
->items
[imux
->num_items
].label
));
5333 imux
->items
[imux
->num_items
].index
= index
;
5337 EXPORT_SYMBOL_GPL(snd_hda_add_imux_item
);
5340 * snd_hda_bus_reset - Reset the bus
5341 * @bus: HD-audio bus
5343 void snd_hda_bus_reset(struct hda_bus
*bus
)
5345 struct hda_codec
*codec
;
5347 list_for_each_entry(codec
, &bus
->codec_list
, list
) {
5348 /* FIXME: maybe a better way needed for forced reset */
5349 cancel_delayed_work_sync(&codec
->jackpoll_work
);
5351 if (hda_codec_is_power_on(codec
)) {
5352 hda_call_codec_suspend(codec
);
5353 hda_call_codec_resume(codec
);
5358 EXPORT_SYMBOL_GPL(snd_hda_bus_reset
);
5365 * snd_array_new - get a new element from the given array
5366 * @array: the array object
5368 * Get a new element from the given array. If it exceeds the
5369 * pre-allocated array size, re-allocate the array.
5371 * Returns NULL if allocation failed.
5373 void *snd_array_new(struct snd_array
*array
)
5375 if (snd_BUG_ON(!array
->elem_size
))
5377 if (array
->used
>= array
->alloced
) {
5378 int num
= array
->alloced
+ array
->alloc_align
;
5379 int size
= (num
+ 1) * array
->elem_size
;
5381 if (snd_BUG_ON(num
>= 4096))
5383 nlist
= krealloc(array
->list
, size
, GFP_KERNEL
| __GFP_ZERO
);
5386 array
->list
= nlist
;
5387 array
->alloced
= num
;
5389 return snd_array_elem(array
, array
->used
++);
5391 EXPORT_SYMBOL_GPL(snd_array_new
);
5394 * snd_array_free - free the given array elements
5395 * @array: the array object
5397 void snd_array_free(struct snd_array
*array
)
5404 EXPORT_SYMBOL_GPL(snd_array_free
);
5407 * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer
5408 * @pcm: PCM caps bits
5409 * @buf: the string buffer to write
5410 * @buflen: the max buffer length
5412 * used by hda_proc.c and hda_eld.c
5414 void snd_print_pcm_bits(int pcm
, char *buf
, int buflen
)
5416 static unsigned int bits
[] = { 8, 16, 20, 24, 32 };
5419 for (i
= 0, j
= 0; i
< ARRAY_SIZE(bits
); i
++)
5420 if (pcm
& (AC_SUPPCM_BITS_8
<< i
))
5421 j
+= snprintf(buf
+ j
, buflen
- j
, " %d", bits
[i
]);
5423 buf
[j
] = '\0'; /* necessary when j == 0 */
5425 EXPORT_SYMBOL_GPL(snd_print_pcm_bits
);
5427 MODULE_DESCRIPTION("HDA codec core");
5428 MODULE_LICENSE("GPL");