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
;
684 if (!bus
|| !bus
->workq
)
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 queue_work(bus
->workq
, &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
));
736 flush_workqueue(bus
->workq
);
737 if (bus
->ops
.private_free
)
738 bus
->ops
.private_free(bus
);
740 destroy_workqueue(bus
->workq
);
745 static int snd_hda_bus_dev_free(struct snd_device
*device
)
747 snd_hda_bus_free(device
->device_data
);
751 static int snd_hda_bus_dev_disconnect(struct snd_device
*device
)
753 struct hda_bus
*bus
= device
->device_data
;
759 * snd_hda_bus_new - create a HDA bus
760 * @card: the card entry
761 * @busp: the pointer to store the created bus instance
763 * Returns 0 if successful, or a negative error code.
765 int snd_hda_bus_new(struct snd_card
*card
,
766 struct hda_bus
**busp
)
770 static struct snd_device_ops dev_ops
= {
771 .dev_disconnect
= snd_hda_bus_dev_disconnect
,
772 .dev_free
= snd_hda_bus_dev_free
,
778 bus
= kzalloc(sizeof(*bus
), GFP_KERNEL
);
783 mutex_init(&bus
->cmd_mutex
);
784 mutex_init(&bus
->prepare_mutex
);
785 INIT_LIST_HEAD(&bus
->codec_list
);
786 INIT_WORK(&bus
->unsol
.work
, process_unsol_events
);
788 snprintf(bus
->workq_name
, sizeof(bus
->workq_name
),
789 "hd-audio%d", card
->number
);
790 bus
->workq
= create_singlethread_workqueue(bus
->workq_name
);
792 dev_err(card
->dev
, "cannot create workqueue %s\n",
798 err
= snd_device_new(card
, SNDRV_DEV_BUS
, bus
, &dev_ops
);
800 snd_hda_bus_free(bus
);
807 EXPORT_SYMBOL_GPL(snd_hda_bus_new
);
810 * look for an AFG and MFG nodes
812 static void setup_fg_nodes(struct hda_codec
*codec
)
814 int i
, total_nodes
, function_id
;
817 total_nodes
= snd_hda_get_sub_nodes(codec
, AC_NODE_ROOT
, &nid
);
818 for (i
= 0; i
< total_nodes
; i
++, nid
++) {
819 function_id
= snd_hda_param_read(codec
, nid
,
820 AC_PAR_FUNCTION_TYPE
);
821 switch (function_id
& 0xff) {
822 case AC_GRP_AUDIO_FUNCTION
:
824 codec
->afg_function_id
= function_id
& 0xff;
825 codec
->afg_unsol
= (function_id
>> 8) & 1;
827 case AC_GRP_MODEM_FUNCTION
:
829 codec
->mfg_function_id
= function_id
& 0xff;
830 codec
->mfg_unsol
= (function_id
>> 8) & 1;
839 * read widget caps for each widget and store in cache
841 static int read_widget_caps(struct hda_codec
*codec
, hda_nid_t fg_node
)
846 codec
->num_nodes
= snd_hda_get_sub_nodes(codec
, fg_node
,
848 codec
->wcaps
= kmalloc(codec
->num_nodes
* 4, GFP_KERNEL
);
851 nid
= codec
->start_nid
;
852 for (i
= 0; i
< codec
->num_nodes
; i
++, nid
++)
853 codec
->wcaps
[i
] = snd_hda_param_read(codec
, nid
,
854 AC_PAR_AUDIO_WIDGET_CAP
);
858 /* read all pin default configurations and save codec->init_pins */
859 static int read_pin_defaults(struct hda_codec
*codec
)
862 hda_nid_t nid
= codec
->start_nid
;
864 for (i
= 0; i
< codec
->num_nodes
; i
++, nid
++) {
865 struct hda_pincfg
*pin
;
866 unsigned int wcaps
= get_wcaps(codec
, nid
);
867 unsigned int wid_type
= get_wcaps_type(wcaps
);
868 if (wid_type
!= AC_WID_PIN
)
870 pin
= snd_array_new(&codec
->init_pins
);
874 pin
->cfg
= snd_hda_codec_read(codec
, nid
, 0,
875 AC_VERB_GET_CONFIG_DEFAULT
, 0);
876 pin
->ctrl
= snd_hda_codec_read(codec
, nid
, 0,
877 AC_VERB_GET_PIN_WIDGET_CONTROL
,
883 /* look up the given pin config list and return the item matching with NID */
884 static struct hda_pincfg
*look_up_pincfg(struct hda_codec
*codec
,
885 struct snd_array
*array
,
889 for (i
= 0; i
< array
->used
; i
++) {
890 struct hda_pincfg
*pin
= snd_array_elem(array
, i
);
897 /* set the current pin config value for the given NID.
898 * the value is cached, and read via snd_hda_codec_get_pincfg()
900 int snd_hda_add_pincfg(struct hda_codec
*codec
, struct snd_array
*list
,
901 hda_nid_t nid
, unsigned int cfg
)
903 struct hda_pincfg
*pin
;
905 /* the check below may be invalid when pins are added by a fixup
906 * dynamically (e.g. via snd_hda_codec_update_widgets()), so disabled
910 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN)
914 pin
= look_up_pincfg(codec
, list
, nid
);
916 pin
= snd_array_new(list
);
926 * snd_hda_codec_set_pincfg - Override a pin default configuration
927 * @codec: the HDA codec
928 * @nid: NID to set the pin config
929 * @cfg: the pin default config value
931 * Override a pin default configuration value in the cache.
932 * This value can be read by snd_hda_codec_get_pincfg() in a higher
933 * priority than the real hardware value.
935 int snd_hda_codec_set_pincfg(struct hda_codec
*codec
,
936 hda_nid_t nid
, unsigned int cfg
)
938 return snd_hda_add_pincfg(codec
, &codec
->driver_pins
, nid
, cfg
);
940 EXPORT_SYMBOL_GPL(snd_hda_codec_set_pincfg
);
943 * snd_hda_codec_get_pincfg - Obtain a pin-default configuration
944 * @codec: the HDA codec
945 * @nid: NID to get the pin config
947 * Get the current pin config value of the given pin NID.
948 * If the pincfg value is cached or overridden via sysfs or driver,
949 * returns the cached value.
951 unsigned int snd_hda_codec_get_pincfg(struct hda_codec
*codec
, hda_nid_t nid
)
953 struct hda_pincfg
*pin
;
955 #ifdef CONFIG_SND_HDA_RECONFIG
957 unsigned int cfg
= 0;
958 mutex_lock(&codec
->user_mutex
);
959 pin
= look_up_pincfg(codec
, &codec
->user_pins
, nid
);
962 mutex_unlock(&codec
->user_mutex
);
967 pin
= look_up_pincfg(codec
, &codec
->driver_pins
, nid
);
970 pin
= look_up_pincfg(codec
, &codec
->init_pins
, nid
);
975 EXPORT_SYMBOL_GPL(snd_hda_codec_get_pincfg
);
978 * snd_hda_codec_set_pin_target - remember the current pinctl target value
979 * @codec: the HDA codec
981 * @val: assigned pinctl value
983 * This function stores the given value to a pinctl target value in the
984 * pincfg table. This isn't always as same as the actually written value
985 * but can be referred at any time via snd_hda_codec_get_pin_target().
987 int snd_hda_codec_set_pin_target(struct hda_codec
*codec
, hda_nid_t nid
,
990 struct hda_pincfg
*pin
;
992 pin
= look_up_pincfg(codec
, &codec
->init_pins
, nid
);
998 EXPORT_SYMBOL_GPL(snd_hda_codec_set_pin_target
);
1001 * snd_hda_codec_get_pin_target - return the current pinctl target value
1002 * @codec: the HDA codec
1005 int snd_hda_codec_get_pin_target(struct hda_codec
*codec
, hda_nid_t nid
)
1007 struct hda_pincfg
*pin
;
1009 pin
= look_up_pincfg(codec
, &codec
->init_pins
, nid
);
1014 EXPORT_SYMBOL_GPL(snd_hda_codec_get_pin_target
);
1017 * snd_hda_shutup_pins - Shut up all pins
1018 * @codec: the HDA codec
1020 * Clear all pin controls to shup up before suspend for avoiding click noise.
1021 * The controls aren't cached so that they can be resumed properly.
1023 void snd_hda_shutup_pins(struct hda_codec
*codec
)
1026 /* don't shut up pins when unloading the driver; otherwise it breaks
1027 * the default pin setup at the next load of the driver
1029 if (codec
->bus
->shutdown
)
1031 for (i
= 0; i
< codec
->init_pins
.used
; i
++) {
1032 struct hda_pincfg
*pin
= snd_array_elem(&codec
->init_pins
, i
);
1033 /* use read here for syncing after issuing each verb */
1034 snd_hda_codec_read(codec
, pin
->nid
, 0,
1035 AC_VERB_SET_PIN_WIDGET_CONTROL
, 0);
1037 codec
->pins_shutup
= 1;
1039 EXPORT_SYMBOL_GPL(snd_hda_shutup_pins
);
1042 /* Restore the pin controls cleared previously via snd_hda_shutup_pins() */
1043 static void restore_shutup_pins(struct hda_codec
*codec
)
1046 if (!codec
->pins_shutup
)
1048 if (codec
->bus
->shutdown
)
1050 for (i
= 0; i
< codec
->init_pins
.used
; i
++) {
1051 struct hda_pincfg
*pin
= snd_array_elem(&codec
->init_pins
, i
);
1052 snd_hda_codec_write(codec
, pin
->nid
, 0,
1053 AC_VERB_SET_PIN_WIDGET_CONTROL
,
1056 codec
->pins_shutup
= 0;
1060 static void hda_jackpoll_work(struct work_struct
*work
)
1062 struct hda_codec
*codec
=
1063 container_of(work
, struct hda_codec
, jackpoll_work
.work
);
1065 snd_hda_jack_set_dirty_all(codec
);
1066 snd_hda_jack_poll_all(codec
);
1068 if (!codec
->jackpoll_interval
)
1071 queue_delayed_work(codec
->bus
->workq
, &codec
->jackpoll_work
,
1072 codec
->jackpoll_interval
);
1075 static void init_hda_cache(struct hda_cache_rec
*cache
,
1076 unsigned int record_size
);
1077 static void free_hda_cache(struct hda_cache_rec
*cache
);
1079 /* release all pincfg lists */
1080 static void free_init_pincfgs(struct hda_codec
*codec
)
1082 snd_array_free(&codec
->driver_pins
);
1083 #ifdef CONFIG_SND_HDA_RECONFIG
1084 snd_array_free(&codec
->user_pins
);
1086 snd_array_free(&codec
->init_pins
);
1090 * audio-converter setup caches
1092 struct hda_cvt_setup
{
1097 unsigned char active
; /* cvt is currently used */
1098 unsigned char dirty
; /* setups should be cleared */
1101 /* get or create a cache entry for the given audio converter NID */
1102 static struct hda_cvt_setup
*
1103 get_hda_cvt_setup(struct hda_codec
*codec
, hda_nid_t nid
)
1105 struct hda_cvt_setup
*p
;
1108 for (i
= 0; i
< codec
->cvt_setups
.used
; i
++) {
1109 p
= snd_array_elem(&codec
->cvt_setups
, i
);
1113 p
= snd_array_new(&codec
->cvt_setups
);
1122 static void release_pcm(struct kref
*kref
)
1124 struct hda_pcm
*pcm
= container_of(kref
, struct hda_pcm
, kref
);
1127 snd_device_free(pcm
->codec
->card
, pcm
->pcm
);
1128 clear_bit(pcm
->device
, pcm
->codec
->bus
->pcm_dev_bits
);
1133 void snd_hda_codec_pcm_put(struct hda_pcm
*pcm
)
1135 kref_put(&pcm
->kref
, release_pcm
);
1137 EXPORT_SYMBOL_GPL(snd_hda_codec_pcm_put
);
1139 struct hda_pcm
*snd_hda_codec_pcm_new(struct hda_codec
*codec
,
1140 const char *fmt
, ...)
1142 struct hda_pcm
*pcm
;
1145 va_start(args
, fmt
);
1146 pcm
= kzalloc(sizeof(*pcm
), GFP_KERNEL
);
1151 kref_init(&pcm
->kref
);
1152 pcm
->name
= kvasprintf(GFP_KERNEL
, fmt
, args
);
1158 list_add_tail(&pcm
->list
, &codec
->pcm_list_head
);
1161 EXPORT_SYMBOL_GPL(snd_hda_codec_pcm_new
);
1163 static void codec_release_pcms(struct hda_codec
*codec
)
1165 struct hda_pcm
*pcm
, *n
;
1167 list_for_each_entry_safe(pcm
, n
, &codec
->pcm_list_head
, list
) {
1168 list_del_init(&pcm
->list
);
1169 snd_hda_codec_pcm_put(pcm
);
1176 static void snd_hda_codec_free(struct hda_codec
*codec
)
1180 cancel_delayed_work_sync(&codec
->jackpoll_work
);
1181 codec_release_pcms(codec
);
1182 if (device_is_registered(hda_codec_dev(codec
)))
1183 device_del(hda_codec_dev(codec
));
1184 snd_hda_jack_tbl_clear(codec
);
1185 free_init_pincfgs(codec
);
1186 flush_workqueue(codec
->bus
->workq
);
1187 list_del(&codec
->list
);
1188 snd_array_free(&codec
->mixers
);
1189 snd_array_free(&codec
->nids
);
1190 snd_array_free(&codec
->cvt_setups
);
1191 snd_array_free(&codec
->spdif_out
);
1192 remove_conn_list(codec
);
1193 codec
->bus
->caddr_tbl
[codec
->addr
] = NULL
;
1194 clear_bit(codec
->addr
, &codec
->bus
->codec_powered
);
1195 snd_hda_sysfs_clear(codec
);
1196 free_hda_cache(&codec
->amp_cache
);
1197 free_hda_cache(&codec
->cmd_cache
);
1198 kfree(codec
->vendor_name
);
1199 kfree(codec
->chip_name
);
1200 kfree(codec
->modelname
);
1201 kfree(codec
->wcaps
);
1202 codec
->bus
->num_codecs
--;
1203 put_device(hda_codec_dev(codec
));
1206 static bool snd_hda_codec_get_supported_ps(struct hda_codec
*codec
,
1207 hda_nid_t fg
, unsigned int power_state
);
1209 static unsigned int hda_set_power_state(struct hda_codec
*codec
,
1210 unsigned int power_state
);
1212 static int snd_hda_codec_dev_register(struct snd_device
*device
)
1214 struct hda_codec
*codec
= device
->device_data
;
1216 snd_hda_register_beep_device(codec
);
1217 if (device_is_registered(hda_codec_dev(codec
)))
1218 pm_runtime_enable(hda_codec_dev(codec
));
1219 /* it was powered up in snd_hda_codec_new(), now all done */
1220 snd_hda_power_down(codec
);
1224 static int snd_hda_codec_dev_disconnect(struct snd_device
*device
)
1226 struct hda_codec
*codec
= device
->device_data
;
1228 snd_hda_detach_beep_device(codec
);
1232 static int snd_hda_codec_dev_free(struct snd_device
*device
)
1234 snd_hda_codec_free(device
->device_data
);
1238 /* just free the container */
1239 static void snd_hda_codec_dev_release(struct device
*dev
)
1241 kfree(dev_to_hda_codec(dev
));
1245 * snd_hda_codec_new - create a HDA codec
1246 * @bus: the bus to assign
1247 * @codec_addr: the codec address
1248 * @codecp: the pointer to store the generated codec
1250 * Returns 0 if successful, or a negative error code.
1252 int snd_hda_codec_new(struct hda_bus
*bus
, struct snd_card
*card
,
1253 unsigned int codec_addr
, struct hda_codec
**codecp
)
1255 struct hda_codec
*codec
;
1260 static struct snd_device_ops dev_ops
= {
1261 .dev_register
= snd_hda_codec_dev_register
,
1262 .dev_disconnect
= snd_hda_codec_dev_disconnect
,
1263 .dev_free
= snd_hda_codec_dev_free
,
1266 if (snd_BUG_ON(!bus
))
1268 if (snd_BUG_ON(codec_addr
> HDA_MAX_CODEC_ADDRESS
))
1271 if (bus
->caddr_tbl
[codec_addr
]) {
1273 "address 0x%x is already occupied\n",
1278 codec
= kzalloc(sizeof(*codec
), GFP_KERNEL
);
1282 dev
= hda_codec_dev(codec
);
1283 device_initialize(dev
);
1284 dev
->parent
= card
->dev
;
1285 dev
->bus
= &snd_hda_bus_type
;
1286 dev
->release
= snd_hda_codec_dev_release
;
1287 dev
->groups
= snd_hda_dev_attr_groups
;
1288 dev_set_name(dev
, "hdaudioC%dD%d", card
->number
, codec_addr
);
1289 dev_set_drvdata(dev
, codec
); /* for sysfs */
1290 device_enable_async_suspend(dev
);
1294 codec
->addr
= codec_addr
;
1295 mutex_init(&codec
->spdif_mutex
);
1296 mutex_init(&codec
->control_mutex
);
1297 mutex_init(&codec
->hash_mutex
);
1298 init_hda_cache(&codec
->amp_cache
, sizeof(struct hda_amp_info
));
1299 init_hda_cache(&codec
->cmd_cache
, sizeof(struct hda_cache_head
));
1300 snd_array_init(&codec
->mixers
, sizeof(struct hda_nid_item
), 32);
1301 snd_array_init(&codec
->nids
, sizeof(struct hda_nid_item
), 32);
1302 snd_array_init(&codec
->init_pins
, sizeof(struct hda_pincfg
), 16);
1303 snd_array_init(&codec
->driver_pins
, sizeof(struct hda_pincfg
), 16);
1304 snd_array_init(&codec
->cvt_setups
, sizeof(struct hda_cvt_setup
), 8);
1305 snd_array_init(&codec
->spdif_out
, sizeof(struct hda_spdif_out
), 16);
1306 snd_array_init(&codec
->jacktbl
, sizeof(struct hda_jack_tbl
), 16);
1307 snd_array_init(&codec
->verbs
, sizeof(struct hda_verb
*), 8);
1308 INIT_LIST_HEAD(&codec
->conn_list
);
1309 INIT_LIST_HEAD(&codec
->pcm_list_head
);
1311 INIT_DELAYED_WORK(&codec
->jackpoll_work
, hda_jackpoll_work
);
1312 codec
->depop_delay
= -1;
1313 codec
->fixup_id
= HDA_FIXUP_ID_NOT_SET
;
1316 /* snd_hda_codec_new() marks the codec as power-up, and leave it as is.
1317 * it's powered down later in snd_hda_codec_dev_register().
1319 set_bit(codec
->addr
, &bus
->codec_powered
);
1320 pm_runtime_set_active(hda_codec_dev(codec
));
1321 pm_runtime_get_noresume(hda_codec_dev(codec
));
1322 codec
->power_jiffies
= jiffies
;
1325 snd_hda_sysfs_init(codec
);
1327 if (codec
->bus
->modelname
) {
1328 codec
->modelname
= kstrdup(codec
->bus
->modelname
, GFP_KERNEL
);
1329 if (!codec
->modelname
) {
1335 list_add_tail(&codec
->list
, &bus
->codec_list
);
1338 bus
->caddr_tbl
[codec_addr
] = codec
;
1340 codec
->vendor_id
= snd_hda_param_read(codec
, AC_NODE_ROOT
,
1342 if (codec
->vendor_id
== -1)
1343 /* read again, hopefully the access method was corrected
1344 * in the last read...
1346 codec
->vendor_id
= snd_hda_param_read(codec
, AC_NODE_ROOT
,
1348 codec
->subsystem_id
= snd_hda_param_read(codec
, AC_NODE_ROOT
,
1349 AC_PAR_SUBSYSTEM_ID
);
1350 codec
->revision_id
= snd_hda_param_read(codec
, AC_NODE_ROOT
,
1353 setup_fg_nodes(codec
);
1354 if (!codec
->afg
&& !codec
->mfg
) {
1355 dev_err(card
->dev
, "no AFG or MFG node found\n");
1360 fg
= codec
->afg
? codec
->afg
: codec
->mfg
;
1361 err
= read_widget_caps(codec
, fg
);
1364 err
= read_pin_defaults(codec
);
1368 if (!codec
->subsystem_id
) {
1369 codec
->subsystem_id
=
1370 snd_hda_codec_read(codec
, fg
, 0,
1371 AC_VERB_GET_SUBSYSTEM_ID
, 0);
1375 codec
->d3_stop_clk
= snd_hda_codec_get_supported_ps(codec
, fg
,
1378 codec
->epss
= snd_hda_codec_get_supported_ps(codec
, fg
,
1381 /* power-up all before initialization */
1382 hda_set_power_state(codec
, AC_PWRST_D0
);
1384 snd_hda_codec_proc_new(codec
);
1386 snd_hda_create_hwdep(codec
);
1388 sprintf(component
, "HDA:%08x,%08x,%08x", codec
->vendor_id
,
1389 codec
->subsystem_id
, codec
->revision_id
);
1390 snd_component_add(card
, component
);
1392 err
= snd_device_new(card
, SNDRV_DEV_CODEC
, codec
, &dev_ops
);
1401 snd_hda_codec_free(codec
);
1404 EXPORT_SYMBOL_GPL(snd_hda_codec_new
);
1407 * snd_hda_codec_update_widgets - Refresh widget caps and pin defaults
1408 * @codec: the HDA codec
1410 * Forcibly refresh the all widget caps and the init pin configurations of
1413 int snd_hda_codec_update_widgets(struct hda_codec
*codec
)
1418 /* Assume the function group node does not change,
1419 * only the widget nodes may change.
1421 kfree(codec
->wcaps
);
1422 fg
= codec
->afg
? codec
->afg
: codec
->mfg
;
1423 err
= read_widget_caps(codec
, fg
);
1427 snd_array_free(&codec
->init_pins
);
1428 err
= read_pin_defaults(codec
);
1432 EXPORT_SYMBOL_GPL(snd_hda_codec_update_widgets
);
1434 /* update the stream-id if changed */
1435 static void update_pcm_stream_id(struct hda_codec
*codec
,
1436 struct hda_cvt_setup
*p
, hda_nid_t nid
,
1437 u32 stream_tag
, int channel_id
)
1439 unsigned int oldval
, newval
;
1441 if (p
->stream_tag
!= stream_tag
|| p
->channel_id
!= channel_id
) {
1442 oldval
= snd_hda_codec_read(codec
, nid
, 0, AC_VERB_GET_CONV
, 0);
1443 newval
= (stream_tag
<< 4) | channel_id
;
1444 if (oldval
!= newval
)
1445 snd_hda_codec_write(codec
, nid
, 0,
1446 AC_VERB_SET_CHANNEL_STREAMID
,
1448 p
->stream_tag
= stream_tag
;
1449 p
->channel_id
= channel_id
;
1453 /* update the format-id if changed */
1454 static void update_pcm_format(struct hda_codec
*codec
, struct hda_cvt_setup
*p
,
1455 hda_nid_t nid
, int format
)
1457 unsigned int oldval
;
1459 if (p
->format_id
!= format
) {
1460 oldval
= snd_hda_codec_read(codec
, nid
, 0,
1461 AC_VERB_GET_STREAM_FORMAT
, 0);
1462 if (oldval
!= format
) {
1464 snd_hda_codec_write(codec
, nid
, 0,
1465 AC_VERB_SET_STREAM_FORMAT
,
1468 p
->format_id
= format
;
1473 * snd_hda_codec_setup_stream - set up the codec for streaming
1474 * @codec: the CODEC to set up
1475 * @nid: the NID to set up
1476 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
1477 * @channel_id: channel id to pass, zero based.
1478 * @format: stream format.
1480 void snd_hda_codec_setup_stream(struct hda_codec
*codec
, hda_nid_t nid
,
1482 int channel_id
, int format
)
1484 struct hda_codec
*c
;
1485 struct hda_cvt_setup
*p
;
1493 "hda_codec_setup_stream: NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
1494 nid
, stream_tag
, channel_id
, format
);
1495 p
= get_hda_cvt_setup(codec
, nid
);
1499 if (codec
->pcm_format_first
)
1500 update_pcm_format(codec
, p
, nid
, format
);
1501 update_pcm_stream_id(codec
, p
, nid
, stream_tag
, channel_id
);
1502 if (!codec
->pcm_format_first
)
1503 update_pcm_format(codec
, p
, nid
, format
);
1508 /* make other inactive cvts with the same stream-tag dirty */
1509 type
= get_wcaps_type(get_wcaps(codec
, nid
));
1510 list_for_each_entry(c
, &codec
->bus
->codec_list
, list
) {
1511 for (i
= 0; i
< c
->cvt_setups
.used
; i
++) {
1512 p
= snd_array_elem(&c
->cvt_setups
, i
);
1513 if (!p
->active
&& p
->stream_tag
== stream_tag
&&
1514 get_wcaps_type(get_wcaps(c
, p
->nid
)) == type
)
1519 EXPORT_SYMBOL_GPL(snd_hda_codec_setup_stream
);
1521 static void really_cleanup_stream(struct hda_codec
*codec
,
1522 struct hda_cvt_setup
*q
);
1525 * __snd_hda_codec_cleanup_stream - clean up the codec for closing
1526 * @codec: the CODEC to clean up
1527 * @nid: the NID to clean up
1528 * @do_now: really clean up the stream instead of clearing the active flag
1530 void __snd_hda_codec_cleanup_stream(struct hda_codec
*codec
, hda_nid_t nid
,
1533 struct hda_cvt_setup
*p
;
1538 if (codec
->no_sticky_stream
)
1541 codec_dbg(codec
, "hda_codec_cleanup_stream: NID=0x%x\n", nid
);
1542 p
= get_hda_cvt_setup(codec
, nid
);
1544 /* here we just clear the active flag when do_now isn't set;
1545 * actual clean-ups will be done later in
1546 * purify_inactive_streams() called from snd_hda_codec_prpapre()
1549 really_cleanup_stream(codec
, p
);
1554 EXPORT_SYMBOL_GPL(__snd_hda_codec_cleanup_stream
);
1556 static void really_cleanup_stream(struct hda_codec
*codec
,
1557 struct hda_cvt_setup
*q
)
1559 hda_nid_t nid
= q
->nid
;
1560 if (q
->stream_tag
|| q
->channel_id
)
1561 snd_hda_codec_write(codec
, nid
, 0, AC_VERB_SET_CHANNEL_STREAMID
, 0);
1563 snd_hda_codec_write(codec
, nid
, 0, AC_VERB_SET_STREAM_FORMAT
, 0
1565 memset(q
, 0, sizeof(*q
));
1569 /* clean up the all conflicting obsolete streams */
1570 static void purify_inactive_streams(struct hda_codec
*codec
)
1572 struct hda_codec
*c
;
1575 list_for_each_entry(c
, &codec
->bus
->codec_list
, list
) {
1576 for (i
= 0; i
< c
->cvt_setups
.used
; i
++) {
1577 struct hda_cvt_setup
*p
;
1578 p
= snd_array_elem(&c
->cvt_setups
, i
);
1580 really_cleanup_stream(c
, p
);
1586 /* clean up all streams; called from suspend */
1587 static void hda_cleanup_all_streams(struct hda_codec
*codec
)
1591 for (i
= 0; i
< codec
->cvt_setups
.used
; i
++) {
1592 struct hda_cvt_setup
*p
= snd_array_elem(&codec
->cvt_setups
, i
);
1594 really_cleanup_stream(codec
, p
);
1600 * amp access functions
1603 /* FIXME: more better hash key? */
1604 #define HDA_HASH_KEY(nid, dir, idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
1605 #define HDA_HASH_PINCAP_KEY(nid) (u32)((nid) + (0x02 << 24))
1606 #define HDA_HASH_PARPCM_KEY(nid) (u32)((nid) + (0x03 << 24))
1607 #define HDA_HASH_PARSTR_KEY(nid) (u32)((nid) + (0x04 << 24))
1608 #define INFO_AMP_CAPS (1<<0)
1609 #define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
1611 /* initialize the hash table */
1612 static void init_hda_cache(struct hda_cache_rec
*cache
,
1613 unsigned int record_size
)
1615 memset(cache
, 0, sizeof(*cache
));
1616 memset(cache
->hash
, 0xff, sizeof(cache
->hash
));
1617 snd_array_init(&cache
->buf
, record_size
, 64);
1620 static void free_hda_cache(struct hda_cache_rec
*cache
)
1622 snd_array_free(&cache
->buf
);
1625 /* query the hash. allocate an entry if not found. */
1626 static struct hda_cache_head
*get_hash(struct hda_cache_rec
*cache
, u32 key
)
1628 u16 idx
= key
% (u16
)ARRAY_SIZE(cache
->hash
);
1629 u16 cur
= cache
->hash
[idx
];
1630 struct hda_cache_head
*info
;
1632 while (cur
!= 0xffff) {
1633 info
= snd_array_elem(&cache
->buf
, cur
);
1634 if (info
->key
== key
)
1641 /* query the hash. allocate an entry if not found. */
1642 static struct hda_cache_head
*get_alloc_hash(struct hda_cache_rec
*cache
,
1645 struct hda_cache_head
*info
= get_hash(cache
, key
);
1648 /* add a new hash entry */
1649 info
= snd_array_new(&cache
->buf
);
1652 cur
= snd_array_index(&cache
->buf
, info
);
1656 idx
= key
% (u16
)ARRAY_SIZE(cache
->hash
);
1657 info
->next
= cache
->hash
[idx
];
1658 cache
->hash
[idx
] = cur
;
1663 /* query and allocate an amp hash entry */
1664 static inline struct hda_amp_info
*
1665 get_alloc_amp_hash(struct hda_codec
*codec
, u32 key
)
1667 return (struct hda_amp_info
*)get_alloc_hash(&codec
->amp_cache
, key
);
1670 /* overwrite the value with the key in the caps hash */
1671 static int write_caps_hash(struct hda_codec
*codec
, u32 key
, unsigned int val
)
1673 struct hda_amp_info
*info
;
1675 mutex_lock(&codec
->hash_mutex
);
1676 info
= get_alloc_amp_hash(codec
, key
);
1678 mutex_unlock(&codec
->hash_mutex
);
1681 info
->amp_caps
= val
;
1682 info
->head
.val
|= INFO_AMP_CAPS
;
1683 mutex_unlock(&codec
->hash_mutex
);
1687 /* query the value from the caps hash; if not found, fetch the current
1688 * value from the given function and store in the hash
1691 query_caps_hash(struct hda_codec
*codec
, hda_nid_t nid
, int dir
, u32 key
,
1692 unsigned int (*func
)(struct hda_codec
*, hda_nid_t
, int))
1694 struct hda_amp_info
*info
;
1697 mutex_lock(&codec
->hash_mutex
);
1698 info
= get_alloc_amp_hash(codec
, key
);
1700 mutex_unlock(&codec
->hash_mutex
);
1703 if (!(info
->head
.val
& INFO_AMP_CAPS
)) {
1704 mutex_unlock(&codec
->hash_mutex
); /* for reentrance */
1705 val
= func(codec
, nid
, dir
);
1706 write_caps_hash(codec
, key
, val
);
1708 val
= info
->amp_caps
;
1709 mutex_unlock(&codec
->hash_mutex
);
1714 static unsigned int read_amp_cap(struct hda_codec
*codec
, hda_nid_t nid
,
1717 if (!(get_wcaps(codec
, nid
) & AC_WCAP_AMP_OVRD
))
1719 return snd_hda_param_read(codec
, nid
,
1720 direction
== HDA_OUTPUT
?
1721 AC_PAR_AMP_OUT_CAP
: AC_PAR_AMP_IN_CAP
);
1725 * query_amp_caps - query AMP capabilities
1726 * @codec: the HD-auio codec
1727 * @nid: the NID to query
1728 * @direction: either #HDA_INPUT or #HDA_OUTPUT
1730 * Query AMP capabilities for the given widget and direction.
1731 * Returns the obtained capability bits.
1733 * When cap bits have been already read, this doesn't read again but
1734 * returns the cached value.
1736 u32
query_amp_caps(struct hda_codec
*codec
, hda_nid_t nid
, int direction
)
1738 return query_caps_hash(codec
, nid
, direction
,
1739 HDA_HASH_KEY(nid
, direction
, 0),
1742 EXPORT_SYMBOL_GPL(query_amp_caps
);
1745 * snd_hda_check_amp_caps - query AMP capabilities
1746 * @codec: the HD-audio codec
1747 * @nid: the NID to query
1748 * @dir: either #HDA_INPUT or #HDA_OUTPUT
1749 * @bits: bit mask to check the result
1751 * Check whether the widget has the given amp capability for the direction.
1753 bool snd_hda_check_amp_caps(struct hda_codec
*codec
, hda_nid_t nid
,
1754 int dir
, unsigned int bits
)
1758 if (get_wcaps(codec
, nid
) & (1 << (dir
+ 1)))
1759 if (query_amp_caps(codec
, nid
, dir
) & bits
)
1763 EXPORT_SYMBOL_GPL(snd_hda_check_amp_caps
);
1766 * snd_hda_override_amp_caps - Override the AMP capabilities
1767 * @codec: the CODEC to clean up
1768 * @nid: the NID to clean up
1769 * @dir: either #HDA_INPUT or #HDA_OUTPUT
1770 * @caps: the capability bits to set
1772 * Override the cached AMP caps bits value by the given one.
1773 * This function is useful if the driver needs to adjust the AMP ranges,
1774 * e.g. limit to 0dB, etc.
1776 * Returns zero if successful or a negative error code.
1778 int snd_hda_override_amp_caps(struct hda_codec
*codec
, hda_nid_t nid
, int dir
,
1781 return write_caps_hash(codec
, HDA_HASH_KEY(nid
, dir
, 0), caps
);
1783 EXPORT_SYMBOL_GPL(snd_hda_override_amp_caps
);
1785 static unsigned int read_pin_cap(struct hda_codec
*codec
, hda_nid_t nid
,
1788 return snd_hda_param_read(codec
, nid
, AC_PAR_PIN_CAP
);
1792 * snd_hda_query_pin_caps - Query PIN capabilities
1793 * @codec: the HD-auio codec
1794 * @nid: the NID to query
1796 * Query PIN capabilities for the given widget.
1797 * Returns the obtained capability bits.
1799 * When cap bits have been already read, this doesn't read again but
1800 * returns the cached value.
1802 u32
snd_hda_query_pin_caps(struct hda_codec
*codec
, hda_nid_t nid
)
1804 return query_caps_hash(codec
, nid
, 0, HDA_HASH_PINCAP_KEY(nid
),
1807 EXPORT_SYMBOL_GPL(snd_hda_query_pin_caps
);
1810 * snd_hda_override_pin_caps - Override the pin capabilities
1812 * @nid: the NID to override
1813 * @caps: the capability bits to set
1815 * Override the cached PIN capabilitiy bits value by the given one.
1817 * Returns zero if successful or a negative error code.
1819 int snd_hda_override_pin_caps(struct hda_codec
*codec
, hda_nid_t nid
,
1822 return write_caps_hash(codec
, HDA_HASH_PINCAP_KEY(nid
), caps
);
1824 EXPORT_SYMBOL_GPL(snd_hda_override_pin_caps
);
1826 /* read or sync the hash value with the current value;
1827 * call within hash_mutex
1829 static struct hda_amp_info
*
1830 update_amp_hash(struct hda_codec
*codec
, hda_nid_t nid
, int ch
,
1831 int direction
, int index
, bool init_only
)
1833 struct hda_amp_info
*info
;
1834 unsigned int parm
, val
= 0;
1835 bool val_read
= false;
1838 info
= get_alloc_amp_hash(codec
, HDA_HASH_KEY(nid
, direction
, index
));
1841 if (!(info
->head
.val
& INFO_AMP_VOL(ch
))) {
1843 mutex_unlock(&codec
->hash_mutex
);
1844 parm
= ch
? AC_AMP_GET_RIGHT
: AC_AMP_GET_LEFT
;
1845 parm
|= direction
== HDA_OUTPUT
?
1846 AC_AMP_GET_OUTPUT
: AC_AMP_GET_INPUT
;
1848 val
= snd_hda_codec_read(codec
, nid
, 0,
1849 AC_VERB_GET_AMP_GAIN_MUTE
, parm
);
1852 mutex_lock(&codec
->hash_mutex
);
1855 info
->vol
[ch
] = val
;
1856 info
->head
.val
|= INFO_AMP_VOL(ch
);
1857 } else if (init_only
)
1863 * write the current volume in info to the h/w
1865 static void put_vol_mute(struct hda_codec
*codec
, unsigned int amp_caps
,
1866 hda_nid_t nid
, int ch
, int direction
, int index
,
1871 parm
= ch
? AC_AMP_SET_RIGHT
: AC_AMP_SET_LEFT
;
1872 parm
|= direction
== HDA_OUTPUT
? AC_AMP_SET_OUTPUT
: AC_AMP_SET_INPUT
;
1873 parm
|= index
<< AC_AMP_SET_INDEX_SHIFT
;
1874 if ((val
& HDA_AMP_MUTE
) && !(amp_caps
& AC_AMPCAP_MUTE
) &&
1875 (amp_caps
& AC_AMPCAP_MIN_MUTE
))
1876 ; /* set the zero value as a fake mute */
1879 snd_hda_codec_write(codec
, nid
, 0, AC_VERB_SET_AMP_GAIN_MUTE
, parm
);
1883 * snd_hda_codec_amp_read - Read AMP value
1884 * @codec: HD-audio codec
1885 * @nid: NID to read the AMP value
1886 * @ch: channel (left=0 or right=1)
1887 * @direction: #HDA_INPUT or #HDA_OUTPUT
1888 * @index: the index value (only for input direction)
1890 * Read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
1892 int snd_hda_codec_amp_read(struct hda_codec
*codec
, hda_nid_t nid
, int ch
,
1893 int direction
, int index
)
1895 struct hda_amp_info
*info
;
1896 unsigned int val
= 0;
1898 mutex_lock(&codec
->hash_mutex
);
1899 info
= update_amp_hash(codec
, nid
, ch
, direction
, index
, false);
1901 val
= info
->vol
[ch
];
1902 mutex_unlock(&codec
->hash_mutex
);
1905 EXPORT_SYMBOL_GPL(snd_hda_codec_amp_read
);
1907 static int codec_amp_update(struct hda_codec
*codec
, hda_nid_t nid
, int ch
,
1908 int direction
, int idx
, int mask
, int val
,
1909 bool init_only
, bool cache_only
)
1911 struct hda_amp_info
*info
;
1914 if (snd_BUG_ON(mask
& ~0xff))
1918 mutex_lock(&codec
->hash_mutex
);
1919 info
= update_amp_hash(codec
, nid
, ch
, direction
, idx
, init_only
);
1921 mutex_unlock(&codec
->hash_mutex
);
1924 val
|= info
->vol
[ch
] & ~mask
;
1925 if (info
->vol
[ch
] == val
) {
1926 mutex_unlock(&codec
->hash_mutex
);
1929 info
->vol
[ch
] = val
;
1930 info
->head
.dirty
|= cache_only
;
1931 caps
= info
->amp_caps
;
1932 mutex_unlock(&codec
->hash_mutex
);
1934 put_vol_mute(codec
, caps
, nid
, ch
, direction
, idx
, val
);
1939 * snd_hda_codec_amp_update - update the AMP value
1940 * @codec: HD-audio codec
1941 * @nid: NID to read the AMP value
1942 * @ch: channel (left=0 or right=1)
1943 * @direction: #HDA_INPUT or #HDA_OUTPUT
1944 * @idx: the index value (only for input direction)
1945 * @mask: bit mask to set
1946 * @val: the bits value to set
1948 * Update the AMP value with a bit mask.
1949 * Returns 0 if the value is unchanged, 1 if changed.
1951 int snd_hda_codec_amp_update(struct hda_codec
*codec
, hda_nid_t nid
, int ch
,
1952 int direction
, int idx
, int mask
, int val
)
1954 return codec_amp_update(codec
, nid
, ch
, direction
, idx
, mask
, val
,
1955 false, codec
->cached_write
);
1957 EXPORT_SYMBOL_GPL(snd_hda_codec_amp_update
);
1960 * snd_hda_codec_amp_stereo - update the AMP stereo values
1961 * @codec: HD-audio codec
1962 * @nid: NID to read the AMP value
1963 * @direction: #HDA_INPUT or #HDA_OUTPUT
1964 * @idx: the index value (only for input direction)
1965 * @mask: bit mask to set
1966 * @val: the bits value to set
1968 * Update the AMP values like snd_hda_codec_amp_update(), but for a
1969 * stereo widget with the same mask and value.
1971 int snd_hda_codec_amp_stereo(struct hda_codec
*codec
, hda_nid_t nid
,
1972 int direction
, int idx
, int mask
, int val
)
1976 if (snd_BUG_ON(mask
& ~0xff))
1978 for (ch
= 0; ch
< 2; ch
++)
1979 ret
|= snd_hda_codec_amp_update(codec
, nid
, ch
, direction
,
1983 EXPORT_SYMBOL_GPL(snd_hda_codec_amp_stereo
);
1986 * snd_hda_codec_amp_init - initialize the AMP value
1987 * @codec: the HDA codec
1988 * @nid: NID to read the AMP value
1989 * @ch: channel (left=0 or right=1)
1990 * @dir: #HDA_INPUT or #HDA_OUTPUT
1991 * @idx: the index value (only for input direction)
1992 * @mask: bit mask to set
1993 * @val: the bits value to set
1995 * Works like snd_hda_codec_amp_update() but it writes the value only at
1996 * the first access. If the amp was already initialized / updated beforehand,
1997 * this does nothing.
1999 int snd_hda_codec_amp_init(struct hda_codec
*codec
, hda_nid_t nid
, int ch
,
2000 int dir
, int idx
, int mask
, int val
)
2002 return codec_amp_update(codec
, nid
, ch
, dir
, idx
, mask
, val
, true,
2003 codec
->cached_write
);
2005 EXPORT_SYMBOL_GPL(snd_hda_codec_amp_init
);
2008 * snd_hda_codec_amp_init_stereo - initialize the stereo AMP value
2009 * @codec: the HDA codec
2010 * @nid: NID to read the AMP value
2011 * @dir: #HDA_INPUT or #HDA_OUTPUT
2012 * @idx: the index value (only for input direction)
2013 * @mask: bit mask to set
2014 * @val: the bits value to set
2016 * Call snd_hda_codec_amp_init() for both stereo channels.
2018 int snd_hda_codec_amp_init_stereo(struct hda_codec
*codec
, hda_nid_t nid
,
2019 int dir
, int idx
, int mask
, int val
)
2023 if (snd_BUG_ON(mask
& ~0xff))
2025 for (ch
= 0; ch
< 2; ch
++)
2026 ret
|= snd_hda_codec_amp_init(codec
, nid
, ch
, dir
,
2030 EXPORT_SYMBOL_GPL(snd_hda_codec_amp_init_stereo
);
2033 * snd_hda_codec_resume_amp - Resume all AMP commands from the cache
2034 * @codec: HD-audio codec
2036 * Resume the all amp commands from the cache.
2038 void snd_hda_codec_resume_amp(struct hda_codec
*codec
)
2042 mutex_lock(&codec
->hash_mutex
);
2043 codec
->cached_write
= 0;
2044 for (i
= 0; i
< codec
->amp_cache
.buf
.used
; i
++) {
2045 struct hda_amp_info
*buffer
;
2048 unsigned int idx
, dir
, ch
;
2049 struct hda_amp_info info
;
2051 buffer
= snd_array_elem(&codec
->amp_cache
.buf
, i
);
2052 if (!buffer
->head
.dirty
)
2054 buffer
->head
.dirty
= 0;
2056 key
= info
.head
.key
;
2060 idx
= (key
>> 16) & 0xff;
2061 dir
= (key
>> 24) & 0xff;
2062 for (ch
= 0; ch
< 2; ch
++) {
2063 if (!(info
.head
.val
& INFO_AMP_VOL(ch
)))
2065 mutex_unlock(&codec
->hash_mutex
);
2066 put_vol_mute(codec
, info
.amp_caps
, nid
, ch
, dir
, idx
,
2068 mutex_lock(&codec
->hash_mutex
);
2071 mutex_unlock(&codec
->hash_mutex
);
2073 EXPORT_SYMBOL_GPL(snd_hda_codec_resume_amp
);
2075 static u32
get_amp_max_value(struct hda_codec
*codec
, hda_nid_t nid
, int dir
,
2078 u32 caps
= query_amp_caps(codec
, nid
, dir
);
2080 caps
= (caps
& AC_AMPCAP_NUM_STEPS
) >> AC_AMPCAP_NUM_STEPS_SHIFT
;
2087 * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer
2088 * @kcontrol: referred ctl element
2089 * @uinfo: pointer to get/store the data
2091 * The control element is supposed to have the private_value field
2092 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2094 int snd_hda_mixer_amp_volume_info(struct snd_kcontrol
*kcontrol
,
2095 struct snd_ctl_elem_info
*uinfo
)
2097 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2098 u16 nid
= get_amp_nid(kcontrol
);
2099 u8 chs
= get_amp_channels(kcontrol
);
2100 int dir
= get_amp_direction(kcontrol
);
2101 unsigned int ofs
= get_amp_offset(kcontrol
);
2103 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
2104 uinfo
->count
= chs
== 3 ? 2 : 1;
2105 uinfo
->value
.integer
.min
= 0;
2106 uinfo
->value
.integer
.max
= get_amp_max_value(codec
, nid
, dir
, ofs
);
2107 if (!uinfo
->value
.integer
.max
) {
2109 "num_steps = 0 for NID=0x%x (ctl = %s)\n",
2110 nid
, kcontrol
->id
.name
);
2115 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_info
);
2118 static inline unsigned int
2119 read_amp_value(struct hda_codec
*codec
, hda_nid_t nid
,
2120 int ch
, int dir
, int idx
, unsigned int ofs
)
2123 val
= snd_hda_codec_amp_read(codec
, nid
, ch
, dir
, idx
);
2124 val
&= HDA_AMP_VOLMASK
;
2133 update_amp_value(struct hda_codec
*codec
, hda_nid_t nid
,
2134 int ch
, int dir
, int idx
, unsigned int ofs
,
2137 unsigned int maxval
;
2141 /* ofs = 0: raw max value */
2142 maxval
= get_amp_max_value(codec
, nid
, dir
, 0);
2145 return codec_amp_update(codec
, nid
, ch
, dir
, idx
, HDA_AMP_VOLMASK
, val
,
2146 false, !hda_codec_is_power_on(codec
));
2150 * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume
2151 * @kcontrol: ctl element
2152 * @ucontrol: pointer to get/store the data
2154 * The control element is supposed to have the private_value field
2155 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2157 int snd_hda_mixer_amp_volume_get(struct snd_kcontrol
*kcontrol
,
2158 struct snd_ctl_elem_value
*ucontrol
)
2160 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2161 hda_nid_t nid
= get_amp_nid(kcontrol
);
2162 int chs
= get_amp_channels(kcontrol
);
2163 int dir
= get_amp_direction(kcontrol
);
2164 int idx
= get_amp_index(kcontrol
);
2165 unsigned int ofs
= get_amp_offset(kcontrol
);
2166 long *valp
= ucontrol
->value
.integer
.value
;
2169 *valp
++ = read_amp_value(codec
, nid
, 0, dir
, idx
, ofs
);
2171 *valp
= read_amp_value(codec
, nid
, 1, dir
, idx
, ofs
);
2174 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_get
);
2177 * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume
2178 * @kcontrol: ctl element
2179 * @ucontrol: pointer to get/store the data
2181 * The control element is supposed to have the private_value field
2182 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2184 int snd_hda_mixer_amp_volume_put(struct snd_kcontrol
*kcontrol
,
2185 struct snd_ctl_elem_value
*ucontrol
)
2187 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2188 hda_nid_t nid
= get_amp_nid(kcontrol
);
2189 int chs
= get_amp_channels(kcontrol
);
2190 int dir
= get_amp_direction(kcontrol
);
2191 int idx
= get_amp_index(kcontrol
);
2192 unsigned int ofs
= get_amp_offset(kcontrol
);
2193 long *valp
= ucontrol
->value
.integer
.value
;
2197 change
= update_amp_value(codec
, nid
, 0, dir
, idx
, ofs
, *valp
);
2201 change
|= update_amp_value(codec
, nid
, 1, dir
, idx
, ofs
, *valp
);
2204 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_put
);
2207 * snd_hda_mixer_amp_volume_put - TLV callback for a standard AMP mixer volume
2208 * @kcontrol: ctl element
2209 * @op_flag: operation flag
2210 * @size: byte size of input TLV
2213 * The control element is supposed to have the private_value field
2214 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2216 int snd_hda_mixer_amp_tlv(struct snd_kcontrol
*kcontrol
, int op_flag
,
2217 unsigned int size
, unsigned int __user
*_tlv
)
2219 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2220 hda_nid_t nid
= get_amp_nid(kcontrol
);
2221 int dir
= get_amp_direction(kcontrol
);
2222 unsigned int ofs
= get_amp_offset(kcontrol
);
2223 bool min_mute
= get_amp_min_mute(kcontrol
);
2224 u32 caps
, val1
, val2
;
2226 if (size
< 4 * sizeof(unsigned int))
2228 caps
= query_amp_caps(codec
, nid
, dir
);
2229 val2
= (caps
& AC_AMPCAP_STEP_SIZE
) >> AC_AMPCAP_STEP_SIZE_SHIFT
;
2230 val2
= (val2
+ 1) * 25;
2231 val1
= -((caps
& AC_AMPCAP_OFFSET
) >> AC_AMPCAP_OFFSET_SHIFT
);
2233 val1
= ((int)val1
) * ((int)val2
);
2234 if (min_mute
|| (caps
& AC_AMPCAP_MIN_MUTE
))
2235 val2
|= TLV_DB_SCALE_MUTE
;
2236 if (put_user(SNDRV_CTL_TLVT_DB_SCALE
, _tlv
))
2238 if (put_user(2 * sizeof(unsigned int), _tlv
+ 1))
2240 if (put_user(val1
, _tlv
+ 2))
2242 if (put_user(val2
, _tlv
+ 3))
2246 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_tlv
);
2249 * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control
2250 * @codec: HD-audio codec
2251 * @nid: NID of a reference widget
2252 * @dir: #HDA_INPUT or #HDA_OUTPUT
2253 * @tlv: TLV data to be stored, at least 4 elements
2255 * Set (static) TLV data for a virtual master volume using the AMP caps
2256 * obtained from the reference NID.
2257 * The volume range is recalculated as if the max volume is 0dB.
2259 void snd_hda_set_vmaster_tlv(struct hda_codec
*codec
, hda_nid_t nid
, int dir
,
2265 caps
= query_amp_caps(codec
, nid
, dir
);
2266 nums
= (caps
& AC_AMPCAP_NUM_STEPS
) >> AC_AMPCAP_NUM_STEPS_SHIFT
;
2267 step
= (caps
& AC_AMPCAP_STEP_SIZE
) >> AC_AMPCAP_STEP_SIZE_SHIFT
;
2268 step
= (step
+ 1) * 25;
2269 tlv
[0] = SNDRV_CTL_TLVT_DB_SCALE
;
2270 tlv
[1] = 2 * sizeof(unsigned int);
2271 tlv
[2] = -nums
* step
;
2274 EXPORT_SYMBOL_GPL(snd_hda_set_vmaster_tlv
);
2276 /* find a mixer control element with the given name */
2277 static struct snd_kcontrol
*
2278 find_mixer_ctl(struct hda_codec
*codec
, const char *name
, int dev
, int idx
)
2280 struct snd_ctl_elem_id id
;
2281 memset(&id
, 0, sizeof(id
));
2282 id
.iface
= SNDRV_CTL_ELEM_IFACE_MIXER
;
2285 if (snd_BUG_ON(strlen(name
) >= sizeof(id
.name
)))
2287 strcpy(id
.name
, name
);
2288 return snd_ctl_find_id(codec
->card
, &id
);
2292 * snd_hda_find_mixer_ctl - Find a mixer control element with the given name
2293 * @codec: HD-audio codec
2294 * @name: ctl id name string
2296 * Get the control element with the given id string and IFACE_MIXER.
2298 struct snd_kcontrol
*snd_hda_find_mixer_ctl(struct hda_codec
*codec
,
2301 return find_mixer_ctl(codec
, name
, 0, 0);
2303 EXPORT_SYMBOL_GPL(snd_hda_find_mixer_ctl
);
2305 static int find_empty_mixer_ctl_idx(struct hda_codec
*codec
, const char *name
,
2309 /* 16 ctlrs should be large enough */
2310 for (i
= 0, idx
= start_idx
; i
< 16; i
++, idx
++) {
2311 if (!find_mixer_ctl(codec
, name
, 0, idx
))
2318 * snd_hda_ctl_add - Add a control element and assign to the codec
2319 * @codec: HD-audio codec
2320 * @nid: corresponding NID (optional)
2321 * @kctl: the control element to assign
2323 * Add the given control element to an array inside the codec instance.
2324 * All control elements belonging to a codec are supposed to be added
2325 * by this function so that a proper clean-up works at the free or
2326 * reconfiguration time.
2328 * If non-zero @nid is passed, the NID is assigned to the control element.
2329 * The assignment is shown in the codec proc file.
2331 * snd_hda_ctl_add() checks the control subdev id field whether
2332 * #HDA_SUBDEV_NID_FLAG bit is set. If set (and @nid is zero), the lower
2333 * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit
2334 * specifies if kctl->private_value is a HDA amplifier value.
2336 int snd_hda_ctl_add(struct hda_codec
*codec
, hda_nid_t nid
,
2337 struct snd_kcontrol
*kctl
)
2340 unsigned short flags
= 0;
2341 struct hda_nid_item
*item
;
2343 if (kctl
->id
.subdevice
& HDA_SUBDEV_AMP_FLAG
) {
2344 flags
|= HDA_NID_ITEM_AMP
;
2346 nid
= get_amp_nid_(kctl
->private_value
);
2348 if ((kctl
->id
.subdevice
& HDA_SUBDEV_NID_FLAG
) != 0 && nid
== 0)
2349 nid
= kctl
->id
.subdevice
& 0xffff;
2350 if (kctl
->id
.subdevice
& (HDA_SUBDEV_NID_FLAG
|HDA_SUBDEV_AMP_FLAG
))
2351 kctl
->id
.subdevice
= 0;
2352 err
= snd_ctl_add(codec
->card
, kctl
);
2355 item
= snd_array_new(&codec
->mixers
);
2360 item
->flags
= flags
;
2363 EXPORT_SYMBOL_GPL(snd_hda_ctl_add
);
2366 * snd_hda_add_nid - Assign a NID to a control element
2367 * @codec: HD-audio codec
2368 * @nid: corresponding NID (optional)
2369 * @kctl: the control element to assign
2370 * @index: index to kctl
2372 * Add the given control element to an array inside the codec instance.
2373 * This function is used when #snd_hda_ctl_add cannot be used for 1:1
2374 * NID:KCTL mapping - for example "Capture Source" selector.
2376 int snd_hda_add_nid(struct hda_codec
*codec
, struct snd_kcontrol
*kctl
,
2377 unsigned int index
, hda_nid_t nid
)
2379 struct hda_nid_item
*item
;
2382 item
= snd_array_new(&codec
->nids
);
2386 item
->index
= index
;
2390 codec_err(codec
, "no NID for mapping control %s:%d:%d\n",
2391 kctl
->id
.name
, kctl
->id
.index
, index
);
2394 EXPORT_SYMBOL_GPL(snd_hda_add_nid
);
2397 * snd_hda_ctls_clear - Clear all controls assigned to the given codec
2398 * @codec: HD-audio codec
2400 void snd_hda_ctls_clear(struct hda_codec
*codec
)
2403 struct hda_nid_item
*items
= codec
->mixers
.list
;
2404 for (i
= 0; i
< codec
->mixers
.used
; i
++)
2405 snd_ctl_remove(codec
->card
, items
[i
].kctl
);
2406 snd_array_free(&codec
->mixers
);
2407 snd_array_free(&codec
->nids
);
2411 * snd_hda_lock_devices - pseudo device locking
2414 * toggle card->shutdown to allow/disallow the device access (as a hack)
2416 int snd_hda_lock_devices(struct hda_bus
*bus
)
2418 struct snd_card
*card
= bus
->card
;
2419 struct hda_codec
*codec
;
2421 spin_lock(&card
->files_lock
);
2425 if (!list_empty(&card
->ctl_files
))
2428 list_for_each_entry(codec
, &bus
->codec_list
, list
) {
2429 struct hda_pcm
*cpcm
;
2430 list_for_each_entry(cpcm
, &codec
->pcm_list_head
, list
) {
2433 if (cpcm
->pcm
->streams
[0].substream_opened
||
2434 cpcm
->pcm
->streams
[1].substream_opened
)
2438 spin_unlock(&card
->files_lock
);
2444 spin_unlock(&card
->files_lock
);
2447 EXPORT_SYMBOL_GPL(snd_hda_lock_devices
);
2450 * snd_hda_unlock_devices - pseudo device unlocking
2453 void snd_hda_unlock_devices(struct hda_bus
*bus
)
2455 struct snd_card
*card
= bus
->card
;
2458 spin_lock(&card
->files_lock
);
2460 spin_unlock(&card
->files_lock
);
2462 EXPORT_SYMBOL_GPL(snd_hda_unlock_devices
);
2465 * snd_hda_codec_reset - Clear all objects assigned to the codec
2466 * @codec: HD-audio codec
2468 * This frees the all PCM and control elements assigned to the codec, and
2469 * clears the caches and restores the pin default configurations.
2471 * When a device is being used, it returns -EBSY. If successfully freed,
2474 int snd_hda_codec_reset(struct hda_codec
*codec
)
2476 struct hda_bus
*bus
= codec
->bus
;
2478 if (snd_hda_lock_devices(bus
) < 0)
2481 /* OK, let it free */
2482 cancel_delayed_work_sync(&codec
->jackpoll_work
);
2483 flush_workqueue(bus
->workq
);
2484 snd_hda_ctls_clear(codec
);
2485 codec_release_pcms(codec
);
2486 snd_hda_detach_beep_device(codec
);
2487 if (device_is_registered(hda_codec_dev(codec
)))
2488 device_del(hda_codec_dev(codec
));
2490 memset(&codec
->patch_ops
, 0, sizeof(codec
->patch_ops
));
2491 snd_hda_jack_tbl_clear(codec
);
2492 codec
->proc_widget_hook
= NULL
;
2494 free_hda_cache(&codec
->amp_cache
);
2495 free_hda_cache(&codec
->cmd_cache
);
2496 init_hda_cache(&codec
->amp_cache
, sizeof(struct hda_amp_info
));
2497 init_hda_cache(&codec
->cmd_cache
, sizeof(struct hda_cache_head
));
2498 /* free only driver_pins so that init_pins + user_pins are restored */
2499 snd_array_free(&codec
->driver_pins
);
2500 snd_array_free(&codec
->cvt_setups
);
2501 snd_array_free(&codec
->spdif_out
);
2502 snd_array_free(&codec
->verbs
);
2503 codec
->preset
= NULL
;
2504 codec
->slave_dig_outs
= NULL
;
2505 codec
->spdif_status_reset
= 0;
2507 /* allow device access again */
2508 snd_hda_unlock_devices(bus
);
2512 typedef int (*map_slave_func_t
)(struct hda_codec
*, void *, struct snd_kcontrol
*);
2514 /* apply the function to all matching slave ctls in the mixer list */
2515 static int map_slaves(struct hda_codec
*codec
, const char * const *slaves
,
2516 const char *suffix
, map_slave_func_t func
, void *data
)
2518 struct hda_nid_item
*items
;
2519 const char * const *s
;
2522 items
= codec
->mixers
.list
;
2523 for (i
= 0; i
< codec
->mixers
.used
; i
++) {
2524 struct snd_kcontrol
*sctl
= items
[i
].kctl
;
2525 if (!sctl
|| sctl
->id
.iface
!= SNDRV_CTL_ELEM_IFACE_MIXER
)
2527 for (s
= slaves
; *s
; s
++) {
2528 char tmpname
[sizeof(sctl
->id
.name
)];
2529 const char *name
= *s
;
2531 snprintf(tmpname
, sizeof(tmpname
), "%s %s",
2535 if (!strcmp(sctl
->id
.name
, name
)) {
2536 err
= func(codec
, data
, sctl
);
2546 static int check_slave_present(struct hda_codec
*codec
,
2547 void *data
, struct snd_kcontrol
*sctl
)
2552 /* guess the value corresponding to 0dB */
2553 static int get_kctl_0dB_offset(struct hda_codec
*codec
,
2554 struct snd_kcontrol
*kctl
, int *step_to_check
)
2557 const int *tlv
= NULL
;
2560 if (kctl
->vd
[0].access
& SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK
) {
2561 /* FIXME: set_fs() hack for obtaining user-space TLV data */
2562 mm_segment_t fs
= get_fs();
2564 if (!kctl
->tlv
.c(kctl
, 0, sizeof(_tlv
), _tlv
))
2567 } else if (kctl
->vd
[0].access
& SNDRV_CTL_ELEM_ACCESS_TLV_READ
)
2569 if (tlv
&& tlv
[0] == SNDRV_CTL_TLVT_DB_SCALE
) {
2571 step
&= ~TLV_DB_SCALE_MUTE
;
2574 if (*step_to_check
&& *step_to_check
!= step
) {
2575 codec_err(codec
, "Mismatching dB step for vmaster slave (%d!=%d)\n",
2576 - *step_to_check
, step
);
2579 *step_to_check
= step
;
2580 val
= -tlv
[2] / step
;
2585 /* call kctl->put with the given value(s) */
2586 static int put_kctl_with_value(struct snd_kcontrol
*kctl
, int val
)
2588 struct snd_ctl_elem_value
*ucontrol
;
2589 ucontrol
= kzalloc(sizeof(*ucontrol
), GFP_KERNEL
);
2592 ucontrol
->value
.integer
.value
[0] = val
;
2593 ucontrol
->value
.integer
.value
[1] = val
;
2594 kctl
->put(kctl
, ucontrol
);
2599 /* initialize the slave volume with 0dB */
2600 static int init_slave_0dB(struct hda_codec
*codec
,
2601 void *data
, struct snd_kcontrol
*slave
)
2603 int offset
= get_kctl_0dB_offset(codec
, slave
, data
);
2605 put_kctl_with_value(slave
, offset
);
2609 /* unmute the slave */
2610 static int init_slave_unmute(struct hda_codec
*codec
,
2611 void *data
, struct snd_kcontrol
*slave
)
2613 return put_kctl_with_value(slave
, 1);
2616 static int add_slave(struct hda_codec
*codec
,
2617 void *data
, struct snd_kcontrol
*slave
)
2619 return snd_ctl_add_slave(data
, slave
);
2623 * __snd_hda_add_vmaster - create a virtual master control and add slaves
2624 * @codec: HD-audio codec
2625 * @name: vmaster control name
2626 * @tlv: TLV data (optional)
2627 * @slaves: slave control names (optional)
2628 * @suffix: suffix string to each slave name (optional)
2629 * @init_slave_vol: initialize slaves to unmute/0dB
2630 * @ctl_ret: store the vmaster kcontrol in return
2632 * Create a virtual master control with the given name. The TLV data
2633 * must be either NULL or a valid data.
2635 * @slaves is a NULL-terminated array of strings, each of which is a
2636 * slave control name. All controls with these names are assigned to
2637 * the new virtual master control.
2639 * This function returns zero if successful or a negative error code.
2641 int __snd_hda_add_vmaster(struct hda_codec
*codec
, char *name
,
2642 unsigned int *tlv
, const char * const *slaves
,
2643 const char *suffix
, bool init_slave_vol
,
2644 struct snd_kcontrol
**ctl_ret
)
2646 struct snd_kcontrol
*kctl
;
2652 err
= map_slaves(codec
, slaves
, suffix
, check_slave_present
, NULL
);
2654 codec_dbg(codec
, "No slave found for %s\n", name
);
2657 kctl
= snd_ctl_make_virtual_master(name
, tlv
);
2660 err
= snd_hda_ctl_add(codec
, 0, kctl
);
2664 err
= map_slaves(codec
, slaves
, suffix
, add_slave
, kctl
);
2668 /* init with master mute & zero volume */
2669 put_kctl_with_value(kctl
, 0);
2670 if (init_slave_vol
) {
2672 map_slaves(codec
, slaves
, suffix
,
2673 tlv
? init_slave_0dB
: init_slave_unmute
, &step
);
2680 EXPORT_SYMBOL_GPL(__snd_hda_add_vmaster
);
2683 * mute-LED control using vmaster
2685 static int vmaster_mute_mode_info(struct snd_kcontrol
*kcontrol
,
2686 struct snd_ctl_elem_info
*uinfo
)
2688 static const char * const texts
[] = {
2689 "On", "Off", "Follow Master"
2692 return snd_ctl_enum_info(uinfo
, 1, 3, texts
);
2695 static int vmaster_mute_mode_get(struct snd_kcontrol
*kcontrol
,
2696 struct snd_ctl_elem_value
*ucontrol
)
2698 struct hda_vmaster_mute_hook
*hook
= snd_kcontrol_chip(kcontrol
);
2699 ucontrol
->value
.enumerated
.item
[0] = hook
->mute_mode
;
2703 static int vmaster_mute_mode_put(struct snd_kcontrol
*kcontrol
,
2704 struct snd_ctl_elem_value
*ucontrol
)
2706 struct hda_vmaster_mute_hook
*hook
= snd_kcontrol_chip(kcontrol
);
2707 unsigned int old_mode
= hook
->mute_mode
;
2709 hook
->mute_mode
= ucontrol
->value
.enumerated
.item
[0];
2710 if (hook
->mute_mode
> HDA_VMUTE_FOLLOW_MASTER
)
2711 hook
->mute_mode
= HDA_VMUTE_FOLLOW_MASTER
;
2712 if (old_mode
== hook
->mute_mode
)
2714 snd_hda_sync_vmaster_hook(hook
);
2718 static struct snd_kcontrol_new vmaster_mute_mode
= {
2719 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
2720 .name
= "Mute-LED Mode",
2721 .info
= vmaster_mute_mode_info
,
2722 .get
= vmaster_mute_mode_get
,
2723 .put
= vmaster_mute_mode_put
,
2727 * snd_hda_add_vmaster_hook - Add a vmaster hook for mute-LED
2728 * @codec: the HDA codec
2729 * @hook: the vmaster hook object
2730 * @expose_enum_ctl: flag to create an enum ctl
2732 * Add a mute-LED hook with the given vmaster switch kctl.
2733 * When @expose_enum_ctl is set, "Mute-LED Mode" control is automatically
2734 * created and associated with the given hook.
2736 int snd_hda_add_vmaster_hook(struct hda_codec
*codec
,
2737 struct hda_vmaster_mute_hook
*hook
,
2738 bool expose_enum_ctl
)
2740 struct snd_kcontrol
*kctl
;
2742 if (!hook
->hook
|| !hook
->sw_kctl
)
2744 snd_ctl_add_vmaster_hook(hook
->sw_kctl
, hook
->hook
, codec
);
2745 hook
->codec
= codec
;
2746 hook
->mute_mode
= HDA_VMUTE_FOLLOW_MASTER
;
2747 if (!expose_enum_ctl
)
2749 kctl
= snd_ctl_new1(&vmaster_mute_mode
, hook
);
2752 return snd_hda_ctl_add(codec
, 0, kctl
);
2754 EXPORT_SYMBOL_GPL(snd_hda_add_vmaster_hook
);
2757 * snd_hda_sync_vmaster_hook - Sync vmaster hook
2758 * @hook: the vmaster hook
2760 * Call the hook with the current value for synchronization.
2761 * Should be called in init callback.
2763 void snd_hda_sync_vmaster_hook(struct hda_vmaster_mute_hook
*hook
)
2765 if (!hook
->hook
|| !hook
->codec
)
2767 /* don't call vmaster hook in the destructor since it might have
2768 * been already destroyed
2770 if (hook
->codec
->bus
->shutdown
)
2772 switch (hook
->mute_mode
) {
2773 case HDA_VMUTE_FOLLOW_MASTER
:
2774 snd_ctl_sync_vmaster_hook(hook
->sw_kctl
);
2777 hook
->hook(hook
->codec
, hook
->mute_mode
);
2781 EXPORT_SYMBOL_GPL(snd_hda_sync_vmaster_hook
);
2785 * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch
2786 * @kcontrol: referred ctl element
2787 * @uinfo: pointer to get/store the data
2789 * The control element is supposed to have the private_value field
2790 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2792 int snd_hda_mixer_amp_switch_info(struct snd_kcontrol
*kcontrol
,
2793 struct snd_ctl_elem_info
*uinfo
)
2795 int chs
= get_amp_channels(kcontrol
);
2797 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_BOOLEAN
;
2798 uinfo
->count
= chs
== 3 ? 2 : 1;
2799 uinfo
->value
.integer
.min
= 0;
2800 uinfo
->value
.integer
.max
= 1;
2803 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_info
);
2806 * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch
2807 * @kcontrol: ctl element
2808 * @ucontrol: pointer to get/store the data
2810 * The control element is supposed to have the private_value field
2811 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2813 int snd_hda_mixer_amp_switch_get(struct snd_kcontrol
*kcontrol
,
2814 struct snd_ctl_elem_value
*ucontrol
)
2816 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2817 hda_nid_t nid
= get_amp_nid(kcontrol
);
2818 int chs
= get_amp_channels(kcontrol
);
2819 int dir
= get_amp_direction(kcontrol
);
2820 int idx
= get_amp_index(kcontrol
);
2821 long *valp
= ucontrol
->value
.integer
.value
;
2824 *valp
++ = (snd_hda_codec_amp_read(codec
, nid
, 0, dir
, idx
) &
2825 HDA_AMP_MUTE
) ? 0 : 1;
2827 *valp
= (snd_hda_codec_amp_read(codec
, nid
, 1, dir
, idx
) &
2828 HDA_AMP_MUTE
) ? 0 : 1;
2831 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_get
);
2834 * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch
2835 * @kcontrol: ctl element
2836 * @ucontrol: pointer to get/store the data
2838 * The control element is supposed to have the private_value field
2839 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2841 int snd_hda_mixer_amp_switch_put(struct snd_kcontrol
*kcontrol
,
2842 struct snd_ctl_elem_value
*ucontrol
)
2844 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2845 hda_nid_t nid
= get_amp_nid(kcontrol
);
2846 int chs
= get_amp_channels(kcontrol
);
2847 int dir
= get_amp_direction(kcontrol
);
2848 int idx
= get_amp_index(kcontrol
);
2849 long *valp
= ucontrol
->value
.integer
.value
;
2853 change
= codec_amp_update(codec
, nid
, 0, dir
, idx
,
2855 *valp
? 0 : HDA_AMP_MUTE
, false,
2856 !hda_codec_is_power_on(codec
));
2860 change
|= codec_amp_update(codec
, nid
, 1, dir
, idx
,
2862 *valp
? 0 : HDA_AMP_MUTE
, false,
2863 !hda_codec_is_power_on(codec
));
2864 hda_call_check_power_status(codec
, nid
);
2867 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_put
);
2870 * bound volume controls
2872 * bind multiple volumes (# indices, from 0)
2875 #define AMP_VAL_IDX_SHIFT 19
2876 #define AMP_VAL_IDX_MASK (0x0f<<19)
2879 * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control
2880 * @kcontrol: ctl element
2881 * @ucontrol: pointer to get/store the data
2883 * The control element is supposed to have the private_value field
2884 * set up via HDA_BIND_MUTE*() macros.
2886 int snd_hda_mixer_bind_switch_get(struct snd_kcontrol
*kcontrol
,
2887 struct snd_ctl_elem_value
*ucontrol
)
2889 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2893 mutex_lock(&codec
->control_mutex
);
2894 pval
= kcontrol
->private_value
;
2895 kcontrol
->private_value
= pval
& ~AMP_VAL_IDX_MASK
; /* index 0 */
2896 err
= snd_hda_mixer_amp_switch_get(kcontrol
, ucontrol
);
2897 kcontrol
->private_value
= pval
;
2898 mutex_unlock(&codec
->control_mutex
);
2901 EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_switch_get
);
2904 * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control
2905 * @kcontrol: ctl element
2906 * @ucontrol: pointer to get/store the data
2908 * The control element is supposed to have the private_value field
2909 * set up via HDA_BIND_MUTE*() macros.
2911 int snd_hda_mixer_bind_switch_put(struct snd_kcontrol
*kcontrol
,
2912 struct snd_ctl_elem_value
*ucontrol
)
2914 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2916 int i
, indices
, err
= 0, change
= 0;
2918 mutex_lock(&codec
->control_mutex
);
2919 pval
= kcontrol
->private_value
;
2920 indices
= (pval
& AMP_VAL_IDX_MASK
) >> AMP_VAL_IDX_SHIFT
;
2921 for (i
= 0; i
< indices
; i
++) {
2922 kcontrol
->private_value
= (pval
& ~AMP_VAL_IDX_MASK
) |
2923 (i
<< AMP_VAL_IDX_SHIFT
);
2924 err
= snd_hda_mixer_amp_switch_put(kcontrol
, ucontrol
);
2929 kcontrol
->private_value
= pval
;
2930 mutex_unlock(&codec
->control_mutex
);
2931 return err
< 0 ? err
: change
;
2933 EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_switch_put
);
2936 * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control
2937 * @kcontrol: referred ctl element
2938 * @uinfo: pointer to get/store the data
2940 * The control element is supposed to have the private_value field
2941 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2943 int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol
*kcontrol
,
2944 struct snd_ctl_elem_info
*uinfo
)
2946 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2947 struct hda_bind_ctls
*c
;
2950 mutex_lock(&codec
->control_mutex
);
2951 c
= (struct hda_bind_ctls
*)kcontrol
->private_value
;
2952 kcontrol
->private_value
= *c
->values
;
2953 err
= c
->ops
->info(kcontrol
, uinfo
);
2954 kcontrol
->private_value
= (long)c
;
2955 mutex_unlock(&codec
->control_mutex
);
2958 EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_info
);
2961 * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control
2962 * @kcontrol: ctl element
2963 * @ucontrol: pointer to get/store the data
2965 * The control element is supposed to have the private_value field
2966 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2968 int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol
*kcontrol
,
2969 struct snd_ctl_elem_value
*ucontrol
)
2971 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2972 struct hda_bind_ctls
*c
;
2975 mutex_lock(&codec
->control_mutex
);
2976 c
= (struct hda_bind_ctls
*)kcontrol
->private_value
;
2977 kcontrol
->private_value
= *c
->values
;
2978 err
= c
->ops
->get(kcontrol
, ucontrol
);
2979 kcontrol
->private_value
= (long)c
;
2980 mutex_unlock(&codec
->control_mutex
);
2983 EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_get
);
2986 * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control
2987 * @kcontrol: ctl element
2988 * @ucontrol: pointer to get/store the data
2990 * The control element is supposed to have the private_value field
2991 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2993 int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol
*kcontrol
,
2994 struct snd_ctl_elem_value
*ucontrol
)
2996 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2997 struct hda_bind_ctls
*c
;
2998 unsigned long *vals
;
2999 int err
= 0, change
= 0;
3001 mutex_lock(&codec
->control_mutex
);
3002 c
= (struct hda_bind_ctls
*)kcontrol
->private_value
;
3003 for (vals
= c
->values
; *vals
; vals
++) {
3004 kcontrol
->private_value
= *vals
;
3005 err
= c
->ops
->put(kcontrol
, ucontrol
);
3010 kcontrol
->private_value
= (long)c
;
3011 mutex_unlock(&codec
->control_mutex
);
3012 return err
< 0 ? err
: change
;
3014 EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_put
);
3017 * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control
3018 * @kcontrol: ctl element
3019 * @op_flag: operation flag
3020 * @size: byte size of input TLV
3023 * The control element is supposed to have the private_value field
3024 * set up via HDA_BIND_VOL() macro.
3026 int snd_hda_mixer_bind_tlv(struct snd_kcontrol
*kcontrol
, int op_flag
,
3027 unsigned int size
, unsigned int __user
*tlv
)
3029 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
3030 struct hda_bind_ctls
*c
;
3033 mutex_lock(&codec
->control_mutex
);
3034 c
= (struct hda_bind_ctls
*)kcontrol
->private_value
;
3035 kcontrol
->private_value
= *c
->values
;
3036 err
= c
->ops
->tlv(kcontrol
, op_flag
, size
, tlv
);
3037 kcontrol
->private_value
= (long)c
;
3038 mutex_unlock(&codec
->control_mutex
);
3041 EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_tlv
);
3043 struct hda_ctl_ops snd_hda_bind_vol
= {
3044 .info
= snd_hda_mixer_amp_volume_info
,
3045 .get
= snd_hda_mixer_amp_volume_get
,
3046 .put
= snd_hda_mixer_amp_volume_put
,
3047 .tlv
= snd_hda_mixer_amp_tlv
3049 EXPORT_SYMBOL_GPL(snd_hda_bind_vol
);
3051 struct hda_ctl_ops snd_hda_bind_sw
= {
3052 .info
= snd_hda_mixer_amp_switch_info
,
3053 .get
= snd_hda_mixer_amp_switch_get
,
3054 .put
= snd_hda_mixer_amp_switch_put
,
3055 .tlv
= snd_hda_mixer_amp_tlv
3057 EXPORT_SYMBOL_GPL(snd_hda_bind_sw
);
3060 * SPDIF out controls
3063 static int snd_hda_spdif_mask_info(struct snd_kcontrol
*kcontrol
,
3064 struct snd_ctl_elem_info
*uinfo
)
3066 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_IEC958
;
3071 static int snd_hda_spdif_cmask_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_CON_EMPHASIS_5015
|
3077 IEC958_AES0_CON_NOT_COPYRIGHT
;
3078 ucontrol
->value
.iec958
.status
[1] = IEC958_AES1_CON_CATEGORY
|
3079 IEC958_AES1_CON_ORIGINAL
;
3083 static int snd_hda_spdif_pmask_get(struct snd_kcontrol
*kcontrol
,
3084 struct snd_ctl_elem_value
*ucontrol
)
3086 ucontrol
->value
.iec958
.status
[0] = IEC958_AES0_PROFESSIONAL
|
3087 IEC958_AES0_NONAUDIO
|
3088 IEC958_AES0_PRO_EMPHASIS_5015
;
3092 static int snd_hda_spdif_default_get(struct snd_kcontrol
*kcontrol
,
3093 struct snd_ctl_elem_value
*ucontrol
)
3095 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
3096 int idx
= kcontrol
->private_value
;
3097 struct hda_spdif_out
*spdif
;
3099 mutex_lock(&codec
->spdif_mutex
);
3100 spdif
= snd_array_elem(&codec
->spdif_out
, idx
);
3101 ucontrol
->value
.iec958
.status
[0] = spdif
->status
& 0xff;
3102 ucontrol
->value
.iec958
.status
[1] = (spdif
->status
>> 8) & 0xff;
3103 ucontrol
->value
.iec958
.status
[2] = (spdif
->status
>> 16) & 0xff;
3104 ucontrol
->value
.iec958
.status
[3] = (spdif
->status
>> 24) & 0xff;
3105 mutex_unlock(&codec
->spdif_mutex
);
3110 /* convert from SPDIF status bits to HDA SPDIF bits
3111 * bit 0 (DigEn) is always set zero (to be filled later)
3113 static unsigned short convert_from_spdif_status(unsigned int sbits
)
3115 unsigned short val
= 0;
3117 if (sbits
& IEC958_AES0_PROFESSIONAL
)
3118 val
|= AC_DIG1_PROFESSIONAL
;
3119 if (sbits
& IEC958_AES0_NONAUDIO
)
3120 val
|= AC_DIG1_NONAUDIO
;
3121 if (sbits
& IEC958_AES0_PROFESSIONAL
) {
3122 if ((sbits
& IEC958_AES0_PRO_EMPHASIS
) ==
3123 IEC958_AES0_PRO_EMPHASIS_5015
)
3124 val
|= AC_DIG1_EMPHASIS
;
3126 if ((sbits
& IEC958_AES0_CON_EMPHASIS
) ==
3127 IEC958_AES0_CON_EMPHASIS_5015
)
3128 val
|= AC_DIG1_EMPHASIS
;
3129 if (!(sbits
& IEC958_AES0_CON_NOT_COPYRIGHT
))
3130 val
|= AC_DIG1_COPYRIGHT
;
3131 if (sbits
& (IEC958_AES1_CON_ORIGINAL
<< 8))
3132 val
|= AC_DIG1_LEVEL
;
3133 val
|= sbits
& (IEC958_AES1_CON_CATEGORY
<< 8);
3138 /* convert to SPDIF status bits from HDA SPDIF bits
3140 static unsigned int convert_to_spdif_status(unsigned short val
)
3142 unsigned int sbits
= 0;
3144 if (val
& AC_DIG1_NONAUDIO
)
3145 sbits
|= IEC958_AES0_NONAUDIO
;
3146 if (val
& AC_DIG1_PROFESSIONAL
)
3147 sbits
|= IEC958_AES0_PROFESSIONAL
;
3148 if (sbits
& IEC958_AES0_PROFESSIONAL
) {
3149 if (val
& AC_DIG1_EMPHASIS
)
3150 sbits
|= IEC958_AES0_PRO_EMPHASIS_5015
;
3152 if (val
& AC_DIG1_EMPHASIS
)
3153 sbits
|= IEC958_AES0_CON_EMPHASIS_5015
;
3154 if (!(val
& AC_DIG1_COPYRIGHT
))
3155 sbits
|= IEC958_AES0_CON_NOT_COPYRIGHT
;
3156 if (val
& AC_DIG1_LEVEL
)
3157 sbits
|= (IEC958_AES1_CON_ORIGINAL
<< 8);
3158 sbits
|= val
& (0x7f << 8);
3163 /* set digital convert verbs both for the given NID and its slaves */
3164 static void set_dig_out(struct hda_codec
*codec
, hda_nid_t nid
,
3169 snd_hda_codec_write_cache(codec
, nid
, 0, verb
, val
);
3170 d
= codec
->slave_dig_outs
;
3174 snd_hda_codec_write_cache(codec
, *d
, 0, verb
, val
);
3177 static inline void set_dig_out_convert(struct hda_codec
*codec
, hda_nid_t nid
,
3181 set_dig_out(codec
, nid
, AC_VERB_SET_DIGI_CONVERT_1
, dig1
);
3183 set_dig_out(codec
, nid
, AC_VERB_SET_DIGI_CONVERT_2
, dig2
);
3186 static int snd_hda_spdif_default_put(struct snd_kcontrol
*kcontrol
,
3187 struct snd_ctl_elem_value
*ucontrol
)
3189 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
3190 int idx
= kcontrol
->private_value
;
3191 struct hda_spdif_out
*spdif
;
3196 mutex_lock(&codec
->spdif_mutex
);
3197 spdif
= snd_array_elem(&codec
->spdif_out
, idx
);
3199 spdif
->status
= ucontrol
->value
.iec958
.status
[0] |
3200 ((unsigned int)ucontrol
->value
.iec958
.status
[1] << 8) |
3201 ((unsigned int)ucontrol
->value
.iec958
.status
[2] << 16) |
3202 ((unsigned int)ucontrol
->value
.iec958
.status
[3] << 24);
3203 val
= convert_from_spdif_status(spdif
->status
);
3204 val
|= spdif
->ctls
& 1;
3205 change
= spdif
->ctls
!= val
;
3207 if (change
&& nid
!= (u16
)-1)
3208 set_dig_out_convert(codec
, nid
, val
& 0xff, (val
>> 8) & 0xff);
3209 mutex_unlock(&codec
->spdif_mutex
);
3213 #define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
3215 static int snd_hda_spdif_out_switch_get(struct snd_kcontrol
*kcontrol
,
3216 struct snd_ctl_elem_value
*ucontrol
)
3218 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
3219 int idx
= kcontrol
->private_value
;
3220 struct hda_spdif_out
*spdif
;
3222 mutex_lock(&codec
->spdif_mutex
);
3223 spdif
= snd_array_elem(&codec
->spdif_out
, idx
);
3224 ucontrol
->value
.integer
.value
[0] = spdif
->ctls
& AC_DIG1_ENABLE
;
3225 mutex_unlock(&codec
->spdif_mutex
);
3229 static inline void set_spdif_ctls(struct hda_codec
*codec
, hda_nid_t nid
,
3232 set_dig_out_convert(codec
, nid
, dig1
, dig2
);
3233 /* unmute amp switch (if any) */
3234 if ((get_wcaps(codec
, nid
) & AC_WCAP_OUT_AMP
) &&
3235 (dig1
& AC_DIG1_ENABLE
))
3236 snd_hda_codec_amp_stereo(codec
, nid
, HDA_OUTPUT
, 0,
3240 static int snd_hda_spdif_out_switch_put(struct snd_kcontrol
*kcontrol
,
3241 struct snd_ctl_elem_value
*ucontrol
)
3243 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
3244 int idx
= kcontrol
->private_value
;
3245 struct hda_spdif_out
*spdif
;
3250 mutex_lock(&codec
->spdif_mutex
);
3251 spdif
= snd_array_elem(&codec
->spdif_out
, idx
);
3253 val
= spdif
->ctls
& ~AC_DIG1_ENABLE
;
3254 if (ucontrol
->value
.integer
.value
[0])
3255 val
|= AC_DIG1_ENABLE
;
3256 change
= spdif
->ctls
!= val
;
3258 if (change
&& nid
!= (u16
)-1)
3259 set_spdif_ctls(codec
, nid
, val
& 0xff, -1);
3260 mutex_unlock(&codec
->spdif_mutex
);
3264 static struct snd_kcontrol_new dig_mixes
[] = {
3266 .access
= SNDRV_CTL_ELEM_ACCESS_READ
,
3267 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
3268 .name
= SNDRV_CTL_NAME_IEC958("", PLAYBACK
, CON_MASK
),
3269 .info
= snd_hda_spdif_mask_info
,
3270 .get
= snd_hda_spdif_cmask_get
,
3273 .access
= SNDRV_CTL_ELEM_ACCESS_READ
,
3274 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
3275 .name
= SNDRV_CTL_NAME_IEC958("", PLAYBACK
, PRO_MASK
),
3276 .info
= snd_hda_spdif_mask_info
,
3277 .get
= snd_hda_spdif_pmask_get
,
3280 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
3281 .name
= SNDRV_CTL_NAME_IEC958("", PLAYBACK
, DEFAULT
),
3282 .info
= snd_hda_spdif_mask_info
,
3283 .get
= snd_hda_spdif_default_get
,
3284 .put
= snd_hda_spdif_default_put
,
3287 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
3288 .name
= SNDRV_CTL_NAME_IEC958("", PLAYBACK
, SWITCH
),
3289 .info
= snd_hda_spdif_out_switch_info
,
3290 .get
= snd_hda_spdif_out_switch_get
,
3291 .put
= snd_hda_spdif_out_switch_put
,
3297 * snd_hda_create_dig_out_ctls - create Output SPDIF-related controls
3298 * @codec: the HDA codec
3299 * @associated_nid: NID that new ctls associated with
3300 * @cvt_nid: converter NID
3301 * @type: HDA_PCM_TYPE_*
3302 * Creates controls related with the digital output.
3303 * Called from each patch supporting the digital out.
3305 * Returns 0 if successful, or a negative error code.
3307 int snd_hda_create_dig_out_ctls(struct hda_codec
*codec
,
3308 hda_nid_t associated_nid
,
3313 struct snd_kcontrol
*kctl
;
3314 struct snd_kcontrol_new
*dig_mix
;
3316 const int spdif_index
= 16;
3317 struct hda_spdif_out
*spdif
;
3318 struct hda_bus
*bus
= codec
->bus
;
3320 if (bus
->primary_dig_out_type
== HDA_PCM_TYPE_HDMI
&&
3321 type
== HDA_PCM_TYPE_SPDIF
) {
3323 } else if (bus
->primary_dig_out_type
== HDA_PCM_TYPE_SPDIF
&&
3324 type
== HDA_PCM_TYPE_HDMI
) {
3325 /* suppose a single SPDIF device */
3326 for (dig_mix
= dig_mixes
; dig_mix
->name
; dig_mix
++) {
3327 kctl
= find_mixer_ctl(codec
, dig_mix
->name
, 0, 0);
3330 kctl
->id
.index
= spdif_index
;
3332 bus
->primary_dig_out_type
= HDA_PCM_TYPE_HDMI
;
3334 if (!bus
->primary_dig_out_type
)
3335 bus
->primary_dig_out_type
= type
;
3337 idx
= find_empty_mixer_ctl_idx(codec
, "IEC958 Playback Switch", idx
);
3339 codec_err(codec
, "too many IEC958 outputs\n");
3342 spdif
= snd_array_new(&codec
->spdif_out
);
3345 for (dig_mix
= dig_mixes
; dig_mix
->name
; dig_mix
++) {
3346 kctl
= snd_ctl_new1(dig_mix
, codec
);
3349 kctl
->id
.index
= idx
;
3350 kctl
->private_value
= codec
->spdif_out
.used
- 1;
3351 err
= snd_hda_ctl_add(codec
, associated_nid
, kctl
);
3355 spdif
->nid
= cvt_nid
;
3356 spdif
->ctls
= snd_hda_codec_read(codec
, cvt_nid
, 0,
3357 AC_VERB_GET_DIGI_CONVERT_1
, 0);
3358 spdif
->status
= convert_to_spdif_status(spdif
->ctls
);
3361 EXPORT_SYMBOL_GPL(snd_hda_create_dig_out_ctls
);
3364 * snd_hda_spdif_out_of_nid - get the hda_spdif_out entry from the given NID
3365 * @codec: the HDA codec
3368 * call within spdif_mutex lock
3370 struct hda_spdif_out
*snd_hda_spdif_out_of_nid(struct hda_codec
*codec
,
3374 for (i
= 0; i
< codec
->spdif_out
.used
; i
++) {
3375 struct hda_spdif_out
*spdif
=
3376 snd_array_elem(&codec
->spdif_out
, i
);
3377 if (spdif
->nid
== nid
)
3382 EXPORT_SYMBOL_GPL(snd_hda_spdif_out_of_nid
);
3385 * snd_hda_spdif_ctls_unassign - Unassign the given SPDIF ctl
3386 * @codec: the HDA codec
3387 * @idx: the SPDIF ctl index
3389 * Unassign the widget from the given SPDIF control.
3391 void snd_hda_spdif_ctls_unassign(struct hda_codec
*codec
, int idx
)
3393 struct hda_spdif_out
*spdif
;
3395 mutex_lock(&codec
->spdif_mutex
);
3396 spdif
= snd_array_elem(&codec
->spdif_out
, idx
);
3397 spdif
->nid
= (u16
)-1;
3398 mutex_unlock(&codec
->spdif_mutex
);
3400 EXPORT_SYMBOL_GPL(snd_hda_spdif_ctls_unassign
);
3403 * snd_hda_spdif_ctls_assign - Assign the SPDIF controls to the given NID
3404 * @codec: the HDA codec
3405 * @idx: the SPDIF ctl idx
3408 * Assign the widget to the SPDIF control with the given index.
3410 void snd_hda_spdif_ctls_assign(struct hda_codec
*codec
, int idx
, hda_nid_t nid
)
3412 struct hda_spdif_out
*spdif
;
3415 mutex_lock(&codec
->spdif_mutex
);
3416 spdif
= snd_array_elem(&codec
->spdif_out
, idx
);
3417 if (spdif
->nid
!= nid
) {
3420 set_spdif_ctls(codec
, nid
, val
& 0xff, (val
>> 8) & 0xff);
3422 mutex_unlock(&codec
->spdif_mutex
);
3424 EXPORT_SYMBOL_GPL(snd_hda_spdif_ctls_assign
);
3427 * SPDIF sharing with analog output
3429 static int spdif_share_sw_get(struct snd_kcontrol
*kcontrol
,
3430 struct snd_ctl_elem_value
*ucontrol
)
3432 struct hda_multi_out
*mout
= snd_kcontrol_chip(kcontrol
);
3433 ucontrol
->value
.integer
.value
[0] = mout
->share_spdif
;
3437 static int spdif_share_sw_put(struct snd_kcontrol
*kcontrol
,
3438 struct snd_ctl_elem_value
*ucontrol
)
3440 struct hda_multi_out
*mout
= snd_kcontrol_chip(kcontrol
);
3441 mout
->share_spdif
= !!ucontrol
->value
.integer
.value
[0];
3445 static struct snd_kcontrol_new spdif_share_sw
= {
3446 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
3447 .name
= "IEC958 Default PCM Playback Switch",
3448 .info
= snd_ctl_boolean_mono_info
,
3449 .get
= spdif_share_sw_get
,
3450 .put
= spdif_share_sw_put
,
3454 * snd_hda_create_spdif_share_sw - create Default PCM switch
3455 * @codec: the HDA codec
3456 * @mout: multi-out instance
3458 int snd_hda_create_spdif_share_sw(struct hda_codec
*codec
,
3459 struct hda_multi_out
*mout
)
3461 struct snd_kcontrol
*kctl
;
3463 if (!mout
->dig_out_nid
)
3466 kctl
= snd_ctl_new1(&spdif_share_sw
, mout
);
3469 /* ATTENTION: here mout is passed as private_data, instead of codec */
3470 return snd_hda_ctl_add(codec
, mout
->dig_out_nid
, kctl
);
3472 EXPORT_SYMBOL_GPL(snd_hda_create_spdif_share_sw
);
3478 #define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
3480 static int snd_hda_spdif_in_switch_get(struct snd_kcontrol
*kcontrol
,
3481 struct snd_ctl_elem_value
*ucontrol
)
3483 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
3485 ucontrol
->value
.integer
.value
[0] = codec
->spdif_in_enable
;
3489 static int snd_hda_spdif_in_switch_put(struct snd_kcontrol
*kcontrol
,
3490 struct snd_ctl_elem_value
*ucontrol
)
3492 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
3493 hda_nid_t nid
= kcontrol
->private_value
;
3494 unsigned int val
= !!ucontrol
->value
.integer
.value
[0];
3497 mutex_lock(&codec
->spdif_mutex
);
3498 change
= codec
->spdif_in_enable
!= val
;
3500 codec
->spdif_in_enable
= val
;
3501 snd_hda_codec_write_cache(codec
, nid
, 0,
3502 AC_VERB_SET_DIGI_CONVERT_1
, val
);
3504 mutex_unlock(&codec
->spdif_mutex
);
3508 static int snd_hda_spdif_in_status_get(struct snd_kcontrol
*kcontrol
,
3509 struct snd_ctl_elem_value
*ucontrol
)
3511 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
3512 hda_nid_t nid
= kcontrol
->private_value
;
3516 val
= snd_hda_codec_read(codec
, nid
, 0, AC_VERB_GET_DIGI_CONVERT_1
, 0);
3517 sbits
= convert_to_spdif_status(val
);
3518 ucontrol
->value
.iec958
.status
[0] = sbits
;
3519 ucontrol
->value
.iec958
.status
[1] = sbits
>> 8;
3520 ucontrol
->value
.iec958
.status
[2] = sbits
>> 16;
3521 ucontrol
->value
.iec958
.status
[3] = sbits
>> 24;
3525 static struct snd_kcontrol_new dig_in_ctls
[] = {
3527 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
3528 .name
= SNDRV_CTL_NAME_IEC958("", CAPTURE
, SWITCH
),
3529 .info
= snd_hda_spdif_in_switch_info
,
3530 .get
= snd_hda_spdif_in_switch_get
,
3531 .put
= snd_hda_spdif_in_switch_put
,
3534 .access
= SNDRV_CTL_ELEM_ACCESS_READ
,
3535 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
3536 .name
= SNDRV_CTL_NAME_IEC958("", CAPTURE
, DEFAULT
),
3537 .info
= snd_hda_spdif_mask_info
,
3538 .get
= snd_hda_spdif_in_status_get
,
3544 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
3545 * @codec: the HDA codec
3546 * @nid: audio in widget NID
3548 * Creates controls related with the SPDIF input.
3549 * Called from each patch supporting the SPDIF in.
3551 * Returns 0 if successful, or a negative error code.
3553 int snd_hda_create_spdif_in_ctls(struct hda_codec
*codec
, hda_nid_t nid
)
3556 struct snd_kcontrol
*kctl
;
3557 struct snd_kcontrol_new
*dig_mix
;
3560 idx
= find_empty_mixer_ctl_idx(codec
, "IEC958 Capture Switch", 0);
3562 codec_err(codec
, "too many IEC958 inputs\n");
3565 for (dig_mix
= dig_in_ctls
; dig_mix
->name
; dig_mix
++) {
3566 kctl
= snd_ctl_new1(dig_mix
, codec
);
3569 kctl
->private_value
= nid
;
3570 err
= snd_hda_ctl_add(codec
, nid
, kctl
);
3574 codec
->spdif_in_enable
=
3575 snd_hda_codec_read(codec
, nid
, 0,
3576 AC_VERB_GET_DIGI_CONVERT_1
, 0) &
3580 EXPORT_SYMBOL_GPL(snd_hda_create_spdif_in_ctls
);
3586 /* build a 31bit cache key with the widget id and the command parameter */
3587 #define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
3588 #define get_cmd_cache_nid(key) ((key) & 0xff)
3589 #define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
3592 * snd_hda_codec_write_cache - send a single command with caching
3593 * @codec: the HDA codec
3594 * @nid: NID to send the command
3595 * @flags: optional bit flags
3596 * @verb: the verb to send
3597 * @parm: the parameter for the verb
3599 * Send a single command without waiting for response.
3601 * Returns 0 if successful, or a negative error code.
3603 int snd_hda_codec_write_cache(struct hda_codec
*codec
, hda_nid_t nid
,
3604 int flags
, unsigned int verb
, unsigned int parm
)
3607 struct hda_cache_head
*c
;
3609 unsigned int cache_only
;
3611 cache_only
= codec
->cached_write
;
3613 err
= snd_hda_codec_write(codec
, nid
, flags
, verb
, parm
);
3618 /* parm may contain the verb stuff for get/set amp */
3619 verb
= verb
| (parm
>> 8);
3621 key
= build_cmd_cache_key(nid
, verb
);
3622 mutex_lock(&codec
->bus
->cmd_mutex
);
3623 c
= get_alloc_hash(&codec
->cmd_cache
, key
);
3626 c
->dirty
= cache_only
;
3628 mutex_unlock(&codec
->bus
->cmd_mutex
);
3631 EXPORT_SYMBOL_GPL(snd_hda_codec_write_cache
);
3634 * snd_hda_codec_update_cache - check cache and write the cmd only when needed
3635 * @codec: the HDA codec
3636 * @nid: NID to send the command
3637 * @flags: optional bit flags
3638 * @verb: the verb to send
3639 * @parm: the parameter for the verb
3641 * This function works like snd_hda_codec_write_cache(), but it doesn't send
3642 * command if the parameter is already identical with the cached value.
3643 * If not, it sends the command and refreshes the cache.
3645 * Returns 0 if successful, or a negative error code.
3647 int snd_hda_codec_update_cache(struct hda_codec
*codec
, hda_nid_t nid
,
3648 int flags
, unsigned int verb
, unsigned int parm
)
3650 struct hda_cache_head
*c
;
3653 /* parm may contain the verb stuff for get/set amp */
3654 verb
= verb
| (parm
>> 8);
3656 key
= build_cmd_cache_key(nid
, verb
);
3657 mutex_lock(&codec
->bus
->cmd_mutex
);
3658 c
= get_hash(&codec
->cmd_cache
, key
);
3659 if (c
&& c
->val
== parm
) {
3660 mutex_unlock(&codec
->bus
->cmd_mutex
);
3663 mutex_unlock(&codec
->bus
->cmd_mutex
);
3664 return snd_hda_codec_write_cache(codec
, nid
, flags
, verb
, parm
);
3666 EXPORT_SYMBOL_GPL(snd_hda_codec_update_cache
);
3669 * snd_hda_codec_resume_cache - Resume the all commands from the cache
3670 * @codec: HD-audio codec
3672 * Execute all verbs recorded in the command caches to resume.
3674 void snd_hda_codec_resume_cache(struct hda_codec
*codec
)
3678 mutex_lock(&codec
->hash_mutex
);
3679 codec
->cached_write
= 0;
3680 for (i
= 0; i
< codec
->cmd_cache
.buf
.used
; i
++) {
3681 struct hda_cache_head
*buffer
;
3684 buffer
= snd_array_elem(&codec
->cmd_cache
.buf
, i
);
3691 mutex_unlock(&codec
->hash_mutex
);
3692 snd_hda_codec_write(codec
, get_cmd_cache_nid(key
), 0,
3693 get_cmd_cache_cmd(key
), buffer
->val
);
3694 mutex_lock(&codec
->hash_mutex
);
3696 mutex_unlock(&codec
->hash_mutex
);
3698 EXPORT_SYMBOL_GPL(snd_hda_codec_resume_cache
);
3701 * snd_hda_sequence_write_cache - sequence writes with caching
3702 * @codec: the HDA codec
3703 * @seq: VERB array to send
3705 * Send the commands sequentially from the given array.
3706 * Thte commands are recorded on cache for power-save and resume.
3707 * The array must be terminated with NID=0.
3709 void snd_hda_sequence_write_cache(struct hda_codec
*codec
,
3710 const struct hda_verb
*seq
)
3712 for (; seq
->nid
; seq
++)
3713 snd_hda_codec_write_cache(codec
, seq
->nid
, 0, seq
->verb
,
3716 EXPORT_SYMBOL_GPL(snd_hda_sequence_write_cache
);
3719 * snd_hda_codec_flush_cache - Execute all pending (cached) amps / verbs
3720 * @codec: HD-audio codec
3722 void snd_hda_codec_flush_cache(struct hda_codec
*codec
)
3724 snd_hda_codec_resume_amp(codec
);
3725 snd_hda_codec_resume_cache(codec
);
3727 EXPORT_SYMBOL_GPL(snd_hda_codec_flush_cache
);
3730 * snd_hda_codec_set_power_to_all - Set the power state to all widgets
3731 * @codec: the HDA codec
3732 * @fg: function group (not used now)
3733 * @power_state: the power state to set (AC_PWRST_*)
3735 * Set the given power state to all widgets that have the power control.
3736 * If the codec has power_filter set, it evaluates the power state and
3737 * filter out if it's unchanged as D3.
3739 void snd_hda_codec_set_power_to_all(struct hda_codec
*codec
, hda_nid_t fg
,
3740 unsigned int power_state
)
3742 hda_nid_t nid
= codec
->start_nid
;
3745 for (i
= 0; i
< codec
->num_nodes
; i
++, nid
++) {
3746 unsigned int wcaps
= get_wcaps(codec
, nid
);
3747 unsigned int state
= power_state
;
3748 if (!(wcaps
& AC_WCAP_POWER
))
3750 if (codec
->power_filter
) {
3751 state
= codec
->power_filter(codec
, nid
, power_state
);
3752 if (state
!= power_state
&& power_state
== AC_PWRST_D3
)
3755 snd_hda_codec_write(codec
, nid
, 0, AC_VERB_SET_POWER_STATE
,
3759 EXPORT_SYMBOL_GPL(snd_hda_codec_set_power_to_all
);
3762 * supported power states check
3764 static bool snd_hda_codec_get_supported_ps(struct hda_codec
*codec
, hda_nid_t fg
,
3765 unsigned int power_state
)
3767 int sup
= snd_hda_param_read(codec
, fg
, AC_PAR_POWER_STATE
);
3771 if (sup
& power_state
)
3778 * wait until the state is reached, returns the current state
3780 static unsigned int hda_sync_power_state(struct hda_codec
*codec
,
3782 unsigned int power_state
)
3784 unsigned long end_time
= jiffies
+ msecs_to_jiffies(500);
3785 unsigned int state
, actual_state
;
3788 state
= snd_hda_codec_read(codec
, fg
, 0,
3789 AC_VERB_GET_POWER_STATE
, 0);
3790 if (state
& AC_PWRST_ERROR
)
3792 actual_state
= (state
>> 4) & 0x0f;
3793 if (actual_state
== power_state
)
3795 if (time_after_eq(jiffies
, end_time
))
3797 /* wait until the codec reachs to the target state */
3804 * snd_hda_codec_eapd_power_filter - A power filter callback for EAPD
3805 * @codec: the HDA codec
3807 * @power_state: power state to evalue
3809 * Don't power down the widget if it controls eapd and EAPD_BTLENABLE is set.
3810 * This can be used a codec power_filter callback.
3812 unsigned int snd_hda_codec_eapd_power_filter(struct hda_codec
*codec
,
3814 unsigned int power_state
)
3816 if (nid
== codec
->afg
|| nid
== codec
->mfg
)
3818 if (power_state
== AC_PWRST_D3
&&
3819 get_wcaps_type(get_wcaps(codec
, nid
)) == AC_WID_PIN
&&
3820 (snd_hda_query_pin_caps(codec
, nid
) & AC_PINCAP_EAPD
)) {
3821 int eapd
= snd_hda_codec_read(codec
, nid
, 0,
3822 AC_VERB_GET_EAPD_BTLENABLE
, 0);
3828 EXPORT_SYMBOL_GPL(snd_hda_codec_eapd_power_filter
);
3831 * set power state of the codec, and return the power state
3833 static unsigned int hda_set_power_state(struct hda_codec
*codec
,
3834 unsigned int power_state
)
3836 hda_nid_t fg
= codec
->afg
? codec
->afg
: codec
->mfg
;
3841 /* this delay seems necessary to avoid click noise at power-down */
3842 if (power_state
== AC_PWRST_D3
) {
3843 if (codec
->depop_delay
< 0)
3844 msleep(codec
->epss
? 10 : 100);
3845 else if (codec
->depop_delay
> 0)
3846 msleep(codec
->depop_delay
);
3847 flags
= HDA_RW_NO_RESPONSE_FALLBACK
;
3850 /* repeat power states setting at most 10 times*/
3851 for (count
= 0; count
< 10; count
++) {
3852 if (codec
->patch_ops
.set_power_state
)
3853 codec
->patch_ops
.set_power_state(codec
, fg
,
3856 state
= power_state
;
3857 if (codec
->power_filter
)
3858 state
= codec
->power_filter(codec
, fg
, state
);
3859 if (state
== power_state
|| power_state
!= AC_PWRST_D3
)
3860 snd_hda_codec_read(codec
, fg
, flags
,
3861 AC_VERB_SET_POWER_STATE
,
3863 snd_hda_codec_set_power_to_all(codec
, fg
, power_state
);
3865 state
= hda_sync_power_state(codec
, fg
, power_state
);
3866 if (!(state
& AC_PWRST_ERROR
))
3873 /* sync power states of all widgets;
3874 * this is called at the end of codec parsing
3876 static void sync_power_up_states(struct hda_codec
*codec
)
3878 hda_nid_t nid
= codec
->start_nid
;
3881 /* don't care if no filter is used */
3882 if (!codec
->power_filter
)
3885 for (i
= 0; i
< codec
->num_nodes
; i
++, nid
++) {
3886 unsigned int wcaps
= get_wcaps(codec
, nid
);
3887 unsigned int target
;
3888 if (!(wcaps
& AC_WCAP_POWER
))
3890 target
= codec
->power_filter(codec
, nid
, AC_PWRST_D0
);
3891 if (target
== AC_PWRST_D0
)
3893 if (!snd_hda_check_power_state(codec
, nid
, target
))
3894 snd_hda_codec_write(codec
, nid
, 0,
3895 AC_VERB_SET_POWER_STATE
, target
);
3899 #ifdef CONFIG_SND_HDA_RECONFIG
3900 /* execute additional init verbs */
3901 static void hda_exec_init_verbs(struct hda_codec
*codec
)
3903 if (codec
->init_verbs
.list
)
3904 snd_hda_sequence_write(codec
, codec
->init_verbs
.list
);
3907 static inline void hda_exec_init_verbs(struct hda_codec
*codec
) {}
3911 /* update the power on/off account with the current jiffies */
3912 static void update_power_acct(struct hda_codec
*codec
, bool on
)
3914 unsigned long delta
= jiffies
- codec
->power_jiffies
;
3917 codec
->power_on_acct
+= delta
;
3919 codec
->power_off_acct
+= delta
;
3920 codec
->power_jiffies
+= delta
;
3923 void snd_hda_update_power_acct(struct hda_codec
*codec
)
3925 update_power_acct(codec
, hda_codec_is_power_on(codec
));
3929 * call suspend and power-down; used both from PM and power-save
3930 * this function returns the power state in the end
3932 static unsigned int hda_call_codec_suspend(struct hda_codec
*codec
)
3936 atomic_inc(&codec
->in_pm
);
3938 if (codec
->patch_ops
.suspend
)
3939 codec
->patch_ops
.suspend(codec
);
3940 hda_cleanup_all_streams(codec
);
3941 state
= hda_set_power_state(codec
, AC_PWRST_D3
);
3942 trace_hda_power_down(codec
);
3943 update_power_acct(codec
, true);
3944 atomic_dec(&codec
->in_pm
);
3948 /* mark all entries of cmd and amp caches dirty */
3949 static void hda_mark_cmd_cache_dirty(struct hda_codec
*codec
)
3952 for (i
= 0; i
< codec
->cmd_cache
.buf
.used
; i
++) {
3953 struct hda_cache_head
*cmd
;
3954 cmd
= snd_array_elem(&codec
->cmd_cache
.buf
, i
);
3957 for (i
= 0; i
< codec
->amp_cache
.buf
.used
; i
++) {
3958 struct hda_amp_info
*amp
;
3959 amp
= snd_array_elem(&codec
->amp_cache
.buf
, i
);
3960 amp
->head
.dirty
= 1;
3965 * kick up codec; used both from PM and power-save
3967 static void hda_call_codec_resume(struct hda_codec
*codec
)
3969 atomic_inc(&codec
->in_pm
);
3971 trace_hda_power_up(codec
);
3972 hda_mark_cmd_cache_dirty(codec
);
3974 codec
->power_jiffies
= jiffies
;
3976 hda_set_power_state(codec
, AC_PWRST_D0
);
3977 restore_shutup_pins(codec
);
3978 hda_exec_init_verbs(codec
);
3979 snd_hda_jack_set_dirty_all(codec
);
3980 if (codec
->patch_ops
.resume
)
3981 codec
->patch_ops
.resume(codec
);
3983 if (codec
->patch_ops
.init
)
3984 codec
->patch_ops
.init(codec
);
3985 snd_hda_codec_resume_amp(codec
);
3986 snd_hda_codec_resume_cache(codec
);
3989 if (codec
->jackpoll_interval
)
3990 hda_jackpoll_work(&codec
->jackpoll_work
.work
);
3992 snd_hda_jack_report_sync(codec
);
3993 atomic_dec(&codec
->in_pm
);
3996 static int hda_codec_runtime_suspend(struct device
*dev
)
3998 struct hda_codec
*codec
= dev_to_hda_codec(dev
);
3999 struct hda_pcm
*pcm
;
4002 cancel_delayed_work_sync(&codec
->jackpoll_work
);
4003 list_for_each_entry(pcm
, &codec
->pcm_list_head
, list
)
4004 snd_pcm_suspend_all(pcm
->pcm
);
4005 state
= hda_call_codec_suspend(codec
);
4006 if (codec
->d3_stop_clk
&& codec
->epss
&& (state
& AC_PWRST_CLK_STOP_OK
))
4007 clear_bit(codec
->addr
, &codec
->bus
->codec_powered
);
4011 static int hda_codec_runtime_resume(struct device
*dev
)
4013 struct hda_codec
*codec
= dev_to_hda_codec(dev
);
4015 set_bit(codec
->addr
, &codec
->bus
->codec_powered
);
4016 hda_call_codec_resume(codec
);
4017 pm_runtime_mark_last_busy(dev
);
4020 #endif /* CONFIG_PM */
4022 /* referred in hda_bind.c */
4023 const struct dev_pm_ops hda_codec_driver_pm
= {
4024 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend
,
4025 pm_runtime_force_resume
)
4026 SET_RUNTIME_PM_OPS(hda_codec_runtime_suspend
, hda_codec_runtime_resume
,
4031 * snd_hda_build_controls - build mixer controls
4034 * Creates mixer controls for each codec included in the bus.
4036 * Returns 0 if successful, otherwise a negative error code.
4038 int snd_hda_build_controls(struct hda_bus
*bus
)
4040 struct hda_codec
*codec
;
4042 list_for_each_entry(codec
, &bus
->codec_list
, list
) {
4043 int err
= snd_hda_codec_build_controls(codec
);
4046 "cannot build controls for #%d (error %d)\n",
4048 err
= snd_hda_codec_reset(codec
);
4051 "cannot revert codec\n");
4058 EXPORT_SYMBOL_GPL(snd_hda_build_controls
);
4061 * add standard channel maps if not specified
4063 static int add_std_chmaps(struct hda_codec
*codec
)
4065 struct hda_pcm
*pcm
;
4068 list_for_each_entry(pcm
, &codec
->pcm_list_head
, list
) {
4069 for (str
= 0; str
< 2; str
++) {
4070 struct hda_pcm_stream
*hinfo
= &pcm
->stream
[str
];
4071 struct snd_pcm_chmap
*chmap
;
4072 const struct snd_pcm_chmap_elem
*elem
;
4076 if (!pcm
|| !hinfo
->substreams
)
4078 elem
= hinfo
->chmap
? hinfo
->chmap
: snd_pcm_std_chmaps
;
4079 err
= snd_pcm_add_chmap_ctls(pcm
->pcm
, str
, elem
,
4080 hinfo
->channels_max
,
4084 chmap
->channel_mask
= SND_PCM_CHMAP_MASK_2468
;
4090 /* default channel maps for 2.1 speakers;
4091 * since HD-audio supports only stereo, odd number channels are omitted
4093 const struct snd_pcm_chmap_elem snd_pcm_2_1_chmaps
[] = {
4095 .map
= { SNDRV_CHMAP_FL
, SNDRV_CHMAP_FR
} },
4097 .map
= { SNDRV_CHMAP_FL
, SNDRV_CHMAP_FR
,
4098 SNDRV_CHMAP_LFE
, SNDRV_CHMAP_LFE
} },
4101 EXPORT_SYMBOL_GPL(snd_pcm_2_1_chmaps
);
4103 int snd_hda_codec_build_controls(struct hda_codec
*codec
)
4106 hda_exec_init_verbs(codec
);
4107 /* continue to initialize... */
4108 if (codec
->patch_ops
.init
)
4109 err
= codec
->patch_ops
.init(codec
);
4110 if (!err
&& codec
->patch_ops
.build_controls
)
4111 err
= codec
->patch_ops
.build_controls(codec
);
4115 /* we create chmaps here instead of build_pcms */
4116 err
= add_std_chmaps(codec
);
4120 if (codec
->jackpoll_interval
)
4121 hda_jackpoll_work(&codec
->jackpoll_work
.work
);
4123 snd_hda_jack_report_sync(codec
); /* call at the last init point */
4124 sync_power_up_states(codec
);
4131 struct hda_rate_tbl
{
4133 unsigned int alsa_bits
;
4134 unsigned int hda_fmt
;
4137 /* rate = base * mult / div */
4138 #define HDA_RATE(base, mult, div) \
4139 (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \
4140 (((div) - 1) << AC_FMT_DIV_SHIFT))
4142 static struct hda_rate_tbl rate_bits
[] = {
4143 /* rate in Hz, ALSA rate bitmask, HDA format value */
4145 /* autodetected value used in snd_hda_query_supported_pcm */
4146 { 8000, SNDRV_PCM_RATE_8000
, HDA_RATE(48, 1, 6) },
4147 { 11025, SNDRV_PCM_RATE_11025
, HDA_RATE(44, 1, 4) },
4148 { 16000, SNDRV_PCM_RATE_16000
, HDA_RATE(48, 1, 3) },
4149 { 22050, SNDRV_PCM_RATE_22050
, HDA_RATE(44, 1, 2) },
4150 { 32000, SNDRV_PCM_RATE_32000
, HDA_RATE(48, 2, 3) },
4151 { 44100, SNDRV_PCM_RATE_44100
, HDA_RATE(44, 1, 1) },
4152 { 48000, SNDRV_PCM_RATE_48000
, HDA_RATE(48, 1, 1) },
4153 { 88200, SNDRV_PCM_RATE_88200
, HDA_RATE(44, 2, 1) },
4154 { 96000, SNDRV_PCM_RATE_96000
, HDA_RATE(48, 2, 1) },
4155 { 176400, SNDRV_PCM_RATE_176400
, HDA_RATE(44, 4, 1) },
4156 { 192000, SNDRV_PCM_RATE_192000
, HDA_RATE(48, 4, 1) },
4157 #define AC_PAR_PCM_RATE_BITS 11
4158 /* up to bits 10, 384kHZ isn't supported properly */
4160 /* not autodetected value */
4161 { 9600, SNDRV_PCM_RATE_KNOT
, HDA_RATE(48, 1, 5) },
4163 { 0 } /* terminator */
4167 * snd_hda_calc_stream_format - calculate format bitset
4168 * @codec: HD-audio codec
4169 * @rate: the sample rate
4170 * @channels: the number of channels
4171 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
4172 * @maxbps: the max. bps
4173 * @spdif_ctls: HD-audio SPDIF status bits (0 if irrelevant)
4175 * Calculate the format bitset from the given rate, channels and th PCM format.
4177 * Return zero if invalid.
4179 unsigned int snd_hda_calc_stream_format(struct hda_codec
*codec
,
4181 unsigned int channels
,
4182 unsigned int format
,
4183 unsigned int maxbps
,
4184 unsigned short spdif_ctls
)
4187 unsigned int val
= 0;
4189 for (i
= 0; rate_bits
[i
].hz
; i
++)
4190 if (rate_bits
[i
].hz
== rate
) {
4191 val
= rate_bits
[i
].hda_fmt
;
4194 if (!rate_bits
[i
].hz
) {
4195 codec_dbg(codec
, "invalid rate %d\n", rate
);
4199 if (channels
== 0 || channels
> 8) {
4200 codec_dbg(codec
, "invalid channels %d\n", channels
);
4203 val
|= channels
- 1;
4205 switch (snd_pcm_format_width(format
)) {
4207 val
|= AC_FMT_BITS_8
;
4210 val
|= AC_FMT_BITS_16
;
4215 if (maxbps
>= 32 || format
== SNDRV_PCM_FORMAT_FLOAT_LE
)
4216 val
|= AC_FMT_BITS_32
;
4217 else if (maxbps
>= 24)
4218 val
|= AC_FMT_BITS_24
;
4220 val
|= AC_FMT_BITS_20
;
4223 codec_dbg(codec
, "invalid format width %d\n",
4224 snd_pcm_format_width(format
));
4228 if (spdif_ctls
& AC_DIG1_NONAUDIO
)
4229 val
|= AC_FMT_TYPE_NON_PCM
;
4233 EXPORT_SYMBOL_GPL(snd_hda_calc_stream_format
);
4235 static unsigned int get_pcm_param(struct hda_codec
*codec
, hda_nid_t nid
,
4238 unsigned int val
= 0;
4239 if (nid
!= codec
->afg
&&
4240 (get_wcaps(codec
, nid
) & AC_WCAP_FORMAT_OVRD
))
4241 val
= snd_hda_param_read(codec
, nid
, AC_PAR_PCM
);
4242 if (!val
|| val
== -1)
4243 val
= snd_hda_param_read(codec
, codec
->afg
, AC_PAR_PCM
);
4244 if (!val
|| val
== -1)
4249 static unsigned int query_pcm_param(struct hda_codec
*codec
, hda_nid_t nid
)
4251 return query_caps_hash(codec
, nid
, 0, HDA_HASH_PARPCM_KEY(nid
),
4255 static unsigned int get_stream_param(struct hda_codec
*codec
, hda_nid_t nid
,
4258 unsigned int streams
= snd_hda_param_read(codec
, nid
, AC_PAR_STREAM
);
4259 if (!streams
|| streams
== -1)
4260 streams
= snd_hda_param_read(codec
, codec
->afg
, AC_PAR_STREAM
);
4261 if (!streams
|| streams
== -1)
4266 static unsigned int query_stream_param(struct hda_codec
*codec
, hda_nid_t nid
)
4268 return query_caps_hash(codec
, nid
, 0, HDA_HASH_PARSTR_KEY(nid
),
4273 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
4274 * @codec: the HDA codec
4275 * @nid: NID to query
4276 * @ratesp: the pointer to store the detected rate bitflags
4277 * @formatsp: the pointer to store the detected formats
4278 * @bpsp: the pointer to store the detected format widths
4280 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
4281 * or @bsps argument is ignored.
4283 * Returns 0 if successful, otherwise a negative error code.
4285 int snd_hda_query_supported_pcm(struct hda_codec
*codec
, hda_nid_t nid
,
4286 u32
*ratesp
, u64
*formatsp
, unsigned int *bpsp
)
4288 unsigned int i
, val
, wcaps
;
4290 wcaps
= get_wcaps(codec
, nid
);
4291 val
= query_pcm_param(codec
, nid
);
4295 for (i
= 0; i
< AC_PAR_PCM_RATE_BITS
; i
++) {
4297 rates
|= rate_bits
[i
].alsa_bits
;
4301 "rates == 0 (nid=0x%x, val=0x%x, ovrd=%i)\n",
4303 (wcaps
& AC_WCAP_FORMAT_OVRD
) ? 1 : 0);
4309 if (formatsp
|| bpsp
) {
4311 unsigned int streams
, bps
;
4313 streams
= query_stream_param(codec
, nid
);
4318 if (streams
& AC_SUPFMT_PCM
) {
4319 if (val
& AC_SUPPCM_BITS_8
) {
4320 formats
|= SNDRV_PCM_FMTBIT_U8
;
4323 if (val
& AC_SUPPCM_BITS_16
) {
4324 formats
|= SNDRV_PCM_FMTBIT_S16_LE
;
4327 if (wcaps
& AC_WCAP_DIGITAL
) {
4328 if (val
& AC_SUPPCM_BITS_32
)
4329 formats
|= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
;
4330 if (val
& (AC_SUPPCM_BITS_20
|AC_SUPPCM_BITS_24
))
4331 formats
|= SNDRV_PCM_FMTBIT_S32_LE
;
4332 if (val
& AC_SUPPCM_BITS_24
)
4334 else if (val
& AC_SUPPCM_BITS_20
)
4336 } else if (val
& (AC_SUPPCM_BITS_20
|AC_SUPPCM_BITS_24
|
4337 AC_SUPPCM_BITS_32
)) {
4338 formats
|= SNDRV_PCM_FMTBIT_S32_LE
;
4339 if (val
& AC_SUPPCM_BITS_32
)
4341 else if (val
& AC_SUPPCM_BITS_24
)
4343 else if (val
& AC_SUPPCM_BITS_20
)
4347 #if 0 /* FIXME: CS4206 doesn't work, which is the only codec supporting float */
4348 if (streams
& AC_SUPFMT_FLOAT32
) {
4349 formats
|= SNDRV_PCM_FMTBIT_FLOAT_LE
;
4354 if (streams
== AC_SUPFMT_AC3
) {
4355 /* should be exclusive */
4356 /* temporary hack: we have still no proper support
4357 * for the direct AC3 stream...
4359 formats
|= SNDRV_PCM_FMTBIT_U8
;
4364 "formats == 0 (nid=0x%x, val=0x%x, ovrd=%i, streams=0x%x)\n",
4366 (wcaps
& AC_WCAP_FORMAT_OVRD
) ? 1 : 0,
4371 *formatsp
= formats
;
4378 EXPORT_SYMBOL_GPL(snd_hda_query_supported_pcm
);
4381 * snd_hda_is_supported_format - Check the validity of the format
4382 * @codec: HD-audio codec
4383 * @nid: NID to check
4384 * @format: the HD-audio format value to check
4386 * Check whether the given node supports the format value.
4388 * Returns 1 if supported, 0 if not.
4390 int snd_hda_is_supported_format(struct hda_codec
*codec
, hda_nid_t nid
,
4391 unsigned int format
)
4394 unsigned int val
= 0, rate
, stream
;
4396 val
= query_pcm_param(codec
, nid
);
4400 rate
= format
& 0xff00;
4401 for (i
= 0; i
< AC_PAR_PCM_RATE_BITS
; i
++)
4402 if (rate_bits
[i
].hda_fmt
== rate
) {
4407 if (i
>= AC_PAR_PCM_RATE_BITS
)
4410 stream
= query_stream_param(codec
, nid
);
4414 if (stream
& AC_SUPFMT_PCM
) {
4415 switch (format
& 0xf0) {
4417 if (!(val
& AC_SUPPCM_BITS_8
))
4421 if (!(val
& AC_SUPPCM_BITS_16
))
4425 if (!(val
& AC_SUPPCM_BITS_20
))
4429 if (!(val
& AC_SUPPCM_BITS_24
))
4433 if (!(val
& AC_SUPPCM_BITS_32
))
4440 /* FIXME: check for float32 and AC3? */
4445 EXPORT_SYMBOL_GPL(snd_hda_is_supported_format
);
4450 static int hda_pcm_default_open_close(struct hda_pcm_stream
*hinfo
,
4451 struct hda_codec
*codec
,
4452 struct snd_pcm_substream
*substream
)
4457 static int hda_pcm_default_prepare(struct hda_pcm_stream
*hinfo
,
4458 struct hda_codec
*codec
,
4459 unsigned int stream_tag
,
4460 unsigned int format
,
4461 struct snd_pcm_substream
*substream
)
4463 snd_hda_codec_setup_stream(codec
, hinfo
->nid
, stream_tag
, 0, format
);
4467 static int hda_pcm_default_cleanup(struct hda_pcm_stream
*hinfo
,
4468 struct hda_codec
*codec
,
4469 struct snd_pcm_substream
*substream
)
4471 snd_hda_codec_cleanup_stream(codec
, hinfo
->nid
);
4475 static int set_pcm_default_values(struct hda_codec
*codec
,
4476 struct hda_pcm_stream
*info
)
4480 /* query support PCM information from the given NID */
4481 if (info
->nid
&& (!info
->rates
|| !info
->formats
)) {
4482 err
= snd_hda_query_supported_pcm(codec
, info
->nid
,
4483 info
->rates
? NULL
: &info
->rates
,
4484 info
->formats
? NULL
: &info
->formats
,
4485 info
->maxbps
? NULL
: &info
->maxbps
);
4489 if (info
->ops
.open
== NULL
)
4490 info
->ops
.open
= hda_pcm_default_open_close
;
4491 if (info
->ops
.close
== NULL
)
4492 info
->ops
.close
= hda_pcm_default_open_close
;
4493 if (info
->ops
.prepare
== NULL
) {
4494 if (snd_BUG_ON(!info
->nid
))
4496 info
->ops
.prepare
= hda_pcm_default_prepare
;
4498 if (info
->ops
.cleanup
== NULL
) {
4499 if (snd_BUG_ON(!info
->nid
))
4501 info
->ops
.cleanup
= hda_pcm_default_cleanup
;
4507 * codec prepare/cleanup entries
4510 * snd_hda_codec_prepare - Prepare a stream
4511 * @codec: the HDA codec
4512 * @hinfo: PCM information
4513 * @stream: stream tag to assign
4514 * @format: format id to assign
4515 * @substream: PCM substream to assign
4517 * Calls the prepare callback set by the codec with the given arguments.
4518 * Clean up the inactive streams when successful.
4520 int snd_hda_codec_prepare(struct hda_codec
*codec
,
4521 struct hda_pcm_stream
*hinfo
,
4522 unsigned int stream
,
4523 unsigned int format
,
4524 struct snd_pcm_substream
*substream
)
4527 mutex_lock(&codec
->bus
->prepare_mutex
);
4528 if (hinfo
->ops
.prepare
)
4529 ret
= hinfo
->ops
.prepare(hinfo
, codec
, stream
, format
,
4534 purify_inactive_streams(codec
);
4535 mutex_unlock(&codec
->bus
->prepare_mutex
);
4538 EXPORT_SYMBOL_GPL(snd_hda_codec_prepare
);
4541 * snd_hda_codec_cleanup - Prepare a stream
4542 * @codec: the HDA codec
4543 * @hinfo: PCM information
4544 * @substream: PCM substream
4546 * Calls the cleanup callback set by the codec with the given arguments.
4548 void snd_hda_codec_cleanup(struct hda_codec
*codec
,
4549 struct hda_pcm_stream
*hinfo
,
4550 struct snd_pcm_substream
*substream
)
4552 mutex_lock(&codec
->bus
->prepare_mutex
);
4553 if (hinfo
->ops
.cleanup
)
4554 hinfo
->ops
.cleanup(hinfo
, codec
, substream
);
4555 mutex_unlock(&codec
->bus
->prepare_mutex
);
4557 EXPORT_SYMBOL_GPL(snd_hda_codec_cleanup
);
4560 const char *snd_hda_pcm_type_name
[HDA_PCM_NTYPES
] = {
4561 "Audio", "SPDIF", "HDMI", "Modem"
4565 * get the empty PCM device number to assign
4567 static int get_empty_pcm_device(struct hda_bus
*bus
, unsigned int type
)
4569 /* audio device indices; not linear to keep compatibility */
4570 /* assigned to static slots up to dev#10; if more needed, assign
4571 * the later slot dynamically (when CONFIG_SND_DYNAMIC_MINORS=y)
4573 static int audio_idx
[HDA_PCM_NTYPES
][5] = {
4574 [HDA_PCM_TYPE_AUDIO
] = { 0, 2, 4, 5, -1 },
4575 [HDA_PCM_TYPE_SPDIF
] = { 1, -1 },
4576 [HDA_PCM_TYPE_HDMI
] = { 3, 7, 8, 9, -1 },
4577 [HDA_PCM_TYPE_MODEM
] = { 6, -1 },
4581 if (type
>= HDA_PCM_NTYPES
) {
4582 dev_err(bus
->card
->dev
, "Invalid PCM type %d\n", type
);
4586 for (i
= 0; audio_idx
[type
][i
] >= 0; i
++) {
4587 #ifndef CONFIG_SND_DYNAMIC_MINORS
4588 if (audio_idx
[type
][i
] >= 8)
4591 if (!test_and_set_bit(audio_idx
[type
][i
], bus
->pcm_dev_bits
))
4592 return audio_idx
[type
][i
];
4595 #ifdef CONFIG_SND_DYNAMIC_MINORS
4596 /* non-fixed slots starting from 10 */
4597 for (i
= 10; i
< 32; i
++) {
4598 if (!test_and_set_bit(i
, bus
->pcm_dev_bits
))
4603 dev_warn(bus
->card
->dev
, "Too many %s devices\n",
4604 snd_hda_pcm_type_name
[type
]);
4605 #ifndef CONFIG_SND_DYNAMIC_MINORS
4606 dev_warn(bus
->card
->dev
,
4607 "Consider building the kernel with CONFIG_SND_DYNAMIC_MINORS=y\n");
4612 /* call build_pcms ops of the given codec and set up the default parameters */
4613 int snd_hda_codec_parse_pcms(struct hda_codec
*codec
)
4615 struct hda_pcm
*cpcm
;
4618 if (!list_empty(&codec
->pcm_list_head
))
4619 return 0; /* already parsed */
4621 if (!codec
->patch_ops
.build_pcms
)
4624 err
= codec
->patch_ops
.build_pcms(codec
);
4626 codec_err(codec
, "cannot build PCMs for #%d (error %d)\n",
4631 list_for_each_entry(cpcm
, &codec
->pcm_list_head
, list
) {
4634 for (stream
= 0; stream
< 2; stream
++) {
4635 struct hda_pcm_stream
*info
= &cpcm
->stream
[stream
];
4637 if (!info
->substreams
)
4639 err
= set_pcm_default_values(codec
, info
);
4642 "fail to setup default for PCM %s\n",
4652 /* assign all PCMs of the given codec */
4653 int snd_hda_codec_build_pcms(struct hda_codec
*codec
)
4655 struct hda_bus
*bus
= codec
->bus
;
4656 struct hda_pcm
*cpcm
;
4659 if (snd_BUG_ON(!bus
->ops
.attach_pcm
))
4662 err
= snd_hda_codec_parse_pcms(codec
);
4664 snd_hda_codec_reset(codec
);
4668 /* attach a new PCM streams */
4669 list_for_each_entry(cpcm
, &codec
->pcm_list_head
, list
) {
4671 continue; /* already attached */
4672 if (!cpcm
->stream
[0].substreams
&& !cpcm
->stream
[1].substreams
)
4673 continue; /* no substreams assigned */
4675 dev
= get_empty_pcm_device(bus
, cpcm
->pcm_type
);
4677 continue; /* no fatal error */
4679 err
= bus
->ops
.attach_pcm(bus
, codec
, cpcm
);
4682 "cannot attach PCM stream %d for codec #%d\n",
4684 continue; /* no fatal error */
4692 * snd_hda_build_pcms - build PCM information
4695 * Create PCM information for each codec included in the bus.
4697 * The build_pcms codec patch is requested to create and assign new
4698 * hda_pcm objects. The codec is responsible to call snd_hda_codec_pcm_new()
4699 * and fills the fields. Later they are instantiated by this function.
4701 * At least, substreams, channels_min and channels_max must be filled for
4702 * each stream. substreams = 0 indicates that the stream doesn't exist.
4703 * When rates and/or formats are zero, the supported values are queried
4704 * from the given nid. The nid is used also by the default ops.prepare
4705 * and ops.cleanup callbacks.
4707 * The driver needs to call ops.open in its open callback. Similarly,
4708 * ops.close is supposed to be called in the close callback.
4709 * ops.prepare should be called in the prepare or hw_params callback
4710 * with the proper parameters for set up.
4711 * ops.cleanup should be called in hw_free for clean up of streams.
4713 * This function returns 0 if successful, or a negative error code.
4715 int snd_hda_build_pcms(struct hda_bus
*bus
)
4717 struct hda_codec
*codec
;
4719 list_for_each_entry(codec
, &bus
->codec_list
, list
) {
4720 int err
= snd_hda_codec_build_pcms(codec
);
4726 EXPORT_SYMBOL_GPL(snd_hda_build_pcms
);
4729 * snd_hda_add_new_ctls - create controls from the array
4730 * @codec: the HDA codec
4731 * @knew: the array of struct snd_kcontrol_new
4733 * This helper function creates and add new controls in the given array.
4734 * The array must be terminated with an empty entry as terminator.
4736 * Returns 0 if successful, or a negative error code.
4738 int snd_hda_add_new_ctls(struct hda_codec
*codec
,
4739 const struct snd_kcontrol_new
*knew
)
4743 for (; knew
->name
; knew
++) {
4744 struct snd_kcontrol
*kctl
;
4745 int addr
= 0, idx
= 0;
4746 if (knew
->iface
== -1) /* skip this codec private value */
4749 kctl
= snd_ctl_new1(knew
, codec
);
4753 kctl
->id
.device
= addr
;
4755 kctl
->id
.index
= idx
;
4756 err
= snd_hda_ctl_add(codec
, 0, kctl
);
4759 /* try first with another device index corresponding to
4760 * the codec addr; if it still fails (or it's the
4761 * primary codec), then try another control index
4763 if (!addr
&& codec
->addr
)
4765 else if (!idx
&& !knew
->index
) {
4766 idx
= find_empty_mixer_ctl_idx(codec
,
4776 EXPORT_SYMBOL_GPL(snd_hda_add_new_ctls
);
4780 * snd_hda_power_up - Power-up the codec
4781 * @codec: HD-audio codec
4783 * Increment the usage counter and resume the device if not done yet.
4785 void snd_hda_power_up(struct hda_codec
*codec
)
4787 struct device
*dev
= hda_codec_dev(codec
);
4789 if (codec_in_pm(codec
))
4791 pm_runtime_get_sync(dev
);
4793 EXPORT_SYMBOL_GPL(snd_hda_power_up
);
4796 * snd_hda_power_down - Power-down the codec
4797 * @codec: HD-audio codec
4799 * Decrement the usage counter and schedules the autosuspend if none used.
4801 void snd_hda_power_down(struct hda_codec
*codec
)
4803 struct device
*dev
= hda_codec_dev(codec
);
4805 if (codec_in_pm(codec
))
4807 pm_runtime_mark_last_busy(dev
);
4808 pm_runtime_put_autosuspend(dev
);
4810 EXPORT_SYMBOL_GPL(snd_hda_power_down
);
4812 static void codec_set_power_save(struct hda_codec
*codec
, int delay
)
4814 struct device
*dev
= hda_codec_dev(codec
);
4817 pm_runtime_set_autosuspend_delay(dev
, delay
);
4818 pm_runtime_use_autosuspend(dev
);
4819 pm_runtime_allow(dev
);
4820 if (!pm_runtime_suspended(dev
))
4821 pm_runtime_mark_last_busy(dev
);
4823 pm_runtime_dont_use_autosuspend(dev
);
4824 pm_runtime_forbid(dev
);
4829 * snd_hda_set_power_save - reprogram autosuspend for the given delay
4830 * @bus: HD-audio bus
4831 * @delay: autosuspend delay in msec, 0 = off
4833 * Synchronize the runtime PM autosuspend state from the power_save option.
4835 void snd_hda_set_power_save(struct hda_bus
*bus
, int delay
)
4837 struct hda_codec
*c
;
4839 list_for_each_entry(c
, &bus
->codec_list
, list
)
4840 codec_set_power_save(c
, delay
);
4842 EXPORT_SYMBOL_GPL(snd_hda_set_power_save
);
4845 * snd_hda_check_amp_list_power - Check the amp list and update the power
4846 * @codec: HD-audio codec
4847 * @check: the object containing an AMP list and the status
4848 * @nid: NID to check / update
4850 * Check whether the given NID is in the amp list. If it's in the list,
4851 * check the current AMP status, and update the the power-status according
4852 * to the mute status.
4854 * This function is supposed to be set or called from the check_power_status
4857 int snd_hda_check_amp_list_power(struct hda_codec
*codec
,
4858 struct hda_loopback_check
*check
,
4861 const struct hda_amp_list
*p
;
4864 if (!check
->amplist
)
4866 for (p
= check
->amplist
; p
->nid
; p
++) {
4871 return 0; /* nothing changed */
4873 for (p
= check
->amplist
; p
->nid
; p
++) {
4874 for (ch
= 0; ch
< 2; ch
++) {
4875 v
= snd_hda_codec_amp_read(codec
, p
->nid
, ch
, p
->dir
,
4877 if (!(v
& HDA_AMP_MUTE
) && v
> 0) {
4878 if (!check
->power_on
) {
4879 check
->power_on
= 1;
4880 snd_hda_power_up(codec
);
4886 if (check
->power_on
) {
4887 check
->power_on
= 0;
4888 snd_hda_power_down(codec
);
4892 EXPORT_SYMBOL_GPL(snd_hda_check_amp_list_power
);
4900 * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum
4901 * @imux: imux helper object
4902 * @uinfo: pointer to get/store the data
4904 int snd_hda_input_mux_info(const struct hda_input_mux
*imux
,
4905 struct snd_ctl_elem_info
*uinfo
)
4909 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_ENUMERATED
;
4911 uinfo
->value
.enumerated
.items
= imux
->num_items
;
4912 if (!imux
->num_items
)
4914 index
= uinfo
->value
.enumerated
.item
;
4915 if (index
>= imux
->num_items
)
4916 index
= imux
->num_items
- 1;
4917 strcpy(uinfo
->value
.enumerated
.name
, imux
->items
[index
].label
);
4920 EXPORT_SYMBOL_GPL(snd_hda_input_mux_info
);
4923 * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum
4924 * @codec: the HDA codec
4925 * @imux: imux helper object
4926 * @ucontrol: pointer to get/store the data
4927 * @nid: input mux NID
4928 * @cur_val: pointer to get/store the current imux value
4930 int snd_hda_input_mux_put(struct hda_codec
*codec
,
4931 const struct hda_input_mux
*imux
,
4932 struct snd_ctl_elem_value
*ucontrol
,
4934 unsigned int *cur_val
)
4938 if (!imux
->num_items
)
4940 idx
= ucontrol
->value
.enumerated
.item
[0];
4941 if (idx
>= imux
->num_items
)
4942 idx
= imux
->num_items
- 1;
4943 if (*cur_val
== idx
)
4945 snd_hda_codec_write_cache(codec
, nid
, 0, AC_VERB_SET_CONNECT_SEL
,
4946 imux
->items
[idx
].index
);
4950 EXPORT_SYMBOL_GPL(snd_hda_input_mux_put
);
4954 * snd_hda_enum_helper_info - Helper for simple enum ctls
4955 * @kcontrol: ctl element
4956 * @uinfo: pointer to get/store the data
4957 * @num_items: number of enum items
4958 * @texts: enum item string array
4960 * process kcontrol info callback of a simple string enum array
4961 * when @num_items is 0 or @texts is NULL, assume a boolean enum array
4963 int snd_hda_enum_helper_info(struct snd_kcontrol
*kcontrol
,
4964 struct snd_ctl_elem_info
*uinfo
,
4965 int num_items
, const char * const *texts
)
4967 static const char * const texts_default
[] = {
4968 "Disabled", "Enabled"
4971 if (!texts
|| !num_items
) {
4973 texts
= texts_default
;
4976 return snd_ctl_enum_info(uinfo
, 1, num_items
, texts
);
4978 EXPORT_SYMBOL_GPL(snd_hda_enum_helper_info
);
4981 * Multi-channel / digital-out PCM helper functions
4984 /* setup SPDIF output stream */
4985 static void setup_dig_out_stream(struct hda_codec
*codec
, hda_nid_t nid
,
4986 unsigned int stream_tag
, unsigned int format
)
4988 struct hda_spdif_out
*spdif
;
4989 unsigned int curr_fmt
;
4992 spdif
= snd_hda_spdif_out_of_nid(codec
, nid
);
4993 curr_fmt
= snd_hda_codec_read(codec
, nid
, 0,
4994 AC_VERB_GET_STREAM_FORMAT
, 0);
4995 reset
= codec
->spdif_status_reset
&&
4996 (spdif
->ctls
& AC_DIG1_ENABLE
) &&
4999 /* turn off SPDIF if needed; otherwise the IEC958 bits won't be
5002 set_dig_out_convert(codec
, nid
,
5003 spdif
->ctls
& ~AC_DIG1_ENABLE
& 0xff,
5005 snd_hda_codec_setup_stream(codec
, nid
, stream_tag
, 0, format
);
5006 if (codec
->slave_dig_outs
) {
5008 for (d
= codec
->slave_dig_outs
; *d
; d
++)
5009 snd_hda_codec_setup_stream(codec
, *d
, stream_tag
, 0,
5012 /* turn on again (if needed) */
5014 set_dig_out_convert(codec
, nid
,
5015 spdif
->ctls
& 0xff, -1);
5018 static void cleanup_dig_out_stream(struct hda_codec
*codec
, hda_nid_t nid
)
5020 snd_hda_codec_cleanup_stream(codec
, nid
);
5021 if (codec
->slave_dig_outs
) {
5023 for (d
= codec
->slave_dig_outs
; *d
; d
++)
5024 snd_hda_codec_cleanup_stream(codec
, *d
);
5029 * snd_hda_bus_reboot_notify - call the reboot notifier of each codec
5030 * @bus: HD-audio bus
5032 void snd_hda_bus_reboot_notify(struct hda_bus
*bus
)
5034 struct hda_codec
*codec
;
5038 list_for_each_entry(codec
, &bus
->codec_list
, list
) {
5039 if (hda_codec_is_power_on(codec
) &&
5040 codec
->patch_ops
.reboot_notify
)
5041 codec
->patch_ops
.reboot_notify(codec
);
5044 EXPORT_SYMBOL_GPL(snd_hda_bus_reboot_notify
);
5047 * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode
5048 * @codec: the HDA codec
5049 * @mout: hda_multi_out object
5051 int snd_hda_multi_out_dig_open(struct hda_codec
*codec
,
5052 struct hda_multi_out
*mout
)
5054 mutex_lock(&codec
->spdif_mutex
);
5055 if (mout
->dig_out_used
== HDA_DIG_ANALOG_DUP
)
5056 /* already opened as analog dup; reset it once */
5057 cleanup_dig_out_stream(codec
, mout
->dig_out_nid
);
5058 mout
->dig_out_used
= HDA_DIG_EXCLUSIVE
;
5059 mutex_unlock(&codec
->spdif_mutex
);
5062 EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_open
);
5065 * snd_hda_multi_out_dig_prepare - prepare the digital out stream
5066 * @codec: the HDA codec
5067 * @mout: hda_multi_out object
5068 * @stream_tag: stream tag to assign
5069 * @format: format id to assign
5070 * @substream: PCM substream to assign
5072 int snd_hda_multi_out_dig_prepare(struct hda_codec
*codec
,
5073 struct hda_multi_out
*mout
,
5074 unsigned int stream_tag
,
5075 unsigned int format
,
5076 struct snd_pcm_substream
*substream
)
5078 mutex_lock(&codec
->spdif_mutex
);
5079 setup_dig_out_stream(codec
, mout
->dig_out_nid
, stream_tag
, format
);
5080 mutex_unlock(&codec
->spdif_mutex
);
5083 EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_prepare
);
5086 * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream
5087 * @codec: the HDA codec
5088 * @mout: hda_multi_out object
5090 int snd_hda_multi_out_dig_cleanup(struct hda_codec
*codec
,
5091 struct hda_multi_out
*mout
)
5093 mutex_lock(&codec
->spdif_mutex
);
5094 cleanup_dig_out_stream(codec
, mout
->dig_out_nid
);
5095 mutex_unlock(&codec
->spdif_mutex
);
5098 EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_cleanup
);
5101 * snd_hda_multi_out_dig_close - release the digital out stream
5102 * @codec: the HDA codec
5103 * @mout: hda_multi_out object
5105 int snd_hda_multi_out_dig_close(struct hda_codec
*codec
,
5106 struct hda_multi_out
*mout
)
5108 mutex_lock(&codec
->spdif_mutex
);
5109 mout
->dig_out_used
= 0;
5110 mutex_unlock(&codec
->spdif_mutex
);
5113 EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_close
);
5116 * snd_hda_multi_out_analog_open - open analog outputs
5117 * @codec: the HDA codec
5118 * @mout: hda_multi_out object
5119 * @substream: PCM substream to assign
5120 * @hinfo: PCM information to assign
5122 * Open analog outputs and set up the hw-constraints.
5123 * If the digital outputs can be opened as slave, open the digital
5126 int snd_hda_multi_out_analog_open(struct hda_codec
*codec
,
5127 struct hda_multi_out
*mout
,
5128 struct snd_pcm_substream
*substream
,
5129 struct hda_pcm_stream
*hinfo
)
5131 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
5132 runtime
->hw
.channels_max
= mout
->max_channels
;
5133 if (mout
->dig_out_nid
) {
5134 if (!mout
->analog_rates
) {
5135 mout
->analog_rates
= hinfo
->rates
;
5136 mout
->analog_formats
= hinfo
->formats
;
5137 mout
->analog_maxbps
= hinfo
->maxbps
;
5139 runtime
->hw
.rates
= mout
->analog_rates
;
5140 runtime
->hw
.formats
= mout
->analog_formats
;
5141 hinfo
->maxbps
= mout
->analog_maxbps
;
5143 if (!mout
->spdif_rates
) {
5144 snd_hda_query_supported_pcm(codec
, mout
->dig_out_nid
,
5146 &mout
->spdif_formats
,
5147 &mout
->spdif_maxbps
);
5149 mutex_lock(&codec
->spdif_mutex
);
5150 if (mout
->share_spdif
) {
5151 if ((runtime
->hw
.rates
& mout
->spdif_rates
) &&
5152 (runtime
->hw
.formats
& mout
->spdif_formats
)) {
5153 runtime
->hw
.rates
&= mout
->spdif_rates
;
5154 runtime
->hw
.formats
&= mout
->spdif_formats
;
5155 if (mout
->spdif_maxbps
< hinfo
->maxbps
)
5156 hinfo
->maxbps
= mout
->spdif_maxbps
;
5158 mout
->share_spdif
= 0;
5159 /* FIXME: need notify? */
5162 mutex_unlock(&codec
->spdif_mutex
);
5164 return snd_pcm_hw_constraint_step(substream
->runtime
, 0,
5165 SNDRV_PCM_HW_PARAM_CHANNELS
, 2);
5167 EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_open
);
5170 * snd_hda_multi_out_analog_prepare - Preapre the analog outputs.
5171 * @codec: the HDA codec
5172 * @mout: hda_multi_out object
5173 * @stream_tag: stream tag to assign
5174 * @format: format id to assign
5175 * @substream: PCM substream to assign
5177 * Set up the i/o for analog out.
5178 * When the digital out is available, copy the front out to digital out, too.
5180 int snd_hda_multi_out_analog_prepare(struct hda_codec
*codec
,
5181 struct hda_multi_out
*mout
,
5182 unsigned int stream_tag
,
5183 unsigned int format
,
5184 struct snd_pcm_substream
*substream
)
5186 const hda_nid_t
*nids
= mout
->dac_nids
;
5187 int chs
= substream
->runtime
->channels
;
5188 struct hda_spdif_out
*spdif
;
5191 mutex_lock(&codec
->spdif_mutex
);
5192 spdif
= snd_hda_spdif_out_of_nid(codec
, mout
->dig_out_nid
);
5193 if (mout
->dig_out_nid
&& mout
->share_spdif
&&
5194 mout
->dig_out_used
!= HDA_DIG_EXCLUSIVE
) {
5196 snd_hda_is_supported_format(codec
, mout
->dig_out_nid
,
5198 !(spdif
->status
& IEC958_AES0_NONAUDIO
)) {
5199 mout
->dig_out_used
= HDA_DIG_ANALOG_DUP
;
5200 setup_dig_out_stream(codec
, mout
->dig_out_nid
,
5201 stream_tag
, format
);
5203 mout
->dig_out_used
= 0;
5204 cleanup_dig_out_stream(codec
, mout
->dig_out_nid
);
5207 mutex_unlock(&codec
->spdif_mutex
);
5210 snd_hda_codec_setup_stream(codec
, nids
[HDA_FRONT
], stream_tag
,
5212 if (!mout
->no_share_stream
&&
5213 mout
->hp_nid
&& mout
->hp_nid
!= nids
[HDA_FRONT
])
5214 /* headphone out will just decode front left/right (stereo) */
5215 snd_hda_codec_setup_stream(codec
, mout
->hp_nid
, stream_tag
,
5217 /* extra outputs copied from front */
5218 for (i
= 0; i
< ARRAY_SIZE(mout
->hp_out_nid
); i
++)
5219 if (!mout
->no_share_stream
&& mout
->hp_out_nid
[i
])
5220 snd_hda_codec_setup_stream(codec
,
5221 mout
->hp_out_nid
[i
],
5222 stream_tag
, 0, format
);
5225 for (i
= 1; i
< mout
->num_dacs
; i
++) {
5226 if (chs
>= (i
+ 1) * 2) /* independent out */
5227 snd_hda_codec_setup_stream(codec
, nids
[i
], stream_tag
,
5229 else if (!mout
->no_share_stream
) /* copy front */
5230 snd_hda_codec_setup_stream(codec
, nids
[i
], stream_tag
,
5234 /* extra surrounds */
5235 for (i
= 0; i
< ARRAY_SIZE(mout
->extra_out_nid
); i
++) {
5237 if (!mout
->extra_out_nid
[i
])
5239 if (chs
>= (i
+ 1) * 2)
5241 else if (!mout
->no_share_stream
)
5243 snd_hda_codec_setup_stream(codec
, mout
->extra_out_nid
[i
],
5244 stream_tag
, ch
, format
);
5249 EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_prepare
);
5252 * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out
5253 * @codec: the HDA codec
5254 * @mout: hda_multi_out object
5256 int snd_hda_multi_out_analog_cleanup(struct hda_codec
*codec
,
5257 struct hda_multi_out
*mout
)
5259 const hda_nid_t
*nids
= mout
->dac_nids
;
5262 for (i
= 0; i
< mout
->num_dacs
; i
++)
5263 snd_hda_codec_cleanup_stream(codec
, nids
[i
]);
5265 snd_hda_codec_cleanup_stream(codec
, mout
->hp_nid
);
5266 for (i
= 0; i
< ARRAY_SIZE(mout
->hp_out_nid
); i
++)
5267 if (mout
->hp_out_nid
[i
])
5268 snd_hda_codec_cleanup_stream(codec
,
5269 mout
->hp_out_nid
[i
]);
5270 for (i
= 0; i
< ARRAY_SIZE(mout
->extra_out_nid
); i
++)
5271 if (mout
->extra_out_nid
[i
])
5272 snd_hda_codec_cleanup_stream(codec
,
5273 mout
->extra_out_nid
[i
]);
5274 mutex_lock(&codec
->spdif_mutex
);
5275 if (mout
->dig_out_nid
&& mout
->dig_out_used
== HDA_DIG_ANALOG_DUP
) {
5276 cleanup_dig_out_stream(codec
, mout
->dig_out_nid
);
5277 mout
->dig_out_used
= 0;
5279 mutex_unlock(&codec
->spdif_mutex
);
5282 EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_cleanup
);
5285 * snd_hda_get_default_vref - Get the default (mic) VREF pin bits
5286 * @codec: the HDA codec
5287 * @pin: referred pin NID
5289 * Guess the suitable VREF pin bits to be set as the pin-control value.
5290 * Note: the function doesn't set the AC_PINCTL_IN_EN bit.
5292 unsigned int snd_hda_get_default_vref(struct hda_codec
*codec
, hda_nid_t pin
)
5294 unsigned int pincap
;
5295 unsigned int oldval
;
5296 oldval
= snd_hda_codec_read(codec
, pin
, 0,
5297 AC_VERB_GET_PIN_WIDGET_CONTROL
, 0);
5298 pincap
= snd_hda_query_pin_caps(codec
, pin
);
5299 pincap
= (pincap
& AC_PINCAP_VREF
) >> AC_PINCAP_VREF_SHIFT
;
5300 /* Exception: if the default pin setup is vref50, we give it priority */
5301 if ((pincap
& AC_PINCAP_VREF_80
) && oldval
!= PIN_VREF50
)
5302 return AC_PINCTL_VREF_80
;
5303 else if (pincap
& AC_PINCAP_VREF_50
)
5304 return AC_PINCTL_VREF_50
;
5305 else if (pincap
& AC_PINCAP_VREF_100
)
5306 return AC_PINCTL_VREF_100
;
5307 else if (pincap
& AC_PINCAP_VREF_GRD
)
5308 return AC_PINCTL_VREF_GRD
;
5309 return AC_PINCTL_VREF_HIZ
;
5311 EXPORT_SYMBOL_GPL(snd_hda_get_default_vref
);
5314 * snd_hda_correct_pin_ctl - correct the pin ctl value for matching with the pin cap
5315 * @codec: the HDA codec
5316 * @pin: referred pin NID
5317 * @val: pin ctl value to audit
5319 unsigned int snd_hda_correct_pin_ctl(struct hda_codec
*codec
,
5320 hda_nid_t pin
, unsigned int val
)
5322 static unsigned int cap_lists
[][2] = {
5323 { AC_PINCTL_VREF_100
, AC_PINCAP_VREF_100
},
5324 { AC_PINCTL_VREF_80
, AC_PINCAP_VREF_80
},
5325 { AC_PINCTL_VREF_50
, AC_PINCAP_VREF_50
},
5326 { AC_PINCTL_VREF_GRD
, AC_PINCAP_VREF_GRD
},
5332 cap
= snd_hda_query_pin_caps(codec
, pin
);
5334 return val
; /* don't know what to do... */
5336 if (val
& AC_PINCTL_OUT_EN
) {
5337 if (!(cap
& AC_PINCAP_OUT
))
5338 val
&= ~(AC_PINCTL_OUT_EN
| AC_PINCTL_HP_EN
);
5339 else if ((val
& AC_PINCTL_HP_EN
) && !(cap
& AC_PINCAP_HP_DRV
))
5340 val
&= ~AC_PINCTL_HP_EN
;
5343 if (val
& AC_PINCTL_IN_EN
) {
5344 if (!(cap
& AC_PINCAP_IN
))
5345 val
&= ~(AC_PINCTL_IN_EN
| AC_PINCTL_VREFEN
);
5347 unsigned int vcap
, vref
;
5349 vcap
= (cap
& AC_PINCAP_VREF
) >> AC_PINCAP_VREF_SHIFT
;
5350 vref
= val
& AC_PINCTL_VREFEN
;
5351 for (i
= 0; i
< ARRAY_SIZE(cap_lists
); i
++) {
5352 if (vref
== cap_lists
[i
][0] &&
5353 !(vcap
& cap_lists
[i
][1])) {
5354 if (i
== ARRAY_SIZE(cap_lists
) - 1)
5355 vref
= AC_PINCTL_VREF_HIZ
;
5357 vref
= cap_lists
[i
+ 1][0];
5360 val
&= ~AC_PINCTL_VREFEN
;
5367 EXPORT_SYMBOL_GPL(snd_hda_correct_pin_ctl
);
5370 * _snd_hda_pin_ctl - Helper to set pin ctl value
5371 * @codec: the HDA codec
5372 * @pin: referred pin NID
5373 * @val: pin control value to set
5374 * @cached: access over codec pinctl cache or direct write
5376 * This function is a helper to set a pin ctl value more safely.
5377 * It corrects the pin ctl value via snd_hda_correct_pin_ctl(), stores the
5378 * value in pin target array via snd_hda_codec_set_pin_target(), then
5379 * actually writes the value via either snd_hda_codec_update_cache() or
5380 * snd_hda_codec_write() depending on @cached flag.
5382 int _snd_hda_set_pin_ctl(struct hda_codec
*codec
, hda_nid_t pin
,
5383 unsigned int val
, bool cached
)
5385 val
= snd_hda_correct_pin_ctl(codec
, pin
, val
);
5386 snd_hda_codec_set_pin_target(codec
, pin
, val
);
5388 return snd_hda_codec_update_cache(codec
, pin
, 0,
5389 AC_VERB_SET_PIN_WIDGET_CONTROL
, val
);
5391 return snd_hda_codec_write(codec
, pin
, 0,
5392 AC_VERB_SET_PIN_WIDGET_CONTROL
, val
);
5394 EXPORT_SYMBOL_GPL(_snd_hda_set_pin_ctl
);
5397 * snd_hda_add_imux_item - Add an item to input_mux
5398 * @codec: the HDA codec
5399 * @imux: imux helper object
5400 * @label: the name of imux item to assign
5401 * @index: index number of imux item to assign
5402 * @type_idx: pointer to store the resultant label index
5404 * When the same label is used already in the existing items, the number
5405 * suffix is appended to the label. This label index number is stored
5406 * to type_idx when non-NULL pointer is given.
5408 int snd_hda_add_imux_item(struct hda_codec
*codec
,
5409 struct hda_input_mux
*imux
, const char *label
,
5410 int index
, int *type_idx
)
5412 int i
, label_idx
= 0;
5413 if (imux
->num_items
>= HDA_MAX_NUM_INPUTS
) {
5414 codec_err(codec
, "hda_codec: Too many imux items!\n");
5417 for (i
= 0; i
< imux
->num_items
; i
++) {
5418 if (!strncmp(label
, imux
->items
[i
].label
, strlen(label
)))
5422 *type_idx
= label_idx
;
5424 snprintf(imux
->items
[imux
->num_items
].label
,
5425 sizeof(imux
->items
[imux
->num_items
].label
),
5426 "%s %d", label
, label_idx
);
5428 strlcpy(imux
->items
[imux
->num_items
].label
, label
,
5429 sizeof(imux
->items
[imux
->num_items
].label
));
5430 imux
->items
[imux
->num_items
].index
= index
;
5434 EXPORT_SYMBOL_GPL(snd_hda_add_imux_item
);
5437 * snd_hda_bus_reset - Reset the bus
5438 * @bus: HD-audio bus
5440 void snd_hda_bus_reset(struct hda_bus
*bus
)
5442 struct hda_codec
*codec
;
5444 list_for_each_entry(codec
, &bus
->codec_list
, list
) {
5445 /* FIXME: maybe a better way needed for forced reset */
5446 cancel_delayed_work_sync(&codec
->jackpoll_work
);
5448 if (hda_codec_is_power_on(codec
)) {
5449 hda_call_codec_suspend(codec
);
5450 hda_call_codec_resume(codec
);
5455 EXPORT_SYMBOL_GPL(snd_hda_bus_reset
);
5462 * snd_array_new - get a new element from the given array
5463 * @array: the array object
5465 * Get a new element from the given array. If it exceeds the
5466 * pre-allocated array size, re-allocate the array.
5468 * Returns NULL if allocation failed.
5470 void *snd_array_new(struct snd_array
*array
)
5472 if (snd_BUG_ON(!array
->elem_size
))
5474 if (array
->used
>= array
->alloced
) {
5475 int num
= array
->alloced
+ array
->alloc_align
;
5476 int size
= (num
+ 1) * array
->elem_size
;
5478 if (snd_BUG_ON(num
>= 4096))
5480 nlist
= krealloc(array
->list
, size
, GFP_KERNEL
| __GFP_ZERO
);
5483 array
->list
= nlist
;
5484 array
->alloced
= num
;
5486 return snd_array_elem(array
, array
->used
++);
5488 EXPORT_SYMBOL_GPL(snd_array_new
);
5491 * snd_array_free - free the given array elements
5492 * @array: the array object
5494 void snd_array_free(struct snd_array
*array
)
5501 EXPORT_SYMBOL_GPL(snd_array_free
);
5504 * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer
5505 * @pcm: PCM caps bits
5506 * @buf: the string buffer to write
5507 * @buflen: the max buffer length
5509 * used by hda_proc.c and hda_eld.c
5511 void snd_print_pcm_bits(int pcm
, char *buf
, int buflen
)
5513 static unsigned int bits
[] = { 8, 16, 20, 24, 32 };
5516 for (i
= 0, j
= 0; i
< ARRAY_SIZE(bits
); i
++)
5517 if (pcm
& (AC_SUPPCM_BITS_8
<< i
))
5518 j
+= snprintf(buf
+ j
, buflen
- j
, " %d", bits
[i
]);
5520 buf
[j
] = '\0'; /* necessary when j == 0 */
5522 EXPORT_SYMBOL_GPL(snd_print_pcm_bits
);
5524 MODULE_DESCRIPTION("HDA codec core");
5525 MODULE_LICENSE("GPL");