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>
29 #include <sound/core.h>
30 #include "hda_codec.h"
31 #include <sound/asoundef.h>
32 #include <sound/tlv.h>
33 #include <sound/initval.h>
34 #include <sound/jack.h>
35 #include "hda_local.h"
38 #include <sound/hda_hwdep.h>
40 #define CREATE_TRACE_POINTS
41 #include "hda_trace.h"
44 * vendor / preset table
47 struct hda_vendor_id
{
52 /* codec vendor labels */
53 static struct hda_vendor_id hda_vendor_ids
[] = {
55 { 0x1013, "Cirrus Logic" },
56 { 0x1057, "Motorola" },
57 { 0x1095, "Silicon Image" },
59 { 0x10ec, "Realtek" },
60 { 0x1102, "Creative" },
64 { 0x11d4, "Analog Devices" },
65 { 0x13f6, "C-Media" },
66 { 0x14f1, "Conexant" },
67 { 0x17e8, "Chrontel" },
69 { 0x1aec, "Wolfson Microelectronics" },
71 { 0x434d, "C-Media" },
73 { 0x8384, "SigmaTel" },
77 static DEFINE_MUTEX(preset_mutex
);
78 static LIST_HEAD(hda_preset_tables
);
81 * snd_hda_add_codec_preset - Add a codec preset to the chain
82 * @preset: codec preset table to add
84 int snd_hda_add_codec_preset(struct hda_codec_preset_list
*preset
)
86 mutex_lock(&preset_mutex
);
87 list_add_tail(&preset
->list
, &hda_preset_tables
);
88 mutex_unlock(&preset_mutex
);
91 EXPORT_SYMBOL_GPL(snd_hda_add_codec_preset
);
94 * snd_hda_delete_codec_preset - Delete a codec preset from the chain
95 * @preset: codec preset table to delete
97 int snd_hda_delete_codec_preset(struct hda_codec_preset_list
*preset
)
99 mutex_lock(&preset_mutex
);
100 list_del(&preset
->list
);
101 mutex_unlock(&preset_mutex
);
104 EXPORT_SYMBOL_GPL(snd_hda_delete_codec_preset
);
107 #define codec_in_pm(codec) ((codec)->in_pm)
108 static void hda_power_work(struct work_struct
*work
);
109 static void hda_keep_power_on(struct hda_codec
*codec
);
110 #define hda_codec_is_power_on(codec) ((codec)->power_on)
112 static void hda_call_pm_notify(struct hda_codec
*codec
, bool power_up
)
114 struct hda_bus
*bus
= codec
->bus
;
116 if ((power_up
&& codec
->pm_up_notified
) ||
117 (!power_up
&& !codec
->pm_up_notified
))
119 if (bus
->ops
.pm_notify
)
120 bus
->ops
.pm_notify(bus
, power_up
);
121 codec
->pm_up_notified
= power_up
;
125 #define codec_in_pm(codec) 0
126 static inline void hda_keep_power_on(struct hda_codec
*codec
) {}
127 #define hda_codec_is_power_on(codec) 1
128 #define hda_call_pm_notify(codec, state) {}
132 * snd_hda_get_jack_location - Give a location string of the jack
133 * @cfg: pin default config value
135 * Parse the pin default config value and returns the string of the
136 * jack location, e.g. "Rear", "Front", etc.
138 const char *snd_hda_get_jack_location(u32 cfg
)
140 static char *bases
[7] = {
141 "N/A", "Rear", "Front", "Left", "Right", "Top", "Bottom",
143 static unsigned char specials_idx
[] = {
148 static char *specials
[] = {
149 "Rear Panel", "Drive Bar",
150 "Riser", "HDMI", "ATAPI",
151 "Mobile-In", "Mobile-Out"
154 cfg
= (cfg
& AC_DEFCFG_LOCATION
) >> AC_DEFCFG_LOCATION_SHIFT
;
155 if ((cfg
& 0x0f) < 7)
156 return bases
[cfg
& 0x0f];
157 for (i
= 0; i
< ARRAY_SIZE(specials_idx
); i
++) {
158 if (cfg
== specials_idx
[i
])
163 EXPORT_SYMBOL_GPL(snd_hda_get_jack_location
);
166 * snd_hda_get_jack_connectivity - Give a connectivity string of the jack
167 * @cfg: pin default config value
169 * Parse the pin default config value and returns the string of the
170 * jack connectivity, i.e. external or internal connection.
172 const char *snd_hda_get_jack_connectivity(u32 cfg
)
174 static char *jack_locations
[4] = { "Ext", "Int", "Sep", "Oth" };
176 return jack_locations
[(cfg
>> (AC_DEFCFG_LOCATION_SHIFT
+ 4)) & 3];
178 EXPORT_SYMBOL_GPL(snd_hda_get_jack_connectivity
);
181 * snd_hda_get_jack_type - Give a type string of the jack
182 * @cfg: pin default config value
184 * Parse the pin default config value and returns the string of the
185 * jack type, i.e. the purpose of the jack, such as Line-Out or CD.
187 const char *snd_hda_get_jack_type(u32 cfg
)
189 static char *jack_types
[16] = {
190 "Line Out", "Speaker", "HP Out", "CD",
191 "SPDIF Out", "Digital Out", "Modem Line", "Modem Hand",
192 "Line In", "Aux", "Mic", "Telephony",
193 "SPDIF In", "Digital In", "Reserved", "Other"
196 return jack_types
[(cfg
& AC_DEFCFG_DEVICE
)
197 >> AC_DEFCFG_DEVICE_SHIFT
];
199 EXPORT_SYMBOL_GPL(snd_hda_get_jack_type
);
202 * Compose a 32bit command word to be sent to the HD-audio controller
204 static inline unsigned int
205 make_codec_cmd(struct hda_codec
*codec
, hda_nid_t nid
, int flags
,
206 unsigned int verb
, unsigned int parm
)
210 if ((codec
->addr
& ~0xf) || (nid
& ~0x7f) ||
211 (verb
& ~0xfff) || (parm
& ~0xffff)) {
212 codec_err(codec
, "hda-codec: out of range cmd %x:%x:%x:%x\n",
213 codec
->addr
, nid
, verb
, parm
);
217 val
= (u32
)codec
->addr
<< 28;
218 val
|= (u32
)nid
<< 20;
225 * Send and receive a verb
227 static int codec_exec_verb(struct hda_codec
*codec
, unsigned int cmd
,
228 int flags
, unsigned int *res
)
230 struct hda_bus
*bus
= codec
->bus
;
239 snd_hda_power_up(codec
);
240 mutex_lock(&bus
->cmd_mutex
);
241 if (flags
& HDA_RW_NO_RESPONSE_FALLBACK
)
242 bus
->no_response_fallback
= 1;
244 trace_hda_send_cmd(codec
, cmd
);
245 err
= bus
->ops
.command(bus
, cmd
);
248 /* process pending verbs */
249 bus
->ops
.get_response(bus
, codec
->addr
);
252 *res
= bus
->ops
.get_response(bus
, codec
->addr
);
253 trace_hda_get_response(codec
, *res
);
255 bus
->no_response_fallback
= 0;
256 mutex_unlock(&bus
->cmd_mutex
);
257 snd_hda_power_down(codec
);
258 if (!codec_in_pm(codec
) && res
&& *res
== -1 && bus
->rirb_error
) {
259 if (bus
->response_reset
) {
261 "resetting BUS due to fatal communication error\n");
262 trace_hda_bus_reset(bus
);
263 bus
->ops
.bus_reset(bus
);
267 /* clear reset-flag when the communication gets recovered */
268 if (!err
|| codec_in_pm(codec
))
269 bus
->response_reset
= 0;
274 * snd_hda_codec_read - send a command and get the response
275 * @codec: the HDA codec
276 * @nid: NID to send the command
277 * @flags: optional bit flags
278 * @verb: the verb to send
279 * @parm: the parameter for the verb
281 * Send a single command and read the corresponding response.
283 * Returns the obtained response value, or -1 for an error.
285 unsigned int snd_hda_codec_read(struct hda_codec
*codec
, hda_nid_t nid
,
287 unsigned int verb
, unsigned int parm
)
289 unsigned cmd
= make_codec_cmd(codec
, nid
, flags
, verb
, parm
);
291 if (codec_exec_verb(codec
, cmd
, flags
, &res
))
295 EXPORT_SYMBOL_GPL(snd_hda_codec_read
);
298 * snd_hda_codec_write - send a single command without waiting for response
299 * @codec: the HDA codec
300 * @nid: NID to send the command
301 * @flags: optional bit flags
302 * @verb: the verb to send
303 * @parm: the parameter for the verb
305 * Send a single command without waiting for response.
307 * Returns 0 if successful, or a negative error code.
309 int snd_hda_codec_write(struct hda_codec
*codec
, hda_nid_t nid
, int flags
,
310 unsigned int verb
, unsigned int parm
)
312 unsigned int cmd
= make_codec_cmd(codec
, nid
, flags
, verb
, parm
);
314 return codec_exec_verb(codec
, cmd
, flags
,
315 codec
->bus
->sync_write
? &res
: NULL
);
317 EXPORT_SYMBOL_GPL(snd_hda_codec_write
);
320 * snd_hda_sequence_write - sequence writes
321 * @codec: the HDA codec
322 * @seq: VERB array to send
324 * Send the commands sequentially from the given array.
325 * The array must be terminated with NID=0.
327 void snd_hda_sequence_write(struct hda_codec
*codec
, const struct hda_verb
*seq
)
329 for (; seq
->nid
; seq
++)
330 snd_hda_codec_write(codec
, seq
->nid
, 0, seq
->verb
, seq
->param
);
332 EXPORT_SYMBOL_GPL(snd_hda_sequence_write
);
335 * snd_hda_get_sub_nodes - get the range of sub nodes
336 * @codec: the HDA codec
338 * @start_id: the pointer to store the start NID
340 * Parse the NID and store the start NID of its sub-nodes.
341 * Returns the number of sub-nodes.
343 int snd_hda_get_sub_nodes(struct hda_codec
*codec
, hda_nid_t nid
,
348 parm
= snd_hda_param_read(codec
, nid
, AC_PAR_NODE_COUNT
);
351 *start_id
= (parm
>> 16) & 0x7fff;
352 return (int)(parm
& 0x7fff);
354 EXPORT_SYMBOL_GPL(snd_hda_get_sub_nodes
);
356 /* connection list element */
357 struct hda_conn_list
{
358 struct list_head list
;
364 /* look up the cached results */
365 static struct hda_conn_list
*
366 lookup_conn_list(struct hda_codec
*codec
, hda_nid_t nid
)
368 struct hda_conn_list
*p
;
369 list_for_each_entry(p
, &codec
->conn_list
, list
) {
376 static int add_conn_list(struct hda_codec
*codec
, hda_nid_t nid
, int len
,
377 const hda_nid_t
*list
)
379 struct hda_conn_list
*p
;
381 p
= kmalloc(sizeof(*p
) + len
* sizeof(hda_nid_t
), GFP_KERNEL
);
386 memcpy(p
->conns
, list
, len
* sizeof(hda_nid_t
));
387 list_add(&p
->list
, &codec
->conn_list
);
391 static void remove_conn_list(struct hda_codec
*codec
)
393 while (!list_empty(&codec
->conn_list
)) {
394 struct hda_conn_list
*p
;
395 p
= list_first_entry(&codec
->conn_list
, typeof(*p
), list
);
401 /* read the connection and add to the cache */
402 static int read_and_add_raw_conns(struct hda_codec
*codec
, hda_nid_t nid
)
405 hda_nid_t
*result
= list
;
408 len
= snd_hda_get_raw_connections(codec
, nid
, list
, ARRAY_SIZE(list
));
409 if (len
== -ENOSPC
) {
410 len
= snd_hda_get_num_raw_conns(codec
, nid
);
411 result
= kmalloc(sizeof(hda_nid_t
) * len
, GFP_KERNEL
);
414 len
= snd_hda_get_raw_connections(codec
, nid
, result
, len
);
417 len
= snd_hda_override_conn_list(codec
, nid
, len
, result
);
424 * snd_hda_get_conn_list - get connection list
425 * @codec: the HDA codec
427 * @listp: the pointer to store NID list
429 * Parses the connection list of the given widget and stores the pointer
430 * to the list of NIDs.
432 * Returns the number of connections, or a negative error code.
434 * Note that the returned pointer isn't protected against the list
435 * modification. If snd_hda_override_conn_list() might be called
436 * concurrently, protect with a mutex appropriately.
438 int snd_hda_get_conn_list(struct hda_codec
*codec
, hda_nid_t nid
,
439 const hda_nid_t
**listp
)
445 const struct hda_conn_list
*p
;
447 /* if the connection-list is already cached, read it */
448 p
= lookup_conn_list(codec
, nid
);
454 if (snd_BUG_ON(added
))
457 err
= read_and_add_raw_conns(codec
, nid
);
463 EXPORT_SYMBOL_GPL(snd_hda_get_conn_list
);
466 * snd_hda_get_connections - copy connection list
467 * @codec: the HDA codec
469 * @conn_list: connection list array; when NULL, checks only the size
470 * @max_conns: max. number of connections to store
472 * Parses the connection list of the given widget and stores the list
475 * Returns the number of connections, or a negative error code.
477 int snd_hda_get_connections(struct hda_codec
*codec
, hda_nid_t nid
,
478 hda_nid_t
*conn_list
, int max_conns
)
480 const hda_nid_t
*list
;
481 int len
= snd_hda_get_conn_list(codec
, nid
, &list
);
483 if (len
> 0 && conn_list
) {
484 if (len
> max_conns
) {
485 codec_err(codec
, "Too many connections %d for NID 0x%x\n",
489 memcpy(conn_list
, list
, len
* sizeof(hda_nid_t
));
494 EXPORT_SYMBOL_GPL(snd_hda_get_connections
);
496 /* return CONNLIST_LEN parameter of the given widget */
497 static unsigned int get_num_conns(struct hda_codec
*codec
, hda_nid_t nid
)
499 unsigned int wcaps
= get_wcaps(codec
, nid
);
502 if (!(wcaps
& AC_WCAP_CONN_LIST
) &&
503 get_wcaps_type(wcaps
) != AC_WID_VOL_KNB
)
506 parm
= snd_hda_param_read(codec
, nid
, AC_PAR_CONNLIST_LEN
);
512 int snd_hda_get_num_raw_conns(struct hda_codec
*codec
, hda_nid_t nid
)
514 return snd_hda_get_raw_connections(codec
, nid
, NULL
, 0);
518 * snd_hda_get_raw_connections - copy connection list without cache
519 * @codec: the HDA codec
521 * @conn_list: connection list array
522 * @max_conns: max. number of connections to store
524 * Like snd_hda_get_connections(), copy the connection list but without
525 * checking through the connection-list cache.
526 * Currently called only from hda_proc.c, so not exported.
528 int snd_hda_get_raw_connections(struct hda_codec
*codec
, hda_nid_t nid
,
529 hda_nid_t
*conn_list
, int max_conns
)
532 int i
, conn_len
, conns
;
533 unsigned int shift
, num_elems
, mask
;
537 parm
= get_num_conns(codec
, nid
);
541 if (parm
& AC_CLIST_LONG
) {
550 conn_len
= parm
& AC_CLIST_LENGTH
;
551 mask
= (1 << (shift
-1)) - 1;
554 return 0; /* no connection */
557 /* single connection */
558 parm
= snd_hda_codec_read(codec
, nid
, 0,
559 AC_VERB_GET_CONNECT_LIST
, 0);
560 if (parm
== -1 && codec
->bus
->rirb_error
)
563 conn_list
[0] = parm
& mask
;
567 /* multi connection */
570 for (i
= 0; i
< conn_len
; i
++) {
574 if (i
% num_elems
== 0) {
575 parm
= snd_hda_codec_read(codec
, nid
, 0,
576 AC_VERB_GET_CONNECT_LIST
, i
);
577 if (parm
== -1 && codec
->bus
->rirb_error
)
580 range_val
= !!(parm
& (1 << (shift
-1))); /* ranges */
582 if (val
== 0 && null_count
++) { /* no second chance */
584 "invalid CONNECT_LIST verb %x[%i]:%x\n",
590 /* ranges between the previous and this one */
591 if (!prev_nid
|| prev_nid
>= val
) {
593 "invalid dep_range_val %x:%x\n",
597 for (n
= prev_nid
+ 1; n
<= val
; n
++) {
599 if (conns
>= max_conns
)
601 conn_list
[conns
] = n
;
607 if (conns
>= max_conns
)
609 conn_list
[conns
] = val
;
619 * snd_hda_override_conn_list - add/modify the connection-list to cache
620 * @codec: the HDA codec
622 * @len: number of connection list entries
623 * @list: the list of connection entries
625 * Add or modify the given connection-list to the cache. If the corresponding
626 * cache already exists, invalidate it and append a new one.
628 * Returns zero or a negative error code.
630 int snd_hda_override_conn_list(struct hda_codec
*codec
, hda_nid_t nid
, int len
,
631 const hda_nid_t
*list
)
633 struct hda_conn_list
*p
;
635 p
= lookup_conn_list(codec
, nid
);
641 return add_conn_list(codec
, nid
, len
, list
);
643 EXPORT_SYMBOL_GPL(snd_hda_override_conn_list
);
646 * snd_hda_get_conn_index - get the connection index of the given NID
647 * @codec: the HDA codec
648 * @mux: NID containing the list
649 * @nid: NID to select
650 * @recursive: 1 when searching NID recursively, otherwise 0
652 * Parses the connection list of the widget @mux and checks whether the
653 * widget @nid is present. If it is, return the connection index.
654 * Otherwise it returns -1.
656 int snd_hda_get_conn_index(struct hda_codec
*codec
, hda_nid_t mux
,
657 hda_nid_t nid
, int recursive
)
659 const hda_nid_t
*conn
;
662 nums
= snd_hda_get_conn_list(codec
, mux
, &conn
);
663 for (i
= 0; i
< nums
; i
++)
668 if (recursive
> 10) {
669 codec_dbg(codec
, "too deep connection for 0x%x\n", nid
);
673 for (i
= 0; i
< nums
; i
++) {
674 unsigned int type
= get_wcaps_type(get_wcaps(codec
, conn
[i
]));
675 if (type
== AC_WID_PIN
|| type
== AC_WID_AUD_OUT
)
677 if (snd_hda_get_conn_index(codec
, conn
[i
], nid
, recursive
) >= 0)
682 EXPORT_SYMBOL_GPL(snd_hda_get_conn_index
);
685 /* return DEVLIST_LEN parameter of the given widget */
686 static unsigned int get_num_devices(struct hda_codec
*codec
, hda_nid_t nid
)
688 unsigned int wcaps
= get_wcaps(codec
, nid
);
691 if (!codec
->dp_mst
|| !(wcaps
& AC_WCAP_DIGITAL
) ||
692 get_wcaps_type(wcaps
) != AC_WID_PIN
)
695 parm
= snd_hda_param_read(codec
, nid
, AC_PAR_DEVLIST_LEN
);
696 if (parm
== -1 && codec
->bus
->rirb_error
)
698 return parm
& AC_DEV_LIST_LEN_MASK
;
702 * snd_hda_get_devices - copy device list without cache
703 * @codec: the HDA codec
704 * @nid: NID of the pin to parse
705 * @dev_list: device list array
706 * @max_devices: max. number of devices to store
708 * Copy the device list. This info is dynamic and so not cached.
709 * Currently called only from hda_proc.c, so not exported.
711 int snd_hda_get_devices(struct hda_codec
*codec
, hda_nid_t nid
,
712 u8
*dev_list
, int max_devices
)
715 int i
, dev_len
, devices
;
717 parm
= get_num_devices(codec
, nid
);
718 if (!parm
) /* not multi-stream capable */
722 dev_len
= dev_len
< max_devices
? dev_len
: max_devices
;
725 while (devices
< dev_len
) {
726 parm
= snd_hda_codec_read(codec
, nid
, 0,
727 AC_VERB_GET_DEVICE_LIST
, devices
);
728 if (parm
== -1 && codec
->bus
->rirb_error
)
731 for (i
= 0; i
< 8; i
++) {
732 dev_list
[devices
] = (u8
)parm
;
735 if (devices
>= dev_len
)
743 * snd_hda_queue_unsol_event - add an unsolicited event to queue
745 * @res: unsolicited event (lower 32bit of RIRB entry)
746 * @res_ex: codec addr and flags (upper 32bit or RIRB entry)
748 * Adds the given event to the queue. The events are processed in
749 * the workqueue asynchronously. Call this function in the interrupt
750 * hanlder when RIRB receives an unsolicited event.
752 * Returns 0 if successful, or a negative error code.
754 int snd_hda_queue_unsol_event(struct hda_bus
*bus
, u32 res
, u32 res_ex
)
756 struct hda_bus_unsolicited
*unsol
;
759 if (!bus
|| !bus
->workq
)
762 trace_hda_unsol_event(bus
, res
, res_ex
);
767 wp
= (unsol
->wp
+ 1) % HDA_UNSOL_QUEUE_SIZE
;
771 unsol
->queue
[wp
] = res
;
772 unsol
->queue
[wp
+ 1] = res_ex
;
774 queue_work(bus
->workq
, &unsol
->work
);
778 EXPORT_SYMBOL_GPL(snd_hda_queue_unsol_event
);
781 * process queued unsolicited events
783 static void process_unsol_events(struct work_struct
*work
)
785 struct hda_bus_unsolicited
*unsol
=
786 container_of(work
, struct hda_bus_unsolicited
, work
);
787 struct hda_bus
*bus
= unsol
->bus
;
788 struct hda_codec
*codec
;
789 unsigned int rp
, caddr
, res
;
791 while (unsol
->rp
!= unsol
->wp
) {
792 rp
= (unsol
->rp
+ 1) % HDA_UNSOL_QUEUE_SIZE
;
795 res
= unsol
->queue
[rp
];
796 caddr
= unsol
->queue
[rp
+ 1];
797 if (!(caddr
& (1 << 4))) /* no unsolicited event? */
799 codec
= bus
->caddr_tbl
[caddr
& 0x0f];
800 if (codec
&& codec
->patch_ops
.unsol_event
)
801 codec
->patch_ops
.unsol_event(codec
, res
);
806 * initialize unsolicited queue
808 static int init_unsol_queue(struct hda_bus
*bus
)
810 struct hda_bus_unsolicited
*unsol
;
812 if (bus
->unsol
) /* already initialized */
815 unsol
= kzalloc(sizeof(*unsol
), GFP_KERNEL
);
817 dev_err(bus
->card
->dev
, "can't allocate unsolicited queue\n");
820 INIT_WORK(&unsol
->work
, process_unsol_events
);
829 static void snd_hda_bus_free(struct hda_bus
*bus
)
834 WARN_ON(!list_empty(&bus
->codec_list
));
836 flush_workqueue(bus
->workq
);
838 if (bus
->ops
.private_free
)
839 bus
->ops
.private_free(bus
);
841 destroy_workqueue(bus
->workq
);
846 static int snd_hda_bus_dev_free(struct snd_device
*device
)
848 snd_hda_bus_free(device
->device_data
);
852 static int snd_hda_bus_dev_disconnect(struct snd_device
*device
)
854 struct hda_bus
*bus
= device
->device_data
;
860 * snd_hda_bus_new - create a HDA bus
861 * @card: the card entry
862 * @temp: the template for hda_bus information
863 * @busp: the pointer to store the created bus instance
865 * Returns 0 if successful, or a negative error code.
867 int snd_hda_bus_new(struct snd_card
*card
,
868 const struct hda_bus_template
*temp
,
869 struct hda_bus
**busp
)
873 static struct snd_device_ops dev_ops
= {
874 .dev_disconnect
= snd_hda_bus_dev_disconnect
,
875 .dev_free
= snd_hda_bus_dev_free
,
878 if (snd_BUG_ON(!temp
))
880 if (snd_BUG_ON(!temp
->ops
.command
|| !temp
->ops
.get_response
))
886 bus
= kzalloc(sizeof(*bus
), GFP_KERNEL
);
888 dev_err(card
->dev
, "can't allocate struct hda_bus\n");
893 bus
->private_data
= temp
->private_data
;
894 bus
->pci
= temp
->pci
;
895 bus
->modelname
= temp
->modelname
;
896 bus
->power_save
= temp
->power_save
;
897 bus
->ops
= temp
->ops
;
899 mutex_init(&bus
->cmd_mutex
);
900 mutex_init(&bus
->prepare_mutex
);
901 INIT_LIST_HEAD(&bus
->codec_list
);
903 snprintf(bus
->workq_name
, sizeof(bus
->workq_name
),
904 "hd-audio%d", card
->number
);
905 bus
->workq
= create_singlethread_workqueue(bus
->workq_name
);
907 dev_err(card
->dev
, "cannot create workqueue %s\n",
913 err
= snd_device_new(card
, SNDRV_DEV_BUS
, bus
, &dev_ops
);
915 snd_hda_bus_free(bus
);
922 EXPORT_SYMBOL_GPL(snd_hda_bus_new
);
924 #if IS_ENABLED(CONFIG_SND_HDA_GENERIC)
925 #define is_generic_config(codec) \
926 (codec->modelname && !strcmp(codec->modelname, "generic"))
928 #define is_generic_config(codec) 0
932 #define HDA_MODREQ_MAX_COUNT 2 /* two request_modules()'s */
934 #define HDA_MODREQ_MAX_COUNT 0 /* all presets are statically linked */
938 * find a matching codec preset
940 static const struct hda_codec_preset
*
941 find_codec_preset(struct hda_codec
*codec
)
943 struct hda_codec_preset_list
*tbl
;
944 const struct hda_codec_preset
*preset
;
945 unsigned int mod_requested
= 0;
948 mutex_lock(&preset_mutex
);
949 list_for_each_entry(tbl
, &hda_preset_tables
, list
) {
950 if (!try_module_get(tbl
->owner
)) {
951 codec_err(codec
, "cannot module_get\n");
954 for (preset
= tbl
->preset
; preset
->id
; preset
++) {
955 u32 mask
= preset
->mask
;
956 if (preset
->afg
&& preset
->afg
!= codec
->afg
)
958 if (preset
->mfg
&& preset
->mfg
!= codec
->mfg
)
962 if (preset
->id
== (codec
->vendor_id
& mask
) &&
964 preset
->rev
== codec
->revision_id
)) {
965 mutex_unlock(&preset_mutex
);
966 codec
->owner
= tbl
->owner
;
970 module_put(tbl
->owner
);
972 mutex_unlock(&preset_mutex
);
974 if (mod_requested
< HDA_MODREQ_MAX_COUNT
) {
976 request_module("snd-hda-codec-id:%08x",
979 request_module("snd-hda-codec-id:%04x*",
980 (codec
->vendor_id
>> 16) & 0xffff);
988 * get_codec_name - store the codec name
990 static int get_codec_name(struct hda_codec
*codec
)
992 const struct hda_vendor_id
*c
;
993 const char *vendor
= NULL
;
994 u16 vendor_id
= codec
->vendor_id
>> 16;
997 if (codec
->vendor_name
)
1000 for (c
= hda_vendor_ids
; c
->id
; c
++) {
1001 if (c
->id
== vendor_id
) {
1007 sprintf(tmp
, "Generic %04x", vendor_id
);
1010 codec
->vendor_name
= kstrdup(vendor
, GFP_KERNEL
);
1011 if (!codec
->vendor_name
)
1015 if (codec
->chip_name
)
1018 if (codec
->preset
&& codec
->preset
->name
)
1019 codec
->chip_name
= kstrdup(codec
->preset
->name
, GFP_KERNEL
);
1021 sprintf(tmp
, "ID %x", codec
->vendor_id
& 0xffff);
1022 codec
->chip_name
= kstrdup(tmp
, GFP_KERNEL
);
1024 if (!codec
->chip_name
)
1030 * look for an AFG and MFG nodes
1032 static void setup_fg_nodes(struct hda_codec
*codec
)
1034 int i
, total_nodes
, function_id
;
1037 total_nodes
= snd_hda_get_sub_nodes(codec
, AC_NODE_ROOT
, &nid
);
1038 for (i
= 0; i
< total_nodes
; i
++, nid
++) {
1039 function_id
= snd_hda_param_read(codec
, nid
,
1040 AC_PAR_FUNCTION_TYPE
);
1041 switch (function_id
& 0xff) {
1042 case AC_GRP_AUDIO_FUNCTION
:
1044 codec
->afg_function_id
= function_id
& 0xff;
1045 codec
->afg_unsol
= (function_id
>> 8) & 1;
1047 case AC_GRP_MODEM_FUNCTION
:
1049 codec
->mfg_function_id
= function_id
& 0xff;
1050 codec
->mfg_unsol
= (function_id
>> 8) & 1;
1059 * read widget caps for each widget and store in cache
1061 static int read_widget_caps(struct hda_codec
*codec
, hda_nid_t fg_node
)
1066 codec
->num_nodes
= snd_hda_get_sub_nodes(codec
, fg_node
,
1068 codec
->wcaps
= kmalloc(codec
->num_nodes
* 4, GFP_KERNEL
);
1071 nid
= codec
->start_nid
;
1072 for (i
= 0; i
< codec
->num_nodes
; i
++, nid
++)
1073 codec
->wcaps
[i
] = snd_hda_param_read(codec
, nid
,
1074 AC_PAR_AUDIO_WIDGET_CAP
);
1078 /* read all pin default configurations and save codec->init_pins */
1079 static int read_pin_defaults(struct hda_codec
*codec
)
1082 hda_nid_t nid
= codec
->start_nid
;
1084 for (i
= 0; i
< codec
->num_nodes
; i
++, nid
++) {
1085 struct hda_pincfg
*pin
;
1086 unsigned int wcaps
= get_wcaps(codec
, nid
);
1087 unsigned int wid_type
= get_wcaps_type(wcaps
);
1088 if (wid_type
!= AC_WID_PIN
)
1090 pin
= snd_array_new(&codec
->init_pins
);
1094 pin
->cfg
= snd_hda_codec_read(codec
, nid
, 0,
1095 AC_VERB_GET_CONFIG_DEFAULT
, 0);
1096 pin
->ctrl
= snd_hda_codec_read(codec
, nid
, 0,
1097 AC_VERB_GET_PIN_WIDGET_CONTROL
,
1103 /* look up the given pin config list and return the item matching with NID */
1104 static struct hda_pincfg
*look_up_pincfg(struct hda_codec
*codec
,
1105 struct snd_array
*array
,
1109 for (i
= 0; i
< array
->used
; i
++) {
1110 struct hda_pincfg
*pin
= snd_array_elem(array
, i
);
1111 if (pin
->nid
== nid
)
1117 /* set the current pin config value for the given NID.
1118 * the value is cached, and read via snd_hda_codec_get_pincfg()
1120 int snd_hda_add_pincfg(struct hda_codec
*codec
, struct snd_array
*list
,
1121 hda_nid_t nid
, unsigned int cfg
)
1123 struct hda_pincfg
*pin
;
1125 /* the check below may be invalid when pins are added by a fixup
1126 * dynamically (e.g. via snd_hda_codec_update_widgets()), so disabled
1130 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN)
1134 pin
= look_up_pincfg(codec
, list
, nid
);
1136 pin
= snd_array_new(list
);
1146 * snd_hda_codec_set_pincfg - Override a pin default configuration
1147 * @codec: the HDA codec
1148 * @nid: NID to set the pin config
1149 * @cfg: the pin default config value
1151 * Override a pin default configuration value in the cache.
1152 * This value can be read by snd_hda_codec_get_pincfg() in a higher
1153 * priority than the real hardware value.
1155 int snd_hda_codec_set_pincfg(struct hda_codec
*codec
,
1156 hda_nid_t nid
, unsigned int cfg
)
1158 return snd_hda_add_pincfg(codec
, &codec
->driver_pins
, nid
, cfg
);
1160 EXPORT_SYMBOL_GPL(snd_hda_codec_set_pincfg
);
1163 * snd_hda_codec_get_pincfg - Obtain a pin-default configuration
1164 * @codec: the HDA codec
1165 * @nid: NID to get the pin config
1167 * Get the current pin config value of the given pin NID.
1168 * If the pincfg value is cached or overridden via sysfs or driver,
1169 * returns the cached value.
1171 unsigned int snd_hda_codec_get_pincfg(struct hda_codec
*codec
, hda_nid_t nid
)
1173 struct hda_pincfg
*pin
;
1175 #ifdef CONFIG_SND_HDA_RECONFIG
1177 unsigned int cfg
= 0;
1178 mutex_lock(&codec
->user_mutex
);
1179 pin
= look_up_pincfg(codec
, &codec
->user_pins
, nid
);
1182 mutex_unlock(&codec
->user_mutex
);
1187 pin
= look_up_pincfg(codec
, &codec
->driver_pins
, nid
);
1190 pin
= look_up_pincfg(codec
, &codec
->init_pins
, nid
);
1195 EXPORT_SYMBOL_GPL(snd_hda_codec_get_pincfg
);
1198 * snd_hda_codec_set_pin_target - remember the current pinctl target value
1199 * @codec: the HDA codec
1201 * @val: assigned pinctl value
1203 * This function stores the given value to a pinctl target value in the
1204 * pincfg table. This isn't always as same as the actually written value
1205 * but can be referred at any time via snd_hda_codec_get_pin_target().
1207 int snd_hda_codec_set_pin_target(struct hda_codec
*codec
, hda_nid_t nid
,
1210 struct hda_pincfg
*pin
;
1212 pin
= look_up_pincfg(codec
, &codec
->init_pins
, nid
);
1218 EXPORT_SYMBOL_GPL(snd_hda_codec_set_pin_target
);
1221 * snd_hda_codec_get_pin_target - return the current pinctl target value
1222 * @codec: the HDA codec
1225 int snd_hda_codec_get_pin_target(struct hda_codec
*codec
, hda_nid_t nid
)
1227 struct hda_pincfg
*pin
;
1229 pin
= look_up_pincfg(codec
, &codec
->init_pins
, nid
);
1234 EXPORT_SYMBOL_GPL(snd_hda_codec_get_pin_target
);
1237 * snd_hda_shutup_pins - Shut up all pins
1238 * @codec: the HDA codec
1240 * Clear all pin controls to shup up before suspend for avoiding click noise.
1241 * The controls aren't cached so that they can be resumed properly.
1243 void snd_hda_shutup_pins(struct hda_codec
*codec
)
1246 /* don't shut up pins when unloading the driver; otherwise it breaks
1247 * the default pin setup at the next load of the driver
1249 if (codec
->bus
->shutdown
)
1251 for (i
= 0; i
< codec
->init_pins
.used
; i
++) {
1252 struct hda_pincfg
*pin
= snd_array_elem(&codec
->init_pins
, i
);
1253 /* use read here for syncing after issuing each verb */
1254 snd_hda_codec_read(codec
, pin
->nid
, 0,
1255 AC_VERB_SET_PIN_WIDGET_CONTROL
, 0);
1257 codec
->pins_shutup
= 1;
1259 EXPORT_SYMBOL_GPL(snd_hda_shutup_pins
);
1262 /* Restore the pin controls cleared previously via snd_hda_shutup_pins() */
1263 static void restore_shutup_pins(struct hda_codec
*codec
)
1266 if (!codec
->pins_shutup
)
1268 if (codec
->bus
->shutdown
)
1270 for (i
= 0; i
< codec
->init_pins
.used
; i
++) {
1271 struct hda_pincfg
*pin
= snd_array_elem(&codec
->init_pins
, i
);
1272 snd_hda_codec_write(codec
, pin
->nid
, 0,
1273 AC_VERB_SET_PIN_WIDGET_CONTROL
,
1276 codec
->pins_shutup
= 0;
1280 static void hda_jackpoll_work(struct work_struct
*work
)
1282 struct hda_codec
*codec
=
1283 container_of(work
, struct hda_codec
, jackpoll_work
.work
);
1285 snd_hda_jack_set_dirty_all(codec
);
1286 snd_hda_jack_poll_all(codec
);
1288 if (!codec
->jackpoll_interval
)
1291 queue_delayed_work(codec
->bus
->workq
, &codec
->jackpoll_work
,
1292 codec
->jackpoll_interval
);
1295 static void init_hda_cache(struct hda_cache_rec
*cache
,
1296 unsigned int record_size
);
1297 static void free_hda_cache(struct hda_cache_rec
*cache
);
1299 /* release all pincfg lists */
1300 static void free_init_pincfgs(struct hda_codec
*codec
)
1302 snd_array_free(&codec
->driver_pins
);
1303 #ifdef CONFIG_SND_HDA_RECONFIG
1304 snd_array_free(&codec
->user_pins
);
1306 snd_array_free(&codec
->init_pins
);
1310 * audio-converter setup caches
1312 struct hda_cvt_setup
{
1317 unsigned char active
; /* cvt is currently used */
1318 unsigned char dirty
; /* setups should be cleared */
1321 /* get or create a cache entry for the given audio converter NID */
1322 static struct hda_cvt_setup
*
1323 get_hda_cvt_setup(struct hda_codec
*codec
, hda_nid_t nid
)
1325 struct hda_cvt_setup
*p
;
1328 for (i
= 0; i
< codec
->cvt_setups
.used
; i
++) {
1329 p
= snd_array_elem(&codec
->cvt_setups
, i
);
1333 p
= snd_array_new(&codec
->cvt_setups
);
1340 * Dynamic symbol binding for the codec parsers
1343 #define load_parser(codec, sym) \
1344 ((codec)->parser = (int (*)(struct hda_codec *))symbol_request(sym))
1346 static void unload_parser(struct hda_codec
*codec
)
1349 symbol_put_addr(codec
->parser
);
1350 codec
->parser
= NULL
;
1356 static void snd_hda_codec_free(struct hda_codec
*codec
)
1360 cancel_delayed_work_sync(&codec
->jackpoll_work
);
1361 snd_hda_jack_tbl_clear(codec
);
1362 free_init_pincfgs(codec
);
1364 cancel_delayed_work(&codec
->power_work
);
1365 flush_workqueue(codec
->bus
->workq
);
1367 list_del(&codec
->list
);
1368 snd_array_free(&codec
->mixers
);
1369 snd_array_free(&codec
->nids
);
1370 snd_array_free(&codec
->cvt_setups
);
1371 snd_array_free(&codec
->spdif_out
);
1372 remove_conn_list(codec
);
1373 codec
->bus
->caddr_tbl
[codec
->addr
] = NULL
;
1374 if (codec
->patch_ops
.free
)
1375 codec
->patch_ops
.free(codec
);
1376 hda_call_pm_notify(codec
, false); /* cancel leftover refcounts */
1377 snd_hda_sysfs_clear(codec
);
1378 unload_parser(codec
);
1379 module_put(codec
->owner
);
1380 free_hda_cache(&codec
->amp_cache
);
1381 free_hda_cache(&codec
->cmd_cache
);
1382 kfree(codec
->vendor_name
);
1383 kfree(codec
->chip_name
);
1384 kfree(codec
->modelname
);
1385 kfree(codec
->wcaps
);
1386 codec
->bus
->num_codecs
--;
1387 put_device(&codec
->dev
);
1390 static bool snd_hda_codec_get_supported_ps(struct hda_codec
*codec
,
1391 hda_nid_t fg
, unsigned int power_state
);
1393 static unsigned int hda_set_power_state(struct hda_codec
*codec
,
1394 unsigned int power_state
);
1396 static int snd_hda_codec_dev_register(struct snd_device
*device
)
1398 struct hda_codec
*codec
= device
->device_data
;
1399 int err
= device_add(&codec
->dev
);
1403 snd_hda_register_beep_device(codec
);
1407 static int snd_hda_codec_dev_disconnect(struct snd_device
*device
)
1409 struct hda_codec
*codec
= device
->device_data
;
1411 snd_hda_detach_beep_device(codec
);
1412 device_del(&codec
->dev
);
1416 static int snd_hda_codec_dev_free(struct snd_device
*device
)
1418 snd_hda_codec_free(device
->device_data
);
1422 /* just free the container */
1423 static void snd_hda_codec_dev_release(struct device
*dev
)
1425 kfree(container_of(dev
, struct hda_codec
, dev
));
1429 * snd_hda_codec_new - create a HDA codec
1430 * @bus: the bus to assign
1431 * @codec_addr: the codec address
1432 * @codecp: the pointer to store the generated codec
1434 * Returns 0 if successful, or a negative error code.
1436 int snd_hda_codec_new(struct hda_bus
*bus
,
1437 unsigned int codec_addr
,
1438 struct hda_codec
**codecp
)
1440 struct hda_codec
*codec
;
1444 static struct snd_device_ops dev_ops
= {
1445 .dev_register
= snd_hda_codec_dev_register
,
1446 .dev_disconnect
= snd_hda_codec_dev_disconnect
,
1447 .dev_free
= snd_hda_codec_dev_free
,
1450 if (snd_BUG_ON(!bus
))
1452 if (snd_BUG_ON(codec_addr
> HDA_MAX_CODEC_ADDRESS
))
1455 if (bus
->caddr_tbl
[codec_addr
]) {
1456 dev_err(bus
->card
->dev
,
1457 "address 0x%x is already occupied\n",
1462 codec
= kzalloc(sizeof(*codec
), GFP_KERNEL
);
1463 if (codec
== NULL
) {
1464 dev_err(bus
->card
->dev
, "can't allocate struct hda_codec\n");
1468 device_initialize(&codec
->dev
);
1469 codec
->dev
.parent
= &bus
->card
->card_dev
;
1470 codec
->dev
.class = sound_class
;
1471 codec
->dev
.release
= snd_hda_codec_dev_release
;
1472 codec
->dev
.groups
= snd_hda_dev_attr_groups
;
1473 dev_set_name(&codec
->dev
, "hdaudioC%dD%d", bus
->card
->number
,
1475 dev_set_drvdata(&codec
->dev
, codec
); /* for sysfs */
1478 codec
->addr
= codec_addr
;
1479 mutex_init(&codec
->spdif_mutex
);
1480 mutex_init(&codec
->control_mutex
);
1481 mutex_init(&codec
->hash_mutex
);
1482 init_hda_cache(&codec
->amp_cache
, sizeof(struct hda_amp_info
));
1483 init_hda_cache(&codec
->cmd_cache
, sizeof(struct hda_cache_head
));
1484 snd_array_init(&codec
->mixers
, sizeof(struct hda_nid_item
), 32);
1485 snd_array_init(&codec
->nids
, sizeof(struct hda_nid_item
), 32);
1486 snd_array_init(&codec
->init_pins
, sizeof(struct hda_pincfg
), 16);
1487 snd_array_init(&codec
->driver_pins
, sizeof(struct hda_pincfg
), 16);
1488 snd_array_init(&codec
->cvt_setups
, sizeof(struct hda_cvt_setup
), 8);
1489 snd_array_init(&codec
->spdif_out
, sizeof(struct hda_spdif_out
), 16);
1490 snd_array_init(&codec
->jacktbl
, sizeof(struct hda_jack_tbl
), 16);
1491 snd_array_init(&codec
->verbs
, sizeof(struct hda_verb
*), 8);
1492 INIT_LIST_HEAD(&codec
->conn_list
);
1494 INIT_DELAYED_WORK(&codec
->jackpoll_work
, hda_jackpoll_work
);
1495 codec
->depop_delay
= -1;
1496 codec
->fixup_id
= HDA_FIXUP_ID_NOT_SET
;
1499 spin_lock_init(&codec
->power_lock
);
1500 INIT_DELAYED_WORK(&codec
->power_work
, hda_power_work
);
1501 /* snd_hda_codec_new() marks the codec as power-up, and leave it as is.
1502 * the caller has to power down appropriatley after initialization
1505 hda_keep_power_on(codec
);
1508 snd_hda_sysfs_init(codec
);
1510 if (codec
->bus
->modelname
) {
1511 codec
->modelname
= kstrdup(codec
->bus
->modelname
, GFP_KERNEL
);
1512 if (!codec
->modelname
) {
1518 list_add_tail(&codec
->list
, &bus
->codec_list
);
1521 bus
->caddr_tbl
[codec_addr
] = codec
;
1523 codec
->vendor_id
= snd_hda_param_read(codec
, AC_NODE_ROOT
,
1525 if (codec
->vendor_id
== -1)
1526 /* read again, hopefully the access method was corrected
1527 * in the last read...
1529 codec
->vendor_id
= snd_hda_param_read(codec
, AC_NODE_ROOT
,
1531 codec
->subsystem_id
= snd_hda_param_read(codec
, AC_NODE_ROOT
,
1532 AC_PAR_SUBSYSTEM_ID
);
1533 codec
->revision_id
= snd_hda_param_read(codec
, AC_NODE_ROOT
,
1536 setup_fg_nodes(codec
);
1537 if (!codec
->afg
&& !codec
->mfg
) {
1538 dev_err(bus
->card
->dev
, "no AFG or MFG node found\n");
1543 fg
= codec
->afg
? codec
->afg
: codec
->mfg
;
1544 err
= read_widget_caps(codec
, fg
);
1546 dev_err(bus
->card
->dev
, "cannot malloc\n");
1549 err
= read_pin_defaults(codec
);
1553 if (!codec
->subsystem_id
) {
1554 codec
->subsystem_id
=
1555 snd_hda_codec_read(codec
, fg
, 0,
1556 AC_VERB_GET_SUBSYSTEM_ID
, 0);
1560 codec
->d3_stop_clk
= snd_hda_codec_get_supported_ps(codec
, fg
,
1563 codec
->epss
= snd_hda_codec_get_supported_ps(codec
, fg
,
1566 if (!codec
->d3_stop_clk
|| !codec
->epss
)
1567 bus
->power_keep_link_on
= 1;
1571 /* power-up all before initialization */
1572 hda_set_power_state(codec
, AC_PWRST_D0
);
1574 snd_hda_codec_proc_new(codec
);
1576 snd_hda_create_hwdep(codec
);
1578 sprintf(component
, "HDA:%08x,%08x,%08x", codec
->vendor_id
,
1579 codec
->subsystem_id
, codec
->revision_id
);
1580 snd_component_add(codec
->bus
->card
, component
);
1582 err
= snd_device_new(bus
->card
, SNDRV_DEV_CODEC
, codec
, &dev_ops
);
1591 snd_hda_codec_free(codec
);
1594 EXPORT_SYMBOL_GPL(snd_hda_codec_new
);
1597 * snd_hda_codec_update_widgets - Refresh widget caps and pin defaults
1598 * @codec: the HDA codec
1600 * Forcibly refresh the all widget caps and the init pin configurations of
1603 int snd_hda_codec_update_widgets(struct hda_codec
*codec
)
1608 /* Assume the function group node does not change,
1609 * only the widget nodes may change.
1611 kfree(codec
->wcaps
);
1612 fg
= codec
->afg
? codec
->afg
: codec
->mfg
;
1613 err
= read_widget_caps(codec
, fg
);
1615 codec_err(codec
, "cannot malloc\n");
1619 snd_array_free(&codec
->init_pins
);
1620 err
= read_pin_defaults(codec
);
1624 EXPORT_SYMBOL_GPL(snd_hda_codec_update_widgets
);
1627 #if IS_ENABLED(CONFIG_SND_HDA_CODEC_HDMI)
1628 /* if all audio out widgets are digital, let's assume the codec as a HDMI/DP */
1629 static bool is_likely_hdmi_codec(struct hda_codec
*codec
)
1631 hda_nid_t nid
= codec
->start_nid
;
1634 for (i
= 0; i
< codec
->num_nodes
; i
++, nid
++) {
1635 unsigned int wcaps
= get_wcaps(codec
, nid
);
1636 switch (get_wcaps_type(wcaps
)) {
1638 return false; /* HDMI parser supports only HDMI out */
1639 case AC_WID_AUD_OUT
:
1640 if (!(wcaps
& AC_WCAP_DIGITAL
))
1648 /* no HDMI codec parser support */
1649 #define is_likely_hdmi_codec(codec) false
1650 #endif /* CONFIG_SND_HDA_CODEC_HDMI */
1653 * snd_hda_codec_configure - (Re-)configure the HD-audio codec
1654 * @codec: the HDA codec
1656 * Start parsing of the given codec tree and (re-)initialize the whole
1659 * Returns 0 if successful or a negative error code.
1661 int snd_hda_codec_configure(struct hda_codec
*codec
)
1663 int (*patch
)(struct hda_codec
*) = NULL
;
1666 codec
->preset
= find_codec_preset(codec
);
1667 if (!codec
->vendor_name
|| !codec
->chip_name
) {
1668 err
= get_codec_name(codec
);
1673 if (!is_generic_config(codec
) && codec
->preset
)
1674 patch
= codec
->preset
->patch
;
1676 unload_parser(codec
); /* to be sure */
1677 if (is_likely_hdmi_codec(codec
)) {
1678 #if IS_MODULE(CONFIG_SND_HDA_CODEC_HDMI)
1679 patch
= load_parser(codec
, snd_hda_parse_hdmi_codec
);
1680 #elif IS_BUILTIN(CONFIG_SND_HDA_CODEC_HDMI)
1681 patch
= snd_hda_parse_hdmi_codec
;
1685 #if IS_MODULE(CONFIG_SND_HDA_GENERIC)
1686 patch
= load_parser(codec
, snd_hda_parse_generic_codec
);
1687 #elif IS_BUILTIN(CONFIG_SND_HDA_GENERIC)
1688 patch
= snd_hda_parse_generic_codec
;
1692 codec_err(codec
, "No codec parser is available\n");
1699 unload_parser(codec
);
1703 if (codec
->patch_ops
.unsol_event
) {
1704 err
= init_unsol_queue(codec
->bus
);
1709 /* audio codec should override the mixer name */
1710 if (codec
->afg
|| !*codec
->bus
->card
->mixername
)
1711 snprintf(codec
->bus
->card
->mixername
,
1712 sizeof(codec
->bus
->card
->mixername
),
1713 "%s %s", codec
->vendor_name
, codec
->chip_name
);
1716 EXPORT_SYMBOL_GPL(snd_hda_codec_configure
);
1718 /* update the stream-id if changed */
1719 static void update_pcm_stream_id(struct hda_codec
*codec
,
1720 struct hda_cvt_setup
*p
, hda_nid_t nid
,
1721 u32 stream_tag
, int channel_id
)
1723 unsigned int oldval
, newval
;
1725 if (p
->stream_tag
!= stream_tag
|| p
->channel_id
!= channel_id
) {
1726 oldval
= snd_hda_codec_read(codec
, nid
, 0, AC_VERB_GET_CONV
, 0);
1727 newval
= (stream_tag
<< 4) | channel_id
;
1728 if (oldval
!= newval
)
1729 snd_hda_codec_write(codec
, nid
, 0,
1730 AC_VERB_SET_CHANNEL_STREAMID
,
1732 p
->stream_tag
= stream_tag
;
1733 p
->channel_id
= channel_id
;
1737 /* update the format-id if changed */
1738 static void update_pcm_format(struct hda_codec
*codec
, struct hda_cvt_setup
*p
,
1739 hda_nid_t nid
, int format
)
1741 unsigned int oldval
;
1743 if (p
->format_id
!= format
) {
1744 oldval
= snd_hda_codec_read(codec
, nid
, 0,
1745 AC_VERB_GET_STREAM_FORMAT
, 0);
1746 if (oldval
!= format
) {
1748 snd_hda_codec_write(codec
, nid
, 0,
1749 AC_VERB_SET_STREAM_FORMAT
,
1752 p
->format_id
= format
;
1757 * snd_hda_codec_setup_stream - set up the codec for streaming
1758 * @codec: the CODEC to set up
1759 * @nid: the NID to set up
1760 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
1761 * @channel_id: channel id to pass, zero based.
1762 * @format: stream format.
1764 void snd_hda_codec_setup_stream(struct hda_codec
*codec
, hda_nid_t nid
,
1766 int channel_id
, int format
)
1768 struct hda_codec
*c
;
1769 struct hda_cvt_setup
*p
;
1777 "hda_codec_setup_stream: NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
1778 nid
, stream_tag
, channel_id
, format
);
1779 p
= get_hda_cvt_setup(codec
, nid
);
1783 if (codec
->pcm_format_first
)
1784 update_pcm_format(codec
, p
, nid
, format
);
1785 update_pcm_stream_id(codec
, p
, nid
, stream_tag
, channel_id
);
1786 if (!codec
->pcm_format_first
)
1787 update_pcm_format(codec
, p
, nid
, format
);
1792 /* make other inactive cvts with the same stream-tag dirty */
1793 type
= get_wcaps_type(get_wcaps(codec
, nid
));
1794 list_for_each_entry(c
, &codec
->bus
->codec_list
, list
) {
1795 for (i
= 0; i
< c
->cvt_setups
.used
; i
++) {
1796 p
= snd_array_elem(&c
->cvt_setups
, i
);
1797 if (!p
->active
&& p
->stream_tag
== stream_tag
&&
1798 get_wcaps_type(get_wcaps(c
, p
->nid
)) == type
)
1803 EXPORT_SYMBOL_GPL(snd_hda_codec_setup_stream
);
1805 static void really_cleanup_stream(struct hda_codec
*codec
,
1806 struct hda_cvt_setup
*q
);
1809 * __snd_hda_codec_cleanup_stream - clean up the codec for closing
1810 * @codec: the CODEC to clean up
1811 * @nid: the NID to clean up
1812 * @do_now: really clean up the stream instead of clearing the active flag
1814 void __snd_hda_codec_cleanup_stream(struct hda_codec
*codec
, hda_nid_t nid
,
1817 struct hda_cvt_setup
*p
;
1822 if (codec
->no_sticky_stream
)
1825 codec_dbg(codec
, "hda_codec_cleanup_stream: NID=0x%x\n", nid
);
1826 p
= get_hda_cvt_setup(codec
, nid
);
1828 /* here we just clear the active flag when do_now isn't set;
1829 * actual clean-ups will be done later in
1830 * purify_inactive_streams() called from snd_hda_codec_prpapre()
1833 really_cleanup_stream(codec
, p
);
1838 EXPORT_SYMBOL_GPL(__snd_hda_codec_cleanup_stream
);
1840 static void really_cleanup_stream(struct hda_codec
*codec
,
1841 struct hda_cvt_setup
*q
)
1843 hda_nid_t nid
= q
->nid
;
1844 if (q
->stream_tag
|| q
->channel_id
)
1845 snd_hda_codec_write(codec
, nid
, 0, AC_VERB_SET_CHANNEL_STREAMID
, 0);
1847 snd_hda_codec_write(codec
, nid
, 0, AC_VERB_SET_STREAM_FORMAT
, 0
1849 memset(q
, 0, sizeof(*q
));
1853 /* clean up the all conflicting obsolete streams */
1854 static void purify_inactive_streams(struct hda_codec
*codec
)
1856 struct hda_codec
*c
;
1859 list_for_each_entry(c
, &codec
->bus
->codec_list
, list
) {
1860 for (i
= 0; i
< c
->cvt_setups
.used
; i
++) {
1861 struct hda_cvt_setup
*p
;
1862 p
= snd_array_elem(&c
->cvt_setups
, i
);
1864 really_cleanup_stream(c
, p
);
1870 /* clean up all streams; called from suspend */
1871 static void hda_cleanup_all_streams(struct hda_codec
*codec
)
1875 for (i
= 0; i
< codec
->cvt_setups
.used
; i
++) {
1876 struct hda_cvt_setup
*p
= snd_array_elem(&codec
->cvt_setups
, i
);
1878 really_cleanup_stream(codec
, p
);
1884 * amp access functions
1887 /* FIXME: more better hash key? */
1888 #define HDA_HASH_KEY(nid, dir, idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
1889 #define HDA_HASH_PINCAP_KEY(nid) (u32)((nid) + (0x02 << 24))
1890 #define HDA_HASH_PARPCM_KEY(nid) (u32)((nid) + (0x03 << 24))
1891 #define HDA_HASH_PARSTR_KEY(nid) (u32)((nid) + (0x04 << 24))
1892 #define INFO_AMP_CAPS (1<<0)
1893 #define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
1895 /* initialize the hash table */
1896 static void init_hda_cache(struct hda_cache_rec
*cache
,
1897 unsigned int record_size
)
1899 memset(cache
, 0, sizeof(*cache
));
1900 memset(cache
->hash
, 0xff, sizeof(cache
->hash
));
1901 snd_array_init(&cache
->buf
, record_size
, 64);
1904 static void free_hda_cache(struct hda_cache_rec
*cache
)
1906 snd_array_free(&cache
->buf
);
1909 /* query the hash. allocate an entry if not found. */
1910 static struct hda_cache_head
*get_hash(struct hda_cache_rec
*cache
, u32 key
)
1912 u16 idx
= key
% (u16
)ARRAY_SIZE(cache
->hash
);
1913 u16 cur
= cache
->hash
[idx
];
1914 struct hda_cache_head
*info
;
1916 while (cur
!= 0xffff) {
1917 info
= snd_array_elem(&cache
->buf
, cur
);
1918 if (info
->key
== key
)
1925 /* query the hash. allocate an entry if not found. */
1926 static struct hda_cache_head
*get_alloc_hash(struct hda_cache_rec
*cache
,
1929 struct hda_cache_head
*info
= get_hash(cache
, key
);
1932 /* add a new hash entry */
1933 info
= snd_array_new(&cache
->buf
);
1936 cur
= snd_array_index(&cache
->buf
, info
);
1940 idx
= key
% (u16
)ARRAY_SIZE(cache
->hash
);
1941 info
->next
= cache
->hash
[idx
];
1942 cache
->hash
[idx
] = cur
;
1947 /* query and allocate an amp hash entry */
1948 static inline struct hda_amp_info
*
1949 get_alloc_amp_hash(struct hda_codec
*codec
, u32 key
)
1951 return (struct hda_amp_info
*)get_alloc_hash(&codec
->amp_cache
, key
);
1954 /* overwrite the value with the key in the caps hash */
1955 static int write_caps_hash(struct hda_codec
*codec
, u32 key
, unsigned int val
)
1957 struct hda_amp_info
*info
;
1959 mutex_lock(&codec
->hash_mutex
);
1960 info
= get_alloc_amp_hash(codec
, key
);
1962 mutex_unlock(&codec
->hash_mutex
);
1965 info
->amp_caps
= val
;
1966 info
->head
.val
|= INFO_AMP_CAPS
;
1967 mutex_unlock(&codec
->hash_mutex
);
1971 /* query the value from the caps hash; if not found, fetch the current
1972 * value from the given function and store in the hash
1975 query_caps_hash(struct hda_codec
*codec
, hda_nid_t nid
, int dir
, u32 key
,
1976 unsigned int (*func
)(struct hda_codec
*, hda_nid_t
, int))
1978 struct hda_amp_info
*info
;
1981 mutex_lock(&codec
->hash_mutex
);
1982 info
= get_alloc_amp_hash(codec
, key
);
1984 mutex_unlock(&codec
->hash_mutex
);
1987 if (!(info
->head
.val
& INFO_AMP_CAPS
)) {
1988 mutex_unlock(&codec
->hash_mutex
); /* for reentrance */
1989 val
= func(codec
, nid
, dir
);
1990 write_caps_hash(codec
, key
, val
);
1992 val
= info
->amp_caps
;
1993 mutex_unlock(&codec
->hash_mutex
);
1998 static unsigned int read_amp_cap(struct hda_codec
*codec
, hda_nid_t nid
,
2001 if (!(get_wcaps(codec
, nid
) & AC_WCAP_AMP_OVRD
))
2003 return snd_hda_param_read(codec
, nid
,
2004 direction
== HDA_OUTPUT
?
2005 AC_PAR_AMP_OUT_CAP
: AC_PAR_AMP_IN_CAP
);
2009 * query_amp_caps - query AMP capabilities
2010 * @codec: the HD-auio codec
2011 * @nid: the NID to query
2012 * @direction: either #HDA_INPUT or #HDA_OUTPUT
2014 * Query AMP capabilities for the given widget and direction.
2015 * Returns the obtained capability bits.
2017 * When cap bits have been already read, this doesn't read again but
2018 * returns the cached value.
2020 u32
query_amp_caps(struct hda_codec
*codec
, hda_nid_t nid
, int direction
)
2022 return query_caps_hash(codec
, nid
, direction
,
2023 HDA_HASH_KEY(nid
, direction
, 0),
2026 EXPORT_SYMBOL_GPL(query_amp_caps
);
2029 * snd_hda_check_amp_caps - query AMP capabilities
2030 * @codec: the HD-audio codec
2031 * @nid: the NID to query
2032 * @dir: either #HDA_INPUT or #HDA_OUTPUT
2033 * @bits: bit mask to check the result
2035 * Check whether the widget has the given amp capability for the direction.
2037 bool snd_hda_check_amp_caps(struct hda_codec
*codec
, hda_nid_t nid
,
2038 int dir
, unsigned int bits
)
2042 if (get_wcaps(codec
, nid
) & (1 << (dir
+ 1)))
2043 if (query_amp_caps(codec
, nid
, dir
) & bits
)
2047 EXPORT_SYMBOL_GPL(snd_hda_check_amp_caps
);
2050 * snd_hda_override_amp_caps - Override the AMP capabilities
2051 * @codec: the CODEC to clean up
2052 * @nid: the NID to clean up
2053 * @dir: either #HDA_INPUT or #HDA_OUTPUT
2054 * @caps: the capability bits to set
2056 * Override the cached AMP caps bits value by the given one.
2057 * This function is useful if the driver needs to adjust the AMP ranges,
2058 * e.g. limit to 0dB, etc.
2060 * Returns zero if successful or a negative error code.
2062 int snd_hda_override_amp_caps(struct hda_codec
*codec
, hda_nid_t nid
, int dir
,
2065 return write_caps_hash(codec
, HDA_HASH_KEY(nid
, dir
, 0), caps
);
2067 EXPORT_SYMBOL_GPL(snd_hda_override_amp_caps
);
2069 static unsigned int read_pin_cap(struct hda_codec
*codec
, hda_nid_t nid
,
2072 return snd_hda_param_read(codec
, nid
, AC_PAR_PIN_CAP
);
2076 * snd_hda_query_pin_caps - Query PIN capabilities
2077 * @codec: the HD-auio codec
2078 * @nid: the NID to query
2080 * Query PIN capabilities for the given widget.
2081 * Returns the obtained capability bits.
2083 * When cap bits have been already read, this doesn't read again but
2084 * returns the cached value.
2086 u32
snd_hda_query_pin_caps(struct hda_codec
*codec
, hda_nid_t nid
)
2088 return query_caps_hash(codec
, nid
, 0, HDA_HASH_PINCAP_KEY(nid
),
2091 EXPORT_SYMBOL_GPL(snd_hda_query_pin_caps
);
2094 * snd_hda_override_pin_caps - Override the pin capabilities
2096 * @nid: the NID to override
2097 * @caps: the capability bits to set
2099 * Override the cached PIN capabilitiy bits value by the given one.
2101 * Returns zero if successful or a negative error code.
2103 int snd_hda_override_pin_caps(struct hda_codec
*codec
, hda_nid_t nid
,
2106 return write_caps_hash(codec
, HDA_HASH_PINCAP_KEY(nid
), caps
);
2108 EXPORT_SYMBOL_GPL(snd_hda_override_pin_caps
);
2110 /* read or sync the hash value with the current value;
2111 * call within hash_mutex
2113 static struct hda_amp_info
*
2114 update_amp_hash(struct hda_codec
*codec
, hda_nid_t nid
, int ch
,
2115 int direction
, int index
, bool init_only
)
2117 struct hda_amp_info
*info
;
2118 unsigned int parm
, val
= 0;
2119 bool val_read
= false;
2122 info
= get_alloc_amp_hash(codec
, HDA_HASH_KEY(nid
, direction
, index
));
2125 if (!(info
->head
.val
& INFO_AMP_VOL(ch
))) {
2127 mutex_unlock(&codec
->hash_mutex
);
2128 parm
= ch
? AC_AMP_GET_RIGHT
: AC_AMP_GET_LEFT
;
2129 parm
|= direction
== HDA_OUTPUT
?
2130 AC_AMP_GET_OUTPUT
: AC_AMP_GET_INPUT
;
2132 val
= snd_hda_codec_read(codec
, nid
, 0,
2133 AC_VERB_GET_AMP_GAIN_MUTE
, parm
);
2136 mutex_lock(&codec
->hash_mutex
);
2139 info
->vol
[ch
] = val
;
2140 info
->head
.val
|= INFO_AMP_VOL(ch
);
2141 } else if (init_only
)
2147 * write the current volume in info to the h/w
2149 static void put_vol_mute(struct hda_codec
*codec
, unsigned int amp_caps
,
2150 hda_nid_t nid
, int ch
, int direction
, int index
,
2155 parm
= ch
? AC_AMP_SET_RIGHT
: AC_AMP_SET_LEFT
;
2156 parm
|= direction
== HDA_OUTPUT
? AC_AMP_SET_OUTPUT
: AC_AMP_SET_INPUT
;
2157 parm
|= index
<< AC_AMP_SET_INDEX_SHIFT
;
2158 if ((val
& HDA_AMP_MUTE
) && !(amp_caps
& AC_AMPCAP_MUTE
) &&
2159 (amp_caps
& AC_AMPCAP_MIN_MUTE
))
2160 ; /* set the zero value as a fake mute */
2163 snd_hda_codec_write(codec
, nid
, 0, AC_VERB_SET_AMP_GAIN_MUTE
, parm
);
2167 * snd_hda_codec_amp_read - Read AMP value
2168 * @codec: HD-audio codec
2169 * @nid: NID to read the AMP value
2170 * @ch: channel (left=0 or right=1)
2171 * @direction: #HDA_INPUT or #HDA_OUTPUT
2172 * @index: the index value (only for input direction)
2174 * Read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
2176 int snd_hda_codec_amp_read(struct hda_codec
*codec
, hda_nid_t nid
, int ch
,
2177 int direction
, int index
)
2179 struct hda_amp_info
*info
;
2180 unsigned int val
= 0;
2182 mutex_lock(&codec
->hash_mutex
);
2183 info
= update_amp_hash(codec
, nid
, ch
, direction
, index
, false);
2185 val
= info
->vol
[ch
];
2186 mutex_unlock(&codec
->hash_mutex
);
2189 EXPORT_SYMBOL_GPL(snd_hda_codec_amp_read
);
2191 static int codec_amp_update(struct hda_codec
*codec
, hda_nid_t nid
, int ch
,
2192 int direction
, int idx
, int mask
, int val
,
2195 struct hda_amp_info
*info
;
2197 unsigned int cache_only
;
2199 if (snd_BUG_ON(mask
& ~0xff))
2203 mutex_lock(&codec
->hash_mutex
);
2204 info
= update_amp_hash(codec
, nid
, ch
, direction
, idx
, init_only
);
2206 mutex_unlock(&codec
->hash_mutex
);
2209 val
|= info
->vol
[ch
] & ~mask
;
2210 if (info
->vol
[ch
] == val
) {
2211 mutex_unlock(&codec
->hash_mutex
);
2214 info
->vol
[ch
] = val
;
2215 cache_only
= info
->head
.dirty
= codec
->cached_write
;
2216 caps
= info
->amp_caps
;
2217 mutex_unlock(&codec
->hash_mutex
);
2219 put_vol_mute(codec
, caps
, nid
, ch
, direction
, idx
, val
);
2224 * snd_hda_codec_amp_update - update the AMP value
2225 * @codec: HD-audio codec
2226 * @nid: NID to read the AMP value
2227 * @ch: channel (left=0 or right=1)
2228 * @direction: #HDA_INPUT or #HDA_OUTPUT
2229 * @idx: the index value (only for input direction)
2230 * @mask: bit mask to set
2231 * @val: the bits value to set
2233 * Update the AMP value with a bit mask.
2234 * Returns 0 if the value is unchanged, 1 if changed.
2236 int snd_hda_codec_amp_update(struct hda_codec
*codec
, hda_nid_t nid
, int ch
,
2237 int direction
, int idx
, int mask
, int val
)
2239 return codec_amp_update(codec
, nid
, ch
, direction
, idx
, mask
, val
, false);
2241 EXPORT_SYMBOL_GPL(snd_hda_codec_amp_update
);
2244 * snd_hda_codec_amp_stereo - update the AMP stereo values
2245 * @codec: HD-audio codec
2246 * @nid: NID to read the AMP value
2247 * @direction: #HDA_INPUT or #HDA_OUTPUT
2248 * @idx: the index value (only for input direction)
2249 * @mask: bit mask to set
2250 * @val: the bits value to set
2252 * Update the AMP values like snd_hda_codec_amp_update(), but for a
2253 * stereo widget with the same mask and value.
2255 int snd_hda_codec_amp_stereo(struct hda_codec
*codec
, hda_nid_t nid
,
2256 int direction
, int idx
, int mask
, int val
)
2260 if (snd_BUG_ON(mask
& ~0xff))
2262 for (ch
= 0; ch
< 2; ch
++)
2263 ret
|= snd_hda_codec_amp_update(codec
, nid
, ch
, direction
,
2267 EXPORT_SYMBOL_GPL(snd_hda_codec_amp_stereo
);
2270 * snd_hda_codec_amp_init - initialize the AMP value
2271 * @codec: the HDA codec
2272 * @nid: NID to read the AMP value
2273 * @ch: channel (left=0 or right=1)
2274 * @dir: #HDA_INPUT or #HDA_OUTPUT
2275 * @idx: the index value (only for input direction)
2276 * @mask: bit mask to set
2277 * @val: the bits value to set
2279 * Works like snd_hda_codec_amp_update() but it writes the value only at
2280 * the first access. If the amp was already initialized / updated beforehand,
2281 * this does nothing.
2283 int snd_hda_codec_amp_init(struct hda_codec
*codec
, hda_nid_t nid
, int ch
,
2284 int dir
, int idx
, int mask
, int val
)
2286 return codec_amp_update(codec
, nid
, ch
, dir
, idx
, mask
, val
, true);
2288 EXPORT_SYMBOL_GPL(snd_hda_codec_amp_init
);
2291 * snd_hda_codec_amp_init_stereo - initialize the stereo AMP value
2292 * @codec: the HDA codec
2293 * @nid: NID to read the AMP value
2294 * @dir: #HDA_INPUT or #HDA_OUTPUT
2295 * @idx: the index value (only for input direction)
2296 * @mask: bit mask to set
2297 * @val: the bits value to set
2299 * Call snd_hda_codec_amp_init() for both stereo channels.
2301 int snd_hda_codec_amp_init_stereo(struct hda_codec
*codec
, hda_nid_t nid
,
2302 int dir
, int idx
, int mask
, int val
)
2306 if (snd_BUG_ON(mask
& ~0xff))
2308 for (ch
= 0; ch
< 2; ch
++)
2309 ret
|= snd_hda_codec_amp_init(codec
, nid
, ch
, dir
,
2313 EXPORT_SYMBOL_GPL(snd_hda_codec_amp_init_stereo
);
2316 * snd_hda_codec_resume_amp - Resume all AMP commands from the cache
2317 * @codec: HD-audio codec
2319 * Resume the all amp commands from the cache.
2321 void snd_hda_codec_resume_amp(struct hda_codec
*codec
)
2325 mutex_lock(&codec
->hash_mutex
);
2326 codec
->cached_write
= 0;
2327 for (i
= 0; i
< codec
->amp_cache
.buf
.used
; i
++) {
2328 struct hda_amp_info
*buffer
;
2331 unsigned int idx
, dir
, ch
;
2332 struct hda_amp_info info
;
2334 buffer
= snd_array_elem(&codec
->amp_cache
.buf
, i
);
2335 if (!buffer
->head
.dirty
)
2337 buffer
->head
.dirty
= 0;
2339 key
= info
.head
.key
;
2343 idx
= (key
>> 16) & 0xff;
2344 dir
= (key
>> 24) & 0xff;
2345 for (ch
= 0; ch
< 2; ch
++) {
2346 if (!(info
.head
.val
& INFO_AMP_VOL(ch
)))
2348 mutex_unlock(&codec
->hash_mutex
);
2349 put_vol_mute(codec
, info
.amp_caps
, nid
, ch
, dir
, idx
,
2351 mutex_lock(&codec
->hash_mutex
);
2354 mutex_unlock(&codec
->hash_mutex
);
2356 EXPORT_SYMBOL_GPL(snd_hda_codec_resume_amp
);
2358 static u32
get_amp_max_value(struct hda_codec
*codec
, hda_nid_t nid
, int dir
,
2361 u32 caps
= query_amp_caps(codec
, nid
, dir
);
2363 caps
= (caps
& AC_AMPCAP_NUM_STEPS
) >> AC_AMPCAP_NUM_STEPS_SHIFT
;
2370 * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer
2371 * @kcontrol: referred ctl element
2372 * @uinfo: pointer to get/store the data
2374 * The control element is supposed to have the private_value field
2375 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2377 int snd_hda_mixer_amp_volume_info(struct snd_kcontrol
*kcontrol
,
2378 struct snd_ctl_elem_info
*uinfo
)
2380 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2381 u16 nid
= get_amp_nid(kcontrol
);
2382 u8 chs
= get_amp_channels(kcontrol
);
2383 int dir
= get_amp_direction(kcontrol
);
2384 unsigned int ofs
= get_amp_offset(kcontrol
);
2386 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
2387 uinfo
->count
= chs
== 3 ? 2 : 1;
2388 uinfo
->value
.integer
.min
= 0;
2389 uinfo
->value
.integer
.max
= get_amp_max_value(codec
, nid
, dir
, ofs
);
2390 if (!uinfo
->value
.integer
.max
) {
2392 "num_steps = 0 for NID=0x%x (ctl = %s)\n",
2393 nid
, kcontrol
->id
.name
);
2398 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_info
);
2401 static inline unsigned int
2402 read_amp_value(struct hda_codec
*codec
, hda_nid_t nid
,
2403 int ch
, int dir
, int idx
, unsigned int ofs
)
2406 val
= snd_hda_codec_amp_read(codec
, nid
, ch
, dir
, idx
);
2407 val
&= HDA_AMP_VOLMASK
;
2416 update_amp_value(struct hda_codec
*codec
, hda_nid_t nid
,
2417 int ch
, int dir
, int idx
, unsigned int ofs
,
2420 unsigned int maxval
;
2424 /* ofs = 0: raw max value */
2425 maxval
= get_amp_max_value(codec
, nid
, dir
, 0);
2428 return snd_hda_codec_amp_update(codec
, nid
, ch
, dir
, idx
,
2429 HDA_AMP_VOLMASK
, val
);
2433 * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume
2434 * @kcontrol: ctl element
2435 * @ucontrol: pointer to get/store the data
2437 * The control element is supposed to have the private_value field
2438 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2440 int snd_hda_mixer_amp_volume_get(struct snd_kcontrol
*kcontrol
,
2441 struct snd_ctl_elem_value
*ucontrol
)
2443 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2444 hda_nid_t nid
= get_amp_nid(kcontrol
);
2445 int chs
= get_amp_channels(kcontrol
);
2446 int dir
= get_amp_direction(kcontrol
);
2447 int idx
= get_amp_index(kcontrol
);
2448 unsigned int ofs
= get_amp_offset(kcontrol
);
2449 long *valp
= ucontrol
->value
.integer
.value
;
2452 *valp
++ = read_amp_value(codec
, nid
, 0, dir
, idx
, ofs
);
2454 *valp
= read_amp_value(codec
, nid
, 1, dir
, idx
, ofs
);
2457 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_get
);
2460 * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume
2461 * @kcontrol: ctl element
2462 * @ucontrol: pointer to get/store the data
2464 * The control element is supposed to have the private_value field
2465 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2467 int snd_hda_mixer_amp_volume_put(struct snd_kcontrol
*kcontrol
,
2468 struct snd_ctl_elem_value
*ucontrol
)
2470 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2471 hda_nid_t nid
= get_amp_nid(kcontrol
);
2472 int chs
= get_amp_channels(kcontrol
);
2473 int dir
= get_amp_direction(kcontrol
);
2474 int idx
= get_amp_index(kcontrol
);
2475 unsigned int ofs
= get_amp_offset(kcontrol
);
2476 long *valp
= ucontrol
->value
.integer
.value
;
2479 snd_hda_power_up(codec
);
2481 change
= update_amp_value(codec
, nid
, 0, dir
, idx
, ofs
, *valp
);
2485 change
|= update_amp_value(codec
, nid
, 1, dir
, idx
, ofs
, *valp
);
2486 snd_hda_power_down(codec
);
2489 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_put
);
2492 * snd_hda_mixer_amp_volume_put - TLV callback for a standard AMP mixer volume
2493 * @kcontrol: ctl element
2494 * @op_flag: operation flag
2495 * @size: byte size of input TLV
2498 * The control element is supposed to have the private_value field
2499 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2501 int snd_hda_mixer_amp_tlv(struct snd_kcontrol
*kcontrol
, int op_flag
,
2502 unsigned int size
, unsigned int __user
*_tlv
)
2504 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2505 hda_nid_t nid
= get_amp_nid(kcontrol
);
2506 int dir
= get_amp_direction(kcontrol
);
2507 unsigned int ofs
= get_amp_offset(kcontrol
);
2508 bool min_mute
= get_amp_min_mute(kcontrol
);
2509 u32 caps
, val1
, val2
;
2511 if (size
< 4 * sizeof(unsigned int))
2513 caps
= query_amp_caps(codec
, nid
, dir
);
2514 val2
= (caps
& AC_AMPCAP_STEP_SIZE
) >> AC_AMPCAP_STEP_SIZE_SHIFT
;
2515 val2
= (val2
+ 1) * 25;
2516 val1
= -((caps
& AC_AMPCAP_OFFSET
) >> AC_AMPCAP_OFFSET_SHIFT
);
2518 val1
= ((int)val1
) * ((int)val2
);
2519 if (min_mute
|| (caps
& AC_AMPCAP_MIN_MUTE
))
2520 val2
|= TLV_DB_SCALE_MUTE
;
2521 if (put_user(SNDRV_CTL_TLVT_DB_SCALE
, _tlv
))
2523 if (put_user(2 * sizeof(unsigned int), _tlv
+ 1))
2525 if (put_user(val1
, _tlv
+ 2))
2527 if (put_user(val2
, _tlv
+ 3))
2531 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_tlv
);
2534 * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control
2535 * @codec: HD-audio codec
2536 * @nid: NID of a reference widget
2537 * @dir: #HDA_INPUT or #HDA_OUTPUT
2538 * @tlv: TLV data to be stored, at least 4 elements
2540 * Set (static) TLV data for a virtual master volume using the AMP caps
2541 * obtained from the reference NID.
2542 * The volume range is recalculated as if the max volume is 0dB.
2544 void snd_hda_set_vmaster_tlv(struct hda_codec
*codec
, hda_nid_t nid
, int dir
,
2550 caps
= query_amp_caps(codec
, nid
, dir
);
2551 nums
= (caps
& AC_AMPCAP_NUM_STEPS
) >> AC_AMPCAP_NUM_STEPS_SHIFT
;
2552 step
= (caps
& AC_AMPCAP_STEP_SIZE
) >> AC_AMPCAP_STEP_SIZE_SHIFT
;
2553 step
= (step
+ 1) * 25;
2554 tlv
[0] = SNDRV_CTL_TLVT_DB_SCALE
;
2555 tlv
[1] = 2 * sizeof(unsigned int);
2556 tlv
[2] = -nums
* step
;
2559 EXPORT_SYMBOL_GPL(snd_hda_set_vmaster_tlv
);
2561 /* find a mixer control element with the given name */
2562 static struct snd_kcontrol
*
2563 find_mixer_ctl(struct hda_codec
*codec
, const char *name
, int dev
, int idx
)
2565 struct snd_ctl_elem_id id
;
2566 memset(&id
, 0, sizeof(id
));
2567 id
.iface
= SNDRV_CTL_ELEM_IFACE_MIXER
;
2570 if (snd_BUG_ON(strlen(name
) >= sizeof(id
.name
)))
2572 strcpy(id
.name
, name
);
2573 return snd_ctl_find_id(codec
->bus
->card
, &id
);
2577 * snd_hda_find_mixer_ctl - Find a mixer control element with the given name
2578 * @codec: HD-audio codec
2579 * @name: ctl id name string
2581 * Get the control element with the given id string and IFACE_MIXER.
2583 struct snd_kcontrol
*snd_hda_find_mixer_ctl(struct hda_codec
*codec
,
2586 return find_mixer_ctl(codec
, name
, 0, 0);
2588 EXPORT_SYMBOL_GPL(snd_hda_find_mixer_ctl
);
2590 static int find_empty_mixer_ctl_idx(struct hda_codec
*codec
, const char *name
,
2594 /* 16 ctlrs should be large enough */
2595 for (i
= 0, idx
= start_idx
; i
< 16; i
++, idx
++) {
2596 if (!find_mixer_ctl(codec
, name
, 0, idx
))
2603 * snd_hda_ctl_add - Add a control element and assign to the codec
2604 * @codec: HD-audio codec
2605 * @nid: corresponding NID (optional)
2606 * @kctl: the control element to assign
2608 * Add the given control element to an array inside the codec instance.
2609 * All control elements belonging to a codec are supposed to be added
2610 * by this function so that a proper clean-up works at the free or
2611 * reconfiguration time.
2613 * If non-zero @nid is passed, the NID is assigned to the control element.
2614 * The assignment is shown in the codec proc file.
2616 * snd_hda_ctl_add() checks the control subdev id field whether
2617 * #HDA_SUBDEV_NID_FLAG bit is set. If set (and @nid is zero), the lower
2618 * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit
2619 * specifies if kctl->private_value is a HDA amplifier value.
2621 int snd_hda_ctl_add(struct hda_codec
*codec
, hda_nid_t nid
,
2622 struct snd_kcontrol
*kctl
)
2625 unsigned short flags
= 0;
2626 struct hda_nid_item
*item
;
2628 if (kctl
->id
.subdevice
& HDA_SUBDEV_AMP_FLAG
) {
2629 flags
|= HDA_NID_ITEM_AMP
;
2631 nid
= get_amp_nid_(kctl
->private_value
);
2633 if ((kctl
->id
.subdevice
& HDA_SUBDEV_NID_FLAG
) != 0 && nid
== 0)
2634 nid
= kctl
->id
.subdevice
& 0xffff;
2635 if (kctl
->id
.subdevice
& (HDA_SUBDEV_NID_FLAG
|HDA_SUBDEV_AMP_FLAG
))
2636 kctl
->id
.subdevice
= 0;
2637 err
= snd_ctl_add(codec
->bus
->card
, kctl
);
2640 item
= snd_array_new(&codec
->mixers
);
2645 item
->flags
= flags
;
2648 EXPORT_SYMBOL_GPL(snd_hda_ctl_add
);
2651 * snd_hda_add_nid - Assign a NID to a control element
2652 * @codec: HD-audio codec
2653 * @nid: corresponding NID (optional)
2654 * @kctl: the control element to assign
2655 * @index: index to kctl
2657 * Add the given control element to an array inside the codec instance.
2658 * This function is used when #snd_hda_ctl_add cannot be used for 1:1
2659 * NID:KCTL mapping - for example "Capture Source" selector.
2661 int snd_hda_add_nid(struct hda_codec
*codec
, struct snd_kcontrol
*kctl
,
2662 unsigned int index
, hda_nid_t nid
)
2664 struct hda_nid_item
*item
;
2667 item
= snd_array_new(&codec
->nids
);
2671 item
->index
= index
;
2675 codec_err(codec
, "no NID for mapping control %s:%d:%d\n",
2676 kctl
->id
.name
, kctl
->id
.index
, index
);
2679 EXPORT_SYMBOL_GPL(snd_hda_add_nid
);
2682 * snd_hda_ctls_clear - Clear all controls assigned to the given codec
2683 * @codec: HD-audio codec
2685 void snd_hda_ctls_clear(struct hda_codec
*codec
)
2688 struct hda_nid_item
*items
= codec
->mixers
.list
;
2689 for (i
= 0; i
< codec
->mixers
.used
; i
++)
2690 snd_ctl_remove(codec
->bus
->card
, items
[i
].kctl
);
2691 snd_array_free(&codec
->mixers
);
2692 snd_array_free(&codec
->nids
);
2696 * snd_hda_lock_devices - pseudo device locking
2699 * toggle card->shutdown to allow/disallow the device access (as a hack)
2701 int snd_hda_lock_devices(struct hda_bus
*bus
)
2703 struct snd_card
*card
= bus
->card
;
2704 struct hda_codec
*codec
;
2706 spin_lock(&card
->files_lock
);
2710 if (!list_empty(&card
->ctl_files
))
2713 list_for_each_entry(codec
, &bus
->codec_list
, list
) {
2715 for (pcm
= 0; pcm
< codec
->num_pcms
; pcm
++) {
2716 struct hda_pcm
*cpcm
= &codec
->pcm_info
[pcm
];
2719 if (cpcm
->pcm
->streams
[0].substream_opened
||
2720 cpcm
->pcm
->streams
[1].substream_opened
)
2724 spin_unlock(&card
->files_lock
);
2730 spin_unlock(&card
->files_lock
);
2733 EXPORT_SYMBOL_GPL(snd_hda_lock_devices
);
2736 * snd_hda_unlock_devices - pseudo device unlocking
2739 void snd_hda_unlock_devices(struct hda_bus
*bus
)
2741 struct snd_card
*card
= bus
->card
;
2744 spin_lock(&card
->files_lock
);
2746 spin_unlock(&card
->files_lock
);
2748 EXPORT_SYMBOL_GPL(snd_hda_unlock_devices
);
2751 * snd_hda_codec_reset - Clear all objects assigned to the codec
2752 * @codec: HD-audio codec
2754 * This frees the all PCM and control elements assigned to the codec, and
2755 * clears the caches and restores the pin default configurations.
2757 * When a device is being used, it returns -EBSY. If successfully freed,
2760 int snd_hda_codec_reset(struct hda_codec
*codec
)
2762 struct hda_bus
*bus
= codec
->bus
;
2763 struct snd_card
*card
= bus
->card
;
2766 if (snd_hda_lock_devices(bus
) < 0)
2769 /* OK, let it free */
2770 cancel_delayed_work_sync(&codec
->jackpoll_work
);
2772 cancel_delayed_work_sync(&codec
->power_work
);
2773 flush_workqueue(bus
->workq
);
2775 snd_hda_ctls_clear(codec
);
2777 for (i
= 0; i
< codec
->num_pcms
; i
++) {
2778 if (codec
->pcm_info
[i
].pcm
) {
2779 snd_device_free(card
, codec
->pcm_info
[i
].pcm
);
2780 clear_bit(codec
->pcm_info
[i
].device
,
2784 snd_hda_detach_beep_device(codec
);
2785 if (codec
->patch_ops
.free
)
2786 codec
->patch_ops
.free(codec
);
2787 memset(&codec
->patch_ops
, 0, sizeof(codec
->patch_ops
));
2788 snd_hda_jack_tbl_clear(codec
);
2789 codec
->proc_widget_hook
= NULL
;
2791 free_hda_cache(&codec
->amp_cache
);
2792 free_hda_cache(&codec
->cmd_cache
);
2793 init_hda_cache(&codec
->amp_cache
, sizeof(struct hda_amp_info
));
2794 init_hda_cache(&codec
->cmd_cache
, sizeof(struct hda_cache_head
));
2795 /* free only driver_pins so that init_pins + user_pins are restored */
2796 snd_array_free(&codec
->driver_pins
);
2797 snd_array_free(&codec
->cvt_setups
);
2798 snd_array_free(&codec
->spdif_out
);
2799 snd_array_free(&codec
->verbs
);
2800 codec
->num_pcms
= 0;
2801 codec
->pcm_info
= NULL
;
2802 codec
->preset
= NULL
;
2803 codec
->slave_dig_outs
= NULL
;
2804 codec
->spdif_status_reset
= 0;
2805 unload_parser(codec
);
2806 module_put(codec
->owner
);
2807 codec
->owner
= NULL
;
2809 /* allow device access again */
2810 snd_hda_unlock_devices(bus
);
2814 typedef int (*map_slave_func_t
)(struct hda_codec
*, void *, struct snd_kcontrol
*);
2816 /* apply the function to all matching slave ctls in the mixer list */
2817 static int map_slaves(struct hda_codec
*codec
, const char * const *slaves
,
2818 const char *suffix
, map_slave_func_t func
, void *data
)
2820 struct hda_nid_item
*items
;
2821 const char * const *s
;
2824 items
= codec
->mixers
.list
;
2825 for (i
= 0; i
< codec
->mixers
.used
; i
++) {
2826 struct snd_kcontrol
*sctl
= items
[i
].kctl
;
2827 if (!sctl
|| sctl
->id
.iface
!= SNDRV_CTL_ELEM_IFACE_MIXER
)
2829 for (s
= slaves
; *s
; s
++) {
2830 char tmpname
[sizeof(sctl
->id
.name
)];
2831 const char *name
= *s
;
2833 snprintf(tmpname
, sizeof(tmpname
), "%s %s",
2837 if (!strcmp(sctl
->id
.name
, name
)) {
2838 err
= func(codec
, data
, sctl
);
2848 static int check_slave_present(struct hda_codec
*codec
,
2849 void *data
, struct snd_kcontrol
*sctl
)
2854 /* guess the value corresponding to 0dB */
2855 static int get_kctl_0dB_offset(struct hda_codec
*codec
,
2856 struct snd_kcontrol
*kctl
, int *step_to_check
)
2859 const int *tlv
= NULL
;
2862 if (kctl
->vd
[0].access
& SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK
) {
2863 /* FIXME: set_fs() hack for obtaining user-space TLV data */
2864 mm_segment_t fs
= get_fs();
2866 if (!kctl
->tlv
.c(kctl
, 0, sizeof(_tlv
), _tlv
))
2869 } else if (kctl
->vd
[0].access
& SNDRV_CTL_ELEM_ACCESS_TLV_READ
)
2871 if (tlv
&& tlv
[0] == SNDRV_CTL_TLVT_DB_SCALE
) {
2873 step
&= ~TLV_DB_SCALE_MUTE
;
2876 if (*step_to_check
&& *step_to_check
!= step
) {
2877 codec_err(codec
, "Mismatching dB step for vmaster slave (%d!=%d)\n",
2878 - *step_to_check
, step
);
2881 *step_to_check
= step
;
2882 val
= -tlv
[2] / step
;
2887 /* call kctl->put with the given value(s) */
2888 static int put_kctl_with_value(struct snd_kcontrol
*kctl
, int val
)
2890 struct snd_ctl_elem_value
*ucontrol
;
2891 ucontrol
= kzalloc(sizeof(*ucontrol
), GFP_KERNEL
);
2894 ucontrol
->value
.integer
.value
[0] = val
;
2895 ucontrol
->value
.integer
.value
[1] = val
;
2896 kctl
->put(kctl
, ucontrol
);
2901 /* initialize the slave volume with 0dB */
2902 static int init_slave_0dB(struct hda_codec
*codec
,
2903 void *data
, struct snd_kcontrol
*slave
)
2905 int offset
= get_kctl_0dB_offset(codec
, slave
, data
);
2907 put_kctl_with_value(slave
, offset
);
2911 /* unmute the slave */
2912 static int init_slave_unmute(struct hda_codec
*codec
,
2913 void *data
, struct snd_kcontrol
*slave
)
2915 return put_kctl_with_value(slave
, 1);
2918 static int add_slave(struct hda_codec
*codec
,
2919 void *data
, struct snd_kcontrol
*slave
)
2921 return snd_ctl_add_slave(data
, slave
);
2925 * __snd_hda_add_vmaster - create a virtual master control and add slaves
2926 * @codec: HD-audio codec
2927 * @name: vmaster control name
2928 * @tlv: TLV data (optional)
2929 * @slaves: slave control names (optional)
2930 * @suffix: suffix string to each slave name (optional)
2931 * @init_slave_vol: initialize slaves to unmute/0dB
2932 * @ctl_ret: store the vmaster kcontrol in return
2934 * Create a virtual master control with the given name. The TLV data
2935 * must be either NULL or a valid data.
2937 * @slaves is a NULL-terminated array of strings, each of which is a
2938 * slave control name. All controls with these names are assigned to
2939 * the new virtual master control.
2941 * This function returns zero if successful or a negative error code.
2943 int __snd_hda_add_vmaster(struct hda_codec
*codec
, char *name
,
2944 unsigned int *tlv
, const char * const *slaves
,
2945 const char *suffix
, bool init_slave_vol
,
2946 struct snd_kcontrol
**ctl_ret
)
2948 struct snd_kcontrol
*kctl
;
2954 err
= map_slaves(codec
, slaves
, suffix
, check_slave_present
, NULL
);
2956 codec_dbg(codec
, "No slave found for %s\n", name
);
2959 kctl
= snd_ctl_make_virtual_master(name
, tlv
);
2962 err
= snd_hda_ctl_add(codec
, 0, kctl
);
2966 err
= map_slaves(codec
, slaves
, suffix
, add_slave
, kctl
);
2970 /* init with master mute & zero volume */
2971 put_kctl_with_value(kctl
, 0);
2972 if (init_slave_vol
) {
2974 map_slaves(codec
, slaves
, suffix
,
2975 tlv
? init_slave_0dB
: init_slave_unmute
, &step
);
2982 EXPORT_SYMBOL_GPL(__snd_hda_add_vmaster
);
2985 * mute-LED control using vmaster
2987 static int vmaster_mute_mode_info(struct snd_kcontrol
*kcontrol
,
2988 struct snd_ctl_elem_info
*uinfo
)
2990 static const char * const texts
[] = {
2991 "On", "Off", "Follow Master"
2994 return snd_ctl_enum_info(uinfo
, 1, 3, texts
);
2997 static int vmaster_mute_mode_get(struct snd_kcontrol
*kcontrol
,
2998 struct snd_ctl_elem_value
*ucontrol
)
3000 struct hda_vmaster_mute_hook
*hook
= snd_kcontrol_chip(kcontrol
);
3001 ucontrol
->value
.enumerated
.item
[0] = hook
->mute_mode
;
3005 static int vmaster_mute_mode_put(struct snd_kcontrol
*kcontrol
,
3006 struct snd_ctl_elem_value
*ucontrol
)
3008 struct hda_vmaster_mute_hook
*hook
= snd_kcontrol_chip(kcontrol
);
3009 unsigned int old_mode
= hook
->mute_mode
;
3011 hook
->mute_mode
= ucontrol
->value
.enumerated
.item
[0];
3012 if (hook
->mute_mode
> HDA_VMUTE_FOLLOW_MASTER
)
3013 hook
->mute_mode
= HDA_VMUTE_FOLLOW_MASTER
;
3014 if (old_mode
== hook
->mute_mode
)
3016 snd_hda_sync_vmaster_hook(hook
);
3020 static struct snd_kcontrol_new vmaster_mute_mode
= {
3021 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
3022 .name
= "Mute-LED Mode",
3023 .info
= vmaster_mute_mode_info
,
3024 .get
= vmaster_mute_mode_get
,
3025 .put
= vmaster_mute_mode_put
,
3029 * snd_hda_add_vmaster_hook - Add a vmaster hook for mute-LED
3030 * @codec: the HDA codec
3031 * @hook: the vmaster hook object
3032 * @expose_enum_ctl: flag to create an enum ctl
3034 * Add a mute-LED hook with the given vmaster switch kctl.
3035 * When @expose_enum_ctl is set, "Mute-LED Mode" control is automatically
3036 * created and associated with the given hook.
3038 int snd_hda_add_vmaster_hook(struct hda_codec
*codec
,
3039 struct hda_vmaster_mute_hook
*hook
,
3040 bool expose_enum_ctl
)
3042 struct snd_kcontrol
*kctl
;
3044 if (!hook
->hook
|| !hook
->sw_kctl
)
3046 snd_ctl_add_vmaster_hook(hook
->sw_kctl
, hook
->hook
, codec
);
3047 hook
->codec
= codec
;
3048 hook
->mute_mode
= HDA_VMUTE_FOLLOW_MASTER
;
3049 if (!expose_enum_ctl
)
3051 kctl
= snd_ctl_new1(&vmaster_mute_mode
, hook
);
3054 return snd_hda_ctl_add(codec
, 0, kctl
);
3056 EXPORT_SYMBOL_GPL(snd_hda_add_vmaster_hook
);
3059 * snd_hda_sync_vmaster_hook - Sync vmaster hook
3060 * @hook: the vmaster hook
3062 * Call the hook with the current value for synchronization.
3063 * Should be called in init callback.
3065 void snd_hda_sync_vmaster_hook(struct hda_vmaster_mute_hook
*hook
)
3067 if (!hook
->hook
|| !hook
->codec
)
3069 /* don't call vmaster hook in the destructor since it might have
3070 * been already destroyed
3072 if (hook
->codec
->bus
->shutdown
)
3074 switch (hook
->mute_mode
) {
3075 case HDA_VMUTE_FOLLOW_MASTER
:
3076 snd_ctl_sync_vmaster_hook(hook
->sw_kctl
);
3079 hook
->hook(hook
->codec
, hook
->mute_mode
);
3083 EXPORT_SYMBOL_GPL(snd_hda_sync_vmaster_hook
);
3087 * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch
3088 * @kcontrol: referred ctl element
3089 * @uinfo: pointer to get/store the data
3091 * The control element is supposed to have the private_value field
3092 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
3094 int snd_hda_mixer_amp_switch_info(struct snd_kcontrol
*kcontrol
,
3095 struct snd_ctl_elem_info
*uinfo
)
3097 int chs
= get_amp_channels(kcontrol
);
3099 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_BOOLEAN
;
3100 uinfo
->count
= chs
== 3 ? 2 : 1;
3101 uinfo
->value
.integer
.min
= 0;
3102 uinfo
->value
.integer
.max
= 1;
3105 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_info
);
3108 * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch
3109 * @kcontrol: ctl element
3110 * @ucontrol: pointer to get/store the data
3112 * The control element is supposed to have the private_value field
3113 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
3115 int snd_hda_mixer_amp_switch_get(struct snd_kcontrol
*kcontrol
,
3116 struct snd_ctl_elem_value
*ucontrol
)
3118 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
3119 hda_nid_t nid
= get_amp_nid(kcontrol
);
3120 int chs
= get_amp_channels(kcontrol
);
3121 int dir
= get_amp_direction(kcontrol
);
3122 int idx
= get_amp_index(kcontrol
);
3123 long *valp
= ucontrol
->value
.integer
.value
;
3126 *valp
++ = (snd_hda_codec_amp_read(codec
, nid
, 0, dir
, idx
) &
3127 HDA_AMP_MUTE
) ? 0 : 1;
3129 *valp
= (snd_hda_codec_amp_read(codec
, nid
, 1, dir
, idx
) &
3130 HDA_AMP_MUTE
) ? 0 : 1;
3133 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_get
);
3136 * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch
3137 * @kcontrol: ctl element
3138 * @ucontrol: pointer to get/store the data
3140 * The control element is supposed to have the private_value field
3141 * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
3143 int snd_hda_mixer_amp_switch_put(struct snd_kcontrol
*kcontrol
,
3144 struct snd_ctl_elem_value
*ucontrol
)
3146 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
3147 hda_nid_t nid
= get_amp_nid(kcontrol
);
3148 int chs
= get_amp_channels(kcontrol
);
3149 int dir
= get_amp_direction(kcontrol
);
3150 int idx
= get_amp_index(kcontrol
);
3151 long *valp
= ucontrol
->value
.integer
.value
;
3154 snd_hda_power_up(codec
);
3156 change
= snd_hda_codec_amp_update(codec
, nid
, 0, dir
, idx
,
3158 *valp
? 0 : HDA_AMP_MUTE
);
3162 change
|= snd_hda_codec_amp_update(codec
, nid
, 1, dir
, idx
,
3164 *valp
? 0 : HDA_AMP_MUTE
);
3165 hda_call_check_power_status(codec
, nid
);
3166 snd_hda_power_down(codec
);
3169 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_put
);
3172 * bound volume controls
3174 * bind multiple volumes (# indices, from 0)
3177 #define AMP_VAL_IDX_SHIFT 19
3178 #define AMP_VAL_IDX_MASK (0x0f<<19)
3181 * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control
3182 * @kcontrol: ctl element
3183 * @ucontrol: pointer to get/store the data
3185 * The control element is supposed to have the private_value field
3186 * set up via HDA_BIND_MUTE*() macros.
3188 int snd_hda_mixer_bind_switch_get(struct snd_kcontrol
*kcontrol
,
3189 struct snd_ctl_elem_value
*ucontrol
)
3191 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
3195 mutex_lock(&codec
->control_mutex
);
3196 pval
= kcontrol
->private_value
;
3197 kcontrol
->private_value
= pval
& ~AMP_VAL_IDX_MASK
; /* index 0 */
3198 err
= snd_hda_mixer_amp_switch_get(kcontrol
, ucontrol
);
3199 kcontrol
->private_value
= pval
;
3200 mutex_unlock(&codec
->control_mutex
);
3203 EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_switch_get
);
3206 * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control
3207 * @kcontrol: ctl element
3208 * @ucontrol: pointer to get/store the data
3210 * The control element is supposed to have the private_value field
3211 * set up via HDA_BIND_MUTE*() macros.
3213 int snd_hda_mixer_bind_switch_put(struct snd_kcontrol
*kcontrol
,
3214 struct snd_ctl_elem_value
*ucontrol
)
3216 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
3218 int i
, indices
, err
= 0, change
= 0;
3220 mutex_lock(&codec
->control_mutex
);
3221 pval
= kcontrol
->private_value
;
3222 indices
= (pval
& AMP_VAL_IDX_MASK
) >> AMP_VAL_IDX_SHIFT
;
3223 for (i
= 0; i
< indices
; i
++) {
3224 kcontrol
->private_value
= (pval
& ~AMP_VAL_IDX_MASK
) |
3225 (i
<< AMP_VAL_IDX_SHIFT
);
3226 err
= snd_hda_mixer_amp_switch_put(kcontrol
, ucontrol
);
3231 kcontrol
->private_value
= pval
;
3232 mutex_unlock(&codec
->control_mutex
);
3233 return err
< 0 ? err
: change
;
3235 EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_switch_put
);
3238 * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control
3239 * @kcontrol: referred ctl element
3240 * @uinfo: pointer to get/store the data
3242 * The control element is supposed to have the private_value field
3243 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
3245 int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol
*kcontrol
,
3246 struct snd_ctl_elem_info
*uinfo
)
3248 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
3249 struct hda_bind_ctls
*c
;
3252 mutex_lock(&codec
->control_mutex
);
3253 c
= (struct hda_bind_ctls
*)kcontrol
->private_value
;
3254 kcontrol
->private_value
= *c
->values
;
3255 err
= c
->ops
->info(kcontrol
, uinfo
);
3256 kcontrol
->private_value
= (long)c
;
3257 mutex_unlock(&codec
->control_mutex
);
3260 EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_info
);
3263 * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control
3264 * @kcontrol: ctl element
3265 * @ucontrol: pointer to get/store the data
3267 * The control element is supposed to have the private_value field
3268 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
3270 int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol
*kcontrol
,
3271 struct snd_ctl_elem_value
*ucontrol
)
3273 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
3274 struct hda_bind_ctls
*c
;
3277 mutex_lock(&codec
->control_mutex
);
3278 c
= (struct hda_bind_ctls
*)kcontrol
->private_value
;
3279 kcontrol
->private_value
= *c
->values
;
3280 err
= c
->ops
->get(kcontrol
, ucontrol
);
3281 kcontrol
->private_value
= (long)c
;
3282 mutex_unlock(&codec
->control_mutex
);
3285 EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_get
);
3288 * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control
3289 * @kcontrol: ctl element
3290 * @ucontrol: pointer to get/store the data
3292 * The control element is supposed to have the private_value field
3293 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
3295 int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol
*kcontrol
,
3296 struct snd_ctl_elem_value
*ucontrol
)
3298 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
3299 struct hda_bind_ctls
*c
;
3300 unsigned long *vals
;
3301 int err
= 0, change
= 0;
3303 mutex_lock(&codec
->control_mutex
);
3304 c
= (struct hda_bind_ctls
*)kcontrol
->private_value
;
3305 for (vals
= c
->values
; *vals
; vals
++) {
3306 kcontrol
->private_value
= *vals
;
3307 err
= c
->ops
->put(kcontrol
, ucontrol
);
3312 kcontrol
->private_value
= (long)c
;
3313 mutex_unlock(&codec
->control_mutex
);
3314 return err
< 0 ? err
: change
;
3316 EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_put
);
3319 * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control
3320 * @kcontrol: ctl element
3321 * @op_flag: operation flag
3322 * @size: byte size of input TLV
3325 * The control element is supposed to have the private_value field
3326 * set up via HDA_BIND_VOL() macro.
3328 int snd_hda_mixer_bind_tlv(struct snd_kcontrol
*kcontrol
, int op_flag
,
3329 unsigned int size
, unsigned int __user
*tlv
)
3331 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
3332 struct hda_bind_ctls
*c
;
3335 mutex_lock(&codec
->control_mutex
);
3336 c
= (struct hda_bind_ctls
*)kcontrol
->private_value
;
3337 kcontrol
->private_value
= *c
->values
;
3338 err
= c
->ops
->tlv(kcontrol
, op_flag
, size
, tlv
);
3339 kcontrol
->private_value
= (long)c
;
3340 mutex_unlock(&codec
->control_mutex
);
3343 EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_tlv
);
3345 struct hda_ctl_ops snd_hda_bind_vol
= {
3346 .info
= snd_hda_mixer_amp_volume_info
,
3347 .get
= snd_hda_mixer_amp_volume_get
,
3348 .put
= snd_hda_mixer_amp_volume_put
,
3349 .tlv
= snd_hda_mixer_amp_tlv
3351 EXPORT_SYMBOL_GPL(snd_hda_bind_vol
);
3353 struct hda_ctl_ops snd_hda_bind_sw
= {
3354 .info
= snd_hda_mixer_amp_switch_info
,
3355 .get
= snd_hda_mixer_amp_switch_get
,
3356 .put
= snd_hda_mixer_amp_switch_put
,
3357 .tlv
= snd_hda_mixer_amp_tlv
3359 EXPORT_SYMBOL_GPL(snd_hda_bind_sw
);
3362 * SPDIF out controls
3365 static int snd_hda_spdif_mask_info(struct snd_kcontrol
*kcontrol
,
3366 struct snd_ctl_elem_info
*uinfo
)
3368 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_IEC958
;
3373 static int snd_hda_spdif_cmask_get(struct snd_kcontrol
*kcontrol
,
3374 struct snd_ctl_elem_value
*ucontrol
)
3376 ucontrol
->value
.iec958
.status
[0] = IEC958_AES0_PROFESSIONAL
|
3377 IEC958_AES0_NONAUDIO
|
3378 IEC958_AES0_CON_EMPHASIS_5015
|
3379 IEC958_AES0_CON_NOT_COPYRIGHT
;
3380 ucontrol
->value
.iec958
.status
[1] = IEC958_AES1_CON_CATEGORY
|
3381 IEC958_AES1_CON_ORIGINAL
;
3385 static int snd_hda_spdif_pmask_get(struct snd_kcontrol
*kcontrol
,
3386 struct snd_ctl_elem_value
*ucontrol
)
3388 ucontrol
->value
.iec958
.status
[0] = IEC958_AES0_PROFESSIONAL
|
3389 IEC958_AES0_NONAUDIO
|
3390 IEC958_AES0_PRO_EMPHASIS_5015
;
3394 static int snd_hda_spdif_default_get(struct snd_kcontrol
*kcontrol
,
3395 struct snd_ctl_elem_value
*ucontrol
)
3397 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
3398 int idx
= kcontrol
->private_value
;
3399 struct hda_spdif_out
*spdif
;
3401 mutex_lock(&codec
->spdif_mutex
);
3402 spdif
= snd_array_elem(&codec
->spdif_out
, idx
);
3403 ucontrol
->value
.iec958
.status
[0] = spdif
->status
& 0xff;
3404 ucontrol
->value
.iec958
.status
[1] = (spdif
->status
>> 8) & 0xff;
3405 ucontrol
->value
.iec958
.status
[2] = (spdif
->status
>> 16) & 0xff;
3406 ucontrol
->value
.iec958
.status
[3] = (spdif
->status
>> 24) & 0xff;
3407 mutex_unlock(&codec
->spdif_mutex
);
3412 /* convert from SPDIF status bits to HDA SPDIF bits
3413 * bit 0 (DigEn) is always set zero (to be filled later)
3415 static unsigned short convert_from_spdif_status(unsigned int sbits
)
3417 unsigned short val
= 0;
3419 if (sbits
& IEC958_AES0_PROFESSIONAL
)
3420 val
|= AC_DIG1_PROFESSIONAL
;
3421 if (sbits
& IEC958_AES0_NONAUDIO
)
3422 val
|= AC_DIG1_NONAUDIO
;
3423 if (sbits
& IEC958_AES0_PROFESSIONAL
) {
3424 if ((sbits
& IEC958_AES0_PRO_EMPHASIS
) ==
3425 IEC958_AES0_PRO_EMPHASIS_5015
)
3426 val
|= AC_DIG1_EMPHASIS
;
3428 if ((sbits
& IEC958_AES0_CON_EMPHASIS
) ==
3429 IEC958_AES0_CON_EMPHASIS_5015
)
3430 val
|= AC_DIG1_EMPHASIS
;
3431 if (!(sbits
& IEC958_AES0_CON_NOT_COPYRIGHT
))
3432 val
|= AC_DIG1_COPYRIGHT
;
3433 if (sbits
& (IEC958_AES1_CON_ORIGINAL
<< 8))
3434 val
|= AC_DIG1_LEVEL
;
3435 val
|= sbits
& (IEC958_AES1_CON_CATEGORY
<< 8);
3440 /* convert to SPDIF status bits from HDA SPDIF bits
3442 static unsigned int convert_to_spdif_status(unsigned short val
)
3444 unsigned int sbits
= 0;
3446 if (val
& AC_DIG1_NONAUDIO
)
3447 sbits
|= IEC958_AES0_NONAUDIO
;
3448 if (val
& AC_DIG1_PROFESSIONAL
)
3449 sbits
|= IEC958_AES0_PROFESSIONAL
;
3450 if (sbits
& IEC958_AES0_PROFESSIONAL
) {
3451 if (val
& AC_DIG1_EMPHASIS
)
3452 sbits
|= IEC958_AES0_PRO_EMPHASIS_5015
;
3454 if (val
& AC_DIG1_EMPHASIS
)
3455 sbits
|= IEC958_AES0_CON_EMPHASIS_5015
;
3456 if (!(val
& AC_DIG1_COPYRIGHT
))
3457 sbits
|= IEC958_AES0_CON_NOT_COPYRIGHT
;
3458 if (val
& AC_DIG1_LEVEL
)
3459 sbits
|= (IEC958_AES1_CON_ORIGINAL
<< 8);
3460 sbits
|= val
& (0x7f << 8);
3465 /* set digital convert verbs both for the given NID and its slaves */
3466 static void set_dig_out(struct hda_codec
*codec
, hda_nid_t nid
,
3471 snd_hda_codec_write_cache(codec
, nid
, 0, verb
, val
);
3472 d
= codec
->slave_dig_outs
;
3476 snd_hda_codec_write_cache(codec
, *d
, 0, verb
, val
);
3479 static inline void set_dig_out_convert(struct hda_codec
*codec
, hda_nid_t nid
,
3483 set_dig_out(codec
, nid
, AC_VERB_SET_DIGI_CONVERT_1
, dig1
);
3485 set_dig_out(codec
, nid
, AC_VERB_SET_DIGI_CONVERT_2
, dig2
);
3488 static int snd_hda_spdif_default_put(struct snd_kcontrol
*kcontrol
,
3489 struct snd_ctl_elem_value
*ucontrol
)
3491 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
3492 int idx
= kcontrol
->private_value
;
3493 struct hda_spdif_out
*spdif
;
3498 mutex_lock(&codec
->spdif_mutex
);
3499 spdif
= snd_array_elem(&codec
->spdif_out
, idx
);
3501 spdif
->status
= ucontrol
->value
.iec958
.status
[0] |
3502 ((unsigned int)ucontrol
->value
.iec958
.status
[1] << 8) |
3503 ((unsigned int)ucontrol
->value
.iec958
.status
[2] << 16) |
3504 ((unsigned int)ucontrol
->value
.iec958
.status
[3] << 24);
3505 val
= convert_from_spdif_status(spdif
->status
);
3506 val
|= spdif
->ctls
& 1;
3507 change
= spdif
->ctls
!= val
;
3509 if (change
&& nid
!= (u16
)-1)
3510 set_dig_out_convert(codec
, nid
, val
& 0xff, (val
>> 8) & 0xff);
3511 mutex_unlock(&codec
->spdif_mutex
);
3515 #define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
3517 static int snd_hda_spdif_out_switch_get(struct snd_kcontrol
*kcontrol
,
3518 struct snd_ctl_elem_value
*ucontrol
)
3520 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
3521 int idx
= kcontrol
->private_value
;
3522 struct hda_spdif_out
*spdif
;
3524 mutex_lock(&codec
->spdif_mutex
);
3525 spdif
= snd_array_elem(&codec
->spdif_out
, idx
);
3526 ucontrol
->value
.integer
.value
[0] = spdif
->ctls
& AC_DIG1_ENABLE
;
3527 mutex_unlock(&codec
->spdif_mutex
);
3531 static inline void set_spdif_ctls(struct hda_codec
*codec
, hda_nid_t nid
,
3534 set_dig_out_convert(codec
, nid
, dig1
, dig2
);
3535 /* unmute amp switch (if any) */
3536 if ((get_wcaps(codec
, nid
) & AC_WCAP_OUT_AMP
) &&
3537 (dig1
& AC_DIG1_ENABLE
))
3538 snd_hda_codec_amp_stereo(codec
, nid
, HDA_OUTPUT
, 0,
3542 static int snd_hda_spdif_out_switch_put(struct snd_kcontrol
*kcontrol
,
3543 struct snd_ctl_elem_value
*ucontrol
)
3545 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
3546 int idx
= kcontrol
->private_value
;
3547 struct hda_spdif_out
*spdif
;
3552 mutex_lock(&codec
->spdif_mutex
);
3553 spdif
= snd_array_elem(&codec
->spdif_out
, idx
);
3555 val
= spdif
->ctls
& ~AC_DIG1_ENABLE
;
3556 if (ucontrol
->value
.integer
.value
[0])
3557 val
|= AC_DIG1_ENABLE
;
3558 change
= spdif
->ctls
!= val
;
3560 if (change
&& nid
!= (u16
)-1)
3561 set_spdif_ctls(codec
, nid
, val
& 0xff, -1);
3562 mutex_unlock(&codec
->spdif_mutex
);
3566 static struct snd_kcontrol_new dig_mixes
[] = {
3568 .access
= SNDRV_CTL_ELEM_ACCESS_READ
,
3569 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
3570 .name
= SNDRV_CTL_NAME_IEC958("", PLAYBACK
, CON_MASK
),
3571 .info
= snd_hda_spdif_mask_info
,
3572 .get
= snd_hda_spdif_cmask_get
,
3575 .access
= SNDRV_CTL_ELEM_ACCESS_READ
,
3576 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
3577 .name
= SNDRV_CTL_NAME_IEC958("", PLAYBACK
, PRO_MASK
),
3578 .info
= snd_hda_spdif_mask_info
,
3579 .get
= snd_hda_spdif_pmask_get
,
3582 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
3583 .name
= SNDRV_CTL_NAME_IEC958("", PLAYBACK
, DEFAULT
),
3584 .info
= snd_hda_spdif_mask_info
,
3585 .get
= snd_hda_spdif_default_get
,
3586 .put
= snd_hda_spdif_default_put
,
3589 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
3590 .name
= SNDRV_CTL_NAME_IEC958("", PLAYBACK
, SWITCH
),
3591 .info
= snd_hda_spdif_out_switch_info
,
3592 .get
= snd_hda_spdif_out_switch_get
,
3593 .put
= snd_hda_spdif_out_switch_put
,
3599 * snd_hda_create_dig_out_ctls - create Output SPDIF-related controls
3600 * @codec: the HDA codec
3601 * @associated_nid: NID that new ctls associated with
3602 * @cvt_nid: converter NID
3603 * @type: HDA_PCM_TYPE_*
3604 * Creates controls related with the digital output.
3605 * Called from each patch supporting the digital out.
3607 * Returns 0 if successful, or a negative error code.
3609 int snd_hda_create_dig_out_ctls(struct hda_codec
*codec
,
3610 hda_nid_t associated_nid
,
3615 struct snd_kcontrol
*kctl
;
3616 struct snd_kcontrol_new
*dig_mix
;
3618 const int spdif_index
= 16;
3619 struct hda_spdif_out
*spdif
;
3620 struct hda_bus
*bus
= codec
->bus
;
3622 if (bus
->primary_dig_out_type
== HDA_PCM_TYPE_HDMI
&&
3623 type
== HDA_PCM_TYPE_SPDIF
) {
3625 } else if (bus
->primary_dig_out_type
== HDA_PCM_TYPE_SPDIF
&&
3626 type
== HDA_PCM_TYPE_HDMI
) {
3627 /* suppose a single SPDIF device */
3628 for (dig_mix
= dig_mixes
; dig_mix
->name
; dig_mix
++) {
3629 kctl
= find_mixer_ctl(codec
, dig_mix
->name
, 0, 0);
3632 kctl
->id
.index
= spdif_index
;
3634 bus
->primary_dig_out_type
= HDA_PCM_TYPE_HDMI
;
3636 if (!bus
->primary_dig_out_type
)
3637 bus
->primary_dig_out_type
= type
;
3639 idx
= find_empty_mixer_ctl_idx(codec
, "IEC958 Playback Switch", idx
);
3641 codec_err(codec
, "too many IEC958 outputs\n");
3644 spdif
= snd_array_new(&codec
->spdif_out
);
3647 for (dig_mix
= dig_mixes
; dig_mix
->name
; dig_mix
++) {
3648 kctl
= snd_ctl_new1(dig_mix
, codec
);
3651 kctl
->id
.index
= idx
;
3652 kctl
->private_value
= codec
->spdif_out
.used
- 1;
3653 err
= snd_hda_ctl_add(codec
, associated_nid
, kctl
);
3657 spdif
->nid
= cvt_nid
;
3658 spdif
->ctls
= snd_hda_codec_read(codec
, cvt_nid
, 0,
3659 AC_VERB_GET_DIGI_CONVERT_1
, 0);
3660 spdif
->status
= convert_to_spdif_status(spdif
->ctls
);
3663 EXPORT_SYMBOL_GPL(snd_hda_create_dig_out_ctls
);
3666 * snd_hda_spdif_out_of_nid - get the hda_spdif_out entry from the given NID
3667 * @codec: the HDA codec
3670 * call within spdif_mutex lock
3672 struct hda_spdif_out
*snd_hda_spdif_out_of_nid(struct hda_codec
*codec
,
3676 for (i
= 0; i
< codec
->spdif_out
.used
; i
++) {
3677 struct hda_spdif_out
*spdif
=
3678 snd_array_elem(&codec
->spdif_out
, i
);
3679 if (spdif
->nid
== nid
)
3684 EXPORT_SYMBOL_GPL(snd_hda_spdif_out_of_nid
);
3687 * snd_hda_spdif_ctls_unassign - Unassign the given SPDIF ctl
3688 * @codec: the HDA codec
3689 * @idx: the SPDIF ctl index
3691 * Unassign the widget from the given SPDIF control.
3693 void snd_hda_spdif_ctls_unassign(struct hda_codec
*codec
, int idx
)
3695 struct hda_spdif_out
*spdif
;
3697 mutex_lock(&codec
->spdif_mutex
);
3698 spdif
= snd_array_elem(&codec
->spdif_out
, idx
);
3699 spdif
->nid
= (u16
)-1;
3700 mutex_unlock(&codec
->spdif_mutex
);
3702 EXPORT_SYMBOL_GPL(snd_hda_spdif_ctls_unassign
);
3705 * snd_hda_spdif_ctls_assign - Assign the SPDIF controls to the given NID
3706 * @codec: the HDA codec
3707 * @idx: the SPDIF ctl idx
3710 * Assign the widget to the SPDIF control with the given index.
3712 void snd_hda_spdif_ctls_assign(struct hda_codec
*codec
, int idx
, hda_nid_t nid
)
3714 struct hda_spdif_out
*spdif
;
3717 mutex_lock(&codec
->spdif_mutex
);
3718 spdif
= snd_array_elem(&codec
->spdif_out
, idx
);
3719 if (spdif
->nid
!= nid
) {
3722 set_spdif_ctls(codec
, nid
, val
& 0xff, (val
>> 8) & 0xff);
3724 mutex_unlock(&codec
->spdif_mutex
);
3726 EXPORT_SYMBOL_GPL(snd_hda_spdif_ctls_assign
);
3729 * SPDIF sharing with analog output
3731 static int spdif_share_sw_get(struct snd_kcontrol
*kcontrol
,
3732 struct snd_ctl_elem_value
*ucontrol
)
3734 struct hda_multi_out
*mout
= snd_kcontrol_chip(kcontrol
);
3735 ucontrol
->value
.integer
.value
[0] = mout
->share_spdif
;
3739 static int spdif_share_sw_put(struct snd_kcontrol
*kcontrol
,
3740 struct snd_ctl_elem_value
*ucontrol
)
3742 struct hda_multi_out
*mout
= snd_kcontrol_chip(kcontrol
);
3743 mout
->share_spdif
= !!ucontrol
->value
.integer
.value
[0];
3747 static struct snd_kcontrol_new spdif_share_sw
= {
3748 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
3749 .name
= "IEC958 Default PCM Playback Switch",
3750 .info
= snd_ctl_boolean_mono_info
,
3751 .get
= spdif_share_sw_get
,
3752 .put
= spdif_share_sw_put
,
3756 * snd_hda_create_spdif_share_sw - create Default PCM switch
3757 * @codec: the HDA codec
3758 * @mout: multi-out instance
3760 int snd_hda_create_spdif_share_sw(struct hda_codec
*codec
,
3761 struct hda_multi_out
*mout
)
3763 struct snd_kcontrol
*kctl
;
3765 if (!mout
->dig_out_nid
)
3768 kctl
= snd_ctl_new1(&spdif_share_sw
, mout
);
3771 /* ATTENTION: here mout is passed as private_data, instead of codec */
3772 return snd_hda_ctl_add(codec
, mout
->dig_out_nid
, kctl
);
3774 EXPORT_SYMBOL_GPL(snd_hda_create_spdif_share_sw
);
3780 #define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
3782 static int snd_hda_spdif_in_switch_get(struct snd_kcontrol
*kcontrol
,
3783 struct snd_ctl_elem_value
*ucontrol
)
3785 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
3787 ucontrol
->value
.integer
.value
[0] = codec
->spdif_in_enable
;
3791 static int snd_hda_spdif_in_switch_put(struct snd_kcontrol
*kcontrol
,
3792 struct snd_ctl_elem_value
*ucontrol
)
3794 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
3795 hda_nid_t nid
= kcontrol
->private_value
;
3796 unsigned int val
= !!ucontrol
->value
.integer
.value
[0];
3799 mutex_lock(&codec
->spdif_mutex
);
3800 change
= codec
->spdif_in_enable
!= val
;
3802 codec
->spdif_in_enable
= val
;
3803 snd_hda_codec_write_cache(codec
, nid
, 0,
3804 AC_VERB_SET_DIGI_CONVERT_1
, val
);
3806 mutex_unlock(&codec
->spdif_mutex
);
3810 static int snd_hda_spdif_in_status_get(struct snd_kcontrol
*kcontrol
,
3811 struct snd_ctl_elem_value
*ucontrol
)
3813 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
3814 hda_nid_t nid
= kcontrol
->private_value
;
3818 val
= snd_hda_codec_read(codec
, nid
, 0, AC_VERB_GET_DIGI_CONVERT_1
, 0);
3819 sbits
= convert_to_spdif_status(val
);
3820 ucontrol
->value
.iec958
.status
[0] = sbits
;
3821 ucontrol
->value
.iec958
.status
[1] = sbits
>> 8;
3822 ucontrol
->value
.iec958
.status
[2] = sbits
>> 16;
3823 ucontrol
->value
.iec958
.status
[3] = sbits
>> 24;
3827 static struct snd_kcontrol_new dig_in_ctls
[] = {
3829 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
3830 .name
= SNDRV_CTL_NAME_IEC958("", CAPTURE
, SWITCH
),
3831 .info
= snd_hda_spdif_in_switch_info
,
3832 .get
= snd_hda_spdif_in_switch_get
,
3833 .put
= snd_hda_spdif_in_switch_put
,
3836 .access
= SNDRV_CTL_ELEM_ACCESS_READ
,
3837 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
3838 .name
= SNDRV_CTL_NAME_IEC958("", CAPTURE
, DEFAULT
),
3839 .info
= snd_hda_spdif_mask_info
,
3840 .get
= snd_hda_spdif_in_status_get
,
3846 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
3847 * @codec: the HDA codec
3848 * @nid: audio in widget NID
3850 * Creates controls related with the SPDIF input.
3851 * Called from each patch supporting the SPDIF in.
3853 * Returns 0 if successful, or a negative error code.
3855 int snd_hda_create_spdif_in_ctls(struct hda_codec
*codec
, hda_nid_t nid
)
3858 struct snd_kcontrol
*kctl
;
3859 struct snd_kcontrol_new
*dig_mix
;
3862 idx
= find_empty_mixer_ctl_idx(codec
, "IEC958 Capture Switch", 0);
3864 codec_err(codec
, "too many IEC958 inputs\n");
3867 for (dig_mix
= dig_in_ctls
; dig_mix
->name
; dig_mix
++) {
3868 kctl
= snd_ctl_new1(dig_mix
, codec
);
3871 kctl
->private_value
= nid
;
3872 err
= snd_hda_ctl_add(codec
, nid
, kctl
);
3876 codec
->spdif_in_enable
=
3877 snd_hda_codec_read(codec
, nid
, 0,
3878 AC_VERB_GET_DIGI_CONVERT_1
, 0) &
3882 EXPORT_SYMBOL_GPL(snd_hda_create_spdif_in_ctls
);
3888 /* build a 31bit cache key with the widget id and the command parameter */
3889 #define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
3890 #define get_cmd_cache_nid(key) ((key) & 0xff)
3891 #define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
3894 * snd_hda_codec_write_cache - send a single command with caching
3895 * @codec: the HDA codec
3896 * @nid: NID to send the command
3897 * @flags: optional bit flags
3898 * @verb: the verb to send
3899 * @parm: the parameter for the verb
3901 * Send a single command without waiting for response.
3903 * Returns 0 if successful, or a negative error code.
3905 int snd_hda_codec_write_cache(struct hda_codec
*codec
, hda_nid_t nid
,
3906 int flags
, unsigned int verb
, unsigned int parm
)
3909 struct hda_cache_head
*c
;
3911 unsigned int cache_only
;
3913 cache_only
= codec
->cached_write
;
3915 err
= snd_hda_codec_write(codec
, nid
, flags
, verb
, parm
);
3920 /* parm may contain the verb stuff for get/set amp */
3921 verb
= verb
| (parm
>> 8);
3923 key
= build_cmd_cache_key(nid
, verb
);
3924 mutex_lock(&codec
->bus
->cmd_mutex
);
3925 c
= get_alloc_hash(&codec
->cmd_cache
, key
);
3928 c
->dirty
= cache_only
;
3930 mutex_unlock(&codec
->bus
->cmd_mutex
);
3933 EXPORT_SYMBOL_GPL(snd_hda_codec_write_cache
);
3936 * snd_hda_codec_update_cache - check cache and write the cmd only when needed
3937 * @codec: the HDA codec
3938 * @nid: NID to send the command
3939 * @flags: optional bit flags
3940 * @verb: the verb to send
3941 * @parm: the parameter for the verb
3943 * This function works like snd_hda_codec_write_cache(), but it doesn't send
3944 * command if the parameter is already identical with the cached value.
3945 * If not, it sends the command and refreshes the cache.
3947 * Returns 0 if successful, or a negative error code.
3949 int snd_hda_codec_update_cache(struct hda_codec
*codec
, hda_nid_t nid
,
3950 int flags
, unsigned int verb
, unsigned int parm
)
3952 struct hda_cache_head
*c
;
3955 /* parm may contain the verb stuff for get/set amp */
3956 verb
= verb
| (parm
>> 8);
3958 key
= build_cmd_cache_key(nid
, verb
);
3959 mutex_lock(&codec
->bus
->cmd_mutex
);
3960 c
= get_hash(&codec
->cmd_cache
, key
);
3961 if (c
&& c
->val
== parm
) {
3962 mutex_unlock(&codec
->bus
->cmd_mutex
);
3965 mutex_unlock(&codec
->bus
->cmd_mutex
);
3966 return snd_hda_codec_write_cache(codec
, nid
, flags
, verb
, parm
);
3968 EXPORT_SYMBOL_GPL(snd_hda_codec_update_cache
);
3971 * snd_hda_codec_resume_cache - Resume the all commands from the cache
3972 * @codec: HD-audio codec
3974 * Execute all verbs recorded in the command caches to resume.
3976 void snd_hda_codec_resume_cache(struct hda_codec
*codec
)
3980 mutex_lock(&codec
->hash_mutex
);
3981 codec
->cached_write
= 0;
3982 for (i
= 0; i
< codec
->cmd_cache
.buf
.used
; i
++) {
3983 struct hda_cache_head
*buffer
;
3986 buffer
= snd_array_elem(&codec
->cmd_cache
.buf
, i
);
3993 mutex_unlock(&codec
->hash_mutex
);
3994 snd_hda_codec_write(codec
, get_cmd_cache_nid(key
), 0,
3995 get_cmd_cache_cmd(key
), buffer
->val
);
3996 mutex_lock(&codec
->hash_mutex
);
3998 mutex_unlock(&codec
->hash_mutex
);
4000 EXPORT_SYMBOL_GPL(snd_hda_codec_resume_cache
);
4003 * snd_hda_sequence_write_cache - sequence writes with caching
4004 * @codec: the HDA codec
4005 * @seq: VERB array to send
4007 * Send the commands sequentially from the given array.
4008 * Thte commands are recorded on cache for power-save and resume.
4009 * The array must be terminated with NID=0.
4011 void snd_hda_sequence_write_cache(struct hda_codec
*codec
,
4012 const struct hda_verb
*seq
)
4014 for (; seq
->nid
; seq
++)
4015 snd_hda_codec_write_cache(codec
, seq
->nid
, 0, seq
->verb
,
4018 EXPORT_SYMBOL_GPL(snd_hda_sequence_write_cache
);
4021 * snd_hda_codec_flush_cache - Execute all pending (cached) amps / verbs
4022 * @codec: HD-audio codec
4024 void snd_hda_codec_flush_cache(struct hda_codec
*codec
)
4026 snd_hda_codec_resume_amp(codec
);
4027 snd_hda_codec_resume_cache(codec
);
4029 EXPORT_SYMBOL_GPL(snd_hda_codec_flush_cache
);
4032 * snd_hda_codec_set_power_to_all - Set the power state to all widgets
4033 * @codec: the HDA codec
4034 * @fg: function group (not used now)
4035 * @power_state: the power state to set (AC_PWRST_*)
4037 * Set the given power state to all widgets that have the power control.
4038 * If the codec has power_filter set, it evaluates the power state and
4039 * filter out if it's unchanged as D3.
4041 void snd_hda_codec_set_power_to_all(struct hda_codec
*codec
, hda_nid_t fg
,
4042 unsigned int power_state
)
4044 hda_nid_t nid
= codec
->start_nid
;
4047 for (i
= 0; i
< codec
->num_nodes
; i
++, nid
++) {
4048 unsigned int wcaps
= get_wcaps(codec
, nid
);
4049 unsigned int state
= power_state
;
4050 if (!(wcaps
& AC_WCAP_POWER
))
4052 if (codec
->power_filter
) {
4053 state
= codec
->power_filter(codec
, nid
, power_state
);
4054 if (state
!= power_state
&& power_state
== AC_PWRST_D3
)
4057 snd_hda_codec_write(codec
, nid
, 0, AC_VERB_SET_POWER_STATE
,
4061 EXPORT_SYMBOL_GPL(snd_hda_codec_set_power_to_all
);
4064 * supported power states check
4066 static bool snd_hda_codec_get_supported_ps(struct hda_codec
*codec
, hda_nid_t fg
,
4067 unsigned int power_state
)
4069 int sup
= snd_hda_param_read(codec
, fg
, AC_PAR_POWER_STATE
);
4073 if (sup
& power_state
)
4080 * wait until the state is reached, returns the current state
4082 static unsigned int hda_sync_power_state(struct hda_codec
*codec
,
4084 unsigned int power_state
)
4086 unsigned long end_time
= jiffies
+ msecs_to_jiffies(500);
4087 unsigned int state
, actual_state
;
4090 state
= snd_hda_codec_read(codec
, fg
, 0,
4091 AC_VERB_GET_POWER_STATE
, 0);
4092 if (state
& AC_PWRST_ERROR
)
4094 actual_state
= (state
>> 4) & 0x0f;
4095 if (actual_state
== power_state
)
4097 if (time_after_eq(jiffies
, end_time
))
4099 /* wait until the codec reachs to the target state */
4106 * snd_hda_codec_eapd_power_filter - A power filter callback for EAPD
4107 * @codec: the HDA codec
4109 * @power_state: power state to evalue
4111 * Don't power down the widget if it controls eapd and EAPD_BTLENABLE is set.
4112 * This can be used a codec power_filter callback.
4114 unsigned int snd_hda_codec_eapd_power_filter(struct hda_codec
*codec
,
4116 unsigned int power_state
)
4118 if (nid
== codec
->afg
|| nid
== codec
->mfg
)
4120 if (power_state
== AC_PWRST_D3
&&
4121 get_wcaps_type(get_wcaps(codec
, nid
)) == AC_WID_PIN
&&
4122 (snd_hda_query_pin_caps(codec
, nid
) & AC_PINCAP_EAPD
)) {
4123 int eapd
= snd_hda_codec_read(codec
, nid
, 0,
4124 AC_VERB_GET_EAPD_BTLENABLE
, 0);
4130 EXPORT_SYMBOL_GPL(snd_hda_codec_eapd_power_filter
);
4133 * set power state of the codec, and return the power state
4135 static unsigned int hda_set_power_state(struct hda_codec
*codec
,
4136 unsigned int power_state
)
4138 hda_nid_t fg
= codec
->afg
? codec
->afg
: codec
->mfg
;
4143 /* this delay seems necessary to avoid click noise at power-down */
4144 if (power_state
== AC_PWRST_D3
) {
4145 if (codec
->depop_delay
< 0)
4146 msleep(codec
->epss
? 10 : 100);
4147 else if (codec
->depop_delay
> 0)
4148 msleep(codec
->depop_delay
);
4149 flags
= HDA_RW_NO_RESPONSE_FALLBACK
;
4152 /* repeat power states setting at most 10 times*/
4153 for (count
= 0; count
< 10; count
++) {
4154 if (codec
->patch_ops
.set_power_state
)
4155 codec
->patch_ops
.set_power_state(codec
, fg
,
4158 state
= power_state
;
4159 if (codec
->power_filter
)
4160 state
= codec
->power_filter(codec
, fg
, state
);
4161 if (state
== power_state
|| power_state
!= AC_PWRST_D3
)
4162 snd_hda_codec_read(codec
, fg
, flags
,
4163 AC_VERB_SET_POWER_STATE
,
4165 snd_hda_codec_set_power_to_all(codec
, fg
, power_state
);
4167 state
= hda_sync_power_state(codec
, fg
, power_state
);
4168 if (!(state
& AC_PWRST_ERROR
))
4175 /* sync power states of all widgets;
4176 * this is called at the end of codec parsing
4178 static void sync_power_up_states(struct hda_codec
*codec
)
4180 hda_nid_t nid
= codec
->start_nid
;
4183 /* don't care if no filter is used */
4184 if (!codec
->power_filter
)
4187 for (i
= 0; i
< codec
->num_nodes
; i
++, nid
++) {
4188 unsigned int wcaps
= get_wcaps(codec
, nid
);
4189 unsigned int target
;
4190 if (!(wcaps
& AC_WCAP_POWER
))
4192 target
= codec
->power_filter(codec
, nid
, AC_PWRST_D0
);
4193 if (target
== AC_PWRST_D0
)
4195 if (!snd_hda_check_power_state(codec
, nid
, target
))
4196 snd_hda_codec_write(codec
, nid
, 0,
4197 AC_VERB_SET_POWER_STATE
, target
);
4201 #ifdef CONFIG_SND_HDA_RECONFIG
4202 /* execute additional init verbs */
4203 static void hda_exec_init_verbs(struct hda_codec
*codec
)
4205 if (codec
->init_verbs
.list
)
4206 snd_hda_sequence_write(codec
, codec
->init_verbs
.list
);
4209 static inline void hda_exec_init_verbs(struct hda_codec
*codec
) {}
4214 * call suspend and power-down; used both from PM and power-save
4215 * this function returns the power state in the end
4217 static unsigned int hda_call_codec_suspend(struct hda_codec
*codec
, bool in_wq
)
4223 if (codec
->patch_ops
.suspend
)
4224 codec
->patch_ops
.suspend(codec
);
4225 hda_cleanup_all_streams(codec
);
4226 state
= hda_set_power_state(codec
, AC_PWRST_D3
);
4227 /* Cancel delayed work if we aren't currently running from it. */
4229 cancel_delayed_work_sync(&codec
->power_work
);
4230 spin_lock(&codec
->power_lock
);
4231 snd_hda_update_power_acct(codec
);
4232 trace_hda_power_down(codec
);
4233 codec
->power_on
= 0;
4234 codec
->power_transition
= 0;
4235 codec
->power_jiffies
= jiffies
;
4236 spin_unlock(&codec
->power_lock
);
4241 /* mark all entries of cmd and amp caches dirty */
4242 static void hda_mark_cmd_cache_dirty(struct hda_codec
*codec
)
4245 for (i
= 0; i
< codec
->cmd_cache
.buf
.used
; i
++) {
4246 struct hda_cache_head
*cmd
;
4247 cmd
= snd_array_elem(&codec
->cmd_cache
.buf
, i
);
4250 for (i
= 0; i
< codec
->amp_cache
.buf
.used
; i
++) {
4251 struct hda_amp_info
*amp
;
4252 amp
= snd_array_elem(&codec
->amp_cache
.buf
, i
);
4253 amp
->head
.dirty
= 1;
4258 * kick up codec; used both from PM and power-save
4260 static void hda_call_codec_resume(struct hda_codec
*codec
)
4264 hda_mark_cmd_cache_dirty(codec
);
4266 /* set as if powered on for avoiding re-entering the resume
4267 * in the resume / power-save sequence
4269 hda_keep_power_on(codec
);
4270 hda_set_power_state(codec
, AC_PWRST_D0
);
4271 restore_shutup_pins(codec
);
4272 hda_exec_init_verbs(codec
);
4273 snd_hda_jack_set_dirty_all(codec
);
4274 if (codec
->patch_ops
.resume
)
4275 codec
->patch_ops
.resume(codec
);
4277 if (codec
->patch_ops
.init
)
4278 codec
->patch_ops
.init(codec
);
4279 snd_hda_codec_resume_amp(codec
);
4280 snd_hda_codec_resume_cache(codec
);
4283 if (codec
->jackpoll_interval
)
4284 hda_jackpoll_work(&codec
->jackpoll_work
.work
);
4286 snd_hda_jack_report_sync(codec
);
4289 snd_hda_power_down(codec
); /* flag down before returning */
4291 #endif /* CONFIG_PM */
4295 * snd_hda_build_controls - build mixer controls
4298 * Creates mixer controls for each codec included in the bus.
4300 * Returns 0 if successful, otherwise a negative error code.
4302 int snd_hda_build_controls(struct hda_bus
*bus
)
4304 struct hda_codec
*codec
;
4306 list_for_each_entry(codec
, &bus
->codec_list
, list
) {
4307 int err
= snd_hda_codec_build_controls(codec
);
4310 "cannot build controls for #%d (error %d)\n",
4312 err
= snd_hda_codec_reset(codec
);
4315 "cannot revert codec\n");
4322 EXPORT_SYMBOL_GPL(snd_hda_build_controls
);
4325 * add standard channel maps if not specified
4327 static int add_std_chmaps(struct hda_codec
*codec
)
4331 for (i
= 0; i
< codec
->num_pcms
; i
++) {
4332 for (str
= 0; str
< 2; str
++) {
4333 struct snd_pcm
*pcm
= codec
->pcm_info
[i
].pcm
;
4334 struct hda_pcm_stream
*hinfo
=
4335 &codec
->pcm_info
[i
].stream
[str
];
4336 struct snd_pcm_chmap
*chmap
;
4337 const struct snd_pcm_chmap_elem
*elem
;
4339 if (codec
->pcm_info
[i
].own_chmap
)
4341 if (!pcm
|| !hinfo
->substreams
)
4343 elem
= hinfo
->chmap
? hinfo
->chmap
: snd_pcm_std_chmaps
;
4344 err
= snd_pcm_add_chmap_ctls(pcm
, str
, elem
,
4345 hinfo
->channels_max
,
4349 chmap
->channel_mask
= SND_PCM_CHMAP_MASK_2468
;
4355 /* default channel maps for 2.1 speakers;
4356 * since HD-audio supports only stereo, odd number channels are omitted
4358 const struct snd_pcm_chmap_elem snd_pcm_2_1_chmaps
[] = {
4360 .map
= { SNDRV_CHMAP_FL
, SNDRV_CHMAP_FR
} },
4362 .map
= { SNDRV_CHMAP_FL
, SNDRV_CHMAP_FR
,
4363 SNDRV_CHMAP_LFE
, SNDRV_CHMAP_LFE
} },
4366 EXPORT_SYMBOL_GPL(snd_pcm_2_1_chmaps
);
4368 int snd_hda_codec_build_controls(struct hda_codec
*codec
)
4371 hda_exec_init_verbs(codec
);
4372 /* continue to initialize... */
4373 if (codec
->patch_ops
.init
)
4374 err
= codec
->patch_ops
.init(codec
);
4375 if (!err
&& codec
->patch_ops
.build_controls
)
4376 err
= codec
->patch_ops
.build_controls(codec
);
4380 /* we create chmaps here instead of build_pcms */
4381 err
= add_std_chmaps(codec
);
4385 if (codec
->jackpoll_interval
)
4386 hda_jackpoll_work(&codec
->jackpoll_work
.work
);
4388 snd_hda_jack_report_sync(codec
); /* call at the last init point */
4389 sync_power_up_states(codec
);
4396 struct hda_rate_tbl
{
4398 unsigned int alsa_bits
;
4399 unsigned int hda_fmt
;
4402 /* rate = base * mult / div */
4403 #define HDA_RATE(base, mult, div) \
4404 (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \
4405 (((div) - 1) << AC_FMT_DIV_SHIFT))
4407 static struct hda_rate_tbl rate_bits
[] = {
4408 /* rate in Hz, ALSA rate bitmask, HDA format value */
4410 /* autodetected value used in snd_hda_query_supported_pcm */
4411 { 8000, SNDRV_PCM_RATE_8000
, HDA_RATE(48, 1, 6) },
4412 { 11025, SNDRV_PCM_RATE_11025
, HDA_RATE(44, 1, 4) },
4413 { 16000, SNDRV_PCM_RATE_16000
, HDA_RATE(48, 1, 3) },
4414 { 22050, SNDRV_PCM_RATE_22050
, HDA_RATE(44, 1, 2) },
4415 { 32000, SNDRV_PCM_RATE_32000
, HDA_RATE(48, 2, 3) },
4416 { 44100, SNDRV_PCM_RATE_44100
, HDA_RATE(44, 1, 1) },
4417 { 48000, SNDRV_PCM_RATE_48000
, HDA_RATE(48, 1, 1) },
4418 { 88200, SNDRV_PCM_RATE_88200
, HDA_RATE(44, 2, 1) },
4419 { 96000, SNDRV_PCM_RATE_96000
, HDA_RATE(48, 2, 1) },
4420 { 176400, SNDRV_PCM_RATE_176400
, HDA_RATE(44, 4, 1) },
4421 { 192000, SNDRV_PCM_RATE_192000
, HDA_RATE(48, 4, 1) },
4422 #define AC_PAR_PCM_RATE_BITS 11
4423 /* up to bits 10, 384kHZ isn't supported properly */
4425 /* not autodetected value */
4426 { 9600, SNDRV_PCM_RATE_KNOT
, HDA_RATE(48, 1, 5) },
4428 { 0 } /* terminator */
4432 * snd_hda_calc_stream_format - calculate format bitset
4433 * @codec: HD-audio codec
4434 * @rate: the sample rate
4435 * @channels: the number of channels
4436 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
4437 * @maxbps: the max. bps
4438 * @spdif_ctls: HD-audio SPDIF status bits (0 if irrelevant)
4440 * Calculate the format bitset from the given rate, channels and th PCM format.
4442 * Return zero if invalid.
4444 unsigned int snd_hda_calc_stream_format(struct hda_codec
*codec
,
4446 unsigned int channels
,
4447 unsigned int format
,
4448 unsigned int maxbps
,
4449 unsigned short spdif_ctls
)
4452 unsigned int val
= 0;
4454 for (i
= 0; rate_bits
[i
].hz
; i
++)
4455 if (rate_bits
[i
].hz
== rate
) {
4456 val
= rate_bits
[i
].hda_fmt
;
4459 if (!rate_bits
[i
].hz
) {
4460 codec_dbg(codec
, "invalid rate %d\n", rate
);
4464 if (channels
== 0 || channels
> 8) {
4465 codec_dbg(codec
, "invalid channels %d\n", channels
);
4468 val
|= channels
- 1;
4470 switch (snd_pcm_format_width(format
)) {
4472 val
|= AC_FMT_BITS_8
;
4475 val
|= AC_FMT_BITS_16
;
4480 if (maxbps
>= 32 || format
== SNDRV_PCM_FORMAT_FLOAT_LE
)
4481 val
|= AC_FMT_BITS_32
;
4482 else if (maxbps
>= 24)
4483 val
|= AC_FMT_BITS_24
;
4485 val
|= AC_FMT_BITS_20
;
4488 codec_dbg(codec
, "invalid format width %d\n",
4489 snd_pcm_format_width(format
));
4493 if (spdif_ctls
& AC_DIG1_NONAUDIO
)
4494 val
|= AC_FMT_TYPE_NON_PCM
;
4498 EXPORT_SYMBOL_GPL(snd_hda_calc_stream_format
);
4500 static unsigned int get_pcm_param(struct hda_codec
*codec
, hda_nid_t nid
,
4503 unsigned int val
= 0;
4504 if (nid
!= codec
->afg
&&
4505 (get_wcaps(codec
, nid
) & AC_WCAP_FORMAT_OVRD
))
4506 val
= snd_hda_param_read(codec
, nid
, AC_PAR_PCM
);
4507 if (!val
|| val
== -1)
4508 val
= snd_hda_param_read(codec
, codec
->afg
, AC_PAR_PCM
);
4509 if (!val
|| val
== -1)
4514 static unsigned int query_pcm_param(struct hda_codec
*codec
, hda_nid_t nid
)
4516 return query_caps_hash(codec
, nid
, 0, HDA_HASH_PARPCM_KEY(nid
),
4520 static unsigned int get_stream_param(struct hda_codec
*codec
, hda_nid_t nid
,
4523 unsigned int streams
= snd_hda_param_read(codec
, nid
, AC_PAR_STREAM
);
4524 if (!streams
|| streams
== -1)
4525 streams
= snd_hda_param_read(codec
, codec
->afg
, AC_PAR_STREAM
);
4526 if (!streams
|| streams
== -1)
4531 static unsigned int query_stream_param(struct hda_codec
*codec
, hda_nid_t nid
)
4533 return query_caps_hash(codec
, nid
, 0, HDA_HASH_PARSTR_KEY(nid
),
4538 * snd_hda_query_supported_pcm - query the supported PCM rates and formats
4539 * @codec: the HDA codec
4540 * @nid: NID to query
4541 * @ratesp: the pointer to store the detected rate bitflags
4542 * @formatsp: the pointer to store the detected formats
4543 * @bpsp: the pointer to store the detected format widths
4545 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
4546 * or @bsps argument is ignored.
4548 * Returns 0 if successful, otherwise a negative error code.
4550 int snd_hda_query_supported_pcm(struct hda_codec
*codec
, hda_nid_t nid
,
4551 u32
*ratesp
, u64
*formatsp
, unsigned int *bpsp
)
4553 unsigned int i
, val
, wcaps
;
4555 wcaps
= get_wcaps(codec
, nid
);
4556 val
= query_pcm_param(codec
, nid
);
4560 for (i
= 0; i
< AC_PAR_PCM_RATE_BITS
; i
++) {
4562 rates
|= rate_bits
[i
].alsa_bits
;
4566 "rates == 0 (nid=0x%x, val=0x%x, ovrd=%i)\n",
4568 (wcaps
& AC_WCAP_FORMAT_OVRD
) ? 1 : 0);
4574 if (formatsp
|| bpsp
) {
4576 unsigned int streams
, bps
;
4578 streams
= query_stream_param(codec
, nid
);
4583 if (streams
& AC_SUPFMT_PCM
) {
4584 if (val
& AC_SUPPCM_BITS_8
) {
4585 formats
|= SNDRV_PCM_FMTBIT_U8
;
4588 if (val
& AC_SUPPCM_BITS_16
) {
4589 formats
|= SNDRV_PCM_FMTBIT_S16_LE
;
4592 if (wcaps
& AC_WCAP_DIGITAL
) {
4593 if (val
& AC_SUPPCM_BITS_32
)
4594 formats
|= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
;
4595 if (val
& (AC_SUPPCM_BITS_20
|AC_SUPPCM_BITS_24
))
4596 formats
|= SNDRV_PCM_FMTBIT_S32_LE
;
4597 if (val
& AC_SUPPCM_BITS_24
)
4599 else if (val
& AC_SUPPCM_BITS_20
)
4601 } else if (val
& (AC_SUPPCM_BITS_20
|AC_SUPPCM_BITS_24
|
4602 AC_SUPPCM_BITS_32
)) {
4603 formats
|= SNDRV_PCM_FMTBIT_S32_LE
;
4604 if (val
& AC_SUPPCM_BITS_32
)
4606 else if (val
& AC_SUPPCM_BITS_24
)
4608 else if (val
& AC_SUPPCM_BITS_20
)
4612 #if 0 /* FIXME: CS4206 doesn't work, which is the only codec supporting float */
4613 if (streams
& AC_SUPFMT_FLOAT32
) {
4614 formats
|= SNDRV_PCM_FMTBIT_FLOAT_LE
;
4619 if (streams
== AC_SUPFMT_AC3
) {
4620 /* should be exclusive */
4621 /* temporary hack: we have still no proper support
4622 * for the direct AC3 stream...
4624 formats
|= SNDRV_PCM_FMTBIT_U8
;
4629 "formats == 0 (nid=0x%x, val=0x%x, ovrd=%i, streams=0x%x)\n",
4631 (wcaps
& AC_WCAP_FORMAT_OVRD
) ? 1 : 0,
4636 *formatsp
= formats
;
4643 EXPORT_SYMBOL_GPL(snd_hda_query_supported_pcm
);
4646 * snd_hda_is_supported_format - Check the validity of the format
4647 * @codec: HD-audio codec
4648 * @nid: NID to check
4649 * @format: the HD-audio format value to check
4651 * Check whether the given node supports the format value.
4653 * Returns 1 if supported, 0 if not.
4655 int snd_hda_is_supported_format(struct hda_codec
*codec
, hda_nid_t nid
,
4656 unsigned int format
)
4659 unsigned int val
= 0, rate
, stream
;
4661 val
= query_pcm_param(codec
, nid
);
4665 rate
= format
& 0xff00;
4666 for (i
= 0; i
< AC_PAR_PCM_RATE_BITS
; i
++)
4667 if (rate_bits
[i
].hda_fmt
== rate
) {
4672 if (i
>= AC_PAR_PCM_RATE_BITS
)
4675 stream
= query_stream_param(codec
, nid
);
4679 if (stream
& AC_SUPFMT_PCM
) {
4680 switch (format
& 0xf0) {
4682 if (!(val
& AC_SUPPCM_BITS_8
))
4686 if (!(val
& AC_SUPPCM_BITS_16
))
4690 if (!(val
& AC_SUPPCM_BITS_20
))
4694 if (!(val
& AC_SUPPCM_BITS_24
))
4698 if (!(val
& AC_SUPPCM_BITS_32
))
4705 /* FIXME: check for float32 and AC3? */
4710 EXPORT_SYMBOL_GPL(snd_hda_is_supported_format
);
4715 static int hda_pcm_default_open_close(struct hda_pcm_stream
*hinfo
,
4716 struct hda_codec
*codec
,
4717 struct snd_pcm_substream
*substream
)
4722 static int hda_pcm_default_prepare(struct hda_pcm_stream
*hinfo
,
4723 struct hda_codec
*codec
,
4724 unsigned int stream_tag
,
4725 unsigned int format
,
4726 struct snd_pcm_substream
*substream
)
4728 snd_hda_codec_setup_stream(codec
, hinfo
->nid
, stream_tag
, 0, format
);
4732 static int hda_pcm_default_cleanup(struct hda_pcm_stream
*hinfo
,
4733 struct hda_codec
*codec
,
4734 struct snd_pcm_substream
*substream
)
4736 snd_hda_codec_cleanup_stream(codec
, hinfo
->nid
);
4740 static int set_pcm_default_values(struct hda_codec
*codec
,
4741 struct hda_pcm_stream
*info
)
4745 /* query support PCM information from the given NID */
4746 if (info
->nid
&& (!info
->rates
|| !info
->formats
)) {
4747 err
= snd_hda_query_supported_pcm(codec
, info
->nid
,
4748 info
->rates
? NULL
: &info
->rates
,
4749 info
->formats
? NULL
: &info
->formats
,
4750 info
->maxbps
? NULL
: &info
->maxbps
);
4754 if (info
->ops
.open
== NULL
)
4755 info
->ops
.open
= hda_pcm_default_open_close
;
4756 if (info
->ops
.close
== NULL
)
4757 info
->ops
.close
= hda_pcm_default_open_close
;
4758 if (info
->ops
.prepare
== NULL
) {
4759 if (snd_BUG_ON(!info
->nid
))
4761 info
->ops
.prepare
= hda_pcm_default_prepare
;
4763 if (info
->ops
.cleanup
== NULL
) {
4764 if (snd_BUG_ON(!info
->nid
))
4766 info
->ops
.cleanup
= hda_pcm_default_cleanup
;
4772 * codec prepare/cleanup entries
4775 * snd_hda_codec_prepare - Prepare a stream
4776 * @codec: the HDA codec
4777 * @hinfo: PCM information
4778 * @stream: stream tag to assign
4779 * @format: format id to assign
4780 * @substream: PCM substream to assign
4782 * Calls the prepare callback set by the codec with the given arguments.
4783 * Clean up the inactive streams when successful.
4785 int snd_hda_codec_prepare(struct hda_codec
*codec
,
4786 struct hda_pcm_stream
*hinfo
,
4787 unsigned int stream
,
4788 unsigned int format
,
4789 struct snd_pcm_substream
*substream
)
4792 mutex_lock(&codec
->bus
->prepare_mutex
);
4793 ret
= hinfo
->ops
.prepare(hinfo
, codec
, stream
, format
, substream
);
4795 purify_inactive_streams(codec
);
4796 mutex_unlock(&codec
->bus
->prepare_mutex
);
4799 EXPORT_SYMBOL_GPL(snd_hda_codec_prepare
);
4802 * snd_hda_codec_cleanup - Prepare a stream
4803 * @codec: the HDA codec
4804 * @hinfo: PCM information
4805 * @substream: PCM substream
4807 * Calls the cleanup callback set by the codec with the given arguments.
4809 void snd_hda_codec_cleanup(struct hda_codec
*codec
,
4810 struct hda_pcm_stream
*hinfo
,
4811 struct snd_pcm_substream
*substream
)
4813 mutex_lock(&codec
->bus
->prepare_mutex
);
4814 hinfo
->ops
.cleanup(hinfo
, codec
, substream
);
4815 mutex_unlock(&codec
->bus
->prepare_mutex
);
4817 EXPORT_SYMBOL_GPL(snd_hda_codec_cleanup
);
4820 const char *snd_hda_pcm_type_name
[HDA_PCM_NTYPES
] = {
4821 "Audio", "SPDIF", "HDMI", "Modem"
4825 * get the empty PCM device number to assign
4827 static int get_empty_pcm_device(struct hda_bus
*bus
, unsigned int type
)
4829 /* audio device indices; not linear to keep compatibility */
4830 /* assigned to static slots up to dev#10; if more needed, assign
4831 * the later slot dynamically (when CONFIG_SND_DYNAMIC_MINORS=y)
4833 static int audio_idx
[HDA_PCM_NTYPES
][5] = {
4834 [HDA_PCM_TYPE_AUDIO
] = { 0, 2, 4, 5, -1 },
4835 [HDA_PCM_TYPE_SPDIF
] = { 1, -1 },
4836 [HDA_PCM_TYPE_HDMI
] = { 3, 7, 8, 9, -1 },
4837 [HDA_PCM_TYPE_MODEM
] = { 6, -1 },
4841 if (type
>= HDA_PCM_NTYPES
) {
4842 dev_err(bus
->card
->dev
, "Invalid PCM type %d\n", type
);
4846 for (i
= 0; audio_idx
[type
][i
] >= 0; i
++) {
4847 #ifndef CONFIG_SND_DYNAMIC_MINORS
4848 if (audio_idx
[type
][i
] >= 8)
4851 if (!test_and_set_bit(audio_idx
[type
][i
], bus
->pcm_dev_bits
))
4852 return audio_idx
[type
][i
];
4855 #ifdef CONFIG_SND_DYNAMIC_MINORS
4856 /* non-fixed slots starting from 10 */
4857 for (i
= 10; i
< 32; i
++) {
4858 if (!test_and_set_bit(i
, bus
->pcm_dev_bits
))
4863 dev_warn(bus
->card
->dev
, "Too many %s devices\n",
4864 snd_hda_pcm_type_name
[type
]);
4865 #ifndef CONFIG_SND_DYNAMIC_MINORS
4866 dev_warn(bus
->card
->dev
,
4867 "Consider building the kernel with CONFIG_SND_DYNAMIC_MINORS=y\n");
4873 * attach a new PCM stream
4875 static int snd_hda_attach_pcm(struct hda_codec
*codec
, struct hda_pcm
*pcm
)
4877 struct hda_bus
*bus
= codec
->bus
;
4878 struct hda_pcm_stream
*info
;
4881 if (snd_BUG_ON(!pcm
->name
))
4883 for (stream
= 0; stream
< 2; stream
++) {
4884 info
= &pcm
->stream
[stream
];
4885 if (info
->substreams
) {
4886 err
= set_pcm_default_values(codec
, info
);
4891 return bus
->ops
.attach_pcm(bus
, codec
, pcm
);
4894 /* assign all PCMs of the given codec */
4895 int snd_hda_codec_build_pcms(struct hda_codec
*codec
)
4900 if (!codec
->num_pcms
) {
4901 if (!codec
->patch_ops
.build_pcms
)
4903 err
= codec
->patch_ops
.build_pcms(codec
);
4906 "cannot build PCMs for #%d (error %d)\n",
4908 err
= snd_hda_codec_reset(codec
);
4911 "cannot revert codec\n");
4916 for (pcm
= 0; pcm
< codec
->num_pcms
; pcm
++) {
4917 struct hda_pcm
*cpcm
= &codec
->pcm_info
[pcm
];
4920 if (!cpcm
->stream
[0].substreams
&& !cpcm
->stream
[1].substreams
)
4921 continue; /* no substreams assigned */
4924 dev
= get_empty_pcm_device(codec
->bus
, cpcm
->pcm_type
);
4926 continue; /* no fatal error */
4928 err
= snd_hda_attach_pcm(codec
, cpcm
);
4931 "cannot attach PCM stream %d for codec #%d\n",
4933 continue; /* no fatal error */
4941 * snd_hda_build_pcms - build PCM information
4944 * Create PCM information for each codec included in the bus.
4946 * The build_pcms codec patch is requested to set up codec->num_pcms and
4947 * codec->pcm_info properly. The array is referred by the top-level driver
4948 * to create its PCM instances.
4949 * The allocated codec->pcm_info should be released in codec->patch_ops.free
4952 * At least, substreams, channels_min and channels_max must be filled for
4953 * each stream. substreams = 0 indicates that the stream doesn't exist.
4954 * When rates and/or formats are zero, the supported values are queried
4955 * from the given nid. The nid is used also by the default ops.prepare
4956 * and ops.cleanup callbacks.
4958 * The driver needs to call ops.open in its open callback. Similarly,
4959 * ops.close is supposed to be called in the close callback.
4960 * ops.prepare should be called in the prepare or hw_params callback
4961 * with the proper parameters for set up.
4962 * ops.cleanup should be called in hw_free for clean up of streams.
4964 * This function returns 0 if successful, or a negative error code.
4966 int snd_hda_build_pcms(struct hda_bus
*bus
)
4968 struct hda_codec
*codec
;
4970 list_for_each_entry(codec
, &bus
->codec_list
, list
) {
4971 int err
= snd_hda_codec_build_pcms(codec
);
4977 EXPORT_SYMBOL_GPL(snd_hda_build_pcms
);
4980 * snd_hda_add_new_ctls - create controls from the array
4981 * @codec: the HDA codec
4982 * @knew: the array of struct snd_kcontrol_new
4984 * This helper function creates and add new controls in the given array.
4985 * The array must be terminated with an empty entry as terminator.
4987 * Returns 0 if successful, or a negative error code.
4989 int snd_hda_add_new_ctls(struct hda_codec
*codec
,
4990 const struct snd_kcontrol_new
*knew
)
4994 for (; knew
->name
; knew
++) {
4995 struct snd_kcontrol
*kctl
;
4996 int addr
= 0, idx
= 0;
4997 if (knew
->iface
== -1) /* skip this codec private value */
5000 kctl
= snd_ctl_new1(knew
, codec
);
5004 kctl
->id
.device
= addr
;
5006 kctl
->id
.index
= idx
;
5007 err
= snd_hda_ctl_add(codec
, 0, kctl
);
5010 /* try first with another device index corresponding to
5011 * the codec addr; if it still fails (or it's the
5012 * primary codec), then try another control index
5014 if (!addr
&& codec
->addr
)
5016 else if (!idx
&& !knew
->index
) {
5017 idx
= find_empty_mixer_ctl_idx(codec
,
5027 EXPORT_SYMBOL_GPL(snd_hda_add_new_ctls
);
5030 static void hda_power_work(struct work_struct
*work
)
5032 struct hda_codec
*codec
=
5033 container_of(work
, struct hda_codec
, power_work
.work
);
5034 struct hda_bus
*bus
= codec
->bus
;
5037 spin_lock(&codec
->power_lock
);
5038 if (codec
->power_transition
> 0) { /* during power-up sequence? */
5039 spin_unlock(&codec
->power_lock
);
5042 if (!codec
->power_on
|| codec
->power_count
) {
5043 codec
->power_transition
= 0;
5044 spin_unlock(&codec
->power_lock
);
5047 spin_unlock(&codec
->power_lock
);
5049 state
= hda_call_codec_suspend(codec
, true);
5050 if (!bus
->power_keep_link_on
&& (state
& AC_PWRST_CLK_STOP_OK
))
5051 hda_call_pm_notify(codec
, false);
5054 static void hda_keep_power_on(struct hda_codec
*codec
)
5056 spin_lock(&codec
->power_lock
);
5057 codec
->power_count
++;
5058 codec
->power_on
= 1;
5059 codec
->power_jiffies
= jiffies
;
5060 spin_unlock(&codec
->power_lock
);
5061 hda_call_pm_notify(codec
, true);
5064 /* update the power on/off account with the current jiffies */
5065 void snd_hda_update_power_acct(struct hda_codec
*codec
)
5067 unsigned long delta
= jiffies
- codec
->power_jiffies
;
5068 if (codec
->power_on
)
5069 codec
->power_on_acct
+= delta
;
5071 codec
->power_off_acct
+= delta
;
5072 codec
->power_jiffies
+= delta
;
5075 /* Transition to powered up, if wait_power_down then wait for a pending
5076 * transition to D3 to complete. A pending D3 transition is indicated
5077 * with power_transition == -1. */
5078 /* call this with codec->power_lock held! */
5079 static void __snd_hda_power_up(struct hda_codec
*codec
, bool wait_power_down
)
5081 /* Return if power_on or transitioning to power_on, unless currently
5083 if ((codec
->power_on
|| codec
->power_transition
> 0) &&
5084 !(wait_power_down
&& codec
->power_transition
< 0))
5086 spin_unlock(&codec
->power_lock
);
5088 cancel_delayed_work_sync(&codec
->power_work
);
5090 spin_lock(&codec
->power_lock
);
5091 /* If the power down delayed work was cancelled above before starting,
5092 * then there is no need to go through power up here.
5094 if (codec
->power_on
) {
5095 if (codec
->power_transition
< 0)
5096 codec
->power_transition
= 0;
5100 trace_hda_power_up(codec
);
5101 snd_hda_update_power_acct(codec
);
5102 codec
->power_on
= 1;
5103 codec
->power_jiffies
= jiffies
;
5104 codec
->power_transition
= 1; /* avoid reentrance */
5105 spin_unlock(&codec
->power_lock
);
5107 hda_call_codec_resume(codec
);
5109 spin_lock(&codec
->power_lock
);
5110 codec
->power_transition
= 0;
5113 #define power_save(codec) \
5114 ((codec)->bus->power_save ? *(codec)->bus->power_save : 0)
5116 /* Transition to powered down */
5117 static void __snd_hda_power_down(struct hda_codec
*codec
)
5119 if (!codec
->power_on
|| codec
->power_count
|| codec
->power_transition
)
5122 if (power_save(codec
)) {
5123 codec
->power_transition
= -1; /* avoid reentrance */
5124 queue_delayed_work(codec
->bus
->workq
, &codec
->power_work
,
5125 msecs_to_jiffies(power_save(codec
) * 1000));
5130 * snd_hda_power_save - Power-up/down/sync the codec
5131 * @codec: HD-audio codec
5132 * @delta: the counter delta to change
5133 * @d3wait: sync for D3 transition complete
5135 * Change the power-up counter via @delta, and power up or down the hardware
5136 * appropriately. For the power-down, queue to the delayed action.
5137 * Passing zero to @delta means to synchronize the power state.
5139 void snd_hda_power_save(struct hda_codec
*codec
, int delta
, bool d3wait
)
5141 spin_lock(&codec
->power_lock
);
5142 codec
->power_count
+= delta
;
5143 trace_hda_power_count(codec
);
5145 __snd_hda_power_up(codec
, d3wait
);
5147 __snd_hda_power_down(codec
);
5148 spin_unlock(&codec
->power_lock
);
5150 EXPORT_SYMBOL_GPL(snd_hda_power_save
);
5153 * snd_hda_check_amp_list_power - Check the amp list and update the power
5154 * @codec: HD-audio codec
5155 * @check: the object containing an AMP list and the status
5156 * @nid: NID to check / update
5158 * Check whether the given NID is in the amp list. If it's in the list,
5159 * check the current AMP status, and update the the power-status according
5160 * to the mute status.
5162 * This function is supposed to be set or called from the check_power_status
5165 int snd_hda_check_amp_list_power(struct hda_codec
*codec
,
5166 struct hda_loopback_check
*check
,
5169 const struct hda_amp_list
*p
;
5172 if (!check
->amplist
)
5174 for (p
= check
->amplist
; p
->nid
; p
++) {
5179 return 0; /* nothing changed */
5181 for (p
= check
->amplist
; p
->nid
; p
++) {
5182 for (ch
= 0; ch
< 2; ch
++) {
5183 v
= snd_hda_codec_amp_read(codec
, p
->nid
, ch
, p
->dir
,
5185 if (!(v
& HDA_AMP_MUTE
) && v
> 0) {
5186 if (!check
->power_on
) {
5187 check
->power_on
= 1;
5188 snd_hda_power_up(codec
);
5194 if (check
->power_on
) {
5195 check
->power_on
= 0;
5196 snd_hda_power_down(codec
);
5200 EXPORT_SYMBOL_GPL(snd_hda_check_amp_list_power
);
5204 * Channel mode helper
5208 * snd_hda_ch_mode_info - Info callback helper for the channel mode enum
5209 * @codec: the HDA codec
5210 * @uinfo: pointer to get/store the data
5211 * @chmode: channel mode array
5212 * @num_chmodes: channel mode array size
5214 int snd_hda_ch_mode_info(struct hda_codec
*codec
,
5215 struct snd_ctl_elem_info
*uinfo
,
5216 const struct hda_channel_mode
*chmode
,
5219 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_ENUMERATED
;
5221 uinfo
->value
.enumerated
.items
= num_chmodes
;
5222 if (uinfo
->value
.enumerated
.item
>= num_chmodes
)
5223 uinfo
->value
.enumerated
.item
= num_chmodes
- 1;
5224 sprintf(uinfo
->value
.enumerated
.name
, "%dch",
5225 chmode
[uinfo
->value
.enumerated
.item
].channels
);
5228 EXPORT_SYMBOL_GPL(snd_hda_ch_mode_info
);
5231 * snd_hda_ch_mode_get - Get callback helper for the channel mode enum
5232 * @codec: the HDA codec
5233 * @ucontrol: pointer to get/store the data
5234 * @chmode: channel mode array
5235 * @num_chmodes: channel mode array size
5236 * @max_channels: max number of channels
5238 int snd_hda_ch_mode_get(struct hda_codec
*codec
,
5239 struct snd_ctl_elem_value
*ucontrol
,
5240 const struct hda_channel_mode
*chmode
,
5246 for (i
= 0; i
< num_chmodes
; i
++) {
5247 if (max_channels
== chmode
[i
].channels
) {
5248 ucontrol
->value
.enumerated
.item
[0] = i
;
5254 EXPORT_SYMBOL_GPL(snd_hda_ch_mode_get
);
5257 * snd_hda_ch_mode_put - Put callback helper for the channel mode enum
5258 * @codec: the HDA codec
5259 * @ucontrol: pointer to get/store the data
5260 * @chmode: channel mode array
5261 * @num_chmodes: channel mode array size
5262 * @max_channelsp: pointer to store the max channels
5264 int snd_hda_ch_mode_put(struct hda_codec
*codec
,
5265 struct snd_ctl_elem_value
*ucontrol
,
5266 const struct hda_channel_mode
*chmode
,
5272 mode
= ucontrol
->value
.enumerated
.item
[0];
5273 if (mode
>= num_chmodes
)
5275 if (*max_channelsp
== chmode
[mode
].channels
)
5277 /* change the current channel setting */
5278 *max_channelsp
= chmode
[mode
].channels
;
5279 if (chmode
[mode
].sequence
)
5280 snd_hda_sequence_write_cache(codec
, chmode
[mode
].sequence
);
5283 EXPORT_SYMBOL_GPL(snd_hda_ch_mode_put
);
5290 * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum
5291 * @imux: imux helper object
5292 * @uinfo: pointer to get/store the data
5294 int snd_hda_input_mux_info(const struct hda_input_mux
*imux
,
5295 struct snd_ctl_elem_info
*uinfo
)
5299 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_ENUMERATED
;
5301 uinfo
->value
.enumerated
.items
= imux
->num_items
;
5302 if (!imux
->num_items
)
5304 index
= uinfo
->value
.enumerated
.item
;
5305 if (index
>= imux
->num_items
)
5306 index
= imux
->num_items
- 1;
5307 strcpy(uinfo
->value
.enumerated
.name
, imux
->items
[index
].label
);
5310 EXPORT_SYMBOL_GPL(snd_hda_input_mux_info
);
5313 * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum
5314 * @codec: the HDA codec
5315 * @imux: imux helper object
5316 * @ucontrol: pointer to get/store the data
5317 * @nid: input mux NID
5318 * @cur_val: pointer to get/store the current imux value
5320 int snd_hda_input_mux_put(struct hda_codec
*codec
,
5321 const struct hda_input_mux
*imux
,
5322 struct snd_ctl_elem_value
*ucontrol
,
5324 unsigned int *cur_val
)
5328 if (!imux
->num_items
)
5330 idx
= ucontrol
->value
.enumerated
.item
[0];
5331 if (idx
>= imux
->num_items
)
5332 idx
= imux
->num_items
- 1;
5333 if (*cur_val
== idx
)
5335 snd_hda_codec_write_cache(codec
, nid
, 0, AC_VERB_SET_CONNECT_SEL
,
5336 imux
->items
[idx
].index
);
5340 EXPORT_SYMBOL_GPL(snd_hda_input_mux_put
);
5344 * snd_hda_enum_helper_info - Helper for simple enum ctls
5345 * @kcontrol: ctl element
5346 * @uinfo: pointer to get/store the data
5347 * @num_items: number of enum items
5348 * @texts: enum item string array
5350 * process kcontrol info callback of a simple string enum array
5351 * when @num_items is 0 or @texts is NULL, assume a boolean enum array
5353 int snd_hda_enum_helper_info(struct snd_kcontrol
*kcontrol
,
5354 struct snd_ctl_elem_info
*uinfo
,
5355 int num_items
, const char * const *texts
)
5357 static const char * const texts_default
[] = {
5358 "Disabled", "Enabled"
5361 if (!texts
|| !num_items
) {
5363 texts
= texts_default
;
5366 return snd_ctl_enum_info(uinfo
, 1, num_items
, texts
);
5368 EXPORT_SYMBOL_GPL(snd_hda_enum_helper_info
);
5371 * Multi-channel / digital-out PCM helper functions
5374 /* setup SPDIF output stream */
5375 static void setup_dig_out_stream(struct hda_codec
*codec
, hda_nid_t nid
,
5376 unsigned int stream_tag
, unsigned int format
)
5378 struct hda_spdif_out
*spdif
;
5379 unsigned int curr_fmt
;
5382 spdif
= snd_hda_spdif_out_of_nid(codec
, nid
);
5383 curr_fmt
= snd_hda_codec_read(codec
, nid
, 0,
5384 AC_VERB_GET_STREAM_FORMAT
, 0);
5385 reset
= codec
->spdif_status_reset
&&
5386 (spdif
->ctls
& AC_DIG1_ENABLE
) &&
5389 /* turn off SPDIF if needed; otherwise the IEC958 bits won't be
5392 set_dig_out_convert(codec
, nid
,
5393 spdif
->ctls
& ~AC_DIG1_ENABLE
& 0xff,
5395 snd_hda_codec_setup_stream(codec
, nid
, stream_tag
, 0, format
);
5396 if (codec
->slave_dig_outs
) {
5398 for (d
= codec
->slave_dig_outs
; *d
; d
++)
5399 snd_hda_codec_setup_stream(codec
, *d
, stream_tag
, 0,
5402 /* turn on again (if needed) */
5404 set_dig_out_convert(codec
, nid
,
5405 spdif
->ctls
& 0xff, -1);
5408 static void cleanup_dig_out_stream(struct hda_codec
*codec
, hda_nid_t nid
)
5410 snd_hda_codec_cleanup_stream(codec
, nid
);
5411 if (codec
->slave_dig_outs
) {
5413 for (d
= codec
->slave_dig_outs
; *d
; d
++)
5414 snd_hda_codec_cleanup_stream(codec
, *d
);
5419 * snd_hda_bus_reboot_notify - call the reboot notifier of each codec
5420 * @bus: HD-audio bus
5422 void snd_hda_bus_reboot_notify(struct hda_bus
*bus
)
5424 struct hda_codec
*codec
;
5428 list_for_each_entry(codec
, &bus
->codec_list
, list
) {
5429 if (hda_codec_is_power_on(codec
) &&
5430 codec
->patch_ops
.reboot_notify
)
5431 codec
->patch_ops
.reboot_notify(codec
);
5434 EXPORT_SYMBOL_GPL(snd_hda_bus_reboot_notify
);
5437 * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode
5438 * @codec: the HDA codec
5439 * @mout: hda_multi_out object
5441 int snd_hda_multi_out_dig_open(struct hda_codec
*codec
,
5442 struct hda_multi_out
*mout
)
5444 mutex_lock(&codec
->spdif_mutex
);
5445 if (mout
->dig_out_used
== HDA_DIG_ANALOG_DUP
)
5446 /* already opened as analog dup; reset it once */
5447 cleanup_dig_out_stream(codec
, mout
->dig_out_nid
);
5448 mout
->dig_out_used
= HDA_DIG_EXCLUSIVE
;
5449 mutex_unlock(&codec
->spdif_mutex
);
5452 EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_open
);
5455 * snd_hda_multi_out_dig_prepare - prepare the digital out stream
5456 * @codec: the HDA codec
5457 * @mout: hda_multi_out object
5458 * @stream_tag: stream tag to assign
5459 * @format: format id to assign
5460 * @substream: PCM substream to assign
5462 int snd_hda_multi_out_dig_prepare(struct hda_codec
*codec
,
5463 struct hda_multi_out
*mout
,
5464 unsigned int stream_tag
,
5465 unsigned int format
,
5466 struct snd_pcm_substream
*substream
)
5468 mutex_lock(&codec
->spdif_mutex
);
5469 setup_dig_out_stream(codec
, mout
->dig_out_nid
, stream_tag
, format
);
5470 mutex_unlock(&codec
->spdif_mutex
);
5473 EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_prepare
);
5476 * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream
5477 * @codec: the HDA codec
5478 * @mout: hda_multi_out object
5480 int snd_hda_multi_out_dig_cleanup(struct hda_codec
*codec
,
5481 struct hda_multi_out
*mout
)
5483 mutex_lock(&codec
->spdif_mutex
);
5484 cleanup_dig_out_stream(codec
, mout
->dig_out_nid
);
5485 mutex_unlock(&codec
->spdif_mutex
);
5488 EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_cleanup
);
5491 * snd_hda_multi_out_dig_close - release the digital out stream
5492 * @codec: the HDA codec
5493 * @mout: hda_multi_out object
5495 int snd_hda_multi_out_dig_close(struct hda_codec
*codec
,
5496 struct hda_multi_out
*mout
)
5498 mutex_lock(&codec
->spdif_mutex
);
5499 mout
->dig_out_used
= 0;
5500 mutex_unlock(&codec
->spdif_mutex
);
5503 EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_close
);
5506 * snd_hda_multi_out_analog_open - open analog outputs
5507 * @codec: the HDA codec
5508 * @mout: hda_multi_out object
5509 * @substream: PCM substream to assign
5510 * @hinfo: PCM information to assign
5512 * Open analog outputs and set up the hw-constraints.
5513 * If the digital outputs can be opened as slave, open the digital
5516 int snd_hda_multi_out_analog_open(struct hda_codec
*codec
,
5517 struct hda_multi_out
*mout
,
5518 struct snd_pcm_substream
*substream
,
5519 struct hda_pcm_stream
*hinfo
)
5521 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
5522 runtime
->hw
.channels_max
= mout
->max_channels
;
5523 if (mout
->dig_out_nid
) {
5524 if (!mout
->analog_rates
) {
5525 mout
->analog_rates
= hinfo
->rates
;
5526 mout
->analog_formats
= hinfo
->formats
;
5527 mout
->analog_maxbps
= hinfo
->maxbps
;
5529 runtime
->hw
.rates
= mout
->analog_rates
;
5530 runtime
->hw
.formats
= mout
->analog_formats
;
5531 hinfo
->maxbps
= mout
->analog_maxbps
;
5533 if (!mout
->spdif_rates
) {
5534 snd_hda_query_supported_pcm(codec
, mout
->dig_out_nid
,
5536 &mout
->spdif_formats
,
5537 &mout
->spdif_maxbps
);
5539 mutex_lock(&codec
->spdif_mutex
);
5540 if (mout
->share_spdif
) {
5541 if ((runtime
->hw
.rates
& mout
->spdif_rates
) &&
5542 (runtime
->hw
.formats
& mout
->spdif_formats
)) {
5543 runtime
->hw
.rates
&= mout
->spdif_rates
;
5544 runtime
->hw
.formats
&= mout
->spdif_formats
;
5545 if (mout
->spdif_maxbps
< hinfo
->maxbps
)
5546 hinfo
->maxbps
= mout
->spdif_maxbps
;
5548 mout
->share_spdif
= 0;
5549 /* FIXME: need notify? */
5552 mutex_unlock(&codec
->spdif_mutex
);
5554 return snd_pcm_hw_constraint_step(substream
->runtime
, 0,
5555 SNDRV_PCM_HW_PARAM_CHANNELS
, 2);
5557 EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_open
);
5560 * snd_hda_multi_out_analog_prepare - Preapre the analog outputs.
5561 * @codec: the HDA codec
5562 * @mout: hda_multi_out object
5563 * @stream_tag: stream tag to assign
5564 * @format: format id to assign
5565 * @substream: PCM substream to assign
5567 * Set up the i/o for analog out.
5568 * When the digital out is available, copy the front out to digital out, too.
5570 int snd_hda_multi_out_analog_prepare(struct hda_codec
*codec
,
5571 struct hda_multi_out
*mout
,
5572 unsigned int stream_tag
,
5573 unsigned int format
,
5574 struct snd_pcm_substream
*substream
)
5576 const hda_nid_t
*nids
= mout
->dac_nids
;
5577 int chs
= substream
->runtime
->channels
;
5578 struct hda_spdif_out
*spdif
;
5581 mutex_lock(&codec
->spdif_mutex
);
5582 spdif
= snd_hda_spdif_out_of_nid(codec
, mout
->dig_out_nid
);
5583 if (mout
->dig_out_nid
&& mout
->share_spdif
&&
5584 mout
->dig_out_used
!= HDA_DIG_EXCLUSIVE
) {
5586 snd_hda_is_supported_format(codec
, mout
->dig_out_nid
,
5588 !(spdif
->status
& IEC958_AES0_NONAUDIO
)) {
5589 mout
->dig_out_used
= HDA_DIG_ANALOG_DUP
;
5590 setup_dig_out_stream(codec
, mout
->dig_out_nid
,
5591 stream_tag
, format
);
5593 mout
->dig_out_used
= 0;
5594 cleanup_dig_out_stream(codec
, mout
->dig_out_nid
);
5597 mutex_unlock(&codec
->spdif_mutex
);
5600 snd_hda_codec_setup_stream(codec
, nids
[HDA_FRONT
], stream_tag
,
5602 if (!mout
->no_share_stream
&&
5603 mout
->hp_nid
&& mout
->hp_nid
!= nids
[HDA_FRONT
])
5604 /* headphone out will just decode front left/right (stereo) */
5605 snd_hda_codec_setup_stream(codec
, mout
->hp_nid
, stream_tag
,
5607 /* extra outputs copied from front */
5608 for (i
= 0; i
< ARRAY_SIZE(mout
->hp_out_nid
); i
++)
5609 if (!mout
->no_share_stream
&& mout
->hp_out_nid
[i
])
5610 snd_hda_codec_setup_stream(codec
,
5611 mout
->hp_out_nid
[i
],
5612 stream_tag
, 0, format
);
5615 for (i
= 1; i
< mout
->num_dacs
; i
++) {
5616 if (chs
>= (i
+ 1) * 2) /* independent out */
5617 snd_hda_codec_setup_stream(codec
, nids
[i
], stream_tag
,
5619 else if (!mout
->no_share_stream
) /* copy front */
5620 snd_hda_codec_setup_stream(codec
, nids
[i
], stream_tag
,
5624 /* extra surrounds */
5625 for (i
= 0; i
< ARRAY_SIZE(mout
->extra_out_nid
); i
++) {
5627 if (!mout
->extra_out_nid
[i
])
5629 if (chs
>= (i
+ 1) * 2)
5631 else if (!mout
->no_share_stream
)
5633 snd_hda_codec_setup_stream(codec
, mout
->extra_out_nid
[i
],
5634 stream_tag
, ch
, format
);
5639 EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_prepare
);
5642 * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out
5643 * @codec: the HDA codec
5644 * @mout: hda_multi_out object
5646 int snd_hda_multi_out_analog_cleanup(struct hda_codec
*codec
,
5647 struct hda_multi_out
*mout
)
5649 const hda_nid_t
*nids
= mout
->dac_nids
;
5652 for (i
= 0; i
< mout
->num_dacs
; i
++)
5653 snd_hda_codec_cleanup_stream(codec
, nids
[i
]);
5655 snd_hda_codec_cleanup_stream(codec
, mout
->hp_nid
);
5656 for (i
= 0; i
< ARRAY_SIZE(mout
->hp_out_nid
); i
++)
5657 if (mout
->hp_out_nid
[i
])
5658 snd_hda_codec_cleanup_stream(codec
,
5659 mout
->hp_out_nid
[i
]);
5660 for (i
= 0; i
< ARRAY_SIZE(mout
->extra_out_nid
); i
++)
5661 if (mout
->extra_out_nid
[i
])
5662 snd_hda_codec_cleanup_stream(codec
,
5663 mout
->extra_out_nid
[i
]);
5664 mutex_lock(&codec
->spdif_mutex
);
5665 if (mout
->dig_out_nid
&& mout
->dig_out_used
== HDA_DIG_ANALOG_DUP
) {
5666 cleanup_dig_out_stream(codec
, mout
->dig_out_nid
);
5667 mout
->dig_out_used
= 0;
5669 mutex_unlock(&codec
->spdif_mutex
);
5672 EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_cleanup
);
5675 * snd_hda_get_default_vref - Get the default (mic) VREF pin bits
5676 * @codec: the HDA codec
5677 * @pin: referred pin NID
5679 * Guess the suitable VREF pin bits to be set as the pin-control value.
5680 * Note: the function doesn't set the AC_PINCTL_IN_EN bit.
5682 unsigned int snd_hda_get_default_vref(struct hda_codec
*codec
, hda_nid_t pin
)
5684 unsigned int pincap
;
5685 unsigned int oldval
;
5686 oldval
= snd_hda_codec_read(codec
, pin
, 0,
5687 AC_VERB_GET_PIN_WIDGET_CONTROL
, 0);
5688 pincap
= snd_hda_query_pin_caps(codec
, pin
);
5689 pincap
= (pincap
& AC_PINCAP_VREF
) >> AC_PINCAP_VREF_SHIFT
;
5690 /* Exception: if the default pin setup is vref50, we give it priority */
5691 if ((pincap
& AC_PINCAP_VREF_80
) && oldval
!= PIN_VREF50
)
5692 return AC_PINCTL_VREF_80
;
5693 else if (pincap
& AC_PINCAP_VREF_50
)
5694 return AC_PINCTL_VREF_50
;
5695 else if (pincap
& AC_PINCAP_VREF_100
)
5696 return AC_PINCTL_VREF_100
;
5697 else if (pincap
& AC_PINCAP_VREF_GRD
)
5698 return AC_PINCTL_VREF_GRD
;
5699 return AC_PINCTL_VREF_HIZ
;
5701 EXPORT_SYMBOL_GPL(snd_hda_get_default_vref
);
5704 * snd_hda_correct_pin_ctl - correct the pin ctl value for matching with the pin cap
5705 * @codec: the HDA codec
5706 * @pin: referred pin NID
5707 * @val: pin ctl value to audit
5709 unsigned int snd_hda_correct_pin_ctl(struct hda_codec
*codec
,
5710 hda_nid_t pin
, unsigned int val
)
5712 static unsigned int cap_lists
[][2] = {
5713 { AC_PINCTL_VREF_100
, AC_PINCAP_VREF_100
},
5714 { AC_PINCTL_VREF_80
, AC_PINCAP_VREF_80
},
5715 { AC_PINCTL_VREF_50
, AC_PINCAP_VREF_50
},
5716 { AC_PINCTL_VREF_GRD
, AC_PINCAP_VREF_GRD
},
5722 cap
= snd_hda_query_pin_caps(codec
, pin
);
5724 return val
; /* don't know what to do... */
5726 if (val
& AC_PINCTL_OUT_EN
) {
5727 if (!(cap
& AC_PINCAP_OUT
))
5728 val
&= ~(AC_PINCTL_OUT_EN
| AC_PINCTL_HP_EN
);
5729 else if ((val
& AC_PINCTL_HP_EN
) && !(cap
& AC_PINCAP_HP_DRV
))
5730 val
&= ~AC_PINCTL_HP_EN
;
5733 if (val
& AC_PINCTL_IN_EN
) {
5734 if (!(cap
& AC_PINCAP_IN
))
5735 val
&= ~(AC_PINCTL_IN_EN
| AC_PINCTL_VREFEN
);
5737 unsigned int vcap
, vref
;
5739 vcap
= (cap
& AC_PINCAP_VREF
) >> AC_PINCAP_VREF_SHIFT
;
5740 vref
= val
& AC_PINCTL_VREFEN
;
5741 for (i
= 0; i
< ARRAY_SIZE(cap_lists
); i
++) {
5742 if (vref
== cap_lists
[i
][0] &&
5743 !(vcap
& cap_lists
[i
][1])) {
5744 if (i
== ARRAY_SIZE(cap_lists
) - 1)
5745 vref
= AC_PINCTL_VREF_HIZ
;
5747 vref
= cap_lists
[i
+ 1][0];
5750 val
&= ~AC_PINCTL_VREFEN
;
5757 EXPORT_SYMBOL_GPL(snd_hda_correct_pin_ctl
);
5760 * _snd_hda_pin_ctl - Helper to set pin ctl value
5761 * @codec: the HDA codec
5762 * @pin: referred pin NID
5763 * @val: pin control value to set
5764 * @cached: access over codec pinctl cache or direct write
5766 * This function is a helper to set a pin ctl value more safely.
5767 * It corrects the pin ctl value via snd_hda_correct_pin_ctl(), stores the
5768 * value in pin target array via snd_hda_codec_set_pin_target(), then
5769 * actually writes the value via either snd_hda_codec_update_cache() or
5770 * snd_hda_codec_write() depending on @cached flag.
5772 int _snd_hda_set_pin_ctl(struct hda_codec
*codec
, hda_nid_t pin
,
5773 unsigned int val
, bool cached
)
5775 val
= snd_hda_correct_pin_ctl(codec
, pin
, val
);
5776 snd_hda_codec_set_pin_target(codec
, pin
, val
);
5778 return snd_hda_codec_update_cache(codec
, pin
, 0,
5779 AC_VERB_SET_PIN_WIDGET_CONTROL
, val
);
5781 return snd_hda_codec_write(codec
, pin
, 0,
5782 AC_VERB_SET_PIN_WIDGET_CONTROL
, val
);
5784 EXPORT_SYMBOL_GPL(_snd_hda_set_pin_ctl
);
5787 * snd_hda_add_imux_item - Add an item to input_mux
5788 * @codec: the HDA codec
5789 * @imux: imux helper object
5790 * @label: the name of imux item to assign
5791 * @index: index number of imux item to assign
5792 * @type_idx: pointer to store the resultant label index
5794 * When the same label is used already in the existing items, the number
5795 * suffix is appended to the label. This label index number is stored
5796 * to type_idx when non-NULL pointer is given.
5798 int snd_hda_add_imux_item(struct hda_codec
*codec
,
5799 struct hda_input_mux
*imux
, const char *label
,
5800 int index
, int *type_idx
)
5802 int i
, label_idx
= 0;
5803 if (imux
->num_items
>= HDA_MAX_NUM_INPUTS
) {
5804 codec_err(codec
, "hda_codec: Too many imux items!\n");
5807 for (i
= 0; i
< imux
->num_items
; i
++) {
5808 if (!strncmp(label
, imux
->items
[i
].label
, strlen(label
)))
5812 *type_idx
= label_idx
;
5814 snprintf(imux
->items
[imux
->num_items
].label
,
5815 sizeof(imux
->items
[imux
->num_items
].label
),
5816 "%s %d", label
, label_idx
);
5818 strlcpy(imux
->items
[imux
->num_items
].label
, label
,
5819 sizeof(imux
->items
[imux
->num_items
].label
));
5820 imux
->items
[imux
->num_items
].index
= index
;
5824 EXPORT_SYMBOL_GPL(snd_hda_add_imux_item
);
5833 static void hda_async_suspend(void *data
, async_cookie_t cookie
)
5835 hda_call_codec_suspend(data
, false);
5838 static void hda_async_resume(void *data
, async_cookie_t cookie
)
5840 hda_call_codec_resume(data
);
5844 * snd_hda_suspend - suspend the codecs
5847 * Returns 0 if successful.
5849 int snd_hda_suspend(struct hda_bus
*bus
)
5851 struct hda_codec
*codec
;
5852 ASYNC_DOMAIN_EXCLUSIVE(domain
);
5854 list_for_each_entry(codec
, &bus
->codec_list
, list
) {
5855 cancel_delayed_work_sync(&codec
->jackpoll_work
);
5856 if (hda_codec_is_power_on(codec
)) {
5857 if (bus
->num_codecs
> 1)
5858 async_schedule_domain(hda_async_suspend
, codec
,
5861 hda_call_codec_suspend(codec
, false);
5865 if (bus
->num_codecs
> 1)
5866 async_synchronize_full_domain(&domain
);
5870 EXPORT_SYMBOL_GPL(snd_hda_suspend
);
5873 * snd_hda_resume - resume the codecs
5876 * Returns 0 if successful.
5878 int snd_hda_resume(struct hda_bus
*bus
)
5880 struct hda_codec
*codec
;
5881 ASYNC_DOMAIN_EXCLUSIVE(domain
);
5883 list_for_each_entry(codec
, &bus
->codec_list
, list
) {
5884 if (bus
->num_codecs
> 1)
5885 async_schedule_domain(hda_async_resume
, codec
, &domain
);
5887 hda_call_codec_resume(codec
);
5890 if (bus
->num_codecs
> 1)
5891 async_synchronize_full_domain(&domain
);
5895 EXPORT_SYMBOL_GPL(snd_hda_resume
);
5896 #endif /* CONFIG_PM */
5903 * snd_array_new - get a new element from the given array
5904 * @array: the array object
5906 * Get a new element from the given array. If it exceeds the
5907 * pre-allocated array size, re-allocate the array.
5909 * Returns NULL if allocation failed.
5911 void *snd_array_new(struct snd_array
*array
)
5913 if (snd_BUG_ON(!array
->elem_size
))
5915 if (array
->used
>= array
->alloced
) {
5916 int num
= array
->alloced
+ array
->alloc_align
;
5917 int size
= (num
+ 1) * array
->elem_size
;
5919 if (snd_BUG_ON(num
>= 4096))
5921 nlist
= krealloc(array
->list
, size
, GFP_KERNEL
| __GFP_ZERO
);
5924 array
->list
= nlist
;
5925 array
->alloced
= num
;
5927 return snd_array_elem(array
, array
->used
++);
5929 EXPORT_SYMBOL_GPL(snd_array_new
);
5932 * snd_array_free - free the given array elements
5933 * @array: the array object
5935 void snd_array_free(struct snd_array
*array
)
5942 EXPORT_SYMBOL_GPL(snd_array_free
);
5945 * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer
5946 * @pcm: PCM caps bits
5947 * @buf: the string buffer to write
5948 * @buflen: the max buffer length
5950 * used by hda_proc.c and hda_eld.c
5952 void snd_print_pcm_bits(int pcm
, char *buf
, int buflen
)
5954 static unsigned int bits
[] = { 8, 16, 20, 24, 32 };
5957 for (i
= 0, j
= 0; i
< ARRAY_SIZE(bits
); i
++)
5958 if (pcm
& (AC_SUPPCM_BITS_8
<< i
))
5959 j
+= snprintf(buf
+ j
, buflen
- j
, " %d", bits
[i
]);
5961 buf
[j
] = '\0'; /* necessary when j == 0 */
5963 EXPORT_SYMBOL_GPL(snd_print_pcm_bits
);
5965 MODULE_DESCRIPTION("HDA codec core");
5966 MODULE_LICENSE("GPL");